linux/drivers/usb/serial/io_edgeport.c
<<
>>
Prefs
   1/*
   2 * Edgeport USB Serial Converter driver
   3 *
   4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
   5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.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 of the License, or
  10 *      (at your option) any later version.
  11 *
  12 * Supports the following devices:
  13 *      Edgeport/4
  14 *      Edgeport/4t
  15 *      Edgeport/2
  16 *      Edgeport/4i
  17 *      Edgeport/2i
  18 *      Edgeport/421
  19 *      Edgeport/21
  20 *      Rapidport/4
  21 *      Edgeport/8
  22 *      Edgeport/2D8
  23 *      Edgeport/4D8
  24 *      Edgeport/8i
  25 *
  26 * For questions or problems with this driver, contact Inside Out
  27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
  28 * or Al Borchers <alborchers@steinerpoint.com>.
  29 *
  30 */
  31
  32#include <linux/kernel.h>
  33#include <linux/jiffies.h>
  34#include <linux/errno.h>
  35#include <linux/init.h>
  36#include <linux/slab.h>
  37#include <linux/tty.h>
  38#include <linux/tty_driver.h>
  39#include <linux/tty_flip.h>
  40#include <linux/module.h>
  41#include <linux/spinlock.h>
  42#include <linux/serial.h>
  43#include <linux/ioctl.h>
  44#include <linux/wait.h>
  45#include <linux/firmware.h>
  46#include <linux/ihex.h>
  47#include <linux/uaccess.h>
  48#include <linux/usb.h>
  49#include <linux/usb/serial.h>
  50#include "io_edgeport.h"
  51#include "io_ionsp.h"           /* info for the iosp messages */
  52#include "io_16654.h"           /* 16654 UART defines */
  53
  54/*
  55 * Version Information
  56 */
  57#define DRIVER_VERSION "v2.7"
  58#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  59#define DRIVER_DESC "Edgeport USB Serial Driver"
  60
  61#define MAX_NAME_LEN            64
  62
  63#define CHASE_TIMEOUT           (5*HZ)          /* 5 seconds */
  64#define OPEN_TIMEOUT            (5*HZ)          /* 5 seconds */
  65#define COMMAND_TIMEOUT         (5*HZ)          /* 5 seconds */
  66
  67/* receive port state */
  68enum RXSTATE {
  69        EXPECT_HDR1 = 0,    /* Expect header byte 1 */
  70        EXPECT_HDR2 = 1,    /* Expect header byte 2 */
  71        EXPECT_DATA = 2,    /* Expect 'RxBytesRemaining' data */
  72        EXPECT_HDR3 = 3,    /* Expect header byte 3 (for status hdrs only) */
  73};
  74
  75
  76/* Transmit Fifo
  77 * This Transmit queue is an extension of the edgeport Rx buffer.
  78 * The maximum amount of data buffered in both the edgeport
  79 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
  80 */
  81struct TxFifo {
  82        unsigned int    head;   /* index to head pointer (write) */
  83        unsigned int    tail;   /* index to tail pointer (read)  */
  84        unsigned int    count;  /* Bytes in queue */
  85        unsigned int    size;   /* Max size of queue (equal to Max number of TxCredits) */
  86        unsigned char   *fifo;  /* allocated Buffer */
  87};
  88
  89/* This structure holds all of the local port information */
  90struct edgeport_port {
  91        __u16                   txCredits;              /* our current credits for this port */
  92        __u16                   maxTxCredits;           /* the max size of the port */
  93
  94        struct TxFifo           txfifo;                 /* transmit fifo -- size will be maxTxCredits */
  95        struct urb              *write_urb;             /* write URB for this port */
  96        bool                    write_in_progress;      /* 'true' while a write URB is outstanding */
  97        spinlock_t              ep_lock;
  98
  99        __u8                    shadowLCR;              /* last LCR value received */
 100        __u8                    shadowMCR;              /* last MCR value received */
 101        __u8                    shadowMSR;              /* last MSR value received */
 102        __u8                    shadowLSR;              /* last LSR value received */
 103        __u8                    shadowXonChar;          /* last value set as XON char in Edgeport */
 104        __u8                    shadowXoffChar;         /* last value set as XOFF char in Edgeport */
 105        __u8                    validDataMask;
 106        __u32                   baudRate;
 107
 108        bool                    open;
 109        bool                    openPending;
 110        bool                    commandPending;
 111        bool                    closePending;
 112        bool                    chaseResponsePending;
 113
 114        wait_queue_head_t       wait_chase;             /* for handling sleeping while waiting for chase to finish */
 115        wait_queue_head_t       wait_open;              /* for handling sleeping while waiting for open to finish */
 116        wait_queue_head_t       wait_command;           /* for handling sleeping while waiting for command to finish */
 117        wait_queue_head_t       delta_msr_wait;         /* for handling sleeping while waiting for msr change to happen */
 118
 119        struct async_icount     icount;
 120        struct usb_serial_port  *port;                  /* loop back to the owner of this object */
 121};
 122
 123
 124/* This structure holds all of the individual device information */
 125struct edgeport_serial {
 126        char                    name[MAX_NAME_LEN+2];           /* string name of this device */
 127
 128        struct edge_manuf_descriptor    manuf_descriptor;       /* the manufacturer descriptor */
 129        struct edge_boot_descriptor     boot_descriptor;        /* the boot firmware descriptor */
 130        struct edgeport_product_info    product_info;           /* Product Info */
 131        struct edge_compatibility_descriptor epic_descriptor;   /* Edgeport compatible descriptor */
 132        int                     is_epic;                        /* flag if EPiC device or not */
 133
 134        __u8                    interrupt_in_endpoint;          /* the interrupt endpoint handle */
 135        unsigned char           *interrupt_in_buffer;           /* the buffer we use for the interrupt endpoint */
 136        struct urb              *interrupt_read_urb;            /* our interrupt urb */
 137
 138        __u8                    bulk_in_endpoint;               /* the bulk in endpoint handle */
 139        unsigned char           *bulk_in_buffer;                /* the buffer we use for the bulk in endpoint */
 140        struct urb              *read_urb;                      /* our bulk read urb */
 141        bool                    read_in_progress;
 142        spinlock_t              es_lock;
 143
 144        __u8                    bulk_out_endpoint;              /* the bulk out endpoint handle */
 145
 146        __s16                   rxBytesAvail;                   /* the number of bytes that we need to read from this device */
 147
 148        enum RXSTATE            rxState;                        /* the current state of the bulk receive processor */
 149        __u8                    rxHeader1;                      /* receive header byte 1 */
 150        __u8                    rxHeader2;                      /* receive header byte 2 */
 151        __u8                    rxHeader3;                      /* receive header byte 3 */
 152        __u8                    rxPort;                         /* the port that we are currently receiving data for */
 153        __u8                    rxStatusCode;                   /* the receive status code */
 154        __u8                    rxStatusParam;                  /* the receive status paramater */
 155        __s16                   rxBytesRemaining;               /* the number of port bytes left to read */
 156        struct usb_serial       *serial;                        /* loop back to the owner of this object */
 157};
 158
 159/* baud rate information */
 160struct divisor_table_entry {
 161        __u32   BaudRate;
 162        __u16  Divisor;
 163};
 164
 165/*
 166 * Define table of divisors for Rev A EdgePort/4 hardware
 167 * These assume a 3.6864MHz crystal, the standard /16, and
 168 * MCR.7 = 0.
 169 */
 170
 171static const struct divisor_table_entry divisor_table[] = {
 172        {   50,         4608},
 173        {   75,         3072},
 174        {   110,        2095},  /* 2094.545455 => 230450   => .0217 % over */
 175        {   134,        1713},  /* 1713.011152 => 230398.5 => .00065% under */
 176        {   150,        1536},
 177        {   300,        768},
 178        {   600,        384},
 179        {   1200,       192},
 180        {   1800,       128},
 181        {   2400,       96},
 182        {   4800,       48},
 183        {   7200,       32},
 184        {   9600,       24},
 185        {   14400,      16},
 186        {   19200,      12},
 187        {   38400,      6},
 188        {   57600,      4},
 189        {   115200,     2},
 190        {   230400,     1},
 191};
 192
 193/* local variables */
 194static int debug;
 195
 196static atomic_t CmdUrbs;        /* Number of outstanding Command Write Urbs */
 197
 198
 199/* local function prototypes */
 200
 201/* function prototypes for all URB callbacks */
 202static void edge_interrupt_callback(struct urb *urb);
 203static void edge_bulk_in_callback(struct urb *urb);
 204static void edge_bulk_out_data_callback(struct urb *urb);
 205static void edge_bulk_out_cmd_callback(struct urb *urb);
 206
 207/* function prototypes for the usbserial callbacks */
 208static int edge_open(struct tty_struct *tty, struct usb_serial_port *port,
 209                                        struct file *filp);
 210static void edge_close(struct tty_struct *tty, struct usb_serial_port *port,
 211                                        struct file *filp);
 212static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
 213                                        const unsigned char *buf, int count);
 214static int edge_write_room(struct tty_struct *tty);
 215static int edge_chars_in_buffer(struct tty_struct *tty);
 216static void edge_throttle(struct tty_struct *tty);
 217static void edge_unthrottle(struct tty_struct *tty);
 218static void edge_set_termios(struct tty_struct *tty,
 219                                        struct usb_serial_port *port,
 220                                        struct ktermios *old_termios);
 221static int  edge_ioctl(struct tty_struct *tty, struct file *file,
 222                                        unsigned int cmd, unsigned long arg);
 223static void edge_break(struct tty_struct *tty, int break_state);
 224static int  edge_tiocmget(struct tty_struct *tty, struct file *file);
 225static int  edge_tiocmset(struct tty_struct *tty, struct file *file,
 226                                        unsigned int set, unsigned int clear);
 227static int  edge_startup(struct usb_serial *serial);
 228static void edge_disconnect(struct usb_serial *serial);
 229static void edge_release(struct usb_serial *serial);
 230
 231#include "io_tables.h"  /* all of the devices that this driver supports */
 232
 233/* function prototypes for all of our local functions */
 234
 235static void  process_rcvd_data(struct edgeport_serial *edge_serial,
 236                                unsigned char *buffer, __u16 bufferLength);
 237static void process_rcvd_status(struct edgeport_serial *edge_serial,
 238                                __u8 byte2, __u8 byte3);
 239static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
 240                                unsigned char *data, int length);
 241static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
 242static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
 243                                __u8 lsr, __u8 data);
 244static int  send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
 245                                __u8 param);
 246static int  calc_baud_rate_divisor(int baud_rate, int *divisor);
 247static int  send_cmd_write_baud_rate(struct edgeport_port *edge_port,
 248                                int baudRate);
 249static void change_port_settings(struct tty_struct *tty,
 250                                struct edgeport_port *edge_port,
 251                                struct ktermios *old_termios);
 252static int  send_cmd_write_uart_register(struct edgeport_port *edge_port,
 253                                __u8 regNum, __u8 regValue);
 254static int  write_cmd_usb(struct edgeport_port *edge_port,
 255                                unsigned char *buffer, int writeLength);
 256static void send_more_port_data(struct edgeport_serial *edge_serial,
 257                                struct edgeport_port *edge_port);
 258
 259static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
 260                                        __u16 length, const __u8 *data);
 261static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
 262                                                __u16 length, __u8 *data);
 263static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
 264                                        __u16 length, const __u8 *data);
 265static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
 266static void get_boot_desc(struct edgeport_serial *edge_serial);
 267static void load_application_firmware(struct edgeport_serial *edge_serial);
 268
 269static void unicode_to_ascii(char *string, int buflen,
 270                                __le16 *unicode, int unicode_size);
 271
 272
 273/* ************************************************************************ */
 274/* ************************************************************************ */
 275/* ************************************************************************ */
 276/* ************************************************************************ */
 277
 278/************************************************************************
 279 *                                                                      *
 280 * update_edgeport_E2PROM()     Compare current versions of             *
 281 *                              Boot ROM and Manufacture                *
 282 *                              Descriptors with versions               *
 283 *                              embedded in this driver                 *
 284 *                                                                      *
 285 ************************************************************************/
 286static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
 287{
 288        __u32 BootCurVer;
 289        __u32 BootNewVer;
 290        __u8 BootMajorVersion;
 291        __u8 BootMinorVersion;
 292        __u16 BootBuildNumber;
 293        __u32 Bootaddr;
 294        const struct ihex_binrec *rec;
 295        const struct firmware *fw;
 296        const char *fw_name;
 297        int response;
 298
 299        switch (edge_serial->product_info.iDownloadFile) {
 300        case EDGE_DOWNLOAD_FILE_I930:
 301                fw_name = "edgeport/boot.fw";
 302                break;
 303        case EDGE_DOWNLOAD_FILE_80251:
 304                fw_name = "edgeport/boot2.fw";
 305                break;
 306        default:
 307                return;
 308        }
 309
 310        response = request_ihex_firmware(&fw, fw_name,
 311                                         &edge_serial->serial->dev->dev);
 312        if (response) {
 313                printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
 314                       fw_name, response);
 315                return;
 316        }
 317
 318        rec = (const struct ihex_binrec *)fw->data;
 319        BootMajorVersion = rec->data[0];
 320        BootMinorVersion = rec->data[1];
 321        BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
 322
 323        /* Check Boot Image Version */
 324        BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
 325                     (edge_serial->boot_descriptor.MinorVersion << 16) +
 326                      le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
 327
 328        BootNewVer = (BootMajorVersion << 24) +
 329                     (BootMinorVersion << 16) +
 330                      BootBuildNumber;
 331
 332        dbg("Current Boot Image version %d.%d.%d",
 333            edge_serial->boot_descriptor.MajorVersion,
 334            edge_serial->boot_descriptor.MinorVersion,
 335            le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
 336
 337
 338        if (BootNewVer > BootCurVer) {
 339                dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
 340                    edge_serial->boot_descriptor.MajorVersion,
 341                    edge_serial->boot_descriptor.MinorVersion,
 342                    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
 343                    BootMajorVersion, BootMinorVersion, BootBuildNumber);
 344
 345                dbg("Downloading new Boot Image");
 346
 347                for (rec = ihex_next_binrec(rec); rec;
 348                     rec = ihex_next_binrec(rec)) {
 349                        Bootaddr = be32_to_cpu(rec->addr);
 350                        response = rom_write(edge_serial->serial,
 351                                             Bootaddr >> 16,
 352                                             Bootaddr & 0xFFFF,
 353                                             be16_to_cpu(rec->len),
 354                                             &rec->data[0]);
 355                        if (response < 0) {
 356                                dev_err(&edge_serial->serial->dev->dev,
 357                                        "rom_write failed (%x, %x, %d)\n",
 358                                        Bootaddr >> 16, Bootaddr & 0xFFFF,
 359                                        be16_to_cpu(rec->len));
 360                                break;
 361                        }
 362                }
 363        } else {
 364                dbg("Boot Image -- already up to date");
 365        }
 366        release_firmware(fw);
 367}
 368
 369
 370/************************************************************************
 371 *                                                                      *
 372 *  Get string descriptor from device                                   *
 373 *                                                                      *
 374 ************************************************************************/
 375static int get_string(struct usb_device *dev, int Id, char *string, int buflen)
 376{
 377        struct usb_string_descriptor StringDesc;
 378        struct usb_string_descriptor *pStringDesc;
 379
 380        dbg("%s - USB String ID = %d", __func__, Id);
 381
 382        if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
 383                                        &StringDesc, sizeof(StringDesc)))
 384                return 0;
 385
 386        pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
 387        if (!pStringDesc)
 388                return 0;
 389
 390        if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
 391                                        pStringDesc, StringDesc.bLength)) {
 392                kfree(pStringDesc);
 393                return 0;
 394        }
 395
 396        unicode_to_ascii(string, buflen,
 397                                pStringDesc->wData, pStringDesc->bLength/2);
 398
 399        kfree(pStringDesc);
 400        dbg("%s - USB String %s", __func__, string);
 401        return strlen(string);
 402}
 403
 404
 405#if 0
 406/************************************************************************
 407 *
 408 *  Get string descriptor from device
 409 *
 410 ************************************************************************/
 411static int get_string_desc(struct usb_device *dev, int Id,
 412                                struct usb_string_descriptor **pRetDesc)
 413{
 414        struct usb_string_descriptor StringDesc;
 415        struct usb_string_descriptor *pStringDesc;
 416
 417        dbg("%s - USB String ID = %d", __func__, Id);
 418
 419        if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
 420                                                sizeof(StringDesc)))
 421                return 0;
 422
 423        pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
 424        if (!pStringDesc)
 425                return -1;
 426
 427        if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
 428                                                        StringDesc.bLength)) {
 429                kfree(pStringDesc);
 430                return -1;
 431        }
 432
 433        *pRetDesc = pStringDesc;
 434        return 0;
 435}
 436#endif
 437
 438static void dump_product_info(struct edgeport_product_info *product_info)
 439{
 440        /* Dump Product Info structure */
 441        dbg("**Product Information:");
 442        dbg("  ProductId             %x", product_info->ProductId);
 443        dbg("  NumPorts              %d", product_info->NumPorts);
 444        dbg("  ProdInfoVer           %d", product_info->ProdInfoVer);
 445        dbg("  IsServer              %d", product_info->IsServer);
 446        dbg("  IsRS232               %d", product_info->IsRS232);
 447        dbg("  IsRS422               %d", product_info->IsRS422);
 448        dbg("  IsRS485               %d", product_info->IsRS485);
 449        dbg("  RomSize               %d", product_info->RomSize);
 450        dbg("  RamSize               %d", product_info->RamSize);
 451        dbg("  CpuRev                %x", product_info->CpuRev);
 452        dbg("  BoardRev              %x", product_info->BoardRev);
 453        dbg("  BootMajorVersion      %d.%d.%d", product_info->BootMajorVersion,
 454            product_info->BootMinorVersion,
 455            le16_to_cpu(product_info->BootBuildNumber));
 456        dbg("  FirmwareMajorVersion  %d.%d.%d",
 457                        product_info->FirmwareMajorVersion,
 458                        product_info->FirmwareMinorVersion,
 459                        le16_to_cpu(product_info->FirmwareBuildNumber));
 460        dbg("  ManufactureDescDate   %d/%d/%d",
 461                        product_info->ManufactureDescDate[0],
 462                        product_info->ManufactureDescDate[1],
 463                        product_info->ManufactureDescDate[2]+1900);
 464        dbg("  iDownloadFile         0x%x", product_info->iDownloadFile);
 465        dbg("  EpicVer               %d", product_info->EpicVer);
 466}
 467
 468static void get_product_info(struct edgeport_serial *edge_serial)
 469{
 470        struct edgeport_product_info *product_info = &edge_serial->product_info;
 471
 472        memset(product_info, 0, sizeof(struct edgeport_product_info));
 473
 474        product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
 475        product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
 476        product_info->ProdInfoVer = 0;
 477
 478        product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
 479        product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
 480        product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
 481        product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
 482
 483        product_info->BootMajorVersion =
 484                                edge_serial->boot_descriptor.MajorVersion;
 485        product_info->BootMinorVersion =
 486                                edge_serial->boot_descriptor.MinorVersion;
 487        product_info->BootBuildNumber =
 488                                edge_serial->boot_descriptor.BuildNumber;
 489
 490        memcpy(product_info->ManufactureDescDate,
 491                        edge_serial->manuf_descriptor.DescDate,
 492                        sizeof(edge_serial->manuf_descriptor.DescDate));
 493
 494        /* check if this is 2nd generation hardware */
 495        if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
 496                                            & ION_DEVICE_ID_80251_NETCHIP)
 497                product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
 498        else
 499                product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
 500 
 501        /* Determine Product type and set appropriate flags */
 502        switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
 503        case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
 504        case ION_DEVICE_ID_EDGEPORT_4T:
 505        case ION_DEVICE_ID_EDGEPORT_4:
 506        case ION_DEVICE_ID_EDGEPORT_2:
 507        case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
 508        case ION_DEVICE_ID_EDGEPORT_8:
 509        case ION_DEVICE_ID_EDGEPORT_421:
 510        case ION_DEVICE_ID_EDGEPORT_21:
 511        case ION_DEVICE_ID_EDGEPORT_2_DIN:
 512        case ION_DEVICE_ID_EDGEPORT_4_DIN:
 513        case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
 514                product_info->IsRS232 = 1;
 515                break;
 516
 517        case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
 518                product_info->IsRS422 = 1;
 519                product_info->IsRS485 = 1;
 520                break;
 521
 522        case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
 523        case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
 524                product_info->IsRS422 = 1;
 525                break;
 526        }
 527
 528        dump_product_info(product_info);
 529}
 530
 531static int get_epic_descriptor(struct edgeport_serial *ep)
 532{
 533        int result;
 534        struct usb_serial *serial = ep->serial;
 535        struct edgeport_product_info *product_info = &ep->product_info;
 536        struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
 537        struct edge_compatibility_bits *bits;
 538
 539        ep->is_epic = 0;
 540        result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 541                                 USB_REQUEST_ION_GET_EPIC_DESC,
 542                                 0xC0, 0x00, 0x00,
 543                                 &ep->epic_descriptor,
 544                                 sizeof(struct edge_compatibility_descriptor),
 545                                 300);
 546
 547        dbg("%s result = %d", __func__, result);
 548
 549        if (result > 0) {
 550                ep->is_epic = 1;
 551                memset(product_info, 0, sizeof(struct edgeport_product_info));
 552
 553                product_info->NumPorts = epic->NumPorts;
 554                product_info->ProdInfoVer = 0;
 555                product_info->FirmwareMajorVersion = epic->MajorVersion;
 556                product_info->FirmwareMinorVersion = epic->MinorVersion;
 557                product_info->FirmwareBuildNumber = epic->BuildNumber;
 558                product_info->iDownloadFile = epic->iDownloadFile;
 559                product_info->EpicVer = epic->EpicVer;
 560                product_info->Epic = epic->Supports;
 561                product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
 562                dump_product_info(product_info);
 563
 564                bits = &ep->epic_descriptor.Supports;
 565                dbg("**EPIC descriptor:");
 566                dbg("  VendEnableSuspend: %s", bits->VendEnableSuspend  ? "TRUE": "FALSE");
 567                dbg("  IOSPOpen         : %s", bits->IOSPOpen           ? "TRUE": "FALSE");
 568                dbg("  IOSPClose        : %s", bits->IOSPClose          ? "TRUE": "FALSE");
 569                dbg("  IOSPChase        : %s", bits->IOSPChase          ? "TRUE": "FALSE");
 570                dbg("  IOSPSetRxFlow    : %s", bits->IOSPSetRxFlow      ? "TRUE": "FALSE");
 571                dbg("  IOSPSetTxFlow    : %s", bits->IOSPSetTxFlow      ? "TRUE": "FALSE");
 572                dbg("  IOSPSetXChar     : %s", bits->IOSPSetXChar       ? "TRUE": "FALSE");
 573                dbg("  IOSPRxCheck      : %s", bits->IOSPRxCheck        ? "TRUE": "FALSE");
 574                dbg("  IOSPSetClrBreak  : %s", bits->IOSPSetClrBreak    ? "TRUE": "FALSE");
 575                dbg("  IOSPWriteMCR     : %s", bits->IOSPWriteMCR       ? "TRUE": "FALSE");
 576                dbg("  IOSPWriteLCR     : %s", bits->IOSPWriteLCR       ? "TRUE": "FALSE");
 577                dbg("  IOSPSetBaudRate  : %s", bits->IOSPSetBaudRate    ? "TRUE": "FALSE");
 578                dbg("  TrueEdgeport     : %s", bits->TrueEdgeport       ? "TRUE": "FALSE");
 579        }
 580
 581        return result;
 582}
 583
 584
 585/************************************************************************/
 586/************************************************************************/
 587/*            U S B  C A L L B A C K   F U N C T I O N S                */
 588/*            U S B  C A L L B A C K   F U N C T I O N S                */
 589/************************************************************************/
 590/************************************************************************/
 591
 592/*****************************************************************************
 593 * edge_interrupt_callback
 594 *      this is the callback function for when we have received data on the
 595 *      interrupt endpoint.
 596 *****************************************************************************/
 597static void edge_interrupt_callback(struct urb *urb)
 598{
 599        struct edgeport_serial  *edge_serial = urb->context;
 600        struct edgeport_port *edge_port;
 601        struct usb_serial_port *port;
 602        unsigned char *data = urb->transfer_buffer;
 603        int length = urb->actual_length;
 604        int bytes_avail;
 605        int position;
 606        int txCredits;
 607        int portNumber;
 608        int result;
 609        int status = urb->status;
 610
 611        dbg("%s", __func__);
 612
 613        switch (status) {
 614        case 0:
 615                /* success */
 616                break;
 617        case -ECONNRESET:
 618        case -ENOENT:
 619        case -ESHUTDOWN:
 620                /* this urb is terminated, clean up */
 621                dbg("%s - urb shutting down with status: %d",
 622                                                __func__, status);
 623                return;
 624        default:
 625                dbg("%s - nonzero urb status received: %d", __func__, status);
 626                goto exit;
 627        }
 628
 629        /* process this interrupt-read even if there are no ports open */
 630        if (length) {
 631                usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
 632                                                __func__, length, data);
 633
 634                if (length > 1) {
 635                        bytes_avail = data[0] | (data[1] << 8);
 636                        if (bytes_avail) {
 637                                spin_lock(&edge_serial->es_lock);
 638                                edge_serial->rxBytesAvail += bytes_avail;
 639                                dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
 640
 641                                if (edge_serial->rxBytesAvail > 0 &&
 642                                    !edge_serial->read_in_progress) {
 643                                        dbg("%s - posting a read", __func__);
 644                                        edge_serial->read_in_progress = true;
 645
 646                                        /* we have pending bytes on the
 647                                           bulk in pipe, send a request */
 648                                        edge_serial->read_urb->dev = edge_serial->serial->dev;
 649                                        result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
 650                                        if (result) {
 651                                                dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
 652                                                edge_serial->read_in_progress = false;
 653                                        }
 654                                }
 655                                spin_unlock(&edge_serial->es_lock);
 656                        }
 657                }
 658                /* grab the txcredits for the ports if available */
 659                position = 2;
 660                portNumber = 0;
 661                while ((position < length) &&
 662                                (portNumber < edge_serial->serial->num_ports)) {
 663                        txCredits = data[position] | (data[position+1] << 8);
 664                        if (txCredits) {
 665                                port = edge_serial->serial->port[portNumber];
 666                                edge_port = usb_get_serial_port_data(port);
 667                                if (edge_port->open) {
 668                                        spin_lock(&edge_port->ep_lock);
 669                                        edge_port->txCredits += txCredits;
 670                                        spin_unlock(&edge_port->ep_lock);
 671                                        dbg("%s - txcredits for port%d = %d",
 672                                                        __func__, portNumber,
 673                                                        edge_port->txCredits);
 674
 675                                        /* tell the tty driver that something
 676                                           has changed */
 677                                        if (edge_port->port->port.tty)
 678                                                tty_wakeup(edge_port->port->port.tty);
 679
 680                                        /* Since we have more credit, check
 681                                           if more data can be sent */
 682                                        send_more_port_data(edge_serial,
 683                                                                edge_port);
 684                                }
 685                        }
 686                        position += 2;
 687                        ++portNumber;
 688                }
 689        }
 690
 691exit:
 692        result = usb_submit_urb(urb, GFP_ATOMIC);
 693        if (result)
 694                dev_err(&urb->dev->dev,
 695                        "%s - Error %d submitting control urb\n",
 696                                                __func__, result);
 697}
 698
 699
 700/*****************************************************************************
 701 * edge_bulk_in_callback
 702 *      this is the callback function for when we have received data on the
 703 *      bulk in endpoint.
 704 *****************************************************************************/
 705static void edge_bulk_in_callback(struct urb *urb)
 706{
 707        struct edgeport_serial  *edge_serial = urb->context;
 708        unsigned char           *data = urb->transfer_buffer;
 709        int                     retval;
 710        __u16                   raw_data_length;
 711        int status = urb->status;
 712
 713        dbg("%s", __func__);
 714
 715        if (status) {
 716                dbg("%s - nonzero read bulk status received: %d",
 717                    __func__, status);
 718                edge_serial->read_in_progress = false;
 719                return;
 720        }
 721
 722        if (urb->actual_length == 0) {
 723                dbg("%s - read bulk callback with no data", __func__);
 724                edge_serial->read_in_progress = false;
 725                return;
 726        }
 727
 728        raw_data_length = urb->actual_length;
 729
 730        usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
 731                                        __func__, raw_data_length, data);
 732
 733        spin_lock(&edge_serial->es_lock);
 734
 735        /* decrement our rxBytes available by the number that we just got */
 736        edge_serial->rxBytesAvail -= raw_data_length;
 737
 738        dbg("%s - Received = %d, rxBytesAvail %d", __func__,
 739                                raw_data_length, edge_serial->rxBytesAvail);
 740
 741        process_rcvd_data(edge_serial, data, urb->actual_length);
 742
 743        /* check to see if there's any more data for us to read */
 744        if (edge_serial->rxBytesAvail > 0) {
 745                dbg("%s - posting a read", __func__);
 746                edge_serial->read_urb->dev = edge_serial->serial->dev;
 747                retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
 748                if (retval) {
 749                        dev_err(&urb->dev->dev,
 750                                "%s - usb_submit_urb(read bulk) failed, "
 751                                "retval = %d\n", __func__, retval);
 752                        edge_serial->read_in_progress = false;
 753                }
 754        } else {
 755                edge_serial->read_in_progress = false;
 756        }
 757
 758        spin_unlock(&edge_serial->es_lock);
 759}
 760
 761
 762/*****************************************************************************
 763 * edge_bulk_out_data_callback
 764 *      this is the callback function for when we have finished sending
 765 *      serial data on the bulk out endpoint.
 766 *****************************************************************************/
 767static void edge_bulk_out_data_callback(struct urb *urb)
 768{
 769        struct edgeport_port *edge_port = urb->context;
 770        struct tty_struct *tty;
 771        int status = urb->status;
 772
 773        dbg("%s", __func__);
 774
 775        if (status) {
 776                dbg("%s - nonzero write bulk status received: %d",
 777                    __func__, status);
 778        }
 779
 780        tty = edge_port->port->port.tty;
 781
 782        if (tty && edge_port->open) {
 783                /* let the tty driver wakeup if it has a special
 784                   write_wakeup function */
 785                tty_wakeup(tty);
 786        }
 787
 788        /* Release the Write URB */
 789        edge_port->write_in_progress = false;
 790
 791        /* Check if more data needs to be sent */
 792        send_more_port_data((struct edgeport_serial *)
 793                (usb_get_serial_data(edge_port->port->serial)), edge_port);
 794}
 795
 796
 797/*****************************************************************************
 798 * BulkOutCmdCallback
 799 *      this is the callback function for when we have finished sending a
 800 *      command on the bulk out endpoint.
 801 *****************************************************************************/
 802static void edge_bulk_out_cmd_callback(struct urb *urb)
 803{
 804        struct edgeport_port *edge_port = urb->context;
 805        struct tty_struct *tty;
 806        int status = urb->status;
 807
 808        dbg("%s", __func__);
 809
 810        atomic_dec(&CmdUrbs);
 811        dbg("%s - FREE URB %p (outstanding %d)", __func__,
 812                                        urb, atomic_read(&CmdUrbs));
 813
 814
 815        /* clean up the transfer buffer */
 816        kfree(urb->transfer_buffer);
 817
 818        /* Free the command urb */
 819        usb_free_urb(urb);
 820
 821        if (status) {
 822                dbg("%s - nonzero write bulk status received: %d",
 823                                                        __func__, status);
 824                return;
 825        }
 826
 827        /* Get pointer to tty */
 828        tty = edge_port->port->port.tty;
 829
 830        /* tell the tty driver that something has changed */
 831        if (tty && edge_port->open)
 832                tty_wakeup(tty);
 833
 834        /* we have completed the command */
 835        edge_port->commandPending = false;
 836        wake_up(&edge_port->wait_command);
 837}
 838
 839
 840/*****************************************************************************
 841 * Driver tty interface functions
 842 *****************************************************************************/
 843
 844/*****************************************************************************
 845 * SerialOpen
 846 *      this function is called by the tty driver when a port is opened
 847 *      If successful, we return 0
 848 *      Otherwise we return a negative error number.
 849 *****************************************************************************/
 850static int edge_open(struct tty_struct *tty,
 851                        struct usb_serial_port *port, struct file *filp)
 852{
 853        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
 854        struct usb_serial *serial;
 855        struct edgeport_serial *edge_serial;
 856        int response;
 857
 858        dbg("%s - port %d", __func__, port->number);
 859
 860        if (edge_port == NULL)
 861                return -ENODEV;
 862
 863        /* see if we've set up our endpoint info yet (can't set it up
 864           in edge_startup as the structures were not set up at that time.) */
 865        serial = port->serial;
 866        edge_serial = usb_get_serial_data(serial);
 867        if (edge_serial == NULL)
 868                return -ENODEV;
 869        if (edge_serial->interrupt_in_buffer == NULL) {
 870                struct usb_serial_port *port0 = serial->port[0];
 871
 872                /* not set up yet, so do it now */
 873                edge_serial->interrupt_in_buffer =
 874                                        port0->interrupt_in_buffer;
 875                edge_serial->interrupt_in_endpoint =
 876                                        port0->interrupt_in_endpointAddress;
 877                edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
 878                edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
 879                edge_serial->bulk_in_endpoint =
 880                                        port0->bulk_in_endpointAddress;
 881                edge_serial->read_urb = port0->read_urb;
 882                edge_serial->bulk_out_endpoint =
 883                                        port0->bulk_out_endpointAddress;
 884
 885                /* set up our interrupt urb */
 886                usb_fill_int_urb(edge_serial->interrupt_read_urb,
 887                      serial->dev,
 888                      usb_rcvintpipe(serial->dev,
 889                                port0->interrupt_in_endpointAddress),
 890                      port0->interrupt_in_buffer,
 891                      edge_serial->interrupt_read_urb->transfer_buffer_length,
 892                      edge_interrupt_callback, edge_serial,
 893                      edge_serial->interrupt_read_urb->interval);
 894
 895                /* set up our bulk in urb */
 896                usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
 897                        usb_rcvbulkpipe(serial->dev,
 898                                port0->bulk_in_endpointAddress),
 899                        port0->bulk_in_buffer,
 900                        edge_serial->read_urb->transfer_buffer_length,
 901                        edge_bulk_in_callback, edge_serial);
 902                edge_serial->read_in_progress = false;
 903
 904                /* start interrupt read for this edgeport
 905                 * this interrupt will continue as long
 906                 * as the edgeport is connected */
 907                response = usb_submit_urb(edge_serial->interrupt_read_urb,
 908                                                                GFP_KERNEL);
 909                if (response) {
 910                        dev_err(&port->dev,
 911                                "%s - Error %d submitting control urb\n",
 912                                                        __func__, response);
 913                }
 914        }
 915
 916        /* initialize our wait queues */
 917        init_waitqueue_head(&edge_port->wait_open);
 918        init_waitqueue_head(&edge_port->wait_chase);
 919        init_waitqueue_head(&edge_port->delta_msr_wait);
 920        init_waitqueue_head(&edge_port->wait_command);
 921
 922        /* initialize our icount structure */
 923        memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
 924
 925        /* initialize our port settings */
 926        edge_port->txCredits = 0;       /* Can't send any data yet */
 927        /* Must always set this bit to enable ints! */
 928        edge_port->shadowMCR = MCR_MASTER_IE;
 929        edge_port->chaseResponsePending = false;
 930
 931        /* send a open port command */
 932        edge_port->openPending = true;
 933        edge_port->open        = false;
 934        response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
 935
 936        if (response < 0) {
 937                dev_err(&port->dev, "%s - error sending open port command\n",
 938                                                                __func__);
 939                edge_port->openPending = false;
 940                return -ENODEV;
 941        }
 942
 943        /* now wait for the port to be completely opened */
 944        wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
 945                                                                OPEN_TIMEOUT);
 946
 947        if (!edge_port->open) {
 948                /* open timed out */
 949                dbg("%s - open timedout", __func__);
 950                edge_port->openPending = false;
 951                return -ENODEV;
 952        }
 953
 954        /* create the txfifo */
 955        edge_port->txfifo.head  = 0;
 956        edge_port->txfifo.tail  = 0;
 957        edge_port->txfifo.count = 0;
 958        edge_port->txfifo.size  = edge_port->maxTxCredits;
 959        edge_port->txfifo.fifo  = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
 960
 961        if (!edge_port->txfifo.fifo) {
 962                dbg("%s - no memory", __func__);
 963                edge_close(tty, port, filp);
 964                return -ENOMEM;
 965        }
 966
 967        /* Allocate a URB for the write */
 968        edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
 969        edge_port->write_in_progress = false;
 970
 971        if (!edge_port->write_urb) {
 972                dbg("%s - no memory", __func__);
 973                edge_close(tty, port, filp);
 974                return -ENOMEM;
 975        }
 976
 977        dbg("%s(%d) - Initialize TX fifo to %d bytes",
 978                        __func__, port->number, edge_port->maxTxCredits);
 979
 980        dbg("%s exited", __func__);
 981
 982        return 0;
 983}
 984
 985
 986/************************************************************************
 987 *
 988 * block_until_chase_response
 989 *
 990 *      This function will block the close until one of the following:
 991 *              1. Response to our Chase comes from Edgeport
 992 *              2. A timeout of 10 seconds without activity has expired
 993 *                 (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
 994 *
 995 ************************************************************************/
 996static void block_until_chase_response(struct edgeport_port *edge_port)
 997{
 998        DEFINE_WAIT(wait);
 999        __u16 lastCredits;
1000        int timeout = 1*HZ;
1001        int loop = 10;
1002
1003        while (1) {
1004                /* Save Last credits */
1005                lastCredits = edge_port->txCredits;
1006
1007                /* Did we get our Chase response */
1008                if (!edge_port->chaseResponsePending) {
1009                        dbg("%s - Got Chase Response", __func__);
1010
1011                        /* did we get all of our credit back? */
1012                        if (edge_port->txCredits == edge_port->maxTxCredits) {
1013                                dbg("%s - Got all credits", __func__);
1014                                return;
1015                        }
1016                }
1017
1018                /* Block the thread for a while */
1019                prepare_to_wait(&edge_port->wait_chase, &wait,
1020                                                TASK_UNINTERRUPTIBLE);
1021                schedule_timeout(timeout);
1022                finish_wait(&edge_port->wait_chase, &wait);
1023
1024                if (lastCredits == edge_port->txCredits) {
1025                        /* No activity.. count down. */
1026                        loop--;
1027                        if (loop == 0) {
1028                                edge_port->chaseResponsePending = false;
1029                                dbg("%s - Chase TIMEOUT", __func__);
1030                                return;
1031                        }
1032                } else {
1033                        /* Reset timeout value back to 10 seconds */
1034                        dbg("%s - Last %d, Current %d", __func__,
1035                                        lastCredits, edge_port->txCredits);
1036                        loop = 10;
1037                }
1038        }
1039}
1040
1041
1042/************************************************************************
1043 *
1044 * block_until_tx_empty
1045 *
1046 *      This function will block the close until one of the following:
1047 *              1. TX count are 0
1048 *              2. The edgeport has stopped
1049 *              3. A timeout of 3 seconds without activity has expired
1050 *
1051 ************************************************************************/
1052static void block_until_tx_empty(struct edgeport_port *edge_port)
1053{
1054        DEFINE_WAIT(wait);
1055        struct TxFifo *fifo = &edge_port->txfifo;
1056        __u32 lastCount;
1057        int timeout = HZ/10;
1058        int loop = 30;
1059
1060        while (1) {
1061                /* Save Last count */
1062                lastCount = fifo->count;
1063
1064                /* Is the Edgeport Buffer empty? */
1065                if (lastCount == 0) {
1066                        dbg("%s - TX Buffer Empty", __func__);
1067                        return;
1068                }
1069
1070                /* Block the thread for a while */
1071                prepare_to_wait(&edge_port->wait_chase, &wait,
1072                                                TASK_UNINTERRUPTIBLE);
1073                schedule_timeout(timeout);
1074                finish_wait(&edge_port->wait_chase, &wait);
1075
1076                dbg("%s wait", __func__);
1077
1078                if (lastCount == fifo->count) {
1079                        /* No activity.. count down. */
1080                        loop--;
1081                        if (loop == 0) {
1082                                dbg("%s - TIMEOUT", __func__);
1083                                return;
1084                        }
1085                } else {
1086                        /* Reset timeout value back to seconds */
1087                        loop = 30;
1088                }
1089        }
1090}
1091
1092
1093/*****************************************************************************
1094 * edge_close
1095 *      this function is called by the tty driver when a port is closed
1096 *****************************************************************************/
1097static void edge_close(struct tty_struct *tty,
1098                        struct usb_serial_port *port, struct file *filp)
1099{
1100        struct edgeport_serial *edge_serial;
1101        struct edgeport_port *edge_port;
1102        int status;
1103
1104        dbg("%s - port %d", __func__, port->number);
1105
1106        edge_serial = usb_get_serial_data(port->serial);
1107        edge_port = usb_get_serial_port_data(port);
1108        if (edge_serial == NULL || edge_port == NULL)
1109                return;
1110
1111        /* block until tx is empty */
1112        block_until_tx_empty(edge_port);
1113
1114        edge_port->closePending = true;
1115
1116        if ((!edge_serial->is_epic) ||
1117            ((edge_serial->is_epic) &&
1118             (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1119                /* flush and chase */
1120                edge_port->chaseResponsePending = true;
1121
1122                dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1123                status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1124                if (status == 0)
1125                        /* block until chase finished */
1126                        block_until_chase_response(edge_port);
1127                else
1128                        edge_port->chaseResponsePending = false;
1129        }
1130
1131        if ((!edge_serial->is_epic) ||
1132            ((edge_serial->is_epic) &&
1133             (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1134               /* close the port */
1135                dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
1136                send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1137        }
1138
1139        /* port->close = true; */
1140        edge_port->closePending = false;
1141        edge_port->open = false;
1142        edge_port->openPending = false;
1143
1144        usb_kill_urb(edge_port->write_urb);
1145
1146        if (edge_port->write_urb) {
1147                /* if this urb had a transfer buffer already
1148                                (old transfer) free it */
1149                kfree(edge_port->write_urb->transfer_buffer);
1150                usb_free_urb(edge_port->write_urb);
1151                edge_port->write_urb = NULL;
1152        }
1153        kfree(edge_port->txfifo.fifo);
1154        edge_port->txfifo.fifo = NULL;
1155
1156        dbg("%s exited", __func__);
1157}
1158
1159/*****************************************************************************
1160 * SerialWrite
1161 *      this function is called by the tty driver when data should be written
1162 *      to the port.
1163 *      If successful, we return the number of bytes written, otherwise we
1164 *      return a negative error number.
1165 *****************************************************************************/
1166static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1167                                        const unsigned char *data, int count)
1168{
1169        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1170        struct TxFifo *fifo;
1171        int copySize;
1172        int bytesleft;
1173        int firsthalf;
1174        int secondhalf;
1175        unsigned long flags;
1176
1177        dbg("%s - port %d", __func__, port->number);
1178
1179        if (edge_port == NULL)
1180                return -ENODEV;
1181
1182        /* get a pointer to the Tx fifo */
1183        fifo = &edge_port->txfifo;
1184
1185        spin_lock_irqsave(&edge_port->ep_lock, flags);
1186
1187        /* calculate number of bytes to put in fifo */
1188        copySize = min((unsigned int)count,
1189                                (edge_port->txCredits - fifo->count));
1190
1191        dbg("%s(%d) of %d byte(s) Fifo room  %d -- will copy %d bytes",
1192                        __func__, port->number, count,
1193                        edge_port->txCredits - fifo->count, copySize);
1194
1195        /* catch writes of 0 bytes which the tty driver likes to give us,
1196           and when txCredits is empty */
1197        if (copySize == 0) {
1198                dbg("%s - copySize = Zero", __func__);
1199                goto finish_write;
1200        }
1201
1202        /* queue the data
1203         * since we can never overflow the buffer we do not have to check for a
1204         * full condition
1205         *
1206         * the copy is done is two parts -- first fill to the end of the buffer
1207         * then copy the reset from the start of the buffer
1208         */
1209        bytesleft = fifo->size - fifo->head;
1210        firsthalf = min(bytesleft, copySize);
1211        dbg("%s - copy %d bytes of %d into fifo ", __func__,
1212                                        firsthalf, bytesleft);
1213
1214        /* now copy our data */
1215        memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1216        usb_serial_debug_data(debug, &port->dev, __func__,
1217                                        firsthalf, &fifo->fifo[fifo->head]);
1218
1219        /* update the index and size */
1220        fifo->head  += firsthalf;
1221        fifo->count += firsthalf;
1222
1223        /* wrap the index */
1224        if (fifo->head == fifo->size)
1225                fifo->head = 0;
1226
1227        secondhalf = copySize-firsthalf;
1228
1229        if (secondhalf) {
1230                dbg("%s - copy rest of data %d", __func__, secondhalf);
1231                memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1232                usb_serial_debug_data(debug, &port->dev, __func__,
1233                                        secondhalf, &fifo->fifo[fifo->head]);
1234                /* update the index and size */
1235                fifo->count += secondhalf;
1236                fifo->head  += secondhalf;
1237                /* No need to check for wrap since we can not get to end of
1238                 * the fifo in this part
1239                 */
1240        }
1241
1242finish_write:
1243        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1244
1245        send_more_port_data((struct edgeport_serial *)
1246                        usb_get_serial_data(port->serial), edge_port);
1247
1248        dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
1249                                copySize, edge_port->txCredits, fifo->count);
1250
1251        return copySize;
1252}
1253
1254
1255/************************************************************************
1256 *
1257 * send_more_port_data()
1258 *
1259 *      This routine attempts to write additional UART transmit data
1260 *      to a port over the USB bulk pipe. It is called (1) when new
1261 *      data has been written to a port's TxBuffer from higher layers
1262 *      (2) when the peripheral sends us additional TxCredits indicating
1263 *      that it can accept more Tx data for a given port; and (3) when
1264 *      a bulk write completes successfully and we want to see if we
1265 *      can transmit more.
1266 *
1267 ************************************************************************/
1268static void send_more_port_data(struct edgeport_serial *edge_serial,
1269                                        struct edgeport_port *edge_port)
1270{
1271        struct TxFifo   *fifo = &edge_port->txfifo;
1272        struct urb      *urb;
1273        unsigned char   *buffer;
1274        int             status;
1275        int             count;
1276        int             bytesleft;
1277        int             firsthalf;
1278        int             secondhalf;
1279        unsigned long   flags;
1280
1281        dbg("%s(%d)", __func__, edge_port->port->number);
1282
1283        spin_lock_irqsave(&edge_port->ep_lock, flags);
1284
1285        if (edge_port->write_in_progress ||
1286            !edge_port->open             ||
1287            (fifo->count == 0)) {
1288                dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
1289                                __func__, edge_port->port->number,
1290                                fifo->count, edge_port->write_in_progress);
1291                goto exit_send;
1292        }
1293
1294        /* since the amount of data in the fifo will always fit into the
1295         * edgeport buffer we do not need to check the write length
1296         *
1297         * Do we have enough credits for this port to make it worthwhile
1298         * to bother queueing a write. If it's too small, say a few bytes,
1299         * it's better to wait for more credits so we can do a larger write.
1300         */
1301        if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1302                dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
1303                        __func__, edge_port->port->number, fifo->count,
1304                        edge_port->txCredits);
1305                goto exit_send;
1306        }
1307
1308        /* lock this write */
1309        edge_port->write_in_progress = true;
1310
1311        /* get a pointer to the write_urb */
1312        urb = edge_port->write_urb;
1313
1314        /* make sure transfer buffer is freed */
1315        kfree(urb->transfer_buffer);
1316        urb->transfer_buffer = NULL;
1317
1318        /* build the data header for the buffer and port that we are about
1319           to send out */
1320        count = fifo->count;
1321        buffer = kmalloc(count+2, GFP_ATOMIC);
1322        if (buffer == NULL) {
1323                dev_err(&edge_port->port->dev,
1324                                "%s - no more kernel memory...\n", __func__);
1325                edge_port->write_in_progress = false;
1326                goto exit_send;
1327        }
1328        buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1329                                - edge_port->port->serial->minor, count);
1330        buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1331                                - edge_port->port->serial->minor, count);
1332
1333        /* now copy our data */
1334        bytesleft =  fifo->size - fifo->tail;
1335        firsthalf = min(bytesleft, count);
1336        memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1337        fifo->tail  += firsthalf;
1338        fifo->count -= firsthalf;
1339        if (fifo->tail == fifo->size)
1340                fifo->tail = 0;
1341
1342        secondhalf = count-firsthalf;
1343        if (secondhalf) {
1344                memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1345                                                                secondhalf);
1346                fifo->tail  += secondhalf;
1347                fifo->count -= secondhalf;
1348        }
1349
1350        if (count)
1351                usb_serial_debug_data(debug, &edge_port->port->dev,
1352                                                __func__, count, &buffer[2]);
1353
1354        /* fill up the urb with all of our data and submit it */
1355        usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1356                        usb_sndbulkpipe(edge_serial->serial->dev,
1357                                        edge_serial->bulk_out_endpoint),
1358                        buffer, count+2,
1359                        edge_bulk_out_data_callback, edge_port);
1360
1361        /* decrement the number of credits we have by the number we just sent */
1362        edge_port->txCredits -= count;
1363        edge_port->icount.tx += count;
1364
1365        urb->dev = edge_serial->serial->dev;
1366        status = usb_submit_urb(urb, GFP_ATOMIC);
1367        if (status) {
1368                /* something went wrong */
1369                dev_err(&edge_port->port->dev,
1370                        "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1371                                __func__, status);
1372                edge_port->write_in_progress = false;
1373
1374                /* revert the credits as something bad happened. */
1375                edge_port->txCredits += count;
1376                edge_port->icount.tx -= count;
1377        }
1378        dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
1379                        __func__, count, edge_port->txCredits, fifo->count);
1380
1381exit_send:
1382        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1383}
1384
1385
1386/*****************************************************************************
1387 * edge_write_room
1388 *      this function is called by the tty driver when it wants to know how
1389 *      many bytes of data we can accept for a specific port. If successful,
1390 *      we return the amount of room that we have for this port (the txCredits)
1391 *      otherwise we return a negative error number.
1392 *****************************************************************************/
1393static int edge_write_room(struct tty_struct *tty)
1394{
1395        struct usb_serial_port *port = tty->driver_data;
1396        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1397        int room;
1398        unsigned long flags;
1399
1400        dbg("%s", __func__);
1401
1402        if (edge_port == NULL)
1403                return 0;
1404        if (edge_port->closePending)
1405                return 0;
1406
1407        dbg("%s - port %d", __func__, port->number);
1408
1409        if (!edge_port->open) {
1410                dbg("%s - port not opened", __func__);
1411                return 0;
1412        }
1413
1414        /* total of both buffers is still txCredit */
1415        spin_lock_irqsave(&edge_port->ep_lock, flags);
1416        room = edge_port->txCredits - edge_port->txfifo.count;
1417        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1418
1419        dbg("%s - returns %d", __func__, room);
1420        return room;
1421}
1422
1423
1424/*****************************************************************************
1425 * edge_chars_in_buffer
1426 *      this function is called by the tty driver when it wants to know how
1427 *      many bytes of data we currently have outstanding in the port (data that
1428 *      has been written, but hasn't made it out the port yet)
1429 *      If successful, we return the number of bytes left to be written in the
1430 *      system,
1431 *      Otherwise we return a negative error number.
1432 *****************************************************************************/
1433static int edge_chars_in_buffer(struct tty_struct *tty)
1434{
1435        struct usb_serial_port *port = tty->driver_data;
1436        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1437        int num_chars;
1438        unsigned long flags;
1439
1440        dbg("%s", __func__);
1441
1442        if (edge_port == NULL)
1443                return 0;
1444        if (edge_port->closePending)
1445                return 0;
1446
1447        if (!edge_port->open) {
1448                dbg("%s - port not opened", __func__);
1449                return 0;
1450        }
1451
1452        spin_lock_irqsave(&edge_port->ep_lock, flags);
1453        num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1454                                                edge_port->txfifo.count;
1455        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1456        if (num_chars) {
1457                dbg("%s(port %d) - returns %d", __func__,
1458                                                port->number, num_chars);
1459        }
1460
1461        return num_chars;
1462}
1463
1464
1465/*****************************************************************************
1466 * SerialThrottle
1467 *      this function is called by the tty driver when it wants to stop the data
1468 *      being read from the port.
1469 *****************************************************************************/
1470static void edge_throttle(struct tty_struct *tty)
1471{
1472        struct usb_serial_port *port = tty->driver_data;
1473        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1474        int status;
1475
1476        dbg("%s - port %d", __func__, port->number);
1477
1478        if (edge_port == NULL)
1479                return;
1480
1481        if (!edge_port->open) {
1482                dbg("%s - port not opened", __func__);
1483                return;
1484        }
1485
1486        /* if we are implementing XON/XOFF, send the stop character */
1487        if (I_IXOFF(tty)) {
1488                unsigned char stop_char = STOP_CHAR(tty);
1489                status = edge_write(tty, port, &stop_char, 1);
1490                if (status <= 0)
1491                        return;
1492        }
1493
1494        /* if we are implementing RTS/CTS, toggle that line */
1495        if (tty->termios->c_cflag & CRTSCTS) {
1496                edge_port->shadowMCR &= ~MCR_RTS;
1497                status = send_cmd_write_uart_register(edge_port, MCR,
1498                                                        edge_port->shadowMCR);
1499                if (status != 0)
1500                        return;
1501        }
1502
1503        return;
1504}
1505
1506
1507/*****************************************************************************
1508 * edge_unthrottle
1509 *      this function is called by the tty driver when it wants to resume the
1510 *      data being read from the port (called after SerialThrottle is called)
1511 *****************************************************************************/
1512static void edge_unthrottle(struct tty_struct *tty)
1513{
1514        struct usb_serial_port *port = tty->driver_data;
1515        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1516        int status;
1517
1518        dbg("%s - port %d", __func__, port->number);
1519
1520        if (edge_port == NULL)
1521                return;
1522
1523        if (!edge_port->open) {
1524                dbg("%s - port not opened", __func__);
1525                return;
1526        }
1527
1528        /* if we are implementing XON/XOFF, send the start character */
1529        if (I_IXOFF(tty)) {
1530                unsigned char start_char = START_CHAR(tty);
1531                status = edge_write(tty, port, &start_char, 1);
1532                if (status <= 0)
1533                        return;
1534        }
1535        /* if we are implementing RTS/CTS, toggle that line */
1536        if (tty->termios->c_cflag & CRTSCTS) {
1537                edge_port->shadowMCR |= MCR_RTS;
1538                send_cmd_write_uart_register(edge_port, MCR,
1539                                                edge_port->shadowMCR);
1540        }
1541}
1542
1543
1544/*****************************************************************************
1545 * SerialSetTermios
1546 *      this function is called by the tty driver when it wants to change
1547 * the termios structure
1548 *****************************************************************************/
1549static void edge_set_termios(struct tty_struct *tty,
1550        struct usb_serial_port *port, struct ktermios *old_termios)
1551{
1552        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1553        unsigned int cflag;
1554
1555        cflag = tty->termios->c_cflag;
1556        dbg("%s - clfag %08x iflag %08x", __func__,
1557            tty->termios->c_cflag, tty->termios->c_iflag);
1558        dbg("%s - old clfag %08x old iflag %08x", __func__,
1559            old_termios->c_cflag, old_termios->c_iflag);
1560
1561        dbg("%s - port %d", __func__, port->number);
1562
1563        if (edge_port == NULL)
1564                return;
1565
1566        if (!edge_port->open) {
1567                dbg("%s - port not opened", __func__);
1568                return;
1569        }
1570
1571        /* change the port settings to the new ones specified */
1572        change_port_settings(tty, edge_port, old_termios);
1573}
1574
1575
1576/*****************************************************************************
1577 * get_lsr_info - get line status register info
1578 *
1579 * Purpose: Let user call ioctl() to get info when the UART physically
1580 *          is emptied.  On bus types like RS485, the transmitter must
1581 *          release the bus after transmitting. This must be done when
1582 *          the transmit shift register is empty, not be done when the
1583 *          transmit holding register is empty.  This functionality
1584 *          allows an RS485 driver to be written in user space.
1585 *****************************************************************************/
1586static int get_lsr_info(struct edgeport_port *edge_port,
1587                                                unsigned int __user *value)
1588{
1589        unsigned int result = 0;
1590        unsigned long flags;
1591
1592        spin_lock_irqsave(&edge_port->ep_lock, flags);
1593        if (edge_port->maxTxCredits == edge_port->txCredits &&
1594            edge_port->txfifo.count == 0) {
1595                dbg("%s -- Empty", __func__);
1596                result = TIOCSER_TEMT;
1597        }
1598        spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1599
1600        if (copy_to_user(value, &result, sizeof(int)))
1601                return -EFAULT;
1602        return 0;
1603}
1604
1605static int edge_tiocmset(struct tty_struct *tty, struct file *file,
1606                                        unsigned int set, unsigned int clear)
1607{
1608        struct usb_serial_port *port = tty->driver_data;
1609        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1610        unsigned int mcr;
1611
1612        dbg("%s - port %d", __func__, port->number);
1613
1614        mcr = edge_port->shadowMCR;
1615        if (set & TIOCM_RTS)
1616                mcr |= MCR_RTS;
1617        if (set & TIOCM_DTR)
1618                mcr |= MCR_DTR;
1619        if (set & TIOCM_LOOP)
1620                mcr |= MCR_LOOPBACK;
1621
1622        if (clear & TIOCM_RTS)
1623                mcr &= ~MCR_RTS;
1624        if (clear & TIOCM_DTR)
1625                mcr &= ~MCR_DTR;
1626        if (clear & TIOCM_LOOP)
1627                mcr &= ~MCR_LOOPBACK;
1628
1629        edge_port->shadowMCR = mcr;
1630
1631        send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1632
1633        return 0;
1634}
1635
1636static int edge_tiocmget(struct tty_struct *tty, struct file *file)
1637{
1638        struct usb_serial_port *port = tty->driver_data;
1639        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1640        unsigned int result = 0;
1641        unsigned int msr;
1642        unsigned int mcr;
1643
1644        dbg("%s - port %d", __func__, port->number);
1645
1646        msr = edge_port->shadowMSR;
1647        mcr = edge_port->shadowMCR;
1648        result = ((mcr & MCR_DTR)       ? TIOCM_DTR: 0)   /* 0x002 */
1649                  | ((mcr & MCR_RTS)    ? TIOCM_RTS: 0)   /* 0x004 */
1650                  | ((msr & EDGEPORT_MSR_CTS)   ? TIOCM_CTS: 0)   /* 0x020 */
1651                  | ((msr & EDGEPORT_MSR_CD)    ? TIOCM_CAR: 0)   /* 0x040 */
1652                  | ((msr & EDGEPORT_MSR_RI)    ? TIOCM_RI:  0)   /* 0x080 */
1653                  | ((msr & EDGEPORT_MSR_DSR)   ? TIOCM_DSR: 0);  /* 0x100 */
1654
1655
1656        dbg("%s -- %x", __func__, result);
1657
1658        return result;
1659}
1660
1661static int get_serial_info(struct edgeport_port *edge_port,
1662                                struct serial_struct __user *retinfo)
1663{
1664        struct serial_struct tmp;
1665
1666        if (!retinfo)
1667                return -EFAULT;
1668
1669        memset(&tmp, 0, sizeof(tmp));
1670
1671        tmp.type                = PORT_16550A;
1672        tmp.line                = edge_port->port->serial->minor;
1673        tmp.port                = edge_port->port->number;
1674        tmp.irq                 = 0;
1675        tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1676        tmp.xmit_fifo_size      = edge_port->maxTxCredits;
1677        tmp.baud_base           = 9600;
1678        tmp.close_delay         = 5*HZ;
1679        tmp.closing_wait        = 30*HZ;
1680
1681        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1682                return -EFAULT;
1683        return 0;
1684}
1685
1686
1687
1688/*****************************************************************************
1689 * SerialIoctl
1690 *      this function handles any ioctl calls to the driver
1691 *****************************************************************************/
1692static int edge_ioctl(struct tty_struct *tty, struct file *file,
1693                                        unsigned int cmd, unsigned long arg)
1694{
1695        struct usb_serial_port *port = tty->driver_data;
1696        DEFINE_WAIT(wait);
1697        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1698        struct async_icount cnow;
1699        struct async_icount cprev;
1700        struct serial_icounter_struct icount;
1701
1702        dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1703
1704        switch (cmd) {
1705        case TIOCSERGETLSR:
1706                dbg("%s (%d) TIOCSERGETLSR", __func__,  port->number);
1707                return get_lsr_info(edge_port, (unsigned int __user *) arg);
1708
1709        case TIOCGSERIAL:
1710                dbg("%s (%d) TIOCGSERIAL", __func__,  port->number);
1711                return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1712
1713        case TIOCMIWAIT:
1714                dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
1715                cprev = edge_port->icount;
1716                while (1) {
1717                        prepare_to_wait(&edge_port->delta_msr_wait,
1718                                                &wait, TASK_INTERRUPTIBLE);
1719                        schedule();
1720                        finish_wait(&edge_port->delta_msr_wait, &wait);
1721                        /* see if a signal did it */
1722                        if (signal_pending(current))
1723                                return -ERESTARTSYS;
1724                        cnow = edge_port->icount;
1725                        if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1726                            cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1727                                return -EIO; /* no change => error */
1728                        if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1729                            ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1730                            ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1731                            ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1732                                return 0;
1733                        }
1734                        cprev = cnow;
1735                }
1736                /* NOTREACHED */
1737                break;
1738
1739        case TIOCGICOUNT:
1740                cnow = edge_port->icount;
1741                memset(&icount, 0, sizeof(icount));
1742                icount.cts = cnow.cts;
1743                icount.dsr = cnow.dsr;
1744                icount.rng = cnow.rng;
1745                icount.dcd = cnow.dcd;
1746                icount.rx = cnow.rx;
1747                icount.tx = cnow.tx;
1748                icount.frame = cnow.frame;
1749                icount.overrun = cnow.overrun;
1750                icount.parity = cnow.parity;
1751                icount.brk = cnow.brk;
1752                icount.buf_overrun = cnow.buf_overrun;
1753
1754                dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
1755                                __func__,  port->number, icount.rx, icount.tx);
1756                if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1757                        return -EFAULT;
1758                return 0;
1759        }
1760        return -ENOIOCTLCMD;
1761}
1762
1763
1764/*****************************************************************************
1765 * SerialBreak
1766 *      this function sends a break to the port
1767 *****************************************************************************/
1768static void edge_break(struct tty_struct *tty, int break_state)
1769{
1770        struct usb_serial_port *port = tty->driver_data;
1771        struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1772        struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1773        int status;
1774
1775        if ((!edge_serial->is_epic) ||
1776            ((edge_serial->is_epic) &&
1777             (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1778                /* flush and chase */
1779                edge_port->chaseResponsePending = true;
1780
1781                dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
1782                status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1783                if (status == 0) {
1784                        /* block until chase finished */
1785                        block_until_chase_response(edge_port);
1786                } else {
1787                        edge_port->chaseResponsePending = false;
1788                }
1789        }
1790
1791        if ((!edge_serial->is_epic) ||
1792            ((edge_serial->is_epic) &&
1793             (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1794                if (break_state == -1) {
1795                        dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
1796                        status = send_iosp_ext_cmd(edge_port,
1797                                                IOSP_CMD_SET_BREAK, 0);
1798                } else {
1799                        dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
1800                        status = send_iosp_ext_cmd(edge_port,
1801                                                IOSP_CMD_CLEAR_BREAK, 0);
1802                }
1803                if (status)
1804                        dbg("%s - error sending break set/clear command.",
1805                                __func__);
1806        }
1807
1808        return;
1809}
1810
1811
1812/*****************************************************************************
1813 * process_rcvd_data
1814 *      this function handles the data received on the bulk in pipe.
1815 *****************************************************************************/
1816static void process_rcvd_data(struct edgeport_serial *edge_serial,
1817                                unsigned char *buffer, __u16 bufferLength)
1818{
1819        struct usb_serial_port *port;
1820        struct edgeport_port *edge_port;
1821        struct tty_struct *tty;
1822        __u16 lastBufferLength;
1823        __u16 rxLen;
1824
1825        dbg("%s", __func__);
1826
1827        lastBufferLength = bufferLength + 1;
1828
1829        while (bufferLength > 0) {
1830                /* failsafe incase we get a message that we don't understand */
1831                if (lastBufferLength == bufferLength) {
1832                        dbg("%s - stuck in loop, exiting it.", __func__);
1833                        break;
1834                }
1835                lastBufferLength = bufferLength;
1836
1837                switch (edge_serial->rxState) {
1838                case EXPECT_HDR1:
1839                        edge_serial->rxHeader1 = *buffer;
1840                        ++buffer;
1841                        --bufferLength;
1842
1843                        if (bufferLength == 0) {
1844                                edge_serial->rxState = EXPECT_HDR2;
1845                                break;
1846                        }
1847                        /* otherwise, drop on through */
1848                case EXPECT_HDR2:
1849                        edge_serial->rxHeader2 = *buffer;
1850                        ++buffer;
1851                        --bufferLength;
1852
1853                        dbg("%s - Hdr1=%02X Hdr2=%02X", __func__,
1854                            edge_serial->rxHeader1, edge_serial->rxHeader2);
1855                        /* Process depending on whether this header is
1856                         * data or status */
1857
1858                        if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1859                                /* Decode this status header and go to
1860                                 * EXPECT_HDR1 (if we can process the status
1861                                 * with only 2 bytes), or go to EXPECT_HDR3 to
1862                                 * get the third byte. */
1863                                edge_serial->rxPort =
1864                                    IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1865                                edge_serial->rxStatusCode =
1866                                    IOSP_GET_STATUS_CODE(
1867                                                edge_serial->rxHeader1);
1868
1869                                if (!IOSP_STATUS_IS_2BYTE(
1870                                                edge_serial->rxStatusCode)) {
1871                                        /* This status needs additional bytes.
1872                                         * Save what we have and then wait for
1873                                         * more data.
1874                                         */
1875                                        edge_serial->rxStatusParam
1876                                                = edge_serial->rxHeader2;
1877                                        edge_serial->rxState = EXPECT_HDR3;
1878                                        break;
1879                                }
1880                                /* We have all the header bytes, process the
1881                                   status now */
1882                                process_rcvd_status(edge_serial,
1883                                                edge_serial->rxHeader2, 0);
1884                                edge_serial->rxState = EXPECT_HDR1;
1885                                break;
1886                        } else {
1887                                edge_serial->rxPort =
1888                                    IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1889                                edge_serial->rxBytesRemaining =
1890                                    IOSP_GET_HDR_DATA_LEN(
1891                                                edge_serial->rxHeader1,
1892                                                edge_serial->rxHeader2);
1893                                dbg("%s - Data for Port %u Len %u",
1894                                                __func__,
1895                                                edge_serial->rxPort,
1896                                                edge_serial->rxBytesRemaining);
1897
1898                                /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1899                                 * ASSERT(DevExt->RxBytesRemaining <
1900                                 *              IOSP_MAX_DATA_LENGTH);
1901                                 */
1902
1903                                if (bufferLength == 0) {
1904                                        edge_serial->rxState = EXPECT_DATA;
1905                                        break;
1906                                }
1907                                /* Else, drop through */
1908                        }
1909                case EXPECT_DATA: /* Expect data */
1910                        if (bufferLength < edge_serial->rxBytesRemaining) {
1911                                rxLen = bufferLength;
1912                                /* Expect data to start next buffer */
1913                                edge_serial->rxState = EXPECT_DATA;
1914                        } else {
1915                                /* BufLen >= RxBytesRemaining */
1916                                rxLen = edge_serial->rxBytesRemaining;
1917                                /* Start another header next time */
1918                                edge_serial->rxState = EXPECT_HDR1;
1919                        }
1920
1921                        bufferLength -= rxLen;
1922                        edge_serial->rxBytesRemaining -= rxLen;
1923
1924                        /* spit this data back into the tty driver if this
1925                           port is open */
1926                        if (rxLen) {
1927                                port = edge_serial->serial->port[
1928                                                        edge_serial->rxPort];
1929                                edge_port = usb_get_serial_port_data(port);
1930                                if (edge_port->open) {
1931                                        tty = edge_port->port->port.tty;
1932                                        if (tty) {
1933                                                dbg("%s - Sending %d bytes to TTY for port %d",
1934                                                        __func__, rxLen, edge_serial->rxPort);
1935                                                edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1936                                        }
1937                                        edge_port->icount.rx += rxLen;
1938                                }
1939                                buffer += rxLen;
1940                        }
1941                        break;
1942
1943                case EXPECT_HDR3:       /* Expect 3rd byte of status header */
1944                        edge_serial->rxHeader3 = *buffer;
1945                        ++buffer;
1946                        --bufferLength;
1947
1948                        /* We have all the header bytes, process the
1949                           status now */
1950                        process_rcvd_status(edge_serial,
1951                                edge_serial->rxStatusParam,
1952                                edge_serial->rxHeader3);
1953                        edge_serial->rxState = EXPECT_HDR1;
1954                        break;
1955                }
1956        }
1957}
1958
1959
1960/*****************************************************************************
1961 * process_rcvd_status
1962 *      this function handles the any status messages received on the
1963 *      bulk in pipe.
1964 *****************************************************************************/
1965static void process_rcvd_status(struct edgeport_serial *edge_serial,
1966                                                __u8 byte2, __u8 byte3)
1967{
1968        struct usb_serial_port *port;
1969        struct edgeport_port *edge_port;
1970        __u8 code = edge_serial->rxStatusCode;
1971
1972        /* switch the port pointer to the one being currently talked about */
1973        port = edge_serial->serial->port[edge_serial->rxPort];
1974        edge_port = usb_get_serial_port_data(port);
1975        if (edge_port == NULL) {
1976                dev_err(&edge_serial->serial->dev->dev,
1977                        "%s - edge_port == NULL for port %d\n",
1978                                        __func__, edge_serial->rxPort);
1979                return;
1980        }
1981
1982        dbg("%s - port %d", __func__, edge_serial->rxPort);
1983
1984        if (code == IOSP_EXT_STATUS) {
1985                switch (byte2) {
1986                case IOSP_EXT_STATUS_CHASE_RSP:
1987                        /* we want to do EXT status regardless of port
1988                         * open/closed */
1989                        dbg("%s - Port %u EXT CHASE_RSP Data = %02x",
1990                                        __func__, edge_serial->rxPort, byte3);
1991                        /* Currently, the only EXT_STATUS is Chase, so process
1992                         * here instead of one more call to one more subroutine
1993                         * If/when more EXT_STATUS, there'll be more work to do
1994                         * Also, we currently clear flag and close the port
1995                         * regardless of content of above's Byte3.
1996                         * We could choose to do something else when Byte3 says
1997                         * Timeout on Chase from Edgeport, like wait longer in
1998                         * block_until_chase_response, but for now we don't.
1999                         */
2000                        edge_port->chaseResponsePending = false;
2001                        wake_up(&edge_port->wait_chase);
2002                        return;
2003
2004                case IOSP_EXT_STATUS_RX_CHECK_RSP:
2005                        dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3);
2006                        /* Port->RxCheckRsp = true; */
2007                        return;
2008                }
2009        }
2010
2011        if (code == IOSP_STATUS_OPEN_RSP) {
2012                edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2013                edge_port->maxTxCredits = edge_port->txCredits;
2014                dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
2015                handle_new_msr(edge_port, byte2);
2016
2017                /* send the current line settings to the port so we are
2018                   in sync with any further termios calls */
2019                /* FIXME: locking on tty */
2020                if (edge_port->port->port.tty)
2021                        change_port_settings(edge_port->port->port.tty,
2022                                edge_port, edge_port->port->port.tty->termios);
2023
2024                /* we have completed the open */
2025                edge_port->openPending = false;
2026                edge_port->open = true;
2027                wake_up(&edge_port->wait_open);
2028                return;
2029        }
2030
2031        /* If port is closed, silently discard all rcvd status. We can
2032         * have cases where buffered status is received AFTER the close
2033         * port command is sent to the Edgeport.
2034         */
2035        if (!edge_port->open || edge_port->closePending)
2036                return;
2037
2038        switch (code) {
2039        /* Not currently sent by Edgeport */
2040        case IOSP_STATUS_LSR:
2041                dbg("%s - Port %u LSR Status = %02x",
2042                                        __func__, edge_serial->rxPort, byte2);
2043                handle_new_lsr(edge_port, false, byte2, 0);
2044                break;
2045
2046        case IOSP_STATUS_LSR_DATA:
2047                dbg("%s - Port %u LSR Status = %02x, Data = %02x",
2048                                __func__, edge_serial->rxPort, byte2, byte3);
2049                /* byte2 is LSR Register */
2050                /* byte3 is broken data byte */
2051                handle_new_lsr(edge_port, true, byte2, byte3);
2052                break;
2053        /*
2054         *      case IOSP_EXT_4_STATUS:
2055         *              dbg("%s - Port %u LSR Status = %02x Data = %02x",
2056         *                      __func__, edge_serial->rxPort, byte2, byte3);
2057         *              break;
2058         */
2059        case IOSP_STATUS_MSR:
2060                dbg("%s - Port %u MSR Status = %02x",
2061                                        __func__, edge_serial->rxPort, byte2);
2062                /*
2063                 * Process this new modem status and generate appropriate
2064                 * events, etc, based on the new status. This routine
2065                 * also saves the MSR in Port->ShadowMsr.
2066                 */
2067                handle_new_msr(edge_port, byte2);
2068                break;
2069
2070        default:
2071                dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
2072                break;
2073        }
2074        return;
2075}
2076
2077
2078/*****************************************************************************
2079 * edge_tty_recv
2080 *      this function passes data on to the tty flip buffer
2081 *****************************************************************************/
2082static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2083                                        unsigned char *data, int length)
2084{
2085        int cnt;
2086
2087        do {
2088                cnt = tty_buffer_request_room(tty, length);
2089                if (cnt < length) {
2090                        dev_err(dev, "%s - dropping data, %d bytes lost\n",
2091                                        __func__, length - cnt);
2092                        if (cnt == 0)
2093                                break;
2094                }
2095                tty_insert_flip_string(tty, data, cnt);
2096                data += cnt;
2097                length -= cnt;
2098        } while (length > 0);
2099
2100        tty_flip_buffer_push(tty);
2101}
2102
2103
2104/*****************************************************************************
2105 * handle_new_msr
2106 *      this function handles any change to the msr register for a port.
2107 *****************************************************************************/
2108static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2109{
2110        struct  async_icount *icount;
2111
2112        dbg("%s %02x", __func__, newMsr);
2113
2114        if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2115                        EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2116                icount = &edge_port->icount;
2117
2118                /* update input line counters */
2119                if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2120                        icount->cts++;
2121                if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2122                        icount->dsr++;
2123                if (newMsr & EDGEPORT_MSR_DELTA_CD)
2124                        icount->dcd++;
2125                if (newMsr & EDGEPORT_MSR_DELTA_RI)
2126                        icount->rng++;
2127                wake_up_interruptible(&edge_port->delta_msr_wait);
2128        }
2129
2130        /* Save the new modem status */
2131        edge_port->shadowMSR = newMsr & 0xf0;
2132
2133        return;
2134}
2135
2136
2137/*****************************************************************************
2138 * handle_new_lsr
2139 *      this function handles any change to the lsr register for a port.
2140 *****************************************************************************/
2141static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2142                                                        __u8 lsr, __u8 data)
2143{
2144        __u8 newLsr = (__u8) (lsr & (__u8)
2145                (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2146        struct async_icount *icount;
2147
2148        dbg("%s - %02x", __func__, newLsr);
2149
2150        edge_port->shadowLSR = lsr;
2151
2152        if (newLsr & LSR_BREAK) {
2153                /*
2154                 * Parity and Framing errors only count if they
2155                 * occur exclusive of a break being
2156                 * received.
2157                 */
2158                newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2159        }
2160
2161        /* Place LSR data byte into Rx buffer */
2162        if (lsrData && edge_port->port->port.tty)
2163                edge_tty_recv(&edge_port->port->dev,
2164                                        edge_port->port->port.tty, &data, 1);
2165
2166        /* update input line counters */
2167        icount = &edge_port->icount;
2168        if (newLsr & LSR_BREAK)
2169                icount->brk++;
2170        if (newLsr & LSR_OVER_ERR)
2171                icount->overrun++;
2172        if (newLsr & LSR_PAR_ERR)
2173                icount->parity++;
2174        if (newLsr & LSR_FRM_ERR)
2175                icount->frame++;
2176
2177        return;
2178}
2179
2180
2181/****************************************************************************
2182 * sram_write
2183 *      writes a number of bytes to the Edgeport device's sram starting at the
2184 *      given address.
2185 *      If successful returns the number of bytes written, otherwise it returns
2186 *      a negative error number of the problem.
2187 ****************************************************************************/
2188static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2189                                        __u16 length, const __u8 *data)
2190{
2191        int result;
2192        __u16 current_length;
2193        unsigned char *transfer_buffer;
2194
2195        dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2196
2197        transfer_buffer =  kmalloc(64, GFP_KERNEL);
2198        if (!transfer_buffer) {
2199                dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2200                                                        __func__, 64);
2201                return -ENOMEM;
2202        }
2203
2204        /* need to split these writes up into 64 byte chunks */
2205        result = 0;
2206        while (length > 0) {
2207                if (length > 64)
2208                        current_length = 64;
2209                else
2210                        current_length = length;
2211
2212/*              dbg("%s - writing %x, %x, %d", __func__,
2213                                        extAddr, addr, current_length); */
2214                memcpy(transfer_buffer, data, current_length);
2215                result = usb_control_msg(serial->dev,
2216                                        usb_sndctrlpipe(serial->dev, 0),
2217                                        USB_REQUEST_ION_WRITE_RAM,
2218                                        0x40, addr, extAddr, transfer_buffer,
2219                                        current_length, 300);
2220                if (result < 0)
2221                        break;
2222                length -= current_length;
2223                addr += current_length;
2224                data += current_length;
2225        }
2226
2227        kfree(transfer_buffer);
2228        return result;
2229}
2230
2231
2232/****************************************************************************
2233 * rom_write
2234 *      writes a number of bytes to the Edgeport device's ROM starting at the
2235 *      given address.
2236 *      If successful returns the number of bytes written, otherwise it returns
2237 *      a negative error number of the problem.
2238 ****************************************************************************/
2239static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2240                                        __u16 length, const __u8 *data)
2241{
2242        int result;
2243        __u16 current_length;
2244        unsigned char *transfer_buffer;
2245
2246/*      dbg("%s - %x, %x, %d", __func__, extAddr, addr, length); */
2247
2248        transfer_buffer =  kmalloc(64, GFP_KERNEL);
2249        if (!transfer_buffer) {
2250                dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2251                                                                __func__, 64);
2252                return -ENOMEM;
2253        }
2254
2255        /* need to split these writes up into 64 byte chunks */
2256        result = 0;
2257        while (length > 0) {
2258                if (length > 64)
2259                        current_length = 64;
2260                else
2261                        current_length = length;
2262/*              dbg("%s - writing %x, %x, %d", __func__,
2263                                        extAddr, addr, current_length); */
2264                memcpy(transfer_buffer, data, current_length);
2265                result = usb_control_msg(serial->dev,
2266                                        usb_sndctrlpipe(serial->dev, 0),
2267                                        USB_REQUEST_ION_WRITE_ROM, 0x40,
2268                                        addr, extAddr,
2269                                        transfer_buffer, current_length, 300);
2270                if (result < 0)
2271                        break;
2272                length -= current_length;
2273                addr += current_length;
2274                data += current_length;
2275        }
2276
2277        kfree(transfer_buffer);
2278        return result;
2279}
2280
2281
2282/****************************************************************************
2283 * rom_read
2284 *      reads a number of bytes from the Edgeport device starting at the given
2285 *      address.
2286 *      If successful returns the number of bytes read, otherwise it returns
2287 *      a negative error number of the problem.
2288 ****************************************************************************/
2289static int rom_read(struct usb_serial *serial, __u16 extAddr,
2290                                        __u16 addr, __u16 length, __u8 *data)
2291{
2292        int result;
2293        __u16 current_length;
2294        unsigned char *transfer_buffer;
2295
2296        dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
2297
2298        transfer_buffer =  kmalloc(64, GFP_KERNEL);
2299        if (!transfer_buffer) {
2300                dev_err(&serial->dev->dev,
2301                        "%s - kmalloc(%d) failed.\n", __func__, 64);
2302                return -ENOMEM;
2303        }
2304
2305        /* need to split these reads up into 64 byte chunks */
2306        result = 0;
2307        while (length > 0) {
2308                if (length > 64)
2309                        current_length = 64;
2310                else
2311                        current_length = length;
2312/*              dbg("%s - %x, %x, %d", __func__,
2313                                extAddr, addr, current_length); */
2314                result = usb_control_msg(serial->dev,
2315                                        usb_rcvctrlpipe(serial->dev, 0),
2316                                        USB_REQUEST_ION_READ_ROM,
2317                                        0xC0, addr, extAddr, transfer_buffer,
2318                                        current_length, 300);
2319                if (result < 0)
2320                        break;
2321                memcpy(data, transfer_buffer, current_length);
2322                length -= current_length;
2323                addr += current_length;
2324                data += current_length;
2325        }
2326
2327        kfree(transfer_buffer);
2328        return result;
2329}
2330
2331
2332/****************************************************************************
2333 * send_iosp_ext_cmd
2334 *      Is used to send a IOSP message to the Edgeport device
2335 ****************************************************************************/
2336static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2337                                                __u8 command, __u8 param)
2338{
2339        unsigned char   *buffer;
2340        unsigned char   *currentCommand;
2341        int             length = 0;
2342        int             status = 0;
2343
2344        dbg("%s - %d, %d", __func__, command, param);
2345
2346        buffer = kmalloc(10, GFP_ATOMIC);
2347        if (!buffer) {
2348                dev_err(&edge_port->port->dev,
2349                                "%s - kmalloc(%d) failed.\n", __func__, 10);
2350                return -ENOMEM;
2351        }
2352
2353        currentCommand = buffer;
2354
2355        MAKE_CMD_EXT_CMD(&currentCommand, &length,
2356                edge_port->port->number - edge_port->port->serial->minor,
2357                command, param);
2358
2359        status = write_cmd_usb(edge_port, buffer, length);
2360        if (status) {
2361                /* something bad happened, let's free up the memory */
2362                kfree(buffer);
2363        }
2364
2365        return status;
2366}
2367
2368
2369/*****************************************************************************
2370 * write_cmd_usb
2371 *      this function writes the given buffer out to the bulk write endpoint.
2372 *****************************************************************************/
2373static int write_cmd_usb(struct edgeport_port *edge_port,
2374                                        unsigned char *buffer, int length)
2375{
2376        struct edgeport_serial *edge_serial =
2377                                usb_get_serial_data(edge_port->port->serial);
2378        int status = 0;
2379        struct urb *urb;
2380        int timeout;
2381
2382        usb_serial_debug_data(debug, &edge_port->port->dev,
2383                                                __func__, length, buffer);
2384
2385        /* Allocate our next urb */
2386        urb = usb_alloc_urb(0, GFP_ATOMIC);
2387        if (!urb)
2388                return -ENOMEM;
2389
2390        atomic_inc(&CmdUrbs);
2391        dbg("%s - ALLOCATE URB %p (outstanding %d)",
2392                                __func__, urb, atomic_read(&CmdUrbs));
2393
2394        usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2395                        usb_sndbulkpipe(edge_serial->serial->dev,
2396                                        edge_serial->bulk_out_endpoint),
2397                        buffer, length, edge_bulk_out_cmd_callback, edge_port);
2398
2399        edge_port->commandPending = true;
2400        status = usb_submit_urb(urb, GFP_ATOMIC);
2401
2402        if (status) {
2403                /* something went wrong */
2404                dev_err(&edge_port->port->dev,
2405                    "%s - usb_submit_urb(write command) failed, status = %d\n",
2406                                                        __func__, status);
2407                usb_kill_urb(urb);
2408                usb_free_urb(urb);
2409                atomic_dec(&CmdUrbs);
2410                return status;
2411        }
2412
2413        /* wait for command to finish */
2414        timeout = COMMAND_TIMEOUT;
2415#if 0
2416        wait_event(&edge_port->wait_command, !edge_port->commandPending);
2417
2418        if (edge_port->commandPending) {
2419                /* command timed out */
2420                dbg("%s - command timed out", __func__);
2421                status = -EINVAL;
2422        }
2423#endif
2424        return status;
2425}
2426
2427
2428/*****************************************************************************
2429 * send_cmd_write_baud_rate
2430 *      this function sends the proper command to change the baud rate of the
2431 *      specified port.
2432 *****************************************************************************/
2433static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2434                                                                int baudRate)
2435{
2436        struct edgeport_serial *edge_serial =
2437                                usb_get_serial_data(edge_port->port->serial);
2438        unsigned char *cmdBuffer;
2439        unsigned char *currCmd;
2440        int cmdLen = 0;
2441        int divisor;
2442        int status;
2443        unsigned char number =
2444                edge_port->port->number - edge_port->port->serial->minor;
2445
2446        if (edge_serial->is_epic &&
2447            !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2448                dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2449                    edge_port->port->number, baudRate);
2450                return 0;
2451        }
2452
2453        dbg("%s - port = %d, baud = %d", __func__,
2454                                        edge_port->port->number, baudRate);
2455
2456        status = calc_baud_rate_divisor(baudRate, &divisor);
2457        if (status) {
2458                dev_err(&edge_port->port->dev, "%s - bad baud rate\n",
2459                                                                __func__);
2460                return status;
2461        }
2462
2463        /* Alloc memory for the string of commands. */
2464        cmdBuffer =  kmalloc(0x100, GFP_ATOMIC);
2465        if (!cmdBuffer) {
2466                dev_err(&edge_port->port->dev,
2467                        "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2468                return -ENOMEM;
2469        }
2470        currCmd = cmdBuffer;
2471
2472        /* Enable access to divisor latch */
2473        MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2474
2475        /* Write the divisor itself */
2476        MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2477        MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2478
2479        /* Restore original value to disable access to divisor latch */
2480        MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2481                                                edge_port->shadowLCR);
2482
2483        status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2484        if (status) {
2485                /* something bad happened, let's free up the memory */
2486                kfree(cmdBuffer);
2487        }
2488
2489        return status;
2490}
2491
2492
2493/*****************************************************************************
2494 * calc_baud_rate_divisor
2495 *      this function calculates the proper baud rate divisor for the specified
2496 *      baud rate.
2497 *****************************************************************************/
2498static int calc_baud_rate_divisor(int baudrate, int *divisor)
2499{
2500        int i;
2501        __u16 custom;
2502
2503
2504        dbg("%s - %d", __func__, baudrate);
2505
2506        for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2507                if (divisor_table[i].BaudRate == baudrate) {
2508                        *divisor = divisor_table[i].Divisor;
2509                        return 0;
2510                }
2511        }
2512
2513        /* We have tried all of the standard baud rates
2514         * lets try to calculate the divisor for this baud rate
2515         * Make sure the baud rate is reasonable */
2516        if (baudrate > 50 && baudrate < 230400) {
2517                /* get divisor */
2518                custom = (__u16)((230400L + baudrate/2) / baudrate);
2519
2520                *divisor = custom;
2521
2522                dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
2523                return 0;
2524        }
2525
2526        return -1;
2527}
2528
2529
2530/*****************************************************************************
2531 * send_cmd_write_uart_register
2532 *  this function builds up a uart register message and sends to to the device.
2533 *****************************************************************************/
2534static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2535                                                __u8 regNum, __u8 regValue)
2536{
2537        struct edgeport_serial *edge_serial =
2538                                usb_get_serial_data(edge_port->port->serial);
2539        unsigned char *cmdBuffer;
2540        unsigned char *currCmd;
2541        unsigned long cmdLen = 0;
2542        int status;
2543
2544        dbg("%s - write to %s register 0x%02x",
2545                        (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2546
2547        if (edge_serial->is_epic &&
2548            !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2549            regNum == MCR) {
2550                dbg("SendCmdWriteUartReg - Not writing to MCR Register");
2551                return 0;
2552        }
2553
2554        if (edge_serial->is_epic &&
2555            !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2556            regNum == LCR) {
2557                dbg("SendCmdWriteUartReg - Not writing to LCR Register");
2558                return 0;
2559        }
2560
2561        /* Alloc memory for the string of commands. */
2562        cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2563        if (cmdBuffer == NULL)
2564                return -ENOMEM;
2565
2566        currCmd = cmdBuffer;
2567
2568        /* Build a cmd in the buffer to write the given register */
2569        MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2570                edge_port->port->number - edge_port->port->serial->minor,
2571                regNum, regValue);
2572
2573        status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2574        if (status) {
2575                /* something bad happened, let's free up the memory */
2576                kfree(cmdBuffer);
2577        }
2578
2579        return status;
2580}
2581
2582
2583/*****************************************************************************
2584 * change_port_settings
2585 *      This routine is called to set the UART on the device to match the
2586 *      specified new settings.
2587 *****************************************************************************/
2588
2589static void change_port_settings(struct tty_struct *tty,
2590        struct edgeport_port *edge_port, struct ktermios *old_termios)
2591{
2592        struct edgeport_serial *edge_serial =
2593                        usb_get_serial_data(edge_port->port->serial);
2594        int baud;
2595        unsigned cflag;
2596        __u8 mask = 0xff;
2597        __u8 lData;
2598        __u8 lParity;
2599        __u8 lStop;
2600        __u8 rxFlow;
2601        __u8 txFlow;
2602        int status;
2603
2604        dbg("%s - port %d", __func__, edge_port->port->number);
2605
2606        if (!edge_port->open &&
2607            !edge_port->openPending) {
2608                dbg("%s - port not opened", __func__);
2609                return;
2610        }
2611
2612        cflag = tty->termios->c_cflag;
2613
2614        switch (cflag & CSIZE) {
2615        case CS5:
2616                lData = LCR_BITS_5; mask = 0x1f;
2617                dbg("%s - data bits = 5", __func__);
2618                break;
2619        case CS6:
2620                lData = LCR_BITS_6; mask = 0x3f;
2621                dbg("%s - data bits = 6", __func__);
2622                break;
2623        case CS7:
2624                lData = LCR_BITS_7; mask = 0x7f;
2625                dbg("%s - data bits = 7", __func__);
2626                break;
2627        default:
2628        case CS8:
2629                lData = LCR_BITS_8;
2630                dbg("%s - data bits = 8", __func__);
2631                break;
2632        }
2633
2634        lParity = LCR_PAR_NONE;
2635        if (cflag & PARENB) {
2636                if (cflag & CMSPAR) {
2637                        if (cflag & PARODD) {
2638                                lParity = LCR_PAR_MARK;
2639                                dbg("%s - parity = mark", __func__);
2640                        } else {
2641                                lParity = LCR_PAR_SPACE;
2642                                dbg("%s - parity = space", __func__);
2643                        }
2644                } else if (cflag & PARODD) {
2645                        lParity = LCR_PAR_ODD;
2646                        dbg("%s - parity = odd", __func__);
2647                } else {
2648                        lParity = LCR_PAR_EVEN;
2649                        dbg("%s - parity = even", __func__);
2650                }
2651        } else {
2652                dbg("%s - parity = none", __func__);
2653        }
2654
2655        if (cflag & CSTOPB) {
2656                lStop = LCR_STOP_2;
2657                dbg("%s - stop bits = 2", __func__);
2658        } else {
2659                lStop = LCR_STOP_1;
2660                dbg("%s - stop bits = 1", __func__);
2661        }
2662
2663        /* figure out the flow control settings */
2664        rxFlow = txFlow = 0x00;
2665        if (cflag & CRTSCTS) {
2666                rxFlow |= IOSP_RX_FLOW_RTS;
2667                txFlow |= IOSP_TX_FLOW_CTS;
2668                dbg("%s - RTS/CTS is enabled", __func__);
2669        } else {
2670                dbg("%s - RTS/CTS is disabled", __func__);
2671        }
2672
2673        /* if we are implementing XON/XOFF, set the start and stop character
2674           in the device */
2675        if (I_IXOFF(tty) || I_IXON(tty)) {
2676                unsigned char stop_char  = STOP_CHAR(tty);
2677                unsigned char start_char = START_CHAR(tty);
2678
2679                if ((!edge_serial->is_epic) ||
2680                    ((edge_serial->is_epic) &&
2681                     (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2682                        send_iosp_ext_cmd(edge_port,
2683                                        IOSP_CMD_SET_XON_CHAR, start_char);
2684                        send_iosp_ext_cmd(edge_port,
2685                                        IOSP_CMD_SET_XOFF_CHAR, stop_char);
2686                }
2687
2688                /* if we are implementing INBOUND XON/XOFF */
2689                if (I_IXOFF(tty)) {
2690                        rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2691                        dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2692                                        __func__, start_char, stop_char);
2693                } else {
2694                        dbg("%s - INBOUND XON/XOFF is disabled", __func__);
2695                }
2696
2697                /* if we are implementing OUTBOUND XON/XOFF */
2698                if (I_IXON(tty)) {
2699                        txFlow |= IOSP_TX_FLOW_XON_XOFF;
2700                        dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2701                                        __func__, start_char, stop_char);
2702                } else {
2703                        dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
2704                }
2705        }
2706
2707        /* Set flow control to the configured value */
2708        if ((!edge_serial->is_epic) ||
2709            ((edge_serial->is_epic) &&
2710             (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2711                send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2712        if ((!edge_serial->is_epic) ||
2713            ((edge_serial->is_epic) &&
2714             (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2715                send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2716
2717
2718        edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2719        edge_port->shadowLCR |= (lData | lParity | lStop);
2720
2721        edge_port->validDataMask = mask;
2722
2723        /* Send the updated LCR value to the EdgePort */
2724        status = send_cmd_write_uart_register(edge_port, LCR,
2725                                                        edge_port->shadowLCR);
2726        if (status != 0)
2727                return;
2728
2729        /* set up the MCR register and send it to the EdgePort */
2730        edge_port->shadowMCR = MCR_MASTER_IE;
2731        if (cflag & CBAUD)
2732                edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2733
2734        status = send_cmd_write_uart_register(edge_port, MCR,
2735                                                edge_port->shadowMCR);
2736        if (status != 0)
2737                return;
2738
2739        /* Determine divisor based on baud rate */
2740        baud = tty_get_baud_rate(tty);
2741        if (!baud) {
2742                /* pick a default, any default... */
2743                baud = 9600;
2744        }
2745
2746        dbg("%s - baud rate = %d", __func__, baud);
2747        status = send_cmd_write_baud_rate(edge_port, baud);
2748        if (status == -1) {
2749                /* Speed change was not possible - put back the old speed */
2750                baud = tty_termios_baud_rate(old_termios);
2751                tty_encode_baud_rate(tty, baud, baud);
2752        }
2753        return;
2754}
2755
2756
2757/****************************************************************************
2758 * unicode_to_ascii
2759 *      Turns a string from Unicode into ASCII.
2760 *      Doesn't do a good job with any characters that are outside the normal
2761 *      ASCII range, but it's only for debugging...
2762 *      NOTE: expects the unicode in LE format
2763 ****************************************************************************/
2764static void unicode_to_ascii(char *string, int buflen,
2765                                        __le16 *unicode, int unicode_size)
2766{
2767        int i;
2768
2769        if (buflen <= 0)        /* never happens, but... */
2770                return;
2771        --buflen;               /* space for nul */
2772
2773        for (i = 0; i < unicode_size; i++) {
2774                if (i >= buflen)
2775                        break;
2776                string[i] = (char)(le16_to_cpu(unicode[i]));
2777        }
2778        string[i] = 0x00;
2779}
2780
2781
2782/****************************************************************************
2783 * get_manufacturing_desc
2784 *      reads in the manufacturing descriptor and stores it into the serial
2785 *      structure.
2786 ****************************************************************************/
2787static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2788{
2789        int response;
2790
2791        dbg("getting manufacturer descriptor");
2792
2793        response = rom_read(edge_serial->serial,
2794                                (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2795                                (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2796                                EDGE_MANUF_DESC_LEN,
2797                                (__u8 *)(&edge_serial->manuf_descriptor));
2798
2799        if (response < 1)
2800                dev_err(&edge_serial->serial->dev->dev,
2801                        "error in getting manufacturer descriptor\n");
2802        else {
2803                char string[30];
2804                dbg("**Manufacturer Descriptor");
2805                dbg("  RomSize:        %dK",
2806                        edge_serial->manuf_descriptor.RomSize);
2807                dbg("  RamSize:        %dK",
2808                        edge_serial->manuf_descriptor.RamSize);
2809                dbg("  CpuRev:         %d",
2810                        edge_serial->manuf_descriptor.CpuRev);
2811                dbg("  BoardRev:       %d",
2812                        edge_serial->manuf_descriptor.BoardRev);
2813                dbg("  NumPorts:       %d",
2814                        edge_serial->manuf_descriptor.NumPorts);
2815                dbg("  DescDate:       %d/%d/%d",
2816                        edge_serial->manuf_descriptor.DescDate[0],
2817                        edge_serial->manuf_descriptor.DescDate[1],
2818                        edge_serial->manuf_descriptor.DescDate[2]+1900);
2819                unicode_to_ascii(string, sizeof(string),
2820                        edge_serial->manuf_descriptor.SerialNumber,
2821                        edge_serial->manuf_descriptor.SerNumLength/2);
2822                dbg("  SerialNumber: %s", string);
2823                unicode_to_ascii(string, sizeof(string),
2824                        edge_serial->manuf_descriptor.AssemblyNumber,
2825                        edge_serial->manuf_descriptor.AssemblyNumLength/2);
2826                dbg("  AssemblyNumber: %s", string);
2827                unicode_to_ascii(string, sizeof(string),
2828                    edge_serial->manuf_descriptor.OemAssyNumber,
2829                    edge_serial->manuf_descriptor.OemAssyNumLength/2);
2830                dbg("  OemAssyNumber:  %s", string);
2831                dbg("  UartType:       %d",
2832                        edge_serial->manuf_descriptor.UartType);
2833                dbg("  IonPid:         %d",
2834                        edge_serial->manuf_descriptor.IonPid);
2835                dbg("  IonConfig:      %d",
2836                        edge_serial->manuf_descriptor.IonConfig);
2837        }
2838}
2839
2840
2841/****************************************************************************
2842 * get_boot_desc
2843 *      reads in the bootloader descriptor and stores it into the serial
2844 *      structure.
2845 ****************************************************************************/
2846static void get_boot_desc(struct edgeport_serial *edge_serial)
2847{
2848        int response;
2849
2850        dbg("getting boot descriptor");
2851
2852        response = rom_read(edge_serial->serial,
2853                                (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2854                                (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2855                                EDGE_BOOT_DESC_LEN,
2856                                (__u8 *)(&edge_serial->boot_descriptor));
2857
2858        if (response < 1)
2859                dev_err(&edge_serial->serial->dev->dev,
2860                                "error in getting boot descriptor\n");
2861        else {
2862                dbg("**Boot Descriptor:");
2863                dbg("  BootCodeLength: %d",
2864                    le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2865                dbg("  MajorVersion:   %d",
2866                        edge_serial->boot_descriptor.MajorVersion);
2867                dbg("  MinorVersion:   %d",
2868                        edge_serial->boot_descriptor.MinorVersion);
2869                dbg("  BuildNumber:    %d",
2870                        le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2871                dbg("  Capabilities:   0x%x",
2872                      le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2873                dbg("  UConfig0:       %d",
2874                        edge_serial->boot_descriptor.UConfig0);
2875                dbg("  UConfig1:       %d",
2876                        edge_serial->boot_descriptor.UConfig1);
2877        }
2878}
2879
2880
2881/****************************************************************************
2882 * load_application_firmware
2883 *      This is called to load the application firmware to the device
2884 ****************************************************************************/
2885static void load_application_firmware(struct edgeport_serial *edge_serial)
2886{
2887        const struct ihex_binrec *rec;
2888        const struct firmware *fw;
2889        const char *fw_name;
2890        const char *fw_info;
2891        int response;
2892        __u32 Operaddr;
2893        __u16 build;
2894
2895        switch (edge_serial->product_info.iDownloadFile) {
2896                case EDGE_DOWNLOAD_FILE_I930:
2897                        fw_info = "downloading firmware version (930)";
2898                        fw_name = "edgeport/down.fw";
2899                        break;
2900
2901                case EDGE_DOWNLOAD_FILE_80251:
2902                        fw_info = "downloading firmware version (80251)";
2903                        fw_name = "edgeport/down2.fw";
2904                        break;
2905
2906                case EDGE_DOWNLOAD_FILE_NONE:
2907                        dbg     ("No download file specified, skipping download\n");
2908                        return;
2909
2910                default:
2911                        return;
2912        }
2913
2914        response = request_ihex_firmware(&fw, fw_name,
2915                                    &edge_serial->serial->dev->dev);
2916        if (response) {
2917                printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2918                       fw_name, response);
2919                return;
2920        }
2921
2922        rec = (const struct ihex_binrec *)fw->data;
2923        build = (rec->data[2] << 8) | rec->data[3];
2924
2925        dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2926
2927        edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2928        edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2929        edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2930
2931        for (rec = ihex_next_binrec(rec); rec;
2932             rec = ihex_next_binrec(rec)) {
2933                Operaddr = be32_to_cpu(rec->addr);
2934                response = sram_write(edge_serial->serial,
2935                                     Operaddr >> 16,
2936                                     Operaddr & 0xFFFF,
2937                                     be16_to_cpu(rec->len),
2938                                     &rec->data[0]);
2939                if (response < 0) {
2940                        dev_err(&edge_serial->serial->dev->dev,
2941                                "sram_write failed (%x, %x, %d)\n",
2942                                Operaddr >> 16, Operaddr & 0xFFFF,
2943                                be16_to_cpu(rec->len));
2944                        break;
2945                }
2946        }
2947
2948        dbg("sending exec_dl_code");
2949        response = usb_control_msg (edge_serial->serial->dev, 
2950                                    usb_sndctrlpipe(edge_serial->serial->dev, 0), 
2951                                    USB_REQUEST_ION_EXEC_DL_CODE, 
2952                                    0x40, 0x4000, 0x0001, NULL, 0, 3000);
2953
2954        release_firmware(fw);
2955        return;
2956}
2957
2958
2959/****************************************************************************
2960 * edge_startup
2961 ****************************************************************************/
2962static int edge_startup(struct usb_serial *serial)
2963{
2964        struct edgeport_serial *edge_serial;
2965        struct edgeport_port *edge_port;
2966        struct usb_device *dev;
2967        int i, j;
2968        int response;
2969        bool interrupt_in_found;
2970        bool bulk_in_found;
2971        bool bulk_out_found;
2972        static __u32 descriptor[3] = {  EDGE_COMPATIBILITY_MASK0,
2973                                        EDGE_COMPATIBILITY_MASK1,
2974                                        EDGE_COMPATIBILITY_MASK2 };
2975
2976        dev = serial->dev;
2977
2978        /* create our private serial structure */
2979        edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2980        if (edge_serial == NULL) {
2981                dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2982                return -ENOMEM;
2983        }
2984        spin_lock_init(&edge_serial->es_lock);
2985        edge_serial->serial = serial;
2986        usb_set_serial_data(serial, edge_serial);
2987
2988        /* get the name for the device from the device */
2989        i = get_string(dev, dev->descriptor.iManufacturer,
2990            &edge_serial->name[0], MAX_NAME_LEN+1);
2991        edge_serial->name[i++] = ' ';
2992        get_string(dev, dev->descriptor.iProduct,
2993            &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2994
2995        dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2996
2997        /* Read the epic descriptor */
2998        if (get_epic_descriptor(edge_serial) <= 0) {
2999                /* memcpy descriptor to Supports structures */
3000                memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
3001                       sizeof(struct edge_compatibility_bits));
3002
3003                /* get the manufacturing descriptor for this device */
3004                get_manufacturing_desc(edge_serial);
3005
3006                /* get the boot descriptor */
3007                get_boot_desc(edge_serial);
3008
3009                get_product_info(edge_serial);
3010        }
3011
3012        /* set the number of ports from the manufacturing description */
3013        /* serial->num_ports = serial->product_info.NumPorts; */
3014        if ((!edge_serial->is_epic) &&
3015            (edge_serial->product_info.NumPorts != serial->num_ports)) {
3016                dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
3017                         "vs. core thinking we have %d ports, email "
3018                         "greg@kroah.com this information.\n",
3019                         edge_serial->product_info.NumPorts,
3020                         serial->num_ports);
3021        }
3022
3023        dbg("%s - time 1 %ld", __func__, jiffies);
3024
3025        /* If not an EPiC device */
3026        if (!edge_serial->is_epic) {
3027                /* now load the application firmware into this device */
3028                load_application_firmware(edge_serial);
3029
3030                dbg("%s - time 2 %ld", __func__, jiffies);
3031
3032                /* Check current Edgeport EEPROM and update if necessary */
3033                update_edgeport_E2PROM(edge_serial);
3034
3035                dbg("%s - time 3 %ld", __func__, jiffies);
3036
3037                /* set the configuration to use #1 */
3038/*              dbg("set_configuration 1"); */
3039/*              usb_set_configuration (dev, 1); */
3040        }
3041        dbg("  FirmwareMajorVersion  %d.%d.%d",
3042            edge_serial->product_info.FirmwareMajorVersion,
3043            edge_serial->product_info.FirmwareMinorVersion,
3044            le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
3045
3046        /* we set up the pointers to the endpoints in the edge_open function,
3047         * as the structures aren't created yet. */
3048
3049        /* set up our port private structures */
3050        for (i = 0; i < serial->num_ports; ++i) {
3051                edge_port = kmalloc(sizeof(struct edgeport_port), GFP_KERNEL);
3052                if (edge_port == NULL) {
3053                        dev_err(&serial->dev->dev, "%s - Out of memory\n",
3054                                                                   __func__);
3055                        for (j = 0; j < i; ++j) {
3056                                kfree(usb_get_serial_port_data(serial->port[j]));
3057                                usb_set_serial_port_data(serial->port[j],
3058                                                                        NULL);
3059                        }
3060                        usb_set_serial_data(serial, NULL);
3061                        kfree(edge_serial);
3062                        return -ENOMEM;
3063                }
3064                memset(edge_port, 0, sizeof(struct edgeport_port));
3065                spin_lock_init(&edge_port->ep_lock);
3066                edge_port->port = serial->port[i];
3067                usb_set_serial_port_data(serial->port[i], edge_port);
3068        }
3069
3070        response = 0;
3071
3072        if (edge_serial->is_epic) {
3073                /* EPIC thing, set up our interrupt polling now and our read
3074                 * urb, so that the device knows it really is connected. */
3075                interrupt_in_found = bulk_in_found = bulk_out_found = false;
3076                for (i = 0; i < serial->interface->altsetting[0]
3077                                                .desc.bNumEndpoints; ++i) {
3078                        struct usb_endpoint_descriptor *endpoint;
3079                        int buffer_size;
3080
3081                        endpoint = &serial->interface->altsetting[0].
3082                                                        endpoint[i].desc;
3083                        buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
3084                        if (!interrupt_in_found &&
3085                            (usb_endpoint_is_int_in(endpoint))) {
3086                                /* we found a interrupt in endpoint */
3087                                dbg("found interrupt in");
3088
3089                                /* not set up yet, so do it now */
3090                                edge_serial->interrupt_read_urb =
3091                                                usb_alloc_urb(0, GFP_KERNEL);
3092                                if (!edge_serial->interrupt_read_urb) {
3093                                        err("out of memory");
3094                                        return -ENOMEM;
3095                                }
3096                                edge_serial->interrupt_in_buffer =
3097                                        kmalloc(buffer_size, GFP_KERNEL);
3098                                if (!edge_serial->interrupt_in_buffer) {
3099                                        err("out of memory");
3100                                        usb_free_urb(edge_serial->interrupt_read_urb);
3101                                        return -ENOMEM;
3102                                }
3103                                edge_serial->interrupt_in_endpoint =
3104                                                endpoint->bEndpointAddress;
3105
3106                                /* set up our interrupt urb */
3107                                usb_fill_int_urb(
3108                                        edge_serial->interrupt_read_urb,
3109                                        dev,
3110                                        usb_rcvintpipe(dev,
3111                                                endpoint->bEndpointAddress),
3112                                        edge_serial->interrupt_in_buffer,
3113                                        buffer_size,
3114                                        edge_interrupt_callback,
3115                                        edge_serial,
3116                                        endpoint->bInterval);
3117
3118                                interrupt_in_found = true;
3119                        }
3120
3121                        if (!bulk_in_found &&
3122                                (usb_endpoint_is_bulk_in(endpoint))) {
3123                                /* we found a bulk in endpoint */
3124                                dbg("found bulk in");
3125
3126                                /* not set up yet, so do it now */
3127                                edge_serial->read_urb =
3128                                                usb_alloc_urb(0, GFP_KERNEL);
3129                                if (!edge_serial->read_urb) {
3130                                        err("out of memory");
3131                                        return -ENOMEM;
3132                                }
3133                                edge_serial->bulk_in_buffer =
3134                                        kmalloc(buffer_size, GFP_KERNEL);
3135                                if (!edge_serial->bulk_in_buffer) {
3136                                        err("out of memory");
3137                                        usb_free_urb(edge_serial->read_urb);
3138                                        return -ENOMEM;
3139                                }
3140                                edge_serial->bulk_in_endpoint =
3141                                                endpoint->bEndpointAddress;
3142
3143                                /* set up our bulk in urb */
3144                                usb_fill_bulk_urb(edge_serial->read_urb, dev,
3145                                        usb_rcvbulkpipe(dev,
3146                                                endpoint->bEndpointAddress),
3147                                        edge_serial->bulk_in_buffer,
3148                                        le16_to_cpu(endpoint->wMaxPacketSize),
3149                                        edge_bulk_in_callback,
3150                                        edge_serial);
3151                                bulk_in_found = true;
3152                        }
3153
3154                        if (!bulk_out_found &&
3155                            (usb_endpoint_is_bulk_out(endpoint))) {
3156                                /* we found a bulk out endpoint */
3157                                dbg("found bulk out");
3158                                edge_serial->bulk_out_endpoint =
3159                                                endpoint->bEndpointAddress;
3160                                bulk_out_found = true;
3161                        }
3162                }
3163
3164                if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3165                        err("Error - the proper endpoints were not found!");
3166                        return -ENODEV;
3167                }
3168
3169                /* start interrupt read for this edgeport this interrupt will
3170                 * continue as long as the edgeport is connected */
3171                response = usb_submit_urb(edge_serial->interrupt_read_urb,
3172                                                                GFP_KERNEL);
3173                if (response)
3174                        err("%s - Error %d submitting control urb",
3175                                                        __func__, response);
3176        }
3177        return response;
3178}
3179
3180
3181/****************************************************************************
3182 * edge_disconnect
3183 *      This function is called whenever the device is removed from the usb bus.
3184 ****************************************************************************/
3185static void edge_disconnect(struct usb_serial *serial)
3186{
3187        struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3188
3189        dbg("%s", __func__);
3190
3191        /* stop reads and writes on all ports */
3192        /* free up our endpoint stuff */
3193        if (edge_serial->is_epic) {
3194                usb_kill_urb(edge_serial->interrupt_read_urb);
3195                usb_free_urb(edge_serial->interrupt_read_urb);
3196                kfree(edge_serial->interrupt_in_buffer);
3197
3198                usb_kill_urb(edge_serial->read_urb);
3199                usb_free_urb(edge_serial->read_urb);
3200                kfree(edge_serial->bulk_in_buffer);
3201        }
3202}
3203
3204
3205/****************************************************************************
3206 * edge_release
3207 *      This function is called when the device structure is deallocated.
3208 ****************************************************************************/
3209static void edge_release(struct usb_serial *serial)
3210{
3211        struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3212        int i;
3213
3214        dbg("%s", __func__);
3215
3216        for (i = 0; i < serial->num_ports; ++i)
3217                kfree(usb_get_serial_port_data(serial->port[i]));
3218
3219        kfree(edge_serial);
3220}
3221
3222
3223/****************************************************************************
3224 * edgeport_init
3225 *      This is called by the module subsystem, or on startup to initialize us
3226 ****************************************************************************/
3227static int __init edgeport_init(void)
3228{
3229        int retval;
3230
3231        retval = usb_serial_register(&edgeport_2port_device);
3232        if (retval)
3233                goto failed_2port_device_register;
3234        retval = usb_serial_register(&edgeport_4port_device);
3235        if (retval)
3236                goto failed_4port_device_register;
3237        retval = usb_serial_register(&edgeport_8port_device);
3238        if (retval)
3239                goto failed_8port_device_register;
3240        retval = usb_serial_register(&epic_device);
3241        if (retval)
3242                goto failed_epic_device_register;
3243        retval = usb_register(&io_driver);
3244        if (retval)
3245                goto failed_usb_register;
3246        atomic_set(&CmdUrbs, 0);
3247        info(DRIVER_DESC " " DRIVER_VERSION);
3248        return 0;
3249
3250failed_usb_register:
3251        usb_serial_deregister(&epic_device);
3252failed_epic_device_register:
3253        usb_serial_deregister(&edgeport_8port_device);
3254failed_8port_device_register:
3255        usb_serial_deregister(&edgeport_4port_device);
3256failed_4port_device_register:
3257        usb_serial_deregister(&edgeport_2port_device);
3258failed_2port_device_register:
3259        return retval;
3260}
3261
3262
3263/****************************************************************************
3264 * edgeport_exit
3265 *      Called when the driver is about to be unloaded.
3266 ****************************************************************************/
3267static void __exit edgeport_exit (void)
3268{
3269        usb_deregister(&io_driver);
3270        usb_serial_deregister(&edgeport_2port_device);
3271        usb_serial_deregister(&edgeport_4port_device);
3272        usb_serial_deregister(&edgeport_8port_device);
3273        usb_serial_deregister(&epic_device);
3274}
3275
3276module_init(edgeport_init);
3277module_exit(edgeport_exit);
3278
3279/* Module information */
3280MODULE_AUTHOR(DRIVER_AUTHOR);
3281MODULE_DESCRIPTION(DRIVER_DESC);
3282MODULE_LICENSE("GPL");
3283MODULE_FIRMWARE("edgeport/boot.fw");
3284MODULE_FIRMWARE("edgeport/boot2.fw");
3285MODULE_FIRMWARE("edgeport/down.fw");
3286MODULE_FIRMWARE("edgeport/down2.fw");
3287
3288module_param(debug, bool, S_IRUGO | S_IWUSR);
3289MODULE_PARM_DESC(debug, "Debug enabled or not");
3290
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.