#!/bin/sh

[ -e /lib/firmware/$FIRMWARE ] && exit 0

. /lib/functions/caldata.sh
. /lib/functions/k2t.sh

board=$(board_name)

case "$FIRMWARE" in
"ath10k/cal-pci-0000:00:00.0.bin")
	case $board in
	allnet,all-wap02860ac|\
	araknis,an-500-ap-i-ac|\
	araknis,an-700-ap-i-ac|\
	engenius,enstationac-v1|\
	glinet,gl-x750|\
	watchguard,ap300)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0x0) 2)
		;;
	asus,pl-ac56|\
	asus,rp-ac66|\
	comfast,cf-wr650ac-v1|\
	comfast,cf-wr650ac-v2|\
	devolo,dlan-pro-1200plus-ac|\
	devolo,magic-2-wifi|\
	joyit,jt-or750i|\
	qxwlan,e1700ac-v2-8m|\
	qxwlan,e1700ac-v2-16m|\
	qxwlan,e600gac-v2-8m|\
	qxwlan,e600gac-v2-16m|\
	sophos,ap15|\
	sophos,ap55|\
	sophos,ap55c|\
	sophos,ap100|\
	sophos,ap100c|\
	ubnt,bullet-ac|\
	ubnt,unifiac-lite|\
	ubnt,unifiac-lr|\
	ubnt,unifiac-mesh|\
	ubnt,unifiac-mesh-pro|\
	ubnt,lap-120|\
	ubnt,litebeam-ac-gen2|\
	ubnt,nanobeam-ac|\
	ubnt,nanobeam-ac-gen2|\
	ubnt,nanobeam-ac-xc|\
	ubnt,nanostation-ac|\
	ubnt,nanostation-ac-loco|\
	ubnt,powerbeam-5ac-500|\
	ubnt,powerbeam-5ac-gen2|\
	ubnt,rocket-5ac-lite|\
	ubnt,unifiac-pro|\
	yuncore,a770|\
	yuncore,xd3200)
		caldata_extract "art" 0x5000 0x844
		;;
	avm,fritz1750e|\
	avm,fritzdvbc)
		caldata_extract "urlader" 0x198a 0x844
		;;
	devolo,dvl1200e|\
	devolo,dvl1200i|\
	devolo,dvl1750c|\
	devolo,dvl1750e|\
	devolo,dvl1750i|\
	devolo,dvl1750x)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0x0) -1)
		;;
	dlink,dir-859-a1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(mtd_get_mac_ascii devdata "wlan5mac")
		;;
	engenius,ecb1200|\
	engenius,ecb1750)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(mtd_get_mac_ascii u-boot-env athaddr)
		;;
	engenius,ews511ap)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) 1)
		;;
	extreme-networks,ws-ap3805i)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(mtd_get_mac_ascii cfg1 RADIOADDR0)
		;;
	glinet,gl-ar750)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0x0) 1)
		;;
	iodata,wn-ac1167dgr|\
	iodata,wn-ac1600dgr2|\
	sitecom,wlr-7100|\
	zyxel,nbg6616)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_ascii u-boot-env ethaddr) 1)
		;;
	nec,wg800hp)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(mtd_get_mac_text board_data 0x880)
		;;
	ocedo,koala|\
	ocedo,ursus)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(mtd_get_mac_binary art 0xc)
		;;
	openmesh,a40|\
	openmesh,a60|\
	openmesh,mr1750-v1|\
	openmesh,mr1750-v2|\
	openmesh,om5p-ac-v2)
		caldata_extract "ART" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) 16)
		;;
	qihoo,c301)
		caldata_extract "radiocfg" 0x5000 0x844
		ath10k_patch_mac $(mtd_get_mac_ascii devdata wlan5mac)
		;;
	tplink,archer-a7-v5|\
	tplink,archer-c2-v3|\
	tplink,archer-c7-v4|\
	tplink,archer-c7-v5|\
	tplink,archer-c25-v1|\
	tplink,tl-wr902ac-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary info 0x8) -1)
		;;
	tplink,archer-c5-v1|\
	tplink,tl-wdr7500-v3)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary u-boot 0x1fc00) -1)
		;;
	tplink,archer-d50-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary romfile 0xf100) 2)
		;;
	tplink,archer-d7-v1|\
	tplink,archer-d7b-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary romfs 0xf100) 2)
		;;
	tplink,re350k-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary info 0x8) 2)
		;;
	tplink,re355-v1|\
	tplink,re450-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) -2)
		;;
	tplink,re450-v2|\
	tplink,re450-v3|\
	tplink,re455-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary info 0x8) 1)
		;;
	tplink,tl-wpa8630-v1)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary u-boot 0x0fc00) 1)
		;;
	esac
	;;
"ath10k/cal-pci-0000:01:00.0.bin")
	case $board in
	openmesh,om5p-ac-v1)
		caldata_extract "ART" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +16)
		;;
	sitecom,wlr-8100)
		caldata_extract "art" 0x5000 0x844
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_ascii u-boot-env ethaddr) 1)
		;;
	esac
	;;
"ath10k/pre-cal-pci-0000:00:00.0.bin")
	case $board in
	asus,rp-ac51|\
	yuncore,a782|\
	yuncore,xd4200)
		caldata_extract "art" 0x5000 0x2f20
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	comfast,cf-e313ac)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(mtd_get_mac_binary art 0x6)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		rm /lib/firmware/ath10k/QCA9888/hw2.0/board-2.bin
		;;
	comfast,cf-e375ac|\
	comfast,cf-e560ac|\
	comfast,cf-ew72|\
	comfast,cf-wr752ac-v1)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0x0) 2)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		rm /lib/firmware/ath10k/QCA9888/hw2.0/board-2.bin
		;;
	dlink,dir-842-c1|\
	dlink,dir-842-c2|\
	dlink,dir-842-c3)
		caldata_extract "art" 0x5000 0x2f20
		caldata_valid "202f" || caldata_extract "reserved" 0x15000 0x2f20
		ath10k_patch_mac $(mtd_get_mac_ascii devdata wlan5mac)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	nec,wf1200cr|\
	nec,wg1200cr)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(mtd_get_mac_ascii devdata wlan5mac)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	phicomm,k2t)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(k2t_get_mac "5g_mac")
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	tplink,archer-c58-v1|\
	tplink,archer-c59-v1|\
	tplink,archer-c59-v2|\
	tplink,archer-c60-v1|\
	tplink,archer-c60-v2|\
	tplink,archer-c60-v3|\
	tplink,archer-c6-v2|\
	tplink,archer-c6-v2-us|\
	tplink,tl-wa1201-v2)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary info 0x8) -1)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	tplink,cpe710-v1)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(mtd_get_mac_binary info 0x8)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	xiaomi,aiot-ac2350)
		caldata_extract "art" 0x5000 0x2f20
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9984/hw1.0/board.bin
		;;
	tplink,deco-m4r-v1)
		caldata_extract "art" 0x5000 0x2f20
		ath10k_patch_mac $(macaddr_add $(mtd_get_mac_binary config 0x8) -1)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	tplink,deco-s4-v2)
		caldata_extract "art" 0x5000 0x2f20
		base_mac=$(mtd_get_mac_encrypted_deco $(find_mtd_part config))
		ath10k_patch_mac $(macaddr_add $base_mac 2)
		ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
			/lib/firmware/ath10k/QCA9888/hw2.0/board.bin
		;;
	esac
	;;
*)
	exit 1
	;;
esac
