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