#!/bin/sh
# chkconfig: 2345 19 71
### BEGIN INIT INFO
# Provides:          ganges
# Required-Start:    modules
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: radio stop/start/restart script
# Description:       radio stop/start/restart script
# X-Start-Before:    network
# X-Stop-After:      network
# X-Timesys-Start-Number:  19
# X-Timesys-Stop-Number:  81
### END INIT INFO

#
# ganges radio stop/start/restart script
#
# $1 = command = stop|start|restart|start_eval|restart_eval
# $2/$3 = [mac] or [debug], in either order
#       [mac] = "mac=xx:xx:xx:xx:xx:xx" format
#       [debug] = "debug=<decimal value>" format

#
# wait $1 microseconds for file $2 to exist
# $1 is time in microseconds
# $2 is file to wait for
#
wait_us_fileexists() {
	wait_us=`expr $1`
	wait_interval_us=100000
	
	while [ $wait_us -gt 0 ]
	do
		usleep $wait_interval_us
		[ -e $2 ] && return 0;
		wait_us=`expr $wait_us - $wait_interval_us`
	done
	return 1
}

stop_radio_driver() {
	rmmod /lib/modules/rsi_master.ko
	rmmod /lib/modules/rsi_client.ko
}
stop_host_driver() {
	rmmod at91_mci
}
power_down() {
	/sbin/ltrx_ctrl -c WL_POWERDOWN
}
stop() {
	stop_radio_driver > /dev/null 2>&1
	stop_host_driver > /dev/null 2>&1
	power_down > /dev/null 2>&1
	usleep 50000
	return 0
}

power_up() {
	/sbin/ltrx_ctrl -c WL_POWERUP
}
start_host_driver() {
	insmod /lib/modules/`uname -r`/kernel/drivers/mmc/host/at91_mci.ko
	lrc=$?
	if [ "$lrc" -ne "0" ] ; then
		echo "Failed to insmod at91_mci host driver rc=$lrc"
		return 2
	fi
	wait_us_fileexists 500000 /sys/module/at91_mci
	if [ "$?" -ne "0" ] ; then
		echo "Failed to start at91_mci host driver"
		return 3
	fi
	wait_us_fileexists 1000000 /sys/bus/mmc/devices/mmc0:fffd
	if [ "$?" -ne "0" ] ; then
		echo "Failed to find radio via at91_mci host driver"
		return 4
	fi
}
start_radio_driver() {
	insmod /lib/modules/rsi_client.ko
	lrc=$?
	if [ "$lrc" -ne "0" ] ; then
		echo "Failed to insmod rsi_client driver rc=$lrc"
		return 5
	fi
	if [ "$3" -ne "" ] ; then
		insmod /lib/modules/rsi_master.ko mac_address=$1 driver_mode=$2 ganges_zone_enabled=$3
	else
		insmod /lib/modules/rsi_master.ko mac_address=$1 driver_mode=$2
	fi
	lrc=$?
	if [ "$lrc" -ne "0" ] ; then
		echo "Failed to insmod rsi_master driver rc=$lrc"
		return 6
	fi
	wait_us_fileexists 2000000 /proc/wlan0
	if [ "$?" -ne "0" ] ; then
		echo "Failed to start ganges radio driver"
		return 7
	fi
}
start() {
	power_up
	usleep 100000
	# powering up wlan will reset eth phy, so now we re-enable it
	/sbin/modprobe macb
	if [ -e /sys/class/net/bridge0 ] ; then
		iplink set eth0 down
		sleep 1
		iplink set eth0 up
		ifconfig eth0 up
		. /etc/sysconfig/network-scripts/ifcfg-eth0
		if [ "$ETHTOOL_OPTS" != "" ] ; then
			ethtool -s eth0 $ETHTOOL_OPTS
		fi
	fi
	start_host_driver
	lrc=$?
	if [ "$lrc" -ne "0" ] ; then
		stop
		eval return $lrc
	fi
	start_radio_driver $1 $2 $3
	lrc=$?
	if [ "$lrc" -ne "0" ] ; then
		stop
		eval return $lrc
	fi
}

restart() {
	stop
	# powering up wlan will reset eth phy, so disable to keep it in a good state
	/sbin/rmmod macb
	start $1 $2 $3
}

##############################################################################
# main...
#

# set mac and debug variables to empty
mac=""
debug=""

# set default wlan mac address from kernel command line
cat /proc/cmdline | grep wlanaddr=
if [ "$?" = "0" ] ; then
	mac=`cat /proc/cmdline | grep wlanaddr= | sed 's,.*wlanaddr=\(.*\),\1,' | cut -c 1-17`
fi

# check for optional mac and debug in $2
if [ -n "$2" ] ; then
	# first check if optional mac is passed
	echo $2 | grep mac= > /dev/null
	if [ "$?" = "0" ] ; then
		mac=`echo $2 | cut -c5-21`
	fi
	# next check if optional debug is passed
	echo $2 | grep debug= > /dev/null
	if [ "$?" = "0" ] ; then
		debug=`echo $2 | cut -c7-12`
	fi
fi

# check for optional mac and debug in $3
if [ -n "$3" ] ; then
	# first check if optional mac is passed
	echo $3 | grep mac= > /dev/null 
	if [ "$?" = "0" ] ; then
		mac=`echo $3 | cut -c5-21`
	fi
	# next check if optional debug is passed
	echo $3 | grep debug= > /dev/null
	if [ "$?" = "0" ] ; then
		debug=`echo $3 | cut -c7-12`
	fi
fi

case "$1" in
  stop)
	stop
	;;
  start)
	if [ "$mac" = "" ] ; then
		echo "no mac address available for ganges driver start"
		exit 1
	fi
	start $mac 1 $debug
	;;
  restart)
	if [ "$mac" = "" ] ; then
		echo "no mac address available for ganges driver start"
		exit 1
	fi
	restart $mac 1 $debug
	;;
  start_eval)
	if [ "$mac" = "" ] ; then
		echo "no mac address available for ganges driver start"
		exit 1
	fi
	start $mac 2 $debug
	;;
  restart_eval)
	if [ "$mac" = "" ] ; then
		echo "no mac address available for ganges driver start"
		exit 1
	fi
	restart $mac 2 $debug
	;;
  *)
	echo $"Usage: $0 {stop|start [mac]|restart [mac]|start_eval [mac]|restart_eval [mac]}"
	exit 1
esac

exit $?

