ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / hisax / hfc_usb.c
1 /*
2  * hfc_usb.c
3  *
4  * modular HiSax ISDN driver for Colognechip HFC-USB chip
5  *
6  * Authors : Peter Sprenger  (sprenger@moving-byters.de)
7  *           Martin Bachem   (info@colognechip.com)
8  *           based on the first hfc_usb driver of Werner Cornelius (werner@isdn-development.de)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24 */
25
26
27 #include <linux/types.h>
28 #include <linux/stddef.h>
29 #include <linux/timer.h>
30 #include <linux/config.h>
31 #include <linux/init.h>
32 #include "hisax.h"
33 #include <linux/module.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/usb.h>
36 #include <linux/kernel.h>
37 #include <linux/smp_lock.h>
38 #include <linux/sched.h>
39 #include "hisax_if.h"
40
41 static const char *hfcusb_revision = "4.0";
42
43 /*
44         to enable much mire debug messages in this driver, define
45                         VERBOSE_USB_DEBUG and VERBOSE_ISDN_DEBUG
46         below
47 */
48
49 #define VERBOSE_USB_DEBUG
50 #define VERBOSE_ISDN_DEBUG
51
52 #define INCLUDE_INLINE_FUNCS
53
54 #define TRUE  1
55 #define FALSE 0
56
57
58 /***********/
59 /* defines */
60 /***********/
61 #define HFC_CTRL_TIMEOUT        20  //(HZ * USB_CTRL_GET_TIMEOUT)
62 /* 5ms timeout writing/reading regs */
63 #define HFC_TIMER_T3     8000      /* timeout for l1 activation timer */
64 #define HFC_TIMER_T4     500       /* time for state change interval */
65
66 #define HFCUSB_L1_STATECHANGE   0  /* L1 state changed */
67 #define HFCUSB_L1_DRX           1  /* D-frame received */
68 #define HFCUSB_L1_ERX           2  /* E-frame received */
69 #define HFCUSB_L1_DTX           4  /* D-frames completed */
70
71 #define MAX_BCH_SIZE        2048   /* allowed B-channel packet size */
72
73 #define HFCUSB_RX_THRESHOLD 64     /* threshold for fifo report bit rx */
74 #define HFCUSB_TX_THRESHOLD 64     /* threshold for fifo report bit tx */
75
76 #define HFCUSB_CHIP_ID    0x16     /* Chip ID register index */
77 #define HFCUSB_CIRM       0x00     /* cirm register index */
78 #define HFCUSB_USB_SIZE   0x07     /* int length register */
79 #define HFCUSB_USB_SIZE_I 0x06     /* iso length register */
80 #define HFCUSB_F_CROSS    0x0b     /* bit order register */
81 #define HFCUSB_CLKDEL     0x37     /* bit delay register */
82 #define HFCUSB_CON_HDLC   0xfa     /* channel connect register */
83 #define HFCUSB_HDLC_PAR   0xfb
84 #define HFCUSB_SCTRL      0x31     /* S-bus control register (tx) */
85 #define HFCUSB_SCTRL_E    0x32     /* same for E and special funcs */
86 #define HFCUSB_SCTRL_R    0x33     /* S-bus control register (rx) */
87 #define HFCUSB_F_THRES    0x0c     /* threshold register */
88 #define HFCUSB_FIFO       0x0f     /* fifo select register */
89 #define HFCUSB_F_USAGE    0x1a     /* fifo usage register */
90 #define HFCUSB_MST_MODE0  0x14
91 #define HFCUSB_MST_MODE1  0x15
92 #define HFCUSB_P_DATA     0x1f
93 #define HFCUSB_INC_RES_F  0x0e
94 #define HFCUSB_STATES     0x30
95
96 #define HFCUSB_CHIPID 0x40         /* ID value of HFC-USB */
97
98 /******************/
99 /* fifo registers */
100 /******************/
101 #define HFCUSB_NUM_FIFOS   8       /* maximum number of fifos */
102 #define HFCUSB_B1_TX       0       /* index for B1 transmit bulk/int */
103 #define HFCUSB_B1_RX       1       /* index for B1 receive bulk/int */
104 #define HFCUSB_B2_TX       2
105 #define HFCUSB_B2_RX       3
106 #define HFCUSB_D_TX        4
107 #define HFCUSB_D_RX        5
108 #define HFCUSB_PCM_TX      6
109 #define HFCUSB_PCM_RX      7
110
111 /*
112 * used to switch snd_transfer_mode for different TA modes e.g. the Billion USB TA just
113 * supports ISO out, while the Cologne Chip EVAL TA just supports BULK out
114 */
115 #define USB_INT         0
116 #define USB_BULK        1
117 #define USB_ISOC        2
118
119 #define ISOC_PACKETS_D  8
120 #define ISOC_PACKETS_B  8
121 #define ISO_BUFFER_SIZE 128
122
123 // ISO send definitions
124 #define SINK_MAX        68
125 #define SINK_MIN        48
126 #define SINK_DMIN       12
127 #define SINK_DMAX       18
128 #define BITLINE_INF     (-64*8)
129
130
131
132
133 /**********/
134 /* macros */
135 /**********/
136 #define write_usb(a,b,c) usb_control_msg((a)->dev,(a)->ctrl_out_pipe,0,0x40,(c),(b),0,0,HFC_CTRL_TIMEOUT)
137 #define read_usb(a,b,c) usb_control_msg((a)->dev,(a)->ctrl_in_pipe,1,0xC0,0,(b),(c),1,HFC_CTRL_TIMEOUT)
138
139 /*************************************************/
140 /* entry and size of output/input control buffer */
141 /*************************************************/
142 #define HFC_CTRL_BUFSIZE 32
143 typedef struct
144 {
145         __u8 hfc_reg;           /* register number */
146         __u8 reg_val;           /* value to be written (or read) */
147         int action;         /* data for action handler */
148
149 } ctrl_buft;
150
151 typedef struct
152 {
153         int vendor;         // vendor id
154         int prod_id;        // product id
155         char *vend_name;    // vendor string
156         __u8 led_scheme;    // led display scheme
157         __u8 led_invert;    // invert led aux port settings
158         __u8 led_bits[8];   // array of 8 possible LED bitmask settings
159
160 } vendor_data;
161
162 /***************************************************************/
163 /* structure defining input+output fifos (interrupt/bulk mode) */
164 /***************************************************************/
165
166 struct usb_fifo;                        /* forward definition */
167 typedef struct iso_urb_struct
168 {
169         struct urb *purb;
170         __u8 buffer[ISO_BUFFER_SIZE];   /* buffer incoming/outgoing data */
171         struct usb_fifo *owner_fifo;    // pointer to owner fifo
172 } iso_urb_struct;
173
174
175 struct hfcusb_data;                     /* forward definition */
176 typedef struct usb_fifo
177 {
178         int fifonum;                    /* fifo index attached to this structure */
179         int active;                     /* fifo is currently active */
180         struct hfcusb_data *hfc;        /* pointer to main structure */
181         int pipe;                       /* address of endpoint */
182         __u8 usb_packet_maxlen;         /* maximum length for usb transfer */
183         unsigned int max_size;          /* maximum size of receive/send packet */
184         __u8 intervall;                 /* interrupt interval */
185         struct sk_buff *skbuff;         /* actual used buffer */
186         struct urb *urb;                /* transfer structure for usb routines */
187         __u8 buffer[128];               /* buffer incoming/outgoing data */
188         int bit_line;                   /* how much bits are in the fifo? */
189
190         volatile __u8 usb_transfer_mode;/* switched between ISO and INT */
191         iso_urb_struct iso[2];          /* need two urbs to have one always for pending */
192         struct hisax_if *hif;           /* hisax interface */
193         int delete_flg;                 /* only delete skbuff once */
194         int last_urblen;                /* remember length of last packet */
195
196 } usb_fifo;
197
198
199 /*********************************************/
200 /* structure holding all data for one device */
201 /*********************************************/
202 typedef struct hfcusb_data
203 {
204         // HiSax Interface for loadable Layer1 drivers
205         struct hisax_d_if d_if;                 /* see hisax_if.h */
206         struct hisax_b_if b_if[2];              /* see hisax_if.h */
207         int protocol;
208         
209         struct usb_device *dev;                 /* our device */
210         int if_used;                            /* used interface number */
211         int alt_used;                           /* used alternate config */
212         int ctrl_paksize;                       /* control pipe packet size */
213         int ctrl_in_pipe, ctrl_out_pipe;        /* handles for control pipe */
214         int cfg_used;                           /* configuration index used */
215         int vend_idx;                           // vendor found
216
217         int b_mode[2];                          // B-channel mode
218
219         int l1_activated;                       // layer 1 activated
220
221         int packet_size,iso_packet_size;        
222
223         /* control pipe background handling */
224         ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE];  /* buffer holding queued data */
225         volatile int ctrl_in_idx, ctrl_out_idx,
226                 ctrl_cnt;                       /* input/output pointer + count */
227         struct urb *ctrl_urb;                   /* transfer structure for control channel */
228
229         struct usb_ctrlrequest ctrl_write;      /* buffer for control write request */
230         struct usb_ctrlrequest ctrl_read;       /* same for read request */
231
232         __u8 led_state,led_new_data,led_b_active;
233
234         volatile __u8 threshold_mask;           /* threshold actually reported */
235         volatile __u8 bch_enables;              /* or mask for sctrl_r and sctrl register values */
236
237         usb_fifo fifos[HFCUSB_NUM_FIFOS];       /* structure holding all fifo data */
238
239         volatile __u8 l1_state;                 /* actual l1 state */
240         struct timer_list t3_timer;             /* timer 3 for activation/deactivation */
241         struct timer_list t4_timer;             /* timer 4 for activation/deactivation */
242         struct timer_list led_timer;            /* timer flashing leds */
243
244 } hfcusb_data;
245
246
247 static void collect_rx_frame(usb_fifo *fifo,__u8 *data,int len,int finish);
248
249
250
251 /******************************************************/
252 /* start next background transfer for control channel */
253 /******************************************************/
254 static void ctrl_start_transfer(hfcusb_data * hfc)
255 {
256         int err;
257         if(hfc->ctrl_cnt)
258         {
259                 hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
260                 hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
261                 hfc->ctrl_urb->transfer_buffer = NULL;
262                 hfc->ctrl_urb->transfer_buffer_length = 0;
263                 hfc->ctrl_write.wIndex = hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg;
264                 hfc->ctrl_write.wValue = hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val;
265                 err = usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC);        /* start transfer */
266                 printk(KERN_DEBUG "ctrl_start_transfer: submit %d\n", err);
267         }
268 }                               /* ctrl_start_transfer */
269
270 /************************************/
271 /* queue a control transfer request */
272 /* return 0 on success.             */
273 /************************************/
274 static int queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val,int action)
275 {
276         ctrl_buft *buf;
277
278 #ifdef VERBOSE_USB_DEBUG
279         printk ("HFC_USB: queue_control_request reg: %x, val: %x\n", reg, val);
280 #endif
281
282         if(hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)  return(1);          /* no space left */
283         buf = &hfc->ctrl_buff[hfc->ctrl_in_idx];        /* pointer to new index */
284         buf->hfc_reg = reg;
285         buf->reg_val = val;
286         buf->action=action;
287         if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
288                 hfc->ctrl_in_idx = 0;   /* pointer wrap */
289         if (++hfc->ctrl_cnt == 1)
290                 ctrl_start_transfer(hfc);
291         return(0);
292 }               /* queue_control_request */
293
294
295 static int control_action_handler(hfcusb_data *hfc,int reg,int val,int action)
296 {
297         if(!action) return(1);  // no action defined
298
299         return(0);
300 }
301
302
303 /***************************************************************/
304 /* control completion routine handling background control cmds */
305 /***************************************************************/
306 static void ctrl_complete(struct urb *urb, struct pt_regs *regs)
307 {
308         hfcusb_data *hfc = (hfcusb_data *) urb->context;
309         ctrl_buft *buf;
310
311         printk(KERN_DEBUG "ctrl_complete cnt %d\n", hfc->ctrl_cnt);
312         urb->dev = hfc->dev;
313         if(hfc->ctrl_cnt)
314         {
315                 buf=&hfc->ctrl_buff[hfc->ctrl_out_idx];
316                 control_action_handler(hfc,buf->hfc_reg,buf->reg_val,buf->action);
317
318                 hfc->ctrl_cnt--;        /* decrement actual count */
319                 if(++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE) hfc->ctrl_out_idx = 0;      /* pointer wrap */
320
321                 ctrl_start_transfer(hfc);       /* start next transfer */
322         }
323 }                               /* ctrl_complete */
324
325
326
327 #define LED_OFF      0   // no LED support
328 #define LED_SCHEME1  1   // LED standard scheme
329 #define LED_SCHEME2  2   // not used yet...
330
331 #define LED_POWER_ON    1
332 #define LED_POWER_OFF   2
333 #define LED_S0_ON               3
334 #define LED_S0_OFF              4
335 #define LED_B1_ON               5
336 #define LED_B1_OFF              6
337 #define LED_B1_DATA             7
338 #define LED_B2_ON               8
339 #define LED_B2_OFF              9
340 #define LED_B2_DATA        10
341
342 #define LED_NORMAL   0   // LEDs are normal
343 #define LED_INVERTED 1   // LEDs are inverted
344
345 // time for LED flashing
346 #define LED_TIME      250
347
348 vendor_data vdata[]=
349 {
350     {0x959, 0x2bd0, "ISDN USB TA (Cologne Chip HFC-S USB based)", LED_OFF,LED_NORMAL,{4,0,2,1}},     /* CologneChip Eval TA */
351         {0x7b0, 0x0007, "Billion tiny USB ISDN TA 128", LED_SCHEME1,  LED_INVERTED, {8,0x40,0x20,0x10}},  /* Billion TA */
352         {0x742, 0x2008, "Stollmann USB TA",             LED_SCHEME1,  LED_NORMAL,   {4,0,2,1}},           /* Stollmann TA */
353         {0x8e3, 0x0301, "Olitec USB RNIS",              LED_SCHEME1,  LED_NORMAL,   {2,0,1,4}},           /* Olitec TA  */
354         {0x675, 0x1688, "DrayTec USB ISDN TA",          LED_SCHEME1,  LED_NORMAL,   {4,0,2,1}},           /* Draytec TA */
355         {0x7fa, 0x0846, "Bewan Modem RNIS USB",         LED_SCHEME1,  LED_INVERTED, {8,0x40,0x20,0x10}},  /* Bewan TA   */
356         {0,0,0}                    // EOL element
357 };
358                                                                                 
359 /***************************************************/
360 /* write led data to auxport & invert if necessary */
361 /***************************************************/
362 static void write_led(hfcusb_data * hfc,__u8 led_state)
363 {
364         if(led_state!=hfc->led_state)
365         {
366                 hfc->led_state=led_state;
367                 queue_control_request(hfc, HFCUSB_P_DATA,(vdata[hfc->vend_idx].led_invert) ? ~led_state : led_state,1);
368         }
369 }
370
371 /******************************************/
372 /* invert B-channel LEDs if data is sent  */
373 /******************************************/
374 static void led_timer(hfcusb_data * hfc)
375 {
376         static int cnt=0;
377         __u8 led_state=hfc->led_state;
378
379         if(cnt)
380         {
381                 if(hfc->led_b_active&1) led_state|=vdata[hfc->vend_idx].led_bits[2];
382                 if(hfc->led_b_active&2) led_state|=vdata[hfc->vend_idx].led_bits[3];
383         }
384         else
385         {
386                 if(!(hfc->led_b_active&1) || hfc->led_new_data&1) led_state&=~vdata[hfc->vend_idx].led_bits[2];
387                 if(!(hfc->led_b_active&2) || hfc->led_new_data&2) led_state&=~vdata[hfc->vend_idx].led_bits[3];
388         }
389
390         write_led(hfc,led_state);
391         hfc->led_new_data=0;
392
393         cnt=!cnt;
394         // restart 4 hz timer
395         hfc->led_timer.expires = jiffies + (LED_TIME * HZ) / 1000;
396         if(!timer_pending(&hfc->led_timer)) add_timer(&hfc->led_timer);
397 }
398
399 /**************************/
400 /* handle LED requests    */
401 /**************************/
402 static void handle_led(hfcusb_data * hfc,int event)
403 {
404         __u8 led_state=hfc->led_state;
405
406         // if no scheme -> no LED action
407         if(vdata[hfc->vend_idx].led_scheme==LED_OFF) return;
408
409         switch(event)
410         {
411                 case LED_POWER_ON:
412                                    led_state|=vdata[hfc->vend_idx].led_bits[0];
413                                 break;
414                 case LED_POWER_OFF: // no Power off handling
415                                 break;
416                 case LED_S0_ON:
417                                    led_state|=vdata[hfc->vend_idx].led_bits[1];
418                                 break;
419                 case LED_S0_OFF:
420                                    led_state&=~vdata[hfc->vend_idx].led_bits[1];
421                                 break;
422                 case LED_B1_ON:
423                                         hfc->led_b_active|=1;
424                                 break;
425                 case LED_B1_OFF:
426                                         hfc->led_b_active&=~1;
427                                 break;
428                 case LED_B1_DATA:
429                                    hfc->led_new_data|=1;
430                                 break;
431                 case LED_B2_ON:
432                                    hfc->led_b_active|=2;
433                                 break;
434                 case LED_B2_OFF:
435                                         hfc->led_b_active&=~2;
436                                 break;
437                 case LED_B2_DATA:
438                                    hfc->led_new_data|=2;
439                                 break;
440         }
441         
442         write_led(hfc,led_state);
443 }
444
445 /********************************/
446 /* called when timer t3 expires */
447 /********************************/
448 static void l1_timer_expire_t3(hfcusb_data * hfc)
449 {
450     //printk (KERN_INFO "HFC-USB: l1_timer_expire_t3\n");
451
452         hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_DEACTIVATE | INDICATION,NULL);
453 #ifdef VERBOSE_USB_DEBUG
454         printk(KERN_INFO "PH_DEACTIVATE | INDICATION sent\n");
455 #endif
456         hfc->l1_activated=FALSE;
457         handle_led(hfc,LED_S0_OFF);
458 }
459
460 /********************************/
461 /* called when timer t4 expires */
462 /********************************/
463 static void l1_timer_expire_t4(hfcusb_data * hfc)
464 {
465     //printk (KERN_INFO "HFC-USB: l1_timer_expire_t4\n");
466
467         hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_DEACTIVATE | INDICATION,NULL);
468 #ifdef VERBOSE_USB_DEBUG
469         printk(KERN_INFO "PH_DEACTIVATE | INDICATION sent\n");
470 #endif
471         hfc->l1_activated=FALSE;
472         handle_led(hfc,LED_S0_OFF);
473 }
474
475 /*****************************/
476 /* handle S0 state changes   */
477 /*****************************/
478 static void state_handler(hfcusb_data * hfc,__u8 state)
479 {
480         __u8 old_state;
481
482         old_state=hfc->l1_state;
483
484         // range check
485         if(state==old_state || state<1 || state>8) return;
486
487 #ifdef VERBOSE_ISDN_DEBUG
488         printk(KERN_INFO "HFC-USB: new S0 state:%d old_state:%d\n",state,old_state);
489 #endif
490
491         if(state<4 || state==7 || state==8)
492         {
493         if(timer_pending(&hfc->t3_timer)) del_timer(&hfc->t3_timer);
494                 //printk(KERN_INFO "HFC-USB: T3 deactivated\n");
495         }
496
497         if(state>=7)
498         {
499         if(timer_pending(&hfc->t4_timer)) del_timer(&hfc->t4_timer);
500                 //printk(KERN_INFO "HFC-USB: T4 deactivated\n");
501         }
502
503         if(state==7 && !hfc->l1_activated)
504         {
505                 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_ACTIVATE | INDICATION,NULL);
506                 //printk(KERN_INFO "HFC-USB: PH_ACTIVATE | INDICATION sent\n");
507                 hfc->l1_activated=TRUE;
508                 handle_led(hfc,LED_S0_ON);
509         }
510         else
511         if(state<=3 /* && activated*/)
512         {
513                 if(old_state==7 || old_state==8)
514                 {
515                         //printk(KERN_INFO "HFC-USB: T4 activated\n");
516                         hfc->t4_timer.expires = jiffies + (HFC_TIMER_T4 * HZ) / 1000;
517                         if(!timer_pending(&hfc->t4_timer)) add_timer(&hfc->t4_timer);
518                 }
519                 else
520                 {
521                         hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_DEACTIVATE | INDICATION,NULL);
522                         //printk(KERN_INFO "HFC-USB: PH_DEACTIVATE | INDICATION sent\n");
523                         hfc->l1_activated=FALSE;
524                         handle_led(hfc,LED_S0_OFF);
525                 }
526         }
527
528         hfc->l1_state=state;
529 }
530
531
532 /* prepare iso urb */
533 static void fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe, void *buf,
534         int num_packets, int packet_size, int interval, usb_complete_t complete, void *context)
535 {
536         int k;
537
538         spin_lock_init(&urb->lock);     // do we really need spin_lock_init ?
539         urb->dev = dev;
540         urb->pipe = pipe;
541         urb->complete = complete;
542         urb->number_of_packets = num_packets;
543         urb->transfer_buffer_length = packet_size * num_packets;
544         urb->context = context;
545         urb->transfer_buffer = buf;
546         urb->transfer_flags = 0;
547         urb->transfer_flags = URB_ISO_ASAP;
548         urb->actual_length = 0;
549         urb->interval = interval;
550         for (k = 0; k < num_packets; k++) {
551                 urb->iso_frame_desc[k].offset = packet_size * k;
552                 urb->iso_frame_desc[k].length = packet_size;
553                 urb->iso_frame_desc[k].actual_length = 0;
554         }
555 }
556
557 /* allocs urbs and start isoc transfer with two pending urbs to avoid gaps in the transfer chain */
558 static int start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,usb_complete_t complete,int packet_size)
559 {
560         int i, k, errcode;
561
562 #ifdef VERBOSE_USB_DEBUG
563         printk(KERN_INFO "HFC-USB: starting ISO-chain for Fifo %i\n",  fifo->fifonum);
564 #endif
565
566
567         // allocate Memory for Iso out Urbs
568         for (i = 0; i < 2; i++) {
569                 if (!(fifo->iso[i].purb)) {
570                         fifo->iso[i].purb = usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
571                         fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
572
573                         // Init the first iso
574                         if (ISO_BUFFER_SIZE >= (fifo->usb_packet_maxlen * num_packets_per_urb))
575                         {
576
577                                 fill_isoc_urb(fifo->iso[i].purb, fifo->hfc->dev, fifo->pipe, fifo->iso[i].buffer,
578                                         num_packets_per_urb, fifo->usb_packet_maxlen, fifo->intervall,
579                                         complete, &fifo->iso[i]);
580
581                                 memset(fifo->iso[i].buffer, 0, sizeof(fifo->iso[i].buffer));
582
583                                 // defining packet delimeters in fifo->buffer
584                                 for(k = 0; k < num_packets_per_urb; k++)
585                                 {
586                                         fifo->iso[i].purb->iso_frame_desc[k].offset = k*packet_size;
587                                         fifo->iso[i].purb->iso_frame_desc[k].length = packet_size;
588                                 }
589                         }
590                 }
591
592                 fifo->bit_line = BITLINE_INF;
593
594                 errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
595                 fifo->active = (errcode >= 0) ? 1 : 0;
596                 if(errcode < 0)
597                 {
598                         printk(KERN_INFO "HFC-USB: error submitting ISO URB: %i.%i \n",  errcode, i);
599                 };
600
601         }
602
603         // errcode = (usb_submit_urb(fifo->iso[0].purb, GFP_KERNEL));
604         return(fifo->active);
605 }
606
607 /* stops running iso chain and frees their pending urbs */
608 static void stop_isoc_chain(usb_fifo * fifo)
609 {
610         int i;
611
612         for(i = 0; i < 2; i++)
613         {
614                 if(fifo->iso[i].purb)
615                 {
616 #ifdef VERBOSE_USB_DEBUG
617                         printk(KERN_INFO "HFC-USB: Stopping iso chain for fifo %i.%i\n", fifo->fifonum, i);
618 #endif
619                         usb_unlink_urb(fifo->iso[i].purb);
620                         usb_free_urb(fifo->iso[i].purb);
621                         fifo->iso[i].purb = NULL;
622                 }
623         }
624         if (fifo->urb) {
625                 usb_unlink_urb(fifo->urb);
626                 usb_free_urb(fifo->urb);
627                 fifo->urb = NULL;
628         }
629         fifo->active = 0;
630 }
631
632 // defines how much ISO packets are handled in one URB
633 static int iso_packets[8]={ISOC_PACKETS_B,ISOC_PACKETS_B,ISOC_PACKETS_B,ISOC_PACKETS_B,
634                                ISOC_PACKETS_D,ISOC_PACKETS_D,ISOC_PACKETS_D,ISOC_PACKETS_D};
635
636 /*****************************************************/
637 /* transmit completion routine for all ISO tx fifos */
638 /*****************************************************/
639 static void tx_iso_complete(struct urb *urb, struct pt_regs *regs)
640 {
641         iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
642         usb_fifo *fifo = context_iso_urb->owner_fifo;
643         hfcusb_data *hfc = fifo->hfc;
644         int k, tx_offset, num_isoc_packets, sink, len, current_len,errcode,frame_complete,transp_mode,fifon;
645         __u8 threshbit;
646         __u8 threshtable[8] = { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
647
648         fifon=fifo->fifonum;
649         tx_offset=0;
650         // very weird error code when using ohci drivers, for now : ignore this error ...  (MB)
651         if(urb->status == -EOVERFLOW)
652         {
653                 urb->status = 0;
654 #ifdef VERBOSE_USB_DEBUG
655                 printk(KERN_INFO "HFC-USB: ignoring USB DATAOVERRUN  for fifo  %i \n",fifon);
656 #endif
657         }
658
659         if(fifo->active && !urb->status)
660         {
661                 transp_mode=0;
662                 if(fifon<4 && hfc->b_mode[fifon/2]==L1_MODE_TRANS) transp_mode=TRUE;
663
664                 threshbit = threshtable[fifon] & hfc->threshold_mask;   // is threshold set for our channel?
665                 num_isoc_packets=iso_packets[fifon];
666
667                 if(fifon >= HFCUSB_D_TX)
668                 {
669                         sink = (threshbit) ? SINK_DMIN : SINK_DMAX;     // how much bit go to the sink for D-channel?
670                 }
671                 else
672                 {
673                         sink = (threshbit) ? SINK_MIN : SINK_MAX;       // how much bit go to the sink for B-channel?
674                 }
675
676                 // prepare ISO Urb
677                 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,context_iso_urb->buffer, num_isoc_packets,
678                         fifo->usb_packet_maxlen, fifo->intervall, tx_iso_complete, urb->context);
679                 memset(context_iso_urb->buffer, 0, sizeof(context_iso_urb->buffer));
680
681                 frame_complete=FALSE;
682
683                 // Generate Iso Packets
684                 for(k = 0; k < num_isoc_packets; ++k)
685                 {
686                         if(fifo->skbuff)
687                         {
688                                 len = fifo->skbuff->len;        // remaining length
689
690                                 fifo->bit_line -= sink; // we lower data margin every msec
691                                 current_len = (0 - fifo->bit_line) / 8;
692                                 if(current_len > 14) current_len = 14;  // maximum 15 byte for every ISO packet makes our life easier
693                                 current_len = (len <= current_len) ? len : current_len;
694                                 fifo->bit_line += current_len * 8;      // how much bit do we put on the line?
695
696                                 context_iso_urb->buffer[tx_offset] = 0;
697                                 if(current_len == len)
698                                 {
699                                         if(!transp_mode)
700                                         {
701                                                 context_iso_urb->buffer[tx_offset] = 1; // here frame completion
702                                                 fifo->bit_line += 32;   // add 2 byte flags and 16bit CRC at end of ISDN frame
703                                         }
704                                         frame_complete = TRUE;
705                                 }
706
707                                 // copy bytes from buffer into ISO_URB
708                                 memcpy(context_iso_urb->buffer+tx_offset+1,fifo->skbuff->data,current_len);
709                                 skb_pull(fifo->skbuff,current_len);
710
711                                 // define packet delimeters within the URB buffer
712                                 urb->iso_frame_desc[k].offset = tx_offset;
713                                 urb->iso_frame_desc[k].length = current_len + 1;
714
715                                 tx_offset += (current_len + 1);
716                                 // printk(KERN_INFO "HFC-USB: fifonum:%d,%d bytes to send, %d bytes ISO packet,bitline:%d,sink:%d,threshbit:%d,threshmask:%x\n",fifon,len,current_len,fifo->bit_line,sink,threshbit,hfc->threshold_mask);
717                                 if(!transp_mode)
718                                 {
719                                         if(fifon==HFCUSB_B1_TX) handle_led(hfc,LED_B1_DATA);
720                                         if(fifon==HFCUSB_B2_TX) handle_led(hfc,LED_B2_DATA);
721                                 }
722                         }
723                         else
724                         {
725                                 // we have no more data - generate 1 byte ISO packets
726                                 urb->iso_frame_desc[k].offset = tx_offset++;
727
728                                 urb->iso_frame_desc[k].length = 1;
729                                 fifo->bit_line -= sink; // we lower data margin every msec
730
731                                 if(fifo->bit_line < BITLINE_INF)
732                                 {
733                                         fifo->bit_line = BITLINE_INF;
734                                         //printk (KERN_INFO "HFC-USB: BITLINE_INF underrun\n");
735                                 }
736                         }
737
738                         if(frame_complete)
739                         {
740                                 // delete the buffer only once, here or in hfc_usb_l2l1() in a PH_DATA|REQUEST
741                                 fifo->delete_flg=TRUE;
742
743                                 fifo->hif->l1l2(fifo->hif,PH_DATA|CONFIRM,(void*)fifo->skbuff->truesize);
744
745                                 if(fifo->skbuff && fifo->delete_flg)
746                                 {
747                                         dev_kfree_skb_any(fifo->skbuff);
748                                         //printk(KERN_INFO "HFC-USB: skbuff=NULL on fifo:%d\n",fifo->fifonum);
749                                         fifo->skbuff = NULL;
750                                         fifo->delete_flg=FALSE;
751                                 }
752
753                                 frame_complete=FALSE;
754                         }
755         }
756
757                 errcode = usb_submit_urb(urb, GFP_ATOMIC);
758                 if(errcode < 0)
759                 {
760                         printk(KERN_INFO "HFC-USB: error submitting ISO URB: %i \n",  errcode);
761                 }
762         }
763         else
764         {
765                 if(urb->status)
766                 {
767                         printk(KERN_INFO "HFC-USB: tx_iso_complete : urb->status %i, fifonum %i\n",  urb->status,fifon);
768                 }
769         }
770
771 }                               /* tx_iso_complete */
772
773 /*****************************************************/
774 /* receive completion routine for all ISO tx fifos   */
775 /*****************************************************/
776 static void rx_iso_complete(struct urb *urb, struct pt_regs *regs)
777 {
778         iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
779         usb_fifo *fifo = context_iso_urb->owner_fifo;
780         hfcusb_data *hfc = fifo->hfc;
781         int k, len, errcode, offset, num_isoc_packets,fifon;
782         __u8 *buf;
783
784         fifon=fifo->fifonum;
785         // very weird error code when using ohci drivers, for now : ignore this error ...  (MB)
786         if(urb->status == -EOVERFLOW)
787         {
788                 urb->status = 0;
789 #ifdef VERBOSE_USB_DEBUG
790                 printk(KERN_INFO "HFC-USB: ignoring USB DATAOVERRUN  for fifo  %i \n",fifon);
791 #endif
792         }
793
794         if(fifo->active && !urb->status)
795         {
796                 num_isoc_packets=iso_packets[fifon];
797
798                 // Generate D-Channel Iso Packets
799                 for(k = 0; k < num_isoc_packets; ++k)
800                 {
801                         len=urb->iso_frame_desc[k].actual_length;
802                         offset=urb->iso_frame_desc[k].offset;
803                         buf=context_iso_urb->buffer+offset;
804
805                         if(fifo->last_urblen!=fifo->usb_packet_maxlen)
806                         {
807                                 // the threshold mask is in the 2nd status byte
808                                 hfc->threshold_mask=buf[1];
809                                 // the S0 state is in the upper half of the 1st status byte
810                                 state_handler(hfc,buf[0] >> 4);
811                                 // if we have more than the 2 status bytes -> collect data
812                                 if(len>2) collect_rx_frame(fifo,buf+2,len-2,buf[0]&1);
813                         }
814                         else collect_rx_frame(fifo,buf,len,0);
815
816                         fifo->last_urblen=len;
817
818         }
819
820                 // prepare ISO Urb
821                 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,context_iso_urb->buffer, num_isoc_packets,
822                         fifo->usb_packet_maxlen, fifo->intervall, rx_iso_complete, urb->context);
823
824                 errcode = usb_submit_urb(urb, GFP_ATOMIC);
825                 if(errcode < 0)
826                 {
827                         printk(KERN_INFO "HFC-USB: error submitting ISO URB: %i \n",  errcode);
828                 }
829         }
830         else
831         {
832                 if(urb->status)
833                 {
834                         printk(KERN_INFO "HFC-USB: rx_iso_complete : urb->status %i, fifonum %i\n",  urb->status,fifon);
835                 }
836         }
837 }                               /* rx_iso_complete */
838
839
840 /*****************************************************/
841 /* collect data from interrupt or isochron in        */
842 /*****************************************************/
843 static void collect_rx_frame(usb_fifo *fifo,__u8 *data,int len,int finish)
844 {
845         hfcusb_data *hfc = fifo->hfc;
846         int transp_mode,fifon;
847
848         fifon=fifo->fifonum;
849         transp_mode=0;
850         if(fifon<4 && hfc->b_mode[fifon/2]==L1_MODE_TRANS) transp_mode=TRUE;
851
852         //printk(KERN_INFO "HFC-USB: got %d bytes finish:%d max_size:%d fifo:%d\n",len,finish,fifo->max_size,fifon);
853         if(!fifo->skbuff)
854         {
855                 // allocate sk buffer
856                 fifo->skbuff=dev_alloc_skb(fifo->max_size + 3);
857                 if(!fifo->skbuff)
858                 {
859                         printk(KERN_INFO "HFC-USB: cannot allocate buffer (dev_alloc_skb) fifo:%d\n",fifon);
860                         return;
861                 }
862                 
863         }
864
865         if(len && fifo->skbuff->len+len<fifo->max_size)
866         {
867                 memcpy(skb_put(fifo->skbuff,len),data,len);
868         }
869         else printk(KERN_INFO "HCF-USB: got frame exceeded fifo->max_size:%d\n",fifo->max_size);
870
871         // give transparent data up, when 128 byte are available
872         if(transp_mode && fifo->skbuff->len>=128)
873         {
874                 fifo->hif->l1l2(fifo->hif,PH_DATA | INDICATION,fifo->skbuff);
875                 fifo->skbuff = NULL;  // buffer was freed from upper layer
876                 return;
877         }
878
879         // we have a complete hdlc packet
880         if(finish)
881         {
882                 if(!fifo->skbuff->data[fifo->skbuff->len-1])
883                 {
884                         skb_trim(fifo->skbuff,fifo->skbuff->len-3);  // remove CRC & status
885
886                         //printk(KERN_INFO "HFC-USB: got frame %d bytes on fifo:%d\n",fifo->skbuff->len,fifon);
887
888                         if(fifon==HFCUSB_PCM_RX) fifo->hif->l1l2(fifo->hif,PH_DATA_E | INDICATION,fifo->skbuff);
889                         else fifo->hif->l1l2(fifo->hif,PH_DATA | INDICATION,fifo->skbuff);
890
891                         fifo->skbuff = NULL;  // buffer was freed from upper layer
892                 }
893                 else
894                 {
895                         printk(KERN_INFO "HFC-USB: got frame %d bytes but CRC ERROR!!!\n",fifo->skbuff->len);
896
897                         skb_trim(fifo->skbuff,0);  // clear whole buffer
898                 }
899         }
900
901         // LED flashing only in HDLC mode
902         if(!transp_mode)
903         {
904                 if(fifon==HFCUSB_B1_RX) handle_led(hfc,LED_B1_DATA);
905                 if(fifon==HFCUSB_B2_RX) handle_led(hfc,LED_B2_DATA);
906         }
907 }
908
909 /***********************************************/
910 /* receive completion routine for all rx fifos */
911 /***********************************************/
912 static void rx_complete(struct urb *urb, struct pt_regs *regs)
913 {
914         int len;
915         __u8 *buf;
916         usb_fifo *fifo = (usb_fifo *) urb->context;     /* pointer to our fifo */
917         hfcusb_data *hfc = fifo->hfc;
918
919         urb->dev = hfc->dev;    /* security init */
920
921         if((!fifo->active) || (urb->status)) {
922 #ifdef VERBOSE_USB_DEBUG
923                 printk(KERN_INFO "HFC-USB: RX-Fifo %i is going down (%i)\n", fifo->fifonum, urb->status);
924 #endif
925                 fifo->urb->interval = 0;        /* cancel automatic rescheduling */
926                 if(fifo->skbuff) {
927                         dev_kfree_skb_any(fifo->skbuff);
928                         fifo->skbuff = NULL;
929                 }
930                 return;
931         }
932
933         len=urb->actual_length;
934         buf=fifo->buffer;
935
936         if(fifo->last_urblen!=fifo->usb_packet_maxlen) {
937                 // the threshold mask is in the 2nd status byte
938                 hfc->threshold_mask=buf[1];
939                 // the S0 state is in the upper half of the 1st status byte
940                 state_handler(hfc,buf[0] >> 4);
941                 // if we have more than the 2 status bytes -> collect data
942                 if(len>2) collect_rx_frame(fifo,buf+2,urb->actual_length-2,buf[0]&1);
943         } else
944                 collect_rx_frame(fifo,buf,urb->actual_length,0);
945
946         fifo->last_urblen=urb->actual_length;
947
948
949 }       /* rx_complete */
950
951
952
953 /***************************************************/
954 /* start the interrupt transfer for the given fifo */
955 /***************************************************/
956 static void start_int_fifo(usb_fifo * fifo)
957 {
958         int errcode;
959
960 #ifdef VERBOSE_USB_DEBUG
961         printk(KERN_INFO "HFC-USB: starting intr IN fifo:%d\n", fifo->fifonum);
962 #endif
963         if (!fifo->urb) {
964                 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
965                 if (!fifo->urb)
966                         return;
967         }
968         usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe, fifo->buffer,
969                                  fifo->usb_packet_maxlen, rx_complete, fifo, fifo->intervall);
970         fifo->active = 1;               /* must be marked active */
971         errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
972
973         if(errcode)
974         {
975                 printk(KERN_INFO "HFC-USB: submit URB error(start_int_info): status:%i\n",   errcode);
976                 fifo->active = 0;
977                 fifo->skbuff = NULL;
978         }
979 } /* start_int_fifo */
980
981 /*****************************/
982 /* set the B-channel mode    */
983 /*****************************/
984 static void set_hfcmode(hfcusb_data *hfc,int channel,int mode)
985 {
986         __u8 val,idx_table[2]={0,2};
987
988 #ifdef VERBOSE_ISDN_DEBUG
989   printk (KERN_INFO "HFC-USB: setting channel %d to mode %d\n",channel,mode);
990 #endif
991
992         hfc->b_mode[channel]=mode;
993
994         // setup CON_HDLC
995         val=0;
996         if(mode!=L1_MODE_NULL) val=8;    // enable fifo?
997         if(mode==L1_MODE_TRANS) val|=2;  // set transparent bit
998
999         queue_control_request(hfc,HFCUSB_FIFO,idx_table[channel],1); // set FIFO to transmit register
1000         queue_control_request(hfc,HFCUSB_CON_HDLC,val,1);
1001         queue_control_request(hfc,HFCUSB_INC_RES_F,2,1); // reset fifo
1002
1003         queue_control_request(hfc,HFCUSB_FIFO,idx_table[channel]+1,1); // set FIFO to receive register
1004         queue_control_request(hfc,HFCUSB_CON_HDLC,val,1);
1005         queue_control_request(hfc,HFCUSB_INC_RES_F,2,1);  // reset fifo
1006
1007         val=0x40;
1008         if(hfc->b_mode[0]) val|=1;
1009         if(hfc->b_mode[1]) val|=2;
1010         queue_control_request(hfc,HFCUSB_SCTRL,val,1);
1011
1012         val=0;
1013         if(hfc->b_mode[0]) val|=1;
1014         if(hfc->b_mode[1]) val|=2;
1015         queue_control_request(hfc,HFCUSB_SCTRL_R,val,1);
1016
1017         if(mode==L1_MODE_NULL)
1018         {
1019                 if(channel) handle_led(hfc,LED_B2_OFF);
1020                 else handle_led(hfc,LED_B1_OFF);
1021         }
1022         else
1023         {
1024                 if(channel) handle_led(hfc,LED_B2_ON);
1025                 else handle_led(hfc,LED_B1_ON);
1026         }
1027 }
1028
1029 /*
1030    --------------------------------------------------------------------------------------
1031    from here : hisax_if callback routines :
1032      - void hfc_usb_d_l2l1(struct hisax_if *hisax_d_if, int pr, void *arg) {
1033
1034    l1 to l2 routines :
1035      - static void hfc_usb_l1l2(hfcusb_data * hfc)
1036
1037 */
1038
1039 void hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
1040 {
1041     usb_fifo *fifo = my_hisax_if->priv;
1042         hfcusb_data *hfc = fifo->hfc;
1043
1044     switch (pr) {
1045                 case PH_ACTIVATE | REQUEST:
1046                                 if(fifo->fifonum==HFCUSB_D_TX)
1047                                 {
1048 #ifdef VERBOSE_ISDN_DEBUG
1049                                         printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST\n");
1050 #endif
1051                                         queue_control_request(hfc, HFCUSB_STATES,0x60,1);       /* make activation */
1052                                         hfc->t3_timer.expires = jiffies + (HFC_TIMER_T3 * HZ) / 1000;
1053                                         if(!timer_pending(&hfc->t3_timer)) add_timer(&hfc->t3_timer);
1054                                 }
1055                                 else
1056                                 {
1057 #ifdef VERBOSE_ISDN_DEBUG
1058                                         printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_ACTIVATE | REQUEST\n");
1059 #endif
1060                                         set_hfcmode(hfc,(fifo->fifonum==HFCUSB_B1_TX) ? 0 : 1 ,(int)arg);
1061                                         fifo->hif->l1l2(fifo->hif,PH_ACTIVATE | INDICATION, NULL);
1062                                 }
1063                 break;
1064         case PH_DEACTIVATE | REQUEST:
1065                                 if(fifo->fifonum==HFCUSB_D_TX)
1066                                 {
1067 #ifdef VERBOSE_ISDN_DEBUG
1068                                         printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST\n");
1069 #endif
1070                                         printk (KERN_INFO "HFC-USB: ISDN TE device should not deativate...\n");
1071                                 }
1072                                 else
1073                                 {
1074 #ifdef VERBOSE_ISDN_DEBUG
1075                                         printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST\n");
1076 #endif
1077                                         set_hfcmode(hfc,(fifo->fifonum==HFCUSB_B1_TX) ? 0 : 1 ,(int)L1_MODE_NULL);
1078                                         fifo->hif->l1l2(fifo->hif,PH_DEACTIVATE | INDICATION, NULL);
1079                                 }
1080                 break;
1081                 case PH_DATA | REQUEST:
1082                                 if(fifo->skbuff && fifo->delete_flg)
1083                                 {
1084                                         dev_kfree_skb_any(fifo->skbuff);
1085                                         //printk(KERN_INFO "skbuff=NULL on fifo:%d\n",fifo->fifonum);
1086                                         fifo->skbuff = NULL;
1087                                         fifo->delete_flg=FALSE;
1088                                 }
1089
1090                                 fifo->skbuff=arg; // we have a new buffer
1091
1092                                 //if(fifo->fifonum==HFCUSB_D_TX) printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DATA | REQUEST\n");
1093                                 //else printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DATA | REQUEST\n");
1094                 break;
1095         default:
1096                 printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1: unkown state : %#x\n", pr);
1097                 break;
1098     }
1099 }
1100
1101 // valid configurations
1102 #define CNF_4INT3ISO  1      // 4 INT IN, 3 ISO OUT
1103 #define CNF_3INT3ISO  2      // 3 INT IN, 3 ISO OUT
1104 #define CNF_4ISO3ISO  3      // 4 ISO IN, 3 ISO OUT
1105 #define CNF_3ISO3ISO  4          // 3 ISO IN, 3 ISO OUT
1106
1107
1108 /*
1109    --------------------------------------------------------------------------------------
1110    From here on USB initialization and deactivation related routines are implemented :
1111
1112    - hfc_usb_init :
1113       is the main Entry Point for the USB Subsystem when the device get plugged
1114       in. This function calls usb_register with usb_driver as parameter.
1115       Here, further entry points for probing (hfc_usb_probe) and disconnecting
1116       the device (hfc_usb_disconnect) are published, as the id_table
1117
1118    - hfc_usb_probe
1119       this function is called by the usb subsystem, and steps through the alternate
1120       settings of the currently plugged in device to detect all Endpoints needed to
1121       run an ISDN TA.
1122       Needed EndPoints are
1123       3 (+1) IntIn EndPoints   (D-in,  E-in, B1-in, B2-in, (E-in)) or
1124       3 (+1) Isochron In Endpoints (D-out, B1-out, B2-out) and 3 IsoOut Endpoints
1125       The currently used transfer mode of on the Out-Endpoints will be stored in
1126       hfc->usb_transfer_mode and is either USB_INT or USB_ISO
1127       When a valid alternate setting could be found, the usb_init (see blow)
1128       function is called
1129
1130    - usb_init
1131       Here, the HFC_USB Chip itself gets initialized and the USB framework to send/receive
1132       Data to/from the several EndPoints are initialized:
1133        The E- and D-Channel Int-In chain gets started
1134        The IsoChain for the Iso-Out traffic get started
1135
1136    - hfc_usb_disconnect
1137       this function is called by the usb subsystem and has to free all resources
1138       and stop all usb traffic to allow a proper hotplugging disconnect.
1139
1140 */
1141
1142 /***************************************************************************/
1143 /* usb_init is called once when a new matching device is detected to setup */
1144 /* main parameters. It registers the driver at the main hisax module.       */
1145 /* on success 0 is returned.                                               */
1146 /***************************************************************************/
1147 static int usb_init(hfcusb_data * hfc)
1148 {
1149         usb_fifo *fifo;
1150         int i, err;
1151         u_char b;
1152         struct hisax_b_if *p_b_if[2];
1153         
1154         /* check the chip id */
1155         printk(KERN_INFO "HFCUSB_CHIP_ID begin\n");
1156         if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
1157                 printk(KERN_INFO "HFC-USB: cannot read chip id\n");
1158                 return(1); 
1159         }
1160         printk(KERN_INFO "HFCUSB_CHIP_ID %x\n", b);
1161         if (b != HFCUSB_CHIPID) {
1162                 printk(KERN_INFO "HFC-USB: Invalid chip id 0x%02x\n", b);
1163                 return(1);
1164         }
1165
1166         /* first set the needed config, interface and alternate */
1167         printk(KERN_INFO "usb_init 1\n");
1168 //      usb_set_configuration(hfc->dev, 1);
1169         printk(KERN_INFO "usb_init 2\n");
1170         err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1171         printk(KERN_INFO "usb_init usb_set_interface return %d\n", err);
1172         /* now we initialize the chip */
1173         write_usb(hfc, HFCUSB_CIRM, 8);     // do reset
1174         write_usb(hfc, HFCUSB_CIRM, 0x10);      // aux = output, reset off
1175
1176         // set USB_SIZE to match the the wMaxPacketSize for INT or BULK transfers
1177         write_usb(hfc, HFCUSB_USB_SIZE,(hfc->packet_size/8) | ((hfc->packet_size/8) << 4));
1178
1179         // set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers
1180         write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
1181
1182         /* enable PCM/GCI master mode */
1183         write_usb(hfc, HFCUSB_MST_MODE1, 0);    /* set default values */
1184         write_usb(hfc, HFCUSB_MST_MODE0, 1);    /* enable master mode */
1185
1186         /* init the fifos */
1187         write_usb(hfc, HFCUSB_F_THRES, (HFCUSB_TX_THRESHOLD/8) |((HFCUSB_RX_THRESHOLD/8) << 4));
1188
1189         fifo = hfc->fifos;
1190         for(i = 0; i < HFCUSB_NUM_FIFOS; i++)
1191         {
1192                 write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
1193                 fifo[i].skbuff = NULL;  /* init buffer pointer */
1194                 fifo[i].max_size = (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1195                 fifo[i].last_urblen=0;
1196                 write_usb(hfc, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));     // set 2 bit for D- & E-channel
1197                 write_usb(hfc, HFCUSB_CON_HDLC, ((i==HFCUSB_D_TX) ? 0x09 : 0x08));      // rx hdlc, enable IFF for D-channel
1198                 write_usb(hfc, HFCUSB_INC_RES_F, 2);    /* reset the fifo */
1199         }
1200
1201         write_usb(hfc, HFCUSB_CLKDEL, 0x0f);     /* clock delay value */
1202         write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
1203         write_usb(hfc, HFCUSB_STATES, 3);            /* enable state machine */
1204
1205         write_usb(hfc, HFCUSB_SCTRL_R, 0);           /* disable both B receivers */
1206         write_usb(hfc, HFCUSB_SCTRL, 0x40);          /* disable B transmitters + capacitive mode */
1207
1208         // set both B-channel to not connected
1209         hfc->b_mode[0]=L1_MODE_NULL;
1210         hfc->b_mode[1]=L1_MODE_NULL;
1211
1212         hfc->l1_activated=FALSE;
1213         hfc->led_state=0;
1214         hfc->led_new_data=0;
1215
1216         /* init the t3 timer */
1217         init_timer(&hfc->t3_timer);
1218         hfc->t3_timer.data = (long) hfc;
1219         hfc->t3_timer.function = (void *) l1_timer_expire_t3;
1220         /* init the t4 timer */
1221         init_timer(&hfc->t4_timer);
1222         hfc->t4_timer.data = (long) hfc;
1223         hfc->t4_timer.function = (void *) l1_timer_expire_t4;
1224         /* init the led timer */
1225         init_timer(&hfc->led_timer);
1226         hfc->led_timer.data = (long) hfc;
1227         hfc->led_timer.function = (void *) led_timer;
1228         // trigger 4 hz led timer
1229         hfc->led_timer.expires = jiffies + (LED_TIME * HZ) / 1000;
1230         if(!timer_pending(&hfc->led_timer)) add_timer(&hfc->led_timer);
1231
1232         // init the background machinery for control requests
1233         hfc->ctrl_read.bRequestType = 0xc0;
1234         hfc->ctrl_read.bRequest = 1;
1235         hfc->ctrl_read.wLength = 1;
1236         hfc->ctrl_write.bRequestType = 0x40;
1237         hfc->ctrl_write.bRequest = 0;
1238         hfc->ctrl_write.wLength = 0;
1239         usb_fill_control_urb(hfc->ctrl_urb, hfc->dev, hfc->ctrl_out_pipe,(u_char *) & hfc->ctrl_write, NULL, 0, ctrl_complete, hfc);
1240                                         
1241         /* Init All Fifos */
1242         for(i = 0; i < HFCUSB_NUM_FIFOS; i++)
1243         {
1244                 hfc->fifos[i].iso[0].purb = NULL;
1245                 hfc->fifos[i].iso[1].purb = NULL;
1246                 hfc->fifos[i].active = 0;
1247         }
1248
1249         // register like Germaschewski :
1250         hfc->d_if.owner = THIS_MODULE;
1251         hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
1252         hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
1253
1254         for (i=0; i<2; i++)
1255         {
1256                 hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX+i*2];
1257                 hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
1258                 p_b_if[i] = &hfc->b_if[i];
1259         }
1260         
1261         hfc->protocol = 2;  /* default EURO ISDN, should be a module_param */
1262         hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
1263         
1264         for (i=0; i<4; i++)
1265                 hfc->fifos[i].hif=&p_b_if[i/2]->ifc;
1266         for (i=4; i<8; i++)
1267                 hfc->fifos[i].hif=&hfc->d_if.ifc;
1268
1269         // 3 (+1) INT IN + 3 ISO OUT
1270         if(hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO)
1271         {
1272                 start_int_fifo(hfc->fifos + HFCUSB_D_RX);       // Int IN D-fifo
1273                 if(hfc->fifos[HFCUSB_PCM_RX].pipe) start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);  // E-fifo
1274                 start_int_fifo(hfc->fifos + HFCUSB_B1_RX);      // Int IN B1-fifo
1275                 start_int_fifo(hfc->fifos + HFCUSB_B2_RX);      // Int IN B2-fifo
1276         }
1277
1278         // 3 (+1) ISO IN + 3 ISO OUT
1279         if(hfc->cfg_used==CNF_3ISO3ISO || hfc->cfg_used==CNF_4ISO3ISO)
1280         {
1281                 start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D, rx_iso_complete,16);
1282                 if(hfc->fifos[HFCUSB_PCM_RX].pipe) start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX, ISOC_PACKETS_D, rx_iso_complete,16);
1283                 start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B, rx_iso_complete,16);
1284                 start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B, rx_iso_complete,16);
1285         }
1286
1287         start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D, tx_iso_complete,1);
1288         start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B, tx_iso_complete,1);
1289         start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B, tx_iso_complete,1);
1290
1291         handle_led(hfc,LED_POWER_ON);
1292
1293         return(0);
1294 }       /* usb_init */
1295
1296
1297 /****************************************/
1298 /* data defining the devices to be used */
1299 /****************************************/
1300 // static __devinitdata const struct usb_device_id hfc_usb_idtab[3] = {
1301 static struct usb_device_id hfc_usb_idtab[] = {
1302         {USB_DEVICE(0x7b0, 0x0007)},    /* Billion USB TA 2 */
1303         {USB_DEVICE(0x742, 0x2008)},    /* Stollmann USB TA */
1304         {USB_DEVICE(0x959, 0x2bd0)},    /* Colognechip USB eval TA */
1305         {USB_DEVICE(0x8e3, 0x0301)},    /* OliTec ISDN USB */
1306         {USB_DEVICE(0x675, 0x1688)},    /* DrayTec ISDN USB */
1307         {USB_DEVICE(0x7fa, 0x0846)},    /* Bewan ISDN USB TA */
1308         {}                              /* end with an all-zeroes entry */
1309 };
1310
1311 MODULE_AUTHOR("Peter Sprenger (sprenger@moving-byters.de)/Martin Bachem (info@colognechip.com)");
1312 MODULE_DESCRIPTION("HFC I4L USB driver");
1313 MODULE_DEVICE_TABLE(usb, hfc_usb_idtab);
1314 MODULE_LICENSE("GPL");
1315
1316 #define EP_NUL 1    // Endpoint at this position not allowed
1317 #define EP_NOP 2        // all type of endpoints allowed at this position
1318 #define EP_ISO 3        // Isochron endpoint mandatory at this position
1319 #define EP_BLK 4        // Bulk endpoint mandatory at this position
1320 #define EP_INT 5        // Interrupt endpoint mandatory at this position
1321
1322 // this array represents all endpoints possible in the HCF-USB
1323 // the last 2 entries are the configuration number and the minimum interval for Interrupt endpoints
1324 int validconf[][18]=
1325 {
1326         // INT in, ISO out config
1327         {EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NOP,EP_INT,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_NUL,EP_NUL,CNF_4INT3ISO,2},
1328         {EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NUL,EP_NUL,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_NUL,EP_NUL,CNF_3INT3ISO,2},
1329         // ISO in, ISO out config
1330         {EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_NOP,EP_ISO,CNF_4ISO3ISO,2},
1331         {EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_NUL,EP_NUL,CNF_3ISO3ISO,2},
1332         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}       // EOL element
1333 };
1334
1335 // string description of chosen config
1336 char *conf_str[]=
1337 {
1338         "4 Interrupt IN + 3 Isochron OUT",
1339         "3 Interrupt IN + 3 Isochron OUT",
1340         "4 Isochron IN + 3 Isochron OUT",
1341         "3 Isochron IN + 3 Isochron OUT"
1342 };
1343
1344
1345 /*************************************************/
1346 /* function called to probe a new plugged device */
1347 /*************************************************/
1348 static int hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1349 {
1350         struct usb_device *dev= interface_to_usbdev(intf);
1351         hfcusb_data *context;
1352         struct usb_host_interface *iface = intf->cur_altsetting;
1353         struct usb_host_interface *iface_used = NULL;
1354         struct usb_host_endpoint *ep;
1355         int ifnum = iface->desc.bInterfaceNumber;
1356         int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found, cidx, ep_addr;
1357         int cmptbl[16],small_match,iso_packet_size,packet_size,alt_used=0;
1358
1359 //        usb_show_device(dev);
1360 //      usb_show_device_descriptor(&dev->descriptor);
1361 //      usb_show_interface_descriptor(&iface->desc);
1362         vend_idx=0xffff;
1363         for(i=0;vdata[i].vendor;i++)
1364         {
1365                 if(dev->descriptor.idVendor==vdata[i].vendor && dev->descriptor.idProduct==vdata[i].prod_id) vend_idx=i;
1366         }
1367         
1368
1369 #ifdef VERBOSE_USB_DEBUG        
1370         printk(KERN_INFO "HFC-USB: probing interface(%d) actalt(%d) minor(%d)\n",
1371                 ifnum, iface->desc.bAlternateSetting, intf->minor);
1372 #endif
1373
1374         if (vend_idx != 0xffff) {
1375 #ifdef VERBOSE_USB_DEBUG
1376                 printk(KERN_INFO "HFC-USB: found vendor idx:%d  name:%s\n",vend_idx,vdata[vend_idx].vend_name);
1377 #endif
1378                 /* if vendor and product ID is OK, start probing a matching alternate setting ... */
1379                 alt_idx = 0;
1380                 small_match=0xffff;
1381                 // default settings
1382                 iso_packet_size=16;
1383                 packet_size=64;
1384
1385                 while (alt_idx < intf->num_altsetting) {
1386                         iface = intf->altsetting + alt_idx;
1387                         probe_alt_setting = iface->desc.bAlternateSetting;
1388                         cfg_used=0;
1389
1390 #ifdef VERBOSE_USB_DEBUG
1391                         printk(KERN_INFO "HFC-USB: test alt_setting %d\n", probe_alt_setting);
1392 #endif
1393                         // check for config EOL element
1394                         while (validconf[cfg_used][0]) {
1395                                 cfg_found=TRUE;
1396                                 vcf=validconf[cfg_used];
1397                                 ep = iface->endpoint;   /* first endpoint descriptor */
1398
1399 #ifdef VERBOSE_USB_DEBUG
1400                                 printk(KERN_INFO "HFC-USB: (if=%d alt=%d cfg_used=%d)\n",
1401                                         ifnum, probe_alt_setting, cfg_used);
1402 #endif
1403                                 // copy table
1404                                 memcpy(cmptbl,vcf,16*sizeof(int));
1405
1406                                 // check for all endpoints in this alternate setting
1407                                 for (i=0; i < iface->desc.bNumEndpoints; i++) {
1408                                         ep_addr = ep->desc.bEndpointAddress;
1409                                         idx = ((ep_addr & 0x7f)-1)*2;   /* get endpoint base */
1410                                         if (ep_addr & 0x80)
1411                                                 idx++;
1412                                         attr = ep->desc.bmAttributes;
1413
1414                                         if (cmptbl[idx] == EP_NUL) {
1415                                                 printk(KERN_INFO "HFC-USB: cfg_found=FALSE in idx:%d  attr:%d  cmptbl[%d]:%d\n",
1416                                                         idx, attr, idx, cmptbl[idx]);
1417                                                 cfg_found = FALSE;
1418                                         }
1419
1420                                         if (attr == USB_ENDPOINT_XFER_INT && cmptbl[idx] == EP_INT)
1421                                                 cmptbl[idx] = EP_NUL;
1422                                         if (attr == USB_ENDPOINT_XFER_BULK && cmptbl[idx] == EP_BLK)
1423                                                 cmptbl[idx] = EP_NUL;
1424                                         if (attr == USB_ENDPOINT_XFER_ISOC && cmptbl[idx] == EP_ISO)
1425                                                 cmptbl[idx] = EP_NUL;
1426
1427                                         // check if all INT endpoints match minimum interval
1428                                         if (attr == USB_ENDPOINT_XFER_INT && ep->desc.bInterval < vcf[17]) {
1429 #ifdef VERBOSE_USB_DEBUG
1430                                                 if (cfg_found)
1431                                                         printk(KERN_INFO "HFC-USB: Interrupt Endpoint interval < %d found - skipping config\n",
1432                                                                 vcf[17]);
1433 #endif
1434                                                 cfg_found = FALSE;
1435                                         }
1436
1437                                         ep++;
1438                                 }
1439
1440                                 for (i = 0; i < 16; i++) {
1441                                         // printk(KERN_INFO "HFC-USB: cmptbl[%d]:%d\n", i, cmptbl[i]);
1442
1443                                         // all entries must be EP_NOP or EP_NUL for a valid config
1444                                         if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
1445                                                 cfg_found = FALSE;
1446                                 }
1447
1448                                 // we check for smallest match, to provide configuration priority
1449                                 // configurations with smaller index have higher priority
1450                                 if (cfg_found) {
1451                                         if (cfg_used < small_match) {
1452                                                 small_match = cfg_used;
1453                                                 alt_used = probe_alt_setting;
1454                                                 iface_used = iface;
1455                                         }
1456 #ifdef VERBOSE_USB_DEBUG
1457                                         printk(KERN_INFO "HFC-USB: small_match=%x %x\n", small_match, alt_used);
1458 #endif
1459                                 }
1460
1461                                 cfg_used++;
1462                         }
1463
1464                         alt_idx++;
1465                 }               /* (alt_idx < intf->num_altsetting) */
1466 #ifdef VERBOSE_USB_DEBUG
1467                 printk(KERN_INFO "HFC-USB: final small_match=%x alt_used=%x\n",small_match, alt_used);
1468 #endif
1469                 // yiipiee, we found a valid config
1470                 if (small_match != 0xffff) {
1471                         iface = iface_used;
1472
1473                         if (!(context = kmalloc(sizeof(hfcusb_data), GFP_KERNEL)))
1474                                 return(-ENOMEM);  /* got no mem */
1475                         memset(context, 0, sizeof(hfcusb_data));        /* clear the structure */
1476
1477                         ep = iface->endpoint;   /* first endpoint descriptor */
1478                         vcf = validconf[small_match];
1479
1480                         for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1481                                 ep_addr = ep->desc.bEndpointAddress;
1482                                 idx = ((ep_addr & 0x7f)-1)*2;   /* get endpoint base */
1483                                 if (ep_addr & 0x80)
1484                                         idx++;
1485                                 cidx = idx & 7;
1486                                 attr = ep->desc.bmAttributes;
1487
1488                                 // only initialize used endpoints
1489                                 if (vcf[idx] != EP_NOP && vcf[idx] != EP_NUL) {
1490                                         switch (attr) {
1491                                                 case USB_ENDPOINT_XFER_INT:
1492                                                         context->fifos[cidx].pipe = usb_rcvintpipe(dev, ep->desc.bEndpointAddress);
1493                                                         context->fifos[cidx].usb_transfer_mode = USB_INT;
1494                                                         packet_size = ep->desc.wMaxPacketSize; // remember max packet size
1495 #ifdef VERBOSE_USB_DEBUG
1496                                                         printk (KERN_INFO "HFC-USB: Interrupt-In Endpoint found %d ms(idx:%d cidx:%d)!\n",
1497                                                                 ep->desc.bInterval, idx, cidx);
1498 #endif
1499                                                         break;
1500                                                 case USB_ENDPOINT_XFER_BULK:
1501                                                         if (ep_addr & 0x80)
1502                                                                 context->fifos[cidx].pipe = usb_rcvbulkpipe(dev, ep->desc.bEndpointAddress);
1503                                                         else
1504                                                                 context->fifos[cidx].pipe = usb_sndbulkpipe(dev, ep->desc.bEndpointAddress);
1505                                                         context->fifos[cidx].usb_transfer_mode = USB_BULK;
1506                                                         packet_size = ep->desc.wMaxPacketSize; // remember max packet size
1507 #ifdef VERBOSE_USB_DEBUG
1508                                                         printk (KERN_INFO "HFC-USB: Bulk Endpoint found (idx:%d cidx:%d)!\n",
1509                                                                 idx, cidx);
1510 #endif
1511                                                         break;
1512                                                 case USB_ENDPOINT_XFER_ISOC:
1513                                                         if (ep_addr & 0x80)
1514                                                                 context->fifos[cidx].pipe = usb_rcvisocpipe(dev, ep->desc.bEndpointAddress);
1515                                                         else
1516                                                                 context->fifos[cidx].pipe = usb_sndisocpipe(dev, ep->desc.bEndpointAddress);
1517                                                         context->fifos[cidx].usb_transfer_mode = USB_ISOC;
1518                                                         iso_packet_size = ep->desc.wMaxPacketSize; // remember max packet size
1519 #ifdef VERBOSE_USB_DEBUG
1520                                                         printk (KERN_INFO "HFC-USB: ISO Endpoint found (idx:%d cidx:%d)!\n",
1521                                                                 idx, cidx);
1522 #endif
1523                                                         break;
1524                                                 default:
1525                                                         context->fifos[cidx].pipe = 0;  /* reset data */
1526                                         }       /* switch attribute */
1527
1528                                         if (context->fifos[cidx].pipe) {
1529                                                 context->fifos[cidx].fifonum = cidx;
1530                                                 context->fifos[cidx].hfc = context;
1531                                                 context->fifos[cidx].usb_packet_maxlen = ep->desc.wMaxPacketSize;
1532                                                 context->fifos[cidx].intervall = ep->desc.bInterval;
1533                                                 context->fifos[cidx].skbuff = NULL;
1534 #ifdef VERBOSE_USB_DEBUG
1535                                                 printk (KERN_INFO "HFC-USB: fifo%d pktlen %d interval %d\n",
1536                                                         context->fifos[cidx].fifonum,
1537                                                         context->fifos[cidx].usb_packet_maxlen,
1538                                                         context->fifos[cidx].intervall);
1539 #endif
1540                                         }
1541                                 }
1542
1543                                 ep++;
1544                         }
1545
1546                         // now share our luck
1547                         context->dev = dev;                                             /* save device */
1548                         context->if_used = ifnum;                                       /* save used interface */
1549                         context->alt_used = alt_used;                                   /* and alternate config */
1550                         context->ctrl_paksize = dev->descriptor.bMaxPacketSize0;        /* control size */
1551                         context->cfg_used=vcf[16];                                      // store used config
1552                         context->vend_idx=vend_idx;                                     // store found vendor
1553                         context->packet_size=packet_size;
1554                         context->iso_packet_size=iso_packet_size;
1555
1556                         /* create the control pipes needed for register access */
1557                         context->ctrl_in_pipe = usb_rcvctrlpipe(context->dev, 0);
1558                         context->ctrl_out_pipe = usb_sndctrlpipe(context->dev, 0);
1559                         context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1560
1561                         printk(KERN_INFO "HFC-USB: detected \"%s\" configuration: %s (if=%d alt=%d)\n",
1562                                 vdata[vend_idx].vend_name, conf_str[small_match], context->if_used, context->alt_used);
1563
1564                         /* init the chip and register the driver */
1565                         if (usb_init(context))
1566                         {
1567                                 if (context->ctrl_urb) {
1568                                         usb_unlink_urb(context->ctrl_urb);
1569                                         usb_free_urb(context->ctrl_urb);
1570                                         context->ctrl_urb = NULL;
1571                                 }
1572                                 kfree(context);
1573                                 return(-EIO);
1574                         }
1575                         usb_set_intfdata(intf, context);
1576                         return(0);
1577                 } 
1578         }
1579         return(-EIO);
1580 }
1581
1582 /****************************************************/
1583 /* function called when an active device is removed */
1584 /****************************************************/
1585 static void hfc_usb_disconnect(struct usb_interface *intf)
1586 {
1587         hfcusb_data *context = usb_get_intfdata(intf);
1588         int i;
1589
1590         printk(KERN_INFO "HFC-USB: device disconnect\n");
1591         
1592         usb_set_intfdata(intf, NULL);
1593         if (!context)
1594                 return;
1595         if (timer_pending(&context->t3_timer))
1596                 del_timer(&context->t3_timer);
1597         if (timer_pending(&context->t4_timer))
1598                 del_timer(&context->t4_timer);
1599         if (timer_pending(&context->led_timer))
1600                 del_timer(&context->led_timer);
1601
1602         hisax_unregister(&context->d_if);
1603
1604         /* tell all fifos to terminate */
1605         for(i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1606                 if(context->fifos[i].usb_transfer_mode == USB_ISOC) {
1607                         if(context->fifos[i].active > 0) {
1608                                 stop_isoc_chain(&context->fifos[i]);
1609 #ifdef VERBOSE_USB_DEBUG
1610                                 printk (KERN_INFO "HFC-USB: hfc_usb_disconnect: stopping ISOC chain Fifo no %i\n", i);
1611 #endif
1612                         }
1613                 } else {
1614                         if(context->fifos[i].active > 0) {
1615                                 context->fifos[i].active = 0;
1616 #ifdef VERBOSE_USB_DEBUG
1617                                 printk (KERN_INFO "HFC-USB: hfc_usb_disconnect: unlinking URB for Fifo no %i\n", i);
1618 #endif
1619                         }
1620                         if (context->fifos[i].urb) {
1621                                 usb_unlink_urb(context->fifos[i].urb);
1622                                 usb_free_urb(context->fifos[i].urb);
1623                                 context->fifos[i].urb = NULL;
1624                         }
1625                 }
1626                 context->fifos[i].active = 0;
1627         }
1628         if (context->ctrl_urb) {
1629                 usb_unlink_urb(context->ctrl_urb);
1630                 usb_free_urb(context->ctrl_urb);
1631                 context->ctrl_urb = NULL;
1632         }
1633         kfree(context);         /* free our structure again */
1634 }                               /* hfc_usb_disconnect */
1635
1636
1637 /************************************/
1638 /* our driver information structure */
1639 /************************************/
1640 static struct usb_driver hfc_drv = {
1641         .owner =        THIS_MODULE,
1642         .name =         "hfc_usb",
1643         .id_table =     hfc_usb_idtab,
1644         .probe =        hfc_usb_probe,
1645         .disconnect =   hfc_usb_disconnect,
1646 };
1647
1648 static void __exit hfc_usb_exit(void)
1649 {
1650 #ifdef VERBOSE_USB_DEBUG
1651         printk ("HFC-USB: calling \"hfc_usb_exit\" ...\n");
1652 #endif
1653         usb_deregister(&hfc_drv);       /* release our driver */
1654         printk(KERN_INFO "HFC-USB module removed\n");
1655 }
1656
1657 static int __init hfc_usb_init(void)
1658 {
1659         printk ("HFC-USB: driver module revision %s loaded\n", hfcusb_revision);
1660
1661         if(usb_register(&hfc_drv))
1662         {
1663                 printk(KERN_INFO "HFC-USB: Unable to register HFC-USB module at usb stack\n");
1664                 return(-1);                /* unable to register */
1665         }
1666         return(0);
1667 }
1668
1669 module_init(hfc_usb_init);
1670 module_exit(hfc_usb_exit);