mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hugo Villeneuve <hugo@hugovil.com>
To: gregkh@linuxfoundation.org, jirislaby@kernel.org, alan@linux.intel.com
Cc: hugo@hugovil.com, gregkh@suse.de, linux-kernel@vger.kernel.org,
	linux-serial@vger.kernel.org,
	Hugo Villeneuve <hvilleneuve@dimonoff.com>
Subject: [PATCH 5/5] serial: core: refactor uart_get_baud_rate() for readability
Date: Tue, 20 Oct 2026 14:41:21 -0400	[thread overview]
Message-ID: <20261020184131.2893575-6-hugo@hugovil.com> (raw)
In-Reply-To: <20261020184131.2893575-1-hugo@hugovil.com>

From: Hugo Villeneuve <hvilleneuve@dimonoff.com>

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@dimonoff.com>
---
 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


      parent reply	other threads:[~2026-10-20 18:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20 18:41 [PATCH 0/5] serial: core: fixes and improvements to uart_get_baud_rate() Hugo Villeneuve
2026-10-20 18:41 ` [PATCH 1/5] serial: core: fix division by zero if old termios is present but has invalid rate Hugo Villeneuve
2026-10-20 18:41 ` [PATCH 2/5] serial: core: clip out-of-range baud rate to min/max, not min+1/max-1 Hugo Villeneuve
2026-10-20 18:41 ` [PATCH 3/5] serial: core: always return in-range baud rate even in case of failure Hugo Villeneuve
2026-10-20 18:41 ` [PATCH 4/5] serial: core: avoid redundant baud assignment without any UPF_SPD_* flags Hugo Villeneuve
2026-10-20 18:41 ` Hugo Villeneuve [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261020184131.2893575-6-hugo@hugovil.com \
    --to=hugo@hugovil.com \
    --cc=alan@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=gregkh@suse.de \
    --cc=hvilleneuve@dimonoff.com \
    --cc=jirislaby@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®