/* 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_debug.h"
#include "x_typedef.h"
#include "x_os.h"
#include "x_printf.h"
#include "x_assert.h"

#include "vdpomx_mod.h"

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

#include <asm/uaccess.h>

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

//#define VDPOMX_DEBUG
#define FBM_DBG_FLAG 1
#define VDP_DBG_FLAG 2

#ifdef VDPOMX_DEBUG
    unsigned int vdp_dbg;  
    #define VDPOMX_DBG(fmt...)    if(vdp_dbg & VDP_DBG_FLAG) {printk("%s:","[vdpomx_mod]");  printk(fmt);  }
#else
    #define VDPOMX_DBG(fmt...)
#endif

//extern INT32 _CB_PutEvent(CB_FCT_ID_T eFctId, INT32 i4TagSize, void *pvTag);

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

//static int _u4VdpOmxOpenCnt = 0;


extern INT32 _CB_PutEvent(CB_FCT_ID_T eFctId, INT32 i4TagSize, void *pvTag);

static int vdpomx_open(struct inode *inode, struct file *flip)
{
 //   VDPOMX_DBG("vdpomx_open\n");
#if 0
    if (0 == _u4VdpOmxOpenCnt)
    {
    }
    
    _u4VdpOmxOpenCnt ++;
#endif
    return 0;
}


static int vdpomx_release(struct inode *inode, struct file *flip)
{
    //(xxx *)flip->private_data;
//    VDPOMX_DBG("vdpomx_release\n");
#if 0
    _u4VdpOmxOpenCnt --;

    if (0 == _u4VdpOmxOpenCnt)
    {
    }
#endif    

    return 0;
}

//-----------------------------------------------------------------------------
/** vdec_ioctl.
 */
//-----------------------------------------------------------------------------
static int vdpomx_ioctl(struct inode *inode,
                struct file *file, 
                unsigned int cmd,
                 unsigned long arg)
{
    INT32  i4Ret = 0;
    //VDPOMX_DBG("vdpomx_ioctl\n");
    switch (cmd)
    {
        case VDPOMX_SET_OUT_REGION :
             i4Ret =   vdp_set_out_region((VDPOMX_SET_REGION_PARM_T*)arg); 
            VDPOMX_DBG("set out region\n");   
            break;
        case VDPOMX_SET_SRC_REGION :
             i4Ret =   vdp_set_src_region((VDPOMX_SET_REGION_PARM_T*)arg); 
            VDPOMX_DBG("set src region\n");   
            break;      
        case VDPOMX_SET_OUT_CHANNEL :
            i4Ret =   vdp_set_out_channel((VDPOMX_SET_CHANNEL_PARM_T*)arg); 
            VDPOMX_DBG("out channel\n");   
            break;         
        case VDPOMX_SET_DISPLAY_BUF :
            i4Ret =   vdp_set_display_buf((VDPOMX_SET_DISPLAY_BUF_PARM_T*)arg);
            VDPOMX_DBG("set display buffer\n");   
            break;        
        case VDPOMX_GET_CHANNEL_FREE :
            i4Ret =   vdp_get_channel_free((VDPOMX_GET_CHANNEL_FREE_PARM_T*)arg);
            VDPOMX_DBG("get channel free\n");   
            break;                 
        case VDPOMX_GET_BUF_LOCKED :
            i4Ret =   vdp_get_buf_locked((VDPOMX_GET_BUF_LOCKED_PARM_T*)arg);
            VDPOMX_DBG("get buffer locked\n");   
            break;
        case VDPOMX_CONNECT_VDEC:
            i4Ret =   vdp_connect_vdec((VDPOMX_CONNECT_VDEC_PARM_T*)arg);
            VDPOMX_DBG("connnect vdec\n");   
            break;
        case VDPOMX_SRM_SETTVMODE :
            i4Ret =   vdp_srm_settvmode(arg);
            VDPOMX_DBG("SRM_SetTvMode arg %d\n", arg);
            break;
        case VDPOMX_GET_FB_WIDTH_HEIGHT:
            i4Ret = vdp_get_fb_width_height_by_tag((VDPOMX_GET_FB_WIDTH_HEIGHT_PARM_T *)arg);
            VDPOMX_DBG("vdpomx ioctl VDPOMX_GET_FB_WIDTH_HEIGHT\n");
            break;

        default:
            break;
    }

    return i4Ret;    
}

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

struct file_operations vdpomx_fops = {
    .owner              = THIS_MODULE,
    .open               = vdpomx_open,
#ifdef USE_UNLOCK_IOCTL
    .unlocked_ioctl     = vdpomx_unlocked_ioctl,
#else
    .ioctl              = vdpomx_ioctl,
#endif
    .release            = vdpomx_release,
};


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


