-
Notifications
You must be signed in to change notification settings - Fork 175
/
hid-rebind
executable file
·92 lines (79 loc) · 2.68 KB
/
hid-rebind
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
#!/bin/sh
#
# Rebind HID device to specific driver.
# Author: Nikolai Kondrashov <[email protected]>
#
# To be used with udev rules like this one:
# SUBSYSTEM=="hid", ACTION=="add", ENV{HID_ID}=="0003:0000172F:*", \
# RUN+="/usr/local/bin/hid-rebind"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -e -u
export PATH="/sbin:/usr/sbin:$PATH"
progname=`basename "$0"`
# Write a string to a file, ignoring ENODEV.
write_ignore_enodev() {
local str="$1"
local path="$2"
local output
# Write the string with tee, capturing error output
if ! output=`echo "$str" | tee "$path" 2>&1 >/dev/null`; then
# Raise anything except ENODEV
if test "${output##*: }" != "No such device"; then
echo "$output" >&2
return 1
fi
fi
}
{
id=`basename "$DEVPATH"`
cur_driver_path=`readlink -v -f "/sys${DEVPATH}/driver"`
if test -e "$cur_driver_path"; then
cur_driver=`basename "$cur_driver_path"`
else
cur_driver=""
fi
# Choose a matching module installed under /extra or /updates
new_module=$(
modprobe -R "$MODALIAS" | tr - _ |
while read -r m; do
if modinfo -F filename "$m" |
grep -q '/extra/\|/updates/'; then
echo "$m"
break
fi
done
)
if test -z "$new_module"; then
exit 0
fi
# Assume the driver will be called the same as module,
# but without the "hid_" prefix
new_driver="${new_module#hid_}"
new_driver_path="/sys/bus/hid/drivers/$new_driver"
if test "$new_driver" = "$cur_driver"; then
exit 0
fi
logger -p daemon.notice -t "$progname" \
"rebinding $DEVPATH to \"$new_driver\" driver"
# Ensure the new driver module is loaded
modprobe "$new_module"
# Unbind from the current driver, if any
if test -e "$cur_driver_path"; then
write_ignore_enodev "$id" "$cur_driver_path/unbind"
fi
# Bind to the new driver
write_ignore_enodev "$id" "$new_driver_path/bind"
} 2>&1 | logger -p daemon.warning -t "$progname"