From: Chen-Yu Tsai <wenst@chromium.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>
Cc: "Chen-Yu Tsai" <wenst@chromium.org>,
"Alan Stern" <stern@rowland.harvard.edu>,
linux-usb@vger.kernel.org, devicetree@vger.kernel.org,
"Matthias Kaehlcke" <mka@chromium.org>,
"Stephen Boyd" <swboyd@chromium.org>,
"Rafał Miłecki" <rafal@milecki.pl>,
"Johan Hovold" <johan@kernel.org>,
"Mathias Nyman" <mathias.nyman@linux.intel.com>,
"Neil Armstrong" <neil.armstrong@linaro.org>,
"Bjorn Andersson" <andersson@kernel.org>
Subject: [PATCH 4/4] usb: of: Check compatible string of device node
Date: Fri, 4 Dec 2026 10:52:42 +0800 [thread overview]
Message-ID: <20261204025243.807657-5-wenst@chromium.org> (raw)
In-Reply-To: <20261204025243.807657-1-wenst@chromium.org>
Currently the USB core finds and associates a device node for a USB
device solely based on the USB hub's downstream port number. There is
no check if the compatible string is a USB one, or if it matches the
vendor and product ID from the USB descriptor.
Check the compatible string of each new USB device and warn if there
is a mismatch. This is done immediately after the USB device IDs are
announced. This should help folks working with onboard USB devices trying
to figure out power sequencing races due to incorrect port assignments.
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
---
drivers/usb/core/hub.c | 3 +++
drivers/usb/core/of.c | 46 ++++++++++++++++++++++++++++++++++++++++++
drivers/usb/core/usb.h | 8 ++++++++
3 files changed, 57 insertions(+)
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index ceb57009af07..bb89fb86daf5 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -2666,6 +2666,9 @@ int usb_new_device(struct usb_device *udev)
/* Announce the device identity */
announce_device_ids(udev);
+ /* Check if associated device node is compatible */
+ usb_of_check_device_compatible(udev);
+
/* Tell the runtime-PM framework the device is active */
pm_runtime_set_active(&udev->dev);
pm_runtime_get_noresume(&udev->dev);
diff --git a/drivers/usb/core/of.c b/drivers/usb/core/of.c
index 47e5428fc8d4..5e10841eb093 100644
--- a/drivers/usb/core/of.c
+++ b/drivers/usb/core/of.c
@@ -7,12 +7,19 @@
* Copyright (C) 2017 Johan Hovold <johan@kernel.org>
*/
+#include <linux/dev_printk.h>
#include <linux/of.h>
#include <linux/of_graph.h>
+#include <linux/sprintf.h>
+#include <linux/types.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/usb/of.h>
+#include <asm/byteorder.h>
+
+#include "usb.h"
+
static int usb_of_find_raw_port(struct usb_device *hub, int port1)
{
if (!hub->parent)
@@ -211,3 +218,42 @@ usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
return NULL;
}
EXPORT_SYMBOL_GPL(usb_of_get_interface_node);
+
+/**
+ * usb_of_check_device_compatible() - check USB device node compatible string
+ * @udev: USB device
+ *
+ * Check if the device node associated with @udev has the correct compatible.
+ */
+void usb_of_check_device_compatible(struct usb_device *udev)
+{
+ struct device_node *np = udev->dev.of_node;
+ u16 vid, pid;
+ char buf[20];
+
+ /* root hubs use the host controller's node and don't have USB compatibles */
+ if (!udev->parent)
+ return;
+
+ if (!np)
+ return;
+
+ /* compatible string is not required */
+ if (!of_property_present(np, "compatible"))
+ return;
+
+ vid = le16_to_cpu(udev->descriptor.idVendor);
+ pid = le16_to_cpu(udev->descriptor.idProduct);
+
+ snprintf(buf, sizeof(buf), "usb%x,%x", vid, pid);
+ if (of_device_is_compatible(np, buf))
+ return;
+
+ /* binding says leading zeros must be suppressed, but some made it in. */
+ snprintf(buf, sizeof(buf), "usb%04x,%04x", vid, pid);
+ if (of_device_is_compatible(np, buf))
+ return;
+
+ dev_warn(&udev->dev, "%pOF: compatible string does not match expected usb%x,%x\n",
+ np, vid, pid);
+}
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 0601460c0e72..68554432c516 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -218,3 +218,11 @@ static inline void usb_acpi_unregister(void) { };
static inline int usb_acpi_port_prr_reset(struct usb_device *hdev,
int port1) { return -ENODEV; }
#endif
+
+#ifdef CONFIG_OF
+void usb_of_check_device_compatible(struct usb_device *udev);
+#else
+static inline void usb_of_check_device_compatible(struct usb_device *udev)
+{
+}
+#endif
--
2.56.1.915.g69d5ec7298-goog
prev parent reply other threads:[~2026-12-04 2:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 2:52 [PATCH 0/4] usb: of: Fix root-hub raw port lookups and graph handling Chen-Yu Tsai
2026-12-04 2:52 ` [PATCH 1/4] usb: of: Ignore 0-based or unnumbered OF graphs in usb_of_get_connect_type() Chen-Yu Tsai
2026-12-04 2:52 ` [PATCH 2/4] usb: of: Use raw port numbers for root hub OF lookups Chen-Yu Tsai
2026-12-04 3:10 ` sashiko-bot
2026-12-04 2:52 ` [PATCH 3/4] usb: misc: onboard_usb_dev: Create pdevs for secondary root hubs Chen-Yu Tsai
2026-12-04 2:52 ` Chen-Yu Tsai [this message]
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=20261204025243.807657-5-wenst@chromium.org \
--to=wenst@chromium.org \
--cc=andersson@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=johan@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@linux.intel.com \
--cc=mka@chromium.org \
--cc=neil.armstrong@linaro.org \
--cc=rafal@milecki.pl \
--cc=robh@kernel.org \
--cc=stern@rowland.harvard.edu \
--cc=swboyd@chromium.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox