linux/drivers/staging/serqt_usb2/serqt_usb2.c
<<
>>
Prefs
   1/*
   2 * This code was developed for the Quatech USB line for linux, it used
   3 * much of the code developed by Greg Kroah-Hartman for USB serial devices
   4 *
   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 bool debug;
  20
  21/* Version Information */
  22#define DRIVER_VERSION "v2.14"
  23#define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
  24#define DRIVER_DESC "Quatech USB to Serial Driver"
  25
  26#define USB_VENDOR_ID_QUATECH                   0x061d  /* Quatech VID */
  27#define QUATECH_SSU200  0xC030  /* SSU200 */
  28#define QUATECH_DSU100  0xC040  /* DSU100 */
  29#define QUATECH_DSU200  0xC050  /* DSU200 */
  30#define QUATECH_QSU100  0xC060  /* QSU100 */
  31#define QUATECH_QSU200  0xC070  /* QSU200 */
  32#define QUATECH_ESU100A 0xC080  /* ESU100A */
  33#define QUATECH_ESU100B 0xC081  /* ESU100B */
  34#define QUATECH_ESU200A 0xC0A0  /* ESU200A */
  35#define QUATECH_ESU200B 0xC0A1  /* ESU200B */
  36#define QUATECH_HSU100A 0xC090  /* HSU100A */
  37#define QUATECH_HSU100B 0xC091  /* HSU100B */
  38#define QUATECH_HSU100C 0xC092  /* HSU100C */
  39#define QUATECH_HSU100D 0xC093  /* HSU100D */
  40#define QUATECH_HSU200A 0xC0B0  /* HSU200A */
  41#define QUATECH_HSU200B 0xC0B1  /* HSU200B */
  42#define QUATECH_HSU200C 0xC0B2  /* HSU200C */
  43#define QUATECH_HSU200D 0xC0B3  /* HSU200D */
  44#define QUATECH_SSU100_2  0xC120        /* SSU100_2 */
  45#define QUATECH_DSU100_2  0xC140        /* DSU100_2 */
  46#define QUATECH_DSU400_2  0xC150        /* DSU400_2 */
  47#define QUATECH_QSU100_2  0xC160        /* QSU100_2 */
  48#define QUATECH_QSU400_2  0xC170        /* QSU400_2 */
  49#define QUATECH_ESU400_2  0xC180        /* ESU400_2 */
  50#define QUATECH_ESU100_2  0xC1A0        /* ESU100_2 */
  51
  52#define QT_SET_GET_DEVICE           0xc2
  53#define QT_OPEN_CLOSE_CHANNEL       0xca
  54#define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
  55#define QT_SET_ATF                  0xcd
  56#define QT_GET_SET_REGISTER         0xc0
  57#define QT_GET_SET_UART             0xc1
  58#define QT_HW_FLOW_CONTROL_MASK     0xc5
  59#define QT_SW_FLOW_CONTROL_MASK     0xc6
  60#define QT_SW_FLOW_CONTROL_DISABLE  0xc7
  61#define QT_BREAK_CONTROL            0xc8
  62
  63#define USBD_TRANSFER_DIRECTION_IN    0xc0
  64#define USBD_TRANSFER_DIRECTION_OUT   0x40
  65
  66#define  MAX_BAUD_RATE              460800
  67#define  MAX_BAUD_REMAINDER         4608
  68
  69#define  DIV_LATCH_LS               0x00
  70#define  XMT_HOLD_REGISTER          0x00
  71#define  XVR_BUFFER_REGISTER        0x00
  72#define  DIV_LATCH_MS               0x01
  73#define  FIFO_CONTROL_REGISTER      0x02
  74#define  LINE_CONTROL_REGISTER      0x03
  75#define  MODEM_CONTROL_REGISTER     0x04
  76#define  LINE_STATUS_REGISTER       0x05
  77#define  MODEM_STATUS_REGISTER      0x06
  78
  79#define  SERIAL_MCR_DTR             0x01
  80#define  SERIAL_MCR_RTS             0x02
  81#define  SERIAL_MCR_LOOP            0x10
  82
  83#define  SERIAL_MSR_CTS             0x10
  84#define  SERIAL_MSR_CD              0x80
  85#define  SERIAL_MSR_RI              0x40
  86#define  SERIAL_MSR_DSR             0x20
  87#define  SERIAL_MSR_MASK            0xf0
  88
  89#define  SERIAL_8_DATA              0x03
  90#define  SERIAL_7_DATA              0x02
  91#define  SERIAL_6_DATA              0x01
  92#define  SERIAL_5_DATA              0x00
  93
  94#define  SERIAL_ODD_PARITY          0X08
  95#define  SERIAL_EVEN_PARITY         0X18
  96#define  SERIAL_TWO_STOPB           0x04
  97#define  SERIAL_ONE_STOPB           0x00
  98
  99#define DEFAULT_DIVISOR  0x30   /* gives 9600 baud rate */
 100#define DEFAULT_LCR SERIAL_8_DATA       /* 8, none , 1 */
 101
 102#define FULLPWRBIT          0x00000080
 103#define NEXT_BOARD_POWER_BIT        0x00000004
 104
 105#define SERIAL_LSR_OE       0x02
 106#define SERIAL_LSR_PE       0x04
 107#define SERIAL_LSR_FE       0x08
 108#define SERIAL_LSR_BI       0x10
 109
 110#define  SERIAL_MSR_CTS             0x10
 111#define  SERIAL_MSR_CD              0x80
 112#define  SERIAL_MSR_RI              0x40
 113#define  SERIAL_MSR_DSR             0x20
 114#define  SERIAL_MSR_MASK            0xf0
 115
 116#define PREFUFF_LEVEL_CONSERVATIVE  128
 117#define ATC_DISABLED                0x0
 118
 119#define RR_BITS             0x03        /* for clearing clock bits */
 120#define DUPMODE_BITS        0xc0
 121#define CLKS_X4             0x02
 122
 123#define LOOPMODE_BITS       0x41        /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
 124#define ALL_LOOPBACK        0x01
 125#define MODEM_CTRL          0x40
 126#define RS232_MODE          0x00
 127
 128static const struct usb_device_id serqt_id_table[] = {
 129        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)},
 130        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)},
 131        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)},
 132        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)},
 133        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)},
 134        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)},
 135        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)},
 136        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)},
 137        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)},
 138        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)},
 139        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)},
 140        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)},
 141        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)},
 142        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)},
 143        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)},
 144        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)},
 145        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)},
 146        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100_2)},
 147        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100_2)},
 148        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU400_2)},
 149        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100_2)},
 150        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU400_2)},
 151        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU400_2)},
 152        {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100_2)},
 153        {}                      /* Terminating entry */
 154};
 155
 156MODULE_DEVICE_TABLE(usb, serqt_id_table);
 157
 158struct qt_get_device_data {
 159        __u8 porta;
 160        __u8 portb;
 161        __u8 portc;
 162};
 163
 164struct qt_open_channel_data {
 165        __u8 line_status;
 166        __u8 modem_status;
 167};
 168
 169struct quatech_port {
 170        int port_num;           /* number of the port */
 171        struct urb *write_urb;  /* write URB for this port */
 172        struct urb *read_urb;   /* read URB for this port */
 173        struct urb *int_urb;
 174
 175        __u8 shadowLCR;         /* last LCR value received */
 176        __u8 shadowMCR;         /* last MCR value received */
 177        __u8 shadowMSR;         /* last MSR value received */
 178        __u8 shadowLSR;         /* last LSR value received */
 179        char open_ports;
 180
 181        /* Used for TIOCMIWAIT */
 182        wait_queue_head_t msr_wait;
 183        char prev_status, diff_status;
 184
 185        wait_queue_head_t wait;
 186
 187        struct async_icount icount;
 188
 189        struct usb_serial_port *port;   /* owner of this object */
 190        struct qt_get_device_data DeviceData;
 191        struct mutex lock;
 192        bool read_urb_busy;
 193        int RxHolding;
 194        int ReadBulkStopped;
 195        char closePending;
 196};
 197
 198static struct usb_driver serqt_usb_driver = {
 199        .name = "quatech-usb-serial",
 200        .probe = usb_serial_probe,
 201        .disconnect = usb_serial_disconnect,
 202        .id_table = serqt_id_table,
 203};
 204
 205static int port_paranoia_check(struct usb_serial_port *port,
 206                               const char *function)
 207{
 208        if (!port) {
 209                dbg("%s - port == NULL", function);
 210                return -1;
 211        }
 212        if (!port->serial) {
 213                dbg("%s - port->serial == NULL\n", function);
 214                return -1;
 215        }
 216
 217        return 0;
 218}
 219
 220static int serial_paranoia_check(struct usb_serial *serial,
 221                                 const char *function)
 222{
 223        if (!serial) {
 224                dbg("%s - serial == NULL\n", function);
 225                return -1;
 226        }
 227
 228        if (!serial->type) {
 229                dbg("%s - serial->type == NULL!", function);
 230                return -1;
 231        }
 232
 233        return 0;
 234}
 235
 236static inline struct quatech_port *qt_get_port_private(struct usb_serial_port
 237                                                       *port)
 238{
 239        return (struct quatech_port *)usb_get_serial_port_data(port);
 240}
 241
 242static inline void qt_set_port_private(struct usb_serial_port *port,
 243                                       struct quatech_port *data)
 244{
 245        usb_set_serial_port_data(port, (void *)data);
 246}
 247
 248static struct usb_serial *get_usb_serial(struct usb_serial_port *port,
 249                                         const char *function)
 250{
 251        /* if no port was specified, or it fails a paranoia check */
 252        if (!port ||
 253            port_paranoia_check(port, function) ||
 254            serial_paranoia_check(port->serial, function)) {
 255                /*
 256                 * then say that we dont have a valid usb_serial thing,
 257                 * which will end up genrating -ENODEV return values
 258                 */
 259                return NULL;
 260        }
 261
 262        return port->serial;
 263}
 264
 265static void ProcessLineStatus(struct quatech_port *qt_port,
 266                              unsigned char line_status)
 267{
 268
 269        qt_port->shadowLSR =
 270            line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE |
 271                           SERIAL_LSR_BI);
 272        return;
 273}
 274
 275static void ProcessModemStatus(struct quatech_port *qt_port,
 276                               unsigned char modem_status)
 277{
 278
 279        qt_port->shadowMSR = modem_status;
 280        wake_up_interruptible(&qt_port->wait);
 281        return;
 282}
 283
 284static void ProcessRxChar(struct tty_struct *tty, struct usb_serial_port *port,
 285                                                unsigned char data)
 286{
 287        struct urb *urb = port->read_urb;
 288        if (urb->actual_length)
 289                tty_insert_flip_char(tty, data, TTY_NORMAL);
 290}
 291
 292static void qt_write_bulk_callback(struct urb *urb)
 293{
 294        struct tty_struct *tty;
 295        int status;
 296        struct quatech_port *quatech_port;
 297
 298        status = urb->status;
 299
 300        if (status) {
 301                dbg("nonzero write bulk status received:%d\n", status);
 302                return;
 303        }
 304
 305        quatech_port = urb->context;
 306
 307        dbg("%s - port %d\n", __func__, quatech_port->port_num);
 308
 309        tty = tty_port_tty_get(&quatech_port->port->port);
 310
 311        if (tty)
 312                tty_wakeup(tty);
 313        tty_kref_put(tty);
 314}
 315
 316static void qt_interrupt_callback(struct urb *urb)
 317{
 318        /* FIXME */
 319}
 320
 321static void qt_read_bulk_callback(struct urb *urb)
 322{
 323
 324        struct usb_serial_port *port = urb->context;
 325        struct usb_serial *serial = get_usb_serial(port, __func__);
 326        struct quatech_port *qt_port = qt_get_port_private(port);
 327        unsigned char *data;
 328        struct tty_struct *tty;
 329        unsigned int index;
 330        unsigned int RxCount;
 331        int i, result;
 332        int flag, flag_data;
 333
 334        if (urb->status) {
 335                qt_port->ReadBulkStopped = 1;
 336                dbg("%s - nonzero write bulk status received: %d\n",
 337                    __func__, urb->status);
 338                return;
 339        }
 340
 341        tty = tty_port_tty_get(&port->port);
 342        if (!tty) {
 343                dbg("%s - bad tty pointer - exiting", __func__);
 344                return;
 345        }
 346
 347        data = urb->transfer_buffer;
 348
 349        RxCount = urb->actual_length;
 350
 351        /* index = MINOR(port->tty->device) - serial->minor; */
 352        index = tty->index - serial->minor;
 353
 354        dbg("%s - port %d\n", __func__, port->number);
 355        dbg("%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
 356
 357        if (port_paranoia_check(port, __func__) != 0) {
 358                dbg("%s - port_paranoia_check, exiting\n", __func__);
 359                qt_port->ReadBulkStopped = 1;
 360                goto exit;
 361        }
 362
 363        if (!serial) {
 364                dbg("%s - bad serial pointer, exiting\n", __func__);
 365                goto exit;
 366        }
 367        if (qt_port->closePending == 1) {
 368                /* Were closing , stop reading */
 369                dbg("%s - (qt_port->closepending == 1\n", __func__);
 370                qt_port->ReadBulkStopped = 1;
 371                goto exit;
 372        }
 373
 374        /*
 375         * RxHolding is asserted by throttle, if we assert it, we're not
 376         * receiving any more characters and let the box handle the flow
 377         * control
 378         */
 379        if (qt_port->RxHolding == 1) {
 380                qt_port->ReadBulkStopped = 1;
 381                goto exit;
 382        }
 383
 384        if (urb->status) {
 385                qt_port->ReadBulkStopped = 1;
 386
 387                dbg("%s - nonzero read bulk status received: %d\n",
 388                    __func__, urb->status);
 389                goto exit;
 390        }
 391
 392        if (tty && RxCount) {
 393                flag_data = 0;
 394                for (i = 0; i < RxCount; ++i) {
 395                        /* Look ahead code here */
 396                        if ((i <= (RxCount - 3)) && (data[i] == 0x1b)
 397                            && (data[i + 1] == 0x1b)) {
 398                                flag = 0;
 399                                switch (data[i + 2]) {
 400                                case 0x00:
 401                                        /* line status change 4th byte must follow */
 402                                        if (i > (RxCount - 4)) {
 403                                                dbg("Illegal escape seuences in received data\n");
 404                                                break;
 405                                        }
 406                                        ProcessLineStatus(qt_port, data[i + 3]);
 407                                        i += 3;
 408                                        flag = 1;
 409                                        break;
 410
 411                                case 0x01:
 412                                        /* Modem status status change 4th byte must follow */
 413                                        dbg("Modem status status.\n");
 414                                        if (i > (RxCount - 4)) {
 415                                                dbg("Illegal escape sequences in received data\n");
 416                                                break;
 417                                        }
 418                                        ProcessModemStatus(qt_port,
 419                                                           data[i + 3]);
 420                                        i += 3;
 421                                        flag = 1;
 422                                        break;
 423                                case 0xff:
 424                                        dbg("No status sequence.\n");
 425
 426                                        if (tty) {
 427                                                ProcessRxChar(tty, port, data[i]);
 428                                                ProcessRxChar(tty, port, data[i + 1]);
 429                                        }
 430                                        i += 2;
 431                                        break;
 432                                }
 433                                if (flag == 1)
 434                                        continue;
 435                        }
 436
 437                        if (tty && urb->actual_length)
 438                                tty_insert_flip_char(tty, data[i], TTY_NORMAL);
 439
 440                }
 441                tty_flip_buffer_push(tty);
 442        }
 443
 444        /* Continue trying to always read  */
 445        usb_fill_bulk_urb(port->read_urb, serial->dev,
 446                          usb_rcvbulkpipe(serial->dev,
 447                                          port->bulk_in_endpointAddress),
 448                          port->read_urb->transfer_buffer,
 449                          port->read_urb->transfer_buffer_length,
 450                          qt_read_bulk_callback, port);
 451        result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
 452        if (result)
 453                dbg("%s - failed resubmitting read urb, error %d",
 454                    __func__, result);
 455        else {
 456                if (tty && RxCount) {
 457                        tty_flip_buffer_push(tty);
 458                        tty_schedule_flip(tty);
 459                }
 460        }
 461
 462        schedule_work(&port->work);
 463exit:
 464        tty_kref_put(tty);
 465}
 466
 467/*
 468 * qt_get_device
 469 *   Issue a GET_DEVICE vendor-specific request on the default control pipe If
 470 *   successful, fills in the qt_get_device_data structure pointed to by
 471 *   device_data, otherwise return a negative error number of the problem.
 472 */
 473
 474static int qt_get_device(struct usb_serial *serial,
 475                         struct qt_get_device_data *device_data)
 476{
 477        int result;
 478        unsigned char *transfer_buffer;
 479
 480        transfer_buffer =
 481            kmalloc(sizeof(struct qt_get_device_data), GFP_KERNEL);
 482        if (!transfer_buffer)
 483                return -ENOMEM;
 484
 485        result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 486                                 QT_SET_GET_DEVICE, 0xc0, 0, 0,
 487                                 transfer_buffer,
 488                                 sizeof(struct qt_get_device_data), 300);
 489        if (result > 0)
 490                memcpy(device_data, transfer_buffer,
 491                       sizeof(struct qt_get_device_data));
 492        kfree(transfer_buffer);
 493
 494        return result;
 495}
 496
 497/****************************************************************************
 498 *  BoxSetPrebufferLevel
 499   TELLS BOX WHEN TO ASSERT FLOW CONTROL
 500 ****************************************************************************/
 501static int BoxSetPrebufferLevel(struct usb_serial *serial)
 502{
 503        int result;
 504        __u16 buffer_length;
 505
 506        buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
 507        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 508                                 QT_GET_SET_PREBUF_TRIG_LVL, 0x40,
 509                                 buffer_length, 0, NULL, 0, 300);
 510        return result;
 511}
 512
 513/****************************************************************************
 514 *  BoxSetATC
 515   TELLS BOX WHEN TO ASSERT automatic transmitter control
 516   ****************************************************************************/
 517static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode)
 518{
 519        int result;
 520        __u16 buffer_length;
 521
 522        buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
 523
 524        result =
 525            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 526                            QT_SET_ATF, 0x40, n_Mode, 0, NULL, 0, 300);
 527
 528        return result;
 529}
 530
 531/**
 532 * qt_set_device
 533 *   Issue a SET_DEVICE vendor-specific request on the default control pipe If
 534 *   successful returns the number of bytes written, otherwise it returns a
 535 *   negative error number of the problem.
 536 */
 537static int qt_set_device(struct usb_serial *serial,
 538                         struct qt_get_device_data *device_data)
 539{
 540        int result;
 541        __u16 length;
 542        __u16 PortSettings;
 543
 544        PortSettings = ((__u16) (device_data->portb));
 545        PortSettings = (PortSettings << 8);
 546        PortSettings += ((__u16) (device_data->porta));
 547
 548        length = sizeof(struct qt_get_device_data);
 549        dbg("%s - PortSettings = 0x%x\n", __func__, PortSettings);
 550
 551        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 552                                 QT_SET_GET_DEVICE, 0x40, PortSettings,
 553                                 0, NULL, 0, 300);
 554        return result;
 555}
 556
 557static int qt_open_channel(struct usb_serial *serial, __u16 Uart_Number,
 558                           struct qt_open_channel_data *pDeviceData)
 559{
 560        int result;
 561
 562        result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 563                                 QT_OPEN_CLOSE_CHANNEL,
 564                                 USBD_TRANSFER_DIRECTION_IN, 1, Uart_Number,
 565                                 pDeviceData,
 566                                 sizeof(struct qt_open_channel_data), 300);
 567
 568        return result;
 569
 570}
 571
 572static int qt_close_channel(struct usb_serial *serial, __u16 Uart_Number)
 573{
 574        int result;
 575
 576        result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 577                                 QT_OPEN_CLOSE_CHANNEL,
 578                                 USBD_TRANSFER_DIRECTION_OUT, 0, Uart_Number,
 579                                 NULL, 0, 300);
 580
 581        return result;
 582
 583}
 584
 585/****************************************************************************
 586* BoxGetRegister
 587*       issuse a GET_REGISTER vendor-spcific request on the default control pipe
 588*       If successful, fills in the  pValue with the register value asked for
 589****************************************************************************/
 590static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number,
 591                          unsigned short Register_Num, __u8 *pValue)
 592{
 593        int result;
 594        __u16 current_length;
 595
 596        current_length = sizeof(struct qt_get_device_data);
 597
 598        result =
 599            usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 600                            QT_GET_SET_REGISTER, 0xC0, Register_Num,
 601                            Uart_Number, (void *)pValue, sizeof(*pValue), 300);
 602
 603        return result;
 604}
 605
 606/****************************************************************************
 607* BoxSetRegister
 608*       issuse a GET_REGISTER vendor-spcific request on the default control pipe
 609*       If successful, fills in the  pValue with the register value asked for
 610****************************************************************************/
 611static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number,
 612                          unsigned short Register_Num, unsigned short Value)
 613{
 614        int result;
 615        unsigned short RegAndByte;
 616
 617        RegAndByte = Value;
 618        RegAndByte = RegAndByte << 8;
 619        RegAndByte = RegAndByte + Register_Num;
 620
 621/*
 622        result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 623                                 QT_GET_SET_REGISTER, 0xC0, Register_Num,
 624                                 Uart_Number, NULL, 0, 300);
 625*/
 626
 627        result =
 628            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 629                            QT_GET_SET_REGISTER, 0x40, RegAndByte, Uart_Number,
 630                            NULL, 0, 300);
 631
 632        return result;
 633}
 634
 635/*
 636 * qt_setuart
 637 * issuse a SET_UART vendor-spcific request on the default control pipe
 638 * If successful sets baud rate divisor and LCR value
 639 */
 640static int qt_setuart(struct usb_serial *serial, unsigned short Uart_Number,
 641                      unsigned short default_divisor, unsigned char default_LCR)
 642{
 643        int result;
 644        unsigned short UartNumandLCR;
 645
 646        UartNumandLCR = (default_LCR << 8) + Uart_Number;
 647
 648        result =
 649            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 650                            QT_GET_SET_UART, 0x40, default_divisor,
 651                            UartNumandLCR, NULL, 0, 300);
 652
 653        return result;
 654}
 655
 656static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index,
 657                             int bSet)
 658{
 659        __u8 mcr = 0;
 660        __u8 msr = 0, MOUT_Value = 0;
 661        unsigned int status;
 662
 663        if (bSet == 1) {
 664                /* flow control, box will clear RTS line to prevent remote */
 665                mcr = SERIAL_MCR_RTS;
 666        } /* device from xmitting more chars */
 667        else {
 668                /* no flow control to remote device */
 669                mcr = 0;
 670
 671        }
 672        MOUT_Value = mcr << 8;
 673
 674        if (bSet == 1) {
 675                /* flow control, box will inhibit xmit data if CTS line is
 676                 * asserted */
 677                msr = SERIAL_MSR_CTS;
 678        } else {
 679                /* Box will not inhimbe xmit data due to CTS line */
 680                msr = 0;
 681        }
 682        MOUT_Value |= msr;
 683
 684        status =
 685            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 686                            QT_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value,
 687                            index, NULL, 0, 300);
 688        return status;
 689
 690}
 691
 692static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 index,
 693                             unsigned char stop_char, unsigned char start_char)
 694{
 695        __u16 nSWflowout;
 696        int result;
 697
 698        nSWflowout = start_char << 8;
 699        nSWflowout = (unsigned short)stop_char;
 700
 701        result =
 702            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 703                            QT_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout,
 704                            index, NULL, 0, 300);
 705        return result;
 706
 707}
 708
 709static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index)
 710{
 711        int result;
 712
 713        result =
 714            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
 715                            QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index,
 716                            NULL, 0, 300);
 717        return result;
 718
 719}
 720
 721static int qt_startup(struct usb_serial *serial)
 722{
 723        struct usb_serial_port *port;
 724        struct quatech_port *qt_port;
 725        struct qt_get_device_data DeviceData;
 726        int i;
 727        int status;
 728
 729        dbg("enterting %s", __func__);
 730
 731        /* Now setup per port private data */
 732        for (i = 0; i < serial->num_ports; i++) {
 733                port = serial->port[i];
 734                qt_port = kzalloc(sizeof(*qt_port), GFP_KERNEL);
 735                if (!qt_port) {
 736                        dbg("%s: kmalloc for quatech_port (%d) failed!.",
 737                            __func__, i);
 738                        for (--i; i >= 0; i--) {
 739                                port = serial->port[i];
 740                                kfree(usb_get_serial_port_data(port));
 741                                usb_set_serial_port_data(port, NULL);
 742                        }
 743                        return -ENOMEM;
 744                }
 745                mutex_init(&qt_port->lock);
 746
 747                usb_set_serial_port_data(port, qt_port);
 748
 749        }
 750
 751        status = qt_get_device(serial, &DeviceData);
 752        if (status < 0) {
 753                dbg(__FILE__ "box_get_device failed");
 754                goto startup_error;
 755        }
 756
 757        dbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb);
 758
 759        DeviceData.portb &= ~FULLPWRBIT;
 760        dbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb);
 761
 762        status = qt_set_device(serial, &DeviceData);
 763        if (status < 0) {
 764                dbg(__FILE__ "qt_set_device failed\n");
 765                goto startup_error;
 766        }
 767
 768        status = qt_get_device(serial, &DeviceData);
 769        if (status < 0) {
 770                dbg(__FILE__ "qt_get_device failed");
 771                goto startup_error;
 772        }
 773
 774        switch (serial->dev->descriptor.idProduct) {
 775        case QUATECH_DSU100:
 776        case QUATECH_QSU100:
 777        case QUATECH_ESU100A:
 778        case QUATECH_ESU100B:
 779        case QUATECH_HSU100A:
 780        case QUATECH_HSU100B:
 781        case QUATECH_HSU100C:
 782        case QUATECH_HSU100D:
 783                DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
 784                DeviceData.porta |= CLKS_X4;
 785                DeviceData.portb &= ~(LOOPMODE_BITS);
 786                DeviceData.portb |= RS232_MODE;
 787                break;
 788
 789        case QUATECH_SSU200:
 790        case QUATECH_DSU200:
 791        case QUATECH_QSU200:
 792        case QUATECH_ESU200A:
 793        case QUATECH_ESU200B:
 794        case QUATECH_HSU200A:
 795        case QUATECH_HSU200B:
 796        case QUATECH_HSU200C:
 797        case QUATECH_HSU200D:
 798                DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
 799                DeviceData.porta |= CLKS_X4;
 800                DeviceData.portb &= ~(LOOPMODE_BITS);
 801                DeviceData.portb |= ALL_LOOPBACK;
 802                break;
 803        default:
 804                DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
 805                DeviceData.porta |= CLKS_X4;
 806                DeviceData.portb &= ~(LOOPMODE_BITS);
 807                DeviceData.portb |= RS232_MODE;
 808                break;
 809
 810        }
 811
 812        status = BoxSetPrebufferLevel(serial);  /* sets to default value */
 813        if (status < 0) {
 814                dbg(__FILE__ "BoxSetPrebufferLevel failed\n");
 815                goto startup_error;
 816        }
 817
 818        status = BoxSetATC(serial, ATC_DISABLED);
 819        if (status < 0) {
 820                dbg(__FILE__ "BoxSetATC failed\n");
 821                goto startup_error;
 822        }
 823
 824        dbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb);
 825
 826        DeviceData.portb |= NEXT_BOARD_POWER_BIT;
 827        dbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb);
 828
 829        status = qt_set_device(serial, &DeviceData);
 830        if (status < 0) {
 831                dbg(__FILE__ "qt_set_device failed\n");
 832                goto startup_error;
 833        }
 834
 835        dbg("Exit Success %s\n", __func__);
 836
 837        return 0;
 838
 839startup_error:
 840        for (i = 0; i < serial->num_ports; i++) {
 841                port = serial->port[i];
 842                qt_port = qt_get_port_private(port);
 843                kfree(qt_port);
 844                usb_set_serial_port_data(port, NULL);
 845        }
 846
 847        dbg("Exit fail %s\n", __func__);
 848
 849        return -EIO;
 850}
 851
 852static void qt_release(struct usb_serial *serial)
 853{
 854        struct usb_serial_port *port;
 855        struct quatech_port *qt_port;
 856        int i;
 857
 858        dbg("enterting %s", __func__);
 859
 860        for (i = 0; i < serial->num_ports; i++) {
 861                port = serial->port[i];
 862                if (!port)
 863                        continue;
 864
 865                qt_port = usb_get_serial_port_data(port);
 866                kfree(qt_port);
 867                usb_set_serial_port_data(port, NULL);
 868        }
 869
 870}
 871
 872static int qt_open(struct tty_struct *tty,
 873                   struct usb_serial_port *port)
 874{
 875        struct usb_serial *serial;
 876        struct quatech_port *quatech_port;
 877        struct quatech_port *port0;
 878        struct qt_open_channel_data ChannelData;
 879
 880        int result;
 881
 882        if (port_paranoia_check(port, __func__))
 883                return -ENODEV;
 884
 885        dbg("%s - port %d\n", __func__, port->number);
 886
 887        serial = port->serial;
 888
 889        if (serial_paranoia_check(serial, __func__))
 890                return -ENODEV;
 891
 892        quatech_port = qt_get_port_private(port);
 893        port0 = qt_get_port_private(serial->port[0]);
 894
 895        if (quatech_port == NULL || port0 == NULL)
 896                return -ENODEV;
 897
 898        usb_clear_halt(serial->dev, port->write_urb->pipe);
 899        usb_clear_halt(serial->dev, port->read_urb->pipe);
 900        port0->open_ports++;
 901
 902        result = qt_get_device(serial, &port0->DeviceData);
 903
 904        /* Port specific setups */
 905        result = qt_open_channel(serial, port->number, &ChannelData);
 906        if (result < 0) {
 907                dbg(__FILE__ "qt_open_channel failed\n");
 908                return result;
 909        }
 910        dbg(__FILE__ "qt_open_channel completed.\n");
 911
 912/* FIXME: are these needed?  Does it even do anything useful? */
 913        quatech_port->shadowLSR = ChannelData.line_status &
 914            (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI);
 915
 916        quatech_port->shadowMSR = ChannelData.modem_status &
 917            (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD);
 918
 919        /* Set Baud rate to default and turn off (default)flow control here */
 920        result = qt_setuart(serial, port->number, DEFAULT_DIVISOR, DEFAULT_LCR);
 921        if (result < 0) {
 922                dbg(__FILE__ "qt_setuart failed\n");
 923                return result;
 924        }
 925        dbg(__FILE__ "qt_setuart completed.\n");
 926
 927        /*
 928         * Put this here to make it responsive to stty and defaults set by
 929         * the tty layer
 930         */
 931        /* FIXME: is this needed? */
 932        /* qt_set_termios(tty, port, NULL); */
 933
 934        /*  Check to see if we've set up our endpoint info yet */
 935        if (port0->open_ports == 1) {
 936                if (serial->port[0]->interrupt_in_buffer == NULL) {
 937                        /* set up interrupt urb */
 938                        usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
 939                                         serial->dev,
 940                                         usb_rcvintpipe(serial->dev,
 941                                                        serial->port[0]->interrupt_in_endpointAddress),
 942                                         serial->port[0]->interrupt_in_buffer,
 943                                         serial->port[0]->
 944                                         interrupt_in_urb->transfer_buffer_length,
 945                                         qt_interrupt_callback, serial,
 946                                         serial->port[0]->
 947                                         interrupt_in_urb->interval);
 948
 949                        result =
 950                            usb_submit_urb(serial->port[0]->interrupt_in_urb,
 951                                           GFP_KERNEL);
 952                        if (result) {
 953                                dev_err(&port->dev,
 954                                        "%s - Error %d submitting "
 955                                        "interrupt urb\n", __func__, result);
 956                        }
 957
 958                }
 959
 960        }
 961
 962        dbg("port number is %d\n", port->number);
 963        dbg("serial number is %d\n", port->serial->minor);
 964        dbg("Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
 965        dbg("BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
 966        dbg("Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
 967        dbg("port's number in the device is %d\n", quatech_port->port_num);
 968        quatech_port->read_urb = port->read_urb;
 969
 970        /* set up our bulk in urb */
 971
 972        usb_fill_bulk_urb(quatech_port->read_urb,
 973                          serial->dev,
 974                          usb_rcvbulkpipe(serial->dev,
 975                                          port->bulk_in_endpointAddress),
 976                          port->bulk_in_buffer,
 977                          quatech_port->read_urb->transfer_buffer_length,
 978                          qt_read_bulk_callback, quatech_port);
 979
 980        dbg("qt_open: bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
 981        quatech_port->read_urb_busy = true;
 982        result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL);
 983        if (result) {
 984                dev_err(&port->dev,
 985                        "%s - Error %d submitting control urb\n",
 986                        __func__, result);
 987                quatech_port->read_urb_busy = false;
 988        }
 989
 990        /* initialize our wait queues */
 991        init_waitqueue_head(&quatech_port->wait);
 992        init_waitqueue_head(&quatech_port->msr_wait);
 993
 994        /* initialize our icount structure */
 995        memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount));
 996
 997        return 0;
 998
 999}
1000
1001static int qt_chars_in_buffer(struct tty_struct *tty)
1002{
1003        struct usb_serial_port *port = tty->driver_data;
1004        struct usb_serial *serial;
1005        int chars = 0;
1006
1007        serial = get_usb_serial(port, __func__);
1008
1009        dbg("%s - port %d\n", __func__, port->number);
1010
1011        if (serial->num_bulk_out) {
1012                if (port->write_urb->status == -EINPROGRESS)
1013                        chars = port->write_urb->transfer_buffer_length;
1014        }
1015
1016        dbg("%s - returns %d\n", __func__, chars);
1017
1018        return chars;
1019}
1020
1021static void qt_block_until_empty(struct tty_struct *tty,
1022                                 struct quatech_port *qt_port)
1023{
1024        int timeout = HZ / 10;
1025        int wait = 30;
1026        int count;
1027
1028        while (1) {
1029
1030                count = qt_chars_in_buffer(tty);
1031
1032                if (count <= 0)
1033                        return;
1034
1035                interruptible_sleep_on_timeout(&qt_port->wait, timeout);
1036
1037                wait--;
1038                if (wait == 0) {
1039                        dbg("%s - TIMEOUT", __func__);
1040                        return;
1041                } else {
1042                        wait = 30;
1043                }
1044        }
1045}
1046
1047static void qt_close(struct usb_serial_port *port)
1048{
1049        struct usb_serial *serial = port->serial;
1050        struct quatech_port *qt_port;
1051        struct quatech_port *port0;
1052        struct tty_struct *tty;
1053        int status;
1054        unsigned int index;
1055        status = 0;
1056
1057        dbg("%s - port %d\n", __func__, port->number);
1058
1059        tty = tty_port_tty_get(&port->port);
1060        index = tty->index - serial->minor;
1061
1062        qt_port = qt_get_port_private(port);
1063        port0 = qt_get_port_private(serial->port[0]);
1064
1065        /* shutdown any bulk reads that might be going on */
1066        if (serial->num_bulk_out)
1067                usb_unlink_urb(port->write_urb);
1068        if (serial->num_bulk_in)
1069                usb_unlink_urb(port->read_urb);
1070
1071        /* wait up to for transmitter to empty */
1072        if (serial->dev)
1073                qt_block_until_empty(tty, qt_port);
1074        tty_kref_put(tty);
1075
1076        /* Close uart channel */
1077        status = qt_close_channel(serial, index);
1078        if (status < 0)
1079                dbg("%s - port %d qt_close_channel failed.\n",
1080                    __func__, port->number);
1081
1082        port0->open_ports--;
1083
1084        dbg("qt_num_open_ports in close%d:in port%d\n",
1085            port0->open_ports, port->number);
1086
1087        if (port0->open_ports == 0) {
1088                if (serial->port[0]->interrupt_in_urb) {
1089                        dbg("%s", "Shutdown interrupt_in_urb\n");
1090                        usb_kill_urb(serial->port[0]->interrupt_in_urb);
1091                }
1092
1093        }
1094
1095        if (qt_port->write_urb) {
1096                /* if this urb had a transfer buffer already (old tx) free it */
1097                kfree(qt_port->write_urb->transfer_buffer);
1098                usb_free_urb(qt_port->write_urb);
1099        }
1100
1101}
1102
1103static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1104                    const unsigned char *buf, int count)
1105{
1106        int result;
1107        struct usb_serial *serial = get_usb_serial(port, __func__);
1108
1109        if (serial == NULL)
1110                return -ENODEV;
1111
1112        dbg("%s - port %d\n", __func__, port->number);
1113
1114        if (count == 0) {
1115                dbg("%s - write request of 0 bytes\n", __func__);
1116                return 0;
1117        }
1118
1119        /* only do something if we have a bulk out endpoint */
1120        if (serial->num_bulk_out) {
1121                if (port->write_urb->status == -EINPROGRESS) {
1122                        dbg("%s - already writing\n", __func__);
1123                        return 0;
1124                }
1125
1126                count =
1127                    (count > port->bulk_out_size) ? port->bulk_out_size : count;
1128                memcpy(port->write_urb->transfer_buffer, buf, count);
1129
1130                /* set up our urb */
1131
1132                usb_fill_bulk_urb(port->write_urb, serial->dev,
1133                                  usb_sndbulkpipe(serial->dev,
1134                                                  port->
1135                                                  bulk_out_endpointAddress),
1136                                  port->write_urb->transfer_buffer, count,
1137                                  qt_write_bulk_callback, port);
1138
1139                /* send the data out the bulk port */
1140                result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1141                if (result)
1142                        dbg("%s - failed submitting write urb, error %d\n",
1143                            __func__, result);
1144                else
1145                        result = count;
1146
1147                return result;
1148        }
1149
1150        /* no bulk out, so return 0 bytes written */
1151        return 0;
1152}
1153
1154static int qt_write_room(struct tty_struct *tty)
1155{
1156        struct usb_serial_port *port = tty->driver_data;
1157        struct usb_serial *serial;
1158        struct quatech_port *qt_port;
1159
1160        int retval = -EINVAL;
1161
1162        if (port_paranoia_check(port, __func__)) {
1163                dbg("%s", "Invalid port\n");
1164                return -1;
1165        }
1166
1167        serial = get_usb_serial(port, __func__);
1168
1169        if (!serial)
1170                return -ENODEV;
1171
1172        qt_port = qt_get_port_private(port);
1173
1174        mutex_lock(&qt_port->lock);
1175
1176        dbg("%s - port %d\n", __func__, port->number);
1177
1178        if (serial->num_bulk_out) {
1179                if (port->write_urb->status != -EINPROGRESS)
1180                        retval = port->bulk_out_size;
1181        }
1182
1183        mutex_unlock(&qt_port->lock);
1184        return retval;
1185
1186}
1187
1188static int qt_ioctl(struct tty_struct *tty,
1189                    unsigned int cmd, unsigned long arg)
1190{
1191        struct usb_serial_port *port = tty->driver_data;
1192        struct quatech_port *qt_port = qt_get_port_private(port);
1193        struct usb_serial *serial = get_usb_serial(port, __func__);
1194        unsigned int index;
1195
1196        dbg("%s cmd 0x%04x", __func__, cmd);
1197
1198        index = tty->index - serial->minor;
1199
1200        if (cmd == TIOCMIWAIT) {
1201                while (qt_port != NULL) {
1202                        interruptible_sleep_on(&qt_port->msr_wait);
1203                        if (signal_pending(current))
1204                                return -ERESTARTSYS;
1205                        else {
1206                                char diff = qt_port->diff_status;
1207
1208                                if (diff == 0)
1209                                        return -EIO;    /* no change => error */
1210
1211                                /* Consume all events */
1212                                qt_port->diff_status = 0;
1213
1214                                if (((arg & TIOCM_RNG)
1215                                     && (diff & SERIAL_MSR_RI))
1216                                    || ((arg & TIOCM_DSR)
1217                                        && (diff & SERIAL_MSR_DSR))
1218                                    || ((arg & TIOCM_CD)
1219                                        && (diff & SERIAL_MSR_CD))
1220                                    || ((arg & TIOCM_CTS)
1221                                        && (diff & SERIAL_MSR_CTS))) {
1222                                        return 0;
1223                                }
1224                        }
1225                }
1226                return 0;
1227        }
1228
1229        dbg("%s -No ioctl for that one.  port = %d\n", __func__, port->number);
1230        return -ENOIOCTLCMD;
1231}
1232
1233static void qt_set_termios(struct tty_struct *tty,
1234                           struct usb_serial_port *port,
1235                           struct ktermios *old_termios)
1236{
1237        struct ktermios *termios = tty->termios;
1238        unsigned char new_LCR = 0;
1239        unsigned int cflag = termios->c_cflag;
1240        unsigned int index;
1241        int baud, divisor, remainder;
1242        int status;
1243
1244        dbg("%s", __func__);
1245
1246        index = tty->index - port->serial->minor;
1247
1248        switch (cflag) {
1249        case CS5:
1250                new_LCR |= SERIAL_5_DATA;
1251                break;
1252        case CS6:
1253                new_LCR |= SERIAL_6_DATA;
1254                break;
1255        case CS7:
1256                new_LCR |= SERIAL_7_DATA;
1257                break;
1258        default:
1259        case CS8:
1260                new_LCR |= SERIAL_8_DATA;
1261                break;
1262        }
1263
1264        /* Parity stuff */
1265        if (cflag & PARENB) {
1266                if (cflag & PARODD)
1267                        new_LCR |= SERIAL_ODD_PARITY;
1268                else
1269                        new_LCR |= SERIAL_EVEN_PARITY;
1270        }
1271        if (cflag & CSTOPB)
1272                new_LCR |= SERIAL_TWO_STOPB;
1273        else
1274                new_LCR |= SERIAL_ONE_STOPB;
1275
1276        dbg("%s - 4\n", __func__);
1277
1278        /* Thats the LCR stuff, go ahead and set it */
1279        baud = tty_get_baud_rate(tty);
1280        if (!baud)
1281                /* pick a default, any default... */
1282                baud = 9600;
1283
1284        dbg("%s - got baud = %d\n", __func__, baud);
1285
1286        divisor = MAX_BAUD_RATE / baud;
1287        remainder = MAX_BAUD_RATE % baud;
1288        /* Round to nearest divisor */
1289        if (((remainder * 2) >= baud) && (baud != 110))
1290                divisor++;
1291
1292        /*
1293         * Set Baud rate to default and turn off (default)flow control here
1294         */
1295        status =
1296            qt_setuart(port->serial, index, (unsigned short)divisor, new_LCR);
1297        if (status < 0) {
1298                dbg(__FILE__ "qt_setuart failed\n");
1299                return;
1300        }
1301
1302        /* Now determine flow control */
1303        if (cflag & CRTSCTS) {
1304                dbg("%s - Enabling HW flow control port %d\n", __func__,
1305                    port->number);
1306
1307                /* Enable RTS/CTS flow control */
1308                status = BoxSetHW_FlowCtrl(port->serial, index, 1);
1309
1310                if (status < 0) {
1311                        dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1312                        return;
1313                }
1314        } else {
1315                /* Disable RTS/CTS flow control */
1316                dbg("%s - disabling HW flow control port %d\n", __func__,
1317                    port->number);
1318
1319                status = BoxSetHW_FlowCtrl(port->serial, index, 0);
1320                if (status < 0) {
1321                        dbg(__FILE__ "BoxSetHW_FlowCtrl failed\n");
1322                        return;
1323                }
1324
1325        }
1326
1327        /* if we are implementing XON/XOFF, set the start and stop character in
1328         * the device */
1329        if (I_IXOFF(tty) || I_IXON(tty)) {
1330                unsigned char stop_char = STOP_CHAR(tty);
1331                unsigned char start_char = START_CHAR(tty);
1332                status =
1333                    BoxSetSW_FlowCtrl(port->serial, index, stop_char,
1334                                      start_char);
1335                if (status < 0)
1336                        dbg(__FILE__ "BoxSetSW_FlowCtrl (enabled) failed\n");
1337
1338        } else {
1339                /* disable SW flow control */
1340                status = BoxDisable_SW_FlowCtrl(port->serial, index);
1341                if (status < 0)
1342                        dbg(__FILE__ "BoxSetSW_FlowCtrl (diabling) failed\n");
1343
1344        }
1345        tty->termios->c_cflag &= ~CMSPAR;
1346        /* FIXME: Error cases should be returning the actual bits changed only */
1347}
1348
1349static void qt_break(struct tty_struct *tty, int break_state)
1350{
1351        struct usb_serial_port *port = tty->driver_data;
1352        struct usb_serial *serial = get_usb_serial(port, __func__);
1353        struct quatech_port *qt_port;
1354        u16 index, onoff;
1355        unsigned int result;
1356
1357        index = tty->index - serial->minor;
1358
1359        qt_port = qt_get_port_private(port);
1360
1361        if (break_state == -1)
1362                onoff = 1;
1363        else
1364                onoff = 0;
1365
1366        mutex_lock(&qt_port->lock);
1367
1368        dbg("%s - port %d\n", __func__, port->number);
1369
1370        result =
1371            usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1372                            QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1373
1374        mutex_unlock(&qt_port->lock);
1375}
1376
1377static inline int qt_real_tiocmget(struct tty_struct *tty,
1378                                   struct usb_serial_port *port,
1379                                   struct usb_serial *serial)
1380{
1381
1382        u8 mcr;
1383        u8 msr;
1384        unsigned int result = 0;
1385        int status;
1386        unsigned int index;
1387
1388        dbg("%s - port %d, tty =0x%p\n", __func__, port->number, tty);
1389
1390        index = tty->index - serial->minor;
1391        status =
1392            BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1393        if (status >= 0) {
1394                status =
1395                    BoxGetRegister(port->serial, index,
1396                                   MODEM_STATUS_REGISTER, &msr);
1397
1398        }
1399
1400        if (status >= 0) {
1401                result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1402                    /* DTR IS SET */
1403                    | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1404                    /* RTS IS SET */
1405                    | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1406                    /* CTS is set */
1407                    | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1408                    /* Carrier detect is set */
1409                    | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1410                    /* Ring indicator set */
1411                    | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1412                /* DSR is set */
1413                return result;
1414
1415        } else
1416                return -ESPIPE;
1417}
1418
1419static inline int qt_real_tiocmset(struct tty_struct *tty,
1420                                   struct usb_serial_port *port,
1421                                   struct usb_serial *serial,
1422                                   unsigned int value)
1423{
1424
1425        u8 mcr;
1426        int status;
1427        unsigned int index;
1428
1429        dbg("%s - port %d\n", __func__, port->number);
1430
1431        index = tty->index - serial->minor;
1432        status =
1433            BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1434        if (status < 0)
1435                return -ESPIPE;
1436
1437        /*
1438         * Turn off the RTS and DTR and loopbcck and then only turn on what was
1439         * asked for
1440         */
1441        mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1442        if (value & TIOCM_RTS)
1443                mcr |= SERIAL_MCR_RTS;
1444        if (value & TIOCM_DTR)
1445                mcr |= SERIAL_MCR_DTR;
1446        if (value & TIOCM_LOOP)
1447                mcr |= SERIAL_MCR_LOOP;
1448
1449        status =
1450            BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1451        if (status < 0)
1452                return -ESPIPE;
1453        else
1454                return 0;
1455}
1456
1457static int qt_tiocmget(struct tty_struct *tty)
1458{
1459        struct usb_serial_port *port = tty->driver_data;
1460        struct usb_serial *serial = get_usb_serial(port, __func__);
1461        struct quatech_port *qt_port = qt_get_port_private(port);
1462        int retval = -ENODEV;
1463
1464        dbg("In %s\n", __func__);
1465
1466        if (!serial)
1467                return -ENODEV;
1468
1469        mutex_lock(&qt_port->lock);
1470
1471        dbg("%s - port %d\n", __func__, port->number);
1472        dbg("%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
1473
1474        retval = qt_real_tiocmget(tty, port, serial);
1475
1476        mutex_unlock(&qt_port->lock);
1477        return retval;
1478}
1479
1480static int qt_tiocmset(struct tty_struct *tty,
1481                       unsigned int set, unsigned int clear)
1482{
1483
1484        struct usb_serial_port *port = tty->driver_data;
1485        struct usb_serial *serial = get_usb_serial(port, __func__);
1486        struct quatech_port *qt_port = qt_get_port_private(port);
1487        int retval = -ENODEV;
1488
1489        dbg("In %s\n", __func__);
1490
1491        if (!serial)
1492                return -ENODEV;
1493
1494        mutex_lock(&qt_port->lock);
1495
1496        dbg("%s - port %d\n", __func__, port->number);
1497        dbg("%s - qt_port->RxHolding = %d\n", __func__, qt_port->RxHolding);
1498
1499        retval = qt_real_tiocmset(tty, port, serial, set);
1500
1501        mutex_unlock(&qt_port->lock);
1502        return retval;
1503}
1504
1505static void qt_throttle(struct tty_struct *tty)
1506{
1507        struct usb_serial_port *port = tty->driver_data;
1508        struct usb_serial *serial = get_usb_serial(port, __func__);
1509        struct quatech_port *qt_port;
1510
1511        dbg("%s - port %d\n", __func__, port->number);
1512
1513        if (!serial)
1514                return;
1515
1516        qt_port = qt_get_port_private(port);
1517
1518        mutex_lock(&qt_port->lock);
1519
1520        /* pass on to the driver specific version of this function */
1521        qt_port->RxHolding = 1;
1522        dbg("%s - port->RxHolding = 1\n", __func__);
1523
1524        mutex_unlock(&qt_port->lock);
1525        return;
1526}
1527
1528static void qt_unthrottle(struct tty_struct *tty)
1529{
1530        struct usb_serial_port *port = tty->driver_data;
1531        struct usb_serial *serial = get_usb_serial(port, __func__);
1532        struct quatech_port *qt_port;
1533        unsigned int result;
1534
1535        if (!serial)
1536                return;
1537
1538        qt_port = qt_get_port_private(port);
1539
1540        mutex_lock(&qt_port->lock);
1541
1542        dbg("%s - port %d\n", __func__, port->number);
1543
1544        if (qt_port->RxHolding == 1) {
1545                dbg("%s -qt_port->RxHolding == 1\n", __func__);
1546
1547                qt_port->RxHolding = 0;
1548                dbg("%s - qt_port->RxHolding = 0\n", __func__);
1549
1550                /* if we have a bulk endpoint, start it up */
1551                if ((serial->num_bulk_in) && (qt_port->ReadBulkStopped == 1)) {
1552                        /* Start reading from the device */
1553                        usb_fill_bulk_urb(port->read_urb, serial->dev,
1554                                          usb_rcvbulkpipe(serial->dev,
1555                                                          port->bulk_in_endpointAddress),
1556                                          port->read_urb->transfer_buffer,
1557                                          port->read_urb->
1558                                          transfer_buffer_length,
1559                                          qt_read_bulk_callback, port);
1560                        result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1561                        if (result)
1562                                err("%s - failed restarting read urb, error %d",
1563                                    __func__, result);
1564                }
1565        }
1566        mutex_unlock(&qt_port->lock);
1567        return;
1568
1569}
1570
1571static int qt_calc_num_ports(struct usb_serial *serial)
1572{
1573        int num_ports;
1574
1575        dbg("numberofendpoints: %d\n",
1576            (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
1577        dbg("numberofendpoints: %d\n",
1578            (int)serial->interface->altsetting->desc.bNumEndpoints);
1579
1580        num_ports =
1581            (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1582
1583        return num_ports;
1584}
1585
1586static struct usb_serial_driver quatech_device = {
1587        .driver = {
1588                   .owner = THIS_MODULE,
1589                   .name = "serqt",
1590                   },
1591        .description = DRIVER_DESC,
1592        .id_table = serqt_id_table,
1593        .num_ports = 8,
1594        .open = qt_open,
1595        .close = qt_close,
1596        .write = qt_write,
1597        .write_room = qt_write_room,
1598        .chars_in_buffer = qt_chars_in_buffer,
1599        .throttle = qt_throttle,
1600        .unthrottle = qt_unthrottle,
1601        .calc_num_ports = qt_calc_num_ports,
1602        .ioctl = qt_ioctl,
1603        .set_termios = qt_set_termios,
1604        .break_ctl = qt_break,
1605        .tiocmget = qt_tiocmget,
1606        .tiocmset = qt_tiocmset,
1607        .attach = qt_startup,
1608        .release = qt_release,
1609};
1610
1611static struct usb_serial_driver * const serial_drivers[] = {
1612        &quatech_device, NULL
1613};
1614
1615module_usb_serial_driver(serqt_usb_driver, serial_drivers);
1616
1617MODULE_AUTHOR(DRIVER_AUTHOR);
1618MODULE_DESCRIPTION(DRIVER_DESC);
1619MODULE_LICENSE("GPL");
1620
1621module_param(debug, bool, S_IRUGO | S_IWUSR);
1622MODULE_PARM_DESC(debug, "Debug enabled or not");
1623