#!/bin/sh
#===============================================================================
#
#  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
#
#===============================================================================

set -e

[ "${IFACE}" != "wlan0" ] && exit 0

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

#
# Get the wlan MAC address from kernel command line.  Use a default
# value if the address has not been set.
#
MAC_ADDR="$(sed -ne 's,.\+ethaddr2=\([^[:blank:]]\+\)[[:blank:]].*,\1,g;T;p' /proc/cmdline)"
if [ -z "${MAC_ADDR}" -o "${MAC_ADDR}" = "00:00:00:00:00:00" ]; then
	MAC_ADDR="00:04:F3:4C:B1:D3"
fi

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}

BDATA_SOURCE="calData_AR6103_Digi_X2e_B_world.bin"
if [ -e "/sys/kernel/machine/name" ]; then
	# Figure out which wireless region we are in.  The US has rules for
	# what channels can be used and at what power level.  We use a different set of
	# rules for the other regions in the world that we sell into.  The mod_cert field
	# in OTP will be set to 0x0 for the US.  Once we know the region, make sure the
	# appropriate calibration file is loaded.
	#
	US_CODE="0x0"
	MACHINE="$(cat /sys/kernel/machine/name)"
	REGION_CODE="$(cat /sys/kernel/${MACHINE}/mod_cert)"
	if [ -z "${REGION_CODE}" -o "${US_CODE}" = "${REGION_CODE}" ]; then
		BDATA_SOURCE="calData_AR6103_Digi_X2e_B.bin"
	fi
else
	logger -t atheros "Undefined machine, defaulting to world region."
fi

# We don't want to rewrite NAND every time we boot so only
# change the link if it is wrong.
BDATA_LINK="${FIRMWARE_DIR}/bdata.bin"
if [ ! -e "${BDATA_LINK}" ] || ! cmp -s "${BDATA_LINK}" "${FIRMWARE_DIR}/${BDATA_SOURCE}"; then
	ln -sf "${BDATA_SOURCE}" "${BDATA_LINK}"
fi

# ath6kl_sdio.ko
grep -qs ath6kl_sdio /proc/modules || \
	modprobe --ignore-install -q ath6kl_sdio || echo "Loading ath6kl_sdio module: [FAILED]"

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