#! /bin/sh
#
# Startup script for USBnet (networking, instead of USB Mass Storage behaviour)
# Author: Kyller Gorgonio (based on Michael Mlivoncic usbnet script)

NAME=usbnet
DESC="USB Networking for Nokia Internet Tablets"
INITFILE=/etc/init.d/$NAME
G_ETHER=/mnt/initfs/lib/modules/`uname -r`/g_ether.ko
G_STORAGE=/mnt/initfs/lib/modules/`uname -r`/g_file_storage.ko
G_STORAGE_OPTS="stall=0 luns=2 removable"

# Start RNDIS/Ethernet networking device
run_insmod() {
	/sbin/insmod $1 $2
	if [ $? != 0 ] ; then
		echo "$NAME: insmod exited with failure" >> /tmp/usbnet
		return 1
	fi
	return 0
}

# Delay to let kernel do it's magic
yield() {
	sleep 1
}

# Unload any existing gadget drivers
unload() {
	rmmod $1
	if [ $? -ne 0 ]; then
		echo "$1: rmmod exited with failure" >> /tmp/usbnet
	fi
	yield
}

switch_to_usb_network () {

	# Disable ke-recv daemon.  Otherwise we end up
	# in loading g_file_storage module when USB
	# cable is connected which has to be avoided.
	if [ -x /etc/init.d/ke-recv ]; then
		/etc/init.d/ke-recv stop
	fi

	# remove g_file_storage module
	if grep -q "g_file_storage" /proc/modules ; then
		unload g_file_storage
	fi

	echo "loading g_ether" >> /tmp/usbnet
	if ! run_insmod $G_ETHER ; then
		exit 
	fi
	# Run insmod again if module insertion failed
	if ! grep -q "g_ether" /proc/modules ; then
		if ! run_insmod $G_ETHER ; then
			exit 
		fi
	fi

	# Take usb0 interface up
	echo "initializing usb0" >> /tmp/usbnet
	/sbin/ifup usb0

}

switch_to_file_storage () {
	
	# Take usb0 interface down
	if /sbin/ifconfig usb0 2>/dev/null | grep -q UP ; then
		echo "stop usb0" >> /tmp/usbnet
		/sbin/ifdown usb0
	fi

	# Remove g_ether module
	if grep -q "g_ether" /proc/modules ; then
		echo "removing g_ether" >> /tmp/usbnet
		unload g_ether
	else
		echo "g_ether not loaded" >> /tmp/usbnet
	fi

	echo "loading g_file_storage" >> /tmp/usbnet
	if ! run_insmod $G_STORAGE "$G_STORAGE_OPTS" ; then
		exit 
	fi
	# Run insmod again if module insertion failed
	if ! grep -q "g_file_storage" /proc/modules ; then
		if ! run_insmod $G_STORAGE "$G_STORAGE_OPTS" ; then
			exit 
		fi
	fi

	# Now we can enable ke-recv again
	if [ -x /etc/init.d/ke-recv ]; then
		/etc/init.d/ke-recv start
	fi
}

check_g_ether_status () {

	/sbin/lsmod | grep g_ether > /dev/null
	if [ $? = 0 ]; then
		echo "g_ether is loaded" >> /tmp/usbnet
		exit 0
	else
		echo "g_ether not loaded" >> /tmp/usbnet
		exit 1
	fi
}

switch_to_host () {
#	# remove g_file_storage module
#	if grep -q "g_file_storage" /proc/modules ; then
#		unload g_file_storage
#	fi
	
    echo host > /sys/devices/platform/musb_hdrc/mode
}

switch_to_otg () {
    echo otg > /sys/devices/platform/musb_hdrc/mode    
}

case "$1" in
start)
	echo "switching to USB network mode" >> /tmp/usbnet
	switch_to_otg
	switch_to_usb_network
	;;
stop)
	echo "switching back to USB Mass Storage mode" >> /tmp/usbnet
	switch_to_otg
	switch_to_file_storage
	;;
status)
	echo "checking usbnet status" > /tmp/usbnet
	check_g_ether_status
	;;
host)
    echo "changing usb to host mode" >> /tmp/usbnet
    switch_to_otg
    switch_to_host
    ;;
otg)
    echo "changing usb to otg mode" >> /tmp/usbnet
    switch_to_otg
    ;;
*)
	echo "usbnet syntax error" >> /tmp/usbnet
	printf "Usage: $INITFILE {start|stop|status}\n" >&2
	exit 1
	;;
esac

exit 0

