162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _userp:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci*****************************
762306a36Sopenharmony_ciStreaming I/O (User Pointers)
862306a36Sopenharmony_ci*****************************
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciInput and output devices support this I/O method when the
1162306a36Sopenharmony_ci``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct
1262306a36Sopenharmony_ci:c:type:`v4l2_capability` returned by the
1362306a36Sopenharmony_ci:ref:`VIDIOC_QUERYCAP` ioctl is set. If the
1462306a36Sopenharmony_ciparticular user pointer method (not only memory mapping) is supported
1562306a36Sopenharmony_cimust be determined by calling the :ref:`VIDIOC_REQBUFS` ioctl
1662306a36Sopenharmony_ciwith the memory type set to ``V4L2_MEMORY_USERPTR``.
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciThis I/O method combines advantages of the read/write and memory mapping
1962306a36Sopenharmony_cimethods. Buffers (planes) are allocated by the application itself, and
2062306a36Sopenharmony_cican reside for example in virtual or shared memory. Only pointers to
2162306a36Sopenharmony_cidata are exchanged, these pointers and meta-information are passed in
2262306a36Sopenharmony_cistruct :c:type:`v4l2_buffer` (or in struct
2362306a36Sopenharmony_ci:c:type:`v4l2_plane` in the multi-planar API case). The
2462306a36Sopenharmony_cidriver must be switched into user pointer I/O mode by calling the
2562306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS` with the desired buffer type.
2662306a36Sopenharmony_ciNo buffers (planes) are allocated beforehand, consequently they are not
2762306a36Sopenharmony_ciindexed and cannot be queried like mapped buffers with the
2862306a36Sopenharmony_ci:ref:`VIDIOC_QUERYBUF <VIDIOC_QUERYBUF>` ioctl.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciExample: Initiating streaming I/O with user pointers
3162306a36Sopenharmony_ci====================================================
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci.. code-block:: c
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci    struct v4l2_requestbuffers reqbuf;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci    memset (&reqbuf, 0, sizeof (reqbuf));
3862306a36Sopenharmony_ci    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3962306a36Sopenharmony_ci    reqbuf.memory = V4L2_MEMORY_USERPTR;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci    if (ioctl (fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
4262306a36Sopenharmony_ci	if (errno == EINVAL)
4362306a36Sopenharmony_ci	    printf ("Video capturing or user pointer streaming is not supported\\n");
4462306a36Sopenharmony_ci	else
4562306a36Sopenharmony_ci	    perror ("VIDIOC_REQBUFS");
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	exit (EXIT_FAILURE);
4862306a36Sopenharmony_ci    }
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciBuffer (plane) addresses and sizes are passed on the fly with the
5162306a36Sopenharmony_ci:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. Although buffers are commonly
5262306a36Sopenharmony_cicycled, applications can pass different addresses and sizes at each
5362306a36Sopenharmony_ci:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call. If required by the hardware the
5462306a36Sopenharmony_cidriver swaps memory pages within physical memory to create a continuous
5562306a36Sopenharmony_ciarea of memory. This happens transparently to the application in the
5662306a36Sopenharmony_civirtual memory subsystem of the kernel. When buffer pages have been
5762306a36Sopenharmony_ciswapped out to disk they are brought back and finally locked in physical
5862306a36Sopenharmony_cimemory for DMA. [#f1]_
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciFilled or displayed buffers are dequeued with the
6162306a36Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the
6262306a36Sopenharmony_cimemory pages at any time between the completion of the DMA and this
6362306a36Sopenharmony_ciioctl. The memory is also unlocked when
6462306a36Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called,
6562306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS`, or when the device is closed.
6662306a36Sopenharmony_ciApplications must take care not to free buffers without dequeuing.
6762306a36Sopenharmony_ciFirstly, the buffers remain locked for longer, wasting physical memory.
6862306a36Sopenharmony_ciSecondly the driver will not be notified when the memory is returned to
6962306a36Sopenharmony_cithe application's free list and subsequently reused for other purposes,
7062306a36Sopenharmony_cipossibly completing the requested DMA and overwriting valuable data.
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciFor capturing applications it is customary to enqueue a number of empty
7362306a36Sopenharmony_cibuffers, to start capturing and enter the read loop. Here the
7462306a36Sopenharmony_ciapplication waits until a filled buffer can be dequeued, and re-enqueues
7562306a36Sopenharmony_cithe buffer when the data is no longer needed. Output applications fill
7662306a36Sopenharmony_ciand enqueue buffers, when enough buffers are stacked up output is
7762306a36Sopenharmony_cistarted. In the write loop, when the application runs out of free
7862306a36Sopenharmony_cibuffers it must wait until an empty buffer can be dequeued and reused.
7962306a36Sopenharmony_ciTwo methods exist to suspend execution of the application until one or
8062306a36Sopenharmony_cimore buffers can be dequeued. By default :ref:`VIDIOC_DQBUF
8162306a36Sopenharmony_ci<VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the
8262306a36Sopenharmony_ci``O_NONBLOCK`` flag was given to the :c:func:`open()` function,
8362306a36Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``
8462306a36Sopenharmony_cierror code when no buffer is available. The :ref:`select()
8562306a36Sopenharmony_ci<func-select>` or :c:func:`poll()` function are always
8662306a36Sopenharmony_ciavailable.
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciTo start and stop capturing or output applications call the
8962306a36Sopenharmony_ci:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and
9062306a36Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctl.
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci.. note::
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci   :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` removes all buffers from
9562306a36Sopenharmony_ci   both queues and unlocks all buffers as a side effect. Since there is no
9662306a36Sopenharmony_ci   notion of doing anything "now" on a multitasking system, if an
9762306a36Sopenharmony_ci   application needs to synchronize with another event it should examine
9862306a36Sopenharmony_ci   the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or
9962306a36Sopenharmony_ci   outputted buffers.
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciDrivers implementing user pointer I/O must support the
10262306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`,
10362306a36Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>`
10462306a36Sopenharmony_ciand :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls, the
10562306a36Sopenharmony_ci:c:func:`select()` and :c:func:`poll()` function. [#f2]_
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci.. [#f1]
10862306a36Sopenharmony_ci   We expect that frequently used buffers are typically not swapped out.
10962306a36Sopenharmony_ci   Anyway, the process of swapping, locking or generating scatter-gather
11062306a36Sopenharmony_ci   lists may be time consuming. The delay can be masked by the depth of
11162306a36Sopenharmony_ci   the incoming buffer queue, and perhaps by maintaining caches assuming
11262306a36Sopenharmony_ci   a buffer will be soon enqueued again. On the other hand, to optimize
11362306a36Sopenharmony_ci   memory usage drivers can limit the number of buffers locked in
11462306a36Sopenharmony_ci   advance and recycle the most recently used buffers first. Of course,
11562306a36Sopenharmony_ci   the pages of empty buffers in the incoming queue need not be saved to
11662306a36Sopenharmony_ci   disk. Output buffers must be saved on the incoming and outgoing queue
11762306a36Sopenharmony_ci   because an application may share them with other processes.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci.. [#f2]
12062306a36Sopenharmony_ci   At the driver level :c:func:`select()` and :c:func:`poll()` are
12162306a36Sopenharmony_ci   the same, and :c:func:`select()` is too important to be optional.
12262306a36Sopenharmony_ci   The rest should be evident.
123