18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _dmabuf:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci************************************
78c2ecf20Sopenharmony_ciStreaming I/O (DMA buffer importing)
88c2ecf20Sopenharmony_ci************************************
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciThe DMABUF framework provides a generic method for sharing buffers
118c2ecf20Sopenharmony_cibetween multiple devices. Device drivers that support DMABUF can export
128c2ecf20Sopenharmony_cia DMA buffer to userspace as a file descriptor (known as the exporter
138c2ecf20Sopenharmony_cirole), import a DMA buffer from userspace using a file descriptor
148c2ecf20Sopenharmony_cipreviously exported for a different or the same device (known as the
158c2ecf20Sopenharmony_ciimporter role), or both. This section describes the DMABUF importer role
168c2ecf20Sopenharmony_ciAPI in V4L2.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciRefer to :ref:`DMABUF exporting <VIDIOC_EXPBUF>` for details about
198c2ecf20Sopenharmony_ciexporting V4L2 buffers as DMABUF file descriptors.
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciInput and output devices support the streaming I/O method when the
228c2ecf20Sopenharmony_ci``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct
238c2ecf20Sopenharmony_ci:c:type:`v4l2_capability` returned by the
248c2ecf20Sopenharmony_ci:ref:`VIDIOC_QUERYCAP <VIDIOC_QUERYCAP>` ioctl is set. Whether
258c2ecf20Sopenharmony_ciimporting DMA buffers through DMABUF file descriptors is supported is
268c2ecf20Sopenharmony_cidetermined by calling the :ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`
278c2ecf20Sopenharmony_ciioctl with the memory type set to ``V4L2_MEMORY_DMABUF``.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciThis I/O method is dedicated to sharing DMA buffers between different
308c2ecf20Sopenharmony_cidevices, which may be V4L devices or other video-related devices (e.g.
318c2ecf20Sopenharmony_ciDRM). Buffers (planes) are allocated by a driver on behalf of an
328c2ecf20Sopenharmony_ciapplication. Next, these buffers are exported to the application as file
338c2ecf20Sopenharmony_cidescriptors using an API which is specific for an allocator driver. Only
348c2ecf20Sopenharmony_cisuch file descriptor are exchanged. The descriptors and meta-information
358c2ecf20Sopenharmony_ciare passed in struct :c:type:`v4l2_buffer` (or in struct
368c2ecf20Sopenharmony_ci:c:type:`v4l2_plane` in the multi-planar API case). The
378c2ecf20Sopenharmony_cidriver must be switched into DMABUF I/O mode by calling the
388c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>` with the desired buffer type.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciExample: Initiating streaming I/O with DMABUF file descriptors
418c2ecf20Sopenharmony_ci==============================================================
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci.. code-block:: c
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci    struct v4l2_requestbuffers reqbuf;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci    memset(&reqbuf, 0, sizeof (reqbuf));
488c2ecf20Sopenharmony_ci    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
498c2ecf20Sopenharmony_ci    reqbuf.memory = V4L2_MEMORY_DMABUF;
508c2ecf20Sopenharmony_ci    reqbuf.count = 1;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci    if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
538c2ecf20Sopenharmony_ci	if (errno == EINVAL)
548c2ecf20Sopenharmony_ci	    printf("Video capturing or DMABUF streaming is not supported\\n");
558c2ecf20Sopenharmony_ci	else
568c2ecf20Sopenharmony_ci	    perror("VIDIOC_REQBUFS");
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
598c2ecf20Sopenharmony_ci    }
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciThe buffer (plane) file descriptor is passed on the fly with the
628c2ecf20Sopenharmony_ci:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. In case of multiplanar
638c2ecf20Sopenharmony_cibuffers, every plane can be associated with a different DMABUF
648c2ecf20Sopenharmony_cidescriptor. Although buffers are commonly cycled, applications can pass
658c2ecf20Sopenharmony_cia different DMABUF descriptor at each :ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call.
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciExample: Queueing DMABUF using single plane API
688c2ecf20Sopenharmony_ci===============================================
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci.. code-block:: c
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci    int buffer_queue(int v4lfd, int index, int dmafd)
738c2ecf20Sopenharmony_ci    {
748c2ecf20Sopenharmony_ci	struct v4l2_buffer buf;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	memset(&buf, 0, sizeof buf);
778c2ecf20Sopenharmony_ci	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
788c2ecf20Sopenharmony_ci	buf.memory = V4L2_MEMORY_DMABUF;
798c2ecf20Sopenharmony_ci	buf.index = index;
808c2ecf20Sopenharmony_ci	buf.m.fd = dmafd;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {
838c2ecf20Sopenharmony_ci	    perror("VIDIOC_QBUF");
848c2ecf20Sopenharmony_ci	    return -1;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci    }
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciExample 3.6. Queueing DMABUF using multi plane API
918c2ecf20Sopenharmony_ci==================================================
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci.. code-block:: c
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci    int buffer_queue_mp(int v4lfd, int index, int dmafd[], int n_planes)
968c2ecf20Sopenharmony_ci    {
978c2ecf20Sopenharmony_ci	struct v4l2_buffer buf;
988c2ecf20Sopenharmony_ci	struct v4l2_plane planes[VIDEO_MAX_PLANES];
998c2ecf20Sopenharmony_ci	int i;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	memset(&buf, 0, sizeof buf);
1028c2ecf20Sopenharmony_ci	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1038c2ecf20Sopenharmony_ci	buf.memory = V4L2_MEMORY_DMABUF;
1048c2ecf20Sopenharmony_ci	buf.index = index;
1058c2ecf20Sopenharmony_ci	buf.m.planes = planes;
1068c2ecf20Sopenharmony_ci	buf.length = n_planes;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	memset(&planes, 0, sizeof planes);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	for (i = 0; i < n_planes; ++i)
1118c2ecf20Sopenharmony_ci	    buf.m.planes[i].m.fd = dmafd[i];
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {
1148c2ecf20Sopenharmony_ci	    perror("VIDIOC_QBUF");
1158c2ecf20Sopenharmony_ci	    return -1;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return 0;
1198c2ecf20Sopenharmony_ci    }
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciCaptured or displayed buffers are dequeued with the
1228c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the
1238c2ecf20Sopenharmony_cibuffer at any time between the completion of the DMA and this ioctl. The
1248c2ecf20Sopenharmony_cimemory is also unlocked when
1258c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called,
1268c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, or when the device is closed.
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciFor capturing applications it is customary to enqueue a number of empty
1298c2ecf20Sopenharmony_cibuffers, to start capturing and enter the read loop. Here the
1308c2ecf20Sopenharmony_ciapplication waits until a filled buffer can be dequeued, and re-enqueues
1318c2ecf20Sopenharmony_cithe buffer when the data is no longer needed. Output applications fill
1328c2ecf20Sopenharmony_ciand enqueue buffers, when enough buffers are stacked up output is
1338c2ecf20Sopenharmony_cistarted. In the write loop, when the application runs out of free
1348c2ecf20Sopenharmony_cibuffers it must wait until an empty buffer can be dequeued and reused.
1358c2ecf20Sopenharmony_ciTwo methods exist to suspend execution of the application until one or
1368c2ecf20Sopenharmony_cimore buffers can be dequeued. By default :ref:`VIDIOC_DQBUF
1378c2ecf20Sopenharmony_ci<VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the
1388c2ecf20Sopenharmony_ci``O_NONBLOCK`` flag was given to the :c:func:`open()` function,
1398c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``
1408c2ecf20Sopenharmony_cierror code when no buffer is available. The
1418c2ecf20Sopenharmony_ci:c:func:`select()` and :c:func:`poll()`
1428c2ecf20Sopenharmony_cifunctions are always available.
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciTo start and stop capturing or displaying applications call the
1458c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and
1468c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls.
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci.. note::
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci   :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` removes all buffers from
1518c2ecf20Sopenharmony_ci   both queues and unlocks all buffers as a side effect. Since there is no
1528c2ecf20Sopenharmony_ci   notion of doing anything "now" on a multitasking system, if an
1538c2ecf20Sopenharmony_ci   application needs to synchronize with another event it should examine
1548c2ecf20Sopenharmony_ci   the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or
1558c2ecf20Sopenharmony_ci   outputted buffers.
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciDrivers implementing DMABUF importing I/O must support the
1588c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`,
1598c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON
1608c2ecf20Sopenharmony_ci<VIDIOC_STREAMON>` and :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls,
1618c2ecf20Sopenharmony_ciand the :c:func:`select()` and :c:func:`poll()`
1628c2ecf20Sopenharmony_cifunctions.
163