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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38))
18 #include <linux/smp_lock.h>
20 #include <linux/module.h>
22 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
23 static void usbcan_disconnect(struct usb_interface *interface);
24 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
25 void release_device(struct kref *refcount);
27 void release_device(struct candevice_t *candev);
30 volatile int usbcan_chip_count=0;
32 /* table of devices that work with this driver */
33 static struct usb_device_id usbcan_table [] = {
34 { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
35 { } /* Terminating entry */
37 MODULE_DEVICE_TABLE(usb, usbcan_table);
39 static struct usb_driver usbcan_driver = {
41 .id_table = usbcan_table,
42 .probe = usbcan_probe,
43 .disconnect = usbcan_disconnect,
47 * usbcan_request_io: - reserve io or memory range for can board
48 * @candev: pointer to candevice/board which asks for io. Field @io_addr
49 * of @candev is used in most cases to define start of the range
51 * The function usbcan_request_io() is used to reserve the io-memory. If your
52 * hardware uses a dedicated memory range as hardware control registers you
53 * will have to add the code to reserve this memory as well.
54 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
55 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
56 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
57 * Return Value: The function returns zero on success or %-ENODEV on failure
60 int usbcan_request_io(struct candevice_t *candev)
62 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
65 CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
66 CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
74 * usbcan_release_io - free reserved io memory range
75 * @candev: pointer to candevice/board which releases io
77 * The function usbcan_release_io() is used to free reserved io-memory.
78 * In case you have reserved more io memory, don't forget to free it here.
79 * IO_RANGE is the io-memory range that gets released, please adjust according
80 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
81 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
82 * Return Value: The function always returns zero
85 int usbcan_release_io(struct candevice_t *candev)
91 * usbcan_reset - hardware reset routine
92 * @candev: Pointer to candevice/board structure
94 * The function usbcan_reset() is used to give a hardware reset. This is
95 * rather hardware specific so I haven't included example code. Don't forget to
96 * check the reset status of the chip before returning.
97 * Return Value: The function returns zero on success or %-ENODEV on failure
100 int usbcan_reset(struct candevice_t *candev)
106 * usbcan_init_hw_data - Initialize hardware cards
107 * @candev: Pointer to candevice/board structure
109 * The function usbcan_init_hw_data() is used to initialize the hardware
110 * structure containing information about the installed CAN-board.
111 * %RESET_ADDR represents the io-address of the hardware reset register.
112 * %NR_82527 represents the number of Intel 82527 chips on the board.
113 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
114 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
115 * the hardware uses programmable interrupts.
116 * Return Value: The function always returns zero
119 int usbcan_init_hw_data(struct candevice_t *candev)
121 candev->res_addr=RESET_ADDR;
122 candev->nr_82527_chips=0;
123 candev->nr_sja1000_chips=0;
124 candev->nr_all_chips=usbcan_chip_count;
125 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
131 * usbcan_init_obj_data - Initialize message buffers
132 * @chip: Pointer to chip specific structure
133 * @objnr: Number of the message buffer
135 * The function usbcan_init_obj_data() is used to initialize the hardware
136 * structure containing information about the different message objects on the
137 * CAN chip. In case of the sja1000 there's only one message object but on the
138 * i82527 chip there are 15.
139 * The code below is for a i82527 chip and initializes the object base addresses
140 * The entry @obj_base_addr represents the first memory address of the message
141 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
143 * Unless the hardware uses a segmented memory map, flags can be set zero.
144 * Return Value: The function always returns zero
147 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
149 chip->msgobj[objnr]->obj_base_addr=0;
155 * usbcan_program_irq - program interrupts
156 * @candev: Pointer to candevice/board structure
158 * The function usbcan_program_irq() is used for hardware that uses
159 * programmable interrupts. If your hardware doesn't use programmable interrupts
160 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
161 * leave this function unedited. Again this function is hardware specific so
162 * there's no example code.
163 * Return value: The function returns zero on success or %-ENODEV on failure
166 int usbcan_program_irq(struct candevice_t *candev)
171 int usbcan_register(struct hwspecops_t *hwspecops)
173 hwspecops->request_io = usbcan_request_io;
174 hwspecops->release_io = usbcan_release_io;
175 hwspecops->reset = usbcan_reset;
176 hwspecops->init_hw_data = usbcan_init_hw_data;
177 hwspecops->init_chip_data = usbcan_init_chip_data;
178 hwspecops->init_obj_data = usbcan_init_obj_data;
179 hwspecops->write_register = NULL;
180 hwspecops->read_register = NULL;
181 hwspecops->program_irq = usbcan_program_irq;
182 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
183 hwspecops->release_device = release_device;
188 // static int sja1000_report_error_limit_counter;
190 static void usbcan_report_error(struct canchip_t *chip,
191 unsigned sr, unsigned ir, unsigned ecc)
193 /*TODO : Error reporting from device */
196 if(sja1000_report_error_limit_counter>=100)
199 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
202 sja1000_report_error_limit_counter+=10;
204 if(sja1000_report_error_limit_counter>=100){
205 sja1000_report_error_limit_counter+=10;
206 CANMSG("Error: too many errors, reporting disabled\n");
210 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
211 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
212 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
213 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
214 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
215 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
216 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
217 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
218 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
219 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
220 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
221 if((sr&sjaIR_EI) || 1){
222 CANMSG("EI: %s %s %s\n",
223 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
224 ecc&sjaECC_DIR?"RX":"TX",
225 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
228 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
234 * usbcan_enable_configuration - enable chip configuration mode
235 * @chip: pointer to chip state structure
237 int usbcan_enable_configuration(struct canchip_t *chip)
243 * usbcan_disable_configuration - disable chip configuration mode
244 * @chip: pointer to chip state structure
246 int usbcan_disable_configuration(struct canchip_t *chip)
252 * usbcan_chip_config: - can chip configuration
253 * @chip: pointer to chip state structure
255 * This function configures chip and prepares it for message
256 * transmission and reception. The function resets chip,
257 * resets mask for acceptance of all messages by call to
258 * usbcan_extended_mask() function and then
259 * computes and sets baudrate with use of function usbcan_baud_rate().
260 * Return Value: negative value reports error.
263 int usbcan_chip_config(struct canchip_t *chip)
269 * usbcan_extended_mask: - setup of extended mask for message filtering
270 * @chip: pointer to chip state structure
271 * @code: can message acceptance code
272 * @mask: can message acceptance mask
274 * Return Value: negative value reports error.
277 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
280 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
282 u8 usbbuf[USBCAN_TRANSFER_SIZE];
287 *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
288 *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
290 retval=usb_control_msg(dev->udev,
291 usb_sndctrlpipe(dev->udev, 0),
292 USBCAN_VENDOR_EXT_MASK_SET,
294 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
295 &usbbuf, USBCAN_TRANSFER_SIZE,
300 retval = usb_control_msg(dev->udev,
301 usb_rcvctrlpipe(dev->udev, 0),
302 USBCAN_VENDOR_EXT_MASK_STATUS,
304 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
305 &usbbuf, USBCAN_TRANSFER_SIZE,
310 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
311 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
316 CANMSG("Setting extended mask failed\n");
321 * usbcan_baud_rate: - set communication parameters.
322 * @chip: pointer to chip state structure
323 * @rate: baud rate in Hz
324 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
325 * @sjw: synchronization jump width (0-3) prescaled clock cycles
326 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
327 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
329 * Return Value: negative value reports error.
332 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
333 int sampl_pt, int flags)
336 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
338 u8 usbbuf[USBCAN_TRANSFER_SIZE];
343 *(int32_t *)(usbbuf)=cpu_to_le32(rate);
344 *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
345 *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
346 *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
348 retval=usb_control_msg(dev->udev,
349 usb_sndctrlpipe(dev->udev, 0),
350 USBCAN_VENDOR_BAUD_RATE_SET,
352 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
353 &usbbuf, USBCAN_TRANSFER_SIZE,
358 retval = usb_control_msg(dev->udev,
359 usb_rcvctrlpipe(dev->udev, 0),
360 USBCAN_VENDOR_BAUD_RATE_STATUS,
362 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
363 usbbuf, USBCAN_TRANSFER_SIZE,
371 CANMSG("baud rate %d is not possible to set\n",
377 * usbcan_pre_read_config: - prepares message object for message reception
378 * @chip: pointer to chip state structure
379 * @obj: pointer to message object state structure
381 * Return Value: negative value reports error.
382 * Positive value indicates immediate reception of message.
385 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
390 #define MAX_TRANSMIT_WAIT_LOOPS 10
392 * usbcan_pre_write_config: - prepares message object for message transmission
393 * @chip: pointer to chip state structure
394 * @obj: pointer to message object state structure
395 * @msg: pointer to CAN message
397 * This function prepares selected message object for future initiation
398 * of message transmission by usbcan_send_msg() function.
399 * The CAN message data and message ID are transfered from @msg slot
400 * into chip buffer in this function.
401 * Return Value: negative value reports error.
404 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
405 struct canmsg_t *msg)
411 * usbcan_send_msg: - initiate message transmission
412 * @chip: pointer to chip state structure
413 * @obj: pointer to message object state structure
414 * @msg: pointer to CAN message
416 * This function is called after usbcan_pre_write_config() function,
417 * which prepares data in chip buffer.
418 * Return Value: negative value reports error.
421 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
422 struct canmsg_t *msg)
428 * usbcan_check_tx_stat: - checks state of transmission engine
429 * @chip: pointer to chip state structure
431 * Return Value: negative value reports error.
432 * Positive return value indicates transmission under way status.
433 * Zero value indicates finishing of all issued transmission requests.
436 int usbcan_check_tx_stat(struct canchip_t *chip)
438 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
441 if (test_bit(USBCAN_TX_PENDING,&dev->flags))
447 * usbcan_set_btregs: - configures bitrate registers
448 * @chip: pointer to chip state structure
449 * @btr0: bitrate register 0
450 * @btr1: bitrate register 1
452 * Return Value: negative value reports error.
455 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
459 u8 buf[USBCAN_TRANSFER_SIZE];
460 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
461 uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
466 retval = usb_control_msg(dev->udev,
467 usb_rcvctrlpipe(dev->udev, 0),
468 USBCAN_VENDOR_SET_BTREGS,
470 cpu_to_le16(value), cpu_to_le16(chip->chip_idx),
471 &buf, USBCAN_TRANSFER_SIZE,
482 * usbcan_start_chip: - starts chip message processing
483 * @chip: pointer to chip state structure
485 * Return Value: negative value reports error.
488 int usbcan_start_chip(struct canchip_t *chip)
491 u8 buf[USBCAN_TRANSFER_SIZE];
492 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
497 retval = usb_control_msg(dev->udev,
498 usb_rcvctrlpipe(dev->udev, 0),
499 USBCAN_VENDOR_START_CHIP,
501 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
502 &buf, USBCAN_TRANSFER_SIZE,
513 * usbcan_chip_queue_status: - gets queue status from usb device
514 * @chip: pointer to chip state structure
516 * Return Value: negative value reports error.
517 * 0 means queue is not full
518 * 1 means queue is full
521 int usbcan_chip_queue_status(struct canchip_t *chip)
524 u8 buf[USBCAN_TRANSFER_SIZE];
525 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
529 retval = usb_control_msg(dev->udev,
530 usb_rcvctrlpipe(dev->udev, 0),
531 USBCAN_VENDOR_CHECK_TX_STAT,
533 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
534 &buf, USBCAN_TRANSFER_SIZE,
538 DEBUGMSG("Chip_queue_status: %d\n",buf[0]);
544 CANMSG("Chip_queue_status error: %d\n",retval);
549 * usbcan_stop_chip: - stops chip message processing
550 * @chip: pointer to chip state structure
552 * Return Value: negative value reports error.
555 int usbcan_stop_chip(struct canchip_t *chip)
558 u8 buf[USBCAN_TRANSFER_SIZE];
559 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
564 retval = usb_control_msg(dev->udev,
565 usb_rcvctrlpipe(dev->udev, 0),
566 USBCAN_VENDOR_STOP_CHIP,
568 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
569 &buf, USBCAN_TRANSFER_SIZE,
580 * usbcan_register_devs: - attaches usb device data to the chip structure
581 * @chip: pointer to chip state structure
582 * @data: usb device data
586 int usbcan_register_devs(struct canchip_t *chip,void *data){
587 struct usbcan_devs *usbdevs=(struct usbcan_devs *)data;
589 CANMSG("Bad structure given\n");
592 if (chip->chip_idx>=usbdevs->count) {
593 CANMSG("Requested chip number is bigger than chip count\n");
597 usbdevs->devs[chip->chip_idx]->chip=chip;
598 chip->chip_data=(void *)usbdevs->devs[chip->chip_idx];
603 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
604 * @chip: pointer to chip state structure
606 * Return Value: negative value reports error.
609 int usbcan_attach_to_chip(struct canchip_t *chip)
611 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
613 /* start kernel thread */
614 dev->comthread=can_kthread_run(usbcan_kthread, (void *)dev, "usbcan_%d",chip->chip_idx);
620 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
621 * @chip: pointer to chip state structure
623 * Return Value: negative value reports error.
626 int usbcan_release_chip(struct canchip_t *chip)
628 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
630 usbcan_stop_chip(chip);
632 /* terminate the kernel thread */
633 set_bit(USBCAN_TERMINATE,&dev->flags);
634 // wake_up_process(dev->comthread);
635 can_kthread_stop(dev->comthread);
641 * usbcan_remote_request: - configures message object and asks for RTR message
642 * @chip: pointer to chip state structure
643 * @obj: pointer to message object structure
645 * Return Value: negative value reports error.
648 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
650 CANMSG("usbcan_remote_request not implemented\n");
655 * usbcan_standard_mask: - setup of mask for message filtering
656 * @chip: pointer to chip state structure
657 * @code: can message acceptance code
658 * @mask: can message acceptance mask
660 * Return Value: negative value reports error.
663 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
666 CANMSG("usbcan_standard_mask not implemented\n");
671 * usbcan_clear_objects: - clears state of all message object residing in chip
672 * @chip: pointer to chip state structure
674 * Return Value: negative value reports error.
677 int usbcan_clear_objects(struct canchip_t *chip)
679 CANMSG("usbcan_clear_objects not implemented\n");
684 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
685 * @chip: pointer to chip state structure
686 * @irqs: requested chip IRQ configuration
688 * Return Value: negative value reports error.
691 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
693 CANMSG("usbcan_config_irqs not implemented\n");
698 static void usbcan_usb_message_move_list(struct usbcan_usb *dev,
699 struct usbcan_message *m, struct list_head *head)
701 can_spin_irqflags_t flags;
702 can_spin_lock_irqsave(&dev->list_lock, flags);
703 list_del(&m->list_node);
704 list_add_tail(&m->list_node, head);
705 can_spin_unlock_irqrestore(&dev->list_lock, flags);
710 * usbcan_kthread_read_handler: - part of kthread code responsible for receive completed events
711 * @dev: pointer to usb device related structure
712 * @obj: pointer to attached message object description
714 * The main purpose of this function is to read message from usb urb
715 * and transfer message contents to CAN queue ends.
716 * This subroutine is called by
720 void usbcan_kthread_read_handler(struct usbcan_usb *dev, struct usbcan_message *m,
721 struct msgobj_t *obj)
726 DEBUGMSG("USBCAN RX handler\n");
728 if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
729 CANMSG("Strange, Rx handler USBCAN_MESSAGE_DATA_OK not set\n");
733 if (!(dev->chip)||!(dev->chip->flags & CHIP_CONFIGURED)) {
734 CANMSG("Destination chip not found\n");
739 DEBUGMSG("USBCAN Thread has received a message\n");
741 len=*(u8 *)(m->msg+1);
742 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
743 obj->rx_msg.length = len;
745 obj->rx_msg.flags=le16_to_cpu(*(u16 *)(m->msg+2));
746 obj->rx_msg.id=le32_to_cpu((*(u32 *)(m->msg+4)));
748 for(ptr=m->msg+8,i=0; i < len; ptr++,i++) {
749 obj->rx_msg.data[i]=*ptr;
752 // fill CAN message timestamp
753 can_filltimestamp(&obj->rx_msg.timestamp);
754 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
757 DEBUGMSG("Renewing RX urb\n");
759 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
760 retval = usb_submit_urb (m->u, GFP_KERNEL);
762 CANMSG("URB error %d\n", retval);
763 set_bit(USBCAN_ERROR,&dev->flags);
768 * usbcan_kthread_write_handler: - part of kthread code responsible for transmit done events
769 * @dev: pointer to usb device related structure
770 * @obj: pointer to attached message object description
772 * The main purpose of this function is to free allocated resources on transmit done event
773 * This subroutine is called by
777 void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct usbcan_message *m,
778 struct msgobj_t *obj)
780 if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
781 CANMSG("Strange, Tx handler USBCAN_MESSAGE_DATA_OK not set\n");
785 DEBUGMSG("USBCAN Message successfully sent\n");
788 // Do local transmitted message distribution if enabled
790 // fill CAN message timestamp
791 can_filltimestamp(&m->slot->msg.timestamp);
793 m->slot->msg.flags |= MSG_LOCAL;
794 canque_filter_msg2edges(obj->qends, &m->slot->msg);
796 // Free transmitted slot
797 canque_free_outslot(obj->qends, m->qedge, m->slot);
801 /*FIXME - why there*/
802 can_msgobj_clear_fl(obj,TX_PENDING);
805 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
807 set_bit(USBCAN_TX_PENDING,&dev->flags);
809 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
813 * usbcan_kthread_write_request_handler: - part of kthread code responsible for sending transmit urbs
814 * @dev: pointer to usb device related structure
815 * @obj: pointer to attached message object description
817 * The main purpose of this function is to create a usb transmit safe object
818 * and send it via free transmit usb urb
819 * This subroutine is called by
823 void usbcan_kthread_write_request_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
824 int i, cmd, len, retval;
826 struct usbcan_message *m;
828 if(list_empty(&dev->tx_idle_list)) {
829 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
833 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
835 cmd=canque_test_outslot(obj->qends, &m->qedge, &m->slot);
837 DEBUGMSG("USBCAN Sending a message\n");
839 can_msgobj_set_fl(obj,TX_PENDING);
840 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
843 len = m->slot->msg.length;
844 if(len > CAN_MSG_LENGTH)
845 len = CAN_MSG_LENGTH;
846 *(u8 *)(m->msg+1)=len & 0xFF;
847 *(u16 *)(m->msg+2)=cpu_to_le16(m->slot->msg.flags);
848 *(u32 *)(m->msg+4)=cpu_to_le32(m->slot->msg.id);
850 for(ptr=m->msg+8, i=0; i < len; ptr++,i++) {
851 *ptr=m->slot->msg.data[i] & 0xFF;
853 for(; i < 8; ptr++,i++) {
858 usbcan_usb_message_move_list(dev, m, &dev->tx_pend_list);
860 retval = usb_submit_urb (m->u, GFP_KERNEL);
862 CANMSG("%d. URB error %d\n",i,retval);
863 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
865 canque_notify_inends(m->qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
866 canque_free_outslot(obj->qends, m->qedge, m->slot);
868 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
870 set_bit(USBCAN_TX_PENDING,&dev->flags);
873 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
880 * usbcan_irq_handler: - interrupt service routine
881 * @irq: interrupt vector number, this value is system specific
882 * @chip: pointer to chip state structure
884 * Interrupt handler is activated when state of CAN controller chip changes,
885 * there is message to be read or there is more space for new messages or
886 * error occurs. The receive events results in reading of the message from
887 * CAN controller chip and distribution of message through attached
891 int usbcan_irq_handler(int irq, struct canchip_t *chip)
893 return CANCHIP_IRQ_HANDLED;
897 * usbcan_wakeup_tx: - wakeups TX processing
898 * @chip: pointer to chip state structure
899 * @obj: pointer to message object structure
901 * Function is responsible for initiating message transmition.
902 * It is responsible for clearing of object TX_REQUEST flag
904 * Return Value: negative value reports error.
907 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
909 struct usbcan_usb *dev=(struct usbcan_usb *)chip->chip_data;
911 DEBUGMSG("Trying to send message\n");
912 can_preempt_disable();
914 can_msgobj_set_fl(obj,TX_PENDING);
915 can_msgobj_set_fl(obj,TX_REQUEST);
916 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
917 can_msgobj_clear_fl(obj,TX_REQUEST);
919 if (test_and_clear_bit(USBCAN_FREE_TX_URB,&dev->flags)){
921 set_bit(USBCAN_TX_PENDING,&dev->flags);
922 if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
923 wake_up_process(dev->comthread);
926 can_msgobj_clear_fl(obj,TX_LOCK);
927 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
928 CANMSG("TX looping in usbcan_wakeup_tx\n");
931 can_preempt_enable();
935 int usbcan_chipregister(struct chipspecops_t *chipspecops)
937 CANMSG("initializing usbcan chip operations\n");
938 chipspecops->chip_config=usbcan_chip_config;
939 chipspecops->baud_rate=usbcan_baud_rate;
940 chipspecops->standard_mask=usbcan_standard_mask;
941 chipspecops->extended_mask=usbcan_extended_mask;
942 chipspecops->message15_mask=usbcan_extended_mask;
943 chipspecops->clear_objects=usbcan_clear_objects;
944 chipspecops->config_irqs=usbcan_config_irqs;
945 chipspecops->pre_read_config=usbcan_pre_read_config;
946 chipspecops->pre_write_config=usbcan_pre_write_config;
947 chipspecops->send_msg=usbcan_send_msg;
948 chipspecops->check_tx_stat=usbcan_check_tx_stat;
949 chipspecops->wakeup_tx=usbcan_wakeup_tx;
950 chipspecops->remote_request=usbcan_remote_request;
951 chipspecops->enable_configuration=usbcan_enable_configuration;
952 chipspecops->disable_configuration=usbcan_disable_configuration;
953 chipspecops->attach_to_chip=usbcan_attach_to_chip;
954 chipspecops->release_chip=usbcan_release_chip;
955 chipspecops->set_btregs=usbcan_set_btregs;
956 chipspecops->start_chip=usbcan_start_chip;
957 chipspecops->stop_chip=usbcan_stop_chip;
958 chipspecops->irq_handler=usbcan_irq_handler;
959 chipspecops->irq_accept=NULL;
964 * usbcan_fill_chipspecops - fills chip specific operations
965 * @chip: pointer to chip representation structure
967 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
969 * Return Value: returns negative number in the case of fail
971 int usbcan_fill_chipspecops(struct canchip_t *chip)
973 chip->chip_type="usbcan";
975 usbcan_chipregister(chip->chipspecops);
980 * usbcan_init_chip_data - Initialize chips
981 * @candev: Pointer to candevice/board structure
982 * @chipnr: Number of the CAN chip on the hardware card
984 * The function usbcan_init_chip_data() is used to initialize the hardware
985 * structure containing information about the CAN chips.
986 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
988 * The @chip_base_addr entry represents the start of the 'official' memory map
989 * of the installed chip. It's likely that this is the same as the @io_addr
990 * argument supplied at module loading time.
991 * The @clock entry holds the chip clock value in Hz.
992 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
993 * register. Options defined in the %sja1000.h file:
994 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
995 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
996 * register. Options defined in the %sja1000.h file:
997 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
998 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
999 * The entry @int_clk_reg holds hardware specific options for the Clock Out
1000 * register. Options defined in the %i82527.h file:
1001 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
1002 * The entry @int_bus_reg holds hardware specific options for the Bus
1003 * Configuration register. Options defined in the %i82527.h file:
1004 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
1005 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
1006 * register. Options defined in the %i82527.h file:
1007 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
1008 * Return Value: The function always returns zero
1009 * File: src/usbcan.c
1011 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
1013 struct canchip_t *chip=candev->chip[chipnr];
1015 usbcan_fill_chipspecops(chip);
1017 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM|CHIP_KEEP_DATA;
1018 candev->chip[chipnr]->chip_base_addr=0;
1019 candev->chip[chipnr]->clock = 0;
1025 /** *********************************
1026 * USB related functions
1027 * ********************************* */
1029 static int usbcan_sleep_thread(struct usbcan_usb *dev)
1033 /* Wait until a signal arrives or we are woken up */
1036 set_current_state(TASK_INTERRUPTIBLE);
1037 if (signal_pending(current)) {
1042 can_kthread_should_stop() ||
1043 test_bit(USBCAN_DATA_OK,&dev->flags) ||
1044 test_bit(USBCAN_TX_PENDING,&dev->flags) ||
1045 test_bit(USBCAN_TERMINATE,&dev->flags) ||
1046 test_bit(USBCAN_ERROR,&dev->flags)
1051 __set_current_state(TASK_RUNNING);
1055 static void usbcan_tx_callback(struct urb *urb)
1057 struct usbcan_message *m = urb->context;
1060 if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1062 if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
1065 switch (urb->status) {
1068 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1069 set_bit(USBCAN_DATA_OK,&m->dev->flags);
1070 set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
1071 DEBUGMSG("%s > TX flag set\n", __FUNCTION__);
1072 set_bit(USBCAN_DATA_TX,&m->dev->flags);
1073 usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
1074 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1075 wake_up_process(m->dev->comthread);
1077 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1082 /* this urb is terminated, clean up */
1083 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1084 // set_bit(USBCAN_TERMINATE,&m->dev->flags);
1085 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1088 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1092 // Try to send urb again on non significant errors
1093 retval = usb_submit_urb (urb, GFP_ATOMIC);
1095 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1096 set_bit(USBCAN_ERROR,&m->dev->flags);
1097 usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
1098 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1099 wake_up_process(m->dev->comthread);
1103 static void usbcan_rx_callback(struct urb *urb)
1105 struct usbcan_message *m = urb->context;
1108 if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1110 if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
1113 switch (urb->status) {
1116 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1117 set_bit(USBCAN_DATA_OK,&m->dev->flags);
1118 set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
1119 DEBUGMSG("%s > RX flag set\n", __FUNCTION__);
1120 set_bit(USBCAN_DATA_RX,&m->dev->flags);
1121 usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
1122 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1123 wake_up_process(m->dev->comthread);
1125 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1130 /* this urb is terminated, clean up */
1131 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1132 // set_bit(USBCAN_TERMINATE,&m->dev->flags);
1133 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1136 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1140 // Try to send urb again on non significant errors
1141 retval = usb_submit_urb (urb, GFP_ATOMIC);
1143 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1144 set_bit(USBCAN_ERROR,&m->dev->flags);
1145 usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
1146 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1147 wake_up_process(m->dev->comthread);
1152 static void usbcan_kthread_free_urbs(struct usbcan_usb *dev)
1154 while(!list_empty(&dev->rx_pend_list)) {
1155 struct usbcan_message *m;
1156 m = list_first_entry(&dev->rx_pend_list, typeof(*m), list_node);
1157 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1159 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
1162 while(!list_empty(&dev->tx_pend_list)) {
1163 struct usbcan_message *m;
1164 m = list_first_entry(&dev->tx_pend_list, typeof(*m), list_node);
1165 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1167 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
1170 while(!list_empty(&dev->rx_ready_list)) {
1171 struct usbcan_message *m;
1172 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1173 list_del(&m->list_node);
1178 while(!list_empty(&dev->tx_ready_list)) {
1179 struct usbcan_message *m;
1180 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
1181 list_del(&m->list_node);
1186 while(!list_empty(&dev->tx_idle_list)) {
1187 struct usbcan_message *m;
1188 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
1189 list_del(&m->list_node);
1196 int usbcan_kthread(void *data)
1199 struct usbcan_usb *dev=(struct usbcan_usb *)data;
1200 struct msgobj_t *obj;
1202 CANMSG("Usbcan thread started...\n");
1206 obj=dev->chip->msgobj[0];
1208 INIT_LIST_HEAD(&dev->rx_pend_list);
1209 INIT_LIST_HEAD(&dev->rx_ready_list);
1210 INIT_LIST_HEAD(&dev->tx_idle_list);
1211 INIT_LIST_HEAD(&dev->tx_pend_list);
1212 INIT_LIST_HEAD(&dev->tx_ready_list);
1215 struct sched_param param = { .sched_priority = 1 };
1216 sched_setscheduler(current, SCHED_FIFO, ¶m);
1220 /* Prepare receive urbs */
1221 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1222 struct usbcan_message *m;
1223 struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
1225 CANMSG("Error allocating %d. usb receive urb\n",i);
1228 m = kzalloc(sizeof(struct usbcan_message), GFP_KERNEL);
1231 CANMSG("Error allocating %d. receive usbcan_message\n",i);
1237 usb_fill_bulk_urb(u, dev->udev,
1238 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1239 m->msg, USBCAN_TRANSFER_SIZE, usbcan_rx_callback, m);
1241 list_add_tail(&m->list_node, &dev->rx_ready_list);
1244 /* Prepare transmit urbs */
1245 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
1246 struct usbcan_message *m;
1247 struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
1249 CANMSG("Error allocating %d. usb transmit urb\n",i);
1252 m = kzalloc(sizeof(struct usbcan_message), GFP_KERNEL);
1255 CANMSG("Error allocating %d. transmit usbcan_message\n",i);
1261 usb_fill_bulk_urb(u, dev->udev,
1262 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
1263 m->msg, USBCAN_TRANSFER_SIZE, usbcan_tx_callback, m);
1265 list_add_tail(&m->list_node, &dev->tx_idle_list);
1269 set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1270 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
1272 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1273 struct usbcan_message *m;
1274 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1275 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
1277 retval=usb_submit_urb(m->u, GFP_KERNEL);
1279 CANMSG("%d. URB error %d\n",i,retval);
1280 set_bit(USBCAN_ERROR,&dev->flags);
1281 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
1285 /* an endless loop in which we are doing our work */
1288 /* We need to do a memory barrier here to be sure that
1289 the flags are visible on all CPUs. */
1292 if (!can_kthread_should_stop() && !test_bit(USBCAN_TERMINATE,&dev->flags) && (usbcan_sleep_thread(dev)<0)){
1295 /* We need to do a memory barrier here to be sure that the flags are visible on all CPUs. */
1298 if (can_kthread_should_stop() || test_bit(USBCAN_TERMINATE,&dev->flags)){
1302 clear_bit(USBCAN_DATA_OK,&dev->flags);
1306 while(!list_empty(&dev->rx_ready_list)) {
1307 struct usbcan_message *m;
1308 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1309 usbcan_kthread_read_handler(dev, m, obj);
1312 while(!list_empty(&dev->tx_ready_list)) {
1313 struct usbcan_message *m;
1314 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
1315 usbcan_kthread_write_handler(dev, m, obj);
1318 if (test_and_clear_bit(USBCAN_TX_PENDING,&dev->flags)) {
1319 usbcan_kthread_write_request_handler(dev, obj);
1323 set_bit(USBCAN_TERMINATE,&dev->flags);
1326 usbcan_kthread_free_urbs(dev);
1327 clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1329 CANMSG ("usbcan thread finished!\n");
1332 /* cleanup the thread, leave */
1333 usbcan_kthread_free_urbs(dev);
1335 CANMSG ("kernel thread terminated!\n");
1339 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1341 struct usbcan_devs *usbdevs=NULL;
1342 struct usb_host_interface *iface_desc;
1343 struct usb_endpoint_descriptor *endpoint;
1346 int retval = -ENOMEM;
1348 iface_desc = interface->cur_altsetting;
1349 if (iface_desc->desc.bNumEndpoints % 2){
1350 CANMSG("Endpoint count must be even");
1354 usbcan_chip_count = iface_desc->desc.bNumEndpoints / 2;
1356 usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
1360 memset(usbdevs, 0, sizeof(struct usbcan_devs));
1362 usbdevs->count = usbcan_chip_count;
1363 usbdevs->udev = interface_to_usbdev(interface);
1365 usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
1366 if (!usbdevs->devs) {
1369 memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
1371 for (j=0;j<usbcan_chip_count;j++){
1372 struct usbcan_usb *dev;
1375 /* allocate memory for our device state and initialize it */
1376 usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1377 if (!usbdevs->devs[j]) {
1380 memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
1381 dev=usbdevs->devs[j];
1382 spin_lock_init(&dev->list_lock);
1384 mutex_init(&dev->io_mutex);
1385 init_waitqueue_head(&dev->queue);
1386 dev->udev = usbdevs->udev;
1387 dev->interface = interface;
1389 /* set up the endpoint information */
1390 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1391 endpoint = &iface_desc->endpoint[i].desc;
1396 if ((usbdevs->devs[k]->bulk_in_endpointAddr & USB_ENDPOINT_NUMBER_MASK) == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
1400 epnum=endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1403 if (!dev->bulk_in_endpointAddr &&
1404 usb_endpoint_is_bulk_in(endpoint)) {
1405 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1406 /* we found a bulk in endpoint */
1407 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1408 dev->bulk_in_size = buffer_size;
1409 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1410 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1411 if (!dev->bulk_in_buffer) {
1412 CANMSG("Could not allocate bulk_in_buffer");
1418 if (!dev->bulk_out_endpointAddr &&
1419 usb_endpoint_is_bulk_out(endpoint)) {
1420 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1421 /* we found a bulk out endpoint */
1422 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1427 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1428 CANMSG("Could not find all bulk-in and bulk-out endpoints for chip %d",j);
1433 usb_get_dev(usbdevs->udev);
1435 /* save our data pointer in this interface device */
1436 usb_set_intfdata(interface, usbdevs);
1438 usbdevs->candev=register_hotplug_dev("usbcan", usbcan_register_devs,(void *) usbdevs);
1439 if (!(usbdevs->candev)){
1440 CANMSG("register_hotplug_dev() failed\n");
1441 goto register_error;
1444 /* let the user know what node this device is now attached to */
1445 CANMSG("USBCAN device now attached\n");
1449 // cleanup_hotplug_dev(usbdevs->candev);
1450 usb_put_dev(usbdevs->udev);
1454 for (j=0;j<usbdevs->count;j++){
1455 if (!usbdevs->devs[j]) continue;
1457 if (usbdevs->devs[j]->bulk_in_buffer)
1458 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1459 if (usbdevs->devs[j]->chip){
1460 usbdevs->devs[j]->chip->chip_data=NULL;
1462 can_checked_free(usbdevs->devs[j]);
1464 can_checked_free(usbdevs->devs);
1466 can_checked_free(usbdevs);
1472 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1473 void release_device(struct kref *refcount){
1474 struct candevice_t *candev = container_of(refcount,struct candevice_t,refcount);
1476 void release_device(struct candevice_t *candev){
1478 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
1484 cleanup_hotplug_dev(usbdevs->candev);
1487 /* Finally, release all structures in USB subsystem */
1489 panic("udev is already null on device release");
1490 usb_put_dev(usbdevs->udev);
1492 for (j=0;j<usbdevs->count;j++){
1493 if (!usbdevs->devs[j]) continue;
1495 if (usbdevs->devs[j]->bulk_in_buffer)
1496 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1497 can_checked_free(usbdevs->devs[j]);
1498 usbdevs->devs[j]=NULL;
1500 can_checked_free(usbdevs->devs);
1502 can_checked_free(usbdevs);
1504 CANMSG("USBCAN now disconnected\n");
1507 // Physically disconnected device
1508 static void usbcan_disconnect(struct usb_interface *interface)
1510 struct usbcan_devs *usbdevs;
1513 /* prevent more I/O from starting */
1514 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38))
1518 usbdevs = usb_get_intfdata(interface);
1520 CANMSG("USBCAN device seems to be already removed\n");
1521 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38))
1526 usb_set_intfdata(interface, NULL);
1527 deregister_hotplug_dev(usbdevs->candev);
1529 for (j=0;j<usbdevs->count;j++){
1530 if (!usbdevs->devs[j]) continue;
1531 mutex_lock(&usbdevs->devs[j]->io_mutex);
1532 usbdevs->devs[j]->interface = NULL;
1533 mutex_unlock(&usbdevs->devs[j]->io_mutex);
1536 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38))
1540 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1541 kref_put(&usbdevs->candev->refcount,release_device);
1543 release_device(&usbdevs->candev);
1547 int usbcan_init(void){
1548 return usb_register(&usbcan_driver);
1551 void usbcan_exit(void){
1552 usb_deregister(&usbcan_driver);