-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-usb-device-udev-rule-instructions.txt
95 lines (67 loc) · 3.86 KB
/
create-usb-device-udev-rule-instructions.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Instructions on creating a udev rule for a USB device
===========================================================================================================
==============================================
Create the udev rule
==============================================
1. Get the info of all devices with "lsusb" and find the correct device.
Example:
"Bus 001 Device 004: ID 47c9:7b13 Motorola PCS"
bus: 001
device: 004
id vendor: 47c9
id product: 7b13
2. Create a udev rules file in /etc/udev/rules.d (e.g. 55-mydevice.rules). Note that all rules files are collectively
sorted and processed in lexical order.
3. Add the following rule in the file for a USB device using permissions 0550, group "systmdev" and the information
from "lsusb" (Note: SYMLINK is used to create a symlink to the device in /dev. E.g., the value "android%n" will
create symlink "/dev/android1"):
SUBSYSTEM=="usb", ATTR{idVendor}=="<id_vendor>", ATTR{idProduct}=="<id_product>", MODE="0550",
GROUP="systmdev", OWNER="<user>", SYMLINK+="<symlink>"
Use the following instead to run a command when the USB device is connected:
SUBSYSTEM=="usb", ATTR{idVendor}=="<id_vendor>", ATTR{idProduct}=="<id_product>", RUN+="<command>"
==============================================
Other
==============================================
----------------------------------------------
Helper commands
----------------------------------------------
# GENERAL
# List the udev attributes for a device.
# Example device names: "/dev/sda1"; "/dev/bus/usb/<device_directory>"
udevadm info -a -p "$(udevadm info -q path -n '<device name>')"
# Reload all udev rules.
# Note: This is not necessary after creating a new udev rule file as udev automatically detects file changes.
sudo udevadm control --reload
# TESTING UDEV RULES
# Test a udev rule.
# Note: This shows the effects of all keys of a rule except for RUN. To also see the effects of RUN, use
# "udevadm trigger" instead. This also removes old symlinks that have been changed.
sudo udevadm test "$(udevadm info -q path -n /dev/bus/usb/<device_directory>)" 2>&1
# Force udev to trigger all rules
# Note: This does not update smylinks or run the command in RUN.
sudo udevadm trigger
----------------------------------------------
Notes
----------------------------------------------
•A udev rule will run several times (e.g. over 100) for some USB devices connected during the boot process,
causing the command defined in the key RUN to run that many times. Also, if the "%n" modifier in SYMLINK is used, it
will create several symlinks (e.g. about 50) for the device, each with an incremented number appended. If
ENV{DEVTYPE}=="usb_device" is added, the udev rule will run a fraction of the number of times and create only a
fraction of the symlinks. It should be added for usb devices as it speeds up the boot process.
•To limit the number of times a command defined in key RUN runs to about once, create a container script with the
following code:
numProcsOpen="$(ps aux | grep -v 'grep profile/' | grep profile/mobiledevconnected.sh | wc -l)"
if [ "${numProcsOpen}" -lt 3 ]; then
# higher chance of "${numProcsOpen}" being less than 3 more than twice if it is lower than 2
sleep 2
...user commands...
fi
Note: Several other methods have been tried to make a udev rule run once for a USB device that is connected during
the boot process, including the use of a lock directory and modifying SIGCONT/SIGSTOP.
----------------------------------------------
Website references
----------------------------------------------
https://wiki.debian.org/udev
http://www.reactivated.net/writing_udev_rules.html#example-printer
http://www.janosgyerik.com/adding-udev-rules-for-usb-debugging-android-devices/
https://wiki.archlinux.org/index.php/udev