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 * 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 *****************************************************************************/
27 #include <linux/export.h>
29 u8
rtl_query_rxpwrpercentage(char antpower
)
31 if ((antpower
<= -100) || (antpower
>= 20))
33 else if (antpower
>= 0)
36 return 100 + antpower
;
38 EXPORT_SYMBOL(rtl_query_rxpwrpercentage
);
40 u8
rtl_evm_db_to_percentage(char value
)
42 char ret_val
= clamp(-value
, 0, 33) * 3;
49 EXPORT_SYMBOL(rtl_evm_db_to_percentage
);
51 static long rtl_translate_todbm(struct ieee80211_hw
*hw
,
52 u8 signal_strength_index
)
56 signal_power
= (long)((signal_strength_index
+ 1) >> 1);
61 long rtl_signal_scale_mapping(struct ieee80211_hw
*hw
, long currsig
)
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)
77 else if (currsig
== 3)
79 else if (currsig
== 2)
81 else if (currsig
== 1)
88 EXPORT_SYMBOL(rtl_signal_scale_mapping
);
90 static void rtl_process_ui_rssi(struct ieee80211_hw
*hw
,
91 struct rtl_stats
*pstatus
)
93 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
94 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
96 u32 last_rssi
, tmpval
;
98 if (!pstatus
->packet_toself
&& !pstatus
->packet_beacon
)
101 rtlpriv
->stats
.pwdb_all_cnt
+= pstatus
->rx_pwdb_all
;
102 rtlpriv
->stats
.rssi_calculate_cnt
++;
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
;
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
,
119 pstatus
->rssi
= rtlpriv
->stats
.signal_strength
;
124 for (rfpath
= RF90_PATH_A
; rfpath
< rtlphy
->num_total_rfpath
;
126 if (rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] == 0) {
127 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
128 pstatus
->rx_mimo_signalstrength
[rfpath
];
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
])) /
138 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
139 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] + 1;
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
])) /
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
];
156 static void rtl_update_rxsignalstatistics(struct ieee80211_hw
*hw
,
157 struct rtl_stats
*pstatus
)
159 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
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
)
166 else if (pstatus
->recvsignalpower
< rtlpriv
->stats
.recv_signal_power
)
168 rtlpriv
->stats
.recv_signal_power
= (rtlpriv
->stats
.recv_signal_power
*
169 5 + pstatus
->recvsignalpower
+ weighting
) / 6;
172 static void rtl_process_pwdb(struct ieee80211_hw
*hw
, struct rtl_stats
*pstatus
)
174 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
175 struct rtl_sta_info
*drv_priv
= NULL
;
176 struct ieee80211_sta
*sta
= NULL
;
180 if (rtlpriv
->mac80211
.opmode
!= NL80211_IFTYPE_STATION
)
181 sta
= rtl_find_sta(hw
, pstatus
->psaddr
);
183 /* adhoc or ap mode */
185 drv_priv
= (struct rtl_sta_info
*) sta
->drv_priv
;
186 undec_sm_pwdb
= drv_priv
->rssi_stat
.undec_sm_pwdb
;
188 undec_sm_pwdb
= rtlpriv
->dm
.undec_sm_pwdb
;
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;
199 undec_sm_pwdb
= (((undec_sm_pwdb
) *
200 (RX_SMOOTH_FACTOR
- 1)) +
201 (pstatus
->rx_pwdb_all
)) / (RX_SMOOTH_FACTOR
);
205 drv_priv
->rssi_stat
.undec_sm_pwdb
= undec_sm_pwdb
;
207 rtlpriv
->dm
.undec_sm_pwdb
= undec_sm_pwdb
;
211 rtl_update_rxsignalstatistics(hw
, pstatus
);
214 static void rtl_process_ui_link_quality(struct ieee80211_hw
*hw
,
215 struct rtl_stats
*pstatus
)
217 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
218 u32 last_evm
, n_stream
, tmpval
;
220 if (pstatus
->signalquality
== 0)
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
;
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
];
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)) /
257 void rtl_process_phyinfo(struct ieee80211_hw
*hw
, u8
*buffer
,
258 struct rtl_stats
*pstatus
)
261 if (!pstatus
->packet_matchbssid
)
264 rtl_process_ui_rssi(hw
, pstatus
);
265 rtl_process_pwdb(hw
, pstatus
);
266 rtl_process_ui_link_quality(hw
, pstatus
);
268 EXPORT_SYMBOL(rtl_process_phyinfo
);