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"
15 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
16 static void usbcan_disconnect(struct usb_interface *interface);
18 volatile int usbcan_chip_count=0;
20 /* table of devices that work with this driver */
21 static struct usb_device_id usbcan_table [] = {
22 { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
23 { } /* Terminating entry */
25 MODULE_DEVICE_TABLE(usb, usbcan_table);
27 static struct usb_driver usbcan_driver = {
29 .id_table = usbcan_table,
30 .probe = usbcan_probe,
31 .disconnect = usbcan_disconnect,
35 * usbcan_request_io: - reserve io or memory range for can board
36 * @candev: pointer to candevice/board which asks for io. Field @io_addr
37 * of @candev is used in most cases to define start of the range
39 * The function usbcan_request_io() is used to reserve the io-memory. If your
40 * hardware uses a dedicated memory range as hardware control registers you
41 * will have to add the code to reserve this memory as well.
42 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
43 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
44 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
45 * Return Value: The function returns zero on success or %-ENODEV on failure
48 int usbcan_request_io(struct candevice_t *candev)
50 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
53 CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
54 CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
62 * usbcan_release_io - free reserved io memory range
63 * @candev: pointer to candevice/board which releases io
65 * The function usbcan_release_io() is used to free reserved io-memory.
66 * In case you have reserved more io memory, don't forget to free it here.
67 * IO_RANGE is the io-memory range that gets released, please adjust according
68 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
69 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
70 * Return Value: The function always returns zero
73 int usbcan_release_io(struct candevice_t *candev)
79 * usbcan_reset - hardware reset routine
80 * @candev: Pointer to candevice/board structure
82 * The function usbcan_reset() is used to give a hardware reset. This is
83 * rather hardware specific so I haven't included example code. Don't forget to
84 * check the reset status of the chip before returning.
85 * Return Value: The function returns zero on success or %-ENODEV on failure
88 int usbcan_reset(struct candevice_t *candev)
94 * usbcan_init_hw_data - Initialize hardware cards
95 * @candev: Pointer to candevice/board structure
97 * The function usbcan_init_hw_data() is used to initialize the hardware
98 * structure containing information about the installed CAN-board.
99 * %RESET_ADDR represents the io-address of the hardware reset register.
100 * %NR_82527 represents the number of Intel 82527 chips on the board.
101 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
102 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
103 * the hardware uses programmable interrupts.
104 * Return Value: The function always returns zero
107 int usbcan_init_hw_data(struct candevice_t *candev)
109 candev->res_addr=RESET_ADDR;
110 candev->nr_82527_chips=0;
111 candev->nr_sja1000_chips=0;
112 candev->nr_all_chips=usbcan_chip_count;
113 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
119 * usbcan_init_obj_data - Initialize message buffers
120 * @chip: Pointer to chip specific structure
121 * @objnr: Number of the message buffer
123 * The function usbcan_init_obj_data() is used to initialize the hardware
124 * structure containing information about the different message objects on the
125 * CAN chip. In case of the sja1000 there's only one message object but on the
126 * i82527 chip there are 15.
127 * The code below is for a i82527 chip and initializes the object base addresses
128 * The entry @obj_base_addr represents the first memory address of the message
129 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
131 * Unless the hardware uses a segmented memory map, flags can be set zero.
132 * Return Value: The function always returns zero
135 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
137 chip->msgobj[objnr]->obj_base_addr=0;
143 * usbcan_program_irq - program interrupts
144 * @candev: Pointer to candevice/board structure
146 * The function usbcan_program_irq() is used for hardware that uses
147 * programmable interrupts. If your hardware doesn't use programmable interrupts
148 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
149 * leave this function unedited. Again this function is hardware specific so
150 * there's no example code.
151 * Return value: The function returns zero on success or %-ENODEV on failure
154 int usbcan_program_irq(struct candevice_t *candev)
159 /* !!! Don't change this function !!! */
160 int usbcan_register(struct hwspecops_t *hwspecops)
162 hwspecops->request_io = usbcan_request_io;
163 hwspecops->release_io = usbcan_release_io;
164 hwspecops->reset = usbcan_reset;
165 hwspecops->init_hw_data = usbcan_init_hw_data;
166 hwspecops->init_chip_data = usbcan_init_chip_data;
167 hwspecops->init_obj_data = usbcan_init_obj_data;
168 hwspecops->write_register = NULL;
169 hwspecops->read_register = NULL;
170 hwspecops->program_irq = usbcan_program_irq;
174 // static int sja1000_report_error_limit_counter;
176 static void sja1000_report_error(struct canchip_t *chip,
177 unsigned sr, unsigned ir, unsigned ecc)
179 /*TODO : Error reporting from device */
181 /* if(sja1000_report_error_limit_counter>=100)
184 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
187 sja1000_report_error_limit_counter+=10;
189 if(sja1000_report_error_limit_counter>=100){
190 sja1000_report_error_limit_counter+=10;
191 CANMSG("Error: too many errors, reporting disabled\n");
195 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
196 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
197 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
198 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
199 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
200 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
201 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
202 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
203 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
204 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
205 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
206 if((sr&sjaIR_EI) || 1){
207 CANMSG("EI: %s %s %s\n",
208 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
209 ecc&sjaECC_DIR?"RX":"TX",
210 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
213 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
218 * usbcan_enable_configuration - enable chip configuration mode
219 * @chip: pointer to chip state structure
221 int usbcan_enable_configuration(struct canchip_t *chip)
227 * usbcan_disable_configuration - disable chip configuration mode
228 * @chip: pointer to chip state structure
230 int usbcan_disable_configuration(struct canchip_t *chip)
236 * usbcan_chip_config: - can chip configuration
237 * @chip: pointer to chip state structure
239 * This function configures chip and prepares it for message
240 * transmission and reception. The function resets chip,
241 * resets mask for acceptance of all messages by call to
242 * usbcan_extended_mask() function and then
243 * computes and sets baudrate with use of function usbcan_baud_rate().
244 * Return Value: negative value reports error.
247 int usbcan_chip_config(struct canchip_t *chip)
253 * usbcan_extended_mask: - setup of extended mask for message filtering
254 * @chip: pointer to chip state structure
255 * @code: can message acceptance code
256 * @mask: can message acceptance mask
258 * Return Value: negative value reports error.
261 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
264 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
271 *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
272 *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
274 retval=usb_control_msg(dev->udev,
275 usb_sndctrlpipe(dev->udev, 0),
276 USBCAN_VENDOR_EXT_MASK_SET,
278 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
284 retval = usb_control_msg(dev->udev,
285 usb_rcvctrlpipe(dev->udev, 0),
286 USBCAN_VENDOR_EXT_MASK_STATUS,
288 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
294 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
295 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
300 CANMSG("Setting extended mask failed\n");
305 * usbcan_baud_rate: - set communication parameters.
306 * @chip: pointer to chip state structure
307 * @rate: baud rate in Hz
308 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
309 * @sjw: synchronization jump width (0-3) prescaled clock cycles
310 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
311 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
313 * Return Value: negative value reports error.
316 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
317 int sampl_pt, int flags)
320 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
327 *(int32_t *)(usbbuf)=cpu_to_le32(rate);
328 *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
329 *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
330 *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
332 retval=usb_control_msg(dev->udev,
333 usb_sndctrlpipe(dev->udev, 0),
334 USBCAN_VENDOR_BAUD_RATE_SET,
336 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
342 retval = usb_control_msg(dev->udev,
343 usb_rcvctrlpipe(dev->udev, 0),
344 USBCAN_VENDOR_BAUD_RATE_STATUS,
346 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
355 CANMSG("baud rate %d is not possible to set\n",
361 * usbcan_pre_read_config: - prepares message object for message reception
362 * @chip: pointer to chip state structure
363 * @obj: pointer to message object state structure
365 * Return Value: negative value reports error.
366 * Positive value indicates immediate reception of message.
369 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
374 #define MAX_TRANSMIT_WAIT_LOOPS 10
376 * usbcan_pre_write_config: - prepares message object for message transmission
377 * @chip: pointer to chip state structure
378 * @obj: pointer to message object state structure
379 * @msg: pointer to CAN message
381 * This function prepares selected message object for future initiation
382 * of message transmission by usbcan_send_msg() function.
383 * The CAN message data and message ID are transfered from @msg slot
384 * into chip buffer in this function.
385 * Return Value: negative value reports error.
388 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
389 struct canmsg_t *msg)
391 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
398 /* Wait until Transmit Buffer Status is released */
399 while ( usbcan_chip_queue_status(chip) &&
400 i++<MAX_TRANSMIT_WAIT_LOOPS) {
403 if (usbcan_chip_queue_status(chip)){
404 CANMSG("Buffer full, cannot send message\n");
408 *(uint8_t *)(dev->tx_msg)=chip->chip_idx & 0xFF;
411 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
413 *(uint8_t *)(dev->tx_msg+1)=len & 0xFF;
414 *(uint16_t *)(dev->tx_msg+2)=cpu_to_le16(msg->flags);
415 *(uint32_t *)(dev->tx_msg+4)=cpu_to_le32(msg->id);
417 for(ptr=dev->tx_msg+8,i=0; i < len; ptr++,i++) {
418 *ptr=msg->data[i] & 0xFF;
420 for(; i < 8; ptr++,i++) {
427 * usbcan_send_msg: - initiate message transmission
428 * @chip: pointer to chip state structure
429 * @obj: pointer to message object state structure
430 * @msg: pointer to CAN message
432 * This function is called after usbcan_pre_write_config() function,
433 * which prepares data in chip buffer.
434 * Return Value: negative value reports error.
437 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
438 struct canmsg_t *msg)
440 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
446 set_bit(USBCAN_TX_PENDING,&dev->flags);
447 retval=usb_bulk_msg(dev->udev,
448 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
451 clear_bit(USBCAN_TX_PENDING,&dev->flags);
453 CANMSG("URB error %d\n",retval);
457 CANMSG("CAN message not sent\n");
460 CANMSG("Message sent\n");
463 // Do local transmitted message distribution if enabled
465 // fill CAN message timestamp
466 can_filltimestamp(&obj->tx_slot->msg.timestamp);
468 obj->tx_slot->msg.flags |= MSG_LOCAL;
469 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
471 // Free transmitted slot
472 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
476 can_msgobj_clear_fl(obj,TX_PENDING);
481 * usbcan_check_tx_stat: - checks state of transmission engine
482 * @chip: pointer to chip state structure
484 * Return Value: negative value reports error.
485 * Positive return value indicates transmission under way status.
486 * Zero value indicates finishing of all issued transmission requests.
489 int usbcan_check_tx_stat(struct canchip_t *chip)
491 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
494 if (test_bit(USBCAN_TX_PENDING,&dev->flags))
500 * usbcan_set_btregs: - configures bitrate registers
501 * @chip: pointer to chip state structure
502 * @btr0: bitrate register 0
503 * @btr1: bitrate register 1
505 * Return Value: negative value reports error.
508 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
513 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
514 uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
519 retval = usb_control_msg(dev->udev,
520 usb_rcvctrlpipe(dev->udev, 0),
521 USBCAN_VENDOR_SET_BTREGS,
523 cpu_to_le16(value), cpu_to_le16(chip->chip_idx),
535 * usbcan_start_chip: - starts chip message processing
536 * @chip: pointer to chip state structure
538 * Return Value: negative value reports error.
541 int usbcan_start_chip(struct canchip_t *chip)
545 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
550 retval = usb_control_msg(dev->udev,
551 usb_rcvctrlpipe(dev->udev, 0),
552 USBCAN_VENDOR_START_CHIP,
554 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
566 * usbcan_chip_queue_status: - gets queue status from usb device
567 * @chip: pointer to chip state structure
569 * Return Value: negative value reports error.
570 * 0 means queue is not full
571 * 1 means queue is full
574 int usbcan_chip_queue_status(struct canchip_t *chip)
578 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
582 retval = usb_control_msg(dev->udev,
583 usb_rcvctrlpipe(dev->udev, 0),
584 USBCAN_VENDOR_CHECK_TX_STAT,
586 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
591 CANMSG("Chip_queue_status: %d\n",buf[0]);
597 CANMSG("Chip_queue_status error: %d\n",retval);
602 * usbcan_stop_chip: - stops chip message processing
603 * @chip: pointer to chip state structure
605 * Return Value: negative value reports error.
608 int usbcan_stop_chip(struct canchip_t *chip)
612 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
617 retval = usb_control_msg(dev->udev,
618 usb_rcvctrlpipe(dev->udev, 0),
619 USBCAN_VENDOR_STOP_CHIP,
621 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
633 * usbcan_register_devs: - attaches usb device data to the chip structure
634 * @chip: pointer to chip state structure
635 * @data: usb device data
639 void usbcan_register_devs(struct canchip_t *chip,void *data){
640 struct usbcan_devs *usbdevs=(struct usbcan_devs *)data;
642 CANMSG("Bad structure given\n");
645 if (chip->chip_idx>=usbdevs->count) {
646 CANMSG("Requested chip number is bigger than chip count\n");
650 usbdevs->devs[chip->chip_idx]->chip=chip;
651 chip->chip_data=(void *)usbdevs->devs[chip->chip_idx];
655 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
656 * @chip: pointer to chip state structure
658 * Return Value: negative value reports error.
661 int usbcan_attach_to_chip(struct canchip_t *chip)
663 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
665 /* start kernel thread */
666 dev->comthread=can_kthread_run(usbcan_read_kthread, (void *)dev, "usbcan_%d",chip->chip_idx);
672 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
673 * @chip: pointer to chip state structure
675 * Return Value: negative value reports error.
678 int usbcan_release_chip(struct canchip_t *chip)
680 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
682 usbcan_stop_chip(chip);
684 /* terminate the kernel thread */
685 can_kthread_stop(dev->comthread);
691 * usbcan_remote_request: - configures message object and asks for RTR message
692 * @chip: pointer to chip state structure
693 * @obj: pointer to message object structure
695 * Return Value: negative value reports error.
698 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
700 CANMSG("usbcan_remote_request not implemented\n");
705 * usbcan_standard_mask: - setup of mask for message filtering
706 * @chip: pointer to chip state structure
707 * @code: can message acceptance code
708 * @mask: can message acceptance mask
710 * Return Value: negative value reports error.
713 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
716 CANMSG("usbcan_standard_mask not implemented\n");
721 * usbcan_clear_objects: - clears state of all message object residing in chip
722 * @chip: pointer to chip state structure
724 * Return Value: negative value reports error.
727 int usbcan_clear_objects(struct canchip_t *chip)
729 CANMSG("usbcan_clear_objects not implemented\n");
734 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
735 * @chip: pointer to chip state structure
736 * @irqs: requested chip IRQ configuration
738 * Return Value: negative value reports error.
741 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
743 CANMSG("usbcan_config_irqs not implemented\n");
748 * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
749 * @chip: pointer to chip state structure
750 * @obj: pointer to attached queue description
752 * The main purpose of this function is to read message from attached queues
753 * and transfer message contents into CAN controller chip.
754 * This subroutine is called by
755 * usbcan_irq_write_handler() for transmit events.
758 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
763 // Do local transmitted message distribution if enabled
765 // fill CAN message timestamp
766 can_filltimestamp(&obj->tx_slot->msg.timestamp);
768 obj->tx_slot->msg.flags |= MSG_LOCAL;
769 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
771 // Free transmitted slot
772 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
776 can_msgobj_clear_fl(obj,TX_PENDING);
777 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
780 can_msgobj_set_fl(obj,TX_PENDING);
782 if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
784 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
785 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
789 if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
791 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
792 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
801 * usbcan_irq_handler: - interrupt service routine
802 * @irq: interrupt vector number, this value is system specific
803 * @chip: pointer to chip state structure
805 * Interrupt handler is activated when state of CAN controller chip changes,
806 * there is message to be read or there is more space for new messages or
807 * error occurs. The receive events results in reading of the message from
808 * CAN controller chip and distribution of message through attached
812 int usbcan_irq_handler(int irq, struct canchip_t *chip)
814 /* int irq_register, status, error_code;
815 struct msgobj_t *obj=chip->msgobj[0];
816 int loop_cnt=CHIP_MAX_IRQLOOP;
818 irq_register=can_read_reg(chip,SJAIR);
819 // DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
820 // DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
821 // can_read_reg(chip,SJASR));
823 if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
824 return CANCHIP_IRQ_NONE;
826 if(!(chip->flags&CHIP_CONFIGURED)) {
827 CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
828 return CANCHIP_IRQ_NONE;
831 status=can_read_reg(chip,SJASR);
836 CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
837 return CANCHIP_IRQ_STUCK;
840 // (irq_register & sjaIR_TI)
841 // old variant using SJAIR, collides with intended use with irq_accept
842 if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
843 (can_msgobj_test_fl(obj,TX_REQUEST))) {
844 DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
846 can_msgobj_set_fl(obj,TX_REQUEST);
847 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
848 can_msgobj_clear_fl(obj,TX_REQUEST);
850 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
851 usbcan_irq_write_handler(chip, obj);
853 can_msgobj_clear_fl(obj,TX_LOCK);
854 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
855 DEBUGMSG("TX looping in sja1000_irq_handler\n");
858 if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
859 // Some error happened
860 error_code=can_read_reg(chip,SJAECC);
861 sja1000_report_error(chip, status, irq_register, error_code);
862 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
863 // Reset flag set to 0 if chip is already off the bus. Full state report
866 if(error_code == 0xd9) {
868 // no such device or address - no ACK received
870 if(obj->tx_retry_cnt++>MAX_RETR) {
871 can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
872 obj->tx_retry_cnt = 0;
874 if(status&sjaSR_BS) {
875 CANMSG("bus-off, resetting usbcan\n");
876 can_write_reg(chip, 0, SJAMOD);
880 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
881 //canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
886 if(sja1000_report_error_limit_counter)
887 sja1000_report_error_limit_counter--;
891 irq_register=can_read_reg(chip,SJAIR);
893 status=can_read_reg(chip,SJASR);
895 if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
896 (irq_register & sjaIR_TI))
897 can_msgobj_set_fl(obj,TX_REQUEST);
899 } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
900 (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
901 (status & sjaSR_RBS));
903 return CANCHIP_IRQ_HANDLED;
907 * usbcan_wakeup_tx: - wakeups TX processing
908 * @chip: pointer to chip state structure
909 * @obj: pointer to message object structure
911 * Function is responsible for initiating message transmition.
912 * It is responsible for clearing of object TX_REQUEST flag
914 * Return Value: negative value reports error.
917 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
919 CANMSG("Trying to send message\n");
920 can_preempt_disable();
922 can_msgobj_set_fl(obj,TX_PENDING);
923 can_msgobj_set_fl(obj,TX_REQUEST);
924 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
925 can_msgobj_clear_fl(obj,TX_REQUEST);
927 if (!usbcan_chip_queue_status(chip)){
929 usbcan_irq_write_handler(chip, obj);
932 can_msgobj_clear_fl(obj,TX_LOCK);
933 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
934 DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
937 can_preempt_enable();
941 int usbcan_chipregister(struct chipspecops_t *chipspecops)
943 CANMSG("initializing usbcan chip operations\n");
944 chipspecops->chip_config=usbcan_chip_config;
945 chipspecops->baud_rate=usbcan_baud_rate;
946 chipspecops->standard_mask=usbcan_standard_mask;
947 chipspecops->extended_mask=usbcan_extended_mask;
948 chipspecops->message15_mask=usbcan_extended_mask;
949 chipspecops->clear_objects=usbcan_clear_objects;
950 chipspecops->config_irqs=usbcan_config_irqs;
951 chipspecops->pre_read_config=usbcan_pre_read_config;
952 chipspecops->pre_write_config=usbcan_pre_write_config;
953 chipspecops->send_msg=usbcan_send_msg;
954 chipspecops->check_tx_stat=usbcan_check_tx_stat;
955 chipspecops->wakeup_tx=usbcan_wakeup_tx;
956 chipspecops->remote_request=usbcan_remote_request;
957 chipspecops->enable_configuration=usbcan_enable_configuration;
958 chipspecops->disable_configuration=usbcan_disable_configuration;
959 chipspecops->attach_to_chip=usbcan_attach_to_chip;
960 chipspecops->release_chip=usbcan_release_chip;
961 chipspecops->set_btregs=usbcan_set_btregs;
962 chipspecops->start_chip=usbcan_start_chip;
963 chipspecops->stop_chip=usbcan_stop_chip;
964 chipspecops->irq_handler=usbcan_irq_handler;
965 chipspecops->irq_accept=NULL;
970 * usbcan_fill_chipspecops - fills chip specific operations
971 * @chip: pointer to chip representation structure
973 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
975 * Return Value: returns negative number in the case of fail
977 int usbcan_fill_chipspecops(struct canchip_t *chip)
979 chip->chip_type="usbcan";
981 usbcan_chipregister(chip->chipspecops);
986 * usbcan_init_chip_data - Initialize chips
987 * @candev: Pointer to candevice/board structure
988 * @chipnr: Number of the CAN chip on the hardware card
990 * The function usbcan_init_chip_data() is used to initialize the hardware
991 * structure containing information about the CAN chips.
992 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
994 * The @chip_base_addr entry represents the start of the 'official' memory map
995 * of the installed chip. It's likely that this is the same as the @io_addr
996 * argument supplied at module loading time.
997 * The @clock entry holds the chip clock value in Hz.
998 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
999 * register. Options defined in the %sja1000.h file:
1000 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
1001 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
1002 * register. Options defined in the %sja1000.h file:
1003 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
1004 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
1005 * The entry @int_clk_reg holds hardware specific options for the Clock Out
1006 * register. Options defined in the %i82527.h file:
1007 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
1008 * The entry @int_bus_reg holds hardware specific options for the Bus
1009 * Configuration register. Options defined in the %i82527.h file:
1010 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
1011 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
1012 * register. Options defined in the %i82527.h file:
1013 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
1014 * Return Value: The function always returns zero
1015 * File: src/usbcan.c
1017 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
1019 struct canchip_t *chip=candev->chip[chipnr];
1021 usbcan_fill_chipspecops(chip);
1023 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
1024 candev->chip[chipnr]->chip_base_addr=0;
1025 candev->chip[chipnr]->clock = 0;
1032 /* --------------------------------------------------------------------------------------------------- */
1033 static int usbcan_sleep_thread(struct usbcan_usb *dev)
1037 /* Wait until a signal arrives or we are woken up */
1040 set_current_state(TASK_INTERRUPTIBLE);
1041 if (signal_pending(current)) {
1046 can_kthread_should_stop() ||
1047 test_bit(USBCAN_DATA_READ,&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_rcv(struct urb *urb)
1060 struct usbcan_usb *dev = urb->context;
1063 if (!test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
1066 switch (urb->status) {
1069 set_bit(USBCAN_DATA_READ,&dev->flags);
1070 CANMSG("Message received\n");
1071 if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
1072 wake_up_process(dev->comthread);
1077 /* this urb is terminated, clean up */
1078 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1079 set_bit(USBCAN_TERMINATE,&dev->flags);
1082 // CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1086 retval = usb_submit_urb (urb, GFP_ATOMIC);
1088 CANMSG("%s - usb_submit_urb failed with result %d ... retrying\n",
1089 __FUNCTION__, retval);
1090 set_bit(USBCAN_ERROR,&dev->flags);
1091 if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
1092 wake_up_process(dev->comthread);
1096 int usbcan_read_kthread(void *data)
1099 struct usbcan_usb *dev=(struct usbcan_usb *)data;
1100 struct msgobj_t *obj;
1102 /* this is normal work to do */
1103 CANMSG ("usbcan thread started!\n");
1105 dev->rx = usb_alloc_urb(0, GFP_KERNEL);
1107 CANMSG("Error allocating usb urb\n");
1110 dev->rx->dev = dev->udev;
1111 usb_fill_bulk_urb(dev->rx, dev->udev,
1112 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1116 set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1117 /* an endless loop in which we are doing our work */
1120 retval=usb_submit_urb(dev->rx, GFP_ATOMIC);
1122 CANMSG("URB error %d\n",retval);
1123 set_bit(USBCAN_ERROR,&dev->flags);
1126 /* We need to do a memory barrier here to be sure that
1127 the flags are visible on all CPUs. */
1130 usbcan_sleep_thread(dev);
1131 /* We need to do a memory barrier here to be sure that
1132 the flags are visible on all CPUs. */
1135 /* here we are back from sleep because we caught a signal. */
1136 if (can_kthread_should_stop())
1138 /* we received a request to terminate ourself */
1142 { /* Normal work to do */
1143 if (test_bit(USBCAN_DATA_READ,&dev->flags)){
1145 clear_bit(USBCAN_DATA_READ,&dev->flags);
1146 CANMSG("Thread got received message\n");
1149 (dev->chip->flags & CHIP_CONFIGURED)
1153 obj=dev->chip->msgobj[0];
1155 len=*(uint8_t *)(dev->rx_msg+1);
1156 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
1157 obj->rx_msg.length = len;
1159 obj->rx_msg.flags=le16_to_cpu(*(uint16_t *)(dev->rx_msg+2));
1160 obj->rx_msg.id=le32_to_cpu((*(uint32_t *)(dev->rx_msg+4)));
1162 for(ptr=dev->rx_msg+8,i=0; i < len; ptr++,i++) {
1163 obj->rx_msg.data[i]=*ptr;
1166 // fill CAN message timestamp
1167 can_filltimestamp(&obj->rx_msg.timestamp);
1168 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
1171 CANMSG("Destination chip not found\n");
1175 clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1177 /* here we go only in case of termination of the thread */
1179 usb_kill_urb(dev->rx);
1180 usb_free_urb(dev->rx);
1183 CANMSG ("usbcan thread finished!\n");
1186 /* cleanup the thread, leave */
1187 CANMSG ("kernel thread terminated!\n");
1191 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1193 struct usbcan_devs *usbdevs=NULL;
1194 struct usb_host_interface *iface_desc;
1195 struct usb_endpoint_descriptor *endpoint;
1198 int retval = -ENOMEM;
1200 iface_desc = interface->cur_altsetting;
1201 if (iface_desc->desc.bNumEndpoints % 2){
1202 err("Endpoint count must be even");
1206 usbcan_chip_count = iface_desc->desc.bNumEndpoints / 2;
1208 usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
1210 err("Out of memory");
1213 memset(usbdevs, 0, sizeof(struct usbcan_devs));
1215 usbdevs->count=usbcan_chip_count;
1217 usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
1218 if (!usbdevs->devs) {
1219 err("Out of memory");
1222 memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
1224 for (j=0;j<usbcan_chip_count;j++){
1225 struct usbcan_usb *dev;
1228 /* allocate memory for our device state and initialize it */
1229 usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1230 if (!usbdevs->devs[j]) {
1231 err("Out of memory");
1234 memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
1235 dev=usbdevs->devs[j];
1237 mutex_init(&dev->io_mutex);
1238 dev->udev = interface_to_usbdev(interface);
1239 dev->interface = interface;
1241 /* set up the endpoint information */
1242 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1243 endpoint = &iface_desc->endpoint[i].desc;
1248 if ((usbdevs->devs[k]->bulk_in_endpointAddr & USB_ENDPOINT_NUMBER_MASK) == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
1252 epnum=endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1255 if (!dev->bulk_in_endpointAddr &&
1256 usb_endpoint_is_bulk_in(endpoint)) {
1257 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1258 /* we found a bulk in endpoint */
1259 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1260 dev->bulk_in_size = buffer_size;
1261 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1262 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1263 if (!dev->bulk_in_buffer) {
1264 err("Could not allocate bulk_in_buffer");
1270 if (!dev->bulk_out_endpointAddr &&
1271 usb_endpoint_is_bulk_out(endpoint)) {
1272 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1273 /* we found a bulk out endpoint */
1274 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1279 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1280 err("Could not find all bulk-in and bulk-out endpoints for chip %d",j);
1284 /* save our data pointer in this interface device */
1285 usb_set_intfdata(interface, usbdevs);
1287 if (!(usbdevs->candev=register_usbdev("usbcan",(void *) usbdevs, usbcan_register_devs)))
1288 goto register_error;
1290 /* let the user know what node this device is now attached to */
1291 DEBUGMSG("USBCAN device now attached");
1295 cleanup_usbdev(usbdevs->candev);
1299 if (usbdevs->devs[0]){
1300 usb_put_dev(usbdevs->devs[0]->udev);
1302 for (j=0;j<usbdevs->count;j++){
1303 if (!usbdevs->devs[j]) continue;
1305 if (usbdevs->devs[j]->bulk_in_buffer)
1306 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1307 if (usbdevs->devs[j]->chip){
1308 usbdevs->devs[j]->chip->chip_data=NULL;
1310 can_checked_free(usbdevs->devs[j]);
1312 can_checked_free(usbdevs->devs);
1314 can_checked_free(usbdevs);
1320 // Physically disconnected device
1321 static void usbcan_disconnect(struct usb_interface *interface)
1323 struct usbcan_devs *usbdevs;
1325 usbdevs = usb_get_intfdata(interface);
1327 CANMSG("USBCAN device seems to be removed\n");
1330 usb_set_intfdata(interface, NULL);
1333 usb_put_dev((*usbdevs->devs)->udev);
1335 cleanup_usbdev(usbdevs->candev);
1337 for (j=0;j<usbdevs->count;j++){
1338 if (!usbdevs->devs[j]) continue;
1340 /* prevent more I/O from starting */
1341 mutex_lock(&usbdevs->devs[j]->io_mutex);
1342 usbdevs->devs[j]->interface = NULL;
1343 mutex_unlock(&usbdevs->devs[j]->io_mutex);
1345 if (usbdevs->devs[j]->bulk_in_buffer)
1346 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1347 // if (usbdevs->devs[j]->chip){
1348 // usbdevs->devs[j]->chip->chip_data=NULL;
1350 can_checked_free(usbdevs->devs[j]);
1351 usbdevs->devs[j]=NULL;
1353 can_checked_free(usbdevs->devs);
1355 can_checked_free(usbdevs);
1357 DEBUGMSG("USBCAN now disconnected");
1360 int usbcan_init(void){
1361 return usb_register(&usbcan_driver);
1364 void usbcan_exit(void){
1365 usb_deregister(&usbcan_driver);