[PATCH 1/2] USB: serial: allow drivers to use more read urbs

From: Mateusz Sikora

Date: Sat Dec 05 2026 - 19:18:56 EST


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@xxxxxxxxx>
---
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)