serial: core, 8250: set RS485 termination GPIO in serial core
authorLino Sanfilippo <l.sanfilippo@kunbus.com>
Sun, 10 Jul 2022 16:44:37 +0000 (18:44 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 14 Jul 2022 14:34:23 +0000 (16:34 +0200)
In serial8250_em485_config() the termination GPIO is set with the uart_port
spinlock held. This is an issue if setting the GPIO line can sleep (e.g.
since the concerning GPIO expander is connected via SPI or I2C).

Fix this by setting the termination line outside of the uart_port spinlock
in the serial core and using gpiod_set_value_cansleep() which instead of
gpiod_set_value() allows it to sleep.

Beside fixing the termination GPIO line setting for the 8250 driver this
change also makes setting the termination GPIO generic for all UART
drivers.

Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
Link: https://lore.kernel.org/r/20220710164442.2958979-4-LinoSanfilippo@gmx.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/serial/8250/8250_port.c
drivers/tty/serial/serial_core.c

index ed2a606..72252d9 100644 (file)
@@ -676,9 +676,6 @@ int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
                rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
        }
 
-       gpiod_set_value(port->rs485_term_gpio,
-                       rs485->flags & SER_RS485_TERMINATE_BUS);
-
        /*
         * Both serial8250_em485_init() and serial8250_em485_destroy()
         * are idempotent.
index 1db44cd..047ec51 100644 (file)
@@ -1358,12 +1358,23 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4
        memset(rs485->padding1, 0, sizeof(rs485->padding1));
 }
 
+static void uart_set_rs485_termination(struct uart_port *port,
+                                      const struct serial_rs485 *rs485)
+{
+       if (!(rs485->flags & SER_RS485_ENABLED))
+               return;
+
+       gpiod_set_value_cansleep(port->rs485_term_gpio,
+                                !!(rs485->flags & SER_RS485_TERMINATE_BUS));
+}
+
 int uart_rs485_config(struct uart_port *port)
 {
        struct serial_rs485 *rs485 = &port->rs485;
        int ret;
 
        uart_sanitize_serial_rs485(port, rs485);
+       uart_set_rs485_termination(port, rs485);
 
        ret = port->rs485_config(port, NULL, rs485);
        if (ret)
@@ -1406,6 +1417,7 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
        if (ret)
                return ret;
        uart_sanitize_serial_rs485(port, &rs485);
+       uart_set_rs485_termination(port, &rs485);
 
        spin_lock_irqsave(&port->lock, flags);
        ret = port->rs485_config(port, &tty->termios, &rs485);