162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _func-mmap:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci***********
762306a36Sopenharmony_ciV4L2 mmap()
862306a36Sopenharmony_ci***********
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciName
1162306a36Sopenharmony_ci====
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_civ4l2-mmap - Map device memory into application address space
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciSynopsis
1662306a36Sopenharmony_ci========
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci.. code-block:: c
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci    #include <unistd.h>
2162306a36Sopenharmony_ci    #include <sys/mman.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci.. c:function:: void *mmap( void *start, size_t length, int prot, int flags, int fd, off_t offset )
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciArguments
2662306a36Sopenharmony_ci=========
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci``start``
2962306a36Sopenharmony_ci    Map the buffer to this address in the application's address space.
3062306a36Sopenharmony_ci    When the ``MAP_FIXED`` flag is specified, ``start`` must be a
3162306a36Sopenharmony_ci    multiple of the pagesize and mmap will fail when the specified
3262306a36Sopenharmony_ci    address cannot be used. Use of this option is discouraged;
3362306a36Sopenharmony_ci    applications should just specify a ``NULL`` pointer here.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci``length``
3662306a36Sopenharmony_ci    Length of the memory area to map. This must be the same value as
3762306a36Sopenharmony_ci    returned by the driver in the struct
3862306a36Sopenharmony_ci    :c:type:`v4l2_buffer` ``length`` field for the
3962306a36Sopenharmony_ci    single-planar API, and the same value as returned by the driver in
4062306a36Sopenharmony_ci    the struct :c:type:`v4l2_plane` ``length`` field for
4162306a36Sopenharmony_ci    the multi-planar API.
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci``prot``
4462306a36Sopenharmony_ci    The ``prot`` argument describes the desired memory protection.
4562306a36Sopenharmony_ci    Regardless of the device type and the direction of data exchange it
4662306a36Sopenharmony_ci    should be set to ``PROT_READ`` | ``PROT_WRITE``, permitting read
4762306a36Sopenharmony_ci    and write access to image buffers. Drivers should support at least
4862306a36Sopenharmony_ci    this combination of flags.
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci    .. note::
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci      #. The Linux ``videobuf`` kernel module, which is used by some
5362306a36Sopenharmony_ci	 drivers supports only ``PROT_READ`` | ``PROT_WRITE``. When the
5462306a36Sopenharmony_ci	 driver does not support the desired protection, the
5562306a36Sopenharmony_ci	 :c:func:`mmap()` function fails.
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci      #. Device memory accesses (e. g. the memory on a graphics card
5862306a36Sopenharmony_ci	 with video capturing hardware) may incur a performance penalty
5962306a36Sopenharmony_ci	 compared to main memory accesses, or reads may be significantly
6062306a36Sopenharmony_ci	 slower than writes or vice versa. Other I/O methods may be more
6162306a36Sopenharmony_ci	 efficient in such case.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci``flags``
6462306a36Sopenharmony_ci    The ``flags`` parameter specifies the type of the mapped object,
6562306a36Sopenharmony_ci    mapping options and whether modifications made to the mapped copy of
6662306a36Sopenharmony_ci    the page are private to the process or are to be shared with other
6762306a36Sopenharmony_ci    references.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci    ``MAP_FIXED`` requests that the driver selects no other address than
7062306a36Sopenharmony_ci    the one specified. If the specified address cannot be used,
7162306a36Sopenharmony_ci    :c:func:`mmap()` will fail. If ``MAP_FIXED`` is specified,
7262306a36Sopenharmony_ci    ``start`` must be a multiple of the pagesize. Use of this option is
7362306a36Sopenharmony_ci    discouraged.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci    One of the ``MAP_SHARED`` or ``MAP_PRIVATE`` flags must be set.
7662306a36Sopenharmony_ci    ``MAP_SHARED`` allows applications to share the mapped memory with
7762306a36Sopenharmony_ci    other (e. g. child-) processes.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci    .. note::
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci       The Linux ``videobuf`` module  which is used by some
8262306a36Sopenharmony_ci       drivers supports only ``MAP_SHARED``. ``MAP_PRIVATE`` requests
8362306a36Sopenharmony_ci       copy-on-write semantics. V4L2 applications should not set the
8462306a36Sopenharmony_ci       ``MAP_PRIVATE``, ``MAP_DENYWRITE``, ``MAP_EXECUTABLE`` or ``MAP_ANON``
8562306a36Sopenharmony_ci       flags.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci``fd``
8862306a36Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci``offset``
9162306a36Sopenharmony_ci    Offset of the buffer in device memory. This must be the same value
9262306a36Sopenharmony_ci    as returned by the driver in the struct
9362306a36Sopenharmony_ci    :c:type:`v4l2_buffer` ``m`` union ``offset`` field for
9462306a36Sopenharmony_ci    the single-planar API, and the same value as returned by the driver
9562306a36Sopenharmony_ci    in the struct :c:type:`v4l2_plane` ``m`` union
9662306a36Sopenharmony_ci    ``mem_offset`` field for the multi-planar API.
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciDescription
9962306a36Sopenharmony_ci===========
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciThe :c:func:`mmap()` function asks to map ``length`` bytes starting at
10262306a36Sopenharmony_ci``offset`` in the memory of the device specified by ``fd`` into the
10362306a36Sopenharmony_ciapplication address space, preferably at address ``start``. This latter
10462306a36Sopenharmony_ciaddress is a hint only, and is usually specified as 0.
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciSuitable length and offset parameters are queried with the
10762306a36Sopenharmony_ci:ref:`VIDIOC_QUERYBUF` ioctl. Buffers must be
10862306a36Sopenharmony_ciallocated with the :ref:`VIDIOC_REQBUFS` ioctl
10962306a36Sopenharmony_cibefore they can be queried.
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciTo unmap buffers the :c:func:`munmap()` function is used.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciReturn Value
11462306a36Sopenharmony_ci============
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciOn success :c:func:`mmap()` returns a pointer to the mapped buffer. On
11762306a36Sopenharmony_cierror ``MAP_FAILED`` (-1) is returned, and the ``errno`` variable is set
11862306a36Sopenharmony_ciappropriately. Possible error codes are:
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciEBADF
12162306a36Sopenharmony_ci    ``fd`` is not a valid file descriptor.
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciEACCES
12462306a36Sopenharmony_ci    ``fd`` is not open for reading and writing.
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciEINVAL
12762306a36Sopenharmony_ci    The ``start`` or ``length`` or ``offset`` are not suitable. (E. g.
12862306a36Sopenharmony_ci    they are too large, or not aligned on a ``PAGESIZE`` boundary.)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci    The ``flags`` or ``prot`` value is not supported.
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci    No buffers have been allocated with the
13362306a36Sopenharmony_ci    :ref:`VIDIOC_REQBUFS` ioctl.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciENOMEM
13662306a36Sopenharmony_ci    Not enough physical or virtual memory was available to complete the
13762306a36Sopenharmony_ci    request.
138