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