1.. -*- coding: utf-8; mode: rst -*- 2 3.. _open: 4 5*************************** 6Opening and Closing Devices 7*************************** 8 9 10Device Naming 11============= 12 13V4L2 drivers are implemented as kernel modules, loaded manually by the 14system administrator or automatically when a device is first discovered. 15The driver modules plug into the "videodev" kernel module. It provides 16helper functions and a common application interface specified in this 17document. 18 19Each driver thus loaded registers one or more device nodes with major 20number 81 and a minor number between 0 and 255. Minor numbers are 21allocated dynamically unless the kernel is compiled with the kernel 22option CONFIG_VIDEO_FIXED_MINOR_RANGES. In that case minor numbers 23are allocated in ranges depending on the device node type (video, radio, 24etc.). 25 26Many drivers support "video_nr", "radio_nr" or "vbi_nr" module 27options to select specific video/radio/vbi node numbers. This allows the 28user to request that the device node is named e.g. /dev/video5 instead 29of leaving it to chance. When the driver supports multiple devices of 30the same type more than one device node number can be assigned, 31separated by commas: 32 33.. code-block:: none 34 35 # modprobe mydriver video_nr=0,1 radio_nr=0,1 36 37In ``/etc/modules.conf`` this may be written as: 38 39:: 40 41 options mydriver video_nr=0,1 radio_nr=0,1 42 43When no device node number is given as module option the driver supplies 44a default. 45 46Normally udev will create the device nodes in /dev automatically for 47you. If udev is not installed, then you need to enable the 48CONFIG_VIDEO_FIXED_MINOR_RANGES kernel option in order to be able to 49correctly relate a minor number to a device node number. I.e., you need 50to be certain that minor number 5 maps to device node name video5. With 51this kernel option different device types have different minor number 52ranges. These ranges are listed in :ref:`devices`. 53 54The creation of character special files (with mknod) is a privileged 55operation and devices cannot be opened by major and minor number. That 56means applications cannot *reliable* scan for loaded or installed 57drivers. The user must enter a device name, or the application can try 58the conventional device names. 59 60 61.. _related: 62 63Related Devices 64=============== 65 66Devices can support several functions. For example video capturing, VBI 67capturing and radio support. 68 69The V4L2 API creates different nodes for each of these functions. 70 71The V4L2 API was designed with the idea that one device node could 72support all functions. However, in practice this never worked: this 73'feature' was never used by applications and many drivers did not 74support it and if they did it was certainly never tested. In addition, 75switching a device node between different functions only works when 76using the streaming I/O API, not with the 77:ref:`read() <func-read>`/\ :ref:`write() <func-write>` API. 78 79Today each device node supports just one function. 80 81Besides video input or output the hardware may also support audio 82sampling or playback. If so, these functions are implemented as ALSA PCM 83devices with optional ALSA audio mixer devices. 84 85One problem with all these devices is that the V4L2 API makes no 86provisions to find these related devices. Some really complex devices 87use the Media Controller (see :ref:`media_controller`) which can be 88used for this purpose. But most drivers do not use it, and while some 89code exists that uses sysfs to discover related devices (see 90libmedia_dev in the 91`v4l-utils <http://git.linuxtv.org/cgit.cgi/v4l-utils.git/>`__ git 92repository), there is no library yet that can provide a single API 93towards both Media Controller-based devices and devices that do not use 94the Media Controller. If you want to work on this please write to the 95linux-media mailing list: 96`https://linuxtv.org/lists.php <https://linuxtv.org/lists.php>`__. 97 98 99Multiple Opens 100============== 101 102V4L2 devices can be opened more than once. [#f1]_ When this is supported 103by the driver, users can for example start a "panel" application to 104change controls like brightness or audio volume, while another 105application captures video and audio. In other words, panel applications 106are comparable to an ALSA audio mixer application. Just opening a V4L2 107device should not change the state of the device. [#f2]_ 108 109Once an application has allocated the memory buffers needed for 110streaming data (by calling the :ref:`VIDIOC_REQBUFS` 111or :ref:`VIDIOC_CREATE_BUFS` ioctls, or 112implicitly by calling the :ref:`read() <func-read>` or 113:ref:`write() <func-write>` functions) that application (filehandle) 114becomes the owner of the device. It is no longer allowed to make changes 115that would affect the buffer sizes (e.g. by calling the 116:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl) and other applications are 117no longer allowed to allocate buffers or start or stop streaming. The 118EBUSY error code will be returned instead. 119 120Merely opening a V4L2 device does not grant exclusive access. [#f3]_ 121Initiating data exchange however assigns the right to read or write the 122requested type of data, and to change related properties, to this file 123descriptor. Applications can request additional access privileges using 124the priority mechanism described in :ref:`app-pri`. 125 126 127Shared Data Streams 128=================== 129 130V4L2 drivers should not support multiple applications reading or writing 131the same data stream on a device by copying buffers, time multiplexing 132or similar means. This is better handled by a proxy application in user 133space. 134 135 136Functions 137========= 138 139To open and close V4L2 devices applications use the 140:ref:`open() <func-open>` and :ref:`close() <func-close>` function, 141respectively. Devices are programmed using the 142:ref:`ioctl() <func-ioctl>` function as explained in the following 143sections. 144 145.. [#f1] 146 There are still some old and obscure drivers that have not been 147 updated to allow for multiple opens. This implies that for such 148 drivers :ref:`open() <func-open>` can return an ``EBUSY`` error code 149 when the device is already in use. 150 151.. [#f2] 152 Unfortunately, opening a radio device often switches the state of the 153 device to radio mode in many drivers. This behavior should be fixed 154 eventually as it violates the V4L2 specification. 155 156.. [#f3] 157 Drivers could recognize the ``O_EXCL`` open flag. Presently this is 158 not required, so applications cannot know if it really works. 159