blob: a4c5b24b2266e580df31bc3d37cfa4b4f5bc58d4 [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)) {
234 hfcsusb_start_endpoint(hw, bch->nr);
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,
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 */
494 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
495 bch->ch.protocol = rq->protocol;
496 rq->ch = &bch->ch;
497
498 /* start USB endpoint for bchannel */
499 if (rq->adr.channel == 1)
500 hfcsusb_start_endpoint(hw, HFC_CHAN_B1);
501 else
502 hfcsusb_start_endpoint(hw, HFC_CHAN_B2);
503
504 if (!try_module_get(THIS_MODULE))
505 printk(KERN_WARNING "%s: %s:cannot get module\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800506 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100507 return 0;
508}
509
510static int
511channel_ctrl(struct hfcsusb *hw, struct mISDN_ctrl_req *cq)
512{
513 int ret = 0;
514
515 if (debug & DBG_HFC_CALL_TRACE)
516 printk(KERN_DEBUG "%s: %s op(0x%x) channel(0x%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800517 hw->name, __func__, (cq->op), (cq->channel));
Karsten Keil69f52ad2009-01-09 16:20:51 +0100518
519 switch (cq->op) {
520 case MISDN_CTRL_GETOP:
521 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
Joe Perches475be4d2012-02-19 19:52:38 -0800522 MISDN_CTRL_DISCONNECT;
Karsten Keil69f52ad2009-01-09 16:20:51 +0100523 break;
524 default:
525 printk(KERN_WARNING "%s: %s: unknown Op %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800526 hw->name, __func__, cq->op);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100527 ret = -EINVAL;
528 break;
529 }
530 return ret;
531}
532
533/*
534 * device control function
535 */
536static int
537hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
538{
539 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
540 struct dchannel *dch = container_of(dev, struct dchannel, dev);
541 struct hfcsusb *hw = dch->hw;
542 struct channel_req *rq;
543 int err = 0;
544
545 if (dch->debug & DEBUG_HW)
546 printk(KERN_DEBUG "%s: %s: cmd:%x %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800547 hw->name, __func__, cmd, arg);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100548 switch (cmd) {
549 case OPEN_CHANNEL:
550 rq = arg;
551 if ((rq->protocol == ISDN_P_TE_S0) ||
552 (rq->protocol == ISDN_P_NT_S0))
553 err = open_dchannel(hw, ch, rq);
554 else
555 err = open_bchannel(hw, rq);
556 if (!err)
557 hw->open++;
558 break;
559 case CLOSE_CHANNEL:
560 hw->open--;
561 if (debug & DEBUG_HW_OPEN)
562 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800563 "%s: %s: dev(%d) close from %p (open %d)\n",
564 hw->name, __func__, hw->dch.dev.id,
565 __builtin_return_address(0), hw->open);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100566 if (!hw->open) {
567 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
568 if (hw->fifos[HFCUSB_PCM_RX].pipe)
569 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
570 handle_led(hw, LED_POWER_ON);
571 }
572 module_put(THIS_MODULE);
573 break;
574 case CONTROL_CHANNEL:
575 err = channel_ctrl(hw, arg);
576 break;
577 default:
578 if (dch->debug & DEBUG_HW)
579 printk(KERN_DEBUG "%s: %s: unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800580 hw->name, __func__, cmd);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100581 return -EINVAL;
582 }
583 return err;
584}
585
586/*
587 * S0 TE state change event handler
588 */
589static void
590ph_state_te(struct dchannel *dch)
591{
592 struct hfcsusb *hw = dch->hw;
593
594 if (debug & DEBUG_HW) {
595 if (dch->state <= HFC_MAX_TE_LAYER1_STATE)
596 printk(KERN_DEBUG "%s: %s: %s\n", hw->name, __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800597 HFC_TE_LAYER1_STATES[dch->state]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100598 else
599 printk(KERN_DEBUG "%s: %s: TE F%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800600 hw->name, __func__, dch->state);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100601 }
602
603 switch (dch->state) {
604 case 0:
605 l1_event(dch->l1, HW_RESET_IND);
606 break;
607 case 3:
608 l1_event(dch->l1, HW_DEACT_IND);
609 break;
610 case 5:
611 case 8:
612 l1_event(dch->l1, ANYSIGNAL);
613 break;
614 case 6:
615 l1_event(dch->l1, INFO2);
616 break;
617 case 7:
618 l1_event(dch->l1, INFO4_P8);
619 break;
620 }
621 if (dch->state == 7)
622 handle_led(hw, LED_S0_ON);
623 else
624 handle_led(hw, LED_S0_OFF);
625}
626
627/*
628 * S0 NT state change event handler
629 */
630static void
631ph_state_nt(struct dchannel *dch)
632{
633 struct hfcsusb *hw = dch->hw;
634
635 if (debug & DEBUG_HW) {
636 if (dch->state <= HFC_MAX_NT_LAYER1_STATE)
637 printk(KERN_DEBUG "%s: %s: %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800638 hw->name, __func__,
639 HFC_NT_LAYER1_STATES[dch->state]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100640
641 else
642 printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800643 hw->name, __func__, dch->state);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100644 }
645
646 switch (dch->state) {
647 case (1):
648 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
649 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
650 hw->nt_timer = 0;
651 hw->timers &= ~NT_ACTIVATION_TIMER;
652 handle_led(hw, LED_S0_OFF);
653 break;
654
655 case (2):
656 if (hw->nt_timer < 0) {
657 hw->nt_timer = 0;
658 hw->timers &= ~NT_ACTIVATION_TIMER;
659 hfcsusb_ph_command(dch->hw, HFC_L1_DEACTIVATE_NT);
660 } else {
661 hw->timers |= NT_ACTIVATION_TIMER;
662 hw->nt_timer = NT_T1_COUNT;
663 /* allow G2 -> G3 transition */
664 write_reg(hw, HFCUSB_STATES, 2 | HFCUSB_NT_G2_G3);
665 }
666 break;
667 case (3):
668 hw->nt_timer = 0;
669 hw->timers &= ~NT_ACTIVATION_TIMER;
670 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
671 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -0800672 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100673 handle_led(hw, LED_S0_ON);
674 break;
675 case (4):
676 hw->nt_timer = 0;
677 hw->timers &= ~NT_ACTIVATION_TIMER;
678 break;
679 default:
680 break;
681 }
682 hfcsusb_ph_info(hw);
683}
684
685static void
686ph_state(struct dchannel *dch)
687{
688 struct hfcsusb *hw = dch->hw;
689
690 if (hw->protocol == ISDN_P_NT_S0)
691 ph_state_nt(dch);
692 else if (hw->protocol == ISDN_P_TE_S0)
693 ph_state_te(dch);
694}
695
696/*
697 * disable/enable BChannel for desired protocoll
698 */
699static int
700hfcsusb_setup_bch(struct bchannel *bch, int protocol)
701{
702 struct hfcsusb *hw = bch->hw;
703 __u8 conhdlc, sctrl, sctrl_r;
704
705 if (debug & DEBUG_HW)
706 printk(KERN_DEBUG "%s: %s: protocol %x-->%x B%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800707 hw->name, __func__, bch->state, protocol,
708 bch->nr);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100709
710 /* setup val for CON_HDLC */
711 conhdlc = 0;
712 if (protocol > ISDN_P_NONE)
713 conhdlc = 8; /* enable FIFO */
714
715 switch (protocol) {
716 case (-1): /* used for init */
717 bch->state = -1;
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200718 /* fall through */
Karsten Keil69f52ad2009-01-09 16:20:51 +0100719 case (ISDN_P_NONE):
720 if (bch->state == ISDN_P_NONE)
721 return 0; /* already in idle state */
722 bch->state = ISDN_P_NONE;
723 clear_bit(FLG_HDLC, &bch->Flags);
724 clear_bit(FLG_TRANSPARENT, &bch->Flags);
725 break;
726 case (ISDN_P_B_RAW):
727 conhdlc |= 2;
728 bch->state = protocol;
729 set_bit(FLG_TRANSPARENT, &bch->Flags);
730 break;
731 case (ISDN_P_B_HDLC):
732 bch->state = protocol;
733 set_bit(FLG_HDLC, &bch->Flags);
734 break;
735 default:
736 if (debug & DEBUG_HW)
737 printk(KERN_DEBUG "%s: %s: prot not known %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800738 hw->name, __func__, protocol);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100739 return -ENOPROTOOPT;
740 }
741
742 if (protocol >= ISDN_P_NONE) {
743 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 0 : 2);
744 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
745 write_reg(hw, HFCUSB_INC_RES_F, 2);
746 write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 1 : 3);
747 write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
748 write_reg(hw, HFCUSB_INC_RES_F, 2);
749
750 sctrl = 0x40 + ((hw->protocol == ISDN_P_TE_S0) ? 0x00 : 0x04);
751 sctrl_r = 0x0;
752 if (test_bit(FLG_ACTIVE, &hw->bch[0].Flags)) {
753 sctrl |= 1;
754 sctrl_r |= 1;
755 }
756 if (test_bit(FLG_ACTIVE, &hw->bch[1].Flags)) {
757 sctrl |= 2;
758 sctrl_r |= 2;
759 }
760 write_reg(hw, HFCUSB_SCTRL, sctrl);
761 write_reg(hw, HFCUSB_SCTRL_R, sctrl_r);
762
763 if (protocol > ISDN_P_NONE)
764 handle_led(hw, (bch->nr == 1) ? LED_B1_ON : LED_B2_ON);
765 else
766 handle_led(hw, (bch->nr == 1) ? LED_B1_OFF :
Joe Perches475be4d2012-02-19 19:52:38 -0800767 LED_B2_OFF);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100768 }
769 hfcsusb_ph_info(hw);
770 return 0;
771}
772
773static void
774hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
775{
776 if (debug & DEBUG_HW)
777 printk(KERN_DEBUG "%s: %s: %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800778 hw->name, __func__, command);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100779
780 switch (command) {
781 case HFC_L1_ACTIVATE_TE:
782 /* force sending sending INFO1 */
783 write_reg(hw, HFCUSB_STATES, 0x14);
784 /* start l1 activation */
785 write_reg(hw, HFCUSB_STATES, 0x04);
786 break;
787
788 case HFC_L1_FORCE_DEACTIVATE_TE:
789 write_reg(hw, HFCUSB_STATES, 0x10);
790 write_reg(hw, HFCUSB_STATES, 0x03);
791 break;
792
793 case HFC_L1_ACTIVATE_NT:
794 if (hw->dch.state == 3)
795 _queue_data(&hw->dch.dev.D, PH_ACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -0800796 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100797 else
798 write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
Joe Perches475be4d2012-02-19 19:52:38 -0800799 HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100800 break;
801
802 case HFC_L1_DEACTIVATE_NT:
803 write_reg(hw, HFCUSB_STATES,
Joe Perches475be4d2012-02-19 19:52:38 -0800804 HFCUSB_DO_ACTION);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100805 break;
806 }
807}
808
809/*
810 * Layer 1 B-channel hardware access
811 */
812static int
813channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
814{
815 int ret = 0;
816
817 switch (cq->op) {
818 case MISDN_CTRL_GETOP:
819 cq->op = MISDN_CTRL_FILL_EMPTY;
820 break;
821 case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
822 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
823 if (debug & DEBUG_HW_OPEN)
824 printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
Joe Perches475be4d2012-02-19 19:52:38 -0800825 "off=%d)\n", __func__, bch->nr, !!cq->p1);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100826 break;
827 default:
828 printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
829 ret = -EINVAL;
830 break;
831 }
832 return ret;
833}
834
835/* collect data from incoming interrupt or isochron USB data */
836static void
837hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
Joe Perches475be4d2012-02-19 19:52:38 -0800838 int finish)
Karsten Keil69f52ad2009-01-09 16:20:51 +0100839{
840 struct hfcsusb *hw = fifo->hw;
841 struct sk_buff *rx_skb = NULL;
842 int maxlen = 0;
843 int fifon = fifo->fifonum;
844 int i;
845 int hdlc = 0;
846
847 if (debug & DBG_HFC_CALL_TRACE)
848 printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
Joe Perches475be4d2012-02-19 19:52:38 -0800849 "dch(%p) bch(%p) ech(%p)\n",
850 hw->name, __func__, fifon, len,
851 fifo->dch, fifo->bch, fifo->ech);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100852
853 if (!len)
854 return;
855
856 if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
857 printk(KERN_DEBUG "%s: %s: undefined channel\n",
858 hw->name, __func__);
859 return;
860 }
861
862 spin_lock(&hw->lock);
863 if (fifo->dch) {
864 rx_skb = fifo->dch->rx_skb;
865 maxlen = fifo->dch->maxlen;
866 hdlc = 1;
867 }
868 if (fifo->bch) {
869 rx_skb = fifo->bch->rx_skb;
870 maxlen = fifo->bch->maxlen;
871 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
872 }
873 if (fifo->ech) {
874 rx_skb = fifo->ech->rx_skb;
875 maxlen = fifo->ech->maxlen;
876 hdlc = 1;
877 }
878
879 if (!rx_skb) {
880 rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
881 if (rx_skb) {
882 if (fifo->dch)
883 fifo->dch->rx_skb = rx_skb;
884 if (fifo->bch)
885 fifo->bch->rx_skb = rx_skb;
886 if (fifo->ech)
887 fifo->ech->rx_skb = rx_skb;
888 skb_trim(rx_skb, 0);
889 } else {
890 printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800891 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100892 spin_unlock(&hw->lock);
893 return;
894 }
895 }
896
897 if (fifo->dch || fifo->ech) {
898 /* D/E-Channel SKB range check */
899 if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
900 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
Joe Perches475be4d2012-02-19 19:52:38 -0800901 "for fifo(%d) HFCUSB_D_RX\n",
902 hw->name, __func__, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100903 skb_trim(rx_skb, 0);
904 spin_unlock(&hw->lock);
905 return;
906 }
907 } else if (fifo->bch) {
908 /* B-Channel SKB range check */
909 if ((rx_skb->len + len) >= (MAX_BCH_SIZE + 3)) {
910 printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
Joe Perches475be4d2012-02-19 19:52:38 -0800911 "for fifo(%d) HFCUSB_B_RX\n",
912 hw->name, __func__, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100913 skb_trim(rx_skb, 0);
914 spin_unlock(&hw->lock);
915 return;
916 }
917 }
918
919 memcpy(skb_put(rx_skb, len), data, len);
920
921 if (hdlc) {
922 /* we have a complete hdlc packet */
923 if (finish) {
924 if ((rx_skb->len > 3) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800925 (!(rx_skb->data[rx_skb->len - 1]))) {
Karsten Keil69f52ad2009-01-09 16:20:51 +0100926 if (debug & DBG_HFC_FIFO_VERBOSE) {
927 printk(KERN_DEBUG "%s: %s: fifon(%i)"
Joe Perches475be4d2012-02-19 19:52:38 -0800928 " new RX len(%i): ",
929 hw->name, __func__, fifon,
930 rx_skb->len);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100931 i = 0;
932 while (i < rx_skb->len)
933 printk("%02x ",
Joe Perches475be4d2012-02-19 19:52:38 -0800934 rx_skb->data[i++]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100935 printk("\n");
936 }
937
938 /* remove CRC & status */
939 skb_trim(rx_skb, rx_skb->len - 3);
940
941 if (fifo->dch)
942 recv_Dchannel(fifo->dch);
943 if (fifo->bch)
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000944 recv_Bchannel(fifo->bch, MISDN_ID_ANY);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100945 if (fifo->ech)
946 recv_Echannel(fifo->ech,
Joe Perches475be4d2012-02-19 19:52:38 -0800947 &hw->dch);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100948 } else {
949 if (debug & DBG_HFC_FIFO_VERBOSE) {
950 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800951 "%s: CRC or minlen ERROR fifon(%i) "
952 "RX len(%i): ",
953 hw->name, fifon, rx_skb->len);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100954 i = 0;
955 while (i < rx_skb->len)
956 printk("%02x ",
Joe Perches475be4d2012-02-19 19:52:38 -0800957 rx_skb->data[i++]);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100958 printk("\n");
959 }
960 skb_trim(rx_skb, 0);
961 }
962 }
963 } else {
964 /* deliver transparent data to layer2 */
965 if (rx_skb->len >= poll)
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000966 recv_Bchannel(fifo->bch, MISDN_ID_ANY);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100967 }
968 spin_unlock(&hw->lock);
969}
970
Hannes Eder6c2959a2009-02-12 09:28:40 +0000971static void
Karsten Keil69f52ad2009-01-09 16:20:51 +0100972fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
973 void *buf, int num_packets, int packet_size, int interval,
974 usb_complete_t complete, void *context)
975{
976 int k;
977
978 usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
Joe Perches475be4d2012-02-19 19:52:38 -0800979 complete, context);
Karsten Keil69f52ad2009-01-09 16:20:51 +0100980
981 urb->number_of_packets = num_packets;
982 urb->transfer_flags = URB_ISO_ASAP;
983 urb->actual_length = 0;
984 urb->interval = interval;
985
986 for (k = 0; k < num_packets; k++) {
987 urb->iso_frame_desc[k].offset = packet_size * k;
988 urb->iso_frame_desc[k].length = packet_size;
989 urb->iso_frame_desc[k].actual_length = 0;
990 }
991}
992
993/* receive completion routine for all ISO tx fifos */
994static void
995rx_iso_complete(struct urb *urb)
996{
997 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
998 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
999 struct hfcsusb *hw = fifo->hw;
1000 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
Joe Perches475be4d2012-02-19 19:52:38 -08001001 status, iso_status, i;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001002 __u8 *buf;
1003 static __u8 eof[8];
1004 __u8 s0_state;
1005
1006 fifon = fifo->fifonum;
1007 status = urb->status;
1008
1009 spin_lock(&hw->lock);
1010 if (fifo->stop_gracefull) {
1011 fifo->stop_gracefull = 0;
1012 fifo->active = 0;
1013 spin_unlock(&hw->lock);
1014 return;
1015 }
1016 spin_unlock(&hw->lock);
1017
1018 /*
1019 * ISO transfer only partially completed,
1020 * look at individual frame status for details
1021 */
1022 if (status == -EXDEV) {
1023 if (debug & DEBUG_HW)
1024 printk(KERN_DEBUG "%s: %s: with -EXDEV "
Joe Perches475be4d2012-02-19 19:52:38 -08001025 "urb->status %d, fifonum %d\n",
1026 hw->name, __func__, status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001027
1028 /* clear status, so go on with ISO transfers */
1029 status = 0;
1030 }
1031
1032 s0_state = 0;
1033 if (fifo->active && !status) {
1034 num_isoc_packets = iso_packets[fifon];
1035 maxlen = fifo->usb_packet_maxlen;
1036
1037 for (k = 0; k < num_isoc_packets; ++k) {
1038 len = urb->iso_frame_desc[k].actual_length;
1039 offset = urb->iso_frame_desc[k].offset;
1040 buf = context_iso_urb->buffer + offset;
1041 iso_status = urb->iso_frame_desc[k].status;
1042
1043 if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
1044 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001045 "ISO packet %i, status: %i\n",
1046 hw->name, __func__, k, iso_status);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001047 }
1048
1049 /* USB data log for every D ISO in */
1050 if ((fifon == HFCUSB_D_RX) &&
1051 (debug & DBG_HFC_USB_VERBOSE)) {
1052 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001053 "%s: %s: %d (%d/%d) len(%d) ",
1054 hw->name, __func__, urb->start_frame,
1055 k, num_isoc_packets - 1,
1056 len);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001057 for (i = 0; i < len; i++)
1058 printk("%x ", buf[i]);
1059 printk("\n");
1060 }
1061
1062 if (!iso_status) {
1063 if (fifo->last_urblen != maxlen) {
1064 /*
1065 * save fifo fill-level threshold bits
1066 * to use them later in TX ISO URB
1067 * completions
1068 */
1069 hw->threshold_mask = buf[1];
1070
1071 if (fifon == HFCUSB_D_RX)
1072 s0_state = (buf[0] >> 4);
1073
1074 eof[fifon] = buf[0] & 1;
1075 if (len > 2)
1076 hfcsusb_rx_frame(fifo, buf + 2,
Joe Perches475be4d2012-02-19 19:52:38 -08001077 len - 2, (len < maxlen)
1078 ? eof[fifon] : 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001079 } else
1080 hfcsusb_rx_frame(fifo, buf, len,
Joe Perches475be4d2012-02-19 19:52:38 -08001081 (len < maxlen) ?
1082 eof[fifon] : 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001083 fifo->last_urblen = len;
1084 }
1085 }
1086
1087 /* signal S0 layer1 state change */
1088 if ((s0_state) && (hw->initdone) &&
1089 (s0_state != hw->dch.state)) {
1090 hw->dch.state = s0_state;
1091 schedule_event(&hw->dch, FLG_PHCHANGE);
1092 }
1093
1094 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1095 context_iso_urb->buffer, num_isoc_packets,
1096 fifo->usb_packet_maxlen, fifo->intervall,
1097 (usb_complete_t)rx_iso_complete, urb->context);
1098 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1099 if (errcode < 0) {
1100 if (debug & DEBUG_HW)
1101 printk(KERN_DEBUG "%s: %s: error submitting "
Joe Perches475be4d2012-02-19 19:52:38 -08001102 "ISO URB: %d\n",
1103 hw->name, __func__, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001104 }
1105 } else {
1106 if (status && (debug & DBG_HFC_URB_INFO))
1107 printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
Joe Perches475be4d2012-02-19 19:52:38 -08001108 "urb->status %d, fifonum %d\n",
1109 hw->name, __func__, status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001110 }
1111}
1112
1113/* receive completion routine for all interrupt rx fifos */
1114static void
1115rx_int_complete(struct urb *urb)
1116{
1117 int len, status, i;
1118 __u8 *buf, maxlen, fifon;
1119 struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
1120 struct hfcsusb *hw = fifo->hw;
1121 static __u8 eof[8];
1122
1123 spin_lock(&hw->lock);
1124 if (fifo->stop_gracefull) {
1125 fifo->stop_gracefull = 0;
1126 fifo->active = 0;
1127 spin_unlock(&hw->lock);
1128 return;
1129 }
1130 spin_unlock(&hw->lock);
1131
1132 fifon = fifo->fifonum;
1133 if ((!fifo->active) || (urb->status)) {
1134 if (debug & DBG_HFC_URB_ERROR)
1135 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001136 "%s: %s: RX-Fifo %i is going down (%i)\n",
1137 hw->name, __func__, fifon, urb->status);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001138
1139 fifo->urb->interval = 0; /* cancel automatic rescheduling */
1140 return;
1141 }
1142 len = urb->actual_length;
1143 buf = fifo->buffer;
1144 maxlen = fifo->usb_packet_maxlen;
1145
1146 /* USB data log for every D INT in */
1147 if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
1148 printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
Joe Perches475be4d2012-02-19 19:52:38 -08001149 hw->name, __func__, len);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001150 for (i = 0; i < len; i++)
1151 printk("%02x ", buf[i]);
1152 printk("\n");
1153 }
1154
1155 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
1156 /* the threshold mask is in the 2nd status byte */
1157 hw->threshold_mask = buf[1];
1158
1159 /* signal S0 layer1 state change */
1160 if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
1161 hw->dch.state = (buf[0] >> 4);
1162 schedule_event(&hw->dch, FLG_PHCHANGE);
1163 }
1164
1165 eof[fifon] = buf[0] & 1;
1166 /* if we have more than the 2 status bytes -> collect data */
1167 if (len > 2)
1168 hfcsusb_rx_frame(fifo, buf + 2,
Joe Perches475be4d2012-02-19 19:52:38 -08001169 urb->actual_length - 2,
1170 (len < maxlen) ? eof[fifon] : 0);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001171 } else {
1172 hfcsusb_rx_frame(fifo, buf, urb->actual_length,
1173 (len < maxlen) ? eof[fifon] : 0);
1174 }
1175 fifo->last_urblen = urb->actual_length;
1176
1177 status = usb_submit_urb(urb, GFP_ATOMIC);
1178 if (status) {
1179 if (debug & DEBUG_HW)
1180 printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001181 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001182 }
1183}
1184
1185/* transmit completion routine for all ISO tx fifos */
1186static void
1187tx_iso_complete(struct urb *urb)
1188{
1189 struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
1190 struct usb_fifo *fifo = context_iso_urb->owner_fifo;
1191 struct hfcsusb *hw = fifo->hw;
1192 struct sk_buff *tx_skb;
1193 int k, tx_offset, num_isoc_packets, sink, remain, current_len,
Joe Perches475be4d2012-02-19 19:52:38 -08001194 errcode, hdlc, i;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001195 int *tx_idx;
1196 int frame_complete, fifon, status;
1197 __u8 threshbit;
1198
1199 spin_lock(&hw->lock);
1200 if (fifo->stop_gracefull) {
1201 fifo->stop_gracefull = 0;
1202 fifo->active = 0;
1203 spin_unlock(&hw->lock);
1204 return;
1205 }
1206
1207 if (fifo->dch) {
1208 tx_skb = fifo->dch->tx_skb;
1209 tx_idx = &fifo->dch->tx_idx;
1210 hdlc = 1;
1211 } else if (fifo->bch) {
1212 tx_skb = fifo->bch->tx_skb;
1213 tx_idx = &fifo->bch->tx_idx;
1214 hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
1215 } else {
1216 printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001217 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001218 spin_unlock(&hw->lock);
1219 return;
1220 }
1221
1222 fifon = fifo->fifonum;
1223 status = urb->status;
1224
1225 tx_offset = 0;
1226
1227 /*
1228 * ISO transfer only partially completed,
1229 * look at individual frame status for details
1230 */
1231 if (status == -EXDEV) {
1232 if (debug & DBG_HFC_URB_ERROR)
1233 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001234 "-EXDEV (%i) fifon (%d)\n",
1235 hw->name, __func__, status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001236
1237 /* clear status, so go on with ISO transfers */
1238 status = 0;
1239 }
1240
1241 if (fifo->active && !status) {
1242 /* is FifoFull-threshold set for our channel? */
1243 threshbit = (hw->threshold_mask & (1 << fifon));
1244 num_isoc_packets = iso_packets[fifon];
1245
1246 /* predict dataflow to avoid fifo overflow */
1247 if (fifon >= HFCUSB_D_TX)
1248 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
1249 else
1250 sink = (threshbit) ? SINK_MIN : SINK_MAX;
1251 fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
1252 context_iso_urb->buffer, num_isoc_packets,
1253 fifo->usb_packet_maxlen, fifo->intervall,
1254 (usb_complete_t)tx_iso_complete, urb->context);
1255 memset(context_iso_urb->buffer, 0,
1256 sizeof(context_iso_urb->buffer));
1257 frame_complete = 0;
1258
1259 for (k = 0; k < num_isoc_packets; ++k) {
1260 /* analyze tx success of previous ISO packets */
1261 if (debug & DBG_HFC_URB_ERROR) {
1262 errcode = urb->iso_frame_desc[k].status;
1263 if (errcode) {
1264 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001265 "ISO packet %i, status: %i\n",
1266 hw->name, __func__, k, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001267 }
1268 }
1269
1270 /* Generate next ISO Packets */
1271 if (tx_skb)
1272 remain = tx_skb->len - *tx_idx;
1273 else
1274 remain = 0;
1275
1276 if (remain > 0) {
1277 fifo->bit_line -= sink;
1278 current_len = (0 - fifo->bit_line) / 8;
1279 if (current_len > 14)
1280 current_len = 14;
1281 if (current_len < 0)
1282 current_len = 0;
1283 if (remain < current_len)
1284 current_len = remain;
1285
1286 /* how much bit do we put on the line? */
1287 fifo->bit_line += current_len * 8;
1288
1289 context_iso_urb->buffer[tx_offset] = 0;
1290 if (current_len == remain) {
1291 if (hdlc) {
1292 /* signal frame completion */
1293 context_iso_urb->
Joe Perches475be4d2012-02-19 19:52:38 -08001294 buffer[tx_offset] = 1;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001295 /* add 2 byte flags and 16bit
1296 * CRC at end of ISDN frame */
1297 fifo->bit_line += 32;
1298 }
1299 frame_complete = 1;
1300 }
1301
1302 /* copy tx data to iso-urb buffer */
1303 memcpy(context_iso_urb->buffer + tx_offset + 1,
1304 (tx_skb->data + *tx_idx), current_len);
1305 *tx_idx += current_len;
1306
1307 urb->iso_frame_desc[k].offset = tx_offset;
1308 urb->iso_frame_desc[k].length = current_len + 1;
1309
1310 /* USB data log for every D ISO out */
1311 if ((fifon == HFCUSB_D_RX) &&
1312 (debug & DBG_HFC_USB_VERBOSE)) {
1313 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001314 "%s: %s (%d/%d) offs(%d) len(%d) ",
1315 hw->name, __func__,
1316 k, num_isoc_packets - 1,
1317 urb->iso_frame_desc[k].offset,
1318 urb->iso_frame_desc[k].length);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001319
1320 for (i = urb->iso_frame_desc[k].offset;
1321 i < (urb->iso_frame_desc[k].offset
Joe Perches475be4d2012-02-19 19:52:38 -08001322 + urb->iso_frame_desc[k].length);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001323 i++)
1324 printk("%x ",
Joe Perches475be4d2012-02-19 19:52:38 -08001325 context_iso_urb->buffer[i]);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001326
1327 printk(" skb->len(%i) tx-idx(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001328 tx_skb->len, *tx_idx);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001329 }
1330
1331 tx_offset += (current_len + 1);
1332 } else {
1333 urb->iso_frame_desc[k].offset = tx_offset++;
1334 urb->iso_frame_desc[k].length = 1;
1335 /* we lower data margin every msec */
1336 fifo->bit_line -= sink;
1337 if (fifo->bit_line < BITLINE_INF)
1338 fifo->bit_line = BITLINE_INF;
1339 }
1340
1341 if (frame_complete) {
1342 frame_complete = 0;
1343
1344 if (debug & DBG_HFC_FIFO_VERBOSE) {
1345 printk(KERN_DEBUG "%s: %s: "
Joe Perches475be4d2012-02-19 19:52:38 -08001346 "fifon(%i) new TX len(%i): ",
1347 hw->name, __func__,
1348 fifon, tx_skb->len);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001349 i = 0;
1350 while (i < tx_skb->len)
1351 printk("%02x ",
Joe Perches475be4d2012-02-19 19:52:38 -08001352 tx_skb->data[i++]);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001353 printk("\n");
1354 }
1355
1356 dev_kfree_skb(tx_skb);
1357 tx_skb = NULL;
1358 if (fifo->dch && get_next_dframe(fifo->dch))
1359 tx_skb = fifo->dch->tx_skb;
1360 else if (fifo->bch &&
Karsten Keil8bfddfb2012-05-15 23:51:02 +00001361 get_next_bframe(fifo->bch))
Karsten Keil69f52ad2009-01-09 16:20:51 +01001362 tx_skb = fifo->bch->tx_skb;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001363 }
1364 }
1365 errcode = usb_submit_urb(urb, GFP_ATOMIC);
1366 if (errcode < 0) {
1367 if (debug & DEBUG_HW)
1368 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001369 "%s: %s: error submitting ISO URB: %d \n",
1370 hw->name, __func__, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001371 }
1372
1373 /*
1374 * abuse DChannel tx iso completion to trigger NT mode state
1375 * changes tx_iso_complete is assumed to be called every
1376 * fifo->intervall (ms)
1377 */
1378 if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
1379 && (hw->timers & NT_ACTIVATION_TIMER)) {
1380 if ((--hw->nt_timer) < 0)
1381 schedule_event(&hw->dch, FLG_PHCHANGE);
1382 }
1383
1384 } else {
1385 if (status && (debug & DBG_HFC_URB_ERROR))
1386 printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
Joe Perches475be4d2012-02-19 19:52:38 -08001387 "fifonum=%d\n",
1388 hw->name, __func__,
1389 symbolic(urb_errlist, status), status, fifon);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001390 }
1391 spin_unlock(&hw->lock);
1392}
1393
1394/*
1395 * allocs urbs and start isoc transfer with two pending urbs to avoid
1396 * gaps in the transfer chain
1397 */
1398static int
1399start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
1400 usb_complete_t complete, int packet_size)
1401{
1402 struct hfcsusb *hw = fifo->hw;
1403 int i, k, errcode;
1404
1405 if (debug)
1406 printk(KERN_DEBUG "%s: %s: fifo %i\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001407 hw->name, __func__, fifo->fifonum);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001408
1409 /* allocate Memory for Iso out Urbs */
1410 for (i = 0; i < 2; i++) {
1411 if (!(fifo->iso[i].urb)) {
1412 fifo->iso[i].urb =
Joe Perches475be4d2012-02-19 19:52:38 -08001413 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001414 if (!(fifo->iso[i].urb)) {
1415 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001416 "%s: %s: alloc urb for fifo %i failed",
1417 hw->name, __func__, fifo->fifonum);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001418 }
1419 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
1420 fifo->iso[i].indx = i;
1421
1422 /* Init the first iso */
1423 if (ISO_BUFFER_SIZE >=
1424 (fifo->usb_packet_maxlen *
1425 num_packets_per_urb)) {
1426 fill_isoc_urb(fifo->iso[i].urb,
Joe Perches475be4d2012-02-19 19:52:38 -08001427 fifo->hw->dev, fifo->pipe,
1428 fifo->iso[i].buffer,
1429 num_packets_per_urb,
1430 fifo->usb_packet_maxlen,
1431 fifo->intervall, complete,
1432 &fifo->iso[i]);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001433 memset(fifo->iso[i].buffer, 0,
1434 sizeof(fifo->iso[i].buffer));
1435
1436 for (k = 0; k < num_packets_per_urb; k++) {
1437 fifo->iso[i].urb->
Joe Perches475be4d2012-02-19 19:52:38 -08001438 iso_frame_desc[k].offset =
1439 k * packet_size;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001440 fifo->iso[i].urb->
Joe Perches475be4d2012-02-19 19:52:38 -08001441 iso_frame_desc[k].length =
1442 packet_size;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001443 }
1444 } else {
1445 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001446 "%s: %s: ISO Buffer size to small!\n",
1447 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001448 }
1449 }
1450 fifo->bit_line = BITLINE_INF;
1451
1452 errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
1453 fifo->active = (errcode >= 0) ? 1 : 0;
1454 fifo->stop_gracefull = 0;
1455 if (errcode < 0) {
1456 printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001457 hw->name, __func__,
1458 symbolic(urb_errlist, errcode), i);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001459 }
1460 }
1461 return fifo->active;
1462}
1463
1464static void
1465stop_iso_gracefull(struct usb_fifo *fifo)
1466{
1467 struct hfcsusb *hw = fifo->hw;
1468 int i, timeout;
1469 u_long flags;
1470
1471 for (i = 0; i < 2; i++) {
1472 spin_lock_irqsave(&hw->lock, flags);
1473 if (debug)
1474 printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
1475 hw->name, __func__, fifo->fifonum, i);
1476 fifo->stop_gracefull = 1;
1477 spin_unlock_irqrestore(&hw->lock, flags);
1478 }
1479
1480 for (i = 0; i < 2; i++) {
1481 timeout = 3;
1482 while (fifo->stop_gracefull && timeout--)
Joe Perches475be4d2012-02-19 19:52:38 -08001483 schedule_timeout_interruptible((HZ / 1000) * 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001484 if (debug && fifo->stop_gracefull)
1485 printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001486 hw->name, __func__, fifo->fifonum, i);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001487 }
1488}
1489
1490static void
1491stop_int_gracefull(struct usb_fifo *fifo)
1492{
1493 struct hfcsusb *hw = fifo->hw;
1494 int timeout;
1495 u_long flags;
1496
1497 spin_lock_irqsave(&hw->lock, flags);
1498 if (debug)
1499 printk(KERN_DEBUG "%s: %s for fifo %i\n",
1500 hw->name, __func__, fifo->fifonum);
1501 fifo->stop_gracefull = 1;
1502 spin_unlock_irqrestore(&hw->lock, flags);
1503
1504 timeout = 3;
1505 while (fifo->stop_gracefull && timeout--)
Joe Perches475be4d2012-02-19 19:52:38 -08001506 schedule_timeout_interruptible((HZ / 1000) * 3);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001507 if (debug && fifo->stop_gracefull)
1508 printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
1509 hw->name, __func__, fifo->fifonum);
1510}
1511
1512/* start the interrupt transfer for the given fifo */
1513static void
1514start_int_fifo(struct usb_fifo *fifo)
1515{
1516 struct hfcsusb *hw = fifo->hw;
1517 int errcode;
1518
1519 if (debug)
1520 printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001521 hw->name, __func__, fifo->fifonum);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001522
1523 if (!fifo->urb) {
1524 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
1525 if (!fifo->urb)
1526 return;
1527 }
1528 usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
Joe Perches475be4d2012-02-19 19:52:38 -08001529 fifo->buffer, fifo->usb_packet_maxlen,
1530 (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001531 fifo->active = 1;
1532 fifo->stop_gracefull = 0;
1533 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
1534 if (errcode) {
1535 printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001536 hw->name, __func__, errcode);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001537 fifo->active = 0;
1538 }
1539}
1540
1541static void
1542setPortMode(struct hfcsusb *hw)
1543{
1544 if (debug & DEBUG_HW)
1545 printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
Joe Perches475be4d2012-02-19 19:52:38 -08001546 (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
Karsten Keil69f52ad2009-01-09 16:20:51 +01001547
1548 if (hw->protocol == ISDN_P_TE_S0) {
1549 write_reg(hw, HFCUSB_SCTRL, 0x40);
1550 write_reg(hw, HFCUSB_SCTRL_E, 0x00);
1551 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
1552 write_reg(hw, HFCUSB_STATES, 3 | 0x10);
1553 write_reg(hw, HFCUSB_STATES, 3);
1554 } else {
1555 write_reg(hw, HFCUSB_SCTRL, 0x44);
1556 write_reg(hw, HFCUSB_SCTRL_E, 0x09);
1557 write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
1558 write_reg(hw, HFCUSB_STATES, 1 | 0x10);
1559 write_reg(hw, HFCUSB_STATES, 1);
1560 }
1561}
1562
1563static void
1564reset_hfcsusb(struct hfcsusb *hw)
1565{
1566 struct usb_fifo *fifo;
1567 int i;
1568
1569 if (debug & DEBUG_HW)
1570 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1571
1572 /* do Chip reset */
1573 write_reg(hw, HFCUSB_CIRM, 8);
1574
1575 /* aux = output, reset off */
1576 write_reg(hw, HFCUSB_CIRM, 0x10);
1577
1578 /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
1579 write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
Joe Perches475be4d2012-02-19 19:52:38 -08001580 ((hw->packet_size / 8) << 4));
Karsten Keil69f52ad2009-01-09 16:20:51 +01001581
1582 /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
1583 write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
1584
1585 /* enable PCM/GCI master mode */
1586 write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
1587 write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
1588
1589 /* init the fifos */
1590 write_reg(hw, HFCUSB_F_THRES,
Joe Perches475be4d2012-02-19 19:52:38 -08001591 (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
Karsten Keil69f52ad2009-01-09 16:20:51 +01001592
1593 fifo = hw->fifos;
1594 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1595 write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
1596 fifo[i].max_size =
Joe Perches475be4d2012-02-19 19:52:38 -08001597 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001598 fifo[i].last_urblen = 0;
1599
1600 /* set 2 bit for D- & E-channel */
1601 write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
1602
1603 /* enable all fifos */
1604 if (i == HFCUSB_D_TX)
1605 write_reg(hw, HFCUSB_CON_HDLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001606 (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001607 else
1608 write_reg(hw, HFCUSB_CON_HDLC, 0x08);
1609 write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1610 }
1611
1612 write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1613 handle_led(hw, LED_POWER_ON);
1614}
1615
1616/* start USB data pipes dependand on device's endpoint configuration */
1617static void
1618hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
1619{
1620 /* quick check if endpoint already running */
1621 if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
1622 return;
1623 if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
1624 return;
1625 if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
1626 return;
1627 if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
1628 return;
1629
1630 /* start rx endpoints using USB INT IN method */
1631 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
Joe Perches475be4d2012-02-19 19:52:38 -08001632 start_int_fifo(hw->fifos + channel * 2 + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001633
1634 /* start rx endpoints using USB ISO IN method */
1635 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
1636 switch (channel) {
1637 case HFC_CHAN_D:
1638 start_isoc_chain(hw->fifos + HFCUSB_D_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001639 ISOC_PACKETS_D,
1640 (usb_complete_t)rx_iso_complete,
1641 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001642 break;
1643 case HFC_CHAN_E:
1644 start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001645 ISOC_PACKETS_D,
1646 (usb_complete_t)rx_iso_complete,
1647 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001648 break;
1649 case HFC_CHAN_B1:
1650 start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001651 ISOC_PACKETS_B,
1652 (usb_complete_t)rx_iso_complete,
1653 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001654 break;
1655 case HFC_CHAN_B2:
1656 start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
Joe Perches475be4d2012-02-19 19:52:38 -08001657 ISOC_PACKETS_B,
1658 (usb_complete_t)rx_iso_complete,
1659 16);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001660 break;
1661 }
1662 }
1663
1664 /* start tx endpoints using USB ISO OUT method */
1665 switch (channel) {
1666 case HFC_CHAN_D:
1667 start_isoc_chain(hw->fifos + HFCUSB_D_TX,
Joe Perches475be4d2012-02-19 19:52:38 -08001668 ISOC_PACKETS_B,
1669 (usb_complete_t)tx_iso_complete, 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001670 break;
1671 case HFC_CHAN_B1:
1672 start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
Joe Perches475be4d2012-02-19 19:52:38 -08001673 ISOC_PACKETS_D,
1674 (usb_complete_t)tx_iso_complete, 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001675 break;
1676 case HFC_CHAN_B2:
1677 start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
Joe Perches475be4d2012-02-19 19:52:38 -08001678 ISOC_PACKETS_B,
1679 (usb_complete_t)tx_iso_complete, 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001680 break;
1681 }
1682}
1683
1684/* stop USB data pipes dependand on device's endpoint configuration */
1685static void
1686hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
1687{
1688 /* quick check if endpoint currently running */
1689 if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
1690 return;
1691 if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
1692 return;
1693 if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
1694 return;
1695 if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
1696 return;
1697
1698 /* rx endpoints using USB INT IN method */
1699 if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
Joe Perches475be4d2012-02-19 19:52:38 -08001700 stop_int_gracefull(hw->fifos + channel * 2 + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001701
1702 /* rx endpoints using USB ISO IN method */
1703 if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
Joe Perches475be4d2012-02-19 19:52:38 -08001704 stop_iso_gracefull(hw->fifos + channel * 2 + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001705
1706 /* tx endpoints using USB ISO OUT method */
1707 if (channel != HFC_CHAN_E)
Joe Perches475be4d2012-02-19 19:52:38 -08001708 stop_iso_gracefull(hw->fifos + channel * 2);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001709}
1710
1711
1712/* Hardware Initialization */
Hannes Eder6c2959a2009-02-12 09:28:40 +00001713static int
Karsten Keil69f52ad2009-01-09 16:20:51 +01001714setup_hfcsusb(struct hfcsusb *hw)
1715{
Karsten Keil69f52ad2009-01-09 16:20:51 +01001716 u_char b;
1717
1718 if (debug & DBG_HFC_CALL_TRACE)
1719 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1720
1721 /* check the chip id */
1722 if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
1723 printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001724 hw->name, __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001725 return 1;
1726 }
1727 if (b != HFCUSB_CHIPID) {
1728 printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001729 hw->name, __func__, b);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001730 return 1;
1731 }
1732
1733 /* first set the needed config, interface and alternate */
David S. Miller3c76c582011-04-17 16:35:27 -07001734 (void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001735
1736 hw->led_state = 0;
1737
1738 /* init the background machinery for control requests */
1739 hw->ctrl_read.bRequestType = 0xc0;
1740 hw->ctrl_read.bRequest = 1;
1741 hw->ctrl_read.wLength = cpu_to_le16(1);
1742 hw->ctrl_write.bRequestType = 0x40;
1743 hw->ctrl_write.bRequest = 0;
1744 hw->ctrl_write.wLength = 0;
1745 usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
Joe Perches475be4d2012-02-19 19:52:38 -08001746 (u_char *)&hw->ctrl_write, NULL, 0,
1747 (usb_complete_t)ctrl_complete, hw);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001748
1749 reset_hfcsusb(hw);
1750 return 0;
1751}
1752
1753static void
1754release_hw(struct hfcsusb *hw)
1755{
1756 if (debug & DBG_HFC_CALL_TRACE)
1757 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1758
1759 /*
1760 * stop all endpoints gracefully
1761 * TODO: mISDN_core should generate CLOSE_CHANNEL
1762 * signals after calling mISDN_unregister_device()
1763 */
1764 hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
1765 hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
1766 hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
1767 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1768 hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
1769 if (hw->protocol == ISDN_P_TE_S0)
1770 l1_event(hw->dch.l1, CLOSE_CHANNEL);
1771
1772 mISDN_unregister_device(&hw->dch.dev);
1773 mISDN_freebchannel(&hw->bch[1]);
1774 mISDN_freebchannel(&hw->bch[0]);
1775 mISDN_freedchannel(&hw->dch);
1776
1777 if (hw->ctrl_urb) {
1778 usb_kill_urb(hw->ctrl_urb);
1779 usb_free_urb(hw->ctrl_urb);
1780 hw->ctrl_urb = NULL;
1781 }
1782
1783 if (hw->intf)
1784 usb_set_intfdata(hw->intf, NULL);
1785 list_del(&hw->list);
1786 kfree(hw);
1787 hw = NULL;
1788}
1789
1790static void
1791deactivate_bchannel(struct bchannel *bch)
1792{
1793 struct hfcsusb *hw = bch->hw;
1794 u_long flags;
1795
1796 if (bch->debug & DEBUG_HW)
1797 printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001798 hw->name, __func__, bch->nr);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001799
1800 spin_lock_irqsave(&hw->lock, flags);
Karsten Keilfb286f02009-07-09 10:02:29 +02001801 mISDN_clear_bchannel(bch);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001802 spin_unlock_irqrestore(&hw->lock, flags);
1803 hfcsusb_setup_bch(bch, ISDN_P_NONE);
1804 hfcsusb_stop_endpoint(hw, bch->nr);
1805}
1806
1807/*
1808 * Layer 1 B-channel hardware access
1809 */
1810static int
1811hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1812{
1813 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1814 int ret = -EINVAL;
1815
1816 if (bch->debug & DEBUG_HW)
1817 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1818
1819 switch (cmd) {
1820 case HW_TESTRX_RAW:
1821 case HW_TESTRX_HDLC:
1822 case HW_TESTRX_OFF:
1823 ret = -EINVAL;
1824 break;
1825
1826 case CLOSE_CHANNEL:
1827 test_and_clear_bit(FLG_OPEN, &bch->Flags);
Karsten Keil13681122012-05-15 23:51:01 +00001828 deactivate_bchannel(bch);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001829 ch->protocol = ISDN_P_NONE;
1830 ch->peer = NULL;
1831 module_put(THIS_MODULE);
1832 ret = 0;
1833 break;
1834 case CONTROL_CHANNEL:
1835 ret = channel_bctrl(bch, arg);
1836 break;
1837 default:
1838 printk(KERN_WARNING "%s: unknown prim(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001839 __func__, cmd);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001840 }
1841 return ret;
1842}
1843
1844static int
1845setup_instance(struct hfcsusb *hw, struct device *parent)
1846{
1847 u_long flags;
1848 int err, i;
1849
1850 if (debug & DBG_HFC_CALL_TRACE)
1851 printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
1852
1853 spin_lock_init(&hw->ctrl_lock);
1854 spin_lock_init(&hw->lock);
1855
1856 mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
1857 hw->dch.debug = debug & 0xFFFF;
1858 hw->dch.hw = hw;
1859 hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1860 hw->dch.dev.D.send = hfcusb_l2l1D;
1861 hw->dch.dev.D.ctrl = hfc_dctrl;
1862
1863 /* enable E-Channel logging */
1864 if (hw->fifos[HFCUSB_PCM_RX].pipe)
1865 mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
1866
1867 hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
Joe Perches475be4d2012-02-19 19:52:38 -08001868 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
Karsten Keil69f52ad2009-01-09 16:20:51 +01001869 hw->dch.dev.nrbchan = 2;
1870 for (i = 0; i < 2; i++) {
1871 hw->bch[i].nr = i + 1;
1872 set_channelmap(i + 1, hw->dch.dev.channelmap);
1873 hw->bch[i].debug = debug;
1874 mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM);
1875 hw->bch[i].hw = hw;
1876 hw->bch[i].ch.send = hfcusb_l2l1B;
1877 hw->bch[i].ch.ctrl = hfc_bctrl;
1878 hw->bch[i].ch.nr = i + 1;
1879 list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
1880 }
1881
1882 hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
1883 hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
1884 hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
1885 hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
1886 hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
1887 hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
1888 hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
1889 hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
1890
1891 err = setup_hfcsusb(hw);
1892 if (err)
1893 goto out;
1894
1895 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
Joe Perches475be4d2012-02-19 19:52:38 -08001896 hfcsusb_cnt + 1);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001897 printk(KERN_INFO "%s: registered as '%s'\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001898 DRIVER_NAME, hw->name);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001899
1900 err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
1901 if (err)
1902 goto out;
1903
1904 hfcsusb_cnt++;
1905 write_lock_irqsave(&HFClock, flags);
1906 list_add_tail(&hw->list, &HFClist);
1907 write_unlock_irqrestore(&HFClock, flags);
1908 return 0;
1909
1910out:
1911 mISDN_freebchannel(&hw->bch[1]);
1912 mISDN_freebchannel(&hw->bch[0]);
1913 mISDN_freedchannel(&hw->dch);
1914 kfree(hw);
1915 return err;
1916}
1917
1918static int
1919hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1920{
1921 struct hfcsusb *hw;
1922 struct usb_device *dev = interface_to_usbdev(intf);
1923 struct usb_host_interface *iface = intf->cur_altsetting;
1924 struct usb_host_interface *iface_used = NULL;
1925 struct usb_host_endpoint *ep;
1926 struct hfcsusb_vdata *driver_info;
1927 int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
Joe Perches475be4d2012-02-19 19:52:38 -08001928 probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
1929 ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
1930 alt_used = 0;
Karsten Keil69f52ad2009-01-09 16:20:51 +01001931
1932 vend_idx = 0xffff;
1933 for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
1934 if ((le16_to_cpu(dev->descriptor.idVendor)
Joe Perches475be4d2012-02-19 19:52:38 -08001935 == hfcsusb_idtab[i].idVendor) &&
Karsten Keil69f52ad2009-01-09 16:20:51 +01001936 (le16_to_cpu(dev->descriptor.idProduct)
Joe Perches475be4d2012-02-19 19:52:38 -08001937 == hfcsusb_idtab[i].idProduct)) {
Karsten Keil69f52ad2009-01-09 16:20:51 +01001938 vend_idx = i;
1939 continue;
1940 }
1941 }
1942
1943 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001944 "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
1945 __func__, ifnum, iface->desc.bAlternateSetting,
1946 intf->minor, vend_idx);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001947
1948 if (vend_idx == 0xffff) {
1949 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001950 "%s: no valid vendor found in USB descriptor\n",
1951 __func__);
Karsten Keil69f52ad2009-01-09 16:20:51 +01001952 return -EIO;
1953 }
1954 /* if vendor and product ID is OK, start probing alternate settings */
1955 alt_idx = 0;
1956 small_match = -1;
1957
1958 /* default settings */
1959 iso_packet_size = 16;
1960 packet_size = 64;
1961
1962 while (alt_idx < intf->num_altsetting) {
1963 iface = intf->altsetting + alt_idx;
1964 probe_alt_setting = iface->desc.bAlternateSetting;
1965 cfg_used = 0;
1966
1967 while (validconf[cfg_used][0]) {
1968 cfg_found = 1;
1969 vcf = validconf[cfg_used];
1970 ep = iface->endpoint;
1971 memcpy(cmptbl, vcf, 16 * sizeof(int));
1972
1973 /* check for all endpoints in this alternate setting */
1974 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1975 ep_addr = ep->desc.bEndpointAddress;
1976
1977 /* get endpoint base */
1978 idx = ((ep_addr & 0x7f) - 1) * 2;
1979 if (ep_addr & 0x80)
1980 idx++;
1981 attr = ep->desc.bmAttributes;
1982
1983 if (cmptbl[idx] != EP_NOP) {
1984 if (cmptbl[idx] == EP_NUL)
1985 cfg_found = 0;
1986 if (attr == USB_ENDPOINT_XFER_INT
Joe Perches475be4d2012-02-19 19:52:38 -08001987 && cmptbl[idx] == EP_INT)
Karsten Keil69f52ad2009-01-09 16:20:51 +01001988 cmptbl[idx] = EP_NUL;
1989 if (attr == USB_ENDPOINT_XFER_BULK
Joe Perches475be4d2012-02-19 19:52:38 -08001990 && cmptbl[idx] == EP_BLK)
Karsten Keil69f52ad2009-01-09 16:20:51 +01001991 cmptbl[idx] = EP_NUL;
1992 if (attr == USB_ENDPOINT_XFER_ISOC
Joe Perches475be4d2012-02-19 19:52:38 -08001993 && cmptbl[idx] == EP_ISO)
Karsten Keil69f52ad2009-01-09 16:20:51 +01001994 cmptbl[idx] = EP_NUL;
1995
1996 if (attr == USB_ENDPOINT_XFER_INT &&
Joe Perches475be4d2012-02-19 19:52:38 -08001997 ep->desc.bInterval < vcf[17]) {
Karsten Keil69f52ad2009-01-09 16:20:51 +01001998 cfg_found = 0;
1999 }
2000 }
2001 ep++;
2002 }
2003
2004 for (i = 0; i < 16; i++)
2005 if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
2006 cfg_found = 0;
2007
2008 if (cfg_found) {
2009 if (small_match < cfg_used) {
2010 small_match = cfg_used;
2011 alt_used = probe_alt_setting;
2012 iface_used = iface;
2013 }
2014 }
2015 cfg_used++;
2016 }
2017 alt_idx++;
2018 } /* (alt_idx < intf->num_altsetting) */
2019
2020 /* not found a valid USB Ta Endpoint config */
2021 if (small_match == -1)
2022 return -EIO;
2023
2024 iface = iface_used;
2025 hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
2026 if (!hw)
2027 return -ENOMEM; /* got no mem */
2028 snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
2029
2030 ep = iface->endpoint;
2031 vcf = validconf[small_match];
2032
2033 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
2034 struct usb_fifo *f;
2035
2036 ep_addr = ep->desc.bEndpointAddress;
2037 /* get endpoint base */
2038 idx = ((ep_addr & 0x7f) - 1) * 2;
2039 if (ep_addr & 0x80)
2040 idx++;
2041 f = &hw->fifos[idx & 7];
2042
2043 /* init Endpoints */
2044 if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
2045 ep++;
2046 continue;
2047 }
2048 switch (ep->desc.bmAttributes) {
2049 case USB_ENDPOINT_XFER_INT:
2050 f->pipe = usb_rcvintpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002051 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002052 f->usb_transfer_mode = USB_INT;
2053 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2054 break;
2055 case USB_ENDPOINT_XFER_BULK:
2056 if (ep_addr & 0x80)
2057 f->pipe = usb_rcvbulkpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002058 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002059 else
2060 f->pipe = usb_sndbulkpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002061 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002062 f->usb_transfer_mode = USB_BULK;
2063 packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2064 break;
2065 case USB_ENDPOINT_XFER_ISOC:
2066 if (ep_addr & 0x80)
2067 f->pipe = usb_rcvisocpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002068 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002069 else
2070 f->pipe = usb_sndisocpipe(dev,
Joe Perches475be4d2012-02-19 19:52:38 -08002071 ep->desc.bEndpointAddress);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002072 f->usb_transfer_mode = USB_ISOC;
2073 iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
2074 break;
2075 default:
2076 f->pipe = 0;
2077 }
2078
2079 if (f->pipe) {
2080 f->fifonum = idx & 7;
2081 f->hw = hw;
2082 f->usb_packet_maxlen =
Joe Perches475be4d2012-02-19 19:52:38 -08002083 le16_to_cpu(ep->desc.wMaxPacketSize);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002084 f->intervall = ep->desc.bInterval;
2085 }
2086 ep++;
2087 }
2088 hw->dev = dev; /* save device */
2089 hw->if_used = ifnum; /* save used interface */
2090 hw->alt_used = alt_used; /* and alternate config */
2091 hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
2092 hw->cfg_used = vcf[16]; /* store used config */
2093 hw->vend_idx = vend_idx; /* store found vendor */
2094 hw->packet_size = packet_size;
2095 hw->iso_packet_size = iso_packet_size;
2096
2097 /* create the control pipes needed for register access */
2098 hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
2099 hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
2100 hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
2101
2102 driver_info =
2103 (struct hfcsusb_vdata *)hfcsusb_idtab[vend_idx].driver_info;
2104 printk(KERN_DEBUG "%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002105 hw->name, __func__, driver_info->vend_name,
2106 conf_str[small_match], ifnum, alt_used);
Karsten Keil69f52ad2009-01-09 16:20:51 +01002107
2108 if (setup_instance(hw, dev->dev.parent))
2109 return -EIO;
2110
2111 hw->intf = intf;
2112 usb_set_intfdata(hw->intf, hw);
2113 return 0;
2114}
2115
2116/* function called when an active device is removed */
2117static void
2118hfcsusb_disconnect(struct usb_interface *intf)
2119{
2120 struct hfcsusb *hw = usb_get_intfdata(intf);
2121 struct hfcsusb *next;
2122 int cnt = 0;
2123
2124 printk(KERN_INFO "%s: device disconnected\n", hw->name);
2125
2126 handle_led(hw, LED_POWER_OFF);
2127 release_hw(hw);
2128
2129 list_for_each_entry_safe(hw, next, &HFClist, list)
2130 cnt++;
2131 if (!cnt)
2132 hfcsusb_cnt = 0;
2133
2134 usb_set_intfdata(intf, NULL);
2135}
2136
2137static struct usb_driver hfcsusb_drv = {
2138 .name = DRIVER_NAME,
2139 .id_table = hfcsusb_idtab,
2140 .probe = hfcsusb_probe,
2141 .disconnect = hfcsusb_disconnect,
2142};
2143
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002144module_usb_driver(hfcsusb_drv);