1059b1c5bSMauro Carvalho Chehab.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 2*407e84cdSMauro Carvalho Chehab.. c:namespace:: V4L 354f38fcaSMauro Carvalho Chehab 454f38fcaSMauro Carvalho Chehab.. _open: 554f38fcaSMauro Carvalho Chehab 654f38fcaSMauro Carvalho Chehab*************************** 754f38fcaSMauro Carvalho ChehabOpening and Closing Devices 854f38fcaSMauro Carvalho Chehab*************************** 954f38fcaSMauro Carvalho Chehab 10cfe9e707SMauro Carvalho Chehab.. _v4l2_hardware_control: 11cfe9e707SMauro Carvalho Chehab 12cfe9e707SMauro Carvalho ChehabControlling a hardware peripheral via V4L2 13cfe9e707SMauro Carvalho Chehab========================================== 14cfe9e707SMauro Carvalho Chehab 15cfe9e707SMauro Carvalho ChehabHardware that is supported using the V4L2 uAPI often consists of multiple 16cfe9e707SMauro Carvalho Chehabdevices or peripherals, each of which have their own driver. 17cfe9e707SMauro Carvalho Chehab 18cfe9e707SMauro Carvalho ChehabThe bridge driver exposes one or more V4L2 device nodes 19cfe9e707SMauro Carvalho Chehab(see :ref:`v4l2_device_naming`). 20cfe9e707SMauro Carvalho Chehab 21cfe9e707SMauro Carvalho ChehabThere are other drivers providing support for other components of 22cfe9e707SMauro Carvalho Chehabthe hardware, which may also expose device nodes, called V4L2 sub-devices. 23cfe9e707SMauro Carvalho Chehab 24cfe9e707SMauro Carvalho ChehabWhen such V4L2 sub-devices are exposed, they allow controlling those 25cfe9e707SMauro Carvalho Chehabother hardware components - usually connected via a serial bus (like 26cfe9e707SMauro Carvalho ChehabI²C, SMBus or SPI). Depending on the bridge driver, those sub-devices 27cfe9e707SMauro Carvalho Chehabcan be controlled indirectly via the bridge driver or explicitly via 28cfe9e707SMauro Carvalho Chehabthe :ref:`Media Controller <media_controller>` and via the 29cfe9e707SMauro Carvalho Chehab:ref:`V4L2 sub-devices <subdev>`. 30cfe9e707SMauro Carvalho Chehab 31cfe9e707SMauro Carvalho ChehabThe devices that require the use of the 32cfe9e707SMauro Carvalho Chehab:ref:`Media Controller <media_controller>` are called **MC-centric** 33cfe9e707SMauro Carvalho Chehabdevices. The devices that are fully controlled via V4L2 device nodes 34cfe9e707SMauro Carvalho Chehabare called **video-node-centric**. 35cfe9e707SMauro Carvalho Chehab 36cfe9e707SMauro Carvalho ChehabUserspace can check if a V4L2 hardware peripheral is MC-centric by 37cfe9e707SMauro Carvalho Chehabcalling :ref:`VIDIOC_QUERYCAP` and checking the 38cfe9e707SMauro Carvalho Chehab:ref:`device_caps field <device-capabilities>`. 39cfe9e707SMauro Carvalho Chehab 40cfe9e707SMauro Carvalho ChehabIf the device returns ``V4L2_CAP_IO_MC`` flag at ``device_caps``, 41cfe9e707SMauro Carvalho Chehabthen it is MC-centric, otherwise, it is video-node-centric. 42cfe9e707SMauro Carvalho Chehab 43cfe9e707SMauro Carvalho ChehabIt is required for MC-centric drivers to identify the V4L2 44cfe9e707SMauro Carvalho Chehabsub-devices and to configure the pipelines via the 45cfe9e707SMauro Carvalho Chehab:ref:`media controller API <media_controller>` before using the peripheral. 46cfe9e707SMauro Carvalho ChehabAlso, the sub-devices' configuration shall be controlled via the 47cfe9e707SMauro Carvalho Chehab:ref:`sub-device API <subdev>`. 48cfe9e707SMauro Carvalho Chehab 49cfe9e707SMauro Carvalho Chehab.. note:: 50cfe9e707SMauro Carvalho Chehab 51cfe9e707SMauro Carvalho Chehab A video-node-centric may still provide media-controller and 52cfe9e707SMauro Carvalho Chehab sub-device interfaces as well. 53cfe9e707SMauro Carvalho Chehab 54cfe9e707SMauro Carvalho Chehab However, in that case the media-controller and the sub-device 55cfe9e707SMauro Carvalho Chehab interfaces are read-only and just provide information about the 56cfe9e707SMauro Carvalho Chehab device. The actual configuration is done via the video nodes. 5754f38fcaSMauro Carvalho Chehab 58374d1432SMauro Carvalho Chehab.. _v4l2_device_naming: 59374d1432SMauro Carvalho Chehab 60374d1432SMauro Carvalho ChehabV4L2 Device Node Naming 61374d1432SMauro Carvalho Chehab======================= 6254f38fcaSMauro Carvalho Chehab 6354f38fcaSMauro Carvalho ChehabV4L2 drivers are implemented as kernel modules, loaded manually by the 6454f38fcaSMauro Carvalho Chehabsystem administrator or automatically when a device is first discovered. 65374d1432SMauro Carvalho ChehabThe driver modules plug into the ``videodev`` kernel module. It provides 6654f38fcaSMauro Carvalho Chehabhelper functions and a common application interface specified in this 6754f38fcaSMauro Carvalho Chehabdocument. 6854f38fcaSMauro Carvalho Chehab 6954f38fcaSMauro Carvalho ChehabEach driver thus loaded registers one or more device nodes with major 703c5f9362SMauro Carvalho Chehabnumber 81. Minor numbers are allocated dynamically unless the kernel 713c5f9362SMauro Carvalho Chehabis compiled with the kernel option CONFIG_VIDEO_FIXED_MINOR_RANGES. 723c5f9362SMauro Carvalho ChehabIn that case minor numbers are allocated in ranges depending on the 733c5f9362SMauro Carvalho Chehabdevice node type. 7454f38fcaSMauro Carvalho Chehab 75374d1432SMauro Carvalho ChehabThe device nodes supported by the Video4Linux subsystem are: 76374d1432SMauro Carvalho Chehab 77374d1432SMauro Carvalho Chehab======================== ==================================================== 78374d1432SMauro Carvalho ChehabDefault device node name Usage 79374d1432SMauro Carvalho Chehab======================== ==================================================== 80374d1432SMauro Carvalho Chehab``/dev/videoX`` Video and metadata for capture/output devices 81374d1432SMauro Carvalho Chehab``/dev/vbiX`` Vertical blank data (i.e. closed captions, teletext) 82374d1432SMauro Carvalho Chehab``/dev/radioX`` Radio tuners and modulators 83374d1432SMauro Carvalho Chehab``/dev/swradioX`` Software Defined Radio tuners and modulators 84374d1432SMauro Carvalho Chehab``/dev/v4l-touchX`` Touch sensors 85374d1432SMauro Carvalho Chehab``/dev/v4l-subdevX`` Video sub-devices (used by sensors and other 86374d1432SMauro Carvalho Chehab components of the hardware peripheral)\ [#]_ 87374d1432SMauro Carvalho Chehab======================== ==================================================== 88374d1432SMauro Carvalho Chehab 89374d1432SMauro Carvalho ChehabWhere ``X`` is a non-negative integer. 90374d1432SMauro Carvalho Chehab 91374d1432SMauro Carvalho Chehab.. note:: 92374d1432SMauro Carvalho Chehab 93374d1432SMauro Carvalho Chehab 1. The actual device node name is system-dependent, as udev rules may apply. 94374d1432SMauro Carvalho Chehab 2. There is no guarantee that ``X`` will remain the same for the same 95374d1432SMauro Carvalho Chehab device, as the number depends on the device driver's probe order. 96374d1432SMauro Carvalho Chehab If you need an unique name, udev default rules produce 97374d1432SMauro Carvalho Chehab ``/dev/v4l/by-id/`` and ``/dev/v4l/by-path/`` directories containing 98374d1432SMauro Carvalho Chehab links that can be used uniquely to identify a V4L2 device node:: 99374d1432SMauro Carvalho Chehab 100374d1432SMauro Carvalho Chehab $ tree /dev/v4l 101374d1432SMauro Carvalho Chehab /dev/v4l 102374d1432SMauro Carvalho Chehab ├── by-id 103374d1432SMauro Carvalho Chehab │ └── usb-OmniVision._USB_Camera-B4.04.27.1-video-index0 -> ../../video0 104374d1432SMauro Carvalho Chehab └── by-path 105374d1432SMauro Carvalho Chehab └── pci-0000:00:14.0-usb-0:2:1.0-video-index0 -> ../../video0 106374d1432SMauro Carvalho Chehab 107374d1432SMauro Carvalho Chehab.. [#] **V4L2 sub-device nodes** (e. g. ``/dev/v4l-subdevX``) use a different 108374d1432SMauro Carvalho Chehab set of system calls, as covered at :ref:`subdev`. 109374d1432SMauro Carvalho Chehab 11054f38fcaSMauro Carvalho ChehabMany drivers support "video_nr", "radio_nr" or "vbi_nr" module 11154f38fcaSMauro Carvalho Chehaboptions to select specific video/radio/vbi node numbers. This allows the 11254f38fcaSMauro Carvalho Chehabuser to request that the device node is named e.g. /dev/video5 instead 11354f38fcaSMauro Carvalho Chehabof leaving it to chance. When the driver supports multiple devices of 11454f38fcaSMauro Carvalho Chehabthe same type more than one device node number can be assigned, 11554f38fcaSMauro Carvalho Chehabseparated by commas: 11654f38fcaSMauro Carvalho Chehab 11754f38fcaSMauro Carvalho Chehab.. code-block:: none 11854f38fcaSMauro Carvalho Chehab 11954f38fcaSMauro Carvalho Chehab # modprobe mydriver video_nr=0,1 radio_nr=0,1 12054f38fcaSMauro Carvalho Chehab 12154f38fcaSMauro Carvalho ChehabIn ``/etc/modules.conf`` this may be written as: 12254f38fcaSMauro Carvalho Chehab 12354f38fcaSMauro Carvalho Chehab:: 12454f38fcaSMauro Carvalho Chehab 12554f38fcaSMauro Carvalho Chehab options mydriver video_nr=0,1 radio_nr=0,1 12654f38fcaSMauro Carvalho Chehab 12754f38fcaSMauro Carvalho ChehabWhen no device node number is given as module option the driver supplies 12854f38fcaSMauro Carvalho Chehaba default. 12954f38fcaSMauro Carvalho Chehab 13054f38fcaSMauro Carvalho ChehabNormally udev will create the device nodes in /dev automatically for 13154f38fcaSMauro Carvalho Chehabyou. If udev is not installed, then you need to enable the 13254f38fcaSMauro Carvalho ChehabCONFIG_VIDEO_FIXED_MINOR_RANGES kernel option in order to be able to 13354f38fcaSMauro Carvalho Chehabcorrectly relate a minor number to a device node number. I.e., you need 13454f38fcaSMauro Carvalho Chehabto be certain that minor number 5 maps to device node name video5. With 13554f38fcaSMauro Carvalho Chehabthis kernel option different device types have different minor number 13654f38fcaSMauro Carvalho Chehabranges. These ranges are listed in :ref:`devices`. 13754f38fcaSMauro Carvalho Chehab 13854f38fcaSMauro Carvalho ChehabThe creation of character special files (with mknod) is a privileged 13954f38fcaSMauro Carvalho Chehaboperation and devices cannot be opened by major and minor number. That 14054f38fcaSMauro Carvalho Chehabmeans applications cannot *reliably* scan for loaded or installed 14154f38fcaSMauro Carvalho Chehabdrivers. The user must enter a device name, or the application can try 14254f38fcaSMauro Carvalho Chehabthe conventional device names. 14354f38fcaSMauro Carvalho Chehab 14454f38fcaSMauro Carvalho Chehab.. _related: 14554f38fcaSMauro Carvalho Chehab 14654f38fcaSMauro Carvalho ChehabRelated Devices 14754f38fcaSMauro Carvalho Chehab=============== 14854f38fcaSMauro Carvalho Chehab 14954f38fcaSMauro Carvalho ChehabDevices can support several functions. For example video capturing, VBI 15054f38fcaSMauro Carvalho Chehabcapturing and radio support. 15154f38fcaSMauro Carvalho Chehab 152cfe9e707SMauro Carvalho ChehabThe V4L2 API creates different V4L2 device nodes for each of these functions. 15354f38fcaSMauro Carvalho Chehab 15454f38fcaSMauro Carvalho ChehabThe V4L2 API was designed with the idea that one device node could 15554f38fcaSMauro Carvalho Chehabsupport all functions. However, in practice this never worked: this 15654f38fcaSMauro Carvalho Chehab'feature' was never used by applications and many drivers did not 15754f38fcaSMauro Carvalho Chehabsupport it and if they did it was certainly never tested. In addition, 15854f38fcaSMauro Carvalho Chehabswitching a device node between different functions only works when 15954f38fcaSMauro Carvalho Chehabusing the streaming I/O API, not with the 160*407e84cdSMauro Carvalho Chehab:c:func:`read()`/\ :c:func:`write()` API. 16154f38fcaSMauro Carvalho Chehab 162cfe9e707SMauro Carvalho ChehabToday each V4L2 device node supports just one function. 16354f38fcaSMauro Carvalho Chehab 16454f38fcaSMauro Carvalho ChehabBesides video input or output the hardware may also support audio 16554f38fcaSMauro Carvalho Chehabsampling or playback. If so, these functions are implemented as ALSA PCM 16654f38fcaSMauro Carvalho Chehabdevices with optional ALSA audio mixer devices. 16754f38fcaSMauro Carvalho Chehab 16854f38fcaSMauro Carvalho ChehabOne problem with all these devices is that the V4L2 API makes no 169cfe9e707SMauro Carvalho Chehabprovisions to find these related V4L2 device nodes. Some really complex 170cfe9e707SMauro Carvalho Chehabhardware use the Media Controller (see :ref:`media_controller`) which can 171cfe9e707SMauro Carvalho Chehabbe used for this purpose. But several drivers do not use it, and while some 172cfe9e707SMauro Carvalho Chehabcode exists that uses sysfs to discover related V4L2 device nodes (see 17354f38fcaSMauro Carvalho Chehablibmedia_dev in the 17454f38fcaSMauro Carvalho Chehab`v4l-utils <http://git.linuxtv.org/cgit.cgi/v4l-utils.git/>`__ git 17554f38fcaSMauro Carvalho Chehabrepository), there is no library yet that can provide a single API 17654f38fcaSMauro Carvalho Chehabtowards both Media Controller-based devices and devices that do not use 17754f38fcaSMauro Carvalho Chehabthe Media Controller. If you want to work on this please write to the 17854f38fcaSMauro Carvalho Chehablinux-media mailing list: 17954f38fcaSMauro Carvalho Chehab`https://linuxtv.org/lists.php <https://linuxtv.org/lists.php>`__. 18054f38fcaSMauro Carvalho Chehab 18154f38fcaSMauro Carvalho ChehabMultiple Opens 18254f38fcaSMauro Carvalho Chehab============== 18354f38fcaSMauro Carvalho Chehab 18454f38fcaSMauro Carvalho ChehabV4L2 devices can be opened more than once. [#f1]_ When this is supported 18554f38fcaSMauro Carvalho Chehabby the driver, users can for example start a "panel" application to 18654f38fcaSMauro Carvalho Chehabchange controls like brightness or audio volume, while another 18754f38fcaSMauro Carvalho Chehabapplication captures video and audio. In other words, panel applications 18854f38fcaSMauro Carvalho Chehabare comparable to an ALSA audio mixer application. Just opening a V4L2 18954f38fcaSMauro Carvalho Chehabdevice should not change the state of the device. [#f2]_ 19054f38fcaSMauro Carvalho Chehab 19154f38fcaSMauro Carvalho ChehabOnce an application has allocated the memory buffers needed for 19254f38fcaSMauro Carvalho Chehabstreaming data (by calling the :ref:`VIDIOC_REQBUFS` 19354f38fcaSMauro Carvalho Chehabor :ref:`VIDIOC_CREATE_BUFS` ioctls, or 194*407e84cdSMauro Carvalho Chehabimplicitly by calling the :c:func:`read()` or 195*407e84cdSMauro Carvalho Chehab:c:func:`write()` functions) that application (filehandle) 19654f38fcaSMauro Carvalho Chehabbecomes the owner of the device. It is no longer allowed to make changes 19754f38fcaSMauro Carvalho Chehabthat would affect the buffer sizes (e.g. by calling the 19854f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl) and other applications are 19954f38fcaSMauro Carvalho Chehabno longer allowed to allocate buffers or start or stop streaming. The 20054f38fcaSMauro Carvalho ChehabEBUSY error code will be returned instead. 20154f38fcaSMauro Carvalho Chehab 20254f38fcaSMauro Carvalho ChehabMerely opening a V4L2 device does not grant exclusive access. [#f3]_ 20354f38fcaSMauro Carvalho ChehabInitiating data exchange however assigns the right to read or write the 20454f38fcaSMauro Carvalho Chehabrequested type of data, and to change related properties, to this file 20554f38fcaSMauro Carvalho Chehabdescriptor. Applications can request additional access privileges using 20654f38fcaSMauro Carvalho Chehabthe priority mechanism described in :ref:`app-pri`. 20754f38fcaSMauro Carvalho Chehab 20854f38fcaSMauro Carvalho ChehabShared Data Streams 20954f38fcaSMauro Carvalho Chehab=================== 21054f38fcaSMauro Carvalho Chehab 21154f38fcaSMauro Carvalho ChehabV4L2 drivers should not support multiple applications reading or writing 21254f38fcaSMauro Carvalho Chehabthe same data stream on a device by copying buffers, time multiplexing 21354f38fcaSMauro Carvalho Chehabor similar means. This is better handled by a proxy application in user 21454f38fcaSMauro Carvalho Chehabspace. 21554f38fcaSMauro Carvalho Chehab 21654f38fcaSMauro Carvalho ChehabFunctions 21754f38fcaSMauro Carvalho Chehab========= 21854f38fcaSMauro Carvalho Chehab 21954f38fcaSMauro Carvalho ChehabTo open and close V4L2 devices applications use the 220*407e84cdSMauro Carvalho Chehab:c:func:`open()` and :c:func:`close()` function, 22154f38fcaSMauro Carvalho Chehabrespectively. Devices are programmed using the 22254f38fcaSMauro Carvalho Chehab:ref:`ioctl() <func-ioctl>` function as explained in the following 22354f38fcaSMauro Carvalho Chehabsections. 22454f38fcaSMauro Carvalho Chehab 22554f38fcaSMauro Carvalho Chehab.. [#f1] 22654f38fcaSMauro Carvalho Chehab There are still some old and obscure drivers that have not been 22754f38fcaSMauro Carvalho Chehab updated to allow for multiple opens. This implies that for such 228*407e84cdSMauro Carvalho Chehab drivers :c:func:`open()` can return an ``EBUSY`` error code 22954f38fcaSMauro Carvalho Chehab when the device is already in use. 23054f38fcaSMauro Carvalho Chehab 23154f38fcaSMauro Carvalho Chehab.. [#f2] 23254f38fcaSMauro Carvalho Chehab Unfortunately, opening a radio device often switches the state of the 23354f38fcaSMauro Carvalho Chehab device to radio mode in many drivers. This behavior should be fixed 23454f38fcaSMauro Carvalho Chehab eventually as it violates the V4L2 specification. 23554f38fcaSMauro Carvalho Chehab 23654f38fcaSMauro Carvalho Chehab.. [#f3] 23754f38fcaSMauro Carvalho Chehab Drivers could recognize the ``O_EXCL`` open flag. Presently this is 23854f38fcaSMauro Carvalho Chehab not required, so applications cannot know if it really works. 239