[RAMEN9610-20350][9610] drivers: muic: support bad TA
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / include / media / videobuf2-core.h
1 /*
2 * videobuf2-core.h - Video Buffer 2 Core Framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
6 * Author: Pawel Osciak <pawel@osciak.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 */
12 #ifndef _MEDIA_VIDEOBUF2_CORE_H
13 #define _MEDIA_VIDEOBUF2_CORE_H
14
15 #include <linux/mm_types.h>
16 #include <linux/mutex.h>
17 #include <linux/poll.h>
18 #include <linux/dma-buf.h>
19 #include <linux/dma-fence.h>
20
21 #define VB2_MAX_FRAME (32)
22 #define VB2_MAX_PLANES (8)
23
24 /**
25 * enum vb2_memory - type of memory model used to make the buffers visible
26 * on userspace.
27 *
28 * @VB2_MEMORY_UNKNOWN: Buffer status is unknown or it is not used yet on
29 * userspace.
30 * @VB2_MEMORY_MMAP: The buffers are allocated by the Kernel and it is
31 * memory mapped via mmap() ioctl. This model is
32 * also used when the user is using the buffers via
33 * read() or write() system calls.
34 * @VB2_MEMORY_USERPTR: The buffers was allocated in userspace and it is
35 * memory mapped via mmap() ioctl.
36 * @VB2_MEMORY_DMABUF: The buffers are passed to userspace via DMA buffer.
37 */
38 enum vb2_memory {
39 VB2_MEMORY_UNKNOWN = 0,
40 VB2_MEMORY_MMAP = 1,
41 VB2_MEMORY_USERPTR = 2,
42 VB2_MEMORY_DMABUF = 4,
43 };
44
45 struct vb2_fileio_data;
46 struct vb2_threadio_data;
47
48 /**
49 * struct vb2_mem_ops - memory handling/memory allocator operations
50 * @alloc: allocate video memory and, optionally, allocator private data,
51 * return ERR_PTR() on failure or a pointer to allocator private,
52 * per-buffer data on success; the returned private structure
53 * will then be passed as @buf_priv argument to other ops in this
54 * structure. Additional gfp_flags to use when allocating the
55 * are also passed to this operation. These flags are from the
56 * gfp_flags field of vb2_queue.
57 * @put: inform the allocator that the buffer will no longer be used;
58 * usually will result in the allocator freeing the buffer (if
59 * no other users of this buffer are present); the @buf_priv
60 * argument is the allocator private per-buffer structure
61 * previously returned from the alloc callback.
62 * @get_dmabuf: acquire userspace memory for a hardware operation; used for
63 * DMABUF memory types.
64 * @get_userptr: acquire userspace memory for a hardware operation; used for
65 * USERPTR memory types; vaddr is the address passed to the
66 * videobuf layer when queuing a video buffer of USERPTR type;
67 * should return an allocator private per-buffer structure
68 * associated with the buffer on success, ERR_PTR() on failure;
69 * the returned private structure will then be passed as @buf_priv
70 * argument to other ops in this structure.
71 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
72 * be used.
73 * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation;
74 * used for DMABUF memory types; dev is the alloc device
75 * dbuf is the shared dma_buf; returns ERR_PTR() on failure;
76 * allocator private per-buffer structure on success;
77 * this needs to be used for further accesses to the buffer.
78 * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
79 * buffer is no longer used; the @buf_priv argument is the
80 * allocator private per-buffer structure previously returned
81 * from the attach_dmabuf callback.
82 * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
83 * of dmabuf is informed that this driver is going to use the
84 * dmabuf.
85 * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
86 * that this driver is done using the dmabuf for now.
87 * @prepare: called every time the buffer is passed from userspace to the
88 * driver, useful for cache synchronisation, optional.
89 * @finish: called every time the buffer is passed back from the driver
90 * to the userspace, also optional.
91 * @vaddr: return a kernel virtual address to a given memory buffer
92 * associated with the passed private structure or NULL if no
93 * such mapping exists.
94 * @cookie: return allocator specific cookie for a given memory buffer
95 * associated with the passed private structure or NULL if not
96 * available.
97 * @num_users: return the current number of users of a memory buffer;
98 * return 1 if the videobuf layer (or actually the driver using
99 * it) is the only user.
100 * @mmap: setup a userspace mapping for a given memory buffer under
101 * the provided virtual memory region.
102 *
103 * Those operations are used by the videobuf2 core to implement the memory
104 * handling/memory allocators for each type of supported streaming I/O method.
105 *
106 * .. note::
107 * #) Required ops for USERPTR types: get_userptr, put_userptr.
108 *
109 * #) Required ops for MMAP types: alloc, put, num_users, mmap.
110 *
111 * #) Required ops for read/write access types: alloc, put, num_users, vaddr.
112 *
113 * #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf,
114 * map_dmabuf, unmap_dmabuf.
115 */
116 struct vb2_mem_ops {
117 void *(*alloc)(struct device *dev, unsigned long attrs,
118 unsigned long size,
119 enum dma_data_direction dma_dir,
120 gfp_t gfp_flags, int memflags);
121 void (*put)(void *buf_priv);
122 struct dma_buf *(*get_dmabuf)(void *buf_priv, unsigned long flags);
123
124 void *(*get_userptr)(struct device *dev, unsigned long vaddr,
125 unsigned long size,
126 enum dma_data_direction dma_dir,
127 int memflags);
128 void (*put_userptr)(void *buf_priv);
129
130 void (*prepare)(void *buf_priv, size_t size, int memflags);
131 void (*finish)(void *buf_priv, size_t size, int memflags);
132
133 void *(*attach_dmabuf)(struct device *dev,
134 struct dma_buf *dbuf,
135 unsigned long size,
136 enum dma_data_direction dma_dir);
137 void (*detach_dmabuf)(void *buf_priv);
138 int (*map_dmabuf)(void *buf_priv, size_t size,
139 int memflags);
140 void (*unmap_dmabuf)(void *buf_priv, size_t size);
141
142 void *(*vaddr)(void *buf_priv);
143 void *(*cookie)(void *buf_priv);
144
145 unsigned int (*num_users)(void *buf_priv);
146
147 int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
148 };
149
150 /**
151 * struct vb2_plane - plane information
152 * @mem_priv: private data with this plane
153 * @dbuf: dma_buf - shared buffer object
154 * @dbuf_mapped: flag to show whether dbuf is mapped or not
155 * @bytesused: number of bytes occupied by data in the plane (payload)
156 * @length: size of this plane (NOT the payload) in bytes
157 * @min_length: minimum required size of this plane (NOT the payload) in bytes.
158 * @length is always greater or equal to @min_length.
159 * @offset: when memory in the associated struct vb2_buffer is
160 * VB2_MEMORY_MMAP, equals the offset from the start of
161 * the device memory for this plane (or is a "cookie" that
162 * should be passed to mmap() called on the video node)
163 * @userptr: when memory is VB2_MEMORY_USERPTR, a userspace pointer
164 * pointing to this plane
165 * @fd: when memory is VB2_MEMORY_DMABUF, a userspace file
166 * descriptor associated with this plane
167 * @m: Union with memtype-specific data (@offset, @userptr or
168 * @fd).
169 * @data_offset: offset in the plane to the start of data; usually 0,
170 * unless there is a header in front of the data
171 * Should contain enough information to be able to cover all the fields
172 * of struct v4l2_plane at videodev2.h
173 */
174 struct vb2_plane {
175 void *mem_priv;
176 struct dma_buf *dbuf;
177 unsigned int dbuf_mapped;
178 unsigned int bytesused;
179 unsigned int length;
180 unsigned int min_length;
181 union {
182 unsigned int offset;
183 unsigned long userptr;
184 int fd;
185 } m;
186 unsigned int data_offset;
187 };
188
189 /**
190 * enum vb2_io_modes - queue access methods
191 * @VB2_MMAP: driver supports MMAP with streaming API
192 * @VB2_USERPTR: driver supports USERPTR with streaming API
193 * @VB2_READ: driver supports read() style access
194 * @VB2_WRITE: driver supports write() style access
195 * @VB2_DMABUF: driver supports DMABUF with streaming API
196 */
197 enum vb2_io_modes {
198 VB2_MMAP = (1 << 0),
199 VB2_USERPTR = (1 << 1),
200 VB2_READ = (1 << 2),
201 VB2_WRITE = (1 << 3),
202 VB2_DMABUF = (1 << 4),
203 };
204
205 /**
206 * enum vb2_buffer_state - current video buffer state
207 * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
208 * @VB2_BUF_STATE_PREPARING: buffer is being prepared in videobuf
209 * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
210 * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
211 * @VB2_BUF_STATE_REQUEUEING: re-queue a buffer to the driver
212 * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
213 * in a hardware operation
214 * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
215 * not yet dequeued to userspace
216 * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
217 * has ended with an error, which will be reported
218 * to the userspace when it is dequeued
219 */
220 enum vb2_buffer_state {
221 VB2_BUF_STATE_DEQUEUED,
222 VB2_BUF_STATE_PREPARING,
223 VB2_BUF_STATE_PREPARED,
224 VB2_BUF_STATE_QUEUED,
225 VB2_BUF_STATE_REQUEUEING,
226 VB2_BUF_STATE_ACTIVE,
227 VB2_BUF_STATE_DONE,
228 VB2_BUF_STATE_ERROR,
229 };
230
231 struct vb2_queue;
232
233 /**
234 * struct vb2_buffer - represents a video buffer
235 * @vb2_queue: the queue to which this driver belongs
236 * @index: id number of the buffer
237 * @type: buffer type
238 * @memory: the method, in which the actual data is passed
239 * @num_planes: number of planes in the buffer
240 * on an internal driver queue
241 * @planes: private per-plane information; do not change
242 * @timestamp: frame timestamp in ns
243 */
244 struct vb2_buffer {
245 struct vb2_queue *vb2_queue;
246 unsigned int index;
247 unsigned int type;
248 unsigned int memory;
249 unsigned int num_planes;
250 struct vb2_plane planes[VB2_MAX_PLANES];
251 u64 timestamp;
252
253 /* private: internal use only
254 *
255 * state: current buffer state; do not change
256 * queued_entry: entry on the queued buffers list, which holds
257 * all buffers queued from userspace
258 * done_entry: entry on the list that stores all buffers ready
259 * to be dequeued to userspace
260 * in_fence: fence received from vb2 client to wait on before
261 * using the buffer (queueing to the driver)
262 * fence_cb: fence callback information
263 * fence_cb_lock: protect callback signal/remove
264 * out_fence_fd: the out_fence_fd to be shared with userspace.
265 * out_fence: the out-fence associated with the buffer once
266 * it is queued to the driver.
267 * sync_file: the sync file to wrap the out fence
268 */
269 enum vb2_buffer_state state;
270
271 struct list_head queued_entry;
272 struct list_head done_entry;
273
274 struct dma_fence *in_fence;
275 struct dma_fence_cb fence_cb;
276 struct work_struct qbuf_work;
277 spinlock_t fence_cb_lock;
278
279 struct timer_list fence_timer;
280
281 int out_fence_fd;
282 struct dma_fence *out_fence;
283 struct sync_file *sync_file;
284
285 #ifdef CONFIG_VIDEO_ADV_DEBUG
286 /*
287 * Counters for how often these buffer-related ops are
288 * called. Used to check for unbalanced ops.
289 */
290 u32 cnt_mem_alloc;
291 u32 cnt_mem_put;
292 u32 cnt_mem_get_dmabuf;
293 u32 cnt_mem_get_userptr;
294 u32 cnt_mem_put_userptr;
295 u32 cnt_mem_prepare;
296 u32 cnt_mem_finish;
297 u32 cnt_mem_attach_dmabuf;
298 u32 cnt_mem_detach_dmabuf;
299 u32 cnt_mem_map_dmabuf;
300 u32 cnt_mem_unmap_dmabuf;
301 u32 cnt_mem_vaddr;
302 u32 cnt_mem_cookie;
303 u32 cnt_mem_num_users;
304 u32 cnt_mem_mmap;
305
306 u32 cnt_buf_init;
307 u32 cnt_buf_prepare;
308 u32 cnt_buf_finish;
309 u32 cnt_buf_cleanup;
310 u32 cnt_buf_queue;
311
312 /* This counts the number of calls to vb2_buffer_done() */
313 u32 cnt_buf_done;
314 #endif
315 };
316
317 /**
318 * struct vb2_ops - driver-specific callbacks
319 *
320 * @queue_setup: called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
321 * handlers before memory allocation. It can be called
322 * twice: if the original number of requested buffers
323 * could not be allocated, then it will be called a
324 * second time with the actually allocated number of
325 * buffers to verify if that is OK.
326 * The driver should return the required number of buffers
327 * in \*num_buffers, the required number of planes per
328 * buffer in \*num_planes, the size of each plane should be
329 * set in the sizes\[\] array and optional per-plane
330 * allocator specific device in the alloc_devs\[\] array.
331 * When called from VIDIOC_REQBUFS(), \*num_planes == 0,
332 * the driver has to use the currently configured format to
333 * determine the plane sizes and \*num_buffers is the total
334 * number of buffers that are being allocated. When called
335 * from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it
336 * describes the requested number of planes and sizes\[\]
337 * contains the requested plane sizes. In this case
338 * \*num_buffers are being allocated additionally to
339 * q->num_buffers. If either \*num_planes or the requested
340 * sizes are invalid callback must return %-EINVAL.
341 * @wait_prepare: release any locks taken while calling vb2 functions;
342 * it is called before an ioctl needs to wait for a new
343 * buffer to arrive; required to avoid a deadlock in
344 * blocking access type.
345 * @wait_finish: reacquire all locks released in the previous callback;
346 * required to continue operation after sleeping while
347 * waiting for a new buffer to arrive.
348 * @buf_init: called once after allocating a buffer (in MMAP case)
349 * or after acquiring a new USERPTR buffer; drivers may
350 * perform additional buffer-related initialization;
351 * initialization failure (return != 0) will prevent
352 * queue setup from completing successfully; optional.
353 * @buf_prepare: called every time the buffer is queued from userspace
354 * and from the VIDIOC_PREPARE_BUF() ioctl; drivers may
355 * perform any initialization required before each
356 * hardware operation in this callback; drivers can
357 * access/modify the buffer here as it is still synced for
358 * the CPU; drivers that support VIDIOC_CREATE_BUFS() must
359 * also validate the buffer size; if an error is returned,
360 * the buffer will not be queued in driver; optional.
361 * @buf_finish: called before every dequeue of the buffer back to
362 * userspace; the buffer is synced for the CPU, so drivers
363 * can access/modify the buffer contents; drivers may
364 * perform any operations required before userspace
365 * accesses the buffer; optional. The buffer state can be
366 * one of the following: %DONE and %ERROR occur while
367 * streaming is in progress, and the %PREPARED state occurs
368 * when the queue has been canceled and all pending
369 * buffers are being returned to their default %DEQUEUED
370 * state. Typically you only have to do something if the
371 * state is %VB2_BUF_STATE_DONE, since in all other cases
372 * the buffer contents will be ignored anyway.
373 * @buf_cleanup: called once before the buffer is freed; drivers may
374 * perform any additional cleanup; optional.
375 * @start_streaming: called once to enter 'streaming' state; the driver may
376 * receive buffers with @buf_queue callback
377 * before @start_streaming is called; the driver gets the
378 * number of already queued buffers in count parameter;
379 * driver can return an error if hardware fails, in that
380 * case all buffers that have been already given by
381 * the @buf_queue callback are to be returned by the driver
382 * by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED.
383 * If you need a minimum number of buffers before you can
384 * start streaming, then set @min_buffers_needed in the
385 * vb2_queue structure. If that is non-zero then
386 * @start_streaming won't be called until at least that
387 * many buffers have been queued up by userspace.
388 * @stop_streaming: called when 'streaming' state must be disabled; driver
389 * should stop any DMA transactions or wait until they
390 * finish and give back all buffers it got from &buf_queue
391 * callback by calling vb2_buffer_done() with either
392 * %VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use
393 * vb2_wait_for_all_buffers() function
394 * @is_unordered: tell if the queue is unordered, i.e. buffers can be
395 * dequeued in a different order from how they were queued.
396 * The default is assumed to be ordered and this function
397 * only needs to be implemented for unordered queues.
398 * @buf_queue: passes buffer vb to the driver; driver may start
399 * hardware operation on this buffer; driver should give
400 * the buffer back by calling vb2_buffer_done() function;
401 * it is allways called after calling VIDIOC_STREAMON()
402 * ioctl; might be called before @start_streaming callback
403 * if user pre-queued buffers before calling
404 * VIDIOC_STREAMON().
405 * @mem_flags: called before events of buffer manipulations including
406 * buffer acquisition and mappin to study extra information
407 * vb2 mem implementations. The return values of
408 * @mem_flags are implementation specific.
409 */
410 struct vb2_ops {
411 int (*queue_setup)(struct vb2_queue *q,
412 unsigned int *num_buffers, unsigned int *num_planes,
413 unsigned int sizes[], struct device *alloc_devs[]);
414
415 void (*wait_prepare)(struct vb2_queue *q);
416 void (*wait_finish)(struct vb2_queue *q);
417
418 int (*buf_init)(struct vb2_buffer *vb);
419 int (*buf_prepare)(struct vb2_buffer *vb);
420 void (*buf_finish)(struct vb2_buffer *vb);
421 void (*buf_cleanup)(struct vb2_buffer *vb);
422
423 int (*start_streaming)(struct vb2_queue *q, unsigned int count);
424 void (*stop_streaming)(struct vb2_queue *q);
425 bool (*is_unordered)(struct vb2_queue *q);
426
427 void (*buf_queue)(struct vb2_buffer *vb);
428 int (*mem_flags)(struct vb2_buffer *vb);
429 };
430
431 /**
432 * struct vb2_buf_ops - driver-specific callbacks
433 *
434 * @verify_planes_array: Verify that a given user space structure contains
435 * enough planes for the buffer. This is called
436 * for each dequeued buffer.
437 * @fill_user_buffer: given a vb2_buffer fill in the userspace structure.
438 * For V4L2 this is a struct v4l2_buffer.
439 * @fill_vb2_buffer: given a userspace structure, fill in the vb2_buffer.
440 * If the userspace structure is invalid, then this op
441 * will return an error.
442 * @copy_timestamp: copy the timestamp from a userspace structure to
443 * the vb2_buffer struct.
444 */
445 struct vb2_buf_ops {
446 int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
447 void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
448 int (*fill_vb2_buffer)(struct vb2_buffer *vb, const void *pb,
449 struct vb2_plane *planes);
450 void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
451 };
452
453 /**
454 * struct vb2_queue - a videobuf queue
455 *
456 * @type: private buffer type whose content is defined by the vb2-core
457 * caller. For example, for V4L2, it should match
458 * the types defined on enum &v4l2_buf_type
459 * @io_modes: supported io methods (see vb2_io_modes enum)
460 * @dev: device to use for the default allocation context if the driver
461 * doesn't fill in the @alloc_devs array.
462 * @dma_attrs: DMA attributes to use for the DMA.
463 * @bidirectional: when this flag is set the DMA direction for the buffers of
464 * this queue will be overridden with DMA_BIDIRECTIONAL direction.
465 * This is useful in cases where the hardware (firmware) writes to
466 * a buffer which is mapped as read (DMA_TO_DEVICE), or reads from
467 * buffer which is mapped for write (DMA_FROM_DEVICE) in order
468 * to satisfy some internal hardware restrictions or adds a padding
469 * needed by the processing algorithm. In case the DMA mapping is
470 * not bidirectional but the hardware (firmware) trying to access
471 * the buffer (in the opposite direction) this could lead to an
472 * IOMMU protection faults.
473 * @fileio_read_once: report EOF after reading the first buffer
474 * @fileio_write_immediately: queue buffer after each write() call
475 * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver
476 * @quirk_poll_must_check_waiting_for_buffers: Return POLLERR at poll when QBUF
477 * has not been called. This is a vb1 idiom that has been adopted
478 * also by vb2.
479 * @lock: pointer to a mutex that protects the vb2_queue struct. The
480 * driver can set this to a mutex to let the v4l2 core serialize
481 * the queuing ioctls. If the driver wants to handle locking
482 * itself, then this should be set to NULL. This lock is not used
483 * by the videobuf2 core API.
484 * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
485 * that called reqbufs, create_buffers or started fileio.
486 * This field is not used by the videobuf2 core API, but it allows
487 * drivers to easily associate an owner filehandle with the queue.
488 * @ops: driver-specific callbacks
489 * @mem_ops: memory allocator specific callbacks
490 * @buf_ops: callbacks to deliver buffer information
491 * between user-space and kernel-space
492 * @drv_priv: driver private data
493 * @buf_struct_size: size of the driver-specific buffer structure;
494 * "0" indicates the driver doesn't want to use a custom buffer
495 * structure type. for example, sizeof(struct vb2_v4l2_buffer)
496 * will be used for v4l2.
497 * @timestamp_flags: Timestamp flags; V4L2_BUF_FLAG_TIMESTAMP_* and
498 * V4L2_BUF_FLAG_TSTAMP_SRC_*
499 * @gfp_flags: additional gfp flags used when allocating the buffers.
500 * Typically this is 0, but it may be e.g. GFP_DMA or __GFP_DMA32
501 * to force the buffer allocation to a specific memory zone.
502 * @min_buffers_needed: the minimum number of buffers needed before
503 * @start_streaming can be called. Used when a DMA engine
504 * cannot be started unless at least this number of buffers
505 * have been queued into the driver.
506 */
507 /*
508 * Private elements (won't appear at the uAPI book):
509 * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped
510 * @memory: current memory type used
511 * @dma_dir: DMA mapping direction.
512 * @bufs: videobuf buffer structures
513 * @num_buffers: number of allocated/used buffers
514 * @queued_list: list of buffers currently queued from userspace
515 * @queued_count: number of buffers queued and ready for streaming.
516 * @owned_by_drv_count: number of buffers owned by the driver
517 * @done_list: list of buffers ready to be dequeued to userspace
518 * @done_lock: lock to protect done_list list
519 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
520 * @alloc_devs: memory type/allocator-specific per-plane device
521 * @streaming: current streaming state
522 * @start_streaming_called: @start_streaming was called successfully and we
523 * started streaming.
524 * @error: a fatal error occurred on the queue
525 * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
526 * buffers. Only set for capture queues if qbuf has not yet been
527 * called since poll() needs to return POLLERR in that situation.
528 * @is_multiplanar: set if buffer type is multiplanar
529 * @is_output: set if buffer type is output
530 * @copy_timestamp: set if vb2-core should set timestamps
531 * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
532 * last decoded buffer was already dequeued. Set for capture queues
533 * when a buffer with the V4L2_BUF_FLAG_LAST is dequeued.
534 * @fileio: file io emulator internal data, used only if emulator is active
535 * @threadio: thread io internal data, used only if thread is active
536 */
537 struct vb2_queue {
538 unsigned int type;
539 unsigned int io_modes;
540 struct device *dev;
541 unsigned long dma_attrs;
542 unsigned bidirectional:1;
543 unsigned fileio_read_once:1;
544 unsigned fileio_write_immediately:1;
545 unsigned allow_zero_bytesused:1;
546 unsigned quirk_poll_must_check_waiting_for_buffers:1;
547
548 struct mutex *lock;
549 void *owner;
550
551 const struct vb2_ops *ops;
552 const struct vb2_mem_ops *mem_ops;
553 const struct vb2_buf_ops *buf_ops;
554
555 void *drv_priv;
556 unsigned int buf_struct_size;
557 u32 timestamp_flags;
558 gfp_t gfp_flags;
559 u32 min_buffers_needed;
560
561 /* private: internal use only */
562 struct mutex mmap_lock;
563 unsigned int memory;
564 enum dma_data_direction dma_dir;
565 struct vb2_buffer *bufs[VB2_MAX_FRAME];
566 unsigned int num_buffers;
567
568 struct list_head queued_list;
569 unsigned int queued_count;
570
571 atomic_t owned_by_drv_count;
572 struct list_head done_list;
573 spinlock_t done_lock;
574 wait_queue_head_t done_wq;
575
576 struct device *alloc_devs[VB2_MAX_PLANES];
577
578 unsigned int streaming:1;
579 unsigned int start_streaming_called:1;
580 unsigned int error:1;
581 unsigned int waiting_for_buffers:1;
582 unsigned int is_multiplanar:1;
583 unsigned int is_output:1;
584 unsigned int copy_timestamp:1;
585 unsigned int last_buffer_dequeued:1;
586 unsigned int queueing_started:1;
587
588 u64 out_fence_context;
589 spinlock_t out_fence_lock;
590
591 struct vb2_fileio_data *fileio;
592 struct vb2_threadio_data *threadio;
593
594 #ifdef CONFIG_VIDEO_ADV_DEBUG
595 /*
596 * Counters for how often these queue-related ops are
597 * called. Used to check for unbalanced ops.
598 */
599 u32 cnt_queue_setup;
600 u32 cnt_wait_prepare;
601 u32 cnt_wait_finish;
602 u32 cnt_start_streaming;
603 u32 cnt_stop_streaming;
604 u32 cnt_is_unordered;
605 u32 cnt_mem_flags;
606 #endif
607 };
608
609 /**
610 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
611 * @vb: vb2_buffer to which the plane in question belongs to
612 * @plane_no: plane number for which the address is to be returned
613 *
614 * This function returns a kernel virtual address of a given plane if
615 * such a mapping exist, NULL otherwise.
616 */
617 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
618
619 /**
620 * vb2_plane_cookie() - Return allocator specific cookie for the given plane
621 * @vb: vb2_buffer to which the plane in question belongs to
622 * @plane_no: plane number for which the cookie is to be returned
623 *
624 * This function returns an allocator specific cookie for a given plane if
625 * available, NULL otherwise. The allocator should provide some simple static
626 * inline function, which would convert this cookie to the allocator specific
627 * type that can be used directly by the driver to access the buffer. This can
628 * be for example physical address, pointer to scatter list or IOMMU mapping.
629 */
630 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
631
632 /**
633 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
634 * @vb: vb2_buffer returned from the driver
635 * @state: either %VB2_BUF_STATE_DONE if the operation finished
636 * successfully, %VB2_BUF_STATE_ERROR if the operation finished
637 * with an error or %VB2_BUF_STATE_QUEUED if the driver wants to
638 * requeue buffers. If start_streaming fails then it should return
639 * buffers with state %VB2_BUF_STATE_QUEUED to put them back into
640 * the queue.
641 *
642 * This function should be called by the driver after a hardware operation on
643 * a buffer is finished and the buffer may be returned to userspace. The driver
644 * cannot use this buffer anymore until it is queued back to it by videobuf
645 * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued
646 * to the driver by &vb2_ops->buf_queue can be passed to this function.
647 *
648 * While streaming a buffer can only be returned in state DONE or ERROR.
649 * The start_streaming op can also return them in case the DMA engine cannot
650 * be started for some reason. In that case the buffers should be returned with
651 * state QUEUED.
652 */
653 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
654
655 /**
656 * vb2_discard_done() - discard all buffers marked as DONE
657 * @q: videobuf2 queue
658 *
659 * This function is intended to be used with suspend/resume operations. It
660 * discards all 'done' buffers as they would be too old to be requested after
661 * resume.
662 *
663 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
664 * delayed works before calling this function to make sure no buffer will be
665 * touched by the driver and/or hardware.
666 */
667 void vb2_discard_done(struct vb2_queue *q);
668
669 /**
670 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
671 * @q: videobuf2 queue
672 *
673 * This function will wait until all buffers that have been given to the driver
674 * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). It
675 * doesn't call wait_prepare()/wait_finish() pair. It is intended to be called
676 * with all locks taken, for example from &vb2_ops->stop_streaming callback.
677 */
678 int vb2_wait_for_all_buffers(struct vb2_queue *q);
679
680 /**
681 * vb2_core_querybuf() - query video buffer information
682 * @q: videobuf queue
683 * @index: id number of the buffer
684 * @pb: buffer struct passed from userspace
685 *
686 * Should be called from vidioc_querybuf ioctl handler in driver.
687 * The passed buffer should have been verified.
688 * This function fills the relevant information for the userspace.
689 */
690 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
691
692 /**
693 * vb2_core_reqbufs() - Initiate streaming
694 * @q: videobuf2 queue
695 * @memory: memory type
696 * @count: requested buffer count
697 *
698 * Should be called from vidioc_reqbufs ioctl handler of a driver.
699 *
700 * This function:
701 *
702 * #) verifies streaming parameters passed from the userspace,
703 * #) sets up the queue,
704 * #) negotiates number of buffers and planes per buffer with the driver
705 * to be used during streaming,
706 * #) allocates internal buffer structures (struct vb2_buffer), according to
707 * the agreed parameters,
708 * #) for MMAP memory type, allocates actual video memory, using the
709 * memory handling/allocation routines provided during queue initialization
710 *
711 * If req->count is 0, all the memory will be freed instead.
712 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
713 * and the queue is not busy, memory will be reallocated.
714 *
715 * The return values from this function are intended to be directly returned
716 * from vidioc_reqbufs handler in driver.
717 */
718 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
719 unsigned int *count);
720
721 /**
722 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
723 * @q: videobuf2 queue
724 * @memory: memory type
725 * @count: requested buffer count
726 * @requested_planes: number of planes requested
727 * @requested_sizes: array with the size of the planes
728 *
729 * Should be called from VIDIOC_CREATE_BUFS() ioctl handler of a driver.
730 * This function:
731 *
732 * #) verifies parameter sanity
733 * #) calls the .queue_setup() queue operation
734 * #) performs any necessary memory allocations
735 *
736 * Return: the return values from this function are intended to be directly
737 * returned from VIDIOC_CREATE_BUFS() handler in driver.
738 */
739 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
740 unsigned int *count, unsigned int requested_planes,
741 const unsigned int requested_sizes[]);
742
743 /**
744 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
745 * to the kernel
746 * @q: videobuf2 queue
747 * @index: id number of the buffer
748 * @pb: buffer structure passed from userspace to vidioc_prepare_buf
749 * handler in driver
750 *
751 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
752 * The passed buffer should have been verified.
753 * This function calls buf_prepare callback in the driver (if provided),
754 * in which driver-specific buffer initialization can be performed,
755 *
756 * The return values from this function are intended to be directly returned
757 * from vidioc_prepare_buf handler in driver.
758 */
759 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
760
761 /**
762 * vb2_setup_out_fence() - setup new out-fence
763 * @q: The vb2_queue where to setup it
764 * @index: index of the buffer
765 *
766 * Setup the file descriptor, the fence and the sync_file for the next
767 * buffer to be queued and add everything to the tail of the q->out_fence_list.
768 */
769 int vb2_setup_out_fence(struct vb2_queue *q, unsigned int index);
770
771 /**
772 * vb2_core_qbuf() - Queue a buffer from userspace
773 *
774 * @q: videobuf2 queue
775 * @index: id number of the buffer
776 * @pb: buffer structure passed from userspace to vidioc_qbuf handler
777 * in driver
778 * @in_fence: in-fence to wait on before queueing the buffer
779 *
780 * Should be called from vidioc_qbuf ioctl handler of a driver.
781 * The passed buffer should have been verified.
782 *
783 * This function:
784 *
785 * #) if necessary, calls buf_prepare callback in the driver (if provided), in
786 * which driver-specific buffer initialization can be performed,
787 * #) if streaming is on, queues the buffer in driver by the means of
788 * &vb2_ops->buf_queue callback for processing.
789 *
790 * The return values from this function are intended to be directly returned
791 * from vidioc_qbuf handler in driver.
792 */
793 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
794 struct dma_fence *in_fence);
795
796 /**
797 * vb2_core_dqbuf() - Dequeue a buffer to the userspace
798 * @q: videobuf2 queue
799 * @pindex: pointer to the buffer index. May be NULL
800 * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
801 * in driver
802 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
803 * buffers ready for dequeuing are present. Normally the driver
804 * would be passing (file->f_flags & O_NONBLOCK) here
805 *
806 * Should be called from vidioc_dqbuf ioctl handler of a driver.
807 * The passed buffer should have been verified.
808 *
809 * This function:
810 *
811 * #) calls buf_finish callback in the driver (if provided), in which
812 * driver can perform any additional operations that may be required before
813 * returning the buffer to userspace, such as cache sync,
814 * #) the buffer struct members are filled with relevant information for
815 * the userspace.
816 *
817 * The return values from this function are intended to be directly returned
818 * from vidioc_dqbuf handler in driver.
819 */
820 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
821 bool nonblocking);
822
823 int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
824 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
825
826 /**
827 * vb2_core_expbuf() - Export a buffer as a file descriptor
828 * @q: videobuf2 queue
829 * @fd: file descriptor associated with DMABUF (set by driver) *
830 * @type: buffer type
831 * @index: id number of the buffer
832 * @plane: index of the plane to be exported, 0 for single plane queues
833 * @flags: flags for newly created file, currently only O_CLOEXEC is
834 * supported, refer to manual of open syscall for more details
835 *
836 * The return values from this function are intended to be directly returned
837 * from vidioc_expbuf handler in driver.
838 */
839 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
840 unsigned int index, unsigned int plane, unsigned int flags);
841
842 /**
843 * vb2_core_queue_init() - initialize a videobuf2 queue
844 * @q: videobuf2 queue; this structure should be allocated in driver
845 *
846 * The vb2_queue structure should be allocated by the driver. The driver is
847 * responsible of clearing it's content and setting initial values for some
848 * required entries before calling this function.
849 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
850 * to the struct vb2_queue description in include/media/videobuf2-core.h
851 * for more information.
852 */
853 int vb2_core_queue_init(struct vb2_queue *q);
854
855 /**
856 * vb2_core_queue_release() - stop streaming, release the queue and free memory
857 * @q: videobuf2 queue
858 *
859 * This function stops streaming and performs necessary clean ups, including
860 * freeing video buffer memory. The driver is responsible for freeing
861 * the vb2_queue structure itself.
862 */
863 void vb2_core_queue_release(struct vb2_queue *q);
864
865 /**
866 * vb2_queue_error() - signal a fatal error on the queue
867 * @q: videobuf2 queue
868 *
869 * Flag that a fatal unrecoverable error has occurred and wake up all processes
870 * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
871 * buffers will return -EIO.
872 *
873 * The error flag will be cleared when cancelling the queue, either from
874 * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
875 * function before starting the stream, otherwise the error flag will remain set
876 * until the queue is released when closing the device node.
877 */
878 void vb2_queue_error(struct vb2_queue *q);
879
880 /**
881 * vb2_mmap() - map video buffers into application address space
882 * @q: videobuf2 queue
883 * @vma: vma passed to the mmap file operation handler in the driver
884 *
885 * Should be called from mmap file operation handler of a driver.
886 * This function maps one plane of one of the available video buffers to
887 * userspace. To map whole video memory allocated on reqbufs, this function
888 * has to be called once per each plane per each buffer previously allocated.
889 *
890 * When the userspace application calls mmap, it passes to it an offset returned
891 * to it earlier by the means of vidioc_querybuf handler. That offset acts as
892 * a "cookie", which is then used to identify the plane to be mapped.
893 * This function finds a plane with a matching offset and a mapping is performed
894 * by the means of a provided memory operation.
895 *
896 * The return values from this function are intended to be directly returned
897 * from the mmap handler in driver.
898 */
899 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
900
901 #ifndef CONFIG_MMU
902 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
903 unsigned long addr,
904 unsigned long len,
905 unsigned long pgoff,
906 unsigned long flags);
907 #endif
908
909 /**
910 * vb2_core_poll() - implements poll userspace operation
911 * @q: videobuf2 queue
912 * @file: file argument passed to the poll file operation handler
913 * @wait: wait argument passed to the poll file operation handler
914 *
915 * This function implements poll file operation handler for a driver.
916 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
917 * be informed that the file descriptor of a video device is available for
918 * reading.
919 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
920 * will be reported as available for writing.
921 *
922 * The return values from this function are intended to be directly returned
923 * from poll handler in driver.
924 */
925 unsigned int vb2_core_poll(struct vb2_queue *q, struct file *file,
926 poll_table *wait);
927
928 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
929 loff_t *ppos, int nonblock);
930 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
931 loff_t *ppos, int nonblock);
932
933 /**
934 * typedef vb2_thread_fnc - callback function for use with vb2_thread
935 *
936 * @vb: pointer to struct &vb2_buffer
937 * @priv: pointer to a private pointer
938 *
939 * This is called whenever a buffer is dequeued in the thread.
940 */
941 typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
942
943 /**
944 * vb2_thread_start() - start a thread for the given queue.
945 * @q: videobuf queue
946 * @fnc: callback function
947 * @priv: priv pointer passed to the callback function
948 * @thread_name:the name of the thread. This will be prefixed with "vb2-".
949 *
950 * This starts a thread that will queue and dequeue until an error occurs
951 * or @vb2_thread_stop is called.
952 *
953 * .. attention::
954 *
955 * This function should not be used for anything else but the videobuf2-dvb
956 * support. If you think you have another good use-case for this, then please
957 * contact the linux-media mailing list first.
958 */
959 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
960 const char *thread_name);
961
962 /**
963 * vb2_thread_stop() - stop the thread for the given queue.
964 * @q: videobuf queue
965 */
966 int vb2_thread_stop(struct vb2_queue *q);
967
968 /**
969 * vb2_is_streaming() - return streaming status of the queue
970 * @q: videobuf queue
971 */
972 static inline bool vb2_is_streaming(struct vb2_queue *q)
973 {
974 return q->streaming;
975 }
976
977 /**
978 * vb2_fileio_is_active() - return true if fileio is active.
979 * @q: videobuf queue
980 *
981 * This returns true if read() or write() is used to stream the data
982 * as opposed to stream I/O. This is almost never an important distinction,
983 * except in rare cases. One such case is that using read() or write() to
984 * stream a format using V4L2_FIELD_ALTERNATE is not allowed since there
985 * is no way you can pass the field information of each buffer to/from
986 * userspace. A driver that supports this field format should check for
987 * this in the queue_setup op and reject it if this function returns true.
988 */
989 static inline bool vb2_fileio_is_active(struct vb2_queue *q)
990 {
991 return q->fileio;
992 }
993
994 /**
995 * vb2_is_busy() - return busy status of the queue
996 * @q: videobuf queue
997 *
998 * This function checks if queue has any buffers allocated.
999 */
1000 static inline bool vb2_is_busy(struct vb2_queue *q)
1001 {
1002 return (q->num_buffers > 0);
1003 }
1004
1005 /**
1006 * vb2_get_drv_priv() - return driver private data associated with the queue
1007 * @q: videobuf queue
1008 */
1009 static inline void *vb2_get_drv_priv(struct vb2_queue *q)
1010 {
1011 return q->drv_priv;
1012 }
1013
1014 /**
1015 * vb2_set_plane_payload() - set bytesused for the plane plane_no
1016 * @vb: buffer for which plane payload should be set
1017 * @plane_no: plane number for which payload should be set
1018 * @size: payload in bytes
1019 */
1020 static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
1021 unsigned int plane_no, unsigned long size)
1022 {
1023 if (plane_no < vb->num_planes)
1024 vb->planes[plane_no].bytesused = size;
1025 }
1026
1027 /**
1028 * vb2_get_plane_payload() - get bytesused for the plane plane_no
1029 * @vb: buffer for which plane payload should be set
1030 * @plane_no: plane number for which payload should be set
1031 */
1032 static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
1033 unsigned int plane_no)
1034 {
1035 if (plane_no < vb->num_planes)
1036 return vb->planes[plane_no].bytesused;
1037 return 0;
1038 }
1039
1040 /**
1041 * vb2_plane_size() - return plane size in bytes
1042 * @vb: buffer for which plane size should be returned
1043 * @plane_no: plane number for which size should be returned
1044 */
1045 static inline unsigned long
1046 vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
1047 {
1048 if (plane_no < vb->num_planes)
1049 return vb->planes[plane_no].length;
1050 return 0;
1051 }
1052
1053 /**
1054 * vb2_start_streaming_called() - return streaming status of driver
1055 * @q: videobuf queue
1056 */
1057 static inline bool vb2_start_streaming_called(struct vb2_queue *q)
1058 {
1059 return q->start_streaming_called;
1060 }
1061
1062 /**
1063 * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue
1064 * @q: videobuf queue
1065 */
1066 static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
1067 {
1068 q->last_buffer_dequeued = false;
1069 }
1070
1071 /*
1072 * The following functions are not part of the vb2 core API, but are useful
1073 * functions for videobuf2-*.
1074 */
1075
1076 /**
1077 * vb2_buffer_in_use() - return true if the buffer is in use and
1078 * the queue cannot be freed (by the means of REQBUFS(0)) call
1079 *
1080 * @vb: buffer for which plane size should be returned
1081 * @q: videobuf queue
1082 */
1083 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
1084
1085 /**
1086 * vb2_verify_memory_type() - Check whether the memory type and buffer type
1087 * passed to a buffer operation are compatible with the queue.
1088 *
1089 * @q: videobuf queue
1090 * @memory: memory model, as defined by enum &vb2_memory.
1091 * @type: private buffer type whose content is defined by the vb2-core
1092 * caller. For example, for V4L2, it should match
1093 * the types defined on enum &v4l2_buf_type
1094 */
1095 int vb2_verify_memory_type(struct vb2_queue *q,
1096 enum vb2_memory memory, unsigned int type);
1097 #endif /* _MEDIA_VIDEOBUF2_CORE_H */