madwifi: add antenna gpio support for nanostation loco2
[openwrt/openwrt.git] / package / madwifi / files / lib / wifi / madwifi.sh
1 #!/bin/sh
2 append DRIVERS "atheros"
3
4 scan_atheros() {
5 local device="$1"
6 local wds
7 local adhoc ahdemo sta ap monitor
8
9 config_get vifs "$device" vifs
10 for vif in $vifs; do
11
12 config_get ifname "$vif" ifname
13 config_set "$vif" ifname "${ifname:-ath}"
14
15 config_get mode "$vif" mode
16 case "$mode" in
17 adhoc|ahdemo|sta|ap|monitor)
18 append $mode "$vif"
19 ;;
20 wds)
21 config_get ssid "$vif" ssid
22 [ -z "$ssid" ] && continue
23
24 config_set "$vif" wds 1
25 config_set "$vif" mode sta
26 mode="sta"
27 addr="$ssid"
28 ${addr:+append $mode "$vif"}
29 ;;
30 *) echo "$device($vif): Invalid mode, ignored."; continue;;
31 esac
32 done
33
34 case "${adhoc:+1}:${sta:+1}:${ap:+1}" in
35 # valid mode combinations
36 1::) wds="";;
37 1::1);;
38 :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
39 :1:);;
40 ::1);;
41 ::);;
42 *) echo "$device: Invalid mode combination in config"; return 1;;
43 esac
44
45 config_set "$device" vifs "${sta:+$sta }${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${wds:+$wds }${monitor:+$monitor}"
46 }
47
48
49 disable_atheros() (
50 local device="$1"
51
52 set_wifi_down "$device"
53
54 include /lib/network
55 cd /proc/sys/net
56 for dev in *; do
57 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
58 [ -f "/var/run/wifi-${dev}.pid" ] &&
59 kill "$(cat "/var/run/wifi-${dev}.pid")"
60 ifconfig "$dev" down
61 unbridge "$dev"
62 wlanconfig "$dev" destroy
63 }
64 done
65 return 0
66 )
67
68 enable_atheros() {
69 local device="$1"
70
71 config_get regdomain "$device" regdomain
72 [ -n "$regdomain" ] && echo "$regdomain" > /proc/sys/dev/$device/regdomain
73
74 config_get country "$device" country
75 [ -z "$country" ] && country="0"
76 echo "$country" > /proc/sys/dev/$device/countrycode
77
78 config_get_bool outdoor "$device" outdoor "0"
79 echo "$outdoor" > /proc/sys/dev/$device/outdoor
80
81 config_get channel "$device" channel
82 config_get vifs "$device" vifs
83 config_get txpower "$device" txpower
84
85 [ auto = "$channel" ] && channel=0
86
87 config_get_bool antdiv "$device" diversity
88 config_get antrx "$device" rxantenna
89 config_get anttx "$device" txantenna
90 config_get_bool softled "$device" softled 1
91
92 devname="$(cat /proc/sys/dev/$device/dev_name)"
93 local antgpio=
94 local invert=
95 case "$devname" in
96 NanoStation2) antgpio=7; invert=1;;
97 NanoStation5) antgpio=1; invert=1;;
98 "NanoStation Loco2") antgpio=2;;
99 esac
100 if [ -n "$invert" ]; then
101 _set="clear"
102 _clear="set"
103 else
104 _set="set"
105 _clear="clear"
106 fi
107 if [ -n "$antgpio" ]; then
108 softled=0
109 config_get antenna "$device" antenna
110 case "$antenna" in
111 external) antdiv=0; antrx=1; anttx=1 ;;
112 horizontal) antdiv=0; antrx=1; anttx=1 ;;
113 vertical) antdiv=0; antrx=2; anttx=2 ;;
114 auto) antdiv=1; antrx=0; anttx=0 ;;
115 esac
116
117 [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
118 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
119 case "$antenna" in
120 horizontal|vertical|auto)
121 gpioctl "$_clear" "$antgpio" >/dev/null 2>&1
122 ;;
123 external)
124 gpioctl "$_set" "$antgpio" >/dev/null 2>&1
125 ;;
126 esac
127 fi
128
129 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
130 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
131 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
132 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
133
134 config_get distance "$device" distance
135 [ -n "$distance" ] && sysctl -w dev."$device".distance="$distance" >&-
136
137 for vif in $vifs; do
138 local start_hostapd= vif_txpower= nosbeacon=
139 config_get ifname "$vif" ifname
140 config_get enc "$vif" encryption
141 config_get eap_type "$vif" eap_type
142 config_get mode "$vif" mode
143
144 case "$mode" in
145 sta) config_get_bool nosbeacon "$device" nosbeacon;;
146 adhoc) config_get_bool nosbeacon "$vif" sw_merge 1;;
147 esac
148
149 [ "$nosbeacon" = 1 ] || nosbeacon=""
150 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
151 [ $? -ne 0 ] && {
152 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
153 continue
154 }
155 config_set "$vif" ifname "$ifname"
156
157 config_get hwmode "$device" hwmode
158 [ -z "$hwmode" ] && config_get hwmode "$device" mode
159
160 pureg=0
161 case "$hwmode" in
162 *b) hwmode=11b;;
163 *bg) hwmode=11g;;
164 *g) hwmode=11g; pureg=1;;
165 *gdt) hwmode=11gdt;;
166 *a) hwmode=11a;;
167 *adt) hwmode=11adt;;
168 *ast) hwmode=11ast;;
169 *fh) hwmode=fh;;
170 *) hwmode=auto;;
171 esac
172 iwpriv "$ifname" mode "$hwmode"
173 iwpriv "$ifname" pureg "$pureg"
174
175 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
176
177 config_get_bool hidden "$vif" hidden 0
178 iwpriv "$ifname" hide_ssid "$hidden"
179
180 config_get ff "$vif" ff
181 if [ -n "$ff" ]; then
182 iwpriv "$ifname" ff "$ff"
183 fi
184
185 config_get wds "$vif" wds
186 case "$wds" in
187 1|on|enabled) wds=1;;
188 *) wds=0;;
189 esac
190 iwpriv "$ifname" wds "$wds" >/dev/null 2>&1
191
192 [ "$mode" = ap -a "$wds" = 1 ] && {
193 config_get_bool wdssep "$vif" wdssep 1
194 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
195 }
196
197 case "$enc" in
198 WEP|wep)
199 for idx in 1 2 3 4; do
200 config_get key "$vif" "key${idx}"
201 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
202 done
203 config_get key "$vif" key
204 key="${key:-1}"
205 case "$key" in
206 [1234]) iwconfig "$ifname" enc "[$key]";;
207 *) iwconfig "$ifname" enc "$key";;
208 esac
209 ;;
210 psk*|wpa*)
211 start_hostapd=1
212 config_get key "$vif" key
213 ;;
214 esac
215
216 case "$mode" in
217 sta|adhoc|ahdemo)
218 config_get addr "$vif" bssid
219 [ -z "$addr" ] || {
220 iwconfig "$ifname" ap "$addr"
221 }
222 ;;
223 esac
224
225 config_get_bool uapsd "$vif" uapsd 0
226 iwpriv "$ifname" uapsd "$uapsd"
227
228 config_get_bool bgscan "$vif" bgscan
229 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
230
231 config_get rate "$vif" rate
232 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
233
234 config_get mcast_rate "$vif" mcast_rate
235 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
236
237 config_get frag "$vif" frag
238 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
239
240 config_get rts "$vif" rts
241 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
242
243 config_get_bool comp "$vif" compression 0
244 iwpriv "$ifname" compression "$comp" >/dev/null 2>&1
245
246 config_get_bool minrate "$vif" minrate
247 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
248
249 config_get_bool maxrate "$vif" maxrate
250 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
251
252 config_get_bool burst "$vif" bursting
253 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
254
255 config_get_bool wmm "$vif" wmm
256 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
257
258 config_get_bool xr "$vif" xr
259 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
260
261 config_get_bool ar "$vif" ar
262 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
263
264 config_get_bool beacon_power "$vif" beacon_power
265 [ -n "$beacon_power" ] && iwpriv "$ifname" beacon_pwr "$beacon_power"
266
267 config_get_bool doth "$vif" doth 0
268 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
269
270 config_get_bool probereq "$vif" probereq
271 [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
272
273 config_get maclist "$vif" maclist
274 [ -n "$maclist" ] && {
275 # flush MAC list
276 iwpriv "$ifname" maccmd 3
277 for mac in $maclist; do
278 iwpriv "$ifname" addmac "$mac"
279 done
280 }
281
282 config_get macpolicy "$vif" macpolicy
283 case "$macpolicy" in
284 allow)
285 iwpriv "$ifname" maccmd 1
286 ;;
287 deny)
288 iwpriv "$ifname" maccmd 2
289 ;;
290 *)
291 # default deny policy if mac list exists
292 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
293 ;;
294 esac
295
296 ifconfig "$ifname" up
297
298 local net_cfg bridge
299 net_cfg="$(find_net_config "$vif")"
300 [ -z "$net_cfg" ] || {
301 bridge="$(bridge_interface "$net_cfg")"
302 config_set "$vif" bridge "$bridge"
303 start_net "$ifname" "$net_cfg"
304 }
305
306 config_get ssid "$vif" ssid
307 [ -n "$ssid" ] && {
308 iwconfig "$ifname" essid on
309 iwconfig "$ifname" essid "$ssid"
310 }
311
312 set_wifi_up "$vif" "$ifname"
313
314 # TXPower settings only work if device is up already
315 # while atheros hardware theoretically is capable of per-vif (even per-packet) txpower
316 # adjustment it does not work with the current atheros hal/madwifi driver
317
318 config_get vif_txpower "$vif" txpower
319 # use vif_txpower (from wifi-iface) instead of txpower (from wifi-device) if
320 # the latter doesn't exist
321 txpower="${txpower:-$vif_txpower}"
322 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
323
324 case "$mode" in
325 ap)
326 config_get_bool isolate "$vif" isolate 0
327 iwpriv "$ifname" ap_bridge "$((isolate^1))"
328
329 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
330 hostapd_setup_vif "$vif" madwifi || {
331 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
332 # make sure this wifi interface won't accidentally stay open without encryption
333 ifconfig "$ifname" down
334 wlanconfig "$ifname" destroy
335 continue
336 }
337 fi
338 ;;
339 wds|sta)
340 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
341 wpa_supplicant_setup_vif "$vif" madwifi || {
342 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
343 ifconfig "$ifname" down
344 wlanconfig "$ifname" destroy
345 continue
346 }
347 fi
348 ;;
349 esac
350 done
351 }
352
353
354 detect_atheros() {
355 cd /proc/sys/dev
356 [ -d ath ] || return
357 for dev in $(ls -d wifi* 2>&-); do
358 config_get type "$dev" type
359 devname="$(cat /proc/sys/dev/$dev/dev_name)"
360 case "$devname" in
361 NanoStation*)
362 EXTRA_DEV="
363 # Ubiquiti NanoStation features
364 option antenna auto # (auto|horizontal|vertical|external)
365 "
366 ;;
367 esac
368 [ "$type" = atheros ] && continue
369 cat <<EOF
370 config wifi-device $dev
371 option type atheros
372 option channel auto
373 $EXTRA_DEV
374 # REMOVE THIS LINE TO ENABLE WIFI:
375 option disabled 1
376
377 config wifi-iface
378 option device $dev
379 option network lan
380 option mode ap
381 option ssid OpenWrt
382 option encryption none
383 EOF
384 done
385 }