usb_modeswitch: ver. 1.1.9 with data package 2011-08-05
[tomato.git] / release / src / router / usbmodeswitch / usb_modeswitch.sh
blobfad48555218032d2a8802ad157778942026f0f65
1 #!/bin/sh
2 # part of usb_modeswitch 1.1.9
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 case "$1" in
41 --driver-bind)
43 dir=$(ls -d /sys$2/ttyUSB* 2>/dev/null)
44 if [ ! -z "$dir" ]; then
45 exit 0
47 set +e
48 device_in "bind_list" $v_id $p_id
49 if [ "$?" = "1" ]; then
50 id_attr="/sys/bus/usb-serial/drivers/option1/new_id"
51 if [ ! -e "$id_attr" ]; then
52 /sbin/modprobe option 2>/dev/null || true
54 if [ -e "$id_attr" ]; then
55 echo "$v_id $p_id" > $id_attr
56 else
57 /sbin/modprobe -r usbserial
58 /sbin/modprobe usbserial "vendor=0x$v_id" "product=0x$p_id"
61 ) &
62 exit 0
64 --symlink-name)
65 device_in "link_list" $v_id $p_id
66 if [ "$?" = "1" ]; then
67 if [ -e "/usr/bin/tclsh" ]; then
68 exec /usr/bin/tclsh /usr/sbin/usb_modeswitch_dispatcher $1 $2 $v_id $p_id 2>/dev/null
71 exit 0
73 esac
74 exec 1<&- 2<&- 5<&- 7<&-
76 count=120
77 while [ $count != 0 ]; do
78 if [ ! -e "/usr/bin/tclsh" ]; then
79 sleep 1
80 count=$(($count - 1))
81 else
82 exec /usr/bin/tclsh /usr/sbin/usb_modeswitch_dispatcher "$@" 2>/dev/null &
83 exit 0
85 done
86 ) &
87 exit 0