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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
16 #include <linux/freezer.h>
19 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
20 static void usbcan_disconnect(struct usb_interface *interface);
22 volatile int usbcan_chip_count=0;
24 /* table of devices that work with this driver */
25 static struct usb_device_id usbcan_table [] = {
26 { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
27 { } /* Terminating entry */
29 MODULE_DEVICE_TABLE(usb, usbcan_table);
31 static struct usb_driver usbcan_driver = {
33 .id_table = usbcan_table,
34 .probe = usbcan_probe,
35 .disconnect = usbcan_disconnect,
39 * usbcan_request_io: - reserve io or memory range for can board
40 * @candev: pointer to candevice/board which asks for io. Field @io_addr
41 * of @candev is used in most cases to define start of the range
43 * The function usbcan_request_io() is used to reserve the io-memory. If your
44 * hardware uses a dedicated memory range as hardware control registers you
45 * will have to add the code to reserve this memory as well.
46 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
47 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
48 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
49 * Return Value: The function returns zero on success or %-ENODEV on failure
52 int usbcan_request_io(struct candevice_t *candev)
54 struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
57 CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
58 CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
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)
83 * usbcan_reset - hardware reset routine
84 * @candev: Pointer to candevice/board structure
86 * The function usbcan_reset() is used to give a hardware reset. This is
87 * rather hardware specific so I haven't included example code. Don't forget to
88 * check the reset status of the chip before returning.
89 * Return Value: The function returns zero on success or %-ENODEV on failure
92 int usbcan_reset(struct candevice_t *candev)
98 * usbcan_init_hw_data - Initialize hardware cards
99 * @candev: Pointer to candevice/board structure
101 * The function usbcan_init_hw_data() is used to initialize the hardware
102 * structure containing information about the installed CAN-board.
103 * %RESET_ADDR represents the io-address of the hardware reset register.
104 * %NR_82527 represents the number of Intel 82527 chips on the board.
105 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
106 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
107 * the hardware uses programmable interrupts.
108 * Return Value: The function always returns zero
111 int usbcan_init_hw_data(struct candevice_t *candev)
113 candev->res_addr=RESET_ADDR;
114 candev->nr_82527_chips=0;
115 candev->nr_sja1000_chips=0;
116 candev->nr_all_chips=usbcan_chip_count;
117 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
123 * usbcan_init_obj_data - Initialize message buffers
124 * @chip: Pointer to chip specific structure
125 * @objnr: Number of the message buffer
127 * The function usbcan_init_obj_data() is used to initialize the hardware
128 * structure containing information about the different message objects on the
129 * CAN chip. In case of the sja1000 there's only one message object but on the
130 * i82527 chip there are 15.
131 * The code below is for a i82527 chip and initializes the object base addresses
132 * The entry @obj_base_addr represents the first memory address of the message
133 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
135 * Unless the hardware uses a segmented memory map, flags can be set zero.
136 * Return Value: The function always returns zero
139 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
141 chip->msgobj[objnr]->obj_base_addr=0;
147 * usbcan_program_irq - program interrupts
148 * @candev: Pointer to candevice/board structure
150 * The function usbcan_program_irq() is used for hardware that uses
151 * programmable interrupts. If your hardware doesn't use programmable interrupts
152 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
153 * leave this function unedited. Again this function is hardware specific so
154 * there's no example code.
155 * Return value: The function returns zero on success or %-ENODEV on failure
158 int usbcan_program_irq(struct candevice_t *candev)
163 /* !!! Don't change this function !!! */
164 int usbcan_register(struct hwspecops_t *hwspecops)
166 hwspecops->request_io = usbcan_request_io;
167 hwspecops->release_io = usbcan_release_io;
168 hwspecops->reset = usbcan_reset;
169 hwspecops->init_hw_data = usbcan_init_hw_data;
170 hwspecops->init_chip_data = usbcan_init_chip_data;
171 hwspecops->init_obj_data = usbcan_init_obj_data;
172 hwspecops->write_register = NULL;
173 hwspecops->read_register = NULL;
174 hwspecops->program_irq = usbcan_program_irq;
178 // static int sja1000_report_error_limit_counter;
180 static void usbcan_report_error(struct canchip_t *chip,
181 unsigned sr, unsigned ir, unsigned ecc)
183 /*TODO : Error reporting from device */
186 if(sja1000_report_error_limit_counter>=100)
189 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
192 sja1000_report_error_limit_counter+=10;
194 if(sja1000_report_error_limit_counter>=100){
195 sja1000_report_error_limit_counter+=10;
196 CANMSG("Error: too many errors, reporting disabled\n");
200 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
201 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
202 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
203 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
204 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
205 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
206 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
207 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
208 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
209 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
210 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
211 if((sr&sjaIR_EI) || 1){
212 CANMSG("EI: %s %s %s\n",
213 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
214 ecc&sjaECC_DIR?"RX":"TX",
215 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
218 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
224 * usbcan_enable_configuration - enable chip configuration mode
225 * @chip: pointer to chip state structure
227 int usbcan_enable_configuration(struct canchip_t *chip)
233 * usbcan_disable_configuration - disable chip configuration mode
234 * @chip: pointer to chip state structure
236 int usbcan_disable_configuration(struct canchip_t *chip)
242 * usbcan_chip_config: - can chip configuration
243 * @chip: pointer to chip state structure
245 * This function configures chip and prepares it for message
246 * transmission and reception. The function resets chip,
247 * resets mask for acceptance of all messages by call to
248 * usbcan_extended_mask() function and then
249 * computes and sets baudrate with use of function usbcan_baud_rate().
250 * Return Value: negative value reports error.
253 int usbcan_chip_config(struct canchip_t *chip)
259 * usbcan_extended_mask: - setup of extended mask for message filtering
260 * @chip: pointer to chip state structure
261 * @code: can message acceptance code
262 * @mask: can message acceptance mask
264 * Return Value: negative value reports error.
267 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
270 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
272 u8 usbbuf[USBCAN_TRANSFER_SIZE];
277 *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
278 *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
280 retval=usb_control_msg(dev->udev,
281 usb_sndctrlpipe(dev->udev, 0),
282 USBCAN_VENDOR_EXT_MASK_SET,
284 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
285 &usbbuf, USBCAN_TRANSFER_SIZE,
290 retval = usb_control_msg(dev->udev,
291 usb_rcvctrlpipe(dev->udev, 0),
292 USBCAN_VENDOR_EXT_MASK_STATUS,
294 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
295 &usbbuf, USBCAN_TRANSFER_SIZE,
300 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
301 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
306 CANMSG("Setting extended mask failed\n");
311 * usbcan_baud_rate: - set communication parameters.
312 * @chip: pointer to chip state structure
313 * @rate: baud rate in Hz
314 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
315 * @sjw: synchronization jump width (0-3) prescaled clock cycles
316 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
317 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
319 * Return Value: negative value reports error.
322 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
323 int sampl_pt, int flags)
326 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
328 u8 usbbuf[USBCAN_TRANSFER_SIZE];
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, 0),
340 USBCAN_VENDOR_BAUD_RATE_SET,
342 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
343 &usbbuf, USBCAN_TRANSFER_SIZE,
348 retval = usb_control_msg(dev->udev,
349 usb_rcvctrlpipe(dev->udev, 0),
350 USBCAN_VENDOR_BAUD_RATE_STATUS,
352 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
353 usbbuf, USBCAN_TRANSFER_SIZE,
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)
401 * usbcan_send_msg: - initiate message transmission
402 * @chip: pointer to chip state structure
403 * @obj: pointer to message object state structure
404 * @msg: pointer to CAN message
406 * This function is called after usbcan_pre_write_config() function,
407 * which prepares data in chip buffer.
408 * Return Value: negative value reports error.
411 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
412 struct canmsg_t *msg)
418 * usbcan_check_tx_stat: - checks state of transmission engine
419 * @chip: pointer to chip state structure
421 * Return Value: negative value reports error.
422 * Positive return value indicates transmission under way status.
423 * Zero value indicates finishing of all issued transmission requests.
426 int usbcan_check_tx_stat(struct canchip_t *chip)
428 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
431 if (test_bit(USBCAN_TX_PENDING,&dev->flags))
437 * usbcan_set_btregs: - configures bitrate registers
438 * @chip: pointer to chip state structure
439 * @btr0: bitrate register 0
440 * @btr1: bitrate register 1
442 * Return Value: negative value reports error.
445 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
449 u8 buf[USBCAN_TRANSFER_SIZE];
450 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
451 uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
456 retval = usb_control_msg(dev->udev,
457 usb_rcvctrlpipe(dev->udev, 0),
458 USBCAN_VENDOR_SET_BTREGS,
460 cpu_to_le16(value), cpu_to_le16(chip->chip_idx),
461 &buf, USBCAN_TRANSFER_SIZE,
472 * usbcan_start_chip: - starts chip message processing
473 * @chip: pointer to chip state structure
475 * Return Value: negative value reports error.
478 int usbcan_start_chip(struct canchip_t *chip)
481 u8 buf[USBCAN_TRANSFER_SIZE];
482 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
487 retval = usb_control_msg(dev->udev,
488 usb_rcvctrlpipe(dev->udev, 0),
489 USBCAN_VENDOR_START_CHIP,
491 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
492 &buf, USBCAN_TRANSFER_SIZE,
503 * usbcan_chip_queue_status: - gets queue status from usb device
504 * @chip: pointer to chip state structure
506 * Return Value: negative value reports error.
507 * 0 means queue is not full
508 * 1 means queue is full
511 int usbcan_chip_queue_status(struct canchip_t *chip)
514 u8 buf[USBCAN_TRANSFER_SIZE];
515 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
519 retval = usb_control_msg(dev->udev,
520 usb_rcvctrlpipe(dev->udev, 0),
521 USBCAN_VENDOR_CHECK_TX_STAT,
523 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
524 &buf, USBCAN_TRANSFER_SIZE,
528 DEBUGMSG("Chip_queue_status: %d\n",buf[0]);
534 CANMSG("Chip_queue_status error: %d\n",retval);
539 * usbcan_stop_chip: - stops chip message processing
540 * @chip: pointer to chip state structure
542 * Return Value: negative value reports error.
545 int usbcan_stop_chip(struct canchip_t *chip)
548 u8 buf[USBCAN_TRANSFER_SIZE];
549 struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
554 retval = usb_control_msg(dev->udev,
555 usb_rcvctrlpipe(dev->udev, 0),
556 USBCAN_VENDOR_STOP_CHIP,
558 cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
559 &buf, USBCAN_TRANSFER_SIZE,
570 * usbcan_register_devs: - attaches usb device data to the chip structure
571 * @chip: pointer to chip state structure
572 * @data: usb device data
576 void usbcan_register_devs(struct canchip_t *chip,void *data){
577 struct usbcan_devs *usbdevs=(struct usbcan_devs *)data;
579 CANMSG("Bad structure given\n");
582 if (chip->chip_idx>=usbdevs->count) {
583 CANMSG("Requested chip number is bigger than chip count\n");
587 usbdevs->devs[chip->chip_idx]->chip=chip;
588 chip->chip_data=(void *)usbdevs->devs[chip->chip_idx];
592 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
593 * @chip: pointer to chip state structure
595 * Return Value: negative value reports error.
598 int usbcan_attach_to_chip(struct canchip_t *chip)
600 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
602 /* start kernel thread */
603 dev->comthread=can_kthread_run(usbcan_kthread, (void *)dev, "usbcan_%d",chip->chip_idx);
609 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
610 * @chip: pointer to chip state structure
612 * Return Value: negative value reports error.
615 int usbcan_release_chip(struct canchip_t *chip)
617 struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
619 usbcan_stop_chip(chip);
621 /* terminate the kernel thread */
622 set_bit(USBCAN_TERMINATE,&dev->flags);
623 wake_up_process(dev->comthread);
624 // can_kthread_stop(dev->comthread);
630 * usbcan_remote_request: - configures message object and asks for RTR message
631 * @chip: pointer to chip state structure
632 * @obj: pointer to message object structure
634 * Return Value: negative value reports error.
637 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
639 CANMSG("usbcan_remote_request not implemented\n");
644 * usbcan_standard_mask: - setup of mask for message filtering
645 * @chip: pointer to chip state structure
646 * @code: can message acceptance code
647 * @mask: can message acceptance mask
649 * Return Value: negative value reports error.
652 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
655 CANMSG("usbcan_standard_mask not implemented\n");
660 * usbcan_clear_objects: - clears state of all message object residing in chip
661 * @chip: pointer to chip state structure
663 * Return Value: negative value reports error.
666 int usbcan_clear_objects(struct canchip_t *chip)
668 CANMSG("usbcan_clear_objects not implemented\n");
673 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
674 * @chip: pointer to chip state structure
675 * @irqs: requested chip IRQ configuration
677 * Return Value: negative value reports error.
680 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
682 CANMSG("usbcan_config_irqs not implemented\n");
687 * usbcan_kthread_read_handler: - part of kthread code responsible for receive completed events
688 * @dev: pointer to usb device related structure
689 * @obj: pointer to attached message object description
691 * The main purpose of this function is to read message from usb urb
692 * and transfer message contents to CAN queue ends.
693 * This subroutine is called by
697 void usbcan_kthread_read_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
698 int i, j, len, retval;
699 DEBUGMSG("USBCAN RX handler\n");
700 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
701 if (test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&dev->rx[i].flags)){
702 DEBUGMSG("USBCAN Thread has received a message\n");
703 if ((dev->chip)&&(dev->chip->flags & CHIP_CONFIGURED)){
705 struct usbcan_message *mess=&dev->rx[i];
707 len=*(u8 *)(mess->msg+1);
708 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
709 obj->rx_msg.length = len;
711 obj->rx_msg.flags=le16_to_cpu(*(u16 *)(mess->msg+2));
712 obj->rx_msg.id=le32_to_cpu((*(u32 *)(mess->msg+4)));
714 for(ptr=mess->msg+8,j=0; j < len; ptr++,j++) {
715 obj->rx_msg.data[j]=*ptr;
718 // fill CAN message timestamp
719 can_filltimestamp(&obj->rx_msg.timestamp);
720 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
723 CANMSG("Destination chip not found\n");
725 if (!test_bit(USBCAN_MESSAGE_URB_PENDING,&dev->rx[i].flags)){
726 DEBUGMSG("Renewing RX urb\n");
727 retval = usb_submit_urb (dev->rx[i].u, GFP_KERNEL);
729 CANMSG("%d. URB error %d\n", i, retval);
730 set_bit(USBCAN_ERROR,&dev->flags);
733 set_bit(USBCAN_MESSAGE_URB_PENDING,&dev->rx[i].flags);
739 * usbcan_kthread_write_handler: - part of kthread code responsible for transmit done events
740 * @dev: pointer to usb device related structure
741 * @obj: pointer to attached message object description
743 * The main purpose of this function is to free allocated resources on transmit done event
744 * This subroutine is called by
748 void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
750 DEBUGMSG("USBCAN TX handler\n");
751 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
752 if (test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&dev->tx[i].flags)){
753 struct usbcan_message *mess=&dev->tx[i];
754 DEBUGMSG("USBCAN Message successfully sent\n");
757 // Do local transmitted message distribution if enabled
759 // fill CAN message timestamp
760 can_filltimestamp(&mess->slot->msg.timestamp);
762 mess->slot->msg.flags |= MSG_LOCAL;
763 canque_filter_msg2edges(obj->qends, &mess->slot->msg);
765 // Free transmitted slot
766 canque_free_outslot(obj->qends, mess->qedge, mess->slot);
769 can_msgobj_clear_fl(obj,TX_PENDING);
771 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
772 set_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
774 // Test if some new messages arrived
775 set_bit(USBCAN_TX_PENDING,&dev->flags);
781 * usbcan_kthread_write_request_handler: - part of kthread code responsible for sending transmit urbs
782 * @dev: pointer to usb device related structure
783 * @obj: pointer to attached message object description
785 * The main purpose of this function is to create a usb transmit safe object
786 * and send it via free transmit usb urb
787 * This subroutine is called by
791 void usbcan_kthread_write_request_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
792 int i, j, cmd, len, retval;
793 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
794 if (test_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags)){
795 struct usbcan_message *mess=&dev->tx[i];
797 cmd=canque_test_outslot(obj->qends, &mess->qedge, &mess->slot);
799 DEBUGMSG("USBCAN Sending a message\n");
801 can_msgobj_set_fl(obj,TX_PENDING);
802 clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
803 clear_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
805 *(u8 *)(mess->msg)=0;
806 len = mess->slot->msg.length;
807 if(len > CAN_MSG_LENGTH)
808 len = CAN_MSG_LENGTH;
809 *(u8 *)(mess->msg+1)=len & 0xFF;
810 *(u16 *)(mess->msg+2)=cpu_to_le16(mess->slot->msg.flags);
811 *(u32 *)(mess->msg+4)=cpu_to_le32(mess->slot->msg.id);
813 for(ptr=mess->msg+8,j=0; j < len; ptr++,j++) {
814 *ptr=mess->slot->msg.data[j] & 0xFF;
816 for(; j < 8; ptr++,j++) {
820 set_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
821 retval = usb_submit_urb (dev->tx[i].u, GFP_KERNEL);
823 CANMSG("%d. URB error %d\n",i,retval);
824 clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
825 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
826 set_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
828 canque_notify_inends(mess->qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
829 canque_free_outslot(obj->qends, mess->qedge, mess->slot);
834 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
844 * usbcan_irq_handler: - interrupt service routine
845 * @irq: interrupt vector number, this value is system specific
846 * @chip: pointer to chip state structure
848 * Interrupt handler is activated when state of CAN controller chip changes,
849 * there is message to be read or there is more space for new messages or
850 * error occurs. The receive events results in reading of the message from
851 * CAN controller chip and distribution of message through attached
855 int usbcan_irq_handler(int irq, struct canchip_t *chip)
857 return CANCHIP_IRQ_HANDLED;
861 * usbcan_wakeup_tx: - wakeups TX processing
862 * @chip: pointer to chip state structure
863 * @obj: pointer to message object structure
865 * Function is responsible for initiating message transmition.
866 * It is responsible for clearing of object TX_REQUEST flag
868 * Return Value: negative value reports error.
871 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
873 struct usbcan_usb *dev=(struct usbcan_usb *)chip->chip_data;
875 DEBUGMSG("Trying to send message\n");
876 can_preempt_disable();
878 can_msgobj_set_fl(obj,TX_PENDING);
879 can_msgobj_set_fl(obj,TX_REQUEST);
880 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
881 can_msgobj_clear_fl(obj,TX_REQUEST);
883 if (test_and_clear_bit(USBCAN_FREE_TX_URB,&dev->flags)){
885 set_bit(USBCAN_TX_PENDING,&dev->flags);
886 if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
887 wake_up_process(dev->comthread);
890 can_msgobj_clear_fl(obj,TX_LOCK);
891 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
892 CANMSG("TX looping in usbcan_wakeup_tx\n");
895 can_preempt_enable();
899 int usbcan_chipregister(struct chipspecops_t *chipspecops)
901 CANMSG("initializing usbcan chip operations\n");
902 chipspecops->chip_config=usbcan_chip_config;
903 chipspecops->baud_rate=usbcan_baud_rate;
904 chipspecops->standard_mask=usbcan_standard_mask;
905 chipspecops->extended_mask=usbcan_extended_mask;
906 chipspecops->message15_mask=usbcan_extended_mask;
907 chipspecops->clear_objects=usbcan_clear_objects;
908 chipspecops->config_irqs=usbcan_config_irqs;
909 chipspecops->pre_read_config=usbcan_pre_read_config;
910 chipspecops->pre_write_config=usbcan_pre_write_config;
911 chipspecops->send_msg=usbcan_send_msg;
912 chipspecops->check_tx_stat=usbcan_check_tx_stat;
913 chipspecops->wakeup_tx=usbcan_wakeup_tx;
914 chipspecops->remote_request=usbcan_remote_request;
915 chipspecops->enable_configuration=usbcan_enable_configuration;
916 chipspecops->disable_configuration=usbcan_disable_configuration;
917 chipspecops->attach_to_chip=usbcan_attach_to_chip;
918 chipspecops->release_chip=usbcan_release_chip;
919 chipspecops->set_btregs=usbcan_set_btregs;
920 chipspecops->start_chip=usbcan_start_chip;
921 chipspecops->stop_chip=usbcan_stop_chip;
922 chipspecops->irq_handler=usbcan_irq_handler;
923 chipspecops->irq_accept=NULL;
928 * usbcan_fill_chipspecops - fills chip specific operations
929 * @chip: pointer to chip representation structure
931 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
933 * Return Value: returns negative number in the case of fail
935 int usbcan_fill_chipspecops(struct canchip_t *chip)
937 chip->chip_type="usbcan";
939 usbcan_chipregister(chip->chipspecops);
944 * usbcan_init_chip_data - Initialize chips
945 * @candev: Pointer to candevice/board structure
946 * @chipnr: Number of the CAN chip on the hardware card
948 * The function usbcan_init_chip_data() is used to initialize the hardware
949 * structure containing information about the CAN chips.
950 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
952 * The @chip_base_addr entry represents the start of the 'official' memory map
953 * of the installed chip. It's likely that this is the same as the @io_addr
954 * argument supplied at module loading time.
955 * The @clock entry holds the chip clock value in Hz.
956 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
957 * register. Options defined in the %sja1000.h file:
958 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
959 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
960 * register. Options defined in the %sja1000.h file:
961 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
962 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
963 * The entry @int_clk_reg holds hardware specific options for the Clock Out
964 * register. Options defined in the %i82527.h file:
965 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
966 * The entry @int_bus_reg holds hardware specific options for the Bus
967 * Configuration register. Options defined in the %i82527.h file:
968 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
969 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
970 * register. Options defined in the %i82527.h file:
971 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
972 * Return Value: The function always returns zero
975 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
977 struct canchip_t *chip=candev->chip[chipnr];
979 usbcan_fill_chipspecops(chip);
981 candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
982 candev->chip[chipnr]->chip_base_addr=0;
983 candev->chip[chipnr]->clock = 0;
989 /** *********************************
990 * USB related functions
991 * ********************************* */
993 static int usbcan_sleep_thread(struct usbcan_usb *dev)
997 /* Wait until a signal arrives or we are woken up */
1000 set_current_state(TASK_INTERRUPTIBLE);
1001 if (signal_pending(current)) {
1006 can_kthread_should_stop() ||
1007 test_bit(USBCAN_DATA_OK,&dev->flags) ||
1008 test_bit(USBCAN_TX_PENDING,&dev->flags) ||
1009 test_bit(USBCAN_TERMINATE,&dev->flags) ||
1010 test_bit(USBCAN_ERROR,&dev->flags)
1015 __set_current_state(TASK_RUNNING);
1019 static void usbcan_callback(struct urb *urb)
1021 struct usbcan_message *mess = urb->context;
1024 if (!test_bit(USBCAN_THREAD_RUNNING,&mess->dev->flags))
1026 if (test_bit(USBCAN_MESSAGE_TERMINATE,&mess->flags))
1029 switch (urb->status) {
1032 DEBUGMSG("%s > Message OK\n", __FUNCTION__);
1033 set_bit(USBCAN_DATA_OK,&mess->dev->flags);
1034 set_bit(USBCAN_MESSAGE_DATA_OK,&mess->flags);
1035 if (test_bit(USBCAN_MESSAGE_TYPE_RX,&mess->flags)){
1036 DEBUGMSG("%s > RX flag set\n", __FUNCTION__);
1037 set_bit(USBCAN_DATA_RX,&mess->dev->flags);
1039 if (test_bit(USBCAN_MESSAGE_TYPE_TX,&mess->flags))
1040 DEBUGMSG("%s > TX flag set\n", __FUNCTION__);
1041 set_bit(USBCAN_DATA_TX,&mess->dev->flags);
1042 clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
1043 if (test_bit(USBCAN_THREAD_RUNNING,&mess->dev->flags))
1044 wake_up_process(mess->dev->comthread);
1046 CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
1047 // wake_up(&mess->dev->queue);
1052 /* this urb is terminated, clean up */
1053 CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1054 set_bit(USBCAN_TERMINATE,&mess->dev->flags);
1055 set_bit(USBCAN_MESSAGE_TERMINATE,&mess->flags);
1056 clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
1059 //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
1063 // Try to send urb again on non significant errors
1064 retval = usb_submit_urb (urb, GFP_ATOMIC);
1066 CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
1067 set_bit(USBCAN_ERROR,&mess->dev->flags);
1068 clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
1069 if (test_bit(USBCAN_THREAD_RUNNING,&mess->dev->flags))
1070 wake_up_process(mess->dev->comthread);
1071 // wake_up(&mess->dev->queue);
1075 int usbcan_kthread(void *data)
1078 struct usbcan_usb *dev=(struct usbcan_usb *)data;
1079 struct msgobj_t *obj;
1081 CANMSG("Usbcan thread started...\n");
1085 obj=dev->chip->msgobj[0];
1087 /* Prepare receive urbs */
1088 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1089 dev->rx[i].u = usb_alloc_urb(0, GFP_KERNEL);
1091 CANMSG("Error allocating %d. usb receive urb\n",i);
1094 dev->rx[i].u->dev = dev->udev;
1095 dev->rx[i].dev = dev;
1096 usb_fill_bulk_urb(dev->rx[i].u, dev->udev,
1097 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1098 dev->rx[i].msg, USBCAN_TRANSFER_SIZE,
1099 usbcan_callback, &dev->rx[i]);
1100 set_bit(USBCAN_MESSAGE_TYPE_RX,&dev->rx[i].flags);
1103 /* Prepare transmit urbs */
1104 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
1105 dev->tx[i].u = usb_alloc_urb(0, GFP_KERNEL);
1107 CANMSG("Error allocating %d. usb transmit urb\n",i);
1110 dev->tx[i].u->dev = dev->udev;
1111 dev->tx[i].dev = dev;
1112 usb_fill_bulk_urb(dev->tx[i].u, dev->udev,
1113 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
1114 dev->tx[i].msg, USBCAN_TRANSFER_SIZE,
1115 usbcan_callback, &dev->tx[i]);
1116 set_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
1117 set_bit(USBCAN_MESSAGE_TYPE_TX,&dev->tx[i].flags);
1120 set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1121 set_bit(USBCAN_FREE_TX_URB,&dev->flags);
1123 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1124 retval=usb_submit_urb(dev->rx[i].u, GFP_KERNEL);
1126 CANMSG("%d. URB error %d\n",i,retval);
1127 set_bit(USBCAN_ERROR,&dev->flags);
1131 set_bit(USBCAN_MESSAGE_URB_PENDING,&dev->rx[i].flags);
1133 /* an endless loop in which we are doing our work */
1136 /* We need to do a memory barrier here to be sure that
1137 the flags are visible on all CPUs. */
1140 if (!(can_kthread_should_stop() || test_bit(USBCAN_TERMINATE,&dev->flags))){
1141 if (usbcan_sleep_thread(dev)<0)
1143 /* wait_event_interruptible(dev->queue,
1144 can_kthread_should_stop() ||
1145 test_bit(USBCAN_DATA_OK,&dev->flags) ||
1146 test_bit(USBCAN_TX_PENDING,&dev->flags) ||
1147 test_bit(USBCAN_TERMINATE,&dev->flags) ||
1148 test_bit(USBCAN_ERROR,&dev->flags)
1151 /* We need to do a memory barrier here to be sure that
1152 the flags are visible on all CPUs. */
1155 /* here we are back from sleep because we caught a signal. */
1156 if (can_kthread_should_stop()){
1157 /* we received a request to terminate ourself */
1161 /* here we are back from sleep because we caught a signal. */
1162 if (test_bit(USBCAN_TERMINATE,&dev->flags)){
1163 /* we received a request to terminate ourself */
1167 { /* Normal work to do */
1168 if (test_and_clear_bit(USBCAN_DATA_OK,&dev->flags)){
1169 DEBUGMSG("USBCAN Succesfull data transfer\n");
1171 if (test_and_clear_bit(USBCAN_DATA_RX,&dev->flags)){
1172 usbcan_kthread_read_handler(dev, obj);
1174 if (test_and_clear_bit(USBCAN_DATA_TX,&dev->flags)){
1175 usbcan_kthread_write_handler(dev, obj);
1178 if (test_and_clear_bit(USBCAN_TX_PENDING,&dev->flags)){
1179 usbcan_kthread_write_request_handler(dev, obj);
1183 set_bit(USBCAN_TERMINATE,&dev->flags);
1185 /* here we go only in case of termination of the thread */
1186 for (i=0;i<USBCAN_TOT_RX_URBS;i++){
1188 set_bit(USBCAN_MESSAGE_TERMINATE,&dev->rx[i].flags);
1189 usb_kill_urb(dev->rx[i].u);
1190 usb_free_urb(dev->rx[i].u);
1193 for (i=0;i<USBCAN_TOT_TX_URBS;i++){
1195 set_bit(USBCAN_MESSAGE_TERMINATE,&dev->tx[i].flags);
1196 usb_kill_urb(dev->tx[i].u);
1197 usb_free_urb(dev->tx[i].u);
1200 clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
1202 CANMSG ("usbcan thread finished!\n");
1205 /* cleanup the thread, leave */
1206 CANMSG ("kernel thread terminated!\n");
1210 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1212 struct usbcan_devs *usbdevs=NULL;
1213 struct usb_host_interface *iface_desc;
1214 struct usb_endpoint_descriptor *endpoint;
1217 int retval = -ENOMEM;
1219 iface_desc = interface->cur_altsetting;
1220 if (iface_desc->desc.bNumEndpoints % 2){
1221 CANMSG("Endpoint count must be even");
1225 usbcan_chip_count = iface_desc->desc.bNumEndpoints / 2;
1227 usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
1229 CANMSG("Out of memory");
1232 memset(usbdevs, 0, sizeof(struct usbcan_devs));
1234 usbdevs->count=usbcan_chip_count;
1236 usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
1237 if (!usbdevs->devs) {
1238 CANMSG("Out of memory");
1241 memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
1243 for (j=0;j<usbcan_chip_count;j++){
1244 struct usbcan_usb *dev;
1247 /* allocate memory for our device state and initialize it */
1248 usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
1249 if (!usbdevs->devs[j]) {
1250 CANMSG("Out of memory");
1253 memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
1254 dev=usbdevs->devs[j];
1256 mutex_init(&dev->io_mutex);
1257 init_waitqueue_head(&dev->queue);
1258 dev->udev = interface_to_usbdev(interface);
1259 dev->interface = interface;
1261 /* set up the endpoint information */
1262 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1263 endpoint = &iface_desc->endpoint[i].desc;
1268 if ((usbdevs->devs[k]->bulk_in_endpointAddr & USB_ENDPOINT_NUMBER_MASK) == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
1272 epnum=endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1275 if (!dev->bulk_in_endpointAddr &&
1276 usb_endpoint_is_bulk_in(endpoint)) {
1277 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1278 /* we found a bulk in endpoint */
1279 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1280 dev->bulk_in_size = buffer_size;
1281 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1282 dev->bulk_in_buffer = can_checked_malloc(buffer_size);
1283 if (!dev->bulk_in_buffer) {
1284 CANMSG("Could not allocate bulk_in_buffer");
1290 if (!dev->bulk_out_endpointAddr &&
1291 usb_endpoint_is_bulk_out(endpoint)) {
1292 if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
1293 /* we found a bulk out endpoint */
1294 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1299 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1300 CANMSG("Could not find all bulk-in and bulk-out endpoints for chip %d",j);
1304 /* save our data pointer in this interface device */
1305 usb_set_intfdata(interface, usbdevs);
1307 if (!(usbdevs->candev=register_usbdev("usbcan",(void *) usbdevs, usbcan_register_devs)))
1308 goto register_error;
1310 /* let the user know what node this device is now attached to */
1311 CANMSG("USBCAN device now attached\n");
1315 cleanup_usbdev(usbdevs->candev);
1319 if (usbdevs->devs[0]){
1320 usb_put_dev(usbdevs->devs[0]->udev);
1322 for (j=0;j<usbdevs->count;j++){
1323 if (!usbdevs->devs[j]) continue;
1325 if (usbdevs->devs[j]->bulk_in_buffer)
1326 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1327 if (usbdevs->devs[j]->chip){
1328 usbdevs->devs[j]->chip->chip_data=NULL;
1330 can_checked_free(usbdevs->devs[j]);
1332 can_checked_free(usbdevs->devs);
1334 can_checked_free(usbdevs);
1340 // Physically disconnected device
1341 static void usbcan_disconnect(struct usb_interface *interface)
1343 struct usbcan_devs *usbdevs;
1345 usbdevs = usb_get_intfdata(interface);
1347 CANMSG("USBCAN device seems to be removed\n");
1350 usb_set_intfdata(interface, NULL);
1353 usb_put_dev((*usbdevs->devs)->udev);
1355 cleanup_usbdev(usbdevs->candev);
1357 for (j=0;j<usbdevs->count;j++){
1358 if (!usbdevs->devs[j]) continue;
1360 /* prevent more I/O from starting */
1361 mutex_lock(&usbdevs->devs[j]->io_mutex);
1362 usbdevs->devs[j]->interface = NULL;
1363 mutex_unlock(&usbdevs->devs[j]->io_mutex);
1365 while (test_bit(USBCAN_THREAD_RUNNING,&usbdevs->devs[j]->flags))
1367 CANMSG("USBCAN thread has not stopped, trying to wake...\n");
1368 set_bit(USBCAN_TERMINATE,&usbdevs->devs[j]->flags);
1369 wake_up_process(usbdevs->devs[j]->comthread);
1371 // can_kthread_stop(dev->comthread);
1374 if (usbdevs->devs[j]->bulk_in_buffer)
1375 can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
1376 // if (usbdevs->devs[j]->chip){
1377 // usbdevs->devs[j]->chip->chip_data=NULL;
1379 can_checked_free(usbdevs->devs[j]);
1380 usbdevs->devs[j]=NULL;
1382 can_checked_free(usbdevs->devs);
1384 can_checked_free(usbdevs);
1386 CANMSG("USBCAN now disconnected\n");
1389 int usbcan_init(void){
1390 return usb_register(&usbcan_driver);
1393 void usbcan_exit(void){
1394 usb_deregister(&usbcan_driver);