#!/bin/sh -l
#===============================================================================
#
#  10-atheros_pre-up
#
#  Copyright (C) 2012 by Digi International Inc.
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2 as published by
#  the Free Software Foundation.
#
#
#  !Description: Load Atheros' wireless driver
#
#===============================================================================

# Exit immediately if bmiloader times out or other error occurs
set -e

# Exit if not the wlan0 interface,
# or we've already run once
[ "${IFACE}" != "wlan0" -o -e /var/run/10-atheros_pre_up ] && exit 0

# If not manufacturing mode, exit if product has no wifi
if [ -z "$1" -a ! -e /var/run/product_info/wifi ]; then
    echo "No wifi device present, skipping wifi startup"
    exit 0
fi

FIRMWARE_DIR="/lib/firmware/ath6k/AR6003/hw2.1.1"
RAM_DRIVE="/var/run"
MAC_FILENAME="softmac"

#
# Use $1 for the MAC address, or, if empty, extract from ethaddr2 in NVRAM
# and then strip 'ethaddr2='
#
MAC_ADDR=${1:-$(nvram print module ethaddr2)}
MAC_ADDR=${MAC_ADDR#*=}

mac1="$(echo ${MAC_ADDR} | cut -d':' -f1)"
mac2="$(echo ${MAC_ADDR} | cut -d':' -f2)"
mac3="$(echo ${MAC_ADDR} | cut -d':' -f3)"
mac4="$(echo ${MAC_ADDR} | cut -d':' -f4)"
mac5="$(echo ${MAC_ADDR} | cut -d':' -f5)"
mac6="$(echo ${MAC_ADDR} | cut -d':' -f6)"

printf "\x${mac1}\x${mac2}\x${mac3}\x${mac4}\x${mac5}\x${mac6}" > ${RAM_DRIVE}/${MAC_FILENAME}

# We need to write the WLAN MAC address to softmac in the ath6k firmware
# directory.  However, we don't want to rewrite the file if it already exists
# and the address is the same because we don't want to wear out NAND flash.
#
# So create the file on the RAM DRIVE first and compare the two.  Only update
# the copy on NAND if the address has changed.
#
if ! cmp -s ${RAM_DRIVE}/${MAC_FILENAME} ${FIRMWARE_DIR}/${MAC_FILENAME}; then
	cp ${RAM_DRIVE}/${MAC_FILENAME} ${FIRMWARE_DIR}/${MAC_FILENAME}
fi
rm -f ${RAM_DRIVE}/${MAC_FILENAME}

#
# Set the correct calibration file.  The calibration file determines
# which channels are valid and what their power levels are.  Different
# regions require different settings.
#
read wifi_version dummy 2>/dev/null < /var/run/product_info/wifi || true
# wifi_version: 0=6103 1=6233
INTL_MODE=0
if [ -e /var/run/product_info/intl ]; then
    INTL_MODE=1
fi
# INTL_MODE 0 == United States, 1 == international
case "${wifi_version}${INTL_MODE}" in
    00)	BDATA_SOURCE=${FIRMWARE_DIR}/calData_AR6103_Digi_X2e_B.bin ;;
    01)	BDATA_SOURCE=${FIRMWARE_DIR}/calData_AR6103_Digi_X2e_B_world.bin ;;
    10)	BDATA_SOURCE=${FIRMWARE_DIR}/Digi_6203-6233-US.bin ;;
    11)	BDATA_SOURCE=${FIRMWARE_DIR}/Digi_6203-6233-World.bin ;;
    *) echo "Unexpected wifi hardware /${wifi_version}${INTL_MODE}/"
esac
BDATA_LINK=${FIRMWARE_DIR}/bdata.bin
# We don't want to rewrite NAND everytime we boot so only
# change the link if it is wrong.
if [ ! -e ${BDATA_LINK} ] || ! cmp -s ${BDATA_LINK} ${BDATA_SOURCE}; then
	rm -f ${BDATA_LINK}
	ln ${BDATA_SOURCE} ${BDATA_LINK}
fi

## Load the driver.  Load in test mode if /root/wifi_test_mode exists.
## No need to explicitly load 'compat' and 'cfg80211' modules. They are resolved as
## dependences of 'ath6kl_sdio' module and loaded automatically.
ATH6KL_PARAMS=""
[ -f "/root/wifi_test_mode" ] && { ATH6KL_PARAMS="${ATH6KL_PARAMS} testmode=1"; test_mode_str=" in test mode"; }
grep -qs ath6kl_sdio /proc/modules || \
	modprobe -q ath6kl_sdio ${ATH6KL_PARAMS} || echo "Loading ath6kl_sdio module${test_mode_str}: [FAILED]"

# Delay required for the interface 'wlan0' to settle down before trying to configure it.
sleep 0.5

#
# Turn off power save mode and background scanning.  This improves performance and
# reduces latency.
#
wmi_config -i wlan0 --scan --bg 0
iw wlan0 set power_save off
