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 ((dm_odm->SupportInterface == ODM_ITRF_USB) ||
47 (dm_odm->SupportInterface == ODM_ITRF_SDIO)) {
48 if (CurrSig >= 51 && CurrSig <= 100)
50 else if (CurrSig >= 41 && CurrSig <= 50)
51 RetSig = 80 + ((CurrSig - 40)*2);
52 else if (CurrSig >= 31 && CurrSig <= 40)
53 RetSig = 66 + (CurrSig - 30);
54 else if (CurrSig >= 21 && CurrSig <= 30)
55 RetSig = 54 + (CurrSig - 20);
56 else if (CurrSig >= 10 && CurrSig <= 20)
57 RetSig = 42 + (((CurrSig - 10) * 2) / 3);
58 else if (CurrSig >= 5 && CurrSig <= 9)
59 RetSig = 22 + (((CurrSig - 5) * 3) / 2);
60 else if (CurrSig >= 1 && CurrSig <= 4)
61 RetSig = 6 + (((CurrSig - 1) * 3) / 2);
68 static s32 odm_SignalScaleMapping(struct odm_dm_struct *dm_odm, s32 CurrSig)
70 return odm_SignalScaleMapping_92CSeries(dm_odm, CurrSig);
73 static u8 odm_EVMdbToPercentage(s8 Value)
75 /* -33dB~0dB to 0%~99% */
85 ret_val = 0 - ret_val;
93 static void odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct *dm_odm,
94 struct odm_phy_status_info *pPhyInfo,
96 struct odm_per_pkt_info *pPktinfo)
98 struct sw_ant_switch *pDM_SWAT_Table = &dm_odm->DM_SWAT_Table;
99 u8 i, Max_spatial_stream;
100 s8 rx_pwr[4], rx_pwr_all = 0;
101 u8 EVM, PWDB_ALL = 0, PWDB_ALL_BT;
102 u8 RSSI, total_rssi = 0;
108 struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
110 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
112 pPhyInfo->RxMIMOSignalQuality[ODM_RF_PATH_A] = -1;
113 pPhyInfo->RxMIMOSignalQuality[ODM_RF_PATH_B] = -1;
118 dm_odm->PhyDbgInfo.NumQryPhyStatusCCK++;
119 /* (1)Hardware does not provide RSSI for CCK */
120 /* (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
122 cck_highpwr = dm_odm->bCckHighPower;
124 cck_agc_rpt = pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a ;
126 /* 2011.11.28 LukeLee: 88E use different LNA & VGA gain table */
127 /* The RSSI formula should be modified according to the gain table */
128 /* In 88E, cck_highpwr is always set to 1 */
129 LNA_idx = ((cck_agc_rpt & 0xE0) >> 5);
130 VGA_idx = (cck_agc_rpt & 0x1F);
134 rx_pwr_all = -100 + 2*(27-VGA_idx); /* VGA_idx = 27~2 */
139 rx_pwr_all = -48 + 2*(2-VGA_idx); /* VGA_idx = 2~0 */
142 rx_pwr_all = -42 + 2*(7-VGA_idx); /* VGA_idx = 7~5 */
145 rx_pwr_all = -36 + 2*(7-VGA_idx); /* VGA_idx = 7~4 */
148 rx_pwr_all = -24 + 2*(7-VGA_idx); /* VGA_idx = 7~0 */
152 rx_pwr_all = -12 + 2*(5-VGA_idx); /* VGA_idx = 5~0 */
154 rx_pwr_all = -6 + 2*(5-VGA_idx);
157 rx_pwr_all = 8-2*VGA_idx;
160 rx_pwr_all = 14-2*VGA_idx;
166 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
169 PWDB_ALL = ((PWDB_ALL-80)<<1)+((PWDB_ALL-80)>>1)+80;
170 else if ((PWDB_ALL <= 78) && (PWDB_ALL >= 20))
176 pPhyInfo->RxPWDBAll = PWDB_ALL;
177 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL;
178 pPhyInfo->RecvSignalPower = rx_pwr_all;
179 /* (3) Get Signal Quality (EVM) */
180 if (pPktinfo->bPacketMatchBSSID) {
183 if (pPhyInfo->RxPWDBAll > 40 && !dm_odm->bInHctTest) {
186 SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
190 else if (SQ_rpt < 20)
193 SQ = ((64-SQ_rpt) * 100) / 44;
195 pPhyInfo->SignalQuality = SQ;
196 pPhyInfo->RxMIMOSignalQuality[ODM_RF_PATH_A] = SQ;
197 pPhyInfo->RxMIMOSignalQuality[ODM_RF_PATH_B] = -1;
199 } else { /* is OFDM rate */
200 dm_odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
202 /* (1)Get RSSI for HT rate */
204 for (i = ODM_RF_PATH_A; i < ODM_RF_PATH_MAX; i++) {
205 /* 2008/01/30 MH we will judge RF RX path now. */
206 if (dm_odm->RFPathRxEnable & BIT(i))
209 rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F)*2) - 110;
211 pPhyInfo->RxPwr[i] = rx_pwr[i];
213 /* Translate DBM to percentage. */
214 RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
217 /* Modification for ext-LNA board */
218 if (dm_odm->BoardType == ODM_BOARD_HIGHPWR) {
219 if ((pPhyStaRpt->path_agc[i].trsw) == 1)
220 RSSI = (RSSI > 94) ? 100 : (RSSI + 6);
222 RSSI = (RSSI <= 16) ? (RSSI >> 3) : (RSSI - 16);
224 if ((RSSI <= 34) && (RSSI >= 4))
228 pPhyInfo->RxMIMOSignalStrength[i] = (u8)RSSI;
230 /* Get Rx snr value in DB */
231 pPhyInfo->RxSNR[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
232 dm_odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
234 /* (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
235 rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1) & 0x7f) - 110;
237 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
238 PWDB_ALL_BT = PWDB_ALL;
240 pPhyInfo->RxPWDBAll = PWDB_ALL;
241 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL_BT;
242 pPhyInfo->RxPower = rx_pwr_all;
243 pPhyInfo->RecvSignalPower = rx_pwr_all;
245 /* (3)EVM of HT rate */
246 if (pPktinfo->Rate >= DESC92C_RATEMCS8 && pPktinfo->Rate <= DESC92C_RATEMCS15)
247 Max_spatial_stream = 2; /* both spatial stream make sense */
249 Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
251 for (i = 0; i < Max_spatial_stream; i++) {
252 /* Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
253 /* fill most significant bit to "zero" when doing shifting operation which may change a negative */
254 /* value to positive one, then the dbm value (which is supposed to be negative) is not correct anymore. */
255 EVM = odm_EVMdbToPercentage((pPhyStaRpt->stream_rxevm[i])); /* dbm */
257 if (pPktinfo->bPacketMatchBSSID) {
258 if (i == ODM_RF_PATH_A) /* Fill value in RFD, Get the first spatial stream only */
259 pPhyInfo->SignalQuality = (u8)(EVM & 0xff);
260 pPhyInfo->RxMIMOSignalQuality[i] = (u8)(EVM & 0xff);
264 /* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
265 /* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
267 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, PWDB_ALL));/* PWDB_ALL; */
270 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, total_rssi /= rf_rx_num));
273 /* For 92C/92D HW (Hybrid) Antenna Diversity */
274 pDM_SWAT_Table->antsel = pPhyStaRpt->ant_sel;
275 /* For 88E HW Antenna Diversity */
276 dm_odm->DM_FatTable.antsel_rx_keep_0 = pPhyStaRpt->ant_sel;
277 dm_odm->DM_FatTable.antsel_rx_keep_1 = pPhyStaRpt->ant_sel_b;
278 dm_odm->DM_FatTable.antsel_rx_keep_2 = pPhyStaRpt->antsel_rx_keep_2;
281 void odm_Init_RSSIForDM(struct odm_dm_struct *dm_odm)
285 static void odm_Process_RSSIForDM(struct odm_dm_struct *dm_odm,
286 struct odm_phy_status_info *pPhyInfo,
287 struct odm_per_pkt_info *pPktinfo)
289 s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
290 s32 UndecoratedSmoothedOFDM, RSSI_Ave;
292 u8 RSSI_max, RSSI_min, i;
295 struct sta_info *pEntry;
297 struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
299 if (pPktinfo->StationID == 0xFF)
301 pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
302 if (!IS_STA_VALID(pEntry))
304 if ((!pPktinfo->bPacketMatchBSSID))
307 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
309 /* Smart Antenna Debug Message------------------ */
311 if (dm_odm->AntDivType == CG_TRX_SMART_ANTDIV) {
312 if (pDM_FatTable->FAT_State == FAT_TRAINING_STATE) {
313 if (pPktinfo->bPacketToSelf) {
314 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
315 (pDM_FatTable->antsel_rx_keep_1<<1) |
316 pDM_FatTable->antsel_rx_keep_0;
317 pDM_FatTable->antSumRSSI[antsel_tr_mux] += pPhyInfo->RxPWDBAll;
318 pDM_FatTable->antRSSIcnt[antsel_tr_mux]++;
321 } else if ((dm_odm->AntDivType == CG_TRX_HW_ANTDIV) || (dm_odm->AntDivType == CGCS_RX_HW_ANTDIV)) {
322 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
323 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
324 (pDM_FatTable->antsel_rx_keep_1<<1) | pDM_FatTable->antsel_rx_keep_0;
325 ODM_AntselStatistics_88E(dm_odm, antsel_tr_mux, pPktinfo->StationID, pPhyInfo->RxPWDBAll);
328 /* Smart Antenna Debug Message------------------ */
330 UndecoratedSmoothedCCK = pEntry->rssi_stat.UndecoratedSmoothedCCK;
331 UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
332 UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
334 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
335 if (!isCCKrate) { /* ofdm rate */
336 if (pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_B] == 0) {
337 RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_A];
339 if (pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_B]) {
340 RSSI_max = pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_A];
341 RSSI_min = pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_B];
343 RSSI_max = pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_B];
344 RSSI_min = pPhyInfo->RxMIMOSignalStrength[ODM_RF_PATH_A];
346 if ((RSSI_max - RSSI_min) < 3)
348 else if ((RSSI_max - RSSI_min) < 6)
349 RSSI_Ave = RSSI_max - 1;
350 else if ((RSSI_max - RSSI_min) < 10)
351 RSSI_Ave = RSSI_max - 2;
353 RSSI_Ave = RSSI_max - 3;
356 /* 1 Process OFDM RSSI */
357 if (UndecoratedSmoothedOFDM <= 0) { /* initialize */
358 UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
360 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
361 UndecoratedSmoothedOFDM =
362 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
363 (RSSI_Ave)) / (Rx_Smooth_Factor);
364 UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
366 UndecoratedSmoothedOFDM =
367 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
368 (RSSI_Ave)) / (Rx_Smooth_Factor);
372 pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT0;
375 RSSI_Ave = pPhyInfo->RxPWDBAll;
377 /* 1 Process CCK RSSI */
378 if (UndecoratedSmoothedCCK <= 0) { /* initialize */
379 UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
381 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
382 UndecoratedSmoothedCCK =
383 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
384 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
385 UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
387 UndecoratedSmoothedCCK =
388 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
389 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
392 pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
394 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
395 if (pEntry->rssi_stat.ValidBit >= 64)
396 pEntry->rssi_stat.ValidBit = 64;
398 pEntry->rssi_stat.ValidBit++;
400 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
401 OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i)&BIT0;
403 if (pEntry->rssi_stat.ValidBit == 64) {
404 Weighting = ((OFDM_pkt<<4) > 64) ? 64 : (OFDM_pkt<<4);
405 UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
407 if (pEntry->rssi_stat.ValidBit != 0)
408 UndecoratedSmoothedPWDB = (OFDM_pkt * UndecoratedSmoothedOFDM +
409 (pEntry->rssi_stat.ValidBit-OFDM_pkt) *
410 UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
412 UndecoratedSmoothedPWDB = 0;
414 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
415 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
416 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
420 /* Endianness before calling this API */
421 static void ODM_PhyStatusQuery_92CSeries(struct odm_dm_struct *dm_odm,
422 struct odm_phy_status_info *pPhyInfo,
424 struct odm_per_pkt_info *pPktinfo)
426 odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus,
428 if (dm_odm->RSSI_test) {
429 ;/* Select the packets to do RSSI checking for antenna switching. */
431 odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
435 void ODM_PhyStatusQuery(struct odm_dm_struct *dm_odm,
436 struct odm_phy_status_info *pPhyInfo,
437 u8 *pPhyStatus, struct odm_per_pkt_info *pPktinfo)
439 ODM_PhyStatusQuery_92CSeries(dm_odm, pPhyInfo, pPhyStatus, pPktinfo);
442 /* For future use. */
443 void ODM_MacStatusQuery(struct odm_dm_struct *dm_odm, u8 *mac_stat,
444 u8 macid, bool pkt_match_bssid,
445 bool pkttoself, bool pkt_beacon)
447 /* 2011/10/19 Driver team will handle in the future. */
450 enum HAL_STATUS ODM_ConfigRFWithHeaderFile(struct odm_dm_struct *dm_odm,
451 enum ODM_RF_RADIO_PATH content,
452 enum ODM_RF_RADIO_PATH rfpath)
454 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD, ("===>ODM_ConfigRFWithHeaderFile\n"));
455 if (rfpath == ODM_RF_PATH_A)
456 READ_AND_CONFIG(8188E, _RadioA_1T_);
457 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD, (" ===> ODM_ConfigRFWithHeaderFile() Radio_A:Rtl8188ERadioA_1TArray\n"));
458 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD, (" ===> ODM_ConfigRFWithHeaderFile() Radio_B:Rtl8188ERadioB_1TArray\n"));
460 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_TRACE, ("ODM_ConfigRFWithHeaderFile: Radio No %x\n", rfpath));
461 return HAL_STATUS_SUCCESS;
464 enum HAL_STATUS ODM_ConfigBBWithHeaderFile(struct odm_dm_struct *dm_odm,
465 enum odm_bb_config_type config_tp)
467 if (config_tp == CONFIG_BB_PHY_REG) {
468 READ_AND_CONFIG(8188E, _PHY_REG_1T_);
469 } else if (config_tp == CONFIG_BB_AGC_TAB) {
470 READ_AND_CONFIG(8188E, _AGC_TAB_1T_);
471 } else if (config_tp == CONFIG_BB_PHY_REG_PG) {
472 READ_AND_CONFIG(8188E, _PHY_REG_PG_);
473 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD,
474 (" ===> phy_ConfigBBWithHeaderFile() agc:Rtl8188EPHY_REG_PGArray\n"));
476 return HAL_STATUS_SUCCESS;
479 enum HAL_STATUS ODM_ConfigMACWithHeaderFile(struct odm_dm_struct *dm_odm)
481 u8 result = HAL_STATUS_SUCCESS;
482 result = READ_AND_CONFIG(8188E, _MAC_REG_);