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