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