broadcom-wl: fix led support
[openwrt.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                                 ifconfig "$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         [ ${nmode:-0} -ne 0 -a -n "$band" -a -n "$channel" ] && {
203                 case "$htmode" in
204                         HT40-)  chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); channel=;;
205                         HT40+)  chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); channel=;;
206                         HT20)   chanspec=$(printf 0x%x%x%02x $band 0xb $channel); channel=;;
207                         *) ;;
208                 esac
209         }
210
211         local leddc = $(wlc ifname "$device" leddc)
212         if [ "$leddc" -eq 0xffff ]; then
213                 leddc = 0x0;
214         fi
215
216         local _c=0
217         local nas="$(which nas)"
218         local if_pre_up if_up nas_cmd
219         local vif vif_pre_up vif_post_up vif_do_up vif_txpower
220         local bssmax=$(wlc ifname "$device" bssmax)
221         bssmax=${bssmax:-4}
222
223         for vif in $vifs; do
224                 [ $_c -ge $bssmax ] && break
225
226                 config_get vif_txpower "$vif" txpower
227
228                 local mode
229                 config_get mode "$vif" mode
230                 append vif_pre_up "vif $_c" "$N"
231                 append vif_post_up "vif $_c" "$N"
232                 append vif_do_up "vif $_c" "$N"
233
234                 config_get_bool wmm "$vif" wmm "$wmm"
235                 config_get_bool doth "$vif" doth "$doth"
236
237                 [ "$mode" = "sta" ] || {
238                         local hidden isolate
239                         config_get_bool hidden "$vif" hidden 0
240                         append vif_pre_up "closed $hidden" "$N"
241                         config_get_bool isolate "$vif" isolate 0
242                         append vif_pre_up "ap_isolate $isolate" "$N"
243                 }
244
245                 local wsec_r=0
246                 local eap_r=0
247                 local wsec=0
248                 local auth=0
249                 local nasopts=
250                 local enc key rekey
251
252                 config_get enc "$vif" encryption
253                 case "$enc" in
254                         *wep*)
255                                 local def defkey k knr
256                                 wsec_r=1
257                                 wsec=1
258                                 defkey=1
259                                 config_get key "$vif" key
260                                 case "$enc" in
261                                         *shared*) append vif_do_up "wepauth 1" "$N";;
262                                         *) append vif_do_up "wepauth 0" "$N";;
263                                 esac
264                                 case "$key" in
265                                         [1234])
266                                                 defkey="$key"
267                                                 for knr in 1 2 3 4; do
268                                                         config_get k "$vif" key$knr
269                                                         [ -n "$k" ] || continue
270                                                         [ "$defkey" = "$knr" ] && def="=" || def=""
271                                                         append vif_do_up "wepkey $def$knr,$k" "$N"
272                                                 done
273                                         ;;
274                                         "");;
275                                         *) append vif_do_up "wepkey =1,$key" "$N";;
276                                 esac
277                         ;;
278                         *psk*)
279                                 wsec_r=1
280                                 config_get key "$vif" key
281
282                                 # psk version + default cipher
283                                 case "$enc" in
284                                         *mixed*|*psk+psk2*) auth=132; wsec=6;;
285                                         *psk2*) auth=128; wsec=4;;
286                                         *) auth=4; wsec=2;;
287                                 esac
288
289                                 # cipher override
290                                 case "$enc" in
291                                         *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
292                                         *aes*|*ccmp*) wsec=4;;
293                                         *tkip*) wsec=2;;
294                                 esac
295
296                                 # group rekey interval
297                                 config_get rekey "$vif" wpa_group_rekey
298
299                                 eval "${vif}_key=\"\$key\""
300                                 nasopts="-k \"\$${vif}_key\"${rekey:+ -g $rekey}"
301                         ;;
302                         *wpa*)
303                                 local auth_port auth_secret auth_server
304                                 wsec_r=1
305                                 eap_r=1
306                                 config_get auth_server "$vif" auth_server
307                                 [ -z "$auth_server" ] && config_get auth_server "$vif" server
308                                 config_get auth_port "$vif" auth_port
309                                 [ -z "$auth_port" ] && config_get auth_port "$vif" port
310                                 config_get auth_secret "$vif" auth_secret
311                                 [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
312
313                                 # wpa version + default cipher
314                                 case "$enc" in
315                                         *mixed*|*wpa+wpa2*) auth=66; wsec=6;;
316                                         *wpa2*) auth=64; wsec=4;;
317                                         *) auth=2; wsec=2;;
318                                 esac
319
320                                 # cipher override
321                                 case "$enc" in
322                                         *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
323                                         *aes*|*ccmp*) wsec=4;;
324                                         *tkip*) wsec=2;;
325                                 esac
326
327                                 # group rekey interval
328                                 config_get rekey "$vif" wpa_group_rekey
329
330                                 eval "${vif}_key=\"\$auth_secret\""
331                                 nasopts="-r \"\$${vif}_key\" -h $auth_server -p ${auth_port:-1812}${rekey:+ -g $rekey}"
332                         ;;
333                 esac
334                 append vif_do_up "wsec $wsec" "$N"
335                 append vif_do_up "wpa_auth $auth" "$N"
336                 append vif_do_up "wsec_restrict $wsec_r" "$N"
337                 append vif_do_up "eap_restrict $eap_r" "$N"
338
339                 local ssid
340                 config_get ssid "$vif" ssid
341                 append vif_post_up "vlan_mode 0" "$N"
342                 append vif_pre_up "ssid $ssid" "$N"
343
344                 [ "$mode" = "monitor" ] && {
345                         append vif_post_up "monitor $monitor" "$N"
346                 }
347
348                 [ "$mode" = "adhoc" ] && {
349                         local bssid
350                         config_get bssid "$vif" bssid
351                         [ -n "$bssid" ] && {
352                                 append vif_pre_up "bssid $bssid" "$N"
353                                 append vif_pre_up "ibss_merge 0" "$N"
354                         } || {
355                                 append vif_pre_up "ibss_merge 1" "$N"
356                         }
357                 }
358
359                 append vif_post_up "enabled 1" "$N"
360
361                 local ifname
362                 config_get ifname "$vif" ifname
363                 local if_cmd="if_pre_up"
364                 [ "$ifname" != "${ifname##${device}-}" ] && if_cmd="if_up"
365                 append $if_cmd "macaddr=\$(wlc ifname '$ifname' cur_etheraddr)" ";$N"
366                 append $if_cmd "ifconfig '$ifname' \${macaddr:+hw ether \$macaddr}" ";$N"
367                 append if_up "ifconfig '$ifname' up" ";$N"
368
369                 local net_cfg="$(find_net_config "$vif")"
370                 [ -z "$net_cfg" ] || {
371                         append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
372                         append if_up "start_net '$ifname' '$net_cfg'" ";$N"
373                 }
374                 [ -z "$nas" -o -z "$nasopts" ] || {
375                         eval "${vif}_ssid=\"\$ssid\""
376                         local nas_mode="-A"
377                         [ "$mode" = "sta" ] && nas_mode="-S"
378                         [ -z "$nas_cmd" ] && {
379                                 local pid_file=/var/run/nas.$device.pid
380                                 nas_cmd="start-stop-daemon -S -b -p $pid_file -x $nas -- -P $pid_file -H 34954"
381                         }
382                         append nas_cmd "-i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
383                 }
384                 _c=$(($_c + 1))
385         done
386         wlc ifname "$device" stdin <<EOF
387 ${macaddr:+bssid $macaddr}
388 ${macaddr:+cur_etheraddr $macaddr}
389 band ${band:-0}
390 ${nmode:+nmode $nmode}
391 ${nmode:+${nreqd:+nreqd $nreqd}}
392 ${gmode:+gmode $gmode}
393 leddc $leddc
394 apsta $apsta
395 ap $ap
396 ${mssid:+mssid $mssid}
397 infra $infra
398 ${wet:+wet 1}
399 802.11d 0
400 802.11h ${doth:-0}
401 wme ${wmm:-1}
402 rxant ${rxantenna:-3}
403 txant ${txantenna:-3}
404 fragthresh ${frag:-2346}
405 rtsthresh ${rts:-2347}
406 monitor ${monitor:-0}
407
408 radio ${radio:-1}
409 macfilter ${macfilter:-0}
410 maclist ${maclist:-none}
411 ${wds:+wds $wds}
412 country ${country:-US}
413 ${channel:+channel $channel}
414 ${chanspec:+chanspec $chanspec}
415 maxassoc ${maxassoc:-128}
416 slottime ${slottime:--1}
417 ${frameburst:+frameburst $frameburst}
418
419 $vif_pre_up
420 EOF
421         eval "$if_pre_up"
422         wlc ifname "$device" stdin <<EOF
423 up
424 $vif_post_up
425 EOF
426         eval "$if_up"
427         wlc ifname "$device" stdin <<EOF
428 $vif_do_up
429 EOF
430
431         # use vif_txpower (from last wifi-iface) instead of txpower (from
432         # wifi-device) if the latter does not exist
433         txpower=${txpower:-$vif_txpower}
434         [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
435
436         eval "$nas_cmd"
437 }
438
439
440 detect_broadcom() {
441         local i=-1
442
443         while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
444                 local channel type
445
446                 config_get type wl${i} type
447                 [ "$type" = broadcom ] && continue
448                 channel=`wlc ifname wl${i} channel`
449                 cat <<EOF
450 config wifi-device  wl${i}
451         option type     broadcom
452         option channel  ${channel:-11}
453
454         # REMOVE THIS LINE TO ENABLE WIFI:
455         option disabled 1
456
457 config wifi-iface
458         option device   wl${i}
459         option network  lan
460         option mode     ap
461         option ssid     OpenWrt${i#0}
462         option encryption none
463
464 EOF
465         done
466 }