* [PATCH v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed
2026-11-05 7:12 [PATCH v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
@ 2026-11-05 7:12 ` Tapio Reijonen
2026-11-05 7:25 ` sashiko-bot
2026-11-05 7:12 ` [PATCH v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
2026-11-05 7:12 ` [PATCH v2 3/3] serial: core: Drive the " Tapio Reijonen
2 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-11-05 7:12 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: linux-serial, devicetree, linux-kernel, Lukas Wunner,
Christoph Niedermaier, Ilpo Järvinen, Lino Sanfilippo,
Hugo Villeneuve, Tapio Reijonen
The RS485 termination and RX-during-TX GPIO descriptors are acquired
with devm on behalf of the port's device in uart_get_rs485_mode() and
stored in struct uart_port, but nothing ever clears them. A uart_port
can outlive the device it was registered with: the 8250 core keeps its
ports in a static array, re-adds an unregistered entry as a placeholder
and hands it to the next device that claims the slot. If that device
has no RS485 support or no firmware node, uart_get_rs485_mode() returns
before acquiring anything and the stale descriptors stay in place,
pointing at lines that devres has already released. Enabling RS485 on
such a port then drives GPIO lines the port no longer owns. The same
happens when uart_get_rs485_mode() itself fails after the first
descriptor was acquired.
Forget the descriptors when a port is removed, on entry to
uart_get_rs485_mode() and on its error paths, so a port only ever
carries descriptors acquired for its current device.
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/serial_core.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index c0deec0e6af41db10de15ccca4169c603d9cebf6..9516f50a72139c73d70826f4bb0627aed2d3d310 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1415,6 +1415,17 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4
memset(rs485->padding1, 0, sizeof(rs485->padding1));
}
+/*
+ * Drop the port's references to the RS485 GPIOs. Do not touch the
+ * descriptors themselves: they are devres-managed and may already have
+ * been released if the port struct outlived the device it was set up for.
+ */
+static void uart_forget_rs485_gpios(struct uart_port *port)
+{
+ port->rs485_term_gpio = NULL;
+ port->rs485_rx_during_tx_gpio = NULL;
+}
+
static void uart_set_rs485_termination(struct uart_port *port,
const struct serial_rs485 *rs485)
{
@@ -3265,6 +3276,7 @@ static void serial_core_remove_one_port(struct uart_driver *drv,
*/
uport->type = PORT_UNKNOWN;
uport->port_dev = NULL;
+ uart_forget_rs485_gpios(uport);
guard(mutex)(&port->mutex);
WARN_ON(atomic_dec_return(&state->refcount) < 0);
@@ -3579,6 +3591,8 @@ int uart_get_rs485_mode(struct uart_port *port)
u32 rs485_delay[2];
int ret;
+ uart_forget_rs485_gpios(port);
+
if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
return 0;
@@ -3628,8 +3642,11 @@ int uart_get_rs485_mode(struct uart_port *port)
* Works fine for short cables and users may enable for longer cables.
*/
desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW);
- if (IS_ERR(desc))
- return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n");
+ if (IS_ERR(desc)) {
+ uart_forget_rs485_gpios(port);
+ return dev_err_probe(dev, PTR_ERR(desc),
+ "Cannot get rs485-term-gpios\n");
+ }
port->rs485_term_gpio = desc;
if (port->rs485_term_gpio)
port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
@@ -3637,8 +3654,11 @@ int uart_get_rs485_mode(struct uart_port *port)
dflags = (rs485conf->flags & SER_RS485_RX_DURING_TX) ?
GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags);
- if (IS_ERR(desc))
- return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n");
+ if (IS_ERR(desc)) {
+ uart_forget_rs485_gpios(port);
+ return dev_err_probe(dev, PTR_ERR(desc),
+ "Cannot get rs485-rx-during-tx-gpios\n");
+ }
port->rs485_rx_during_tx_gpio = desc;
if (port->rs485_rx_during_tx_gpio)
port->rs485_supported.flags |= SER_RS485_RX_DURING_TX;
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO
2026-11-05 7:12 [PATCH v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
2026-11-05 7:12 ` [PATCH v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
@ 2026-11-05 7:12 ` Tapio Reijonen
2026-11-05 7:18 ` sashiko-bot
2026-11-05 7:12 ` [PATCH v2 3/3] serial: core: Drive the " Tapio Reijonen
2 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-11-05 7:12 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: linux-serial, devicetree, linux-kernel, Lukas Wunner,
Christoph Niedermaier, Ilpo Järvinen, Lino Sanfilippo,
Hugo Villeneuve, Tapio Reijonen
Multiprotocol transceivers such as the LTC2873 provide both an RS232
and an RS485 interface on the same bus pins and choose between them
with an interface select input: on the LTC2873 a logic low on the
485/232 pin enables RS232 mode and a high enables RS485 mode.
Add an optional rs485-select-gpios property for that input, next to
the existing rs485-term-gpios and rs485-rx-during-tx-gpios. The active
state selects the RS485 interface, the inactive state selects the
transceiver's alternative interface, RS232 on the LTC2873.
Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
Documentation/devicetree/bindings/serial/rs485.yaml | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/Documentation/devicetree/bindings/serial/rs485.yaml b/Documentation/devicetree/bindings/serial/rs485.yaml
index b93254ad2a287ad496f791e3ee90eb9defa0609d..2b5fd405e164f308b6036331edce7eb61da2f3ff 100644
--- a/Documentation/devicetree/bindings/serial/rs485.yaml
+++ b/Documentation/devicetree/bindings/serial/rs485.yaml
@@ -60,6 +60,14 @@ properties:
the active state enables RX during TX.
maxItems: 1
+ rs485-select-gpios:
+ description: GPIO connected to the interface select input of a
+ multiprotocol transceiver that provides an RS485 interface and an
+ alternative interface, typically RS232, on the same bus pins. The
+ active state selects the RS485 interface, the inactive state selects
+ the alternative interface.
+ maxItems: 1
+
additionalProperties: true
...
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 3/3] serial: core: Drive the transceiver interface select GPIO
2026-11-05 7:12 [PATCH v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
2026-11-05 7:12 ` [PATCH v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
2026-11-05 7:12 ` [PATCH v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
@ 2026-11-05 7:12 ` Tapio Reijonen
2026-11-05 7:26 ` sashiko-bot
2 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-11-05 7:12 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: linux-serial, devicetree, linux-kernel, Lukas Wunner,
Christoph Niedermaier, Ilpo Järvinen, Lino Sanfilippo,
Hugo Villeneuve, Tapio Reijonen
Multiprotocol transceivers such as the LTC2873 provide both an RS232
and an RS485 interface on the same bus pins and choose between them
with an interface select input. There is no generic way to drive that
input today, so a port wired to such a transceiver is stuck in
whichever interface the pin is strapped to.
Add the optional rs485-select-gpios property to the GPIOs acquired by
uart_get_rs485_mode() and drive it from the RS485 configuration state:
the GPIO is active while RS485 is enabled, which selects the RS485
interface, and inactive otherwise, which selects the alternative
interface, typically RS232. RS422 is a mode of SER_RS485_ENABLED and
therefore selects the RS485 interface as well.
The state is applied in uart_rs485_config() for the boot-time and
resume paths and in uart_set_rs485_config() for the ioctl path. Unlike
the termination and RX-during-TX GPIOs, which only matter while RS485
is enabled, this GPIO must also be driven when RS485 is disabled to
return the transceiver to its alternative interface. It is deasserted
when the driver rejects the boot-time configuration, restored to the
previous setting when the driver rejects an ioctl, and forgotten with
the other RS485 descriptors on failure and port removal.
Unlike the termination and RX-during-TX lines, whose level is harmless
before the port is configured, the select line decides which driver sits
on the bus pins. Driving it to the configured state at request time is
wrong either way: a port strapped for RS485 would present the
alternative interface during probe, and a port strapped for the
alternative interface would select RS485 while probe code such as the
8250 autoconfig still toggles RTS, the driver enable in RS485 mode.
Request the GPIO without changing its state and make it an output at the
level it already has, so the transceiver keeps the interface it was
strapped for until uart_rs485_config() runs after the driver has placed
RTS at its RS485 idle level. A line that cannot be read back, as on
output-only GPIO controllers, starts at the inactive level instead of
failing the probe. A port that boots with RS485 disabled is switched to
the alternative interface by uart_rs485_config() as well, after the
modem-control lines have been deasserted and without involving the
driver callback.
In RS485 mode RTS is the driver enable of the transceiver. The GPIO is
therefore driven after the driver callback when RS485 is enabled, so
RTS already sits at its RS485 idle level when the transceiver switches
over, and before the driver callback when RS485 is disabled, so the
transceiver has left RS485 mode before RTS reverts to modem control.
Drivers calling uart_get_rs485_mode() need no change to support such
transceivers.
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/serial_core.c | 45 +++++++++++++++++++++++++++++++++++++++-
include/linux/serial_core.h | 1 +
2 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 9516f50a72139c73d70826f4bb0627aed2d3d310..6c0a6d9dcb851b6db0acd91143e651bcb33ce5ae 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1424,6 +1424,7 @@ static void uart_forget_rs485_gpios(struct uart_port *port)
{
port->rs485_term_gpio = NULL;
port->rs485_rx_during_tx_gpio = NULL;
+ port->rs485_select_gpio = NULL;
}
static void uart_set_rs485_termination(struct uart_port *port,
@@ -1446,13 +1447,22 @@ static void uart_set_rs485_rx_during_tx(struct uart_port *port,
!!(rs485->flags & SER_RS485_RX_DURING_TX));
}
+static void uart_set_rs485_select_gpio(struct uart_port *port,
+ const struct serial_rs485 *rs485)
+{
+ gpiod_set_value_cansleep(port->rs485_select_gpio,
+ !!(rs485->flags & SER_RS485_ENABLED));
+}
+
static int uart_rs485_config(struct uart_port *port)
{
struct serial_rs485 *rs485 = &port->rs485;
int ret;
- if (!(rs485->flags & SER_RS485_ENABLED))
+ if (!(rs485->flags & SER_RS485_ENABLED)) {
+ uart_set_rs485_select_gpio(port, rs485);
return 0;
+ }
uart_sanitize_serial_rs485(port, rs485);
uart_set_rs485_termination(port, rs485);
@@ -1467,6 +1477,8 @@ static int uart_rs485_config(struct uart_port *port)
gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
}
+ uart_set_rs485_select_gpio(port, rs485);
+
return ret;
}
@@ -1503,6 +1515,10 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
uart_set_rs485_termination(port, &rs485);
uart_set_rs485_rx_during_tx(port, &rs485);
+ /* Leave RS485 before RTS reverts to modem control below */
+ if (!(rs485.flags & SER_RS485_ENABLED))
+ uart_set_rs485_select_gpio(port, &rs485);
+
scoped_guard(uart_port_lock_irqsave, port) {
ret = port->rs485_config(port, &tty->termios, &rs485);
if (!ret) {
@@ -1519,9 +1535,14 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
!!(port->rs485.flags & SER_RS485_TERMINATE_BUS));
gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
!!(port->rs485.flags & SER_RS485_RX_DURING_TX));
+ uart_set_rs485_select_gpio(port, &port->rs485);
return ret;
}
+ /* Enter RS485 only once the driver has parked RTS */
+ if (rs485.flags & SER_RS485_ENABLED)
+ uart_set_rs485_select_gpio(port, &rs485);
+
if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
return -EFAULT;
@@ -3663,6 +3684,28 @@ int uart_get_rs485_mode(struct uart_port *port)
if (port->rs485_rx_during_tx_gpio)
port->rs485_supported.flags |= SER_RS485_RX_DURING_TX;
+ /*
+ * Keep the strap level until the port has been configured. A line
+ * that cannot be read back, as on output-only GPIO controllers,
+ * starts on the alternative interface.
+ */
+ desc = devm_gpiod_get_optional(dev, "rs485-select", GPIOD_ASIS);
+ if (IS_ERR(desc)) {
+ uart_forget_rs485_gpios(port);
+ return dev_err_probe(dev, PTR_ERR(desc),
+ "Cannot get rs485-select-gpios\n");
+ }
+ if (desc) {
+ ret = gpiod_get_value_cansleep(desc);
+ ret = gpiod_direction_output(desc, ret > 0);
+ if (ret) {
+ uart_forget_rs485_gpios(port);
+ return dev_err_probe(dev, ret,
+ "Cannot drive rs485-select-gpios\n");
+ }
+ }
+ port->rs485_select_gpio = desc;
+
return 0;
}
EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 4eeb8253752aa08b27829e80a259c7db65728e8e..fe90a8aada98cacf5a80f20ec2a91e249c92d0ed 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -600,6 +600,7 @@ struct uart_port {
struct serial_rs485 rs485_supported; /* Supported mask for serial_rs485 */
struct gpio_desc *rs485_term_gpio; /* enable RS485 bus termination */
struct gpio_desc *rs485_rx_during_tx_gpio; /* Output GPIO that sets the state of RS485 RX during TX */
+ struct gpio_desc *rs485_select_gpio; /* select the RS485 interface */
struct serial_iso7816 iso7816;
void *private_data; /* generic platform data pointer */
};
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread