linux下如何识别USB HUB(根)和连接到HUB(根设备)的设备(子设备)?

问题描述 投票:0回答:1

我想编写一个应用程序来从连接的 USB 集线器读取数据(iSerial 信息)到 Linux 以及将其他设备连接到该 USB 集线器。

-> 我想首先检测 USB 集线器是否已连接到 linux,我可以使用 idVendor:idProduct 来识别它

->如果已连接,我想读取其信息,则必须遍历连接到该 USB 集线器的所有 USB 设备,并且也必须读取该设备信息

我正在使用此 USB 集线器:https://www.cablematters.com/pc-873-126-usb-c-multiport-adapter-with-4k-hdmi-2x-usb-30-gigabit-ethernet-and -供电.aspx 其中有 5 个端口如下:

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

测试1:初始USB设备:

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:~$

测试2:将USB Hub连接到PC后:

-> 这里,当我将 USB HUB 连接到 PC 时,为什么 lsusb 显示此 USB Hub 的这 4 个 USB 设备

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:~$

测试3:将无线键盘和鼠标连接到USB集线器后:

无线鼠标:连接到以下端口之一:USB 3.0 到此 USB 集线器

有线键盘:连接到以下端口之一:USB 3.0 到此 USB 集线器

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:~$ 

当我将 USB 集线器连接到 PC 时,我猜根设备就是这个

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

那么我如何列出在此根节点下连接的所有设备(在本例中为无线键盘和鼠标)

我使用这个示例代码来列出所有 USB 设备: https://github.com/libusb/libusb/blob/master/examples/listdevs.c

但是我不确定如何确定列出的所有设备之间的父->子关系以执行以下任务:

  1. 想要检测根集线器设备
  2. 然后基于这个根集线器设备想要列出连接到这个集线器设备的所有设备(这里是鼠标和键盘)

伪代码:

/* 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 */
        }

    }
}


c linux usb libusb libusb-1.0
1个回答
0
投票

我记得几年前就做过这件事,但我手头没有代码。

您已经有了一个很好的开始,浏览了设备列表;那么你需要打电话:

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

现在您可以继续检查此描述符上的

desc.idVendor
desc.idProduct
是否正确。

请参阅 libusb_get_parent 的规范。

© www.soinside.com 2019 - 2024. All rights reserved.