mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
To: johan@kernel.org
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
Subject: [PATCH] USB: serial: cp210x: Adding tx_empty() to avoid cp2108 failure
Date: Wed,  7 Oct 2015 18:38:27 -0500	[thread overview]
Message-ID: <1444261107-4965-1-git-send-email-konstantin.shkolnyy@gmail.com> (raw)

Occasionally, writing data and immediately closing the port makes cp2108
stop responding. The device had to be unplugged to clear the error.
The failure is induced by shutting down the device while its Tx queue still has
unsent data. Reporting the correct amount of those data avoids the problem.
Adding tx_empty() has no adverse effect on other cp210x devices.

Signed-off-by: Konstantin Shkolnyy <konstantin.shkolnyy@gmail.com>
---
 drivers/usb/serial/cp210x.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index eac7cca..0189e64 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -38,6 +38,7 @@ static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
 							struct ktermios *);
 static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
 							struct ktermios*);
+static bool cp210x_tx_empty(struct usb_serial_port *port);
 static int cp210x_tiocmget(struct tty_struct *);
 static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
 static int cp210x_tiocmset_port(struct usb_serial_port *port,
@@ -214,6 +215,7 @@ static struct usb_serial_driver cp210x_device = {
 	.close			= cp210x_close,
 	.break_ctl		= cp210x_break_ctl,
 	.set_termios		= cp210x_set_termios,
+	.tx_empty		= cp210x_tx_empty,
 	.tiocmget		= cp210x_tiocmget,
 	.tiocmset		= cp210x_tiocmset,
 	.attach			= cp210x_startup,
@@ -249,6 +251,16 @@ static struct usb_serial_driver * const serial_drivers[] = {
 #define CP210X_GET_CHARS	0x0E
 #define CP210X_GET_PROPS	0x0F
 #define CP210X_GET_COMM_STATUS	0x10
+/* Data returned by CP210X_GET_COMM_STATUS -- h/w doc says it's 0x13 bytes */
+struct cp210x_comm_status {
+	u32     errors;
+	u32     hold_reasons;
+	u32     amount_in_in_queue;
+	u32     amount_in_out_queue;
+	u8      eof_received;
+	u8      wait_for_immediate;
+	u8      reserved;
+};
 #define CP210X_RESET		0x11
 #define CP210X_PURGE		0x12
 #define CP210X_SET_FLOW		0x13
@@ -479,6 +491,24 @@ static void cp210x_close(struct usb_serial_port *port)
 	cp210x_set_config_single(port, CP210X_IFC_ENABLE, UART_DISABLE);
 }
 
+static bool cp210x_tx_empty(struct usb_serial_port *port)
+{
+	int err;
+
+	/* get_config needs "array of integers large enough", so pad to 0x14 bytes */
+	struct cp210x_comm_status_container {
+		struct cp210x_comm_status  sts; /* 0x13 bytes */
+		u8                         pad_to_0x14_bytes;
+	} comm_sts_cont;
+
+	err = cp210x_get_config(port, CP210X_GET_COMM_STATUS, (unsigned int *) &comm_sts_cont, 0x13);
+
+	if (!err)
+		if (comm_sts_cont.sts.amount_in_out_queue)
+			return false;
+	return true;
+}
+
 /*
  * cp210x_get_termios
  * Reads the baud rate, data bits, parity, stop bits and flow control mode
-- 
1.9.1


             reply	other threads:[~2015-10-07 23:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-07 23:38 Konstantin Shkolnyy [this message]
2015-10-15 22:07 Konstantin Shkolnyy
2015-10-16 11:19 ` Sergei Shtylyov
2015-10-16 12:35   ` Konstantin Shkolnyy
2015-10-16 12:39     ` Johan Hovold
2015-10-16 12:55 ` Johan Hovold
2015-10-16 13:48   ` Konstantin Shkolnyy
2015-10-16 14:04     ` 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=1444261107-4965-1-git-send-email-konstantin.shkolnyy@gmail.com \
    --to=konstantin.shkolnyy@gmail.com \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.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

all inboxes | Powered by JetHome®