xref: /openbmc/linux/Documentation/userspace-api/media/v4l/vidioc-reqbufs.rst (revision 059b1c5b15579b09c3787c3a3f843574233ab976)
1*059b1c5bSMauro Carvalho Chehab.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
254f38fcaSMauro Carvalho Chehab
354f38fcaSMauro Carvalho Chehab.. _VIDIOC_REQBUFS:
454f38fcaSMauro Carvalho Chehab
554f38fcaSMauro Carvalho Chehab********************
654f38fcaSMauro Carvalho Chehabioctl VIDIOC_REQBUFS
754f38fcaSMauro Carvalho Chehab********************
854f38fcaSMauro Carvalho Chehab
954f38fcaSMauro Carvalho ChehabName
1054f38fcaSMauro Carvalho Chehab====
1154f38fcaSMauro Carvalho Chehab
1254f38fcaSMauro Carvalho ChehabVIDIOC_REQBUFS - Initiate Memory Mapping, User Pointer I/O or DMA buffer I/O
1354f38fcaSMauro Carvalho Chehab
1454f38fcaSMauro Carvalho Chehab
1554f38fcaSMauro Carvalho ChehabSynopsis
1654f38fcaSMauro Carvalho Chehab========
1754f38fcaSMauro Carvalho Chehab
1854f38fcaSMauro Carvalho Chehab.. c:function:: int ioctl( int fd, VIDIOC_REQBUFS, struct v4l2_requestbuffers *argp )
1954f38fcaSMauro Carvalho Chehab    :name: VIDIOC_REQBUFS
2054f38fcaSMauro Carvalho Chehab
2154f38fcaSMauro Carvalho Chehab
2254f38fcaSMauro Carvalho ChehabArguments
2354f38fcaSMauro Carvalho Chehab=========
2454f38fcaSMauro Carvalho Chehab
2554f38fcaSMauro Carvalho Chehab``fd``
2654f38fcaSMauro Carvalho Chehab    File descriptor returned by :ref:`open() <func-open>`.
2754f38fcaSMauro Carvalho Chehab
2854f38fcaSMauro Carvalho Chehab``argp``
2954f38fcaSMauro Carvalho Chehab    Pointer to struct :c:type:`v4l2_requestbuffers`.
3054f38fcaSMauro Carvalho Chehab
3154f38fcaSMauro Carvalho ChehabDescription
3254f38fcaSMauro Carvalho Chehab===========
3354f38fcaSMauro Carvalho Chehab
3454f38fcaSMauro Carvalho ChehabThis ioctl is used to initiate :ref:`memory mapped <mmap>`,
3554f38fcaSMauro Carvalho Chehab:ref:`user pointer <userp>` or :ref:`DMABUF <dmabuf>` based I/O.
3654f38fcaSMauro Carvalho ChehabMemory mapped buffers are located in device memory and must be allocated
3754f38fcaSMauro Carvalho Chehabwith this ioctl before they can be mapped into the application's address
3854f38fcaSMauro Carvalho Chehabspace. User buffers are allocated by applications themselves, and this
3954f38fcaSMauro Carvalho Chehabioctl is merely used to switch the driver into user pointer I/O mode and
4054f38fcaSMauro Carvalho Chehabto setup some internal structures. Similarly, DMABUF buffers are
4154f38fcaSMauro Carvalho Chehaballocated by applications through a device driver, and this ioctl only
4254f38fcaSMauro Carvalho Chehabconfigures the driver into DMABUF I/O mode without performing any direct
4354f38fcaSMauro Carvalho Chehaballocation.
4454f38fcaSMauro Carvalho Chehab
4554f38fcaSMauro Carvalho ChehabTo allocate device buffers applications initialize all fields of the
4654f38fcaSMauro Carvalho Chehabstruct :c:type:`v4l2_requestbuffers` structure. They set the ``type``
4754f38fcaSMauro Carvalho Chehabfield to the respective stream or buffer type, the ``count`` field to
4854f38fcaSMauro Carvalho Chehabthe desired number of buffers, ``memory`` must be set to the requested
4954f38fcaSMauro Carvalho ChehabI/O method and the ``reserved`` array must be zeroed. When the ioctl is
5054f38fcaSMauro Carvalho Chehabcalled with a pointer to this structure the driver will attempt to
5154f38fcaSMauro Carvalho Chehaballocate the requested number of buffers and it stores the actual number
5254f38fcaSMauro Carvalho Chehaballocated in the ``count`` field. It can be smaller than the number
5354f38fcaSMauro Carvalho Chehabrequested, even zero, when the driver runs out of free memory. A larger
5454f38fcaSMauro Carvalho Chehabnumber is also possible when the driver requires more buffers to
5554f38fcaSMauro Carvalho Chehabfunction correctly. For example video output requires at least two
5654f38fcaSMauro Carvalho Chehabbuffers, one displayed and one filled by the application.
5754f38fcaSMauro Carvalho Chehab
5854f38fcaSMauro Carvalho ChehabWhen the I/O method is not supported the ioctl returns an ``EINVAL`` error
5954f38fcaSMauro Carvalho Chehabcode.
6054f38fcaSMauro Carvalho Chehab
6154f38fcaSMauro Carvalho ChehabApplications can call :ref:`VIDIOC_REQBUFS` again to change the number of
6254f38fcaSMauro Carvalho Chehabbuffers. Note that if any buffers are still mapped or exported via DMABUF,
6354f38fcaSMauro Carvalho Chehabthen :ref:`VIDIOC_REQBUFS` can only succeed if the
6454f38fcaSMauro Carvalho Chehab``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` capability is set. Otherwise
6554f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_REQBUFS` will return the ``EBUSY`` error code.
6654f38fcaSMauro Carvalho ChehabIf ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` is set, then these buffers are
6754f38fcaSMauro Carvalho Chehaborphaned and will be freed when they are unmapped or when the exported DMABUF
6854f38fcaSMauro Carvalho Chehabfds are closed. A ``count`` value of zero frees or orphans all buffers, after
6954f38fcaSMauro Carvalho Chehababorting or finishing any DMA in progress, an implicit
7054f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>`.
7154f38fcaSMauro Carvalho Chehab
7254f38fcaSMauro Carvalho Chehab
7354f38fcaSMauro Carvalho Chehab.. c:type:: v4l2_requestbuffers
7454f38fcaSMauro Carvalho Chehab
7554f38fcaSMauro Carvalho Chehab.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.7cm}|
7654f38fcaSMauro Carvalho Chehab
7754f38fcaSMauro Carvalho Chehab.. flat-table:: struct v4l2_requestbuffers
7854f38fcaSMauro Carvalho Chehab    :header-rows:  0
7954f38fcaSMauro Carvalho Chehab    :stub-columns: 0
8054f38fcaSMauro Carvalho Chehab    :widths:       1 1 2
8154f38fcaSMauro Carvalho Chehab
8254f38fcaSMauro Carvalho Chehab    * - __u32
8354f38fcaSMauro Carvalho Chehab      - ``count``
8454f38fcaSMauro Carvalho Chehab      - The number of buffers requested or granted.
8554f38fcaSMauro Carvalho Chehab    * - __u32
8654f38fcaSMauro Carvalho Chehab      - ``type``
8754f38fcaSMauro Carvalho Chehab      - Type of the stream or buffers, this is the same as the struct
8854f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_format` ``type`` field. See
8954f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_buf_type` for valid values.
9054f38fcaSMauro Carvalho Chehab    * - __u32
9154f38fcaSMauro Carvalho Chehab      - ``memory``
9254f38fcaSMauro Carvalho Chehab      - Applications set this field to ``V4L2_MEMORY_MMAP``,
9354f38fcaSMauro Carvalho Chehab	``V4L2_MEMORY_DMABUF`` or ``V4L2_MEMORY_USERPTR``. See
9454f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_memory`.
9554f38fcaSMauro Carvalho Chehab    * - __u32
9654f38fcaSMauro Carvalho Chehab      - ``capabilities``
9754f38fcaSMauro Carvalho Chehab      - Set by the driver. If 0, then the driver doesn't support
9854f38fcaSMauro Carvalho Chehab        capabilities. In that case all you know is that the driver is
9954f38fcaSMauro Carvalho Chehab	guaranteed to support ``V4L2_MEMORY_MMAP`` and *might* support
100fcab45adSHans Verkuil	other :c:type:`v4l2_memory` types. It will not support any other
10154f38fcaSMauro Carvalho Chehab	capabilities.
10254f38fcaSMauro Carvalho Chehab
10354f38fcaSMauro Carvalho Chehab	If you want to query the capabilities with a minimum of side-effects,
10454f38fcaSMauro Carvalho Chehab	then this can be called with ``count`` set to 0, ``memory`` set to
10554f38fcaSMauro Carvalho Chehab	``V4L2_MEMORY_MMAP`` and ``type`` set to the buffer type. This will
10654f38fcaSMauro Carvalho Chehab	free any previously allocated buffers, so this is typically something
10754f38fcaSMauro Carvalho Chehab	that will be done at the start of the application.
1081e0b2318SSergey Senozhatsky    * - union {
1091e0b2318SSergey Senozhatsky      - (anonymous)
1101e0b2318SSergey Senozhatsky    * - __u32
1111e0b2318SSergey Senozhatsky      - ``flags``
1121e0b2318SSergey Senozhatsky      - Specifies additional buffer management attributes.
1131e0b2318SSergey Senozhatsky	See :ref:`memory-flags`.
11454f38fcaSMauro Carvalho Chehab    * - __u32
11554f38fcaSMauro Carvalho Chehab      - ``reserved``\ [1]
1161e0b2318SSergey Senozhatsky      - Kept for backwards compatibility. Use ``flags`` instead.
1171e0b2318SSergey Senozhatsky    * - }
1181e0b2318SSergey Senozhatsky      -
11954f38fcaSMauro Carvalho Chehab
12054f38fcaSMauro Carvalho Chehab.. tabularcolumns:: |p{6.1cm}|p{2.2cm}|p{8.7cm}|
12154f38fcaSMauro Carvalho Chehab
12254f38fcaSMauro Carvalho Chehab.. _v4l2-buf-capabilities:
12354f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-MMAP:
12454f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-USERPTR:
12554f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-DMABUF:
12654f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-REQUESTS:
12754f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-ORPHANED-BUFS:
12854f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-M2M-HOLD-CAPTURE-BUF:
129ac53503eSSergey Senozhatsky.. _V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS:
13054f38fcaSMauro Carvalho Chehab
13154f38fcaSMauro Carvalho Chehab.. cssclass:: longtable
13254f38fcaSMauro Carvalho Chehab
13354f38fcaSMauro Carvalho Chehab.. flat-table:: V4L2 Buffer Capabilities Flags
13454f38fcaSMauro Carvalho Chehab    :header-rows:  0
13554f38fcaSMauro Carvalho Chehab    :stub-columns: 0
13654f38fcaSMauro Carvalho Chehab    :widths:       3 1 4
13754f38fcaSMauro Carvalho Chehab
13854f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP``
13954f38fcaSMauro Carvalho Chehab      - 0x00000001
14054f38fcaSMauro Carvalho Chehab      - This buffer type supports the ``V4L2_MEMORY_MMAP`` streaming mode.
14154f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_USERPTR``
14254f38fcaSMauro Carvalho Chehab      - 0x00000002
14354f38fcaSMauro Carvalho Chehab      - This buffer type supports the ``V4L2_MEMORY_USERPTR`` streaming mode.
14454f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_DMABUF``
14554f38fcaSMauro Carvalho Chehab      - 0x00000004
14654f38fcaSMauro Carvalho Chehab      - This buffer type supports the ``V4L2_MEMORY_DMABUF`` streaming mode.
14754f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_REQUESTS``
14854f38fcaSMauro Carvalho Chehab      - 0x00000008
14954f38fcaSMauro Carvalho Chehab      - This buffer type supports :ref:`requests <media-request-api>`.
15054f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS``
15154f38fcaSMauro Carvalho Chehab      - 0x00000010
15254f38fcaSMauro Carvalho Chehab      - The kernel allows calling :ref:`VIDIOC_REQBUFS` while buffers are still
15354f38fcaSMauro Carvalho Chehab        mapped or exported via DMABUF. These orphaned buffers will be freed
15454f38fcaSMauro Carvalho Chehab        when they are unmapped or when the exported DMABUF fds are closed.
15554f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF``
15654f38fcaSMauro Carvalho Chehab      - 0x00000020
15754f38fcaSMauro Carvalho Chehab      - Only valid for stateless decoders. If set, then userspace can set the
15854f38fcaSMauro Carvalho Chehab        ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag to hold off on returning the
15954f38fcaSMauro Carvalho Chehab	capture buffer until the OUTPUT timestamp changes.
160ac53503eSSergey Senozhatsky    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS``
161ac53503eSSergey Senozhatsky      - 0x00000040
162ac53503eSSergey Senozhatsky      - This capability is set by the driver to indicate that the queue supports
163ac53503eSSergey Senozhatsky        cache and memory management hints. However, it's only valid when the
164ac53503eSSergey Senozhatsky        queue is used for :ref:`memory mapping <mmap>` streaming I/O. See
165ac53503eSSergey Senozhatsky        :ref:`V4L2_FLAG_MEMORY_NON_CONSISTENT <V4L2-FLAG-MEMORY-NON-CONSISTENT>`,
166ac53503eSSergey Senozhatsky        :ref:`V4L2_BUF_FLAG_NO_CACHE_INVALIDATE <V4L2-BUF-FLAG-NO-CACHE-INVALIDATE>` and
167ac53503eSSergey Senozhatsky        :ref:`V4L2_BUF_FLAG_NO_CACHE_CLEAN <V4L2-BUF-FLAG-NO-CACHE-CLEAN>`.
168ac53503eSSergey Senozhatsky
16954f38fcaSMauro Carvalho Chehab
17054f38fcaSMauro Carvalho ChehabReturn Value
17154f38fcaSMauro Carvalho Chehab============
17254f38fcaSMauro Carvalho Chehab
17354f38fcaSMauro Carvalho ChehabOn success 0 is returned, on error -1 and the ``errno`` variable is set
17454f38fcaSMauro Carvalho Chehabappropriately. The generic error codes are described at the
17554f38fcaSMauro Carvalho Chehab:ref:`Generic Error Codes <gen-errors>` chapter.
17654f38fcaSMauro Carvalho Chehab
17754f38fcaSMauro Carvalho ChehabEINVAL
17854f38fcaSMauro Carvalho Chehab    The buffer type (``type`` field) or the requested I/O method
17954f38fcaSMauro Carvalho Chehab    (``memory``) is not supported.
180