libusbgx: exit with failure code when no UDC is detected

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>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
This commit is contained in:
Ernest Van Hoecke 2025-07-02 16:32:09 +02:00 committed by Khem Raj
parent b7e233f84a
commit 88c7b3ee93
No known key found for this signature in database
GPG Key ID: BB053355919D3314

View File

@ -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