diff mbox series

[net,1/2] USB: Allow usb_device_driver to override usb_choose_configuration

Message ID 20231223233523.4411-2-maxtram95@gmail.com (mailing list archive)
State New
Headers show
Series r8152: Fix a regression with usbguard | expand

Commit Message

Maxim Mikityanskiy Dec. 23, 2023, 11:35 p.m. UTC
usb_choose_configuration is called in two cases: on probe and on
authorization. If a usb_device_driver wants to override the default
configuration (like r8152 does to choose the vendor config), it can do
that on probe, but it has no control over what happens on authorization.
This breaks the intention on machines that use usbguard (all devices are
not authorized by default, and the permitted ones get the authorization
a moment later), because a wrong configuration ends up being selected.

Allow usb_device_driver to override usb_choose_configuration
specifically.

Signed-off-by: Maxim Mikityanskiy <maxtram95@gmail.com>
---
 drivers/usb/core/generic.c | 10 ++++++++++
 include/linux/usb.h        |  3 +++
 2 files changed, 13 insertions(+)
diff mbox series

Patch

diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c
index 740342a2812a..a1bc4f875d37 100644
--- a/drivers/usb/core/generic.c
+++ b/drivers/usb/core/generic.c
@@ -59,10 +59,20 @@  int usb_choose_configuration(struct usb_device *udev)
 	int num_configs;
 	int insufficient_power = 0;
 	struct usb_host_config *c, *best;
+	struct usb_device_driver *udriver = NULL;
 
 	if (usb_device_is_owned(udev))
 		return 0;
 
+	if (udev->dev.driver) {
+		udriver = to_usb_device_driver(udev->dev.driver);
+		if (udriver->choose_configuration) {
+			i = udriver->choose_configuration(udev);
+			if (i != -EOPNOTSUPP)
+				return max(i, -1);
+		}
+	}
+
 	best = NULL;
 	c = udev->config;
 	num_configs = udev->descriptor.bNumConfigurations;
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 8c61643acd49..4f59b10f2fdd 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1259,6 +1259,8 @@  struct usb_driver {
  *	device.  If it is, probe returns zero and uses dev_set_drvdata()
  *	to associate driver-specific data with the device.  If unwilling
  *	to manage the device, return a negative errno value.
+ * @choose_configuration: Called from usb_choose_configuration, allows the
+ *	driver to override the default configuration.
  * @disconnect: Called when the device is no longer accessible, usually
  *	because it has been (or is being) disconnected or the driver's
  *	module is being unloaded.
@@ -1283,6 +1285,7 @@  struct usb_device_driver {
 
 	bool (*match) (struct usb_device *udev);
 	int (*probe) (struct usb_device *udev);
+	int (*choose_configuration) (struct usb_device *udev);
 	void (*disconnect) (struct usb_device *udev);
 
 	int (*suspend) (struct usb_device *udev, pm_message_t message);