/* 
	==========================================================================
	Description:
		This routine is called at initialization. It returns a channel number
		that complies to regulation domain and less interference with current
		enviornment.
	Return:
		ch -  channel number that
	NOTE:
		The retruned channel number is guaranteed to comply to current regulation
		domain that recorded in pAd->CommonCfg.CountryRegion
        Usage: 
               1.) iwpriv ra0 set AutoChannelSel=1
                   Ues the number of AP and inference status to choose
               2.) iwpriv ra0 set AutoChannelSel=2
                   Ues the False CCA count and Rssi to choose
	==========================================================================
 */
UCHAR APAutoSelectChannel(
	IN PRTMP_ADAPTER pAd,
	IN ChannelSel_Alg Alg)
{
	UCHAR ch = 0, i;

	/* passive scan channel 1-14. collect statistics */
	
	/*
		In the autochannel select case. AP didn't get channel yet.
		So have no way to determine which Band AP used by channel number.
	*/

	/* Init some structures before doing AutoChannelSelect() */
	APAutoChannelInit(pAd);

	if (( Alg == ChannelAlgRandom ) && (pAd->pChannelInfo->IsABand == TRUE))
	{   /*for Dfs */
		ch = SelectClearChannelRandom(pAd);
	}
	else
	{
#ifdef MICROWAVE_OVEN_SUPPORT
		pAd->CommonCfg.MO_Cfg.bEnable = FALSE;
		AsicMeasureFalseCCA(pAd);
#endif
		/*find RSSI in each channel */
		for (i=0; i<pAd->ChannelListNum; i++)
		{

			AsicSwitchChannel(pAd, pAd->ChannelList[i].Channel, TRUE);
			AsicLockChannel(pAd, pAd->ChannelList[i].Channel);/*do nothing */
			pAd->ApCfg.current_channel_index = i;


			pAd->ApCfg.AutoChannel_Channel = pAd->ChannelList[i].Channel;
			
#ifdef AP_QLOAD_SUPPORT
			if (QLOAD_DOES_ALARM_OCCUR(pAd))
			{   /* QLOAD ALARM, ever alarm from QLOAD module */
				OS_WAIT(400); /* wait for 400 ms at each channel. */
			}
			else
#endif /* AP_QLOAD_SUPPORT */
			{
				OS_WAIT(200); /* wait for 200 ms at each channel. */
			}

			UpdateChannelInfo(pAd, i,Alg);
		}

		ch = SelectBestChannel(pAd, Alg);
	}
		
	return ch;
}
/* 
	==========================================================================
	Description:
		This routine is called at initialization. It returns a channel number
		that complies to regulation domain and less interference with current
		enviornment.
	Return:
		ch -  channel number that
	NOTE:
		The retruned channel number is guaranteed to comply to current regulation
		domain that recorded in pAd->CommonCfg.CountryRegion
        Usage: 
               1.) iwpriv ra0 set AutoChannelSel=1
                   Ues the number of AP and inference status to choose
               2.) iwpriv ra0 set AutoChannelSel=2
                   Ues the False CCA count and Rssi to choose
	==========================================================================
 */
UCHAR APAutoSelectChannel(
	IN PRTMP_ADAPTER pAd,
	IN ChannelSel_Alg Alg)
{
	UCHAR ch = 0, i;

	// passive scan channel 1-14. collect statistics
	
	// In the autochannel select case. AP didn't get channel yet.
	// So have no way to determine which Band AP used by channel number.

	//
	// Init some structures before doing AutoChannelSelect()
	//
	APAutoChannelInit(pAd);

	if (( Alg == ChannelAlgRandom ) && (pAd->pChannelInfo->IsABand == TRUE))
	{   //for Dfs
		ch = SelectClearChannelRandom(pAd);
	}
	else
	{

		//find RSSI in each channel //
		for (i=0; i<pAd->ChannelListNum; i++)
		{

			AsicSwitchChannel(pAd, pAd->ChannelList[i].Channel, TRUE);
			AsicLockChannel(pAd, pAd->ChannelList[i].Channel);//do nothing
			pAd->ApCfg.current_channel_index = i;


			pAd->ApCfg.AutoChannel_Channel = pAd->ChannelList[i].Channel;
			
#ifdef AP_QLOAD_SUPPORT
			if (QLOAD_DOES_ALARM_OCCUR(pAd))
			{   /* QLOAD ALARM, ever alarm from QLOAD module */
				OS_WAIT(400); // wait for 400 ms at each channel.
			}
			else
#endif
			{
				OS_WAIT(200); // wait for 200 ms at each channel.
			}

			UpdateChannelInfo(pAd, i,Alg);
		}

		ch = SelectBestChannel(pAd, Alg);
	}
		
	return ch;
}
/* 
	==========================================================================
	Description:
		This routine is called at initialization. It returns a channel number
		that complies to regulation domain and less interference with current
		enviornment.
	Return:
		ch -  channel number that
	NOTE:
		The retruned channel number is guaranteed to comply to current regulation
		domain that recorded in pAd->CommonCfg.CountryRegion
        Usage: 
               1.) iwpriv ra0 set AutoChannelSel=1
                   Ues the number of AP and inference status to choose
               2.) iwpriv ra0 set AutoChannelSel=2
                   Ues the False CCA count and Rssi to choose
	==========================================================================
 */
UCHAR APAutoSelectChannel(RTMP_ADAPTER *pAd, ChannelSel_Alg Alg)
{
    UCHAR ch = 0;
#ifndef SUPPORT_ACS_BY_SCAN	
	UCHAR i = 0;
#endif
    /* passive scan channel 1-14. collect statistics */
    
    DBGPRINT(RT_DEBUG_TRACE, ("Entering ACS (Alg=%d)...\n", Alg));
    /*
        In the autochannel select case. AP didn't get channel yet.
        So have no way to determine which Band AP used by channel number.
    */
    /* Init some structures before doing AutoChannelSelect() */
    APAutoChannelInit(pAd);

    if (( Alg == ChannelAlgRandom ) && (pAd->pChannelInfo->IsABand == TRUE))
    {   /*for Dfs */
        DBGPRINT(RT_DEBUG_TRACE, ("ACS: Select from random\n"));
        ch = SelectClearChannelRandom(pAd);
    }
    else
    {
#ifndef SUPPORT_ACS_BY_SCAN    
        /*find RSSI in each channel */
        DBGPRINT(RT_DEBUG_TRACE, ("Start ACS scan (total channel = %d, stay_time = %dms)\n", pAd->ChannelListNum, AUTO_CHANNEL_SEL_TIMEOUT));
        for (i=0; i<pAd->ChannelListNum; i++)
        {
            AsicSwitchChannel(pAd, pAd->ChannelList[i].Channel, TRUE);
            AsicLockChannel(pAd, pAd->ChannelList[i].Channel); /*do nothing */
            pAd->ApCfg.current_channel_index = i;
            pAd->ApCfg.AutoChannel_Channel = pAd->ChannelList[i].Channel;
            OS_WAIT(AUTO_CHANNEL_SEL_TIMEOUT); /* wait for 200 ms at each channel. */
            UpdateChannelInfo(pAd, i, Alg);
        }        
        DBGPRINT(RT_DEBUG_TRACE, ("ACS scan complete)\n"));
#ifdef SUPPORT_ACS_ALL_CHANNEL_RANK
        if ((ch = ACS_PerformAlgorithm(pAd, Alg)) <= 0)
        {
            /* return ch is the total number of channels in the rank list */
            DBGPRINT(RT_DEBUG_ERROR, ("ACS: Error perform algorithm!\n"));
        }
        else
            DBGPRINT(RT_DEBUG_ERROR, ("ACS: Perform algorithm OK (ch_count=%d)!\n", ch));
#else
        ch = SelectBestChannel(pAd, Alg);
#endif /* SUPPORT_ACS_ALL_CHANNEL_RANK */
#endif /* SUPPORT_ACS_BY_SCAN */
    }

    return ch;

}