Skip to content

usbdevice: hardcoded android0 breaks after gadget directory recreation #8

@frankurcrazy

Description

@frankurcrazy

Bug Description

/usr/bin/usbdevice hardcodes android0 when reading the USB gadget state at line 519:

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

The kernel uses a monotonic counter (gadget_index++) in drivers/usb/gadget/configfs.c (android_device_create()) to name android_usb devices. If the configfs gadget directory (/sys/kernel/config/usb_gadget/rockchip) is removed with rmdir and recreated, the new device is named android1 instead of android0, because the counter is never decremented.

This causes usbdevice update to silently fail — USB_STATE becomes empty, and any logic that depends on the gadget state (e.g., UMS mount/unmount in ums_start()) misbehaves.

Root Cause

In drivers/usb/gadget/configfs.c:

static int gadget_index;  // never decremented

static int android_device_create(struct gadget_info *gi)
{
    gi->dev = device_create(android_class, NULL,
            MKDEV(0, 0), NULL, "android%d", gadget_index++);
    // ...
}

Source: https://github.com/LubanCat/kernel/blob/a5b4e9f62f94f1865ab8c3d86be7d9168f3da406/drivers/usb/gadget/configfs.c#L1776

Fix

Use a glob instead of hardcoding android0:

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

This works regardless of which androidN device exists.

Environment

  • Board: RK3588 (LubanCat)
  • Kernel: 5.10.160

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions