linux/drivers/staging/quatech_usb2/quatech_usb2.c
<<
>>
Prefs
   1/*
   2 * Driver for Quatech Inc USB2.0 to serial adaptors. Largely unrelated to the
   3 * serqt_usb driver, based on a re-write of the vendor supplied serqt_usb2 code,
   4 * which is unrelated to the serqt_usb2 in the staging kernel
   5 */
   6
   7#include <linux/errno.h>
   8#include <linux/init.h>
   9#include <linux/slab.h>
  10#include <linux/tty.h>
  11#include <linux/tty_driver.h>
  12#include <linux/tty_flip.h>
  13#include <linux/module.h>
  14#include <linux/serial.h>
  15#include <linux/usb.h>
  16#include <linux/usb/serial.h>
  17#include <linux/uaccess.h>
  18
  19static int debug;
  20
  21/* Version Information */
  22#define DRIVER_VERSION "v2.00"
  23#define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
  24#define DRIVER_DESC "Quatech USB 2.0 to Serial Driver"
  25
  26/* vendor and device IDs */
  27#define USB_VENDOR_ID_QUATECH 0x061d    /* Quatech VID */
  28#define QUATECH_SSU2_100 0xC120         /* RS232 single port */
  29#define QUATECH_DSU2_100 0xC140         /* RS232 dual port */
  30#define QUATECH_DSU2_400 0xC150         /* RS232/422/485 dual port */
  31#define QUATECH_QSU2_100 0xC160         /* RS232 four port */
  32#define QUATECH_QSU2_400 0xC170         /* RS232/422/485 four port */
  33#define QUATECH_ESU2_100 0xC1A0         /* RS232 eight port */
  34#define QUATECH_ESU2_400 0xC180         /* RS232/422/485 eight port */
  35
  36/* magic numbers go here, when we find out which ones are needed */
  37
  38#define QU2BOXPWRON 0x8000              /* magic number to turn FPGA power on */
  39#define QU2BOX232 0x40                  /* RS232 mode on MEI devices */
  40#define QU2BOXSPD9600 0x60              /* set speed to 9600 baud */
  41#define QT2_FIFO_DEPTH 1024                     /* size of hardware fifos */
  42#define QT2_TX_HEADER_LENGTH    5
  43/* length of the header sent to the box with each write URB */
  44
  45/* directions for USB transfers */
  46#define USBD_TRANSFER_DIRECTION_IN    0xc0
  47#define USBD_TRANSFER_DIRECTION_OUT   0x40
  48
  49/* special Quatech command IDs. These are pushed down the
  50 USB control pipe to get the box on the end to do things */
  51#define QT_SET_GET_DEVICE               0xc2
  52#define QT_OPEN_CLOSE_CHANNEL           0xca
  53/*#define QT_GET_SET_PREBUF_TRIG_LVL    0xcc
  54#define QT_SET_ATF                      0xcd*/
  55#define QT2_GET_SET_REGISTER                    0xc0
  56#define QT2_GET_SET_UART                        0xc1
  57#define QT2_HW_FLOW_CONTROL_MASK                0xc5
  58#define QT2_SW_FLOW_CONTROL_MASK                0xc6
  59#define QT2_SW_FLOW_CONTROL_DISABLE             0xc7
  60#define QT2_BREAK_CONTROL                       0xc8
  61#define QT2_STOP_RECEIVE                        0xe0
  62#define QT2_FLUSH_DEVICE                        0xc4
  63#define QT2_GET_SET_QMCR                        0xe1
  64
  65/* sorts of flush we can do on */
  66#define QT2_FLUSH_RX                    0x00
  67#define QT2_FLUSH_TX                    0x01
  68
  69/* port setting constants, used to set up serial port speeds, flow
  70 * control and so on */
  71#define QT2_SERIAL_MCR_DTR      0x01
  72#define QT2_SERIAL_MCR_RTS      0x02
  73#define QT2_SERIAL_MCR_LOOP     0x10
  74
  75#define QT2_SERIAL_MSR_CTS      0x10
  76#define QT2_SERIAL_MSR_CD       0x80
  77#define QT2_SERIAL_MSR_RI       0x40
  78#define QT2_SERIAL_MSR_DSR      0x20
  79#define QT2_SERIAL_MSR_MASK     0xf0
  80
  81#define QT2_SERIAL_8_DATA       0x03
  82#define QT2_SERIAL_7_DATA       0x02
  83#define QT2_SERIAL_6_DATA       0x01
  84#define QT2_SERIAL_5_DATA       0x00
  85
  86#define QT2_SERIAL_ODD_PARITY   0x08
  87#define QT2_SERIAL_EVEN_PARITY  0x18
  88#define QT2_SERIAL_TWO_STOPB    0x04
  89#define QT2_SERIAL_ONE_STOPB    0x00
  90
  91#define QT2_MAX_BAUD_RATE       921600
  92#define QT2_MAX_BAUD_REMAINDER  4608
  93
  94#define QT2_SERIAL_LSR_OE       0x02
  95#define QT2_SERIAL_LSR_PE       0x04
  96#define QT2_SERIAL_LSR_FE       0x08
  97#define QT2_SERIAL_LSR_BI       0x10
  98
  99/* value of Line Status Register when UART has completed
 100 * emptying data out on the line */
 101#define QT2_LSR_TEMT     0x40
 102
 103/* register numbers on each UART, for use with  qt2_box_[get|set]_register*/
 104#define  QT2_XMT_HOLD_REGISTER          0x00
 105#define  QT2_XVR_BUFFER_REGISTER        0x00
 106#define  QT2_FIFO_CONTROL_REGISTER      0x02
 107#define  QT2_LINE_CONTROL_REGISTER      0x03
 108#define  QT2_MODEM_CONTROL_REGISTER     0x04
 109#define  QT2_LINE_STATUS_REGISTER       0x05
 110#define  QT2_MODEM_STATUS_REGISTER      0x06
 111
 112/* handy macros for doing escape sequence parsing on data reads */
 113#define THISCHAR        ((unsigned char *)(urb->transfer_buffer))[i]
 114#define NEXTCHAR        ((unsigned char *)(urb->transfer_buffer))[i + 1]
 115#define THIRDCHAR       ((unsigned char *)(urb->transfer_buffer))[i + 2]
 116#define FOURTHCHAR      ((unsigned char *)(urb->transfer_buffer))[i + 3]
 117#define FIFTHCHAR       ((unsigned char *)(urb->transfer_buffer))[i + 4]
 118
 119static struct usb_device_id quausb2_id_table[] = {
 120        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
 121        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
 122        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
 123        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
 124        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
 125        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
 126        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
 127        {}      /* Terminating entry */
 128};
 129
 130MODULE_DEVICE_TABLE(usb, quausb2_id_table);
 131
 132/* custom structures we need go here */
 133static struct usb_driver quausb2_usb_driver = {
 134        .name = "quatech-usb2-serial",
 135        .probe = usb_serial_probe,
 136        .disconnect = usb_serial_disconnect,
 137        .id_table = quausb2_id_table,
 138        .no_dynamic_id = 1,
 139};
 140
 141/**
 142 * quatech2_port: Structure in which to keep all the messy stuff that this
 143 * driver needs alongside the usb_serial_port structure
 144 * @read_urb_busy: Flag indicating that port->read_urb is in use
 145 * @close_pending: flag indicating that this port is in the process of
 146 * being closed (and so no new reads / writes should be started).
 147 * @shadowLSR: Last received state of the line status register, holds the
 148 * value of the line status flags from the port
 149 * @shadowMSR: Last received state of the modem status register, holds
 150 * the value of the modem status received from the port
 151 * @rcv_flush: Flag indicating that a receive flush has occured on
 152 * the hardware.
 153 * @xmit_flush: Flag indicating that a transmit flush has been processed by
 154 * the hardware.
 155 * @tx_pending_bytes: Number of bytes waiting to be sent. This total
 156 * includes the size (excluding header) of URBs that have been submitted but
 157 * have not yet been sent to to the device, and bytes that have been sent out
 158 * of the port but not yet reported sent by the "xmit_empty" messages (which
 159 * indicate the number of bytes sent each time they are recieved, despite the
 160 * misleading name).
 161 * - Starts at zero when port is initialised.
 162 * - is incremented by the size of the data to be written (no headers)
 163 * each time a write urb is dispatched.
 164 * - is decremented each time a "transmit empty" message is received
 165 * by the driver in the data stream.
 166 * @lock: Mutex to lock access to this structure when we need to ensure that
 167 * races don't occur to access bits of it.
 168 * @open_count: The number of uses of the port currently having
 169 * it open, i.e. the reference count.
 170 */
 171struct quatech2_port {
 172        int     magic;
 173        bool    read_urb_busy;
 174        bool    close_pending;
 175        __u8    shadowLSR;
 176        __u8    shadowMSR;
 177        bool    rcv_flush;
 178        bool    xmit_flush;
 179        int     tx_pending_bytes;
 180        struct mutex modelock;
 181        int     open_count;
 182
 183        char    active;         /* someone has this device open */
 184        unsigned char           *xfer_to_tty_buffer;
 185        wait_queue_head_t       wait;
 186        __u8    shadowLCR;      /* last LCR value received */
 187        __u8    shadowMCR;      /* last MCR value received */
 188        char    RxHolding;
 189        struct semaphore        pend_xmit_sem;  /* locks this structure */
 190        spinlock_t lock;
 191};
 192
 193/**
 194 * Structure to hold device-wide internal status information
 195 * @param ReadBulkStopped The last bulk read attempt ended in tears
 196 * @param open_ports The number of serial ports currently in use on the box
 197 * @param current_port Pointer to the serial port structure of the port which
 198 * the read stream is currently directed to. Escape sequences in the read
 199 * stream will change this around as data arrives from different ports on the
 200 * box
 201 * @buffer_size: The max size buffer each URB can take, used to set the size of
 202 * the buffers allocated for writing to each port on the device (we need to
 203 * store this because it is known only to the endpoint, but used each time a
 204 * port is opened and a new buffer is allocated.
 205 */
 206struct quatech2_dev {
 207        bool    ReadBulkStopped;
 208        char    open_ports;
 209        struct usb_serial_port *current_port;
 210        int     buffer_size;
 211};
 212
 213/* structure which holds line and modem status flags */
 214struct qt2_status_data {
 215        __u8 line_status;
 216        __u8 modem_status;
 217};
 218
 219/* Function prototypes */
 220static int qt2_boxpoweron(struct usb_serial *serial);
 221static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
 222                        __u8 QMCR_Value);
 223static int port_paranoia_check(struct usb_serial_port *port,
 224                        const char *function);
 225static int serial_paranoia_check(struct usb_serial *serial,
 226                         const char *function);
 227static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
 228                        *port);
 229static inline void qt2_set_port_private(struct usb_serial_port *port,
 230                        struct quatech2_port *data);
 231static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
 232                        *serial);
 233static inline void qt2_set_dev_private(struct usb_serial *serial,
 234                        struct quatech2_dev *data);
 235static int qt2_openboxchannel(struct usb_serial *serial, __u16
 236                        Uart_Number, struct qt2_status_data *pDeviceData);
 237static int qt2_closeboxchannel(struct usb_serial *serial, __u16
 238                        Uart_Number);
 239static int qt2_conf_uart(struct usb_serial *serial,  unsigned short Uart_Number,
 240                         unsigned short divisor, unsigned char LCR);
 241static void qt2_read_bulk_callback(struct urb *urb);
 242static void qt2_write_bulk_callback(struct urb *urb);
 243static void qt2_process_line_status(struct usb_serial_port *port,
 244                              unsigned char LineStatus);
 245static void qt2_process_modem_status(struct usb_serial_port *port,
 246                               unsigned char ModemStatus);
 247static void qt2_process_xmit_empty(struct usb_serial_port *port,
 248        unsigned char fourth_char, unsigned char fifth_char);
 249static void qt2_process_port_change(struct usb_serial_port *port,
 250                              unsigned char New_Current_Port);
 251static void qt2_process_rcv_flush(struct usb_serial_port *port);
 252static void qt2_process_xmit_flush(struct usb_serial_port *port);
 253static void qt2_process_rx_char(struct usb_serial_port *port,
 254                                unsigned char data);
 255static int qt2_box_get_register(struct usb_serial *serial,
 256                unsigned char uart_number, unsigned short register_num,
 257                __u8 *pValue);
 258static int qt2_box_set_register(struct usb_serial *serial,
 259                unsigned short Uart_Number, unsigned short Register_Num,
 260                unsigned short Value);
 261static int qt2_box_flush(struct usb_serial *serial,  unsigned char uart_number,
 262                unsigned short rcv_or_xmit);
 263static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
 264                unsigned short default_divisor, unsigned char default_LCR);
 265static int qt2_boxsethw_flowctl(struct usb_serial *serial,
 266                unsigned int UartNumber, bool bSet);
 267static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
 268                unsigned char stop_char,  unsigned char start_char);
 269static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber);
 270static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
 271                         unsigned short stop);
 272
 273/* implementation functions, roughly in order of use, are here */
 274static int qt2_calc_num_ports(struct usb_serial *serial)
 275{
 276        int num_ports;
 277        int flag_as_400;
 278        switch (serial->dev->descriptor.idProduct) {
 279        case QUATECH_SSU2_100:
 280                num_ports = 1;
 281                break;
 282
 283        case QUATECH_DSU2_400:
 284                flag_as_400 = true;
 285        case QUATECH_DSU2_100:
 286                num_ports = 2;
 287        break;
 288
 289        case QUATECH_QSU2_400:
 290                flag_as_400 = true;
 291        case QUATECH_QSU2_100:
 292                num_ports = 4;
 293        break;
 294
 295        case QUATECH_ESU2_400:
 296                flag_as_400 = true;
 297        case QUATECH_ESU2_100:
 298                num_ports = 8;
 299        break;
 300        default:
 301        num_ports = 1;
 302        break;
 303        }
 304        return num_ports;
 305}
 306
 307static int qt2_attach(struct usb_serial *serial)
 308{
 309        struct usb_serial_port *port;
 310        struct quatech2_port *qt2_port; /* port-specific private data pointer */
 311        struct quatech2_dev  *qt2_dev;  /* dev-specific private data pointer */
 312        int i;
 313        /* stuff for storing endpoint addresses now */
 314        struct usb_endpoint_descriptor *endpoint;
 315        struct usb_host_interface *iface_desc;
 316        struct usb_serial_port *port0;  /* first port structure on device */
 317
 318        /* check how many endpoints there are on the device, for
 319         * sanity's sake */
 320        dbg("%s(): Endpoints: %d bulk in, %d bulk out, %d interrupt in",
 321                        __func__, serial->num_bulk_in,
 322                        serial->num_bulk_out, serial->num_interrupt_in);
 323        if ((serial->num_bulk_in != 1) || (serial->num_bulk_out != 1)) {
 324                dbg("Device has wrong number of bulk endpoints!");
 325                return -ENODEV;
 326        }
 327        iface_desc = serial->interface->cur_altsetting;
 328
 329        /* Set up per-device private data, storing extra data alongside
 330         * struct usb_serial */
 331        qt2_dev = kzalloc(sizeof(*qt2_dev), GFP_KERNEL);
 332        if (!qt2_dev) {
 333                dbg("%s: kmalloc for quatech2_dev failed!",
 334                    __func__);
 335                return -ENOMEM;
 336        }
 337        qt2_dev->open_ports = 0;        /* no ports open */
 338        qt2_set_dev_private(serial, qt2_dev);   /* store private data */
 339
 340        /* Now setup per port private data, which replaces all the things
 341         * that quatech added to standard kernel structures in their driver */
 342        for (i = 0; i < serial->num_ports; i++) {
 343                port = serial->port[i];
 344                qt2_port = kzalloc(sizeof(*qt2_port), GFP_KERNEL);
 345                if (!qt2_port) {
 346                        dbg("%s: kmalloc for quatech2_port (%d) failed!.",
 347                            __func__, i);
 348                        return -ENOMEM;
 349                }
 350                /* initialise stuff in the structure */
 351                qt2_port->open_count = 0;       /* port is not open */
 352                spin_lock_init(&qt2_port->lock);
 353                mutex_init(&qt2_port->modelock);
 354                qt2_set_port_private(port, qt2_port);
 355        }
 356
 357        /* gain access to port[0]'s structure because we want to store
 358         * device-level stuff in it */
 359        if (serial_paranoia_check(serial, __func__))
 360                return -ENODEV;
 361        port0 = serial->port[0]; /* get the first port's device structure */
 362
 363        /* print endpoint addresses so we can check them later
 364         * by hand */
 365        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
 366                endpoint = &iface_desc->endpoint[i].desc;
 367                if ((endpoint->bEndpointAddress & 0x80) &&
 368                        ((endpoint->bmAttributes & 3) == 0x02)) {
 369                        /* we found a bulk in endpoint */
 370                        dbg("found bulk in at %#.2x",
 371                                endpoint->bEndpointAddress);
 372                }
 373
 374                if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
 375                        ((endpoint->bmAttributes & 3) == 0x02)) {
 376                        /* we found a bulk out endpoint */
 377                        dbg("found bulk out at %#.2x",
 378                                endpoint->bEndpointAddress);
 379                        qt2_dev->buffer_size = endpoint->wMaxPacketSize;
 380                        /* max size of URB needs recording for the device */
 381                }
 382        }       /* end printing endpoint addresses */
 383
 384        /* switch on power to the hardware */
 385        if (qt2_boxpoweron(serial) < 0) {
 386                dbg("qt2_boxpoweron() failed");
 387                goto startup_error;
 388        }
 389        /* set all ports to RS232 mode */
 390        for (i = 0; i < serial->num_ports; ++i) {
 391                if (qt2_boxsetQMCR(serial, i, QU2BOX232) < 0) {
 392                        dbg("qt2_boxsetQMCR() on port %d failed",
 393                                i);
 394                        goto startup_error;
 395                }
 396        }
 397
 398        return 0;
 399
 400startup_error:
 401        for (i = 0; i < serial->num_ports; i++) {
 402                port = serial->port[i];
 403                qt2_port = qt2_get_port_private(port);
 404                kfree(qt2_port);
 405                qt2_set_port_private(port, NULL);
 406        }
 407        qt2_dev = qt2_get_dev_private(serial);
 408        kfree(qt2_dev);
 409        qt2_set_dev_private(serial, NULL);
 410
 411        dbg("Exit fail %s\n", __func__);
 412        return -EIO;
 413}
 414
 415static void qt2_release(struct usb_serial *serial)
 416{
 417        struct usb_serial_port *port;
 418        struct quatech2_port *qt_port;
 419        int i;
 420
 421        dbg("enterting %s", __func__);
 422
 423        for (i = 0; i < serial->num_ports; i++) {
 424                port = serial->port[i];
 425                if (!port)
 426                        continue;
 427
 428                qt_port = usb_get_serial_port_data(port);
 429                kfree(qt_port);
 430                usb_set_serial_port_data(port, NULL);
 431        }
 432}
 433/* This function is called once per serial port on the device, when
 434 * that port is opened by a userspace application.
 435 * The tty_struct and the usb_serial_port belong to this port,
 436 * i.e. there are multiple ones for a multi-port device.
 437 * However the usb_serial_port structure has a back-pointer
 438 * to the parent usb_serial structure which belongs to the device,
 439 * so we can access either the device-wide information or
 440 * any other port's information (because there are also forward
 441 * pointers) via that pointer.
 442 * This is most helpful if the device shares resources (e.g. end
 443 * points) between different ports
 444 */
 445int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
 446{
 447        struct usb_serial *serial;      /* device structure */
 448        struct usb_serial_port *port0;  /* first port structure on device */
 449        struct quatech2_port *port_extra;       /* extra data for this port */
 450        struct quatech2_port *port0_extra;      /* extra data for first port */
 451        struct quatech2_dev *dev_extra;         /* extra data for the device */
 452        struct qt2_status_data ChannelData;
 453        unsigned short default_divisor = QU2BOXSPD9600;
 454        unsigned char  default_LCR = QT2_SERIAL_8_DATA;
 455        int status;
 456        int result;
 457
 458        if (port_paranoia_check(port, __func__))
 459                return -ENODEV;
 460
 461        dbg("%s(): port %d", __func__, port->number);
 462
 463        serial = port->serial;  /* get the parent device structure */
 464        if (serial_paranoia_check(serial, __func__)) {
 465                dbg("usb_serial struct failed sanity check");
 466                return -ENODEV;
 467        }
 468        dev_extra = qt2_get_dev_private(serial);
 469        /* get the device private data */
 470        if (dev_extra == NULL) {
 471                dbg("device extra data pointer is null");
 472                return -ENODEV;
 473        }
 474        port0 = serial->port[0]; /* get the first port's device structure */
 475        if (port_paranoia_check(port0, __func__)) {
 476                dbg("port0 usb_serial_port struct failed sanity check");
 477                return -ENODEV;
 478        }
 479
 480        port_extra = qt2_get_port_private(port);
 481        port0_extra = qt2_get_port_private(port0);
 482        if (port_extra == NULL || port0_extra == NULL) {
 483                dbg("failed to get private data for port or port0");
 484                return -ENODEV;
 485        }
 486
 487        /* FIXME: are these needed?  Does it even do anything useful? */
 488        /* get the modem and line status values from the UART */
 489        status = qt2_openboxchannel(serial, port->number,
 490                        &ChannelData);
 491        if (status < 0) {
 492                dbg("qt2_openboxchannel on channel %d failed",
 493                    port->number);
 494                return status;
 495        }
 496        port_extra->shadowLSR = ChannelData.line_status &
 497                        (QT2_SERIAL_LSR_OE | QT2_SERIAL_LSR_PE |
 498                        QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
 499        port_extra->shadowMSR = ChannelData.modem_status &
 500                        (QT2_SERIAL_MSR_CTS | QT2_SERIAL_MSR_DSR |
 501                        QT2_SERIAL_MSR_RI | QT2_SERIAL_MSR_CD);
 502
 503/*      port_extra->fifo_empty_flag = true;*/
 504        dbg("qt2_openboxchannel on channel %d completed.",
 505            port->number);
 506
 507        /* Set Baud rate to default and turn off flow control here */
 508        status = qt2_conf_uart(serial, port->number, default_divisor,
 509                                default_LCR);
 510        if (status < 0) {
 511                dbg("qt2_conf_uart() failed on channel %d",
 512                    port->number);
 513                return status;
 514        }
 515        dbg("qt2_conf_uart() completed on channel %d",
 516                port->number);
 517
 518        /*
 519         * At this point we will need some end points to make further progress.
 520         * Handlily, the correct endpoint addresses have been filled out into
 521         * the usb_serial_port structure for us by the driver core, so we
 522         * already have access to them.
 523         * As there is only one bulk in and one bulk out end-point, these are in
 524         * port[0]'s structure, and the rest are uninitialised. Handily,
 525         * when we do a write to a port, we will use the same endpoint
 526         * regardless of the port, with a 5-byte header added on to
 527         * tell the box which port it should eventually come out of, so we only
 528         * need the one set of endpoints. We will have one URB per port for
 529         * writing, so that multiple ports can be writing at once.
 530         * Finally we need a bulk in URB to use for background reads from the
 531         * device, which will deal with uplink data from the box to host.
 532         */
 533        dbg("port0 bulk in endpoint is %#.2x", port0->bulk_in_endpointAddress);
 534        dbg("port0 bulk out endpoint is %#.2x",
 535                port0->bulk_out_endpointAddress);
 536
 537        /* set up write_urb for bulk out transfers on this port. The USB
 538         * serial framework will have allocated a blank URB, buffer etc for
 539         * port0 when it put the endpoints there, but not for any of the other
 540         * ports on the device because there are no more endpoints. Thus we
 541         * have to allocate our own URBs for ports 1-7
 542         */
 543        if (port->write_urb == NULL) {
 544                dbg("port->write_urb == NULL, allocating one");
 545                port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
 546                if (!port->write_urb) {
 547                        err("Allocating write URB failed");
 548                        return -ENOMEM;
 549                }
 550                /* buffer same size as port0 */
 551                port->bulk_out_size = dev_extra->buffer_size;
 552                port->bulk_out_buffer = kmalloc(port->bulk_out_size,
 553                                                GFP_KERNEL);
 554                if (!port->bulk_out_buffer) {
 555                        err("Couldn't allocate bulk_out_buffer");
 556                        return -ENOMEM;
 557                }
 558        }
 559        if (serial->dev == NULL)
 560                dbg("serial->dev == NULL");
 561        dbg("port->bulk_out_size is %d", port->bulk_out_size);
 562
 563        usb_fill_bulk_urb(port->write_urb, serial->dev,
 564                        usb_sndbulkpipe(serial->dev,
 565                        port0->bulk_out_endpointAddress),
 566                        port->bulk_out_buffer,
 567                        port->bulk_out_size,
 568                        qt2_write_bulk_callback,
 569                        port);
 570        port_extra->tx_pending_bytes = 0;
 571
 572        if (dev_extra->open_ports == 0) {
 573                /* this is first port to be opened, so need the read URB
 574                 * initialised for bulk in transfers (this is shared amongst
 575                 * all the ports on the device) */
 576                usb_fill_bulk_urb(port0->read_urb, serial->dev,
 577                        usb_rcvbulkpipe(serial->dev,
 578                        port0->bulk_in_endpointAddress),
 579                        port0->bulk_in_buffer,
 580                        port0->bulk_in_size,
 581                        qt2_read_bulk_callback, serial);
 582                dbg("port0 bulk in URB intialised");
 583
 584                /* submit URB, i.e. start reading from device (async) */
 585                dev_extra->ReadBulkStopped = false;
 586                port_extra->read_urb_busy = true;
 587                result = usb_submit_urb(port->read_urb, GFP_KERNEL);
 588                if (result) {
 589                        dev_err(&port->dev,
 590                                 "%s(): Error %d submitting bulk in urb",
 591                                __func__, result);
 592                        port_extra->read_urb_busy = false;
 593                        dev_extra->ReadBulkStopped = true;
 594                }
 595
 596                /* When the first port is opened, initialise the value of
 597                 * current_port in dev_extra to this port, so it is set
 598                 * to something. Once the box sends data it will send the
 599                 * relevant escape sequences to get it to the right port anyway
 600                 */
 601                dev_extra->current_port = port;
 602        }
 603
 604        /* initialize our wait queues */
 605        init_waitqueue_head(&port_extra->wait);
 606        /* increment the count of openings of this port by one */
 607        port_extra->open_count++;
 608
 609        /* remember to store dev_extra, port_extra and port0_extra back again at
 610         * end !*/
 611        qt2_set_port_private(port, port_extra);
 612        qt2_set_port_private(serial->port[0], port0_extra);
 613        qt2_set_dev_private(serial, dev_extra);
 614
 615        dev_extra->open_ports++; /* one more port opened */
 616
 617        return 0;
 618}
 619
 620/* called when a port is closed by userspace. It won't be called, however,
 621 * until calls to chars_in_buffer() reveal that the port has completed
 622 * sending buffered data, and there is nothing else to do. Thus we don't have
 623 * to rely on forcing data through in this function. */
 624/* Setting close_pending should keep new data from being written out,
 625 * once all the data in the enpoint buffers is moved out we won't get
 626 * any more. */
 627/* BoxStopReceive would keep any more data from coming from a given
 628 * port, but isn't called by the vendor driver, although their comments
 629 * mention it. Should it be used here to stop the inbound data
 630 * flow?
 631 */
 632static void qt2_close(struct usb_serial_port *port)
 633{
 634        /* time out value for flush loops */
 635        unsigned long jift;
 636        struct quatech2_port *port_extra;       /* extra data for this port */
 637        struct usb_serial *serial;      /* device structure */
 638        struct quatech2_dev *dev_extra; /* extra data for the device */
 639        __u8  lsr_value = 0;    /* value of Line Status Register */
 640        int status;     /* result of last USB comms function */
 641
 642        dbg("%s(): port %d", __func__, port->number);
 643        serial = port->serial;  /* get the parent device structure */
 644        dev_extra = qt2_get_dev_private(serial);
 645        /* get the device private data */
 646        port_extra = qt2_get_port_private(port); /* port private data */
 647
 648        /* we don't need to force flush though the hardware, so we skip using
 649         * qt2_box_flush() here */
 650
 651        /* we can now (and only now) stop reading data */
 652        port_extra->close_pending = true;
 653        dbg("%s(): port_extra->close_pending = true", __func__);
 654        /* although the USB side is now empty, the UART itself may
 655         * still be pushing characters out over the line, so we have to
 656         * wait testing the actual line status until the lines change
 657         * indicating that the data is done transfering. */
 658        /* FIXME: slow this polling down so it doesn't run the USB bus flat out
 659         * if it actually has to spend any time in this loop (which it normally
 660         * doesn't because the buffer is nearly empty) */
 661        jift = jiffies + (10 * HZ);     /* 10 sec timeout */
 662        do {
 663                status = qt2_box_get_register(serial, port->number,
 664                        QT2_LINE_STATUS_REGISTER, &lsr_value);
 665                if (status < 0) {
 666                        dbg("%s(): qt2_box_get_register failed", __func__);
 667                        break;
 668                }
 669                if ((lsr_value & QT2_LSR_TEMT)) {
 670                        dbg("UART done sending");
 671                        break;
 672                }
 673                schedule();
 674        } while (jiffies <= jift);
 675
 676        status = qt2_closeboxchannel(serial, port->number);
 677        if (status < 0)
 678                dbg("%s(): port %d qt2_box_open_close_channel failed",
 679                        __func__, port->number);
 680        /* to avoid leaking URBs, we should now free the write_urb for this
 681         * port and set the pointer to null so that next time the port is opened
 682         * a new URB is allocated. This avoids leaking URBs when the device is
 683         * removed */
 684        usb_free_urb(port->write_urb);
 685        kfree(port->bulk_out_buffer);
 686        port->bulk_out_buffer = NULL;
 687        port->bulk_out_size = 0;
 688
 689        /* decrement the count of openings of this port by one */
 690        port_extra->open_count--;
 691        /* one less overall open as well */
 692        dev_extra->open_ports--;
 693        dbg("%s(): Exit, dev_extra->open_ports  = %d", __func__,
 694                dev_extra->open_ports);
 695}
 696
 697/**
 698 * qt2_write - write bytes from the tty layer out to the USB device.
 699 * @buf: The data to be written, size at least count.
 700 * @count: The number of bytes requested for transmission.
 701 * @return The number of bytes actually accepted for transmission to the device.
 702 */
 703static int qt2_write(struct tty_struct *tty, struct usb_serial_port *port,
 704                const unsigned char *buf, int count)
 705{
 706        struct usb_serial *serial;      /* parent device struct */
 707        __u8 header_array[5];   /* header used to direct writes to the correct
 708        port on the device */
 709        struct quatech2_port *port_extra;       /* extra data for this port */
 710        int result;
 711
 712        serial = port->serial; /* get the parent device of the port */
 713        port_extra = qt2_get_port_private(port); /* port extra info */
 714        if (serial == NULL)
 715                return -ENODEV;
 716        dbg("%s(): port %d, requested to write %d bytes, %d already pending",
 717                __func__, port->number, count, port_extra->tx_pending_bytes);
 718
 719        if (count <= 0) {
 720                dbg("%s(): write request of <= 0 bytes", __func__);
 721                return 0;       /* no bytes written */
 722        }
 723
 724        /* check if the write urb is already in use, i.e. data already being
 725         * sent to this port */
 726        if ((port->write_urb->status == -EINPROGRESS)) {
 727                /* Fifo hasn't been emptied since last write to this port */
 728                dbg("%s(): already writing, port->write_urb->status == "
 729                        "-EINPROGRESS", __func__);
 730                /* schedule_work(&port->work); commented in vendor driver */
 731                return 0;
 732        } else if (port_extra->tx_pending_bytes >= QT2_FIFO_DEPTH) {
 733                /* buffer is full (==). > should not occur, but would indicate
 734                 * that an overflow had occured */
 735                dbg("%s(): port transmit buffer is full!", __func__);
 736                /* schedule_work(&port->work); commented in vendor driver */
 737                return 0;
 738        }
 739
 740        /* We must fill the first 5 bytes of anything we sent with a transmit
 741         * header which directes the data to the correct port. The maximum
 742         * size we can send out in one URB is port->bulk_out_size, which caps
 743         * the number of bytes of real data we can send in each write. As the
 744         * semantics of write allow us to write less than we were give, we cap
 745         * the maximum we will ever write to the device as 5 bytes less than
 746         * one URB's worth, by reducing the value of the count argument
 747         * appropriately*/
 748        if (count > port->bulk_out_size - QT2_TX_HEADER_LENGTH) {
 749                count = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
 750                dbg("%s(): write request bigger than urb, only accepting "
 751                        "%d bytes", __func__, count);
 752        }
 753        /* we must also ensure that the FIFO at the other end can cope with the
 754         * URB we send it, otherwise it will have problems. As above, we can
 755         * restrict the write size by just shrinking count.*/
 756        if (count > (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes)) {
 757                count = QT2_FIFO_DEPTH - port_extra->tx_pending_bytes;
 758                dbg("%s(): not enough room in buffer, only accepting %d bytes",
 759                        __func__, count);
 760        }
 761        /* now build the header for transmission */
 762        header_array[0] = 0x1b;
 763        header_array[1] = 0x1b;
 764        header_array[2] = (__u8)port->number;
 765        header_array[3] = (__u8)count;
 766        header_array[4] = (__u8)count >> 8;
 767        /* copy header into URB */
 768        memcpy(port->write_urb->transfer_buffer, header_array,
 769                QT2_TX_HEADER_LENGTH);
 770        /* and actual data to write */
 771        memcpy(port->write_urb->transfer_buffer + 5, buf, count);
 772
 773        dbg("%s(): first data byte to send = %#.2x", __func__, *buf);
 774
 775        /* set up our urb */
 776        usb_fill_bulk_urb(port->write_urb, serial->dev,
 777                        usb_sndbulkpipe(serial->dev,
 778                        port->bulk_out_endpointAddress),
 779                        port->write_urb->transfer_buffer, count + 5,
 780                        (qt2_write_bulk_callback), port);
 781        /* send the data out the bulk port */
 782        result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
 783        if (result) {
 784                /* error couldn't submit urb */
 785                result = 0;     /* return 0 as nothing got written */
 786                dbg("%s(): failed submitting write urb, error %d",
 787                        __func__, result);
 788        } else {
 789                port_extra->tx_pending_bytes += count;
 790                result = count; /* return number of bytes written, i.e. count */
 791                dbg("%s(): submitted write urb, wrote %d bytes, "
 792                        "total pending bytes %d",
 793                        __func__, result, port_extra->tx_pending_bytes);
 794        }
 795        return result;
 796}
 797
 798/* This is used by the next layer up to know how much space is available
 799 * in the buffer on the device. It is used on a device closure to avoid
 800 * calling close() until the buffer is reported to be empty.
 801 * The returned value must never go down by more than the number of bytes
 802 * written for correct behaviour further up the driver stack, i.e. if I call
 803 * it, then write 6 bytes, then call again I should get 6 less, or possibly
 804 * only 5 less if one was written in the meantime, etc. I should never get 7
 805 * less (or any bigger number) because I only wrote 6 bytes.
 806 */
 807static int qt2_write_room(struct tty_struct *tty)
 808{
 809        struct usb_serial_port *port = tty->driver_data;
 810                /* parent usb_serial_port pointer */
 811        struct quatech2_port *port_extra;       /* extra data for this port */
 812        int room = 0;
 813        port_extra = qt2_get_port_private(port);
 814
 815        if (port_extra->close_pending == true) {
 816                dbg("%s(): port_extra->close_pending == true", __func__);
 817                return -ENODEV;
 818        }
 819        /* Q: how many bytes would a write() call actually succeed in writing
 820         * if it happened now?
 821         * A: one QT2_FIFO_DEPTH, less the number of bytes waiting to be sent
 822         * out of the port, unless this is more than the size of the
 823         * write_urb output buffer less the header, which is the maximum
 824         * size write we can do.
 825
 826         * Most of the implementation of this is done when writes to the device
 827         * are started or terminate. When we send a write to the device, we
 828         * reduce the free space count by the size of the dispatched write.
 829         * When a "transmit empty" message comes back up the USB read stream,
 830         * we decrement the count by the number of bytes reported sent, thus
 831         * keeping track of the difference between sent and recieved bytes.
 832         */
 833
 834        room = (QT2_FIFO_DEPTH - port_extra->tx_pending_bytes);
 835        /* space in FIFO */
 836        if (room > port->bulk_out_size - QT2_TX_HEADER_LENGTH)
 837                room = port->bulk_out_size - QT2_TX_HEADER_LENGTH;
 838        /* if more than the URB can hold, then cap to that limit */
 839
 840        dbg("%s(): port %d: write room is %d", __func__, port->number, room);
 841        return room;
 842}
 843
 844static int qt2_chars_in_buffer(struct tty_struct *tty)
 845{
 846        struct usb_serial_port *port = tty->driver_data;
 847        /* parent usb_serial_port pointer */
 848        struct quatech2_port *port_extra;       /* extra data for this port */
 849        port_extra = qt2_get_port_private(port);
 850
 851        dbg("%s(): port %d: chars_in_buffer = %d", __func__,
 852                port->number, port_extra->tx_pending_bytes);
 853        return port_extra->tx_pending_bytes;
 854}
 855
 856/* called when userspace does an ioctl() on the device. Note that
 857 * TIOCMGET and TIOCMSET are filtered off to their own methods before they get
 858 * here, so we don't have to handle them.
 859 */
 860static int qt2_ioctl(struct tty_struct *tty, struct file *file,
 861                     unsigned int cmd, unsigned long arg)
 862{
 863        struct usb_serial_port *port = tty->driver_data;
 864        struct usb_serial *serial = port->serial;
 865        __u8 mcr_value; /* Modem Control Register value */
 866        __u8 msr_value; /* Modem Status Register value */
 867        unsigned short prev_msr_value; /* Previous value of Modem Status
 868         * Register used to implement waiting for a line status change to
 869         * occur */
 870        struct quatech2_port *port_extra;       /* extra data for this port */
 871        DECLARE_WAITQUEUE(wait, current);
 872        /* Declare a wait queue named "wait" */
 873
 874        unsigned int value;
 875        unsigned int UartNumber;
 876
 877        if (serial == NULL)
 878                return -ENODEV;
 879        UartNumber = tty->index - serial->minor;
 880        port_extra = qt2_get_port_private(port);
 881
 882        dbg("%s(): port %d, UartNumber %d, tty =0x%p", __func__,
 883            port->number, UartNumber, tty);
 884
 885        if (cmd == TIOCMBIS || cmd == TIOCMBIC) {
 886                if (qt2_box_get_register(port->serial, UartNumber,
 887                        QT2_MODEM_CONTROL_REGISTER, &mcr_value) < 0)
 888                        return -ESPIPE;
 889                if (copy_from_user(&value, (unsigned int *)arg,
 890                        sizeof(value)))
 891                        return -EFAULT;
 892
 893                switch (cmd) {
 894                case TIOCMBIS:
 895                        if (value & TIOCM_RTS)
 896                                mcr_value |= QT2_SERIAL_MCR_RTS;
 897                        if (value & TIOCM_DTR)
 898                                mcr_value |= QT2_SERIAL_MCR_DTR;
 899                        if (value & TIOCM_LOOP)
 900                                mcr_value |= QT2_SERIAL_MCR_LOOP;
 901                break;
 902                case TIOCMBIC:
 903                        if (value & TIOCM_RTS)
 904                                mcr_value &= ~QT2_SERIAL_MCR_RTS;
 905                        if (value & TIOCM_DTR)
 906                                mcr_value &= ~QT2_SERIAL_MCR_DTR;
 907                        if (value & TIOCM_LOOP)
 908                                mcr_value &= ~QT2_SERIAL_MCR_LOOP;
 909                break;
 910                default:
 911                break;
 912                }       /* end of local switch on cmd */
 913                if (qt2_box_set_register(port->serial,  UartNumber,
 914                    QT2_MODEM_CONTROL_REGISTER, mcr_value) < 0) {
 915                        return -ESPIPE;
 916                } else {
 917                        port_extra->shadowMCR = mcr_value;
 918                        return 0;
 919                }
 920        } else if (cmd == TIOCMIWAIT) {
 921                dbg("%s() port %d, cmd == TIOCMIWAIT enter",
 922                        __func__, port->number);
 923                prev_msr_value = port_extra->shadowMSR  & QT2_SERIAL_MSR_MASK;
 924                while (1) {
 925                        add_wait_queue(&port_extra->wait, &wait);
 926                        set_current_state(TASK_INTERRUPTIBLE);
 927                        schedule();
 928                        dbg("%s(): port %d, cmd == TIOCMIWAIT here\n",
 929                                __func__, port->number);
 930                        remove_wait_queue(&port_extra->wait, &wait);
 931                        /* see if a signal woke us up */
 932                        if (signal_pending(current))
 933                                return -ERESTARTSYS;
 934                        msr_value = port_extra->shadowMSR & QT2_SERIAL_MSR_MASK;
 935                        if (msr_value == prev_msr_value)
 936                                return -EIO;  /* no change - error */
 937                        if ((arg & TIOCM_RNG &&
 938                                ((prev_msr_value & QT2_SERIAL_MSR_RI) ==
 939                                        (msr_value & QT2_SERIAL_MSR_RI))) ||
 940                                (arg & TIOCM_DSR &&
 941                                ((prev_msr_value & QT2_SERIAL_MSR_DSR) ==
 942                                        (msr_value & QT2_SERIAL_MSR_DSR))) ||
 943                                (arg & TIOCM_CD &&
 944                                ((prev_msr_value & QT2_SERIAL_MSR_CD) ==
 945                                        (msr_value & QT2_SERIAL_MSR_CD))) ||
 946                                (arg & TIOCM_CTS &&
 947                                ((prev_msr_value & QT2_SERIAL_MSR_CTS) ==
 948                                        (msr_value & QT2_SERIAL_MSR_CTS)))) {
 949                                return 0;
 950                        }
 951                } /* end inifinite while */
 952                /* FIXME: This while loop needs a way to break out if the device
 953                 * is disconnected while a process is waiting for the MSR to
 954                 * change, because once it's disconnected, it isn't going to
 955                 * change state ... */
 956        } else {
 957                /* any other ioctls we don't know about come here */
 958                dbg("%s(): No ioctl for that one. port = %d", __func__,
 959                        port->number);
 960                return -ENOIOCTLCMD;
 961        }
 962}
 963
 964/* Called when the user wishes to change the port settings using the termios
 965 * userspace interface */
 966static void qt2_set_termios(struct tty_struct *tty,
 967        struct usb_serial_port *port, struct ktermios *old_termios)
 968{
 969        struct usb_serial *serial; /* parent serial device */
 970        int baud, divisor, remainder;
 971        unsigned char LCR_change_to = 0;
 972        int status;
 973        __u16 UartNumber;
 974
 975        dbg("%s(): port %d", __func__, port->number);
 976
 977        serial = port->serial;
 978
 979        UartNumber = port->number;
 980
 981        if (old_termios && !tty_termios_hw_change(old_termios, tty->termios))
 982                return;
 983
 984        switch (tty->termios->c_cflag) {
 985        case CS5:
 986                LCR_change_to |= QT2_SERIAL_5_DATA;
 987                break;
 988        case CS6:
 989                LCR_change_to |= QT2_SERIAL_6_DATA;
 990                break;
 991        case CS7:
 992                LCR_change_to |= QT2_SERIAL_7_DATA;
 993                break;
 994        default:
 995        case CS8:
 996                LCR_change_to |= QT2_SERIAL_8_DATA;
 997                break;
 998        }
 999
1000        /* Parity stuff */
1001        if (tty->termios->c_cflag & PARENB) {
1002                if (tty->termios->c_cflag & PARODD)
1003                        LCR_change_to |= QT2_SERIAL_ODD_PARITY;
1004                else
1005                        LCR_change_to |= QT2_SERIAL_EVEN_PARITY;
1006        }
1007        /* Because LCR_change_to is initialised to zero, we don't have to worry
1008         * about the case where PARENB is not set or clearing bits, because by
1009         * default all of them are cleared, turning parity off.
1010         * as we don't support mark/space parity, we should clear the
1011         * mark/space parity bit in c_cflag, so the caller can tell we have
1012         * ignored the request */
1013        tty->termios->c_cflag &= ~CMSPAR;
1014
1015        if (tty->termios->c_cflag & CSTOPB)
1016                LCR_change_to |= QT2_SERIAL_TWO_STOPB;
1017        else
1018                LCR_change_to |= QT2_SERIAL_ONE_STOPB;
1019
1020        /* Thats the LCR stuff, next we need to work out the divisor as the
1021         * LCR and the divisor are set together */
1022        baud = tty_get_baud_rate(tty);
1023        if (!baud) {
1024                /* pick a default, any default... */
1025                baud = 9600;
1026        }
1027        dbg("%s(): got baud = %d", __func__, baud);
1028
1029        divisor = QT2_MAX_BAUD_RATE / baud;
1030        remainder = QT2_MAX_BAUD_RATE % baud;
1031        /* Round to nearest divisor */
1032        if (((remainder * 2) >= baud) && (baud != 110))
1033                divisor++;
1034        dbg("%s(): setting divisor = %d, QT2_MAX_BAUD_RATE = %d , LCR = %#.2x",
1035              __func__, divisor, QT2_MAX_BAUD_RATE, LCR_change_to);
1036
1037        status = qt2_boxsetuart(serial, UartNumber, (unsigned short) divisor,
1038                            LCR_change_to);
1039        if (status < 0) {
1040                dbg("qt2_boxsetuart() failed");
1041                return;
1042        } else {
1043                /* now encode the baud rate we actually set, which may be
1044                 * different to the request */
1045                baud = QT2_MAX_BAUD_RATE / divisor;
1046                tty_encode_baud_rate(tty, baud, baud);
1047        }
1048
1049        /* Now determine flow control */
1050        if (tty->termios->c_cflag & CRTSCTS) {
1051                dbg("%s(): Enabling HW flow control port %d", __func__,
1052                      port->number);
1053                /* Enable  RTS/CTS flow control */
1054                status = qt2_boxsethw_flowctl(serial, UartNumber, true);
1055                if (status < 0) {
1056                        dbg("qt2_boxsethw_flowctl() failed");
1057                        return;
1058                }
1059        } else {
1060                /* Disable RTS/CTS flow control */
1061                dbg("%s(): disabling HW flow control port %d", __func__,
1062                        port->number);
1063                status = qt2_boxsethw_flowctl(serial, UartNumber, false);
1064                if (status < 0) {
1065                        dbg("qt2_boxsethw_flowctl failed");
1066                        return;
1067                }
1068        }
1069        /* if we are implementing XON/XOFF, set the start and stop character
1070         * in the device */
1071        if (I_IXOFF(tty) || I_IXON(tty)) {
1072                unsigned char stop_char  = STOP_CHAR(tty);
1073                unsigned char start_char = START_CHAR(tty);
1074                status = qt2_boxsetsw_flowctl(serial, UartNumber, stop_char,
1075                                start_char);
1076                if (status < 0)
1077                        dbg("qt2_boxsetsw_flowctl (enabled) failed");
1078        } else {
1079                /* disable SW flow control */
1080                status = qt2_boxunsetsw_flowctl(serial, UartNumber);
1081                if (status < 0)
1082                        dbg("qt2_boxunsetsw_flowctl (disabling) failed");
1083        }
1084}
1085
1086static int qt2_tiocmget(struct tty_struct *tty, struct file *file)
1087{
1088        struct usb_serial_port *port = tty->driver_data;
1089        struct usb_serial *serial = port->serial;
1090
1091        __u8 mcr_value; /* Modem Control Register value */
1092        __u8 msr_value; /* Modem Status Register value */
1093        unsigned int result = 0;
1094        int status;
1095        unsigned int UartNumber;
1096
1097        if (serial == NULL)
1098                return -ENODEV;
1099
1100        dbg("%s(): port %d, tty =0x%p", __func__, port->number, tty);
1101        UartNumber = tty->index - serial->minor;
1102        dbg("UartNumber is %d", UartNumber);
1103
1104        status = qt2_box_get_register(port->serial, UartNumber,
1105                        QT2_MODEM_CONTROL_REGISTER,     &mcr_value);
1106        if (status >= 0) {
1107                status = qt2_box_get_register(port->serial,  UartNumber,
1108                                QT2_MODEM_STATUS_REGISTER, &msr_value);
1109        }
1110        if (status >= 0) {
1111                result = ((mcr_value & QT2_SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1112                                /*DTR set */
1113                        | ((mcr_value & QT2_SERIAL_MCR_RTS)  ? TIOCM_RTS : 0)
1114                                /*RTS set */
1115                        | ((msr_value & QT2_SERIAL_MSR_CTS)  ? TIOCM_CTS : 0)
1116                                /* CTS set */
1117                        | ((msr_value & QT2_SERIAL_MSR_CD)  ? TIOCM_CAR : 0)
1118                                /*Carrier detect set */
1119                        | ((msr_value & QT2_SERIAL_MSR_RI)  ? TIOCM_RI : 0)
1120                                /* Ring indicator set */
1121                        | ((msr_value & QT2_SERIAL_MSR_DSR)  ? TIOCM_DSR : 0);
1122                                /* DSR set */
1123                return result;
1124        } else {
1125                return -ESPIPE;
1126        }
1127}
1128
1129static int qt2_tiocmset(struct tty_struct *tty, struct file *file,
1130                       unsigned int set, unsigned int clear)
1131{
1132        struct usb_serial_port *port = tty->driver_data;
1133        struct usb_serial *serial = port->serial;
1134        __u8 mcr_value; /* Modem Control Register value */
1135        int status;
1136        unsigned int UartNumber;
1137
1138        if (serial == NULL)
1139                return -ENODEV;
1140
1141        UartNumber = tty->index - serial->minor;
1142        dbg("%s(): port %d, UartNumber %d", __func__, port->number, UartNumber);
1143
1144        status = qt2_box_get_register(port->serial, UartNumber,
1145                        QT2_MODEM_CONTROL_REGISTER, &mcr_value);
1146        if (status < 0)
1147                return -ESPIPE;
1148
1149        /* Turn off RTS, DTR and loopback, then only turn on what was asked
1150         * for */
1151        mcr_value &= ~(QT2_SERIAL_MCR_RTS | QT2_SERIAL_MCR_DTR |
1152                        QT2_SERIAL_MCR_LOOP);
1153        if (set & TIOCM_RTS)
1154                mcr_value |= QT2_SERIAL_MCR_RTS;
1155        if (set & TIOCM_DTR)
1156                mcr_value |= QT2_SERIAL_MCR_DTR;
1157        if (set & TIOCM_LOOP)
1158                mcr_value |= QT2_SERIAL_MCR_LOOP;
1159
1160        status = qt2_box_set_register(port->serial, UartNumber,
1161                        QT2_MODEM_CONTROL_REGISTER, mcr_value);
1162        if (status < 0)
1163                return -ESPIPE;
1164        else
1165                return 0;
1166}
1167
1168/** qt2_break - Turn BREAK on and off on the UARTs
1169 */
1170static void qt2_break(struct tty_struct *tty, int break_state)
1171{
1172        struct usb_serial_port *port = tty->driver_data; /* parent port */
1173        struct usb_serial *serial = port->serial;       /* parent device */
1174        struct quatech2_port *port_extra;       /* extra data for this port */
1175        __u16 break_value;
1176        unsigned int result;
1177
1178        port_extra = qt2_get_port_private(port);
1179        if (!serial) {
1180                dbg("%s(): port %d: no serial object", __func__, port->number);
1181                return;
1182        }
1183
1184        if (break_state == -1)
1185                break_value = 1;
1186        else
1187                break_value = 0;
1188        dbg("%s(): port %d, break_value %d", __func__, port->number,
1189                break_value);
1190
1191        mutex_lock(&port_extra->modelock);
1192        if (!port_extra->open_count) {
1193                dbg("%s(): port not open", __func__);
1194                goto exit;
1195        }
1196
1197        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1198                                QT2_BREAK_CONTROL, 0x40, break_value,
1199                                port->number, NULL, 0, 300);
1200exit:
1201        mutex_unlock(&port_extra->modelock);
1202        dbg("%s(): exit port %d", __func__, port->number);
1203
1204}
1205/**
1206 * qt2_throttle: - stop reading new data from the port
1207 */
1208static void qt2_throttle(struct tty_struct *tty)
1209{
1210        struct usb_serial_port *port = tty->driver_data;
1211        struct usb_serial *serial = port->serial;
1212        struct quatech2_port *port_extra;       /* extra data for this port */
1213        dbg("%s(): port %d", __func__, port->number);
1214
1215        port_extra = qt2_get_port_private(port);
1216        if (!serial) {
1217                dbg("%s(): enter port %d no serial object", __func__,
1218                      port->number);
1219                return;
1220        }
1221
1222        mutex_lock(&port_extra->modelock);      /* lock structure */
1223        if (!port_extra->open_count) {
1224                dbg("%s(): port not open", __func__);
1225                goto exit;
1226        }
1227        /* Send command to box to stop receiving stuff. This will stop this
1228         * particular UART from filling the endpoint - in the multiport case the
1229         * FPGA UART will handle any flow control implmented, but for the single
1230         * port it's handed differently and we just quit submitting urbs
1231         */
1232        if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100)
1233                qt2_boxstoprx(serial, port->number, 1);
1234
1235        port->throttled = 1;
1236exit:
1237        mutex_unlock(&port_extra->modelock);
1238        dbg("%s(): port %d: setting port->throttled", __func__, port->number);
1239        return;
1240}
1241
1242/**
1243 * qt2_unthrottle: - start receiving data through the port again after being
1244 * throttled
1245 */
1246static void qt2_unthrottle(struct tty_struct *tty)
1247{
1248        struct usb_serial_port *port = tty->driver_data;
1249        struct usb_serial *serial = port->serial;
1250        struct quatech2_port *port_extra;       /* extra data for this port */
1251        struct usb_serial_port *port0;  /* first port structure on device */
1252        struct quatech2_dev *dev_extra;         /* extra data for the device */
1253
1254        if (!serial) {
1255                dbg("%s() enter port %d no serial object!", __func__,
1256                        port->number);
1257                return;
1258        }
1259        dbg("%s(): enter port %d", __func__, port->number);
1260        dev_extra = qt2_get_dev_private(serial);
1261        port_extra = qt2_get_port_private(port);
1262        port0 = serial->port[0]; /* get the first port's device structure */
1263
1264        mutex_lock(&port_extra->modelock);
1265        if (!port_extra->open_count) {
1266                dbg("%s(): port %d not open", __func__, port->number);
1267                goto exit;
1268        }
1269
1270        if (port->throttled != 0) {
1271                dbg("%s(): port %d: unsetting port->throttled", __func__,
1272                    port->number);
1273                port->throttled = 0;
1274                /* Send command to box to start receiving stuff */
1275                if (serial->dev->descriptor.idProduct != QUATECH_SSU2_100) {
1276                        qt2_boxstoprx(serial,  port->number, 0);
1277                } else if (dev_extra->ReadBulkStopped == true) {
1278                        usb_fill_bulk_urb(port0->read_urb, serial->dev,
1279                                usb_rcvbulkpipe(serial->dev,
1280                                port0->bulk_in_endpointAddress),
1281                                port0->bulk_in_buffer,
1282                                port0->bulk_in_size,
1283                                qt2_read_bulk_callback,
1284                                serial);
1285                }
1286        }
1287exit:
1288        mutex_unlock(&port_extra->modelock);
1289        dbg("%s(): exit port %d", __func__, port->number);
1290        return;
1291}
1292
1293/* internal, private helper functions for the driver */
1294
1295/* Power up the FPGA in the box to get it working */
1296static int qt2_boxpoweron(struct usb_serial *serial)
1297{
1298        int result;
1299        __u8  Direcion;
1300        unsigned int pipe;
1301        Direcion = USBD_TRANSFER_DIRECTION_OUT;
1302        pipe = usb_rcvctrlpipe(serial->dev, 0);
1303        result = usb_control_msg(serial->dev, pipe, QT_SET_GET_DEVICE,
1304                                Direcion, QU2BOXPWRON, 0x00, NULL, 0x00,
1305                                5000);
1306        return result;
1307}
1308
1309/*
1310 * qt2_boxsetQMCR Issue a QT2_GET_SET_QMCR vendor-spcific request on the
1311 * default control pipe. If successful return the number of bytes written,
1312 * otherwise return a negative error number of the problem.
1313 */
1314static int qt2_boxsetQMCR(struct usb_serial *serial, __u16 Uart_Number,
1315                          __u8 QMCR_Value)
1316{
1317        int result;
1318        __u16 PortSettings;
1319
1320        PortSettings = (__u16)(QMCR_Value);
1321
1322        dbg("%s(): Port = %d, PortSettings = 0x%x", __func__,
1323                        Uart_Number, PortSettings);
1324
1325        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1326                                QT2_GET_SET_QMCR, 0x40, PortSettings,
1327                                (__u16)Uart_Number, NULL, 0, 5000);
1328        return result;
1329}
1330
1331static int port_paranoia_check(struct usb_serial_port *port,
1332                               const char *function)
1333{
1334        if (!port) {
1335                dbg("%s - port == NULL", function);
1336                return -1;
1337        }
1338        if (!port->serial) {
1339                dbg("%s - port->serial == NULL\n", function);
1340                return -1;
1341        }
1342        return 0;
1343}
1344
1345static int serial_paranoia_check(struct usb_serial *serial,
1346                                 const char *function)
1347{
1348        if (!serial) {
1349                dbg("%s - serial == NULL\n", function);
1350                return -1;
1351        }
1352
1353        if (!serial->type) {
1354                dbg("%s - serial->type == NULL!", function);
1355                return -1;
1356        }
1357
1358        return 0;
1359}
1360
1361static inline struct quatech2_port *qt2_get_port_private(struct usb_serial_port
1362                *port)
1363{
1364        return (struct quatech2_port *)usb_get_serial_port_data(port);
1365}
1366
1367static inline void qt2_set_port_private(struct usb_serial_port *port,
1368                struct quatech2_port *data)
1369{
1370        usb_set_serial_port_data(port, (void *)data);
1371}
1372
1373static inline struct quatech2_dev *qt2_get_dev_private(struct usb_serial
1374                *serial)
1375{
1376        return (struct quatech2_dev *)usb_get_serial_data(serial);
1377}
1378static inline void qt2_set_dev_private(struct usb_serial *serial,
1379                struct quatech2_dev *data)
1380{
1381        usb_set_serial_data(serial, (void *)data);
1382}
1383
1384static int qt2_openboxchannel(struct usb_serial *serial, __u16
1385                Uart_Number, struct qt2_status_data *status)
1386{
1387        int result;
1388        __u16 length;
1389        __u8  Direcion;
1390        unsigned int pipe;
1391        length = sizeof(struct qt2_status_data);
1392        Direcion = USBD_TRANSFER_DIRECTION_IN;
1393        pipe = usb_rcvctrlpipe(serial->dev, 0);
1394        result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1395                        Direcion, 0x00, Uart_Number, status, length, 5000);
1396        return result;
1397}
1398static int qt2_closeboxchannel(struct usb_serial *serial, __u16 Uart_Number)
1399{
1400        int result;
1401        __u8  direcion;
1402        unsigned int pipe;
1403        direcion = USBD_TRANSFER_DIRECTION_OUT;
1404        pipe = usb_sndctrlpipe(serial->dev, 0);
1405        result = usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL,
1406                  direcion, 0, Uart_Number, NULL, 0, 5000);
1407        return result;
1408}
1409
1410/* qt2_conf_uart Issue a SET_UART vendor-spcific request on the default
1411 * control pipe. If successful sets baud rate divisor and LCR value
1412 */
1413static int qt2_conf_uart(struct usb_serial *serial,  unsigned short Uart_Number,
1414                      unsigned short divisor, unsigned char LCR)
1415{
1416        int result;
1417        unsigned short UartNumandLCR;
1418
1419        UartNumandLCR = (LCR << 8) + Uart_Number;
1420
1421        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1422                                QT2_GET_SET_UART, 0x40, divisor, UartNumandLCR,
1423                                NULL, 0, 300);
1424        return result;
1425}
1426
1427/** @brief Callback for asynchronous submission of read URBs on bulk in
1428 * endpoints
1429 *
1430 * Registered in qt2_open_port(), used to deal with incomming data
1431 * from the box.
1432 */
1433static void qt2_read_bulk_callback(struct urb *urb)
1434{
1435        /* Get the device pointer (struct usb_serial) back out of the URB */
1436        struct usb_serial *serial = urb->context;
1437        /* get the extra struct for the device */
1438        struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1439        /* Get first port structure from the device */
1440        struct usb_serial_port *port0 = serial->port[0];
1441        /* Get the currently active port structure from serial struct */
1442        struct usb_serial_port *active = dev_extra->current_port;
1443        /* get the extra struct for port 0 */
1444        struct quatech2_port *port0_extra = qt2_get_port_private(port0);
1445        /* and for the currently active port */
1446        struct quatech2_port *active_extra = qt2_get_port_private(active);
1447        /* When we finally get to doing some tty stuff, we will need this */
1448        struct tty_struct *tty_st;
1449        unsigned int RxCount;   /* the length of the data to process */
1450        unsigned int i; /* loop counter over the data to process */
1451        int result;     /* return value cache variable */
1452        bool escapeflag;        /* flag set to true if this loop iteration is
1453                                 * parsing an escape sequence, rather than
1454                                 * ordinary data */
1455        dbg("%s(): callback running, active port is %d", __func__,
1456                active->number);
1457
1458        if (urb->status) {
1459                /* read didn't go well */
1460                dev_extra->ReadBulkStopped = true;
1461                dbg("%s(): nonzero bulk read status received: %d",
1462                        __func__, urb->status);
1463                return;
1464        }
1465
1466        /* inline port_sofrint() here */
1467        if (port_paranoia_check(port0, __func__) != 0) {
1468                dbg("%s - port_paranoia_check on port0 failed, exiting\n",
1469__func__);
1470                return;
1471        }
1472        if (port_paranoia_check(active, __func__) != 0) {
1473                dbg("%s - port_paranoia_check on current_port "
1474                        "failed, exiting", __func__);
1475                return;
1476        }
1477
1478/* This single callback function has to do for all the ports on
1479 * the device. Data being read up the USB can contain certain
1480 * escape sequences which are used to communicate out-of-band
1481 * information from the serial port in-band over the USB.
1482 * These escapes include sending modem and flow control line
1483 * status, and switching the port. The concept of a "Current Port"
1484 * is used, which is where data is going until a port change
1485 * escape seqence is received. This Current Port is kept between
1486 * callbacks so that when this function enters we know which the
1487 * currently active port is and can get to work right away without
1488 * the box having to send repeat escape sequences (anyway, how
1489 * would it know to do so?).
1490 */
1491
1492        if (active_extra->close_pending == true) {
1493                /* We are closing , stop reading */
1494                dbg("%s - (active->close_pending == true", __func__);
1495                if (dev_extra->open_ports <= 0) {
1496                        /* If this is the only port left open - stop the
1497                         * bulk read */
1498                        dev_extra->ReadBulkStopped = true;
1499                        dbg("%s - (ReadBulkStopped == true;", __func__);
1500                        return;
1501                }
1502        }
1503
1504        /*
1505         * RxHolding is asserted by throttle, if we assert it, we're not
1506         * receiving any more characters and let the box handle the flow
1507         * control
1508         */
1509        if ((port0_extra->RxHolding == true) &&
1510                    (serial->dev->descriptor.idProduct == QUATECH_SSU2_100)) {
1511                /* single port device, input is already stopped, so we don't
1512                 * need any more input data */
1513                dev_extra->ReadBulkStopped = true;
1514                return;
1515        }
1516        /* finally, we are in a situation where we might consider the data
1517         * that is contained within the URB, and what to do about it.
1518         * This is likely to involved communicating up to the TTY layer, so
1519         * we will need to get hold of the tty for the port we are currently
1520         * dealing with */
1521
1522        /* active is a usb_serial_port. It has a member port which is a
1523         * tty_port. From this we get a tty_struct pointer which is what we
1524         * actually wanted, and keep it on tty_st */
1525        tty_st = tty_port_tty_get(&active->port);
1526        if (!tty_st) {
1527                dbg("%s - bad tty pointer - exiting", __func__);
1528                return;
1529        }
1530        RxCount = urb->actual_length;   /* grab length of data handy */
1531
1532        if (RxCount) {
1533                /* skip all this if no data to process */
1534                for (i = 0; i < RxCount ; ++i) {
1535                        /* Look ahead code here -works on several bytes at onc*/
1536                        if ((i <= (RxCount - 3)) && (THISCHAR == 0x1b)
1537                                && (NEXTCHAR == 0x1b)) {
1538                                /* we are in an escape sequence, type
1539                                 * determined by the 3rd char */
1540                                escapeflag = false;
1541                                switch (THIRDCHAR) {
1542                                case 0x00:
1543                                        /* Line status change 4th byte must
1544                                         * follow */
1545                                        if (i > (RxCount - 4)) {
1546                                                dbg("Illegal escape sequences "
1547                                                "in received data");
1548                                                break;
1549                                        }
1550                                        qt2_process_line_status(active,
1551                                                FOURTHCHAR);
1552                                        i += 3;
1553                                        escapeflag = true;
1554                                        break;
1555                                case 0x01:
1556                                        /* Modem status status change 4th byte
1557                                         * must follow */
1558                                        if (i > (RxCount - 4)) {
1559                                                dbg("Illegal escape sequences "
1560                                                "in received data");
1561                                                break;
1562                                        }
1563                                        qt2_process_modem_status(active,
1564                                                FOURTHCHAR);
1565                                        i += 3;
1566                                        escapeflag = true;
1567                                        break;
1568                                case 0x02:
1569                                        /* xmit hold empty 4th byte
1570                                         * must follow */
1571                                        if (i > (RxCount - 4)) {
1572                                                dbg("Illegal escape sequences "
1573                                                "in received data");
1574                                                break;
1575                                        }
1576                                        qt2_process_xmit_empty(active,
1577                                                FOURTHCHAR, FIFTHCHAR);
1578                                        i += 4;
1579                                        escapeflag = true;
1580                                        break;
1581                                case 0x03:
1582                                        /* Port number change 4th byte
1583                                         * must follow */
1584                                        if (i > (RxCount - 4)) {
1585                                                dbg("Illegal escape sequences "
1586                                                "in received data");
1587                                                break;
1588                                        }
1589                                        /* Port change. If port open push
1590                                         * current data up to tty layer */
1591                                        if (active_extra->open_count > 0)
1592                                                tty_flip_buffer_push(tty_st);
1593
1594                                        dbg("Port Change: new port = %d",
1595                                                FOURTHCHAR);
1596                                        qt2_process_port_change(active,
1597                                                FOURTHCHAR);
1598                                        i += 3;
1599                                        escapeflag = true;
1600                                        /* having changed port, the pointers for
1601                                         * the currently active port are all out
1602                                         * of date and need updating */
1603                                        active = dev_extra->current_port;
1604                                        active_extra =
1605                                                qt2_get_port_private(active);
1606                                        tty_st = tty_port_tty_get(
1607                                                &active->port);
1608                                        break;
1609                                case 0x04:
1610                                        /* Recv flush 3rd byte must
1611                                         * follow */
1612                                        if (i > (RxCount - 3)) {
1613                                                dbg("Illegal escape sequences "
1614                                                        "in received data");
1615                                                break;
1616                                        }
1617                                        qt2_process_rcv_flush(active);
1618                                        i += 2;
1619                                        escapeflag = true;
1620                                        break;
1621                                case 0x05:
1622                                        /* xmit flush 3rd byte must follow */
1623                                        if (i > (RxCount - 3)) {
1624                                                dbg("Illegal escape sequences "
1625                                                "in received data");
1626                                                break;
1627                                        }
1628                                        qt2_process_xmit_flush(active);
1629                                        i += 2;
1630                                        escapeflag = true;
1631                                        break;
1632                                case 0xff:
1633                                        dbg("No status sequence");
1634                                        qt2_process_rx_char(active, THISCHAR);
1635                                        qt2_process_rx_char(active, NEXTCHAR);
1636                                        i += 2;
1637                                        break;
1638                                default:
1639                                        qt2_process_rx_char(active, THISCHAR);
1640                                        i += 1;
1641                                        break;
1642                                } /*end switch*/
1643                                if (escapeflag == true)
1644                                        continue;
1645                                /* if we did an escape char, we don't need
1646                                 * to mess around pushing data through the
1647                                 * tty layer, and can go round again */
1648                        } /*endif*/
1649                        if (tty_st && urb->actual_length) {
1650                                tty_buffer_request_room(tty_st, 1);
1651                                tty_insert_flip_string(tty_st,
1652                                        &((unsigned char *)(urb->transfer_buffer)
1653                                                )[i],
1654                                        1);
1655                        }
1656                } /*endfor*/
1657                tty_flip_buffer_push(tty_st);
1658        } /*endif*/
1659
1660        /* at this point we have complete dealing with the data for this
1661         * callback. All we have to do now is to start the async read process
1662         * back off again. */
1663
1664        usb_fill_bulk_urb(port0->read_urb, serial->dev,
1665                usb_rcvbulkpipe(serial->dev, port0->bulk_in_endpointAddress),
1666                port0->bulk_in_buffer, port0->bulk_in_size,
1667                qt2_read_bulk_callback, serial);
1668        result = usb_submit_urb(port0->read_urb, GFP_ATOMIC);
1669        if (result) {
1670                dbg("%s(): failed resubmitting read urb, error %d",
1671                        __func__, result);
1672        } else {
1673                dbg("%s() sucessfully resumitted read urb", __func__);
1674                if (tty_st && RxCount) {
1675                        /* if some inbound data was processed, then
1676                         * we need to push that through the tty layer
1677                         */
1678                        tty_flip_buffer_push(tty_st);
1679                        tty_schedule_flip(tty_st);
1680                }
1681        }
1682
1683        /* cribbed from serqt_usb2 driver, but not sure which work needs
1684         * scheduling - port0 or currently active port? */
1685        /* schedule_work(&port->work); */
1686        dbg("%s() completed", __func__);
1687        return;
1688}
1689
1690/** @brief Callback for asynchronous submission of write URBs on bulk in
1691 * endpoints
1692 *
1693 * Registered in qt2_write(), used to deal with outgoing data
1694 * to the box.
1695 */
1696static void qt2_write_bulk_callback(struct urb *urb)
1697{
1698        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1699        struct usb_serial *serial = port->serial;
1700        dbg("%s(): port %d", __func__, port->number);
1701        if (!serial) {
1702                dbg("%s(): bad serial pointer, exiting", __func__);
1703                return;
1704        }
1705        if (urb->status) {
1706                dbg("%s(): nonzero write bulk status received: %d",
1707                        __func__, urb->status);
1708                return;
1709        }
1710        /* FIXME What is supposed to be going on here?
1711         * does this actually do anything useful, and should it?
1712         */
1713        /*port_softint((void *) serial); commented in vendor driver */
1714        schedule_work(&port->work);
1715        dbg("%s(): port %d exit", __func__, port->number);
1716        return;
1717}
1718
1719static void qt2_process_line_status(struct usb_serial_port *port,
1720        unsigned char LineStatus)
1721{
1722        /* obtain the private structure for the port */
1723        struct quatech2_port *port_extra = qt2_get_port_private(port);
1724        port_extra->shadowLSR = LineStatus & (QT2_SERIAL_LSR_OE |
1725                QT2_SERIAL_LSR_PE | QT2_SERIAL_LSR_FE | QT2_SERIAL_LSR_BI);
1726}
1727static void qt2_process_modem_status(struct usb_serial_port *port,
1728        unsigned char ModemStatus)
1729{
1730        /* obtain the private structure for the port */
1731        struct quatech2_port *port_extra = qt2_get_port_private(port);
1732        port_extra->shadowMSR = ModemStatus;
1733        wake_up_interruptible(&port_extra->wait);
1734        /* this wakes up the otherwise indefinitely waiting code for
1735         * the TIOCMIWAIT ioctl, so that it can notice that
1736         * port_extra->shadowMSR has changed and the ioctl needs to return.
1737         */
1738}
1739
1740static void qt2_process_xmit_empty(struct usb_serial_port *port,
1741        unsigned char fourth_char, unsigned char fifth_char)
1742{
1743        int byte_count;
1744        /* obtain the private structure for the port */
1745        struct quatech2_port *port_extra = qt2_get_port_private(port);
1746
1747        byte_count = (int)(fifth_char * 16);
1748        byte_count +=  (int)fourth_char;
1749        /* byte_count indicates how many bytes the device has written out. This
1750         * message appears to occur regularly, and is used in the vendor driver
1751         * to keep track of the fill state of the port transmit buffer */
1752        port_extra->tx_pending_bytes -= byte_count;
1753        /* reduce the stored data queue length by the known number of bytes
1754         * sent */
1755        dbg("port %d: %d bytes reported sent, %d still pending", port->number,
1756                        byte_count, port_extra->tx_pending_bytes);
1757
1758        /*port_extra->xmit_fifo_room_bytes = FIFO_DEPTH; ???*/
1759}
1760
1761static void qt2_process_port_change(struct usb_serial_port *port,
1762        unsigned char New_Current_Port)
1763{
1764        /* obtain the parent usb serial device structure */
1765        struct usb_serial *serial = port->serial;
1766        /* obtain the private structure for the device */
1767        struct quatech2_dev *dev_extra = qt2_get_dev_private(serial);
1768        dev_extra->current_port = serial->port[New_Current_Port];
1769        /* what should I do with this? commented out in upstream
1770         * driver */
1771        /*schedule_work(&port->work);*/
1772}
1773
1774static void qt2_process_rcv_flush(struct usb_serial_port *port)
1775{
1776        /* obtain the private structure for the port */
1777        struct quatech2_port *port_extra = qt2_get_port_private(port);
1778        port_extra->rcv_flush = true;
1779}
1780static void qt2_process_xmit_flush(struct usb_serial_port *port)
1781{
1782        /* obtain the private structure for the port */
1783        struct quatech2_port *port_extra = qt2_get_port_private(port);
1784        port_extra->xmit_flush = true;
1785}
1786
1787static void qt2_process_rx_char(struct usb_serial_port *port,
1788        unsigned char data)
1789{
1790        /* get the tty_struct for this port */
1791        struct tty_struct *tty = tty_port_tty_get(&(port->port));
1792        /* get the URB with the data in to push */
1793        struct urb *urb = port->serial->port[0]->read_urb;
1794
1795        if (tty && urb->actual_length) {
1796                tty_buffer_request_room(tty, 1);
1797                tty_insert_flip_string(tty, &data, 1);
1798                /* should this be commented out here? */
1799                /*tty_flip_buffer_push(tty);*/
1800        }
1801}
1802
1803/** @brief Retreive the value of a register from the device
1804 *
1805 * Issues a GET_REGISTER vendor-spcific request over the USB control
1806 * pipe to obtain a value back from a specific register on a specific
1807 * UART
1808 * @param serial Serial device handle to access the device through
1809 * @param uart_number Which UART the value is wanted from
1810 * @param register_num Which register to read the value from
1811 * @param pValue Pointer to somewhere to put the retrieved value
1812 */
1813static int qt2_box_get_register(struct usb_serial *serial,
1814                unsigned char uart_number, unsigned short register_num,
1815                __u8 *pValue)
1816{
1817        int result;
1818        result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1819                        QT2_GET_SET_REGISTER, 0xC0, register_num,
1820                        uart_number, (void *)pValue, sizeof(*pValue), 300);
1821        return result;
1822}
1823
1824/** qt2_box_set_register
1825 * Issue a SET_REGISTER vendor-specific request on the default control pipe
1826 */
1827static int qt2_box_set_register(struct usb_serial *serial,
1828                unsigned short Uart_Number, unsigned short Register_Num,
1829                unsigned short Value)
1830{
1831        int result;
1832        unsigned short reg_and_byte;
1833
1834        reg_and_byte = Value;
1835        reg_and_byte = reg_and_byte << 8;
1836        reg_and_byte = reg_and_byte + Register_Num;
1837
1838        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1839                        QT2_GET_SET_REGISTER, 0x40, reg_and_byte,
1840                        Uart_Number, NULL, 0, 300);
1841        return result;
1842}
1843
1844
1845/** @brief Request the Tx or Rx buffers on the USB side be flushed
1846 *
1847 * Tx flush: When all the currently buffered data has been sent, send an escape
1848 * sequence back up the data stream to us
1849 * Rx flush: add a flag in the data stream now so we know when it's made it's
1850 * way up to us.
1851 */
1852static int qt2_box_flush(struct usb_serial *serial,  unsigned char uart_number,
1853                    unsigned short rcv_or_xmit)
1854{
1855        int result;
1856        result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1857                QT2_FLUSH_DEVICE, 0x40, rcv_or_xmit, uart_number, NULL, 0,
1858                300);
1859        return result;
1860}
1861
1862/** qt2_boxsetuart - Issue a SET_UART vendor-spcific request on the default
1863 * control pipe. If successful sets baud rate divisor and LCR value.
1864 */
1865static int qt2_boxsetuart(struct usb_serial *serial, unsigned short Uart_Number,
1866                unsigned short default_divisor, unsigned char default_LCR)
1867{
1868        unsigned short UartNumandLCR;
1869
1870        UartNumandLCR = (default_LCR << 8) + Uart_Number;
1871
1872        return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1873                        QT2_GET_SET_UART, 0x40, default_divisor, UartNumandLCR,
1874                        NULL, 0, 300);
1875}
1876/** qt2_boxsethw_flowctl - Turn hardware (RTS/CTS) flow control on and off for
1877 * a hardware UART.
1878 */
1879static int qt2_boxsethw_flowctl(struct usb_serial *serial,
1880                unsigned int UartNumber, bool bSet)
1881{
1882        __u8 MCR_Value = 0;
1883        __u8 MSR_Value = 0;
1884        __u16 MOUT_Value = 0;
1885
1886        if (bSet == true) {
1887                MCR_Value =  QT2_SERIAL_MCR_RTS;
1888                /* flow control, box will clear RTS line to prevent remote
1889                 * device from transmitting more chars */
1890        } else {
1891                /* no flow control to remote device */
1892                MCR_Value =  0;
1893        }
1894        MOUT_Value = MCR_Value << 8;
1895
1896        if (bSet == true) {
1897                MSR_Value = QT2_SERIAL_MSR_CTS;
1898                /* flow control on, box will inhibit tx data if CTS line is
1899                 * asserted */
1900        } else {
1901                /* Box will not inhibit tx data due to CTS line */
1902                MSR_Value = 0;
1903        }
1904        MOUT_Value |= MSR_Value;
1905        return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1906                        QT2_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, UartNumber,
1907                        NULL, 0, 300);
1908}
1909
1910/** qt2_boxsetsw_flowctl - Turn software (XON/XOFF) flow control on for
1911 * a hardware UART, and set the XON and XOFF characters.
1912 */
1913static int qt2_boxsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber,
1914                        unsigned char stop_char,  unsigned char start_char)
1915{
1916        __u16 nSWflowout;
1917
1918        nSWflowout = start_char << 8;
1919        nSWflowout = (unsigned short)stop_char;
1920        return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1921                        QT2_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, UartNumber,
1922                        NULL, 0, 300);
1923}
1924
1925/** qt2_boxunsetsw_flowctl - Turn software (XON/XOFF) flow control off for
1926 * a hardware UART.
1927 */
1928static int qt2_boxunsetsw_flowctl(struct usb_serial *serial, __u16 UartNumber)
1929{
1930        return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1931                        QT2_SW_FLOW_CONTROL_DISABLE, 0x40, 0, UartNumber, NULL,
1932                        0, 300);
1933}
1934
1935/**
1936 * qt2_boxstoprx - Start and stop reception of data by the FPGA UART in
1937 * response to requests from the tty layer
1938 * @serial: pointer to the usb_serial structure for the parent device
1939 * @uart_number: which UART on the device we are addressing
1940 * @stop: Whether to start or stop data reception. Set to 1 to stop data being
1941 * received, and to 0 to start it being received.
1942 */
1943static int qt2_boxstoprx(struct usb_serial *serial, unsigned short uart_number,
1944                unsigned short stop)
1945{
1946        return usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1947                QT2_STOP_RECEIVE, 0x40, stop, uart_number, NULL, 0, 300);
1948}
1949
1950
1951/*
1952 * last things in file: stuff to register this driver into the generic
1953 * USB serial framework.
1954 */
1955
1956static struct usb_serial_driver quatech2_device = {
1957        .driver = {
1958                .owner = THIS_MODULE,
1959                .name = "quatech_usb2",
1960        },
1961        .description = DRIVER_DESC,
1962        .usb_driver = &quausb2_usb_driver,
1963        .id_table = quausb2_id_table,
1964        .num_ports = 8,
1965        .open = qt2_open,
1966        .close = qt2_close,
1967        .write = qt2_write,
1968        .write_room = qt2_write_room,
1969        .chars_in_buffer = qt2_chars_in_buffer,
1970        .throttle = qt2_throttle,
1971        .unthrottle = qt2_unthrottle,
1972        .calc_num_ports = qt2_calc_num_ports,
1973        .ioctl = qt2_ioctl,
1974        .set_termios = qt2_set_termios,
1975        .break_ctl = qt2_break,
1976        .tiocmget = qt2_tiocmget,
1977        .tiocmset = qt2_tiocmset,
1978        .attach = qt2_attach,
1979        .release = qt2_release,
1980        .read_bulk_callback = qt2_read_bulk_callback,
1981        .write_bulk_callback = qt2_write_bulk_callback,
1982};
1983
1984static int __init quausb2_usb_init(void)
1985{
1986        int retval;
1987
1988        dbg("%s\n", __func__);
1989
1990        /* register with usb-serial */
1991        retval = usb_serial_register(&quatech2_device);
1992
1993        if (retval)
1994                goto failed_usb_serial_register;
1995
1996        printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1997                        DRIVER_DESC "\n");
1998
1999        /* register with usb */
2000
2001        retval = usb_register(&quausb2_usb_driver);
2002        if (retval == 0)
2003                return 0;
2004
2005        /* if we're here, usb_register() failed */
2006        usb_serial_deregister(&quatech2_device);
2007failed_usb_serial_register:
2008                return retval;
2009}
2010
2011static void __exit quausb2_usb_exit(void)
2012{
2013        usb_deregister(&quausb2_usb_driver);
2014        usb_serial_deregister(&quatech2_device);
2015}
2016
2017module_init(quausb2_usb_init);
2018module_exit(quausb2_usb_exit);
2019
2020MODULE_AUTHOR(DRIVER_AUTHOR);
2021MODULE_DESCRIPTION(DRIVER_DESC);
2022MODULE_LICENSE("GPL");
2023
2024module_param(debug, bool, S_IRUGO | S_IWUSR);
2025MODULE_PARM_DESC(debug, "Debug enabled or not");
2026