#!/bin/sh
# A script which installs wifi

##############################
#Global Variables
##############################

# Directory where WiFi Driver is installed
MDIR="/lib/modules/`uname -r`/kernel/net/wifi"

# check if its XScale Processor
IS_XSCALE=$(cat /proc/cpuinfo | grep -e "Processor.*:.*XScale-PXA270.*" -c)

IS_WIFI_PRESENT=$(cat /proc/modules | grep -e ".*sd8xxx.*- Live.*" -c)
IS_SDIO_PRESENT=$(cat /proc/modules | grep -e ".*sdio.*- Live.*" -c)

NODE="`uname -n`"
ZEPFLOYD="zepfloyd"
CASTLE="castle"
JOPLINEMU2="joplinemu2"

# @todo -- allow the args below to be set via command-line

#-----------------------------
#OMAP SDIO Configuration parameters:
#	param_sdio_bus_width and
#	param_use_sdio_irq
#
# these parameters are passed
# as WiFiDriver module arguments
# on the insmod command line.
#
# They are initially only supported
# in the OMAP build of the WiFi Driver
#
#
# Comment them out for driver-default behavior
#-----------------------------

#param_sdio_bus_width: allowed values are
#	0 = keep negotiated bus width(don't change)
#	1 = force 1-bit SDIO bus width
#	4 = force 4-bit SDIO bus width
OMAP_SDIO_BUSWIDTH_ARGS="param_sdio_bus_width=0"

#param_use_sdio_irq: allowed values are
#	0 = use GPIO interrupt mode
#	1 = use SDIO interrupt mode
OMAP_SDIO_INTMODE_ARGS="param_use_sdio_irq=0"


# module parameter: param_sd_slot_selector - SDIO slot number.
# Allowed values:
# 0 (on-board SDIO slot for Scrambler module)
# 1 (the extra SDIO socket on Joplin)
# 
# @todo: this is a temporary work-around for selecting the correct slot
# when registering for SDIO interrupts.  This is necessary due to a flash
# in the WindRiver SDIO interrupt-related API functions that take a slot
# number instead of mmc_card pointer to identify the SDIO slot.  Using 
# the mmc_card pointer will make the slot selection automatic because we
# receive the mmc_card pointer (not to be confused with marvell's mmc_card_t)
# in the SDIO probe callback.  This parameter and related code should be
# removed when WindRiver fixes the API's (a bug has been filed in WindRiver's
# TRACE system for these API's).
#
OMAP_SDIO_SLOT_ARGS="param_sd_slot_selector=0" 


############################## 
#FUNCTIONS  
##############################
check_error () {
    CHECK_ERROR_CODE=$?
    if [ "$CHECK_ERROR_CODE" -ne 0 ]
    then
	echo "!!!!!!!!!!!!ERROR err code = $CHECK_ERROR_CODE !!!!!!!!!!"
	exit 1
    fi
}

configure_wifi () {

    echo "Setting up the antenna on the Scrambler ..."

    if [ "$NODE" == "$CASTLE" ]
    then
	echo "Switch settings for castle"

        wlanconfig eth0 wrmac 0xa58c 0x00018000
        check_error

        wlanconfig eth0 wrmac 0xa5a0 0x0000d21c
        check_error

        wlanconfig eth0 wrmac 0xa5f0 0xa027801d
        check_error
    fi

    if [ "$NODE" == "$ZEPFLOYD" ]
    then
	echo "Switch settings for zepfloyd"

        wlanconfig eth0 wrmac 0xa58c 0x00040214
        check_error

        wlanconfig eth0 wrmac 0xa5a0 0x0000d24d
        check_error

        wlanconfig eth0 wrmac 0xa5f0 0xa227181c
        check_error
    fi

    if [ "$NODE" == "$JOPLINEMU2" ]
    then
	echo "Switch settings for joplinemu2"

        wlanconfig eth0 wrmac 0xa58c 0x00040214
        check_error

        wlanconfig eth0 wrmac 0xa5a0 0x0000d24d
        check_error

        wlanconfig eth0 wrmac 0xa5f0 0xa227181c
        check_error
    fi
    echo "DONE: Antenna Configured For Scrambler .."

}

install_sdio_driver () {

    #install the marvell sdio driver
    echo "Installing the Marvell SDIO driver ..."

    insmod $MDIR/sdio.ko
    if [ "$?" -ne 0 ]
    then
	echo "SDIO DRIVER DID NOT INSTALL THE FIRST TIME."
	echo "Note -Most likey you have a Marvel EVK and Treo 650 Combination"
	echo "!!!!!!!!!!!!IS THE CARD PLUGGED IN????!!!!!!!!!!"
	echo " "
	echo "TRYING AGAIN..."
	insmod $MDIR/sdio.ko
    fi
    check_error
}

install_wifi_driver () {

    if [ "$IS_XSCALE" -ne 0 ]
    then 
	if [ "$IS_SDIO_PRESENT" -ne 0 ]
	then # SDIO is present
	    echo "SDIO Driver is already present. Skipping Install..."
	else
	    install_sdio_driver
	fi
    fi

    #install the marvell wifi driver

    # Set up OMAP-specific args
    if [ "$IS_XSCALE" -eq 0 ]
    then 
    	omap_wifi_driver_args="$OMAP_SDIO_BUSWIDTH_ARGS $OMAP_SDIO_INTMODE_ARGS $OMAP_SDIO_SLOT_ARGS"
    fi

    echo "Installing the SD8686 driver for WR .."
    insmod $MDIR/sd8xxx.ko helper_name=/lib/firmware/helper_sd.bin \
	    fw_name=/lib/firmware/sd8686.bin \
	    $omap_wifi_driver_args

    check_error

    echo "DONE !! WiFi Module Loaded.."

}

mfg_wifi_driver_setup () {

    echo "Marvell Manufacturing Tools being setup ..."

    if [ "$IS_WIFI_PRESENT" -ne 0 ]
    then # WIFi is present
	echo "ERROR: WiFi Driver is already present. Exiting ..."
	exit
    fi 

    if [ "$IS_XSCALE" -ne 0 ]
    then 
	if [ "$IS_SDIO_PRESENT" -ne 0 ]
	then # SDIO is present
	    echo "SDIO Driver is already present. Skipping Install..."
	else
	    install_sdio_driver
	fi
    fi

    #install the marvell wifi driver
    echo "Installing the SD8686 driver (MFGMODE=1)
          with Manufacturing firmware for WR .."
    insmod $MDIR/sd8xxx.ko mfgmode=1 helper_name=/lib/firmware/helper_sd.bin fw_name=/lib/firmware/mfg_sd8686.bin
    check_error
    echo ""
    echo "-------------------------------------"
    echo "DONE !! WIFI Installed with MFGMODE=1"
    echo "For MFG CMDS -"
    echo "Run the Marvell's Manufacturing Labtool wifi-mfg-labtool located at /usr/local/bin"
    echo "-------------------------------------"
    echo ""
    echo "Remember you need to configure the antenna from the wifi-mfg-labtool"
    echo ""
}

install_wifi() {

    if [ "$IS_WIFI_PRESENT" -ne 0 ]
    then # WIFi is present
	echo "WiFi Driver is already present. Skipping Install..."
	return
    fi

    install_wifi_driver
    echo "Driver Loaded. Waiting for it to be ready. Sleeping 4 seconds before configuring wifi antenna"
    sleep 1
    

    echo ""
    echo "-------------------------------------------------"
    echo "CONGRATULATIONS!!! WiFi is setup on your platform."
    echo "-------------------------------------------------"
    echo ""
}

install_wifi_sdio_interrupt(){

# set the parameters for SDIO MODE
# BUSWIDTH =1 and ENABLE SDIO_INTERRUPTS
OMAP_SDIO_BUSWIDTH_ARGS="param_sdio_bus_width=1"
OMAP_SDIO_INTMODE_ARGS="param_use_sdio_irq=1"

install_wifi

}

uninstall_wifi() {
    echo "Uninstalling the Marvell WiFi driver .."
    echo " Bringing Down The interface..."
    ifconfig eth0 down
    echo "Unloading wifi driver..."
    rmmod  sd8xxx
    check_error

    echo ""
    echo "-------------------------------------------------"
    echo "WIFI UNINSTALLED" 

    if [ "$IS_XSCALE" -ne 0 ]
    then 
	echo "Uninstalling Marvell sdio driver .."
	rmmod sdio
	check_error
	echo "You can run wififdriver install again to install WiFi"

    else

    	echo "You need to run \"wifidriver -i\" before you can use WiFi again!"
    fi

    echo "-------------------------------------------------"
    echo ""

}

set_usb_charging_off() {
    echo "Turning Usb Charging using modem off..."
    setcharge -a 0
    echo "Done!! you will need to reboot to turn USB Charing on ..."

}

set_wifi_hw_off() {
    echo "Turning wifi hardware off..."
    wifidriver -u    
    echo ""
    
}

set_wifi_hw_on() {
    echo "Turning wifi hardware on..."
   
    wifidriver -i

}
set_wifi_restart() {

    echo "*******************"
    echo "RESTARTING WIFI ..."
    echo "*******************"

    #uninstall wifidriver if present
    wifidriver -u
   

    echo "sleeping 2 seconds .."
    sleep 2

    wifidriver wifi-on
    check_error

    echo "WiFi Successfully Restarted! :-)"
    echo ""
}
set_wifi_deepsleep_on() {
    echo "Make WiFi HW go to deepsleep.."
    iwpriv eth0 deepsleep 1
    echo ""
}

set_wifi_deepsleep_off() {
    echo "Make WiFi HW come out of deepsleep.."
    iwpriv eth0 deepsleep 0
    echo ""
}

usage() {
    echo ""  
    echo "--------------------------------------------------"
    echo "USAGE : wifidriver [-i|--install|install|-u|--uninstall|uninstall|m|mfg|-s|-sdio]"
    echo "wifidriver -i  : Installs   WiFi"
    echo "wifidriver -u  : Uninstalls WiFi"
    echo "wifidriver -c  : Configure WiFi Antenna for your platform"
    echo "wifidriver -m  : Install WiFi in Manufacturing Mode"                      
    echo "wifidriver -sdio: Install WiFi and Enable SDIO interrupts instead of GPIO (Default)"
    echo ""  
    echo "--------------------------------------------------"
    echo "Advanced Commands are also available now..."
    echo "wifidriver set-usb-charging-off"
    echo "wifidriver wifi-off           : Set WiFi Chip in Reset. same as -u option"
    echo "wifidriver wifi-on            : Get WiFi Chip out of Reset and rescan WiFi Card and install wifidriver same as -i option"
    echo "wifidriver restart            : Restart WiFi Hw, Fw and Driver"
    echo "wifidriver deepsleep-on       : Put WiFi Hw in DeepSleep mode"
    echo "wifidriver deepsleep-off      : Get WiFi Hw out of DeepSleep mode" 
    echo "" 
    echo ""     
}

print_platform () {

    if [ "$IS_XSCALE" -ne 0 ]
    then 
    	echo ""
	echo "You are using Treo 650 Platform"
    else 
	echo "You are using Joplin EMU 2.0 Platform"
    fi
}
###########################################
# MAIN FUNCTION
##########################################

## ONLY ONE ARGUMENT IS ALLOWED FOR NOW
if [ "$#" -ne 1 ]
then 
    echo "INCORRECT NO OF ARGUMENTS!!!"
    usage
    exit
fi

print_platform
 
case "$1" in
  -i|--install|install)
            # "-i" or "--install" parameter?
	    install_wifi
            ;;
  -u|--uninstall|uninstall)
	    # uinstall
            uninstall_wifi
            ;;
  -m|-mfg|manufacturing)
	    # install in MFG Mode
	    mfg_wifi_driver_setup
	    ;;
   -s|-sdio)
	    # installs WiFi Driver in SDIO mode
	    install_wifi_sdio_interrupt
	    ;;
   -c|-configure)
	    configure_wifi
	    ;;
    set-usb-charging-off)
            # turn off usb charging
            set_usb_charging_off
            ;;
    wifi-off)
            # turn off wifi hardware
           set_wifi_hw_off
           ;;
    wifi-on)
            # turn off wifi hardware
           set_wifi_hw_on
           ;;
    restart)
            # restart wifi hardware
            set_wifi_restart
           ;;

    deepsleep-on)
            #make wifi go to deep sleep
            set_wifi_deepsleep_on
            ;;
    deepsleep-off)
            #make wifi go to deep sleep
            set_wifi_deepsleep_off
            ;;
    *) 
	    # catch all 
	    echo "INCORRECT ARGUMENT"
	    usage 
	    ;;
esac

