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