blob: 1d85e24791e42f650708a78bec3a2917b095638f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Pawel Osciak7f986392010-04-23 05:38:37 -03002/*
3 * Memory-to-memory device framework for Video for Linux 2.
4 *
5 * Helper functions for devices that use memory buffers for both source
6 * and destination.
7 *
8 * Copyright (c) 2009 Samsung Electronics Co., Ltd.
Pawel Osciak95072082011-03-13 15:23:32 -03009 * Pawel Osciak, <pawel@osciak.com>
Pawel Osciak7f986392010-04-23 05:38:37 -030010 * Marek Szyprowski, <m.szyprowski@samsung.com>
Pawel Osciak7f986392010-04-23 05:38:37 -030011 */
12
13#ifndef _MEDIA_V4L2_MEM2MEM_H
14#define _MEDIA_V4L2_MEM2MEM_H
15
Junghak Sungc1399902015-09-22 10:30:29 -030016#include <media/videobuf2-v4l2.h>
Pawel Osciak7f986392010-04-23 05:38:37 -030017
18/**
19 * struct v4l2_m2m_ops - mem-to-mem device driver callbacks
20 * @device_run: required. Begin the actual job (transaction) inside this
21 * callback.
22 * The job does NOT have to end before this callback returns
23 * (and it will be the usual case). When the job finishes,
Hans Verkuilf8cca8c2019-10-11 06:32:41 -030024 * v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish()
25 * has to be called.
Pawel Osciak7f986392010-04-23 05:38:37 -030026 * @job_ready: optional. Should return 0 if the driver does not have a job
27 * fully prepared to run yet (i.e. it will not be able to finish a
28 * transaction without sleeping). If not provided, it will be
29 * assumed that one source and one destination buffer are all
30 * that is required for the driver to perform one full transaction.
31 * This method may not sleep.
Ezequiel Garcia5525b832018-06-18 00:38:52 -040032 * @job_abort: optional. Informs the driver that it has to abort the currently
Pawel Osciak7f986392010-04-23 05:38:37 -030033 * running transaction as soon as possible (i.e. as soon as it can
34 * stop the device safely; e.g. in the next interrupt handler),
35 * even if the transaction would not have been finished by then.
36 * After the driver performs the necessary steps, it has to call
Hans Verkuilf8cca8c2019-10-11 06:32:41 -030037 * v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish() as
38 * if the transaction ended normally.
Pawel Osciak7f986392010-04-23 05:38:37 -030039 * This function does not have to (and will usually not) wait
40 * until the device enters a state when it can be stopped.
41 */
42struct v4l2_m2m_ops {
43 void (*device_run)(void *priv);
44 int (*job_ready)(void *priv);
45 void (*job_abort)(void *priv);
46};
47
Ezequiel Garciabe2fff62018-07-02 11:36:05 -040048struct video_device;
Pawel Osciak7f986392010-04-23 05:38:37 -030049struct v4l2_m2m_dev;
50
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -030051/**
52 * struct v4l2_m2m_queue_ctx - represents a queue for buffers ready to be
53 * processed
54 *
55 * @q: pointer to struct &vb2_queue
56 * @rdy_queue: List of V4L2 mem-to-mem queues
57 * @rdy_spinlock: spin lock to protect the struct usage
58 * @num_rdy: number of buffers ready to be processed
59 * @buffered: is the queue buffered?
60 *
61 * Queue for buffers ready to be processed as soon as this
62 * instance receives access to the device.
63 */
64
Pawel Osciak7f986392010-04-23 05:38:37 -030065struct v4l2_m2m_queue_ctx {
Marek Szyprowski908a0d72011-01-12 06:50:24 -030066 struct vb2_queue q;
Pawel Osciak7f986392010-04-23 05:38:37 -030067
Pawel Osciak7f986392010-04-23 05:38:37 -030068 struct list_head rdy_queue;
Marek Szyprowski908a0d72011-01-12 06:50:24 -030069 spinlock_t rdy_spinlock;
Pawel Osciak7f986392010-04-23 05:38:37 -030070 u8 num_rdy;
Philipp Zabel33bdd5a2013-06-03 04:23:48 -030071 bool buffered;
Pawel Osciak7f986392010-04-23 05:38:37 -030072};
73
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -030074/**
75 * struct v4l2_m2m_ctx - Memory to memory context structure
76 *
77 * @q_lock: struct &mutex lock
Hans Verkuilf07602a2019-10-11 06:32:44 -030078 * @new_frame: valid in the device_run callback: if true, then this
79 * starts a new frame; if false, then this is a new slice
80 * for an existing frame. This is always true unless
81 * V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF is set, which
82 * indicates slicing support.
Mauro Carvalho Chehab9f8d3a22016-09-08 17:20:44 -030083 * @m2m_dev: opaque pointer to the internal data to handle M2M context
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -030084 * @cap_q_ctx: Capture (output to memory) queue context
85 * @out_q_ctx: Output (input from memory) queue context
86 * @queue: List of memory to memory contexts
87 * @job_flags: Job queue flags, used internally by v4l2-mem2mem.c:
88 * %TRANS_QUEUED, %TRANS_RUNNING and %TRANS_ABORT.
89 * @finished: Wait queue used to signalize when a job queue finished.
90 * @priv: Instance private data
Sakari Ailus708f48e2016-10-18 11:15:32 -020091 *
92 * The memory to memory context is specific to a file handle, NOT to e.g.
93 * a device.
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -030094 */
Pawel Osciak7f986392010-04-23 05:38:37 -030095struct v4l2_m2m_ctx {
Sylwester Nawrocki8e6e8f92013-09-14 18:39:04 -030096 /* optional cap/out vb2 queues lock */
97 struct mutex *q_lock;
98
Hans Verkuilf07602a2019-10-11 06:32:44 -030099 bool new_frame;
100
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300101 /* internal use only */
Pawel Osciak7f986392010-04-23 05:38:37 -0300102 struct v4l2_m2m_dev *m2m_dev;
103
Pawel Osciak7f986392010-04-23 05:38:37 -0300104 struct v4l2_m2m_queue_ctx cap_q_ctx;
105
Pawel Osciak7f986392010-04-23 05:38:37 -0300106 struct v4l2_m2m_queue_ctx out_q_ctx;
107
108 /* For device job queue */
109 struct list_head queue;
110 unsigned long job_flags;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300111 wait_queue_head_t finished;
Pawel Osciak7f986392010-04-23 05:38:37 -0300112
Pawel Osciak7f986392010-04-23 05:38:37 -0300113 void *priv;
114};
115
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300116/**
117 * struct v4l2_m2m_buffer - Memory to memory buffer
118 *
119 * @vb: pointer to struct &vb2_v4l2_buffer
120 * @list: list of m2m buffers
121 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300122struct v4l2_m2m_buffer {
Junghak Sung2d700712015-09-22 10:30:30 -0300123 struct vb2_v4l2_buffer vb;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300124 struct list_head list;
125};
126
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300127/**
128 * v4l2_m2m_get_curr_priv() - return driver private data for the currently
129 * running instance or NULL if no instance is running
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300130 *
Mauro Carvalho Chehab9f8d3a22016-09-08 17:20:44 -0300131 * @m2m_dev: opaque pointer to the internal data to handle M2M context
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300132 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300133void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev);
134
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300135/**
136 * v4l2_m2m_get_vq() - return vb2_queue for the given type
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300137 *
138 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
139 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300140 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300141struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
Pawel Osciak7f986392010-04-23 05:38:37 -0300142 enum v4l2_buf_type type);
143
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300144/**
145 * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
146 * the pending job queue and add it if so.
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300147 *
148 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300149 *
150 * There are three basic requirements an instance has to meet to be able to run:
151 * 1) at least one source buffer has to be queued,
152 * 2) at least one destination buffer has to be queued,
153 * 3) streaming has to be on.
154 *
155 * If a queue is buffered (for example a decoder hardware ringbuffer that has
156 * to be drained before doing streamoff), allow scheduling without v4l2 buffers
157 * on that queue.
158 *
159 * There may also be additional, custom requirements. In such case the driver
160 * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
161 * return 1 if the instance is ready.
162 * An example of the above could be an instance that requires more than one
163 * src/dst buffer per transaction.
164 */
Michael Olbrich1190a412014-07-22 09:36:04 -0300165void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx);
166
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300167/**
168 * v4l2_m2m_job_finish() - inform the framework that a job has been finished
169 * and have it clean up
170 *
Mauro Carvalho Chehab9f8d3a22016-09-08 17:20:44 -0300171 * @m2m_dev: opaque pointer to the internal data to handle M2M context
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300172 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
173 *
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300174 * Called by a driver to yield back the device after it has finished with it.
175 * Should be called as soon as possible after reaching a state which allows
176 * other instances to take control of the device.
177 *
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300178 * This function has to be called only after &v4l2_m2m_ops->device_run
179 * callback has been called on the driver. To prevent recursion, it should
180 * not be called directly from the &v4l2_m2m_ops->device_run callback though.
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300181 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300182void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
183 struct v4l2_m2m_ctx *m2m_ctx);
184
Hans Verkuilf8cca8c2019-10-11 06:32:41 -0300185/**
186 * v4l2_m2m_buf_done_and_job_finish() - return source/destination buffers with
187 * state and inform the framework that a job has been finished and have it
188 * clean up
189 *
190 * @m2m_dev: opaque pointer to the internal data to handle M2M context
191 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
192 * @state: vb2 buffer state passed to v4l2_m2m_buf_done().
193 *
194 * Drivers that set V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF must use this
195 * function instead of job_finish() to take held buffers into account. It is
196 * optional for other drivers.
197 *
198 * This function removes the source buffer from the ready list and returns
199 * it with the given state. The same is done for the destination buffer, unless
200 * it is marked 'held'. In that case the buffer is kept on the ready list.
201 *
202 * After that the job is finished (see job_finish()).
203 *
204 * This allows for multiple output buffers to be used to fill in a single
205 * capture buffer. This is typically used by stateless decoders where
206 * multiple e.g. H.264 slices contribute to a single decoded frame.
207 */
208void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
209 struct v4l2_m2m_ctx *m2m_ctx,
210 enum vb2_buffer_state state);
211
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300212static inline void
Junghak Sung2d700712015-09-22 10:30:30 -0300213v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300214{
Junghak Sung2d700712015-09-22 10:30:30 -0300215 vb2_buffer_done(&buf->vb2_buf, state);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300216}
217
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300218/**
219 * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300220 *
221 * @file: pointer to struct &file
222 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
223 * @reqbufs: pointer to struct &v4l2_requestbuffers
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300224 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300225int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
226 struct v4l2_requestbuffers *reqbufs);
227
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300228/**
229 * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
230 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300231 * @file: pointer to struct &file
232 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
233 * @buf: pointer to struct &v4l2_buffer
234 *
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300235 * See v4l2_m2m_mmap() documentation for details.
236 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300237int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
238 struct v4l2_buffer *buf);
239
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300240/**
241 * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
242 * the type
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300243 *
244 * @file: pointer to struct &file
245 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
246 * @buf: pointer to struct &v4l2_buffer
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300247 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300248int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
249 struct v4l2_buffer *buf);
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300250
251/**
252 * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
253 * the type
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300254 *
255 * @file: pointer to struct &file
256 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
257 * @buf: pointer to struct &v4l2_buffer
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300258 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300259int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
260 struct v4l2_buffer *buf);
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300261
262/**
263 * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on
264 * the type
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300265 *
266 * @file: pointer to struct &file
267 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
268 * @buf: pointer to struct &v4l2_buffer
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300269 */
Hans Verkuile68cf472015-06-05 11:28:50 -0300270int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
271 struct v4l2_buffer *buf);
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300272
273/**
274 * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
275 * on the type
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300276 *
277 * @file: pointer to struct &file
278 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
279 * @create: pointer to struct &v4l2_create_buffers
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300280 */
Philipp Zabel8b94ca62013-05-21 04:16:28 -0300281int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
282 struct v4l2_create_buffers *create);
Pawel Osciak7f986392010-04-23 05:38:37 -0300283
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300284/**
285 * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
286 * the type
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300287 *
288 * @file: pointer to struct &file
289 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
290 * @eb: pointer to struct &v4l2_exportbuffer
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300291 */
Tomasz Stanislawski83ae7c52012-06-14 11:32:24 -0300292int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
293 struct v4l2_exportbuffer *eb);
294
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300295/**
296 * v4l2_m2m_streamon() - turn on streaming for a video queue
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300297 *
298 * @file: pointer to struct &file
299 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
300 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300301 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300302int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
303 enum v4l2_buf_type type);
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300304
305/**
306 * v4l2_m2m_streamoff() - turn off streaming for a video queue
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300307 *
308 * @file: pointer to struct &file
309 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
310 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300311 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300312int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
313 enum v4l2_buf_type type);
314
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300315/**
316 * v4l2_m2m_poll() - poll replacement, for destination buffers only
317 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300318 * @file: pointer to struct &file
319 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
320 * @wait: pointer to struct &poll_table_struct
321 *
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300322 * Call from the driver's poll() function. Will poll both queues. If a buffer
323 * is available to dequeue (with dqbuf) from the source queue, this will
324 * indicate that a non-blocking write can be performed, while read will be
325 * returned in case of the destination queue.
326 */
Al Viroc23e0cb2017-07-03 03:02:56 -0400327__poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
Pawel Osciak7f986392010-04-23 05:38:37 -0300328 struct poll_table_struct *wait);
329
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300330/**
331 * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
332 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300333 * @file: pointer to struct &file
334 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
335 * @vma: pointer to struct &vm_area_struct
336 *
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300337 * Call from driver's mmap() function. Will handle mmap() for both queues
338 * seamlessly for videobuffer, which will receive normal per-queue offsets and
339 * proper videobuf queue pointers. The differentiation is made outside videobuf
340 * by adding a predefined offset to buffers from one of the queues and
341 * subtracting it before passing it back to videobuf. Only drivers (and
342 * thus applications) receive modified offsets.
343 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300344int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
345 struct vm_area_struct *vma);
346
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300347/**
348 * v4l2_m2m_init() - initialize per-driver m2m data
349 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300350 * @m2m_ops: pointer to struct v4l2_m2m_ops
351 *
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300352 * Usually called from driver's ``probe()`` function.
353 *
354 * Return: returns an opaque pointer to the internal data to handle M2M context
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300355 */
Guennadi Liakhovetskib1252eb2012-09-11 06:32:17 -0300356struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300357
Ezequiel Garciabe2fff62018-07-02 11:36:05 -0400358#if defined(CONFIG_MEDIA_CONTROLLER)
359void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev);
360int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
361 struct video_device *vdev, int function);
362#else
363static inline void
364v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
365{
366}
367
368static inline int
369v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
370 struct video_device *vdev, int function)
371{
372 return 0;
373}
374#endif
375
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300376/**
377 * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
378 *
Mauro Carvalho Chehab9f8d3a22016-09-08 17:20:44 -0300379 * @m2m_dev: opaque pointer to the internal data to handle M2M context
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300380 *
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300381 * Usually called from driver's ``remove()`` function.
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300382 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300383void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev);
384
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300385/**
386 * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300387 *
Mauro Carvalho Chehab9f8d3a22016-09-08 17:20:44 -0300388 * @m2m_dev: opaque pointer to the internal data to handle M2M context
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300389 * @drv_priv: driver's instance private data
390 * @queue_init: a callback for queue type-specific initialization function
391 * to be used for initializing videobuf_queues
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300392 *
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300393 * Usually called from driver's ``open()`` function.
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300394 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300395struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
396 void *drv_priv,
397 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq));
398
Philipp Zabel33bdd5a2013-06-03 04:23:48 -0300399static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx,
400 bool buffered)
401{
402 m2m_ctx->out_q_ctx.buffered = buffered;
403}
404
405static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx,
406 bool buffered)
407{
408 m2m_ctx->cap_q_ctx.buffered = buffered;
409}
410
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300411/**
412 * v4l2_m2m_ctx_release() - release m2m context
413 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300414 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
415 *
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300416 * Usually called from driver's release() function.
417 */
Pawel Osciak7f986392010-04-23 05:38:37 -0300418void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx);
419
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300420/**
421 * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
422 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300423 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
424 * @vbuf: pointer to struct &vb2_v4l2_buffer
425 *
Mauro Carvalho Chehab5fa5edb2016-09-08 10:16:36 -0300426 * Call from videobuf_queue_ops->ops->buf_queue, videobuf_queue_ops callback.
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300427 */
Junghak Sung2d700712015-09-22 10:30:30 -0300428void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
429 struct vb2_v4l2_buffer *vbuf);
Pawel Osciak7f986392010-04-23 05:38:37 -0300430
431/**
432 * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for
433 * use
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300434 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300435 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300436 */
437static inline
438unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
439{
Sascha Hauer961ae442012-08-31 09:18:04 -0300440 return m2m_ctx->out_q_ctx.num_rdy;
Pawel Osciak7f986392010-04-23 05:38:37 -0300441}
442
443/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300444 * v4l2_m2m_num_dst_bufs_ready() - return the number of destination buffers
Pawel Osciak7f986392010-04-23 05:38:37 -0300445 * ready for use
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300446 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300447 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300448 */
449static inline
450unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
451{
Sascha Hauer961ae442012-08-31 09:18:04 -0300452 return m2m_ctx->cap_q_ctx.num_rdy;
Pawel Osciak7f986392010-04-23 05:38:37 -0300453}
454
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300455/**
456 * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300457 *
458 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300459 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500460struct vb2_v4l2_buffer *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx);
Pawel Osciak7f986392010-04-23 05:38:37 -0300461
462/**
463 * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready
464 * buffers
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300465 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300466 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300467 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500468static inline struct vb2_v4l2_buffer *
469v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300470{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300471 return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx);
Pawel Osciak7f986392010-04-23 05:38:37 -0300472}
473
474/**
475 * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of
476 * ready buffers
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300477 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300478 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300479 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500480static inline struct vb2_v4l2_buffer *
481v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300482{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300483 return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx);
Pawel Osciak7f986392010-04-23 05:38:37 -0300484}
485
486/**
Hans Verkuilee1228c2018-06-26 09:26:52 -0400487 * v4l2_m2m_last_buf() - return last buffer from the list of ready buffers
488 *
489 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
490 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500491struct vb2_v4l2_buffer *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx);
Hans Verkuilee1228c2018-06-26 09:26:52 -0400492
493/**
494 * v4l2_m2m_last_src_buf() - return last destination buffer from the list of
495 * ready buffers
496 *
497 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
498 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500499static inline struct vb2_v4l2_buffer *
500v4l2_m2m_last_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
Hans Verkuilee1228c2018-06-26 09:26:52 -0400501{
502 return v4l2_m2m_last_buf(&m2m_ctx->out_q_ctx);
503}
504
505/**
506 * v4l2_m2m_last_dst_buf() - return last destination buffer from the list of
507 * ready buffers
508 *
509 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
510 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500511static inline struct vb2_v4l2_buffer *
512v4l2_m2m_last_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
Hans Verkuilee1228c2018-06-26 09:26:52 -0400513{
514 return v4l2_m2m_last_buf(&m2m_ctx->cap_q_ctx);
515}
516
517/**
Stanimir Varbanovd4987562017-06-15 13:31:42 -0300518 * v4l2_m2m_for_each_dst_buf() - iterate over a list of destination ready
519 * buffers
520 *
521 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
522 * @b: current buffer of type struct v4l2_m2m_buffer
523 */
524#define v4l2_m2m_for_each_dst_buf(m2m_ctx, b) \
525 list_for_each_entry(b, &m2m_ctx->cap_q_ctx.rdy_queue, list)
526
527/**
528 * v4l2_m2m_for_each_src_buf() - iterate over a list of source ready buffers
529 *
530 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
531 * @b: current buffer of type struct v4l2_m2m_buffer
532 */
533#define v4l2_m2m_for_each_src_buf(m2m_ctx, b) \
534 list_for_each_entry(b, &m2m_ctx->out_q_ctx.rdy_queue, list)
535
536/**
537 * v4l2_m2m_for_each_dst_buf_safe() - iterate over a list of destination ready
538 * buffers safely
539 *
540 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
541 * @b: current buffer of type struct v4l2_m2m_buffer
542 * @n: used as temporary storage
543 */
544#define v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, b, n) \
545 list_for_each_entry_safe(b, n, &m2m_ctx->cap_q_ctx.rdy_queue, list)
546
547/**
548 * v4l2_m2m_for_each_src_buf_safe() - iterate over a list of source ready
549 * buffers safely
550 *
551 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
552 * @b: current buffer of type struct v4l2_m2m_buffer
553 * @n: used as temporary storage
554 */
555#define v4l2_m2m_for_each_src_buf_safe(m2m_ctx, b, n) \
556 list_for_each_entry_safe(b, n, &m2m_ctx->out_q_ctx.rdy_queue, list)
557
558/**
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300559 * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300560 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300561 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300562 */
563static inline
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300564struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300565{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300566 return &m2m_ctx->out_q_ctx.q;
Pawel Osciak7f986392010-04-23 05:38:37 -0300567}
568
569/**
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300570 * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300571 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300572 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300573 */
574static inline
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300575struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300576{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300577 return &m2m_ctx->cap_q_ctx.q;
Pawel Osciak7f986392010-04-23 05:38:37 -0300578}
579
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300580/**
581 * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
582 * return it
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300583 *
584 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
Mauro Carvalho Chehab47816462016-09-08 10:16:27 -0300585 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500586struct vb2_v4l2_buffer *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx);
Pawel Osciak7f986392010-04-23 05:38:37 -0300587
588/**
589 * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready
590 * buffers and return it
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300591 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300592 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300593 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500594static inline struct vb2_v4l2_buffer *
595v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300596{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300597 return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx);
Pawel Osciak7f986392010-04-23 05:38:37 -0300598}
599
600/**
601 * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of
602 * ready buffers and return it
Mauro Carvalho Chehab62c0d012015-08-22 05:34:40 -0300603 *
Mauro Carvalho Chehabdcbd8732016-09-08 10:39:58 -0300604 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
Pawel Osciak7f986392010-04-23 05:38:37 -0300605 */
Ezequiel Garcia8dd22b22019-02-08 11:17:48 -0500606static inline struct vb2_v4l2_buffer *
607v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300608{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300609 return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx);
Pawel Osciak7f986392010-04-23 05:38:37 -0300610}
611
Stanimir Varbanovd4987562017-06-15 13:31:42 -0300612/**
613 * v4l2_m2m_buf_remove_by_buf() - take off exact buffer from the list of ready
614 * buffers
615 *
616 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
617 * @vbuf: the buffer to be removed
618 */
619void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx,
620 struct vb2_v4l2_buffer *vbuf);
621
622/**
623 * v4l2_m2m_src_buf_remove_by_buf() - take off exact source buffer from the list
624 * of ready buffers
625 *
626 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
627 * @vbuf: the buffer to be removed
628 */
629static inline void v4l2_m2m_src_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx,
630 struct vb2_v4l2_buffer *vbuf)
631{
632 v4l2_m2m_buf_remove_by_buf(&m2m_ctx->out_q_ctx, vbuf);
633}
634
635/**
636 * v4l2_m2m_dst_buf_remove_by_buf() - take off exact destination buffer from the
637 * list of ready buffers
638 *
639 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
640 * @vbuf: the buffer to be removed
641 */
642static inline void v4l2_m2m_dst_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx,
643 struct vb2_v4l2_buffer *vbuf)
644{
645 v4l2_m2m_buf_remove_by_buf(&m2m_ctx->cap_q_ctx, vbuf);
646}
647
648struct vb2_v4l2_buffer *
649v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx);
650
651static inline struct vb2_v4l2_buffer *
652v4l2_m2m_src_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx)
653{
654 return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->out_q_ctx, idx);
655}
656
657static inline struct vb2_v4l2_buffer *
658v4l2_m2m_dst_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx)
659{
660 return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->cap_q_ctx, idx);
661}
662
Hans Verkuile2d8ffe22018-11-13 03:52:18 -0500663/**
Ezequiel Garciaa4d3d612019-02-05 16:20:33 -0500664 * v4l2_m2m_buf_copy_metadata() - copy buffer metadata from
665 * the output buffer to the capture buffer
Hans Verkuile2d8ffe22018-11-13 03:52:18 -0500666 *
Ezequiel Garciaa4d3d612019-02-05 16:20:33 -0500667 * @out_vb: the output buffer that is the source of the metadata.
668 * @cap_vb: the capture buffer that will receive the metadata.
Hans Verkuile2d8ffe22018-11-13 03:52:18 -0500669 * @copy_frame_flags: copy the KEY/B/PFRAME flags as well.
670 *
671 * This helper function copies the timestamp, timecode (if the TIMECODE
672 * buffer flag was set), field and the TIMECODE, KEYFRAME, BFRAME, PFRAME
673 * and TSTAMP_SRC_MASK flags from @out_vb to @cap_vb.
674 *
675 * If @copy_frame_flags is false, then the KEYFRAME, BFRAME and PFRAME
676 * flags are not copied. This is typically needed for encoders that
677 * set this bits explicitly.
678 */
Ezequiel Garciaa4d3d612019-02-05 16:20:33 -0500679void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb,
680 struct vb2_v4l2_buffer *cap_vb,
681 bool copy_frame_flags);
Hans Verkuile2d8ffe22018-11-13 03:52:18 -0500682
Hans Verkuil803a7ab2018-05-21 04:54:53 -0400683/* v4l2 request helper */
684
Ezequiel Garciaef86eaf2018-10-18 14:54:29 -0400685void v4l2_m2m_request_queue(struct media_request *req);
Hans Verkuil803a7ab2018-05-21 04:54:53 -0400686
Sylwester Nawrocki8e6e8f92013-09-14 18:39:04 -0300687/* v4l2 ioctl helpers */
688
689int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
690 struct v4l2_requestbuffers *rb);
691int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh,
692 struct v4l2_create_buffers *create);
693int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh,
694 struct v4l2_buffer *buf);
695int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,
696 struct v4l2_exportbuffer *eb);
697int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh,
698 struct v4l2_buffer *buf);
699int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh,
700 struct v4l2_buffer *buf);
Hans Verkuile68cf472015-06-05 11:28:50 -0300701int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh,
702 struct v4l2_buffer *buf);
Sylwester Nawrocki8e6e8f92013-09-14 18:39:04 -0300703int v4l2_m2m_ioctl_streamon(struct file *file, void *fh,
704 enum v4l2_buf_type type);
705int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh,
706 enum v4l2_buf_type type);
Hans Verkuilef732d52019-05-29 02:45:59 -0400707int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *fh,
708 struct v4l2_encoder_cmd *ec);
709int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *fh,
710 struct v4l2_decoder_cmd *dc);
Jernej Skrabecbef41d92019-10-11 06:32:43 -0300711int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *fh,
712 struct v4l2_decoder_cmd *dc);
713int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv,
714 struct v4l2_decoder_cmd *dc);
Sylwester Nawrocki8e6e8f92013-09-14 18:39:04 -0300715int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma);
Al Viroc23e0cb2017-07-03 03:02:56 -0400716__poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait);
Sylwester Nawrocki8e6e8f92013-09-14 18:39:04 -0300717
Pawel Osciak7f986392010-04-23 05:38:37 -0300718#endif /* _MEDIA_V4L2_MEM2MEM_H */
719