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 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
16 *
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
21 *
22 * Larry Finger <Larry.Finger@lwfinger.net>
23 *
24 *****************************************************************************/
25#include "wifi.h"
26#include "stats.h"
27#include <linux/export.h>
28
29u8 rtl_query_rxpwrpercentage(char antpower)
30{
31	if ((antpower <= -100) || (antpower >= 20))
32		return 0;
33	else if (antpower >= 0)
34		return 100;
35	else
36		return 100 + antpower;
37}
38EXPORT_SYMBOL(rtl_query_rxpwrpercentage);
39
40u8 rtl_evm_db_to_percentage(char value)
41{
42	char ret_val = clamp(-value, 0, 33) * 3;
43
44	if (ret_val == 99)
45		ret_val = 100;
46
47	return ret_val;
48}
49EXPORT_SYMBOL(rtl_evm_db_to_percentage);
50
51static long rtl_translate_todbm(struct ieee80211_hw *hw,
52			 u8 signal_strength_index)
53{
54	long signal_power;
55
56	signal_power = (long)((signal_strength_index + 1) >> 1);
57	signal_power -= 95;
58	return signal_power;
59}
60
61long rtl_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
62{
63	long retsig;
64
65	if (currsig >= 61 && currsig <= 100)
66		retsig = 90 + ((currsig - 60) / 4);
67	else if (currsig >= 41 && currsig <= 60)
68		retsig = 78 + ((currsig - 40) / 2);
69	else if (currsig >= 31 && currsig <= 40)
70		retsig = 66 + (currsig - 30);
71	else if (currsig >= 21 && currsig <= 30)
72		retsig = 54 + (currsig - 20);
73	else if (currsig >= 5 && currsig <= 20)
74		retsig = 42 + (((currsig - 5) * 2) / 3);
75	else if (currsig == 4)
76		retsig = 36;
77	else if (currsig == 3)
78		retsig = 27;
79	else if (currsig == 2)
80		retsig = 18;
81	else if (currsig == 1)
82		retsig = 9;
83	else
84		retsig = currsig;
85
86	return retsig;
87}
88EXPORT_SYMBOL(rtl_signal_scale_mapping);
89
90static void rtl_process_ui_rssi(struct ieee80211_hw *hw,
91				struct rtl_stats *pstatus)
92{
93	struct rtl_priv *rtlpriv = rtl_priv(hw);
94	struct rtl_phy *rtlphy = &(rtlpriv->phy);
95	u8 rfpath;
96	u32 last_rssi, tmpval;
97
98	if (!pstatus->packet_toself && !pstatus->packet_beacon)
99		return;
100
101	rtlpriv->stats.pwdb_all_cnt += pstatus->rx_pwdb_all;
102	rtlpriv->stats.rssi_calculate_cnt++;
103
104	if (rtlpriv->stats.ui_rssi.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
105		rtlpriv->stats.ui_rssi.total_num = PHY_RSSI_SLID_WIN_MAX;
106		last_rssi = rtlpriv->stats.ui_rssi.elements[
107			rtlpriv->stats.ui_rssi.index];
108		rtlpriv->stats.ui_rssi.total_val -= last_rssi;
109	}
110	rtlpriv->stats.ui_rssi.total_val += pstatus->signalstrength;
111	rtlpriv->stats.ui_rssi.elements[rtlpriv->stats.ui_rssi.index++] =
112	    pstatus->signalstrength;
113	if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
114		rtlpriv->stats.ui_rssi.index = 0;
115	tmpval = rtlpriv->stats.ui_rssi.total_val /
116		rtlpriv->stats.ui_rssi.total_num;
117	rtlpriv->stats.signal_strength = rtl_translate_todbm(hw,
118		(u8) tmpval);
119	pstatus->rssi = rtlpriv->stats.signal_strength;
120
121	if (pstatus->is_cck)
122		return;
123
124	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
125	     rfpath++) {
126		if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
127			rtlpriv->stats.rx_rssi_percentage[rfpath] =
128			    pstatus->rx_mimo_signalstrength[rfpath];
129
130		}
131		if (pstatus->rx_mimo_signalstrength[rfpath] >
132		    rtlpriv->stats.rx_rssi_percentage[rfpath]) {
133			rtlpriv->stats.rx_rssi_percentage[rfpath] =
134			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
135			      (RX_SMOOTH_FACTOR - 1)) +
136			     (pstatus->rx_mimo_signalstrength[rfpath])) /
137			    (RX_SMOOTH_FACTOR);
138			rtlpriv->stats.rx_rssi_percentage[rfpath] =
139			    rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
140		} else {
141			rtlpriv->stats.rx_rssi_percentage[rfpath] =
142			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
143			      (RX_SMOOTH_FACTOR - 1)) +
144			     (pstatus->rx_mimo_signalstrength[rfpath])) /
145			    (RX_SMOOTH_FACTOR);
146		}
147		rtlpriv->stats.rx_snr_db[rfpath] = pstatus->rx_snr[rfpath];
148		rtlpriv->stats.rx_evm_dbm[rfpath] =
149					pstatus->rx_mimo_evm_dbm[rfpath];
150		rtlpriv->stats.rx_cfo_short[rfpath] =
151					pstatus->cfo_short[rfpath];
152		rtlpriv->stats.rx_cfo_tail[rfpath] = pstatus->cfo_tail[rfpath];
153	}
154}
155
156static void rtl_update_rxsignalstatistics(struct ieee80211_hw *hw,
157					  struct rtl_stats *pstatus)
158{
159	struct rtl_priv *rtlpriv = rtl_priv(hw);
160	int weighting = 0;
161
162	if (rtlpriv->stats.recv_signal_power == 0)
163		rtlpriv->stats.recv_signal_power = pstatus->recvsignalpower;
164	if (pstatus->recvsignalpower > rtlpriv->stats.recv_signal_power)
165		weighting = 5;
166	else if (pstatus->recvsignalpower < rtlpriv->stats.recv_signal_power)
167		weighting = (-5);
168	rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
169		5 + pstatus->recvsignalpower + weighting) / 6;
170}
171
172static void rtl_process_pwdb(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
173{
174	struct rtl_priv *rtlpriv = rtl_priv(hw);
175	struct rtl_sta_info *drv_priv = NULL;
176	struct ieee80211_sta *sta = NULL;
177	long undec_sm_pwdb;
178
179	rcu_read_lock();
180	if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
181		sta = rtl_find_sta(hw, pstatus->psaddr);
182
183	/* adhoc or ap mode */
184	if (sta) {
185		drv_priv = (struct rtl_sta_info *) sta->drv_priv;
186		undec_sm_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
187	} else {
188		undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
189	}
190
191	if (undec_sm_pwdb < 0)
192		undec_sm_pwdb = pstatus->rx_pwdb_all;
193	if (pstatus->rx_pwdb_all > (u32) undec_sm_pwdb) {
194		undec_sm_pwdb = (((undec_sm_pwdb) *
195		      (RX_SMOOTH_FACTOR - 1)) +
196		     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
197		undec_sm_pwdb = undec_sm_pwdb + 1;
198	} else {
199		undec_sm_pwdb = (((undec_sm_pwdb) *
200		      (RX_SMOOTH_FACTOR - 1)) +
201		     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
202	}
203
204	if (sta) {
205		drv_priv->rssi_stat.undec_sm_pwdb = undec_sm_pwdb;
206	} else {
207		rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
208	}
209	rcu_read_unlock();
210
211	rtl_update_rxsignalstatistics(hw, pstatus);
212}
213
214static 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
257void 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}
268EXPORT_SYMBOL(rtl_process_phyinfo);
269