/* 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 * $RCSfile: io_mtstc.c,v $
 * $Revision: #1 $
 *---------------------------------------------------------------------------*/

/** @file io_mtstc.c
 *  This file contains implementation of exported APIs of STC module.
 */


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


#include "mtstc.h"
#include "stc_drvif.h"
#include "cb_low.h"
#include <linux/mtal_ioctl.h>

//-----------------------------------------------------------------------------
// Configurations
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Type definitions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Define definitions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------
#define USR_SPACE_ACCESS_VALIDATE_1ARG(arg)                 \
    if (!access_ok(VERIFY_READ, (void __user *)arg,         \
                   sizeof(int)))                            \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");     \
        return -1;                                          \
    }

#define COPY_FROM_USER_1ARG(ArgUsr, ArgKernel)              \
    if (copy_from_user(&ArgKernel, (void __user *)ArgUsr,   \
                       sizeof(int)))                        \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return -1;                                          \
    }

#define USR_SPACE_ACCESS_VALIDATE_2ARG(arg)                 \
    if (!access_ok(VERIFY_READ, (void __user *)arg,         \
                   sizeof(MTAL_IOCTL_2ARG_T)))              \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");     \
        return -1;                                          \
    }

#define COPY_FROM_USER_2ARG(ArgUsr, ArgKernel)              \
    if (copy_from_user(&ArgKernel, (void __user *)ArgUsr,   \
                       sizeof(MTAL_IOCTL_2ARG_T)))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return -1;                                          \
    }
#define USR_SPACE_ACCESS_VALIDATE_3ARG(arg)                 \
    if (!access_ok(VERIFY_READ, (void __user *)arg,         \
                   sizeof(MTAL_IOCTL_3ARG_T)))              \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");     \
        return -1;                                          \
    }

#define COPY_FROM_USER_3ARG(ArgUsr, ArgKernel)              \
    if (copy_from_user(&ArgKernel, (void __user *)ArgUsr,   \
                       sizeof(MTAL_IOCTL_3ARG_T)))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return -1;                                          \
    }
#define USR_SPACE_ACCESS_VALIDATE_4ARG(arg)                 \
    if (!access_ok(VERIFY_READ, (void __user *)arg,         \
                   sizeof(MTAL_IOCTL_4ARG_T)))              \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");     \
        return -1;                                          \
    }

#define COPY_FROM_USER_4ARG(ArgUsr, ArgKernel)              \
    if (copy_from_user(&ArgKernel, (void __user *)ArgUsr,   \
                       sizeof(MTAL_IOCTL_4ARG_T)))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return -1;                                          \
    }
#define USR_SPACE_ACCESS_VALIDATE_ARG(arg,type)                 \
    if (!access_ok(VERIFY_READ, (void __user *)arg,         \
                   sizeof(type)))              \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");     \
        return MTR_ERR_INV;                                          \
    }

#define USR_SPACE_ACCESS_VALIDATE_ARG_K(arg,size)                 \
    if (!access_ok(VERIFY_READ, (void __user *)arg,         \
                   size))              \
    {                                                       \
        printk("mtal_ioctl_video valid argument error\n");     \
        return MTR_ERR_INV;                                          \
    }   

#define COPY_TO_USER_ARG(ArgUsr, ArgKernel,type)              \
    if (copy_to_user((void __user *)ArgUsr, &ArgKernel,   \
                       sizeof(type)))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return MTR_ERR_INV;                                          \
    }
#define COPY_FROM_USER_ARG(ArgUsr, ArgKernel,type)              \
    if (copy_from_user(&ArgKernel,(void __user *)ArgUsr,    \
                       sizeof(type)))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return MTR_ERR_INV;                                          \
    }
    
#define COPY_TO_USER_DATA(ArgUsr, ArgKernel, size)              \
    if (copy_to_user((void __user *)ArgUsr, (void *)ArgKernel,   \
                       size))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return MTR_ERR_INV;                                          \
    }
#define COPY_FROM_USER_DATA(ArgUsr, ArgKernel, size)              \
    if (copy_from_user((void *)ArgKernel,(void __user *)ArgUsr,    \
                       size))          \
    {                                                       \
        printk("mtal_ioctl_video argument error\n");      \
        return MTR_ERR_INV;                                          \
    }


//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Static variables
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Static functions
//-----------------------------------------------------------------------------

static MT_RESULT_T _MTSTC_GetStcValue(unsigned long arg)
{
    MTAL_IOCTL_2ARG_T rArg;
    UINT8 ucStcId;
    UINT32* pu4StcValue;
    UINT32 u4StcValue;
    
    USR_SPACE_ACCESS_VALIDATE_2ARG(arg);
    COPY_FROM_USER_2ARG(arg, rArg);
    
    ucStcId = (UINT8)(rArg.ai4Arg[0]);
    pu4StcValue = (UINT32*)(rArg.ai4Arg[1]);
    
    u4StcValue = STC_GetStcValue(ucStcId);

    USR_SPACE_ACCESS_VALIDATE_ARG(pu4StcValue,UINT32);
    COPY_TO_USER_ARG(pu4StcValue, u4StcValue, UINT32);

    return MTR_OK;    
}

static MT_RESULT_T _MTSTC_SetStcValue(unsigned long arg)
{
    MTAL_IOCTL_2ARG_T rArg;
    UINT8 ucStcId;
    UINT32 u4StcValue;
    
    USR_SPACE_ACCESS_VALIDATE_2ARG(arg);
    COPY_FROM_USER_2ARG(arg, rArg);
    
    ucStcId = (UINT8)(rArg.ai4Arg[0]);
    u4StcValue = (UINT32)(rArg.ai4Arg[1]);

    STC_SetStcValue(ucStcId,u4StcValue);
    
    return MTR_OK;    
}

static MT_RESULT_T _MTSTC_SetSyncMode(unsigned long arg)
{
    MTAL_IOCTL_4ARG_T rArg;
    UINT8 ucStcId;
    UINT8 ucAdecId;
    UINT8 ucVdecId;
    UINT32 u4SyncMode;
    
    USR_SPACE_ACCESS_VALIDATE_4ARG(arg);
    COPY_FROM_USER_4ARG(arg, rArg);
    
    ucStcId = (UINT8)(rArg.ai4Arg[0]);
    ucAdecId = (UINT8)(rArg.ai4Arg[1]);
    ucVdecId = (UINT8)(rArg.ai4Arg[2]);
    u4SyncMode = (UINT32)(rArg.ai4Arg[3]);

    STC_SetSyncMode(ucStcId,ucAdecId,ucVdecId,u4SyncMode);
    
    return MTR_OK;    
}

static MT_RESULT_T _MTSTC_StartStc(unsigned long arg)
{
    INT32 i4Arg;
    UINT8 ucStcId;
    
    USR_SPACE_ACCESS_VALIDATE_ARG(arg,INT32);
    COPY_FROM_USER_ARG(arg, i4Arg,INT32);
    
    ucStcId = (UINT8)(i4Arg);

    STC_StartStc(ucStcId);
    
    return MTR_OK;    
}

static MT_RESULT_T _MTSTC_StopStc(unsigned long arg)
{
    INT32 i4Arg;
    UINT8 ucStcId;
    USR_SPACE_ACCESS_VALIDATE_ARG(arg,INT32);
    COPY_FROM_USER_ARG(arg, i4Arg,INT32);
    
    ucStcId = (UINT8)(i4Arg);

    STC_StopStc(ucStcId);
    
    return MTR_OK;    
}

static MT_RESULT_T _MTSTC_GetSrc(unsigned long arg)
{
    MTAL_IOCTL_2ARG_T rArg;
    UINT8 ucStcId;
    MTSTC_CLOCK* prStcInfo;
    MTSTC_CLOCK  rStcInfo;

    x_memset(&rStcInfo,0,sizeof(MTSTC_CLOCK));
    
    USR_SPACE_ACCESS_VALIDATE_2ARG(arg);
    COPY_FROM_USER_2ARG(arg, rArg);
    
    ucStcId = (UINT8)(rArg.ai4Arg[0]);
    prStcInfo = (MTSTC_CLOCK*)(rArg.ai4Arg[1]);

    STC_GetSrc(ucStcId,(STC_CLOCK*)&rStcInfo);

    USR_SPACE_ACCESS_VALIDATE_ARG(prStcInfo,MTSTC_CLOCK);
    COPY_TO_USER_ARG(prStcInfo, rStcInfo, MTSTC_CLOCK);

    return MTR_OK;    
}

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



int mtal_ioctl_mtstc(struct inode *inode, struct file *file, unsigned int cmd,
              unsigned long arg)
{
    int i4Ret = 0;

	switch (cmd)
	{
		case MTAL_IO_STC_STOP_STC:
		    return _MTSTC_StopStc(arg);
        case MTAL_IO_STC_SET_SYNC_MODE:
            return _MTSTC_SetSyncMode(arg);
        case MTAL_IO_STC_SET_STC_VALUE:
            return _MTSTC_SetStcValue(arg);        
        case MTAL_IO_STC_START_STC:
            return _MTSTC_StartStc(arg);
        case MTAL_IO_STC_GET_STC_VALUE:
            return _MTSTC_GetStcValue(arg);
        case MTAL_IO_STC_GET_SRC:
            return _MTSTC_GetSrc(arg);            
		default:
		    break;
	}
	return i4Ret;
}
