xref: /linux/Documentation/userspace-api/media/v4l/vidioc-reqbufs.rst (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2.. c:namespace:: V4L
3
4.. _VIDIOC_REQBUFS:
5
6********************
7ioctl VIDIOC_REQBUFS
8********************
9
10Name
11====
12
13VIDIOC_REQBUFS - Initiate Memory Mapping, User Pointer I/O or DMA buffer I/O
14
15Synopsis
16========
17
18.. c:macro:: VIDIOC_REQBUFS
19
20``int ioctl(int fd, VIDIOC_REQBUFS, struct v4l2_requestbuffers *argp)``
21
22Arguments
23=========
24
25``fd``
26    File descriptor returned by :c:func:`open()`.
27
28``argp``
29    Pointer to struct :c:type:`v4l2_requestbuffers`.
30
31Description
32===========
33
34This ioctl is used to initiate :ref:`memory mapped <mmap>`,
35:ref:`user pointer <userp>` or :ref:`DMABUF <dmabuf>` based I/O.
36Memory mapped buffers are located in device memory and must be allocated
37with this ioctl before they can be mapped into the application's address
38space. User buffers are allocated by applications themselves, and this
39ioctl is merely used to switch the driver into user pointer I/O mode and
40to setup some internal structures. Similarly, DMABUF buffers are
41allocated by applications through a device driver, and this ioctl only
42configures the driver into DMABUF I/O mode without performing any direct
43allocation.
44
45To allocate device buffers applications initialize all fields of the
46struct :c:type:`v4l2_requestbuffers` structure. They set the ``type``
47field to the respective stream or buffer type, the ``count`` field to
48the desired number of buffers, ``memory`` must be set to the requested
49I/O method and the ``reserved`` array must be zeroed. When the ioctl is
50called with a pointer to this structure the driver will attempt to
51allocate the requested number of buffers and it stores the actual number
52allocated in the ``count`` field. It can be smaller than the number
53requested, even zero, when the driver runs out of free memory. A larger
54number is also possible when the driver requires more buffers to
55function correctly. For example video output requires at least two
56buffers, one displayed and one filled by the application.
57
58When the I/O method is not supported the ioctl returns an ``EINVAL`` error
59code.
60
61Applications can call :ref:`VIDIOC_REQBUFS` again to change the number of
62buffers. Note that if any buffers are still mapped or exported via DMABUF,
63then :ref:`VIDIOC_REQBUFS` can only succeed if the
64``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` capability is set. Otherwise
65:ref:`VIDIOC_REQBUFS` will return the ``EBUSY`` error code.
66If ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` is set, then these buffers are
67orphaned and will be freed when they are unmapped or when the exported DMABUF
68fds are closed. A ``count`` value of zero frees or orphans all buffers, after
69aborting or finishing any DMA in progress, an implicit
70:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>`.
71
72.. c:type:: v4l2_requestbuffers
73
74.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
75
76.. flat-table:: struct v4l2_requestbuffers
77    :header-rows:  0
78    :stub-columns: 0
79    :widths:       1 1 2
80
81    * - __u32
82      - ``count``
83      - The number of buffers requested or granted.
84    * - __u32
85      - ``type``
86      - Type of the stream or buffers, this is the same as the struct
87	:c:type:`v4l2_format` ``type`` field. See
88	:c:type:`v4l2_buf_type` for valid values.
89    * - __u32
90      - ``memory``
91      - Applications set this field to ``V4L2_MEMORY_MMAP``,
92	``V4L2_MEMORY_DMABUF`` or ``V4L2_MEMORY_USERPTR``. See
93	:c:type:`v4l2_memory`.
94    * - __u32
95      - ``capabilities``
96      - Set by the driver. If 0, then the driver doesn't support
97        capabilities. In that case all you know is that the driver is
98	guaranteed to support ``V4L2_MEMORY_MMAP`` and *might* support
99	other :c:type:`v4l2_memory` types. It will not support any other
100	capabilities.
101
102	If you want to query the capabilities with a minimum of side-effects,
103	then this can be called with ``count`` set to 0, ``memory`` set to
104	``V4L2_MEMORY_MMAP`` and ``type`` set to the buffer type. This will
105	free any previously allocated buffers, so this is typically something
106	that will be done at the start of the application.
107    * - __u8
108      - ``flags``
109      - Specifies additional buffer management attributes.
110	See :ref:`memory-flags`.
111    * - __u8
112      - ``reserved``\ [3]
113      - Reserved for future extensions.
114
115.. _v4l2-buf-capabilities:
116.. _V4L2-BUF-CAP-SUPPORTS-MMAP:
117.. _V4L2-BUF-CAP-SUPPORTS-USERPTR:
118.. _V4L2-BUF-CAP-SUPPORTS-DMABUF:
119.. _V4L2-BUF-CAP-SUPPORTS-REQUESTS:
120.. _V4L2-BUF-CAP-SUPPORTS-ORPHANED-BUFS:
121.. _V4L2-BUF-CAP-SUPPORTS-M2M-HOLD-CAPTURE-BUF:
122.. _V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS:
123
124.. raw:: latex
125
126   \footnotesize
127
128.. tabularcolumns:: |p{8.1cm}|p{2.2cm}|p{7.0cm}|
129
130.. cssclass:: longtable
131
132.. flat-table:: V4L2 Buffer Capabilities Flags
133    :header-rows:  0
134    :stub-columns: 0
135    :widths:       3 1 4
136
137    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP``
138      - 0x00000001
139      - This buffer type supports the ``V4L2_MEMORY_MMAP`` streaming mode.
140    * - ``V4L2_BUF_CAP_SUPPORTS_USERPTR``
141      - 0x00000002
142      - This buffer type supports the ``V4L2_MEMORY_USERPTR`` streaming mode.
143    * - ``V4L2_BUF_CAP_SUPPORTS_DMABUF``
144      - 0x00000004
145      - This buffer type supports the ``V4L2_MEMORY_DMABUF`` streaming mode.
146    * - ``V4L2_BUF_CAP_SUPPORTS_REQUESTS``
147      - 0x00000008
148      - This buffer type supports :ref:`requests <media-request-api>`.
149    * - ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS``
150      - 0x00000010
151      - The kernel allows calling :ref:`VIDIOC_REQBUFS` while buffers are still
152        mapped or exported via DMABUF. These orphaned buffers will be freed
153        when they are unmapped or when the exported DMABUF fds are closed.
154    * - ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF``
155      - 0x00000020
156      - Only valid for stateless decoders. If set, then userspace can set the
157        ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF`` flag to hold off on returning the
158	capture buffer until the OUTPUT timestamp changes.
159    * - ``V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS``
160      - 0x00000040
161      - This capability is set by the driver to indicate that the queue supports
162        cache and memory management hints. However, it's only valid when the
163        queue is used for :ref:`memory mapping <mmap>` streaming I/O. See
164        :ref:`V4L2_BUF_FLAG_NO_CACHE_INVALIDATE <V4L2-BUF-FLAG-NO-CACHE-INVALIDATE>`,
165        :ref:`V4L2_BUF_FLAG_NO_CACHE_CLEAN <V4L2-BUF-FLAG-NO-CACHE-CLEAN>` and
166        :ref:`V4L2_MEMORY_FLAG_NON_COHERENT <V4L2-MEMORY-FLAG-NON-COHERENT>`.
167
168.. raw:: latex
169
170   \normalsize
171
172Return Value
173============
174
175On success 0 is returned, on error -1 and the ``errno`` variable is set
176appropriately. The generic error codes are described at the
177:ref:`Generic Error Codes <gen-errors>` chapter.
178
179EINVAL
180    The buffer type (``type`` field) or the requested I/O method
181    (``memory``) is not supported.
182