Udev

From HalfgeekKB
Jump to navigation Jump to search

Setting permissions for an arbitrary device

This is the basic info on how I got an ST-Link 2 clone to be accessible to users in a specific group. The platform is Linux Mint 17.3 on x64.

General rule

The general form of the rule is

SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="<vendor ID hex>", ATTRS{idProduct}=="<product ID hex>", MODE:="<4-digit octal permissions>", GROUP:="<owning group name>"

Take careful note of the equals symbols used; the matching parts of the rule use a double-equal operator == while the assignments use a colon-equal operator :=. (Some others' examples also have a single-equal operator = for assignments, but that notation didn't work in my environment.)

This rule is to be placed in a file /etc/udev/rules.d/<two-digit priority number>-<description>.rules. The two-digit priority number is present for the express purpose of placing the filename at a given point in alphabetical sorting order.

For more information about what this actually does, see documentation for udev rules.

Example

Use lsusb to determine the vendor id and product id. For this example, the output contained the line:

$ lsusb
...
Bus 006 Device 020: ID 0483:3748 STMicroelectronics ST-LINK/V2
...

Here, the device ID is two 16-bit hex values separated by a colon: 0483:3748. The left and right parts are the vendor ID and the product ID, respectively.

I've decided that the group which owns the device is called usbusers, and to this end I have created the group, added myself to the group, then logged out and back in for the change to be effective.

I've decided I want to limit access to the owner (root) and group, so I've chosen the permissions 0660. To allow access to all users, set the mode to 0666 and don't bother with the GROUP clause.

The rules file will be named 45-psm-stlink.rules, having the arbitrarily chosen priority of 45.

/etc/udev/rules.d/45-psm-stlink.rules:

SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE:="0660", GROUP:="usbusers"

Once the rules file is in place, restart the udev service.


Check the work

Detach (if attached) then reattach the device. Run lsusb once again to see the updated bus and device number:

$ lsusb
...
Bus 006 Device 021: ID 0483:3748 STMicroelectronics ST-LINK/V2
...

To examine whether the permissions change took, do

ls -l /dev/bus/usb/<bus number>/<device number>

For the bus 006 and device 021:

$ ls -l /dev/bus/usb/006/021
crw-rw---- 1 root usbusers 189, 660 Dec  4 10:46 /dev/bus/usb/006/021

Note that the group is usbusers and that only the user and group have access; our rule has worked.

udev rules for Maple Mini development

As specified in the stm32duino doc:

/etc/udev/rules.d/45-maple.rules:

ATTRS{idProduct}=="1001", ATTRS{idVendor}=="0110", MODE="664", GROUP="plugdev"
ATTRS{idProduct}=="1002", ATTRS{idVendor}=="0110", MODE="664", GROUP="plugdev"
ATTRS{idProduct}=="0003", ATTRS{idVendor}=="1eaf", MODE="664", GROUP="plugdev" SYMLINK+="maple"
ATTRS{idProduct}=="0004", ATTRS{idVendor}=="1eaf", MODE="664", GROUP="plugdev" SYMLINK+="maple"

...