Skip to contentSkip to conversation

usb: xhci: Relax high-speed bulk max packet size rules

PATCH In review Michal Pecio 3 participants 3 messages Oct 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: xhci: Relax high-speed bulk max packet size rules

Nicolai reported multiple dodgy high-speed devices still being made which declare out-of-spec bulk wMaxPacketSize and actually require the host to use it for correct operation. Apparently, Windows copies this field to the EP Context verbatim and other Linux HCDs like EHCI and DWC2 do similar things for compatibility's sake.

We are also aware of devices like astronomical cameras which use 1KB to reduce protocol overhead, and audio devices with MIDI endpoints that specify unusual MPS like 8 or 4 (the latter was full-speed) and work with larger sizes just fine because they never do multi-packet transfers where MPS really matters.

I found that sensible (power of two, 8 - 512) MPS are usable on many xHCI controllers from vendors like AMD, ASMedia, Fresco, NEC/Renesas, and VIA. Nicolai also tested two Raspberry Pi SBCs and a modern Intel machine running Windows 11 and 64B endpoints worked.

OTOH, Etron rejects Configure Endpoint if MPS != 512. For such cases, commit e4f47e3675e6 ("USB: xHCI: override bogus bulk wMaxPacketSize values") sets HS bulk MPS to 512, which obviously breaks those dodgy devices that perform multi-packet transfers with other size.

Problematic devices known so far use power of two MPS of at least 64, so try to recognize them with this heuristic. Non power of two or small MPS seems to indicate a clueless device which treats bulk like interrupt and specifies intended transfer size; we will use standard MPS here to avoid trouble with potential other HCs like Etron.

---
 drivers/usb/host/xhci-mem.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-mem.c +15 -3
index 83ed26c4f9e4..bbde39ccc4f9 100644
@@ -1473,10 +1473,22 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
 	/* Allow 3 retries for everything but isoc, set CErr = 3 */
 	if (!usb_endpoint_xfer_isoc(&ep->desc))
 		err_count = 3;
-	/* HS bulk max packet should be 512, FS bulk supports 8, 16, 32 or 64 */
+
 	if (usb_endpoint_xfer_bulk(&ep->desc)) {
-		if (udev->speed == USB_SPEED_HIGH)
-			max_packet = 512;
+		/*
+		 * HS MPS should be 512, but Windows lets devices use anything.
+		 * Etron rejects MPS != 512, many HCs dislike >512 and a few <8.
+		 * Non power of 2 troubles Fresco, hopefully it means the device
+		 * uses single-packet transfers only and 512 will work. Some 64B
+		 * endpoints (shared FS / HS logic?) use multi-packet transfers,
+		 * so MPS must be exact. 1KB EPs are unlikely to tolerate less.
+		 */
+		if (udev->speed == USB_SPEED_HIGH) {
+			if ((xhci->quirks & XHCI_ETRON_HOST) ||
+			    !is_power_of_2(max_packet) || max_packet < 64)
+				max_packet = 512;
+		}
+		/* FS bulk supports 8, 16, 32 or 64 */
 		if (udev->speed == USB_SPEED_FULL) {
 			max_packet = rounddown_pow_of_two(max_packet);
 			max_packet = clamp_val(max_packet, 8, 64);
Signature · 1 line
2.48.1
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

Nicolai reported multiple dodgy high-speed devices still being made which declare out-of-spec bulk wMaxPacketSize and actually require the host to use it for correct operation. Apparently, Windows copies this field to the EP Context verbatim and other Linux HCDs like EHCI and DWC2 do similar things for compatibility's sake.

We are also aware of devices like astronomical cameras which use 1KB to reduce protocol overhead, and audio devices with MIDI endpoints that specify unusual MPS like 8 or 4 (the latter was full-speed) and work with larger sizes just fine because they never do multi-packet transfers where MPS really matters.

I found that sensible (power of two, 8 - 512) MPS are usable on many xHCI controllers from vendors like AMD, ASMedia, Fresco, NEC/Renesas, and VIA. Nicolai also tested two Raspberry Pi SBCs and a modern Intel

Quoted 6 lines

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Michal Pecio

Hi Michal

Quoted 5 lines · Michal Pecio

Nicolai reported multiple dodgy high-speed devices still being made which declare out-of-spec bulk wMaxPacketSize and actually require the host to use it for correct operation. Apparently, Windows copies this field to the EP Context verbatim and other Linux HCDs like EHCI and DWC2 do similar things for compatibility's sake.

Thanks for picking this up!

Quoted 32 lines

We are also aware of devices like astronomical cameras which use 1KB to reduce protocol overhead, and audio devices with MIDI endpoints that specify unusual MPS like 8 or 4 (the latter was full-speed) and work with larger sizes just fine because they never do multi-packet transfers where MPS really matters.

I found that sensible (power of two, 8 - 512) MPS are usable on many xHCI controllers from vendors like AMD, ASMedia, Fresco, NEC/Renesas, and VIA. Nicolai also tested two Raspberry Pi SBCs and a modern Intel machine running Windows 11 and 64B endpoints worked.

OTOH, Etron rejects Configure Endpoint if MPS != 512. For such cases, commit e4f47e3675e6 ("USB: xHCI: override bogus bulk wMaxPacketSize values") sets HS bulk MPS to 512, which obviously breaks those dodgy devices that perform multi-packet transfers with other size.

Problematic devices known so far use power of two MPS of at least 64, so try to recognize them with this heuristic. Non power of two or small MPS seems to indicate a clueless device which treats bulk like interrupt and specifies intended transfer size; we will use standard MPS here to avoid trouble with potential other HCs like Etron.

Closes:
https://lore.kernel.org/linux-usb/[email protected]/
Link:
https://lore.kernel.org/linux-usb/[email protected]/
Link:
https://lore.kernel.org/alsa-devel/CAE-xCyPhrS6pgY0T9SG0TCubSi0CE2Yd5H=ttOzBmvgrR8fTPA@mail.gmail.com/
Link:
https://lore.kernel.org/linux-usb/[email protected]/

Quoted 3 lines

---
[...]

I ran the patch on a Raspberry Pi CM4S with a Rigol DP832A. Multi-packet replies up to ~1 KB and writes up to 760 bytes work fine. dmesg doesn't show anything suspicious.

Regards,
Nicolai

Signature · 1 line
Nicolai Buchwitz <[email protected]>