1.1. Opening and Closing Devices¶
1.1.1. Controlling a hardware peripheral via V4L2¶
Hardware that is supported using the V4L2 uAPI often consists of multiple devices or peripherals, each of which have their own driver.
The bridge driver exposes one or more V4L2 device nodes (see V4L2 Device Node Naming).
There are other drivers providing support for other components of the hardware, which may also expose device nodes, called V4L2 sub-devices.
When such V4L2 sub-devices are exposed, they allow controlling those other hardware components - usually connected via a serial bus (like I²C, SMBus or SPI). Depending on the bridge driver, those sub-devices can be controlled indirectly via the bridge driver or explicitly via the Media Controller and via the V4L2 sub-devices.
The devices that require the use of the Media Controller are called MC-centric devices. The devices that are fully controlled via V4L2 device nodes are called video-node-centric.
If the device returns
V4L2_CAP_IO_MC flag at
then it is MC-centric, otherwise, it is video-node-centric.
It is required for MC-centric drivers to identify the V4L2 sub-devices and to configure the pipelines via the media controller API before using the peripheral. Also, the sub-devices' configuration shall be controlled via the sub-device API.
A video-node-centric may still provide media-controller and sub-device interfaces as well.
However, in that case the media-controller and the sub-device interfaces are read-only and just provide information about the device. The actual configuration is done via the video nodes.
1.1.2. V4L2 Device Node Naming¶
V4L2 drivers are implemented as kernel modules, loaded manually by the
system administrator or automatically when a device is first discovered.
The driver modules plug into the
videodev kernel module. It provides
helper functions and a common application interface specified in this
Each driver thus loaded registers one or more device nodes with major number 81. Minor numbers are allocated dynamically unless the kernel is compiled with the kernel option CONFIG_VIDEO_FIXED_MINOR_RANGES. In that case minor numbers are allocated in ranges depending on the device node type.
The device nodes supported by the Video4Linux subsystem are:
Default device node name
Video and metadata for capture/output devices
Vertical blank data (i.e. closed captions, teletext)
Radio tuners and modulators
Software Defined Radio tuners and modulators
Video sub-devices (used by sensors and other components of the hardware peripheral)1
X is a non-negative integer.
The actual device node name is system-dependent, as udev rules may apply.
There is no guarantee that
Xwill remain the same for the same device, as the number depends on the device driver's probe order. If you need an unique name, udev default rules produce
/dev/v4l/by-path/directories containing links that can be used uniquely to identify a V4L2 device node:
$ tree /dev/v4l /dev/v4l ├── by-id │ └── usb-OmniVision._USB_Camera-B4.04.27.1-video-index0 -> ../../video0 └── by-path └── pci-0000:00:14.0-usb-0:2:1.0-video-index0 -> ../../video0
V4L2 sub-device nodes (e. g.
/dev/v4l-subdevX) use a different set of system calls, as covered at Sub-device Interface.
Many drivers support "video_nr", "radio_nr" or "vbi_nr" module options to select specific video/radio/vbi node numbers. This allows the user to request that the device node is named e.g. /dev/video5 instead of leaving it to chance. When the driver supports multiple devices of the same type more than one device node number can be assigned, separated by commas:
# modprobe mydriver video_nr=0,1 radio_nr=0,1
/etc/modules.conf this may be written as:
options mydriver video_nr=0,1 radio_nr=0,1
When no device node number is given as module option the driver supplies a default.
Normally udev will create the device nodes in /dev automatically for you. If udev is not installed, then you need to enable the CONFIG_VIDEO_FIXED_MINOR_RANGES kernel option in order to be able to correctly relate a minor number to a device node number. I.e., you need to be certain that minor number 5 maps to device node name video5. With this kernel option different device types have different minor number ranges. These ranges are listed in Interfaces.
The creation of character special files (with mknod) is a privileged operation and devices cannot be opened by major and minor number. That means applications cannot reliably scan for loaded or installed drivers. The user must enter a device name, or the application can try the conventional device names.
1.1.4. Multiple Opens¶
V4L2 devices can be opened more than once. 2 When this is supported by the driver, users can for example start a "panel" application to change controls like brightness or audio volume, while another application captures video and audio. In other words, panel applications are comparable to an ALSA audio mixer application. Just opening a V4L2 device should not change the state of the device. 3
Once an application has allocated the memory buffers needed for
streaming data (by calling the ioctl VIDIOC_REQBUFS
or ioctl VIDIOC_CREATE_BUFS ioctls, or
implicitly by calling the
write() functions) that application (filehandle)
becomes the owner of the device. It is no longer allowed to make changes
that would affect the buffer sizes (e.g. by calling the
VIDIOC_S_FMT ioctl) and other applications are
no longer allowed to allocate buffers or start or stop streaming. The
EBUSY error code will be returned instead.
Merely opening a V4L2 device does not grant exclusive access. 4 Initiating data exchange however assigns the right to read or write the requested type of data, and to change related properties, to this file descriptor. Applications can request additional access privileges using the priority mechanism described in Application Priority.
There are still some old and obscure drivers that have not been updated to allow for multiple opens. This implies that for such drivers
open()can return an
EBUSYerror code when the device is already in use.
Unfortunately, opening a radio device often switches the state of the device to radio mode in many drivers. This behavior should be fixed eventually as it violates the V4L2 specification.
Drivers could recognize the
O_EXCLopen flag. Presently this is not required, so applications cannot know if it really works.