1#!/bin/sh
2# echo "This is a script to find the modem act TTY nodes out."
3
4
5modem_act_path=`nvram get usb_modem_act_path`
6modem_type=`nvram get usb_modem_act_type`
7modem_vid=`nvram get usb_modem_act_vid`
8modem_pid=`nvram get usb_modem_act_pid`
9usb_gobi2=`nvram get usb_gobi2`
10dev_home=/dev
11
12at_lock="flock -x /tmp/at_cmd_lock"
13
14
15_find_act_devs(){
16	act_devs=
17
18	ttyUSB_devs=`cd $dev_home && ls ttyUSB* 2>/dev/null`
19	if [ -n "$ttyUSB_devs" ]; then
20		tty_devs=`echo $ttyUSB_devs`
21	fi
22	ttyACM_devs=`cd $dev_home && ls ttyACM* 2>/dev/null`
23	if [ -n "$ttyACM_devs" ]; then
24		ttyACM_devs=`echo $ttyACM_devs`
25
26		tty_devs=$tty_devs" "$ttyACM_devs
27	fi
28	tty_devs=`echo $tty_devs |sed 's/$/ /g' |sed 's/ $//g'`
29
30	for dev in $tty_devs; do
31		path=`nvram get usb_path_$dev`
32		if [ "$path" == "$modem_act_path" ]; then
33			if [ -n "$act_devs" ]; then
34				act_devs=$act_devs" "$dev
35			else
36				act_devs=$dev
37			fi
38		fi
39	done
40
41	echo "$act_devs"
42}
43
44_find_in_out_devs(){
45	io_devs=
46
47	for dev in $1; do
48		t=`echo $dev |head -c 6`
49		if [ "$t" == "ttyACM" ]; then
50			if [ -n "$io_devs" ]; then
51				io_devs=$io_devs" "$dev
52			else
53				io_devs=$dev
54			fi
55
56			continue
57		fi
58
59		real_path=`readlink -f /sys/class/tty/$dev/device`"/.."
60		eps=`cd $real_path && ls -d ep_* 2>/dev/null`
61		if [ -z "$eps" ]; then
62			continue
63		fi
64
65		got_in=0
66		got_out=0
67		for ep in $eps; do
68			direction=`cat $real_path/$ep/direction`
69			if [ "$direction" == "in" ]; then
70				got_in=1
71			elif [ "$direction" == "out" ]; then
72				got_out=1
73			fi
74
75			if [ $got_in -eq 1 -a $got_out -eq 1 ]; then
76				if [ -n "$io_devs" ]; then
77					io_devs=$io_devs" "$dev
78				else
79					io_devs=$dev
80				fi
81
82				break
83			fi
84		done
85	done
86
87	echo "$io_devs"
88}
89
90_find_dial_devs(){
91	dial_devs=
92
93	got_acm=0
94	for dev in $1; do
95		t=`echo $dev |head -c 6`
96		if [ "$t" == "ttyACM" ]; then
97			got_acm=1
98			break
99		fi
100	done
101
102	count=0
103	for dev in $1; do
104		t=`echo $dev |head -c 6`
105		if [ $got_acm -eq 1 -a "$t" == "ttyUSB" ]; then
106			continue
107		fi
108
109		$at_lock chat -t 1 -e '' 'ATQ0' OK >> /dev/$dev < /dev/$dev 2>/dev/null
110		if [ "$?" == "0" ]; then
111			count=$((count+1))
112
113			if [ -n "$dial_devs" ]; then
114				dial_devs=$dial_devs" "$dev
115			else
116				dial_devs=$dev
117			fi
118		fi
119
120		if [ $count -eq 2 ]; then
121			break
122		fi
123	done
124
125	echo "$dial_devs"
126}
127
128_find_int_devs(){
129	int_devs=
130	first_dev=
131
132	for dev in $1; do
133		if [ -z "$first_dev" ]; then
134			first_dev=$dev
135		fi
136
137		real_path=`readlink -f /sys/class/tty/$dev/device`"/.."
138		eps=`cd $real_path && ls -d ep_* 2>/dev/null`
139		if [ -z "$eps" ]; then
140			continue
141		fi
142
143		for ep in $eps; do
144			attribute=`cat $real_path/$ep/bmAttributes`
145			if [ "$attribute" == "03" ]; then
146				if [ -n "$int_devs" ]; then
147					int_devs=$int_devs" "$dev
148				else
149					int_devs=$dev
150				fi
151
152				break
153			fi
154		done
155	done
156
157	if [ -n "$int_devs" ]; then
158		echo "$int_devs"
159	else
160		echo "$first_dev" # default
161	fi
162}
163
164_find_first_int_dev(){
165	int_devs=`_find_int_devs "$1"`
166
167	for dev in $int_devs; do
168		echo "$dev"
169		break
170	done
171}
172
173_find_noint_devs(){
174	noint_devs=
175
176	for dev in $1; do
177		real_path=`readlink -f /sys/class/tty/$dev/device`"/.."
178		eps=`cd $real_path && ls -d ep_* 2>/dev/null`
179		if [ -z "$eps" ]; then
180			continue
181		fi
182
183		got_int=0
184		for ep in $eps; do
185			attribute=`cat $real_path/$ep/bmAttributes`
186			if [ "$attribute" == "03" ]; then
187				got_int=1
188
189				break
190			fi
191		done
192
193		if [ $got_int -ne 1 ]; then
194			if [ -n "$noint_devs" ]; then
195				noint_devs=$noint_devs" "$dev
196			else
197				noint_devs=$dev
198			fi
199		fi
200	done
201
202	echo "$noint_devs"
203}
204
205_except_first_int_dev(){
206	first_int_dev=`_find_first_int_dev "$1"`
207	bulk_dev=
208
209	for dev in $1; do
210		if [ "$dev" != "$first_int_dev" ]; then
211			echo "$dev"
212			return
213		fi
214	done
215}
216
217# $1: VID, $2: PID.
218_get_gobi_device(){
219	if [ -z "$1" ] || [ -z "$2" ]; then
220		echo "0"
221		return
222	fi
223
224	if [ "$1" == "1478" ] && [ "$2" == "36902" -o "$2" == "36903" ]; then
225		echo "1"
226		return
227	fi
228
229	echo "0"
230}
231
232
233act_devs=`_find_act_devs`
234echo "act_devs=$act_devs."
235
236io_devs=`_find_in_out_devs "$act_devs"`
237echo "io_devs=$io_devs."
238
239is_gobi=`_get_gobi_device $modem_vid $modem_pid`
240
241if [ "$modem_type" == "tty" ] && [ "$modem_vid" == "6610" -o "$modem_vid" == "1032" ]; then # e.q. ZTE MF637U, ROYAL Q110.
242	first_int_dev=`_find_first_int_dev "$io_devs"`
243	echo "first_int_dev=$first_int_dev."
244
245	first_bulk_dev=""
246	echo "first_bulk_dev=$first_bulk_dev."
247elif [ "$usb_gobi2" != "" ] && [ "$is_gobi" == "1" ]; then # TM-AC1900v2
248	first_int_dev=`_find_first_int_dev "$io_devs"`
249	echo "first_int_dev=$first_int_dev."
250
251	first_bulk_dev=""
252	echo "Can't get the bulk node."
253else
254	dial_devs=`_find_dial_devs "$io_devs"`
255	echo "dial_devs=$dial_devs."
256
257	first_int_dev=`_find_first_int_dev "$dial_devs"`
258	echo "first_int_dev=$first_int_dev."
259
260	first_bulk_dev=`_except_first_int_dev "$dial_devs"`
261	echo "first_bulk_dev=$first_bulk_dev."
262fi
263
264nvram set usb_modem_act_int=$first_int_dev
265nvram set usb_modem_act_bulk=$first_bulk_dev
266
267