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