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