static inline int get_sig_cnr(U32 *bb_cnr)
{
	int cnr;

	*bb_cnr = rtvISDBT_GetCNR();

	cnr = *bb_cnr / RTV_ISDBT_CNR_DIVIDER;

	return cnr;
}
static int mtv250_ioctl(struct inode *inode, struct file *filp,
				        unsigned int cmd,  unsigned long arg)
#endif
{
	int ret = 0;
	void __user *argp = (void __user *)arg;	
	RTV_IOCTL_REGISTER_ACCESS ioctl_register_access;	
	RTV_IOCTL_TEST_GPIO_INFO gpio_info;
	U8 reg_page = 0;
	UINT lock_mask;	
	UINT isdbt_ch_num;
	RTV_ISDBT_TMCC_INFO isdbt_tmcc_info;
	IOCTL_ISDBT_SIGNAL_INFO isdbt_signal_info;
	IOCTL_ISDBT_LGE_TUNER_INFO lge_tuner_info;

	switch( cmd )
	{
		case IOCTL_ISDBT_POWER_ON: // with adc clk type
			mtv250_cb_ptr->tv_mode = DMB_TV_MODE_1SEG;
			mtv250_cb_ptr->country_band_type = RTV_COUNTRY_BAND_JAPAN;
			
			DMBMSG("[mtv] IOCTL_ISDBT_POWER_ON:  country_band_type(%d)\n", mtv250_cb_ptr->country_band_type);
	
			ret = mtv250_power_on();
			if(ret  != 0)
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}	
			break;

		case IOCTL_ISDBT_POWER_OFF:
			DMBMSG("[mtv] IOCTL_ISDBT_POWER_OFF\n");
			mtv250_power_off();
			break;

		case IOCTL_ISDBT_SCAN_FREQ:
			if (copy_from_user(&isdbt_ch_num, argp, sizeof(UINT)))
			{
				ret = -EFAULT;
				goto IOCTL_EXIT;
			}
			
			ret = rtvISDBT_ScanFrequency(isdbt_ch_num);
		//	DMBMSG("[mtv] ISDBT SCAN(%d) result: %d\n", isdbt_ch_num, ret);
			if(ret != RTV_SUCCESS)
			{
				ret = -EFAULT;	
				goto IOCTL_EXIT;
			}
			break;
			
		case IOCTL_ISDBT_SET_FREQ:
			if (copy_from_user(&isdbt_ch_num, argp, sizeof(UINT)))
			{
				ret = -EFAULT;
				goto IOCTL_EXIT;
			}
			
		//	DMBMSG("[mtv] IOCTL_ISDBT_SET_FREQ: %d\n", isdbt_ch_num);
			ret=rtvISDBT_SetFrequency(isdbt_ch_num);
			if(ret != RTV_SUCCESS)
			{
				DMBERR("[mtv] IOCTL_ISDBT_SET_FREQ error %d\n", ret);
				ret = -EFAULT;	
				goto IOCTL_EXIT;
			}
			break;		

		case IOCTL_ISDBT_GET_LOCK_STATUS:
		//	DMBMSG("[mtv] IOCTL_ISDBT_GET_LOCK_STATUS\n");
			lock_mask=rtvISDBT_GetLockStatus();
			if (copy_to_user(argp,&lock_mask, sizeof(UINT)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}
			break;

		case IOCTL_ISDBT_GET_TMCC:		
		//	DMBMSG("[mtv] IOCTL_ISDBT_GET_TMCC\n");
			rtvISDBT_GetTMCC(&isdbt_tmcc_info);
			if (copy_to_user(argp,&isdbt_tmcc_info, sizeof(RTV_ISDBT_TMCC_INFO)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}		
			break;

		case IOCTL_ISDBT_GET_SIGNAL_INFO:
		//	DMBMSG("[mtv] IOCTL_ISDBT_GET_SIGNAL_INFO\n");
			isdbt_signal_info.ber = rtvISDBT_GetBER(); 
			if(isdbt_signal_info.ber > 1500) 
				isdbt_signal_info.ber = 100000;
			
			isdbt_signal_info.cnr = rtvISDBT_GetCNR() / RTV_ISDBT_CNR_DIVIDER; 
			isdbt_signal_info.cnr = 10 * isdbt_signal_info.cnr;
			isdbt_signal_info.per = rtvISDBT_GetPER() * 3125; 
			isdbt_signal_info.rssi = rtvISDBT_GetRSSI() / RTV_ISDBT_RSSI_DIVIDER; 
			
			if (copy_to_user(argp, &isdbt_signal_info, sizeof(IOCTL_ISDBT_SIGNAL_INFO)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}
			break;	

		case IOCTL_ISDBT_LGE_GET_TUNER_INFO:
		//                                                  
	
			lge_tuner_info.LOCK = rtvISDBT_GetLockStatus() == RTV_ISDBT_CHANNEL_LOCK_OK ? 1: 0;

			lge_tuner_info.CNo = rtvISDBT_GetCNR() / RTV_ISDBT_CNR_DIVIDER; 
			lge_tuner_info.CNo = 10 * lge_tuner_info.CNo;
		
			lge_tuner_info.BER = rtvISDBT_GetBER(); 	
			if(lge_tuner_info.BER > 1500) 
				lge_tuner_info.BER = 100000;
				
			lge_tuner_info.PER = rtvISDBT_GetPER() * 3125; 

#if 1
			if(lge_tuner_info.LOCK == 0)
				lge_tuner_info.BER = lge_tuner_info.PER = 100000;
#endif
			
			lge_tuner_info.AGC= rtvISDBT_GetAGC(); 
			
			lge_tuner_info.RSSI= rtvISDBT_GetRSSI() / RTV_ISDBT_RSSI_DIVIDER; 

			// temp!!!!
			lge_tuner_info.start_ts_int_cnt = mtv250_cb_ptr->start_ts_int_cnt;
			lge_tuner_info.ovf_err_cnt = mtv250_cb_ptr->ovf_err_cnt;
			
			if (copy_to_user(argp, &lge_tuner_info, sizeof(IOCTL_ISDBT_LGE_TUNER_INFO)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}
			break;

		case IOCTL_ISDBT_START_TS:			 
			RTV_GUARD_LOCK;
			mtv250_cb_ptr->start_ts_int_cnt = 0;
			mtv250_cb_ptr->ovf_err_cnt = 0;
			mtv250_cb_ptr->max_read_jiffies = 0;
			rtv_StreamEnable();
			RTV_GUARD_FREE;
			
			break;

		case IOCTL_ISDBT_STOP_TS:
			rtvISDBT_DisableStreamOut();
			mtv250_reset_tsp();
			break;

		case IOCTL_TEST_DMB_POWER_ON:	
			DMBMSG("[mtv250_ioctl] IOCTL_TEST_DMB_POWER_ON\n");
			
			RTV_MASTER_CHIP_SEL;
			rtvOEM_PowerOn(1);
			mtv250_cb_ptr->is_power_on = TRUE;
			break;

		case IOCTL_TEST_DMB_POWER_OFF:			
			RTV_MASTER_CHIP_SEL;
			rtvOEM_PowerOn(0);
			mtv250_cb_ptr->is_power_on = FALSE;
			break;
			
		case IOCTL_REGISTER_READ:
			if(mtv250_cb_ptr->is_power_on == FALSE)
			{			
				DMBMSG("[mtv] Power Down state!Must Power ON\n");
				ret = -EFAULT;
				goto IOCTL_EXIT;
			}
			
			if (copy_from_user(&ioctl_register_access, argp, sizeof(RTV_IOCTL_REGISTER_ACCESS)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}		

			//DMBMSG("[mtv] IOCTL_REGISTER_READ: [%d] 0x%02X\n", ioctl_register_access.page, ioctl_register_access.Addr); 	

			switch( mtv250_cb_ptr->tv_mode )
			{
				case DMB_TV_MODE_TDMB:
				case DMB_TV_MODE_FM:
					switch( ioctl_register_access.page )
					{
						case 6: reg_page = 0x06; break; // OFDM
						case 7: reg_page = 0x09; break; // FEC
						case 8: reg_page = 0x0A; break; // FEC
						default: reg_page = mtv_reg_page_addr[ioctl_register_access.page];					
					}
					break;

				case DMB_TV_MODE_1SEG:
					switch( ioctl_register_access.page )
					{
						case 6: reg_page = 0x02; break; // OFDM
						case 7: reg_page = 0x03; break; // FEC
						default: reg_page = mtv_reg_page_addr[ioctl_register_access.page];					
					}
					break;
				default: break;
			}
					
			RTV_REG_MAP_SEL(reg_page); 
			ioctl_register_access.data[0] = RTV_REG_GET(ioctl_register_access.Addr);
			if (copy_to_user(argp, &ioctl_register_access, sizeof(RTV_IOCTL_REGISTER_ACCESS)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}
			break;

		case IOCTL_REGISTER_WRITE:
			if(mtv250_cb_ptr->is_power_on == FALSE)
			{			
				DMBMSG("[mtv] Power Down state!Must Power ON\n");
				ret = -EFAULT;
				goto IOCTL_EXIT;
			}
			
			if (copy_from_user(&ioctl_register_access, argp, sizeof(RTV_IOCTL_REGISTER_ACCESS)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}	

			switch( mtv250_cb_ptr->tv_mode )
			{
				case DMB_TV_MODE_TDMB:
				case DMB_TV_MODE_FM:
					switch( ioctl_register_access.page )
					{
						case 6: reg_page = 0x06; break; // OFDM
						case 7: reg_page = 0x09; break; // FEC
						case 8: reg_page = 0x0A; break; // FEC
						default: reg_page = mtv_reg_page_addr[ioctl_register_access.page];					
					}
					break;

				case DMB_TV_MODE_1SEG:
					switch( ioctl_register_access.page )
					{
						case 6: reg_page = 0x02; break; // OFDM
						case 7: reg_page = 0x03; break; // FEC
						default: reg_page = mtv_reg_page_addr[ioctl_register_access.page];					
					}
					break;
				default: break;
			}
					
			RTV_REG_MAP_SEL(reg_page); 
			RTV_REG_SET(ioctl_register_access.Addr, ioctl_register_access.data[0]);
			break;

		case IOCTL_TEST_GPIO_SET:
			if(mtv250_cb_ptr->is_power_on == FALSE)
			{			
				DMBMSG("[mtv] Power Down state!Must Power ON\n");
				ret = -EFAULT;
				goto IOCTL_EXIT;
			}
			
			if (copy_from_user(&gpio_info, argp, sizeof(RTV_IOCTL_TEST_GPIO_INFO)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}	
			gpio_set_value(gpio_info.pin, gpio_info.value);
			break;

		case IOCTL_TEST_GPIO_GET:
			if(mtv250_cb_ptr->is_power_on == FALSE)
			{			
				DMBMSG("[mtv] Power Down state!Must Power ON\n");
				ret = -EFAULT;
				goto IOCTL_EXIT;
			}
			
			if (copy_from_user(&gpio_info, argp, sizeof(RTV_IOCTL_TEST_GPIO_INFO)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}	
			
			gpio_info.value = gpio_get_value(gpio_info.pin);
			if(copy_to_user(argp, &gpio_info, sizeof(RTV_IOCTL_TEST_GPIO_INFO)))
			{
				ret = -EFAULT;		
				goto IOCTL_EXIT;
			}				
			break;
		
		default:
			DMBERR("[mtv] Invalid ioctl command: %d\n", cmd);
			ret = -ENOTTY;
			break;
	}

IOCTL_EXIT:

	return ret;
}