Devicetree
 help / color / mirror / Atom feed
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>,
	stable@vger.kernel.org
Subject: [PATCH 2/4] usb: of: Use raw port numbers for root hub OF lookups
Date: Fri,  4 Dec 2026 10:52:40 +0800	[thread overview]
Message-ID: <20261204025243.807657-3-wenst@chromium.org> (raw)
In-Reply-To: <20261204025243.807657-1-wenst@chromium.org>

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", &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


  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 ` Chen-Yu Tsai [this message]
2026-12-04  3:10   ` [PATCH 2/4] usb: of: Use raw port numbers for root hub OF lookups 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 ` [PATCH 4/4] usb: of: Check compatible string of device node Chen-Yu Tsai

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-3-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=stable@vger.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