#!/bin/sh
####################################################################
#
# WLAN Initialization script
#
# This script will initialize the wlan modules required for operation
# of the AP hardware.  There are several environmental variables that
# can be set to modify the operation of the AP.  These are set to
# default values (or not used) if not defined.  Parameters are:
#
#  DFS_domainoverride   0=Unitialized (default),
#                       1=FCC Domain (FCC3, US)
#                       2=ETSI Domain (Europe)
#                       3=Japan Domain
#  DFS_usenol           1=Use channel NOL (default)
#  ATH_countrycode      Set Country Code (  Override the use of channel NOL
#                       Override default country code (-1 auto, default)
#                       Use country code 0x1ff for demo channels
#  ATH_outdoor          0=Disable (default)
#                       1=Enable
#  ATH_xchanmode        0=Disable Extended Channels
#                       1=Enable Extended Channels (default)
#  ATH_use_eeprom       0=Use Flash for Calibration Data (default)
#                       1=Use EEPROM for Calibratin Data
#  ATH_force_11a_ch     1=Force AP to use 11na channels only
#                       0=Use all channels
#  ATH_debug            Load time debug flags, 0x00000000 default
#
###################################################################
. /etc/ath/apcfg

KVER=`uname -r | cut -f 1 -d '-'`
MODULE_PATH=/lib/modules/$KVER/net

###################################################################
## The input parameter is either up or down.  We assume that
## if no argument is provided, UP is intended
###################################################################

echo "Args: $#"

if [ $1 != down ]; then
    #
    # Calculate the parameter values required.  Add the appropriate settings
    # to the strings
    #
    
    DFS_ARGS=""
    if [ "${DFS_domainoverride}" != "" ]; then
        DFS_ARGS="domainoverride=$DFS_domainoverride $DFS_ARGS"
    fi
    if [ "${DFS_usenol}" != "" ]; then
        DFS_ARGS="usenol=$DFS_usenol $DFS_ARGS"
    fi

    #
    # PCI Args
    #

    PCI_ARGS=""
    if [ "$ATH_countrycode" != "" ]; then
        PCI_ARGS="countrycode=$ATH_countrycode $PCI_ARGS"
    fi
    if [ "$ATH_outdoor" != "" ]; then
        PCI_ARGS="outdoor=$ATH_outdoor $PCI_ARGS"
    fi
    if [ "$ATH_xchanmode" != "" ]; then
        PCI_ARGS="xchanmode=$ATH_xchanmode $PCI_ARGS"
    fi
    if [ "${ATH_use_eeprom}" != "" ]; then
        PCI_ARGS="use_eeprom=$ATH_use_eeprom $PCI_ARGS"
    fi

    if [ "$ATH_debug" != "" ]; then
        PCI_ARGS="ath_debug=$ATH_debug $PCI_ARGS"
    fi

#
# Finally, insert the modules
#
    insmod $MODULE_PATH/ath_dfs.ko $DFS_ARGS
    insmod  $MODULE_PATH/wlan.ko 
    insmod  $MODULE_PATH/wlan_acl.ko 
    insmod  $MODULE_PATH/wlan_ccmp.ko 
    insmod  $MODULE_PATH/wlan_me.ko 
    insmod  $MODULE_PATH/wlan_scan_ap.ko 
    insmod  $MODULE_PATH/wlan_scan_sta.ko 
    insmod  $MODULE_PATH/wlan_tkip.ko 
    insmod  $MODULE_PATH/wlan_wep.ko 
    insmod  $MODULE_PATH/wlan_xauth.ko 
    insmod  $MODULE_PATH/ath_rate_atheros.ko 
    insmod    $MODULE_PATH/ath_hal.ko 
    insmod    $MODULE_PATH/ath_dev.ko 
    insmod    $MODULE_PATH/ath_pktlog.ko 
    insmod    $MODULE_PATH/ath_pci.ko 
else
#
# First, check to see if all of the VAPs have been eliminated
# If not, kill them all.  Don't continue, since this script
# will be called by killVAP all
#

    APS=`iwconfig | grep ath`

    if [ "${APS}" != "" ]; then
        /etc/ath/killVap all

        #
        # We exit here since the above script will call this script
        # to delete all modules
        #

        exit
    fi
    
    rmmod   ath_pci.ko 
    rmmod   ath_pktlog.ko 
    rmmod   ath_dev.ko 
    rmmod   ath_hal.ko 
    rmmod ath_rate_atheros.ko 
    rmmod wlan_xauth.ko 
    rmmod wlan_wep.ko 
    rmmod wlan_tkip.ko 
    rmmod wlan_scan_sta.ko 
    
    rmmod wlan_scan_ap.ko 
    rmmod wlan_me.ko 
    rmmod wlan_ccmp.ko 
    rmmod wlan_acl.ko 
    rmmod wlan.ko 
    rmmod ath_dfs.ko

fi
# 
