broadcom-wl: don't inherit lock descriptor in nas process
[openwrt/staging/rmilecki.git] / package / kernel / broadcom-wl / files / lib / wifi / broadcom.sh
index 614db0cf0b9d099f9c98c06331f2f4f1b8338e27..33447341b259ba0493b1efe120a02988ce6b3dba 100644 (file)
@@ -101,7 +101,7 @@ disable_broadcom() {
                for dev in /sys/class/net/wds${device##wl}-* /sys/class/net/${device}-* /sys/class/net/${device}; do
                        if [ -e "$dev" ]; then
                                ifname=${dev##/sys/class/net/}
-                               ifconfig "$ifname" down
+                               ip link set dev "$ifname" down
                                unbridge "$ifname"
                        fi
                done
@@ -120,6 +120,7 @@ disable_broadcom() {
 
                wlc ifname "$device" stdin <<EOF
 $ifdown
+leddc 0xffff
 EOF
        )
        true
@@ -198,15 +199,29 @@ enable_broadcom() {
        }
 
        # Use 'chanspec' instead of 'channel' for 'N' modes (See bcmwifi.h)
-       [ ${nmode:-0} -ne 0 -a -n "$band" -a -n "$channel" ] && {
+       [ -n "$nmode" -a -n "$band" -a -n "$channel" ] && {
                case "$htmode" in
-                       HT40-)  chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); channel=;;
-                       HT40+)  chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); channel=;;
-                       HT20)   chanspec=$(printf 0x%x%x%02x $band 0xb $channel); channel=;;
+                       HT40)
+                               if [ -n "$gmode" ]; then
+                                       [ $channel -lt 7 ] && htmode="HT40+" || htmode="HT40-"
+                               else
+                                       [ $(( ($channel / 4) % 2 )) -eq 1 ] && htmode="HT40+" || htmode="HT40-"
+                               fi
+                       ;;
+               esac
+               case "$htmode" in
+                       HT40-)  chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); nmode=1; channel=;;
+                       HT40+)  chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); nmode=1; channel=;;
+                       HT20)   chanspec=$(printf 0x%x%x%02x $band 0xb $channel); nmode=1; channel=;;
                        *) ;;
                esac
        }
 
+       local leddc=$(wlc ifname "$device" leddc)
+       [ $((leddc)) -eq $((0xffff)) ] && {
+               leddc=0x005a000a;
+       }
+
        local _c=0
        local nas="$(which nas)"
        local if_pre_up if_up nas_cmd
@@ -357,10 +372,12 @@ enable_broadcom() {
                local if_cmd="if_pre_up"
                [ "$ifname" != "${ifname##${device}-}" ] && if_cmd="if_up"
                append $if_cmd "macaddr=\$(wlc ifname '$ifname' cur_etheraddr)" ";$N"
-               append $if_cmd "ifconfig '$ifname' \${macaddr:+hw ether \$macaddr}" ";$N"
+               append $if_cmd "ip link set dev '$ifname' address \$macaddr" ";$N"
+               append if_up "ip link set dev '$ifname' up" ";$N"
 
                local net_cfg="$(find_net_config "$vif")"
                [ -z "$net_cfg" ] || {
+                       ubus -t 30 wait_for network.interface."$net_cfg"
                        append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
                        append if_up "start_net '$ifname' '$net_cfg'" ";$N"
                }
@@ -383,6 +400,7 @@ band ${band:-0}
 ${nmode:+nmode $nmode}
 ${nmode:+${nreqd:+nreqd $nreqd}}
 ${gmode:+gmode $gmode}
+leddc $leddc
 apsta $apsta
 ap $ap
 ${mssid:+mssid $mssid}
@@ -425,7 +443,10 @@ EOF
        txpower=${txpower:-$vif_txpower}
        [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
 
-       eval "$nas_cmd"
+       # fd 1000 is an inherited lock file descriptor for preventing concurrent
+       # init script executions. Close it here to prevent the nas daemon from
+       # inheriting it further to avoid holding the lock indefinitely.
+       eval "$nas_cmd 1000>&-"
 }
 
 
@@ -438,21 +459,22 @@ detect_broadcom() {
                config_get type wl${i} type
                [ "$type" = broadcom ] && continue
                channel=`wlc ifname wl${i} channel`
-               cat <<EOF
-config wifi-device  wl${i}
-       option type     broadcom
-       option channel  ${channel:-11}
-
-       # REMOVE THIS LINE TO ENABLE WIFI:
-       option disabled 1
-
-config wifi-iface
-       option device   wl${i}
-       option network  lan
-       option mode     ap
-       option ssid     OpenWrt${i#0}
-       option encryption none
 
+               uci -q batch <<-EOF
+                       set wireless.wl${i}=wifi-device
+                       set wireless.wl${i}.type=broadcom
+                       set wireless.wl${i}.channel=${channel:-11}
+                       set wireless.wl${i}.txantenna=3
+                       set wireless.wl${i}.rxantenna=3
+                       set wireless.wl${i}.disabled=1
+
+                       set wireless.default_wl${i}=wifi-iface
+                       set wireless.default_wl${i}.device=wl${i}
+                       set wireless.default_wl${i}.network=lan
+                       set wireless.default_wl${i}.mode=ap
+                       set wireless.default_wl${i}.ssid=OpenWrt${i#0}
+                       set wireless.default_wl${i}.encryption=none
 EOF
+               uci -q commit wireless
        done
 }