Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003 *  Digi AccelePort USB-4 and USB-2 Serial Converters
0004 *
0005 *  Copyright 2000 by Digi International
0006 *
0007 *  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
0008 *  usb-serial driver.
0009 *
0010 *  Peter Berger (pberger@brimson.com)
0011 *  Al Borchers (borchers@steinerpoint.com)
0012 */
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/errno.h>
0016 #include <linux/slab.h>
0017 #include <linux/tty.h>
0018 #include <linux/tty_driver.h>
0019 #include <linux/tty_flip.h>
0020 #include <linux/module.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/uaccess.h>
0023 #include <linux/usb.h>
0024 #include <linux/wait.h>
0025 #include <linux/sched/signal.h>
0026 #include <linux/usb/serial.h>
0027 
0028 /* Defines */
0029 
0030 #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
0031 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
0032 
0033 /* port output buffer length -- must be <= transfer buffer length - 2 */
0034 /* so we can be sure to send the full buffer in one urb */
0035 #define DIGI_OUT_BUF_SIZE       8
0036 
0037 /* port input buffer length -- must be >= transfer buffer length - 3 */
0038 /* so we can be sure to hold at least one full buffer from one urb */
0039 #define DIGI_IN_BUF_SIZE        64
0040 
0041 /* retry timeout while sleeping */
0042 #define DIGI_RETRY_TIMEOUT      (HZ/10)
0043 
0044 /* timeout while waiting for tty output to drain in close */
0045 /* this delay is used twice in close, so the total delay could */
0046 /* be twice this value */
0047 #define DIGI_CLOSE_TIMEOUT      (5*HZ)
0048 
0049 
0050 /* AccelePort USB Defines */
0051 
0052 /* ids */
0053 #define DIGI_VENDOR_ID          0x05c5
0054 #define DIGI_2_ID           0x0002  /* USB-2 */
0055 #define DIGI_4_ID           0x0004  /* USB-4 */
0056 
0057 /* commands
0058  * "INB": can be used on the in-band endpoint
0059  * "OOB": can be used on the out-of-band endpoint
0060  */
0061 #define DIGI_CMD_SET_BAUD_RATE          0   /* INB, OOB */
0062 #define DIGI_CMD_SET_WORD_SIZE          1   /* INB, OOB */
0063 #define DIGI_CMD_SET_PARITY         2   /* INB, OOB */
0064 #define DIGI_CMD_SET_STOP_BITS          3   /* INB, OOB */
0065 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL     4   /* INB, OOB */
0066 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL    5   /* INB, OOB */
0067 #define DIGI_CMD_SET_DTR_SIGNAL         6   /* INB, OOB */
0068 #define DIGI_CMD_SET_RTS_SIGNAL         7   /* INB, OOB */
0069 #define DIGI_CMD_READ_INPUT_SIGNALS     8   /*      OOB */
0070 #define DIGI_CMD_IFLUSH_FIFO            9   /*      OOB */
0071 #define DIGI_CMD_RECEIVE_ENABLE         10  /* INB, OOB */
0072 #define DIGI_CMD_BREAK_CONTROL          11  /* INB, OOB */
0073 #define DIGI_CMD_LOCAL_LOOPBACK         12  /* INB, OOB */
0074 #define DIGI_CMD_TRANSMIT_IDLE          13  /* INB, OOB */
0075 #define DIGI_CMD_READ_UART_REGISTER     14  /*      OOB */
0076 #define DIGI_CMD_WRITE_UART_REGISTER        15  /* INB, OOB */
0077 #define DIGI_CMD_AND_UART_REGISTER      16  /* INB, OOB */
0078 #define DIGI_CMD_OR_UART_REGISTER       17  /* INB, OOB */
0079 #define DIGI_CMD_SEND_DATA          18  /* INB      */
0080 #define DIGI_CMD_RECEIVE_DATA           19  /* INB      */
0081 #define DIGI_CMD_RECEIVE_DISABLE        20  /* INB      */
0082 #define DIGI_CMD_GET_PORT_TYPE          21  /*      OOB */
0083 
0084 /* baud rates */
0085 #define DIGI_BAUD_50                0
0086 #define DIGI_BAUD_75                1
0087 #define DIGI_BAUD_110               2
0088 #define DIGI_BAUD_150               3
0089 #define DIGI_BAUD_200               4
0090 #define DIGI_BAUD_300               5
0091 #define DIGI_BAUD_600               6
0092 #define DIGI_BAUD_1200              7
0093 #define DIGI_BAUD_1800              8
0094 #define DIGI_BAUD_2400              9
0095 #define DIGI_BAUD_4800              10
0096 #define DIGI_BAUD_7200              11
0097 #define DIGI_BAUD_9600              12
0098 #define DIGI_BAUD_14400             13
0099 #define DIGI_BAUD_19200             14
0100 #define DIGI_BAUD_28800             15
0101 #define DIGI_BAUD_38400             16
0102 #define DIGI_BAUD_57600             17
0103 #define DIGI_BAUD_76800             18
0104 #define DIGI_BAUD_115200            19
0105 #define DIGI_BAUD_153600            20
0106 #define DIGI_BAUD_230400            21
0107 #define DIGI_BAUD_460800            22
0108 
0109 /* arguments */
0110 #define DIGI_WORD_SIZE_5            0
0111 #define DIGI_WORD_SIZE_6            1
0112 #define DIGI_WORD_SIZE_7            2
0113 #define DIGI_WORD_SIZE_8            3
0114 
0115 #define DIGI_PARITY_NONE            0
0116 #define DIGI_PARITY_ODD             1
0117 #define DIGI_PARITY_EVEN            2
0118 #define DIGI_PARITY_MARK            3
0119 #define DIGI_PARITY_SPACE           4
0120 
0121 #define DIGI_STOP_BITS_1            0
0122 #define DIGI_STOP_BITS_2            1
0123 
0124 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF    1
0125 #define DIGI_INPUT_FLOW_CONTROL_RTS     2
0126 #define DIGI_INPUT_FLOW_CONTROL_DTR     4
0127 
0128 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF   1
0129 #define DIGI_OUTPUT_FLOW_CONTROL_CTS        2
0130 #define DIGI_OUTPUT_FLOW_CONTROL_DSR        4
0131 
0132 #define DIGI_DTR_INACTIVE           0
0133 #define DIGI_DTR_ACTIVE             1
0134 #define DIGI_DTR_INPUT_FLOW_CONTROL     2
0135 
0136 #define DIGI_RTS_INACTIVE           0
0137 #define DIGI_RTS_ACTIVE             1
0138 #define DIGI_RTS_INPUT_FLOW_CONTROL     2
0139 #define DIGI_RTS_TOGGLE             3
0140 
0141 #define DIGI_FLUSH_TX               1
0142 #define DIGI_FLUSH_RX               2
0143 #define DIGI_RESUME_TX              4 /* clears xoff condition */
0144 
0145 #define DIGI_TRANSMIT_NOT_IDLE          0
0146 #define DIGI_TRANSMIT_IDLE          1
0147 
0148 #define DIGI_DISABLE                0
0149 #define DIGI_ENABLE             1
0150 
0151 #define DIGI_DEASSERT               0
0152 #define DIGI_ASSERT             1
0153 
0154 /* in band status codes */
0155 #define DIGI_OVERRUN_ERROR          4
0156 #define DIGI_PARITY_ERROR           8
0157 #define DIGI_FRAMING_ERROR          16
0158 #define DIGI_BREAK_ERROR            32
0159 
0160 /* out of band status */
0161 #define DIGI_NO_ERROR               0
0162 #define DIGI_BAD_FIRST_PARAMETER        1
0163 #define DIGI_BAD_SECOND_PARAMETER       2
0164 #define DIGI_INVALID_LINE           3
0165 #define DIGI_INVALID_OPCODE         4
0166 
0167 /* input signals */
0168 #define DIGI_READ_INPUT_SIGNALS_SLOT        1
0169 #define DIGI_READ_INPUT_SIGNALS_ERR     2
0170 #define DIGI_READ_INPUT_SIGNALS_BUSY        4
0171 #define DIGI_READ_INPUT_SIGNALS_PE      8
0172 #define DIGI_READ_INPUT_SIGNALS_CTS     16
0173 #define DIGI_READ_INPUT_SIGNALS_DSR     32
0174 #define DIGI_READ_INPUT_SIGNALS_RI      64
0175 #define DIGI_READ_INPUT_SIGNALS_DCD     128
0176 
0177 
0178 /* Structures */
0179 
0180 struct digi_serial {
0181     spinlock_t ds_serial_lock;
0182     struct usb_serial_port *ds_oob_port;    /* out-of-band port */
0183     int ds_oob_port_num;            /* index of out-of-band port */
0184     int ds_device_started;
0185 };
0186 
0187 struct digi_port {
0188     spinlock_t dp_port_lock;
0189     int dp_port_num;
0190     int dp_out_buf_len;
0191     unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
0192     int dp_write_urb_in_use;
0193     unsigned int dp_modem_signals;
0194     int dp_transmit_idle;
0195     wait_queue_head_t dp_transmit_idle_wait;
0196     int dp_throttled;
0197     int dp_throttle_restart;
0198     wait_queue_head_t dp_flush_wait;
0199     wait_queue_head_t dp_close_wait;    /* wait queue for close */
0200     wait_queue_head_t write_wait;
0201     struct usb_serial_port *dp_port;
0202 };
0203 
0204 
0205 /* Local Function Declarations */
0206 
0207 static int digi_write_oob_command(struct usb_serial_port *port,
0208     unsigned char *buf, int count, int interruptible);
0209 static int digi_write_inb_command(struct usb_serial_port *port,
0210     unsigned char *buf, int count, unsigned long timeout);
0211 static int digi_set_modem_signals(struct usb_serial_port *port,
0212     unsigned int modem_signals, int interruptible);
0213 static int digi_transmit_idle(struct usb_serial_port *port,
0214     unsigned long timeout);
0215 static void digi_rx_throttle(struct tty_struct *tty);
0216 static void digi_rx_unthrottle(struct tty_struct *tty);
0217 static void digi_set_termios(struct tty_struct *tty,
0218         struct usb_serial_port *port, struct ktermios *old_termios);
0219 static void digi_break_ctl(struct tty_struct *tty, int break_state);
0220 static int digi_tiocmget(struct tty_struct *tty);
0221 static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
0222         unsigned int clear);
0223 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
0224         const unsigned char *buf, int count);
0225 static void digi_write_bulk_callback(struct urb *urb);
0226 static unsigned int digi_write_room(struct tty_struct *tty);
0227 static unsigned int digi_chars_in_buffer(struct tty_struct *tty);
0228 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
0229 static void digi_close(struct usb_serial_port *port);
0230 static void digi_dtr_rts(struct usb_serial_port *port, int on);
0231 static int digi_startup_device(struct usb_serial *serial);
0232 static int digi_startup(struct usb_serial *serial);
0233 static void digi_disconnect(struct usb_serial *serial);
0234 static void digi_release(struct usb_serial *serial);
0235 static int digi_port_probe(struct usb_serial_port *port);
0236 static void digi_port_remove(struct usb_serial_port *port);
0237 static void digi_read_bulk_callback(struct urb *urb);
0238 static int digi_read_inb_callback(struct urb *urb);
0239 static int digi_read_oob_callback(struct urb *urb);
0240 
0241 
0242 static const struct usb_device_id id_table_combined[] = {
0243     { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
0244     { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
0245     { }                     /* Terminating entry */
0246 };
0247 
0248 static const struct usb_device_id id_table_2[] = {
0249     { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
0250     { }                     /* Terminating entry */
0251 };
0252 
0253 static const struct usb_device_id id_table_4[] = {
0254     { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
0255     { }                     /* Terminating entry */
0256 };
0257 
0258 MODULE_DEVICE_TABLE(usb, id_table_combined);
0259 
0260 /* device info needed for the Digi serial converter */
0261 
0262 static struct usb_serial_driver digi_acceleport_2_device = {
0263     .driver = {
0264         .owner =        THIS_MODULE,
0265         .name =         "digi_2",
0266     },
0267     .description =          "Digi 2 port USB adapter",
0268     .id_table =         id_table_2,
0269     .num_ports =            3,
0270     .num_bulk_in =          4,
0271     .num_bulk_out =         4,
0272     .open =             digi_open,
0273     .close =            digi_close,
0274     .dtr_rts =          digi_dtr_rts,
0275     .write =            digi_write,
0276     .write_room =           digi_write_room,
0277     .write_bulk_callback =      digi_write_bulk_callback,
0278     .read_bulk_callback =       digi_read_bulk_callback,
0279     .chars_in_buffer =      digi_chars_in_buffer,
0280     .throttle =         digi_rx_throttle,
0281     .unthrottle =           digi_rx_unthrottle,
0282     .set_termios =          digi_set_termios,
0283     .break_ctl =            digi_break_ctl,
0284     .tiocmget =         digi_tiocmget,
0285     .tiocmset =         digi_tiocmset,
0286     .attach =           digi_startup,
0287     .disconnect =           digi_disconnect,
0288     .release =          digi_release,
0289     .port_probe =           digi_port_probe,
0290     .port_remove =          digi_port_remove,
0291 };
0292 
0293 static struct usb_serial_driver digi_acceleport_4_device = {
0294     .driver = {
0295         .owner =        THIS_MODULE,
0296         .name =         "digi_4",
0297     },
0298     .description =          "Digi 4 port USB adapter",
0299     .id_table =         id_table_4,
0300     .num_ports =            4,
0301     .num_bulk_in =          5,
0302     .num_bulk_out =         5,
0303     .open =             digi_open,
0304     .close =            digi_close,
0305     .write =            digi_write,
0306     .write_room =           digi_write_room,
0307     .write_bulk_callback =      digi_write_bulk_callback,
0308     .read_bulk_callback =       digi_read_bulk_callback,
0309     .chars_in_buffer =      digi_chars_in_buffer,
0310     .throttle =         digi_rx_throttle,
0311     .unthrottle =           digi_rx_unthrottle,
0312     .set_termios =          digi_set_termios,
0313     .break_ctl =            digi_break_ctl,
0314     .tiocmget =         digi_tiocmget,
0315     .tiocmset =         digi_tiocmset,
0316     .attach =           digi_startup,
0317     .disconnect =           digi_disconnect,
0318     .release =          digi_release,
0319     .port_probe =           digi_port_probe,
0320     .port_remove =          digi_port_remove,
0321 };
0322 
0323 static struct usb_serial_driver * const serial_drivers[] = {
0324     &digi_acceleport_2_device, &digi_acceleport_4_device, NULL
0325 };
0326 
0327 /* Functions */
0328 
0329 /*
0330  *  Cond Wait Interruptible Timeout Irqrestore
0331  *
0332  *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
0333  *  so that wake ups are not lost if they occur between the unlock
0334  *  and the sleep.  In other words, spin_unlock_irqrestore and
0335  *  interruptible_sleep_on_timeout are "atomic" with respect to
0336  *  wake ups.  This is used to implement condition variables.
0337  *
0338  *  interruptible_sleep_on_timeout is deprecated and has been replaced
0339  *  with the equivalent code.
0340  */
0341 
0342 static long cond_wait_interruptible_timeout_irqrestore(
0343     wait_queue_head_t *q, long timeout,
0344     spinlock_t *lock, unsigned long flags)
0345 __releases(lock)
0346 {
0347     DEFINE_WAIT(wait);
0348 
0349     prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
0350     spin_unlock_irqrestore(lock, flags);
0351     timeout = schedule_timeout(timeout);
0352     finish_wait(q, &wait);
0353 
0354     return timeout;
0355 }
0356 
0357 /*
0358  *  Digi Write OOB Command
0359  *
0360  *  Write commands on the out of band port.  Commands are 4
0361  *  bytes each, multiple commands can be sent at once, and
0362  *  no command will be split across USB packets.  Returns 0
0363  *  if successful, -EINTR if interrupted while sleeping and
0364  *  the interruptible flag is true, or a negative error
0365  *  returned by usb_submit_urb.
0366  */
0367 
0368 static int digi_write_oob_command(struct usb_serial_port *port,
0369     unsigned char *buf, int count, int interruptible)
0370 {
0371     int ret = 0;
0372     int len;
0373     struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
0374     struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
0375     unsigned long flags;
0376 
0377     dev_dbg(&port->dev,
0378         "digi_write_oob_command: TOP: port=%d, count=%d\n",
0379         oob_priv->dp_port_num, count);
0380 
0381     spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
0382     while (count > 0) {
0383         while (oob_priv->dp_write_urb_in_use) {
0384             cond_wait_interruptible_timeout_irqrestore(
0385                 &oob_priv->write_wait, DIGI_RETRY_TIMEOUT,
0386                 &oob_priv->dp_port_lock, flags);
0387             if (interruptible && signal_pending(current))
0388                 return -EINTR;
0389             spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
0390         }
0391 
0392         /* len must be a multiple of 4, so commands are not split */
0393         len = min(count, oob_port->bulk_out_size);
0394         if (len > 4)
0395             len &= ~3;
0396         memcpy(oob_port->write_urb->transfer_buffer, buf, len);
0397         oob_port->write_urb->transfer_buffer_length = len;
0398         ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
0399         if (ret == 0) {
0400             oob_priv->dp_write_urb_in_use = 1;
0401             count -= len;
0402             buf += len;
0403         }
0404     }
0405     spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
0406     if (ret)
0407         dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
0408             __func__, ret);
0409     return ret;
0410 
0411 }
0412 
0413 
0414 /*
0415  *  Digi Write In Band Command
0416  *
0417  *  Write commands on the given port.  Commands are 4
0418  *  bytes each, multiple commands can be sent at once, and
0419  *  no command will be split across USB packets.  If timeout
0420  *  is non-zero, write in band command will return after
0421  *  waiting unsuccessfully for the URB status to clear for
0422  *  timeout ticks.  Returns 0 if successful, or a negative
0423  *  error returned by digi_write.
0424  */
0425 
0426 static int digi_write_inb_command(struct usb_serial_port *port,
0427     unsigned char *buf, int count, unsigned long timeout)
0428 {
0429     int ret = 0;
0430     int len;
0431     struct digi_port *priv = usb_get_serial_port_data(port);
0432     unsigned char *data = port->write_urb->transfer_buffer;
0433     unsigned long flags;
0434 
0435     dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
0436         priv->dp_port_num, count);
0437 
0438     if (timeout)
0439         timeout += jiffies;
0440     else
0441         timeout = ULONG_MAX;
0442 
0443     spin_lock_irqsave(&priv->dp_port_lock, flags);
0444     while (count > 0 && ret == 0) {
0445         while (priv->dp_write_urb_in_use &&
0446                time_before(jiffies, timeout)) {
0447             cond_wait_interruptible_timeout_irqrestore(
0448                 &priv->write_wait, DIGI_RETRY_TIMEOUT,
0449                 &priv->dp_port_lock, flags);
0450             if (signal_pending(current))
0451                 return -EINTR;
0452             spin_lock_irqsave(&priv->dp_port_lock, flags);
0453         }
0454 
0455         /* len must be a multiple of 4 and small enough to */
0456         /* guarantee the write will send buffered data first, */
0457         /* so commands are in order with data and not split */
0458         len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
0459         if (len > 4)
0460             len &= ~3;
0461 
0462         /* write any buffered data first */
0463         if (priv->dp_out_buf_len > 0) {
0464             data[0] = DIGI_CMD_SEND_DATA;
0465             data[1] = priv->dp_out_buf_len;
0466             memcpy(data + 2, priv->dp_out_buf,
0467                 priv->dp_out_buf_len);
0468             memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
0469             port->write_urb->transfer_buffer_length
0470                 = priv->dp_out_buf_len + 2 + len;
0471         } else {
0472             memcpy(data, buf, len);
0473             port->write_urb->transfer_buffer_length = len;
0474         }
0475 
0476         ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
0477         if (ret == 0) {
0478             priv->dp_write_urb_in_use = 1;
0479             priv->dp_out_buf_len = 0;
0480             count -= len;
0481             buf += len;
0482         }
0483 
0484     }
0485     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0486 
0487     if (ret)
0488         dev_err(&port->dev,
0489             "%s: usb_submit_urb failed, ret=%d, port=%d\n",
0490             __func__, ret, priv->dp_port_num);
0491     return ret;
0492 }
0493 
0494 
0495 /*
0496  *  Digi Set Modem Signals
0497  *
0498  *  Sets or clears DTR and RTS on the port, according to the
0499  *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
0500  *  for the modem_signals argument.  Returns 0 if successful,
0501  *  -EINTR if interrupted while sleeping, or a non-zero error
0502  *  returned by usb_submit_urb.
0503  */
0504 
0505 static int digi_set_modem_signals(struct usb_serial_port *port,
0506     unsigned int modem_signals, int interruptible)
0507 {
0508 
0509     int ret;
0510     struct digi_port *port_priv = usb_get_serial_port_data(port);
0511     struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
0512     struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
0513     unsigned char *data = oob_port->write_urb->transfer_buffer;
0514     unsigned long flags;
0515 
0516     dev_dbg(&port->dev,
0517         "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
0518         port_priv->dp_port_num, modem_signals);
0519 
0520     spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
0521     spin_lock(&port_priv->dp_port_lock);
0522 
0523     while (oob_priv->dp_write_urb_in_use) {
0524         spin_unlock(&port_priv->dp_port_lock);
0525         cond_wait_interruptible_timeout_irqrestore(
0526             &oob_priv->write_wait, DIGI_RETRY_TIMEOUT,
0527             &oob_priv->dp_port_lock, flags);
0528         if (interruptible && signal_pending(current))
0529             return -EINTR;
0530         spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
0531         spin_lock(&port_priv->dp_port_lock);
0532     }
0533     data[0] = DIGI_CMD_SET_DTR_SIGNAL;
0534     data[1] = port_priv->dp_port_num;
0535     data[2] = (modem_signals & TIOCM_DTR) ?
0536                     DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
0537     data[3] = 0;
0538     data[4] = DIGI_CMD_SET_RTS_SIGNAL;
0539     data[5] = port_priv->dp_port_num;
0540     data[6] = (modem_signals & TIOCM_RTS) ?
0541                     DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
0542     data[7] = 0;
0543 
0544     oob_port->write_urb->transfer_buffer_length = 8;
0545 
0546     ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
0547     if (ret == 0) {
0548         oob_priv->dp_write_urb_in_use = 1;
0549         port_priv->dp_modem_signals &= ~(TIOCM_DTR | TIOCM_RTS);
0550         port_priv->dp_modem_signals |=
0551                 modem_signals & (TIOCM_DTR | TIOCM_RTS);
0552     }
0553     spin_unlock(&port_priv->dp_port_lock);
0554     spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
0555     if (ret)
0556         dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
0557             __func__, ret);
0558     return ret;
0559 }
0560 
0561 /*
0562  *  Digi Transmit Idle
0563  *
0564  *  Digi transmit idle waits, up to timeout ticks, for the transmitter
0565  *  to go idle.  It returns 0 if successful or a negative error.
0566  *
0567  *  There are race conditions here if more than one process is calling
0568  *  digi_transmit_idle on the same port at the same time.  However, this
0569  *  is only called from close, and only one process can be in close on a
0570  *  port at a time, so its ok.
0571  */
0572 
0573 static int digi_transmit_idle(struct usb_serial_port *port,
0574     unsigned long timeout)
0575 {
0576     int ret;
0577     unsigned char buf[2];
0578     struct digi_port *priv = usb_get_serial_port_data(port);
0579     unsigned long flags;
0580 
0581     spin_lock_irqsave(&priv->dp_port_lock, flags);
0582     priv->dp_transmit_idle = 0;
0583     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0584 
0585     buf[0] = DIGI_CMD_TRANSMIT_IDLE;
0586     buf[1] = 0;
0587 
0588     timeout += jiffies;
0589 
0590     ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
0591     if (ret != 0)
0592         return ret;
0593 
0594     spin_lock_irqsave(&priv->dp_port_lock, flags);
0595 
0596     while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
0597         cond_wait_interruptible_timeout_irqrestore(
0598             &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
0599             &priv->dp_port_lock, flags);
0600         if (signal_pending(current))
0601             return -EINTR;
0602         spin_lock_irqsave(&priv->dp_port_lock, flags);
0603     }
0604     priv->dp_transmit_idle = 0;
0605     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0606     return 0;
0607 
0608 }
0609 
0610 
0611 static void digi_rx_throttle(struct tty_struct *tty)
0612 {
0613     unsigned long flags;
0614     struct usb_serial_port *port = tty->driver_data;
0615     struct digi_port *priv = usb_get_serial_port_data(port);
0616 
0617     /* stop receiving characters by not resubmitting the read urb */
0618     spin_lock_irqsave(&priv->dp_port_lock, flags);
0619     priv->dp_throttled = 1;
0620     priv->dp_throttle_restart = 0;
0621     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0622 }
0623 
0624 
0625 static void digi_rx_unthrottle(struct tty_struct *tty)
0626 {
0627     int ret = 0;
0628     unsigned long flags;
0629     struct usb_serial_port *port = tty->driver_data;
0630     struct digi_port *priv = usb_get_serial_port_data(port);
0631 
0632     spin_lock_irqsave(&priv->dp_port_lock, flags);
0633 
0634     /* restart read chain */
0635     if (priv->dp_throttle_restart)
0636         ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
0637 
0638     /* turn throttle off */
0639     priv->dp_throttled = 0;
0640     priv->dp_throttle_restart = 0;
0641 
0642     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0643 
0644     if (ret)
0645         dev_err(&port->dev,
0646             "%s: usb_submit_urb failed, ret=%d, port=%d\n",
0647             __func__, ret, priv->dp_port_num);
0648 }
0649 
0650 
0651 static void digi_set_termios(struct tty_struct *tty,
0652         struct usb_serial_port *port, struct ktermios *old_termios)
0653 {
0654     struct digi_port *priv = usb_get_serial_port_data(port);
0655     struct device *dev = &port->dev;
0656     unsigned int iflag = tty->termios.c_iflag;
0657     unsigned int cflag = tty->termios.c_cflag;
0658     unsigned int old_iflag = old_termios->c_iflag;
0659     unsigned int old_cflag = old_termios->c_cflag;
0660     unsigned char buf[32];
0661     unsigned int modem_signals;
0662     int arg, ret;
0663     int i = 0;
0664     speed_t baud;
0665 
0666     dev_dbg(dev,
0667         "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
0668         priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
0669 
0670     /* set baud rate */
0671     baud = tty_get_baud_rate(tty);
0672     if (baud != tty_termios_baud_rate(old_termios)) {
0673         arg = -1;
0674 
0675         /* reassert DTR and (maybe) RTS on transition from B0 */
0676         if ((old_cflag & CBAUD) == B0) {
0677             /* don't set RTS if using hardware flow control */
0678             /* and throttling input */
0679             modem_signals = TIOCM_DTR;
0680             if (!C_CRTSCTS(tty) || !tty_throttled(tty))
0681                 modem_signals |= TIOCM_RTS;
0682             digi_set_modem_signals(port, modem_signals, 1);
0683         }
0684         switch (baud) {
0685         /* drop DTR and RTS on transition to B0 */
0686         case 0: digi_set_modem_signals(port, 0, 1); break;
0687         case 50: arg = DIGI_BAUD_50; break;
0688         case 75: arg = DIGI_BAUD_75; break;
0689         case 110: arg = DIGI_BAUD_110; break;
0690         case 150: arg = DIGI_BAUD_150; break;
0691         case 200: arg = DIGI_BAUD_200; break;
0692         case 300: arg = DIGI_BAUD_300; break;
0693         case 600: arg = DIGI_BAUD_600; break;
0694         case 1200: arg = DIGI_BAUD_1200; break;
0695         case 1800: arg = DIGI_BAUD_1800; break;
0696         case 2400: arg = DIGI_BAUD_2400; break;
0697         case 4800: arg = DIGI_BAUD_4800; break;
0698         case 9600: arg = DIGI_BAUD_9600; break;
0699         case 19200: arg = DIGI_BAUD_19200; break;
0700         case 38400: arg = DIGI_BAUD_38400; break;
0701         case 57600: arg = DIGI_BAUD_57600; break;
0702         case 115200: arg = DIGI_BAUD_115200; break;
0703         case 230400: arg = DIGI_BAUD_230400; break;
0704         case 460800: arg = DIGI_BAUD_460800; break;
0705         default:
0706             arg = DIGI_BAUD_9600;
0707             baud = 9600;
0708             break;
0709         }
0710         if (arg != -1) {
0711             buf[i++] = DIGI_CMD_SET_BAUD_RATE;
0712             buf[i++] = priv->dp_port_num;
0713             buf[i++] = arg;
0714             buf[i++] = 0;
0715         }
0716     }
0717     /* set parity */
0718     tty->termios.c_cflag &= ~CMSPAR;
0719 
0720     if ((cflag & (PARENB | PARODD)) != (old_cflag & (PARENB | PARODD))) {
0721         if (cflag & PARENB) {
0722             if (cflag & PARODD)
0723                 arg = DIGI_PARITY_ODD;
0724             else
0725                 arg = DIGI_PARITY_EVEN;
0726         } else {
0727             arg = DIGI_PARITY_NONE;
0728         }
0729         buf[i++] = DIGI_CMD_SET_PARITY;
0730         buf[i++] = priv->dp_port_num;
0731         buf[i++] = arg;
0732         buf[i++] = 0;
0733     }
0734     /* set word size */
0735     if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
0736         arg = -1;
0737         switch (cflag & CSIZE) {
0738         case CS5: arg = DIGI_WORD_SIZE_5; break;
0739         case CS6: arg = DIGI_WORD_SIZE_6; break;
0740         case CS7: arg = DIGI_WORD_SIZE_7; break;
0741         case CS8: arg = DIGI_WORD_SIZE_8; break;
0742         default:
0743             dev_dbg(dev,
0744                 "digi_set_termios: can't handle word size %d\n",
0745                 cflag & CSIZE);
0746             break;
0747         }
0748 
0749         if (arg != -1) {
0750             buf[i++] = DIGI_CMD_SET_WORD_SIZE;
0751             buf[i++] = priv->dp_port_num;
0752             buf[i++] = arg;
0753             buf[i++] = 0;
0754         }
0755 
0756     }
0757 
0758     /* set stop bits */
0759     if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
0760 
0761         if ((cflag & CSTOPB))
0762             arg = DIGI_STOP_BITS_2;
0763         else
0764             arg = DIGI_STOP_BITS_1;
0765 
0766         buf[i++] = DIGI_CMD_SET_STOP_BITS;
0767         buf[i++] = priv->dp_port_num;
0768         buf[i++] = arg;
0769         buf[i++] = 0;
0770 
0771     }
0772 
0773     /* set input flow control */
0774     if ((iflag & IXOFF) != (old_iflag & IXOFF) ||
0775             (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
0776         arg = 0;
0777         if (iflag & IXOFF)
0778             arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
0779         else
0780             arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
0781 
0782         if (cflag & CRTSCTS) {
0783             arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
0784 
0785             /* On USB-4 it is necessary to assert RTS prior */
0786             /* to selecting RTS input flow control.  */
0787             buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
0788             buf[i++] = priv->dp_port_num;
0789             buf[i++] = DIGI_RTS_ACTIVE;
0790             buf[i++] = 0;
0791 
0792         } else {
0793             arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
0794         }
0795         buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
0796         buf[i++] = priv->dp_port_num;
0797         buf[i++] = arg;
0798         buf[i++] = 0;
0799     }
0800 
0801     /* set output flow control */
0802     if ((iflag & IXON) != (old_iflag & IXON) ||
0803             (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
0804         arg = 0;
0805         if (iflag & IXON)
0806             arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
0807         else
0808             arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
0809 
0810         if (cflag & CRTSCTS)
0811             arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
0812         else
0813             arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
0814 
0815         buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
0816         buf[i++] = priv->dp_port_num;
0817         buf[i++] = arg;
0818         buf[i++] = 0;
0819     }
0820 
0821     /* set receive enable/disable */
0822     if ((cflag & CREAD) != (old_cflag & CREAD)) {
0823         if (cflag & CREAD)
0824             arg = DIGI_ENABLE;
0825         else
0826             arg = DIGI_DISABLE;
0827 
0828         buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
0829         buf[i++] = priv->dp_port_num;
0830         buf[i++] = arg;
0831         buf[i++] = 0;
0832     }
0833     ret = digi_write_oob_command(port, buf, i, 1);
0834     if (ret != 0)
0835         dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
0836     tty_encode_baud_rate(tty, baud, baud);
0837 }
0838 
0839 
0840 static void digi_break_ctl(struct tty_struct *tty, int break_state)
0841 {
0842     struct usb_serial_port *port = tty->driver_data;
0843     unsigned char buf[4];
0844 
0845     buf[0] = DIGI_CMD_BREAK_CONTROL;
0846     buf[1] = 2;             /* length */
0847     buf[2] = break_state ? 1 : 0;
0848     buf[3] = 0;             /* pad */
0849     digi_write_inb_command(port, buf, 4, 0);
0850 }
0851 
0852 
0853 static int digi_tiocmget(struct tty_struct *tty)
0854 {
0855     struct usb_serial_port *port = tty->driver_data;
0856     struct digi_port *priv = usb_get_serial_port_data(port);
0857     unsigned int val;
0858     unsigned long flags;
0859 
0860     spin_lock_irqsave(&priv->dp_port_lock, flags);
0861     val = priv->dp_modem_signals;
0862     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0863     return val;
0864 }
0865 
0866 
0867 static int digi_tiocmset(struct tty_struct *tty,
0868                     unsigned int set, unsigned int clear)
0869 {
0870     struct usb_serial_port *port = tty->driver_data;
0871     struct digi_port *priv = usb_get_serial_port_data(port);
0872     unsigned int val;
0873     unsigned long flags;
0874 
0875     spin_lock_irqsave(&priv->dp_port_lock, flags);
0876     val = (priv->dp_modem_signals & ~clear) | set;
0877     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0878     return digi_set_modem_signals(port, val, 1);
0879 }
0880 
0881 
0882 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
0883                     const unsigned char *buf, int count)
0884 {
0885 
0886     int ret, data_len, new_len;
0887     struct digi_port *priv = usb_get_serial_port_data(port);
0888     unsigned char *data = port->write_urb->transfer_buffer;
0889     unsigned long flags;
0890 
0891     dev_dbg(&port->dev, "digi_write: TOP: port=%d, count=%d\n",
0892         priv->dp_port_num, count);
0893 
0894     /* copy user data (which can sleep) before getting spin lock */
0895     count = min(count, port->bulk_out_size-2);
0896     count = min(64, count);
0897 
0898     /* be sure only one write proceeds at a time */
0899     /* there are races on the port private buffer */
0900     spin_lock_irqsave(&priv->dp_port_lock, flags);
0901 
0902     /* wait for urb status clear to submit another urb */
0903     if (priv->dp_write_urb_in_use) {
0904         /* buffer data if count is 1 (probably put_char) if possible */
0905         if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
0906             priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
0907             new_len = 1;
0908         } else {
0909             new_len = 0;
0910         }
0911         spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0912         return new_len;
0913     }
0914 
0915     /* allow space for any buffered data and for new data, up to */
0916     /* transfer buffer size - 2 (for command and length bytes) */
0917     new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
0918     data_len = new_len + priv->dp_out_buf_len;
0919 
0920     if (data_len == 0) {
0921         spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0922         return 0;
0923     }
0924 
0925     port->write_urb->transfer_buffer_length = data_len+2;
0926 
0927     *data++ = DIGI_CMD_SEND_DATA;
0928     *data++ = data_len;
0929 
0930     /* copy in buffered data first */
0931     memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
0932     data += priv->dp_out_buf_len;
0933 
0934     /* copy in new data */
0935     memcpy(data, buf, new_len);
0936 
0937     ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
0938     if (ret == 0) {
0939         priv->dp_write_urb_in_use = 1;
0940         ret = new_len;
0941         priv->dp_out_buf_len = 0;
0942     }
0943 
0944     /* return length of new data written, or error */
0945     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0946     if (ret < 0)
0947         dev_err_console(port,
0948             "%s: usb_submit_urb failed, ret=%d, port=%d\n",
0949             __func__, ret, priv->dp_port_num);
0950     dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
0951     return ret;
0952 
0953 }
0954 
0955 static void digi_write_bulk_callback(struct urb *urb)
0956 {
0957 
0958     struct usb_serial_port *port = urb->context;
0959     struct usb_serial *serial;
0960     struct digi_port *priv;
0961     struct digi_serial *serial_priv;
0962     unsigned long flags;
0963     int ret = 0;
0964     int status = urb->status;
0965     bool wakeup;
0966 
0967     /* port and serial sanity check */
0968     if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
0969         pr_err("%s: port or port->private is NULL, status=%d\n",
0970             __func__, status);
0971         return;
0972     }
0973     serial = port->serial;
0974     if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
0975         dev_err(&port->dev,
0976             "%s: serial or serial->private is NULL, status=%d\n",
0977             __func__, status);
0978         return;
0979     }
0980 
0981     /* handle oob callback */
0982     if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
0983         dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
0984         spin_lock_irqsave(&priv->dp_port_lock, flags);
0985         priv->dp_write_urb_in_use = 0;
0986         wake_up_interruptible(&priv->write_wait);
0987         spin_unlock_irqrestore(&priv->dp_port_lock, flags);
0988         return;
0989     }
0990 
0991     /* try to send any buffered data on this port */
0992     wakeup = true;
0993     spin_lock_irqsave(&priv->dp_port_lock, flags);
0994     priv->dp_write_urb_in_use = 0;
0995     if (priv->dp_out_buf_len > 0) {
0996         *((unsigned char *)(port->write_urb->transfer_buffer))
0997             = (unsigned char)DIGI_CMD_SEND_DATA;
0998         *((unsigned char *)(port->write_urb->transfer_buffer) + 1)
0999             = (unsigned char)priv->dp_out_buf_len;
1000         port->write_urb->transfer_buffer_length =
1001                         priv->dp_out_buf_len + 2;
1002         memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
1003             priv->dp_out_buf_len);
1004         ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1005         if (ret == 0) {
1006             priv->dp_write_urb_in_use = 1;
1007             priv->dp_out_buf_len = 0;
1008             wakeup = false;
1009         }
1010     }
1011     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1012 
1013     if (ret && ret != -EPERM)
1014         dev_err_console(port,
1015             "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1016             __func__, ret, priv->dp_port_num);
1017 
1018     if (wakeup)
1019         tty_port_tty_wakeup(&port->port);
1020 }
1021 
1022 static unsigned int digi_write_room(struct tty_struct *tty)
1023 {
1024     struct usb_serial_port *port = tty->driver_data;
1025     struct digi_port *priv = usb_get_serial_port_data(port);
1026     unsigned long flags;
1027     unsigned int room;
1028 
1029     spin_lock_irqsave(&priv->dp_port_lock, flags);
1030 
1031     if (priv->dp_write_urb_in_use)
1032         room = 0;
1033     else
1034         room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
1035 
1036     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1037     dev_dbg(&port->dev, "digi_write_room: port=%d, room=%u\n", priv->dp_port_num, room);
1038     return room;
1039 
1040 }
1041 
1042 static unsigned int digi_chars_in_buffer(struct tty_struct *tty)
1043 {
1044     struct usb_serial_port *port = tty->driver_data;
1045     struct digi_port *priv = usb_get_serial_port_data(port);
1046     unsigned long flags;
1047     unsigned int chars;
1048 
1049     spin_lock_irqsave(&priv->dp_port_lock, flags);
1050     if (priv->dp_write_urb_in_use)
1051         chars = port->bulk_out_size - 2;
1052     else
1053         chars = priv->dp_out_buf_len;
1054     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1055 
1056     dev_dbg(&port->dev, "%s: port=%d, chars=%d\n", __func__,
1057             priv->dp_port_num, chars);
1058     return chars;
1059 }
1060 
1061 static void digi_dtr_rts(struct usb_serial_port *port, int on)
1062 {
1063     /* Adjust DTR and RTS */
1064     digi_set_modem_signals(port, on * (TIOCM_DTR | TIOCM_RTS), 1);
1065 }
1066 
1067 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
1068 {
1069     int ret;
1070     unsigned char buf[32];
1071     struct digi_port *priv = usb_get_serial_port_data(port);
1072     struct ktermios not_termios;
1073 
1074     /* be sure the device is started up */
1075     if (digi_startup_device(port->serial) != 0)
1076         return -ENXIO;
1077 
1078     /* read modem signals automatically whenever they change */
1079     buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1080     buf[1] = priv->dp_port_num;
1081     buf[2] = DIGI_ENABLE;
1082     buf[3] = 0;
1083 
1084     /* flush fifos */
1085     buf[4] = DIGI_CMD_IFLUSH_FIFO;
1086     buf[5] = priv->dp_port_num;
1087     buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1088     buf[7] = 0;
1089 
1090     ret = digi_write_oob_command(port, buf, 8, 1);
1091     if (ret != 0)
1092         dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
1093 
1094     /* set termios settings */
1095     if (tty) {
1096         not_termios.c_cflag = ~tty->termios.c_cflag;
1097         not_termios.c_iflag = ~tty->termios.c_iflag;
1098         digi_set_termios(tty, port, &not_termios);
1099     }
1100     return 0;
1101 }
1102 
1103 
1104 static void digi_close(struct usb_serial_port *port)
1105 {
1106     DEFINE_WAIT(wait);
1107     int ret;
1108     unsigned char buf[32];
1109     struct digi_port *priv = usb_get_serial_port_data(port);
1110 
1111     mutex_lock(&port->serial->disc_mutex);
1112     /* if disconnected, just clear flags */
1113     if (port->serial->disconnected)
1114         goto exit;
1115 
1116     /* FIXME: Transmit idle belongs in the wait_unti_sent path */
1117     digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
1118 
1119     /* disable input flow control */
1120     buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1121     buf[1] = priv->dp_port_num;
1122     buf[2] = DIGI_DISABLE;
1123     buf[3] = 0;
1124 
1125     /* disable output flow control */
1126     buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1127     buf[5] = priv->dp_port_num;
1128     buf[6] = DIGI_DISABLE;
1129     buf[7] = 0;
1130 
1131     /* disable reading modem signals automatically */
1132     buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
1133     buf[9] = priv->dp_port_num;
1134     buf[10] = DIGI_DISABLE;
1135     buf[11] = 0;
1136 
1137     /* disable receive */
1138     buf[12] = DIGI_CMD_RECEIVE_ENABLE;
1139     buf[13] = priv->dp_port_num;
1140     buf[14] = DIGI_DISABLE;
1141     buf[15] = 0;
1142 
1143     /* flush fifos */
1144     buf[16] = DIGI_CMD_IFLUSH_FIFO;
1145     buf[17] = priv->dp_port_num;
1146     buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1147     buf[19] = 0;
1148 
1149     ret = digi_write_oob_command(port, buf, 20, 0);
1150     if (ret != 0)
1151         dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n",
1152                                     ret);
1153     /* wait for final commands on oob port to complete */
1154     prepare_to_wait(&priv->dp_flush_wait, &wait,
1155             TASK_INTERRUPTIBLE);
1156     schedule_timeout(DIGI_CLOSE_TIMEOUT);
1157     finish_wait(&priv->dp_flush_wait, &wait);
1158 
1159     /* shutdown any outstanding bulk writes */
1160     usb_kill_urb(port->write_urb);
1161 exit:
1162     spin_lock_irq(&priv->dp_port_lock);
1163     priv->dp_write_urb_in_use = 0;
1164     wake_up_interruptible(&priv->dp_close_wait);
1165     spin_unlock_irq(&priv->dp_port_lock);
1166     mutex_unlock(&port->serial->disc_mutex);
1167 }
1168 
1169 
1170 /*
1171  *  Digi Startup Device
1172  *
1173  *  Starts reads on all ports.  Must be called AFTER startup, with
1174  *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
1175  */
1176 
1177 static int digi_startup_device(struct usb_serial *serial)
1178 {
1179     int i, ret = 0;
1180     struct digi_serial *serial_priv = usb_get_serial_data(serial);
1181     struct usb_serial_port *port;
1182 
1183     /* be sure this happens exactly once */
1184     spin_lock(&serial_priv->ds_serial_lock);
1185     if (serial_priv->ds_device_started) {
1186         spin_unlock(&serial_priv->ds_serial_lock);
1187         return 0;
1188     }
1189     serial_priv->ds_device_started = 1;
1190     spin_unlock(&serial_priv->ds_serial_lock);
1191 
1192     /* start reading from each bulk in endpoint for the device */
1193     /* set USB_DISABLE_SPD flag for write bulk urbs */
1194     for (i = 0; i < serial->type->num_ports + 1; i++) {
1195         port = serial->port[i];
1196         ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
1197         if (ret != 0) {
1198             dev_err(&port->dev,
1199                 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1200                 __func__, ret, i);
1201             break;
1202         }
1203     }
1204     return ret;
1205 }
1206 
1207 static int digi_port_init(struct usb_serial_port *port, unsigned port_num)
1208 {
1209     struct digi_port *priv;
1210 
1211     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1212     if (!priv)
1213         return -ENOMEM;
1214 
1215     spin_lock_init(&priv->dp_port_lock);
1216     priv->dp_port_num = port_num;
1217     init_waitqueue_head(&priv->dp_transmit_idle_wait);
1218     init_waitqueue_head(&priv->dp_flush_wait);
1219     init_waitqueue_head(&priv->dp_close_wait);
1220     init_waitqueue_head(&priv->write_wait);
1221     priv->dp_port = port;
1222 
1223     usb_set_serial_port_data(port, priv);
1224 
1225     return 0;
1226 }
1227 
1228 static int digi_startup(struct usb_serial *serial)
1229 {
1230     struct digi_serial *serial_priv;
1231     int ret;
1232 
1233     serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
1234     if (!serial_priv)
1235         return -ENOMEM;
1236 
1237     spin_lock_init(&serial_priv->ds_serial_lock);
1238     serial_priv->ds_oob_port_num = serial->type->num_ports;
1239     serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1240 
1241     ret = digi_port_init(serial_priv->ds_oob_port,
1242                         serial_priv->ds_oob_port_num);
1243     if (ret) {
1244         kfree(serial_priv);
1245         return ret;
1246     }
1247 
1248     usb_set_serial_data(serial, serial_priv);
1249 
1250     return 0;
1251 }
1252 
1253 
1254 static void digi_disconnect(struct usb_serial *serial)
1255 {
1256     int i;
1257 
1258     /* stop reads and writes on all ports */
1259     for (i = 0; i < serial->type->num_ports + 1; i++) {
1260         usb_kill_urb(serial->port[i]->read_urb);
1261         usb_kill_urb(serial->port[i]->write_urb);
1262     }
1263 }
1264 
1265 
1266 static void digi_release(struct usb_serial *serial)
1267 {
1268     struct digi_serial *serial_priv;
1269     struct digi_port *priv;
1270 
1271     serial_priv = usb_get_serial_data(serial);
1272 
1273     priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
1274     kfree(priv);
1275 
1276     kfree(serial_priv);
1277 }
1278 
1279 static int digi_port_probe(struct usb_serial_port *port)
1280 {
1281     return digi_port_init(port, port->port_number);
1282 }
1283 
1284 static void digi_port_remove(struct usb_serial_port *port)
1285 {
1286     struct digi_port *priv;
1287 
1288     priv = usb_get_serial_port_data(port);
1289     kfree(priv);
1290 }
1291 
1292 static void digi_read_bulk_callback(struct urb *urb)
1293 {
1294     struct usb_serial_port *port = urb->context;
1295     struct digi_port *priv;
1296     struct digi_serial *serial_priv;
1297     int ret;
1298     int status = urb->status;
1299 
1300     /* port sanity check, do not resubmit if port is not valid */
1301     if (port == NULL)
1302         return;
1303     priv = usb_get_serial_port_data(port);
1304     if (priv == NULL) {
1305         dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1306             __func__, status);
1307         return;
1308     }
1309     if (port->serial == NULL ||
1310         (serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1311         dev_err(&port->dev, "%s: serial is bad or serial->private "
1312             "is NULL, status=%d\n", __func__, status);
1313         return;
1314     }
1315 
1316     /* do not resubmit urb if it has any status error */
1317     if (status) {
1318         dev_err(&port->dev,
1319             "%s: nonzero read bulk status: status=%d, port=%d\n",
1320             __func__, status, priv->dp_port_num);
1321         return;
1322     }
1323 
1324     /* handle oob or inb callback, do not resubmit if error */
1325     if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1326         if (digi_read_oob_callback(urb) != 0)
1327             return;
1328     } else {
1329         if (digi_read_inb_callback(urb) != 0)
1330             return;
1331     }
1332 
1333     /* continue read */
1334     ret = usb_submit_urb(urb, GFP_ATOMIC);
1335     if (ret != 0 && ret != -EPERM) {
1336         dev_err(&port->dev,
1337             "%s: failed resubmitting urb, ret=%d, port=%d\n",
1338             __func__, ret, priv->dp_port_num);
1339     }
1340 
1341 }
1342 
1343 /*
1344  *  Digi Read INB Callback
1345  *
1346  *  Digi Read INB Callback handles reads on the in band ports, sending
1347  *  the data on to the tty subsystem.  When called we know port and
1348  *  port->private are not NULL and port->serial has been validated.
1349  *  It returns 0 if successful, 1 if successful but the port is
1350  *  throttled, and -1 if the sanity checks failed.
1351  */
1352 
1353 static int digi_read_inb_callback(struct urb *urb)
1354 {
1355     struct usb_serial_port *port = urb->context;
1356     struct digi_port *priv = usb_get_serial_port_data(port);
1357     unsigned char *buf = urb->transfer_buffer;
1358     unsigned long flags;
1359     int opcode;
1360     int len;
1361     int port_status;
1362     unsigned char *data;
1363     int tty_flag, throttled;
1364 
1365     /* short/multiple packet check */
1366     if (urb->actual_length < 2) {
1367         dev_warn(&port->dev, "short packet received\n");
1368         return -1;
1369     }
1370 
1371     opcode = buf[0];
1372     len = buf[1];
1373 
1374     if (urb->actual_length != len + 2) {
1375         dev_err(&port->dev, "malformed packet received: port=%d, opcode=%d, len=%d, actual_length=%u\n",
1376             priv->dp_port_num, opcode, len, urb->actual_length);
1377         return -1;
1378     }
1379 
1380     if (opcode == DIGI_CMD_RECEIVE_DATA && len < 1) {
1381         dev_err(&port->dev, "malformed data packet received\n");
1382         return -1;
1383     }
1384 
1385     spin_lock_irqsave(&priv->dp_port_lock, flags);
1386 
1387     /* check for throttle; if set, do not resubmit read urb */
1388     /* indicate the read chain needs to be restarted on unthrottle */
1389     throttled = priv->dp_throttled;
1390     if (throttled)
1391         priv->dp_throttle_restart = 1;
1392 
1393     /* receive data */
1394     if (opcode == DIGI_CMD_RECEIVE_DATA) {
1395         port_status = buf[2];
1396         data = &buf[3];
1397 
1398         /* get flag from port_status */
1399         tty_flag = 0;
1400 
1401         /* overrun is special, not associated with a char */
1402         if (port_status & DIGI_OVERRUN_ERROR)
1403             tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1404 
1405         /* break takes precedence over parity, */
1406         /* which takes precedence over framing errors */
1407         if (port_status & DIGI_BREAK_ERROR)
1408             tty_flag = TTY_BREAK;
1409         else if (port_status & DIGI_PARITY_ERROR)
1410             tty_flag = TTY_PARITY;
1411         else if (port_status & DIGI_FRAMING_ERROR)
1412             tty_flag = TTY_FRAME;
1413 
1414         /* data length is len-1 (one byte of len is port_status) */
1415         --len;
1416         if (len > 0) {
1417             tty_insert_flip_string_fixed_flag(&port->port, data,
1418                     tty_flag, len);
1419             tty_flip_buffer_push(&port->port);
1420         }
1421     }
1422     spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1423 
1424     if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1425         dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
1426     else if (opcode != DIGI_CMD_RECEIVE_DATA)
1427         dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
1428 
1429     return throttled ? 1 : 0;
1430 
1431 }
1432 
1433 
1434 /*
1435  *  Digi Read OOB Callback
1436  *
1437  *  Digi Read OOB Callback handles reads on the out of band port.
1438  *  When called we know port and port->private are not NULL and
1439  *  the port->serial is valid.  It returns 0 if successful, and
1440  *  -1 if the sanity checks failed.
1441  */
1442 
1443 static int digi_read_oob_callback(struct urb *urb)
1444 {
1445 
1446     struct usb_serial_port *port = urb->context;
1447     struct usb_serial *serial = port->serial;
1448     struct tty_struct *tty;
1449     struct digi_port *priv;
1450     unsigned char *buf = urb->transfer_buffer;
1451     int opcode, line, status, val;
1452     unsigned long flags;
1453     int i;
1454     unsigned int rts;
1455 
1456     if (urb->actual_length < 4)
1457         return -1;
1458 
1459     /* handle each oob command */
1460     for (i = 0; i < urb->actual_length - 3; i += 4) {
1461         opcode = buf[i];
1462         line = buf[i + 1];
1463         status = buf[i + 2];
1464         val = buf[i + 3];
1465 
1466         dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
1467             opcode, line, status, val);
1468 
1469         if (status != 0 || line >= serial->type->num_ports)
1470             continue;
1471 
1472         port = serial->port[line];
1473 
1474         priv = usb_get_serial_port_data(port);
1475         if (priv == NULL)
1476             return -1;
1477 
1478         tty = tty_port_tty_get(&port->port);
1479 
1480         rts = 0;
1481         if (tty)
1482             rts = C_CRTSCTS(tty);
1483 
1484         if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1485             bool wakeup = false;
1486 
1487             spin_lock_irqsave(&priv->dp_port_lock, flags);
1488             /* convert from digi flags to termiox flags */
1489             if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
1490                 priv->dp_modem_signals |= TIOCM_CTS;
1491                 if (rts)
1492                     wakeup = true;
1493             } else {
1494                 priv->dp_modem_signals &= ~TIOCM_CTS;
1495                 /* port must be open to use tty struct */
1496             }
1497             if (val & DIGI_READ_INPUT_SIGNALS_DSR)
1498                 priv->dp_modem_signals |= TIOCM_DSR;
1499             else
1500                 priv->dp_modem_signals &= ~TIOCM_DSR;
1501             if (val & DIGI_READ_INPUT_SIGNALS_RI)
1502                 priv->dp_modem_signals |= TIOCM_RI;
1503             else
1504                 priv->dp_modem_signals &= ~TIOCM_RI;
1505             if (val & DIGI_READ_INPUT_SIGNALS_DCD)
1506                 priv->dp_modem_signals |= TIOCM_CD;
1507             else
1508                 priv->dp_modem_signals &= ~TIOCM_CD;
1509 
1510             spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1511 
1512             if (wakeup)
1513                 tty_port_tty_wakeup(&port->port);
1514         } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
1515             spin_lock_irqsave(&priv->dp_port_lock, flags);
1516             priv->dp_transmit_idle = 1;
1517             wake_up_interruptible(&priv->dp_transmit_idle_wait);
1518             spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1519         } else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
1520             wake_up_interruptible(&priv->dp_flush_wait);
1521         }
1522         tty_kref_put(tty);
1523     }
1524     return 0;
1525 
1526 }
1527 
1528 module_usb_serial_driver(serial_drivers, id_table_combined);
1529 
1530 MODULE_AUTHOR(DRIVER_AUTHOR);
1531 MODULE_DESCRIPTION(DRIVER_DESC);
1532 MODULE_LICENSE("GPL");