Linux 4.16.11
[linux/fpc-iii.git] / drivers / staging / rtlwifi / stats.c
blob96eb14c92c011b28254a099f92b5a93ecca320d4
1 /******************************************************************************
3 * Copyright(c) 2009-2012 Realtek Corporation.
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.
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.
14 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
22 * Larry Finger <Larry.Finger@lwfinger.net>
24 *****************************************************************************/
25 #include "wifi.h"
26 #include "stats.h"
27 #include <linux/export.h>
29 u8 rtl_query_rxpwrpercentage(s8 antpower)
31 if ((antpower <= -100) || (antpower >= 20))
32 return 0;
33 else if (antpower >= 0)
34 return 100;
35 else
36 return 100 + antpower;
39 u8 rtl_evm_db_to_percentage(s8 value)
41 s8 ret_val = clamp(-value, 0, 33) * 3;
43 if (ret_val == 99)
44 ret_val = 100;
46 return ret_val;
49 static long rtl_translate_todbm(struct ieee80211_hw *hw,
50 u8 signal_strength_index)
52 long signal_power;
54 signal_power = (long)((signal_strength_index + 1) >> 1);
55 signal_power -= 95;
56 return signal_power;
59 long rtl_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
61 long retsig;
63 if (currsig >= 61 && currsig <= 100)
64 retsig = 90 + ((currsig - 60) / 4);
65 else if (currsig >= 41 && currsig <= 60)
66 retsig = 78 + ((currsig - 40) / 2);
67 else if (currsig >= 31 && currsig <= 40)
68 retsig = 66 + (currsig - 30);
69 else if (currsig >= 21 && currsig <= 30)
70 retsig = 54 + (currsig - 20);
71 else if (currsig >= 5 && currsig <= 20)
72 retsig = 42 + (((currsig - 5) * 2) / 3);
73 else if (currsig == 4)
74 retsig = 36;
75 else if (currsig == 3)
76 retsig = 27;
77 else if (currsig == 2)
78 retsig = 18;
79 else if (currsig == 1)
80 retsig = 9;
81 else
82 retsig = currsig;
84 return retsig;
87 static void rtl_process_ui_rssi(struct ieee80211_hw *hw,
88 struct rtl_stats *pstatus)
90 struct rtl_priv *rtlpriv = rtl_priv(hw);
91 struct rtl_phy *rtlphy = &rtlpriv->phy;
92 u8 rfpath;
93 u32 last_rssi, tmpval;
95 if (!pstatus->packet_toself && !pstatus->packet_beacon)
96 return;
98 rtlpriv->stats.pwdb_all_cnt += pstatus->rx_pwdb_all;
99 rtlpriv->stats.rssi_calculate_cnt++;
101 if (rtlpriv->stats.ui_rssi.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
102 rtlpriv->stats.ui_rssi.total_num = PHY_RSSI_SLID_WIN_MAX;
103 last_rssi = rtlpriv->stats.ui_rssi.elements[
104 rtlpriv->stats.ui_rssi.index];
105 rtlpriv->stats.ui_rssi.total_val -= last_rssi;
107 rtlpriv->stats.ui_rssi.total_val += pstatus->signalstrength;
108 rtlpriv->stats.ui_rssi.elements[rtlpriv->stats.ui_rssi.index++] =
109 pstatus->signalstrength;
110 if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
111 rtlpriv->stats.ui_rssi.index = 0;
112 tmpval = rtlpriv->stats.ui_rssi.total_val /
113 rtlpriv->stats.ui_rssi.total_num;
114 rtlpriv->stats.signal_strength = rtl_translate_todbm(hw, (u8)tmpval);
115 pstatus->rssi = rtlpriv->stats.signal_strength;
117 if (pstatus->is_cck)
118 return;
120 for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
121 rfpath++) {
122 if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
123 rtlpriv->stats.rx_rssi_percentage[rfpath] =
124 pstatus->rx_mimo_signalstrength[rfpath];
126 if (pstatus->rx_mimo_signalstrength[rfpath] >
127 rtlpriv->stats.rx_rssi_percentage[rfpath]) {
128 rtlpriv->stats.rx_rssi_percentage[rfpath] =
129 ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
130 (RX_SMOOTH_FACTOR - 1)) +
131 (pstatus->rx_mimo_signalstrength[rfpath])) /
132 (RX_SMOOTH_FACTOR);
133 rtlpriv->stats.rx_rssi_percentage[rfpath] =
134 rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
135 } else {
136 rtlpriv->stats.rx_rssi_percentage[rfpath] =
137 ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
138 (RX_SMOOTH_FACTOR - 1)) +
139 (pstatus->rx_mimo_signalstrength[rfpath])) /
140 (RX_SMOOTH_FACTOR);
142 rtlpriv->stats.rx_snr_db[rfpath] = pstatus->rx_snr[rfpath];
143 rtlpriv->stats.rx_evm_dbm[rfpath] =
144 pstatus->rx_mimo_evm_dbm[rfpath];
145 rtlpriv->stats.rx_cfo_short[rfpath] =
146 pstatus->cfo_short[rfpath];
147 rtlpriv->stats.rx_cfo_tail[rfpath] = pstatus->cfo_tail[rfpath];
151 static void rtl_update_rxsignalstatistics(struct ieee80211_hw *hw,
152 struct rtl_stats *pstatus)
154 struct rtl_priv *rtlpriv = rtl_priv(hw);
155 int weighting = 0;
157 if (rtlpriv->stats.recv_signal_power == 0)
158 rtlpriv->stats.recv_signal_power = pstatus->recvsignalpower;
159 if (pstatus->recvsignalpower > rtlpriv->stats.recv_signal_power)
160 weighting = 5;
161 else if (pstatus->recvsignalpower < rtlpriv->stats.recv_signal_power)
162 weighting = (-5);
163 rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
164 5 + pstatus->recvsignalpower + weighting) / 6;
167 static void rtl_process_pwdb(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
169 struct rtl_priv *rtlpriv = rtl_priv(hw);
170 struct rtl_sta_info *drv_priv = NULL;
171 struct ieee80211_sta *sta = NULL;
172 long undec_sm_pwdb;
174 rcu_read_lock();
175 if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
176 sta = rtl_find_sta(hw, pstatus->psaddr);
178 /* adhoc or ap mode */
179 if (sta) {
180 drv_priv = (struct rtl_sta_info *)sta->drv_priv;
181 undec_sm_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
182 } else {
183 undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
186 if (undec_sm_pwdb < 0)
187 undec_sm_pwdb = pstatus->rx_pwdb_all;
188 if (pstatus->rx_pwdb_all > (u32)undec_sm_pwdb) {
189 undec_sm_pwdb = (((undec_sm_pwdb) *
190 (RX_SMOOTH_FACTOR - 1)) +
191 (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
192 undec_sm_pwdb = undec_sm_pwdb + 1;
193 } else {
194 undec_sm_pwdb = (((undec_sm_pwdb) *
195 (RX_SMOOTH_FACTOR - 1)) +
196 (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
199 if (sta)
200 drv_priv->rssi_stat.undec_sm_pwdb = undec_sm_pwdb;
201 else
202 rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
203 rcu_read_unlock();
205 rtl_update_rxsignalstatistics(hw, pstatus);
208 static void rtl_process_ui_link_quality(struct ieee80211_hw *hw,
209 struct rtl_stats *pstatus)
211 struct rtl_priv *rtlpriv = rtl_priv(hw);
212 u32 last_evm, n_stream, tmpval;
214 if (pstatus->signalquality == 0)
215 return;
217 if (rtlpriv->stats.ui_link_quality.total_num++ >=
218 PHY_LINKQUALITY_SLID_WIN_MAX) {
219 rtlpriv->stats.ui_link_quality.total_num =
220 PHY_LINKQUALITY_SLID_WIN_MAX;
221 last_evm = rtlpriv->stats.ui_link_quality.elements[
222 rtlpriv->stats.ui_link_quality.index];
223 rtlpriv->stats.ui_link_quality.total_val -= last_evm;
225 rtlpriv->stats.ui_link_quality.total_val += pstatus->signalquality;
226 rtlpriv->stats.ui_link_quality.elements[
227 rtlpriv->stats.ui_link_quality.index++] =
228 pstatus->signalquality;
229 if (rtlpriv->stats.ui_link_quality.index >=
230 PHY_LINKQUALITY_SLID_WIN_MAX)
231 rtlpriv->stats.ui_link_quality.index = 0;
232 tmpval = rtlpriv->stats.ui_link_quality.total_val /
233 rtlpriv->stats.ui_link_quality.total_num;
234 rtlpriv->stats.signal_quality = tmpval;
235 rtlpriv->stats.last_sigstrength_inpercent = tmpval;
236 for (n_stream = 0; n_stream < 2; n_stream++) {
237 if (pstatus->rx_mimo_sig_qual[n_stream] != -1) {
238 if (rtlpriv->stats.rx_evm_percentage[n_stream] == 0) {
239 rtlpriv->stats.rx_evm_percentage[n_stream] =
240 pstatus->rx_mimo_sig_qual[n_stream];
242 rtlpriv->stats.rx_evm_percentage[n_stream] =
243 ((rtlpriv->stats.rx_evm_percentage[n_stream]
244 * (RX_SMOOTH_FACTOR - 1)) +
245 (pstatus->rx_mimo_sig_qual[n_stream] * 1)) /
246 (RX_SMOOTH_FACTOR);
251 void rtl_process_phyinfo(struct ieee80211_hw *hw, u8 *buffer,
252 struct rtl_stats *pstatus)
254 if (!pstatus->packet_matchbssid)
255 return;
257 rtl_process_ui_rssi(hw, pstatus);
258 rtl_process_pwdb(hw, pstatus);
259 rtl_process_ui_link_quality(hw, pstatus);