18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 28c2ecf20Sopenharmony_ci.. c:namespace:: V4L 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci.. _userp: 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci***************************** 78c2ecf20Sopenharmony_ciStreaming I/O (User Pointers) 88c2ecf20Sopenharmony_ci***************************** 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ciInput and output devices support this I/O method when the 118c2ecf20Sopenharmony_ci``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct 128c2ecf20Sopenharmony_ci:c:type:`v4l2_capability` returned by the 138c2ecf20Sopenharmony_ci:ref:`VIDIOC_QUERYCAP` ioctl is set. If the 148c2ecf20Sopenharmony_ciparticular user pointer method (not only memory mapping) is supported 158c2ecf20Sopenharmony_cimust be determined by calling the :ref:`VIDIOC_REQBUFS` ioctl 168c2ecf20Sopenharmony_ciwith the memory type set to ``V4L2_MEMORY_USERPTR``. 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ciThis I/O method combines advantages of the read/write and memory mapping 198c2ecf20Sopenharmony_cimethods. Buffers (planes) are allocated by the application itself, and 208c2ecf20Sopenharmony_cican reside for example in virtual or shared memory. Only pointers to 218c2ecf20Sopenharmony_cidata are exchanged, these pointers and meta-information are passed in 228c2ecf20Sopenharmony_cistruct :c:type:`v4l2_buffer` (or in struct 238c2ecf20Sopenharmony_ci:c:type:`v4l2_plane` in the multi-planar API case). The 248c2ecf20Sopenharmony_cidriver must be switched into user pointer I/O mode by calling the 258c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS` with the desired buffer type. 268c2ecf20Sopenharmony_ciNo buffers (planes) are allocated beforehand, consequently they are not 278c2ecf20Sopenharmony_ciindexed and cannot be queried like mapped buffers with the 288c2ecf20Sopenharmony_ci:ref:`VIDIOC_QUERYBUF <VIDIOC_QUERYBUF>` ioctl. 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciExample: Initiating streaming I/O with user pointers 318c2ecf20Sopenharmony_ci==================================================== 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci.. code-block:: c 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci struct v4l2_requestbuffers reqbuf; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci memset (&reqbuf, 0, sizeof (reqbuf)); 388c2ecf20Sopenharmony_ci reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 398c2ecf20Sopenharmony_ci reqbuf.memory = V4L2_MEMORY_USERPTR; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci if (ioctl (fd, VIDIOC_REQBUFS, &reqbuf) == -1) { 428c2ecf20Sopenharmony_ci if (errno == EINVAL) 438c2ecf20Sopenharmony_ci printf ("Video capturing or user pointer streaming is not supported\\n"); 448c2ecf20Sopenharmony_ci else 458c2ecf20Sopenharmony_ci perror ("VIDIOC_REQBUFS"); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci exit (EXIT_FAILURE); 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ciBuffer (plane) addresses and sizes are passed on the fly with the 518c2ecf20Sopenharmony_ci:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. Although buffers are commonly 528c2ecf20Sopenharmony_cicycled, applications can pass different addresses and sizes at each 538c2ecf20Sopenharmony_ci:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call. If required by the hardware the 548c2ecf20Sopenharmony_cidriver swaps memory pages within physical memory to create a continuous 558c2ecf20Sopenharmony_ciarea of memory. This happens transparently to the application in the 568c2ecf20Sopenharmony_civirtual memory subsystem of the kernel. When buffer pages have been 578c2ecf20Sopenharmony_ciswapped out to disk they are brought back and finally locked in physical 588c2ecf20Sopenharmony_cimemory for DMA. [#f1]_ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ciFilled or displayed buffers are dequeued with the 618c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the 628c2ecf20Sopenharmony_cimemory pages at any time between the completion of the DMA and this 638c2ecf20Sopenharmony_ciioctl. The memory is also unlocked when 648c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called, 658c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS`, or when the device is closed. 668c2ecf20Sopenharmony_ciApplications must take care not to free buffers without dequeuing. 678c2ecf20Sopenharmony_ciFirstly, the buffers remain locked for longer, wasting physical memory. 688c2ecf20Sopenharmony_ciSecondly the driver will not be notified when the memory is returned to 698c2ecf20Sopenharmony_cithe application's free list and subsequently reused for other purposes, 708c2ecf20Sopenharmony_cipossibly completing the requested DMA and overwriting valuable data. 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciFor capturing applications it is customary to enqueue a number of empty 738c2ecf20Sopenharmony_cibuffers, to start capturing and enter the read loop. Here the 748c2ecf20Sopenharmony_ciapplication waits until a filled buffer can be dequeued, and re-enqueues 758c2ecf20Sopenharmony_cithe buffer when the data is no longer needed. Output applications fill 768c2ecf20Sopenharmony_ciand enqueue buffers, when enough buffers are stacked up output is 778c2ecf20Sopenharmony_cistarted. In the write loop, when the application runs out of free 788c2ecf20Sopenharmony_cibuffers it must wait until an empty buffer can be dequeued and reused. 798c2ecf20Sopenharmony_ciTwo methods exist to suspend execution of the application until one or 808c2ecf20Sopenharmony_cimore buffers can be dequeued. By default :ref:`VIDIOC_DQBUF 818c2ecf20Sopenharmony_ci<VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the 828c2ecf20Sopenharmony_ci``O_NONBLOCK`` flag was given to the :c:func:`open()` function, 838c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN`` 848c2ecf20Sopenharmony_cierror code when no buffer is available. The :ref:`select() 858c2ecf20Sopenharmony_ci<func-select>` or :c:func:`poll()` function are always 868c2ecf20Sopenharmony_ciavailable. 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ciTo start and stop capturing or output applications call the 898c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and 908c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctl. 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci.. note:: 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` removes all buffers from 958c2ecf20Sopenharmony_ci both queues and unlocks all buffers as a side effect. Since there is no 968c2ecf20Sopenharmony_ci notion of doing anything "now" on a multitasking system, if an 978c2ecf20Sopenharmony_ci application needs to synchronize with another event it should examine 988c2ecf20Sopenharmony_ci the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or 998c2ecf20Sopenharmony_ci outputted buffers. 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ciDrivers implementing user pointer I/O must support the 1028c2ecf20Sopenharmony_ci:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`, 1038c2ecf20Sopenharmony_ci:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` 1048c2ecf20Sopenharmony_ciand :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls, the 1058c2ecf20Sopenharmony_ci:c:func:`select()` and :c:func:`poll()` function. [#f2]_ 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci.. [#f1] 1088c2ecf20Sopenharmony_ci We expect that frequently used buffers are typically not swapped out. 1098c2ecf20Sopenharmony_ci Anyway, the process of swapping, locking or generating scatter-gather 1108c2ecf20Sopenharmony_ci lists may be time consuming. The delay can be masked by the depth of 1118c2ecf20Sopenharmony_ci the incoming buffer queue, and perhaps by maintaining caches assuming 1128c2ecf20Sopenharmony_ci a buffer will be soon enqueued again. On the other hand, to optimize 1138c2ecf20Sopenharmony_ci memory usage drivers can limit the number of buffers locked in 1148c2ecf20Sopenharmony_ci advance and recycle the most recently used buffers first. Of course, 1158c2ecf20Sopenharmony_ci the pages of empty buffers in the incoming queue need not be saved to 1168c2ecf20Sopenharmony_ci disk. Output buffers must be saved on the incoming and outgoing queue 1178c2ecf20Sopenharmony_ci because an application may share them with other processes. 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci.. [#f2] 1208c2ecf20Sopenharmony_ci At the driver level :c:func:`select()` and :c:func:`poll()` are 1218c2ecf20Sopenharmony_ci the same, and :c:func:`select()` is too important to be optional. 1228c2ecf20Sopenharmony_ci The rest should be evident. 123