Beispiel #1
0
void StaRateAdaptive87B(_adapter *padapter )
{
	struct mib_info *_sys_mib = &(padapter->_sys_mib);
	u64			CurrTxokCnt;
	u16			CurrRetryCnt;
	u16			CurrRetryRate;
	u8			CurrentOperaRate;
	u64			CurrRxokCnt;
	u8			bTryUp = _FALSE;
	u8			bTryDown = _FALSE;
	u8			TryUpTh = 1;
	u8			TryDownTh = 2;
	u32			TxThroughput;
	s32			CurrSignalStrength;
	u8			bUpdateInitialGain = _FALSE;

	CurrRetryCnt	= _sys_mib->CurrRetryCnt;
	CurrTxokCnt	= _sys_mib->NumTxOkTotal - _sys_mib->LastTxokCnt;
	CurrRxokCnt	= _sys_mib->NumRxOkTotal - _sys_mib->LastRxokCnt;
	CurrSignalStrength = _sys_mib->RecvSignalPower;
	TxThroughput = (u32)(_sys_mib->NumTxOkBytesTotal - _sys_mib->LastTxOKBytes);
	_sys_mib->LastTxOKBytes = _sys_mib->NumTxOkBytesTotal;
	CurrentOperaRate = (u8)((_sys_mib->rate/10) * 2);
	DEBUG_INFO(("CurrentOperaRate = %d \n", CurrentOperaRate));

	//2 Compute retry ratio.
	if (CurrTxokCnt>0)
	{
		CurrRetryRate = (u16)((int)CurrRetryCnt*100/(int)CurrTxokCnt);
	}
	else
	{ // It may be serious retry. To distinguish serious retry or no packets modified by Bruce
		CurrRetryRate = (u16)((int)CurrRetryCnt*100/1);
	}

	//
	// For debug information.
	//
	DEBUG_INFO(("\n(1) LastRetryRate: %d \n",(int)_sys_mib->LastRetryRate));
	DEBUG_INFO(("(2) RetryCnt = %d  \n", (int)CurrRetryCnt));
	DEBUG_INFO(("(3) TxokCnt = %d \n", (int)CurrTxokCnt));
	DEBUG_INFO(("(4) CurrRetryRate = %d \n", (int)CurrRetryRate));
	DEBUG_INFO(("(5) SignalStrength = %d \n",(int)_sys_mib->RecvSignalPower));
	DEBUG_INFO(("(6) ReceiveSignalPower = %d \n",(int)CurrSignalStrength));

	_sys_mib->LastRetryCnt	= _sys_mib->CurrRetryCnt;
	_sys_mib->LastTxokCnt	= _sys_mib->NumTxOkTotal;
	_sys_mib->LastRxokCnt	= _sys_mib->NumRxOkTotal;
	_sys_mib->CurrRetryCnt	= 0;

	//2No Tx packets, return to init_rate or not?
	if (CurrRetryRate==0 && CurrTxokCnt == 0)
	{	
		//
		// 2007.04.09, by Roger.
		//After 4.5 seconds in this condition, we try to raise rate.
		//
		_sys_mib->TryupingCountNoData++;		
		
		if (_sys_mib->TryupingCountNoData>15)
		{
			_sys_mib->TryupingCountNoData = 0;
		 	CurrentOperaRate = GetUpgradeTxRate(padapter, CurrentOperaRate);
			// Reset Fail Record
			_sys_mib->LastFailTxRate = 0;
			_sys_mib->LastFailTxRateSS = -200;
			_sys_mib->FailTxRateCount = 0;
		}
		goto SetInitialGain;
	}
        else
	{
		_sys_mib->TryupingCountNoData=0; //Reset trying up times.
	}

	//
	// Restructure rate adaptive as the following main stages:
	// (1) Add retry threshold in 54M upgrading condition with signal strength.
	// (2) Add the mechanism to degrade to CCK rate according to signal strength 
	//		and retry rate.
	// (3) Remove all Initial Gain Updates over OFDM rate. To avoid the complicated 
	//		situation, Initial Gain Update is upon on DIG mechanism except CCK rate.
	// (4) Add the mehanism of trying to upgrade tx rate.
	// (5) Record the information of upping tx rate to avoid trying upping tx rate constantly.
	// By Bruce, 2007-06-05.
	//	
	//

	// 11Mbps or 36Mbps
	// Check more times in these rate(key rates).
	//
	if(CurrentOperaRate == 22 || CurrentOperaRate == 72)
	{ 
		TryUpTh += 9;
	}
	//
	// Let these rates down more difficult.
	//
	if(MgntIsCckRate(CurrentOperaRate) || CurrentOperaRate == 36)
	{
			TryDownTh += 1;
	}
	//1 Adjust Rate.
	if (_sys_mib->bTryuping == _TRUE)
	{	
		//2 For Test Upgrading mechanism
		// Note:
		// 	Sometimes the throughput is upon on the capability bwtween the AP and NIC,
		// 	thus the low data rate does not improve the performance.
		// 	We randomly upgrade the data rate and check if the retry rate is improved.
		
		// Upgrading rate did not improve the retry rate, fallback to the original rate.
		if ( (CurrRetryRate > 25) && TxThroughput < _sys_mib->LastTxThroughput )
		{
			//Not necessary raising rate, fall back rate.
			bTryDown = _TRUE;
		}
		else
		{
			_sys_mib->bTryuping = _FALSE;
		}
	}	
	else if (CurrSignalStrength > -51 && (CurrRetryRate < 100))
	{
		//2For High Power
		//
		// Added by Roger, 2007.04.09.
		// Return to highest data rate, if signal strength is good enough.
		// SignalStrength threshold(-50dbm) is for RTL8186.
		// Revise SignalStrength threshold to -51dbm.	
		//
		// Also need to check retry rate for safety, by Bruce, 2007-06-05.
		if(CurrentOperaRate != 108)
		{
			bTryUp = _TRUE;
			// Upgrade Tx Rate directly.
			_sys_mib->TryupingCount += TryUpTh;			
		}
	}
	else if(CurrTxokCnt< 100 && CurrRetryRate >= 600) 
	{
		//2 For Serious Retry
		//
		// Traffic is not busy but our Tx retry is serious. 
		//
		bTryDown = _TRUE;
		// Let Rate Mechanism to degrade tx rate directly.
		_sys_mib->TryDownCountLowData += TryDownTh;
		DEBUG_INFO(("RA: Tx Retry is serious. Degrade Tx Rate to %d directly...\n", CurrentOperaRate));
	}
	else if ( CurrentOperaRate == 108 )
	{
		//2For 54Mbps
		// if ( (CurrRetryRate>38)&&(pHalData->LastRetryRate>35)) 
		if ( (CurrRetryRate>33)&&(_sys_mib->LastRetryRate>32))
		{
			//(30,25) for cable link threshold. (38,35) for air link.
			//Down to rate 48Mbps.
			bTryDown = _TRUE;
		}
	}
	else if ( CurrentOperaRate == 96 )
	{
		//2For 48Mbps
		// if ( ((CurrRetryRate>73) && (pHalData->LastRetryRate>72)) && IncludedInSupportedRates(Adapter, 72) )
		if ( ((CurrRetryRate>48) && (_sys_mib->LastRetryRate>47)))
		{	
			//(73, 72) for temp used.
			//(25, 23) for cable link, (60,59) for air link.
			//CurrRetryRate plus 25 and 26 respectively for air link.
			//Down to rate 36Mbps.
			bTryDown = _TRUE;
		}
		else if ( (CurrRetryRate<8) && (_sys_mib->LastRetryRate<8) ) //TO DO: need to consider (RSSI)
		{
			bTryUp = _TRUE;
		}
	}
	else if ( CurrentOperaRate == 72 )
	{
		//2For 36Mbps
		//if ( (CurrRetryRate>97) && (pHalData->LastRetryRate>97)) 
		if ( (CurrRetryRate>55) && (_sys_mib->LastRetryRate>54)) 
		{	
			//(30,25) for cable link threshold respectively. (103,10) for air link respectively.
			//CurrRetryRate plus 65 and 69 respectively for air link threshold.
			//Down to rate 24Mbps.
			bTryDown = _TRUE;
		}
		// else if ( (CurrRetryRate<20) &&  (pHalData->LastRetryRate<20) && IncludedInSupportedRates(Adapter, 96) )//&& (device->LastRetryRate<15) ) //TO DO: need to consider (RSSI)
		else if ( (CurrRetryRate<15) &&  (_sys_mib->LastRetryRate<16))//&& (device->LastRetryRate<15) ) //TO DO: need to consider (RSSI)
		{
			bTryUp = _TRUE;
		}
	}
	else if ( CurrentOperaRate == 48 )
	{
		//2For 24Mbps
		// if ( ((CurrRetryRate>119) && (pHalData->LastRetryRate>119) && IncludedInSupportedRates(Adapter, 36)))
		if ( ((CurrRetryRate>63) && (_sys_mib->LastRetryRate>62)))
		{	
			//(15,15) for cable link threshold respectively. (119, 119) for air link threshold.
			//Plus 84 for air link threshold.
			//Down to rate 18Mbps.
			bTryDown = _TRUE;
		}
  		// else if ( (CurrRetryRate<14) && (pHalData->LastRetryRate<15) && IncludedInSupportedRates(Adapter, 72)) //TO DO: need to consider (RSSI)
  		else if ( (CurrRetryRate<20) && (_sys_mib->LastRetryRate<21)) //TO DO: need to consider (RSSI)
		{	
			bTryUp = _TRUE;	
		}
	}
	else if ( CurrentOperaRate == 36 )
	{
		//2For 18Mbps
		if ( ((CurrRetryRate>109) && (_sys_mib->LastRetryRate>109)))
		{
			//(99,99) for cable link, (109,109) for air link.
			//Down to rate 11Mbps.
			bTryDown = _TRUE;
		}
		// else if ( (CurrRetryRate<15) && (pHalData->LastRetryRate<16) && IncludedInSupportedRates(Adapter, 48)) //TO DO: need to consider (RSSI)	
		else if ( (CurrRetryRate<25) && (_sys_mib->LastRetryRate<26)) //TO DO: need to consider (RSSI)
		{	
			bTryUp = _TRUE;	
		}
	}
	else if ( CurrentOperaRate == 22 )
	{
		//2For 11Mbps
		// if (CurrRetryRate>299 && IncludedInSupportedRates(Adapter, 11))
		if (CurrRetryRate>95)
		{
			bTryDown = _TRUE;
		}
		else if (CurrRetryRate<55)//&& (device->LastRetryRate<55) ) //TO DO: need to consider (RSSI)
		{
			bTryUp = _TRUE;
		}
	}	
	else if ( CurrentOperaRate == 11 )
	{
		//2For 5.5Mbps
		// if (CurrRetryRate>159 && IncludedInSupportedRates(Adapter, 4) ) 
		if (CurrRetryRate>149) 
		{	
			bTryDown = _TRUE;			
		}
		// else if ( (CurrRetryRate<30) && (pHalData->LastRetryRate<30) && IncludedInSupportedRates(Adapter, 22) )
		else if ( (CurrRetryRate<60) && (_sys_mib->LastRetryRate < 65))
		{
			bTryUp = _TRUE;
		}		
	}
	else if ( CurrentOperaRate == 4 )
	{
		//2For 2 Mbps
		if((CurrRetryRate>99) && (_sys_mib->LastRetryRate>99))
		{
			bTryDown = _TRUE;			
	}
		// else if ( (CurrRetryRate<50) && (pHalData->LastRetryRate<65) && IncludedInSupportedRates(Adapter, 11) )
		else if ( (CurrRetryRate < 65) && (_sys_mib->LastRetryRate < 70))
	{
			bTryUp = _TRUE;
		}
	}
	else if ( CurrentOperaRate == 2 )
		{
		//2For 1 Mbps
		// if ( (CurrRetryRate<50) && (pHalData->LastRetryRate<65) && IncludedInSupportedRates(Adapter, 4))
		if ( (CurrRetryRate<70) && (_sys_mib->LastRetryRate<75))
			{
			bTryUp = _TRUE;
		}
	}

	//1 Test Upgrading Tx Rate
	// Sometimes the cause of the low throughput (high retry rate) is the compatibility between the AP and NIC.
	// To test if the upper rate may cause lower retry rate, this mechanism randomly occurs to test upgrading tx rate.
	if(!bTryUp && !bTryDown && (_sys_mib->TryupingCount == 0) && (_sys_mib->TryDownCountLowData == 0)
		&& CurrentOperaRate != 108 && _sys_mib->FailTxRateCount < 2)
	{
		if(jiffies% (CurrRetryRate + 101) == 0)
		{
			bTryUp = _TRUE;	
			_sys_mib->bTryuping = _TRUE;
			DEBUG_INFO(("StaRateAdaptive87B(): Randomly try upgrading...\n"));
		}
	}

	//1 Rate Mechanism
	if(bTryUp)
	{
		_sys_mib->TryupingCount++;
		_sys_mib->TryDownCountLowData = 0;
			
		//
		// Check more times if we need to upgrade indeed.
		// Because the largest value of pHalData->TryupingCount is 0xFFFF and 
		// the largest value of pHalData->FailTxRateCount is 0x14,
		// this condition will be satisfied at most every 2 min.
		//
		if((_sys_mib->TryupingCount > (TryUpTh + _sys_mib->FailTxRateCount * _sys_mib->FailTxRateCount)) ||
			(CurrSignalStrength > _sys_mib->LastFailTxRateSS) || _sys_mib->bTryuping)
		{
			_sys_mib->TryupingCount = 0;
			// 
			// When transfering from CCK to OFDM, DIG is an important issue.
			//
			if(CurrentOperaRate == 22)
				bUpdateInitialGain = _TRUE;
			// (1)To avoid upgrade frequently to the fail tx rate, add the FailTxRateCount into the threshold.
			// (2)If the signal strength is increased, it may be able to upgrade.
			CurrentOperaRate = GetUpgradeTxRate(padapter, CurrentOperaRate);
			DEBUG_INFO(("StaRateAdaptive87B(): Upgrade Tx Rate to %d\n", CurrentOperaRate));
				
			// Update Fail Tx rate and count.
			if(_sys_mib->LastFailTxRate != CurrentOperaRate)
			{
				_sys_mib->LastFailTxRate = CurrentOperaRate;
				_sys_mib->FailTxRateCount = 0;
				_sys_mib->LastFailTxRateSS = -200; // Set lowest power.
			}
		}
	}
	else
	{	
		if(_sys_mib->TryupingCount > 0)
			_sys_mib->TryupingCount --;
	}
	
	if(bTryDown)
	{
		_sys_mib->TryDownCountLowData++;
		_sys_mib->TryupingCount = 0;
				
		
		//Check if Tx rate can be degraded or Test trying upgrading should fallback.
		if(_sys_mib->TryDownCountLowData > TryDownTh || _sys_mib->bTryuping)
		{
			_sys_mib->TryDownCountLowData = 0;
			_sys_mib->bTryuping = _FALSE;
			// Update fail information.
			if(_sys_mib->LastFailTxRate == CurrentOperaRate)
			{
				_sys_mib->FailTxRateCount ++;
				// Record the Tx fail rate signal strength.
				if(CurrSignalStrength > _sys_mib->LastFailTxRateSS)
				{
					_sys_mib->LastFailTxRateSS = CurrSignalStrength;
				}
			}
			else
			{
				_sys_mib->LastFailTxRate = CurrentOperaRate;
				_sys_mib->FailTxRateCount = 1;
				_sys_mib->LastFailTxRateSS = CurrSignalStrength;
			}
			CurrentOperaRate = GetDegradeTxRate(padapter, CurrentOperaRate);
			//
			// When it is CCK rate, it may need to update initial gain to receive lower power packets.
			//
			if(MgntIsCckRate(CurrentOperaRate))
			{
				bUpdateInitialGain = _TRUE;
			}
			DEBUG_INFO(("StaRateAdaptive87B(): Degrade Tx Rate to %d\n", CurrentOperaRate));
		}
	}
	else
	{
		if(_sys_mib->TryDownCountLowData > 0)
			_sys_mib->TryDownCountLowData --;
	}
			
	// Keep the Tx fail rate count to equal to 0x15 at most.
	// Reduce the fail count at least to 10 sec if tx rate is tending stable.
	if(_sys_mib->FailTxRateCount >= 0x15 || 
		(!bTryUp && !bTryDown && _sys_mib->TryDownCountLowData == 0 && _sys_mib->TryupingCount && _sys_mib->FailTxRateCount > 0x6))
	{
		_sys_mib->FailTxRateCount --;
	}

	//
	// We need update initial gain when we set tx rate "from OFDM to CCK" or
	// "from CCK to OFDM". 
	//	
SetInitialGain:
	if(bUpdateInitialGain)
	{
		if(MgntIsCckRate(CurrentOperaRate)) // CCK
		{
			if(_sys_mib->InitialGain > _sys_mib->RegBModeGainStage)
			{
				if(CurrSignalStrength < -85) // Low power, OFDM [0x17] = 26.
				{
					_sys_mib->InitialGain = _sys_mib->RegBModeGainStage;
				}
				else if(_sys_mib->InitialGain > _sys_mib->RegBModeGainStage + 1)
				{
					_sys_mib->InitialGain -= 2;
				}
				else
				{
					_sys_mib->InitialGain --;
				}
				DEBUG_INFO(("StaRateAdaptive87B(): update init_gain to index %d for date rate %d\n",_sys_mib->InitialGain, CurrentOperaRate));
				UpdateInitialGain(padapter);
			}
		}
		else // OFDM
		{			
			if(_sys_mib->InitialGain < 4)
			{
				_sys_mib->InitialGain ++;
				DEBUG_INFO(("StaRateAdaptive87B(): update init_gain to index %d for date rate %d\n",_sys_mib->InitialGain, CurrentOperaRate));			
				UpdateInitialGain(padapter);
			}					
		}
	}

	//Record the related info
	_sys_mib->rate = ((int)CurrentOperaRate/2) *10;
	_sys_mib->LastRetryRate = CurrRetryRate;
	_sys_mib->LastTxThroughput = TxThroughput;

}
Beispiel #2
0
static void StaRateAdaptive87SE(struct net_device *dev)
{
    struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
    unsigned long	CurrTxokCnt;
    u16		CurrRetryCnt;
    u16		CurrRetryRate;
    unsigned long	CurrRxokCnt;
    bool		bTryUp = false;
    bool		bTryDown = false;
    u8		TryUpTh = 1;
    u8		TryDownTh = 2;
    u32		TxThroughput;
    long		CurrSignalStrength;
    bool		bUpdateInitialGain = false;
    u8		u1bOfdm = 0, u1bCck = 0;
    char		OfdmTxPwrIdx, CckTxPwrIdx;

    priv->RateAdaptivePeriod = RATE_ADAPTIVE_TIMER_PERIOD;


    CurrRetryCnt	= priv->CurrRetryCnt;
    CurrTxokCnt	= priv->NumTxOkTotal - priv->LastTxokCnt;
    CurrRxokCnt	= priv->ieee80211->NumRxOkTotal - priv->LastRxokCnt;
    CurrSignalStrength = priv->Stats_RecvSignalPower;
    TxThroughput = (u32)(priv->NumTxOkBytesTotal - priv->LastTxOKBytes);
    priv->LastTxOKBytes = priv->NumTxOkBytesTotal;
    priv->CurrentOperaRate = priv->ieee80211->rate / 5;
    /* 2 Compute retry ratio. */
    if (CurrTxokCnt > 0) {
        CurrRetryRate = (u16)(CurrRetryCnt * 100 / CurrTxokCnt);
    } else {
        /* It may be serious retry. To distinguish serious retry or no packets modified by Bruce */
        CurrRetryRate = (u16)(CurrRetryCnt * 100 / 1);
    }

    priv->LastRetryCnt = priv->CurrRetryCnt;
    priv->LastTxokCnt = priv->NumTxOkTotal;
    priv->LastRxokCnt = priv->ieee80211->NumRxOkTotal;
    priv->CurrRetryCnt = 0;

    /* 2No Tx packets, return to init_rate or not? */
    if (CurrRetryRate == 0 && CurrTxokCnt == 0) {
        /*
         * After 9 (30*300ms) seconds in this condition, we try to raise rate.
         */
        priv->TryupingCountNoData++;

        /* [TRC Dell Lab] Extend raised period from 4.5sec to 9sec, Isaiah 2008-02-15 18:00 */
        if (priv->TryupingCountNoData > 30) {
            priv->TryupingCountNoData = 0;
            priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
            /* Reset Fail Record */
            priv->LastFailTxRate = 0;
            priv->LastFailTxRateSS = -200;
            priv->FailTxRateCount = 0;
        }
        goto SetInitialGain;
    } else {
        priv->TryupingCountNoData = 0; /*Reset trying up times. */
    }


    /*
     * For Netgear case, I comment out the following signal strength estimation,
     * which can results in lower rate to transmit when sample is NOT enough (e.g. PING request).
     *
     * Restructure rate adaptive as the following main stages:
     * (1) Add retry threshold in 54M upgrading condition with signal strength.
     * (2) Add the mechanism to degrade to CCK rate according to signal strength
     *		and retry rate.
     * (3) Remove all Initial Gain Updates over OFDM rate. To avoid the complicated
     *		situation, Initial Gain Update is upon on DIG mechanism except CCK rate.
     * (4) Add the mechanism of trying to upgrade tx rate.
     * (5) Record the information of upping tx rate to avoid trying upping tx rate constantly.
     *
     */

    /*
     * 11Mbps or 36Mbps
     * Check more times in these rate(key rates).
     */
    if (priv->CurrentOperaRate == 22 || priv->CurrentOperaRate == 72)
        TryUpTh += 9;
    /*
     * Let these rates down more difficult.
     */
    if (MgntIsCckRate(priv->CurrentOperaRate) || priv->CurrentOperaRate == 36)
        TryDownTh += 1;

    /* 1 Adjust Rate. */
    if (priv->bTryuping == true) {
        /* 2 For Test Upgrading mechanism
         * Note:
         *	Sometimes the throughput is upon on the capability between the AP and NIC,
         *	thus the low data rate does not improve the performance.
         *	We randomly upgrade the data rate and check if the retry rate is improved.
         */

        /* Upgrading rate did not improve the retry rate, fallback to the original rate. */
        if ((CurrRetryRate > 25) && TxThroughput < priv->LastTxThroughput) {
            /*Not necessary raising rate, fall back rate. */
            bTryDown = true;
        } else {
            priv->bTryuping = false;
        }
    } else if (CurrSignalStrength > -47 && (CurrRetryRate < 50)) {
        /*
         * 2For High Power
         *
         * Return to highest data rate, if signal strength is good enough.
         * SignalStrength threshold(-50dbm) is for RTL8186.
         * Revise SignalStrength threshold to -51dbm.
         */
        /* Also need to check retry rate for safety, by Bruce, 2007-06-05. */
        if (priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate) {
            bTryUp = true;
            /* Upgrade Tx Rate directly. */
            priv->TryupingCount += TryUpTh;
        }

    } else if (CurrTxokCnt > 9 && CurrTxokCnt < 100 && CurrRetryRate >= 600) {
        /*
         *2 For Serious Retry
         *
         * Traffic is not busy but our Tx retry is serious.
         */
        bTryDown = true;
        /* Let Rate Mechanism to degrade tx rate directly. */
        priv->TryDownCountLowData += TryDownTh;
    } else if (priv->CurrentOperaRate == 108) {
        /* 2For 54Mbps */
        /* Air Link */
        if ((CurrRetryRate > 26) && (priv->LastRetryRate > 25)) {
            bTryDown = true;
        }
        /* Cable Link */
        else if ((CurrRetryRate > 17) && (priv->LastRetryRate > 16) && (CurrSignalStrength > -72)) {
            bTryDown = true;
        }

        if (bTryDown && (CurrSignalStrength < -75)) /* cable link */
            priv->TryDownCountLowData += TryDownTh;
    } else if (priv->CurrentOperaRate == 96) {
        /* 2For 48Mbps */
        /* Air Link */
        if (((CurrRetryRate > 48) && (priv->LastRetryRate > 47))) {
            bTryDown = true;
        } else if (((CurrRetryRate > 21) && (priv->LastRetryRate > 20)) && (CurrSignalStrength > -74)) { /* Cable Link */
            /* Down to rate 36Mbps. */
            bTryDown = true;
        } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
            bTryDown = true;
            priv->TryDownCountLowData += TryDownTh;
        } else if ((CurrRetryRate < 8) && (priv->LastRetryRate < 8)) { /* TO DO: need to consider (RSSI) */
            bTryUp = true;
        }

        if (bTryDown && (CurrSignalStrength < -75)) {
            priv->TryDownCountLowData += TryDownTh;
        }
    } else if (priv->CurrentOperaRate == 72) {
        /* 2For 36Mbps */
        if ((CurrRetryRate > 43) && (priv->LastRetryRate > 41)) {
            /* Down to rate 24Mbps. */
            bTryDown = true;
        } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
            bTryDown = true;
            priv->TryDownCountLowData += TryDownTh;
        } else if ((CurrRetryRate < 15) &&  (priv->LastRetryRate < 16)) { /* TO DO: need to consider (RSSI) */
            bTryUp = true;
        }

        if (bTryDown && (CurrSignalStrength < -80))
            priv->TryDownCountLowData += TryDownTh;

    } else if (priv->CurrentOperaRate == 48) {
        /* 2For 24Mbps */
        /* Air Link */
        if (((CurrRetryRate > 63) && (priv->LastRetryRate > 62))) {
            bTryDown = true;
        } else if (((CurrRetryRate > 33) && (priv->LastRetryRate > 32)) && (CurrSignalStrength > -82)) { /* Cable Link */
            bTryDown = true;
        } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
            bTryDown = true;
            priv->TryDownCountLowData += TryDownTh;
        } else if ((CurrRetryRate < 20) && (priv->LastRetryRate < 21)) { /* TO DO: need to consider (RSSI) */
            bTryUp = true;
        }

        if (bTryDown && (CurrSignalStrength < -82))
            priv->TryDownCountLowData += TryDownTh;

    } else if (priv->CurrentOperaRate == 36) {
        if (((CurrRetryRate > 85) && (priv->LastRetryRate > 86))) {
            bTryDown = true;
        } else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
            bTryDown = true;
            priv->TryDownCountLowData += TryDownTh;
        } else if ((CurrRetryRate < 22) && (priv->LastRetryRate < 23)) { /* TO DO: need to consider (RSSI) */
            bTryUp = true;
        }
    } else if (priv->CurrentOperaRate == 22) {
        /* 2For 11Mbps */
        if (CurrRetryRate > 95) {
            bTryDown = true;
        } else if ((CurrRetryRate < 29) && (priv->LastRetryRate < 30)) { /*TO DO: need to consider (RSSI) */
            bTryUp = true;
        }
    } else if (priv->CurrentOperaRate == 11) {
        /* 2For 5.5Mbps */
        if (CurrRetryRate > 149) {
            bTryDown = true;
        } else if ((CurrRetryRate < 60) && (priv->LastRetryRate < 65)) {
            bTryUp = true;
        }
    } else if (priv->CurrentOperaRate == 4) {
        /* 2For 2 Mbps */
        if ((CurrRetryRate > 99) && (priv->LastRetryRate > 99)) {
            bTryDown = true;
        } else if ((CurrRetryRate < 65) && (priv->LastRetryRate < 70)) {
            bTryUp = true;
        }
    } else if (priv->CurrentOperaRate == 2) {
        /* 2For 1 Mbps */
        if ((CurrRetryRate < 70) && (priv->LastRetryRate < 75)) {
            bTryUp = true;
        }
    }

    if (bTryUp && bTryDown)
        printk("StaRateAdaptive87B(): Tx Rate tried upping and downing simultaneously!\n");

    /* 1 Test Upgrading Tx Rate
     * Sometimes the cause of the low throughput (high retry rate) is the compatibility between the AP and NIC.
     * To test if the upper rate may cause lower retry rate, this mechanism randomly occurs to test upgrading tx rate.
     */
    if (!bTryUp && !bTryDown && (priv->TryupingCount == 0) && (priv->TryDownCountLowData == 0)
            && priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate && priv->FailTxRateCount < 2) {
        if (jiffies % (CurrRetryRate + 101) == 0) {
            bTryUp = true;
            priv->bTryuping = true;
        }
    }

    /* 1 Rate Mechanism */
    if (bTryUp) {
        priv->TryupingCount++;
        priv->TryDownCountLowData = 0;

        /*
         * Check more times if we need to upgrade indeed.
         * Because the largest value of pHalData->TryupingCount is 0xFFFF and
         * the largest value of pHalData->FailTxRateCount is 0x14,
         * this condition will be satisfied at most every 2 min.
         */

        if ((priv->TryupingCount > (TryUpTh + priv->FailTxRateCount * priv->FailTxRateCount)) ||
                (CurrSignalStrength > priv->LastFailTxRateSS) || priv->bTryuping) {
            priv->TryupingCount = 0;
            /*
             * When transferring from CCK to OFDM, DIG is an important issue.
             */
            if (priv->CurrentOperaRate == 22)
                bUpdateInitialGain = true;

            /*
             * The difference in throughput between 48Mbps and 36Mbps is 8M.
             * So, we must be careful in this rate scale. Isaiah 2008-02-15.
             */
            if (((priv->CurrentOperaRate == 72) || (priv->CurrentOperaRate == 48) || (priv->CurrentOperaRate == 36)) &&
                    (priv->FailTxRateCount > 2))
                priv->RateAdaptivePeriod = (RATE_ADAPTIVE_TIMER_PERIOD / 2);

            /* (1)To avoid upgrade frequently to the fail tx rate, add the FailTxRateCount into the threshold. */
            /* (2)If the signal strength is increased, it may be able to upgrade. */

            priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);

            if (priv->CurrentOperaRate == 36) {
                priv->bUpdateARFR = true;
                write_nic_word(dev, ARFR, 0x0F8F); /* bypass 12/9/6 */
            } else if (priv->bUpdateARFR) {
                priv->bUpdateARFR = false;
                write_nic_word(dev, ARFR, 0x0FFF); /* set 1M ~ 54Mbps. */
            }

            /* Update Fail Tx rate and count. */
            if (priv->LastFailTxRate != priv->CurrentOperaRate) {
                priv->LastFailTxRate = priv->CurrentOperaRate;
                priv->FailTxRateCount = 0;
                priv->LastFailTxRateSS = -200; /* Set lowest power. */
            }
        }
    } else {
        if (priv->TryupingCount > 0)
            priv->TryupingCount--;
    }

    if (bTryDown) {
        priv->TryDownCountLowData++;
        priv->TryupingCount = 0;

        /* Check if Tx rate can be degraded or Test trying upgrading should fallback. */
        if (priv->TryDownCountLowData > TryDownTh || priv->bTryuping) {
            priv->TryDownCountLowData = 0;
            priv->bTryuping = false;
            /* Update fail information. */
            if (priv->LastFailTxRate == priv->CurrentOperaRate) {
                priv->FailTxRateCount++;
                /* Record the Tx fail rate signal strength. */
                if (CurrSignalStrength > priv->LastFailTxRateSS)
                    priv->LastFailTxRateSS = CurrSignalStrength;
            } else {
                priv->LastFailTxRate = priv->CurrentOperaRate;
                priv->FailTxRateCount = 1;
                priv->LastFailTxRateSS = CurrSignalStrength;
            }
            priv->CurrentOperaRate = GetDegradeTxRate(dev, priv->CurrentOperaRate);

            /* Reduce chariot training time at weak signal strength situation. SD3 ED demand. */
            if ((CurrSignalStrength < -80) && (priv->CurrentOperaRate > 72)) {
                priv->CurrentOperaRate = 72;
            }

            if (priv->CurrentOperaRate == 36) {
                priv->bUpdateARFR = true;
                write_nic_word(dev, ARFR, 0x0F8F); /* bypass 12/9/6 */
            } else if (priv->bUpdateARFR) {
                priv->bUpdateARFR = false;
                write_nic_word(dev, ARFR, 0x0FFF); /* set 1M ~ 54Mbps. */
            }

            /*
             * When it is CCK rate, it may need to update initial gain to receive lower power packets.
             */
            if (MgntIsCckRate(priv->CurrentOperaRate)) {
                bUpdateInitialGain = true;
            }
        }
    } else {
        if (priv->TryDownCountLowData > 0)
            priv->TryDownCountLowData--;
    }

    /*
     * Keep the Tx fail rate count to equal to 0x15 at most.
     * Reduce the fail count at least to 10 sec if tx rate is tending stable.
     */
    if (priv->FailTxRateCount >= 0x15 ||
            (!bTryUp && !bTryDown && priv->TryDownCountLowData == 0 && priv->TryupingCount && priv->FailTxRateCount > 0x6)) {
        priv->FailTxRateCount--;
    }


    OfdmTxPwrIdx  = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
    CckTxPwrIdx  = priv->chtxpwr[priv->ieee80211->current_network.channel];

    /* Mac0x9e increase 2 level in 36M~18M situation */
    if ((priv->CurrentOperaRate < 96) && (priv->CurrentOperaRate > 22)) {
        u1bCck = read_nic_byte(dev, CCK_TXAGC);
        u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);

        /* case 1: Never enter High power */
        if (u1bCck == CckTxPwrIdx) {
            if (u1bOfdm != (OfdmTxPwrIdx + 2)) {
                priv->bEnhanceTxPwr = true;
                u1bOfdm = ((u1bOfdm + 2) > 35) ? 35 : (u1bOfdm + 2);
                write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
            }
        } else if (u1bCck < CckTxPwrIdx) {
            /* case 2: enter high power */
            if (!priv->bEnhanceTxPwr) {
                priv->bEnhanceTxPwr = true;
                u1bOfdm = ((u1bOfdm + 2) > 35) ? 35 : (u1bOfdm + 2);
                write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
            }
        }
    } else if (priv->bEnhanceTxPwr) {  /* 54/48/11/5.5/2/1 */
        u1bCck = read_nic_byte(dev, CCK_TXAGC);
        u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);

        /* case 1: Never enter High power */
        if (u1bCck == CckTxPwrIdx) {
            priv->bEnhanceTxPwr = false;
            write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
        }
        /* case 2: enter high power */
        else if (u1bCck < CckTxPwrIdx) {
            priv->bEnhanceTxPwr = false;
            u1bOfdm = ((u1bOfdm - 2) > 0) ? (u1bOfdm - 2) : 0;
            write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
        }
    }

    /*
     * We need update initial gain when we set tx rate "from OFDM to CCK" or
     * "from CCK to OFDM".
     */
SetInitialGain:
    if (bUpdateInitialGain) {
        if (MgntIsCckRate(priv->CurrentOperaRate)) { /* CCK */
            if (priv->InitialGain > priv->RegBModeGainStage) {
                priv->InitialGainBackUp = priv->InitialGain;

                if (CurrSignalStrength < -85) /* Low power, OFDM [0x17] = 26. */
                    /* SD3 SYs suggest that CurrSignalStrength < -65, ofdm 0x17=26. */
                    priv->InitialGain = priv->RegBModeGainStage;

                else if (priv->InitialGain > priv->RegBModeGainStage + 1)
                    priv->InitialGain -= 2;

                else
                    priv->InitialGain--;

                printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n", priv->InitialGain, priv->CurrentOperaRate);
                UpdateInitialGain(dev);
            }
        } else { /* OFDM */
            if (priv->InitialGain < 4) {
                priv->InitialGainBackUp = priv->InitialGain;

                priv->InitialGain++;
                printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n", priv->InitialGain, priv->CurrentOperaRate);
                UpdateInitialGain(dev);
            }
        }
    }

    /* Record the related info */
    priv->LastRetryRate = CurrRetryRate;
    priv->LastTxThroughput = TxThroughput;
    priv->ieee80211->rate = priv->CurrentOperaRate * 5;
}
void StaRateAdaptive87SE(struct net_device *dev)
{
	struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
	unsigned long	CurrTxokCnt;
	u16		CurrRetryCnt;
	u16		CurrRetryRate;
	unsigned long	CurrRxokCnt;
	bool		bTryUp = false;
	bool		bTryDown = false;
	u8		TryUpTh = 1;
	u8		TryDownTh = 2;
	u32		TxThroughput;
	long		CurrSignalStrength;
	bool		bUpdateInitialGain = false;
	u8		u1bOfdm = 0, u1bCck = 0;
	char		OfdmTxPwrIdx, CckTxPwrIdx;

	priv->RateAdaptivePeriod = RATE_ADAPTIVE_TIMER_PERIOD;


	CurrRetryCnt	= priv->CurrRetryCnt;
	CurrTxokCnt	= priv->NumTxOkTotal - priv->LastTxokCnt;
	CurrRxokCnt	= priv->ieee80211->NumRxOkTotal - priv->LastRxokCnt;
	CurrSignalStrength = priv->Stats_RecvSignalPower;
	TxThroughput = (u32)(priv->NumTxOkBytesTotal - priv->LastTxOKBytes);
	priv->LastTxOKBytes = priv->NumTxOkBytesTotal;
	priv->CurrentOperaRate = priv->ieee80211->rate / 5;
	
	if (CurrTxokCnt > 0) {
		CurrRetryRate = (u16)(CurrRetryCnt * 100 / CurrTxokCnt);
	} else {
	
		CurrRetryRate = (u16)(CurrRetryCnt * 100 / 1);
	}

	priv->LastRetryCnt = priv->CurrRetryCnt;
	priv->LastTxokCnt = priv->NumTxOkTotal;
	priv->LastRxokCnt = priv->ieee80211->NumRxOkTotal;
	priv->CurrRetryCnt = 0;

	
	if (CurrRetryRate == 0 && CurrTxokCnt == 0) {
		priv->TryupingCountNoData++;

		
		if (priv->TryupingCountNoData > 30) {
			priv->TryupingCountNoData = 0;
			priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
			
			priv->LastFailTxRate = 0;
			priv->LastFailTxRateSS = -200;
			priv->FailTxRateCount = 0;
		}
		goto SetInitialGain;
	} else {
		priv->TryupingCountNoData = 0; 
	}



	if (priv->CurrentOperaRate == 22 || priv->CurrentOperaRate == 72)
		TryUpTh += 9;
	if (MgntIsCckRate(priv->CurrentOperaRate) || priv->CurrentOperaRate == 36)
		TryDownTh += 1;

	
	if (priv->bTryuping == true) {

		
		if ((CurrRetryRate > 25) && TxThroughput < priv->LastTxThroughput) {
			
			bTryDown = true;
		} else {
			priv->bTryuping = false;
		}
	} else if (CurrSignalStrength > -47 && (CurrRetryRate < 50)) {
		
		if (priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate) {
			bTryUp = true;
			
			priv->TryupingCount += TryUpTh;
		}

	} else if (CurrTxokCnt > 9 && CurrTxokCnt < 100 && CurrRetryRate >= 600) {
		bTryDown = true;
		
		priv->TryDownCountLowData += TryDownTh;
	} else if (priv->CurrentOperaRate == 108) {
		
		
		if ((CurrRetryRate > 26) && (priv->LastRetryRate > 25)) {
			bTryDown = true;
		}
		
		else if ((CurrRetryRate > 17) && (priv->LastRetryRate > 16) && (CurrSignalStrength > -72)) {
			bTryDown = true;
		}

		if (bTryDown && (CurrSignalStrength < -75)) 
			priv->TryDownCountLowData += TryDownTh;
	}
	else if (priv->CurrentOperaRate == 96) {
		
		
		if (((CurrRetryRate > 48) && (priv->LastRetryRate > 47))) {
			bTryDown = true;
		} else if (((CurrRetryRate > 21) && (priv->LastRetryRate > 20)) && (CurrSignalStrength > -74)) { 
			
			bTryDown = true;
		} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		} else if ((CurrRetryRate < 8) && (priv->LastRetryRate < 8)) { 
			bTryUp = true;
		}

		if (bTryDown && (CurrSignalStrength < -75)){
			priv->TryDownCountLowData += TryDownTh;
		}
	} else if (priv->CurrentOperaRate == 72) {
		
		if ((CurrRetryRate > 43) && (priv->LastRetryRate > 41)) {
			
			bTryDown = true;
		} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		} else if ((CurrRetryRate < 15) &&  (priv->LastRetryRate < 16)) { 
			bTryUp = true;
		}

		if (bTryDown && (CurrSignalStrength < -80))
			priv->TryDownCountLowData += TryDownTh;

	} else if (priv->CurrentOperaRate == 48) {
		
		
		if (((CurrRetryRate > 63) && (priv->LastRetryRate > 62))) {
			bTryDown = true;
		} else if (((CurrRetryRate > 33) && (priv->LastRetryRate > 32)) && (CurrSignalStrength > -82)) { 
			bTryDown = true;
		} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2 )) {
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		} else if ((CurrRetryRate < 20) && (priv->LastRetryRate < 21)) { 
			bTryUp = true;
		}

		if (bTryDown && (CurrSignalStrength < -82))
			priv->TryDownCountLowData += TryDownTh;

	} else if (priv->CurrentOperaRate == 36) {
		if (((CurrRetryRate > 85) && (priv->LastRetryRate > 86))) {
			bTryDown = true;
		} else if ((CurrRetryRate > (priv->LastRetryRate + 50)) && (priv->FailTxRateCount > 2)) {
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		} else if ((CurrRetryRate < 22) && (priv->LastRetryRate < 23)) { 
			bTryUp = true;
		}
	} else if (priv->CurrentOperaRate == 22) {
		
		if (CurrRetryRate > 95) {
			bTryDown = true;
		}
		else if ((CurrRetryRate < 29) && (priv->LastRetryRate < 30)) { 
			bTryUp = true;
		}
	} else if (priv->CurrentOperaRate == 11) {
		
		if (CurrRetryRate > 149) {
			bTryDown = true;
		} else if ((CurrRetryRate < 60) && (priv->LastRetryRate < 65)) {
			bTryUp = true;
		}
	} else if (priv->CurrentOperaRate == 4) {
		
		if ((CurrRetryRate > 99) && (priv->LastRetryRate > 99)) {
			bTryDown = true;
		} else if ((CurrRetryRate < 65) && (priv->LastRetryRate < 70)) {
			bTryUp = true;
		}
	} else if (priv->CurrentOperaRate == 2) {
		
		if ((CurrRetryRate < 70) && (priv->LastRetryRate < 75)) {
			bTryUp = true;
		}
	}

	if (bTryUp && bTryDown)
	printk("StaRateAdaptive87B(): Tx Rate tried upping and downing simultaneously!\n");

 
	if (!bTryUp && !bTryDown && (priv->TryupingCount == 0) && (priv->TryDownCountLowData == 0)
		&& priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate && priv->FailTxRateCount < 2) {
		if (jiffies % (CurrRetryRate + 101) == 0) {
			bTryUp = true;
			priv->bTryuping = true;
		}
	}

	
	if (bTryUp) {
		priv->TryupingCount++;
		priv->TryDownCountLowData = 0;


		if ((priv->TryupingCount > (TryUpTh + priv->FailTxRateCount * priv->FailTxRateCount)) ||
			(CurrSignalStrength > priv->LastFailTxRateSS) || priv->bTryuping) {
			priv->TryupingCount = 0;
			if (priv->CurrentOperaRate == 22)
				bUpdateInitialGain = true;

			if (((priv->CurrentOperaRate == 72) || (priv->CurrentOperaRate == 48) || (priv->CurrentOperaRate == 36)) &&
				(priv->FailTxRateCount > 2))
				priv->RateAdaptivePeriod = (RATE_ADAPTIVE_TIMER_PERIOD / 2);

			
			

			priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);

			if (priv->CurrentOperaRate == 36) {
				priv->bUpdateARFR = true;
				write_nic_word(dev, ARFR, 0x0F8F); 
			} else if(priv->bUpdateARFR) {
				priv->bUpdateARFR = false;
				write_nic_word(dev, ARFR, 0x0FFF); 
			}

			
			if (priv->LastFailTxRate != priv->CurrentOperaRate) {
				priv->LastFailTxRate = priv->CurrentOperaRate;
				priv->FailTxRateCount = 0;
				priv->LastFailTxRateSS = -200; 
			}
		}
	} else {
		if (priv->TryupingCount > 0)
			priv->TryupingCount --;
	}

	if (bTryDown) {
		priv->TryDownCountLowData++;
		priv->TryupingCount = 0;

		
		if (priv->TryDownCountLowData > TryDownTh || priv->bTryuping) {
			priv->TryDownCountLowData = 0;
			priv->bTryuping = false;
			
			if (priv->LastFailTxRate == priv->CurrentOperaRate) {
				priv->FailTxRateCount++;
				
				if (CurrSignalStrength > priv->LastFailTxRateSS)
					priv->LastFailTxRateSS = CurrSignalStrength;
			} else {
				priv->LastFailTxRate = priv->CurrentOperaRate;
				priv->FailTxRateCount = 1;
				priv->LastFailTxRateSS = CurrSignalStrength;
			}
			priv->CurrentOperaRate = GetDegradeTxRate(dev, priv->CurrentOperaRate);

			
			if ((CurrSignalStrength < -80) && (priv->CurrentOperaRate > 72 )) {
				priv->CurrentOperaRate = 72;
			}

			if (priv->CurrentOperaRate == 36) {
				priv->bUpdateARFR = true;
				write_nic_word(dev, ARFR, 0x0F8F); 
			} else if (priv->bUpdateARFR) {
				priv->bUpdateARFR = false;
				write_nic_word(dev, ARFR, 0x0FFF); 
			}

			if (MgntIsCckRate(priv->CurrentOperaRate)) {
				bUpdateInitialGain = true;
			}
		}
	} else {
		if (priv->TryDownCountLowData > 0)
			priv->TryDownCountLowData--;
	}

	if (priv->FailTxRateCount >= 0x15 ||
		(!bTryUp && !bTryDown && priv->TryDownCountLowData == 0 && priv->TryupingCount && priv->FailTxRateCount > 0x6)) {
		priv->FailTxRateCount--;
	}


	OfdmTxPwrIdx  = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
	CckTxPwrIdx  = priv->chtxpwr[priv->ieee80211->current_network.channel];

	
	if ((priv->CurrentOperaRate < 96) && (priv->CurrentOperaRate > 22)) {
		u1bCck = read_nic_byte(dev, CCK_TXAGC);
		u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);

		
		if (u1bCck == CckTxPwrIdx) {
			if (u1bOfdm != (OfdmTxPwrIdx + 2)) {
			priv->bEnhanceTxPwr = true;
			u1bOfdm = ((u1bOfdm + 2) > 35) ? 35: (u1bOfdm + 2);
			write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
			}
		} else if (u1bCck < CckTxPwrIdx) {
		
			if (!priv->bEnhanceTxPwr) {
				priv->bEnhanceTxPwr = true;
				u1bOfdm = ((u1bOfdm + 2) > 35) ? 35: (u1bOfdm + 2);
				write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
			}
		}
	} else if (priv->bEnhanceTxPwr) {  
		u1bCck = read_nic_byte(dev, CCK_TXAGC);
		u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);

		
		if (u1bCck == CckTxPwrIdx) {
			priv->bEnhanceTxPwr = false;
			write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
		}
		
		else if (u1bCck < CckTxPwrIdx) {
			priv->bEnhanceTxPwr = false;
			u1bOfdm = ((u1bOfdm - 2) > 0) ? (u1bOfdm - 2): 0;
			write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
		}
	}

SetInitialGain:
	if (bUpdateInitialGain) {
		if (MgntIsCckRate(priv->CurrentOperaRate)) { 
			if (priv->InitialGain > priv->RegBModeGainStage) {
				priv->InitialGainBackUp = priv->InitialGain;

				if (CurrSignalStrength < -85) 
					
					priv->InitialGain = priv->RegBModeGainStage;

				else if (priv->InitialGain > priv->RegBModeGainStage + 1)
					priv->InitialGain -= 2;

				else
					priv->InitialGain--;

				printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n",priv->InitialGain, priv->CurrentOperaRate);
				UpdateInitialGain(dev);
			}
		} else { 
			if (priv->InitialGain < 4) {
				priv->InitialGainBackUp = priv->InitialGain;

				priv->InitialGain++;
				printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n",priv->InitialGain, priv->CurrentOperaRate);
				UpdateInitialGain(dev);
			}
		}
	}

	
	priv->LastRetryRate = CurrRetryRate;
	priv->LastTxThroughput = TxThroughput;
	priv->ieee80211->rate = priv->CurrentOperaRate * 5;
}
Beispiel #4
0
void
StaRateAdaptive87SE(
	struct net_device *dev
	)
{
	struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
	unsigned long 			CurrTxokCnt;
	u16			CurrRetryCnt;
	u16			CurrRetryRate;
	//u16			i,idx;
	unsigned long       	CurrRxokCnt;
	bool			bTryUp = false;
	bool			bTryDown = false;
	u8			TryUpTh = 1;
	u8			TryDownTh = 2;
	u32			TxThroughput;
	long		CurrSignalStrength;
	bool		bUpdateInitialGain = false;
    	u8			u1bOfdm=0, u1bCck = 0;
	char		OfdmTxPwrIdx, CckTxPwrIdx;

	priv->RateAdaptivePeriod= RATE_ADAPTIVE_TIMER_PERIOD;

	CurrRetryCnt	= priv->CurrRetryCnt;
	CurrTxokCnt	= priv->NumTxOkTotal - priv->LastTxokCnt;
	CurrRxokCnt	= priv->ieee80211->NumRxOkTotal - priv->LastRxokCnt;
	CurrSignalStrength = priv->Stats_RecvSignalPower;
	TxThroughput = (u32)(priv->NumTxOkBytesTotal - priv->LastTxOKBytes);
	priv->LastTxOKBytes = priv->NumTxOkBytesTotal;
	priv->CurrentOperaRate = priv->ieee80211->rate/5;
	//printk("priv->CurrentOperaRate is %d\n",priv->CurrentOperaRate);
	//2 Compute retry ratio.
	if (CurrTxokCnt>0)
	{
		CurrRetryRate = (u16)(CurrRetryCnt*100/CurrTxokCnt);
	}
	else
	{ // It may be serious retry. To distinguish serious retry or no packets modified by Bruce
		CurrRetryRate = (u16)(CurrRetryCnt*100/1);
	}

	//
	// Added by Roger, 2007.01.02.
	// For debug information.
	//
	//printk("\n(1) pHalData->LastRetryRate: %d \n",priv->LastRetryRate);
	//printk("(2) RetryCnt = %d  \n", CurrRetryCnt);
	//printk("(3) TxokCnt = %d \n", CurrTxokCnt);
	//printk("(4) CurrRetryRate = %d \n", CurrRetryRate);
	//printk("(5) CurrSignalStrength = %d \n",CurrSignalStrength);
	//printk("(6) TxThroughput is %d\n",TxThroughput);
	//printk("priv->NumTxOkBytesTotal is %d\n",priv->NumTxOkBytesTotal);

	priv->LastRetryCnt = priv->CurrRetryCnt;
	priv->LastTxokCnt = priv->NumTxOkTotal;
	priv->LastRxokCnt = priv->ieee80211->NumRxOkTotal;
	priv->CurrRetryCnt = 0;

	//2No Tx packets, return to init_rate or not?
	if (CurrRetryRate==0 && CurrTxokCnt == 0)
	{
		//
		//After 9 (30*300ms) seconds in this condition, we try to raise rate.
		//
		priv->TryupingCountNoData++;

//		printk("No Tx packets, TryupingCountNoData(%d)\n", priv->TryupingCountNoData);
		//[TRC Dell Lab] Extend raised period from 4.5sec to 9sec, Isaiah 2008-02-15 18:00
		if (priv->TryupingCountNoData>30)
		{
			priv->TryupingCountNoData = 0;
		 	priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
			// Reset Fail Record
			priv->LastFailTxRate = 0;
			priv->LastFailTxRateSS = -200;
			priv->FailTxRateCount = 0;
		}
		goto SetInitialGain;
	}
        else
	{
		priv->TryupingCountNoData=0; //Reset trying up times.
	}

	//
	// For Netgear case, I comment out the following signal strength estimation,
	// which can results in lower rate to transmit when sample is NOT enough (e.g. PING request).
	// 2007.04.09, by Roger.
	//

	//
	// Restructure rate adaptive as the following main stages:
	// (1) Add retry threshold in 54M upgrading condition with signal strength.
	// (2) Add the mechanism to degrade to CCK rate according to signal strength
	//		and retry rate.
	// (3) Remove all Initial Gain Updates over OFDM rate. To avoid the complicated
	//		situation, Initial Gain Update is upon on DIG mechanism except CCK rate.
	// (4) Add the mehanism of trying to upgrade tx rate.
	// (5) Record the information of upping tx rate to avoid trying upping tx rate constantly.
	// By Bruce, 2007-06-05.
	//
	//

	// 11Mbps or 36Mbps
	// Check more times in these rate(key rates).
	//
	if(priv->CurrentOperaRate == 22 || priv->CurrentOperaRate == 72)
	{
		TryUpTh += 9;
	}
	//
	// Let these rates down more difficult.
	//
	if(MgntIsCckRate(priv->CurrentOperaRate) || priv->CurrentOperaRate == 36)
	{
			TryDownTh += 1;
	}

	//1 Adjust Rate.
	if (priv->bTryuping == true)
	{
		//2 For Test Upgrading mechanism
		// Note:
		// 	Sometimes the throughput is upon on the capability bwtween the AP and NIC,
		// 	thus the low data rate does not improve the performance.
		// 	We randomly upgrade the data rate and check if the retry rate is improved.

		// Upgrading rate did not improve the retry rate, fallback to the original rate.
		if ( (CurrRetryRate > 25) && TxThroughput < priv->LastTxThroughput)
		{
			//Not necessary raising rate, fall back rate.
			bTryDown = true;
			//printk("case1-1: Not necessary raising rate, fall back rate....\n");
			//printk("case1-1: pMgntInfo->CurrentOperaRate =%d, TxThroughput = %d, LastThroughput = %d\n",
			//		priv->CurrentOperaRate, TxThroughput, priv->LastTxThroughput);
		}
		else
		{
			priv->bTryuping = false;
		}
	}
	else if (CurrSignalStrength > -47 && (CurrRetryRate < 50))
	{
		//2For High Power
		//
		// Added by Roger, 2007.04.09.
		// Return to highest data rate, if signal strength is good enough.
		// SignalStrength threshold(-50dbm) is for RTL8186.
		// Revise SignalStrength threshold to -51dbm.
		//
		// Also need to check retry rate for safety, by Bruce, 2007-06-05.
		if(priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate )
		{
			bTryUp = true;
			// Upgrade Tx Rate directly.
			priv->TryupingCount += TryUpTh;
		}
//		printk("case2: StaRateAdaptive87SE: Power(%d) is high enough!!. \n", CurrSignalStrength);

	}
	else if(CurrTxokCnt > 9 && CurrTxokCnt< 100 && CurrRetryRate >= 600)
	{
		//2 For Serious Retry
		//
		// Traffic is not busy but our Tx retry is serious.
		//
		bTryDown = true;
		// Let Rate Mechanism to degrade tx rate directly.
		priv->TryDownCountLowData += TryDownTh;
//		printk("case3: RA: Tx Retry is serious. Degrade Tx Rate to %d directly...\n", priv->CurrentOperaRate);
	}
	else if ( priv->CurrentOperaRate == 108 )
	{
		//2For 54Mbps
		// Air Link
		if ( (CurrRetryRate>26)&&(priv->LastRetryRate>25))
//		if ( (CurrRetryRate>40)&&(priv->LastRetryRate>39))
		{
			//Down to rate 48Mbps.
			bTryDown = true;
		}
		// Cable Link
		else if ( (CurrRetryRate>17)&&(priv->LastRetryRate>16) && (CurrSignalStrength > -72))
//		else if ( (CurrRetryRate>17)&&(priv->LastRetryRate>16) && (CurrSignalStrength > -72))
		{
			//Down to rate 48Mbps.
			bTryDown = true;
		}

		if(bTryDown && (CurrSignalStrength < -75)) //cable link
		{
			priv->TryDownCountLowData += TryDownTh;
		}
		//printk("case4---54M \n");

	}
	else if ( priv->CurrentOperaRate == 96 )
	{
		//2For 48Mbps
		//Air Link
		if ( ((CurrRetryRate>48) && (priv->LastRetryRate>47)))
//		if ( ((CurrRetryRate>65) && (priv->LastRetryRate>64)))

		{
			//Down to rate 36Mbps.
			bTryDown = true;
		}
		//Cable Link
		else if ( ((CurrRetryRate>21) && (priv->LastRetryRate>20)) && (CurrSignalStrength > -74))
		{
			//Down to rate 36Mbps.
			bTryDown = true;
		}
		else if((CurrRetryRate>  (priv->LastRetryRate + 50 )) && (priv->FailTxRateCount >2 ))
//		else if((CurrRetryRate>  (priv->LastRetryRate + 70 )) && (priv->FailTxRateCount >2 ))
		{
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		}
		else if ( (CurrRetryRate<8) && (priv->LastRetryRate<8) ) //TO DO: need to consider (RSSI)
//		else if ( (CurrRetryRate<28) && (priv->LastRetryRate<8) )
		{
			bTryUp = true;
		}

		if(bTryDown && (CurrSignalStrength < -75))
		{
			priv->TryDownCountLowData += TryDownTh;
		}
		//printk("case5---48M \n");
	}
	else if ( priv->CurrentOperaRate == 72 )
	{
		//2For 36Mbps
		if ( (CurrRetryRate>43) && (priv->LastRetryRate>41))
//		if ( (CurrRetryRate>60) && (priv->LastRetryRate>59))
		{
			//Down to rate 24Mbps.
			bTryDown = true;
		}
		else if((CurrRetryRate>  (priv->LastRetryRate + 50 )) && (priv->FailTxRateCount >2 ))
//		else if((CurrRetryRate>  (priv->LastRetryRate + 70 )) && (priv->FailTxRateCount >2 ))
		{
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		}
		else if ( (CurrRetryRate<15) &&  (priv->LastRetryRate<16)) //TO DO: need to consider (RSSI)
//		else if ( (CurrRetryRate<35) &&  (priv->LastRetryRate<36))
		{
			bTryUp = true;
		}

		if(bTryDown && (CurrSignalStrength < -80))
		{
			priv->TryDownCountLowData += TryDownTh;
		}
		//printk("case6---36M \n");
	}
	else if ( priv->CurrentOperaRate == 48 )
	{
		//2For 24Mbps
		// Air Link
		if ( ((CurrRetryRate>63) && (priv->LastRetryRate>62)))
//		if ( ((CurrRetryRate>83) && (priv->LastRetryRate>82)))
		{
			//Down to rate 18Mbps.
			bTryDown = true;
		}
		//Cable Link
		else if ( ((CurrRetryRate>33) && (priv->LastRetryRate>32)) && (CurrSignalStrength > -82) )
//		 else if ( ((CurrRetryRate>50) && (priv->LastRetryRate>49)) && (CurrSignalStrength > -82) )
		{
			//Down to rate 18Mbps.
			bTryDown = true;
		}
		else if((CurrRetryRate>  (priv->LastRetryRate + 50 )) && (priv->FailTxRateCount >2 ))
//		else if((CurrRetryRate>  (priv->LastRetryRate + 70 )) && (priv->FailTxRateCount >2 ))

		{
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		}
  		else if ( (CurrRetryRate<20) && (priv->LastRetryRate<21)) //TO DO: need to consider (RSSI)
//		else if ( (CurrRetryRate<40) && (priv->LastRetryRate<41))
		{
			bTryUp = true;
		}

		if(bTryDown && (CurrSignalStrength < -82))
		{
			priv->TryDownCountLowData += TryDownTh;
		}
		//printk("case7---24M \n");
	}
	else if ( priv->CurrentOperaRate == 36 )
	{
		//2For 18Mbps
		// original (109, 109)
		//[TRC Dell Lab] (90, 91), Isaiah 2008-02-18 23:24
		//			     (85, 86), Isaiah 2008-02-18 24:00
		if ( ((CurrRetryRate>85) && (priv->LastRetryRate>86)))
//		if ( ((CurrRetryRate>115) && (priv->LastRetryRate>116)))
		{
			//Down to rate 11Mbps.
			bTryDown = true;
		}
		//[TRC Dell Lab]  Isaiah 2008-02-18 23:24
		else if((CurrRetryRate>  (priv->LastRetryRate + 50 )) && (priv->FailTxRateCount >2 ))
//		else if((CurrRetryRate>  (priv->LastRetryRate + 70 )) && (priv->FailTxRateCount >2 ))
		{
			bTryDown = true;
			priv->TryDownCountLowData += TryDownTh;
		}
		else if ( (CurrRetryRate<22) && (priv->LastRetryRate<23)) //TO DO: need to consider (RSSI)
//		else if ( (CurrRetryRate<42) && (priv->LastRetryRate<43))
		{
			bTryUp = true;
		}
		//printk("case8---18M \n");
	}
	else if ( priv->CurrentOperaRate == 22 )
	{
		//2For 11Mbps
		if (CurrRetryRate>95)
//		if (CurrRetryRate>155)
		{
			bTryDown = true;
		}
		else if ( (CurrRetryRate<29) && (priv->LastRetryRate <30) )//TO DO: need to consider (RSSI)
//		else if ( (CurrRetryRate<49) && (priv->LastRetryRate <50) )
			{
			bTryUp = true;
			}
		//printk("case9---11M \n");
		}
	else if ( priv->CurrentOperaRate == 11 )
	{
		//2For 5.5Mbps
		if (CurrRetryRate>149)
//		if (CurrRetryRate>189)
		{
			bTryDown = true;
		}
		else if ( (CurrRetryRate<60) && (priv->LastRetryRate < 65))
//		else if ( (CurrRetryRate<80) && (priv->LastRetryRate < 85))

			{
			bTryUp = true;
			}
		//printk("case10---5.5M \n");
		}
	else if ( priv->CurrentOperaRate == 4 )
	{
		//2For 2 Mbps
		if((CurrRetryRate>99) && (priv->LastRetryRate>99))
//		if((CurrRetryRate>199) && (priv->LastRetryRate>199))
		{
			bTryDown = true;
		}
		else if ( (CurrRetryRate < 65) && (priv->LastRetryRate < 70))
//		else if ( (CurrRetryRate < 85) && (priv->LastRetryRate < 90))
		{
			bTryUp = true;
		}
		//printk("case11---2M \n");
	}
	else if ( priv->CurrentOperaRate == 2 )
	{
		//2For 1 Mbps
		if( (CurrRetryRate<70) && (priv->LastRetryRate<75))
//		if( (CurrRetryRate<90) && (priv->LastRetryRate<95))
		{
			bTryUp = true;
		}
		//printk("case12---1M \n");
	}

	if(bTryUp && bTryDown)
    	printk("StaRateAdaptive87B(): Tx Rate tried upping and downing simultaneously!\n");

	//1 Test Upgrading Tx Rate
	// Sometimes the cause of the low throughput (high retry rate) is the compatibility between the AP and NIC.
	// To test if the upper rate may cause lower retry rate, this mechanism randomly occurs to test upgrading tx rate.
	if(!bTryUp && !bTryDown && (priv->TryupingCount == 0) && (priv->TryDownCountLowData == 0)
		&& priv->CurrentOperaRate != priv->ieee80211->current_network.HighestOperaRate && priv->FailTxRateCount < 2)
	{
		if(jiffies% (CurrRetryRate + 101) == 0)
		{
			bTryUp = true;
			priv->bTryuping = true;
			//printk("StaRateAdaptive87SE(): Randomly try upgrading...\n");
		}
	}

	//1 Rate Mechanism
	if(bTryUp)
	{
		priv->TryupingCount++;
		priv->TryDownCountLowData = 0;

		{
//			printk("UP: pHalData->TryupingCount = %d\n", priv->TryupingCount);
//			printk("UP: TryUpTh(%d)+ (FailTxRateCount(%d))^2 =%d\n",
//				TryUpTh, priv->FailTxRateCount, (TryUpTh + priv->FailTxRateCount * priv->FailTxRateCount) );
//			printk("UP: pHalData->bTryuping=%d\n",  priv->bTryuping);

		}

		//
		// Check more times if we need to upgrade indeed.
		// Because the largest value of pHalData->TryupingCount is 0xFFFF and
		// the largest value of pHalData->FailTxRateCount is 0x14,
		// this condition will be satisfied at most every 2 min.
		//

		if((priv->TryupingCount > (TryUpTh + priv->FailTxRateCount * priv->FailTxRateCount)) ||
			(CurrSignalStrength > priv->LastFailTxRateSS) || priv->bTryuping)
		{
			priv->TryupingCount = 0;
			//
			// When transferring from CCK to OFDM, DIG is an important issue.
			//
			if(priv->CurrentOperaRate == 22)
				bUpdateInitialGain = true;

			// The difference in throughput between 48Mbps and 36Mbps is 8M.
			// So, we must be carefully in this rate scale. Isaiah 2008-02-15.
			//
			if(  ((priv->CurrentOperaRate == 72) || (priv->CurrentOperaRate == 48) || (priv->CurrentOperaRate == 36)) &&
				(priv->FailTxRateCount > 2) )
				priv->RateAdaptivePeriod= (RATE_ADAPTIVE_TIMER_PERIOD/2);

			// (1)To avoid upgrade frequently to the fail tx rate, add the FailTxRateCount into the threshold.
			// (2)If the signal strength is increased, it may be able to upgrade.

			priv->CurrentOperaRate = GetUpgradeTxRate(dev, priv->CurrentOperaRate);
//			printk("StaRateAdaptive87SE(): Upgrade Tx Rate to %d\n", priv->CurrentOperaRate);

			//[TRC Dell Lab] Bypass 12/9/6, Isaiah 2008-02-18 20:00
			if(priv->CurrentOperaRate ==36)
			{
				priv->bUpdateARFR=true;
				write_nic_word(dev, ARFR, 0x0F8F); //bypass 12/9/6
//				printk("UP: ARFR=0xF8F\n");
			}
			else if(priv->bUpdateARFR)
			{
				priv->bUpdateARFR=false;
				write_nic_word(dev, ARFR, 0x0FFF); //set 1M ~ 54Mbps.
//				printk("UP: ARFR=0xFFF\n");
			}

			// Update Fail Tx rate and count.
			if(priv->LastFailTxRate != priv->CurrentOperaRate)
			{
				priv->LastFailTxRate = priv->CurrentOperaRate;
				priv->FailTxRateCount = 0;
				priv->LastFailTxRateSS = -200; // Set lowest power.
			}
		}
	}
	else
	{
		if(priv->TryupingCount > 0)
			priv->TryupingCount --;
	}

	if(bTryDown)
	{
		priv->TryDownCountLowData++;
		priv->TryupingCount = 0;
		{
//			printk("DN: pHalData->TryDownCountLowData = %d\n",priv->TryDownCountLowData);
//			printk("DN: TryDownTh =%d\n", TryDownTh);
//			printk("DN: pHalData->bTryuping=%d\n",  priv->bTryuping);
		}

		//Check if Tx rate can be degraded or Test trying upgrading should fallback.
		if(priv->TryDownCountLowData > TryDownTh || priv->bTryuping)
		{
			priv->TryDownCountLowData = 0;
			priv->bTryuping = false;
			// Update fail information.
			if(priv->LastFailTxRate == priv->CurrentOperaRate)
			{
				priv->FailTxRateCount ++;
				// Record the Tx fail rate signal strength.
				if(CurrSignalStrength > priv->LastFailTxRateSS)
				{
					priv->LastFailTxRateSS = CurrSignalStrength;
				}
			}
			else
			{
				priv->LastFailTxRate = priv->CurrentOperaRate;
				priv->FailTxRateCount = 1;
				priv->LastFailTxRateSS = CurrSignalStrength;
			}
			priv->CurrentOperaRate = GetDegradeTxRate(dev, priv->CurrentOperaRate);

			// Reduce chariot training time at weak signal strength situation. SD3 ED demand.
			//[TRC Dell Lab] Revise Signal Threshold from -75 to -80 , Isaiah 2008-02-18 20:00
			if( (CurrSignalStrength < -80) && (priv->CurrentOperaRate > 72 ))
			{
				priv->CurrentOperaRate = 72;
//				printk("DN: weak signal strength (%d), degrade to 36Mbps\n", CurrSignalStrength);
			}

			//[TRC Dell Lab] Bypass 12/9/6, Isaiah 2008-02-18 20:00
			if(priv->CurrentOperaRate ==36)
			{
				priv->bUpdateARFR=true;
				write_nic_word(dev, ARFR, 0x0F8F); //bypass 12/9/6
//				printk("DN: ARFR=0xF8F\n");
			}
			else if(priv->bUpdateARFR)
			{
				priv->bUpdateARFR=false;
				write_nic_word(dev, ARFR, 0x0FFF); //set 1M ~ 54Mbps.
//				printk("DN: ARFR=0xFFF\n");
			}

			//
			// When it is CCK rate, it may need to update initial gain to receive lower power packets.
			//
			if(MgntIsCckRate(priv->CurrentOperaRate))
			{
				bUpdateInitialGain = true;
			}
//			printk("StaRateAdaptive87SE(): Degrade Tx Rate to %d\n", priv->CurrentOperaRate);
		}
	}
	else
	{
		if(priv->TryDownCountLowData > 0)
			priv->TryDownCountLowData --;
	}

	// Keep the Tx fail rate count to equal to 0x15 at most.
	// Reduce the fail count at least to 10 sec if tx rate is tending stable.
	if(priv->FailTxRateCount >= 0x15 ||
		(!bTryUp && !bTryDown && priv->TryDownCountLowData == 0 && priv->TryupingCount && priv->FailTxRateCount > 0x6))
	{
		priv->FailTxRateCount --;
	}

	OfdmTxPwrIdx  = priv->chtxpwr_ofdm[priv->ieee80211->current_network.channel];
	CckTxPwrIdx  = priv->chtxpwr[priv->ieee80211->current_network.channel];

	//[TRC Dell Lab] Mac0x9e increase 2 level in 36M~18M situation, Isaiah 2008-02-18 24:00
	if((priv->CurrentOperaRate < 96) &&(priv->CurrentOperaRate > 22))
	{
		u1bCck = read_nic_byte(dev, CCK_TXAGC);
		u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);

		// case 1: Never enter High power
		if(u1bCck == CckTxPwrIdx )
		{
			if(u1bOfdm != (OfdmTxPwrIdx+2) )
			{
			priv->bEnhanceTxPwr= true;
			u1bOfdm = ((u1bOfdm+2) > 35) ? 35: (u1bOfdm+2);
			write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
//			printk("Enhance OFDM_TXAGC : +++++ u1bOfdm= 0x%x\n", u1bOfdm);
			}
		}
		// case 2: enter high power
		else if(u1bCck < CckTxPwrIdx)
		{
			if(!priv->bEnhanceTxPwr)
			{
				priv->bEnhanceTxPwr= true;
				u1bOfdm = ((u1bOfdm+2) > 35) ? 35: (u1bOfdm+2);
				write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
				//RT_TRACE(COMP_RATE, DBG_TRACE, ("Enhance OFDM_TXAGC(2) : +++++ u1bOfdm= 0x%x\n", u1bOfdm));
			}
		}
	}
	else if(priv->bEnhanceTxPwr)  //54/48/11/5.5/2/1
	{
		u1bCck = read_nic_byte(dev, CCK_TXAGC);
		u1bOfdm = read_nic_byte(dev, OFDM_TXAGC);

		// case 1: Never enter High power
		if(u1bCck == CckTxPwrIdx )
		{
		priv->bEnhanceTxPwr= false;
		write_nic_byte(dev, OFDM_TXAGC, OfdmTxPwrIdx);
		//printk("Recover OFDM_TXAGC : ===== u1bOfdm= 0x%x\n", OfdmTxPwrIdx);
		}
		// case 2: enter high power
		else if(u1bCck < CckTxPwrIdx)
		{
			priv->bEnhanceTxPwr= false;
			u1bOfdm = ((u1bOfdm-2) > 0) ? (u1bOfdm-2): 0;
			write_nic_byte(dev, OFDM_TXAGC, u1bOfdm);
			//RT_TRACE(COMP_RATE, DBG_TRACE, ("Recover OFDM_TXAGC(2): ===== u1bOfdm= 0x%x\n", u1bOfdm));

		}
	}

	//
	// We need update initial gain when we set tx rate "from OFDM to CCK" or
	// "from CCK to OFDM".
	//
SetInitialGain:
	if(bUpdateInitialGain)
	{
		if(MgntIsCckRate(priv->CurrentOperaRate)) // CCK
		{
			if(priv->InitialGain > priv->RegBModeGainStage)
			{
				priv->InitialGainBackUp= priv->InitialGain;

				if(CurrSignalStrength < -85) // Low power, OFDM [0x17] = 26.
				{
					//SD3 SYs suggest that CurrSignalStrength < -65, ofdm 0x17=26.
					priv->InitialGain = priv->RegBModeGainStage;
				}
				else if(priv->InitialGain > priv->RegBModeGainStage + 1)
				{
					priv->InitialGain -= 2;
				}
				else
				{
					priv->InitialGain --;
				}
				printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n",priv->InitialGain, priv->CurrentOperaRate);
				UpdateInitialGain(dev);
			}
		}
		else // OFDM
		{
			if(priv->InitialGain < 4)
			{
				priv->InitialGainBackUp= priv->InitialGain;

				priv->InitialGain ++;
				printk("StaRateAdaptive87SE(): update init_gain to index %d for date rate %d\n",priv->InitialGain, priv->CurrentOperaRate);
				UpdateInitialGain(dev);
			}
		}
	}

	//Record the related info
	priv->LastRetryRate = CurrRetryRate;
	priv->LastTxThroughput = TxThroughput;
	priv->ieee80211->rate = priv->CurrentOperaRate * 5;
}