#!/bin/sh
#===============================================================================
#
#  qualcomm-pre-up
#
#  Copyright (C) 2016 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 Qualcomm' wireless driver
#
#===============================================================================

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

FIRMWARE_DIR="/lib/firmware"
MACFILE="${FIRMWARE_DIR}/wlan/wlan_mac.bin"
TMP_MACFILE="$(mktemp -t wlan_mac.XXXXXX)"

# Read the MACs from DeviceTree. We can have up to four wireless interfaces
# The only required one is wlan0 that is mapped with device tree mac address
# without suffix.
for index in $(seq 0 3); do
	DT_WLANx_MAC="/proc/device-tree/wireless/mac-address"
	if [ "${index}" = "0" ]; then
		# Set a default MAC for wlan0
		MAC_ADDR="00:04:F3:FF:FF:FB"
	else
		# Add the interface suffix for the device tree node
		DT_WLANx_MAC=${DT_WLANx_MAC}${index}
		MAC_ADDR=""
	fi

	if [ -f "${DT_WLANx_MAC}" ]; then
		MAC_ADDR="$(hexdump -ve '1/1 "%02X" ":"' ${DT_WLANx_MAC} | sed 's/:$//g')"
	fi

	# Dump the MAC address in a file with the expected firmware format.
	#  example: Intf0MacAddress=0004f3fffffb
	echo "Intf${index}MacAddress=${MAC_ADDR}" | sed s/://g >> ${TMP_MACFILE}
done

# Override the MAC firmware file only if the MAC file has changed.
if ! cmp -s ${TMP_MACFILE} ${MACFILE}; then
	cp ${TMP_MACFILE} ${MACFILE}
fi
rm -f ${TMP_MACFILE}

OTP_REGION_CODE="$(cat /proc/device-tree/digi,hwid,cert 2>/dev/null)"
DTB_REGION_CODE="$(cat /proc/device-tree/wireless/regulatory-domain 2>/dev/null)"
US_CODE="0x0"
WW_CODE="0x1"
JP_CODE="0x2"
# Check if the DTB_REGION_CODE is in the list of valid codes,
# if not use the OTP programmed value.
case "${DTB_REGION_CODE}" in
    ${US_CODE}|${WW_CODE}|${JP_CODE})
	REGULATORY_DOMAIN=${DTB_REGION_CODE};;
    *)
	logger -t qca6564 "[ERROR] Invalid region code in device tree, using OTP value"
	REGULATORY_DOMAIN=${OTP_REGION_CODE};;
esac

BDATA_LINK="${FIRMWARE_DIR}/bdwlan30.bin"
# Create a symbolic link to the FW file for the specific country region.
BDATA_SOURCE="${FIRMWARE_DIR}/bdwlan30_US.bin"
case "${REGULATORY_DOMAIN}" in
	${US_CODE})
		logger -t qca6564 "Setting US wireless region";;
	${WW_CODE}|${JP_CODE})
		logger -t qca6564 "Setting WW (world wide) wireless region"
		BDATA_SOURCE="${FIRMWARE_DIR}/bdwlan30_World.bin";;
	"")
		logger -t qca6564 "[WARN] region code not found, using US";;
	*)
		logger -t qca6564 "[WARN] Invalid region code, using US";;
esac

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

# Check the version of modprobe installed to compound the arguments.
if readlink -f $(which modprobe) | grep -qs kmod; then
	MODPROBE_ARGS="-i"
fi
# Load the wireless module with the params defined in modprobe.d/qualcomm.conf
modprobe ${MODPROBE_ARGS} wlan

# Validate that firmware was loaded by checking if the interface is present.
if [ -d "/sys/class/net/${IFACE}" ]; then
	echo "Loading qca6564 module: [OK]"
else
	echo "Loading qca6564 module: [FAILED]"
	exit 1
fi
