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