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->rcvthread.arg = dev;
58 start_kthread(usbcan_read_kthread, &dev->rcvthread);
60 /* Adding link to can device into usbcan_usb struct */
61 ((struct usbcan_usb*)candev->sysdevptr.anydev)->candev=candev;
66 * usbcan_release_io - free reserved io memory range
67 * @candev: pointer to candevice/board which releases io
69 * The function usbcan_release_io() is used to free reserved io-memory.
70 * In case you have reserved more io memory, don't forget to free it here.
71 * IO_RANGE is the io-memory range that gets released, please adjust according
72 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
73 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
74 * Return Value: The function always returns zero
77 int usbcan_release_io(struct candevice_t *candev)
79 struct usbcan_usb *dev = ((struct usbcan_usb*)candev->sysdevptr.anydev);
81 /* terminate the kernel thread */
83 usb_kill_urb(dev->rcv);
84 usb_free_urb(dev->rcv);
86 stop_kthread(&dev->rcvthread);
91 * usbcan_reset - hardware reset routine
92 * @candev: Pointer to candevice/board structure
94 * The function usbcan_reset() is used to give a hardware reset. This is
95 * rather hardware specific so I haven't included example code. Don't forget to
96 * check the reset status of the chip before returning.
97 * Return Value: The function returns zero on success or %-ENODEV on failure
100 int usbcan_reset(struct candevice_t *candev)
106 * usbcan_init_hw_data - Initialize hardware cards
107 * @candev: Pointer to candevice/board structure
109 * The function usbcan_init_hw_data() is used to initialize the hardware
110 * structure containing information about the installed CAN-board.
111 * %RESET_ADDR represents the io-address of the hardware reset register.
112 * %NR_82527 represents the number of Intel 82527 chips on the board.
113 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
114 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
115 * the hardware uses programmable interrupts.
116 * Return Value: The function always returns zero
119 int usbcan_init_hw_data(struct candevice_t *candev)
121 candev->res_addr=RESET_ADDR;
122 candev->nr_82527_chips=0;
123 candev->nr_sja1000_chips=0;
124 candev->nr_all_chips=1;
125 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
131 * usbcan_init_obj_data - Initialize message buffers
132 * @chip: Pointer to chip specific structure
133 * @objnr: Number of the message buffer
135 * The function usbcan_init_obj_data() is used to initialize the hardware
136 * structure containing information about the different message objects on the
137 * CAN chip. In case of the sja1000 there's only one message object but on the
138 * i82527 chip there are 15.
139 * The code below is for a i82527 chip and initializes the object base addresses
140 * The entry @obj_base_addr represents the first memory address of the message
141 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
143 * Unless the hardware uses a segmented memory map, flags can be set zero.
144 * Return Value: The function always returns zero
147 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
149 chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
155 * usbcan_program_irq - program interrupts
156 * @candev: Pointer to candevice/board structure
158 * The function usbcan_program_irq() is used for hardware that uses
159 * programmable interrupts. If your hardware doesn't use programmable interrupts
160 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
161 * leave this function unedited. Again this function is hardware specific so
162 * there's no example code.
163 * Return value: The function returns zero on success or %-ENODEV on failure
166 int usbcan_program_irq(struct candevice_t *candev)
171 /* !!! Don't change this function !!! */
172 int usbcan_register(struct hwspecops_t *hwspecops)
174 hwspecops->request_io = usbcan_request_io;
175 hwspecops->release_io = usbcan_release_io;
176 hwspecops->reset = usbcan_reset;
177 hwspecops->init_hw_data = usbcan_init_hw_data;
178 hwspecops->init_chip_data = usbcan_init_chip_data;
179 hwspecops->init_obj_data = usbcan_init_obj_data;
180 hwspecops->write_register = NULL;
181 hwspecops->read_register = NULL;
182 hwspecops->program_irq = usbcan_program_irq;
186 // static int sja1000_report_error_limit_counter;
188 static void sja1000_report_error(struct canchip_t *chip,
189 unsigned sr, unsigned ir, unsigned ecc)
191 /*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*/
230 * usbcan_enable_configuration - enable chip configuration mode
231 * @chip: pointer to chip state structure
233 int usbcan_enable_configuration(struct canchip_t *chip)
239 * usbcan_disable_configuration - disable chip configuration mode
240 * @chip: pointer to chip state structure
242 int usbcan_disable_configuration(struct canchip_t *chip)
248 * usbcan_chip_config: - can chip configuration
249 * @chip: pointer to chip state structure
251 * This function configures chip and prepares it for message
252 * transmission and reception. The function resets chip,
253 * resets mask for acceptance of all messages by call to
254 * usbcan_extended_mask() function and then
255 * computes and sets baudrate with use of function usbcan_baud_rate().
256 * Return Value: negative value reports error.
259 int usbcan_chip_config(struct canchip_t *chip)
265 * usbcan_extended_mask: - setup of extended mask for message filtering
266 * @chip: pointer to chip state structure
267 * @code: can message acceptance code
268 * @mask: can message acceptance mask
270 * Return Value: negative value reports error.
273 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
276 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
280 *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
281 *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
283 retval=usb_control_msg(dev->udev,
284 usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
285 USBCAN_VENDOR_EXT_MASK_SET,
293 retval = usb_control_msg(dev->udev,
294 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
295 USBCAN_VENDOR_EXT_MASK_STATUS,
303 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
304 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
309 CANMSG("Setting extended mask failed\n");
314 * usbcan_baud_rate: - set communication parameters.
315 * @chip: pointer to chip state structure
316 * @rate: baud rate in Hz
317 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
318 * @sjw: synchronization jump width (0-3) prescaled clock cycles
319 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
320 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
322 * Return Value: negative value reports error.
325 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
326 int sampl_pt, int flags)
329 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
333 *(int32_t *)(usbbuf)=cpu_to_le32(rate);
334 *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
335 *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
336 *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
338 retval=usb_control_msg(dev->udev,
339 usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
340 USBCAN_VENDOR_BAUD_RATE_SET,
348 retval = usb_control_msg(dev->udev,
349 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
350 USBCAN_VENDOR_BAUD_RATE_STATUS,
361 CANMSG("baud rate %d is not possible to set\n",
367 * usbcan_pre_read_config: - prepares message object for message reception
368 * @chip: pointer to chip state structure
369 * @obj: pointer to message object state structure
371 * Return Value: negative value reports error.
372 * Positive value indicates immediate reception of message.
375 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
380 #define MAX_TRANSMIT_WAIT_LOOPS 10
382 * usbcan_pre_write_config: - prepares message object for message transmission
383 * @chip: pointer to chip state structure
384 * @obj: pointer to message object state structure
385 * @msg: pointer to CAN message
387 * This function prepares selected message object for future initiation
388 * of message transmission by usbcan_send_msg() function.
389 * The CAN message data and message ID are transfered from @msg slot
390 * into chip buffer in this function.
391 * Return Value: negative value reports error.
394 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
395 struct canmsg_t *msg)
397 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
402 /* Wait until Transmit Buffer Status is released */
403 while ( usbcan_chip_queue_status(chip) &&
404 i++<MAX_TRANSMIT_WAIT_LOOPS) {
407 if (usbcan_chip_queue_status(chip)){
408 CANMSG("Buffer full, cannot send message\n");
412 *(uint8_t *)(dev->tx_msg)=chip->chip_idx & 0xFF;
415 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
417 *(uint8_t *)(dev->tx_msg+1)=len & 0xFF;
418 *(uint16_t *)(dev->tx_msg+2)=cpu_to_le16(msg->flags);
419 *(uint32_t *)(dev->tx_msg+4)=cpu_to_le32(msg->id);
421 for(ptr=dev->tx_msg+8,i=0; i < len; ptr++,i++) {
422 *ptr=msg->data[i] & 0xFF;
424 for(; i < 8; ptr++,i++) {
431 * usbcan_send_msg: - initiate message transmission
432 * @chip: pointer to chip state structure
433 * @obj: pointer to message object state structure
434 * @msg: pointer to CAN message
436 * This function is called after usbcan_pre_write_config() function,
437 * which prepares data in chip buffer.
438 * Return Value: negative value reports error.
441 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
442 struct canmsg_t *msg)
444 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
447 set_bit(USBCAN_TX_PENDING,&dev->flags);
448 retval=usb_bulk_msg(dev->udev,
449 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
452 clear_bit(USBCAN_TX_PENDING,&dev->flags);
454 CANMSG("URB error %d\n",retval);
458 CANMSG("CAN message not sent\n");
466 * usbcan_check_tx_stat: - checks state of transmission engine
467 * @chip: pointer to chip state structure
469 * Return Value: negative value reports error.
470 * Positive return value indicates transmission under way status.
471 * Zero value indicates finishing of all issued transmission requests.
474 int usbcan_check_tx_stat(struct canchip_t *chip)
476 if (test_bit(USBCAN_TX_PENDING,&((struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev)->flags))
482 * usbcan_set_btregs: - configures bitrate registers
483 * @chip: pointer to chip state structure
484 * @btr0: bitrate register 0
485 * @btr1: bitrate register 1
487 * Return Value: negative value reports error.
490 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
494 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
495 uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
497 retval = usb_control_msg(dev->udev,
498 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
499 USBCAN_VENDOR_SET_BTREGS,
501 cpu_to_le16(value), chip->chip_idx,
502 dev->ctl_in_buffer, dev->ctl_in_size,
506 if(dev->ctl_in_buffer[0]==1)
513 * usbcan_start_chip: - starts chip message processing
514 * @chip: pointer to chip state structure
516 * Return Value: negative value reports error.
519 int usbcan_start_chip(struct canchip_t *chip)
522 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
524 retval = usb_control_msg(dev->udev,
525 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
526 USBCAN_VENDOR_START_CHIP,
529 dev->ctl_in_buffer, dev->ctl_in_size,
533 if(dev->ctl_in_buffer[0]==1)
540 * usbcan_chip_queue_status: - gets queue status from usb device
541 * @chip: pointer to chip state structure
543 * Return Value: negative value reports error.
544 * 0 means queue is not full
545 * 1 means queue is full
548 int usbcan_chip_queue_status(struct canchip_t *chip)
551 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
553 retval = usb_control_msg(dev->udev,
554 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
555 USBCAN_VENDOR_CHECK_TX_STAT,
558 dev->ctl_in_buffer, dev->ctl_in_size,
562 if(dev->ctl_in_buffer[0]==1)
564 if(dev->ctl_in_buffer[0]==0)
571 * usbcan_stop_chip: - stops chip message processing
572 * @chip: pointer to chip state structure
574 * Return Value: negative value reports error.
577 int usbcan_stop_chip(struct canchip_t *chip)
580 struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
582 retval = usb_control_msg(dev->udev,
583 usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
584 USBCAN_VENDOR_STOP_CHIP,
587 dev->ctl_in_buffer, dev->ctl_in_size,
591 if(dev->ctl_in_buffer[0]==1)
598 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
599 * @chip: pointer to chip state structure
601 * Return Value: negative value reports error.
604 int usbcan_attach_to_chip(struct canchip_t *chip)
610 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
611 * @chip: pointer to chip state structure
613 * Return Value: negative value reports error.
616 int usbcan_release_chip(struct canchip_t *chip)
618 usbcan_stop_chip(chip);
623 * usbcan_remote_request: - configures message object and asks for RTR message
624 * @chip: pointer to chip state structure
625 * @obj: pointer to message object structure
627 * Return Value: negative value reports error.
630 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
632 CANMSG("usbcan_remote_request not implemented\n");
637 * usbcan_standard_mask: - setup of mask for message filtering
638 * @chip: pointer to chip state structure
639 * @code: can message acceptance code
640 * @mask: can message acceptance mask
642 * Return Value: negative value reports error.
645 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
648 CANMSG("usbcan_standard_mask not implemented\n");
653 * usbcan_clear_objects: - clears state of all message object residing in chip
654 * @chip: pointer to chip state structure
656 * Return Value: negative value reports error.
659 int usbcan_clear_objects(struct canchip_t *chip)
661 CANMSG("usbcan_clear_objects not implemented\n");
666 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
667 * @chip: pointer to chip state structure
668 * @irqs: requested chip IRQ configuration
670 * Return Value: negative value reports error.
673 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
675 CANMSG("usbcan_config_irqs not implemented\n");
680 * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
681 * @chip: pointer to chip state structure
682 * @obj: pointer to attached queue description
684 * The main purpose of this function is to read message from attached queues
685 * and transfer message contents into CAN controller chip.
686 * This subroutine is called by
687 * usbcan_irq_write_handler() for transmit events.
690 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
695 // Do local transmitted message distribution if enabled
697 // fill CAN message timestamp
698 can_filltimestamp(&obj->tx_slot->msg.timestamp);
700 obj->tx_slot->msg.flags |= MSG_LOCAL;
701 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
703 // Free transmitted slot
704 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
708 can_msgobj_clear_fl(obj,TX_PENDING);
709 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
712 can_msgobj_set_fl(obj,TX_PENDING);
714 if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
716 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
717 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
721 if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
723 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
724 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
733 * usbcan_irq_handler: - interrupt service routine
734 * @irq: interrupt vector number, this value is system specific
735 * @chip: pointer to chip state structure
737 * Interrupt handler is activated when state of CAN controller chip changes,
738 * there is message to be read or there is more space for new messages or
739 * error occurs. The receive events results in reading of the message from
740 * CAN controller chip and distribution of message through attached
744 int usbcan_irq_handler(int irq, struct canchip_t *chip)
746 /* int irq_register, status, error_code;
747 struct msgobj_t *obj=chip->msgobj[0];
748 int loop_cnt=CHIP_MAX_IRQLOOP;
750 irq_register=can_read_reg(chip,SJAIR);
751 // DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
752 // DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
753 // can_read_reg(chip,SJASR));
755 if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
756 return CANCHIP_IRQ_NONE;
758 if(!(chip->flags&CHIP_CONFIGURED)) {
759 CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
760 return CANCHIP_IRQ_NONE;
763 status=can_read_reg(chip,SJASR);
768 CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
769 return CANCHIP_IRQ_STUCK;
772 // (irq_register & sjaIR_TI)
773 // old variant using SJAIR, collides with intended use with irq_accept
774 if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
775 (can_msgobj_test_fl(obj,TX_REQUEST))) {
776 DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
778 can_msgobj_set_fl(obj,TX_REQUEST);
779 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
780 can_msgobj_clear_fl(obj,TX_REQUEST);
782 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
783 usbcan_irq_write_handler(chip, obj);
785 can_msgobj_clear_fl(obj,TX_LOCK);
786 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
787 DEBUGMSG("TX looping in sja1000_irq_handler\n");
790 if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
791 // Some error happened
792 error_code=can_read_reg(chip,SJAECC);
793 sja1000_report_error(chip, status, irq_register, error_code);
794 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
795 // Reset flag set to 0 if chip is already off the bus. Full state report
798 if(error_code == 0xd9) {
800 // no such device or address - no ACK received
802 if(obj->tx_retry_cnt++>MAX_RETR) {
803 can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
804 obj->tx_retry_cnt = 0;
806 if(status&sjaSR_BS) {
807 CANMSG("bus-off, resetting usbcan\n");
808 can_write_reg(chip, 0, SJAMOD);
812 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
813 //canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
818 if(sja1000_report_error_limit_counter)
819 sja1000_report_error_limit_counter--;
823 irq_register=can_read_reg(chip,SJAIR);
825 status=can_read_reg(chip,SJASR);
827 if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
828 (irq_register & sjaIR_TI))
829 can_msgobj_set_fl(obj,TX_REQUEST);
831 } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
832 (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
833 (status & sjaSR_RBS));
835 return CANCHIP_IRQ_HANDLED;
839 * usbcan_wakeup_tx: - wakeups TX processing
840 * @chip: pointer to chip state structure
841 * @obj: pointer to message object structure
843 * Function is responsible for initiating message transmition.
844 * It is responsible for clearing of object TX_REQUEST flag
846 * Return Value: negative value reports error.
849 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
852 can_preempt_disable();
854 can_msgobj_set_fl(obj,TX_PENDING);
855 can_msgobj_set_fl(obj,TX_REQUEST);
856 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
857 can_msgobj_clear_fl(obj,TX_REQUEST);
859 if (!usbcan_chip_queue_status(chip)){
861 usbcan_irq_write_handler(chip, obj);
864 can_msgobj_clear_fl(obj,TX_LOCK);
865 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
866 DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
869 can_preempt_enable();
873 int usbcan_chipregister(struct chipspecops_t *chipspecops)
875 CANMSG("initializing usbcan chip operations\n");
876 chipspecops->chip_config=usbcan_chip_config;
877 chipspecops->baud_rate=usbcan_baud_rate;
878 chipspecops->standard_mask=usbcan_standard_mask;
879 chipspecops->extended_mask=usbcan_extended_mask;
880 chipspecops->message15_mask=usbcan_extended_mask;
881 chipspecops->clear_objects=usbcan_clear_objects;
882 chipspecops->config_irqs=usbcan_config_irqs;
883 chipspecops->pre_read_config=usbcan_pre_read_config;
884 chipspecops->pre_write_config=usbcan_pre_write_config;
885 chipspecops->send_msg=usbcan_send_msg;
886 chipspecops->check_tx_stat=usbcan_check_tx_stat;
887 chipspecops->wakeup_tx=usbcan_wakeup_tx;
888 chipspecops->remote_request=usbcan_remote_request;
889 chipspecops->enable_configuration=usbcan_enable_configuration;
890 chipspecops->disable_configuration=usbcan_disable_configuration;
891 chipspecops->attach_to_chip=usbcan_attach_to_chip;
892 chipspecops->release_chip=usbcan_release_chip;
893 chipspecops->set_btregs=usbcan_set_btregs;
894 chipspecops->start_chip=usbcan_start_chip;
895 chipspecops->stop_chip=usbcan_stop_chip;
896 chipspecops->irq_handler=usbcan_irq_handler;
897 chipspecops->irq_accept=NULL;
902 * usbcan_fill_chipspecops - fills chip specific operations
903 * @chip: pointer to chip representation structure
905 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
907 * Return Value: returns negative number in the case of fail
909 int usbcan_fill_chipspecops(struct canchip_t *chip)
911 chip->chip_type="usbcan";
913 usbcan_chipregister(chip->chipspecops);
918 * usbcan_init_chip_data - Initialize chips
919 * @candev: Pointer to candevice/board structure
920 * @chipnr: Number of the CAN chip on the hardware card
922 * The function usbcan_init_chip_data() is used to initialize the hardware
923 * structure containing information about the CAN chips.
924 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
926 * The @chip_base_addr entry represents the start of the 'official' memory map
927 * of the installed chip. It's likely that this is the same as the @io_addr
928 * argument supplied at module loading time.
929 * The @clock entry holds the chip clock value in Hz.
930 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
931 * register. Options defined in the %sja1000.h file:
932 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
933 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
934 * register. Options defined in the %sja1000.h file:
935 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
936 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
937 * The entry @int_clk_reg holds hardware specific options for the Clock Out
938 * register. Options defined in the %i82527.h file:
939 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
940 * The entry @int_bus_reg holds hardware specific options for the Bus
941 * Configuration register. Options defined in the %i82527.h file:
942 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
943 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
944 * register. Options defined in the %i82527.h file:
945 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
946 * Return Value: The function always returns zero
949 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
951 struct canchip_t *chip=candev->chip[chipnr];
953 usbcan_fill_chipspecops(chip);
955 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
956 candev->chip[chipnr]->chip_base_addr=0;
957 candev->chip[chipnr]->clock = 0;
964 /* --------------------------------------------------------------------------------------------------- */
966 static void usbcan_rcv(struct urb *urb)
968 struct usbcan_usb *dev = urb->context;
971 switch (urb->status) {
974 set_bit(USBCAN_DATA_READ,&dev->flags);
975 wake_up(&dev->rcvthread.queue);
980 /* this urb is terminated, clean up */
981 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
982 set_bit(USBCAN_TERMINATE,&dev->flags);
983 wake_up(&dev->rcvthread.queue);
986 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
990 retval = usb_submit_urb (urb, GFP_ATOMIC);
992 CANMSG("%s - usb_submit_urb failed with result %d\n",
993 __FUNCTION__, retval);
994 set_bit(USBCAN_ERROR,&dev->flags);
995 wake_up(&dev->rcvthread.queue);
999 void usbcan_read_kthread(kthread_t *kthread)
1002 struct usbcan_usb *dev=(struct usbcan_usb *)kthread->arg;
1003 struct msgobj_t *obj;
1005 /* setup the thread environment */
1006 init_kthread(kthread, "usbcan");
1008 /* this is normal work to do */
1009 CANMSG ("usbcan thread started!\n");
1011 dev->rcv = usb_alloc_urb(0, GFP_KERNEL);
1013 CANMSG("Error allocating usb urb\n");
1016 dev->rcv->dev = dev->udev;
1017 usb_fill_bulk_urb(dev->rcv, dev->udev,
1018 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1022 /* an endless loop in which we are doing our work */
1025 retval=usb_submit_urb(dev->rcv, GFP_KERNEL);
1027 CANMSG("URB error %d\n",retval);
1031 wait_event_interruptible(kthread->queue,
1032 test_bit(USBCAN_DATA_READ,&dev->flags)
1033 || test_bit(USBCAN_TERMINATE,&dev->flags)
1034 || test_bit(USBCAN_ERROR,&dev->flags)
1037 /* We need to do a memory barrier here to be sure that
1038 the flags are visible on all CPUs. */
1041 /* here we are back from sleep because we caught a signal. */
1042 if (kthread->terminate)
1044 /* we received a request to terminate ourself */
1048 if (test_bit(USBCAN_ERROR,&dev->flags)){
1049 CANMSG("URB error %d\n",retval);
1050 clear_bit(USBCAN_ERROR,&dev->flags);
1053 { /* Normal work to do */
1054 if (test_bit(USBCAN_DATA_READ,&dev->flags)){
1056 clear_bit(USBCAN_DATA_READ,&dev->flags);
1058 if ((dev->candev->chip[dev->rcv_msg[0]])&&
1059 (dev->candev->chip[dev->rcv_msg[0]]->flags & CHIP_CONFIGURED)
1063 obj=dev->candev->chip[dev->rcv_msg[0]]->msgobj[0];
1065 len=*(uint8_t *)(dev->rcv_msg+1);
1066 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
1067 obj->rx_msg.length = len;
1069 obj->rx_msg.flags=le16_to_cpu(*(uint16_t *)(dev->rcv_msg+2));
1070 obj->rx_msg.id=le32_to_cpu((*(uint32_t *)(dev->rcv_msg+4)));
1072 for(ptr=dev->rcv_msg+8,i=0; i < len; ptr++,i++) {
1073 obj->rx_msg.data[i]=*ptr;
1076 // fill CAN message timestamp
1077 can_filltimestamp(&obj->rx_msg.timestamp);
1078 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
1083 /* here we go only in case of termination of the thread */
1085 /* cleanup the thread, leave */
1086 CANMSG ("kernel thread terminated!\n");
1087 exit_kthread(kthread);
1089 /* returning from the thread here calls the exit functions */
1092 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1094 struct usbcan_usb *dev;
1095 struct usb_host_interface *iface_desc;
1096 struct usb_endpoint_descriptor *endpoint;
1099 int retval = -ENOMEM;
1101 /* allocate memory for our device state and initialize it */
1102 dev = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1104 err("Out of memory");
1107 memset(dev, 0, sizeof(struct usbcan_usb));
1109 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1110 spin_lock_init(&dev->err_lock);
1111 init_usb_anchor(&dev->submitted);
1113 // dev->udev = usb_get_dev(interface_to_usbdev(interface));
1114 dev->udev = interface_to_usbdev(interface);
1115 dev->interface = interface;
1117 /* set up the endpoint information */
1118 /* use only the first bulk-in and bulk-out endpoints */
1119 iface_desc = interface->cur_altsetting;
1120 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1121 endpoint = &iface_desc->endpoint[i].desc;
1123 if (!dev->bulk_in_endpointAddr &&
1124 usb_endpoint_is_bulk_in(endpoint)) {
1125 /* we found a bulk in endpoint */
1126 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1127 dev->bulk_in_size = buffer_size;
1128 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1129 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1130 if (!dev->bulk_in_buffer) {
1131 err("Could not allocate bulk_in_buffer");
1136 if (!dev->bulk_out_endpointAddr &&
1137 usb_endpoint_is_bulk_out(endpoint)) {
1138 /* we found a bulk out endpoint */
1139 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1142 if (!dev->ctl_in_endpointAddr &&
1143 usb_endpoint_xfer_control(endpoint) &&
1144 usb_endpoint_dir_in(endpoint)) {
1145 /* we found a bulk in endpoint */
1146 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1147 dev->ctl_in_size = buffer_size;
1148 dev->ctl_in_endpointAddr = endpoint->bEndpointAddress;
1149 dev->ctl_in_buffer = can_checked_malloc(buffer_size);
1150 if (!dev->ctl_in_buffer) {
1151 err("Could not allocate bulk_in_buffer");
1156 if (!dev->ctl_out_endpointAddr &&
1157 usb_endpoint_xfer_control(endpoint) &&
1158 usb_endpoint_dir_out(endpoint)) {
1159 /* we found a bulk out endpoint */
1160 dev->ctl_out_endpointAddr = endpoint->bEndpointAddress;
1163 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1164 err("Could not find all bulk-in and bulk-out endpoints");
1168 /* save our data pointer in this interface device */
1169 usb_set_intfdata(interface, dev);
1171 register_usbdev("usbcan",(void *) dev);
1173 /* let the user know what node this device is now attached to */
1174 info("USB Skeleton device now attached");
1178 usb_put_dev(dev->udev);
1179 if (dev->bulk_in_buffer)
1180 can_checked_free(dev->bulk_in_buffer);
1181 if (dev->ctl_in_buffer)
1182 can_checked_free(dev->ctl_in_buffer);
1184 dev->candev->sysdevptr.anydev=NULL;
1185 cleanup_usbdev(dev->candev);
1187 can_checked_free(dev);
1191 // Physically disconnected device
1192 static void usbcan_disconnect(struct usb_interface *interface)
1194 struct usbcan_usb *dev;
1195 int minor = interface->minor;
1197 dev = usb_get_intfdata(interface);
1200 usb_set_intfdata(interface, NULL);
1202 /* prevent more I/O from starting */
1203 mutex_lock(&dev->io_mutex);
1204 dev->interface = NULL;
1205 mutex_unlock(&dev->io_mutex);
1207 //usb_kill_anchored_urbs(&dev->submitted);
1209 usb_put_dev(dev->udev);
1210 if (dev->bulk_in_buffer)
1211 can_checked_free(dev->bulk_in_buffer);
1212 if (dev->ctl_in_buffer)
1213 can_checked_free(dev->ctl_in_buffer);
1215 dev->candev->sysdevptr.anydev=NULL;
1216 cleanup_usbdev(dev->candev);
1218 can_checked_free(dev);
1220 info("USB Skeleton now disconnected");
1223 int usbcan_init(void){
1224 return usb_register(&usbcan_driver);
1227 void usbcan_exit(void){
1228 usb_deregister(&usbcan_driver);