Linux 以非root用户身份访问具有libusb-1.0的USB设备

Linux 以非root用户身份访问具有libusb-1.0的USB设备,linux,permissions,libusb,Linux,Permissions,Libusb,我正在尝试以RHEL5上的非root用户身份与USB设备接口。该设备是一个使用libusb-1.0的GPIO接口(其文档可在中找到)。使用API打开设备的步骤如下: sub_device d; d = sub_find_devices(0); sub_handle h = sub_open(d); 当我这样做时,sub\u find\u devices()调用工作,但是在sub\u open()调用中,我得到了libusb错误-3,这表明我没有权限打开设备进行写入 我对这个问题做了一些研究,发

我正在尝试以RHEL5上的非root用户身份与USB设备接口。该设备是一个使用libusb-1.0的GPIO接口(其文档可在中找到)。使用API打开设备的步骤如下:

sub_device d;
d = sub_find_devices(0);
sub_handle h = sub_open(d);
当我这样做时,
sub\u find\u devices()
调用工作,但是在
sub\u open()
调用中,我得到了libusb错误-3,这表明我没有权限打开设备进行写入

我对这个问题做了一些研究,发现我应该创建一个udev规则。在设备的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"
然后,我在文件
/etc/udev/rules.d/991 local.rules
中创建了以下udev规则:

SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="ffc3", NAME="sub20", GROUP="582", MODE="0660"
582是我的普通用户所属的组的GID。我还尝试了使用组名的规则,但它不起作用。创建此规则后,将使用正确的权限创建设备文件
/dev/sub20
,但仅在插入设备时存在,这使我有理由相信udev规则在正确的设备上匹配。但是,我的代码仍然出现错误-3

对代码进行扫描后显示此调用:

open("/dev/bus/usb/002/006", O_RDWR)    = -1 EACCES (Permission denied)
/dev/bus/usb…
节点上的权限仍然是root:root,因此这可能表明我的udev规则存在问题,尽管我不知道可能是什么问题

如果我尝试调用
open(“/dev/sub20”,O_RDWR)
,我会得到返回值
ENXIO(没有这样的设备或地址)
,这是udev规则中另一个可能的错误指示器,尽管
/dev/sub20
文件显然以某种方式与正确的设备关联,因为它只在设备插入时存在


我还可以做些什么来让它工作呢?

我用来使用libusb访问设备的udev规则如下:
子系统==“usb”,ATTRS{idVendor}==“04d8”,ATTRS{idProduct}==“ffc3”,SYMLINK+=“sub20”,GROUP=“usb”,MODE=“660”
。它应该只是向设备添加一个符号链接,但之后权限也对我有效(我是usb组的成员)。

对于usb设备FTDI,我使用脚本:

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" 
然后以非root用户身份运行应用程序。它工作