blob: b539b10d6f3e1476078db174f6e88eea72a21e87 [file] [log] [blame]
Karsten Keil69f52ad2009-01-09 16:20:51 +01001/* 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 *
Danny Kukawka670d6082012-02-10 05:01:08 +000030 * Revision: 0.3.3 (socket), 2008-11-05
Karsten Keil69f52ad2009-01-09 16:20:51 +010031 */
32
33#include <linux/module.h>
34#include <linux/delay.h>
35#include <linux/usb.h>
36#include <linux/mISDNhw.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Karsten Keil69f52ad2009-01-09 16:20:51 +010038#include "hfcsusb.h"
39
Karsten Keil69f52ad2009-01-09 16:20:51 +010040static unsigned int debug;
41static int poll = DEFAULT_TRANSP_BURST_SZ;
42
43static LIST_HEAD(HFClist);
44static DEFINE_RWLOCK(HFClock);
45
46
47MODULE_AUTHOR("Martin Bachem");
48MODULE_LICENSE("GPL");
49module_param(debug, uint, S_IRUGO | S_IWUSR);
50module_param(poll, int, 0);
51
52static int hfcsusb_cnt;
53
54/* some function prototypes */
55static void hfcsusb_ph_command(struct hfcsusb *hw, u_char command);
56static void release_hw(struct hfcsusb *hw);
57static void reset_hfcsusb(struct hfcsusb *hw);
58static void setPortMode(struct hfcsusb *hw);
59static void hfcsusb_start_endpoint(struct hfcsusb *hw, int channel);
60static void hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel);
61static int hfcsusb_setup_bch(struct bchannel *bch, int protocol);
62static void deactivate_bchannel(struct bchannel *bch);
63static void hfcsusb_ph_info(struct hfcsusb *hw);
64
65/* start next background transfer for control channel */
66static void
67ctrl_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 =
Joe Perches475be4d2012-02-19 19:52:38 -080078 cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].hfcs_reg);
Karsten Keil69f52ad2009-01-09 16:20:51 +010079 hw->ctrl_write.wValue =
Joe Perches475be4d2012-02-19 19:52:38 -080080 cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].reg_val);
Karsten Keil69f52ad2009-01-09 16:20:51 +010081
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 */
90static 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",
Joe Perches475be4d2012-02-19 19:52:38 -080096 hw->name, __func__, reg, val);
Karsten Keil69f52ad2009-01-09 16:20:51 +010097
98 spin_lock(&hw->ctrl_lock);
Julia Lawalle72e9f32010-05-26 05:55:10 +000099 if (hw->ctrl_cnt >= HFC_CTRL_BUFSIZE) {
100 spin_unlock(&hw->ctrl_lock);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100101 return 1;
Julia Lawalle72e9f32010-05-26 05:55:10 +0000102 }
Karsten Keil69f52ad2009-01-09 16:20:51 +0100103 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 */
116static void
117ctrl_complete(struct urb *urb)
118{
119 struct hfcsusb *hw = (struct hfcsusb *) urb->context;
Karsten Keil69f52ad2009-01-09 16:20:51 +0100120
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) {
Karsten Keil69f52ad2009-01-09 16:20:51 +0100126 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 */
135static void
136set_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 */
152static void
153handle_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800199 hw->name, __func__,
200 HFCUSB_P_DATA, hw->led_state);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100201
202 write_reg(hw, HFCUSB_P_DATA, hw->led_state);
203 }
204}
205
206/*
207 * Layer2 -> Layer 1 Bchannel data
208 */
209static int
210hfcusb_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800228 hw->name, __func__, ret);
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000229 if (ret > 0)
Karsten Keil69f52ad2009-01-09 16:20:51 +0100230 ret = 0;
Karsten Keil69f52ad2009-01-09 16:20:51 +0100231 return ret;
232 case PH_ACTIVATE_REQ:
233 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
Martin Bachem37952cf2012-05-15 23:51:04 +0000234 hfcsusb_start_endpoint(hw, bch->nr - 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100235 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,
Joe Perches475be4d2012-02-19 19:52:38 -0800240 0, NULL, GFP_KERNEL);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100241 break;
242 case PH_DEACTIVATE_REQ:
243 deactivate_bchannel(bch);
244 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY,
Joe Perches475be4d2012-02-19 19:52:38 -0800245 0, NULL, GFP_KERNEL);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100246 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 */
258static void
259hfcsusb_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) +
Joe Perches475be4d2012-02-19 19:52:38 -0800266 dch->dev.nrbchan * sizeof(struct ph_info_ch), GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100267 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,
Joe Perches475be4d2012-02-19 19:52:38 -0800276 sizeof(struct ph_info_dch) + dch->dev.nrbchan *
277 sizeof(struct ph_info_ch), phi, GFP_ATOMIC);
Jesper Juhl84860c72011-06-11 06:36:42 +0000278 kfree(phi);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100279}
280
281/*
282 * Layer2 -> Layer 1 Dchannel data
283 */
284static int
285hfcusb_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800298 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100299
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",
Joe Perches475be4d2012-02-19 19:52:38 -0800312 hw->name, __func__,
313 (hw->protocol == ISDN_P_NT_S0) ? "NT" : "TE");
Karsten Keil69f52ad2009-01-09 16:20:51 +0100314
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800319 PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
320 NULL, GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100321 } else {
322 hfcsusb_ph_command(hw,
Joe Perches475be4d2012-02-19 19:52:38 -0800323 HFC_L1_ACTIVATE_NT);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100324 test_and_set_bit(FLG_L2_ACTIVATED,
Joe Perches475be4d2012-02-19 19:52:38 -0800325 &dch->Flags);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100326 }
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",
Joe Perches475be4d2012-02-19 19:52:38 -0800336 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100337 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 */
374static int
375hfc_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800381 hw->name, __func__, cmd);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100382
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800406 GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100407 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,
Joe Perches475be4d2012-02-19 19:52:38 -0800411 GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100412 break;
413 default:
414 if (dch->debug & DEBUG_HW)
415 printk(KERN_DEBUG "%s: %s: unknown cmd %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800416 hw->name, __func__, cmd);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100417 return -1;
418 }
419 hfcsusb_ph_info(hw);
420 return 0;
421}
422
423static int
424open_dchannel(struct hfcsusb *hw, struct mISDNchannel *ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800425 struct channel_req *rq)
Karsten Keil69f52ad2009-01-09 16:20:51 +0100426{
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",
Joe Perches475be4d2012-02-19 19:52:38 -0800431 hw->name, __func__, hw->dch.dev.id, rq->adr.channel,
432 __builtin_return_address(0));
Karsten Keil69f52ad2009-01-09 16:20:51 +0100433 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,
Joe Perches475be4d2012-02-19 19:52:38 -0800446 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100447 } 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,
Joe Perches475be4d2012-02-19 19:52:38 -0800469 0, NULL, GFP_KERNEL);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100470 rq->ch = ch;
471 if (!try_module_get(THIS_MODULE))
472 printk(KERN_WARNING "%s: %s: cannot get module\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800473 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100474 return 0;
475}
476
477static int
478open_bchannel(struct hfcsusb *hw, struct channel_req *rq)
479{
480 struct bchannel *bch;
481
Dan Carpenter819a1002012-03-26 21:20:48 +0000482 if (rq->adr.channel == 0 || rq->adr.channel > 2)
Karsten Keil69f52ad2009-01-09 16:20:51 +0100483 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",
Joe Perches475be4d2012-02-19 19:52:38 -0800489 hw->name, __func__, rq->adr.channel);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100490
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 */
Karsten Keil69f52ad2009-01-09 16:20:51 +0100494 bch->ch.protocol = rq->protocol;
495 rq->ch = &bch->ch;
496
Karsten Keil69f52ad2009-01-09 16:20:51 +0100497 if (!try_module_get(THIS_MODULE))
498 printk(KERN_WARNING "%s: %s:cannot get module\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800499 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100500 return 0;
501}
502
503static int
504channel_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800510 hw->name, __func__, (cq->op), (cq->channel));
Karsten Keil69f52ad2009-01-09 16:20:51 +0100511
512 switch (cq->op) {
513 case MISDN_CTRL_GETOP:
514 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
Joe Perches475be4d2012-02-19 19:52:38 -0800515 MISDN_CTRL_DISCONNECT;
Karsten Keil69f52ad2009-01-09 16:20:51 +0100516 break;
517 default:
518 printk(KERN_WARNING "%s: %s: unknown Op %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800519 hw->name, __func__, cq->op);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100520 ret = -EINVAL;
521 break;
522 }
523 return ret;
524}
525
526/*
527 * device control function
528 */
529static int
530hfc_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800540 hw->name, __func__, cmd, arg);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100541 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
Joe Perches475be4d2012-02-19 19:52:38 -0800556 "%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);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100559 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",
Joe Perches475be4d2012-02-19 19:52:38 -0800573 hw->name, __func__, cmd);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100574 return -EINVAL;
575 }
576 return err;
577}
578
579/*
580 * S0 TE state change event handler
581 */
582static void
583ph_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__,
Joe Perches475be4d2012-02-19 19:52:38 -0800590 HFC_TE_LAYER1_STATES[dch->state]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100591 else
592 printk(KERN_DEBUG "%s: %s: TE F%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800593 hw->name, __func__, dch->state);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100594 }
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 */
623static void
624ph_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800631 hw->name, __func__,
632 HFC_NT_LAYER1_STATES[dch->state]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100633
634 else
635 printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800636 hw->name, __func__, dch->state);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100637 }
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800665 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100666 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
678static void
679ph_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 */
692static int
693hfcsusb_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",
Joe Perches475be4d2012-02-19 19:52:38 -0800700 hw->name, __func__, bch->state, protocol,
701 bch->nr);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100702
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;
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200711 /* fall through */
Karsten Keil69f52ad2009-01-09 16:20:51 +0100712 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",
Joe Perches475be4d2012-02-19 19:52:38 -0800731 hw->name, __func__, protocol);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100732 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 :
Joe Perches475be4d2012-02-19 19:52:38 -0800760 LED_B2_OFF);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100761 }
762 hfcsusb_ph_info(hw);
763 return 0;
764}
765
766static void
767hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
768{
769 if (debug & DEBUG_HW)
770 printk(KERN_DEBUG "%s: %s: %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800771 hw->name, __func__, command);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100772
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800789 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100790 else
791 write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
Joe Perches475be4d2012-02-19 19:52:38 -0800792 HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100793 break;
794
795 case HFC_L1_DEACTIVATE_NT:
796 write_reg(hw, HFCUSB_STATES,
Joe Perches475be4d2012-02-19 19:52:38 -0800797 HFCUSB_DO_ACTION);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100798 break;
799 }
800}
801
802/*
803 * Layer 1 B-channel hardware access
804 */
805static int
806channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
807{
Karsten Keil6d1ee482012-05-15 23:51:07 +0000808 return mISDN_ctrl_bchannel(bch, cq);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100809}
810
811/* collect data from incoming interrupt or isochron USB data */
812static void
813hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
Joe Perches475be4d2012-02-19 19:52:38 -0800814 int finish)
Karsten Keil69f52ad2009-01-09 16:20:51 +0100815{
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
823 if (debug & DBG_HFC_CALL_TRACE)
824 printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
Joe Perches475be4d2012-02-19 19:52:38 -0800825 "dch(%p) bch(%p) ech(%p)\n",
826 hw->name, __func__, fifon, len,
827 fifo->dch, fifo->bch, fifo->ech);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100828
829 if (!len)
830 return;
831
832 if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
833 printk(KERN_DEBUG "%s: %s: undefined channel\n",
834 hw->name, __func__);
835 return;
836 }
837
838 spin_lock(&hw->lock);
839 if (fifo->dch) {
840 rx_skb = fifo->dch->rx_skb;
841 maxlen = fifo->dch->maxlen;
842 hdlc = 1;
843 }
844 if (fifo->bch) {
Karsten Keil7206e652012-05-15 23:51:05 +0000845 maxlen = bchannel_get_rxbuf(fifo->bch, len);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100846 rx_skb = fifo->bch->rx_skb;
Karsten Keil7206e652012-05-15 23:51:05 +0000847 if (maxlen < 0) {
848 if (rx_skb)
849 skb_trim(rx_skb, 0);
850 pr_warning("%s.B%d: No bufferspace for %d bytes\n",
851 hw->name, fifo->bch->nr, len);
852 spin_unlock(&hw->lock);
853 return;
854 }
Karsten Keil69f52ad2009-01-09 16:20:51 +0100855 maxlen = fifo->bch->maxlen;
856 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
857 }
858 if (fifo->ech) {
859 rx_skb = fifo->ech->rx_skb;
860 maxlen = fifo->ech->maxlen;
861 hdlc = 1;
862 }
863
Karsten Keil69f52ad2009-01-09 16:20:51 +0100864 if (fifo->dch || fifo->ech) {
Karsten Keil7206e652012-05-15 23:51:05 +0000865 if (!rx_skb) {
866 rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
867 if (rx_skb) {
868 if (fifo->dch)
869 fifo->dch->rx_skb = rx_skb;
870 if (fifo->ech)
871 fifo->ech->rx_skb = rx_skb;
872 skb_trim(rx_skb, 0);
873 } else {
874 printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
875 hw->name, __func__);
876 spin_unlock(&hw->lock);
877 return;
878 }
879 }
Karsten Keil69f52ad2009-01-09 16:20:51 +0100880 /* D/E-Channel SKB range check */
881 if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
882 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
Joe Perches475be4d2012-02-19 19:52:38 -0800883 "for fifo(%d) HFCUSB_D_RX\n",
884 hw->name, __func__, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100885 skb_trim(rx_skb, 0);
886 spin_unlock(&hw->lock);
887 return;
888 }
Karsten Keil69f52ad2009-01-09 16:20:51 +0100889 }
890
891 memcpy(skb_put(rx_skb, len), data, len);
892
893 if (hdlc) {
894 /* we have a complete hdlc packet */
895 if (finish) {
896 if ((rx_skb->len > 3) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800897 (!(rx_skb->data[rx_skb->len - 1]))) {
Karsten Keil69f52ad2009-01-09 16:20:51 +0100898 if (debug & DBG_HFC_FIFO_VERBOSE) {
899 printk(KERN_DEBUG "%s: %s: fifon(%i)"
Joe Perches475be4d2012-02-19 19:52:38 -0800900 " new RX len(%i): ",
901 hw->name, __func__, fifon,
902 rx_skb->len);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100903 i = 0;
904 while (i < rx_skb->len)
905 printk("%02x ",
Joe Perches475be4d2012-02-19 19:52:38 -0800906 rx_skb->data[i++]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100907 printk("\n");
908 }
909
910 /* remove CRC & status */
911 skb_trim(rx_skb, rx_skb->len - 3);
912
913 if (fifo->dch)
914 recv_Dchannel(fifo->dch);
915 if (fifo->bch)
Karsten Keil034005a2012-05-15 23:51:06 +0000916 recv_Bchannel(fifo->bch, MISDN_ID_ANY,
917 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100918 if (fifo->ech)
919 recv_Echannel(fifo->ech,
Joe Perches475be4d2012-02-19 19:52:38 -0800920 &hw->dch);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100921 } else {
922 if (debug & DBG_HFC_FIFO_VERBOSE) {
923 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800924 "%s: CRC or minlen ERROR fifon(%i) "
925 "RX len(%i): ",
926 hw->name, fifon, rx_skb->len);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100927 i = 0;
928 while (i < rx_skb->len)
929 printk("%02x ",
Joe Perches475be4d2012-02-19 19:52:38 -0800930 rx_skb->data[i++]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100931 printk("\n");
932 }
933 skb_trim(rx_skb, 0);
934 }
935 }
936 } else {
937 /* deliver transparent data to layer2 */
Karsten Keil034005a2012-05-15 23:51:06 +0000938 recv_Bchannel(fifo->bch, MISDN_ID_ANY, false);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100939 }
940 spin_unlock(&hw->lock);
941}
942
Hannes Eder6c2959a2009-02-12 09:28:40 +0000943static void
Karsten Keil69f52ad2009-01-09 16:20:51 +0100944fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
945 void *buf, int num_packets, int packet_size, int interval,
946 usb_complete_t complete, void *context)
947{
948 int k;
949
950 usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
Joe Perches475be4d2012-02-19 19:52:38 -0800951 complete, context);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100952
953 urb->number_of_packets = num_packets;
954 urb->transfer_flags = URB_ISO_ASAP;
955 urb->actual_length = 0;
956 urb->interval = interval;
957
958 for (k = 0; k < num_packets; k++) {
959 urb->iso_frame_desc[k].offset = packet_size * k;
960 urb->iso_frame_desc[k].length = packet_size;
961 urb->iso_frame_desc[k].actual_length = 0;
962 }
963}
964
965/* receive completion routine for all ISO tx fifos */
966static void
967rx_iso_complete(struct urb *urb)
968{
969 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
970 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
971 struct hfcsusb *hw = fifo->hw;
972 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
Joe Perches475be4d2012-02-19 19:52:38 -0800973 status, iso_status, i;
Karsten Keil69f52ad2009-01-09 16:20:51 +0100974 __u8 *buf;
975 static __u8 eof[8];
976 __u8 s0_state;
977
978 fifon = fifo->fifonum;
979 status = urb->status;
980
981 spin_lock(&hw->lock);
982 if (fifo->stop_gracefull) {
983 fifo->stop_gracefull = 0;
984 fifo->active = 0;
985 spin_unlock(&hw->lock);
986 return;
987 }
988 spin_unlock(&hw->lock);
989
990 /*
991 * ISO transfer only partially completed,
992 * look at individual frame status for details
993 */
994 if (status == -EXDEV) {
995 if (debug & DEBUG_HW)
996 printk(KERN_DEBUG "%s: %s: with -EXDEV "
Joe Perches475be4d2012-02-19 19:52:38 -0800997 "urb->status %d, fifonum %d\n",
998 hw->name, __func__, status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100999
1000 /* clear status, so go on with ISO transfers */
1001 status = 0;
1002 }
1003
1004 s0_state = 0;
1005 if (fifo->active && !status) {
1006 num_isoc_packets = iso_packets[fifon];
1007 maxlen = fifo->usb_packet_maxlen;
1008
1009 for (k = 0; k < num_isoc_packets; ++k) {
1010 len = urb->iso_frame_desc[k].actual_length;
1011 offset = urb->iso_frame_desc[k].offset;
1012 buf = context_iso_urb->buffer + offset;
1013 iso_status = urb->iso_frame_desc[k].status;
1014
1015 if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
1016 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001017 "ISO packet %i, status: %i\n",
1018 hw->name, __func__, k, iso_status);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001019 }
1020
1021 /* USB data log for every D ISO in */
1022 if ((fifon == HFCUSB_D_RX) &&
1023 (debug & DBG_HFC_USB_VERBOSE)) {
1024 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001025 "%s: %s: %d (%d/%d) len(%d) ",
1026 hw->name, __func__, urb->start_frame,
1027 k, num_isoc_packets - 1,
1028 len);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001029 for (i = 0; i < len; i++)
1030 printk("%x ", buf[i]);
1031 printk("\n");
1032 }
1033
1034 if (!iso_status) {
1035 if (fifo->last_urblen != maxlen) {
1036 /*
1037 * save fifo fill-level threshold bits
1038 * to use them later in TX ISO URB
1039 * completions
1040 */
1041 hw->threshold_mask = buf[1];
1042
1043 if (fifon == HFCUSB_D_RX)
1044 s0_state = (buf[0] >> 4);
1045
1046 eof[fifon] = buf[0] & 1;
1047 if (len > 2)
1048 hfcsusb_rx_frame(fifo, buf + 2,
Joe Perches475be4d2012-02-19 19:52:38 -08001049 len - 2, (len < maxlen)
1050 ? eof[fifon] : 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001051 } else
1052 hfcsusb_rx_frame(fifo, buf, len,
Joe Perches475be4d2012-02-19 19:52:38 -08001053 (len < maxlen) ?
1054 eof[fifon] : 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001055 fifo->last_urblen = len;
1056 }
1057 }
1058
1059 /* signal S0 layer1 state change */
1060 if ((s0_state) && (hw->initdone) &&
1061 (s0_state != hw->dch.state)) {
1062 hw->dch.state = s0_state;
1063 schedule_event(&hw->dch, FLG_PHCHANGE);
1064 }
1065
1066 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1067 context_iso_urb->buffer, num_isoc_packets,
1068 fifo->usb_packet_maxlen, fifo->intervall,
1069 (usb_complete_t)rx_iso_complete, urb->context);
1070 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1071 if (errcode < 0) {
1072 if (debug & DEBUG_HW)
1073 printk(KERN_DEBUG "%s: %s: error submitting "
Joe Perches475be4d2012-02-19 19:52:38 -08001074 "ISO URB: %d\n",
1075 hw->name, __func__, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001076 }
1077 } else {
1078 if (status && (debug & DBG_HFC_URB_INFO))
1079 printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
Joe Perches475be4d2012-02-19 19:52:38 -08001080 "urb->status %d, fifonum %d\n",
1081 hw->name, __func__, status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001082 }
1083}
1084
1085/* receive completion routine for all interrupt rx fifos */
1086static void
1087rx_int_complete(struct urb *urb)
1088{
1089 int len, status, i;
1090 __u8 *buf, maxlen, fifon;
1091 struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
1092 struct hfcsusb *hw = fifo->hw;
1093 static __u8 eof[8];
1094
1095 spin_lock(&hw->lock);
1096 if (fifo->stop_gracefull) {
1097 fifo->stop_gracefull = 0;
1098 fifo->active = 0;
1099 spin_unlock(&hw->lock);
1100 return;
1101 }
1102 spin_unlock(&hw->lock);
1103
1104 fifon = fifo->fifonum;
1105 if ((!fifo->active) || (urb->status)) {
1106 if (debug & DBG_HFC_URB_ERROR)
1107 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001108 "%s: %s: RX-Fifo %i is going down (%i)\n",
1109 hw->name, __func__, fifon, urb->status);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001110
1111 fifo->urb->interval = 0; /* cancel automatic rescheduling */
1112 return;
1113 }
1114 len = urb->actual_length;
1115 buf = fifo->buffer;
1116 maxlen = fifo->usb_packet_maxlen;
1117
1118 /* USB data log for every D INT in */
1119 if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
1120 printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
Joe Perches475be4d2012-02-19 19:52:38 -08001121 hw->name, __func__, len);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001122 for (i = 0; i < len; i++)
1123 printk("%02x ", buf[i]);
1124 printk("\n");
1125 }
1126
1127 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
1128 /* the threshold mask is in the 2nd status byte */
1129 hw->threshold_mask = buf[1];
1130
1131 /* signal S0 layer1 state change */
1132 if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
1133 hw->dch.state = (buf[0] >> 4);
1134 schedule_event(&hw->dch, FLG_PHCHANGE);
1135 }
1136
1137 eof[fifon] = buf[0] & 1;
1138 /* if we have more than the 2 status bytes -> collect data */
1139 if (len > 2)
1140 hfcsusb_rx_frame(fifo, buf + 2,
Joe Perches475be4d2012-02-19 19:52:38 -08001141 urb->actual_length - 2,
1142 (len < maxlen) ? eof[fifon] : 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001143 } else {
1144 hfcsusb_rx_frame(fifo, buf, urb->actual_length,
1145 (len < maxlen) ? eof[fifon] : 0);
1146 }
1147 fifo->last_urblen = urb->actual_length;
1148
1149 status = usb_submit_urb(urb, GFP_ATOMIC);
1150 if (status) {
1151 if (debug & DEBUG_HW)
1152 printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001153 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001154 }
1155}
1156
1157/* transmit completion routine for all ISO tx fifos */
1158static void
1159tx_iso_complete(struct urb *urb)
1160{
1161 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1162 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1163 struct hfcsusb *hw = fifo->hw;
1164 struct sk_buff *tx_skb;
1165 int k, tx_offset, num_isoc_packets, sink, remain, current_len,
Joe Perches475be4d2012-02-19 19:52:38 -08001166 errcode, hdlc, i;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001167 int *tx_idx;
Karsten Keil6d1ee482012-05-15 23:51:07 +00001168 int frame_complete, fifon, status, fillempty = 0;
1169 __u8 threshbit, *p;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001170
1171 spin_lock(&hw->lock);
1172 if (fifo->stop_gracefull) {
1173 fifo->stop_gracefull = 0;
1174 fifo->active = 0;
1175 spin_unlock(&hw->lock);
1176 return;
1177 }
1178
1179 if (fifo->dch) {
1180 tx_skb = fifo->dch->tx_skb;
1181 tx_idx = &fifo->dch->tx_idx;
1182 hdlc = 1;
1183 } else if (fifo->bch) {
1184 tx_skb = fifo->bch->tx_skb;
1185 tx_idx = &fifo->bch->tx_idx;
1186 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
Karsten Keil6d1ee482012-05-15 23:51:07 +00001187 if (!tx_skb && !hdlc &&
1188 test_bit(FLG_FILLEMPTY, &fifo->bch->Flags))
1189 fillempty = 1;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001190 } else {
1191 printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001192 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001193 spin_unlock(&hw->lock);
1194 return;
1195 }
1196
1197 fifon = fifo->fifonum;
1198 status = urb->status;
1199
1200 tx_offset = 0;
1201
1202 /*
1203 * ISO transfer only partially completed,
1204 * look at individual frame status for details
1205 */
1206 if (status == -EXDEV) {
1207 if (debug & DBG_HFC_URB_ERROR)
1208 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001209 "-EXDEV (%i) fifon (%d)\n",
1210 hw->name, __func__, status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001211
1212 /* clear status, so go on with ISO transfers */
1213 status = 0;
1214 }
1215
1216 if (fifo->active && !status) {
1217 /* is FifoFull-threshold set for our channel? */
1218 threshbit = (hw->threshold_mask & (1 << fifon));
1219 num_isoc_packets = iso_packets[fifon];
1220
1221 /* predict dataflow to avoid fifo overflow */
1222 if (fifon >= HFCUSB_D_TX)
1223 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
1224 else
1225 sink = (threshbit) ? SINK_MIN : SINK_MAX;
1226 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1227 context_iso_urb->buffer, num_isoc_packets,
1228 fifo->usb_packet_maxlen, fifo->intervall,
1229 (usb_complete_t)tx_iso_complete, urb->context);
1230 memset(context_iso_urb->buffer, 0,
1231 sizeof(context_iso_urb->buffer));
1232 frame_complete = 0;
1233
1234 for (k = 0; k < num_isoc_packets; ++k) {
1235 /* analyze tx success of previous ISO packets */
1236 if (debug & DBG_HFC_URB_ERROR) {
1237 errcode = urb->iso_frame_desc[k].status;
1238 if (errcode) {
1239 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001240 "ISO packet %i, status: %i\n",
1241 hw->name, __func__, k, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001242 }
1243 }
1244
1245 /* Generate next ISO Packets */
1246 if (tx_skb)
1247 remain = tx_skb->len - *tx_idx;
Karsten Keil6d1ee482012-05-15 23:51:07 +00001248 else if (fillempty)
1249 remain = 15; /* > not complete */
Karsten Keil69f52ad2009-01-09 16:20:51 +01001250 else
1251 remain = 0;
1252
1253 if (remain > 0) {
1254 fifo->bit_line -= sink;
1255 current_len = (0 - fifo->bit_line) / 8;
1256 if (current_len > 14)
1257 current_len = 14;
1258 if (current_len < 0)
1259 current_len = 0;
1260 if (remain < current_len)
1261 current_len = remain;
1262
1263 /* how much bit do we put on the line? */
1264 fifo->bit_line += current_len * 8;
1265
1266 context_iso_urb->buffer[tx_offset] = 0;
1267 if (current_len == remain) {
1268 if (hdlc) {
1269 /* signal frame completion */
1270 context_iso_urb->
Joe Perches475be4d2012-02-19 19:52:38 -08001271 buffer[tx_offset] = 1;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001272 /* add 2 byte flags and 16bit
1273 * CRC at end of ISDN frame */
1274 fifo->bit_line += 32;
1275 }
1276 frame_complete = 1;
1277 }
1278
1279 /* copy tx data to iso-urb buffer */
Karsten Keil6d1ee482012-05-15 23:51:07 +00001280 p = context_iso_urb->buffer + tx_offset + 1;
1281 if (fillempty) {
1282 memset(p, fifo->bch->fill[0],
1283 current_len);
1284 } else {
1285 memcpy(p, (tx_skb->data + *tx_idx),
1286 current_len);
1287 *tx_idx += current_len;
1288 }
Karsten Keil69f52ad2009-01-09 16:20:51 +01001289 urb->iso_frame_desc[k].offset = tx_offset;
1290 urb->iso_frame_desc[k].length = current_len + 1;
1291
1292 /* USB data log for every D ISO out */
Karsten Keil6d1ee482012-05-15 23:51:07 +00001293 if ((fifon == HFCUSB_D_RX) && !fillempty &&
Karsten Keil69f52ad2009-01-09 16:20:51 +01001294 (debug & DBG_HFC_USB_VERBOSE)) {
1295 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001296 "%s: %s (%d/%d) offs(%d) len(%d) ",
1297 hw->name, __func__,
1298 k, num_isoc_packets - 1,
1299 urb->iso_frame_desc[k].offset,
1300 urb->iso_frame_desc[k].length);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001301
1302 for (i = urb->iso_frame_desc[k].offset;
1303 i < (urb->iso_frame_desc[k].offset
Joe Perches475be4d2012-02-19 19:52:38 -08001304 + urb->iso_frame_desc[k].length);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001305 i++)
1306 printk("%x ",
Joe Perches475be4d2012-02-19 19:52:38 -08001307 context_iso_urb->buffer[i]);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001308
1309 printk(" skb->len(%i) tx-idx(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001310 tx_skb->len, *tx_idx);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001311 }
1312
1313 tx_offset += (current_len + 1);
1314 } else {
1315 urb->iso_frame_desc[k].offset = tx_offset++;
1316 urb->iso_frame_desc[k].length = 1;
1317 /* we lower data margin every msec */
1318 fifo->bit_line -= sink;
1319 if (fifo->bit_line < BITLINE_INF)
1320 fifo->bit_line = BITLINE_INF;
1321 }
1322
1323 if (frame_complete) {
1324 frame_complete = 0;
1325
1326 if (debug & DBG_HFC_FIFO_VERBOSE) {
1327 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001328 "fifon(%i) new TX len(%i): ",
1329 hw->name, __func__,
1330 fifon, tx_skb->len);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001331 i = 0;
1332 while (i < tx_skb->len)
1333 printk("%02x ",
Joe Perches475be4d2012-02-19 19:52:38 -08001334 tx_skb->data[i++]);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001335 printk("\n");
1336 }
1337
1338 dev_kfree_skb(tx_skb);
1339 tx_skb = NULL;
1340 if (fifo->dch && get_next_dframe(fifo->dch))
1341 tx_skb = fifo->dch->tx_skb;
1342 else if (fifo->bch &&
Karsten Keil8bfddfb2012-05-15 23:51:02 +00001343 get_next_bframe(fifo->bch))
Karsten Keil69f52ad2009-01-09 16:20:51 +01001344 tx_skb = fifo->bch->tx_skb;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001345 }
1346 }
1347 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1348 if (errcode < 0) {
1349 if (debug & DEBUG_HW)
1350 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001351 "%s: %s: error submitting ISO URB: %d \n",
1352 hw->name, __func__, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001353 }
1354
1355 /*
1356 * abuse DChannel tx iso completion to trigger NT mode state
1357 * changes tx_iso_complete is assumed to be called every
1358 * fifo->intervall (ms)
1359 */
1360 if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
1361 && (hw->timers & NT_ACTIVATION_TIMER)) {
1362 if ((--hw->nt_timer) < 0)
1363 schedule_event(&hw->dch, FLG_PHCHANGE);
1364 }
1365
1366 } else {
1367 if (status && (debug & DBG_HFC_URB_ERROR))
1368 printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
Joe Perches475be4d2012-02-19 19:52:38 -08001369 "fifonum=%d\n",
1370 hw->name, __func__,
1371 symbolic(urb_errlist, status), status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001372 }
1373 spin_unlock(&hw->lock);
1374}
1375
1376/*
1377 * allocs urbs and start isoc transfer with two pending urbs to avoid
1378 * gaps in the transfer chain
1379 */
1380static int
1381start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
1382 usb_complete_t complete, int packet_size)
1383{
1384 struct hfcsusb *hw = fifo->hw;
1385 int i, k, errcode;
1386
1387 if (debug)
1388 printk(KERN_DEBUG "%s: %s: fifo %i\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001389 hw->name, __func__, fifo->fifonum);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001390
1391 /* allocate Memory for Iso out Urbs */
1392 for (i = 0; i < 2; i++) {
1393 if (!(fifo->iso[i].urb)) {
1394 fifo->iso[i].urb =
Joe Perches475be4d2012-02-19 19:52:38 -08001395 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001396 if (!(fifo->iso[i].urb)) {
1397 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001398 "%s: %s: alloc urb for fifo %i failed",
1399 hw->name, __func__, fifo->fifonum);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001400 }
1401 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
1402 fifo->iso[i].indx = i;
1403
1404 /* Init the first iso */
1405 if (ISO_BUFFER_SIZE >=
1406 (fifo->usb_packet_maxlen *
1407 num_packets_per_urb)) {
1408 fill_isoc_urb(fifo->iso[i].urb,
Joe Perches475be4d2012-02-19 19:52:38 -08001409 fifo->hw->dev, fifo->pipe,
1410 fifo->iso[i].buffer,
1411 num_packets_per_urb,
1412 fifo->usb_packet_maxlen,
1413 fifo->intervall, complete,
1414 &fifo->iso[i]);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001415 memset(fifo->iso[i].buffer, 0,
1416 sizeof(fifo->iso[i].buffer));
1417
1418 for (k = 0; k < num_packets_per_urb; k++) {
1419 fifo->iso[i].urb->
Joe Perches475be4d2012-02-19 19:52:38 -08001420 iso_frame_desc[k].offset =
1421 k * packet_size;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001422 fifo->iso[i].urb->
Joe Perches475be4d2012-02-19 19:52:38 -08001423 iso_frame_desc[k].length =
1424 packet_size;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001425 }
1426 } else {
1427 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001428 "%s: %s: ISO Buffer size to small!\n",
1429 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001430 }
1431 }
1432 fifo->bit_line = BITLINE_INF;
1433
1434 errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
1435 fifo->active = (errcode >= 0) ? 1 : 0;
1436 fifo->stop_gracefull = 0;
1437 if (errcode < 0) {
1438 printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001439 hw->name, __func__,
1440 symbolic(urb_errlist, errcode), i);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001441 }
1442 }
1443 return fifo->active;
1444}
1445
1446static void
1447stop_iso_gracefull(struct usb_fifo *fifo)
1448{
1449 struct hfcsusb *hw = fifo->hw;
1450 int i, timeout;
1451 u_long flags;
1452
1453 for (i = 0; i < 2; i++) {
1454 spin_lock_irqsave(&hw->lock, flags);
1455 if (debug)
1456 printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
1457 hw->name, __func__, fifo->fifonum, i);
1458 fifo->stop_gracefull = 1;
1459 spin_unlock_irqrestore(&hw->lock, flags);
1460 }
1461
1462 for (i = 0; i < 2; i++) {
1463 timeout = 3;
1464 while (fifo->stop_gracefull && timeout--)
Joe Perches475be4d2012-02-19 19:52:38 -08001465 schedule_timeout_interruptible((HZ / 1000) * 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001466 if (debug && fifo->stop_gracefull)
1467 printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001468 hw->name, __func__, fifo->fifonum, i);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001469 }
1470}
1471
1472static void
1473stop_int_gracefull(struct usb_fifo *fifo)
1474{
1475 struct hfcsusb *hw = fifo->hw;
1476 int timeout;
1477 u_long flags;
1478
1479 spin_lock_irqsave(&hw->lock, flags);
1480 if (debug)
1481 printk(KERN_DEBUG "%s: %s for fifo %i\n",
1482 hw->name, __func__, fifo->fifonum);
1483 fifo->stop_gracefull = 1;
1484 spin_unlock_irqrestore(&hw->lock, flags);
1485
1486 timeout = 3;
1487 while (fifo->stop_gracefull && timeout--)
Joe Perches475be4d2012-02-19 19:52:38 -08001488 schedule_timeout_interruptible((HZ / 1000) * 3);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001489 if (debug && fifo->stop_gracefull)
1490 printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
1491 hw->name, __func__, fifo->fifonum);
1492}
1493
1494/* start the interrupt transfer for the given fifo */
1495static void
1496start_int_fifo(struct usb_fifo *fifo)
1497{
1498 struct hfcsusb *hw = fifo->hw;
1499 int errcode;
1500
1501 if (debug)
1502 printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001503 hw->name, __func__, fifo->fifonum);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001504
1505 if (!fifo->urb) {
1506 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
1507 if (!fifo->urb)
1508 return;
1509 }
1510 usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
Joe Perches475be4d2012-02-19 19:52:38 -08001511 fifo->buffer, fifo->usb_packet_maxlen,
1512 (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001513 fifo->active = 1;
1514 fifo->stop_gracefull = 0;
1515 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
1516 if (errcode) {
1517 printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001518 hw->name, __func__, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001519 fifo->active = 0;
1520 }
1521}
1522
1523static void
1524setPortMode(struct hfcsusb *hw)
1525{
1526 if (debug & DEBUG_HW)
1527 printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
Joe Perches475be4d2012-02-19 19:52:38 -08001528 (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
Karsten Keil69f52ad2009-01-09 16:20:51 +01001529
1530 if (hw->protocol == ISDN_P_TE_S0) {
1531 write_reg(hw, HFCUSB_SCTRL, 0x40);
1532 write_reg(hw, HFCUSB_SCTRL_E, 0x00);
1533 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
1534 write_reg(hw, HFCUSB_STATES, 3 | 0x10);
1535 write_reg(hw, HFCUSB_STATES, 3);
1536 } else {
1537 write_reg(hw, HFCUSB_SCTRL, 0x44);
1538 write_reg(hw, HFCUSB_SCTRL_E, 0x09);
1539 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
1540 write_reg(hw, HFCUSB_STATES, 1 | 0x10);
1541 write_reg(hw, HFCUSB_STATES, 1);
1542 }
1543}
1544
1545static void
1546reset_hfcsusb(struct hfcsusb *hw)
1547{
1548 struct usb_fifo *fifo;
1549 int i;
1550
1551 if (debug & DEBUG_HW)
1552 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1553
1554 /* do Chip reset */
1555 write_reg(hw, HFCUSB_CIRM, 8);
1556
1557 /* aux = output, reset off */
1558 write_reg(hw, HFCUSB_CIRM, 0x10);
1559
1560 /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
1561 write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
Joe Perches475be4d2012-02-19 19:52:38 -08001562 ((hw->packet_size / 8) << 4));
Karsten Keil69f52ad2009-01-09 16:20:51 +01001563
1564 /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
1565 write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
1566
1567 /* enable PCM/GCI master mode */
1568 write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
1569 write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
1570
1571 /* init the fifos */
1572 write_reg(hw, HFCUSB_F_THRES,
Joe Perches475be4d2012-02-19 19:52:38 -08001573 (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
Karsten Keil69f52ad2009-01-09 16:20:51 +01001574
1575 fifo = hw->fifos;
1576 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1577 write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
1578 fifo[i].max_size =
Joe Perches475be4d2012-02-19 19:52:38 -08001579 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001580 fifo[i].last_urblen = 0;
1581
1582 /* set 2 bit for D- & E-channel */
1583 write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
1584
1585 /* enable all fifos */
1586 if (i == HFCUSB_D_TX)
1587 write_reg(hw, HFCUSB_CON_HDLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001588 (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001589 else
1590 write_reg(hw, HFCUSB_CON_HDLC, 0x08);
1591 write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1592 }
1593
1594 write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1595 handle_led(hw, LED_POWER_ON);
1596}
1597
1598/* start USB data pipes dependand on device's endpoint configuration */
1599static void
1600hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
1601{
1602 /* quick check if endpoint already running */
1603 if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
1604 return;
1605 if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
1606 return;
1607 if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
1608 return;
1609 if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
1610 return;
1611
1612 /* start rx endpoints using USB INT IN method */
1613 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
Joe Perches475be4d2012-02-19 19:52:38 -08001614 start_int_fifo(hw->fifos + channel * 2 + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001615
1616 /* start rx endpoints using USB ISO IN method */
1617 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
1618 switch (channel) {
1619 case HFC_CHAN_D:
1620 start_isoc_chain(hw->fifos + HFCUSB_D_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001621 ISOC_PACKETS_D,
1622 (usb_complete_t)rx_iso_complete,
1623 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001624 break;
1625 case HFC_CHAN_E:
1626 start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001627 ISOC_PACKETS_D,
1628 (usb_complete_t)rx_iso_complete,
1629 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001630 break;
1631 case HFC_CHAN_B1:
1632 start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001633 ISOC_PACKETS_B,
1634 (usb_complete_t)rx_iso_complete,
1635 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001636 break;
1637 case HFC_CHAN_B2:
1638 start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001639 ISOC_PACKETS_B,
1640 (usb_complete_t)rx_iso_complete,
1641 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001642 break;
1643 }
1644 }
1645
1646 /* start tx endpoints using USB ISO OUT method */
1647 switch (channel) {
1648 case HFC_CHAN_D:
1649 start_isoc_chain(hw->fifos + HFCUSB_D_TX,
Joe Perches475be4d2012-02-19 19:52:38 -08001650 ISOC_PACKETS_B,
1651 (usb_complete_t)tx_iso_complete, 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001652 break;
1653 case HFC_CHAN_B1:
1654 start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
Joe Perches475be4d2012-02-19 19:52:38 -08001655 ISOC_PACKETS_D,
1656 (usb_complete_t)tx_iso_complete, 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001657 break;
1658 case HFC_CHAN_B2:
1659 start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
Joe Perches475be4d2012-02-19 19:52:38 -08001660 ISOC_PACKETS_B,
1661 (usb_complete_t)tx_iso_complete, 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001662 break;
1663 }
1664}
1665
1666/* stop USB data pipes dependand on device's endpoint configuration */
1667static void
1668hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
1669{
1670 /* quick check if endpoint currently running */
1671 if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
1672 return;
1673 if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
1674 return;
1675 if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
1676 return;
1677 if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
1678 return;
1679
1680 /* rx endpoints using USB INT IN method */
1681 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
Joe Perches475be4d2012-02-19 19:52:38 -08001682 stop_int_gracefull(hw->fifos + channel * 2 + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001683
1684 /* rx endpoints using USB ISO IN method */
1685 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
Joe Perches475be4d2012-02-19 19:52:38 -08001686 stop_iso_gracefull(hw->fifos + channel * 2 + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001687
1688 /* tx endpoints using USB ISO OUT method */
1689 if (channel != HFC_CHAN_E)
Joe Perches475be4d2012-02-19 19:52:38 -08001690 stop_iso_gracefull(hw->fifos + channel * 2);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001691}
1692
1693
1694/* Hardware Initialization */
Hannes Eder6c2959a2009-02-12 09:28:40 +00001695static int
Karsten Keil69f52ad2009-01-09 16:20:51 +01001696setup_hfcsusb(struct hfcsusb *hw)
1697{
Karsten Keil69f52ad2009-01-09 16:20:51 +01001698 u_char b;
1699
1700 if (debug & DBG_HFC_CALL_TRACE)
1701 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1702
1703 /* check the chip id */
1704 if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
1705 printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001706 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001707 return 1;
1708 }
1709 if (b != HFCUSB_CHIPID) {
1710 printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001711 hw->name, __func__, b);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001712 return 1;
1713 }
1714
1715 /* first set the needed config, interface and alternate */
David S. Miller3c76c582011-04-17 16:35:27 -07001716 (void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001717
1718 hw->led_state = 0;
1719
1720 /* init the background machinery for control requests */
1721 hw->ctrl_read.bRequestType = 0xc0;
1722 hw->ctrl_read.bRequest = 1;
1723 hw->ctrl_read.wLength = cpu_to_le16(1);
1724 hw->ctrl_write.bRequestType = 0x40;
1725 hw->ctrl_write.bRequest = 0;
1726 hw->ctrl_write.wLength = 0;
1727 usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
Joe Perches475be4d2012-02-19 19:52:38 -08001728 (u_char *)&hw->ctrl_write, NULL, 0,
1729 (usb_complete_t)ctrl_complete, hw);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001730
1731 reset_hfcsusb(hw);
1732 return 0;
1733}
1734
1735static void
1736release_hw(struct hfcsusb *hw)
1737{
1738 if (debug & DBG_HFC_CALL_TRACE)
1739 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1740
1741 /*
1742 * stop all endpoints gracefully
1743 * TODO: mISDN_core should generate CLOSE_CHANNEL
1744 * signals after calling mISDN_unregister_device()
1745 */
1746 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
1747 hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
1748 hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
1749 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1750 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
1751 if (hw->protocol == ISDN_P_TE_S0)
1752 l1_event(hw->dch.l1, CLOSE_CHANNEL);
1753
1754 mISDN_unregister_device(&hw->dch.dev);
1755 mISDN_freebchannel(&hw->bch[1]);
1756 mISDN_freebchannel(&hw->bch[0]);
1757 mISDN_freedchannel(&hw->dch);
1758
1759 if (hw->ctrl_urb) {
1760 usb_kill_urb(hw->ctrl_urb);
1761 usb_free_urb(hw->ctrl_urb);
1762 hw->ctrl_urb = NULL;
1763 }
1764
1765 if (hw->intf)
1766 usb_set_intfdata(hw->intf, NULL);
1767 list_del(&hw->list);
1768 kfree(hw);
1769 hw = NULL;
1770}
1771
1772static void
1773deactivate_bchannel(struct bchannel *bch)
1774{
1775 struct hfcsusb *hw = bch->hw;
1776 u_long flags;
1777
1778 if (bch->debug & DEBUG_HW)
1779 printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001780 hw->name, __func__, bch->nr);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001781
1782 spin_lock_irqsave(&hw->lock, flags);
Karsten Keilfb286f02009-07-09 10:02:29 +02001783 mISDN_clear_bchannel(bch);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001784 spin_unlock_irqrestore(&hw->lock, flags);
1785 hfcsusb_setup_bch(bch, ISDN_P_NONE);
Martin Bachem37952cf2012-05-15 23:51:04 +00001786 hfcsusb_stop_endpoint(hw, bch->nr - 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001787}
1788
1789/*
1790 * Layer 1 B-channel hardware access
1791 */
1792static int
1793hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1794{
1795 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1796 int ret = -EINVAL;
1797
1798 if (bch->debug & DEBUG_HW)
1799 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1800
1801 switch (cmd) {
1802 case HW_TESTRX_RAW:
1803 case HW_TESTRX_HDLC:
1804 case HW_TESTRX_OFF:
1805 ret = -EINVAL;
1806 break;
1807
1808 case CLOSE_CHANNEL:
1809 test_and_clear_bit(FLG_OPEN, &bch->Flags);
Karsten Keil13681122012-05-15 23:51:01 +00001810 deactivate_bchannel(bch);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001811 ch->protocol = ISDN_P_NONE;
1812 ch->peer = NULL;
1813 module_put(THIS_MODULE);
1814 ret = 0;
1815 break;
1816 case CONTROL_CHANNEL:
1817 ret = channel_bctrl(bch, arg);
1818 break;
1819 default:
1820 printk(KERN_WARNING "%s: unknown prim(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001821 __func__, cmd);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001822 }
1823 return ret;
1824}
1825
1826static int
1827setup_instance(struct hfcsusb *hw, struct device *parent)
1828{
1829 u_long flags;
1830 int err, i;
1831
1832 if (debug & DBG_HFC_CALL_TRACE)
1833 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1834
1835 spin_lock_init(&hw->ctrl_lock);
1836 spin_lock_init(&hw->lock);
1837
1838 mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
1839 hw->dch.debug = debug & 0xFFFF;
1840 hw->dch.hw = hw;
1841 hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1842 hw->dch.dev.D.send = hfcusb_l2l1D;
1843 hw->dch.dev.D.ctrl = hfc_dctrl;
1844
1845 /* enable E-Channel logging */
1846 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1847 mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
1848
1849 hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
Joe Perches475be4d2012-02-19 19:52:38 -08001850 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
Karsten Keil69f52ad2009-01-09 16:20:51 +01001851 hw->dch.dev.nrbchan = 2;
1852 for (i = 0; i < 2; i++) {
1853 hw->bch[i].nr = i + 1;
1854 set_channelmap(i + 1, hw->dch.dev.channelmap);
1855 hw->bch[i].debug = debug;
Karsten Keil034005a2012-05-15 23:51:06 +00001856 mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM, poll >> 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001857 hw->bch[i].hw = hw;
1858 hw->bch[i].ch.send = hfcusb_l2l1B;
1859 hw->bch[i].ch.ctrl = hfc_bctrl;
1860 hw->bch[i].ch.nr = i + 1;
1861 list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
1862 }
1863
1864 hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
1865 hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
1866 hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
1867 hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
1868 hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
1869 hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
1870 hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
1871 hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
1872
1873 err = setup_hfcsusb(hw);
1874 if (err)
1875 goto out;
1876
1877 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
Joe Perches475be4d2012-02-19 19:52:38 -08001878 hfcsusb_cnt + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001879 printk(KERN_INFO "%s: registered as '%s'\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001880 DRIVER_NAME, hw->name);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001881
1882 err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
1883 if (err)
1884 goto out;
1885
1886 hfcsusb_cnt++;
1887 write_lock_irqsave(&HFClock, flags);
1888 list_add_tail(&hw->list, &HFClist);
1889 write_unlock_irqrestore(&HFClock, flags);
1890 return 0;
1891
1892out:
1893 mISDN_freebchannel(&hw->bch[1]);
1894 mISDN_freebchannel(&hw->bch[0]);
1895 mISDN_freedchannel(&hw->dch);
1896 kfree(hw);
1897 return err;
1898}
1899
1900static int
1901hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1902{
1903 struct hfcsusb *hw;
1904 struct usb_device *dev = interface_to_usbdev(intf);
1905 struct usb_host_interface *iface = intf->cur_altsetting;
1906 struct usb_host_interface *iface_used = NULL;
1907 struct usb_host_endpoint *ep;
1908 struct hfcsusb_vdata *driver_info;
1909 int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
Joe Perches475be4d2012-02-19 19:52:38 -08001910 probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
1911 ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
1912 alt_used = 0;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001913
1914 vend_idx = 0xffff;
1915 for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
1916 if ((le16_to_cpu(dev->descriptor.idVendor)
Joe Perches475be4d2012-02-19 19:52:38 -08001917 == hfcsusb_idtab[i].idVendor) &&
Karsten Keil69f52ad2009-01-09 16:20:51 +01001918 (le16_to_cpu(dev->descriptor.idProduct)
Joe Perches475be4d2012-02-19 19:52:38 -08001919 == hfcsusb_idtab[i].idProduct)) {
Karsten Keil69f52ad2009-01-09 16:20:51 +01001920 vend_idx = i;
1921 continue;
1922 }
1923 }
1924
1925 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001926 "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
1927 __func__, ifnum, iface->desc.bAlternateSetting,
1928 intf->minor, vend_idx);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001929
1930 if (vend_idx == 0xffff) {
1931 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001932 "%s: no valid vendor found in USB descriptor\n",
1933 __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001934 return -EIO;
1935 }
1936 /* if vendor and product ID is OK, start probing alternate settings */
1937 alt_idx = 0;
1938 small_match = -1;
1939
1940 /* default settings */
1941 iso_packet_size = 16;
1942 packet_size = 64;
1943
1944 while (alt_idx < intf->num_altsetting) {
1945 iface = intf->altsetting + alt_idx;
1946 probe_alt_setting = iface->desc.bAlternateSetting;
1947 cfg_used = 0;
1948
1949 while (validconf[cfg_used][0]) {
1950 cfg_found = 1;
1951 vcf = validconf[cfg_used];
1952 ep = iface->endpoint;
1953 memcpy(cmptbl, vcf, 16 * sizeof(int));
1954
1955 /* check for all endpoints in this alternate setting */
1956 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1957 ep_addr = ep->desc.bEndpointAddress;
1958
1959 /* get endpoint base */
1960 idx = ((ep_addr & 0x7f) - 1) * 2;
1961 if (ep_addr & 0x80)
1962 idx++;
1963 attr = ep->desc.bmAttributes;
1964
1965 if (cmptbl[idx] != EP_NOP) {
1966 if (cmptbl[idx] == EP_NUL)
1967 cfg_found = 0;
1968 if (attr == USB_ENDPOINT_XFER_INT
Joe Perches475be4d2012-02-19 19:52:38 -08001969 && cmptbl[idx] == EP_INT)
Karsten Keil69f52ad2009-01-09 16:20:51 +01001970 cmptbl[idx] = EP_NUL;
1971 if (attr == USB_ENDPOINT_XFER_BULK
Joe Perches475be4d2012-02-19 19:52:38 -08001972 && cmptbl[idx] == EP_BLK)
Karsten Keil69f52ad2009-01-09 16:20:51 +01001973 cmptbl[idx] = EP_NUL;
1974 if (attr == USB_ENDPOINT_XFER_ISOC
Joe Perches475be4d2012-02-19 19:52:38 -08001975 && cmptbl[idx] == EP_ISO)
Karsten Keil69f52ad2009-01-09 16:20:51 +01001976 cmptbl[idx] = EP_NUL;
1977
1978 if (attr == USB_ENDPOINT_XFER_INT &&
Joe Perches475be4d2012-02-19 19:52:38 -08001979 ep->desc.bInterval < vcf[17]) {
Karsten Keil69f52ad2009-01-09 16:20:51 +01001980 cfg_found = 0;
1981 }
1982 }
1983 ep++;
1984 }
1985
1986 for (i = 0; i < 16; i++)
1987 if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
1988 cfg_found = 0;
1989
1990 if (cfg_found) {
1991 if (small_match < cfg_used) {
1992 small_match = cfg_used;
1993 alt_used = probe_alt_setting;
1994 iface_used = iface;
1995 }
1996 }
1997 cfg_used++;
1998 }
1999 alt_idx++;
2000 } /* (alt_idx < intf->num_altsetting) */
2001
2002 /* not found a valid USB Ta Endpoint config */
2003 if (small_match == -1)
2004 return -EIO;
2005
2006 iface = iface_used;
2007 hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
2008 if (!hw)
2009 return -ENOMEM; /* got no mem */
2010 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
2011
2012 ep = iface->endpoint;
2013 vcf = validconf[small_match];
2014
2015 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2016 struct usb_fifo *f;
2017
2018 ep_addr = ep->desc.bEndpointAddress;
2019 /* get endpoint base */
2020 idx = ((ep_addr & 0x7f) - 1) * 2;
2021 if (ep_addr & 0x80)
2022 idx++;
2023 f = &hw->fifos[idx & 7];
2024
2025 /* init Endpoints */
2026 if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
2027 ep++;
2028 continue;
2029 }
2030 switch (ep->desc.bmAttributes) {
2031 case USB_ENDPOINT_XFER_INT:
2032 f->pipe = usb_rcvintpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002033 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002034 f->usb_transfer_mode = USB_INT;
2035 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2036 break;
2037 case USB_ENDPOINT_XFER_BULK:
2038 if (ep_addr & 0x80)
2039 f->pipe = usb_rcvbulkpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002040 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002041 else
2042 f->pipe = usb_sndbulkpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002043 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002044 f->usb_transfer_mode = USB_BULK;
2045 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2046 break;
2047 case USB_ENDPOINT_XFER_ISOC:
2048 if (ep_addr & 0x80)
2049 f->pipe = usb_rcvisocpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002050 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002051 else
2052 f->pipe = usb_sndisocpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002053 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002054 f->usb_transfer_mode = USB_ISOC;
2055 iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2056 break;
2057 default:
2058 f->pipe = 0;
2059 }
2060
2061 if (f->pipe) {
2062 f->fifonum = idx & 7;
2063 f->hw = hw;
2064 f->usb_packet_maxlen =
Joe Perches475be4d2012-02-19 19:52:38 -08002065 le16_to_cpu(ep->desc.wMaxPacketSize);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002066 f->intervall = ep->desc.bInterval;
2067 }
2068 ep++;
2069 }
2070 hw->dev = dev; /* save device */
2071 hw->if_used = ifnum; /* save used interface */
2072 hw->alt_used = alt_used; /* and alternate config */
2073 hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
2074 hw->cfg_used = vcf[16]; /* store used config */
2075 hw->vend_idx = vend_idx; /* store found vendor */
2076 hw->packet_size = packet_size;
2077 hw->iso_packet_size = iso_packet_size;
2078
2079 /* create the control pipes needed for register access */
2080 hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
2081 hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
2082 hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
2083
2084 driver_info =
2085 (struct hfcsusb_vdata *)hfcsusb_idtab[vend_idx].driver_info;
2086 printk(KERN_DEBUG "%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002087 hw->name, __func__, driver_info->vend_name,
2088 conf_str[small_match], ifnum, alt_used);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002089
2090 if (setup_instance(hw, dev->dev.parent))
2091 return -EIO;
2092
2093 hw->intf = intf;
2094 usb_set_intfdata(hw->intf, hw);
2095 return 0;
2096}
2097
2098/* function called when an active device is removed */
2099static void
2100hfcsusb_disconnect(struct usb_interface *intf)
2101{
2102 struct hfcsusb *hw = usb_get_intfdata(intf);
2103 struct hfcsusb *next;
2104 int cnt = 0;
2105
2106 printk(KERN_INFO "%s: device disconnected\n", hw->name);
2107
2108 handle_led(hw, LED_POWER_OFF);
2109 release_hw(hw);
2110
2111 list_for_each_entry_safe(hw, next, &HFClist, list)
2112 cnt++;
2113 if (!cnt)
2114 hfcsusb_cnt = 0;
2115
2116 usb_set_intfdata(intf, NULL);
2117}
2118
2119static struct usb_driver hfcsusb_drv = {
2120 .name = DRIVER_NAME,
2121 .id_table = hfcsusb_idtab,
2122 .probe = hfcsusb_probe,
2123 .disconnect = hfcsusb_disconnect,
2124};
2125
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002126module_usb_driver(hfcsusb_drv);