linux/drivers/usb/serial/keyspan.c
<<
>>
Prefs
   1/*
   2  Keyspan USB to Serial Converter driver
   3
   4  (C) Copyright (C) 2000-2001   Hugh Blemings <hugh@blemings.org>
   5  (C) Copyright (C) 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  See http://blemings.org/hugh/keyspan.html for more information.
  13
  14  Code in this driver inspired by and in a number of places taken
  15  from Brian Warner's original Keyspan-PDA driver.
  16
  17  This driver has been put together with the support of Innosys, Inc.
  18  and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
  19  Thanks Guys :)
  20
  21  Thanks to Paulus for miscellaneous tidy ups, some largish chunks
  22  of much nicer and/or completely new code and (perhaps most uniquely)
  23  having the patience to sit down and explain why and where he'd changed
  24  stuff.
  25
  26  Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
  27  staff in their work on open source projects.
  28*/
  29
  30
  31#include <linux/kernel.h>
  32#include <linux/jiffies.h>
  33#include <linux/errno.h>
  34#include <linux/init.h>
  35#include <linux/slab.h>
  36#include <linux/tty.h>
  37#include <linux/tty_driver.h>
  38#include <linux/tty_flip.h>
  39#include <linux/module.h>
  40#include <linux/spinlock.h>
  41#include <linux/uaccess.h>
  42#include <linux/usb.h>
  43#include <linux/usb/serial.h>
  44#include <linux/usb/ezusb.h>
  45#include "keyspan.h"
  46
  47#define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
  48#define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
  49
  50#define INSTAT_BUFLEN   32
  51#define GLOCONT_BUFLEN  64
  52#define INDAT49W_BUFLEN 512
  53
  54        /* Per device and per port private data */
  55struct keyspan_serial_private {
  56        const struct keyspan_device_details     *device_details;
  57
  58        struct urb      *instat_urb;
  59        char            instat_buf[INSTAT_BUFLEN];
  60
  61        /* added to support 49wg, where data from all 4 ports comes in
  62           on 1 EP and high-speed supported */
  63        struct urb      *indat_urb;
  64        char            indat_buf[INDAT49W_BUFLEN];
  65
  66        /* XXX this one probably will need a lock */
  67        struct urb      *glocont_urb;
  68        char            glocont_buf[GLOCONT_BUFLEN];
  69        char            ctrl_buf[8];    /* for EP0 control message */
  70};
  71
  72struct keyspan_port_private {
  73        /* Keep track of which input & output endpoints to use */
  74        int             in_flip;
  75        int             out_flip;
  76
  77        /* Keep duplicate of device details in each port
  78           structure as well - simplifies some of the
  79           callback functions etc. */
  80        const struct keyspan_device_details     *device_details;
  81
  82        /* Input endpoints and buffer for this port */
  83        struct urb      *in_urbs[2];
  84        char            in_buffer[2][64];
  85        /* Output endpoints and buffer for this port */
  86        struct urb      *out_urbs[2];
  87        char            out_buffer[2][64];
  88
  89        /* Input ack endpoint */
  90        struct urb      *inack_urb;
  91        char            inack_buffer[1];
  92
  93        /* Output control endpoint */
  94        struct urb      *outcont_urb;
  95        char            outcont_buffer[64];
  96
  97        /* Settings for the port */
  98        int             baud;
  99        int             old_baud;
 100        unsigned int    cflag;
 101        unsigned int    old_cflag;
 102        enum            {flow_none, flow_cts, flow_xon} flow_control;
 103        int             rts_state;      /* Handshaking pins (outputs) */
 104        int             dtr_state;
 105        int             cts_state;      /* Handshaking pins (inputs) */
 106        int             dsr_state;
 107        int             dcd_state;
 108        int             ri_state;
 109        int             break_on;
 110
 111        unsigned long   tx_start_time[2];
 112        int             resend_cont;    /* need to resend control packet */
 113};
 114
 115/* Include Keyspan message headers.  All current Keyspan Adapters
 116   make use of one of five message formats which are referred
 117   to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
 118   within this driver. */
 119#include "keyspan_usa26msg.h"
 120#include "keyspan_usa28msg.h"
 121#include "keyspan_usa49msg.h"
 122#include "keyspan_usa90msg.h"
 123#include "keyspan_usa67msg.h"
 124
 125
 126module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
 127
 128static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
 129{
 130        struct usb_serial_port *port = tty->driver_data;
 131        struct keyspan_port_private     *p_priv;
 132
 133        p_priv = usb_get_serial_port_data(port);
 134
 135        if (break_state == -1)
 136                p_priv->break_on = 1;
 137        else
 138                p_priv->break_on = 0;
 139
 140        keyspan_send_setup(port, 0);
 141}
 142
 143
 144static void keyspan_set_termios(struct tty_struct *tty,
 145                struct usb_serial_port *port, struct ktermios *old_termios)
 146{
 147        int                             baud_rate, device_port;
 148        struct keyspan_port_private     *p_priv;
 149        const struct keyspan_device_details     *d_details;
 150        unsigned int                    cflag;
 151
 152        p_priv = usb_get_serial_port_data(port);
 153        d_details = p_priv->device_details;
 154        cflag = tty->termios.c_cflag;
 155        device_port = port->number - port->serial->minor;
 156
 157        /* Baud rate calculation takes baud rate as an integer
 158           so other rates can be generated if desired. */
 159        baud_rate = tty_get_baud_rate(tty);
 160        /* If no match or invalid, don't change */
 161        if (d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
 162                                NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
 163                /* FIXME - more to do here to ensure rate changes cleanly */
 164                /* FIXME - calcuate exact rate from divisor ? */
 165                p_priv->baud = baud_rate;
 166        } else
 167                baud_rate = tty_termios_baud_rate(old_termios);
 168
 169        tty_encode_baud_rate(tty, baud_rate, baud_rate);
 170        /* set CTS/RTS handshake etc. */
 171        p_priv->cflag = cflag;
 172        p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
 173
 174        /* Mark/Space not supported */
 175        tty->termios.c_cflag &= ~CMSPAR;
 176
 177        keyspan_send_setup(port, 0);
 178}
 179
 180static int keyspan_tiocmget(struct tty_struct *tty)
 181{
 182        struct usb_serial_port *port = tty->driver_data;
 183        struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
 184        unsigned int                    value;
 185
 186        value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
 187                ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
 188                ((p_priv->cts_state) ? TIOCM_CTS : 0) |
 189                ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
 190                ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
 191                ((p_priv->ri_state) ? TIOCM_RNG : 0);
 192
 193        return value;
 194}
 195
 196static int keyspan_tiocmset(struct tty_struct *tty,
 197                            unsigned int set, unsigned int clear)
 198{
 199        struct usb_serial_port *port = tty->driver_data;
 200        struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
 201
 202        if (set & TIOCM_RTS)
 203                p_priv->rts_state = 1;
 204        if (set & TIOCM_DTR)
 205                p_priv->dtr_state = 1;
 206        if (clear & TIOCM_RTS)
 207                p_priv->rts_state = 0;
 208        if (clear & TIOCM_DTR)
 209                p_priv->dtr_state = 0;
 210        keyspan_send_setup(port, 0);
 211        return 0;
 212}
 213
 214/* Write function is similar for the four protocols used
 215   with only a minor change for usa90 (usa19hs) required */
 216static int keyspan_write(struct tty_struct *tty,
 217        struct usb_serial_port *port, const unsigned char *buf, int count)
 218{
 219        struct keyspan_port_private     *p_priv;
 220        const struct keyspan_device_details     *d_details;
 221        int                             flip;
 222        int                             left, todo;
 223        struct urb                      *this_urb;
 224        int                             err, maxDataLen, dataOffset;
 225
 226        p_priv = usb_get_serial_port_data(port);
 227        d_details = p_priv->device_details;
 228
 229        if (d_details->msg_format == msg_usa90) {
 230                maxDataLen = 64;
 231                dataOffset = 0;
 232        } else {
 233                maxDataLen = 63;
 234                dataOffset = 1;
 235        }
 236
 237        dev_dbg(&port->dev, "%s - for port %d (%d chars), flip=%d\n",
 238                __func__, port->number, count, p_priv->out_flip);
 239
 240        for (left = count; left > 0; left -= todo) {
 241                todo = left;
 242                if (todo > maxDataLen)
 243                        todo = maxDataLen;
 244
 245                flip = p_priv->out_flip;
 246
 247                /* Check we have a valid urb/endpoint before we use it... */
 248                this_urb = p_priv->out_urbs[flip];
 249                if (this_urb == NULL) {
 250                        /* no bulk out, so return 0 bytes written */
 251                        dev_dbg(&port->dev, "%s - no output urb :(\n", __func__);
 252                        return count;
 253                }
 254
 255                dev_dbg(&port->dev, "%s - endpoint %d flip %d\n",
 256                        __func__, usb_pipeendpoint(this_urb->pipe), flip);
 257
 258                if (this_urb->status == -EINPROGRESS) {
 259                        if (time_before(jiffies,
 260                                        p_priv->tx_start_time[flip] + 10 * HZ))
 261                                break;
 262                        usb_unlink_urb(this_urb);
 263                        break;
 264                }
 265
 266                /* First byte in buffer is "last flag" (except for usa19hx)
 267                   - unused so for now so set to zero */
 268                ((char *)this_urb->transfer_buffer)[0] = 0;
 269
 270                memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
 271                buf += todo;
 272
 273                /* send the data out the bulk port */
 274                this_urb->transfer_buffer_length = todo + dataOffset;
 275
 276                err = usb_submit_urb(this_urb, GFP_ATOMIC);
 277                if (err != 0)
 278                        dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed (%d)\n", err);
 279                p_priv->tx_start_time[flip] = jiffies;
 280
 281                /* Flip for next time if usa26 or usa28 interface
 282                   (not used on usa49) */
 283                p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
 284        }
 285
 286        return count - left;
 287}
 288
 289static void     usa26_indat_callback(struct urb *urb)
 290{
 291        int                     i, err;
 292        int                     endpoint;
 293        struct usb_serial_port  *port;
 294        unsigned char           *data = urb->transfer_buffer;
 295        int status = urb->status;
 296
 297        endpoint = usb_pipeendpoint(urb->pipe);
 298
 299        if (status) {
 300                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
 301                        __func__, status, endpoint);
 302                return;
 303        }
 304
 305        port =  urb->context;
 306        if (urb->actual_length) {
 307                /* 0x80 bit is error flag */
 308                if ((data[0] & 0x80) == 0) {
 309                        /* no errors on individual bytes, only
 310                           possible overrun err */
 311                        if (data[0] & RXERROR_OVERRUN)
 312                                err = TTY_OVERRUN;
 313                        else
 314                                err = 0;
 315                        for (i = 1; i < urb->actual_length ; ++i)
 316                                tty_insert_flip_char(&port->port, data[i], err);
 317                } else {
 318                        /* some bytes had errors, every byte has status */
 319                        dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
 320                        for (i = 0; i + 1 < urb->actual_length; i += 2) {
 321                                int stat = data[i], flag = 0;
 322                                if (stat & RXERROR_OVERRUN)
 323                                        flag |= TTY_OVERRUN;
 324                                if (stat & RXERROR_FRAMING)
 325                                        flag |= TTY_FRAME;
 326                                if (stat & RXERROR_PARITY)
 327                                        flag |= TTY_PARITY;
 328                                /* XXX should handle break (0x10) */
 329                                tty_insert_flip_char(&port->port, data[i+1],
 330                                                flag);
 331                        }
 332                }
 333                tty_flip_buffer_push(&port->port);
 334        }
 335
 336        /* Resubmit urb so we continue receiving */
 337        err = usb_submit_urb(urb, GFP_ATOMIC);
 338        if (err != 0)
 339                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 340}
 341
 342/* Outdat handling is common for all devices */
 343static void     usa2x_outdat_callback(struct urb *urb)
 344{
 345        struct usb_serial_port *port;
 346        struct keyspan_port_private *p_priv;
 347
 348        port =  urb->context;
 349        p_priv = usb_get_serial_port_data(port);
 350        dev_dbg(&port->dev, "%s - urb %d\n", __func__, urb == p_priv->out_urbs[1]);
 351
 352        usb_serial_port_softint(port);
 353}
 354
 355static void     usa26_inack_callback(struct urb *urb)
 356{
 357}
 358
 359static void     usa26_outcont_callback(struct urb *urb)
 360{
 361        struct usb_serial_port *port;
 362        struct keyspan_port_private *p_priv;
 363
 364        port =  urb->context;
 365        p_priv = usb_get_serial_port_data(port);
 366
 367        if (p_priv->resend_cont) {
 368                dev_dbg(&port->dev, "%s - sending setup\n", __func__);
 369                keyspan_usa26_send_setup(port->serial, port,
 370                                                p_priv->resend_cont - 1);
 371        }
 372}
 373
 374static void     usa26_instat_callback(struct urb *urb)
 375{
 376        unsigned char                           *data = urb->transfer_buffer;
 377        struct keyspan_usa26_portStatusMessage  *msg;
 378        struct usb_serial                       *serial;
 379        struct usb_serial_port                  *port;
 380        struct keyspan_port_private             *p_priv;
 381        int old_dcd_state, err;
 382        int status = urb->status;
 383
 384        serial =  urb->context;
 385
 386        if (status) {
 387                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
 388                return;
 389        }
 390        if (urb->actual_length != 9) {
 391                dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
 392                goto exit;
 393        }
 394
 395        msg = (struct keyspan_usa26_portStatusMessage *)data;
 396
 397#if 0
 398        dev_dbg(&urb->dev->dev,
 399                "%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
 400                __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr,
 401                msg->ri, msg->_txOff, msg->_txXoff, msg->rxEnabled,
 402                msg->controlResponse);
 403#endif
 404
 405        /* Now do something useful with the data */
 406
 407
 408        /* Check port number from message and retrieve private data */
 409        if (msg->port >= serial->num_ports) {
 410                dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
 411                goto exit;
 412        }
 413        port = serial->port[msg->port];
 414        p_priv = usb_get_serial_port_data(port);
 415
 416        /* Update handshaking pin state information */
 417        old_dcd_state = p_priv->dcd_state;
 418        p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
 419        p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
 420        p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
 421        p_priv->ri_state = ((msg->ri) ? 1 : 0);
 422
 423        if (old_dcd_state != p_priv->dcd_state)
 424                tty_port_tty_hangup(&port->port, true);
 425
 426        /* Resubmit urb so we continue receiving */
 427        err = usb_submit_urb(urb, GFP_ATOMIC);
 428        if (err != 0)
 429                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 430exit: ;
 431}
 432
 433static void     usa26_glocont_callback(struct urb *urb)
 434{
 435}
 436
 437
 438static void usa28_indat_callback(struct urb *urb)
 439{
 440        int                     err;
 441        struct usb_serial_port  *port;
 442        unsigned char           *data;
 443        struct keyspan_port_private             *p_priv;
 444        int status = urb->status;
 445
 446        port =  urb->context;
 447        p_priv = usb_get_serial_port_data(port);
 448        data = urb->transfer_buffer;
 449
 450        if (urb != p_priv->in_urbs[p_priv->in_flip])
 451                return;
 452
 453        do {
 454                if (status) {
 455                        dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
 456                                __func__, status, usb_pipeendpoint(urb->pipe));
 457                        return;
 458                }
 459
 460                port =  urb->context;
 461                p_priv = usb_get_serial_port_data(port);
 462                data = urb->transfer_buffer;
 463
 464                if (urb->actual_length) {
 465                        tty_insert_flip_string(&port->port, data,
 466                                        urb->actual_length);
 467                        tty_flip_buffer_push(&port->port);
 468                }
 469
 470                /* Resubmit urb so we continue receiving */
 471                err = usb_submit_urb(urb, GFP_ATOMIC);
 472                if (err != 0)
 473                        dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n",
 474                                                        __func__, err);
 475                p_priv->in_flip ^= 1;
 476
 477                urb = p_priv->in_urbs[p_priv->in_flip];
 478        } while (urb->status != -EINPROGRESS);
 479}
 480
 481static void     usa28_inack_callback(struct urb *urb)
 482{
 483}
 484
 485static void     usa28_outcont_callback(struct urb *urb)
 486{
 487        struct usb_serial_port *port;
 488        struct keyspan_port_private *p_priv;
 489
 490        port =  urb->context;
 491        p_priv = usb_get_serial_port_data(port);
 492
 493        if (p_priv->resend_cont) {
 494                dev_dbg(&port->dev, "%s - sending setup\n", __func__);
 495                keyspan_usa28_send_setup(port->serial, port,
 496                                                p_priv->resend_cont - 1);
 497        }
 498}
 499
 500static void     usa28_instat_callback(struct urb *urb)
 501{
 502        int                                     err;
 503        unsigned char                           *data = urb->transfer_buffer;
 504        struct keyspan_usa28_portStatusMessage  *msg;
 505        struct usb_serial                       *serial;
 506        struct usb_serial_port                  *port;
 507        struct keyspan_port_private             *p_priv;
 508        int old_dcd_state;
 509        int status = urb->status;
 510
 511        serial =  urb->context;
 512
 513        if (status) {
 514                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
 515                return;
 516        }
 517
 518        if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
 519                dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
 520                goto exit;
 521        }
 522
 523        /*
 524        dev_dbg(&urb->dev->dev,
 525                "%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__,
 526                data[0], data[1], data[2], data[3], data[4], data[5],
 527                data[6], data[7], data[8], data[9], data[10], data[11]);
 528        */
 529
 530        /* Now do something useful with the data */
 531        msg = (struct keyspan_usa28_portStatusMessage *)data;
 532
 533        /* Check port number from message and retrieve private data */
 534        if (msg->port >= serial->num_ports) {
 535                dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
 536                goto exit;
 537        }
 538        port = serial->port[msg->port];
 539        p_priv = usb_get_serial_port_data(port);
 540
 541        /* Update handshaking pin state information */
 542        old_dcd_state = p_priv->dcd_state;
 543        p_priv->cts_state = ((msg->cts) ? 1 : 0);
 544        p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
 545        p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
 546        p_priv->ri_state = ((msg->ri) ? 1 : 0);
 547
 548        if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
 549                tty_port_tty_hangup(&port->port, true);
 550
 551                /* Resubmit urb so we continue receiving */
 552        err = usb_submit_urb(urb, GFP_ATOMIC);
 553        if (err != 0)
 554                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 555exit: ;
 556}
 557
 558static void     usa28_glocont_callback(struct urb *urb)
 559{
 560}
 561
 562
 563static void     usa49_glocont_callback(struct urb *urb)
 564{
 565        struct usb_serial *serial;
 566        struct usb_serial_port *port;
 567        struct keyspan_port_private *p_priv;
 568        int i;
 569
 570        serial =  urb->context;
 571        for (i = 0; i < serial->num_ports; ++i) {
 572                port = serial->port[i];
 573                p_priv = usb_get_serial_port_data(port);
 574
 575                if (p_priv->resend_cont) {
 576                        dev_dbg(&port->dev, "%s - sending setup\n", __func__);
 577                        keyspan_usa49_send_setup(serial, port,
 578                                                p_priv->resend_cont - 1);
 579                        break;
 580                }
 581        }
 582}
 583
 584        /* This is actually called glostat in the Keyspan
 585           doco */
 586static void     usa49_instat_callback(struct urb *urb)
 587{
 588        int                                     err;
 589        unsigned char                           *data = urb->transfer_buffer;
 590        struct keyspan_usa49_portStatusMessage  *msg;
 591        struct usb_serial                       *serial;
 592        struct usb_serial_port                  *port;
 593        struct keyspan_port_private             *p_priv;
 594        int old_dcd_state;
 595        int status = urb->status;
 596
 597        serial =  urb->context;
 598
 599        if (status) {
 600                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
 601                return;
 602        }
 603
 604        if (urb->actual_length !=
 605                        sizeof(struct keyspan_usa49_portStatusMessage)) {
 606                dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
 607                goto exit;
 608        }
 609
 610        /*
 611        dev_dbg(&urb->dev->dev, "%s: %x %x %x %x %x %x %x %x %x %x %x",
 612                __func__, data[0], data[1], data[2], data[3], data[4],
 613                data[5], data[6], data[7], data[8], data[9], data[10]);
 614        */
 615
 616        /* Now do something useful with the data */
 617        msg = (struct keyspan_usa49_portStatusMessage *)data;
 618
 619        /* Check port number from message and retrieve private data */
 620        if (msg->portNumber >= serial->num_ports) {
 621                dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
 622                        __func__, msg->portNumber);
 623                goto exit;
 624        }
 625        port = serial->port[msg->portNumber];
 626        p_priv = usb_get_serial_port_data(port);
 627
 628        /* Update handshaking pin state information */
 629        old_dcd_state = p_priv->dcd_state;
 630        p_priv->cts_state = ((msg->cts) ? 1 : 0);
 631        p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
 632        p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
 633        p_priv->ri_state = ((msg->ri) ? 1 : 0);
 634
 635        if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
 636                tty_port_tty_hangup(&port->port, true);
 637
 638        /* Resubmit urb so we continue receiving */
 639        err = usb_submit_urb(urb, GFP_ATOMIC);
 640        if (err != 0)
 641                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 642exit:   ;
 643}
 644
 645static void     usa49_inack_callback(struct urb *urb)
 646{
 647}
 648
 649static void     usa49_indat_callback(struct urb *urb)
 650{
 651        int                     i, err;
 652        int                     endpoint;
 653        struct usb_serial_port  *port;
 654        unsigned char           *data = urb->transfer_buffer;
 655        int status = urb->status;
 656
 657        endpoint = usb_pipeendpoint(urb->pipe);
 658
 659        if (status) {
 660                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
 661                        __func__, status, endpoint);
 662                return;
 663        }
 664
 665        port =  urb->context;
 666        if (urb->actual_length) {
 667                /* 0x80 bit is error flag */
 668                if ((data[0] & 0x80) == 0) {
 669                        /* no error on any byte */
 670                        tty_insert_flip_string(&port->port, data + 1,
 671                                                urb->actual_length - 1);
 672                } else {
 673                        /* some bytes had errors, every byte has status */
 674                        for (i = 0; i + 1 < urb->actual_length; i += 2) {
 675                                int stat = data[i], flag = 0;
 676                                if (stat & RXERROR_OVERRUN)
 677                                        flag |= TTY_OVERRUN;
 678                                if (stat & RXERROR_FRAMING)
 679                                        flag |= TTY_FRAME;
 680                                if (stat & RXERROR_PARITY)
 681                                        flag |= TTY_PARITY;
 682                                /* XXX should handle break (0x10) */
 683                                tty_insert_flip_char(&port->port, data[i+1],
 684                                                flag);
 685                        }
 686                }
 687                tty_flip_buffer_push(&port->port);
 688        }
 689
 690        /* Resubmit urb so we continue receiving */
 691        err = usb_submit_urb(urb, GFP_ATOMIC);
 692        if (err != 0)
 693                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 694}
 695
 696static void usa49wg_indat_callback(struct urb *urb)
 697{
 698        int                     i, len, x, err;
 699        struct usb_serial       *serial;
 700        struct usb_serial_port  *port;
 701        unsigned char           *data = urb->transfer_buffer;
 702        int status = urb->status;
 703
 704        serial = urb->context;
 705
 706        if (status) {
 707                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
 708                return;
 709        }
 710
 711        /* inbound data is in the form P#, len, status, data */
 712        i = 0;
 713        len = 0;
 714
 715        while (i < urb->actual_length) {
 716
 717                /* Check port number from message */
 718                if (data[i] >= serial->num_ports) {
 719                        dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
 720                                __func__, data[i]);
 721                        return;
 722                }
 723                port = serial->port[data[i++]];
 724                len = data[i++];
 725
 726                /* 0x80 bit is error flag */
 727                if ((data[i] & 0x80) == 0) {
 728                        /* no error on any byte */
 729                        i++;
 730                        for (x = 1; x < len && i < urb->actual_length; ++x)
 731                                tty_insert_flip_char(&port->port,
 732                                                data[i++], 0);
 733                } else {
 734                        /*
 735                         * some bytes had errors, every byte has status
 736                         */
 737                        for (x = 0; x + 1 < len &&
 738                                    i + 1 < urb->actual_length; x += 2) {
 739                                int stat = data[i], flag = 0;
 740
 741                                if (stat & RXERROR_OVERRUN)
 742                                        flag |= TTY_OVERRUN;
 743                                if (stat & RXERROR_FRAMING)
 744                                        flag |= TTY_FRAME;
 745                                if (stat & RXERROR_PARITY)
 746                                        flag |= TTY_PARITY;
 747                                /* XXX should handle break (0x10) */
 748                                tty_insert_flip_char(&port->port, data[i+1],
 749                                                     flag);
 750                                i += 2;
 751                        }
 752                }
 753                tty_flip_buffer_push(&port->port);
 754        }
 755
 756        /* Resubmit urb so we continue receiving */
 757        err = usb_submit_urb(urb, GFP_ATOMIC);
 758        if (err != 0)
 759                dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 760}
 761
 762/* not used, usa-49 doesn't have per-port control endpoints */
 763static void usa49_outcont_callback(struct urb *urb)
 764{
 765}
 766
 767static void usa90_indat_callback(struct urb *urb)
 768{
 769        int                     i, err;
 770        int                     endpoint;
 771        struct usb_serial_port  *port;
 772        struct keyspan_port_private             *p_priv;
 773        unsigned char           *data = urb->transfer_buffer;
 774        int status = urb->status;
 775
 776        endpoint = usb_pipeendpoint(urb->pipe);
 777
 778        if (status) {
 779                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
 780                    __func__, status, endpoint);
 781                return;
 782        }
 783
 784        port =  urb->context;
 785        p_priv = usb_get_serial_port_data(port);
 786
 787        if (urb->actual_length) {
 788                /* if current mode is DMA, looks like usa28 format
 789                   otherwise looks like usa26 data format */
 790
 791                if (p_priv->baud > 57600)
 792                        tty_insert_flip_string(&port->port, data,
 793                                        urb->actual_length);
 794                else {
 795                        /* 0x80 bit is error flag */
 796                        if ((data[0] & 0x80) == 0) {
 797                                /* no errors on individual bytes, only
 798                                   possible overrun err*/
 799                                if (data[0] & RXERROR_OVERRUN)
 800                                        err = TTY_OVERRUN;
 801                                else
 802                                        err = 0;
 803                                for (i = 1; i < urb->actual_length ; ++i)
 804                                        tty_insert_flip_char(&port->port,
 805                                                        data[i], err);
 806                        }  else {
 807                        /* some bytes had errors, every byte has status */
 808                                dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
 809                                for (i = 0; i + 1 < urb->actual_length; i += 2) {
 810                                        int stat = data[i], flag = 0;
 811                                        if (stat & RXERROR_OVERRUN)
 812                                                flag |= TTY_OVERRUN;
 813                                        if (stat & RXERROR_FRAMING)
 814                                                flag |= TTY_FRAME;
 815                                        if (stat & RXERROR_PARITY)
 816                                                flag |= TTY_PARITY;
 817                                        /* XXX should handle break (0x10) */
 818                                        tty_insert_flip_char(&port->port,
 819                                                        data[i+1], flag);
 820                                }
 821                        }
 822                }
 823                tty_flip_buffer_push(&port->port);
 824        }
 825
 826        /* Resubmit urb so we continue receiving */
 827        err = usb_submit_urb(urb, GFP_ATOMIC);
 828        if (err != 0)
 829                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 830}
 831
 832
 833static void     usa90_instat_callback(struct urb *urb)
 834{
 835        unsigned char                           *data = urb->transfer_buffer;
 836        struct keyspan_usa90_portStatusMessage  *msg;
 837        struct usb_serial                       *serial;
 838        struct usb_serial_port                  *port;
 839        struct keyspan_port_private             *p_priv;
 840        int old_dcd_state, err;
 841        int status = urb->status;
 842
 843        serial =  urb->context;
 844
 845        if (status) {
 846                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
 847                return;
 848        }
 849        if (urb->actual_length < 14) {
 850                dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
 851                goto exit;
 852        }
 853
 854        msg = (struct keyspan_usa90_portStatusMessage *)data;
 855
 856        /* Now do something useful with the data */
 857
 858        port = serial->port[0];
 859        p_priv = usb_get_serial_port_data(port);
 860
 861        /* Update handshaking pin state information */
 862        old_dcd_state = p_priv->dcd_state;
 863        p_priv->cts_state = ((msg->cts) ? 1 : 0);
 864        p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
 865        p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
 866        p_priv->ri_state = ((msg->ri) ? 1 : 0);
 867
 868        if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
 869                tty_port_tty_hangup(&port->port, true);
 870
 871        /* Resubmit urb so we continue receiving */
 872        err = usb_submit_urb(urb, GFP_ATOMIC);
 873        if (err != 0)
 874                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 875exit:
 876        ;
 877}
 878
 879static void     usa90_outcont_callback(struct urb *urb)
 880{
 881        struct usb_serial_port *port;
 882        struct keyspan_port_private *p_priv;
 883
 884        port =  urb->context;
 885        p_priv = usb_get_serial_port_data(port);
 886
 887        if (p_priv->resend_cont) {
 888                dev_dbg(&urb->dev->dev, "%s - sending setup\n", __func__);
 889                keyspan_usa90_send_setup(port->serial, port,
 890                                                p_priv->resend_cont - 1);
 891        }
 892}
 893
 894/* Status messages from the 28xg */
 895static void     usa67_instat_callback(struct urb *urb)
 896{
 897        int                                     err;
 898        unsigned char                           *data = urb->transfer_buffer;
 899        struct keyspan_usa67_portStatusMessage  *msg;
 900        struct usb_serial                       *serial;
 901        struct usb_serial_port                  *port;
 902        struct keyspan_port_private             *p_priv;
 903        int old_dcd_state;
 904        int status = urb->status;
 905
 906        serial = urb->context;
 907
 908        if (status) {
 909                dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
 910                return;
 911        }
 912
 913        if (urb->actual_length !=
 914                        sizeof(struct keyspan_usa67_portStatusMessage)) {
 915                dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
 916                return;
 917        }
 918
 919
 920        /* Now do something useful with the data */
 921        msg = (struct keyspan_usa67_portStatusMessage *)data;
 922
 923        /* Check port number from message and retrieve private data */
 924        if (msg->port >= serial->num_ports) {
 925                dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
 926                return;
 927        }
 928
 929        port = serial->port[msg->port];
 930        p_priv = usb_get_serial_port_data(port);
 931
 932        /* Update handshaking pin state information */
 933        old_dcd_state = p_priv->dcd_state;
 934        p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
 935        p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
 936
 937        if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
 938                tty_port_tty_hangup(&port->port, true);
 939
 940        /* Resubmit urb so we continue receiving */
 941        err = usb_submit_urb(urb, GFP_ATOMIC);
 942        if (err != 0)
 943                dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
 944}
 945
 946static void usa67_glocont_callback(struct urb *urb)
 947{
 948        struct usb_serial *serial;
 949        struct usb_serial_port *port;
 950        struct keyspan_port_private *p_priv;
 951        int i;
 952
 953        serial = urb->context;
 954        for (i = 0; i < serial->num_ports; ++i) {
 955                port = serial->port[i];
 956                p_priv = usb_get_serial_port_data(port);
 957
 958                if (p_priv->resend_cont) {
 959                        dev_dbg(&port->dev, "%s - sending setup\n", __func__);
 960                        keyspan_usa67_send_setup(serial, port,
 961                                                p_priv->resend_cont - 1);
 962                        break;
 963                }
 964        }
 965}
 966
 967static int keyspan_write_room(struct tty_struct *tty)
 968{
 969        struct usb_serial_port *port = tty->driver_data;
 970        struct keyspan_port_private     *p_priv;
 971        const struct keyspan_device_details     *d_details;
 972        int                             flip;
 973        int                             data_len;
 974        struct urb                      *this_urb;
 975
 976        p_priv = usb_get_serial_port_data(port);
 977        d_details = p_priv->device_details;
 978
 979        /* FIXME: locking */
 980        if (d_details->msg_format == msg_usa90)
 981                data_len = 64;
 982        else
 983                data_len = 63;
 984
 985        flip = p_priv->out_flip;
 986
 987        /* Check both endpoints to see if any are available. */
 988        this_urb = p_priv->out_urbs[flip];
 989        if (this_urb != NULL) {
 990                if (this_urb->status != -EINPROGRESS)
 991                        return data_len;
 992                flip = (flip + 1) & d_details->outdat_endp_flip;
 993                this_urb = p_priv->out_urbs[flip];
 994                if (this_urb != NULL) {
 995                        if (this_urb->status != -EINPROGRESS)
 996                                return data_len;
 997                }
 998        }
 999        return 0;
1000}
1001
1002
1003static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
1004{
1005        struct keyspan_port_private     *p_priv;
1006        const struct keyspan_device_details     *d_details;
1007        int                             i, err;
1008        int                             baud_rate, device_port;
1009        struct urb                      *urb;
1010        unsigned int                    cflag = 0;
1011
1012        p_priv = usb_get_serial_port_data(port);
1013        d_details = p_priv->device_details;
1014
1015        /* Set some sane defaults */
1016        p_priv->rts_state = 1;
1017        p_priv->dtr_state = 1;
1018        p_priv->baud = 9600;
1019
1020        /* force baud and lcr to be set on open */
1021        p_priv->old_baud = 0;
1022        p_priv->old_cflag = 0;
1023
1024        p_priv->out_flip = 0;
1025        p_priv->in_flip = 0;
1026
1027        /* Reset low level data toggle and start reading from endpoints */
1028        for (i = 0; i < 2; i++) {
1029                urb = p_priv->in_urbs[i];
1030                if (urb == NULL)
1031                        continue;
1032
1033                /* make sure endpoint data toggle is synchronized
1034                   with the device */
1035                usb_clear_halt(urb->dev, urb->pipe);
1036                err = usb_submit_urb(urb, GFP_KERNEL);
1037                if (err != 0)
1038                        dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
1039        }
1040
1041        /* Reset low level data toggle on out endpoints */
1042        for (i = 0; i < 2; i++) {
1043                urb = p_priv->out_urbs[i];
1044                if (urb == NULL)
1045                        continue;
1046                /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1047                                                usb_pipeout(urb->pipe), 0); */
1048        }
1049
1050        /* get the terminal config for the setup message now so we don't
1051         * need to send 2 of them */
1052
1053        device_port = port->number - port->serial->minor;
1054        if (tty) {
1055                cflag = tty->termios.c_cflag;
1056                /* Baud rate calculation takes baud rate as an integer
1057                   so other rates can be generated if desired. */
1058                baud_rate = tty_get_baud_rate(tty);
1059                /* If no match or invalid, leave as default */
1060                if (baud_rate >= 0
1061                    && d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
1062                                        NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1063                        p_priv->baud = baud_rate;
1064                }
1065        }
1066        /* set CTS/RTS handshake etc. */
1067        p_priv->cflag = cflag;
1068        p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
1069
1070        keyspan_send_setup(port, 1);
1071        /* mdelay(100); */
1072        /* keyspan_set_termios(port, NULL); */
1073
1074        return 0;
1075}
1076
1077static inline void stop_urb(struct urb *urb)
1078{
1079        if (urb && urb->status == -EINPROGRESS)
1080                usb_kill_urb(urb);
1081}
1082
1083static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1084{
1085        struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1086
1087        p_priv->rts_state = on;
1088        p_priv->dtr_state = on;
1089        keyspan_send_setup(port, 0);
1090}
1091
1092static void keyspan_close(struct usb_serial_port *port)
1093{
1094        int                     i;
1095        struct keyspan_port_private     *p_priv;
1096
1097        p_priv = usb_get_serial_port_data(port);
1098
1099        p_priv->rts_state = 0;
1100        p_priv->dtr_state = 0;
1101
1102        keyspan_send_setup(port, 2);
1103        /* pilot-xfer seems to work best with this delay */
1104        mdelay(100);
1105
1106        p_priv->out_flip = 0;
1107        p_priv->in_flip = 0;
1108
1109        stop_urb(p_priv->inack_urb);
1110        for (i = 0; i < 2; i++) {
1111                stop_urb(p_priv->in_urbs[i]);
1112                stop_urb(p_priv->out_urbs[i]);
1113        }
1114}
1115
1116/* download the firmware to a pre-renumeration device */
1117static int keyspan_fake_startup(struct usb_serial *serial)
1118{
1119        char    *fw_name;
1120
1121        dev_dbg(&serial->dev->dev, "Keyspan startup version %04x product %04x\n",
1122                le16_to_cpu(serial->dev->descriptor.bcdDevice),
1123                le16_to_cpu(serial->dev->descriptor.idProduct));
1124
1125        if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1126                                                                != 0x8000) {
1127                dev_dbg(&serial->dev->dev, "Firmware already loaded.  Quitting.\n");
1128                return 1;
1129        }
1130
1131                /* Select firmware image on the basis of idProduct */
1132        switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1133        case keyspan_usa28_pre_product_id:
1134                fw_name = "keyspan/usa28.fw";
1135                break;
1136
1137        case keyspan_usa28x_pre_product_id:
1138                fw_name = "keyspan/usa28x.fw";
1139                break;
1140
1141        case keyspan_usa28xa_pre_product_id:
1142                fw_name = "keyspan/usa28xa.fw";
1143                break;
1144
1145        case keyspan_usa28xb_pre_product_id:
1146                fw_name = "keyspan/usa28xb.fw";
1147                break;
1148
1149        case keyspan_usa19_pre_product_id:
1150                fw_name = "keyspan/usa19.fw";
1151                break;
1152
1153        case keyspan_usa19qi_pre_product_id:
1154                fw_name = "keyspan/usa19qi.fw";
1155                break;
1156
1157        case keyspan_mpr_pre_product_id:
1158                fw_name = "keyspan/mpr.fw";
1159                break;
1160
1161        case keyspan_usa19qw_pre_product_id:
1162                fw_name = "keyspan/usa19qw.fw";
1163                break;
1164
1165        case keyspan_usa18x_pre_product_id:
1166                fw_name = "keyspan/usa18x.fw";
1167                break;
1168
1169        case keyspan_usa19w_pre_product_id:
1170                fw_name = "keyspan/usa19w.fw";
1171                break;
1172
1173        case keyspan_usa49w_pre_product_id:
1174                fw_name = "keyspan/usa49w.fw";
1175                break;
1176
1177        case keyspan_usa49wlc_pre_product_id:
1178                fw_name = "keyspan/usa49wlc.fw";
1179                break;
1180
1181        default:
1182                dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1183                        le16_to_cpu(serial->dev->descriptor.idProduct));
1184                return 1;
1185        }
1186
1187        dev_dbg(&serial->dev->dev, "Uploading Keyspan %s firmware.\n", fw_name);
1188
1189        if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
1190                dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
1191                        fw_name);
1192                return -ENOENT;
1193        }
1194
1195        /* after downloading firmware Renumeration will occur in a
1196          moment and the new device will bind to the real driver */
1197
1198        /* we don't want this device to have a driver assigned to it. */
1199        return 1;
1200}
1201
1202/* Helper functions used by keyspan_setup_urbs */
1203static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1204                                                     int endpoint)
1205{
1206        struct usb_host_interface *iface_desc;
1207        struct usb_endpoint_descriptor *ep;
1208        int i;
1209
1210        iface_desc = serial->interface->cur_altsetting;
1211        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1212                ep = &iface_desc->endpoint[i].desc;
1213                if (ep->bEndpointAddress == endpoint)
1214                        return ep;
1215        }
1216        dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1217                 "endpoint %x\n", endpoint);
1218        return NULL;
1219}
1220
1221static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1222                                      int dir, void *ctx, char *buf, int len,
1223                                      void (*callback)(struct urb *))
1224{
1225        struct urb *urb;
1226        struct usb_endpoint_descriptor const *ep_desc;
1227        char const *ep_type_name;
1228
1229        if (endpoint == -1)
1230                return NULL;            /* endpoint not needed */
1231
1232        dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d.\n", __func__, endpoint);
1233        urb = usb_alloc_urb(0, GFP_KERNEL);             /* No ISO */
1234        if (urb == NULL) {
1235                dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d failed.\n", __func__, endpoint);
1236                return NULL;
1237        }
1238
1239        if (endpoint == 0) {
1240                /* control EP filled in when used */
1241                return urb;
1242        }
1243
1244        ep_desc = find_ep(serial, endpoint);
1245        if (!ep_desc) {
1246                /* leak the urb, something's wrong and the callers don't care */
1247                return urb;
1248        }
1249        if (usb_endpoint_xfer_int(ep_desc)) {
1250                ep_type_name = "INT";
1251                usb_fill_int_urb(urb, serial->dev,
1252                                 usb_sndintpipe(serial->dev, endpoint) | dir,
1253                                 buf, len, callback, ctx,
1254                                 ep_desc->bInterval);
1255        } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1256                ep_type_name = "BULK";
1257                usb_fill_bulk_urb(urb, serial->dev,
1258                                  usb_sndbulkpipe(serial->dev, endpoint) | dir,
1259                                  buf, len, callback, ctx);
1260        } else {
1261                dev_warn(&serial->interface->dev,
1262                         "unsupported endpoint type %x\n",
1263                         usb_endpoint_type(ep_desc));
1264                usb_free_urb(urb);
1265                return NULL;
1266        }
1267
1268        dev_dbg(&serial->interface->dev, "%s - using urb %p for %s endpoint %x\n",
1269            __func__, urb, ep_type_name, endpoint);
1270        return urb;
1271}
1272
1273static struct callbacks {
1274        void    (*instat_callback)(struct urb *);
1275        void    (*glocont_callback)(struct urb *);
1276        void    (*indat_callback)(struct urb *);
1277        void    (*outdat_callback)(struct urb *);
1278        void    (*inack_callback)(struct urb *);
1279        void    (*outcont_callback)(struct urb *);
1280} keyspan_callbacks[] = {
1281        {
1282                /* msg_usa26 callbacks */
1283                .instat_callback =      usa26_instat_callback,
1284                .glocont_callback =     usa26_glocont_callback,
1285                .indat_callback =       usa26_indat_callback,
1286                .outdat_callback =      usa2x_outdat_callback,
1287                .inack_callback =       usa26_inack_callback,
1288                .outcont_callback =     usa26_outcont_callback,
1289        }, {
1290                /* msg_usa28 callbacks */
1291                .instat_callback =      usa28_instat_callback,
1292                .glocont_callback =     usa28_glocont_callback,
1293                .indat_callback =       usa28_indat_callback,
1294                .outdat_callback =      usa2x_outdat_callback,
1295                .inack_callback =       usa28_inack_callback,
1296                .outcont_callback =     usa28_outcont_callback,
1297        }, {
1298                /* msg_usa49 callbacks */
1299                .instat_callback =      usa49_instat_callback,
1300                .glocont_callback =     usa49_glocont_callback,
1301                .indat_callback =       usa49_indat_callback,
1302                .outdat_callback =      usa2x_outdat_callback,
1303                .inack_callback =       usa49_inack_callback,
1304                .outcont_callback =     usa49_outcont_callback,
1305        }, {
1306                /* msg_usa90 callbacks */
1307                .instat_callback =      usa90_instat_callback,
1308                .glocont_callback =     usa28_glocont_callback,
1309                .indat_callback =       usa90_indat_callback,
1310                .outdat_callback =      usa2x_outdat_callback,
1311                .inack_callback =       usa28_inack_callback,
1312                .outcont_callback =     usa90_outcont_callback,
1313        }, {
1314                /* msg_usa67 callbacks */
1315                .instat_callback =      usa67_instat_callback,
1316                .glocont_callback =     usa67_glocont_callback,
1317                .indat_callback =       usa26_indat_callback,
1318                .outdat_callback =      usa2x_outdat_callback,
1319                .inack_callback =       usa26_inack_callback,
1320                .outcont_callback =     usa26_outcont_callback,
1321        }
1322};
1323
1324        /* Generic setup urbs function that uses
1325           data in device_details */
1326static void keyspan_setup_urbs(struct usb_serial *serial)
1327{
1328        struct keyspan_serial_private   *s_priv;
1329        const struct keyspan_device_details     *d_details;
1330        struct callbacks                *cback;
1331
1332        s_priv = usb_get_serial_data(serial);
1333        d_details = s_priv->device_details;
1334
1335        /* Setup values for the various callback routines */
1336        cback = &keyspan_callbacks[d_details->msg_format];
1337
1338        /* Allocate and set up urbs for each one that is in use,
1339           starting with instat endpoints */
1340        s_priv->instat_urb = keyspan_setup_urb
1341                (serial, d_details->instat_endpoint, USB_DIR_IN,
1342                 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1343                 cback->instat_callback);
1344
1345        s_priv->indat_urb = keyspan_setup_urb
1346                (serial, d_details->indat_endpoint, USB_DIR_IN,
1347                 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1348                 usa49wg_indat_callback);
1349
1350        s_priv->glocont_urb = keyspan_setup_urb
1351                (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1352                 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1353                 cback->glocont_callback);
1354}
1355
1356/* usa19 function doesn't require prescaler */
1357static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
1358                                   u32 baud_rate, u32 baudclk, u8 *rate_hi,
1359                                   u8 *rate_low, u8 *prescaler, int portnum)
1360{
1361        u32     b16,    /* baud rate times 16 (actual rate used internally) */
1362                div,    /* divisor */
1363                cnt;    /* inverse of divisor (programmed into 8051) */
1364
1365        dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1366
1367        /* prevent divide by zero...  */
1368        b16 = baud_rate * 16L;
1369        if (b16 == 0)
1370                return KEYSPAN_INVALID_BAUD_RATE;
1371        /* Any "standard" rate over 57k6 is marginal on the USA-19
1372           as we run out of divisor resolution. */
1373        if (baud_rate > 57600)
1374                return KEYSPAN_INVALID_BAUD_RATE;
1375
1376        /* calculate the divisor and the counter (its inverse) */
1377        div = baudclk / b16;
1378        if (div == 0)
1379                return KEYSPAN_INVALID_BAUD_RATE;
1380        else
1381                cnt = 0 - div;
1382
1383        if (div > 0xffff)
1384                return KEYSPAN_INVALID_BAUD_RATE;
1385
1386        /* return the counter values if non-null */
1387        if (rate_low)
1388                *rate_low = (u8) (cnt & 0xff);
1389        if (rate_hi)
1390                *rate_hi = (u8) ((cnt >> 8) & 0xff);
1391        if (rate_low && rate_hi)
1392                dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1393                                __func__, baud_rate, *rate_hi, *rate_low);
1394        return KEYSPAN_BAUD_RATE_OK;
1395}
1396
1397/* usa19hs function doesn't require prescaler */
1398static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
1399                                     u32 baud_rate, u32 baudclk, u8 *rate_hi,
1400                                     u8 *rate_low, u8 *prescaler, int portnum)
1401{
1402        u32     b16,    /* baud rate times 16 (actual rate used internally) */
1403                        div;    /* divisor */
1404
1405        dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1406
1407        /* prevent divide by zero...  */
1408        b16 = baud_rate * 16L;
1409        if (b16 == 0)
1410                return KEYSPAN_INVALID_BAUD_RATE;
1411
1412        /* calculate the divisor */
1413        div = baudclk / b16;
1414        if (div == 0)
1415                return KEYSPAN_INVALID_BAUD_RATE;
1416
1417        if (div > 0xffff)
1418                return KEYSPAN_INVALID_BAUD_RATE;
1419
1420        /* return the counter values if non-null */
1421        if (rate_low)
1422                *rate_low = (u8) (div & 0xff);
1423
1424        if (rate_hi)
1425                *rate_hi = (u8) ((div >> 8) & 0xff);
1426
1427        if (rate_low && rate_hi)
1428                dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1429                        __func__, baud_rate, *rate_hi, *rate_low);
1430
1431        return KEYSPAN_BAUD_RATE_OK;
1432}
1433
1434static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
1435                                    u32 baud_rate, u32 baudclk, u8 *rate_hi,
1436                                    u8 *rate_low, u8 *prescaler, int portnum)
1437{
1438        u32     b16,    /* baud rate times 16 (actual rate used internally) */
1439                clk,    /* clock with 13/8 prescaler */
1440                div,    /* divisor using 13/8 prescaler */
1441                res,    /* resulting baud rate using 13/8 prescaler */
1442                diff,   /* error using 13/8 prescaler */
1443                smallest_diff;
1444        u8      best_prescaler;
1445        int     i;
1446
1447        dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1448
1449        /* prevent divide by zero */
1450        b16 = baud_rate * 16L;
1451        if (b16 == 0)
1452                return KEYSPAN_INVALID_BAUD_RATE;
1453
1454        /* Calculate prescaler by trying them all and looking
1455           for best fit */
1456
1457        /* start with largest possible difference */
1458        smallest_diff = 0xffffffff;
1459
1460                /* 0 is an invalid prescaler, used as a flag */
1461        best_prescaler = 0;
1462
1463        for (i = 8; i <= 0xff; ++i) {
1464                clk = (baudclk * 8) / (u32) i;
1465
1466                div = clk / b16;
1467                if (div == 0)
1468                        continue;
1469
1470                res = clk / div;
1471                diff = (res > b16) ? (res-b16) : (b16-res);
1472
1473                if (diff < smallest_diff) {
1474                        best_prescaler = i;
1475                        smallest_diff = diff;
1476                }
1477        }
1478
1479        if (best_prescaler == 0)
1480                return KEYSPAN_INVALID_BAUD_RATE;
1481
1482        clk = (baudclk * 8) / (u32) best_prescaler;
1483        div = clk / b16;
1484
1485        /* return the divisor and prescaler if non-null */
1486        if (rate_low)
1487                *rate_low = (u8) (div & 0xff);
1488        if (rate_hi)
1489                *rate_hi = (u8) ((div >> 8) & 0xff);
1490        if (prescaler) {
1491                *prescaler = best_prescaler;
1492                /*  dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
1493        }
1494        return KEYSPAN_BAUD_RATE_OK;
1495}
1496
1497        /* USA-28 supports different maximum baud rates on each port */
1498static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
1499                                   u32 baud_rate, u32 baudclk, u8 *rate_hi,
1500                                   u8 *rate_low, u8 *prescaler, int portnum)
1501{
1502        u32     b16,    /* baud rate times 16 (actual rate used internally) */
1503                div,    /* divisor */
1504                cnt;    /* inverse of divisor (programmed into 8051) */
1505
1506        dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1507
1508                /* prevent divide by zero */
1509        b16 = baud_rate * 16L;
1510        if (b16 == 0)
1511                return KEYSPAN_INVALID_BAUD_RATE;
1512
1513        /* calculate the divisor and the counter (its inverse) */
1514        div = KEYSPAN_USA28_BAUDCLK / b16;
1515        if (div == 0)
1516                return KEYSPAN_INVALID_BAUD_RATE;
1517        else
1518                cnt = 0 - div;
1519
1520        /* check for out of range, based on portnum,
1521           and return result */
1522        if (portnum == 0) {
1523                if (div > 0xffff)
1524                        return KEYSPAN_INVALID_BAUD_RATE;
1525        } else {
1526                if (portnum == 1) {
1527                        if (div > 0xff)
1528                                return KEYSPAN_INVALID_BAUD_RATE;
1529                } else
1530                        return KEYSPAN_INVALID_BAUD_RATE;
1531        }
1532
1533                /* return the counter values if not NULL
1534                   (port 1 will ignore retHi) */
1535        if (rate_low)
1536                *rate_low = (u8) (cnt & 0xff);
1537        if (rate_hi)
1538                *rate_hi = (u8) ((cnt >> 8) & 0xff);
1539        dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate);
1540        return KEYSPAN_BAUD_RATE_OK;
1541}
1542
1543static int keyspan_usa26_send_setup(struct usb_serial *serial,
1544                                    struct usb_serial_port *port,
1545                                    int reset_port)
1546{
1547        struct keyspan_usa26_portControlMessage msg;
1548        struct keyspan_serial_private           *s_priv;
1549        struct keyspan_port_private             *p_priv;
1550        const struct keyspan_device_details     *d_details;
1551        struct urb                              *this_urb;
1552        int                                     device_port, err;
1553
1554        dev_dbg(&port->dev, "%s reset=%d\n", __func__, reset_port);
1555
1556        s_priv = usb_get_serial_data(serial);
1557        p_priv = usb_get_serial_port_data(port);
1558        d_details = s_priv->device_details;
1559        device_port = port->number - port->serial->minor;
1560
1561        this_urb = p_priv->outcont_urb;
1562
1563        dev_dbg(&port->dev, "%s - endpoint %d\n", __func__, usb_pipeendpoint(this_urb->pipe));
1564
1565                /* Make sure we have an urb then send the message */
1566        if (this_urb == NULL) {
1567                dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
1568                return -1;
1569        }
1570
1571        /* Save reset port val for resend.
1572           Don't overwrite resend for open/close condition. */
1573        if ((reset_port + 1) > p_priv->resend_cont)
1574                p_priv->resend_cont = reset_port + 1;
1575        if (this_urb->status == -EINPROGRESS) {
1576                /*  dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1577                mdelay(5);
1578                return -1;
1579        }
1580
1581        memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1582
1583        /* Only set baud rate if it's changed */
1584        if (p_priv->old_baud != p_priv->baud) {
1585                p_priv->old_baud = p_priv->baud;
1586                msg.setClocking = 0xff;
1587                if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1588                                                   &msg.baudHi, &msg.baudLo, &msg.prescaler,
1589                                                   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1590                        dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
1591                                __func__, p_priv->baud);
1592                        msg.baudLo = 0;
1593                        msg.baudHi = 125;       /* Values for 9600 baud */
1594                        msg.prescaler = 10;
1595                }
1596                msg.setPrescaler = 0xff;
1597        }
1598
1599        msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
1600        switch (p_priv->cflag & CSIZE) {
1601        case CS5:
1602                msg.lcr |= USA_DATABITS_5;
1603                break;
1604        case CS6:
1605                msg.lcr |= USA_DATABITS_6;
1606                break;
1607        case CS7:
1608                msg.lcr |= USA_DATABITS_7;
1609                break;
1610        case CS8:
1611                msg.lcr |= USA_DATABITS_8;
1612                break;
1613        }
1614        if (p_priv->cflag & PARENB) {
1615                /* note USA_PARITY_NONE == 0 */
1616                msg.lcr |= (p_priv->cflag & PARODD) ?
1617                        USA_PARITY_ODD : USA_PARITY_EVEN;
1618        }
1619        msg.setLcr = 0xff;
1620
1621        msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1622        msg.xonFlowControl = 0;
1623        msg.setFlowControl = 0xff;
1624        msg.forwardingLength = 16;
1625        msg.xonChar = 17;
1626        msg.xoffChar = 19;
1627
1628        /* Opening port */
1629        if (reset_port == 1) {
1630                msg._txOn = 1;
1631                msg._txOff = 0;
1632                msg.txFlush = 0;
1633                msg.txBreak = 0;
1634                msg.rxOn = 1;
1635                msg.rxOff = 0;
1636                msg.rxFlush = 1;
1637                msg.rxForward = 0;
1638                msg.returnStatus = 0;
1639                msg.resetDataToggle = 0xff;
1640        }
1641
1642        /* Closing port */
1643        else if (reset_port == 2) {
1644                msg._txOn = 0;
1645                msg._txOff = 1;
1646                msg.txFlush = 0;
1647                msg.txBreak = 0;
1648                msg.rxOn = 0;
1649                msg.rxOff = 1;
1650                msg.rxFlush = 1;
1651                msg.rxForward = 0;
1652                msg.returnStatus = 0;
1653                msg.resetDataToggle = 0;
1654        }
1655
1656        /* Sending intermediate configs */
1657        else {
1658                msg._txOn = (!p_priv->break_on);
1659                msg._txOff = 0;
1660                msg.txFlush = 0;
1661                msg.txBreak = (p_priv->break_on);
1662                msg.rxOn = 0;
1663                msg.rxOff = 0;
1664                msg.rxFlush = 0;
1665                msg.rxForward = 0;
1666                msg.returnStatus = 0;
1667                msg.resetDataToggle = 0x0;
1668        }
1669
1670        /* Do handshaking outputs */
1671        msg.setTxTriState_setRts = 0xff;
1672        msg.txTriState_rts = p_priv->rts_state;
1673
1674        msg.setHskoa_setDtr = 0xff;
1675        msg.hskoa_dtr = p_priv->dtr_state;
1676
1677        p_priv->resend_cont = 0;
1678        memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1679
1680        /* send the data out the device on control endpoint */
1681        this_urb->transfer_buffer_length = sizeof(msg);
1682
1683        err = usb_submit_urb(this_urb, GFP_ATOMIC);
1684        if (err != 0)
1685                dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
1686        return 0;
1687}
1688
1689static int keyspan_usa28_send_setup(struct usb_serial *serial,
1690                                    struct usb_serial_port *port,
1691                                    int reset_port)
1692{
1693        struct keyspan_usa28_portControlMessage msg;
1694        struct keyspan_serial_private           *s_priv;
1695        struct keyspan_port_private             *p_priv;
1696        const struct keyspan_device_details     *d_details;
1697        struct urb                              *this_urb;
1698        int                                     device_port, err;
1699
1700        s_priv = usb_get_serial_data(serial);
1701        p_priv = usb_get_serial_port_data(port);
1702        d_details = s_priv->device_details;
1703        device_port = port->number - port->serial->minor;
1704
1705        /* only do something if we have a bulk out endpoint */
1706        this_urb = p_priv->outcont_urb;
1707        if (this_urb == NULL) {
1708                dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
1709                return -1;
1710        }
1711
1712        /* Save reset port val for resend.
1713           Don't overwrite resend for open/close condition. */
1714        if ((reset_port + 1) > p_priv->resend_cont)
1715                p_priv->resend_cont = reset_port + 1;
1716        if (this_urb->status == -EINPROGRESS) {
1717                dev_dbg(&port->dev, "%s already writing\n", __func__);
1718                mdelay(5);
1719                return -1;
1720        }
1721
1722        memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
1723
1724        msg.setBaudRate = 1;
1725        if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1726                                           &msg.baudHi, &msg.baudLo, NULL,
1727                                           device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1728                dev_dbg(&port->dev, "%s - Invalid baud rate requested %d.\n",
1729                                                __func__, p_priv->baud);
1730                msg.baudLo = 0xff;
1731                msg.baudHi = 0xb2;      /* Values for 9600 baud */
1732        }
1733
1734        /* If parity is enabled, we must calculate it ourselves. */
1735        msg.parity = 0;         /* XXX for now */
1736
1737        msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1738        msg.xonFlowControl = 0;
1739
1740        /* Do handshaking outputs, DTR is inverted relative to RTS */
1741        msg.rts = p_priv->rts_state;
1742        msg.dtr = p_priv->dtr_state;
1743
1744        msg.forwardingLength = 16;
1745        msg.forwardMs = 10;
1746        msg.breakThreshold = 45;
1747        msg.xonChar = 17;
1748        msg.xoffChar = 19;
1749
1750        /*msg.returnStatus = 1;
1751        msg.resetDataToggle = 0xff;*/
1752        /* Opening port */
1753        if (reset_port == 1) {
1754                msg._txOn = 1;
1755                msg._txOff = 0;
1756                msg.txFlush = 0;
1757                msg.txForceXoff = 0;
1758                msg.txBreak = 0;
1759                msg.rxOn = 1;
1760                msg.rxOff = 0;
1761                msg.rxFlush = 1;
1762                msg.rxForward = 0;
1763                msg.returnStatus = 0;
1764                msg.resetDataToggle = 0xff;
1765        }
1766        /* Closing port */
1767        else if (reset_port == 2) {
1768                msg._txOn = 0;
1769                msg._txOff = 1;
1770                msg.txFlush = 0;
1771                msg.txForceXoff = 0;
1772                msg.txBreak = 0;
1773                msg.rxOn = 0;
1774                msg.rxOff = 1;
1775                msg.rxFlush = 1;
1776                msg.rxForward = 0;
1777                msg.returnStatus = 0;
1778                msg.resetDataToggle = 0;
1779        }
1780        /* Sending intermediate configs */
1781        else {
1782                msg._txOn = (!p_priv->break_on);
1783                msg._txOff = 0;
1784                msg.txFlush = 0;
1785                msg.txForceXoff = 0;
1786                msg.txBreak = (p_priv->break_on);
1787                msg.rxOn = 0;
1788                msg.rxOff = 0;
1789                msg.rxFlush = 0;
1790                msg.rxForward = 0;
1791                msg.returnStatus = 0;
1792                msg.resetDataToggle = 0x0;
1793        }
1794
1795        p_priv->resend_cont = 0;
1796        memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1797
1798        /* send the data out the device on control endpoint */
1799        this_urb->transfer_buffer_length = sizeof(msg);
1800
1801        err = usb_submit_urb(this_urb, GFP_ATOMIC);
1802        if (err != 0)
1803                dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed\n", __func__);
1804#if 0
1805        else {
1806                dev_dbg(&port->dev, "%s - usb_submit_urb(setup) OK %d bytes\n", __func__,
1807                    this_urb->transfer_buffer_length);
1808        }
1809#endif
1810
1811        return 0;
1812}
1813
1814static int keyspan_usa49_send_setup(struct usb_serial *serial,
1815                                    struct usb_serial_port *port,
1816                                    int reset_port)
1817{
1818        struct keyspan_usa49_portControlMessage msg;
1819        struct usb_ctrlrequest                  *dr = NULL;
1820        struct keyspan_serial_private           *s_priv;
1821        struct keyspan_port_private             *p_priv;
1822        const struct keyspan_device_details     *d_details;
1823        struct urb                              *this_urb;
1824        int                                     err, device_port;
1825
1826        s_priv = usb_get_serial_data(serial);
1827        p_priv = usb_get_serial_port_data(port);
1828        d_details = s_priv->device_details;
1829
1830        this_urb = s_priv->glocont_urb;
1831
1832        /* Work out which port within the device is being setup */
1833        device_port = port->number - port->serial->minor;
1834
1835        /* Make sure we have an urb then send the message */
1836        if (this_urb == NULL) {
1837                dev_dbg(&port->dev, "%s - oops no urb for port %d.\n", __func__, port->number);
1838                return -1;
1839        }
1840
1841        dev_dbg(&port->dev, "%s - endpoint %d port %d (%d)\n",
1842                __func__, usb_pipeendpoint(this_urb->pipe),
1843                port->number, device_port);
1844
1845        /* Save reset port val for resend.
1846           Don't overwrite resend for open/close condition. */
1847        if ((reset_port + 1) > p_priv->resend_cont)
1848                p_priv->resend_cont = reset_port + 1;
1849
1850        if (this_urb->status == -EINPROGRESS) {
1851                /*  dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1852                mdelay(5);
1853                return -1;
1854        }
1855
1856        memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
1857
1858        /*msg.portNumber = port->number;*/
1859        msg.portNumber = device_port;
1860
1861        /* Only set baud rate if it's changed */
1862        if (p_priv->old_baud != p_priv->baud) {
1863                p_priv->old_baud = p_priv->baud;
1864                msg.setClocking = 0xff;
1865                if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1866                                                   &msg.baudHi, &msg.baudLo, &msg.prescaler,
1867                                                   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1868                        dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
1869                                __func__, p_priv->baud);
1870                        msg.baudLo = 0;
1871                        msg.baudHi = 125;       /* Values for 9600 baud */
1872                        msg.prescaler = 10;
1873                }
1874                /* msg.setPrescaler = 0xff; */
1875        }
1876
1877        msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
1878        switch (p_priv->cflag & CSIZE) {
1879        case CS5:
1880                msg.lcr |= USA_DATABITS_5;
1881                break;
1882        case CS6:
1883                msg.lcr |= USA_DATABITS_6;
1884                break;
1885        case CS7:
1886                msg.lcr |= USA_DATABITS_7;
1887                break;
1888        case CS8:
1889                msg.lcr |= USA_DATABITS_8;
1890                break;
1891        }
1892        if (p_priv->cflag & PARENB) {
1893                /* note USA_PARITY_NONE == 0 */
1894                msg.lcr |= (p_priv->cflag & PARODD) ?
1895                        USA_PARITY_ODD : USA_PARITY_EVEN;
1896        }
1897        msg.setLcr = 0xff;
1898
1899        msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1900        msg.xonFlowControl = 0;
1901        msg.setFlowControl = 0xff;
1902
1903        msg.forwardingLength = 16;
1904        msg.xonChar = 17;
1905        msg.xoffChar = 19;
1906
1907        /* Opening port */
1908        if (reset_port == 1) {
1909                msg._txOn = 1;
1910                msg._txOff = 0;
1911                msg.txFlush = 0;
1912                msg.txBreak = 0;
1913                msg.rxOn = 1;
1914                msg.rxOff = 0;
1915                msg.rxFlush = 1;
1916                msg.rxForward = 0;
1917                msg.returnStatus = 0;
1918                msg.resetDataToggle = 0xff;
1919                msg.enablePort = 1;
1920                msg.disablePort = 0;
1921        }
1922        /* Closing port */
1923        else if (reset_port == 2) {
1924                msg._txOn = 0;
1925                msg._txOff = 1;
1926                msg.txFlush = 0;
1927                msg.txBreak = 0;
1928                msg.rxOn = 0;
1929                msg.rxOff = 1;
1930                msg.rxFlush = 1;
1931                msg.rxForward = 0;
1932                msg.returnStatus = 0;
1933                msg.resetDataToggle = 0;
1934                msg.enablePort = 0;
1935                msg.disablePort = 1;
1936        }
1937        /* Sending intermediate configs */
1938        else {
1939                msg._txOn = (!p_priv->break_on);
1940                msg._txOff = 0;
1941                msg.txFlush = 0;
1942                msg.txBreak = (p_priv->break_on);
1943                msg.rxOn = 0;
1944                msg.rxOff = 0;
1945                msg.rxFlush = 0;
1946                msg.rxForward = 0;
1947                msg.returnStatus = 0;
1948                msg.resetDataToggle = 0x0;
1949                msg.enablePort = 0;
1950                msg.disablePort = 0;
1951        }
1952
1953        /* Do handshaking outputs */
1954        msg.setRts = 0xff;
1955        msg.rts = p_priv->rts_state;
1956
1957        msg.setDtr = 0xff;
1958        msg.dtr = p_priv->dtr_state;
1959
1960        p_priv->resend_cont = 0;
1961
1962        /* if the device is a 49wg, we send control message on usb
1963           control EP 0 */
1964
1965        if (d_details->product_id == keyspan_usa49wg_product_id) {
1966                dr = (void *)(s_priv->ctrl_buf);
1967                dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
1968                dr->bRequest = 0xB0;    /* 49wg control message */;
1969                dr->wValue = 0;
1970                dr->wIndex = 0;
1971                dr->wLength = cpu_to_le16(sizeof(msg));
1972
1973                memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
1974
1975                usb_fill_control_urb(this_urb, serial->dev,
1976                                usb_sndctrlpipe(serial->dev, 0),
1977                                (unsigned char *)dr, s_priv->glocont_buf,
1978                                sizeof(msg), usa49_glocont_callback, serial);
1979
1980        } else {
1981                memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1982
1983                /* send the data out the device on control endpoint */
1984                this_urb->transfer_buffer_length = sizeof(msg);
1985        }
1986        err = usb_submit_urb(this_urb, GFP_ATOMIC);
1987        if (err != 0)
1988                dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
1989#if 0
1990        else {
1991                dev_dbg(&port->dev, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__,
1992                        outcont_urb, this_urb->transfer_buffer_length,
1993                        usb_pipeendpoint(this_urb->pipe));
1994        }
1995#endif
1996
1997        return 0;
1998}
1999
2000static int keyspan_usa90_send_setup(struct usb_serial *serial,
2001                                    struct usb_serial_port *port,
2002                                    int reset_port)
2003{
2004        struct keyspan_usa90_portControlMessage msg;
2005        struct keyspan_serial_private           *s_priv;
2006        struct keyspan_port_private             *p_priv;
2007        const struct keyspan_device_details     *d_details;
2008        struct urb                              *this_urb;
2009        int                                     err;
2010        u8                                              prescaler;
2011
2012        s_priv = usb_get_serial_data(serial);
2013        p_priv = usb_get_serial_port_data(port);
2014        d_details = s_priv->device_details;
2015
2016        /* only do something if we have a bulk out endpoint */
2017        this_urb = p_priv->outcont_urb;
2018        if (this_urb == NULL) {
2019                dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
2020                return -1;
2021        }
2022
2023        /* Save reset port val for resend.
2024           Don't overwrite resend for open/close condition. */
2025        if ((reset_port + 1) > p_priv->resend_cont)
2026                p_priv->resend_cont = reset_port + 1;
2027        if (this_urb->status == -EINPROGRESS) {
2028                dev_dbg(&port->dev, "%s already writing\n", __func__);
2029                mdelay(5);
2030                return -1;
2031        }
2032
2033        memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2034
2035        /* Only set baud rate if it's changed */
2036        if (p_priv->old_baud != p_priv->baud) {
2037                p_priv->old_baud = p_priv->baud;
2038                msg.setClocking = 0x01;
2039                if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2040                                                   &msg.baudHi, &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2041                        dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2042                                __func__, p_priv->baud);
2043                        p_priv->baud = 9600;
2044                        d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2045                                &msg.baudHi, &msg.baudLo, &prescaler, 0);
2046                }
2047                msg.setRxMode = 1;
2048                msg.setTxMode = 1;
2049        }
2050
2051        /* modes must always be correctly specified */
2052        if (p_priv->baud > 57600) {
2053                msg.rxMode = RXMODE_DMA;
2054                msg.txMode = TXMODE_DMA;
2055        } else {
2056                msg.rxMode = RXMODE_BYHAND;
2057                msg.txMode = TXMODE_BYHAND;
2058        }
2059
2060        msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2061        switch (p_priv->cflag & CSIZE) {
2062        case CS5:
2063                msg.lcr |= USA_DATABITS_5;
2064                break;
2065        case CS6:
2066                msg.lcr |= USA_DATABITS_6;
2067                break;
2068        case CS7:
2069                msg.lcr |= USA_DATABITS_7;
2070                break;
2071        case CS8:
2072                msg.lcr |= USA_DATABITS_8;
2073                break;
2074        }
2075        if (p_priv->cflag & PARENB) {
2076                /* note USA_PARITY_NONE == 0 */
2077                msg.lcr |= (p_priv->cflag & PARODD) ?
2078                        USA_PARITY_ODD : USA_PARITY_EVEN;
2079        }
2080        if (p_priv->old_cflag != p_priv->cflag) {
2081                p_priv->old_cflag = p_priv->cflag;
2082                msg.setLcr = 0x01;
2083        }
2084
2085        if (p_priv->flow_control == flow_cts)
2086                msg.txFlowControl = TXFLOW_CTS;
2087        msg.setTxFlowControl = 0x01;
2088        msg.setRxFlowControl = 0x01;
2089
2090        msg.rxForwardingLength = 16;
2091        msg.rxForwardingTimeout = 16;
2092        msg.txAckSetting = 0;
2093        msg.xonChar = 17;
2094        msg.xoffChar = 19;
2095
2096        /* Opening port */
2097        if (reset_port == 1) {
2098                msg.portEnabled = 1;
2099                msg.rxFlush = 1;
2100                msg.txBreak = (p_priv->break_on);
2101        }
2102        /* Closing port */
2103        else if (reset_port == 2)
2104                msg.portEnabled = 0;
2105        /* Sending intermediate configs */
2106        else {
2107                msg.portEnabled = 1;
2108                msg.txBreak = (p_priv->break_on);
2109        }
2110
2111        /* Do handshaking outputs */
2112        msg.setRts = 0x01;
2113        msg.rts = p_priv->rts_state;
2114
2115        msg.setDtr = 0x01;
2116        msg.dtr = p_priv->dtr_state;
2117
2118        p_priv->resend_cont = 0;
2119        memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2120
2121        /* send the data out the device on control endpoint */
2122        this_urb->transfer_buffer_length = sizeof(msg);
2123
2124        err = usb_submit_urb(this_urb, GFP_ATOMIC);
2125        if (err != 0)
2126                dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2127        return 0;
2128}
2129
2130static int keyspan_usa67_send_setup(struct usb_serial *serial,
2131                                    struct usb_serial_port *port,
2132                                    int reset_port)
2133{
2134        struct keyspan_usa67_portControlMessage msg;
2135        struct keyspan_serial_private           *s_priv;
2136        struct keyspan_port_private             *p_priv;
2137        const struct keyspan_device_details     *d_details;
2138        struct urb                              *this_urb;
2139        int                                     err, device_port;
2140
2141        s_priv = usb_get_serial_data(serial);
2142        p_priv = usb_get_serial_port_data(port);
2143        d_details = s_priv->device_details;
2144
2145        this_urb = s_priv->glocont_urb;
2146
2147        /* Work out which port within the device is being setup */
2148        device_port = port->number - port->serial->minor;
2149
2150        /* Make sure we have an urb then send the message */
2151        if (this_urb == NULL) {
2152                dev_dbg(&port->dev, "%s - oops no urb for port %d.\n", __func__,
2153                        port->number);
2154                return -1;
2155        }
2156
2157        /* Save reset port val for resend.
2158           Don't overwrite resend for open/close condition. */
2159        if ((reset_port + 1) > p_priv->resend_cont)
2160                p_priv->resend_cont = reset_port + 1;
2161        if (this_urb->status == -EINPROGRESS) {
2162                /*  dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2163                mdelay(5);
2164                return -1;
2165        }
2166
2167        memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2168
2169        msg.port = device_port;
2170
2171        /* Only set baud rate if it's changed */
2172        if (p_priv->old_baud != p_priv->baud) {
2173                p_priv->old_baud = p_priv->baud;
2174                msg.setClocking = 0xff;
2175                if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2176                                                   &msg.baudHi, &msg.baudLo, &msg.prescaler,
2177                                                   device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2178                        dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2179                                __func__, p_priv->baud);
2180                        msg.baudLo = 0;
2181                        msg.baudHi = 125;       /* Values for 9600 baud */
2182                        msg.prescaler = 10;
2183                }
2184                msg.setPrescaler = 0xff;
2185        }
2186
2187        msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2188        switch (p_priv->cflag & CSIZE) {
2189        case CS5:
2190                msg.lcr |= USA_DATABITS_5;
2191                break;
2192        case CS6:
2193                msg.lcr |= USA_DATABITS_6;
2194                break;
2195        case CS7:
2196                msg.lcr |= USA_DATABITS_7;
2197                break;
2198        case CS8:
2199                msg.lcr |= USA_DATABITS_8;
2200                break;
2201        }
2202        if (p_priv->cflag & PARENB) {
2203                /* note USA_PARITY_NONE == 0 */
2204                msg.lcr |= (p_priv->cflag & PARODD) ?
2205                                        USA_PARITY_ODD : USA_PARITY_EVEN;
2206        }
2207        msg.setLcr = 0xff;
2208
2209        msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2210        msg.xonFlowControl = 0;
2211        msg.setFlowControl = 0xff;
2212        msg.forwardingLength = 16;
2213        msg.xonChar = 17;
2214        msg.xoffChar = 19;
2215
2216        if (reset_port == 1) {
2217                /* Opening port */
2218                msg._txOn = 1;
2219                msg._txOff = 0;
2220                msg.txFlush = 0;
2221                msg.txBreak = 0;
2222                msg.rxOn = 1;
2223                msg.rxOff = 0;
2224                msg.rxFlush = 1;
2225                msg.rxForward = 0;
2226                msg.returnStatus = 0;
2227                msg.resetDataToggle = 0xff;
2228        } else if (reset_port == 2) {
2229                /* Closing port */
2230                msg._txOn = 0;
2231                msg._txOff = 1;
2232                msg.txFlush = 0;
2233                msg.txBreak = 0;
2234                msg.rxOn = 0;
2235                msg.rxOff = 1;
2236                msg.rxFlush = 1;
2237                msg.rxForward = 0;
2238                msg.returnStatus = 0;
2239                msg.resetDataToggle = 0;
2240        } else {
2241                /* Sending intermediate configs */
2242                msg._txOn = (!p_priv->break_on);
2243                msg._txOff = 0;
2244                msg.txFlush = 0;
2245                msg.txBreak = (p_priv->break_on);
2246                msg.rxOn = 0;
2247                msg.rxOff = 0;
2248                msg.rxFlush = 0;
2249                msg.rxForward = 0;
2250                msg.returnStatus = 0;
2251                msg.resetDataToggle = 0x0;
2252        }
2253
2254        /* Do handshaking outputs */
2255        msg.setTxTriState_setRts = 0xff;
2256        msg.txTriState_rts = p_priv->rts_state;
2257
2258        msg.setHskoa_setDtr = 0xff;
2259        msg.hskoa_dtr = p_priv->dtr_state;
2260
2261        p_priv->resend_cont = 0;
2262
2263        memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2264
2265        /* send the data out the device on control endpoint */
2266        this_urb->transfer_buffer_length = sizeof(msg);
2267
2268        err = usb_submit_urb(this_urb, GFP_ATOMIC);
2269        if (err != 0)
2270                dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2271        return 0;
2272}
2273
2274static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2275{
2276        struct usb_serial *serial = port->serial;
2277        struct keyspan_serial_private *s_priv;
2278        const struct keyspan_device_details *d_details;
2279
2280        s_priv = usb_get_serial_data(serial);
2281        d_details = s_priv->device_details;
2282
2283        switch (d_details->msg_format) {
2284        case msg_usa26:
2285                keyspan_usa26_send_setup(serial, port, reset_port);
2286                break;
2287        case msg_usa28:
2288                keyspan_usa28_send_setup(serial, port, reset_port);
2289                break;
2290        case msg_usa49:
2291                keyspan_usa49_send_setup(serial, port, reset_port);
2292                break;
2293        case msg_usa90:
2294                keyspan_usa90_send_setup(serial, port, reset_port);
2295                break;
2296        case msg_usa67:
2297                keyspan_usa67_send_setup(serial, port, reset_port);
2298                break;
2299        }
2300}
2301
2302
2303/* Gets called by the "real" driver (ie once firmware is loaded
2304   and renumeration has taken place. */
2305static int keyspan_startup(struct usb_serial *serial)
2306{
2307        int                             i, err;
2308        struct keyspan_serial_private   *s_priv;
2309        const struct keyspan_device_details     *d_details;
2310
2311        for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2312                if (d_details->product_id ==
2313                                le16_to_cpu(serial->dev->descriptor.idProduct))
2314                        break;
2315        if (d_details == NULL) {
2316                dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2317                    __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2318                return 1;
2319        }
2320
2321        /* Setup private data for serial driver */
2322        s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2323        if (!s_priv) {
2324                dev_dbg(&serial->dev->dev, "%s - kmalloc for keyspan_serial_private failed.\n", __func__);
2325                return -ENOMEM;
2326        }
2327
2328        s_priv->device_details = d_details;
2329        usb_set_serial_data(serial, s_priv);
2330
2331        keyspan_setup_urbs(serial);
2332
2333        if (s_priv->instat_urb != NULL) {
2334                err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2335                if (err != 0)
2336                        dev_dbg(&serial->dev->dev, "%s - submit instat urb failed %d\n", __func__, err);
2337        }
2338        if (s_priv->indat_urb != NULL) {
2339                err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2340                if (err != 0)
2341                        dev_dbg(&serial->dev->dev, "%s - submit indat urb failed %d\n", __func__, err);
2342        }
2343
2344        return 0;
2345}
2346
2347static void keyspan_disconnect(struct usb_serial *serial)
2348{
2349        struct keyspan_serial_private *s_priv;
2350
2351        s_priv = usb_get_serial_data(serial);
2352
2353        stop_urb(s_priv->instat_urb);
2354        stop_urb(s_priv->glocont_urb);
2355        stop_urb(s_priv->indat_urb);
2356}
2357
2358static void keyspan_release(struct usb_serial *serial)
2359{
2360        struct keyspan_serial_private *s_priv;
2361
2362        s_priv = usb_get_serial_data(serial);
2363
2364        usb_free_urb(s_priv->instat_urb);
2365        usb_free_urb(s_priv->indat_urb);
2366        usb_free_urb(s_priv->glocont_urb);
2367
2368        kfree(s_priv);
2369}
2370
2371static int keyspan_port_probe(struct usb_serial_port *port)
2372{
2373        struct usb_serial *serial = port->serial;
2374        struct keyspan_serial_private *s_priv;
2375        struct keyspan_port_private *p_priv;
2376        const struct keyspan_device_details *d_details;
2377        struct callbacks *cback;
2378        int endp;
2379        int port_num;
2380        int i;
2381
2382        s_priv = usb_get_serial_data(serial);
2383        d_details = s_priv->device_details;
2384
2385        p_priv = kzalloc(sizeof(*p_priv), GFP_KERNEL);
2386        if (!p_priv)
2387                return -ENOMEM;
2388
2389        p_priv->device_details = d_details;
2390
2391        /* Setup values for the various callback routines */
2392        cback = &keyspan_callbacks[d_details->msg_format];
2393
2394        port_num = port->number - port->serial->minor;
2395
2396        /* Do indat endpoints first, once for each flip */
2397        endp = d_details->indat_endpoints[port_num];
2398        for (i = 0; i <= d_details->indat_endp_flip; ++i, ++endp) {
2399                p_priv->in_urbs[i] = keyspan_setup_urb(serial, endp,
2400                                                USB_DIR_IN, port,
2401                                                p_priv->in_buffer[i], 64,
2402                                                cback->indat_callback);
2403        }
2404        /* outdat endpoints also have flip */
2405        endp = d_details->outdat_endpoints[port_num];
2406        for (i = 0; i <= d_details->outdat_endp_flip; ++i, ++endp) {
2407                p_priv->out_urbs[i] = keyspan_setup_urb(serial, endp,
2408                                                USB_DIR_OUT, port,
2409                                                p_priv->out_buffer[i], 64,
2410                                                cback->outdat_callback);
2411        }
2412        /* inack endpoint */
2413        p_priv->inack_urb = keyspan_setup_urb(serial,
2414                                        d_details->inack_endpoints[port_num],
2415                                        USB_DIR_IN, port,
2416                                        p_priv->inack_buffer, 1,
2417                                        cback->inack_callback);
2418        /* outcont endpoint */
2419        p_priv->outcont_urb = keyspan_setup_urb(serial,
2420                                        d_details->outcont_endpoints[port_num],
2421                                        USB_DIR_OUT, port,
2422                                        p_priv->outcont_buffer, 64,
2423                                         cback->outcont_callback);
2424
2425        usb_set_serial_port_data(port, p_priv);
2426
2427        return 0;
2428}
2429
2430static int keyspan_port_remove(struct usb_serial_port *port)
2431{
2432        struct keyspan_port_private *p_priv;
2433        int i;
2434
2435        p_priv = usb_get_serial_port_data(port);
2436
2437        stop_urb(p_priv->inack_urb);
2438        stop_urb(p_priv->outcont_urb);
2439        for (i = 0; i < 2; i++) {
2440                stop_urb(p_priv->in_urbs[i]);
2441                stop_urb(p_priv->out_urbs[i]);
2442        }
2443
2444        usb_free_urb(p_priv->inack_urb);
2445        usb_free_urb(p_priv->outcont_urb);
2446        for (i = 0; i < 2; i++) {
2447                usb_free_urb(p_priv->in_urbs[i]);
2448                usb_free_urb(p_priv->out_urbs[i]);
2449        }
2450
2451        kfree(p_priv);
2452
2453        return 0;
2454}
2455
2456MODULE_AUTHOR(DRIVER_AUTHOR);
2457MODULE_DESCRIPTION(DRIVER_DESC);
2458MODULE_LICENSE("GPL");
2459
2460MODULE_FIRMWARE("keyspan/usa28.fw");
2461MODULE_FIRMWARE("keyspan/usa28x.fw");
2462MODULE_FIRMWARE("keyspan/usa28xa.fw");
2463MODULE_FIRMWARE("keyspan/usa28xb.fw");
2464MODULE_FIRMWARE("keyspan/usa19.fw");
2465MODULE_FIRMWARE("keyspan/usa19qi.fw");
2466MODULE_FIRMWARE("keyspan/mpr.fw");
2467MODULE_FIRMWARE("keyspan/usa19qw.fw");
2468MODULE_FIRMWARE("keyspan/usa18x.fw");
2469MODULE_FIRMWARE("keyspan/usa19w.fw");
2470MODULE_FIRMWARE("keyspan/usa49w.fw");
2471MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2472