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