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 /* our private defines. if this grows any larger, use your own .h file */
16 #define MAX_TRANSFER (PAGE_SIZE - 512)
17 /* MAX_TRANSFER is chosen so that the VM is not stressed by
18 allocations > PAGE_SIZE and the number of packets in a page
19 is an integer 512 is the largest possible packet on EHCI */
20 #define WRITES_IN_FLIGHT 8
21 /* arbitrarily chosen */
23 /* Define these values to match your devices */
24 #define USB_SKEL_VENDOR_ID 0xDEAD
25 #define USB_SKEL_PRODUCT_ID 0x1001
27 /* table of devices that work with this driver */
28 static struct usb_device_id usbcan_table [] = {
29 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
30 { } /* Terminating entry */
32 MODULE_DEVICE_TABLE(usb, usbcan_table);
34 extern struct file_operations can_fops;
36 int usbcan_register(struct hwspecops_t *hwspecops);
39 * IO_RANGE is the io-memory range that gets reserved, please adjust according
40 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
41 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
43 #define IO_RANGE 0x100
45 /* Structure to hold all of our device specific stuff */
47 struct usb_device *udev; /* the usb device for this device */
48 struct usb_interface *interface; /* the interface for this device */
49 struct semaphore limit_sem; /* limiting the number of writes in progress */
50 struct usb_anchor submitted; /* in case we need to retract our submissions */
51 unsigned char *bulk_in_buffer; /* the buffer to receive data */
52 size_t bulk_in_size; /* the size of the receive buffer */
53 unsigned char *int_in_buffer; /* the buffer to receive data */
54 size_t int_in_size; /* the size of the receive buffer */
55 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
56 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
57 __u8 int_in_endpointAddr; /* the address of the interrupt in endpoint */
59 int errors; /* the last request tanked */
60 int open_count; /* count the number of openers */
61 spinlock_t err_lock; /* lock for errors */
62 struct mutex io_mutex; /* synchronize I/O with disconnect */
64 struct candevice_t *candev;
67 static struct usb_driver usbcan_driver;
70 * usbcan_request_io: - reserve io or memory range for can board
71 * @candev: pointer to candevice/board which asks for io. Field @io_addr
72 * of @candev is used in most cases to define start of the range
74 * The function usbcan_request_io() is used to reserve the io-memory. If your
75 * hardware uses a dedicated memory range as hardware control registers you
76 * will have to add the code to reserve this memory as well.
77 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
78 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
79 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
80 * Return Value: The function returns zero on success or %-ENODEV on failure
83 int usbcan_request_io(struct candevice_t *candev)
85 ((struct usb_ul_usb1*)candev->sysdevptr.anydev)->candev=candev;
90 * usbcan_release_io - free reserved io memory range
91 * @candev: pointer to candevice/board which releases io
93 * The function usbcan_release_io() is used to free reserved io-memory.
94 * In case you have reserved more io memory, don't forget to free it here.
95 * IO_RANGE is the io-memory range that gets released, please adjust according
96 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
97 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
98 * Return Value: The function always returns zero
101 int usbcan_release_io(struct candevice_t *candev)
103 struct usb_ul_usb1 *dev;
104 if (candev->sysdevptr.anydev){
105 dev=(struct usb_ul_usb1*) candev->sysdevptr.anydev;
106 usb_put_dev(dev->udev);
107 usb_kill_urb(dev->irq);
108 usb_free_urb(dev->irq);
109 kfree(dev->bulk_in_buffer);
110 kfree(dev->int_in_buffer);
112 dev->candev->sysdevptr.anydev=NULL;
113 //cleanup_usbdev(dev->candev);
122 * usbcan_reset - hardware reset routine
123 * @candev: Pointer to candevice/board structure
125 * The function usbcan_reset() is used to give a hardware reset. This is
126 * rather hardware specific so I haven't included example code. Don't forget to
127 * check the reset status of the chip before returning.
128 * Return Value: The function returns zero on success or %-ENODEV on failure
131 int usbcan_reset(struct candevice_t *candev)
136 #define RESET_ADDR 0x0
139 * usbcan_init_hw_data - Initialize hardware cards
140 * @candev: Pointer to candevice/board structure
142 * The function usbcan_init_hw_data() is used to initialize the hardware
143 * structure containing information about the installed CAN-board.
144 * %RESET_ADDR represents the io-address of the hardware reset register.
145 * %NR_82527 represents the number of Intel 82527 chips on the board.
146 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
147 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
148 * the hardware uses programmable interrupts.
149 * Return Value: The function always returns zero
152 int usbcan_init_hw_data(struct candevice_t *candev)
154 candev->res_addr=RESET_ADDR;
155 candev->nr_82527_chips=0;
156 candev->nr_sja1000_chips=0;
157 candev->nr_all_chips=1;
158 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
164 * usbcan_init_obj_data - Initialize message buffers
165 * @chip: Pointer to chip specific structure
166 * @objnr: Number of the message buffer
168 * The function usbcan_init_obj_data() is used to initialize the hardware
169 * structure containing information about the different message objects on the
170 * CAN chip. In case of the sja1000 there's only one message object but on the
171 * i82527 chip there are 15.
172 * The code below is for a i82527 chip and initializes the object base addresses
173 * The entry @obj_base_addr represents the first memory address of the message
174 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
176 * Unless the hardware uses a segmented memory map, flags can be set zero.
177 * Return Value: The function always returns zero
180 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
182 chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
188 * usbcan_program_irq - program interrupts
189 * @candev: Pointer to candevice/board structure
191 * The function usbcan_program_irq() is used for hardware that uses
192 * programmable interrupts. If your hardware doesn't use programmable interrupts
193 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
194 * leave this function unedited. Again this function is hardware specific so
195 * there's no example code.
196 * Return value: The function returns zero on success or %-ENODEV on failure
199 int usbcan_program_irq(struct candevice_t *candev)
205 * usbcan_write_register - Low level write register routine
206 * @data: data to be written
207 * @address: memory address to write to
209 * The function usbcan_write_register() is used to write to hardware registers
210 * on the CAN chip. You should only have to edit this function if your hardware
211 * uses some specific write process.
212 * Return Value: The function does not return a value
215 void usbcan_write_register(unsigned data, unsigned long address)
221 * usbcan_read_register - Low level read register routine
222 * @address: memory address to read from
224 * The function usbcan_read_register() is used to read from hardware registers
225 * on the CAN chip. You should only have to edit this function if your hardware
226 * uses some specific read process.
227 * Return Value: The function returns the value stored in @address
230 unsigned usbcan_read_register(unsigned long address)
235 /* !!! Don't change this function !!! */
236 int usbcan_register(struct hwspecops_t *hwspecops)
238 hwspecops->request_io = usbcan_request_io;
239 hwspecops->release_io = usbcan_release_io;
240 hwspecops->reset = usbcan_reset;
241 hwspecops->init_hw_data = usbcan_init_hw_data;
242 hwspecops->init_chip_data = usbcan_init_chip_data;
243 hwspecops->init_obj_data = usbcan_init_obj_data;
244 hwspecops->write_register = usbcan_write_register;
245 hwspecops->read_register = usbcan_read_register;
246 hwspecops->program_irq = usbcan_program_irq;
250 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
252 static const char *sja1000_ecc_errc_str[]={
256 "other type of error"
259 static const char *sja1000_ecc_seg_str[]={
279 "tolerate dominant bits",
282 "passive error flag",
287 "acknowledge delimiter",
294 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
296 static int sja1000_report_error_limit_counter;
298 static void sja1000_report_error(struct canchip_t *chip,
299 unsigned sr, unsigned ir, unsigned ecc)
301 /* if(sja1000_report_error_limit_counter>=100)
304 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
307 sja1000_report_error_limit_counter+=10;
309 if(sja1000_report_error_limit_counter>=100){
310 sja1000_report_error_limit_counter+=10;
311 CANMSG("Error: too many errors, reporting disabled\n");
315 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
316 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
317 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
318 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
319 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
320 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
321 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
322 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
323 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
324 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
325 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
326 if((sr&sjaIR_EI) || 1){
327 CANMSG("EI: %s %s %s\n",
328 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
329 ecc&sjaECC_DIR?"RX":"TX",
330 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
333 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
338 * usbcan_enable_configuration - enable chip configuration mode
339 * @chip: pointer to chip state structure
341 int usbcan_enable_configuration(struct canchip_t *chip)
344 enum sja1000_PeliCAN_MOD flags;
346 can_disable_irq(chip->chip_irq);
348 flags=can_read_reg(chip,SJAMOD);
350 while ((!(flags & sjaMOD_RM)) && (i<=10)) {
351 can_write_reg(chip, sjaMOD_RM, SJAMOD);
352 // TODO: configurable sjaMOD_AFM (32/16 bit acceptance filter)
353 // config sjaMOD_LOM (listen only)
356 flags=can_read_reg(chip, SJAMOD);
359 CANMSG("Reset error\n");
360 can_enable_irq(chip->chip_irq);
368 * usbcan_disable_configuration - disable chip configuration mode
369 * @chip: pointer to chip state structure
371 int usbcan_disable_configuration(struct canchip_t *chip)
374 enum sja1000_PeliCAN_MOD flags;
376 flags=can_read_reg(chip,SJAMOD);
378 while ( (flags & sjaMOD_RM) && (i<=50) ) {
379 // could be as long as 11*128 bit times after buss-off
380 can_write_reg(chip, 0, SJAMOD);
381 // TODO: configurable sjaMOD_AFM (32/16 bit acceptance filter)
382 // config sjaMOD_LOM (listen only)
385 flags=can_read_reg(chip, SJAMOD);
388 CANMSG("Error leaving reset status\n");
392 can_enable_irq(chip->chip_irq);
398 * usbcan_chip_config: - can chip configuration
399 * @chip: pointer to chip state structure
401 * This function configures chip and prepares it for message
402 * transmission and reception. The function resets chip,
403 * resets mask for acceptance of all messages by call to
404 * usbcan_extended_mask() function and then
405 * computes and sets baudrate with use of function usbcan_baud_rate().
406 * Return Value: negative value reports error.
409 int usbcan_chip_config(struct canchip_t *chip)
414 if (usbcan_enable_configuration(chip))
417 // Set mode, clock out, comparator
418 can_write_reg(chip,sjaCDR_PELICAN|chip->sja_cdr_reg,SJACDR);
420 // Ensure, that interrupts are disabled even on the chip level now
421 can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
423 // Set driver output configuration
424 can_write_reg(chip,chip->sja_ocr_reg,SJAOCR);
426 // Simple check for chip presence
427 for (i=0, n=0x5a; i<8; i++, n+=0xf) {
428 can_write_reg(chip,n,SJAACR0+i);
430 for (i=0, n=0x5a; i<8; i++, n+=0xf) {
431 r = n^can_read_reg(chip,SJAACR0+i);
433 CANMSG("usbcan_chip_config: chip connection broken,"
434 " readback differ 0x%02x\n", r);
440 if (usbcan_extended_mask(chip,0x00000000, 0xffffffff))
444 chip->baudrate=1000000;
445 if (usbcan_baud_rate(chip,chip->baudrate,chip->clock,0,75,0))
448 // Enable hardware interrupts
449 can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER);
451 usbcan_disable_configuration(chip);
457 * usbcan_extended_mask: - setup of extended mask for message filtering
458 * @chip: pointer to chip state structure
459 * @code: can message acceptance code
460 * @mask: can message acceptance mask
462 * Return Value: negative value reports error.
465 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
469 if (usbcan_enable_configuration(chip))
472 // LSB to +3, MSB to +0
473 for(i=SJA_PeliCAN_AC_LEN; --i>=0;) {
474 can_write_reg(chip,code&0xff,SJAACR0+i);
475 can_write_reg(chip,mask&0xff,SJAAMR0+i);
480 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
481 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
483 usbcan_disable_configuration(chip);
489 * usbcan_baud_rate: - set communication parameters.
490 * @chip: pointer to chip state structure
491 * @rate: baud rate in Hz
492 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
493 * @sjw: synchronization jump width (0-3) prescaled clock cycles
494 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
495 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
497 * Return Value: negative value reports error.
500 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
501 int sampl_pt, int flags)
503 // int best_error = 1000000000, error;
504 // int best_tseg=0, best_brp=0, best_rate=0, brp=0;
505 // int tseg=0, tseg1=0, tseg2=0;
507 // if (usbcan_enable_configuration(chip))
512 // // tseg even = round down, odd = round up
513 // for (tseg=(0+0+2)*2; tseg<=(sjaMAX_TSEG2+sjaMAX_TSEG1+2)*2+1; tseg++) {
514 // brp = clock/((1+tseg/2)*rate)+tseg%2;
515 // if (brp == 0 || brp > 64)
517 // error = rate - clock/(brp*(1+tseg/2));
520 // if (error <= best_error) {
521 // best_error = error;
522 // best_tseg = tseg/2;
524 // best_rate = clock/(brp*(1+tseg/2));
527 // if (best_error && (rate/best_error < 10)) {
528 // CANMSG("baud rate %d is not possible with %d Hz clock\n",
530 // CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
531 // best_rate, best_brp, best_tseg, tseg1, tseg2);
534 // tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
537 // if (tseg2 > sjaMAX_TSEG2)
538 // tseg2 = sjaMAX_TSEG2;
539 // tseg1 = best_tseg-tseg2-2;
540 // if (tseg1>sjaMAX_TSEG1) {
541 // tseg1 = sjaMAX_TSEG1;
542 // tseg2 = best_tseg-tseg1-2;
545 // DEBUGMSG("Setting %d bps.\n", best_rate);
546 // DEBUGMSG("brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d, sampl_pt=%d\n",
547 // best_brp, best_tseg, tseg1, tseg2,
548 // (100*(best_tseg-tseg2)/(best_tseg+1)));
551 // can_write_reg(chip, sjw<<6 | best_brp, SJABTR0);
552 // can_write_reg(chip, ((flags & BTR1_SAM) != 0)<<7 | (tseg2<<4)
553 // | tseg1, SJABTR1);
555 // usbcan_disable_configuration(chip);
561 * usbcan_read: - reads and distributes one or more received messages
562 * @chip: pointer to chip state structure
563 * @obj: pinter to CAN message queue information
567 void usbcan_read(struct canchip_t *chip, struct msgobj_t *obj) {
568 /* int i, flags, len, datastart;
570 flags = can_read_reg(chip,SJAFRM);
571 if(flags&sjaFRM_FF) {
573 (can_read_reg(chip,SJAID0)<<21) +
574 (can_read_reg(chip,SJAID1)<<13) +
575 (can_read_reg(chip,SJAID2)<<5) +
576 (can_read_reg(chip,SJAID3)>>3);
580 (can_read_reg(chip,SJAID0)<<3) +
581 (can_read_reg(chip,SJAID1)>>5);
585 ((flags & sjaFRM_RTR) ? MSG_RTR : 0) |
586 ((flags & sjaFRM_FF) ? MSG_EXT : 0);
587 len = flags & sjaFRM_DLC_M;
588 obj->rx_msg.length = len;
589 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
590 for(i=0; i< len; i++) {
591 obj->rx_msg.data[i]=can_read_reg(chip,datastart+i);
594 // fill CAN message timestamp
595 can_filltimestamp(&obj->rx_msg.timestamp);
597 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
599 can_write_reg(chip, sjaCMR_RRB, SJACMR);
601 } while (can_read_reg(chip, SJASR) & sjaSR_RBS);*/
605 * usbcan_pre_read_config: - prepares message object for message reception
606 * @chip: pointer to chip state structure
607 * @obj: pointer to message object state structure
609 * Return Value: negative value reports error.
610 * Positive value indicates immediate reception of message.
613 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
616 status=can_read_reg(chip,SJASR);
618 if(status & sjaSR_BS) {
619 // Try to recover from error condition
620 DEBUGMSG("usbcan_pre_read_config bus-off recover 0x%x\n",status);
621 usbcan_enable_configuration(chip);
622 can_write_reg(chip, 0, SJARXERR);
623 can_write_reg(chip, 0, SJATXERR1);
624 can_read_reg(chip, SJAECC);
625 usbcan_disable_configuration(chip);
628 if (!(status&sjaSR_RBS)) {
632 can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER); //disable interrupts for a moment
633 usbcan_read(chip, obj);
634 can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER); //enable interrupts*/
638 #define MAX_TRANSMIT_WAIT_LOOPS 10
640 * usbcan_pre_write_config: - prepares message object for message transmission
641 * @chip: pointer to chip state structure
642 * @obj: pointer to message object state structure
643 * @msg: pointer to CAN message
645 * This function prepares selected message object for future initiation
646 * of message transmission by usbcan_send_msg() function.
647 * The CAN message data and message ID are transfered from @msg slot
648 * into chip buffer in this function.
649 * Return Value: negative value reports error.
652 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
653 struct canmsg_t *msg)
660 // Wait until Transmit Buffer Status is released
661 while ( !((status=can_read_reg(chip, SJASR)) & sjaSR_TBS) &&
662 i++<MAX_TRANSMIT_WAIT_LOOPS) {
666 if(status & sjaSR_BS) {
667 // Try to recover from error condition
668 DEBUGMSG("usbcan_pre_write_config bus-off recover 0x%x\n",status);
669 usbcan_enable_configuration(chip);
670 can_write_reg(chip, 0, SJARXERR);
671 can_write_reg(chip, 0, SJATXERR1);
672 can_read_reg(chip, SJAECC);
673 usbcan_disable_configuration(chip);
675 if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
676 CANMSG("Transmit timed out, cancelling\n");
677 // here we should check if there is no write/select waiting for this
678 // transmit. If so, set error ret and wake up.
679 // CHECKME: if we do not disable sjaIER_TIE (TX IRQ) here we get interrupt
681 can_write_reg(chip, sjaCMR_AT, SJACMR);
683 while ( !(can_read_reg(chip, SJASR) & sjaSR_TBS) &&
684 i++<MAX_TRANSMIT_WAIT_LOOPS) {
687 if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
688 CANMSG("Could not cancel, please reset\n");
693 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
694 // len &= sjaFRM_DLC_M; ensured by above condition already
695 can_write_reg(chip, ((msg->flags&MSG_EXT)?sjaFRM_FF:0) |
696 ((msg->flags & MSG_RTR) ? sjaFRM_RTR : 0) | len, SJAFRM);
697 if(msg->flags&MSG_EXT) {
699 can_write_reg(chip, id & 0xff, SJAID3);
701 can_write_reg(chip, id & 0xff, SJAID2);
703 can_write_reg(chip, id & 0xff, SJAID1);
705 can_write_reg(chip, id, SJAID0);
706 for(i=0; i < len; i++) {
707 can_write_reg(chip, msg->data[i], SJADATE+i);
711 can_write_reg(chip, (id >> 8) & 0xff, SJAID0);
712 can_write_reg(chip, id & 0xff, SJAID1);
713 for(i=0; i < len; i++) {
714 can_write_reg(chip, msg->data[i], SJADATS+i);
721 * usbcan_send_msg: - initiate message transmission
722 * @chip: pointer to chip state structure
723 * @obj: pointer to message object state structure
724 * @msg: pointer to CAN message
726 * This function is called after usbcan_pre_write_config() function,
727 * which prepares data in chip buffer.
728 * Return Value: negative value reports error.
731 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
732 struct canmsg_t *msg)
734 /* can_write_reg(chip, sjaCMR_TR, SJACMR);
740 * usbcan_check_tx_stat: - checks state of transmission engine
741 * @chip: pointer to chip state structure
743 * Return Value: negative value reports error.
744 * Positive return value indicates transmission under way status.
745 * Zero value indicates finishing of all issued transmission requests.
748 int usbcan_check_tx_stat(struct canchip_t *chip)
750 // if (can_read_reg(chip,SJASR) & sjaSR_TCS)
757 * usbcan_set_btregs: - configures bitrate registers
758 * @chip: pointer to chip state structure
759 * @btr0: bitrate register 0
760 * @btr1: bitrate register 1
762 * Return Value: negative value reports error.
765 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
768 /* if (usbcan_enable_configuration(chip))
771 can_write_reg(chip, btr0, SJABTR0);
772 can_write_reg(chip, btr1, SJABTR1);
774 usbcan_disable_configuration(chip);
780 * usbcan_start_chip: - starts chip message processing
781 * @chip: pointer to chip state structure
783 * Return Value: negative value reports error.
786 int usbcan_start_chip(struct canchip_t *chip)
788 /* enum sja1000_PeliCAN_MOD flags;
790 flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
791 can_write_reg(chip, flags, SJAMOD);
793 sja1000_report_error_limit_counter=0;
799 * usbcan_stop_chip: - stops chip message processing
800 * @chip: pointer to chip state structure
802 * Return Value: negative value reports error.
805 int usbcan_stop_chip(struct canchip_t *chip)
807 /* enum sja1000_PeliCAN_MOD flags;
809 flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
810 can_write_reg(chip, flags|sjaMOD_RM, SJAMOD);
816 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
817 * @chip: pointer to chip state structure
819 * Return Value: negative value reports error.
822 int usbcan_attach_to_chip(struct canchip_t *chip)
828 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
829 * @chip: pointer to chip state structure
831 * Return Value: negative value reports error.
834 int usbcan_release_chip(struct canchip_t *chip)
836 /* usbcan_stop_chip(chip);
837 can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
843 * usbcan_remote_request: - configures message object and asks for RTR message
844 * @chip: pointer to chip state structure
845 * @obj: pointer to message object structure
847 * Return Value: negative value reports error.
850 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
852 CANMSG("usbcan_remote_request not implemented\n");
857 * usbcan_standard_mask: - setup of mask for message filtering
858 * @chip: pointer to chip state structure
859 * @code: can message acceptance code
860 * @mask: can message acceptance mask
862 * Return Value: negative value reports error.
865 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
868 CANMSG("usbcan_standard_mask not implemented\n");
873 * usbcan_clear_objects: - clears state of all message object residing in chip
874 * @chip: pointer to chip state structure
876 * Return Value: negative value reports error.
879 int usbcan_clear_objects(struct canchip_t *chip)
881 CANMSG("usbcan_clear_objects not implemented\n");
886 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
887 * @chip: pointer to chip state structure
888 * @irqs: requested chip IRQ configuration
890 * Return Value: negative value reports error.
893 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
895 CANMSG("usbcan_config_irqs not implemented\n");
900 * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
901 * @chip: pointer to chip state structure
902 * @obj: pointer to attached queue description
904 * The main purpose of this function is to read message from attached queues
905 * and transfer message contents into CAN controller chip.
906 * This subroutine is called by
907 * usbcan_irq_write_handler() for transmit events.
910 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
915 // Do local transmitted message distribution if enabled
917 // fill CAN message timestamp
918 can_filltimestamp(&obj->tx_slot->msg.timestamp);
920 obj->tx_slot->msg.flags |= MSG_LOCAL;
921 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
923 // Free transmitted slot
924 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
928 can_msgobj_clear_fl(obj,TX_PENDING);
929 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
932 can_msgobj_set_fl(obj,TX_PENDING);
934 if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
936 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
937 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
941 if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
943 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
944 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
954 * usbcan_irq_handler: - interrupt service routine
955 * @irq: interrupt vector number, this value is system specific
956 * @chip: pointer to chip state structure
958 * Interrupt handler is activated when state of CAN controller chip changes,
959 * there is message to be read or there is more space for new messages or
960 * error occurs. The receive events results in reading of the message from
961 * CAN controller chip and distribution of message through attached
965 int usbcan_irq_handler(int irq, struct canchip_t *chip)
967 /* int irq_register, status, error_code;
968 struct msgobj_t *obj=chip->msgobj[0];
969 int loop_cnt=CHIP_MAX_IRQLOOP;
971 irq_register=can_read_reg(chip,SJAIR);
972 // DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
973 // DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
974 // can_read_reg(chip,SJASR));
976 if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
977 return CANCHIP_IRQ_NONE;
979 if(!(chip->flags&CHIP_CONFIGURED)) {
980 CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
981 return CANCHIP_IRQ_NONE;
984 status=can_read_reg(chip,SJASR);
989 CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
990 return CANCHIP_IRQ_STUCK;
993 // (irq_register & sjaIR_RI)
994 // old variant using SJAIR, collides with intended use with irq_accept
995 if (status & sjaSR_RBS) {
996 DEBUGMSG("sja1000_irq_handler: RI or RBS\n");
997 usbcan_read(chip,obj);
1001 // (irq_register & sjaIR_TI)
1002 // old variant using SJAIR, collides with intended use with irq_accept
1003 if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
1004 (can_msgobj_test_fl(obj,TX_REQUEST))) {
1005 DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
1007 can_msgobj_set_fl(obj,TX_REQUEST);
1008 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
1009 can_msgobj_clear_fl(obj,TX_REQUEST);
1011 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
1012 usbcan_irq_write_handler(chip, obj);
1014 can_msgobj_clear_fl(obj,TX_LOCK);
1015 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
1016 DEBUGMSG("TX looping in sja1000_irq_handler\n");
1019 if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
1020 // Some error happened
1021 error_code=can_read_reg(chip,SJAECC);
1022 sja1000_report_error(chip, status, irq_register, error_code);
1023 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
1024 // Reset flag set to 0 if chip is already off the bus. Full state report
1027 if(error_code == 0xd9) {
1029 // no such device or address - no ACK received
1031 if(obj->tx_retry_cnt++>MAX_RETR) {
1032 can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
1033 obj->tx_retry_cnt = 0;
1035 if(status&sjaSR_BS) {
1036 CANMSG("bus-off, resetting usbcan\n");
1037 can_write_reg(chip, 0, SJAMOD);
1041 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
1042 //canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
1043 //obj->tx_slot=NULL;
1047 if(sja1000_report_error_limit_counter)
1048 sja1000_report_error_limit_counter--;
1049 obj->tx_retry_cnt=0;
1052 irq_register=can_read_reg(chip,SJAIR);
1054 status=can_read_reg(chip,SJASR);
1056 if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
1057 (irq_register & sjaIR_TI))
1058 can_msgobj_set_fl(obj,TX_REQUEST);
1060 } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
1061 (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
1062 (status & sjaSR_RBS));
1064 return CANCHIP_IRQ_HANDLED;
1068 * usbcan_wakeup_tx: - wakeups TX processing
1069 * @chip: pointer to chip state structure
1070 * @obj: pointer to message object structure
1072 * Function is responsible for initiating message transmition.
1073 * It is responsible for clearing of object TX_REQUEST flag
1075 * Return Value: negative value reports error.
1076 * File: src/usbcan.c
1078 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
1081 /* can_preempt_disable();
1083 can_msgobj_set_fl(obj,TX_PENDING);
1084 can_msgobj_set_fl(obj,TX_REQUEST);
1085 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
1086 can_msgobj_clear_fl(obj,TX_REQUEST);
1088 if (can_read_reg(chip, SJASR) & sjaSR_TBS){
1089 obj->tx_retry_cnt=0;
1090 usbcan_irq_write_handler(chip, obj);
1093 can_msgobj_clear_fl(obj,TX_LOCK);
1094 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
1095 DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
1098 can_preempt_enable();*/
1102 int usbcan_chipregister(struct chipspecops_t *chipspecops)
1108 * usbcan_fill_chipspecops - fills chip specific operations
1109 * @chip: pointer to chip representation structure
1111 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
1113 * Return Value: returns negative number in the case of fail
1115 int usbcan_fill_chipspecops(struct canchip_t *chip)
1121 * usbcan_init_chip_data - Initialize chips
1122 * @candev: Pointer to candevice/board structure
1123 * @chipnr: Number of the CAN chip on the hardware card
1125 * The function usbcan_init_chip_data() is used to initialize the hardware
1126 * structure containing information about the CAN chips.
1127 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
1129 * The @chip_base_addr entry represents the start of the 'official' memory map
1130 * of the installed chip. It's likely that this is the same as the @io_addr
1131 * argument supplied at module loading time.
1132 * The @clock entry holds the chip clock value in Hz.
1133 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
1134 * register. Options defined in the %sja1000.h file:
1135 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
1136 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
1137 * register. Options defined in the %sja1000.h file:
1138 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
1139 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
1140 * The entry @int_clk_reg holds hardware specific options for the Clock Out
1141 * register. Options defined in the %i82527.h file:
1142 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
1143 * The entry @int_bus_reg holds hardware specific options for the Bus
1144 * Configuration register. Options defined in the %i82527.h file:
1145 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
1146 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
1147 * register. Options defined in the %i82527.h file:
1148 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
1149 * Return Value: The function always returns zero
1150 * File: src/usbcan.c
1152 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
1154 struct canchip_t *chip=candev->chip[chipnr];
1156 chip->chip_type="usbcan";
1157 chip->max_objects=1;
1158 usbcan_chipregister(chip->chipspecops);
1160 CANMSG("initializing usbcan chip operations\n");
1161 chip->chipspecops->chip_config=usbcan_chip_config;
1162 chip->chipspecops->baud_rate=usbcan_baud_rate;
1163 chip->chipspecops->standard_mask=usbcan_standard_mask;
1164 chip->chipspecops->extended_mask=usbcan_extended_mask;
1165 chip->chipspecops->message15_mask=usbcan_extended_mask;
1166 chip->chipspecops->clear_objects=usbcan_clear_objects;
1167 chip->chipspecops->config_irqs=usbcan_config_irqs;
1168 chip->chipspecops->pre_read_config=usbcan_pre_read_config;
1169 chip->chipspecops->pre_write_config=usbcan_pre_write_config;
1170 chip->chipspecops->send_msg=usbcan_send_msg;
1171 chip->chipspecops->check_tx_stat=usbcan_check_tx_stat;
1172 chip->chipspecops->wakeup_tx=usbcan_wakeup_tx;
1173 chip->chipspecops->remote_request=usbcan_remote_request;
1174 chip->chipspecops->enable_configuration=usbcan_enable_configuration;
1175 chip->chipspecops->disable_configuration=usbcan_disable_configuration;
1176 chip->chipspecops->attach_to_chip=usbcan_attach_to_chip;
1177 chip->chipspecops->release_chip=usbcan_release_chip;
1178 chip->chipspecops->set_btregs=usbcan_set_btregs;
1179 chip->chipspecops->start_chip=usbcan_start_chip;
1180 chip->chipspecops->stop_chip=usbcan_stop_chip;
1181 chip->chipspecops->irq_handler=usbcan_irq_handler;
1182 chip->chipspecops->irq_accept=NULL;
1184 candev->chip[chipnr]->chip_base_addr=candev->io_addr;
1185 candev->chip[chipnr]->clock = 16000000;
1186 /* candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
1187 candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
1188 candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
1189 candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
1190 candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
1198 /* --------------------------------------------------------------------------------------------------- */
1201 static void usbcan_irq(struct urb *urb)
1203 struct usb_usbcan *dev = urb->context;
1206 CANMSG("Interrupt poll\n");
1208 switch (urb->status) {
1215 /* this urb is terminated, clean up */
1216 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1219 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1223 dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
1224 CANMSG("Interrupt caught\n");
1227 retval = usb_submit_urb (urb, GFP_ATOMIC);
1229 CANMSG("%s - usb_submit_urb failed with result %d\n",
1230 __FUNCTION__, retval);
1233 static void usbcan_delete(struct usb_usbcan *dev)
1235 usb_put_dev(dev->udev);
1236 usb_kill_urb(dev->irq);
1237 usb_free_urb(dev->irq);
1238 kfree(dev->bulk_in_buffer);
1239 kfree(dev->int_in_buffer);
1241 dev->candev->sysdevptr.anydev=NULL;
1242 cleanup_usbdev(dev->candev);
1247 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1249 struct usb_usbcan *dev;
1250 struct usb_host_interface *iface_desc;
1251 struct usb_endpoint_descriptor *endpoint;
1252 struct candevice_t *candev;
1255 int retval = -ENOMEM;
1257 /* allocate memory for our device state and initialize it */
1258 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1260 err("Out of memory");
1263 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1264 spin_lock_init(&dev->err_lock);
1265 init_usb_anchor(&dev->submitted);
1267 // dev->udev = usb_get_dev(interface_to_usbdev(interface));
1268 dev->udev = interface_to_usbdev(interface);
1269 dev->interface = interface;
1271 /* set up the endpoint information */
1272 /* use only the first bulk-in and bulk-out endpoints */
1273 iface_desc = interface->cur_altsetting;
1274 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1275 endpoint = &iface_desc->endpoint[i].desc;
1277 if (!dev->bulk_in_endpointAddr &&
1278 usb_endpoint_is_bulk_in(endpoint)) {
1279 /* we found a bulk in endpoint */
1280 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1281 dev->bulk_in_size = buffer_size;
1282 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1283 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
1284 if (!dev->bulk_in_buffer) {
1285 err("Could not allocate bulk_in_buffer");
1290 if (!dev->bulk_out_endpointAddr &&
1291 usb_endpoint_is_bulk_out(endpoint)) {
1292 /* we found a bulk out endpoint */
1293 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1296 /* if (!dev->int_in_endpointAddr &&
1297 usb_endpoint_is_int_in(endpoint)) {
1298 // we found an interrupt in endpoint
1299 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1300 dev->int_in_size = buffer_size;
1301 dev->int_in_endpointAddr = endpoint->bEndpointAddress;
1302 dev->int_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
1303 dev->int_in_interval = endpoint->bInterval;
1304 if (!dev->int_in_buffer) {
1305 err("Could not allocate int_in_buffer");
1310 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1311 err("Could not find all bulk-in, bulk-out and interrupt endpoints");
1315 /* save our data pointer in this interface device */
1316 usb_set_intfdata(interface, dev);
1318 register_usbdev("usbcan",(void *) dev);
1320 /* dev->irq = usb_alloc_urb(0, GFP_KERNEL);
1322 CANMSG("Error allocating usb urb\n");
1325 dev->irq->dev = dev->udev;
1326 usb_fill_int_urb(dev->irq, dev->udev,
1327 usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
1328 dev->int_in_buffer, dev->int_in_size,
1329 usbcan_irq, dev, dev->int_in_interval);*/
1330 /* usb_fill_bulk_urb(dev->irq, dev->udev,
1331 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1332 dev->int_in_buffer, dev->int_in_size,
1335 /* dev->irq->transfer_dma = wacom->data_dma;
1336 dev->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;*/
1337 // retval=usb_submit_urb(dev->irq, GFP_KERNEL);
1339 // CANMSG("INT URB %d\n",retval);
1342 // CANMSG("INT URB SUCCCESS\n");
1344 /* let the user know what node this device is now attached to */
1345 info("USB Skeleton device now attached");
1353 static void usbcan_disconnect(struct usb_interface *interface)
1355 struct usb_usbcan *dev;
1356 int minor = interface->minor;
1358 dev = usb_get_intfdata(interface);
1359 usb_set_intfdata(interface, NULL);
1361 /* prevent more I/O from starting */
1362 mutex_lock(&dev->io_mutex);
1363 dev->interface = NULL;
1364 mutex_unlock(&dev->io_mutex);
1366 //usb_kill_anchored_urbs(&dev->submitted);
1370 info("USB Skeleton now disconnected");
1373 static struct usb_driver usbcan_driver = {
1375 .id_table = usbcan_table,
1376 .probe = usbcan_probe,
1377 .disconnect = usbcan_disconnect,
1380 int usbcan_init(void){
1381 return usb_register(&usbcan_driver);
1384 void usbcan_exit(void){
1385 usb_deregister(&usbcan_driver);