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