1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2009-2012 Realtek Corporation.
7 * wlanfae <wlanfae@realtek.com>
8 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
11 * Larry Finger <Larry.Finger@lwfinger.net>
13 *****************************************************************************/
16 #include <linux/export.h>
18 u8
rtl_query_rxpwrpercentage(s8 antpower
)
20 if ((antpower
<= -100) || (antpower
>= 20))
22 else if (antpower
>= 0)
25 return 100 + antpower
;
28 u8
rtl_evm_db_to_percentage(s8 value
)
30 s8 ret_val
= clamp(-value
, 0, 33) * 3;
38 static long rtl_translate_todbm(struct ieee80211_hw
*hw
,
39 u8 signal_strength_index
)
43 signal_power
= (long)((signal_strength_index
+ 1) >> 1);
48 long rtl_signal_scale_mapping(struct ieee80211_hw
*hw
, long currsig
)
52 if (currsig
>= 61 && currsig
<= 100)
53 retsig
= 90 + ((currsig
- 60) / 4);
54 else if (currsig
>= 41 && currsig
<= 60)
55 retsig
= 78 + ((currsig
- 40) / 2);
56 else if (currsig
>= 31 && currsig
<= 40)
57 retsig
= 66 + (currsig
- 30);
58 else if (currsig
>= 21 && currsig
<= 30)
59 retsig
= 54 + (currsig
- 20);
60 else if (currsig
>= 5 && currsig
<= 20)
61 retsig
= 42 + (((currsig
- 5) * 2) / 3);
62 else if (currsig
== 4)
64 else if (currsig
== 3)
66 else if (currsig
== 2)
68 else if (currsig
== 1)
76 static void rtl_process_ui_rssi(struct ieee80211_hw
*hw
,
77 struct rtl_stats
*pstatus
)
79 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
80 struct rtl_phy
*rtlphy
= &rtlpriv
->phy
;
82 u32 last_rssi
, tmpval
;
84 if (!pstatus
->packet_toself
&& !pstatus
->packet_beacon
)
87 rtlpriv
->stats
.pwdb_all_cnt
+= pstatus
->rx_pwdb_all
;
88 rtlpriv
->stats
.rssi_calculate_cnt
++;
90 if (rtlpriv
->stats
.ui_rssi
.total_num
++ >= PHY_RSSI_SLID_WIN_MAX
) {
91 rtlpriv
->stats
.ui_rssi
.total_num
= PHY_RSSI_SLID_WIN_MAX
;
92 last_rssi
= rtlpriv
->stats
.ui_rssi
.elements
[
93 rtlpriv
->stats
.ui_rssi
.index
];
94 rtlpriv
->stats
.ui_rssi
.total_val
-= last_rssi
;
96 rtlpriv
->stats
.ui_rssi
.total_val
+= pstatus
->signalstrength
;
97 rtlpriv
->stats
.ui_rssi
.elements
[rtlpriv
->stats
.ui_rssi
.index
++] =
98 pstatus
->signalstrength
;
99 if (rtlpriv
->stats
.ui_rssi
.index
>= PHY_RSSI_SLID_WIN_MAX
)
100 rtlpriv
->stats
.ui_rssi
.index
= 0;
101 tmpval
= rtlpriv
->stats
.ui_rssi
.total_val
/
102 rtlpriv
->stats
.ui_rssi
.total_num
;
103 rtlpriv
->stats
.signal_strength
= rtl_translate_todbm(hw
, (u8
)tmpval
);
104 pstatus
->rssi
= rtlpriv
->stats
.signal_strength
;
109 for (rfpath
= RF90_PATH_A
; rfpath
< rtlphy
->num_total_rfpath
;
111 if (rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] == 0) {
112 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
113 pstatus
->rx_mimo_signalstrength
[rfpath
];
115 if (pstatus
->rx_mimo_signalstrength
[rfpath
] >
116 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
]) {
117 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
118 ((rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] *
119 (RX_SMOOTH_FACTOR
- 1)) +
120 (pstatus
->rx_mimo_signalstrength
[rfpath
])) /
122 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
123 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] + 1;
125 rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] =
126 ((rtlpriv
->stats
.rx_rssi_percentage
[rfpath
] *
127 (RX_SMOOTH_FACTOR
- 1)) +
128 (pstatus
->rx_mimo_signalstrength
[rfpath
])) /
131 rtlpriv
->stats
.rx_snr_db
[rfpath
] = pstatus
->rx_snr
[rfpath
];
132 rtlpriv
->stats
.rx_evm_dbm
[rfpath
] =
133 pstatus
->rx_mimo_evm_dbm
[rfpath
];
134 rtlpriv
->stats
.rx_cfo_short
[rfpath
] =
135 pstatus
->cfo_short
[rfpath
];
136 rtlpriv
->stats
.rx_cfo_tail
[rfpath
] = pstatus
->cfo_tail
[rfpath
];
140 static void rtl_update_rxsignalstatistics(struct ieee80211_hw
*hw
,
141 struct rtl_stats
*pstatus
)
143 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
146 if (rtlpriv
->stats
.recv_signal_power
== 0)
147 rtlpriv
->stats
.recv_signal_power
= pstatus
->recvsignalpower
;
148 if (pstatus
->recvsignalpower
> rtlpriv
->stats
.recv_signal_power
)
150 else if (pstatus
->recvsignalpower
< rtlpriv
->stats
.recv_signal_power
)
152 rtlpriv
->stats
.recv_signal_power
= (rtlpriv
->stats
.recv_signal_power
*
153 5 + pstatus
->recvsignalpower
+ weighting
) / 6;
156 static void rtl_process_pwdb(struct ieee80211_hw
*hw
, struct rtl_stats
*pstatus
)
158 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
159 struct rtl_sta_info
*drv_priv
= NULL
;
160 struct ieee80211_sta
*sta
= NULL
;
164 if (rtlpriv
->mac80211
.opmode
!= NL80211_IFTYPE_STATION
)
165 sta
= rtl_find_sta(hw
, pstatus
->psaddr
);
167 /* adhoc or ap mode */
169 drv_priv
= (struct rtl_sta_info
*)sta
->drv_priv
;
170 undec_sm_pwdb
= drv_priv
->rssi_stat
.undec_sm_pwdb
;
172 undec_sm_pwdb
= rtlpriv
->dm
.undec_sm_pwdb
;
175 if (undec_sm_pwdb
< 0)
176 undec_sm_pwdb
= pstatus
->rx_pwdb_all
;
177 if (pstatus
->rx_pwdb_all
> (u32
)undec_sm_pwdb
) {
178 undec_sm_pwdb
= (((undec_sm_pwdb
) *
179 (RX_SMOOTH_FACTOR
- 1)) +
180 (pstatus
->rx_pwdb_all
)) / (RX_SMOOTH_FACTOR
);
181 undec_sm_pwdb
= undec_sm_pwdb
+ 1;
183 undec_sm_pwdb
= (((undec_sm_pwdb
) *
184 (RX_SMOOTH_FACTOR
- 1)) +
185 (pstatus
->rx_pwdb_all
)) / (RX_SMOOTH_FACTOR
);
189 drv_priv
->rssi_stat
.undec_sm_pwdb
= undec_sm_pwdb
;
191 rtlpriv
->dm
.undec_sm_pwdb
= undec_sm_pwdb
;
194 rtl_update_rxsignalstatistics(hw
, pstatus
);
197 static void rtl_process_ui_link_quality(struct ieee80211_hw
*hw
,
198 struct rtl_stats
*pstatus
)
200 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
201 u32 last_evm
, n_stream
, tmpval
;
203 if (pstatus
->signalquality
== 0)
206 if (rtlpriv
->stats
.ui_link_quality
.total_num
++ >=
207 PHY_LINKQUALITY_SLID_WIN_MAX
) {
208 rtlpriv
->stats
.ui_link_quality
.total_num
=
209 PHY_LINKQUALITY_SLID_WIN_MAX
;
210 last_evm
= rtlpriv
->stats
.ui_link_quality
.elements
[
211 rtlpriv
->stats
.ui_link_quality
.index
];
212 rtlpriv
->stats
.ui_link_quality
.total_val
-= last_evm
;
214 rtlpriv
->stats
.ui_link_quality
.total_val
+= pstatus
->signalquality
;
215 rtlpriv
->stats
.ui_link_quality
.elements
[
216 rtlpriv
->stats
.ui_link_quality
.index
++] =
217 pstatus
->signalquality
;
218 if (rtlpriv
->stats
.ui_link_quality
.index
>=
219 PHY_LINKQUALITY_SLID_WIN_MAX
)
220 rtlpriv
->stats
.ui_link_quality
.index
= 0;
221 tmpval
= rtlpriv
->stats
.ui_link_quality
.total_val
/
222 rtlpriv
->stats
.ui_link_quality
.total_num
;
223 rtlpriv
->stats
.signal_quality
= tmpval
;
224 rtlpriv
->stats
.last_sigstrength_inpercent
= tmpval
;
225 for (n_stream
= 0; n_stream
< 2; n_stream
++) {
226 if (pstatus
->rx_mimo_sig_qual
[n_stream
] != -1) {
227 if (rtlpriv
->stats
.rx_evm_percentage
[n_stream
] == 0) {
228 rtlpriv
->stats
.rx_evm_percentage
[n_stream
] =
229 pstatus
->rx_mimo_sig_qual
[n_stream
];
231 rtlpriv
->stats
.rx_evm_percentage
[n_stream
] =
232 ((rtlpriv
->stats
.rx_evm_percentage
[n_stream
]
233 * (RX_SMOOTH_FACTOR
- 1)) +
234 (pstatus
->rx_mimo_sig_qual
[n_stream
] * 1)) /
240 void rtl_process_phyinfo(struct ieee80211_hw
*hw
, u8
*buffer
,
241 struct rtl_stats
*pstatus
)
243 if (!pstatus
->packet_matchbssid
)
246 rtl_process_ui_rssi(hw
, pstatus
);
247 rtl_process_pwdb(hw
, pstatus
);
248 rtl_process_ui_link_quality(hw
, pstatus
);