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