[PATCH] serial: max3100: unwind port allocation on add failure

From: 박명훈

Date: Tue Apr 28 2026 - 03:09:52 EST


From: Myeonghun Pak <mhun512@xxxxxxxxx>

max3100_probe() reports errors from uart_add_one_port(), but then
continues initialization and returns success. The device is left with
per-port state in max3100s[] even though serial core did not add the
port.

Return the uart_add_one_port() error instead. Free the per-port state
and, when no other chips remain, unregister the UART driver that probe
registered.

Fixes: 7831d56b0a35 ("tty: MAX3100")
Co-developed-by: Ijae Kim <ae878000@xxxxxxxxx>
Signed-off-by: Ijae Kim <ae878000@xxxxxxxxx>
Signed-off-by: Myeonghun Pak <mhun512@xxxxxxxxx>
---
drivers/tty/serial/max3100.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/max3100.c b/drivers/tty/serial/max3100.c
index 475b0a6efc..83a9db1819 100644
--- a/drivers/tty/serial/max3100.c
+++ b/drivers/tty/serial/max3100.c
@@ -732,13 +732,31 @@ static int max3100_probe(struct spi_device *spi)
device_property_read_u32(dev, "clock-frequency", &max3100s[i]->port.uartclk);

retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
- if (retval < 0)
+ if (retval < 0) {
dev_err_probe(dev, retval, "uart_add_one_port failed for line %d\n", i);
+ goto err_free_port;
+ }

/* set shutdown mode to save power. Will be woken-up on open */
max3100_sr(max3100s[i], MAX3100_WC | MAX3100_SHDN, &rx);
mutex_unlock(&max3100s_lock);
return 0;
+
+err_free_port:
+ kfree(max3100s[i]);
+ max3100s[i] = NULL;
+
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (max3100s[i])
+ break;
+
+ if (i == MAX_MAX3100) {
+ uart_unregister_driver(&max3100_uart_driver);
+ uart_driver_registered = 0;
+ }
+
+ mutex_unlock(&max3100s_lock);
+ return retval;
}

static void max3100_remove(struct spi_device *spi)
--
2.50.1