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