linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling
@ 2025-01-24 16:10 Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set Andy Shevchenko
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

It appears that the conversion to use uart_read_and_validate_port_properties()
broke 8250_of driver for the cases when UPIO_PORT is required.

Looking at the code there is an opportunity to clean up it a bit as well,
hence this mini-series.

Patches 1 and 2 (and 1 is most important) are the fixes to the helper function
followed by patch 3 that makes code robust against possible future changes in
the same area.

The top three patches are post-fix cleanups.

This is done in a series, but can be split to fix-series + cleanup-series for
the routing to the current and next Linux kernel releases. It's also possible
to route all of them as fixes as they are toughly linked to each other and have
not much code changes overall.

Andy Shevchenko (6):
  serial: port: Assign ->iotype correctly when ->iobase is set
  serial: port: Always update ->iotype in __uart_read_properties()
  serial: port: Make ->iotype validation global in
    __uart_read_properties()
  serial: 8250_of: Remove unneeded ->iotype assignment
  serial: 8250_platform: Remove unneeded ->iotype assignment
  serial: 8250_pnp: Remove unneeded ->iotype assignment

 drivers/tty/serial/8250/8250_of.c       |  1 -
 drivers/tty/serial/8250/8250_platform.c |  9 ---------
 drivers/tty/serial/8250/8250_pnp.c      | 10 ----------
 drivers/tty/serial/serial_port.c        | 12 +++++++-----
 4 files changed, 7 insertions(+), 25 deletions(-)

-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set
  2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
@ 2025-01-24 16:10 ` Andy Shevchenko
  2025-02-04 13:45   ` Greg Kroah-Hartman
  2025-01-24 16:10 ` [PATCH v1 2/6] serial: port: Always update ->iotype in __uart_read_properties() Andy Shevchenko
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

Currently the ->iotype is always assigned to the UPIO_MEM when
the respective property is not found. However, this will not
support the cases when user wants to have UPIO_PORT to be set
or preserved.  Support this scenario by checking ->iobase value
and default the ->iotype respectively.

Fixes: 1117a6fdc7c1 ("serial: 8250_of: Switch to use uart_read_port_properties()")
Fixes: e894b6005dce ("serial: port: Introduce a common helper to read properties")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/tty/serial/serial_port.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/serial_port.c b/drivers/tty/serial/serial_port.c
index d35f1d24156c..f28d0633fe6b 100644
--- a/drivers/tty/serial/serial_port.c
+++ b/drivers/tty/serial/serial_port.c
@@ -173,6 +173,7 @@ EXPORT_SYMBOL(uart_remove_one_port);
  * The caller is responsible to initialize the following fields of the @port
  *   ->dev (must be valid)
  *   ->flags
+ *   ->iobase
  *   ->mapbase
  *   ->mapsize
  *   ->regshift (if @use_defaults is false)
@@ -214,7 +215,7 @@ static int __uart_read_properties(struct uart_port *port, bool use_defaults)
 	/* Read the registers I/O access type (default: MMIO 8-bit) */
 	ret = device_property_read_u32(dev, "reg-io-width", &value);
 	if (ret) {
-		port->iotype = UPIO_MEM;
+		port->iotype = port->iobase ? UPIO_PORT : UPIO_MEM;
 	} else {
 		switch (value) {
 		case 1:
-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* [PATCH v1 2/6] serial: port: Always update ->iotype in __uart_read_properties()
  2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set Andy Shevchenko
@ 2025-01-24 16:10 ` Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 3/6] serial: port: Make ->iotype validation global " Andy Shevchenko
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

The documentation of the __uart_read_properties() states that
->iotype member is always altered after the function call, but
the code doesn't do that in the case when use_defaults == false
and the value of reg-io-width is unsupported. Make sure the code
follows the documentation.

Note, the current users of the uart_read_and_validate_port_properties()
will fail and the change doesn't affect their behaviour, neither
users of uart_read_port_properties() will be affected since the
alteration happens there even in the current code flow.

Fixes: e894b6005dce ("serial: port: Introduce a common helper to read properties")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/tty/serial/serial_port.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/serial_port.c b/drivers/tty/serial/serial_port.c
index f28d0633fe6b..85285c56fabf 100644
--- a/drivers/tty/serial/serial_port.c
+++ b/drivers/tty/serial/serial_port.c
@@ -228,11 +228,11 @@ static int __uart_read_properties(struct uart_port *port, bool use_defaults)
 			port->iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32;
 			break;
 		default:
+			port->iotype = UPIO_UNKNOWN;
 			if (!use_defaults) {
 				dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
 				return -EINVAL;
 			}
-			port->iotype = UPIO_UNKNOWN;
 			break;
 		}
 	}
-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* [PATCH v1 3/6] serial: port: Make ->iotype validation global in __uart_read_properties()
  2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 2/6] serial: port: Always update ->iotype in __uart_read_properties() Andy Shevchenko
@ 2025-01-24 16:10 ` Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 4/6] serial: 8250_of: Remove unneeded ->iotype assignment Andy Shevchenko
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

In order to make code robust against potential changes in the future
move ->iotype validation outside of switch in __uart_read_properties().
If any code will be added in between that might leave the ->iotype value
unknown the validation catches this up.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/tty/serial/serial_port.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/tty/serial/serial_port.c b/drivers/tty/serial/serial_port.c
index 85285c56fabf..2fc48cd63f6c 100644
--- a/drivers/tty/serial/serial_port.c
+++ b/drivers/tty/serial/serial_port.c
@@ -229,14 +229,15 @@ static int __uart_read_properties(struct uart_port *port, bool use_defaults)
 			break;
 		default:
 			port->iotype = UPIO_UNKNOWN;
-			if (!use_defaults) {
-				dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
-				return -EINVAL;
-			}
 			break;
 		}
 	}
 
+	if (!use_defaults && port->iotype == UPIO_UNKNOWN) {
+		dev_err(dev, "Unsupported reg-io-width (%u)\n", value);
+		return -EINVAL;
+	}
+
 	/* Read the address mapping base offset (default: no offset) */
 	ret = device_property_read_u32(dev, "reg-offset", &value);
 	if (ret)
-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* [PATCH v1 4/6] serial: 8250_of: Remove unneeded ->iotype assignment
  2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
                   ` (2 preceding siblings ...)
  2025-01-24 16:10 ` [PATCH v1 3/6] serial: port: Make ->iotype validation global " Andy Shevchenko
@ 2025-01-24 16:10 ` Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 5/6] serial: 8250_platform: " Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 6/6] serial: 8250_pnp: " Andy Shevchenko
  5 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

If ->iobase is set the default will be UPIO_PORT for ->iotype after
the uart_read_and_validate_port_properties() call. Hence no need
to assign that explicitly.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/tty/serial/8250/8250_of.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250_of.c b/drivers/tty/serial/8250/8250_of.c
index 64aed7efc569..11c860ea80f6 100644
--- a/drivers/tty/serial/8250/8250_of.c
+++ b/drivers/tty/serial/8250/8250_of.c
@@ -110,7 +110,6 @@ static int of_platform_serial_setup(struct platform_device *ofdev,
 	spin_lock_init(&port->lock);
 
 	if (resource_type(&resource) == IORESOURCE_IO) {
-		port->iotype = UPIO_PORT;
 		port->iobase = resource.start;
 	} else {
 		port->mapbase = resource.start;
-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* [PATCH v1 5/6] serial: 8250_platform: Remove unneeded ->iotype assignment
  2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
                   ` (3 preceding siblings ...)
  2025-01-24 16:10 ` [PATCH v1 4/6] serial: 8250_of: Remove unneeded ->iotype assignment Andy Shevchenko
@ 2025-01-24 16:10 ` Andy Shevchenko
  2025-01-24 16:10 ` [PATCH v1 6/6] serial: 8250_pnp: " Andy Shevchenko
  5 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

If ->iobase is set the default will be UPIO_PORT for ->iotype after
the uart_read_and_validate_port_properties() call. Hence no need
to assign that explicitly. Otherwise it will be UPIO_MEM.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/tty/serial/8250/8250_platform.c | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_platform.c b/drivers/tty/serial/8250/8250_platform.c
index 348b7fa2e94d..d8cbd8d87b0e 100644
--- a/drivers/tty/serial/8250/8250_platform.c
+++ b/drivers/tty/serial/8250/8250_platform.c
@@ -112,7 +112,6 @@ static int serial8250_probe_acpi(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct uart_8250_port uart = { };
 	struct resource *regs;
-	unsigned char iotype;
 	int ret, line;
 
 	regs = platform_get_mem_or_io(pdev, 0);
@@ -122,13 +121,11 @@ static int serial8250_probe_acpi(struct platform_device *pdev)
 	switch (resource_type(regs)) {
 	case IORESOURCE_IO:
 		uart.port.iobase = regs->start;
-		iotype = UPIO_PORT;
 		break;
 	case IORESOURCE_MEM:
 		uart.port.mapbase = regs->start;
 		uart.port.mapsize = resource_size(regs);
 		uart.port.flags = UPF_IOREMAP;
-		iotype = UPIO_MEM;
 		break;
 	default:
 		return -EINVAL;
@@ -147,12 +144,6 @@ static int serial8250_probe_acpi(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	/*
-	 * The previous call may not set iotype correctly when reg-io-width
-	 * property is absent and it doesn't support IO port resource.
-	 */
-	uart.port.iotype = iotype;
-
 	line = serial8250_register_8250_port(&uart);
 	if (line < 0)
 		return line;
-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* [PATCH v1 6/6] serial: 8250_pnp: Remove unneeded ->iotype assignment
  2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
                   ` (4 preceding siblings ...)
  2025-01-24 16:10 ` [PATCH v1 5/6] serial: 8250_platform: " Andy Shevchenko
@ 2025-01-24 16:10 ` Andy Shevchenko
  5 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2025-01-24 16:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Andy Shevchenko, Guanbing Huang, linux-kernel,
	linux-serial
  Cc: Jiri Slaby

If ->iobase is set the default will be UPIO_PORT for ->iotype after
the uart_read_and_validate_port_properties() call. Hence no need
to assign that explicitly. Otherwise it will be UPIO_MEM.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/tty/serial/8250/8250_pnp.c | 10 ----------
 1 file changed, 10 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_pnp.c b/drivers/tty/serial/8250/8250_pnp.c
index 7c06ae79d8e2..7a837fdf9df1 100644
--- a/drivers/tty/serial/8250/8250_pnp.c
+++ b/drivers/tty/serial/8250/8250_pnp.c
@@ -436,7 +436,6 @@ serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
 {
 	struct uart_8250_port uart, *port;
 	int ret, flags = dev_id->driver_data;
-	unsigned char iotype;
 	long line;
 
 	if (flags & UNKNOWN_DEV) {
@@ -448,14 +447,11 @@ serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
 	memset(&uart, 0, sizeof(uart));
 	if ((flags & CIR_PORT) && pnp_port_valid(dev, 2)) {
 		uart.port.iobase = pnp_port_start(dev, 2);
-		iotype = UPIO_PORT;
 	} else if (pnp_port_valid(dev, 0)) {
 		uart.port.iobase = pnp_port_start(dev, 0);
-		iotype = UPIO_PORT;
 	} else if (pnp_mem_valid(dev, 0)) {
 		uart.port.mapbase = pnp_mem_start(dev, 0);
 		uart.port.mapsize = pnp_mem_len(dev, 0);
-		iotype = UPIO_MEM;
 		uart.port.flags = UPF_IOREMAP;
 	} else
 		return -ENODEV;
@@ -471,12 +467,6 @@ serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
 	if (ret)
 		return ret;
 
-	/*
-	 * The previous call may not set iotype correctly when reg-io-width
-	 * property is absent and it doesn't support IO port resource.
-	 */
-	uart.port.iotype = iotype;
-
 	if (flags & CIR_PORT) {
 		uart.port.flags |= UPF_FIXED_PORT | UPF_FIXED_TYPE;
 		uart.port.type = PORT_8250_CIR;
-- 
2.43.0.rc1.1336.g36b5255a03ac


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

* Re: [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set
  2025-01-24 16:10 ` [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set Andy Shevchenko
@ 2025-02-04 13:45   ` Greg Kroah-Hartman
  0 siblings, 0 replies; 8+ messages in thread
From: Greg Kroah-Hartman @ 2025-02-04 13:45 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Guanbing Huang, linux-kernel, linux-serial, Jiri Slaby

On Fri, Jan 24, 2025 at 06:10:46PM +0200, Andy Shevchenko wrote:
> Currently the ->iotype is always assigned to the UPIO_MEM when
> the respective property is not found. However, this will not
> support the cases when user wants to have UPIO_PORT to be set
> or preserved.  Support this scenario by checking ->iobase value
> and default the ->iotype respectively.
> 
> Fixes: 1117a6fdc7c1 ("serial: 8250_of: Switch to use uart_read_port_properties()")
> Fixes: e894b6005dce ("serial: port: Introduce a common helper to read properties")

You forgot the cc: stable lines.  I'll go add them in, but be more
careful next time please.

thanks,

greg k-h

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

end of thread, other threads:[~2025-02-04 13:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-24 16:10 [PATCH v1 0/6] serial: port: Fix UPIO_PORT iotype handling Andy Shevchenko
2025-01-24 16:10 ` [PATCH v1 1/6] serial: port: Assign ->iotype correctly when ->iobase is set Andy Shevchenko
2025-02-04 13:45   ` Greg Kroah-Hartman
2025-01-24 16:10 ` [PATCH v1 2/6] serial: port: Always update ->iotype in __uart_read_properties() Andy Shevchenko
2025-01-24 16:10 ` [PATCH v1 3/6] serial: port: Make ->iotype validation global " Andy Shevchenko
2025-01-24 16:10 ` [PATCH v1 4/6] serial: 8250_of: Remove unneeded ->iotype assignment Andy Shevchenko
2025-01-24 16:10 ` [PATCH v1 5/6] serial: 8250_platform: " Andy Shevchenko
2025-01-24 16:10 ` [PATCH v1 6/6] serial: 8250_pnp: " Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).