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

/*----------------------------------------------------------------------------*/
/*! @file 
 *  $RCSfile: $
 *  $Revision: #1 $
 *  $Date: 2015/11/10 $
 *  $Author: dtvbm11 $
 *  
 *  @par Description:
 *         This header file contains DRM specific definitions,
 *         which are exported.
 */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*! @defgroup groupMW_DRM DRM
 *
 *  @ingroup groupMW
 *  @brief Provides a unified interface for DRM
 *
 *  @see groupMW
 *  @{ 
 */
/*----------------------------------------------------------------------------*/

#ifndef _X_DRM_H_
#define _X_DRM_H_
/*-----------------------------------------------------------------------------
                    include files
 ----------------------------------------------------------------------------*/
#include "x_common.h"
#include "x_mm_common.h"
#include "x_handle.h"
#include "x_handle_grp.h"
#include "x_drm_drv.h"
#include "u_drm.h"

#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------
                    macros, defines, typedefs, enums
 ----------------------------------------------------------------------------*/
/* The following lists all error codes for drm module. */
#define DRMR_RENTAL_EXPIRED ((INT32)3)              /**< DRM RETURN RENTAL EXPIRED */
#define DRMR_AUTH_ERROR ((INT32)2)                  /**< DRM RETURN AUTH ERROR */
#define DRMR_UNSPECIFIED_ERROR ((INT32)1)           /**< DRM RETURN UNSPECIFIED ERROR */
#define DRMR_OK ((INT32)0)                          /**< DRM RETURN OK */
#define DRMR_INV_ARG ((INT32)-1)                    /**< DRM RETURN INVALID ARGD */
#define DRMR_INV_HANDLE ((INT32)-2)                 /**< DRM RETURN INVALID HANDLE */
#define DRMR_INV_COMMAND ((INT32)-3)                /**< DRM RETURN INVALID COMMAND */
#define DRMR_OUT_OF_HANDLE ((INT32)-4)              /**< DRM RETURN OUT OF HANDLE */
#define DRMR_OUT_OF_MEMORY ((INT32)-5)              /**< DRM RETURN OUT OF MEMORY */
#define DRMR_INSUFFICIENT_BUFFER ((INT32)-6)        /**< DRM RETURN INSUFFICIENT BUFFER */
#define DRMR_CLI_ERROR ((INT32)-7)                  /**< DRM RETURN CLI ERROR */
#define DRMR_INITIAL_ERROR ((INT32)-8)              /**< DRM RETURN INITIAL ERROR */
#define DRMR_INTERNAL_ERROR ((INT32)-9)             /**< DRM RETURN INTERNAL ERROR */
#define DRMR_FCT_NOT_SUPPORT ((INT32)-10)           /**< DRM RETURN FUNCTION NOT SUPPORT */
#define DRMR_TYPE_NOT_SUPPORT ((INT32)-11)          /**< DRM RETURN TYPE NOT SUPPORT */
#define DRMR_HANDLER_NOT_SUPPORT ((INT32)-12)       /**< DRM RETURN HANDLER NOT SUPPORT */
#define DRMR_HANDLER_NAME_EXIST ((INT32)-13)        /**< DRM RETURN HANDLER NAME EXIST */
#define DRMR_INFO_NOT_SUPPORT ((INT32)-14)          /**< DRM RETURN INFO NOT SUPPORT */

#if 0 // move to u_drm_drv.h
/* This defines the none drm type. */
#define DRM_TYPE_NONE ((DRM_TYPE_T)0)               /**< DRM TYPE NONE */
#endif

/*---------------------------------------------------------------------------*/
/*! 
 *  @brief  This defines the type of command code.
 *  @code
 *  typedef UINT32 DRM_CMD_CODE_T;
 *  @endcode
 */
/*---------------------------------------------------------------------------*/
typedef UINT32 DRM_CMD_CODE_T;

/* This defines the end of drm command. */
#define DRM_CMD_CODE_END ((DRM_CMD_CODE_T)0)        /**< DRM CMD CODE END */

/*---------------------------------------------------------------------------*/
/*! 
 *  @brief  This structure defines drm command.
 *  @code
 *  typedef struct
 *  {
 *      DRM_CMD_CODE_T e_code;
 *      union
 *      {
 *          BOOL b_boolean;
 *          UINT8 ui1_data;
 *          UINT16 ui2_data;
 *          UINT32 ui4_data;
 *          HANDLE_T h_obj;
 *          CHAR* ps_text;
 *          VOID* pv_data;
 *      } u;
 *  } DRM_COMMAND_T;
 *  @endcode
 *  @li@c  e_code     -  DRM CMD CODE
 *  @li@c  b_boolean  -  DRM CMD STRUCTURE
 *  @li@c  ui1_data   -  DRM CMD STRUCTURE
 *  @li@c  ui2_data   -  DRM CMD STRUCTURE
 *  @li@c  ui4_data   -  DRM CMD STRUCTURE
 *  @li@c  h_obj      -  DRM CMD STRUCTURE
 *  @li@c  ps_text    -  DRM CMD STRUCTURE
 *  @li@c  pv_data    -  DRM CMD STRUCTURE
 */
/*---------------------------------------------------------------------------*/
typedef struct
{
    DRM_CMD_CODE_T e_code;
    union
    {
        BOOL b_boolean;
        UINT8 ui1_data;
        UINT16 ui2_data;
        UINT32 ui4_data;
        HANDLE_T h_obj;
        CHAR* ps_text;
        VOID* pv_data;
    } u;
} DRM_COMMAND_T;

/* 
    set/get drm types definition.
    31                        20 19                                          0
    --------------------------------------------------------------
    |     Group(12 bits)  |         Code(20 bits)                |
    --------------------------------------------------------------   
*/
/*---------------------------------------------------------------------------*/
/*! 
 *  @brief  This defines set type of DRM.
 *  @code
 *  typedef UINT32 DRM_SET_TYPE_T;
 *  @endcode
 */
/*---------------------------------------------------------------------------*/
typedef UINT32 DRM_SET_TYPE_T;
/*---------------------------------------------------------------------------*/
/*! 
 *  @brief  This defines get type of DRM.
 *  @code
 *  typedef UINT32 DRM_GET_TYPE_T;
 *  @endcode
 */
/*---------------------------------------------------------------------------*/
typedef UINT32 DRM_GET_TYPE_T;

#define DRM_GRP_BITS 12                             /**< DRM GROUP SIZE IN BIT */
#define DRM_CODE_BITS 20                            /**< DRM CODE SIZE IN BIT */
#define DRM_GRP_MASK 0xFFF00000                     /**< DRM GROUP MASK */
#define DRM_CODE_MASK 0x000FFFFF                    /**< DRM CODE MASK */
#define DRM_GET_GRP(type) (type & DRM_GRP_MASK)     /**< DRM GET GROUP */
#define DRM_GET_CODE(type) (type & DRM_CODE_MASK)   /**< DRM GET CODE */

/* This defines the common group. */
#define DRM_GRP_COMMON (1 << DRM_CODE_BITS)         /**< DRM GROUP COMMON */
#define DRM_GRP_LAST_ENTRY (64)                     /**< DRM GROUP LAST */

/* The followings lists all drm get common types. */
#define DRM_GET_TYPE_OUTPUT_SIGNAL_PROTECTION_INFO ((DRM_GET_TYPE_T)(DRM_GRP_COMMON | 0))   /**< DRM GET TYPE OUTPUT SIGNAL PROTECTION INFO */

/*-----------------------------------------------------------------------------
                    extern data declarations
 ----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
                    data declarations
 ----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
                    functions declarations
 ----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*! @brief  This API opens a drm handle according to the drm command set.
 *
 *  @param  [in] pt_drm_cmd_sets  Specify the command set to initialize drm. 
 *  @param  [in] t_drm_type  Specify the drm type.
 *  @param  [in, out] ph_drm  Specify handle pointer contains the drm handle.
 *
 *  @return  INT32
 ----------------------------------------------------------------------------*/
extern INT32 x_drm_open (
    DRM_COMMAND_T* pt_drm_cmd_sets,
    DRM_TYPE_T t_drm_type,
    HANDLE_T* ph_drm);

/*---------------------------------------------------------------------------*/
/*! @brief  This API closes the drm handle.
 *
 *  @param  [in] h_drm  Specify the drm handle to be closed.
 *
 *  @return  INT32
 ----------------------------------------------------------------------------*/ 
extern INT32 x_drm_close (
    HANDLE_T h_drm);

/*---------------------------------------------------------------------------*/
/*! @brief  This API retrieves information of the drm handle.
 *
 *  @param  [in] h_drm  Specify the drm handle.
 *  @param  [in] t_drm_get_type  Specify the get information type of drm.
 *  @param  [in, out] pv_buf  Specify memory buffer to retrive the drm information.
 *  @param  [in] z_buf_leng  Specify length of the pv_buf. If it is smaller than 
                             required, return DRM_INSUFFICENT_MEM.
 *
 *  @return  INT32
 ----------------------------------------------------------------------------*/
extern INT32 x_drm_get_info (
    HANDLE_T h_drm,
    DRM_GET_TYPE_T t_drm_get_type,
    VOID* pv_buf,
    SIZE_T z_buf_leng);

/*---------------------------------------------------------------------------*/
/*! @brief  This API sets information of the drm handle.
 *
 *  @param  [in] h_drm  Specify the drm handle.
 *  @param  [in] t_drm_set_type  Specify the set information type of drm.
 *  @param  [in] pv_buf  Specify memory buffer to set the drm information.
 *  @param  [in] z_buf_leng  Specify length of the pv_buf. 
 *
 *  @return  INT32
 ----------------------------------------------------------------------------*/ 
extern INT32 x_drm_set_info (
    HANDLE_T h_drm,
    DRM_SET_TYPE_T t_drm_set_type,
    VOID* pv_buf,
    SIZE_T z_buf_leng);

/*---------------------------------------------------------------------------*/
/*! @brief  This API retrieves generic information of the specific drm type.
 *
 *  @param  [in] h_drm  Specify the drm handle.
 *  @param  [in] t_drm_get_type  Specify the get information type of drm.
 *  @param  [in, out] pv_buf  Specify memory buffer to retrive the drm information.
 *  @param  [in] z_buf_leng  Specify length of the pv_buf. If it is smaller than 
                             required, return DRM_INSUFFICENT_MEM.
 *
 *  @return  INT32
 ----------------------------------------------------------------------------*/
extern INT32 x_drm_get_generic_info (
    DRM_TYPE_T t_drm_type,
    DRM_GET_TYPE_T t_drm_get_type,
    VOID* pv_buf,
    SIZE_T z_buf_leng);

/*---------------------------------------------------------------------------*/
/*! @brief  This API sets generic information of the specific drm type.
 *
 *  @param  [in] h_drm  Specify the drm handle.
 *  @param  [in] t_drm_set_type  Specify the set information type of drm.
 *  @param  [in] pv_buf  Specify memory buffer to set the drm information.
 *  @param  [in] z_buf_leng  Specify length of the pv_buf. 
 *
 *  @return  INT32
 ----------------------------------------------------------------------------*/  
extern INT32 x_drm_set_generic_info (
    DRM_TYPE_T t_drm_type,
    DRM_SET_TYPE_T t_drm_set_type,
    VOID* pv_buf,
    SIZE_T z_buf_leng);

#ifdef __cplusplus
}
#endif

#endif /* _X_DRM_H_ */

/*----------------------------------------------------------------------------*/
/*! @} */                                          /* end of groupMW_DRM      */
/*----------------------------------------------------------------------------*/
