OSDN Git Service

USB: serial: add generic support for TIOCSSERIAL
authorJohan Hovold <johan@kernel.org>
Wed, 7 Apr 2021 10:39:21 +0000 (12:39 +0200)
committerJohan Hovold <johan@kernel.org>
Thu, 8 Apr 2021 07:46:02 +0000 (09:46 +0200)
TIOCSSERIAL is a horrid, underspecified, legacy interface which for most
serial devices is only useful for setting the close_delay and
closing_wait parameters.

The closing_wait parameter determines how long to wait for the transfer
buffers to drain during close and the default timeout of 30 seconds may
not be sufficient at low line speeds. In other cases, when for example
flow is stopped, the default timeout may instead be too long.

Add generic support for TIOCSSERIAL and TIOCGSERIAL with handling of the
three common parameters close_delay, closing_wait and line for the
benefit of all USB serial drivers while still allowing drivers to
implement further functionality through the existing callbacks.

This currently includes a few drivers that report their base baud clock
rate even if that is really only of interest when setting custom
divisors through the deprecated ASYNC_SPD_CUST interface; an interface
which only the FTDI driver actually implements.

Some drivers have also been reporting back a fake UART type, something
which should no longer be needed and will be dropped by a follow-on
patch.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
19 files changed:
drivers/usb/serial/ark3116.c
drivers/usb/serial/f81232.c
drivers/usb/serial/f81534.c
drivers/usb/serial/ftdi_sio.c
drivers/usb/serial/io_edgeport.c
drivers/usb/serial/io_ti.c
drivers/usb/serial/mos7720.c
drivers/usb/serial/mos7840.c
drivers/usb/serial/opticon.c
drivers/usb/serial/option.c
drivers/usb/serial/pl2303.c
drivers/usb/serial/quatech2.c
drivers/usb/serial/ssu100.c
drivers/usb/serial/ti_usb_3410_5052.c
drivers/usb/serial/usb-serial.c
drivers/usb/serial/usb-wwan.h
drivers/usb/serial/usb_wwan.c
drivers/usb/serial/whiteheat.c
include/linux/usb/serial.h

index 957cdd6..c0cf60e 100644 (file)
@@ -385,17 +385,10 @@ err_free:
        return result;
 }
 
-static int ark3116_get_serial_info(struct tty_struct *tty,
+static void ark3116_get_serial_info(struct tty_struct *tty,
                        struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
        ss->type = PORT_16654;
-       ss->line = port->minor;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 static int ark3116_tiocmget(struct tty_struct *tty)
index af0fe2a..5e34b36 100644 (file)
@@ -820,19 +820,13 @@ static int f81232_carrier_raised(struct usb_serial_port *port)
        return 0;
 }
 
-static int f81232_get_serial_info(struct tty_struct *tty,
-               struct serial_struct *ss)
+static void f81232_get_serial(struct tty_struct *tty, struct serial_struct *ss)
 {
        struct usb_serial_port *port = tty->driver_data;
        struct f81232_private *priv = usb_get_serial_port_data(port);
 
        ss->type = PORT_16550A;
-       ss->line = port->minor;
        ss->baud_base = priv->baud_base;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 static void  f81232_interrupt_work(struct work_struct *work)
@@ -1023,7 +1017,7 @@ static struct usb_serial_driver f81232_device = {
        .close =                f81232_close,
        .dtr_rts =              f81232_dtr_rts,
        .carrier_raised =       f81232_carrier_raised,
-       .get_serial =           f81232_get_serial_info,
+       .get_serial =           f81232_get_serial,
        .break_ctl =            f81232_break_ctl,
        .set_termios =          f81232_set_termios,
        .tiocmget =             f81232_tiocmget,
@@ -1048,7 +1042,7 @@ static struct usb_serial_driver f81534a_device = {
        .close =                f81232_close,
        .dtr_rts =              f81232_dtr_rts,
        .carrier_raised =       f81232_carrier_raised,
-       .get_serial =           f81232_get_serial_info,
+       .get_serial =           f81232_get_serial,
        .break_ctl =            f81232_break_ctl,
        .set_termios =          f81232_set_termios,
        .tiocmget =             f81232_tiocmget,
index c9f90d4..633de52 100644 (file)
@@ -1140,8 +1140,7 @@ static void f81534_close(struct usb_serial_port *port)
        mutex_unlock(&serial_priv->urb_mutex);
 }
 
-static int f81534_get_serial_info(struct tty_struct *tty,
-                                 struct serial_struct *ss)
+static void f81534_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
        struct usb_serial_port *port = tty->driver_data;
        struct f81534_port_private *port_priv;
@@ -1149,12 +1148,7 @@ static int f81534_get_serial_info(struct tty_struct *tty,
        port_priv = usb_get_serial_port_data(port);
 
        ss->type = PORT_16550A;
-       ss->line = port->minor;
        ss->baud_base = port_priv->baud_base;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 static void f81534_process_per_serial_block(struct usb_serial_port *port,
index f8a0911..16d3e50 100644 (file)
@@ -1082,8 +1082,7 @@ static int  ftdi_tiocmset(struct tty_struct *tty,
                        unsigned int set, unsigned int clear);
 static int  ftdi_ioctl(struct tty_struct *tty,
                        unsigned int cmd, unsigned long arg);
-static int get_serial_info(struct tty_struct *tty,
-                               struct serial_struct *ss);
+static void get_serial_info(struct tty_struct *tty, struct serial_struct *ss);
 static int set_serial_info(struct tty_struct *tty,
                                struct serial_struct *ss);
 static void ftdi_break_ctl(struct tty_struct *tty, int break_state);
@@ -1477,20 +1476,14 @@ static int read_latency_timer(struct usb_serial_port *port)
        return 0;
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                               struct serial_struct *ss)
+static void get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
        struct usb_serial_port *port = tty->driver_data;
        struct ftdi_private *priv = usb_get_serial_port_data(port);
 
-       ss->line = port->minor;
        ss->flags = priv->flags;
        ss->baud_base = priv->baud_base;
        ss->custom_divisor = priv->custom_divisor;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 static int set_serial_info(struct tty_struct *tty,
index 3b48098..6b86e68 100644 (file)
@@ -1594,17 +1594,9 @@ static int edge_tiocmget(struct tty_struct *tty)
        return result;
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                               struct serial_struct *ss)
+static void get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
-       ss->type                = PORT_16550A;
-       ss->line                = port->minor;
-       ss->close_delay         = 50;
-       ss->closing_wait        = 3000;
-
-       return 0;
+       ss->type = PORT_16550A;
 }
 
 
index f5aab57..dce994c 100644 (file)
@@ -2433,22 +2433,9 @@ static int edge_tiocmget(struct tty_struct *tty)
        return result;
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                               struct serial_struct *ss)
+static void get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-       unsigned cwait;
-
-       cwait = port->port.closing_wait;
-       if (cwait != ASYNC_CLOSING_WAIT_NONE)
-               cwait = jiffies_to_msecs(cwait) / 10;
-
-       ss->type                = PORT_16550A;
-       ss->line                = port->minor;
-       ss->close_delay         = 50;
-       ss->closing_wait        = cwait;
-
-       return 0;
+       ss->type = PORT_16550A;
 }
 
 static void edge_break(struct tty_struct *tty, int break_state)
index 7289d46..4012b44 100644 (file)
@@ -1634,17 +1634,9 @@ static int mos7720_tiocmset(struct tty_struct *tty,
        return 0;
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss)
+static void get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
-       ss->type                = PORT_16550A;
-       ss->line                = port->minor;
-       ss->close_delay         = 50;
-       ss->closing_wait        = 3000;
-
-       return 0;
+       ss->type = PORT_16550A;
 }
 
 static int mos7720_ioctl(struct tty_struct *tty,
index 77cbe18..d20fb0a 100644 (file)
@@ -1388,17 +1388,9 @@ static int mos7840_get_lsr_info(struct tty_struct *tty,
  *      function to get information about serial port
  *****************************************************************************/
 
-static int mos7840_get_serial_info(struct tty_struct *tty,
-                                  struct serial_struct *ss)
+static void mos7840_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
        ss->type = PORT_16550A;
-       ss->line = port->minor;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 /*****************************************************************************
index 1c7e5dc..db84afc 100644 (file)
@@ -352,18 +352,10 @@ static int opticon_tiocmset(struct tty_struct *tty,
        return 0;
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss)
+static void get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
        /* fake emulate a 16550 uart to make userspace code happy */
-       ss->type                = PORT_16550A;
-       ss->line                = port->minor;
-       ss->close_delay         = 50;
-       ss->closing_wait        = 3000;
-
-       return 0;
+       ss->type = PORT_16550A;
 }
 
 static int opticon_port_probe(struct usb_serial_port *port)
index c6969ca..3e79a54 100644 (file)
@@ -2095,8 +2095,6 @@ static struct usb_serial_driver option_1port_device = {
        .chars_in_buffer   = usb_wwan_chars_in_buffer,
        .tiocmget          = usb_wwan_tiocmget,
        .tiocmset          = usb_wwan_tiocmset,
-       .get_serial        = usb_wwan_get_serial_info,
-       .set_serial        = usb_wwan_set_serial_info,
        .attach            = option_attach,
        .release           = option_release,
        .port_probe        = usb_wwan_port_probe,
index 1bb870c..64f08a4 100644 (file)
@@ -1048,17 +1048,9 @@ static int pl2303_carrier_raised(struct usb_serial_port *port)
        return 0;
 }
 
-static int pl2303_get_serial(struct tty_struct *tty,
-                       struct serial_struct *ss)
+static void pl2303_get_serial(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
        ss->type = PORT_16654;
-       ss->line = port->minor;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
index 0d23e56..5f2e7f6 100644 (file)
@@ -453,18 +453,6 @@ static void qt2_disconnect(struct usb_serial *serial)
        usb_kill_urb(serial_priv->read_urb);
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss)
-{
-       struct usb_serial_port *port = tty->driver_data;
-
-       ss->line                = port->minor;
-       ss->close_delay         = 50;
-       ss->closing_wait        = 3000;
-
-       return 0;
-}
-
 static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
 {
        switch (*ch) {
@@ -975,7 +963,6 @@ static struct usb_serial_driver qt2_device = {
        .tiocmset            = qt2_tiocmset,
        .tiocmiwait          = usb_serial_generic_tiocmiwait,
        .get_icount          = usb_serial_generic_get_icount,
-       .get_serial          = get_serial_info,
        .set_termios         = qt2_set_termios,
 };
 
index c4616c3..3baf7c0 100644 (file)
@@ -331,18 +331,6 @@ static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
        return usb_serial_generic_open(tty, port);
 }
 
-static int get_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss)
-{
-       struct usb_serial_port *port = tty->driver_data;
-
-       ss->line                = port->minor;
-       ss->close_delay         = 50;
-       ss->closing_wait        = 3000;
-
-       return 0;
-}
-
 static int ssu100_attach(struct usb_serial *serial)
 {
        return ssu100_initdevice(serial->dev);
@@ -542,7 +530,6 @@ static struct usb_serial_driver ssu100_device = {
        .tiocmset            = ssu100_tiocmset,
        .tiocmiwait          = usb_serial_generic_tiocmiwait,
        .get_icount          = usb_serial_generic_get_icount,
-       .get_serial          = get_serial_info,
        .set_termios         = ssu100_set_termios,
 };
 
index bb50098..6df316b 100644 (file)
@@ -328,10 +328,7 @@ static void ti_recv(struct usb_serial_port *port, unsigned char *data,
 static void ti_send(struct ti_port *tport);
 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
 static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
-static int ti_get_serial_info(struct tty_struct *tty,
-       struct serial_struct *ss);
-static int ti_set_serial_info(struct tty_struct *tty,
-       struct serial_struct *ss);
+static void ti_get_serial_info(struct tty_struct *tty, struct serial_struct *ss);
 static void ti_handle_new_msr(struct ti_port *tport, u8 msr);
 
 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
@@ -435,7 +432,6 @@ static struct usb_serial_driver ti_1port_device = {
        .throttle               = ti_throttle,
        .unthrottle             = ti_unthrottle,
        .get_serial             = ti_get_serial_info,
-       .set_serial             = ti_set_serial_info,
        .set_termios            = ti_set_termios,
        .tiocmget               = ti_tiocmget,
        .tiocmset               = ti_tiocmset,
@@ -469,7 +465,6 @@ static struct usb_serial_driver ti_2port_device = {
        .throttle               = ti_throttle,
        .unthrottle             = ti_unthrottle,
        .get_serial             = ti_get_serial_info,
-       .set_serial             = ti_set_serial_info,
        .set_termios            = ti_set_termios,
        .tiocmget               = ti_tiocmget,
        .tiocmset               = ti_tiocmset,
@@ -1393,46 +1388,13 @@ free_data:
 }
 
 
-static int ti_get_serial_info(struct tty_struct *tty,
-       struct serial_struct *ss)
+static void ti_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
        struct usb_serial_port *port = tty->driver_data;
        struct ti_port *tport = usb_get_serial_port_data(port);
-       unsigned cwait;
-
-       cwait = port->port.closing_wait;
-       if (cwait != ASYNC_CLOSING_WAIT_NONE)
-               cwait = jiffies_to_msecs(cwait) / 10;
 
        ss->type = PORT_16550A;
-       ss->line = port->minor;
        ss->baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
-       ss->close_delay = 50;
-       ss->closing_wait = cwait;
-
-       return 0;
-}
-
-
-static int ti_set_serial_info(struct tty_struct *tty,
-       struct serial_struct *ss)
-{
-       struct usb_serial_port *port = tty->driver_data;
-       struct tty_port *tport = &port->port;
-       unsigned cwait;
-
-       cwait = ss->closing_wait;
-       if (cwait != ASYNC_CLOSING_WAIT_NONE)
-               cwait = msecs_to_jiffies(10 * ss->closing_wait);
-
-       if (!capable(CAP_SYS_ADMIN)) {
-               if (cwait != tport->closing_wait)
-                       return -EPERM;
-       }
-
-       tport->closing_wait = cwait;
-
-       return 0;
 }
 
 
index f53a830..255f562 100644 (file)
@@ -437,19 +437,62 @@ static void serial_unthrottle(struct tty_struct *tty)
 static int serial_get_serial(struct tty_struct *tty, struct serial_struct *ss)
 {
        struct usb_serial_port *port = tty->driver_data;
+       struct tty_port *tport = &port->port;
+       unsigned int close_delay, closing_wait;
+
+       mutex_lock(&tport->mutex);
+
+       close_delay = jiffies_to_msecs(tport->close_delay) / 10;
+       closing_wait = tport->closing_wait;
+       if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
+               closing_wait = jiffies_to_msecs(closing_wait) / 10;
+
+       ss->line = port->minor;
+       ss->close_delay = close_delay;
+       ss->closing_wait = closing_wait;
 
        if (port->serial->type->get_serial)
-               return port->serial->type->get_serial(tty, ss);
-       return -ENOTTY;
+               port->serial->type->get_serial(tty, ss);
+
+       mutex_unlock(&tport->mutex);
+
+       return 0;
 }
 
 static int serial_set_serial(struct tty_struct *tty, struct serial_struct *ss)
 {
        struct usb_serial_port *port = tty->driver_data;
+       struct tty_port *tport = &port->port;
+       unsigned int close_delay, closing_wait;
+       int ret = 0;
+
+       close_delay = msecs_to_jiffies(ss->close_delay * 10);
+       closing_wait = ss->closing_wait;
+       if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
+               closing_wait = msecs_to_jiffies(closing_wait * 10);
+
+       mutex_lock(&tport->mutex);
+
+       if (!capable(CAP_SYS_ADMIN)) {
+               if (close_delay != tport->close_delay ||
+                               closing_wait != tport->closing_wait) {
+                       ret = -EPERM;
+                       goto out_unlock;
+               }
+       }
 
-       if (port->serial->type->set_serial)
-               return port->serial->type->set_serial(tty, ss);
-       return -ENOTTY;
+       if (port->serial->type->set_serial) {
+               ret = port->serial->type->set_serial(tty, ss);
+               if (ret)
+                       goto out_unlock;
+       }
+
+       tport->close_delay = close_delay;
+       tport->closing_wait = closing_wait;
+out_unlock:
+       mutex_unlock(&tport->mutex);
+
+       return ret;
 }
 
 static int serial_ioctl(struct tty_struct *tty,
index 79dafd9..b5331d0 100644 (file)
@@ -15,10 +15,6 @@ extern int usb_wwan_write_room(struct tty_struct *tty);
 extern int usb_wwan_tiocmget(struct tty_struct *tty);
 extern int usb_wwan_tiocmset(struct tty_struct *tty,
                             unsigned int set, unsigned int clear);
-extern int usb_wwan_get_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss);
-extern int usb_wwan_set_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss);
 extern int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
                          const unsigned char *buf, int count);
 extern int usb_wwan_chars_in_buffer(struct tty_struct *tty);
index 4ea315e..3eb72c5 100644 (file)
@@ -132,48 +132,6 @@ int usb_wwan_tiocmset(struct tty_struct *tty,
 }
 EXPORT_SYMBOL(usb_wwan_tiocmset);
 
-int usb_wwan_get_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss)
-{
-       struct usb_serial_port *port = tty->driver_data;
-
-       ss->line            = port->minor;
-       ss->close_delay     = jiffies_to_msecs(port->port.close_delay) / 10;
-       ss->closing_wait    = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
-                                ASYNC_CLOSING_WAIT_NONE :
-                                jiffies_to_msecs(port->port.closing_wait) / 10;
-       return 0;
-}
-EXPORT_SYMBOL(usb_wwan_get_serial_info);
-
-int usb_wwan_set_serial_info(struct tty_struct *tty,
-                          struct serial_struct *ss)
-{
-       struct usb_serial_port *port = tty->driver_data;
-       unsigned int closing_wait, close_delay;
-       int retval = 0;
-
-       close_delay = msecs_to_jiffies(ss->close_delay * 10);
-       closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
-                       ASYNC_CLOSING_WAIT_NONE :
-                       msecs_to_jiffies(ss->closing_wait * 10);
-
-       mutex_lock(&port->port.mutex);
-
-       if (!capable(CAP_SYS_ADMIN)) {
-               if ((close_delay != port->port.close_delay) ||
-                   (closing_wait != port->port.closing_wait))
-                       retval = -EPERM;
-       } else {
-               port->port.close_delay  = close_delay;
-               port->port.closing_wait = closing_wait;
-       }
-
-       mutex_unlock(&port->port.mutex);
-       return retval;
-}
-EXPORT_SYMBOL(usb_wwan_set_serial_info);
-
 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
                   const unsigned char *buf, int count)
 {
index c8b10fa..6a95c5a 100644 (file)
@@ -83,7 +83,7 @@ static void whiteheat_port_remove(struct usb_serial_port *port);
 static int  whiteheat_open(struct tty_struct *tty,
                        struct usb_serial_port *port);
 static void whiteheat_close(struct usb_serial_port *port);
-static int  whiteheat_get_serial(struct tty_struct *tty,
+static void whiteheat_get_serial(struct tty_struct *tty,
                        struct serial_struct *ss);
 static void whiteheat_set_termios(struct tty_struct *tty,
                        struct usb_serial_port *port, struct ktermios *old);
@@ -439,18 +439,10 @@ static int whiteheat_tiocmset(struct tty_struct *tty,
 }
 
 
-static int whiteheat_get_serial(struct tty_struct *tty,
-                               struct serial_struct *ss)
+static void whiteheat_get_serial(struct tty_struct *tty, struct serial_struct *ss)
 {
-       struct usb_serial_port *port = tty->driver_data;
-
        ss->type = PORT_16654;
-       ss->line = port->minor;
        ss->baud_base = 460800;
-       ss->close_delay = 50;
-       ss->closing_wait = 3000;
-
-       return 0;
 }
 
 
index e9b9057..8c63fa9 100644 (file)
@@ -279,7 +279,7 @@ struct usb_serial_driver {
        int  (*write_room)(struct tty_struct *tty);
        int  (*ioctl)(struct tty_struct *tty,
                      unsigned int cmd, unsigned long arg);
-       int  (*get_serial)(struct tty_struct *tty, struct serial_struct *ss);
+       void (*get_serial)(struct tty_struct *tty, struct serial_struct *ss);
        int  (*set_serial)(struct tty_struct *tty, struct serial_struct *ss);
        void (*set_termios)(struct tty_struct *tty,
                        struct usb_serial_port *port, struct ktermios *old);