stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Johan Hovold <johan@kernel.org>
Cc: Jakub Nantl <jn@forever.cz>, Jonathan Olds <jontio@i4free.co.nz>,
	Michael Dreher <michael@5dot1.de>,
	linux-usb@vger.kernel.org, stable <stable@vger.kernel.org>
Subject: [PATCH] USB: serial: ch341: fix receiver regression
Date: Thu,  6 Feb 2020 12:18:19 +0100	[thread overview]
Message-ID: <20200206111819.20829-1-johan@kernel.org> (raw)

While assumed not to make a difference, not using the factor 2 prescaler
makes the receiver more susceptible to errors.

Specifically, there have been reports of problems with devices that
cannot generate a 115200 rate with a smaller error than 2.1% (e.g.
117647 bps). But this can also be reproduced with a low-speed RS232
tranceiver at 115200 when the input rate is close to nominal.

So whenever possible, enable the factor 2 prescaler and halve the
divisor in order to use settings closer to that of the previous
algorithm.

Fixes: 35714565089e ("USB: serial: ch341: reimplement line-speed handling")
Cc: stable <stable@vger.kernel.org>	# 5.5
Reported-by: Jakub Nantl <jn@forever.cz>
Tested-by: Jakub Nantl <jn@forever.cz>
Signed-off-by: Johan Hovold <johan@kernel.org>
---

I was able to reproduce this using a MAX232 tranceiver at 115200 even
with a close-to-nominal input rate, so the receiver is definitely more
sensitive to errors with the 2-prescaler disabled.

Note that I had this step in the first version of the new algorithm, but
couldn't convince myself that it wasn't redundant.

Reverse-engineering is fun.

Johan


 drivers/usb/serial/ch341.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index df582fe855f0..415c3d31492b 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -205,6 +205,16 @@ static int ch341_get_divisor(speed_t speed)
 			16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1)))
 		div++;
 
+	/*
+	 * Prefer lower base clock (fact = 0) if even divisor.
+	 *
+	 * Note that this makes the receiver more tolerant to errors.
+	 */
+	if (fact == 1 && div % 2 == 0) {
+		div /= 2;
+		fact = 0;
+	}
+
 	return (0x100 - div) << 8 | fact << 2 | ps;
 }
 
-- 
2.24.1


             reply	other threads:[~2020-02-06 11:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-06 11:18 Johan Hovold [this message]
2020-02-06 11:45 ` [PATCH] USB: serial: ch341: fix receiver regression Greg KH
2020-02-10  9:35   ` 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=20200206111819.20829-1-johan@kernel.org \
    --to=johan@kernel.org \
    --cc=jn@forever.cz \
    --cc=jontio@i4free.co.nz \
    --cc=linux-usb@vger.kernel.org \
    --cc=michael@5dot1.de \
    --cc=stable@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).