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