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 Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series r8152: Fix a regression with usbguard | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1400 this patch: 1400
netdev/cc_maintainers success CCed 3 of 3 maintainers
netdev/build_clang success Errors and warnings before: 1176 this patch: 1176
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1445 this patch: 1445
netdev/checkpatch warning WARNING: Unnecessary space before function pointer arguments
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

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);