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