/*
 * Copyright 2010 Trident Microsystems (Far East) Ltd. All rights reserved.
 * All prior copyright rights in this work and the accompanying software
 * products transferred to Trident Microsystems (Far East) Ltd. by written
 * agreement.
 */
/*****************************************************************************/
/* Copyright (c) 2010 NXP B.V.  All rights are 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 NXP 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      */
/* OWNER 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.                                                                   */
/*                                                                           */
/*****************************************************************************/
//
// FILENAME:     tmDbFormats.h
//
// DESCRIPTION:  Common formats for digital broadcasting
//
// DOCUMENT REF: 
//
// NOTES:
//
//-----------------------------------------------------------------------------


#ifndef     _TM_DBFORMATS_H
#define     _TM_DBFORMATS_H


//-----------------------------------------------------------------------------
// Standard include files:
//-----------------------------------------------------------------------------

#include <tmNxTypes.h>
//-----------------------------------------------------------------------------
// Project include files:
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------


#if        defined(__cplusplus)
extern "C"
{
#endif  // defined(__cplusplus)

typedef enum
{
    tmDbDssSectionMpg = 0,
    tmDbDssSectionSpg,
    tmDbDssSectionSegm,
    tmDBDssSectionApgd,
    tmDbDssSectionCssm,
    tmDbDssSectionPism,
    tmDbDssSectionDip,
    tmDbDssSectionPip,
    tmDbDssSectionCat,
    tmDbDssSectionMpt
} tmDbDssSectionType_t;
    
typedef enum
{
    tmDbModeDSS  =  0x00000001,     // Broadcasting is in DSS mode
    tmDbModeDVB  =  0x00000002,     // Broadcasting is in DVB mode
    tmDbModeATSC =  0x00000004      // Broadcasting is in ATSC mode
} tmDbMode_t;

typedef enum
{
    tmDbHsfValue8Byte,      // 8 byte Value filtering
    tmDbHsfValue12Byte,     // 12 byte Value filtering
    tmDbHsfRange8Byte,      // 8 byte Range filtering - Kirch type 
    tmDbHsfValue16Byte      // 16 byte Value filtering
} tmDbHsfType_t;

/* deprecated due to endianness ambiguity */
typedef struct _tmDbDescIV_t
{
    UInt32  CBC[2];             // descrambler/copy protect CBC
    UInt32  OFB[2];             // descrambler/copy protect OFB
} tmDbDescIV_t, *ptmDbDescIV_t;

typedef struct _tmDbDescSysIV_t
{
    UInt8  CBC[8];             // descrambler/copy protect CBC
    UInt8  OFB[8];             // descrambler/copy protect OFB
} tmDbDescSysIV_t, *ptmDbDescSysIV_t;


// These values are based on VMSP hardware description.
// In future if the description changes, instead of manipulating
// these values, change the code that uses these values.
typedef enum _tmDbDescMode_t
{
    tmDbDescModeTDES_FB_ECB_RB_NONE_SB_NONE,
    tmDbDescModeTDES_FB_ECB_RB_OFBIV_SB_NONE,
    tmDbDescModeTDES_FB_ECB_RB_OFBPB_SB_NONE,
    tmDbDescModeTDES_FB_ECB_RB_OFBIV_SB_OFBIV,
    tmDbDescModeTDES_FB_ECB_RB_OFBPB_SB_OFBIV,
    tmDbDescModeTDES_FB_CBC_RB_NONE_SB_NONE,
    tmDbDescModeTDES_FB_CBC_RB_OFBIV_SB_NONE,
    tmDbDescModeTDES_FB_CBC_RB_OFBPB_SB_NONE,
    tmDbDescModeTDES_FB_CBC_RB_OFBIV_SB_OFBIV,
    tmDbDescModeTDES_FB_CBC_RB_OFBPB_SB_OFBIV,

    tmDbDescModeSDES_FB_ECB_RB_NONE_SB_NONE,
    tmDbDescModeSDES_FB_ECB_RB_OFBIV_SB_NONE,
    tmDbDescModeSDES_FB_ECB_RB_OFBPB_SB_NONE,
    tmDbDescModeSDES_FB_ECB_RB_OFBIV_SB_OFBIV,
    tmDbDescModeSDES_FB_ECB_RB_OFBPB_SB_OFBIV,
    tmDbDescModeSDES_FB_CBC_RB_NONE_SB_NONE,
    tmDbDescModeSDES_FB_CBC_RB_OFBIV_SB_NONE,
    tmDbDescModeSDES_FB_CBC_RB_OFBPB_SB_NONE,
    tmDbDescModeSDES_FB_CBC_RB_OFBIV_SB_OFBIV,
    tmDbDescModeSDES_FB_CBC_RB_OFBPB_SB_OFBIV,
    
    tmDbDescModeAES_FB_ECBdec_RB_ECBdec_SB_ECBdec,// Descrambling in AES mode ECB decode
    tmDbDescModeAES_FB_ECBenc_RB_ECBenc_SB_ECBenc,// Descrambling in AES mode ECB encode
    tmDbDescModeAES_FB_CBCdec_RB_CBCdec_SB_CBCdec,// Descrambling in AES mode CBC decode, null IV
    tmDbDescModeAES_FB_CBCenc_RB_CBCenc_SB_CBCenc,// Descrambling in AES mode CBC encode, null IV
    tmDbDescModeAES_FB_CBCdec_RB_NONE_SB_NONE,    // Descrambling in AES mode CBC for CI+
    
    tmDbDescModeDVB,    // Descrambling in DVB mode
    tmDbDescModeICAM,   // Descrambling in ICAM mode
    tmDbDescModeMulti2, // Descrambling in Multi2 mode
    tmDbDescModeNone    // No descrambling mode
} tmDbDescMode_t;

typedef enum
{
    tmDbOrderBit7Msb,      // Bit 7 is MSb (Most Significant Bit)
    tmDbOrderBit0Msb       // Bit 0 is MSb
}tmDbFrmrOrder_t;

typedef struct _tmDbClock_t
{
    UInt32 clockHi;                  // Bit 33 of clock
    UInt32 clockLo;                  // Bits 0-32 of clock
    UInt32 clockExtn;                // 9 bits clock extn value
} tmDbClock_t, *ptmDbClock_t;

typedef struct _tmDbSectionFilter_t
{
    UInt8  *value;        // 12 byte value that the section should be
                          // matched with
    UInt8  *mask;         // 12 byte mask.  A 1 matches the respective 
                          // bit of value with the section, 0 ignores
    UInt8  *mode;         // 12 byte mode.  0 is positive mode, and 1 
                          // is negative mode.  All positive modes 
                          // should match, and at least one negative 
                          // mode fail.
    UInt32 filterLen;     // Filter Length in number of bytes
    tmDbDssSectionType_t dssSectionType; // DSS Section Filter Type
} tmDbSectionFilter_t, *ptmDbSectionFilter_t;

typedef struct _tmDbSectionRange_t
{
    UInt8 minValue[8];          // Minimum value
    UInt8 maxValue[8];          // Maximum value
    UInt8 mask[8];              // Mask
    UInt8 CompFunc;             // Compare function, inner range or outer range
} tmDbSectionRange_t, *ptmDbSectionRange_t;


#define TMDB_DESC_ODDKEY    0
#define TMDB_DESC_EVENKEY   1
#define TMDB_DESC_BOTHKEY   2
#define TMDB_DESC_PKKEY     3
#define TMDB_DESC_FKKEY     4
#define TMDB_DESC_CPCWKEY   5
#define TMDB_DESC_ODDIV     6
#define TMDB_DESC_EVENIV    7
#define TMDB_DESC_BOTHIV    8

typedef struct _tmDbDescKey_t
{
    UInt8 odd[24];  // 8/16/24 bytes of Odd field (de-/encrypted) and PK, FK, CPCW keys
    UInt8 even[24]; // 8 (DVB, Multi2, SDES)/16(AES)/24 (TDES) bytes of Even field (decrypted/encrypted)
} tmDbDescKey_t, *ptmDbDescKey_t;

typedef enum
{
    tmDbScrStatNone,        // Not scrambled
    tmDbScrStatOdd,         // Scrambled with odd key
    tmDbScrStatEven,        // Scrambled with even key
    tmDbScrStatUser,        // User defined scrambling
    tmDbScrStatUnknown      // Scrambling status unknown
} tmDbScrStat_t, *ptmDbScrStat_t;

/* deprecated due to endianness ambiguity */
typedef struct _tmDbMulti2_t
{
    UInt32          multi2Key[8];   //  I: 256 bits multi II key
} tmDbMulti2_t, *ptmDbMulti2_t;

typedef struct _tmDbMulti2SysKey_t
{
    UInt8          multi2Key[32];   //  I: 256 bits multi II key
} tmDbMulti2SysKey_t, *ptmDbMulti2SysKey_t;

typedef enum
{
    tmDbDataTypeTP,            // The complete TP is output 
    tmDbDataTypePES,           // PES packet is output 
    tmDbDataTypePESPayload,    // PES payload is output
    tmDbDataTypeSection,       // Section data is output
    tmDbDataTypeAdapField,     // Adaptation field is output
    tmDbDataTypePESHdr,        // PES header is output
    tmDbDataTypeDssPesLayerAc3,        // DSS PES Data Types
    tmDbDataTypeDssPesLayerMpeg1Audio, //
    tmDbDataTypeDssPesLayerHdVideo     //
} tmDbDataType_t;

typedef struct _tmDbTSFilter_t
{
    tmDbClock_t maxClock;     // max timestamp value, extn. not used
    tmDbClock_t minClock;     // min timestamp value, extn. not used
} tmDbTSFilter_t, *ptmDbTSFilter_t; // Time Stamp filter

typedef struct _tmDbHdrFilter_t
{
    UInt16  mask;             // Mask to be set
    UInt16  value;            // Value to be set
} tmDbHdrFilter_t, *ptmDbHdrFilter_t;

typedef enum
{                                      // Full    - Residual    - Solitary Blk
    tmDbCPModeNone         = 0x00,     // No copy protect
    tmDbCPModeECB,                     // DES-ECB - None        - None
    tmDbCPModeECB_OFBPB,               // DES-ECB - DES-OFB(PB) - None
    tmDbCPModeECB_OFBPB_IV,            // DES-ECB - DES-OFB(PB) - DES-OFB(IV)
    tmDbCPModeECB_OFBIV,               // DES-ECB - DES-OFB(IV) - None
    tmDbCPModeECB_OFBIV_IV,            // DES-ECB - DES-OFB(IV) - DES-OFB(IV)
    tmDbCPModeCBC          = 0x07,     // DES-CBC - None        - None
    tmDbCPModeCBC_OFBPB,               // DES-CBC - DES-OFB(PB) - None
    tmDbCPModeCBC_OFBPB_IV,            // DES-CBC - DES-OFB(PB) - DES-OFB(IV)
    tmDbCPModeCBC_OFBIV,               // DES-CBC - DES-OFB(IV) - None
    tmDbCPModeCBC_OFBIV_IV             // DES-CBC - DES-OFB(IV) - DES-OFB(IV)
} tmDbCPMode_t;

typedef enum
{
    tmDbLevel1,         // Basic PID filtering
    tmDbLevel2,         // Section filtering or TP filtering
    tmDbLevel3          // PES header filtering
} tmDbFilterMode_t;


/******************************************************************************/
/** \brief This type is 128 bits long. It aims to contain AES information like 
 *         AES key, single AES word, the CBC-IV vector.
 *
 ******************************************************************************/
#define TM_DBFORMATS_AES_WORD_SIZE 4
typedef struct _tmDbAesWord_t
{
  UInt32 word[ TM_DBFORMATS_AES_WORD_SIZE ];
} tmDbAesWord_t, *ptmDbAesWord_t;



/******************************************************************************/
/** \brief tmDbDesTripleKey_t contains the triple DES key. In every single key,
 *         the most significant 32bits is placed at the zero index.
 *
 ******************************************************************************/
#define TM_DBFORMATS_DES_KEY_SIZE  2
typedef UInt32 tmDbDesKey_t[ TM_DBFORMATS_DES_KEY_SIZE ]; 

typedef struct _tmDbDesTripleKey_t
{
  tmDbDesKey_t   key1; 	
  tmDbDesKey_t   key2; 	
  tmDbDesKey_t   key3; 	
} tmDbDesTripleKey_t, *ptmDbDesTripleKey_t;



#if        defined(__cplusplus)
}
#endif  // defined(__cplusplus)

#endif  // _TMHW_VMSP_H
