procfs: do not confuse jiffies with cputime64_t
[zen-stable.git] / drivers / isdn / hardware / mISDN / hfcsusb.c
blob71a8eb6ef71ead7efca9b247f205aef4cca86817
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 <linux/slab.h>
37 #include "hfcsusb.h"
39 static const char *hfcsusb_rev = "Revision: 0.3.3 (socket), 2008-11-05";
41 static unsigned int debug;
42 static int poll = DEFAULT_TRANSP_BURST_SZ;
44 static LIST_HEAD(HFClist);
45 static DEFINE_RWLOCK(HFClock);
48 MODULE_AUTHOR("Martin Bachem");
49 MODULE_LICENSE("GPL");
50 module_param(debug, uint, S_IRUGO | S_IWUSR);
51 module_param(poll, int, 0);
53 static int hfcsusb_cnt;
55 /* some function prototypes */
56 static void hfcsusb_ph_command(struct hfcsusb *hw, u_char command);
57 static void release_hw(struct hfcsusb *hw);
58 static void reset_hfcsusb(struct hfcsusb *hw);
59 static void setPortMode(struct hfcsusb *hw);
60 static void hfcsusb_start_endpoint(struct hfcsusb *hw, int channel);
61 static void hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel);
62 static int hfcsusb_setup_bch(struct bchannel *bch, int protocol);
63 static void deactivate_bchannel(struct bchannel *bch);
64 static void hfcsusb_ph_info(struct hfcsusb *hw);
66 /* start next background transfer for control channel */
67 static void
68 ctrl_start_transfer(struct hfcsusb *hw)
70 if (debug & DBG_HFC_CALL_TRACE)
71 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
73 if (hw->ctrl_cnt) {
74 hw->ctrl_urb->pipe = hw->ctrl_out_pipe;
75 hw->ctrl_urb->setup_packet = (u_char *)&hw->ctrl_write;
76 hw->ctrl_urb->transfer_buffer = NULL;
77 hw->ctrl_urb->transfer_buffer_length = 0;
78 hw->ctrl_write.wIndex =
79 cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].hfcs_reg);
80 hw->ctrl_write.wValue =
81 cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].reg_val);
83 usb_submit_urb(hw->ctrl_urb, GFP_ATOMIC);
88 * queue a control transfer request to write HFC-S USB
89 * chip register using CTRL resuest queue
91 static int write_reg(struct hfcsusb *hw, __u8 reg, __u8 val)
93 struct ctrl_buf *buf;
95 if (debug & DBG_HFC_CALL_TRACE)
96 printk(KERN_DEBUG "%s: %s reg(0x%02x) val(0x%02x)\n",
97 hw->name, __func__, reg, val);
99 spin_lock(&hw->ctrl_lock);
100 if (hw->ctrl_cnt >= HFC_CTRL_BUFSIZE) {
101 spin_unlock(&hw->ctrl_lock);
102 return 1;
104 buf = &hw->ctrl_buff[hw->ctrl_in_idx];
105 buf->hfcs_reg = reg;
106 buf->reg_val = val;
107 if (++hw->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
108 hw->ctrl_in_idx = 0;
109 if (++hw->ctrl_cnt == 1)
110 ctrl_start_transfer(hw);
111 spin_unlock(&hw->ctrl_lock);
113 return 0;
116 /* control completion routine handling background control cmds */
117 static void
118 ctrl_complete(struct urb *urb)
120 struct hfcsusb *hw = (struct hfcsusb *) urb->context;
122 if (debug & DBG_HFC_CALL_TRACE)
123 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
125 urb->dev = hw->dev;
126 if (hw->ctrl_cnt) {
127 hw->ctrl_cnt--; /* decrement actual count */
128 if (++hw->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
129 hw->ctrl_out_idx = 0; /* pointer wrap */
131 ctrl_start_transfer(hw); /* start next transfer */
135 /* handle LED bits */
136 static void
137 set_led_bit(struct hfcsusb *hw, signed short led_bits, int set_on)
139 if (set_on) {
140 if (led_bits < 0)
141 hw->led_state &= ~abs(led_bits);
142 else
143 hw->led_state |= led_bits;
144 } else {
145 if (led_bits < 0)
146 hw->led_state |= abs(led_bits);
147 else
148 hw->led_state &= ~led_bits;
152 /* handle LED requests */
153 static void
154 handle_led(struct hfcsusb *hw, int event)
156 struct hfcsusb_vdata *driver_info = (struct hfcsusb_vdata *)
157 hfcsusb_idtab[hw->vend_idx].driver_info;
158 __u8 tmpled;
160 if (driver_info->led_scheme == LED_OFF)
161 return;
162 tmpled = hw->led_state;
164 switch (event) {
165 case LED_POWER_ON:
166 set_led_bit(hw, driver_info->led_bits[0], 1);
167 set_led_bit(hw, driver_info->led_bits[1], 0);
168 set_led_bit(hw, driver_info->led_bits[2], 0);
169 set_led_bit(hw, driver_info->led_bits[3], 0);
170 break;
171 case LED_POWER_OFF:
172 set_led_bit(hw, driver_info->led_bits[0], 0);
173 set_led_bit(hw, driver_info->led_bits[1], 0);
174 set_led_bit(hw, driver_info->led_bits[2], 0);
175 set_led_bit(hw, driver_info->led_bits[3], 0);
176 break;
177 case LED_S0_ON:
178 set_led_bit(hw, driver_info->led_bits[1], 1);
179 break;
180 case LED_S0_OFF:
181 set_led_bit(hw, driver_info->led_bits[1], 0);
182 break;
183 case LED_B1_ON:
184 set_led_bit(hw, driver_info->led_bits[2], 1);
185 break;
186 case LED_B1_OFF:
187 set_led_bit(hw, driver_info->led_bits[2], 0);
188 break;
189 case LED_B2_ON:
190 set_led_bit(hw, driver_info->led_bits[3], 1);
191 break;
192 case LED_B2_OFF:
193 set_led_bit(hw, driver_info->led_bits[3], 0);
194 break;
197 if (hw->led_state != tmpled) {
198 if (debug & DBG_HFC_CALL_TRACE)
199 printk(KERN_DEBUG "%s: %s reg(0x%02x) val(x%02x)\n",
200 hw->name, __func__,
201 HFCUSB_P_DATA, hw->led_state);
203 write_reg(hw, HFCUSB_P_DATA, hw->led_state);
208 * Layer2 -> Layer 1 Bchannel data
210 static int
211 hfcusb_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
213 struct bchannel *bch = container_of(ch, struct bchannel, ch);
214 struct hfcsusb *hw = bch->hw;
215 int ret = -EINVAL;
216 struct mISDNhead *hh = mISDN_HEAD_P(skb);
217 u_long flags;
219 if (debug & DBG_HFC_CALL_TRACE)
220 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
222 switch (hh->prim) {
223 case PH_DATA_REQ:
224 spin_lock_irqsave(&hw->lock, flags);
225 ret = bchannel_senddata(bch, skb);
226 spin_unlock_irqrestore(&hw->lock, flags);
227 if (debug & DBG_HFC_CALL_TRACE)
228 printk(KERN_DEBUG "%s: %s PH_DATA_REQ ret(%i)\n",
229 hw->name, __func__, ret);
230 if (ret > 0) {
232 * other l1 drivers don't send early confirms on
233 * transp data, but hfcsusb does because tx_next
234 * skb is needed in tx_iso_complete()
236 queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
237 ret = 0;
239 return ret;
240 case PH_ACTIVATE_REQ:
241 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
242 hfcsusb_start_endpoint(hw, bch->nr);
243 ret = hfcsusb_setup_bch(bch, ch->protocol);
244 } else
245 ret = 0;
246 if (!ret)
247 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
248 0, NULL, GFP_KERNEL);
249 break;
250 case PH_DEACTIVATE_REQ:
251 deactivate_bchannel(bch);
252 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY,
253 0, NULL, GFP_KERNEL);
254 ret = 0;
255 break;
257 if (!ret)
258 dev_kfree_skb(skb);
259 return ret;
263 * send full D/B channel status information
264 * as MPH_INFORMATION_IND
266 static void
267 hfcsusb_ph_info(struct hfcsusb *hw)
269 struct ph_info *phi;
270 struct dchannel *dch = &hw->dch;
271 int i;
273 phi = kzalloc(sizeof(struct ph_info) +
274 dch->dev.nrbchan * sizeof(struct ph_info_ch), GFP_ATOMIC);
275 phi->dch.ch.protocol = hw->protocol;
276 phi->dch.ch.Flags = dch->Flags;
277 phi->dch.state = dch->state;
278 phi->dch.num_bch = dch->dev.nrbchan;
279 for (i = 0; i < dch->dev.nrbchan; i++) {
280 phi->bch[i].protocol = hw->bch[i].ch.protocol;
281 phi->bch[i].Flags = hw->bch[i].Flags;
283 _queue_data(&dch->dev.D, MPH_INFORMATION_IND, MISDN_ID_ANY,
284 sizeof(struct ph_info_dch) + dch->dev.nrbchan *
285 sizeof(struct ph_info_ch), phi, GFP_ATOMIC);
286 kfree(phi);
290 * Layer2 -> Layer 1 Dchannel data
292 static int
293 hfcusb_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
295 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
296 struct dchannel *dch = container_of(dev, struct dchannel, dev);
297 struct mISDNhead *hh = mISDN_HEAD_P(skb);
298 struct hfcsusb *hw = dch->hw;
299 int ret = -EINVAL;
300 u_long flags;
302 switch (hh->prim) {
303 case PH_DATA_REQ:
304 if (debug & DBG_HFC_CALL_TRACE)
305 printk(KERN_DEBUG "%s: %s: PH_DATA_REQ\n",
306 hw->name, __func__);
308 spin_lock_irqsave(&hw->lock, flags);
309 ret = dchannel_senddata(dch, skb);
310 spin_unlock_irqrestore(&hw->lock, flags);
311 if (ret > 0) {
312 ret = 0;
313 queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
315 break;
317 case PH_ACTIVATE_REQ:
318 if (debug & DBG_HFC_CALL_TRACE)
319 printk(KERN_DEBUG "%s: %s: PH_ACTIVATE_REQ %s\n",
320 hw->name, __func__,
321 (hw->protocol == ISDN_P_NT_S0) ? "NT" : "TE");
323 if (hw->protocol == ISDN_P_NT_S0) {
324 ret = 0;
325 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
326 _queue_data(&dch->dev.D,
327 PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
328 NULL, GFP_ATOMIC);
329 } else {
330 hfcsusb_ph_command(hw,
331 HFC_L1_ACTIVATE_NT);
332 test_and_set_bit(FLG_L2_ACTIVATED,
333 &dch->Flags);
335 } else {
336 hfcsusb_ph_command(hw, HFC_L1_ACTIVATE_TE);
337 ret = l1_event(dch->l1, hh->prim);
339 break;
341 case PH_DEACTIVATE_REQ:
342 if (debug & DBG_HFC_CALL_TRACE)
343 printk(KERN_DEBUG "%s: %s: PH_DEACTIVATE_REQ\n",
344 hw->name, __func__);
345 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
347 if (hw->protocol == ISDN_P_NT_S0) {
348 hfcsusb_ph_command(hw, HFC_L1_DEACTIVATE_NT);
349 spin_lock_irqsave(&hw->lock, flags);
350 skb_queue_purge(&dch->squeue);
351 if (dch->tx_skb) {
352 dev_kfree_skb(dch->tx_skb);
353 dch->tx_skb = NULL;
355 dch->tx_idx = 0;
356 if (dch->rx_skb) {
357 dev_kfree_skb(dch->rx_skb);
358 dch->rx_skb = NULL;
360 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
361 spin_unlock_irqrestore(&hw->lock, flags);
362 #ifdef FIXME
363 if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
364 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
365 #endif
366 ret = 0;
367 } else
368 ret = l1_event(dch->l1, hh->prim);
369 break;
370 case MPH_INFORMATION_REQ:
371 hfcsusb_ph_info(hw);
372 ret = 0;
373 break;
376 return ret;
380 * Layer 1 callback function
382 static int
383 hfc_l1callback(struct dchannel *dch, u_int cmd)
385 struct hfcsusb *hw = dch->hw;
387 if (debug & DBG_HFC_CALL_TRACE)
388 printk(KERN_DEBUG "%s: %s cmd 0x%x\n",
389 hw->name, __func__, cmd);
391 switch (cmd) {
392 case INFO3_P8:
393 case INFO3_P10:
394 case HW_RESET_REQ:
395 case HW_POWERUP_REQ:
396 break;
398 case HW_DEACT_REQ:
399 skb_queue_purge(&dch->squeue);
400 if (dch->tx_skb) {
401 dev_kfree_skb(dch->tx_skb);
402 dch->tx_skb = NULL;
404 dch->tx_idx = 0;
405 if (dch->rx_skb) {
406 dev_kfree_skb(dch->rx_skb);
407 dch->rx_skb = NULL;
409 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
410 break;
411 case PH_ACTIVATE_IND:
412 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
413 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
414 GFP_ATOMIC);
415 break;
416 case PH_DEACTIVATE_IND:
417 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
418 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
419 GFP_ATOMIC);
420 break;
421 default:
422 if (dch->debug & DEBUG_HW)
423 printk(KERN_DEBUG "%s: %s: unknown cmd %x\n",
424 hw->name, __func__, cmd);
425 return -1;
427 hfcsusb_ph_info(hw);
428 return 0;
431 static int
432 open_dchannel(struct hfcsusb *hw, struct mISDNchannel *ch,
433 struct channel_req *rq)
435 int err = 0;
437 if (debug & DEBUG_HW_OPEN)
438 printk(KERN_DEBUG "%s: %s: dev(%d) open addr(%i) from %p\n",
439 hw->name, __func__, hw->dch.dev.id, rq->adr.channel,
440 __builtin_return_address(0));
441 if (rq->protocol == ISDN_P_NONE)
442 return -EINVAL;
444 test_and_clear_bit(FLG_ACTIVE, &hw->dch.Flags);
445 test_and_clear_bit(FLG_ACTIVE, &hw->ech.Flags);
446 hfcsusb_start_endpoint(hw, HFC_CHAN_D);
448 /* E-Channel logging */
449 if (rq->adr.channel == 1) {
450 if (hw->fifos[HFCUSB_PCM_RX].pipe) {
451 hfcsusb_start_endpoint(hw, HFC_CHAN_E);
452 set_bit(FLG_ACTIVE, &hw->ech.Flags);
453 _queue_data(&hw->ech.dev.D, PH_ACTIVATE_IND,
454 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
455 } else
456 return -EINVAL;
459 if (!hw->initdone) {
460 hw->protocol = rq->protocol;
461 if (rq->protocol == ISDN_P_TE_S0) {
462 err = create_l1(&hw->dch, hfc_l1callback);
463 if (err)
464 return err;
466 setPortMode(hw);
467 ch->protocol = rq->protocol;
468 hw->initdone = 1;
469 } else {
470 if (rq->protocol != ch->protocol)
471 return -EPROTONOSUPPORT;
474 if (((ch->protocol == ISDN_P_NT_S0) && (hw->dch.state == 3)) ||
475 ((ch->protocol == ISDN_P_TE_S0) && (hw->dch.state == 7)))
476 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
477 0, NULL, GFP_KERNEL);
478 rq->ch = ch;
479 if (!try_module_get(THIS_MODULE))
480 printk(KERN_WARNING "%s: %s: cannot get module\n",
481 hw->name, __func__);
482 return 0;
485 static int
486 open_bchannel(struct hfcsusb *hw, struct channel_req *rq)
488 struct bchannel *bch;
490 if (rq->adr.channel > 2)
491 return -EINVAL;
492 if (rq->protocol == ISDN_P_NONE)
493 return -EINVAL;
495 if (debug & DBG_HFC_CALL_TRACE)
496 printk(KERN_DEBUG "%s: %s B%i\n",
497 hw->name, __func__, rq->adr.channel);
499 bch = &hw->bch[rq->adr.channel - 1];
500 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
501 return -EBUSY; /* b-channel can be only open once */
502 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
503 bch->ch.protocol = rq->protocol;
504 rq->ch = &bch->ch;
506 /* start USB endpoint for bchannel */
507 if (rq->adr.channel == 1)
508 hfcsusb_start_endpoint(hw, HFC_CHAN_B1);
509 else
510 hfcsusb_start_endpoint(hw, HFC_CHAN_B2);
512 if (!try_module_get(THIS_MODULE))
513 printk(KERN_WARNING "%s: %s:cannot get module\n",
514 hw->name, __func__);
515 return 0;
518 static int
519 channel_ctrl(struct hfcsusb *hw, struct mISDN_ctrl_req *cq)
521 int ret = 0;
523 if (debug & DBG_HFC_CALL_TRACE)
524 printk(KERN_DEBUG "%s: %s op(0x%x) channel(0x%x)\n",
525 hw->name, __func__, (cq->op), (cq->channel));
527 switch (cq->op) {
528 case MISDN_CTRL_GETOP:
529 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
530 MISDN_CTRL_DISCONNECT;
531 break;
532 default:
533 printk(KERN_WARNING "%s: %s: unknown Op %x\n",
534 hw->name, __func__, cq->op);
535 ret = -EINVAL;
536 break;
538 return ret;
542 * device control function
544 static int
545 hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
547 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
548 struct dchannel *dch = container_of(dev, struct dchannel, dev);
549 struct hfcsusb *hw = dch->hw;
550 struct channel_req *rq;
551 int err = 0;
553 if (dch->debug & DEBUG_HW)
554 printk(KERN_DEBUG "%s: %s: cmd:%x %p\n",
555 hw->name, __func__, cmd, arg);
556 switch (cmd) {
557 case OPEN_CHANNEL:
558 rq = arg;
559 if ((rq->protocol == ISDN_P_TE_S0) ||
560 (rq->protocol == ISDN_P_NT_S0))
561 err = open_dchannel(hw, ch, rq);
562 else
563 err = open_bchannel(hw, rq);
564 if (!err)
565 hw->open++;
566 break;
567 case CLOSE_CHANNEL:
568 hw->open--;
569 if (debug & DEBUG_HW_OPEN)
570 printk(KERN_DEBUG
571 "%s: %s: dev(%d) close from %p (open %d)\n",
572 hw->name, __func__, hw->dch.dev.id,
573 __builtin_return_address(0), hw->open);
574 if (!hw->open) {
575 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
576 if (hw->fifos[HFCUSB_PCM_RX].pipe)
577 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
578 handle_led(hw, LED_POWER_ON);
580 module_put(THIS_MODULE);
581 break;
582 case CONTROL_CHANNEL:
583 err = channel_ctrl(hw, arg);
584 break;
585 default:
586 if (dch->debug & DEBUG_HW)
587 printk(KERN_DEBUG "%s: %s: unknown command %x\n",
588 hw->name, __func__, cmd);
589 return -EINVAL;
591 return err;
595 * S0 TE state change event handler
597 static void
598 ph_state_te(struct dchannel *dch)
600 struct hfcsusb *hw = dch->hw;
602 if (debug & DEBUG_HW) {
603 if (dch->state <= HFC_MAX_TE_LAYER1_STATE)
604 printk(KERN_DEBUG "%s: %s: %s\n", hw->name, __func__,
605 HFC_TE_LAYER1_STATES[dch->state]);
606 else
607 printk(KERN_DEBUG "%s: %s: TE F%d\n",
608 hw->name, __func__, dch->state);
611 switch (dch->state) {
612 case 0:
613 l1_event(dch->l1, HW_RESET_IND);
614 break;
615 case 3:
616 l1_event(dch->l1, HW_DEACT_IND);
617 break;
618 case 5:
619 case 8:
620 l1_event(dch->l1, ANYSIGNAL);
621 break;
622 case 6:
623 l1_event(dch->l1, INFO2);
624 break;
625 case 7:
626 l1_event(dch->l1, INFO4_P8);
627 break;
629 if (dch->state == 7)
630 handle_led(hw, LED_S0_ON);
631 else
632 handle_led(hw, LED_S0_OFF);
636 * S0 NT state change event handler
638 static void
639 ph_state_nt(struct dchannel *dch)
641 struct hfcsusb *hw = dch->hw;
643 if (debug & DEBUG_HW) {
644 if (dch->state <= HFC_MAX_NT_LAYER1_STATE)
645 printk(KERN_DEBUG "%s: %s: %s\n",
646 hw->name, __func__,
647 HFC_NT_LAYER1_STATES[dch->state]);
649 else
650 printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
651 hw->name, __func__, dch->state);
654 switch (dch->state) {
655 case (1):
656 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
657 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
658 hw->nt_timer = 0;
659 hw->timers &= ~NT_ACTIVATION_TIMER;
660 handle_led(hw, LED_S0_OFF);
661 break;
663 case (2):
664 if (hw->nt_timer < 0) {
665 hw->nt_timer = 0;
666 hw->timers &= ~NT_ACTIVATION_TIMER;
667 hfcsusb_ph_command(dch->hw, HFC_L1_DEACTIVATE_NT);
668 } else {
669 hw->timers |= NT_ACTIVATION_TIMER;
670 hw->nt_timer = NT_T1_COUNT;
671 /* allow G2 -> G3 transition */
672 write_reg(hw, HFCUSB_STATES, 2 | HFCUSB_NT_G2_G3);
674 break;
675 case (3):
676 hw->nt_timer = 0;
677 hw->timers &= ~NT_ACTIVATION_TIMER;
678 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
679 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
680 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
681 handle_led(hw, LED_S0_ON);
682 break;
683 case (4):
684 hw->nt_timer = 0;
685 hw->timers &= ~NT_ACTIVATION_TIMER;
686 break;
687 default:
688 break;
690 hfcsusb_ph_info(hw);
693 static void
694 ph_state(struct dchannel *dch)
696 struct hfcsusb *hw = dch->hw;
698 if (hw->protocol == ISDN_P_NT_S0)
699 ph_state_nt(dch);
700 else if (hw->protocol == ISDN_P_TE_S0)
701 ph_state_te(dch);
705 * disable/enable BChannel for desired protocoll
707 static int
708 hfcsusb_setup_bch(struct bchannel *bch, int protocol)
710 struct hfcsusb *hw = bch->hw;
711 __u8 conhdlc, sctrl, sctrl_r;
713 if (debug & DEBUG_HW)
714 printk(KERN_DEBUG "%s: %s: protocol %x-->%x B%d\n",
715 hw->name, __func__, bch->state, protocol,
716 bch->nr);
718 /* setup val for CON_HDLC */
719 conhdlc = 0;
720 if (protocol > ISDN_P_NONE)
721 conhdlc = 8; /* enable FIFO */
723 switch (protocol) {
724 case (-1): /* used for init */
725 bch->state = -1;
726 /* fall through */
727 case (ISDN_P_NONE):
728 if (bch->state == ISDN_P_NONE)
729 return 0; /* already in idle state */
730 bch->state = ISDN_P_NONE;
731 clear_bit(FLG_HDLC, &bch->Flags);
732 clear_bit(FLG_TRANSPARENT, &bch->Flags);
733 break;
734 case (ISDN_P_B_RAW):
735 conhdlc |= 2;
736 bch->state = protocol;
737 set_bit(FLG_TRANSPARENT, &bch->Flags);
738 break;
739 case (ISDN_P_B_HDLC):
740 bch->state = protocol;
741 set_bit(FLG_HDLC, &bch->Flags);
742 break;
743 default:
744 if (debug & DEBUG_HW)
745 printk(KERN_DEBUG "%s: %s: prot not known %x\n",
746 hw->name, __func__, protocol);
747 return -ENOPROTOOPT;
750 if (protocol >= ISDN_P_NONE) {
751 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 0 : 2);
752 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
753 write_reg(hw, HFCUSB_INC_RES_F, 2);
754 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 1 : 3);
755 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
756 write_reg(hw, HFCUSB_INC_RES_F, 2);
758 sctrl = 0x40 + ((hw->protocol == ISDN_P_TE_S0) ? 0x00 : 0x04);
759 sctrl_r = 0x0;
760 if (test_bit(FLG_ACTIVE, &hw->bch[0].Flags)) {
761 sctrl |= 1;
762 sctrl_r |= 1;
764 if (test_bit(FLG_ACTIVE, &hw->bch[1].Flags)) {
765 sctrl |= 2;
766 sctrl_r |= 2;
768 write_reg(hw, HFCUSB_SCTRL, sctrl);
769 write_reg(hw, HFCUSB_SCTRL_R, sctrl_r);
771 if (protocol > ISDN_P_NONE)
772 handle_led(hw, (bch->nr == 1) ? LED_B1_ON : LED_B2_ON);
773 else
774 handle_led(hw, (bch->nr == 1) ? LED_B1_OFF :
775 LED_B2_OFF);
777 hfcsusb_ph_info(hw);
778 return 0;
781 static void
782 hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
784 if (debug & DEBUG_HW)
785 printk(KERN_DEBUG "%s: %s: %x\n",
786 hw->name, __func__, command);
788 switch (command) {
789 case HFC_L1_ACTIVATE_TE:
790 /* force sending sending INFO1 */
791 write_reg(hw, HFCUSB_STATES, 0x14);
792 /* start l1 activation */
793 write_reg(hw, HFCUSB_STATES, 0x04);
794 break;
796 case HFC_L1_FORCE_DEACTIVATE_TE:
797 write_reg(hw, HFCUSB_STATES, 0x10);
798 write_reg(hw, HFCUSB_STATES, 0x03);
799 break;
801 case HFC_L1_ACTIVATE_NT:
802 if (hw->dch.state == 3)
803 _queue_data(&hw->dch.dev.D, PH_ACTIVATE_IND,
804 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
805 else
806 write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
807 HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
808 break;
810 case HFC_L1_DEACTIVATE_NT:
811 write_reg(hw, HFCUSB_STATES,
812 HFCUSB_DO_ACTION);
813 break;
818 * Layer 1 B-channel hardware access
820 static int
821 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
823 int ret = 0;
825 switch (cq->op) {
826 case MISDN_CTRL_GETOP:
827 cq->op = MISDN_CTRL_FILL_EMPTY;
828 break;
829 case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
830 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
831 if (debug & DEBUG_HW_OPEN)
832 printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
833 "off=%d)\n", __func__, bch->nr, !!cq->p1);
834 break;
835 default:
836 printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
837 ret = -EINVAL;
838 break;
840 return ret;
843 /* collect data from incoming interrupt or isochron USB data */
844 static void
845 hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
846 int finish)
848 struct hfcsusb *hw = fifo->hw;
849 struct sk_buff *rx_skb = NULL;
850 int maxlen = 0;
851 int fifon = fifo->fifonum;
852 int i;
853 int hdlc = 0;
855 if (debug & DBG_HFC_CALL_TRACE)
856 printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
857 "dch(%p) bch(%p) ech(%p)\n",
858 hw->name, __func__, fifon, len,
859 fifo->dch, fifo->bch, fifo->ech);
861 if (!len)
862 return;
864 if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
865 printk(KERN_DEBUG "%s: %s: undefined channel\n",
866 hw->name, __func__);
867 return;
870 spin_lock(&hw->lock);
871 if (fifo->dch) {
872 rx_skb = fifo->dch->rx_skb;
873 maxlen = fifo->dch->maxlen;
874 hdlc = 1;
876 if (fifo->bch) {
877 rx_skb = fifo->bch->rx_skb;
878 maxlen = fifo->bch->maxlen;
879 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
881 if (fifo->ech) {
882 rx_skb = fifo->ech->rx_skb;
883 maxlen = fifo->ech->maxlen;
884 hdlc = 1;
887 if (!rx_skb) {
888 rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
889 if (rx_skb) {
890 if (fifo->dch)
891 fifo->dch->rx_skb = rx_skb;
892 if (fifo->bch)
893 fifo->bch->rx_skb = rx_skb;
894 if (fifo->ech)
895 fifo->ech->rx_skb = rx_skb;
896 skb_trim(rx_skb, 0);
897 } else {
898 printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
899 hw->name, __func__);
900 spin_unlock(&hw->lock);
901 return;
905 if (fifo->dch || fifo->ech) {
906 /* D/E-Channel SKB range check */
907 if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
908 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
909 "for fifo(%d) HFCUSB_D_RX\n",
910 hw->name, __func__, fifon);
911 skb_trim(rx_skb, 0);
912 spin_unlock(&hw->lock);
913 return;
915 } else if (fifo->bch) {
916 /* B-Channel SKB range check */
917 if ((rx_skb->len + len) >= (MAX_BCH_SIZE + 3)) {
918 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
919 "for fifo(%d) HFCUSB_B_RX\n",
920 hw->name, __func__, fifon);
921 skb_trim(rx_skb, 0);
922 spin_unlock(&hw->lock);
923 return;
927 memcpy(skb_put(rx_skb, len), data, len);
929 if (hdlc) {
930 /* we have a complete hdlc packet */
931 if (finish) {
932 if ((rx_skb->len > 3) &&
933 (!(rx_skb->data[rx_skb->len - 1]))) {
934 if (debug & DBG_HFC_FIFO_VERBOSE) {
935 printk(KERN_DEBUG "%s: %s: fifon(%i)"
936 " new RX len(%i): ",
937 hw->name, __func__, fifon,
938 rx_skb->len);
939 i = 0;
940 while (i < rx_skb->len)
941 printk("%02x ",
942 rx_skb->data[i++]);
943 printk("\n");
946 /* remove CRC & status */
947 skb_trim(rx_skb, rx_skb->len - 3);
949 if (fifo->dch)
950 recv_Dchannel(fifo->dch);
951 if (fifo->bch)
952 recv_Bchannel(fifo->bch, MISDN_ID_ANY);
953 if (fifo->ech)
954 recv_Echannel(fifo->ech,
955 &hw->dch);
956 } else {
957 if (debug & DBG_HFC_FIFO_VERBOSE) {
958 printk(KERN_DEBUG
959 "%s: CRC or minlen ERROR fifon(%i) "
960 "RX len(%i): ",
961 hw->name, fifon, rx_skb->len);
962 i = 0;
963 while (i < rx_skb->len)
964 printk("%02x ",
965 rx_skb->data[i++]);
966 printk("\n");
968 skb_trim(rx_skb, 0);
971 } else {
972 /* deliver transparent data to layer2 */
973 if (rx_skb->len >= poll)
974 recv_Bchannel(fifo->bch, MISDN_ID_ANY);
976 spin_unlock(&hw->lock);
979 static void
980 fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
981 void *buf, int num_packets, int packet_size, int interval,
982 usb_complete_t complete, void *context)
984 int k;
986 usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
987 complete, context);
989 urb->number_of_packets = num_packets;
990 urb->transfer_flags = URB_ISO_ASAP;
991 urb->actual_length = 0;
992 urb->interval = interval;
994 for (k = 0; k < num_packets; k++) {
995 urb->iso_frame_desc[k].offset = packet_size * k;
996 urb->iso_frame_desc[k].length = packet_size;
997 urb->iso_frame_desc[k].actual_length = 0;
1001 /* receive completion routine for all ISO tx fifos */
1002 static void
1003 rx_iso_complete(struct urb *urb)
1005 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1006 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1007 struct hfcsusb *hw = fifo->hw;
1008 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
1009 status, iso_status, i;
1010 __u8 *buf;
1011 static __u8 eof[8];
1012 __u8 s0_state;
1014 fifon = fifo->fifonum;
1015 status = urb->status;
1017 spin_lock(&hw->lock);
1018 if (fifo->stop_gracefull) {
1019 fifo->stop_gracefull = 0;
1020 fifo->active = 0;
1021 spin_unlock(&hw->lock);
1022 return;
1024 spin_unlock(&hw->lock);
1027 * ISO transfer only partially completed,
1028 * look at individual frame status for details
1030 if (status == -EXDEV) {
1031 if (debug & DEBUG_HW)
1032 printk(KERN_DEBUG "%s: %s: with -EXDEV "
1033 "urb->status %d, fifonum %d\n",
1034 hw->name, __func__, status, fifon);
1036 /* clear status, so go on with ISO transfers */
1037 status = 0;
1040 s0_state = 0;
1041 if (fifo->active && !status) {
1042 num_isoc_packets = iso_packets[fifon];
1043 maxlen = fifo->usb_packet_maxlen;
1045 for (k = 0; k < num_isoc_packets; ++k) {
1046 len = urb->iso_frame_desc[k].actual_length;
1047 offset = urb->iso_frame_desc[k].offset;
1048 buf = context_iso_urb->buffer + offset;
1049 iso_status = urb->iso_frame_desc[k].status;
1051 if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
1052 printk(KERN_DEBUG "%s: %s: "
1053 "ISO packet %i, status: %i\n",
1054 hw->name, __func__, k, iso_status);
1057 /* USB data log for every D ISO in */
1058 if ((fifon == HFCUSB_D_RX) &&
1059 (debug & DBG_HFC_USB_VERBOSE)) {
1060 printk(KERN_DEBUG
1061 "%s: %s: %d (%d/%d) len(%d) ",
1062 hw->name, __func__, urb->start_frame,
1063 k, num_isoc_packets-1,
1064 len);
1065 for (i = 0; i < len; i++)
1066 printk("%x ", buf[i]);
1067 printk("\n");
1070 if (!iso_status) {
1071 if (fifo->last_urblen != maxlen) {
1073 * save fifo fill-level threshold bits
1074 * to use them later in TX ISO URB
1075 * completions
1077 hw->threshold_mask = buf[1];
1079 if (fifon == HFCUSB_D_RX)
1080 s0_state = (buf[0] >> 4);
1082 eof[fifon] = buf[0] & 1;
1083 if (len > 2)
1084 hfcsusb_rx_frame(fifo, buf + 2,
1085 len - 2, (len < maxlen)
1086 ? eof[fifon] : 0);
1087 } else
1088 hfcsusb_rx_frame(fifo, buf, len,
1089 (len < maxlen) ?
1090 eof[fifon] : 0);
1091 fifo->last_urblen = len;
1095 /* signal S0 layer1 state change */
1096 if ((s0_state) && (hw->initdone) &&
1097 (s0_state != hw->dch.state)) {
1098 hw->dch.state = s0_state;
1099 schedule_event(&hw->dch, FLG_PHCHANGE);
1102 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1103 context_iso_urb->buffer, num_isoc_packets,
1104 fifo->usb_packet_maxlen, fifo->intervall,
1105 (usb_complete_t)rx_iso_complete, urb->context);
1106 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1107 if (errcode < 0) {
1108 if (debug & DEBUG_HW)
1109 printk(KERN_DEBUG "%s: %s: error submitting "
1110 "ISO URB: %d\n",
1111 hw->name, __func__, errcode);
1113 } else {
1114 if (status && (debug & DBG_HFC_URB_INFO))
1115 printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
1116 "urb->status %d, fifonum %d\n",
1117 hw->name, __func__, status, fifon);
1121 /* receive completion routine for all interrupt rx fifos */
1122 static void
1123 rx_int_complete(struct urb *urb)
1125 int len, status, i;
1126 __u8 *buf, maxlen, fifon;
1127 struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
1128 struct hfcsusb *hw = fifo->hw;
1129 static __u8 eof[8];
1131 spin_lock(&hw->lock);
1132 if (fifo->stop_gracefull) {
1133 fifo->stop_gracefull = 0;
1134 fifo->active = 0;
1135 spin_unlock(&hw->lock);
1136 return;
1138 spin_unlock(&hw->lock);
1140 fifon = fifo->fifonum;
1141 if ((!fifo->active) || (urb->status)) {
1142 if (debug & DBG_HFC_URB_ERROR)
1143 printk(KERN_DEBUG
1144 "%s: %s: RX-Fifo %i is going down (%i)\n",
1145 hw->name, __func__, fifon, urb->status);
1147 fifo->urb->interval = 0; /* cancel automatic rescheduling */
1148 return;
1150 len = urb->actual_length;
1151 buf = fifo->buffer;
1152 maxlen = fifo->usb_packet_maxlen;
1154 /* USB data log for every D INT in */
1155 if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
1156 printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
1157 hw->name, __func__, len);
1158 for (i = 0; i < len; i++)
1159 printk("%02x ", buf[i]);
1160 printk("\n");
1163 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
1164 /* the threshold mask is in the 2nd status byte */
1165 hw->threshold_mask = buf[1];
1167 /* signal S0 layer1 state change */
1168 if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
1169 hw->dch.state = (buf[0] >> 4);
1170 schedule_event(&hw->dch, FLG_PHCHANGE);
1173 eof[fifon] = buf[0] & 1;
1174 /* if we have more than the 2 status bytes -> collect data */
1175 if (len > 2)
1176 hfcsusb_rx_frame(fifo, buf + 2,
1177 urb->actual_length - 2,
1178 (len < maxlen) ? eof[fifon] : 0);
1179 } else {
1180 hfcsusb_rx_frame(fifo, buf, urb->actual_length,
1181 (len < maxlen) ? eof[fifon] : 0);
1183 fifo->last_urblen = urb->actual_length;
1185 status = usb_submit_urb(urb, GFP_ATOMIC);
1186 if (status) {
1187 if (debug & DEBUG_HW)
1188 printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
1189 hw->name, __func__);
1193 /* transmit completion routine for all ISO tx fifos */
1194 static void
1195 tx_iso_complete(struct urb *urb)
1197 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1198 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1199 struct hfcsusb *hw = fifo->hw;
1200 struct sk_buff *tx_skb;
1201 int k, tx_offset, num_isoc_packets, sink, remain, current_len,
1202 errcode, hdlc, i;
1203 int *tx_idx;
1204 int frame_complete, fifon, status;
1205 __u8 threshbit;
1207 spin_lock(&hw->lock);
1208 if (fifo->stop_gracefull) {
1209 fifo->stop_gracefull = 0;
1210 fifo->active = 0;
1211 spin_unlock(&hw->lock);
1212 return;
1215 if (fifo->dch) {
1216 tx_skb = fifo->dch->tx_skb;
1217 tx_idx = &fifo->dch->tx_idx;
1218 hdlc = 1;
1219 } else if (fifo->bch) {
1220 tx_skb = fifo->bch->tx_skb;
1221 tx_idx = &fifo->bch->tx_idx;
1222 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
1223 } else {
1224 printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
1225 hw->name, __func__);
1226 spin_unlock(&hw->lock);
1227 return;
1230 fifon = fifo->fifonum;
1231 status = urb->status;
1233 tx_offset = 0;
1236 * ISO transfer only partially completed,
1237 * look at individual frame status for details
1239 if (status == -EXDEV) {
1240 if (debug & DBG_HFC_URB_ERROR)
1241 printk(KERN_DEBUG "%s: %s: "
1242 "-EXDEV (%i) fifon (%d)\n",
1243 hw->name, __func__, status, fifon);
1245 /* clear status, so go on with ISO transfers */
1246 status = 0;
1249 if (fifo->active && !status) {
1250 /* is FifoFull-threshold set for our channel? */
1251 threshbit = (hw->threshold_mask & (1 << fifon));
1252 num_isoc_packets = iso_packets[fifon];
1254 /* predict dataflow to avoid fifo overflow */
1255 if (fifon >= HFCUSB_D_TX)
1256 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
1257 else
1258 sink = (threshbit) ? SINK_MIN : SINK_MAX;
1259 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1260 context_iso_urb->buffer, num_isoc_packets,
1261 fifo->usb_packet_maxlen, fifo->intervall,
1262 (usb_complete_t)tx_iso_complete, urb->context);
1263 memset(context_iso_urb->buffer, 0,
1264 sizeof(context_iso_urb->buffer));
1265 frame_complete = 0;
1267 for (k = 0; k < num_isoc_packets; ++k) {
1268 /* analyze tx success of previous ISO packets */
1269 if (debug & DBG_HFC_URB_ERROR) {
1270 errcode = urb->iso_frame_desc[k].status;
1271 if (errcode) {
1272 printk(KERN_DEBUG "%s: %s: "
1273 "ISO packet %i, status: %i\n",
1274 hw->name, __func__, k, errcode);
1278 /* Generate next ISO Packets */
1279 if (tx_skb)
1280 remain = tx_skb->len - *tx_idx;
1281 else
1282 remain = 0;
1284 if (remain > 0) {
1285 fifo->bit_line -= sink;
1286 current_len = (0 - fifo->bit_line) / 8;
1287 if (current_len > 14)
1288 current_len = 14;
1289 if (current_len < 0)
1290 current_len = 0;
1291 if (remain < current_len)
1292 current_len = remain;
1294 /* how much bit do we put on the line? */
1295 fifo->bit_line += current_len * 8;
1297 context_iso_urb->buffer[tx_offset] = 0;
1298 if (current_len == remain) {
1299 if (hdlc) {
1300 /* signal frame completion */
1301 context_iso_urb->
1302 buffer[tx_offset] = 1;
1303 /* add 2 byte flags and 16bit
1304 * CRC at end of ISDN frame */
1305 fifo->bit_line += 32;
1307 frame_complete = 1;
1310 /* copy tx data to iso-urb buffer */
1311 memcpy(context_iso_urb->buffer + tx_offset + 1,
1312 (tx_skb->data + *tx_idx), current_len);
1313 *tx_idx += current_len;
1315 urb->iso_frame_desc[k].offset = tx_offset;
1316 urb->iso_frame_desc[k].length = current_len + 1;
1318 /* USB data log for every D ISO out */
1319 if ((fifon == HFCUSB_D_RX) &&
1320 (debug & DBG_HFC_USB_VERBOSE)) {
1321 printk(KERN_DEBUG
1322 "%s: %s (%d/%d) offs(%d) len(%d) ",
1323 hw->name, __func__,
1324 k, num_isoc_packets-1,
1325 urb->iso_frame_desc[k].offset,
1326 urb->iso_frame_desc[k].length);
1328 for (i = urb->iso_frame_desc[k].offset;
1329 i < (urb->iso_frame_desc[k].offset
1330 + urb->iso_frame_desc[k].length);
1331 i++)
1332 printk("%x ",
1333 context_iso_urb->buffer[i]);
1335 printk(" skb->len(%i) tx-idx(%d)\n",
1336 tx_skb->len, *tx_idx);
1339 tx_offset += (current_len + 1);
1340 } else {
1341 urb->iso_frame_desc[k].offset = tx_offset++;
1342 urb->iso_frame_desc[k].length = 1;
1343 /* we lower data margin every msec */
1344 fifo->bit_line -= sink;
1345 if (fifo->bit_line < BITLINE_INF)
1346 fifo->bit_line = BITLINE_INF;
1349 if (frame_complete) {
1350 frame_complete = 0;
1352 if (debug & DBG_HFC_FIFO_VERBOSE) {
1353 printk(KERN_DEBUG "%s: %s: "
1354 "fifon(%i) new TX len(%i): ",
1355 hw->name, __func__,
1356 fifon, tx_skb->len);
1357 i = 0;
1358 while (i < tx_skb->len)
1359 printk("%02x ",
1360 tx_skb->data[i++]);
1361 printk("\n");
1364 dev_kfree_skb(tx_skb);
1365 tx_skb = NULL;
1366 if (fifo->dch && get_next_dframe(fifo->dch))
1367 tx_skb = fifo->dch->tx_skb;
1368 else if (fifo->bch &&
1369 get_next_bframe(fifo->bch)) {
1370 if (test_bit(FLG_TRANSPARENT,
1371 &fifo->bch->Flags))
1372 confirm_Bsend(fifo->bch);
1373 tx_skb = fifo->bch->tx_skb;
1377 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1378 if (errcode < 0) {
1379 if (debug & DEBUG_HW)
1380 printk(KERN_DEBUG
1381 "%s: %s: error submitting ISO URB: %d \n",
1382 hw->name, __func__, errcode);
1386 * abuse DChannel tx iso completion to trigger NT mode state
1387 * changes tx_iso_complete is assumed to be called every
1388 * fifo->intervall (ms)
1390 if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
1391 && (hw->timers & NT_ACTIVATION_TIMER)) {
1392 if ((--hw->nt_timer) < 0)
1393 schedule_event(&hw->dch, FLG_PHCHANGE);
1396 } else {
1397 if (status && (debug & DBG_HFC_URB_ERROR))
1398 printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
1399 "fifonum=%d\n",
1400 hw->name, __func__,
1401 symbolic(urb_errlist, status), status, fifon);
1403 spin_unlock(&hw->lock);
1407 * allocs urbs and start isoc transfer with two pending urbs to avoid
1408 * gaps in the transfer chain
1410 static int
1411 start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
1412 usb_complete_t complete, int packet_size)
1414 struct hfcsusb *hw = fifo->hw;
1415 int i, k, errcode;
1417 if (debug)
1418 printk(KERN_DEBUG "%s: %s: fifo %i\n",
1419 hw->name, __func__, fifo->fifonum);
1421 /* allocate Memory for Iso out Urbs */
1422 for (i = 0; i < 2; i++) {
1423 if (!(fifo->iso[i].urb)) {
1424 fifo->iso[i].urb =
1425 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
1426 if (!(fifo->iso[i].urb)) {
1427 printk(KERN_DEBUG
1428 "%s: %s: alloc urb for fifo %i failed",
1429 hw->name, __func__, fifo->fifonum);
1431 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
1432 fifo->iso[i].indx = i;
1434 /* Init the first iso */
1435 if (ISO_BUFFER_SIZE >=
1436 (fifo->usb_packet_maxlen *
1437 num_packets_per_urb)) {
1438 fill_isoc_urb(fifo->iso[i].urb,
1439 fifo->hw->dev, fifo->pipe,
1440 fifo->iso[i].buffer,
1441 num_packets_per_urb,
1442 fifo->usb_packet_maxlen,
1443 fifo->intervall, complete,
1444 &fifo->iso[i]);
1445 memset(fifo->iso[i].buffer, 0,
1446 sizeof(fifo->iso[i].buffer));
1448 for (k = 0; k < num_packets_per_urb; k++) {
1449 fifo->iso[i].urb->
1450 iso_frame_desc[k].offset =
1451 k * packet_size;
1452 fifo->iso[i].urb->
1453 iso_frame_desc[k].length =
1454 packet_size;
1456 } else {
1457 printk(KERN_DEBUG
1458 "%s: %s: ISO Buffer size to small!\n",
1459 hw->name, __func__);
1462 fifo->bit_line = BITLINE_INF;
1464 errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
1465 fifo->active = (errcode >= 0) ? 1 : 0;
1466 fifo->stop_gracefull = 0;
1467 if (errcode < 0) {
1468 printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
1469 hw->name, __func__,
1470 symbolic(urb_errlist, errcode), i);
1473 return fifo->active;
1476 static void
1477 stop_iso_gracefull(struct usb_fifo *fifo)
1479 struct hfcsusb *hw = fifo->hw;
1480 int i, timeout;
1481 u_long flags;
1483 for (i = 0; i < 2; i++) {
1484 spin_lock_irqsave(&hw->lock, flags);
1485 if (debug)
1486 printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
1487 hw->name, __func__, fifo->fifonum, i);
1488 fifo->stop_gracefull = 1;
1489 spin_unlock_irqrestore(&hw->lock, flags);
1492 for (i = 0; i < 2; i++) {
1493 timeout = 3;
1494 while (fifo->stop_gracefull && timeout--)
1495 schedule_timeout_interruptible((HZ/1000)*16);
1496 if (debug && fifo->stop_gracefull)
1497 printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
1498 hw->name, __func__, fifo->fifonum, i);
1502 static void
1503 stop_int_gracefull(struct usb_fifo *fifo)
1505 struct hfcsusb *hw = fifo->hw;
1506 int timeout;
1507 u_long flags;
1509 spin_lock_irqsave(&hw->lock, flags);
1510 if (debug)
1511 printk(KERN_DEBUG "%s: %s for fifo %i\n",
1512 hw->name, __func__, fifo->fifonum);
1513 fifo->stop_gracefull = 1;
1514 spin_unlock_irqrestore(&hw->lock, flags);
1516 timeout = 3;
1517 while (fifo->stop_gracefull && timeout--)
1518 schedule_timeout_interruptible((HZ/1000)*3);
1519 if (debug && fifo->stop_gracefull)
1520 printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
1521 hw->name, __func__, fifo->fifonum);
1524 /* start the interrupt transfer for the given fifo */
1525 static void
1526 start_int_fifo(struct usb_fifo *fifo)
1528 struct hfcsusb *hw = fifo->hw;
1529 int errcode;
1531 if (debug)
1532 printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
1533 hw->name, __func__, fifo->fifonum);
1535 if (!fifo->urb) {
1536 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
1537 if (!fifo->urb)
1538 return;
1540 usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
1541 fifo->buffer, fifo->usb_packet_maxlen,
1542 (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
1543 fifo->active = 1;
1544 fifo->stop_gracefull = 0;
1545 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
1546 if (errcode) {
1547 printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
1548 hw->name, __func__, errcode);
1549 fifo->active = 0;
1553 static void
1554 setPortMode(struct hfcsusb *hw)
1556 if (debug & DEBUG_HW)
1557 printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
1558 (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
1560 if (hw->protocol == ISDN_P_TE_S0) {
1561 write_reg(hw, HFCUSB_SCTRL, 0x40);
1562 write_reg(hw, HFCUSB_SCTRL_E, 0x00);
1563 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
1564 write_reg(hw, HFCUSB_STATES, 3 | 0x10);
1565 write_reg(hw, HFCUSB_STATES, 3);
1566 } else {
1567 write_reg(hw, HFCUSB_SCTRL, 0x44);
1568 write_reg(hw, HFCUSB_SCTRL_E, 0x09);
1569 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
1570 write_reg(hw, HFCUSB_STATES, 1 | 0x10);
1571 write_reg(hw, HFCUSB_STATES, 1);
1575 static void
1576 reset_hfcsusb(struct hfcsusb *hw)
1578 struct usb_fifo *fifo;
1579 int i;
1581 if (debug & DEBUG_HW)
1582 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1584 /* do Chip reset */
1585 write_reg(hw, HFCUSB_CIRM, 8);
1587 /* aux = output, reset off */
1588 write_reg(hw, HFCUSB_CIRM, 0x10);
1590 /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
1591 write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
1592 ((hw->packet_size / 8) << 4));
1594 /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
1595 write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
1597 /* enable PCM/GCI master mode */
1598 write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
1599 write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
1601 /* init the fifos */
1602 write_reg(hw, HFCUSB_F_THRES,
1603 (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1605 fifo = hw->fifos;
1606 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1607 write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
1608 fifo[i].max_size =
1609 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1610 fifo[i].last_urblen = 0;
1612 /* set 2 bit for D- & E-channel */
1613 write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
1615 /* enable all fifos */
1616 if (i == HFCUSB_D_TX)
1617 write_reg(hw, HFCUSB_CON_HDLC,
1618 (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
1619 else
1620 write_reg(hw, HFCUSB_CON_HDLC, 0x08);
1621 write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1624 write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1625 handle_led(hw, LED_POWER_ON);
1628 /* start USB data pipes dependand on device's endpoint configuration */
1629 static void
1630 hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
1632 /* quick check if endpoint already running */
1633 if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
1634 return;
1635 if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
1636 return;
1637 if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
1638 return;
1639 if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
1640 return;
1642 /* start rx endpoints using USB INT IN method */
1643 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
1644 start_int_fifo(hw->fifos + channel*2 + 1);
1646 /* start rx endpoints using USB ISO IN method */
1647 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
1648 switch (channel) {
1649 case HFC_CHAN_D:
1650 start_isoc_chain(hw->fifos + HFCUSB_D_RX,
1651 ISOC_PACKETS_D,
1652 (usb_complete_t)rx_iso_complete,
1653 16);
1654 break;
1655 case HFC_CHAN_E:
1656 start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
1657 ISOC_PACKETS_D,
1658 (usb_complete_t)rx_iso_complete,
1659 16);
1660 break;
1661 case HFC_CHAN_B1:
1662 start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
1663 ISOC_PACKETS_B,
1664 (usb_complete_t)rx_iso_complete,
1665 16);
1666 break;
1667 case HFC_CHAN_B2:
1668 start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
1669 ISOC_PACKETS_B,
1670 (usb_complete_t)rx_iso_complete,
1671 16);
1672 break;
1676 /* start tx endpoints using USB ISO OUT method */
1677 switch (channel) {
1678 case HFC_CHAN_D:
1679 start_isoc_chain(hw->fifos + HFCUSB_D_TX,
1680 ISOC_PACKETS_B,
1681 (usb_complete_t)tx_iso_complete, 1);
1682 break;
1683 case HFC_CHAN_B1:
1684 start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
1685 ISOC_PACKETS_D,
1686 (usb_complete_t)tx_iso_complete, 1);
1687 break;
1688 case HFC_CHAN_B2:
1689 start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
1690 ISOC_PACKETS_B,
1691 (usb_complete_t)tx_iso_complete, 1);
1692 break;
1696 /* stop USB data pipes dependand on device's endpoint configuration */
1697 static void
1698 hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
1700 /* quick check if endpoint currently running */
1701 if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
1702 return;
1703 if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
1704 return;
1705 if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
1706 return;
1707 if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
1708 return;
1710 /* rx endpoints using USB INT IN method */
1711 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
1712 stop_int_gracefull(hw->fifos + channel*2 + 1);
1714 /* rx endpoints using USB ISO IN method */
1715 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
1716 stop_iso_gracefull(hw->fifos + channel*2 + 1);
1718 /* tx endpoints using USB ISO OUT method */
1719 if (channel != HFC_CHAN_E)
1720 stop_iso_gracefull(hw->fifos + channel*2);
1724 /* Hardware Initialization */
1725 static int
1726 setup_hfcsusb(struct hfcsusb *hw)
1728 u_char b;
1730 if (debug & DBG_HFC_CALL_TRACE)
1731 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1733 /* check the chip id */
1734 if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
1735 printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
1736 hw->name, __func__);
1737 return 1;
1739 if (b != HFCUSB_CHIPID) {
1740 printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
1741 hw->name, __func__, b);
1742 return 1;
1745 /* first set the needed config, interface and alternate */
1746 (void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
1748 hw->led_state = 0;
1750 /* init the background machinery for control requests */
1751 hw->ctrl_read.bRequestType = 0xc0;
1752 hw->ctrl_read.bRequest = 1;
1753 hw->ctrl_read.wLength = cpu_to_le16(1);
1754 hw->ctrl_write.bRequestType = 0x40;
1755 hw->ctrl_write.bRequest = 0;
1756 hw->ctrl_write.wLength = 0;
1757 usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
1758 (u_char *)&hw->ctrl_write, NULL, 0,
1759 (usb_complete_t)ctrl_complete, hw);
1761 reset_hfcsusb(hw);
1762 return 0;
1765 static void
1766 release_hw(struct hfcsusb *hw)
1768 if (debug & DBG_HFC_CALL_TRACE)
1769 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1772 * stop all endpoints gracefully
1773 * TODO: mISDN_core should generate CLOSE_CHANNEL
1774 * signals after calling mISDN_unregister_device()
1776 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
1777 hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
1778 hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
1779 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1780 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
1781 if (hw->protocol == ISDN_P_TE_S0)
1782 l1_event(hw->dch.l1, CLOSE_CHANNEL);
1784 mISDN_unregister_device(&hw->dch.dev);
1785 mISDN_freebchannel(&hw->bch[1]);
1786 mISDN_freebchannel(&hw->bch[0]);
1787 mISDN_freedchannel(&hw->dch);
1789 if (hw->ctrl_urb) {
1790 usb_kill_urb(hw->ctrl_urb);
1791 usb_free_urb(hw->ctrl_urb);
1792 hw->ctrl_urb = NULL;
1795 if (hw->intf)
1796 usb_set_intfdata(hw->intf, NULL);
1797 list_del(&hw->list);
1798 kfree(hw);
1799 hw = NULL;
1802 static void
1803 deactivate_bchannel(struct bchannel *bch)
1805 struct hfcsusb *hw = bch->hw;
1806 u_long flags;
1808 if (bch->debug & DEBUG_HW)
1809 printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
1810 hw->name, __func__, bch->nr);
1812 spin_lock_irqsave(&hw->lock, flags);
1813 mISDN_clear_bchannel(bch);
1814 spin_unlock_irqrestore(&hw->lock, flags);
1815 hfcsusb_setup_bch(bch, ISDN_P_NONE);
1816 hfcsusb_stop_endpoint(hw, bch->nr);
1820 * Layer 1 B-channel hardware access
1822 static int
1823 hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1825 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1826 int ret = -EINVAL;
1828 if (bch->debug & DEBUG_HW)
1829 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1831 switch (cmd) {
1832 case HW_TESTRX_RAW:
1833 case HW_TESTRX_HDLC:
1834 case HW_TESTRX_OFF:
1835 ret = -EINVAL;
1836 break;
1838 case CLOSE_CHANNEL:
1839 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1840 if (test_bit(FLG_ACTIVE, &bch->Flags))
1841 deactivate_bchannel(bch);
1842 ch->protocol = ISDN_P_NONE;
1843 ch->peer = NULL;
1844 module_put(THIS_MODULE);
1845 ret = 0;
1846 break;
1847 case CONTROL_CHANNEL:
1848 ret = channel_bctrl(bch, arg);
1849 break;
1850 default:
1851 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1852 __func__, cmd);
1854 return ret;
1857 static int
1858 setup_instance(struct hfcsusb *hw, struct device *parent)
1860 u_long flags;
1861 int err, i;
1863 if (debug & DBG_HFC_CALL_TRACE)
1864 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1866 spin_lock_init(&hw->ctrl_lock);
1867 spin_lock_init(&hw->lock);
1869 mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
1870 hw->dch.debug = debug & 0xFFFF;
1871 hw->dch.hw = hw;
1872 hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1873 hw->dch.dev.D.send = hfcusb_l2l1D;
1874 hw->dch.dev.D.ctrl = hfc_dctrl;
1876 /* enable E-Channel logging */
1877 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1878 mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
1880 hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1881 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1882 hw->dch.dev.nrbchan = 2;
1883 for (i = 0; i < 2; i++) {
1884 hw->bch[i].nr = i + 1;
1885 set_channelmap(i + 1, hw->dch.dev.channelmap);
1886 hw->bch[i].debug = debug;
1887 mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM);
1888 hw->bch[i].hw = hw;
1889 hw->bch[i].ch.send = hfcusb_l2l1B;
1890 hw->bch[i].ch.ctrl = hfc_bctrl;
1891 hw->bch[i].ch.nr = i + 1;
1892 list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
1895 hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
1896 hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
1897 hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
1898 hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
1899 hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
1900 hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
1901 hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
1902 hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
1904 err = setup_hfcsusb(hw);
1905 if (err)
1906 goto out;
1908 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
1909 hfcsusb_cnt + 1);
1910 printk(KERN_INFO "%s: registered as '%s'\n",
1911 DRIVER_NAME, hw->name);
1913 err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
1914 if (err)
1915 goto out;
1917 hfcsusb_cnt++;
1918 write_lock_irqsave(&HFClock, flags);
1919 list_add_tail(&hw->list, &HFClist);
1920 write_unlock_irqrestore(&HFClock, flags);
1921 return 0;
1923 out:
1924 mISDN_freebchannel(&hw->bch[1]);
1925 mISDN_freebchannel(&hw->bch[0]);
1926 mISDN_freedchannel(&hw->dch);
1927 kfree(hw);
1928 return err;
1931 static int
1932 hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1934 struct hfcsusb *hw;
1935 struct usb_device *dev = interface_to_usbdev(intf);
1936 struct usb_host_interface *iface = intf->cur_altsetting;
1937 struct usb_host_interface *iface_used = NULL;
1938 struct usb_host_endpoint *ep;
1939 struct hfcsusb_vdata *driver_info;
1940 int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
1941 probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
1942 ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
1943 alt_used = 0;
1945 vend_idx = 0xffff;
1946 for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
1947 if ((le16_to_cpu(dev->descriptor.idVendor)
1948 == hfcsusb_idtab[i].idVendor) &&
1949 (le16_to_cpu(dev->descriptor.idProduct)
1950 == hfcsusb_idtab[i].idProduct)) {
1951 vend_idx = i;
1952 continue;
1956 printk(KERN_DEBUG
1957 "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
1958 __func__, ifnum, iface->desc.bAlternateSetting,
1959 intf->minor, vend_idx);
1961 if (vend_idx == 0xffff) {
1962 printk(KERN_WARNING
1963 "%s: no valid vendor found in USB descriptor\n",
1964 __func__);
1965 return -EIO;
1967 /* if vendor and product ID is OK, start probing alternate settings */
1968 alt_idx = 0;
1969 small_match = -1;
1971 /* default settings */
1972 iso_packet_size = 16;
1973 packet_size = 64;
1975 while (alt_idx < intf->num_altsetting) {
1976 iface = intf->altsetting + alt_idx;
1977 probe_alt_setting = iface->desc.bAlternateSetting;
1978 cfg_used = 0;
1980 while (validconf[cfg_used][0]) {
1981 cfg_found = 1;
1982 vcf = validconf[cfg_used];
1983 ep = iface->endpoint;
1984 memcpy(cmptbl, vcf, 16 * sizeof(int));
1986 /* check for all endpoints in this alternate setting */
1987 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1988 ep_addr = ep->desc.bEndpointAddress;
1990 /* get endpoint base */
1991 idx = ((ep_addr & 0x7f) - 1) * 2;
1992 if (ep_addr & 0x80)
1993 idx++;
1994 attr = ep->desc.bmAttributes;
1996 if (cmptbl[idx] != EP_NOP) {
1997 if (cmptbl[idx] == EP_NUL)
1998 cfg_found = 0;
1999 if (attr == USB_ENDPOINT_XFER_INT
2000 && cmptbl[idx] == EP_INT)
2001 cmptbl[idx] = EP_NUL;
2002 if (attr == USB_ENDPOINT_XFER_BULK
2003 && cmptbl[idx] == EP_BLK)
2004 cmptbl[idx] = EP_NUL;
2005 if (attr == USB_ENDPOINT_XFER_ISOC
2006 && cmptbl[idx] == EP_ISO)
2007 cmptbl[idx] = EP_NUL;
2009 if (attr == USB_ENDPOINT_XFER_INT &&
2010 ep->desc.bInterval < vcf[17]) {
2011 cfg_found = 0;
2014 ep++;
2017 for (i = 0; i < 16; i++)
2018 if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
2019 cfg_found = 0;
2021 if (cfg_found) {
2022 if (small_match < cfg_used) {
2023 small_match = cfg_used;
2024 alt_used = probe_alt_setting;
2025 iface_used = iface;
2028 cfg_used++;
2030 alt_idx++;
2031 } /* (alt_idx < intf->num_altsetting) */
2033 /* not found a valid USB Ta Endpoint config */
2034 if (small_match == -1)
2035 return -EIO;
2037 iface = iface_used;
2038 hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
2039 if (!hw)
2040 return -ENOMEM; /* got no mem */
2041 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
2043 ep = iface->endpoint;
2044 vcf = validconf[small_match];
2046 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2047 struct usb_fifo *f;
2049 ep_addr = ep->desc.bEndpointAddress;
2050 /* get endpoint base */
2051 idx = ((ep_addr & 0x7f) - 1) * 2;
2052 if (ep_addr & 0x80)
2053 idx++;
2054 f = &hw->fifos[idx & 7];
2056 /* init Endpoints */
2057 if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
2058 ep++;
2059 continue;
2061 switch (ep->desc.bmAttributes) {
2062 case USB_ENDPOINT_XFER_INT:
2063 f->pipe = usb_rcvintpipe(dev,
2064 ep->desc.bEndpointAddress);
2065 f->usb_transfer_mode = USB_INT;
2066 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2067 break;
2068 case USB_ENDPOINT_XFER_BULK:
2069 if (ep_addr & 0x80)
2070 f->pipe = usb_rcvbulkpipe(dev,
2071 ep->desc.bEndpointAddress);
2072 else
2073 f->pipe = usb_sndbulkpipe(dev,
2074 ep->desc.bEndpointAddress);
2075 f->usb_transfer_mode = USB_BULK;
2076 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2077 break;
2078 case USB_ENDPOINT_XFER_ISOC:
2079 if (ep_addr & 0x80)
2080 f->pipe = usb_rcvisocpipe(dev,
2081 ep->desc.bEndpointAddress);
2082 else
2083 f->pipe = usb_sndisocpipe(dev,
2084 ep->desc.bEndpointAddress);
2085 f->usb_transfer_mode = USB_ISOC;
2086 iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2087 break;
2088 default:
2089 f->pipe = 0;
2092 if (f->pipe) {
2093 f->fifonum = idx & 7;
2094 f->hw = hw;
2095 f->usb_packet_maxlen =
2096 le16_to_cpu(ep->desc.wMaxPacketSize);
2097 f->intervall = ep->desc.bInterval;
2099 ep++;
2101 hw->dev = dev; /* save device */
2102 hw->if_used = ifnum; /* save used interface */
2103 hw->alt_used = alt_used; /* and alternate config */
2104 hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
2105 hw->cfg_used = vcf[16]; /* store used config */
2106 hw->vend_idx = vend_idx; /* store found vendor */
2107 hw->packet_size = packet_size;
2108 hw->iso_packet_size = iso_packet_size;
2110 /* create the control pipes needed for register access */
2111 hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
2112 hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
2113 hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
2115 driver_info =
2116 (struct hfcsusb_vdata *)hfcsusb_idtab[vend_idx].driver_info;
2117 printk(KERN_DEBUG "%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
2118 hw->name, __func__, driver_info->vend_name,
2119 conf_str[small_match], ifnum, alt_used);
2121 if (setup_instance(hw, dev->dev.parent))
2122 return -EIO;
2124 hw->intf = intf;
2125 usb_set_intfdata(hw->intf, hw);
2126 return 0;
2129 /* function called when an active device is removed */
2130 static void
2131 hfcsusb_disconnect(struct usb_interface *intf)
2133 struct hfcsusb *hw = usb_get_intfdata(intf);
2134 struct hfcsusb *next;
2135 int cnt = 0;
2137 printk(KERN_INFO "%s: device disconnected\n", hw->name);
2139 handle_led(hw, LED_POWER_OFF);
2140 release_hw(hw);
2142 list_for_each_entry_safe(hw, next, &HFClist, list)
2143 cnt++;
2144 if (!cnt)
2145 hfcsusb_cnt = 0;
2147 usb_set_intfdata(intf, NULL);
2150 static struct usb_driver hfcsusb_drv = {
2151 .name = DRIVER_NAME,
2152 .id_table = hfcsusb_idtab,
2153 .probe = hfcsusb_probe,
2154 .disconnect = hfcsusb_disconnect,
2157 static int __init
2158 hfcsusb_init(void)
2160 printk(KERN_INFO DRIVER_NAME " driver Rev. %s debug(0x%x) poll(%i)\n",
2161 hfcsusb_rev, debug, poll);
2163 if (usb_register(&hfcsusb_drv)) {
2164 printk(KERN_INFO DRIVER_NAME
2165 ": Unable to register hfcsusb module at usb stack\n");
2166 return -ENODEV;
2169 return 0;
2172 static void __exit
2173 hfcsusb_cleanup(void)
2175 if (debug & DBG_HFC_CALL_TRACE)
2176 printk(KERN_INFO DRIVER_NAME ": %s\n", __func__);
2178 /* unregister Hardware */
2179 usb_deregister(&hfcsusb_drv); /* release our driver */
2182 module_init(hfcsusb_init);
2183 module_exit(hfcsusb_cleanup);