brintos

brintos / linux-shallow public Read only

0
0
Text · 4.6 KiB · e6f6ac4 Raw
131 lines · plain
1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later2.. c:namespace:: V4L3 4.. _func-read:5 6***********7V4L2 read()8***********9 10Name11====12 13v4l2-read - Read from a V4L2 device14 15Synopsis16========17 18.. code-block:: c19 20    #include <unistd.h>21 22.. c:function:: ssize_t read( int fd, void *buf, size_t count )23 24Arguments25=========26 27``fd``28    File descriptor returned by :c:func:`open()`.29 30``buf``31   Buffer to be filled32 33``count``34  Max number of bytes to read35 36Description37===========38 39:c:func:`read()` attempts to read up to ``count`` bytes from file40descriptor ``fd`` into the buffer starting at ``buf``. The layout of the41data in the buffer is discussed in the respective device interface42section, see ##. If ``count`` is zero, :c:func:`read()` returns zero43and has no other results. If ``count`` is greater than ``SSIZE_MAX``,44the result is unspecified. Regardless of the ``count`` value each45:c:func:`read()` call will provide at most one frame (two fields)46worth of data.47 48By default :c:func:`read()` blocks until data becomes available. When49the ``O_NONBLOCK`` flag was given to the :c:func:`open()`50function it returns immediately with an ``EAGAIN`` error code when no data51is available. The :c:func:`select()` or52:c:func:`poll()` functions can always be used to suspend53execution until data becomes available. All drivers supporting the54:c:func:`read()` function must also support :c:func:`select()` and55:c:func:`poll()`.56 57Drivers can implement read functionality in different ways, using a58single or multiple buffers and discarding the oldest or newest frames59once the internal buffers are filled.60 61:c:func:`read()` never returns a "snapshot" of a buffer being filled.62Using a single buffer the driver will stop capturing when the63application starts reading the buffer until the read is finished. Thus64only the period of the vertical blanking interval is available for65reading, or the capture rate must fall below the nominal frame rate of66the video standard.67 68The behavior of :c:func:`read()` when called during the active picture69period or the vertical blanking separating the top and bottom field70depends on the discarding policy. A driver discarding the oldest frames71keeps capturing into an internal buffer, continuously overwriting the72previously, not read frame, and returns the frame being received at the73time of the :c:func:`read()` call as soon as it is complete.74 75A driver discarding the newest frames stops capturing until the next76:c:func:`read()` call. The frame being received at :c:func:`read()`77time is discarded, returning the following frame instead. Again this78implies a reduction of the capture rate to one half or less of the79nominal frame rate. An example of this model is the video read mode of80the bttv driver, initiating a DMA to user memory when :c:func:`read()`81is called and returning when the DMA finished.82 83In the multiple buffer model drivers maintain a ring of internal84buffers, automatically advancing to the next free buffer. This allows85continuous capturing when the application can empty the buffers fast86enough. Again, the behavior when the driver runs out of free buffers87depends on the discarding policy.88 89Applications can get and set the number of buffers used internally by90the driver with the :ref:`VIDIOC_G_PARM <VIDIOC_G_PARM>` and91:ref:`VIDIOC_S_PARM <VIDIOC_G_PARM>` ioctls. They are optional,92however. The discarding policy is not reported and cannot be changed.93For minimum requirements see :ref:`devices`.94 95Return Value96============97 98On success, the number of bytes read is returned. It is not an error if99this number is smaller than the number of bytes requested, or the amount100of data required for one frame. This may happen for example because101:c:func:`read()` was interrupted by a signal. On error, -1 is102returned, and the ``errno`` variable is set appropriately. In this case103the next read will start at the beginning of a new frame. Possible error104codes are:105 106EAGAIN107    Non-blocking I/O has been selected using O_NONBLOCK and no data was108    immediately available for reading.109 110EBADF111    ``fd`` is not a valid file descriptor or is not open for reading, or112    the process already has the maximum number of files open.113 114EBUSY115    The driver does not support multiple read streams and the device is116    already in use.117 118EFAULT119    ``buf`` references an inaccessible memory area.120 121EINTR122    The call was interrupted by a signal before any data was read.123 124EIO125    I/O error. This indicates some hardware problem or a failure to126    communicate with a remote device (USB camera etc.).127 128EINVAL129    The :c:func:`read()` function is not supported by this driver, not130    on this device, or generally not on this type of device.131