xref: /OK3568_Linux_fs/kernel/include/media/v4l2-mem2mem.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Memory-to-memory device framework for Video for Linux 2.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Helper functions for devices that use memory buffers for both source
6*4882a593Smuzhiyun  * and destination.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (c) 2009 Samsung Electronics Co., Ltd.
9*4882a593Smuzhiyun  * Pawel Osciak, <pawel@osciak.com>
10*4882a593Smuzhiyun  * Marek Szyprowski, <m.szyprowski@samsung.com>
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #ifndef _MEDIA_V4L2_MEM2MEM_H
14*4882a593Smuzhiyun #define _MEDIA_V4L2_MEM2MEM_H
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <media/videobuf2-v4l2.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun  * struct v4l2_m2m_ops - mem-to-mem device driver callbacks
20*4882a593Smuzhiyun  * @device_run:	required. Begin the actual job (transaction) inside this
21*4882a593Smuzhiyun  *		callback.
22*4882a593Smuzhiyun  *		The job does NOT have to end before this callback returns
23*4882a593Smuzhiyun  *		(and it will be the usual case). When the job finishes,
24*4882a593Smuzhiyun  *		v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish()
25*4882a593Smuzhiyun  *		has to be called.
26*4882a593Smuzhiyun  * @job_ready:	optional. Should return 0 if the driver does not have a job
27*4882a593Smuzhiyun  *		fully prepared to run yet (i.e. it will not be able to finish a
28*4882a593Smuzhiyun  *		transaction without sleeping). If not provided, it will be
29*4882a593Smuzhiyun  *		assumed that one source and one destination buffer are all
30*4882a593Smuzhiyun  *		that is required for the driver to perform one full transaction.
31*4882a593Smuzhiyun  *		This method may not sleep.
32*4882a593Smuzhiyun  * @job_abort:	optional. Informs the driver that it has to abort the currently
33*4882a593Smuzhiyun  *		running transaction as soon as possible (i.e. as soon as it can
34*4882a593Smuzhiyun  *		stop the device safely; e.g. in the next interrupt handler),
35*4882a593Smuzhiyun  *		even if the transaction would not have been finished by then.
36*4882a593Smuzhiyun  *		After the driver performs the necessary steps, it has to call
37*4882a593Smuzhiyun  *		v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish() as
38*4882a593Smuzhiyun  *		if the transaction ended normally.
39*4882a593Smuzhiyun  *		This function does not have to (and will usually not) wait
40*4882a593Smuzhiyun  *		until the device enters a state when it can be stopped.
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun struct v4l2_m2m_ops {
43*4882a593Smuzhiyun 	void (*device_run)(void *priv);
44*4882a593Smuzhiyun 	int (*job_ready)(void *priv);
45*4882a593Smuzhiyun 	void (*job_abort)(void *priv);
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun struct video_device;
49*4882a593Smuzhiyun struct v4l2_m2m_dev;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /**
52*4882a593Smuzhiyun  * struct v4l2_m2m_queue_ctx - represents a queue for buffers ready to be
53*4882a593Smuzhiyun  *	processed
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * @q:		pointer to struct &vb2_queue
56*4882a593Smuzhiyun  * @rdy_queue:	List of V4L2 mem-to-mem queues
57*4882a593Smuzhiyun  * @rdy_spinlock: spin lock to protect the struct usage
58*4882a593Smuzhiyun  * @num_rdy:	number of buffers ready to be processed
59*4882a593Smuzhiyun  * @buffered:	is the queue buffered?
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * Queue for buffers ready to be processed as soon as this
62*4882a593Smuzhiyun  * instance receives access to the device.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun struct v4l2_m2m_queue_ctx {
66*4882a593Smuzhiyun 	struct vb2_queue	q;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	struct list_head	rdy_queue;
69*4882a593Smuzhiyun 	spinlock_t		rdy_spinlock;
70*4882a593Smuzhiyun 	u8			num_rdy;
71*4882a593Smuzhiyun 	bool			buffered;
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  * struct v4l2_m2m_ctx - Memory to memory context structure
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * @q_lock: struct &mutex lock
78*4882a593Smuzhiyun  * @new_frame: valid in the device_run callback: if true, then this
79*4882a593Smuzhiyun  *		starts a new frame; if false, then this is a new slice
80*4882a593Smuzhiyun  *		for an existing frame. This is always true unless
81*4882a593Smuzhiyun  *		V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF is set, which
82*4882a593Smuzhiyun  *		indicates slicing support.
83*4882a593Smuzhiyun  * @is_draining: indicates device is in draining phase
84*4882a593Smuzhiyun  * @last_src_buf: indicate the last source buffer for draining
85*4882a593Smuzhiyun  * @next_buf_last: next capture queud buffer will be tagged as last
86*4882a593Smuzhiyun  * @has_stopped: indicate the device has been stopped
87*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
88*4882a593Smuzhiyun  * @cap_q_ctx: Capture (output to memory) queue context
89*4882a593Smuzhiyun  * @out_q_ctx: Output (input from memory) queue context
90*4882a593Smuzhiyun  * @queue: List of memory to memory contexts
91*4882a593Smuzhiyun  * @job_flags: Job queue flags, used internally by v4l2-mem2mem.c:
92*4882a593Smuzhiyun  *		%TRANS_QUEUED, %TRANS_RUNNING and %TRANS_ABORT.
93*4882a593Smuzhiyun  * @finished: Wait queue used to signalize when a job queue finished.
94*4882a593Smuzhiyun  * @priv: Instance private data
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * The memory to memory context is specific to a file handle, NOT to e.g.
97*4882a593Smuzhiyun  * a device.
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun struct v4l2_m2m_ctx {
100*4882a593Smuzhiyun 	/* optional cap/out vb2 queues lock */
101*4882a593Smuzhiyun 	struct mutex			*q_lock;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	bool				new_frame;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	bool				is_draining;
106*4882a593Smuzhiyun 	struct vb2_v4l2_buffer		*last_src_buf;
107*4882a593Smuzhiyun 	bool				next_buf_last;
108*4882a593Smuzhiyun 	bool				has_stopped;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* internal use only */
111*4882a593Smuzhiyun 	struct v4l2_m2m_dev		*m2m_dev;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	struct v4l2_m2m_queue_ctx	cap_q_ctx;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	struct v4l2_m2m_queue_ctx	out_q_ctx;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* For device job queue */
118*4882a593Smuzhiyun 	struct list_head		queue;
119*4882a593Smuzhiyun 	unsigned long			job_flags;
120*4882a593Smuzhiyun 	wait_queue_head_t		finished;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	void				*priv;
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /**
126*4882a593Smuzhiyun  * struct v4l2_m2m_buffer - Memory to memory buffer
127*4882a593Smuzhiyun  *
128*4882a593Smuzhiyun  * @vb: pointer to struct &vb2_v4l2_buffer
129*4882a593Smuzhiyun  * @list: list of m2m buffers
130*4882a593Smuzhiyun  */
131*4882a593Smuzhiyun struct v4l2_m2m_buffer {
132*4882a593Smuzhiyun 	struct vb2_v4l2_buffer	vb;
133*4882a593Smuzhiyun 	struct list_head	list;
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * v4l2_m2m_get_curr_priv() - return driver private data for the currently
138*4882a593Smuzhiyun  * running instance or NULL if no instance is running
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /**
145*4882a593Smuzhiyun  * v4l2_m2m_get_vq() - return vb2_queue for the given type
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
148*4882a593Smuzhiyun  * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
151*4882a593Smuzhiyun 				       enum v4l2_buf_type type);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /**
154*4882a593Smuzhiyun  * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
155*4882a593Smuzhiyun  * the pending job queue and add it if so.
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
158*4882a593Smuzhiyun  *
159*4882a593Smuzhiyun  * There are three basic requirements an instance has to meet to be able to run:
160*4882a593Smuzhiyun  * 1) at least one source buffer has to be queued,
161*4882a593Smuzhiyun  * 2) at least one destination buffer has to be queued,
162*4882a593Smuzhiyun  * 3) streaming has to be on.
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  * If a queue is buffered (for example a decoder hardware ringbuffer that has
165*4882a593Smuzhiyun  * to be drained before doing streamoff), allow scheduling without v4l2 buffers
166*4882a593Smuzhiyun  * on that queue.
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * There may also be additional, custom requirements. In such case the driver
169*4882a593Smuzhiyun  * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
170*4882a593Smuzhiyun  * return 1 if the instance is ready.
171*4882a593Smuzhiyun  * An example of the above could be an instance that requires more than one
172*4882a593Smuzhiyun  * src/dst buffer per transaction.
173*4882a593Smuzhiyun  */
174*4882a593Smuzhiyun void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun  * v4l2_m2m_job_finish() - inform the framework that a job has been finished
178*4882a593Smuzhiyun  * and have it clean up
179*4882a593Smuzhiyun  *
180*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
181*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * Called by a driver to yield back the device after it has finished with it.
184*4882a593Smuzhiyun  * Should be called as soon as possible after reaching a state which allows
185*4882a593Smuzhiyun  * other instances to take control of the device.
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * This function has to be called only after &v4l2_m2m_ops->device_run
188*4882a593Smuzhiyun  * callback has been called on the driver. To prevent recursion, it should
189*4882a593Smuzhiyun  * not be called directly from the &v4l2_m2m_ops->device_run callback though.
190*4882a593Smuzhiyun  */
191*4882a593Smuzhiyun void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
192*4882a593Smuzhiyun 			 struct v4l2_m2m_ctx *m2m_ctx);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun  * v4l2_m2m_buf_done_and_job_finish() - return source/destination buffers with
196*4882a593Smuzhiyun  * state and inform the framework that a job has been finished and have it
197*4882a593Smuzhiyun  * clean up
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
200*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
201*4882a593Smuzhiyun  * @state: vb2 buffer state passed to v4l2_m2m_buf_done().
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * Drivers that set V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF must use this
204*4882a593Smuzhiyun  * function instead of job_finish() to take held buffers into account. It is
205*4882a593Smuzhiyun  * optional for other drivers.
206*4882a593Smuzhiyun  *
207*4882a593Smuzhiyun  * This function removes the source buffer from the ready list and returns
208*4882a593Smuzhiyun  * it with the given state. The same is done for the destination buffer, unless
209*4882a593Smuzhiyun  * it is marked 'held'. In that case the buffer is kept on the ready list.
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  * After that the job is finished (see job_finish()).
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * This allows for multiple output buffers to be used to fill in a single
214*4882a593Smuzhiyun  * capture buffer. This is typically used by stateless decoders where
215*4882a593Smuzhiyun  * multiple e.g. H.264 slices contribute to a single decoded frame.
216*4882a593Smuzhiyun  */
217*4882a593Smuzhiyun void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
218*4882a593Smuzhiyun 				      struct v4l2_m2m_ctx *m2m_ctx,
219*4882a593Smuzhiyun 				      enum vb2_buffer_state state);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun static inline void
v4l2_m2m_buf_done(struct vb2_v4l2_buffer * buf,enum vb2_buffer_state state)222*4882a593Smuzhiyun v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	vb2_buffer_done(&buf->vb2_buf, state);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /**
228*4882a593Smuzhiyun  * v4l2_m2m_clear_state() - clear encoding/decoding state
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun static inline void
v4l2_m2m_clear_state(struct v4l2_m2m_ctx * m2m_ctx)233*4882a593Smuzhiyun v4l2_m2m_clear_state(struct v4l2_m2m_ctx *m2m_ctx)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	m2m_ctx->next_buf_last = false;
236*4882a593Smuzhiyun 	m2m_ctx->is_draining = false;
237*4882a593Smuzhiyun 	m2m_ctx->has_stopped = false;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /**
241*4882a593Smuzhiyun  * v4l2_m2m_mark_stopped() - set current encoding/decoding state as stopped
242*4882a593Smuzhiyun  *
243*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
244*4882a593Smuzhiyun  */
245*4882a593Smuzhiyun static inline void
v4l2_m2m_mark_stopped(struct v4l2_m2m_ctx * m2m_ctx)246*4882a593Smuzhiyun v4l2_m2m_mark_stopped(struct v4l2_m2m_ctx *m2m_ctx)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	m2m_ctx->next_buf_last = false;
249*4882a593Smuzhiyun 	m2m_ctx->is_draining = false;
250*4882a593Smuzhiyun 	m2m_ctx->has_stopped = true;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun /**
254*4882a593Smuzhiyun  * v4l2_m2m_dst_buf_is_last() - return the current encoding/decoding session
255*4882a593Smuzhiyun  * draining management state of next queued capture buffer
256*4882a593Smuzhiyun  *
257*4882a593Smuzhiyun  * This last capture buffer should be tagged with V4L2_BUF_FLAG_LAST to notify
258*4882a593Smuzhiyun  * the end of the capture session.
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
261*4882a593Smuzhiyun  */
262*4882a593Smuzhiyun static inline bool
v4l2_m2m_dst_buf_is_last(struct v4l2_m2m_ctx * m2m_ctx)263*4882a593Smuzhiyun v4l2_m2m_dst_buf_is_last(struct v4l2_m2m_ctx *m2m_ctx)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	return m2m_ctx->is_draining && m2m_ctx->next_buf_last;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun /**
269*4882a593Smuzhiyun  * v4l2_m2m_has_stopped() - return the current encoding/decoding session
270*4882a593Smuzhiyun  * stopped state
271*4882a593Smuzhiyun  *
272*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
273*4882a593Smuzhiyun  */
274*4882a593Smuzhiyun static inline bool
v4l2_m2m_has_stopped(struct v4l2_m2m_ctx * m2m_ctx)275*4882a593Smuzhiyun v4l2_m2m_has_stopped(struct v4l2_m2m_ctx *m2m_ctx)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	return m2m_ctx->has_stopped;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /**
281*4882a593Smuzhiyun  * v4l2_m2m_is_last_draining_src_buf() - return the output buffer draining
282*4882a593Smuzhiyun  * state in the current encoding/decoding session
283*4882a593Smuzhiyun  *
284*4882a593Smuzhiyun  * This will identify the last output buffer queued before a session stop
285*4882a593Smuzhiyun  * was required, leading to an actual encoding/decoding session stop state
286*4882a593Smuzhiyun  * in the encoding/decoding process after being processed.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
289*4882a593Smuzhiyun  * @vbuf: pointer to struct &v4l2_buffer
290*4882a593Smuzhiyun  */
291*4882a593Smuzhiyun static inline bool
v4l2_m2m_is_last_draining_src_buf(struct v4l2_m2m_ctx * m2m_ctx,struct vb2_v4l2_buffer * vbuf)292*4882a593Smuzhiyun v4l2_m2m_is_last_draining_src_buf(struct v4l2_m2m_ctx *m2m_ctx,
293*4882a593Smuzhiyun 				  struct vb2_v4l2_buffer *vbuf)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	return m2m_ctx->is_draining && vbuf == m2m_ctx->last_src_buf;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun  * v4l2_m2m_last_buffer_done() - marks the buffer with LAST flag and DONE
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
302*4882a593Smuzhiyun  * @vbuf: pointer to struct &v4l2_buffer
303*4882a593Smuzhiyun  */
304*4882a593Smuzhiyun void v4l2_m2m_last_buffer_done(struct v4l2_m2m_ctx *m2m_ctx,
305*4882a593Smuzhiyun 			       struct vb2_v4l2_buffer *vbuf);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun /**
308*4882a593Smuzhiyun  * v4l2_m2m_suspend() - stop new jobs from being run and wait for current job
309*4882a593Smuzhiyun  * to finish
310*4882a593Smuzhiyun  *
311*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  * Called by a driver in the suspend hook. Stop new jobs from being run, and
314*4882a593Smuzhiyun  * wait for current running job to finish.
315*4882a593Smuzhiyun  */
316*4882a593Smuzhiyun void v4l2_m2m_suspend(struct v4l2_m2m_dev *m2m_dev);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /**
319*4882a593Smuzhiyun  * v4l2_m2m_resume() - resume job running and try to run a queued job
320*4882a593Smuzhiyun  *
321*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
322*4882a593Smuzhiyun  *
323*4882a593Smuzhiyun  * Called by a driver in the resume hook. This reverts the operation of
324*4882a593Smuzhiyun  * v4l2_m2m_suspend() and allows job to be run. Also try to run a queued job if
325*4882a593Smuzhiyun  * there is any.
326*4882a593Smuzhiyun  */
327*4882a593Smuzhiyun void v4l2_m2m_resume(struct v4l2_m2m_dev *m2m_dev);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun  * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
331*4882a593Smuzhiyun  *
332*4882a593Smuzhiyun  * @file: pointer to struct &file
333*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
334*4882a593Smuzhiyun  * @reqbufs: pointer to struct &v4l2_requestbuffers
335*4882a593Smuzhiyun  */
336*4882a593Smuzhiyun int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
337*4882a593Smuzhiyun 		     struct v4l2_requestbuffers *reqbufs);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun /**
340*4882a593Smuzhiyun  * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
341*4882a593Smuzhiyun  *
342*4882a593Smuzhiyun  * @file: pointer to struct &file
343*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
344*4882a593Smuzhiyun  * @buf: pointer to struct &v4l2_buffer
345*4882a593Smuzhiyun  *
346*4882a593Smuzhiyun  * See v4l2_m2m_mmap() documentation for details.
347*4882a593Smuzhiyun  */
348*4882a593Smuzhiyun int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
349*4882a593Smuzhiyun 		      struct v4l2_buffer *buf);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun  * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
353*4882a593Smuzhiyun  * the type
354*4882a593Smuzhiyun  *
355*4882a593Smuzhiyun  * @file: pointer to struct &file
356*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
357*4882a593Smuzhiyun  * @buf: pointer to struct &v4l2_buffer
358*4882a593Smuzhiyun  */
359*4882a593Smuzhiyun int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
360*4882a593Smuzhiyun 		  struct v4l2_buffer *buf);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun /**
363*4882a593Smuzhiyun  * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
364*4882a593Smuzhiyun  * the type
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  * @file: pointer to struct &file
367*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
368*4882a593Smuzhiyun  * @buf: pointer to struct &v4l2_buffer
369*4882a593Smuzhiyun  */
370*4882a593Smuzhiyun int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
371*4882a593Smuzhiyun 		   struct v4l2_buffer *buf);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /**
374*4882a593Smuzhiyun  * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on
375*4882a593Smuzhiyun  * the type
376*4882a593Smuzhiyun  *
377*4882a593Smuzhiyun  * @file: pointer to struct &file
378*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
379*4882a593Smuzhiyun  * @buf: pointer to struct &v4l2_buffer
380*4882a593Smuzhiyun  */
381*4882a593Smuzhiyun int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
382*4882a593Smuzhiyun 			 struct v4l2_buffer *buf);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun  * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
386*4882a593Smuzhiyun  * on the type
387*4882a593Smuzhiyun  *
388*4882a593Smuzhiyun  * @file: pointer to struct &file
389*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
390*4882a593Smuzhiyun  * @create: pointer to struct &v4l2_create_buffers
391*4882a593Smuzhiyun  */
392*4882a593Smuzhiyun int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
393*4882a593Smuzhiyun 			 struct v4l2_create_buffers *create);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /**
396*4882a593Smuzhiyun  * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
397*4882a593Smuzhiyun  * the type
398*4882a593Smuzhiyun  *
399*4882a593Smuzhiyun  * @file: pointer to struct &file
400*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
401*4882a593Smuzhiyun  * @eb: pointer to struct &v4l2_exportbuffer
402*4882a593Smuzhiyun  */
403*4882a593Smuzhiyun int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
404*4882a593Smuzhiyun 		   struct v4l2_exportbuffer *eb);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun  * v4l2_m2m_streamon() - turn on streaming for a video queue
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  * @file: pointer to struct &file
410*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
411*4882a593Smuzhiyun  * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
412*4882a593Smuzhiyun  */
413*4882a593Smuzhiyun int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
414*4882a593Smuzhiyun 		      enum v4l2_buf_type type);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun /**
417*4882a593Smuzhiyun  * v4l2_m2m_streamoff() - turn off streaming for a video queue
418*4882a593Smuzhiyun  *
419*4882a593Smuzhiyun  * @file: pointer to struct &file
420*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
421*4882a593Smuzhiyun  * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
422*4882a593Smuzhiyun  */
423*4882a593Smuzhiyun int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
424*4882a593Smuzhiyun 		       enum v4l2_buf_type type);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun /**
427*4882a593Smuzhiyun  * v4l2_m2m_update_start_streaming_state() - update the encoding/decoding
428*4882a593Smuzhiyun  * session state when a start of streaming of a video queue is requested
429*4882a593Smuzhiyun  *
430*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
431*4882a593Smuzhiyun  * @q: queue
432*4882a593Smuzhiyun  */
433*4882a593Smuzhiyun void v4l2_m2m_update_start_streaming_state(struct v4l2_m2m_ctx *m2m_ctx,
434*4882a593Smuzhiyun 					   struct vb2_queue *q);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun /**
437*4882a593Smuzhiyun  * v4l2_m2m_update_stop_streaming_state() -  update the encoding/decoding
438*4882a593Smuzhiyun  * session state when a stop of streaming of a video queue is requested
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
441*4882a593Smuzhiyun  * @q: queue
442*4882a593Smuzhiyun  */
443*4882a593Smuzhiyun void v4l2_m2m_update_stop_streaming_state(struct v4l2_m2m_ctx *m2m_ctx,
444*4882a593Smuzhiyun 					  struct vb2_queue *q);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /**
447*4882a593Smuzhiyun  * v4l2_m2m_encoder_cmd() - execute an encoder command
448*4882a593Smuzhiyun  *
449*4882a593Smuzhiyun  * @file: pointer to struct &file
450*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
451*4882a593Smuzhiyun  * @ec: pointer to the encoder command
452*4882a593Smuzhiyun  */
453*4882a593Smuzhiyun int v4l2_m2m_encoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
454*4882a593Smuzhiyun 			 struct v4l2_encoder_cmd *ec);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /**
457*4882a593Smuzhiyun  * v4l2_m2m_decoder_cmd() - execute a decoder command
458*4882a593Smuzhiyun  *
459*4882a593Smuzhiyun  * @file: pointer to struct &file
460*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
461*4882a593Smuzhiyun  * @dc: pointer to the decoder command
462*4882a593Smuzhiyun  */
463*4882a593Smuzhiyun int v4l2_m2m_decoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
464*4882a593Smuzhiyun 			 struct v4l2_decoder_cmd *dc);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun /**
467*4882a593Smuzhiyun  * v4l2_m2m_poll() - poll replacement, for destination buffers only
468*4882a593Smuzhiyun  *
469*4882a593Smuzhiyun  * @file: pointer to struct &file
470*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
471*4882a593Smuzhiyun  * @wait: pointer to struct &poll_table_struct
472*4882a593Smuzhiyun  *
473*4882a593Smuzhiyun  * Call from the driver's poll() function. Will poll both queues. If a buffer
474*4882a593Smuzhiyun  * is available to dequeue (with dqbuf) from the source queue, this will
475*4882a593Smuzhiyun  * indicate that a non-blocking write can be performed, while read will be
476*4882a593Smuzhiyun  * returned in case of the destination queue.
477*4882a593Smuzhiyun  */
478*4882a593Smuzhiyun __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
479*4882a593Smuzhiyun 			   struct poll_table_struct *wait);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun /**
482*4882a593Smuzhiyun  * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
483*4882a593Smuzhiyun  *
484*4882a593Smuzhiyun  * @file: pointer to struct &file
485*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
486*4882a593Smuzhiyun  * @vma: pointer to struct &vm_area_struct
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * Call from driver's mmap() function. Will handle mmap() for both queues
489*4882a593Smuzhiyun  * seamlessly for videobuffer, which will receive normal per-queue offsets and
490*4882a593Smuzhiyun  * proper videobuf queue pointers. The differentiation is made outside videobuf
491*4882a593Smuzhiyun  * by adding a predefined offset to buffers from one of the queues and
492*4882a593Smuzhiyun  * subtracting it before passing it back to videobuf. Only drivers (and
493*4882a593Smuzhiyun  * thus applications) receive modified offsets.
494*4882a593Smuzhiyun  */
495*4882a593Smuzhiyun int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
496*4882a593Smuzhiyun 		  struct vm_area_struct *vma);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun /**
499*4882a593Smuzhiyun  * v4l2_m2m_init() - initialize per-driver m2m data
500*4882a593Smuzhiyun  *
501*4882a593Smuzhiyun  * @m2m_ops: pointer to struct v4l2_m2m_ops
502*4882a593Smuzhiyun  *
503*4882a593Smuzhiyun  * Usually called from driver's ``probe()`` function.
504*4882a593Smuzhiyun  *
505*4882a593Smuzhiyun  * Return: returns an opaque pointer to the internal data to handle M2M context
506*4882a593Smuzhiyun  */
507*4882a593Smuzhiyun struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun #if defined(CONFIG_MEDIA_CONTROLLER)
510*4882a593Smuzhiyun void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev);
511*4882a593Smuzhiyun int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
512*4882a593Smuzhiyun 			struct video_device *vdev, int function);
513*4882a593Smuzhiyun #else
514*4882a593Smuzhiyun static inline void
v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev * m2m_dev)515*4882a593Smuzhiyun v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun static inline int
v4l2_m2m_register_media_controller(struct v4l2_m2m_dev * m2m_dev,struct video_device * vdev,int function)520*4882a593Smuzhiyun v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
521*4882a593Smuzhiyun 		struct video_device *vdev, int function)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	return 0;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun #endif
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun /**
528*4882a593Smuzhiyun  * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
531*4882a593Smuzhiyun  *
532*4882a593Smuzhiyun  * Usually called from driver's ``remove()`` function.
533*4882a593Smuzhiyun  */
534*4882a593Smuzhiyun void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun /**
537*4882a593Smuzhiyun  * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
538*4882a593Smuzhiyun  *
539*4882a593Smuzhiyun  * @m2m_dev: opaque pointer to the internal data to handle M2M context
540*4882a593Smuzhiyun  * @drv_priv: driver's instance private data
541*4882a593Smuzhiyun  * @queue_init: a callback for queue type-specific initialization function
542*4882a593Smuzhiyun  *	to be used for initializing videobuf_queues
543*4882a593Smuzhiyun  *
544*4882a593Smuzhiyun  * Usually called from driver's ``open()`` function.
545*4882a593Smuzhiyun  */
546*4882a593Smuzhiyun struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
547*4882a593Smuzhiyun 		void *drv_priv,
548*4882a593Smuzhiyun 		int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq));
549*4882a593Smuzhiyun 
v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx * m2m_ctx,bool buffered)550*4882a593Smuzhiyun static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx,
551*4882a593Smuzhiyun 					     bool buffered)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun 	m2m_ctx->out_q_ctx.buffered = buffered;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx * m2m_ctx,bool buffered)556*4882a593Smuzhiyun static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx,
557*4882a593Smuzhiyun 					     bool buffered)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun 	m2m_ctx->cap_q_ctx.buffered = buffered;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun /**
563*4882a593Smuzhiyun  * v4l2_m2m_ctx_release() - release m2m context
564*4882a593Smuzhiyun  *
565*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  * Usually called from driver's release() function.
568*4882a593Smuzhiyun  */
569*4882a593Smuzhiyun void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun  * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
573*4882a593Smuzhiyun  *
574*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
575*4882a593Smuzhiyun  * @vbuf: pointer to struct &vb2_v4l2_buffer
576*4882a593Smuzhiyun  *
577*4882a593Smuzhiyun  * Call from videobuf_queue_ops->ops->buf_queue, videobuf_queue_ops callback.
578*4882a593Smuzhiyun  */
579*4882a593Smuzhiyun void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
580*4882a593Smuzhiyun 			struct vb2_v4l2_buffer *vbuf);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun /**
583*4882a593Smuzhiyun  * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for
584*4882a593Smuzhiyun  * use
585*4882a593Smuzhiyun  *
586*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
587*4882a593Smuzhiyun  */
588*4882a593Smuzhiyun static inline
v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx * m2m_ctx)589*4882a593Smuzhiyun unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	return m2m_ctx->out_q_ctx.num_rdy;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /**
595*4882a593Smuzhiyun  * v4l2_m2m_num_dst_bufs_ready() - return the number of destination buffers
596*4882a593Smuzhiyun  * ready for use
597*4882a593Smuzhiyun  *
598*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
599*4882a593Smuzhiyun  */
600*4882a593Smuzhiyun static inline
v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx * m2m_ctx)601*4882a593Smuzhiyun unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	return m2m_ctx->cap_q_ctx.num_rdy;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun /**
607*4882a593Smuzhiyun  * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
610*4882a593Smuzhiyun  */
611*4882a593Smuzhiyun struct vb2_v4l2_buffer *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /**
614*4882a593Smuzhiyun  * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready
615*4882a593Smuzhiyun  * buffers
616*4882a593Smuzhiyun  *
617*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
618*4882a593Smuzhiyun  */
619*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx * m2m_ctx)620*4882a593Smuzhiyun v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun /**
626*4882a593Smuzhiyun  * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of
627*4882a593Smuzhiyun  * ready buffers
628*4882a593Smuzhiyun  *
629*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
630*4882a593Smuzhiyun  */
631*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx * m2m_ctx)632*4882a593Smuzhiyun v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun /**
638*4882a593Smuzhiyun  * v4l2_m2m_last_buf() - return last buffer from the list of ready buffers
639*4882a593Smuzhiyun  *
640*4882a593Smuzhiyun  * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
641*4882a593Smuzhiyun  */
642*4882a593Smuzhiyun struct vb2_v4l2_buffer *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun /**
645*4882a593Smuzhiyun  * v4l2_m2m_last_src_buf() - return last destination buffer from the list of
646*4882a593Smuzhiyun  * ready buffers
647*4882a593Smuzhiyun  *
648*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
649*4882a593Smuzhiyun  */
650*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_last_src_buf(struct v4l2_m2m_ctx * m2m_ctx)651*4882a593Smuzhiyun v4l2_m2m_last_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
652*4882a593Smuzhiyun {
653*4882a593Smuzhiyun 	return v4l2_m2m_last_buf(&m2m_ctx->out_q_ctx);
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun /**
657*4882a593Smuzhiyun  * v4l2_m2m_last_dst_buf() - return last destination buffer from the list of
658*4882a593Smuzhiyun  * ready buffers
659*4882a593Smuzhiyun  *
660*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
661*4882a593Smuzhiyun  */
662*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_last_dst_buf(struct v4l2_m2m_ctx * m2m_ctx)663*4882a593Smuzhiyun v4l2_m2m_last_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	return v4l2_m2m_last_buf(&m2m_ctx->cap_q_ctx);
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun /**
669*4882a593Smuzhiyun  * v4l2_m2m_for_each_dst_buf() - iterate over a list of destination ready
670*4882a593Smuzhiyun  * buffers
671*4882a593Smuzhiyun  *
672*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
673*4882a593Smuzhiyun  * @b: current buffer of type struct v4l2_m2m_buffer
674*4882a593Smuzhiyun  */
675*4882a593Smuzhiyun #define v4l2_m2m_for_each_dst_buf(m2m_ctx, b)	\
676*4882a593Smuzhiyun 	list_for_each_entry(b, &m2m_ctx->cap_q_ctx.rdy_queue, list)
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /**
679*4882a593Smuzhiyun  * v4l2_m2m_for_each_src_buf() - iterate over a list of source ready buffers
680*4882a593Smuzhiyun  *
681*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
682*4882a593Smuzhiyun  * @b: current buffer of type struct v4l2_m2m_buffer
683*4882a593Smuzhiyun  */
684*4882a593Smuzhiyun #define v4l2_m2m_for_each_src_buf(m2m_ctx, b)	\
685*4882a593Smuzhiyun 	list_for_each_entry(b, &m2m_ctx->out_q_ctx.rdy_queue, list)
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun /**
688*4882a593Smuzhiyun  * v4l2_m2m_for_each_dst_buf_safe() - iterate over a list of destination ready
689*4882a593Smuzhiyun  * buffers safely
690*4882a593Smuzhiyun  *
691*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
692*4882a593Smuzhiyun  * @b: current buffer of type struct v4l2_m2m_buffer
693*4882a593Smuzhiyun  * @n: used as temporary storage
694*4882a593Smuzhiyun  */
695*4882a593Smuzhiyun #define v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, b, n)	\
696*4882a593Smuzhiyun 	list_for_each_entry_safe(b, n, &m2m_ctx->cap_q_ctx.rdy_queue, list)
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun /**
699*4882a593Smuzhiyun  * v4l2_m2m_for_each_src_buf_safe() - iterate over a list of source ready
700*4882a593Smuzhiyun  * buffers safely
701*4882a593Smuzhiyun  *
702*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
703*4882a593Smuzhiyun  * @b: current buffer of type struct v4l2_m2m_buffer
704*4882a593Smuzhiyun  * @n: used as temporary storage
705*4882a593Smuzhiyun  */
706*4882a593Smuzhiyun #define v4l2_m2m_for_each_src_buf_safe(m2m_ctx, b, n)	\
707*4882a593Smuzhiyun 	list_for_each_entry_safe(b, n, &m2m_ctx->out_q_ctx.rdy_queue, list)
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun /**
710*4882a593Smuzhiyun  * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers
711*4882a593Smuzhiyun  *
712*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
713*4882a593Smuzhiyun  */
714*4882a593Smuzhiyun static inline
v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx * m2m_ctx)715*4882a593Smuzhiyun struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun 	return &m2m_ctx->out_q_ctx.q;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /**
721*4882a593Smuzhiyun  * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers
722*4882a593Smuzhiyun  *
723*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
724*4882a593Smuzhiyun  */
725*4882a593Smuzhiyun static inline
v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx * m2m_ctx)726*4882a593Smuzhiyun struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	return &m2m_ctx->cap_q_ctx.q;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun /**
732*4882a593Smuzhiyun  * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
733*4882a593Smuzhiyun  * return it
734*4882a593Smuzhiyun  *
735*4882a593Smuzhiyun  * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
736*4882a593Smuzhiyun  */
737*4882a593Smuzhiyun struct vb2_v4l2_buffer *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun /**
740*4882a593Smuzhiyun  * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready
741*4882a593Smuzhiyun  * buffers and return it
742*4882a593Smuzhiyun  *
743*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
744*4882a593Smuzhiyun  */
745*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx * m2m_ctx)746*4882a593Smuzhiyun v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx);
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun /**
752*4882a593Smuzhiyun  * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of
753*4882a593Smuzhiyun  * ready buffers and return it
754*4882a593Smuzhiyun  *
755*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
756*4882a593Smuzhiyun  */
757*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx * m2m_ctx)758*4882a593Smuzhiyun v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx);
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun /**
764*4882a593Smuzhiyun  * v4l2_m2m_buf_remove_by_buf() - take off exact buffer from the list of ready
765*4882a593Smuzhiyun  * buffers
766*4882a593Smuzhiyun  *
767*4882a593Smuzhiyun  * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
768*4882a593Smuzhiyun  * @vbuf: the buffer to be removed
769*4882a593Smuzhiyun  */
770*4882a593Smuzhiyun void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx,
771*4882a593Smuzhiyun 				struct vb2_v4l2_buffer *vbuf);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun /**
774*4882a593Smuzhiyun  * v4l2_m2m_src_buf_remove_by_buf() - take off exact source buffer from the list
775*4882a593Smuzhiyun  * of ready buffers
776*4882a593Smuzhiyun  *
777*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
778*4882a593Smuzhiyun  * @vbuf: the buffer to be removed
779*4882a593Smuzhiyun  */
v4l2_m2m_src_buf_remove_by_buf(struct v4l2_m2m_ctx * m2m_ctx,struct vb2_v4l2_buffer * vbuf)780*4882a593Smuzhiyun static inline void v4l2_m2m_src_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx,
781*4882a593Smuzhiyun 						  struct vb2_v4l2_buffer *vbuf)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun 	v4l2_m2m_buf_remove_by_buf(&m2m_ctx->out_q_ctx, vbuf);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun /**
787*4882a593Smuzhiyun  * v4l2_m2m_dst_buf_remove_by_buf() - take off exact destination buffer from the
788*4882a593Smuzhiyun  * list of ready buffers
789*4882a593Smuzhiyun  *
790*4882a593Smuzhiyun  * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
791*4882a593Smuzhiyun  * @vbuf: the buffer to be removed
792*4882a593Smuzhiyun  */
v4l2_m2m_dst_buf_remove_by_buf(struct v4l2_m2m_ctx * m2m_ctx,struct vb2_v4l2_buffer * vbuf)793*4882a593Smuzhiyun static inline void v4l2_m2m_dst_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx,
794*4882a593Smuzhiyun 						  struct vb2_v4l2_buffer *vbuf)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun 	v4l2_m2m_buf_remove_by_buf(&m2m_ctx->cap_q_ctx, vbuf);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun struct vb2_v4l2_buffer *
800*4882a593Smuzhiyun v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_src_buf_remove_by_idx(struct v4l2_m2m_ctx * m2m_ctx,unsigned int idx)803*4882a593Smuzhiyun v4l2_m2m_src_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->out_q_ctx, idx);
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun static inline struct vb2_v4l2_buffer *
v4l2_m2m_dst_buf_remove_by_idx(struct v4l2_m2m_ctx * m2m_ctx,unsigned int idx)809*4882a593Smuzhiyun v4l2_m2m_dst_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun 	return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->cap_q_ctx, idx);
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun /**
815*4882a593Smuzhiyun  * v4l2_m2m_buf_copy_metadata() - copy buffer metadata from
816*4882a593Smuzhiyun  * the output buffer to the capture buffer
817*4882a593Smuzhiyun  *
818*4882a593Smuzhiyun  * @out_vb: the output buffer that is the source of the metadata.
819*4882a593Smuzhiyun  * @cap_vb: the capture buffer that will receive the metadata.
820*4882a593Smuzhiyun  * @copy_frame_flags: copy the KEY/B/PFRAME flags as well.
821*4882a593Smuzhiyun  *
822*4882a593Smuzhiyun  * This helper function copies the timestamp, timecode (if the TIMECODE
823*4882a593Smuzhiyun  * buffer flag was set), field and the TIMECODE, KEYFRAME, BFRAME, PFRAME
824*4882a593Smuzhiyun  * and TSTAMP_SRC_MASK flags from @out_vb to @cap_vb.
825*4882a593Smuzhiyun  *
826*4882a593Smuzhiyun  * If @copy_frame_flags is false, then the KEYFRAME, BFRAME and PFRAME
827*4882a593Smuzhiyun  * flags are not copied. This is typically needed for encoders that
828*4882a593Smuzhiyun  * set this bits explicitly.
829*4882a593Smuzhiyun  */
830*4882a593Smuzhiyun void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb,
831*4882a593Smuzhiyun 				struct vb2_v4l2_buffer *cap_vb,
832*4882a593Smuzhiyun 				bool copy_frame_flags);
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun /* v4l2 request helper */
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun void v4l2_m2m_request_queue(struct media_request *req);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun /* v4l2 ioctl helpers */
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
841*4882a593Smuzhiyun 				struct v4l2_requestbuffers *rb);
842*4882a593Smuzhiyun int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh,
843*4882a593Smuzhiyun 				struct v4l2_create_buffers *create);
844*4882a593Smuzhiyun int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh,
845*4882a593Smuzhiyun 				struct v4l2_buffer *buf);
846*4882a593Smuzhiyun int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,
847*4882a593Smuzhiyun 				struct v4l2_exportbuffer *eb);
848*4882a593Smuzhiyun int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh,
849*4882a593Smuzhiyun 				struct v4l2_buffer *buf);
850*4882a593Smuzhiyun int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh,
851*4882a593Smuzhiyun 				struct v4l2_buffer *buf);
852*4882a593Smuzhiyun int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh,
853*4882a593Smuzhiyun 			       struct v4l2_buffer *buf);
854*4882a593Smuzhiyun int v4l2_m2m_ioctl_streamon(struct file *file, void *fh,
855*4882a593Smuzhiyun 				enum v4l2_buf_type type);
856*4882a593Smuzhiyun int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh,
857*4882a593Smuzhiyun 				enum v4l2_buf_type type);
858*4882a593Smuzhiyun int v4l2_m2m_ioctl_encoder_cmd(struct file *file, void *fh,
859*4882a593Smuzhiyun 			       struct v4l2_encoder_cmd *ec);
860*4882a593Smuzhiyun int v4l2_m2m_ioctl_decoder_cmd(struct file *file, void *fh,
861*4882a593Smuzhiyun 			       struct v4l2_decoder_cmd *dc);
862*4882a593Smuzhiyun int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *fh,
863*4882a593Smuzhiyun 				   struct v4l2_encoder_cmd *ec);
864*4882a593Smuzhiyun int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *fh,
865*4882a593Smuzhiyun 				   struct v4l2_decoder_cmd *dc);
866*4882a593Smuzhiyun int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *fh,
867*4882a593Smuzhiyun 					     struct v4l2_decoder_cmd *dc);
868*4882a593Smuzhiyun int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv,
869*4882a593Smuzhiyun 					 struct v4l2_decoder_cmd *dc);
870*4882a593Smuzhiyun int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma);
871*4882a593Smuzhiyun __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun #endif /* _MEDIA_V4L2_MEM2MEM_H */
874*4882a593Smuzhiyun 
875