blob: b6c7c41a14a96694ff13e8f77cd1db00ca83b49a [file] [log] [blame]
#!/bin/sh /etc/rc.common
# Copyright (C) 2010-2018 OpenWrt.org
START=40
SET_WLAN="iwpriv wlan1"
SET_WLAN_PARAM="$SET_WLAN set_mib"
IFCONFIG=ifconfig
wlan_power_ctrl() {
local on=$1
local name
local power
for name in $(ls /sys/devices | grep "rfkill"); do
[ ! -e "/sys/devices/${name}/pwr_ctrl" ] && continue
power=0
$(cat /sys/devices/${name}/pwr_ctrl | grep -q on) && power=1
[ "$power" == "$on" ] && continue
echo $on > /sys/devices/${name}/pwr_ctrl
done
}
rtl8192fr_write_cali() {
$SET_WLAN set_mib pwrlevelCCK_A=2828282828282828282828282828
$SET_WLAN set_mib pwrlevelCCK_B=2D2D2D2D2D2D2D2D2D2D2D2D2D2D
$SET_WLAN set_mib pwrlevelHT40_1S_A=2727272727272727272727272727
$SET_WLAN set_mib pwrlevelHT40_1S_B=2A2A2A2A2A2A2A2A2A2A2A2A2A2A
$SET_WLAN set_mib pwrdiffOFDM=2828282828282828282828282828
$SET_WLAN set_mib xcap=23
$SET_WLAN set_mib thermal=35
}
rtl8812fr_write_cali() {
$SET_WLAN set_mib pwrlevel5GHT40_1S_A=00000000000000000000000000000000000000000000000000000000000000000000005A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A54545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454
$SET_WLAN set_mib pwrlevel5GHT40_1S_B=00000000000000000000000000000000000000000000000000000000000000000000005A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A54545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454
$SET_WLAN set_mib pwrdiff5GHT40_2S=00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
$SET_WLAN set_mib pwrdiff5GHT20=00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
$SET_WLAN set_mib pwrdiff5GOFDM=00000000000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
$SET_WLAN set_mib pwrlevel_TSSI5GHT40_1S_A=0000000000000000000000000000000000000000000000000000000000000000000000FEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFAFAFAFAFAFAFAFAFAFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFDFDFDFDFDFDFDFDFEFEFEFEFEFEFEFEFFFFFFFFFFFFFFFFFFFAFAFAFAFAFAFAFAFBFBFBFBFBFBFBFBFCFCFCFCFCFCFCFCFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFB
$SET_WLAN set_mib pwrlevel_TSSI5GHT40_1S_B=0000000000000000000000000000000000000000000000000000000000000000000000FBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFBFDFDFDFDFDFDFDFDFDFCFCFCFCFCFCFCFCFCFCFCFCFCFCFCFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
$SET_WLAN set_mib xcap=32
$SET_WLAN set_mib thermal1=33
$SET_WLAN set_mib thermal2=32
}
rtl_write_cali() {
grep -l "RTL8192F" /proc/wlan1/mib_rf > /dev/null
if [ $? -eq 0 ]; then
rtl8192fr_write_cali
fi
grep -l "RTL8812" /proc/wlan1/mib_rf > /dev/null
if [ $? -eq 0 ]; then
rtl8812_write_cali
fi
}
wlan_ops_lock() {
echo mwlan > /sys/power/wake_lock
}
wlan_ops_unlock() {
echo mwlan > /sys/power/wake_unlock
}
wlan_ubus_event() {
case $1 in
start )
ubus send mwlan.stat.action ' { "stat" : "start" } ' ;;
up )
ubus send mwlan.stat.action ' { "stat" : "up" } ' ;;
down )
ubus send mwlan.stat.action ' { "stat" : "down" } ' ;;
* )
echo "unknown option ($1)" ;;
esac
}
wlan_enable_vap() {
local _device _disabled _ifname
_wlan_enable_vap() {
config_get _device $1 device
config_get _disabled $_device disabled
config_get _ifname $1 ifname
[ 1 == "$disabled" ] && return
case $_ifname in
*-va[0-9])
iwpriv ${_ifname%-va*} set_mib vap_enable=1
;;
*)
;;
esac
}
config_foreach _wlan_enable_vap wifi-iface
}
find_phy() {
[ -n "$path" ] && {
for phy in /sys/devices/$path/ieee80211/phy*; do
[ -e "$phy" ] && {
phy="${phy##*/}"
return 0
}
done
}
[ -n "$macaddr" ] && {
for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
done
}
return 1
}
wlan_set_sta_channel_to_ap() {
local _device _disabled _ifname _ssid
local ap_device=$1 root_ifname=$2 ap_channel=$3 sta_channel=
local path macaddr phy
__wlan_set_sta_channel_to_ap() {
config_get _device $1 device
config_get _disabled $_device disabled
config_get path $_device path
config_get _mode $1 mode
config_get _ifname $1 ifname
config_get _ssid $1 ssid
config_get macaddr $1 macaddr
[ 1 == "$_disabled" ] && return
[ "${_mode}" != "sta" ] && return
[ "$root_ifname" != "${_ifname%%-v*}" ] && return
find_phy || {
echo "Could not find PHY for device '$_device'"
return
}
for i in $( seq 1 10 ); do
iw phy "$phy" interface add "$root_ifname" type station
ifconfig "$root_ifname" hw ether "$macaddr"
ifconfig "$root_ifname" up 2>/dev/null && {
echo "==> $root_ifname is up for scan ssid" > /dev/kmsg
break
}
sleep 1
done
for i in $( seq 1 10 ); do
freq=$(iw dev "$root_ifname" scan ssid "$_ssid" | awk '/freq/{print $2}' | head -1)
if [ $freq -ge 2412 -a $freq -le 2472 ]; then
sta_channel=$((($freq - 2407)/5))
elif [ $freq -eq 2484 ]; then
sta_channel=14
elif [ $freq -ge 4900 -a $freq -lt 5000 ]; then
sta_channel=$((($freq - 4000)/5))
else
sta_channel=
sleep 1
echo "==> try $i times to scan ssid $_ssid" > /dev/kmsg
continue
fi
break
done
echo "==> scan $_ssid, get channel = $sta_channel" > /dev/kmsg
[ -n "$sta_channel" ] && {
[ "$sta_channel" != "$ap_channel" ] && {
uci_set wireless $ap_device channel $sta_channel
uci_commit wireless
echo "==> $ap_device: set channel to $sta_channel" > /dev/kmsg
}
eval wifichannel_${root_ifname}=\${sta_channel}
}
}
eval wifichannel=\${wifichannel_${root_ifname}}
[ -n "$wifichannel" ] && {
echo "==> already set channel for root interface" > /dev/kmsg
[ "$wifichannel" != "$ap_channel" ] && {
echo "==> $ap_device: set channel to $wifichannel, same with ${root_ifname}" > /dev/kmsg
uci_set wireless $ap_device channel $wifichannel
uci_commit wireless
}
return
}
config_foreach __wlan_set_sta_channel_to_ap wifi-iface
}
wlan_updown() {
local act=up i=1
local _device _disabled _ifname _mode _up _isolate _channel
local ret
__wlan_updown() {
config_get _device $1 device
config_get _ifname $1 ifname
config_get _mode $1 mode
config_get _isolate $1 isolate
config_get _channel $_device channel
config_get _disabled $_device disabled
if [ "$act" == "down" ]; then
$(/sbin/wifi status $_device 2>/dev/null | grep -q "\"up\": true") || return
/sbin/wifi down $_device
elif [ "$act" == "up" -a "$_disabled" != "1" ]; then
$(/sbin/wifi status $_device 2>/dev/null | grep -q "\"up\": true") && return
case $_ifname in
*-vxd)
iwpriv $_ifname copy_mib
;;
*)
;;
esac
if [ "${_mode}" == "sta" ]; then
iwpriv $_ifname set_mib opmode=8
else
iwpriv $_ifname set_mib opmode=16
# AP need to use same channel with STA
wlan_set_sta_channel_to_ap $_device ${_ifname%%-v*} $_channel
fi
/sbin/wifi up $_device
else
return
fi
while [ $i -lt 20 ]
do
_up=0
$(/sbin/wifi status $_device 2>/dev/null | grep -q "\"up\": true") && _up=1
if [ "$act" == "down" -a "$_up" == "0" ]; then
break;
elif [ "$act" == "up" -a "$_up" == "1" ]; then
if [ $_mode == "ap" ]; then
if [ $_isolate == "1" ]; then
iwpriv $_ifname set_mib block_relay=1
else
iwpriv $_ifname set_mib block_relay=0
fi
fi
break;
fi
sleep 1
i=$(($i+1))
done
[ "$i" -ge "20" ] && LOGE "wlan_updown $act ${_device} failed" && ret=1
}
ret=0
[ "$#" -ge "1" ] && act=$1
config_load wireless
[ "$act" == "up" ] && wlan_enable_vap
config_foreach __wlan_updown wifi-iface
return $ret
}
start() {
wlan_ops_lock
wlan_ubus_event start
wlan_power_ctrl 1
wlan_updown up
wlan_ubus_event up
wlan_ops_unlock
}
boot() {
wlan_check_wifi_exist() {
local _ifname _device _disabled
config_get _device $1 device
config_get _disabled $_device disabled
config_get _ifname $1 ifname
if [ $_disabled == "1" ]; then
mkdir -p /var/wifi_ok
touch /var/wifi_ok/$_device
else
ifconfig -a | grep -q $_ifname && {
mkdir -p /var/wifi_ok
touch /var/wifi_ok/$_device
}
fi
}
start
sleep 1
config_load wireless
config_foreach wlan_check_wifi_exist wifi-iface
}
stop() {
wlan_ops_lock
wlan_updown down
sleep 1
wlan_power_ctrl 0
wlan_ubus_event down
wlan_ops_unlock
}
restart() {
wlan_ops_lock
wlan_power_ctrl 1
wlan_updown down
wlan_updown up
wlan_ops_unlock
}
reload() {
wlan_ops_lock
wlan_power_ctrl 1
wlan_updown down
wlan_updown up
wlan_ops_unlock
}