Skip to content

fix(usb): avoid gadget bind race with USB HAL - #19

Draft
rodrig20 wants to merge 1 commit into
mainfrom
fix/usb-hal-bind-race
Draft

rodrig20 wants to merge 1 commit into
mainfrom
fix/usb-hal-bind-race

Conversation

@rodrig20

@rodrig20 rodrig20 commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Refs #17

What changed

  • The gadget waits for init to finish the USB mode switch before writing to ConfigFS. Previously the bind could happen while adbd still held the controller, which caused EBUSY or a reset right after.
  • The UDC is read from the bound gadget and sys.usb.controller first, with /sys/class/udc only as a fallback.
  • mass_storage is linked under a free name instead of assuming function.0, which adb already occupies when USB debugging is on. Existing adb links are left alone.
  • On disable, the previous sys.usb.config is restored instead of forcing mtp,adb.
  • Root diagnostics include usbcfg, usbstate and ffs.

Why

With USB debugging on, adbd holds the controller and init runs its sys.usb.config=none handler asynchronously. The app used to start setting up the gadget before that finished, so the bind landed on a busy or soon to be reset controller. This change waits for the HAL to settle and stops relying on the first function slot being free.

@StanTody can you try the debug APK from this PR and tell me if it fixes the problem you had with USB debugging on?

@StanTody

StanTody commented Dec 2, 2026

Copy link
Copy Markdown

@rodrig20 , unfortunately, there is no success - at least on my Xiaomi 14 with custom ROM Xiaomi.Eu Android 15 and Zealous Thunder kernel.

With USB debugging turned off there is flickering of Windows Explorer on "Gadget On", showing Xiaomi 14, but no disk device.

With USB debugging turned ON, turning on the gadget makes no effect at all.

As additional information: see "USB Arsenal", it works well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants