Skip to contentSkip to conversation

usbcore: Add quirk for 255-bytes initial config read

PATCH Superseded Nikhil Solanke 2 participants 6 messages Jun 19 – Jun 23

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 aklsjdasd 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. This is not visible in Windows Wireshark/USBPcap traces because Windows routes enumeration-phase traffic to sniffers only after initialization completes. 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.

---
 drivers/usb/core/config.c  | 56 +++++++++++++++++++++++++++-----------
 drivers/usb/core/quirks.c  |  3 ++
 include/linux/usb/quirks.h |  4 +++
 3 files changed, 47 insertions(+), 16 deletions(-)
drivers/usb/core/config.c +40 -16
index 45e20c6d76c0..623425cef085 100644
@@ -912,6 +912,8 @@ int usb_get_configuration(struct usb_device *dev)
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 	int result;
+	size_t usb_dt_config_size = (dev->quirks & USB_QUIRK_CONFIG_SIZE)
+		? USB_DT_CONFIG_SIZE_QUIRK : USB_DT_CONFIG_SIZE;
 
 	if (ncfg > USB_MAXCONFIG) {
 		dev_notice(ddev, "too many configurations: %d, "
@@ -938,7 +940,8 @@ int usb_get_configuration(struct usb_device *dev)
 	if (!dev->rawdescriptors)
 		return -ENOMEM;
 
-	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+	desc = kmalloc(usb_dt_config_size, GFP_KERNEL);
+
 	if (!desc)
 		return -ENOMEM;
 
@@ -946,7 +949,7 @@ int usb_get_configuration(struct usb_device *dev)
 		/* We grab just the first descriptor so we know how long
 		 * the whole configuration is */
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    desc, USB_DT_CONFIG_SIZE);
+		    desc, usb_dt_config_size);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -957,26 +960,39 @@ 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);
+			    "(expected %zu, got %i)\n", cfgno,
+			    usb_dt_config_size, result);
 			result = -EINVAL;
 			goto err;
 		}
-		length = max_t(int, le16_to_cpu(desc->wTotalLength),
-		    USB_DT_CONFIG_SIZE);
+		/* If the device does returns the full length configuration
+		 * descriptor, skip the second read. Fallback to default
+		 * behavior otherwise.
+		 */
+		if (dev->quirks & USB_QUIRK_CONFIG_SIZE
+				&& result == le16_to_cpu(desc->wTotalLength)
+				&& result < USB_DT_CONFIG_SIZE_QUIRK) {
 
-		/* Now that we know the length, get the whole thing */
-		bigbuffer = kmalloc(length, GFP_KERNEL);
-		if (!bigbuffer) {
-			result = -ENOMEM;
-			goto err;
-		}
+			bigbuffer = (unsigned char *) desc;
+			desc = NULL;
+			length = result;
+		} else {
+			length = max_t(int, le16_to_cpu(desc->wTotalLength),
+			    usb_dt_config_size);
+
+			/* Now that we know the length, get the whole thing */
+			bigbuffer = kmalloc(length, GFP_KERNEL);
+			if (!bigbuffer) {
+				result = -ENOMEM;
+				goto err;
+			}
 
-		if (dev->quirks & USB_QUIRK_DELAY_INIT)
-			msleep(200);
+			if (dev->quirks & USB_QUIRK_DELAY_INIT)
+				msleep(200);
 
-		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    bigbuffer, length);
+			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");
@@ -1000,6 +1016,14 @@ int usb_get_configuration(struct usb_device *dev)
 	}
 
 err:
+	/* Log failed device's VID:PID pair to make it easy to debug and fix
+	 * enumeration and initialization issues
+	 */
+	if (result < 0) {
+		dev_err(ddev, "Failed to initialize device %04x:%04x due to above errors.",
+		    le16_to_cpu(dev->descriptor.idVendor), le16_to_cpu(dev->descriptor.idProduct));
+	}
+
 	kfree(desc);
 	dev->descriptor.bNumConfigurations = cfgno;
 
drivers/usb/core/quirks.c +3 -0
index 87810eff974e..92219684a604 100644
@@ -142,6 +142,9 @@ 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_CONFIG_SIZE;
 			/* Ignore unrecognized flag characters */
 			}
 		}
include/linux/usb/quirks.h +4 -0
index b3cc7beab4a3..f864571da870 100644
@@ -81,4 +81,8 @@
 /* Device claims zero configurations, forcing to 1 */
 #define USB_QUIRK_FORCE_ONE_CONFIG		BIT(18)
 
+/* Use a 255 byte sized config descriptor request */
+#define USB_QUIRK_CONFIG_SIZE			BIT(19)
+#define USB_DT_CONFIG_SIZE_QUIRK		255
+
 #endif /* __LINUX_USB_QUIRKS_H */
Signature · 1 line
2.54.0
#1 lore raw
Quoted 26 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 aklsjdasd 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. This is not visible in Windows Wireshark/USBPcap traces because Windows routes enumeration-phase traffic to sniffers only after initialization completes. 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.

Quoted 7 lines

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.

You don't need Suggested-by here. It's redundant; we always assume that people are responsible for authorship of the patches they write and submit, unless they say otherwise.

Quoted 18 lines
---
 drivers/usb/core/config.c  | 56 +++++++++++++++++++++++++++-----------
 drivers/usb/core/quirks.c  |  3 ++
 include/linux/usb/quirks.h |  4 +++
 3 files changed, 47 insertions(+), 16 deletions(-)
drivers/usb/core/config.c +0 -0
index 45e20c6d76c0..623425cef085 100644

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

Quoted 5 lines
drivers/usb/core/config.c +2 -0
@@ -912,6 +912,8 @@ int usb_get_configuration(struct usb_device *dev)
 	unsigned char *bigbuffer;
 	struct usb_config_descriptor *desc;
 	int result;
+	size_t usb_dt_config_size = (dev->quirks & USB_QUIRK_CONFIG_SIZE)
+		? USB_DT_CONFIG_SIZE_QUIRK : USB_DT_CONFIG_SIZE;

I wouldn't call the variable usb_dt_config_size. It isn't always the size of a USB configuration descriptor; it isn't even always the size you expect for the response. Rather, it is the size you intend to ask for.

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

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

Quoted 2 lines
drivers/usb/core/config.c +0 -0
@@ -946,7 +949,7 @@ int usb_get_configuration(struct usb_device *dev)
 		/* We grab just the first descriptor so we know how long
 		 * the whole configuration is */

This comment is now out of date. It should be rewritten to explain why the quirk does and why.

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

Quoted 13 lines
drivers/usb/core/config.c +2 -3
 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    desc, USB_DT_CONFIG_SIZE);
+		    desc, usb_dt_config_size);
 		if (result < 0) {
 			dev_err(ddev, "unable to read config index %d "
 			    "descriptor/%s: %d\n", cfgno, "start", result);
@@ -957,26 +960,39 @@ 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);
+			    "(expected %zu, got %i)\n", cfgno,

Likewise, "expected" here is wrong. It should be "asked for" or something like that.

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

Quoted 10 lines
drivers/usb/core/config.c +5 -2
+			    usb_dt_config_size, result);
 			result = -EINVAL;
 			goto err;
 		}
-		length = max_t(int, le16_to_cpu(desc->wTotalLength),
-		    USB_DT_CONFIG_SIZE);
+		/* If the device does returns the full length configuration
+		 * descriptor, skip the second read. Fallback to default
+		 * behavior otherwise.
+		 */

New multiline comments (or ones that are rewritten) should use the same format as the rest of the USB stack:

	/*
	 * Blah, blah, blah
	 * Blah, blah, blah
	 */

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

Quoted 3 lines
drivers/usb/core/config.c +3 -0
+		if (dev->quirks & USB_QUIRK_CONFIG_SIZE
+				&& result == le16_to_cpu(desc->wTotalLength)
+				&& result < USB_DT_CONFIG_SIZE_QUIRK) {

Whether the quirk flag is set doesn't matter. All you care about is whether the information received earlier contains the entire descriptor set. The first and third tests here should be removed.

There is some question about what to do if wTotalLength < result. My advice is to use the smaller value in this case, but not smaller than USB_DT_CONFIG_SIZE.

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

Quoted 10 lines
drivers/usb/core/config.c +3 -6
-		/* Now that we know the length, get the whole thing */
-		bigbuffer = kmalloc(length, GFP_KERNEL);
-		if (!bigbuffer) {
-			result = -ENOMEM;
-			goto err;
-		}
+			bigbuffer = (unsigned char *) desc;
+			desc = NULL;
+			length = result;

Don't keep the entire 255-byte buffer. Use krealloc() to shrink the buffer down to the right size.

Quoted 25 lines
drivers/usb/core/config.c +15 -4
+		} else {
+			length = max_t(int, le16_to_cpu(desc->wTotalLength),
+			    usb_dt_config_size);
+
+			/* Now that we know the length, get the whole thing */
+			bigbuffer = kmalloc(length, GFP_KERNEL);
+			if (!bigbuffer) {
+				result = -ENOMEM;
+				goto err;
+			}
 
-		if (dev->quirks & USB_QUIRK_DELAY_INIT)
-			msleep(200);
+			if (dev->quirks & USB_QUIRK_DELAY_INIT)
+				msleep(200);
 
-		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-		    bigbuffer, length);
+			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");

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

Quoted 8 lines
drivers/usb/core/config.c +5 -0
@@ -1000,6 +1016,14 @@ int usb_get_configuration(struct usb_device *dev)
 	}
 
 err:
+	/* Log failed device's VID:PID pair to make it easy to debug and fix
+	 * enumeration and initialization issues
+	 */
+	if (result < 0) {
+		dev_err(ddev, "Failed to initialize device %04x:%04x due to above errors.",

The "due to above errors" part isn't needed, since the errors will be obvious in the kernel log. In fact, it probably would be better not to put this information here at all but instead modify the error message in usb_enumerate_device() (the caller).

Quoted 11 lines
drivers/usb/core/config.c +3 -0
+		    le16_to_cpu(dev->descriptor.idVendor), le16_to_cpu(dev->descriptor.idProduct));
+	}
+
 	kfree(desc);
 	dev->descriptor.bNumConfigurations = cfgno;
 
drivers/usb/core/quirks.c +0 -0
index 87810eff974e..92219684a604 100644

drivers/usb/core/quirks.c static int quirks_param_set(const char *value, const struct kernel_param *kp) · line 147

Quoted 6 lines
drivers/usb/core/quirks.c +3 -0
@@ -142,6 +142,9 @@ 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_CONFIG_SIZE;

For good style, there should be a "break" statement here.

Also, you need to document the new flag under the usbcore.quirks entry in Documentation/admin-guide/kernel-parameters.txt.

Quoted 8 lines
drivers/usb/core/quirks.c +0 -0
 			/* Ignore unrecognized flag characters */
 			}
 		}
include/linux/usb/quirks.h +0 -0
index b3cc7beab4a3..f864571da870 100644

include/linux/usb/quirks.h line 86

Quoted 6 lines
include/linux/usb/quirks.h +3 -0
@@ -81,4 +81,8 @@
 /* Device claims zero configurations, forcing to 1 */
 #define USB_QUIRK_FORCE_ONE_CONFIG		BIT(18)
 
+/* Use a 255 byte sized config descriptor request */
+#define USB_QUIRK_CONFIG_SIZE			BIT(19)
+#define USB_DT_CONFIG_SIZE_QUIRK		255

Again, I don't like this name. It's not a quirk in the size of the configuration descriptor type, which is what "USB_DT_CONFIG_SIZE" stands for; it's a quirk in the way the kernel asks for config descriptors. (Or in what size request the device will accept, if you prefer.)

And the 255 value doesn't belong in this header file anyway. It should be defined in config.c since that's the only place it gets used.

Alan Stern

Quoted 5 lines
include/linux/usb/quirks.h +1 -0
+
 #endif /* __LINUX_USB_QUIRKS_H */
Signature · 1 line
2.54.0
#2 lore raw
Quoted 46 lines

You don't need Suggested-by here. It's redundant; we always assume that people are responsible for authorship of the patches they write and submit, unless they say otherwise.

Quoted 18 lines
---
 drivers/usb/core/config.c  | 56 +++++++++++++++++++++++++++-----------
 drivers/usb/core/quirks.c  |  3 ++
 include/linux/usb/quirks.h |  4 +++
 3 files changed, 47 insertions(+), 16 deletions(-)
drivers/usb/core/config.c +0 -0
index 45e20c6d76c0..623425cef085 100644

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

Quoted 5 lines
drivers/usb/core/config.c +2 -0
@@ -912,6 +912,8 @@ int usb_get_configuration(struct usb_device *dev)
      unsigned char *bigbuffer;
      struct usb_config_descriptor *desc;
      int result;
+     size_t usb_dt_config_size = (dev->quirks & USB_QUIRK_CONFIG_SIZE)
+             ? USB_DT_CONFIG_SIZE_QUIRK : USB_DT_CONFIG_SIZE;

I wouldn't call the variable usb_dt_config_size. It isn't always the size of a USB configuration descriptor; it isn't even always the size you expect for the response. Rather, it is the size you intend to ask for.

Quoted 13 lines
drivers/usb/core/config.c +2 -1
      if (ncfg > USB_MAXCONFIG) {
              dev_notice(ddev, "too many configurations: %d, "
@@ -938,7 +940,8 @@ int usb_get_configuration(struct usb_device *dev)
      if (!dev->rawdescriptors)
              return -ENOMEM;
 
-     desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
+     desc = kmalloc(usb_dt_config_size, GFP_KERNEL);
+
      if (!desc)
              return -ENOMEM;
 
Quoted 6 lines

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

Quoted 3 lines
drivers/usb/core/config.c +0 -0
@@ -946,7 +949,7 @@ int usb_get_configuration(struct usb_device *dev)
              /* We grab just the first descriptor so we know how long
               * the whole configuration is */

This comment is now out of date. It should be rewritten to explain why the quirk does and why.

I will explain the quirk above where the variable is defined and reference it here. A reader would probably question about the quirk there.

Quoted 17 lines
Quoted 8 lines
drivers/usb/core/config.c +1 -1
              result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
-                 desc, USB_DT_CONFIG_SIZE);
+                 desc, usb_dt_config_size);
              if (result < 0) {
                      dev_err(ddev, "unable to read config index %d "
                          "descriptor/%s: %d\n", cfgno, "start", result);

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

Quoted 6 lines
drivers/usb/core/config.c +1 -2
@@ -957,26 +960,39 @@ 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);
+                         "(expected %zu, got %i)\n", cfgno,

Likewise, "expected" here is wrong. It should be "asked for" or something like that.

For this branch tho, we are expecting atleast 9 bytes. if we don't get those we simply bail out. expected is the right word here. But this was the originial implementation. With the introduction of the quirk, the wording of it does fall apart. Instead of adding more branches just to rename a log message, let's just keep it as "expected"? In a similar branch later on, when we ask for the bigbuffer, it does make sense to use "asked for" like you suggested. I will make the change there.

Quoted 12 lines

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

Quoted 11 lines
drivers/usb/core/config.c +5 -2
+                         usb_dt_config_size, result);
                      result = -EINVAL;
                      goto err;
              }
-             length = max_t(int, le16_to_cpu(desc->wTotalLength),
-                 USB_DT_CONFIG_SIZE);
+             /* If the device does returns the full length configuration
+              * descriptor, skip the second read. Fallback to default
+              * behavior otherwise.
+              */
Quoted 8 lines

New multiline comments (or ones that are rewritten) should use the same format as the rest of the USB stack:

/*
* Blah, blah, blah
* Blah, blah, blah
*/

Alright. About the comments formatting, all other comments were not following a consistent format in the same file. so I didn't bother to fix such small style changes. But I will still fix them in the way you (and kernel code style guidelines) say.

Quoted 7 lines

Whether the quirk flag is set doesn't matter. All you care about is whether the information received earlier contains the entire descriptor set. The first and third tests here should be removed.

There is some question about what to do if wTotalLength < result. My advice is to use the smaller value in this case, but not smaller than USB_DT_CONFIG_SIZE.

In the case where wTotalLength < result, wouldn't it be better to consider the result value as the truth? Or are there scenarios where the device or the buffer will contain gibberish just to fill it, which is why you suggested a smaller value. I did understand the part that it should be atleast USB_DT_CONFIG_SIZE because its the header, but isn't that part already handled above with result < 4? It does ensure all the critical fields are actually present. And with just the second test, the code will naturally jump to the else branch for any cases like you mentioned. I will change the max_t back to use USB_DT_CONFIG_SIZE and everything seems to be covered now? Also looking at the 3rd test now, it is actually redundant. Thanks for pointing that out.

Quoted 11 lines

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

Quoted 10 lines
drivers/usb/core/config.c +3 -6
-             /* Now that we know the length, get the whole thing */
-             bigbuffer = kmalloc(length, GFP_KERNEL);
-             if (!bigbuffer) {
-                     result = -ENOMEM;
-                     goto err;
-             }
+                     bigbuffer = (unsigned char *) desc;
+                     desc = NULL;
+                     length = result;
Quoted 2 lines

Don't keep the entire 255-byte buffer. Use krealloc() to shrink the buffer down to the right size.

I did intially though of using krealloc(), but when looked at existing implementation, bigbuffer is alloced with wTotalLength while ensuring its atleast USB_DT_CONFIG_SIZE (9) bytes. Then when we receive the result, the bigbuffer isn't realloced as per the size we received. So I tried to mirror this exising behavior in fear that I might mess up something else while trying to be smart. (Although yea, it is waste of memory).

Quoted 4 lines

The "due to above errors" part isn't needed, since the errors will be obvious in the kernel log. In fact, it probably would be better not to put this information here at all but instead modify the error message in usb_enumerate_device() (the caller).

Alright

Quoted 5 lines
Quoted 5 lines
drivers/usb/core/quirks.c +0 -0
index 87810eff974e..92219684a604 100644
@@ -142,6 +142,9 @@ static int quirks_param_set(const char *value, const struct kernel_param *kp)
Quoted 8 lines

drivers/usb/core/quirks.c static int quirks_param_set(const char *value, const struct kernel_param *kp) · line 147

Quoted 6 lines
drivers/usb/core/quirks.c +3 -0
                              break;
                      case 'q':
                              flags |= USB_QUIRK_FORCE_ONE_CONFIG;
+                             break;
+                     case 'r':
+                             flags |= USB_QUIRK_CONFIG_SIZE;

For good style, there should be a "break" statement here.

Yea I do agree. Then again I tried to keep my changes in respect to what was like originally. I will add the missing break then.

Quoted 2 lines

Also, you need to document the new flag under the usbcore.quirks entry in Documentation/admin-guide/kernel-parameters.txt.

Oh sorry, totally missed it. Working on it.

Quoted 7 lines

Again, I don't like this name. It's not a quirk in the size of the configuration descriptor type, which is what "USB_DT_CONFIG_SIZE" stands for; it's a quirk in the way the kernel asks for config descriptors. (Or in what size request the device will accept, if you prefer.)

And the 255 value doesn't belong in this header file anyway. It should be defined in config.c since that's the only place it gets used.

So what about USB_CONFIG_REQ_SIZE? it's what you had suggested before in earlier conversations (without the QUIRK word in between naming our magic number 255).

Nikhil Solanke

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

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

Quoted 3 lines
drivers/usb/core/config.c +0 -0
@@ -946,7 +949,7 @@ int usb_get_configuration(struct usb_device *dev)
              /* We grab just the first descriptor so we know how long
               * the whole configuration is */

This comment is now out of date. It should be rewritten to explain why the quirk does and why.

I will explain the quirk above where the variable is defined and reference it here. A reader would probably question about the quirk there.

Okay, so long as you don't keep a comment that is now incorrect.

Quoted 11 lines
Quoted 10 lines

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

Quoted 7 lines
drivers/usb/core/config.c +1 -2
@@ -957,26 +960,39 @@ 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);
+                         "(expected %zu, got %i)\n", cfgno,

Likewise, "expected" here is wrong. It should be "asked for" or something like that.

Quoted 7 lines

For this branch tho, we are expecting atleast 9 bytes. if we don't get those we simply bail out. expected is the right word here. But this was the originial implementation. With the introduction of the quirk, the wording of it does fall apart. Instead of adding more branches just to rename a log message, let's just keep it as "expected"? In a similar branch later on, when we ask for the bigbuffer, it does make sense to use "asked for" like you suggested. I will make the change there.

Again, I don't care too much about the exact wording; I just want to make sure that the log message isn't actually wrong. Saying "expected 255" would definitely be wrong, but saying "expected at least 9" would be okay.

Quoted 20 lines
Quoted 11 lines

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

Quoted 10 lines
drivers/usb/core/config.c +5 -2
+                         usb_dt_config_size, result);
                      result = -EINVAL;
                      goto err;
              }
-             length = max_t(int, le16_to_cpu(desc->wTotalLength),
-                 USB_DT_CONFIG_SIZE);
+             /* If the device does returns the full length configuration
+              * descriptor, skip the second read. Fallback to default
+              * behavior otherwise.
+              */
Quoted 8 lines

New multiline comments (or ones that are rewritten) should use the same format as the rest of the USB stack:

/*
* Blah, blah, blah
* Blah, blah, blah
*/

Quoted 4 lines

Alright. About the comments formatting, all other comments were not following a consistent format in the same file. so I didn't bother to fix such small style changes. But I will still fix them in the way you (and kernel code style guidelines) say.

Yeah, don't bother to fix up comments that the patch doesn't otherwise touch -- that would be mere pointless churn and would make reviewing the patch more difficult for no good reason. But comments that you _do_ alter are fair game for reformatting.

Quoted 4 lines
Quoted 4 lines

Whether the quirk flag is set doesn't matter. All you care about is whether the information received earlier contains the entire descriptor set. The first and third tests here should be removed.

Quoted 8 lines
Quoted 3 lines

There is some question about what to do if wTotalLength < result. My advice is to use the smaller value in this case, but not smaller than USB_DT_CONFIG_SIZE.

In the case where wTotalLength < result, wouldn't it be better to consider the result value as the truth? Or are there scenarios where the device or the buffer will contain gibberish just to fill it, which is why you suggested a smaller value.

Sort of. Software that parses the descriptors later on will ignore the extra stuff in any case, limiting itself to just the first wTotalLength bytes. So there's not really any need to keep the excess.

Quoted 4 lines

I did understand the part that it should be
atleast USB_DT_CONFIG_SIZE because its the header, but isn't that part already handled above with result < 4? It does ensure all the critical fields are actually present.

The critical fields are not just the first 4 bytes, but the first 9 bytes. If you want, you can change that test against 4 above, making it against USB_DT_CONFIG_SIZE. The reason the existing code only tests against 4 is because it knows that it will issue another request for the full length, but that won't always be true after your changes.

Quoted 21 lines

And with just the second test, the code will
naturally jump to the else branch for any cases like you mentioned. I will change the max_t back to use USB_DT_CONFIG_SIZE and everything seems to be covered now? Also looking at the 3rd test now, it is actually redundant. Thanks for pointing that out.

Quoted 11 lines

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

Quoted 10 lines
drivers/usb/core/config.c +3 -6
-             /* Now that we know the length, get the whole thing */
-             bigbuffer = kmalloc(length, GFP_KERNEL);
-             if (!bigbuffer) {
-                     result = -ENOMEM;
-                     goto err;
-             }
+                     bigbuffer = (unsigned char *) desc;
+                     desc = NULL;
+                     length = result;
Quoted 2 lines

Don't keep the entire 255-byte buffer. Use krealloc() to shrink the buffer down to the right size.

Quoted 6 lines

I did intially though of using krealloc(), but when looked at existing implementation, bigbuffer is alloced with wTotalLength while ensuring its atleast USB_DT_CONFIG_SIZE (9) bytes. Then when we receive the result, the bigbuffer isn't realloced as per the size we received. So I tried to mirror this exising behavior in fear that I might mess up something else while trying to be smart. (Although yea, it is waste of memory).

You're right that the existing code could have reallocated the buffer if it received less than it asked for. I believe it doesn't bother to do this because that possibility is considered to be very unlikely.

In fact, you could arrange to do the check and reallocation after the two code paths merge back together. That would be the best solution.

Quoted 11 lines
Quoted 7 lines

Again, I don't like this name. It's not a quirk in the size of the configuration descriptor type, which is what "USB_DT_CONFIG_SIZE" stands for; it's a quirk in the way the kernel asks for config descriptors. (Or in what size request the device will accept, if you prefer.)

And the 255 value doesn't belong in this header file anyway. It should be defined in config.c since that's the only place it gets used.

So what about USB_CONFIG_REQ_SIZE? it's what you had suggested before in earlier conversations (without the QUIRK word in between naming our magic number 255).

Yes, that seems like a better choice for the name. Or maybe USB_CONFIG_BIG_REQ_SIZE or USB_CONFIG_WINDOWS_REQ_SIZE, to indicate this isn't the value we normally use but copies the behavior of Windows.

Alan Stern

#4 lore raw

I have a v2 patch ready with all the requested changes along with the documentation in Documentation/admin-guide/kernel-parameters.txt. Is there any other place where I have to write documentation?

Also, I would like to know the "low-level" type reason as to why do we have 2 separate buffers. a desc and then a bigbuffer? Why don't we just realloc the desc buffer? Does this have something to do with reallocs in general?

Also (a bit more tangent), can the usb device potentially fingerprint the host os? if we are asking for 9 bytes first and windows ask for 255, is it possible that some usb devices will fingerprint the OS based on this, and behave differently? are there any other such places where fingerprinting is possible? In those cases, is it theoretically possible that this patch might fix some weird devices that "seem to work" on windows but not on linux? I might just add this one line to documentation that it might theoretically fix other usb devices as well instead of it just being a quirk to fix a game controller.

Nikhil Solanke

#5 lore raw
Quoted 3 lines · Nikhil Solanke

I have a v2 patch ready with all the requested changes along with the documentation in Documentation/admin-guide/kernel-parameters.txt. Is there any other place where I have to write documentation?

No.

Quoted 4 lines

Also, I would like to know the "low-level" type reason as to why do we have 2 separate buffers. a desc and then a bigbuffer? Why don't we just realloc the desc buffer? Does this have something to do with reallocs in general?

No particular reason. I guess it just seemed more straightforward to allocate a second, larger buffer than to expand the first, smaller buffer.

Quoted 7 lines

Also (a bit more tangent), can the usb device potentially fingerprint the host os? if we are asking for 9 bytes first and windows ask for 255, is it possible that some usb devices will fingerprint the OS based on this, and behave differently? are there any other such places where fingerprinting is possible? In those cases, is it theoretically possible that this patch might fix some weird devices that "seem to work" on windows but not on linux?

Of course it's possible. Devices can do whatever they want, as long as they obey the requirements of the USB spec.

Quoted 3 lines

I might just add this one line to
documentation that it might theoretically fix other usb devices as well instead of it just being a quirk to fix a game controller.

Isn't that true of any quirk? As a theoretical possibility, I mean.

Alan Stern