]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/usbcan.c
91807f77a669db1d2e5a8e4e248255adb5ca4952
[lincan.git] / lincan / src / usbcan.c
1 /* usbcan.h
2  * Header file for the Linux CAN-bus driver.
3  * Written by Jan Kriz email:johen@post.cz
4  * This software is released under the GPL-License.
5  * Version lincan-0.3  17 Jul 2008
6  */
7
8 #include "../include/can.h"
9 #include "../include/can_sysdep.h"
10 #include "../include/main.h"
11 #include "../include/devcommon.h"
12 #include "../include/setup.h"
13 #include "../include/usbcan.h"
14
15 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
16 static void usbcan_disconnect(struct usb_interface *interface);
17
18 /* table of devices that work with this driver */
19 static struct usb_device_id usbcan_table [] = {
20         { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
21         { }                                     /* Terminating entry */
22 };
23 MODULE_DEVICE_TABLE(usb, usbcan_table);
24
25 static struct usb_driver usbcan_driver = {
26         .name =         "usbcan",
27         .id_table = usbcan_table,
28         .probe =        usbcan_probe,
29         .disconnect =   usbcan_disconnect,
30 };
31
32 /**
33  * usbcan_request_io: - reserve io or memory range for can board
34  * @candev: pointer to candevice/board which asks for io. Field @io_addr
35  *      of @candev is used in most cases to define start of the range
36  *
37  * The function usbcan_request_io() is used to reserve the io-memory. If your
38  * hardware uses a dedicated memory range as hardware control registers you
39  * will have to add the code to reserve this memory as well.
40  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
41  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
42  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
43  * Return Value: The function returns zero on success or %-ENODEV on failure
44  * File: src/usbcan.c
45  */
46 int usbcan_request_io(struct candevice_t *candev)
47 {
48         struct usbcan_usb *dev = (struct usbcan_usb*)candev->sysdevptr.anydev;
49
50         if (!dev){
51                 CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
52                 CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
53                 return -ENODEV;
54         }
55
56         /* start kernel thread */
57         dev->rcvthread.arg = dev;
58         start_kthread(usbcan_read_kthread, &dev->rcvthread);
59
60         /* Adding link to can device into usbcan_usb struct */
61         ((struct usbcan_usb*)candev->sysdevptr.anydev)->candev=candev;
62         return 0;
63 }
64
65 /**
66  * usbcan_release_io - free reserved io memory range
67  * @candev: pointer to candevice/board which releases io
68  *
69  * The function usbcan_release_io() is used to free reserved io-memory.
70  * In case you have reserved more io memory, don't forget to free it here.
71  * IO_RANGE is the io-memory range that gets released, please adjust according
72  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
73  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
74  * Return Value: The function always returns zero
75  * File: src/usbcan.c
76  */
77 int usbcan_release_io(struct candevice_t *candev)
78 {
79         struct usbcan_usb *dev = ((struct usbcan_usb*)candev->sysdevptr.anydev);
80
81         /* terminate the kernel thread */
82         if (dev->rcv){
83                 usb_kill_urb(dev->rcv);
84                 usb_free_urb(dev->rcv);
85         }
86         stop_kthread(&dev->rcvthread);
87         return 0;
88 }
89
90 /**
91  * usbcan_reset - hardware reset routine
92  * @candev: Pointer to candevice/board structure
93  *
94  * The function usbcan_reset() is used to give a hardware reset. This is
95  * rather hardware specific so I haven't included example code. Don't forget to
96  * check the reset status of the chip before returning.
97  * Return Value: The function returns zero on success or %-ENODEV on failure
98  * File: src/usbcan.c
99  */
100 int usbcan_reset(struct candevice_t *candev)
101 {
102         return 0;
103 }
104
105 /**
106  * usbcan_init_hw_data - Initialize hardware cards
107  * @candev: Pointer to candevice/board structure
108  *
109  * The function usbcan_init_hw_data() is used to initialize the hardware
110  * structure containing information about the installed CAN-board.
111  * %RESET_ADDR represents the io-address of the hardware reset register.
112  * %NR_82527 represents the number of Intel 82527 chips on the board.
113  * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
114  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
115  * the hardware uses programmable interrupts.
116  * Return Value: The function always returns zero
117  * File: src/usbcan.c
118  */
119 int usbcan_init_hw_data(struct candevice_t *candev)
120 {
121         candev->res_addr=RESET_ADDR;
122         candev->nr_82527_chips=0;
123         candev->nr_sja1000_chips=0;
124         candev->nr_all_chips=1;
125         candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
126
127         return 0;
128 }
129
130 /**
131  * usbcan_init_obj_data - Initialize message buffers
132  * @chip: Pointer to chip specific structure
133  * @objnr: Number of the message buffer
134  *
135  * The function usbcan_init_obj_data() is used to initialize the hardware
136  * structure containing information about the different message objects on the
137  * CAN chip. In case of the sja1000 there's only one message object but on the
138  * i82527 chip there are 15.
139  * The code below is for a i82527 chip and initializes the object base addresses
140  * The entry @obj_base_addr represents the first memory address of the message
141  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
142  * base address.
143  * Unless the hardware uses a segmented memory map, flags can be set zero.
144  * Return Value: The function always returns zero
145  * File: src/usbcan.c
146  */
147 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
148 {
149         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
150
151         return 0;
152 }
153
154 /**
155  * usbcan_program_irq - program interrupts
156  * @candev: Pointer to candevice/board structure
157  *
158  * The function usbcan_program_irq() is used for hardware that uses
159  * programmable interrupts. If your hardware doesn't use programmable interrupts
160  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
161  * leave this function unedited. Again this function is hardware specific so
162  * there's no example code.
163  * Return value: The function returns zero on success or %-ENODEV on failure
164  * File: src/usbcan.c
165  */
166 int usbcan_program_irq(struct candevice_t *candev)
167 {
168         return 0;
169 }
170
171 /* !!! Don't change this function !!! */
172 int usbcan_register(struct hwspecops_t *hwspecops)
173 {
174         hwspecops->request_io = usbcan_request_io;
175         hwspecops->release_io = usbcan_release_io;
176         hwspecops->reset = usbcan_reset;
177         hwspecops->init_hw_data = usbcan_init_hw_data;
178         hwspecops->init_chip_data = usbcan_init_chip_data;
179         hwspecops->init_obj_data = usbcan_init_obj_data;
180         hwspecops->write_register = NULL;
181         hwspecops->read_register = NULL;
182         hwspecops->program_irq = usbcan_program_irq;
183         return 0;
184 }
185
186 // static int sja1000_report_error_limit_counter;
187
188 static void sja1000_report_error(struct canchip_t *chip,
189                                 unsigned sr, unsigned ir, unsigned ecc)
190 {
191         /*TODO : Error reporting from device */
192
193 /*      if(sja1000_report_error_limit_counter>=100)
194                 return;
195
196         CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
197                 sr, ir, ecc);
198
199         sja1000_report_error_limit_counter+=10;
200
201         if(sja1000_report_error_limit_counter>=100){
202                 sja1000_report_error_limit_counter+=10;
203                 CANMSG("Error: too many errors, reporting disabled\n");
204                 return;
205         }
206
207 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
208         CANMSG("SR: BS=%c  ES=%c  TS=%c  RS=%c  TCS=%c TBS=%c DOS=%c RBS=%c\n",
209                 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
210                 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
211                 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
212                 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
213         CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c  TI=%c  RI=%c\n",
214                 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
215                 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
216                 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
217                 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
218         if((sr&sjaIR_EI) || 1){
219                 CANMSG("EI: %s %s %s\n",
220                        sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
221                        ecc&sjaECC_DIR?"RX":"TX",
222                        sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
223                       );
224         }
225 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
226 }
227
228
229 /**
230  * usbcan_enable_configuration - enable chip configuration mode
231  * @chip: pointer to chip state structure
232  */
233 int usbcan_enable_configuration(struct canchip_t *chip)
234 {
235         return 0;
236 }
237
238 /**
239  * usbcan_disable_configuration - disable chip configuration mode
240  * @chip: pointer to chip state structure
241  */
242 int usbcan_disable_configuration(struct canchip_t *chip)
243 {
244         return 0;
245 }
246
247 /**
248  * usbcan_chip_config: - can chip configuration
249  * @chip: pointer to chip state structure
250  *
251  * This function configures chip and prepares it for message
252  * transmission and reception. The function resets chip,
253  * resets mask for acceptance of all messages by call to
254  * usbcan_extended_mask() function and then
255  * computes and sets baudrate with use of function usbcan_baud_rate().
256  * Return Value: negative value reports error.
257  * File: src/usbcan.c
258  */
259 int usbcan_chip_config(struct canchip_t *chip)
260 {
261         return 0;
262 }
263
264 /**
265  * usbcan_extended_mask: - setup of extended mask for message filtering
266  * @chip: pointer to chip state structure
267  * @code: can message acceptance code
268  * @mask: can message acceptance mask
269  *
270  * Return Value: negative value reports error.
271  * File: src/usbcan.c
272  */
273 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  long mask)
274 {
275         int retval;
276         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
277
278         u8 usbbuf[16];
279
280         *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
281         *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
282
283         retval=usb_control_msg(dev->udev,
284                 usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
285                 USBCAN_VENDOR_EXT_MASK_SET,
286                 USB_TYPE_VENDOR,
287                 0, chip->chip_idx,
288                 &usbbuf, 16,
289                 10000);
290         if (retval<0)
291                 return -ENODEV;
292
293         retval = usb_control_msg(dev->udev,
294                 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
295                 USBCAN_VENDOR_EXT_MASK_STATUS,
296                 USB_TYPE_VENDOR,
297                 0, chip->chip_idx,
298                 &usbbuf, 16,
299                 10000);
300
301         if (retval==1){
302                 if(usbbuf[0]==1){
303                         DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
304                         DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
305                         return 0;
306                 }
307         }
308
309         CANMSG("Setting extended mask failed\n");
310         return -EINVAL;
311 }
312
313 /**
314  * usbcan_baud_rate: - set communication parameters.
315  * @chip: pointer to chip state structure
316  * @rate: baud rate in Hz
317  * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
318  * @sjw: synchronization jump width (0-3) prescaled clock cycles
319  * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
320  * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
321  *
322  * Return Value: negative value reports error.
323  * File: src/usbcan.c
324  */
325 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
326                                                         int sampl_pt, int flags)
327 {
328         int retval;
329         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
330
331         u8 usbbuf[16];
332
333         *(int32_t *)(usbbuf)=cpu_to_le32(rate);
334         *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
335         *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
336         *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
337
338         retval=usb_control_msg(dev->udev,
339                 usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
340                 USBCAN_VENDOR_BAUD_RATE_SET,
341                 USB_TYPE_VENDOR,
342                 0, chip->chip_idx,
343                 &usbbuf, 16,
344                 10000);
345         if (retval<0)
346                 return -ENODEV;
347
348         retval = usb_control_msg(dev->udev,
349                 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
350                 USBCAN_VENDOR_BAUD_RATE_STATUS,
351                 USB_TYPE_VENDOR,
352                 0, chip->chip_idx,
353                 usbbuf, 16,
354                 10000);
355
356         if (retval==1){
357                 if(usbbuf[0]==1)
358                         return 0;
359         }
360
361         CANMSG("baud rate %d is not possible to set\n",
362                 rate);
363         return -EINVAL;
364 }
365
366 /**
367  * usbcan_pre_read_config: - prepares message object for message reception
368  * @chip: pointer to chip state structure
369  * @obj: pointer to message object state structure
370  *
371  * Return Value: negative value reports error.
372  *      Positive value indicates immediate reception of message.
373  * File: src/usbcan.c
374  */
375 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
376 {
377         return 0;
378 }
379
380 #define MAX_TRANSMIT_WAIT_LOOPS 10
381 /**
382  * usbcan_pre_write_config: - prepares message object for message transmission
383  * @chip: pointer to chip state structure
384  * @obj: pointer to message object state structure
385  * @msg: pointer to CAN message
386  *
387  * This function prepares selected message object for future initiation
388  * of message transmission by usbcan_send_msg() function.
389  * The CAN message data and message ID are transfered from @msg slot
390  * into chip buffer in this function.
391  * Return Value: negative value reports error.
392  * File: src/usbcan.c
393  */
394 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
395                                                         struct canmsg_t *msg)
396 {
397         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
398         int i=0;
399         int len;
400         u8 *ptr;
401
402         /* Wait until Transmit Buffer Status is released */
403         while ( usbcan_chip_queue_status(chip) &&
404                                                 i++<MAX_TRANSMIT_WAIT_LOOPS) {
405                 udelay(i);
406         }
407         if (usbcan_chip_queue_status(chip)){
408                 CANMSG("Buffer full, cannot send message\n");
409                 return -EIO;
410         }
411
412         *(uint8_t *)(dev->tx_msg)=chip->chip_idx & 0xFF;
413
414         len = msg->length;
415         if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
416
417         *(uint8_t *)(dev->tx_msg+1)=len & 0xFF;
418         *(uint16_t *)(dev->tx_msg+2)=cpu_to_le16(msg->flags);
419         *(uint32_t *)(dev->tx_msg+4)=cpu_to_le32(msg->id);
420
421         for(ptr=dev->tx_msg+8,i=0; i < len; ptr++,i++) {
422                 *ptr=msg->data[i] & 0xFF;
423         }
424         for(; i < 8; ptr++,i++) {
425                 *ptr=0;
426         }
427         return 0;
428 }
429
430 /**
431  * usbcan_send_msg: - initiate message transmission
432  * @chip: pointer to chip state structure
433  * @obj: pointer to message object state structure
434  * @msg: pointer to CAN message
435  *
436  * This function is called after usbcan_pre_write_config() function,
437  * which prepares data in chip buffer.
438  * Return Value: negative value reports error.
439  * File: src/usbcan.c
440  */
441 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
442                                                         struct canmsg_t *msg)
443 {
444         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
445         int len,retval;
446
447         set_bit(USBCAN_TX_PENDING,&dev->flags);
448         retval=usb_bulk_msg(dev->udev,
449                         usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
450                         &dev->tx_msg, 16,
451                         &len,10000);
452         clear_bit(USBCAN_TX_PENDING,&dev->flags);
453         if (retval){
454                 CANMSG("URB error %d\n",retval);
455                 return -EIO;
456         }
457         if (len!=16){
458                 CANMSG("CAN message not sent\n");
459                 return -EIO;
460         }
461
462         return 0;
463 }
464
465 /**
466  * usbcan_check_tx_stat: - checks state of transmission engine
467  * @chip: pointer to chip state structure
468  *
469  * Return Value: negative value reports error.
470  *      Positive return value indicates transmission under way status.
471  *      Zero value indicates finishing of all issued transmission requests.
472  * File: src/usbcan.c
473  */
474 int usbcan_check_tx_stat(struct canchip_t *chip)
475 {
476         if (test_bit(USBCAN_TX_PENDING,&((struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev)->flags))
477                 return 1;
478         return 0;
479 }
480
481 /**
482  * usbcan_set_btregs: -  configures bitrate registers
483  * @chip: pointer to chip state structure
484  * @btr0: bitrate register 0
485  * @btr1: bitrate register 1
486  *
487  * Return Value: negative value reports error.
488  * File: src/usbcan.c
489  */
490 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
491                                                         unsigned short btr1)
492 {
493         int retval;
494         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
495         uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
496
497         retval = usb_control_msg(dev->udev,
498         usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
499         USBCAN_VENDOR_SET_BTREGS,
500         USB_TYPE_VENDOR,
501         cpu_to_le16(value), chip->chip_idx,
502         dev->ctl_in_buffer, dev->ctl_in_size,
503         10000);
504
505         if (retval==1){
506                 if(dev->ctl_in_buffer[0]==1)
507                         return 0;
508         }
509         return -ENODEV;
510 }
511
512 /**
513  * usbcan_start_chip: -  starts chip message processing
514  * @chip: pointer to chip state structure
515  *
516  * Return Value: negative value reports error.
517  * File: src/usbcan.c
518  */
519 int usbcan_start_chip(struct canchip_t *chip)
520 {
521         int retval;
522         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
523
524         retval = usb_control_msg(dev->udev,
525         usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
526         USBCAN_VENDOR_START_CHIP,
527         USB_TYPE_VENDOR,
528         0, chip->chip_idx,
529         dev->ctl_in_buffer, dev->ctl_in_size,
530         10000);
531
532         if (retval==1){
533                 if(dev->ctl_in_buffer[0]==1)
534                         return 0;
535         }
536         return -ENODEV;
537 }
538
539 /**
540  * usbcan_chip_queue_status: -  gets queue status from usb device
541  * @chip: pointer to chip state structure
542  *
543  * Return Value: negative value reports error.
544  * 0 means queue is not full
545  * 1 means queue is full
546  * File: src/usbcan.c
547  */
548 int usbcan_chip_queue_status(struct canchip_t *chip)
549 {
550         int retval;
551         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
552
553         retval = usb_control_msg(dev->udev,
554         usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
555         USBCAN_VENDOR_CHECK_TX_STAT,
556         USB_TYPE_VENDOR,
557         0, chip->chip_idx,
558         dev->ctl_in_buffer, dev->ctl_in_size,
559         10000);
560
561         if (retval==1){
562                 if(dev->ctl_in_buffer[0]==1)
563                         return 0;
564                 if(dev->ctl_in_buffer[0]==0)
565                         return 1;
566         }
567         return -ENODEV;
568 }
569
570 /**
571  * usbcan_stop_chip: -  stops chip message processing
572  * @chip: pointer to chip state structure
573  *
574  * Return Value: negative value reports error.
575  * File: src/usbcan.c
576  */
577 int usbcan_stop_chip(struct canchip_t *chip)
578 {
579         int retval;
580         struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
581
582         retval = usb_control_msg(dev->udev,
583         usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
584         USBCAN_VENDOR_STOP_CHIP,
585         USB_TYPE_VENDOR,
586         0, chip->chip_idx,
587         dev->ctl_in_buffer, dev->ctl_in_size,
588         10000);
589
590         if (retval==1){
591                 if(dev->ctl_in_buffer[0]==1)
592                         return 0;
593         }
594         return -ENODEV;
595 }
596
597 /**
598  * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
599  * @chip: pointer to chip state structure
600  *
601  * Return Value: negative value reports error.
602  * File: src/usbcan.c
603  */
604 int usbcan_attach_to_chip(struct canchip_t *chip)
605 {
606         return 0;
607 }
608
609 /**
610  * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
611  * @chip: pointer to chip state structure
612  *
613  * Return Value: negative value reports error.
614  * File: src/usbcan.c
615  */
616 int usbcan_release_chip(struct canchip_t *chip)
617 {
618         usbcan_stop_chip(chip);
619         return 0;
620 }
621
622 /**
623  * usbcan_remote_request: - configures message object and asks for RTR message
624  * @chip: pointer to chip state structure
625  * @obj: pointer to message object structure
626  *
627  * Return Value: negative value reports error.
628  * File: src/usbcan.c
629  */
630 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
631 {
632         CANMSG("usbcan_remote_request not implemented\n");
633         return -ENOSYS;
634 }
635
636 /**
637  * usbcan_standard_mask: - setup of mask for message filtering
638  * @chip: pointer to chip state structure
639  * @code: can message acceptance code
640  * @mask: can message acceptance mask
641  *
642  * Return Value: negative value reports error.
643  * File: src/usbcan.c
644  */
645 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
646                 unsigned short mask)
647 {
648         CANMSG("usbcan_standard_mask not implemented\n");
649         return -ENOSYS;
650 }
651
652 /**
653  * usbcan_clear_objects: - clears state of all message object residing in chip
654  * @chip: pointer to chip state structure
655  *
656  * Return Value: negative value reports error.
657  * File: src/usbcan.c
658  */
659 int usbcan_clear_objects(struct canchip_t *chip)
660 {
661         CANMSG("usbcan_clear_objects not implemented\n");
662         return -ENOSYS;
663 }
664
665 /**
666  * usbcan_config_irqs: - tunes chip hardware interrupt delivery
667  * @chip: pointer to chip state structure
668  * @irqs: requested chip IRQ configuration
669  *
670  * Return Value: negative value reports error.
671  * File: src/usbcan.c
672  */
673 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
674 {
675         CANMSG("usbcan_config_irqs not implemented\n");
676         return -ENOSYS;
677 }
678
679 /**
680  * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
681  * @chip: pointer to chip state structure
682  * @obj: pointer to attached queue description
683  *
684  * The main purpose of this function is to read message from attached queues
685  * and transfer message contents into CAN controller chip.
686  * This subroutine is called by
687  * usbcan_irq_write_handler() for transmit events.
688  * File: src/usbcan.c
689  */
690 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
691 {
692         int cmd;
693
694         if(obj->tx_slot){
695                 // Do local transmitted message distribution if enabled
696                 if (processlocal){
697                         // fill CAN message timestamp
698                         can_filltimestamp(&obj->tx_slot->msg.timestamp);
699
700                         obj->tx_slot->msg.flags |= MSG_LOCAL;
701                         canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
702                 }
703                 // Free transmitted slot
704                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
705                 obj->tx_slot=NULL;
706         }
707
708         can_msgobj_clear_fl(obj,TX_PENDING);
709         cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
710         if(cmd<0)
711                 return;
712         can_msgobj_set_fl(obj,TX_PENDING);
713
714         if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
715                 obj->ret = -1;
716                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
717                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
718                 obj->tx_slot=NULL;
719                 return;
720         }
721         if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
722                 obj->ret = -1;
723                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
724                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
725                 obj->tx_slot=NULL;
726                 return;
727         }
728 }
729
730 #define MAX_RETR 10
731
732 /**
733  * usbcan_irq_handler: - interrupt service routine
734  * @irq: interrupt vector number, this value is system specific
735  * @chip: pointer to chip state structure
736  *
737  * Interrupt handler is activated when state of CAN controller chip changes,
738  * there is message to be read or there is more space for new messages or
739  * error occurs. The receive events results in reading of the message from
740  * CAN controller chip and distribution of message through attached
741  * message queues.
742  * File: src/usbcan.c
743  */
744 int usbcan_irq_handler(int irq, struct canchip_t *chip)
745 {
746 /*      int irq_register, status, error_code;
747         struct msgobj_t *obj=chip->msgobj[0];
748         int loop_cnt=CHIP_MAX_IRQLOOP;
749
750         irq_register=can_read_reg(chip,SJAIR);
751 //      DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
752 //      DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
753 //                                      can_read_reg(chip,SJASR));
754
755         if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
756                 return CANCHIP_IRQ_NONE;
757
758         if(!(chip->flags&CHIP_CONFIGURED)) {
759                 CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
760                 return CANCHIP_IRQ_NONE;
761         }
762
763         status=can_read_reg(chip,SJASR);
764
765         do {
766
767                 if(!loop_cnt--) {
768                         CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
769                         return CANCHIP_IRQ_STUCK;
770                 }
771
772                 // (irq_register & sjaIR_TI)
773                 //      old variant using SJAIR, collides with intended use with irq_accept
774                 if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
775                     (can_msgobj_test_fl(obj,TX_REQUEST))) {
776                         DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
777                         obj->ret = 0;
778                         can_msgobj_set_fl(obj,TX_REQUEST);
779                         while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
780                                 can_msgobj_clear_fl(obj,TX_REQUEST);
781
782                                 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
783                                         usbcan_irq_write_handler(chip, obj);
784
785                                 can_msgobj_clear_fl(obj,TX_LOCK);
786                                 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
787                                 DEBUGMSG("TX looping in sja1000_irq_handler\n");
788                         }
789                 }
790                 if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
791                         // Some error happened
792                         error_code=can_read_reg(chip,SJAECC);
793                         sja1000_report_error(chip, status, irq_register, error_code);
794 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
795 // Reset flag set to 0 if chip is already off the bus. Full state report
796                         obj->ret=-1;
797
798                         if(error_code == 0xd9) {
799                                 obj->ret= -ENXIO;
800                                 // no such device or address - no ACK received
801                         }
802                         if(obj->tx_retry_cnt++>MAX_RETR) {
803                                 can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
804                                 obj->tx_retry_cnt = 0;
805                         }
806                         if(status&sjaSR_BS) {
807                                 CANMSG("bus-off, resetting usbcan\n");
808                                 can_write_reg(chip, 0, SJAMOD);
809                         }
810
811                         if(obj->tx_slot){
812                                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
813                                 //canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
814                                 //obj->tx_slot=NULL;
815                         }
816
817                 } else {
818                         if(sja1000_report_error_limit_counter)
819                                 sja1000_report_error_limit_counter--;
820                         obj->tx_retry_cnt=0;
821                 }
822
823                 irq_register=can_read_reg(chip,SJAIR);
824
825                 status=can_read_reg(chip,SJASR);
826
827                 if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
828                    (irq_register & sjaIR_TI))
829                          can_msgobj_set_fl(obj,TX_REQUEST);
830
831         } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
832                 (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
833                 (status & sjaSR_RBS));
834 */
835         return CANCHIP_IRQ_HANDLED;
836 }
837
838 /**
839  * usbcan_wakeup_tx: - wakeups TX processing
840  * @chip: pointer to chip state structure
841  * @obj: pointer to message object structure
842  *
843  * Function is responsible for initiating message transmition.
844  * It is responsible for clearing of object TX_REQUEST flag
845  *
846  * Return Value: negative value reports error.
847  * File: src/usbcan.c
848  */
849 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
850 {
851
852         can_preempt_disable();
853
854         can_msgobj_set_fl(obj,TX_PENDING);
855         can_msgobj_set_fl(obj,TX_REQUEST);
856         while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
857                 can_msgobj_clear_fl(obj,TX_REQUEST);
858
859                 if (!usbcan_chip_queue_status(chip)){
860                         obj->tx_retry_cnt=0;
861                         usbcan_irq_write_handler(chip, obj);
862                 }
863
864                 can_msgobj_clear_fl(obj,TX_LOCK);
865                 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
866                 DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
867         }
868
869         can_preempt_enable();
870         return 0;
871 }
872
873 int usbcan_chipregister(struct chipspecops_t *chipspecops)
874 {
875         CANMSG("initializing usbcan chip operations\n");
876         chipspecops->chip_config=usbcan_chip_config;
877         chipspecops->baud_rate=usbcan_baud_rate;
878         chipspecops->standard_mask=usbcan_standard_mask;
879         chipspecops->extended_mask=usbcan_extended_mask;
880         chipspecops->message15_mask=usbcan_extended_mask;
881         chipspecops->clear_objects=usbcan_clear_objects;
882         chipspecops->config_irqs=usbcan_config_irqs;
883         chipspecops->pre_read_config=usbcan_pre_read_config;
884         chipspecops->pre_write_config=usbcan_pre_write_config;
885         chipspecops->send_msg=usbcan_send_msg;
886         chipspecops->check_tx_stat=usbcan_check_tx_stat;
887         chipspecops->wakeup_tx=usbcan_wakeup_tx;
888         chipspecops->remote_request=usbcan_remote_request;
889         chipspecops->enable_configuration=usbcan_enable_configuration;
890         chipspecops->disable_configuration=usbcan_disable_configuration;
891         chipspecops->attach_to_chip=usbcan_attach_to_chip;
892         chipspecops->release_chip=usbcan_release_chip;
893         chipspecops->set_btregs=usbcan_set_btregs;
894         chipspecops->start_chip=usbcan_start_chip;
895         chipspecops->stop_chip=usbcan_stop_chip;
896         chipspecops->irq_handler=usbcan_irq_handler;
897         chipspecops->irq_accept=NULL;
898         return 0;
899 }
900
901 /**
902  * usbcan_fill_chipspecops - fills chip specific operations
903  * @chip: pointer to chip representation structure
904  *
905  * The function fills chip specific operations for sja1000 (PeliCAN) chip.
906  *
907  * Return Value: returns negative number in the case of fail
908  */
909 int usbcan_fill_chipspecops(struct canchip_t *chip)
910 {
911         chip->chip_type="usbcan";
912         chip->max_objects=1;
913         usbcan_chipregister(chip->chipspecops);
914         return 0;
915 }
916
917 /**
918  * usbcan_init_chip_data - Initialize chips
919  * @candev: Pointer to candevice/board structure
920  * @chipnr: Number of the CAN chip on the hardware card
921  *
922  * The function usbcan_init_chip_data() is used to initialize the hardware
923  * structure containing information about the CAN chips.
924  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
925  * "sja1000".
926  * The @chip_base_addr entry represents the start of the 'official' memory map
927  * of the installed chip. It's likely that this is the same as the @io_addr
928  * argument supplied at module loading time.
929  * The @clock entry holds the chip clock value in Hz.
930  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
931  * register. Options defined in the %sja1000.h file:
932  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
933  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
934  * register. Options defined in the %sja1000.h file:
935  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
936  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
937  * The entry @int_clk_reg holds hardware specific options for the Clock Out
938  * register. Options defined in the %i82527.h file:
939  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
940  * The entry @int_bus_reg holds hardware specific options for the Bus
941  * Configuration register. Options defined in the %i82527.h file:
942  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
943  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
944  * register. Options defined in the %i82527.h file:
945  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
946  * Return Value: The function always returns zero
947  * File: src/usbcan.c
948  */
949 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
950 {
951         struct canchip_t *chip=candev->chip[chipnr];
952
953         usbcan_fill_chipspecops(chip);
954
955         candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
956         candev->chip[chipnr]->chip_base_addr=0;
957         candev->chip[chipnr]->clock = 0;
958
959         return 0;
960 }
961
962
963
964 /* --------------------------------------------------------------------------------------------------- */
965
966 static void usbcan_rcv(struct urb *urb)
967 {
968         struct usbcan_usb *dev = urb->context;
969         int retval;
970
971         switch (urb->status) {
972         case 0:
973                 /* success */
974                 set_bit(USBCAN_DATA_READ,&dev->flags);
975                 wake_up(&dev->rcvthread.queue);
976                 return;
977         case -ECONNRESET:
978         case -ENOENT:
979         case -ESHUTDOWN:
980                 /* this urb is terminated, clean up */
981                 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
982                 set_bit(USBCAN_TERMINATE,&dev->flags);
983                 wake_up(&dev->rcvthread.queue);
984                 return;
985         default:
986                 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
987                 break;
988         }
989
990         retval = usb_submit_urb (urb, GFP_ATOMIC);
991         if (retval<0){
992                 CANMSG("%s - usb_submit_urb failed with result %d\n",
993                      __FUNCTION__, retval);
994                 set_bit(USBCAN_ERROR,&dev->flags);
995                 wake_up(&dev->rcvthread.queue);
996         }
997 }
998
999 void usbcan_read_kthread(kthread_t *kthread)
1000 {
1001         int retval;
1002         struct usbcan_usb *dev=(struct usbcan_usb *)kthread->arg;
1003         struct msgobj_t *obj;
1004
1005   /* setup the thread environment */
1006   init_kthread(kthread, "usbcan");
1007
1008   /* this is normal work to do */
1009   CANMSG ("usbcan thread started!\n");
1010
1011         dev->rcv = usb_alloc_urb(0, GFP_KERNEL);
1012         if (!dev->rcv){
1013                 CANMSG("Error allocating usb urb\n");
1014                 goto error;
1015         }
1016         dev->rcv->dev = dev->udev;
1017         usb_fill_bulk_urb(dev->rcv, dev->udev,
1018                          usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1019                          &dev->rcv_msg, 16,
1020                          usbcan_rcv, dev);
1021
1022   /* an endless loop in which we are doing our work */
1023   for(;;)
1024   {
1025                 retval=usb_submit_urb(dev->rcv, GFP_KERNEL);
1026                 if (retval){
1027                         CANMSG("URB error %d\n",retval);
1028                         break;
1029                 }
1030                 /* fall asleep */
1031                 wait_event_interruptible(kthread->queue,
1032                         test_bit(USBCAN_DATA_READ,&dev->flags)
1033                         || test_bit(USBCAN_TERMINATE,&dev->flags)
1034                         || test_bit(USBCAN_ERROR,&dev->flags)
1035                 );
1036
1037                 /* We need to do a memory barrier here to be sure that
1038                 the flags are visible on all CPUs. */
1039                 mb();
1040
1041                 /* here we are back from sleep because we caught a signal. */
1042                 if (kthread->terminate)
1043                 {
1044                         /* we received a request to terminate ourself */
1045                         break;
1046                 }
1047
1048                 if (test_bit(USBCAN_ERROR,&dev->flags)){
1049                         CANMSG("URB error %d\n",retval);
1050                         clear_bit(USBCAN_ERROR,&dev->flags);
1051                 }
1052
1053                 { /* Normal work to do */
1054                         if (test_bit(USBCAN_DATA_READ,&dev->flags)){
1055                                 int i, len;
1056                                 clear_bit(USBCAN_DATA_READ,&dev->flags);
1057
1058                                 if ((dev->candev->chip[dev->rcv_msg[0]])&&
1059                                         (dev->candev->chip[dev->rcv_msg[0]]->flags & CHIP_CONFIGURED)
1060                                 ){
1061                                         u8 *ptr;
1062
1063                                         obj=dev->candev->chip[dev->rcv_msg[0]]->msgobj[0];
1064
1065                                         len=*(uint8_t *)(dev->rcv_msg+1);
1066                                         if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
1067                                         obj->rx_msg.length = len;
1068
1069                                         obj->rx_msg.flags=le16_to_cpu(*(uint16_t *)(dev->rcv_msg+2));
1070                                         obj->rx_msg.id=le32_to_cpu((*(uint32_t *)(dev->rcv_msg+4)));
1071
1072                                         for(ptr=dev->rcv_msg+8,i=0; i < len; ptr++,i++) {
1073                                                 obj->rx_msg.data[i]=*ptr;
1074                                         }
1075
1076                                         // fill CAN message timestamp
1077                                         can_filltimestamp(&obj->rx_msg.timestamp);
1078                                         canque_filter_msg2edges(obj->qends, &obj->rx_msg);
1079                                 }
1080                         }
1081     }
1082   }
1083   /* here we go only in case of termination of the thread */
1084 error:
1085   /* cleanup the thread, leave */
1086   CANMSG ("kernel thread terminated!\n");
1087   exit_kthread(kthread);
1088
1089   /* returning from the thread here calls the exit functions */
1090 }
1091
1092 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1093 {
1094         struct usbcan_usb *dev;
1095         struct usb_host_interface *iface_desc;
1096         struct usb_endpoint_descriptor *endpoint;
1097         size_t buffer_size;
1098         int i;
1099         int retval = -ENOMEM;
1100
1101         /* allocate memory for our device state and initialize it */
1102         dev = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1103         if (!dev) {
1104                 err("Out of memory");
1105                 goto error;
1106         }
1107         memset(dev, 0, sizeof(struct usbcan_usb));
1108
1109         sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1110         spin_lock_init(&dev->err_lock);
1111         init_usb_anchor(&dev->submitted);
1112
1113 //      dev->udev = usb_get_dev(interface_to_usbdev(interface));
1114         dev->udev = interface_to_usbdev(interface);
1115         dev->interface = interface;
1116
1117         /* set up the endpoint information */
1118         /* use only the first bulk-in and bulk-out endpoints */
1119         iface_desc = interface->cur_altsetting;
1120         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1121                 endpoint = &iface_desc->endpoint[i].desc;
1122
1123                 if (!dev->bulk_in_endpointAddr &&
1124                     usb_endpoint_is_bulk_in(endpoint)) {
1125                         /* we found a bulk in endpoint */
1126                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1127                         dev->bulk_in_size = buffer_size;
1128                         dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1129                         dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1130                         if (!dev->bulk_in_buffer) {
1131                                 err("Could not allocate bulk_in_buffer");
1132                                 goto error;
1133                         }
1134                 }
1135
1136                 if (!dev->bulk_out_endpointAddr &&
1137                     usb_endpoint_is_bulk_out(endpoint)) {
1138                         /* we found a bulk out endpoint */
1139                                 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1140                 }
1141
1142                 if (!dev->ctl_in_endpointAddr &&
1143                     usb_endpoint_xfer_control(endpoint) &&
1144                     usb_endpoint_dir_in(endpoint)) {
1145                         /* we found a bulk in endpoint */
1146                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1147                         dev->ctl_in_size = buffer_size;
1148                         dev->ctl_in_endpointAddr = endpoint->bEndpointAddress;
1149                         dev->ctl_in_buffer = can_checked_malloc(buffer_size);
1150                         if (!dev->ctl_in_buffer) {
1151                                 err("Could not allocate bulk_in_buffer");
1152                                 goto error;
1153                         }
1154                 }
1155
1156                 if (!dev->ctl_out_endpointAddr &&
1157                     usb_endpoint_xfer_control(endpoint) &&
1158                     usb_endpoint_dir_out(endpoint)) {
1159                         /* we found a bulk out endpoint */
1160                                 dev->ctl_out_endpointAddr = endpoint->bEndpointAddress;
1161                 }
1162         }
1163         if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1164                 err("Could not find all bulk-in and bulk-out endpoints");
1165                 goto error;
1166         }
1167
1168         /* save our data pointer in this interface device */
1169         usb_set_intfdata(interface, dev);
1170
1171         register_usbdev("usbcan",(void *) dev);
1172
1173         /* let the user know what node this device is now attached to */
1174         info("USB Skeleton device now attached");
1175         return 0;
1176
1177 error:
1178         usb_put_dev(dev->udev);
1179         if (dev->bulk_in_buffer)
1180                 can_checked_free(dev->bulk_in_buffer);
1181         if (dev->ctl_in_buffer)
1182                 can_checked_free(dev->ctl_in_buffer);
1183         if (dev->candev){
1184                 dev->candev->sysdevptr.anydev=NULL;
1185                 cleanup_usbdev(dev->candev);
1186         }
1187         can_checked_free(dev);
1188         return retval;
1189 }
1190
1191 // Physically disconnected device
1192 static void usbcan_disconnect(struct usb_interface *interface)
1193 {
1194         struct usbcan_usb *dev;
1195         int minor = interface->minor;
1196
1197         dev = usb_get_intfdata(interface);
1198         if (!dev)
1199                 return;
1200         usb_set_intfdata(interface, NULL);
1201
1202         /* prevent more I/O from starting */
1203         mutex_lock(&dev->io_mutex);
1204         dev->interface = NULL;
1205         mutex_unlock(&dev->io_mutex);
1206
1207         //usb_kill_anchored_urbs(&dev->submitted);
1208
1209         usb_put_dev(dev->udev);
1210         if (dev->bulk_in_buffer)
1211                 can_checked_free(dev->bulk_in_buffer);
1212         if (dev->ctl_in_buffer)
1213                 can_checked_free(dev->ctl_in_buffer);
1214         if (dev->candev){
1215                 dev->candev->sysdevptr.anydev=NULL;
1216                 cleanup_usbdev(dev->candev);
1217         }
1218         can_checked_free(dev);
1219
1220         info("USB Skeleton now disconnected");
1221 }
1222
1223 int usbcan_init(void){
1224         return usb_register(&usbcan_driver);
1225 }
1226
1227 void usbcan_exit(void){
1228         usb_deregister(&usbcan_driver);
1229 }