162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _func-read:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci***********
762306a36Sopenharmony_ciV4L2 read()
862306a36Sopenharmony_ci***********
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciName
1162306a36Sopenharmony_ci====
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_civ4l2-read - Read from a V4L2 device
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciSynopsis
1662306a36Sopenharmony_ci========
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci.. code-block:: c
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci    #include <unistd.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci.. c:function:: ssize_t read( int fd, void *buf, size_t count )
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciArguments
2562306a36Sopenharmony_ci=========
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci``fd``
2862306a36Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci``buf``
3162306a36Sopenharmony_ci   Buffer to be filled
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci``count``
3462306a36Sopenharmony_ci  Max number of bytes to read
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciDescription
3762306a36Sopenharmony_ci===========
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci:c:func:`read()` attempts to read up to ``count`` bytes from file
4062306a36Sopenharmony_cidescriptor ``fd`` into the buffer starting at ``buf``. The layout of the
4162306a36Sopenharmony_cidata in the buffer is discussed in the respective device interface
4262306a36Sopenharmony_cisection, see ##. If ``count`` is zero, :c:func:`read()` returns zero
4362306a36Sopenharmony_ciand has no other results. If ``count`` is greater than ``SSIZE_MAX``,
4462306a36Sopenharmony_cithe result is unspecified. Regardless of the ``count`` value each
4562306a36Sopenharmony_ci:c:func:`read()` call will provide at most one frame (two fields)
4662306a36Sopenharmony_ciworth of data.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciBy default :c:func:`read()` blocks until data becomes available. When
4962306a36Sopenharmony_cithe ``O_NONBLOCK`` flag was given to the :c:func:`open()`
5062306a36Sopenharmony_cifunction it returns immediately with an ``EAGAIN`` error code when no data
5162306a36Sopenharmony_ciis available. The :c:func:`select()` or
5262306a36Sopenharmony_ci:c:func:`poll()` functions can always be used to suspend
5362306a36Sopenharmony_ciexecution until data becomes available. All drivers supporting the
5462306a36Sopenharmony_ci:c:func:`read()` function must also support :c:func:`select()` and
5562306a36Sopenharmony_ci:c:func:`poll()`.
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciDrivers can implement read functionality in different ways, using a
5862306a36Sopenharmony_cisingle or multiple buffers and discarding the oldest or newest frames
5962306a36Sopenharmony_cionce the internal buffers are filled.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci:c:func:`read()` never returns a "snapshot" of a buffer being filled.
6262306a36Sopenharmony_ciUsing a single buffer the driver will stop capturing when the
6362306a36Sopenharmony_ciapplication starts reading the buffer until the read is finished. Thus
6462306a36Sopenharmony_cionly the period of the vertical blanking interval is available for
6562306a36Sopenharmony_cireading, or the capture rate must fall below the nominal frame rate of
6662306a36Sopenharmony_cithe video standard.
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciThe behavior of :c:func:`read()` when called during the active picture
6962306a36Sopenharmony_ciperiod or the vertical blanking separating the top and bottom field
7062306a36Sopenharmony_cidepends on the discarding policy. A driver discarding the oldest frames
7162306a36Sopenharmony_cikeeps capturing into an internal buffer, continuously overwriting the
7262306a36Sopenharmony_cipreviously, not read frame, and returns the frame being received at the
7362306a36Sopenharmony_citime of the :c:func:`read()` call as soon as it is complete.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciA driver discarding the newest frames stops capturing until the next
7662306a36Sopenharmony_ci:c:func:`read()` call. The frame being received at :c:func:`read()`
7762306a36Sopenharmony_citime is discarded, returning the following frame instead. Again this
7862306a36Sopenharmony_ciimplies a reduction of the capture rate to one half or less of the
7962306a36Sopenharmony_cinominal frame rate. An example of this model is the video read mode of
8062306a36Sopenharmony_cithe bttv driver, initiating a DMA to user memory when :c:func:`read()`
8162306a36Sopenharmony_ciis called and returning when the DMA finished.
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciIn the multiple buffer model drivers maintain a ring of internal
8462306a36Sopenharmony_cibuffers, automatically advancing to the next free buffer. This allows
8562306a36Sopenharmony_cicontinuous capturing when the application can empty the buffers fast
8662306a36Sopenharmony_cienough. Again, the behavior when the driver runs out of free buffers
8762306a36Sopenharmony_cidepends on the discarding policy.
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciApplications can get and set the number of buffers used internally by
9062306a36Sopenharmony_cithe driver with the :ref:`VIDIOC_G_PARM <VIDIOC_G_PARM>` and
9162306a36Sopenharmony_ci:ref:`VIDIOC_S_PARM <VIDIOC_G_PARM>` ioctls. They are optional,
9262306a36Sopenharmony_cihowever. The discarding policy is not reported and cannot be changed.
9362306a36Sopenharmony_ciFor minimum requirements see :ref:`devices`.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciReturn Value
9662306a36Sopenharmony_ci============
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciOn success, the number of bytes read is returned. It is not an error if
9962306a36Sopenharmony_cithis number is smaller than the number of bytes requested, or the amount
10062306a36Sopenharmony_ciof data required for one frame. This may happen for example because
10162306a36Sopenharmony_ci:c:func:`read()` was interrupted by a signal. On error, -1 is
10262306a36Sopenharmony_cireturned, and the ``errno`` variable is set appropriately. In this case
10362306a36Sopenharmony_cithe next read will start at the beginning of a new frame. Possible error
10462306a36Sopenharmony_cicodes are:
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciEAGAIN
10762306a36Sopenharmony_ci    Non-blocking I/O has been selected using O_NONBLOCK and no data was
10862306a36Sopenharmony_ci    immediately available for reading.
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciEBADF
11162306a36Sopenharmony_ci    ``fd`` is not a valid file descriptor or is not open for reading, or
11262306a36Sopenharmony_ci    the process already has the maximum number of files open.
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciEBUSY
11562306a36Sopenharmony_ci    The driver does not support multiple read streams and the device is
11662306a36Sopenharmony_ci    already in use.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciEFAULT
11962306a36Sopenharmony_ci    ``buf`` references an inaccessible memory area.
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciEINTR
12262306a36Sopenharmony_ci    The call was interrupted by a signal before any data was read.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciEIO
12562306a36Sopenharmony_ci    I/O error. This indicates some hardware problem or a failure to
12662306a36Sopenharmony_ci    communicate with a remote device (USB camera etc.).
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciEINVAL
12962306a36Sopenharmony_ci    The :c:func:`read()` function is not supported by this driver, not
13062306a36Sopenharmony_ci    on this device, or generally not on this type of device.
131