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