vidioc-reqbufs.rst 8.0 KB

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