Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * usb-serial driver for Quatech USB 2 devices
0004  *
0005  * Copyright (C) 2012 Bill Pemberton (wfp5p@virginia.edu)
0006  *
0007  *  These devices all have only 1 bulk in and 1 bulk out that is shared
0008  *  for all serial ports.
0009  *
0010  */
0011 
0012 #include <asm/unaligned.h>
0013 #include <linux/errno.h>
0014 #include <linux/slab.h>
0015 #include <linux/tty.h>
0016 #include <linux/tty_driver.h>
0017 #include <linux/tty_flip.h>
0018 #include <linux/module.h>
0019 #include <linux/serial.h>
0020 #include <linux/usb.h>
0021 #include <linux/usb/serial.h>
0022 #include <linux/serial_reg.h>
0023 #include <linux/uaccess.h>
0024 
0025 /* default urb timeout for usb operations */
0026 #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
0027 
0028 #define QT_OPEN_CLOSE_CHANNEL       0xca
0029 #define QT_SET_GET_DEVICE           0xc2
0030 #define QT_SET_GET_REGISTER         0xc0
0031 #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
0032 #define QT_SET_ATF                  0xcd
0033 #define QT_TRANSFER_IN              0xc0
0034 #define QT_HW_FLOW_CONTROL_MASK     0xc5
0035 #define QT_SW_FLOW_CONTROL_MASK     0xc6
0036 #define QT2_BREAK_CONTROL       0xc8
0037 #define QT2_GET_SET_UART            0xc1
0038 #define QT2_FLUSH_DEVICE        0xc4
0039 #define QT2_GET_SET_QMCR            0xe1
0040 #define QT2_QMCR_RS232              0x40
0041 #define QT2_QMCR_RS422              0x10
0042 
0043 #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
0044 
0045 #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR)
0046 
0047 /* status bytes for the device */
0048 #define QT2_CONTROL_BYTE    0x1b
0049 #define QT2_LINE_STATUS     0x00  /* following 1 byte is line status */
0050 #define QT2_MODEM_STATUS    0x01  /* following 1 byte is modem status */
0051 #define QT2_XMIT_HOLD       0x02  /* following 2 bytes are ?? */
0052 #define QT2_CHANGE_PORT     0x03  /* following 1 byte is port to change to */
0053 #define QT2_REC_FLUSH       0x04  /* no following info */
0054 #define QT2_XMIT_FLUSH      0x05  /* no following info */
0055 #define QT2_CONTROL_ESCAPE  0xff  /* pass through previous 2 control bytes */
0056 
0057 #define  MAX_BAUD_RATE              921600
0058 #define  DEFAULT_BAUD_RATE          9600
0059 
0060 #define QT2_READ_BUFFER_SIZE    512  /* size of read buffer */
0061 #define QT2_WRITE_BUFFER_SIZE   512  /* size of write buffer */
0062 #define QT2_WRITE_CONTROL_SIZE  5    /* control bytes used for a write */
0063 
0064 #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
0065 
0066 #define USB_VENDOR_ID_QUATECH   0x061d
0067 #define QUATECH_SSU2_100    0xC120  /* RS232 single port */
0068 #define QUATECH_DSU2_100    0xC140  /* RS232 dual port */
0069 #define QUATECH_DSU2_400    0xC150  /* RS232/422/485 dual port */
0070 #define QUATECH_QSU2_100    0xC160  /* RS232 four port */
0071 #define QUATECH_QSU2_400    0xC170  /* RS232/422/485 four port */
0072 #define QUATECH_ESU2_100    0xC1A0  /* RS232 eight port */
0073 #define QUATECH_ESU2_400    0xC180  /* RS232/422/485 eight port */
0074 
0075 struct qt2_device_detail {
0076     int product_id;
0077     int num_ports;
0078 };
0079 
0080 #define QT_DETAILS(prod, ports) \
0081     .product_id = (prod),   \
0082     .num_ports = (ports)
0083 
0084 static const struct qt2_device_detail qt2_device_details[] = {
0085     {QT_DETAILS(QUATECH_SSU2_100, 1)},
0086     {QT_DETAILS(QUATECH_DSU2_400, 2)},
0087     {QT_DETAILS(QUATECH_DSU2_100, 2)},
0088     {QT_DETAILS(QUATECH_QSU2_400, 4)},
0089     {QT_DETAILS(QUATECH_QSU2_100, 4)},
0090     {QT_DETAILS(QUATECH_ESU2_400, 8)},
0091     {QT_DETAILS(QUATECH_ESU2_100, 8)},
0092     {QT_DETAILS(0, 0)}  /* Terminating entry */
0093 };
0094 
0095 static const struct usb_device_id id_table[] = {
0096     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
0097     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
0098     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
0099     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
0100     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
0101     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
0102     {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
0103     {}          /* Terminating entry */
0104 };
0105 MODULE_DEVICE_TABLE(usb, id_table);
0106 
0107 struct qt2_serial_private {
0108     unsigned char current_port;  /* current port for incoming data */
0109 
0110     struct urb  *read_urb;   /* shared among all ports */
0111     char        *read_buffer;
0112 };
0113 
0114 struct qt2_port_private {
0115     u8   device_port;
0116 
0117     spinlock_t urb_lock;
0118     bool       urb_in_use;
0119     struct urb *write_urb;
0120     char       *write_buffer;
0121 
0122     spinlock_t  lock;
0123     u8          shadowLSR;
0124     u8          shadowMSR;
0125 
0126     struct usb_serial_port *port;
0127 };
0128 
0129 static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch);
0130 static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch);
0131 static void qt2_write_bulk_callback(struct urb *urb);
0132 static void qt2_read_bulk_callback(struct urb *urb);
0133 
0134 static void qt2_release(struct usb_serial *serial)
0135 {
0136     struct qt2_serial_private *serial_priv;
0137 
0138     serial_priv = usb_get_serial_data(serial);
0139 
0140     usb_kill_urb(serial_priv->read_urb);
0141     usb_free_urb(serial_priv->read_urb);
0142     kfree(serial_priv->read_buffer);
0143     kfree(serial_priv);
0144 }
0145 
0146 static inline int calc_baud_divisor(int baudrate)
0147 {
0148     int divisor, rem;
0149 
0150     divisor = MAX_BAUD_RATE / baudrate;
0151     rem = MAX_BAUD_RATE % baudrate;
0152     /* Round to nearest divisor */
0153     if (((rem * 2) >= baudrate) && (baudrate != 110))
0154         divisor++;
0155 
0156     return divisor;
0157 }
0158 
0159 static inline int qt2_set_port_config(struct usb_device *dev,
0160                       unsigned char port_number,
0161                       u16 baudrate, u16 lcr)
0162 {
0163     int divisor = calc_baud_divisor(baudrate);
0164     u16 index = ((u16) (lcr << 8) | (u16) (port_number));
0165 
0166     return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
0167                    QT2_GET_SET_UART, 0x40,
0168                    divisor, index, NULL, 0, QT2_USB_TIMEOUT);
0169 }
0170 
0171 static inline int qt2_control_msg(struct usb_device *dev,
0172                   u8 request, u16 data, u16 index)
0173 {
0174     return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
0175                    request, 0x40, data, index,
0176                    NULL, 0, QT2_USB_TIMEOUT);
0177 }
0178 
0179 static inline int qt2_setdevice(struct usb_device *dev, u8 *data)
0180 {
0181     u16 x = ((u16) (data[1] << 8) | (u16) (data[0]));
0182 
0183     return qt2_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
0184 }
0185 
0186 
0187 static inline int qt2_getregister(struct usb_device *dev,
0188                   u8 uart,
0189                   u8 reg,
0190                   u8 *data)
0191 {
0192     int ret;
0193 
0194     ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
0195                   QT_SET_GET_REGISTER, 0xc0, reg,
0196                   uart, data, sizeof(*data), QT2_USB_TIMEOUT);
0197     if (ret < (int)sizeof(*data)) {
0198         if (ret >= 0)
0199             ret = -EIO;
0200     }
0201 
0202     return ret;
0203 }
0204 
0205 static inline int qt2_setregister(struct usb_device *dev,
0206                   u8 uart, u8 reg, u16 data)
0207 {
0208     u16 value = (data << 8) | reg;
0209 
0210     return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
0211                    QT_SET_GET_REGISTER, 0x40, value, uart,
0212                    NULL, 0, QT2_USB_TIMEOUT);
0213 }
0214 
0215 static inline int update_mctrl(struct qt2_port_private *port_priv,
0216                    unsigned int set, unsigned int clear)
0217 {
0218     struct usb_serial_port *port = port_priv->port;
0219     struct usb_device *dev = port->serial->dev;
0220     unsigned urb_value;
0221     int status;
0222 
0223     if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
0224         dev_dbg(&port->dev,
0225             "update_mctrl - DTR|RTS not being set|cleared\n");
0226         return 0;   /* no change */
0227     }
0228 
0229     clear &= ~set;  /* 'set' takes precedence over 'clear' */
0230     urb_value = 0;
0231     if (set & TIOCM_DTR)
0232         urb_value |= UART_MCR_DTR;
0233     if (set & TIOCM_RTS)
0234         urb_value |= UART_MCR_RTS;
0235 
0236     status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
0237                  urb_value);
0238     if (status < 0)
0239         dev_err(&port->dev,
0240             "update_mctrl - Error from MODEM_CTRL urb: %i\n",
0241             status);
0242     return status;
0243 }
0244 
0245 static int qt2_calc_num_ports(struct usb_serial *serial,
0246                     struct usb_serial_endpoints *epds)
0247 {
0248     struct qt2_device_detail d;
0249     int i;
0250 
0251     for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
0252         if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
0253             return d.num_ports;
0254     }
0255 
0256     /* we didn't recognize the device */
0257     dev_err(&serial->dev->dev,
0258          "don't know the number of ports, assuming 1\n");
0259 
0260     return 1;
0261 }
0262 
0263 static void qt2_set_termios(struct tty_struct *tty,
0264                 struct usb_serial_port *port,
0265                 struct ktermios *old_termios)
0266 {
0267     struct usb_device *dev = port->serial->dev;
0268     struct qt2_port_private *port_priv;
0269     struct ktermios *termios = &tty->termios;
0270     u16 baud;
0271     unsigned int cflag = termios->c_cflag;
0272     u16 new_lcr = 0;
0273     int status;
0274 
0275     port_priv = usb_get_serial_port_data(port);
0276 
0277     if (cflag & PARENB) {
0278         if (cflag & PARODD)
0279             new_lcr |= UART_LCR_PARITY;
0280         else
0281             new_lcr |= SERIAL_EVEN_PARITY;
0282     }
0283 
0284     new_lcr |= UART_LCR_WLEN(tty_get_char_size(cflag));
0285 
0286     baud = tty_get_baud_rate(tty);
0287     if (!baud)
0288         baud = 9600;
0289 
0290     status = qt2_set_port_config(dev, port_priv->device_port, baud,
0291                      new_lcr);
0292     if (status < 0)
0293         dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
0294             __func__, status);
0295 
0296     if (cflag & CRTSCTS)
0297         status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
0298                      SERIAL_CRTSCTS,
0299                      port_priv->device_port);
0300     else
0301         status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
0302                      0, port_priv->device_port);
0303     if (status < 0)
0304         dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
0305             __func__, status);
0306 
0307     if (I_IXOFF(tty) || I_IXON(tty)) {
0308         u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
0309 
0310         status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
0311                      x, port_priv->device_port);
0312     } else
0313         status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
0314                      0, port_priv->device_port);
0315 
0316     if (status < 0)
0317         dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
0318             __func__, status);
0319 
0320 }
0321 
0322 static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
0323 {
0324     struct usb_serial *serial;
0325     struct qt2_port_private *port_priv;
0326     u8 *data;
0327     u16 device_port;
0328     int status;
0329     unsigned long flags;
0330 
0331     device_port = port->port_number;
0332 
0333     serial = port->serial;
0334 
0335     port_priv = usb_get_serial_port_data(port);
0336 
0337     /* set the port to RS232 mode */
0338     status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
0339                  QT2_QMCR_RS232, device_port);
0340     if (status < 0) {
0341         dev_err(&port->dev,
0342             "%s failed to set RS232 mode for port %i error %i\n",
0343             __func__, device_port, status);
0344         return status;
0345     }
0346 
0347     data = kzalloc(2, GFP_KERNEL);
0348     if (!data)
0349         return -ENOMEM;
0350 
0351     /* open the port */
0352     status = usb_control_msg(serial->dev,
0353                  usb_rcvctrlpipe(serial->dev, 0),
0354                  QT_OPEN_CLOSE_CHANNEL,
0355                  0xc0, 0,
0356                  device_port, data, 2, QT2_USB_TIMEOUT);
0357 
0358     if (status < 2) {
0359         dev_err(&port->dev, "%s - open port failed %i\n", __func__,
0360             status);
0361         if (status >= 0)
0362             status = -EIO;
0363         kfree(data);
0364         return status;
0365     }
0366 
0367     spin_lock_irqsave(&port_priv->lock, flags);
0368     port_priv->shadowLSR = data[0];
0369     port_priv->shadowMSR = data[1];
0370     spin_unlock_irqrestore(&port_priv->lock, flags);
0371 
0372     kfree(data);
0373 
0374     /* set to default speed and 8bit word size */
0375     status = qt2_set_port_config(serial->dev, device_port,
0376                      DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
0377     if (status < 0) {
0378         dev_err(&port->dev, "%s - initial setup failed (%i)\n",
0379             __func__, device_port);
0380         return status;
0381     }
0382 
0383     port_priv->device_port = (u8) device_port;
0384 
0385     if (tty)
0386         qt2_set_termios(tty, port, &tty->termios);
0387 
0388     return 0;
0389 
0390 }
0391 
0392 static void qt2_close(struct usb_serial_port *port)
0393 {
0394     struct usb_serial *serial;
0395     struct qt2_port_private *port_priv;
0396     int i;
0397 
0398     serial = port->serial;
0399     port_priv = usb_get_serial_port_data(port);
0400 
0401     usb_kill_urb(port_priv->write_urb);
0402 
0403     /* flush the port transmit buffer */
0404     i = usb_control_msg(serial->dev,
0405                 usb_sndctrlpipe(serial->dev, 0),
0406                 QT2_FLUSH_DEVICE, 0x40, 1,
0407                 port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
0408 
0409     if (i < 0)
0410         dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
0411             __func__, i);
0412 
0413     /* flush the port receive buffer */
0414     i = usb_control_msg(serial->dev,
0415                 usb_sndctrlpipe(serial->dev, 0),
0416                 QT2_FLUSH_DEVICE, 0x40, 0,
0417                 port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
0418 
0419     if (i < 0)
0420         dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
0421             __func__, i);
0422 
0423     /* close the port */
0424     i = usb_control_msg(serial->dev,
0425                 usb_sndctrlpipe(serial->dev, 0),
0426                 QT_OPEN_CLOSE_CHANNEL,
0427                 0x40, 0,
0428                 port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
0429 
0430     if (i < 0)
0431         dev_err(&port->dev, "%s - close port failed %i\n",
0432             __func__, i);
0433 }
0434 
0435 static void qt2_disconnect(struct usb_serial *serial)
0436 {
0437     struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
0438 
0439     usb_kill_urb(serial_priv->read_urb);
0440 }
0441 
0442 static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
0443 {
0444     switch (*ch) {
0445     case QT2_LINE_STATUS:
0446         qt2_update_lsr(port, ch + 1);
0447         break;
0448     case QT2_MODEM_STATUS:
0449         qt2_update_msr(port, ch + 1);
0450         break;
0451     }
0452 }
0453 
0454 static void qt2_process_read_urb(struct urb *urb)
0455 {
0456     struct usb_serial *serial;
0457     struct qt2_serial_private *serial_priv;
0458     struct usb_serial_port *port;
0459     bool escapeflag;
0460     unsigned char *ch;
0461     int i;
0462     unsigned char newport;
0463     int len = urb->actual_length;
0464 
0465     if (!len)
0466         return;
0467 
0468     ch = urb->transfer_buffer;
0469     serial = urb->context;
0470     serial_priv = usb_get_serial_data(serial);
0471     port = serial->port[serial_priv->current_port];
0472 
0473     for (i = 0; i < urb->actual_length; i++) {
0474         ch = (unsigned char *)urb->transfer_buffer + i;
0475         if ((i <= (len - 3)) &&
0476             (*ch == QT2_CONTROL_BYTE) &&
0477             (*(ch + 1) == QT2_CONTROL_BYTE)) {
0478             escapeflag = false;
0479             switch (*(ch + 2)) {
0480             case QT2_LINE_STATUS:
0481             case QT2_MODEM_STATUS:
0482                 if (i > (len - 4)) {
0483                     dev_warn(&port->dev,
0484                          "%s - status message too short\n",
0485                         __func__);
0486                     break;
0487                 }
0488                 qt2_process_status(port, ch + 2);
0489                 i += 3;
0490                 escapeflag = true;
0491                 break;
0492             case QT2_XMIT_HOLD:
0493                 if (i > (len - 5)) {
0494                     dev_warn(&port->dev,
0495                          "%s - xmit_empty message too short\n",
0496                          __func__);
0497                     break;
0498                 }
0499                 /* bytes_written = (ch[1] << 4) + ch[0]; */
0500                 i += 4;
0501                 escapeflag = true;
0502                 break;
0503             case QT2_CHANGE_PORT:
0504                 if (i > (len - 4)) {
0505                     dev_warn(&port->dev,
0506                          "%s - change_port message too short\n",
0507                          __func__);
0508                     break;
0509                 }
0510                 tty_flip_buffer_push(&port->port);
0511 
0512                 newport = *(ch + 3);
0513 
0514                 if (newport > serial->num_ports) {
0515                     dev_err(&port->dev,
0516                         "%s - port change to invalid port: %i\n",
0517                         __func__, newport);
0518                     break;
0519                 }
0520 
0521                 serial_priv->current_port = newport;
0522                 port = serial->port[serial_priv->current_port];
0523                 i += 3;
0524                 escapeflag = true;
0525                 break;
0526             case QT2_REC_FLUSH:
0527             case QT2_XMIT_FLUSH:
0528                 i += 2;
0529                 escapeflag = true;
0530                 break;
0531             case QT2_CONTROL_ESCAPE:
0532                 tty_insert_flip_string(&port->port, ch, 2);
0533                 i += 2;
0534                 escapeflag = true;
0535                 break;
0536             default:
0537                 dev_warn(&port->dev,
0538                      "%s - unsupported command %i\n",
0539                      __func__, *(ch + 2));
0540                 break;
0541             }
0542             if (escapeflag)
0543                 continue;
0544         }
0545 
0546         tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
0547     }
0548 
0549     tty_flip_buffer_push(&port->port);
0550 }
0551 
0552 static void qt2_write_bulk_callback(struct urb *urb)
0553 {
0554     struct usb_serial_port *port;
0555     struct qt2_port_private *port_priv;
0556     unsigned long flags;
0557 
0558     port = urb->context;
0559     port_priv = usb_get_serial_port_data(port);
0560 
0561     spin_lock_irqsave(&port_priv->urb_lock, flags);
0562 
0563     port_priv->urb_in_use = false;
0564     usb_serial_port_softint(port);
0565 
0566     spin_unlock_irqrestore(&port_priv->urb_lock, flags);
0567 
0568 }
0569 
0570 static void qt2_read_bulk_callback(struct urb *urb)
0571 {
0572     struct usb_serial *serial = urb->context;
0573     int status;
0574 
0575     if (urb->status) {
0576         dev_warn(&serial->dev->dev,
0577              "%s - non-zero urb status: %i\n", __func__,
0578              urb->status);
0579         return;
0580     }
0581 
0582     qt2_process_read_urb(urb);
0583 
0584     status = usb_submit_urb(urb, GFP_ATOMIC);
0585     if (status != 0)
0586         dev_err(&serial->dev->dev,
0587             "%s - resubmit read urb failed: %i\n",
0588             __func__, status);
0589 }
0590 
0591 static int qt2_setup_urbs(struct usb_serial *serial)
0592 {
0593     struct usb_serial_port *port0;
0594     struct qt2_serial_private *serial_priv;
0595     int status;
0596 
0597     port0 = serial->port[0];
0598 
0599     serial_priv = usb_get_serial_data(serial);
0600     serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
0601     if (!serial_priv->read_urb)
0602         return -ENOMEM;
0603 
0604     usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
0605               usb_rcvbulkpipe(serial->dev,
0606                       port0->bulk_in_endpointAddress),
0607               serial_priv->read_buffer,
0608               QT2_READ_BUFFER_SIZE,
0609               qt2_read_bulk_callback, serial);
0610 
0611     status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
0612     if (status != 0) {
0613         dev_err(&serial->dev->dev,
0614             "%s - submit read urb failed %i\n", __func__, status);
0615         usb_free_urb(serial_priv->read_urb);
0616         return status;
0617     }
0618 
0619     return 0;
0620 }
0621 
0622 static int qt2_attach(struct usb_serial *serial)
0623 {
0624     struct qt2_serial_private *serial_priv;
0625     int status;
0626 
0627     /* power on unit */
0628     status = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
0629                  0xc2, 0x40, 0x8000, 0, NULL, 0,
0630                  QT2_USB_TIMEOUT);
0631     if (status < 0) {
0632         dev_err(&serial->dev->dev,
0633             "%s - failed to power on unit: %i\n", __func__, status);
0634         return status;
0635     }
0636 
0637     serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
0638     if (!serial_priv)
0639         return -ENOMEM;
0640 
0641     serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
0642     if (!serial_priv->read_buffer) {
0643         status = -ENOMEM;
0644         goto err_buf;
0645     }
0646 
0647     usb_set_serial_data(serial, serial_priv);
0648 
0649     status = qt2_setup_urbs(serial);
0650     if (status != 0)
0651         goto attach_failed;
0652 
0653     return 0;
0654 
0655 attach_failed:
0656     kfree(serial_priv->read_buffer);
0657 err_buf:
0658     kfree(serial_priv);
0659     return status;
0660 }
0661 
0662 static int qt2_port_probe(struct usb_serial_port *port)
0663 {
0664     struct usb_serial *serial = port->serial;
0665     struct qt2_port_private *port_priv;
0666     u8 bEndpointAddress;
0667 
0668     port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
0669     if (!port_priv)
0670         return -ENOMEM;
0671 
0672     spin_lock_init(&port_priv->lock);
0673     spin_lock_init(&port_priv->urb_lock);
0674     port_priv->port = port;
0675 
0676     port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
0677     if (!port_priv->write_buffer)
0678         goto err_buf;
0679 
0680     port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
0681     if (!port_priv->write_urb)
0682         goto err_urb;
0683 
0684     bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
0685     usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
0686                 usb_sndbulkpipe(serial->dev, bEndpointAddress),
0687                 port_priv->write_buffer,
0688                 QT2_WRITE_BUFFER_SIZE,
0689                 qt2_write_bulk_callback, port);
0690 
0691     usb_set_serial_port_data(port, port_priv);
0692 
0693     return 0;
0694 err_urb:
0695     kfree(port_priv->write_buffer);
0696 err_buf:
0697     kfree(port_priv);
0698     return -ENOMEM;
0699 }
0700 
0701 static void qt2_port_remove(struct usb_serial_port *port)
0702 {
0703     struct qt2_port_private *port_priv;
0704 
0705     port_priv = usb_get_serial_port_data(port);
0706     usb_free_urb(port_priv->write_urb);
0707     kfree(port_priv->write_buffer);
0708     kfree(port_priv);
0709 }
0710 
0711 static int qt2_tiocmget(struct tty_struct *tty)
0712 {
0713     struct usb_serial_port *port = tty->driver_data;
0714     struct usb_device *dev = port->serial->dev;
0715     struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
0716     u8 *d;
0717     int r;
0718 
0719     d = kzalloc(2, GFP_KERNEL);
0720     if (!d)
0721         return -ENOMEM;
0722 
0723     r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
0724     if (r < 0)
0725         goto mget_out;
0726 
0727     r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
0728     if (r < 0)
0729         goto mget_out;
0730 
0731     r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
0732         (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
0733         (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
0734         (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
0735         (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
0736         (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
0737 
0738 mget_out:
0739     kfree(d);
0740     return r;
0741 }
0742 
0743 static int qt2_tiocmset(struct tty_struct *tty,
0744             unsigned int set, unsigned int clear)
0745 {
0746     struct qt2_port_private *port_priv;
0747 
0748     port_priv = usb_get_serial_port_data(tty->driver_data);
0749     return update_mctrl(port_priv, set, clear);
0750 }
0751 
0752 static void qt2_break_ctl(struct tty_struct *tty, int break_state)
0753 {
0754     struct usb_serial_port *port = tty->driver_data;
0755     struct qt2_port_private *port_priv;
0756     int status;
0757     u16 val;
0758 
0759     port_priv = usb_get_serial_port_data(port);
0760 
0761     val = (break_state == -1) ? 1 : 0;
0762 
0763     status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
0764                  val, port_priv->device_port);
0765     if (status < 0)
0766         dev_warn(&port->dev,
0767              "%s - failed to send control message: %i\n", __func__,
0768              status);
0769 }
0770 
0771 
0772 
0773 static void qt2_dtr_rts(struct usb_serial_port *port, int on)
0774 {
0775     struct usb_device *dev = port->serial->dev;
0776     struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
0777 
0778     /* Disable flow control */
0779     if (!on) {
0780         if (qt2_setregister(dev, port_priv->device_port,
0781                        UART_MCR, 0) < 0)
0782             dev_warn(&port->dev, "error from flowcontrol urb\n");
0783     }
0784     /* drop RTS and DTR */
0785     if (on)
0786         update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
0787     else
0788         update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
0789 }
0790 
0791 static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
0792 {
0793     struct qt2_port_private *port_priv;
0794     u8 newMSR = (u8) *ch;
0795     unsigned long flags;
0796 
0797     /* May be called from qt2_process_read_urb() for an unbound port. */
0798     port_priv = usb_get_serial_port_data(port);
0799     if (!port_priv)
0800         return;
0801 
0802     spin_lock_irqsave(&port_priv->lock, flags);
0803     port_priv->shadowMSR = newMSR;
0804     spin_unlock_irqrestore(&port_priv->lock, flags);
0805 
0806     if (newMSR & UART_MSR_ANY_DELTA) {
0807         /* update input line counters */
0808         if (newMSR & UART_MSR_DCTS)
0809             port->icount.cts++;
0810         if (newMSR & UART_MSR_DDSR)
0811             port->icount.dsr++;
0812         if (newMSR & UART_MSR_DDCD)
0813             port->icount.dcd++;
0814         if (newMSR & UART_MSR_TERI)
0815             port->icount.rng++;
0816 
0817         wake_up_interruptible(&port->port.delta_msr_wait);
0818     }
0819 }
0820 
0821 static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
0822 {
0823     struct qt2_port_private *port_priv;
0824     struct async_icount *icount;
0825     unsigned long flags;
0826     u8 newLSR = (u8) *ch;
0827 
0828     /* May be called from qt2_process_read_urb() for an unbound port. */
0829     port_priv = usb_get_serial_port_data(port);
0830     if (!port_priv)
0831         return;
0832 
0833     if (newLSR & UART_LSR_BI)
0834         newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
0835 
0836     spin_lock_irqsave(&port_priv->lock, flags);
0837     port_priv->shadowLSR = newLSR;
0838     spin_unlock_irqrestore(&port_priv->lock, flags);
0839 
0840     icount = &port->icount;
0841 
0842     if (newLSR & UART_LSR_BRK_ERROR_BITS) {
0843 
0844         if (newLSR & UART_LSR_BI)
0845             icount->brk++;
0846 
0847         if (newLSR & UART_LSR_OE)
0848             icount->overrun++;
0849 
0850         if (newLSR & UART_LSR_PE)
0851             icount->parity++;
0852 
0853         if (newLSR & UART_LSR_FE)
0854             icount->frame++;
0855     }
0856 
0857 }
0858 
0859 static unsigned int qt2_write_room(struct tty_struct *tty)
0860 {
0861     struct usb_serial_port *port = tty->driver_data;
0862     struct qt2_port_private *port_priv;
0863     unsigned long flags;
0864     unsigned int r;
0865 
0866     port_priv = usb_get_serial_port_data(port);
0867 
0868     spin_lock_irqsave(&port_priv->urb_lock, flags);
0869 
0870     if (port_priv->urb_in_use)
0871         r = 0;
0872     else
0873         r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
0874 
0875     spin_unlock_irqrestore(&port_priv->urb_lock, flags);
0876 
0877     return r;
0878 }
0879 
0880 static int qt2_write(struct tty_struct *tty,
0881              struct usb_serial_port *port,
0882              const unsigned char *buf, int count)
0883 {
0884     struct qt2_port_private *port_priv;
0885     struct urb *write_urb;
0886     unsigned char *data;
0887     unsigned long flags;
0888     int status;
0889     int bytes_out = 0;
0890 
0891     port_priv = usb_get_serial_port_data(port);
0892 
0893     if (port_priv->write_urb == NULL) {
0894         dev_err(&port->dev, "%s - no output urb\n", __func__);
0895         return 0;
0896     }
0897     write_urb = port_priv->write_urb;
0898 
0899     count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
0900 
0901     data = write_urb->transfer_buffer;
0902     spin_lock_irqsave(&port_priv->urb_lock, flags);
0903     if (port_priv->urb_in_use) {
0904         dev_err(&port->dev, "qt2_write - urb is in use\n");
0905         goto write_out;
0906     }
0907 
0908     *data++ = QT2_CONTROL_BYTE;
0909     *data++ = QT2_CONTROL_BYTE;
0910     *data++ = port_priv->device_port;
0911     put_unaligned_le16(count, data);
0912     data += 2;
0913     memcpy(data, buf, count);
0914 
0915     write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
0916 
0917     status = usb_submit_urb(write_urb, GFP_ATOMIC);
0918     if (status == 0) {
0919         port_priv->urb_in_use = true;
0920         bytes_out += count;
0921     }
0922 
0923 write_out:
0924     spin_unlock_irqrestore(&port_priv->urb_lock, flags);
0925     return bytes_out;
0926 }
0927 
0928 
0929 static struct usb_serial_driver qt2_device = {
0930     .driver = {
0931         .owner = THIS_MODULE,
0932         .name = "quatech-serial",
0933     },
0934     .description         = DRIVER_DESC,
0935     .id_table        = id_table,
0936     .open            = qt2_open,
0937     .close           = qt2_close,
0938     .write               = qt2_write,
0939     .write_room          = qt2_write_room,
0940     .calc_num_ports      = qt2_calc_num_ports,
0941     .attach              = qt2_attach,
0942     .release             = qt2_release,
0943     .disconnect          = qt2_disconnect,
0944     .port_probe          = qt2_port_probe,
0945     .port_remove         = qt2_port_remove,
0946     .dtr_rts             = qt2_dtr_rts,
0947     .break_ctl           = qt2_break_ctl,
0948     .tiocmget            = qt2_tiocmget,
0949     .tiocmset            = qt2_tiocmset,
0950     .tiocmiwait          = usb_serial_generic_tiocmiwait,
0951     .get_icount      = usb_serial_generic_get_icount,
0952     .set_termios         = qt2_set_termios,
0953 };
0954 
0955 static struct usb_serial_driver *const serial_drivers[] = {
0956     &qt2_device, NULL
0957 };
0958 
0959 module_usb_serial_driver(serial_drivers, id_table);
0960 
0961 MODULE_DESCRIPTION(DRIVER_DESC);
0962 MODULE_LICENSE("GPL v2");