f8457ef488260bc4e4af7db6a3cdc9c672c15b5a
[linux-2.6.git] / drivers / isdn / hisax / hfc_usb.c
1 /*
2  * hfc_usb.c
3  *
4  * $Id: hfc_usb.c,v 4.36 2005/04/08 09:55:13 martinb1 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: 4.36 $ $Date: 2005/04/08 09:55:13 $ ";
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 "<unkown 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                     hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg;
239                 hfc->ctrl_write.wValue =
240                     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 = 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 (dev->descriptor.idVendor == hfcusb_idtab[i].idVendor
1377                     && dev->descriptor.idProduct ==
1378                     hfcusb_idtab[i].idProduct) {
1379                         vend_idx = i;
1380                         continue;
1381                 }
1382         }
1383
1384 #ifdef CONFIG_HISAX_DEBUG
1385         DBG(USB_DBG,
1386             "HFC-USB: probing interface(%d) actalt(%d) minor(%d)\n", ifnum,
1387             iface->desc.bAlternateSetting, intf->minor);
1388 #endif
1389         printk(KERN_INFO
1390                "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
1391                ifnum, iface->desc.bAlternateSetting, intf->minor);
1392
1393         if (vend_idx != 0xffff) {
1394                 /* if vendor and product ID is OK, start probing alternate settings */
1395                 alt_idx = 0;
1396                 small_match = 0xffff;
1397
1398                 /* default settings */
1399                 iso_packet_size = 16;
1400                 packet_size = 64;
1401
1402                 while (alt_idx < intf->num_altsetting) {
1403                         iface = intf->altsetting + alt_idx;
1404                         probe_alt_setting = iface->desc.bAlternateSetting;
1405                         cfg_used = 0;
1406
1407                         /* check for config EOL element */
1408                         while (validconf[cfg_used][0]) {
1409                                 cfg_found = TRUE;
1410                                 vcf = validconf[cfg_used];
1411                                 /* first endpoint descriptor */
1412                                 ep = iface->endpoint;
1413 #ifdef CONFIG_HISAX_DEBUG
1414                                 DBG(USB_DBG,
1415                                     "HFC-S USB: (if=%d alt=%d cfg_used=%d)\n",
1416                                     ifnum, probe_alt_setting, cfg_used);
1417 #endif
1418                                 memcpy(cmptbl, vcf, 16 * sizeof(int));
1419
1420                                 /* check for all endpoints in this alternate setting */
1421                                 for (i = 0; i < iface->desc.bNumEndpoints;
1422                                      i++) {
1423                                         ep_addr =
1424                                             ep->desc.bEndpointAddress;
1425                                         /* get endpoint base */
1426                                         idx = ((ep_addr & 0x7f) - 1) * 2;
1427                                         if (ep_addr & 0x80)
1428                                                 idx++;
1429                                         attr = ep->desc.bmAttributes;
1430                                         if (cmptbl[idx] == EP_NUL) {
1431                                                 cfg_found = FALSE;
1432                                         }
1433                                         if (attr == USB_ENDPOINT_XFER_INT
1434                                             && cmptbl[idx] == EP_INT)
1435                                                 cmptbl[idx] = EP_NUL;
1436                                         if (attr == USB_ENDPOINT_XFER_BULK
1437                                             && cmptbl[idx] == EP_BLK)
1438                                                 cmptbl[idx] = EP_NUL;
1439                                         if (attr == USB_ENDPOINT_XFER_ISOC
1440                                             && cmptbl[idx] == EP_ISO)
1441                                                 cmptbl[idx] = EP_NUL;
1442
1443                                         /* check if all INT endpoints match minimum interval */
1444                                         if (attr == USB_ENDPOINT_XFER_INT
1445                                             && ep->desc.bInterval <
1446                                             vcf[17]) {
1447 #ifdef CONFIG_HISAX_DEBUG
1448                                                 if (cfg_found)
1449                                                         DBG(USB_DBG,
1450                                                             "HFC-S USB: Interrupt Endpoint interval < %d found - skipping config",
1451                                                             vcf[17]);
1452 #endif
1453                                                 cfg_found = FALSE;
1454                                         }
1455                                         ep++;
1456                                 }
1457                                 for (i = 0; i < 16; i++) {
1458                                         /* all entries must be EP_NOP or EP_NUL for a valid config */
1459                                         if (cmptbl[i] != EP_NOP
1460                                             && cmptbl[i] != EP_NUL)
1461                                                 cfg_found = FALSE;
1462                                 }
1463                                 if (cfg_found) {
1464                                         if (cfg_used < small_match) {
1465                                                 small_match = cfg_used;
1466                                                 alt_used =
1467                                                     probe_alt_setting;
1468                                                 iface_used = iface;
1469                                         }
1470 #ifdef CONFIG_HISAX_DEBUG
1471                                         DBG(USB_DBG,
1472                                             "HFC-USB: small_match=%x %x\n",
1473                                             small_match, alt_used);
1474 #endif
1475                                 }
1476                                 cfg_used++;
1477                         }
1478                         alt_idx++;
1479                 }               /* (alt_idx < intf->num_altsetting) */
1480
1481                 /* found a valid USB Ta Endpint config */
1482                 if (small_match != 0xffff) {
1483                         iface = iface_used;
1484                         if (!
1485                             (context =
1486                              kmalloc(sizeof(hfcusb_data), GFP_KERNEL)))
1487                                 return (-ENOMEM);       /* got no mem */
1488                         memset(context, 0, sizeof(hfcusb_data));
1489
1490                         ep = iface->endpoint;
1491                         vcf = validconf[small_match];
1492
1493                         for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1494                                 ep_addr = ep->desc.bEndpointAddress;
1495                                 /* get endpoint base */
1496                                 idx = ((ep_addr & 0x7f) - 1) * 2;
1497                                 if (ep_addr & 0x80)
1498                                         idx++;
1499                                 cidx = idx & 7;
1500                                 attr = ep->desc.bmAttributes;
1501
1502                                 /* init Endpoints */
1503                                 if (vcf[idx] != EP_NOP
1504                                     && vcf[idx] != EP_NUL) {
1505                                         switch (attr) {
1506                                                 case USB_ENDPOINT_XFER_INT:
1507                                                         context->
1508                                                             fifos[cidx].
1509                                                             pipe =
1510                                                             usb_rcvintpipe
1511                                                             (dev,
1512                                                              ep->desc.
1513                                                              bEndpointAddress);
1514                                                         context->
1515                                                             fifos[cidx].
1516                                                             usb_transfer_mode
1517                                                             = USB_INT;
1518                                                         packet_size =
1519                                                             ep->desc.
1520                                                             wMaxPacketSize;
1521                                                         break;
1522                                                 case USB_ENDPOINT_XFER_BULK:
1523                                                         if (ep_addr & 0x80)
1524                                                                 context->
1525                                                                     fifos
1526                                                                     [cidx].
1527                                                                     pipe =
1528                                                                     usb_rcvbulkpipe
1529                                                                     (dev,
1530                                                                      ep->
1531                                                                      desc.
1532                                                                      bEndpointAddress);
1533                                                         else
1534                                                                 context->
1535                                                                     fifos
1536                                                                     [cidx].
1537                                                                     pipe =
1538                                                                     usb_sndbulkpipe
1539                                                                     (dev,
1540                                                                      ep->
1541                                                                      desc.
1542                                                                      bEndpointAddress);
1543                                                         context->
1544                                                             fifos[cidx].
1545                                                             usb_transfer_mode
1546                                                             = USB_BULK;
1547                                                         packet_size =
1548                                                             ep->desc.
1549                                                             wMaxPacketSize;
1550                                                         break;
1551                                                 case USB_ENDPOINT_XFER_ISOC:
1552                                                         if (ep_addr & 0x80)
1553                                                                 context->
1554                                                                     fifos
1555                                                                     [cidx].
1556                                                                     pipe =
1557                                                                     usb_rcvisocpipe
1558                                                                     (dev,
1559                                                                      ep->
1560                                                                      desc.
1561                                                                      bEndpointAddress);
1562                                                         else
1563                                                                 context->
1564                                                                     fifos
1565                                                                     [cidx].
1566                                                                     pipe =
1567                                                                     usb_sndisocpipe
1568                                                                     (dev,
1569                                                                      ep->
1570                                                                      desc.
1571                                                                      bEndpointAddress);
1572                                                         context->
1573                                                             fifos[cidx].
1574                                                             usb_transfer_mode
1575                                                             = USB_ISOC;
1576                                                         iso_packet_size =
1577                                                             ep->desc.
1578                                                             wMaxPacketSize;
1579                                                         break;
1580                                                 default:
1581                                                         context->
1582                                                             fifos[cidx].
1583                                                             pipe = 0;
1584                                         }       /* switch attribute */
1585
1586                                         if (context->fifos[cidx].pipe) {
1587                                                 context->fifos[cidx].
1588                                                     fifonum = cidx;
1589                                                 context->fifos[cidx].hfc =
1590                                                     context;
1591                                                 context->fifos[cidx].
1592                                                     usb_packet_maxlen =
1593                                                     ep->desc.
1594                                                     wMaxPacketSize;
1595                                                 context->fifos[cidx].
1596                                                     intervall =
1597                                                     ep->desc.bInterval;
1598                                                 context->fifos[cidx].
1599                                                     skbuff = NULL;
1600                                         }
1601                                 }
1602                                 ep++;
1603                         }
1604                         context->dev = dev;     /* save device */
1605                         context->if_used = ifnum;       /* save used interface */
1606                         context->alt_used = alt_used;   /* and alternate config */
1607                         context->ctrl_paksize = dev->descriptor.bMaxPacketSize0;        /* control size */
1608                         context->cfg_used = vcf[16];    /* store used config */
1609                         context->vend_idx = vend_idx;   /* store found vendor */
1610                         context->packet_size = packet_size;
1611                         context->iso_packet_size = iso_packet_size;
1612
1613                         /* create the control pipes needed for register access */
1614                         context->ctrl_in_pipe =
1615                             usb_rcvctrlpipe(context->dev, 0);
1616                         context->ctrl_out_pipe =
1617                             usb_sndctrlpipe(context->dev, 0);
1618                         context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1619
1620                         driver_info =
1621                             (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
1622                             driver_info;
1623                         printk(KERN_INFO "HFC-S USB: detected \"%s\"\n",
1624                                driver_info->vend_name);
1625 #ifdef CONFIG_HISAX_DEBUG
1626                         DBG(USB_DBG,
1627                             "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d)\n",
1628                             conf_str[small_match], context->if_used,
1629                             context->alt_used);
1630                         printk(KERN_INFO
1631                                "HFC-S USB: E-channel (\"ECHO:\") logging ");
1632                         if (validconf[small_match][18])
1633                                 printk(" possible\n");
1634                         else
1635                                 printk("NOT possible\n");
1636 #endif
1637                         /* init the chip and register the driver */
1638                         if (usb_init(context)) {
1639                                 if (context->ctrl_urb) {
1640                                         usb_unlink_urb(context->ctrl_urb);
1641                                         usb_free_urb(context->ctrl_urb);
1642                                         context->ctrl_urb = NULL;
1643                                 }
1644                                 kfree(context);
1645                                 return (-EIO);
1646                         }
1647                         usb_set_intfdata(intf, context);
1648                         return (0);
1649                 }
1650         } else {
1651                 printk(KERN_INFO
1652                        "HFC-S USB: no valid vendor found in USB descriptor\n");
1653         }
1654         return (-EIO);
1655 }
1656
1657 /****************************************************/
1658 /* function called when an active device is removed */
1659 /****************************************************/
1660 static void
1661 hfc_usb_disconnect(struct usb_interface
1662                    *intf)
1663 {
1664         hfcusb_data *context = usb_get_intfdata(intf);
1665         int i;
1666         printk(KERN_INFO "HFC-S USB: device disconnect\n");
1667         context->disc_flag = TRUE;
1668         usb_set_intfdata(intf, NULL);
1669         if (!context)
1670                 return;
1671         if (timer_pending(&context->t3_timer))
1672                 del_timer(&context->t3_timer);
1673         if (timer_pending(&context->t4_timer))
1674                 del_timer(&context->t4_timer);
1675         /* tell all fifos to terminate */
1676         for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1677                 if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
1678                         if (context->fifos[i].active > 0) {
1679                                 stop_isoc_chain(&context->fifos[i]);
1680 #ifdef CONFIG_HISAX_DEBUG
1681                                 DBG(USB_DBG,
1682                                     "HFC-S USB: hfc_usb_disconnect: stopping ISOC chain Fifo no %i",
1683                                     i);
1684 #endif
1685                         }
1686                 } else {
1687                         if (context->fifos[i].active > 0) {
1688                                 context->fifos[i].active = 0;
1689 #ifdef CONFIG_HISAX_DEBUG
1690                                 DBG(USB_DBG,
1691                                     "HFC-S USB: hfc_usb_disconnect: unlinking URB for Fifo no %i",
1692                                     i);
1693 #endif
1694                         }
1695                         if (context->fifos[i].urb) {
1696                                 usb_unlink_urb(context->fifos[i].urb);
1697                                 usb_free_urb(context->fifos[i].urb);
1698                                 context->fifos[i].urb = NULL;
1699                         }
1700                 }
1701                 context->fifos[i].active = 0;
1702         }
1703         /* wait for all URBS to terminate */
1704         mdelay(10);
1705         if (context->ctrl_urb) {
1706                 usb_unlink_urb(context->ctrl_urb);
1707                 usb_free_urb(context->ctrl_urb);
1708                 context->ctrl_urb = NULL;
1709         }
1710         hisax_unregister(&context->d_if);
1711         kfree(context);         /* free our structure again */
1712 }                               /* hfc_usb_disconnect */
1713
1714 /************************************/
1715 /* our driver information structure */
1716 /************************************/
1717 static struct usb_driver hfc_drv = {
1718         .owner = THIS_MODULE,
1719         .name  = "hfc_usb",
1720         .id_table = hfcusb_idtab,
1721         .probe = hfc_usb_probe,
1722         .disconnect = hfc_usb_disconnect,
1723 };
1724 static void __exit
1725 hfc_usb_exit(void)
1726 {
1727 #ifdef CONFIG_HISAX_DEBUG
1728         DBG(USB_DBG, "HFC-S USB: calling \"hfc_usb_exit\" ...");
1729 #endif
1730         usb_deregister(&hfc_drv);       /* release our driver */
1731         printk(KERN_INFO "HFC-S USB: module removed\n");
1732 }
1733
1734 static int __init
1735 hfc_usb_init(void)
1736 {
1737 #ifndef CONFIG_HISAX_DEBUG
1738         unsigned int debug = -1;
1739 #endif
1740         char revstr[30], datestr[30], dummy[30];
1741         sscanf(hfcusb_revision,
1742                "%s %s $ %s %s %s $ ", dummy, revstr,
1743                dummy, datestr, dummy);
1744         printk(KERN_INFO
1745                "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
1746                revstr, datestr, debug);
1747         if (usb_register(&hfc_drv)) {
1748                 printk(KERN_INFO
1749                        "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
1750                 return (-1);    /* unable to register */
1751         }
1752         return (0);
1753 }
1754
1755 module_init(hfc_usb_init);
1756 module_exit(hfc_usb_exit);
1757 MODULE_AUTHOR(DRIVER_AUTHOR);
1758 MODULE_DESCRIPTION(DRIVER_DESC);
1759 MODULE_LICENSE("GPL");
1760 MODULE_DEVICE_TABLE(usb, hfcusb_idtab);