Get Access to Usb Device on Linux (Libusb-1.0)

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):

  1. Open the terminal
  2. Go to the directory where your program executable is present(check your netbeans creates the project and type cd completepath).
  3. sudo ./a.out
  4. 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



Leave a reply



Submit