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