From patchwork Fri Dec 1 18:29:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 749439 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="bqnAI4pb" Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04654131 for ; Fri, 1 Dec 2023 10:31:32 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6cdce15f0a3so2726070b3a.1 for ; Fri, 01 Dec 2023 10:31:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1701455490; x=1702060290; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=V5Q+AM+DhOXR89Ju18vzTGC3wrGXus7g2e66yEQ6u/o=; b=bqnAI4pbt/2JXEGzsxEb82qCt80IXSahvoeovzNLUMK3RKJ0eZpYZpeeMkPrTFuYHw wkzmYMAFMBigaGHIToD8H1Mzq0zB/CkB/TVRC16pPdJv/mCV8h9Q5OlsfYWx5L71pKKL 4yBrFGesWjwRfQAOW6UCMTq29cHFFT1JMtw7s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701455490; x=1702060290; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V5Q+AM+DhOXR89Ju18vzTGC3wrGXus7g2e66yEQ6u/o=; b=S5RiUqMnbcaGcJ6DX8Zc8uFx05bwejwq0jY1ELlAQKjr/EquTDlnk2LHTekyXqygTn lBZAbeLhLZnJGY68BD7Q3nhQLS7f0kUOC0HEewJdAFsl7ySBM/6lCdyFjnJuC/Jm5NLM gCXYX7xidR4WphXSGNDYYRWtg/WxUZE9rv4lC/Kjg1M/3Fqci4Vqm4WdPONUKX4wvFwc +NkMoswBvSjE6vpSOqEVpHczmLKjVwrGU/IIgTxUFR6zesd3NsI0WCoHXAgJu1nwshZI HXuywAn+ACW/A8zKWhNPahE6noGjCbpiEDg9ix1ICvJo4JDV2GM3R+uRnaryxR4RMz9T bqPA== X-Gm-Message-State: AOJu0YxL1figLma56du1UvViENyiOqI2FqDTrq38gJRqW124j+Dxry6P L1Mb+HboaS2oWDceRoVRKg8rLMrMu6T1Xx4phlZza4D6 X-Google-Smtp-Source: AGHT+IHAuOQjIDX2waOHWW3Fi2purVTSJ192L6IErkIR7DpoPp8Bdvp+y5ic6UoUqwLOExUhrlIMsA== X-Received: by 2002:a05:6a00:23c4:b0:6cb:cd66:2102 with SMTP id g4-20020a056a0023c400b006cbcd662102mr35720154pfc.4.1701455490601; Fri, 01 Dec 2023 10:31:30 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:11eb:92ac:94e:c791]) by smtp.gmail.com with ESMTPSA id g11-20020a056a00078b00b006cdda10bdafsm3306926pfu.183.2023.12.01.10.31.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 10:31:30 -0800 (PST) From: Douglas Anderson To: linux-usb@vger.kernel.org, Greg Kroah-Hartman , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Grant Grundler , Hayes Wang , Simon Horman , =?utf-8?q?Bj=C3=B8rn_Mork?= , netdev@vger.kernel.org, Brian Geffon , Alan Stern , Douglas Anderson , Hans de Goede , Heikki Krogerus , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] usb: core: Don't force USB generic_subclass drivers to define probe() Date: Fri, 1 Dec 2023 10:29:50 -0800 Message-ID: <20231201102946.v2.1.I7ea0dd55ee2acdb48b0e6d28c1a704ab2c29206f@changeid> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog In-Reply-To: <20231201183113.343256-1-dianders@chromium.org> References: <20231201183113.343256-1-dianders@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There's no real reason that subclassed USB drivers _need_ to define probe() since they might want to subclass for some other reason. Make it optional to define probe() if we're a generic_subclass. Signed-off-by: Douglas Anderson Reviewed-by: Grant Grundler --- Changes in v2: - ("Don't force USB generic_subclass drivers to define ...") new for v2. drivers/usb/core/driver.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c index f58a0299fb3b..1dc0c0413043 100644 --- a/drivers/usb/core/driver.c +++ b/drivers/usb/core/driver.c @@ -290,7 +290,10 @@ static int usb_probe_device(struct device *dev) * specialised device drivers prior to setting the * use_generic_driver bit. */ - error = udriver->probe(udev); + if (udriver->probe) + error = udriver->probe(udev); + else if (!udriver->generic_subclass) + error = -EINVAL; if (error == -ENODEV && udriver != &usb_generic_driver && (udriver->id_table || udriver->match)) { udev->use_generic_driver = 1; From patchwork Fri Dec 1 18:29:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 749438 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CDMaHPE9" Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6AC2196 for ; Fri, 1 Dec 2023 10:31:34 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6cdd9c53282so2193115b3a.3 for ; Fri, 01 Dec 2023 10:31:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1701455494; x=1702060294; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sb01GRQ68eUgyc84/+R565jrsla/E08IXGMB+9OJhaQ=; b=CDMaHPE9v+9nZO1+SszfsPXIqd5f9EKU4uSBGbv1Y4TY0nJrfMWfAWojTNRklK2zbC hR+5Yj8xV5FTAQgFbGKrhcgCqH2GfoamqawzP3Yk2fZV5Mz3MRIe3bJvEdBIV8bghoNk hZXDMoPP+Xpadr+B1n0cyypEy99oKiAsU5V0Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701455494; x=1702060294; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sb01GRQ68eUgyc84/+R565jrsla/E08IXGMB+9OJhaQ=; b=UlJ3sgxZHIEE62p710BA7orPtZ936hJXzxA/TWhWpzGRlpEkMQ+mMu+tO86SP9uSF7 rotWWffI9z8AV5Fl8LcMr6P9XfsTYJ8ulW7E7mqfj1vq2nQIFTUNIqkzLT6OIrP2qudC 018PsgMYlw5nxo3N4Bfpq+LmsmOyWaItKshznGEkMLHMNYMrBkmGrPaynLo5cwdGDrMD BDptSZotPdGpgCTDamA8dzu4dfbfCqDynEH1QhPvYGmwNxfhMQLQPhxavBa8zxI95/et 1MmuzV7BKe7B/HT5BTefHGTwFWfd/mBklcAB6wZQ7Pdq3kvRKzhZ+jAgYYWv+fSHXC9t Nxbg== X-Gm-Message-State: AOJu0Yx7cKeure5RfMdIfM4MRkNH0thI0GwUH4qwYD75HAAz6BedE2/l KBKVyQfZiXASOAFW4By0aWzQgDIfEiq8lEnZEHlz74q+ X-Google-Smtp-Source: AGHT+IE8ytyPVbqRYk7CYLOc1dseuNIRRK2+hGKn1jZLFV3Q171b71RSlG8/WIPvAesDpkr5kxX5YA== X-Received: by 2002:a05:6a00:3016:b0:6cd:dc2e:a444 with SMTP id ay22-20020a056a00301600b006cddc2ea444mr8860209pfb.27.1701455492435; Fri, 01 Dec 2023 10:31:32 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:11eb:92ac:94e:c791]) by smtp.gmail.com with ESMTPSA id g11-20020a056a00078b00b006cdda10bdafsm3306926pfu.183.2023.12.01.10.31.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 10:31:31 -0800 (PST) From: Douglas Anderson To: linux-usb@vger.kernel.org, Greg Kroah-Hartman , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Grant Grundler , Hayes Wang , Simon Horman , =?utf-8?q?Bj=C3=B8rn_Mork?= , netdev@vger.kernel.org, Brian Geffon , Alan Stern , Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] usb: core: Allow subclassed USB drivers to override usb_choose_configuration() Date: Fri, 1 Dec 2023 10:29:51 -0800 Message-ID: <20231201102946.v2.2.Iade5fa31997f1a0ca3e1dec0591633b02471df12@changeid> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog In-Reply-To: <20231201183113.343256-1-dianders@chromium.org> References: <20231201183113.343256-1-dianders@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For some USB devices we might want to do something different for usb_choose_configuration(). One example here is the r8152 driver where we want to end up using the vendor driver with the preferred interface. The r8152 driver tried to make things work by implementing a USB generic_subclass driver and then overriding the normal config selection after it happened. This is less than ideal and also caused breakage if someone deauthorized and re-authorized the USB device because the USB core ended up going back to it's default logic for choosing the best config. I made an attempt to fix this [1] but it was a bit ugly. Let's do this better and allow USB generic_subclass drivers to override usb_choose_configuration(). [1] https://lore.kernel.org/r/20231130154337.1.Ie00e07f07f87149c9ce0b27ae4e26991d307e14b@changeid Suggested-by: Alan Stern Signed-off-by: Douglas Anderson Reviewed-by: Alan Stern --- Changes in v2: - ("Allow subclassed USB drivers to override ...") new for v2. drivers/usb/core/generic.c | 7 +++++++ include/linux/usb.h | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c index 740342a2812a..dcb897158228 100644 --- a/drivers/usb/core/generic.c +++ b/drivers/usb/core/generic.c @@ -59,10 +59,17 @@ 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 = to_usb_device_driver(udev->dev.driver); if (usb_device_is_owned(udev)) return 0; + if (udriver->choose_configuration) { + i = udriver->choose_configuration(udev); + if (i >= 0) + return i; + } + best = NULL; c = udev->config; num_configs = udev->descriptor.bNumConfigurations; diff --git a/include/linux/usb.h b/include/linux/usb.h index 8c61643acd49..618e5a0b1a22 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -1264,6 +1264,9 @@ struct usb_driver { * module is being unloaded. * @suspend: Called when the device is going to be suspended by the system. * @resume: Called when the device is being resumed by the system. + * @choose_configuration: If non-NULL, called instead of the default + * usb_choose_configuration(). If this returns an error then we'll go + * on to call the normal usb_choose_configuration(). * @dev_groups: Attributes attached to the device that will be created once it * is bound to the driver. * @drvwrap: Driver-model core structure wrapper. @@ -1287,6 +1290,9 @@ struct usb_device_driver { int (*suspend) (struct usb_device *udev, pm_message_t message); int (*resume) (struct usb_device *udev, pm_message_t message); + + int (*choose_configuration) (struct usb_device *udev); + const struct attribute_group **dev_groups; struct usbdrv_wrap drvwrap; const struct usb_device_id *id_table; From patchwork Fri Dec 1 18:29:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 750008 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="NAm7TJYY" Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48B35133 for ; Fri, 1 Dec 2023 10:31:35 -0800 (PST) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6cdce15f0a3so2726116b3a.1 for ; Fri, 01 Dec 2023 10:31:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1701455494; x=1702060294; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4QGkTsdRERlNpczcwqSp2Og8v+XNs9gRL2qzJvnknds=; b=NAm7TJYYWKXNyXJQqhfgCjmvkRz0pVGCxouKfwO38mMJeiJ23rVzMICF/GVfMZ8ppf XDm6mJRBLudjMjQjlp47m2wVOs4IifZCBN/9iMaEIbDOoAiAPXb3AFkjVjvVTzWBlTiZ GkP5lpvIVvdufNP1MS3ttUlFaQMJJJEgB4Q1g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701455494; x=1702060294; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4QGkTsdRERlNpczcwqSp2Og8v+XNs9gRL2qzJvnknds=; b=C8AqXXZHHj9Zpp2aoEPrc6eielI5qNqONpNmwLQVTYrycQ16gqDMsvu7qsyZvtb4We Pgw6vWCLdZ0648lczbf+Mtq+obWWZ8HUSFD8weYbSfq2MALMJtgSokj6EYwvAIAxr6y/ POHX9+KICqbvEcmXkmGIvvNsI7xzHfB985MR1Ab1BzG92OkceYKorG+K8NvBhM6naP07 UEGSkj+tZ5irW5Ad+YkWdH+QXUVrcYJL2GijOANtJy//1psEsz/a0Wu6+wyV1e/64tb2 dGOvfbPJZqUpnXRhvTh7EY/Gi7OK4sfDJVxL54m/L7ZNVc0wyzF82OR/PIl+kWyQNLAL LGFA== X-Gm-Message-State: AOJu0YwxtkISfOJkM4xiYWWLZOE5Q4bO2jkYUxbQw2490+vc9IcBNZzm QbzwIADFEoO26PCU9tklAZ7qSVEmD+UH4wTzmwCQzeE6 X-Google-Smtp-Source: AGHT+IEVreiCKxTXFQEfBUwM2+9H3I4ehz8qYButpoJ4w50/BJH9JRBwy33v+a0gi/YRn62yOPKwgg== X-Received: by 2002:a05:6a00:430e:b0:6cd:fe6e:ac9b with SMTP id cb14-20020a056a00430e00b006cdfe6eac9bmr3530436pfb.0.1701455494239; Fri, 01 Dec 2023 10:31:34 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:11eb:92ac:94e:c791]) by smtp.gmail.com with ESMTPSA id g11-20020a056a00078b00b006cdda10bdafsm3306926pfu.183.2023.12.01.10.31.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 10:31:33 -0800 (PST) From: Douglas Anderson To: linux-usb@vger.kernel.org, Greg Kroah-Hartman , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Grant Grundler , Hayes Wang , Simon Horman , =?utf-8?q?Bj=C3=B8rn_Mork?= , netdev@vger.kernel.org, Brian Geffon , Alan Stern , Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH net v2 3/3] r8152: Choose our USB config with choose_configuration() rather than probe() Date: Fri, 1 Dec 2023 10:29:52 -0800 Message-ID: <20231201102946.v2.3.Ie00e07f07f87149c9ce0b27ae4e26991d307e14b@changeid> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog In-Reply-To: <20231201183113.343256-1-dianders@chromium.org> References: <20231201183113.343256-1-dianders@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If you deauthorize the r8152 device (by writing 0 to the "authorized" field in sysfs) and then reauthorize it (by writing a 1) then it no longer works. This is because when you do the above we lose the special configuration that we set in rtl8152_cfgselector_probe(). Deauthorizing causes the config to be set to -1 and then reauthorizing runs the default logic for choosing the best config. I made an attempt to fix it so that the config is kept across deauthorizing / reauthorizing [1] but it was a bit ugly. Let's instead use the new USB core feature to override choose_configuration(). This patch relies upon the patches ("usb: core: Don't force USB generic_subclass drivers to define probe()") and ("usb: core: Allow subclassed USB drivers to override usb_choose_configuration()") [1] https://lore.kernel.org/r/20231130154337.1.Ie00e07f07f87149c9ce0b27ae4e26991d307e14b@changeid Fixes: ec51fbd1b8a2 ("r8152: add USB device driver for config selection") Suggested-by: Alan Stern Signed-off-by: Douglas Anderson Acked-by: Jakub Kicinski --- Changes in v2: - ("Choose our USB config with choose_configuration()...) new for v2. drivers/net/usb/r8152.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c index 2c5c1e91ded6..0da723d11326 100644 --- a/drivers/net/usb/r8152.c +++ b/drivers/net/usb/r8152.c @@ -10053,7 +10053,7 @@ static struct usb_driver rtl8152_driver = { .disable_hub_initiated_lpm = 1, }; -static int rtl8152_cfgselector_probe(struct usb_device *udev) +static int rtl8152_cfgselector_choose_configuration(struct usb_device *udev) { struct usb_host_config *c; int i, num_configs; @@ -10080,19 +10080,13 @@ static int rtl8152_cfgselector_probe(struct usb_device *udev) if (i == num_configs) return -ENODEV; - if (usb_set_configuration(udev, c->desc.bConfigurationValue)) { - dev_err(&udev->dev, "Failed to set configuration %d\n", - c->desc.bConfigurationValue); - return -ENODEV; - } - - return 0; + return c->desc.bConfigurationValue; } static struct usb_device_driver rtl8152_cfgselector_driver = { - .name = MODULENAME "-cfgselector", - .probe = rtl8152_cfgselector_probe, - .id_table = rtl8152_table, + .name = MODULENAME "-cfgselector", + .choose_configuration = rtl8152_cfgselector_choose_configuration, + .id_table = rtl8152_table, .generic_subclass = 1, .supports_autosuspend = 1, };