Path: blob/master/Documentation/DocBook/v4l/func-mmap.xml
10823 views
<refentry id="func-mmap">1<refmeta>2<refentrytitle>V4L2 mmap()</refentrytitle>3&manvol;4</refmeta>56<refnamediv>7<refname>v4l2-mmap</refname>8<refpurpose>Map device memory into application address space</refpurpose>9</refnamediv>1011<refsynopsisdiv>12<funcsynopsis>13<funcsynopsisinfo>14#include <unistd.h>15#include <sys/mman.h></funcsynopsisinfo>16<funcprototype>17<funcdef>void *<function>mmap</function></funcdef>18<paramdef>void *<parameter>start</parameter></paramdef>19<paramdef>size_t <parameter>length</parameter></paramdef>20<paramdef>int <parameter>prot</parameter></paramdef>21<paramdef>int <parameter>flags</parameter></paramdef>22<paramdef>int <parameter>fd</parameter></paramdef>23<paramdef>off_t <parameter>offset</parameter></paramdef>24</funcprototype>25</funcsynopsis>26</refsynopsisdiv>2728<refsect1>29<title>Arguments</title>30<variablelist>31<varlistentry>32<term><parameter>start</parameter></term>33<listitem>34<para>Map the buffer to this address in the35application's address space. When the <constant>MAP_FIXED</constant>36flag is specified, <parameter>start</parameter> must be a multiple of the37pagesize and mmap will fail when the specified address38cannot be used. Use of this option is discouraged; applications should39just specify a <constant>NULL</constant> pointer here.</para>40</listitem>41</varlistentry>42<varlistentry>43<term><parameter>length</parameter></term>44<listitem>45<para>Length of the memory area to map. This must be the46same value as returned by the driver in the &v4l2-buffer;47<structfield>length</structfield> field for the48single-planar API, and the same value as returned by the driver49in the &v4l2-plane; <structfield>length</structfield> field for the50multi-planar API.</para>51</listitem>52</varlistentry>53<varlistentry>54<term><parameter>prot</parameter></term>55<listitem>56<para>The <parameter>prot</parameter> argument describes the57desired memory protection. Regardless of the device type and the58direction of data exchange it should be set to59<constant>PROT_READ</constant> | <constant>PROT_WRITE</constant>,60permitting read and write access to image buffers. Drivers should61support at least this combination of flags. Note the Linux62<filename>video-buf</filename> kernel module, which is used by the63bttv, saa7134, saa7146, cx88 and vivi driver supports only64<constant>PROT_READ</constant> | <constant>PROT_WRITE</constant>. When65the driver does not support the desired protection the66<function>mmap()</function> function fails.</para>67<para>Note device memory accesses (⪚ the memory on a68graphics card with video capturing hardware) may incur a performance69penalty compared to main memory accesses, or reads may be70significantly slower than writes or vice versa. Other I/O methods may71be more efficient in this case.</para>72</listitem>73</varlistentry>74<varlistentry>75<term><parameter>flags</parameter></term>76<listitem>77<para>The <parameter>flags</parameter> parameter78specifies the type of the mapped object, mapping options and whether79modifications made to the mapped copy of the page are private to the80process or are to be shared with other references.</para>81<para><constant>MAP_FIXED</constant> requests that the82driver selects no other address than the one specified. If the83specified address cannot be used, <function>mmap()</function> will fail. If84<constant>MAP_FIXED</constant> is specified,85<parameter>start</parameter> must be a multiple of the pagesize. Use86of this option is discouraged.</para>87<para>One of the <constant>MAP_SHARED</constant> or88<constant>MAP_PRIVATE</constant> flags must be set.89<constant>MAP_SHARED</constant> allows applications to share the90mapped memory with other (⪚ child-) processes. Note the Linux91<filename>video-buf</filename> module which is used by the bttv,92saa7134, saa7146, cx88 and vivi driver supports only93<constant>MAP_SHARED</constant>. <constant>MAP_PRIVATE</constant>94requests copy-on-write semantics. V4L2 applications should not set the95<constant>MAP_PRIVATE</constant>, <constant>MAP_DENYWRITE</constant>,96<constant>MAP_EXECUTABLE</constant> or <constant>MAP_ANON</constant>97flag.</para>98</listitem>99</varlistentry>100<varlistentry>101<term><parameter>fd</parameter></term>102<listitem>103<para>&fd;</para>104</listitem>105</varlistentry>106<varlistentry>107<term><parameter>offset</parameter></term>108<listitem>109<para>Offset of the buffer in device memory. This must be the110same value as returned by the driver in the &v4l2-buffer;111<structfield>m</structfield> union <structfield>offset</structfield> field for112the single-planar API, and the same value as returned by the driver113in the &v4l2-plane; <structfield>m</structfield> union114<structfield>mem_offset</structfield> field for the multi-planar API.</para>115</listitem>116</varlistentry>117</variablelist>118</refsect1>119120<refsect1>121<title>Description</title>122123<para>The <function>mmap()</function> function asks to map124<parameter>length</parameter> bytes starting at125<parameter>offset</parameter> in the memory of the device specified by126<parameter>fd</parameter> into the application address space,127preferably at address <parameter>start</parameter>. This latter128address is a hint only, and is usually specified as 0.</para>129130<para>Suitable length and offset parameters are queried with the131&VIDIOC-QUERYBUF; ioctl. Buffers must be allocated with the132&VIDIOC-REQBUFS; ioctl before they can be queried.</para>133134<para>To unmap buffers the &func-munmap; function is used.</para>135</refsect1>136137<refsect1>138<title>Return Value</title>139140<para>On success <function>mmap()</function> returns a pointer to141the mapped buffer. On error <constant>MAP_FAILED</constant> (-1) is142returned, and the <varname>errno</varname> variable is set143appropriately. Possible error codes are:</para>144145<variablelist>146<varlistentry>147<term><errorcode>EBADF</errorcode></term>148<listitem>149<para><parameter>fd</parameter> is not a valid file150descriptor.</para>151</listitem>152</varlistentry>153<varlistentry>154<term><errorcode>EACCES</errorcode></term>155<listitem>156<para><parameter>fd</parameter> is157not open for reading and writing.</para>158</listitem>159</varlistentry>160<varlistentry>161<term><errorcode>EINVAL</errorcode></term>162<listitem>163<para>The <parameter>start</parameter> or164<parameter>length</parameter> or <parameter>offset</parameter> are not165suitable. (E. g. they are too large, or not aligned on a166<constant>PAGESIZE</constant> boundary.)</para>167<para>The <parameter>flags</parameter> or168<parameter>prot</parameter> value is not supported.</para>169<para>No buffers have been allocated with the170&VIDIOC-REQBUFS; ioctl.</para>171</listitem>172</varlistentry>173<varlistentry>174<term><errorcode>ENOMEM</errorcode></term>175<listitem>176<para>Not enough physical or virtual memory was available to177complete the request.</para>178</listitem>179</varlistentry>180</variablelist>181</refsect1>182</refentry>183184<!--185Local Variables:186mode: sgml187sgml-parent-document: "v4l2.sgml"188indent-tabs-mode: nil189End:190-->191192193