Linux USB
 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 1/4] usb: of: Ignore 0-based or unnumbered OF graphs in usb_of_get_connect_type()
Date: Fri,  4 Dec 2026 10:52:39 +0800	[thread overview]
Message-ID: <20261204025243.807657-2-wenst@chromium.org> (raw)
In-Reply-To: <20261204025243.807657-1-wenst@chromium.org>

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


  reply	other threads:[~2026-12-04  2:53 UTC|newest]

Thread overview: 5+ 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 ` Chen-Yu Tsai [this message]
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 ` [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-2-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