xref: /OK3568_Linux_fs/kernel/Documentation/userspace-api/media/v4l/userp.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2*4882a593Smuzhiyun.. c:namespace:: V4L
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun.. _userp:
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun*****************************
7*4882a593SmuzhiyunStreaming I/O (User Pointers)
8*4882a593Smuzhiyun*****************************
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunInput and output devices support this I/O method when the
11*4882a593Smuzhiyun``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct
12*4882a593Smuzhiyun:c:type:`v4l2_capability` returned by the
13*4882a593Smuzhiyun:ref:`VIDIOC_QUERYCAP` ioctl is set. If the
14*4882a593Smuzhiyunparticular user pointer method (not only memory mapping) is supported
15*4882a593Smuzhiyunmust be determined by calling the :ref:`VIDIOC_REQBUFS` ioctl
16*4882a593Smuzhiyunwith the memory type set to ``V4L2_MEMORY_USERPTR``.
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunThis I/O method combines advantages of the read/write and memory mapping
19*4882a593Smuzhiyunmethods. Buffers (planes) are allocated by the application itself, and
20*4882a593Smuzhiyuncan reside for example in virtual or shared memory. Only pointers to
21*4882a593Smuzhiyundata are exchanged, these pointers and meta-information are passed in
22*4882a593Smuzhiyunstruct :c:type:`v4l2_buffer` (or in struct
23*4882a593Smuzhiyun:c:type:`v4l2_plane` in the multi-planar API case). The
24*4882a593Smuzhiyundriver must be switched into user pointer I/O mode by calling the
25*4882a593Smuzhiyun:ref:`VIDIOC_REQBUFS` with the desired buffer type.
26*4882a593SmuzhiyunNo buffers (planes) are allocated beforehand, consequently they are not
27*4882a593Smuzhiyunindexed and cannot be queried like mapped buffers with the
28*4882a593Smuzhiyun:ref:`VIDIOC_QUERYBUF <VIDIOC_QUERYBUF>` ioctl.
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunExample: Initiating streaming I/O with user pointers
31*4882a593Smuzhiyun====================================================
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun.. code-block:: c
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun    struct v4l2_requestbuffers reqbuf;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun    memset (&reqbuf, 0, sizeof (reqbuf));
38*4882a593Smuzhiyun    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
39*4882a593Smuzhiyun    reqbuf.memory = V4L2_MEMORY_USERPTR;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun    if (ioctl (fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
42*4882a593Smuzhiyun	if (errno == EINVAL)
43*4882a593Smuzhiyun	    printf ("Video capturing or user pointer streaming is not supported\\n");
44*4882a593Smuzhiyun	else
45*4882a593Smuzhiyun	    perror ("VIDIOC_REQBUFS");
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun	exit (EXIT_FAILURE);
48*4882a593Smuzhiyun    }
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunBuffer (plane) addresses and sizes are passed on the fly with the
51*4882a593Smuzhiyun:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. Although buffers are commonly
52*4882a593Smuzhiyuncycled, applications can pass different addresses and sizes at each
53*4882a593Smuzhiyun:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call. If required by the hardware the
54*4882a593Smuzhiyundriver swaps memory pages within physical memory to create a continuous
55*4882a593Smuzhiyunarea of memory. This happens transparently to the application in the
56*4882a593Smuzhiyunvirtual memory subsystem of the kernel. When buffer pages have been
57*4882a593Smuzhiyunswapped out to disk they are brought back and finally locked in physical
58*4882a593Smuzhiyunmemory for DMA. [#f1]_
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunFilled or displayed buffers are dequeued with the
61*4882a593Smuzhiyun:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the
62*4882a593Smuzhiyunmemory pages at any time between the completion of the DMA and this
63*4882a593Smuzhiyunioctl. The memory is also unlocked when
64*4882a593Smuzhiyun:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called,
65*4882a593Smuzhiyun:ref:`VIDIOC_REQBUFS`, or when the device is closed.
66*4882a593SmuzhiyunApplications must take care not to free buffers without dequeuing.
67*4882a593SmuzhiyunFirstly, the buffers remain locked for longer, wasting physical memory.
68*4882a593SmuzhiyunSecondly the driver will not be notified when the memory is returned to
69*4882a593Smuzhiyunthe application's free list and subsequently reused for other purposes,
70*4882a593Smuzhiyunpossibly completing the requested DMA and overwriting valuable data.
71*4882a593Smuzhiyun
72*4882a593SmuzhiyunFor capturing applications it is customary to enqueue a number of empty
73*4882a593Smuzhiyunbuffers, to start capturing and enter the read loop. Here the
74*4882a593Smuzhiyunapplication waits until a filled buffer can be dequeued, and re-enqueues
75*4882a593Smuzhiyunthe buffer when the data is no longer needed. Output applications fill
76*4882a593Smuzhiyunand enqueue buffers, when enough buffers are stacked up output is
77*4882a593Smuzhiyunstarted. In the write loop, when the application runs out of free
78*4882a593Smuzhiyunbuffers it must wait until an empty buffer can be dequeued and reused.
79*4882a593SmuzhiyunTwo methods exist to suspend execution of the application until one or
80*4882a593Smuzhiyunmore buffers can be dequeued. By default :ref:`VIDIOC_DQBUF
81*4882a593Smuzhiyun<VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the
82*4882a593Smuzhiyun``O_NONBLOCK`` flag was given to the :c:func:`open()` function,
83*4882a593Smuzhiyun:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``
84*4882a593Smuzhiyunerror code when no buffer is available. The :ref:`select()
85*4882a593Smuzhiyun<func-select>` or :c:func:`poll()` function are always
86*4882a593Smuzhiyunavailable.
87*4882a593Smuzhiyun
88*4882a593SmuzhiyunTo start and stop capturing or output applications call the
89*4882a593Smuzhiyun:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and
90*4882a593Smuzhiyun:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctl.
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun.. note::
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun   :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` removes all buffers from
95*4882a593Smuzhiyun   both queues and unlocks all buffers as a side effect. Since there is no
96*4882a593Smuzhiyun   notion of doing anything "now" on a multitasking system, if an
97*4882a593Smuzhiyun   application needs to synchronize with another event it should examine
98*4882a593Smuzhiyun   the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or
99*4882a593Smuzhiyun   outputted buffers.
100*4882a593Smuzhiyun
101*4882a593SmuzhiyunDrivers implementing user pointer I/O must support the
102*4882a593Smuzhiyun:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`,
103*4882a593Smuzhiyun:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>`
104*4882a593Smuzhiyunand :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls, the
105*4882a593Smuzhiyun:c:func:`select()` and :c:func:`poll()` function. [#f2]_
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun.. [#f1]
108*4882a593Smuzhiyun   We expect that frequently used buffers are typically not swapped out.
109*4882a593Smuzhiyun   Anyway, the process of swapping, locking or generating scatter-gather
110*4882a593Smuzhiyun   lists may be time consuming. The delay can be masked by the depth of
111*4882a593Smuzhiyun   the incoming buffer queue, and perhaps by maintaining caches assuming
112*4882a593Smuzhiyun   a buffer will be soon enqueued again. On the other hand, to optimize
113*4882a593Smuzhiyun   memory usage drivers can limit the number of buffers locked in
114*4882a593Smuzhiyun   advance and recycle the most recently used buffers first. Of course,
115*4882a593Smuzhiyun   the pages of empty buffers in the incoming queue need not be saved to
116*4882a593Smuzhiyun   disk. Output buffers must be saved on the incoming and outgoing queue
117*4882a593Smuzhiyun   because an application may share them with other processes.
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun.. [#f2]
120*4882a593Smuzhiyun   At the driver level :c:func:`select()` and :c:func:`poll()` are
121*4882a593Smuzhiyun   the same, and :c:func:`select()` is too important to be optional.
122*4882a593Smuzhiyun   The rest should be evident.
123