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 set_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
755 DEBUGMSG("Renewing RX urb\n");
757 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
758 retval = usb_submit_urb (m->u, GFP_KERNEL);
760 CANMSG("URB error %d\n", retval);
761 set_bit(USBCAN_ERROR,&dev->flags);
766 * usbcan_kthread_write_handler: - part of kthread code responsible for transmit done events
767 * @dev: pointer to usb device related structure
768 * @obj: pointer to attached message object description
770 * The main purpose of this function is to free allocated resources on transmit done event
771 * This subroutine is called by
775 void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct usbcan_message *m,
776 struct msgobj_t *obj)
778 if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
779 CANMSG("Strange, Tx handler USBCAN_MESSAGE_DATA_OK not set\n");
783 DEBUGMSG("USBCAN Message successfully sent\n");
786 // Do local transmitted message distribution if enabled
788 // fill CAN message timestamp
789 can_filltimestamp(&m->slot->msg.timestamp);
791 m->slot->msg.flags |= MSG_LOCAL;
792 canque_filter_msg2edges(obj->qends, &m->slot->msg);
794 // Free transmitted slot
795 canque_free_outslot(obj->qends, m->qedge, m->slot);
799 /*FIXME - why there*/
800 can_msgobj_clear_fl(obj,TX_PENDING);
803 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
804 set_bit(USBCAN_MESSAGE_FREE,&m->flags);
806 set_bit(USBCAN_TX_PENDING,&dev->flags);
808 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
812 * usbcan_kthread_write_request_handler: - part of kthread code responsible for sending transmit urbs
813 * @dev: pointer to usb device related structure
814 * @obj: pointer to attached message object description
816 * The main purpose of this function is to create a usb transmit safe object
817 * and send it via free transmit usb urb
818 * This subroutine is called by
822 void usbcan_kthread_write_request_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
823 int i, cmd, len, retval;
825 struct usbcan_message *m;
827 if(list_empty(&dev->tx_idle_list)) {
828 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
832 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
834 cmd=canque_test_outslot(obj->qends, &m->qedge, &m->slot);
836 DEBUGMSG("USBCAN Sending a message\n");
838 can_msgobj_set_fl(obj,TX_PENDING);
839 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
840 clear_bit(USBCAN_MESSAGE_FREE,&m->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++) {
857 set_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
859 usbcan_usb_message_move_list(dev, m, &dev->tx_pend_list);
861 retval = usb_submit_urb (m->u, GFP_KERNEL);
863 CANMSG("%d. URB error %d\n",i,retval);
864 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
865 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
866 set_bit(USBCAN_MESSAGE_FREE,&m->flags);
868 canque_notify_inends(m->qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
869 canque_free_outslot(obj->qends, m->qedge, m->slot);
871 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
873 set_bit(USBCAN_TX_PENDING,&dev->flags);
876 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
883 * usbcan_irq_handler: - interrupt service routine
884 * @irq: interrupt vector number, this value is system specific
885 * @chip: pointer to chip state structure
887 * Interrupt handler is activated when state of CAN controller chip changes,
888 * there is message to be read or there is more space for new messages or
889 * error occurs. The receive events results in reading of the message from
890 * CAN controller chip and distribution of message through attached
894 int usbcan_irq_handler(int irq, struct canchip_t *chip)
896 return CANCHIP_IRQ_HANDLED;
900 * usbcan_wakeup_tx: - wakeups TX processing
901 * @chip: pointer to chip state structure
902 * @obj: pointer to message object structure
904 * Function is responsible for initiating message transmition.
905 * It is responsible for clearing of object TX_REQUEST flag
907 * Return Value: negative value reports error.
910 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
912 struct usbcan_usb *dev=(struct usbcan_usb *)chip->chip_data;
914 DEBUGMSG("Trying to send message\n");
915 can_preempt_disable();
917 can_msgobj_set_fl(obj,TX_PENDING);
918 can_msgobj_set_fl(obj,TX_REQUEST);
919 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
920 can_msgobj_clear_fl(obj,TX_REQUEST);
922 if (test_and_clear_bit(USBCAN_FREE_TX_URB,&dev->flags)){
924 set_bit(USBCAN_TX_PENDING,&dev->flags);
925 if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
926 wake_up_process(dev->comthread);
929 can_msgobj_clear_fl(obj,TX_LOCK);
930 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
931 CANMSG("TX looping in usbcan_wakeup_tx\n");
934 can_preempt_enable();
938 int usbcan_chipregister(struct chipspecops_t *chipspecops)
940 CANMSG("initializing usbcan chip operations\n");
941 chipspecops->chip_config=usbcan_chip_config;
942 chipspecops->baud_rate=usbcan_baud_rate;
943 chipspecops->standard_mask=usbcan_standard_mask;
944 chipspecops->extended_mask=usbcan_extended_mask;
945 chipspecops->message15_mask=usbcan_extended_mask;
946 chipspecops->clear_objects=usbcan_clear_objects;
947 chipspecops->config_irqs=usbcan_config_irqs;
948 chipspecops->pre_read_config=usbcan_pre_read_config;
949 chipspecops->pre_write_config=usbcan_pre_write_config;
950 chipspecops->send_msg=usbcan_send_msg;
951 chipspecops->check_tx_stat=usbcan_check_tx_stat;
952 chipspecops->wakeup_tx=usbcan_wakeup_tx;
953 chipspecops->remote_request=usbcan_remote_request;
954 chipspecops->enable_configuration=usbcan_enable_configuration;
955 chipspecops->disable_configuration=usbcan_disable_configuration;
956 chipspecops->attach_to_chip=usbcan_attach_to_chip;
957 chipspecops->release_chip=usbcan_release_chip;
958 chipspecops->set_btregs=usbcan_set_btregs;
959 chipspecops->start_chip=usbcan_start_chip;
960 chipspecops->stop_chip=usbcan_stop_chip;
961 chipspecops->irq_handler=usbcan_irq_handler;
962 chipspecops->irq_accept=NULL;
967 * usbcan_fill_chipspecops - fills chip specific operations
968 * @chip: pointer to chip representation structure
970 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
972 * Return Value: returns negative number in the case of fail
974 int usbcan_fill_chipspecops(struct canchip_t *chip)
976 chip->chip_type="usbcan";
978 usbcan_chipregister(chip->chipspecops);
983 * usbcan_init_chip_data - Initialize chips
984 * @candev: Pointer to candevice/board structure
985 * @chipnr: Number of the CAN chip on the hardware card
987 * The function usbcan_init_chip_data() is used to initialize the hardware
988 * structure containing information about the CAN chips.
989 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
991 * The @chip_base_addr entry represents the start of the 'official' memory map
992 * of the installed chip. It's likely that this is the same as the @io_addr
993 * argument supplied at module loading time.
994 * The @clock entry holds the chip clock value in Hz.
995 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
996 * register. Options defined in the %sja1000.h file:
997 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
998 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
999 * register. Options defined in the %sja1000.h file:
1000 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
1001 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
1002 * The entry @int_clk_reg holds hardware specific options for the Clock Out
1003 * register. Options defined in the %i82527.h file:
1004 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
1005 * The entry @int_bus_reg holds hardware specific options for the Bus
1006 * Configuration register. Options defined in the %i82527.h file:
1007 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
1008 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
1009 * register. Options defined in the %i82527.h file:
1010 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
1011 * Return Value: The function always returns zero
1012 * File: src/usbcan.c
1014 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
1016 struct canchip_t *chip=candev->chip[chipnr];
1018 usbcan_fill_chipspecops(chip);
1020 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM|CHIP_KEEP_DATA;
1021 candev->chip[chipnr]->chip_base_addr=0;
1022 candev->chip[chipnr]->clock = 0;
1028 /** *********************************
1029 * USB related functions
1030 * ********************************* */
1032 static int usbcan_sleep_thread(struct usbcan_usb *dev)
1036 /* Wait until a signal arrives or we are woken up */
1039 set_current_state(TASK_INTERRUPTIBLE);
1040 if (signal_pending(current)) {
1045 can_kthread_should_stop() ||
1046 test_bit(USBCAN_DATA_OK,&dev->flags) ||
1047 test_bit(USBCAN_TX_PENDING,&dev->flags) ||
1048 test_bit(USBCAN_TERMINATE,&dev->flags) ||
1049 test_bit(USBCAN_ERROR,&dev->flags)
1054 __set_current_state(TASK_RUNNING);
1058 static void usbcan_tx_callback(struct urb *urb)
1060 struct usbcan_message *m = urb->context;
1063 if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1065 if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
1068 switch (urb->status) {
1071 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1072 set_bit(USBCAN_DATA_OK,&m->dev->flags);
1073 set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
1074 DEBUGMSG("%s > TX flag set\n", __FUNCTION__);
1075 set_bit(USBCAN_DATA_TX,&m->dev->flags);
1076 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1077 usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
1078 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1079 wake_up_process(m->dev->comthread);
1081 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1086 /* this urb is terminated, clean up */
1087 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1088 // set_bit(USBCAN_TERMINATE,&m->dev->flags);
1089 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1090 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1093 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1097 // Try to send urb again on non significant errors
1098 retval = usb_submit_urb (urb, GFP_ATOMIC);
1100 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1101 set_bit(USBCAN_ERROR,&m->dev->flags);
1102 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1103 usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
1104 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1105 wake_up_process(m->dev->comthread);
1109 static void usbcan_rx_callback(struct urb *urb)
1111 struct usbcan_message *m = urb->context;
1114 if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1116 if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
1119 switch (urb->status) {
1122 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1123 set_bit(USBCAN_DATA_OK,&m->dev->flags);
1124 set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
1125 DEBUGMSG("%s > RX flag set\n", __FUNCTION__);
1126 set_bit(USBCAN_DATA_RX,&m->dev->flags);
1127 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1128 usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
1129 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1130 wake_up_process(m->dev->comthread);
1132 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1137 /* this urb is terminated, clean up */
1138 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1139 // set_bit(USBCAN_TERMINATE,&m->dev->flags);
1140 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1141 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1144 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1148 // Try to send urb again on non significant errors
1149 retval = usb_submit_urb (urb, GFP_ATOMIC);
1151 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1152 set_bit(USBCAN_ERROR,&m->dev->flags);
1153 clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1154 usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
1155 if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
1156 wake_up_process(m->dev->comthread);
1161 static void usbcan_kthread_free_urbs(struct usbcan_usb *dev)
1163 while(!list_empty(&dev->rx_pend_list)) {
1164 struct usbcan_message *m;
1165 m = list_first_entry(&dev->rx_pend_list, typeof(*m), list_node);
1166 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1168 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
1171 while(!list_empty(&dev->tx_pend_list)) {
1172 struct usbcan_message *m;
1173 m = list_first_entry(&dev->tx_pend_list, typeof(*m), list_node);
1174 set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
1176 usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
1179 while(!list_empty(&dev->rx_ready_list)) {
1180 struct usbcan_message *m;
1181 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1182 list_del(&m->list_node);
1187 while(!list_empty(&dev->tx_ready_list)) {
1188 struct usbcan_message *m;
1189 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
1190 list_del(&m->list_node);
1195 while(!list_empty(&dev->tx_idle_list)) {
1196 struct usbcan_message *m;
1197 m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
1198 list_del(&m->list_node);
1205 int usbcan_kthread(void *data)
1208 struct usbcan_usb *dev=(struct usbcan_usb *)data;
1209 struct msgobj_t *obj;
1211 CANMSG("Usbcan thread started...\n");
1215 obj=dev->chip->msgobj[0];
1217 INIT_LIST_HEAD(&dev->rx_pend_list);
1218 INIT_LIST_HEAD(&dev->rx_ready_list);
1219 INIT_LIST_HEAD(&dev->tx_idle_list);
1220 INIT_LIST_HEAD(&dev->tx_pend_list);
1221 INIT_LIST_HEAD(&dev->tx_ready_list);
1224 struct sched_param param = { .sched_priority = 1 };
1225 sched_setscheduler(current, SCHED_FIFO, ¶m);
1229 /* Prepare receive urbs */
1230 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1231 struct usbcan_message *m;
1232 struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
1234 CANMSG("Error allocating %d. usb receive urb\n",i);
1237 m = kmalloc(sizeof(struct usbcan_message), GFP_KERNEL);
1240 CANMSG("Error allocating %d. receive usbcan_message\n",i);
1246 usb_fill_bulk_urb(u, dev->udev,
1247 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1248 m->msg, USBCAN_TRANSFER_SIZE, usbcan_rx_callback, m);
1249 set_bit(USBCAN_MESSAGE_TYPE_RX, &m->flags);
1250 list_add_tail(&m->list_node, &dev->rx_ready_list);
1253 /* Prepare transmit urbs */
1254 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
1255 struct usbcan_message *m;
1256 struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
1258 CANMSG("Error allocating %d. usb transmit urb\n",i);
1261 m = kmalloc(sizeof(struct usbcan_message), GFP_KERNEL);
1264 CANMSG("Error allocating %d. transmit usbcan_message\n",i);
1270 usb_fill_bulk_urb(u, dev->udev,
1271 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
1272 m->msg, USBCAN_TRANSFER_SIZE, usbcan_tx_callback, m);
1274 set_bit(USBCAN_MESSAGE_FREE,&m->flags);
1275 set_bit(USBCAN_MESSAGE_TYPE_TX,&m->flags);
1276 list_add_tail(&m->list_node, &dev->tx_idle_list);
1280 set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1281 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
1283 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1284 struct usbcan_message *m;
1285 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1286 set_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
1287 usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
1289 retval=usb_submit_urb(m->u, GFP_KERNEL);
1291 CANMSG("%d. URB error %d\n",i,retval);
1292 set_bit(USBCAN_ERROR,&dev->flags);
1293 usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
1297 /* an endless loop in which we are doing our work */
1300 /* We need to do a memory barrier here to be sure that
1301 the flags are visible on all CPUs. */
1304 if (!can_kthread_should_stop() && !test_bit(USBCAN_TERMINATE,&dev->flags) && (usbcan_sleep_thread(dev)<0)){
1307 /* We need to do a memory barrier here to be sure that the flags are visible on all CPUs. */
1310 if (can_kthread_should_stop() || test_bit(USBCAN_TERMINATE,&dev->flags)){
1314 clear_bit(USBCAN_DATA_OK,&dev->flags);
1318 while(!list_empty(&dev->rx_ready_list)) {
1319 struct usbcan_message *m;
1320 m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
1321 usbcan_kthread_read_handler(dev, m, obj);
1324 while(!list_empty(&dev->tx_ready_list)) {
1325 struct usbcan_message *m;
1326 m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
1327 usbcan_kthread_write_handler(dev, m, obj);
1330 if (test_and_clear_bit(USBCAN_TX_PENDING,&dev->flags)) {
1331 usbcan_kthread_write_request_handler(dev, obj);
1335 set_bit(USBCAN_TERMINATE,&dev->flags);
1338 usbcan_kthread_free_urbs(dev);
1339 clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1341 CANMSG ("usbcan thread finished!\n");
1344 /* cleanup the thread, leave */
1345 usbcan_kthread_free_urbs(dev);
1347 CANMSG ("kernel thread terminated!\n");
1351 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1353 struct usbcan_devs *usbdevs=NULL;
1354 struct usb_host_interface *iface_desc;
1355 struct usb_endpoint_descriptor *endpoint;
1358 int retval = -ENOMEM;
1360 iface_desc = interface->cur_altsetting;
1361 if (iface_desc->desc.bNumEndpoints % 2){
1362 CANMSG("Endpoint count must be even");
1366 usbcan_chip_count = iface_desc->desc.bNumEndpoints / 2;
1368 usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
1372 memset(usbdevs, 0, sizeof(struct usbcan_devs));
1374 usbdevs->count = usbcan_chip_count;
1375 usbdevs->udev = interface_to_usbdev(interface);
1377 usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
1378 if (!usbdevs->devs) {
1381 memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
1383 for (j=0;j<usbcan_chip_count;j++){
1384 struct usbcan_usb *dev;
1387 /* allocate memory for our device state and initialize it */
1388 usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1389 if (!usbdevs->devs[j]) {
1392 memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
1393 dev=usbdevs->devs[j];
1394 spin_lock_init(&dev->list_lock);
1396 mutex_init(&dev->io_mutex);
1397 init_waitqueue_head(&dev->queue);
1398 dev->udev = usbdevs->udev;
1399 dev->interface = interface;
1401 /* set up the endpoint information */
1402 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1403 endpoint = &iface_desc->endpoint[i].desc;
1408 if ((usbdevs->devs[k]->bulk_in_endpointAddr & USB_ENDPOINT_NUMBER_MASK) == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
1412 epnum=endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1415 if (!dev->bulk_in_endpointAddr &&
1416 usb_endpoint_is_bulk_in(endpoint)) {
1417 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1418 /* we found a bulk in endpoint */
1419 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1420 dev->bulk_in_size = buffer_size;
1421 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1422 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1423 if (!dev->bulk_in_buffer) {
1424 CANMSG("Could not allocate bulk_in_buffer");
1430 if (!dev->bulk_out_endpointAddr &&
1431 usb_endpoint_is_bulk_out(endpoint)) {
1432 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1433 /* we found a bulk out endpoint */
1434 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1439 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1440 CANMSG("Could not find all bulk-in and bulk-out endpoints for chip %d",j);
1445 usb_get_dev(usbdevs->udev);
1447 /* save our data pointer in this interface device */
1448 usb_set_intfdata(interface, usbdevs);
1450 usbdevs->candev=register_hotplug_dev("usbcan", usbcan_register_devs,(void *) usbdevs);
1451 if (!(usbdevs->candev)){
1452 CANMSG("register_hotplug_dev() failed\n");
1453 goto register_error;
1456 /* let the user know what node this device is now attached to */
1457 CANMSG("USBCAN device now attached\n");
1461 // cleanup_hotplug_dev(usbdevs->candev);
1462 usb_put_dev(usbdevs->udev);
1466 for (j=0;j<usbdevs->count;j++){
1467 if (!usbdevs->devs[j]) continue;
1469 if (usbdevs->devs[j]->bulk_in_buffer)
1470 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1471 if (usbdevs->devs[j]->chip){
1472 usbdevs->devs[j]->chip->chip_data=NULL;
1474 can_checked_free(usbdevs->devs[j]);
1476 can_checked_free(usbdevs->devs);
1478 can_checked_free(usbdevs);
1484 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1485 void release_device(struct kref *refcount){
1486 struct candevice_t *candev = container_of(refcount,struct candevice_t,refcount);
1488 void release_device(struct candevice_t *candev){
1490 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
1496 cleanup_hotplug_dev(usbdevs->candev);
1499 /* Finally, release all structures in USB subsystem */
1501 panic("udev is already null on device release");
1502 usb_put_dev(usbdevs->udev);
1504 for (j=0;j<usbdevs->count;j++){
1505 if (!usbdevs->devs[j]) continue;
1507 if (usbdevs->devs[j]->bulk_in_buffer)
1508 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1509 can_checked_free(usbdevs->devs[j]);
1510 usbdevs->devs[j]=NULL;
1512 can_checked_free(usbdevs->devs);
1514 can_checked_free(usbdevs);
1516 CANMSG("USBCAN now disconnected\n");
1519 // Physically disconnected device
1520 static void usbcan_disconnect(struct usb_interface *interface)
1522 struct usbcan_devs *usbdevs;
1525 /* prevent more I/O from starting */
1528 usbdevs = usb_get_intfdata(interface);
1530 CANMSG("USBCAN device seems to be already removed\n");
1534 usb_set_intfdata(interface, NULL);
1535 deregister_hotplug_dev(usbdevs->candev);
1537 for (j=0;j<usbdevs->count;j++){
1538 if (!usbdevs->devs[j]) continue;
1539 mutex_lock(&usbdevs->devs[j]->io_mutex);
1540 usbdevs->devs[j]->interface = NULL;
1541 mutex_unlock(&usbdevs->devs[j]->io_mutex);
1546 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
1547 kref_put(&usbdevs->candev->refcount,release_device);
1549 release_device(&usbdevs->candev);
1553 int usbcan_init(void){
1554 return usb_register(&usbcan_driver);
1557 void usbcan_exit(void){
1558 usb_deregister(&usbcan_driver);