]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/media/v4l2-mem2mem.h
[media] v4l2-mem2mem.h: document function arguments
[mirror_ubuntu-bionic-kernel.git] / include / media / v4l2-mem2mem.h
CommitLineData
7f98639d
PO
1/*
2 * Memory-to-memory device framework for Video for Linux 2.
3 *
4 * Helper functions for devices that use memory buffers for both source
5 * and destination.
6 *
7 * Copyright (c) 2009 Samsung Electronics Co., Ltd.
95072084 8 * Pawel Osciak, <pawel@osciak.com>
7f98639d
PO
9 * Marek Szyprowski, <m.szyprowski@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version
15 */
16
17#ifndef _MEDIA_V4L2_MEM2MEM_H
18#define _MEDIA_V4L2_MEM2MEM_H
19
c139990e 20#include <media/videobuf2-v4l2.h>
7f98639d
PO
21
22/**
23 * struct v4l2_m2m_ops - mem-to-mem device driver callbacks
24 * @device_run: required. Begin the actual job (transaction) inside this
25 * callback.
26 * The job does NOT have to end before this callback returns
27 * (and it will be the usual case). When the job finishes,
28 * v4l2_m2m_job_finish() has to be called.
29 * @job_ready: optional. Should return 0 if the driver does not have a job
30 * fully prepared to run yet (i.e. it will not be able to finish a
31 * transaction without sleeping). If not provided, it will be
32 * assumed that one source and one destination buffer are all
33 * that is required for the driver to perform one full transaction.
34 * This method may not sleep.
35 * @job_abort: required. Informs the driver that it has to abort the currently
36 * running transaction as soon as possible (i.e. as soon as it can
37 * stop the device safely; e.g. in the next interrupt handler),
38 * even if the transaction would not have been finished by then.
39 * After the driver performs the necessary steps, it has to call
40 * v4l2_m2m_job_finish() (as if the transaction ended normally).
41 * This function does not have to (and will usually not) wait
42 * until the device enters a state when it can be stopped.
62c0d016
MCC
43 * @lock: optional. Define a driver's own lock callback, instead of using
44 * m2m_ctx->q_lock.
45 * @unlock: optional. Define a driver's own unlock callback, instead of
46 * using m2m_ctx->q_lock.
7f98639d
PO
47 */
48struct v4l2_m2m_ops {
49 void (*device_run)(void *priv);
50 int (*job_ready)(void *priv);
51 void (*job_abort)(void *priv);
908a0d7c
MS
52 void (*lock)(void *priv);
53 void (*unlock)(void *priv);
7f98639d
PO
54};
55
56struct v4l2_m2m_dev;
57
58struct v4l2_m2m_queue_ctx {
59/* private: internal use only */
908a0d7c 60 struct vb2_queue q;
7f98639d
PO
61
62 /* Queue for buffers ready to be processed as soon as this
63 * instance receives access to the device */
64 struct list_head rdy_queue;
908a0d7c 65 spinlock_t rdy_spinlock;
7f98639d 66 u8 num_rdy;
33bdd5a8 67 bool buffered;
7f98639d
PO
68};
69
70struct v4l2_m2m_ctx {
8e6e8f93
SN
71 /* optional cap/out vb2 queues lock */
72 struct mutex *q_lock;
73
7f98639d
PO
74/* private: internal use only */
75 struct v4l2_m2m_dev *m2m_dev;
76
77 /* Capture (output to memory) queue context */
78 struct v4l2_m2m_queue_ctx cap_q_ctx;
79
80 /* Output (input from memory) queue context */
81 struct v4l2_m2m_queue_ctx out_q_ctx;
82
83 /* For device job queue */
84 struct list_head queue;
85 unsigned long job_flags;
908a0d7c 86 wait_queue_head_t finished;
7f98639d
PO
87
88 /* Instance private data */
89 void *priv;
90};
91
908a0d7c 92struct v4l2_m2m_buffer {
2d700715 93 struct vb2_v4l2_buffer vb;
908a0d7c
MS
94 struct list_head list;
95};
96
4781646c
MCC
97/**
98 * v4l2_m2m_get_curr_priv() - return driver private data for the currently
99 * running instance or NULL if no instance is running
dcbd8735
MCC
100 *
101 * @m2m_dev: pointer to struct &v4l2_m2m_dev
4781646c 102 */
7f98639d
PO
103void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev);
104
4781646c
MCC
105/**
106 * v4l2_m2m_get_vq() - return vb2_queue for the given type
dcbd8735
MCC
107 *
108 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
109 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
4781646c 110 */
908a0d7c 111struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
7f98639d
PO
112 enum v4l2_buf_type type);
113
4781646c
MCC
114/**
115 * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
116 * the pending job queue and add it if so.
dcbd8735
MCC
117 *
118 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
4781646c
MCC
119 *
120 * There are three basic requirements an instance has to meet to be able to run:
121 * 1) at least one source buffer has to be queued,
122 * 2) at least one destination buffer has to be queued,
123 * 3) streaming has to be on.
124 *
125 * If a queue is buffered (for example a decoder hardware ringbuffer that has
126 * to be drained before doing streamoff), allow scheduling without v4l2 buffers
127 * on that queue.
128 *
129 * There may also be additional, custom requirements. In such case the driver
130 * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
131 * return 1 if the instance is ready.
132 * An example of the above could be an instance that requires more than one
133 * src/dst buffer per transaction.
134 */
1190a419
MO
135void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx);
136
4781646c
MCC
137/**
138 * v4l2_m2m_job_finish() - inform the framework that a job has been finished
139 * and have it clean up
140 *
dcbd8735
MCC
141 * @m2m_dev: pointer to struct &v4l2_m2m_dev
142 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
143 *
4781646c
MCC
144 * Called by a driver to yield back the device after it has finished with it.
145 * Should be called as soon as possible after reaching a state which allows
146 * other instances to take control of the device.
147 *
148 * This function has to be called only after device_run() callback has been
149 * called on the driver. To prevent recursion, it should not be called directly
150 * from the device_run() callback though.
151 */
7f98639d
PO
152void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
153 struct v4l2_m2m_ctx *m2m_ctx);
154
908a0d7c 155static inline void
2d700715 156v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state)
908a0d7c 157{
2d700715 158 vb2_buffer_done(&buf->vb2_buf, state);
908a0d7c
MS
159}
160
4781646c
MCC
161/**
162 * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
dcbd8735
MCC
163 *
164 * @file: pointer to struct &file
165 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
166 * @reqbufs: pointer to struct &v4l2_requestbuffers
4781646c 167 */
7f98639d
PO
168int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
169 struct v4l2_requestbuffers *reqbufs);
170
4781646c
MCC
171/**
172 * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
173 *
dcbd8735
MCC
174 * @file: pointer to struct &file
175 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
176 * @buf: pointer to struct &v4l2_buffer
177 *
4781646c
MCC
178 * See v4l2_m2m_mmap() documentation for details.
179 */
7f98639d
PO
180int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
181 struct v4l2_buffer *buf);
182
4781646c
MCC
183/**
184 * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
185 * the type
dcbd8735
MCC
186 *
187 * @file: pointer to struct &file
188 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
189 * @buf: pointer to struct &v4l2_buffer
4781646c 190 */
7f98639d
PO
191int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
192 struct v4l2_buffer *buf);
4781646c
MCC
193
194/**
195 * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
196 * the type
dcbd8735
MCC
197 *
198 * @file: pointer to struct &file
199 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
200 * @buf: pointer to struct &v4l2_buffer
4781646c 201 */
7f98639d
PO
202int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
203 struct v4l2_buffer *buf);
4781646c
MCC
204
205/**
206 * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on
207 * the type
dcbd8735
MCC
208 *
209 * @file: pointer to struct &file
210 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
211 * @buf: pointer to struct &v4l2_buffer
4781646c 212 */
e68cf471
HV
213int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
214 struct v4l2_buffer *buf);
4781646c
MCC
215
216/**
217 * v4l2_m2m_create_bufs() - create a source or destination buffer, depending
218 * on the type
dcbd8735
MCC
219 *
220 * @file: pointer to struct &file
221 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
222 * @create: pointer to struct &v4l2_create_buffers
4781646c 223 */
8b94ca61
PZ
224int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
225 struct v4l2_create_buffers *create);
7f98639d 226
4781646c
MCC
227/**
228 * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
229 * the type
dcbd8735
MCC
230 *
231 * @file: pointer to struct &file
232 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
233 * @eb: pointer to struct &v4l2_exportbuffer
4781646c 234 */
83ae7c5a
TS
235int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
236 struct v4l2_exportbuffer *eb);
237
4781646c
MCC
238/**
239 * v4l2_m2m_streamon() - turn on streaming for a video queue
dcbd8735
MCC
240 *
241 * @file: pointer to struct &file
242 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
243 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
4781646c 244 */
7f98639d
PO
245int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
246 enum v4l2_buf_type type);
4781646c
MCC
247
248/**
249 * v4l2_m2m_streamoff() - turn off streaming for a video queue
dcbd8735
MCC
250 *
251 * @file: pointer to struct &file
252 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
253 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type
4781646c 254 */
7f98639d
PO
255int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
256 enum v4l2_buf_type type);
257
4781646c
MCC
258/**
259 * v4l2_m2m_poll() - poll replacement, for destination buffers only
260 *
dcbd8735
MCC
261 * @file: pointer to struct &file
262 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
263 * @wait: pointer to struct &poll_table_struct
264 *
4781646c
MCC
265 * Call from the driver's poll() function. Will poll both queues. If a buffer
266 * is available to dequeue (with dqbuf) from the source queue, this will
267 * indicate that a non-blocking write can be performed, while read will be
268 * returned in case of the destination queue.
269 */
7f98639d
PO
270unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
271 struct poll_table_struct *wait);
272
4781646c
MCC
273/**
274 * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
275 *
dcbd8735
MCC
276 * @file: pointer to struct &file
277 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
278 * @vma: pointer to struct &vm_area_struct
279 *
4781646c
MCC
280 * Call from driver's mmap() function. Will handle mmap() for both queues
281 * seamlessly for videobuffer, which will receive normal per-queue offsets and
282 * proper videobuf queue pointers. The differentiation is made outside videobuf
283 * by adding a predefined offset to buffers from one of the queues and
284 * subtracting it before passing it back to videobuf. Only drivers (and
285 * thus applications) receive modified offsets.
286 */
7f98639d
PO
287int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
288 struct vm_area_struct *vma);
289
4781646c
MCC
290/**
291 * v4l2_m2m_init() - initialize per-driver m2m data
292 *
dcbd8735
MCC
293 * @m2m_ops: pointer to struct v4l2_m2m_ops
294 *
4781646c
MCC
295 * Usually called from driver's probe() function.
296 */
b1252eb8 297struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops);
4781646c
MCC
298
299/**
300 * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
301 *
dcbd8735
MCC
302 * @m2m_dev: pointer to struct &v4l2_m2m_dev
303 *
4781646c
MCC
304 * Usually called from driver's remove() function.
305 */
7f98639d
PO
306void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev);
307
4781646c
MCC
308/**
309 * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
dcbd8735
MCC
310 *
311 * @m2m_dev: a previously initialized m2m_dev struct
312 * @drv_priv: driver's instance private data
313 * @queue_init: a callback for queue type-specific initialization function
314 * to be used for initializing videobuf_queues
4781646c
MCC
315 *
316 * Usually called from driver's open() function.
317 */
908a0d7c
MS
318struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
319 void *drv_priv,
320 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq));
321
33bdd5a8
PZ
322static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx,
323 bool buffered)
324{
325 m2m_ctx->out_q_ctx.buffered = buffered;
326}
327
328static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx,
329 bool buffered)
330{
331 m2m_ctx->cap_q_ctx.buffered = buffered;
332}
333
4781646c
MCC
334/**
335 * v4l2_m2m_ctx_release() - release m2m context
336 *
dcbd8735
MCC
337 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
338 *
4781646c
MCC
339 * Usually called from driver's release() function.
340 */
7f98639d
PO
341void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx);
342
4781646c
MCC
343/**
344 * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
345 *
dcbd8735
MCC
346 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
347 * @vbuf: pointer to struct &vb2_v4l2_buffer
348 *
4781646c
MCC
349 * Call from buf_queue(), videobuf_queue_ops callback.
350 */
2d700715
JS
351void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
352 struct vb2_v4l2_buffer *vbuf);
7f98639d
PO
353
354/**
355 * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for
356 * use
62c0d016 357 *
dcbd8735 358 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
359 */
360static inline
361unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
362{
961ae449 363 return m2m_ctx->out_q_ctx.num_rdy;
7f98639d
PO
364}
365
366/**
367 * v4l2_m2m_num_src_bufs_ready() - return the number of destination buffers
368 * ready for use
62c0d016 369 *
dcbd8735 370 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
371 */
372static inline
373unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx)
374{
961ae449 375 return m2m_ctx->cap_q_ctx.num_rdy;
7f98639d
PO
376}
377
4781646c
MCC
378/**
379 * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
dcbd8735
MCC
380 *
381 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
4781646c 382 */
908a0d7c 383void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx);
7f98639d
PO
384
385/**
386 * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready
387 * buffers
62c0d016 388 *
dcbd8735 389 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
390 */
391static inline void *v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx)
392{
908a0d7c 393 return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx);
7f98639d
PO
394}
395
396/**
397 * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of
398 * ready buffers
62c0d016 399 *
dcbd8735 400 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
401 */
402static inline void *v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx)
403{
908a0d7c 404 return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx);
7f98639d
PO
405}
406
407/**
908a0d7c 408 * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers
62c0d016 409 *
dcbd8735 410 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
411 */
412static inline
908a0d7c 413struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx)
7f98639d 414{
908a0d7c 415 return &m2m_ctx->out_q_ctx.q;
7f98639d
PO
416}
417
418/**
908a0d7c 419 * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers
62c0d016 420 *
dcbd8735 421 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
422 */
423static inline
908a0d7c 424struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx)
7f98639d 425{
908a0d7c 426 return &m2m_ctx->cap_q_ctx.q;
7f98639d
PO
427}
428
4781646c
MCC
429/**
430 * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
431 * return it
dcbd8735
MCC
432 *
433 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx
4781646c 434 */
908a0d7c 435void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx);
7f98639d
PO
436
437/**
438 * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready
439 * buffers and return it
62c0d016 440 *
dcbd8735 441 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
442 */
443static inline void *v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
444{
908a0d7c 445 return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx);
7f98639d
PO
446}
447
448/**
449 * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of
450 * ready buffers and return it
62c0d016 451 *
dcbd8735 452 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
7f98639d
PO
453 */
454static inline void *v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx)
455{
908a0d7c 456 return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx);
7f98639d
PO
457}
458
8e6e8f93
SN
459/* v4l2 ioctl helpers */
460
461int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
462 struct v4l2_requestbuffers *rb);
463int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh,
464 struct v4l2_create_buffers *create);
465int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh,
466 struct v4l2_buffer *buf);
467int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,
468 struct v4l2_exportbuffer *eb);
469int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh,
470 struct v4l2_buffer *buf);
471int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh,
472 struct v4l2_buffer *buf);
e68cf471
HV
473int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh,
474 struct v4l2_buffer *buf);
8e6e8f93
SN
475int v4l2_m2m_ioctl_streamon(struct file *file, void *fh,
476 enum v4l2_buf_type type);
477int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh,
478 enum v4l2_buf_type type);
479int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma);
480unsigned int v4l2_m2m_fop_poll(struct file *file, poll_table *wait);
481
7f98639d
PO
482#endif /* _MEDIA_V4L2_MEM2MEM_H */
483