#!/bin/sh
#
# Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of The Linux Foundation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#Starts the WLAN over HSIC

#xielei add begin

if [ -f "/etc/softmac" ]; then
	mac=`cat /etc/softmac`
else
	mac="00:11:22:33:44:55"
fi

#xielei add end

#added by liupeng for exchange between MCC and SCC begin
mcc_enable=`cfg get m_band_enable`
if [ "$mcc_enable" == "1" ] || [ "$2" == "sta,ap" ]; then
	ath6kl_quirks=0xFC00
else
	ath6kl_quirks=0x7C00
fi
#added by liupeng for exchange between MCC and SCCend
export KERNEL=`uname -r`
export MODULE_BASE=/lib/modules/$KERNEL/updates
#modified by renbin, change RETRY_LIMIT to 5
export RETRY_LIMIT=5
HELP="Usage $0 {start | stop | restart} <ap | ap,ap | sta,ap>"
CONF_FILE="/etc/wlan_config"
DEVICE_PATTERN="device=AR6003"
CHIP="AR6004"
SDIO_POLLING_PATH="/sys/devices/msm_sdcc.3/polling"
INTERFACE_NAME="wlan"

#Default configuration files available
AR6003_HOSTAPD_CONF="/etc/AR6003_hostapd.conf"
AR6003_AP1_HOSTAPD_CONF="/etc/AR6003_AP1_hostapd.conf"
AR6004_HOSTAPD_CONF="/etc/AR6004_hostapd.conf"
AR6004_AP1_HOSTAPD_CONF="/etc/AR6004_AP1_hostapd.conf"

#This is per QCMAP default configuration
#Can be changed per application requirement
HOSTAPD_CONF="/etc/hostapd.conf"
HOSTAPD_AP1_CONF="/etc/hostapd-wlan1.conf"
HOSTAPD_STA_AP_CONF="/etc/sta_mode_hostapd.conf"

do_ctrl_ar6004_hsic () {
    case "$1" in
    start)
        echo "Starting WLAN... $@"
        #added by renbin start
        echo 1 > /sys/class/firmware/timeout
        #added by renbin end
        shift
        start_ap1=0
        devmode=1
        if [ "$1" == "ap,ap" ] || [ "$1" == "sta,ap" ]; then
            start_ap1=1
            devmode=2
            shift
        elif [ "$1" == "ap" ]; then
            shift
        elif [ "$1" == "ap,sta" ]; then
            return 1
        fi

        set -e
        echo hsic 2000 > /sys/bus/platform/devices/usb_bam/inactivity_timer
        echo msm_hsic_host > /sys/bus/platform/drivers/msm_hsic_host/bind
        insmod $MODULE_BASE/compat/compat.ko
        insmod $MODULE_BASE/net/wireless/cfg80211.ko
		sleep 1
#xielei del begin
#		insmod $MODULE_BASE/drivers/net/wireless/ath/ath6kl/ath6kl_core.ko devmode=$devmode $@
#xielei del end
#xielei add begin
        if [ "$1" == "ap" ]; then
            insmod $MODULE_BASE/drivers/net/wireless/ath/ath6kl/ath6kl_core.ko devmode=$devmode ath6kl_wifi_mac="$mac" debug_quirks=$ath6kl_quirks mcc_adj_ch_spacing=100 enable_ani=1 $@
        else
            insmod $MODULE_BASE/drivers/net/wireless/ath/ath6kl/ath6kl_core.ko devmode=$devmode ath6kl_wifi_mac="$mac" debug_quirks=$ath6kl_quirks mcc_adj_ch_spacing=100 $@
        fi
        
#xielei add end
        insmod $MODULE_BASE/drivers/net/wireless/ath/ath6kl/ath6kl_usb.ko
        set +e

        c=1
        ifconfig wlan0 up 2>  /dev/null
        rc=$?
        while [ $rc -ne 0 -a $c -le $RETRY_LIMIT ]; do
            sleep 1
            ifconfig wlan0 up 2> /dev/null
            rc=$?
            c=`expr $c + 1`
        done

        if [ $c -gt $RETRY_LIMIT ]; then
            echo "WLAN bring-up failed!"
            exit 1
        fi

        if [ $start_ap1 -eq 1 ]; then
            echo "Adding second AP interface(wlan1)"
            iw dev wlan0 interface add wlan1 type __ap
            ifconfig wlan1 up 2> /dev/null
        fi

        #deleted by liuweipeng for no use LTE-WIFI COEX 20140324 begin
        #echo "Starting CXMAPP..."
        #cxmapp --init
        #cxmapp &
		#deleted by liuweipeng for no use LTE-WIFI COEX 20140324 end
		
        ;;

    start_ftm)
        do_ctrl_ar6004_hsic start debug_quirks=0x10
        ;;

    stop)
        echo "Stopping WLAN..."
        killall -15 cxmapp

        rmmod ath6kl_usb
        rmmod ath6kl_core
        rmmod cfg80211
        rmmod compat
        echo msm_hsic_host > /sys/bus/platform/drivers/msm_hsic_host/unbind
        echo hsic 0 > /sys/bus/platform/devices/usb_bam/inactivity_timer
        ;;

    restart)
        do_ctrl_ar6004_hsic stop
        shift
        do_ctrl_ar6004_hsic start $@

        if [ $? -ne 0 ]; then
            return 1
        fi
        ;;

    *)
        return 1
        ;;
    esac

    return 0
}


do_ctrl_ar6003 () {
    case "$1" in
    start)
        echo "Starting WLAN... $@"
        shift
        MODE="ap"
        if [ "$1" == "ap,ap" ] || [ "$1" == "sta,ap" ] || [ "$1" == "ap" ]; then
            MODE="$1"
            shift
        elif [ "$1" == "ap,sta" ]; then
            return 1
        fi

        if [ -w $SDIO_POLLING_PATH ]; then
            echo 1 > $SDIO_POLLING_PATH
        else
            echo "Cannot write to $SDIO_POLLING_PATH"
            exit 1
        fi

        set -e
        modprobe ar6000 targetconf="router" devmode=$MODE ifname=$INTERFACE_NAME $@
        set +e

        sleep 1
        c=1
        ifconfig wlan0 up 2>  /dev/null
        rc=$?
        while [ $rc -ne 0 -a $c -le $RETRY_LIMIT ]; do
            sleep 1
            ifconfig wlan0 up 2> /dev/null
            rc=$?
            c=`expr $c + 1`
        done

        if [ $c -gt $RETRY_LIMIT ]; then
            echo "WLAN bring-up failed!"
            exit 1
        fi

        if [ $MODE == "ap,ap" ] || [ $MODE == "sta,ap" ]; then
            ifconfig wlan1 up 2> /dev/null
        fi

        echo 0 > $SDIO_POLLING_PATH

        echo "Starting CXMAPP..."
        cxmapp --init
        cxmapp --ar6003 &
        ;;

    start_ftm)
        do_ctrl_ar6003 start testmode=1

    	#modified by renbin
        #$0 start testmode=2 debug_quirks=0x0
        #modified by liuweipeng for change the param 20130716
        $0 start debug_quirks=0x10 

        ;;

    stop)
        echo "Stopping WLAN..."
        killall -15 cxmapp

        echo 0 > $SDIO_POLLING_PATH
        rmmod ar6000
        ;;

    restart)
        do_ctrl_ar6003 stop
        shift
        do_ctrl_ar6003 start $@

        if [ $? -ne 0 ]; then
            return 1
        fi
        ;;

    *)
        return 1
        ;;
    esac
}

do_configure_ar6004() {
    if [ "$1" == "start" ] || [ "$1" == "restart" ]; then
        ln -f -s $AR6004_HOSTAPD_CONF $HOSTAPD_CONF
        if [ "$2"  == "ap,ap" ]; then
            ln -f -s $AR6004_AP1_HOSTAPD_CONF $HOSTAPD_AP1_CONF
        fi
        if [ "$2"  == "sta,ap" ]; then
            ln -f -s $AR6004_AP1_HOSTAPD_CONF $HOSTAPD_STA_AP_CONF
        fi
    fi
}

do_configure_ar6003() {
    if [ "$1" == "start" ] || [ "$1" == "restart" ]; then
        ln -f -s $AR6003_HOSTAPD_CONF $HOSTAPD_CONF
        if [ "$2"  == "ap,ap" ]; then
            ln -f -s $AR6003_AP1_HOSTAPD_CONF $HOSTAPD_AP1_CONF
        fi
        if [ "$2"  == "sta,ap" ]; then
            ln -f -s $AR6003_AP1_HOSTAPD_CONF $HOSTAPD_STA_AP_CONF
        fi
    fi
}

if [ -r $CONF_FILE ]; then
    grep -iq $DEVICE_PATTERN $CONF_FILE
    ret=$?
    if [ $ret -eq 0 ]; then
        CHIP="AR6003"
    else
        CHIP="AR6004"
    fi
fi

echo "$CHIP chip detected!"

if [ $CHIP == "AR6003" ]; then
    do_ctrl_ar6003 $@
    if [ $? -ne 0 ]; then
        echo $HELP >&2
        exit 1
    fi
    do_configure_ar6003 $@
else
    do_ctrl_ar6004_hsic $@
    if [ $? -ne 0 ]; then
        echo $HELP >&2
        exit 1
    fi
    do_configure_ar6004 $@
fi

exit 0
