From: Agalakov Daniil <ade@amicon.ru>
To: Johan Hovold <johan@kernel.org>
Cc: Agalakov Daniil <ade@amicon.ru>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
<linux-usb@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<lvc-project@linuxtesting.org>, Roman Razov <rrv@amicon.ru>,
Iskhakov Daniil <dish@amicon.ru>
Subject: [PATCH] USB: serial: mos7720: fix actual baud rate not reported back to TTY
Date: Wed, 29 Apr 2026 16:38:12 +0300 [thread overview]
Message-ID: <20260429133815.317534-1-ade@amicon.ru> (raw)
The calc_baud_rate_divisor() function computes a divisor from the
requested baud rate using integer arithmetic, which means the actual
hardware baud rate may differ from the requested one. Previously this
rounded value was never propagated back to the TTY layer, so
tty_encode_baud_rate() was called with the requested rate rather than
the actual one.
Change baudrate parameters in calc_baud_rate_divisor() and
send_cmd_write_baud_rate() to pointers so the actual resulting baud
rate can be written back to the caller. Update *baudrate with the
computed value (230400 / custom) after divisor calculation.
In change_port_settings(), pass &baud to send_cmd_write_baud_rate()
and guard tty_encode_baud_rate() with a status check, so the TTY is
only updated on success and receives the true hardware baud rate.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes: 65d063ab21fe ("USB: mos7720: clean up termios")
Co-developed-by: Iskhakov Daniil <dish@amicon.ru>
Signed-off-by: Iskhakov Daniil <dish@amicon.ru>
Signed-off-by: Agalakov Daniil <ade@amicon.ru>
---
drivers/usb/serial/mos7720.c | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c
index 94459408e7fb..1052217edf26 100644
--- a/drivers/usb/serial/mos7720.c
+++ b/drivers/usb/serial/mos7720.c
@@ -1264,9 +1264,9 @@ static const struct divisor_table_entry divisor_table[] = {
/*****************************************************************************
* calc_baud_rate_divisor
* this function calculates the proper baud rate divisor for the specified
- * baud rate.
+ * baud rate and updates the baudrate parameter with the actual value.
*****************************************************************************/
-static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
+static int calc_baud_rate_divisor(struct usb_serial_port *port, int *baudrate, int *divisor)
{
int i;
__u16 custom;
@@ -1274,10 +1274,10 @@ static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, in
__u16 round;
- dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
+ dev_dbg(&port->dev, "%s - %d\n", __func__, *baudrate);
for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
- if (divisor_table[i].baudrate == baudrate) {
+ if (divisor_table[i].baudrate == *baudrate) {
*divisor = divisor_table[i].divisor;
return 0;
}
@@ -1285,18 +1285,19 @@ static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, in
/* After trying for all the standard baud rates *
* Try calculating the divisor for this baud rate */
- if (baudrate > 75 && baudrate < 230400) {
+ if (*baudrate > 75 && *baudrate < 230400) {
/* get the divisor */
- custom = (__u16)(230400L / baudrate);
+ custom = (__u16)(230400L / *baudrate);
/* Check for round off */
- round1 = (__u16)(2304000L / baudrate);
+ round1 = (__u16)(2304000L / *baudrate);
round = (__u16)(round1 - (custom * 10));
if (round > 4)
custom++;
*divisor = custom;
+ *baudrate = 230400L / custom;
- dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
+ dev_dbg(&port->dev, "Baud %d = %d\n", *baudrate, custom);
return 0;
}
@@ -1307,10 +1308,10 @@ static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, in
/*
* send_cmd_write_baud_rate
* this function sends the proper command to change the baud rate of the
- * specified port.
+ * specified port and updates the baudrate parameter with the actual value.
*/
static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
- int baudrate)
+ int *baudrate)
{
struct usb_serial_port *port;
struct usb_serial *serial;
@@ -1325,7 +1326,7 @@ static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
serial = port->serial;
number = port->port_number;
- dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
+ dev_dbg(&port->dev, "%s - baud = %d\n", __func__, *baudrate);
/* Calculate the Divisor */
status = calc_baud_rate_divisor(port, baudrate, &divisor);
@@ -1474,10 +1475,8 @@ static void change_port_settings(struct tty_struct *tty,
}
dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
- status = send_cmd_write_baud_rate(mos7720_port, baud);
- /* FIXME: needs to write actual resulting baud back not just
- blindly do so */
- if (cflag & CBAUD)
+ status = send_cmd_write_baud_rate(mos7720_port, &baud);
+ if (!status && (cflag & CBAUD))
tty_encode_baud_rate(tty, baud, baud);
/* Enable Interrupts */
write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
--
2.51.0
reply other threads:[~2026-04-29 13:53 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260429133815.317534-1-ade@amicon.ru \
--to=ade@amicon.ru \
--cc=dish@amicon.ru \
--cc=gregkh@linuxfoundation.org \
--cc=johan@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=lvc-project@linuxtesting.org \
--cc=rrv@amicon.ru \
/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