Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 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 | |
| 33 | u8 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 | } |
| 42 | EXPORT_SYMBOL(rtl_query_rxpwrpercentage); |
| 43 | |
| 44 | u8 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 | } |
| 60 | EXPORT_SYMBOL(rtl_evm_db_to_percentage); |
| 61 | |
| 62 | static 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 | |
| 72 | long 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 | } |
| 99 | EXPORT_SYMBOL(rtl_signal_scale_mapping); |
| 100 | |
| 101 | static 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 | |
| 157 | static 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 | |
| 173 | static 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; |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 178 | long undec_sm_pwdb; |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 179 | |
| 180 | rcu_read_lock(); |
| 181 | if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION) |
| 182 | sta = rtl_find_sta(hw, pstatus->psaddr); |
| 183 | |
| 184 | /* adhoc or ap mode */ |
| 185 | if (sta) { |
| 186 | drv_priv = (struct rtl_sta_info *) sta->drv_priv; |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 187 | undec_sm_pwdb = drv_priv->rssi_stat.undec_sm_pwdb; |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 188 | } else { |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 189 | undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb; |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 192 | if (undec_sm_pwdb < 0) |
| 193 | undec_sm_pwdb = pstatus->rx_pwdb_all; |
| 194 | if (pstatus->rx_pwdb_all > (u32) undec_sm_pwdb) { |
| 195 | undec_sm_pwdb = (((undec_sm_pwdb) * |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 196 | (RX_SMOOTH_FACTOR - 1)) + |
| 197 | (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR); |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 198 | undec_sm_pwdb = undec_sm_pwdb + 1; |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 199 | } else { |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 200 | undec_sm_pwdb = (((undec_sm_pwdb) * (RX_SMOOTH_FACTOR - 1)) + |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 201 | (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR); |
| 202 | } |
| 203 | |
| 204 | if (sta) { |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 205 | drv_priv->rssi_stat.undec_sm_pwdb = undec_sm_pwdb; |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 206 | } else { |
Larry Finger | 0f01545 | 2012-10-25 13:46:46 -0500 | [diff] [blame^] | 207 | rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb; |
Larry Finger | a290593 | 2012-10-25 13:46:45 -0500 | [diff] [blame] | 208 | } |
| 209 | rcu_read_unlock(); |
| 210 | |
| 211 | rtl_update_rxsignalstatistics(hw, pstatus); |
| 212 | } |
| 213 | |
| 214 | static void rtl_process_ui_link_quality(struct ieee80211_hw *hw, |
| 215 | struct rtl_stats *pstatus) |
| 216 | { |
| 217 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 218 | u32 last_evm, n_stream, tmpval; |
| 219 | |
| 220 | if (pstatus->signalquality == 0) |
| 221 | return; |
| 222 | |
| 223 | if (rtlpriv->stats.ui_link_quality.total_num++ >= |
| 224 | PHY_LINKQUALITY_SLID_WIN_MAX) { |
| 225 | rtlpriv->stats.ui_link_quality.total_num = |
| 226 | PHY_LINKQUALITY_SLID_WIN_MAX; |
| 227 | last_evm = rtlpriv->stats.ui_link_quality.elements[ |
| 228 | rtlpriv->stats.ui_link_quality.index]; |
| 229 | rtlpriv->stats.ui_link_quality.total_val -= last_evm; |
| 230 | } |
| 231 | rtlpriv->stats.ui_link_quality.total_val += pstatus->signalquality; |
| 232 | rtlpriv->stats.ui_link_quality.elements[ |
| 233 | rtlpriv->stats.ui_link_quality.index++] = |
| 234 | pstatus->signalquality; |
| 235 | if (rtlpriv->stats.ui_link_quality.index >= |
| 236 | PHY_LINKQUALITY_SLID_WIN_MAX) |
| 237 | rtlpriv->stats.ui_link_quality.index = 0; |
| 238 | tmpval = rtlpriv->stats.ui_link_quality.total_val / |
| 239 | rtlpriv->stats.ui_link_quality.total_num; |
| 240 | rtlpriv->stats.signal_quality = tmpval; |
| 241 | rtlpriv->stats.last_sigstrength_inpercent = tmpval; |
| 242 | for (n_stream = 0; n_stream < 2; n_stream++) { |
| 243 | if (pstatus->rx_mimo_sig_qual[n_stream] != -1) { |
| 244 | if (rtlpriv->stats.rx_evm_percentage[n_stream] == 0) { |
| 245 | rtlpriv->stats.rx_evm_percentage[n_stream] = |
| 246 | pstatus->rx_mimo_sig_qual[n_stream]; |
| 247 | } |
| 248 | rtlpriv->stats.rx_evm_percentage[n_stream] = |
| 249 | ((rtlpriv->stats.rx_evm_percentage[n_stream] |
| 250 | * (RX_SMOOTH_FACTOR - 1)) + |
| 251 | (pstatus->rx_mimo_sig_qual[n_stream] * 1)) / |
| 252 | (RX_SMOOTH_FACTOR); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void rtl_process_phyinfo(struct ieee80211_hw *hw, u8 *buffer, |
| 258 | struct rtl_stats *pstatus) |
| 259 | { |
| 260 | |
| 261 | if (!pstatus->packet_matchbssid) |
| 262 | return; |
| 263 | |
| 264 | rtl_process_ui_rssi(hw, pstatus); |
| 265 | rtl_process_pwdb(hw, pstatus); |
| 266 | rtl_process_ui_link_quality(hw, pstatus); |
| 267 | } |
| 268 | EXPORT_SYMBOL(rtl_process_phyinfo); |