linux / libusb获得usb设备路径。

时间:2022-05-31 16:13:22

I use libusb to enumerate over a few usb-devices. Now i like to get the "device-path". I think it's not called usb device-path, because i was not successful with google.

我用libusb来列举几个usb设备。现在我喜欢“设备路径”。我认为它不被称为usb设备路径,因为我在谷歌没有成功。

If i connect a usb-device with linux, i get a message in dmesg, here are a few examples for such a "device-path" with an usb temperature sensor (something like this):

如果我将一个usb设备与linux连接起来,我在dmesg中得到一个消息,这里有几个例子,可以使用usb温度传感器(类似这样的东西)这样的“设备路径”:

Directly to a usb port: [68448.099682] generic-usb 0003:0C45:7401.0056: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.0-1/input0 => 12.0-1

直接到usb端口:[68448.099682]generic-usb - 0003:0C45:7401.0056:输入,hidraw1: usb HID v1.10键盘[RDing TEMPer1V1.2]在usb-0000:00 00:12 -1/input0 => 12.0-1。

Directly to another port: [68560.853108] generic-usb 0003:0C45:7401.0058: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:13.0-1/input0 => 13.0-1

直接到另一个端口:[68560.853108]generic-usb 0003:0C45:7401.0058:输入,hidraw1: USB HID v1.10键盘[RDing TEMPer1V1.2]在USB -0000:00:13 0-1/input0 => 13.0-1。

To a usb hub on the first used port: [68600.245809] generic-usb 0003:0C45:7401.005A: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4/input0 => 12.2-1.4

在第一个使用的端口上的usb集线器:[68600.245809]generic-usb - 0003:0C45:7401.005A:输入,hidraw1: usb HID v1.10键盘[RDing TEMPer1V1.2]在usb-0000:00:12 -1.4/input0 => 12.2-1.4。

To another port on the same usb hub: [68647.925092] generic-usb 0003:0C45:7401.005C: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.3/input0 => 12.2-1.3

在同一个usb集线器上的另一个端口:[68647.925092]generic-usb 0003:0C45:7401.005C:输入,hidraw1: usb HID v1.10键盘[RDing TEMPer1V1.2]在usb-0000:00:12 -1.3/input0 => 12.2-1.3。

An now to a usb hub on the usb hub used before: [68740.715518] generic-usb 0003:0C45:7401.005E: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4.4/input0 => 12.2-1.4.4

a现在到usb集线器的usb集线器上使用:[68740.715518]generic-usb 0003:0C45:7401.005E:输入,hidraw1: usb HID v1.10键盘[RDing TEMPer1V1.2]在usb-0000:00:12 -1.4.4/input0 => 12.2-1.4.4。

Long story short: The kernel message always contains a unique path for the physical usb device location (see the bold text before). Is it possible to get this "path" in the user space via libusb? I tried many things with struct usb_bus and struct usb_device, but i always was unsuccessfully.

长话短说:内核消息总是包含物理usb设备位置的唯一路径(参见前面的粗体文本)。是否有可能通过libusb获得用户空间中的“路径”?我尝试了很多结构usb_bus和struct usb_device,但我总是失败。

I need this to identify multiple of these usb thermometers, because they don't have a unique serial number and sometimes they just "reconnect" at runtime, so they get different usb id's. So i think the only way to identify them is via the physical location.

我需要这个来识别多个usb温度计,因为它们没有一个唯一的序列号,有时候它们只是在运行时“重新连接”,所以它们会得到不同的usb id。所以我认为辨别它们的唯一方法是通过物理位置。

Thanks for the help,

谢谢你的帮助,

Best Regards Kevin M.

最好的祝福凯文·M。

-edit-

编辑- - - - - -

Currently i use the following code to search my usb device:

目前我使用以下代码搜索我的usb设备:

usb_dev_handle *find_lvr_winusb() {

     struct usb_bus *bus;
        struct usb_device *dev;

        for (bus = usb_busses; bus; bus = bus->next) {
        for (dev = bus->devices; dev; dev = dev->next) {
                        if (dev->descriptor.idVendor == VENDOR_ID && 
                                dev->descriptor.idProduct == PRODUCT_ID ) {
                                usb_dev_handle *handle;
                                if(debug) {
                                  printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);
                                    printf("INFO: %d\n", dev->bus->location);
                                    printf("INFO: %d %s\n", bus->location, bus->dirname);
                                }

                                if (!(handle = usb_open(dev))) {
                                        printf("Could not open USB device\n");
                                        return NULL;
                                }
                                return handle;
                        }
                }
        }
        return NULL;
}

But with this code i cannot get a unique physical position id. The bus->location returns an integer (bus->dirname contains the same, but as string), which is not unique. I know usb has a hierarchy and in the dmesg i can see this hierarchys path.

但是使用此代码,我无法获得唯一的物理位置id。总线->位置返回一个整数(bus->dirname包含相同的,但作为字符串),这不是唯一的。我知道usb有一个层次结构,在dmesg中我可以看到这个层次结构的路径。

With libusb i only can get the bus-id (?) and some device id's. But they don't help me, because i need to identify two or more of these temperature sensors. The device-id always changes when the temperature sensor reset's the connection (every 5 to 60 seconds) and the bus id is not unique. Unfortunately the temperature sensor has no unique serial id.

有了libusb,我只能得到总线id(?)和一些设备id。但是他们没有帮助我,因为我需要确定两个或更多的温度传感器。当温度传感器重置连接(每5到60秒)和总线id并不是唯一的时候,设备id总是会发生变化。不幸的是,温度传感器没有唯一的序列号。

So i think the physical path is the only way to identify the device.

所以我认为物理路径是识别设备的唯一方法。

Best regards Kevin M.

最好的祝福凯文·M。

3 个解决方案

#1


5  

Since libusb 1.0.12, they have introduced libusb_get_port_path(), and in 1.0.16 replaced it with libusb_get_port_numbers() which allows you to query the bus topology.

由于libusb 1.0.12,他们引入了libusb_get_port_path(),在1.0.16中使用libusb_get_port_numbers()替换它,它允许您查询总线拓扑。

#2


3  

Overall summery of the sysfs structure path:

sysfs结构路径整体夏季:

1-1.3:1.0

|_usb root hub - bus number - 1
  |_ port number - 1 of root hub
    |_port number - 3 of intermediate hub
      |_current configuration number - 1
        |_ current interface number - 0

More information here

更多的信息在这里

#3


0  

It's possible, like here.

它是可能的,像在这里。

Just scan all USB devices at all busses. When you find needed VID/PID — that's your device.

只需扫描所有总线上的USB设备。当你发现需要的VID/PID时,这是你的设备。

Or you can do simpler: write an udev-rule which would create symlink like /dev/thermoX when you attach you device. All you need after is to open needed /dev/thermoX.

或者你可以做的更简单:编写一个udev-rule,当你附加设备时,它会创建一个像/dev/thermoX这样的符号链接。您所需要的是打开需要的/dev/ thermox。

#1


5  

Since libusb 1.0.12, they have introduced libusb_get_port_path(), and in 1.0.16 replaced it with libusb_get_port_numbers() which allows you to query the bus topology.

由于libusb 1.0.12,他们引入了libusb_get_port_path(),在1.0.16中使用libusb_get_port_numbers()替换它,它允许您查询总线拓扑。

#2


3  

Overall summery of the sysfs structure path:

sysfs结构路径整体夏季:

1-1.3:1.0

|_usb root hub - bus number - 1
  |_ port number - 1 of root hub
    |_port number - 3 of intermediate hub
      |_current configuration number - 1
        |_ current interface number - 0

More information here

更多的信息在这里

#3


0  

It's possible, like here.

它是可能的,像在这里。

Just scan all USB devices at all busses. When you find needed VID/PID — that's your device.

只需扫描所有总线上的USB设备。当你发现需要的VID/PID时,这是你的设备。

Or you can do simpler: write an udev-rule which would create symlink like /dev/thermoX when you attach you device. All you need after is to open needed /dev/thermoX.

或者你可以做的更简单:编写一个udev-rule,当你附加设备时,它会创建一个像/dev/thermoX这样的符号链接。您所需要的是打开需要的/dev/ thermox。