4 * modular HiSax ISDN driver for Colognechip HFC-USB chip
6 * Authors : Peter Sprenger (sprenger@moving-byters.de)
7 * Martin Bachem (info@colognechip.com)
8 * based on the first hfc_usb driver of Werner Cornelius (werner@isdn-development.de)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/types.h>
28 #include <linux/stddef.h>
29 #include <linux/timer.h>
30 #include <linux/config.h>
31 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/usb.h>
36 #include <linux/kernel.h>
37 #include <linux/smp_lock.h>
38 #include <linux/sched.h>
41 static const char *hfcusb_revision = "4.0";
44 to enable much mire debug messages in this driver, define
45 VERBOSE_USB_DEBUG and VERBOSE_ISDN_DEBUG
49 #define VERBOSE_USB_DEBUG
50 #define VERBOSE_ISDN_DEBUG
52 #define INCLUDE_INLINE_FUNCS
61 #define HFC_CTRL_TIMEOUT 20 //(HZ * USB_CTRL_GET_TIMEOUT)
62 /* 5ms timeout writing/reading regs */
63 #define HFC_TIMER_T3 8000 /* timeout for l1 activation timer */
64 #define HFC_TIMER_T4 500 /* time for state change interval */
66 #define HFCUSB_L1_STATECHANGE 0 /* L1 state changed */
67 #define HFCUSB_L1_DRX 1 /* D-frame received */
68 #define HFCUSB_L1_ERX 2 /* E-frame received */
69 #define HFCUSB_L1_DTX 4 /* D-frames completed */
71 #define MAX_BCH_SIZE 2048 /* allowed B-channel packet size */
73 #define HFCUSB_RX_THRESHOLD 64 /* threshold for fifo report bit rx */
74 #define HFCUSB_TX_THRESHOLD 64 /* threshold for fifo report bit tx */
76 #define HFCUSB_CHIP_ID 0x16 /* Chip ID register index */
77 #define HFCUSB_CIRM 0x00 /* cirm register index */
78 #define HFCUSB_USB_SIZE 0x07 /* int length register */
79 #define HFCUSB_USB_SIZE_I 0x06 /* iso length register */
80 #define HFCUSB_F_CROSS 0x0b /* bit order register */
81 #define HFCUSB_CLKDEL 0x37 /* bit delay register */
82 #define HFCUSB_CON_HDLC 0xfa /* channel connect register */
83 #define HFCUSB_HDLC_PAR 0xfb
84 #define HFCUSB_SCTRL 0x31 /* S-bus control register (tx) */
85 #define HFCUSB_SCTRL_E 0x32 /* same for E and special funcs */
86 #define HFCUSB_SCTRL_R 0x33 /* S-bus control register (rx) */
87 #define HFCUSB_F_THRES 0x0c /* threshold register */
88 #define HFCUSB_FIFO 0x0f /* fifo select register */
89 #define HFCUSB_F_USAGE 0x1a /* fifo usage register */
90 #define HFCUSB_MST_MODE0 0x14
91 #define HFCUSB_MST_MODE1 0x15
92 #define HFCUSB_P_DATA 0x1f
93 #define HFCUSB_INC_RES_F 0x0e
94 #define HFCUSB_STATES 0x30
96 #define HFCUSB_CHIPID 0x40 /* ID value of HFC-USB */
101 #define HFCUSB_NUM_FIFOS 8 /* maximum number of fifos */
102 #define HFCUSB_B1_TX 0 /* index for B1 transmit bulk/int */
103 #define HFCUSB_B1_RX 1 /* index for B1 receive bulk/int */
104 #define HFCUSB_B2_TX 2
105 #define HFCUSB_B2_RX 3
106 #define HFCUSB_D_TX 4
107 #define HFCUSB_D_RX 5
108 #define HFCUSB_PCM_TX 6
109 #define HFCUSB_PCM_RX 7
112 * used to switch snd_transfer_mode for different TA modes e.g. the Billion USB TA just
113 * supports ISO out, while the Cologne Chip EVAL TA just supports BULK out
119 #define ISOC_PACKETS_D 8
120 #define ISOC_PACKETS_B 8
121 #define ISO_BUFFER_SIZE 128
123 // ISO send definitions
128 #define BITLINE_INF (-64*8)
136 #define write_usb(a,b,c) usb_control_msg((a)->dev,(a)->ctrl_out_pipe,0,0x40,(c),(b),NULL,0,HFC_CTRL_TIMEOUT)
137 #define read_usb(a,b,c) usb_control_msg((a)->dev,(a)->ctrl_in_pipe,1,0xC0,0,(b),(c),1,HFC_CTRL_TIMEOUT)
139 /*************************************************/
140 /* entry and size of output/input control buffer */
141 /*************************************************/
142 #define HFC_CTRL_BUFSIZE 32
145 __u8 hfc_reg; /* register number */
146 __u8 reg_val; /* value to be written (or read) */
147 int action; /* data for action handler */
153 int vendor; // vendor id
154 int prod_id; // product id
155 char *vend_name; // vendor string
156 __u8 led_scheme; // led display scheme
157 __u8 led_invert; // invert led aux port settings
158 __u8 led_bits[8]; // array of 8 possible LED bitmask settings
162 /***************************************************************/
163 /* structure defining input+output fifos (interrupt/bulk mode) */
164 /***************************************************************/
166 struct usb_fifo; /* forward definition */
167 typedef struct iso_urb_struct
170 __u8 buffer[ISO_BUFFER_SIZE]; /* buffer incoming/outgoing data */
171 struct usb_fifo *owner_fifo; // pointer to owner fifo
175 struct hfcusb_data; /* forward definition */
176 typedef struct usb_fifo
178 int fifonum; /* fifo index attached to this structure */
179 int active; /* fifo is currently active */
180 struct hfcusb_data *hfc; /* pointer to main structure */
181 int pipe; /* address of endpoint */
182 __u8 usb_packet_maxlen; /* maximum length for usb transfer */
183 unsigned int max_size; /* maximum size of receive/send packet */
184 __u8 intervall; /* interrupt interval */
185 struct sk_buff *skbuff; /* actual used buffer */
186 struct urb *urb; /* transfer structure for usb routines */
187 __u8 buffer[128]; /* buffer incoming/outgoing data */
188 int bit_line; /* how much bits are in the fifo? */
190 volatile __u8 usb_transfer_mode;/* switched between ISO and INT */
191 iso_urb_struct iso[2]; /* need two urbs to have one always for pending */
192 struct hisax_if *hif; /* hisax interface */
193 int delete_flg; /* only delete skbuff once */
194 int last_urblen; /* remember length of last packet */
199 /*********************************************/
200 /* structure holding all data for one device */
201 /*********************************************/
202 typedef struct hfcusb_data
204 // HiSax Interface for loadable Layer1 drivers
205 struct hisax_d_if d_if; /* see hisax_if.h */
206 struct hisax_b_if b_if[2]; /* see hisax_if.h */
209 struct usb_device *dev; /* our device */
210 int if_used; /* used interface number */
211 int alt_used; /* used alternate config */
212 int ctrl_paksize; /* control pipe packet size */
213 int ctrl_in_pipe, ctrl_out_pipe; /* handles for control pipe */
214 int cfg_used; /* configuration index used */
215 int vend_idx; // vendor found
217 int b_mode[2]; // B-channel mode
219 int l1_activated; // layer 1 activated
221 int packet_size,iso_packet_size;
223 /* control pipe background handling */
224 ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE]; /* buffer holding queued data */
225 volatile int ctrl_in_idx, ctrl_out_idx,
226 ctrl_cnt; /* input/output pointer + count */
227 struct urb *ctrl_urb; /* transfer structure for control channel */
229 struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
230 struct usb_ctrlrequest ctrl_read; /* same for read request */
232 __u8 led_state,led_new_data,led_b_active;
234 volatile __u8 threshold_mask; /* threshold actually reported */
235 volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
237 usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
239 volatile __u8 l1_state; /* actual l1 state */
240 struct timer_list t3_timer; /* timer 3 for activation/deactivation */
241 struct timer_list t4_timer; /* timer 4 for activation/deactivation */
242 struct timer_list led_timer; /* timer flashing leds */
247 static void collect_rx_frame(usb_fifo *fifo,__u8 *data,int len,int finish);
251 /******************************************************/
252 /* start next background transfer for control channel */
253 /******************************************************/
254 static void ctrl_start_transfer(hfcusb_data * hfc)
259 hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
260 hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
261 hfc->ctrl_urb->transfer_buffer = NULL;
262 hfc->ctrl_urb->transfer_buffer_length = 0;
263 hfc->ctrl_write.wIndex = hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg;
264 hfc->ctrl_write.wValue = hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val;
265 err = usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
266 printk(KERN_DEBUG "ctrl_start_transfer: submit %d\n", err);
268 } /* ctrl_start_transfer */
270 /************************************/
271 /* queue a control transfer request */
272 /* return 0 on success. */
273 /************************************/
274 static int queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val,int action)
278 #ifdef VERBOSE_USB_DEBUG
279 printk ("HFC_USB: queue_control_request reg: %x, val: %x\n", reg, val);
282 if(hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE) return(1); /* no space left */
283 buf = &hfc->ctrl_buff[hfc->ctrl_in_idx]; /* pointer to new index */
287 if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
288 hfc->ctrl_in_idx = 0; /* pointer wrap */
289 if (++hfc->ctrl_cnt == 1)
290 ctrl_start_transfer(hfc);
292 } /* queue_control_request */
295 static int control_action_handler(hfcusb_data *hfc,int reg,int val,int action)
297 if(!action) return(1); // no action defined
303 /***************************************************************/
304 /* control completion routine handling background control cmds */
305 /***************************************************************/
306 static void ctrl_complete(struct urb *urb, struct pt_regs *regs)
308 hfcusb_data *hfc = (hfcusb_data *) urb->context;
311 printk(KERN_DEBUG "ctrl_complete cnt %d\n", hfc->ctrl_cnt);
315 buf=&hfc->ctrl_buff[hfc->ctrl_out_idx];
316 control_action_handler(hfc,buf->hfc_reg,buf->reg_val,buf->action);
318 hfc->ctrl_cnt--; /* decrement actual count */
319 if(++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE) hfc->ctrl_out_idx = 0; /* pointer wrap */
321 ctrl_start_transfer(hfc); /* start next transfer */
323 } /* ctrl_complete */
327 #define LED_OFF 0 // no LED support
328 #define LED_SCHEME1 1 // LED standard scheme
329 #define LED_SCHEME2 2 // not used yet...
331 #define LED_POWER_ON 1
332 #define LED_POWER_OFF 2
337 #define LED_B1_DATA 7
340 #define LED_B2_DATA 10
342 #define LED_NORMAL 0 // LEDs are normal
343 #define LED_INVERTED 1 // LEDs are inverted
345 // time for LED flashing
350 {0x959, 0x2bd0, "ISDN USB TA (Cologne Chip HFC-S USB based)", LED_OFF,LED_NORMAL,{4,0,2,1}}, /* CologneChip Eval TA */
351 {0x7b0, 0x0007, "Billion tiny USB ISDN TA 128", LED_SCHEME1, LED_INVERTED, {8,0x40,0x20,0x10}}, /* Billion TA */
352 {0x742, 0x2008, "Stollmann USB TA", LED_SCHEME1, LED_NORMAL, {4,0,2,1}}, /* Stollmann TA */
353 {0x8e3, 0x0301, "Olitec USB RNIS", LED_SCHEME1, LED_NORMAL, {2,0,1,4}}, /* Olitec TA */
354 {0x675, 0x1688, "DrayTec USB ISDN TA", LED_SCHEME1, LED_NORMAL, {4,0,2,1}}, /* Draytec TA */
355 {0x7fa, 0x0846, "Bewan Modem RNIS USB", LED_SCHEME1, LED_INVERTED, {8,0x40,0x20,0x10}}, /* Bewan TA */
359 /***************************************************/
360 /* write led data to auxport & invert if necessary */
361 /***************************************************/
362 static void write_led(hfcusb_data * hfc,__u8 led_state)
364 if(led_state!=hfc->led_state)
366 hfc->led_state=led_state;
367 queue_control_request(hfc, HFCUSB_P_DATA,(vdata[hfc->vend_idx].led_invert) ? ~led_state : led_state,1);
371 /******************************************/
372 /* invert B-channel LEDs if data is sent */
373 /******************************************/
374 static void led_timer(hfcusb_data * hfc)
377 __u8 led_state=hfc->led_state;
381 if(hfc->led_b_active&1) led_state|=vdata[hfc->vend_idx].led_bits[2];
382 if(hfc->led_b_active&2) led_state|=vdata[hfc->vend_idx].led_bits[3];
386 if(!(hfc->led_b_active&1) || hfc->led_new_data&1) led_state&=~vdata[hfc->vend_idx].led_bits[2];
387 if(!(hfc->led_b_active&2) || hfc->led_new_data&2) led_state&=~vdata[hfc->vend_idx].led_bits[3];
390 write_led(hfc,led_state);
394 // restart 4 hz timer
395 hfc->led_timer.expires = jiffies + (LED_TIME * HZ) / 1000;
396 if(!timer_pending(&hfc->led_timer)) add_timer(&hfc->led_timer);
399 /**************************/
400 /* handle LED requests */
401 /**************************/
402 static void handle_led(hfcusb_data * hfc,int event)
404 __u8 led_state=hfc->led_state;
406 // if no scheme -> no LED action
407 if(vdata[hfc->vend_idx].led_scheme==LED_OFF) return;
412 led_state|=vdata[hfc->vend_idx].led_bits[0];
414 case LED_POWER_OFF: // no Power off handling
417 led_state|=vdata[hfc->vend_idx].led_bits[1];
420 led_state&=~vdata[hfc->vend_idx].led_bits[1];
423 hfc->led_b_active|=1;
426 hfc->led_b_active&=~1;
429 hfc->led_new_data|=1;
432 hfc->led_b_active|=2;
435 hfc->led_b_active&=~2;
438 hfc->led_new_data|=2;
442 write_led(hfc,led_state);
445 /********************************/
446 /* called when timer t3 expires */
447 /********************************/
448 static void l1_timer_expire_t3(hfcusb_data * hfc)
450 //printk (KERN_INFO "HFC-USB: l1_timer_expire_t3\n");
452 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_DEACTIVATE | INDICATION,NULL);
453 #ifdef VERBOSE_USB_DEBUG
454 printk(KERN_INFO "PH_DEACTIVATE | INDICATION sent\n");
456 hfc->l1_activated=FALSE;
457 handle_led(hfc,LED_S0_OFF);
460 /********************************/
461 /* called when timer t4 expires */
462 /********************************/
463 static void l1_timer_expire_t4(hfcusb_data * hfc)
465 //printk (KERN_INFO "HFC-USB: l1_timer_expire_t4\n");
467 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_DEACTIVATE | INDICATION,NULL);
468 #ifdef VERBOSE_USB_DEBUG
469 printk(KERN_INFO "PH_DEACTIVATE | INDICATION sent\n");
471 hfc->l1_activated=FALSE;
472 handle_led(hfc,LED_S0_OFF);
475 /*****************************/
476 /* handle S0 state changes */
477 /*****************************/
478 static void state_handler(hfcusb_data * hfc,__u8 state)
482 old_state=hfc->l1_state;
485 if(state==old_state || state<1 || state>8) return;
487 #ifdef VERBOSE_ISDN_DEBUG
488 printk(KERN_INFO "HFC-USB: new S0 state:%d old_state:%d\n",state,old_state);
491 if(state<4 || state==7 || state==8)
493 if(timer_pending(&hfc->t3_timer)) del_timer(&hfc->t3_timer);
494 //printk(KERN_INFO "HFC-USB: T3 deactivated\n");
499 if(timer_pending(&hfc->t4_timer)) del_timer(&hfc->t4_timer);
500 //printk(KERN_INFO "HFC-USB: T4 deactivated\n");
503 if(state==7 && !hfc->l1_activated)
505 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_ACTIVATE | INDICATION,NULL);
506 //printk(KERN_INFO "HFC-USB: PH_ACTIVATE | INDICATION sent\n");
507 hfc->l1_activated=TRUE;
508 handle_led(hfc,LED_S0_ON);
511 if(state<=3 /* && activated*/)
513 if(old_state==7 || old_state==8)
515 //printk(KERN_INFO "HFC-USB: T4 activated\n");
516 hfc->t4_timer.expires = jiffies + (HFC_TIMER_T4 * HZ) / 1000;
517 if(!timer_pending(&hfc->t4_timer)) add_timer(&hfc->t4_timer);
521 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,PH_DEACTIVATE | INDICATION,NULL);
522 //printk(KERN_INFO "HFC-USB: PH_DEACTIVATE | INDICATION sent\n");
523 hfc->l1_activated=FALSE;
524 handle_led(hfc,LED_S0_OFF);
532 /* prepare iso urb */
533 static void fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe, void *buf,
534 int num_packets, int packet_size, int interval, usb_complete_t complete, void *context)
538 spin_lock_init(&urb->lock); // do we really need spin_lock_init ?
541 urb->complete = complete;
542 urb->number_of_packets = num_packets;
543 urb->transfer_buffer_length = packet_size * num_packets;
544 urb->context = context;
545 urb->transfer_buffer = buf;
546 urb->transfer_flags = 0;
547 urb->transfer_flags = URB_ISO_ASAP;
548 urb->actual_length = 0;
549 urb->interval = interval;
550 for (k = 0; k < num_packets; k++) {
551 urb->iso_frame_desc[k].offset = packet_size * k;
552 urb->iso_frame_desc[k].length = packet_size;
553 urb->iso_frame_desc[k].actual_length = 0;
557 /* allocs urbs and start isoc transfer with two pending urbs to avoid gaps in the transfer chain */
558 static int start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,usb_complete_t complete,int packet_size)
562 #ifdef VERBOSE_USB_DEBUG
563 printk(KERN_INFO "HFC-USB: starting ISO-chain for Fifo %i\n", fifo->fifonum);
567 // allocate Memory for Iso out Urbs
568 for (i = 0; i < 2; i++) {
569 if (!(fifo->iso[i].purb)) {
570 fifo->iso[i].purb = usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
571 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
573 // Init the first iso
574 if (ISO_BUFFER_SIZE >= (fifo->usb_packet_maxlen * num_packets_per_urb))
577 fill_isoc_urb(fifo->iso[i].purb, fifo->hfc->dev, fifo->pipe, fifo->iso[i].buffer,
578 num_packets_per_urb, fifo->usb_packet_maxlen, fifo->intervall,
579 complete, &fifo->iso[i]);
581 memset(fifo->iso[i].buffer, 0, sizeof(fifo->iso[i].buffer));
583 // defining packet delimeters in fifo->buffer
584 for(k = 0; k < num_packets_per_urb; k++)
586 fifo->iso[i].purb->iso_frame_desc[k].offset = k*packet_size;
587 fifo->iso[i].purb->iso_frame_desc[k].length = packet_size;
592 fifo->bit_line = BITLINE_INF;
594 errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
595 fifo->active = (errcode >= 0) ? 1 : 0;
598 printk(KERN_INFO "HFC-USB: error submitting ISO URB: %i.%i \n", errcode, i);
603 // errcode = (usb_submit_urb(fifo->iso[0].purb, GFP_KERNEL));
604 return(fifo->active);
607 /* stops running iso chain and frees their pending urbs */
608 static void stop_isoc_chain(usb_fifo * fifo)
612 for(i = 0; i < 2; i++)
614 if(fifo->iso[i].purb)
616 #ifdef VERBOSE_USB_DEBUG
617 printk(KERN_INFO "HFC-USB: Stopping iso chain for fifo %i.%i\n", fifo->fifonum, i);
619 usb_unlink_urb(fifo->iso[i].purb);
620 usb_free_urb(fifo->iso[i].purb);
621 fifo->iso[i].purb = NULL;
625 usb_unlink_urb(fifo->urb);
626 usb_free_urb(fifo->urb);
632 // defines how much ISO packets are handled in one URB
633 static int iso_packets[8]={ISOC_PACKETS_B,ISOC_PACKETS_B,ISOC_PACKETS_B,ISOC_PACKETS_B,
634 ISOC_PACKETS_D,ISOC_PACKETS_D,ISOC_PACKETS_D,ISOC_PACKETS_D};
636 /*****************************************************/
637 /* transmit completion routine for all ISO tx fifos */
638 /*****************************************************/
639 static void tx_iso_complete(struct urb *urb, struct pt_regs *regs)
641 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
642 usb_fifo *fifo = context_iso_urb->owner_fifo;
643 hfcusb_data *hfc = fifo->hfc;
644 int k, tx_offset, num_isoc_packets, sink, len, current_len,errcode,frame_complete,transp_mode,fifon;
646 __u8 threshtable[8] = { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
650 // very weird error code when using ohci drivers, for now : ignore this error ... (MB)
651 if(urb->status == -EOVERFLOW)
654 #ifdef VERBOSE_USB_DEBUG
655 printk(KERN_INFO "HFC-USB: ignoring USB DATAOVERRUN for fifo %i \n",fifon);
659 if(fifo->active && !urb->status)
662 if(fifon<4 && hfc->b_mode[fifon/2]==L1_MODE_TRANS) transp_mode=TRUE;
664 threshbit = threshtable[fifon] & hfc->threshold_mask; // is threshold set for our channel?
665 num_isoc_packets=iso_packets[fifon];
667 if(fifon >= HFCUSB_D_TX)
669 sink = (threshbit) ? SINK_DMIN : SINK_DMAX; // how much bit go to the sink for D-channel?
673 sink = (threshbit) ? SINK_MIN : SINK_MAX; // how much bit go to the sink for B-channel?
677 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,context_iso_urb->buffer, num_isoc_packets,
678 fifo->usb_packet_maxlen, fifo->intervall, tx_iso_complete, urb->context);
679 memset(context_iso_urb->buffer, 0, sizeof(context_iso_urb->buffer));
681 frame_complete=FALSE;
683 // Generate Iso Packets
684 for(k = 0; k < num_isoc_packets; ++k)
688 len = fifo->skbuff->len; // remaining length
690 fifo->bit_line -= sink; // we lower data margin every msec
691 current_len = (0 - fifo->bit_line) / 8;
692 if(current_len > 14) current_len = 14; // maximum 15 byte for every ISO packet makes our life easier
693 current_len = (len <= current_len) ? len : current_len;
694 fifo->bit_line += current_len * 8; // how much bit do we put on the line?
696 context_iso_urb->buffer[tx_offset] = 0;
697 if(current_len == len)
701 context_iso_urb->buffer[tx_offset] = 1; // here frame completion
702 fifo->bit_line += 32; // add 2 byte flags and 16bit CRC at end of ISDN frame
704 frame_complete = TRUE;
707 // copy bytes from buffer into ISO_URB
708 memcpy(context_iso_urb->buffer+tx_offset+1,fifo->skbuff->data,current_len);
709 skb_pull(fifo->skbuff,current_len);
711 // define packet delimeters within the URB buffer
712 urb->iso_frame_desc[k].offset = tx_offset;
713 urb->iso_frame_desc[k].length = current_len + 1;
715 tx_offset += (current_len + 1);
716 // printk(KERN_INFO "HFC-USB: fifonum:%d,%d bytes to send, %d bytes ISO packet,bitline:%d,sink:%d,threshbit:%d,threshmask:%x\n",fifon,len,current_len,fifo->bit_line,sink,threshbit,hfc->threshold_mask);
719 if(fifon==HFCUSB_B1_TX) handle_led(hfc,LED_B1_DATA);
720 if(fifon==HFCUSB_B2_TX) handle_led(hfc,LED_B2_DATA);
725 // we have no more data - generate 1 byte ISO packets
726 urb->iso_frame_desc[k].offset = tx_offset++;
728 urb->iso_frame_desc[k].length = 1;
729 fifo->bit_line -= sink; // we lower data margin every msec
731 if(fifo->bit_line < BITLINE_INF)
733 fifo->bit_line = BITLINE_INF;
734 //printk (KERN_INFO "HFC-USB: BITLINE_INF underrun\n");
740 // delete the buffer only once, here or in hfc_usb_l2l1() in a PH_DATA|REQUEST
741 fifo->delete_flg=TRUE;
743 fifo->hif->l1l2(fifo->hif,PH_DATA|CONFIRM,(void*)fifo->skbuff->truesize);
745 if(fifo->skbuff && fifo->delete_flg)
747 dev_kfree_skb_any(fifo->skbuff);
748 //printk(KERN_INFO "HFC-USB: skbuff=NULL on fifo:%d\n",fifo->fifonum);
750 fifo->delete_flg=FALSE;
753 frame_complete=FALSE;
757 errcode = usb_submit_urb(urb, GFP_ATOMIC);
760 printk(KERN_INFO "HFC-USB: error submitting ISO URB: %i \n", errcode);
767 printk(KERN_INFO "HFC-USB: tx_iso_complete : urb->status %i, fifonum %i\n", urb->status,fifon);
771 } /* tx_iso_complete */
773 /*****************************************************/
774 /* receive completion routine for all ISO tx fifos */
775 /*****************************************************/
776 static void rx_iso_complete(struct urb *urb, struct pt_regs *regs)
778 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
779 usb_fifo *fifo = context_iso_urb->owner_fifo;
780 hfcusb_data *hfc = fifo->hfc;
781 int k, len, errcode, offset, num_isoc_packets,fifon;
785 // very weird error code when using ohci drivers, for now : ignore this error ... (MB)
786 if(urb->status == -EOVERFLOW)
789 #ifdef VERBOSE_USB_DEBUG
790 printk(KERN_INFO "HFC-USB: ignoring USB DATAOVERRUN for fifo %i \n",fifon);
794 if(fifo->active && !urb->status)
796 num_isoc_packets=iso_packets[fifon];
798 // Generate D-Channel Iso Packets
799 for(k = 0; k < num_isoc_packets; ++k)
801 len=urb->iso_frame_desc[k].actual_length;
802 offset=urb->iso_frame_desc[k].offset;
803 buf=context_iso_urb->buffer+offset;
805 if(fifo->last_urblen!=fifo->usb_packet_maxlen)
807 // the threshold mask is in the 2nd status byte
808 hfc->threshold_mask=buf[1];
809 // the S0 state is in the upper half of the 1st status byte
810 state_handler(hfc,buf[0] >> 4);
811 // if we have more than the 2 status bytes -> collect data
812 if(len>2) collect_rx_frame(fifo,buf+2,len-2,buf[0]&1);
814 else collect_rx_frame(fifo,buf,len,0);
816 fifo->last_urblen=len;
821 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,context_iso_urb->buffer, num_isoc_packets,
822 fifo->usb_packet_maxlen, fifo->intervall, rx_iso_complete, urb->context);
824 errcode = usb_submit_urb(urb, GFP_ATOMIC);
827 printk(KERN_INFO "HFC-USB: error submitting ISO URB: %i \n", errcode);
834 printk(KERN_INFO "HFC-USB: rx_iso_complete : urb->status %i, fifonum %i\n", urb->status,fifon);
837 } /* rx_iso_complete */
840 /*****************************************************/
841 /* collect data from interrupt or isochron in */
842 /*****************************************************/
843 static void collect_rx_frame(usb_fifo *fifo,__u8 *data,int len,int finish)
845 hfcusb_data *hfc = fifo->hfc;
846 int transp_mode,fifon;
850 if(fifon<4 && hfc->b_mode[fifon/2]==L1_MODE_TRANS) transp_mode=TRUE;
852 //printk(KERN_INFO "HFC-USB: got %d bytes finish:%d max_size:%d fifo:%d\n",len,finish,fifo->max_size,fifon);
855 // allocate sk buffer
856 fifo->skbuff=dev_alloc_skb(fifo->max_size + 3);
859 printk(KERN_INFO "HFC-USB: cannot allocate buffer (dev_alloc_skb) fifo:%d\n",fifon);
865 if(len && fifo->skbuff->len+len<fifo->max_size)
867 memcpy(skb_put(fifo->skbuff,len),data,len);
869 else printk(KERN_INFO "HCF-USB: got frame exceeded fifo->max_size:%d\n",fifo->max_size);
871 // give transparent data up, when 128 byte are available
872 if(transp_mode && fifo->skbuff->len>=128)
874 fifo->hif->l1l2(fifo->hif,PH_DATA | INDICATION,fifo->skbuff);
875 fifo->skbuff = NULL; // buffer was freed from upper layer
879 // we have a complete hdlc packet
882 if(!fifo->skbuff->data[fifo->skbuff->len-1])
884 skb_trim(fifo->skbuff,fifo->skbuff->len-3); // remove CRC & status
886 //printk(KERN_INFO "HFC-USB: got frame %d bytes on fifo:%d\n",fifo->skbuff->len,fifon);
888 if(fifon==HFCUSB_PCM_RX) fifo->hif->l1l2(fifo->hif,PH_DATA_E | INDICATION,fifo->skbuff);
889 else fifo->hif->l1l2(fifo->hif,PH_DATA | INDICATION,fifo->skbuff);
891 fifo->skbuff = NULL; // buffer was freed from upper layer
895 printk(KERN_INFO "HFC-USB: got frame %d bytes but CRC ERROR!!!\n",fifo->skbuff->len);
897 skb_trim(fifo->skbuff,0); // clear whole buffer
901 // LED flashing only in HDLC mode
904 if(fifon==HFCUSB_B1_RX) handle_led(hfc,LED_B1_DATA);
905 if(fifon==HFCUSB_B2_RX) handle_led(hfc,LED_B2_DATA);
909 /***********************************************/
910 /* receive completion routine for all rx fifos */
911 /***********************************************/
912 static void rx_complete(struct urb *urb, struct pt_regs *regs)
916 usb_fifo *fifo = (usb_fifo *) urb->context; /* pointer to our fifo */
917 hfcusb_data *hfc = fifo->hfc;
919 urb->dev = hfc->dev; /* security init */
921 if((!fifo->active) || (urb->status)) {
922 #ifdef VERBOSE_USB_DEBUG
923 printk(KERN_INFO "HFC-USB: RX-Fifo %i is going down (%i)\n", fifo->fifonum, urb->status);
925 fifo->urb->interval = 0; /* cancel automatic rescheduling */
927 dev_kfree_skb_any(fifo->skbuff);
933 len=urb->actual_length;
936 if(fifo->last_urblen!=fifo->usb_packet_maxlen) {
937 // the threshold mask is in the 2nd status byte
938 hfc->threshold_mask=buf[1];
939 // the S0 state is in the upper half of the 1st status byte
940 state_handler(hfc,buf[0] >> 4);
941 // if we have more than the 2 status bytes -> collect data
942 if(len>2) collect_rx_frame(fifo,buf+2,urb->actual_length-2,buf[0]&1);
944 collect_rx_frame(fifo,buf,urb->actual_length,0);
946 fifo->last_urblen=urb->actual_length;
953 /***************************************************/
954 /* start the interrupt transfer for the given fifo */
955 /***************************************************/
956 static void start_int_fifo(usb_fifo * fifo)
960 #ifdef VERBOSE_USB_DEBUG
961 printk(KERN_INFO "HFC-USB: starting intr IN fifo:%d\n", fifo->fifonum);
964 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
968 usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe, fifo->buffer,
969 fifo->usb_packet_maxlen, rx_complete, fifo, fifo->intervall);
970 fifo->active = 1; /* must be marked active */
971 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
975 printk(KERN_INFO "HFC-USB: submit URB error(start_int_info): status:%i\n", errcode);
979 } /* start_int_fifo */
981 /*****************************/
982 /* set the B-channel mode */
983 /*****************************/
984 static void set_hfcmode(hfcusb_data *hfc,int channel,int mode)
986 __u8 val,idx_table[2]={0,2};
988 #ifdef VERBOSE_ISDN_DEBUG
989 printk (KERN_INFO "HFC-USB: setting channel %d to mode %d\n",channel,mode);
992 hfc->b_mode[channel]=mode;
996 if(mode!=L1_MODE_NULL) val=8; // enable fifo?
997 if(mode==L1_MODE_TRANS) val|=2; // set transparent bit
999 queue_control_request(hfc,HFCUSB_FIFO,idx_table[channel],1); // set FIFO to transmit register
1000 queue_control_request(hfc,HFCUSB_CON_HDLC,val,1);
1001 queue_control_request(hfc,HFCUSB_INC_RES_F,2,1); // reset fifo
1003 queue_control_request(hfc,HFCUSB_FIFO,idx_table[channel]+1,1); // set FIFO to receive register
1004 queue_control_request(hfc,HFCUSB_CON_HDLC,val,1);
1005 queue_control_request(hfc,HFCUSB_INC_RES_F,2,1); // reset fifo
1008 if(hfc->b_mode[0]) val|=1;
1009 if(hfc->b_mode[1]) val|=2;
1010 queue_control_request(hfc,HFCUSB_SCTRL,val,1);
1013 if(hfc->b_mode[0]) val|=1;
1014 if(hfc->b_mode[1]) val|=2;
1015 queue_control_request(hfc,HFCUSB_SCTRL_R,val,1);
1017 if(mode==L1_MODE_NULL)
1019 if(channel) handle_led(hfc,LED_B2_OFF);
1020 else handle_led(hfc,LED_B1_OFF);
1024 if(channel) handle_led(hfc,LED_B2_ON);
1025 else handle_led(hfc,LED_B1_ON);
1030 --------------------------------------------------------------------------------------
1031 from here : hisax_if callback routines :
1032 - void hfc_usb_d_l2l1(struct hisax_if *hisax_d_if, int pr, void *arg) {
1035 - static void hfc_usb_l1l2(hfcusb_data * hfc)
1039 void hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
1041 usb_fifo *fifo = my_hisax_if->priv;
1042 hfcusb_data *hfc = fifo->hfc;
1045 case PH_ACTIVATE | REQUEST:
1046 if(fifo->fifonum==HFCUSB_D_TX)
1048 #ifdef VERBOSE_ISDN_DEBUG
1049 printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST\n");
1051 queue_control_request(hfc, HFCUSB_STATES,0x60,1); /* make activation */
1052 hfc->t3_timer.expires = jiffies + (HFC_TIMER_T3 * HZ) / 1000;
1053 if(!timer_pending(&hfc->t3_timer)) add_timer(&hfc->t3_timer);
1057 #ifdef VERBOSE_ISDN_DEBUG
1058 printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_ACTIVATE | REQUEST\n");
1060 set_hfcmode(hfc,(fifo->fifonum==HFCUSB_B1_TX) ? 0 : 1 ,(int)arg);
1061 fifo->hif->l1l2(fifo->hif,PH_ACTIVATE | INDICATION, NULL);
1064 case PH_DEACTIVATE | REQUEST:
1065 if(fifo->fifonum==HFCUSB_D_TX)
1067 #ifdef VERBOSE_ISDN_DEBUG
1068 printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST\n");
1070 printk (KERN_INFO "HFC-USB: ISDN TE device should not deativate...\n");
1074 #ifdef VERBOSE_ISDN_DEBUG
1075 printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST\n");
1077 set_hfcmode(hfc,(fifo->fifonum==HFCUSB_B1_TX) ? 0 : 1 ,(int)L1_MODE_NULL);
1078 fifo->hif->l1l2(fifo->hif,PH_DEACTIVATE | INDICATION, NULL);
1081 case PH_DATA | REQUEST:
1082 if(fifo->skbuff && fifo->delete_flg)
1084 dev_kfree_skb_any(fifo->skbuff);
1085 //printk(KERN_INFO "skbuff=NULL on fifo:%d\n",fifo->fifonum);
1086 fifo->skbuff = NULL;
1087 fifo->delete_flg=FALSE;
1090 fifo->skbuff=arg; // we have a new buffer
1092 //if(fifo->fifonum==HFCUSB_D_TX) printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DATA | REQUEST\n");
1093 //else printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DATA | REQUEST\n");
1096 printk (KERN_INFO "HFC_USB: hfc_usb_d_l2l1: unkown state : %#x\n", pr);
1101 // valid configurations
1102 #define CNF_4INT3ISO 1 // 4 INT IN, 3 ISO OUT
1103 #define CNF_3INT3ISO 2 // 3 INT IN, 3 ISO OUT
1104 #define CNF_4ISO3ISO 3 // 4 ISO IN, 3 ISO OUT
1105 #define CNF_3ISO3ISO 4 // 3 ISO IN, 3 ISO OUT
1109 --------------------------------------------------------------------------------------
1110 From here on USB initialization and deactivation related routines are implemented :
1113 is the main Entry Point for the USB Subsystem when the device get plugged
1114 in. This function calls usb_register with usb_driver as parameter.
1115 Here, further entry points for probing (hfc_usb_probe) and disconnecting
1116 the device (hfc_usb_disconnect) are published, as the id_table
1119 this function is called by the usb subsystem, and steps through the alternate
1120 settings of the currently plugged in device to detect all Endpoints needed to
1122 Needed EndPoints are
1123 3 (+1) IntIn EndPoints (D-in, E-in, B1-in, B2-in, (E-in)) or
1124 3 (+1) Isochron In Endpoints (D-out, B1-out, B2-out) and 3 IsoOut Endpoints
1125 The currently used transfer mode of on the Out-Endpoints will be stored in
1126 hfc->usb_transfer_mode and is either USB_INT or USB_ISO
1127 When a valid alternate setting could be found, the usb_init (see blow)
1131 Here, the HFC_USB Chip itself gets initialized and the USB framework to send/receive
1132 Data to/from the several EndPoints are initialized:
1133 The E- and D-Channel Int-In chain gets started
1134 The IsoChain for the Iso-Out traffic get started
1136 - hfc_usb_disconnect
1137 this function is called by the usb subsystem and has to free all resources
1138 and stop all usb traffic to allow a proper hotplugging disconnect.
1142 /***************************************************************************/
1143 /* usb_init is called once when a new matching device is detected to setup */
1144 /* main parameters. It registers the driver at the main hisax module. */
1145 /* on success 0 is returned. */
1146 /***************************************************************************/
1147 static int usb_init(hfcusb_data * hfc)
1152 struct hisax_b_if *p_b_if[2];
1154 /* check the chip id */
1155 printk(KERN_INFO "HFCUSB_CHIP_ID begin\n");
1156 if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
1157 printk(KERN_INFO "HFC-USB: cannot read chip id\n");
1160 printk(KERN_INFO "HFCUSB_CHIP_ID %x\n", b);
1161 if (b != HFCUSB_CHIPID) {
1162 printk(KERN_INFO "HFC-USB: Invalid chip id 0x%02x\n", b);
1166 /* first set the needed config, interface and alternate */
1167 printk(KERN_INFO "usb_init 1\n");
1168 // usb_set_configuration(hfc->dev, 1);
1169 printk(KERN_INFO "usb_init 2\n");
1170 err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1171 printk(KERN_INFO "usb_init usb_set_interface return %d\n", err);
1172 /* now we initialize the chip */
1173 write_usb(hfc, HFCUSB_CIRM, 8); // do reset
1174 write_usb(hfc, HFCUSB_CIRM, 0x10); // aux = output, reset off
1176 // set USB_SIZE to match the the wMaxPacketSize for INT or BULK transfers
1177 write_usb(hfc, HFCUSB_USB_SIZE,(hfc->packet_size/8) | ((hfc->packet_size/8) << 4));
1179 // set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers
1180 write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
1182 /* enable PCM/GCI master mode */
1183 write_usb(hfc, HFCUSB_MST_MODE1, 0); /* set default values */
1184 write_usb(hfc, HFCUSB_MST_MODE0, 1); /* enable master mode */
1186 /* init the fifos */
1187 write_usb(hfc, HFCUSB_F_THRES, (HFCUSB_TX_THRESHOLD/8) |((HFCUSB_RX_THRESHOLD/8) << 4));
1190 for(i = 0; i < HFCUSB_NUM_FIFOS; i++)
1192 write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
1193 fifo[i].skbuff = NULL; /* init buffer pointer */
1194 fifo[i].max_size = (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1195 fifo[i].last_urblen=0;
1196 write_usb(hfc, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2)); // set 2 bit for D- & E-channel
1197 write_usb(hfc, HFCUSB_CON_HDLC, ((i==HFCUSB_D_TX) ? 0x09 : 0x08)); // rx hdlc, enable IFF for D-channel
1198 write_usb(hfc, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1201 write_usb(hfc, HFCUSB_CLKDEL, 0x0f); /* clock delay value */
1202 write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
1203 write_usb(hfc, HFCUSB_STATES, 3); /* enable state machine */
1205 write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1206 write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
1208 // set both B-channel to not connected
1209 hfc->b_mode[0]=L1_MODE_NULL;
1210 hfc->b_mode[1]=L1_MODE_NULL;
1212 hfc->l1_activated=FALSE;
1214 hfc->led_new_data=0;
1216 /* init the t3 timer */
1217 init_timer(&hfc->t3_timer);
1218 hfc->t3_timer.data = (long) hfc;
1219 hfc->t3_timer.function = (void *) l1_timer_expire_t3;
1220 /* init the t4 timer */
1221 init_timer(&hfc->t4_timer);
1222 hfc->t4_timer.data = (long) hfc;
1223 hfc->t4_timer.function = (void *) l1_timer_expire_t4;
1224 /* init the led timer */
1225 init_timer(&hfc->led_timer);
1226 hfc->led_timer.data = (long) hfc;
1227 hfc->led_timer.function = (void *) led_timer;
1228 // trigger 4 hz led timer
1229 hfc->led_timer.expires = jiffies + (LED_TIME * HZ) / 1000;
1230 if(!timer_pending(&hfc->led_timer)) add_timer(&hfc->led_timer);
1232 // init the background machinery for control requests
1233 hfc->ctrl_read.bRequestType = 0xc0;
1234 hfc->ctrl_read.bRequest = 1;
1235 hfc->ctrl_read.wLength = 1;
1236 hfc->ctrl_write.bRequestType = 0x40;
1237 hfc->ctrl_write.bRequest = 0;
1238 hfc->ctrl_write.wLength = 0;
1239 usb_fill_control_urb(hfc->ctrl_urb, hfc->dev, hfc->ctrl_out_pipe,(u_char *) & hfc->ctrl_write, NULL, 0, ctrl_complete, hfc);
1241 /* Init All Fifos */
1242 for(i = 0; i < HFCUSB_NUM_FIFOS; i++)
1244 hfc->fifos[i].iso[0].purb = NULL;
1245 hfc->fifos[i].iso[1].purb = NULL;
1246 hfc->fifos[i].active = 0;
1249 // register like Germaschewski :
1250 hfc->d_if.owner = THIS_MODULE;
1251 hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
1252 hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
1256 hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX+i*2];
1257 hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
1258 p_b_if[i] = &hfc->b_if[i];
1261 hfc->protocol = 2; /* default EURO ISDN, should be a module_param */
1262 hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
1265 hfc->fifos[i].hif=&p_b_if[i/2]->ifc;
1267 hfc->fifos[i].hif=&hfc->d_if.ifc;
1269 // 3 (+1) INT IN + 3 ISO OUT
1270 if(hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO)
1272 start_int_fifo(hfc->fifos + HFCUSB_D_RX); // Int IN D-fifo
1273 if(hfc->fifos[HFCUSB_PCM_RX].pipe) start_int_fifo(hfc->fifos + HFCUSB_PCM_RX); // E-fifo
1274 start_int_fifo(hfc->fifos + HFCUSB_B1_RX); // Int IN B1-fifo
1275 start_int_fifo(hfc->fifos + HFCUSB_B2_RX); // Int IN B2-fifo
1278 // 3 (+1) ISO IN + 3 ISO OUT
1279 if(hfc->cfg_used==CNF_3ISO3ISO || hfc->cfg_used==CNF_4ISO3ISO)
1281 start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D, rx_iso_complete,16);
1282 if(hfc->fifos[HFCUSB_PCM_RX].pipe) start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX, ISOC_PACKETS_D, rx_iso_complete,16);
1283 start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B, rx_iso_complete,16);
1284 start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B, rx_iso_complete,16);
1287 start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D, tx_iso_complete,1);
1288 start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B, tx_iso_complete,1);
1289 start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B, tx_iso_complete,1);
1291 handle_led(hfc,LED_POWER_ON);
1297 /****************************************/
1298 /* data defining the devices to be used */
1299 /****************************************/
1300 // static __devinitdata const struct usb_device_id hfc_usb_idtab[3] = {
1301 static struct usb_device_id hfc_usb_idtab[] = {
1302 {USB_DEVICE(0x7b0, 0x0007)}, /* Billion USB TA 2 */
1303 {USB_DEVICE(0x742, 0x2008)}, /* Stollmann USB TA */
1304 {USB_DEVICE(0x959, 0x2bd0)}, /* Colognechip USB eval TA */
1305 {USB_DEVICE(0x8e3, 0x0301)}, /* OliTec ISDN USB */
1306 {USB_DEVICE(0x675, 0x1688)}, /* DrayTec ISDN USB */
1307 {USB_DEVICE(0x7fa, 0x0846)}, /* Bewan ISDN USB TA */
1308 {} /* end with an all-zeroes entry */
1311 MODULE_AUTHOR("Peter Sprenger (sprenger@moving-byters.de)/Martin Bachem (info@colognechip.com)");
1312 MODULE_DESCRIPTION("HFC I4L USB driver");
1313 MODULE_DEVICE_TABLE(usb, hfc_usb_idtab);
1314 MODULE_LICENSE("GPL");
1316 #define EP_NUL 1 // Endpoint at this position not allowed
1317 #define EP_NOP 2 // all type of endpoints allowed at this position
1318 #define EP_ISO 3 // Isochron endpoint mandatory at this position
1319 #define EP_BLK 4 // Bulk endpoint mandatory at this position
1320 #define EP_INT 5 // Interrupt endpoint mandatory at this position
1322 // this array represents all endpoints possible in the HCF-USB
1323 // the last 2 entries are the configuration number and the minimum interval for Interrupt endpoints
1324 int validconf[][18]=
1326 // INT in, ISO out config
1327 {EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NOP,EP_INT,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_NUL,EP_NUL,CNF_4INT3ISO,2},
1328 {EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NUL,EP_INT,EP_NUL,EP_NUL,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_ISO,EP_NUL,EP_NUL,EP_NUL,CNF_3INT3ISO,2},
1329 // ISO in, ISO out config
1330 {EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_NOP,EP_ISO,CNF_4ISO3ISO,2},
1331 {EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_NUL,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_ISO,EP_NUL,EP_NUL,CNF_3ISO3ISO,2},
1332 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} // EOL element
1335 // string description of chosen config
1338 "4 Interrupt IN + 3 Isochron OUT",
1339 "3 Interrupt IN + 3 Isochron OUT",
1340 "4 Isochron IN + 3 Isochron OUT",
1341 "3 Isochron IN + 3 Isochron OUT"
1345 /*************************************************/
1346 /* function called to probe a new plugged device */
1347 /*************************************************/
1348 static int hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1350 struct usb_device *dev= interface_to_usbdev(intf);
1351 hfcusb_data *context;
1352 struct usb_host_interface *iface = intf->cur_altsetting;
1353 struct usb_host_interface *iface_used = NULL;
1354 struct usb_host_endpoint *ep;
1355 int ifnum = iface->desc.bInterfaceNumber;
1356 int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found, cidx, ep_addr;
1357 int cmptbl[16],small_match,iso_packet_size,packet_size,alt_used=0;
1359 // usb_show_device(dev);
1360 // usb_show_device_descriptor(&dev->descriptor);
1361 // usb_show_interface_descriptor(&iface->desc);
1363 for(i=0;vdata[i].vendor;i++) {
1364 if (le16_to_cpu(dev->descriptor.idVendor) == vdata[i].vendor &&
1365 le16_to_cpu(dev->descriptor.idProduct) == vdata[i].prod_id)
1370 #ifdef VERBOSE_USB_DEBUG
1371 printk(KERN_INFO "HFC-USB: probing interface(%d) actalt(%d) minor(%d)\n",
1372 ifnum, iface->desc.bAlternateSetting, intf->minor);
1375 if (vend_idx != 0xffff) {
1376 #ifdef VERBOSE_USB_DEBUG
1377 printk(KERN_INFO "HFC-USB: found vendor idx:%d name:%s\n",vend_idx,vdata[vend_idx].vend_name);
1379 /* if vendor and product ID is OK, start probing a matching alternate setting ... */
1386 while (alt_idx < intf->num_altsetting) {
1387 iface = intf->altsetting + alt_idx;
1388 probe_alt_setting = iface->desc.bAlternateSetting;
1391 #ifdef VERBOSE_USB_DEBUG
1392 printk(KERN_INFO "HFC-USB: test alt_setting %d\n", probe_alt_setting);
1394 // check for config EOL element
1395 while (validconf[cfg_used][0]) {
1397 vcf=validconf[cfg_used];
1398 ep = iface->endpoint; /* first endpoint descriptor */
1400 #ifdef VERBOSE_USB_DEBUG
1401 printk(KERN_INFO "HFC-USB: (if=%d alt=%d cfg_used=%d)\n",
1402 ifnum, probe_alt_setting, cfg_used);
1405 memcpy(cmptbl,vcf,16*sizeof(int));
1407 // check for all endpoints in this alternate setting
1408 for (i=0; i < iface->desc.bNumEndpoints; i++) {
1409 ep_addr = ep->desc.bEndpointAddress;
1410 idx = ((ep_addr & 0x7f)-1)*2; /* get endpoint base */
1413 attr = ep->desc.bmAttributes;
1415 if (cmptbl[idx] == EP_NUL) {
1416 printk(KERN_INFO "HFC-USB: cfg_found=FALSE in idx:%d attr:%d cmptbl[%d]:%d\n",
1417 idx, attr, idx, cmptbl[idx]);
1421 if (attr == USB_ENDPOINT_XFER_INT && cmptbl[idx] == EP_INT)
1422 cmptbl[idx] = EP_NUL;
1423 if (attr == USB_ENDPOINT_XFER_BULK && cmptbl[idx] == EP_BLK)
1424 cmptbl[idx] = EP_NUL;
1425 if (attr == USB_ENDPOINT_XFER_ISOC && cmptbl[idx] == EP_ISO)
1426 cmptbl[idx] = EP_NUL;
1428 // check if all INT endpoints match minimum interval
1429 if (attr == USB_ENDPOINT_XFER_INT && ep->desc.bInterval < vcf[17]) {
1430 #ifdef VERBOSE_USB_DEBUG
1432 printk(KERN_INFO "HFC-USB: Interrupt Endpoint interval < %d found - skipping config\n",
1441 for (i = 0; i < 16; i++) {
1442 // printk(KERN_INFO "HFC-USB: cmptbl[%d]:%d\n", i, cmptbl[i]);
1444 // all entries must be EP_NOP or EP_NUL for a valid config
1445 if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
1449 // we check for smallest match, to provide configuration priority
1450 // configurations with smaller index have higher priority
1452 if (cfg_used < small_match) {
1453 small_match = cfg_used;
1454 alt_used = probe_alt_setting;
1457 #ifdef VERBOSE_USB_DEBUG
1458 printk(KERN_INFO "HFC-USB: small_match=%x %x\n", small_match, alt_used);
1466 } /* (alt_idx < intf->num_altsetting) */
1467 #ifdef VERBOSE_USB_DEBUG
1468 printk(KERN_INFO "HFC-USB: final small_match=%x alt_used=%x\n",small_match, alt_used);
1470 // yiipiee, we found a valid config
1471 if (small_match != 0xffff) {
1474 if (!(context = kmalloc(sizeof(hfcusb_data), GFP_KERNEL)))
1475 return(-ENOMEM); /* got no mem */
1476 memset(context, 0, sizeof(hfcusb_data)); /* clear the structure */
1478 ep = iface->endpoint; /* first endpoint descriptor */
1479 vcf = validconf[small_match];
1481 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1482 ep_addr = ep->desc.bEndpointAddress;
1483 idx = ((ep_addr & 0x7f)-1)*2; /* get endpoint base */
1487 attr = ep->desc.bmAttributes;
1489 // only initialize used endpoints
1490 if (vcf[idx] != EP_NOP && vcf[idx] != EP_NUL) {
1492 case USB_ENDPOINT_XFER_INT:
1493 context->fifos[cidx].pipe = usb_rcvintpipe(dev, ep->desc.bEndpointAddress);
1494 context->fifos[cidx].usb_transfer_mode = USB_INT;
1495 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize); // remember max packet size
1496 #ifdef VERBOSE_USB_DEBUG
1497 printk (KERN_INFO "HFC-USB: Interrupt-In Endpoint found %d ms(idx:%d cidx:%d)!\n",
1498 ep->desc.bInterval, idx, cidx);
1501 case USB_ENDPOINT_XFER_BULK:
1503 context->fifos[cidx].pipe = usb_rcvbulkpipe(dev, ep->desc.bEndpointAddress);
1505 context->fifos[cidx].pipe = usb_sndbulkpipe(dev, ep->desc.bEndpointAddress);
1506 context->fifos[cidx].usb_transfer_mode = USB_BULK;
1507 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize); // remember max packet size
1508 #ifdef VERBOSE_USB_DEBUG
1509 printk (KERN_INFO "HFC-USB: Bulk Endpoint found (idx:%d cidx:%d)!\n",
1513 case USB_ENDPOINT_XFER_ISOC:
1515 context->fifos[cidx].pipe = usb_rcvisocpipe(dev, ep->desc.bEndpointAddress);
1517 context->fifos[cidx].pipe = usb_sndisocpipe(dev, ep->desc.bEndpointAddress);
1518 context->fifos[cidx].usb_transfer_mode = USB_ISOC;
1519 iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize); // remember max packet size
1520 #ifdef VERBOSE_USB_DEBUG
1521 printk (KERN_INFO "HFC-USB: ISO Endpoint found (idx:%d cidx:%d)!\n",
1526 context->fifos[cidx].pipe = 0; /* reset data */
1527 } /* switch attribute */
1529 if (context->fifos[cidx].pipe) {
1530 context->fifos[cidx].fifonum = cidx;
1531 context->fifos[cidx].hfc = context;
1532 context->fifos[cidx].usb_packet_maxlen = le16_to_cpu(ep->desc.wMaxPacketSize);
1533 context->fifos[cidx].intervall = ep->desc.bInterval;
1534 context->fifos[cidx].skbuff = NULL;
1535 #ifdef VERBOSE_USB_DEBUG
1536 printk (KERN_INFO "HFC-USB: fifo%d pktlen %d interval %d\n",
1537 context->fifos[cidx].fifonum,
1538 context->fifos[cidx].usb_packet_maxlen,
1539 context->fifos[cidx].intervall);
1547 // now share our luck
1548 context->dev = dev; /* save device */
1549 context->if_used = ifnum; /* save used interface */
1550 context->alt_used = alt_used; /* and alternate config */
1551 context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
1552 context->cfg_used=vcf[16]; // store used config
1553 context->vend_idx=vend_idx; // store found vendor
1554 context->packet_size=packet_size;
1555 context->iso_packet_size=iso_packet_size;
1557 /* create the control pipes needed for register access */
1558 context->ctrl_in_pipe = usb_rcvctrlpipe(context->dev, 0);
1559 context->ctrl_out_pipe = usb_sndctrlpipe(context->dev, 0);
1560 context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1562 printk(KERN_INFO "HFC-USB: detected \"%s\" configuration: %s (if=%d alt=%d)\n",
1563 vdata[vend_idx].vend_name, conf_str[small_match], context->if_used, context->alt_used);
1565 /* init the chip and register the driver */
1566 if (usb_init(context))
1568 if (context->ctrl_urb) {
1569 usb_unlink_urb(context->ctrl_urb);
1570 usb_free_urb(context->ctrl_urb);
1571 context->ctrl_urb = NULL;
1576 usb_set_intfdata(intf, context);
1583 /****************************************************/
1584 /* function called when an active device is removed */
1585 /****************************************************/
1586 static void hfc_usb_disconnect(struct usb_interface *intf)
1588 hfcusb_data *context = usb_get_intfdata(intf);
1591 printk(KERN_INFO "HFC-USB: device disconnect\n");
1593 usb_set_intfdata(intf, NULL);
1596 if (timer_pending(&context->t3_timer))
1597 del_timer(&context->t3_timer);
1598 if (timer_pending(&context->t4_timer))
1599 del_timer(&context->t4_timer);
1600 if (timer_pending(&context->led_timer))
1601 del_timer(&context->led_timer);
1603 hisax_unregister(&context->d_if);
1605 /* tell all fifos to terminate */
1606 for(i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1607 if(context->fifos[i].usb_transfer_mode == USB_ISOC) {
1608 if(context->fifos[i].active > 0) {
1609 stop_isoc_chain(&context->fifos[i]);
1610 #ifdef VERBOSE_USB_DEBUG
1611 printk (KERN_INFO "HFC-USB: hfc_usb_disconnect: stopping ISOC chain Fifo no %i\n", i);
1615 if(context->fifos[i].active > 0) {
1616 context->fifos[i].active = 0;
1617 #ifdef VERBOSE_USB_DEBUG
1618 printk (KERN_INFO "HFC-USB: hfc_usb_disconnect: unlinking URB for Fifo no %i\n", i);
1621 if (context->fifos[i].urb) {
1622 usb_unlink_urb(context->fifos[i].urb);
1623 usb_free_urb(context->fifos[i].urb);
1624 context->fifos[i].urb = NULL;
1627 context->fifos[i].active = 0;
1629 if (context->ctrl_urb) {
1630 usb_unlink_urb(context->ctrl_urb);
1631 usb_free_urb(context->ctrl_urb);
1632 context->ctrl_urb = NULL;
1634 kfree(context); /* free our structure again */
1635 } /* hfc_usb_disconnect */
1638 /************************************/
1639 /* our driver information structure */
1640 /************************************/
1641 static struct usb_driver hfc_drv = {
1642 .owner = THIS_MODULE,
1644 .id_table = hfc_usb_idtab,
1645 .probe = hfc_usb_probe,
1646 .disconnect = hfc_usb_disconnect,
1649 static void __exit hfc_usb_exit(void)
1651 #ifdef VERBOSE_USB_DEBUG
1652 printk ("HFC-USB: calling \"hfc_usb_exit\" ...\n");
1654 usb_deregister(&hfc_drv); /* release our driver */
1655 printk(KERN_INFO "HFC-USB module removed\n");
1658 static int __init hfc_usb_init(void)
1660 printk ("HFC-USB: driver module revision %s loaded\n", hfcusb_revision);
1662 if(usb_register(&hfc_drv))
1664 printk(KERN_INFO "HFC-USB: Unable to register HFC-USB module at usb stack\n");
1665 return(-1); /* unable to register */
1670 module_init(hfc_usb_init);
1671 module_exit(hfc_usb_exit);