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

/*-----------------------------------------------------------------------------
 *
 * $Author: danning.wang $
 * $Date: 2015/06/22 $
 * $RCSfile: dmx_emu.h,v $
 * $Revision: #2 $
 *
 *---------------------------------------------------------------------------*/

/** @file dmx_emu.h
 *  Demux driver - emulation header
 */

#ifndef DMX_EMU_H
#define DMX_EMU_H


//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#include "x_typedef.h"
#include "dmx_mm_if.h"

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

//#define EMU_5391


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

#if defined(CC_MT5890) || defined(CC_MT5891) || defined(CC_MT5886) || defined(CC_MT5893)
#define MAX_TS_INDEX                    4
#else //MT5882 and MT5883
#define MAX_TS_INDEX                    3
#endif

#define DEF_SRC_BUF_SIZE                0x100000

#define DEF_DST_BUF_SIZE                0x120000

#define DEF_BUF_FULL_GAP                0x30

#define TS_RP_BOUND                     (188 * 14)

#define TS_PACKET_SIZE                  188

#define MAX_KEY_NO                      DMX_DMEM_CA_KEY_NUM

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

typedef enum
{
    KEY_SELECT_EVEN = 0,
    KEY_SELECT_ODD,
    KEY_SELECT_RANDOM
} KEY_SELECT_T;

typedef struct
{
    UINT8 au1Even[32];
    UINT8 au1Odd[32];
} DMX_KEY_T;

typedef struct
{
    BOOL fgValid;
    UINT8 u1KeyIndex;
    UINT16 u2Pid;
} DMX_KEY_MAP_T;

typedef struct
{
    DMX_KEY_T arKey[DMX_DMEM_CA_KEY_NUM];
    DMX_KEY_MAP_T arKeyMap[DMX_NUM_PID_INDEX];
    KEY_SELECT_T eKeySelect;
    UINT16 u2KeyLen;
    UINT8 au1InitVector[16];
    DMX_CA_FEEDBACK_MODE_T eFeedbackMode;
    DMX_DESC_RTB_MODE_T eRtb;
    UINT8 au1SampleId[8];
    BOOL fgCTRResidual;
} AES_PARAM_T;

typedef struct
{
    UINT16 u2KeyLen;
    UINT8 au1Key[32];
    UINT8 au1InitVector[16];
    UINT8 au1SampleId[8];
    BOOL fgCbc;
    DMX_DRM_MODE_T eMode;
} DRM_PARAM_T;

typedef struct
{
    UINT16 u2Pid;
	UINT16 u2PidEx[4]; 
    UINT8 au1EvenKey[24];
    UINT8 au1OddKey[24];
    BOOL fgValid;
} ENPID_T;

typedef struct
{
    ENPID_T arEnPid[MAX_KEY_NO];
    KEY_SELECT_T eKeySelect;
    UINT8 au1InitVector[8];
    BOOL fg3Des;
    BOOL fgCbc;
    BOOL fgSCTE;
    DMX_CA_FEEDBACK_MODE_T eFeedbackMode;
    DMX_DESC_RTB_MODE_T eRtb;
    UINT8 au1SampleId[8];
    BOOL fgCTRResidual;
} DES_PARAM_T;

typedef struct
{
    UINT16 u2Pid;
    KEY_SELECT_T eKeySelect;
    UINT8 au1InitVector[16];
    UINT8 au1OddKey[16];
    UINT8 au1EvenKey[16];
    UINT8 u1KeyIdx;
    DMX_CA_FEEDBACK_MODE_T eFeedbackMode;
    DMX_DESC_RTB_MODE_T eRtb;
} SMS4_PARAM_T;
//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Function prototypes
//-----------------------------------------------------------------------------
EXTERN BOOL _DMX_EMU_DDI_Init(UINT8 u1TsIndex);
EXTERN BOOL _DMX_EMU_DDI_130byteEnable(BOOL fgEnable, UINT8 u1PktSize);
EXTERN BOOL _DMX_EMU_PalyBack_130byteEnable(BOOL fgEnable, UINT8 u1PktSize);
EXTERN BOOL _DMX_EMU_Framer_130byteEnable(UINT8 u1Framer, BOOL fgDemod130byteTs, BOOL fgInputEnable, UINT8 u1InputPktSize ,BOOL fgOutputEnable, UINT8 u1OutputPktSize);
EXTERN BOOL _DMX_EMU_DDI_SetBuffer(UINT32 u4Addr, UINT32 u4Size);
EXTERN BOOL _DMX_EMU_DDI_Transfer(UINT8 u1Type, UINT32 u4Addr, UINT32 u4Size);

EXTERN BOOL _DMX_EMU_SyncToggleBitInBypassCtrlWord(UINT8 u1TsIndex, UINT8 u1Pidx);

EXTERN BOOL _DMX_EMU_SetPktSize(UINT8 u1TsIndex, UINT16 u2PktSize, UINT16 u2ValidSize);

EXTERN BOOL _DDI_RECPULL_Transfer(UINT8 u1TsIndex, UINT32 u4Addr, UINT32 u4Size);
EXTERN BOOL _DDI_PULLTransfer(UINT8 u1Pidx, UINT32 u4Addr, UINT32 u4Size);

EXTERN BOOL _DMX_EncryptAesPacket(UINT8* pu1Buf, AES_PARAM_T* prParam, UINT8 u1ForceKeyId);
EXTERN BOOL _DMX_EncryptDesPacket(UINT8* pu1Buf, DES_PARAM_T* prParam);

#endif  // DMX_EMU_H



