[PATCH v3] USB: serial: pl2303: account for deficits of clones

Jan Kiszka posted 1 patch 2 months, 2 weeks ago
drivers/usb/serial/pl2303.c | 41 ++++++++++++++++++++++++++++++++++++-
1 file changed, 40 insertions(+), 1 deletion(-)
[PATCH v3] USB: serial: pl2303: account for deficits of clones
Posted by Jan Kiszka 2 months, 2 weeks ago
From: Jan Kiszka <jan.kiszka@siemens.com>

There are apparently incomplete clones of the HXD type chip in use.
Those return -EPIPE on GET_LINE_REQUEST and BREAK_REQUEST. Avoid
flooding the kernel log with those errors. Detect them during startup
and then use the line_settings cache instead of GET_LINE_REQUEST. Signal
missing break support via -ENOTTY.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---

Changes in v3:
 - use quirks field instead of new type (suggested by Michal)

 drivers/usb/serial/pl2303.c | 41 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/serial/pl2303.c b/drivers/usb/serial/pl2303.c
index d93f5d584557..bc433178a6b0 100644
--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -31,6 +31,7 @@
 #define PL2303_QUIRK_UART_STATE_IDX0		BIT(0)
 #define PL2303_QUIRK_LEGACY			BIT(1)
 #define PL2303_QUIRK_ENDPOINT_HACK		BIT(2)
+#define PL2304_QUIRK_NO_BREAK_GETLINE		BIT(3)
 
 static const struct usb_device_id id_table[] = {
 	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID),
@@ -466,6 +467,28 @@ static int pl2303_detect_type(struct usb_serial *serial)
 	return -ENODEV;
 }
 
+static bool pl2303_is_hxd_clone(struct usb_serial *serial)
+{
+	struct usb_device *udev = serial->dev;
+	unsigned char *buf;
+	int ret;
+
+	buf = kmalloc(7, GFP_KERNEL);
+	if (!buf) {
+		dev_err(&serial->interface->dev,
+			"could not check for clone type, not enough memory\n");
+		return false;
+	}
+
+	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
+			      GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
+			      0, 0, buf, 7, 100);
+
+	kfree(buf);
+
+	return ret == -EPIPE;
+}
+
 static int pl2303_startup(struct usb_serial *serial)
 {
 	struct pl2303_serial_private *spriv;
@@ -488,6 +511,9 @@ static int pl2303_startup(struct usb_serial *serial)
 	spriv->quirks = (unsigned long)usb_get_serial_data(serial);
 	spriv->quirks |= spriv->type->quirks;
 
+	if (type == TYPE_HXD && pl2303_is_hxd_clone(serial))
+		spriv->quirks |= PL2304_QUIRK_NO_BREAK_GETLINE;
+
 	usb_set_serial_data(serial, spriv);
 
 	if (type != TYPE_HXN) {
@@ -724,9 +750,18 @@ static void pl2303_encode_baud_rate(struct tty_struct *tty,
 static int pl2303_get_line_request(struct usb_serial_port *port,
 							unsigned char buf[7])
 {
-	struct usb_device *udev = port->serial->dev;
+	struct usb_serial *serial = port->serial;
+	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
+	struct usb_device *udev = serial->dev;
 	int ret;
 
+	if (spriv->quirks & PL2304_QUIRK_NO_BREAK_GETLINE) {
+		struct pl2303_private *priv = usb_get_serial_port_data(port);
+
+		memcpy(buf, priv->line_settings, 7);
+		return 0;
+	}
+
 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
 				GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
 				0, 0, buf, 7, 100);
@@ -1063,9 +1098,13 @@ static int pl2303_carrier_raised(struct usb_serial_port *port)
 static int pl2303_set_break(struct usb_serial_port *port, bool enable)
 {
 	struct usb_serial *serial = port->serial;
+	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
 	u16 state;
 	int result;
 
+	if (spriv->quirks & PL2304_QUIRK_NO_BREAK_GETLINE)
+		return -ENOTTY;
+
 	if (enable)
 		state = BREAK_ON;
 	else
-- 
2.43.0
Re: [PATCH v3] USB: serial: pl2303: account for deficits of clones
Posted by Johan Hovold 3 weeks, 2 days ago
On Thu, Sep 12, 2024 at 10:37:39AM +0200, Jan Kiszka wrote:
> From: Jan Kiszka <jan.kiszka@siemens.com>
> 
> There are apparently incomplete clones of the HXD type chip in use.
> Those return -EPIPE on GET_LINE_REQUEST and BREAK_REQUEST. Avoid
> flooding the kernel log with those errors. Detect them during startup
> and then use the line_settings cache instead of GET_LINE_REQUEST. Signal
> missing break support via -ENOTTY.
> 
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
> 
> Changes in v3:
>  - use quirks field instead of new type (suggested by Michal)

>  #define PL2303_QUIRK_UART_STATE_IDX0		BIT(0)
>  #define PL2303_QUIRK_LEGACY			BIT(1)
>  #define PL2303_QUIRK_ENDPOINT_HACK		BIT(2)
> +#define PL2304_QUIRK_NO_BREAK_GETLINE		BIT(3)

I believe you meant to use a "PL2303" prefix here as well.

> +static bool pl2303_is_hxd_clone(struct usb_serial *serial)
> +{
> +	struct usb_device *udev = serial->dev;
> +	unsigned char *buf;
> +	int ret;
> +
> +	buf = kmalloc(7, GFP_KERNEL);
> +	if (!buf) {
> +		dev_err(&serial->interface->dev,
> +			"could not check for clone type, not enough memory\n");

And there's no need to log out-of-memory errors (e.g. which would
already have been logged by allocator).

> +		return false;
> +	}
> +
> +	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
> +			      GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
> +			      0, 0, buf, 7, 100);
> +
> +	kfree(buf);
> +
> +	return ret == -EPIPE;
> +}

I fixed up the above when applying. Looks nice and clean otherwise.

Thanks.

Johan