Linux Serial subsystem development
 help / color / mirror / Atom feed
* [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO
@ 2026-12-06  8:50 Tapio Reijonen
  2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Tapio Reijonen @ 2026-12-06  8:50 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 an RS485
interface and an alternative interface, typically RS232, on the same
bus pins and choose between them with an interface select input. Add
an optional rs485-select-gpios property next to the existing
rs485-term-gpios and rs485-rx-during-tx-gpios, and let the serial core
drive it from the RS485 configuration state, so that every UART driver
using uart_get_rs485_mode() supports such transceivers without driver
changes.

Patch 1 is new in v2 and fixes a pre-existing problem the review
found: the RS485 GPIO descriptors stored in struct uart_port were
never cleared, so a port that outlives its device, as the 8250 ports
do, kept descriptors of lines devres had already released. The select
GPIO would have made that worse, since it is driven whenever RS485 is
reconfigured, not only while RS485 is enabled.

Tested on an i.MX6SX UART and on a MAX14830 channel, each wired to an
LTC2873, by observing the bus line while switching RS485 on and off
with TIOCSRS485, across probe, unbind and rebind of both drivers.

Tool use: Claude Code (Anthropic) was used throughout. It drafted the
code and the commit messages, reviewed the series against the serial
core and the 8250 port lifetime, and prepared the v2 changes from the
review findings. I decided the design, in particular that the select
line keeps its strapped level until the port is configured, reviewed
every change against the code, and ran the hardware tests. The code
and the messages are mine to defend.

Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
Resend of v2, unchanged. The serial core patches have had no review
since it was posted on 2026-11-05; it still applies to tty-next. Only
Hugo Villeneuve's address in Cc is updated.
Link to the first posting: https://lore.kernel.org/r/20261105-serial-rs485-mode-gpio-v2-0-027e5348892f@vaisala.com

Changes in v2:
- New patch 1: forget the RS485 GPIO descriptors when a port is
  removed, on entry to uart_get_rs485_mode() and on its error paths,
  so a port never carries descriptors of a device it no longer belongs
  to.
- Request the select GPIO without changing its state and make it an
  output at the level it already has, or inactive when that level
  cannot be read back, as on output-only GPIO controllers, so the
  transceiver keeps the interface it was strapped for until
  uart_rs485_config() has placed RTS at its RS485 idle level; forget
  the descriptor if the request fails.
- Commit message of the select patch explains the ordering of the GPIO
  against the driver callback and why the select line, unlike the
  termination and RX-during-TX lines, must not be driven at request
  time.
- Rebased onto v7.4-rc1; Rob's Reviewed-by added to the binding, which
  is unchanged.
- Link to v1: https://lore.kernel.org/r/20261021-serial-rs485-mode-gpio-v1-0-80d7b0903d93@vaisala.com

---
Tapio Reijonen (3):
      serial: core: Forget the RS485 GPIO descriptors when a port is removed
      dt-bindings: serial: rs485: Add transceiver interface select GPIO
      serial: core: Drive the transceiver interface select GPIO

 .../devicetree/bindings/serial/rs485.yaml          |  8 +++
 drivers/tty/serial/serial_core.c                   | 73 ++++++++++++++++++++--
 include/linux/serial_core.h                        |  1 +
 3 files changed, 77 insertions(+), 5 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261019-serial-rs485-mode-gpio-e76bb72b6f21

Best regards,
-- 
Tapio Reijonen <tapio.reijonen@vaisala.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed
  2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
@ 2026-12-06  8:50 ` Tapio Reijonen
  2026-12-06  9:19   ` sashiko-bot
  2026-12-06  8:50 ` [PATCH RESEND v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-12-06  8:50 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 RESEND v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO
  2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
  2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
@ 2026-12-06  8:50 ` Tapio Reijonen
  2026-12-06  8:50 ` [PATCH RESEND v2 3/3] serial: core: Drive the " Tapio Reijonen
  2026-12-06  9:19 ` [PATCH RESEND v2 0/3] serial: core: add " sashiko-bot
  3 siblings, 0 replies; 7+ messages in thread
From: Tapio Reijonen @ 2026-12-06  8:50 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 RESEND v2 3/3] serial: core: Drive the transceiver interface select GPIO
  2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
  2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
  2026-12-06  8:50 ` [PATCH RESEND v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
@ 2026-12-06  8:50 ` Tapio Reijonen
  2026-12-06  9:15   ` sashiko-bot
  2026-12-06  9:19 ` [PATCH RESEND v2 0/3] serial: core: add " sashiko-bot
  3 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-12-06  8:50 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

* Re: [PATCH RESEND v2 3/3] serial: core: Drive the transceiver interface select GPIO
  2026-12-06  8:50 ` [PATCH RESEND v2 3/3] serial: core: Drive the " Tapio Reijonen
@ 2026-12-06  9:15   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  9:15 UTC (permalink / raw)
  To: Tapio Reijonen; +Cc: linux-serial, devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] serial: rs485_select_gpio driven before async rs485_config()
  parks RTS
- [Medium] serial: uart_set_rs485_config() leaves rs485_term_gpio high
  on disable
- [Medium] serial: core: set_mctrl() runs before uart_rs485_config()
  leaves RS485
--

commit 42e26c01d500a86e5f47a0d41e2915cadf1d5b27
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>

serial: core: Drive the transceiver interface select GPIO

This commit adds an optional rs485-select-gpios property to
uart_get_rs485_mode() and drives it from uart_rs485_config() and
uart_set_rs485_config() to select between RS485 and an alternative
transceiver interface.

> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index 9516f50a72139..6c0a6d9dcb851 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c

[ ... ]

> @@ -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;
> +	}

[Severity: Medium]
Does uart_configure_port() drive RTS via set_mctrl() before this path
takes the transceiver out of RS485 mode?

When rs485-select-gpios is strapped active and
linux,rs485-enabled-at-boot-time is not set, uart_get_rs485_mode() keeps
the select line active through probe.  uart_configure_port() then calls
port->ops->set_mctrl(port, port->mctrl) before uart_rs485_config().

On drivers where clearing TIOCM_RTS drives the physical RTS/DE pin high
(such as imx_uart_set_mctrl() or pl011_set_mctrl()), that enables the
RS485 driver onto the shared bus until gpiod_set_value_cansleep()
switches the transceiver to the alternative interface.

Deasserting rs485_select_gpio before set_mctrl() in uart_configure_port(),
matching uart_set_rs485_config(), would avoid the glitch.

[ ... ]

> @@ -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);

[Severity: Medium]
Does disabling RS485 here leave rs485_term_gpio asserted after switching
to the alternative interface?

When userspace enables RS485 with SER_RS485_TERMINATE_BUS and later
disables RS485 via TIOCSRS485, uart_set_rs485_termination() returns early
because SER_RS485_ENABLED is clear.  uart_set_rs485_select_gpio() then
switches the transceiver to RS232 on the same bus pins while
rs485_term_gpio stays high and TIOCGRS485 reports termination off.

On boards where termination is not internally gated by the select pin,
the resistor stays connected across the RS232 lines and overloads the
driver.  Dropping the early return in uart_set_rs485_termination() would
clear it, since uart_sanitize_serial_rs485() already clears the flag.

[ ... ]

> +	/* Enter RS485 only once the driver has parked RTS */
> +	if (rs485.flags & SER_RS485_ENABLED)
> +		uart_set_rs485_select_gpio(port, &rs485);

[Severity: Medium]
Can uart_set_rs485_select_gpio() here (and in uart_rs485_config()) enable
RS485 before drivers with asynchronous rs485_config() callbacks park RTS?

SPI/I2C drivers such as max310x and sc16is7xx call uart_get_rs485_mode()
but defer hardware setup to a worker because port->rs485_config() runs
under port->lock.  In max310x, the worker blocks on port->mutex, which
serial_core holds across the GPIO update:

uart_ioctl()                        kworker (max310x_rs_proc)
mutex_lock(&port->mutex)
uart_set_rs485_config()
  max310x_rs485_config()
    schedule_work(&one->rs_work)
                                    mutex_lock(&port->mutex)  <-- blocks
  uart_set_rs485_select_gpio()  <-- RS485 enabled with unparked RTS
mutex_unlock(&port->mutex)
                                    max310x_set_rts_ctl_params()

sc16is7xx_config_rs485() similarly queues reg_work on s->kworker and
races with uart_set_rs485_select_gpio().  If RTS is asserted from RS232
modem control on an open port, switching to RS485 before the worker parks
RTS enables the RS485 transmitter onto the shared bus.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206-serial-rs485-mode-gpio-v2-0-b9ea0dc2af3a@vaisala.com?part=3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed
  2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
@ 2026-12-06  9:19   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  9:19 UTC (permalink / raw)
  To: Tapio Reijonen; +Cc: linux-serial, devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] serial: core: unlocked uart_forget_rs485_gpios() before
  refcount drain
- [Low] serial: core: missing Fixes trailers for RS485 GPIO commits
- [Low] serial: core: redundant uart_forget_rs485_gpios() call on error
  path
--

commit 31b880969cda7f2c09673a8a1805582d0de68b47
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>

> serial: core: Forget the RS485 GPIO descriptors when a port is removed
>
> 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>

[Severity: Low]
This fixes stale devm-managed RS485 GPIO descriptors across port removal
and re-probe.  Should this include Fixes: tags for the commits that
introduced them?

  Fixes: d58a2df3d887 ("serial: 8250: Support rs485 bus termination GPIO")
  Fixes: 163f080eb717 ("serial: core: Add option to output RS485 RX_DURING_TX state via GPIO")

> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index c0deec0e6af41..9516f50a72139 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c

[ ... ]

> @@ -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);

[Severity: Medium]
Can clearing the RS485 GPIO pointers before taking port->mutex and
draining state->refcount race with concurrent ioctls on /dev/console?

When the removed port is the active console, tty_port_tty_vhangup()
keeps console_fops and skips uart_hangup(), leaving TTY_IO_ERROR unset.
Before port->mutex is re-acquired and state->refcount is drained, a
TIOCSRS485 ioctl on /dev/console can still enter uart_set_rs485_config():

CPU0                                   CPU1
serial_core_remove_one_port()
  tty_port_tty_vhangup()
    /* /dev/console keeps console_fops */
  unlock(&port->mutex)
                                       uart_ioctl(TIOCSRS485)
                                         lock(&port->mutex)
                                         uart_set_rs485_config()
  uart_forget_rs485_gpios(uport)
    uport->rs485_term_gpio = NULL;
                                           gpiod_set_value_cansleep(
                                             port->rs485_term_gpio, ...)
                                             <-- data race
  lock(&port->mutex)
  wait_event(... !refcount)
  state->uart_port = NULL

This causes a data race on the RS485 GPIO pointers and can leave the
GPIOs in an inconsistent state if uart_set_rs485_config() rolls back on
error.  Moving uart_forget_rs485_gpios(uport) inside the mutex guard
after wait_event() and state->uart_port = NULL closes the race.

> @@ -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");
> +	}

[Severity: Low]
Is this uart_forget_rs485_gpios() call redundant?  The function already
cleared both GPIO pointers on entry and has not assigned either one yet.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206-serial-rs485-mode-gpio-v2-0-b9ea0dc2af3a@vaisala.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO
  2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
                   ` (2 preceding siblings ...)
  2026-12-06  8:50 ` [PATCH RESEND v2 3/3] serial: core: Drive the " Tapio Reijonen
@ 2026-12-06  9:19 ` sashiko-bot
  3 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  9:19 UTC (permalink / raw)
  To: Tapio Reijonen; +Cc: linux-serial

Sashiko has finished reviewing this 3-patch series:

- Patch 2: no issues found.
- Patch 1 ("serial: core: Forget the RS485 GPIO descriptors when..."): 3
  potential issues (1 Medium, 2 Low)
- Patch 3 ("serial: core: Drive the transceiver interface select..."): 3
  potential issues (3 Medium)

Detailed reviews have been sent as separate replies to patches 1 and 3.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206-serial-rs485-mode-gpio-v2-0-b9ea0dc2af3a@vaisala.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-06  9:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
2026-12-06  9:19   ` sashiko-bot
2026-12-06  8:50 ` [PATCH RESEND v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
2026-12-06  8:50 ` [PATCH RESEND v2 3/3] serial: core: Drive the " Tapio Reijonen
2026-12-06  9:15   ` sashiko-bot
2026-12-06  9:19 ` [PATCH RESEND v2 0/3] serial: core: add " sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox