All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maxim Mikityanskiy <maxtram95@gmail.com>
To: "David S. Miller" <davem@davemloft.net>,
	"Bjørn Mork" <bjorn@mork.no>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Hayes Wang" <hayeswang@realtek.com>
Cc: Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Douglas Anderson <dianders@chromium.org>,
	Grant Grundler <grundler@chromium.org>,
	linux-usb@vger.kernel.org, netdev@vger.kernel.org,
	Maxim Mikityanskiy <maxtram95@gmail.com>
Subject: [PATCH net 1/2] USB: Allow usb_device_driver to override usb_choose_configuration
Date: Sun, 24 Dec 2023 01:35:22 +0200	[thread overview]
Message-ID: <20231223233523.4411-2-maxtram95@gmail.com> (raw)
In-Reply-To: <20231223233523.4411-1-maxtram95@gmail.com>

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


  reply	other threads:[~2023-12-23 23:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-23 23:35 [PATCH net 0/2] r8152: Fix a regression with usbguard Maxim Mikityanskiy
2023-12-23 23:35 ` Maxim Mikityanskiy [this message]
2023-12-23 23:35 ` [PATCH net 2/2] r8152: Switch to using choose_configuration Maxim Mikityanskiy
2023-12-26  7:18   ` Grant Grundler
2023-12-26  7:42     ` Grant Grundler
2023-12-26 10:21       ` Maxim Mikityanskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231223233523.4411-2-maxtram95@gmail.com \
    --to=maxtram95@gmail.com \
    --cc=bjorn@mork.no \
    --cc=davem@davemloft.net \
    --cc=dianders@chromium.org \
    --cc=edumazet@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=grundler@chromium.org \
    --cc=hayeswang@realtek.com \
    --cc=kuba@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.