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/usbcan.h"
14 * IO_RANGE is the io-memory range that gets reserved, please adjust according
15 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
16 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
18 #define IO_RANGE 0x100
21 * usbcan_request_io: - reserve io or memory range for can board
22 * @candev: pointer to candevice/board which asks for io. Field @io_addr
23 * of @candev is used in most cases to define start of the range
25 * The function usbcan_request_io() is used to reserve the io-memory. If your
26 * hardware uses a dedicated memory range as hardware control registers you
27 * will have to add the code to reserve this memory as well.
28 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
29 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
30 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
31 * Return Value: The function returns zero on success or %-ENODEV on failure
34 int usbcan_request_io(struct candevice_t *candev)
36 if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
37 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
40 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
46 * usbcan_release_io - free reserved io memory range
47 * @candev: pointer to candevice/board which releases io
49 * The function usbcan_release_io() is used to free reserved io-memory.
50 * In case you have reserved more io memory, don't forget to free it here.
51 * IO_RANGE is the io-memory range that gets released, please adjust according
52 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
53 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
54 * Return Value: The function always returns zero
57 int usbcan_release_io(struct candevice_t *candev)
59 can_release_io_region(candev->io_addr,IO_RANGE);
65 * usbcan_reset - hardware reset routine
66 * @candev: Pointer to candevice/board structure
68 * The function usbcan_reset() is used to give a hardware reset. This is
69 * rather hardware specific so I haven't included example code. Don't forget to
70 * check the reset status of the chip before returning.
71 * Return Value: The function returns zero on success or %-ENODEV on failure
74 int usbcan_reset(struct candevice_t *candev)
79 #define RESET_ADDR 0x0
82 * usbcan_init_hw_data - Initialize hardware cards
83 * @candev: Pointer to candevice/board structure
85 * The function usbcan_init_hw_data() is used to initialize the hardware
86 * structure containing information about the installed CAN-board.
87 * %RESET_ADDR represents the io-address of the hardware reset register.
88 * %NR_82527 represents the number of Intel 82527 chips on the board.
89 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
90 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
91 * the hardware uses programmable interrupts.
92 * Return Value: The function always returns zero
95 int usbcan_init_hw_data(struct candevice_t *candev)
97 candev->res_addr=RESET_ADDR;
98 candev->nr_82527_chips=0;
99 candev->nr_sja1000_chips=0;
100 candev->nr_all_chips=1;
101 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
107 * usbcan_init_chip_data - Initialize chips
108 * @candev: Pointer to candevice/board structure
109 * @chipnr: Number of the CAN chip on the hardware card
111 * The function usbcan_init_chip_data() is used to initialize the hardware
112 * structure containing information about the CAN chips.
113 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
115 * The @chip_base_addr entry represents the start of the 'official' memory map
116 * of the installed chip. It's likely that this is the same as the @io_addr
117 * argument supplied at module loading time.
118 * The @clock entry holds the chip clock value in Hz.
119 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
120 * register. Options defined in the %sja1000.h file:
121 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
122 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
123 * register. Options defined in the %sja1000.h file:
124 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
125 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
126 * The entry @int_clk_reg holds hardware specific options for the Clock Out
127 * register. Options defined in the %i82527.h file:
128 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
129 * The entry @int_bus_reg holds hardware specific options for the Bus
130 * Configuration register. Options defined in the %i82527.h file:
131 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
132 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
133 * register. Options defined in the %i82527.h file:
134 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
135 * Return Value: The function always returns zero
138 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
140 canchip_t chip=candev->chip[chipnr];
142 chip->chip_type="usbcan";
144 usbcan_register(chip->chipspecops);
146 CANMSG("initializing usbcan chip operations\n");
147 chipspecops->chip_config=usbcan_chip_config;
148 chipspecops->baud_rate=usbcan_baud_rate;
149 chipspecops->standard_mask=usbcan_standard_mask;
150 chipspecops->extended_mask=usbcan_extended_mask;
151 chipspecops->message15_mask=usbcan_extended_mask;
152 chipspecops->clear_objects=usbcan_clear_objects;
153 chipspecops->config_irqs=usbcan_config_irqs;
154 chipspecops->pre_read_config=usbcan_pre_read_config;
155 chipspecops->pre_write_config=usbcan_pre_write_config;
156 chipspecops->send_msg=usbcan_send_msg;
157 chipspecops->check_tx_stat=usbcan_check_tx_stat;
158 chipspecops->wakeup_tx=usbcan_wakeup_tx;
159 chipspecops->remote_request=usbcan_remote_request;
160 chipspecops->enable_configuration=usbcan_enable_configuration;
161 chipspecops->disable_configuration=usbcan_disable_configuration;
162 chipspecops->attach_to_chip=usbcan_attach_to_chip;
163 chipspecops->release_chip=usbcan_release_chip;
164 chipspecops->set_btregs=usbcan_set_btregs;
165 chipspecops->start_chip=usbcan_start_chip;
166 chipspecops->stop_chip=usbcan_stop_chip;
167 chipspecops->irq_handler=usbcan_irq_handler;
168 chipspecops->irq_accept=NULL;
170 candev->chip[chipnr]->chip_base_addr=candev->io_addr;
171 candev->chip[chipnr]->clock = 16000000;
172 candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
173 candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
174 candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
175 candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
176 candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
183 * usbcan_init_obj_data - Initialize message buffers
184 * @chip: Pointer to chip specific structure
185 * @objnr: Number of the message buffer
187 * The function usbcan_init_obj_data() is used to initialize the hardware
188 * structure containing information about the different message objects on the
189 * CAN chip. In case of the sja1000 there's only one message object but on the
190 * i82527 chip there are 15.
191 * The code below is for a i82527 chip and initializes the object base addresses
192 * The entry @obj_base_addr represents the first memory address of the message
193 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
195 * Unless the hardware uses a segmented memory map, flags can be set zero.
196 * Return Value: The function always returns zero
199 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
201 chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
207 * usbcan_program_irq - program interrupts
208 * @candev: Pointer to candevice/board structure
210 * The function usbcan_program_irq() is used for hardware that uses
211 * programmable interrupts. If your hardware doesn't use programmable interrupts
212 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
213 * leave this function unedited. Again this function is hardware specific so
214 * there's no example code.
215 * Return value: The function returns zero on success or %-ENODEV on failure
218 int usbcan_program_irq(struct candevice_t *candev)
224 * usbcan_write_register - Low level write register routine
225 * @data: data to be written
226 * @address: memory address to write to
228 * The function usbcan_write_register() is used to write to hardware registers
229 * on the CAN chip. You should only have to edit this function if your hardware
230 * uses some specific write process.
231 * Return Value: The function does not return a value
234 void usbcan_write_register(unsigned data, unsigned long address)
240 * usbcan_read_register - Low level read register routine
241 * @address: memory address to read from
243 * The function usbcan_read_register() is used to read from hardware registers
244 * on the CAN chip. You should only have to edit this function if your hardware
245 * uses some specific read process.
246 * Return Value: The function returns the value stored in @address
249 unsigned usbcan_read_register(unsigned long address)
254 /* !!! Don't change this function !!! */
255 int usbcan_register(struct hwspecops_t *hwspecops)
257 hwspecops->request_io = usbcan_request_io;
258 hwspecops->release_io = usbcan_release_io;
259 hwspecops->reset = usbcan_reset;
260 hwspecops->init_hw_data = usbcan_init_hw_data;
261 hwspecops->init_chip_data = usbcan_init_chip_data;
262 hwspecops->init_obj_data = usbcan_init_obj_data;
263 hwspecops->write_register = usbcan_write_register;
264 hwspecops->read_register = usbcan_read_register;
265 hwspecops->program_irq = usbcan_program_irq;
269 static const char *sja1000_ecc_errc_str[]={
273 "other type of error"
276 static const char *sja1000_ecc_seg_str[]={
296 "tolerate dominant bits",
299 "passive error flag",
304 "acknowledge delimiter",
311 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
313 static int sja1000_report_error_limit_counter;
315 static void sja1000_report_error(struct canchip_t *chip,
316 unsigned sr, unsigned ir, unsigned ecc)
318 if(sja1000_report_error_limit_counter>=100)
321 CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
324 sja1000_report_error_limit_counter+=10;
326 if(sja1000_report_error_limit_counter>=100){
327 sja1000_report_error_limit_counter+=10;
328 CANMSG("Error: too many errors, reporting disabled\n");
332 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
333 CANMSG("SR: BS=%c ES=%c TS=%c RS=%c TCS=%c TBS=%c DOS=%c RBS=%c\n",
334 sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
335 sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
336 sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
337 sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
338 CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c TI=%c RI=%c\n",
339 sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
340 sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
341 sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
342 sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
343 if((sr&sjaIR_EI) || 1){
344 CANMSG("EI: %s %s %s\n",
345 sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
346 ecc&sjaECC_DIR?"RX":"TX",
347 sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
350 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
355 * usbcan_enable_configuration - enable chip configuration mode
356 * @chip: pointer to chip state structure
358 int usbcan_enable_configuration(struct canchip_t *chip)
361 enum sja1000_PeliCAN_MOD flags;
363 can_disable_irq(chip->chip_irq);
365 flags=can_read_reg(chip,SJAMOD);
367 while ((!(flags & sjaMOD_RM)) && (i<=10)) {
368 can_write_reg(chip, sjaMOD_RM, SJAMOD);
369 // TODO: configurable sjaMOD_AFM (32/16 bit acceptance filter)
370 // config sjaMOD_LOM (listen only)
373 flags=can_read_reg(chip, SJAMOD);
376 CANMSG("Reset error\n");
377 can_enable_irq(chip->chip_irq);
385 * usbcan_disable_configuration - disable chip configuration mode
386 * @chip: pointer to chip state structure
388 int usbcan_disable_configuration(struct canchip_t *chip)
391 enum sja1000_PeliCAN_MOD flags;
393 flags=can_read_reg(chip,SJAMOD);
395 while ( (flags & sjaMOD_RM) && (i<=50) ) {
396 // could be as long as 11*128 bit times after buss-off
397 can_write_reg(chip, 0, SJAMOD);
398 // TODO: configurable sjaMOD_AFM (32/16 bit acceptance filter)
399 // config sjaMOD_LOM (listen only)
402 flags=can_read_reg(chip, SJAMOD);
405 CANMSG("Error leaving reset status\n");
409 can_enable_irq(chip->chip_irq);
415 * usbcan_chip_config: - can chip configuration
416 * @chip: pointer to chip state structure
418 * This function configures chip and prepares it for message
419 * transmission and reception. The function resets chip,
420 * resets mask for acceptance of all messages by call to
421 * usbcan_extended_mask() function and then
422 * computes and sets baudrate with use of function usbcan_baud_rate().
423 * Return Value: negative value reports error.
426 int usbcan_chip_config(struct canchip_t *chip)
431 if (usbcan_enable_configuration(chip))
434 /* Set mode, clock out, comparator */
435 can_write_reg(chip,sjaCDR_PELICAN|chip->sja_cdr_reg,SJACDR);
437 /* Ensure, that interrupts are disabled even on the chip level now */
438 can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
440 /* Set driver output configuration */
441 can_write_reg(chip,chip->sja_ocr_reg,SJAOCR);
443 /* Simple check for chip presence */
444 for (i=0, n=0x5a; i<8; i++, n+=0xf) {
445 can_write_reg(chip,n,SJAACR0+i);
447 for (i=0, n=0x5a; i<8; i++, n+=0xf) {
448 r = n^can_read_reg(chip,SJAACR0+i);
450 CANMSG("usbcan_chip_config: chip connection broken,"
451 " readback differ 0x%02x\n", r);
457 if (usbcan_extended_mask(chip,0x00000000, 0xffffffff))
461 chip->baudrate=1000000;
462 if (usbcan_baud_rate(chip,chip->baudrate,chip->clock,0,75,0))
465 /* Enable hardware interrupts */
466 can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER);
468 usbcan_disable_configuration(chip);
474 * usbcan_extended_mask: - setup of extended mask for message filtering
475 * @chip: pointer to chip state structure
476 * @code: can message acceptance code
477 * @mask: can message acceptance mask
479 * Return Value: negative value reports error.
482 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask)
486 if (usbcan_enable_configuration(chip))
489 // LSB to +3, MSB to +0
490 for(i=SJA_PeliCAN_AC_LEN; --i>=0;) {
491 can_write_reg(chip,code&0xff,SJAACR0+i);
492 can_write_reg(chip,mask&0xff,SJAAMR0+i);
497 DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
498 DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
500 usbcan_disable_configuration(chip);
506 * usbcan_baud_rate: - set communication parameters.
507 * @chip: pointer to chip state structure
508 * @rate: baud rate in Hz
509 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
510 * @sjw: synchronization jump width (0-3) prescaled clock cycles
511 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
512 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
514 * Return Value: negative value reports error.
517 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
518 int sampl_pt, int flags)
520 int best_error = 1000000000, error;
521 int best_tseg=0, best_brp=0, best_rate=0, brp=0;
522 int tseg=0, tseg1=0, tseg2=0;
524 if (usbcan_enable_configuration(chip))
529 /* tseg even = round down, odd = round up */
530 for (tseg=(0+0+2)*2; tseg<=(sjaMAX_TSEG2+sjaMAX_TSEG1+2)*2+1; tseg++) {
531 brp = clock/((1+tseg/2)*rate)+tseg%2;
532 if (brp == 0 || brp > 64)
534 error = rate - clock/(brp*(1+tseg/2));
537 if (error <= best_error) {
541 best_rate = clock/(brp*(1+tseg/2));
544 if (best_error && (rate/best_error < 10)) {
545 CANMSG("baud rate %d is not possible with %d Hz clock\n",
547 CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
548 best_rate, best_brp, best_tseg, tseg1, tseg2);
551 tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
554 if (tseg2 > sjaMAX_TSEG2)
555 tseg2 = sjaMAX_TSEG2;
556 tseg1 = best_tseg-tseg2-2;
557 if (tseg1>sjaMAX_TSEG1) {
558 tseg1 = sjaMAX_TSEG1;
559 tseg2 = best_tseg-tseg1-2;
562 DEBUGMSG("Setting %d bps.\n", best_rate);
563 DEBUGMSG("brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d, sampl_pt=%d\n",
564 best_brp, best_tseg, tseg1, tseg2,
565 (100*(best_tseg-tseg2)/(best_tseg+1)));
568 can_write_reg(chip, sjw<<6 | best_brp, SJABTR0);
569 can_write_reg(chip, ((flags & BTR1_SAM) != 0)<<7 | (tseg2<<4)
572 usbcan_disable_configuration(chip);
578 * usbcan_read: - reads and distributes one or more received messages
579 * @chip: pointer to chip state structure
580 * @obj: pinter to CAN message queue information
584 void usbcan_read(struct canchip_t *chip, struct msgobj_t *obj) {
585 int i, flags, len, datastart;
587 flags = can_read_reg(chip,SJAFRM);
588 if(flags&sjaFRM_FF) {
590 (can_read_reg(chip,SJAID0)<<21) +
591 (can_read_reg(chip,SJAID1)<<13) +
592 (can_read_reg(chip,SJAID2)<<5) +
593 (can_read_reg(chip,SJAID3)>>3);
597 (can_read_reg(chip,SJAID0)<<3) +
598 (can_read_reg(chip,SJAID1)>>5);
602 ((flags & sjaFRM_RTR) ? MSG_RTR : 0) |
603 ((flags & sjaFRM_FF) ? MSG_EXT : 0);
604 len = flags & sjaFRM_DLC_M;
605 obj->rx_msg.length = len;
606 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
607 for(i=0; i< len; i++) {
608 obj->rx_msg.data[i]=can_read_reg(chip,datastart+i);
611 /* fill CAN message timestamp */
612 can_filltimestamp(&obj->rx_msg.timestamp);
614 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
616 can_write_reg(chip, sjaCMR_RRB, SJACMR);
618 } while (can_read_reg(chip, SJASR) & sjaSR_RBS);
622 * usbcan_pre_read_config: - prepares message object for message reception
623 * @chip: pointer to chip state structure
624 * @obj: pointer to message object state structure
626 * Return Value: negative value reports error.
627 * Positive value indicates immediate reception of message.
630 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
633 status=can_read_reg(chip,SJASR);
635 if(status & sjaSR_BS) {
636 /* Try to recover from error condition */
637 DEBUGMSG("usbcan_pre_read_config bus-off recover 0x%x\n",status);
638 usbcan_enable_configuration(chip);
639 can_write_reg(chip, 0, SJARXERR);
640 can_write_reg(chip, 0, SJATXERR1);
641 can_read_reg(chip, SJAECC);
642 usbcan_disable_configuration(chip);
645 if (!(status&sjaSR_RBS)) {
649 can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER); //disable interrupts for a moment
650 usbcan_read(chip, obj);
651 can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER); //enable interrupts
655 #define MAX_TRANSMIT_WAIT_LOOPS 10
657 * usbcan_pre_write_config: - prepares message object for message transmission
658 * @chip: pointer to chip state structure
659 * @obj: pointer to message object state structure
660 * @msg: pointer to CAN message
662 * This function prepares selected message object for future initiation
663 * of message transmission by usbcan_send_msg() function.
664 * The CAN message data and message ID are transfered from @msg slot
665 * into chip buffer in this function.
666 * Return Value: negative value reports error.
669 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
670 struct canmsg_t *msg)
677 /* Wait until Transmit Buffer Status is released */
678 while ( !((status=can_read_reg(chip, SJASR)) & sjaSR_TBS) &&
679 i++<MAX_TRANSMIT_WAIT_LOOPS) {
683 if(status & sjaSR_BS) {
684 /* Try to recover from error condition */
685 DEBUGMSG("usbcan_pre_write_config bus-off recover 0x%x\n",status);
686 usbcan_enable_configuration(chip);
687 can_write_reg(chip, 0, SJARXERR);
688 can_write_reg(chip, 0, SJATXERR1);
689 can_read_reg(chip, SJAECC);
690 usbcan_disable_configuration(chip);
692 if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
693 CANMSG("Transmit timed out, cancelling\n");
694 // here we should check if there is no write/select waiting for this
695 // transmit. If so, set error ret and wake up.
696 // CHECKME: if we do not disable sjaIER_TIE (TX IRQ) here we get interrupt
698 can_write_reg(chip, sjaCMR_AT, SJACMR);
700 while ( !(can_read_reg(chip, SJASR) & sjaSR_TBS) &&
701 i++<MAX_TRANSMIT_WAIT_LOOPS) {
704 if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
705 CANMSG("Could not cancel, please reset\n");
710 if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
711 /* len &= sjaFRM_DLC_M; ensured by above condition already */
712 can_write_reg(chip, ((msg->flags&MSG_EXT)?sjaFRM_FF:0) |
713 ((msg->flags & MSG_RTR) ? sjaFRM_RTR : 0) | len, SJAFRM);
714 if(msg->flags&MSG_EXT) {
716 can_write_reg(chip, id & 0xff, SJAID3);
718 can_write_reg(chip, id & 0xff, SJAID2);
720 can_write_reg(chip, id & 0xff, SJAID1);
722 can_write_reg(chip, id, SJAID0);
723 for(i=0; i < len; i++) {
724 can_write_reg(chip, msg->data[i], SJADATE+i);
728 can_write_reg(chip, (id >> 8) & 0xff, SJAID0);
729 can_write_reg(chip, id & 0xff, SJAID1);
730 for(i=0; i < len; i++) {
731 can_write_reg(chip, msg->data[i], SJADATS+i);
738 * usbcan_send_msg: - initiate message transmission
739 * @chip: pointer to chip state structure
740 * @obj: pointer to message object state structure
741 * @msg: pointer to CAN message
743 * This function is called after usbcan_pre_write_config() function,
744 * which prepares data in chip buffer.
745 * Return Value: negative value reports error.
748 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
749 struct canmsg_t *msg)
751 can_write_reg(chip, sjaCMR_TR, SJACMR);
757 * usbcan_check_tx_stat: - checks state of transmission engine
758 * @chip: pointer to chip state structure
760 * Return Value: negative value reports error.
761 * Positive return value indicates transmission under way status.
762 * Zero value indicates finishing of all issued transmission requests.
765 int usbcan_check_tx_stat(struct canchip_t *chip)
767 if (can_read_reg(chip,SJASR) & sjaSR_TCS)
774 * usbcan_set_btregs: - configures bitrate registers
775 * @chip: pointer to chip state structure
776 * @btr0: bitrate register 0
777 * @btr1: bitrate register 1
779 * Return Value: negative value reports error.
782 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
785 if (usbcan_enable_configuration(chip))
788 can_write_reg(chip, btr0, SJABTR0);
789 can_write_reg(chip, btr1, SJABTR1);
791 usbcan_disable_configuration(chip);
797 * usbcan_start_chip: - starts chip message processing
798 * @chip: pointer to chip state structure
800 * Return Value: negative value reports error.
803 int usbcan_start_chip(struct canchip_t *chip)
805 enum sja1000_PeliCAN_MOD flags;
807 flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
808 can_write_reg(chip, flags, SJAMOD);
810 sja1000_report_error_limit_counter=0;
816 * usbcan_stop_chip: - stops chip message processing
817 * @chip: pointer to chip state structure
819 * Return Value: negative value reports error.
822 int usbcan_stop_chip(struct canchip_t *chip)
824 enum sja1000_PeliCAN_MOD flags;
826 flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
827 can_write_reg(chip, flags|sjaMOD_RM, SJAMOD);
833 * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
834 * @chip: pointer to chip state structure
836 * Return Value: negative value reports error.
839 int usbcan_attach_to_chip(struct canchip_t *chip)
845 * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
846 * @chip: pointer to chip state structure
848 * Return Value: negative value reports error.
851 int usbcan_release_chip(struct canchip_t *chip)
853 usbcan_stop_chip(chip);
854 can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
860 * usbcan_remote_request: - configures message object and asks for RTR message
861 * @chip: pointer to chip state structure
862 * @obj: pointer to message object structure
864 * Return Value: negative value reports error.
867 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
869 CANMSG("usbcan_remote_request not implemented\n");
874 * usbcan_standard_mask: - setup of mask for message filtering
875 * @chip: pointer to chip state structure
876 * @code: can message acceptance code
877 * @mask: can message acceptance mask
879 * Return Value: negative value reports error.
882 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
885 CANMSG("usbcan_standard_mask not implemented\n");
890 * usbcan_clear_objects: - clears state of all message object residing in chip
891 * @chip: pointer to chip state structure
893 * Return Value: negative value reports error.
896 int usbcan_clear_objects(struct canchip_t *chip)
898 CANMSG("usbcan_clear_objects not implemented\n");
903 * usbcan_config_irqs: - tunes chip hardware interrupt delivery
904 * @chip: pointer to chip state structure
905 * @irqs: requested chip IRQ configuration
907 * Return Value: negative value reports error.
910 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
912 CANMSG("usbcan_config_irqs not implemented\n");
917 * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
918 * @chip: pointer to chip state structure
919 * @obj: pointer to attached queue description
921 * The main purpose of this function is to read message from attached queues
922 * and transfer message contents into CAN controller chip.
923 * This subroutine is called by
924 * usbcan_irq_write_handler() for transmit events.
927 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
932 /* Do local transmitted message distribution if enabled */
934 /* fill CAN message timestamp */
935 can_filltimestamp(&obj->tx_slot->msg.timestamp);
937 obj->tx_slot->msg.flags |= MSG_LOCAL;
938 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
940 /* Free transmitted slot */
941 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
945 can_msgobj_clear_fl(obj,TX_PENDING);
946 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
949 can_msgobj_set_fl(obj,TX_PENDING);
951 if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
953 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
954 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
958 if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
960 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
961 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
971 * usbcan_irq_handler: - interrupt service routine
972 * @irq: interrupt vector number, this value is system specific
973 * @chip: pointer to chip state structure
975 * Interrupt handler is activated when state of CAN controller chip changes,
976 * there is message to be read or there is more space for new messages or
977 * error occurs. The receive events results in reading of the message from
978 * CAN controller chip and distribution of message through attached
982 int usbcan_irq_handler(int irq, struct canchip_t *chip)
984 int irq_register, status, error_code;
985 struct msgobj_t *obj=chip->msgobj[0];
986 int loop_cnt=CHIP_MAX_IRQLOOP;
988 irq_register=can_read_reg(chip,SJAIR);
989 // DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
990 // DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
991 // can_read_reg(chip,SJASR));
993 if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
994 return CANCHIP_IRQ_NONE;
996 if(!(chip->flags&CHIP_CONFIGURED)) {
997 CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
998 return CANCHIP_IRQ_NONE;
1001 status=can_read_reg(chip,SJASR);
1006 CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
1007 return CANCHIP_IRQ_STUCK;
1010 /* (irq_register & sjaIR_RI) */
1011 /* old variant using SJAIR, collides with intended use with irq_accept */
1012 if (status & sjaSR_RBS) {
1013 DEBUGMSG("sja1000_irq_handler: RI or RBS\n");
1014 usbcan_read(chip,obj);
1018 /* (irq_register & sjaIR_TI) */
1019 /* old variant using SJAIR, collides with intended use with irq_accept */
1020 if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
1021 (can_msgobj_test_fl(obj,TX_REQUEST))) {
1022 DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
1024 can_msgobj_set_fl(obj,TX_REQUEST);
1025 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
1026 can_msgobj_clear_fl(obj,TX_REQUEST);
1028 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
1029 usbcan_irq_write_handler(chip, obj);
1031 can_msgobj_clear_fl(obj,TX_LOCK);
1032 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
1033 DEBUGMSG("TX looping in sja1000_irq_handler\n");
1036 if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
1037 // Some error happened
1038 error_code=can_read_reg(chip,SJAECC);
1039 sja1000_report_error(chip, status, irq_register, error_code);
1040 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
1041 // Reset flag set to 0 if chip is already off the bus. Full state report
1044 if(error_code == 0xd9) {
1046 /* no such device or address - no ACK received */
1048 if(obj->tx_retry_cnt++>MAX_RETR) {
1049 can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
1050 obj->tx_retry_cnt = 0;
1052 if(status&sjaSR_BS) {
1053 CANMSG("bus-off, resetting usbcan\n");
1054 can_write_reg(chip, 0, SJAMOD);
1058 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
1059 /*canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
1060 obj->tx_slot=NULL;*/
1064 if(sja1000_report_error_limit_counter)
1065 sja1000_report_error_limit_counter--;
1066 obj->tx_retry_cnt=0;
1069 irq_register=can_read_reg(chip,SJAIR);
1071 status=can_read_reg(chip,SJASR);
1073 if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
1074 (irq_register & sjaIR_TI))
1075 can_msgobj_set_fl(obj,TX_REQUEST);
1077 } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
1078 (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
1079 (status & sjaSR_RBS));
1081 return CANCHIP_IRQ_HANDLED;
1085 * usbcan_wakeup_tx: - wakeups TX processing
1086 * @chip: pointer to chip state structure
1087 * @obj: pointer to message object structure
1089 * Function is responsible for initiating message transmition.
1090 * It is responsible for clearing of object TX_REQUEST flag
1092 * Return Value: negative value reports error.
1093 * File: src/usbcan.c
1095 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
1098 can_preempt_disable();
1100 can_msgobj_set_fl(obj,TX_PENDING);
1101 can_msgobj_set_fl(obj,TX_REQUEST);
1102 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
1103 can_msgobj_clear_fl(obj,TX_REQUEST);
1105 if (can_read_reg(chip, SJASR) & sjaSR_TBS){
1106 obj->tx_retry_cnt=0;
1107 usbcan_irq_write_handler(chip, obj);
1110 can_msgobj_clear_fl(obj,TX_LOCK);
1111 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
1112 DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
1115 can_preempt_enable();
1119 int usbcan_register(struct chipspecops_t *chipspecops)
1125 * usbcan_fill_chipspecops - fills chip specific operations
1126 * @chip: pointer to chip representation structure
1128 * The function fills chip specific operations for sja1000 (PeliCAN) chip.
1130 * Return Value: returns negative number in the case of fail
1132 int usbcan_fill_chipspecops(struct canchip_t *chip)
1139 /* --------------------------------------------------------------------------------------------------- */
1142 static void ul_usb1_irq(struct urb *urb)
1144 struct usb_ul_usb1 *dev = urb->context;
1145 struct ul_usb1_combo devc;
1148 CANMSG("Interrupt poll\n");
1150 switch (urb->status) {
1157 /* this urb is terminated, clean up */
1158 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1161 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1168 dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
1169 CANMSG("Interrupt caught\n");
1172 retval = usb_submit_urb (urb, GFP_ATOMIC);
1174 CANMSG("%s - usb_submit_urb failed with result %d\n",
1175 __FUNCTION__, retval);
1178 static void ul_usb1_delete(struct usb_ul_usb1 *dev)
1180 usb_put_dev(dev->udev);
1181 usb_kill_urb(dev->irq);
1182 usb_free_urb(dev->irq);
1183 kfree(dev->bulk_in_buffer);
1184 kfree(dev->int_in_buffer);
1186 dev->candev->sysdevptr.anydev=NULL;
1187 cleanup_usbdev(dev->candev);
1192 static int ul_usb1_probe(struct usb_interface *interface, const struct usb_device_id *id)
1194 struct usb_ul_usb1 *dev;
1195 struct usb_host_interface *iface_desc;
1196 struct usb_endpoint_descriptor *endpoint;
1197 struct candevice_t *candev;
1200 int retval = -ENOMEM;
1202 /* allocate memory for our device state and initialize it */
1203 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1205 err("Out of memory");
1208 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1209 mutex_init(&dev->io_mutex);
1210 spin_lock_init(&dev->err_lock);
1211 init_usb_anchor(&dev->submitted);
1213 // dev->udev = usb_get_dev(interface_to_usbdev(interface));
1214 dev->udev = interface_to_usbdev(interface);
1215 dev->interface = interface;
1217 /* set up the endpoint information */
1218 /* use only the first bulk-in and bulk-out endpoints */
1219 iface_desc = interface->cur_altsetting;
1220 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1221 endpoint = &iface_desc->endpoint[i].desc;
1223 if (!dev->bulk_in_endpointAddr &&
1224 usb_endpoint_is_bulk_in(endpoint)) {
1225 /* we found a bulk in endpoint */
1226 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1227 dev->bulk_in_size = buffer_size;
1228 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1229 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
1230 if (!dev->bulk_in_buffer) {
1231 err("Could not allocate bulk_in_buffer");
1236 if (!dev->bulk_out_endpointAddr &&
1237 usb_endpoint_is_bulk_out(endpoint)) {
1238 /* we found a bulk out endpoint */
1239 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1242 if (!dev->int_in_endpointAddr &&
1243 usb_endpoint_is_int_in(endpoint)) {
1244 /* we found an interrupt in endpoint */
1245 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1246 dev->int_in_size = buffer_size;
1247 dev->int_in_endpointAddr = endpoint->bEndpointAddress;
1248 dev->int_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
1249 dev->int_in_interval = endpoint->bInterval;
1250 if (!dev->int_in_buffer) {
1251 err("Could not allocate int_in_buffer");
1256 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr && dev->int_in_endpointAddr)) {
1257 err("Could not find all bulk-in, bulk-out and interrupt endpoints");
1261 /* save our data pointer in this interface device */
1262 usb_set_intfdata(interface, dev);
1264 if (main_init_done==1)
1265 register_usbdev("ul_usb1",(void *) dev);
1267 mutex_lock(&usbdev_reg_mutex);
1268 if (main_init_done==1)
1269 register_usbdev("ul_usb1",(void *) dev);
1271 for (i=0;i<MAX_HW_CARDS;i++){
1272 if (usbregq[i]==NULL){
1273 usbregq[i]=(struct usbdev_reg_query *)can_checked_malloc(sizeof(struct usbdev_reg_query));
1275 CANMSG("Error allocating usbdev_reg_query");
1276 mutex_unlock(&usbdev_reg_mutex);
1279 sprintf (usbregq[i]->hwname,"ul_usb1");
1280 usbregq[i]->anydev=(void *) dev;
1284 if (i==MAX_HW_CARDS){
1285 CANMSG("No free space to register new card");
1286 mutex_unlock(&usbdev_reg_mutex);
1290 mutex_unlock(&usbdev_reg_mutex);
1293 dev->irq = usb_alloc_urb(0, GFP_KERNEL);
1295 CANMSG("Error allocating usb urb\n");
1298 dev->irq->dev = dev->udev;
1299 usb_fill_int_urb(dev->irq, dev->udev,
1300 usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
1301 dev->int_in_buffer, dev->int_in_size,
1302 ul_usb1_irq, dev, dev->int_in_interval);
1303 /* usb_fill_bulk_urb(dev->irq, dev->udev,
1304 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1305 dev->int_in_buffer, dev->int_in_size,
1306 ul_usb1_irq, dev);*/
1308 /* dev->irq->transfer_dma = wacom->data_dma;
1309 dev->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;*/
1310 retval=usb_submit_urb(dev->irq, GFP_KERNEL);
1312 CANMSG("INT URB %d\n",retval);
1315 CANMSG("INT URB SUCCCESS\n");
1317 /* let the user know what node this device is now attached to */
1318 info("USB Skeleton device now attached");
1322 ul_usb1_delete(dev);
1326 static void ul_usb1_disconnect(struct usb_interface *interface)
1328 struct usb_ul_usb1 *dev;
1329 int minor = interface->minor;
1331 dev = usb_get_intfdata(interface);
1332 usb_set_intfdata(interface, NULL);
1334 /* prevent more I/O from starting */
1335 mutex_lock(&dev->io_mutex);
1336 dev->interface = NULL;
1337 mutex_unlock(&dev->io_mutex);
1339 //usb_kill_anchored_urbs(&dev->submitted);
1341 ul_usb1_delete(dev);
1343 info("USB Skeleton now disconnected");
1346 static struct usb_driver ul_usb1_driver = {
1347 .name = "ul_usb1-can",
1348 .id_table = ul_usb1_table,
1349 .probe = ul_usb1_probe,
1350 .disconnect = ul_usb1_disconnect,
1351 .id_table = ul_usb1_table,
1354 int ul_usb1_init(void){
1355 return usb_register(&ul_usb1_driver);
1358 void ul_usb1_exit(void){
1359 usb_deregister(&ul_usb1_driver);