clinuxusblibusblibusb-1.0

How to identyfy USB HUB(root) and devices(child devices) conected to HUB(root device) in linux?


I want to write an application to read data(iSerial info) from connected USB Hub to linux as well as connected other devices to that USB hub.

-> I want to first detect whether USB Hub Connected or Not to linux which I can identify using idVendor:idProduct

->if its connected I want to read its info then have to iterate trough all USB devices connected to that USB Hub and have to read that devices info too

I am using this USB Hub: https://www.cablematters.com/pc-873-126-usb-c-multiport-adapter-with-4k-hdmi-2x-usb-30-gigabit-ethernet-and-power-delivery.aspx which has 5 Port as follow:

1) USB-C for 80W laptop charging or 5Gbps data transfer
2) Gigabit Ethernet Network
3) 4K HDMI
4) USB 3.0
5) USB 3.0

Test1: Initial USB devices:

user@user:~$ sudo lsusb 
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 009: ID 8087:0026 Intel Corp. AX201 Bluetooth
Bus 003 Device 006: ID 0c45:672e Microdia Integrated_Webcam_HD
Bus 003 Device 004: ID 27c6:63ac Shenzhen Goodix Technology Co.,Ltd. Goodix USB2.0 MISC
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
user@user:~$

Test2: After connecting USB Hub to PC:

-> Here when I connected USB HUB to PC why lsusb shows up these 4 USB devices for this one USB Hub

Bus 004 Device 027: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Bus 004 Device 026: ID 2109:0817 VIA Labs, Inc. USB3.0 Hub             
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 062: ID 2109:8817 VIA Labs, Inc. USB-C Multiport Adapter
Bus 003 Device 061: ID 2109:2817 VIA Labs, Inc. USB2.0 Hub  
user@user:~$ sudo dmesg 

[18985.773383] usb 3-3: new high-speed USB device number 61 using xhci_hcd
[18985.923960] usb 3-3: New USB device found, idVendor=2109, idProduct=2817, bcdDevice= 5.33
[18985.923971] usb 3-3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[18985.923975] usb 3-3: Product: USB2.0 Hub             
[18985.923978] usb 3-3: Manufacturer: VIA Labs, Inc.         
[18985.925703] hub 3-3:1.0: USB hub found
[18985.925886] hub 3-3:1.0: 5 ports detected
[18986.049724] usb 4-1: new SuperSpeed USB device number 26 using xhci_hcd
[18986.075263] usb 4-1: New USB device found, idVendor=2109, idProduct=0817, bcdDevice= 5.33
[18986.075266] usb 4-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[18986.075267] usb 4-1: Product: USB3.0 Hub             
[18986.075268] usb 4-1: Manufacturer: VIA Labs, Inc.         
[18986.076329] hub 4-1:1.0: USB hub found
[18986.076441] hub 4-1:1.0: 4 ports detected
[18986.637428] usb 4-1.1: new SuperSpeed USB device number 27 using xhci_hcd
[18986.658315] usb 4-1.1: New USB device found, idVendor=0bda, idProduct=8153, bcdDevice=30.00
[18986.658318] usb 4-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=6
[18986.658319] usb 4-1.1: Product: USB 10/100/1000 LAN
[18986.658321] usb 4-1.1: Manufacturer: CMI
[18986.658321] usb 4-1.1: SerialNumber: 000001
[18986.745830] r8152-cfgselector 4-1.1: reset SuperSpeed USB device number 27 using xhci_hcd
[18986.793249] r8152 4-1.1:1.0: load rtl8153a-4 v2 02/07/20 successfully
[18986.826148] r8152 4-1.1:1.0 eth0: v1.12.13
[18986.856606] r8152 4-1.1:1.0 enx5c857e3d381a: renamed from eth0
[18990.785555] usb 3-3.5: new high-speed USB device number 62 using xhci_hcd
[18990.887055] usb 3-3.5: New USB device found, idVendor=2109, idProduct=8817, bcdDevice= 0.01
[18990.887067] usb 3-3.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[18990.887071] usb 3-3.5: Product: USB-C Multiport Adapter
[18990.887082] usb 3-3.5: Manufacturer: VIA Labs, Inc.         
[18990.887089] usb 3-3.5: SerialNumber: 0000000000000001
user@user:~$

user@user:~$ sudo lsusb 
Bus 004 Device 027: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Bus 004 Device 026: ID 2109:0817 VIA Labs, Inc. USB3.0 Hub             
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 062: ID 2109:8817 VIA Labs, Inc. USB-C Multiport Adapter
Bus 003 Device 061: ID 2109:2817 VIA Labs, Inc. USB2.0 Hub             
Bus 003 Device 009: ID 8087:0026 Intel Corp. AX201 Bluetooth
Bus 003 Device 006: ID 0c45:672e Microdia Integrated_Webcam_HD
Bus 003 Device 004: ID 27c6:63ac Shenzhen Goodix Technology Co.,Ltd. Goodix USB2.0 MISC
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
user@user:~$

user@user:~$ sudo lsusb -tv
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/6p, 20000M/x2
    ID 1d6b:0003 Linux Foundation 3.0 root hub
    |__ Port 1: Dev 26, If 0, Class=Hub, Driver=hub/4p, 5000M
        ID 2109:0817 VIA Labs, Inc. 
        |__ Port 1: Dev 27, If 0, Class=Vendor Specific Class, Driver=r8152, 5000M
            ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/16p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 3: Dev 61, If 0, Class=Hub, Driver=hub/5p, 480M
        ID 2109:2817 VIA Labs, Inc. 
        |__ Port 5: Dev 62, If 0, Class=, Driver=, 480M
            ID 2109:8817 VIA Labs, Inc. 
    |__ Port 10: Dev 4, If 0, Class=Vendor Specific Class, Driver=, 12M
        ID 27c6:63ac Shenzhen Goodix Technology Co.,Ltd. 
    |__ Port 11: Dev 6, If 0, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 11: Dev 6, If 1, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 11: Dev 6, If 2, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 11: Dev 6, If 3, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 14: Dev 9, If 0, Class=Wireless, Driver=btusb, 12M
        ID 8087:0026 Intel Corp. AX201 Bluetooth
    |__ Port 14: Dev 9, If 1, Class=Wireless, Driver=btusb, 12M
        ID 8087:0026 Intel Corp. AX201 Bluetooth
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
user@user:~$

Test3: After connecting Wireless Keyboard and Mouse to USB Hub:

Wireless Mouse : Connected to one of the Port: USB 3.0 to this USB Hub

Wired Keyboard : Connected to one of the Port: USB 3.0 to this USB Hub

user@user:~$ sudo dmesg 


[19065.514543] usb 3-3.2: new full-speed USB device number 63 using xhci_hcd
[19065.666592] usb 3-3.2: New USB device found, idVendor=046d, idProduct=c548, bcdDevice= 5.01
[19065.666604] usb 3-3.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[19065.666608] usb 3-3.2: Product: USB Receiver
[19065.666611] usb 3-3.2: Manufacturer: Logitech
[19065.675895] input: Logitech USB Receiver as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.2/3-3.2:1.0/0003:046D:C548.0052/input/input80
[19065.735155] hid-generic 0003:046D:C548.0052: input,hidraw1: USB HID v1.11 Keyboard [Logitech USB Receiver] on usb-0000:00:14.0-3.2/input0
[19065.736724] input: Logitech USB Receiver Mouse as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.2/3-3.2:1.1/0003:046D:C548.0053/input/input81
[19065.736956] input: Logitech USB Receiver Consumer Control as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.2/3-3.2:1.1/0003:046D:C548.0053/input/input82
[19065.794883] input: Logitech USB Receiver System Control as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.2/3-3.2:1.1/0003:046D:C548.0053/input/input83
[19065.795488] hid-generic 0003:046D:C548.0053: input,hidraw2: USB HID v1.11 Mouse [Logitech USB Receiver] on usb-0000:00:14.0-3.2/input1
[19065.797179] hid-generic 0003:046D:C548.0054: hiddev0,hidraw3: USB HID v1.11 Device [Logitech USB Receiver] on usb-0000:00:14.0-3.2/input2
[19069.106472] usb 3-3.3: new full-speed USB device number 64 using xhci_hcd
[19069.258696] usb 3-3.3: New USB device found, idVendor=046d, idProduct=c52b, bcdDevice=12.11
[19069.258708] usb 3-3.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[19069.258712] usb 3-3.3: Product: USB Receiver
[19069.258716] usb 3-3.3: Manufacturer: Logitech
[19069.274724] logitech-djreceiver 0003:046D:C52B.0057: hiddev1,hidraw4: USB HID v1.11 Device [Logitech USB Receiver] on usb-0000:00:14.0-3.3/input2
[19069.405450] input: Logitech M510 as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3.3/3-3.3:1.2/0003:046D:C52B.0057/0003:046D:4051.0058/input/input84
[19069.405813] logitech-hidpp-device 0003:046D:4051.0058: input,hidraw5: USB HID v1.11 Mouse [Logitech M510] on usb-0000:00:14.0-3.3/input2:1
[19069.619234] logitech-hidpp-device 0003:046D:4051.0058: HID++ 4.5 device connected.
user@user:~$



user@user:~$ sudo lsusb 
Bus 004 Device 027: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Bus 004 Device 026: ID 2109:0817 VIA Labs, Inc. USB3.0 Hub             
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 062: ID 2109:8817 VIA Labs, Inc. USB-C Multiport Adapter
Bus 003 Device 064: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 003 Device 063: ID 046d:c548 Logitech, Inc. USB Receiver
Bus 003 Device 061: ID 2109:2817 VIA Labs, Inc. USB2.0 Hub             
Bus 003 Device 009: ID 8087:0026 Intel Corp. AX201 Bluetooth
Bus 003 Device 006: ID 0c45:672e Microdia Integrated_Webcam_HD
Bus 003 Device 004: ID 27c6:63ac Shenzhen Goodix Technology Co.,Ltd. Goodix USB2.0 MISC
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
user@user:~$
user@user:~$ sudo lsusb -tv
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/6p, 20000M/x2
    ID 1d6b:0003 Linux Foundation 3.0 root hub
    |__ Port 1: Dev 26, If 0, Class=Hub, Driver=hub/4p, 5000M
        ID 2109:0817 VIA Labs, Inc. 
        |__ Port 1: Dev 27, If 0, Class=Vendor Specific Class, Driver=r8152, 5000M
            ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/16p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 3: Dev 61, If 0, Class=Hub, Driver=hub/5p, 480M
        ID 2109:2817 VIA Labs, Inc. 
        |__ Port 5: Dev 62, If 0, Class=, Driver=, 480M
            ID 2109:8817 VIA Labs, Inc. 
        |__ Port 3: Dev 64, If 1, Class=Human Interface Device, Driver=usbhid, 12M
            ID 046d:c52b Logitech, Inc. Unifying Receiver
        |__ Port 3: Dev 64, If 2, Class=Human Interface Device, Driver=usbhid, 12M
            ID 046d:c52b Logitech, Inc. Unifying Receiver
        |__ Port 3: Dev 64, If 0, Class=Human Interface Device, Driver=usbhid, 12M
            ID 046d:c52b Logitech, Inc. Unifying Receiver
        |__ Port 2: Dev 63, If 2, Class=Human Interface Device, Driver=usbhid, 12M
            ID 046d:c548 Logitech, Inc. 
        |__ Port 2: Dev 63, If 0, Class=Human Interface Device, Driver=usbhid, 12M
            ID 046d:c548 Logitech, Inc. 
        |__ Port 2: Dev 63, If 1, Class=Human Interface Device, Driver=usbhid, 12M
            ID 046d:c548 Logitech, Inc. 
    |__ Port 10: Dev 4, If 0, Class=Vendor Specific Class, Driver=, 12M
        ID 27c6:63ac Shenzhen Goodix Technology Co.,Ltd. 
    |__ Port 11: Dev 6, If 0, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 11: Dev 6, If 1, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 11: Dev 6, If 2, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 11: Dev 6, If 3, Class=Video, Driver=uvcvideo, 480M
        ID 0c45:672e Microdia 
    |__ Port 14: Dev 9, If 0, Class=Wireless, Driver=btusb, 12M
        ID 8087:0026 Intel Corp. AX201 Bluetooth
    |__ Port 14: Dev 9, If 1, Class=Wireless, Driver=btusb, 12M
        ID 8087:0026 Intel Corp. AX201 Bluetooth
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
user@user:~$ 

As when I connect USB Hub to PC I guess root device is this one

Bus 003 Device 062: ID 2109:8817 VIA Labs, Inc. USB-C Multiport Adapter

then how does I list all devices connected under this root node in this case its Wireless Keyboard and Mouse

sample code which I am using this one to list all USB devices: https://github.com/libusb/libusb/blob/master/examples/listdevs.c

However I am not sure how to determine Parent->Child relationship between listed all devices to perform following task:

  1. Want to detect root Hub device
  2. then based on this root hub device want to list all devices connected to this hub device (here its Mouse and Keyboard)

pseudo code:

/* get all usb device using this API */
libusb_get_device_list(NULL, &devs);
/* find child devices connected to root device*/
print_devs(devs);
static void print_devs(libusb_device **devs)
{
    libusb_device *dev;
    int i = 0, j = 0;

    while ((dev = devs[i++]) != NULL) {
        struct libusb_device_descriptor desc;
        int r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0) {
            fprintf(stderr, "failed to get device descriptor");
            return;
        }

        if(desc.idVendor == rootDeviceidVendor && desc.idProduct == rootDevicepProduct){
            /* want to list only devices connected to this hub device */
        }

    }
}



Solution

  • I remember doing this exact thing a couple of years ago, but I do not have the code at hand.

    You have a very good start there, going through the list of devices; Then you need to call:

    libusb_device *parent = libusb_get_parent(dev);
    int r = libusb_get_device_descriptor(parent, &desc);
    

    Now you can go ahead and check for proper desc.idVendor and desc.idProduct on this descriptor.

    See spec of libusb_get_parent.