Get access to USB device on Linux (libusb-1.0)?
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"
linux / libusb get usb device path
Since libusb 1.0.12, they have introduced libusb_get_port_path(), and in 1.0.16 replaced it with libusb_get_port_numbers() which allows you to query the bus topology.
libusb cannot open USB device, permission isse. NetBeans/Ubuntu
Well you can run your application in root user mode and get rid with the permission
issue. However you should be aware about the consequences of running the program in
root user and should not be do until you fully understand the user/group permission
on UNIX based system. However if it is your test machine, i think you can do the
following(for program name ./a.out):
- Open the terminal
- Go to the directory where your program executable is present(check your netbeans creates the project and type
cd completepath
). sudo ./a.out
- Now command prompt would ask to enter root password
Now you should be able to run the program successfully.
However if you want to provide/change the permission of read/write/execute for a particular user you should try to understand about chmod command. You would have to change the permission to directory(/dev/bus/usb/002/) and any particular file residing under this directory. Hope this information would be useful.
libusb can't claim device
this is a problem with the configuration. in USB you have to obey the hierarchy
device descriptor - configuration descriptor - interface descriptor - endpoint descriptor
( http://www.beyondlogic.org/usbnutshell/usb1.shtml )
if there is an error on configuration level all other levels also fail
very likely the error is in this code :
mConfig.alternate = 0;
mConfig.config = 0;
mConfig.interface = 0;
mConfig.readEp = 0x81;
mConfig.writeEp = 0x02;
the problem is that i do not know the structure of the USB tree of your device so you have to do this. you can get the structure by lsusb -v
or usb-devices
and search your device for the structural ordering 'device - configuration - interface - endpoint'
if you have the information you have to modify the above code (i.e. changing interface index, alternate setting ,...)
the critical part of the source code is
int conf;
libusb_get_configuration(mDevHandle, &conf);
if (conf != mConfig.config) {
if (mDebug) qDebug("Configuration needs to be changed");
rc = libusb_set_configuration(mDevHandle, mConfig.config);
if (rc != 0) {
qWarning("Cannot Set Configuration");
this->printUsbError(rc);
return -3;
}
}
rc = libusb_claim_interface(mDevHandle, mConfig.interface);
if (rc != 0) {
qWarning("Cannot Claim Interface");
this->printUsbError(rc);
return -4;
}
source: https://github.com/fpoussin/QtUsb/blob/master/src/qlibusb.cpp
this is where the 'Cannot Claim Interface' error message comes from because there is a problem with setting the accurate configuration. as said try lsusb -v
or usb-devices
to get the correct information ...
Cannot open USB device with libusb-1.0 in cygwin
It turns out this was a kind of driver issue. I had to tell Windows to associate the particular device I'm using with the libusb drivers.
libusb-win32-1.2.6.0 comes with some tools to make that association (although you may need to configure your system to allow the installation of unsigned drivers).
There's one tricky bit. If you just want to associate the device with libusb, you can use the inf-wizard.exe
tool to make that association, but that will change the primary association to be with libusb. In my case, the device is a USB Audio Class device (i.e. USB sound card) that also has some libusb functionality. When I used inf-wizard.exe
, libusb started working (yay!), but then it stopped working as an audio device.
In my case, I needed to use the install-filter-win.exe
tool to install a filter driver for libusb. That allows the device to still show up as a USB Audio device, but also interact with it using libusb.
How to use libusb and libusb_get_device_descriptor()?
You didn't include a full, compilable test case. So I built one. This works for me on CentOS 6 x64. I'm also running this as a normal user account.
Source
#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>
int main() {
libusb_context *context = NULL;
libusb_device **list = NULL;
int rc = 0;
ssize_t count = 0;
rc = libusb_init(&context);
assert(rc == 0);
count = libusb_get_device_list(context, &list);
assert(count > 0);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = list[idx];
libusb_device_descriptor desc = {0};
rc = libusb_get_device_descriptor(device, &desc);
assert(rc == 0);
printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
}
libusb_free_device_list(list, count);
libusb_exit(context);
}
Output
Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0002
Vendor:Device = 8087:0020
Vendor:Device = 8087:0020
Vendor:Device = 0424:2514
Vendor:Device = 10c4:ea60
Vendor:Device = 051d:0002
Vendor:Device = 0624:0248
Related Topics
Is It Secure to Rely on "X-Forwarded-For" to Restrict Access by Ip in Apache While Using Cloudflare
Xfs - How to Not Modify Mtime When Writing to File
Cmakelist File to Generate Llvm Bitcode File from C Source File
No Output from Iio (Character) Device Output - Iio Buffer
Stty: Standard Input: Inappropriate Ioctl for Device
How to Get a Hex Color Code from a Solid-Color Image for a Script
How to Attach Domain Name to My Server
Print The Lines Between Two Dates from The Log Using Shell Command in Linux
How to Use Sysfs Inside Kernel Module
How to Implement Getthreadcontext in Linux/Unix
Correct Way to Export Multiple Ld_Library_Paths
Why 'Read' Command in Shell Script Is Missing Initial Characters
How to Have Postgresql Not Collapse Punctuation and Spaces When Collating Using a Language
Setgid Bit Not Preserved by Git on New Directory in '.Git' Folder
Linux/Cygwin Recursively Copy File Change Extension
Copying an Image to The Clipboard from Command Line
How to Type "Cargo Run" Without Needing to Set The Ld_Library_Path Shell Variable