Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
0002 .. c:namespace:: V4L
0003 
0004 .. _func-mmap:
0005 
0006 ***********
0007 V4L2 mmap()
0008 ***********
0009 
0010 Name
0011 ====
0012 
0013 v4l2-mmap - Map device memory into application address space
0014 
0015 Synopsis
0016 ========
0017 
0018 .. code-block:: c
0019 
0020     #include <unistd.h>
0021     #include <sys/mman.h>
0022 
0023 .. c:function:: void *mmap( void *start, size_t length, int prot, int flags, int fd, off_t offset )
0024 
0025 Arguments
0026 =========
0027 
0028 ``start``
0029     Map the buffer to this address in the application's address space.
0030     When the ``MAP_FIXED`` flag is specified, ``start`` must be a
0031     multiple of the pagesize and mmap will fail when the specified
0032     address cannot be used. Use of this option is discouraged;
0033     applications should just specify a ``NULL`` pointer here.
0034 
0035 ``length``
0036     Length of the memory area to map. This must be the same value as
0037     returned by the driver in the struct
0038     :c:type:`v4l2_buffer` ``length`` field for the
0039     single-planar API, and the same value as returned by the driver in
0040     the struct :c:type:`v4l2_plane` ``length`` field for
0041     the multi-planar API.
0042 
0043 ``prot``
0044     The ``prot`` argument describes the desired memory protection.
0045     Regardless of the device type and the direction of data exchange it
0046     should be set to ``PROT_READ`` | ``PROT_WRITE``, permitting read
0047     and write access to image buffers. Drivers should support at least
0048     this combination of flags.
0049 
0050     .. note::
0051 
0052       #. The Linux ``videobuf`` kernel module, which is used by some
0053          drivers supports only ``PROT_READ`` | ``PROT_WRITE``. When the
0054          driver does not support the desired protection, the
0055          :c:func:`mmap()` function fails.
0056 
0057       #. Device memory accesses (e. g. the memory on a graphics card
0058          with video capturing hardware) may incur a performance penalty
0059          compared to main memory accesses, or reads may be significantly
0060          slower than writes or vice versa. Other I/O methods may be more
0061          efficient in such case.
0062 
0063 ``flags``
0064     The ``flags`` parameter specifies the type of the mapped object,
0065     mapping options and whether modifications made to the mapped copy of
0066     the page are private to the process or are to be shared with other
0067     references.
0068 
0069     ``MAP_FIXED`` requests that the driver selects no other address than
0070     the one specified. If the specified address cannot be used,
0071     :c:func:`mmap()` will fail. If ``MAP_FIXED`` is specified,
0072     ``start`` must be a multiple of the pagesize. Use of this option is
0073     discouraged.
0074 
0075     One of the ``MAP_SHARED`` or ``MAP_PRIVATE`` flags must be set.
0076     ``MAP_SHARED`` allows applications to share the mapped memory with
0077     other (e. g. child-) processes.
0078 
0079     .. note::
0080 
0081        The Linux ``videobuf`` module  which is used by some
0082        drivers supports only ``MAP_SHARED``. ``MAP_PRIVATE`` requests
0083        copy-on-write semantics. V4L2 applications should not set the
0084        ``MAP_PRIVATE``, ``MAP_DENYWRITE``, ``MAP_EXECUTABLE`` or ``MAP_ANON``
0085        flags.
0086 
0087 ``fd``
0088     File descriptor returned by :c:func:`open()`.
0089 
0090 ``offset``
0091     Offset of the buffer in device memory. This must be the same value
0092     as returned by the driver in the struct
0093     :c:type:`v4l2_buffer` ``m`` union ``offset`` field for
0094     the single-planar API, and the same value as returned by the driver
0095     in the struct :c:type:`v4l2_plane` ``m`` union
0096     ``mem_offset`` field for the multi-planar API.
0097 
0098 Description
0099 ===========
0100 
0101 The :c:func:`mmap()` function asks to map ``length`` bytes starting at
0102 ``offset`` in the memory of the device specified by ``fd`` into the
0103 application address space, preferably at address ``start``. This latter
0104 address is a hint only, and is usually specified as 0.
0105 
0106 Suitable length and offset parameters are queried with the
0107 :ref:`VIDIOC_QUERYBUF` ioctl. Buffers must be
0108 allocated with the :ref:`VIDIOC_REQBUFS` ioctl
0109 before they can be queried.
0110 
0111 To unmap buffers the :c:func:`munmap()` function is used.
0112 
0113 Return Value
0114 ============
0115 
0116 On success :c:func:`mmap()` returns a pointer to the mapped buffer. On
0117 error ``MAP_FAILED`` (-1) is returned, and the ``errno`` variable is set
0118 appropriately. Possible error codes are:
0119 
0120 EBADF
0121     ``fd`` is not a valid file descriptor.
0122 
0123 EACCES
0124     ``fd`` is not open for reading and writing.
0125 
0126 EINVAL
0127     The ``start`` or ``length`` or ``offset`` are not suitable. (E. g.
0128     they are too large, or not aligned on a ``PAGESIZE`` boundary.)
0129 
0130     The ``flags`` or ``prot`` value is not supported.
0131 
0132     No buffers have been allocated with the
0133     :ref:`VIDIOC_REQBUFS` ioctl.
0134 
0135 ENOMEM
0136     Not enough physical or virtual memory was available to complete the
0137     request.