Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get access to USB device on Linux (libusb-1.0)?

I am writing a small program to communicate with a specific USB HID product (identified by vendor and product IDs), using libusb-1.0 on Linux. Right now, I have to run the program as root because "libusb requires write access to USB device nodes". Is there a way to change the permissions on the device I need so that I don't need to run my program as root?

like image 974
erjiang Avatar asked Nov 29 '10 04:11

erjiang


People also ask

What is libusb in Linux?

libusb is a library that provides applications with access for controlling data transfer to and from USB devices on Unix and non-Unix systems, without the need for kernel-mode drivers.

What is libusb win32 devices?

description: libusb-win32 is a port of the USB library libusb 0.1 to the Microsoft Windows operating systems. The library allows user space applications to access many USB device on Windows in a generic way without writing a line of kernel driver code.

How does libusb work?

libusb is a C library that provides generic access to USB devices. It is intended to be used by developers to facilitate the production of applications that communicate with USB hardware. It is portable: Using a single cross-platform API, it provides access to USB devices on Linux, macOS, Windows, etc.

What is libusb Dev?

libusb is an open source library that allows you to communicate with USB devices from user space. For more info, see the libusb homepage. This documentation is aimed at application developers wishing to communicate with USB peripherals from their own software.


1 Answers

On modern Linux systems, udevd (man 7 udev) creates the device nodes for USB devices when they're plugged in. Add a udev rule that matches your device (eg. you could match by USB Vendor and Product IDs), and sets the OWNER / GROUP / MODE of the device node.

The best approach is probably to create a new group for users who should be able to access the device, then set that as the group owner in the udev rule. You may also need to use MODE to ensure that it has group read/write permissions. Eg. your rule will probably look something like:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffee", ATTRS{idProduct}=="5a5a", MODE="0660", GROUP="foobar"
like image 72
caf Avatar answered Oct 16 '22 10:10

caf