Using udev rules to create a symbolic link to USB mouse and keyboard

For some kind of application, we need to know exactly the keyboard event device name, such as /dev/input/event2. However, the "2" is related to the inserting sequence of USB input devices. In other words, we need a way to make this event device name as unique for application use.

To achieve this, we can use udev rules to create a symbolic links to existing event device name. The detailed steps is as below.

# Query mouse and keyboard info
We can use udevadm to query the properties of a event device name.

(Query the mouse device info)
udevadm info --query=property --name=/dev/input/event2

Result:
UDEV_LOG=3
DEVPATH=/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.4/1-1.4:1.0/input/input19/event2
MAJOR=13
MINOR=66
DEVNAME=/dev/input/event2
SUBSYSTEM=input
ID_INPUT=1
ID_INPUT_MOUSE=1
ID_VENDOR=0461
ID_VENDOR_ENC=0461
ID_VENDOR_ID=0461
ID_MODEL=USB_Optical_Mouse
ID_MODEL_ENC=USB\x20Optical\x20Mouse
ID_MODEL_ID=4d22
ID_REVISION=0200
ID_SERIAL=0461_USB_Optical_Mouse
ID_TYPE=hid
ID_BUS=usb
ID_USB_INTERFACES=:030102:
ID_USB_INTERFACE_NUM=00
ID_USB_DRIVER=usbhid
ID_PATH=platform-musb-hdrc.1-usb-0:1.4:1.0
DEVLINKS=/dev/char/13:66 /dev/input/by-id/usb-0461_USB_Optical_Mouse-event-mouse /dev/input/by-path/platform-musb-hdrc.1-usb-0:1.4:1.0-event-mouse

(Query the keyboard device info)
udevadm info --query=property --name=/dev/input/event4

Result:
UDEV_LOG=3
DEVPATH=/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.3/1-1.3:1.0/input/input20/event4
MAJOR=13
MINOR=68
DEVNAME=/dev/input/event4
SUBSYSTEM=input
ID_INPUT=1
ID_INPUT_KEY=1
ID_INPUT_KEYBOARD=1
ID_VENDOR=Dell
ID_VENDOR_ENC=Dell
ID_VENDOR_ID=413c
ID_MODEL=Dell_USB_Keyboard
ID_MODEL_ENC=Dell\x20USB\x20Keyboard
ID_MODEL_ID=2003
ID_REVISION=0301
ID_SERIAL=Dell_Dell_USB_Keyboard
ID_TYPE=hid
ID_BUS=usb
ID_USB_INTERFACES=:030101:
ID_USB_INTERFACE_NUM=00
ID_USB_DRIVER=usbhid
ID_PATH=platform-musb-hdrc.1-usb-0:1.3:1.0
DEVLINKS=/dev/char/13:68 /dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd /dev/input/by-path/platform-musb-hdrc.1-usb-0:1.3:1.0-event-kbd

# Create udev rules
vim /etc/udev/rules.d/98-input.rules
ENV{ID_INPUT_MOUSE}=="?*", SYMLINK+="input/hmi_mouse"
ENV{ID_INPUT_KEYBOARD}=="?*", SYMLINK+="input/hmi_kbd"
(save and exit)

# Tell udevd to reload rules
udevadm control --reload-rules

Now we can plug in a USB mouse or keyboard, we will see /dev/input/hmi_mouse or /dev/input/hmi_kbd is created and pointed to its associated event device name.

Note that if we plug in another new USB mouse or keyboard, the symbolic link will point to the newest event device name.

Ref: http://naiveprogrammer.blogspot.tw/2011/01/using-udev-to-catch-keyboard-and-mouse.html

留言

熱門文章