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
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 *****************************************************************************/
31 #include <linux/export.h>
33 u8
rtl_query_rxpwrpercentage(char antpower
)
35 if ((antpower
<= -100) || (antpower
>= 20))
37 else if (antpower
>= 0)
40 return 100 + antpower
;
42 EXPORT_SYMBOL(rtl_query_rxpwrpercentage
);
44 u8
rtl_evm_db_to_percentage(char value
)
53 ret_val
= 0 - ret_val
;
60 EXPORT_SYMBOL(rtl_evm_db_to_percentage
);
62 static long rtl_translate_todbm(struct ieee80211_hw
*hw
,
63 u8 signal_strength_index
)
67 signal_power
= (long)((signal_strength_index
+ 1) >> 1);
72 long rtl_signal_scale_mapping(struct ieee80211_hw
*hw
, long currsig
)
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)
88 else if (currsig
== 3)
90 else if (currsig
== 2)
92 else if (currsig
== 1)
99 EXPORT_SYMBOL(rtl_signal_scale_mapping
);
101 static void rtl_process_ui_rssi(struct ieee80211_hw
*hw
,
102 struct rtl_stats
*pstatus
)
104 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
105 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
107 u32 last_rssi
, tmpval
;
109 rtlpriv
->stats
.rssi_calculate_cnt
++;
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
;
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
,
126 pstatus
->rssi
= rtlpriv
->stats
.signal_strength
;
131 for (rfpath
= RF90_PATH_A
; rfpath
< rtlphy
->num_total_rfpath
;
133 if (rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] == 0) {
134 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
135 pstatus
->rx_mimo_signalstrength
[rfpath
];
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
])) /
145 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
146 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] + 1;
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
])) /
157 static void rtl_update_rxsignalstatistics(struct ieee80211_hw
*hw
,
158 struct rtl_stats
*pstatus
)
160 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
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
)
167 else if (pstatus
->recvsignalpower
< rtlpriv
->stats
.recv_signal_power
)
169 rtlpriv
->stats
.recv_signal_power
= (rtlpriv
->stats
.recv_signal_power
*
170 5 + pstatus
->recvsignalpower
+ weighting
) / 6;
173 static void rtl_process_pwdb(struct ieee80211_hw
*hw
, struct rtl_stats
*pstatus
)
175 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
176 struct rtl_sta_info
*drv_priv
= NULL
;
177 struct ieee80211_sta
*sta
= NULL
;
182 if (rtlpriv
->mac80211
.opmode
!= NL80211_IFTYPE_STATION
)
183 sta
= rtl_find_sta(hw
, pstatus
->psaddr
);
185 /* adhoc or ap mode */
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
;
191 undec_sm_pwdb
= rtlpriv
->dm
.undec_sm_pwdb
;
192 undec_sm_cck
= rtlpriv
->dm
.undec_sm_cck
;
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;
205 undec_sm_pwdb
= (((undec_sm_pwdb
) * (RX_SMOOTH_FACTOR
- 1)) +
206 (pstatus
->rx_pwdb_all
)) / (RX_SMOOTH_FACTOR
);
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;
214 undec_sm_pwdb
= (((undec_sm_cck
) * (RX_SMOOTH_FACTOR
- 1)) +
215 (pstatus
->rx_pwdb_all
)) / (RX_SMOOTH_FACTOR
);
219 drv_priv
->rssi_stat
.undec_sm_pwdb
= undec_sm_pwdb
;
221 rtlpriv
->dm
.undec_sm_pwdb
= undec_sm_pwdb
;
225 rtl_update_rxsignalstatistics(hw
, pstatus
);
228 static void rtl_process_ui_link_quality(struct ieee80211_hw
*hw
,
229 struct rtl_stats
*pstatus
)
231 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
232 u32 last_evm
, n_stream
, tmpval
;
234 if (pstatus
->signalquality
== 0)
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
;
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
];
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)) /
271 void rtl_process_phyinfo(struct ieee80211_hw
*hw
, u8
*buffer
,
272 struct rtl_stats
*pstatus
)
275 if (!pstatus
->packet_matchbssid
)
278 rtl_process_ui_rssi(hw
, pstatus
);
279 rtl_process_pwdb(hw
, pstatus
);
280 rtl_process_ui_link_quality(hw
, pstatus
);
282 EXPORT_SYMBOL(rtl_process_phyinfo
);