简体   繁体   English

使用libusb-1.0作为非root用户访问USB设备

[英]Accessing a USB device with libusb-1.0 as a non-root user

I am trying to interface with a USB device as a non-root user on RHEL5. 我试图在RHEL5上作为非root用户与USB设备连接。 The device is a GPIO interface (its documentation can be found at http://www.xdimax.com/sub20/sub20.html ) which uses libusb-1.0. 该设备是一个GPIO接口(其文档可以在http://www.xdimax.com/sub20/sub20.html上找到),它使用libusb-1.0。 The procedure for opening the device, using its API, is: 使用其API打开设备的过程是:

sub_device d;
d = sub_find_devices(0);
sub_handle h = sub_open(d);

When I do this, the sub_find_devices() call works, but on the sub_open() call, I get the libusb error -3, which indicates that I do not have permissions to open the device for writing. 当我这样做时, sub_find_devices()调用工作,但在sub_open()调用,我得到libusb错误-3,这表明我没有权限打开设备进行写入。

I did some research on this problem, and found that I should create a udev rule. 我对这个问题进行了一些研究,发现我应该创建一个udev规则。 Using udevinfo on the device's sysfs node, I got: 在设备的sysfs节点上使用udevinfo,我得到了:

looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2':
KERNEL=="2-1.2"
SUBSYSTEM=="usb"
SYSFS{configuration}==""
SYSFS{serial}=="15F2"
SYSFS{product}=="SUB-20"
SYSFS{manufacturer}=="XDIMAX"
SYSFS{maxchild}=="0"
SYSFS{version}==" 1.10"
SYSFS{devnum}=="6"
SYSFS{speed}=="12"
SYSFS{bMaxPacketSize0}=="64"
SYSFS{bNumConfigurations}=="1"
SYSFS{bDeviceProtocol}=="00"
SYSFS{bDeviceSubClass}=="00"
SYSFS{bDeviceClass}=="ff"
SYSFS{bcdDevice}=="0001"
SYSFS{idProduct}=="ffc3"
SYSFS{idVendor}=="04d8"
SYSFS{bMaxPower}=="100mA"
SYSFS{bmAttributes}=="80"
SYSFS{bConfigurationValue}=="1"
SYSFS{bNumInterfaces}==" 1"

I then created the following udev rule, in the file /etc/udev/rules.d/991-local.rules : 然后我在/etc/udev/rules.d/991-local.rules文件中创建了以下udev规则:

SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="ffc3", NAME="sub20", GROUP="582", MODE="0660"

582 is the GID of a group that my normal user belongs to. 582是我的普通用户所属的组的GID。 I also tried the rule with the group name and it did not work. 我也尝试了组名称的规则,但它没有用。 After creating this rule, the device file /dev/sub20 is created with the correct permissions, but only exists when the device is plugged in, which gives me reasonable confidence that the udev rule is matching on the correct device. 创建此规则后,将使用正确的权限创建设备文件/dev/sub20 ,但仅在插入设备时才存在,这使我有理由相信udev规则在正确的设备上匹配。 However, my code still gets error -3. 但是,我的代码仍然会出错-3。

Doing an strace on the code revealed this call: 对代码进行分析后发现了这个问题:

open("/dev/bus/usb/002/006", O_RDWR)    = -1 EACCES (Permission denied)

The permissions on the /dev/bus/usb... node are still root:root, so maybe this is an indication that there is a problem with my udev rule, although I don't know what that could be. /dev/bus/usb...节点上的权限仍然是root:root,所以这可能表明我的udev规则存在问题,尽管我不知道那可能是什么。

If I try doing a call to open("/dev/sub20", O_RDWR) , I get the return value ENXIO (No such device or address) , another possible indicator of an error in the udev rule, although the /dev/sub20 file clearly is associated with the correct device somehow, since it only exists when the device is plugged in. 如果我尝试open("/dev/sub20", O_RDWR)调用open("/dev/sub20", O_RDWR) ,我得到返回值ENXIO (No such device or address) ,这是udev规则中错误的另一个可能指示,尽管/dev/sub20文件清楚地以某种方式与正确的设备相关联,因为它仅在插入设备时才存在。

What else can I do to try and get this to work? 我还能做些什么来尝试让它发挥作用?

The udev rules I use to be able to access devices with libusb look like this: SUBSYSTEMS=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="ffc3", SYMLINK+="sub20", GROUP="usb", MODE="660" . 我用来访问libusb设备的udev规则如下所示: SUBSYSTEMS=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="ffc3", SYMLINK+="sub20", GROUP="usb", MODE="660" It's supposed to just add a symlink to the device, but the permissions are also working for me afterwards (I am a member of the group usb). 它应该只是添加一个符号链接到设备,但之后权限也适用于我(我是组usb的成员)。

For usb devices FTDI I use the script: 对于usb设备FTDI我使用脚本:

ftdi_config.sh ftdi_config.sh

#!/bin/sh

echo "This script is for modern debian systems, and does everything that should"
echo "be necessary to use ftdi usb devices as a regular user with libftdi,"
echo "instead of the built-in dumb kernel driver."
echo
if [ $(id -u) != 0 ]; then
  echo "This script must be run as root."
  exit 1
else
  read -p "Press enter to continue, or ctrl-c to bail..." x
fi
echo
echo "** Adding usb group"
groupadd usb
echo
echo "** Setting udev permissions on usb devices"
echo 'SUBSYSTEMS=="usb", ACTION=="add", MODE="0664", GROUP="usb"' >> /etc/udev/rules.d/99-usbftdi.rules
echo
echo "** Reloading udev rules"
/etc/init.d/udev reload
echo
echo "** Blacklisting ftdi_sio driver"
echo 'blacklist ftdi_sio' > /etc/modprobe.d/ftdi.conf
echo
echo "** Removing old ftdi_sio driver (it's ok if it fails)"
rmmod ftdi_sio
echo
echo "!! Run the following command as root, to add your user to the usb group:"
echo "useradd -G usb yourusernamehere"
echo
echo "or"
echo
echo "Adding to a existing user:"
echo "usermod -a -G usb yourusernamehere"
echo
echo "as then you must reboot the system:"
echo "reboot" 

then run your app as a non-root user. 然后以非root用户身份运行您的应用。 It works!!! 有用!!!

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

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