gthvidsten
Posts: 10
Joined: Fri Jul 31, 2015 8:52 am

Can't get udev to properly recognize USB device

Mon Jun 06, 2016 9:27 pm

I'm accessing a USB device using libusb, but I'm only able to do so if I start my application as root/sudo.
After churning through various sources I've found that I need to add a rule file to udev in /etc/udev/rules.d/ to be able to give permissions to other users.
I named this 50-MyRule.rules and added the following:

Code: Select all

SUBSYSTEM="usb",ATTRS{idVendor}=="abcd",ATTRS{idProduct}=="1234",MODE="0660",GROUP="mygroup",SYMLINK+="mydevice"
(where I have replaced idVendor and idProduct with the USB device's VID and PID)

After reloading (udev udevadm control --reload), rebooting, plugging the USB device in and out, no symlinks with the name mydevice shows up in /dev or anywhere else, the permissions in /dev/bus/usb/001/[USB bus number] are still only accessible by root, and I still have to start my application as root/sudo to get it to use the USB device.

Obviously I'm missing something but after an evening of googling and trying out small variations of the above I've had no luck and don't know where to go from here.

For extra information, this is the output for the USB device from the attributes in udevadm:

Code: Select all

looking at device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4':
    KERNEL=="1-1.4"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{bDeviceSubClass}=="ff"
    ATTR{bDeviceProtocol}=="ff"
    ATTR{devpath}=="1.4"
    ATTR{idVendor}=="abcd"
    ATTR{speed}=="12"
    ATTR{bNumInterfaces}==" 1"
    ATTR{bConfigurationValue}=="1"
    ATTR{bMaxPacketSize0}=="8"
    ATTR{busnum}=="1"
    ATTR{devnum}=="9"
    ATTR{configuration}=="Configuration Name"
    ATTR{bMaxPower}=="0mA"
    ATTR{authorized}=="1"
    ATTR{bmAttributes}=="c0"
    ATTR{bNumConfigurations}=="1"
    ATTR{maxchild}=="0"
    ATTR{bcdDevice}=="0110"
    ATTR{avoid_reset_quirk}=="0"
    ATTR{quirks}=="0x0"
    ATTR{version}==" 1.10"
    ATTR{urbnum}=="16"
    ATTR{ltm_capable}=="no"
    ATTR{manufacturer}=="Redacted"
    ATTR{removable}=="removable"
    ATTR{idProduct}=="1234"
    ATTR{bDeviceClass}=="ff"
    ATTR{product}=="Custom USB"
And this is what is in /var/log/syslog when I connect the device:

Code: Select all

Jun  6 20:49:10 raspberrypi kernel: [ 1685.527841] usb 1-1.4: new full-speed USB device number 11 using dwc_otg
Jun  6 20:49:10 raspberrypi systemd-udevd[109]: validate module index
Jun  6 20:49:10 raspberrypi systemd-udevd[109]: Check if link configuration needs reloading.
Jun  6 20:49:10 raspberrypi systemd-udevd[109]: seq 1023 queued, 'add' 'usb'
Jun  6 20:49:10 raspberrypi kernel: [ 1685.659179] usb 1-1.4: New USB device found, idVendor=abcd, idProduct=1234
Jun  6 20:49:10 raspberrypi kernel: [ 1685.659209] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=7
Jun  6 20:49:10 raspberrypi kernel: [ 1685.659226] usb 1-1.4: Product: Custom USB
Jun  6 20:49:10 raspberrypi kernel: [ 1685.659242] usb 1-1.4: Manufacturer: Redacted
Jun  6 20:49:10 raspberrypi systemd-udevd[109]: seq 1023 forked new worker [1297]
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: seq 1023 running
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: no db file to read /run/udev/data/c189:10: No such file or directory
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: IMPORT builtin 'usb_id' /lib/udev/rules.d/50-udev-default.rules:9
Jun  6 20:49:10 raspberrypi systemd-udevd[109]: seq 1024 queued, 'add' 'usb'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: IMPORT builtin 'hwdb' /lib/udev/rules.d/50-udev-default.rules:9
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f4b98 has devpath '/devices/platform/soc/3f980000.usb/usb1/1-1'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f4b98 filled with db file data
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: MODE 0664 /lib/udev/rules.d/50-udev-default.rules:41
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f5c20 has devpath '/devices/platform/soc/3f980000.usb/usb1'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f6070 has devpath '/devices/platform/soc/3f980000.usb'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f64b0 has devpath '/devices/platform/soc'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f68d0 has devpath '/devices/platform'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: handling device node '/dev/bus/usb/001/011', devnum=c189:10, mode=0664, uid=0, gid=0
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: set permissions /dev/bus/usb/001/011, 020664, uid=0, gid=0
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: creating symlink '/dev/char/189:10' to '../bus/usb/001/011'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: created db file '/run/udev/data/c189:10' for '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: passed -1 bytes to netlink monitor 0x556f3c28
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: seq 1023 processed with 0
Jun  6 20:49:10 raspberrypi systemd-udevd[109]: passed 319 bytes to netlink monitor 0x556f20c0
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: seq 1024 running
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: no db file to read /run/udev/data/+usb:1-1.4:1.0: No such file or directory
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f39e0 has devpath '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: IMPORT builtin 'hwdb' /lib/udev/rules.d/50-udev-default.rules:11
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f5910 has devpath '/devices/platform/soc/3f980000.usb/usb1/1-1'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f5f68 has devpath '/devices/platform/soc/3f980000.usb/usb1'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f67c8 has devpath '/devices/platform/soc/3f980000.usb'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f3f68 has devpath '/devices/platform/soc'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f2038 has devpath '/devices/platform'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: device 0x556f39e0 filled with db file data
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: RUN 'kmod load $env{MODALIAS}' /lib/udev/rules.d/80-drivers.rules:5
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: created db file '/run/udev/data/+usb:1-1.4:1.0' for '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: execute 'load' 'usb:v00E3p6324d0110dcFFdscFFdpFFic0Aisc00ip00in00'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: no module matches 'usb:v00E3p6324d0110dcFFdscFFdpFFic0Aisc00ip00in00'
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: passed -1 bytes to netlink monitor 0x556f3c28
Jun  6 20:49:10 raspberrypi systemd-udevd[1297]: seq 1024 processed with 0
Jun  6 20:49:13 raspberrypi systemd-udevd[109]: cleanup idle workers
Jun  6 20:49:13 raspberrypi systemd-udevd[109]: validate module index
Jun  6 20:49:13 raspberrypi systemd-udevd[109]: Check if link configuration needs reloading.
Jun  6 20:49:13 raspberrypi systemd-udevd[1297]: unload module index
Jun  6 20:49:13 raspberrypi systemd-udevd[1297]: Unloaded link configuration context.
Jun  6 20:49:13 raspberrypi systemd-udevd[109]: worker [1297] exit
Jun  6 20:49:13 raspberrypi systemd-udevd[109]: worker [1297] cleaned u
"Anything one man can imagine other men can make real."
- Jules Verne

User avatar
Paeryn
Posts: 2666
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Can't get udev to properly recognize USB device

Mon Jun 06, 2016 11:13 pm

gthvidsten wrote:I'm accessing a USB device using libusb, but I'm only able to do so if I start my application as root/sudo.
After churning through various sources I've found that I need to add a rule file to udev in /etc/udev/rules.d/ to be able to give permissions to other users.
I named this 50-MyRule.rules and added the following:

Code: Select all

SUBSYSTEM="usb",ATTRS{idVendor}=="abcd",ATTRS{idProduct}=="1234",MODE="0660",GROUP="mygroup",SYMLINK+="mydevice"
(where I have replaced idVendor and idProduct with the USB device's VID and PID)
You've got an error in the rule, that should be a == after SYBSYSTEM

Code: Select all

SUBSYSTEM=="usb",ATTRS{idVendor}=="abcd",ATTRS{idProduct}=="1234",MODE="0660",GROUP="mygroup",SYMLINK+="mydevice"
She who travels light — forgot something.

gthvidsten
Posts: 10
Joined: Fri Jul 31, 2015 8:52 am

Re: Can't get udev to properly recognize USB device

Tue Jun 07, 2016 7:28 am

See, I just knew there was something simple and silly!

I have now got a symlink in /dev named "mydevice" which points to /dev/bus/usb/001/004 (or whichever number the device receives). The 004 has the group owner I set in the rule.

What happens if I connect another device with the same VID/PID? Does it automatically create /dev/mydevice1 or do I need to do something else to make it support multiple devices?
"Anything one man can imagine other men can make real."
- Jules Verne

epoch1970
Posts: 3684
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Can't get udev to properly recognize USB device

Tue Jun 07, 2016 7:51 am

gthvidsten wrote:What happens if I connect another device with the same VID/PID? Does it automatically create /dev/mydevice1 or do I need to do something else to make it support multiple devices?
I've read quickly and I'm not sure what your device is, but you probably can use the %n or %k variables to handle multiple instances.
As in: '...SYMLINK+="mydevice-%n" ' or '... SYMLINK+="%n/mydevice" '

(A note to future readers: AFAIK there is a trap in udev, in that the environment variables are not created in one go. They are created by other rules and rules are called in lexicographic order.
As a result, a rule called "00-my_match.rules" might not work while the same rule renamed to "zz-my_match.rules" would, in case one of the environment variables you match against in the rule is created late.)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

gthvidsten
Posts: 10
Joined: Fri Jul 31, 2015 8:52 am

Re: Can't get udev to properly recognize USB device

Tue Jun 07, 2016 6:20 pm

Worked like a charm. Thank you! :)
"Anything one man can imagine other men can make real."
- Jules Verne

Return to “Raspbian”