All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 0/7] Fixes and improvements for RS485
@ 2023-12-25 11:35 Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Lino Sanfilippo
                   ` (6 more replies)
  0 siblings, 7 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo

The following series includes some fixes and improvements around RS485 in
the serial core and UART drivers:

Patch 1: Do not hold the port lock when setting rx-during-tx GPIO
Patch 2: set missing supported flag for RX during TX GPIO
Patch 3: fix sanitizing check for RTS settings
Patch 4: make sure RS485 is cannot be enabled when it is not supported
Patch 5: imx: do not set RS485 enabled if it is not supported
Patch 6: omap: do not override settings for rs485 support
Patch 7: exar: set missing RS485 supported flag

Changes in v6:
- restore RS485 GPIO settings if setting rs485 configuration fails
(pointed out by Ilpo)
- drop change for uart_get_rs485_config() 
- rephrase and correct commit messages (pointed out by Ilpo)
- remove unnecessary structure nullification (suggested by Ilpo)
- move all RS485 tasks into one function (suggested by Ilpo)

Changes in v5:

- do not combine the functions that set the RS484 GPIOs (as Hugo originally
  suggested)

Changes in v4:

- add comment for function uart_set_rs485_gpios after hint from Hugo
- correct commit message as pointed out by Hugo
- rephrase commit messages
- add patch 7 after discussion with Ilpo

Changes in v3
- Drop patch "Get rid of useless wrapper pl011_get_rs485_mode()" as
  requested by Greg

Changes in v2:
- add missing 'Fixes' tags as requested by Greg
- corrected a typo as pointed out by Hugo
- fix issue in imx driver in the serial core as suggested by Uwe
- partly rephrase some commit messages
- add patch 7


Lino Sanfilippo (7):
  serial: Do not hold the port lock when setting rx-during-tx GPIO
  serial: core: set missing supported flag for RX during TX GPIO
  serial: core: fix sanitizing check for RTS settings
  serial: core: make sure RS485 cannot be enabled when it is not
    supported
  serial: core, imx: do not set RS485 enabled if it is not supported
  serial: omap: do not override settings for RS485 support
  serial: 8250_exar: Set missing rs485_supported flag

 drivers/tty/serial/8250/8250_exar.c |  5 ++-
 drivers/tty/serial/imx.c            | 11 ------
 drivers/tty/serial/omap-serial.c    | 13 +++---
 drivers/tty/serial/serial_core.c    | 61 +++++++++++++++++++++++------
 drivers/tty/serial/stm32-usart.c    |  5 +--
 5 files changed, 59 insertions(+), 36 deletions(-)


base-commit: ceb6a6f023fd3e8b07761ed900352ef574010bcb
-- 
2.43.0


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

* [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  2023-12-25 12:31   ` Maarten Brock
  2023-12-25 11:35 ` [PATCH v6 2/7] serial: core: set missing supported flag for RX during TX GPIO Lino Sanfilippo
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable,
	Hugo Villeneuve

Both the imx and stm32 driver set the rx-during-tx GPIO in rs485_config().
Since this function is called with the port lock held, this can be an
problem in case that setting the GPIO line can sleep (e.g. if a GPIO
expander is used which is connected via SPI or I2C).

Avoid this issue by moving the GPIO setting outside of the port lock into
the serial core and thus making it a generic feature.

Also reset old GPIO settings in case that changing the RS485 configuration
failed.

Fixes: c54d48543689 ("serial: stm32: Add support for rs485 RX_DURING_TX output GPIO")
Fixes: ca530cfa968c ("serial: imx: Add support for RS485 RX_DURING_TX output GPIO")
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: stable@vger.kernel.org
Reviewed-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/imx.c         |  4 ----
 drivers/tty/serial/serial_core.c | 26 ++++++++++++++++++++++++--
 drivers/tty/serial/stm32-usart.c |  5 +----
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 708b9852a575..9cffeb23112b 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -1943,10 +1943,6 @@ static int imx_uart_rs485_config(struct uart_port *port, struct ktermios *termio
 	    rs485conf->flags & SER_RS485_RX_DURING_TX)
 		imx_uart_start_rx(port);
 
-	if (port->rs485_rx_during_tx_gpio)
-		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
-					 !!(rs485conf->flags & SER_RS485_RX_DURING_TX));
-
 	return 0;
 }
 
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index f1348a509552..d155131f221d 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1402,6 +1402,16 @@ static void uart_set_rs485_termination(struct uart_port *port,
 				 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
 }
 
+static void uart_set_rs485_rx_during_tx(struct uart_port *port,
+					const struct serial_rs485 *rs485)
+{
+	if (!(rs485->flags & SER_RS485_ENABLED))
+		return;
+
+	gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
+				 !!(rs485->flags & SER_RS485_RX_DURING_TX));
+}
+
 static int uart_rs485_config(struct uart_port *port)
 {
 	struct serial_rs485 *rs485 = &port->rs485;
@@ -1413,12 +1423,17 @@ static int uart_rs485_config(struct uart_port *port)
 
 	uart_sanitize_serial_rs485(port, rs485);
 	uart_set_rs485_termination(port, rs485);
+	uart_set_rs485_rx_during_tx(port, rs485);
 
 	uart_port_lock_irqsave(port, &flags);
 	ret = port->rs485_config(port, NULL, rs485);
 	uart_port_unlock_irqrestore(port, flags);
-	if (ret)
+	if (ret) {
 		memset(rs485, 0, sizeof(*rs485));
+		/* unset GPIOs */
+		gpiod_set_value_cansleep(port->rs485_term_gpio, 0);
+		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
+	}
 
 	return ret;
 }
@@ -1457,6 +1472,7 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
 		return ret;
 	uart_sanitize_serial_rs485(port, &rs485);
 	uart_set_rs485_termination(port, &rs485);
+	uart_set_rs485_rx_during_tx(port, &rs485);
 
 	uart_port_lock_irqsave(port, &flags);
 	ret = port->rs485_config(port, &tty->termios, &rs485);
@@ -1468,8 +1484,14 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
 			port->ops->set_mctrl(port, port->mctrl);
 	}
 	uart_port_unlock_irqrestore(port, flags);
-	if (ret)
+	if (ret) {
+		/* restore old GPIO settings */
+		gpiod_set_value_cansleep(port->rs485_term_gpio,
+			!!(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));
 		return ret;
+	}
 
 	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
 		return -EFAULT;
diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c
index 3048620315d6..ec9a72a5bea9 100644
--- a/drivers/tty/serial/stm32-usart.c
+++ b/drivers/tty/serial/stm32-usart.c
@@ -226,10 +226,7 @@ static int stm32_usart_config_rs485(struct uart_port *port, struct ktermios *ter
 
 	stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
 
-	if (port->rs485_rx_during_tx_gpio)
-		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
-					 !!(rs485conf->flags & SER_RS485_RX_DURING_TX));
-	else
+	if (!port->rs485_rx_during_tx_gpio)
 		rs485conf->flags |= SER_RS485_RX_DURING_TX;
 
 	if (rs485conf->flags & SER_RS485_ENABLED) {
-- 
2.43.0


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

* [PATCH v6 2/7] serial: core: set missing supported flag for RX during TX GPIO
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 3/7] serial: core: fix sanitizing check for RTS settings Lino Sanfilippo
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable

If the RS485 feature RX-during-TX is supported by means of a GPIO set the
according supported flag. Otherwise setting this feature from userspace may
not be possible, since in uart_sanitize_serial_rs485() the passed RS485
configuration is matched against the supported features and unsupported
settings are thereby removed and thus take no effect.

Cc: stable@vger.kernel.org
Fixes: 163f080eb717 ("serial: core: Add option to output RS485 RX_DURING_TX state via GPIO")
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/serial_core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index d155131f221d..1204102d7162 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -3642,6 +3642,8 @@ int uart_get_rs485_mode(struct uart_port *port)
 	if (IS_ERR(desc))
 		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;
 
 	return 0;
 }
-- 
2.43.0


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

* [PATCH v6 3/7] serial: core: fix sanitizing check for RTS settings
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 2/7] serial: core: set missing supported flag for RX during TX GPIO Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 4/7] serial: core: make sure RS485 cannot be enabled when it is not supported Lino Sanfilippo
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable

Among other things uart_sanitize_serial_rs485() tests the sanity of the RTS
settings in a RS485 configuration that has been passed by userspace.
If RTS-on-send and RTS-after-send are both set or unset the configuration
is adjusted and RTS-after-send is disabled and RTS-on-send enabled.

This however makes only sense if both RTS modes are actually supported by
the driver.

With commit be2e2cb1d281 ("serial: Sanitize rs485_struct") the code does
take the driver support into account but only checks if one of both RTS
modes are supported. This may lead to the errorneous result of RTS-on-send
being set even if only RTS-after-send is supported.

Fix this by changing the implemented logic: First clear all unsupported
flags in the RS485 configuration, then adjust an invalid RTS setting by
taking into account which RTS mode is supported.

Cc: stable@vger.kernel.org
Fixes: be2e2cb1d281 ("serial: Sanitize rs485_struct")
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/serial_core.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 1204102d7162..f67fb6a04983 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1371,19 +1371,27 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4
 		return;
 	}
 
+	rs485->flags &= supported_flags;
+
 	/* Pick sane settings if the user hasn't */
-	if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
-	    !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
+	if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
 	    !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
-		dev_warn_ratelimited(port->dev,
-			"%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
-			port->name, port->line);
-		rs485->flags |= SER_RS485_RTS_ON_SEND;
-		rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
-		supported_flags |= SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND;
-	}
+		if (supported_flags & SER_RS485_RTS_ON_SEND) {
+			rs485->flags |= SER_RS485_RTS_ON_SEND;
+			rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
 
-	rs485->flags &= supported_flags;
+			dev_warn_ratelimited(port->dev,
+				"%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
+				port->name, port->line);
+		} else {
+			rs485->flags |= SER_RS485_RTS_AFTER_SEND;
+			rs485->flags &= ~SER_RS485_RTS_ON_SEND;
+
+			dev_warn_ratelimited(port->dev,
+				"%s (%d): invalid RTS setting, using RTS_AFTER_SEND instead\n",
+				port->name, port->line);
+		}
+	}
 
 	uart_sanitize_serial_rs485_delays(port, rs485);
 
-- 
2.43.0


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

* [PATCH v6 4/7] serial: core: make sure RS485 cannot be enabled when it is not supported
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
                   ` (2 preceding siblings ...)
  2023-12-25 11:35 ` [PATCH v6 3/7] serial: core: fix sanitizing check for RTS settings Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 5/7] serial: core, imx: do not set RS485 enabled if " Lino Sanfilippo
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable

Some uart drivers specify a rs485_config() function and then decide later
to disable RS485 support for some reason (e.g. imx and ar933).

In these cases userspace may be able to activate RS485 via TIOCSRS485
nevertheless, since in uart_set_rs485_config() an existing rs485_config()
function indicates that RS485 is supported.

Make sure that this is not longer possible by checking the uarts
rs485_supported.flags instead and bailing out if SER_RS485_ENABLED is not
set.

Furthermore instead of returning an empty structure return -ENOTTY if the
RS485 configuration is requested via TIOCGRS485 but RS485 is not supported.
This has a small impact on userspace visibility but it is consistent with
the -ENOTTY error for TIOCGRS485.

Fixes: e849145e1fdd ("serial: ar933x: Fill in rs485_supported")
Fixes: 55e18c6b6d42 ("serial: imx: Remove serial_rs485 sanitization")
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: stable@vger.kernel.org
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/serial_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index f67fb6a04983..28bcbc686c67 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1469,7 +1469,7 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
 	int ret;
 	unsigned long flags;
 
-	if (!port->rs485_config)
+	if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
 		return -ENOTTY;
 
 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
-- 
2.43.0


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

* [PATCH v6 5/7] serial: core, imx: do not set RS485 enabled if it is not supported
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
                   ` (3 preceding siblings ...)
  2023-12-25 11:35 ` [PATCH v6 4/7] serial: core: make sure RS485 cannot be enabled when it is not supported Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 6/7] serial: omap: do not override settings for RS485 support Lino Sanfilippo
  2023-12-25 11:35 ` [PATCH v6 7/7] serial: 8250_exar: Set missing rs485_supported flag Lino Sanfilippo
  6 siblings, 0 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable

If the imx driver cannot support RS485 it nullifies the ports
rs485_supported structure. But it still calls uart_get_rs485_mode() which
may set the RS485_ENABLED flag nevertheless.

This may lead to an attempt to configure RS485 even if it is not supported
when the flag is evaluated in uart_configure_port() at port startup.

Avoid this by bailing out of uart_get_rs485_mode() if the RS485_ENABLED
flag is not supported by the caller.

With this fix a check for RTS availability is now obsolete in the imx
driver, since it can not evaluate to true any more. So remove this check.

Furthermore the explicit nullifcation of rs485_supported is not needed,
since the memory has already been set to zeros at allocation. So remove
this, too.

Fixes: 00d7a00e2a6f ("serial: imx: Fill in rs485_supported")
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: stable@vger.kernel.org
Suggested-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/imx.c         | 7 -------
 drivers/tty/serial/serial_core.c | 3 +++
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 9cffeb23112b..198ce7e7bc8b 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -2206,7 +2206,6 @@ static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t)
 	return HRTIMER_NORESTART;
 }
 
-static const struct serial_rs485 imx_no_rs485 = {};	/* No RS485 if no RTS */
 static const struct serial_rs485 imx_rs485_supported = {
 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
 		 SER_RS485_RX_DURING_TX,
@@ -2290,8 +2289,6 @@ static int imx_uart_probe(struct platform_device *pdev)
 	/* RTS is required to control the RS485 transmitter */
 	if (sport->have_rtscts || sport->have_rtsgpio)
 		sport->port.rs485_supported = imx_rs485_supported;
-	else
-		sport->port.rs485_supported = imx_no_rs485;
 	sport->port.flags = UPF_BOOT_AUTOCONF;
 	timer_setup(&sport->timer, imx_uart_timeout, 0);
 
@@ -2328,10 +2325,6 @@ static int imx_uart_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	if (sport->port.rs485.flags & SER_RS485_ENABLED &&
-	    (!sport->have_rtscts && !sport->have_rtsgpio))
-		dev_err(&pdev->dev, "no RTS control, disabling rs485\n");
-
 	/*
 	 * If using the i.MX UART RTS/CTS control then the RTS (CTS_B)
 	 * signal cannot be set low during transmission in case the
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 28bcbc686c67..93e4e1693601 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -3600,6 +3600,9 @@ int uart_get_rs485_mode(struct uart_port *port)
 	u32 rs485_delay[2];
 	int ret;
 
+	if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
+		return 0;
+
 	ret = device_property_read_u32_array(dev, "rs485-rts-delay",
 					     rs485_delay, 2);
 	if (!ret) {
-- 
2.43.0


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

* [PATCH v6 6/7] serial: omap: do not override settings for RS485 support
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
                   ` (4 preceding siblings ...)
  2023-12-25 11:35 ` [PATCH v6 5/7] serial: core, imx: do not set RS485 enabled if " Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  2023-12-25 22:29   ` kernel test robot
  2023-12-25 23:30   ` kernel test robot
  2023-12-25 11:35 ` [PATCH v6 7/7] serial: 8250_exar: Set missing rs485_supported flag Lino Sanfilippo
  6 siblings, 2 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable

The drivers RS485 support is deactivated if there is no RTS GPIO available.
This is done by nullifying the ports rs485_supported struct. After that
however the settings in serial_omap_rs485_supported are assigned to the
same structure unconditionally, which results in an unintended reactivation
of RS485 support.

Fix this by moving the assignment to the beginning of
serial_omap_probe_rs485() and thus before uart_get_rs485_mode() gets
called.

Also replace the assignment of rs485_config() to have the complete RS485
setup in one function.

Fixes: e2752ae3cfc9 ("serial: omap: Disallow RS-485 if rts-gpio is not specified")
Cc: stable@vger.kernel.org
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/omap-serial.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
index ad4c1c5d0a7f..b563c109caa9 100644
--- a/drivers/tty/serial/omap-serial.c
+++ b/drivers/tty/serial/omap-serial.c
@@ -1497,6 +1497,9 @@ static int serial_omap_probe_rs485(struct uart_omap_port *up,
 	if (!np)
 		return 0;
 
+	up->port.rs485_config = serial_omap_config_rs485;
+	up->port.rs485_supported = serial_omap_rs485_supported;
+
 	ret = uart_get_rs485_mode(&up->port);
 	if (ret)
 		return ret;
@@ -1604,17 +1607,11 @@ static int serial_omap_probe(struct platform_device *pdev)
 		dev_info(up->port.dev, "no wakeirq for uart%d\n",
 			 up->port.line);
 
-	ret = serial_omap_probe_rs485(up, &pdev->dev);
-	if (ret < 0)
-		goto err_rs485;
-
 	sprintf(up->name, "OMAP UART%d", up->port.line);
 	up->port.mapbase = mem->start;
 	up->port.membase = base;
 	up->port.flags = omap_up_info->flags;
 	up->port.uartclk = omap_up_info->uartclk;
-	up->port.rs485_config = serial_omap_config_rs485;
-	up->port.rs485_supported = serial_omap_rs485_supported;
 	if (!up->port.uartclk) {
 		up->port.uartclk = DEFAULT_CLK_SPEED;
 		dev_warn(&pdev->dev,
@@ -1622,6 +1619,10 @@ static int serial_omap_probe(struct platform_device *pdev)
 			 DEFAULT_CLK_SPEED);
 	}
 
+	ret = serial_omap_probe_rs485(up, &pdev->dev);
+	if (ret < 0)
+		goto err_rs485;
+
 	up->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
 	up->calc_latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
 	cpu_latency_qos_add_request(&up->pm_qos_request, up->latency);
-- 
2.43.0


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

* [PATCH v6 7/7] serial: 8250_exar: Set missing rs485_supported flag
  2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
                   ` (5 preceding siblings ...)
  2023-12-25 11:35 ` [PATCH v6 6/7] serial: omap: do not override settings for RS485 support Lino Sanfilippo
@ 2023-12-25 11:35 ` Lino Sanfilippo
  6 siblings, 0 replies; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-25 11:35 UTC (permalink / raw
  To: gregkh, jirislaby, ilpo.jarvinen
  Cc: u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	LinoSanfilippo, lukas, p.rosenberger, Lino Sanfilippo, stable

The UART supports an auto-RTS mode in which the RTS pin is automatically
activated during transmission. So mark this mode as being supported even
if RTS is not controlled by the driver but the UART.

Also the serial core expects now at least one of both modes rts-on-send or
rts-after-send to be supported. This is since during sanitization
unsupported flags are deleted from a RS485 configuration set by userspace.
However if the configuration ends up with both flags unset, the core prints
a warning since it considers such a configuration invalid (see
uart_sanitize_serial_rs485()).

Cc: stable@vger.kernel.org
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Lino Sanfilippo <l.sanfilippo@kunbus.com>
---
 drivers/tty/serial/8250/8250_exar.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_exar.c b/drivers/tty/serial/8250/8250_exar.c
index 6085d356ad86..23366f868ae3 100644
--- a/drivers/tty/serial/8250/8250_exar.c
+++ b/drivers/tty/serial/8250/8250_exar.c
@@ -480,7 +480,7 @@ static int sealevel_rs485_config(struct uart_port *port, struct ktermios *termio
 }
 
 static const struct serial_rs485 generic_rs485_supported = {
-	.flags = SER_RS485_ENABLED,
+	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND,
 };
 
 static const struct exar8250_platform exar8250_default_platform = {
@@ -524,7 +524,8 @@ static int iot2040_rs485_config(struct uart_port *port, struct ktermios *termios
 }
 
 static const struct serial_rs485 iot2040_rs485_supported = {
-	.flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
+	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
+		 SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
 };
 
 static const struct property_entry iot2040_gpio_properties[] = {
-- 
2.43.0


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

* Re: [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO
  2023-12-25 11:35 ` [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Lino Sanfilippo
@ 2023-12-25 12:31   ` Maarten Brock
  2023-12-29 15:03     ` Lino Sanfilippo
  0 siblings, 1 reply; 13+ messages in thread
From: Maarten Brock @ 2023-12-25 12:31 UTC (permalink / raw
  To: Lino Sanfilippo
  Cc: gregkh, jirislaby, ilpo.jarvinen, u.kleine-koenig, shawnguo,
	s.hauer, mcoquelin.stm32, alexandre.torgue, cniedermaier, hugo,
	linux-kernel, linux-serial, LinoSanfilippo, lukas, p.rosenberger,
	stable, Hugo Villeneuve

Lino Sanfilippo wrote on 2023-12-25 12:35:
> diff --git a/drivers/tty/serial/serial_core.c 
> b/drivers/tty/serial/serial_core.c
> index f1348a509552..d155131f221d 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c
> @@ -1402,6 +1402,16 @@ static void uart_set_rs485_termination(struct
> uart_port *port,
>  				 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
>  }
> 
> +static void uart_set_rs485_rx_during_tx(struct uart_port *port,
> +					const struct serial_rs485 *rs485)
> +{
> +	if (!(rs485->flags & SER_RS485_ENABLED))
> +		return;
> +

How about checking port->rs485_rx_during_tx_gpio here against NULL 
instead of
before every call?

> +	gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
> +				 !!(rs485->flags & SER_RS485_RX_DURING_TX));
> +}
> +
>  static int uart_rs485_config(struct uart_port *port)
>  {
>  	struct serial_rs485 *rs485 = &port->rs485;
> @@ -1413,12 +1423,17 @@ static int uart_rs485_config(struct uart_port 
> *port)
> 
>  	uart_sanitize_serial_rs485(port, rs485);
>  	uart_set_rs485_termination(port, rs485);
> +	uart_set_rs485_rx_during_tx(port, rs485);
> 
>  	uart_port_lock_irqsave(port, &flags);
>  	ret = port->rs485_config(port, NULL, rs485);
>  	uart_port_unlock_irqrestore(port, flags);
> -	if (ret)
> +	if (ret) {
>  		memset(rs485, 0, sizeof(*rs485));
> +		/* unset GPIOs */
> +		gpiod_set_value_cansleep(port->rs485_term_gpio, 0);
> +		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
> +	}
> 
>  	return ret;
>  }
> @@ -1457,6 +1472,7 @@ static int uart_set_rs485_config(struct
> tty_struct *tty, struct uart_port *port,
>  		return ret;
>  	uart_sanitize_serial_rs485(port, &rs485);
>  	uart_set_rs485_termination(port, &rs485);
> +	uart_set_rs485_rx_during_tx(port, &rs485);
> 
>  	uart_port_lock_irqsave(port, &flags);
>  	ret = port->rs485_config(port, &tty->termios, &rs485);
> @@ -1468,8 +1484,14 @@ static int uart_set_rs485_config(struct
> tty_struct *tty, struct uart_port *port,
>  			port->ops->set_mctrl(port, port->mctrl);
>  	}
>  	uart_port_unlock_irqrestore(port, flags);
> -	if (ret)
> +	if (ret) {
> +		/* restore old GPIO settings */
> +		gpiod_set_value_cansleep(port->rs485_term_gpio,
> +			!!(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));

This does not look like restoring.
Further this looks suspiciously like duplicated code.

>  		return ret;
> +	}
> 
>  	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
>  		return -EFAULT;
> diff --git a/drivers/tty/serial/stm32-usart.c 
> b/drivers/tty/serial/stm32-usart.c
> index 3048620315d6..ec9a72a5bea9 100644
> --- a/drivers/tty/serial/stm32-usart.c
> +++ b/drivers/tty/serial/stm32-usart.c
> @@ -226,10 +226,7 @@ static int stm32_usart_config_rs485(struct
> uart_port *port, struct ktermios *ter
> 
>  	stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
> 
> -	if (port->rs485_rx_during_tx_gpio)
> -		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
> -					 !!(rs485conf->flags & SER_RS485_RX_DURING_TX));
> -	else
> +	if (!port->rs485_rx_during_tx_gpio)

Should the ! be there?

>  		rs485conf->flags |= SER_RS485_RX_DURING_TX;
> 
>  	if (rs485conf->flags & SER_RS485_ENABLED) {

Kind Regards
Maarten Brock


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

* Re: [PATCH v6 6/7] serial: omap: do not override settings for RS485 support
  2023-12-25 11:35 ` [PATCH v6 6/7] serial: omap: do not override settings for RS485 support Lino Sanfilippo
@ 2023-12-25 22:29   ` kernel test robot
  2023-12-25 23:30   ` kernel test robot
  1 sibling, 0 replies; 13+ messages in thread
From: kernel test robot @ 2023-12-25 22:29 UTC (permalink / raw
  To: Lino Sanfilippo, gregkh, jirislaby, ilpo.jarvinen
  Cc: oe-kbuild-all, u.kleine-koenig, shawnguo, s.hauer,
	mcoquelin.stm32, alexandre.torgue, cniedermaier, hugo,
	linux-kernel, linux-serial, LinoSanfilippo, lukas, p.rosenberger,
	Lino Sanfilippo, stable

Hi Lino,

kernel test robot noticed the following build errors:

[auto build test ERROR on ceb6a6f023fd3e8b07761ed900352ef574010bcb]

url:    https://github.com/intel-lab-lkp/linux/commits/Lino-Sanfilippo/serial-Do-not-hold-the-port-lock-when-setting-rx-during-tx-GPIO/20231225-193833
base:   ceb6a6f023fd3e8b07761ed900352ef574010bcb
patch link:    https://lore.kernel.org/r/20231225113524.8800-7-l.sanfilippo%40kunbus.com
patch subject: [PATCH v6 6/7] serial: omap: do not override settings for RS485 support
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20231226/202312260601.aT9SYjjo-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231226/202312260601.aT9SYjjo-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312260601.aT9SYjjo-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   drivers/tty/serial/omap-serial.c: In function 'serial_omap_probe_rs485':
>> drivers/tty/serial/omap-serial.c:1501:36: error: 'serial_omap_rs485_supported' undeclared (first use in this function); did you mean 'serial_omap_request_port'?
    1501 |         up->port.rs485_supported = serial_omap_rs485_supported;
         |                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                    serial_omap_request_port
   drivers/tty/serial/omap-serial.c:1501:36: note: each undeclared identifier is reported only once for each function it appears in
   drivers/tty/serial/omap-serial.c: At top level:
>> drivers/tty/serial/omap-serial.c:1537:34: warning: 'serial_omap_rs485_supported' defined but not used [-Wunused-const-variable=]
    1537 | static const struct serial_rs485 serial_omap_rs485_supported = {
         |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~


vim +1501 drivers/tty/serial/omap-serial.c

  1485	
  1486	static int serial_omap_probe_rs485(struct uart_omap_port *up,
  1487					   struct device *dev)
  1488	{
  1489		struct serial_rs485 *rs485conf = &up->port.rs485;
  1490		struct device_node *np = dev->of_node;
  1491		enum gpiod_flags gflags;
  1492		int ret;
  1493	
  1494		rs485conf->flags = 0;
  1495		up->rts_gpiod = NULL;
  1496	
  1497		if (!np)
  1498			return 0;
  1499	
  1500		up->port.rs485_config = serial_omap_config_rs485;
> 1501		up->port.rs485_supported = serial_omap_rs485_supported;
  1502	
  1503		ret = uart_get_rs485_mode(&up->port);
  1504		if (ret)
  1505			return ret;
  1506	
  1507		if (of_property_read_bool(np, "rs485-rts-active-high")) {
  1508			rs485conf->flags |= SER_RS485_RTS_ON_SEND;
  1509			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
  1510		} else {
  1511			rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
  1512			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  1513		}
  1514	
  1515		/* check for tx enable gpio */
  1516		gflags = rs485conf->flags & SER_RS485_RTS_AFTER_SEND ?
  1517			GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  1518		up->rts_gpiod = devm_gpiod_get_optional(dev, "rts", gflags);
  1519		if (IS_ERR(up->rts_gpiod)) {
  1520			ret = PTR_ERR(up->rts_gpiod);
  1521		        if (ret == -EPROBE_DEFER)
  1522				return ret;
  1523	
  1524			up->rts_gpiod = NULL;
  1525			up->port.rs485_supported = (const struct serial_rs485) { };
  1526			if (rs485conf->flags & SER_RS485_ENABLED) {
  1527				dev_err(dev, "disabling RS-485 (rts-gpio missing in device tree)\n");
  1528				memset(rs485conf, 0, sizeof(*rs485conf));
  1529			}
  1530		} else {
  1531			gpiod_set_consumer_name(up->rts_gpiod, "omap-serial");
  1532		}
  1533	
  1534		return 0;
  1535	}
  1536	
> 1537	static const struct serial_rs485 serial_omap_rs485_supported = {
  1538		.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
  1539			 SER_RS485_RX_DURING_TX,
  1540		.delay_rts_before_send = 1,
  1541		.delay_rts_after_send = 1,
  1542	};
  1543	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH v6 6/7] serial: omap: do not override settings for RS485 support
  2023-12-25 11:35 ` [PATCH v6 6/7] serial: omap: do not override settings for RS485 support Lino Sanfilippo
  2023-12-25 22:29   ` kernel test robot
@ 2023-12-25 23:30   ` kernel test robot
  1 sibling, 0 replies; 13+ messages in thread
From: kernel test robot @ 2023-12-25 23:30 UTC (permalink / raw
  To: Lino Sanfilippo, gregkh, jirislaby, ilpo.jarvinen
  Cc: llvm, oe-kbuild-all, u.kleine-koenig, shawnguo, s.hauer,
	mcoquelin.stm32, alexandre.torgue, cniedermaier, hugo,
	linux-kernel, linux-serial, LinoSanfilippo, lukas, p.rosenberger,
	Lino Sanfilippo, stable

Hi Lino,

kernel test robot noticed the following build errors:

[auto build test ERROR on ceb6a6f023fd3e8b07761ed900352ef574010bcb]

url:    https://github.com/intel-lab-lkp/linux/commits/Lino-Sanfilippo/serial-Do-not-hold-the-port-lock-when-setting-rx-during-tx-GPIO/20231225-193833
base:   ceb6a6f023fd3e8b07761ed900352ef574010bcb
patch link:    https://lore.kernel.org/r/20231225113524.8800-7-l.sanfilippo%40kunbus.com
patch subject: [PATCH v6 6/7] serial: omap: do not override settings for RS485 support
config: powerpc-allmodconfig (https://download.01.org/0day-ci/archive/20231226/202312260719.mI12i497-lkp@intel.com/config)
compiler: clang version 18.0.0git (https://github.com/llvm/llvm-project d3ef86708241a3bee902615c190dead1638c4e09)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231226/202312260719.mI12i497-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312260719.mI12i497-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/tty/serial/omap-serial.c:1501:29: error: use of undeclared identifier 'serial_omap_rs485_supported'
    1501 |         up->port.rs485_supported = serial_omap_rs485_supported;
         |                                    ^
   1 error generated.


vim +/serial_omap_rs485_supported +1501 drivers/tty/serial/omap-serial.c

  1485	
  1486	static int serial_omap_probe_rs485(struct uart_omap_port *up,
  1487					   struct device *dev)
  1488	{
  1489		struct serial_rs485 *rs485conf = &up->port.rs485;
  1490		struct device_node *np = dev->of_node;
  1491		enum gpiod_flags gflags;
  1492		int ret;
  1493	
  1494		rs485conf->flags = 0;
  1495		up->rts_gpiod = NULL;
  1496	
  1497		if (!np)
  1498			return 0;
  1499	
  1500		up->port.rs485_config = serial_omap_config_rs485;
> 1501		up->port.rs485_supported = serial_omap_rs485_supported;
  1502	
  1503		ret = uart_get_rs485_mode(&up->port);
  1504		if (ret)
  1505			return ret;
  1506	
  1507		if (of_property_read_bool(np, "rs485-rts-active-high")) {
  1508			rs485conf->flags |= SER_RS485_RTS_ON_SEND;
  1509			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
  1510		} else {
  1511			rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
  1512			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  1513		}
  1514	
  1515		/* check for tx enable gpio */
  1516		gflags = rs485conf->flags & SER_RS485_RTS_AFTER_SEND ?
  1517			GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  1518		up->rts_gpiod = devm_gpiod_get_optional(dev, "rts", gflags);
  1519		if (IS_ERR(up->rts_gpiod)) {
  1520			ret = PTR_ERR(up->rts_gpiod);
  1521		        if (ret == -EPROBE_DEFER)
  1522				return ret;
  1523	
  1524			up->rts_gpiod = NULL;
  1525			up->port.rs485_supported = (const struct serial_rs485) { };
  1526			if (rs485conf->flags & SER_RS485_ENABLED) {
  1527				dev_err(dev, "disabling RS-485 (rts-gpio missing in device tree)\n");
  1528				memset(rs485conf, 0, sizeof(*rs485conf));
  1529			}
  1530		} else {
  1531			gpiod_set_consumer_name(up->rts_gpiod, "omap-serial");
  1532		}
  1533	
  1534		return 0;
  1535	}
  1536	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO
  2023-12-25 12:31   ` Maarten Brock
@ 2023-12-29 15:03     ` Lino Sanfilippo
  2024-01-02 15:53       ` Maarten Brock
  0 siblings, 1 reply; 13+ messages in thread
From: Lino Sanfilippo @ 2023-12-29 15:03 UTC (permalink / raw
  To: Maarten Brock, Lino Sanfilippo
  Cc: gregkh, jirislaby, ilpo.jarvinen, u.kleine-koenig, shawnguo,
	s.hauer, mcoquelin.stm32, alexandre.torgue, cniedermaier, hugo,
	linux-kernel, linux-serial, lukas, p.rosenberger, stable,
	Hugo Villeneuve


Hi,

On 25.12.23 at 13:31, Maarten Brock wrote:
> Lino Sanfilippo wrote on 2023-12-25 12:35:
>> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
>> index f1348a509552..d155131f221d 100644
>> --- a/drivers/tty/serial/serial_core.c
>> +++ b/drivers/tty/serial/serial_core.c
>> @@ -1402,6 +1402,16 @@ static void uart_set_rs485_termination(struct
>> uart_port *port,
>>                   !!(rs485->flags & SER_RS485_TERMINATE_BUS));
>>  }
>>
>> +static void uart_set_rs485_rx_during_tx(struct uart_port *port,
>> +                    const struct serial_rs485 *rs485)
>> +{
>> +    if (!(rs485->flags & SER_RS485_ENABLED))
>> +        return;
>> +
>
> How about checking port->rs485_rx_during_tx_gpio here against NULL instead of
> before every call?
>

gpiod_set_value_cansleep() already checks for a NULL pointer, so doing this check
in the caller is not needed.

>> +    gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
>> +                 !!(rs485->flags & SER_RS485_RX_DURING_TX));
>> +}
>> +
>>  static int uart_rs485_config(struct uart_port *port)
>>  {
>>      struct serial_rs485 *rs485 = &port->rs485;
>> @@ -1413,12 +1423,17 @@ static int uart_rs485_config(struct uart_port *port)
>>
>>      uart_sanitize_serial_rs485(port, rs485);
>>      uart_set_rs485_termination(port, rs485);
>> +    uart_set_rs485_rx_during_tx(port, rs485);
>>
>>      uart_port_lock_irqsave(port, &flags);
>>      ret = port->rs485_config(port, NULL, rs485);
>>      uart_port_unlock_irqrestore(port, flags);
>> -    if (ret)
>> +    if (ret) {
>>          memset(rs485, 0, sizeof(*rs485));
>> +        /* unset GPIOs */
>> +        gpiod_set_value_cansleep(port->rs485_term_gpio, 0);
>> +        gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
>> +    }
>>
>>      return ret;
>>  }
>> @@ -1457,6 +1472,7 @@ static int uart_set_rs485_config(struct
>> tty_struct *tty, struct uart_port *port,
>>          return ret;
>>      uart_sanitize_serial_rs485(port, &rs485);
>>      uart_set_rs485_termination(port, &rs485);
>> +    uart_set_rs485_rx_during_tx(port, &rs485);
>>
>>      uart_port_lock_irqsave(port, &flags);
>>      ret = port->rs485_config(port, &tty->termios, &rs485);
>> @@ -1468,8 +1484,14 @@ static int uart_set_rs485_config(struct
>> tty_struct *tty, struct uart_port *port,
>>              port->ops->set_mctrl(port, port->mctrl);
>>      }
>>      uart_port_unlock_irqrestore(port, flags);
>> -    if (ret)
>> +    if (ret) {
>> +        /* restore old GPIO settings */
>> +        gpiod_set_value_cansleep(port->rs485_term_gpio,
>> +            !!(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));
>
> This does not look like restoring.


Hmm. The rx-during-tx and terminate-bus GPIOs may have changed before the
drivers rs485_config() was called. If that function fails, the GPIOs
are set back to the values they had before (i.e what is still stored in
the ports serial_rs485 struct). So what is wrong with the term "restore"?

> Further this looks suspiciously like duplicated code

Since the added code consists of two one-liners I am not sure how to
decrease code duplication in this case. We could introduce wrapper functions (the only
ones we have so far to set the GPIOs are uart_set_rs485_termination() and
uart_set_rs485_rx_during_tx() which cannot be used here due to the initial
check for SER_RS485_ENABLED). But would that really help?


>
>>          return ret;
>> +    }
>>
>>      if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
>>          return -EFAULT;
>> diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c
>> index 3048620315d6..ec9a72a5bea9 100644
>> --- a/drivers/tty/serial/stm32-usart.c
>> +++ b/drivers/tty/serial/stm32-usart.c
>> @@ -226,10 +226,7 @@ static int stm32_usart_config_rs485(struct
>> uart_port *port, struct ktermios *ter
>>
>>      stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
>>
>> -    if (port->rs485_rx_during_tx_gpio)
>> -        gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
>> -                     !!(rs485conf->flags & SER_RS485_RX_DURING_TX));
>> -    else
>> +    if (!port->rs485_rx_during_tx_gpio)
>
> Should the ! be there?
>

Thats a good point, the "else" seems indeed to be wrong. It has been introduced
with the code that added the GPIO support (c54d48543689 "serial: stm32: Add support for rs485 RX_DURING_TX output GPIO")

I will fix it in the next version of this patch, thanks.


>>          rs485conf->flags |= SER_RS485_RX_DURING_TX;
>>
>>      if (rs485conf->flags & SER_RS485_ENABLED) {
>
> Kind Regards
> Maarten Brock
>

Thanks a lot for the review.

BR,
Lino

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

* Re: [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO
  2023-12-29 15:03     ` Lino Sanfilippo
@ 2024-01-02 15:53       ` Maarten Brock
  0 siblings, 0 replies; 13+ messages in thread
From: Maarten Brock @ 2024-01-02 15:53 UTC (permalink / raw
  To: Lino Sanfilippo
  Cc: Lino Sanfilippo, gregkh, jirislaby, ilpo.jarvinen,
	u.kleine-koenig, shawnguo, s.hauer, mcoquelin.stm32,
	alexandre.torgue, cniedermaier, hugo, linux-kernel, linux-serial,
	lukas, p.rosenberger, stable, Hugo Villeneuve

Lino Sanfilippo wrote on 2023-12-29 16:03:
> Hi,
> 
> On 25.12.23 at 13:31, Maarten Brock wrote:
>> Lino Sanfilippo wrote on 2023-12-25 12:35:
>>> diff --git a/drivers/tty/serial/serial_core.c 
>>> b/drivers/tty/serial/serial_core.c
>>> +static void uart_set_rs485_rx_during_tx(struct uart_port *port,
>>> +                    const struct serial_rs485 *rs485)
>>> +{
>>> +    if (!(rs485->flags & SER_RS485_ENABLED))
>>> +        return;
>> 
>> How about checking port->rs485_rx_during_tx_gpio here against NULL 
>> instead of
>> before every call?
> 
> gpiod_set_value_cansleep() already checks for a NULL pointer, so doing
> this check in the caller is not needed.

Ah, sorry, you're right.

>>> +    gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
>>> +                 !!(rs485->flags & SER_RS485_RX_DURING_TX));
>>> +}
>>> +
>>> @@ -1457,6 +1472,7 @@ static int uart_set_rs485_config(struct
>>> tty_struct *tty, struct uart_port *port,
>>>          return ret;
>>>      uart_sanitize_serial_rs485(port, &rs485);
>>>      uart_set_rs485_termination(port, &rs485);
>>> +    uart_set_rs485_rx_during_tx(port, &rs485);
>>> 
>>>      uart_port_lock_irqsave(port, &flags);
>>>      ret = port->rs485_config(port, &tty->termios, &rs485);
>>> @@ -1468,8 +1484,14 @@ static int uart_set_rs485_config(struct
>>> tty_struct *tty, struct uart_port *port,
>>>              port->ops->set_mctrl(port, port->mctrl);
>>>      }
>>>      uart_port_unlock_irqrestore(port, flags);
>>> -    if (ret)
>>> +    if (ret) {
>>> +        /* restore old GPIO settings */
>>> +        gpiod_set_value_cansleep(port->rs485_term_gpio,
>>> +            !!(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));
>> 
>> This does not look like restoring.
> 
> Hmm. The rx-during-tx and terminate-bus GPIOs may have changed before 
> the
> drivers rs485_config() was called. If that function fails, the GPIOs
> are set back to the values they had before (i.e what is still stored in
> the ports serial_rs485 struct). So what is wrong with the term 
> "restore"?

Oops, I missed that too that port-rs485 is not updated in this case.

Kind Regards,
Maarten Brock


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

end of thread, other threads:[~2024-01-02 16:24 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-25 11:35 [PATCH v6 0/7] Fixes and improvements for RS485 Lino Sanfilippo
2023-12-25 11:35 ` [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Lino Sanfilippo
2023-12-25 12:31   ` Maarten Brock
2023-12-29 15:03     ` Lino Sanfilippo
2024-01-02 15:53       ` Maarten Brock
2023-12-25 11:35 ` [PATCH v6 2/7] serial: core: set missing supported flag for RX during TX GPIO Lino Sanfilippo
2023-12-25 11:35 ` [PATCH v6 3/7] serial: core: fix sanitizing check for RTS settings Lino Sanfilippo
2023-12-25 11:35 ` [PATCH v6 4/7] serial: core: make sure RS485 cannot be enabled when it is not supported Lino Sanfilippo
2023-12-25 11:35 ` [PATCH v6 5/7] serial: core, imx: do not set RS485 enabled if " Lino Sanfilippo
2023-12-25 11:35 ` [PATCH v6 6/7] serial: omap: do not override settings for RS485 support Lino Sanfilippo
2023-12-25 22:29   ` kernel test robot
2023-12-25 23:30   ` kernel test robot
2023-12-25 11:35 ` [PATCH v6 7/7] serial: 8250_exar: Set missing rs485_supported flag Lino Sanfilippo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.