/*--------------------------------------------------------------------------
int8 tunerbb_drv_mtv319_get_ber(struct broadcast_tdmb_sig_info *dmb_bb_info)
    (1)   Getting the RF/BB Information 
    (2)   Return Value
           Sucess : 1
           Fail : 0 or negative interger (If there is error code)
    (3)   Argument
            struct broadcast_tdmb_sig_info *dmb_bb_info (IN/OUT)
           struct broadcast_tdmb_sig_info
           {
                 uint32 dab_ok;
                 uint32 msc_ber;
                 uint32 sync_lock;
                 uint32 afc_ok;
                 uint32 cir;
                 uint32 fic_ber;
                 uint32 tp_lock;
                 uint32 sch_ber;
                 uint32 tp_err_cnt;
                 uint32 va_ber;
                 byte   srv_state_flag;
                 uint32 antenna_level;
           };

           These paramters are dependent on Information supplied by Device.
---------------------------------------------------------------------------- */
int8	tunerbb_drv_mtv319_get_ber(struct broadcast_tdmb_sig_info *dmb_bb_info)
{
	uint32 lock_mask, msc_cer, fic_cer, per, va_ber;

	msc_cer = rtvTDMB_GetCER();
	dmb_bb_info->msc_ber = msc_cer;
	dmb_bb_info->antenna_level = rtvTDMB_GetAntennaLevel(msc_cer);

	lock_mask = rtvTDMB_GetLockStatus();
	if (lock_mask & RTV_TDMB_OFDM_LOCK_MASK)
	{
		dmb_bb_info->sync_lock = 1;
		dmb_bb_info->dab_ok = 1;
		dmb_bb_info->cir = 1;
		dmb_bb_info->sch_ber = 1;
	}
	else
	{
		dmb_bb_info->sync_lock = 0;
		dmb_bb_info->dab_ok = 0;
		dmb_bb_info->cir = 0;
		dmb_bb_info->sch_ber = 0;
	}

	if (lock_mask & RTV_TDMB_AGC_LOCK_MASK)
		dmb_bb_info->afc_ok = 1;
	else
		dmb_bb_info->afc_ok = 0;

	if ((opened_subch_info[0].svc_type == MTV319_DMB)
	|| (opened_subch_info[0].svc_type == MTV319_VISUAL))
	{
		if (lock_mask & RTV_TDMB_FEC_LOCK_MASK)
			dmb_bb_info->tp_lock = 1;
		else
			dmb_bb_info->tp_lock = 0;

		va_ber = rtvTDMB_GetBER();
		dmb_bb_info->va_ber = va_ber;

		per = rtvTDMB_GetPER();
		dmb_bb_info->tp_err_cnt = per;
	}
	else
	{
		dmb_bb_info->tp_lock = 0;
		dmb_bb_info->tp_err_cnt = 0;
		dmb_bb_info->va_ber = 0;
	}

	fic_cer = rtvTDMB_GetFicCER();
	dmb_bb_info->fic_ber = fic_cer;

	return MTV319_RESULT_SUCCESS;
}
/*====================================================================
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);
}