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
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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
15 #include <linux/freezer.h>
17 #include <linux/smp_lock.h>
19 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
20 static void usbcan_disconnect(struct usb_interface *interface);
21 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
22 void release_device(struct kref *refcount);
24 void release_device(struct candevice_t *candev);
27 volatile int usbcan_chip_count=0;
29 /* table of devices that work with this driver */
30 static struct usb_device_id usbcan_table [] = {
31 { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
32 { } /* Terminating entry */
34 MODULE_DEVICE_TABLE(usb, usbcan_table);
36 static struct usb_driver usbcan_driver = {
38 .id_table = usbcan_table,
39 .probe = usbcan_probe,
40 .disconnect = usbcan_disconnect,
44 * usbcan_request_io: - reserve io or memory range for can board
45 * @candev: pointer to candevice/board which asks for io. Field @io_addr
46 * of @candev is used in most cases to define start of the range
48 * The function usbcan_request_io() is used to reserve the io-memory. If your
49 * hardware uses a dedicated memory range as hardware control registers you
50 * will have to add the code to reserve this memory as well.
51 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
52 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
53 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
54 * Return Value: The function returns zero on success or %-ENODEV on failure
57 int usbcan_request_io(struct candevice_t *candev)
59 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
62 CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
63 CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
71 * usbcan_release_io - free reserved io memory range
72 * @candev: pointer to candevice/board which releases io
74 * The function usbcan_release_io() is used to free reserved io-memory.
75 * In case you have reserved more io memory, don't forget to free it here.
76 * IO_RANGE is the io-memory range that gets released, please adjust according
77 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
78 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
79 * Return Value: The function always returns zero
82 int usbcan_release_io(struct candevice_t *candev)
88 * usbcan_reset - hardware reset routine
89 * @candev: Pointer to candevice/board structure
91 * The function usbcan_reset() is used to give a hardware reset. This is
92 * rather hardware specific so I haven't included example code. Don't forget to
93 * check the reset status of the chip before returning.
94 * Return Value: The function returns zero on success or %-ENODEV on failure
97 int usbcan_reset(struct candevice_t *candev)
103 * usbcan_init_hw_data - Initialize hardware cards
104 * @candev: Pointer to candevice/board structure
106 * The function usbcan_init_hw_data() is used to initialize the hardware
107 * structure containing information about the installed CAN-board.
108 * %RESET_ADDR represents the io-address of the hardware reset register.
109 * %NR_82527 represents the number of Intel 82527 chips on the board.
110 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
111 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
112 * the hardware uses programmable interrupts.
113 * Return Value: The function always returns zero
116 int usbcan_init_hw_data(struct candevice_t *candev)
118 candev->res_addr=RESET_ADDR;
119 candev->nr_82527_chips=0;
120 candev->nr_sja1000_chips=0;
121 candev->nr_all_chips=usbcan_chip_count;
122 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
128 * usbcan_init_obj_data - Initialize message buffers
129 * @chip: Pointer to chip specific structure
130 * @objnr: Number of the message buffer
132 * The function usbcan_init_obj_data() is used to initialize the hardware
133 * structure containing information about the different message objects on the
134 * CAN chip. In case of the sja1000 there's only one message object but on the
135 * i82527 chip there are 15.
136 * The code below is for a i82527 chip and initializes the object base addresses
137 * The entry @obj_base_addr represents the first memory address of the message
138 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
140 * Unless the hardware uses a segmented memory map, flags can be set zero.
141 * Return Value: The function always returns zero
144 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
146 chip->msgobj[objnr]->obj_base_addr=0;
152 * usbcan_program_irq - program interrupts
153 * @candev: Pointer to candevice/board structure
155 * The function usbcan_program_irq() is used for hardware that uses
156 * programmable interrupts. If your hardware doesn't use programmable interrupts
157 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
158 * leave this function unedited. Again this function is hardware specific so
159 * there's no example code.
160 * Return value: The function returns zero on success or %-ENODEV on failure
163 int usbcan_program_irq(struct candevice_t *candev)
168 int usbcan_register(struct hwspecops_t *hwspecops)
170 hwspecops->request_io = usbcan_request_io;
171 hwspecops->release_io = usbcan_release_io;
172 hwspecops->reset = usbcan_reset;
173 hwspecops->init_hw_data = usbcan_init_hw_data;
174 hwspecops->init_chip_data = usbcan_init_chip_data;
175 hwspecops->init_obj_data = usbcan_init_obj_data;
176 hwspecops->write_register = NULL;
177 hwspecops->read_register = NULL;
178 hwspecops->program_irq = usbcan_program_irq;
179 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
180 hwspecops->release_device = release_device;
185 // static int sja1000_report_error_limit_counter;
187 static void usbcan_report_error(struct canchip_t *chip,
188 unsigned sr, unsigned ir, unsigned ecc)
190 /*TODO : Error reporting from device */
193 if(sja1000_report_error_limit_counter>=100)
196 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
199 sja1000_report_error_limit_counter+=10;
201 if(sja1000_report_error_limit_counter>=100){
202 sja1000_report_error_limit_counter+=10;
203 CANMSG("Error: too many errors, reporting disabled\n");
207 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
208 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
209 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
210 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
211 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
212 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
213 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
214 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
215 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
216 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
217 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
218 if((sr&sjaIR_EI) || 1){
219 CANMSG("EI: %s %s %s\n",
220 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
221 ecc&sjaECC_DIR?"RX":"TX",
222 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
225 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
231 * usbcan_enable_configuration - enable chip configuration mode
232 * @chip: pointer to chip state structure
234 int usbcan_enable_configuration(struct canchip_t *chip)
240 * usbcan_disable_configuration - disable chip configuration mode
241 * @chip: pointer to chip state structure
243 int usbcan_disable_configuration(struct canchip_t *chip)
249 * usbcan_chip_config: - can chip configuration
250 * @chip: pointer to chip state structure
252 * This function configures chip and prepares it for message
253 * transmission and reception. The function resets chip,
254 * resets mask for acceptance of all messages by call to
255 * usbcan_extended_mask() function and then
256 * computes and sets baudrate with use of function usbcan_baud_rate().
257 * Return Value: negative value reports error.
260 int usbcan_chip_config(struct canchip_t *chip)
266 * usbcan_extended_mask: - setup of extended mask for message filtering
267 * @chip: pointer to chip state structure
268 * @code: can message acceptance code
269 * @mask: can message acceptance mask
271 * Return Value: negative value reports error.
274 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
277 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
279 u8 usbbuf[USBCAN_TRANSFER_SIZE];
284 *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
285 *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
287 retval=usb_control_msg(dev->udev,
288 usb_sndctrlpipe(dev->udev, 0),
289 USBCAN_VENDOR_EXT_MASK_SET,
291 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
292 &usbbuf, USBCAN_TRANSFER_SIZE,
297 retval = usb_control_msg(dev->udev,
298 usb_rcvctrlpipe(dev->udev, 0),
299 USBCAN_VENDOR_EXT_MASK_STATUS,
301 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
302 &usbbuf, USBCAN_TRANSFER_SIZE,
307 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
308 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
313 CANMSG("Setting extended mask failed\n");
318 * usbcan_baud_rate: - set communication parameters.
319 * @chip: pointer to chip state structure
320 * @rate: baud rate in Hz
321 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
322 * @sjw: synchronization jump width (0-3) prescaled clock cycles
323 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
324 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
326 * Return Value: negative value reports error.
329 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
330 int sampl_pt, int flags)
333 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
335 u8 usbbuf[USBCAN_TRANSFER_SIZE];
340 *(int32_t *)(usbbuf)=cpu_to_le32(rate);
341 *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
342 *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
343 *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
345 retval=usb_control_msg(dev->udev,
346 usb_sndctrlpipe(dev->udev, 0),
347 USBCAN_VENDOR_BAUD_RATE_SET,
349 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
350 &usbbuf, USBCAN_TRANSFER_SIZE,
355 retval = usb_control_msg(dev->udev,
356 usb_rcvctrlpipe(dev->udev, 0),
357 USBCAN_VENDOR_BAUD_RATE_STATUS,
359 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
360 usbbuf, USBCAN_TRANSFER_SIZE,
368 CANMSG("baud rate %d is not possible to set\n",
374 * usbcan_pre_read_config: - prepares message object for message reception
375 * @chip: pointer to chip state structure
376 * @obj: pointer to message object state structure
378 * Return Value: negative value reports error.
379 * Positive value indicates immediate reception of message.
382 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
387 #define MAX_TRANSMIT_WAIT_LOOPS 10
389 * usbcan_pre_write_config: - prepares message object for message transmission
390 * @chip: pointer to chip state structure
391 * @obj: pointer to message object state structure
392 * @msg: pointer to CAN message
394 * This function prepares selected message object for future initiation
395 * of message transmission by usbcan_send_msg() function.
396 * The CAN message data and message ID are transfered from @msg slot
397 * into chip buffer in this function.
398 * Return Value: negative value reports error.
401 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
402 struct canmsg_t *msg)
408 * usbcan_send_msg: - initiate message transmission
409 * @chip: pointer to chip state structure
410 * @obj: pointer to message object state structure
411 * @msg: pointer to CAN message
413 * This function is called after usbcan_pre_write_config() function,
414 * which prepares data in chip buffer.
415 * Return Value: negative value reports error.
418 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
419 struct canmsg_t *msg)
425 * usbcan_check_tx_stat: - checks state of transmission engine
426 * @chip: pointer to chip state structure
428 * Return Value: negative value reports error.
429 * Positive return value indicates transmission under way status.
430 * Zero value indicates finishing of all issued transmission requests.
433 int usbcan_check_tx_stat(struct canchip_t *chip)
435 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
438 if (test_bit(USBCAN_TX_PENDING,&dev->flags))
444 * usbcan_set_btregs: - configures bitrate registers
445 * @chip: pointer to chip state structure
446 * @btr0: bitrate register 0
447 * @btr1: bitrate register 1
449 * Return Value: negative value reports error.
452 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
456 u8 buf[USBCAN_TRANSFER_SIZE];
457 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
458 uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
463 retval = usb_control_msg(dev->udev,
464 usb_rcvctrlpipe(dev->udev, 0),
465 USBCAN_VENDOR_SET_BTREGS,
467 cpu_to_le16(value), cpu_to_le16(chip->chip_idx),
468 &buf, USBCAN_TRANSFER_SIZE,
479 * usbcan_start_chip: - starts chip message processing
480 * @chip: pointer to chip state structure
482 * Return Value: negative value reports error.
485 int usbcan_start_chip(struct canchip_t *chip)
488 u8 buf[USBCAN_TRANSFER_SIZE];
489 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
494 retval = usb_control_msg(dev->udev,
495 usb_rcvctrlpipe(dev->udev, 0),
496 USBCAN_VENDOR_START_CHIP,
498 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
499 &buf, USBCAN_TRANSFER_SIZE,
510 * usbcan_chip_queue_status: - gets queue status from usb device
511 * @chip: pointer to chip state structure
513 * Return Value: negative value reports error.
514 * 0 means queue is not full
515 * 1 means queue is full
518 int usbcan_chip_queue_status(struct canchip_t *chip)
521 u8 buf[USBCAN_TRANSFER_SIZE];
522 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
526 retval = usb_control_msg(dev->udev,
527 usb_rcvctrlpipe(dev->udev, 0),
528 USBCAN_VENDOR_CHECK_TX_STAT,
530 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
531 &buf, USBCAN_TRANSFER_SIZE,
535 DEBUGMSG("Chip_queue_status: %d\n",buf[0]);
541 CANMSG("Chip_queue_status error: %d\n",retval);
546 * usbcan_stop_chip: - stops chip message processing
547 * @chip: pointer to chip state structure
549 * Return Value: negative value reports error.
552 int usbcan_stop_chip(struct canchip_t *chip)
555 u8 buf[USBCAN_TRANSFER_SIZE];
556 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
561 retval = usb_control_msg(dev->udev,
562 usb_rcvctrlpipe(dev->udev, 0),
563 USBCAN_VENDOR_STOP_CHIP,
565 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
566 &buf, USBCAN_TRANSFER_SIZE,
577 * usbcan_register_devs: - attaches usb device data to the chip structure
578 * @chip: pointer to chip state structure
579 * @data: usb device data
583 int usbcan_register_devs(struct canchip_t *chip,void *data){
584 struct usbcan_devs *usbdevs=(struct usbcan_devs *)data;
586 CANMSG("Bad structure given\n");
589 if (chip->chip_idx>=usbdevs->count) {
590 CANMSG("Requested chip number is bigger than chip count\n");
594 usbdevs->devs[chip->chip_idx]->chip=chip;
595 chip->chip_data=(void *)usbdevs->devs[chip->chip_idx];
600 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
601 * @chip: pointer to chip state structure
603 * Return Value: negative value reports error.
606 int usbcan_attach_to_chip(struct canchip_t *chip)
608 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
610 /* start kernel thread */
611 dev->comthread=can_kthread_run(usbcan_kthread, (void *)dev, "usbcan_%d",chip->chip_idx);
617 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
618 * @chip: pointer to chip state structure
620 * Return Value: negative value reports error.
623 int usbcan_release_chip(struct canchip_t *chip)
625 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
627 usbcan_stop_chip(chip);
629 /* terminate the kernel thread */
630 set_bit(USBCAN_TERMINATE,&dev->flags);
631 // wake_up_process(dev->comthread);
632 can_kthread_stop(dev->comthread);
638 * usbcan_remote_request: - configures message object and asks for RTR message
639 * @chip: pointer to chip state structure
640 * @obj: pointer to message object structure
642 * Return Value: negative value reports error.
645 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
647 CANMSG("usbcan_remote_request not implemented\n");
652 * usbcan_standard_mask: - setup of mask for message filtering
653 * @chip: pointer to chip state structure
654 * @code: can message acceptance code
655 * @mask: can message acceptance mask
657 * Return Value: negative value reports error.
660 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
663 CANMSG("usbcan_standard_mask not implemented\n");
668 * usbcan_clear_objects: - clears state of all message object residing in chip
669 * @chip: pointer to chip state structure
671 * Return Value: negative value reports error.
674 int usbcan_clear_objects(struct canchip_t *chip)
676 CANMSG("usbcan_clear_objects not implemented\n");
681 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
682 * @chip: pointer to chip state structure
683 * @irqs: requested chip IRQ configuration
685 * Return Value: negative value reports error.
688 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
690 CANMSG("usbcan_config_irqs not implemented\n");
695 static void usbcan_usb_message_move_list(struct usbcan_usb *dev,
696 struct usbcan_message *m, struct list_head *head)
698 can_spin_irqflags_t flags;
699 can_spin_lock_irqsave(&dev->list_lock, flags);
700 list_del(&m->list_node);
701 list_add_tail(&m->list_node, head);
702 can_spin_unlock_irqrestore(&dev->list_lock, flags);
707 * usbcan_kthread_read_handler: - part of kthread code responsible for receive completed events
708 * @dev: pointer to usb device related structure
709 * @obj: pointer to attached message object description
711 * The main purpose of this function is to read message from usb urb
712 * and transfer message contents to CAN queue ends.
713 * This subroutine is called by
717 void usbcan_kthread_read_handler(struct usbcan_usb *dev, struct usbcan_message *m,
718 struct msgobj_t *obj)
723 DEBUGMSG("USBCAN RX handler\n");
725 if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
726 CANMSG("Strange, Rx handler USBCAN_MESSAGE_DATA_OK not set\n");
730 if (!(dev->chip)||!(dev->chip->flags & CHIP_CONFIGURED)) {
731 CANMSG("Destination chip not found\n");
736 DEBUGMSG("USBCAN Thread has received a message\n");
738 len=*(u8 *)(m->msg+1);
739 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
740 obj->rx_msg.length = len;
742 obj->rx_msg.flags=le16_to_cpu(*(u16 *)(m->msg+2));
743 obj->rx_msg.id=le32_to_cpu((*(u32 *)(m->msg+4)));
745 for(ptr=m->msg+8,i=0; i < len; ptr++,i++) {
746 obj->rx_msg.data[i]=*ptr;
749 // fill CAN message timestamp
750 can_filltimestamp(&obj->rx_msg.timestamp);
751 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
754 DEBUGMSG("Renewing RX urb\n");
756 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
757 retval = usb_submit_urb (m->u, GFP_KERNEL);
759 CANMSG("URB error %d\n", retval);
760 set_bit(USBCAN_ERROR,&dev->flags);
765 * usbcan_kthread_write_handler: - part of kthread code responsible for transmit done events
766 * @dev: pointer to usb device related structure
767 * @obj: pointer to attached message object description
769 * The main purpose of this function is to free allocated resources on transmit done event
770 * This subroutine is called by
774 void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct usbcan_message *m,
775 struct msgobj_t *obj)
777 if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
778 CANMSG("Strange, Tx handler USBCAN_MESSAGE_DATA_OK not set\n");
782 DEBUGMSG("USBCAN Message successfully sent\n");
785 // Do local transmitted message distribution if enabled
787 // fill CAN message timestamp
788 can_filltimestamp(&m->slot->msg.timestamp);
790 m->slot->msg.flags |= MSG_LOCAL;
791 canque_filter_msg2edges(obj->qends, &m->slot->msg);
793 // Free transmitted slot
794 canque_free_outslot(obj->qends, m->qedge, m->slot);
798 /*FIXME - why there*/
799 can_msgobj_clear_fl(obj,TX_PENDING);
802 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
804 set_bit(USBCAN_TX_PENDING,&dev->flags);
806 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
810 * usbcan_kthread_write_request_handler: - part of kthread code responsible for sending transmit urbs
811 * @dev: pointer to usb device related structure
812 * @obj: pointer to attached message object description
814 * The main purpose of this function is to create a usb transmit safe object
815 * and send it via free transmit usb urb
816 * This subroutine is called by
820 void usbcan_kthread_write_request_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
821 int i, cmd, len, retval;
823 struct usbcan_message *m;
825 if(list_empty(&dev->tx_idle_list)) {
826 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
830 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
832 cmd=canque_test_outslot(obj->qends, &m->qedge, &m->slot);
834 DEBUGMSG("USBCAN Sending a message\n");
836 can_msgobj_set_fl(obj,TX_PENDING);
837 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
840 len = m->slot->msg.length;
841 if(len > CAN_MSG_LENGTH)
842 len = CAN_MSG_LENGTH;
843 *(u8 *)(m->msg+1)=len & 0xFF;
844 *(u16 *)(m->msg+2)=cpu_to_le16(m->slot->msg.flags);
845 *(u32 *)(m->msg+4)=cpu_to_le32(m->slot->msg.id);
847 for(ptr=m->msg+8, i=0; i < len; ptr++,i++) {
848 *ptr=m->slot->msg.data[i] & 0xFF;
850 for(; i < 8; ptr++,i++) {
855 usbcan_usb_message_move_list(dev, m, &dev->tx_pend_list);
857 retval = usb_submit_urb (m->u, GFP_KERNEL);
859 CANMSG("%d. URB error %d\n",i,retval);
860 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
862 canque_notify_inends(m->qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
863 canque_free_outslot(obj->qends, m->qedge, m->slot);
865 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
867 set_bit(USBCAN_TX_PENDING,&dev->flags);
870 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
877 * usbcan_irq_handler: - interrupt service routine
878 * @irq: interrupt vector number, this value is system specific
879 * @chip: pointer to chip state structure
881 * Interrupt handler is activated when state of CAN controller chip changes,
882 * there is message to be read or there is more space for new messages or
883 * error occurs. The receive events results in reading of the message from
884 * CAN controller chip and distribution of message through attached
888 int usbcan_irq_handler(int irq, struct canchip_t *chip)
890 return CANCHIP_IRQ_HANDLED;
894 * usbcan_wakeup_tx: - wakeups TX processing
895 * @chip: pointer to chip state structure
896 * @obj: pointer to message object structure
898 * Function is responsible for initiating message transmition.
899 * It is responsible for clearing of object TX_REQUEST flag
901 * Return Value: negative value reports error.
904 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
906 struct usbcan_usb *dev=(struct usbcan_usb *)chip->chip_data;
908 DEBUGMSG("Trying to send message\n");
909 can_preempt_disable();
911 can_msgobj_set_fl(obj,TX_PENDING);
912 can_msgobj_set_fl(obj,TX_REQUEST);
913 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
914 can_msgobj_clear_fl(obj,TX_REQUEST);
916 if (test_and_clear_bit(USBCAN_FREE_TX_URB,&dev->flags)){
918 set_bit(USBCAN_TX_PENDING,&dev->flags);
919 if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
920 wake_up_process(dev->comthread);
923 can_msgobj_clear_fl(obj,TX_LOCK);
924 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
925 CANMSG("TX looping in usbcan_wakeup_tx\n");
928 can_preempt_enable();
932 int usbcan_chipregister(struct chipspecops_t *chipspecops)
934 CANMSG("initializing usbcan chip operations\n");
935 chipspecops->chip_config=usbcan_chip_config;
936 chipspecops->baud_rate=usbcan_baud_rate;
937 chipspecops->standard_mask=usbcan_standard_mask;
938 chipspecops->extended_mask=usbcan_extended_mask;
939 chipspecops->message15_mask=usbcan_extended_mask;
940 chipspecops->clear_objects=usbcan_clear_objects;
941 chipspecops->config_irqs=usbcan_config_irqs;
942 chipspecops->pre_read_config=usbcan_pre_read_config;
943 chipspecops->pre_write_config=usbcan_pre_write_config;
944 chipspecops->send_msg=usbcan_send_msg;
945 chipspecops->check_tx_stat=usbcan_check_tx_stat;
946 chipspecops->wakeup_tx=usbcan_wakeup_tx;
947 chipspecops->remote_request=usbcan_remote_request;
948 chipspecops->enable_configuration=usbcan_enable_configuration;
949 chipspecops->disable_configuration=usbcan_disable_configuration;
950 chipspecops->attach_to_chip=usbcan_attach_to_chip;
951 chipspecops->release_chip=usbcan_release_chip;
952 chipspecops->set_btregs=usbcan_set_btregs;
953 chipspecops->start_chip=usbcan_start_chip;
954 chipspecops->stop_chip=usbcan_stop_chip;
955 chipspecops->irq_handler=usbcan_irq_handler;
956 chipspecops->irq_accept=NULL;
961 * usbcan_fill_chipspecops - fills chip specific operations
962 * @chip: pointer to chip representation structure
964 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
966 * Return Value: returns negative number in the case of fail
968 int usbcan_fill_chipspecops(struct canchip_t *chip)
970 chip->chip_type="usbcan";
972 usbcan_chipregister(chip->chipspecops);
977 * usbcan_init_chip_data - Initialize chips
978 * @candev: Pointer to candevice/board structure
979 * @chipnr: Number of the CAN chip on the hardware card
981 * The function usbcan_init_chip_data() is used to initialize the hardware
982 * structure containing information about the CAN chips.
983 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
985 * The @chip_base_addr entry represents the start of the 'official' memory map
986 * of the installed chip. It's likely that this is the same as the @io_addr
987 * argument supplied at module loading time.
988 * The @clock entry holds the chip clock value in Hz.
989 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
990 * register. Options defined in the %sja1000.h file:
991 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
992 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
993 * register. Options defined in the %sja1000.h file:
994 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
995 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
996 * The entry @int_clk_reg holds hardware specific options for the Clock Out
997 * register. Options defined in the %i82527.h file:
998 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
999 * The entry @int_bus_reg holds hardware specific options for the Bus
1000 * Configuration register. Options defined in the %i82527.h file:
1001 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
1002 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
1003 * register. Options defined in the %i82527.h file:
1004 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
1005 * Return Value: The function always returns zero
1006 * File: src/usbcan.c
1008 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
1010 struct canchip_t *chip=candev->chip[chipnr];
1012 usbcan_fill_chipspecops(chip);
1014 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM|CHIP_KEEP_DATA;
1015 candev->chip[chipnr]->chip_base_addr=0;
1016 candev->chip[chipnr]->clock = 0;
1022 /** *********************************
1023 * USB related functions
1024 * ********************************* */
1026 static int usbcan_sleep_thread(struct usbcan_usb *dev)
1030 /* Wait until a signal arrives or we are woken up */
1033 set_current_state(TASK_INTERRUPTIBLE);
1034 if (signal_pending(current)) {
1039 can_kthread_should_stop() ||
1040 test_bit(USBCAN_DATA_OK,&dev->flags) ||
1041 test_bit(USBCAN_TX_PENDING,&dev->flags) ||
1042 test_bit(USBCAN_TERMINATE,&dev->flags) ||
1043 test_bit(USBCAN_ERROR,&dev->flags)
1048 __set_current_state(TASK_RUNNING);
1052 static void usbcan_tx_callback(struct urb *urb)
1054 struct usbcan_message *m = urb->context;
1057 if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1059 if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
1062 switch (urb->status) {
1065 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1066 set_bit(USBCAN_DATA_OK,&m->dev->flags);
1067 set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
1068 DEBUGMSG("%s > TX flag set\n", __FUNCTION__);
1069 set_bit(USBCAN_DATA_TX,&m->dev->flags);
1070 usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
1071 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1072 wake_up_process(m->dev->comthread);
1074 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1079 /* this urb is terminated, clean up */
1080 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1081 // set_bit(USBCAN_TERMINATE,&m->dev->flags);
1082 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1085 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1089 // Try to send urb again on non significant errors
1090 retval = usb_submit_urb (urb, GFP_ATOMIC);
1092 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1093 set_bit(USBCAN_ERROR,&m->dev->flags);
1094 usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
1095 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1096 wake_up_process(m->dev->comthread);
1100 static void usbcan_rx_callback(struct urb *urb)
1102 struct usbcan_message *m = urb->context;
1105 if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1107 if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
1110 switch (urb->status) {
1113 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1114 set_bit(USBCAN_DATA_OK,&m->dev->flags);
1115 set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
1116 DEBUGMSG("%s > RX flag set\n", __FUNCTION__);
1117 set_bit(USBCAN_DATA_RX,&m->dev->flags);
1118 usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
1119 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1120 wake_up_process(m->dev->comthread);
1122 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1127 /* this urb is terminated, clean up */
1128 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1129 // set_bit(USBCAN_TERMINATE,&m->dev->flags);
1130 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1133 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1137 // Try to send urb again on non significant errors
1138 retval = usb_submit_urb (urb, GFP_ATOMIC);
1140 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1141 set_bit(USBCAN_ERROR,&m->dev->flags);
1142 usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
1143 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1144 wake_up_process(m->dev->comthread);
1149 static void usbcan_kthread_free_urbs(struct usbcan_usb *dev)
1151 while(!list_empty(&dev->rx_pend_list)) {
1152 struct usbcan_message *m;
1153 m = list_first_entry(&dev->rx_pend_list, typeof(*m), list_node);
1154 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1156 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
1159 while(!list_empty(&dev->tx_pend_list)) {
1160 struct usbcan_message *m;
1161 m = list_first_entry(&dev->tx_pend_list, typeof(*m), list_node);
1162 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1164 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
1167 while(!list_empty(&dev->rx_ready_list)) {
1168 struct usbcan_message *m;
1169 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1170 list_del(&m->list_node);
1175 while(!list_empty(&dev->tx_ready_list)) {
1176 struct usbcan_message *m;
1177 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
1178 list_del(&m->list_node);
1183 while(!list_empty(&dev->tx_idle_list)) {
1184 struct usbcan_message *m;
1185 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
1186 list_del(&m->list_node);
1193 int usbcan_kthread(void *data)
1196 struct usbcan_usb *dev=(struct usbcan_usb *)data;
1197 struct msgobj_t *obj;
1199 CANMSG("Usbcan thread started...\n");
1203 obj=dev->chip->msgobj[0];
1205 INIT_LIST_HEAD(&dev->rx_pend_list);
1206 INIT_LIST_HEAD(&dev->rx_ready_list);
1207 INIT_LIST_HEAD(&dev->tx_idle_list);
1208 INIT_LIST_HEAD(&dev->tx_pend_list);
1209 INIT_LIST_HEAD(&dev->tx_ready_list);
1212 struct sched_param param = { .sched_priority = 1 };
1213 sched_setscheduler(current, SCHED_FIFO, ¶m);
1217 /* Prepare receive urbs */
1218 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1219 struct usbcan_message *m;
1220 struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
1222 CANMSG("Error allocating %d. usb receive urb\n",i);
1225 m = kzalloc(sizeof(struct usbcan_message), GFP_KERNEL);
1228 CANMSG("Error allocating %d. receive usbcan_message\n",i);
1234 usb_fill_bulk_urb(u, dev->udev,
1235 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1236 m->msg, USBCAN_TRANSFER_SIZE, usbcan_rx_callback, m);
1238 list_add_tail(&m->list_node, &dev->rx_ready_list);
1241 /* Prepare transmit urbs */
1242 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
1243 struct usbcan_message *m;
1244 struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
1246 CANMSG("Error allocating %d. usb transmit urb\n",i);
1249 m = kzalloc(sizeof(struct usbcan_message), GFP_KERNEL);
1252 CANMSG("Error allocating %d. transmit usbcan_message\n",i);
1258 usb_fill_bulk_urb(u, dev->udev,
1259 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
1260 m->msg, USBCAN_TRANSFER_SIZE, usbcan_tx_callback, m);
1262 list_add_tail(&m->list_node, &dev->tx_idle_list);
1266 set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1267 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
1269 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1270 struct usbcan_message *m;
1271 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1272 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
1274 retval=usb_submit_urb(m->u, GFP_KERNEL);
1276 CANMSG("%d. URB error %d\n",i,retval);
1277 set_bit(USBCAN_ERROR,&dev->flags);
1278 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
1282 /* an endless loop in which we are doing our work */
1285 /* We need to do a memory barrier here to be sure that
1286 the flags are visible on all CPUs. */
1289 if (!can_kthread_should_stop() && !test_bit(USBCAN_TERMINATE,&dev->flags) && (usbcan_sleep_thread(dev)<0)){
1292 /* We need to do a memory barrier here to be sure that the flags are visible on all CPUs. */
1295 if (can_kthread_should_stop() || test_bit(USBCAN_TERMINATE,&dev->flags)){
1299 clear_bit(USBCAN_DATA_OK,&dev->flags);
1303 while(!list_empty(&dev->rx_ready_list)) {
1304 struct usbcan_message *m;
1305 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1306 usbcan_kthread_read_handler(dev, m, obj);
1309 while(!list_empty(&dev->tx_ready_list)) {
1310 struct usbcan_message *m;
1311 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
1312 usbcan_kthread_write_handler(dev, m, obj);
1315 if (test_and_clear_bit(USBCAN_TX_PENDING,&dev->flags)) {
1316 usbcan_kthread_write_request_handler(dev, obj);
1320 set_bit(USBCAN_TERMINATE,&dev->flags);
1323 usbcan_kthread_free_urbs(dev);
1324 clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1326 CANMSG ("usbcan thread finished!\n");
1329 /* cleanup the thread, leave */
1330 usbcan_kthread_free_urbs(dev);
1332 CANMSG ("kernel thread terminated!\n");
1336 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1338 struct usbcan_devs *usbdevs=NULL;
1339 struct usb_host_interface *iface_desc;
1340 struct usb_endpoint_descriptor *endpoint;
1343 int retval = -ENOMEM;
1345 iface_desc = interface->cur_altsetting;
1346 if (iface_desc->desc.bNumEndpoints % 2){
1347 CANMSG("Endpoint count must be even");
1351 usbcan_chip_count = iface_desc->desc.bNumEndpoints / 2;
1353 usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
1357 memset(usbdevs, 0, sizeof(struct usbcan_devs));
1359 usbdevs->count = usbcan_chip_count;
1360 usbdevs->udev = interface_to_usbdev(interface);
1362 usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
1363 if (!usbdevs->devs) {
1366 memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
1368 for (j=0;j<usbcan_chip_count;j++){
1369 struct usbcan_usb *dev;
1372 /* allocate memory for our device state and initialize it */
1373 usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1374 if (!usbdevs->devs[j]) {
1377 memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
1378 dev=usbdevs->devs[j];
1379 spin_lock_init(&dev->list_lock);
1381 mutex_init(&dev->io_mutex);
1382 init_waitqueue_head(&dev->queue);
1383 dev->udev = usbdevs->udev;
1384 dev->interface = interface;
1386 /* set up the endpoint information */
1387 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1388 endpoint = &iface_desc->endpoint[i].desc;
1393 if ((usbdevs->devs[k]->bulk_in_endpointAddr & USB_ENDPOINT_NUMBER_MASK) == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
1397 epnum=endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1400 if (!dev->bulk_in_endpointAddr &&
1401 usb_endpoint_is_bulk_in(endpoint)) {
1402 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1403 /* we found a bulk in endpoint */
1404 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1405 dev->bulk_in_size = buffer_size;
1406 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1407 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1408 if (!dev->bulk_in_buffer) {
1409 CANMSG("Could not allocate bulk_in_buffer");
1415 if (!dev->bulk_out_endpointAddr &&
1416 usb_endpoint_is_bulk_out(endpoint)) {
1417 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1418 /* we found a bulk out endpoint */
1419 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1424 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1425 CANMSG("Could not find all bulk-in and bulk-out endpoints for chip %d",j);
1430 usb_get_dev(usbdevs->udev);
1432 /* save our data pointer in this interface device */
1433 usb_set_intfdata(interface, usbdevs);
1435 usbdevs->candev=register_hotplug_dev("usbcan", usbcan_register_devs,(void *) usbdevs);
1436 if (!(usbdevs->candev)){
1437 CANMSG("register_hotplug_dev() failed\n");
1438 goto register_error;
1441 /* let the user know what node this device is now attached to */
1442 CANMSG("USBCAN device now attached\n");
1446 // cleanup_hotplug_dev(usbdevs->candev);
1447 usb_put_dev(usbdevs->udev);
1451 for (j=0;j<usbdevs->count;j++){
1452 if (!usbdevs->devs[j]) continue;
1454 if (usbdevs->devs[j]->bulk_in_buffer)
1455 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1456 if (usbdevs->devs[j]->chip){
1457 usbdevs->devs[j]->chip->chip_data=NULL;
1459 can_checked_free(usbdevs->devs[j]);
1461 can_checked_free(usbdevs->devs);
1463 can_checked_free(usbdevs);
1469 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1470 void release_device(struct kref *refcount){
1471 struct candevice_t *candev = container_of(refcount,struct candevice_t,refcount);
1473 void release_device(struct candevice_t *candev){
1475 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
1481 cleanup_hotplug_dev(usbdevs->candev);
1484 /* Finally, release all structures in USB subsystem */
1486 panic("udev is already null on device release");
1487 usb_put_dev(usbdevs->udev);
1489 for (j=0;j<usbdevs->count;j++){
1490 if (!usbdevs->devs[j]) continue;
1492 if (usbdevs->devs[j]->bulk_in_buffer)
1493 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1494 can_checked_free(usbdevs->devs[j]);
1495 usbdevs->devs[j]=NULL;
1497 can_checked_free(usbdevs->devs);
1499 can_checked_free(usbdevs);
1501 CANMSG("USBCAN now disconnected\n");
1504 // Physically disconnected device
1505 static void usbcan_disconnect(struct usb_interface *interface)
1507 struct usbcan_devs *usbdevs;
1510 /* prevent more I/O from starting */
1513 usbdevs = usb_get_intfdata(interface);
1515 CANMSG("USBCAN device seems to be already removed\n");
1519 usb_set_intfdata(interface, NULL);
1520 deregister_hotplug_dev(usbdevs->candev);
1522 for (j=0;j<usbdevs->count;j++){
1523 if (!usbdevs->devs[j]) continue;
1524 mutex_lock(&usbdevs->devs[j]->io_mutex);
1525 usbdevs->devs[j]->interface = NULL;
1526 mutex_unlock(&usbdevs->devs[j]->io_mutex);
1531 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1532 kref_put(&usbdevs->candev->refcount,release_device);
1534 release_device(&usbdevs->candev);
1538 int usbcan_init(void){
1539 return usb_register(&usbcan_driver);
1542 void usbcan_exit(void){
1543 usb_deregister(&usbcan_driver);