18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _func-read:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci***********
78c2ecf20Sopenharmony_ciV4L2 read()
88c2ecf20Sopenharmony_ci***********
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciName
118c2ecf20Sopenharmony_ci====
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civ4l2-read - Read from a V4L2 device
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciSynopsis
168c2ecf20Sopenharmony_ci========
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci.. code-block:: c
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci    #include <unistd.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci.. c:function:: ssize_t read( int fd, void *buf, size_t count )
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciArguments
258c2ecf20Sopenharmony_ci=========
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci``fd``
288c2ecf20Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci``buf``
318c2ecf20Sopenharmony_ci   Buffer to be filled
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci``count``
348c2ecf20Sopenharmony_ci  Max number of bytes to read
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciDescription
378c2ecf20Sopenharmony_ci===========
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci:c:func:`read()` attempts to read up to ``count`` bytes from file
408c2ecf20Sopenharmony_cidescriptor ``fd`` into the buffer starting at ``buf``. The layout of the
418c2ecf20Sopenharmony_cidata in the buffer is discussed in the respective device interface
428c2ecf20Sopenharmony_cisection, see ##. If ``count`` is zero, :c:func:`read()` returns zero
438c2ecf20Sopenharmony_ciand has no other results. If ``count`` is greater than ``SSIZE_MAX``,
448c2ecf20Sopenharmony_cithe result is unspecified. Regardless of the ``count`` value each
458c2ecf20Sopenharmony_ci:c:func:`read()` call will provide at most one frame (two fields)
468c2ecf20Sopenharmony_ciworth of data.
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciBy default :c:func:`read()` blocks until data becomes available. When
498c2ecf20Sopenharmony_cithe ``O_NONBLOCK`` flag was given to the :c:func:`open()`
508c2ecf20Sopenharmony_cifunction it returns immediately with an ``EAGAIN`` error code when no data
518c2ecf20Sopenharmony_ciis available. The :c:func:`select()` or
528c2ecf20Sopenharmony_ci:c:func:`poll()` functions can always be used to suspend
538c2ecf20Sopenharmony_ciexecution until data becomes available. All drivers supporting the
548c2ecf20Sopenharmony_ci:c:func:`read()` function must also support :c:func:`select()` and
558c2ecf20Sopenharmony_ci:c:func:`poll()`.
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ciDrivers can implement read functionality in different ways, using a
588c2ecf20Sopenharmony_cisingle or multiple buffers and discarding the oldest or newest frames
598c2ecf20Sopenharmony_cionce the internal buffers are filled.
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci:c:func:`read()` never returns a "snapshot" of a buffer being filled.
628c2ecf20Sopenharmony_ciUsing a single buffer the driver will stop capturing when the
638c2ecf20Sopenharmony_ciapplication starts reading the buffer until the read is finished. Thus
648c2ecf20Sopenharmony_cionly the period of the vertical blanking interval is available for
658c2ecf20Sopenharmony_cireading, or the capture rate must fall below the nominal frame rate of
668c2ecf20Sopenharmony_cithe video standard.
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciThe behavior of :c:func:`read()` when called during the active picture
698c2ecf20Sopenharmony_ciperiod or the vertical blanking separating the top and bottom field
708c2ecf20Sopenharmony_cidepends on the discarding policy. A driver discarding the oldest frames
718c2ecf20Sopenharmony_cikeeps capturing into an internal buffer, continuously overwriting the
728c2ecf20Sopenharmony_cipreviously, not read frame, and returns the frame being received at the
738c2ecf20Sopenharmony_citime of the :c:func:`read()` call as soon as it is complete.
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ciA driver discarding the newest frames stops capturing until the next
768c2ecf20Sopenharmony_ci:c:func:`read()` call. The frame being received at :c:func:`read()`
778c2ecf20Sopenharmony_citime is discarded, returning the following frame instead. Again this
788c2ecf20Sopenharmony_ciimplies a reduction of the capture rate to one half or less of the
798c2ecf20Sopenharmony_cinominal frame rate. An example of this model is the video read mode of
808c2ecf20Sopenharmony_cithe bttv driver, initiating a DMA to user memory when :c:func:`read()`
818c2ecf20Sopenharmony_ciis called and returning when the DMA finished.
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciIn the multiple buffer model drivers maintain a ring of internal
848c2ecf20Sopenharmony_cibuffers, automatically advancing to the next free buffer. This allows
858c2ecf20Sopenharmony_cicontinuous capturing when the application can empty the buffers fast
868c2ecf20Sopenharmony_cienough. Again, the behavior when the driver runs out of free buffers
878c2ecf20Sopenharmony_cidepends on the discarding policy.
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciApplications can get and set the number of buffers used internally by
908c2ecf20Sopenharmony_cithe driver with the :ref:`VIDIOC_G_PARM <VIDIOC_G_PARM>` and
918c2ecf20Sopenharmony_ci:ref:`VIDIOC_S_PARM <VIDIOC_G_PARM>` ioctls. They are optional,
928c2ecf20Sopenharmony_cihowever. The discarding policy is not reported and cannot be changed.
938c2ecf20Sopenharmony_ciFor minimum requirements see :ref:`devices`.
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciReturn Value
968c2ecf20Sopenharmony_ci============
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciOn success, the number of bytes read is returned. It is not an error if
998c2ecf20Sopenharmony_cithis number is smaller than the number of bytes requested, or the amount
1008c2ecf20Sopenharmony_ciof data required for one frame. This may happen for example because
1018c2ecf20Sopenharmony_ci:c:func:`read()` was interrupted by a signal. On error, -1 is
1028c2ecf20Sopenharmony_cireturned, and the ``errno`` variable is set appropriately. In this case
1038c2ecf20Sopenharmony_cithe next read will start at the beginning of a new frame. Possible error
1048c2ecf20Sopenharmony_cicodes are:
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciEAGAIN
1078c2ecf20Sopenharmony_ci    Non-blocking I/O has been selected using O_NONBLOCK and no data was
1088c2ecf20Sopenharmony_ci    immediately available for reading.
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciEBADF
1118c2ecf20Sopenharmony_ci    ``fd`` is not a valid file descriptor or is not open for reading, or
1128c2ecf20Sopenharmony_ci    the process already has the maximum number of files open.
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciEBUSY
1158c2ecf20Sopenharmony_ci    The driver does not support multiple read streams and the device is
1168c2ecf20Sopenharmony_ci    already in use.
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ciEFAULT
1198c2ecf20Sopenharmony_ci    ``buf`` references an inaccessible memory area.
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciEINTR
1228c2ecf20Sopenharmony_ci    The call was interrupted by a signal before any data was read.
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciEIO
1258c2ecf20Sopenharmony_ci    I/O error. This indicates some hardware problem or a failure to
1268c2ecf20Sopenharmony_ci    communicate with a remote device (USB camera etc.).
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciEINVAL
1298c2ecf20Sopenharmony_ci    The :c:func:`read()` function is not supported by this driver, not
1308c2ecf20Sopenharmony_ci    on this device, or generally not on this type of device.
131