madwifi: add config option for adjusting the beacon power relative to the full tx...
[openwrt/svn-archive/archive.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 sta ap
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 # Can only set the country code to one setting for the entire system. The last country code is the one that will be applied.
71 config_get country "$device" country
72 [ -z "$country" ] && country="0"
73 local cc="0"
74 [ -e /proc/sys/dev/$device/countrycode ] && cc="$(cat /proc/sys/dev/$device/countrycode)"
75 if [ ! "$cc" = "$country" ] ; then
76 rmmod ath_pci
77 insmod ath_pci countrycode=$country
78 fi
79 config_get channel "$device" channel
80 config_get vifs "$device" vifs
81 config_get txpower "$device" txpower
82
83 [ auto = "$channel" ] && channel=0
84
85 local first=1
86 for vif in $vifs; do
87 local start_hostapd vif_txpower
88 nosbeacon=
89 config_get ifname "$vif" ifname
90 config_get enc "$vif" encryption
91 config_get eap_type "$vif" eap_type
92 config_get mode "$vif" mode
93
94 case "$mode" in
95 sta) config_get nosbeacon "$device" nosbeacon;;
96 adhoc) config_get nosbeacon "$vif" sw_merge;;
97 esac
98
99 config_get ifname "$vif" ifname
100 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
101 [ $? -ne 0 ] && {
102 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
103 continue
104 }
105 config_set "$vif" ifname "$ifname"
106
107 # only need to change freq band and channel on the first vif
108 config_get hwmode "$device" hwmode
109 [ -z "$hwmode" ] && config_get hwmode "$device" mode
110
111 pureg=0
112 case "$hwmode" in
113 *b) hwmode=11b;;
114 *bg) hwmode=11g;;
115 *g) hwmode=11g; pureg=1;;
116 *gdt) hwmode=11gdt;;
117 *a) hwmode=11a;;
118 *adt) hwmode=11adt;;
119 *ast) hwmode=11ast;;
120 *fh) hwmode=fh;;
121 *) hwmode=auto;;
122 esac
123 iwpriv "$ifname" mode "$hwmode"
124 iwpriv "$ifname" pureg "$pureg"
125
126 [ "$first" = 1 ] && {
127 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
128 }
129
130 config_get_bool hidden "$vif" hidden 0
131 iwpriv "$ifname" hide_ssid "$hidden"
132
133 config_get ff "$vif" ff
134 if [ -n "$ff" ]; then
135 iwpriv "$ifname" ff "$ff"
136 fi
137
138 config_get wds "$vif" wds
139 case "$wds" in
140 1|on|enabled) wds=1;;
141 *) wds=0;;
142 esac
143 iwpriv "$ifname" wds "$wds"
144
145 [ "$mode" = ap -a "$wds" = 1 ] && {
146 config_get_bool wdssep "$vif" wdssep 1
147 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
148 }
149
150 case "$enc" in
151 WEP|wep)
152 for idx in 1 2 3 4; do
153 config_get key "$vif" "key${idx}"
154 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
155 done
156 config_get key "$vif" key
157 key="${key:-1}"
158 case "$key" in
159 [1234]) iwconfig "$ifname" enc "[$key]";;
160 *) iwconfig "$ifname" enc "$key";;
161 esac
162 ;;
163 psk*|wpa*)
164 start_hostapd=1
165 config_get key "$vif" key
166 ;;
167 esac
168
169 case "$mode" in
170 adhoc|ahdemo)
171 config_get addr "$vif" bssid
172 [ -z "$addr" ] || {
173 iwconfig "$ifname" ap "$addr"
174 }
175 ;;
176 esac
177 config_get ssid "$vif" ssid
178
179 config_get_bool bgscan "$vif" bgscan
180 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
181
182 config_get_bool antdiv "$device" diversity
183 config_get antrx "$device" rxantenna
184 config_get anttx "$device" txantenna
185 config_get_bool softled "$device" softled 1
186
187 devname="$(cat /proc/sys/dev/$device/dev_name)"
188 antgpio=
189 case "$devname" in
190 NanoStation2) antgpio=7;;
191 NanoStation5) antgpio=1;;
192 esac
193 if [ -n "$antgpio" ]; then
194 softled=0
195 config_get antenna "$device" antenna
196 case "$antenna" in
197 external) antdiv=0; antrx=1; anttx=1 ;;
198 horizontal) antdiv=0; antrx=1; anttx=1 ;;
199 vertical) antdiv=0; antrx=2; anttx=2 ;;
200 auto) antdiv=1; antrx=0; anttx=0 ;;
201 esac
202
203 [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
204 case "$antenna" in
205 horizontal|vertical|auto)
206 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
207 gpioctl "set" "$antgpio" >/dev/null 2>&1
208 ;;
209 external)
210 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
211 gpioctl "clear" "$antgpio" >/dev/null 2>&1
212 ;;
213 esac
214 fi
215
216 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
217 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
218 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
219 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
220
221 config_get distance "$device" distance
222 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
223
224 config_get rate "$vif" rate
225 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
226
227 config_get mcast_rate "$vif" mcast_rate
228 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
229
230 config_get frag "$vif" frag
231 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
232
233 config_get rts "$vif" rts
234 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
235
236 config_get_bool comp "$vif" compression
237 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
238
239 config_get_bool minrate "$vif" minrate
240 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
241
242 config_get_bool maxrate "$vif" maxrate
243 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
244
245 config_get_bool burst "$vif" bursting
246 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
247
248 config_get_bool wmm "$vif" wmm
249 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
250
251 config_get_bool xr "$vif" xr
252 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
253
254 config_get_bool ar "$vif" ar
255 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
256
257 config_get_bool turbo "$vif" turbo
258 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
259
260 config_get_bool beacon_power "$vif" beacon_power
261 [ -n "$beacon_power" ] && iwpriv "$ifname" beacon_pwr "$beacon_power"
262
263 config_get_bool doth "$vif" doth 0
264 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
265
266 config_get_bool probereq "$vif" probereq
267 [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
268
269 config_get maclist "$vif" maclist
270 [ -n "$maclist" ] && {
271 # flush MAC list
272 iwpriv "$ifname" maccmd 3
273 for mac in $maclist; do
274 iwpriv "$ifname" addmac "$mac"
275 done
276 }
277
278 config_get macpolicy "$vif" macpolicy
279 case "$macpolicy" in
280 allow)
281 iwpriv "$ifname" maccmd 1
282 ;;
283 deny)
284 iwpriv "$ifname" maccmd 2
285 ;;
286 *)
287 # default deny policy if mac list exists
288 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
289 ;;
290 esac
291
292 ifconfig "$ifname" up
293
294 local net_cfg bridge
295 net_cfg="$(find_net_config "$vif")"
296 [ -z "$net_cfg" ] || {
297 bridge="$(bridge_interface "$net_cfg")"
298 config_set "$vif" bridge "$bridge"
299 start_net "$ifname" "$net_cfg"
300 }
301 [ -n "$ssid" ] && iwconfig "$ifname" essid on
302 iwconfig "$ifname" essid "$ssid"
303 set_wifi_up "$vif" "$ifname"
304
305 # TXPower settings only work if device is up already
306 # while atheros hardware theoretically is capable of per-vif (even per-packet) txpower
307 # adjustment it does not work with the current atheros hal/madwifi driver
308
309 config_get vif_txpower "$vif" txpower
310 # use vif_txpower (from wifi-iface) instead of txpower (from wifi-device) if
311 # the latter doesn't exist
312 txpower="${txpower:-$vif_txpower}"
313 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
314
315 case "$mode" in
316 ap)
317 config_get_bool isolate "$vif" isolate 0
318 iwpriv "$ifname" ap_bridge "$((isolate^1))"
319
320 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
321 hostapd_setup_vif "$vif" madwifi || {
322 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
323 # make sure this wifi interface won't accidentally stay open without encryption
324 ifconfig "$ifname" down
325 wlanconfig "$ifname" destroy
326 continue
327 }
328 fi
329 ;;
330 wds|sta)
331 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
332 wpa_supplicant_setup_vif "$vif" madwifi || {
333 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
334 ifconfig "$ifname" down
335 wlanconfig "$ifname" destroy
336 continue
337 }
338 fi
339 ;;
340 esac
341 first=0
342 done
343 }
344
345
346 detect_atheros() {
347 cd /proc/sys/dev
348 [ -d ath ] || return
349 for dev in $(ls -d wifi* 2>&-); do
350 config_get type "$dev" type
351 devname="$(cat /proc/sys/dev/$dev/dev_name)"
352 case "$devname" in
353 NanoStation*)
354 EXTRA_DEV="
355 # Ubiquiti NanoStation features
356 option antenna auto # (auto|horizontal|vertical|external)
357 "
358 ;;
359 esac
360 [ "$type" = atheros ] && return
361 cat <<EOF
362 config wifi-device $dev
363 option type atheros
364 option channel auto
365 $EXTRA_DEV
366 # REMOVE THIS LINE TO ENABLE WIFI:
367 option disabled 1
368
369 config wifi-iface
370 option device $dev
371 option network lan
372 option mode ap
373 option ssid OpenWrt
374 option encryption none
375 EOF
376 done
377 }