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_EXPBUF:
554f38fcaSMauro Carvalho Chehab
654f38fcaSMauro Carvalho Chehab*******************
754f38fcaSMauro Carvalho Chehabioctl VIDIOC_EXPBUF
854f38fcaSMauro Carvalho Chehab*******************
954f38fcaSMauro Carvalho Chehab
1054f38fcaSMauro Carvalho ChehabName
1154f38fcaSMauro Carvalho Chehab====
1254f38fcaSMauro Carvalho Chehab
1354f38fcaSMauro Carvalho ChehabVIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.
1454f38fcaSMauro Carvalho Chehab
1554f38fcaSMauro Carvalho ChehabSynopsis
1654f38fcaSMauro Carvalho Chehab========
1754f38fcaSMauro Carvalho Chehab
18407e84cdSMauro Carvalho Chehab.. c:macro:: VIDIOC_EXPBUF
1954f38fcaSMauro Carvalho Chehab
20407e84cdSMauro Carvalho Chehab``int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *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_exportbuffer`.
3054f38fcaSMauro Carvalho Chehab
3154f38fcaSMauro Carvalho ChehabDescription
3254f38fcaSMauro Carvalho Chehab===========
3354f38fcaSMauro Carvalho Chehab
3454f38fcaSMauro Carvalho ChehabThis ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
3554f38fcaSMauro Carvalho Chehabmethod, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
3654f38fcaSMauro Carvalho ChehabIt can be used to export a buffer as a DMABUF file at any time after
3754f38fcaSMauro Carvalho Chehabbuffers have been allocated with the
3854f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_REQBUFS` ioctl.
3954f38fcaSMauro Carvalho Chehab
4054f38fcaSMauro Carvalho ChehabTo export a buffer, applications fill struct
4154f38fcaSMauro Carvalho Chehab:c:type:`v4l2_exportbuffer`. The ``type`` field is
4254f38fcaSMauro Carvalho Chehabset to the same buffer type as was previously used with struct
4354f38fcaSMauro Carvalho Chehab:c:type:`v4l2_requestbuffers` ``type``.
4454f38fcaSMauro Carvalho ChehabApplications must also set the ``index`` field. Valid index numbers
4554f38fcaSMauro Carvalho Chehabrange from zero to the number of buffers allocated with
4654f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_REQBUFS` (struct
4754f38fcaSMauro Carvalho Chehab:c:type:`v4l2_requestbuffers` ``count``) minus
4854f38fcaSMauro Carvalho Chehabone. For the multi-planar API, applications set the ``plane`` field to
4954f38fcaSMauro Carvalho Chehabthe index of the plane to be exported. Valid planes range from zero to
5054f38fcaSMauro Carvalho Chehabthe maximal number of valid planes for the currently active format. For
5154f38fcaSMauro Carvalho Chehabthe single-planar API, applications must set ``plane`` to zero.
5254f38fcaSMauro Carvalho ChehabAdditional flags may be posted in the ``flags`` field. Refer to a manual
5354f38fcaSMauro Carvalho Chehabfor open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
5454f38fcaSMauro Carvalho Chehaband O_RDWR are supported. All other fields must be set to zero. In the
5554f38fcaSMauro Carvalho Chehabcase of multi-planar API, every plane is exported separately using
5654f38fcaSMauro Carvalho Chehabmultiple :ref:`VIDIOC_EXPBUF` calls.
5754f38fcaSMauro Carvalho Chehab
5854f38fcaSMauro Carvalho ChehabAfter calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
5954f38fcaSMauro Carvalho Chehabdriver. This is a DMABUF file descriptor. The application may pass it to
6054f38fcaSMauro Carvalho Chehabother DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
6154f38fcaSMauro Carvalho Chehabfor details about importing DMABUF files into V4L2 nodes. It is
6254f38fcaSMauro Carvalho Chehabrecommended to close a DMABUF file when it is no longer used to allow
6354f38fcaSMauro Carvalho Chehabthe associated memory to be reclaimed.
6454f38fcaSMauro Carvalho Chehab
6554f38fcaSMauro Carvalho ChehabExamples
6654f38fcaSMauro Carvalho Chehab========
6754f38fcaSMauro Carvalho Chehab
6854f38fcaSMauro Carvalho Chehab.. code-block:: c
6954f38fcaSMauro Carvalho Chehab
7054f38fcaSMauro Carvalho Chehab    int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
7154f38fcaSMauro Carvalho Chehab    {
7254f38fcaSMauro Carvalho Chehab	struct v4l2_exportbuffer expbuf;
7354f38fcaSMauro Carvalho Chehab
7454f38fcaSMauro Carvalho Chehab	memset(&expbuf, 0, sizeof(expbuf));
7554f38fcaSMauro Carvalho Chehab	expbuf.type = bt;
7654f38fcaSMauro Carvalho Chehab	expbuf.index = index;
7754f38fcaSMauro Carvalho Chehab	if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
7854f38fcaSMauro Carvalho Chehab	    perror("VIDIOC_EXPBUF");
7954f38fcaSMauro Carvalho Chehab	    return -1;
8054f38fcaSMauro Carvalho Chehab	}
8154f38fcaSMauro Carvalho Chehab
8254f38fcaSMauro Carvalho Chehab	*dmafd = expbuf.fd;
8354f38fcaSMauro Carvalho Chehab
8454f38fcaSMauro Carvalho Chehab	return 0;
8554f38fcaSMauro Carvalho Chehab    }
8654f38fcaSMauro Carvalho Chehab
8754f38fcaSMauro Carvalho Chehab.. code-block:: c
8854f38fcaSMauro Carvalho Chehab
8954f38fcaSMauro Carvalho Chehab    int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
9054f38fcaSMauro Carvalho Chehab	int dmafd[], int n_planes)
9154f38fcaSMauro Carvalho Chehab    {
9254f38fcaSMauro Carvalho Chehab	int i;
9354f38fcaSMauro Carvalho Chehab
9454f38fcaSMauro Carvalho Chehab	for (i = 0; i < n_planes; ++i) {
9554f38fcaSMauro Carvalho Chehab	    struct v4l2_exportbuffer expbuf;
9654f38fcaSMauro Carvalho Chehab
9754f38fcaSMauro Carvalho Chehab	    memset(&expbuf, 0, sizeof(expbuf));
9854f38fcaSMauro Carvalho Chehab	    expbuf.type = bt;
9954f38fcaSMauro Carvalho Chehab	    expbuf.index = index;
10054f38fcaSMauro Carvalho Chehab	    expbuf.plane = i;
10154f38fcaSMauro Carvalho Chehab	    if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
10254f38fcaSMauro Carvalho Chehab		perror("VIDIOC_EXPBUF");
10354f38fcaSMauro Carvalho Chehab		while (i)
10454f38fcaSMauro Carvalho Chehab		    close(dmafd[--i]);
10554f38fcaSMauro Carvalho Chehab		return -1;
10654f38fcaSMauro Carvalho Chehab	    }
10754f38fcaSMauro Carvalho Chehab	    dmafd[i] = expbuf.fd;
10854f38fcaSMauro Carvalho Chehab	}
10954f38fcaSMauro Carvalho Chehab
11054f38fcaSMauro Carvalho Chehab	return 0;
11154f38fcaSMauro Carvalho Chehab    }
11254f38fcaSMauro Carvalho Chehab
11354f38fcaSMauro Carvalho Chehab.. c:type:: v4l2_exportbuffer
11454f38fcaSMauro Carvalho Chehab
115*fea13a69SMauro Carvalho Chehab.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
11654f38fcaSMauro Carvalho Chehab
11754f38fcaSMauro Carvalho Chehab.. flat-table:: struct v4l2_exportbuffer
11854f38fcaSMauro Carvalho Chehab    :header-rows:  0
11954f38fcaSMauro Carvalho Chehab    :stub-columns: 0
12054f38fcaSMauro Carvalho Chehab    :widths:       1 1 2
12154f38fcaSMauro Carvalho Chehab
12254f38fcaSMauro Carvalho Chehab    * - __u32
12354f38fcaSMauro Carvalho Chehab      - ``type``
12454f38fcaSMauro Carvalho Chehab      - Type of the buffer, same as struct
12554f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_format` ``type`` or struct
12654f38fcaSMauro Carvalho Chehab	:c:type:`v4l2_requestbuffers` ``type``, set
12754f38fcaSMauro Carvalho Chehab	by the application. See :c:type:`v4l2_buf_type`
12854f38fcaSMauro Carvalho Chehab    * - __u32
12954f38fcaSMauro Carvalho Chehab      - ``index``
13054f38fcaSMauro Carvalho Chehab      - Number of the buffer, set by the application. This field is only
13154f38fcaSMauro Carvalho Chehab	used for :ref:`memory mapping <mmap>` I/O and can range from
13254f38fcaSMauro Carvalho Chehab	zero to the number of buffers allocated with the
13354f38fcaSMauro Carvalho Chehab	:ref:`VIDIOC_REQBUFS` and/or
13454f38fcaSMauro Carvalho Chehab	:ref:`VIDIOC_CREATE_BUFS` ioctls.
13554f38fcaSMauro Carvalho Chehab    * - __u32
13654f38fcaSMauro Carvalho Chehab      - ``plane``
13754f38fcaSMauro Carvalho Chehab      - Index of the plane to be exported when using the multi-planar API.
13854f38fcaSMauro Carvalho Chehab	Otherwise this value must be set to zero.
13954f38fcaSMauro Carvalho Chehab    * - __u32
14054f38fcaSMauro Carvalho Chehab      - ``flags``
14154f38fcaSMauro Carvalho Chehab      - Flags for the newly created file, currently only ``O_CLOEXEC``,
14254f38fcaSMauro Carvalho Chehab	``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
14354f38fcaSMauro Carvalho Chehab	the manual of open() for more details.
14454f38fcaSMauro Carvalho Chehab    * - __s32
14554f38fcaSMauro Carvalho Chehab      - ``fd``
14654f38fcaSMauro Carvalho Chehab      - The DMABUF file descriptor associated with a buffer. Set by the
14754f38fcaSMauro Carvalho Chehab	driver.
14854f38fcaSMauro Carvalho Chehab    * - __u32
14954f38fcaSMauro Carvalho Chehab      - ``reserved[11]``
15054f38fcaSMauro Carvalho Chehab      - Reserved field for future use. Drivers and applications must set
15154f38fcaSMauro Carvalho Chehab	the array to zero.
15254f38fcaSMauro Carvalho Chehab
15354f38fcaSMauro Carvalho ChehabReturn Value
15454f38fcaSMauro Carvalho Chehab============
15554f38fcaSMauro Carvalho Chehab
15654f38fcaSMauro Carvalho ChehabOn success 0 is returned, on error -1 and the ``errno`` variable is set
15754f38fcaSMauro Carvalho Chehabappropriately. The generic error codes are described at the
15854f38fcaSMauro Carvalho Chehab:ref:`Generic Error Codes <gen-errors>` chapter.
15954f38fcaSMauro Carvalho Chehab
16054f38fcaSMauro Carvalho ChehabEINVAL
16154f38fcaSMauro Carvalho Chehab    A queue is not in MMAP mode or DMABUF exporting is not supported or
16254f38fcaSMauro Carvalho Chehab    ``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.
163