#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org 

START=20
EXTRA_COMMANDS='usb_pw'
EXTRA_HELP='usb_pw	usb_pw [usb1/usb2/all] on/off'

update_config () {
	echo "Update USB configuration" > /dev/console
	cat << EOF > /tmp/network_usb.bak
config 'interface' 'usb1'
	option 'ifname' ''
	option 'status' 'disable'
	option 'physical' 'usb'
	option 'port' 'USB1'
	option 'proto' '3g'
	option detect    2
	option netmask   '0.0.0.0'
	option device    '/dev/ttyUSB0'
	option pincode   ''
	option apn       'internet'
	option init_string1 'AT&F'
	option init_string2 'ATE0V1X1&D2&C1S0=0'
	option dial_string 'ATDT*99#'
	option username  ''
	option password  ''
	option is_56k    0
	option backup '0'
	option load_balance '0'
	option port 'USB1'

config 'interface' 'usb2'
	option 'ifname' ''
	option 'status' 'disable'
	option 'physical' 'usb'
	option 'port' 'USB2'
	option 'proto' '3g'
	option detect    2
	option netmask   '0.0.0.0'
	option device    '/dev/ttyUSB0'
	option pincode   ''
	option apn       'internet'
	option init_string1 'AT&F'
	option init_string2 'ATE0V1X1&D2&C1S0=0'
	option dial_string 'ATDT*99#'
	option username  ''
	option password  ''
	option is_56k    0
	option backup '0'
	option load_balance '0'
	option port 'USB2'

EOF
	cat /tmp/network_usb.bak >> /etc/config/network
	uci commit network
}

usb_pw(){
	usb_port="$3"
	usb_sw="$4"
	usb_dlatch=""
	usb_value=""

	if [ "$usb_sw" == "off" ]; then
		usb_ppp=$(ps | grep ppp | grep -v grep | awk '{print $1}')
		for usb_pid in $usb_ppp ;do
			if [ "$usb_port" == "all" ]; then
				usb_kill=$(cat /proc/$usb_pid/cmdline 2>/dev/null | grep usb1 -c)
				[ "$usb_kill" == "0" ] || {
					logger -p 160.5 -t hotplug.3gusb "kill ppp pid $usb_pid of usb1"
					echo "kill ppp pid $usb_pid of usb1" > /dev/console
					/bin/kill $usb_pid
				}
				usb_kill=$(cat /proc/$usb_pid/cmdline 2>/dev/null | grep usb2 -c)
				[ "$usb_kill" == "0" ] || {
					logger -p 160.5 -t hotplug.3gusb "kill ppp pid $usb_pid of usb2"
					echo "kill ppp pid $usb_pid of usb2" > /dev/console
					/bin/kill $usb_pid
				}
			else
				usb_kill=$(cat /proc/$usb_pid/cmdline 2>/dev/null | grep $usb_port -c)
				[ "$usb_kill" == "0" ] || {
					logger -p 160.5 -t hotplug.3gusb "kill ppp pid $usb_pid of $usb_port"
					echo "kill ppp pid $usb_pid of $usb_port" > /dev/console
					/bin/kill $usb_pid
				}
			fi
		done
	fi
	model=$(head -n 1 /etc/version)
	if [ "$model" = "Vigor2960" -o "$model" = "Vigor300B" -o "$model" = "Vigor2960F" ];then
		usb_dlatch="0x24000c00"
		if [ "$usb_port" == "usb1" -a "$usb_sw" == "off" ]; then
			usb_value="0x71717171"
		elif [ "$usb_port" == "usb2" -a "$usb_sw" == "off" ]; then
			usb_value="0xb1b1b1b1"
		elif [ "$usb_port" == "all" -a "$usb_sw" == "off" ]; then
			usb_value="0x31313131"
		else
			usb_value="0xf1f1f1f1"
		fi
	else	# for Vigor3900
		usb_dlatch="0x28018000"
		if [ "$usb_port" == "usb1" -a "$usb_sw" == "off" ]; then
			usb_value="0x3e3e3e3e"
		elif [ "$usb_port" == "usb2" -a "$usb_sw" == "off" ]; then
			usb_value="0x3d3d3d3d"
		elif [ "$usb_port" == "all" -a "$usb_sw" == "off" ]; then
			usb_value="0x3c3c3c3c"
		else
			usb_value="0x3f3f3f3f"
		fi
	fi

	echo "UUU usb_pw $model $usb_port $usb_sw" > /dev/console
	[ "$usb_sw" == "off" ] && {
		json delete network.$usb_port
		/etc/bind/restart_my_dns 4 $usb_port &
		uci -q -P /var/state revert network.$usb_port
	}
	echo "mmiotool -w $usb_dlatch $usb_value" > /dev/console
	mmiotool -w $usb_dlatch $usb_value
}
start() {
	[ -d /proc/bus/usb ] && {
		/bin/mount -t usbfs none /proc/bus/usb
	}
	new_usb_cf=$(cat /etc/config/network | grep usb1 -c)
	[ "$new_usb_cf" == "0" ] && update_config
}
