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