[PATCH 5/5] serial: core: refactor uart_get_baud_rate() for readability
From: Hugo Villeneuve
Date: Tue Oct 20 2026 - 14:44:12 EST
From: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>
The current loop makes it hard to properly understand the code flow,
so replace it with more linear code using a validation function to
improve readability.
Signed-off-by: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>
---
drivers/tty/serial/serial_core.c | 103 ++++++++++++++++++-------------
1 file changed, 60 insertions(+), 43 deletions(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 08a6afa75af07..7457c6b44f325 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -453,6 +453,40 @@ uart_update_timeout(struct uart_port *port, unsigned int cflag,
}
EXPORT_SYMBOL(uart_update_timeout);
+static int
+uart_validate_baud_rate(const struct ktermios *termios, unsigned int altbaud,
+ unsigned int *ret_baud, unsigned int min, unsigned int max)
+{
+ unsigned int baud;
+ int rc;
+
+ baud = tty_termios_baud_rate(termios);
+
+ /*
+ * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
+ * Die! Die! Die!
+ */
+ if (altbaud && baud == 38400)
+ baud = altbaud;
+
+ if (baud == 0) {
+ /* Special case: B0 rate. */
+ baud = max(min, 9600);
+ rc = 1;
+ } else if (baud < min) {
+ baud = min; /* Clip to lowest chip supported rate. */
+ rc = -EINVAL;
+ } else if (baud > max) {
+ baud = max; /* Clip to highest chip supported rate. */
+ rc = -EINVAL;
+ } else {
+ rc = 0; /* Valid. */
+ }
+
+ *ret_baud = baud;
+ return rc;
+}
+
/**
* uart_get_baud_rate - return baud rate for a particular port
* @port: uart_port structure describing the port in question.
@@ -475,7 +509,7 @@ unsigned int
uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
const struct ktermios *old, unsigned int min, unsigned int max)
{
- unsigned int try;
+ int rc;
unsigned int baud;
unsigned int altbaud;
upf_t flags = port->flags & UPF_SPD_MASK;
@@ -498,54 +532,37 @@ uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
break;
}
- for (try = 0; try < 3; try++) {
- baud = tty_termios_baud_rate(termios);
+ rc = uart_validate_baud_rate(termios, altbaud, &baud, min, max);
+ if (rc >= 0)
+ return baud;
- /*
- * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
- * Die! Die! Die!
- */
- if (altbaud && baud == 38400)
- baud = altbaud;
+ /*
+ * Invalid baud rate detected. First clear c_cflag because
+ * tty_termios_encode_baud_rate() tests existing c_cflag before encoding new value.
+ */
+ termios->c_cflag &= ~CBAUD;
- /*
- * Special case: B0 rate.
- */
- if (baud == 0)
- return max(min, 9600);
+ altbaud = 0; /* No more spd_* kludge. */
- if (baud >= min && baud <= max)
- return baud;
-
- altbaud = 0; /* No more spd_* kludge for next iterations. */
-
- /*
- * If the range cannot be met then try again with
- * the old baud rate if possible.
- */
- termios->c_cflag &= ~CBAUD;
- if (old) {
- baud = tty_termios_baud_rate(old);
- tty_termios_encode_baud_rate(termios, baud, baud);
- old = NULL;
- continue;
+ /*
+ * If old termios is present, retry with the old baud rate. Otherwise,
+ * baud holds clipped value, and this will be used.
+ */
+ if (old) {
+ rc = uart_validate_baud_rate(old, altbaud, &baud, min, max);
+ if (rc == 1) {
+ /* Special case: old was B0 (hang-up). */
+ tty_termios_encode_baud_rate(termios, 0, 0); /* Encode B0 in termios. */
+ return baud; /* Return B0-mapped baud rate, not B0. */
}
-
- /*
- * As a last resort, if the range cannot be met then clip to
- * the nearest chip supported rate.
- */
- if (baud < min)
- baud = min;
- else
- baud = max;
-
- tty_termios_encode_baud_rate(termios, baud, baud);
}
- /* Should never happen */
- dev_warn_ratelimited(port->dev, "%s (%u): unable to find fallback baud rate\n",
- port->name, port->line);
+ tty_termios_encode_baud_rate(termios, baud, baud);
+ rc = uart_validate_baud_rate(termios, altbaud, &baud, min, max);
+ if (rc != 0)
+ dev_warn_ratelimited(port->dev, "%s (%u): unable to find fallback baud rate\n",
+ port->name, port->line);
+
return baud;
}
EXPORT_SYMBOL(uart_get_baud_rate);
--
2.47.3