/* 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: x_pvr.h,v $
 * $Revision: #1 $
 * $Date: 2015/11/10 $
 * $Author: dtvbm11 $
 *
 * Description:
 *         This header file contains Personal Video Recorder driver exported definitions.
 *---------------------------------------------------------------------------*/

#ifndef _X_PVR_H_
#define _X_PVR_H_

/*-----------------------------------------------------------------------------
                    include files
-----------------------------------------------------------------------------*/

#include "x_common.h"
#include "x_rm.h"
#include "x_mm_common.h"


/*-----------------------------------------------------------------------------
                    macros, defines, typedefs, enums
 -----------------------------------------------------------------------------*/

/* Get operations */
#define PVR_GET_TYPE_CAP_INFO          (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T)  0))
#define PVR_GET_TYPE_CTRL              (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T)  1))
#define PVR_GET_TYPE_BUFFER_INFO       (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T)  2))
#define PVR_GET_TYPE_BUFFER_STATUS     (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T)  3))
#define PVR_GET_TYPE_RESIDUAL_DATA     (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T)  4))
#define PVR_GET_TYPE_PVR_FBM_BUF_INFO  (RM_GET_TYPE_LAST_ENTRY + ((DRV_GET_TYPE_T)  5))


/* Set operations */
#define PVR_SET_TYPE_INIT_CTRL         (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  0))
#define PVR_SET_TYPE_DEINIT_CTRL       (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  1))
#define PVR_SET_TYPE_NFY_FCT           (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  2))
#define PVR_SET_TYPE_CTRL             ((RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  3)) | RM_SET_TYPE_ARG_NO_REF)
#define PVR_SET_TYPE_CTRL_OUTPUT      ((RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  4)) | RM_SET_TYPE_ARG_NO_REF)
#define PVR_SET_TYPE_UNLOCK_BUFFER     (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  5))
#define PVR_SET_TYPE_UNLOCK_METADATA  ((RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  6)) | RM_SET_TYPE_ARG_NO_REF)
#define PVR_SET_TYPE_FLUSH_BUFFER      (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  7))
#define PVR_SET_TYPE_EN_PIC_SEARCH    ((RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  8)) | RM_SET_TYPE_ARG_NO_REF)
#define PVR_SET_TYPE_SWAP              (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T)  9))
#define PVR_SET_TYPE_ENCRYPTION        (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 10))
#define PVR_SET_TYPE_IV                (RM_SET_TYPE_LAST_ENTRY + ((DRV_SET_TYPE_T) 11))

/* notification conditions */
typedef enum _PVR_NFY_COND_T
{
    PVR_COND_ERROR    = -2,
    PVR_COND_OVERFLOW = -1,
    PVR_COND_TICK     =  0

} PVR_NFY_COND_T;

/* Overflow reasons ((*x_pvr_nfy_fct)(e_nfy_cond == PVR_COND_OVERFLOW,
                                      ui4_data => overflow reason) ) */
#define PVR_OVERFLOW_REASON_TS_BUF_FULL         ((UINT32) 1)
#define PVR_OVERFLOW_REASON_METADATA_BUF_FULL   ((UINT32) 2)

/* Packet swap type */
typedef enum _PVR_SWAP_TYPE_T
{
    PVR_SWAP_TYPE_DISABLE = 0,
    PVR_SWAP_TYPE_NULL_PKT,
    PVR_SWAP_TYPE_SECT_LIST

}   PVR_SWAP_TYPE_T;

/* Frame type mask (PVR_CTRL_CONFIG_T.ui1_nfy_frm_mask) */
#define PVR_FRAME_TYPE_MASK_I       MAKE_BIT_MASK_8(0)
#define PVR_FRAME_TYPE_MASK_P       MAKE_BIT_MASK_8(1)
#define PVR_FRAME_TYPE_MASK_B       MAKE_BIT_MASK_8(2)

/* (*x_pvr_nfy_fct) */
typedef BOOL (*x_pvr_nfy_fct) (VOID*              pv_tag,
                               PVR_NFY_COND_T     e_nfy_cond,
                               UINT32             ui4_data);


/*-----------------------------------------------------------------------------
                    structures
-----------------------------------------------------------------------------*/
typedef struct _PVR_CAP_INFO_T
{
    UINT32      ui4_align_buf_start;
    UINT32      ui4_align_buf_size;
    UINT32      ui4_max_pid_num;
    UINT32      ui4_max_encrypt_pid_num;
    UINT32      ui4_max_encrypt_key_num;

} PVR_CAP_INFO_T;

/* PVR Ctrl initial config (PVR_SET_TYPE_INIT_CTRL) */
typedef struct _PVR_CTRL_CONFIG_T
{
    UINT32              ui4_tick_period;
    UINT8               ui1_nfy_frm_mask;
    SIZE_T              z_buf_size;
    VOID*               pv_buf_phy;
    VOID*               pv_buf_mw;

} PVR_CTRL_CONFIG_T;

/* Notify info setting (PVR_SET_TYPE_NFY_FCT) */
typedef struct _PVR_NFY_INFO_T
{
    VOID*               pv_tag;
    x_pvr_nfy_fct       pf_nfy;

} PVR_NFY_INFO_T;

/* Driver pre-allocated buffer info structure (PVR_GET_TYPE_BUFFER_INFO) */
typedef struct _PVR_BUFFER_INFO_T
{
    UINT8*      pui1_buf_addr;
    SIZE_T      z_buf_len;

} PVR_BUFFER_INFO_T;

/* Buffer status info structure (PVR_GET_TYPE_BUFFER_STATUS) */
typedef struct _PVR_BUFFER_STATUS_T
{
    const UINT8*      pui1_write_ptr;
    const UINT8*      pui1_read_ptr;

} PVR_BUFFER_STATUS_T;

/* Residual data without notifying in driver info structure (PVR_GET_TYPE_RESIDUAL_DATA) */
typedef struct _PVR_RESIDUAL_DATA_INFO_T
{
    UINT8       *pui1_addr;
    SIZE_T      z_len;

}   PVR_RESIDUAL_DATA_INFO_T;

/* Unlock buffer info (PVR_SET_TYPE_UNLOCK_BUFFER) */
typedef struct _PVR_UNLOCK_BUFFER_INFO_T
{
    UINT8*      pui1_data_addr;
    SIZE_T      z_data_len;

} PVR_UNLOCK_BUFFER_INFO_T;

/* Section data */
typedef struct _PVR_SECT_DATA_T
{
    VOID*       pv_sect_data;
    SIZE_T      z_sect_len;

} PVR_SECT_DATA_T;

/* Swap data when PVR_SWAP_INFO_T.e_swap_type == PVR_SWAP_TYPE_SECT_LIST */
typedef struct _PVR_SWAP_SECT_LIST_T
{
    UINT16                   ui2_num_sect;
    PVR_SECT_DATA_T*         at_sects;

} PVR_SWAP_SECT_LIST_T;

/* Packet swap info structure (PVR_SET_TYPE_SWAP) */
typedef struct _PVR_SWAP_INFO_T
{
    PVR_SWAP_TYPE_T          e_swap_type;

    union
    {
        /* PVR_SWAP_TYPE_DISABLE */
        /* nothing */

        /* PVR_SWAP_TYPE_NULL_PKT */
        /* nothing */

        /* PVR_SWAP_TYPE_SECT_LIST */
        PVR_SWAP_SECT_LIST_T    t_sect_list;
    } u;

}   PVR_SWAP_INFO_T;

/* Metadata info */
typedef struct _PVR_METADATA_T
{
    PVR_TICK_DETAIL_T        t_tick_detail;

} PVR_METADATA_T;

/* Tick data info (*x_pvr_nfy_fct)(PVR_COND_TICK, ui4_data->(PVR_TICK_DATA_T *)) */
typedef struct _PVR_TICK_DATA_T
{
    VOID*                    pv_metadata_tag;
    const PVR_METADATA_T*    pt_metadata;
    const UINT8*             pui1_data;
    UINT32                   ui4_pkt_count;

} PVR_TICK_DATA_T;


#endif /* _X_PVR_H_ */


