-
Notifications
You must be signed in to change notification settings - Fork 0
/
plug-usb.sh
executable file
·60 lines (55 loc) · 1.36 KB
/
plug-usb.sh
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
#!/bin/sh
set -e
init_args() {
if [ -z "$3" ]; then
echo "Software connect/disconnect USB device"
echo "Usage: $0 in|out <device-VID> <device-PID>"
exit 1
fi
vid=$2
pid=$3
idfile=/tmp/plug-usb-$vid-$pid
[ "$(whoami)" = "root" ] || SUDO=sudo
}
find_device() {
cd /sys/bus/usb/drivers/usb
for dir in $(ls -d *-*)
do
[ "$(cat $dir/idVendor)" = "$vid" ] && [ "$(cat $dir/idProduct)" = "$pid" ] && echo $dir && return
done
}
plug_in() {
if [ ! -f $idfile ]; then
lsusb -d $vid:$pid > /dev/null
local rc=$?
if [ $rc -eq 0 ]; then
echo "The device $vid:$pid is already plugged in. Quit"
exit 11
else
echo "Cannot find plug-out record of device $vid:$pid. Quit"
exit 12
fi
fi
local num=$(cat $idfile)
if [ -z "$num" ]; then
echo "Cannot find device $vid:$pid. Quit"
exit 13
fi
echo "Found device $vid:$pid at $num"
echo "Plug in device $num"
echo $num | $SUDO tee /sys/bus/usb/drivers/usb/bind > /dev/null
$SUDO rm $idfile
}
plug_out() {
local num=$(find_device $vid $pid)
if [ -z "$num" ]; then
echo "Cannot find device $vid:$pid (already plugged out?). Quit"
exit 21
fi
echo "Found device $vid:$pid at $num"
echo "Plug out device $num"
echo $num | $SUDO tee /sys/bus/usb/drivers/usb/unbind > /dev/null
echo $num | $SUDO tee $idfile > /dev/null
}
init_args $*
plug_$1