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 /* table of devices that work with this driver */
19 static struct usb_device_id usbcan_table [] = {
20 { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
21 { } /* Terminating entry */
23 MODULE_DEVICE_TABLE(usb, usbcan_table);
25 static struct usb_driver usbcan_driver = {
27 .id_table = usbcan_table,
28 .probe = usbcan_probe,
29 .disconnect = usbcan_disconnect,
33 * usbcan_request_io: - reserve io or memory range for can board
34 * @candev: pointer to candevice/board which asks for io. Field @io_addr
35 * of @candev is used in most cases to define start of the range
37 * The function usbcan_request_io() is used to reserve the io-memory. If your
38 * hardware uses a dedicated memory range as hardware control registers you
39 * will have to add the code to reserve this memory as well.
40 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
41 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
42 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
43 * Return Value: The function returns zero on success or %-ENODEV on failure
46 int usbcan_request_io(struct candevice_t *candev)
48 struct usbcan_usb *dev = (struct usbcan_usb*)candev->sysdevptr.anydev;
51 CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
52 CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
56 /* start kernel thread */
57 dev->comthread=can_kthread_run(usbcan_read_kthread, (void *)dev, "usbcan");
59 /* Adding link to can device into usbcan_usb struct */
60 ((struct usbcan_usb*)candev->sysdevptr.anydev)->candev=candev;
65 * usbcan_release_io - free reserved io memory range
66 * @candev: pointer to candevice/board which releases io
68 * The function usbcan_release_io() is used to free reserved io-memory.
69 * In case you have reserved more io memory, don't forget to free it here.
70 * IO_RANGE is the io-memory range that gets released, please adjust according
71 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
72 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
73 * Return Value: The function always returns zero
76 int usbcan_release_io(struct candevice_t *candev)
78 struct usbcan_usb *dev = ((struct usbcan_usb*)candev->sysdevptr.anydev);
82 /* terminate the kernel thread */
83 can_kthread_stop(dev->comthread);
89 * usbcan_reset - hardware reset routine
90 * @candev: Pointer to candevice/board structure
92 * The function usbcan_reset() is used to give a hardware reset. This is
93 * rather hardware specific so I haven't included example code. Don't forget to
94 * check the reset status of the chip before returning.
95 * Return Value: The function returns zero on success or %-ENODEV on failure
98 int usbcan_reset(struct candevice_t *candev)
104 * usbcan_init_hw_data - Initialize hardware cards
105 * @candev: Pointer to candevice/board structure
107 * The function usbcan_init_hw_data() is used to initialize the hardware
108 * structure containing information about the installed CAN-board.
109 * %RESET_ADDR represents the io-address of the hardware reset register.
110 * %NR_82527 represents the number of Intel 82527 chips on the board.
111 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
112 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
113 * the hardware uses programmable interrupts.
114 * Return Value: The function always returns zero
117 int usbcan_init_hw_data(struct candevice_t *candev)
119 candev->res_addr=RESET_ADDR;
120 candev->nr_82527_chips=0;
121 candev->nr_sja1000_chips=0;
122 candev->nr_all_chips=1;
123 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
129 * usbcan_init_obj_data - Initialize message buffers
130 * @chip: Pointer to chip specific structure
131 * @objnr: Number of the message buffer
133 * The function usbcan_init_obj_data() is used to initialize the hardware
134 * structure containing information about the different message objects on the
135 * CAN chip. In case of the sja1000 there's only one message object but on the
136 * i82527 chip there are 15.
137 * The code below is for a i82527 chip and initializes the object base addresses
138 * The entry @obj_base_addr represents the first memory address of the message
139 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
141 * Unless the hardware uses a segmented memory map, flags can be set zero.
142 * Return Value: The function always returns zero
145 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
147 chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
153 * usbcan_program_irq - program interrupts
154 * @candev: Pointer to candevice/board structure
156 * The function usbcan_program_irq() is used for hardware that uses
157 * programmable interrupts. If your hardware doesn't use programmable interrupts
158 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
159 * leave this function unedited. Again this function is hardware specific so
160 * there's no example code.
161 * Return value: The function returns zero on success or %-ENODEV on failure
164 int usbcan_program_irq(struct candevice_t *candev)
169 /* !!! Don't change this function !!! */
170 int usbcan_register(struct hwspecops_t *hwspecops)
172 hwspecops->request_io = usbcan_request_io;
173 hwspecops->release_io = usbcan_release_io;
174 hwspecops->reset = usbcan_reset;
175 hwspecops->init_hw_data = usbcan_init_hw_data;
176 hwspecops->init_chip_data = usbcan_init_chip_data;
177 hwspecops->init_obj_data = usbcan_init_obj_data;
178 hwspecops->write_register = NULL;
179 hwspecops->read_register = NULL;
180 hwspecops->program_irq = usbcan_program_irq;
184 // static int sja1000_report_error_limit_counter;
186 static void sja1000_report_error(struct canchip_t *chip,
187 unsigned sr, unsigned ir, unsigned ecc)
189 /*TODO : Error reporting from device */
191 /* if(sja1000_report_error_limit_counter>=100)
194 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
197 sja1000_report_error_limit_counter+=10;
199 if(sja1000_report_error_limit_counter>=100){
200 sja1000_report_error_limit_counter+=10;
201 CANMSG("Error: too many errors, reporting disabled\n");
205 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
206 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
207 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
208 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
209 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
210 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
211 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
212 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
213 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
214 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
215 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
216 if((sr&sjaIR_EI) || 1){
217 CANMSG("EI: %s %s %s\n",
218 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
219 ecc&sjaECC_DIR?"RX":"TX",
220 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
223 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
228 * usbcan_enable_configuration - enable chip configuration mode
229 * @chip: pointer to chip state structure
231 int usbcan_enable_configuration(struct canchip_t *chip)
237 * usbcan_disable_configuration - disable chip configuration mode
238 * @chip: pointer to chip state structure
240 int usbcan_disable_configuration(struct canchip_t *chip)
246 * usbcan_chip_config: - can chip configuration
247 * @chip: pointer to chip state structure
249 * This function configures chip and prepares it for message
250 * transmission and reception. The function resets chip,
251 * resets mask for acceptance of all messages by call to
252 * usbcan_extended_mask() function and then
253 * computes and sets baudrate with use of function usbcan_baud_rate().
254 * Return Value: negative value reports error.
257 int usbcan_chip_config(struct canchip_t *chip)
263 * usbcan_extended_mask: - setup of extended mask for message filtering
264 * @chip: pointer to chip state structure
265 * @code: can message acceptance code
266 * @mask: can message acceptance mask
268 * Return Value: negative value reports error.
271 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
274 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
281 *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
282 *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
284 retval=usb_control_msg(dev->udev,
285 usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
286 USBCAN_VENDOR_EXT_MASK_SET,
288 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
294 retval = usb_control_msg(dev->udev,
295 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
296 USBCAN_VENDOR_EXT_MASK_STATUS,
298 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
304 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
305 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
310 CANMSG("Setting extended mask failed\n");
315 * usbcan_baud_rate: - set communication parameters.
316 * @chip: pointer to chip state structure
317 * @rate: baud rate in Hz
318 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
319 * @sjw: synchronization jump width (0-3) prescaled clock cycles
320 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
321 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
323 * Return Value: negative value reports error.
326 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
327 int sampl_pt, int flags)
330 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
337 *(int32_t *)(usbbuf)=cpu_to_le32(rate);
338 *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
339 *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
340 *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
342 retval=usb_control_msg(dev->udev,
343 usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
344 USBCAN_VENDOR_BAUD_RATE_SET,
346 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
352 retval = usb_control_msg(dev->udev,
353 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
354 USBCAN_VENDOR_BAUD_RATE_STATUS,
356 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
365 CANMSG("baud rate %d is not possible to set\n",
371 * usbcan_pre_read_config: - prepares message object for message reception
372 * @chip: pointer to chip state structure
373 * @obj: pointer to message object state structure
375 * Return Value: negative value reports error.
376 * Positive value indicates immediate reception of message.
379 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
384 #define MAX_TRANSMIT_WAIT_LOOPS 10
386 * usbcan_pre_write_config: - prepares message object for message transmission
387 * @chip: pointer to chip state structure
388 * @obj: pointer to message object state structure
389 * @msg: pointer to CAN message
391 * This function prepares selected message object for future initiation
392 * of message transmission by usbcan_send_msg() function.
393 * The CAN message data and message ID are transfered from @msg slot
394 * into chip buffer in this function.
395 * Return Value: negative value reports error.
398 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
399 struct canmsg_t *msg)
401 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
408 /* Wait until Transmit Buffer Status is released */
409 while ( usbcan_chip_queue_status(chip) &&
410 i++<MAX_TRANSMIT_WAIT_LOOPS) {
413 if (usbcan_chip_queue_status(chip)){
414 CANMSG("Buffer full, cannot send message\n");
418 *(uint8_t *)(dev->tx_msg)=chip->chip_idx & 0xFF;
421 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
423 *(uint8_t *)(dev->tx_msg+1)=len & 0xFF;
424 *(uint16_t *)(dev->tx_msg+2)=cpu_to_le16(msg->flags);
425 *(uint32_t *)(dev->tx_msg+4)=cpu_to_le32(msg->id);
427 for(ptr=dev->tx_msg+8,i=0; i < len; ptr++,i++) {
428 *ptr=msg->data[i] & 0xFF;
430 for(; i < 8; ptr++,i++) {
437 * usbcan_send_msg: - initiate message transmission
438 * @chip: pointer to chip state structure
439 * @obj: pointer to message object state structure
440 * @msg: pointer to CAN message
442 * This function is called after usbcan_pre_write_config() function,
443 * which prepares data in chip buffer.
444 * Return Value: negative value reports error.
447 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
448 struct canmsg_t *msg)
450 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
456 set_bit(USBCAN_TX_PENDING,&dev->flags);
457 retval=usb_bulk_msg(dev->udev,
458 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
461 clear_bit(USBCAN_TX_PENDING,&dev->flags);
463 CANMSG("URB error %d\n",retval);
467 CANMSG("CAN message not sent\n");
470 CANMSG("Message sent\n");
473 // Do local transmitted message distribution if enabled
475 // fill CAN message timestamp
476 can_filltimestamp(&obj->tx_slot->msg.timestamp);
478 obj->tx_slot->msg.flags |= MSG_LOCAL;
479 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
481 // Free transmitted slot
482 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
486 can_msgobj_clear_fl(obj,TX_PENDING);
491 * usbcan_check_tx_stat: - checks state of transmission engine
492 * @chip: pointer to chip state structure
494 * Return Value: negative value reports error.
495 * Positive return value indicates transmission under way status.
496 * Zero value indicates finishing of all issued transmission requests.
499 int usbcan_check_tx_stat(struct canchip_t *chip)
501 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
504 if (test_bit(USBCAN_TX_PENDING,&dev->flags))
510 * usbcan_set_btregs: - configures bitrate registers
511 * @chip: pointer to chip state structure
512 * @btr0: bitrate register 0
513 * @btr1: bitrate register 1
515 * Return Value: negative value reports error.
518 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
522 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
523 uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
528 retval = usb_control_msg(dev->udev,
529 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
530 USBCAN_VENDOR_SET_BTREGS,
532 cpu_to_le16(value), cpu_to_le16(chip->chip_idx),
533 dev->ctl_in_buffer, dev->ctl_in_size,
537 if(dev->ctl_in_buffer[0]==1)
544 * usbcan_start_chip: - starts chip message processing
545 * @chip: pointer to chip state structure
547 * Return Value: negative value reports error.
550 int usbcan_start_chip(struct canchip_t *chip)
553 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
558 retval = usb_control_msg(dev->udev,
559 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
560 USBCAN_VENDOR_START_CHIP,
562 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
563 dev->ctl_in_buffer, dev->ctl_in_size,
567 if(dev->ctl_in_buffer[0]==1)
574 * usbcan_chip_queue_status: - gets queue status from usb device
575 * @chip: pointer to chip state structure
577 * Return Value: negative value reports error.
578 * 0 means queue is not full
579 * 1 means queue is full
582 int usbcan_chip_queue_status(struct canchip_t *chip)
585 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
589 retval = usb_control_msg(dev->udev,
590 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
591 USBCAN_VENDOR_CHECK_TX_STAT,
593 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
594 dev->ctl_in_buffer, dev->ctl_in_size,
597 for (i=0;i<dev->ctl_in_size;i++)
598 CANMSG("Buffer content: %d\n",dev->ctl_in_buffer[i]);
600 CANMSG("Chip_queue_status: %d\n",dev->ctl_in_buffer[0]);
601 if(dev->ctl_in_buffer[0]==1)
603 if(dev->ctl_in_buffer[0]==0)
606 CANMSG("Chip_queue_status error: %d\n",retval);
611 * usbcan_stop_chip: - stops chip message processing
612 * @chip: pointer to chip state structure
614 * Return Value: negative value reports error.
617 int usbcan_stop_chip(struct canchip_t *chip)
620 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
625 retval = usb_control_msg(dev->udev,
626 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
627 USBCAN_VENDOR_STOP_CHIP,
629 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
630 dev->ctl_in_buffer, dev->ctl_in_size,
634 if(dev->ctl_in_buffer[0]==1)
641 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
642 * @chip: pointer to chip state structure
644 * Return Value: negative value reports error.
647 int usbcan_attach_to_chip(struct canchip_t *chip)
653 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
654 * @chip: pointer to chip state structure
656 * Return Value: negative value reports error.
659 int usbcan_release_chip(struct canchip_t *chip)
661 usbcan_stop_chip(chip);
666 * usbcan_remote_request: - configures message object and asks for RTR message
667 * @chip: pointer to chip state structure
668 * @obj: pointer to message object structure
670 * Return Value: negative value reports error.
673 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
675 CANMSG("usbcan_remote_request not implemented\n");
680 * usbcan_standard_mask: - setup of mask for message filtering
681 * @chip: pointer to chip state structure
682 * @code: can message acceptance code
683 * @mask: can message acceptance mask
685 * Return Value: negative value reports error.
688 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
691 CANMSG("usbcan_standard_mask not implemented\n");
696 * usbcan_clear_objects: - clears state of all message object residing in chip
697 * @chip: pointer to chip state structure
699 * Return Value: negative value reports error.
702 int usbcan_clear_objects(struct canchip_t *chip)
704 CANMSG("usbcan_clear_objects not implemented\n");
709 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
710 * @chip: pointer to chip state structure
711 * @irqs: requested chip IRQ configuration
713 * Return Value: negative value reports error.
716 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
718 CANMSG("usbcan_config_irqs not implemented\n");
723 * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
724 * @chip: pointer to chip state structure
725 * @obj: pointer to attached queue description
727 * The main purpose of this function is to read message from attached queues
728 * and transfer message contents into CAN controller chip.
729 * This subroutine is called by
730 * usbcan_irq_write_handler() for transmit events.
733 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
738 // Do local transmitted message distribution if enabled
740 // fill CAN message timestamp
741 can_filltimestamp(&obj->tx_slot->msg.timestamp);
743 obj->tx_slot->msg.flags |= MSG_LOCAL;
744 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
746 // Free transmitted slot
747 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
751 can_msgobj_clear_fl(obj,TX_PENDING);
752 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
755 can_msgobj_set_fl(obj,TX_PENDING);
757 if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
759 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
760 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
764 if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
766 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
767 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
776 * usbcan_irq_handler: - interrupt service routine
777 * @irq: interrupt vector number, this value is system specific
778 * @chip: pointer to chip state structure
780 * Interrupt handler is activated when state of CAN controller chip changes,
781 * there is message to be read or there is more space for new messages or
782 * error occurs. The receive events results in reading of the message from
783 * CAN controller chip and distribution of message through attached
787 int usbcan_irq_handler(int irq, struct canchip_t *chip)
789 /* int irq_register, status, error_code;
790 struct msgobj_t *obj=chip->msgobj[0];
791 int loop_cnt=CHIP_MAX_IRQLOOP;
793 irq_register=can_read_reg(chip,SJAIR);
794 // DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
795 // DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
796 // can_read_reg(chip,SJASR));
798 if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
799 return CANCHIP_IRQ_NONE;
801 if(!(chip->flags&CHIP_CONFIGURED)) {
802 CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
803 return CANCHIP_IRQ_NONE;
806 status=can_read_reg(chip,SJASR);
811 CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
812 return CANCHIP_IRQ_STUCK;
815 // (irq_register & sjaIR_TI)
816 // old variant using SJAIR, collides with intended use with irq_accept
817 if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
818 (can_msgobj_test_fl(obj,TX_REQUEST))) {
819 DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
821 can_msgobj_set_fl(obj,TX_REQUEST);
822 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
823 can_msgobj_clear_fl(obj,TX_REQUEST);
825 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
826 usbcan_irq_write_handler(chip, obj);
828 can_msgobj_clear_fl(obj,TX_LOCK);
829 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
830 DEBUGMSG("TX looping in sja1000_irq_handler\n");
833 if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
834 // Some error happened
835 error_code=can_read_reg(chip,SJAECC);
836 sja1000_report_error(chip, status, irq_register, error_code);
837 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
838 // Reset flag set to 0 if chip is already off the bus. Full state report
841 if(error_code == 0xd9) {
843 // no such device or address - no ACK received
845 if(obj->tx_retry_cnt++>MAX_RETR) {
846 can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
847 obj->tx_retry_cnt = 0;
849 if(status&sjaSR_BS) {
850 CANMSG("bus-off, resetting usbcan\n");
851 can_write_reg(chip, 0, SJAMOD);
855 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
856 //canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
861 if(sja1000_report_error_limit_counter)
862 sja1000_report_error_limit_counter--;
866 irq_register=can_read_reg(chip,SJAIR);
868 status=can_read_reg(chip,SJASR);
870 if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
871 (irq_register & sjaIR_TI))
872 can_msgobj_set_fl(obj,TX_REQUEST);
874 } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
875 (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
876 (status & sjaSR_RBS));
878 return CANCHIP_IRQ_HANDLED;
882 * usbcan_wakeup_tx: - wakeups TX processing
883 * @chip: pointer to chip state structure
884 * @obj: pointer to message object structure
886 * Function is responsible for initiating message transmition.
887 * It is responsible for clearing of object TX_REQUEST flag
889 * Return Value: negative value reports error.
892 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
894 CANMSG("Trying to send message\n");
895 can_preempt_disable();
897 can_msgobj_set_fl(obj,TX_PENDING);
898 can_msgobj_set_fl(obj,TX_REQUEST);
899 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
900 can_msgobj_clear_fl(obj,TX_REQUEST);
902 if (!usbcan_chip_queue_status(chip)){
904 usbcan_irq_write_handler(chip, obj);
907 can_msgobj_clear_fl(obj,TX_LOCK);
908 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
909 DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
912 can_preempt_enable();
916 int usbcan_chipregister(struct chipspecops_t *chipspecops)
918 CANMSG("initializing usbcan chip operations\n");
919 chipspecops->chip_config=usbcan_chip_config;
920 chipspecops->baud_rate=usbcan_baud_rate;
921 chipspecops->standard_mask=usbcan_standard_mask;
922 chipspecops->extended_mask=usbcan_extended_mask;
923 chipspecops->message15_mask=usbcan_extended_mask;
924 chipspecops->clear_objects=usbcan_clear_objects;
925 chipspecops->config_irqs=usbcan_config_irqs;
926 chipspecops->pre_read_config=usbcan_pre_read_config;
927 chipspecops->pre_write_config=usbcan_pre_write_config;
928 chipspecops->send_msg=usbcan_send_msg;
929 chipspecops->check_tx_stat=usbcan_check_tx_stat;
930 chipspecops->wakeup_tx=usbcan_wakeup_tx;
931 chipspecops->remote_request=usbcan_remote_request;
932 chipspecops->enable_configuration=usbcan_enable_configuration;
933 chipspecops->disable_configuration=usbcan_disable_configuration;
934 chipspecops->attach_to_chip=usbcan_attach_to_chip;
935 chipspecops->release_chip=usbcan_release_chip;
936 chipspecops->set_btregs=usbcan_set_btregs;
937 chipspecops->start_chip=usbcan_start_chip;
938 chipspecops->stop_chip=usbcan_stop_chip;
939 chipspecops->irq_handler=usbcan_irq_handler;
940 chipspecops->irq_accept=NULL;
945 * usbcan_fill_chipspecops - fills chip specific operations
946 * @chip: pointer to chip representation structure
948 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
950 * Return Value: returns negative number in the case of fail
952 int usbcan_fill_chipspecops(struct canchip_t *chip)
954 chip->chip_type="usbcan";
956 usbcan_chipregister(chip->chipspecops);
961 * usbcan_init_chip_data - Initialize chips
962 * @candev: Pointer to candevice/board structure
963 * @chipnr: Number of the CAN chip on the hardware card
965 * The function usbcan_init_chip_data() is used to initialize the hardware
966 * structure containing information about the CAN chips.
967 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
969 * The @chip_base_addr entry represents the start of the 'official' memory map
970 * of the installed chip. It's likely that this is the same as the @io_addr
971 * argument supplied at module loading time.
972 * The @clock entry holds the chip clock value in Hz.
973 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
974 * register. Options defined in the %sja1000.h file:
975 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
976 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
977 * register. Options defined in the %sja1000.h file:
978 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
979 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
980 * The entry @int_clk_reg holds hardware specific options for the Clock Out
981 * register. Options defined in the %i82527.h file:
982 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
983 * The entry @int_bus_reg holds hardware specific options for the Bus
984 * Configuration register. Options defined in the %i82527.h file:
985 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
986 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
987 * register. Options defined in the %i82527.h file:
988 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
989 * Return Value: The function always returns zero
992 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
994 struct canchip_t *chip=candev->chip[chipnr];
996 usbcan_fill_chipspecops(chip);
998 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
999 candev->chip[chipnr]->chip_base_addr=0;
1000 candev->chip[chipnr]->clock = 0;
1007 /* --------------------------------------------------------------------------------------------------- */
1008 static int usbcan_sleep_thread(struct usbcan_usb *dev)
1012 /* Wait until a signal arrives or we are woken up */
1015 set_current_state(TASK_INTERRUPTIBLE);
1016 if (signal_pending(current)) {
1021 can_kthread_should_stop() ||
1022 test_bit(USBCAN_DATA_READ,&dev->flags) ||
1023 test_bit(USBCAN_TERMINATE,&dev->flags) ||
1024 test_bit(USBCAN_ERROR,&dev->flags)
1029 __set_current_state(TASK_RUNNING);
1033 static void usbcan_rcv(struct urb *urb)
1035 struct usbcan_usb *dev = urb->context;
1038 switch (urb->status) {
1041 set_bit(USBCAN_DATA_READ,&dev->flags);
1042 CANMSG("Message received\n");
1043 wake_up_process(dev->comthread);
1048 /* this urb is terminated, clean up */
1049 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1050 set_bit(USBCAN_TERMINATE,&dev->flags);
1051 wake_up_process(dev->comthread);
1054 // CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1058 retval = usb_submit_urb (urb, GFP_ATOMIC);
1060 CANMSG("%s - usb_submit_urb failed with result %d\n",
1061 __FUNCTION__, retval);
1062 set_bit(USBCAN_ERROR,&dev->flags);
1063 wake_up_process(dev->comthread);
1067 int usbcan_read_kthread(void *data)
1070 struct usbcan_usb *dev=(struct usbcan_usb *)data;
1071 struct msgobj_t *obj;
1073 /* this is normal work to do */
1074 CANMSG ("usbcan thread started!\n");
1076 dev->rx = usb_alloc_urb(0, GFP_KERNEL);
1078 CANMSG("Error allocating usb urb\n");
1081 dev->rx->dev = dev->udev;
1082 usb_fill_bulk_urb(dev->rx, dev->udev,
1083 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1087 /* an endless loop in which we are doing our work */
1090 if (!can_kthread_should_stop()){
1091 retval=usb_submit_urb(dev->rx, GFP_KERNEL);
1093 CANMSG("URB error %d\n",retval);
1099 usbcan_sleep_thread(dev);
1101 /* We need to do a memory barrier here to be sure that
1102 the flags are visible on all CPUs. */
1105 /* here we are back from sleep because we caught a signal. */
1106 if (can_kthread_should_stop())
1108 /* we received a request to terminate ourself */
1112 if (test_bit(USBCAN_ERROR,&dev->flags)){
1113 CANMSG("URB error %d\n",retval);
1114 clear_bit(USBCAN_ERROR,&dev->flags);
1117 { /* Normal work to do */
1118 if (test_bit(USBCAN_DATA_READ,&dev->flags)){
1120 clear_bit(USBCAN_DATA_READ,&dev->flags);
1121 CANMSG("Thread got received message\n");
1123 if ((dev->candev->chip[dev->rx_msg[0]])&&
1124 (dev->candev->chip[dev->rx_msg[0]]->flags & CHIP_CONFIGURED)
1128 obj=dev->candev->chip[dev->rx_msg[0]]->msgobj[0];
1130 len=*(uint8_t *)(dev->rx_msg+1);
1131 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
1132 obj->rx_msg.length = len;
1134 obj->rx_msg.flags=le16_to_cpu(*(uint16_t *)(dev->rx_msg+2));
1135 obj->rx_msg.id=le32_to_cpu((*(uint32_t *)(dev->rx_msg+4)));
1137 for(ptr=dev->rx_msg+8,i=0; i < len; ptr++,i++) {
1138 obj->rx_msg.data[i]=*ptr;
1141 // fill CAN message timestamp
1142 can_filltimestamp(&obj->rx_msg.timestamp);
1143 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
1146 CANMSG("Destination chip not found\n");
1150 /* here we go only in case of termination of the thread */
1152 usb_kill_urb(dev->rx);
1153 usb_free_urb(dev->rx);
1157 /* cleanup the thread, leave */
1158 CANMSG ("kernel thread terminated!\n");
1160 // exit_kthread(kthread);
1162 /* returning from the thread here calls the exit functions */
1165 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1167 struct usbcan_usb *dev;
1168 struct usb_host_interface *iface_desc;
1169 struct usb_endpoint_descriptor *endpoint;
1172 int retval = -ENOMEM;
1174 /* allocate memory for our device state and initialize it */
1175 dev = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1177 err("Out of memory");
1180 memset(dev, 0, sizeof(struct usbcan_usb));
1182 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1183 mutex_init(&dev->io_mutex);
1184 spin_lock_init(&dev->err_lock);
1185 init_usb_anchor(&dev->submitted);
1187 // dev->udev = usb_get_dev(interface_to_usbdev(interface));
1188 dev->udev = interface_to_usbdev(interface);
1189 dev->interface = interface;
1191 /* set up the endpoint information */
1192 /* use only the first bulk-in and bulk-out endpoints */
1193 iface_desc = interface->cur_altsetting;
1194 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1195 endpoint = &iface_desc->endpoint[i].desc;
1197 if (!dev->bulk_in_endpointAddr &&
1198 usb_endpoint_is_bulk_in(endpoint)) {
1199 /* we found a bulk in endpoint */
1200 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1201 dev->bulk_in_size = buffer_size;
1202 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1203 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1204 if (!dev->bulk_in_buffer) {
1205 err("Could not allocate bulk_in_buffer");
1210 if (!dev->bulk_out_endpointAddr &&
1211 usb_endpoint_is_bulk_out(endpoint)) {
1212 /* we found a bulk out endpoint */
1213 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1217 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1218 err("Could not find all bulk-in and bulk-out endpoints");
1221 dev->ctl_in_endpointAddr=0;
1222 dev->ctl_in_size=16;
1223 dev->ctl_in_buffer = can_checked_malloc(dev->ctl_in_size);
1224 dev->ctl_out_endpointAddr=0;
1226 /* save our data pointer in this interface device */
1227 usb_set_intfdata(interface, dev);
1229 register_usbdev("usbcan",(void *) dev);
1231 /* let the user know what node this device is now attached to */
1232 info("USB Skeleton device now attached");
1236 usb_put_dev(dev->udev);
1237 if (dev->bulk_in_buffer)
1238 can_checked_free(dev->bulk_in_buffer);
1239 if (dev->ctl_in_buffer)
1240 can_checked_free(dev->ctl_in_buffer);
1242 dev->candev->sysdevptr.anydev=NULL;
1243 cleanup_usbdev(dev->candev);
1245 can_checked_free(dev);
1249 // Physically disconnected device
1250 static void usbcan_disconnect(struct usb_interface *interface)
1252 struct usbcan_usb *dev;
1253 int minor = interface->minor;
1255 dev = usb_get_intfdata(interface);
1258 usb_set_intfdata(interface, NULL);
1260 /* prevent more I/O from starting */
1261 mutex_lock(&dev->io_mutex);
1262 dev->interface = NULL;
1263 mutex_unlock(&dev->io_mutex);
1265 //usb_kill_anchored_urbs(&dev->submitted);
1267 usb_put_dev(dev->udev);
1270 dev->candev->sysdevptr.anydev=NULL;
1271 cleanup_usbdev(dev->candev);
1274 if (dev->bulk_in_buffer)
1275 can_checked_free(dev->bulk_in_buffer);
1276 if (dev->ctl_in_buffer)
1277 can_checked_free(dev->ctl_in_buffer);
1279 can_checked_free(dev);
1281 info("USB Skeleton now disconnected");
1284 int usbcan_init(void){
1285 return usb_register(&usbcan_driver);
1288 void usbcan_exit(void){
1289 usb_deregister(&usbcan_driver);