18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _VIDIOC_EXPBUF:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci*******************
78c2ecf20Sopenharmony_ciioctl VIDIOC_EXPBUF
88c2ecf20Sopenharmony_ci*******************
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciName
118c2ecf20Sopenharmony_ci====
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciVIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciSynopsis
168c2ecf20Sopenharmony_ci========
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci.. c:macro:: VIDIOC_EXPBUF
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci``int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)``
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciArguments
238c2ecf20Sopenharmony_ci=========
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci``fd``
268c2ecf20Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci``argp``
298c2ecf20Sopenharmony_ci    Pointer to struct :c:type:`v4l2_exportbuffer`.
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciDescription
328c2ecf20Sopenharmony_ci===========
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciThis ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
358c2ecf20Sopenharmony_cimethod, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
368c2ecf20Sopenharmony_ciIt can be used to export a buffer as a DMABUF file at any time after
378c2ecf20Sopenharmony_cibuffers have been allocated with the
388c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS` ioctl.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciTo export a buffer, applications fill struct
418c2ecf20Sopenharmony_ci:c:type:`v4l2_exportbuffer`. The ``type`` field is
428c2ecf20Sopenharmony_ciset to the same buffer type as was previously used with struct
438c2ecf20Sopenharmony_ci:c:type:`v4l2_requestbuffers` ``type``.
448c2ecf20Sopenharmony_ciApplications must also set the ``index`` field. Valid index numbers
458c2ecf20Sopenharmony_cirange from zero to the number of buffers allocated with
468c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS` (struct
478c2ecf20Sopenharmony_ci:c:type:`v4l2_requestbuffers` ``count``) minus
488c2ecf20Sopenharmony_cione. For the multi-planar API, applications set the ``plane`` field to
498c2ecf20Sopenharmony_cithe index of the plane to be exported. Valid planes range from zero to
508c2ecf20Sopenharmony_cithe maximal number of valid planes for the currently active format. For
518c2ecf20Sopenharmony_cithe single-planar API, applications must set ``plane`` to zero.
528c2ecf20Sopenharmony_ciAdditional flags may be posted in the ``flags`` field. Refer to a manual
538c2ecf20Sopenharmony_cifor open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
548c2ecf20Sopenharmony_ciand O_RDWR are supported. All other fields must be set to zero. In the
558c2ecf20Sopenharmony_cicase of multi-planar API, every plane is exported separately using
568c2ecf20Sopenharmony_cimultiple :ref:`VIDIOC_EXPBUF` calls.
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciAfter calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
598c2ecf20Sopenharmony_cidriver. This is a DMABUF file descriptor. The application may pass it to
608c2ecf20Sopenharmony_ciother DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
618c2ecf20Sopenharmony_cifor details about importing DMABUF files into V4L2 nodes. It is
628c2ecf20Sopenharmony_cirecommended to close a DMABUF file when it is no longer used to allow
638c2ecf20Sopenharmony_cithe associated memory to be reclaimed.
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciExamples
668c2ecf20Sopenharmony_ci========
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci.. code-block:: c
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci    int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
718c2ecf20Sopenharmony_ci    {
728c2ecf20Sopenharmony_ci	struct v4l2_exportbuffer expbuf;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	memset(&expbuf, 0, sizeof(expbuf));
758c2ecf20Sopenharmony_ci	expbuf.type = bt;
768c2ecf20Sopenharmony_ci	expbuf.index = index;
778c2ecf20Sopenharmony_ci	if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
788c2ecf20Sopenharmony_ci	    perror("VIDIOC_EXPBUF");
798c2ecf20Sopenharmony_ci	    return -1;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	*dmafd = expbuf.fd;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return 0;
858c2ecf20Sopenharmony_ci    }
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci.. code-block:: c
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci    int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
908c2ecf20Sopenharmony_ci	int dmafd[], int n_planes)
918c2ecf20Sopenharmony_ci    {
928c2ecf20Sopenharmony_ci	int i;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	for (i = 0; i < n_planes; ++i) {
958c2ecf20Sopenharmony_ci	    struct v4l2_exportbuffer expbuf;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	    memset(&expbuf, 0, sizeof(expbuf));
988c2ecf20Sopenharmony_ci	    expbuf.type = bt;
998c2ecf20Sopenharmony_ci	    expbuf.index = index;
1008c2ecf20Sopenharmony_ci	    expbuf.plane = i;
1018c2ecf20Sopenharmony_ci	    if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
1028c2ecf20Sopenharmony_ci		perror("VIDIOC_EXPBUF");
1038c2ecf20Sopenharmony_ci		while (i)
1048c2ecf20Sopenharmony_ci		    close(dmafd[--i]);
1058c2ecf20Sopenharmony_ci		return -1;
1068c2ecf20Sopenharmony_ci	    }
1078c2ecf20Sopenharmony_ci	    dmafd[i] = expbuf.fd;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return 0;
1118c2ecf20Sopenharmony_ci    }
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci.. c:type:: v4l2_exportbuffer
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.7cm}|
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci.. flat-table:: struct v4l2_exportbuffer
1188c2ecf20Sopenharmony_ci    :header-rows:  0
1198c2ecf20Sopenharmony_ci    :stub-columns: 0
1208c2ecf20Sopenharmony_ci    :widths:       1 1 2
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci    * - __u32
1238c2ecf20Sopenharmony_ci      - ``type``
1248c2ecf20Sopenharmony_ci      - Type of the buffer, same as struct
1258c2ecf20Sopenharmony_ci	:c:type:`v4l2_format` ``type`` or struct
1268c2ecf20Sopenharmony_ci	:c:type:`v4l2_requestbuffers` ``type``, set
1278c2ecf20Sopenharmony_ci	by the application. See :c:type:`v4l2_buf_type`
1288c2ecf20Sopenharmony_ci    * - __u32
1298c2ecf20Sopenharmony_ci      - ``index``
1308c2ecf20Sopenharmony_ci      - Number of the buffer, set by the application. This field is only
1318c2ecf20Sopenharmony_ci	used for :ref:`memory mapping <mmap>` I/O and can range from
1328c2ecf20Sopenharmony_ci	zero to the number of buffers allocated with the
1338c2ecf20Sopenharmony_ci	:ref:`VIDIOC_REQBUFS` and/or
1348c2ecf20Sopenharmony_ci	:ref:`VIDIOC_CREATE_BUFS` ioctls.
1358c2ecf20Sopenharmony_ci    * - __u32
1368c2ecf20Sopenharmony_ci      - ``plane``
1378c2ecf20Sopenharmony_ci      - Index of the plane to be exported when using the multi-planar API.
1388c2ecf20Sopenharmony_ci	Otherwise this value must be set to zero.
1398c2ecf20Sopenharmony_ci    * - __u32
1408c2ecf20Sopenharmony_ci      - ``flags``
1418c2ecf20Sopenharmony_ci      - Flags for the newly created file, currently only ``O_CLOEXEC``,
1428c2ecf20Sopenharmony_ci	``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
1438c2ecf20Sopenharmony_ci	the manual of open() for more details.
1448c2ecf20Sopenharmony_ci    * - __s32
1458c2ecf20Sopenharmony_ci      - ``fd``
1468c2ecf20Sopenharmony_ci      - The DMABUF file descriptor associated with a buffer. Set by the
1478c2ecf20Sopenharmony_ci	driver.
1488c2ecf20Sopenharmony_ci    * - __u32
1498c2ecf20Sopenharmony_ci      - ``reserved[11]``
1508c2ecf20Sopenharmony_ci      - Reserved field for future use. Drivers and applications must set
1518c2ecf20Sopenharmony_ci	the array to zero.
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciReturn Value
1548c2ecf20Sopenharmony_ci============
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciOn success 0 is returned, on error -1 and the ``errno`` variable is set
1578c2ecf20Sopenharmony_ciappropriately. The generic error codes are described at the
1588c2ecf20Sopenharmony_ci:ref:`Generic Error Codes <gen-errors>` chapter.
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ciEINVAL
1618c2ecf20Sopenharmony_ci    A queue is not in MMAP mode or DMABUF exporting is not supported or
1628c2ecf20Sopenharmony_ci    ``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.
163