root/drivers/isdn/hardware/mISDN/hfcsusb.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ctrl_start_transfer
  2. write_reg
  3. ctrl_complete
  4. set_led_bit
  5. handle_led
  6. hfcusb_l2l1B
  7. hfcsusb_ph_info
  8. hfcusb_l2l1D
  9. hfc_l1callback
  10. open_dchannel
  11. open_bchannel
  12. channel_ctrl
  13. hfc_dctrl
  14. ph_state_te
  15. ph_state_nt
  16. ph_state
  17. hfcsusb_setup_bch
  18. hfcsusb_ph_command
  19. channel_bctrl
  20. hfcsusb_rx_frame
  21. fill_isoc_urb
  22. rx_iso_complete
  23. rx_int_complete
  24. tx_iso_complete
  25. start_isoc_chain
  26. stop_iso_gracefull
  27. stop_int_gracefull
  28. start_int_fifo
  29. setPortMode
  30. reset_hfcsusb
  31. hfcsusb_start_endpoint
  32. hfcsusb_stop_endpoint
  33. setup_hfcsusb
  34. release_hw
  35. deactivate_bchannel
  36. hfc_bctrl
  37. setup_instance
  38. hfcsusb_probe
  39. hfcsusb_disconnect

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

/* [<][>][^][v][top][bottom][index][help] */