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