/*====================================================================
FUNCTION       mtv319_ch_scan_start
DESCRIPTION 
    if for_air is greater than 0, this is for air play.
DEPENDENCIES
RETURN VALUE
SIDE EFFECTS
======================================================================*/
void mtv319_ch_scan_start(int freq, int band, unsigned char for_air)
{
  int res;
  TDMB_MSG_RTV_BB("[%s] Channel Frequency[%d] Band[%d] Mode[%d]\n", __func__, freq, band, for_air);

  rtvTDMB_CloseFIC();

  res = rtvTDMB_ScanFrequency(freq);

  rtvTDMB_OpenFIC();

  // 0501 for autoscan
  if((dmb_mode == TDMB_MODE_AUTOSCAN) 
#ifdef FEATURE_DMB_AUTOSCAN_DISCRETE
     || (dmb_mode == TDMB_MODE_SCAN_METRO)
#endif
  )
  {
    g_sync_status = (res == RTV_SUCCESS ? RTV_TDMB_CHANNEL_LOCK_OK : 0);
  }
  else
  {
    g_sync_status = RTV_TDMB_CHANNEL_LOCK_OK;
  }

  TDMB_MSG_RTV_BB("[%s]  %s  res[%d] sync_status[%d]\n",__func__,res==RTV_SUCCESS?"OK":"FAIL" ,res, g_sync_status);
}
static bool mtv318_scan_ch(struct ensemble_info_type *e_info
							, unsigned long freq)
{
	bool ret = false;

	if (mtv318_pwr_on == true && e_info != NULL) {
		if (rtvTDMB_ScanFrequency(freq/1000) == RTV_SUCCESS) {
			rtvTDMB_OpenFIC();
			ret = rtvFICDEC_Decode(freq/1000);
			rtvTDMB_CloseFIC();
			if (ret == true)
				__get_ensemble_info(e_info, (freq));
		}
	}

	return ret;
}
static bool mtv319_scan_ch(struct ensemble_info_type *e_info
							, unsigned long freq)
{
	enum E_RTV_FIC_DEC_RET_TYPE dc;
	bool ret = false;

	if (mtv319_pwr_on == true && e_info != NULL) {
		rtvTDMB_CloseAllSubChannels();

		if (rtvTDMB_ScanFrequency(freq/1000) == RTV_SUCCESS) {
			unsigned int i;
			int ret_size;

			rtvFICDEC_Init(); /* FIC parser Init */

			for (i = 0; i < 30; i++) {
				ret_size = rtvTDMB_ReadFIC(fic_buf);
				if (ret_size > 0) {
					dc = rtvFICDEC_Decode(fic_buf, 384);
					if (dc == RTV_FIC_RET_GOING)
						continue;

					if (dc == RTV_FIC_RET_DONE)
						ret = true;

					break; /* Stop */
				} else {
					DPRINTK("mtv319_scan_ch READ Fail\n");
				}
			}

			rtvTDMB_CloseFIC();
			if (ret == true)
				__get_ensemble_info(e_info, (freq));
		}
	}

	return ret;
}
/*-------------------------------------------------------------------------------------
int8 tunerbb_drv_mtv319_multi_set_channel(int32 freq_num, uint8 subch_cnt, uint8 subch_id[ ], uint8 op_mode[ ])
    (1)   Setting the frequency , subch_id and op_mode.
            This function is used in Single Service and Mulitiple Service
    (2)   Return Value
           Sucess : 1
           Fail : 0 or negative interger (If there is error code)
    (3)   Argument
           int32 freq_num (IN)
                - TDMB Frequency index(e.g 7A(71), 13C(133) etc). Convert frequency if needed
           uint8 subch_cnt (IN)
                - The number of multiple service. This value is 1 in case of Single Service
           uint8 subch_id[ ] (IN)
                - Service Componet Sub-Channel ID
           uint8  op_mode[ ] (IN)
                - Service Operation Mode
                DAB  = 1;
                DMB = 2;
                VISUAL = 3;
                DATA = 4;
                TPEG = 5;
                ENSQUERY = 6

           <notice> The size of subch_cnt[ ] and op_mode[ ] is the maximum number being supported by MTV319
--------------------------------------------------------------------------------------- */
int8	tunerbb_drv_mtv319_multi_set_channel(int32 freq_num, uint8 subch_cnt, uint8 subch_id[ ], uint8 op_mode[ ])
{
	uint8 i;
	uint32 freq_khz;
	enum E_RTV_SERVICE_TYPE svc_type;
	int ret;
	UINT intr_size; /* TS interrupt size. */
	bool fic_open_in_play = FALSE;

	freq_khz = get_freq_from_table(freq_num);
	//printk("tunerbb_drv_mtv319_multi_set_channel, freq_khz = %d, freq_num = %d\n",freq_khz, freq_num);

	if (op_mode[0] == MTV319_ENSQUERY)
	{
		fic_timeout_retry_cnt = 0;

		rtvTDMB_CloseFIC();
		rtvTDMB_CloseAllSubChannels();

		ret = rtvTDMB_ScanFrequency(freq_khz);
		if (ret == RTV_SUCCESS)
			return MTV319_RESULT_SUCCESS; /* Channel found and FIC opened */
		else 
		{
			if(ret != RTV_CHANNEL_NOT_DETECTED)
				DMBERR("Device error: %d\n", ret);

			return MTV319_RESULT_ERROR;
		}
	}

	for (i = 0; i < subch_cnt; i++)
	{
		opened_subch_info[i].svc_type = op_mode[i];
		opened_subch_info[i].subch_id = subch_id[i];

		switch (op_mode[i])
		{
		case MTV319_DMB:
		case MTV319_VISUAL:
		case MTV319_BLT_TEST:
			svc_type = RTV_SERVICE_DMB;
			intr_size = MTV319_DMB_INTERRUPT_SIZE;
			opened_subch_info[i].data_type = TDMB_BB_DATA_TS;
			break;

		case MTV319_DAB:
			svc_type = RTV_SERVICE_DAB;
			intr_size = 10 * 188;//MTV319_DMB_INTERRUPT_SIZE;
			opened_subch_info[i].data_type = TDMB_BB_DATA_DAB;
			break;

		case MTV319_DATA:
			svc_type = RTV_SERVICE_DAB;
			intr_size = 10 * 188;//MTV319_DMB_INTERRUPT_SIZE;
			opened_subch_info[i].data_type = TDMB_BB_DATA_PACK;
			break;

		case MTV319_ENSQUERY:
			fic_open_in_play = TRUE;
			opened_subch_info[i].data_type = TDMB_BB_DATA_FIC;
			break;

		default:
			DMBERR("Invalid op mode (%d)\n", op_mode[i]);
			goto svc_open_err;
		}

		if (op_mode[i] != MTV319_ENSQUERY)
		{
			ret = rtvTDMB_OpenSubChannel(freq_khz, subch_id[i],
										svc_type, intr_size);
			if (ret != RTV_SUCCESS)
			{
				if (ret != RTV_ALREADY_OPENED_SUBCHANNEL_ID)
				{
					DMBERR("Sub channel open failed: %d\n", ret);
					goto svc_open_err;
				}
			}
		}
	}

	if (fic_open_in_play)
	{	/* Must open fic after sub channel in play state. */
		ret = rtvTDMB_OpenFIC();
		if (ret != RTV_SUCCESS)
		{
			DMBERR("FIC open failed: %d\n", ret);
			goto svc_open_err;
		}
	}

	return MTV319_RESULT_SUCCESS;

svc_open_err:
	rtvTDMB_CloseAllSubChannels();

	return MTV319_RESULT_ERROR;
}
/*====================================================================
FUNCTION       mtv350_test
DESCRIPTION 
DEPENDENCIES
RETURN VALUE
SIDE EFFECTS
======================================================================*/
void mtv350_test(int servicetype)
{
  static boolean powered = FALSE;
  st_subch_info *stInfo;
  int res;

  if(!powered)
  {
    if(!tdmb_power_on)
      mtv319_power_on();
    else
      TDMB_MSG_RTV_BB("[%s] skip mtv319_power_on [%d]\n", __func__, tdmb_power_on);

    powered = TRUE;
  }

  TDMB_MSG_RTV_BB("[%s] mtv319_init\n", __func__);

  mtv319_init();

#ifdef  FEATURE_DMB_I2C_CMD
  mtv319_i2c_write(0x03, 0x07); //24.576Mhz 경우
  res = mtv319_i2c_read(0x00); //0x8A ?˜ì????
  TDMB_MSG_RTV_BB("[%s] RW test  Reg. 0x00 val[0x%x]==0x8a\n", __func__, res);
#endif

  stInfo = kmalloc(sizeof(st_subch_info), GFP_KERNEL);
  memset(stInfo, 0, sizeof(st_subch_info));

  tdmb_get_fixed_chan_info((service_t)servicetype, &stInfo->astSubChInfo[stInfo->nSetCnt]);

  stInfo->nSetCnt++;

  // 2012/04/26: RAONTECH
  stInfo->astSubChInfo[0].uiServiceType = (stInfo->astSubChInfo[0].uiServiceType==0x18) ? RTV_SERVICE_DMB : RTV_SERVICE_DAB;
  TDMB_MSG_RTV_BB("[%s] TEST start freq [%d]  dmb_mode [%d]\n", __func__, (int)stInfo->astSubChInfo[0].ulRFNum, (int)dmb_mode);
  TDMB_MSG_RTV_BB("[%s] TEST start service type [0x%x]  schsize[0x%x]\n", __func__, stInfo->astSubChInfo[0].uiServiceType, stInfo->astSubChInfo[0].uiSchSize);
  TDMB_MSG_RTV_BB("[%s] TEST start subch id [0x%x]\n", __func__, stInfo->astSubChInfo[0].uiSubChID);

  res = rtvTDMB_ScanFrequency(stInfo->astSubChInfo[0].ulRFNum);
  TDMB_MSG_RTV_BB("rtvTDMB_ScanFrequency %d\n", res);  
  if(res == RTV_SUCCESS)
  {
    TDMB_MSG_RTV_BB("rtvTDMB_ScanFrequency OK %d\n", res);
  }

  g_sync_status = rtvTDMB_GetLockStatus();

  TDMB_MSG_RTV_BB("RTV GetLockStatus  g_sync_status[%d]  [%d]\n", g_sync_status,RTV_TDMB_CHANNEL_LOCK_OK);

  rtvTDMB_CloseFIC(); //wgon test add

  rtvTDMB_CloseSubChannel(prev_subch_id); // for single 
  res = rtvTDMB_OpenSubChannel(stInfo->astSubChInfo[0].ulRFNum, stInfo->astSubChInfo[0].uiSubChID, stInfo->astSubChInfo[0].uiServiceType, RTV_SPI_CIF_MODE_INTERRUPT_SIZE);
  prev_subch_id = stInfo->astSubChInfo[0].uiSubChID;

  if(res != RTV_SUCCESS)
  {
    TDMB_MSG_RTV_BB("[%s] rtvTDMB_OpenSubChannel error [%d]\n", __func__, res);
  }

  kfree(stInfo);
}
/*================================================================== */
void mtv350_test(int servicetype)
{
  static boolean powered = FALSE;
  st_subch_info *stInfo;
  int res;

  if(!powered)
  {
    if(!tdmb_power_on)
      mtv350_power_on();
    else
      TDMB_MSG_RTV_BB("[%s] skip mtv350_power_on [%d]\n", __func__, tdmb_power_on);

    powered = TRUE;
  }

  TDMB_MSG_RTV_BB("[%s] mtv350_init\n", __func__);

  mtv350_init();

  mtv350_i2c_write(0x03, 0x07); //24.576Mhz 경우
  res = mtv350_i2c_read(0x00); //0x8A ?˜ì????
  TDMB_MSG_RTV_BB("[%s] RW test  Reg. 0x00 val[0x%x]==0x8a\n", __func__, res);

  stInfo = kmalloc(sizeof(st_subch_info), GFP_KERNEL);
  memset(stInfo, 0, sizeof(st_subch_info));

  tdmb_get_fixed_chan_info((service_t)servicetype, &stInfo->astSubChInfo[stInfo->nSetCnt]);

  stInfo->nSetCnt++;

  // 2012/04/26: RAONTECH
  stInfo->astSubChInfo[0].uiServiceType = (stInfo->astSubChInfo[0].uiServiceType==0x18) ? 0x01 : 0x02; //VIDEO 1, AUDIO 2
  TDMB_MSG_RTV_BB("[%s] TEST start freq [%d]  dmb_mode [%d]\n", __func__, (int)stInfo->astSubChInfo[0].ulRFNum, (int)dmb_mode);
  TDMB_MSG_RTV_BB("[%s] TEST start service type [0x%x]  schsize[0x%x]\n", __func__, stInfo->astSubChInfo[0].uiServiceType, stInfo->astSubChInfo[0].uiSchSize);
  TDMB_MSG_RTV_BB("[%s] TEST start subch id [0x%x]\n", __func__, stInfo->astSubChInfo[0].uiSubChID);

  res = rtvTDMB_ScanFrequency(stInfo->astSubChInfo[0].ulRFNum);
  TDMB_MSG_RTV_BB("rtvTDMB_ScanFrequency %d\n", res);  
  if(res == RTV_SUCCESS)
  {
    TDMB_MSG_RTV_BB("rtvTDMB_ScanFrequency OK %d\n", res);
  }

  g_sync_status = rtvTDMB_GetLockStatus();

  TDMB_MSG_RTV_BB("RTV GetLockStatus  g_sync_status[%d]  [%d]\n", g_sync_status,RTV_TDMB_CHANNEL_LOCK_OK);

  rtvTDMB_CloseSubChannel(prev_subch_id); // for single 
  res = rtvTDMB_OpenSubChannel(stInfo->astSubChInfo[0].ulRFNum, stInfo->astSubChInfo[0].uiSubChID, stInfo->astSubChInfo[0].uiServiceType, 188*8);
  prev_subch_id = stInfo->astSubChInfo[0].uiSubChID;

  if(res != RTV_SUCCESS)
  {
	if (res == RTV_ALREADY_OPENED_SUB_CHANNEL)
		TDMB_MSG_RTV_BB("[%s] Already opened %d\n", __func__, res);
	else
      TDMB_MSG_RTV_BB("[%s] RTV_IOCTL_TDMB_SET_SUBCHANNEL error %d\n", __func__, res);
  }

  kfree(stInfo);
}