linux/drivers/net/wireless/rtlwifi/stats.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2009-2012  Realtek Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of version 2 of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program; if not, write to the Free Software Foundation, Inc.,
  16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17 *
  18 * The full GNU General Public License is included in this distribution in the
  19 * file called LICENSE.
  20 *
  21 * Contact Information:
  22 * wlanfae <wlanfae@realtek.com>
  23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24 * Hsinchu 300, Taiwan.
  25 *
  26 * Larry Finger <Larry.Finger@lwfinger.net>
  27 *
  28 *****************************************************************************/
  29#include "wifi.h"
  30#include "stats.h"
  31#include <linux/export.h>
  32
  33u8 rtl_query_rxpwrpercentage(char antpower)
  34{
  35        if ((antpower <= -100) || (antpower >= 20))
  36                return 0;
  37        else if (antpower >= 0)
  38                return 100;
  39        else
  40                return 100 + antpower;
  41}
  42EXPORT_SYMBOL(rtl_query_rxpwrpercentage);
  43
  44u8 rtl_evm_db_to_percentage(char value)
  45{
  46        char ret_val;
  47        ret_val = value;
  48
  49        if (ret_val >= 0)
  50                ret_val = 0;
  51        if (ret_val <= -33)
  52                ret_val = -33;
  53        ret_val = 0 - ret_val;
  54        ret_val *= 3;
  55        if (ret_val == 99)
  56                ret_val = 100;
  57
  58        return ret_val;
  59}
  60EXPORT_SYMBOL(rtl_evm_db_to_percentage);
  61
  62static long rtl_translate_todbm(struct ieee80211_hw *hw,
  63                                u8 signal_strength_index)
  64{
  65        long signal_power;
  66
  67        signal_power = (long)((signal_strength_index + 1) >> 1);
  68        signal_power -= 95;
  69        return signal_power;
  70}
  71
  72long rtl_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
  73{
  74        long retsig;
  75
  76        if (currsig >= 61 && currsig <= 100)
  77                retsig = 90 + ((currsig - 60) / 4);
  78        else if (currsig >= 41 && currsig <= 60)
  79                retsig = 78 + ((currsig - 40) / 2);
  80        else if (currsig >= 31 && currsig <= 40)
  81                retsig = 66 + (currsig - 30);
  82        else if (currsig >= 21 && currsig <= 30)
  83                retsig = 54 + (currsig - 20);
  84        else if (currsig >= 5 && currsig <= 20)
  85                retsig = 42 + (((currsig - 5) * 2) / 3);
  86        else if (currsig == 4)
  87                retsig = 36;
  88        else if (currsig == 3)
  89                retsig = 27;
  90        else if (currsig == 2)
  91                retsig = 18;
  92        else if (currsig == 1)
  93                retsig = 9;
  94        else
  95                retsig = currsig;
  96
  97        return retsig;
  98}
  99EXPORT_SYMBOL(rtl_signal_scale_mapping);
 100
 101static void rtl_process_ui_rssi(struct ieee80211_hw *hw,
 102                                struct rtl_stats *pstatus)
 103{
 104        struct rtl_priv *rtlpriv = rtl_priv(hw);
 105        struct rtl_phy *rtlphy = &(rtlpriv->phy);
 106        u8 rfpath;
 107        u32 last_rssi, tmpval;
 108
 109        rtlpriv->stats.rssi_calculate_cnt++;
 110
 111        if (rtlpriv->stats.ui_rssi.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
 112                rtlpriv->stats.ui_rssi.total_num = PHY_RSSI_SLID_WIN_MAX;
 113                last_rssi = rtlpriv->stats.ui_rssi.elements[
 114                        rtlpriv->stats.ui_rssi.index];
 115                rtlpriv->stats.ui_rssi.total_val -= last_rssi;
 116        }
 117        rtlpriv->stats.ui_rssi.total_val += pstatus->signalstrength;
 118        rtlpriv->stats.ui_rssi.elements[rtlpriv->stats.ui_rssi.index++] =
 119            pstatus->signalstrength;
 120        if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
 121                rtlpriv->stats.ui_rssi.index = 0;
 122        tmpval = rtlpriv->stats.ui_rssi.total_val /
 123                rtlpriv->stats.ui_rssi.total_num;
 124        rtlpriv->stats.signal_strength = rtl_translate_todbm(hw,
 125                (u8) tmpval);
 126        pstatus->rssi = rtlpriv->stats.signal_strength;
 127
 128        if (pstatus->is_cck)
 129                return;
 130
 131        for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
 132             rfpath++) {
 133                if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
 134                        rtlpriv->stats.rx_rssi_percentage[rfpath] =
 135                            pstatus->rx_mimo_signalstrength[rfpath];
 136
 137                }
 138                if (pstatus->rx_mimo_signalstrength[rfpath] >
 139                    rtlpriv->stats.rx_rssi_percentage[rfpath]) {
 140                        rtlpriv->stats.rx_rssi_percentage[rfpath] =
 141                            ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
 142                              (RX_SMOOTH_FACTOR - 1)) +
 143                             (pstatus->rx_mimo_signalstrength[rfpath])) /
 144                            (RX_SMOOTH_FACTOR);
 145                        rtlpriv->stats.rx_rssi_percentage[rfpath] =
 146                            rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
 147                } else {
 148                        rtlpriv->stats.rx_rssi_percentage[rfpath] =
 149                            ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
 150                              (RX_SMOOTH_FACTOR - 1)) +
 151                             (pstatus->rx_mimo_signalstrength[rfpath])) /
 152                            (RX_SMOOTH_FACTOR);
 153                }
 154        }
 155}
 156
 157static void rtl_update_rxsignalstatistics(struct ieee80211_hw *hw,
 158                                          struct rtl_stats *pstatus)
 159{
 160        struct rtl_priv *rtlpriv = rtl_priv(hw);
 161        int weighting = 0;
 162
 163        if (rtlpriv->stats.recv_signal_power == 0)
 164                rtlpriv->stats.recv_signal_power = pstatus->recvsignalpower;
 165        if (pstatus->recvsignalpower > rtlpriv->stats.recv_signal_power)
 166                weighting = 5;
 167        else if (pstatus->recvsignalpower < rtlpriv->stats.recv_signal_power)
 168                weighting = (-5);
 169        rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
 170                5 + pstatus->recvsignalpower + weighting) / 6;
 171}
 172
 173static void rtl_process_pwdb(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
 174{
 175        struct rtl_priv *rtlpriv = rtl_priv(hw);
 176        struct rtl_sta_info *drv_priv = NULL;
 177        struct ieee80211_sta *sta = NULL;
 178        long undec_sm_pwdb;
 179        long undec_sm_cck;
 180
 181        rcu_read_lock();
 182        if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
 183                sta = rtl_find_sta(hw, pstatus->psaddr);
 184
 185        /* adhoc or ap mode */
 186        if (sta) {
 187                drv_priv = (struct rtl_sta_info *) sta->drv_priv;
 188                undec_sm_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
 189                undec_sm_cck = drv_priv->rssi_stat.undec_sm_cck;
 190        } else {
 191                undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
 192                undec_sm_cck = rtlpriv->dm.undec_sm_cck;
 193        }
 194
 195        if (undec_sm_pwdb < 0)
 196                undec_sm_pwdb = pstatus->rx_pwdb_all;
 197        if (undec_sm_cck < 0)
 198                undec_sm_cck = pstatus->rx_pwdb_all;
 199        if (pstatus->rx_pwdb_all > (u32) undec_sm_pwdb) {
 200                undec_sm_pwdb = (((undec_sm_pwdb) *
 201                      (RX_SMOOTH_FACTOR - 1)) +
 202                     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
 203                undec_sm_pwdb = undec_sm_pwdb + 1;
 204        } else {
 205                undec_sm_pwdb = (((undec_sm_pwdb) * (RX_SMOOTH_FACTOR - 1)) +
 206                     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
 207        }
 208        if (pstatus->rx_pwdb_all > (u32) undec_sm_cck) {
 209                undec_sm_cck = (((undec_sm_pwdb) *
 210                      (RX_SMOOTH_FACTOR - 1)) +
 211                     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
 212                undec_sm_cck = undec_sm_cck + 1;
 213        } else {
 214                undec_sm_pwdb = (((undec_sm_cck) * (RX_SMOOTH_FACTOR - 1)) +
 215                     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
 216        }
 217
 218        if (sta) {
 219                drv_priv->rssi_stat.undec_sm_pwdb = undec_sm_pwdb;
 220        } else {
 221                rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
 222        }
 223        rcu_read_unlock();
 224
 225        rtl_update_rxsignalstatistics(hw, pstatus);
 226}
 227
 228static void rtl_process_ui_link_quality(struct ieee80211_hw *hw,
 229                                        struct rtl_stats *pstatus)
 230{
 231        struct rtl_priv *rtlpriv = rtl_priv(hw);
 232        u32 last_evm, n_stream, tmpval;
 233
 234        if (pstatus->signalquality == 0)
 235                return;
 236
 237        if (rtlpriv->stats.ui_link_quality.total_num++ >=
 238            PHY_LINKQUALITY_SLID_WIN_MAX) {
 239                rtlpriv->stats.ui_link_quality.total_num =
 240                    PHY_LINKQUALITY_SLID_WIN_MAX;
 241                last_evm = rtlpriv->stats.ui_link_quality.elements[
 242                        rtlpriv->stats.ui_link_quality.index];
 243                rtlpriv->stats.ui_link_quality.total_val -= last_evm;
 244        }
 245        rtlpriv->stats.ui_link_quality.total_val += pstatus->signalquality;
 246        rtlpriv->stats.ui_link_quality.elements[
 247                rtlpriv->stats.ui_link_quality.index++] =
 248                                                 pstatus->signalquality;
 249        if (rtlpriv->stats.ui_link_quality.index >=
 250            PHY_LINKQUALITY_SLID_WIN_MAX)
 251                rtlpriv->stats.ui_link_quality.index = 0;
 252        tmpval = rtlpriv->stats.ui_link_quality.total_val /
 253            rtlpriv->stats.ui_link_quality.total_num;
 254        rtlpriv->stats.signal_quality = tmpval;
 255        rtlpriv->stats.last_sigstrength_inpercent = tmpval;
 256        for (n_stream = 0; n_stream < 2; n_stream++) {
 257                if (pstatus->rx_mimo_sig_qual[n_stream] != -1) {
 258                        if (rtlpriv->stats.rx_evm_percentage[n_stream] == 0) {
 259                                rtlpriv->stats.rx_evm_percentage[n_stream] =
 260                                    pstatus->rx_mimo_sig_qual[n_stream];
 261                        }
 262                        rtlpriv->stats.rx_evm_percentage[n_stream] =
 263                            ((rtlpriv->stats.rx_evm_percentage[n_stream]
 264                              * (RX_SMOOTH_FACTOR - 1)) +
 265                             (pstatus->rx_mimo_sig_qual[n_stream] * 1)) /
 266                            (RX_SMOOTH_FACTOR);
 267                }
 268        }
 269}
 270
 271void rtl_process_phyinfo(struct ieee80211_hw *hw, u8 *buffer,
 272        struct rtl_stats *pstatus)
 273{
 274
 275        if (!pstatus->packet_matchbssid)
 276                return;
 277
 278        rtl_process_ui_rssi(hw, pstatus);
 279        rtl_process_pwdb(hw, pstatus);
 280        rtl_process_ui_link_quality(hw, pstatus);
 281}
 282EXPORT_SYMBOL(rtl_process_phyinfo);
 283