Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * videobuf2-core.h - Video Buffer 2 Core Framework
0003  *
0004  * Copyright (C) 2010 Samsung Electronics
0005  *
0006  * Author: Pawel Osciak <pawel@osciak.com>
0007  *
0008  * This program is free software; you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation.
0011  */
0012 #ifndef _MEDIA_VIDEOBUF2_CORE_H
0013 #define _MEDIA_VIDEOBUF2_CORE_H
0014 
0015 #include <linux/mm_types.h>
0016 #include <linux/mutex.h>
0017 #include <linux/poll.h>
0018 #include <linux/dma-buf.h>
0019 #include <linux/bitops.h>
0020 #include <media/media-request.h>
0021 #include <media/frame_vector.h>
0022 
0023 #define VB2_MAX_FRAME   (32)
0024 #define VB2_MAX_PLANES  (8)
0025 
0026 /**
0027  * enum vb2_memory - type of memory model used to make the buffers visible
0028  *  on userspace.
0029  *
0030  * @VB2_MEMORY_UNKNOWN: Buffer status is unknown or it is not used yet on
0031  *          userspace.
0032  * @VB2_MEMORY_MMAP:    The buffers are allocated by the Kernel and it is
0033  *          memory mapped via mmap() ioctl. This model is
0034  *          also used when the user is using the buffers via
0035  *          read() or write() system calls.
0036  * @VB2_MEMORY_USERPTR: The buffers was allocated in userspace and it is
0037  *          memory mapped via mmap() ioctl.
0038  * @VB2_MEMORY_DMABUF:  The buffers are passed to userspace via DMA buffer.
0039  */
0040 enum vb2_memory {
0041     VB2_MEMORY_UNKNOWN  = 0,
0042     VB2_MEMORY_MMAP     = 1,
0043     VB2_MEMORY_USERPTR  = 2,
0044     VB2_MEMORY_DMABUF   = 4,
0045 };
0046 
0047 struct vb2_fileio_data;
0048 struct vb2_threadio_data;
0049 struct vb2_buffer;
0050 
0051 /**
0052  * struct vb2_mem_ops - memory handling/memory allocator operations.
0053  * @alloc:  allocate video memory and, optionally, allocator private data,
0054  *      return ERR_PTR() on failure or a pointer to allocator private,
0055  *      per-buffer data on success; the returned private structure
0056  *      will then be passed as @buf_priv argument to other ops in this
0057  *      structure. The size argument to this function shall be
0058  *      *page aligned*.
0059  * @put:    inform the allocator that the buffer will no longer be used;
0060  *      usually will result in the allocator freeing the buffer (if
0061  *      no other users of this buffer are present); the @buf_priv
0062  *      argument is the allocator private per-buffer structure
0063  *      previously returned from the alloc callback.
0064  * @get_dmabuf: acquire userspace memory for a hardware operation; used for
0065  *       DMABUF memory types.
0066  * @get_userptr: acquire userspace memory for a hardware operation; used for
0067  *       USERPTR memory types; vaddr is the address passed to the
0068  *       videobuf layer when queuing a video buffer of USERPTR type;
0069  *       should return an allocator private per-buffer structure
0070  *       associated with the buffer on success, ERR_PTR() on failure;
0071  *       the returned private structure will then be passed as @buf_priv
0072  *       argument to other ops in this structure.
0073  * @put_userptr: inform the allocator that a USERPTR buffer will no longer
0074  *       be used.
0075  * @attach_dmabuf: attach a shared &struct dma_buf for a hardware operation;
0076  *         used for DMABUF memory types; dev is the alloc device
0077  *         dbuf is the shared dma_buf; returns ERR_PTR() on failure;
0078  *         allocator private per-buffer structure on success;
0079  *         this needs to be used for further accesses to the buffer.
0080  * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
0081  *         buffer is no longer used; the @buf_priv argument is the
0082  *         allocator private per-buffer structure previously returned
0083  *         from the attach_dmabuf callback.
0084  * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
0085  *      of dmabuf is informed that this driver is going to use the
0086  *      dmabuf.
0087  * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
0088  *        that this driver is done using the dmabuf for now.
0089  * @prepare:    called every time the buffer is passed from userspace to the
0090  *      driver, useful for cache synchronisation, optional.
0091  * @finish: called every time the buffer is passed back from the driver
0092  *      to the userspace, also optional.
0093  * @vaddr:  return a kernel virtual address to a given memory buffer
0094  *      associated with the passed private structure or NULL if no
0095  *      such mapping exists.
0096  * @cookie: return allocator specific cookie for a given memory buffer
0097  *      associated with the passed private structure or NULL if not
0098  *      available.
0099  * @num_users:  return the current number of users of a memory buffer;
0100  *      return 1 if the videobuf layer (or actually the driver using
0101  *      it) is the only user.
0102  * @mmap:   setup a userspace mapping for a given memory buffer under
0103  *      the provided virtual memory region.
0104  *
0105  * Those operations are used by the videobuf2 core to implement the memory
0106  * handling/memory allocators for each type of supported streaming I/O method.
0107  *
0108  * .. note::
0109  *    #) Required ops for USERPTR types: get_userptr, put_userptr.
0110  *
0111  *    #) Required ops for MMAP types: alloc, put, num_users, mmap.
0112  *
0113  *    #) Required ops for read/write access types: alloc, put, num_users, vaddr.
0114  *
0115  *    #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf,
0116  *       map_dmabuf, unmap_dmabuf.
0117  */
0118 struct vb2_mem_ops {
0119     void        *(*alloc)(struct vb2_buffer *vb,
0120                   struct device *dev,
0121                   unsigned long size);
0122     void        (*put)(void *buf_priv);
0123     struct dma_buf *(*get_dmabuf)(struct vb2_buffer *vb,
0124                       void *buf_priv,
0125                       unsigned long flags);
0126 
0127     void        *(*get_userptr)(struct vb2_buffer *vb,
0128                     struct device *dev,
0129                     unsigned long vaddr,
0130                     unsigned long size);
0131     void        (*put_userptr)(void *buf_priv);
0132 
0133     void        (*prepare)(void *buf_priv);
0134     void        (*finish)(void *buf_priv);
0135 
0136     void        *(*attach_dmabuf)(struct vb2_buffer *vb,
0137                       struct device *dev,
0138                       struct dma_buf *dbuf,
0139                       unsigned long size);
0140     void        (*detach_dmabuf)(void *buf_priv);
0141     int     (*map_dmabuf)(void *buf_priv);
0142     void        (*unmap_dmabuf)(void *buf_priv);
0143 
0144     void        *(*vaddr)(struct vb2_buffer *vb, void *buf_priv);
0145     void        *(*cookie)(struct vb2_buffer *vb, void *buf_priv);
0146 
0147     unsigned int    (*num_users)(void *buf_priv);
0148 
0149     int     (*mmap)(void *buf_priv, struct vm_area_struct *vma);
0150 };
0151 
0152 /**
0153  * struct vb2_plane - plane information.
0154  * @mem_priv:   private data with this plane.
0155  * @dbuf:   dma_buf - shared buffer object.
0156  * @dbuf_mapped:    flag to show whether dbuf is mapped or not
0157  * @bytesused:  number of bytes occupied by data in the plane (payload).
0158  * @length: size of this plane (NOT the payload) in bytes. The maximum
0159  *      valid size is MAX_UINT - PAGE_SIZE.
0160  * @min_length: minimum required size of this plane (NOT the payload) in bytes.
0161  *      @length is always greater or equal to @min_length, and like
0162  *      @length, it is limited to MAX_UINT - PAGE_SIZE.
0163  * @m:      Union with memtype-specific data.
0164  * @m.offset:   when memory in the associated struct vb2_buffer is
0165  *      %VB2_MEMORY_MMAP, equals the offset from the start of
0166  *      the device memory for this plane (or is a "cookie" that
0167  *      should be passed to mmap() called on the video node).
0168  * @m.userptr:  when memory is %VB2_MEMORY_USERPTR, a userspace pointer
0169  *      pointing to this plane.
0170  * @m.fd:   when memory is %VB2_MEMORY_DMABUF, a userspace file
0171  *      descriptor associated with this plane.
0172  * @data_offset:    offset in the plane to the start of data; usually 0,
0173  *      unless there is a header in front of the data.
0174  *
0175  * Should contain enough information to be able to cover all the fields
0176  * of &struct v4l2_plane at videodev2.h.
0177  */
0178 struct vb2_plane {
0179     void            *mem_priv;
0180     struct dma_buf      *dbuf;
0181     unsigned int        dbuf_mapped;
0182     unsigned int        bytesused;
0183     unsigned int        length;
0184     unsigned int        min_length;
0185     union {
0186         unsigned int    offset;
0187         unsigned long   userptr;
0188         int     fd;
0189     } m;
0190     unsigned int        data_offset;
0191 };
0192 
0193 /**
0194  * enum vb2_io_modes - queue access methods.
0195  * @VB2_MMAP:       driver supports MMAP with streaming API.
0196  * @VB2_USERPTR:    driver supports USERPTR with streaming API.
0197  * @VB2_READ:       driver supports read() style access.
0198  * @VB2_WRITE:      driver supports write() style access.
0199  * @VB2_DMABUF:     driver supports DMABUF with streaming API.
0200  */
0201 enum vb2_io_modes {
0202     VB2_MMAP    = BIT(0),
0203     VB2_USERPTR = BIT(1),
0204     VB2_READ    = BIT(2),
0205     VB2_WRITE   = BIT(3),
0206     VB2_DMABUF  = BIT(4),
0207 };
0208 
0209 /**
0210  * enum vb2_buffer_state - current video buffer state.
0211  * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control.
0212  * @VB2_BUF_STATE_IN_REQUEST:   buffer is queued in media request.
0213  * @VB2_BUF_STATE_PREPARING:    buffer is being prepared in videobuf.
0214  * @VB2_BUF_STATE_QUEUED:   buffer queued in videobuf, but not in driver.
0215  * @VB2_BUF_STATE_ACTIVE:   buffer queued in driver and possibly used
0216  *              in a hardware operation.
0217  * @VB2_BUF_STATE_DONE:     buffer returned from driver to videobuf, but
0218  *              not yet dequeued to userspace.
0219  * @VB2_BUF_STATE_ERROR:    same as above, but the operation on the buffer
0220  *              has ended with an error, which will be reported
0221  *              to the userspace when it is dequeued.
0222  */
0223 enum vb2_buffer_state {
0224     VB2_BUF_STATE_DEQUEUED,
0225     VB2_BUF_STATE_IN_REQUEST,
0226     VB2_BUF_STATE_PREPARING,
0227     VB2_BUF_STATE_QUEUED,
0228     VB2_BUF_STATE_ACTIVE,
0229     VB2_BUF_STATE_DONE,
0230     VB2_BUF_STATE_ERROR,
0231 };
0232 
0233 struct vb2_queue;
0234 
0235 /**
0236  * struct vb2_buffer - represents a video buffer.
0237  * @vb2_queue:      pointer to &struct vb2_queue with the queue to
0238  *          which this driver belongs.
0239  * @index:      id number of the buffer.
0240  * @type:       buffer type.
0241  * @memory:     the method, in which the actual data is passed.
0242  * @num_planes:     number of planes in the buffer
0243  *          on an internal driver queue.
0244  * @timestamp:      frame timestamp in ns.
0245  * @request:        the request this buffer is associated with.
0246  * @req_obj:        used to bind this buffer to a request. This
0247  *          request object has a refcount.
0248  */
0249 struct vb2_buffer {
0250     struct vb2_queue    *vb2_queue;
0251     unsigned int        index;
0252     unsigned int        type;
0253     unsigned int        memory;
0254     unsigned int        num_planes;
0255     u64         timestamp;
0256     struct media_request    *request;
0257     struct media_request_object req_obj;
0258 
0259     /* private: internal use only
0260      *
0261      * state:       current buffer state; do not change
0262      * synced:      this buffer has been synced for DMA, i.e. the
0263      *          'prepare' memop was called. It is cleared again
0264      *          after the 'finish' memop is called.
0265      * prepared:        this buffer has been prepared, i.e. the
0266      *          buf_prepare op was called. It is cleared again
0267      *          after the 'buf_finish' op is called.
0268      * copied_timestamp:    the timestamp of this capture buffer was copied
0269      *          from an output buffer.
0270      * skip_cache_sync_on_prepare: when set buffer's ->prepare() function
0271      *          skips cache sync/invalidation.
0272      * skip_cache_sync_on_finish: when set buffer's ->finish() function
0273      *          skips cache sync/invalidation.
0274      * queued_entry:    entry on the queued buffers list, which holds
0275      *          all buffers queued from userspace
0276      * done_entry:      entry on the list that stores all buffers ready
0277      *          to be dequeued to userspace
0278      * vb2_plane:       per-plane information; do not change
0279      */
0280     enum vb2_buffer_state   state;
0281     unsigned int        synced:1;
0282     unsigned int        prepared:1;
0283     unsigned int        copied_timestamp:1;
0284     unsigned int        skip_cache_sync_on_prepare:1;
0285     unsigned int        skip_cache_sync_on_finish:1;
0286 
0287     struct vb2_plane    planes[VB2_MAX_PLANES];
0288     struct list_head    queued_entry;
0289     struct list_head    done_entry;
0290 #ifdef CONFIG_VIDEO_ADV_DEBUG
0291     /*
0292      * Counters for how often these buffer-related ops are
0293      * called. Used to check for unbalanced ops.
0294      */
0295     u32     cnt_mem_alloc;
0296     u32     cnt_mem_put;
0297     u32     cnt_mem_get_dmabuf;
0298     u32     cnt_mem_get_userptr;
0299     u32     cnt_mem_put_userptr;
0300     u32     cnt_mem_prepare;
0301     u32     cnt_mem_finish;
0302     u32     cnt_mem_attach_dmabuf;
0303     u32     cnt_mem_detach_dmabuf;
0304     u32     cnt_mem_map_dmabuf;
0305     u32     cnt_mem_unmap_dmabuf;
0306     u32     cnt_mem_vaddr;
0307     u32     cnt_mem_cookie;
0308     u32     cnt_mem_num_users;
0309     u32     cnt_mem_mmap;
0310 
0311     u32     cnt_buf_out_validate;
0312     u32     cnt_buf_init;
0313     u32     cnt_buf_prepare;
0314     u32     cnt_buf_finish;
0315     u32     cnt_buf_cleanup;
0316     u32     cnt_buf_queue;
0317     u32     cnt_buf_request_complete;
0318 
0319     /* This counts the number of calls to vb2_buffer_done() */
0320     u32     cnt_buf_done;
0321 #endif
0322 };
0323 
0324 /**
0325  * struct vb2_ops - driver-specific callbacks.
0326  *
0327  * These operations are not called from interrupt context except where
0328  * mentioned specifically.
0329  *
0330  * @queue_setup:    called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
0331  *          handlers before memory allocation. It can be called
0332  *          twice: if the original number of requested buffers
0333  *          could not be allocated, then it will be called a
0334  *          second time with the actually allocated number of
0335  *          buffers to verify if that is OK.
0336  *          The driver should return the required number of buffers
0337  *          in \*num_buffers, the required number of planes per
0338  *          buffer in \*num_planes, the size of each plane should be
0339  *          set in the sizes\[\] array and optional per-plane
0340  *          allocator specific device in the alloc_devs\[\] array.
0341  *          When called from VIDIOC_REQBUFS(), \*num_planes == 0,
0342  *          the driver has to use the currently configured format to
0343  *          determine the plane sizes and \*num_buffers is the total
0344  *          number of buffers that are being allocated. When called
0345  *          from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it
0346  *          describes the requested number of planes and sizes\[\]
0347  *          contains the requested plane sizes. In this case
0348  *          \*num_buffers are being allocated additionally to
0349  *          q->num_buffers. If either \*num_planes or the requested
0350  *          sizes are invalid callback must return %-EINVAL.
0351  * @wait_prepare:   release any locks taken while calling vb2 functions;
0352  *          it is called before an ioctl needs to wait for a new
0353  *          buffer to arrive; required to avoid a deadlock in
0354  *          blocking access type.
0355  * @wait_finish:    reacquire all locks released in the previous callback;
0356  *          required to continue operation after sleeping while
0357  *          waiting for a new buffer to arrive.
0358  * @buf_out_validate:   called when the output buffer is prepared or queued
0359  *          to a request; drivers can use this to validate
0360  *          userspace-provided information; this is required only
0361  *          for OUTPUT queues.
0362  * @buf_init:       called once after allocating a buffer (in MMAP case)
0363  *          or after acquiring a new USERPTR buffer; drivers may
0364  *          perform additional buffer-related initialization;
0365  *          initialization failure (return != 0) will prevent
0366  *          queue setup from completing successfully; optional.
0367  * @buf_prepare:    called every time the buffer is queued from userspace
0368  *          and from the VIDIOC_PREPARE_BUF() ioctl; drivers may
0369  *          perform any initialization required before each
0370  *          hardware operation in this callback; drivers can
0371  *          access/modify the buffer here as it is still synced for
0372  *          the CPU; drivers that support VIDIOC_CREATE_BUFS() must
0373  *          also validate the buffer size; if an error is returned,
0374  *          the buffer will not be queued in driver; optional.
0375  * @buf_finish:     called before every dequeue of the buffer back to
0376  *          userspace; the buffer is synced for the CPU, so drivers
0377  *          can access/modify the buffer contents; drivers may
0378  *          perform any operations required before userspace
0379  *          accesses the buffer; optional. The buffer state can be
0380  *          one of the following: %DONE and %ERROR occur while
0381  *          streaming is in progress, and the %PREPARED state occurs
0382  *          when the queue has been canceled and all pending
0383  *          buffers are being returned to their default %DEQUEUED
0384  *          state. Typically you only have to do something if the
0385  *          state is %VB2_BUF_STATE_DONE, since in all other cases
0386  *          the buffer contents will be ignored anyway.
0387  * @buf_cleanup:    called once before the buffer is freed; drivers may
0388  *          perform any additional cleanup; optional.
0389  * @start_streaming:    called once to enter 'streaming' state; the driver may
0390  *          receive buffers with @buf_queue callback
0391  *          before @start_streaming is called; the driver gets the
0392  *          number of already queued buffers in count parameter;
0393  *          driver can return an error if hardware fails, in that
0394  *          case all buffers that have been already given by
0395  *          the @buf_queue callback are to be returned by the driver
0396  *          by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED.
0397  *          If you need a minimum number of buffers before you can
0398  *          start streaming, then set
0399  *          &vb2_queue->min_buffers_needed. If that is non-zero
0400  *          then @start_streaming won't be called until at least
0401  *          that many buffers have been queued up by userspace.
0402  * @stop_streaming: called when 'streaming' state must be disabled; driver
0403  *          should stop any DMA transactions or wait until they
0404  *          finish and give back all buffers it got from &buf_queue
0405  *          callback by calling vb2_buffer_done() with either
0406  *          %VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use
0407  *          vb2_wait_for_all_buffers() function
0408  * @buf_queue:      passes buffer vb to the driver; driver may start
0409  *          hardware operation on this buffer; driver should give
0410  *          the buffer back by calling vb2_buffer_done() function;
0411  *          it is always called after calling VIDIOC_STREAMON()
0412  *          ioctl; might be called before @start_streaming callback
0413  *          if user pre-queued buffers before calling
0414  *          VIDIOC_STREAMON().
0415  * @buf_request_complete: a buffer that was never queued to the driver but is
0416  *          associated with a queued request was canceled.
0417  *          The driver will have to mark associated objects in the
0418  *          request as completed; required if requests are
0419  *          supported.
0420  */
0421 struct vb2_ops {
0422     int (*queue_setup)(struct vb2_queue *q,
0423                unsigned int *num_buffers, unsigned int *num_planes,
0424                unsigned int sizes[], struct device *alloc_devs[]);
0425 
0426     void (*wait_prepare)(struct vb2_queue *q);
0427     void (*wait_finish)(struct vb2_queue *q);
0428 
0429     int (*buf_out_validate)(struct vb2_buffer *vb);
0430     int (*buf_init)(struct vb2_buffer *vb);
0431     int (*buf_prepare)(struct vb2_buffer *vb);
0432     void (*buf_finish)(struct vb2_buffer *vb);
0433     void (*buf_cleanup)(struct vb2_buffer *vb);
0434 
0435     int (*start_streaming)(struct vb2_queue *q, unsigned int count);
0436     void (*stop_streaming)(struct vb2_queue *q);
0437 
0438     void (*buf_queue)(struct vb2_buffer *vb);
0439 
0440     void (*buf_request_complete)(struct vb2_buffer *vb);
0441 };
0442 
0443 /**
0444  * struct vb2_buf_ops - driver-specific callbacks.
0445  *
0446  * @verify_planes_array: Verify that a given user space structure contains
0447  *          enough planes for the buffer. This is called
0448  *          for each dequeued buffer.
0449  * @init_buffer:    given a &vb2_buffer initialize the extra data after
0450  *          struct vb2_buffer.
0451  *          For V4L2 this is a &struct vb2_v4l2_buffer.
0452  * @fill_user_buffer:   given a &vb2_buffer fill in the userspace structure.
0453  *          For V4L2 this is a &struct v4l2_buffer.
0454  * @fill_vb2_buffer:    given a userspace structure, fill in the &vb2_buffer.
0455  *          If the userspace structure is invalid, then this op
0456  *          will return an error.
0457  * @copy_timestamp: copy the timestamp from a userspace structure to
0458  *          the &struct vb2_buffer.
0459  */
0460 struct vb2_buf_ops {
0461     int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
0462     void (*init_buffer)(struct vb2_buffer *vb);
0463     void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
0464     int (*fill_vb2_buffer)(struct vb2_buffer *vb, struct vb2_plane *planes);
0465     void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
0466 };
0467 
0468 /**
0469  * struct vb2_queue - a videobuf queue.
0470  *
0471  * @type:   private buffer type whose content is defined by the vb2-core
0472  *      caller. For example, for V4L2, it should match
0473  *      the types defined on &enum v4l2_buf_type.
0474  * @io_modes:   supported io methods (see &enum vb2_io_modes).
0475  * @alloc_devs: &struct device memory type/allocator-specific per-plane device
0476  * @dev:    device to use for the default allocation context if the driver
0477  *      doesn't fill in the @alloc_devs array.
0478  * @dma_attrs:  DMA attributes to use for the DMA.
0479  * @bidirectional: when this flag is set the DMA direction for the buffers of
0480  *      this queue will be overridden with %DMA_BIDIRECTIONAL direction.
0481  *      This is useful in cases where the hardware (firmware) writes to
0482  *      a buffer which is mapped as read (%DMA_TO_DEVICE), or reads from
0483  *      buffer which is mapped for write (%DMA_FROM_DEVICE) in order
0484  *      to satisfy some internal hardware restrictions or adds a padding
0485  *      needed by the processing algorithm. In case the DMA mapping is
0486  *      not bidirectional but the hardware (firmware) trying to access
0487  *      the buffer (in the opposite direction) this could lead to an
0488  *      IOMMU protection faults.
0489  * @fileio_read_once:       report EOF after reading the first buffer
0490  * @fileio_write_immediately:   queue buffer after each write() call
0491  * @allow_zero_bytesused:   allow bytesused == 0 to be passed to the driver
0492  * @quirk_poll_must_check_waiting_for_buffers: Return %EPOLLERR at poll when QBUF
0493  *              has not been called. This is a vb1 idiom that has been adopted
0494  *              also by vb2.
0495  * @supports_requests: this queue supports the Request API.
0496  * @requires_requests: this queue requires the Request API. If this is set to 1,
0497  *      then supports_requests must be set to 1 as well.
0498  * @uses_qbuf:  qbuf was used directly for this queue. Set to 1 the first
0499  *      time this is called. Set to 0 when the queue is canceled.
0500  *      If this is 1, then you cannot queue buffers from a request.
0501  * @uses_requests: requests are used for this queue. Set to 1 the first time
0502  *      a request is queued. Set to 0 when the queue is canceled.
0503  *      If this is 1, then you cannot queue buffers directly.
0504  * @allow_cache_hints: when set user-space can pass cache management hints in
0505  *      order to skip cache flush/invalidation on ->prepare() or/and
0506  *      ->finish().
0507  * @non_coherent_mem: when set queue will attempt to allocate buffers using
0508  *      non-coherent memory.
0509  * @lock:   pointer to a mutex that protects the &struct vb2_queue. The
0510  *      driver can set this to a mutex to let the v4l2 core serialize
0511  *      the queuing ioctls. If the driver wants to handle locking
0512  *      itself, then this should be set to NULL. This lock is not used
0513  *      by the videobuf2 core API.
0514  * @owner:  The filehandle that 'owns' the buffers, i.e. the filehandle
0515  *      that called reqbufs, create_buffers or started fileio.
0516  *      This field is not used by the videobuf2 core API, but it allows
0517  *      drivers to easily associate an owner filehandle with the queue.
0518  * @ops:    driver-specific callbacks
0519  * @mem_ops:    memory allocator specific callbacks
0520  * @buf_ops:    callbacks to deliver buffer information.
0521  *      between user-space and kernel-space.
0522  * @drv_priv:   driver private data.
0523  * @subsystem_flags: Flags specific to the subsystem (V4L2/DVB/etc.). Not used
0524  *      by the vb2 core.
0525  * @buf_struct_size: size of the driver-specific buffer structure;
0526  *      "0" indicates the driver doesn't want to use a custom buffer
0527  *      structure type. In that case a subsystem-specific struct
0528  *      will be used (in the case of V4L2 that is
0529  *      ``sizeof(struct vb2_v4l2_buffer)``). The first field of the
0530  *      driver-specific buffer structure must be the subsystem-specific
0531  *      struct (vb2_v4l2_buffer in the case of V4L2).
0532  * @timestamp_flags: Timestamp flags; ``V4L2_BUF_FLAG_TIMESTAMP_*`` and
0533  *      ``V4L2_BUF_FLAG_TSTAMP_SRC_*``
0534  * @gfp_flags:  additional gfp flags used when allocating the buffers.
0535  *      Typically this is 0, but it may be e.g. %GFP_DMA or %__GFP_DMA32
0536  *      to force the buffer allocation to a specific memory zone.
0537  * @min_buffers_needed: the minimum number of buffers needed before
0538  *      @start_streaming can be called. Used when a DMA engine
0539  *      cannot be started unless at least this number of buffers
0540  *      have been queued into the driver.
0541  */
0542 /*
0543  * Private elements (won't appear at the uAPI book):
0544  * @mmap_lock:  private mutex used when buffers are allocated/freed/mmapped
0545  * @memory: current memory type used
0546  * @dma_dir:    DMA mapping direction.
0547  * @bufs:   videobuf buffer structures
0548  * @num_buffers: number of allocated/used buffers
0549  * @queued_list: list of buffers currently queued from userspace
0550  * @queued_count: number of buffers queued and ready for streaming.
0551  * @owned_by_drv_count: number of buffers owned by the driver
0552  * @done_list:  list of buffers ready to be dequeued to userspace
0553  * @done_lock:  lock to protect done_list list
0554  * @done_wq:    waitqueue for processes waiting for buffers ready to be dequeued
0555  * @streaming:  current streaming state
0556  * @start_streaming_called: @start_streaming was called successfully and we
0557  *      started streaming.
0558  * @error:  a fatal error occurred on the queue
0559  * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
0560  *      buffers. Only set for capture queues if qbuf has not yet been
0561  *      called since poll() needs to return %EPOLLERR in that situation.
0562  * @is_multiplanar: set if buffer type is multiplanar
0563  * @is_output:  set if buffer type is output
0564  * @copy_timestamp: set if vb2-core should set timestamps
0565  * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
0566  *      last decoded buffer was already dequeued. Set for capture queues
0567  *      when a buffer with the %V4L2_BUF_FLAG_LAST is dequeued.
0568  * @fileio: file io emulator internal data, used only if emulator is active
0569  * @threadio:   thread io internal data, used only if thread is active
0570  * @name:   queue name, used for logging purpose. Initialized automatically
0571  *      if left empty by drivers.
0572  */
0573 struct vb2_queue {
0574     unsigned int            type;
0575     unsigned int            io_modes;
0576     struct device           *dev;
0577     unsigned long           dma_attrs;
0578     unsigned int            bidirectional:1;
0579     unsigned int            fileio_read_once:1;
0580     unsigned int            fileio_write_immediately:1;
0581     unsigned int            allow_zero_bytesused:1;
0582     unsigned int           quirk_poll_must_check_waiting_for_buffers:1;
0583     unsigned int            supports_requests:1;
0584     unsigned int            requires_requests:1;
0585     unsigned int            uses_qbuf:1;
0586     unsigned int            uses_requests:1;
0587     unsigned int            allow_cache_hints:1;
0588     unsigned int            non_coherent_mem:1;
0589 
0590     struct mutex            *lock;
0591     void                *owner;
0592 
0593     const struct vb2_ops        *ops;
0594     const struct vb2_mem_ops    *mem_ops;
0595     const struct vb2_buf_ops    *buf_ops;
0596 
0597     void                *drv_priv;
0598     u32             subsystem_flags;
0599     unsigned int            buf_struct_size;
0600     u32             timestamp_flags;
0601     gfp_t               gfp_flags;
0602     u32             min_buffers_needed;
0603 
0604     struct device           *alloc_devs[VB2_MAX_PLANES];
0605 
0606     /* private: internal use only */
0607     struct mutex            mmap_lock;
0608     unsigned int            memory;
0609     enum dma_data_direction     dma_dir;
0610     struct vb2_buffer       *bufs[VB2_MAX_FRAME];
0611     unsigned int            num_buffers;
0612 
0613     struct list_head        queued_list;
0614     unsigned int            queued_count;
0615 
0616     atomic_t            owned_by_drv_count;
0617     struct list_head        done_list;
0618     spinlock_t          done_lock;
0619     wait_queue_head_t       done_wq;
0620 
0621     unsigned int            streaming:1;
0622     unsigned int            start_streaming_called:1;
0623     unsigned int            error:1;
0624     unsigned int            waiting_for_buffers:1;
0625     unsigned int            waiting_in_dqbuf:1;
0626     unsigned int            is_multiplanar:1;
0627     unsigned int            is_output:1;
0628     unsigned int            copy_timestamp:1;
0629     unsigned int            last_buffer_dequeued:1;
0630 
0631     struct vb2_fileio_data      *fileio;
0632     struct vb2_threadio_data    *threadio;
0633 
0634     char                name[32];
0635 
0636 #ifdef CONFIG_VIDEO_ADV_DEBUG
0637     /*
0638      * Counters for how often these queue-related ops are
0639      * called. Used to check for unbalanced ops.
0640      */
0641     u32             cnt_queue_setup;
0642     u32             cnt_wait_prepare;
0643     u32             cnt_wait_finish;
0644     u32             cnt_start_streaming;
0645     u32             cnt_stop_streaming;
0646 #endif
0647 };
0648 
0649 /**
0650  * vb2_queue_allows_cache_hints() - Return true if the queue allows cache
0651  * and memory consistency hints.
0652  *
0653  * @q:      pointer to &struct vb2_queue with videobuf2 queue
0654  */
0655 static inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q)
0656 {
0657     return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP;
0658 }
0659 
0660 /**
0661  * vb2_plane_vaddr() - Return a kernel virtual address of a given plane.
0662  * @vb:     pointer to &struct vb2_buffer to which the plane in
0663  *      question belongs to.
0664  * @plane_no:   plane number for which the address is to be returned.
0665  *
0666  * This function returns a kernel virtual address of a given plane if
0667  * such a mapping exist, NULL otherwise.
0668  */
0669 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
0670 
0671 /**
0672  * vb2_plane_cookie() - Return allocator specific cookie for the given plane.
0673  * @vb:     pointer to &struct vb2_buffer to which the plane in
0674  *      question belongs to.
0675  * @plane_no:   plane number for which the cookie is to be returned.
0676  *
0677  * This function returns an allocator specific cookie for a given plane if
0678  * available, NULL otherwise. The allocator should provide some simple static
0679  * inline function, which would convert this cookie to the allocator specific
0680  * type that can be used directly by the driver to access the buffer. This can
0681  * be for example physical address, pointer to scatter list or IOMMU mapping.
0682  */
0683 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
0684 
0685 /**
0686  * vb2_buffer_done() - inform videobuf that an operation on a buffer
0687  *  is finished.
0688  * @vb:     pointer to &struct vb2_buffer to be used.
0689  * @state:  state of the buffer, as defined by &enum vb2_buffer_state.
0690  *      Either %VB2_BUF_STATE_DONE if the operation finished
0691  *      successfully, %VB2_BUF_STATE_ERROR if the operation finished
0692  *      with an error or %VB2_BUF_STATE_QUEUED.
0693  *
0694  * This function should be called by the driver after a hardware operation on
0695  * a buffer is finished and the buffer may be returned to userspace. The driver
0696  * cannot use this buffer anymore until it is queued back to it by videobuf
0697  * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued
0698  * to the driver by &vb2_ops->buf_queue can be passed to this function.
0699  *
0700  * While streaming a buffer can only be returned in state DONE or ERROR.
0701  * The &vb2_ops->start_streaming op can also return them in case the DMA engine
0702  * cannot be started for some reason. In that case the buffers should be
0703  * returned with state QUEUED to put them back into the queue.
0704  */
0705 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
0706 
0707 /**
0708  * vb2_discard_done() - discard all buffers marked as DONE.
0709  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0710  *
0711  * This function is intended to be used with suspend/resume operations. It
0712  * discards all 'done' buffers as they would be too old to be requested after
0713  * resume.
0714  *
0715  * Drivers must stop the hardware and synchronize with interrupt handlers and/or
0716  * delayed works before calling this function to make sure no buffer will be
0717  * touched by the driver and/or hardware.
0718  */
0719 void vb2_discard_done(struct vb2_queue *q);
0720 
0721 /**
0722  * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2.
0723  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0724  *
0725  * This function will wait until all buffers that have been given to the driver
0726  * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). It
0727  * doesn't call &vb2_ops->wait_prepare/&vb2_ops->wait_finish pair.
0728  * It is intended to be called with all locks taken, for example from
0729  * &vb2_ops->stop_streaming callback.
0730  */
0731 int vb2_wait_for_all_buffers(struct vb2_queue *q);
0732 
0733 /**
0734  * vb2_core_querybuf() - query video buffer information.
0735  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0736  * @index:  id number of the buffer.
0737  * @pb:     buffer struct passed from userspace.
0738  *
0739  * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called
0740  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
0741  *
0742  * The passed buffer should have been verified.
0743  *
0744  * This function fills the relevant information for the userspace.
0745  *
0746  * Return: returns zero on success; an error code otherwise.
0747  */
0748 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
0749 
0750 /**
0751  * vb2_core_reqbufs() - Initiate streaming.
0752  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0753  * @memory: memory type, as defined by &enum vb2_memory.
0754  * @flags:  auxiliary queue/buffer management flags. Currently, the only
0755  *      used flag is %V4L2_MEMORY_FLAG_NON_COHERENT.
0756  * @count:  requested buffer count.
0757  *
0758  * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
0759  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
0760  *
0761  * This function:
0762  *
0763  * #) verifies streaming parameters passed from the userspace;
0764  * #) sets up the queue;
0765  * #) negotiates number of buffers and planes per buffer with the driver
0766  *    to be used during streaming;
0767  * #) allocates internal buffer structures (&struct vb2_buffer), according to
0768  *    the agreed parameters;
0769  * #) for MMAP memory type, allocates actual video memory, using the
0770  *    memory handling/allocation routines provided during queue initialization.
0771  *
0772  * If req->count is 0, all the memory will be freed instead.
0773  *
0774  * If the queue has been allocated previously by a previous vb2_core_reqbufs()
0775  * call and the queue is not busy, memory will be reallocated.
0776  *
0777  * Return: returns zero on success; an error code otherwise.
0778  */
0779 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
0780              unsigned int flags, unsigned int *count);
0781 
0782 /**
0783  * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
0784  * @q: pointer to &struct vb2_queue with videobuf2 queue.
0785  * @memory: memory type, as defined by &enum vb2_memory.
0786  * @flags: auxiliary queue/buffer management flags.
0787  * @count: requested buffer count.
0788  * @requested_planes: number of planes requested.
0789  * @requested_sizes: array with the size of the planes.
0790  *
0791  * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is
0792  * called internally by VB2 by an API-specific handler, like
0793  * ``videobuf2-v4l2.h``.
0794  *
0795  * This function:
0796  *
0797  * #) verifies parameter sanity;
0798  * #) calls the &vb2_ops->queue_setup queue operation;
0799  * #) performs any necessary memory allocations.
0800  *
0801  * Return: returns zero on success; an error code otherwise.
0802  */
0803 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
0804              unsigned int flags, unsigned int *count,
0805              unsigned int requested_planes,
0806              const unsigned int requested_sizes[]);
0807 
0808 /**
0809  * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
0810  *          to the kernel.
0811  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0812  * @index:  id number of the buffer.
0813  * @pb:     buffer structure passed from userspace to
0814  *      &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
0815  *
0816  * Videobuf2 core helper to implement VIDIOC_PREPARE_BUF() operation. It is
0817  * called internally by VB2 by an API-specific handler, like
0818  * ``videobuf2-v4l2.h``.
0819  *
0820  * The passed buffer should have been verified.
0821  *
0822  * This function calls vb2_ops->buf_prepare callback in the driver
0823  * (if provided), in which driver-specific buffer initialization can
0824  * be performed.
0825  *
0826  * Return: returns zero on success; an error code otherwise.
0827  */
0828 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
0829 
0830 /**
0831  * vb2_core_qbuf() - Queue a buffer from userspace
0832  *
0833  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0834  * @index:  id number of the buffer
0835  * @pb:     buffer structure passed from userspace to
0836  *      v4l2_ioctl_ops->vidioc_qbuf handler in driver
0837  * @req:    pointer to &struct media_request, may be NULL.
0838  *
0839  * Videobuf2 core helper to implement VIDIOC_QBUF() operation. It is called
0840  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
0841  *
0842  * This function:
0843  *
0844  * #) If @req is non-NULL, then the buffer will be bound to this
0845  *    media request and it returns. The buffer will be prepared and
0846  *    queued to the driver (i.e. the next two steps) when the request
0847  *    itself is queued.
0848  * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
0849  *    (if provided), in which driver-specific buffer initialization can
0850  *    be performed;
0851  * #) if streaming is on, queues the buffer in driver by the means of
0852  *    &vb2_ops->buf_queue callback for processing.
0853  *
0854  * Return: returns zero on success; an error code otherwise.
0855  */
0856 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
0857           struct media_request *req);
0858 
0859 /**
0860  * vb2_core_dqbuf() - Dequeue a buffer to the userspace
0861  * @q:      pointer to &struct vb2_queue with videobuf2 queue
0862  * @pindex: pointer to the buffer index. May be NULL
0863  * @pb:     buffer structure passed from userspace to
0864  *      v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
0865  * @nonblocking: if true, this call will not sleep waiting for a buffer if no
0866  *       buffers ready for dequeuing are present. Normally the driver
0867  *       would be passing (file->f_flags & O_NONBLOCK) here.
0868  *
0869  * Videobuf2 core helper to implement VIDIOC_DQBUF() operation. It is called
0870  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
0871  *
0872  * This function:
0873  *
0874  * #) calls buf_finish callback in the driver (if provided), in which
0875  *    driver can perform any additional operations that may be required before
0876  *    returning the buffer to userspace, such as cache sync,
0877  * #) the buffer struct members are filled with relevant information for
0878  *    the userspace.
0879  *
0880  * Return: returns zero on success; an error code otherwise.
0881  */
0882 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
0883            bool nonblocking);
0884 
0885 /**
0886  * vb2_core_streamon() - Implements VB2 stream ON logic
0887  *
0888  * @q:      pointer to &struct vb2_queue with videobuf2 queue
0889  * @type:   type of the queue to be started.
0890  *      For V4L2, this is defined by &enum v4l2_buf_type type.
0891  *
0892  * Videobuf2 core helper to implement VIDIOC_STREAMON() operation. It is called
0893  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
0894  *
0895  * Return: returns zero on success; an error code otherwise.
0896  */
0897 int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
0898 
0899 /**
0900  * vb2_core_streamoff() - Implements VB2 stream OFF logic
0901  *
0902  * @q:      pointer to &struct vb2_queue with videobuf2 queue
0903  * @type:   type of the queue to be started.
0904  *      For V4L2, this is defined by &enum v4l2_buf_type type.
0905  *
0906  * Videobuf2 core helper to implement VIDIOC_STREAMOFF() operation. It is
0907  * called internally by VB2 by an API-specific handler, like
0908  * ``videobuf2-v4l2.h``.
0909  *
0910  * Return: returns zero on success; an error code otherwise.
0911  */
0912 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
0913 
0914 /**
0915  * vb2_core_expbuf() - Export a buffer as a file descriptor.
0916  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0917  * @fd:     pointer to the file descriptor associated with DMABUF
0918  *      (set by driver).
0919  * @type:   buffer type.
0920  * @index:  id number of the buffer.
0921  * @plane:  index of the plane to be exported, 0 for single plane queues
0922  * @flags:  file flags for newly created file, as defined at
0923  *      include/uapi/asm-generic/fcntl.h.
0924  *      Currently, the only used flag is %O_CLOEXEC.
0925  *      is supported, refer to manual of open syscall for more details.
0926  *
0927  *
0928  * Videobuf2 core helper to implement VIDIOC_EXPBUF() operation. It is called
0929  * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
0930  *
0931  * Return: returns zero on success; an error code otherwise.
0932  */
0933 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
0934         unsigned int index, unsigned int plane, unsigned int flags);
0935 
0936 /**
0937  * vb2_core_queue_init() - initialize a videobuf2 queue
0938  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0939  *      This structure should be allocated in driver
0940  *
0941  * The &vb2_queue structure should be allocated by the driver. The driver is
0942  * responsible of clearing it's content and setting initial values for some
0943  * required entries before calling this function.
0944  *
0945  * .. note::
0946  *
0947  *    The following fields at @q should be set before calling this function:
0948  *    &vb2_queue->ops, &vb2_queue->mem_ops, &vb2_queue->type.
0949  */
0950 int vb2_core_queue_init(struct vb2_queue *q);
0951 
0952 /**
0953  * vb2_core_queue_release() - stop streaming, release the queue and free memory
0954  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0955  *
0956  * This function stops streaming and performs necessary clean ups, including
0957  * freeing video buffer memory. The driver is responsible for freeing
0958  * the &struct vb2_queue itself.
0959  */
0960 void vb2_core_queue_release(struct vb2_queue *q);
0961 
0962 /**
0963  * vb2_queue_error() - signal a fatal error on the queue
0964  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0965  *
0966  * Flag that a fatal unrecoverable error has occurred and wake up all processes
0967  * waiting on the queue. Polling will now set %EPOLLERR and queuing and dequeuing
0968  * buffers will return %-EIO.
0969  *
0970  * The error flag will be cleared when canceling the queue, either from
0971  * vb2_streamoff() or vb2_queue_release(). Drivers should thus not call this
0972  * function before starting the stream, otherwise the error flag will remain set
0973  * until the queue is released when closing the device node.
0974  */
0975 void vb2_queue_error(struct vb2_queue *q);
0976 
0977 /**
0978  * vb2_mmap() - map video buffers into application address space.
0979  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0980  * @vma:    pointer to &struct vm_area_struct with the vma passed
0981  *      to the mmap file operation handler in the driver.
0982  *
0983  * Should be called from mmap file operation handler of a driver.
0984  * This function maps one plane of one of the available video buffers to
0985  * userspace. To map whole video memory allocated on reqbufs, this function
0986  * has to be called once per each plane per each buffer previously allocated.
0987  *
0988  * When the userspace application calls mmap, it passes to it an offset returned
0989  * to it earlier by the means of &v4l2_ioctl_ops->vidioc_querybuf handler.
0990  * That offset acts as a "cookie", which is then used to identify the plane
0991  * to be mapped.
0992  *
0993  * This function finds a plane with a matching offset and a mapping is performed
0994  * by the means of a provided memory operation.
0995  *
0996  * The return values from this function are intended to be directly returned
0997  * from the mmap handler in driver.
0998  */
0999 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
1000 
1001 #ifndef CONFIG_MMU
1002 /**
1003  * vb2_get_unmapped_area - map video buffers into application address space.
1004  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1005  * @addr:   memory address.
1006  * @len:    buffer size.
1007  * @pgoff:  page offset.
1008  * @flags:  memory flags.
1009  *
1010  * This function is used in noMMU platforms to propose address mapping
1011  * for a given buffer. It's intended to be used as a handler for the
1012  * &file_operations->get_unmapped_area operation.
1013  *
1014  * This is called by the mmap() syscall routines will call this
1015  * to get a proposed address for the mapping, when ``!CONFIG_MMU``.
1016  */
1017 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
1018                     unsigned long addr,
1019                     unsigned long len,
1020                     unsigned long pgoff,
1021                     unsigned long flags);
1022 #endif
1023 
1024 /**
1025  * vb2_core_poll() - implements poll syscall() logic.
1026  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1027  * @file:   &struct file argument passed to the poll
1028  *      file operation handler.
1029  * @wait:   &poll_table wait argument passed to the poll
1030  *      file operation handler.
1031  *
1032  * This function implements poll file operation handler for a driver.
1033  * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
1034  * be informed that the file descriptor of a video device is available for
1035  * reading.
1036  * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
1037  * will be reported as available for writing.
1038  *
1039  * The return values from this function are intended to be directly returned
1040  * from poll handler in driver.
1041  */
1042 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
1043                poll_table *wait);
1044 
1045 /**
1046  * vb2_read() - implements read() syscall logic.
1047  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1048  * @data:   pointed to target userspace buffer
1049  * @count:  number of bytes to read
1050  * @ppos:   file handle position tracking pointer
1051  * @nonblock:   mode selector (1 means blocking calls, 0 means nonblocking)
1052  */
1053 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1054         loff_t *ppos, int nonblock);
1055 /**
1056  * vb2_write() - implements write() syscall logic.
1057  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1058  * @data:   pointed to target userspace buffer
1059  * @count:  number of bytes to write
1060  * @ppos:   file handle position tracking pointer
1061  * @nonblock:   mode selector (1 means blocking calls, 0 means nonblocking)
1062  */
1063 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
1064         loff_t *ppos, int nonblock);
1065 
1066 /**
1067  * typedef vb2_thread_fnc - callback function for use with vb2_thread.
1068  *
1069  * @vb: pointer to struct &vb2_buffer.
1070  * @priv: pointer to a private data.
1071  *
1072  * This is called whenever a buffer is dequeued in the thread.
1073  */
1074 typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
1075 
1076 /**
1077  * vb2_thread_start() - start a thread for the given queue.
1078  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1079  * @fnc:    &vb2_thread_fnc callback function.
1080  * @priv:   priv pointer passed to the callback function.
1081  * @thread_name:the name of the thread. This will be prefixed with "vb2-".
1082  *
1083  * This starts a thread that will queue and dequeue until an error occurs
1084  * or vb2_thread_stop() is called.
1085  *
1086  * .. attention::
1087  *
1088  *   This function should not be used for anything else but the videobuf2-dvb
1089  *   support. If you think you have another good use-case for this, then please
1090  *   contact the linux-media mailing list first.
1091  */
1092 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
1093              const char *thread_name);
1094 
1095 /**
1096  * vb2_thread_stop() - stop the thread for the given queue.
1097  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1098  */
1099 int vb2_thread_stop(struct vb2_queue *q);
1100 
1101 /**
1102  * vb2_is_streaming() - return streaming status of the queue.
1103  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1104  */
1105 static inline bool vb2_is_streaming(struct vb2_queue *q)
1106 {
1107     return q->streaming;
1108 }
1109 
1110 /**
1111  * vb2_fileio_is_active() - return true if fileio is active.
1112  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1113  *
1114  * This returns true if read() or write() is used to stream the data
1115  * as opposed to stream I/O. This is almost never an important distinction,
1116  * except in rare cases. One such case is that using read() or write() to
1117  * stream a format using %V4L2_FIELD_ALTERNATE is not allowed since there
1118  * is no way you can pass the field information of each buffer to/from
1119  * userspace. A driver that supports this field format should check for
1120  * this in the &vb2_ops->queue_setup op and reject it if this function returns
1121  * true.
1122  */
1123 static inline bool vb2_fileio_is_active(struct vb2_queue *q)
1124 {
1125     return q->fileio;
1126 }
1127 
1128 /**
1129  * vb2_is_busy() - return busy status of the queue.
1130  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1131  *
1132  * This function checks if queue has any buffers allocated.
1133  */
1134 static inline bool vb2_is_busy(struct vb2_queue *q)
1135 {
1136     return (q->num_buffers > 0);
1137 }
1138 
1139 /**
1140  * vb2_get_drv_priv() - return driver private data associated with the queue.
1141  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1142  */
1143 static inline void *vb2_get_drv_priv(struct vb2_queue *q)
1144 {
1145     return q->drv_priv;
1146 }
1147 
1148 /**
1149  * vb2_set_plane_payload() - set bytesused for the plane @plane_no.
1150  * @vb:     pointer to &struct vb2_buffer to which the plane in
1151  *      question belongs to.
1152  * @plane_no:   plane number for which payload should be set.
1153  * @size:   payload in bytes.
1154  */
1155 static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
1156                  unsigned int plane_no, unsigned long size)
1157 {
1158     /*
1159      * size must never be larger than the buffer length, so
1160      * warn and clamp to the buffer length if that's the case.
1161      */
1162     if (plane_no < vb->num_planes) {
1163         if (WARN_ON_ONCE(size > vb->planes[plane_no].length))
1164             size = vb->planes[plane_no].length;
1165         vb->planes[plane_no].bytesused = size;
1166     }
1167 }
1168 
1169 /**
1170  * vb2_get_plane_payload() - get bytesused for the plane plane_no
1171  * @vb:     pointer to &struct vb2_buffer to which the plane in
1172  *      question belongs to.
1173  * @plane_no:   plane number for which payload should be set.
1174  */
1175 static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
1176                  unsigned int plane_no)
1177 {
1178     if (plane_no < vb->num_planes)
1179         return vb->planes[plane_no].bytesused;
1180     return 0;
1181 }
1182 
1183 /**
1184  * vb2_plane_size() - return plane size in bytes.
1185  * @vb:     pointer to &struct vb2_buffer to which the plane in
1186  *      question belongs to.
1187  * @plane_no:   plane number for which size should be returned.
1188  */
1189 static inline unsigned long
1190 vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
1191 {
1192     if (plane_no < vb->num_planes)
1193         return vb->planes[plane_no].length;
1194     return 0;
1195 }
1196 
1197 /**
1198  * vb2_start_streaming_called() - return streaming status of driver.
1199  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1200  */
1201 static inline bool vb2_start_streaming_called(struct vb2_queue *q)
1202 {
1203     return q->start_streaming_called;
1204 }
1205 
1206 /**
1207  * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue.
1208  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1209  */
1210 static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
1211 {
1212     q->last_buffer_dequeued = false;
1213 }
1214 
1215 /**
1216  * vb2_get_buffer() - get a buffer from a queue
1217  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1218  * @index:  buffer index
1219  *
1220  * This function obtains a buffer from a queue, by its index.
1221  * Keep in mind that there is no refcounting involved in this
1222  * operation, so the buffer lifetime should be taken into
1223  * consideration.
1224  */
1225 static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q,
1226                         unsigned int index)
1227 {
1228     if (index < q->num_buffers)
1229         return q->bufs[index];
1230     return NULL;
1231 }
1232 
1233 /*
1234  * The following functions are not part of the vb2 core API, but are useful
1235  * functions for videobuf2-*.
1236  */
1237 
1238 /**
1239  * vb2_buffer_in_use() - return true if the buffer is in use and
1240  * the queue cannot be freed (by the means of VIDIOC_REQBUFS(0)) call.
1241  *
1242  * @vb:     buffer for which plane size should be returned.
1243  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1244  */
1245 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
1246 
1247 /**
1248  * vb2_verify_memory_type() - Check whether the memory type and buffer type
1249  * passed to a buffer operation are compatible with the queue.
1250  *
1251  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
1252  * @memory: memory model, as defined by enum &vb2_memory.
1253  * @type:   private buffer type whose content is defined by the vb2-core
1254  *      caller. For example, for V4L2, it should match
1255  *      the types defined on enum &v4l2_buf_type.
1256  */
1257 int vb2_verify_memory_type(struct vb2_queue *q,
1258         enum vb2_memory memory, unsigned int type);
1259 
1260 /**
1261  * vb2_request_object_is_buffer() - return true if the object is a buffer
1262  *
1263  * @obj:    the request object.
1264  */
1265 bool vb2_request_object_is_buffer(struct media_request_object *obj);
1266 
1267 /**
1268  * vb2_request_buffer_cnt() - return the number of buffers in the request
1269  *
1270  * @req:    the request.
1271  */
1272 unsigned int vb2_request_buffer_cnt(struct media_request *req);
1273 
1274 #endif /* _MEDIA_VIDEOBUF2_CORE_H */