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 void odm_Init_RSSIForDM(struct odm_dm_struct *dm_odm)
282 static void odm_Process_RSSIForDM(struct odm_dm_struct *dm_odm,
283 struct odm_phy_status_info *pPhyInfo,
284 struct odm_per_pkt_info *pPktinfo)
286 s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
287 s32 UndecoratedSmoothedOFDM, RSSI_Ave;
289 u8 RSSI_max, RSSI_min, i;
292 struct sta_info *pEntry;
294 struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
296 if (pPktinfo->StationID == 0xFF)
298 pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
299 if (!IS_STA_VALID(pEntry))
301 if ((!pPktinfo->bPacketMatchBSSID))
304 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
306 /* Smart Antenna Debug Message------------------ */
308 if (dm_odm->AntDivType == CG_TRX_SMART_ANTDIV) {
309 if (pDM_FatTable->FAT_State == FAT_TRAINING_STATE) {
310 if (pPktinfo->bPacketToSelf) {
311 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
312 (pDM_FatTable->antsel_rx_keep_1<<1) |
313 pDM_FatTable->antsel_rx_keep_0;
314 pDM_FatTable->antSumRSSI[antsel_tr_mux] += pPhyInfo->RxPWDBAll;
315 pDM_FatTable->antRSSIcnt[antsel_tr_mux]++;
318 } else if ((dm_odm->AntDivType == CG_TRX_HW_ANTDIV) || (dm_odm->AntDivType == CGCS_RX_HW_ANTDIV)) {
319 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
320 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
321 (pDM_FatTable->antsel_rx_keep_1<<1) | pDM_FatTable->antsel_rx_keep_0;
322 ODM_AntselStatistics_88E(dm_odm, antsel_tr_mux, pPktinfo->StationID, pPhyInfo->RxPWDBAll);
325 /* Smart Antenna Debug Message------------------ */
327 UndecoratedSmoothedCCK = pEntry->rssi_stat.UndecoratedSmoothedCCK;
328 UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
329 UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
331 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
332 if (!isCCKrate) { /* ofdm rate */
333 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_B] == 0) {
334 RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
336 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[RF_PATH_B]) {
337 RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
338 RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
340 RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
341 RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
343 if ((RSSI_max - RSSI_min) < 3)
345 else if ((RSSI_max - RSSI_min) < 6)
346 RSSI_Ave = RSSI_max - 1;
347 else if ((RSSI_max - RSSI_min) < 10)
348 RSSI_Ave = RSSI_max - 2;
350 RSSI_Ave = RSSI_max - 3;
353 /* 1 Process OFDM RSSI */
354 if (UndecoratedSmoothedOFDM <= 0) { /* initialize */
355 UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
357 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
358 UndecoratedSmoothedOFDM =
359 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
360 (RSSI_Ave)) / (Rx_Smooth_Factor);
361 UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
363 UndecoratedSmoothedOFDM =
364 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
365 (RSSI_Ave)) / (Rx_Smooth_Factor);
369 pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT0;
372 RSSI_Ave = pPhyInfo->RxPWDBAll;
374 /* 1 Process CCK RSSI */
375 if (UndecoratedSmoothedCCK <= 0) { /* initialize */
376 UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
378 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
379 UndecoratedSmoothedCCK =
380 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
381 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
382 UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
384 UndecoratedSmoothedCCK =
385 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
386 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
389 pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
391 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
392 if (pEntry->rssi_stat.ValidBit >= 64)
393 pEntry->rssi_stat.ValidBit = 64;
395 pEntry->rssi_stat.ValidBit++;
397 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
398 OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i)&BIT0;
400 if (pEntry->rssi_stat.ValidBit == 64) {
401 Weighting = ((OFDM_pkt<<4) > 64) ? 64 : (OFDM_pkt<<4);
402 UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
404 if (pEntry->rssi_stat.ValidBit != 0)
405 UndecoratedSmoothedPWDB = (OFDM_pkt * UndecoratedSmoothedOFDM +
406 (pEntry->rssi_stat.ValidBit-OFDM_pkt) *
407 UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
409 UndecoratedSmoothedPWDB = 0;
411 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
412 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
413 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
417 /* Endianness before calling this API */
418 static void ODM_PhyStatusQuery_92CSeries(struct odm_dm_struct *dm_odm,
419 struct odm_phy_status_info *pPhyInfo,
421 struct odm_per_pkt_info *pPktinfo)
423 odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus,
425 if (dm_odm->RSSI_test) {
426 ;/* Select the packets to do RSSI checking for antenna switching. */
428 odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
432 void ODM_PhyStatusQuery(struct odm_dm_struct *dm_odm,
433 struct odm_phy_status_info *pPhyInfo,
434 u8 *pPhyStatus, struct odm_per_pkt_info *pPktinfo)
436 ODM_PhyStatusQuery_92CSeries(dm_odm, pPhyInfo, pPhyStatus, pPktinfo);
439 /* For future use. */
440 void ODM_MacStatusQuery(struct odm_dm_struct *dm_odm, u8 *mac_stat,
441 u8 macid, bool pkt_match_bssid,
442 bool pkttoself, bool pkt_beacon)
444 /* 2011/10/19 Driver team will handle in the future. */
447 enum HAL_STATUS ODM_ConfigRFWithHeaderFile(struct odm_dm_struct *dm_odm,
448 enum rf_radio_path content,
449 enum rf_radio_path rfpath)
451 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD, ("===>ODM_ConfigRFWithHeaderFile\n"));
452 if (rfpath == RF_PATH_A)
453 READ_AND_CONFIG(8188E, _RadioA_1T_);
454 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD, (" ===> ODM_ConfigRFWithHeaderFile() Radio_A:Rtl8188ERadioA_1TArray\n"));
455 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD, (" ===> ODM_ConfigRFWithHeaderFile() Radio_B:Rtl8188ERadioB_1TArray\n"));
457 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_TRACE, ("ODM_ConfigRFWithHeaderFile: Radio No %x\n", rfpath));
458 return HAL_STATUS_SUCCESS;
461 enum HAL_STATUS ODM_ConfigBBWithHeaderFile(struct odm_dm_struct *dm_odm,
462 enum odm_bb_config_type config_tp)
464 if (config_tp == CONFIG_BB_PHY_REG) {
465 READ_AND_CONFIG(8188E, _PHY_REG_1T_);
466 } else if (config_tp == CONFIG_BB_AGC_TAB) {
467 READ_AND_CONFIG(8188E, _AGC_TAB_1T_);
468 } else if (config_tp == CONFIG_BB_PHY_REG_PG) {
469 READ_AND_CONFIG(8188E, _PHY_REG_PG_);
470 ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD,
471 (" ===> phy_ConfigBBWithHeaderFile() agc:Rtl8188EPHY_REG_PGArray\n"));
473 return HAL_STATUS_SUCCESS;
476 enum HAL_STATUS ODM_ConfigMACWithHeaderFile(struct odm_dm_struct *dm_odm)
478 u8 result = HAL_STATUS_SUCCESS;
479 result = READ_AND_CONFIG(8188E, _MAC_REG_);