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