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