| Mauro Carvalho Chehab | 8e080c2 | 2009-09-13 22:16:04 -0300 | [diff] [blame] | 1 | <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 <unistd.h> | 
 | 16 | #include <sys/mman.h></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 | 
 | 36 | application's address space. When the <constant>MAP_FIXED</constant> | 
 | 37 | flag is specified, <parameter>start</parameter> must be a multiple of the | 
 | 38 | pagesize and mmap will fail when the specified address | 
 | 39 | cannot be used. Use of this option is discouraged; applications should | 
 | 40 | just 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 | 
 | 47 | same 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 | 
 | 55 | desired memory protection. Regardless of the device type and the | 
 | 56 | direction of data exchange it should be set to | 
 | 57 | <constant>PROT_READ</constant> | <constant>PROT_WRITE</constant>, | 
 | 58 | permitting read and write access to image buffers. Drivers should | 
 | 59 | support at least this combination of flags. Note the Linux | 
 | 60 | <filename>video-buf</filename> kernel module, which is used by the | 
 | 61 | bttv, saa7134, saa7146, cx88 and vivi driver supports only | 
 | 62 | <constant>PROT_READ</constant> | <constant>PROT_WRITE</constant>. When | 
 | 63 | the driver does not support the desired protection the | 
 | 64 | <function>mmap()</function> function fails.</para> | 
 | 65 | 	  <para>Note device memory accesses (⪚ the memory on a | 
 | 66 | graphics card with video capturing hardware) may incur a performance | 
 | 67 | penalty compared to main memory accesses, or reads may be | 
 | 68 | significantly slower than writes or vice versa. Other I/O methods may | 
 | 69 | be 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 | 
 | 76 | specifies the type of the mapped object, mapping options and whether | 
 | 77 | modifications made to the mapped copy of the page are private to the | 
 | 78 | process or are to be shared with other references.</para> | 
 | 79 | 	  <para><constant>MAP_FIXED</constant> requests that the | 
 | 80 | driver selects no other address than the one specified. If the | 
 | 81 | specified 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 | 
 | 84 | of 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 | 
 | 88 | mapped memory with other (⪚ child-) processes. Note the Linux | 
 | 89 | <filename>video-buf</filename> module which is used by the bttv, | 
 | 90 | saa7134, saa7146, cx88 and vivi driver supports only | 
 | 91 | <constant>MAP_SHARED</constant>. <constant>MAP_PRIVATE</constant> | 
 | 92 | requests 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> | 
 | 95 | flag.</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 | 
 | 108 | same 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, | 
 | 122 | preferably at address <parameter>start</parameter>. This latter | 
 | 123 | address 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 | 
 | 136 | the mapped buffer. On error <constant>MAP_FAILED</constant> (-1) is | 
 | 137 | returned, and the <varname>errno</varname> variable is set | 
 | 138 | appropriately. 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 | 
 | 145 | descriptor.</para> | 
 | 146 | 	</listitem> | 
 | 147 |       </varlistentry> | 
 | 148 |       <varlistentry> | 
 | 149 | 	<term><errorcode>EACCES</errorcode></term> | 
 | 150 | 	<listitem> | 
 | 151 | 	  <para><parameter>fd</parameter> is | 
 | 152 | not 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 | 
 | 160 | suitable. (E. 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 | 
 | 172 | complete the request.</para> | 
 | 173 | 	</listitem> | 
 | 174 |       </varlistentry> | 
 | 175 |     </variablelist> | 
 | 176 |   </refsect1> | 
 | 177 | </refentry> | 
 | 178 |  | 
 | 179 | <!-- | 
 | 180 | Local Variables: | 
 | 181 | mode: sgml | 
 | 182 | sgml-parent-document: "v4l2.sgml" | 
 | 183 | indent-tabs-mode: nil | 
 | 184 | End: | 
 | 185 | --> |