int32_t cmdqRecWriteSecure(cmdqRecHandle handle, uint32_t addr,
			CMDQ_SEC_ADDR_METADATA_TYPE type,
			uint32_t baseHandle,
			uint32_t offset,
			uint32_t size,
			uint32_t port)
{
#ifdef CMDQ_SECURE_PATH_SUPPORT
	int32_t status;
	int32_t writeInstrIndex;
	cmdqSecAddrMetadataStruct metadata;
	const uint32_t mask = 0xFFFFFFFF;

	/* append command*/
	status = cmdqRecWrite(handle, addr, baseHandle, mask);
	if (0 != status) {
		return status;
	}

	/* append to metadata list */
	writeInstrIndex = (handle->blockSize) / CMDQ_INST_SIZE - 1; /* start from 0 */

	memset(&metadata, 0, sizeof(cmdqSecAddrMetadataStruct));
	metadata.instrIndex = writeInstrIndex;
	metadata.type = type;
	metadata.baseHandle = baseHandle;
	metadata.offset = offset;
	metadata.size = size;
	metadata.port = port;

	status = cmdq_append_addr_metadata(handle, &metadata);

	return 0;
#else
	CMDQ_ERR("%s failed since not support secure path\n", __func__);
	return -EFAULT;
#endif
}
static int jpeg_dec_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
{
    unsigned int*       pStatus;
    unsigned int        decResult;
    long timeout_jiff;
    JPEG_DEC_DRV_IN dec_params;
    JPEG_DEC_CONFIG_ROW dec_row_params ;
    JPEG_DEC_CONFIG_CMDQ cfg_cmdq_params ;
    
    unsigned int irq_st = 0;
    unsigned int i = 0;
    //unsigned int timeout = 0x1FFFFF;
    
    JPEG_DEC_DRV_OUT outParams;

    pStatus = (unsigned int*)file->private_data;

    if (NULL == pStatus)
    {
        JPEG_MSG("[JPEGDRV]JPEG Decoder: Private data is null in flush operation. SOME THING WRONG??\n");
        return -EFAULT;
    }
    switch(cmd)
    { 
            // initial and reset JPEG encoder
        case JPEG_DEC_IOCTL_INIT:   /* OT:OK */
            JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Init!!\n");
            if(jpeg_drv_dec_init() == 0)
            {
                *pStatus = JPEG_DEC_PROCESS;
            }
            break;
            
        case JPEG_DEC_IOCTL_RESET:  /* OT:OK */
            JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Reset!!\n");
            jpeg_drv_dec_reset();
            break;
            
        case JPEG_DEC_IOCTL_CONFIG:
            JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Configration!!\n");
            if(*pStatus != JPEG_DEC_PROCESS)
            {
                JPEG_MSG("[JPEGDRV]Permission Denied! This process can not access decoder\n");
                return -EFAULT;
            }
            if(dec_status == 0)
            {
                JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
                *pStatus = 0;
                return -EFAULT;
            }
            if(copy_from_user(&dec_params, (void *)arg, sizeof(JPEG_DEC_DRV_IN)))
            {
                JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
                return -EFAULT;
            }
            //_jpeg_dec_dump_reg_en = dec_params.regDecDumpEn;
            if(dec_params.decodeMode == JPEG_DEC_MODE_MCU_ROW)
                _jpeg_dec_mode = 1;
            else 
                _jpeg_dec_mode = 0;
               
            if (jpeg_drv_dec_set_config_data(&dec_params) < 0)
                return -EFAULT;

            break;
            
            case JPEG_DEC_IOCTL_FLUSH_CMDQ :
              
              JPEG_MSG("[JPEGDRV]enter JPEG BUILD CMDQ !!\n");
              if(*pStatus != JPEG_DEC_PROCESS)
              {
                  JPEG_MSG("[JPEGDRV]Permission Denied! This process can not access decoder\n");
                  return -EFAULT;
              }
              if(dec_status == 0)
              {
                  JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
                  *pStatus = 0;
                  return -EFAULT;
              }
              if(copy_from_user(&cfg_cmdq_params, (void *)arg, sizeof(JPEG_DEC_CONFIG_CMDQ)))
              {
                  JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
                  return -EFAULT;
              }
              JPEG_MSG("[JPEGDRV]JPEG CREATE CMDQ !!\n");
              
              cmdqRecCreate(CMDQ_SCENARIO_JPEG_DEC, &jpegCMDQ_handle) ;
              
              cmdqRecWait(jpegCMDQ_handle, CMDQ_EVENT_JPEG_DEC_EOF) ; 
              
              for( i = 0 ; i < cfg_cmdq_params.goNum ; i++ ){
                JPEG_MSG("[JPEGDRV]JPEG gen CMDQ %x %x %x %x  !!\n", cfg_cmdq_params.pauseMCUidx[i], 
                cfg_cmdq_params.decRowBuf0[i],  cfg_cmdq_params.decRowBuf1[i], cfg_cmdq_params.decRowBuf2[i]);
                cmdqRecWrite(jpegCMDQ_handle,  0x18004170 /*REG_ADDR_JPGDEC_PAUSE_MCU_NUM*/, cfg_cmdq_params.pauseMCUidx[i]-1 , 0xFFFFFFFF) ;    
                cmdqRecWrite(jpegCMDQ_handle,  0x18004140 /*REG_ADDR_JPGDEC_DEST_ADDR0_Y */,    cfg_cmdq_params.decRowBuf0[i] , 0xFFFFFFFF) ;    
                cmdqRecWrite(jpegCMDQ_handle,  0x18004144 /*REG_ADDR_JPGDEC_DEST_ADDR0_U */,    cfg_cmdq_params.decRowBuf1[i] , 0xFFFFFFFF) ;    
                cmdqRecWrite(jpegCMDQ_handle,  0x18004148 /*REG_ADDR_JPGDEC_DEST_ADDR0_V */,    cfg_cmdq_params.decRowBuf2[i] , 0xFFFFFFFF) ;    

                JPEG_MSG("[JPEGDRV]JPEG gen CMDQ go!!\n");
                //trigger 
                cmdqRecWrite(jpegCMDQ_handle, 0x18004274 /*REG_ADDR_JPGDEC_INTERRUPT_STATUS*/ ,    BIT_INQST_MASK_PAUSE , 0xFFFFFFFF) ;    
                
                JPEG_MSG("[JPEGDRV]JPEG gen CMDQ wait!!\n");
                //wait frame done
                cmdqRecWait(jpegCMDQ_handle, CMDQ_EVENT_JPEG_DEC_EOF) ; 
                //cmdqRecPoll(jpegCMDQ_handle, 0x18004274 /*REG_ADDR_JPGDEC_INTERRUPT_STATUS*/ ,    BIT_INQST_MASK_PAUSE , 0x0010) ;    
              }
              
              JPEG_MSG("[JPEGDRV]JPEG flush CMDQ start!!\n");
              cmdqRecFlush(jpegCMDQ_handle) ;
              JPEG_MSG("[JPEGDRV]JPEG flush CMDQ end!!\n");
              
              cmdqRecDestroy(jpegCMDQ_handle) ;    
              JPEG_MSG("[JPEGDRV]JPEG destroy CMDQ end!!\n");

              break;
            
            case JPEG_DEC_IOCTL_RESUME:
              if(*pStatus != JPEG_DEC_PROCESS)
              {
                  JPEG_MSG("[JPEGDRV]Permission Denied! This process can not access decoder\n");
                  return -EFAULT;
              }
              if(dec_status == 0)
              {
                  JPEG_MSG("[JPEGDRV]JPEG Decoder is unlocked!!");
                  *pStatus = 0;
                  return -EFAULT;
              }
              if(copy_from_user(&dec_row_params, (void *)arg, sizeof(JPEG_DEC_CONFIG_ROW)))
              {
                  JPEG_MSG("[JPEGDRV]JPEG Decoder : Copy from user error\n");
                  return -EFAULT;
              }

              JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Resume, [%d] %x %x %x !!\n", dec_row_params.pauseMCU -1,dec_row_params.decRowBuf[0], dec_row_params.decRowBuf[1], dec_row_params.decRowBuf[2]);              
              
              jpeg_drv_dec_set_dst_bank0( dec_row_params.decRowBuf[0], dec_row_params.decRowBuf[1], dec_row_params.decRowBuf[2]);
      
              jpeg_drv_dec_set_pause_mcu_idx(dec_row_params.pauseMCU -1) ;
            
              // lock CPU to ensure irq is enabled after trigger HW
              spin_lock(&jpeg_dec_lock);
              jpeg_drv_dec_resume(BIT_INQST_MASK_PAUSE);
              spin_unlock(&jpeg_dec_lock);
            break;

        case JPEG_DEC_IOCTL_START:    /* OT:OK */
            JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Start!!\n");
            
            //Debug: printk("0xF0: 0x%08x\n", *(volatile unsigned int*)(JPEG_DEC_BASE + 0xF0));
            
            jpeg_drv_dec_start();
            break;
            
        case JPEG_DEC_IOCTL_WAIT:
            if(*pStatus != JPEG_DEC_PROCESS)
            {
                JPEG_WRN("Permission Denied! This process can not access decoder");
                return -EFAULT;
            }
            if(dec_status == 0)
            {
                JPEG_WRN("Decoder status is available, HOW COULD THIS HAPPEN ??");
                *pStatus = 0;
                return -EFAULT;
            }           
            if(copy_from_user(&outParams, (void *)arg, sizeof(JPEG_DEC_DRV_OUT)))
            {
                JPEG_WRN("JPEG Decoder : Copy from user error\n");
                return -EFAULT;
            }

            //set timeout
            timeout_jiff = outParams.timeout* HZ / 1000;
            //JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Wait Resume Time Jiffies : %ld\n", timeout_jiff);   
#ifdef FPGA_VERSION
//#if 1

            JPEG_MSG("[JPEGDRV]Polling JPEG Status");

            do
            {
                _jpeg_dec_int_status = REG_JPGDEC_INTERRUPT_STATUS;
            } while(_jpeg_dec_int_status == 0);
#else

            //if(outParams.timeout >= 5000){
            //       
            //  JPEG_MSG("Polling JPEG Status");              
            //  do
            //  {
            //      _jpeg_dec_int_status = REG_JPGDEC_INTERRUPT_STATUS;
            //     timeout--;
            //  } while(_jpeg_dec_int_status == 0 && timeout != 0);                              
            //  if(timeout == 0) JPEG_MSG("Polling JPEG Status TIMEOUT!!\n");              
            //}else
            if(jpeg_isr_dec_lisr()<0){
              //JPEG_MSG("wait JPEG irq\n");
              wait_event_interruptible_timeout(dec_wait_queue, _jpeg_dec_int_status, timeout_jiff);              
              JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Enter IRQ Wait Done!!\n");
              //printk("[JPEGDRV]wait JPEG irq done\n");
            }else{
              JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Enter IRQ Wait Already Done!!\n"); 
              //printk("[JPEGDRV]JPEG decoder already done\n"); 
            }
#endif
            
            decResult = jpeg_drv_dec_get_result();

            //jpeg_drv_dec_dump_key_reg();
            
            if(decResult >= 2)
            {
                JPEG_MSG("[JPEGDRV]Decode Result : %d, status %x!\n", decResult, _jpeg_dec_int_status );            
                jpeg_drv_dec_dump_key_reg();  
                //jpeg_drv_dec_dump_reg();
                jpeg_drv_dec_reset();
            }
            irq_st = _jpeg_dec_int_status ;
            decResult = decResult | (irq_st<<8) ;
            _jpeg_dec_int_status = 0;    
            if(copy_to_user(outParams.result, &decResult, sizeof(unsigned int)))
            {
                JPEG_WRN("JPEG Decoder : Copy to user error (result)\n");
                return -EFAULT;            
            }
    
            break;

        case JPEG_DEC_IOCTL_BREAK:
            if (jpeg_drv_dec_break() < 0)
                return -EFAULT;
            break;
            
        case JPEG_DEC_IOCTL_DUMP_REG:
            JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder DUMP REGISTER !!\n");
            jpeg_drv_dec_dump_reg();
            break;

        case JPEG_DEC_IOCTL_DEINIT:
            JPEG_MSG("[JPEGDRV][IOCTL] JPEG Decoder Deinit !!\n");
            // copy input parameters
            if(*pStatus != JPEG_DEC_PROCESS)
            {
                JPEG_ERR("Permission Denied! This process can not access encoder");
                return -EFAULT;
            }

            if(dec_status == 0)
            {
                JPEG_ERR("Encoder status is available, HOW COULD THIS HAPPEN ??");
                *pStatus = 0;
                return -EFAULT;
            }
            jpeg_drv_dec_deinit();
            *pStatus = 0;   
            break;
#ifdef FOR_COMPILE            
        case JPEG_DEC_IOCTL_RW_REG: /* OT:OK */
            jpeg_drv_dec_rw_reg();
            break;
#endif
        default:
            JPEG_ERR("JPEG DEC IOCTL NO THIS COMMAND\n");
            break;
    }
    return 0;
}