Linux USB
 help / color / mirror / Atom feed
From: Mateusz Sikora <ms1qaz@gmail.com>
To: Johan Hovold <johan@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Mateusz Sikora <ms1qaz@gmail.com>
Subject: [PATCH 1/2] USB: serial: allow drivers to use more read urbs
Date: Sun,  6 Dec 2026 01:18:43 +0100	[thread overview]
Message-ID: <20261206001844.14858-2-ms1qaz@gmail.com> (raw)
In-Reply-To: <20261206001844.14858-1-ms1qaz@gmail.com>

The generic read implementation uses two bulk-in urbs per port. For
devices with small bulk endpoints this means that only two packets can
be in flight, which at higher line speeds leaves little margin for
completion latency on the host before the device runs out of buffer
space and starts dropping characters.

Add a num_read_urbs field to struct usb_serial_driver so that drivers
can request more read urbs (up to 32, the number of bits in the
read_urbs_free bitmap on 32-bit architectures). The default remains
two.

The remaining loops over the read urbs already handle unused (NULL)
entries.

Note that this grows struct usb_serial_port by 480 bytes on 64-bit.

This patch and its description were drafted with an AI assistant
(Claude) and reviewed and tested by me.

Signed-off-by: Mateusz Sikora <ms1qaz@gmail.com>
---
 drivers/usb/serial/usb-serial.c | 13 ++++++++++++-
 include/linux/usb/serial.h      | 10 ++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 4a19621f0..6de1823e1 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -889,19 +889,25 @@ static void find_endpoints(struct usb_serial *serial,
 	}
 }
 
+/* There is a bit for each read urb in read_urbs_free. */
+static_assert(USB_SERIAL_MAX_READ_URBS <= BITS_PER_LONG);
+
 static int setup_port_bulk_in(struct usb_serial_port *port,
 					struct usb_endpoint_descriptor *epd)
 {
 	struct usb_serial_driver *type = port->serial->type;
 	struct usb_device *udev = port->serial->dev;
 	int buffer_size;
+	int num_urbs;
 	int i;
 
 	buffer_size = max_t(int, type->bulk_in_size, usb_endpoint_maxp(epd));
 	port->bulk_in_size = buffer_size;
 	port->bulk_in_endpointAddress = epd->bEndpointAddress;
 
-	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
+	num_urbs = type->num_read_urbs ?: USB_SERIAL_DEFAULT_READ_URBS;
+
+	for (i = 0; i < num_urbs; ++i) {
 		set_bit(i, &port->read_urbs_free);
 		port->read_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
 		if (!port->read_urbs[i])
@@ -1476,6 +1482,11 @@ static int usb_serial_register(struct usb_serial_driver *driver)
 				driver->description);
 		return -EINVAL;
 	}
+	if (driver->num_read_urbs > USB_SERIAL_MAX_READ_URBS) {
+		WARN(1, "Serial driver %s requests too many read urbs\n",
+				driver->description);
+		return -EINVAL;
+	}
 
 	/* Prevent individual ports from being unbound. */
 	driver->driver.suppress_bind_attrs = true;
diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 5866f19d3..7df729a44 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -18,6 +18,10 @@
 /* The maximum number of ports one device can grab at once */
 #define MAX_NUM_PORTS		16
 
+/* The default and maximum number of bulk-in urbs per port */
+#define USB_SERIAL_DEFAULT_READ_URBS	2
+#define USB_SERIAL_MAX_READ_URBS	32
+
 /* USB serial flags */
 #define USB_SERIAL_WRITE_BUSY	0
 #define USB_SERIAL_THROTTLED	1
@@ -85,8 +89,8 @@ struct usb_serial_port {
 	struct urb		*read_urb;
 	__u8			bulk_in_endpointAddress;
 
-	unsigned char		*bulk_in_buffers[2];
-	struct urb		*read_urbs[2];
+	unsigned char		*bulk_in_buffers[USB_SERIAL_MAX_READ_URBS];
+	struct urb		*read_urbs[USB_SERIAL_MAX_READ_URBS];
 	unsigned long		read_urbs_free;
 
 	unsigned char		*bulk_out_buffer;
@@ -194,6 +198,7 @@ struct usb_serial_endpoints {
  * @num_bulk_out: minimum number of bulk-out endpoints
  * @num_interrupt_in: minimum number of interrupt-in endpoints
  * @num_interrupt_out: minimum number of interrupt-out endpoints
+ * @num_read_urbs: number of bulk-in urbs to allocate per port (0 = 2)
  * @bulk_in_size: minimum number of bytes to allocate for bulk-in buffer
  *	(0 = end-point size)
  * @bulk_out_size: bytes to allocate for bulk-out buffer (0 = end-point size)
@@ -246,6 +251,7 @@ struct usb_serial_driver {
 	unsigned char		num_bulk_out;
 	unsigned char		num_interrupt_in;
 	unsigned char		num_interrupt_out;
+	unsigned char		num_read_urbs;
 
 	size_t			bulk_in_size;
 	size_t			bulk_out_size;
-- 
2.54.0 (Apple Git-157)


  reply	other threads:[~2026-12-06  0:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  0:18 [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds Mateusz Sikora
2026-12-06  0:18 ` Mateusz Sikora [this message]
2026-12-06  0:18 ` [PATCH 2/2] USB: serial: ch341: use more read urbs Mateusz Sikora

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=20261206001844.14858-2-ms1qaz@gmail.com \
    --to=ms1qaz@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --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