Предоставить приложению / пользователю доступ к USB-устройству

1065
GTHvidsten

У меня есть приложение Mono, работающее на Raspbian, которое использует определенное USB-устройство через libusb. Если я обычно запускаю приложение, оно не может открыть устройство. Если я запускаю приложение как root (или через sudo), приложение успешно открывает устройство.

Как я могу дать приложению или пользователю надлежащие разрешения для устройства USB, чтобы приложение не запускалось с правами root?

3

1 ответ на вопрос

2
GTHvidsten

To give a specific group access to a USB device you have to add a rule to udev in /etc/udev/rules.d/. I added a file called 50-MyDevice.rules that contains this line:

SUBSYSTEM=="usb",ATTRS=="abcd",ATTRS=="1234",MODE="0660",GROUP="mygroup",SYMLINK+="mydevice%n" 

Where I have replaced the values after idVendor and idProduct with the USB device's VID and PID. Pay special attention to the number of equal signs after each parameter!

After this I rebooted linux.

Now, every time I plug in the USB device (or if I plug in several of the same device) I get a symlink called /dev/mydevice1 or /dev/mydevice2. This is a symlink pointing to /dev/bus/usb/001/001 (or in the second case /dev/bus/usb/001/002). The targets of these symlinks have the the proper mode and group permissions defined in the rules file.