7.66. V4L2 mmap()¶
7.66.1. Name¶
v4l2-mmap - Map device memory into application address space
7.66.2. Synopsis¶
#include <unistd.h>
#include <sys/mman.h>
-
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)¶
7.66.3. Arguments¶
startMap the buffer to this address in the application’s address space. When the
MAP_FIXEDflag is specified,startmust be a multiple of the pagesize and mmap will fail when the specified address cannot be used. Use of this option is discouraged; applications should just specify aNULLpointer here.lengthLength of the memory area to map. This must be the same value as returned by the driver in the struct
v4l2_bufferlengthfield for the single-planar API, and the same value as returned by the driver in the structv4l2_planelengthfield for the multi-planar API.protThe
protargument describes the desired memory protection. Regardless of the device type and the direction of data exchange it should be set toPROT_READ|PROT_WRITE, permitting read and write access to image buffers. Drivers should support at least this combination of flags.Note
The Linux
videobufkernel module, which is used by some drivers supports onlyPROT_READ|PROT_WRITE. When the driver does not support the desired protection, themmap()function fails.Device memory accesses (e. g. the memory on a graphics card with video capturing hardware) may incur a performance penalty compared to main memory accesses, or reads may be significantly slower than writes or vice versa. Other I/O methods may be more efficient in such case.
flagsThe
flagsparameter specifies the type of the mapped object, mapping options and whether modifications made to the mapped copy of the page are private to the process or are to be shared with other references.MAP_FIXEDrequests that the driver selects no other address than the one specified. If the specified address cannot be used,mmap()will fail. IfMAP_FIXEDis specified,startmust be a multiple of the pagesize. Use of this option is discouraged.One of the
MAP_SHAREDorMAP_PRIVATEflags must be set.MAP_SHAREDallows applications to share the mapped memory with other (e. g. child-) processes.Note
The Linux
videobufmodule which is used by some drivers supports onlyMAP_SHARED.MAP_PRIVATErequests copy-on-write semantics. V4L2 applications should not set theMAP_PRIVATE,MAP_DENYWRITE,MAP_EXECUTABLEorMAP_ANONflags.fdFile descriptor returned by
open().offsetOffset of the buffer in device memory. This must be the same value as returned by the driver in the struct
v4l2_buffermunionoffsetfield for the single-planar API, and the same value as returned by the driver in the structv4l2_planemunionmem_offsetfield for the multi-planar API.
7.66.4. Description¶
The mmap() function asks to map length bytes starting at
offset in the memory of the device specified by fd into the
application address space, preferably at address start. This latter
address is a hint only, and is usually specified as 0.
Suitable length and offset parameters are queried with the ioctl VIDIOC_QUERYBUF ioctl. Buffers must be allocated with the ioctl VIDIOC_REQBUFS ioctl before they can be queried.
To unmap buffers the munmap() function is used.
7.66.5. Return Value¶
On success mmap() returns a pointer to the mapped buffer. On
error MAP_FAILED (-1) is returned, and the errno variable is set
appropriately. Possible error codes are:
- EBADF
fdis not a valid file descriptor.- EACCES
fdis not open for reading and writing.- EINVAL
The
startorlengthoroffsetare not suitable. (E. g. they are too large, or not aligned on aPAGESIZEboundary.)The
flagsorprotvalue is not supported.No buffers have been allocated with the ioctl VIDIOC_REQBUFS ioctl.
- ENOMEM
Not enough physical or virtual memory was available to complete the request.