x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / drivers / isdn / hardware / mISDN / hfcsusb.c
blobfc46a26cb14fab046d417fa35142995bcb48b3d5
1 /* hfcsusb.c
2 * mISDN driver for Colognechip HFC-S USB chip
4 * Copyright 2001 by Peter Sprenger (sprenger@moving-bytes.de)
5 * Copyright 2008 by Martin Bachem (info@bachem-it.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * module params
23 * debug=<n>, default=0, with n=0xHHHHGGGG
24 * H - l1 driver flags described in hfcsusb.h
25 * G - common mISDN debug flags described at mISDNhw.h
27 * poll=<n>, default 128
28 * n : burst size of PH_DATA_IND at transparent rx data
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/usb.h>
35 #include <linux/mISDNhw.h>
36 #include "hfcsusb.h"
38 static const char *hfcsusb_rev = "Revision: 0.3.3 (socket), 2008-11-05";
40 static unsigned int debug;
41 static int poll = DEFAULT_TRANSP_BURST_SZ;
43 static LIST_HEAD(HFClist);
44 static DEFINE_RWLOCK(HFClock);
47 MODULE_AUTHOR("Martin Bachem");
48 MODULE_LICENSE("GPL");
49 module_param(debug, uint, S_IRUGO | S_IWUSR);
50 module_param(poll, int, 0);
52 static int hfcsusb_cnt;
54 /* some function prototypes */
55 static void hfcsusb_ph_command(struct hfcsusb *hw, u_char command);
56 static void release_hw(struct hfcsusb *hw);
57 static void reset_hfcsusb(struct hfcsusb *hw);
58 static void setPortMode(struct hfcsusb *hw);
59 static void hfcsusb_start_endpoint(struct hfcsusb *hw, int channel);
60 static void hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel);
61 static int hfcsusb_setup_bch(struct bchannel *bch, int protocol);
62 static void deactivate_bchannel(struct bchannel *bch);
63 static void hfcsusb_ph_info(struct hfcsusb *hw);
65 /* start next background transfer for control channel */
66 static void
67 ctrl_start_transfer(struct hfcsusb *hw)
69 if (debug & DBG_HFC_CALL_TRACE)
70 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
72 if (hw->ctrl_cnt) {
73 hw->ctrl_urb->pipe = hw->ctrl_out_pipe;
74 hw->ctrl_urb->setup_packet = (u_char *)&hw->ctrl_write;
75 hw->ctrl_urb->transfer_buffer = NULL;
76 hw->ctrl_urb->transfer_buffer_length = 0;
77 hw->ctrl_write.wIndex =
78 cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].hfcs_reg);
79 hw->ctrl_write.wValue =
80 cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].reg_val);
82 usb_submit_urb(hw->ctrl_urb, GFP_ATOMIC);
87 * queue a control transfer request to write HFC-S USB
88 * chip register using CTRL resuest queue
90 static int write_reg(struct hfcsusb *hw, __u8 reg, __u8 val)
92 struct ctrl_buf *buf;
94 if (debug & DBG_HFC_CALL_TRACE)
95 printk(KERN_DEBUG "%s: %s reg(0x%02x) val(0x%02x)\n",
96 hw->name, __func__, reg, val);
98 spin_lock(&hw->ctrl_lock);
99 if (hw->ctrl_cnt >= HFC_CTRL_BUFSIZE)
100 return 1;
101 buf = &hw->ctrl_buff[hw->ctrl_in_idx];
102 buf->hfcs_reg = reg;
103 buf->reg_val = val;
104 if (++hw->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
105 hw->ctrl_in_idx = 0;
106 if (++hw->ctrl_cnt == 1)
107 ctrl_start_transfer(hw);
108 spin_unlock(&hw->ctrl_lock);
110 return 0;
113 /* control completion routine handling background control cmds */
114 static void
115 ctrl_complete(struct urb *urb)
117 struct hfcsusb *hw = (struct hfcsusb *) urb->context;
118 struct ctrl_buf *buf;
120 if (debug & DBG_HFC_CALL_TRACE)
121 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
123 urb->dev = hw->dev;
124 if (hw->ctrl_cnt) {
125 buf = &hw->ctrl_buff[hw->ctrl_out_idx];
126 hw->ctrl_cnt--; /* decrement actual count */
127 if (++hw->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
128 hw->ctrl_out_idx = 0; /* pointer wrap */
130 ctrl_start_transfer(hw); /* start next transfer */
134 /* handle LED bits */
135 static void
136 set_led_bit(struct hfcsusb *hw, signed short led_bits, int set_on)
138 if (set_on) {
139 if (led_bits < 0)
140 hw->led_state &= ~abs(led_bits);
141 else
142 hw->led_state |= led_bits;
143 } else {
144 if (led_bits < 0)
145 hw->led_state |= abs(led_bits);
146 else
147 hw->led_state &= ~led_bits;
151 /* handle LED requests */
152 static void
153 handle_led(struct hfcsusb *hw, int event)
155 struct hfcsusb_vdata *driver_info = (struct hfcsusb_vdata *)
156 hfcsusb_idtab[hw->vend_idx].driver_info;
157 __u8 tmpled;
159 if (driver_info->led_scheme == LED_OFF)
160 return;
161 tmpled = hw->led_state;
163 switch (event) {
164 case LED_POWER_ON:
165 set_led_bit(hw, driver_info->led_bits[0], 1);
166 set_led_bit(hw, driver_info->led_bits[1], 0);
167 set_led_bit(hw, driver_info->led_bits[2], 0);
168 set_led_bit(hw, driver_info->led_bits[3], 0);
169 break;
170 case LED_POWER_OFF:
171 set_led_bit(hw, driver_info->led_bits[0], 0);
172 set_led_bit(hw, driver_info->led_bits[1], 0);
173 set_led_bit(hw, driver_info->led_bits[2], 0);
174 set_led_bit(hw, driver_info->led_bits[3], 0);
175 break;
176 case LED_S0_ON:
177 set_led_bit(hw, driver_info->led_bits[1], 1);
178 break;
179 case LED_S0_OFF:
180 set_led_bit(hw, driver_info->led_bits[1], 0);
181 break;
182 case LED_B1_ON:
183 set_led_bit(hw, driver_info->led_bits[2], 1);
184 break;
185 case LED_B1_OFF:
186 set_led_bit(hw, driver_info->led_bits[2], 0);
187 break;
188 case LED_B2_ON:
189 set_led_bit(hw, driver_info->led_bits[3], 1);
190 break;
191 case LED_B2_OFF:
192 set_led_bit(hw, driver_info->led_bits[3], 0);
193 break;
196 if (hw->led_state != tmpled) {
197 if (debug & DBG_HFC_CALL_TRACE)
198 printk(KERN_DEBUG "%s: %s reg(0x%02x) val(x%02x)\n",
199 hw->name, __func__,
200 HFCUSB_P_DATA, hw->led_state);
202 write_reg(hw, HFCUSB_P_DATA, hw->led_state);
207 * Layer2 -> Layer 1 Bchannel data
209 static int
210 hfcusb_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
212 struct bchannel *bch = container_of(ch, struct bchannel, ch);
213 struct hfcsusb *hw = bch->hw;
214 int ret = -EINVAL;
215 struct mISDNhead *hh = mISDN_HEAD_P(skb);
216 u_long flags;
218 if (debug & DBG_HFC_CALL_TRACE)
219 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
221 switch (hh->prim) {
222 case PH_DATA_REQ:
223 spin_lock_irqsave(&hw->lock, flags);
224 ret = bchannel_senddata(bch, skb);
225 spin_unlock_irqrestore(&hw->lock, flags);
226 if (debug & DBG_HFC_CALL_TRACE)
227 printk(KERN_DEBUG "%s: %s PH_DATA_REQ ret(%i)\n",
228 hw->name, __func__, ret);
229 if (ret > 0) {
231 * other l1 drivers don't send early confirms on
232 * transp data, but hfcsusb does because tx_next
233 * skb is needed in tx_iso_complete()
235 queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
236 ret = 0;
238 return ret;
239 case PH_ACTIVATE_REQ:
240 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
241 hfcsusb_start_endpoint(hw, bch->nr);
242 ret = hfcsusb_setup_bch(bch, ch->protocol);
243 } else
244 ret = 0;
245 if (!ret)
246 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
247 0, NULL, GFP_KERNEL);
248 break;
249 case PH_DEACTIVATE_REQ:
250 deactivate_bchannel(bch);
251 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY,
252 0, NULL, GFP_KERNEL);
253 ret = 0;
254 break;
256 if (!ret)
257 dev_kfree_skb(skb);
258 return ret;
262 * send full D/B channel status information
263 * as MPH_INFORMATION_IND
265 static void
266 hfcsusb_ph_info(struct hfcsusb *hw)
268 struct ph_info *phi;
269 struct dchannel *dch = &hw->dch;
270 int i;
272 phi = kzalloc(sizeof(struct ph_info) +
273 dch->dev.nrbchan * sizeof(struct ph_info_ch), GFP_ATOMIC);
274 phi->dch.ch.protocol = hw->protocol;
275 phi->dch.ch.Flags = dch->Flags;
276 phi->dch.state = dch->state;
277 phi->dch.num_bch = dch->dev.nrbchan;
278 for (i = 0; i < dch->dev.nrbchan; i++) {
279 phi->bch[i].protocol = hw->bch[i].ch.protocol;
280 phi->bch[i].Flags = hw->bch[i].Flags;
282 _queue_data(&dch->dev.D, MPH_INFORMATION_IND, MISDN_ID_ANY,
283 sizeof(struct ph_info_dch) + dch->dev.nrbchan *
284 sizeof(struct ph_info_ch), phi, GFP_ATOMIC);
288 * Layer2 -> Layer 1 Dchannel data
290 static int
291 hfcusb_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
293 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
294 struct dchannel *dch = container_of(dev, struct dchannel, dev);
295 struct mISDNhead *hh = mISDN_HEAD_P(skb);
296 struct hfcsusb *hw = dch->hw;
297 int ret = -EINVAL;
298 u_long flags;
300 switch (hh->prim) {
301 case PH_DATA_REQ:
302 if (debug & DBG_HFC_CALL_TRACE)
303 printk(KERN_DEBUG "%s: %s: PH_DATA_REQ\n",
304 hw->name, __func__);
306 spin_lock_irqsave(&hw->lock, flags);
307 ret = dchannel_senddata(dch, skb);
308 spin_unlock_irqrestore(&hw->lock, flags);
309 if (ret > 0) {
310 ret = 0;
311 queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
313 break;
315 case PH_ACTIVATE_REQ:
316 if (debug & DBG_HFC_CALL_TRACE)
317 printk(KERN_DEBUG "%s: %s: PH_ACTIVATE_REQ %s\n",
318 hw->name, __func__,
319 (hw->protocol == ISDN_P_NT_S0) ? "NT" : "TE");
321 if (hw->protocol == ISDN_P_NT_S0) {
322 ret = 0;
323 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
324 _queue_data(&dch->dev.D,
325 PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
326 NULL, GFP_ATOMIC);
327 } else {
328 hfcsusb_ph_command(hw,
329 HFC_L1_ACTIVATE_NT);
330 test_and_set_bit(FLG_L2_ACTIVATED,
331 &dch->Flags);
333 } else {
334 hfcsusb_ph_command(hw, HFC_L1_ACTIVATE_TE);
335 ret = l1_event(dch->l1, hh->prim);
337 break;
339 case PH_DEACTIVATE_REQ:
340 if (debug & DBG_HFC_CALL_TRACE)
341 printk(KERN_DEBUG "%s: %s: PH_DEACTIVATE_REQ\n",
342 hw->name, __func__);
343 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
345 if (hw->protocol == ISDN_P_NT_S0) {
346 hfcsusb_ph_command(hw, HFC_L1_DEACTIVATE_NT);
347 spin_lock_irqsave(&hw->lock, flags);
348 skb_queue_purge(&dch->squeue);
349 if (dch->tx_skb) {
350 dev_kfree_skb(dch->tx_skb);
351 dch->tx_skb = NULL;
353 dch->tx_idx = 0;
354 if (dch->rx_skb) {
355 dev_kfree_skb(dch->rx_skb);
356 dch->rx_skb = NULL;
358 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
359 spin_unlock_irqrestore(&hw->lock, flags);
360 #ifdef FIXME
361 if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
362 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
363 #endif
364 ret = 0;
365 } else
366 ret = l1_event(dch->l1, hh->prim);
367 break;
368 case MPH_INFORMATION_REQ:
369 hfcsusb_ph_info(hw);
370 ret = 0;
371 break;
374 return ret;
378 * Layer 1 callback function
380 static int
381 hfc_l1callback(struct dchannel *dch, u_int cmd)
383 struct hfcsusb *hw = dch->hw;
385 if (debug & DBG_HFC_CALL_TRACE)
386 printk(KERN_DEBUG "%s: %s cmd 0x%x\n",
387 hw->name, __func__, cmd);
389 switch (cmd) {
390 case INFO3_P8:
391 case INFO3_P10:
392 case HW_RESET_REQ:
393 case HW_POWERUP_REQ:
394 break;
396 case HW_DEACT_REQ:
397 skb_queue_purge(&dch->squeue);
398 if (dch->tx_skb) {
399 dev_kfree_skb(dch->tx_skb);
400 dch->tx_skb = NULL;
402 dch->tx_idx = 0;
403 if (dch->rx_skb) {
404 dev_kfree_skb(dch->rx_skb);
405 dch->rx_skb = NULL;
407 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
408 break;
409 case PH_ACTIVATE_IND:
410 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
411 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
412 GFP_ATOMIC);
413 break;
414 case PH_DEACTIVATE_IND:
415 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
416 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
417 GFP_ATOMIC);
418 break;
419 default:
420 if (dch->debug & DEBUG_HW)
421 printk(KERN_DEBUG "%s: %s: unknown cmd %x\n",
422 hw->name, __func__, cmd);
423 return -1;
425 hfcsusb_ph_info(hw);
426 return 0;
429 static int
430 open_dchannel(struct hfcsusb *hw, struct mISDNchannel *ch,
431 struct channel_req *rq)
433 int err = 0;
435 if (debug & DEBUG_HW_OPEN)
436 printk(KERN_DEBUG "%s: %s: dev(%d) open addr(%i) from %p\n",
437 hw->name, __func__, hw->dch.dev.id, rq->adr.channel,
438 __builtin_return_address(0));
439 if (rq->protocol == ISDN_P_NONE)
440 return -EINVAL;
442 test_and_clear_bit(FLG_ACTIVE, &hw->dch.Flags);
443 test_and_clear_bit(FLG_ACTIVE, &hw->ech.Flags);
444 hfcsusb_start_endpoint(hw, HFC_CHAN_D);
446 /* E-Channel logging */
447 if (rq->adr.channel == 1) {
448 if (hw->fifos[HFCUSB_PCM_RX].pipe) {
449 hfcsusb_start_endpoint(hw, HFC_CHAN_E);
450 set_bit(FLG_ACTIVE, &hw->ech.Flags);
451 _queue_data(&hw->ech.dev.D, PH_ACTIVATE_IND,
452 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
453 } else
454 return -EINVAL;
457 if (!hw->initdone) {
458 hw->protocol = rq->protocol;
459 if (rq->protocol == ISDN_P_TE_S0) {
460 err = create_l1(&hw->dch, hfc_l1callback);
461 if (err)
462 return err;
464 setPortMode(hw);
465 ch->protocol = rq->protocol;
466 hw->initdone = 1;
467 } else {
468 if (rq->protocol != ch->protocol)
469 return -EPROTONOSUPPORT;
472 if (((ch->protocol == ISDN_P_NT_S0) && (hw->dch.state == 3)) ||
473 ((ch->protocol == ISDN_P_TE_S0) && (hw->dch.state == 7)))
474 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
475 0, NULL, GFP_KERNEL);
476 rq->ch = ch;
477 if (!try_module_get(THIS_MODULE))
478 printk(KERN_WARNING "%s: %s: cannot get module\n",
479 hw->name, __func__);
480 return 0;
483 static int
484 open_bchannel(struct hfcsusb *hw, struct channel_req *rq)
486 struct bchannel *bch;
488 if (rq->adr.channel > 2)
489 return -EINVAL;
490 if (rq->protocol == ISDN_P_NONE)
491 return -EINVAL;
493 if (debug & DBG_HFC_CALL_TRACE)
494 printk(KERN_DEBUG "%s: %s B%i\n",
495 hw->name, __func__, rq->adr.channel);
497 bch = &hw->bch[rq->adr.channel - 1];
498 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
499 return -EBUSY; /* b-channel can be only open once */
500 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
501 bch->ch.protocol = rq->protocol;
502 rq->ch = &bch->ch;
504 /* start USB endpoint for bchannel */
505 if (rq->adr.channel == 1)
506 hfcsusb_start_endpoint(hw, HFC_CHAN_B1);
507 else
508 hfcsusb_start_endpoint(hw, HFC_CHAN_B2);
510 if (!try_module_get(THIS_MODULE))
511 printk(KERN_WARNING "%s: %s:cannot get module\n",
512 hw->name, __func__);
513 return 0;
516 static int
517 channel_ctrl(struct hfcsusb *hw, struct mISDN_ctrl_req *cq)
519 int ret = 0;
521 if (debug & DBG_HFC_CALL_TRACE)
522 printk(KERN_DEBUG "%s: %s op(0x%x) channel(0x%x)\n",
523 hw->name, __func__, (cq->op), (cq->channel));
525 switch (cq->op) {
526 case MISDN_CTRL_GETOP:
527 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
528 MISDN_CTRL_DISCONNECT;
529 break;
530 default:
531 printk(KERN_WARNING "%s: %s: unknown Op %x\n",
532 hw->name, __func__, cq->op);
533 ret = -EINVAL;
534 break;
536 return ret;
540 * device control function
542 static int
543 hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
545 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
546 struct dchannel *dch = container_of(dev, struct dchannel, dev);
547 struct hfcsusb *hw = dch->hw;
548 struct channel_req *rq;
549 int err = 0;
551 if (dch->debug & DEBUG_HW)
552 printk(KERN_DEBUG "%s: %s: cmd:%x %p\n",
553 hw->name, __func__, cmd, arg);
554 switch (cmd) {
555 case OPEN_CHANNEL:
556 rq = arg;
557 if ((rq->protocol == ISDN_P_TE_S0) ||
558 (rq->protocol == ISDN_P_NT_S0))
559 err = open_dchannel(hw, ch, rq);
560 else
561 err = open_bchannel(hw, rq);
562 if (!err)
563 hw->open++;
564 break;
565 case CLOSE_CHANNEL:
566 hw->open--;
567 if (debug & DEBUG_HW_OPEN)
568 printk(KERN_DEBUG
569 "%s: %s: dev(%d) close from %p (open %d)\n",
570 hw->name, __func__, hw->dch.dev.id,
571 __builtin_return_address(0), hw->open);
572 if (!hw->open) {
573 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
574 if (hw->fifos[HFCUSB_PCM_RX].pipe)
575 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
576 handle_led(hw, LED_POWER_ON);
578 module_put(THIS_MODULE);
579 break;
580 case CONTROL_CHANNEL:
581 err = channel_ctrl(hw, arg);
582 break;
583 default:
584 if (dch->debug & DEBUG_HW)
585 printk(KERN_DEBUG "%s: %s: unknown command %x\n",
586 hw->name, __func__, cmd);
587 return -EINVAL;
589 return err;
593 * S0 TE state change event handler
595 static void
596 ph_state_te(struct dchannel *dch)
598 struct hfcsusb *hw = dch->hw;
600 if (debug & DEBUG_HW) {
601 if (dch->state <= HFC_MAX_TE_LAYER1_STATE)
602 printk(KERN_DEBUG "%s: %s: %s\n", hw->name, __func__,
603 HFC_TE_LAYER1_STATES[dch->state]);
604 else
605 printk(KERN_DEBUG "%s: %s: TE F%d\n",
606 hw->name, __func__, dch->state);
609 switch (dch->state) {
610 case 0:
611 l1_event(dch->l1, HW_RESET_IND);
612 break;
613 case 3:
614 l1_event(dch->l1, HW_DEACT_IND);
615 break;
616 case 5:
617 case 8:
618 l1_event(dch->l1, ANYSIGNAL);
619 break;
620 case 6:
621 l1_event(dch->l1, INFO2);
622 break;
623 case 7:
624 l1_event(dch->l1, INFO4_P8);
625 break;
627 if (dch->state == 7)
628 handle_led(hw, LED_S0_ON);
629 else
630 handle_led(hw, LED_S0_OFF);
634 * S0 NT state change event handler
636 static void
637 ph_state_nt(struct dchannel *dch)
639 struct hfcsusb *hw = dch->hw;
641 if (debug & DEBUG_HW) {
642 if (dch->state <= HFC_MAX_NT_LAYER1_STATE)
643 printk(KERN_DEBUG "%s: %s: %s\n",
644 hw->name, __func__,
645 HFC_NT_LAYER1_STATES[dch->state]);
647 else
648 printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
649 hw->name, __func__, dch->state);
652 switch (dch->state) {
653 case (1):
654 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
655 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
656 hw->nt_timer = 0;
657 hw->timers &= ~NT_ACTIVATION_TIMER;
658 handle_led(hw, LED_S0_OFF);
659 break;
661 case (2):
662 if (hw->nt_timer < 0) {
663 hw->nt_timer = 0;
664 hw->timers &= ~NT_ACTIVATION_TIMER;
665 hfcsusb_ph_command(dch->hw, HFC_L1_DEACTIVATE_NT);
666 } else {
667 hw->timers |= NT_ACTIVATION_TIMER;
668 hw->nt_timer = NT_T1_COUNT;
669 /* allow G2 -> G3 transition */
670 write_reg(hw, HFCUSB_STATES, 2 | HFCUSB_NT_G2_G3);
672 break;
673 case (3):
674 hw->nt_timer = 0;
675 hw->timers &= ~NT_ACTIVATION_TIMER;
676 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
677 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
678 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
679 handle_led(hw, LED_S0_ON);
680 break;
681 case (4):
682 hw->nt_timer = 0;
683 hw->timers &= ~NT_ACTIVATION_TIMER;
684 break;
685 default:
686 break;
688 hfcsusb_ph_info(hw);
691 static void
692 ph_state(struct dchannel *dch)
694 struct hfcsusb *hw = dch->hw;
696 if (hw->protocol == ISDN_P_NT_S0)
697 ph_state_nt(dch);
698 else if (hw->protocol == ISDN_P_TE_S0)
699 ph_state_te(dch);
703 * disable/enable BChannel for desired protocoll
705 static int
706 hfcsusb_setup_bch(struct bchannel *bch, int protocol)
708 struct hfcsusb *hw = bch->hw;
709 __u8 conhdlc, sctrl, sctrl_r;
711 if (debug & DEBUG_HW)
712 printk(KERN_DEBUG "%s: %s: protocol %x-->%x B%d\n",
713 hw->name, __func__, bch->state, protocol,
714 bch->nr);
716 /* setup val for CON_HDLC */
717 conhdlc = 0;
718 if (protocol > ISDN_P_NONE)
719 conhdlc = 8; /* enable FIFO */
721 switch (protocol) {
722 case (-1): /* used for init */
723 bch->state = -1;
724 /* fall trough */
725 case (ISDN_P_NONE):
726 if (bch->state == ISDN_P_NONE)
727 return 0; /* already in idle state */
728 bch->state = ISDN_P_NONE;
729 clear_bit(FLG_HDLC, &bch->Flags);
730 clear_bit(FLG_TRANSPARENT, &bch->Flags);
731 break;
732 case (ISDN_P_B_RAW):
733 conhdlc |= 2;
734 bch->state = protocol;
735 set_bit(FLG_TRANSPARENT, &bch->Flags);
736 break;
737 case (ISDN_P_B_HDLC):
738 bch->state = protocol;
739 set_bit(FLG_HDLC, &bch->Flags);
740 break;
741 default:
742 if (debug & DEBUG_HW)
743 printk(KERN_DEBUG "%s: %s: prot not known %x\n",
744 hw->name, __func__, protocol);
745 return -ENOPROTOOPT;
748 if (protocol >= ISDN_P_NONE) {
749 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 0 : 2);
750 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
751 write_reg(hw, HFCUSB_INC_RES_F, 2);
752 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 1 : 3);
753 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
754 write_reg(hw, HFCUSB_INC_RES_F, 2);
756 sctrl = 0x40 + ((hw->protocol == ISDN_P_TE_S0) ? 0x00 : 0x04);
757 sctrl_r = 0x0;
758 if (test_bit(FLG_ACTIVE, &hw->bch[0].Flags)) {
759 sctrl |= 1;
760 sctrl_r |= 1;
762 if (test_bit(FLG_ACTIVE, &hw->bch[1].Flags)) {
763 sctrl |= 2;
764 sctrl_r |= 2;
766 write_reg(hw, HFCUSB_SCTRL, sctrl);
767 write_reg(hw, HFCUSB_SCTRL_R, sctrl_r);
769 if (protocol > ISDN_P_NONE)
770 handle_led(hw, (bch->nr == 1) ? LED_B1_ON : LED_B2_ON);
771 else
772 handle_led(hw, (bch->nr == 1) ? LED_B1_OFF :
773 LED_B2_OFF);
775 hfcsusb_ph_info(hw);
776 return 0;
779 static void
780 hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
782 if (debug & DEBUG_HW)
783 printk(KERN_DEBUG "%s: %s: %x\n",
784 hw->name, __func__, command);
786 switch (command) {
787 case HFC_L1_ACTIVATE_TE:
788 /* force sending sending INFO1 */
789 write_reg(hw, HFCUSB_STATES, 0x14);
790 /* start l1 activation */
791 write_reg(hw, HFCUSB_STATES, 0x04);
792 break;
794 case HFC_L1_FORCE_DEACTIVATE_TE:
795 write_reg(hw, HFCUSB_STATES, 0x10);
796 write_reg(hw, HFCUSB_STATES, 0x03);
797 break;
799 case HFC_L1_ACTIVATE_NT:
800 if (hw->dch.state == 3)
801 _queue_data(&hw->dch.dev.D, PH_ACTIVATE_IND,
802 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
803 else
804 write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
805 HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
806 break;
808 case HFC_L1_DEACTIVATE_NT:
809 write_reg(hw, HFCUSB_STATES,
810 HFCUSB_DO_ACTION);
811 break;
816 * Layer 1 B-channel hardware access
818 static int
819 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
821 int ret = 0;
823 switch (cq->op) {
824 case MISDN_CTRL_GETOP:
825 cq->op = MISDN_CTRL_FILL_EMPTY;
826 break;
827 case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
828 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
829 if (debug & DEBUG_HW_OPEN)
830 printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
831 "off=%d)\n", __func__, bch->nr, !!cq->p1);
832 break;
833 default:
834 printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
835 ret = -EINVAL;
836 break;
838 return ret;
841 /* collect data from incoming interrupt or isochron USB data */
842 static void
843 hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
844 int finish)
846 struct hfcsusb *hw = fifo->hw;
847 struct sk_buff *rx_skb = NULL;
848 int maxlen = 0;
849 int fifon = fifo->fifonum;
850 int i;
851 int hdlc = 0;
853 if (debug & DBG_HFC_CALL_TRACE)
854 printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
855 "dch(%p) bch(%p) ech(%p)\n",
856 hw->name, __func__, fifon, len,
857 fifo->dch, fifo->bch, fifo->ech);
859 if (!len)
860 return;
862 if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
863 printk(KERN_DEBUG "%s: %s: undefined channel\n",
864 hw->name, __func__);
865 return;
868 spin_lock(&hw->lock);
869 if (fifo->dch) {
870 rx_skb = fifo->dch->rx_skb;
871 maxlen = fifo->dch->maxlen;
872 hdlc = 1;
874 if (fifo->bch) {
875 rx_skb = fifo->bch->rx_skb;
876 maxlen = fifo->bch->maxlen;
877 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
879 if (fifo->ech) {
880 rx_skb = fifo->ech->rx_skb;
881 maxlen = fifo->ech->maxlen;
882 hdlc = 1;
885 if (!rx_skb) {
886 rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
887 if (rx_skb) {
888 if (fifo->dch)
889 fifo->dch->rx_skb = rx_skb;
890 if (fifo->bch)
891 fifo->bch->rx_skb = rx_skb;
892 if (fifo->ech)
893 fifo->ech->rx_skb = rx_skb;
894 skb_trim(rx_skb, 0);
895 } else {
896 printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
897 hw->name, __func__);
898 spin_unlock(&hw->lock);
899 return;
903 if (fifo->dch || fifo->ech) {
904 /* D/E-Channel SKB range check */
905 if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
906 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
907 "for fifo(%d) HFCUSB_D_RX\n",
908 hw->name, __func__, fifon);
909 skb_trim(rx_skb, 0);
910 spin_unlock(&hw->lock);
911 return;
913 } else if (fifo->bch) {
914 /* B-Channel SKB range check */
915 if ((rx_skb->len + len) >= (MAX_BCH_SIZE + 3)) {
916 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
917 "for fifo(%d) HFCUSB_B_RX\n",
918 hw->name, __func__, fifon);
919 skb_trim(rx_skb, 0);
920 spin_unlock(&hw->lock);
921 return;
925 memcpy(skb_put(rx_skb, len), data, len);
927 if (hdlc) {
928 /* we have a complete hdlc packet */
929 if (finish) {
930 if ((rx_skb->len > 3) &&
931 (!(rx_skb->data[rx_skb->len - 1]))) {
932 if (debug & DBG_HFC_FIFO_VERBOSE) {
933 printk(KERN_DEBUG "%s: %s: fifon(%i)"
934 " new RX len(%i): ",
935 hw->name, __func__, fifon,
936 rx_skb->len);
937 i = 0;
938 while (i < rx_skb->len)
939 printk("%02x ",
940 rx_skb->data[i++]);
941 printk("\n");
944 /* remove CRC & status */
945 skb_trim(rx_skb, rx_skb->len - 3);
947 if (fifo->dch)
948 recv_Dchannel(fifo->dch);
949 if (fifo->bch)
950 recv_Bchannel(fifo->bch, MISDN_ID_ANY);
951 if (fifo->ech)
952 recv_Echannel(fifo->ech,
953 &hw->dch);
954 } else {
955 if (debug & DBG_HFC_FIFO_VERBOSE) {
956 printk(KERN_DEBUG
957 "%s: CRC or minlen ERROR fifon(%i) "
958 "RX len(%i): ",
959 hw->name, fifon, rx_skb->len);
960 i = 0;
961 while (i < rx_skb->len)
962 printk("%02x ",
963 rx_skb->data[i++]);
964 printk("\n");
966 skb_trim(rx_skb, 0);
969 } else {
970 /* deliver transparent data to layer2 */
971 if (rx_skb->len >= poll)
972 recv_Bchannel(fifo->bch, MISDN_ID_ANY);
974 spin_unlock(&hw->lock);
977 static void
978 fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
979 void *buf, int num_packets, int packet_size, int interval,
980 usb_complete_t complete, void *context)
982 int k;
984 usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
985 complete, context);
987 urb->number_of_packets = num_packets;
988 urb->transfer_flags = URB_ISO_ASAP;
989 urb->actual_length = 0;
990 urb->interval = interval;
992 for (k = 0; k < num_packets; k++) {
993 urb->iso_frame_desc[k].offset = packet_size * k;
994 urb->iso_frame_desc[k].length = packet_size;
995 urb->iso_frame_desc[k].actual_length = 0;
999 /* receive completion routine for all ISO tx fifos */
1000 static void
1001 rx_iso_complete(struct urb *urb)
1003 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1004 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1005 struct hfcsusb *hw = fifo->hw;
1006 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
1007 status, iso_status, i;
1008 __u8 *buf;
1009 static __u8 eof[8];
1010 __u8 s0_state;
1012 fifon = fifo->fifonum;
1013 status = urb->status;
1015 spin_lock(&hw->lock);
1016 if (fifo->stop_gracefull) {
1017 fifo->stop_gracefull = 0;
1018 fifo->active = 0;
1019 spin_unlock(&hw->lock);
1020 return;
1022 spin_unlock(&hw->lock);
1025 * ISO transfer only partially completed,
1026 * look at individual frame status for details
1028 if (status == -EXDEV) {
1029 if (debug & DEBUG_HW)
1030 printk(KERN_DEBUG "%s: %s: with -EXDEV "
1031 "urb->status %d, fifonum %d\n",
1032 hw->name, __func__, status, fifon);
1034 /* clear status, so go on with ISO transfers */
1035 status = 0;
1038 s0_state = 0;
1039 if (fifo->active && !status) {
1040 num_isoc_packets = iso_packets[fifon];
1041 maxlen = fifo->usb_packet_maxlen;
1043 for (k = 0; k < num_isoc_packets; ++k) {
1044 len = urb->iso_frame_desc[k].actual_length;
1045 offset = urb->iso_frame_desc[k].offset;
1046 buf = context_iso_urb->buffer + offset;
1047 iso_status = urb->iso_frame_desc[k].status;
1049 if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
1050 printk(KERN_DEBUG "%s: %s: "
1051 "ISO packet %i, status: %i\n",
1052 hw->name, __func__, k, iso_status);
1055 /* USB data log for every D ISO in */
1056 if ((fifon == HFCUSB_D_RX) &&
1057 (debug & DBG_HFC_USB_VERBOSE)) {
1058 printk(KERN_DEBUG
1059 "%s: %s: %d (%d/%d) len(%d) ",
1060 hw->name, __func__, urb->start_frame,
1061 k, num_isoc_packets-1,
1062 len);
1063 for (i = 0; i < len; i++)
1064 printk("%x ", buf[i]);
1065 printk("\n");
1068 if (!iso_status) {
1069 if (fifo->last_urblen != maxlen) {
1071 * save fifo fill-level threshold bits
1072 * to use them later in TX ISO URB
1073 * completions
1075 hw->threshold_mask = buf[1];
1077 if (fifon == HFCUSB_D_RX)
1078 s0_state = (buf[0] >> 4);
1080 eof[fifon] = buf[0] & 1;
1081 if (len > 2)
1082 hfcsusb_rx_frame(fifo, buf + 2,
1083 len - 2, (len < maxlen)
1084 ? eof[fifon] : 0);
1085 } else
1086 hfcsusb_rx_frame(fifo, buf, len,
1087 (len < maxlen) ?
1088 eof[fifon] : 0);
1089 fifo->last_urblen = len;
1093 /* signal S0 layer1 state change */
1094 if ((s0_state) && (hw->initdone) &&
1095 (s0_state != hw->dch.state)) {
1096 hw->dch.state = s0_state;
1097 schedule_event(&hw->dch, FLG_PHCHANGE);
1100 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1101 context_iso_urb->buffer, num_isoc_packets,
1102 fifo->usb_packet_maxlen, fifo->intervall,
1103 (usb_complete_t)rx_iso_complete, urb->context);
1104 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1105 if (errcode < 0) {
1106 if (debug & DEBUG_HW)
1107 printk(KERN_DEBUG "%s: %s: error submitting "
1108 "ISO URB: %d\n",
1109 hw->name, __func__, errcode);
1111 } else {
1112 if (status && (debug & DBG_HFC_URB_INFO))
1113 printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
1114 "urb->status %d, fifonum %d\n",
1115 hw->name, __func__, status, fifon);
1119 /* receive completion routine for all interrupt rx fifos */
1120 static void
1121 rx_int_complete(struct urb *urb)
1123 int len, status, i;
1124 __u8 *buf, maxlen, fifon;
1125 struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
1126 struct hfcsusb *hw = fifo->hw;
1127 static __u8 eof[8];
1129 spin_lock(&hw->lock);
1130 if (fifo->stop_gracefull) {
1131 fifo->stop_gracefull = 0;
1132 fifo->active = 0;
1133 spin_unlock(&hw->lock);
1134 return;
1136 spin_unlock(&hw->lock);
1138 fifon = fifo->fifonum;
1139 if ((!fifo->active) || (urb->status)) {
1140 if (debug & DBG_HFC_URB_ERROR)
1141 printk(KERN_DEBUG
1142 "%s: %s: RX-Fifo %i is going down (%i)\n",
1143 hw->name, __func__, fifon, urb->status);
1145 fifo->urb->interval = 0; /* cancel automatic rescheduling */
1146 return;
1148 len = urb->actual_length;
1149 buf = fifo->buffer;
1150 maxlen = fifo->usb_packet_maxlen;
1152 /* USB data log for every D INT in */
1153 if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
1154 printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
1155 hw->name, __func__, len);
1156 for (i = 0; i < len; i++)
1157 printk("%02x ", buf[i]);
1158 printk("\n");
1161 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
1162 /* the threshold mask is in the 2nd status byte */
1163 hw->threshold_mask = buf[1];
1165 /* signal S0 layer1 state change */
1166 if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
1167 hw->dch.state = (buf[0] >> 4);
1168 schedule_event(&hw->dch, FLG_PHCHANGE);
1171 eof[fifon] = buf[0] & 1;
1172 /* if we have more than the 2 status bytes -> collect data */
1173 if (len > 2)
1174 hfcsusb_rx_frame(fifo, buf + 2,
1175 urb->actual_length - 2,
1176 (len < maxlen) ? eof[fifon] : 0);
1177 } else {
1178 hfcsusb_rx_frame(fifo, buf, urb->actual_length,
1179 (len < maxlen) ? eof[fifon] : 0);
1181 fifo->last_urblen = urb->actual_length;
1183 status = usb_submit_urb(urb, GFP_ATOMIC);
1184 if (status) {
1185 if (debug & DEBUG_HW)
1186 printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
1187 hw->name, __func__);
1191 /* transmit completion routine for all ISO tx fifos */
1192 static void
1193 tx_iso_complete(struct urb *urb)
1195 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1196 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1197 struct hfcsusb *hw = fifo->hw;
1198 struct sk_buff *tx_skb;
1199 int k, tx_offset, num_isoc_packets, sink, remain, current_len,
1200 errcode, hdlc, i;
1201 int *tx_idx;
1202 int frame_complete, fifon, status;
1203 __u8 threshbit;
1205 spin_lock(&hw->lock);
1206 if (fifo->stop_gracefull) {
1207 fifo->stop_gracefull = 0;
1208 fifo->active = 0;
1209 spin_unlock(&hw->lock);
1210 return;
1213 if (fifo->dch) {
1214 tx_skb = fifo->dch->tx_skb;
1215 tx_idx = &fifo->dch->tx_idx;
1216 hdlc = 1;
1217 } else if (fifo->bch) {
1218 tx_skb = fifo->bch->tx_skb;
1219 tx_idx = &fifo->bch->tx_idx;
1220 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
1221 } else {
1222 printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
1223 hw->name, __func__);
1224 spin_unlock(&hw->lock);
1225 return;
1228 fifon = fifo->fifonum;
1229 status = urb->status;
1231 tx_offset = 0;
1234 * ISO transfer only partially completed,
1235 * look at individual frame status for details
1237 if (status == -EXDEV) {
1238 if (debug & DBG_HFC_URB_ERROR)
1239 printk(KERN_DEBUG "%s: %s: "
1240 "-EXDEV (%i) fifon (%d)\n",
1241 hw->name, __func__, status, fifon);
1243 /* clear status, so go on with ISO transfers */
1244 status = 0;
1247 if (fifo->active && !status) {
1248 /* is FifoFull-threshold set for our channel? */
1249 threshbit = (hw->threshold_mask & (1 << fifon));
1250 num_isoc_packets = iso_packets[fifon];
1252 /* predict dataflow to avoid fifo overflow */
1253 if (fifon >= HFCUSB_D_TX)
1254 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
1255 else
1256 sink = (threshbit) ? SINK_MIN : SINK_MAX;
1257 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1258 context_iso_urb->buffer, num_isoc_packets,
1259 fifo->usb_packet_maxlen, fifo->intervall,
1260 (usb_complete_t)tx_iso_complete, urb->context);
1261 memset(context_iso_urb->buffer, 0,
1262 sizeof(context_iso_urb->buffer));
1263 frame_complete = 0;
1265 for (k = 0; k < num_isoc_packets; ++k) {
1266 /* analyze tx success of previous ISO packets */
1267 if (debug & DBG_HFC_URB_ERROR) {
1268 errcode = urb->iso_frame_desc[k].status;
1269 if (errcode) {
1270 printk(KERN_DEBUG "%s: %s: "
1271 "ISO packet %i, status: %i\n",
1272 hw->name, __func__, k, errcode);
1276 /* Generate next ISO Packets */
1277 if (tx_skb)
1278 remain = tx_skb->len - *tx_idx;
1279 else
1280 remain = 0;
1282 if (remain > 0) {
1283 fifo->bit_line -= sink;
1284 current_len = (0 - fifo->bit_line) / 8;
1285 if (current_len > 14)
1286 current_len = 14;
1287 if (current_len < 0)
1288 current_len = 0;
1289 if (remain < current_len)
1290 current_len = remain;
1292 /* how much bit do we put on the line? */
1293 fifo->bit_line += current_len * 8;
1295 context_iso_urb->buffer[tx_offset] = 0;
1296 if (current_len == remain) {
1297 if (hdlc) {
1298 /* signal frame completion */
1299 context_iso_urb->
1300 buffer[tx_offset] = 1;
1301 /* add 2 byte flags and 16bit
1302 * CRC at end of ISDN frame */
1303 fifo->bit_line += 32;
1305 frame_complete = 1;
1308 /* copy tx data to iso-urb buffer */
1309 memcpy(context_iso_urb->buffer + tx_offset + 1,
1310 (tx_skb->data + *tx_idx), current_len);
1311 *tx_idx += current_len;
1313 urb->iso_frame_desc[k].offset = tx_offset;
1314 urb->iso_frame_desc[k].length = current_len + 1;
1316 /* USB data log for every D ISO out */
1317 if ((fifon == HFCUSB_D_RX) &&
1318 (debug & DBG_HFC_USB_VERBOSE)) {
1319 printk(KERN_DEBUG
1320 "%s: %s (%d/%d) offs(%d) len(%d) ",
1321 hw->name, __func__,
1322 k, num_isoc_packets-1,
1323 urb->iso_frame_desc[k].offset,
1324 urb->iso_frame_desc[k].length);
1326 for (i = urb->iso_frame_desc[k].offset;
1327 i < (urb->iso_frame_desc[k].offset
1328 + urb->iso_frame_desc[k].length);
1329 i++)
1330 printk("%x ",
1331 context_iso_urb->buffer[i]);
1333 printk(" skb->len(%i) tx-idx(%d)\n",
1334 tx_skb->len, *tx_idx);
1337 tx_offset += (current_len + 1);
1338 } else {
1339 urb->iso_frame_desc[k].offset = tx_offset++;
1340 urb->iso_frame_desc[k].length = 1;
1341 /* we lower data margin every msec */
1342 fifo->bit_line -= sink;
1343 if (fifo->bit_line < BITLINE_INF)
1344 fifo->bit_line = BITLINE_INF;
1347 if (frame_complete) {
1348 frame_complete = 0;
1350 if (debug & DBG_HFC_FIFO_VERBOSE) {
1351 printk(KERN_DEBUG "%s: %s: "
1352 "fifon(%i) new TX len(%i): ",
1353 hw->name, __func__,
1354 fifon, tx_skb->len);
1355 i = 0;
1356 while (i < tx_skb->len)
1357 printk("%02x ",
1358 tx_skb->data[i++]);
1359 printk("\n");
1362 dev_kfree_skb(tx_skb);
1363 tx_skb = NULL;
1364 if (fifo->dch && get_next_dframe(fifo->dch))
1365 tx_skb = fifo->dch->tx_skb;
1366 else if (fifo->bch &&
1367 get_next_bframe(fifo->bch)) {
1368 if (test_bit(FLG_TRANSPARENT,
1369 &fifo->bch->Flags))
1370 confirm_Bsend(fifo->bch);
1371 tx_skb = fifo->bch->tx_skb;
1375 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1376 if (errcode < 0) {
1377 if (debug & DEBUG_HW)
1378 printk(KERN_DEBUG
1379 "%s: %s: error submitting ISO URB: %d \n",
1380 hw->name, __func__, errcode);
1384 * abuse DChannel tx iso completion to trigger NT mode state
1385 * changes tx_iso_complete is assumed to be called every
1386 * fifo->intervall (ms)
1388 if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
1389 && (hw->timers & NT_ACTIVATION_TIMER)) {
1390 if ((--hw->nt_timer) < 0)
1391 schedule_event(&hw->dch, FLG_PHCHANGE);
1394 } else {
1395 if (status && (debug & DBG_HFC_URB_ERROR))
1396 printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
1397 "fifonum=%d\n",
1398 hw->name, __func__,
1399 symbolic(urb_errlist, status), status, fifon);
1401 spin_unlock(&hw->lock);
1405 * allocs urbs and start isoc transfer with two pending urbs to avoid
1406 * gaps in the transfer chain
1408 static int
1409 start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
1410 usb_complete_t complete, int packet_size)
1412 struct hfcsusb *hw = fifo->hw;
1413 int i, k, errcode;
1415 if (debug)
1416 printk(KERN_DEBUG "%s: %s: fifo %i\n",
1417 hw->name, __func__, fifo->fifonum);
1419 /* allocate Memory for Iso out Urbs */
1420 for (i = 0; i < 2; i++) {
1421 if (!(fifo->iso[i].urb)) {
1422 fifo->iso[i].urb =
1423 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
1424 if (!(fifo->iso[i].urb)) {
1425 printk(KERN_DEBUG
1426 "%s: %s: alloc urb for fifo %i failed",
1427 hw->name, __func__, fifo->fifonum);
1429 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
1430 fifo->iso[i].indx = i;
1432 /* Init the first iso */
1433 if (ISO_BUFFER_SIZE >=
1434 (fifo->usb_packet_maxlen *
1435 num_packets_per_urb)) {
1436 fill_isoc_urb(fifo->iso[i].urb,
1437 fifo->hw->dev, fifo->pipe,
1438 fifo->iso[i].buffer,
1439 num_packets_per_urb,
1440 fifo->usb_packet_maxlen,
1441 fifo->intervall, complete,
1442 &fifo->iso[i]);
1443 memset(fifo->iso[i].buffer, 0,
1444 sizeof(fifo->iso[i].buffer));
1446 for (k = 0; k < num_packets_per_urb; k++) {
1447 fifo->iso[i].urb->
1448 iso_frame_desc[k].offset =
1449 k * packet_size;
1450 fifo->iso[i].urb->
1451 iso_frame_desc[k].length =
1452 packet_size;
1454 } else {
1455 printk(KERN_DEBUG
1456 "%s: %s: ISO Buffer size to small!\n",
1457 hw->name, __func__);
1460 fifo->bit_line = BITLINE_INF;
1462 errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
1463 fifo->active = (errcode >= 0) ? 1 : 0;
1464 fifo->stop_gracefull = 0;
1465 if (errcode < 0) {
1466 printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
1467 hw->name, __func__,
1468 symbolic(urb_errlist, errcode), i);
1471 return fifo->active;
1474 static void
1475 stop_iso_gracefull(struct usb_fifo *fifo)
1477 struct hfcsusb *hw = fifo->hw;
1478 int i, timeout;
1479 u_long flags;
1481 for (i = 0; i < 2; i++) {
1482 spin_lock_irqsave(&hw->lock, flags);
1483 if (debug)
1484 printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
1485 hw->name, __func__, fifo->fifonum, i);
1486 fifo->stop_gracefull = 1;
1487 spin_unlock_irqrestore(&hw->lock, flags);
1490 for (i = 0; i < 2; i++) {
1491 timeout = 3;
1492 while (fifo->stop_gracefull && timeout--)
1493 schedule_timeout_interruptible((HZ/1000)*16);
1494 if (debug && fifo->stop_gracefull)
1495 printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
1496 hw->name, __func__, fifo->fifonum, i);
1500 static void
1501 stop_int_gracefull(struct usb_fifo *fifo)
1503 struct hfcsusb *hw = fifo->hw;
1504 int timeout;
1505 u_long flags;
1507 spin_lock_irqsave(&hw->lock, flags);
1508 if (debug)
1509 printk(KERN_DEBUG "%s: %s for fifo %i\n",
1510 hw->name, __func__, fifo->fifonum);
1511 fifo->stop_gracefull = 1;
1512 spin_unlock_irqrestore(&hw->lock, flags);
1514 timeout = 3;
1515 while (fifo->stop_gracefull && timeout--)
1516 schedule_timeout_interruptible((HZ/1000)*3);
1517 if (debug && fifo->stop_gracefull)
1518 printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
1519 hw->name, __func__, fifo->fifonum);
1522 /* start the interrupt transfer for the given fifo */
1523 static void
1524 start_int_fifo(struct usb_fifo *fifo)
1526 struct hfcsusb *hw = fifo->hw;
1527 int errcode;
1529 if (debug)
1530 printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
1531 hw->name, __func__, fifo->fifonum);
1533 if (!fifo->urb) {
1534 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
1535 if (!fifo->urb)
1536 return;
1538 usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
1539 fifo->buffer, fifo->usb_packet_maxlen,
1540 (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
1541 fifo->active = 1;
1542 fifo->stop_gracefull = 0;
1543 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
1544 if (errcode) {
1545 printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
1546 hw->name, __func__, errcode);
1547 fifo->active = 0;
1551 static void
1552 setPortMode(struct hfcsusb *hw)
1554 if (debug & DEBUG_HW)
1555 printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
1556 (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
1558 if (hw->protocol == ISDN_P_TE_S0) {
1559 write_reg(hw, HFCUSB_SCTRL, 0x40);
1560 write_reg(hw, HFCUSB_SCTRL_E, 0x00);
1561 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
1562 write_reg(hw, HFCUSB_STATES, 3 | 0x10);
1563 write_reg(hw, HFCUSB_STATES, 3);
1564 } else {
1565 write_reg(hw, HFCUSB_SCTRL, 0x44);
1566 write_reg(hw, HFCUSB_SCTRL_E, 0x09);
1567 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
1568 write_reg(hw, HFCUSB_STATES, 1 | 0x10);
1569 write_reg(hw, HFCUSB_STATES, 1);
1573 static void
1574 reset_hfcsusb(struct hfcsusb *hw)
1576 struct usb_fifo *fifo;
1577 int i;
1579 if (debug & DEBUG_HW)
1580 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1582 /* do Chip reset */
1583 write_reg(hw, HFCUSB_CIRM, 8);
1585 /* aux = output, reset off */
1586 write_reg(hw, HFCUSB_CIRM, 0x10);
1588 /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
1589 write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
1590 ((hw->packet_size / 8) << 4));
1592 /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
1593 write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
1595 /* enable PCM/GCI master mode */
1596 write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
1597 write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
1599 /* init the fifos */
1600 write_reg(hw, HFCUSB_F_THRES,
1601 (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1603 fifo = hw->fifos;
1604 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1605 write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
1606 fifo[i].max_size =
1607 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1608 fifo[i].last_urblen = 0;
1610 /* set 2 bit for D- & E-channel */
1611 write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
1613 /* enable all fifos */
1614 if (i == HFCUSB_D_TX)
1615 write_reg(hw, HFCUSB_CON_HDLC,
1616 (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
1617 else
1618 write_reg(hw, HFCUSB_CON_HDLC, 0x08);
1619 write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1622 write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1623 handle_led(hw, LED_POWER_ON);
1626 /* start USB data pipes dependand on device's endpoint configuration */
1627 static void
1628 hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
1630 /* quick check if endpoint already running */
1631 if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
1632 return;
1633 if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
1634 return;
1635 if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
1636 return;
1637 if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
1638 return;
1640 /* start rx endpoints using USB INT IN method */
1641 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
1642 start_int_fifo(hw->fifos + channel*2 + 1);
1644 /* start rx endpoints using USB ISO IN method */
1645 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
1646 switch (channel) {
1647 case HFC_CHAN_D:
1648 start_isoc_chain(hw->fifos + HFCUSB_D_RX,
1649 ISOC_PACKETS_D,
1650 (usb_complete_t)rx_iso_complete,
1651 16);
1652 break;
1653 case HFC_CHAN_E:
1654 start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
1655 ISOC_PACKETS_D,
1656 (usb_complete_t)rx_iso_complete,
1657 16);
1658 break;
1659 case HFC_CHAN_B1:
1660 start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
1661 ISOC_PACKETS_B,
1662 (usb_complete_t)rx_iso_complete,
1663 16);
1664 break;
1665 case HFC_CHAN_B2:
1666 start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
1667 ISOC_PACKETS_B,
1668 (usb_complete_t)rx_iso_complete,
1669 16);
1670 break;
1674 /* start tx endpoints using USB ISO OUT method */
1675 switch (channel) {
1676 case HFC_CHAN_D:
1677 start_isoc_chain(hw->fifos + HFCUSB_D_TX,
1678 ISOC_PACKETS_B,
1679 (usb_complete_t)tx_iso_complete, 1);
1680 break;
1681 case HFC_CHAN_B1:
1682 start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
1683 ISOC_PACKETS_D,
1684 (usb_complete_t)tx_iso_complete, 1);
1685 break;
1686 case HFC_CHAN_B2:
1687 start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
1688 ISOC_PACKETS_B,
1689 (usb_complete_t)tx_iso_complete, 1);
1690 break;
1694 /* stop USB data pipes dependand on device's endpoint configuration */
1695 static void
1696 hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
1698 /* quick check if endpoint currently running */
1699 if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
1700 return;
1701 if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
1702 return;
1703 if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
1704 return;
1705 if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
1706 return;
1708 /* rx endpoints using USB INT IN method */
1709 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
1710 stop_int_gracefull(hw->fifos + channel*2 + 1);
1712 /* rx endpoints using USB ISO IN method */
1713 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
1714 stop_iso_gracefull(hw->fifos + channel*2 + 1);
1716 /* tx endpoints using USB ISO OUT method */
1717 if (channel != HFC_CHAN_E)
1718 stop_iso_gracefull(hw->fifos + channel*2);
1722 /* Hardware Initialization */
1723 static int
1724 setup_hfcsusb(struct hfcsusb *hw)
1726 int err;
1727 u_char b;
1729 if (debug & DBG_HFC_CALL_TRACE)
1730 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1732 /* check the chip id */
1733 if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
1734 printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
1735 hw->name, __func__);
1736 return 1;
1738 if (b != HFCUSB_CHIPID) {
1739 printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
1740 hw->name, __func__, b);
1741 return 1;
1744 /* first set the needed config, interface and alternate */
1745 err = usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
1747 hw->led_state = 0;
1749 /* init the background machinery for control requests */
1750 hw->ctrl_read.bRequestType = 0xc0;
1751 hw->ctrl_read.bRequest = 1;
1752 hw->ctrl_read.wLength = cpu_to_le16(1);
1753 hw->ctrl_write.bRequestType = 0x40;
1754 hw->ctrl_write.bRequest = 0;
1755 hw->ctrl_write.wLength = 0;
1756 usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
1757 (u_char *)&hw->ctrl_write, NULL, 0,
1758 (usb_complete_t)ctrl_complete, hw);
1760 reset_hfcsusb(hw);
1761 return 0;
1764 static void
1765 release_hw(struct hfcsusb *hw)
1767 if (debug & DBG_HFC_CALL_TRACE)
1768 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1771 * stop all endpoints gracefully
1772 * TODO: mISDN_core should generate CLOSE_CHANNEL
1773 * signals after calling mISDN_unregister_device()
1775 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
1776 hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
1777 hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
1778 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1779 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
1780 if (hw->protocol == ISDN_P_TE_S0)
1781 l1_event(hw->dch.l1, CLOSE_CHANNEL);
1783 mISDN_unregister_device(&hw->dch.dev);
1784 mISDN_freebchannel(&hw->bch[1]);
1785 mISDN_freebchannel(&hw->bch[0]);
1786 mISDN_freedchannel(&hw->dch);
1788 if (hw->ctrl_urb) {
1789 usb_kill_urb(hw->ctrl_urb);
1790 usb_free_urb(hw->ctrl_urb);
1791 hw->ctrl_urb = NULL;
1794 if (hw->intf)
1795 usb_set_intfdata(hw->intf, NULL);
1796 list_del(&hw->list);
1797 kfree(hw);
1798 hw = NULL;
1801 static void
1802 deactivate_bchannel(struct bchannel *bch)
1804 struct hfcsusb *hw = bch->hw;
1805 u_long flags;
1807 if (bch->debug & DEBUG_HW)
1808 printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
1809 hw->name, __func__, bch->nr);
1811 spin_lock_irqsave(&hw->lock, flags);
1812 mISDN_clear_bchannel(bch);
1813 spin_unlock_irqrestore(&hw->lock, flags);
1814 hfcsusb_setup_bch(bch, ISDN_P_NONE);
1815 hfcsusb_stop_endpoint(hw, bch->nr);
1819 * Layer 1 B-channel hardware access
1821 static int
1822 hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1824 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1825 int ret = -EINVAL;
1827 if (bch->debug & DEBUG_HW)
1828 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1830 switch (cmd) {
1831 case HW_TESTRX_RAW:
1832 case HW_TESTRX_HDLC:
1833 case HW_TESTRX_OFF:
1834 ret = -EINVAL;
1835 break;
1837 case CLOSE_CHANNEL:
1838 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1839 if (test_bit(FLG_ACTIVE, &bch->Flags))
1840 deactivate_bchannel(bch);
1841 ch->protocol = ISDN_P_NONE;
1842 ch->peer = NULL;
1843 module_put(THIS_MODULE);
1844 ret = 0;
1845 break;
1846 case CONTROL_CHANNEL:
1847 ret = channel_bctrl(bch, arg);
1848 break;
1849 default:
1850 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1851 __func__, cmd);
1853 return ret;
1856 static int
1857 setup_instance(struct hfcsusb *hw, struct device *parent)
1859 u_long flags;
1860 int err, i;
1862 if (debug & DBG_HFC_CALL_TRACE)
1863 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1865 spin_lock_init(&hw->ctrl_lock);
1866 spin_lock_init(&hw->lock);
1868 mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
1869 hw->dch.debug = debug & 0xFFFF;
1870 hw->dch.hw = hw;
1871 hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1872 hw->dch.dev.D.send = hfcusb_l2l1D;
1873 hw->dch.dev.D.ctrl = hfc_dctrl;
1875 /* enable E-Channel logging */
1876 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1877 mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
1879 hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1880 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1881 hw->dch.dev.nrbchan = 2;
1882 for (i = 0; i < 2; i++) {
1883 hw->bch[i].nr = i + 1;
1884 set_channelmap(i + 1, hw->dch.dev.channelmap);
1885 hw->bch[i].debug = debug;
1886 mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM);
1887 hw->bch[i].hw = hw;
1888 hw->bch[i].ch.send = hfcusb_l2l1B;
1889 hw->bch[i].ch.ctrl = hfc_bctrl;
1890 hw->bch[i].ch.nr = i + 1;
1891 list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
1894 hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
1895 hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
1896 hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
1897 hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
1898 hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
1899 hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
1900 hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
1901 hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
1903 err = setup_hfcsusb(hw);
1904 if (err)
1905 goto out;
1907 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
1908 hfcsusb_cnt + 1);
1909 printk(KERN_INFO "%s: registered as '%s'\n",
1910 DRIVER_NAME, hw->name);
1912 err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
1913 if (err)
1914 goto out;
1916 hfcsusb_cnt++;
1917 write_lock_irqsave(&HFClock, flags);
1918 list_add_tail(&hw->list, &HFClist);
1919 write_unlock_irqrestore(&HFClock, flags);
1920 return 0;
1922 out:
1923 mISDN_freebchannel(&hw->bch[1]);
1924 mISDN_freebchannel(&hw->bch[0]);
1925 mISDN_freedchannel(&hw->dch);
1926 kfree(hw);
1927 return err;
1930 static int
1931 hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1933 struct hfcsusb *hw;
1934 struct usb_device *dev = interface_to_usbdev(intf);
1935 struct usb_host_interface *iface = intf->cur_altsetting;
1936 struct usb_host_interface *iface_used = NULL;
1937 struct usb_host_endpoint *ep;
1938 struct hfcsusb_vdata *driver_info;
1939 int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
1940 probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
1941 ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
1942 alt_used = 0;
1944 vend_idx = 0xffff;
1945 for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
1946 if ((le16_to_cpu(dev->descriptor.idVendor)
1947 == hfcsusb_idtab[i].idVendor) &&
1948 (le16_to_cpu(dev->descriptor.idProduct)
1949 == hfcsusb_idtab[i].idProduct)) {
1950 vend_idx = i;
1951 continue;
1955 printk(KERN_DEBUG
1956 "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
1957 __func__, ifnum, iface->desc.bAlternateSetting,
1958 intf->minor, vend_idx);
1960 if (vend_idx == 0xffff) {
1961 printk(KERN_WARNING
1962 "%s: no valid vendor found in USB descriptor\n",
1963 __func__);
1964 return -EIO;
1966 /* if vendor and product ID is OK, start probing alternate settings */
1967 alt_idx = 0;
1968 small_match = -1;
1970 /* default settings */
1971 iso_packet_size = 16;
1972 packet_size = 64;
1974 while (alt_idx < intf->num_altsetting) {
1975 iface = intf->altsetting + alt_idx;
1976 probe_alt_setting = iface->desc.bAlternateSetting;
1977 cfg_used = 0;
1979 while (validconf[cfg_used][0]) {
1980 cfg_found = 1;
1981 vcf = validconf[cfg_used];
1982 ep = iface->endpoint;
1983 memcpy(cmptbl, vcf, 16 * sizeof(int));
1985 /* check for all endpoints in this alternate setting */
1986 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1987 ep_addr = ep->desc.bEndpointAddress;
1989 /* get endpoint base */
1990 idx = ((ep_addr & 0x7f) - 1) * 2;
1991 if (ep_addr & 0x80)
1992 idx++;
1993 attr = ep->desc.bmAttributes;
1995 if (cmptbl[idx] != EP_NOP) {
1996 if (cmptbl[idx] == EP_NUL)
1997 cfg_found = 0;
1998 if (attr == USB_ENDPOINT_XFER_INT
1999 && cmptbl[idx] == EP_INT)
2000 cmptbl[idx] = EP_NUL;
2001 if (attr == USB_ENDPOINT_XFER_BULK
2002 && cmptbl[idx] == EP_BLK)
2003 cmptbl[idx] = EP_NUL;
2004 if (attr == USB_ENDPOINT_XFER_ISOC
2005 && cmptbl[idx] == EP_ISO)
2006 cmptbl[idx] = EP_NUL;
2008 if (attr == USB_ENDPOINT_XFER_INT &&
2009 ep->desc.bInterval < vcf[17]) {
2010 cfg_found = 0;
2013 ep++;
2016 for (i = 0; i < 16; i++)
2017 if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
2018 cfg_found = 0;
2020 if (cfg_found) {
2021 if (small_match < cfg_used) {
2022 small_match = cfg_used;
2023 alt_used = probe_alt_setting;
2024 iface_used = iface;
2027 cfg_used++;
2029 alt_idx++;
2030 } /* (alt_idx < intf->num_altsetting) */
2032 /* not found a valid USB Ta Endpoint config */
2033 if (small_match == -1)
2034 return -EIO;
2036 iface = iface_used;
2037 hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
2038 if (!hw)
2039 return -ENOMEM; /* got no mem */
2040 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
2042 ep = iface->endpoint;
2043 vcf = validconf[small_match];
2045 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2046 struct usb_fifo *f;
2048 ep_addr = ep->desc.bEndpointAddress;
2049 /* get endpoint base */
2050 idx = ((ep_addr & 0x7f) - 1) * 2;
2051 if (ep_addr & 0x80)
2052 idx++;
2053 f = &hw->fifos[idx & 7];
2055 /* init Endpoints */
2056 if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
2057 ep++;
2058 continue;
2060 switch (ep->desc.bmAttributes) {
2061 case USB_ENDPOINT_XFER_INT:
2062 f->pipe = usb_rcvintpipe(dev,
2063 ep->desc.bEndpointAddress);
2064 f->usb_transfer_mode = USB_INT;
2065 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2066 break;
2067 case USB_ENDPOINT_XFER_BULK:
2068 if (ep_addr & 0x80)
2069 f->pipe = usb_rcvbulkpipe(dev,
2070 ep->desc.bEndpointAddress);
2071 else
2072 f->pipe = usb_sndbulkpipe(dev,
2073 ep->desc.bEndpointAddress);
2074 f->usb_transfer_mode = USB_BULK;
2075 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2076 break;
2077 case USB_ENDPOINT_XFER_ISOC:
2078 if (ep_addr & 0x80)
2079 f->pipe = usb_rcvisocpipe(dev,
2080 ep->desc.bEndpointAddress);
2081 else
2082 f->pipe = usb_sndisocpipe(dev,
2083 ep->desc.bEndpointAddress);
2084 f->usb_transfer_mode = USB_ISOC;
2085 iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2086 break;
2087 default:
2088 f->pipe = 0;
2091 if (f->pipe) {
2092 f->fifonum = idx & 7;
2093 f->hw = hw;
2094 f->usb_packet_maxlen =
2095 le16_to_cpu(ep->desc.wMaxPacketSize);
2096 f->intervall = ep->desc.bInterval;
2098 ep++;
2100 hw->dev = dev; /* save device */
2101 hw->if_used = ifnum; /* save used interface */
2102 hw->alt_used = alt_used; /* and alternate config */
2103 hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
2104 hw->cfg_used = vcf[16]; /* store used config */
2105 hw->vend_idx = vend_idx; /* store found vendor */
2106 hw->packet_size = packet_size;
2107 hw->iso_packet_size = iso_packet_size;
2109 /* create the control pipes needed for register access */
2110 hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
2111 hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
2112 hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
2114 driver_info =
2115 (struct hfcsusb_vdata *)hfcsusb_idtab[vend_idx].driver_info;
2116 printk(KERN_DEBUG "%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
2117 hw->name, __func__, driver_info->vend_name,
2118 conf_str[small_match], ifnum, alt_used);
2120 if (setup_instance(hw, dev->dev.parent))
2121 return -EIO;
2123 hw->intf = intf;
2124 usb_set_intfdata(hw->intf, hw);
2125 return 0;
2128 /* function called when an active device is removed */
2129 static void
2130 hfcsusb_disconnect(struct usb_interface *intf)
2132 struct hfcsusb *hw = usb_get_intfdata(intf);
2133 struct hfcsusb *next;
2134 int cnt = 0;
2136 printk(KERN_INFO "%s: device disconnected\n", hw->name);
2138 handle_led(hw, LED_POWER_OFF);
2139 release_hw(hw);
2141 list_for_each_entry_safe(hw, next, &HFClist, list)
2142 cnt++;
2143 if (!cnt)
2144 hfcsusb_cnt = 0;
2146 usb_set_intfdata(intf, NULL);
2149 static struct usb_driver hfcsusb_drv = {
2150 .name = DRIVER_NAME,
2151 .id_table = hfcsusb_idtab,
2152 .probe = hfcsusb_probe,
2153 .disconnect = hfcsusb_disconnect,
2156 static int __init
2157 hfcsusb_init(void)
2159 printk(KERN_INFO DRIVER_NAME " driver Rev. %s debug(0x%x) poll(%i)\n",
2160 hfcsusb_rev, debug, poll);
2162 if (usb_register(&hfcsusb_drv)) {
2163 printk(KERN_INFO DRIVER_NAME
2164 ": Unable to register hfcsusb module at usb stack\n");
2165 return -ENODEV;
2168 return 0;
2171 static void __exit
2172 hfcsusb_cleanup(void)
2174 if (debug & DBG_HFC_CALL_TRACE)
2175 printk(KERN_INFO DRIVER_NAME ": %s\n", __func__);
2177 /* unregister Hardware */
2178 usb_deregister(&hfcsusb_drv); /* release our driver */
2181 module_init(hfcsusb_init);
2182 module_exit(hfcsusb_cleanup);