/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
/*
 * Copyright (c) 2021 MediaTek Inc.
 */

#ifndef MDRV_ATSC30_INTERNAL_H
#define MDRV_ATSC30_INTERNAL_H

#include <linux/types.h>
#include <media/dvb_demux2.h>

#include "alp_hal_if.h"
#include "alp_parser_if.h"

#include "mtk_dmx_drv.h"

//-------------------------------------------------------------------------------------------------
//  Debug Constants and Macros
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
//  Constants
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
//  Macros
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
//  Structs and Enums
//-------------------------------------------------------------------------------------------------
struct mdrv_atsc30_internal_context {
	bool inited;
	int ref_count;

	int framer_idx;
	u32 pkt_id_counter;

	struct list_head head;
};

struct mdrv_atsc30_filter {
	struct dmx2_filter *demux_filter;

	struct list_head list;

	u32 ip_dump_counter;
};

typedef struct {
	alp_hal_frontend_input_packet_type hw_fe_input_pkt_type; // tuner input type: alp or ts188
	alp_hal_framer_mode_t hw_mode;
	u32 hw_cfg_flags;
} mdrv_atsc30_hw_config_t;

typedef bool (*mdrv_atsc30_cb_is_metadata_buffer_enough)(
		struct dmx2_filter *demux_filter);
typedef bool (*mdrv_atsc30_cb_is_buffer_enough)(
		struct dmx2_filter *demux_filter,
		u32 len);

//-------------------------------------------------------------------------------------------------
//  Functions Prototype
//-------------------------------------------------------------------------------------------------
int mdrv_atsc30_priv_get_hw_config(
		mdrv_atsc30_hw_config_t **ppstCfg);

int mdrv_atsc30_priv_decide_alp_parser_config(
		alp_hal_framer_mode_t hw_mode,
		alp_sw_parser_config_t *pstCfg);

int mdrv_atsc30_priv_packet_msg_get_available_size(
		u8 framer_idx,
		size_t *q_available_size);

int mdrv_atsc30_priv_packet_msg_handle_new_msg(
		u8 framer_idx,
		alp_packet_msg_t msg);

int mdrv_atsc30_priv_packet_msg_handler_init(
		u8 framer_idx);

int mdrv_atsc30_priv_packet_msg_handler_exit(
		u8 framer_idx);

int mdrv_atsc30_priv_get_fh_ctx(
		struct dmx2_filter *dmx2_filter,
		struct mtk_demux2_fh_ctx **ctx);
int mdrv_atsc30_priv_get_bc_ctx(
		struct dmx2_filter *dmx2_filter,
		struct mtk_bc_ctx **bc_ctx);
int mdrv_atsc30_priv_set_filter_info(
		struct dmx2_filter *dmx2_filter,
		void *data,
		u32 size);
int mdrv_atsc30_priv_get_filter_info(
		struct dmx2_filter *dmx2_filter,
		void *data,
		u32 size);
int mdrv_atsc30_priv_get_stc_info(
		struct dmx2_filter *dmx2_filter,
		void *data);

bool mdrv_atsc30_priv_is_metadata_buffer_enough(
		struct dmx2_filter *dmx2_filter);
bool mdrv_atsc30_priv_is_buffer_enough(
		struct dmx2_filter *dmx2_filter,
		u32 len);
bool mdrv_atsc30_priv_wait_for_buffer_enough(
		mdrv_atsc30_cb_is_metadata_buffer_enough
			cb_is_metadata_buffer_enough,
		mdrv_atsc30_cb_is_buffer_enough
			cb_is_buffer_enough,
		struct dmx2_filter *dmx2_filter,
		u32 len);

int mdrv_atsc30_priv_context_init(
		struct mdrv_atsc30_internal_context *context);
int mdrv_atsc30_priv_context_exit(
		struct mdrv_atsc30_internal_context *context);

int mdrv_atsc30_priv_filter_add(
		struct mdrv_atsc30_internal_context *context,
		struct dmx2_filter *demux_filter);
int mdrv_atsc30_priv_filter_del(
		struct mdrv_atsc30_internal_context *context,
		struct dmx2_filter *demux_filter);

#endif /* MDRV_ATSC30_INTERNAL_H */

