18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * SPDX-License-Identifier: GPL-2.0
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * dvb-vb2.h - DVB driver helper framework for streaming I/O
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2015 Samsung Electronics
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Author: jh1009.sung@samsung.com
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
118c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by
128c2ecf20Sopenharmony_ci * the Free Software Foundation.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifndef _DVB_VB2_H
168c2ecf20Sopenharmony_ci#define _DVB_VB2_H
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/mutex.h>
198c2ecf20Sopenharmony_ci#include <linux/poll.h>
208c2ecf20Sopenharmony_ci#include <linux/dvb/dmx.h>
218c2ecf20Sopenharmony_ci#include <media/videobuf2-core.h>
228c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-contig.h>
238c2ecf20Sopenharmony_ci#include <media/videobuf2-vmalloc.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/**
268c2ecf20Sopenharmony_ci * enum dvb_buf_type - types of Digital TV memory-mapped buffers
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * @DVB_BUF_TYPE_CAPTURE: buffer is filled by the Kernel,
298c2ecf20Sopenharmony_ci *			  with a received Digital TV stream
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_cienum dvb_buf_type {
328c2ecf20Sopenharmony_ci	DVB_BUF_TYPE_CAPTURE        = 1,
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/**
368c2ecf20Sopenharmony_ci * enum dvb_vb2_states - states to control VB2 state machine
378c2ecf20Sopenharmony_ci * @DVB_VB2_STATE_NONE:
388c2ecf20Sopenharmony_ci *	VB2 engine not initialized yet, init failed or VB2 was released.
398c2ecf20Sopenharmony_ci * @DVB_VB2_STATE_INIT:
408c2ecf20Sopenharmony_ci *	VB2 engine initialized.
418c2ecf20Sopenharmony_ci * @DVB_VB2_STATE_REQBUFS:
428c2ecf20Sopenharmony_ci *	Buffers were requested
438c2ecf20Sopenharmony_ci * @DVB_VB2_STATE_STREAMON:
448c2ecf20Sopenharmony_ci *	VB2 is streaming. Callers should not check it directly. Instead,
458c2ecf20Sopenharmony_ci *	they should use dvb_vb2_is_streaming().
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * Note:
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * Callers should not touch at the state machine directly. This
508c2ecf20Sopenharmony_ci * is handled inside dvb_vb2.c.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cienum dvb_vb2_states {
538c2ecf20Sopenharmony_ci	DVB_VB2_STATE_NONE	= 0x0,
548c2ecf20Sopenharmony_ci	DVB_VB2_STATE_INIT	= 0x1,
558c2ecf20Sopenharmony_ci	DVB_VB2_STATE_REQBUFS	= 0x2,
568c2ecf20Sopenharmony_ci	DVB_VB2_STATE_STREAMON	= 0x4,
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define DVB_VB2_NAME_MAX (20)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/**
628c2ecf20Sopenharmony_ci * struct dvb_buffer - video buffer information for v4l2.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * @vb:		embedded struct &vb2_buffer.
658c2ecf20Sopenharmony_ci * @list:	list of &struct dvb_buffer.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_cistruct dvb_buffer {
688c2ecf20Sopenharmony_ci	struct vb2_buffer	vb;
698c2ecf20Sopenharmony_ci	struct list_head	list;
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/**
738c2ecf20Sopenharmony_ci * struct dvb_vb2_ctx - control struct for VB2 handler
748c2ecf20Sopenharmony_ci * @vb_q:	pointer to &struct vb2_queue with videobuf2 queue.
758c2ecf20Sopenharmony_ci * @mutex:	mutex to serialize vb2 operations. Used by
768c2ecf20Sopenharmony_ci *		vb2 core %wait_prepare and %wait_finish operations.
778c2ecf20Sopenharmony_ci * @slock:	spin lock used to protect buffer filling at dvb_vb2.c.
788c2ecf20Sopenharmony_ci * @dvb_q:	List of buffers that are not filled yet.
798c2ecf20Sopenharmony_ci * @buf:	Pointer to the buffer that are currently being filled.
808c2ecf20Sopenharmony_ci * @offset:	index to the next position at the @buf to be filled.
818c2ecf20Sopenharmony_ci * @remain:	How many bytes are left to be filled at @buf.
828c2ecf20Sopenharmony_ci * @state:	bitmask of buffer states as defined by &enum dvb_vb2_states.
838c2ecf20Sopenharmony_ci * @buf_siz:	size of each VB2 buffer.
848c2ecf20Sopenharmony_ci * @buf_cnt:	number of VB2 buffers.
858c2ecf20Sopenharmony_ci * @nonblocking:
868c2ecf20Sopenharmony_ci *		If different than zero, device is operating on non-blocking
878c2ecf20Sopenharmony_ci *		mode.
888c2ecf20Sopenharmony_ci * @flags:	buffer flags as defined by &enum dmx_buffer_flags.
898c2ecf20Sopenharmony_ci *		Filled only at &DMX_DQBUF. &DMX_QBUF should zero this field.
908c2ecf20Sopenharmony_ci * @count:	monotonic counter for filled buffers. Helps to identify
918c2ecf20Sopenharmony_ci *		data stream loses. Filled only at &DMX_DQBUF. &DMX_QBUF should
928c2ecf20Sopenharmony_ci *		zero this field.
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * @name:	name of the device type. Currently, it can either be
958c2ecf20Sopenharmony_ci *		"dvr" or "demux_filter".
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_cistruct dvb_vb2_ctx {
988c2ecf20Sopenharmony_ci	struct vb2_queue	vb_q;
998c2ecf20Sopenharmony_ci	struct mutex		mutex;
1008c2ecf20Sopenharmony_ci	spinlock_t		slock;
1018c2ecf20Sopenharmony_ci	struct list_head	dvb_q;
1028c2ecf20Sopenharmony_ci	struct dvb_buffer	*buf;
1038c2ecf20Sopenharmony_ci	int	offset;
1048c2ecf20Sopenharmony_ci	int	remain;
1058c2ecf20Sopenharmony_ci	int	state;
1068c2ecf20Sopenharmony_ci	int	buf_siz;
1078c2ecf20Sopenharmony_ci	int	buf_cnt;
1088c2ecf20Sopenharmony_ci	int	nonblocking;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	enum dmx_buffer_flags flags;
1118c2ecf20Sopenharmony_ci	u32	count;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	char	name[DVB_VB2_NAME_MAX + 1];
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#ifndef CONFIG_DVB_MMAP
1178c2ecf20Sopenharmony_cistatic inline int dvb_vb2_init(struct dvb_vb2_ctx *ctx,
1188c2ecf20Sopenharmony_ci			       const char *name, int non_blocking)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return 0;
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_cistatic inline int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci#define dvb_vb2_is_streaming(ctx) (0)
1278c2ecf20Sopenharmony_ci#define dvb_vb2_fill_buffer(ctx, file, wait, flags) (0)
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx,
1308c2ecf20Sopenharmony_ci				    struct file *file,
1318c2ecf20Sopenharmony_ci				    poll_table *wait)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return 0;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci#else
1368c2ecf20Sopenharmony_ci/**
1378c2ecf20Sopenharmony_ci * dvb_vb2_init - initializes VB2 handler
1388c2ecf20Sopenharmony_ci *
1398c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
1408c2ecf20Sopenharmony_ci * @name:	name for the VB2 handler
1418c2ecf20Sopenharmony_ci * @non_blocking:
1428c2ecf20Sopenharmony_ci *		if not zero, it means that the device is at non-blocking mode
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ciint dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int non_blocking);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/**
1478c2ecf20Sopenharmony_ci * dvb_vb2_release - Releases the VB2 handler allocated resources and
1488c2ecf20Sopenharmony_ci *	put @ctx at DVB_VB2_STATE_NONE state.
1498c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
1508c2ecf20Sopenharmony_ci */
1518c2ecf20Sopenharmony_ciint dvb_vb2_release(struct dvb_vb2_ctx *ctx);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/**
1548c2ecf20Sopenharmony_ci * dvb_vb2_is_streaming - checks if the VB2 handler is streaming
1558c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
1568c2ecf20Sopenharmony_ci *
1578c2ecf20Sopenharmony_ci * Return: 0 if not streaming, 1 otherwise.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_ciint dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/**
1628c2ecf20Sopenharmony_ci * dvb_vb2_fill_buffer - fills a VB2 buffer
1638c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
1648c2ecf20Sopenharmony_ci * @src:	place where the data is stored
1658c2ecf20Sopenharmony_ci * @len:	number of bytes to be copied from @src
1668c2ecf20Sopenharmony_ci * @buffer_flags:
1678c2ecf20Sopenharmony_ci *		pointer to buffer flags as defined by &enum dmx_buffer_flags.
1688c2ecf20Sopenharmony_ci *		can be NULL.
1698c2ecf20Sopenharmony_ci */
1708c2ecf20Sopenharmony_ciint dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
1718c2ecf20Sopenharmony_ci			const unsigned char *src, int len,
1728c2ecf20Sopenharmony_ci			enum dmx_buffer_flags *buffer_flags);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/**
1758c2ecf20Sopenharmony_ci * dvb_vb2_poll - Wrapper to vb2_core_streamon() for Digital TV
1768c2ecf20Sopenharmony_ci *      buffer handling.
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
1798c2ecf20Sopenharmony_ci * @file:	&struct file argument passed to the poll
1808c2ecf20Sopenharmony_ci *		file operation handler.
1818c2ecf20Sopenharmony_ci * @wait:	&poll_table wait argument passed to the poll
1828c2ecf20Sopenharmony_ci *		file operation handler.
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * Implements poll syscall() logic.
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_ci__poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
1878c2ecf20Sopenharmony_ci		      poll_table *wait);
1888c2ecf20Sopenharmony_ci#endif
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/**
1918c2ecf20Sopenharmony_ci * dvb_vb2_stream_on() - Wrapper to vb2_core_streamon() for Digital TV
1928c2ecf20Sopenharmony_ci *	buffer handling.
1938c2ecf20Sopenharmony_ci *
1948c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci * Starts dvb streaming
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_ciint dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx);
1998c2ecf20Sopenharmony_ci/**
2008c2ecf20Sopenharmony_ci * dvb_vb2_stream_off() - Wrapper to vb2_core_streamoff() for Digital TV
2018c2ecf20Sopenharmony_ci *	buffer handling.
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * Stops dvb streaming
2068c2ecf20Sopenharmony_ci */
2078c2ecf20Sopenharmony_ciint dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/**
2108c2ecf20Sopenharmony_ci * dvb_vb2_reqbufs() - Wrapper to vb2_core_reqbufs() for Digital TV
2118c2ecf20Sopenharmony_ci *	buffer handling.
2128c2ecf20Sopenharmony_ci *
2138c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2148c2ecf20Sopenharmony_ci * @req:	&struct dmx_requestbuffers passed from userspace in
2158c2ecf20Sopenharmony_ci *		order to handle &DMX_REQBUFS.
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * Initiate streaming by requesting a number of buffers. Also used to
2188c2ecf20Sopenharmony_ci * free previously requested buffers, is ``req->count`` is zero.
2198c2ecf20Sopenharmony_ci */
2208c2ecf20Sopenharmony_ciint dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/**
2238c2ecf20Sopenharmony_ci * dvb_vb2_querybuf() - Wrapper to vb2_core_querybuf() for Digital TV
2248c2ecf20Sopenharmony_ci *	buffer handling.
2258c2ecf20Sopenharmony_ci *
2268c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2278c2ecf20Sopenharmony_ci * @b:		&struct dmx_buffer passed from userspace in
2288c2ecf20Sopenharmony_ci *		order to handle &DMX_QUERYBUF.
2298c2ecf20Sopenharmony_ci *
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci */
2328c2ecf20Sopenharmony_ciint dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/**
2358c2ecf20Sopenharmony_ci * dvb_vb2_expbuf() - Wrapper to vb2_core_expbuf() for Digital TV
2368c2ecf20Sopenharmony_ci *	buffer handling.
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2398c2ecf20Sopenharmony_ci * @exp:	&struct dmx_exportbuffer passed from userspace in
2408c2ecf20Sopenharmony_ci *		order to handle &DMX_EXPBUF.
2418c2ecf20Sopenharmony_ci *
2428c2ecf20Sopenharmony_ci * Export a buffer as a file descriptor.
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_ciint dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/**
2478c2ecf20Sopenharmony_ci * dvb_vb2_qbuf() - Wrapper to vb2_core_qbuf() for Digital TV buffer handling.
2488c2ecf20Sopenharmony_ci *
2498c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2508c2ecf20Sopenharmony_ci * @b:		&struct dmx_buffer passed from userspace in
2518c2ecf20Sopenharmony_ci *		order to handle &DMX_QBUF.
2528c2ecf20Sopenharmony_ci *
2538c2ecf20Sopenharmony_ci * Queue a Digital TV buffer as requested by userspace
2548c2ecf20Sopenharmony_ci */
2558c2ecf20Sopenharmony_ciint dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci/**
2588c2ecf20Sopenharmony_ci * dvb_vb2_dqbuf() - Wrapper to vb2_core_dqbuf() for Digital TV
2598c2ecf20Sopenharmony_ci *	buffer handling.
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2628c2ecf20Sopenharmony_ci * @b:		&struct dmx_buffer passed from userspace in
2638c2ecf20Sopenharmony_ci *		order to handle &DMX_DQBUF.
2648c2ecf20Sopenharmony_ci *
2658c2ecf20Sopenharmony_ci * Dequeue a Digital TV buffer to the userspace
2668c2ecf20Sopenharmony_ci */
2678c2ecf20Sopenharmony_ciint dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci/**
2708c2ecf20Sopenharmony_ci * dvb_vb2_mmap() - Wrapper to vb2_mmap() for Digital TV buffer handling.
2718c2ecf20Sopenharmony_ci *
2728c2ecf20Sopenharmony_ci * @ctx:	control struct for VB2 handler
2738c2ecf20Sopenharmony_ci * @vma:        pointer to &struct vm_area_struct with the vma passed
2748c2ecf20Sopenharmony_ci *              to the mmap file operation handler in the driver.
2758c2ecf20Sopenharmony_ci *
2768c2ecf20Sopenharmony_ci * map Digital TV video buffers into application address space.
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_ciint dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci#endif /* _DVB_VB2_H */
281