]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/usbcan.c
Unoptimized usb extension, adds proc and devfs entry removing function, changed proc...
[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/usbcan.h"
12
13 /*
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.
17  */
18 #define IO_RANGE 0x100
19
20 /**
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
24  *
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
32  * File: src/usbcan.c
33  */
34 int usbcan_request_io(struct candevice_t *candev)
35 {
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);
38                 return -ENODEV;
39         }else {
40                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
41         }
42         return 0;
43 }
44
45 /**
46  * usbcan_release_io - free reserved io memory range
47  * @candev: pointer to candevice/board which releases io
48  *
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
55  * File: src/usbcan.c
56  */
57 int usbcan_release_io(struct candevice_t *candev)
58 {
59         can_release_io_region(candev->io_addr,IO_RANGE);
60
61         return 0;
62 }
63
64 /**
65  * usbcan_reset - hardware reset routine
66  * @candev: Pointer to candevice/board structure
67  *
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
72  * File: src/usbcan.c
73  */
74 int usbcan_reset(struct candevice_t *candev)
75 {
76         return 0;
77 }
78
79 #define RESET_ADDR 0x0
80
81 /**
82  * usbcan_init_hw_data - Initialize hardware cards
83  * @candev: Pointer to candevice/board structure
84  *
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
93  * File: src/usbcan.c
94  */
95 int usbcan_init_hw_data(struct candevice_t *candev)
96 {
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;
102
103         return 0;
104 }
105
106 /**
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
110  *
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
114  * "sja1000".
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
136  * File: src/usbcan.c
137  */
138 int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
139 {
140         canchip_t chip=candev->chip[chipnr];
141
142         chip->chip_type="usbcan";
143         chip->max_objects=1;
144         usbcan_register(chip->chipspecops);
145
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;
169
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 |
177                                                                 sjaOCR_TX0_LH;
178
179         return 0;
180 }
181
182 /**
183  * usbcan_init_obj_data - Initialize message buffers
184  * @chip: Pointer to chip specific structure
185  * @objnr: Number of the message buffer
186  *
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
194  * base address.
195  * Unless the hardware uses a segmented memory map, flags can be set zero.
196  * Return Value: The function always returns zero
197  * File: src/usbcan.c
198  */
199 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
200 {
201         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
202
203         return 0;
204 }
205
206 /**
207  * usbcan_program_irq - program interrupts
208  * @candev: Pointer to candevice/board structure
209  *
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
216  * File: src/usbcan.c
217  */
218 int usbcan_program_irq(struct candevice_t *candev)
219 {
220         return 0;
221 }
222
223 /**
224  * usbcan_write_register - Low level write register routine
225  * @data: data to be written
226  * @address: memory address to write to
227  *
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
232  * File: src/usbcan.c
233  */
234 void usbcan_write_register(unsigned data, unsigned long address)
235 {
236         outb(data,address);
237 }
238
239 /**
240  * usbcan_read_register - Low level read register routine
241  * @address: memory address to read from
242  *
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
247  * File: src/usbcan.c
248  */
249 unsigned usbcan_read_register(unsigned long address)
250 {
251         return inb(address);
252 }
253
254 /* !!! Don't change this function !!! */
255 int usbcan_register(struct hwspecops_t *hwspecops)
256 {
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;
266         return 0;
267 }
268
269 static const char *sja1000_ecc_errc_str[]={
270         "bit error",
271         "form error",
272         "stuff error",
273         "other type of error"
274 };
275
276 static const char *sja1000_ecc_seg_str[]={
277         "?0?",
278         "?1?",
279         "ID.28 to ID.21",
280         "start of frame",
281         "bit SRTR",
282         "bit IDE",
283         "ID.20 to ID.18",
284         "ID.17 to ID.13",
285         "CRC sequence",
286         "reserved bit 0",
287         "data field",
288         "data length code",
289         "bit RTR",
290         "reserved bit 1",
291         "ID.4 to ID.0",
292         "ID.12 to ID.5",
293         "?16?"
294         "active error flag",
295         "intermission",
296         "tolerate dominant bits",
297         "?20?",
298         "?21?",
299         "passive error flag",
300         "error delimiter",
301         "CRC delimiter",
302         "acknowledge slot",
303         "end of frame",
304         "acknowledge delimiter",
305         "overload flag",
306         "?29?",
307         "?30?",
308         "?31?"
309 };
310
311 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
312
313 static int sja1000_report_error_limit_counter;
314
315 static void sja1000_report_error(struct canchip_t *chip,
316                                 unsigned sr, unsigned ir, unsigned ecc)
317 {
318         if(sja1000_report_error_limit_counter>=100)
319                 return;
320
321         CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
322                 sr, ir, ecc);
323
324         sja1000_report_error_limit_counter+=10;
325
326         if(sja1000_report_error_limit_counter>=100){
327                 sja1000_report_error_limit_counter+=10;
328                 CANMSG("Error: too many errors, reporting disabled\n");
329                 return;
330         }
331
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]
348                       );
349         }
350 #endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
351 }
352
353
354 /**
355  * usbcan_enable_configuration - enable chip configuration mode
356  * @chip: pointer to chip state structure
357  */
358 int usbcan_enable_configuration(struct canchip_t *chip)
359 {
360         int i=0;
361         enum sja1000_PeliCAN_MOD flags;
362
363         can_disable_irq(chip->chip_irq);
364
365         flags=can_read_reg(chip,SJAMOD);
366
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)
371                 udelay(100);
372                 i++;
373                 flags=can_read_reg(chip, SJAMOD);
374         }
375         if (i>=10) {
376                 CANMSG("Reset error\n");
377                 can_enable_irq(chip->chip_irq);
378                 return -ENODEV;
379         }
380
381         return 0;
382 }
383
384 /**
385  * usbcan_disable_configuration - disable chip configuration mode
386  * @chip: pointer to chip state structure
387  */
388 int usbcan_disable_configuration(struct canchip_t *chip)
389 {
390         int i=0;
391         enum sja1000_PeliCAN_MOD flags;
392
393         flags=can_read_reg(chip,SJAMOD);
394
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)
400                 udelay(100);
401                 i++;
402                 flags=can_read_reg(chip, SJAMOD);
403         }
404         if (i>=10) {
405                 CANMSG("Error leaving reset status\n");
406                 return -ENODEV;
407         }
408
409         can_enable_irq(chip->chip_irq);
410
411         return 0;
412 }
413
414 /**
415  * usbcan_chip_config: - can chip configuration
416  * @chip: pointer to chip state structure
417  *
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.
424  * File: src/usbcan.c
425  */
426 int usbcan_chip_config(struct canchip_t *chip)
427 {
428         int i;
429         unsigned char n, r;
430
431         if (usbcan_enable_configuration(chip))
432                 return -ENODEV;
433
434         /* Set mode, clock out, comparator */
435         can_write_reg(chip,sjaCDR_PELICAN|chip->sja_cdr_reg,SJACDR);
436
437         /* Ensure, that interrupts are disabled even on the chip level now */
438         can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
439
440         /* Set driver output configuration */
441         can_write_reg(chip,chip->sja_ocr_reg,SJAOCR);
442
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);
446         }
447         for (i=0, n=0x5a; i<8; i++, n+=0xf) {
448                 r = n^can_read_reg(chip,SJAACR0+i);
449                 if (r) {
450                         CANMSG("usbcan_chip_config: chip connection broken,"
451                                 " readback differ 0x%02x\n", r);
452                         return -ENODEV;
453                 }
454         }
455
456
457         if (usbcan_extended_mask(chip,0x00000000, 0xffffffff))
458                 return -ENODEV;
459
460         if (!chip->baudrate)
461                 chip->baudrate=1000000;
462         if (usbcan_baud_rate(chip,chip->baudrate,chip->clock,0,75,0))
463                 return -ENODEV;
464
465         /* Enable hardware interrupts */
466         can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER);
467
468         usbcan_disable_configuration(chip);
469
470         return 0;
471 }
472
473 /**
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
478  *
479  * Return Value: negative value reports error.
480  * File: src/usbcan.c
481  */
482 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  long mask)
483 {
484         int i;
485
486         if (usbcan_enable_configuration(chip))
487                 return -ENODEV;
488
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);
493                 code >>= 8;
494                 mask >>= 8;
495         }
496
497         DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
498         DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
499
500         usbcan_disable_configuration(chip);
501
502         return 0;
503 }
504
505 /**
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
513  *
514  * Return Value: negative value reports error.
515  * File: src/usbcan.c
516  */
517 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
518                                                         int sampl_pt, int flags)
519 {
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;
523
524         if (usbcan_enable_configuration(chip))
525                 return -ENODEV;
526
527         clock /=2;
528
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)
533                         continue;
534                 error = rate - clock/(brp*(1+tseg/2));
535                 if (error < 0)
536                         error = -error;
537                 if (error <= best_error) {
538                         best_error = error;
539                         best_tseg = tseg/2;
540                         best_brp = brp-1;
541                         best_rate = clock/(brp*(1+tseg/2));
542                 }
543         }
544         if (best_error && (rate/best_error < 10)) {
545                 CANMSG("baud rate %d is not possible with %d Hz clock\n",
546                                                                 rate, 2*clock);
547                 CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
548                                 best_rate, best_brp, best_tseg, tseg1, tseg2);
549                 return -EINVAL;
550         }
551         tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
552         if (tseg2 < 0)
553                 tseg2 = 0;
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;
560         }
561
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)));
566
567
568         can_write_reg(chip, sjw<<6 | best_brp, SJABTR0);
569         can_write_reg(chip, ((flags & BTR1_SAM) != 0)<<7 | (tseg2<<4)
570                                         | tseg1, SJABTR1);
571
572         usbcan_disable_configuration(chip);
573
574         return 0;
575 }
576
577 /**
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
581  *
582  * File: src/usbcan.c
583  */
584 void usbcan_read(struct canchip_t *chip, struct msgobj_t *obj) {
585         int i, flags, len, datastart;
586         do {
587                 flags = can_read_reg(chip,SJAFRM);
588                 if(flags&sjaFRM_FF) {
589                         obj->rx_msg.id =
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);
594                         datastart = SJADATE;
595                 } else {
596                         obj->rx_msg.id =
597                                 (can_read_reg(chip,SJAID0)<<3) +
598                                 (can_read_reg(chip,SJAID1)>>5);
599                         datastart = SJADATS;
600                 }
601                 obj->rx_msg.flags =
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);
609                 }
610
611                 /* fill CAN message timestamp */
612                 can_filltimestamp(&obj->rx_msg.timestamp);
613
614                 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
615
616                 can_write_reg(chip, sjaCMR_RRB, SJACMR);
617
618         } while (can_read_reg(chip, SJASR) & sjaSR_RBS);
619 }
620
621 /**
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
625  *
626  * Return Value: negative value reports error.
627  *      Positive value indicates immediate reception of message.
628  * File: src/usbcan.c
629  */
630 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
631 {
632         int status;
633         status=can_read_reg(chip,SJASR);
634
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);
643         }
644
645         if (!(status&sjaSR_RBS)) {
646                 return 0;
647         }
648
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
652         return 1;
653 }
654
655 #define MAX_TRANSMIT_WAIT_LOOPS 10
656 /**
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
661  *
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.
667  * File: src/usbcan.c
668  */
669 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
670                                                         struct canmsg_t *msg)
671 {
672         int i=0;
673         unsigned int id;
674         int status;
675         int len;
676
677         /* Wait until Transmit Buffer Status is released */
678         while ( !((status=can_read_reg(chip, SJASR)) & sjaSR_TBS) &&
679                                                 i++<MAX_TRANSMIT_WAIT_LOOPS) {
680                 udelay(i);
681         }
682
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);
691         }
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
697 // immediately
698                 can_write_reg(chip, sjaCMR_AT, SJACMR);
699                 i=0;
700                 while ( !(can_read_reg(chip, SJASR) & sjaSR_TBS) &&
701                                                 i++<MAX_TRANSMIT_WAIT_LOOPS) {
702                         udelay(i);
703                 }
704                 if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
705                         CANMSG("Could not cancel, please reset\n");
706                         return -EIO;
707                 }
708         }
709         len = msg->length;
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) {
715                 id=msg->id<<3;
716                 can_write_reg(chip, id & 0xff, SJAID3);
717                 id >>= 8;
718                 can_write_reg(chip, id & 0xff, SJAID2);
719                 id >>= 8;
720                 can_write_reg(chip, id & 0xff, SJAID1);
721                 id >>= 8;
722                 can_write_reg(chip, id, SJAID0);
723                 for(i=0; i < len; i++) {
724                         can_write_reg(chip, msg->data[i], SJADATE+i);
725                 }
726         } else {
727                 id=msg->id<<5;
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);
732                 }
733         }
734         return 0;
735 }
736
737 /**
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
742  *
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.
746  * File: src/usbcan.c
747  */
748 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
749                                                         struct canmsg_t *msg)
750 {
751         can_write_reg(chip, sjaCMR_TR, SJACMR);
752
753         return 0;
754 }
755
756 /**
757  * usbcan_check_tx_stat: - checks state of transmission engine
758  * @chip: pointer to chip state structure
759  *
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.
763  * File: src/usbcan.c
764  */
765 int usbcan_check_tx_stat(struct canchip_t *chip)
766 {
767         if (can_read_reg(chip,SJASR) & sjaSR_TCS)
768                 return 0;
769         else
770                 return 1;
771 }
772
773 /**
774  * usbcan_set_btregs: -  configures bitrate registers
775  * @chip: pointer to chip state structure
776  * @btr0: bitrate register 0
777  * @btr1: bitrate register 1
778  *
779  * Return Value: negative value reports error.
780  * File: src/usbcan.c
781  */
782 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
783                                                         unsigned short btr1)
784 {
785         if (usbcan_enable_configuration(chip))
786                 return -ENODEV;
787
788         can_write_reg(chip, btr0, SJABTR0);
789         can_write_reg(chip, btr1, SJABTR1);
790
791         usbcan_disable_configuration(chip);
792
793         return 0;
794 }
795
796 /**
797  * usbcan_start_chip: -  starts chip message processing
798  * @chip: pointer to chip state structure
799  *
800  * Return Value: negative value reports error.
801  * File: src/usbcan.c
802  */
803 int usbcan_start_chip(struct canchip_t *chip)
804 {
805         enum sja1000_PeliCAN_MOD flags;
806
807         flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
808         can_write_reg(chip, flags, SJAMOD);
809
810         sja1000_report_error_limit_counter=0;
811
812         return 0;
813 }
814
815 /**
816  * usbcan_stop_chip: -  stops chip message processing
817  * @chip: pointer to chip state structure
818  *
819  * Return Value: negative value reports error.
820  * File: src/usbcan.c
821  */
822 int usbcan_stop_chip(struct canchip_t *chip)
823 {
824         enum sja1000_PeliCAN_MOD flags;
825
826         flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
827         can_write_reg(chip, flags|sjaMOD_RM, SJAMOD);
828
829         return 0;
830 }
831
832 /**
833  * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
834  * @chip: pointer to chip state structure
835  *
836  * Return Value: negative value reports error.
837  * File: src/usbcan.c
838  */
839 int usbcan_attach_to_chip(struct canchip_t *chip)
840 {
841         return 0;
842 }
843
844 /**
845  * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
846  * @chip: pointer to chip state structure
847  *
848  * Return Value: negative value reports error.
849  * File: src/usbcan.c
850  */
851 int usbcan_release_chip(struct canchip_t *chip)
852 {
853         usbcan_stop_chip(chip);
854         can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
855
856         return 0;
857 }
858
859 /**
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
863  *
864  * Return Value: negative value reports error.
865  * File: src/usbcan.c
866  */
867 int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
868 {
869         CANMSG("usbcan_remote_request not implemented\n");
870         return -ENOSYS;
871 }
872
873 /**
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
878  *
879  * Return Value: negative value reports error.
880  * File: src/usbcan.c
881  */
882 int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
883                 unsigned short mask)
884 {
885         CANMSG("usbcan_standard_mask not implemented\n");
886         return -ENOSYS;
887 }
888
889 /**
890  * usbcan_clear_objects: - clears state of all message object residing in chip
891  * @chip: pointer to chip state structure
892  *
893  * Return Value: negative value reports error.
894  * File: src/usbcan.c
895  */
896 int usbcan_clear_objects(struct canchip_t *chip)
897 {
898         CANMSG("usbcan_clear_objects not implemented\n");
899         return -ENOSYS;
900 }
901
902 /**
903  * usbcan_config_irqs: - tunes chip hardware interrupt delivery
904  * @chip: pointer to chip state structure
905  * @irqs: requested chip IRQ configuration
906  *
907  * Return Value: negative value reports error.
908  * File: src/usbcan.c
909  */
910 int usbcan_config_irqs(struct canchip_t *chip, short irqs)
911 {
912         CANMSG("usbcan_config_irqs not implemented\n");
913         return -ENOSYS;
914 }
915
916 /**
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
920  *
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.
925  * File: src/usbcan.c
926  */
927 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
928 {
929         int cmd;
930
931         if(obj->tx_slot){
932                 /* Do local transmitted message distribution if enabled */
933                 if (processlocal){
934                         /* fill CAN message timestamp */
935                         can_filltimestamp(&obj->tx_slot->msg.timestamp);
936
937                         obj->tx_slot->msg.flags |= MSG_LOCAL;
938                         canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
939                 }
940                 /* Free transmitted slot */
941                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
942                 obj->tx_slot=NULL;
943         }
944
945         can_msgobj_clear_fl(obj,TX_PENDING);
946         cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
947         if(cmd<0)
948                 return;
949         can_msgobj_set_fl(obj,TX_PENDING);
950
951         if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
952                 obj->ret = -1;
953                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
954                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
955                 obj->tx_slot=NULL;
956                 return;
957         }
958         if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
959                 obj->ret = -1;
960                 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
961                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
962                 obj->tx_slot=NULL;
963                 return;
964         }
965
966 }
967
968 #define MAX_RETR 10
969
970 /**
971  * usbcan_irq_handler: - interrupt service routine
972  * @irq: interrupt vector number, this value is system specific
973  * @chip: pointer to chip state structure
974  *
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
979  * message queues.
980  * File: src/usbcan.c
981  */
982 int usbcan_irq_handler(int irq, struct canchip_t *chip)
983 {
984         int irq_register, status, error_code;
985         struct msgobj_t *obj=chip->msgobj[0];
986         int loop_cnt=CHIP_MAX_IRQLOOP;
987
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));
992
993         if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
994                 return CANCHIP_IRQ_NONE;
995
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;
999         }
1000
1001         status=can_read_reg(chip,SJASR);
1002
1003         do {
1004
1005                 if(!loop_cnt--) {
1006                         CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
1007                         return CANCHIP_IRQ_STUCK;
1008                 }
1009
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);
1015                         obj->ret = 0;
1016                 }
1017
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");
1023                         obj->ret = 0;
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);
1027
1028                                 if (can_read_reg(chip, SJASR) & sjaSR_TBS)
1029                                         usbcan_irq_write_handler(chip, obj);
1030
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");
1034                         }
1035                 }
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
1042                         obj->ret=-1;
1043
1044                         if(error_code == 0xd9) {
1045                                 obj->ret= -ENXIO;
1046                                 /* no such device or address - no ACK received */
1047                         }
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;
1051                         }
1052                         if(status&sjaSR_BS) {
1053                                 CANMSG("bus-off, resetting usbcan\n");
1054                                 can_write_reg(chip, 0, SJAMOD);
1055                         }
1056
1057                         if(obj->tx_slot){
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;*/
1061                         }
1062
1063                 } else {
1064                         if(sja1000_report_error_limit_counter)
1065                                 sja1000_report_error_limit_counter--;
1066                         obj->tx_retry_cnt=0;
1067                 }
1068
1069                 irq_register=can_read_reg(chip,SJAIR);
1070
1071                 status=can_read_reg(chip,SJASR);
1072
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);
1076
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));
1080
1081         return CANCHIP_IRQ_HANDLED;
1082 }
1083
1084 /**
1085  * usbcan_wakeup_tx: - wakeups TX processing
1086  * @chip: pointer to chip state structure
1087  * @obj: pointer to message object structure
1088  *
1089  * Function is responsible for initiating message transmition.
1090  * It is responsible for clearing of object TX_REQUEST flag
1091  *
1092  * Return Value: negative value reports error.
1093  * File: src/usbcan.c
1094  */
1095 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
1096 {
1097
1098         can_preempt_disable();
1099
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);
1104
1105                 if (can_read_reg(chip, SJASR) & sjaSR_TBS){
1106                         obj->tx_retry_cnt=0;
1107                         usbcan_irq_write_handler(chip, obj);
1108                 }
1109
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");
1113         }
1114
1115         can_preempt_enable();
1116         return 0;
1117 }
1118
1119 int usbcan_register(struct chipspecops_t *chipspecops)
1120 {
1121         return 0;
1122 }
1123
1124 /**
1125  * usbcan_fill_chipspecops - fills chip specific operations
1126  * @chip: pointer to chip representation structure
1127  *
1128  * The function fills chip specific operations for sja1000 (PeliCAN) chip.
1129  *
1130  * Return Value: returns negative number in the case of fail
1131  */
1132 int usbcan_fill_chipspecops(struct canchip_t *chip)
1133 {
1134         return 0;
1135 }
1136
1137
1138
1139 /* --------------------------------------------------------------------------------------------------- */
1140
1141
1142 static void ul_usb1_irq(struct urb *urb)
1143 {
1144         struct usb_ul_usb1 *dev = urb->context;
1145         struct ul_usb1_combo devc;
1146         int retval;
1147
1148         CANMSG("Interrupt poll\n");
1149
1150         switch (urb->status) {
1151         case 0:
1152                 /* success */
1153                 break;
1154         case -ECONNRESET:
1155         case -ENOENT:
1156         case -ESHUTDOWN:
1157                 /* this urb is terminated, clean up */
1158                 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
1159                 return;
1160         default:
1161                 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
1162                 goto exit;
1163         }
1164
1165         devc.dev = dev;
1166         devc.urb = urb;
1167
1168         dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
1169         CANMSG("Interrupt caught\n");
1170
1171  exit:
1172         retval = usb_submit_urb (urb, GFP_ATOMIC);
1173         if (retval)
1174                 CANMSG("%s - usb_submit_urb failed with result %d\n",
1175                      __FUNCTION__, retval);
1176 }
1177
1178 static void ul_usb1_delete(struct usb_ul_usb1 *dev)
1179 {
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);
1185         if (dev->candev){
1186                 dev->candev->sysdevptr.anydev=NULL;
1187                 cleanup_usbdev(dev->candev);
1188         }
1189         kfree(dev);
1190 }
1191
1192 static int ul_usb1_probe(struct usb_interface *interface, const struct usb_device_id *id)
1193 {
1194         struct usb_ul_usb1 *dev;
1195         struct usb_host_interface *iface_desc;
1196         struct usb_endpoint_descriptor *endpoint;
1197         struct candevice_t *candev;
1198         size_t buffer_size;
1199         int i;
1200         int retval = -ENOMEM;
1201
1202         /* allocate memory for our device state and initialize it */
1203         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1204         if (!dev) {
1205                 err("Out of memory");
1206                 goto error;
1207         }
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);
1212
1213 //      dev->udev = usb_get_dev(interface_to_usbdev(interface));
1214         dev->udev = interface_to_usbdev(interface);
1215         dev->interface = interface;
1216
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;
1222
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");
1232                                 goto error;
1233                         }
1234                 }
1235
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;
1240                 }
1241
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");
1252                                 goto error;
1253                         }
1254                 }
1255         }
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");
1258                 goto error;
1259         }
1260
1261         /* save our data pointer in this interface device */
1262         usb_set_intfdata(interface, dev);
1263
1264         if (main_init_done==1)
1265                 register_usbdev("ul_usb1",(void *) dev);
1266         else {
1267                 mutex_lock(&usbdev_reg_mutex);
1268                 if (main_init_done==1)
1269                         register_usbdev("ul_usb1",(void *) dev);
1270                 else {
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));
1274                                         if (!usbregq[i]){
1275                                                 CANMSG("Error allocating usbdev_reg_query");
1276                                                 mutex_unlock(&usbdev_reg_mutex);
1277                                                 goto error;
1278                                         }
1279                                         sprintf (usbregq[i]->hwname,"ul_usb1");
1280                                         usbregq[i]->anydev=(void *) dev;
1281                                         break;
1282                                 }
1283                         }
1284                         if (i==MAX_HW_CARDS){
1285                                 CANMSG("No free space to register new card");
1286                                 mutex_unlock(&usbdev_reg_mutex);
1287                                 goto error;
1288                         }
1289                 }
1290                 mutex_unlock(&usbdev_reg_mutex);
1291         }
1292
1293         dev->irq = usb_alloc_urb(0, GFP_KERNEL);
1294         if (!dev->irq){
1295                 CANMSG("Error allocating usb urb\n");
1296                 goto error;
1297         }
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);*/
1307
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);
1311         if (retval){
1312                 CANMSG("INT URB %d\n",retval);
1313                 return -EIO;
1314         }else
1315                 CANMSG("INT URB SUCCCESS\n");
1316
1317         /* let the user know what node this device is now attached to */
1318         info("USB Skeleton device now attached");
1319         return 0;
1320
1321 error:
1322                 ul_usb1_delete(dev);
1323         return retval;
1324 }
1325
1326 static void ul_usb1_disconnect(struct usb_interface *interface)
1327 {
1328         struct usb_ul_usb1 *dev;
1329         int minor = interface->minor;
1330
1331         dev = usb_get_intfdata(interface);
1332         usb_set_intfdata(interface, NULL);
1333
1334         /* prevent more I/O from starting */
1335         mutex_lock(&dev->io_mutex);
1336         dev->interface = NULL;
1337         mutex_unlock(&dev->io_mutex);
1338
1339         //usb_kill_anchored_urbs(&dev->submitted);
1340
1341         ul_usb1_delete(dev);
1342
1343         info("USB Skeleton now disconnected");
1344 }
1345
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,
1352 };
1353
1354 int ul_usb1_init(void){
1355         return usb_register(&ul_usb1_driver);
1356 }
1357
1358 void ul_usb1_exit(void){
1359         usb_deregister(&ul_usb1_driver);
1360 }