xref: /linux/Documentation/userspace-api/media/v4l/vidioc-reqbufs.rst (revision 965c1e0bfeb66888fb000540c1fc4e8b55533d9c)
1059b1c5bSMauro Carvalho Chehab.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2407e84cdSMauro Carvalho Chehab.. c:namespace:: V4L
354f38fcaSMauro Carvalho Chehab
454f38fcaSMauro Carvalho Chehab.. _VIDIOC_REQBUFS:
554f38fcaSMauro Carvalho Chehab
654f38fcaSMauro Carvalho Chehab********************
754f38fcaSMauro Carvalho Chehabioctl VIDIOC_REQBUFS
854f38fcaSMauro Carvalho Chehab********************
954f38fcaSMauro Carvalho Chehab
1054f38fcaSMauro Carvalho ChehabName
1154f38fcaSMauro Carvalho Chehab====
1254f38fcaSMauro Carvalho Chehab
1354f38fcaSMauro Carvalho ChehabVIDIOC_REQBUFS - Initiate Memory Mapping, User Pointer I/O or DMA buffer I/O
1454f38fcaSMauro Carvalho Chehab
1554f38fcaSMauro Carvalho ChehabSynopsis
1654f38fcaSMauro Carvalho Chehab========
1754f38fcaSMauro Carvalho Chehab
18407e84cdSMauro Carvalho Chehab.. c:macro:: VIDIOC_REQBUFS
1954f38fcaSMauro Carvalho Chehab
20407e84cdSMauro Carvalho Chehab``int ioctl(int fd, VIDIOC_REQBUFS, struct v4l2_requestbuffers *argp)``
2154f38fcaSMauro Carvalho Chehab
2254f38fcaSMauro Carvalho ChehabArguments
2354f38fcaSMauro Carvalho Chehab=========
2454f38fcaSMauro Carvalho Chehab
2554f38fcaSMauro Carvalho Chehab``fd``
26407e84cdSMauro Carvalho Chehab    File descriptor returned by :c: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.. c:type:: v4l2_requestbuffers
7354f38fcaSMauro Carvalho Chehab
74fea13a69SMauro Carvalho Chehab.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
7554f38fcaSMauro Carvalho Chehab
7654f38fcaSMauro Carvalho Chehab.. flat-table:: struct v4l2_requestbuffers
7754f38fcaSMauro Carvalho Chehab    :header-rows:  0
7854f38fcaSMauro Carvalho Chehab    :stub-columns: 0
7954f38fcaSMauro Carvalho Chehab    :widths:       1 1 2
8054f38fcaSMauro Carvalho Chehab
8154f38fcaSMauro Carvalho Chehab    * - __u32
8254f38fcaSMauro Carvalho Chehab      - ``count``
8354f38fcaSMauro Carvalho Chehab      - The number of buffers requested or granted.
8454f38fcaSMauro Carvalho Chehab    * - __u32
8554f38fcaSMauro Carvalho Chehab      - ``type``
8654f38fcaSMauro Carvalho Chehab      - Type of the stream or buffers, this is the same as the struct
8754f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_format` ``type`` field. See
8854f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_buf_type` for valid values.
8954f38fcaSMauro Carvalho Chehab    * - __u32
9054f38fcaSMauro Carvalho Chehab      - ``memory``
9154f38fcaSMauro Carvalho Chehab      - Applications set this field to ``V4L2_MEMORY_MMAP``,
9254f38fcaSMauro Carvalho Chehab	``V4L2_MEMORY_DMABUF`` or ``V4L2_MEMORY_USERPTR``. See
9354f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_memory`.
9454f38fcaSMauro Carvalho Chehab    * - __u32
9554f38fcaSMauro Carvalho Chehab      - ``capabilities``
9654f38fcaSMauro Carvalho Chehab      - Set by the driver. If 0, then the driver doesn't support
9754f38fcaSMauro Carvalho Chehab        capabilities. In that case all you know is that the driver is
9854f38fcaSMauro Carvalho Chehab	guaranteed to support ``V4L2_MEMORY_MMAP`` and *might* support
99fcab45adSHans Verkuil	other :c:type:`v4l2_memory` types. It will not support any other
10054f38fcaSMauro Carvalho Chehab	capabilities.
10154f38fcaSMauro Carvalho Chehab
10254f38fcaSMauro Carvalho Chehab	If you want to query the capabilities with a minimum of side-effects,
10354f38fcaSMauro Carvalho Chehab	then this can be called with ``count`` set to 0, ``memory`` set to
10454f38fcaSMauro Carvalho Chehab	``V4L2_MEMORY_MMAP`` and ``type`` set to the buffer type. This will
10554f38fcaSMauro Carvalho Chehab	free any previously allocated buffers, so this is typically something
10654f38fcaSMauro Carvalho Chehab	that will be done at the start of the application.
10754f38fcaSMauro Carvalho Chehab    * - __u32
10854f38fcaSMauro Carvalho Chehab      - ``reserved``\ [1]
109129134e5SSergey Senozhatsky      - A place holder for future extensions. Drivers and applications
110129134e5SSergey Senozhatsky	must set the array to zero.
11154f38fcaSMauro Carvalho Chehab
11254f38fcaSMauro Carvalho Chehab.. _v4l2-buf-capabilities:
11354f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-MMAP:
11454f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-USERPTR:
11554f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-DMABUF:
11654f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-REQUESTS:
11754f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-ORPHANED-BUFS:
11854f38fcaSMauro Carvalho Chehab.. _V4L2-BUF-CAP-SUPPORTS-M2M-HOLD-CAPTURE-BUF:
119ac53503eSSergey Senozhatsky.. _V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS:
12054f38fcaSMauro Carvalho Chehab
121fea13a69SMauro Carvalho Chehab.. raw:: latex
122fea13a69SMauro Carvalho Chehab
123fea13a69SMauro Carvalho Chehab   \footnotesize
124fea13a69SMauro Carvalho Chehab
125fea13a69SMauro Carvalho Chehab.. tabularcolumns:: |p{8.1cm}|p{2.2cm}|p{7.0cm}|
126fea13a69SMauro Carvalho Chehab
12754f38fcaSMauro Carvalho Chehab.. cssclass:: longtable
12854f38fcaSMauro Carvalho Chehab
12954f38fcaSMauro Carvalho Chehab.. flat-table:: V4L2 Buffer Capabilities Flags
13054f38fcaSMauro Carvalho Chehab    :header-rows:  0
13154f38fcaSMauro Carvalho Chehab    :stub-columns: 0
13254f38fcaSMauro Carvalho Chehab    :widths:       3 1 4
13354f38fcaSMauro Carvalho Chehab
13454f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP``
13554f38fcaSMauro Carvalho Chehab      - 0x00000001
13654f38fcaSMauro Carvalho Chehab      - This buffer type supports the ``V4L2_MEMORY_MMAP`` streaming mode.
13754f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_USERPTR``
13854f38fcaSMauro Carvalho Chehab      - 0x00000002
13954f38fcaSMauro Carvalho Chehab      - This buffer type supports the ``V4L2_MEMORY_USERPTR`` streaming mode.
14054f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_DMABUF``
14154f38fcaSMauro Carvalho Chehab      - 0x00000004
14254f38fcaSMauro Carvalho Chehab      - This buffer type supports the ``V4L2_MEMORY_DMABUF`` streaming mode.
14354f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_REQUESTS``
14454f38fcaSMauro Carvalho Chehab      - 0x00000008
14554f38fcaSMauro Carvalho Chehab      - This buffer type supports :ref:`requests <media-request-api>`.
14654f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS``
14754f38fcaSMauro Carvalho Chehab      - 0x00000010
14854f38fcaSMauro Carvalho Chehab      - The kernel allows calling :ref:`VIDIOC_REQBUFS` while buffers are still
14954f38fcaSMauro Carvalho Chehab        mapped or exported via DMABUF. These orphaned buffers will be freed
15054f38fcaSMauro Carvalho Chehab        when they are unmapped or when the exported DMABUF fds are closed.
15154f38fcaSMauro Carvalho Chehab    * - ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF``
15254f38fcaSMauro Carvalho Chehab      - 0x00000020
15354f38fcaSMauro Carvalho Chehab      - Only valid for stateless decoders. If set, then userspace can set the
15454f38fcaSMauro Carvalho Chehab        ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag to hold off on returning the
15554f38fcaSMauro Carvalho Chehab	capture buffer until the OUTPUT timestamp changes.
156ac53503eSSergey Senozhatsky    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS``
157ac53503eSSergey Senozhatsky      - 0x00000040
158ac53503eSSergey Senozhatsky      - This capability is set by the driver to indicate that the queue supports
159ac53503eSSergey Senozhatsky        cache and memory management hints. However, it's only valid when the
160ac53503eSSergey Senozhatsky        queue is used for :ref:`memory mapping <mmap>` streaming I/O. See
161*965c1e0bSSergey Senozhatsky        :ref:`V4L2_BUF_FLAG_NO_CACHE_INVALIDATE <V4L2-BUF-FLAG-NO-CACHE-INVALIDATE>`,
162*965c1e0bSSergey Senozhatsky        :ref:`V4L2_BUF_FLAG_NO_CACHE_CLEAN <V4L2-BUF-FLAG-NO-CACHE-CLEAN>` and
163*965c1e0bSSergey Senozhatsky        :ref:`V4L2_MEMORY_FLAG_NON_COHERENT <V4L2-MEMORY-FLAG-NON-COHERENT>`.
164ac53503eSSergey Senozhatsky
165fea13a69SMauro Carvalho Chehab.. raw:: latex
166fea13a69SMauro Carvalho Chehab
167fea13a69SMauro Carvalho Chehab   \normalsize
168fea13a69SMauro Carvalho Chehab
16954f38fcaSMauro Carvalho ChehabReturn Value
17054f38fcaSMauro Carvalho Chehab============
17154f38fcaSMauro Carvalho Chehab
17254f38fcaSMauro Carvalho ChehabOn success 0 is returned, on error -1 and the ``errno`` variable is set
17354f38fcaSMauro Carvalho Chehabappropriately. The generic error codes are described at the
17454f38fcaSMauro Carvalho Chehab:ref:`Generic Error Codes <gen-errors>` chapter.
17554f38fcaSMauro Carvalho Chehab
17654f38fcaSMauro Carvalho ChehabEINVAL
17754f38fcaSMauro Carvalho Chehab    The buffer type (``type`` field) or the requested I/O method
17854f38fcaSMauro Carvalho Chehab    (``memory``) is not supported.
179