Staging: rtxxx0: unify AsicEvaluateRxAnt()
[safe/jmp/linux-2.6] / drivers / staging / rt2860 / common / mlme.c
index e3ff743..0a6da1a 100644 (file)
@@ -50,11 +50,7 @@ UCHAR        Ccx2QosInfo[] = {0x00, 0x40, 0x96, 0x04};
 UCHAR   RALINK_OUI[]  = {0x00, 0x0c, 0x43};
 UCHAR   BROADCOM_OUI[]  = {0x00, 0x90, 0x4c};
 UCHAR   WPS_OUI[] = {0x00, 0x50, 0xf2, 0x04};
-#ifdef CONFIG_STA_SUPPORT
-#ifdef DOT11_N_SUPPORT
 UCHAR  PRE_N_HT_OUI[]  = {0x00, 0x90, 0x4c};
-#endif // DOT11_N_SUPPORT //
-#endif // CONFIG_STA_SUPPORT //
 
 UCHAR RateSwitchTable[] = {
 // Item No.   Mode   Curr-MCS   TrainUp   TrainDown            // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
@@ -130,7 +126,6 @@ UCHAR RateSwitchTable11G[] = {
     0x07, 0x10,  7, 10, 13,
 };
 
-#ifdef DOT11_N_SUPPORT
 UCHAR RateSwitchTable11N1S[] = {
 // Item No.   Mode   Curr-MCS   TrainUp   TrainDown            // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
     0x09, 0x00,  0,  0,  0,                                            // Initial used item after association
@@ -287,7 +282,6 @@ UCHAR RateSwitchTable11BGN3SForABand[] = { // 3*3
     0x0a, 0x20, 23,  8, 25,
     0x0b, 0x22, 23,  8, 25,
 };
-#endif // DOT11_N_SUPPORT //
 
 PUCHAR ReasonString[] = {
        /* 0  */         "Reserved",
@@ -334,14 +328,9 @@ USHORT RateIdTo500Kbps[] = { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 144,
 UCHAR  SsidIe   = IE_SSID;
 UCHAR  SupRateIe = IE_SUPP_RATES;
 UCHAR  ExtRateIe = IE_EXT_SUPP_RATES;
-#ifdef DOT11_N_SUPPORT
 UCHAR  HtCapIe = IE_HT_CAP;
 UCHAR  AddHtInfoIe = IE_ADD_HT;
 UCHAR  NewExtChanIe = IE_SECONDARY_CH_OFFSET;
-#ifdef DOT11N_DRAFT3
-UCHAR  ExtHtCapIe = IE_EXT_CAPABILITY;
-#endif // DOT11N_DRAFT3 //
-#endif // DOT11_N_SUPPORT //
 UCHAR  ErpIe    = IE_ERP;
 UCHAR  DsIe     = IE_DS_PARM;
 UCHAR  TimIe    = IE_TIM;
@@ -349,6 +338,9 @@ UCHAR  WpaIe         = IE_WPA;
 UCHAR  Wpa2Ie   = IE_WPA2;
 UCHAR  IbssIe   = IE_IBSS_PARM;
 UCHAR  Ccx2Ie   = IE_CCX_V2;
+#ifdef RT2870
+UCHAR  WapiIe   = IE_WAPI;
+#endif
 
 extern UCHAR   WPA_OUI[];
 
@@ -457,7 +449,7 @@ FREQUENCY_ITEM FreqItems3020[] =
        {13,   247,      2,  2},
        {14,   248,      2,  4},
 };
-#define        NUM_OF_3020_CHNL        (sizeof(FreqItems3020) / sizeof(FREQUENCY_ITEM))
+UCHAR  NUM_OF_3020_CHNL=(sizeof(FreqItems3020) / sizeof(FREQUENCY_ITEM));
 
 /*
        ==========================================================================
@@ -488,8 +480,6 @@ NDIS_STATUS MlmeInit(
                pAd->Mlme.bRunning = FALSE;
                NdisAllocateSpinLock(&pAd->Mlme.TaskLock);
 
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
                        BssTableInit(&pAd->ScanTab);
 
@@ -501,18 +491,10 @@ NDIS_STATUS MlmeInit(
                        WpaPskStateMachineInit(pAd, &pAd->Mlme.WpaPskMachine, pAd->Mlme.WpaPskFunc);
                        AironetStateMachineInit(pAd, &pAd->Mlme.AironetMachine, pAd->Mlme.AironetFunc);
 
-#ifdef QOS_DLS_SUPPORT
-                       DlsStateMachineInit(pAd, &pAd->Mlme.DlsMachine, pAd->Mlme.DlsFunc);
-#endif // QOS_DLS_SUPPORT //
-
-
                        // Since we are using switch/case to implement it, the init is different from the above
                        // state machine init
                        MlmeCntlInit(pAd, &pAd->Mlme.CntlMachine, NULL);
                }
-#endif // CONFIG_STA_SUPPORT //
-
-
 
                ActionStateMachineInit(pAd, &pAd->Mlme.ActMachine, pAd->Mlme.ActFunc);
 
@@ -525,9 +507,7 @@ NDIS_STATUS MlmeInit(
                // software-based RX Antenna diversity
                RTMPInitTimer(pAd, &pAd->Mlme.RxAntEvalTimer, GET_TIMER_FUNCTION(AsicRxAntEvalTimeout), pAd, FALSE);
 
-
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+#ifdef RT2860
                {
                if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
                {
@@ -536,8 +516,7 @@ NDIS_STATUS MlmeInit(
                        RTMPInitTimer(pAd, &pAd->Mlme.RadioOnOffTimer, GET_TIMER_FUNCTION(RadioOnExec), pAd, FALSE);
                }
                }
-#endif // CONFIG_STA_SUPPORT //
-
+#endif
        } while (FALSE);
 
        DBGPRINT(RT_DEBUG_TRACE, ("<-- MLME Initialize\n"));
@@ -592,12 +571,21 @@ VOID MlmeHandler(
                //From message type, determine which state machine I should drive
                if (MlmeDequeue(&pAd->Mlme.Queue, &Elem))
                {
+#ifdef RT2870
+                       if (Elem->MsgType == MT2_RESET_CONF)
+                       {
+                               DBGPRINT_RAW(RT_DEBUG_TRACE, ("!!! reset MLME state machine !!!\n"));
+                               MlmeRestartStateMachine(pAd);
+                               Elem->Occupied = FALSE;
+                               Elem->MsgLen = 0;
+                               continue;
+                       }
+#endif // RT2870 //
 
                        // if dequeue success
                        switch (Elem->Machine)
                        {
                                // STA state machines
-#ifdef CONFIG_STA_SUPPORT
                                case ASSOC_STATE_MACHINE:
                                        StateMachinePerformAction(pAd, &pAd->Mlme.AssocMachine, Elem);
                                        break;
@@ -616,22 +604,9 @@ VOID MlmeHandler(
                                case WPA_PSK_STATE_MACHINE:
                                        StateMachinePerformAction(pAd, &pAd->Mlme.WpaPskMachine, Elem);
                                        break;
-#ifdef LEAP_SUPPORT
-                               case LEAP_STATE_MACHINE:
-                                       LeapMachinePerformAction(pAd, &pAd->Mlme.LeapMachine, Elem);
-                                       break;
-#endif
                                case AIRONET_STATE_MACHINE:
                                        StateMachinePerformAction(pAd, &pAd->Mlme.AironetMachine, Elem);
                                        break;
-
-#ifdef QOS_DLS_SUPPORT
-                               case DLS_STATE_MACHINE:
-                                       StateMachinePerformAction(pAd, &pAd->Mlme.DlsMachine, Elem);
-                                       break;
-#endif // QOS_DLS_SUPPORT //
-#endif // CONFIG_STA_SUPPORT //
-
                                case ACTION_STATE_MACHINE:
                                        StateMachinePerformAction(pAd, &pAd->Mlme.ActMachine, Elem);
                                        break;
@@ -676,6 +651,9 @@ VOID MlmeHalt(
        IN PRTMP_ADAPTER pAd)
 {
        BOOLEAN           Cancelled;
+#ifdef RT3070
+       UINT32          TxPinCfg = 0x00050F0F;
+#endif // RT3070 //
 
        DBGPRINT(RT_DEBUG_TRACE, ("==> MlmeHalt\n"));
 
@@ -685,12 +663,7 @@ VOID MlmeHalt(
                AsicDisableSync(pAd);
        }
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
-#ifdef QOS_DLS_SUPPORT
-               UCHAR           i;
-#endif // QOS_DLS_SUPPORT //
                // Cancel pending timers
                RTMPCancelTimer(&pAd->MlmeAux.AssocTimer,               &Cancelled);
                RTMPCancelTimer(&pAd->MlmeAux.ReassocTimer,             &Cancelled);
@@ -698,20 +671,14 @@ VOID MlmeHalt(
                RTMPCancelTimer(&pAd->MlmeAux.AuthTimer,                &Cancelled);
                RTMPCancelTimer(&pAd->MlmeAux.BeaconTimer,              &Cancelled);
                RTMPCancelTimer(&pAd->MlmeAux.ScanTimer,                &Cancelled);
+#ifdef RT2860
            if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
            {
                    RTMPCancelTimer(&pAd->Mlme.PsPollTimer,             &Cancelled);
                    RTMPCancelTimer(&pAd->Mlme.RadioOnOffTimer,         &Cancelled);
                }
-
-#ifdef QOS_DLS_SUPPORT
-               for (i=0; i<MAX_NUM_OF_DLS_ENTRY; i++)
-               {
-                       RTMPCancelTimer(&pAd->StaCfg.DLSEntry[i].Timer, &Cancelled);
-               }
-#endif // QOS_DLS_SUPPORT //
+#endif
        }
-#endif // CONFIG_STA_SUPPORT //
 
        RTMPCancelTimer(&pAd->Mlme.PeriodicTimer,               &Cancelled);
        RTMPCancelTimer(&pAd->Mlme.RxAntEvalTimer,              &Cancelled);
@@ -723,6 +690,27 @@ VOID MlmeHalt(
                // Set LED
                RTMPSetLED(pAd, LED_HALT);
         RTMPSetSignalLED(pAd, -100);   // Force signal strength Led to be turned off, firmware is not done it.
+#ifdef RT2870
+        {
+            LED_CFG_STRUC LedCfg;
+            RTMP_IO_READ32(pAd, LED_CFG, &LedCfg.word);
+            LedCfg.field.LedPolar = 0;
+            LedCfg.field.RLedMode = 0;
+            LedCfg.field.GLedMode = 0;
+            LedCfg.field.YLedMode = 0;
+            RTMP_IO_WRITE32(pAd, LED_CFG, LedCfg.word);
+        }
+#endif // RT2870 //
+#ifdef RT3070
+               //
+               // Turn off LNA_PE
+               //
+               if (IS_RT3070(pAd) || IS_RT3071(pAd))
+               {
+                       TxPinCfg &= 0xFFFFF0F0;
+                       RTUSBWriteMACRegister(pAd, TX_PIN_CFG, TxPinCfg);
+               }
+#endif // RT3070 //
        }
 
        RTMPusecDelay(5000);    //  5 msec to gurantee Ant Diversity timer canceled
@@ -793,6 +781,7 @@ VOID MlmePeriodicExec(
        ULONG                   TxTotalCnt;
        PRTMP_ADAPTER   pAd = (RTMP_ADAPTER *)FunctionContext;
 
+#ifdef RT2860
        //Baron 2008/07/10
        //printk("Baron_Test:\t%s", RTMPGetRalinkEncryModeStr(pAd->StaCfg.WepStatus));
        //If the STA security setting is OPEN or WEP, pAd->StaCfg.WpaSupplicantUP = 0.
@@ -806,8 +795,6 @@ VOID MlmePeriodicExec(
                pAd->StaCfg.WpaSupplicantUP = 1;
        }
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
            // If Hardware controlled Radio enabled, we have to check GPIO pin2 every 2 second.
                // Move code to here, because following code will return when radio is off
@@ -847,7 +834,7 @@ VOID MlmePeriodicExec(
                        }
                }
        }
-#endif // CONFIG_STA_SUPPORT //
+#endif /* RT2860 */
 
        // Do nothing if the driver is starting halt state.
        // This might happen when timer already been fired before cancel timer with mlmehalt
@@ -857,7 +844,7 @@ VOID MlmePeriodicExec(
                                                                fRTMP_ADAPTER_RESET_IN_PROGRESS))))
                return;
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+#ifdef RT2860
        {
                if ((pAd->RalinkCounters.LastReceivedByteCount == pAd->RalinkCounters.ReceivedByteCount) && (pAd->StaCfg.bRadio == TRUE))
                {
@@ -895,11 +882,9 @@ VOID MlmePeriodicExec(
                        AsicResetFromDMABusy(pAd);
                }
        }
-
+#endif /* RT2860 */
        RT28XX_MLME_PRE_SANITY_CHECK(pAd);
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // Do nothing if monitor mode is on
                if (MONITOR_ON(pAd))
@@ -924,26 +909,26 @@ VOID MlmePeriodicExec(
                                }
                }
        }
-#endif // CONFIG_STA_SUPPORT //
 
        pAd->bUpdateBcnCntDone = FALSE;
 
 //     RECBATimerTimeout(SystemSpecific1,FunctionContext,SystemSpecific2,SystemSpecific3);
        pAd->Mlme.PeriodicRound ++;
 
+#ifdef RT3070
+       // execute every 100ms, update the Tx FIFO Cnt for update Tx Rate.
+       NICUpdateFifoStaCounters(pAd);
+#endif // RT3070 //
        // execute every 500ms
        if ((pAd->Mlme.PeriodicRound % 5 == 0) && RTMPAutoRateSwitchCheck(pAd)/*(OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_TX_RATE_SWITCH_ENABLED))*/)
        {
-#ifdef CONFIG_STA_SUPPORT
                // perform dynamic tx rate switching based on past TX history
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
                        if ((OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
                                        )
                                && (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)))
                                MlmeDynamicTxRateSwitching(pAd);
                }
-#endif // CONFIG_STA_SUPPORT //
        }
 
        // Normal 1 second Mlme PeriodicExec.
@@ -982,12 +967,12 @@ VOID MlmePeriodicExec(
                // the dynamic tuning mechanism below are based on most up-to-date information
                NICUpdateRawCounters(pAd);
 
+#ifdef RT2870
+               RT2870_WatchDog(pAd);
+#endif // RT2870 //
 
-#ifdef DOT11_N_SUPPORT
                // Need statistics after read counter. So put after NICUpdateRawCounters
                ORIBATimerTimeout(pAd);
-#endif // DOT11_N_SUPPORT //
-
 
                // The time period for checking antenna is according to traffic
                if (pAd->Mlme.bEnableAutoAntennaCheck)
@@ -996,6 +981,7 @@ VOID MlmePeriodicExec(
                                                         pAd->RalinkCounters.OneSecTxRetryOkCount +
                                                         pAd->RalinkCounters.OneSecTxFailCount;
 
+                       // dynamic adjust antenna evaluation period according to the traffic
                        if (TxTotalCnt > 50)
                        {
                                if (pAd->Mlme.OneSecPeriodicRound % 10 == 0)
@@ -1012,17 +998,14 @@ VOID MlmePeriodicExec(
                        }
                }
 
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       STAMlmePeriodicExec(pAd);
-#endif // CONFIG_STA_SUPPORT //
+               STAMlmePeriodicExec(pAd);
 
                MlmeResetRalinkCounters(pAd);
 
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
+#ifdef RT2860
                        if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST) && (pAd->bPCIclkOff == FALSE))
+#endif
                        {
                                // When Adhoc beacon is enabled and RTS/CTS is enabled, there is a chance that hardware MAC FSM will run into a deadlock
                                // and sending CTS-to-self over and over.
@@ -1044,30 +1027,32 @@ VOID MlmePeriodicExec(
                                }
                        }
                }
-#endif // CONFIG_STA_SUPPORT //
 
                RT28XX_MLME_HANDLER(pAd);
        }
 
-
        pAd->bUpdateBcnCntDone = FALSE;
 }
 
-#ifdef CONFIG_STA_SUPPORT
 VOID STAMlmePeriodicExec(
        PRTMP_ADAPTER pAd)
 {
+#ifdef RT2860
        ULONG                       TxTotalCnt;
+#endif
+#ifdef RT2870
+       ULONG   TxTotalCnt;
+       int     i;
+#endif
 
-#ifdef WPA_SUPPLICANT_SUPPORT
     if (pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_DISABLE)
-#endif // WPA_SUPPLICANT_SUPPORT //
     {
        // WPA MIC error should block association attempt for 60 seconds
        if (pAd->StaCfg.bBlockAssoc && (pAd->StaCfg.LastMicErrorTime + (60 * OS_HZ) < pAd->Mlme.Now32))
                pAd->StaCfg.bBlockAssoc = FALSE;
     }
 
+#ifdef RT2860
        //Baron 2008/07/10
        //printk("Baron_Test:\t%s", RTMPGetRalinkEncryModeStr(pAd->StaCfg.WepStatus));
        //If the STA security setting is OPEN or WEP, pAd->StaCfg.WpaSupplicantUP = 0.
@@ -1080,6 +1065,7 @@ VOID STAMlmePeriodicExec(
        {
                pAd->StaCfg.WpaSupplicantUP = 1;
        }
+#endif
 
     if ((pAd->PreMediaState != pAd->IndicateMediaState) && (pAd->CommonCfg.bWirelessEvent))
        {
@@ -1090,6 +1076,7 @@ VOID STAMlmePeriodicExec(
                pAd->PreMediaState = pAd->IndicateMediaState;
        }
 
+#ifdef RT2860
        if ((pAd->OpMode == OPMODE_STA) && (IDLE_ON(pAd)) &&
         (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE)) &&
                (pAd->Mlme.SyncMachine.CurrState == SYNC_IDLE) &&
@@ -1099,6 +1086,7 @@ VOID STAMlmePeriodicExec(
        {
                RT28xxPciAsicRadioOff(pAd, GUI_IDLE_POWER_SAVE, 0);
        }
+#endif
 
 
 
@@ -1121,11 +1109,6 @@ VOID STAMlmePeriodicExec(
 
        if (INFRA_ON(pAd))
        {
-#ifdef QOS_DLS_SUPPORT
-               // Check DLS time out, then tear down those session
-               RTMPCheckDLSTimeOut(pAd);
-#endif // QOS_DLS_SUPPORT //
-
                // Is PSM bit consistent with user power management policy?
                // This is the only place that will set PSM bit ON.
                if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE))
@@ -1170,26 +1153,11 @@ VOID STAMlmePeriodicExec(
                        // Lost AP, send disconnect & link down event
                        LinkDown(pAd, FALSE);
 
-#ifdef WPA_SUPPLICANT_SUPPORT
-#ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
-            if (pAd->StaCfg.WpaSupplicantUP)
-                       {
-                union iwreq_data    wrqu;
-                //send disassociate event to wpa_supplicant
-                memset(&wrqu, 0, sizeof(wrqu));
-                wrqu.data.flags = RT_DISASSOC_EVENT_FLAG;
-                wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
-            }
-#endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
-#endif // WPA_SUPPLICANT_SUPPORT //
-
-#ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
             {
                 union iwreq_data    wrqu;
                 memset(wrqu.ap_addr.sa_data, 0, MAC_ADDR_LEN);
                 wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
             }
-#endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
 
                        MlmeAutoReconnectLastSSID(pAd);
                }
@@ -1215,6 +1183,7 @@ VOID STAMlmePeriodicExec(
        }
        else if (ADHOC_ON(pAd))
        {
+#ifdef RT2860
                // 2003-04-17 john. this is a patch that driver forces a BEACON out if ASIC fails
                // the "TX BEACON competition" for the entire past 1 sec.
                // So that even when ASIC's BEACONgen engine been blocked
@@ -1239,7 +1208,6 @@ VOID STAMlmePeriodicExec(
                        pAd->StaCfg.AdhocBOnlyJoined = FALSE;
                }
 
-#ifdef DOT11_N_SUPPORT
                if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
                {
                        if ((pAd->StaCfg.AdhocBGJoined) &&
@@ -1256,7 +1224,7 @@ VOID STAMlmePeriodicExec(
                                pAd->StaCfg.Adhoc20NJoined = FALSE;
                        }
                }
-#endif // DOT11_N_SUPPORT //
+#endif /* RT2860 */
 
                //radar detect
                if ((pAd->CommonCfg.Channel > 14)
@@ -1281,6 +1249,19 @@ VOID STAMlmePeriodicExec(
                        MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_START_REQ, sizeof(MLME_START_REQ_STRUCT), &StartReq);
                        pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_START;
                }
+
+#ifdef RT2870
+               for (i = 1; i < MAX_LEN_OF_MAC_TABLE; i++)
+               {
+                       MAC_TABLE_ENTRY *pEntry = &pAd->MacTab.Content[i];
+
+                       if (pEntry->ValidAsCLI == FALSE)
+                               continue;
+
+                       if (pEntry->LastBeaconRxTime + ADHOC_BEACON_LOST_TIME < pAd->Mlme.Now32)
+                               MacTableDeleteEntry(pAd, pEntry->Aid, pEntry->Addr);
+               }
+#endif
        }
        else // no INFRA nor ADHOC connection
        {
@@ -1320,14 +1301,6 @@ VOID STAMlmePeriodicExec(
                                }
                                else
                                {
-#ifdef CARRIER_DETECTION_SUPPORT // Roger sync Carrier
-                                       if (pAd->CommonCfg.CarrierDetect.Enable == TRUE)
-                                       {
-                                               if ((pAd->Mlme.OneSecPeriodicRound % 5) == 1)
-                                                       MlmeAutoReconnectLastSSID(pAd);
-                                       }
-                                       else
-#endif // CARRIER_DETECTION_SUPPORT //
                                                MlmeAutoReconnectLastSSID(pAd);
                                }
                        }
@@ -1336,7 +1309,6 @@ VOID STAMlmePeriodicExec(
 
 SKIP_AUTO_SCAN_CONN:
 
-#ifdef DOT11_N_SUPPORT
     if ((pAd->MacTab.Content[BSSID_WCID].TXBAbitmap !=0) && (pAd->MacTab.fAnyBASession == FALSE))
        {
                pAd->MacTab.fAnyBASession = TRUE;
@@ -1347,15 +1319,6 @@ SKIP_AUTO_SCAN_CONN:
                pAd->MacTab.fAnyBASession = FALSE;
                AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode,  ALLN_SETPROTECT, FALSE, FALSE);
        }
-#endif // DOT11_N_SUPPORT //
-
-
-#ifdef DOT11_N_SUPPORT
-#ifdef DOT11N_DRAFT3
-       if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_SCAN_2040))
-               TriEventCounterMaintenance(pAd);
-#endif // DOT11N_DRAFT3 //
-#endif // DOT11_N_SUPPORT //
 
        return;
 }
@@ -1415,7 +1378,6 @@ VOID MlmeAutoReconnectLastSSID(
                RT28XX_MLME_HANDLER(pAd);
        }
 }
-#endif // CONFIG_STA_SUPPORT //
 
 /*
        ==========================================================================
@@ -1464,15 +1426,19 @@ VOID MlmeSelectTxRateTable(
                        break;
                }
 
-#ifdef CONFIG_STA_SUPPORT
                if ((pAd->OpMode == OPMODE_STA) && ADHOC_ON(pAd))
                {
-#ifdef DOT11_N_SUPPORT
                        if ((pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED) &&
+#ifdef RT2860
                                !pAd->StaCfg.AdhocBOnlyJoined &&
                                !pAd->StaCfg.AdhocBGJoined &&
                                (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0xff) &&
                                ((pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0x00) || (pAd->Antenna.field.TxPath == 1)))
+#endif
+#ifdef RT2870
+                               (pEntry->HTCapability.MCSSet[0] == 0xff) &&
+                               ((pEntry->HTCapability.MCSSet[1] == 0x00) || (pAd->Antenna.field.TxPath == 1)))
+#endif
                        {// 11N 1S Adhoc
                                *ppTable = RateSwitchTable11N1S;
                                *pTableSize = RateSwitchTable11N1S[0];
@@ -1480,10 +1446,16 @@ VOID MlmeSelectTxRateTable(
 
                        }
                        else if ((pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED) &&
+#ifdef RT2860
                                        !pAd->StaCfg.AdhocBOnlyJoined &&
                                        !pAd->StaCfg.AdhocBGJoined &&
                                        (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0xff) &&
                                        (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0xff) &&
+#endif
+#ifdef RT2870
+                                       (pEntry->HTCapability.MCSSet[0] == 0xff) &&
+                                       (pEntry->HTCapability.MCSSet[1] == 0xff) &&
+#endif
                                        (pAd->Antenna.field.TxPath == 2))
                        {// 11N 2S Adhoc
                                if (pAd->LatchRfRegs.Channel <= 14)
@@ -1501,7 +1473,7 @@ VOID MlmeSelectTxRateTable(
 
                        }
                        else
-#endif // DOT11_N_SUPPORT //
+#ifdef RT2860
                                if (pAd->CommonCfg.PhyMode == PHY_11B)
                        {
                                *ppTable = RateSwitchTable11B;
@@ -1510,6 +1482,12 @@ VOID MlmeSelectTxRateTable(
 
                        }
                else if((pAd->LatchRfRegs.Channel <= 14) && (pAd->StaCfg.AdhocBOnlyJoined == TRUE))
+#endif
+#ifdef RT2870
+                               if ((pEntry->RateLen == 4)
+                                       && (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
+                                       )
+#endif
                        {
                                // USe B Table when Only b-only Station in my IBSS .
                                *ppTable = RateSwitchTable11B;
@@ -1533,9 +1511,7 @@ VOID MlmeSelectTxRateTable(
                        }
                        break;
                }
-#endif // CONFIG_STA_SUPPORT //
 
-#ifdef DOT11_N_SUPPORT
                if ((pEntry->RateLen == 12) && (pEntry->HTCapability.MCSSet[0] == 0xff) &&
                        ((pEntry->HTCapability.MCSSet[1] == 0x00) || (pAd->CommonCfg.TxStream == 1)))
                {// 11BGN 1S AP
@@ -1592,12 +1568,13 @@ VOID MlmeSelectTxRateTable(
 
                        break;
                }
-#endif // DOT11_N_SUPPORT //
+
                //else if ((pAd->StaActive.SupRateLen == 4) && (pAd->StaActive.ExtRateLen == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
                if ((pEntry->RateLen == 4)
-#ifdef DOT11_N_SUPPORT
+#ifndef RT30xx
+//Iverson mark for Adhoc b mode,sta will use rate 54  Mbps when connect with sta b/g/n mode
                        && (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
-#endif // DOT11_N_SUPPORT //
+#endif
                        )
                {// B only AP
                        *ppTable = RateSwitchTable11B;
@@ -1609,9 +1586,7 @@ VOID MlmeSelectTxRateTable(
 
                //else if ((pAd->StaActive.SupRateLen + pAd->StaActive.ExtRateLen > 8) && (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
                if ((pEntry->RateLen > 8)
-#ifdef DOT11_N_SUPPORT
                        && (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
-#endif // DOT11_N_SUPPORT //
                        )
                {// B/G  mixed AP
                        *ppTable = RateSwitchTable11BG;
@@ -1623,9 +1598,7 @@ VOID MlmeSelectTxRateTable(
 
                //else if ((pAd->StaActive.SupRateLen + pAd->StaActive.ExtRateLen == 8) && (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
                if ((pEntry->RateLen == 8)
-#ifdef DOT11_N_SUPPORT
                        && (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
-#endif // DOT11_N_SUPPORT //
                        )
                {// G only AP
                        *ppTable = RateSwitchTable11G;
@@ -1634,16 +1607,10 @@ VOID MlmeSelectTxRateTable(
 
                        break;
                }
-#ifdef DOT11_N_SUPPORT
-#endif // DOT11_N_SUPPORT //
 
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
-#ifdef DOT11_N_SUPPORT
                        //else if ((pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
                        if ((pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0))
-#endif // DOT11_N_SUPPORT //
                        {       // Legacy mode
                                if (pAd->CommonCfg.MaxTxRate <= RATE_11)
                                {
@@ -1666,7 +1633,7 @@ VOID MlmeSelectTxRateTable(
                                }
                                break;
                        }
-#ifdef DOT11_N_SUPPORT
+
                        if (pAd->LatchRfRegs.Channel <= 14)
                        {
                                if (pAd->CommonCfg.TxStream == 1)
@@ -1701,15 +1668,13 @@ VOID MlmeSelectTxRateTable(
                                        DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode,default use 11N 2S AP \n"));
                                }
                        }
-#endif // DOT11_N_SUPPORT //
+
                        DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode (SupRateLen=%d, ExtRateLen=%d, MCSSet[0]=0x%x, MCSSet[1]=0x%x)\n",
                                pAd->StaActive.SupRateLen, pAd->StaActive.ExtRateLen, pAd->StaActive.SupportedPhyInfo.MCSSet[0], pAd->StaActive.SupportedPhyInfo.MCSSet[1]));
                }
-#endif // CONFIG_STA_SUPPORT //
        } while(FALSE);
 }
 
-#ifdef CONFIG_STA_SUPPORT
 /*
        ==========================================================================
        Description:
@@ -1863,14 +1828,6 @@ VOID MlmeCalculateChannelQuality(
        CHAR  MaxRssi;
        ULONG BeaconLostTime = BEACON_LOST_TIME;
 
-#ifdef CARRIER_DETECTION_SUPPORT // Roger sync Carrier
-       // longer beacon lost time when carrier detection enabled
-       if (pAd->CommonCfg.CarrierDetect.Enable == TRUE)
-       {
-               BeaconLostTime = BEACON_LOST_TIME + BEACON_LOST_TIME/2;
-       }
-#endif // CARRIER_DETECTION_SUPPORT //
-
        MaxRssi = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
 
        //
@@ -1935,13 +1892,11 @@ VOID MlmeSetTxRate(
 {
        UCHAR   MaxMode = MODE_OFDM;
 
-#ifdef DOT11_N_SUPPORT
        MaxMode = MODE_HTGREENFIELD;
 
        if (pTxRate->STBC && (pAd->StaCfg.MaxHTPhyMode.field.STBC) && (pAd->Antenna.field.TxPath == 2))
                pAd->StaCfg.HTPhyMode.field.STBC = STBC_USE;
        else
-#endif // DOT11_N_SUPPORT //
                pAd->StaCfg.HTPhyMode.field.STBC = STBC_NONE;
 
        if (pTxRate->CurrMCS < MCS_AUTO)
@@ -1971,14 +1926,11 @@ VOID MlmeSetTxRate(
                if (pTxRate->Mode <= MaxMode)
                        pAd->StaCfg.HTPhyMode.field.MODE = pTxRate->Mode;
 
-#ifdef DOT11_N_SUPPORT
                if (pTxRate->ShortGI && (pAd->StaCfg.MaxHTPhyMode.field.ShortGI))
                        pAd->StaCfg.HTPhyMode.field.ShortGI = GI_400;
                else
-#endif // DOT11_N_SUPPORT //
                        pAd->StaCfg.HTPhyMode.field.ShortGI = GI_800;
 
-#ifdef DOT11_N_SUPPORT
                // Reexam each bandwidth's SGI support.
                if (pAd->StaCfg.HTPhyMode.field.ShortGI == GI_400)
                {
@@ -2022,17 +1974,15 @@ VOID MlmeSetTxRate(
                {
                        AsicUpdateProtect(pAd, HT_RTSCTS_6M, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
                }
-#endif // DOT11_N_SUPPORT //
 
                pEntry->HTPhyMode.field.STBC    = pAd->StaCfg.HTPhyMode.field.STBC;
                pEntry->HTPhyMode.field.ShortGI = pAd->StaCfg.HTPhyMode.field.ShortGI;
                pEntry->HTPhyMode.field.MCS             = pAd->StaCfg.HTPhyMode.field.MCS;
                pEntry->HTPhyMode.field.MODE    = pAd->StaCfg.HTPhyMode.field.MODE;
-#ifdef DOT11_N_SUPPORT
+
                if ((pAd->StaCfg.MaxHTPhyMode.field.MODE == MODE_HTGREENFIELD) &&
                    pAd->WIFItestbed.bGreenField)
                    pEntry->HTPhyMode.field.MODE = MODE_HTGREENFIELD;
-#endif // DOT11_N_SUPPORT //
        }
 
        pAd->LastTxRate = (USHORT)(pEntry->HTPhyMode.word);
@@ -2072,11 +2022,6 @@ VOID MlmeDynamicTxRateSwitching(
        ULONG                                   TxRetransmit = 0, TxSuccess = 0, TxFailCount = 0;
        MAC_TABLE_ENTRY                 *pEntry;
 
-       /*if (pAd->Antenna.field.RxPath > 1)
-               Rssi = (pAd->StaCfg.RssiSample.AvgRssi0 + pAd->StaCfg.RssiSample.AvgRssi1) >> 1;
-       else
-               Rssi = pAd->StaCfg.RssiSample.AvgRssi0;*/
-
        //
        // walk through MAC table, see if need to change AP's TX rate toward each entry
        //
@@ -2090,7 +2035,15 @@ VOID MlmeDynamicTxRateSwitching(
 
                if ((pAd->MacTab.Size == 1) || (pEntry->ValidAsDls))
                {
+#ifdef RT2860
                        Rssi = RTMPMaxRssi(pAd, (CHAR)pAd->StaCfg.RssiSample.AvgRssi0, (CHAR)pAd->StaCfg.RssiSample.AvgRssi1, (CHAR)pAd->StaCfg.RssiSample.AvgRssi2);
+#endif
+#ifdef RT2870
+                       Rssi = RTMPMaxRssi(pAd,
+                                                          pAd->StaCfg.RssiSample.AvgRssi0,
+                                                          pAd->StaCfg.RssiSample.AvgRssi1,
+                                                          pAd->StaCfg.RssiSample.AvgRssi2);
+#endif
 
                        // Update statistic counter
                        RTMP_IO_READ32(pAd, TX_STA_CNT0, &TxStaCnt0.word);
@@ -2120,7 +2073,21 @@ VOID MlmeDynamicTxRateSwitching(
                }
                else
                {
+#ifdef RT2860
                        Rssi = RTMPMaxRssi(pAd, (CHAR)pEntry->RssiSample.AvgRssi0, (CHAR)pEntry->RssiSample.AvgRssi1, (CHAR)pEntry->RssiSample.AvgRssi2);
+#endif
+#ifdef RT2870
+                       if (INFRA_ON(pAd) && (i == 1))
+                               Rssi = RTMPMaxRssi(pAd,
+                                                                  pAd->StaCfg.RssiSample.AvgRssi0,
+                                                                  pAd->StaCfg.RssiSample.AvgRssi1,
+                                                                  pAd->StaCfg.RssiSample.AvgRssi2);
+                       else
+                               Rssi = RTMPMaxRssi(pAd,
+                                                                  pEntry->RssiSample.AvgRssi0,
+                                                                  pEntry->RssiSample.AvgRssi1,
+                                                                  pEntry->RssiSample.AvgRssi2);
+#endif
 
                        TxTotalCnt = pEntry->OneSecTxNoRetryOkCount +
                                 pEntry->OneSecTxRetryOkCount +
@@ -2177,14 +2144,12 @@ VOID MlmeDynamicTxRateSwitching(
 
                pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(CurrRateIdx+1)*5];
 
-#ifdef DOT11_N_SUPPORT
                if ((Rssi > -65) && (pCurrTxRate->Mode >= MODE_HTMIX))
                {
                        TrainUp         = (pCurrTxRate->TrainUp + (pCurrTxRate->TrainUp >> 1));
                        TrainDown       = (pCurrTxRate->TrainDown + (pCurrTxRate->TrainDown >> 1));
                }
                else
-#endif // DOT11_N_SUPPORT //
                {
                        TrainUp         = pCurrTxRate->TrainUp;
                        TrainDown       = pCurrTxRate->TrainDown;
@@ -2262,7 +2227,6 @@ VOID MlmeDynamicTxRateSwitching(
                                {
                                        MCS14 = idx;
                                }
-                               //else if ((pCurrTxRate->CurrMCS == MCS_15)/* && (pCurrTxRate->ShortGI == GI_800)*/)    //we hope to use ShortGI as initial rate
                                else if ((pCurrTxRate->CurrMCS == MCS_15) && (pCurrTxRate->ShortGI == GI_800))  //we hope to use ShortGI as initial rate, however Atheros's chip has bugs when short GI
                                {
                                        MCS15 = idx;
@@ -2308,7 +2272,7 @@ VOID MlmeDynamicTxRateSwitching(
                                        RssiOffset = 8;
                                }
                        }
-#ifdef DOT11_N_SUPPORT
+
                        /*if (MCS15)*/
                        if ((pTable == RateSwitchTable11BGN3S) ||
                                (pTable == RateSwitchTable11N3S) ||
@@ -2374,7 +2338,6 @@ VOID MlmeDynamicTxRateSwitching(
                                        TxRateIdx = MCS0;
                        }
                        else
-#endif // DOT11_N_SUPPORT //
                        {// Legacy mode
                                if (MCS7 && (Rssi > -70))
                                        TxRateIdx = MCS7;
@@ -2548,7 +2511,12 @@ VOID StaQuickResponeForRateUpExec(
        UCHAR                                   UpRateIdx = 0, DownRateIdx = 0, CurrRateIdx = 0;
        ULONG                                   TxTotalCnt;
        ULONG                                   TxErrorRatio = 0;
+#ifdef RT2860
        BOOLEAN                                 bTxRateChanged = TRUE; //, bUpgradeQuality = FALSE;
+#endif
+#ifdef RT2870
+       BOOLEAN                                 bTxRateChanged; //, bUpgradeQuality = FALSE;
+#endif
        PRTMP_TX_RATE_SWITCH    pCurrTxRate, pNextTxRate = NULL;
        PUCHAR                                  pTable;
        UCHAR                                   TableSize = 0;
@@ -2573,11 +2541,25 @@ VOID StaQuickResponeForRateUpExec(
                if (RTMPCheckEntryEnableAutoRateSwitch(pAd, pEntry) == FALSE)
                        continue;
 
+#ifdef RT2860
                //Rssi = RTMPMaxRssi(pAd, (CHAR)pAd->StaCfg.AvgRssi0, (CHAR)pAd->StaCfg.AvgRssi1, (CHAR)pAd->StaCfg.AvgRssi2);
            if (pAd->Antenna.field.TxPath > 1)
                        Rssi = (pAd->StaCfg.RssiSample.AvgRssi0 + pAd->StaCfg.RssiSample.AvgRssi1) >> 1;
                else
                        Rssi = pAd->StaCfg.RssiSample.AvgRssi0;
+#endif
+#ifdef RT2870
+               if (INFRA_ON(pAd) && (i == 1))
+                       Rssi = RTMPMaxRssi(pAd,
+                                                          pAd->StaCfg.RssiSample.AvgRssi0,
+                                                          pAd->StaCfg.RssiSample.AvgRssi1,
+                                                          pAd->StaCfg.RssiSample.AvgRssi2);
+               else
+                       Rssi = RTMPMaxRssi(pAd,
+                                                          pEntry->RssiSample.AvgRssi0,
+                                                          pEntry->RssiSample.AvgRssi1,
+                                                          pEntry->RssiSample.AvgRssi2);
+#endif
 
                CurrRateIdx = pAd->CommonCfg.TxRateIndex;
 
@@ -2602,14 +2584,12 @@ VOID StaQuickResponeForRateUpExec(
 
                pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(CurrRateIdx+1)*5];
 
-#ifdef DOT11_N_SUPPORT
                if ((Rssi > -65) && (pCurrTxRate->Mode >= MODE_HTMIX))
                {
                        TrainUp         = (pCurrTxRate->TrainUp + (pCurrTxRate->TrainUp >> 1));
                        TrainDown       = (pCurrTxRate->TrainDown + (pCurrTxRate->TrainDown >> 1));
                }
                else
-#endif // DOT11_N_SUPPORT //
                {
                        TrainUp         = pCurrTxRate->TrainUp;
                        TrainDown       = pCurrTxRate->TrainDown;
@@ -2719,6 +2699,9 @@ VOID StaQuickResponeForRateUpExec(
                        pAd->DrsCounters.TxRateUpPenalty = 0;
                        NdisZeroMemory(pAd->DrsCounters.TxQuality, sizeof(USHORT) * MAX_STEP_OF_TX_RATE_SWITCH);
                        NdisZeroMemory(pAd->DrsCounters.PER, sizeof(UCHAR) * MAX_STEP_OF_TX_RATE_SWITCH);
+#ifdef RT2870
+                       bTxRateChanged = TRUE;
+#endif
                }
                // if rate-down happen, only clear DownRate's bad history
                else if (pAd->CommonCfg.TxRateIndex < CurrRateIdx)
@@ -2728,6 +2711,9 @@ VOID StaQuickResponeForRateUpExec(
                        pAd->DrsCounters.TxRateUpPenalty = 0;           // no penalty
                        pAd->DrsCounters.TxQuality[pAd->CommonCfg.TxRateIndex] = 0;
                        pAd->DrsCounters.PER[pAd->CommonCfg.TxRateIndex] = 0;
+#ifdef RT2870
+                       bTxRateChanged = TRUE;
+#endif
                }
                else
                {
@@ -2783,18 +2769,34 @@ VOID MlmeCheckPsmChange(
        if (INFRA_ON(pAd) &&
                (PowerMode != Ndis802_11PowerModeCAM) &&
                (pAd->StaCfg.Psm == PWR_ACTIVE) &&
+#ifdef RT2860
                RTMP_TEST_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP))
+#else
+               (pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE))
+#endif
        {
-               NdisGetSystemUpTime(&pAd->Mlme.LastSendNULLpsmTime);
-               pAd->RalinkCounters.RxCountSinceLastNULL = 0;
-               MlmeSetPsmBit(pAd, PWR_SAVE);
-               if (!(pAd->CommonCfg.bAPSDCapable && pAd->CommonCfg.APEdcaParm.bAPSDCapable))
-               {
-                       RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, FALSE);
-               }
-               else
+#ifdef RT30xx
+               // add by johnli, use Rx OK data count per second to calculate throughput
+               // If Ttraffic is too high ( > 400 Rx per second), don't go to sleep mode. If tx rate is low, use low criteria
+               // Mode=CCK/MCS=3 => 11 Mbps, Mode=OFDM/MCS=3 => 18 Mbps
+               if (((pAd->StaCfg.HTPhyMode.field.MCS <= 3) &&
+                               (pAd->RalinkCounters.OneSecRxOkDataCnt < (ULONG)100)) ||
+                       ((pAd->StaCfg.HTPhyMode.field.MCS > 3) &&
+                       (pAd->RalinkCounters.OneSecRxOkDataCnt < (ULONG)400)))
+#endif
                {
-                       RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, TRUE);
+                               // Get this time
+                       NdisGetSystemUpTime(&pAd->Mlme.LastSendNULLpsmTime);
+                       pAd->RalinkCounters.RxCountSinceLastNULL = 0;
+                       MlmeSetPsmBit(pAd, PWR_SAVE);
+                       if (!(pAd->CommonCfg.bAPSDCapable && pAd->CommonCfg.APEdcaParm.bAPSDCapable))
+                       {
+                               RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, FALSE);
+                       }
+                       else
+                       {
+                               RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, TRUE);
+                       }
                }
        }
 }
@@ -2811,10 +2813,10 @@ VOID MlmeSetPsmBit(
        RTMP_IO_READ32(pAd, AUTO_RSP_CFG, &csr4.word);
        csr4.field.AckCtsPsmBit = (psm == PWR_SAVE)? 1:0;
        RTMP_IO_WRITE32(pAd, AUTO_RSP_CFG, csr4.word);
+#ifndef RT30xx
        DBGPRINT(RT_DEBUG_TRACE, ("MlmeSetPsmBit = %d\n", psm));
+#endif
 }
-#endif // CONFIG_STA_SUPPORT //
-
 
 // IRQL = DISPATCH_LEVEL
 VOID MlmeSetTxPreamble(
@@ -2953,9 +2955,6 @@ VOID MlmeUpdateTxRates(
 
 //===========================================================================
 //===========================================================================
-
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                pHtPhy          = &pAd->StaCfg.HTPhyMode;
                pMaxHtPhy       = &pAd->StaCfg.MaxHTPhyMode;
@@ -2971,7 +2970,6 @@ VOID MlmeUpdateTxRates(
                        MaxDesire = RATE_11;
                }
        }
-#endif // CONFIG_STA_SUPPORT //
 
        pAd->CommonCfg.MaxDesiredRate = MaxDesire;
        pMinHtPhy->word = 0;
@@ -3000,7 +2998,6 @@ VOID MlmeUpdateTxRates(
        }
 #endif
 
-#ifdef CONFIG_STA_SUPPORT
        if ((ADHOC_ON(pAd) || INFRA_ON(pAd)) && (pAd->OpMode == OPMODE_STA))
        {
                pSupRate = &pAd->StaActive.SupRate[0];
@@ -3009,7 +3006,6 @@ VOID MlmeUpdateTxRates(
                ExtRateLen = pAd->StaActive.ExtRateLen;
        }
        else
-#endif // CONFIG_STA_SUPPORT //
        {
                pSupRate = &pAd->CommonCfg.SupRate[0];
                pExtRate = &pAd->CommonCfg.ExtRate[0];
@@ -3086,10 +3082,9 @@ VOID MlmeUpdateTxRates(
        if (*auto_rate_cur_p)
        {
                short dbm = 0;
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       dbm = pAd->StaCfg.RssiSample.AvgRssi0 - pAd->BbpRssiToDbmDelta;
-#endif // CONFIG_STA_SUPPORT //
+
+               dbm = pAd->StaCfg.RssiSample.AvgRssi0 - pAd->BbpRssiToDbmDelta;
+
                if (bLinkUp == TRUE)
                        pAd->CommonCfg.TxRate = RATE_24;
                else
@@ -3147,9 +3142,7 @@ VOID MlmeUpdateTxRates(
                {
                        case PHY_11BG_MIXED:
                        case PHY_11B:
-#ifdef DOT11_N_SUPPORT
                        case PHY_11BGN_MIXED:
-#endif // DOT11_N_SUPPORT //
                                pAd->CommonCfg.MlmeRate = RATE_1;
                                pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_CCK;
                                pAd->CommonCfg.MlmeTransmit.field.MCS = RATE_1;
@@ -3157,22 +3150,18 @@ VOID MlmeUpdateTxRates(
                                break;
                        case PHY_11G:
                        case PHY_11A:
-#ifdef DOT11_N_SUPPORT
                        case PHY_11AGN_MIXED:
                        case PHY_11GN_MIXED:
                        case PHY_11N_2_4G:
                        case PHY_11AN_MIXED:
                        case PHY_11N_5G:
-#endif // DOT11_N_SUPPORT //
                                pAd->CommonCfg.MlmeRate = RATE_6;
                                pAd->CommonCfg.RtsRate = RATE_6;
                                pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
                                pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
                                break;
                        case PHY_11ABG_MIXED:
-#ifdef DOT11_N_SUPPORT
                        case PHY_11ABGN_MIXED:
-#endif // DOT11_N_SUPPORT //
                                if (pAd->CommonCfg.Channel <= 14)
                                {
                                        pAd->CommonCfg.MlmeRate = RATE_1;
@@ -3215,7 +3204,6 @@ VOID MlmeUpdateTxRates(
                         pAd->CommonCfg.MlmeTransmit.word, pAd->MacTab.Content[BSSID_WCID].MinHTPhyMode.word ,pAd->MacTab.Content[BSSID_WCID].MaxHTPhyMode.word ,pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word ));
 }
 
-#ifdef DOT11_N_SUPPORT
 /*
        ==========================================================================
        Description:
@@ -3248,8 +3236,6 @@ VOID MlmeUpdateHtTxRates(
 
        auto_rate_cur_p = NULL;
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                pDesireHtPhy    = &pAd->StaCfg.DesiredHtPhyInfo;
                pActiveHtPhy    = &pAd->StaCfg.DesiredHtPhyInfo;
@@ -3259,9 +3245,7 @@ VOID MlmeUpdateHtTxRates(
 
                auto_rate_cur_p = &pAd->StaCfg.bAutoTxRateSwitch;
        }
-#endif // CONFIG_STA_SUPPORT //
 
-#ifdef CONFIG_STA_SUPPORT
        if ((ADHOC_ON(pAd) || INFRA_ON(pAd)) && (pAd->OpMode == OPMODE_STA))
        {
                if (pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE)
@@ -3277,7 +3261,6 @@ VOID MlmeUpdateHtTxRates(
                        pMaxHtPhy->field.STBC = STBC_NONE;
        }
        else
-#endif // CONFIG_STA_SUPPORT //
        {
                if (pDesireHtPhy->bHtEnable == FALSE)
                        return;
@@ -3328,7 +3311,6 @@ VOID MlmeUpdateHtTxRates(
        pMinHtPhy->field.STBC = 0;
        pMinHtPhy->field.ShortGI = 0;
        //If STA assigns fixed rate. update to fixed here.
-#ifdef CONFIG_STA_SUPPORT
        if ( (pAd->OpMode == OPMODE_STA) && (pDesireHtPhy->MCSSet[0] != 0xff))
        {
                if (pDesireHtPhy->MCSSet[4] != 0)
@@ -3352,8 +3334,6 @@ VOID MlmeUpdateHtTxRates(
                                break;
                }
        }
-#endif // CONFIG_STA_SUPPORT //
-
 
        // Decide ht rate
        pHtPhy->field.STBC = pMaxHtPhy->field.STBC;
@@ -3373,7 +3353,6 @@ VOID MlmeUpdateHtTxRates(
                pHtPhy->field.BW, pHtPhy->field.ShortGI, pHtPhy->field.MODE));
        DBGPRINT(RT_DEBUG_TRACE,("MlmeUpdateHtTxRates<=== \n"));
 }
-#endif // DOT11_N_SUPPORT //
 
 // IRQL = DISPATCH_LEVEL
 VOID MlmeRadioOff(
@@ -3418,7 +3397,6 @@ VOID BssTableInit(
        }
 }
 
-#ifdef DOT11_N_SUPPORT
 VOID BATableInit(
        IN PRTMP_ADAPTER pAd,
     IN BA_TABLE *Tab)
@@ -3438,7 +3416,6 @@ VOID BATableInit(
                Tab->BAOriEntry[i].ORI_BA_Status = Originator_NONE;
        }
 }
-#endif // DOT11_N_SUPPORT //
 
 /*! \brief search the BSS table by SSID
  *     \param p_tab pointer to the bss table
@@ -3548,7 +3525,6 @@ VOID BssTableDeleteEntry(
        }
 }
 
-#ifdef DOT11_N_SUPPORT
 /*
        ========================================================================
        Routine Description:
@@ -3580,7 +3556,6 @@ VOID BATableDeleteORIEntry(
                NdisReleaseSpinLock(&pAd->BATabLock);
        }
 }
-#endif // DOT11_N_SUPPORT //
 
 /*! \brief
  *     \param
@@ -3697,7 +3672,7 @@ VOID BssEntrySet(
 
        pBss->AddHtInfoLen = 0;
        pBss->HtCapabilityLen = 0;
-#ifdef DOT11_N_SUPPORT
+
        if (HtCapabilityLen> 0)
        {
                pBss->HtCapabilityLen = HtCapabilityLen;
@@ -3717,7 +3692,6 @@ VOID BssEntrySet(
                                }
                }
        }
-#endif // DOT11_N_SUPPORT //
 
        BssCipherParse(pBss);
 
@@ -3735,8 +3709,6 @@ VOID BssEntrySet(
        else
                pBss->QbssLoad.bValid = FALSE;
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                PEID_STRUCT     pEid;
                USHORT          Length = 0;
@@ -3744,11 +3716,9 @@ VOID BssEntrySet(
 
                NdisZeroMemory(&pBss->WpaIE.IE[0], MAX_CUSTOM_LEN);
                NdisZeroMemory(&pBss->RsnIE.IE[0], MAX_CUSTOM_LEN);
-#ifdef EXT_BUILD_CHANNEL_LIST
-               NdisZeroMemory(&pBss->CountryString[0], 3);
-               pBss->bHasCountryIE = FALSE;
-#endif // EXT_BUILD_CHANNEL_LIST //
+
                pEid = (PEID_STRUCT) pVIE;
+
                while ((Length + 2 + (USHORT)pEid->Len) <= LengthVIE)
                {
                        switch(pEid->Eid)
@@ -3777,18 +3747,11 @@ VOID BssEntrySet(
                                                NdisMoveMemory(pBss->RsnIE.IE, pEid, pBss->RsnIE.IELen);
                        }
                                break;
-#ifdef EXT_BUILD_CHANNEL_LIST
-                               case IE_COUNTRY:
-                                       NdisMoveMemory(&pBss->CountryString[0], pEid->Octet, 3);
-                                       pBss->bHasCountryIE = TRUE;
-                                       break;
-#endif // EXT_BUILD_CHANNEL_LIST //
             }
                        Length = Length + 2 + (USHORT)pEid->Len;  // Eid[1] + Len[1]+ content[Len]
                        pEid = (PEID_STRUCT)((UCHAR*)pEid + 2 + pEid->Len);
                }
        }
-#endif // CONFIG_STA_SUPPORT //
 }
 
 /*!
@@ -3882,119 +3845,22 @@ ULONG BssTableSetEntry(
        }
        else
        {
+#ifdef RT30xx
+               /* avoid  Hidden SSID form beacon to overwirite correct SSID from probe response */
+               if ((SSID_EQUAL(Ssid, SsidLen, Tab->BssEntry[Idx].Ssid, Tab->BssEntry[Idx].SsidLen)) ||
+                       (NdisEqualMemory(Tab->BssEntry[Idx].Ssid, ZeroSsid, Tab->BssEntry[Idx].SsidLen)))
+               {
+#endif
                BssEntrySet(pAd, &Tab->BssEntry[Idx], pBssid, Ssid, SsidLen, BssType, BeaconPeriod,CfParm, AtimWin,
                                        CapabilityInfo, SupRate, SupRateLen, ExtRate, ExtRateLen,pHtCapability, pAddHtInfo,HtCapabilityLen, AddHtInfoLen,
                                        NewExtChanOffset, ChannelNo, Rssi, TimeStamp, CkipFlag, pEdcaParm, pQosCapability, pQbssLoad, LengthVIE, pVIE);
-       }
-
-       return Idx;
-}
-
-#ifdef CONFIG_STA_SUPPORT
-#ifdef DOT11_N_SUPPORT
-#ifdef DOT11N_DRAFT3
-VOID  TriEventInit(
-       IN      PRTMP_ADAPTER   pAd)
-{
-       UCHAR           i;
-
-       for (i = 0;i < MAX_TRIGGER_EVENT;i++)
-               pAd->CommonCfg.TriggerEventTab.EventA[i].bValid = FALSE;
-
-       pAd->CommonCfg.TriggerEventTab.EventANo = 0;
-       pAd->CommonCfg.TriggerEventTab.EventBCountDown = 0;
-}
-
-ULONG TriEventTableSetEntry(
-       IN      PRTMP_ADAPTER   pAd,
-       OUT TRIGGER_EVENT_TAB *Tab,
-       IN PUCHAR pBssid,
-       IN HT_CAPABILITY_IE *pHtCapability,
-       IN UCHAR                        HtCapabilityLen,
-       IN UCHAR                        RegClass,
-       IN UCHAR ChannelNo)
-{
-       // Event A
-       if (HtCapabilityLen == 0)
-       {
-               if (Tab->EventANo < MAX_TRIGGER_EVENT)
-               {
-                       RTMPMoveMemory(Tab->EventA[Tab->EventANo].BSSID, pBssid, 6);
-                       Tab->EventA[Tab->EventANo].bValid = TRUE;
-                       Tab->EventA[Tab->EventANo].Channel = ChannelNo;
-                       Tab->EventA[Tab->EventANo].CDCounter = pAd->CommonCfg.Dot11BssWidthChanTranDelay;
-                       if (RegClass != 0)
-                       {
-                               // Beacon has Regulatory class IE. So use beacon's
-                               Tab->EventA[Tab->EventANo].RegClass = RegClass;
-                       }
-                       else
-                       {
-                               // Use Station's Regulatory class instead.
-                               if (pAd->StaActive.SupportedHtPhy.bHtEnable == TRUE)
-                               {
-                                       if (pAd->CommonCfg.CentralChannel > pAd->CommonCfg.Channel)
-                                       {
-                                               Tab->EventA[Tab->EventANo].RegClass = 32;
-                                       }
-                                       else if (pAd->CommonCfg.CentralChannel < pAd->CommonCfg.Channel)
-                                               Tab->EventA[Tab->EventANo].RegClass = 33;
-                               }
-                               else
-                                       Tab->EventA[Tab->EventANo].RegClass = ??;
-
-                       }
-
-                       Tab->EventANo ++;
-               }
-       }
-       else if (pHtCapability->HtCapInfo.Intolerant40)
-       {
-               Tab->EventBCountDown = pAd->CommonCfg.Dot11BssWidthChanTranDelay;
-       }
-
-}
-
-/*
-       ========================================================================
-       Routine Description:
-               Trigger Event table Maintainence called once every second.
-
-       Arguments:
-       // IRQL = DISPATCH_LEVEL
-       ========================================================================
-*/
-VOID TriEventCounterMaintenance(
-       IN      PRTMP_ADAPTER   pAd)
-{
-       UCHAR           i;
-       BOOLEAN                 bNotify = FALSE;
-       for (i = 0;i < MAX_TRIGGER_EVENT;i++)
-       {
-               if (pAd->CommonCfg.TriggerEventTab.EventA[i].bValid && (pAd->CommonCfg.TriggerEventTab.EventA[i].CDCounter > 0))
-               {
-                       pAd->CommonCfg.TriggerEventTab.EventA[i].CDCounter--;
-                       if (pAd->CommonCfg.TriggerEventTab.EventA[i].CDCounter == 0)
-                       {
-                               pAd->CommonCfg.TriggerEventTab.EventA[i].bValid = FALSE;
-                               pAd->CommonCfg.TriggerEventTab.EventANo --;
-                               // Need to send 20/40 Coexistence Notify frame if has status change.
-                               bNotify = TRUE;
-                       }
+#ifdef RT30xx
                }
-       }
-       if (pAd->CommonCfg.TriggerEventTab.EventBCountDown > 0)
-       {
-               pAd->CommonCfg.TriggerEventTab.EventBCountDown--;
-               if (pAd->CommonCfg.TriggerEventTab.EventBCountDown == 0)
-                       bNotify = TRUE;
+#endif
        }
 
-       if (bNotify == TRUE)
-               Update2040CoexistFrameAndNotify(pAd, BSSID_WCID, TRUE);
+       return Idx;
 }
-#endif // DOT11N_DRAFT3 //
-#endif // DOT11_N_SUPPORT //
 
 // IRQL = DISPATCH_LEVEL
 VOID BssTableSsidSort(
@@ -4014,9 +3880,6 @@ VOID BssTableSsidSort(
                if (((pAd->CommonCfg.bIEEE80211H == 1) &&
             (pAd->MlmeAux.Channel > 14) &&
              RadarChannelCheck(pAd, pInBss->Channel))
-#ifdef CARRIER_DETECTION_SUPPORT // Roger sync Carrier
-             || (pAd->CommonCfg.CarrierDetect.Enable == TRUE)
-#endif // CARRIER_DETECTION_SUPPORT //
             )
                {
                        if (pInBss->Hidden)
@@ -4028,18 +3891,6 @@ VOID BssTableSsidSort(
                {
                        BSS_ENTRY *pOutBss = &OutTab->BssEntry[OutTab->BssNr];
 
-
-#ifdef EXT_BUILD_CHANNEL_LIST
-                       // If no Country IE exists no Connection will be established when IEEE80211dClientMode is strict.
-                       if ((pAd->StaCfg.IEEE80211dClientMode == Rt802_11_D_Strict) &&
-                               (pInBss->bHasCountryIE == FALSE))
-                       {
-                               DBGPRINT(RT_DEBUG_TRACE,("StaCfg.IEEE80211dClientMode == Rt802_11_D_Strict, but this AP doesn't have country IE.\n"));
-                               continue;
-                       }
-#endif // EXT_BUILD_CHANNEL_LIST //
-
-#ifdef DOT11_N_SUPPORT
                        // 2.4G/5G N only mode
                        if ((pInBss->HtCapabilityLen == 0) &&
                                ((pAd->CommonCfg.PhyMode == PHY_11N_2_4G) || (pAd->CommonCfg.PhyMode == PHY_11N_5G)))
@@ -4047,7 +3898,6 @@ VOID BssTableSsidSort(
                                DBGPRINT(RT_DEBUG_TRACE,("STA is in N-only Mode, this AP don't have Ht capability in Beacon.\n"));
                                continue;
                        }
-#endif // DOT11_N_SUPPORT //
 
                        // New for WPA2
                        // Check the Authmode first
@@ -4067,9 +3917,12 @@ VOID BssTableSsidSort(
                                                        continue;
 
                                        // check group cipher
-                                       if ((pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher) &&
-                                               (pInBss->WPA.GroupCipher != Ndis802_11GroupWEP40Enabled) &&
-                                               (pInBss->WPA.GroupCipher != Ndis802_11GroupWEP104Enabled))
+                                       if (
+#ifndef RT30xx
+                                           pInBss->WPA.GroupCipher != Ndis802_11GroupWEP40Enabled &&
+                                           pInBss->WPA.GroupCipher != Ndis802_11GroupWEP104Enabled &&
+#endif
+                                           pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher)
                                                continue;
 
                                        // check pairwise cipher, skip if none matched
@@ -4088,9 +3941,12 @@ VOID BssTableSsidSort(
                                                        continue;
 
                                        // check group cipher
-                                       if ((pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher) &&
-                                               (pInBss->WPA2.GroupCipher != Ndis802_11GroupWEP40Enabled) &&
-                                               (pInBss->WPA2.GroupCipher != Ndis802_11GroupWEP104Enabled))
+                                       if (
+#ifndef RT30xx
+                                           pInBss->WPA2.GroupCipher != Ndis802_11GroupWEP40Enabled &&
+                                           pInBss->WPA2.GroupCipher != Ndis802_11GroupWEP104Enabled &&
+#endif
+                                           pAd->StaCfg.WepStatus < pInBss->WPA2.GroupCipher)
                                                continue;
 
                                        // check pairwise cipher, skip if none matched
@@ -4120,7 +3976,6 @@ VOID BssTableSsidSort(
                        if (SsidLen == 0)
                                continue;
 
-#ifdef DOT11_N_SUPPORT
                        // If both station and AP use 40MHz, still need to check if the 40MHZ band's legality in my country region
                        // If this 40MHz wideband is not allowed in my country list, use bandwidth 20MHZ instead,
                        if ((pInBss->CentralChannel != pInBss->Channel) &&
@@ -4140,7 +3995,6 @@ VOID BssTableSsidSort(
                                        }
                                }
                        }
-#endif // DOT11_N_SUPPORT //
 
                        // copy matching BSS from InTab to OutTab
                        NdisMoveMemory(pOutBss, pInBss, sizeof(BSS_ENTRY));
@@ -4151,8 +4005,6 @@ VOID BssTableSsidSort(
                {
                        BSS_ENTRY *pOutBss = &OutTab->BssEntry[OutTab->BssNr];
 
-
-#ifdef DOT11_N_SUPPORT
                        // 2.4G/5G N only mode
                        if ((pInBss->HtCapabilityLen == 0) &&
                                ((pAd->CommonCfg.PhyMode == PHY_11N_2_4G) || (pAd->CommonCfg.PhyMode == PHY_11N_5G)))
@@ -4160,7 +4012,6 @@ VOID BssTableSsidSort(
                                DBGPRINT(RT_DEBUG_TRACE,("STA is in N-only Mode, this AP don't have Ht capability in Beacon.\n"));
                                continue;
                        }
-#endif // DOT11_N_SUPPORT //
 
                        // New for WPA2
                        // Check the Authmode first
@@ -4216,7 +4067,6 @@ VOID BssTableSsidSort(
                        else if (pAd->StaCfg.WepStatus != pInBss->WepStatus)
                                        continue;
 
-#ifdef DOT11_N_SUPPORT
                        // If both station and AP use 40MHz, still need to check if the 40MHZ band's legality in my country region
                        // If this 40MHz wideband is not allowed in my country list, use bandwidth 20MHZ instead,
                        if ((pInBss->CentralChannel != pInBss->Channel) &&
@@ -4229,7 +4079,6 @@ VOID BssTableSsidSort(
                                        pAd->CommonCfg.RegTransmitSetting.field.BW = BW_40;
                                }
                        }
-#endif // DOT11_N_SUPPORT //
 
                        // copy matching BSS from InTab to OutTab
                        NdisMoveMemory(pOutBss, pInBss, sizeof(BSS_ENTRY));
@@ -4265,8 +4114,6 @@ VOID BssTableSortByRssi(
                }
        }
 }
-#endif // CONFIG_STA_SUPPORT //
-
 
 VOID BssCipherParse(
        IN OUT  PBSS_ENTRY      pBss)
@@ -4377,10 +4224,16 @@ VOID BssCipherParse(
                                switch (*pTmp)
                                {
                                        case 1:
+#ifndef RT30xx
                                                pBss->WPA.GroupCipher = Ndis802_11GroupWEP40Enabled;
                                                break;
                                        case 5:
                                                pBss->WPA.GroupCipher = Ndis802_11GroupWEP104Enabled;
+#endif
+#ifdef RT30xx
+                                       case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
+                                               pBss->WPA.GroupCipher = Ndis802_11Encryption1Enabled;
+#endif
                                                break;
                                        case 2:
                                                pBss->WPA.GroupCipher = Ndis802_11Encryption2Enabled;
@@ -4467,7 +4320,6 @@ VOID BssCipherParse(
                                        pBss->AuthMode    = Ndis802_11AuthModeWPANone;
                                        pBss->AuthModeAux = Ndis802_11AuthModeWPANone;
                                        pBss->WepStatus   = pBss->WPA.GroupCipher;
-                                       // Patched bugs for old driver
                                        if (pBss->WPA.PairCipherAux == Ndis802_11WEPDisabled)
                                                pBss->WPA.PairCipherAux = pBss->WPA.GroupCipher;
                                }
@@ -4497,10 +4349,16 @@ VOID BssCipherParse(
                                switch (pCipher->Type)
                                {
                                        case 1:
+#ifndef RT30xx
                                                pBss->WPA2.GroupCipher = Ndis802_11GroupWEP40Enabled;
                                                break;
                                        case 5:
                                                pBss->WPA2.GroupCipher = Ndis802_11GroupWEP104Enabled;
+#endif
+#ifdef RT30xx
+                                       case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
+                                               pBss->WPA2.GroupCipher = Ndis802_11Encryption1Enabled;
+#endif
                                                break;
                                        case 2:
                                                pBss->WPA2.GroupCipher = Ndis802_11Encryption2Enabled;
@@ -4594,7 +4452,6 @@ VOID BssCipherParse(
                                        pBss->WPA.PairCipherAux = pBss->WPA2.PairCipherAux;
                                        pBss->WPA.GroupCipher   = pBss->WPA2.GroupCipher;
                                        pBss->WepStatus                 = pBss->WPA.GroupCipher;
-                                       // Patched bugs for old driver
                                        if (pBss->WPA.PairCipherAux == Ndis802_11WEPDisabled)
                                                pBss->WPA.PairCipherAux = pBss->WPA.GroupCipher;
                                }
@@ -4670,10 +4527,9 @@ VOID MgtMacHeaderInit(
        pHdr80211->FC.SubType = SubType;
        pHdr80211->FC.ToDs = ToDs;
        COPY_MAC_ADDR(pHdr80211->Addr1, pDA);
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-               COPY_MAC_ADDR(pHdr80211->Addr2, pAd->CurrentAddress);
-#endif // CONFIG_STA_SUPPORT //
+
+       COPY_MAC_ADDR(pHdr80211->Addr2, pAd->CurrentAddress);
+
        COPY_MAC_ADDR(pHdr80211->Addr3, pBssid);
 }
 
@@ -4880,8 +4736,6 @@ BOOLEAN MlmeEnqueueForRecv(
                return FALSE;
        }
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                if (!MsgTypeSubst(pAd, pFrame, &Machine, &MsgType))
                {
@@ -4889,7 +4743,6 @@ BOOLEAN MlmeEnqueueForRecv(
                        return FALSE;
                }
        }
-#endif // CONFIG_STA_SUPPORT //
 
        // OK, we got all the informations, it is time to put things into queue
        NdisAcquireSpinLock(&(Queue->Lock));
@@ -4957,13 +4810,14 @@ BOOLEAN MlmeDequeue(
 VOID   MlmeRestartStateMachine(
        IN      PRTMP_ADAPTER   pAd)
 {
+#ifdef RT2860
        MLME_QUEUE_ELEM         *Elem = NULL;
-#ifdef CONFIG_STA_SUPPORT
+#endif
        BOOLEAN                         Cancelled;
-#endif // CONFIG_STA_SUPPORT //
 
        DBGPRINT(RT_DEBUG_TRACE, ("MlmeRestartStateMachine \n"));
 
+#ifdef RT2860
        NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
        if(pAd->Mlme.bRunning)
        {
@@ -4991,13 +4845,9 @@ VOID     MlmeRestartStateMachine(
                        DBGPRINT_ERR(("MlmeRestartStateMachine: MlmeQueue empty\n"));
                }
        }
+#endif /* RT2860 */
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
-#ifdef QOS_DLS_SUPPORT
-               UCHAR i;
-#endif // QOS_DLS_SUPPORT //
                // Cancel all timer events
                // Be careful to cancel new added timer
                RTMPCancelTimer(&pAd->MlmeAux.AssocTimer,         &Cancelled);
@@ -5006,15 +4856,7 @@ VOID     MlmeRestartStateMachine(
                RTMPCancelTimer(&pAd->MlmeAux.AuthTimer,           &Cancelled);
                RTMPCancelTimer(&pAd->MlmeAux.BeaconTimer,         &Cancelled);
                RTMPCancelTimer(&pAd->MlmeAux.ScanTimer,           &Cancelled);
-
-#ifdef QOS_DLS_SUPPORT
-               for (i=0; i<MAX_NUM_OF_DLS_ENTRY; i++)
-               {
-                       RTMPCancelTimer(&pAd->StaCfg.DLSEntry[i].Timer, &Cancelled);
-               }
-#endif // QOS_DLS_SUPPORT //
        }
-#endif // CONFIG_STA_SUPPORT //
 
        // Change back to original channel in case of doing scan
        AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
@@ -5023,8 +4865,6 @@ VOID      MlmeRestartStateMachine(
        // Resume MSDU which is turned off durning scan
        RTMPResumeMsduTransmission(pAd);
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // Set all state machines back IDLE
                pAd->Mlme.CntlMachine.CurrState    = CNTL_IDLE;
@@ -5033,16 +4873,14 @@ VOID    MlmeRestartStateMachine(
                pAd->Mlme.AuthRspMachine.CurrState = AUTH_RSP_IDLE;
                pAd->Mlme.SyncMachine.CurrState    = SYNC_IDLE;
                pAd->Mlme.ActMachine.CurrState    = ACT_IDLE;
-#ifdef QOS_DLS_SUPPORT
-               pAd->Mlme.DlsMachine.CurrState    = DLS_IDLE;
-#endif // QOS_DLS_SUPPORT //
        }
-#endif // CONFIG_STA_SUPPORT //
 
+#ifdef RT2860
        // Remove running state
        NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
        pAd->Mlme.bRunning = FALSE;
        NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
+#endif
 }
 
 /*! \brief     test if the MLME Queue is empty
@@ -5120,7 +4958,6 @@ VOID MlmeQueueDestroy(
  IRQL = DISPATCH_LEVEL
 
  */
-#ifdef CONFIG_STA_SUPPORT
 BOOLEAN MsgTypeSubst(
        IN PRTMP_ADAPTER  pAd,
        IN PFRAME_802_11 pFrame,
@@ -5144,16 +4981,6 @@ BOOLEAN MsgTypeSubst(
                        *MsgType = MT2_AIRONET_MSG;
                        return (TRUE);
                }
-#ifdef LEAP_SUPPORT
-               if ( pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP ) //LEAP
-               {
-                       // LEAP frames
-                       *Machine = LEAP_STATE_MACHINE;
-                       EAPType = *((UCHAR*)pFrame + LENGTH_802_11 + LENGTH_802_1_H + 1);
-                       return (LeapMsgTypeSubst(EAPType, MsgType));
-               }
-               else
-#endif // LEAP_SUPPORT //
                {
                        *Machine = WPA_PSK_STATE_MACHINE;
                        EAPType = *((UCHAR*)pFrame + LENGTH_802_11 + LENGTH_802_1_H + 1);
@@ -5240,7 +5067,6 @@ BOOLEAN MsgTypeSubst(
 
        return TRUE;
 }
-#endif // CONFIG_STA_SUPPORT //
 
 // ===========================================================================================
 // state_machine.c
@@ -5462,7 +5288,6 @@ VOID AsicUpdateAutoFallBackTable(
                                        }
                                }
                                break;
-#ifdef DOT11_N_SUPPORT
                        case 2:         //HT-MIX
                        case 3:         //HT-GF
                                {
@@ -5524,7 +5349,6 @@ VOID AsicUpdateAutoFallBackTable(
                                        }
                                }
                                break;
-#endif // DOT11_N_SUPPORT //
                }
 
                pNextTxRate = pCurrTxRate;
@@ -5566,7 +5390,6 @@ VOID      AsicUpdateProtect(
        UCHAR                   i;
        UINT32 MacReg = 0;
 
-#ifdef DOT11_N_SUPPORT
        if (!(pAd->CommonCfg.bHTProtect) && (OperationMode != 8))
        {
                return;
@@ -5580,19 +5403,14 @@ VOID    AsicUpdateProtect(
                SetMask = ALLN_SETPROTECT;
                OperationMode = 8;
        }
-#endif // DOT11_N_SUPPORT //
 
        // Config ASIC RTS threshold register
        RTMP_IO_READ32(pAd, TX_RTS_CFG, &MacReg);
        MacReg &= 0xFF0000FF;
-#if 0
-       MacReg |= (pAd->CommonCfg.RtsThreshold << 8);
-#else
+
        // If the user want disable RtsThreshold and enable Amsdu/Ralink-Aggregation, set the RtsThreshold as 4096
         if ((
-#ifdef DOT11_N_SUPPORT
                        (pAd->CommonCfg.BACapability.field.AmsduEnable) ||
-#endif // DOT11_N_SUPPORT //
                        (pAd->CommonCfg.bAggregationCapable == TRUE))
             && pAd->CommonCfg.RtsThreshold == MAX_RTS_THRESHOLD)
         {
@@ -5602,7 +5420,6 @@ VOID      AsicUpdateProtect(
         {
                        MacReg |= (pAd->CommonCfg.RtsThreshold << 8);
         }
-#endif
 
        RTMP_IO_WRITE32(pAd, TX_RTS_CFG, MacReg);
 
@@ -5641,7 +5458,6 @@ VOID      AsicUpdateProtect(
                Protect[1] = ProtCfg.word;
        }
 
-#ifdef DOT11_N_SUPPORT
        // Decide HT frame protection.
        if ((SetMask & ALLN_SETPROTECT) != 0)
        {
@@ -5772,7 +5588,6 @@ VOID      AsicUpdateProtect(
                                break;
                }
        }
-#endif // DOT11_N_SUPPORT //
 
        offset = CCK_PROT_CFG;
        for (i = 0;i < 6;i++)
@@ -5784,6 +5599,165 @@ VOID    AsicUpdateProtect(
        }
 }
 
+#ifdef RT30xx
+// add by johnli, RF power sequence setup
+/*
+       ==========================================================================
+       Description:
+
+       Load RF normal operation-mode setup
+
+       ==========================================================================
+ */
+VOID RT30xxLoadRFNormalModeSetup(
+       IN PRTMP_ADAPTER        pAd)
+{
+       UCHAR RFValue;
+
+       // RX0_PD & TX0_PD, RF R1 register Bit 2 & Bit 3 to 0 and RF_BLOCK_en,RX1_PD & TX1_PD, Bit0, Bit 4 & Bit5 to 1
+       RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
+       RFValue = (RFValue & (~0x0C)) | 0x31;
+       RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
+
+       // TX_LO2_en, RF R15 register Bit 3 to 0
+       RT30xxReadRFRegister(pAd, RF_R15, &RFValue);
+       RFValue &= (~0x08);
+       RT30xxWriteRFRegister(pAd, RF_R15, RFValue);
+
+       // TX_LO1_en, RF R17 register Bit 3 to 0
+       RT30xxReadRFRegister(pAd, RF_R17, &RFValue);
+       RFValue &= (~0x08);
+       // to fix rx long range issue
+       if (((pAd->MACVersion & 0xffff) >= 0x0211) && (pAd->NicConfig2.field.ExternalLNAForG == 0))
+       {
+               RFValue |= 0x20;
+       }
+       RT30xxWriteRFRegister(pAd, RF_R17, RFValue);
+
+       // RX_LO1_en, RF R20 register Bit 3 to 0
+       RT30xxReadRFRegister(pAd, RF_R20, &RFValue);
+       RFValue &= (~0x08);
+       RT30xxWriteRFRegister(pAd, RF_R20, RFValue);
+
+       // RX_LO2_en, RF R21 register Bit 3 to 0
+       RT30xxReadRFRegister(pAd, RF_R21, &RFValue);
+       RFValue &= (~0x08);
+       RT30xxWriteRFRegister(pAd, RF_R21, RFValue);
+
+       // LDORF_VC, RF R27 register Bit 2 to 0
+       RT30xxReadRFRegister(pAd, RF_R27, &RFValue);
+       if ((pAd->MACVersion & 0xffff) < 0x0211)
+               RFValue = (RFValue & (~0x77)) | 0x3;
+       else
+               RFValue = (RFValue & (~0x77));
+       RT30xxWriteRFRegister(pAd, RF_R27, RFValue);
+       /* end johnli */
+}
+
+/*
+       ==========================================================================
+       Description:
+
+       Load RF sleep-mode setup
+
+       ==========================================================================
+ */
+VOID RT30xxLoadRFSleepModeSetup(
+       IN PRTMP_ADAPTER        pAd)
+{
+       UCHAR RFValue;
+       UINT32 MACValue;
+
+       // RF_BLOCK_en. RF R1 register Bit 0 to 0
+       RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
+       RFValue &= (~0x01);
+       RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
+
+       // VCO_IC, RF R7 register Bit 4 & Bit 5 to 0
+       RT30xxReadRFRegister(pAd, RF_R07, &RFValue);
+       RFValue &= (~0x30);
+       RT30xxWriteRFRegister(pAd, RF_R07, RFValue);
+
+       // Idoh, RF R9 register Bit 1, Bit 2 & Bit 3 to 0
+       RT30xxReadRFRegister(pAd, RF_R09, &RFValue);
+       RFValue &= (~0x0E);
+       RT30xxWriteRFRegister(pAd, RF_R09, RFValue);
+
+       // RX_CTB_en, RF R21 register Bit 7 to 0
+       RT30xxReadRFRegister(pAd, RF_R21, &RFValue);
+       RFValue &= (~0x80);
+       RT30xxWriteRFRegister(pAd, RF_R21, RFValue);
+
+       // LDORF_VC, RF R27 register Bit 0, Bit 1 & Bit 2 to 1
+       RT30xxReadRFRegister(pAd, RF_R27, &RFValue);
+       RFValue |= 0x77;
+       RT30xxWriteRFRegister(pAd, RF_R27, RFValue);
+
+       RTMP_IO_READ32(pAd, LDO_CFG0, &MACValue);
+       MACValue |= 0x1D000000;
+       RTMP_IO_WRITE32(pAd, LDO_CFG0, MACValue);
+}
+
+/*
+       ==========================================================================
+       Description:
+
+       Reverse RF sleep-mode setup
+
+       ==========================================================================
+ */
+VOID RT30xxReverseRFSleepModeSetup(
+       IN PRTMP_ADAPTER        pAd)
+{
+       UCHAR RFValue;
+       UINT32 MACValue;
+
+       // RF_BLOCK_en, RF R1 register Bit 0 to 1
+       RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
+       RFValue |= 0x01;
+       RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
+
+       // VCO_IC, RF R7 register Bit 4 & Bit 5 to 1
+       RT30xxReadRFRegister(pAd, RF_R07, &RFValue);
+       RFValue |= 0x30;
+       RT30xxWriteRFRegister(pAd, RF_R07, RFValue);
+
+       // Idoh, RF R9 register Bit 1, Bit 2 & Bit 3 to 1
+       RT30xxReadRFRegister(pAd, RF_R09, &RFValue);
+       RFValue |= 0x0E;
+       RT30xxWriteRFRegister(pAd, RF_R09, RFValue);
+
+       // RX_CTB_en, RF R21 register Bit 7 to 1
+       RT30xxReadRFRegister(pAd, RF_R21, &RFValue);
+       RFValue |= 0x80;
+       RT30xxWriteRFRegister(pAd, RF_R21, RFValue);
+
+       // LDORF_VC, RF R27 register Bit 2 to 0
+       RT30xxReadRFRegister(pAd, RF_R27, &RFValue);
+       if ((pAd->MACVersion & 0xffff) < 0x0211)
+               RFValue = (RFValue & (~0x77)) | 0x3;
+       else
+               RFValue = (RFValue & (~0x77));
+       RT30xxWriteRFRegister(pAd, RF_R27, RFValue);
+
+       // RT3071 version E has fixed this issue
+       if ((pAd->NicConfig2.field.DACTestBit == 1) && ((pAd->MACVersion & 0xffff) < 0x0211))
+       {
+               // patch tx EVM issue temporarily
+               RTMP_IO_READ32(pAd, LDO_CFG0, &MACValue);
+               MACValue = ((MACValue & 0xE0FFFFFF) | 0x0D000000);
+               RTMP_IO_WRITE32(pAd, LDO_CFG0, MACValue);
+       }
+       else
+       {
+               RTMP_IO_READ32(pAd, LDO_CFG0, &MACValue);
+               MACValue = ((MACValue & 0xE0FFFFFF) | 0x01000000);
+               RTMP_IO_WRITE32(pAd, LDO_CFG0, MACValue);
+       }
+}
+// end johnli
+#endif // RT30xx //
+
 /*
        ==========================================================================
        Description:
@@ -5805,6 +5779,21 @@ VOID AsicSwitchChannel(
        RTMP_RF_REGS *RFRegTable;
 
        // Search Tx power value
+#ifdef RT30xx
+       // We can't use ChannelList to search channel, since some central channl's txpowr doesn't list
+       // in ChannelList, so use TxPower array instead.
+       //
+       for (index = 0; index < MAX_NUM_OF_CHANNELS; index++)
+       {
+               if (Channel == pAd->TxPower[index].Channel)
+       {
+                       TxPwer = pAd->TxPower[index].Power;
+                       TxPwer2 = pAd->TxPower[index].Power2;
+                       break;
+               }
+       }
+#endif
+#ifndef RT30xx
        for (index = 0; index < pAd->ChannelListNum; index++)
        {
                if (Channel == pAd->ChannelList[index].Channel)
@@ -5814,30 +5803,170 @@ VOID AsicSwitchChannel(
                        break;
                }
        }
+#endif
 
        if (index == MAX_NUM_OF_CHANNELS)
        {
+#ifndef RT30xx
                DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: Cant find the Channel#%d \n", Channel));
+#endif
+#ifdef RT30xx
+               DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: Can't find the Channel#%d \n", Channel));
+#endif
        }
 
+#ifdef RT2870
+       // The RF programming sequence is difference between 3xxx and 2xxx
+#ifdef RT30xx
+       if ((IS_RT3070(pAd) || IS_RT3090(pAd)) && ((pAd->RfIcType == RFIC_3020) || (pAd->RfIcType == RFIC_2020) ||
+               (pAd->RfIcType == RFIC_3021) || (pAd->RfIcType == RFIC_3022)))
+#endif
+#ifndef RT30xx
+       if (IS_RT3070(pAd) && ((pAd->RfIcType == RFIC_3020) || (pAd->RfIcType == RFIC_2020)))
+#endif
        {
-               RFRegTable = RF2850RegTable;
+               /* modify by WY for Read RF Reg. error */
+               UCHAR RFValue;
 
-               switch (pAd->RfIcType)
+               for (index = 0; index < NUM_OF_3020_CHNL; index++)
                {
-                       case RFIC_2820:
-                       case RFIC_2850:
-                       case RFIC_2720:
-                       case RFIC_2750:
-
-                       for (index = 0; index < NUM_OF_2850_CHNL; index++)
+                       if (Channel == FreqItems3020[index].Channel)
                        {
-                               if (Channel == RFRegTable[index].Channel)
+                               // Programming channel parameters
+                               RT30xxWriteRFRegister(pAd, RF_R02, FreqItems3020[index].N);
+                               RT30xxWriteRFRegister(pAd, RF_R03, FreqItems3020[index].K);
+
+#ifndef RT30xx
+                               RT30xxReadRFRegister(pAd, RF_R06, (PUCHAR)&RFValue);
+                               RFValue = (RFValue & 0xFC) | FreqItems3020[index].R;
+                               RT30xxWriteRFRegister(pAd, RF_R06, (UCHAR)RFValue);
+
+                               // Set Tx Power
+                               RT30xxReadRFRegister(pAd, RF_R12, (PUCHAR)&RFValue);
+                               RFValue = (RFValue & 0xE0) | TxPwer;
+                               RT30xxWriteRFRegister(pAd, RF_R12, (UCHAR)RFValue);
+
+                               // Set RF offset
+                               RT30xxReadRFRegister(pAd, RF_R23, (PUCHAR)&RFValue);
+                               RFValue = (RFValue & 0x80) | pAd->RfFreqOffset;
+                               RT30xxWriteRFRegister(pAd, RF_R23, (UCHAR)RFValue);
+#endif
+#ifdef RT30xx
+                               RT30xxReadRFRegister(pAd, RF_R06, &RFValue);
+                               RFValue = (RFValue & 0xFC) | FreqItems3020[index].R;
+                               RT30xxWriteRFRegister(pAd, RF_R06, RFValue);
+
+                               // Set Tx0 Power
+                               RT30xxReadRFRegister(pAd, RF_R12, &RFValue);
+                               RFValue = (RFValue & 0xE0) | TxPwer;
+                               RT30xxWriteRFRegister(pAd, RF_R12, RFValue);
+
+                               // Set Tx1 Power
+                               RT30xxReadRFRegister(pAd, RF_R13, &RFValue);
+                               RFValue = (RFValue & 0xE0) | TxPwer2;
+                               RT30xxWriteRFRegister(pAd, RF_R13, RFValue);
+
+                               // Tx/Rx Stream setting
+                               RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
+                               //if (IS_RT3090(pAd))
+                               //      RFValue |= 0x01; // Enable RF block.
+                               RFValue &= 0x03;        //clear bit[7~2]
+                               if (pAd->Antenna.field.TxPath == 1)
+                                       RFValue |= 0xA0;
+                               else if (pAd->Antenna.field.TxPath == 2)
+                                       RFValue |= 0x80;
+                               if (pAd->Antenna.field.RxPath == 1)
+                                       RFValue |= 0x50;
+                               else if (pAd->Antenna.field.RxPath == 2)
+                                       RFValue |= 0x40;
+                               RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
+
+                               // Set RF offset
+                               RT30xxReadRFRegister(pAd, RF_R23, &RFValue);
+                               RFValue = (RFValue & 0x80) | pAd->RfFreqOffset;
+                               RT30xxWriteRFRegister(pAd, RF_R23, RFValue);
+#endif
+
+                               // Set BW
+                               if (!bScan && (pAd->CommonCfg.BBPCurrentBW == BW_40))
                                {
-                                       R2 = RFRegTable[index].R2;
-                                       if (pAd->Antenna.field.TxPath == 1)
-                                       {
-                                               R2 |= 0x4000;   // If TXpath is 1, bit 14 = 1;
+                                       RFValue = pAd->Mlme.CaliBW40RfR24;
+                                       //DISABLE_11N_CHECK(pAd);
+                               }
+                               else
+                               {
+                                       RFValue = pAd->Mlme.CaliBW20RfR24;
+                               }
+#ifndef RT30xx
+                               RT30xxWriteRFRegister(pAd, RF_R24, (UCHAR)RFValue);
+
+                               // Enable RF tuning
+                               RT30xxReadRFRegister(pAd, RF_R07, (PUCHAR)&RFValue);
+                               RFValue = RFValue | 0x1;
+                               RT30xxWriteRFRegister(pAd, RF_R07, (UCHAR)RFValue);
+
+                               // latch channel for future usage.
+                               pAd->LatchRfRegs.Channel = Channel;
+#endif
+#ifdef RT30xx
+                               RT30xxWriteRFRegister(pAd, RF_R24, RFValue);
+                               RT30xxWriteRFRegister(pAd, RF_R31, RFValue);
+
+                               // Enable RF tuning
+                               RT30xxReadRFRegister(pAd, RF_R07, &RFValue);
+                               RFValue = RFValue | 0x1;
+                               RT30xxWriteRFRegister(pAd, RF_R07, RFValue);
+
+                               // latch channel for future usage.
+                               pAd->LatchRfRegs.Channel = Channel;
+
+                               DBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%d, Pwr1=%d, %dT), N=0x%02X, K=0x%02X, R=0x%02X\n",
+                                       Channel,
+                                       pAd->RfIcType,
+                                       TxPwer,
+                                       TxPwer2,
+                                       pAd->Antenna.field.TxPath,
+                                       FreqItems3020[index].N,
+                                       FreqItems3020[index].K,
+                                       FreqItems3020[index].R));
+#endif
+
+                               break;
+                       }
+               }
+
+#ifndef RT30xx
+               DBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%d, Pwr1=%d, %dT), N=0x%02X, K=0x%02X, R=0x%02X\n",
+                       Channel,
+                       pAd->RfIcType,
+                       TxPwer,
+                       TxPwer2,
+                       pAd->Antenna.field.TxPath,
+                       FreqItems3020[index].N,
+                       FreqItems3020[index].K,
+                       FreqItems3020[index].R));
+#endif
+       }
+       else
+#endif // RT2870 //
+       {
+               RFRegTable = RF2850RegTable;
+
+               switch (pAd->RfIcType)
+               {
+                       case RFIC_2820:
+                       case RFIC_2850:
+                       case RFIC_2720:
+                       case RFIC_2750:
+
+                       for (index = 0; index < NUM_OF_2850_CHNL; index++)
+                       {
+                               if (Channel == RFRegTable[index].Channel)
+                               {
+                                       R2 = RFRegTable[index].R2;
+                                       if (pAd->Antenna.field.TxPath == 1)
+                                       {
+                                               R2 |= 0x4000;   // If TXpath is 1, bit 14 = 1;
                                        }
 
                                        if (pAd->Antenna.field.RxPath == 2)
@@ -6073,6 +6202,53 @@ VOID     AsicAntennaSelect(
        IN      PRTMP_ADAPTER   pAd,
        IN      UCHAR                   Channel)
 {
+#ifdef RT30xx
+                       if (pAd->Mlme.OneSecPeriodicRound % 2 == 1)
+                       {
+                               // patch for AsicSetRxAnt failed
+                               pAd->RxAnt.EvaluatePeriod = 0;
+
+                               // check every 2 second. If rcv-beacon less than 5 in the past 2 second, then AvgRSSI is no longer a
+                               // valid indication of the distance between this AP and its clients.
+                               if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
+                               {
+                                       SHORT   realavgrssi1;
+
+                                       // if no traffic then reset average rssi to trigger evaluation
+                                       if (pAd->StaCfg.NumOfAvgRssiSample < 5)
+                                       {
+                                               pAd->RxAnt.Pair1LastAvgRssi = (-99);
+                                               pAd->RxAnt.Pair2LastAvgRssi = (-99);
+                                               DBGPRINT(RT_DEBUG_TRACE, ("MlmePeriodicExec: no traffic/beacon, reset RSSI\n"));
+                                       }
+
+                                       pAd->StaCfg.NumOfAvgRssiSample = 0;
+                                       realavgrssi1 = (pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1PrimaryRxAnt] >> 3);
+
+                                       DBGPRINT(RT_DEBUG_TRACE,("Ant-realrssi0(%d), Lastrssi0(%d), EvaluateStableCnt=%d\n", realavgrssi1, pAd->RxAnt.Pair1LastAvgRssi, pAd->RxAnt.EvaluateStableCnt));
+
+                                       // if the difference between two rssi is larger or less than 5, then evaluate the other antenna
+                                       if ((pAd->RxAnt.EvaluateStableCnt < 2) || (realavgrssi1 > (pAd->RxAnt.Pair1LastAvgRssi + 5)) || (realavgrssi1 < (pAd->RxAnt.Pair1LastAvgRssi - 5)))
+                                       {
+                                               pAd->RxAnt.Pair1LastAvgRssi = realavgrssi1;
+                                               AsicEvaluateRxAnt(pAd);
+                                       }
+                               }
+                               else
+                               {
+                                       // if not connected, always switch antenna to try to connect
+                                       UCHAR   temp;
+
+                                       temp = pAd->RxAnt.Pair1PrimaryRxAnt;
+                                       pAd->RxAnt.Pair1PrimaryRxAnt = pAd->RxAnt.Pair1SecondaryRxAnt;
+                                       pAd->RxAnt.Pair1SecondaryRxAnt = temp;
+
+                                       DBGPRINT(RT_DEBUG_TRACE, ("MlmePeriodicExec: no connect, switch to another one to try connection\n"));
+
+                                       AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
+                               }
+                       }
+#endif /* RT30xx */
 }
 
 /*
@@ -6142,11 +6318,13 @@ VOID AsicAdjustTxPower(
        ULONG           TxPwr[5];
        CHAR            Value;
 
+#ifdef RT2860
        if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)
                || (pAd->bPCIclkOff == TRUE)
                || RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF)
                || RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
                return;
+#endif
 
        if (pAd->CommonCfg.BBPCurrentBW == BW_40)
        {
@@ -6288,95 +6466,6 @@ VOID AsicAdjustTxPower(
        RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &BbpR1);
        BbpR1 &= 0xFC;
 
-#ifdef SINGLE_SKU
-       // Handle regulatory max tx power constrain
-       do
-       {
-               UCHAR    TxPwrInEEPROM = 0xFF, CountryTxPwr = 0xFF, criterion;
-               UCHAR    AdjustMaxTxPwr[40];
-
-               if (pAd->CommonCfg.Channel > 14) // 5G band
-                       TxPwrInEEPROM = ((pAd->CommonCfg.DefineMaxTxPwr & 0xFF00) >> 8);
-               else // 2.4G band
-                       TxPwrInEEPROM = (pAd->CommonCfg.DefineMaxTxPwr & 0x00FF);
-               CountryTxPwr = GetCuntryMaxTxPwr(pAd, pAd->CommonCfg.Channel);
-
-               // error handling, range check
-               if ((TxPwrInEEPROM > 0x50) || (CountryTxPwr > 0x50))
-               {
-                       DBGPRINT(RT_DEBUG_ERROR,("AsicAdjustTxPower - Invalid max tx power (=0x%02x), CountryTxPwr=%d\n", TxPwrInEEPROM, CountryTxPwr));
-                       break;
-               }
-
-               criterion = *((PUCHAR)TxPwr + 2) & 0xF;        // FAE use OFDM 6M as criterion
-
-               DBGPRINT_RAW(RT_DEBUG_TRACE,("AsicAdjustTxPower (criterion=%d, TxPwrInEEPROM=%d, CountryTxPwr=%d)\n", criterion, TxPwrInEEPROM, CountryTxPwr));
-
-               // Adjust max tx power according to the relationship of tx power in E2PROM
-               for (i=0; i<5; i++)
-               {
-                       // CCK will have 4dBm larger than OFDM
-                       // Therefore, we should separate to parse the tx power field
-                       if (i == 0)
-                       {
-                               for (j=0; j<8; j++)
-                               {
-                                       Value = (CHAR)((TxPwr[i] >> j*4) & 0x0F);
-
-                                       if (j < 4)
-                                       {
-                                               // CCK will have 4dBm larger than OFDM
-                                               AdjustMaxTxPwr[i*8+j] = TxPwrInEEPROM + (Value - criterion) + 4;
-                                       }
-                                       else
-                                       {
-                                               AdjustMaxTxPwr[i*8+j] = TxPwrInEEPROM + (Value - criterion);
-                                       }
-                                       DBGPRINT_RAW(RT_DEBUG_TRACE,("AsicAdjustTxPower (i/j=%d/%d, Value=%d, %d)\n", i, j, Value, AdjustMaxTxPwr[i*8+j]));
-                               }
-                       }
-                       else
-                       {
-                               for (j=0; j<8; j++)
-                               {
-                                       Value = (CHAR)((TxPwr[i] >> j*4) & 0x0F);
-
-                                       AdjustMaxTxPwr[i*8+j] = TxPwrInEEPROM + (Value - criterion);
-                                       DBGPRINT_RAW(RT_DEBUG_TRACE,("AsicAdjustTxPower (i/j=%d/%d, Value=%d, %d)\n", i, j, Value, AdjustMaxTxPwr[i*8+j]));
-                               }
-                       }
-               }
-
-               // Adjust tx power according to the relationship
-               for (i=0; i<5; i++)
-               {
-                       if (TxPwr[i] != 0xffffffff)
-                       {
-                               for (j=0; j<8; j++)
-                               {
-                                       Value = (CHAR)((TxPwr[i] >> j*4) & 0x0F);
-
-                                       // The system tx power is larger than the regulatory, the power should be restrain
-                                       if (AdjustMaxTxPwr[i*8+j] > CountryTxPwr)
-                                       {
-                                               // decrease to zero and don't need to take care BBPR1
-                                               if ((Value - (AdjustMaxTxPwr[i*8+j] - CountryTxPwr)) > 0)
-                                                       Value -= (AdjustMaxTxPwr[i*8+j] - CountryTxPwr);
-                                               else
-                                                       Value = 0;
-
-                                               DBGPRINT_RAW(RT_DEBUG_TRACE,("AsicAdjustTxPower (i/j=%d/%d, Value=%d, %d)\n", i, j, Value, AdjustMaxTxPwr[i*8+j]));
-                                       }
-                                       else
-                                               DBGPRINT_RAW(RT_DEBUG_TRACE,("AsicAdjustTxPower (i/j=%d/%d, Value=%d, %d, no change)\n", i, j, Value, AdjustMaxTxPwr[i*8+j]));
-
-                                               TxPwr[i] = (TxPwr[i] & ~(0x0000000F << j*4)) | (Value << j*4);
-                               }
-                       }
-               }
-       } while (FALSE);
-#endif // SINGLE_SKU //
-
        /* calculate delta power based on the percentage specified from UI */
        // E2PROM setting is calibrated for maximum TX power (i.e. 100%)
        // We lower TX power here according to the percentage specified from UI
@@ -6446,7 +6535,6 @@ VOID AsicAdjustTxPower(
 
 }
 
-#ifdef CONFIG_STA_SUPPORT
 /*
        ==========================================================================
        Description:
@@ -6492,12 +6580,22 @@ VOID AsicForceSleep(
  */
 VOID AsicForceWakeup(
        IN PRTMP_ADAPTER pAd,
+#ifdef RT2860
        IN UCHAR         Level)
+#endif
+#ifdef RT2870
+       IN BOOLEAN    bFromTx)
+#endif
 {
     DBGPRINT(RT_DEBUG_TRACE, ("--> AsicForceWakeup \n"));
+#ifdef RT2860
     RT28XX_STA_FORCE_WAKEUP(pAd, Level);
+#endif
+#ifdef RT2870
+    RT28XX_STA_FORCE_WAKEUP(pAd, bFromTx);
+#endif
 }
-#endif // CONFIG_STA_SUPPORT //
+
 /*
        ==========================================================================
        Description:
@@ -6612,9 +6710,7 @@ VOID AsicDisableRDG(
 
        Data  &= 0xFFFFFF00;
        if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_DYNAMIC_BE_TXOP_ACTIVE)
-#ifdef DOT11_N_SUPPORT
                && (pAd->MacTab.fAnyStationMIMOPSDynamic == FALSE)
-#endif // DOT11_N_SUPPORT //
        )
        {
                // For CWC test, change txop from 0x30 to 0x20 in TxBurst mode
@@ -6669,8 +6765,7 @@ VOID AsicEnableBssSync(
        DBGPRINT(RT_DEBUG_TRACE, ("--->AsicEnableBssSync(INFRA mode)\n"));
 
        RTMP_IO_READ32(pAd, BCN_TIME_CFG, &csr.word);
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+
        {
                csr.field.BeaconInterval = pAd->CommonCfg.BeaconPeriod << 4; // ASIC register in units of 1/16 TU
                csr.field.bTsfTicking = 1;
@@ -6678,7 +6773,7 @@ VOID AsicEnableBssSync(
                csr.field.bBeaconGen  = 0; // do NOT generate BEACON
                csr.field.bTBTTEnable = 1;
        }
-#endif // CONFIG_STA_SUPPORT //
+
        RTMP_IO_WRITE32(pAd, BCN_TIME_CFG, csr.word);
 }
 
@@ -6709,6 +6804,7 @@ VOID AsicEnableIbssSync(
        csr9.field.bTsfTicking = 0;
        RTMP_IO_WRITE32(pAd, BCN_TIME_CFG, csr9.word);
 
+#ifdef RT2860
        // move BEACON TXD and frame content to on-chip memory
        ptr = (PUCHAR)&pAd->BeaconTxWI;
        for (i=0; i<TXWI_SIZE; i+=4)  // 16-byte TXWI field
@@ -6726,6 +6822,24 @@ VOID AsicEnableIbssSync(
                RTMP_IO_WRITE32(pAd, HW_BEACON_BASE0 + TXWI_SIZE + i, longptr);
                ptr +=4;
        }
+#endif
+#ifdef RT2870
+       // move BEACON TXD and frame content to on-chip memory
+       ptr = (PUCHAR)&pAd->BeaconTxWI;
+       for (i=0; i<TXWI_SIZE; i+=2)  // 16-byte TXWI field
+       {
+               RTUSBMultiWrite(pAd, HW_BEACON_BASE0 + i, ptr, 2);
+               ptr += 2;
+       }
+
+       // start right after the 16-byte TXWI field
+       ptr = pAd->BeaconBuf;
+       for (i=0; i< pAd->BeaconTxWI.MPDUtotalByteCount; i+=2)
+       {
+               RTUSBMultiWrite(pAd, HW_BEACON_BASE0 + TXWI_SIZE + i, ptr, 2);
+               ptr +=2;
+       }
+#endif // RT2870 //
 
        // start sending BEACON
        csr9.field.BeaconInterval = pAd->CommonCfg.BeaconPeriod << 4; // ASIC register in units of 1/16 TU
@@ -6873,8 +6987,7 @@ VOID AsicSetEdcaParm(
                Ac2Cfg.field.Cwmin = pEdcaParm->Cwmin[QID_AC_VI];
                Ac2Cfg.field.Cwmax = pEdcaParm->Cwmax[QID_AC_VI];
                Ac2Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_VI];
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+
                {
                        // Tuning for Wi-Fi WMM S06
                        if (pAd->CommonCfg.bWiFiTest &&
@@ -6882,15 +6995,22 @@ VOID AsicSetEdcaParm(
                                Ac2Cfg.field.Aifsn -= 1;
 
                        // Tuning for TGn Wi-Fi 5.2.32
-                       // STA TestBed changes in this item: conexant legacy sta ==> broadcom 11n sta
+                       // STA TestBed changes in this item: connexant legacy sta ==> broadcom 11n sta
                        if (STA_TGN_WIFI_ON(pAd) &&
                                pEdcaParm->Aifsn[QID_AC_VI] == 10)
                        {
                                Ac0Cfg.field.Aifsn = 3;
                                Ac2Cfg.field.AcTxop = 5;
                        }
+
+#ifdef RT30xx
+                       if (pAd->RfIcType == RFIC_3020 || pAd->RfIcType == RFIC_2020)
+                       {
+                               // Tuning for WiFi WMM S3-T07: connexant legacy sta ==> broadcom 11n sta.
+                               Ac2Cfg.field.Aifsn = 5;
+                       }
+#endif // RT30xx //
                }
-#endif // CONFIG_STA_SUPPORT //
 
                Ac3Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_VO];
                Ac3Cfg.field.Cwmin = pEdcaParm->Cwmin[QID_AC_VO];
@@ -6932,10 +7052,9 @@ VOID AsicSetEdcaParm(
                CwminCsr.field.Cwmin0 = pEdcaParm->Cwmin[QID_AC_BE];
                CwminCsr.field.Cwmin1 = pEdcaParm->Cwmin[QID_AC_BK];
                CwminCsr.field.Cwmin2 = pEdcaParm->Cwmin[QID_AC_VI];
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       CwminCsr.field.Cwmin3 = pEdcaParm->Cwmin[QID_AC_VO] - 1; //for TGn wifi test
-#endif // CONFIG_STA_SUPPORT //
+
+               CwminCsr.field.Cwmin3 = pEdcaParm->Cwmin[QID_AC_VO] - 1; //for TGn wifi test
+
                RTMP_IO_WRITE32(pAd, WMM_CWMIN_CFG, CwminCsr.word);
 
                CwmaxCsr.word = 0;
@@ -6949,8 +7068,7 @@ VOID AsicSetEdcaParm(
                AifsnCsr.field.Aifsn0 = Ac0Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_BE];
                AifsnCsr.field.Aifsn1 = Ac1Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_BK];
                AifsnCsr.field.Aifsn2 = Ac2Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_VI];
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+
                {
                        // Tuning for Wi-Fi WMM S06
                        if (pAd->CommonCfg.bWiFiTest &&
@@ -6958,20 +7076,25 @@ VOID AsicSetEdcaParm(
                                AifsnCsr.field.Aifsn2 = Ac2Cfg.field.Aifsn - 4;
 
                        // Tuning for TGn Wi-Fi 5.2.32
-                       // STA TestBed changes in this item: conexant legacy sta ==> broadcom 11n sta
+                       // STA TestBed changes in this item: connexant legacy sta ==> broadcom 11n sta
                        if (STA_TGN_WIFI_ON(pAd) &&
                                pEdcaParm->Aifsn[QID_AC_VI] == 10)
                        {
                                AifsnCsr.field.Aifsn0 = 3;
                                AifsnCsr.field.Aifsn2 = 7;
                        }
+#ifdef RT2870
+                       if (INFRA_ON(pAd))
+                               CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_WMM_CAPABLE);
+#endif
                }
-#endif // CONFIG_STA_SUPPORT //
 
-#ifdef CONFIG_STA_SUPPORT
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       AifsnCsr.field.Aifsn3 = Ac3Cfg.field.Aifsn - 1; //pEdcaParm->Aifsn[QID_AC_VO]; //for TGn wifi test
-#endif // CONFIG_STA_SUPPORT //
+               AifsnCsr.field.Aifsn3 = Ac3Cfg.field.Aifsn - 1; //pEdcaParm->Aifsn[QID_AC_VO]; //for TGn wifi test
+#ifdef RT30xx
+               if (pAd->RfIcType == RFIC_3020 || pAd->RfIcType == RFIC_2020)
+                       AifsnCsr.field.Aifsn2 = 0x2; //pEdcaParm->Aifsn[QID_AC_VI]; //for WiFi WMM S4-T04.
+#endif // RT30xx //
+
                RTMP_IO_WRITE32(pAd, WMM_AIFSN_CFG, AifsnCsr.word);
 
                NdisMoveMemory(&pAd->CommonCfg.APEdcaParm, pEdcaParm, sizeof(EDCA_PARM));
@@ -7022,10 +7145,8 @@ VOID     AsicSetSlotTime(
        ULONG   SlotTime;
        UINT32  RegValue = 0;
 
-#ifdef CONFIG_STA_SUPPORT
        if (pAd->CommonCfg.Channel > 14)
                bUseShortSlotTime = TRUE;
-#endif // CONFIG_STA_SUPPORT //
 
        if (bUseShortSlotTime)
                OPSTATUS_SET_FLAG(pAd, fOP_STATUS_SHORT_SLOT_INUSED);
@@ -7034,36 +7155,31 @@ VOID    AsicSetSlotTime(
 
        SlotTime = (bUseShortSlotTime)? 9 : 20;
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
+#ifndef RT30xx
                // force using short SLOT time for FAE to demo performance when TxBurst is ON
                if (((pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE) && (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_WMM_INUSED)))
-#ifdef DOT11_N_SUPPORT
                        || ((pAd->StaActive.SupportedPhyInfo.bHtEnable == TRUE) && (pAd->CommonCfg.BACapability.field.Policy == BA_NOTUSE))
-#endif // DOT11_N_SUPPORT //
                        )
                {
                        // In this case, we will think it is doing Wi-Fi test
                        // And we will not set to short slot when bEnableTxBurst is TRUE.
                }
                else if (pAd->CommonCfg.bEnableTxBurst)
+#endif
+#ifdef RT30xx
+               if (pAd->CommonCfg.bEnableTxBurst)
+#endif
                        SlotTime = 9;
        }
-#endif // CONFIG_STA_SUPPORT //
 
        //
        // For some reasons, always set it to short slot time.
        //
        // ToDo: Should consider capability with 11B
        //
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               if (pAd->StaCfg.BssType == BSS_ADHOC)
-                       SlotTime = 20;
-       }
-#endif // CONFIG_STA_SUPPORT //
+       if (pAd->StaCfg.BssType == BSS_ADHOC)
+               SlotTime = 20;
 
        RTMP_IO_READ32(pAd, BKOFF_SLOT_CFG, &RegValue);
        RegValue = RegValue & 0xFFFFFF00;
@@ -7094,7 +7210,9 @@ VOID AsicAddSharedKeyEntry(
 {
        ULONG offset; //, csr0;
        SHAREDKEY_MODE_STRUC csr1;
+#ifdef RT2860
        INT   i;
+#endif
 
        DBGPRINT(RT_DEBUG_TRACE, ("AsicAddSharedKeyEntry BssIndex=%d, KeyIdx=%d\n", BssIndex,KeyIdx));
 //============================================================================================
@@ -7116,28 +7234,43 @@ VOID AsicAddSharedKeyEntry(
        //
        // fill key material - key + TX MIC + RX MIC
        //
+
        offset = SHARED_KEY_TABLE_BASE + (4*BssIndex + KeyIdx)*HW_KEY_ENTRY_SIZE;
+#ifdef RT2860
        for (i=0; i<MAX_LEN_OF_SHARE_KEY; i++)
        {
                RTMP_IO_WRITE8(pAd, offset + i, pKey[i]);
        }
-
+#endif
+#ifdef RT2870
+       RTUSBMultiWrite(pAd, offset, pKey, MAX_LEN_OF_SHARE_KEY);
+#endif
        offset += MAX_LEN_OF_SHARE_KEY;
        if (pTxMic)
        {
+#ifdef RT2860
                for (i=0; i<8; i++)
                {
                        RTMP_IO_WRITE8(pAd, offset + i, pTxMic[i]);
                }
+#endif
+#ifdef RT2870
+               RTUSBMultiWrite(pAd, offset, pTxMic, 8);
+#endif
        }
 
        offset += 8;
        if (pRxMic)
        {
+#ifdef RT2860
                for (i=0; i<8; i++)
                {
                        RTMP_IO_WRITE8(pAd, offset + i, pRxMic[i]);
                }
+#endif
+#ifdef RT2870
+               RTUSBMultiWrite(pAd, offset, pRxMic, 8);
+#endif
        }
 
 
@@ -7313,7 +7446,9 @@ VOID AsicAddKeyEntry(
        PUCHAR          pTxtsc = pCipherKey->TxTsc;
        UCHAR           CipherAlg = pCipherKey->CipherAlg;
        SHAREDKEY_MODE_STRUC csr1;
+#ifdef RT2860
        UCHAR           i;
+#endif
 
        DBGPRINT(RT_DEBUG_TRACE, ("==> AsicAddKeyEntry\n"));
        //
@@ -7328,10 +7463,15 @@ VOID AsicAddKeyEntry(
        // 2.) Set Key to Asic
        //
        //for (i = 0; i < KeyLen; i++)
+#ifdef RT2860
        for (i = 0; i < MAX_LEN_OF_PEER_KEY; i++)
        {
                RTMP_IO_WRITE8(pAd, offset + i, pKey[i]);
        }
+#endif
+#ifdef RT2870
+       RTUSBMultiWrite(pAd, offset, pKey, MAX_LEN_OF_PEER_KEY);
+#endif
        offset += MAX_LEN_OF_PEER_KEY;
 
        //
@@ -7339,19 +7479,29 @@ VOID AsicAddKeyEntry(
        //
        if (pTxMic)
        {
+#ifdef RT2860
                for (i = 0; i < 8; i++)
                {
                        RTMP_IO_WRITE8(pAd, offset + i, pTxMic[i]);
                }
+#endif
+#ifdef RT2870
+               RTUSBMultiWrite(pAd, offset, pTxMic, 8);
+#endif
        }
        offset += LEN_TKIP_TXMICK;
 
        if (pRxMic)
        {
+#ifdef RT2860
                for (i = 0; i < 8; i++)
                {
                        RTMP_IO_WRITE8(pAd, offset + i, pRxMic[i]);
                }
+#endif
+#ifdef RT2870
+               RTUSBMultiWrite(pAd, offset, pRxMic, 8);
+#endif
        }
 
 
@@ -7361,6 +7511,7 @@ VOID AsicAddKeyEntry(
        //
        if (bTxKey)
        {
+#ifdef RT2860
                offset = MAC_IVEIV_TABLE_BASE + (WCID * HW_IVEIV_ENTRY_SIZE);
                //
                // Write IV
@@ -7384,6 +7535,26 @@ VOID AsicAddKeyEntry(
                        RTMP_IO_WRITE8(pAd, offset + i, pTxtsc[i + 2]);
                }
 
+#endif
+#ifdef RT2870
+               UINT32 tmpVal;
+
+               //
+               // Write IV
+               //
+               IV4 = (KeyIdx << 6);
+               if ((CipherAlg == CIPHER_TKIP) || (CipherAlg == CIPHER_TKIP_NO_MIC) ||(CipherAlg == CIPHER_AES))
+                       IV4 |= 0x20;  // turn on extension bit means EIV existence
+
+               tmpVal = pTxtsc[1] + (((pTxtsc[1] | 0x20) & 0x7f) << 8) + (pTxtsc[0] << 16) + (IV4 << 24);
+               RTMP_IO_WRITE32(pAd, offset, tmpVal);
+
+               //
+               // Write EIV
+               //
+               offset += 4;
+               RTMP_IO_WRITE32(pAd, offset, *(PUINT32)&pCipherKey->TxTsc[2]);
+#endif // RT2870 //
                AsicUpdateWCIDAttribute(pAd, WCID, BssIndex, CipherAlg, bUsePairewiseKeyTable);
        }
 
@@ -7448,10 +7619,15 @@ VOID AsicAddPairwiseKeyEntry(
 
        // EKEY
        offset = PAIRWISE_KEY_TABLE_BASE + (WCID * HW_KEY_ENTRY_SIZE);
+#ifdef RT2860
        for (i=0; i<MAX_LEN_OF_PEER_KEY; i++)
        {
                RTMP_IO_WRITE8(pAd, offset + i, pKey[i]);
        }
+#endif
+#ifdef RT2870
+       RTUSBMultiWrite(pAd, offset, &pCipherKey->Key[0], MAX_LEN_OF_PEER_KEY);
+#endif // RT2870 //
        for (i=0; i<MAX_LEN_OF_PEER_KEY; i+=4)
        {
                UINT32 Value;
@@ -7463,18 +7639,28 @@ VOID AsicAddPairwiseKeyEntry(
        //  MIC KEY
        if (pTxMic)
        {
+#ifdef RT2860
                for (i=0; i<8; i++)
                {
                        RTMP_IO_WRITE8(pAd, offset+i, pTxMic[i]);
                }
+#endif
+#ifdef RT2870
+               RTUSBMultiWrite(pAd, offset, &pCipherKey->TxMic[0], 8);
+#endif // RT2870 //
        }
        offset += 8;
        if (pRxMic)
        {
+#ifdef RT2860
                for (i=0; i<8; i++)
                {
                        RTMP_IO_WRITE8(pAd, offset+i, pRxMic[i]);
                }
+#endif
+#ifdef RT2870
+               RTUSBMultiWrite(pAd, offset, &pCipherKey->RxMic[0], 8);
+#endif // RT2870 //
        }
 
        DBGPRINT(RT_DEBUG_TRACE,("AsicAddPairwiseKeyEntry: WCID #%d Alg=%s\n",WCID, CipherName[CipherAlg]));
@@ -7533,9 +7719,10 @@ BOOLEAN AsicSendCommandToMcu(
                RTMPusecDelay(2);
        } while(i++ < 100);
 
-       if (i >= 100)
+       if (i > 100)
        {
                {
+#ifdef RT2860
                        UINT32 Data;
 
                        // Reset DMA
@@ -7557,9 +7744,13 @@ BOOLEAN AsicSendCommandToMcu(
                        RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
                        Data &= 0xfffffffd;
                        RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
+#endif /* RT2860 */
                DBGPRINT_ERR(("H2M_MAILBOX still hold by MCU. command fail\n"));
                }
                //return FALSE;
+#ifdef RT2870
+               return FALSE;
+#endif
        }
 
        H2MMailbox.field.Owner    = 1;     // pass ownership to MCU
@@ -7579,6 +7770,7 @@ BOOLEAN AsicSendCommandToMcu(
        return TRUE;
 }
 
+#ifdef RT2860
 BOOLEAN AsicCheckCommanOk(
        IN PRTMP_ADAPTER pAd,
        IN UCHAR                 Command)
@@ -7643,6 +7835,7 @@ BOOLEAN AsicCheckCommanOk(
 
        return FALSE;
 }
+#endif /* RT8260 */
 
 /*
        ========================================================================
@@ -7685,8 +7878,6 @@ VOID      RTMPCheckRates(
        NdisMoveMemory(SupRate, NewRate, NewRateLen);
 }
 
-#ifdef CONFIG_STA_SUPPORT
-#ifdef DOT11_N_SUPPORT
 BOOLEAN RTMPCheckChannel(
        IN PRTMP_ADAPTER pAd,
        IN UCHAR                CentralChannel,
@@ -7832,8 +8023,6 @@ BOOLEAN   RTMPCheckHt(
        COPY_AP_HTSETTINGS_FROM_BEACON(pAd, pHtCapability);
        return TRUE;
 }
-#endif // DOT11_N_SUPPORT //
-#endif // CONFIG_STA_SUPPORT //
 
 /*
        ========================================================================
@@ -7866,10 +8055,8 @@ VOID RTMPUpdateMlmeRate(
                        MinimumRate = RATE_1;
                        break;
                case PHY_11BG_MIXED:
-#ifdef DOT11_N_SUPPORT
                case PHY_11ABGN_MIXED:
                case PHY_11BGN_MIXED:
-#endif // DOT11_N_SUPPORT //
                        if ((pAd->MlmeAux.SupRateLen == 4) &&
                                (pAd->MlmeAux.ExtRateLen == 0))
                                // B only AP
@@ -7883,13 +8070,11 @@ VOID RTMPUpdateMlmeRate(
                                MinimumRate = RATE_6;
                        break;
                case PHY_11A:
-#ifdef DOT11_N_SUPPORT
                case PHY_11N_2_4G:      // rt2860 need to check mlmerate for 802.11n
                case PHY_11GN_MIXED:
                case PHY_11AGN_MIXED:
                case PHY_11AN_MIXED:
                case PHY_11N_5G:
-#endif // DOT11_N_SUPPORT //
                        ProperMlmeRate = RATE_24;
                        MinimumRate = RATE_6;
                        break;
@@ -7999,6 +8184,56 @@ CHAR RTMPMaxRssi(
        return larger;
 }
 
+#ifdef RT30xx
+// Antenna divesity use GPIO3 and EESK pin for control
+// Antenna and EEPROM access are both using EESK pin,
+// Therefor we should avoid accessing EESK at the same time
+// Then restore antenna after EEPROM access
+VOID AsicSetRxAnt(
+       IN PRTMP_ADAPTER        pAd,
+       IN UCHAR                        Ant)
+{
+       UINT32  Value;
+       UINT32  x;
+
+       if ((pAd->EepromAccess)                                                                         ||
+               (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS))  ||
+               (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))   ||
+               (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF))                  ||
+               (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
+       {
+               return;
+       }
+
+       // the antenna selection is through firmware and MAC register(GPIO3)
+       if (Ant == 0)
+       {
+               // Main antenna
+               RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
+               x |= (EESK);
+               RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
+
+               RTMP_IO_READ32(pAd, GPIO_CTRL_CFG, &Value);
+               Value &= ~(0x0808);
+               RTMP_IO_WRITE32(pAd, GPIO_CTRL_CFG, Value);
+               DBGPRINT_RAW(RT_DEBUG_TRACE, ("AsicSetRxAnt, switch to main antenna\n"));
+       }
+       else
+       {
+               // Aux antenna
+               RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
+               x &= ~(EESK);
+               RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
+
+               RTMP_IO_READ32(pAd, GPIO_CTRL_CFG, &Value);
+               Value &= ~(0x0808);
+               Value |= 0x08;
+               RTMP_IO_WRITE32(pAd, GPIO_CTRL_CFG, Value);
+               DBGPRINT_RAW(RT_DEBUG_TRACE, ("AsicSetRxAnt, switch to aux antenna\n"));
+       }
+}
+#endif /* RT30xx */
+
 /*
     ========================================================================
     Routine Description:
@@ -8017,40 +8252,67 @@ VOID AsicEvaluateRxAnt(
 {
        UCHAR   BBPR3 = 0;
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS |
-                                                               fRTMP_ADAPTER_HALT_IN_PROGRESS  |
-                                                               fRTMP_ADAPTER_RADIO_OFF                 |
-                                                               fRTMP_ADAPTER_NIC_NOT_EXIST             |
-                                                               fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
-                       return;
-
-               if (pAd->StaCfg.Psm == PWR_SAVE)
+       if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS |
+                               fRTMP_ADAPTER_HALT_IN_PROGRESS |
+                               fRTMP_ADAPTER_RADIO_OFF |
+                               fRTMP_ADAPTER_NIC_NOT_EXIST |
+                               fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS)
+#ifdef RT30xx
+                               || OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)
+                               || (pAd->EepromAccess)
+#endif
+                               )
                        return;
-       }
-#endif // CONFIG_STA_SUPPORT //
 
-       RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
-       BBPR3 &= (~0x18);
-       if(pAd->Antenna.field.RxPath == 3)
+#ifdef RT30xx
+       // two antenna selection mechanism- one is antenna diversity, the other is failed antenna remove
+       // one is antenna diversity:there is only one antenna can rx and tx
+       // the other is failed antenna remove:two physical antenna can rx and tx
+       if (pAd->NicConfig2.field.AntDiversity)
        {
-               BBPR3 |= (0x10);
-       }
-       else if(pAd->Antenna.field.RxPath == 2)
-       {
-               BBPR3 |= (0x8);
+               DBGPRINT(RT_DEBUG_TRACE,("AntDiv - before evaluate Pair1-Ant (%d,%d)\n",
+                       pAd->RxAnt.Pair1PrimaryRxAnt, pAd->RxAnt.Pair1SecondaryRxAnt));
+
+               AsicSetRxAnt(pAd, pAd->RxAnt.Pair1SecondaryRxAnt);
+
+               pAd->RxAnt.EvaluatePeriod = 1; // 1:Means switch to SecondaryRxAnt, 0:Means switch to Pair1PrimaryRxAnt
+               pAd->RxAnt.FirstPktArrivedWhenEvaluate = FALSE;
+               pAd->RxAnt.RcvPktNumWhenEvaluate = 0;
+
+               // a one-shot timer to end the evalution
+               // dynamic adjust antenna evaluation period according to the traffic
+               if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
+                       RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 100);
+               else
+                       RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 300);
        }
-       else if(pAd->Antenna.field.RxPath == 1)
+       else
+#endif
        {
-               BBPR3 |= (0x0);
+               if (pAd->StaCfg.Psm == PWR_SAVE)
+                       return;
+
+               RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
+               BBPR3 &= (~0x18);
+               if(pAd->Antenna.field.RxPath == 3)
+               {
+                       BBPR3 |= (0x10);
+               }
+               else if(pAd->Antenna.field.RxPath == 2)
+               {
+                       BBPR3 |= (0x8);
+               }
+               else if(pAd->Antenna.field.RxPath == 1)
+               {
+                       BBPR3 |= (0x0);
+               }
+               RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
+
+#ifdef RT2860
+               pAd->StaCfg.BBPR3 = BBPR3;
+#endif
        }
-       RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       pAd->StaCfg.BBPR3 = BBPR3;
-#endif // CONFIG_STA_SUPPORT //
+
        if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
                )
        {
@@ -8058,6 +8320,7 @@ VOID AsicEvaluateRxAnt(
                                                                pAd->RalinkCounters.OneSecTxRetryOkCount +
                                                                pAd->RalinkCounters.OneSecTxFailCount;
 
+                       // dynamic adjust antenna evaluation period according to the traffic
                if (TxTotalCnt > 50)
                {
                        RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 20);
@@ -8091,13 +8354,10 @@ VOID AsicRxAntEvalTimeout(
        IN PVOID SystemSpecific3)
 {
        RTMP_ADAPTER    *pAd = (RTMP_ADAPTER *)FunctionContext;
-#ifdef CONFIG_STA_SUPPORT
        UCHAR                   BBPR3 = 0;
        CHAR                    larger = -127, rssi0, rssi1, rssi2;
-#endif // CONFIG_STA_SUPPORT //
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+#ifndef RT30xx
        {
                if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)        ||
                        RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)             ||
@@ -8155,11 +8415,111 @@ VOID AsicRxAntEvalTimeout(
                        BBPR3 |= (0x0);
                }
                RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
+#ifdef RT2860
                pAd->StaCfg.BBPR3 = BBPR3;
+#endif
        }
+#endif /* RT30xx */
+#ifdef RT30xx
+       if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS |
+                                                       fRTMP_ADAPTER_HALT_IN_PROGRESS  |
+                                                       fRTMP_ADAPTER_RADIO_OFF                 |
+                                                       fRTMP_ADAPTER_NIC_NOT_EXIST) ||
+                                                       OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)
+#ifdef RT30xx
+                                                       || (pAd->EepromAccess)
+#endif // RT30xx //
+                                                       )
+               return;
 
-#endif // CONFIG_STA_SUPPORT //
+       {
+               //if (pAd->StaCfg.Psm == PWR_SAVE)
+               //      return;
 
+               if (pAd->NicConfig2.field.AntDiversity)
+               {
+                       if ((pAd->RxAnt.RcvPktNumWhenEvaluate != 0) && (pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1SecondaryRxAnt] >= pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1PrimaryRxAnt]))
+                       {
+                               UCHAR                   temp;
+
+                               //
+                               // select PrimaryRxAntPair
+                               //    Role change, Used Pair1SecondaryRxAnt as PrimaryRxAntPair.
+                               //    Since Pair1SecondaryRxAnt Quality good than Pair1PrimaryRxAnt
+                               //
+                               temp = pAd->RxAnt.Pair1PrimaryRxAnt;
+                               pAd->RxAnt.Pair1PrimaryRxAnt = pAd->RxAnt.Pair1SecondaryRxAnt;
+                               pAd->RxAnt.Pair1SecondaryRxAnt = temp;
+
+                               pAd->RxAnt.Pair1LastAvgRssi = (pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1SecondaryRxAnt] >> 3);
+                               pAd->RxAnt.EvaluateStableCnt = 0;
+                       }
+                       else
+                       {
+                               // if the evaluated antenna is not better than original, switch back to original antenna
+                               AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
+                               pAd->RxAnt.EvaluateStableCnt ++;
+                       }
+
+                       pAd->RxAnt.EvaluatePeriod = 0; // 1:Means switch to SecondaryRxAnt, 0:Means switch to Pair1PrimaryRxAnt
+
+                       DBGPRINT(RT_DEBUG_TRACE,("AsicRxAntEvalAction::After Eval(fix in #%d), <%d, %d>, RcvPktNumWhenEvaluate=%ld\n",
+                                       pAd->RxAnt.Pair1PrimaryRxAnt, (pAd->RxAnt.Pair1AvgRssi[0] >> 3), (pAd->RxAnt.Pair1AvgRssi[1] >> 3), pAd->RxAnt.RcvPktNumWhenEvaluate));
+               }
+               else
+               {
+                       if (pAd->StaCfg.Psm == PWR_SAVE)
+                               return;
+
+                       // if the traffic is low, use average rssi as the criteria
+                       if (pAd->Mlme.bLowThroughput == TRUE)
+                       {
+                               rssi0 = pAd->StaCfg.RssiSample.LastRssi0;
+                               rssi1 = pAd->StaCfg.RssiSample.LastRssi1;
+                               rssi2 = pAd->StaCfg.RssiSample.LastRssi2;
+                       }
+                       else
+                       {
+                               rssi0 = pAd->StaCfg.RssiSample.AvgRssi0;
+                               rssi1 = pAd->StaCfg.RssiSample.AvgRssi1;
+                               rssi2 = pAd->StaCfg.RssiSample.AvgRssi2;
+                       }
+
+                       if(pAd->Antenna.field.RxPath == 3)
+                       {
+                               larger = max(rssi0, rssi1);
+
+                               if (larger > (rssi2 + 20))
+                                       pAd->Mlme.RealRxPath = 2;
+                               else
+                                       pAd->Mlme.RealRxPath = 3;
+                       }
+                       else if(pAd->Antenna.field.RxPath == 2)
+                       {
+                               if (rssi0 > (rssi1 + 20))
+                                       pAd->Mlme.RealRxPath = 1;
+                               else
+                                       pAd->Mlme.RealRxPath = 2;
+                       }
+
+                       RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
+                       BBPR3 &= (~0x18);
+                       if(pAd->Mlme.RealRxPath == 3)
+                       {
+                               BBPR3 |= (0x10);
+                       }
+                       else if(pAd->Mlme.RealRxPath == 2)
+                       {
+                               BBPR3 |= (0x8);
+                       }
+                       else if(pAd->Mlme.RealRxPath == 1)
+                       {
+                               BBPR3 |= (0x0);
+                       }
+                       RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
+               }
+       }
+#endif /* RT30xx */
 }
 
 
@@ -8226,9 +8586,6 @@ BOOLEAN RTMPCheckEntryEnableAutoRateSwitch(
 {
        BOOLEAN         result = TRUE;
 
-
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // only associated STA counts
                if (pEntry && (pEntry->ValidAsCLI) && (pEntry->Sst == SST_ASSOC))
@@ -8237,15 +8594,7 @@ BOOLEAN RTMPCheckEntryEnableAutoRateSwitch(
                }
                else
                        result = FALSE;
-
-#ifdef QOS_DLS_SUPPORT
-               if (pEntry && (pEntry->ValidAsDls))
-                       result = pAd->StaCfg.bAutoTxRateSwitch;
-#endif // QOS_DLS_SUPPORT //
        }
-#endif // CONFIG_STA_SUPPORT //
-
-
 
        return result;
 }
@@ -8254,14 +8603,9 @@ BOOLEAN RTMPCheckEntryEnableAutoRateSwitch(
 BOOLEAN RTMPAutoRateSwitchCheck(
        IN PRTMP_ADAPTER    pAd)
 {
+       if (pAd->StaCfg.bAutoTxRateSwitch)
+               return TRUE;
 
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               if (pAd->StaCfg.bAutoTxRateSwitch)
-                       return TRUE;
-       }
-#endif // CONFIG_STA_SUPPORT //
        return FALSE;
 }
 
@@ -8287,13 +8631,7 @@ UCHAR RTMPStaFixedTxMode(
 {
        UCHAR   tx_mode = FIXED_TXMODE_HT;
 
-
-#ifdef CONFIG_STA_SUPPORT
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               tx_mode = (UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode;
-       }
-#endif // CONFIG_STA_SUPPORT //
+       tx_mode = (UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode;
 
        return tx_mode;
 }
@@ -8350,7 +8688,6 @@ VOID RTMPUpdateLegacyTxSetting(
        }
 }
 
-#ifdef CONFIG_STA_SUPPORT
 /*
        ==========================================================================
        Description:
@@ -8381,7 +8718,12 @@ VOID AsicStaBbpTuning(
                && (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
                        )
                && !(OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE))
+#ifdef RT2860
                && (pAd->bPCIclkOff == FALSE))
+#endif
+#ifdef RT2870
+               )
+#endif
        {
                RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R66, &OrigR66Value);
                R66 = OrigR66Value;
@@ -8393,6 +8735,31 @@ VOID AsicStaBbpTuning(
 
                if (pAd->LatchRfRegs.Channel <= 14)
                {       //BG band
+#ifdef RT2870
+                       // RT3070 is a no LNA solution, it should have different control regarding to AGC gain control
+                       // Otherwise, it will have some throughput side effect when low RSSI
+#ifndef RT30xx
+                       if (IS_RT3070(pAd))
+#endif
+#ifdef RT30xx
+                       if (IS_RT30xx(pAd))
+#endif
+                       {
+                               if (Rssi > RSSI_FOR_MID_LOW_SENSIBILITY)
+                               {
+                                       R66 = 0x1C + 2*GET_LNA_GAIN(pAd) + 0x20;
+                                       if (OrigR66Value != R66)
+                                               RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
+                               }
+                               else
+                               {
+                                       R66 = 0x1C + 2*GET_LNA_GAIN(pAd);
+                                       if (OrigR66Value != R66)
+                                               RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
+                               }
+                       }
+                       else
+#endif // RT2870 //
                        {
                                if (Rssi > RSSI_FOR_MID_LOW_SENSIBILITY)
                                {
@@ -8458,6 +8825,7 @@ VOID AsicStaBbpTuning(
        }
 }
 
+#ifdef RT2860
 VOID AsicResetFromDMABusy(
        IN PRTMP_ADAPTER pAd)
 {
@@ -8557,7 +8925,7 @@ VOID AsicResetPBF(
                DBGPRINT(RT_DEBUG_TRACE, ("<---  Asic HardReset PBF !!!! \n"));
        }
 }
-#endif // CONFIG_STA_SUPPORT //
+#endif /* RT2860 */
 
 VOID RTMPSetAGCInitValue(
        IN PRTMP_ADAPTER        pAd,
@@ -8577,13 +8945,11 @@ VOID RTMPSetAGCInitValue(
                        R66 = (UCHAR)(0x32 + (GET_LNA_GAIN(pAd)*5)/3);
                        RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
                }
-#ifdef DOT11_N_SUPPORT
                else
                {
                        R66 = (UCHAR)(0x3A + (GET_LNA_GAIN(pAd)*5)/3);
                        RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
                }
-#endif // DOT11_N_SUPPORT //
        }
 
 }
@@ -8598,6 +8964,13 @@ VOID AsicTurnOffRFClk(
        UCHAR                   index;
        RTMP_RF_REGS    *RFRegTable;
 
+       // The RF programming sequence is difference between 3xxx and 2xxx
+       if (IS_RT3090(pAd))
+       {
+               RT30xxLoadRFSleepModeSetup(pAd);  // add by johnli,  RF power sequence setup, load RF sleep-mode setup
+               return;
+       }
+
        RFRegTable = RF2850RegTable;
 
        switch (pAd->RfIcType)
@@ -8652,6 +9025,10 @@ VOID AsicTurnOnRFClk(
        UCHAR                   index;
        RTMP_RF_REGS    *RFRegTable;
 
+       // The RF programming sequence is difference between 3xxx and 2xxx
+       if (IS_RT3090(pAd))
+               return;
+
        RFRegTable = RF2850RegTable;
 
        switch (pAd->RfIcType)
@@ -8698,9 +9075,11 @@ VOID AsicTurnOnRFClk(
                        break;
        }
 
+#ifndef RT30xx
        DBGPRINT(RT_DEBUG_TRACE, ("AsicTurnOnRFClk#%d(RF=%d, ) , R2=0x%08x\n",
                Channel,
                pAd->RfIcType,
                R2));
+#endif
 }