简体   繁体   English

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

[英]C/C++ How to get the usb subsystem path for a USB libudev hidraw device?

I'm using libudev in C/C++ with the hidraw subsystem to enumerate and communicate with custom HID devices - working well.我在 C/C++ 中使用 libudev 和 hidraw 子系统来枚举自定义 HID 设备并与之通信 - 运行良好。 My devices are assigned specific usb plugs and they are "hot swappable" - I need to know which plug is connected to each hidraw device.我的设备被分配了特定的 USB 插头,它们是“热插拔的” - 我需要知道哪个插头连接到每个 hidraw 设备。 Is there a correspondence between the usb and the hidraw subsystems and how to get the usb path which details the interface route ( like: /dev/bus/usb/002/001 and not the hidraw path) for each device from its hidraw device pointer? usb 和 hidraw 子系统之间是否有对应关系,以及如何每个设备的 hidraw 设备指针获取详细说明接口路由的 usb 路径(例如:/dev/bus/usb/002/001而不是 hidraw 路径) ?

i think with hidraw device pointer you mean the device nodes like /dev/hidraw0 or similar我认为 hidraw 设备指针是指像/dev/hidraw0或类似的设备节点

Hidraw uses a dynamic major number, meaning that udev should be relied on to create hidraw device nodes. Hidraw 使用动态主编号,这意味着应该依赖 udev 来创建 hidraw 设备节点。 Udev will typically create the device nodes directly under /dev (eg: /dev/hidraw0). Udev 通常会直接在 /dev 下创建设备节点(例如:/dev/hidraw0)。 As this location is distribution- and udev rule-dependent, applications should use libudev to locate hidraw devices attached to the system.由于此位置依赖于发行版和 udev 规则,应用程序应使用 libudev 来定位连接到系统的 hidraw 设备。 There is a tutorial on libudev with a working example at: http://www.signal11.us/oss/udev/有一个关于 libudev 的教程和一个工作示例: http : //www.signal11.us/oss/udev/

linux has two species of device nodes, one created by device drivers ie /dev/sdb for a mass storage device and raw device nodes like /dev/bus/usb/BBB/DDD where BBB is the bus number and DDD is the device number, that are created by the kernel directly : linux 有两种设备节点,一种由设备驱动程序创建,即/dev/sdb用于大容量存储设备和原始设备节点,如/dev/bus/usb/BBB/DDD ,其中BBB是总线编号, DDD是设备编号,由内核直接创建:

USB Device Issues USB 设备问题

USB devices usually have two kinds of device nodes associated with them. USB 设备通常有两种设备节点与之关联。

The first kind is created by device-specific drivers (eg, usb_storage/sd_mod or usblp) in the kernel.第一种是由内核中特定于设备的驱动程序(例如,usb_storage/sd_mod 或 usblp)创建的。 For example, a USB mass storage device would be /dev/sdb, and a USB printer would be /dev/usb/lp0.例如,USB 大容量存储设备将是 /dev/sdb,而 USB 打印机将是 /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.即使设备没有内核驱动程序,也会创建第二种设备节点(/dev/bus/usb/BBB/DDD,其中 BBB 是总线编号,DDD 是设备编号)。 By using these "raw" USB device nodes , an application can exchange arbitrary USB packets with the device, ie, bypass the possibly-existing kernel driver.通过使用这些“原始”USB 设备节点,应用程序可以与设备交换任意 USB 数据包,即绕过可能存在的内核驱动程序。

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

you want to establish a link between the kernel module device node ( ie /dev/hidraw0 ) and the corresponding raw device node ( ie /dev/bus/usb/BBB/DDD )您要在内核模块设备节点(即/dev/hidraw0 )和相应的原始设备节点(即/dev/bus/usb/BBB/DDD )之间建立链接

you can get the bus address ( BBB and DDD ) from the device node using sudo udevadm info -a -p $(sudo udevadm info -q path -n /dev/hidraw0) ( ATTRS{busnum}=="BBB" and ATTRS{devnum}=="DDD" in the output ) however this is a bit ugly您可以使用sudo udevadm info -a -p $(sudo udevadm info -q path -n /dev/hidraw0) ( ATTRS{busnum}=="BBB" and ATTRS{devnum}=="DDD" ) 从设备节点获取总线地址( BBBDDDATTRS{devnum}=="DDD"在输出中)但是这有点难看

in Find bus number and device number with device file symlink is code using libudev to get bus number BBB and device number DDD for a specific device node in /dev/ ie /dev/hidraw0 it uses udev_device_get_sysattr_value(dev, "devnum"));使用设备文件符号链接查找总线编号和设备编号是使用libudev获取/dev/特定设备节点的总线编号 BBB 和设备编号 DDD 的代码,即/dev/hidraw0它使用udev_device_get_sysattr_value(dev, "devnum")); to get DDD in /dev/bus/usb/BBB/DDD and udev_device_get_sysattr_value(dev, busnum")); to get BBB得到DDD/dev/bus/usb/BBB/DDDudev_device_get_sysattr_value(dev, busnum"));以获得BBB

you can also get BBB and DDD from sysfs ( /sys/devices/ ... ) :您还可以从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 bDeviceSubClass 配置 idProduct 移除授权
bmAttributes descriptors idVendor serial avoid_reset_quirk bMaxPacketSize0 dev manufacturer bmAttributes 描述符 idVendor serial avoid_reset_quirk bMaxPacketSize0 dev 制造商
speed bcdDevice bMaxPower devnum速度 bcdDevice bMaxPower devnum
maxchild subsystem bConfigurationValue bNumConfigurations devpath maxchild 子系统 bConfigurationValue bNumConfigurations devpath
power uevent bDeviceClass bNumInterfaces driver power uevent bDeviceClass bNumInterfaces 驱动程序
product urbnum bDeviceProtocol busnum产品 urbnum bDeviceProtocol busnum
ep_00 ep_00

quirks version怪癖版本

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

to get the sysfs path of your device ( the /sys/devices/pci0000:00/0000:00:12.2/usb2/2-5/2-5.4 above ) use sudo udevadm info -q path -n /dev/hidraw0要获取设备的 sysfs 路径(上面的/sys/devices/pci0000:00/0000:00:12.2/usb2/2-5/2-5.4 ),请使用sudo udevadm info -q path -n /dev/hidraw0

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

Hope it's helpful but IIRC, you cannot reliably (with port# being same between insert events) enumerate individual ports on hubs that are connected downstream from a main USB controller point.希望它有帮助,但是 IIRC,您不能可靠地(插入事件之间的端口号相同)枚举连接到主 USB 控制器点下游的集线器上的各个端口。 I think that was one of the reasons we started seeing lots and lots of USB controllers on MB's after USB came out;我认为这是 USB 出现后我们开始在 MB 上看到大量 USB 控制器的原因之一; because when chaining everything off downstream hubs, besides negatively affecting bandwidth, also caused problems with persistent software numbering issues.因为当从下游集线器链接所有东西时,除了对带宽产生负面影响之外,还会导致持续软件编号问题。

I believe when a device is plugged into a USB port directly connected to a USB controller, you can reliably get the same exact port# it's connected to.我相信当设备插入直接连接到 USB 控制器的 USB 端口时,您可以可靠地获得与它连接的完全相同的端口#。 But when doing that from a downstream multi-port USB hub connected upstream to a USB port from a USB controller, the actual port# on the USB hub does not get passed upstream or even if it does, it is not a predictable port# between insertions/power-resets in the same hub port.但是,当从上游连接到 USB 控制器的 USB 端口的下游多端口 USB 集线器执行此操作时,USB 集线器上的实际端口号不会通过上游,或者即使它通过,也不是可预测的端口号之间在同一集线器端口中插入/电源重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM