/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/


#if defined(LINUX_TURNKEY_SOLUTION) && defined(CC_ENABLE_VDECOMX)

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------

#include <linux/cdev.h>
#include <linux/fs.h>          //For file_operations
#include "x_typedef.h"
#include "x_os.h"
#include "x_printf.h"
#include "x_assert.h"

#include "vencomx_mod.h"

#include <linux/cb_data.h>
#include <linux/version.h>
#include <linux/slab.h>
#include <linux/mm.h>

#include <asm/uaccess.h>
#include <linux/proc_fs.h>

#define DEFINE_IS_LOG	VOMX_IsLog
#include "x_debug.h"

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
#define USE_UNLOCK_IOCTL
#endif

//---------------------------------------------------------------------------
// Public functions
//---------------------------------------------------------------------------

static int vencomx_open(struct inode *inode, struct file *file)
{
    return 0;
}

static int vencomx_release(struct inode *inode, struct file *file)
{
    return 0;
}

//-----------------------------------------------------------------------------
/** venc_ioctl.
 */
//-----------------------------------------------------------------------------

static int vencomx_ioctl(struct inode *inode,
                struct file *file, 
                unsigned int cmd,
                 unsigned long arg)
{
    int i4Ret = VENCR_OK;

    switch (cmd)
    {
    default:
#ifndef CC_SUPPORT_VENC
        LOG(0, "vencomx_ioctl: not define CC_SUPPORT_VENC!\n");
#endif 

        LOG(1, "Unknown cmd(%d)\n", cmd);
        i4Ret = VENCR_NOT_OK;
        break;
#ifdef CC_SUPPORT_VENC
    case IO_VENC_COPYDATA:
        i4Ret=_VENCOMX_CopyData(arg);
	    break;
    case IO_VENC_INIT:
        i4Ret = _VENCOMX_Init();
        break;
    case IO_VENC_TERMINT:
        i4Ret = _VENCOMX_Termint();
        break;
    case IO_VENC_GETEVENT:
        i4Ret = _VENCOMX_Get_Event(arg);
        break;
    case IO_VENC_RESET:
        i4Ret = _VENCOMX_Reset(arg);
        break;
    case IO_VENC_SET:
        i4Ret = _VENCOMX_Set(arg);
        break;
    case IO_VENC_GET:
        i4Ret = _VENCOMX_Get(arg);
        break;
    case IO_VENC_OPEN:
        i4Ret = _VENCOMX_Open(arg);
        break;
    case IO_VENC_SET_PARAM:
        i4Ret = _VENCOMX_SetParam(arg);
        break;
    case IO_VENC_SET_CTRL:
        i4Ret = _VENCOMX_SetCtrl(arg);
        break;
    case IO_VENC_DRV_INIT:
        i4Ret = _VENCOMX_DrvInit(arg);
        break;
    case IO_VENC_RUN:
        i4Ret = _VENCOMX_Run(arg);
        break;
    case IO_VENC_STOP:
        i4Ret = _VENCOMX_Stop(arg);
        break;
    case IO_VENC_CLOSE:
        i4Ret = _VENCOMX_Close(arg);
        break;
    case IO_VENC_ENC_SEQHDR_SYNC:
        i4Ret = _VENCOMX_EncSeqHdrSync(arg);
        break;    
    case IO_VENC_ENC_FRAME_SYNC:
        i4Ret = _VENCOMX_EncFrameSync(arg);
        break;
    case IO_VENC_ENC_FRAME_ASYNC:
        i4Ret = _VENCOMX_EncFrameAsync(arg);
        break;
    case IO_VENC_CREATE_BUFFMGR:
        i4Ret = _VENCOMX_CreateBuffMgr(arg);
        break;
    case IO_VENC_DELETE_BUFFMGR:
        i4Ret = _VENCOMX_DeleteBuffMgr(arg);
        break;
    case IO_VENC_BUFFMGR_OBJ_GET:
        i4Ret = _VENCOMX_BUFMGR_OBJ_Get(arg);
        break;
    case IO_VENC_BUFFMGR_OBJ_FREE:
        i4Ret = _VENCOMX_BUFMGR_OBJ_Free(arg);
        break;
    case IO_VENC_FLUSH_FB:
        i4Ret = _VENCOMX_FlushFb(arg);
        break;
    case IO_VENC_INSERT_KEYFRM:
        i4Ret = _VENCOMX_InsertKeyframe(arg);
        break;
    case IO_VENC_REG_CB:
        i4Ret = _VENCOMX_RegCb(arg);
        break;
    case IO_VENC_UNLOCK_BUF:
        i4Ret = _VENCOMX_UnlockBuf(arg);
        break;
#endif // CC_SUPPORT_VENC
    }

    return i4Ret;
}


#ifdef USE_UNLOCK_IOCTL
long vencomx_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long param)
{
    return vencomx_ioctl(0, file, cmd, param);
}
#endif

struct file_operations vencomx_fops = {
    .owner              = THIS_MODULE,
    .open               = vencomx_open,
#ifdef USE_UNLOCK_IOCTL
    .unlocked_ioctl     = vencomx_unlocked_ioctl,
#else
    .ioctl              = vencomx_ioctl,
#endif
	//.mmap               = fbmomx_mmap,
    .release            = vencomx_release,
};

#endif


