[PATCH] i4l: update hfc_usb driver
[linux-2.6/suspend2-2.6.18.git] / drivers / isdn / hisax / hfc_usb.c
blob32bf0d5d0f9a0d0692414c683a6547c3050be5e9
1 /*
2 * hfc_usb.c
4 * $Id: hfc_usb.c,v 4.36 2005/04/08 09:55:13 martinb1 Exp $
6 * modular HiSax ISDN driver for Colognechip HFC-S USB chip
8 * Authors : Peter Sprenger (sprenger@moving-bytes.de)
9 * Martin Bachem (info@colognechip.com)
11 * based on the first hfc_usb driver of
12 * Werner Cornelius (werner@isdn-development.de)
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.
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.
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.
28 * See Version Histroy at the bottom of this file
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"
47 static const char *hfcusb_revision =
48 "$Revision: 4.36 $ $Date: 2005/04/08 09:55:13 $ ";
50 /* Hisax debug support
51 * use "modprobe debug=x" where x is bitfield of USB_DBG & ISDN_DBG
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
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;
69 /****************************************/
70 /* data defining the devices to be used */
71 /****************************************/
72 static struct usb_device_id hfcusb_idtab[] = {
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)"}),
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"}),
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"}),
95 .idVendor = 0x0742,
96 .idProduct = 0x2008,
97 .driver_info = (unsigned long) &((hfcsusb_vdata)
98 {LED_SCHEME1, {4, 0, 2, 1},
99 "Stollmann USB TA"}),
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"}),
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"}),
116 .idVendor = 0x08e3,
117 .idProduct = 0x0301,
118 .driver_info = (unsigned long) &((hfcsusb_vdata)
119 {LED_SCHEME1, {2, 0, 1, 4},
120 "Olitec USB RNIS"}),
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"}),
130 .idVendor = 0x07fa,
131 .idProduct = 0x0847,
132 .driver_info = (unsigned long) &((hfcsusb_vdata)
133 {LED_SCHEME1, {0x80, -64, -32, -16},
134 "Djinn Numeris USB"}),
137 .idVendor = 0x07b0,
138 .idProduct = 0x0006,
139 .driver_info = (unsigned long) &((hfcsusb_vdata)
140 {LED_SCHEME1, {0x80, -64, -32, -16},
141 "Twister ISDN TA"}),
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;
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? */
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 */
177 } usb_fifo;
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;
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;
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 */
205 struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
206 struct usb_ctrlrequest ctrl_read; /* same for read request */
208 __u8 old_led_state, led_state, led_new_data, led_b_active;
210 volatile __u8 threshold_mask; /* threshold actually reported */
211 volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
213 usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
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;
221 static void collect_rx_frame(usb_fifo * fifo, __u8 * data, int len,
222 int finish);
225 static inline const char *
226 symbolic(struct hfcusb_symbolic_list list[], const int num)
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>";
236 /******************************************************/
237 /* start next background transfer for control channel */
238 /******************************************************/
239 static void
240 ctrl_start_transfer(hfcusb_data * hfc)
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;
252 usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
254 } /* ctrl_start_transfer */
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)
263 ctrl_buft *buf;
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 */
278 static int
279 control_action_handler(hfcusb_data * hfc, int reg, int val, int action)
281 if (!action)
282 return (1); /* no action defined */
283 return (0);
286 /***************************************************************/
287 /* control completion routine handling background control cmds */
288 /***************************************************************/
289 static void
290 ctrl_complete(struct urb *urb, struct pt_regs *regs)
292 hfcusb_data *hfc = (hfcusb_data *) urb->context;
293 ctrl_buft *buf;
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);
301 hfc->ctrl_cnt--; /* decrement actual count */
302 if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
303 hfc->ctrl_out_idx = 0; /* pointer wrap */
305 ctrl_start_transfer(hfc); /* start next transfer */
307 } /* ctrl_complete */
309 /***************************************************/
310 /* write led data to auxport & invert if necessary */
311 /***************************************************/
312 static void
313 write_led(hfcusb_data * hfc, __u8 led_state)
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);
321 /**************************/
322 /* handle LED bits */
323 /**************************/
324 static void
325 set_led_bit(hfcusb_data * hfc, signed short led_bits, int unset)
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;
340 /**************************/
341 /* handle LED requests */
342 /**************************/
343 static void
344 handle_led(hfcusb_data * hfc, int event)
346 hfcsusb_vdata *driver_info =
347 (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
349 /* if no scheme -> no LED action */
350 if (driver_info->led_scheme == LED_OFF)
351 return;
353 switch (event) {
354 case LED_POWER_ON:
355 set_led_bit(hfc, driver_info->led_bits[0],
357 set_led_bit(hfc, driver_info->led_bits[1],
359 set_led_bit(hfc, driver_info->led_bits[2],
361 set_led_bit(hfc, driver_info->led_bits[3],
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],
369 break;
370 case LED_S0_OFF:
371 set_led_bit(hfc, driver_info->led_bits[1],
373 break;
374 case LED_B1_ON:
375 set_led_bit(hfc, driver_info->led_bits[2],
377 break;
378 case LED_B1_OFF:
379 set_led_bit(hfc, driver_info->led_bits[2],
381 break;
382 case LED_B2_ON:
383 set_led_bit(hfc, driver_info->led_bits[3],
385 break;
386 case LED_B2_OFF:
387 set_led_bit(hfc, driver_info->led_bits[3],
389 break;
391 write_led(hfc, hfc->led_state);
394 /********************************/
395 /* called when timer t3 expires */
396 /********************************/
397 static void
398 l1_timer_expire_t3(hfcusb_data * hfc)
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);
413 /********************************/
414 /* called when timer t4 expires */
415 /********************************/
416 static void
417 l1_timer_expire_t4(hfcusb_data * hfc)
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);
429 /*****************************/
430 /* handle S0 state changes */
431 /*****************************/
432 static void
433 state_handler(hfcusb_data * hfc, __u8 state)
435 __u8 old_state;
437 old_state = hfc->l1_state;
438 if (state == old_state || state < 1 || state > 8)
439 return;
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
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
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);
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);
490 hfc->l1_state = state;
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)
499 int k;
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;
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)
525 int i, k, errcode;
527 printk(KERN_INFO "HFC-S USB: starting ISO-chain for Fifo %i\n",
528 fifo->fifonum);
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);
540 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
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;
564 } else {
565 printk(KERN_INFO
566 "HFC-S USB: ISO Buffer size to small!\n");
569 fifo->bit_line = BITLINE_INF;
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);
578 return (fifo->active);
581 /* stops running iso chain and frees their pending urbs */
582 static void
583 stop_isoc_chain(usb_fifo * fifo)
585 int i;
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;
599 if (fifo->urb) {
600 usb_unlink_urb(fifo->urb);
601 usb_free_urb(fifo->urb);
602 fifo->urb = NULL;
604 fifo->active = 0;
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
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)
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 };
628 fifon = fifo->fifonum;
629 status = urb->status;
631 tx_offset = 0;
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;
638 /* is FifoFull-threshold set for our channel? */
639 threshbit = threshtable[fifon] & hfc->threshold_mask;
640 num_isoc_packets = iso_packets[fifon];
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;
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;
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;
680 frame_complete = TRUE;
683 memcpy(context_iso_urb->buffer +
684 tx_offset + 1, fifo->skbuff->data,
685 current_len);
686 skb_pull(fifo->skbuff, current_len);
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;
693 tx_offset += (current_len + 1);
694 } else {
695 urb->iso_frame_desc[k].offset =
696 tx_offset++;
698 urb->iso_frame_desc[k].length = 1;
699 fifo->bit_line -= sink; /* we lower data margin every msec */
701 if (fifo->bit_line < BITLINE_INF) {
702 fifo->bit_line = BITLINE_INF;
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;
717 frame_complete = FALSE;
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);
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);
734 } /* tx_iso_complete */
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)
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
754 fifon = fifo->fifonum;
755 status = urb->status;
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;
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);
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");
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);
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] :
814 fifo->last_urblen = len;
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);
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);
835 } /* rx_iso_complete */
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)
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;
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;
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);
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;
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");
915 #endif
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);
923 #endif
924 skb_trim(fifo->skbuff, 0);
929 /***********************************************/
930 /* receive completion routine for all rx fifos */
931 /***********************************************/
932 static void
933 rx_complete(struct urb *urb, struct pt_regs *regs)
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
945 urb->dev = hfc->dev; /* security init */
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;
958 return;
960 len = urb->actual_length;
961 buf = fifo->buffer;
962 maxlen = fifo->usb_packet_maxlen;
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");
973 #endif
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);
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");
996 } /* rx_complete */
998 /***************************************************/
999 /* start the interrupt transfer for the given fifo */
1000 /***************************************************/
1001 static void
1002 start_int_fifo(usb_fifo * fifo)
1004 int errcode;
1006 printk(KERN_INFO "HFC-S USB: starting intr IN fifo:%d\n",
1007 fifo->fifonum);
1009 if (!fifo->urb) {
1010 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
1011 if (!fifo->urb)
1012 return;
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;
1026 } /* start_int_fifo */
1028 /*****************************/
1029 /* set the B-channel mode */
1030 /*****************************/
1031 static void
1032 set_hfcmode(hfcusb_data * hfc, int channel, int mode)
1034 __u8 val, idx_table[2] = { 0, 2 };
1036 if (hfc->disc_flag) {
1037 return;
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;
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 */
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);
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);
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);
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);
1090 static void
1091 hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
1093 usb_fifo *fifo = my_hisax_if->priv;
1094 hfcusb_data *hfc = fifo->hfc;
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
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,
1132 mdelay(1);
1133 /* start l1 activation */
1134 queue_control_request(hfc,
1135 HFCUSB_STATES,
1136 0x04,
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);
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);
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);
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;
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;
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)
1210 usb_fifo *fifo;
1211 int i, err;
1212 u_char b;
1213 struct hisax_b_if *p_b_if[2];
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);
1220 if (b != HFCUSB_CHIPID) {
1221 printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
1222 return (1);
1225 /* first set the needed config, interface and alternate */
1226 err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1228 /* do Chip reset */
1229 write_usb(hfc, HFCUSB_CIRM, 8);
1230 /* aux = output, reset off */
1231 write_usb(hfc, HFCUSB_CIRM, 0x10);
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));
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);
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 */
1244 /* init the fifos */
1245 write_usb(hfc, HFCUSB_F_THRES,
1246 (HFCUSB_TX_THRESHOLD /
1247 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
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 */
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 */
1269 write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1270 write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
1272 /* set both B-channel to not connected */
1273 hfc->b_mode[0] = L1_MODE_NULL;
1274 hfc->b_mode[1] = L1_MODE_NULL;
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;
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;
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;
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;
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];
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);
1323 #ifdef CONFIG_HISAX_DEBUG
1324 hfc_debug = debug;
1325 #endif
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;
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);
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);
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);
1361 handle_led(hfc, LED_POWER_ON);
1363 return (0);
1364 } /* usb_init */
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)
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;
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;
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);
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;
1408 /* default settings */
1409 iso_packet_size = 16;
1410 packet_size = 64;
1412 while (alt_idx < intf->num_altsetting) {
1413 iface = intf->altsetting + alt_idx;
1414 probe_alt_setting = iface->desc.bAlternateSetting;
1415 cfg_used = 0;
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));
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;
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;
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;
1465 ep++;
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;
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;
1480 #ifdef CONFIG_HISAX_DEBUG
1481 DBG(USB_DBG,
1482 "HFC-USB: small_match=%x %x\n",
1483 small_match, alt_used);
1484 #endif
1486 cfg_used++;
1488 alt_idx++;
1489 } /* (alt_idx < intf->num_altsetting) */
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));
1500 ep = iface->endpoint;
1501 vcf = validconf[small_match];
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;
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 */
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;
1612 ep++;
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;
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);
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;
1654 kfree(context);
1655 return (-EIO);
1657 usb_set_intfdata(intf, context);
1658 return (0);
1660 } else {
1661 printk(KERN_INFO
1662 "HFC-S USB: no valid vendor found in USB descriptor\n");
1664 return (-EIO);
1667 /****************************************************/
1668 /* function called when an active device is removed */
1669 /****************************************************/
1670 static void
1671 hfc_usb_disconnect(struct usb_interface
1672 *intf)
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",
1694 #endif
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",
1703 #endif
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;
1711 context->fifos[i].active = 0;
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;
1720 hisax_unregister(&context->d_if);
1721 kfree(context); /* free our structure again */
1722 } /* hfc_usb_disconnect */
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,
1734 static void __exit
1735 hfc_usb_exit(void)
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");
1744 static int __init
1745 hfc_usb_init(void)
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 */
1762 return (0);
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);