usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / usbmodeswitch / usb_modeswitch.sh
blob8e9ba74df3e4fcff650b2297dc467f0f5c4ad8f9
1 #!/bin/sh
2 # part of usb_modeswitch 1.2.6
3 device_in()
5 if [ ! -e /var/lib/usb_modeswitch/$1 ]; then
6 return 0
7 fi
8 while read line
9 do
10 if [ $(expr "$line" : "$2:$3") != 0 ]; then
11 return 1
13 done </var/lib/usb_modeswitch/$1
14 if [ $(expr "$line" : "$2:$3") != 0 ]; then
15 return 1
17 return 0
20 if [ $(expr "$1" : "--.*") ]; then
21 p_id=$4
22 if [ -z $p_id ]; then
23 prod=$5
24 if [ -z $prod ]; then
25 prod=$3
27 prod=${prod%/*}
28 v_id=0x${prod%/*}
29 p_id=0x${prod#*/}
30 if [ "$v_id" = "0x" ]; then
31 v_id="0"
32 p_id="0"
34 v_id="$(printf %04x $(($v_id)))"
35 p_id="$(printf %04x $(($p_id)))"
36 else
37 v_id=$3
40 PATH=/sbin:/usr/sbin:$PATH
41 case "$1" in
42 --driver-bind)
44 dir=$(ls -d /sys$2/ttyUSB* 2>/dev/null)
45 sleep 1
46 if [ ! -z "$dir" ]; then
47 exit 0
49 set +e
50 device_in "bind_list" $v_id $p_id
51 if [ "$?" = "1" ]; then
52 id_attr="/sys/bus/usb-serial/drivers/option1/new_id"
53 if [ ! -e "$id_attr" ]; then
54 modprobe option 2>/dev/null || true
56 if [ -e "$id_attr" ]; then
57 echo "$v_id $p_id ff" > $id_attr
58 else
59 modprobe -r usbserial
60 modprobe usbserial "vendor=0x$v_id" "product=0x$p_id"
63 ) &
64 exit 0
66 --symlink-name)
67 device_in "link_list" $v_id $p_id
68 if [ "$?" = "1" ]; then
69 if [ -e "/usr/sbin/usb_modeswitch_dispatcher" ]; then
70 exec usb_modeswitch_dispatcher $1 $2 2>>/dev/null
73 exit 0
75 esac
76 exec 1<&- 2<&- 5<&- 7<&-
78 count=120
79 while [ $count != 0 ]; do
80 if [ ! -e "/usr/sbin/usb_modeswitch_dispatcher" ]; then
81 sleep 1
82 count=$(($count - 1))
83 else
84 exec usb_modeswitch_dispatcher --switch-mode $1 $0 &
85 exit 0
87 done
88 ) &
89 exit 0