diff mbox series

[meta-oe] libusbgx: exit with failure code when no UDC is detected

Message ID 20250702143209.3345301-1-ernest.vanhoecke@toradex.com
State Under Review
Headers show
Series [meta-oe] libusbgx: exit with failure code when no UDC is detected | expand

Commit Message

Ernest Van Hoecke July 2, 2025, 2:32 p.m. UTC
The systemd target `usb-gadget.target` is triggered by udev when a UDC
first comes up. It can happen that by the time gadget-start runs, this
UDC has been removed from the system again.

Have the gadget-start script exit with status 1 when `ls /sys/class/udc`
returns nothing.

Causing a service failure when no UDC is detected and no default was
given, allows the service to be restarted by a udev rule calling the
service (and not the target since those are not reentrant) directly. On
its own this patch will not do much.

For example, we saw such a situation using the DWC3 USB controller and
usb-conn-gpio kernel modules as loadables. By the time of the DWC3 init,
udev was active, and during init DWC3 started the USB OTG port in device
mode. If a pen drive was plugged in at boot, it would quickly switch to
host mode right after initialisation, emitting another udev event for
the removal of the UDC. The systemd target as thus reached, but by the
time gadget-start ran, the UDC was gone.

  dwc3 init       usb-conn-gpio role switch
      │                       │
      ▼                       ▼
udev: add UDC─┐         udev: del UDC─────►/sys/class/udc empty
              │                                         │
              │                                         x
              │                                         │
              │                                         ▼
              └────────────►usb-gadget.target─────►gadget-start

Signed-off-by: Ernest Van Hoecke <ernest.vanhoecke@toradex.com>
---
 meta-oe/recipes-support/libusbgx/libusbgx/gadget-start | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/meta-oe/recipes-support/libusbgx/libusbgx/gadget-start b/meta-oe/recipes-support/libusbgx/libusbgx/gadget-start
index e80cb2c34068..4ef679888ec4 100755
--- a/meta-oe/recipes-support/libusbgx/libusbgx/gadget-start
+++ b/meta-oe/recipes-support/libusbgx/libusbgx/gadget-start
@@ -15,6 +15,10 @@  for i in $ENABLED_SCHEMAS; do
     if [ -n "${configured_udc}" ] && [ -e "/sys/class/udc/${configured_udc}" ]; then
         echo ${configured_udc} > /sys/kernel/config/usb_gadget/"$i"/UDC
     else
-        ls /sys/class/udc/ > /sys/kernel/config/usb_gadget/"$i"/UDC
+        detected_udc=$(ls /sys/class/udc/)
+        if [ -z "${detected_udc}" ]; then
+            exit 1
+        fi
+        echo "${detected_udc}" > /sys/kernel/config/usb_gadget/"$i"/UDC
     fi
 done