Skip to contentSkip to conversation

usbcore: Add quirk for 255-bytes initial config read

PATCH Superseded Nikhil Solanke 5 participants 25 messages Jun 23 – Jul 6

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usbcore: Add quirk for 255-bytes initial config read

Certain third-party USB game controllers exposing (or spoofing) an Xbox 360-compatible interface (VID:PID 045e:028e) fail to enumerate under Linux. The device disconnects from the bus without responding to the initial GET_DESCRIPTOR(CONFIGURATION) request, and the kernel logs 'unable to read config index 0 descriptor/start: -71'.

The device then falls back to a secondary Android HID mode (with a different VID:PID), losing XInput functionality including rumble support. The failure reproduces across multiple machines, host controller types, and kernel versions including current mainline and LTS. The device enumerates correctly and remains in XInput mode under Windows. Notably, the device enumerates correctly in Android mode when the same 9-byte request is issued for that mode's configuration descriptor, confirming the firmware bug is specific to the XInput mode.

usbmon traces from Linux and Wireshark/USBPcap traces from Windows are identical up to the point of failure, with no visible protocol-level difference explaining the divergence. The root cause was identified when Michal Pecio discovered via a QEMU bus-level capture that Windows does not use wLength=9 for the initial config descriptor request; it uses wLength=255. Alan Stern subsequently confirmed this with a bus analyzer on a different USB 2.0 device, and Michal verified the behavior goes back to Windows 95 OSR2.1.

So, add a new quirk flag USB_QUIRK_CONFIG_SIZE which causes usb_get_configuration() to issue a 255 byte sized configuration request instead of USB_DT_CONFIG_SIZE (9) for the initial GET_DESCRIPTOR(CONFIGURATION) request, mimicking long-standing Windows behavior.

---
Changes in v2:
- Add Documentation
- Naming changes
- Refactored to have a better flow with existing code.

 .../admin-guide/kernel-parameters.txt         |  9 +++
 drivers/usb/core/config.c                     | 61 ++++++++++++++-----
 drivers/usb/core/hub.c                        |  6 +-
 drivers/usb/core/quirks.c                     |  4 ++
 include/linux/usb/quirks.h                    |  3 +
 5 files changed, 67 insertions(+), 16 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +9 -0
index 97007f4f69d4..af4bf0ef2c7b 100644
@@ -8158,6 +8158,15 @@ Kernel parameters
 				q = USB_QUIRK_FORCE_ONE_CONFIG (Device
 					claims zero configurations,
 					forcing to 1);
+                r = USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE (Device
+                    fails during initialization when asked for
+                    9-bytes configuration desciptor request. Ask
+                    for 255-bytes request instead to mirror
+                    Windows' behavior. This quirk is originally
+                    meant to fix some quirky gamepads that refuse
+                    to connect in their XInput mode. But it can also
+                    potentially fix issues with other USB devices
+                    that work on Windows but not on Linux)
 			Example: quirks=0781:5580:bk,0a5c:5834:gij
 
 	usbhid.mousepoll=
drivers/usb/core/config.c +47 -14

113 lines not on this page · raw message

drivers/usb/core/hub.c +4 -2
index 24960ba9caa9..9acd278666fc 100644
@@ -2527,8 +2527,10 @@ static int usb_enumerate_device(struct usb_device *udev)
 		err = usb_get_configuration(udev);
 		if (err < 0) {
 			if (err != -ENODEV)
-				dev_err(&udev->dev, "can't read configurations, error %d\n",
-						err);
+				dev_err(&udev->dev, "can't read configurations, "
+					"for device %04x:%04x, error %d\n",
+					le16_to_cpu(udev->descriptor.idVendor),
+					le16_to_cpu(udev->descriptor.idProduct), err);
 			return err;
 		}
 	}
drivers/usb/core/quirks.c +4 -0
index 87810eff974e..df670b0b66fe 100644
@@ -142,6 +142,10 @@ static int quirks_param_set(const char *value, const struct kernel_param *kp)
 				break;
 			case 'q':
 				flags |= USB_QUIRK_FORCE_ONE_CONFIG;
+				break;
+			case 'r':
+				flags |= USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE;
+				break;
 			/* Ignore unrecognized flag characters */
 			}
 		}
include/linux/usb/quirks.h +3 -0
index b3cc7beab4a3..a4043b33c2c2 100644
@@ -81,4 +81,7 @@
 /* Device claims zero configurations, forcing to 1 */
 #define USB_QUIRK_FORCE_ONE_CONFIG		BIT(18)
 
+/* Use a 255 bytes config descriptor request mirroring windows behavior */
+#define USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE	BIT(19)
+
 #endif /* __LINUX_USB_QUIRKS_H */
Signature · 1 line
2.54.0
reviewer
#1 lore raw
Quoted 55 lines · Nikhil Solanke

Certain third-party USB game controllers exposing (or spoofing) an Xbox 360-compatible interface (VID:PID 045e:028e) fail to enumerate under Linux. The device disconnects from the bus without responding to the initial GET_DESCRIPTOR(CONFIGURATION) request, and the kernel logs 'unable to read config index 0 descriptor/start: -71'.

The device then falls back to a secondary Android HID mode (with a different VID:PID), losing XInput functionality including rumble support. The failure reproduces across multiple machines, host controller types, and kernel versions including current mainline and LTS. The device enumerates correctly and remains in XInput mode under Windows. Notably, the device enumerates correctly in Android mode when the same 9-byte request is issued for that mode's configuration descriptor, confirming the firmware bug is specific to the XInput mode.

usbmon traces from Linux and Wireshark/USBPcap traces from Windows are identical up to the point of failure, with no visible protocol-level difference explaining the divergence. The root cause was identified when Michal Pecio discovered via a QEMU bus-level capture that Windows does not use wLength=9 for the initial config descriptor request; it uses wLength=255. Alan Stern subsequently confirmed this with a bus analyzer on a different USB 2.0 device, and Michal verified the behavior goes back to Windows 95 OSR2.1.

So, add a new quirk flag USB_QUIRK_CONFIG_SIZE which causes usb_get_configuration() to issue a 255 byte sized configuration request instead of USB_DT_CONFIG_SIZE (9) for the initial GET_DESCRIPTOR(CONFIGURATION) request, mimicking long-standing Windows behavior.

---
Changes in v2:
- Add Documentation
- Naming changes
- Refactored to have a better flow with existing code.

 .../admin-guide/kernel-parameters.txt         |  9 +++
 drivers/usb/core/config.c                     | 61 ++++++++++++++-----
 drivers/usb/core/hub.c                        |  6 +-
 drivers/usb/core/quirks.c                     |  4 ++
 include/linux/usb/quirks.h                    |  3 +
 5 files changed, 67 insertions(+), 16 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index 97007f4f69d4..af4bf0ef2c7b 100644

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 8163

Quoted 6 lines
Documentation/admin-guide/kernel-parameters.txt +3 -0
@@ -8158,6 +8158,15 @@ Kernel parameters
 				q = USB_QUIRK_FORCE_ONE_CONFIG (Device
 					claims zero configurations,
 					forcing to 1);
+                r = USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE (Device
+                    fails during initialization when asked for
+                    9-bytes configuration desciptor request. Ask

descriptor

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 8169

Quoted 6 lines
Documentation/admin-guide/kernel-parameters.txt +6 -0
+                    for 255-bytes request instead to mirror
+                    Windows' behavior. This quirk is originally
+                    meant to fix some quirky gamepads that refuse
+                    to connect in their XInput mode. But it can also
+                    potentially fix issues with other USB devices
+                    that work on Windows but not on Linux)

add ending '.'

For all lines added here, use tabs instead of spaces for indentation.

Quoted 3 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0
 			Example: quirks=0781:5580:bk,0a5c:5834:gij
 
 	usbhid.mousepoll=
Signature · 1 line
~Randy
#2 lore raw
Quoted 3 lines

add ending '.'

For all lines added here, use tabs instead of spaces for indentation.

Done! Waiting for any other changes before submitting v3

#3 lore raw
In reply to Nikhil Solanke

This is looking better. A few more things to mention below, but otherwise okay.

Quoted 30 lines · Nikhil Solanke

Certain third-party USB game controllers exposing (or spoofing) an Xbox 360-compatible interface (VID:PID 045e:028e) fail to enumerate under Linux. The device disconnects from the bus without responding to the initial GET_DESCRIPTOR(CONFIGURATION) request, and the kernel logs 'unable to read config index 0 descriptor/start: -71'.

The device then falls back to a secondary Android HID mode (with a different VID:PID), losing XInput functionality including rumble support. The failure reproduces across multiple machines, host controller types, and kernel versions including current mainline and LTS. The device enumerates correctly and remains in XInput mode under Windows. Notably, the device enumerates correctly in Android mode when the same 9-byte request is issued for that mode's configuration descriptor, confirming the firmware bug is specific to the XInput mode.

usbmon traces from Linux and Wireshark/USBPcap traces from Windows are identical up to the point of failure, with no visible protocol-level difference explaining the divergence. The root cause was identified when Michal Pecio discovered via a QEMU bus-level capture that Windows does not use wLength=9 for the initial config descriptor request; it uses wLength=255. Alan Stern subsequently confirmed this with a bus analyzer on a different USB 2.0 device, and Michal verified the behavior goes back to Windows 95 OSR2.1.

So, add a new quirk flag USB_QUIRK_CONFIG_SIZE which causes usb_get_configuration() to issue a 255 byte sized configuration request instead of USB_DT_CONFIG_SIZE (9) for the initial GET_DESCRIPTOR(CONFIGURATION) request, mimicking long-standing Windows behavior.

Quoted 7 lines

Normally people don't leave a blank line before their Signed-off-by:
(unless it's the first tag to appear). But I don't think it makes any difference, especially if the checkpatch.pl script doesn't object.

Quoted 21 lines

---
Changes in v2:
- Add Documentation
- Naming changes
- Refactored to have a better flow with existing code.

 .../admin-guide/kernel-parameters.txt         |  9 +++
 drivers/usb/core/config.c                     | 61 ++++++++++++++-----
 drivers/usb/core/hub.c                        |  6 +-
 drivers/usb/core/quirks.c                     |  4 ++
 include/linux/usb/quirks.h                    |  3 +
 5 files changed, 67 insertions(+), 16 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index 97007f4f69d4..af4bf0ef2c7b 100644
@@ -8158,6 +8158,15 @@ Kernel parameters
 				q = USB_QUIRK_FORCE_ONE_CONFIG (Device
 					claims zero configurations,
 					forcing to 1);

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 8170

Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +9 -0
+                r = USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE (Device
+                    fails during initialization when asked for
+                    9-bytes configuration desciptor request. Ask
+                    for 255-bytes request instead to mirror
+                    Windows' behavior. This quirk is originally
+                    meant to fix some quirky gamepads that refuse
+                    to connect in their XInput mode. But it can also
+                    potentially fix issues with other USB devices
+                    that work on Windows but not on Linux)
 			Example: quirks=0781:5580:bk,0a5c:5834:gij

As Randy said, use tabs instead of spaces. And this new entry should be aligned with all the preceding entries, and it should end with a ';' like they do.

I would leave out the two sentences of explanation, but that's more of a personal choice. We'll see if Greg KH objects.

Quoted 17 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0
 	usbhid.mousepoll=
drivers/usb/core/config.c +3 -0
index 45e20c6d76c0..4fc3145404d6 100644
@@ -19,6 +19,9 @@
 
 #define USB_MAXCONFIG			8	/* Arbitrary limit */
 
+/* config req size if USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE is set */
+#define USB_CONFIG_WINDOWS_REQ_SIZE	255
+
 static int find_next_descriptor(unsigned char *buffer, int size,
     int dt1, int dt2, int *num_skipped)
 {

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 924

Quoted 10 lines
drivers/usb/core/config.c +7 -0
@@ -912,6 +915,13 @@ int usb_get_configuration(struct usb_device *dev)
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 	int result;
+	/*
+	 * Devices with quirky firmware will stall or reset when asked only for
+	 * the configuration header. This variable decides which size to use in
+	 * that case, if the quirk for that device was set.
+	 */
+	size_t usb_config_req_size = (dev->quirks & USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE)
+		? USB_CONFIG_WINDOWS_REQ_SIZE : USB_DT_CONFIG_SIZE;

It's a little unusual to have a multiline comment in the middle of a bunch of variable definitions. The alternative is to do the assignment later on and move the comment there.

Quoted 9 lines
drivers/usb/core/config.c +1 -1
 	if (ncfg > USB_MAXCONFIG) {
 		dev_notice(ddev, "too many configurations: %d, "
@@ -938,18 +948,27 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		return -ENOMEM;
 
-	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	desc = kmalloc(usb_config_req_size, GFP_KERNEL);

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 959

Quoted 10 lines
drivers/usb/core/config.c +4 -2
+
 	if (!desc)
 		return -ENOMEM;
 
 	for (cfgno = 0; cfgno < ncfg; cfgno++) {
-		/* We grab just the first descriptor so we know how long
-		 * the whole configuration is */
+
+		if (dev->quirks & USB_QUIRK_DELAY_INIT)
+			msleep(200);

Moving this delay up here changes the behavior when the quirk flag isn't set. While it agrees with the intention of the USB_QUIRK_DELAY_INIT flag, such a change should be mentioned in the patch description.

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 966

Quoted 7 lines
drivers/usb/core/config.c +7 -0
+
+		/*
+		 * Grab just the first descriptor so we know how long the whole
+		 * configuration is. In case of quirky firmware, try to grab the
+		 * whole thing in one go by asking for a 255-bytes sized buffer
+		 * mirroring Windows behavior.
+		 */

This needs to be rewritten, as it is self-contradictory. When the quirk flag is set we issue a 255-byte request to mimic the Windows behavior, and only when the flag isn't set do we grab just the first descriptor.

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 968

Quoted 3 lines
drivers/usb/core/config.c +1 -1
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    desc, USB_DT_CONFIG_SIZE);
+						desc, usb_config_req_size);

Don't make extraneous changes to the existing indentation (or whitespace in general), here and below.

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 971

Quoted 4 lines
drivers/usb/core/config.c +1 -1
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
-			    "descriptor/%s: %d\n", cfgno, "start", result);
+				"descriptor/%s: %d\n", cfgno, "start", result);

At the time this code was originally written, the policy for kernel code was to break lines before 80 columns. Since then the policy has changed to avoid splitting long literal strings into pieces, even when that means exceeding 80 columns. So your new string here should all go on one line.

Quoted 8 lines
drivers/usb/core/config.c +0 -1
 			if (result != -EPIPE)
 				goto err;
 			dev_notice(ddev, "chopping to %d config(s)\n", cfgno);
@@ -957,13 +976,25 @@ int usb_get_configuration(struct usb_device *dev)
 			break;
 		} else if (result < 4) {
 			dev_err(ddev, "config index %d descriptor too short "
-			    "(expected %i, got %i)\n", cfgno,

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 986

Quoted 10 lines
drivers/usb/core/config.c +4 -2
-			    USB_DT_CONFIG_SIZE, result);
+				"(asked for %zu, got %i, expected at least %i)\n",
+				cfgno, usb_config_req_size, result, 4);
 			result = -EINVAL;
 			goto err;
 		}
+
 		length = max_t(int, le16_to_cpu(desc->wTotalLength),
-		    USB_DT_CONFIG_SIZE);
+				USB_DT_CONFIG_SIZE);

This is another example of a change that has nothing to do with the purpose of the patch.

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 990

Quoted 4 lines
drivers/usb/core/config.c +4 -0
+
+		/*
+		 * If the device returns the full length configuration
+		 * descriptor, skip the second read. Otherwise, send a second

Strictly speaking, the configuration descriptor is only 9 bytes long. What you mean here is the entire configuration descriptor set.

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 993

Quoted 3 lines
drivers/usb/core/config.c +3 -0
+		 * request asking for the full length.
+		 */
+		if (result >= le16_to_cpu(desc->wTotalLength)) {

Shouldn't this be: result >= length? No point in repeating the le16_to_cpu calculation.

Quoted 16 lines
drivers/usb/core/config.c +4 -4
+			bigbuffer = (unsigned char *) desc;
+			desc = NULL;
+			goto store_and_parse;
+		}
 
 		/* Now that we know the length, get the whole thing */
 		bigbuffer = kmalloc(length, GFP_KERNEL);
@@ -972,23 +1003,25 @@ int usb_get_configuration(struct usb_device *dev)
 			goto err;
 		}
 
-		if (dev->quirks & USB_QUIRK_DELAY_INIT)
-			msleep(200);
-
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    bigbuffer, length);

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 1015

Quoted 10 lines
drivers/usb/core/config.c +4 -1
+						bigbuffer, length);
+
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
-			    "descriptor/%s\n", cfgno, "all");
+				"descriptor/%s\n", cfgno, "all");
 			kfree(bigbuffer);
 			goto err;
 		}
+

More examples of unnecessary whitespace changes.

Quoted 18 lines
drivers/usb/core/config.c +4 -1
 		if (result < length) {
 			dev_notice(ddev, "config index %d descriptor too short "
-			    "(expected %i, got %i)\n", cfgno, length, result);
+				"(asked for %i, got %i)\n",
+				cfgno, length, result);
 			length = result;
 		}
 
+store_and_parse:
+		krealloc(bigbuffer, length, GFP_KERNEL);
 		dev->rawdescriptors[cfgno] = bigbuffer;
 
 		result = usb_parse_configuration(dev, cfgno,
drivers/usb/core/hub.c +0 -0
index 24960ba9caa9..9acd278666fc 100644

drivers/usb/core/hub.c static int usb_enumerate_device(struct usb_device *udev) · line 2531

Quoted 7 lines
drivers/usb/core/hub.c +2 -2
@@ -2527,8 +2527,10 @@ static int usb_enumerate_device(struct usb_device *udev)
 		err = usb_get_configuration(udev);
 		if (err < 0) {
 			if (err != -ENODEV)
-				dev_err(&udev->dev, "can't read configurations, error %d\n",
-						err);
+				dev_err(&udev->dev, "can't read configurations, "
+					"for device %04x:%04x, error %d\n",

Like above, this string should all be on one line.

Alan Stern

Quoted 35 lines
drivers/usb/core/hub.c +2 -0
+					le16_to_cpu(udev->descriptor.idVendor),
+					le16_to_cpu(udev->descriptor.idProduct), err);
 			return err;
 		}
 	}
drivers/usb/core/quirks.c +4 -0
index 87810eff974e..df670b0b66fe 100644
@@ -142,6 +142,10 @@ static int quirks_param_set(const char *value, const struct kernel_param *kp)
 				break;
 			case 'q':
 				flags |= USB_QUIRK_FORCE_ONE_CONFIG;
+				break;
+			case 'r':
+				flags |= USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE;
+				break;
 			/* Ignore unrecognized flag characters */
 			}
 		}
include/linux/usb/quirks.h +3 -0
index b3cc7beab4a3..a4043b33c2c2 100644
@@ -81,4 +81,7 @@
 /* Device claims zero configurations, forcing to 1 */
 #define USB_QUIRK_FORCE_ONE_CONFIG		BIT(18)
 
+/* Use a 255 bytes config descriptor request mirroring windows behavior */
+#define USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE	BIT(19)
+
 #endif /* __LINUX_USB_QUIRKS_H */
Signature · 1 line
2.54.0
#4 lore raw
Quoted 3 lines

Moving this delay up here changes the behavior when the quirk flag isn't set. While it agrees with the intention of the USB_QUIRK_DELAY_INIT flag, such a change should be mentioned in the patch description.

How should I mention it then? Nothing comes to mind besides the
obvious: "Also move the USB_QUIRK_DELAY_INIT sleep to before the initial descriptor read, so the delay applies consistently regardless of whether USB_QUIRK_CONFIG_SIZE is set.". Or should i revert it back to original position?

Quoted 11 lines

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 966

Quoted 7 lines
drivers/usb/core/config.c +7 -0
+
+             /*
+              * Grab just the first descriptor so we know how long the whole
+              * configuration is. In case of quirky firmware, try to grab the
+              * whole thing in one go by asking for a 255-bytes sized buffer
+              * mirroring Windows behavior.
+              */

This needs to be rewritten, as it is self-contradictory. When the quirk flag is set we issue a 255-byte request to mimic the Windows behavior, and only when the flag isn't set do we grab just the first descriptor.

I am sorry I didn't understand how it is self contradictory. The comment does say, "in case of quirky firmware..."? Am i missing something?

Quoted 6 lines

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 968

Quoted 3 lines

result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,

drivers/usb/core/config.c +1 -1
-                 desc, USB_DT_CONFIG_SIZE);
+                                             desc, usb_config_req_size);

Don't make extraneous changes to the existing indentation (or whitespace in general), here and below.

Well the linux coding style guidelines mention that those descendants should preferably be aligned with the function open parenthesis. Since i did "touch" that line/part of code I though might as well indent it a bit accordingly. Should i revert the indent then (in this and the other place)?

Quoted 13 lines
Quoted 13 lines
                      if (result != -EPIPE)
                              goto err;
                      dev_notice(ddev, "chopping to %d config(s)\n", cfgno);
drivers/usb/core/config.c +2 -2
@@ -957,13 +976,25 @@ int usb_get_configuration(struct usb_device *dev)
                      break;
              } else if (result < 4) {
                      dev_err(ddev, "config index %d descriptor too short "
-                         "(expected %i, got %i)\n", cfgno,
-                         USB_DT_CONFIG_SIZE, result);
+                             "(asked for %zu, got %i, expected at least %i)\n",
+                             cfgno, usb_config_req_size, result, 4);
                      result = -EINVAL;
                      goto err;
Quoted 8 lines

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 986

Quoted 5 lines
drivers/usb/core/config.c +2 -1
              }
+
              length = max_t(int, le16_to_cpu(desc->wTotalLength),
-                 USB_DT_CONFIG_SIZE);
+                             USB_DT_CONFIG_SIZE);

This is another example of a change that has nothing to do with the purpose of the patch.

Isn't that what you told me to change? So the logs are accurate? I made that change because you suggested it. :')

Quoted 7 lines

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 990

Quoted 4 lines
drivers/usb/core/config.c +4 -0
+
+             /*
+              * If the device returns the full length configuration
+              * descriptor, skip the second read. Otherwise, send a second

Strictly speaking, the configuration descriptor is only 9 bytes long. What you mean here is the entire configuration descriptor set.

Alright i'll reword it.

Quoted 6 lines

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 993

Quoted 3 lines
drivers/usb/core/config.c +3 -0
+              * request asking for the full length.
+              */
+             if (result >= le16_to_cpu(desc->wTotalLength)) {

Shouldn't this be: result >= length? No point in repeating the le16_to_cpu calculation.

Yess. initially the length assignment was happening afterwards in my patch. then i decided to move it before the "if" statement since the outcome of length was going to be similar in any case (within if and after if). but then i forgot to modify the if too. Will fix it.

Quoted 1 line

Like above, this string should all be on one line.

Will fix all the strings as well

Nikhil Solanke

#5 lore raw
Quoted 9 lines · Nikhil Solanke
Quoted 3 lines

Moving this delay up here changes the behavior when the quirk flag isn't set. While it agrees with the intention of the USB_QUIRK_DELAY_INIT flag, such a change should be mentioned in the patch description.

How should I mention it then? Nothing comes to mind besides the
obvious: "Also move the USB_QUIRK_DELAY_INIT sleep to before the initial descriptor read, so the delay applies consistently regardless of whether USB_QUIRK_CONFIG_SIZE is set.". Or should i revert it back to original position?

Actually, the best approach here would be to put this single change into a separate patch that comes before the current one. That removes issues of making more than one functional change in one patch and improves bisectability.

But to answer your question: In general, a patch's description should explain the reasons for the changes that the patch makes. Especially when a particular change doesn't appear, at first glance, to be related to the patch's primary purpose. (On the other hand, it doesn't need to explain in detail what the patch does; we can see that for ourselves just by reading the patch's contents.)

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/usb/core/config.c +6 -0
+             /*
+              * Grab just the first descriptor so we know how long the whole
+              * configuration is. In case of quirky firmware, try to grab the
+              * whole thing in one go by asking for a 255-bytes sized buffer
+              * mirroring Windows behavior.
+              */
Quoted 7 lines
Quoted 3 lines

This needs to be rewritten, as it is self-contradictory. When the quirk flag is set we issue a 255-byte request to mimic the Windows behavior, and only when the flag isn't set do we grab just the first descriptor.

I am sorry I didn't understand how it is self contradictory. The comment does say, "in case of quirky firmware..."? Am i missing something?

Literally, what the comment says is: Grab just the first descriptor, and if the quirk flag is set, get all the descriptors. That's a contradiction -- you can get just the first, or you can get all of them, but you can't do both at the same time!

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,

drivers/usb/core/config.c +1 -1
-                 desc, USB_DT_CONFIG_SIZE);
+                                             desc, usb_config_req_size);
Quoted 8 lines
Quoted 2 lines

Don't make extraneous changes to the existing indentation (or whitespace in general), here and below.

Well the linux coding style guidelines mention that those descendants should preferably be aligned with the function open parenthesis. Since i did "touch" that line/part of code I though might as well indent it a bit accordingly. Should i revert the indent then (in this and the other place)?

The style used in this file is to indent continuation lines by 4 spaces, because some of the continued statements are extremely long. If you want to align new continuation lines with an open paren, you can -- but you didn't even do that in the example above; you aligned it with the space following the first comma.

And while you did change some nearby code, you did not change the code in this line. So reformatting it is not justified.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
                      if (result != -EPIPE)
                              goto err;
                      dev_notice(ddev, "chopping to %d config(s)\n", cfgno);
drivers/usb/core/config.c +2 -2
@@ -957,13 +976,25 @@ int usb_get_configuration(struct usb_device *dev)
                      break;
              } else if (result < 4) {
                      dev_err(ddev, "config index %d descriptor too short "
-                         "(expected %i, got %i)\n", cfgno,
-                         USB_DT_CONFIG_SIZE, result);
+                             "(asked for %zu, got %i, expected at least %i)\n",
+                             cfgno, usb_config_req_size, result, 4);
                      result = -EINVAL;
                      goto err;
Quoted 8 lines

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 986

Quoted 5 lines
drivers/usb/core/config.c +2 -1
              }
+
              length = max_t(int, le16_to_cpu(desc->wTotalLength),
-                 USB_DT_CONFIG_SIZE);
+                             USB_DT_CONFIG_SIZE);

This is another example of a change that has nothing to do with the purpose of the patch.

Quoted 2 lines

Isn't that what you told me to change? So the logs are accurate? I made that change because you suggested it. :')

My comment referred to the two lines directly above it, and I did not suggest leaving the code exactly the same except for indenting it farther. Or inserting an extra blank line just before the assignment to length.

Alan Stern

#6 lore raw
Quoted 4 lines

Actually, the best approach here would be to put this single change into a separate patch that comes before the current one. That removes issues of making more than one functional change in one patch and improves bisectability.

Before? Shouldn't it be after my changes? That would make it easier to justify the changes. And just to be sure, you did mention it does align with what the intention of USB_QUIRK_DELAY_INIT, but it does change its behavior when the quirk is not set. Atleast from what I understood from the documentation and an LLM's summary, the device needs time to prepare the full configuration set. So, does delaying before the first header read really work? I can't test this since I don't have a device that requires the quirk to be set.

I personally think adding a condition to check if the quirk is set and then delaying before sending the first request would be appropriate. What are your opinions on this.

Quoted 5 lines

The style used in this file is to indent continuation lines by 4 spaces, because some of the continued statements are extremely long. If you want to align new continuation lines with an open paren, you can -- but you didn't even do that in the example above; you aligned it with the space following the first comma.

I will make my changes more consistent with the existing file, i.e. continuation with 4 spaces.

Also is it fine if the string lines exceed 100 columns?

Also, is there a need to check for krealloc()'s return value? Since we are only shrinking the buffer, there won't be any moves or completely new blocks (at least as per my understanding). Do I still need to check its return value for completeness' sake?

#7 lore raw
Quoted 14 lines · Nikhil Solanke
Quoted 4 lines

Actually, the best approach here would be to put this single change into a separate patch that comes before the current one. That removes issues of making more than one functional change in one patch and improves bisectability.

Before? Shouldn't it be after my changes? That would make it easier to justify the changes. And just to be sure, you did mention it does align with what the intention of USB_QUIRK_DELAY_INIT, but it does change its behavior when the quirk is not set. Atleast from what I understood from the documentation and an LLM's summary, the device needs time to prepare the full configuration set. So, does delaying before the first header read really work? I can't test this since I don't have a device that requires the quirk to be set.

Quoted 3 lines

I personally think adding a condition to check if the quirk is set and then delaying before sending the first request would be appropriate. What are your opinions on this.

Well, put it this way: If you change the existing behavior, that change belongs in a separate patch. If you want to redo this patch so that it doesn't change anything when the quirk flag isn't set, that's fine.

Quoted 1 line

Also is it fine if the string lines exceed 100 columns?

In lines containing long strings, it's okay for the string to extend well beyond 80 columns. But then you should break the line at some point closely following the end of the string. I'm sure you can find examples of this if you look through some of the other source files.

Quoted 4 lines

Also, is there a need to check for krealloc()'s return value? Since we are only shrinking the buffer, there won't be any moves or completely new blocks (at least as per my understanding). Do I still need to check its return value for completeness' sake?

It's a little tricky to track this down, but if you look in include/linux/slab.h you'll see that krealloc() is defined as krealloc_node(), which is defined as krealloc_node_align(), which is defined as krealloc_node_align_noprof(), which is declared with __must_check. So yes, you need to check the return value from krealloc().

Of course, you could simply try not checking the return value and seeing if that provokes a warning or error from the compiler.

Alan Stern

#8 lore raw

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

2. Regarding my idea of adding a condition — so that it doesn't change the behavior when the quirk isn't set — if the full configuration set exceeds 255 bytes, we would have to issue a 2nd request. In this case the existing behavior would be more justified.

So, I'm a bit confused about how to implement this properly. Adding yet another condition to fix the second case doesn't feel right to me. It would look unnecessarily complicated. I would appreciate a bit of help and advice.

Thanks,
Nikhil Solanke

#9 lore raw
Quoted 13 lines · Nikhil Solanke

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

2. Regarding my idea of adding a condition — so that it doesn't change the behavior when the quirk isn't set — if the full configuration set exceeds 255 bytes, we would have to issue a 2nd request. In this case the existing behavior would be more justified.

Quoted 4 lines

So, I'm a bit confused about how to implement this properly. Adding yet another condition to fix the second case doesn't feel right to me. It would look unnecessarily complicated. I would appreciate a bit of help and advice.

If the 255-byte quirk flag isn't set, do the delay before the second transfer just as it is now.

If the 255-byte quirk flag is set, do the delay before the first transfer. If a second transfer is needed, you can do a second delay before it or not -- I suspect it doesn't matter. If you want to be safe, add the second delay.

Alan Stern

#10 lore raw
Quoted 8 lines · Alan Stern
Quoted 7 lines · Nikhil Solanke

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

git blame is your friend:

The DELAY_INIT quirk only reduces the frequency of enumeration failures with the Logitech HD Pro C920 and C930e webcams, but does not quite eliminate them. We have found that adding a delay of 100ms between the first and second Get Configuration request makes the device enumerate perfectly reliable even after several weeks of extensive testing. The reasons for that are anyone's guess,

Quoted 11 lines
Quoted 10 lines

2. Regarding my idea of adding a condition — so that it doesn't change the behavior when the quirk isn't set — if the full configuration set exceeds 255 bytes, we would have to issue a 2nd request. In this case the existing behavior would be more justified.

So, I'm a bit confused about how to implement this properly. Adding yet another condition to fix the second case doesn't feel right to me. It would look unnecessarily complicated. I would appreciate a bit of help and advice.

Quoted 7 lines

If the 255-byte quirk flag isn't set, do the delay before the second transfer just as it is now.

If the 255-byte quirk flag is set, do the delay before the first transfer. If a second transfer is needed, you can do a second delay before it or not -- I suspect it doesn't matter. If you want to be safe, add the second delay.

How about "keep unrelated changes out of a stable patch", i.e. always do the delay (if any) after the first request, regardless of size?

Regards,
Michal

#11 lore raw
Quoted 33 lines · Michal Pecio
Quoted 8 lines · Alan Stern
Quoted 7 lines · Nikhil Solanke

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

git blame is your friend:

The DELAY_INIT quirk only reduces the frequency of enumeration failures with the Logitech HD Pro C920 and C930e webcams, but does not quite eliminate them. We have found that adding a delay of 100ms between the first and second Get Configuration request makes the device enumerate perfectly reliable even after several weeks of extensive testing. The reasons for that are anyone's guess,

Quoted 14 lines
Quoted 10 lines

2. Regarding my idea of adding a condition — so that it doesn't change the behavior when the quirk isn't set — if the full configuration set exceeds 255 bytes, we would have to issue a 2nd request. In this case the existing behavior would be more justified.

So, I'm a bit confused about how to implement this properly. Adding yet another condition to fix the second case doesn't feel right to me. It would look unnecessarily complicated. I would appreciate a bit of help and advice.

If the 255-byte quirk flag isn't set, do the delay before the second transfer just as it is now.

Quoted 7 lines
Quoted 4 lines

If the 255-byte quirk flag is set, do the delay before the first transfer. If a second transfer is needed, you can do a second delay before it or not -- I suspect it doesn't matter. If you want to be safe, add the second delay.

How about "keep unrelated changes out of a stable patch", i.e. always do the delay (if any) after the first request, regardless of size?

This is not an unrelated change. Rather, it's deciding on how to behave in an entirely new control pathway -- the one where the 255-byte quirk flag is set. The old pathway is completely unaffected.

I suspect no devices will have both this quirk flag and the DELAY_INIT flag set, which means the location of any delays in the new pathway won't matter at all since they will never be used. But even if some such devices do turn up, adding an extra unecessary 200 ms to an initialization that is already at least 2200 ms long won't make much difference.

Alan Stern

#12 lore raw
Quoted 10 lines · Alan Stern
Quoted 2 lines

How about "keep unrelated changes out of a stable patch", i.e. always do the delay (if any) after the first request, regardless of size?

This is not an unrelated change. Rather, it's deciding on how to behave in an entirely new control pathway -- the one where the 255-byte quirk flag is set. The old pathway is completely unaffected.

I suspect no devices will have both this quirk flag and the DELAY_INIT flag set, which means the location of any delays in the new pathway won't matter at all since they will never be used.

If no devices will have both quirks then new delay added before the first configuration request will never execute.

If such devices will exist, then it probably won't matter whether the delay comes after or before the first request. Purpose isn't known, but it appears to be rate limiting configuration descriptor requests or delaying other requests after this function returns.

Either way, no known need exists to add another delay before the first request or alter the existing delay (or its conditions) in any way.

In general, I always object to code which serves no purpose because such code is easy to add but very hard to remove when it gets in the way. There are no known users, no test cases, only paranoia.

So I would keep the delay code completely unchaged.

And skip other random changes like error string nitpicking. Reliable and up to date information about how many bytes are requested, "expected" (what does it even mean, to somebody reading dmesg?), received or verified to exist can be gained from source and usbmon.

A stable patch is supposedly supposed to be 100 lines with context ;)

Regards,
Michal

#13 lore raw

I really think it could (and should) be a simple patch.

This is what I wrote a few weeks ago. It's an unconditional change for all devices, but it would be easy to turn it into a quirk.

drivers/usb/core/config.c +9 -6
@@ -938,15 +938,14 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		return -ENOMEM;
 
-	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	desc = kmalloc(255, GFP_KERNEL);
 	if (!desc)
 		return -ENOMEM;
 
 	for (cfgno = 0; cfgno < ncfg; cfgno++) {
-		/* We grab just the first descriptor so we know how long
-		 * the whole configuration is */
+		/* Try 255 bytes first because that's what Windows does */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    desc, USB_DT_CONFIG_SIZE);
+		    desc, 255);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -975,8 +974,12 @@ int usb_get_configuration(struct usb_device *dev)
 		if (dev->quirks & USB_QUIRK_DELAY_INIT)
 			msleep(200);
 
-		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    bigbuffer, length);
+		/* Don't bother if we already have it all */
+		if (length <= result)
+			memcpy(bigbuffer, desc, length);
+		else
+			result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
+					bigbuffer, length);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s\n", cfgno, "all");
#14 lore raw
In reply to Michal Pecio
Quoted 8 lines · Michal Pecio
Quoted 7 lines · Alan Stern
Quoted 2 lines

How about "keep unrelated changes out of a stable patch", i.e. always do the delay (if any) after the first request, regardless of size?

This is not an unrelated change. Rather, it's deciding on how to behave in an entirely new control pathway -- the one where the 255-byte quirk flag is set. The old pathway is completely unaffected.

Quoted 6 lines
Quoted 3 lines

I suspect no devices will have both this quirk flag and the DELAY_INIT flag set, which means the location of any delays in the new pathway won't matter at all since they will never be used.

If no devices will have both quirks then new delay added before the first configuration request will never execute.

Correct.

Quoted 4 lines

If such devices will exist, then it probably won't matter whether the delay comes after or before the first request. Purpose isn't known, but it appears to be rate limiting configuration descriptor requests or delaying other requests after this function returns.

In fact, the commit that talks about the Logitech webcams does describe their buggy behavior to some extent. It says that they seem to reply with stale video data instead of the real config information, and from there it's a short guess that adding a delay gives time for the video pipeline to drain or time out.

In addition, the fact that the delay is needed after the first request but before the second suggests that the data corruption only affects transfers longer than 9 bytes -- which the new first request would be. Therefore it would be appropriate to have the delay before the new first request. Whether another delay would be needed before the second request (if there is one) is unknown.

Quoted 2 lines

Either way, no known need exists to add another delay before the first request or alter the existing delay (or its conditions) in any way.

See the reasoning above.

Quoted 5 lines

In general, I always object to code which serves no purpose because such code is easy to add but very hard to remove when it gets in the way. There are no known users, no test cases, only paranoia.

So I would keep the delay code completely unchaged.

At this point it's entirely theoretical anyway, since the devices that would get the new 255-byte quirk flag don't also have the DELAY_INIT quirk.

Quoted 4 lines

And skip other random changes like error string nitpicking. Reliable and up to date information about how many bytes are requested, "expected" (what does it even mean, to somebody reading dmesg?), received or verified to exist can be gained from source and usbmon.

Good point. But I dislike messages that actively produce wrong information. Nikhil could get rid of the parts of the log messages you don't like, but he shouldn't leave them as they are. He could even do that in a second patch, separate from this one.

Quoted 1 line

A stable patch is supposedly supposed to be 100 lines with context ;)

Nonsense. An excellent stable patch is 7 lines, including context. :-)

Quoted 32 lines

I really think it could (and should) be a simple patch.

This is what I wrote a few weeks ago. It's an unconditional change for all devices, but it would be easy to turn it into a quirk.

drivers/usb/core/config.c +3 -5
@@ -938,15 +938,14 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		return -ENOMEM;
 
-	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	desc = kmalloc(255, GFP_KERNEL);
 	if (!desc)
 		return -ENOMEM;
 
 	for (cfgno = 0; cfgno < ncfg; cfgno++) {
-		/* We grab just the first descriptor so we know how long
-		 * the whole configuration is */
+		/* Try 255 bytes first because that's what Windows does */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    desc, USB_DT_CONFIG_SIZE);
+		    desc, 255);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -975,8 +974,12 @@ int usb_get_configuration(struct usb_device *dev)
 		if (dev->quirks & USB_QUIRK_DELAY_INIT)
 			msleep(200);
 
-		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 985

Quoted 10 lines
drivers/usb/core/config.c +6 -1
-		    bigbuffer, length);
+		/* Don't bother if we already have it all */
+		if (length <= result)
+			memcpy(bigbuffer, desc, length);
+		else
+			result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
+					bigbuffer, length);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s\n", cfgno, "all");

Making this depend on a quirk flag instead of being unconditional would yield a patch very similar to what Nikhil has already posted.

Alan Stern

#15 lore raw
Quoted 12 lines · Alan Stern
Quoted 4 lines · Michal Pecio

If such devices will exist, then it probably won't matter whether the delay comes after or before the first request. Purpose isn't known, but it appears to be rate limiting configuration descriptor requests or delaying other requests after this function returns.

In fact, the commit that talks about the Logitech webcams does describe their buggy behavior to some extent. It says that they seem to reply with stale video data instead of the real config information, and from there it's a short guess that adding a delay gives time for the video pipeline to drain or time out.

Quoted 6 lines

In addition, the fact that the delay is needed after the first request but before the second suggests that the data corruption only affects transfers longer than 9 bytes -- which the new first request would be. Therefore it would be appropriate to have the delay before the new first request. Whether another delay would be needed before the second request (if there is one) is unknown.

Fair enough, that's possible, but even in these specific webcams it's still unclear what delays would be necessary with both quirks. We wait for the HW to complete something, but we don't know what starts it. If it's device reset, then b2a542bbb308 has already doubled all delays so d86db25e53fa6 isn't even necessary anymore. OTOH, if it's the first config request, then a delay between the requests is mandatory, and a delay before the first request is useless. If it's something in between then your approach could be the only viable choice.

I would worry about it when a device is known that uses both quirks.

Quoted 4 lines

Good point. But I dislike messages that actively produce wrong information. Nikhil could get rid of the parts of the log messages you don't like, but he shouldn't leave them as they are. He could even do that in a second patch, separate from this one.

I can agree that the first "descriptor too short" message becomes misleading, because we no longer expect 9 bytes exactly, but anything between 9 and 255. So this could be changed to "requested".

But I see no need to change the second message. Regardless of initial request length, the next request (if any) asks for the exact size and we do expect it to produce 'length' bytes.

Using different words in these messages has a beneficial side effect of making it possible to tell them apart when wTotalLength == 9.

Regards,
Michal

#16 lore raw
In reply to Michal Pecio
Quoted 7 lines · Michal Pecio
Quoted 5 lines · Alan Stern
Quoted 4 lines · Nikhil Solanke

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

git blame is your friend:

I'll keep in mind to use git blame in future. I haven't worked extensively in a large, collaborative codebase, so using git blame didn't occur to me in this case. Sorry about that!

Thanks,
Nikhil Solanke

#17 lore raw
In reply to Alan Stern
Quoted 23 lines · Alan Stern
Quoted 13 lines · Nikhil Solanke

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

2. Regarding my idea of adding a condition — so that it doesn't change the behavior when the quirk isn't set — if the full configuration set exceeds 255 bytes, we would have to issue a 2nd request. In this case the existing behavior would be more justified.

Quoted 4 lines

So, I'm a bit confused about how to implement this properly. Adding yet another condition to fix the second case doesn't feel right to me. It would look unnecessarily complicated. I would appreciate a bit of help and advice.

If the 255-byte quirk flag isn't set, do the delay before the second transfer just as it is now.

Quoted 6 lines

If the 255-byte quirk flag is set, do the delay before the first transfer. If a second transfer is needed, you can do a second delay before it or not -- I suspect it doesn't matter. If you want to be safe, add the second delay.

Alan Stern

Ok thanks! Just to make sure, because the change I will introduce won't affect any existing behavior, these changes (relating to DELAY_INIT quirk) won't belong in a new patch, right?

Thanks,
Nikhil Solanke

#18 lore raw
Quoted 29 lines · Nikhil Solanke
Quoted 28 lines · Alan Stern
Quoted 13 lines · Nikhil Solanke

I need some help with the USB_QUIRK_DELAY_INIT part. I can't figure out how to make it properly work with my patch because of the following reasons:

1. I don't want to move it to the top because, from my pov, there must have been some reason for placing that quirk where it is now. so i don't want to mess with it.

2. Regarding my idea of adding a condition — so that it doesn't change the behavior when the quirk isn't set — if the full configuration set exceeds 255 bytes, we would have to issue a 2nd request. In this case the existing behavior would be more justified.

Quoted 4 lines

So, I'm a bit confused about how to implement this properly. Adding yet another condition to fix the second case doesn't feel right to me. It would look unnecessarily complicated. I would appreciate a bit of help and advice.

If the 255-byte quirk flag isn't set, do the delay before the second transfer just as it is now.

If the 255-byte quirk flag is set, do the delay before the first transfer. If a second transfer is needed, you can do a second delay before it or not -- I suspect it doesn't matter. If you want to be safe, add the second delay.

Quoted 5 lines
Quoted 1 line

Alan Stern

Ok thanks! Just to make sure, because the change I will introduce won't affect any existing behavior, these changes (relating to DELAY_INIT quirk) won't belong in a new patch, right?

Maybe the best thing to do at this point is to assume that both quirk flags will never be set for the same device. Under that assumption there's no need to change the delay code in any way. Just add a comment mentioning this assumption to avoid confusing people in the future.

Alan Stern

maintainer
#19 lore raw
In reply to Nikhil Solanke

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 924

Quoted 11 lines · Nikhil Solanke
drivers/usb/core/config.c +7 -0
@@ -912,6 +915,13 @@ int usb_get_configuration(struct usb_device *dev)
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 	int result;
+	/*
+	 * Devices with quirky firmware will stall or reset when asked only for
+	 * the configuration header. This variable decides which size to use in
+	 * that case, if the quirk for that device was set.
+	 */
+	size_t usb_config_req_size = (dev->quirks & USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE)
+		? USB_CONFIG_WINDOWS_REQ_SIZE : USB_DT_CONFIG_SIZE;

Please just use if () lines for code logic like this. Don't abuse ?:
stuff as it's not needed. Remember, we write code for people first, compilers second, and in this case the compiler doesn't care either way at all, but an if () line makes people much happier.

thanks,

greg k-h

#20 lore raw
In reply to Nikhil Solanke
Quoted 57 lines · Nikhil Solanke

Certain third-party USB game controllers exposing (or spoofing) an Xbox 360-compatible interface (VID:PID 045e:028e) fail to enumerate under Linux. The device disconnects from the bus without responding to the initial GET_DESCRIPTOR(CONFIGURATION) request, and the kernel logs 'unable to read config index 0 descriptor/start: -71'.

The device then falls back to a secondary Android HID mode (with a different VID:PID), losing XInput functionality including rumble support. The failure reproduces across multiple machines, host controller types, and kernel versions including current mainline and LTS. The device enumerates correctly and remains in XInput mode under Windows. Notably, the device enumerates correctly in Android mode when the same 9-byte request is issued for that mode's configuration descriptor, confirming the firmware bug is specific to the XInput mode.

usbmon traces from Linux and Wireshark/USBPcap traces from Windows are identical up to the point of failure, with no visible protocol-level difference explaining the divergence. The root cause was identified when Michal Pecio discovered via a QEMU bus-level capture that Windows does not use wLength=9 for the initial config descriptor request; it uses wLength=255. Alan Stern subsequently confirmed this with a bus analyzer on a different USB 2.0 device, and Michal verified the behavior goes back to Windows 95 OSR2.1.

So, add a new quirk flag USB_QUIRK_CONFIG_SIZE which causes usb_get_configuration() to issue a 255 byte sized configuration request instead of USB_DT_CONFIG_SIZE (9) for the initial GET_DESCRIPTOR(CONFIGURATION) request, mimicking long-standing Windows behavior.

---
Changes in v2:
- Add Documentation
- Naming changes
- Refactored to have a better flow with existing code.

 .../admin-guide/kernel-parameters.txt         |  9 +++
 drivers/usb/core/config.c                     | 61 ++++++++++++++-----
 drivers/usb/core/hub.c                        |  6 +-
 drivers/usb/core/quirks.c                     |  4 ++
 include/linux/usb/quirks.h                    |  3 +
 5 files changed, 67 insertions(+), 16 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +0 -0
index 97007f4f69d4..af4bf0ef2c7b 100644
@@ -8158,6 +8158,15 @@ Kernel parameters
 				q = USB_QUIRK_FORCE_ONE_CONFIG (Device
 					claims zero configurations,

Documentation/admin-guide/kernel-parameters.txt Kernel parameters · line 8169

Quoted 10 lines
Documentation/admin-guide/kernel-parameters.txt +9 -0
 					forcing to 1);
+                r = USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE (Device
+                    fails during initialization when asked for
+                    9-bytes configuration desciptor request. Ask
+                    for 255-bytes request instead to mirror
+                    Windows' behavior. This quirk is originally
+                    meant to fix some quirky gamepads that refuse
+                    to connect in their XInput mode. But it can also
+                    potentially fix issues with other USB devices
+                    that work on Windows but not on Linux)

I too think it's too long. This file explains what the parametrs do, not their history.

And here it's USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE, but in the commit message it was USB_QUIRK_CONFIG_SIZE.

Honestly, I would suggest a third option: something with "255" instead of "Windows", because not everybody knows how windows queries descriptors, but everybody knows what 255 is.

Quoted 18 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0
 			Example: quirks=0781:5580:bk,0a5c:5834:gij
 
 	usbhid.mousepoll=
drivers/usb/core/config.c +3 -0
index 45e20c6d76c0..4fc3145404d6 100644
@@ -19,6 +19,9 @@
 
 #define USB_MAXCONFIG			8	/* Arbitrary limit */
 
+/* config req size if USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE is set */
+#define USB_CONFIG_WINDOWS_REQ_SIZE	255
+
 static int find_next_descriptor(unsigned char *buffer, int size,
     int dt1, int dt2, int *num_skipped)
 {

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 924

Quoted 10 lines
drivers/usb/core/config.c +7 -0
@@ -912,6 +915,13 @@ int usb_get_configuration(struct usb_device *dev)
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 	int result;
+	/*
+	 * Devices with quirky firmware will stall or reset when asked only for
+	 * the configuration header. This variable decides which size to use in
+	 * that case, if the quirk for that device was set.
+	 */
+	size_t usb_config_req_size = (dev->quirks & USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE)
+		? USB_CONFIG_WINDOWS_REQ_SIZE : USB_DT_CONFIG_SIZE;

That's a lot of capital letters, USBCONFIG_WINDOWS_REQ_SIZE never appears outside this function and personally I would just spell it out as 255 here with appropriate comment.

The whole function is all about "usb" and "config" so maybe these words don't need to appear here. OTOH, "first" would add useful information.

Quoted 48 lines
drivers/usb/core/config.c +17 -7
 	if (ncfg > USB_MAXCONFIG) {
 		dev_notice(ddev, "too many configurations: %d, "
@@ -938,18 +948,27 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		return -ENOMEM;
 
-	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	desc = kmalloc(usb_config_req_size, GFP_KERNEL);
+
 	if (!desc)
 		return -ENOMEM;
 
 	for (cfgno = 0; cfgno < ncfg; cfgno++) {
-		/* We grab just the first descriptor so we know how long
-		 * the whole configuration is */
+
+		if (dev->quirks & USB_QUIRK_DELAY_INIT)
+			msleep(200);
+
+		/*
+		 * Grab just the first descriptor so we know how long the whole
+		 * configuration is. In case of quirky firmware, try to grab the
+		 * whole thing in one go by asking for a 255-bytes sized buffer
+		 * mirroring Windows behavior.
+		 */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    desc, USB_DT_CONFIG_SIZE);
+						desc, usb_config_req_size);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
-			    "descriptor/%s: %d\n", cfgno, "start", result);
+				"descriptor/%s: %d\n", cfgno, "start", result);
 			if (result != -EPIPE)
 				goto err;
 			dev_notice(ddev, "chopping to %d config(s)\n", cfgno);
@@ -957,13 +976,25 @@ int usb_get_configuration(struct usb_device *dev)
 			break;
 		} else if (result < 4) {
 			dev_err(ddev, "config index %d descriptor too short "
-			    "(expected %i, got %i)\n", cfgno,
-			    USB_DT_CONFIG_SIZE, result);
+				"(asked for %zu, got %i, expected at least %i)\n",
+				cfgno, usb_config_req_size, result, 4);
 			result = -EINVAL;
 			goto err;
 		}
+

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 993

Quoted 10 lines
drivers/usb/core/config.c +8 -1
 		length = max_t(int, le16_to_cpu(desc->wTotalLength),
-		    USB_DT_CONFIG_SIZE);
+				USB_DT_CONFIG_SIZE);
+
+		/*
+		 * If the device returns the full length configuration
+		 * descriptor, skip the second read. Otherwise, send a second
+		 * request asking for the full length.
+		 */
+		if (result >= le16_to_cpu(desc->wTotalLength)) {

Technically, this is a behavior change for devices without the quirk, if their wTotalLength is 9. Not sure if this is worth worrynig about, it would mean that the configuration has no interfaces or endpoints.

drivers/usb/core/config.c int usb_get_configuration(struct usb_device *dev) · line 995

Quoted 2 lines
drivers/usb/core/config.c +2 -0
+			bigbuffer = (unsigned char *) desc;
+			desc = NULL;

What happens in the next iteration of the loop?

Quoted 42 lines
drivers/usb/core/config.c +10 -6
+			goto store_and_parse;
+		}
 
 		/* Now that we know the length, get the whole thing */
 		bigbuffer = kmalloc(length, GFP_KERNEL);
@@ -972,23 +1003,25 @@ int usb_get_configuration(struct usb_device *dev)
 			goto err;
 		}
 
-		if (dev->quirks & USB_QUIRK_DELAY_INIT)
-			msleep(200);
-
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    bigbuffer, length);
+						bigbuffer, length);
+
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
-			    "descriptor/%s\n", cfgno, "all");
+				"descriptor/%s\n", cfgno, "all");
 			kfree(bigbuffer);
 			goto err;
 		}
+
 		if (result < length) {
 			dev_notice(ddev, "config index %d descriptor too short "
-			    "(expected %i, got %i)\n", cfgno, length, result);
+				"(asked for %i, got %i)\n",
+				cfgno, length, result);
 			length = result;
 		}
 
+store_and_parse:
+		krealloc(bigbuffer, length, GFP_KERNEL);
 		dev->rawdescriptors[cfgno] = bigbuffer;
 
 		result = usb_parse_configuration(dev, cfgno,
drivers/usb/core/hub.c +0 -0
index 24960ba9caa9..9acd278666fc 100644

drivers/usb/core/hub.c static int usb_enumerate_device(struct usb_device *udev) · line 2533

Quoted 9 lines
drivers/usb/core/hub.c +4 -2
@@ -2527,8 +2527,10 @@ static int usb_enumerate_device(struct usb_device *udev)
 		err = usb_get_configuration(udev);
 		if (err < 0) {
 			if (err != -ENODEV)
-				dev_err(&udev->dev, "can't read configurations, error %d\n",
-						err);
+				dev_err(&udev->dev, "can't read configurations, "
+					"for device %04x:%04x, error %d\n",
+					le16_to_cpu(udev->descriptor.idVendor),
+					le16_to_cpu(udev->descriptor.idProduct), err);

I wonder if it wouldn't make sense to split announce_device() so that the first line is printed as soon as usb_new_device() starts, before enumeration is attempted and possibly fails.

That would be a separate patch, of course.

Quoted 33 lines
drivers/usb/core/hub.c +0 -0
 			return err;
 		}
 	}
drivers/usb/core/quirks.c +4 -0
index 87810eff974e..df670b0b66fe 100644
@@ -142,6 +142,10 @@ static int quirks_param_set(const char *value, const struct kernel_param *kp)
 				break;
 			case 'q':
 				flags |= USB_QUIRK_FORCE_ONE_CONFIG;
+				break;
+			case 'r':
+				flags |= USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE;
+				break;
 			/* Ignore unrecognized flag characters */
 			}
 		}
include/linux/usb/quirks.h +3 -0
index b3cc7beab4a3..a4043b33c2c2 100644
@@ -81,4 +81,7 @@
 /* Device claims zero configurations, forcing to 1 */
 #define USB_QUIRK_FORCE_ONE_CONFIG		BIT(18)
 
+/* Use a 255 bytes config descriptor request mirroring windows behavior */
+#define USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE	BIT(19)
+
 #endif /* __LINUX_USB_QUIRKS_H */
Signature · 1 line
2.54.0
#21 lore raw
Quoted 2 lines

And here it's USB_QUIRK_WINDOWS_CONFIG_REQ_SIZE, but in the commit message it was USB_QUIRK_CONFIG_SIZE.

Yes, I have fixed it already.

Quoted 3 lines

Honestly, I would suggest a third option: something with "255" instead of "Windows", because not everybody knows how windows queries descriptors, but everybody knows what 255 is.

Well naming ain't my strong suit (if I name something, it will definitely get called out). Do you have a specific name in mind? if your concern lies more in people not knowing what Windows does, should i just make it apparent in a comment where it is set?

Quoted 3 lines

That's a lot of capital letters, USBCONFIG_WINDOWS_REQ_SIZE never appears outside this function and personally I would just spell it out as 255 here with appropriate comment.

I wanted to avoid the plausible "avoid magic numbers" criticism. If everybody else is fine with bare 255, I will just do that then.

Quoted 4 lines
Quoted 2 lines

+ bigbuffer = (unsigned char *) desc;
+ desc = NULL;

What happens in the next iteration of the loop?

Disaster to say the least. A memcpy in bigbuffer and memset(0,..) on desc would be the correct approach here, wouldn't it?

On that note, I discovered that usb_get_descriptor just blindly trusts the caller with the allocation of buf, it never checks if buf is null or not. There is only a check for the size. and then there is a memset(buf, 0, size). This results in a segfault if buf is NULL and size > 0. Perhaps it's time for a new patch to fix this?

Quoted 3 lines

I wonder if it wouldn't make sense to split announce_device() so that the first line is printed as soon as usb_new_device() starts, before enumeration is attempted and possibly fails.

Do you want something like announce_device_ids() where it would only print out the first "New Usb device found..." line? and then announce_device_strings()?

Perhaps even another macro announce_device that does both? But its only used in one place, so I doubt it would be necessary.

Quoted 1 line

That would be a separate patch, of course.

If i have to make this change, should this be a completely separate patch or is it fine to include it in this patchseries?

Thanks,
Nikhil Solanke

#22 lore raw
Quoted 5 lines · Nikhil Solanke

On that note, I discovered that usb_get_descriptor just blindly trusts the caller with the allocation of buf, it never checks if buf is null or not. There is only a check for the size. and then there is a memset(buf, 0, size). This results in a segfault if buf is NULL and size > 0. Perhaps it's time for a new patch to fix this?

There's nothing wrong with trusting the caller to do the right thing. Besides, if a segfault does occur then it will be pretty obvious that the caller needs to be fixed.

What would you do if buf is NULL? Return an error code? That won't help anyone locate the bug. Put an error message in the log? Segfaults are much more visible.

Not to mention that nobody has complained about this code failing, so it seems unlikely that buf ever is NULL.

Alan Stern

#23 lore raw
Quoted 7 lines

There's nothing wrong with trusting the caller to do the right thing. Besides, if a segfault does occur then it will be pretty obvious that the caller needs to be fixed.

What would you do if buf is NULL? Return an error code? That won't help anyone locate the bug. Put an error message in the log? Segfaults are much more visible.

Understood. I guess my coding style is a little "too paranoid" and "check everything and report errors". I understood now why this may not always be the best approach in low level programming like kernel development.

Anyways, I have done all the requested changes. Here's a short summary:
- put strings in a single line
- copy bytes from desc to bigbuffer instead of pointer aliasing. (so no krealloc too)
- change tabs to spaces in documentation
- reworded some comments
- drop USB_CONFIG_WINDOWS_REQ_SIZE macro
- revert USB_DELAY_INIT to original behavior. no delay before 1st request.

Let me know if I missed any changes mentioned in previous discussions (or misunderstood and made unnecessary changes :') ).

Quoted 3 lines

I wonder if it wouldn't make sense to split announce_device() so that the first line is printed as soon as usb_new_device() starts, before enumeration is attempted and possibly fails.

The current patch still logs device ids upon failure in usb_enumerate_device(). Do you want me to implement that suggestion?

Thanks,
Nikhil Solanke

#24 lore raw
Quoted 30 lines · Nikhil Solanke
Quoted 8 lines

There's nothing wrong with trusting the caller to do the right thing. Besides, if a segfault does occur then it will be pretty obvious that the caller needs to be fixed.

What would you do if buf is NULL? Return an error code? That won't help anyone locate the bug. Put an error message in the log? Segfaults are much more visible.

Understood. I guess my coding style is a little "too paranoid" and "check everything and report errors". I understood now why this may not always be the best approach in low level programming like kernel development.

Anyways, I have done all the requested changes. Here's a short summary:
- put strings in a single line
- copy bytes from desc to bigbuffer instead of pointer aliasing. (so no krealloc too)
- change tabs to spaces in documentation
- reworded some comments
- drop USB_CONFIG_WINDOWS_REQ_SIZE macro
- revert USB_DELAY_INIT to original behavior. no delay before 1st request.

Let me know if I missed any changes mentioned in previous discussions (or misunderstood and made unnecessary changes :') ).

Quoted 3 lines

I wonder if it wouldn't make sense to split announce_device() so that the first line is printed as soon as usb_new_device() starts, before enumeration is attempted and possibly fails.

Quoted 5 lines

The current patch still logs device ids upon failure in usb_enumerate_device(). Do you want me to implement that suggestion?

Thanks,
Nikhil Solanke

Just following up on my previous email regarding the requested changes to the patch. Any comments on this?

Thanks,
Nikhil Solanke