xref: /OK3568_Linux_fs/kernel/Documentation/userspace-api/media/v4l/func-mmap.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2*4882a593Smuzhiyun.. c:namespace:: V4L
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun.. _func-mmap:
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun***********
7*4882a593SmuzhiyunV4L2 mmap()
8*4882a593Smuzhiyun***********
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunName
11*4882a593Smuzhiyun====
12*4882a593Smuzhiyun
13*4882a593Smuzhiyunv4l2-mmap - Map device memory into application address space
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunSynopsis
16*4882a593Smuzhiyun========
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun.. code-block:: c
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun    #include <unistd.h>
21*4882a593Smuzhiyun    #include <sys/mman.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun.. c:function:: void *mmap( void *start, size_t length, int prot, int flags, int fd, off_t offset )
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunArguments
26*4882a593Smuzhiyun=========
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun``start``
29*4882a593Smuzhiyun    Map the buffer to this address in the application's address space.
30*4882a593Smuzhiyun    When the ``MAP_FIXED`` flag is specified, ``start`` must be a
31*4882a593Smuzhiyun    multiple of the pagesize and mmap will fail when the specified
32*4882a593Smuzhiyun    address cannot be used. Use of this option is discouraged;
33*4882a593Smuzhiyun    applications should just specify a ``NULL`` pointer here.
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun``length``
36*4882a593Smuzhiyun    Length of the memory area to map. This must be the same value as
37*4882a593Smuzhiyun    returned by the driver in the struct
38*4882a593Smuzhiyun    :c:type:`v4l2_buffer` ``length`` field for the
39*4882a593Smuzhiyun    single-planar API, and the same value as returned by the driver in
40*4882a593Smuzhiyun    the struct :c:type:`v4l2_plane` ``length`` field for
41*4882a593Smuzhiyun    the multi-planar API.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun``prot``
44*4882a593Smuzhiyun    The ``prot`` argument describes the desired memory protection.
45*4882a593Smuzhiyun    Regardless of the device type and the direction of data exchange it
46*4882a593Smuzhiyun    should be set to ``PROT_READ`` | ``PROT_WRITE``, permitting read
47*4882a593Smuzhiyun    and write access to image buffers. Drivers should support at least
48*4882a593Smuzhiyun    this combination of flags.
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun    .. note::
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun      #. The Linux ``videobuf`` kernel module, which is used by some
53*4882a593Smuzhiyun	 drivers supports only ``PROT_READ`` | ``PROT_WRITE``. When the
54*4882a593Smuzhiyun	 driver does not support the desired protection, the
55*4882a593Smuzhiyun	 :c:func:`mmap()` function fails.
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun      #. Device memory accesses (e. g. the memory on a graphics card
58*4882a593Smuzhiyun	 with video capturing hardware) may incur a performance penalty
59*4882a593Smuzhiyun	 compared to main memory accesses, or reads may be significantly
60*4882a593Smuzhiyun	 slower than writes or vice versa. Other I/O methods may be more
61*4882a593Smuzhiyun	 efficient in such case.
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun``flags``
64*4882a593Smuzhiyun    The ``flags`` parameter specifies the type of the mapped object,
65*4882a593Smuzhiyun    mapping options and whether modifications made to the mapped copy of
66*4882a593Smuzhiyun    the page are private to the process or are to be shared with other
67*4882a593Smuzhiyun    references.
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun    ``MAP_FIXED`` requests that the driver selects no other address than
70*4882a593Smuzhiyun    the one specified. If the specified address cannot be used,
71*4882a593Smuzhiyun    :c:func:`mmap()` will fail. If ``MAP_FIXED`` is specified,
72*4882a593Smuzhiyun    ``start`` must be a multiple of the pagesize. Use of this option is
73*4882a593Smuzhiyun    discouraged.
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun    One of the ``MAP_SHARED`` or ``MAP_PRIVATE`` flags must be set.
76*4882a593Smuzhiyun    ``MAP_SHARED`` allows applications to share the mapped memory with
77*4882a593Smuzhiyun    other (e. g. child-) processes.
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun    .. note::
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun       The Linux ``videobuf`` module  which is used by some
82*4882a593Smuzhiyun       drivers supports only ``MAP_SHARED``. ``MAP_PRIVATE`` requests
83*4882a593Smuzhiyun       copy-on-write semantics. V4L2 applications should not set the
84*4882a593Smuzhiyun       ``MAP_PRIVATE``, ``MAP_DENYWRITE``, ``MAP_EXECUTABLE`` or ``MAP_ANON``
85*4882a593Smuzhiyun       flags.
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun``fd``
88*4882a593Smuzhiyun    File descriptor returned by :c:func:`open()`.
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun``offset``
91*4882a593Smuzhiyun    Offset of the buffer in device memory. This must be the same value
92*4882a593Smuzhiyun    as returned by the driver in the struct
93*4882a593Smuzhiyun    :c:type:`v4l2_buffer` ``m`` union ``offset`` field for
94*4882a593Smuzhiyun    the single-planar API, and the same value as returned by the driver
95*4882a593Smuzhiyun    in the struct :c:type:`v4l2_plane` ``m`` union
96*4882a593Smuzhiyun    ``mem_offset`` field for the multi-planar API.
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunDescription
99*4882a593Smuzhiyun===========
100*4882a593Smuzhiyun
101*4882a593SmuzhiyunThe :c:func:`mmap()` function asks to map ``length`` bytes starting at
102*4882a593Smuzhiyun``offset`` in the memory of the device specified by ``fd`` into the
103*4882a593Smuzhiyunapplication address space, preferably at address ``start``. This latter
104*4882a593Smuzhiyunaddress is a hint only, and is usually specified as 0.
105*4882a593Smuzhiyun
106*4882a593SmuzhiyunSuitable length and offset parameters are queried with the
107*4882a593Smuzhiyun:ref:`VIDIOC_QUERYBUF` ioctl. Buffers must be
108*4882a593Smuzhiyunallocated with the :ref:`VIDIOC_REQBUFS` ioctl
109*4882a593Smuzhiyunbefore they can be queried.
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunTo unmap buffers the :c:func:`munmap()` function is used.
112*4882a593Smuzhiyun
113*4882a593SmuzhiyunReturn Value
114*4882a593Smuzhiyun============
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunOn success :c:func:`mmap()` returns a pointer to the mapped buffer. On
117*4882a593Smuzhiyunerror ``MAP_FAILED`` (-1) is returned, and the ``errno`` variable is set
118*4882a593Smuzhiyunappropriately. Possible error codes are:
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunEBADF
121*4882a593Smuzhiyun    ``fd`` is not a valid file descriptor.
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunEACCES
124*4882a593Smuzhiyun    ``fd`` is not open for reading and writing.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunEINVAL
127*4882a593Smuzhiyun    The ``start`` or ``length`` or ``offset`` are not suitable. (E. g.
128*4882a593Smuzhiyun    they are too large, or not aligned on a ``PAGESIZE`` boundary.)
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun    The ``flags`` or ``prot`` value is not supported.
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun    No buffers have been allocated with the
133*4882a593Smuzhiyun    :ref:`VIDIOC_REQBUFS` ioctl.
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunENOMEM
136*4882a593Smuzhiyun    Not enough physical or virtual memory was available to complete the
137*4882a593Smuzhiyun    request.
138