18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _func-mmap:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci***********
78c2ecf20Sopenharmony_ciV4L2 mmap()
88c2ecf20Sopenharmony_ci***********
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciName
118c2ecf20Sopenharmony_ci====
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civ4l2-mmap - Map device memory into application address space
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciSynopsis
168c2ecf20Sopenharmony_ci========
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci.. code-block:: c
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci    #include <unistd.h>
218c2ecf20Sopenharmony_ci    #include <sys/mman.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci.. c:function:: void *mmap( void *start, size_t length, int prot, int flags, int fd, off_t offset )
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciArguments
268c2ecf20Sopenharmony_ci=========
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci``start``
298c2ecf20Sopenharmony_ci    Map the buffer to this address in the application's address space.
308c2ecf20Sopenharmony_ci    When the ``MAP_FIXED`` flag is specified, ``start`` must be a
318c2ecf20Sopenharmony_ci    multiple of the pagesize and mmap will fail when the specified
328c2ecf20Sopenharmony_ci    address cannot be used. Use of this option is discouraged;
338c2ecf20Sopenharmony_ci    applications should just specify a ``NULL`` pointer here.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci``length``
368c2ecf20Sopenharmony_ci    Length of the memory area to map. This must be the same value as
378c2ecf20Sopenharmony_ci    returned by the driver in the struct
388c2ecf20Sopenharmony_ci    :c:type:`v4l2_buffer` ``length`` field for the
398c2ecf20Sopenharmony_ci    single-planar API, and the same value as returned by the driver in
408c2ecf20Sopenharmony_ci    the struct :c:type:`v4l2_plane` ``length`` field for
418c2ecf20Sopenharmony_ci    the multi-planar API.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci``prot``
448c2ecf20Sopenharmony_ci    The ``prot`` argument describes the desired memory protection.
458c2ecf20Sopenharmony_ci    Regardless of the device type and the direction of data exchange it
468c2ecf20Sopenharmony_ci    should be set to ``PROT_READ`` | ``PROT_WRITE``, permitting read
478c2ecf20Sopenharmony_ci    and write access to image buffers. Drivers should support at least
488c2ecf20Sopenharmony_ci    this combination of flags.
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci    .. note::
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci      #. The Linux ``videobuf`` kernel module, which is used by some
538c2ecf20Sopenharmony_ci	 drivers supports only ``PROT_READ`` | ``PROT_WRITE``. When the
548c2ecf20Sopenharmony_ci	 driver does not support the desired protection, the
558c2ecf20Sopenharmony_ci	 :c:func:`mmap()` function fails.
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci      #. Device memory accesses (e. g. the memory on a graphics card
588c2ecf20Sopenharmony_ci	 with video capturing hardware) may incur a performance penalty
598c2ecf20Sopenharmony_ci	 compared to main memory accesses, or reads may be significantly
608c2ecf20Sopenharmony_ci	 slower than writes or vice versa. Other I/O methods may be more
618c2ecf20Sopenharmony_ci	 efficient in such case.
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci``flags``
648c2ecf20Sopenharmony_ci    The ``flags`` parameter specifies the type of the mapped object,
658c2ecf20Sopenharmony_ci    mapping options and whether modifications made to the mapped copy of
668c2ecf20Sopenharmony_ci    the page are private to the process or are to be shared with other
678c2ecf20Sopenharmony_ci    references.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci    ``MAP_FIXED`` requests that the driver selects no other address than
708c2ecf20Sopenharmony_ci    the one specified. If the specified address cannot be used,
718c2ecf20Sopenharmony_ci    :c:func:`mmap()` will fail. If ``MAP_FIXED`` is specified,
728c2ecf20Sopenharmony_ci    ``start`` must be a multiple of the pagesize. Use of this option is
738c2ecf20Sopenharmony_ci    discouraged.
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci    One of the ``MAP_SHARED`` or ``MAP_PRIVATE`` flags must be set.
768c2ecf20Sopenharmony_ci    ``MAP_SHARED`` allows applications to share the mapped memory with
778c2ecf20Sopenharmony_ci    other (e. g. child-) processes.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci    .. note::
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci       The Linux ``videobuf`` module  which is used by some
828c2ecf20Sopenharmony_ci       drivers supports only ``MAP_SHARED``. ``MAP_PRIVATE`` requests
838c2ecf20Sopenharmony_ci       copy-on-write semantics. V4L2 applications should not set the
848c2ecf20Sopenharmony_ci       ``MAP_PRIVATE``, ``MAP_DENYWRITE``, ``MAP_EXECUTABLE`` or ``MAP_ANON``
858c2ecf20Sopenharmony_ci       flags.
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci``fd``
888c2ecf20Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci``offset``
918c2ecf20Sopenharmony_ci    Offset of the buffer in device memory. This must be the same value
928c2ecf20Sopenharmony_ci    as returned by the driver in the struct
938c2ecf20Sopenharmony_ci    :c:type:`v4l2_buffer` ``m`` union ``offset`` field for
948c2ecf20Sopenharmony_ci    the single-planar API, and the same value as returned by the driver
958c2ecf20Sopenharmony_ci    in the struct :c:type:`v4l2_plane` ``m`` union
968c2ecf20Sopenharmony_ci    ``mem_offset`` field for the multi-planar API.
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciDescription
998c2ecf20Sopenharmony_ci===========
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciThe :c:func:`mmap()` function asks to map ``length`` bytes starting at
1028c2ecf20Sopenharmony_ci``offset`` in the memory of the device specified by ``fd`` into the
1038c2ecf20Sopenharmony_ciapplication address space, preferably at address ``start``. This latter
1048c2ecf20Sopenharmony_ciaddress is a hint only, and is usually specified as 0.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciSuitable length and offset parameters are queried with the
1078c2ecf20Sopenharmony_ci:ref:`VIDIOC_QUERYBUF` ioctl. Buffers must be
1088c2ecf20Sopenharmony_ciallocated with the :ref:`VIDIOC_REQBUFS` ioctl
1098c2ecf20Sopenharmony_cibefore they can be queried.
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciTo unmap buffers the :c:func:`munmap()` function is used.
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciReturn Value
1148c2ecf20Sopenharmony_ci============
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciOn success :c:func:`mmap()` returns a pointer to the mapped buffer. On
1178c2ecf20Sopenharmony_cierror ``MAP_FAILED`` (-1) is returned, and the ``errno`` variable is set
1188c2ecf20Sopenharmony_ciappropriately. Possible error codes are:
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciEBADF
1218c2ecf20Sopenharmony_ci    ``fd`` is not a valid file descriptor.
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciEACCES
1248c2ecf20Sopenharmony_ci    ``fd`` is not open for reading and writing.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciEINVAL
1278c2ecf20Sopenharmony_ci    The ``start`` or ``length`` or ``offset`` are not suitable. (E. g.
1288c2ecf20Sopenharmony_ci    they are too large, or not aligned on a ``PAGESIZE`` boundary.)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci    The ``flags`` or ``prot`` value is not supported.
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci    No buffers have been allocated with the
1338c2ecf20Sopenharmony_ci    :ref:`VIDIOC_REQBUFS` ioctl.
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciENOMEM
1368c2ecf20Sopenharmony_ci    Not enough physical or virtual memory was available to complete the
1378c2ecf20Sopenharmony_ci    request.
138