Beispiel #1
0
static int jpeg_enc_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
{

    int retValue;
    //unsigned int decResult;

    long timeout_jiff;
    unsigned int file_size, enc_result_code;
    //unsigned int _jpeg_enc_int_status;
    unsigned int jpeg_enc_wait_timeout = 0;
    unsigned int cycle_count;
    unsigned int ret ;


    unsigned int *pStatus;

    //JpegDrvEncParam cfgEnc;
    JPEG_ENC_DRV_IN cfgEnc;

    JpegDrvEncResult enc_result;
    //JpegDrvEncSrcCfg src_cfg;
    //JpegDrvEncDstCfg dst_cfg;
    //JpegDrvEncCtrlCfg ctrl_cfg;

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

    if(NULL == pStatus)
    {
        JPEG_WRN("Private data is null in flush operation. HOW COULD THIS HAPPEN ??\n");
        return -EFAULT;
    }
    switch(cmd)
    {

    case JPEG_ENC_IOCTL_RW_REG:
        //jpeg_drv_enc_rw_reg();
        break;

    // initial and reset JPEG encoder
    case JPEG_ENC_IOCTL_INIT:
        JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Init!!\n");

        retValue = jpeg_drv_enc_init();

        if(retValue == 0)
        {
            *pStatus = JPEG_ENC_PROCESS;
        }

        return retValue;

        break;

    case JPEG_ENC_IOCTL_WARM_RESET:
        JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Warm Reset\n");
        enc_result_code = jpeg_drv_enc_warm_reset();
        if (0 == enc_result_code)
        {
            return -EFAULT;
        }
        break;

    // configure the register
    case JPEG_ENC_IOCTL_CONFIG:
        JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Configure Hardware\n");
        if(*pStatus != JPEG_ENC_PROCESS)
        {
            JPEG_WRN("Permission Denied! This process can not access encoder");
            return -EFAULT;
        }


        if(enc_status == 0)
        {
            JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
            *pStatus = 0;
            return -EFAULT;
        }


        // copy input parameters
        if(copy_from_user(&cfgEnc, (void *)arg, sizeof(JPEG_ENC_DRV_IN)))
        {
            JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy from user error\n");
            return -EFAULT;
        }


        // 0. reset
        jpeg_drv_enc_reset();



        // 1. set src config
        //memset(&src_cfg, 0, sizeof(JpegDrvEncSrcCfg));

        //src_cfg.luma_addr = cfgEnc.srcBufferAddr;
        //if (cfgEnc.encFormat == NV12 || cfgEnc.encFormat == NV21)
        //{
        //    unsigned int srcChromaAddr = cfgEnc.srcChromaAddr;
        //    srcChromaAddr = TO_CEIL(srcChromaAddr, 128);    //((srcChromaAddr+127)&~127);
        //    src_cfg.chroma_addr = srcChromaAddr;
        //}
        //
        //src_cfg.width = cfgEnc.encWidth;
        //src_cfg.height = cfgEnc.encHeight;
        //src_cfg.yuv_format = cfgEnc.encFormat;


        // 1. set src config
        JPEG_MSG("[JPEGDRV]SRC_IMG: %x %x, DU:%x, fmt:%x!!\n", cfgEnc.encWidth, cfgEnc.encHeight, cfgEnc.totalEncDU, cfgEnc.encFormat);

        ret = jpeg_drv_enc_set_src_image(cfgEnc.encWidth, cfgEnc.encHeight, cfgEnc.encFormat, cfgEnc.totalEncDU) ;
        if(ret == 0) {
            JPEG_MSG("[JPEGDRV]JPEG Encoder set srouce image failed\n");
            return -EFAULT;
        }

        // 2. set src buffer info
        JPEG_MSG("[JPEGDRV]SRC_BUF: addr %x, %x, stride %x, %x!!\n", cfgEnc.srcBufferAddr, cfgEnc.srcChromaAddr ,cfgEnc.imgStride, cfgEnc.memStride);

        ret = jpeg_drv_enc_set_src_buf(cfgEnc.encFormat, cfgEnc.imgStride, cfgEnc.memStride, cfgEnc.srcBufferAddr, cfgEnc.srcChromaAddr);
        if(ret == 0) {
            JPEG_MSG("[JPEGDRV]JPEG Encoder set srouce buffer failed\n");
            return -EFAULT;
        }


        //if (0 == jpeg_drv_enc_src_cfg(src_cfg))
        //{
        //    JPEG_MSG("JPEG Encoder src cfg failed\n");
        //    return -EFAULT;
        //}

        // 3. set dst buffer info
        JPEG_MSG("[JPEGDRV]DST_BUF: addr:%x, size:%x, ofs:%x, mask:%x!!\n",cfgEnc.dstBufferAddr, cfgEnc.dstBufferSize, cfgEnc.dstBufAddrOffset, cfgEnc.dstBufAddrOffsetMask);

        ret = jpeg_drv_enc_set_dst_buff(cfgEnc.dstBufferAddr, cfgEnc.dstBufferSize, cfgEnc.dstBufAddrOffset, cfgEnc.dstBufAddrOffsetMask);
        if (ret == 0 ) {
            JPEG_MSG("[JPEGDRV]JPEG Encoder set dst buffer failed\n");
            return -EFAULT;
        }
        //memset(&dst_cfg, 0, sizeof(JpegDrvEncDstCfg));
        //
        //dst_cfg.dst_addr = cfgEnc.dstBufferAddr;
        //dst_cfg.dst_size = cfgEnc.dstBufferSize;
        //dst_cfg.exif_en = cfgEnc.enableEXIF;
        //
        //JPEG_MSG("go L:%d, %x, %d, %d !!", __LINE__, dst_cfg.dst_addr, dst_cfg.dst_size, dst_cfg.file_format);
        //
        //if (0 == jpeg_drv_enc_dst_buff(dst_cfg))
        //    return -EFAULT;


        // 4 .set ctrl config
        JPEG_MSG("[JPEGDRV]ENC_CFG: exif:%d, q:%d, DRI:%d !!\n", cfgEnc.enableEXIF, cfgEnc.encQuality, cfgEnc.restartInterval);

        jpeg_drv_enc_ctrl_cfg(cfgEnc.enableEXIF, cfgEnc.encQuality, cfgEnc.restartInterval);

        //memset(&ctrl_cfg, 0, sizeof(JpegDrvEncCtrlCfg));
        //
        //ctrl_cfg.quality = cfgEnc.encQuality;
        //ctrl_cfg.gmc_disable = cfgEnc.disableGMC;
        //ctrl_cfg.restart_interval = cfgEnc.restartInterval;
        //



        break;

    case JPEG_ENC_IOCTL_START:
        JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Start!!\n");
        if(*pStatus != JPEG_ENC_PROCESS)
        {
            JPEG_WRN("Permission Denied! This process can not access encoder");
            return -EFAULT;
        }
        if(enc_status == 0)
        {
            JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
            *pStatus = 0;
            return -EFAULT;
        }
        jpeg_drv_enc_start();
        break;

    case JPEG_ENC_IOCTL_WAIT:
        JPEG_MSG("[JPEGDRV][IOCTL] JPEG Encoder Wait!!\n");
        if(*pStatus != JPEG_ENC_PROCESS)
        {
            JPEG_WRN("Permission Denied! This process can not access encoder");
            return -EFAULT;
        }
        if(enc_status == 0)
        {
            JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
            *pStatus = 0;
            return -EFAULT;
        }
        if(copy_from_user(&enc_result, (void *)arg, sizeof(JpegDrvEncResult)))
        {
            JPEG_WRN("JPEG Encoder : Copy from user error\n");
            return -EFAULT;
        }

//#ifdef FPGA_VERSION

        //TODO:    ENC_DONE in REG_JPEG_ENC_INTERRUPT_STATUS need to set to 0 after read.
        jpeg_enc_wait_timeout = 0xFFFFFF;
#if 0
        do {
            _jpeg_enc_int_status = REG_JPEG_ENC_INTERRUPT_STATUS;
            jpeg_enc_wait_timeout--;
        } while(_jpeg_enc_int_status == 0 && jpeg_enc_wait_timeout > 0);

        if (jpeg_enc_wait_timeout == 0)
            JPEG_MSG("JPEG Encoder timeout\n");

        ret = jpeg_drv_enc_get_result(&file_size);

        JPEG_MSG("Result : %d, Size : %u, addres : 0x%x\n", ret, file_size, ioread32(JPG_CODEC_BASE + 0x120));

        if(_jpeg_enc_int_status != 1)
        {
            jpeg_drv_enc_dump_reg();
        }


#else


        //set timeout
        timeout_jiff = enc_result.timeout* HZ / 1000;
        JPEG_MSG("[JPEGDRV]JPEG Encoder Time Jiffies : %ld\n", timeout_jiff);
        if(jpeg_isr_enc_lisr()<0) {
            wait_event_interruptible_timeout(enc_wait_queue, _jpeg_enc_int_status, timeout_jiff);
            JPEG_MSG("[JPEGDRV]JPEG Encoder Wait done !!\n");
        } else {
            JPEG_MSG("[JPEGDRV]JPEG Encoder already done !!\n");
        }

        ret = jpeg_drv_enc_get_result(&file_size);

        JPEG_MSG("[JPEGDRV]Result : %d, Size : %u!!\n", ret, file_size);
        if(ret != 0)
        {
            jpeg_drv_enc_dump_reg();
        }


#endif


        cycle_count = jpeg_drv_enc_get_cycle_count();

        if(copy_to_user(enc_result.pFileSize, &file_size, sizeof(unsigned int)))
        {
            JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (file size)\n");
            return -EFAULT;
        }
        if(copy_to_user(enc_result.pResult, &ret, sizeof(unsigned int)))
        {
            JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (status)\n");
            return -EFAULT;
        }
        if(copy_to_user(enc_result.pCycleCount, &cycle_count, sizeof(unsigned int)))
        {
            JPEG_MSG("[JPEGDRV]JPEG Encoder : Copy to user error (cycle)\n");
            return -EFAULT;
        }
        break;

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

        if(enc_status == 0)
        {
            JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
            *pStatus = 0;
            return -EFAULT;
        }
        jpeg_drv_enc_deinit();
        *pStatus = 0;
        break;

    case JPEG_ENC_IOCTL_DUMP_REG:
        jpeg_drv_enc_dump_reg();
        break;

    default:
        JPEG_MSG("[JPEGDRV]JPEG ENC IOCTL NO THIS COMMAND\n");

    }
    return 0;
}
static int jpeg_enc_ioctl(unsigned int cmd, unsigned long arg, struct file *file)
{
    int retValue;
    long timeout_jiff;
    unsigned int fileSize, encResult;
    JPEG_ENC_DRV_IN inParams;
    JPEG_ENC_DRV_OUT outParams;

    unsigned int *pStatus;

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

    if(NULL == pStatus)
    {
        JPEG_WRN("Private data is null in flush operation. HOW COULD THIS HAPPEN ??\n");
        return -EFAULT;
    }
    
    switch(cmd)
    {       
        // initial and reset JPEG encoder
        case JPEG_ENC_IOCTL_INIT: 
            JPEG_MSG("JPEG Encoder Initial and Lock\n");
            
            retValue = jpeg_drv_enc_init();

            if(retValue == 0)
            {
                *pStatus = JPEG_ENC_PROCESS;
            }
            
            return retValue;   
     
            break;

        // Configure the register
        case JPEG_ENC_IOCTL_CONFIG:
            JPEG_MSG("JPEG Encoder Configure Hardware\n");
            if(*pStatus != JPEG_ENC_PROCESS)
            {
                JPEG_WRN("Permission Denied! This process can not access encoder");
                return -EFAULT;
            }

            if(enc_status == 0)
            {
                JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
                *pStatus = 0;
                return -EFAULT;
            }
            
            // copy input parameters
            if(copy_from_user(&inParams, (void *)arg, sizeof(JPEG_ENC_DRV_IN)))
            {
                JPEG_WRN("JPEG Encoder : Copy from user error\n");
                return -EFAULT;
            }

            if(inParams.allocBuffer)
            {
                dstBufferSize = inParams.dstBufferSize;
                dstUserVA = inParams.dstBufferAddr;
                dstBufferVA = dma_alloc_coherent(0, dstBufferSize, &dstBufferPA, GFP_KERNEL);
            }
            else
            {
                dstBufferSize = 0;
                dstBufferPA = (unsigned int)inParams.dstBufferAddr;
            }

           
            JPEG_MSG("JPEG Encoder Buffer Address : %d\n", (unsigned int)inParams.dstBufferAddr);
            JPEG_MSG("JPEG Encoder Buffer Size : %d\n", inParams.dstBufferSize);
            JPEG_MSG("JPEG Encoder Buffer Width : %d\n", inParams.dstWidth);
            JPEG_MSG("JPEG Encoder Buffer Height : %d\n", inParams.dstHeight);
            JPEG_MSG("JPEG Encoder Buffer Format : %d\n", inParams.dstFormat);
            JPEG_MSG("JPEG Encoder Buffer Quality : %d\n", inParams.dstQuality);

            // 0. reset 
            jpeg_drv_enc_reset();

            // 1. set dst address
            jpeg_drv_enc_set_dst_buffer_info(dstBufferPA, inParams.dstBufferSize, 0);

            // 2. set file format
            jpeg_drv_enc_set_file_format(inParams.enableEXIF);

            // 3. set quality
            jpeg_drv_enc_set_quality(inParams.dstQuality);

            // 4. single run
            jpeg_drv_enc_set_mode(0, 0);

            // 5. set sampling factor
            if(jpeg_drv_enc_set_sample_format_related(inParams.dstWidth, inParams.dstHeight, inParams.dstFormat))
            {
                JPEG_WRN("JPEG Encoder : Unvalid YUV Format\n");
                jpeg_drv_enc_deinit();
                return -EINVAL;
            }

            // 6. set sync reset bit
            jpeg_drv_enc_set_sync_reset(inParams.enableSyncReset);
            
            break;

        case JPEG_ENC_IOCTL_START:
            JPEG_MSG("JPEG Encoder Start\n");
            if(*pStatus != JPEG_ENC_PROCESS)
            {
                JPEG_WRN("Permission Denied! This process can not access encoder");
                return -EFAULT;
            }

            if(enc_status == 0)
            {
                JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
                *pStatus = 0;
                return -EFAULT;
            }
            //jpeg_reg_dump();
            jpeg_drv_enc_start();
            
            break;
            
        case JPEG_ENC_IOCTL_WAIT:
            if(*pStatus != JPEG_ENC_PROCESS)
            {
                JPEG_WRN("Permission Denied! This process can not access encoder");
                return -EFAULT;
            }

            if(enc_status == 0)
            {
                JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
                *pStatus = 0;
                return -EFAULT;
            }
            if(copy_from_user(&outParams, (void *)arg, sizeof(JPEG_ENC_DRV_OUT)))
            {
                JPEG_WRN("JPEG Encoder : Copy from user error\n");
                return -EFAULT;
            }

#ifdef FPGA_VERSION
            JPEG_MSG("Polling JPEG Encoder Status");

            do
            {
                _jpeg_enc_int_status = REG_JPEG_ENC_INTERRUPT_STATUS;
            } while(_jpeg_enc_int_status == 0);
#else
            //set timeout
            timeout_jiff = outParams.timeout* HZ / 1000;
            JPEG_MSG("JPEG Encoder Time Jiffies : %ld\n", timeout_jiff);   
            wait_event_interruptible_timeout(enc_wait_queue, _jpeg_enc_int_status, timeout_jiff);
#endif
            encResult = jpeg_drv_enc_get_result(&fileSize);

            JPEG_MSG("Result : %d, Size : %u, addres : 0x%x\n", encResult, fileSize, ioread32(JPG_CODEC_BASE + 0x120));
            if(encResult != 0)
            {
                jpeg_reg_dump();
            }
            
            if(copy_to_user(outParams.fileSize, &fileSize, sizeof(unsigned int)))
            {
                JPEG_WRN("JPEG Encoder : Copy to user error (file size)\n");
                return -EFAULT;
            }
            
            if(copy_to_user(outParams.result, &encResult, sizeof(unsigned int)))
            {
                JPEG_WRN("JPEG Encoder : Copy to user error (result)\n");
                return -EFAULT;            
            }
            
            if(dstBufferSize != 0)
            {
                JPEG_MSG("Copy Data to User\n");
                if(copy_to_user(dstUserVA, dstBufferVA, fileSize))
                {
                    JPEG_WRN("JPEG Encoder : Copy to user error (dstbuffer)\n");
                    return -EFAULT; 
                }
                dma_free_coherent(0, dstBufferSize, dstBufferVA, dstBufferPA);
            }
            
            break;
            
        case JPEG_ENC_IOCTL_DEINIT:
            // copy input parameters
            if(*pStatus != JPEG_ENC_PROCESS)
            {
                JPEG_WRN("Permission Denied! This process can not access encoder");
                return -EFAULT;
            }

            if(enc_status == 0)
            {
                JPEG_WRN("Encoder status is available, HOW COULD THIS HAPPEN ??");
                *pStatus = 0;
                return -EFAULT;
            }
            jpeg_drv_enc_deinit();
            *pStatus = 0;

            return 0;
    }
    
    return 0;
}