vidioc-expbuf.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. .. -*- coding: utf-8; mode: rst -*-
  2. .. _VIDIOC_EXPBUF:
  3. *******************
  4. ioctl VIDIOC_EXPBUF
  5. *******************
  6. Name
  7. ====
  8. VIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.
  9. Synopsis
  10. ========
  11. .. c:function:: int ioctl( int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp )
  12. :name: VIDIOC_EXPBUF
  13. Arguments
  14. =========
  15. ``fd``
  16. File descriptor returned by :ref:`open() <func-open>`.
  17. ``argp``
  18. Description
  19. ===========
  20. This ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
  21. method, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
  22. It can be used to export a buffer as a DMABUF file at any time after
  23. buffers have been allocated with the
  24. :ref:`VIDIOC_REQBUFS` ioctl.
  25. To export a buffer, applications fill struct
  26. :c:type:`v4l2_exportbuffer`. The ``type`` field is
  27. set to the same buffer type as was previously used with struct
  28. :c:type:`v4l2_requestbuffers` ``type``.
  29. Applications must also set the ``index`` field. Valid index numbers
  30. range from zero to the number of buffers allocated with
  31. :ref:`VIDIOC_REQBUFS` (struct
  32. :c:type:`v4l2_requestbuffers` ``count``) minus
  33. one. For the multi-planar API, applications set the ``plane`` field to
  34. the index of the plane to be exported. Valid planes range from zero to
  35. the maximal number of valid planes for the currently active format. For
  36. the single-planar API, applications must set ``plane`` to zero.
  37. Additional flags may be posted in the ``flags`` field. Refer to a manual
  38. for open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
  39. and O_RDWR are supported. All other fields must be set to zero. In the
  40. case of multi-planar API, every plane is exported separately using
  41. multiple :ref:`VIDIOC_EXPBUF` calls.
  42. After calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
  43. driver. This is a DMABUF file descriptor. The application may pass it to
  44. other DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
  45. for details about importing DMABUF files into V4L2 nodes. It is
  46. recommended to close a DMABUF file when it is no longer used to allow
  47. the associated memory to be reclaimed.
  48. Examples
  49. ========
  50. .. code-block:: c
  51. int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
  52. {
  53. struct v4l2_exportbuffer expbuf;
  54. memset(&expbuf, 0, sizeof(expbuf));
  55. expbuf.type = bt;
  56. expbuf.index = index;
  57. if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
  58. perror("VIDIOC_EXPBUF");
  59. return -1;
  60. }
  61. *dmafd = expbuf.fd;
  62. return 0;
  63. }
  64. .. code-block:: c
  65. int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
  66. int dmafd[], int n_planes)
  67. {
  68. int i;
  69. for (i = 0; i < n_planes; ++i) {
  70. struct v4l2_exportbuffer expbuf;
  71. memset(&expbuf, 0, sizeof(expbuf));
  72. expbuf.type = bt;
  73. expbuf.index = index;
  74. expbuf.plane = i;
  75. if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
  76. perror("VIDIOC_EXPBUF");
  77. while (i)
  78. close(dmafd[--i]);
  79. return -1;
  80. }
  81. dmafd[i] = expbuf.fd;
  82. }
  83. return 0;
  84. }
  85. .. c:type:: v4l2_exportbuffer
  86. .. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.7cm}|
  87. .. flat-table:: struct v4l2_exportbuffer
  88. :header-rows: 0
  89. :stub-columns: 0
  90. :widths: 1 1 2
  91. * - __u32
  92. - ``type``
  93. - Type of the buffer, same as struct
  94. :c:type:`v4l2_format` ``type`` or struct
  95. :c:type:`v4l2_requestbuffers` ``type``, set
  96. by the application. See :c:type:`v4l2_buf_type`
  97. * - __u32
  98. - ``index``
  99. - Number of the buffer, set by the application. This field is only
  100. used for :ref:`memory mapping <mmap>` I/O and can range from
  101. zero to the number of buffers allocated with the
  102. :ref:`VIDIOC_REQBUFS` and/or
  103. :ref:`VIDIOC_CREATE_BUFS` ioctls.
  104. * - __u32
  105. - ``plane``
  106. - Index of the plane to be exported when using the multi-planar API.
  107. Otherwise this value must be set to zero.
  108. * - __u32
  109. - ``flags``
  110. - Flags for the newly created file, currently only ``O_CLOEXEC``,
  111. ``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
  112. the manual of open() for more details.
  113. * - __s32
  114. - ``fd``
  115. - The DMABUF file descriptor associated with a buffer. Set by the
  116. driver.
  117. * - __u32
  118. - ``reserved[11]``
  119. - Reserved field for future use. Drivers and applications must set
  120. the array to zero.
  121. Return Value
  122. ============
  123. On success 0 is returned, on error -1 and the ``errno`` variable is set
  124. appropriately. The generic error codes are described at the
  125. :ref:`Generic Error Codes <gen-errors>` chapter.
  126. EINVAL
  127. A queue is not in MMAP mode or DMABUF exporting is not supported or
  128. ``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.