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)
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.
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
30 * Revision: 0.3.3 (socket), 2008-11-05
33 #include <linux/module.h>
34 #include <linux/delay.h>
35 #include <linux/usb.h>
36 #include <linux/mISDNhw.h>
37 #include <linux/slab.h>
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 */
67 ctrl_start_transfer(struct hfcsusb *hw)
69 if (debug & DBG_HFC_CALL_TRACE)
70 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
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)
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 spin_unlock(&hw->ctrl_lock);
103 buf = &hw->ctrl_buff[hw->ctrl_in_idx];
106 if (++hw->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
108 if (++hw->ctrl_cnt == 1)
109 ctrl_start_transfer(hw);
110 spin_unlock(&hw->ctrl_lock);
115 /* control completion routine handling background control cmds */
117 ctrl_complete(struct urb *urb)
119 struct hfcsusb *hw = (struct hfcsusb *) urb->context;
121 if (debug & DBG_HFC_CALL_TRACE)
122 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
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 */
136 set_led_bit(struct hfcsusb *hw, signed short led_bits, int set_on)
140 hw->led_state &= ~abs(led_bits);
142 hw->led_state |= led_bits;
145 hw->led_state |= abs(led_bits);
147 hw->led_state &= ~led_bits;
151 /* handle LED requests */
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;
159 if (driver_info->led_scheme == LED_OFF)
161 tmpled = hw->led_state;
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);
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);
177 set_led_bit(hw, driver_info->led_bits[1], 1);
180 set_led_bit(hw, driver_info->led_bits[1], 0);
183 set_led_bit(hw, driver_info->led_bits[2], 1);
186 set_led_bit(hw, driver_info->led_bits[2], 0);
189 set_led_bit(hw, driver_info->led_bits[3], 1);
192 set_led_bit(hw, driver_info->led_bits[3], 0);
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",
200 HFCUSB_P_DATA, hw->led_state);
202 write_reg(hw, HFCUSB_P_DATA, hw->led_state);
207 * Layer2 -> Layer 1 Bchannel data
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;
215 struct mISDNhead *hh = mISDN_HEAD_P(skb);
218 if (debug & DBG_HFC_CALL_TRACE)
219 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
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);
232 case PH_ACTIVATE_REQ:
233 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
234 hfcsusb_start_endpoint(hw, bch->nr - 1);
235 ret = hfcsusb_setup_bch(bch, ch->protocol);
239 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
240 0, NULL, GFP_KERNEL);
242 case PH_DEACTIVATE_REQ:
243 deactivate_bchannel(bch);
244 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY,
245 0, NULL, GFP_KERNEL);
255 * send full D/B channel status information
256 * as MPH_INFORMATION_IND
259 hfcsusb_ph_info(struct hfcsusb *hw)
262 struct dchannel *dch = &hw->dch;
265 phi = kzalloc(struct_size(phi, bch, dch->dev.nrbchan), GFP_ATOMIC);
266 phi->dch.ch.protocol = hw->protocol;
267 phi->dch.ch.Flags = dch->Flags;
268 phi->dch.state = dch->state;
269 phi->dch.num_bch = dch->dev.nrbchan;
270 for (i = 0; i < dch->dev.nrbchan; i++) {
271 phi->bch[i].protocol = hw->bch[i].ch.protocol;
272 phi->bch[i].Flags = hw->bch[i].Flags;
274 _queue_data(&dch->dev.D, MPH_INFORMATION_IND, MISDN_ID_ANY,
275 sizeof(struct ph_info_dch) + dch->dev.nrbchan *
276 sizeof(struct ph_info_ch), phi, GFP_ATOMIC);
281 * Layer2 -> Layer 1 Dchannel data
284 hfcusb_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
286 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
287 struct dchannel *dch = container_of(dev, struct dchannel, dev);
288 struct mISDNhead *hh = mISDN_HEAD_P(skb);
289 struct hfcsusb *hw = dch->hw;
295 if (debug & DBG_HFC_CALL_TRACE)
296 printk(KERN_DEBUG "%s: %s: PH_DATA_REQ\n",
299 spin_lock_irqsave(&hw->lock, flags);
300 ret = dchannel_senddata(dch, skb);
301 spin_unlock_irqrestore(&hw->lock, flags);
304 queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
308 case PH_ACTIVATE_REQ:
309 if (debug & DBG_HFC_CALL_TRACE)
310 printk(KERN_DEBUG "%s: %s: PH_ACTIVATE_REQ %s\n",
312 (hw->protocol == ISDN_P_NT_S0) ? "NT" : "TE");
314 if (hw->protocol == ISDN_P_NT_S0) {
316 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
317 _queue_data(&dch->dev.D,
318 PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
321 hfcsusb_ph_command(hw,
323 test_and_set_bit(FLG_L2_ACTIVATED,
327 hfcsusb_ph_command(hw, HFC_L1_ACTIVATE_TE);
328 ret = l1_event(dch->l1, hh->prim);
332 case PH_DEACTIVATE_REQ:
333 if (debug & DBG_HFC_CALL_TRACE)
334 printk(KERN_DEBUG "%s: %s: PH_DEACTIVATE_REQ\n",
336 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
338 if (hw->protocol == ISDN_P_NT_S0) {
339 hfcsusb_ph_command(hw, HFC_L1_DEACTIVATE_NT);
340 spin_lock_irqsave(&hw->lock, flags);
341 skb_queue_purge(&dch->squeue);
343 dev_kfree_skb(dch->tx_skb);
348 dev_kfree_skb(dch->rx_skb);
351 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
352 spin_unlock_irqrestore(&hw->lock, flags);
354 if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
355 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
359 ret = l1_event(dch->l1, hh->prim);
361 case MPH_INFORMATION_REQ:
371 * Layer 1 callback function
374 hfc_l1callback(struct dchannel *dch, u_int cmd)
376 struct hfcsusb *hw = dch->hw;
378 if (debug & DBG_HFC_CALL_TRACE)
379 printk(KERN_DEBUG "%s: %s cmd 0x%x\n",
380 hw->name, __func__, cmd);
390 skb_queue_purge(&dch->squeue);
392 dev_kfree_skb(dch->tx_skb);
397 dev_kfree_skb(dch->rx_skb);
400 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
402 case PH_ACTIVATE_IND:
403 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
404 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
407 case PH_DEACTIVATE_IND:
408 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
409 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
413 if (dch->debug & DEBUG_HW)
414 printk(KERN_DEBUG "%s: %s: unknown cmd %x\n",
415 hw->name, __func__, cmd);
423 open_dchannel(struct hfcsusb *hw, struct mISDNchannel *ch,
424 struct channel_req *rq)
428 if (debug & DEBUG_HW_OPEN)
429 printk(KERN_DEBUG "%s: %s: dev(%d) open addr(%i) from %p\n",
430 hw->name, __func__, hw->dch.dev.id, rq->adr.channel,
431 __builtin_return_address(0));
432 if (rq->protocol == ISDN_P_NONE)
435 test_and_clear_bit(FLG_ACTIVE, &hw->dch.Flags);
436 test_and_clear_bit(FLG_ACTIVE, &hw->ech.Flags);
437 hfcsusb_start_endpoint(hw, HFC_CHAN_D);
439 /* E-Channel logging */
440 if (rq->adr.channel == 1) {
441 if (hw->fifos[HFCUSB_PCM_RX].pipe) {
442 hfcsusb_start_endpoint(hw, HFC_CHAN_E);
443 set_bit(FLG_ACTIVE, &hw->ech.Flags);
444 _queue_data(&hw->ech.dev.D, PH_ACTIVATE_IND,
445 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
451 hw->protocol = rq->protocol;
452 if (rq->protocol == ISDN_P_TE_S0) {
453 err = create_l1(&hw->dch, hfc_l1callback);
458 ch->protocol = rq->protocol;
461 if (rq->protocol != ch->protocol)
462 return -EPROTONOSUPPORT;
465 if (((ch->protocol == ISDN_P_NT_S0) && (hw->dch.state == 3)) ||
466 ((ch->protocol == ISDN_P_TE_S0) && (hw->dch.state == 7)))
467 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
468 0, NULL, GFP_KERNEL);
470 if (!try_module_get(THIS_MODULE))
471 printk(KERN_WARNING "%s: %s: cannot get module\n",
477 open_bchannel(struct hfcsusb *hw, struct channel_req *rq)
479 struct bchannel *bch;
481 if (rq->adr.channel == 0 || rq->adr.channel > 2)
483 if (rq->protocol == ISDN_P_NONE)
486 if (debug & DBG_HFC_CALL_TRACE)
487 printk(KERN_DEBUG "%s: %s B%i\n",
488 hw->name, __func__, rq->adr.channel);
490 bch = &hw->bch[rq->adr.channel - 1];
491 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
492 return -EBUSY; /* b-channel can be only open once */
493 bch->ch.protocol = rq->protocol;
496 if (!try_module_get(THIS_MODULE))
497 printk(KERN_WARNING "%s: %s:cannot get module\n",
503 channel_ctrl(struct hfcsusb *hw, struct mISDN_ctrl_req *cq)
507 if (debug & DBG_HFC_CALL_TRACE)
508 printk(KERN_DEBUG "%s: %s op(0x%x) channel(0x%x)\n",
509 hw->name, __func__, (cq->op), (cq->channel));
512 case MISDN_CTRL_GETOP:
513 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
514 MISDN_CTRL_DISCONNECT;
517 printk(KERN_WARNING "%s: %s: unknown Op %x\n",
518 hw->name, __func__, cq->op);
526 * device control function
529 hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
531 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
532 struct dchannel *dch = container_of(dev, struct dchannel, dev);
533 struct hfcsusb *hw = dch->hw;
534 struct channel_req *rq;
537 if (dch->debug & DEBUG_HW)
538 printk(KERN_DEBUG "%s: %s: cmd:%x %p\n",
539 hw->name, __func__, cmd, arg);
543 if ((rq->protocol == ISDN_P_TE_S0) ||
544 (rq->protocol == ISDN_P_NT_S0))
545 err = open_dchannel(hw, ch, rq);
547 err = open_bchannel(hw, rq);
553 if (debug & DEBUG_HW_OPEN)
555 "%s: %s: dev(%d) close from %p (open %d)\n",
556 hw->name, __func__, hw->dch.dev.id,
557 __builtin_return_address(0), hw->open);
559 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
560 if (hw->fifos[HFCUSB_PCM_RX].pipe)
561 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
562 handle_led(hw, LED_POWER_ON);
564 module_put(THIS_MODULE);
566 case CONTROL_CHANNEL:
567 err = channel_ctrl(hw, arg);
570 if (dch->debug & DEBUG_HW)
571 printk(KERN_DEBUG "%s: %s: unknown command %x\n",
572 hw->name, __func__, cmd);
579 * S0 TE state change event handler
582 ph_state_te(struct dchannel *dch)
584 struct hfcsusb *hw = dch->hw;
586 if (debug & DEBUG_HW) {
587 if (dch->state <= HFC_MAX_TE_LAYER1_STATE)
588 printk(KERN_DEBUG "%s: %s: %s\n", hw->name, __func__,
589 HFC_TE_LAYER1_STATES[dch->state]);
591 printk(KERN_DEBUG "%s: %s: TE F%d\n",
592 hw->name, __func__, dch->state);
595 switch (dch->state) {
597 l1_event(dch->l1, HW_RESET_IND);
600 l1_event(dch->l1, HW_DEACT_IND);
604 l1_event(dch->l1, ANYSIGNAL);
607 l1_event(dch->l1, INFO2);
610 l1_event(dch->l1, INFO4_P8);
614 handle_led(hw, LED_S0_ON);
616 handle_led(hw, LED_S0_OFF);
620 * S0 NT state change event handler
623 ph_state_nt(struct dchannel *dch)
625 struct hfcsusb *hw = dch->hw;
627 if (debug & DEBUG_HW) {
628 if (dch->state <= HFC_MAX_NT_LAYER1_STATE)
629 printk(KERN_DEBUG "%s: %s: %s\n",
631 HFC_NT_LAYER1_STATES[dch->state]);
634 printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
635 hw->name, __func__, dch->state);
638 switch (dch->state) {
640 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
641 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
643 hw->timers &= ~NT_ACTIVATION_TIMER;
644 handle_led(hw, LED_S0_OFF);
648 if (hw->nt_timer < 0) {
650 hw->timers &= ~NT_ACTIVATION_TIMER;
651 hfcsusb_ph_command(dch->hw, HFC_L1_DEACTIVATE_NT);
653 hw->timers |= NT_ACTIVATION_TIMER;
654 hw->nt_timer = NT_T1_COUNT;
655 /* allow G2 -> G3 transition */
656 write_reg(hw, HFCUSB_STATES, 2 | HFCUSB_NT_G2_G3);
661 hw->timers &= ~NT_ACTIVATION_TIMER;
662 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
663 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
664 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
665 handle_led(hw, LED_S0_ON);
669 hw->timers &= ~NT_ACTIVATION_TIMER;
678 ph_state(struct dchannel *dch)
680 struct hfcsusb *hw = dch->hw;
682 if (hw->protocol == ISDN_P_NT_S0)
684 else if (hw->protocol == ISDN_P_TE_S0)
689 * disable/enable BChannel for desired protocoll
692 hfcsusb_setup_bch(struct bchannel *bch, int protocol)
694 struct hfcsusb *hw = bch->hw;
695 __u8 conhdlc, sctrl, sctrl_r;
697 if (debug & DEBUG_HW)
698 printk(KERN_DEBUG "%s: %s: protocol %x-->%x B%d\n",
699 hw->name, __func__, bch->state, protocol,
702 /* setup val for CON_HDLC */
704 if (protocol > ISDN_P_NONE)
705 conhdlc = 8; /* enable FIFO */
708 case (-1): /* used for init */
712 if (bch->state == ISDN_P_NONE)
713 return 0; /* already in idle state */
714 bch->state = ISDN_P_NONE;
715 clear_bit(FLG_HDLC, &bch->Flags);
716 clear_bit(FLG_TRANSPARENT, &bch->Flags);
720 bch->state = protocol;
721 set_bit(FLG_TRANSPARENT, &bch->Flags);
723 case (ISDN_P_B_HDLC):
724 bch->state = protocol;
725 set_bit(FLG_HDLC, &bch->Flags);
728 if (debug & DEBUG_HW)
729 printk(KERN_DEBUG "%s: %s: prot not known %x\n",
730 hw->name, __func__, protocol);
734 if (protocol >= ISDN_P_NONE) {
735 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 0 : 2);
736 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
737 write_reg(hw, HFCUSB_INC_RES_F, 2);
738 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 1 : 3);
739 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
740 write_reg(hw, HFCUSB_INC_RES_F, 2);
742 sctrl = 0x40 + ((hw->protocol == ISDN_P_TE_S0) ? 0x00 : 0x04);
744 if (test_bit(FLG_ACTIVE, &hw->bch[0].Flags)) {
748 if (test_bit(FLG_ACTIVE, &hw->bch[1].Flags)) {
752 write_reg(hw, HFCUSB_SCTRL, sctrl);
753 write_reg(hw, HFCUSB_SCTRL_R, sctrl_r);
755 if (protocol > ISDN_P_NONE)
756 handle_led(hw, (bch->nr == 1) ? LED_B1_ON : LED_B2_ON);
758 handle_led(hw, (bch->nr == 1) ? LED_B1_OFF :
766 hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
768 if (debug & DEBUG_HW)
769 printk(KERN_DEBUG "%s: %s: %x\n",
770 hw->name, __func__, command);
773 case HFC_L1_ACTIVATE_TE:
774 /* force sending sending INFO1 */
775 write_reg(hw, HFCUSB_STATES, 0x14);
776 /* start l1 activation */
777 write_reg(hw, HFCUSB_STATES, 0x04);
780 case HFC_L1_FORCE_DEACTIVATE_TE:
781 write_reg(hw, HFCUSB_STATES, 0x10);
782 write_reg(hw, HFCUSB_STATES, 0x03);
785 case HFC_L1_ACTIVATE_NT:
786 if (hw->dch.state == 3)
787 _queue_data(&hw->dch.dev.D, PH_ACTIVATE_IND,
788 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
790 write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
791 HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
794 case HFC_L1_DEACTIVATE_NT:
795 write_reg(hw, HFCUSB_STATES,
802 * Layer 1 B-channel hardware access
805 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
807 return mISDN_ctrl_bchannel(bch, cq);
810 /* collect data from incoming interrupt or isochron USB data */
812 hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
815 struct hfcsusb *hw = fifo->hw;
816 struct sk_buff *rx_skb = NULL;
818 int fifon = fifo->fifonum;
823 if (debug & DBG_HFC_CALL_TRACE)
824 printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
825 "dch(%p) bch(%p) ech(%p)\n",
826 hw->name, __func__, fifon, len,
827 fifo->dch, fifo->bch, fifo->ech);
832 if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
833 printk(KERN_DEBUG "%s: %s: undefined channel\n",
838 spin_lock_irqsave(&hw->lock, flags);
840 rx_skb = fifo->dch->rx_skb;
841 maxlen = fifo->dch->maxlen;
845 if (test_bit(FLG_RX_OFF, &fifo->bch->Flags)) {
846 fifo->bch->dropcnt += len;
847 spin_unlock_irqrestore(&hw->lock, flags);
850 maxlen = bchannel_get_rxbuf(fifo->bch, len);
851 rx_skb = fifo->bch->rx_skb;
855 pr_warning("%s.B%d: No bufferspace for %d bytes\n",
856 hw->name, fifo->bch->nr, len);
857 spin_unlock_irqrestore(&hw->lock, flags);
860 maxlen = fifo->bch->maxlen;
861 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
864 rx_skb = fifo->ech->rx_skb;
865 maxlen = fifo->ech->maxlen;
869 if (fifo->dch || fifo->ech) {
871 rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
874 fifo->dch->rx_skb = rx_skb;
876 fifo->ech->rx_skb = rx_skb;
879 printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
881 spin_unlock_irqrestore(&hw->lock, flags);
885 /* D/E-Channel SKB range check */
886 if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
887 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
888 "for fifo(%d) HFCUSB_D_RX\n",
889 hw->name, __func__, fifon);
891 spin_unlock_irqrestore(&hw->lock, flags);
896 skb_put_data(rx_skb, data, len);
899 /* we have a complete hdlc packet */
901 if ((rx_skb->len > 3) &&
902 (!(rx_skb->data[rx_skb->len - 1]))) {
903 if (debug & DBG_HFC_FIFO_VERBOSE) {
904 printk(KERN_DEBUG "%s: %s: fifon(%i)"
906 hw->name, __func__, fifon,
909 while (i < rx_skb->len)
915 /* remove CRC & status */
916 skb_trim(rx_skb, rx_skb->len - 3);
919 recv_Dchannel(fifo->dch);
921 recv_Bchannel(fifo->bch, MISDN_ID_ANY,
924 recv_Echannel(fifo->ech,
927 if (debug & DBG_HFC_FIFO_VERBOSE) {
929 "%s: CRC or minlen ERROR fifon(%i) "
931 hw->name, fifon, rx_skb->len);
933 while (i < rx_skb->len)
942 /* deliver transparent data to layer2 */
943 recv_Bchannel(fifo->bch, MISDN_ID_ANY, false);
945 spin_unlock_irqrestore(&hw->lock, flags);
949 fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
950 void *buf, int num_packets, int packet_size, int interval,
951 usb_complete_t complete, void *context)
955 usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
958 urb->number_of_packets = num_packets;
959 urb->transfer_flags = URB_ISO_ASAP;
960 urb->actual_length = 0;
961 urb->interval = interval;
963 for (k = 0; k < num_packets; k++) {
964 urb->iso_frame_desc[k].offset = packet_size * k;
965 urb->iso_frame_desc[k].length = packet_size;
966 urb->iso_frame_desc[k].actual_length = 0;
970 /* receive completion routine for all ISO tx fifos */
972 rx_iso_complete(struct urb *urb)
974 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
975 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
976 struct hfcsusb *hw = fifo->hw;
977 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
978 status, iso_status, i;
984 fifon = fifo->fifonum;
985 status = urb->status;
987 spin_lock_irqsave(&hw->lock, flags);
988 if (fifo->stop_gracefull) {
989 fifo->stop_gracefull = 0;
991 spin_unlock_irqrestore(&hw->lock, flags);
994 spin_unlock_irqrestore(&hw->lock, flags);
997 * ISO transfer only partially completed,
998 * look at individual frame status for details
1000 if (status == -EXDEV) {
1001 if (debug & DEBUG_HW)
1002 printk(KERN_DEBUG "%s: %s: with -EXDEV "
1003 "urb->status %d, fifonum %d\n",
1004 hw->name, __func__, status, fifon);
1006 /* clear status, so go on with ISO transfers */
1011 if (fifo->active && !status) {
1012 num_isoc_packets = iso_packets[fifon];
1013 maxlen = fifo->usb_packet_maxlen;
1015 for (k = 0; k < num_isoc_packets; ++k) {
1016 len = urb->iso_frame_desc[k].actual_length;
1017 offset = urb->iso_frame_desc[k].offset;
1018 buf = context_iso_urb->buffer + offset;
1019 iso_status = urb->iso_frame_desc[k].status;
1021 if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
1022 printk(KERN_DEBUG "%s: %s: "
1023 "ISO packet %i, status: %i\n",
1024 hw->name, __func__, k, iso_status);
1027 /* USB data log for every D ISO in */
1028 if ((fifon == HFCUSB_D_RX) &&
1029 (debug & DBG_HFC_USB_VERBOSE)) {
1031 "%s: %s: %d (%d/%d) len(%d) ",
1032 hw->name, __func__, urb->start_frame,
1033 k, num_isoc_packets - 1,
1035 for (i = 0; i < len; i++)
1036 printk("%x ", buf[i]);
1041 if (fifo->last_urblen != maxlen) {
1043 * save fifo fill-level threshold bits
1044 * to use them later in TX ISO URB
1047 hw->threshold_mask = buf[1];
1049 if (fifon == HFCUSB_D_RX)
1050 s0_state = (buf[0] >> 4);
1052 eof[fifon] = buf[0] & 1;
1054 hfcsusb_rx_frame(fifo, buf + 2,
1055 len - 2, (len < maxlen)
1058 hfcsusb_rx_frame(fifo, buf, len,
1061 fifo->last_urblen = len;
1065 /* signal S0 layer1 state change */
1066 if ((s0_state) && (hw->initdone) &&
1067 (s0_state != hw->dch.state)) {
1068 hw->dch.state = s0_state;
1069 schedule_event(&hw->dch, FLG_PHCHANGE);
1072 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1073 context_iso_urb->buffer, num_isoc_packets,
1074 fifo->usb_packet_maxlen, fifo->intervall,
1075 (usb_complete_t)rx_iso_complete, urb->context);
1076 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1078 if (debug & DEBUG_HW)
1079 printk(KERN_DEBUG "%s: %s: error submitting "
1081 hw->name, __func__, errcode);
1084 if (status && (debug & DBG_HFC_URB_INFO))
1085 printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
1086 "urb->status %d, fifonum %d\n",
1087 hw->name, __func__, status, fifon);
1091 /* receive completion routine for all interrupt rx fifos */
1093 rx_int_complete(struct urb *urb)
1096 __u8 *buf, maxlen, fifon;
1097 struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
1098 struct hfcsusb *hw = fifo->hw;
1100 unsigned long flags;
1102 spin_lock_irqsave(&hw->lock, flags);
1103 if (fifo->stop_gracefull) {
1104 fifo->stop_gracefull = 0;
1106 spin_unlock_irqrestore(&hw->lock, flags);
1109 spin_unlock_irqrestore(&hw->lock, flags);
1111 fifon = fifo->fifonum;
1112 if ((!fifo->active) || (urb->status)) {
1113 if (debug & DBG_HFC_URB_ERROR)
1115 "%s: %s: RX-Fifo %i is going down (%i)\n",
1116 hw->name, __func__, fifon, urb->status);
1118 fifo->urb->interval = 0; /* cancel automatic rescheduling */
1121 len = urb->actual_length;
1123 maxlen = fifo->usb_packet_maxlen;
1125 /* USB data log for every D INT in */
1126 if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
1127 printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
1128 hw->name, __func__, len);
1129 for (i = 0; i < len; i++)
1130 printk("%02x ", buf[i]);
1134 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
1135 /* the threshold mask is in the 2nd status byte */
1136 hw->threshold_mask = buf[1];
1138 /* signal S0 layer1 state change */
1139 if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
1140 hw->dch.state = (buf[0] >> 4);
1141 schedule_event(&hw->dch, FLG_PHCHANGE);
1144 eof[fifon] = buf[0] & 1;
1145 /* if we have more than the 2 status bytes -> collect data */
1147 hfcsusb_rx_frame(fifo, buf + 2,
1148 urb->actual_length - 2,
1149 (len < maxlen) ? eof[fifon] : 0);
1151 hfcsusb_rx_frame(fifo, buf, urb->actual_length,
1152 (len < maxlen) ? eof[fifon] : 0);
1154 fifo->last_urblen = urb->actual_length;
1156 status = usb_submit_urb(urb, GFP_ATOMIC);
1158 if (debug & DEBUG_HW)
1159 printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
1160 hw->name, __func__);
1164 /* transmit completion routine for all ISO tx fifos */
1166 tx_iso_complete(struct urb *urb)
1168 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1169 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1170 struct hfcsusb *hw = fifo->hw;
1171 struct sk_buff *tx_skb;
1172 int k, tx_offset, num_isoc_packets, sink, remain, current_len,
1175 int frame_complete, fifon, status, fillempty = 0;
1177 unsigned long flags;
1179 spin_lock_irqsave(&hw->lock, flags);
1180 if (fifo->stop_gracefull) {
1181 fifo->stop_gracefull = 0;
1183 spin_unlock_irqrestore(&hw->lock, flags);
1188 tx_skb = fifo->dch->tx_skb;
1189 tx_idx = &fifo->dch->tx_idx;
1191 } else if (fifo->bch) {
1192 tx_skb = fifo->bch->tx_skb;
1193 tx_idx = &fifo->bch->tx_idx;
1194 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
1195 if (!tx_skb && !hdlc &&
1196 test_bit(FLG_FILLEMPTY, &fifo->bch->Flags))
1199 printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
1200 hw->name, __func__);
1201 spin_unlock_irqrestore(&hw->lock, flags);
1205 fifon = fifo->fifonum;
1206 status = urb->status;
1211 * ISO transfer only partially completed,
1212 * look at individual frame status for details
1214 if (status == -EXDEV) {
1215 if (debug & DBG_HFC_URB_ERROR)
1216 printk(KERN_DEBUG "%s: %s: "
1217 "-EXDEV (%i) fifon (%d)\n",
1218 hw->name, __func__, status, fifon);
1220 /* clear status, so go on with ISO transfers */
1224 if (fifo->active && !status) {
1225 /* is FifoFull-threshold set for our channel? */
1226 threshbit = (hw->threshold_mask & (1 << fifon));
1227 num_isoc_packets = iso_packets[fifon];
1229 /* predict dataflow to avoid fifo overflow */
1230 if (fifon >= HFCUSB_D_TX)
1231 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
1233 sink = (threshbit) ? SINK_MIN : SINK_MAX;
1234 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1235 context_iso_urb->buffer, num_isoc_packets,
1236 fifo->usb_packet_maxlen, fifo->intervall,
1237 (usb_complete_t)tx_iso_complete, urb->context);
1238 memset(context_iso_urb->buffer, 0,
1239 sizeof(context_iso_urb->buffer));
1242 for (k = 0; k < num_isoc_packets; ++k) {
1243 /* analyze tx success of previous ISO packets */
1244 if (debug & DBG_HFC_URB_ERROR) {
1245 errcode = urb->iso_frame_desc[k].status;
1247 printk(KERN_DEBUG "%s: %s: "
1248 "ISO packet %i, status: %i\n",
1249 hw->name, __func__, k, errcode);
1253 /* Generate next ISO Packets */
1255 remain = tx_skb->len - *tx_idx;
1257 remain = 15; /* > not complete */
1262 fifo->bit_line -= sink;
1263 current_len = (0 - fifo->bit_line) / 8;
1264 if (current_len > 14)
1266 if (current_len < 0)
1268 if (remain < current_len)
1269 current_len = remain;
1271 /* how much bit do we put on the line? */
1272 fifo->bit_line += current_len * 8;
1274 context_iso_urb->buffer[tx_offset] = 0;
1275 if (current_len == remain) {
1277 /* signal frame completion */
1279 buffer[tx_offset] = 1;
1280 /* add 2 byte flags and 16bit
1281 * CRC at end of ISDN frame */
1282 fifo->bit_line += 32;
1287 /* copy tx data to iso-urb buffer */
1288 p = context_iso_urb->buffer + tx_offset + 1;
1290 memset(p, fifo->bch->fill[0],
1293 memcpy(p, (tx_skb->data + *tx_idx),
1295 *tx_idx += current_len;
1297 urb->iso_frame_desc[k].offset = tx_offset;
1298 urb->iso_frame_desc[k].length = current_len + 1;
1300 /* USB data log for every D ISO out */
1301 if ((fifon == HFCUSB_D_RX) && !fillempty &&
1302 (debug & DBG_HFC_USB_VERBOSE)) {
1304 "%s: %s (%d/%d) offs(%d) len(%d) ",
1306 k, num_isoc_packets - 1,
1307 urb->iso_frame_desc[k].offset,
1308 urb->iso_frame_desc[k].length);
1310 for (i = urb->iso_frame_desc[k].offset;
1311 i < (urb->iso_frame_desc[k].offset
1312 + urb->iso_frame_desc[k].length);
1315 context_iso_urb->buffer[i]);
1317 printk(" skb->len(%i) tx-idx(%d)\n",
1318 tx_skb->len, *tx_idx);
1321 tx_offset += (current_len + 1);
1323 urb->iso_frame_desc[k].offset = tx_offset++;
1324 urb->iso_frame_desc[k].length = 1;
1325 /* we lower data margin every msec */
1326 fifo->bit_line -= sink;
1327 if (fifo->bit_line < BITLINE_INF)
1328 fifo->bit_line = BITLINE_INF;
1331 if (frame_complete) {
1334 if (debug & DBG_HFC_FIFO_VERBOSE) {
1335 printk(KERN_DEBUG "%s: %s: "
1336 "fifon(%i) new TX len(%i): ",
1338 fifon, tx_skb->len);
1340 while (i < tx_skb->len)
1346 dev_kfree_skb(tx_skb);
1348 if (fifo->dch && get_next_dframe(fifo->dch))
1349 tx_skb = fifo->dch->tx_skb;
1350 else if (fifo->bch &&
1351 get_next_bframe(fifo->bch))
1352 tx_skb = fifo->bch->tx_skb;
1355 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1357 if (debug & DEBUG_HW)
1359 "%s: %s: error submitting ISO URB: %d \n",
1360 hw->name, __func__, errcode);
1364 * abuse DChannel tx iso completion to trigger NT mode state
1365 * changes tx_iso_complete is assumed to be called every
1366 * fifo->intervall (ms)
1368 if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
1369 && (hw->timers & NT_ACTIVATION_TIMER)) {
1370 if ((--hw->nt_timer) < 0)
1371 schedule_event(&hw->dch, FLG_PHCHANGE);
1375 if (status && (debug & DBG_HFC_URB_ERROR))
1376 printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
1379 symbolic(urb_errlist, status), status, fifon);
1381 spin_unlock_irqrestore(&hw->lock, flags);
1385 * allocs urbs and start isoc transfer with two pending urbs to avoid
1386 * gaps in the transfer chain
1389 start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
1390 usb_complete_t complete, int packet_size)
1392 struct hfcsusb *hw = fifo->hw;
1396 printk(KERN_DEBUG "%s: %s: fifo %i\n",
1397 hw->name, __func__, fifo->fifonum);
1399 /* allocate Memory for Iso out Urbs */
1400 for (i = 0; i < 2; i++) {
1401 if (!(fifo->iso[i].urb)) {
1403 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
1404 if (!(fifo->iso[i].urb)) {
1406 "%s: %s: alloc urb for fifo %i failed",
1407 hw->name, __func__, fifo->fifonum);
1409 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
1410 fifo->iso[i].indx = i;
1412 /* Init the first iso */
1413 if (ISO_BUFFER_SIZE >=
1414 (fifo->usb_packet_maxlen *
1415 num_packets_per_urb)) {
1416 fill_isoc_urb(fifo->iso[i].urb,
1417 fifo->hw->dev, fifo->pipe,
1418 fifo->iso[i].buffer,
1419 num_packets_per_urb,
1420 fifo->usb_packet_maxlen,
1421 fifo->intervall, complete,
1423 memset(fifo->iso[i].buffer, 0,
1424 sizeof(fifo->iso[i].buffer));
1426 for (k = 0; k < num_packets_per_urb; k++) {
1428 iso_frame_desc[k].offset =
1431 iso_frame_desc[k].length =
1436 "%s: %s: ISO Buffer size to small!\n",
1437 hw->name, __func__);
1440 fifo->bit_line = BITLINE_INF;
1442 errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
1443 fifo->active = (errcode >= 0) ? 1 : 0;
1444 fifo->stop_gracefull = 0;
1446 printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
1448 symbolic(urb_errlist, errcode), i);
1451 return fifo->active;
1455 stop_iso_gracefull(struct usb_fifo *fifo)
1457 struct hfcsusb *hw = fifo->hw;
1461 for (i = 0; i < 2; i++) {
1462 spin_lock_irqsave(&hw->lock, flags);
1464 printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
1465 hw->name, __func__, fifo->fifonum, i);
1466 fifo->stop_gracefull = 1;
1467 spin_unlock_irqrestore(&hw->lock, flags);
1470 for (i = 0; i < 2; i++) {
1472 while (fifo->stop_gracefull && timeout--)
1473 schedule_timeout_interruptible((HZ / 1000) * 16);
1474 if (debug && fifo->stop_gracefull)
1475 printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
1476 hw->name, __func__, fifo->fifonum, i);
1481 stop_int_gracefull(struct usb_fifo *fifo)
1483 struct hfcsusb *hw = fifo->hw;
1487 spin_lock_irqsave(&hw->lock, flags);
1489 printk(KERN_DEBUG "%s: %s for fifo %i\n",
1490 hw->name, __func__, fifo->fifonum);
1491 fifo->stop_gracefull = 1;
1492 spin_unlock_irqrestore(&hw->lock, flags);
1495 while (fifo->stop_gracefull && timeout--)
1496 schedule_timeout_interruptible((HZ / 1000) * 3);
1497 if (debug && fifo->stop_gracefull)
1498 printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
1499 hw->name, __func__, fifo->fifonum);
1502 /* start the interrupt transfer for the given fifo */
1504 start_int_fifo(struct usb_fifo *fifo)
1506 struct hfcsusb *hw = fifo->hw;
1510 printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
1511 hw->name, __func__, fifo->fifonum);
1514 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
1518 usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
1519 fifo->buffer, fifo->usb_packet_maxlen,
1520 (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
1522 fifo->stop_gracefull = 0;
1523 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
1525 printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
1526 hw->name, __func__, errcode);
1532 setPortMode(struct hfcsusb *hw)
1534 if (debug & DEBUG_HW)
1535 printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
1536 (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
1538 if (hw->protocol == ISDN_P_TE_S0) {
1539 write_reg(hw, HFCUSB_SCTRL, 0x40);
1540 write_reg(hw, HFCUSB_SCTRL_E, 0x00);
1541 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
1542 write_reg(hw, HFCUSB_STATES, 3 | 0x10);
1543 write_reg(hw, HFCUSB_STATES, 3);
1545 write_reg(hw, HFCUSB_SCTRL, 0x44);
1546 write_reg(hw, HFCUSB_SCTRL_E, 0x09);
1547 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
1548 write_reg(hw, HFCUSB_STATES, 1 | 0x10);
1549 write_reg(hw, HFCUSB_STATES, 1);
1554 reset_hfcsusb(struct hfcsusb *hw)
1556 struct usb_fifo *fifo;
1559 if (debug & DEBUG_HW)
1560 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1563 write_reg(hw, HFCUSB_CIRM, 8);
1565 /* aux = output, reset off */
1566 write_reg(hw, HFCUSB_CIRM, 0x10);
1568 /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
1569 write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
1570 ((hw->packet_size / 8) << 4));
1572 /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
1573 write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
1575 /* enable PCM/GCI master mode */
1576 write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
1577 write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
1579 /* init the fifos */
1580 write_reg(hw, HFCUSB_F_THRES,
1581 (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1584 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1585 write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
1587 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1588 fifo[i].last_urblen = 0;
1590 /* set 2 bit for D- & E-channel */
1591 write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
1593 /* enable all fifos */
1594 if (i == HFCUSB_D_TX)
1595 write_reg(hw, HFCUSB_CON_HDLC,
1596 (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
1598 write_reg(hw, HFCUSB_CON_HDLC, 0x08);
1599 write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1602 write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1603 handle_led(hw, LED_POWER_ON);
1606 /* start USB data pipes dependand on device's endpoint configuration */
1608 hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
1610 /* quick check if endpoint already running */
1611 if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
1613 if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
1615 if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
1617 if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
1620 /* start rx endpoints using USB INT IN method */
1621 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
1622 start_int_fifo(hw->fifos + channel * 2 + 1);
1624 /* start rx endpoints using USB ISO IN method */
1625 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
1628 start_isoc_chain(hw->fifos + HFCUSB_D_RX,
1630 (usb_complete_t)rx_iso_complete,
1634 start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
1636 (usb_complete_t)rx_iso_complete,
1640 start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
1642 (usb_complete_t)rx_iso_complete,
1646 start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
1648 (usb_complete_t)rx_iso_complete,
1654 /* start tx endpoints using USB ISO OUT method */
1657 start_isoc_chain(hw->fifos + HFCUSB_D_TX,
1659 (usb_complete_t)tx_iso_complete, 1);
1662 start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
1664 (usb_complete_t)tx_iso_complete, 1);
1667 start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
1669 (usb_complete_t)tx_iso_complete, 1);
1674 /* stop USB data pipes dependand on device's endpoint configuration */
1676 hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
1678 /* quick check if endpoint currently running */
1679 if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
1681 if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
1683 if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
1685 if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
1688 /* rx endpoints using USB INT IN method */
1689 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
1690 stop_int_gracefull(hw->fifos + channel * 2 + 1);
1692 /* rx endpoints using USB ISO IN method */
1693 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
1694 stop_iso_gracefull(hw->fifos + channel * 2 + 1);
1696 /* tx endpoints using USB ISO OUT method */
1697 if (channel != HFC_CHAN_E)
1698 stop_iso_gracefull(hw->fifos + channel * 2);
1702 /* Hardware Initialization */
1704 setup_hfcsusb(struct hfcsusb *hw)
1708 if (debug & DBG_HFC_CALL_TRACE)
1709 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1711 /* check the chip id */
1712 if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
1713 printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
1714 hw->name, __func__);
1717 if (b != HFCUSB_CHIPID) {
1718 printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
1719 hw->name, __func__, b);
1723 /* first set the needed config, interface and alternate */
1724 (void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
1728 /* init the background machinery for control requests */
1729 hw->ctrl_read.bRequestType = 0xc0;
1730 hw->ctrl_read.bRequest = 1;
1731 hw->ctrl_read.wLength = cpu_to_le16(1);
1732 hw->ctrl_write.bRequestType = 0x40;
1733 hw->ctrl_write.bRequest = 0;
1734 hw->ctrl_write.wLength = 0;
1735 usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
1736 (u_char *)&hw->ctrl_write, NULL, 0,
1737 (usb_complete_t)ctrl_complete, hw);
1744 release_hw(struct hfcsusb *hw)
1746 if (debug & DBG_HFC_CALL_TRACE)
1747 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1750 * stop all endpoints gracefully
1751 * TODO: mISDN_core should generate CLOSE_CHANNEL
1752 * signals after calling mISDN_unregister_device()
1754 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
1755 hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
1756 hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
1757 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1758 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
1759 if (hw->protocol == ISDN_P_TE_S0)
1760 l1_event(hw->dch.l1, CLOSE_CHANNEL);
1762 mISDN_unregister_device(&hw->dch.dev);
1763 mISDN_freebchannel(&hw->bch[1]);
1764 mISDN_freebchannel(&hw->bch[0]);
1765 mISDN_freedchannel(&hw->dch);
1768 usb_kill_urb(hw->ctrl_urb);
1769 usb_free_urb(hw->ctrl_urb);
1770 hw->ctrl_urb = NULL;
1774 usb_set_intfdata(hw->intf, NULL);
1775 list_del(&hw->list);
1781 deactivate_bchannel(struct bchannel *bch)
1783 struct hfcsusb *hw = bch->hw;
1786 if (bch->debug & DEBUG_HW)
1787 printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
1788 hw->name, __func__, bch->nr);
1790 spin_lock_irqsave(&hw->lock, flags);
1791 mISDN_clear_bchannel(bch);
1792 spin_unlock_irqrestore(&hw->lock, flags);
1793 hfcsusb_setup_bch(bch, ISDN_P_NONE);
1794 hfcsusb_stop_endpoint(hw, bch->nr - 1);
1798 * Layer 1 B-channel hardware access
1801 hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1803 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1806 if (bch->debug & DEBUG_HW)
1807 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1811 case HW_TESTRX_HDLC:
1817 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1818 deactivate_bchannel(bch);
1819 ch->protocol = ISDN_P_NONE;
1821 module_put(THIS_MODULE);
1824 case CONTROL_CHANNEL:
1825 ret = channel_bctrl(bch, arg);
1828 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1835 setup_instance(struct hfcsusb *hw, struct device *parent)
1840 if (debug & DBG_HFC_CALL_TRACE)
1841 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1843 spin_lock_init(&hw->ctrl_lock);
1844 spin_lock_init(&hw->lock);
1846 mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
1847 hw->dch.debug = debug & 0xFFFF;
1849 hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1850 hw->dch.dev.D.send = hfcusb_l2l1D;
1851 hw->dch.dev.D.ctrl = hfc_dctrl;
1853 /* enable E-Channel logging */
1854 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1855 mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
1857 hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1858 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1859 hw->dch.dev.nrbchan = 2;
1860 for (i = 0; i < 2; i++) {
1861 hw->bch[i].nr = i + 1;
1862 set_channelmap(i + 1, hw->dch.dev.channelmap);
1863 hw->bch[i].debug = debug;
1864 mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM, poll >> 1);
1866 hw->bch[i].ch.send = hfcusb_l2l1B;
1867 hw->bch[i].ch.ctrl = hfc_bctrl;
1868 hw->bch[i].ch.nr = i + 1;
1869 list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
1872 hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
1873 hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
1874 hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
1875 hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
1876 hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
1877 hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
1878 hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
1879 hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
1881 err = setup_hfcsusb(hw);
1885 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
1887 printk(KERN_INFO "%s: registered as '%s'\n",
1888 DRIVER_NAME, hw->name);
1890 err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
1895 write_lock_irqsave(&HFClock, flags);
1896 list_add_tail(&hw->list, &HFClist);
1897 write_unlock_irqrestore(&HFClock, flags);
1901 mISDN_freebchannel(&hw->bch[1]);
1902 mISDN_freebchannel(&hw->bch[0]);
1903 mISDN_freedchannel(&hw->dch);
1909 hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1912 struct usb_device *dev = interface_to_usbdev(intf);
1913 struct usb_host_interface *iface = intf->cur_altsetting;
1914 struct usb_host_interface *iface_used = NULL;
1915 struct usb_host_endpoint *ep;
1916 struct hfcsusb_vdata *driver_info;
1917 int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
1918 probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
1919 ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
1923 for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
1924 if ((le16_to_cpu(dev->descriptor.idVendor)
1925 == hfcsusb_idtab[i].idVendor) &&
1926 (le16_to_cpu(dev->descriptor.idProduct)
1927 == hfcsusb_idtab[i].idProduct)) {
1934 "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
1935 __func__, ifnum, iface->desc.bAlternateSetting,
1936 intf->minor, vend_idx);
1938 if (vend_idx == 0xffff) {
1940 "%s: no valid vendor found in USB descriptor\n",
1944 /* if vendor and product ID is OK, start probing alternate settings */
1948 /* default settings */
1949 iso_packet_size = 16;
1952 while (alt_idx < intf->num_altsetting) {
1953 iface = intf->altsetting + alt_idx;
1954 probe_alt_setting = iface->desc.bAlternateSetting;
1957 while (validconf[cfg_used][0]) {
1959 vcf = validconf[cfg_used];
1960 ep = iface->endpoint;
1961 memcpy(cmptbl, vcf, 16 * sizeof(int));
1963 /* check for all endpoints in this alternate setting */
1964 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1965 ep_addr = ep->desc.bEndpointAddress;
1967 /* get endpoint base */
1968 idx = ((ep_addr & 0x7f) - 1) * 2;
1971 attr = ep->desc.bmAttributes;
1973 if (cmptbl[idx] != EP_NOP) {
1974 if (cmptbl[idx] == EP_NUL)
1976 if (attr == USB_ENDPOINT_XFER_INT
1977 && cmptbl[idx] == EP_INT)
1978 cmptbl[idx] = EP_NUL;
1979 if (attr == USB_ENDPOINT_XFER_BULK
1980 && cmptbl[idx] == EP_BLK)
1981 cmptbl[idx] = EP_NUL;
1982 if (attr == USB_ENDPOINT_XFER_ISOC
1983 && cmptbl[idx] == EP_ISO)
1984 cmptbl[idx] = EP_NUL;
1986 if (attr == USB_ENDPOINT_XFER_INT &&
1987 ep->desc.bInterval < vcf[17]) {
1994 for (i = 0; i < 16; i++)
1995 if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
1999 if (small_match < cfg_used) {
2000 small_match = cfg_used;
2001 alt_used = probe_alt_setting;
2008 } /* (alt_idx < intf->num_altsetting) */
2010 /* not found a valid USB Ta Endpoint config */
2011 if (small_match == -1)
2015 hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
2017 return -ENOMEM; /* got no mem */
2018 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
2020 ep = iface->endpoint;
2021 vcf = validconf[small_match];
2023 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2026 ep_addr = ep->desc.bEndpointAddress;
2027 /* get endpoint base */
2028 idx = ((ep_addr & 0x7f) - 1) * 2;
2031 f = &hw->fifos[idx & 7];
2033 /* init Endpoints */
2034 if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
2038 switch (ep->desc.bmAttributes) {
2039 case USB_ENDPOINT_XFER_INT:
2040 f->pipe = usb_rcvintpipe(dev,
2041 ep->desc.bEndpointAddress);
2042 f->usb_transfer_mode = USB_INT;
2043 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2045 case USB_ENDPOINT_XFER_BULK:
2047 f->pipe = usb_rcvbulkpipe(dev,
2048 ep->desc.bEndpointAddress);
2050 f->pipe = usb_sndbulkpipe(dev,
2051 ep->desc.bEndpointAddress);
2052 f->usb_transfer_mode = USB_BULK;
2053 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2055 case USB_ENDPOINT_XFER_ISOC:
2057 f->pipe = usb_rcvisocpipe(dev,
2058 ep->desc.bEndpointAddress);
2060 f->pipe = usb_sndisocpipe(dev,
2061 ep->desc.bEndpointAddress);
2062 f->usb_transfer_mode = USB_ISOC;
2063 iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2070 f->fifonum = idx & 7;
2072 f->usb_packet_maxlen =
2073 le16_to_cpu(ep->desc.wMaxPacketSize);
2074 f->intervall = ep->desc.bInterval;
2078 hw->dev = dev; /* save device */
2079 hw->if_used = ifnum; /* save used interface */
2080 hw->alt_used = alt_used; /* and alternate config */
2081 hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
2082 hw->cfg_used = vcf[16]; /* store used config */
2083 hw->vend_idx = vend_idx; /* store found vendor */
2084 hw->packet_size = packet_size;
2085 hw->iso_packet_size = iso_packet_size;
2087 /* create the control pipes needed for register access */
2088 hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
2089 hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
2091 driver_info = (struct hfcsusb_vdata *)
2092 hfcsusb_idtab[vend_idx].driver_info;
2094 hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
2095 if (!hw->ctrl_urb) {
2096 pr_warn("%s: No memory for control urb\n",
2097 driver_info->vend_name);
2102 pr_info("%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
2103 hw->name, __func__, driver_info->vend_name,
2104 conf_str[small_match], ifnum, alt_used);
2106 if (setup_instance(hw, dev->dev.parent))
2110 usb_set_intfdata(hw->intf, hw);
2114 /* function called when an active device is removed */
2116 hfcsusb_disconnect(struct usb_interface *intf)
2118 struct hfcsusb *hw = usb_get_intfdata(intf);
2119 struct hfcsusb *next;
2122 printk(KERN_INFO "%s: device disconnected\n", hw->name);
2124 handle_led(hw, LED_POWER_OFF);
2127 list_for_each_entry_safe(hw, next, &HFClist, list)
2132 usb_set_intfdata(intf, NULL);
2135 static struct usb_driver hfcsusb_drv = {
2136 .name = DRIVER_NAME,
2137 .id_table = hfcsusb_idtab,
2138 .probe = hfcsusb_probe,
2139 .disconnect = hfcsusb_disconnect,
2140 .disable_hub_initiated_lpm = 1,
2143 module_usb_driver(hfcsusb_drv);