]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/usbcan.c
minor changes in the code in order to be compilable
[lincan.git] / lincan / src / usbcan.c
1 /* usbcan.h
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
6  */
7
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"
14
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 */
22
23 /* Define these values to match your devices */
24 #define USB_SKEL_VENDOR_ID      0xDEAD
25 #define USB_SKEL_PRODUCT_ID     0x1001
26
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 */
31 };
32 MODULE_DEVICE_TABLE(usb, usbcan_table);
33
34 extern struct file_operations can_fops;
35
36 int usbcan_register(struct hwspecops_t *hwspecops);
37
38 /*
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.
42  */
43 #define IO_RANGE 0x100
44
45 /* Structure to hold all of our device specific stuff */
46 struct usb_usbcan {
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 */
58         int                             int_in_interval;
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 */
63         struct urb              *irq;
64         struct candevice_t              *candev;
65 };
66
67 static struct usb_driver usbcan_driver;
68
69 /**
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
73  *
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
81  * File: src/usbcan.c
82  */
83 int usbcan_request_io(struct candevice_t *candev)
84 {
85         ((struct usb_ul_usb1*)candev->sysdevptr.anydev)->candev=candev;
86         return 0;
87 }
88
89 /**
90  * usbcan_release_io - free reserved io memory range
91  * @candev: pointer to candevice/board which releases io
92  *
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
99  * File: src/usbcan.c
100  */
101 int usbcan_release_io(struct candevice_t *candev)
102 {
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);
111                 if (dev->candev){
112                         dev->candev->sysdevptr.anydev=NULL;
113                         //cleanup_usbdev(dev->candev);
114                 }
115                 kfree(dev);
116         }
117
118         return 0;
119 }
120
121 /**
122  * usbcan_reset - hardware reset routine
123  * @candev: Pointer to candevice/board structure
124  *
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
129  * File: src/usbcan.c
130  */
131 int usbcan_reset(struct candevice_t *candev)
132 {
133         return 0;
134 }
135
136 #define RESET_ADDR 0x0
137
138 /**
139  * usbcan_init_hw_data - Initialize hardware cards
140  * @candev: Pointer to candevice/board structure
141  *
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
150  * File: src/usbcan.c
151  */
152 int usbcan_init_hw_data(struct candevice_t *candev)
153 {
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;
159
160         return 0;
161 }
162
163 /**
164  * usbcan_init_obj_data - Initialize message buffers
165  * @chip: Pointer to chip specific structure
166  * @objnr: Number of the message buffer
167  *
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
175  * base address.
176  * Unless the hardware uses a segmented memory map, flags can be set zero.
177  * Return Value: The function always returns zero
178  * File: src/usbcan.c
179  */
180 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
181 {
182         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
183
184         return 0;
185 }
186
187 /**
188  * usbcan_program_irq - program interrupts
189  * @candev: Pointer to candevice/board structure
190  *
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
197  * File: src/usbcan.c
198  */
199 int usbcan_program_irq(struct candevice_t *candev)
200 {
201         return 0;
202 }
203
204 /**
205  * usbcan_write_register - Low level write register routine
206  * @data: data to be written
207  * @address: memory address to write to
208  *
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
213  * File: src/usbcan.c
214  */
215 void usbcan_write_register(unsigned data, unsigned long address)
216 {
217         outb(data,address);
218 }
219
220 /**
221  * usbcan_read_register - Low level read register routine
222  * @address: memory address to read from
223  *
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
228  * File: src/usbcan.c
229  */
230 unsigned usbcan_read_register(unsigned long address)
231 {
232         return inb(address);
233 }
234
235 /* !!! Don't change this function !!! */
236 int usbcan_register(struct hwspecops_t *hwspecops)
237 {
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;
247         return 0;
248 }
249
250 #ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
251
252 static const char *sja1000_ecc_errc_str[]={
253         "bit error",
254         "form error",
255         "stuff error",
256         "other type of error"
257 };
258
259 static const char *sja1000_ecc_seg_str[]={
260         "?0?",
261         "?1?",
262         "ID.28 to ID.21",
263         "start of frame",
264         "bit SRTR",
265         "bit IDE",
266         "ID.20 to ID.18",
267         "ID.17 to ID.13",
268         "CRC sequence",
269         "reserved bit 0",
270         "data field",
271         "data length code",
272         "bit RTR",
273         "reserved bit 1",
274         "ID.4 to ID.0",
275         "ID.12 to ID.5",
276         "?16?"
277         "active error flag",
278         "intermission",
279         "tolerate dominant bits",
280         "?20?",
281         "?21?",
282         "passive error flag",
283         "error delimiter",
284         "CRC delimiter",
285         "acknowledge slot",
286         "end of frame",
287         "acknowledge delimiter",
288         "overload flag",
289         "?29?",
290         "?30?",
291         "?31?"
292 };
293
294 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
295
296 static int sja1000_report_error_limit_counter;
297
298 static void sja1000_report_error(struct canchip_t *chip,
299                                 unsigned sr, unsigned ir, unsigned ecc)
300 {
301 /*      if(sja1000_report_error_limit_counter>=100)
302                 return;
303
304         CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
305                 sr, ir, ecc);
306
307         sja1000_report_error_limit_counter+=10;
308
309         if(sja1000_report_error_limit_counter>=100){
310                 sja1000_report_error_limit_counter+=10;
311                 CANMSG("Error: too many errors, reporting disabled\n");
312                 return;
313         }
314
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]
331                       );
332         }
333 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
334 }
335
336
337 /**
338  * usbcan_enable_configuration - enable chip configuration mode
339  * @chip: pointer to chip state structure
340  */
341 int usbcan_enable_configuration(struct canchip_t *chip)
342 {
343 /*      int i=0;
344         enum sja1000_PeliCAN_MOD flags;
345
346         can_disable_irq(chip->chip_irq);
347
348         flags=can_read_reg(chip,SJAMOD);
349
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)
354                 udelay(100);
355                 i++;
356                 flags=can_read_reg(chip, SJAMOD);
357         }
358         if (i>=10) {
359                 CANMSG("Reset error\n");
360                 can_enable_irq(chip->chip_irq);
361                 return -ENODEV;
362         }
363 */
364         return 0;
365 }
366
367 /**
368  * usbcan_disable_configuration - disable chip configuration mode
369  * @chip: pointer to chip state structure
370  */
371 int usbcan_disable_configuration(struct canchip_t *chip)
372 {
373 /*      int i=0;
374         enum sja1000_PeliCAN_MOD flags;
375
376         flags=can_read_reg(chip,SJAMOD);
377
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)
383                 udelay(100);
384                 i++;
385                 flags=can_read_reg(chip, SJAMOD);
386         }
387         if (i>=10) {
388                 CANMSG("Error leaving reset status\n");
389                 return -ENODEV;
390         }
391
392         can_enable_irq(chip->chip_irq);
393 */
394         return 0;
395 }
396
397 /**
398  * usbcan_chip_config: - can chip configuration
399  * @chip: pointer to chip state structure
400  *
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.
407  * File: src/usbcan.c
408  */
409 int usbcan_chip_config(struct canchip_t *chip)
410 {
411 /*      int i;
412         unsigned char n, r;
413
414         if (usbcan_enable_configuration(chip))
415                 return -ENODEV;
416
417         // Set mode, clock out, comparator
418         can_write_reg(chip,sjaCDR_PELICAN|chip->sja_cdr_reg,SJACDR);
419
420         // Ensure, that interrupts are disabled even on the chip level now
421         can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
422
423         // Set driver output configuration
424         can_write_reg(chip,chip->sja_ocr_reg,SJAOCR);
425
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);
429         }
430         for (i=0, n=0x5a; i<8; i++, n+=0xf) {
431                 r = n^can_read_reg(chip,SJAACR0+i);
432                 if (r) {
433                         CANMSG("usbcan_chip_config: chip connection broken,"
434                                 " readback differ 0x%02x\n", r);
435                         return -ENODEV;
436                 }
437         }
438
439
440         if (usbcan_extended_mask(chip,0x00000000, 0xffffffff))
441                 return -ENODEV;
442
443         if (!chip->baudrate)
444                 chip->baudrate=1000000;
445         if (usbcan_baud_rate(chip,chip->baudrate,chip->clock,0,75,0))
446                 return -ENODEV;
447
448         // Enable hardware interrupts
449         can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER);
450
451         usbcan_disable_configuration(chip);
452 */
453         return 0;
454 }
455
456 /**
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
461  *
462  * Return Value: negative value reports error.
463  * File: src/usbcan.c
464  */
465 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  long mask)
466 {
467 /*      int i;
468
469         if (usbcan_enable_configuration(chip))
470                 return -ENODEV;
471
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);
476                 code >>= 8;
477                 mask >>= 8;
478         }
479
480         DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
481         DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
482
483         usbcan_disable_configuration(chip);
484 */
485         return 0;
486 }
487
488 /**
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
496  *
497  * Return Value: negative value reports error.
498  * File: src/usbcan.c
499  */
500 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
501                                                         int sampl_pt, int flags)
502 {
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;
506 //
507 //      if (usbcan_enable_configuration(chip))
508 //              return -ENODEV;
509 //
510 //      clock /=2;
511 //
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)
516 //                      continue;
517 //              error = rate - clock/(brp*(1+tseg/2));
518 //              if (error < 0)
519 //                      error = -error;
520 //              if (error <= best_error) {
521 //                      best_error = error;
522 //                      best_tseg = tseg/2;
523 //                      best_brp = brp-1;
524 //                      best_rate = clock/(brp*(1+tseg/2));
525 //              }
526 //      }
527 //      if (best_error && (rate/best_error < 10)) {
528 //              CANMSG("baud rate %d is not possible with %d Hz clock\n",
529 //                                                              rate, 2*clock);
530 //              CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
531 //                              best_rate, best_brp, best_tseg, tseg1, tseg2);
532 //              return -EINVAL;
533 //      }
534 //      tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
535 //      if (tseg2 < 0)
536 //              tseg2 = 0;
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;
543 //      }
544 //
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)));
549 //
550 //
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);
554 //
555 //      usbcan_disable_configuration(chip);
556
557         return 0;
558 }
559
560 /**
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
564  *
565  * File: src/usbcan.c
566  */
567 void usbcan_read(struct canchip_t *chip, struct msgobj_t *obj) {
568 /*      int i, flags, len, datastart;
569         do {
570                 flags = can_read_reg(chip,SJAFRM);
571                 if(flags&sjaFRM_FF) {
572                         obj->rx_msg.id =
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);
577                         datastart = SJADATE;
578                 } else {
579                         obj->rx_msg.id =
580                                 (can_read_reg(chip,SJAID0)<<3) +
581                                 (can_read_reg(chip,SJAID1)>>5);
582                         datastart = SJADATS;
583                 }
584                 obj->rx_msg.flags =
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);
592                 }
593
594                 // fill CAN message timestamp
595                 can_filltimestamp(&obj->rx_msg.timestamp);
596
597                 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
598
599                 can_write_reg(chip, sjaCMR_RRB, SJACMR);
600
601         } while (can_read_reg(chip, SJASR) & sjaSR_RBS);*/
602 }
603
604 /**
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
608  *
609  * Return Value: negative value reports error.
610  *      Positive value indicates immediate reception of message.
611  * File: src/usbcan.c
612  */
613 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
614 {
615 /*      int status;
616         status=can_read_reg(chip,SJASR);
617
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);
626         }
627
628         if (!(status&sjaSR_RBS)) {
629                 return 0;
630         }
631
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*/
635         return 1;
636 }
637
638 #define MAX_TRANSMIT_WAIT_LOOPS 10
639 /**
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
644  *
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.
650  * File: src/usbcan.c
651  */
652 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
653                                                         struct canmsg_t *msg)
654 {
655 /*      int i=0;
656         unsigned int id;
657         int status;
658         int len;
659
660         // Wait until Transmit Buffer Status is released
661         while ( !((status=can_read_reg(chip, SJASR)) & sjaSR_TBS) &&
662                                                 i++<MAX_TRANSMIT_WAIT_LOOPS) {
663                 udelay(i);
664         }
665
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);
674         }
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
680 // immediately
681                 can_write_reg(chip, sjaCMR_AT, SJACMR);
682                 i=0;
683                 while ( !(can_read_reg(chip, SJASR) & sjaSR_TBS) &&
684                                                 i++<MAX_TRANSMIT_WAIT_LOOPS) {
685                         udelay(i);
686                 }
687                 if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
688                         CANMSG("Could not cancel, please reset\n");
689                         return -EIO;
690                 }
691         }
692         len = msg->length;
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) {
698                 id=msg->id<<3;
699                 can_write_reg(chip, id & 0xff, SJAID3);
700                 id >>= 8;
701                 can_write_reg(chip, id & 0xff, SJAID2);
702                 id >>= 8;
703                 can_write_reg(chip, id & 0xff, SJAID1);
704                 id >>= 8;
705                 can_write_reg(chip, id, SJAID0);
706                 for(i=0; i < len; i++) {
707                         can_write_reg(chip, msg->data[i], SJADATE+i);
708                 }
709         } else {
710                 id=msg->id<<5;
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);
715                 }
716         }*/
717         return 0;
718 }
719
720 /**
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
725  *
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.
729  * File: src/usbcan.c
730  */
731 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
732                                                         struct canmsg_t *msg)
733 {
734 /*      can_write_reg(chip, sjaCMR_TR, SJACMR);
735 */
736         return 0;
737 }
738
739 /**
740  * usbcan_check_tx_stat: - checks state of transmission engine
741  * @chip: pointer to chip state structure
742  *
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.
746  * File: src/usbcan.c
747  */
748 int usbcan_check_tx_stat(struct canchip_t *chip)
749 {
750 //      if (can_read_reg(chip,SJASR) & sjaSR_TCS)
751                 return 0;
752 //      else
753 //              return 1;
754 }
755
756 /**
757  * usbcan_set_btregs: -  configures bitrate registers
758  * @chip: pointer to chip state structure
759  * @btr0: bitrate register 0
760  * @btr1: bitrate register 1
761  *
762  * Return Value: negative value reports error.
763  * File: src/usbcan.c
764  */
765 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
766                                                         unsigned short btr1)
767 {
768 /*      if (usbcan_enable_configuration(chip))
769                 return -ENODEV;
770
771         can_write_reg(chip, btr0, SJABTR0);
772         can_write_reg(chip, btr1, SJABTR1);
773
774         usbcan_disable_configuration(chip);
775 */
776         return 0;
777 }
778
779 /**
780  * usbcan_start_chip: -  starts chip message processing
781  * @chip: pointer to chip state structure
782  *
783  * Return Value: negative value reports error.
784  * File: src/usbcan.c
785  */
786 int usbcan_start_chip(struct canchip_t *chip)
787 {
788 /*      enum sja1000_PeliCAN_MOD flags;
789
790         flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
791         can_write_reg(chip, flags, SJAMOD);
792
793         sja1000_report_error_limit_counter=0;
794 */
795         return 0;
796 }
797
798 /**
799  * usbcan_stop_chip: -  stops chip message processing
800  * @chip: pointer to chip state structure
801  *
802  * Return Value: negative value reports error.
803  * File: src/usbcan.c
804  */
805 int usbcan_stop_chip(struct canchip_t *chip)
806 {
807 /*      enum sja1000_PeliCAN_MOD flags;
808
809         flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
810         can_write_reg(chip, flags|sjaMOD_RM, SJAMOD);
811 */
812         return 0;
813 }
814
815 /**
816  * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
817  * @chip: pointer to chip state structure
818  *
819  * Return Value: negative value reports error.
820  * File: src/usbcan.c
821  */
822 int usbcan_attach_to_chip(struct canchip_t *chip)
823 {
824         return 0;
825 }
826
827 /**
828  * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
829  * @chip: pointer to chip state structure
830  *
831  * Return Value: negative value reports error.
832  * File: src/usbcan.c
833  */
834 int usbcan_release_chip(struct canchip_t *chip)
835 {
836 /*      usbcan_stop_chip(chip);
837         can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
838 */
839         return 0;
840 }
841
842 /**
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
846  *
847  * Return Value: negative value reports error.
848  * File: src/usbcan.c
849  */
850 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
851 {
852         CANMSG("usbcan_remote_request not implemented\n");
853         return -ENOSYS;
854 }
855
856 /**
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
861  *
862  * Return Value: negative value reports error.
863  * File: src/usbcan.c
864  */
865 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
866                 unsigned short mask)
867 {
868         CANMSG("usbcan_standard_mask not implemented\n");
869         return -ENOSYS;
870 }
871
872 /**
873  * usbcan_clear_objects: - clears state of all message object residing in chip
874  * @chip: pointer to chip state structure
875  *
876  * Return Value: negative value reports error.
877  * File: src/usbcan.c
878  */
879 int usbcan_clear_objects(struct canchip_t *chip)
880 {
881         CANMSG("usbcan_clear_objects not implemented\n");
882         return -ENOSYS;
883 }
884
885 /**
886  * usbcan_config_irqs: - tunes chip hardware interrupt delivery
887  * @chip: pointer to chip state structure
888  * @irqs: requested chip IRQ configuration
889  *
890  * Return Value: negative value reports error.
891  * File: src/usbcan.c
892  */
893 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
894 {
895         CANMSG("usbcan_config_irqs not implemented\n");
896         return -ENOSYS;
897 }
898
899 /**
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
903  *
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.
908  * File: src/usbcan.c
909  */
910 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
911 {
912 /*      int cmd;
913
914         if(obj->tx_slot){
915                 // Do local transmitted message distribution if enabled
916                 if (processlocal){
917                         // fill CAN message timestamp
918                         can_filltimestamp(&obj->tx_slot->msg.timestamp);
919
920                         obj->tx_slot->msg.flags |= MSG_LOCAL;
921                         canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
922                 }
923                 // Free transmitted slot
924                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
925                 obj->tx_slot=NULL;
926         }
927
928         can_msgobj_clear_fl(obj,TX_PENDING);
929         cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
930         if(cmd<0)
931                 return;
932         can_msgobj_set_fl(obj,TX_PENDING);
933
934         if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
935                 obj->ret = -1;
936                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
937                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
938                 obj->tx_slot=NULL;
939                 return;
940         }
941         if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
942                 obj->ret = -1;
943                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
944                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
945                 obj->tx_slot=NULL;
946                 return;
947         }
948 */
949 }
950
951 #define MAX_RETR 10
952
953 /**
954  * usbcan_irq_handler: - interrupt service routine
955  * @irq: interrupt vector number, this value is system specific
956  * @chip: pointer to chip state structure
957  *
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
962  * message queues.
963  * File: src/usbcan.c
964  */
965 int usbcan_irq_handler(int irq, struct canchip_t *chip)
966 {
967 /*      int irq_register, status, error_code;
968         struct msgobj_t *obj=chip->msgobj[0];
969         int loop_cnt=CHIP_MAX_IRQLOOP;
970
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));
975
976         if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
977                 return CANCHIP_IRQ_NONE;
978
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;
982         }
983
984         status=can_read_reg(chip,SJASR);
985
986         do {
987
988                 if(!loop_cnt--) {
989                         CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
990                         return CANCHIP_IRQ_STUCK;
991                 }
992
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);
998                         obj->ret = 0;
999                 }
1000
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");
1006                         obj->ret = 0;
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);
1010
1011                                 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
1012                                         usbcan_irq_write_handler(chip, obj);
1013
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");
1017                         }
1018                 }
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
1025                         obj->ret=-1;
1026
1027                         if(error_code == 0xd9) {
1028                                 obj->ret= -ENXIO;
1029                                 // no such device or address - no ACK received
1030                         }
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;
1034                         }
1035                         if(status&sjaSR_BS) {
1036                                 CANMSG("bus-off, resetting usbcan\n");
1037                                 can_write_reg(chip, 0, SJAMOD);
1038                         }
1039
1040                         if(obj->tx_slot){
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;
1044                         }
1045
1046                 } else {
1047                         if(sja1000_report_error_limit_counter)
1048                                 sja1000_report_error_limit_counter--;
1049                         obj->tx_retry_cnt=0;
1050                 }
1051
1052                 irq_register=can_read_reg(chip,SJAIR);
1053
1054                 status=can_read_reg(chip,SJASR);
1055
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);
1059
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));
1063 */
1064         return CANCHIP_IRQ_HANDLED;
1065 }
1066
1067 /**
1068  * usbcan_wakeup_tx: - wakeups TX processing
1069  * @chip: pointer to chip state structure
1070  * @obj: pointer to message object structure
1071  *
1072  * Function is responsible for initiating message transmition.
1073  * It is responsible for clearing of object TX_REQUEST flag
1074  *
1075  * Return Value: negative value reports error.
1076  * File: src/usbcan.c
1077  */
1078 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
1079 {
1080
1081 /*      can_preempt_disable();
1082
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);
1087
1088                 if (can_read_reg(chip, SJASR) & sjaSR_TBS){
1089                         obj->tx_retry_cnt=0;
1090                         usbcan_irq_write_handler(chip, obj);
1091                 }
1092
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");
1096         }
1097
1098         can_preempt_enable();*/
1099         return 0;
1100 }
1101
1102 int usbcan_chipregister(struct chipspecops_t *chipspecops)
1103 {
1104         return 0;
1105 }
1106
1107 /**
1108  * usbcan_fill_chipspecops - fills chip specific operations
1109  * @chip: pointer to chip representation structure
1110  *
1111  * The function fills chip specific operations for sja1000 (PeliCAN) chip.
1112  *
1113  * Return Value: returns negative number in the case of fail
1114  */
1115 int usbcan_fill_chipspecops(struct canchip_t *chip)
1116 {
1117         return 0;
1118 }
1119
1120 /**
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
1124  *
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
1128  * "sja1000".
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
1151  */
1152 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
1153 {
1154         struct canchip_t *chip=candev->chip[chipnr];
1155
1156         chip->chip_type="usbcan";
1157         chip->max_objects=1;
1158         usbcan_chipregister(chip->chipspecops);
1159
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;
1183
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 |
1191                                                                 sjaOCR_TX0_LH;*/
1192
1193         return 0;
1194 }
1195
1196
1197
1198 /* --------------------------------------------------------------------------------------------------- */
1199
1200
1201 static void usbcan_irq(struct urb *urb)
1202 {
1203         struct usb_usbcan *dev = urb->context;
1204         int retval;
1205
1206         CANMSG("Interrupt poll\n");
1207
1208         switch (urb->status) {
1209         case 0:
1210                 /* success */
1211                 break;
1212         case -ECONNRESET:
1213         case -ENOENT:
1214         case -ESHUTDOWN:
1215                 /* this urb is terminated, clean up */
1216                 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1217                 return;
1218         default:
1219                 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1220                 goto exit;
1221         }
1222
1223         dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
1224         CANMSG("Interrupt caught\n");
1225
1226  exit:
1227         retval = usb_submit_urb (urb, GFP_ATOMIC);
1228         if (retval)
1229                 CANMSG("%s - usb_submit_urb failed with result %d\n",
1230                      __FUNCTION__, retval);
1231 }
1232
1233 static void usbcan_delete(struct usb_usbcan *dev)
1234 {
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);
1240         if (dev->candev){
1241                 dev->candev->sysdevptr.anydev=NULL;
1242                 cleanup_usbdev(dev->candev);
1243         }
1244         kfree(dev);
1245 }
1246
1247 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
1248 {
1249         struct usb_usbcan *dev;
1250         struct usb_host_interface *iface_desc;
1251         struct usb_endpoint_descriptor *endpoint;
1252         struct candevice_t *candev;
1253         size_t buffer_size;
1254         int i;
1255         int retval = -ENOMEM;
1256
1257         /* allocate memory for our device state and initialize it */
1258         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1259         if (!dev) {
1260                 err("Out of memory");
1261                 goto error;
1262         }
1263         sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
1264         spin_lock_init(&dev->err_lock);
1265         init_usb_anchor(&dev->submitted);
1266
1267 //      dev->udev = usb_get_dev(interface_to_usbdev(interface));
1268         dev->udev = interface_to_usbdev(interface);
1269         dev->interface = interface;
1270
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;
1276
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");
1286                                 goto error;
1287                         }
1288                 }
1289
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;
1294                 }
1295
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");
1306                                 goto error;
1307                         }
1308                 }*/
1309         }
1310         if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
1311                 err("Could not find all bulk-in, bulk-out and interrupt endpoints");
1312                 goto error;
1313         }
1314
1315         /* save our data pointer in this interface device */
1316         usb_set_intfdata(interface, dev);
1317
1318         register_usbdev("usbcan",(void *) dev);
1319
1320 /*      dev->irq = usb_alloc_urb(0, GFP_KERNEL);
1321         if (!dev->irq){
1322                 CANMSG("Error allocating usb urb\n");
1323                 goto error;
1324         }
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,
1333                          usbcan_irq, dev);*/
1334
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);
1338 //      if (retval){
1339 //              CANMSG("INT URB %d\n",retval);
1340 //              return -EIO;
1341 //      }else
1342 //              CANMSG("INT URB SUCCCESS\n");
1343
1344         /* let the user know what node this device is now attached to */
1345         info("USB Skeleton device now attached");
1346         return 0;
1347
1348 error:
1349                 usbcan_delete(dev);
1350         return retval;
1351 }
1352
1353 static void usbcan_disconnect(struct usb_interface *interface)
1354 {
1355         struct usb_usbcan *dev;
1356         int minor = interface->minor;
1357
1358         dev = usb_get_intfdata(interface);
1359         usb_set_intfdata(interface, NULL);
1360
1361         /* prevent more I/O from starting */
1362         mutex_lock(&dev->io_mutex);
1363         dev->interface = NULL;
1364         mutex_unlock(&dev->io_mutex);
1365
1366         //usb_kill_anchored_urbs(&dev->submitted);
1367
1368         usbcan_delete(dev);
1369
1370         info("USB Skeleton now disconnected");
1371 }
1372
1373 static struct usb_driver usbcan_driver = {
1374         .name =         "usbcan",
1375         .id_table = usbcan_table,
1376         .probe =        usbcan_probe,
1377         .disconnect =   usbcan_disconnect,
1378 };
1379
1380 int usbcan_init(void){
1381         return usb_register(&usbcan_driver);
1382 }
1383
1384 void usbcan_exit(void){
1385         usb_deregister(&usbcan_driver);
1386 }