libusb calls without sudo using udev-rules - linux

Tested on Kubuntu 16.04 64 bit only. I have an application which source is not under my control. It uses some libusb calls which ends up in e.g.:
libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/031: Permission denied
libusb: error [_get_usbfs_fd] libusb requires write access to USB device nodes.
When running the above mentioned application as root, it works as expected.
When I change the permissions of the regarding file like:
sudo chmod a+w /dev/bus/usb/001/031
then the application will work with standard user rights (until I disconnect / reconnect my usb device).
Now I'm looking for a way, to e.g. automatically execute the chmod a+w each time when the specific usb device is plugged in.
Might this be possible by writing a specific udev rule?
Maybe other solutions the libusb calls without root rights?
Solution: Based upon David Grayson's answer, I'd now added an additional line with SUBSYSTEM=="usb" to my rule file. My rules file now finally looks like this:
SUBSYSTEM=="tty", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"
SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"

I suggest that you add a new file in /etc/udev/rules.d named usb.rules. It should have the following contents:
SUBSYSTEM=="usb", MODE="0666"
This will make all USB devices readable and writable by all users.
You could also narrow it down to specific USB devices using idVendor and idProduct attributes mentioned in Ignacio's answer.

Assuming Kubuntu 16.04 uses PolicyKit, put the following in a file in /etc/udev/rules.d, naming it similarly to the files that already exist there:
ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="xxxx", TAG+="uaccess", ENV{ID_MM_DEVICE_IGNORE}="1"
Replace the two sets of "xxxx" with the vendor ID and product ID of the device respectively.

Oldie but goldie. Help me to solve my issue with sharing the usb with virtual machines under AQEMU. Thanks a lot.
I added to the /etc/udev/rules.d file usb.rules with this line
SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"
and virtual machine can see USB stick being connected life.

Related

How to automatically run 'sudo modprobe -r ftdi_sio' whenever the device is plugged into my computer

I have a USB device that I'm using and I'm developing an application using WebUSB with Google Chrome.
The thing is whenever I plug the USB device into my Linux computer, I have to manually run sudo modprobe -r ftdi_sio to unload it.
I want it to be unloaded automatically whenever I plug that device into my computer instead of having to type it manually every single time.
Any ideas on how this could be implemented ? Help would be much appreciated
One option would be to "blacklist" the ftdi_sio module to stop it being loaded automatically. To do that create the following file:
/etc/modprobe.d/ftdi_sio-blacklist.conf
# This is a comment. Change it if you want.
blacklist ftdi_sio
Put your command in /etc/rc.local and restart. See if it works. Or you can find how other .ko are configured to automatically load during system startup. Follow the same to make your module load automatically.
The proper way is to create a udev rule that is triggered when the specific USB device is attached.
Create a file /etc/udev/rules.d/99-usb-load.rules, and replace the "7523" and "1a86" with the Product ID and Vendor ID of your USB device.
# For debugging if the rule is working
ACTION=="add", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/bin/sh -c '/bin/echo inserted device >> /tmp/udev_file'"
ACTION=="remove", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/bin/sh -c '/bin/echo removed device >> /tmp/udev_file'"
# Actual rules
ACTION=="add", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/sbin/rmmod ftdi_sio"
ACTION=="remove", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+=""
Restart the udev daemon
sudo /etc/init.d/udev restart
The add rule will be executed whenever the USB device with matching PID/VID is attached and it will unload the module ftdi_sio. The first two rules are for debugging purposes which write a line into /tmp/udev_file and can be used to verify if the rules has been triggered.
Looks like USB device is new and there are no existing drivers to handle as soon as it is plugged in. You need an interrupt line and a USB driver code for your requirement. You need to register your device to that driver and driver to the USB bus. Also need to write appropriate interrupt routines to be called as soon as your device is plugged in. This will make it work as you want !!
If you already have a .rules file for the USB device then append the following to the pre-existing file in the /etc/udev/rules.d/ directory. Otherwise, create a file in the /etc/udev/rules.d/ directory with the following content:
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", ACTION=="add", RUN+="/sbin/rmmod ftdi_sio"
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", ACTION=="remove", RUN+=""
This means that when the device with vendorID 0403 and product ID 6010 is plugged in it runs /sbin/rmmod ftdi_sio which removes the ftdi_sio module. The second rule means nothing will be run when unplugged. See [writing udev rules] for more info on how to name the .rules file (e.g. 99-mydevice.rules)
You'll have to replace 0403 and 6010 with your device's vendor ID and prodcut ID. The vendor ID and product ID can be found by running lsusb on the command line after plugging in the USB device. It will have the format:
Bus xxx Device xxx: ID idVendor:idProduct ManufacturerName
After creating or editing the .rules file in /etc/udev/rules.d/ directory be sure to reload the .rules file with the following command:
sudo udevadm control --reload
Some further references on udev rules:
debian wiki
arch wiki
writing udev rules

Change default permission of device files

I have written my gpio driver and register it with the system using udev. This created all my device files but the permission they are created is 600. how can I change this default permission to 666 or any other. Since I have to run my program reading this file using sudo.
Should i write any rules in udev.. please explain.
Or should i change operating permission of my program within my program itself to root.
Update: my udev rules.d contains
40-scratch.rules:
ATTRS{idVendor}=="0694", ATTRS{idProduct}=="0003", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
and 99-input.rules:
SUBSYSTEM=="input", GROUP="input", MODE="0660"
and udev.conf:
udev_log="err"

IOIO-OTG connect PC udev rules for linux fails to detect

The rules file is called 50-ioio.rules and the the text is:
ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", SYMLINK+="IOIO%n", MODE="666"
I copied this file to the udev rules directory using:
sudo cp 50-ioio.rules /etc/udev/rules.d
I then restarted udev using:
sudo restart udev
However when I connect the IOIO board via a USB cable and look for the serial port with
ls /dev/IOIO*
It is not being created? I cant find any errors in syslog or anything in dmesg or lsusb - I suspect the udev string is wrong but it is in the documentation for the device?
I restarted the whole box and it got discovered, its not the real answer but at least I can progress

Run command after mount/unmout usb pen drive

I have Raspberry Pi (with Raspbian) and using it as DLNA/UPnP server and renderer. I run minidlna as DLNA server and i have some media files on USB.
I would like to automaticaly rebuild DLNA DB when drive is mounted and unmounted. This is done by command:
sudo service minidlna force-reload
Is threre any way how to autorun this command?
BTW I use "USBmount" package for automount USB drives.
Thanx
You can do this using the tool usbmount.
It has the possibility to add scripts that will be run on mount/umount events in /etc/usbmount/mount.d/ and /etc/usbmount/umount.d/.
Start by finding your device in lsusb. Note the ID (eg 12f5:a91a)
Create a new udev rules file in /etc/udev/rules.d/ eg /etc/udev/rules.d/100-my-mount.rules and write a new rule in there like this:
ACTION=="add", ATTRS{idVendor}=="12f5", ATTRS{idProduct}=="a91a", RUN+="/home/your_username/bin/my-mount-script.sh"
For unmounted device use ACTION=="remove" in rule and another script.

Run something when USB device is plugged in doesn't work

I made a script in /etc/udev/rules.d/local.rules
SUBSYSTEM=="usb", SYSFS{idVendor=="b58e"}, SYSFS{idProduct=="9e84"}, ACTION=="add", RUN+="notify-send USB"
I then reload udev with
sudo udevadm control --reload-rules
I've tried to remove everything but subsystem and run. I've tried the run '=' instead of '+=', I've tired ATTR instead of SYSFS. I tried "sudo service udev restart" and "sudo reload udev". I unplug the device, then plug it in again and it does not run the action. I tried renaming it 70-local.rules and changing permissions to a+x. I've tried changing 'subsystem' to 'bus'. I've tried setting run to be "/path/test.sh" which has the same command.
I'm not an expert and this isn't an answer, but I've found the following steps useful in identifying the appropriate attributes to trigger on:
Locate the device path using udevadm, lsusb, or usb-devices. I normally just use lsusb and let tab completion in my shell guide me. In my case, the path is /dev/bus/usb/003/007.
Use udevadm to identify the device attributes for rule writing. In my case, I used udevadm info -a --attribute-walk --root --name=/dev/bus/usb/003/007.
Write the rule and check that it's triggering. In my case, I'm just changing the device owner to user "stephen" and it's very easy for me to check if it's working by using ls -l /dev/bus/usb/003/007. My rule for this case looks like: SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", OWNER="stephen". I have a similar rule that puzzled me for a little while because the subsystem was expecting ATTRS not ATTR, which is why I recommend walking the attributes. The rule in this latter case became: `SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", OWNER="stephen".
And, of course, man udev is always helpful. As you said, you should struggle to identify that your rule is triggering properly and may be best off just doing a quick ownership change on the device as I did for a first step. You can run into trouble with bad attributes or symbolic links sometimes and it's
it does not run the action
No, it runs the action. The problem is that it doesn't know where to send the notification, since there's no notification framework running when udev starts. You will need to send a DBus message across the system bus and have a user daemon catch the message and post a notification instead.

Resources