-
Notifications
You must be signed in to change notification settings - Fork 474
Hi,
on Ubuntu 22.04 I need sudo for a test tool (Python bindings) when libhidapi-libusb is installed.
Enforcing libhidapi-hidraw (by uninstalling libusb backend) allows my tool to run without sudo.
Note: I have access rights to the /dev/hidrawX without sudo.
Thanks!
All reactions
Thanks Youw, your hints did stear me in the right direction.
My IRMP test code now works fine.
I attach the rules files in case someone finds this discussion with a search.
70-hsm-barcode-reader.rules.txt
70-irmp.rules.txt
PS: Answer to "How do you mean?" My example line in that post was only for a specific barcode reader model. The final udev rules do work with all models from Honeywell (HHP, Welch Allyn), at least the once I know (I am retired since 3 years).
Replies: 5 comments 3 replies
From main README:
Note that you will need to install an udev rule file with your application for unprivileged users to be able to access HID devices with hidapi. Refer to the 69-hid.rules file in the udev directory for an example.
All reactions
That's what I did in first place, did develop USB stuff since 1996 (with Linux since 2004).
The devices use "root plugdev" (owner group).
crw-rw---- 1 root plugdev 234, 0 Jan 15 22:13 /dev/hidraw0
Tools using the HidRaw backend work fine without sudo.
Only the UsbLib backend needs sudo (unfortunatelly this is prioritized from Python bindings).
Assumption is some path in /sys/* needs root root. It is that many paths are used in enumeration, probably one of it is the problem.
Double checked the rights for the .so files, these are the same to each other.
And some lib (dfUsbLib) I wrote some years ago also works fine without sudo (always worked), but that only used libudev, not libhidraw.
Next days I will analyze further, just had the hope that someone already knows about this issue.
Edit: Sure I am a member of group plugdev (since years on that PC).
All reactions
That's what I did in first place
If you'd share your udev .rules - that'd be a start for others to help. I had some for a few of my devices - everything worked for both hidraw and libusb backends. But for different backends different udev ruls are required.
Double checked the rights for the .so files, these are the same to each other.
Shouldn't be relevant.
but that only used libudev, not libhidraw.
Not sure what do you mean by libhidraw.
Note: libhidapi-libusb uses libusb under the hood, and nothing more. They have their own libusb FAQ, that one might be of help.
All reactions
69-hidraw.rules.txt
70-hsm-barcode-reader.rules.txt
Originally I wanted to write some python code for the project IRMP which is a receiver for IR-remotes. Finding the issues with sudo I switched to a device I know very well since I developed a big portion of it (barcode reader), especially the USB gadget drivers.
The 70-hsm-barcode-reader.rules.txt is for the barcode reader (Honeywell Xenon 1900). I wrote the udev rules for these devices some years ago as part of an SDK. For my tests the HidPos* interface was used. The relevant lines are 26-34.
- HidPos is short for "point of sales usage pages", A Document I contributed to about 1999. It can be found at usb.org.
The barcode reader works just fine with my own usb lib and the HidRaw backend of the HIDAPI.
Thanks, Dieter Fauth
Edit: Typo
All reactions
What I see from your .rules - you only provide access to the hidraw devices. That's suitable only for libhidapi-hidraw backend.
When libhidapi-libusb is used - HIDRAW is not used and those rules do not have any value. What you should do (at least) is to have an udev rule like:
ATTRS{idVendor}=="0c2e|0536", ENV{INTERFACETYPE}=="HIDPOS", SUBSYSTEMS=="usb", MODE="660", GROUP="plugdev"
NOTE1: SUBSYSTEMS=="usb"
NOTE2: I might be mistaken if it has to be SUBSYSTEM== or SUBSYSTEMS==
This is so you may access the USB device using libusb (or libhidapi-libusb for that matter) w/o sudo in a first place.
Only now I remember that in order to access the device properly, libhidapi-libusb needs to detech kernel driver. I'm not sure if that's possible w/o sudo at all. Need to try - its been a while since I last used such scenario.
works just fine with my own usb lib
I wounder what that might be.
All reactions
Hi,
ATTRS{idVendor}=="0c2e", ATTRS{idProduct}=="0907", SUBSYSTEM=="usb", MODE="660", GROUP="plugdev", SYMLINK+="hsmusb-$attr{serial}"
allows me to detach kernel driver w/o sudo.
No further tests done yet.
Note: This is not a universal solution, but is sufficient for my specific test.
The device is /dev/bus/usb/001/042 (at my test) and it allows access by plugdev menbers.
ls -l /dev/bus/usb/001/042
crw-rw---- 1 root plugdev 189, 41 Jan 22 16:47 /dev/bus/usb/001/042
Both backend libs were installed while testing (libhidapi-libusb0 and libhidapi-hidraw0).
Python code:
import libusb as usb
VID=0x0c2e
PID=0x0907
class Module():
def __init__(self, VID, PID):
self.usb = usb.init(None)
dev = usb.open_device_with_vid_pid(None, VID, PID)
if usb.kernel_driver_active(dev, 0):
print("Kernel has device, detaching...")
usb.detach_kernel_driver(dev, 0)
else:
print("No Kernel driver active")
usb.close(dev)
x = Module(VID, PID)
print("Done")
All reactions
ATTRS{idVendor}=="0c2e", ATTRS{idProduct}=="0907", SUBSYSTEM=="usb", MODE="660", GROUP="plugdev", SYMLINK+="hsmusb-$attr{serial}"allows me to detach kernel driver w/o sudo.
That is exactly what is expected/required by linux users to be able to use libhidapi-libusb. There is no other way. And that is linux limitation, not something HIDAPI may deside.
This is not a universal solution
How do you mean?
All reactions
Thanks Youw, your hints did stear me in the right direction.
My IRMP test code now works fine.
I attach the rules files in case someone finds this discussion with a search.
70-hsm-barcode-reader.rules.txt
70-irmp.rules.txt
PS: Answer to "How do you mean?" My example line in that post was only for a specific barcode reader model. The final udev rules do work with all models from Honeywell (HHP, Welch Allyn), at least the once I know (I am retired since 3 years).