| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# Rebind HID device to specific driver. |
| 4 |
# Author: Nikolai Kondrashov <[email protected]> |
| 5 |
# |
| 6 |
# To be used with udev rules like this one: |
| 7 |
# SUBSYSTEM=="hid", ACTION=="add", ENV{HID_ID}=="0003:0000172F:*", \ |
| 8 |
# RUN+="/usr/local/bin/hid-rebind" |
| 9 |
# |
| 10 |
# This program is free software: you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation, either version 2 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
# |
| 23 |
|
| 24 |
set -e -u |
| 25 |
|
| 26 |
export PATH="/sbin:/usr/sbin:$PATH" |
| 27 |
|
| 28 |
progname=`basename "$0"` |
| 29 |
|
| 30 |
# Write a string to a file, ignoring ENODEV. |
| 31 |
write_ignore_enodev() { |
| 32 |
local str="$1" |
| 33 |
local path="$2" |
| 34 |
local output |
| 35 |
|
| 36 |
# Write the string with tee, capturing error output |
| 37 |
if ! output=`echo "$str" | tee "$path" 2>&1 >/dev/null`; then |
| 38 |
# Raise anything except ENODEV |
| 39 |
if test "${output##*: }" != "No such device"; then |
| 40 |
echo "$output" >&2 |
| 41 |
return 1 |
| 42 |
fi |
| 43 |
fi |
| 44 |
} |
| 45 |
|
| 46 |
{ |
| 47 |
id=`basename "$DEVPATH"` |
| 48 |
|
| 49 |
cur_driver_path=`readlink -v -f "/sys${DEVPATH}/driver"` |
| 50 |
if test -e "$cur_driver_path"; then |
| 51 |
cur_driver=`basename "$cur_driver_path"` |
| 52 |
else |
| 53 |
cur_driver="" |
| 54 |
fi |
| 55 |
|
| 56 |
# Choose a matching module installed under /extra or /updates |
| 57 |
new_module=$( |
| 58 |
modprobe -R "$MODALIAS" | tr - _ | |
| 59 |
while read -r m; do |
| 60 |
if modinfo -F filename "$m" | |
| 61 |
grep -q '/extra/\|/updates/'; then |
| 62 |
echo "$m" |
| 63 |
break |
| 64 |
fi |
| 65 |
done |
| 66 |
) |
| 67 |
if test -z "$new_module"; then |
| 68 |
exit 0 |
| 69 |
fi |
| 70 |
# Assume the driver will be called the same as module, |
| 71 |
# but without the "hid_" prefix |
| 72 |
new_driver="${new_module#hid_}" |
| 73 |
new_driver_path="/sys/bus/hid/drivers/$new_driver" |
| 74 |
|
| 75 |
if test "$new_driver" = "$cur_driver"; then |
| 76 |
exit 0 |
| 77 |
fi |
| 78 |
|
| 79 |
logger -p daemon.notice -t "$progname" \ |
| 80 |
"rebinding $DEVPATH to \"$new_driver\" driver" |
| 81 |
|
| 82 |
# Ensure the new driver module is loaded |
| 83 |
modprobe "$new_module" |
| 84 |
|
| 85 |
# Unbind from the current driver, if any |
| 86 |
if test -e "$cur_driver_path"; then |
| 87 |
write_ignore_enodev "$id" "$cur_driver_path/unbind" |
| 88 |
fi |
| 89 |
|
| 90 |
# Bind to the new driver |
| 91 |
write_ignore_enodev "$id" "$new_driver_path/bind" |
| 92 |
} 2>&1 | logger -p daemon.warning -t "$progname" |