branding: add LEDE branding
[openwrt/staging/lynxis/omap.git] / package / kernel / broadcom-wl / files / lib / wifi / broadcom.sh
1 append DRIVERS "broadcom"
2
3 scan_broadcom() {
4 local device="$1"
5 local vif vifs wds
6 local adhoc sta apmode mon disabled
7 local adhoc_if sta_if ap_if mon_if
8
9 config_get vifs "$device" vifs
10 for vif in $vifs; do
11 config_get_bool disabled "$vif" disabled 0
12 [ $disabled -eq 0 ] || continue
13
14 local mode
15 config_get mode "$vif" mode
16 case "$mode" in
17 adhoc)
18 adhoc=1
19 adhoc_if="$vif"
20 ;;
21 sta)
22 sta=1
23 sta_if="$vif"
24 ;;
25 ap)
26 apmode=1
27 ap_if="${ap_if:+$ap_if }$vif"
28 ;;
29 wds)
30 local addr
31 config_get addr "$vif" bssid
32 [ -z "$addr" ] || {
33 addr=$(echo "$addr" | tr 'A-F' 'a-f')
34 append wds "$addr"
35 }
36 ;;
37 monitor)
38 mon=1
39 mon_if="$vif"
40 ;;
41 *) echo "$device($vif): Invalid mode";;
42 esac
43 done
44 config_set "$device" wds "$wds"
45
46 local _c=
47 for vif in ${adhoc_if:-$sta_if $ap_if $mon_if}; do
48 config_set "$vif" ifname "${device}${_c:+-$_c}"
49 _c=$((${_c:-0} + 1))
50 done
51 config_set "$device" vifs "${adhoc_if:-$sta_if $ap_if $mon_if}"
52
53 ap=1
54 infra=1
55 if [ "$_c" -gt 1 ]; then
56 mssid=1
57 else
58 mssid=
59 fi
60 apsta=0
61 radio=1
62 monitor=0
63 case "$adhoc:$sta:$apmode:$mon" in
64 1*)
65 ap=0
66 mssid=
67 infra=0
68 ;;
69 :1:1:)
70 apsta=1
71 wet=1
72 ;;
73 :1::)
74 wet=1
75 ap=0
76 mssid=
77 ;;
78 :::1)
79 wet=1
80 ap=0
81 mssid=
82 monitor=1
83 ;;
84 ::)
85 radio=0
86 ;;
87 esac
88 }
89
90 disable_broadcom() {
91 local device="$1"
92 set_wifi_down "$device"
93 (
94 include /lib/network
95
96 local pid_file=/var/run/nas.$device.pid
97 [ -e $pid_file ] && start-stop-daemon -K -q -s SIGKILL -p $pid_file && rm $pid_file
98
99 # make sure the interfaces are down and removed from all bridges
100 local dev ifname
101 for dev in /sys/class/net/wds${device##wl}-* /sys/class/net/${device}-* /sys/class/net/${device}; do
102 if [ -e "$dev" ]; then
103 ifname=${dev##/sys/class/net/}
104 ip link set dev "$ifname" down
105 unbridge "$ifname"
106 fi
107 done
108
109 # make sure all of the devices are disabled in the driver
110 local ifdown=
111 local bssmax=$(wlc ifname "$device" bssmax)
112 local vif=$((${bssmax:-4} - 1))
113 append ifdown "down" "$N"
114 append ifdown "wds none" "$N"
115 while [ $vif -ge 0 ]; do
116 append ifdown "vif $vif" "$N"
117 append ifdown "enabled 0" "$N"
118 vif=$(($vif - 1))
119 done
120
121 wlc ifname "$device" stdin <<EOF
122 $ifdown
123 leddc 0xffff
124 EOF
125 )
126 true
127 }
128
129 enable_broadcom() {
130 local device="$1"
131 local channel country maxassoc wds vifs distance slottime rxantenna txantenna
132 local frameburst macfilter maclist macaddr txpower frag rts hwmode htmode
133 config_get channel "$device" channel
134 config_get country "$device" country
135 config_get maxassoc "$device" maxassoc
136 config_get wds "$device" wds
137 config_get vifs "$device" vifs
138 config_get distance "$device" distance
139 config_get slottime "$device" slottime
140 config_get rxantenna "$device" rxantenna
141 config_get txantenna "$device" txantenna
142 config_get_bool frameburst "$device" frameburst
143 config_get macfilter "$device" macfilter
144 config_get maclist "$device" maclist
145 config_get macaddr "$device" macaddr $(wlc ifname "$device" default_bssid)
146 config_get txpower "$device" txpower
147 config_get frag "$device" frag
148 config_get rts "$device" rts
149 config_get hwmode "$device" hwmode
150 config_get htmode "$device" htmode
151 local doth=0
152 local wmm=1
153
154 [ -z "$slottime" ] && {
155 [ -n "$distance" ] && {
156 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
157 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
158 }
159 } || {
160 slottime="${slottime:--1}"
161 }
162
163 case "$macfilter" in
164 allow|2)
165 macfilter=2;
166 ;;
167 deny|1)
168 macfilter=1;
169 ;;
170 disable|none|0)
171 macfilter=0;
172 ;;
173 esac
174
175 local gmode=2 nmode=0 nreqd=
176 case "$hwmode" in
177 *a) gmode=;;
178 *b) gmode=0;;
179 *bg) gmode=1;;
180 *g) gmode=2;;
181 *gst) gmode=4;;
182 *lrs) gmode=5;;
183 *) nmode=1; nreqd=0;;
184 esac
185
186 case "$hwmode" in
187 n|11n) nmode=1; nreqd=1;;
188 *n*) nmode=1; nreqd=0;;
189 esac
190
191 # Use 'nmode' for N-Phy only
192 [ "$(wlc ifname "$device" phytype)" = 4 ] || nmode=
193
194 local band chanspec
195 [ ${channel:-0} -ge 1 -a ${channel:-0} -le 14 ] && band=2
196 [ ${channel:-0} -ge 36 ] && {
197 band=1
198 gmode=
199 }
200
201 # Use 'chanspec' instead of 'channel' for 'N' modes (See bcmwifi.h)
202 [ -n "$nmode" -a -n "$band" -a -n "$channel" ] && {
203 case "$htmode" in
204 HT40)
205 if [ -n "$gmode" ]; then
206 [ $channel -lt 7 ] && htmode="HT40+" || htmode="HT40-"
207 else
208 [ $(( ($channel / 4) % 2 )) -eq 1 ] && htmode="HT40+" || htmode="HT40-"
209 fi
210 ;;
211 esac
212 case "$htmode" in
213 HT40-) chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); nmode=1; channel=;;
214 HT40+) chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); nmode=1; channel=;;
215 HT20) chanspec=$(printf 0x%x%x%02x $band 0xb $channel); nmode=1; channel=;;
216 *) ;;
217 esac
218 }
219
220 local leddc=$(wlc ifname "$device" leddc)
221 [ $((leddc)) -eq $((0xffff)) ] && {
222 leddc=0x005a000a;
223 }
224
225 local _c=0
226 local nas="$(which nas)"
227 local if_pre_up if_up nas_cmd
228 local vif vif_pre_up vif_post_up vif_do_up vif_txpower
229 local bssmax=$(wlc ifname "$device" bssmax)
230 bssmax=${bssmax:-4}
231
232 for vif in $vifs; do
233 [ $_c -ge $bssmax ] && break
234
235 config_get vif_txpower "$vif" txpower
236
237 local mode
238 config_get mode "$vif" mode
239 append vif_pre_up "vif $_c" "$N"
240 append vif_post_up "vif $_c" "$N"
241 append vif_do_up "vif $_c" "$N"
242
243 config_get_bool wmm "$vif" wmm "$wmm"
244 config_get_bool doth "$vif" doth "$doth"
245
246 [ "$mode" = "sta" ] || {
247 local hidden isolate
248 config_get_bool hidden "$vif" hidden 0
249 append vif_pre_up "closed $hidden" "$N"
250 config_get_bool isolate "$vif" isolate 0
251 append vif_pre_up "ap_isolate $isolate" "$N"
252 }
253
254 local wsec_r=0
255 local eap_r=0
256 local wsec=0
257 local auth=0
258 local nasopts=
259 local enc key rekey
260
261 config_get enc "$vif" encryption
262 case "$enc" in
263 *wep*)
264 local def defkey k knr
265 wsec_r=1
266 wsec=1
267 defkey=1
268 config_get key "$vif" key
269 case "$enc" in
270 *shared*) append vif_do_up "wepauth 1" "$N";;
271 *) append vif_do_up "wepauth 0" "$N";;
272 esac
273 case "$key" in
274 [1234])
275 defkey="$key"
276 for knr in 1 2 3 4; do
277 config_get k "$vif" key$knr
278 [ -n "$k" ] || continue
279 [ "$defkey" = "$knr" ] && def="=" || def=""
280 append vif_do_up "wepkey $def$knr,$k" "$N"
281 done
282 ;;
283 "");;
284 *) append vif_do_up "wepkey =1,$key" "$N";;
285 esac
286 ;;
287 *psk*)
288 wsec_r=1
289 config_get key "$vif" key
290
291 # psk version + default cipher
292 case "$enc" in
293 *mixed*|*psk+psk2*) auth=132; wsec=6;;
294 *psk2*) auth=128; wsec=4;;
295 *) auth=4; wsec=2;;
296 esac
297
298 # cipher override
299 case "$enc" in
300 *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
301 *aes*|*ccmp*) wsec=4;;
302 *tkip*) wsec=2;;
303 esac
304
305 # group rekey interval
306 config_get rekey "$vif" wpa_group_rekey
307
308 eval "${vif}_key=\"\$key\""
309 nasopts="-k \"\$${vif}_key\"${rekey:+ -g $rekey}"
310 ;;
311 *wpa*)
312 local auth_port auth_secret auth_server
313 wsec_r=1
314 eap_r=1
315 config_get auth_server "$vif" auth_server
316 [ -z "$auth_server" ] && config_get auth_server "$vif" server
317 config_get auth_port "$vif" auth_port
318 [ -z "$auth_port" ] && config_get auth_port "$vif" port
319 config_get auth_secret "$vif" auth_secret
320 [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
321
322 # wpa version + default cipher
323 case "$enc" in
324 *mixed*|*wpa+wpa2*) auth=66; wsec=6;;
325 *wpa2*) auth=64; wsec=4;;
326 *) auth=2; wsec=2;;
327 esac
328
329 # cipher override
330 case "$enc" in
331 *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
332 *aes*|*ccmp*) wsec=4;;
333 *tkip*) wsec=2;;
334 esac
335
336 # group rekey interval
337 config_get rekey "$vif" wpa_group_rekey
338
339 eval "${vif}_key=\"\$auth_secret\""
340 nasopts="-r \"\$${vif}_key\" -h $auth_server -p ${auth_port:-1812}${rekey:+ -g $rekey}"
341 ;;
342 esac
343 append vif_do_up "wsec $wsec" "$N"
344 append vif_do_up "wpa_auth $auth" "$N"
345 append vif_do_up "wsec_restrict $wsec_r" "$N"
346 append vif_do_up "eap_restrict $eap_r" "$N"
347
348 local ssid
349 config_get ssid "$vif" ssid
350 append vif_post_up "vlan_mode 0" "$N"
351 append vif_pre_up "ssid $ssid" "$N"
352
353 [ "$mode" = "monitor" ] && {
354 append vif_post_up "monitor $monitor" "$N"
355 }
356
357 [ "$mode" = "adhoc" ] && {
358 local bssid
359 config_get bssid "$vif" bssid
360 [ -n "$bssid" ] && {
361 append vif_pre_up "bssid $bssid" "$N"
362 append vif_pre_up "ibss_merge 0" "$N"
363 } || {
364 append vif_pre_up "ibss_merge 1" "$N"
365 }
366 }
367
368 append vif_post_up "enabled 1" "$N"
369
370 local ifname
371 config_get ifname "$vif" ifname
372 local if_cmd="if_pre_up"
373 [ "$ifname" != "${ifname##${device}-}" ] && if_cmd="if_up"
374 append $if_cmd "macaddr=\$(wlc ifname '$ifname' cur_etheraddr)" ";$N"
375 append $if_cmd "ip link set dev '$ifname' address \$macaddr" ";$N"
376 append if_up "ip link set dev '$ifname' up" ";$N"
377
378 local net_cfg="$(find_net_config "$vif")"
379 [ -z "$net_cfg" ] || {
380 ubus -t 30 wait_for network.interface."$net_cfg"
381 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
382 append if_up "start_net '$ifname' '$net_cfg'" ";$N"
383 }
384 [ -z "$nas" -o -z "$nasopts" ] || {
385 eval "${vif}_ssid=\"\$ssid\""
386 local nas_mode="-A"
387 [ "$mode" = "sta" ] && nas_mode="-S"
388 [ -z "$nas_cmd" ] && {
389 local pid_file=/var/run/nas.$device.pid
390 nas_cmd="start-stop-daemon -S -b -p $pid_file -x $nas -- -P $pid_file -H 34954"
391 }
392 append nas_cmd "-i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
393 }
394 _c=$(($_c + 1))
395 done
396 wlc ifname "$device" stdin <<EOF
397 ${macaddr:+bssid $macaddr}
398 ${macaddr:+cur_etheraddr $macaddr}
399 band ${band:-0}
400 ${nmode:+nmode $nmode}
401 ${nmode:+${nreqd:+nreqd $nreqd}}
402 ${gmode:+gmode $gmode}
403 leddc $leddc
404 apsta $apsta
405 ap $ap
406 ${mssid:+mssid $mssid}
407 infra $infra
408 ${wet:+wet 1}
409 802.11d 0
410 802.11h ${doth:-0}
411 wme ${wmm:-1}
412 rxant ${rxantenna:-3}
413 txant ${txantenna:-3}
414 fragthresh ${frag:-2346}
415 rtsthresh ${rts:-2347}
416 monitor ${monitor:-0}
417
418 radio ${radio:-1}
419 macfilter ${macfilter:-0}
420 maclist ${maclist:-none}
421 ${wds:+wds $wds}
422 country ${country:-US}
423 ${channel:+channel $channel}
424 ${chanspec:+chanspec $chanspec}
425 maxassoc ${maxassoc:-128}
426 slottime ${slottime:--1}
427 ${frameburst:+frameburst $frameburst}
428
429 $vif_pre_up
430 EOF
431 eval "$if_pre_up"
432 wlc ifname "$device" stdin <<EOF
433 up
434 $vif_post_up
435 EOF
436 eval "$if_up"
437 wlc ifname "$device" stdin <<EOF
438 $vif_do_up
439 EOF
440
441 # use vif_txpower (from last wifi-iface) instead of txpower (from
442 # wifi-device) if the latter does not exist
443 txpower=${txpower:-$vif_txpower}
444 [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
445
446 eval "$nas_cmd"
447 }
448
449
450 detect_broadcom() {
451 local i=-1
452
453 while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
454 local channel type
455
456 config_get type wl${i} type
457 [ "$type" = broadcom ] && continue
458 channel=`wlc ifname wl${i} channel`
459 cat <<EOF
460 config wifi-device wl${i}
461 option type broadcom
462 option channel ${channel:-11}
463 option txantenna 3
464 option rxantenna 3
465 # REMOVE THIS LINE TO ENABLE WIFI:
466 option disabled 1
467
468 config wifi-iface
469 option device wl${i}
470 option network lan
471 option mode ap
472 option ssid Lede${i#0}
473 option encryption none
474
475 EOF
476 done
477 }