Seeking Assistance: Implementing USB Device Suspend/Resume in User Space Driver

Alan Stern stern at rowland.harvard.edu
Tue May 7 13:51:18 EDT 2024


On Tue, May 07, 2024 at 10:08:13PM +0530, Muni Sekhar wrote:
> Now, I can verify USB device auto-suspend and auto-resume through the
> kernel log.
> 
> In my test system, I found three buses "Bus 001, 002, and 003" using
> the lsusb command.
> 
> The USB device I want to test is located on "Bus 002".
> 
> 
> # lsusb | grep "Bus 002" | wc
>       4      37     224
> 
> As per the output of the above command, there are 4 USB devices on
> "Bus 002" in my test system.
> 
> 
> # ls -l /sys/bus/usb/devices/2*
> lrwxrwxrwx 1 root root 0 Feb 14 14:48 /sys/bus/usb/devices/2-0:1.0 ->
> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-0:1.0
> lrwxrwxrwx 1 root root 0 May  7 13:16 /sys/bus/usb/devices/2-1 ->
> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-1
> lrwxrwxrwx 1 root root 0 May  7 13:16 /sys/bus/usb/devices/2-1.1 ->
> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.1
> lrwxrwxrwx 1 root root 0 May  7 13:16 /sys/bus/usb/devices/2-1:1.0 ->
> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.0
> lrwxrwxrwx 1 root root 0 May  7 13:16 /sys/bus/usb/devices/2-1.1:1.0
> -> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.1/2-1.1:1.0
> lrwxrwxrwx 1 root root 0 May  7 13:16 /sys/bus/usb/devices/2-1.7 ->
> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.7
> lrwxrwxrwx 1 root root 0 May  7 13:16 /sys/bus/usb/devices/2-1.7:1.0
> -> ../../../devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.7/2-1.7:1.0
> 
> # ls -l /sys/bus/usb/devices/2* | wc
>       7      77     892
> 
> 
> The above command shows seven nodes in /sys/bus/usb/devices/2* directory.
> 
> The wakeup, control, and autosuspend_delay_ms attribute files are
> controlled via /sys/bus/usb/devices/.../power/, where "..." represents
> the device's ID.
> 
> I identified my test device in /sys/bus/usb/devices/.../power/ by
> iterating through all the directories, reading the idVendor attribute
> file, and then manipulating the power management attribute files.
> 
> Is there a way to manually map /sys/bus/usb/devices/.../power/ from
> the lsusb output? Can this mapping be done from a user space program
> as well? Do you have any reference examples for this? If so, could you
> share their GitHub location?

libusb already can do this for you.

However, if you want to do it by hand then you can match up devices 
based on the devnum attribute file in the devices sysfs directory.  For 
example, if you were trying to identify which device corresonds to the 
lsusb entry for device 003 on bus 002, you should look through all the 
directories in /sys/bus/usb/devices/2-* and see which one has "3" in its 
devnum file.

There's another way of doing it more directly, using the output from 
"lsusb -t".  For example, on my system:

$ lsusb -t
/:  Bus 001.Port 001: Dev 001, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 002.Port 001: Dev 001, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 001: Dev 002, If 0, Class=Hub, Driver=hub/8p, 480M
/:  Bus 003.Port 001: Dev 001, Class=root_hub, Driver=ehci-pci/4p, 480M
/:  Bus 004.Port 001: Dev 001, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 001: Dev 002, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 002: Dev 003, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
/:  Bus 005.Port 001: Dev 001, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 006.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/15p, 480M
/:  Bus 007.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/6p, 5000M

Let's say we want to locate the Human Interface Device in sysfs.  
According to the listing, it is on port 002 below port 001 below bus 
004.  You reverse the numbers, put a '-' after the bus number, 
and separate the port numbers with a '.', which gives "4-1.2":

$ cat /sys/bus/usb/devices/4-1.2/product
Microsoft 5-Button Mouse with IntelliEye(TM)

Alan Stern



More information about the Kernelnewbies mailing list