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