blob: 2e2fc3933aea73685ee473b9da2f154943850e4c [file] [log] [blame]
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -03001<refentry id="func-mmap">
2 <refmeta>
3 <refentrytitle>V4L2 mmap()</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>v4l2-mmap</refname>
9 <refpurpose>Map device memory into application address space</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcsynopsisinfo>
15#include &lt;unistd.h&gt;
16#include &lt;sys/mman.h&gt;</funcsynopsisinfo>
17 <funcprototype>
18 <funcdef>void *<function>mmap</function></funcdef>
19 <paramdef>void *<parameter>start</parameter></paramdef>
20 <paramdef>size_t <parameter>length</parameter></paramdef>
21 <paramdef>int <parameter>prot</parameter></paramdef>
22 <paramdef>int <parameter>flags</parameter></paramdef>
23 <paramdef>int <parameter>fd</parameter></paramdef>
24 <paramdef>off_t <parameter>offset</parameter></paramdef>
25 </funcprototype>
26 </funcsynopsis>
27 </refsynopsisdiv>
28
29 <refsect1>
30 <title>Arguments</title>
31 <variablelist>
32 <varlistentry>
33 <term><parameter>start</parameter></term>
34 <listitem>
35 <para>Map the buffer to this address in the
36application's address space. When the <constant>MAP_FIXED</constant>
37flag is specified, <parameter>start</parameter> must be a multiple of the
38pagesize and mmap will fail when the specified address
39cannot be used. Use of this option is discouraged; applications should
40just specify a <constant>NULL</constant> pointer here.</para>
41 </listitem>
42 </varlistentry>
43 <varlistentry>
44 <term><parameter>length</parameter></term>
45 <listitem>
46 <para>Length of the memory area to map. This must be the
47same value as returned by the driver in the &v4l2-buffer;
48<structfield>length</structfield> field.</para>
49 </listitem>
50 </varlistentry>
51 <varlistentry>
52 <term><parameter>prot</parameter></term>
53 <listitem>
54 <para>The <parameter>prot</parameter> argument describes the
55desired memory protection. Regardless of the device type and the
56direction of data exchange it should be set to
57<constant>PROT_READ</constant> | <constant>PROT_WRITE</constant>,
58permitting read and write access to image buffers. Drivers should
59support at least this combination of flags. Note the Linux
60<filename>video-buf</filename> kernel module, which is used by the
61bttv, saa7134, saa7146, cx88 and vivi driver supports only
62<constant>PROT_READ</constant> | <constant>PROT_WRITE</constant>. When
63the driver does not support the desired protection the
64<function>mmap()</function> function fails.</para>
65 <para>Note device memory accesses (&eg; the memory on a
66graphics card with video capturing hardware) may incur a performance
67penalty compared to main memory accesses, or reads may be
68significantly slower than writes or vice versa. Other I/O methods may
69be more efficient in this case.</para>
70 </listitem>
71 </varlistentry>
72 <varlistentry>
73 <term><parameter>flags</parameter></term>
74 <listitem>
75 <para>The <parameter>flags</parameter> parameter
76specifies the type of the mapped object, mapping options and whether
77modifications made to the mapped copy of the page are private to the
78process or are to be shared with other references.</para>
79 <para><constant>MAP_FIXED</constant> requests that the
80driver selects no other address than the one specified. If the
81specified address cannot be used, <function>mmap()</function> will fail. If
82<constant>MAP_FIXED</constant> is specified,
83<parameter>start</parameter> must be a multiple of the pagesize. Use
84of this option is discouraged.</para>
85 <para>One of the <constant>MAP_SHARED</constant> or
86<constant>MAP_PRIVATE</constant> flags must be set.
87<constant>MAP_SHARED</constant> allows applications to share the
88mapped memory with other (&eg; child-) processes. Note the Linux
89<filename>video-buf</filename> module which is used by the bttv,
90saa7134, saa7146, cx88 and vivi driver supports only
91<constant>MAP_SHARED</constant>. <constant>MAP_PRIVATE</constant>
92requests copy-on-write semantics. V4L2 applications should not set the
93<constant>MAP_PRIVATE</constant>, <constant>MAP_DENYWRITE</constant>,
94<constant>MAP_EXECUTABLE</constant> or <constant>MAP_ANON</constant>
95flag.</para>
96 </listitem>
97 </varlistentry>
98 <varlistentry>
99 <term><parameter>fd</parameter></term>
100 <listitem>
101 <para>&fd;</para>
102 </listitem>
103 </varlistentry>
104 <varlistentry>
105 <term><parameter>offset</parameter></term>
106 <listitem>
107 <para>Offset of the buffer in device memory. This must be the
108same value as returned by the driver in the &v4l2-buffer;
109<structfield>m</structfield> union <structfield>offset</structfield> field.</para>
110 </listitem>
111 </varlistentry>
112 </variablelist>
113 </refsect1>
114
115 <refsect1>
116 <title>Description</title>
117
118 <para>The <function>mmap()</function> function asks to map
119<parameter>length</parameter> bytes starting at
120<parameter>offset</parameter> in the memory of the device specified by
121<parameter>fd</parameter> into the application address space,
122preferably at address <parameter>start</parameter>. This latter
123address is a hint only, and is usually specified as 0.</para>
124
125 <para>Suitable length and offset parameters are queried with the
126&VIDIOC-QUERYBUF; ioctl. Buffers must be allocated with the
127&VIDIOC-REQBUFS; ioctl before they can be queried.</para>
128
129 <para>To unmap buffers the &func-munmap; function is used.</para>
130 </refsect1>
131
132 <refsect1>
133 <title>Return Value</title>
134
135 <para>On success <function>mmap()</function> returns a pointer to
136the mapped buffer. On error <constant>MAP_FAILED</constant> (-1) is
137returned, and the <varname>errno</varname> variable is set
138appropriately. Possible error codes are:</para>
139
140 <variablelist>
141 <varlistentry>
142 <term><errorcode>EBADF</errorcode></term>
143 <listitem>
144 <para><parameter>fd</parameter> is not a valid file
145descriptor.</para>
146 </listitem>
147 </varlistentry>
148 <varlistentry>
149 <term><errorcode>EACCES</errorcode></term>
150 <listitem>
151 <para><parameter>fd</parameter> is
152not open for reading and writing.</para>
153 </listitem>
154 </varlistentry>
155 <varlistentry>
156 <term><errorcode>EINVAL</errorcode></term>
157 <listitem>
158 <para>The <parameter>start</parameter> or
159<parameter>length</parameter> or <parameter>offset</parameter> are not
160suitable. (E.&nbsp;g. they are too large, or not aligned on a
161<constant>PAGESIZE</constant> boundary.)</para>
162 <para>The <parameter>flags</parameter> or
163<parameter>prot</parameter> value is not supported.</para>
164 <para>No buffers have been allocated with the
165&VIDIOC-REQBUFS; ioctl.</para>
166 </listitem>
167 </varlistentry>
168 <varlistentry>
169 <term><errorcode>ENOMEM</errorcode></term>
170 <listitem>
171 <para>Not enough physical or virtual memory was available to
172complete the request.</para>
173 </listitem>
174 </varlistentry>
175 </variablelist>
176 </refsect1>
177</refentry>
178
179<!--
180Local Variables:
181mode: sgml
182sgml-parent-document: "v4l2.sgml"
183indent-tabs-mode: nil
184End:
185-->