我想写一个应用程序,从连接到Linux的USB集线器读取数据(iSerial信息),以及将其他设备连接到那个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-power-delivery.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

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:

->这里当我将USB集线器连接到PC时,为什么lsusb会为这一个USB集线器显示这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:~$

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

无线鼠标:连接到其中一个端口: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

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

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

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

  1. 想要检测根集线器设备
  2. 然后根据此根集线器设备列出连接到此集线器设备的所有设备(此处为其鼠标和键盘)

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

    }
}


推荐答案

我记得几年前我确实做过这样的事情,但我手头没有代码.

您在这里有一个非常好的开始,浏览了设备列表;然后您需要致电:

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

现在,您可以继续判断此描述符上是否有正确的desc.idVendordesc.idProduct.

请参阅规范libusb_get_parent.

C++相关问答推荐

数据包未从DPDK端口传输到内核端口

*p[num]和(*p)num的区别

编译的时候g++通常会比GCC慢很多吗?

在没有动态内存分配的情况下,用C语言最快地将各种数组复制到单个较大的数组中

C在声明带有值的数组时,声明大小有用吗?

将uintptr_t添加到指针是否对称?

是什么让numpy.sum比优化的(自动矢量化的)C循环更快?

struct -未知大小

加密解密工作正常,但返回错误0x80090005

在WSL关闭/重新启动后,是什么原因导致共享对象依赖关系发生更改?

从C文件中删除注释

C语言中MPI发送接收字符串时出现的分段错误

将回调/基于事件的C API转换为非回调API

循环中的静态变量与块中的变量和循环

为什么电路板被循环删除?

合并对 struct 数组进行排序

将非连续物理内存映射到用户空间

如何逐位读取二进制文件?

生成的头文件不包括用户定义的文件

模仿 memmove 的行为