162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _VIDIOC_REQBUFS:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci********************
762306a36Sopenharmony_ciioctl VIDIOC_REQBUFS
862306a36Sopenharmony_ci********************
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciName
1162306a36Sopenharmony_ci====
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciVIDIOC_REQBUFS - Initiate Memory Mapping, User Pointer I/O or DMA buffer I/O
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciSynopsis
1662306a36Sopenharmony_ci========
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci.. c:macro:: VIDIOC_REQBUFS
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci``int ioctl(int fd, VIDIOC_REQBUFS, struct v4l2_requestbuffers *argp)``
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciArguments
2362306a36Sopenharmony_ci=========
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci``fd``
2662306a36Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci``argp``
2962306a36Sopenharmony_ci    Pointer to struct :c:type:`v4l2_requestbuffers`.
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciDescription
3262306a36Sopenharmony_ci===========
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciThis ioctl is used to initiate :ref:`memory mapped <mmap>`,
3562306a36Sopenharmony_ci:ref:`user pointer <userp>` or :ref:`DMABUF <dmabuf>` based I/O.
3662306a36Sopenharmony_ciMemory mapped buffers are located in device memory and must be allocated
3762306a36Sopenharmony_ciwith this ioctl before they can be mapped into the application's address
3862306a36Sopenharmony_cispace. User buffers are allocated by applications themselves, and this
3962306a36Sopenharmony_ciioctl is merely used to switch the driver into user pointer I/O mode and
4062306a36Sopenharmony_cito setup some internal structures. Similarly, DMABUF buffers are
4162306a36Sopenharmony_ciallocated by applications through a device driver, and this ioctl only
4262306a36Sopenharmony_ciconfigures the driver into DMABUF I/O mode without performing any direct
4362306a36Sopenharmony_ciallocation.
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciTo allocate device buffers applications initialize all fields of the
4662306a36Sopenharmony_cistruct :c:type:`v4l2_requestbuffers` structure. They set the ``type``
4762306a36Sopenharmony_cifield to the respective stream or buffer type, the ``count`` field to
4862306a36Sopenharmony_cithe desired number of buffers, ``memory`` must be set to the requested
4962306a36Sopenharmony_ciI/O method and the ``reserved`` array must be zeroed. When the ioctl is
5062306a36Sopenharmony_cicalled with a pointer to this structure the driver will attempt to
5162306a36Sopenharmony_ciallocate the requested number of buffers and it stores the actual number
5262306a36Sopenharmony_ciallocated in the ``count`` field. It can be smaller than the number
5362306a36Sopenharmony_cirequested, even zero, when the driver runs out of free memory. A larger
5462306a36Sopenharmony_cinumber is also possible when the driver requires more buffers to
5562306a36Sopenharmony_cifunction correctly. For example video output requires at least two
5662306a36Sopenharmony_cibuffers, one displayed and one filled by the application.
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciWhen the I/O method is not supported the ioctl returns an ``EINVAL`` error
5962306a36Sopenharmony_cicode.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciApplications can call :ref:`VIDIOC_REQBUFS` again to change the number of
6262306a36Sopenharmony_cibuffers. Note that if any buffers are still mapped or exported via DMABUF,
6362306a36Sopenharmony_cithen :ref:`VIDIOC_REQBUFS` can only succeed if the
6462306a36Sopenharmony_ci``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` capability is set. Otherwise
6562306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS` will return the ``EBUSY`` error code.
6662306a36Sopenharmony_ciIf ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` is set, then these buffers are
6762306a36Sopenharmony_ciorphaned and will be freed when they are unmapped or when the exported DMABUF
6862306a36Sopenharmony_cifds are closed. A ``count`` value of zero frees or orphans all buffers, after
6962306a36Sopenharmony_ciaborting or finishing any DMA in progress, an implicit
7062306a36Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>`.
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci.. c:type:: v4l2_requestbuffers
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci.. flat-table:: struct v4l2_requestbuffers
7762306a36Sopenharmony_ci    :header-rows:  0
7862306a36Sopenharmony_ci    :stub-columns: 0
7962306a36Sopenharmony_ci    :widths:       1 1 2
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci    * - __u32
8262306a36Sopenharmony_ci      - ``count``
8362306a36Sopenharmony_ci      - The number of buffers requested or granted.
8462306a36Sopenharmony_ci    * - __u32
8562306a36Sopenharmony_ci      - ``type``
8662306a36Sopenharmony_ci      - Type of the stream or buffers, this is the same as the struct
8762306a36Sopenharmony_ci	:c:type:`v4l2_format` ``type`` field. See
8862306a36Sopenharmony_ci	:c:type:`v4l2_buf_type` for valid values.
8962306a36Sopenharmony_ci    * - __u32
9062306a36Sopenharmony_ci      - ``memory``
9162306a36Sopenharmony_ci      - Applications set this field to ``V4L2_MEMORY_MMAP``,
9262306a36Sopenharmony_ci	``V4L2_MEMORY_DMABUF`` or ``V4L2_MEMORY_USERPTR``. See
9362306a36Sopenharmony_ci	:c:type:`v4l2_memory`.
9462306a36Sopenharmony_ci    * - __u32
9562306a36Sopenharmony_ci      - ``capabilities``
9662306a36Sopenharmony_ci      - Set by the driver. If 0, then the driver doesn't support
9762306a36Sopenharmony_ci        capabilities. In that case all you know is that the driver is
9862306a36Sopenharmony_ci	guaranteed to support ``V4L2_MEMORY_MMAP`` and *might* support
9962306a36Sopenharmony_ci	other :c:type:`v4l2_memory` types. It will not support any other
10062306a36Sopenharmony_ci	capabilities.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	If you want to query the capabilities with a minimum of side-effects,
10362306a36Sopenharmony_ci	then this can be called with ``count`` set to 0, ``memory`` set to
10462306a36Sopenharmony_ci	``V4L2_MEMORY_MMAP`` and ``type`` set to the buffer type. This will
10562306a36Sopenharmony_ci	free any previously allocated buffers, so this is typically something
10662306a36Sopenharmony_ci	that will be done at the start of the application.
10762306a36Sopenharmony_ci    * - __u8
10862306a36Sopenharmony_ci      - ``flags``
10962306a36Sopenharmony_ci      - Specifies additional buffer management attributes.
11062306a36Sopenharmony_ci	See :ref:`memory-flags`.
11162306a36Sopenharmony_ci    * - __u8
11262306a36Sopenharmony_ci      - ``reserved``\ [3]
11362306a36Sopenharmony_ci      - Reserved for future extensions.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci.. _v4l2-buf-capabilities:
11662306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-MMAP:
11762306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-USERPTR:
11862306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-DMABUF:
11962306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-REQUESTS:
12062306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-ORPHANED-BUFS:
12162306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-M2M-HOLD-CAPTURE-BUF:
12262306a36Sopenharmony_ci.. _V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS:
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci.. raw:: latex
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci   \footnotesize
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci.. tabularcolumns:: |p{8.1cm}|p{2.2cm}|p{7.0cm}|
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci.. cssclass:: longtable
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci.. flat-table:: V4L2 Buffer Capabilities Flags
13362306a36Sopenharmony_ci    :header-rows:  0
13462306a36Sopenharmony_ci    :stub-columns: 0
13562306a36Sopenharmony_ci    :widths:       3 1 4
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP``
13862306a36Sopenharmony_ci      - 0x00000001
13962306a36Sopenharmony_ci      - This buffer type supports the ``V4L2_MEMORY_MMAP`` streaming mode.
14062306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_USERPTR``
14162306a36Sopenharmony_ci      - 0x00000002
14262306a36Sopenharmony_ci      - This buffer type supports the ``V4L2_MEMORY_USERPTR`` streaming mode.
14362306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_DMABUF``
14462306a36Sopenharmony_ci      - 0x00000004
14562306a36Sopenharmony_ci      - This buffer type supports the ``V4L2_MEMORY_DMABUF`` streaming mode.
14662306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_REQUESTS``
14762306a36Sopenharmony_ci      - 0x00000008
14862306a36Sopenharmony_ci      - This buffer type supports :ref:`requests <media-request-api>`.
14962306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS``
15062306a36Sopenharmony_ci      - 0x00000010
15162306a36Sopenharmony_ci      - The kernel allows calling :ref:`VIDIOC_REQBUFS` while buffers are still
15262306a36Sopenharmony_ci        mapped or exported via DMABUF. These orphaned buffers will be freed
15362306a36Sopenharmony_ci        when they are unmapped or when the exported DMABUF fds are closed.
15462306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF``
15562306a36Sopenharmony_ci      - 0x00000020
15662306a36Sopenharmony_ci      - Only valid for stateless decoders. If set, then userspace can set the
15762306a36Sopenharmony_ci        ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag to hold off on returning the
15862306a36Sopenharmony_ci	capture buffer until the OUTPUT timestamp changes.
15962306a36Sopenharmony_ci    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS``
16062306a36Sopenharmony_ci      - 0x00000040
16162306a36Sopenharmony_ci      - This capability is set by the driver to indicate that the queue supports
16262306a36Sopenharmony_ci        cache and memory management hints. However, it's only valid when the
16362306a36Sopenharmony_ci        queue is used for :ref:`memory mapping <mmap>` streaming I/O. See
16462306a36Sopenharmony_ci        :ref:`V4L2_BUF_FLAG_NO_CACHE_INVALIDATE <V4L2-BUF-FLAG-NO-CACHE-INVALIDATE>`,
16562306a36Sopenharmony_ci        :ref:`V4L2_BUF_FLAG_NO_CACHE_CLEAN <V4L2-BUF-FLAG-NO-CACHE-CLEAN>` and
16662306a36Sopenharmony_ci        :ref:`V4L2_MEMORY_FLAG_NON_COHERENT <V4L2-MEMORY-FLAG-NON-COHERENT>`.
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci.. raw:: latex
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci   \normalsize
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciReturn Value
17362306a36Sopenharmony_ci============
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciOn success 0 is returned, on error -1 and the ``errno`` variable is set
17662306a36Sopenharmony_ciappropriately. The generic error codes are described at the
17762306a36Sopenharmony_ci:ref:`Generic Error Codes <gen-errors>` chapter.
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciEINVAL
18062306a36Sopenharmony_ci    The buffer type (``type`` field) or the requested I/O method
18162306a36Sopenharmony_ci    (``memory``) is not supported.
182