blob: 644951673850f57748a0bbb0dbe1d4f377088afb [file] [log] [blame]
developer89ea81b2022-08-22 15:34:52 +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 sub("\\[", "", channel)
76 sub("\\]", "", channel)
77 bands = bands band channel ":" mode " "
78 }
79 band=""
80}
81
82$1 == "Band" {
83 band = $2
84 channel = ""
85 vht = ""
86 ht = ""
87 he = ""
88}
89
90$0 ~ "Capabilities:" {
91 ht=1
92}
93
94$0 ~ "VHT Capabilities" {
95 vht=1
96}
97
98$0 ~ "HE Iftypes" {
99 he=1
100}
101
102$1 == "*" && $3 == "MHz" && $0 !~ /disabled/ && band && !channel {
103 channel = $4
104}
105
106END {
107 print bands
108}'
109}
110
111get_band_defaults() {
112 local phy="$1"
113
114 for c in $(__get_band_defaults "$phy"); do
115 local band="${c%%:*}"
116 c="${c#*:}"
117 local chan="${c%%:*}"
118 c="${c#*:}"
119 local mode="${c%%:*}"
120
121 case "$band" in
122 1) band=2g;;
123 2) band=5g;;
124 3) band=60g;;
125 4) band=6g;;
126 *) band="";;
127 esac
128
129 [ -n "$band" ] || continue
130 [ -n "$mode_band" -a "$band" = "6g" ] && return
131
132 mode_band="$band"
133 channel="$chan"
134 htmode="$mode"
developerca672072023-02-24 18:01:27 +0800135 if [ "$band" = "6g" ]
136 then
137 encryption=sae
138 key=12345678
139 else
140 encryption=none
141 fi
developer89ea81b2022-08-22 15:34:52 +0800142 done
143}
144
145detect_mac80211() {
146 devidx=0
147 #config_load wireless
148 while :; do
149 #config_get type "radio$devidx" type
150 [ -n "$type" ] || break
151 devidx=$(($devidx + 1))
152 done
153
154 if [ ! -f /etc/config/wireless ]; then
155 mkdir -p /etc/config
156 echo -n > /etc/config/wireless
157 fi
158
developer08587352022-08-29 18:59:45 +0800159 pcieCheck="$(uci get wireless.radio1.path)"
160 checkpath="$(realpath /sys/class/ieee80211/phy1/device | cut -d/ -f4-)"
161
162 if [[ "$pcieCheck"* != "$checkpath"* ]]; then
163 echo -n > /etc/config/wireless
164 rm /nvram/hostapd*
165 fi
166
developer89ea81b2022-08-22 15:34:52 +0800167 old_path=""
168 for _dev in /sys/class/ieee80211/*; do
169 [ -e "$_dev" ] || continue
170
171 dev="${_dev##*/}"
172
173 found="$(uci get wireless.radio${devidx})"
174
175
176 if [ "$found" == "wifi-device" ]; then
177 devidx=$(($devidx + 1))
178 continue
179 fi
180
181 mode_band=""
182 channel=""
183 htmode=""
184 ht_capab=""
185
developer08587352022-08-29 18:59:45 +0800186
developer89ea81b2022-08-22 15:34:52 +0800187 get_band_defaults "$dev"
188
189 path="$(realpath /sys/class/ieee80211/"$dev"/device | cut -d/ -f4-)"
190 if [ -n "$path" ]; then
191 if [ "$path" == "$old_path" ]; then
192 dev_id="set wireless.radio${devidx}.path='$path'+1"
193 else
194 dev_id="set wireless.radio${devidx}.path='$path'"
195 fi
196 else
197 dev_id="set wireless.radio${devidx}.macaddr=$(cat /sys/class/ieee80211/${dev}/macaddress)"
198 fi
199
developer08587352022-08-29 18:59:45 +0800200 if [ "$(cat /sys/class/ieee80211/"$dev"/device/device)" == "0x7906" ]; then
201 isMerlin=1
202 fi
203
204 if [ "$mode_band" = "6g" ]; then
205 channel="37"
206 fi
207 setdisable="set wireless.radio${devidx}.disabled=0"
208
209
210 if [ "$isMerlin" == "1" ] && [ "$devidx" == "2" ] && [ "$mode_band" == "2g" ]; then
211 setdisable="set wireless.radio${devidx}.disabled=1"
212 fi
213
developer89ea81b2022-08-22 15:34:52 +0800214 uci -q batch <<-EOF
215 set wireless.radio${devidx}=wifi-device
216 set wireless.radio${devidx}.type=mac80211
217 ${dev_id}
218 set wireless.radio${devidx}.channel=${channel}
219 set wireless.radio${devidx}.band=${mode_band}
220 set wireless.radio${devidx}.htmode=$htmode
developer08587352022-08-29 18:59:45 +0800221 ${setdisable}
developer89ea81b2022-08-22 15:34:52 +0800222 set wireless.default_radio${devidx}=wifi-iface
223 set wireless.default_radio${devidx}.device=radio${devidx}
224 set wireless.default_radio${devidx}.network=lan
225 set wireless.default_radio${devidx}.mode=ap
226 set wireless.default_radio${devidx}.ssid=Filogic_${mode_band}
developerca672072023-02-24 18:01:27 +0800227 set wireless.default_radio${devidx}.encryption=${encryption}
developer89ea81b2022-08-22 15:34:52 +0800228EOF
developerca672072023-02-24 18:01:27 +0800229 [ -n "$key" ] && {
230 uci -q set wireless.default_radio${devidx}.key=${key}
231 }
developer89ea81b2022-08-22 15:34:52 +0800232 uci -q commit wireless
233
234 devidx=$(($devidx + 1))
235 old_path=$path
236 done
237}
238detect_mac80211