Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
0004  *
0005  * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
0006  * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
0007  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
0008  * Copyright (C) 2003 IBM Corp.
0009  *
0010  * Many thanks to the authors of pl2303 driver: all functions in this file
0011  * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
0012  *
0013  * Warning! You use this driver on your own risk! The only official
0014  * description of this device I have is datasheet from manufacturer,
0015  * and it doesn't contain almost any information needed to write a driver.
0016  * Almost all knowlegde used while writing this driver was gathered by:
0017  *  - analyzing traffic between device and the M$ Windows 2000 driver,
0018  *  - trying different bit combinations and checking pin states
0019  *    with a voltmeter,
0020  *  - receiving malformed frames and producing buffer overflows
0021  *    to learn how errors are reported,
0022  * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
0023  * CONNECTED TO IT!
0024  *
0025  * See Documentation/usb/usb-serial.rst for more information on using this
0026  * driver
0027  *
0028  * TODO:
0029  *  - implement correct flushing for ioctls and oti6858_close()
0030  *  - check how errors (rx overflow, parity error, framing error) are reported
0031  *  - implement oti6858_break_ctl()
0032  *  - implement more ioctls
0033  *  - test/implement flow control
0034  *  - allow setting custom baud rates
0035  */
0036 
0037 #include <linux/kernel.h>
0038 #include <linux/errno.h>
0039 #include <linux/slab.h>
0040 #include <linux/tty.h>
0041 #include <linux/tty_driver.h>
0042 #include <linux/tty_flip.h>
0043 #include <linux/serial.h>
0044 #include <linux/module.h>
0045 #include <linux/moduleparam.h>
0046 #include <linux/spinlock.h>
0047 #include <linux/usb.h>
0048 #include <linux/usb/serial.h>
0049 #include <linux/uaccess.h>
0050 #include <linux/kfifo.h>
0051 #include "oti6858.h"
0052 
0053 #define OTI6858_DESCRIPTION \
0054     "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
0055 #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
0056 
0057 static const struct usb_device_id id_table[] = {
0058     { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
0059     { }
0060 };
0061 
0062 MODULE_DEVICE_TABLE(usb, id_table);
0063 
0064 /* requests */
0065 #define OTI6858_REQ_GET_STATUS      (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
0066 #define OTI6858_REQ_T_GET_STATUS    0x01
0067 
0068 #define OTI6858_REQ_SET_LINE        (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
0069 #define OTI6858_REQ_T_SET_LINE      0x00
0070 
0071 #define OTI6858_REQ_CHECK_TXBUFF    (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
0072 #define OTI6858_REQ_T_CHECK_TXBUFF  0x00
0073 
0074 /* format of the control packet */
0075 struct oti6858_control_pkt {
0076     __le16  divisor;    /* baud rate = 96000000 / (16 * divisor), LE */
0077 #define OTI6858_MAX_BAUD_RATE   3000000
0078     u8  frame_fmt;
0079 #define FMT_STOP_BITS_MASK  0xc0
0080 #define FMT_STOP_BITS_1     0x00
0081 #define FMT_STOP_BITS_2     0x40    /* 1.5 stop bits if FMT_DATA_BITS_5 */
0082 #define FMT_PARITY_MASK     0x38
0083 #define FMT_PARITY_NONE     0x00
0084 #define FMT_PARITY_ODD      0x08
0085 #define FMT_PARITY_EVEN     0x18
0086 #define FMT_PARITY_MARK     0x28
0087 #define FMT_PARITY_SPACE    0x38
0088 #define FMT_DATA_BITS_MASK  0x03
0089 #define FMT_DATA_BITS_5     0x00
0090 #define FMT_DATA_BITS_6     0x01
0091 #define FMT_DATA_BITS_7     0x02
0092 #define FMT_DATA_BITS_8     0x03
0093     u8  something;  /* always equals 0x43 */
0094     u8  control;    /* settings of flow control lines */
0095 #define CONTROL_MASK        0x0c
0096 #define CONTROL_DTR_HIGH    0x08
0097 #define CONTROL_RTS_HIGH    0x04
0098     u8  tx_status;
0099 #define TX_BUFFER_EMPTIED   0x09
0100     u8  pin_state;
0101 #define PIN_MASK        0x3f
0102 #define PIN_MSR_MASK        0x1b
0103 #define PIN_RTS         0x20    /* output pin */
0104 #define PIN_CTS         0x10    /* input pin, active low */
0105 #define PIN_DSR         0x08    /* input pin, active low */
0106 #define PIN_DTR         0x04    /* output pin */
0107 #define PIN_RI          0x02    /* input pin, active low */
0108 #define PIN_DCD         0x01    /* input pin, active low */
0109     u8  rx_bytes_avail;     /* number of bytes in rx buffer */;
0110 };
0111 
0112 #define OTI6858_CTRL_PKT_SIZE   sizeof(struct oti6858_control_pkt)
0113 #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
0114     (((a)->divisor == (priv)->pending_setup.divisor) \
0115       && ((a)->control == (priv)->pending_setup.control) \
0116       && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
0117 
0118 /* function prototypes */
0119 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
0120 static void oti6858_close(struct usb_serial_port *port);
0121 static void oti6858_set_termios(struct tty_struct *tty,
0122             struct usb_serial_port *port, struct ktermios *old);
0123 static void oti6858_init_termios(struct tty_struct *tty);
0124 static void oti6858_read_int_callback(struct urb *urb);
0125 static void oti6858_read_bulk_callback(struct urb *urb);
0126 static void oti6858_write_bulk_callback(struct urb *urb);
0127 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
0128             const unsigned char *buf, int count);
0129 static unsigned int oti6858_write_room(struct tty_struct *tty);
0130 static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty);
0131 static int oti6858_tiocmget(struct tty_struct *tty);
0132 static int oti6858_tiocmset(struct tty_struct *tty,
0133                 unsigned int set, unsigned int clear);
0134 static int oti6858_port_probe(struct usb_serial_port *port);
0135 static void oti6858_port_remove(struct usb_serial_port *port);
0136 
0137 /* device info */
0138 static struct usb_serial_driver oti6858_device = {
0139     .driver = {
0140         .owner =    THIS_MODULE,
0141         .name =     "oti6858",
0142     },
0143     .id_table =     id_table,
0144     .num_ports =        1,
0145     .num_bulk_in =      1,
0146     .num_bulk_out =     1,
0147     .num_interrupt_in = 1,
0148     .open =         oti6858_open,
0149     .close =        oti6858_close,
0150     .write =        oti6858_write,
0151     .set_termios =      oti6858_set_termios,
0152     .init_termios =     oti6858_init_termios,
0153     .tiocmget =     oti6858_tiocmget,
0154     .tiocmset =     oti6858_tiocmset,
0155     .tiocmiwait =       usb_serial_generic_tiocmiwait,
0156     .read_bulk_callback =   oti6858_read_bulk_callback,
0157     .read_int_callback =    oti6858_read_int_callback,
0158     .write_bulk_callback =  oti6858_write_bulk_callback,
0159     .write_room =       oti6858_write_room,
0160     .chars_in_buffer =  oti6858_chars_in_buffer,
0161     .port_probe =       oti6858_port_probe,
0162     .port_remove =      oti6858_port_remove,
0163 };
0164 
0165 static struct usb_serial_driver * const serial_drivers[] = {
0166     &oti6858_device, NULL
0167 };
0168 
0169 struct oti6858_private {
0170     spinlock_t lock;
0171 
0172     struct oti6858_control_pkt status;
0173 
0174     struct {
0175         u8 read_urb_in_use;
0176         u8 write_urb_in_use;
0177     } flags;
0178     struct delayed_work delayed_write_work;
0179 
0180     struct {
0181         __le16 divisor;
0182         u8 frame_fmt;
0183         u8 control;
0184     } pending_setup;
0185     u8 transient;
0186     u8 setup_done;
0187     struct delayed_work delayed_setup_work;
0188 
0189     struct usb_serial_port *port;   /* USB port with which associated */
0190 };
0191 
0192 static void setup_line(struct work_struct *work)
0193 {
0194     struct oti6858_private *priv = container_of(work,
0195             struct oti6858_private, delayed_setup_work.work);
0196     struct usb_serial_port *port = priv->port;
0197     struct oti6858_control_pkt *new_setup;
0198     unsigned long flags;
0199     int result;
0200 
0201     new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
0202     if (!new_setup) {
0203         /* we will try again */
0204         schedule_delayed_work(&priv->delayed_setup_work,
0205                         msecs_to_jiffies(2));
0206         return;
0207     }
0208 
0209     result = usb_control_msg(port->serial->dev,
0210                 usb_rcvctrlpipe(port->serial->dev, 0),
0211                 OTI6858_REQ_T_GET_STATUS,
0212                 OTI6858_REQ_GET_STATUS,
0213                 0, 0,
0214                 new_setup, OTI6858_CTRL_PKT_SIZE,
0215                 100);
0216 
0217     if (result != OTI6858_CTRL_PKT_SIZE) {
0218         dev_err(&port->dev, "%s(): error reading status\n", __func__);
0219         kfree(new_setup);
0220         /* we will try again */
0221         schedule_delayed_work(&priv->delayed_setup_work,
0222                             msecs_to_jiffies(2));
0223         return;
0224     }
0225 
0226     spin_lock_irqsave(&priv->lock, flags);
0227     if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
0228         new_setup->divisor = priv->pending_setup.divisor;
0229         new_setup->control = priv->pending_setup.control;
0230         new_setup->frame_fmt = priv->pending_setup.frame_fmt;
0231 
0232         spin_unlock_irqrestore(&priv->lock, flags);
0233         result = usb_control_msg(port->serial->dev,
0234                     usb_sndctrlpipe(port->serial->dev, 0),
0235                     OTI6858_REQ_T_SET_LINE,
0236                     OTI6858_REQ_SET_LINE,
0237                     0, 0,
0238                     new_setup, OTI6858_CTRL_PKT_SIZE,
0239                     100);
0240     } else {
0241         spin_unlock_irqrestore(&priv->lock, flags);
0242         result = 0;
0243     }
0244     kfree(new_setup);
0245 
0246     spin_lock_irqsave(&priv->lock, flags);
0247     if (result != OTI6858_CTRL_PKT_SIZE)
0248         priv->transient = 0;
0249     priv->setup_done = 1;
0250     spin_unlock_irqrestore(&priv->lock, flags);
0251 
0252     dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
0253     result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0254     if (result != 0) {
0255         dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
0256             __func__, result);
0257     }
0258 }
0259 
0260 static void send_data(struct work_struct *work)
0261 {
0262     struct oti6858_private *priv = container_of(work,
0263             struct oti6858_private, delayed_write_work.work);
0264     struct usb_serial_port *port = priv->port;
0265     int count = 0, result;
0266     unsigned long flags;
0267     u8 *allow;
0268 
0269     spin_lock_irqsave(&priv->lock, flags);
0270     if (priv->flags.write_urb_in_use) {
0271         spin_unlock_irqrestore(&priv->lock, flags);
0272         schedule_delayed_work(&priv->delayed_write_work,
0273                         msecs_to_jiffies(2));
0274         return;
0275     }
0276     priv->flags.write_urb_in_use = 1;
0277     spin_unlock_irqrestore(&priv->lock, flags);
0278 
0279     spin_lock_irqsave(&port->lock, flags);
0280     count = kfifo_len(&port->write_fifo);
0281     spin_unlock_irqrestore(&port->lock, flags);
0282 
0283     if (count > port->bulk_out_size)
0284         count = port->bulk_out_size;
0285 
0286     if (count != 0) {
0287         allow = kmalloc(1, GFP_KERNEL);
0288         if (!allow)
0289             return;
0290 
0291         result = usb_control_msg(port->serial->dev,
0292                 usb_rcvctrlpipe(port->serial->dev, 0),
0293                 OTI6858_REQ_T_CHECK_TXBUFF,
0294                 OTI6858_REQ_CHECK_TXBUFF,
0295                 count, 0, allow, 1, 100);
0296         if (result != 1 || *allow != 0)
0297             count = 0;
0298         kfree(allow);
0299     }
0300 
0301     if (count == 0) {
0302         priv->flags.write_urb_in_use = 0;
0303 
0304         dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
0305         result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
0306         if (result != 0) {
0307             dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
0308                 __func__, result);
0309         }
0310         return;
0311     }
0312 
0313     count = kfifo_out_locked(&port->write_fifo,
0314                     port->write_urb->transfer_buffer,
0315                     count, &port->lock);
0316     port->write_urb->transfer_buffer_length = count;
0317     result = usb_submit_urb(port->write_urb, GFP_NOIO);
0318     if (result != 0) {
0319         dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
0320                 __func__, result);
0321         priv->flags.write_urb_in_use = 0;
0322     }
0323 
0324     usb_serial_port_softint(port);
0325 }
0326 
0327 static int oti6858_port_probe(struct usb_serial_port *port)
0328 {
0329     struct oti6858_private *priv;
0330 
0331     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0332     if (!priv)
0333         return -ENOMEM;
0334 
0335     spin_lock_init(&priv->lock);
0336     priv->port = port;
0337     INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
0338     INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
0339 
0340     usb_set_serial_port_data(port, priv);
0341 
0342     port->port.drain_delay = 256;   /* FIXME: check the FIFO length */
0343 
0344     return 0;
0345 }
0346 
0347 static void oti6858_port_remove(struct usb_serial_port *port)
0348 {
0349     struct oti6858_private *priv;
0350 
0351     priv = usb_get_serial_port_data(port);
0352     kfree(priv);
0353 }
0354 
0355 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
0356             const unsigned char *buf, int count)
0357 {
0358     if (!count)
0359         return count;
0360 
0361     count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
0362 
0363     return count;
0364 }
0365 
0366 static unsigned int oti6858_write_room(struct tty_struct *tty)
0367 {
0368     struct usb_serial_port *port = tty->driver_data;
0369     unsigned int room;
0370     unsigned long flags;
0371 
0372     spin_lock_irqsave(&port->lock, flags);
0373     room = kfifo_avail(&port->write_fifo);
0374     spin_unlock_irqrestore(&port->lock, flags);
0375 
0376     return room;
0377 }
0378 
0379 static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty)
0380 {
0381     struct usb_serial_port *port = tty->driver_data;
0382     unsigned int chars;
0383     unsigned long flags;
0384 
0385     spin_lock_irqsave(&port->lock, flags);
0386     chars = kfifo_len(&port->write_fifo);
0387     spin_unlock_irqrestore(&port->lock, flags);
0388 
0389     return chars;
0390 }
0391 
0392 static void oti6858_init_termios(struct tty_struct *tty)
0393 {
0394     tty_encode_baud_rate(tty, 38400, 38400);
0395 }
0396 
0397 static void oti6858_set_termios(struct tty_struct *tty,
0398         struct usb_serial_port *port, struct ktermios *old_termios)
0399 {
0400     struct oti6858_private *priv = usb_get_serial_port_data(port);
0401     unsigned long flags;
0402     unsigned int cflag;
0403     u8 frame_fmt, control;
0404     __le16 divisor;
0405     int br;
0406 
0407     cflag = tty->termios.c_cflag;
0408 
0409     spin_lock_irqsave(&priv->lock, flags);
0410     divisor = priv->pending_setup.divisor;
0411     frame_fmt = priv->pending_setup.frame_fmt;
0412     control = priv->pending_setup.control;
0413     spin_unlock_irqrestore(&priv->lock, flags);
0414 
0415     frame_fmt &= ~FMT_DATA_BITS_MASK;
0416     switch (cflag & CSIZE) {
0417     case CS5:
0418         frame_fmt |= FMT_DATA_BITS_5;
0419         break;
0420     case CS6:
0421         frame_fmt |= FMT_DATA_BITS_6;
0422         break;
0423     case CS7:
0424         frame_fmt |= FMT_DATA_BITS_7;
0425         break;
0426     default:
0427     case CS8:
0428         frame_fmt |= FMT_DATA_BITS_8;
0429         break;
0430     }
0431 
0432     /* manufacturer claims that this device can work with baud rates
0433      * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
0434      * guarantee that any other baud rate will work (especially
0435      * the higher ones)
0436      */
0437     br = tty_get_baud_rate(tty);
0438     if (br == 0) {
0439         divisor = 0;
0440     } else {
0441         int real_br;
0442         int new_divisor;
0443         br = min(br, OTI6858_MAX_BAUD_RATE);
0444 
0445         new_divisor = (96000000 + 8 * br) / (16 * br);
0446         real_br = 96000000 / (16 * new_divisor);
0447         divisor = cpu_to_le16(new_divisor);
0448         tty_encode_baud_rate(tty, real_br, real_br);
0449     }
0450 
0451     frame_fmt &= ~FMT_STOP_BITS_MASK;
0452     if ((cflag & CSTOPB) != 0)
0453         frame_fmt |= FMT_STOP_BITS_2;
0454     else
0455         frame_fmt |= FMT_STOP_BITS_1;
0456 
0457     frame_fmt &= ~FMT_PARITY_MASK;
0458     if ((cflag & PARENB) != 0) {
0459         if ((cflag & PARODD) != 0)
0460             frame_fmt |= FMT_PARITY_ODD;
0461         else
0462             frame_fmt |= FMT_PARITY_EVEN;
0463     } else {
0464         frame_fmt |= FMT_PARITY_NONE;
0465     }
0466 
0467     control &= ~CONTROL_MASK;
0468     if ((cflag & CRTSCTS) != 0)
0469         control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
0470 
0471     /* change control lines if we are switching to or from B0 */
0472     /* FIXME:
0473     spin_lock_irqsave(&priv->lock, flags);
0474     control = priv->line_control;
0475     if ((cflag & CBAUD) == B0)
0476         priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
0477     else
0478         priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
0479     if (control != priv->line_control) {
0480         control = priv->line_control;
0481         spin_unlock_irqrestore(&priv->lock, flags);
0482         set_control_lines(serial->dev, control);
0483     } else {
0484         spin_unlock_irqrestore(&priv->lock, flags);
0485     }
0486     */
0487 
0488     spin_lock_irqsave(&priv->lock, flags);
0489     if (divisor != priv->pending_setup.divisor
0490             || control != priv->pending_setup.control
0491             || frame_fmt != priv->pending_setup.frame_fmt) {
0492         priv->pending_setup.divisor = divisor;
0493         priv->pending_setup.control = control;
0494         priv->pending_setup.frame_fmt = frame_fmt;
0495     }
0496     spin_unlock_irqrestore(&priv->lock, flags);
0497 }
0498 
0499 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
0500 {
0501     struct oti6858_private *priv = usb_get_serial_port_data(port);
0502     struct usb_serial *serial = port->serial;
0503     struct oti6858_control_pkt *buf;
0504     unsigned long flags;
0505     int result;
0506 
0507     usb_clear_halt(serial->dev, port->write_urb->pipe);
0508     usb_clear_halt(serial->dev, port->read_urb->pipe);
0509 
0510     buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
0511     if (!buf)
0512         return -ENOMEM;
0513 
0514     result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
0515                 OTI6858_REQ_T_GET_STATUS,
0516                 OTI6858_REQ_GET_STATUS,
0517                 0, 0,
0518                 buf, OTI6858_CTRL_PKT_SIZE,
0519                 100);
0520     if (result != OTI6858_CTRL_PKT_SIZE) {
0521         /* assume default (after power-on reset) values */
0522         buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
0523         buf->frame_fmt = 0x03;  /* 8N1 */
0524         buf->something = 0x43;
0525         buf->control = 0x4c;    /* DTR, RTS */
0526         buf->tx_status = 0x00;
0527         buf->pin_state = 0x5b;  /* RTS, CTS, DSR, DTR, RI, DCD */
0528         buf->rx_bytes_avail = 0x00;
0529     }
0530 
0531     spin_lock_irqsave(&priv->lock, flags);
0532     memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
0533     priv->pending_setup.divisor = buf->divisor;
0534     priv->pending_setup.frame_fmt = buf->frame_fmt;
0535     priv->pending_setup.control = buf->control;
0536     spin_unlock_irqrestore(&priv->lock, flags);
0537     kfree(buf);
0538 
0539     dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
0540     result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0541     if (result != 0) {
0542         dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
0543             __func__, result);
0544         oti6858_close(port);
0545         return result;
0546     }
0547 
0548     /* setup termios */
0549     if (tty)
0550         oti6858_set_termios(tty, port, NULL);
0551 
0552     return 0;
0553 }
0554 
0555 static void oti6858_close(struct usb_serial_port *port)
0556 {
0557     struct oti6858_private *priv = usb_get_serial_port_data(port);
0558     unsigned long flags;
0559 
0560     spin_lock_irqsave(&port->lock, flags);
0561     /* clear out any remaining data in the buffer */
0562     kfifo_reset_out(&port->write_fifo);
0563     spin_unlock_irqrestore(&port->lock, flags);
0564 
0565     dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
0566 
0567     /* cancel scheduled setup */
0568     cancel_delayed_work_sync(&priv->delayed_setup_work);
0569     cancel_delayed_work_sync(&priv->delayed_write_work);
0570 
0571     /* shutdown our urbs */
0572     dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
0573     usb_kill_urb(port->write_urb);
0574     usb_kill_urb(port->read_urb);
0575     usb_kill_urb(port->interrupt_in_urb);
0576 }
0577 
0578 static int oti6858_tiocmset(struct tty_struct *tty,
0579                 unsigned int set, unsigned int clear)
0580 {
0581     struct usb_serial_port *port = tty->driver_data;
0582     struct oti6858_private *priv = usb_get_serial_port_data(port);
0583     unsigned long flags;
0584     u8 control;
0585 
0586     dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
0587         __func__, set, clear);
0588 
0589     /* FIXME: check if this is correct (active high/low) */
0590     spin_lock_irqsave(&priv->lock, flags);
0591     control = priv->pending_setup.control;
0592     if ((set & TIOCM_RTS) != 0)
0593         control |= CONTROL_RTS_HIGH;
0594     if ((set & TIOCM_DTR) != 0)
0595         control |= CONTROL_DTR_HIGH;
0596     if ((clear & TIOCM_RTS) != 0)
0597         control &= ~CONTROL_RTS_HIGH;
0598     if ((clear & TIOCM_DTR) != 0)
0599         control &= ~CONTROL_DTR_HIGH;
0600 
0601     if (control != priv->pending_setup.control)
0602         priv->pending_setup.control = control;
0603 
0604     spin_unlock_irqrestore(&priv->lock, flags);
0605     return 0;
0606 }
0607 
0608 static int oti6858_tiocmget(struct tty_struct *tty)
0609 {
0610     struct usb_serial_port *port = tty->driver_data;
0611     struct oti6858_private *priv = usb_get_serial_port_data(port);
0612     unsigned long flags;
0613     unsigned pin_state;
0614     unsigned result = 0;
0615 
0616     spin_lock_irqsave(&priv->lock, flags);
0617     pin_state = priv->status.pin_state & PIN_MASK;
0618     spin_unlock_irqrestore(&priv->lock, flags);
0619 
0620     /* FIXME: check if this is correct (active high/low) */
0621     if ((pin_state & PIN_RTS) != 0)
0622         result |= TIOCM_RTS;
0623     if ((pin_state & PIN_CTS) != 0)
0624         result |= TIOCM_CTS;
0625     if ((pin_state & PIN_DSR) != 0)
0626         result |= TIOCM_DSR;
0627     if ((pin_state & PIN_DTR) != 0)
0628         result |= TIOCM_DTR;
0629     if ((pin_state & PIN_RI) != 0)
0630         result |= TIOCM_RI;
0631     if ((pin_state & PIN_DCD) != 0)
0632         result |= TIOCM_CD;
0633 
0634     dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
0635 
0636     return result;
0637 }
0638 
0639 static void oti6858_read_int_callback(struct urb *urb)
0640 {
0641     struct usb_serial_port *port =  urb->context;
0642     struct oti6858_private *priv = usb_get_serial_port_data(port);
0643     int transient = 0, can_recv = 0, resubmit = 1;
0644     int status = urb->status;
0645 
0646     switch (status) {
0647     case 0:
0648         /* success */
0649         break;
0650     case -ECONNRESET:
0651     case -ENOENT:
0652     case -ESHUTDOWN:
0653         /* this urb is terminated, clean up */
0654         dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
0655             __func__, status);
0656         return;
0657     default:
0658         dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
0659             __func__, status);
0660         break;
0661     }
0662 
0663     if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
0664         struct oti6858_control_pkt *xs = urb->transfer_buffer;
0665         unsigned long flags;
0666 
0667         spin_lock_irqsave(&priv->lock, flags);
0668 
0669         if (!priv->transient) {
0670             if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
0671                 if (xs->rx_bytes_avail == 0) {
0672                     priv->transient = 4;
0673                     priv->setup_done = 0;
0674                     resubmit = 0;
0675                     dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
0676                     schedule_delayed_work(&priv->delayed_setup_work, 0);
0677                 }
0678             }
0679         } else {
0680             if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
0681                 priv->transient = 0;
0682             } else if (!priv->setup_done) {
0683                 resubmit = 0;
0684             } else if (--priv->transient == 0) {
0685                 if (xs->rx_bytes_avail == 0) {
0686                     priv->transient = 4;
0687                     priv->setup_done = 0;
0688                     resubmit = 0;
0689                     dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
0690                     schedule_delayed_work(&priv->delayed_setup_work, 0);
0691                 }
0692             }
0693         }
0694 
0695         if (!priv->transient) {
0696             u8 delta = xs->pin_state ^ priv->status.pin_state;
0697 
0698             if (delta & PIN_MSR_MASK) {
0699                 if (delta & PIN_CTS)
0700                     port->icount.cts++;
0701                 if (delta & PIN_DSR)
0702                     port->icount.dsr++;
0703                 if (delta & PIN_RI)
0704                     port->icount.rng++;
0705                 if (delta & PIN_DCD)
0706                     port->icount.dcd++;
0707 
0708                 wake_up_interruptible(&port->port.delta_msr_wait);
0709             }
0710 
0711             memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
0712         }
0713 
0714         if (!priv->transient && xs->rx_bytes_avail != 0) {
0715             can_recv = xs->rx_bytes_avail;
0716             priv->flags.read_urb_in_use = 1;
0717         }
0718 
0719         transient = priv->transient;
0720         spin_unlock_irqrestore(&priv->lock, flags);
0721     }
0722 
0723     if (can_recv) {
0724         int result;
0725 
0726         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
0727         if (result != 0) {
0728             priv->flags.read_urb_in_use = 0;
0729             dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
0730                     " error %d\n", __func__, result);
0731         } else {
0732             resubmit = 0;
0733         }
0734     } else if (!transient) {
0735         unsigned long flags;
0736         int count;
0737 
0738         spin_lock_irqsave(&port->lock, flags);
0739         count = kfifo_len(&port->write_fifo);
0740         spin_unlock_irqrestore(&port->lock, flags);
0741 
0742         spin_lock_irqsave(&priv->lock, flags);
0743         if (priv->flags.write_urb_in_use == 0 && count != 0) {
0744             schedule_delayed_work(&priv->delayed_write_work, 0);
0745             resubmit = 0;
0746         }
0747         spin_unlock_irqrestore(&priv->lock, flags);
0748     }
0749 
0750     if (resubmit) {
0751         int result;
0752 
0753 /*      dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
0754         result = usb_submit_urb(urb, GFP_ATOMIC);
0755         if (result != 0) {
0756             dev_err(&urb->dev->dev,
0757                     "%s(): usb_submit_urb() failed with"
0758                     " error %d\n", __func__, result);
0759         }
0760     }
0761 }
0762 
0763 static void oti6858_read_bulk_callback(struct urb *urb)
0764 {
0765     struct usb_serial_port *port =  urb->context;
0766     struct oti6858_private *priv = usb_get_serial_port_data(port);
0767     unsigned char *data = urb->transfer_buffer;
0768     unsigned long flags;
0769     int status = urb->status;
0770     int result;
0771 
0772     spin_lock_irqsave(&priv->lock, flags);
0773     priv->flags.read_urb_in_use = 0;
0774     spin_unlock_irqrestore(&priv->lock, flags);
0775 
0776     if (status != 0) {
0777         dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
0778         return;
0779     }
0780 
0781     if (urb->actual_length > 0) {
0782         tty_insert_flip_string(&port->port, data, urb->actual_length);
0783         tty_flip_buffer_push(&port->port);
0784     }
0785 
0786     /* schedule the interrupt urb */
0787     result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
0788     if (result != 0 && result != -EPERM) {
0789         dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
0790                 " error %d\n", __func__, result);
0791     }
0792 }
0793 
0794 static void oti6858_write_bulk_callback(struct urb *urb)
0795 {
0796     struct usb_serial_port *port =  urb->context;
0797     struct oti6858_private *priv = usb_get_serial_port_data(port);
0798     int status = urb->status;
0799     int result;
0800 
0801     switch (status) {
0802     case 0:
0803         /* success */
0804         break;
0805     case -ECONNRESET:
0806     case -ENOENT:
0807     case -ESHUTDOWN:
0808         /* this urb is terminated, clean up */
0809         dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
0810         priv->flags.write_urb_in_use = 0;
0811         return;
0812     default:
0813         /* error in the urb, so we have to resubmit it */
0814         dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
0815         dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
0816 
0817         port->write_urb->transfer_buffer_length = 1;
0818         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
0819         if (result) {
0820             dev_err_console(port, "%s(): usb_submit_urb() failed,"
0821                     " error %d\n", __func__, result);
0822         } else {
0823             return;
0824         }
0825     }
0826 
0827     priv->flags.write_urb_in_use = 0;
0828 
0829     /* schedule the interrupt urb if we are still open */
0830     dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
0831     result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
0832     if (result != 0) {
0833         dev_err(&port->dev, "%s(): failed submitting int urb,"
0834                     " error %d\n", __func__, result);
0835     }
0836 }
0837 
0838 module_usb_serial_driver(serial_drivers, id_table);
0839 
0840 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
0841 MODULE_AUTHOR(OTI6858_AUTHOR);
0842 MODULE_LICENSE("GPL v2");