* [PATCH 1/4] usb: of: Ignore 0-based or unnumbered OF graphs in usb_of_get_connect_type()
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 ` 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
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Chen-Yu Tsai @ 2026-12-04 2:52 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: Chen-Yu Tsai, Alan Stern, linux-usb, devicetree,
Matthias Kaehlcke, Stephen Boyd, Rafał Miłecki,
Johan Hovold, Mathias Nyman, Neil Armstrong, Bjorn Andersson,
stable
USB downstream port numbering in both the USB specifications and the USB
device tree bindings (usb-device.yaml, usb-hub.yaml) starts at 1; valid
USB port graphs never define port@0 or an unnumbered "port" node.
usb-hcd.yaml will also gain a "ports" child node following this 1-based
numbering for attaching USB connectors to downstream ports via OF graphs.
However, existing dual-role USB controller bindings (as well as the
legacy unnumbered "port" in mediatek,mtk-xhci.yaml) use OF graph
connections that do not follow 1-based USB port numbering:
- An unnumbered "port" child node for usb-role-switch (e.g. dwc2,
chipidea, cdns3, and single-port snps,dwc3) or a Type-C SuperSpeed
endpoint (mediatek,mtk-xhci).
- 0-based port@0 (HS) and port@1 (SS) numbering (existing snps,dwc3
bindings).
Currently usb_of_get_connect_type() checks of_graph_is_present() without
verifying that the graph uses 1-based numbering. As a result:
- Controllers with an unnumbered "port" node never match a 1-based port
number and return USB_PORT_NOT_USED, causing external hot-plug devices
to be marked DEVICE_FIXED.
- Controllers with 0-based port@0/port@1 nodes match the wrong port on
port 1, and once root-hub raw port lookup is used, SuperSpeed port 2
finds no port@2 and returns USB_PORT_NOT_USED (DEVICE_FIXED).
Since port@0 indicates a 0-based scheme where port@1 is also shifted,
ignore the entire OF graph whenever port 0 is present (via
of_graph_get_port_by_id(np, 0), which treats a missing "reg" property on
an unnumbered "port" node as 0 as well). Such controllers fall back to
USB_PORT_CONNECT_TYPE_UNKNOWN unless hard-wired child USB device nodes
(which already use 1-based port numbers, e.g. onboard hubs under
snps,dwc3) are present.
Note two behavioral details on existing 0-based snps,dwc3 device trees:
- On controllers with port@0/port@1 nodes (around 174 in-tree nodes,
such as Qualcomm sc8280xp and Apple t600x boards), the sysfs
"removable" attribute changes from "removable" (previously matched
by accident via 0-based port@1 on logical port 1) to "unknown".
- A 0-based snps,dwc3 node defining only port@1 without port@0
(currently only rk3576-lckfb-tspi-3m.dts, which has hub@1 plus
ports/port@1 for its SuperSpeed Type-A port) cannot be distinguished
from a 1-based graph here; once raw-port graph lookup is used, its
SuperSpeed port's connect_type changes from "hardwired" to "not used"
(while "removable" remains "fixed").
This issue was flagged during a review by an AI coding agent
(Antigravity). The code change and commit message were drafted by the
agent and reviewed by me. The port0 != NULL ignore path is
compile-tested only (not exercised on MT8195 Cherry Dojo).
Fixes: 82e82130a78b ("usb: core: Set connect_type of ports based on DT node")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
---
drivers/usb/core/of.c | 49 ++++++++++++++++++++++++++++++-------------
1 file changed, 34 insertions(+), 15 deletions(-)
diff --git a/drivers/usb/core/of.c b/drivers/usb/core/of.c
index a6ee20d8774e..a040fae5710b 100644
--- a/drivers/usb/core/of.c
+++ b/drivers/usb/core/of.c
@@ -76,13 +76,8 @@ bool usb_of_has_combined_node(struct usb_device *udev)
}
EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
-static bool usb_of_has_devices_or_graph(const struct usb_device *hub)
+static bool usb_of_has_devices(const struct device_node *np)
{
- const struct device_node *np = hub->dev.of_node;
-
- if (of_graph_is_present(np))
- return true;
-
for_each_child_of_node_scoped(np, child)
if (of_property_present(child, "reg"))
return true;
@@ -90,38 +85,62 @@ static bool usb_of_has_devices_or_graph(const struct usb_device *hub)
return false;
}
+/*
+ * USB downstream port numbering starts at 1. An OF graph containing port@0
+ * or an unnumbered "port" node (such as legacy usb-role-switch or 0-based
+ * snps,dwc3 bindings) does not follow the USB port numbering scheme.
+ */
+static bool usb_of_has_graph(struct device_node *np)
+{
+ struct device_node *port0;
+
+ if (!of_graph_is_present(np))
+ return false;
+
+ port0 = of_graph_get_port_by_id(np, 0);
+ if (port0) {
+ of_node_put(port0);
+ return false;
+ }
+
+ return true;
+}
+
/**
* usb_of_get_connect_type() - get a USB hub's port connect_type
* @hub: hub to which port is for @port1
* @port1: one-based index of port
*
* Get the connect_type of @port1 based on the device node for @hub. If the
- * port is described in the OF graph, the connect_type is "hotplug". If the
- * @hub has a child device has with a 'reg' property equal to @port1 the
- * connect_type is "hard-wired". If there isn't an OF graph or child node at
- * all then the connect_type is "unknown". Otherwise, the port is considered
- * "unused" because it isn't described at all.
+ * port is described in a valid 1-based OF graph, the connect_type is
+ * "hotplug". OF graphs containing port@0 or an unnumbered "port" node are
+ * ignored. If the @hub has a child device with a 'reg' property equal to
+ * @port1 the connect_type is "hard-wired". If there isn't a valid 1-based OF
+ * graph or any child node with a 'reg' property then the connect_type is
+ * "unknown". Otherwise, the port is considered "unused" because it isn't
+ * described at all.
*
* Return: A connect_type for @port1 based on the device node for @hub.
*/
enum usb_port_connect_type usb_of_get_connect_type(struct usb_device *hub, int port1)
{
- struct device_node *np, *child, *ep, *remote_np;
+ struct device_node *np = hub->dev.of_node;
+ struct device_node *child, *ep, *remote_np;
enum usb_port_connect_type connect_type;
+ bool has_graph = usb_of_has_graph(np);
/* Only set connect_type if binding has ports/hardwired devices. */
- if (!usb_of_has_devices_or_graph(hub))
+ if (!has_graph && !usb_of_has_devices(np))
return USB_PORT_CONNECT_TYPE_UNKNOWN;
/* Assume port is unused if there's a graph or a child node. */
connect_type = USB_PORT_NOT_USED;
- np = hub->dev.of_node;
/*
* Hotplug ports are connected to an available remote node, e.g.
* usb-a-connector compatible node, in the OF graph.
*/
- if (of_graph_is_present(np)) {
+ if (has_graph) {
ep = of_graph_get_endpoint_by_regs(np, port1, -1);
if (ep) {
remote_np = of_graph_get_remote_port_parent(ep);
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 2/4] usb: of: Use raw port numbers for root hub OF lookups
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 ` 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
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Chen-Yu Tsai @ 2026-12-04 2:52 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: Chen-Yu Tsai, Alan Stern, linux-usb, devicetree,
Matthias Kaehlcke, Stephen Boyd, Rafał Miłecki,
Johan Hovold, Mathias Nyman, Neil Armstrong, Bjorn Andersson,
stable
Commit 7739376eb1ed ("USB: of: clean up device-node helper") reworked
usb_of_get_device_node() such that it takes the USB hub object and a
one-based port number. One could look at it and think that the port
number should be the logical port number found on the hub.
This is not the case on root hubs of xHCI host controllers. An xHCI
controller has two root hubs, a primary (HS) root hub and a secondary
(SS) root hub, sharing a single HCD device tree node, with the HS and
SS downstream ports split among them and either protocol block able to
come first in the controller's raw port numbering. Both downstream DT
child nodes (reg = <N>) and 1-based OF graph ports (port@N) are indexed
by the controller's raw port number rather than the logical port number
on a root hub.
Previously only usb_alloc_dev() resolved the raw port number with
usb_hcd_find_raw_port_number() before calling usb_of_get_device_node().
Neither usb_of_get_device_node() nor the OF graph lookup in
usb_of_get_connect_type() remapped root hub port numbers. For example,
on controllers where SS raw ports precede HS raw ports, an OF graph
lookup for HS port 1 would look for raw port 1 (an SS port); if only
the HS data lines are wired up, usb_of_get_connect_type() would
incorrectly return USB_PORT_NOT_USED instead of
USB_PORT_CONNECT_TYPE_HOT_PLUG.
Add a usb_of_find_raw_port() helper that remaps root hub port numbers
via usb_hcd_find_raw_port_number(), and use it in both
usb_of_get_device_node() (after an early NULL check on hub->dev.of_node
to skip the lookup on non-DT root hubs) and the OF graph lookup in
usb_of_get_connect_type(), so that all callers align with the stated
API contract:
- usb_alloc_dev() continues to match downstream USB devices as before.
- usb_of_get_connect_type() now uses raw port numbers for both OF graph
connections and hard-wired child nodes on root hubs.
- onboard_dev_create_pdevs() now looks up hard-wired child nodes on
root hubs using raw port numbers instead of the unmapped hub port
index, fixing lookups when the two differ (such as USB 2.0 ports
whose raw port number exceeds the root hub's maxchild).
- usbport_trig_port_observed() in ledtrig-usbport now matches each root
hub port against its corresponding raw port node rather than matching
port 1 of both HS and SS root hubs to reg = <1>.
Also tweak usb_of_get_device_node()'s and usb_of_get_connect_type()'s
kernel-doc comments to make it clear that hub port numbers are mapped
to raw port numbers.
The missing raw-port remapping for OF graph lookups in
usb_of_get_connect_type() was flagged during a review of the M.2 E-key
USB support series by an AI coding agent (Antigravity).
Fixes: 4f04c210d031 ("usb: core: read USB ports from DT in the usbport LED trigger driver")
Fixes: 82e82130a78b ("usb: core: Set connect_type of ports based on DT node")
Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
---
drivers/usb/core/of.c | 42 ++++++++++++++++++++++++++++++------------
drivers/usb/core/usb.c | 8 +-------
2 files changed, 31 insertions(+), 19 deletions(-)
diff --git a/drivers/usb/core/of.c b/drivers/usb/core/of.c
index a040fae5710b..47e5428fc8d4 100644
--- a/drivers/usb/core/of.c
+++ b/drivers/usb/core/of.c
@@ -9,15 +9,25 @@
#include <linux/of.h>
#include <linux/of_graph.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
#include <linux/usb/of.h>
+static int usb_of_find_raw_port(struct usb_device *hub, int port1)
+{
+ if (!hub->parent)
+ return usb_hcd_find_raw_port_number(bus_to_hcd(hub->bus), port1);
+
+ return port1;
+}
+
/**
* usb_of_get_device_node() - get a USB device node
* @hub: hub to which device is connected
- * @port1: one-based index of port
+ * @port1: one-based index of hub port
*
* Look up the node of a USB device given its parent hub device and one-based
- * port number.
+ * hub port number.
*
* Return: A pointer to the node with incremented refcount if found, or
* %NULL otherwise.
@@ -25,13 +35,19 @@
struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
{
struct device_node *node;
+ int raw_port;
u32 reg;
+ if (!hub->dev.of_node)
+ return NULL;
+
+ raw_port = usb_of_find_raw_port(hub, port1);
+
for_each_child_of_node(hub->dev.of_node, node) {
if (of_property_read_u32(node, "reg", ®))
continue;
- if (reg == port1)
+ if (reg == raw_port)
return node;
}
@@ -109,16 +125,16 @@ static bool usb_of_has_graph(struct device_node *np)
/**
* usb_of_get_connect_type() - get a USB hub's port connect_type
* @hub: hub to which port is for @port1
- * @port1: one-based index of port
+ * @port1: one-based index of hub port
*
* Get the connect_type of @port1 based on the device node for @hub. If the
- * port is described in a valid 1-based OF graph, the connect_type is
- * "hotplug". OF graphs containing port@0 or an unnumbered "port" node are
- * ignored. If the @hub has a child device with a 'reg' property equal to
- * @port1 the connect_type is "hard-wired". If there isn't a valid 1-based OF
- * graph or any child node with a 'reg' property then the connect_type is
- * "unknown". Otherwise, the port is considered "unused" because it isn't
- * described at all.
+ * port's raw port number is described in a valid 1-based OF graph, the
+ * connect_type is "hotplug". OF graphs containing port@0 or an unnumbered
+ * "port" node are ignored. If the @hub has a child device with a 'reg'
+ * property matching the port's raw port number, the connect_type is
+ * "hard-wired". If there isn't a valid 1-based OF graph or any child node
+ * with a 'reg' property then the connect_type is "unknown". Otherwise, the
+ * port is considered "unused" because it isn't described at all.
*
* Return: A connect_type for @port1 based on the device node for @hub.
*/
@@ -141,7 +157,9 @@ enum usb_port_connect_type usb_of_get_connect_type(struct usb_device *hub, int p
* usb-a-connector compatible node, in the OF graph.
*/
if (has_graph) {
- ep = of_graph_get_endpoint_by_regs(np, port1, -1);
+ int raw_port = usb_of_find_raw_port(hub, port1);
+
+ ep = of_graph_get_endpoint_by_regs(np, raw_port, -1);
if (ep) {
remote_np = of_graph_get_remote_port_parent(ep);
of_node_put(ep);
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 2559a180fdee..5f775130ebf6 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -646,7 +646,6 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
{
struct usb_device *dev;
struct usb_hcd *usb_hcd = bus_to_hcd(bus);
- unsigned raw_port = port1;
dev = kzalloc_obj(*dev);
if (!dev)
@@ -725,12 +724,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
dev->dev.parent = &parent->dev;
dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
- if (!parent->parent) {
- /* device under root hub's port */
- raw_port = usb_hcd_find_raw_port_number(usb_hcd,
- port1);
- }
- dev->dev.of_node = usb_of_get_device_node(parent, raw_port);
+ dev->dev.of_node = usb_of_get_device_node(parent, port1);
/* hub driver sets up TT records */
}
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 2/4] usb: of: Use raw port numbers for root hub OF lookups
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
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-04 3:10 UTC (permalink / raw)
To: Chen-Yu Tsai; +Cc: linux-usb, devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: of: UAF/NULL deref race via ledtrig-usbport and
xhci_mem_cleanup()
--
commit c57789cc567b11752bfc353db0a9b7f2d4d5b418
Author: Chen-Yu Tsai <wenst@chromium.org>
usb: of: Use raw port numbers for root hub OF lookups
This commit adds a usb_of_find_raw_port() helper to remap root hub port
numbers via usb_hcd_find_raw_port_number() and uses it in
usb_of_get_device_node() and usb_of_get_connect_type(), removing the
open-coded remapping from usb_alloc_dev().
> diff --git a/drivers/usb/core/of.c b/drivers/usb/core/of.c
> index a040fae5710be..47e5428fc8d4c 100644
> --- a/drivers/usb/core/of.c
> +++ b/drivers/usb/core/of.c
> @@ -9,15 +9,25 @@
>
> #include <linux/of.h>
> #include <linux/of_graph.h>
> +#include <linux/usb.h>
> +#include <linux/usb/hcd.h>
> #include <linux/usb/of.h>
>
> +static int usb_of_find_raw_port(struct usb_device *hub, int port1)
> +{
> + if (!hub->parent)
> + return usb_hcd_find_raw_port_number(bus_to_hcd(hub->bus), port1);
> +
> + return port1;
> +}
> +
> /**
> * usb_of_get_device_node() - get a USB device node
> * @hub: hub to which device is connected
> - * @port1: one-based index of port
> + * @port1: one-based index of hub port
> *
> * Look up the node of a USB device given its parent hub device and one-based
> - * port number.
> + * hub port number.
> *
> * Return: A pointer to the node with incremented refcount if found, or
> * %NULL otherwise.
> @@ -25,13 +35,19 @@
> struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
> {
> struct device_node *node;
> + int raw_port;
> u32 reg;
>
> + if (!hub->dev.of_node)
> + return NULL;
> +
> + raw_port = usb_of_find_raw_port(hub, port1);
[Severity: Medium]
Can usb_of_find_raw_port() trigger a use-after-free or NULL pointer
dereference on xhci->usb{2,3}_rhub.ports when usb_of_get_device_node()
races with usb_remove_hcd()?
Previously usb_of_get_device_node() only read hub->dev.of_node, which
stays valid until the last struct device reference is dropped. Calling
usb_hcd_find_raw_port_number() reaches into xHCI driver state that
xhci_mem_cleanup() frees and clears during HCD teardown while the root
hub device can still be pinned by a bus_for_each_dev() walk:
CPU0 CPU1
usbport_trig_activate()
usb_for_each_dev()
bus_for_each_dev()
usbport_trig_add_port()
kzalloc_obj(...)
usb_remove_hcd()
usb_disconnect(&rhdev)
...
klist_del(&dev->p->knode_bus)
usb_stop_hcd(hcd)
xhci_stop()
xhci_mem_cleanup()
kfree(xhci->usb2_rhub.ports)
kfree(xhci->hw_ports)
usbport_trig_port_observed()
usb_of_get_device_node()
usb_of_find_raw_port()
xhci_find_raw_port_number()
rhub->ports[port1 - 1]->hw_portnum
<-- use-after-free
bus_for_each_dev() holds only a struct device reference without
device_lock() or xhci->mutex, and bus_remove_device() uses non-waiting
klist_del(), so usb_remove_hcd() can proceed to xhci_mem_cleanup()
before usbport_trig_port_observed() runs. Since
xhci_find_raw_port_number() dereferences rhub->ports[port1 - 1] without
locking or a NULL check, this causes a use-after-free read or a NULL
pointer dereference once xhci_mem_cleanup() clears the pointers.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204025243.807657-1-wenst@chromium.org?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/4] usb: misc: onboard_usb_dev: Create pdevs for secondary root hubs
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 2:52 ` Chen-Yu Tsai
2026-12-04 2:52 ` [PATCH 4/4] usb: of: Check compatible string of device node Chen-Yu Tsai
2026-12-04 3:10 ` [PATCH 0/4] usb: of: Fix root-hub raw port lookups and graph handling sashiko-bot
4 siblings, 0 replies; 7+ messages in thread
From: Chen-Yu Tsai @ 2026-12-04 2:52 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: Chen-Yu Tsai, Alan Stern, linux-usb, devicetree,
Matthias Kaehlcke, Stephen Boyd, Rafał Miłecki,
Johan Hovold, Mathias Nyman, Neil Armstrong, Bjorn Andersson,
stable
onboard_dev_create_pdevs() previously skipped root hubs of secondary
(USB 3.x) HCDs because both the primary and secondary root hubs share
the same HCD device tree node, and usb_of_get_device_node() did not map
root hub port numbers to raw port numbers. Scanning both root hubs
would have queried the same 1..maxchild reg values twice; on the second
pass, of_platform_device_create() would reject the already-populated
(OF_POPULATED) device node and log a "failed to create platform device"
error.
Now that usb_of_get_device_node() resolves root hub port numbers via
usb_hcd_find_raw_port_number(), primary and secondary root hubs on xHCI
controllers map to disjoint raw port ranges. Returning early for
secondary root hubs means onboard_dev_create_pdevs() now only visits
the primary (USB 2.0) root hub's ports and never creates platform
devices for standalone USB 3.x onboard devices without a peer-hub
(such as USB 3.0 Ethernet or SATA bridges) attached directly to a
secondary root hub. On SuperSpeed-first xHCI controllers where raw
port 1 belongs to the secondary root hub, this would also regress any
such device at reg = <1> that previously matched the unmapped primary
root hub index.
No in-tree boards are broken by this change: all properly described
in-tree USB 3.x onboard devices attached directly to xHCI root hubs are
dual-speed hubs with a "peer-hub" property (whose platform device is
created via the USB 2.0 peer on the primary root hub), and the
USB 3.0-only non-hub devices supported by this driver have no in-tree
device tree users yet. The one outlier, meson-libretech-cottonwood.dtsi,
describes a GL3510 hub with only its USB 3.0 compatible ("usb5e3,626")
and no "peer-hub" property at reg = <1> (a USB 2.0 port on the Amlogic
G12B/SM1 DWC3 controller), so its platform device is still created via
the primary root hub.
Stop skipping secondary root hubs in onboard_dev_create_pdevs() and
update its kernel-doc comment to match. Dual-speed onboard hubs with a
"peer-hub" property are already deduplicated by the
!usb_hcd_is_primary_hcd(hcd) check inside the loop, and each root hub
tracks and destroys its own platform devices in its hub->onboard_devs
list.
This issue was flagged during a review by an AI coding agent
(Antigravity). The code change and commit message were drafted by the
agent and reviewed by me. Runtime-tested on MT8195 Cherry Dojo with a
dual-speed onboard hub (verifying no duplicate platform device is
created on the secondary root hub); the standalone USB 3.x device
(without peer-hub) path is compile-tested only.
Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
---
drivers/usb/misc/onboard_usb_dev_pdevs.c | 26 ++++++++----------------
1 file changed, 9 insertions(+), 17 deletions(-)
diff --git a/drivers/usb/misc/onboard_usb_dev_pdevs.c b/drivers/usb/misc/onboard_usb_dev_pdevs.c
index cdb6949e18ec..5cbc8b632097 100644
--- a/drivers/usb/misc/onboard_usb_dev_pdevs.c
+++ b/drivers/usb/misc/onboard_usb_dev_pdevs.c
@@ -48,21 +48,16 @@ static bool of_is_onboard_usb_dev(struct device_node *np)
*
* Root hubs don't have dedicated device tree nodes, but use the node of their
* HCD. The primary and secondary HCD are usually represented by a single DT
- * node. That means the root hubs of the primary and secondary HCD share the
- * same device tree node (the HCD node). As a result this function can be called
- * twice with the same DT node for root hubs. We only want to create a single
- * platform device for each physical onboard device, hence for root hubs the
- * loop is only executed for the root hub of the primary HCD. Since the function
- * scans through all child nodes it still creates pdevs for onboard devices
- * connected to the root hub of the secondary HCD if needed.
+ * node, so their root hubs share the same device tree node (the HCD node).
+ * Because usb_of_get_device_node() maps root hub port numbers to raw port
+ * numbers, each root hub only visits the child nodes connected to its own
+ * ports and creates platform devices for onboard devices attached to them.
*
- * Further there must be only one platform device for onboard hubs with a peer
- * hub (the hub is a single physical device). To achieve this two measures are
- * taken: pdevs for onboard hubs with a peer are only created when the function
- * is called on behalf of the parent hub that is connected to the primary HCD
- * (directly or through other hubs). For onboard hubs connected to root hubs
- * the function processes the nodes of both peers. A platform device is only
- * created if the peer hub doesn't have one already.
+ * Further, there must be only one platform device for onboard hubs with a peer
+ * hub (the hub is a single physical device). To achieve this, pdevs for onboard
+ * hubs with a peer are only created when the function is called on behalf of a
+ * parent hub that is connected to the primary HCD (directly or through other
+ * hubs), and only if the peer hub doesn't have one already.
*/
void onboard_dev_create_pdevs(struct usb_device *parent_hub, struct list_head *pdev_list)
{
@@ -75,9 +70,6 @@ void onboard_dev_create_pdevs(struct usb_device *parent_hub, struct list_head *p
if (!parent_hub->dev.of_node)
return;
- if (!parent_hub->parent && !usb_hcd_is_primary_hcd(hcd))
- return;
-
for (i = 1; i <= parent_hub->maxchild; i++) {
np = usb_of_get_device_node(parent_hub, i);
if (!np)
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 4/4] usb: of: Check compatible string of device node
2026-12-04 2:52 [PATCH 0/4] usb: of: Fix root-hub raw port lookups and graph handling Chen-Yu Tsai
` (2 preceding siblings ...)
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
2026-12-04 3:10 ` [PATCH 0/4] usb: of: Fix root-hub raw port lookups and graph handling sashiko-bot
4 siblings, 0 replies; 7+ messages in thread
From: Chen-Yu Tsai @ 2026-12-04 2:52 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: Chen-Yu Tsai, Alan Stern, linux-usb, devicetree,
Matthias Kaehlcke, Stephen Boyd, Rafał Miłecki,
Johan Hovold, Mathias Nyman, Neil Armstrong, Bjorn Andersson
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 0/4] usb: of: Fix root-hub raw port lookups and graph handling
2026-12-04 2:52 [PATCH 0/4] usb: of: Fix root-hub raw port lookups and graph handling Chen-Yu Tsai
` (3 preceding siblings ...)
2026-12-04 2:52 ` [PATCH 4/4] usb: of: Check compatible string of device node Chen-Yu Tsai
@ 2026-12-04 3:10 ` sashiko-bot
4 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-04 3:10 UTC (permalink / raw)
To: Chen-Yu Tsai; +Cc: linux-usb
Sashiko has finished reviewing this 4-patch series:
- Patches 1, 3, and 4: no issues found.
- Patch 2 ("usb: of: Use raw port numbers for root hub OF lookups"): 1
potential issue (1 Medium)
A detailed review has been sent as a separate reply to patch 2.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204025243.807657-1-wenst@chromium.org
^ permalink raw reply [flat|nested] 7+ messages in thread