Staging: rtxxx0: remove unused code
[safe/jmp/linux-2.6] / drivers / staging / rt2860 / common / mlme.c
index 8dbbd28..ecaa149 100644 (file)
@@ -43,16 +43,13 @@ UCHAR       CISCO_OUI[] = {0x00, 0x40, 0x96};
 
 UCHAR  WPA_OUI[] = {0x00, 0x50, 0xf2, 0x01};
 UCHAR  RSN_OUI[] = {0x00, 0x0f, 0xac};
-UCHAR  WAPI_OUI[] = {0x00, 0x14, 0x72};
 UCHAR   WME_INFO_ELEM[]  = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
 UCHAR   WME_PARM_ELEM[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
 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 DOT11_N_SUPPORT
 UCHAR  PRE_N_HT_OUI[]  = {0x00, 0x90, 0x4c};
-#endif // DOT11_N_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)
@@ -128,7 +125,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
@@ -285,7 +281,6 @@ UCHAR RateSwitchTable11BGN3SForABand[] = { // 3*3
     0x0a, 0x20, 23,  8, 25,
     0x0b, 0x22, 23,  8, 25,
 };
-#endif // DOT11_N_SUPPORT //
 
 PUCHAR ReasonString[] = {
        /* 0  */         "Reserved",
@@ -332,11 +327,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;
-#endif // DOT11_N_SUPPORT //
 UCHAR  ErpIe    = IE_ERP;
 UCHAR  DsIe     = IE_DS_PARM;
 UCHAR  TimIe    = IE_TIM;
@@ -452,7 +445,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));
 
 /*
        ==========================================================================
@@ -483,7 +476,6 @@ NDIS_STATUS MlmeInit(
                pAd->Mlme.bRunning = FALSE;
                NdisAllocateSpinLock(&pAd->Mlme.TaskLock);
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
                        BssTableInit(&pAd->ScanTab);
 
@@ -511,7 +503,7 @@ NDIS_STATUS MlmeInit(
                // software-based RX Antenna diversity
                RTMPInitTimer(pAd, &pAd->Mlme.RxAntEvalTimer, GET_TIMER_FUNCTION(AsicRxAntEvalTimeout), pAd, FALSE);
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
+#ifdef RT2860
                {
                if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
                {
@@ -520,6 +512,7 @@ NDIS_STATUS MlmeInit(
                        RTMPInitTimer(pAd, &pAd->Mlme.RadioOnOffTimer, GET_TIMER_FUNCTION(RadioOnExec), pAd, FALSE);
                }
                }
+#endif
        } while (FALSE);
 
        DBGPRINT(RT_DEBUG_TRACE, ("<-- MLME Initialize\n"));
@@ -574,6 +567,16 @@ 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)
@@ -644,6 +647,9 @@ VOID MlmeHalt(
        IN PRTMP_ADAPTER pAd)
 {
        BOOLEAN           Cancelled;
+#ifdef RT3070
+       UINT32          TxPinCfg = 0x00050F0F;
+#endif // RT3070 //
 
        DBGPRINT(RT_DEBUG_TRACE, ("==> MlmeHalt\n"));
 
@@ -653,7 +659,6 @@ VOID MlmeHalt(
                AsicDisableSync(pAd);
        }
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // Cancel pending timers
                RTMPCancelTimer(&pAd->MlmeAux.AssocTimer,               &Cancelled);
@@ -662,11 +667,13 @@ 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);
                }
+#endif
        }
 
        RTMPCancelTimer(&pAd->Mlme.PeriodicTimer,               &Cancelled);
@@ -679,6 +686,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
@@ -749,6 +777,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.
@@ -762,7 +791,6 @@ VOID MlmePeriodicExec(
                pAd->StaCfg.WpaSupplicantUP = 1;
        }
 
-       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
@@ -802,6 +830,7 @@ VOID MlmePeriodicExec(
                        }
                }
        }
+#endif /* RT2860 */
 
        // Do nothing if the driver is starting halt state.
        // This might happen when timer already been fired before cancel timer with mlmehalt
@@ -811,7 +840,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))
                {
@@ -849,10 +878,9 @@ VOID MlmePeriodicExec(
                        AsicResetFromDMABusy(pAd);
                }
        }
-
+#endif /* RT2860 */
        RT28XX_MLME_PRE_SANITY_CHECK(pAd);
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // Do nothing if monitor mode is on
                if (MONITOR_ON(pAd))
@@ -883,11 +911,14 @@ VOID MlmePeriodicExec(
 //     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))*/)
        {
                // 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)
                                        )
@@ -932,12 +963,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)
@@ -946,6 +977,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)
@@ -962,14 +994,14 @@ VOID MlmePeriodicExec(
                        }
                }
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       STAMlmePeriodicExec(pAd);
+               STAMlmePeriodicExec(pAd);
 
                MlmeResetRalinkCounters(pAd);
 
-               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.
@@ -995,24 +1027,28 @@ VOID MlmePeriodicExec(
                RT28XX_MLME_HANDLER(pAd);
        }
 
-
        pAd->bUpdateBcnCntDone = FALSE;
 }
 
 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.
@@ -1025,6 +1061,7 @@ VOID STAMlmePeriodicExec(
        {
                pAd->StaCfg.WpaSupplicantUP = 1;
        }
+#endif
 
     if ((pAd->PreMediaState != pAd->IndicateMediaState) && (pAd->CommonCfg.bWirelessEvent))
        {
@@ -1035,6 +1072,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) &&
@@ -1044,6 +1082,7 @@ VOID STAMlmePeriodicExec(
        {
                RT28xxPciAsicRadioOff(pAd, GUI_IDLE_POWER_SAVE, 0);
        }
+#endif
 
 
 
@@ -1110,26 +1149,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);
                }
@@ -1155,6 +1179,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
@@ -1179,7 +1204,6 @@ VOID STAMlmePeriodicExec(
                        pAd->StaCfg.AdhocBOnlyJoined = FALSE;
                }
 
-#ifdef DOT11_N_SUPPORT
                if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
                {
                        if ((pAd->StaCfg.AdhocBGJoined) &&
@@ -1196,7 +1220,7 @@ VOID STAMlmePeriodicExec(
                                pAd->StaCfg.Adhoc20NJoined = FALSE;
                        }
                }
-#endif // DOT11_N_SUPPORT //
+#endif /* RT2860 */
 
                //radar detect
                if ((pAd->CommonCfg.Channel > 14)
@@ -1221,6 +1245,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
        {
@@ -1268,7 +1305,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;
@@ -1279,7 +1315,6 @@ SKIP_AUTO_SCAN_CONN:
                pAd->MacTab.fAnyBASession = FALSE;
                AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode,  ALLN_SETPROTECT, FALSE, FALSE);
        }
-#endif // DOT11_N_SUPPORT //
 
        return;
 }
@@ -1389,12 +1424,17 @@ VOID MlmeSelectTxRateTable(
 
                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];
@@ -1402,10 +1442,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)
@@ -1423,7 +1469,7 @@ VOID MlmeSelectTxRateTable(
 
                        }
                        else
-#endif // DOT11_N_SUPPORT //
+#ifdef RT2860
                                if (pAd->CommonCfg.PhyMode == PHY_11B)
                        {
                                *ppTable = RateSwitchTable11B;
@@ -1432,6 +1478,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;
@@ -1456,7 +1508,6 @@ VOID MlmeSelectTxRateTable(
                        break;
                }
 
-#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
@@ -1513,12 +1564,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;
@@ -1530,9 +1582,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;
@@ -1544,9 +1594,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;
@@ -1555,15 +1603,10 @@ VOID MlmeSelectTxRateTable(
 
                        break;
                }
-#ifdef DOT11_N_SUPPORT
-#endif // DOT11_N_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)
                                {
@@ -1586,7 +1629,7 @@ VOID MlmeSelectTxRateTable(
                                }
                                break;
                        }
-#ifdef DOT11_N_SUPPORT
+
                        if (pAd->LatchRfRegs.Channel <= 14)
                        {
                                if (pAd->CommonCfg.TxStream == 1)
@@ -1621,7 +1664,7 @@ 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]));
                }
@@ -1845,13 +1888,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)
@@ -1881,14 +1922,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)
                {
@@ -1932,17 +1970,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);
@@ -1982,11 +2018,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
        //
@@ -2000,7 +2031,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);
@@ -2030,7 +2069,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 +
@@ -2087,14 +2140,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;
@@ -2172,7 +2223,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;
@@ -2218,7 +2268,7 @@ VOID MlmeDynamicTxRateSwitching(
                                        RssiOffset = 8;
                                }
                        }
-#ifdef DOT11_N_SUPPORT
+
                        /*if (MCS15)*/
                        if ((pTable == RateSwitchTable11BGN3S) ||
                                (pTable == RateSwitchTable11N3S) ||
@@ -2284,7 +2334,6 @@ VOID MlmeDynamicTxRateSwitching(
                                        TxRateIdx = MCS0;
                        }
                        else
-#endif // DOT11_N_SUPPORT //
                        {// Legacy mode
                                if (MCS7 && (Rssi > -70))
                                        TxRateIdx = MCS7;
@@ -2458,7 +2507,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;
@@ -2483,11 +2537,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;
 
@@ -2512,14 +2580,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;
@@ -2629,6 +2695,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)
@@ -2638,6 +2707,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
                {
@@ -2693,18 +2765,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);
+                       }
                }
        }
 }
@@ -2721,6 +2809,7 @@ 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);
+
        DBGPRINT(RT_DEBUG_TRACE, ("MlmeSetPsmBit = %d\n", psm));
 }
 
@@ -2861,7 +2950,6 @@ VOID MlmeUpdateTxRates(
 
 //===========================================================================
 //===========================================================================
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                pHtPhy          = &pAd->StaCfg.HTPhyMode;
                pMaxHtPhy       = &pAd->StaCfg.MaxHTPhyMode;
@@ -2990,8 +3078,7 @@ VOID MlmeUpdateTxRates(
        {
                short dbm = 0;
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       dbm = pAd->StaCfg.RssiSample.AvgRssi0 - pAd->BbpRssiToDbmDelta;
+               dbm = pAd->StaCfg.RssiSample.AvgRssi0 - pAd->BbpRssiToDbmDelta;
 
                if (bLinkUp == TRUE)
                        pAd->CommonCfg.TxRate = RATE_24;
@@ -3050,9 +3137,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;
@@ -3060,22 +3145,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;
@@ -3118,7 +3199,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:
@@ -3151,7 +3231,6 @@ VOID MlmeUpdateHtTxRates(
 
        auto_rate_cur_p = NULL;
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                pDesireHtPhy    = &pAd->StaCfg.DesiredHtPhyInfo;
                pActiveHtPhy    = &pAd->StaCfg.DesiredHtPhyInfo;
@@ -3269,7 +3348,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(
@@ -3314,7 +3392,6 @@ VOID BssTableInit(
        }
 }
 
-#ifdef DOT11_N_SUPPORT
 VOID BATableInit(
        IN PRTMP_ADAPTER pAd,
     IN BA_TABLE *Tab)
@@ -3334,7 +3411,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
@@ -3444,7 +3520,6 @@ VOID BssTableDeleteEntry(
        }
 }
 
-#ifdef DOT11_N_SUPPORT
 /*
        ========================================================================
        Routine Description:
@@ -3476,7 +3551,6 @@ VOID BATableDeleteORIEntry(
                NdisReleaseSpinLock(&pAd->BATabLock);
        }
 }
-#endif // DOT11_N_SUPPORT //
 
 /*! \brief
  *     \param
@@ -3593,7 +3667,7 @@ VOID BssEntrySet(
 
        pBss->AddHtInfoLen = 0;
        pBss->HtCapabilityLen = 0;
-#ifdef DOT11_N_SUPPORT
+
        if (HtCapabilityLen> 0)
        {
                pBss->HtCapabilityLen = HtCapabilityLen;
@@ -3613,7 +3687,6 @@ VOID BssEntrySet(
                                }
                }
        }
-#endif // DOT11_N_SUPPORT //
 
        BssCipherParse(pBss);
 
@@ -3631,7 +3704,6 @@ VOID BssEntrySet(
        else
                pBss->QbssLoad.bValid = FALSE;
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                PEID_STRUCT     pEid;
                USHORT          Length = 0;
@@ -3768,9 +3840,18 @@ 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);
+#ifdef RT30xx
+               }
+#endif
        }
 
        return Idx;
@@ -3804,7 +3885,7 @@ VOID BssTableSsidSort(
                        (SSID_EQUAL(Ssid, SsidLen, pInBss->Ssid, pInBss->SsidLen) || bIsHiddenApIncluded))
                {
                        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)))
@@ -3812,7 +3893,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
@@ -3832,9 +3912,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
@@ -3853,9 +3936,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
@@ -3885,7 +3971,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) &&
@@ -3905,7 +3990,6 @@ VOID BssTableSsidSort(
                                        }
                                }
                        }
-#endif // DOT11_N_SUPPORT //
 
                        // copy matching BSS from InTab to OutTab
                        NdisMoveMemory(pOutBss, pInBss, sizeof(BSS_ENTRY));
@@ -3916,8 +4000,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)))
@@ -3925,7 +4007,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
@@ -3981,7 +4062,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) &&
@@ -3994,7 +4074,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));
@@ -4140,10 +4219,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;
@@ -4230,7 +4315,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;
                                }
@@ -4260,10 +4344,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;
@@ -4357,7 +4447,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;
                                }
@@ -4434,8 +4523,7 @@ VOID MgtMacHeaderInit(
        pHdr80211->FC.ToDs = ToDs;
        COPY_MAC_ADDR(pHdr80211->Addr1, pDA);
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-               COPY_MAC_ADDR(pHdr80211->Addr2, pAd->CurrentAddress);
+       COPY_MAC_ADDR(pHdr80211->Addr2, pAd->CurrentAddress);
 
        COPY_MAC_ADDR(pHdr80211->Addr3, pBssid);
 }
@@ -4643,7 +4731,6 @@ BOOLEAN MlmeEnqueueForRecv(
                return FALSE;
        }
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                if (!MsgTypeSubst(pAd, pFrame, &Machine, &MsgType))
                {
@@ -4718,11 +4805,14 @@ BOOLEAN MlmeDequeue(
 VOID   MlmeRestartStateMachine(
        IN      PRTMP_ADAPTER   pAd)
 {
+#ifdef RT2860
        MLME_QUEUE_ELEM         *Elem = NULL;
+#endif
        BOOLEAN                         Cancelled;
 
        DBGPRINT(RT_DEBUG_TRACE, ("MlmeRestartStateMachine \n"));
 
+#ifdef RT2860
        NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
        if(pAd->Mlme.bRunning)
        {
@@ -4750,8 +4840,8 @@ VOID      MlmeRestartStateMachine(
                        DBGPRINT_ERR(("MlmeRestartStateMachine: MlmeQueue empty\n"));
                }
        }
+#endif /* RT2860 */
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // Cancel all timer events
                // Be careful to cancel new added timer
@@ -4770,7 +4860,6 @@ VOID      MlmeRestartStateMachine(
        // Resume MSDU which is turned off durning scan
        RTMPResumeMsduTransmission(pAd);
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // Set all state machines back IDLE
                pAd->Mlme.CntlMachine.CurrState    = CNTL_IDLE;
@@ -4781,10 +4870,12 @@ VOID    MlmeRestartStateMachine(
                pAd->Mlme.ActMachine.CurrState    = ACT_IDLE;
        }
 
+#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
@@ -5192,7 +5283,6 @@ VOID AsicUpdateAutoFallBackTable(
                                        }
                                }
                                break;
-#ifdef DOT11_N_SUPPORT
                        case 2:         //HT-MIX
                        case 3:         //HT-GF
                                {
@@ -5254,7 +5344,6 @@ VOID AsicUpdateAutoFallBackTable(
                                        }
                                }
                                break;
-#endif // DOT11_N_SUPPORT //
                }
 
                pNextTxRate = pCurrTxRate;
@@ -5296,7 +5385,6 @@ VOID      AsicUpdateProtect(
        UCHAR                   i;
        UINT32 MacReg = 0;
 
-#ifdef DOT11_N_SUPPORT
        if (!(pAd->CommonCfg.bHTProtect) && (OperationMode != 8))
        {
                return;
@@ -5310,19 +5398,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)
         {
@@ -5332,7 +5415,6 @@ VOID      AsicUpdateProtect(
         {
                        MacReg |= (pAd->CommonCfg.RtsThreshold << 8);
         }
-#endif
 
        RTMP_IO_WRITE32(pAd, TX_RTS_CFG, MacReg);
 
@@ -5371,7 +5453,6 @@ VOID      AsicUpdateProtect(
                Protect[1] = ProtCfg.word;
        }
 
-#ifdef DOT11_N_SUPPORT
        // Decide HT frame protection.
        if ((SetMask & ALLN_SETPROTECT) != 0)
        {
@@ -5502,7 +5583,6 @@ VOID      AsicUpdateProtect(
                                break;
                }
        }
-#endif // DOT11_N_SUPPORT //
 
        offset = CCK_PROT_CFG;
        for (i = 0;i < 6;i++)
@@ -5514,6 +5594,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:
@@ -5535,6 +5774,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)
@@ -5544,12 +5798,143 @@ VOID AsicSwitchChannel(
                        break;
                }
        }
+#endif
 
        if (index == MAX_NUM_OF_CHANNELS)
+               DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: Can't find the Channel#%d \n", Channel));
+
+#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
        {
-               DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: Cant find the Channel#%d \n", Channel));
-       }
+               /* modify by WY for Read RF Reg. error */
+               UCHAR RFValue;
+
+               for (index = 0; index < NUM_OF_3020_CHNL; index++)
+               {
+                       if (Channel == FreqItems3020[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))
+                               {
+                                       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;
+                       }
+               }
+
+               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));
+       }
+       else
+#endif // RT2870 //
        {
                RFRegTable = RF2850RegTable;
 
@@ -5803,6 +6188,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 */
 }
 
 /*
@@ -5872,11 +6304,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)
        {
@@ -6132,10 +6566,20 @@ 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
 }
 
 /*
@@ -6252,9 +6696,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
@@ -6310,7 +6752,6 @@ VOID AsicEnableBssSync(
 
        RTMP_IO_READ32(pAd, BCN_TIME_CFG, &csr.word);
 
-       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;
@@ -6349,6 +6790,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
@@ -6366,6 +6808,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
@@ -6514,7 +6974,6 @@ VOID AsicSetEdcaParm(
                Ac2Cfg.field.Cwmax = pEdcaParm->Cwmax[QID_AC_VI];
                Ac2Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_VI];
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
                        // Tuning for Wi-Fi WMM S06
                        if (pAd->CommonCfg.bWiFiTest &&
@@ -6522,13 +6981,21 @@ 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 //
                }
 
                Ac3Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_VO];
@@ -6572,8 +7039,7 @@ VOID AsicSetEdcaParm(
                CwminCsr.field.Cwmin1 = pEdcaParm->Cwmin[QID_AC_BK];
                CwminCsr.field.Cwmin2 = pEdcaParm->Cwmin[QID_AC_VI];
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       CwminCsr.field.Cwmin3 = pEdcaParm->Cwmin[QID_AC_VO] - 1; //for TGn wifi test
+               CwminCsr.field.Cwmin3 = pEdcaParm->Cwmin[QID_AC_VO] - 1; //for TGn wifi test
 
                RTMP_IO_WRITE32(pAd, WMM_CWMIN_CFG, CwminCsr.word);
 
@@ -6589,7 +7055,6 @@ VOID AsicSetEdcaParm(
                AifsnCsr.field.Aifsn1 = Ac1Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_BK];
                AifsnCsr.field.Aifsn2 = Ac2Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_VI];
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
                {
                        // Tuning for Wi-Fi WMM S06
                        if (pAd->CommonCfg.bWiFiTest &&
@@ -6597,17 +7062,24 @@ 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
                }
 
-               IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-                       AifsnCsr.field.Aifsn3 = Ac3Cfg.field.Aifsn - 1; //pEdcaParm->Aifsn[QID_AC_VO]; //for TGn wifi test
+               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);
 
@@ -6669,19 +7141,21 @@ VOID    AsicSetSlotTime(
 
        SlotTime = (bUseShortSlotTime)? 9 : 20;
 
-       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;
        }
 
@@ -6690,11 +7164,8 @@ VOID     AsicSetSlotTime(
        //
        // ToDo: Should consider capability with 11B
        //
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               if (pAd->StaCfg.BssType == BSS_ADHOC)
-                       SlotTime = 20;
-       }
+       if (pAd->StaCfg.BssType == BSS_ADHOC)
+               SlotTime = 20;
 
        RTMP_IO_READ32(pAd, BKOFF_SLOT_CFG, &RegValue);
        RegValue = RegValue & 0xFFFFFF00;
@@ -6725,7 +7196,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));
 //============================================================================================
@@ -6747,28 +7220,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
        }
 
 
@@ -6944,7 +7432,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"));
        //
@@ -6959,10 +7449,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;
 
        //
@@ -6970,19 +7465,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
        }
 
 
@@ -6992,6 +7497,7 @@ VOID AsicAddKeyEntry(
        //
        if (bTxKey)
        {
+#ifdef RT2860
                offset = MAC_IVEIV_TABLE_BASE + (WCID * HW_IVEIV_ENTRY_SIZE);
                //
                // Write IV
@@ -7015,6 +7521,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);
        }
 
@@ -7079,10 +7605,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;
@@ -7094,18 +7625,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]));
@@ -7164,9 +7705,10 @@ BOOLEAN AsicSendCommandToMcu(
                RTMPusecDelay(2);
        } while(i++ < 100);
 
-       if (i >= 100)
+       if (i > 100)
        {
                {
+#ifdef RT2860
                        UINT32 Data;
 
                        // Reset DMA
@@ -7188,9 +7730,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
@@ -7210,6 +7756,7 @@ BOOLEAN AsicSendCommandToMcu(
        return TRUE;
 }
 
+#ifdef RT2860
 BOOLEAN AsicCheckCommanOk(
        IN PRTMP_ADAPTER pAd,
        IN UCHAR                 Command)
@@ -7274,6 +7821,7 @@ BOOLEAN AsicCheckCommanOk(
 
        return FALSE;
 }
+#endif /* RT8260 */
 
 /*
        ========================================================================
@@ -7316,7 +7864,6 @@ VOID      RTMPCheckRates(
        NdisMoveMemory(SupRate, NewRate, NewRateLen);
 }
 
-#ifdef DOT11_N_SUPPORT
 BOOLEAN RTMPCheckChannel(
        IN PRTMP_ADAPTER pAd,
        IN UCHAR                CentralChannel,
@@ -7462,7 +8009,6 @@ BOOLEAN   RTMPCheckHt(
        COPY_AP_HTSETTINGS_FROM_BEACON(pAd, pHtCapability);
        return TRUE;
 }
-#endif // DOT11_N_SUPPORT //
 
 /*
        ========================================================================
@@ -7495,10 +8041,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
@@ -7512,13 +8056,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;
@@ -7628,6 +8170,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:
@@ -7646,37 +8238,66 @@ VOID AsicEvaluateRxAnt(
 {
        UCHAR   BBPR3 = 0;
 
-       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;
-       }
 
-       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)
+#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 |= (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);
-       }
-       RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
+               if (pAd->StaCfg.Psm == PWR_SAVE)
+                       return;
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       pAd->StaCfg.BBPR3 = BBPR3;
+               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
+       }
 
        if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
                )
@@ -7685,6 +8306,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);
@@ -7721,7 +8343,7 @@ VOID AsicRxAntEvalTimeout(
        UCHAR                   BBPR3 = 0;
        CHAR                    larger = -127, rssi0, rssi1, rssi2;
 
-       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)             ||
@@ -7779,8 +8401,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;
+
+       {
+               //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 */
 }
 
 
@@ -7847,7 +8572,6 @@ BOOLEAN RTMPCheckEntryEnableAutoRateSwitch(
 {
        BOOLEAN         result = TRUE;
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
        {
                // only associated STA counts
                if (pEntry && (pEntry->ValidAsCLI) && (pEntry->Sst == SST_ASSOC))
@@ -7865,11 +8589,8 @@ BOOLEAN RTMPCheckEntryEnableAutoRateSwitch(
 BOOLEAN RTMPAutoRateSwitchCheck(
        IN PRTMP_ADAPTER    pAd)
 {
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               if (pAd->StaCfg.bAutoTxRateSwitch)
-                       return TRUE;
-       }
+       if (pAd->StaCfg.bAutoTxRateSwitch)
+               return TRUE;
 
        return FALSE;
 }
@@ -7896,10 +8617,7 @@ UCHAR RTMPStaFixedTxMode(
 {
        UCHAR   tx_mode = FIXED_TXMODE_HT;
 
-       IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
-       {
-               tx_mode = (UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode;
-       }
+       tx_mode = (UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode;
 
        return tx_mode;
 }
@@ -7986,7 +8704,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;
@@ -7998,6 +8721,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)
                                {
@@ -8063,6 +8811,7 @@ VOID AsicStaBbpTuning(
        }
 }
 
+#ifdef RT2860
 VOID AsicResetFromDMABusy(
        IN PRTMP_ADAPTER pAd)
 {
@@ -8162,6 +8911,7 @@ VOID AsicResetPBF(
                DBGPRINT(RT_DEBUG_TRACE, ("<---  Asic HardReset PBF !!!! \n"));
        }
 }
+#endif /* RT2860 */
 
 VOID RTMPSetAGCInitValue(
        IN PRTMP_ADAPTER        pAd,
@@ -8181,13 +8931,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 //
        }
 
 }
@@ -8202,6 +8950,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)
@@ -8256,6 +9011,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)