Linux – What Happens to /dev/bus/usb After 1001 Connections

alpine-linuxlsusbusbusb deviceusb-drive

/dev/bus/usb/*/* lists all the usb devices connected on Alpine Linux. For example a phone could be /dev/bus/usb/001/009. When that phone is reconnected it will be /dev/bus/usb/001/010, i.e. it increments.

My question is what happens after 1001 connections?

/dev/bus/usb/001/009
                 010
                 ...
                 100
                 ...
                 500
                 ...
                 999
                 ???

Would the ??? go to 1000? Would it create a new folder say 003 under /dev/bus/usb/?

This is for manipulating the output of usb-devices and lsusb. Getting the Vendor and ProdID is not unique in my case.

I'd rather not plug in/out a usb cable 1000 times!

Best Answer

If I read correctly https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/tree/drivers/usb/core/hub.c#n2030 (code for a function called choose_devnum), it may wrap far earlier than that, at value 127 in fact, and then going back to 1:

    /* Try to allocate the next devnum beginning at
     * bus->devnum_next. */
    devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
                    bus->devnum_next);
    if (devnum >= 128)
        devnum = find_next_zero_bit(bus->devmap.devicemap,
                        128, 1);
    bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);

And later on the bus->devnum property is really set only if devnum is less than 128.

Related Question