]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/usb/ctu_usbcan.c
Slow down TX path if needed.
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / usb / ctu_usbcan.c
1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/module.h>
6 #include <linux/kref.h>
7 #include <linux/uaccess.h>
8 #include <linux/usb.h>
9 #include <linux/mutex.h>
10 #include <linux/netdevice.h>
11 #include <linux/version.h>
12 #include <linux/kthread.h>
13 #include <linux/freezer.h>
14
15 #include <linux/can.h>
16 #include <linux/can/dev.h>
17 #include <linux/can/error.h>
18
19 // USBCAN - new usb vendor definitions
20 #define USBCAN_VENDOR_SET_BITTIMING     (9)
21 #define USBCAN_VENDOR_GET_BITTIMING_CONST       (10)
22
23 #define CTU_USBCAN_VENDOR_ID    0x1669
24 #define CTU_USBCAN_PRODUCT_ID   0x1011
25
26 #define USBCAN_TOT_RX_URBS      8
27 #define USBCAN_TOT_TX_URBS      8
28
29 #define USBCAN_TRANSFER_SIZE    16
30 #define USBCAN_BITTIMING_SIZE   16
31 #define USBCAN_BITTIMING_CONST_SIZE     36
32
33 #define CAN_MSG_LENGTH  8
34
35 #define MSG_RTR   (1<<0)
36 #define MSG_OVR   (1<<1)
37 #define MSG_EXT   (1<<2)
38 #define MSG_LOCAL (1<<3)
39
40 #define USBCAN_DATA_OK  (1)
41 #define USBCAN_TX_PENDING       (2)
42 #define USBCAN_BITTIMING_CONST_SET      (3)
43
44
45 MODULE_LICENSE("GPL");
46
47 /* table of devices that work with this driver */
48 static struct usb_device_id ctu_usbcan_table [] = {
49         { USB_DEVICE(CTU_USBCAN_VENDOR_ID, CTU_USBCAN_PRODUCT_ID) },
50         { }                                     /* Terminating entry */
51 };
52 MODULE_DEVICE_TABLE(usb, ctu_usbcan_table);
53
54
55 static struct usb_driver ctu_usbcan_driver;
56 struct ctu_usbcan_usb;
57
58
59 struct usbcan_message {
60         struct urb      *u;
61         struct ctu_usbcan_usb *dev;
62         u8      msg[USBCAN_TRANSFER_SIZE];
63         u32 echo_index;
64         u8      dlc;
65         struct list_head list_node;
66 };
67 /*
68 Structure of byte array msg in struct usbcan_message that represents CAN message (little endian):
69         msg[0] - reserved (1 byte)
70         msg[1] - length (1 byte)
71         msg[2:3] - flags (2 bytes)
72         msg[4:7] - id (4 bytes)
73         msg[8:15] - data (8 bytes)
74
75 */
76
77 /* Structure to hold all of our device specific stuff */
78 struct ctu_usbcan_usb {
79
80         struct can_priv can; /* must be the first member */
81
82         struct can_bittiming_const btc;
83         u32 can_clock;
84
85         struct usb_device *udev;                        /* the usb device for this device */
86         struct net_device *netdev;
87
88         u8 bulk_in_endpointAddr;        /* the address of the bulk in endpoint */
89         u8 bulk_out_endpointAddr;       /* the address of the bulk out endpoint */
90
91         struct list_head        rx_pend_list;           /* URBs waiting for data receive */
92         struct list_head        rx_ready_list;          /* URBs with valid received data */
93         struct list_head        tx_idle_list;           /* URBs prepared to hold Tx messages */
94         struct list_head        tx_pend_list;           /* URBs holding Tx messages in progress */
95         struct list_head        tx_ready_list;          /* URBs with yet confirmed Tx messages */
96
97         spinlock_t              list_lock;                      /* list lock */
98         struct task_struct *comthread;                  /* usb communication kernel thread  */
99
100         volatile long flags;
101
102 };
103
104
105 static void usbcan_usb_message_move_list(struct ctu_usbcan_usb *dev,
106                         struct usbcan_message *m, struct list_head *head)
107 {
108         unsigned long flags;    
109         spin_lock_irqsave(&dev->list_lock, flags);
110         list_del(&m->list_node);
111         list_add_tail(&m->list_node, head);
112         spin_unlock_irqrestore(&dev->list_lock, flags);
113 }
114
115
116 static int get_bittiming_constants(struct ctu_usbcan_usb *dev)
117 {
118
119         int retval;
120         u8 *usbbuf;
121         u32 * ptr;
122
123         usbbuf = kzalloc(sizeof(u8)*USBCAN_BITTIMING_CONST_SIZE, GFP_KERNEL);
124
125         if(!usbbuf){
126                 err("Error allocating receive buffer for bittiming constants\n");
127                 return 1;
128         }
129         
130         retval = usb_control_msg(dev->udev,
131                 usb_rcvctrlpipe(dev->udev, 0),
132                 USBCAN_VENDOR_GET_BITTIMING_CONST,
133                 USB_TYPE_VENDOR,
134                 cpu_to_le16(0), cpu_to_le16(0),
135                 usbbuf, USBCAN_BITTIMING_CONST_SIZE,
136                 1000);
137
138         if(retval<0)
139                 goto exit;
140
141         ptr = (u32*) usbbuf;
142
143         dev->can_clock = le32_to_cpu(*(ptr++));
144         dev->btc.tseg1_min = le32_to_cpu(*(ptr++));
145         dev->btc.tseg1_max = le32_to_cpu(*(ptr++));
146         dev->btc.tseg2_min = le32_to_cpu(*(ptr++));
147         dev->btc.tseg2_max = le32_to_cpu(*(ptr++));
148         dev->btc.sjw_max = le32_to_cpu(*(ptr++));
149         dev->btc.brp_min = le32_to_cpu(*(ptr++));
150         dev->btc.brp_max = le32_to_cpu(*(ptr++));
151         dev->btc.brp_inc = le32_to_cpu(*(ptr));
152
153         set_bit(USBCAN_BITTIMING_CONST_SET,&dev->flags);
154
155
156 exit:
157
158         kfree(usbbuf);
159         if(retval<0)
160                 return retval;
161
162         return 0;
163
164 }
165
166 static int ctu_usbcan_set_mode(struct net_device *netdev, enum can_mode mode)
167 {
168         
169         printk("SET MODE\n");
170
171         return 0;
172 }
173
174 static int ctu_usbcan_set_bittiming(struct net_device *netdev)
175 {
176         struct ctu_usbcan_usb *dev = netdev_priv(netdev);
177         struct can_bittiming *bt = &dev->can.bittiming;
178         u8 *usbbuf;     
179         u32 * ptr;
180         int retval;
181         
182
183         if (!dev)
184                 return -ENODEV;
185
186
187         usbbuf = kzalloc(sizeof(u8)*USBCAN_BITTIMING_SIZE, GFP_KERNEL);
188
189         if(!usbbuf){
190                 err("Error allocating transmit buffer for set bittiming\n");
191                 return 1;
192         }       
193         
194         ptr = (u32*) usbbuf;
195
196         *(ptr++)=cpu_to_le32(bt->brp);          // baudrate prescaler
197         *(ptr++)=cpu_to_le32(bt->sjw);  // sjw
198         *(ptr++)=cpu_to_le32(bt->prop_seg + bt->phase_seg1);    // TSEG1
199         *(ptr)=cpu_to_le32(bt->phase_seg2);     // TSEG2
200
201
202         retval = usb_control_msg(dev->udev,
203                                 usb_sndctrlpipe(dev->udev, 0),
204                                 USBCAN_VENDOR_SET_BITTIMING,
205                                 USB_TYPE_VENDOR,
206                                 cpu_to_le16(0), cpu_to_le16(0),
207                                 usbbuf, USBCAN_BITTIMING_SIZE,
208                                 1000);
209
210         kfree(usbbuf);
211
212         if(retval){
213                 printk("BITRATE %d, BRP: %d, SJW: %d, TSEG1: %d, TSEG2: %d \n", bt->bitrate, bt->brp, bt->sjw,\
214                                 (bt->prop_seg + bt->phase_seg1), bt->phase_seg2);       
215                 return 0;
216         }
217         else{
218                 err("Could not set bittiming\n");
219                 return retval;
220         }
221
222 }
223
224
225 static void ctu_usbcan_tx_callback(struct urb *urb){
226
227         struct usbcan_message *m = urb->context;
228         struct net_device_stats *stats = &m->dev->netdev->stats;
229
230         if (!netif_device_present(m->dev->netdev))
231                 return;
232
233         if (urb->status != 0){
234                 err("TX callback: error");
235                 return;
236         }
237
238         /* success */
239
240         //printk("TX callback: URB successfully transmitted\n");
241
242         stats->tx_packets++;
243         stats->tx_bytes += m->dlc;
244
245         
246         can_get_echo_skb(m->dev->netdev, m->echo_index);
247
248         set_bit(USBCAN_DATA_OK,&m->dev->flags);
249         usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
250         wake_up_process(m->dev->comthread);
251
252 }
253
254 static void ctu_usbcan_rx_callback(struct urb *urb)
255 {
256         
257         struct usbcan_message *m = urb->context;
258         
259         if (!netif_device_present(m->dev->netdev))
260                 return;
261
262
263         if(urb->status != 0){
264                 err("RX callback: error");
265                 return;
266         }
267
268         /* success */
269         
270         //printk("RX callback: URB successfully received\n");
271
272         set_bit(USBCAN_DATA_OK,&m->dev->flags);
273         usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
274         wake_up_process(m->dev->comthread);
275
276 }
277
278 static netdev_tx_t ctu_usbcan_start_xmit(struct sk_buff *skb, struct net_device *netdev)
279 {
280         struct ctu_usbcan_usb *dev = netdev_priv(netdev);
281         struct can_frame *cf = (struct can_frame *)skb->data;
282         struct usbcan_message *m;
283         int i, retval, len;
284         u8 *ptr;
285         u16 flags = 0;
286         
287
288         if(list_empty(&dev->tx_idle_list)){
289                 goto exit;
290         }
291
292         m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
293
294         /* fill */
295
296         len = cf->can_dlc;
297         if(len > CAN_MSG_LENGTH)
298                 len = CAN_MSG_LENGTH;
299
300         if (cf->can_id & CAN_RTR_FLAG)
301                 flags |= MSG_RTR;
302
303         if (cf->can_id & CAN_EFF_FLAG)
304                 flags |= MSG_EXT;
305
306         *(u8 *)(m->msg)=0;
307         *(u8 *)(m->msg+1)=len & 0xFF;
308         *(u16 *)(m->msg+2)=cpu_to_le16(flags);
309         *(u32 *)(m->msg+4)=cpu_to_le32(cf->can_id & CAN_ERR_MASK);
310
311         for(ptr=m->msg+8, i=0; i < len; ptr++,i++)
312                 *ptr= cf->data[i] & 0xFF;
313                 
314         for(; i < 8; ptr++,i++)
315                 *ptr=0;
316
317
318         m->dlc = (u8) len;
319
320         usbcan_usb_message_move_list(dev, m, &dev->tx_pend_list);
321
322         /* put on stack - local echo */
323         can_put_echo_skb(skb, netdev, m->echo_index);
324
325         /* send*/ 
326         retval = usb_submit_urb(m->u, GFP_ATOMIC);
327         if (retval){
328                 err("Error submitting URB: %d", retval);
329                 can_free_echo_skb(netdev, m->echo_index);
330                 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
331                 dev_kfree_skb(skb);
332                 goto exit;
333         }
334
335         /* slow down tx path if needed */
336         if(list_empty(&dev->tx_idle_list))
337                 netif_stop_queue(netdev);
338
339
340
341 exit:
342
343         return NETDEV_TX_OK;
344
345 }
346
347 static void usbcan_kthread_free_urbs(struct ctu_usbcan_usb *dev)
348 {
349         while(!list_empty(&dev->rx_pend_list)) {
350                 struct usbcan_message *m;
351                 m = list_first_entry(&dev->rx_pend_list, typeof(*m), list_node);
352                 usb_kill_urb(m->u);
353                 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
354         }
355
356         while(!list_empty(&dev->tx_pend_list)) {
357                 struct usbcan_message *m;
358                 m = list_first_entry(&dev->tx_pend_list, typeof(*m), list_node);
359                 usb_kill_urb(m->u);
360                 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
361         }
362
363         while(!list_empty(&dev->rx_ready_list)) {
364                 struct usbcan_message *m;
365                 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
366                 list_del(&m->list_node);
367                 usb_free_urb(m->u);
368                 kfree(m);
369         }
370
371         while(!list_empty(&dev->tx_ready_list)) {
372                 struct usbcan_message *m;
373                 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
374                 list_del(&m->list_node);
375                 usb_free_urb(m->u);
376                 kfree(m);
377         }
378
379         while(!list_empty(&dev->tx_idle_list)) {
380                 struct usbcan_message *m;
381                 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
382                 list_del(&m->list_node);
383                 usb_free_urb(m->u);
384                 kfree(m);
385         }
386
387
388 }
389
390 void usbcan_kthread_write_handler(struct ctu_usbcan_usb *dev, struct usbcan_message *m)
391 {
392
393         usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
394
395         if (netif_queue_stopped(m->dev->netdev))
396                 netif_wake_queue(m->dev->netdev);
397
398 }
399
400 void usbcan_kthread_read_handler(struct ctu_usbcan_usb *dev, struct usbcan_message *m)
401 {
402
403         struct can_frame *cf;
404         struct sk_buff *skb;
405         u8 *ptr;
406         int i, len, retval;
407         u16 flags = 0;
408         struct net_device_stats *stats = &m->dev->netdev->stats;
409
410
411 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32)
412         skb = alloc_can_skb(m->dev->netdev, &cf);
413 #else
414         skb = netdev_alloc_skb(m->dev->netdev, sizeof(struct can_frame)); 
415         skb->protocol = htons(ETH_P_CAN);
416         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
417 #endif
418
419         if (skb == NULL){
420                 err("RX: error alloc skb\n");
421                 return;
422         }
423
424         
425         //printk("RX: URB successfully received\n");
426
427         len=*(u8 *)(m->msg+1);
428         if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
429
430         flags = le16_to_cpu(*(u16 *)(m->msg+2));
431         cf->can_id = le32_to_cpu((*(u32 *)(m->msg+4)));
432
433         if (flags & MSG_RTR)
434                 cf->can_id |= CAN_RTR_FLAG;
435
436         if (flags & MSG_EXT)
437                 cf->can_id |= CAN_EFF_FLAG;
438
439         cf->can_dlc = len;
440
441         for(ptr=m->msg+8,i=0; i < len; ptr++,i++) {
442                 cf->data[i]=*ptr;
443         }
444
445
446         netif_rx(skb);
447
448         stats->rx_packets++;
449         stats->rx_bytes += cf->can_dlc;
450
451         /* Renewing RX urb */
452
453         usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
454         retval = usb_submit_urb (m->u, GFP_KERNEL);
455         if (retval<0) {
456                 err("URB error %d\n", retval);
457         }
458
459
460 }
461
462
463 static int usbcan_sleep_thread(struct ctu_usbcan_usb *dev)
464 {
465         int     rc = 0;
466
467         /* Wait until a signal arrives or we are woken up */
468         for (;;) {
469                 try_to_freeze();
470                 set_current_state(TASK_INTERRUPTIBLE);
471                 if (signal_pending(current)) {
472                         rc = -EINTR;
473                         break;
474                 }
475                 if (
476                         kthread_should_stop() ||
477                         test_bit(USBCAN_DATA_OK,&dev->flags)
478                 )
479                         break;
480                 schedule();
481         }
482         __set_current_state(TASK_RUNNING);
483         return rc;
484 }
485
486 int usbcan_kthread(void *data)
487 {
488
489         struct ctu_usbcan_usb *dev=(struct ctu_usbcan_usb *) data;
490
491         int i, retval;
492
493         struct usbcan_message *m;
494         struct urb *u;
495
496         printk("CTU USBCAN: kthread running\n");
497
498         INIT_LIST_HEAD(&dev->rx_pend_list);
499         INIT_LIST_HEAD(&dev->rx_ready_list);
500         INIT_LIST_HEAD(&dev->tx_idle_list);
501         INIT_LIST_HEAD(&dev->tx_pend_list);
502         INIT_LIST_HEAD(&dev->tx_ready_list);
503
504         /* Prepare receive urbs  */
505         for (i=0;i<USBCAN_TOT_RX_URBS;i++){
506                 
507                 u = usb_alloc_urb(0, GFP_KERNEL);               
508                 m = kzalloc(sizeof(struct usbcan_message), GFP_KERNEL);
509                 
510                 if (!u){
511                         err("Error allocating usb receive urb");
512                         goto exit;
513                 }
514
515                 if(!m) {
516                         usb_free_urb(u);
517                         err("Error allocating receive usbcan_message");
518                         goto exit;
519                 }
520                 m->u = u;
521                 u->dev = dev->udev;
522                 m->dev = dev;
523                 usb_fill_bulk_urb(u, dev->udev,
524                         usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
525                         m->msg, USBCAN_TRANSFER_SIZE, ctu_usbcan_rx_callback, m);
526
527
528                 list_add_tail(&m->list_node, &dev->rx_ready_list);
529         }
530
531         /* Prepare transmit urbs  */
532         for (i=0;i<USBCAN_TOT_TX_URBS;i++){
533
534                 u = usb_alloc_urb(0, GFP_KERNEL);
535                 m = kzalloc(sizeof(struct usbcan_message), GFP_KERNEL);
536
537                 if (!u){
538                         err("Error allocating usb transmit urb");
539                         goto exit;
540                 }
541         
542                 if(!m) {
543                         usb_free_urb(u);
544                         err("Error allocating transmit usbcan_message");
545                         goto exit;
546                 }
547                 m->u = u;
548                 u->dev = dev->udev;
549                 m->dev = dev;
550
551                 m->echo_index = i;
552
553                 usb_fill_bulk_urb(u, dev->udev,
554                         usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
555                         m->msg, USBCAN_TRANSFER_SIZE, ctu_usbcan_tx_callback, m);
556
557                 list_add_tail(&m->list_node, &dev->tx_idle_list);
558         }
559
560
561         for (i=0;i<USBCAN_TOT_RX_URBS;i++){
562                 
563                 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
564                 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
565
566                 retval=usb_submit_urb(m->u, GFP_KERNEL);
567                 if (retval){
568                         err("Error submitting URB: %d", retval);
569                         goto exit;
570                 }
571         }
572
573
574
575         /* an endless loop in which we are doing our work */
576         for(;;)
577         {
578
579
580                 /* We need to do a memory barrier here to be sure that
581                 the flags are visible on all CPUs. */
582                 mb();
583
584                 /* fall asleep */
585                 if (!kthread_should_stop() && (usbcan_sleep_thread(dev)<0)){
586                         break;
587                 }
588                 /* We need to do a memory barrier here to be sure that the flags are visible on all CPUs. */
589                 mb();
590
591                 if (kthread_should_stop()){
592                         break;
593                 }
594
595                 mb();
596
597                 clear_bit(USBCAN_DATA_OK,&dev->flags);
598
599                 
600                 while(!list_empty(&dev->rx_ready_list)) {
601                         struct usbcan_message *m;
602                         m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
603                         usbcan_kthread_read_handler(dev, m);
604                 }
605
606                 while(!list_empty(&dev->tx_ready_list)) {
607                         struct usbcan_message *m;
608                         m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
609                         usbcan_kthread_write_handler(dev, m);
610                 }
611
612         }
613
614
615 exit:
616         usbcan_kthread_free_urbs(dev);
617         printk("CTU USBCAN: usbcan thread finished\n");
618         
619         return 0;
620
621 }
622
623 static int ctu_usbcan_open(struct net_device *netdev)
624 {
625         int err;
626
627         struct ctu_usbcan_usb *dev = netdev_priv(netdev);
628
629         /* common open */       
630         err = open_candev(netdev);
631         if (err)
632                 return err;     
633
634         if (netif_queue_stopped(netdev))
635                         netif_wake_queue(netdev);
636
637         /* start kernel thread */
638         dev->comthread = kthread_run(usbcan_kthread, (void *)dev, "usbcan_1");
639
640         return 0;
641 }
642
643 static int ctu_usbcan_close(struct net_device *netdev)
644 {
645
646         struct ctu_usbcan_usb *dev = netdev_priv(netdev);
647         kthread_stop(dev->comthread);
648
649         netif_stop_queue(netdev);
650
651         close_candev(netdev);
652         
653         return 0;
654 }
655
656 static const struct net_device_ops ctu_usbcan_netdev_ops = {
657         .ndo_open = ctu_usbcan_open,
658         .ndo_stop = ctu_usbcan_close,
659         .ndo_start_xmit = ctu_usbcan_start_xmit,
660 };
661
662
663 static int ctu_usbcan_probe(struct usb_interface *intf,
664                                                         const struct usb_device_id *id)
665 {
666         struct net_device *netdev;
667         struct ctu_usbcan_usb *dev;
668         struct usb_host_interface *iface_desc;
669         struct usb_endpoint_descriptor *endpoint;
670         int i;
671         int err = -ENOMEM;
672
673         printk(KERN_INFO "CTU USBCAN device now attached\n");
674
675
676 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32)
677         netdev = alloc_candev(sizeof(struct ctu_usbcan_usb), USBCAN_TOT_TX_URBS);
678 #else
679         netdev = alloc_candev(sizeof(struct ctu_usbcan_usb));
680 #endif
681
682
683         if (!netdev) {
684 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,33)
685                 dev_err(&intf->dev, "Couldn't alloc candev\n");
686 #else
687                 dev_err(netdev->dev.parent, "Couldn't alloc candev\n");
688 #endif
689                 return -ENOMEM;
690         }
691
692         dev = netdev_priv(netdev);
693
694         dev->udev = interface_to_usbdev(intf);
695         dev->netdev = netdev;
696
697
698         netdev->netdev_ops = &ctu_usbcan_netdev_ops;
699         netdev->flags |= IFF_ECHO;
700
701
702         /* set up the endpoint information */
703         /* use only the first bulk-in and bulk-out endpoints */
704         iface_desc = intf->cur_altsetting;
705         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
706                 endpoint = &iface_desc->endpoint[i].desc;
707
708                 if (!dev->bulk_in_endpointAddr &&
709                     usb_endpoint_is_bulk_in(endpoint)) {
710                         /* we found a bulk in endpoint */
711                         dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
712                 }
713
714                 if (!dev->bulk_out_endpointAddr &&
715                     usb_endpoint_is_bulk_out(endpoint)) {
716                         /* we found a bulk out endpoint */
717                         dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
718                 }
719         }
720         if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
721                 err("Could not find both bulk-in and bulk-out endpoints");
722                 goto exit;
723         }
724
725
726         usb_set_intfdata(intf, dev);
727         SET_NETDEV_DEV(netdev, &intf->dev);
728
729         if (get_bittiming_constants(dev)){
730                 err("Could not get bittiming constants\n");
731                 goto exit;
732         }
733
734         dev->can.clock.freq = dev->can_clock;
735         dev->can.bittiming_const = &dev->btc;
736         dev->can.do_set_bittiming = ctu_usbcan_set_bittiming;
737         dev->can.do_set_mode = ctu_usbcan_set_mode;
738
739
740         err = register_candev(netdev);
741         if (err) {
742                 dev_err(netdev->dev.parent,
743                         "couldn't register CAN device: %d\n", err);
744         }
745
746 exit:
747
748         return 0;
749 }
750
751 /* called by the usb core when the device is removed from the system */
752 static void ctu_usbcan_disconnect(struct usb_interface *intf)
753 {
754
755         struct ctu_usbcan_usb *dev = usb_get_intfdata(intf);
756
757         printk(KERN_INFO "CTU USBCAN device now disconnected\n");
758
759         usb_set_intfdata(intf, NULL);
760
761         if (dev) {
762                 if(test_bit(USBCAN_BITTIMING_CONST_SET,&dev->flags))            
763                         unregister_netdev(dev->netdev);
764
765                 free_candev(dev->netdev);
766                 clear_bit(USBCAN_BITTIMING_CONST_SET,&dev->flags);
767         }
768
769 }
770
771 /* usb specific object needed to register this driver with the usb subsystem */
772 static struct usb_driver ctu_usbcan_driver = {
773         .name = "ctu_usbcan",
774         .id_table =     ctu_usbcan_table,
775         .probe =        ctu_usbcan_probe,
776         .disconnect =   ctu_usbcan_disconnect,
777 };
778
779 static int __init ctu_usbcan_init(void)
780 {
781         int result;
782
783         printk(KERN_INFO "CTU USBCAN kernel driver loaded\n");
784
785         /* register this driver with the USB subsystem */
786         result = usb_register(&ctu_usbcan_driver);
787         if (result)
788                 err("usb_register failed. Error number %d", result);
789
790         return result;
791 }
792
793 static void __exit ctu_usbcan_exit(void)
794 {
795         printk(KERN_INFO "CTU USBCAN kernel driver unloaded\n");
796
797         /* deregister this driver with the USB subsystem */
798         usb_deregister(&ctu_usbcan_driver);
799 }
800
801 module_init(ctu_usbcan_init);
802 module_exit(ctu_usbcan_exit);
803