linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: "Johan Hovold" <johan@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Marek Behún" <kabel@kernel.org>
Cc: linux-usb@vger.kernel.org
Subject: [PATCH v3 6/7] USB: serial: ftdi_sio: Fix custom_divisor for TIOCGSERIAL and c_*speed for TCGETS2
Date: Sat, 24 Sep 2022 12:27:17 +0200	[thread overview]
Message-ID: <20220924102718.2984-7-pali@kernel.org> (raw)
In-Reply-To: <20220924102718.2984-1-pali@kernel.org>

When ASYNC_SPD_CUST is used, update custom_divisor field for TIOCGSERIAL
and c_*speed fields for TCGETS2 so that they correspond to the newly set
baud rate value.

So userspace TCGETS2 ioctls in new c_*speed fields will see the true baud
rate that is being used.

This is needed for switching userspace applications to use TCGETS2 API as
currently new c_*speed fields does not report correct values. Without this
change userspace applications still have to use old deprecated TIOCGSERIAL
to retrieve current baud rate.

Signed-off-by: Pali Rohár <pali@kernel.org>
Tested-by: Marek Behún <kabel@kernel.org>
Signed-off-by: Marek Behún <kabel@kernel.org>
---
Please note that if c_*speed fields of TCGETS2 are not going to be fixed
then userspace application cannot be switched to use this new TCGETS2 API
due to this issue.
---
 drivers/usb/serial/ftdi_sio.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 718c86db2900..79b00912c81c 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -1319,6 +1319,7 @@ static u32 get_ftdi_divisor(struct tty_struct *tty,
 {
 	struct ftdi_private *priv = usb_get_serial_port_data(port);
 	struct device *dev = &port->dev;
+	int fix_custom_divisor = 0;
 	u32 div_value = 0;
 	int div_okay = 1;
 	int baud;
@@ -1333,6 +1334,7 @@ static u32 get_ftdi_divisor(struct tty_struct *tty,
 	if (baud == 38400 &&
 	    ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&
 	     (priv->custom_divisor)) {
+		fix_custom_divisor = 1;
 		baud = DIV_ROUND_CLOSEST(priv->baud_base, priv->custom_divisor);
 		dev_dbg(dev, "%s - custom divisor %d sets baud rate to %d\n",
 			__func__, priv->custom_divisor, baud);
@@ -1426,7 +1428,19 @@ static u32 get_ftdi_divisor(struct tty_struct *tty,
 			ftdi_chip_name[priv->chip_type]);
 	}
 
+	/* Fix deprecated async-compatible custom_divisor hack and update tty baud rate */
+	if (fix_custom_divisor) {
+		priv->custom_divisor = DIV_ROUND_CLOSEST(priv->baud_base, baud);
+		old_baud = baud;
+		baud = 38400;
+	}
+
 	tty_encode_baud_rate(tty, baud, baud);
+
+	/* For async-compatible custom_divisor store into TCGETS2 c_*speed fields real baud rate */
+	if (fix_custom_divisor)
+		tty->termios.c_ispeed = tty->termios.c_ospeed = old_baud;
+
 	return div_value;
 }
 
@@ -2699,6 +2713,8 @@ static void ftdi_set_termios(struct tty_struct *tty,
 		dev_dbg(ddev, "%s: forcing baud rate for this device\n", __func__);
 		tty_encode_baud_rate(tty, priv->force_baud,
 					priv->force_baud);
+		termios->c_ispeed = termios->c_ospeed =
+			DIV_ROUND_CLOSEST(priv->baud_base, priv->custom_divisor);
 	}
 
 	/* Force RTS-CTS if this device requires it. */
-- 
2.20.1


  parent reply	other threads:[~2022-09-24 10:29 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-24 10:27 [PATCH v3 0/7] ftdi_sio driver improvements Pali Rohár
2022-09-24 10:27 ` [PATCH v3 1/7] USB: serial: ftdi_sio: Fix divisor overflow Pali Rohár
2022-11-28 14:54   ` Johan Hovold
2022-09-24 10:27 ` [PATCH v3 2/7] USB: serial: ftdi_sio: Add missing baud rate validation Pali Rohár
2022-11-28 15:00   ` Johan Hovold
2022-09-24 10:27 ` [PATCH v3 3/7] USB: serial: ftdi_sio: Extract SIO divisor code to function Pali Rohár
2022-09-24 10:47   ` Greg Kroah-Hartman
2022-10-09 12:17     ` Pali Rohár
2022-11-01 22:50       ` Pali Rohár
2022-11-02  1:47         ` Greg Kroah-Hartman
2022-11-26 16:29           ` Pali Rohár
2022-11-02  7:34         ` Johan Hovold
2022-11-28 15:10   ` Johan Hovold
2022-09-24 10:27 ` [PATCH v3 4/7] USB: serial: ftdi_sio: Do not reset baud rate to 9600 Baud on error Pali Rohár
2022-11-28 16:37   ` Johan Hovold
2022-09-24 10:27 ` [PATCH v3 5/7] USB: serial: ftdi_sio: Fix baud rate rounding for ASYNC_SPD_CUST Pali Rohár
2022-11-28 16:57   ` Johan Hovold
2022-09-24 10:27 ` Pali Rohár [this message]
2022-11-28 17:05   ` [PATCH v3 6/7] USB: serial: ftdi_sio: Fix custom_divisor for TIOCGSERIAL and c_*speed for TCGETS2 Johan Hovold
2022-09-24 10:27 ` [PATCH v3 7/7] USB: serial: ftdi_sio: Fill c_*speed fields with real baud rate Pali Rohár
2022-11-28 17:16   ` Johan Hovold

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=20220924102718.2984-7-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=kabel@kernel.org \
    --cc=linux-usb@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;
as well as URLs for NNTP newsgroup(s).