libusb cannot open USB device, permission isse. NetBeans/Ubuntu
Asked Answered
V

5

13

I'm writing a C/C++ application in NetBeans based on libusb-1.0 on Ubuntu 12.04. I can get basic information from the USB device (for example, the interface description) but I am not able to open the device. The function libusb_open gives me the error:

libusb:error [op_open] libusb couldn't open USB device /dev/bus/usb/002/003: Permission denied.
libusb:error [op_open] libusb requires write access to USB device nodes.

I understand that I need to change the permissions but I don't know how (I am a very basic Linux-user). Thank you!

Vaccaro answered 28/3, 2014 at 13:10 Comment(1)
thank you guys. I solved in a different way and it works. But I think it's a similar solution. In terminal I gave: sudo su Than FROM TERMINAL I run netbeans (the command to run it can be found by clicking on NetBeans icon with right click > Properties > Command) Compile/execute in Netbeans no restriction problem any longer. Maybe someone else can find it helpfulVaccaro
J
4

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.

Jamajamaal answered 28/3, 2014 at 13:58 Comment(1)
This is a long-winded answer to simply say - test run the command using sudo.Worldlywise
E
40

I think the best way to do this is to create a udev rules file for your devices. Simply create a text file names something like myVendor.rules and put the following text in it (where 1234 is your vendor ID:

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", MODE="0666"
SUBSYSTEM=="usb_device", ATTRS{idVendor}=="1234", MODE="0666"

Put this udev file in your /etc/udev/rules.d/ directory. This udev file will grant read and write access to ALL users, include non-privileged users, for ALL USB devices that have a matching Vendor ID. This means your device is accessible to non-root users even without modifying your executable or running it with sudo.

This udev example is specific to the idVendor, but you can restrict it to a VID and PID to be more strict. Check this article for writing udev rules for more information.

Enterovirus answered 28/3, 2014 at 21:23 Comment(4)
Thank you Preston. Only one small question, once I have the text file, how do I tell linux to read the rules?Vaccaro
Whoops, sorry - I've added that to the answer, you'll want to place the file in /etc/udev/rules.d/.Enterovirus
This didn't work for me until I added a colon to bit that sets the mode value: MODE:="0666". I also only needed the usb line, and not the usb_device one. I'm using Debian 7.7.Deangelis
I find it only working when I use lowercase for those hex numbers. Does anybody know why?Blarney
S
12

I think this might be a temporary solution for the problem while Preston's solution would work consistently.

1. Check which usb port is assigned for your device

You can figure out which usb port is assigned to your device by invoking ls command two times(first with device disconnected and second with device connected).

$ ls -l /dev/bus/usb/00*
/dev/bus/usb/001:
total 0
crw-rw-r-- 1 root root 189, 0  1월 10 12:08 001
crw-rw-r-- 1 root root 189, 1  1월 10 12:08 002

/dev/bus/usb/002:
total 0
crw-rw-r-- 1 root root 189, 128  1월 10 12:08 001
crw-rw-r-- 1 root root 189, 129  1월 10 12:08 002

/dev/bus/usb/003:
total 0
crw-rw-r-- 1 root root 189, 256  1월 10 12:08 001
crw-rw-r-- 1 root root 189, 257  1월 10 12:08 002
crw-rw-r-- 1 root root 189, 258  1월 10 12:08 003
crw-rw-r-- 1 root root 189, 259  1월 10 12:08 004
crw-rw-r-- 1 root root 189, 260  1월 10 12:08 005
crw-rw-r-- 1 root root 189, 263  1월 10 15:42 008 <-- this is your device

Let's say /dev/bus/usb/003/008 is your device.

2. Giving write permission for everyone(other)

According to the output of ls -l command, root user(group) has read/write permission on 003/008 port while other user has only read permission.

crw-rw-r-- 1 root root 189, 263  1월 10 15:42 008

You can allow every user to write on specific device using chmod command. While using chmod command, you will need sudo permission.

$ sudo chmod o+w /dev/bus/usb/003/008

or

$ sudo chmod a+w /dev/bus/usb/003/008

Now if you check the permission of usb, you have to see this output

$ ls -l /dev/bus/usb/003/008
crw-rw-rw- 1 root root 189, 263  1월 10 15:42 /dev/bus/usb/003/008

3. Everytime plugging it out or shutting down the system repeat step 1,2

If you plug the device out from usb port or shut down the system, what you did for your usb port will reset.

You have to repeat step 1,2 again.

This is why I'm saying my solution is temporary(volatile).

Further readings

I find these two blog articles would be helpful to your understanding.

Scandalmonger answered 10/1, 2016 at 7:8 Comment(0)
F
7

For every piece of equipment you want to communicate with you need to find out the idVendor (and idProduct if you want). This you can do by bash command lsusb. You get idVendor:idProduct pairs. Example:

$ lsusb
Bus 002 Device 005: ID 5345:1234 Owon PDS6062T Oscilloscope
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 046a:000c Cherry GmbH 
Bus 001 Device 004: ID 0480:a006 Toshiba America Inc External Disk 1.5TB
Bus 001 Device 003: ID 046a:0023 Cherry GmbH Keyboard
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

and then create a rule for it. The rule you place in a file that ends with '.rules' and put that file in either /etc/udev/rules.d/ or /lib/udev/rules.d/ directory:

  • The rules are processed alphabetically (so 70 comes after 60, etc.)
  • The /etc/udev/rules.d/ rules are processed after the /lib/udev/rules.d/ rules

To give an example, this worked for me for Owon oscilloscopes. I put this text in a file named 70-owon.rules in directory /lib/udev/rules.d/:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="5345", ATTRS{idProduct}=="1234", MODE="0666"

and I could communicate with the oscilloscope in my C program.

False answered 29/7, 2020 at 14:53 Comment(0)
M
6

After adding rule to /etc/udev/rules.d/ something like:

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="1234", MODE="0666", OWNER="YOU_USER_ID"

do not forget to reload rules with:

$ sudo udevadm control --reload-rules
Mccary answered 25/8, 2017 at 8:9 Comment(0)
J
4

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.

Jamajamaal answered 28/3, 2014 at 13:58 Comment(1)
This is a long-winded answer to simply say - test run the command using sudo.Worldlywise

© 2022 - 2024 — McMap. All rights reserved.