diff mbox series

[meta-oe] android-tools-conf-configfs: replace fixed sleep with UDC wait loop

Message ID 20260416072305.1447652-1-viswanath.kraleti@oss.qualcomm.com
State Under Review
Headers show
Series [meta-oe] android-tools-conf-configfs: replace fixed sleep with UDC wait loop | expand

Commit Message

Viswanath Kraleti April 16, 2026, 7:23 a.m. UTC
The android-gadget-start script previously relied on a fixed `sleep 10`
delay for configuring the USB gadget UDC. This approach can either delay
boot unnecessarily or still race with UDC or configfs initialization on
slower systems.

Replace the fixed sleep with a limited time polling loop that waits for
both configfs and a valid UDC before configuring the USB ADB gadget.

This makes USB gadget initialization more deterministic across platforms
without relying on arbitrary delays.

Signed-off-by: Viswanath Kraleti <viswanath.kraleti@oss.qualcomm.com>
---
 .../android-gadget-start                      | 35 +++++++++++++++++--
 1 file changed, 32 insertions(+), 3 deletions(-)

Comments

Dmitry Baryshkov April 16, 2026, 11:03 a.m. UTC | #1
On Thu, Apr 16, 2026 at 12:53:05PM +0530, Viswanath Kraleti wrote:
> The android-gadget-start script previously relied on a fixed `sleep 10`
> delay for configuring the USB gadget UDC. This approach can either delay
> boot unnecessarily or still race with UDC or configfs initialization on
> slower systems.
> 
> Replace the fixed sleep with a limited time polling loop that waits for
> both configfs and a valid UDC before configuring the USB ADB gadget.
> 
> This makes USB gadget initialization more deterministic across platforms
> without relying on arbitrary delays.
> 
> Signed-off-by: Viswanath Kraleti <viswanath.kraleti@oss.qualcomm.com>
> ---
>  .../android-gadget-start                      | 35 +++++++++++++++++--
>  1 file changed, 32 insertions(+), 3 deletions(-)

CC'ing Devendra, who changed it from 3 to 10 seconds.

What was the issue? Was it UDC device not being registered or was it
something else?

> 
> diff --git a/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start b/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start
> index 76b5e29624..ecc9a0ff84 100644
> --- a/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start
> +++ b/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start
> @@ -2,8 +2,37 @@
>  
>  set -e
>  
> -sleep 10
> +n=0
> +while true; do
> +    # Pick first available UDC as default. Set UDC_NAME in env to override.
> +    udcname="${UDC_NAME:-$(ls -1 /sys/class/udc 2>/dev/null | head -n 1)}"
>  
> -ls /sys/class/udc/ | head -n 1 | xargs echo -n > /sys/kernel/config/usb_gadget/adb/UDC
> +    if [ $n -eq 30 ]; then
> +        echo "Timeout waiting for UDC configuration"
> +        exit 1
> +    fi

The more I look at it, the more it seems that whole ADB support should
be rewritten to use udev and generators to start adb only once the UDC
gets registered in the system (and start it for every UDC that is
present).

> +
> +    if [ ! -e /sys/kernel/config/usb_gadget/adb/UDC ]; then

Why do you need this?

> +        echo "Waiting for configfs usb_gadget to be initialized... ($n/30)"
> +        sleep 1
> +        n=$((n + 1))
> +        continue
> +    fi
> +    configfs_udc="$(cat /sys/kernel/config/usb_gadget/adb/UDC 2>/dev/null || true)"
> +
> +    if [ -n "$udcname" ] && [ "$configfs_udc" = "$udcname" ]; then
> +        echo "UDC $udcname successfully configured for USB ADB Gadget"
> +        break
> +    fi
> +
> +    if [ -z "$udcname" ]; then
> +        echo "Waiting for UDC to appear... ($n/30)"
> +    else
> +        echo "Setting UDC $udcname for USB ADB Gadget usage"
> +        printf '%s' "$udcname" > /sys/kernel/config/usb_gadget/adb/UDC
> +    fi
> +
> +    sleep 1
> +    n=$((n + 1))
> +done
>  
> -echo "Setting UDC $(ls /sys/class/udc/ | head -n 1) for USB ADB Gadget usage"
>
diff mbox series

Patch

diff --git a/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start b/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start
index 76b5e29624..ecc9a0ff84 100644
--- a/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start
+++ b/meta-oe/recipes-devtools/android-tools/android-tools-conf-configfs/android-gadget-start
@@ -2,8 +2,37 @@ 
 
 set -e
 
-sleep 10
+n=0
+while true; do
+    # Pick first available UDC as default. Set UDC_NAME in env to override.
+    udcname="${UDC_NAME:-$(ls -1 /sys/class/udc 2>/dev/null | head -n 1)}"
 
-ls /sys/class/udc/ | head -n 1 | xargs echo -n > /sys/kernel/config/usb_gadget/adb/UDC
+    if [ $n -eq 30 ]; then
+        echo "Timeout waiting for UDC configuration"
+        exit 1
+    fi
+
+    if [ ! -e /sys/kernel/config/usb_gadget/adb/UDC ]; then
+        echo "Waiting for configfs usb_gadget to be initialized... ($n/30)"
+        sleep 1
+        n=$((n + 1))
+        continue
+    fi
+    configfs_udc="$(cat /sys/kernel/config/usb_gadget/adb/UDC 2>/dev/null || true)"
+
+    if [ -n "$udcname" ] && [ "$configfs_udc" = "$udcname" ]; then
+        echo "UDC $udcname successfully configured for USB ADB Gadget"
+        break
+    fi
+
+    if [ -z "$udcname" ]; then
+        echo "Waiting for UDC to appear... ($n/30)"
+    else
+        echo "Setting UDC $udcname for USB ADB Gadget usage"
+        printf '%s' "$udcname" > /sys/kernel/config/usb_gadget/adb/UDC
+    fi
+
+    sleep 1
+    n=$((n + 1))
+done
 
-echo "Setting UDC $(ls /sys/class/udc/ | head -n 1) for USB ADB Gadget usage"