linux/drivers/isdn/hisax/hfc_usb.c
<<
>>
Prefs
   1/*
   2 * hfc_usb.c
   3 *
   4 * $Id: hfc_usb.c,v 2.3.2.24 2007/10/14 08:40:29 mbachem Exp $
   5 *
   6 * modular HiSax ISDN driver for Colognechip HFC-S USB chip
   7 *
   8 * Authors : Peter Sprenger (sprenger@moving-bytes.de)
   9 *           Martin Bachem (m.bachem@gmx.de, info@colognechip.com)
  10 *
  11 *           based on the first hfc_usb driver of
  12 *           Werner Cornelius (werner@isdn-development.de)
  13 *
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2, or (at your option)
  17 * any later version.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22 * GNU General Public License for more details.
  23 *
  24 * You should have received a copy of the GNU General Public License
  25 * along with this program; if not, write to the Free Software
  26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27 *
  28 * See Version Histroy at the bottom of this file
  29 *
  30 */
  31
  32#include <linux/types.h>
  33#include <linux/stddef.h>
  34#include <linux/timer.h>
  35#include <linux/init.h>
  36#include <linux/module.h>
  37#include <linux/kernel_stat.h>
  38#include <linux/usb.h>
  39#include <linux/kernel.h>
  40#include <linux/sched.h>
  41#include <linux/moduleparam.h>
  42#include <linux/slab.h>
  43#include "hisax.h"
  44#include "hisax_if.h"
  45#include "hfc_usb.h"
  46
  47static const char *hfcusb_revision =
  48        "$Revision: 2.3.2.24 $ $Date: 2007/10/14 08:40:29 $ ";
  49
  50/* Hisax debug support
  51 *  debug flags defined in hfc_usb.h as HFCUSB_DBG_[*]
  52 */
  53#define __debug_variable hfc_debug
  54#include "hisax_debug.h"
  55static u_int debug;
  56module_param(debug, uint, 0);
  57static int hfc_debug;
  58
  59
  60/* private vendor specific data */
  61typedef struct {
  62        __u8 led_scheme;        // led display scheme
  63        signed short led_bits[8];       // array of 8 possible LED bitmask settings
  64        char *vend_name;        // device name
  65} hfcsusb_vdata;
  66
  67/* VID/PID device list */
  68static const struct usb_device_id hfcusb_idtab[] = {
  69        {
  70                USB_DEVICE(0x0959, 0x2bd0),
  71                .driver_info = (unsigned long) &((hfcsusb_vdata)
  72                        {LED_OFF, {4, 0, 2, 1},
  73                                        "ISDN USB TA (Cologne Chip HFC-S USB based)"}),
  74        },
  75        {
  76                USB_DEVICE(0x0675, 0x1688),
  77                .driver_info = (unsigned long) &((hfcsusb_vdata)
  78                        {LED_SCHEME1, {1, 2, 0, 0},
  79                                        "DrayTek miniVigor 128 USB ISDN TA"}),
  80        },
  81        {
  82                USB_DEVICE(0x07b0, 0x0007),
  83                .driver_info = (unsigned long) &((hfcsusb_vdata)
  84                        {LED_SCHEME1, {0x80, -64, -32, -16},
  85                                        "Billion tiny USB ISDN TA 128"}),
  86        },
  87        {
  88                USB_DEVICE(0x0742, 0x2008),
  89                .driver_info = (unsigned long) &((hfcsusb_vdata)
  90                        {LED_SCHEME1, {4, 0, 2, 1},
  91                                        "Stollmann USB TA"}),
  92        },
  93        {
  94                USB_DEVICE(0x0742, 0x2009),
  95                .driver_info = (unsigned long) &((hfcsusb_vdata)
  96                        {LED_SCHEME1, {4, 0, 2, 1},
  97                                        "Aceex USB ISDN TA"}),
  98        },
  99        {
 100                USB_DEVICE(0x0742, 0x200A),
 101                .driver_info = (unsigned long) &((hfcsusb_vdata)
 102                        {LED_SCHEME1, {4, 0, 2, 1},
 103                                        "OEM USB ISDN TA"}),
 104        },
 105        {
 106                USB_DEVICE(0x08e3, 0x0301),
 107                .driver_info = (unsigned long) &((hfcsusb_vdata)
 108                        {LED_SCHEME1, {2, 0, 1, 4},
 109                                        "Olitec USB RNIS"}),
 110        },
 111        {
 112                USB_DEVICE(0x07fa, 0x0846),
 113                .driver_info = (unsigned long) &((hfcsusb_vdata)
 114                        {LED_SCHEME1, {0x80, -64, -32, -16},
 115                                        "Bewan Modem RNIS USB"}),
 116        },
 117        {
 118                USB_DEVICE(0x07fa, 0x0847),
 119                .driver_info = (unsigned long) &((hfcsusb_vdata)
 120                        {LED_SCHEME1, {0x80, -64, -32, -16},
 121                                        "Djinn Numeris USB"}),
 122        },
 123        {
 124                USB_DEVICE(0x07b0, 0x0006),
 125                .driver_info = (unsigned long) &((hfcsusb_vdata)
 126                        {LED_SCHEME1, {0x80, -64, -32, -16},
 127                                        "Twister ISDN TA"}),
 128        },
 129        {
 130                USB_DEVICE(0x071d, 0x1005),
 131                .driver_info = (unsigned long) &((hfcsusb_vdata)
 132                        {LED_SCHEME1, {0x02, 0, 0x01, 0x04},
 133                                        "Eicon DIVA USB 4.0"}),
 134        },
 135        { }
 136};
 137
 138/* structure defining input+output fifos (interrupt/bulk mode) */
 139struct usb_fifo;                /* forward definition */
 140typedef struct iso_urb_struct {
 141        struct urb *purb;
 142        __u8 buffer[ISO_BUFFER_SIZE];   /* buffer incoming/outgoing data */
 143        struct usb_fifo *owner_fifo;    /* pointer to owner fifo */
 144} iso_urb_struct;
 145
 146struct hfcusb_data;             /* forward definition */
 147
 148typedef struct usb_fifo {
 149        int fifonum;            /* fifo index attached to this structure */
 150        int active;             /* fifo is currently active */
 151        struct hfcusb_data *hfc;        /* pointer to main structure */
 152        int pipe;               /* address of endpoint */
 153        __u8 usb_packet_maxlen; /* maximum length for usb transfer */
 154        unsigned int max_size;  /* maximum size of receive/send packet */
 155        __u8 intervall;         /* interrupt interval */
 156        struct sk_buff *skbuff; /* actual used buffer */
 157        struct urb *urb;        /* transfer structure for usb routines */
 158        __u8 buffer[128];       /* buffer incoming/outgoing data */
 159        int bit_line;           /* how much bits are in the fifo? */
 160
 161        volatile __u8 usb_transfer_mode;        /* switched between ISO and INT */
 162        iso_urb_struct iso[2];  /* need two urbs to have one always for pending */
 163        struct hisax_if *hif;   /* hisax interface */
 164        int delete_flg;         /* only delete skbuff once */
 165        int last_urblen;        /* remember length of last packet */
 166} usb_fifo;
 167
 168/* structure holding all data for one device */
 169typedef struct hfcusb_data {
 170        /* HiSax Interface for loadable Layer1 drivers */
 171        struct hisax_d_if d_if;         /* see hisax_if.h */
 172        struct hisax_b_if b_if[2];      /* see hisax_if.h */
 173        int protocol;
 174
 175        struct usb_device *dev; /* our device */
 176        int if_used;            /* used interface number */
 177        int alt_used;           /* used alternate config */
 178        int ctrl_paksize;       /* control pipe packet size */
 179        int ctrl_in_pipe,       /* handles for control pipe */
 180                ctrl_out_pipe;
 181        int cfg_used;           /* configuration index used */
 182        int vend_idx;           /* vendor found */
 183        int b_mode[2];          /* B-channel mode */
 184        int l1_activated;       /* layer 1 activated */
 185        int disc_flag;          /* TRUE if device was disonnected to avoid some USB actions */
 186        int packet_size, iso_packet_size;
 187
 188        /* control pipe background handling */
 189        ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE];  /* buffer holding queued data */
 190        volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt;       /* input/output pointer + count */
 191        struct urb *ctrl_urb;   /* transfer structure for control channel */
 192
 193        struct usb_ctrlrequest ctrl_write;      /* buffer for control write request */
 194        struct usb_ctrlrequest ctrl_read;       /* same for read request */
 195
 196        __u8 old_led_state, led_state;
 197
 198        volatile __u8 threshold_mask;   /* threshold actually reported */
 199        volatile __u8 bch_enables;      /* or mask for sctrl_r and sctrl register values */
 200
 201        usb_fifo fifos[HFCUSB_NUM_FIFOS];       /* structure holding all fifo data */
 202
 203        volatile __u8 l1_state; /* actual l1 state */
 204        struct timer_list t3_timer;     /* timer 3 for activation/deactivation */
 205        struct timer_list t4_timer;     /* timer 4 for activation/deactivation */
 206} hfcusb_data;
 207
 208
 209static void collect_rx_frame(usb_fifo *fifo, __u8 *data, int len,
 210                             int finish);
 211
 212static inline const char *
 213symbolic(struct hfcusb_symbolic_list list[], const int num)
 214{
 215        int i;
 216        for (i = 0; list[i].name != NULL; i++)
 217                if (list[i].num == num)
 218                        return (list[i].name);
 219        return "<unknown ERROR>";
 220}
 221
 222static void
 223ctrl_start_transfer(hfcusb_data *hfc)
 224{
 225        if (hfc->ctrl_cnt) {
 226                hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
 227                hfc->ctrl_urb->setup_packet = (u_char *)&hfc->ctrl_write;
 228                hfc->ctrl_urb->transfer_buffer = NULL;
 229                hfc->ctrl_urb->transfer_buffer_length = 0;
 230                hfc->ctrl_write.wIndex =
 231                        cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
 232                hfc->ctrl_write.wValue =
 233                        cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
 234
 235                usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC);      /* start transfer */
 236        }
 237}                               /* ctrl_start_transfer */
 238
 239static int
 240queue_control_request(hfcusb_data *hfc, __u8 reg, __u8 val, int action)
 241{
 242        ctrl_buft *buf;
 243
 244        if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
 245                return (1);     /* no space left */
 246        buf = &hfc->ctrl_buff[hfc->ctrl_in_idx];        /* pointer to new index */
 247        buf->hfc_reg = reg;
 248        buf->reg_val = val;
 249        buf->action = action;
 250        if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
 251                hfc->ctrl_in_idx = 0;   /* pointer wrap */
 252        if (++hfc->ctrl_cnt == 1)
 253                ctrl_start_transfer(hfc);
 254        return (0);
 255}
 256
 257static void
 258ctrl_complete(struct urb *urb)
 259{
 260        hfcusb_data *hfc = (hfcusb_data *) urb->context;
 261
 262        urb->dev = hfc->dev;
 263        if (hfc->ctrl_cnt) {
 264                hfc->ctrl_cnt--;        /* decrement actual count */
 265                if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
 266                        hfc->ctrl_out_idx = 0;  /* pointer wrap */
 267
 268                ctrl_start_transfer(hfc);       /* start next transfer */
 269        }
 270}
 271
 272/* write led data to auxport & invert if necessary */
 273static void
 274write_led(hfcusb_data *hfc, __u8 led_state)
 275{
 276        if (led_state != hfc->old_led_state) {
 277                hfc->old_led_state = led_state;
 278                queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
 279        }
 280}
 281
 282static void
 283set_led_bit(hfcusb_data *hfc, signed short led_bits, int on)
 284{
 285        if (on) {
 286                if (led_bits < 0)
 287                        hfc->led_state &= ~abs(led_bits);
 288                else
 289                        hfc->led_state |= led_bits;
 290        } else {
 291                if (led_bits < 0)
 292                        hfc->led_state |= abs(led_bits);
 293                else
 294                        hfc->led_state &= ~led_bits;
 295        }
 296}
 297
 298/* handle LED requests */
 299static void
 300handle_led(hfcusb_data *hfc, int event)
 301{
 302        hfcsusb_vdata *driver_info =
 303                (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
 304
 305        /* if no scheme -> no LED action */
 306        if (driver_info->led_scheme == LED_OFF)
 307                return;
 308
 309        switch (event) {
 310        case LED_POWER_ON:
 311                set_led_bit(hfc, driver_info->led_bits[0], 1);
 312                set_led_bit(hfc, driver_info->led_bits[1], 0);
 313                set_led_bit(hfc, driver_info->led_bits[2], 0);
 314                set_led_bit(hfc, driver_info->led_bits[3], 0);
 315                break;
 316        case LED_POWER_OFF:
 317                set_led_bit(hfc, driver_info->led_bits[0], 0);
 318                set_led_bit(hfc, driver_info->led_bits[1], 0);
 319                set_led_bit(hfc, driver_info->led_bits[2], 0);
 320                set_led_bit(hfc, driver_info->led_bits[3], 0);
 321                break;
 322        case LED_S0_ON:
 323                set_led_bit(hfc, driver_info->led_bits[1], 1);
 324                break;
 325        case LED_S0_OFF:
 326                set_led_bit(hfc, driver_info->led_bits[1], 0);
 327                break;
 328        case LED_B1_ON:
 329                set_led_bit(hfc, driver_info->led_bits[2], 1);
 330                break;
 331        case LED_B1_OFF:
 332                set_led_bit(hfc, driver_info->led_bits[2], 0);
 333                break;
 334        case LED_B2_ON:
 335                set_led_bit(hfc, driver_info->led_bits[3], 1);
 336                break;
 337        case LED_B2_OFF:
 338                set_led_bit(hfc, driver_info->led_bits[3], 0);
 339                break;
 340        }
 341        write_led(hfc, hfc->led_state);
 342}
 343
 344/* ISDN l1 timer T3 expires */
 345static void
 346l1_timer_expire_t3(struct timer_list *t)
 347{
 348        hfcusb_data *hfc = from_timer(hfc, t, t3_timer);
 349        hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
 350                           NULL);
 351
 352        DBG(HFCUSB_DBG_STATES,
 353            "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
 354
 355        hfc->l1_activated = 0;
 356        handle_led(hfc, LED_S0_OFF);
 357        /* deactivate : */
 358        queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
 359        queue_control_request(hfc, HFCUSB_STATES, 3, 1);
 360}
 361
 362/* ISDN l1 timer T4 expires */
 363static void
 364l1_timer_expire_t4(struct timer_list *t)
 365{
 366        hfcusb_data *hfc = from_timer(hfc, t, t4_timer);
 367        hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
 368                           NULL);
 369
 370        DBG(HFCUSB_DBG_STATES,
 371            "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
 372
 373        hfc->l1_activated = 0;
 374        handle_led(hfc, LED_S0_OFF);
 375}
 376
 377/* S0 state changed */
 378static void
 379s0_state_handler(hfcusb_data *hfc, __u8 state)
 380{
 381        __u8 old_state;
 382
 383        old_state = hfc->l1_state;
 384        if (state == old_state || state < 1 || state > 8)
 385                return;
 386
 387        DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
 388            old_state, state);
 389
 390        if (state < 4 || state == 7 || state == 8) {
 391                if (timer_pending(&hfc->t3_timer))
 392                        del_timer(&hfc->t3_timer);
 393                DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
 394        }
 395        if (state >= 7) {
 396                if (timer_pending(&hfc->t4_timer))
 397                        del_timer(&hfc->t4_timer);
 398                DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
 399        }
 400
 401        if (state == 7 && !hfc->l1_activated) {
 402                hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
 403                                   PH_ACTIVATE | INDICATION, NULL);
 404                DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
 405                hfc->l1_activated = 1;
 406                handle_led(hfc, LED_S0_ON);
 407        } else if (state <= 3 /* && activated */) {
 408                if (old_state == 7 || old_state == 8) {
 409                        DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
 410                        if (!timer_pending(&hfc->t4_timer)) {
 411                                hfc->t4_timer.expires =
 412                                        jiffies + (HFC_TIMER_T4 * HZ) / 1000;
 413                                add_timer(&hfc->t4_timer);
 414                        }
 415                } else {
 416                        hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
 417                                           PH_DEACTIVATE | INDICATION,
 418                                           NULL);
 419                        DBG(HFCUSB_DBG_STATES,
 420                            "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
 421                        hfc->l1_activated = 0;
 422                        handle_led(hfc, LED_S0_OFF);
 423                }
 424        }
 425        hfc->l1_state = state;
 426}
 427
 428static void
 429fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
 430              void *buf, int num_packets, int packet_size, int interval,
 431              usb_complete_t complete, void *context)
 432{
 433        int k;
 434
 435        urb->dev = dev;
 436        urb->pipe = pipe;
 437        urb->complete = complete;
 438        urb->number_of_packets = num_packets;
 439        urb->transfer_buffer_length = packet_size * num_packets;
 440        urb->context = context;
 441        urb->transfer_buffer = buf;
 442        urb->transfer_flags = URB_ISO_ASAP;
 443        urb->actual_length = 0;
 444        urb->interval = interval;
 445        for (k = 0; k < num_packets; k++) {
 446                urb->iso_frame_desc[k].offset = packet_size * k;
 447                urb->iso_frame_desc[k].length = packet_size;
 448                urb->iso_frame_desc[k].actual_length = 0;
 449        }
 450}
 451
 452/* allocs urbs and start isoc transfer with two pending urbs to avoid
 453 * gaps in the transfer chain
 454 */
 455static int
 456start_isoc_chain(usb_fifo *fifo, int num_packets_per_urb,
 457                 usb_complete_t complete, int packet_size)
 458{
 459        int i, k, errcode;
 460
 461        DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
 462            fifo->fifonum);
 463
 464        /* allocate Memory for Iso out Urbs */
 465        for (i = 0; i < 2; i++) {
 466                if (!(fifo->iso[i].purb)) {
 467                        fifo->iso[i].purb =
 468                                usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
 469                        if (!(fifo->iso[i].purb)) {
 470                                printk(KERN_INFO
 471                                       "alloc urb for fifo %i failed!!!",
 472                                       fifo->fifonum);
 473                        }
 474                        fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
 475
 476                        /* Init the first iso */
 477                        if (ISO_BUFFER_SIZE >=
 478                            (fifo->usb_packet_maxlen *
 479                             num_packets_per_urb)) {
 480                                fill_isoc_urb(fifo->iso[i].purb,
 481                                              fifo->hfc->dev, fifo->pipe,
 482                                              fifo->iso[i].buffer,
 483                                              num_packets_per_urb,
 484                                              fifo->usb_packet_maxlen,
 485                                              fifo->intervall, complete,
 486                                              &fifo->iso[i]);
 487                                memset(fifo->iso[i].buffer, 0,
 488                                       sizeof(fifo->iso[i].buffer));
 489                                /* defining packet delimeters in fifo->buffer */
 490                                for (k = 0; k < num_packets_per_urb; k++) {
 491                                        fifo->iso[i].purb->
 492                                                iso_frame_desc[k].offset =
 493                                                k * packet_size;
 494                                        fifo->iso[i].purb->
 495                                                iso_frame_desc[k].length =
 496                                                packet_size;
 497                                }
 498                        } else {
 499                                printk(KERN_INFO
 500                                       "HFC-S USB: ISO Buffer size to small!\n");
 501                        }
 502                }
 503                fifo->bit_line = BITLINE_INF;
 504
 505                errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
 506                fifo->active = (errcode >= 0) ? 1 : 0;
 507                if (errcode < 0)
 508                        printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
 509                               i, errcode, symbolic(urb_errlist, errcode));
 510        }
 511        return (fifo->active);
 512}
 513
 514/* stops running iso chain and frees their pending urbs */
 515static void
 516stop_isoc_chain(usb_fifo *fifo)
 517{
 518        int i;
 519
 520        for (i = 0; i < 2; i++) {
 521                if (fifo->iso[i].purb) {
 522                        DBG(HFCUSB_DBG_INIT,
 523                            "HFC-S USB: Stopping iso chain for fifo %i.%i",
 524                            fifo->fifonum, i);
 525                        usb_kill_urb(fifo->iso[i].purb);
 526                        usb_free_urb(fifo->iso[i].purb);
 527                        fifo->iso[i].purb = NULL;
 528                }
 529        }
 530
 531        usb_kill_urb(fifo->urb);
 532        usb_free_urb(fifo->urb);
 533        fifo->urb = NULL;
 534        fifo->active = 0;
 535}
 536
 537/* defines how much ISO packets are handled in one URB */
 538static int iso_packets[8] =
 539{ ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
 540  ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
 541};
 542
 543static void
 544tx_iso_complete(struct urb *urb)
 545{
 546        iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
 547        usb_fifo *fifo = context_iso_urb->owner_fifo;
 548        hfcusb_data *hfc = fifo->hfc;
 549        int k, tx_offset, num_isoc_packets, sink, len, current_len,
 550                errcode;
 551        int frame_complete, transp_mode, fifon, status;
 552        __u8 threshbit;
 553
 554        fifon = fifo->fifonum;
 555        status = urb->status;
 556
 557        tx_offset = 0;
 558
 559        /* ISO transfer only partially completed,
 560           look at individual frame status for details */
 561        if (status == -EXDEV) {
 562                DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
 563                    ", urb->status %d, fifonum %d\n",
 564                    status, fifon);
 565
 566                for (k = 0; k < iso_packets[fifon]; ++k) {
 567                        errcode = urb->iso_frame_desc[k].status;
 568                        if (errcode)
 569                                DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
 570                                    "packet %i, status: %i\n",
 571                                    k, errcode);
 572                }
 573
 574                // clear status, so go on with ISO transfers
 575                status = 0;
 576        }
 577
 578        if (fifo->active && !status) {
 579                transp_mode = 0;
 580                if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
 581                        transp_mode = 1;
 582
 583                /* is FifoFull-threshold set for our channel? */
 584                threshbit = (hfc->threshold_mask & (1 << fifon));
 585                num_isoc_packets = iso_packets[fifon];
 586
 587                /* predict dataflow to avoid fifo overflow */
 588                if (fifon >= HFCUSB_D_TX) {
 589                        sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
 590                } else {
 591                        sink = (threshbit) ? SINK_MIN : SINK_MAX;
 592                }
 593                fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
 594                              context_iso_urb->buffer, num_isoc_packets,
 595                              fifo->usb_packet_maxlen, fifo->intervall,
 596                              tx_iso_complete, urb->context);
 597                memset(context_iso_urb->buffer, 0,
 598                       sizeof(context_iso_urb->buffer));
 599                frame_complete = 0;
 600
 601                /* Generate next ISO Packets */
 602                for (k = 0; k < num_isoc_packets; ++k) {
 603                        if (fifo->skbuff) {
 604                                len = fifo->skbuff->len;
 605                                /* we lower data margin every msec */
 606                                fifo->bit_line -= sink;
 607                                current_len = (0 - fifo->bit_line) / 8;
 608                                /* maximum 15 byte for every ISO packet makes our life easier */
 609                                if (current_len > 14)
 610                                        current_len = 14;
 611                                current_len =
 612                                        (len <=
 613                                         current_len) ? len : current_len;
 614                                /* how much bit do we put on the line? */
 615                                fifo->bit_line += current_len * 8;
 616
 617                                context_iso_urb->buffer[tx_offset] = 0;
 618                                if (current_len == len) {
 619                                        if (!transp_mode) {
 620                                                /* here frame completion */
 621                                                context_iso_urb->
 622                                                        buffer[tx_offset] = 1;
 623                                                /* add 2 byte flags and 16bit CRC at end of ISDN frame */
 624                                                fifo->bit_line += 32;
 625                                        }
 626                                        frame_complete = 1;
 627                                }
 628
 629                                memcpy(context_iso_urb->buffer +
 630                                       tx_offset + 1, fifo->skbuff->data,
 631                                       current_len);
 632                                skb_pull(fifo->skbuff, current_len);
 633
 634                                /* define packet delimeters within the URB buffer */
 635                                urb->iso_frame_desc[k].offset = tx_offset;
 636                                urb->iso_frame_desc[k].length =
 637                                        current_len + 1;
 638
 639                                tx_offset += (current_len + 1);
 640                        } else {
 641                                urb->iso_frame_desc[k].offset =
 642                                        tx_offset++;
 643
 644                                urb->iso_frame_desc[k].length = 1;
 645                                fifo->bit_line -= sink; /* we lower data margin every msec */
 646
 647                                if (fifo->bit_line < BITLINE_INF) {
 648                                        fifo->bit_line = BITLINE_INF;
 649                                }
 650                        }
 651
 652                        if (frame_complete) {
 653                                fifo->delete_flg = 1;
 654                                fifo->hif->l1l2(fifo->hif,
 655                                                PH_DATA | CONFIRM,
 656                                                (void *) (unsigned long) fifo->skbuff->
 657                                                truesize);
 658                                if (fifo->skbuff && fifo->delete_flg) {
 659                                        dev_kfree_skb_any(fifo->skbuff);
 660                                        fifo->skbuff = NULL;
 661                                        fifo->delete_flg = 0;
 662                                }
 663                                frame_complete = 0;
 664                        }
 665                }
 666                errcode = usb_submit_urb(urb, GFP_ATOMIC);
 667                if (errcode < 0) {
 668                        printk(KERN_INFO
 669                               "HFC-S USB: error submitting ISO URB: %d\n",
 670                               errcode);
 671                }
 672        } else {
 673                if (status && !hfc->disc_flag) {
 674                        printk(KERN_INFO
 675                               "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
 676                               status, symbolic(urb_errlist, status), fifon);
 677                }
 678        }
 679}
 680
 681static void
 682rx_iso_complete(struct urb *urb)
 683{
 684        iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
 685        usb_fifo *fifo = context_iso_urb->owner_fifo;
 686        hfcusb_data *hfc = fifo->hfc;
 687        int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
 688                status;
 689        unsigned int iso_status;
 690        __u8 *buf;
 691        static __u8 eof[8];
 692
 693        fifon = fifo->fifonum;
 694        status = urb->status;
 695
 696        if (urb->status == -EOVERFLOW) {
 697                DBG(HFCUSB_DBG_VERBOSE_USB,
 698                    "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
 699                status = 0;
 700        }
 701
 702        /* ISO transfer only partially completed,
 703           look at individual frame status for details */
 704        if (status == -EXDEV) {
 705                DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
 706                    "urb->status %d, fifonum %d\n",
 707                    status, fifon);
 708                status = 0;
 709        }
 710
 711        if (fifo->active && !status) {
 712                num_isoc_packets = iso_packets[fifon];
 713                maxlen = fifo->usb_packet_maxlen;
 714                for (k = 0; k < num_isoc_packets; ++k) {
 715                        len = urb->iso_frame_desc[k].actual_length;
 716                        offset = urb->iso_frame_desc[k].offset;
 717                        buf = context_iso_urb->buffer + offset;
 718                        iso_status = urb->iso_frame_desc[k].status;
 719
 720                        if (iso_status && !hfc->disc_flag)
 721                                DBG(HFCUSB_DBG_VERBOSE_USB,
 722                                    "HFC-S USB: rx_iso_complete "
 723                                    "ISO packet %i, status: %i\n",
 724                                    k, iso_status);
 725
 726                        if (fifon == HFCUSB_D_RX) {
 727                                DBG(HFCUSB_DBG_VERBOSE_USB,
 728                                    "HFC-S USB: ISO-D-RX lst_urblen:%2d "
 729                                    "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
 730                                    fifo->last_urblen, len, maxlen,
 731                                    eof[5]);
 732
 733                                DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
 734                        }
 735
 736                        if (fifo->last_urblen != maxlen) {
 737                                /* the threshold mask is in the 2nd status byte */
 738                                hfc->threshold_mask = buf[1];
 739                                /* care for L1 state only for D-Channel
 740                                   to avoid overlapped iso completions */
 741                                if (fifon == HFCUSB_D_RX) {
 742                                        /* the S0 state is in the upper half
 743                                           of the 1st status byte */
 744                                        s0_state_handler(hfc, buf[0] >> 4);
 745                                }
 746                                eof[fifon] = buf[0] & 1;
 747                                if (len > 2)
 748                                        collect_rx_frame(fifo, buf + 2,
 749                                                         len - 2,
 750                                                         (len < maxlen) ?
 751                                                         eof[fifon] : 0);
 752                        } else {
 753                                collect_rx_frame(fifo, buf, len,
 754                                                 (len <
 755                                                  maxlen) ? eof[fifon] :
 756                                                 0);
 757                        }
 758                        fifo->last_urblen = len;
 759                }
 760
 761                fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
 762                              context_iso_urb->buffer, num_isoc_packets,
 763                              fifo->usb_packet_maxlen, fifo->intervall,
 764                              rx_iso_complete, urb->context);
 765                errcode = usb_submit_urb(urb, GFP_ATOMIC);
 766                if (errcode < 0) {
 767                        printk(KERN_ERR
 768                               "HFC-S USB: error submitting ISO URB: %d\n",
 769                               errcode);
 770                }
 771        } else {
 772                if (status && !hfc->disc_flag) {
 773                        printk(KERN_ERR
 774                               "HFC-S USB: rx_iso_complete : "
 775                               "urb->status %d, fifonum %d\n",
 776                               status, fifon);
 777                }
 778        }
 779}
 780
 781/* collect rx data from INT- and ISO-URBs  */
 782static void
 783collect_rx_frame(usb_fifo *fifo, __u8 *data, int len, int finish)
 784{
 785        hfcusb_data *hfc = fifo->hfc;
 786        int transp_mode, fifon;
 787
 788        fifon = fifo->fifonum;
 789        transp_mode = 0;
 790        if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
 791                transp_mode = 1;
 792
 793        if (!fifo->skbuff) {
 794                fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
 795                if (!fifo->skbuff) {
 796                        printk(KERN_ERR
 797                               "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
 798                               fifon);
 799                        return;
 800                }
 801        }
 802        if (len) {
 803                if (fifo->skbuff->len + len < fifo->max_size) {
 804                        skb_put_data(fifo->skbuff, data, len);
 805                } else {
 806                        DBG(HFCUSB_DBG_FIFO_ERR,
 807                            "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
 808                            fifo->max_size, fifon);
 809                        DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
 810                        skb_trim(fifo->skbuff, 0);
 811                }
 812        }
 813        if (transp_mode && fifo->skbuff->len >= 128) {
 814                fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
 815                                fifo->skbuff);
 816                fifo->skbuff = NULL;
 817                return;
 818        }
 819        /* we have a complete hdlc packet */
 820        if (finish) {
 821                if (fifo->skbuff->len > 3 &&
 822                    !fifo->skbuff->data[fifo->skbuff->len - 1]) {
 823
 824                        if (fifon == HFCUSB_D_RX) {
 825                                DBG(HFCUSB_DBG_DCHANNEL,
 826                                    "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
 827                                DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
 828                        }
 829
 830                        /* remove CRC & status */
 831                        skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
 832                        if (fifon == HFCUSB_PCM_RX) {
 833                                fifo->hif->l1l2(fifo->hif,
 834                                                PH_DATA_E | INDICATION,
 835                                                fifo->skbuff);
 836                        } else
 837                                fifo->hif->l1l2(fifo->hif,
 838                                                PH_DATA | INDICATION,
 839                                                fifo->skbuff);
 840                        fifo->skbuff = NULL;    /* buffer was freed from upper layer */
 841                } else {
 842                        DBG(HFCUSB_DBG_FIFO_ERR,
 843                            "HFC-S USB: ERROR frame len(%d) fifo(%d)",
 844                            fifo->skbuff->len, fifon);
 845                        DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
 846                        skb_trim(fifo->skbuff, 0);
 847                }
 848        }
 849}
 850
 851static void
 852rx_int_complete(struct urb *urb)
 853{
 854        int len;
 855        int status;
 856        __u8 *buf, maxlen, fifon;
 857        usb_fifo *fifo = (usb_fifo *) urb->context;
 858        hfcusb_data *hfc = fifo->hfc;
 859        static __u8 eof[8];
 860
 861        urb->dev = hfc->dev;    /* security init */
 862
 863        fifon = fifo->fifonum;
 864        if ((!fifo->active) || (urb->status)) {
 865                DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
 866                    fifon, urb->status);
 867
 868                fifo->urb->interval = 0;        /* cancel automatic rescheduling */
 869                if (fifo->skbuff) {
 870                        dev_kfree_skb_any(fifo->skbuff);
 871                        fifo->skbuff = NULL;
 872                }
 873                return;
 874        }
 875        len = urb->actual_length;
 876        buf = fifo->buffer;
 877        maxlen = fifo->usb_packet_maxlen;
 878
 879        if (fifon == HFCUSB_D_RX) {
 880                DBG(HFCUSB_DBG_VERBOSE_USB,
 881                    "HFC-S USB: INT-D-RX lst_urblen:%2d "
 882                    "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
 883                    fifo->last_urblen, len, maxlen,
 884                    eof[5]);
 885                DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
 886        }
 887
 888        if (fifo->last_urblen != fifo->usb_packet_maxlen) {
 889                /* the threshold mask is in the 2nd status byte */
 890                hfc->threshold_mask = buf[1];
 891                /* the S0 state is in the upper half of the 1st status byte */
 892                s0_state_handler(hfc, buf[0] >> 4);
 893                eof[fifon] = buf[0] & 1;
 894                /* if we have more than the 2 status bytes -> collect data */
 895                if (len > 2)
 896                        collect_rx_frame(fifo, buf + 2,
 897                                         urb->actual_length - 2,
 898                                         (len < maxlen) ? eof[fifon] : 0);
 899        } else {
 900                collect_rx_frame(fifo, buf, urb->actual_length,
 901                                 (len < maxlen) ? eof[fifon] : 0);
 902        }
 903        fifo->last_urblen = urb->actual_length;
 904        status = usb_submit_urb(urb, GFP_ATOMIC);
 905        if (status) {
 906                printk(KERN_INFO
 907                       "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
 908                       __func__, fifon);
 909        }
 910}
 911
 912/* start initial INT-URB for certain fifo */
 913static void
 914start_int_fifo(usb_fifo *fifo)
 915{
 916        int errcode;
 917
 918        DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
 919            fifo->fifonum);
 920
 921        if (!fifo->urb) {
 922                fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
 923                if (!fifo->urb)
 924                        return;
 925        }
 926        usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
 927                         fifo->buffer, fifo->usb_packet_maxlen,
 928                         rx_int_complete, fifo, fifo->intervall);
 929        fifo->active = 1;       /* must be marked active */
 930        errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
 931        if (errcode) {
 932                printk(KERN_ERR "HFC-S USB: submit URB error(%s): status:%i\n",
 933                       __func__, errcode);
 934                fifo->active = 0;
 935                fifo->skbuff = NULL;
 936        }
 937}
 938
 939static void
 940setup_bchannel(hfcusb_data *hfc, int channel, int mode)
 941{
 942        __u8 val, idx_table[2] = { 0, 2 };
 943
 944        if (hfc->disc_flag) {
 945                return;
 946        }
 947        DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
 948            channel, mode);
 949        hfc->b_mode[channel] = mode;
 950
 951        /* setup CON_HDLC */
 952        val = 0;
 953        if (mode != L1_MODE_NULL)
 954                val = 8;        /* enable fifo? */
 955        if (mode == L1_MODE_TRANS)
 956                val |= 2;       /* set transparent bit */
 957
 958        /* set FIFO to transmit register */
 959        queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 1);
 960        queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
 961        /* reset fifo */
 962        queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
 963        /* set FIFO to receive register */
 964        queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
 965        queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
 966        /* reset fifo */
 967        queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
 968
 969        val = 0x40;
 970        if (hfc->b_mode[0])
 971                val |= 1;
 972        if (hfc->b_mode[1])
 973                val |= 2;
 974        queue_control_request(hfc, HFCUSB_SCTRL, val, 1);
 975
 976        val = 0;
 977        if (hfc->b_mode[0])
 978                val |= 1;
 979        if (hfc->b_mode[1])
 980                val |= 2;
 981        queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);
 982
 983        if (mode == L1_MODE_NULL) {
 984                if (channel)
 985                        handle_led(hfc, LED_B2_OFF);
 986                else
 987                        handle_led(hfc, LED_B1_OFF);
 988        } else {
 989                if (channel)
 990                        handle_led(hfc, LED_B2_ON);
 991                else
 992                        handle_led(hfc, LED_B1_ON);
 993        }
 994}
 995
 996static void
 997hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
 998{
 999        usb_fifo *fifo = my_hisax_if->priv;
1000        hfcusb_data *hfc = fifo->hfc;
1001
1002        switch (pr) {
1003        case PH_ACTIVATE | REQUEST:
1004                if (fifo->fifonum == HFCUSB_D_TX) {
1005                        DBG(HFCUSB_DBG_STATES,
1006                            "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
1007
1008                        if (hfc->l1_state != 3
1009                            && hfc->l1_state != 7) {
1010                                hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
1011                                                   PH_DEACTIVATE |
1012                                                   INDICATION,
1013                                                   NULL);
1014                                DBG(HFCUSB_DBG_STATES,
1015                                    "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
1016                        } else {
1017                                if (hfc->l1_state == 7) {       /* l1 already active */
1018                                        hfc->d_if.ifc.l1l2(&hfc->
1019                                                           d_if.
1020                                                           ifc,
1021                                                           PH_ACTIVATE
1022                                                           |
1023                                                           INDICATION,
1024                                                           NULL);
1025                                        DBG(HFCUSB_DBG_STATES,
1026                                            "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
1027                                } else {
1028                                        /* force sending sending INFO1 */
1029                                        queue_control_request(hfc,
1030                                                              HFCUSB_STATES,
1031                                                              0x14,
1032                                                              1);
1033                                        mdelay(1);
1034                                        /* start l1 activation */
1035                                        queue_control_request(hfc,
1036                                                              HFCUSB_STATES,
1037                                                              0x04,
1038                                                              1);
1039                                        if (!timer_pending
1040                                            (&hfc->t3_timer)) {
1041                                                hfc->t3_timer.
1042                                                        expires =
1043                                                        jiffies +
1044                                                        (HFC_TIMER_T3 *
1045                                                         HZ) / 1000;
1046                                                add_timer(&hfc->
1047                                                          t3_timer);
1048                                        }
1049                                }
1050                        }
1051                } else {
1052                        DBG(HFCUSB_DBG_STATES,
1053                            "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
1054                        setup_bchannel(hfc,
1055                                       (fifo->fifonum ==
1056                                        HFCUSB_B1_TX) ? 0 : 1,
1057                                       (long) arg);
1058                        fifo->hif->l1l2(fifo->hif,
1059                                        PH_ACTIVATE | INDICATION,
1060                                        NULL);
1061                }
1062                break;
1063        case PH_DEACTIVATE | REQUEST:
1064                if (fifo->fifonum == HFCUSB_D_TX) {
1065                        DBG(HFCUSB_DBG_STATES,
1066                            "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
1067                } else {
1068                        DBG(HFCUSB_DBG_STATES,
1069                            "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
1070                        setup_bchannel(hfc,
1071                                       (fifo->fifonum ==
1072                                        HFCUSB_B1_TX) ? 0 : 1,
1073                                       (int) L1_MODE_NULL);
1074                        fifo->hif->l1l2(fifo->hif,
1075                                        PH_DEACTIVATE | INDICATION,
1076                                        NULL);
1077                }
1078                break;
1079        case PH_DATA | REQUEST:
1080                if (fifo->skbuff && fifo->delete_flg) {
1081                        dev_kfree_skb_any(fifo->skbuff);
1082                        fifo->skbuff = NULL;
1083                        fifo->delete_flg = 0;
1084                }
1085                fifo->skbuff = arg;     /* we have a new buffer */
1086                break;
1087        default:
1088                DBG(HFCUSB_DBG_STATES,
1089                    "HFC_USB: hfc_usb_d_l2l1: unknown state : %#x", pr);
1090                break;
1091        }
1092}
1093
1094/* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
1095static int
1096hfc_usb_init(hfcusb_data *hfc)
1097{
1098        usb_fifo *fifo;
1099        int i;
1100        u_char b;
1101        struct hisax_b_if *p_b_if[2];
1102
1103        /* check the chip id */
1104        if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
1105                printk(KERN_INFO "HFC-USB: cannot read chip id\n");
1106                return (1);
1107        }
1108        if (b != HFCUSB_CHIPID) {
1109                printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
1110                return (1);
1111        }
1112
1113        /* first set the needed config, interface and alternate */
1114        usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1115
1116        /* do Chip reset */
1117        write_usb(hfc, HFCUSB_CIRM, 8);
1118        /* aux = output, reset off */
1119        write_usb(hfc, HFCUSB_CIRM, 0x10);
1120
1121        /* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
1122        write_usb(hfc, HFCUSB_USB_SIZE,
1123                  (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));
1124
1125        /* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
1126        write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
1127
1128        /* enable PCM/GCI master mode */
1129        write_usb(hfc, HFCUSB_MST_MODE1, 0);    /* set default values */
1130        write_usb(hfc, HFCUSB_MST_MODE0, 1);    /* enable master mode */
1131
1132        /* init the fifos */
1133        write_usb(hfc, HFCUSB_F_THRES,
1134                  (HFCUSB_TX_THRESHOLD /
1135                   8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1136
1137        fifo = hfc->fifos;
1138        for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1139                write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
1140                fifo[i].skbuff = NULL;  /* init buffer pointer */
1141                fifo[i].max_size =
1142                        (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1143                fifo[i].last_urblen = 0;
1144                /* set 2 bit for D- & E-channel */
1145                write_usb(hfc, HFCUSB_HDLC_PAR,
1146                          ((i <= HFCUSB_B2_RX) ? 0 : 2));
1147                /* rx hdlc, enable IFF for D-channel */
1148                write_usb(hfc, HFCUSB_CON_HDLC,
1149                          ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
1150                write_usb(hfc, HFCUSB_INC_RES_F, 2);    /* reset the fifo */
1151        }
1152
1153        write_usb(hfc, HFCUSB_CLKDEL, 0x0f);    /* clock delay value */
1154        write_usb(hfc, HFCUSB_STATES, 3 | 0x10);        /* set deactivated mode */
1155        write_usb(hfc, HFCUSB_STATES, 3);       /* enable state machine */
1156
1157        write_usb(hfc, HFCUSB_SCTRL_R, 0);      /* disable both B receivers */
1158        write_usb(hfc, HFCUSB_SCTRL, 0x40);     /* disable B transmitters + capacitive mode */
1159
1160        /* set both B-channel to not connected */
1161        hfc->b_mode[0] = L1_MODE_NULL;
1162        hfc->b_mode[1] = L1_MODE_NULL;
1163
1164        hfc->l1_activated = 0;
1165        hfc->disc_flag = 0;
1166        hfc->led_state = 0;
1167        hfc->old_led_state = 0;
1168
1169        /* init the t3 timer */
1170        timer_setup(&hfc->t3_timer, l1_timer_expire_t3, 0);
1171
1172        /* init the t4 timer */
1173        timer_setup(&hfc->t4_timer, l1_timer_expire_t4, 0);
1174
1175        /* init the background machinery for control requests */
1176        hfc->ctrl_read.bRequestType = 0xc0;
1177        hfc->ctrl_read.bRequest = 1;
1178        hfc->ctrl_read.wLength = cpu_to_le16(1);
1179        hfc->ctrl_write.bRequestType = 0x40;
1180        hfc->ctrl_write.bRequest = 0;
1181        hfc->ctrl_write.wLength = 0;
1182        usb_fill_control_urb(hfc->ctrl_urb,
1183                             hfc->dev,
1184                             hfc->ctrl_out_pipe,
1185                             (u_char *)&hfc->ctrl_write,
1186                             NULL, 0, ctrl_complete, hfc);
1187        /* Init All Fifos */
1188        for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1189                hfc->fifos[i].iso[0].purb = NULL;
1190                hfc->fifos[i].iso[1].purb = NULL;
1191                hfc->fifos[i].active = 0;
1192        }
1193        /* register Modul to upper Hisax Layers */
1194        hfc->d_if.owner = THIS_MODULE;
1195        hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
1196        hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
1197        for (i = 0; i < 2; i++) {
1198                hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
1199                hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
1200                p_b_if[i] = &hfc->b_if[i];
1201        }
1202        /* default Prot: EURO ISDN, should be a module_param */
1203        hfc->protocol = 2;
1204        i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
1205        if (i) {
1206                printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
1207                return i;
1208        }
1209
1210#ifdef CONFIG_HISAX_DEBUG
1211        hfc_debug = debug;
1212#endif
1213
1214        for (i = 0; i < 4; i++)
1215                hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
1216        for (i = 4; i < 8; i++)
1217                hfc->fifos[i].hif = &hfc->d_if.ifc;
1218
1219        /* 3 (+1) INT IN + 3 ISO OUT */
1220        if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
1221                start_int_fifo(hfc->fifos + HFCUSB_D_RX);
1222                if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1223                        start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
1224                start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
1225                start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
1226        }
1227        /* 3 (+1) ISO IN + 3 ISO OUT */
1228        if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
1229                start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
1230                                 rx_iso_complete, 16);
1231                if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1232                        start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
1233                                         ISOC_PACKETS_D, rx_iso_complete,
1234                                         16);
1235                start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
1236                                 rx_iso_complete, 16);
1237                start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
1238                                 rx_iso_complete, 16);
1239        }
1240
1241        start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
1242                         tx_iso_complete, 1);
1243        start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
1244                         tx_iso_complete, 1);
1245        start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
1246                         tx_iso_complete, 1);
1247
1248        handle_led(hfc, LED_POWER_ON);
1249
1250        return (0);
1251}
1252
1253/* initial callback for each plugged USB device */
1254static int
1255hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1256{
1257        struct usb_device *dev = interface_to_usbdev(intf);
1258        hfcusb_data *context;
1259        struct usb_host_interface *iface = intf->cur_altsetting;
1260        struct usb_host_interface *iface_used = NULL;
1261        struct usb_host_endpoint *ep;
1262        int ifnum = iface->desc.bInterfaceNumber;
1263        int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
1264                attr, cfg_found, cidx, ep_addr;
1265        int cmptbl[16], small_match, iso_packet_size, packet_size,
1266                alt_used = 0;
1267        hfcsusb_vdata *driver_info;
1268
1269        vend_idx = 0xffff;
1270        for (i = 0; hfcusb_idtab[i].idVendor; i++) {
1271                if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
1272                    && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
1273                        vend_idx = i;
1274                        continue;
1275                }
1276        }
1277
1278        printk(KERN_INFO
1279               "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
1280               ifnum, iface->desc.bAlternateSetting, intf->minor);
1281
1282        if (vend_idx != 0xffff) {
1283                /* if vendor and product ID is OK, start probing alternate settings */
1284                alt_idx = 0;
1285                small_match = 0xffff;
1286
1287                /* default settings */
1288                iso_packet_size = 16;
1289                packet_size = 64;
1290
1291                while (alt_idx < intf->num_altsetting) {
1292                        iface = intf->altsetting + alt_idx;
1293                        probe_alt_setting = iface->desc.bAlternateSetting;
1294                        cfg_used = 0;
1295
1296                        /* check for config EOL element */
1297                        while (validconf[cfg_used][0]) {
1298                                cfg_found = 1;
1299                                vcf = validconf[cfg_used];
1300                                /* first endpoint descriptor */
1301                                ep = iface->endpoint;
1302
1303                                memcpy(cmptbl, vcf, 16 * sizeof(int));
1304
1305                                /* check for all endpoints in this alternate setting */
1306                                for (i = 0; i < iface->desc.bNumEndpoints;
1307                                     i++) {
1308                                        ep_addr =
1309                                                ep->desc.bEndpointAddress;
1310                                        /* get endpoint base */
1311                                        idx = ((ep_addr & 0x7f) - 1) * 2;
1312                                        if (ep_addr & 0x80)
1313                                                idx++;
1314                                        attr = ep->desc.bmAttributes;
1315                                        if (cmptbl[idx] == EP_NUL) {
1316                                                cfg_found = 0;
1317                                        }
1318                                        if (attr == USB_ENDPOINT_XFER_INT
1319                                            && cmptbl[idx] == EP_INT)
1320                                                cmptbl[idx] = EP_NUL;
1321                                        if (attr == USB_ENDPOINT_XFER_BULK
1322                                            && cmptbl[idx] == EP_BLK)
1323                                                cmptbl[idx] = EP_NUL;
1324                                        if (attr == USB_ENDPOINT_XFER_ISOC
1325                                            && cmptbl[idx] == EP_ISO)
1326                                                cmptbl[idx] = EP_NUL;
1327
1328                                        /* check if all INT endpoints match minimum interval */
1329                                        if ((attr == USB_ENDPOINT_XFER_INT)
1330                                            && (ep->desc.bInterval < vcf[17])) {
1331                                                cfg_found = 0;
1332                                        }
1333                                        ep++;
1334                                }
1335                                for (i = 0; i < 16; i++) {
1336                                        /* all entries must be EP_NOP or EP_NUL for a valid config */
1337                                        if (cmptbl[i] != EP_NOP
1338                                            && cmptbl[i] != EP_NUL)
1339                                                cfg_found = 0;
1340                                }
1341                                if (cfg_found) {
1342                                        if (cfg_used < small_match) {
1343                                                small_match = cfg_used;
1344                                                alt_used =
1345                                                        probe_alt_setting;
1346                                                iface_used = iface;
1347                                        }
1348                                }
1349                                cfg_used++;
1350                        }
1351                        alt_idx++;
1352                } /* (alt_idx < intf->num_altsetting) */
1353
1354                /* found a valid USB Ta Endpint config */
1355                if (small_match != 0xffff) {
1356                        iface = iface_used;
1357                        if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
1358                                return (-ENOMEM);       /* got no mem */
1359
1360                        ep = iface->endpoint;
1361                        vcf = validconf[small_match];
1362
1363                        for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1364                                ep_addr = ep->desc.bEndpointAddress;
1365                                /* get endpoint base */
1366                                idx = ((ep_addr & 0x7f) - 1) * 2;
1367                                if (ep_addr & 0x80)
1368                                        idx++;
1369                                cidx = idx & 7;
1370                                attr = ep->desc.bmAttributes;
1371
1372                                /* init Endpoints */
1373                                if (vcf[idx] != EP_NOP
1374                                    && vcf[idx] != EP_NUL) {
1375                                        switch (attr) {
1376                                        case USB_ENDPOINT_XFER_INT:
1377                                                context->
1378                                                        fifos[cidx].
1379                                                        pipe =
1380                                                        usb_rcvintpipe
1381                                                        (dev,
1382                                                         ep->desc.
1383                                                         bEndpointAddress);
1384                                                context->
1385                                                        fifos[cidx].
1386                                                        usb_transfer_mode
1387                                                        = USB_INT;
1388                                                packet_size =
1389                                                        le16_to_cpu(ep->desc.wMaxPacketSize);
1390                                                break;
1391                                        case USB_ENDPOINT_XFER_BULK:
1392                                                if (ep_addr & 0x80)
1393                                                        context->
1394                                                                fifos
1395                                                                [cidx].
1396                                                                pipe =
1397                                                                usb_rcvbulkpipe
1398                                                                (dev,
1399                                                                 ep->
1400                                                                 desc.
1401                                                                 bEndpointAddress);
1402                                                else
1403                                                        context->
1404                                                                fifos
1405                                                                [cidx].
1406                                                                pipe =
1407                                                                usb_sndbulkpipe
1408                                                                (dev,
1409                                                                 ep->
1410                                                                 desc.
1411                                                                 bEndpointAddress);
1412                                                context->
1413                                                        fifos[cidx].
1414                                                        usb_transfer_mode
1415                                                        = USB_BULK;
1416                                                packet_size =
1417                                                        le16_to_cpu(ep->desc.wMaxPacketSize);
1418                                                break;
1419                                        case USB_ENDPOINT_XFER_ISOC:
1420                                                if (ep_addr & 0x80)
1421                                                        context->
1422                                                                fifos
1423                                                                [cidx].
1424                                                                pipe =
1425                                                                usb_rcvisocpipe
1426                                                                (dev,
1427                                                                 ep->
1428                                                                 desc.
1429                                                                 bEndpointAddress);
1430                                                else
1431                                                        context->
1432                                                                fifos
1433                                                                [cidx].
1434                                                                pipe =
1435                                                                usb_sndisocpipe
1436                                                                (dev,
1437                                                                 ep->
1438                                                                 desc.
1439                                                                 bEndpointAddress);
1440                                                context->
1441                                                        fifos[cidx].
1442                                                        usb_transfer_mode
1443                                                        = USB_ISOC;
1444                                                iso_packet_size =
1445                                                        le16_to_cpu(ep->desc.wMaxPacketSize);
1446                                                break;
1447                                        default:
1448                                                context->
1449                                                        fifos[cidx].
1450                                                        pipe = 0;
1451                                        }       /* switch attribute */
1452
1453                                        if (context->fifos[cidx].pipe) {
1454                                                context->fifos[cidx].
1455                                                        fifonum = cidx;
1456                                                context->fifos[cidx].hfc =
1457                                                        context;
1458                                                context->fifos[cidx].usb_packet_maxlen =
1459                                                        le16_to_cpu(ep->desc.wMaxPacketSize);
1460                                                context->fifos[cidx].
1461                                                        intervall =
1462                                                        ep->desc.bInterval;
1463                                                context->fifos[cidx].
1464                                                        skbuff = NULL;
1465                                        }
1466                                }
1467                                ep++;
1468                        }
1469                        context->dev = dev;     /* save device */
1470                        context->if_used = ifnum;       /* save used interface */
1471                        context->alt_used = alt_used;   /* and alternate config */
1472                        context->ctrl_paksize = dev->descriptor.bMaxPacketSize0;        /* control size */
1473                        context->cfg_used = vcf[16];    /* store used config */
1474                        context->vend_idx = vend_idx;   /* store found vendor */
1475                        context->packet_size = packet_size;
1476                        context->iso_packet_size = iso_packet_size;
1477
1478                        /* create the control pipes needed for register access */
1479                        context->ctrl_in_pipe =
1480                                usb_rcvctrlpipe(context->dev, 0);
1481                        context->ctrl_out_pipe =
1482                                usb_sndctrlpipe(context->dev, 0);
1483
1484                        driver_info = (hfcsusb_vdata *)
1485                                      hfcusb_idtab[vend_idx].driver_info;
1486
1487                        context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1488
1489                        if (!context->ctrl_urb) {
1490                                pr_warn("%s: No memory for control urb\n",
1491                                        driver_info->vend_name);
1492                                kfree(context);
1493                                return -ENOMEM;
1494                        }
1495
1496                        pr_info("HFC-S USB: detected \"%s\"\n",
1497                                driver_info->vend_name);
1498
1499                        DBG(HFCUSB_DBG_INIT,
1500                            "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
1501                            conf_str[small_match], context->if_used,
1502                            context->alt_used,
1503                            validconf[small_match][18]);
1504
1505                        /* init the chip and register the driver */
1506                        if (hfc_usb_init(context)) {
1507                                usb_kill_urb(context->ctrl_urb);
1508                                usb_free_urb(context->ctrl_urb);
1509                                context->ctrl_urb = NULL;
1510                                kfree(context);
1511                                return (-EIO);
1512                        }
1513                        usb_set_intfdata(intf, context);
1514                        return (0);
1515                }
1516        } else {
1517                printk(KERN_INFO
1518                       "HFC-S USB: no valid vendor found in USB descriptor\n");
1519        }
1520        return (-EIO);
1521}
1522
1523/* callback for unplugged USB device */
1524static void
1525hfc_usb_disconnect(struct usb_interface *intf)
1526{
1527        hfcusb_data *context = usb_get_intfdata(intf);
1528        int i;
1529
1530        handle_led(context, LED_POWER_OFF);
1531        schedule_timeout(HZ / 100);
1532
1533        printk(KERN_INFO "HFC-S USB: device disconnect\n");
1534        context->disc_flag = 1;
1535        usb_set_intfdata(intf, NULL);
1536
1537        if (timer_pending(&context->t3_timer))
1538                del_timer(&context->t3_timer);
1539        if (timer_pending(&context->t4_timer))
1540                del_timer(&context->t4_timer);
1541
1542        /* tell all fifos to terminate */
1543        for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1544                if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
1545                        if (context->fifos[i].active > 0) {
1546                                stop_isoc_chain(&context->fifos[i]);
1547                                DBG(HFCUSB_DBG_INIT,
1548                                    "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
1549                                    __func__, i);
1550                        }
1551                } else {
1552                        if (context->fifos[i].active > 0) {
1553                                context->fifos[i].active = 0;
1554                                DBG(HFCUSB_DBG_INIT,
1555                                    "HFC-S USB: %s unlinking URB for Fifo(%i)",
1556                                    __func__, i);
1557                        }
1558                        usb_kill_urb(context->fifos[i].urb);
1559                        usb_free_urb(context->fifos[i].urb);
1560                        context->fifos[i].urb = NULL;
1561                }
1562                context->fifos[i].active = 0;
1563        }
1564        usb_kill_urb(context->ctrl_urb);
1565        usb_free_urb(context->ctrl_urb);
1566        context->ctrl_urb = NULL;
1567        hisax_unregister(&context->d_if);
1568        kfree(context);         /* free our structure again */
1569}
1570
1571static struct usb_driver hfc_drv = {
1572        .name  = "hfc_usb",
1573        .id_table = hfcusb_idtab,
1574        .probe = hfc_usb_probe,
1575        .disconnect = hfc_usb_disconnect,
1576        .disable_hub_initiated_lpm = 1,
1577};
1578
1579static void __exit
1580hfc_usb_mod_exit(void)
1581{
1582        usb_deregister(&hfc_drv); /* release our driver */
1583        printk(KERN_INFO "HFC-S USB: module removed\n");
1584}
1585
1586static int __init
1587hfc_usb_mod_init(void)
1588{
1589        char revstr[30], datestr[30], dummy[30];
1590#ifndef CONFIG_HISAX_DEBUG
1591        hfc_debug = debug;
1592#endif
1593        sscanf(hfcusb_revision,
1594               "%s %s $ %s %s %s $ ", dummy, revstr,
1595               dummy, datestr, dummy);
1596        printk(KERN_INFO
1597               "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
1598               revstr, datestr, debug);
1599        if (usb_register(&hfc_drv)) {
1600                printk(KERN_INFO
1601                       "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
1602                return (-1);    /* unable to register */
1603        }
1604        return (0);
1605}
1606
1607module_init(hfc_usb_mod_init);
1608module_exit(hfc_usb_mod_exit);
1609MODULE_AUTHOR(DRIVER_AUTHOR);
1610MODULE_DESCRIPTION(DRIVER_DESC);
1611MODULE_LICENSE("GPL");
1612MODULE_DEVICE_TABLE(usb, hfcusb_idtab);
1613