xref: /OK3568_Linux_fs/kernel/include/media/dvb_vb2.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * SPDX-License-Identifier: GPL-2.0
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * dvb-vb2.h - DVB driver helper framework for streaming I/O
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2015 Samsung Electronics
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Author: jh1009.sung@samsung.com
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
12*4882a593Smuzhiyun  * the Free Software Foundation.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifndef _DVB_VB2_H
16*4882a593Smuzhiyun #define _DVB_VB2_H
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/mutex.h>
19*4882a593Smuzhiyun #include <linux/poll.h>
20*4882a593Smuzhiyun #include <linux/dvb/dmx.h>
21*4882a593Smuzhiyun #include <media/videobuf2-core.h>
22*4882a593Smuzhiyun #include <media/videobuf2-dma-contig.h>
23*4882a593Smuzhiyun #include <media/videobuf2-vmalloc.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * enum dvb_buf_type - types of Digital TV memory-mapped buffers
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * @DVB_BUF_TYPE_CAPTURE: buffer is filled by the Kernel,
29*4882a593Smuzhiyun  *			  with a received Digital TV stream
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun enum dvb_buf_type {
32*4882a593Smuzhiyun 	DVB_BUF_TYPE_CAPTURE        = 1,
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun  * enum dvb_vb2_states - states to control VB2 state machine
37*4882a593Smuzhiyun  * @DVB_VB2_STATE_NONE:
38*4882a593Smuzhiyun  *	VB2 engine not initialized yet, init failed or VB2 was released.
39*4882a593Smuzhiyun  * @DVB_VB2_STATE_INIT:
40*4882a593Smuzhiyun  *	VB2 engine initialized.
41*4882a593Smuzhiyun  * @DVB_VB2_STATE_REQBUFS:
42*4882a593Smuzhiyun  *	Buffers were requested
43*4882a593Smuzhiyun  * @DVB_VB2_STATE_STREAMON:
44*4882a593Smuzhiyun  *	VB2 is streaming. Callers should not check it directly. Instead,
45*4882a593Smuzhiyun  *	they should use dvb_vb2_is_streaming().
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * Note:
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * Callers should not touch at the state machine directly. This
50*4882a593Smuzhiyun  * is handled inside dvb_vb2.c.
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun enum dvb_vb2_states {
53*4882a593Smuzhiyun 	DVB_VB2_STATE_NONE	= 0x0,
54*4882a593Smuzhiyun 	DVB_VB2_STATE_INIT	= 0x1,
55*4882a593Smuzhiyun 	DVB_VB2_STATE_REQBUFS	= 0x2,
56*4882a593Smuzhiyun 	DVB_VB2_STATE_STREAMON	= 0x4,
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #define DVB_VB2_NAME_MAX (20)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun  * struct dvb_buffer - video buffer information for v4l2.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * @vb:		embedded struct &vb2_buffer.
65*4882a593Smuzhiyun  * @list:	list of &struct dvb_buffer.
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun struct dvb_buffer {
68*4882a593Smuzhiyun 	struct vb2_buffer	vb;
69*4882a593Smuzhiyun 	struct list_head	list;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /**
73*4882a593Smuzhiyun  * struct dvb_vb2_ctx - control struct for VB2 handler
74*4882a593Smuzhiyun  * @vb_q:	pointer to &struct vb2_queue with videobuf2 queue.
75*4882a593Smuzhiyun  * @mutex:	mutex to serialize vb2 operations. Used by
76*4882a593Smuzhiyun  *		vb2 core %wait_prepare and %wait_finish operations.
77*4882a593Smuzhiyun  * @slock:	spin lock used to protect buffer filling at dvb_vb2.c.
78*4882a593Smuzhiyun  * @dvb_q:	List of buffers that are not filled yet.
79*4882a593Smuzhiyun  * @buf:	Pointer to the buffer that are currently being filled.
80*4882a593Smuzhiyun  * @offset:	index to the next position at the @buf to be filled.
81*4882a593Smuzhiyun  * @remain:	How many bytes are left to be filled at @buf.
82*4882a593Smuzhiyun  * @state:	bitmask of buffer states as defined by &enum dvb_vb2_states.
83*4882a593Smuzhiyun  * @buf_siz:	size of each VB2 buffer.
84*4882a593Smuzhiyun  * @buf_cnt:	number of VB2 buffers.
85*4882a593Smuzhiyun  * @nonblocking:
86*4882a593Smuzhiyun  *		If different than zero, device is operating on non-blocking
87*4882a593Smuzhiyun  *		mode.
88*4882a593Smuzhiyun  * @flags:	buffer flags as defined by &enum dmx_buffer_flags.
89*4882a593Smuzhiyun  *		Filled only at &DMX_DQBUF. &DMX_QBUF should zero this field.
90*4882a593Smuzhiyun  * @count:	monotonic counter for filled buffers. Helps to identify
91*4882a593Smuzhiyun  *		data stream loses. Filled only at &DMX_DQBUF. &DMX_QBUF should
92*4882a593Smuzhiyun  *		zero this field.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * @name:	name of the device type. Currently, it can either be
95*4882a593Smuzhiyun  *		"dvr" or "demux_filter".
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun struct dvb_vb2_ctx {
98*4882a593Smuzhiyun 	struct vb2_queue	vb_q;
99*4882a593Smuzhiyun 	struct mutex		mutex;
100*4882a593Smuzhiyun 	spinlock_t		slock;
101*4882a593Smuzhiyun 	struct list_head	dvb_q;
102*4882a593Smuzhiyun 	struct dvb_buffer	*buf;
103*4882a593Smuzhiyun 	int	offset;
104*4882a593Smuzhiyun 	int	remain;
105*4882a593Smuzhiyun 	int	state;
106*4882a593Smuzhiyun 	int	buf_siz;
107*4882a593Smuzhiyun 	int	buf_cnt;
108*4882a593Smuzhiyun 	int	nonblocking;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	enum dmx_buffer_flags flags;
111*4882a593Smuzhiyun 	u32	count;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	char	name[DVB_VB2_NAME_MAX + 1];
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun #ifndef CONFIG_DVB_MMAP
dvb_vb2_init(struct dvb_vb2_ctx * ctx,const char * name,int non_blocking)117*4882a593Smuzhiyun static inline int dvb_vb2_init(struct dvb_vb2_ctx *ctx,
118*4882a593Smuzhiyun 			       const char *name, int non_blocking)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	return 0;
121*4882a593Smuzhiyun };
dvb_vb2_release(struct dvb_vb2_ctx * ctx)122*4882a593Smuzhiyun static inline int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	return 0;
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun #define dvb_vb2_is_streaming(ctx) (0)
127*4882a593Smuzhiyun #define dvb_vb2_fill_buffer(ctx, file, wait, flags) (0)
128*4882a593Smuzhiyun 
dvb_vb2_poll(struct dvb_vb2_ctx * ctx,struct file * file,poll_table * wait)129*4882a593Smuzhiyun static inline __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx,
130*4882a593Smuzhiyun 				    struct file *file,
131*4882a593Smuzhiyun 				    poll_table *wait)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	return 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun #else
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * dvb_vb2_init - initializes VB2 handler
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
140*4882a593Smuzhiyun  * @name:	name for the VB2 handler
141*4882a593Smuzhiyun  * @non_blocking:
142*4882a593Smuzhiyun  *		if not zero, it means that the device is at non-blocking mode
143*4882a593Smuzhiyun  */
144*4882a593Smuzhiyun int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int non_blocking);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /**
147*4882a593Smuzhiyun  * dvb_vb2_release - Releases the VB2 handler allocated resources and
148*4882a593Smuzhiyun  *	put @ctx at DVB_VB2_STATE_NONE state.
149*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
150*4882a593Smuzhiyun  */
151*4882a593Smuzhiyun int dvb_vb2_release(struct dvb_vb2_ctx *ctx);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /**
154*4882a593Smuzhiyun  * dvb_vb2_is_streaming - checks if the VB2 handler is streaming
155*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * Return: 0 if not streaming, 1 otherwise.
158*4882a593Smuzhiyun  */
159*4882a593Smuzhiyun int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun  * dvb_vb2_fill_buffer - fills a VB2 buffer
163*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
164*4882a593Smuzhiyun  * @src:	place where the data is stored
165*4882a593Smuzhiyun  * @len:	number of bytes to be copied from @src
166*4882a593Smuzhiyun  * @buffer_flags:
167*4882a593Smuzhiyun  *		pointer to buffer flags as defined by &enum dmx_buffer_flags.
168*4882a593Smuzhiyun  *		can be NULL.
169*4882a593Smuzhiyun  */
170*4882a593Smuzhiyun int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
171*4882a593Smuzhiyun 			const unsigned char *src, int len,
172*4882a593Smuzhiyun 			enum dmx_buffer_flags *buffer_flags);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun  * dvb_vb2_poll - Wrapper to vb2_core_streamon() for Digital TV
176*4882a593Smuzhiyun  *      buffer handling.
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
179*4882a593Smuzhiyun  * @file:	&struct file argument passed to the poll
180*4882a593Smuzhiyun  *		file operation handler.
181*4882a593Smuzhiyun  * @wait:	&poll_table wait argument passed to the poll
182*4882a593Smuzhiyun  *		file operation handler.
183*4882a593Smuzhiyun  *
184*4882a593Smuzhiyun  * Implements poll syscall() logic.
185*4882a593Smuzhiyun  */
186*4882a593Smuzhiyun __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
187*4882a593Smuzhiyun 		      poll_table *wait);
188*4882a593Smuzhiyun #endif
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun  * dvb_vb2_stream_on() - Wrapper to vb2_core_streamon() for Digital TV
192*4882a593Smuzhiyun  *	buffer handling.
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Starts dvb streaming
197*4882a593Smuzhiyun  */
198*4882a593Smuzhiyun int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx);
199*4882a593Smuzhiyun /**
200*4882a593Smuzhiyun  * dvb_vb2_stream_off() - Wrapper to vb2_core_streamoff() for Digital TV
201*4882a593Smuzhiyun  *	buffer handling.
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  * Stops dvb streaming
206*4882a593Smuzhiyun  */
207*4882a593Smuzhiyun int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /**
210*4882a593Smuzhiyun  * dvb_vb2_reqbufs() - Wrapper to vb2_core_reqbufs() for Digital TV
211*4882a593Smuzhiyun  *	buffer handling.
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
214*4882a593Smuzhiyun  * @req:	&struct dmx_requestbuffers passed from userspace in
215*4882a593Smuzhiyun  *		order to handle &DMX_REQBUFS.
216*4882a593Smuzhiyun  *
217*4882a593Smuzhiyun  * Initiate streaming by requesting a number of buffers. Also used to
218*4882a593Smuzhiyun  * free previously requested buffers, is ``req->count`` is zero.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun  * dvb_vb2_querybuf() - Wrapper to vb2_core_querybuf() for Digital TV
224*4882a593Smuzhiyun  *	buffer handling.
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
227*4882a593Smuzhiyun  * @b:		&struct dmx_buffer passed from userspace in
228*4882a593Smuzhiyun  *		order to handle &DMX_QUERYBUF.
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun  * dvb_vb2_expbuf() - Wrapper to vb2_core_expbuf() for Digital TV
236*4882a593Smuzhiyun  *	buffer handling.
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
239*4882a593Smuzhiyun  * @exp:	&struct dmx_exportbuffer passed from userspace in
240*4882a593Smuzhiyun  *		order to handle &DMX_EXPBUF.
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  * Export a buffer as a file descriptor.
243*4882a593Smuzhiyun  */
244*4882a593Smuzhiyun int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /**
247*4882a593Smuzhiyun  * dvb_vb2_qbuf() - Wrapper to vb2_core_qbuf() for Digital TV buffer handling.
248*4882a593Smuzhiyun  *
249*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
250*4882a593Smuzhiyun  * @b:		&struct dmx_buffer passed from userspace in
251*4882a593Smuzhiyun  *		order to handle &DMX_QBUF.
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * Queue a Digital TV buffer as requested by userspace
254*4882a593Smuzhiyun  */
255*4882a593Smuzhiyun int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /**
258*4882a593Smuzhiyun  * dvb_vb2_dqbuf() - Wrapper to vb2_core_dqbuf() for Digital TV
259*4882a593Smuzhiyun  *	buffer handling.
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
262*4882a593Smuzhiyun  * @b:		&struct dmx_buffer passed from userspace in
263*4882a593Smuzhiyun  *		order to handle &DMX_DQBUF.
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  * Dequeue a Digital TV buffer to the userspace
266*4882a593Smuzhiyun  */
267*4882a593Smuzhiyun int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /**
270*4882a593Smuzhiyun  * dvb_vb2_mmap() - Wrapper to vb2_mmap() for Digital TV buffer handling.
271*4882a593Smuzhiyun  *
272*4882a593Smuzhiyun  * @ctx:	control struct for VB2 handler
273*4882a593Smuzhiyun  * @vma:        pointer to &struct vm_area_struct with the vma passed
274*4882a593Smuzhiyun  *              to the mmap file operation handler in the driver.
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * map Digital TV video buffers into application address space.
277*4882a593Smuzhiyun  */
278*4882a593Smuzhiyun int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun #endif /* _DVB_VB2_H */
281