1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
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
19 ******************************************************************************/
23 #include "odm_precomp.h"
25 #define READ_AND_CONFIG READ_AND_CONFIG_MP
27 #define READ_AND_CONFIG_MP(ic, txt) (ODM_ReadAndConfig##txt##ic(dm_odm))
28 #define READ_AND_CONFIG_TC(ic, txt) (ODM_ReadAndConfig_TC##txt##ic(dm_odm))
30 static u8 odm_QueryRxPwrPercentage(s8 AntPower)
32 if ((AntPower <= -100) || (AntPower >= 20))
34 else if (AntPower >= 0)
40 /* 2012/01/12 MH MOve some signal strength smooth method to MP HAL layer. */
41 /* IF other SW team do not support the feature, remove this section.?? */
42 static s32 odm_SignalScaleMapping_92CSeries(struct odm_dm_struct *dm_odm, s32 CurrSig)
46 if (CurrSig >= 51 && CurrSig <= 100)
48 else if (CurrSig >= 41 && CurrSig <= 50)
49 RetSig = 80 + ((CurrSig - 40)*2);
50 else if (CurrSig >= 31 && CurrSig <= 40)
51 RetSig = 66 + (CurrSig - 30);
52 else if (CurrSig >= 21 && CurrSig <= 30)
53 RetSig = 54 + (CurrSig - 20);
54 else if (CurrSig >= 10 && CurrSig <= 20)
55 RetSig = 42 + (((CurrSig - 10) * 2) / 3);
56 else if (CurrSig >= 5 && CurrSig <= 9)
57 RetSig = 22 + (((CurrSig - 5) * 3) / 2);
58 else if (CurrSig >= 1 && CurrSig <= 4)
59 RetSig = 6 + (((CurrSig - 1) * 3) / 2);
65 static s32 odm_SignalScaleMapping(struct odm_dm_struct *dm_odm, s32 CurrSig)
67 return odm_SignalScaleMapping_92CSeries(dm_odm, CurrSig);
70 static u8 odm_EVMdbToPercentage(s8 Value)
72 /* -33dB~0dB to 0%~99% */
82 ret_val = 0 - ret_val;
90 static void odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct *dm_odm,
91 struct odm_phy_status_info *pPhyInfo,
93 struct odm_per_pkt_info *pPktinfo)
95 struct sw_ant_switch *pDM_SWAT_Table = &dm_odm->DM_SWAT_Table;
96 u8 i, Max_spatial_stream;
97 s8 rx_pwr[4], rx_pwr_all = 0;
98 u8 EVM, PWDB_ALL = 0, PWDB_ALL_BT;
99 u8 RSSI, total_rssi = 0;
105 struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
107 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
109 pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = -1;
110 pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
115 dm_odm->PhyDbgInfo.NumQryPhyStatusCCK++;
116 /* (1)Hardware does not provide RSSI for CCK */
117 /* (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
119 cck_highpwr = dm_odm->bCckHighPower;
121 cck_agc_rpt = pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
123 /* 2011.11.28 LukeLee: 88E use different LNA & VGA gain table */
124 /* The RSSI formula should be modified according to the gain table */
125 /* In 88E, cck_highpwr is always set to 1 */
126 LNA_idx = (cck_agc_rpt & 0xE0) >> 5;
127 VGA_idx = cck_agc_rpt & 0x1F;
131 rx_pwr_all = -100 + 2*(27-VGA_idx); /* VGA_idx = 27~2 */
136 rx_pwr_all = -48 + 2*(2-VGA_idx); /* VGA_idx = 2~0 */
139 rx_pwr_all = -42 + 2*(7-VGA_idx); /* VGA_idx = 7~5 */
142 rx_pwr_all = -36 + 2*(7-VGA_idx); /* VGA_idx = 7~4 */
145 rx_pwr_all = -24 + 2*(7-VGA_idx); /* VGA_idx = 7~0 */
149 rx_pwr_all = -12 + 2*(5-VGA_idx); /* VGA_idx = 5~0 */
151 rx_pwr_all = -6 + 2*(5-VGA_idx);
154 rx_pwr_all = 8-2*VGA_idx;
157 rx_pwr_all = 14-2*VGA_idx;
163 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
166 PWDB_ALL = ((PWDB_ALL-80)<<1)+((PWDB_ALL-80)>>1)+80;
167 else if ((PWDB_ALL <= 78) && (PWDB_ALL >= 20))
173 pPhyInfo->RxPWDBAll = PWDB_ALL;
174 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL;
175 pPhyInfo->RecvSignalPower = rx_pwr_all;
176 /* (3) Get Signal Quality (EVM) */
177 if (pPktinfo->bPacketMatchBSSID) {
180 if (pPhyInfo->RxPWDBAll > 40 && !dm_odm->bInHctTest) {
183 SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
187 else if (SQ_rpt < 20)
190 SQ = ((64-SQ_rpt) * 100) / 44;
192 pPhyInfo->SignalQuality = SQ;
193 pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = SQ;
194 pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
196 } else { /* is OFDM rate */
197 dm_odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
199 /* (1)Get RSSI for HT rate */
201 for (i = RF_PATH_A; i < RF_PATH_MAX; i++) {
202 /* 2008/01/30 MH we will judge RF RX path now. */
203 if (dm_odm->RFPathRxEnable & BIT(i))
206 rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F)*2) - 110;
208 pPhyInfo->RxPwr[i] = rx_pwr[i];
210 /* Translate DBM to percentage. */
211 RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
214 /* Modification for ext-LNA board */
215 if (dm_odm->BoardType == ODM_BOARD_HIGHPWR) {
216 if ((pPhyStaRpt->path_agc[i].trsw) == 1)
217 RSSI = (RSSI > 94) ? 100 : (RSSI + 6);
219 RSSI = (RSSI <= 16) ? (RSSI >> 3) : (RSSI - 16);
221 if ((RSSI <= 34) && (RSSI >= 4))
225 pPhyInfo->RxMIMOSignalStrength[i] = (u8)RSSI;
227 /* Get Rx snr value in DB */
228 pPhyInfo->RxSNR[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
229 dm_odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
231 /* (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
232 rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1) & 0x7f) - 110;
234 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
235 PWDB_ALL_BT = PWDB_ALL;
237 pPhyInfo->RxPWDBAll = PWDB_ALL;
238 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL_BT;
239 pPhyInfo->RxPower = rx_pwr_all;
240 pPhyInfo->RecvSignalPower = rx_pwr_all;
242 /* (3)EVM of HT rate */
243 if (pPktinfo->Rate >= DESC92C_RATEMCS8 && pPktinfo->Rate <= DESC92C_RATEMCS15)
244 Max_spatial_stream = 2; /* both spatial stream make sense */
246 Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
248 for (i = 0; i < Max_spatial_stream; i++) {
249 /* Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
250 /* fill most significant bit to "zero" when doing shifting operation which may change a negative */
251 /* value to positive one, then the dbm value (which is supposed to be negative) is not correct anymore. */
252 EVM = odm_EVMdbToPercentage((pPhyStaRpt->stream_rxevm[i])); /* dbm */
254 if (pPktinfo->bPacketMatchBSSID) {
255 if (i == RF_PATH_A) /* Fill value in RFD, Get the first spatial stream only */
256 pPhyInfo->SignalQuality = (u8)(EVM & 0xff);
257 pPhyInfo->RxMIMOSignalQuality[i] = (u8)(EVM & 0xff);
261 /* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
262 /* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
264 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, PWDB_ALL));/* PWDB_ALL; */
267 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, total_rssi /= rf_rx_num));
270 /* For 92C/92D HW (Hybrid) Antenna Diversity */
271 pDM_SWAT_Table->antsel = pPhyStaRpt->ant_sel;
272 /* For 88E HW Antenna Diversity */
273 dm_odm->DM_FatTable.antsel_rx_keep_0 = pPhyStaRpt->ant_sel;
274 dm_odm->DM_FatTable.antsel_rx_keep_1 = pPhyStaRpt->ant_sel_b;
275 dm_odm->DM_FatTable.antsel_rx_keep_2 = pPhyStaRpt->antsel_rx_keep_2;
278 static void odm_Process_RSSIForDM(struct odm_dm_struct *dm_odm,
279 struct odm_phy_status_info *pPhyInfo,
280 struct odm_per_pkt_info *pPktinfo)
282 s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
283 s32 UndecoratedSmoothedOFDM, RSSI_Ave;
285 u8 RSSI_max, RSSI_min, i;
288 struct sta_info *pEntry;
290 struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
292 if (pPktinfo->StationID == 0xFF)
294 pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
295 if (!IS_STA_VALID(pEntry))
297 if ((!pPktinfo->bPacketMatchBSSID))
300 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
302 /* Smart Antenna Debug Message------------------ */
304 if (dm_odm->AntDivType == CG_TRX_SMART_ANTDIV) {
305 if (pDM_FatTable->FAT_State == FAT_TRAINING_STATE) {
306 if (pPktinfo->bPacketToSelf) {
307 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
308 (pDM_FatTable->antsel_rx_keep_1<<1) |
309 pDM_FatTable->antsel_rx_keep_0;
310 pDM_FatTable->antSumRSSI[antsel_tr_mux] += pPhyInfo->RxPWDBAll;
311 pDM_FatTable->antRSSIcnt[antsel_tr_mux]++;
314 } else if ((dm_odm->AntDivType == CG_TRX_HW_ANTDIV) || (dm_odm->AntDivType == CGCS_RX_HW_ANTDIV)) {
315 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
316 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
317 (pDM_FatTable->antsel_rx_keep_1<<1) | pDM_FatTable->antsel_rx_keep_0;
318 rtl88eu_dm_ant_sel_statistics(dm_odm, antsel_tr_mux, pPktinfo->StationID, pPhyInfo->RxPWDBAll);
321 /* Smart Antenna Debug Message------------------ */
323 UndecoratedSmoothedCCK = pEntry->rssi_stat.UndecoratedSmoothedCCK;
324 UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
325 UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
327 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
328 if (!isCCKrate) { /* ofdm rate */
329 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_B] == 0) {
330 RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
332 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[RF_PATH_B]) {
333 RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
334 RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
336 RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
337 RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
339 if ((RSSI_max - RSSI_min) < 3)
341 else if ((RSSI_max - RSSI_min) < 6)
342 RSSI_Ave = RSSI_max - 1;
343 else if ((RSSI_max - RSSI_min) < 10)
344 RSSI_Ave = RSSI_max - 2;
346 RSSI_Ave = RSSI_max - 3;
349 /* 1 Process OFDM RSSI */
350 if (UndecoratedSmoothedOFDM <= 0) { /* initialize */
351 UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
353 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
354 UndecoratedSmoothedOFDM =
355 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
356 (RSSI_Ave)) / (Rx_Smooth_Factor);
357 UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
359 UndecoratedSmoothedOFDM =
360 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
361 (RSSI_Ave)) / (Rx_Smooth_Factor);
365 pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT(0);
368 RSSI_Ave = pPhyInfo->RxPWDBAll;
370 /* 1 Process CCK RSSI */
371 if (UndecoratedSmoothedCCK <= 0) { /* initialize */
372 UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
374 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
375 UndecoratedSmoothedCCK =
376 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
377 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
378 UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
380 UndecoratedSmoothedCCK =
381 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
382 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
385 pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
387 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
388 if (pEntry->rssi_stat.ValidBit >= 64)
389 pEntry->rssi_stat.ValidBit = 64;
391 pEntry->rssi_stat.ValidBit++;
393 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
394 OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i) & BIT(0);
396 if (pEntry->rssi_stat.ValidBit == 64) {
397 Weighting = min_t(u32, OFDM_pkt << 4, 64);
398 UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
400 if (pEntry->rssi_stat.ValidBit != 0)
401 UndecoratedSmoothedPWDB = (OFDM_pkt * UndecoratedSmoothedOFDM +
402 (pEntry->rssi_stat.ValidBit-OFDM_pkt) *
403 UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
405 UndecoratedSmoothedPWDB = 0;
407 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
408 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
409 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
413 /* Endianness before calling this API */
414 static void ODM_PhyStatusQuery_92CSeries(struct odm_dm_struct *dm_odm,
415 struct odm_phy_status_info *pPhyInfo,
417 struct odm_per_pkt_info *pPktinfo)
419 odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus,
421 if (dm_odm->RSSI_test) {
422 ;/* Select the packets to do RSSI checking for antenna switching. */
424 odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
428 void ODM_PhyStatusQuery(struct odm_dm_struct *dm_odm,
429 struct odm_phy_status_info *pPhyInfo,
430 u8 *pPhyStatus, struct odm_per_pkt_info *pPktinfo)
432 ODM_PhyStatusQuery_92CSeries(dm_odm, pPhyInfo, pPhyStatus, pPktinfo);