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

#ifndef MTK_DMX_M2TS_H
#define MTK_DMX_M2TS_H

#include <linux/workqueue.h>

#include "mtk_dmx_drv.h"
#include "mtk_dmx_dvrdrv.h"

enum M2TS_PACKET_TYPE {
	M2TS_PACKET_TYPE_NORMAL		= 188,
	M2TS_PACKET_TYPE_WITH_STAMP	= 192
};

#define cast_to_m2ts_filter(filter)	\
	((struct mtk_m2ts_filter *)filter)

#define cast_to_m2ts_feed(feed)	\
	((struct mtk_m2ts_feed *)feed)

#define cast_to_m2ts_dvr_private(priv)	\
	((struct mtk_m2ts_dvr_private *)priv)

enum dvr_m2ts_filein_state {
	DVR_M2TS_FILEIN_INIT_STATE,
	DVR_M2TS_FILEIN_FIRST_BLOCK_STATE,
	DVR_M2TS_FILEIN_NORMAL_STATE,
};

struct mtk_m2ts_filter {
	struct dmx2_filter base;

	/* derived part */
	enum m2ts_pid_type pid_type;
};

struct mtk_m2ts_feed {
	struct dmx2_feed base;

	/* derived part */
};

struct mtk_m2ts_dmx_private {
	u32 stc_cw;
	u32 stc_bypass_3_2;
};

struct mtk_m2ts_dvr_private {
	enum dvr_m2ts_filein_state dvr_m2ts_filein_state;
	struct mtk_dvr2_fh_ctx *filein_ctx;
	ssize_t last_write_offset;
	struct dmx2_buffer_ctx *buf_ctx;
	bool status;
	u8 pwrite_same_count;
};

int mtk_dmx_m2ts_init(struct mtk_demux2 *demux);
int mtk_dmx_m2ts_exit(struct mtk_demux2 *demux);

int mtk_dvr_m2ts_init(struct mtk_dvr2 *dvr);
int mtk_dvr_m2ts_exit(struct mtk_dvr2 *dvr);

void m2ts_handle_section_transfer(struct dmx2_feed *feed);

#endif /* MTK_DMX_M2TS_H */
