All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Eichenberger <eichest@gmail.com>
To: francesco.dolcini@toradex.com, trini@konsulko.com,
	ilias.apalodimas@linaro.org, sbabic@nabladev.com,
	festevam@gmail.com, uboot-imx@nxp.com,
	marek.vasut+usb@mailbox.org, lukma@denx.de,
	mkorpershoek@kernel.org, antoine.gouby@toradex.com,
	alice.guo@nxp.com, stefan.eichenberger@toradex.com,
	sjg@chromium.org, emanuele.ghidoli@toradex.com,
	franz.schnyder@toradex.com, pbrobinson@gmail.com,
	quentin.schulz@cherry.de, jbx6244@gmail.com, ye.li@nxp.com,
	xu.yang_2@nxp.com, bmeng.cn@gmail.com, xypron.glpk@gmx.de,
	hayashi.kunihiko@socionext.com, sean.anderson@linux.dev
Cc: u-boot@lists.u-boot-project.org
Subject: [PATCH v2 4/7] usb: skip host-only devices when picking the ehci gadget controller
Date: Mon, 14 Sep 2026 09:42:46 +0200	[thread overview]
Message-ID: <20260914074314.12522-5-eichest@gmail.com> (raw)
In-Reply-To: <20260914074314.12522-1-eichest@gmail.com>

From: Stefan Eichenberger <stefan.eichenberger@toradex.com>

The i.MX95 SoC has two USB controllers, a DWC3 controller and a ChipIdea
controller. However, U-Boot might select a controller that is marked as
host only in the device tree when attempting to set up a USB gadget,
leading to failures in gadget mode.

Iterate over usb devices and skipping any that are marked host-only when
selecting the controller for gadget mode. This ensures that a controller
that is marked as device or otg is used for USB gadget functionality.

Verified on Verdin iMX95 hardware: "ums 0 mmc 0" now correctly flips
the ChipIdea OTG controller into device mode and enumerates on the
host PC.

Fixes: 821ca608d816 ("usb: Use the first available device for ehci_gadget")
Signed-off-by: Stefan Eichenberger <stefan.eichenberger@toradex.com>
---
 drivers/usb/host/usb-uclass.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/host/usb-uclass.c b/drivers/usb/host/usb-uclass.c
index 1c74d6fd39a..b9e9ef7e86a 100644
--- a/drivers/usb/host/usb-uclass.c
+++ b/drivers/usb/host/usb-uclass.c
@@ -18,6 +18,7 @@
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 #include <dm/uclass-internal.h>
+#include <linux/usb/otg.h>
 #include <time.h>
 
 static bool asynch_allowed;
@@ -484,10 +485,23 @@ int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
 	struct udevice *dev;
 	int ret;
 
-	/* Find the old device and remove it */
+	/*
+	 * Find the first UCLASS_USB device that isn't host-only. SoCs can
+	 * have more than one UCLASS_USB controller (e.g. i.MX95, where a
+	 * host-only DWC3 port and the ChipIdea OTG port both register under
+	 * UCLASS_USB); taking whichever device happens to be first could
+	 * pick the host-only controller instead of the OTG-capable one this
+	 * is actually meant to flip into device mode.
+	 */
 	ret = uclass_find_first_device(UCLASS_USB, &dev);
 	if (ret)
 		return ret;
+	while (dev && usb_get_dr_mode(dev_ofnode(dev)) == USB_DR_MODE_HOST)
+		uclass_find_next_device(&dev);
+	if (!dev)
+		return -ENODEV;
+
+	/* Remove the old device */
 	ret = device_remove(dev, DM_REMOVE_NORMAL);
 	if (ret)
 		return ret;
@@ -507,10 +521,15 @@ int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
 	struct udevice *dev;
 	int ret;
 
-	/* Find the old device and remove it */
+	/* Find the same non-host-only device usb_setup_ehci_gadget() used */
 	ret = uclass_find_first_device(UCLASS_USB, &dev);
 	if (ret)
 		return ret;
+	while (dev && usb_get_dr_mode(dev_ofnode(dev)) == USB_DR_MODE_HOST)
+		uclass_find_next_device(&dev);
+	if (!dev)
+		return -ENODEV;
+
 	ret = device_remove(dev, DM_REMOVE_NORMAL);
 	if (ret)
 		return ret;
-- 
2.55.0


  parent reply	other threads:[~2026-09-14 14:17 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14  7:42 [PATCH v2 0/7] usb: fix DWC3/gadget issues and enable USB on Toradex i.MX95 boards Stefan Eichenberger
2026-09-14  7:42 ` [PATCH v2 1/7] usb: ci_udc: add driver model support Stefan Eichenberger
2026-09-14  7:42 ` [PATCH v2 2/7] usb: ehci-mx6: bind the ChipIdea gadget driver Stefan Eichenberger
2026-09-14  7:42 ` [PATCH v2 3/7] usb: dwc3-generic: fix ref clock lookup indexing wrong clk_bulk array Stefan Eichenberger
2026-09-14 19:26   ` Marek Vasut
2026-09-15  7:44     ` Stefan Eichenberger
2026-09-15 14:22       ` Marek Vasut
2026-09-14  7:42 ` Stefan Eichenberger [this message]
2026-09-14 19:31   ` [PATCH v2 4/7] usb: skip host-only devices when picking the ehci gadget controller Marek Vasut
2026-09-15  7:57     ` Stefan Eichenberger
2026-09-15 14:24       ` Marek Vasut
2026-09-16 12:47         ` Stefan Eichenberger
2026-09-16 14:29           ` Marek Vasut
2026-09-14  7:42 ` [PATCH v2 5/7] configs: verdin-imx95: enable CI_UDC usb gadget driver Stefan Eichenberger
2026-09-14  7:42 ` [PATCH v2 6/7] toradex: verdin-imx95: enable DWC3 USB controller support Stefan Eichenberger
2026-09-14  7:42 ` [PATCH v2 7/7] toradex: smarc-imx95: " Stefan Eichenberger

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=20260914074314.12522-5-eichest@gmail.com \
    --to=eichest@gmail.com \
    --cc=alice.guo@nxp.com \
    --cc=antoine.gouby@toradex.com \
    --cc=bmeng.cn@gmail.com \
    --cc=emanuele.ghidoli@toradex.com \
    --cc=festevam@gmail.com \
    --cc=francesco.dolcini@toradex.com \
    --cc=franz.schnyder@toradex.com \
    --cc=hayashi.kunihiko@socionext.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jbx6244@gmail.com \
    --cc=lukma@denx.de \
    --cc=marek.vasut+usb@mailbox.org \
    --cc=mkorpershoek@kernel.org \
    --cc=pbrobinson@gmail.com \
    --cc=quentin.schulz@cherry.de \
    --cc=sbabic@nabladev.com \
    --cc=sean.anderson@linux.dev \
    --cc=sjg@chromium.org \
    --cc=stefan.eichenberger@toradex.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.u-boot-project.org \
    --cc=uboot-imx@nxp.com \
    --cc=xu.yang_2@nxp.com \
    --cc=xypron.glpk@gmx.de \
    --cc=ye.li@nxp.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.