gpt4 book ai didi

c++ - C/C++ 如何获取 USB libudev hidraw 设备的 usb 子系统路径?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:53 25 4
gpt4 key购买 nike

我在 C/C++ 中使用 libudev 和 hidraw 子系统来枚举自定义 HID 设备并与之通信 - 运行良好。我的设备分配有特定的 USB 插头,并且它们是“可热插拔”的 - 我需要知道哪个插头连接到每个 hidraw 设备。 usb 和 hidraw 子系统之间是否存在对应关系,以及如何获取详细说明接口(interface)路由的 usb 路径(如:/dev/bus/usb/002/001 而不是 hidraw 路径)每个设备从它的 hidraw 设备指针?

最佳答案

我认为 hidraw 设备指针是指像 /dev/hidraw0 或类似的设备节点

Hidraw uses a dynamic major number, meaning that udev should be relied on to create hidraw device nodes. Udev will typically create the device nodes directly under /dev (eg: /dev/hidraw0). As this location is distribution- and udev rule-dependent, applications should use libudev to locate hidraw devices attached to the system. There is a tutorial on libudev with a working example at: http://www.signal11.us/oss/udev/

linux 有两种设备节点,一种是由设备驱动程序创建的,即用于大容量存储设备的 /dev/sdb 和像 /dev/bus/usb/BBB/这样的原始设备节点DDD 其中BBB是总线号,DDD是设备号,都是内核直接创建的:

USB Device Issues

USB devices usually have two kinds of device nodes associated with them.

The first kind is created by device-specific drivers (e.g., usb_storage/sd_mod or usblp) in the kernel. For example, a USB mass storage device would be /dev/sdb, and a USB printer would be /dev/usb/lp0. These device nodes exist only when the device-specific driver is loaded.

The second kind of device nodes (/dev/bus/usb/BBB/DDD, where BBB is the bus number and DDD is the device number) are created even if the device doesn't have a kernel driver. By using these "raw" USB device nodes, an application can exchange arbitrary USB packets with the device, i.e., bypass the possibly-existing kernel driver.

来源:http://www.linuxfromscratch.org/blfs/view/7.10/postlfs/devices.html

您想在内核模块设备节点(即/dev/hidraw0)和相应的原始设备节点(即/dev/bus/usb/BBB/DDD)之间建立链接)

您可以使用 sudo udevadm info -a -p $(sudo udevadm info -q) 从设备节点获取总线地址(BBBDDD) path -n/dev/hidraw0)(输出中的 ATTRS{busnum}=="BBB"ATTRS{devnum}=="DDD" )然而这有点难看

Find bus number and device number with device file symlink是使用 libudev 获取 /dev/ 中特定设备节点的总线号 BBB 和设备号 DDD 的代码,即它使用的 /dev/hidraw0 udev_device_get_sysattr_value(dev, "devnum"));/dev/bus/usb/BBB/DDDudev_device_get_sysattr_value 中获取 DDD (dev, busnum")); 得到 BBB

您还可以从 sysfs ( /sys/devices/... ) 获取 BBB 和 DDD:

/sys/devices/pci0000:00/0000:00:12.2/usb2/2-5/2-5.4$ ls 2-5.4:1.0
bDeviceSubClass configuration idProduct remove authorized
bmAttributes descriptors idVendor serial avoid_reset_quirk bMaxPacketSize0 dev manufacturer
speed bcdDevice bMaxPower devnum
maxchild subsystem bConfigurationValue bNumConfigurations devpath
power uevent bDeviceClass bNumInterfaces driver
product urbnum bDeviceProtocol busnum
ep_00

quirks version

来源:http://www.signal11.us/oss/udev/

要获取设备的 sysfs 路径(上面的 /sys/devices/pci0000:00/0000:00:12.2/usb2/2-5/2-5.4)使用 须藤 udevadm 信息 -q 路径 -n/dev/hidraw0

( https://unix.stackexchange.com/questions/344784/how-to-map-sys-bus-usb-devices-to-dev-video )

关于c++ - C/C++ 如何获取 USB libudev hidraw 设备的 usb 子系统路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com