1258945Sroberto#!/bin/sh
2258945Sroberto
3258945Srobertoath10kcal_die() {
4258945Sroberto	echo "ath10cal: " "$*"
5258945Sroberto	exit 1
6258945Sroberto}
7258945Sroberto
8258945Srobertoath10kcal_from_file() {
9258945Sroberto	local source=$1
10258945Sroberto	local offset=$2
11258945Sroberto	local count=$3
12258945Sroberto
13258945Sroberto	dd if=$source of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$count 2>/dev/null || \
14258945Sroberto		ath10kcal_die "failed to extract calibration data from $source"
15258945Sroberto}
16258945Sroberto
17258945Srobertoath10kcal_extract() {
18258945Sroberto	local part=$1
19258945Sroberto	local offset=$2
20258945Sroberto	local count=$3
21258945Sroberto	local mtd
22258945Sroberto
23258945Sroberto	mtd=$(find_mtd_chardev $part)
24258945Sroberto	[ -n "$mtd" ] || \
25258945Sroberto		ath10kcal_die "no mtd device found for partition $part"
26258945Sroberto
27258945Sroberto	dd if=$mtd of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$count 2>/dev/null || \
28258945Sroberto		ath10kcal_die "failed to extract calibration data from $mtd"
29258945Sroberto}
30258945Sroberto
31258945Srobertoath10kcal_patch_mac() {
32258945Sroberto	local mac=$1
33258945Sroberto
34258945Sroberto	[ -z "$mac" ] && return
35258945Sroberto
36258945Sroberto	macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=6 count=6
37258945Sroberto}
38258945Sroberto
39258945Sroberto[ -e /lib/firmware/$FIRMWARE ] && exit 0
40258945Sroberto
41258945Sroberto. /lib/ar71xx.sh
42258945Sroberto. /lib/functions.sh
43258945Sroberto. /lib/functions/system.sh
44258945Sroberto
45258945Srobertoboard=$(ar71xx_board_name)
46258945Sroberto
47258945Srobertocase "$FIRMWARE" in
48258945Sroberto"ath10k/cal-pci-0000:00:00.0.bin")
49258945Sroberto	case $board in
50258945Sroberto	dlan-pro-1200-ac)
51258945Sroberto		ath10kcal_extract "art" 20480 2116
52258945Sroberto		;;
53258945Sroberto	dw33d)
54258945Sroberto		ath10kcal_extract "art" 20480 2116
55258945Sroberto		ath10kcal_patch_mac $(mtd_get_mac_binary art 18)
56258945Sroberto		;;
57258945Sroberto	mc-mac1200r)
58258945Sroberto		ath10kcal_extract "art" 20480 2116
59258945Sroberto		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth1/address) -1)
60258945Sroberto		;;
61258945Sroberto	tl-wdr6500-v2)
62258945Sroberto		ath10kcal_extract "art" 20480 2116
63258945Sroberto		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth1/address) -2)
64258945Sroberto		;;
65258945Sroberto	r6100)
66258945Sroberto		ath10kcal_extract "caldata" 20480 2116
67258945Sroberto		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth1/address) +2)
68258945Sroberto		;;
69258945Sroberto	qihoo-c301)
70258945Sroberto		ath10kcal_extract "radiocfg" 20480 2116
71258945Sroberto		ath10kcal_patch_mac $(mtd_get_mac_ascii devdata wlan5mac)
72258945Sroberto		;;
73258945Sroberto	esr1750 | \
74258945Sroberto	epg5000)
75258945Sroberto		ath10kcal_extract "caldata" 20480 2116
76258945Sroberto		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +1)
77258945Sroberto		;;
78258945Sroberto	mr1750 | \
79258945Sroberto	mr1750v2 | \
80258945Sroberto	om5p-acv2)
81258945Sroberto		ath10kcal_extract "ART" 20480 2116
82258945Sroberto		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +16)
83258945Sroberto		;;
84258945Sroberto	unifiac-lite | \
85258945Sroberto	unifiac-pro)
86258945Sroberto		ath10kcal_extract "EEPROM" 20480 2116
87258945Sroberto		;;
88258945Sroberto	esac
89258945Sroberto	;;
90258945Sroberto"ath10k/cal-pci-0000:01:00.0.bin")
91258945Sroberto	case $board in
92258945Sroberto	archer-c5 | \
93258945Sroberto	archer-c7)
94258945Sroberto		ath10kcal_extract "art" 20480 2116
95258945Sroberto		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth1/address) -2)
96258945Sroberto		;;
97258945Sroberto	nbg6616 | \
98258945Sroberto	nbg6716)
99258945Sroberto		ath10kcal_extract "RFdata" 20480 2116
100		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth1/address) -2)
101		;;
102	rb-911g-5hpacd)
103		ath10kcal_from_file "/sys/firmware/routerboot/ext_wlan_data" 20480 2116
104		;;
105	om5p-ac)
106		ath10kcal_extract "ART" 20480 2116
107		ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +16)
108		;;
109	esac
110	;;
111*)
112	exit 1
113	;;
114esac
115