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