Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / staging / rtl8821ae / stats.c
bloba20c0f8f65ecaaec0b518410500ef1a3ab1fc933
1 /******************************************************************************
3 * Copyright(c) 2009-2010 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 * 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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 * Larry Finger <Larry.Finger@lwfinger.net>
28 *****************************************************************************/
29 #include "wifi.h"
30 #include "stats.h"
31 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0))
32 #include <linux/export.h>
33 #endif
35 u8 rtl_query_rxpwrpercentage(char antpower)
37 if ((antpower <= -100) || (antpower >= 20))
38 return 0;
39 else if (antpower >= 0)
40 return 100;
41 else
42 return (100 + antpower);
44 //EXPORT_SYMBOL(rtl_query_rxpwrpercentage);
46 u8 rtl_evm_db_to_percentage(char value)
48 char ret_val;
49 ret_val = value;
51 if (ret_val >= 0)
52 ret_val = 0;
53 if (ret_val <= -33)
54 ret_val = -33;
55 ret_val = 0 - ret_val;
56 ret_val *= 3;
57 if (ret_val == 99)
58 ret_val = 100;
60 return ret_val;
62 //EXPORT_SYMBOL(rtl_evm_db_to_percentage);
64 long rtl_translate_todbm(struct ieee80211_hw *hw,
65 u8 signal_strength_index)
67 long signal_power;
69 signal_power = (long)((signal_strength_index + 1) >> 1);
70 signal_power -= 95;
71 return signal_power;
74 long rtl_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
76 long retsig;
78 if (currsig >= 61 && currsig <= 100)
79 retsig = 90 + ((currsig - 60) / 4);
80 else if (currsig >= 41 && currsig <= 60)
81 retsig = 78 + ((currsig - 40) / 2);
82 else if (currsig >= 31 && currsig <= 40)
83 retsig = 66 + (currsig - 30);
84 else if (currsig >= 21 && currsig <= 30)
85 retsig = 54 + (currsig - 20);
86 else if (currsig >= 5 && currsig <= 20)
87 retsig = 42 + (((currsig - 5) * 2) / 3);
88 else if (currsig == 4)
89 retsig = 36;
90 else if (currsig == 3)
91 retsig = 27;
92 else if (currsig == 2)
93 retsig = 18;
94 else if (currsig == 1)
95 retsig = 9;
96 else
97 retsig = currsig;
99 return retsig;
101 //EXPORT_SYMBOL(rtl_signal_scale_mapping);
103 void rtl_process_ui_rssi(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
105 struct rtl_priv *rtlpriv = rtl_priv(hw);
106 struct rtl_phy *rtlphy = &(rtlpriv->phy);
107 u8 rfpath;
108 u32 last_rssi, tmpval;
110 if (!pstatus->b_packet_toself && !pstatus->b_packet_beacon)
111 return;
113 rtlpriv->stats.pwdb_all_cnt += pstatus->rx_pwdb_all;
114 rtlpriv->stats.rssi_calculate_cnt++;
116 if (rtlpriv->stats.ui_rssi.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
117 rtlpriv->stats.ui_rssi.total_num = PHY_RSSI_SLID_WIN_MAX;
118 last_rssi = rtlpriv->stats.ui_rssi.elements[
119 rtlpriv->stats.ui_rssi.index];
120 rtlpriv->stats.ui_rssi.total_val -= last_rssi;
122 rtlpriv->stats.ui_rssi.total_val += pstatus->signalstrength;
123 rtlpriv->stats.ui_rssi.elements[rtlpriv->stats.ui_rssi.index++] =
124 pstatus->signalstrength;
125 if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
126 rtlpriv->stats.ui_rssi.index = 0;
127 tmpval = rtlpriv->stats.ui_rssi.total_val /
128 rtlpriv->stats.ui_rssi.total_num;
129 rtlpriv->stats.signal_strength = rtl_translate_todbm(hw,
130 (u8) tmpval);
131 pstatus->rssi = rtlpriv->stats.signal_strength;
133 if (pstatus->b_is_cck)
134 return;
136 for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
137 rfpath++) {
138 if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
139 rtlpriv->stats.rx_rssi_percentage[rfpath] =
140 pstatus->rx_mimo_signalstrength[rfpath];
143 if (pstatus->rx_mimo_signalstrength[rfpath] >
144 rtlpriv->stats.rx_rssi_percentage[rfpath]) {
145 rtlpriv->stats.rx_rssi_percentage[rfpath] =
146 ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
147 (RX_SMOOTH_FACTOR - 1)) +
148 (pstatus->rx_mimo_signalstrength[rfpath])) /
149 (RX_SMOOTH_FACTOR);
150 rtlpriv->stats.rx_rssi_percentage[rfpath] =
151 rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
152 } else {
153 rtlpriv->stats.rx_rssi_percentage[rfpath] =
154 ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
155 (RX_SMOOTH_FACTOR - 1)) +
156 (pstatus->rx_mimo_signalstrength[rfpath])) /
157 (RX_SMOOTH_FACTOR);
159 rtlpriv->stats.rx_snr_db[rfpath] = pstatus->rx_snr[rfpath];
160 rtlpriv->stats.rx_evm_dbm[rfpath] =
161 pstatus->rx_mimo_evm_dbm[rfpath];
162 rtlpriv->stats.rx_cfo_short[rfpath] =
163 pstatus->cfo_short[rfpath];
164 rtlpriv->stats.rx_cfo_tail[rfpath] = pstatus->cfo_tail[rfpath];
168 static void rtl_update_rxsignalstatistics(struct ieee80211_hw *hw,
169 struct rtl_stats *pstatus)
171 struct rtl_priv *rtlpriv = rtl_priv(hw);
172 int weighting = 0;
174 if (rtlpriv->stats.recv_signal_power == 0)
175 rtlpriv->stats.recv_signal_power = pstatus->recvsignalpower;
176 if (pstatus->recvsignalpower > rtlpriv->stats.recv_signal_power)
177 weighting = 5;
178 else if (pstatus->recvsignalpower < rtlpriv->stats.recv_signal_power)
179 weighting = (-5);
180 rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
181 5 + pstatus->recvsignalpower + weighting) / 6;
184 static void rtl_process_pwdb(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
186 struct rtl_priv *rtlpriv = rtl_priv(hw);
187 struct rtl_sta_info *drv_priv = NULL;
188 struct ieee80211_sta *sta = NULL;
189 long undecorated_smoothed_pwdb;
191 rcu_read_lock();
192 if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
193 sta = rtl_find_sta(hw, pstatus->psaddr);
195 /* adhoc or ap mode */
196 if (sta) {
197 drv_priv = (struct rtl_sta_info *) sta->drv_priv;
198 undecorated_smoothed_pwdb =
199 drv_priv->rssi_stat.undecorated_smoothed_pwdb;
200 } else {
201 undecorated_smoothed_pwdb =
202 rtlpriv->dm.undecorated_smoothed_pwdb;
205 if (undecorated_smoothed_pwdb < 0)
206 undecorated_smoothed_pwdb = pstatus->rx_pwdb_all;
207 if (pstatus->rx_pwdb_all > (u32) undecorated_smoothed_pwdb) {
208 undecorated_smoothed_pwdb = (((undecorated_smoothed_pwdb) *
209 (RX_SMOOTH_FACTOR - 1)) +
210 (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
211 undecorated_smoothed_pwdb = undecorated_smoothed_pwdb + 1;
212 } else {
213 undecorated_smoothed_pwdb = (((undecorated_smoothed_pwdb) *
214 (RX_SMOOTH_FACTOR - 1)) +
215 (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
218 if(sta) {
219 drv_priv->rssi_stat.undecorated_smoothed_pwdb =
220 undecorated_smoothed_pwdb;
221 } else {
222 rtlpriv->dm.undecorated_smoothed_pwdb = undecorated_smoothed_pwdb;
224 rcu_read_unlock();
226 rtl_update_rxsignalstatistics(hw, pstatus);
229 static void rtl_process_ui_link_quality(struct ieee80211_hw *hw,
230 struct rtl_stats *pstatus)
232 struct rtl_priv *rtlpriv = rtl_priv(hw);
233 u32 last_evm, n_stream, tmpval;
235 if (pstatus->signalquality == 0)
236 return;
238 if (rtlpriv->stats.ui_link_quality.total_num++ >=
239 PHY_LINKQUALITY_SLID_WIN_MAX) {
240 rtlpriv->stats.ui_link_quality.total_num =
241 PHY_LINKQUALITY_SLID_WIN_MAX;
242 last_evm = rtlpriv->stats.ui_link_quality.elements[
243 rtlpriv->stats.ui_link_quality.index];
244 rtlpriv->stats.ui_link_quality.total_val -= last_evm;
246 rtlpriv->stats.ui_link_quality.total_val += pstatus->signalquality;
247 rtlpriv->stats.ui_link_quality.elements[
248 rtlpriv->stats.ui_link_quality.index++] =
249 pstatus->signalquality;
250 if (rtlpriv->stats.ui_link_quality.index >=
251 PHY_LINKQUALITY_SLID_WIN_MAX)
252 rtlpriv->stats.ui_link_quality.index = 0;
253 tmpval = rtlpriv->stats.ui_link_quality.total_val /
254 rtlpriv->stats.ui_link_quality.total_num;
255 rtlpriv->stats.signal_quality = tmpval;
256 rtlpriv->stats.last_sigstrength_inpercent = tmpval;
257 for (n_stream = 0; n_stream < 2; n_stream++) {
258 if (pstatus->rx_mimo_signalquality[n_stream] != -1) {
259 if (rtlpriv->stats.rx_evm_percentage[n_stream] == 0) {
260 rtlpriv->stats.rx_evm_percentage[n_stream] =
261 pstatus->rx_mimo_signalquality[n_stream];
263 rtlpriv->stats.rx_evm_percentage[n_stream] =
264 ((rtlpriv->stats.rx_evm_percentage[n_stream]
265 * (RX_SMOOTH_FACTOR - 1)) +
266 (pstatus->rx_mimo_signalquality[n_stream] * 1)) /
267 (RX_SMOOTH_FACTOR);
272 void rtl_process_phyinfo(struct ieee80211_hw *hw, u8 *buffer,
273 struct rtl_stats *pstatus)
276 if (!pstatus->b_packet_matchbssid)
277 return;
279 rtl_process_ui_rssi(hw, pstatus);
280 rtl_process_pwdb(hw, pstatus);
281 rtl_process_ui_link_quality(hw, pstatus);
283 //EXPORT_SYMBOL(rtl_process_phyinfo);