Skip to content

fix(usbdevice): use glob for android_usb state instead of hardcoded android0#4

Open
frankurcrazy wants to merge 2 commits intoLubanCat:debian12from
frankurcrazy:fix/android-usb-hardcoded-index
Open

fix(usbdevice): use glob for android_usb state instead of hardcoded android0#4
frankurcrazy wants to merge 2 commits intoLubanCat:debian12from
frankurcrazy:fix/android-usb-hardcoded-index

Conversation

@frankurcrazy
Copy link

Summary

  • Fix hardcoded /sys/class/android_usb/android0/state to use glob pattern

Problem

The kernel's gadget_index counter in drivers/usb/gadget/configfs.c is monotonic — if the configfs gadget directory is removed and recreated, the android_usb device becomes android1, android2, etc. The hardcoded android0 path then fails silently, leaving USB_STATE empty.

Fix

# Before
USB_STATE=$(cat /sys/class/android_usb/android0/state)

# After
USB_STATE=$(cat /sys/class/android_usb/android*/state 2>/dev/null | head -1)

Fixes #3

The hid_prepare() function uses echo -ne to write the HID report
descriptor binary data. On systems where /bin/sh is dash (not bash),
echo does not support -ne flags, causing the descriptor to be written
as literal ASCII text (e.g., "-ne \x05\x01\x09\x06...") instead of
binary bytes.

This breaks HID keyboard emulation entirely — the USB host cannot
parse the descriptor, sends infinite bus resets, and writes to
/dev/hidg0 block indefinitely.

Replace echo -ne with printf using octal escapes, which work
correctly across all POSIX shells (bash, dash, sh).

Fixes: LubanCat#1
…ndroid0

The kernel uses a monotonic counter (gadget_index++) to name
android_usb devices. If the configfs gadget directory is removed
and recreated, the device becomes android1, android2, etc.

The hardcoded path /sys/class/android_usb/android0/state then
fails silently, leaving USB_STATE empty and causing usbdevice
update to misbehave.

Use a glob pattern (android*/state) to match whichever androidN
device exists.

Fixes: LubanCat#3
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.

usbdevice: hardcoded android0 breaks after gadget directory recreation

1 participant