blob: 640d958f275770fbcc4178794a72c57161335069 [file] [log] [blame]
developer02cc9292023-02-24 18:01:27 +08001#!/bin/sh
2
3#append DRIVERS "mac80211"
4
5lookup_phy() {
6 [ -n "$phy" ] && {
7 [ -d /sys/class/ieee80211/$phy ] && return
8 }
9
10 local devpath
11 config_get devpath "$device" path
12 [ -n "$devpath" ] && {
13 phy="$(iwinfo nl80211 phyname "path=$devpath")"
14 [ -n "$phy" ] && return
15 }
16
17 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
18 [ -n "$macaddr" ] && {
19 for _phy in /sys/class/ieee80211/*; do
20 [ -e "$_phy" ] || continue
21
22 [ "$macaddr" = "$(cat ${_phy}/macaddress)" ] || continue
23 phy="${_phy##*/}"
24 return
25 done
26 }
27 phy=
28 return
29}
30
31find_mac80211_phy() {
32 local device="$1"
33
34 config_get phy "$device" phy
35 lookup_phy
36 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
37 echo "PHY for wifi device $1 not found"
38 return 1
39 }
40 config_set "$device" phy "$phy"
41
42 config_get macaddr "$device" macaddr
43 [ -z "$macaddr" ] && {
44 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
45 }
46
47 return 0
48}
49
50check_mac80211_device() {
51 config_get phy "$1" phy
52 [ -z "$phy" ] && {
53 find_mac80211_phy "$1" >/dev/null || return 0
54 config_get phy "$1" phy
55 }
56 [ "$phy" = "$dev" ] && found=1
57}
58
59
60__get_band_defaults() {
61 local phy="$1"
62
63 ( iw phy "$phy" info; echo ) | awk '
64BEGIN {
65 bands = ""
66}
67
68($1 == "Band" || $1 == "") && band {
69 if (channel) {
70 mode="NOHT"
71 if (ht) mode="HT20"
72 if (vht && band != "1:") mode="VHT80"
73 if (he) mode="HE80"
74 if (he && band == "1:") mode="HE20"
75 if (eht) mode="EHT80"
76 if (eht && band == "1:") mode="EHT20"
77 sub("\\[", "", channel)
78 sub("\\]", "", channel)
79 bands = bands band channel ":" mode " "
80 }
81 band=""
82}
83
84$1 == "Band" {
85 band = $2
86 channel = ""
87 vht = ""
88 ht = ""
89 he = ""
90 eht = ""
91}
92
93$0 ~ "Capabilities:" {
94 ht=1
95}
96
97$0 ~ "VHT Capabilities" {
98 vht=1
99}
100
101$0 ~ "HE Iftypes" {
102 he=1
103}
104
105$0 ~ "EHT Iftypes" {
106 eht=1
107}
108
109$1 == "*" && $3 == "MHz" && $0 !~ /disabled/ && band && !channel {
110 channel = $4
111}
112
113END {
114 print bands
115}'
116}
117
118get_band_defaults() {
119 local phy="$1"
120
121 for c in $(__get_band_defaults "$phy"); do
122 local band="${c%%:*}"
123 c="${c#*:}"
124 local chan="${c%%:*}"
125 c="${c#*:}"
126 local mode="${c%%:*}"
127
128 case "$band" in
129 1) band=2g;;
130 2) band=5g;;
131 3) band=60g;;
132 4) band=6g;;
133 *) band="";;
134 esac
135
136 [ -n "$band" ] || continue
137 [ -n "$mode_band" -a "$band" = "6g" ] && return
138
developer66e89bc2024-04-23 14:50:01 +0800139 # hardcode for default band selection in MLO codebase
140 [ "$phy" = "phy0" -a "$band" != "2g" ] && continue
141 [ "$phy" = "phy1" -a "$band" != "5g" ] && continue
142 [ "$phy" = "phy2" -a "$band" != "6g" ] && continue
143
developer02cc9292023-02-24 18:01:27 +0800144 mode_band="$band"
145 channel="$chan"
146 htmode="$mode"
147 if [ "$band" = "6g" ]
148 then
149 encryption=sae
150 key=12345678
151 else
152 encryption=none
153 fi
154 done
155}
156
157detect_mac80211() {
158 devidx=0
159 #config_load wireless
160 while :; do
161 #config_get type "radio$devidx" type
162 [ -n "$type" ] || break
163 devidx=$(($devidx + 1))
164 done
165
166 if [ ! -f /etc/config/wireless ]; then
167 mkdir -p /etc/config
168 echo -n > /etc/config/wireless
169 fi
170
171 pcieCheck="$(uci get wireless.radio1.path)"
172 checkpath="$(realpath /sys/class/ieee80211/phy1/device | cut -d/ -f4-)"
173
174 if [[ "$pcieCheck"* != "$checkpath"* ]]; then
175 echo -n > /etc/config/wireless
176 rm /nvram/hostapd*
177 fi
178
179 old_path=""
180 for _dev in /sys/class/ieee80211/*; do
181 [ -e "$_dev" ] || continue
182
183 dev="${_dev##*/}"
184
185 found="$(uci get wireless.radio${devidx})"
186
187
188 if [ "$found" == "wifi-device" ]; then
189 devidx=$(($devidx + 1))
190 continue
191 fi
192
193 mode_band=""
194 channel=""
195 htmode=""
196 ht_capab=""
197
198
199 get_band_defaults "$dev"
200
201 path="$(realpath /sys/class/ieee80211/"$dev"/device | cut -d/ -f4-)"
202 if [ -n "$path" ]; then
203 if [ "$path" == "$old_path" ]; then
204 dev_id="set wireless.radio${devidx}.path='$path'+1"
205 else
206 dev_id="set wireless.radio${devidx}.path='$path'"
207 fi
208 else
209 dev_id="set wireless.radio${devidx}.macaddr=$(cat /sys/class/ieee80211/${dev}/macaddress)"
210 fi
211
212 if [ "$(cat /sys/class/ieee80211/"$dev"/device/device)" == "0x7906" ]; then
213 isMerlin=1
214 fi
215
216 if [ "$mode_band" = "6g" ]; then
217 channel="37"
218 fi
219 setdisable="set wireless.radio${devidx}.disabled=0"
220
221
222 if [ "$isMerlin" == "1" ] && [ "$devidx" == "2" ] && [ "$mode_band" == "2g" ]; then
223 setdisable="set wireless.radio${devidx}.disabled=1"
224 fi
225
226 uci -q batch <<-EOF
227 set wireless.radio${devidx}=wifi-device
228 set wireless.radio${devidx}.type=mac80211
229 ${dev_id}
230 set wireless.radio${devidx}.channel=${channel}
231 set wireless.radio${devidx}.band=${mode_band}
232 set wireless.radio${devidx}.htmode=$htmode
233 ${setdisable}
234 set wireless.default_radio${devidx}=wifi-iface
235 set wireless.default_radio${devidx}.device=radio${devidx}
236 set wireless.default_radio${devidx}.network=lan
237 set wireless.default_radio${devidx}.mode=ap
238 set wireless.default_radio${devidx}.ssid=Filogic_${mode_band}
239 set wireless.default_radio${devidx}.encryption=${encryption}
240EOF
241 [ -n "$key" ] && {
242 uci -q set wireless.default_radio${devidx}.key=${key}
243 }
244 uci -q commit wireless
245
246 devidx=$(($devidx + 1))
247 old_path=$path
248 done
249}
250detect_mac80211