open.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
  2. .. c:namespace:: V4L
  3. .. _open:
  4. ***************************
  5. Opening and Closing Devices
  6. ***************************
  7. .. _v4l2_hardware_control:
  8. Controlling a hardware peripheral via V4L2
  9. ==========================================
  10. Hardware that is supported using the V4L2 uAPI often consists of multiple
  11. devices or peripherals, each of which have their own driver.
  12. The bridge driver exposes one or more V4L2 device nodes
  13. (see :ref:`v4l2_device_naming`).
  14. There are other drivers providing support for other components of
  15. the hardware, which may also expose device nodes, called V4L2 sub-devices.
  16. When such V4L2 sub-devices are exposed, they allow controlling those
  17. other hardware components - usually connected via a serial bus (like
  18. I²C, SMBus or SPI). Depending on the bridge driver, those sub-devices
  19. can be controlled indirectly via the bridge driver or explicitly via
  20. the :ref:`Media Controller <media_controller>` and via the
  21. :ref:`V4L2 sub-devices <subdev>`.
  22. The devices that require the use of the
  23. :ref:`Media Controller <media_controller>` are called **MC-centric**
  24. devices. The devices that are fully controlled via V4L2 device nodes
  25. are called **video-node-centric**.
  26. Userspace can check if a V4L2 hardware peripheral is MC-centric by
  27. calling :ref:`VIDIOC_QUERYCAP` and checking the
  28. :ref:`device_caps field <device-capabilities>`.
  29. If the device returns ``V4L2_CAP_IO_MC`` flag at ``device_caps``,
  30. then it is MC-centric, otherwise, it is video-node-centric.
  31. It is required for MC-centric drivers to identify the V4L2
  32. sub-devices and to configure the pipelines via the
  33. :ref:`media controller API <media_controller>` before using the peripheral.
  34. Also, the sub-devices' configuration shall be controlled via the
  35. :ref:`sub-device API <subdev>`.
  36. .. note::
  37. A video-node-centric may still provide media-controller and
  38. sub-device interfaces as well.
  39. However, in that case the media-controller and the sub-device
  40. interfaces are read-only and just provide information about the
  41. device. The actual configuration is done via the video nodes.
  42. .. _v4l2_device_naming:
  43. V4L2 Device Node Naming
  44. =======================
  45. V4L2 drivers are implemented as kernel modules, loaded manually by the
  46. system administrator or automatically when a device is first discovered.
  47. The driver modules plug into the ``videodev`` kernel module. It provides
  48. helper functions and a common application interface specified in this
  49. document.
  50. Each driver thus loaded registers one or more device nodes with major
  51. number 81. Minor numbers are allocated dynamically unless the kernel
  52. is compiled with the kernel option CONFIG_VIDEO_FIXED_MINOR_RANGES.
  53. In that case minor numbers are allocated in ranges depending on the
  54. device node type.
  55. The device nodes supported by the Video4Linux subsystem are:
  56. ======================== ====================================================
  57. Default device node name Usage
  58. ======================== ====================================================
  59. ``/dev/videoX`` Video and metadata for capture/output devices
  60. ``/dev/vbiX`` Vertical blank data (i.e. closed captions, teletext)
  61. ``/dev/radioX`` Radio tuners and modulators
  62. ``/dev/swradioX`` Software Defined Radio tuners and modulators
  63. ``/dev/v4l-touchX`` Touch sensors
  64. ``/dev/v4l-subdevX`` Video sub-devices (used by sensors and other
  65. components of the hardware peripheral)\ [#]_
  66. ======================== ====================================================
  67. Where ``X`` is a non-negative integer.
  68. .. note::
  69. 1. The actual device node name is system-dependent, as udev rules may apply.
  70. 2. There is no guarantee that ``X`` will remain the same for the same
  71. device, as the number depends on the device driver's probe order.
  72. If you need an unique name, udev default rules produce
  73. ``/dev/v4l/by-id/`` and ``/dev/v4l/by-path/`` directories containing
  74. links that can be used uniquely to identify a V4L2 device node::
  75. $ tree /dev/v4l
  76. /dev/v4l
  77. ├── by-id
  78. │   └── usb-OmniVision._USB_Camera-B4.04.27.1-video-index0 -> ../../video0
  79. └── by-path
  80. └── pci-0000:00:14.0-usb-0:2:1.0-video-index0 -> ../../video0
  81. .. [#] **V4L2 sub-device nodes** (e. g. ``/dev/v4l-subdevX``) use a different
  82. set of system calls, as covered at :ref:`subdev`.
  83. Many drivers support "video_nr", "radio_nr" or "vbi_nr" module
  84. options to select specific video/radio/vbi node numbers. This allows the
  85. user to request that the device node is named e.g. /dev/video5 instead
  86. of leaving it to chance. When the driver supports multiple devices of
  87. the same type more than one device node number can be assigned,
  88. separated by commas:
  89. .. code-block:: none
  90. # modprobe mydriver video_nr=0,1 radio_nr=0,1
  91. In ``/etc/modules.conf`` this may be written as:
  92. ::
  93. options mydriver video_nr=0,1 radio_nr=0,1
  94. When no device node number is given as module option the driver supplies
  95. a default.
  96. Normally udev will create the device nodes in /dev automatically for
  97. you. If udev is not installed, then you need to enable the
  98. CONFIG_VIDEO_FIXED_MINOR_RANGES kernel option in order to be able to
  99. correctly relate a minor number to a device node number. I.e., you need
  100. to be certain that minor number 5 maps to device node name video5. With
  101. this kernel option different device types have different minor number
  102. ranges. These ranges are listed in :ref:`devices`.
  103. The creation of character special files (with mknod) is a privileged
  104. operation and devices cannot be opened by major and minor number. That
  105. means applications cannot *reliably* scan for loaded or installed
  106. drivers. The user must enter a device name, or the application can try
  107. the conventional device names.
  108. .. _related:
  109. Related Devices
  110. ===============
  111. Devices can support several functions. For example video capturing, VBI
  112. capturing and radio support.
  113. The V4L2 API creates different V4L2 device nodes for each of these functions.
  114. The V4L2 API was designed with the idea that one device node could
  115. support all functions. However, in practice this never worked: this
  116. 'feature' was never used by applications and many drivers did not
  117. support it and if they did it was certainly never tested. In addition,
  118. switching a device node between different functions only works when
  119. using the streaming I/O API, not with the
  120. :c:func:`read()`/\ :c:func:`write()` API.
  121. Today each V4L2 device node supports just one function.
  122. Besides video input or output the hardware may also support audio
  123. sampling or playback. If so, these functions are implemented as ALSA PCM
  124. devices with optional ALSA audio mixer devices.
  125. One problem with all these devices is that the V4L2 API makes no
  126. provisions to find these related V4L2 device nodes. Some really complex
  127. hardware use the Media Controller (see :ref:`media_controller`) which can
  128. be used for this purpose. But several drivers do not use it, and while some
  129. code exists that uses sysfs to discover related V4L2 device nodes (see
  130. libmedia_dev in the
  131. `v4l-utils <http://git.linuxtv.org/cgit.cgi/v4l-utils.git/>`__ git
  132. repository), there is no library yet that can provide a single API
  133. towards both Media Controller-based devices and devices that do not use
  134. the Media Controller. If you want to work on this please write to the
  135. linux-media mailing list:
  136. `https://linuxtv.org/lists.php <https://linuxtv.org/lists.php>`__.
  137. Multiple Opens
  138. ==============
  139. V4L2 devices can be opened more than once. [#f1]_ When this is supported
  140. by the driver, users can for example start a "panel" application to
  141. change controls like brightness or audio volume, while another
  142. application captures video and audio. In other words, panel applications
  143. are comparable to an ALSA audio mixer application. Just opening a V4L2
  144. device should not change the state of the device. [#f2]_
  145. Once an application has allocated the memory buffers needed for
  146. streaming data (by calling the :ref:`VIDIOC_REQBUFS`
  147. or :ref:`VIDIOC_CREATE_BUFS` ioctls, or
  148. implicitly by calling the :c:func:`read()` or
  149. :c:func:`write()` functions) that application (filehandle)
  150. becomes the owner of the device. It is no longer allowed to make changes
  151. that would affect the buffer sizes (e.g. by calling the
  152. :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl) and other applications are
  153. no longer allowed to allocate buffers or start or stop streaming. The
  154. EBUSY error code will be returned instead.
  155. Merely opening a V4L2 device does not grant exclusive access. [#f3]_
  156. Initiating data exchange however assigns the right to read or write the
  157. requested type of data, and to change related properties, to this file
  158. descriptor. Applications can request additional access privileges using
  159. the priority mechanism described in :ref:`app-pri`.
  160. Shared Data Streams
  161. ===================
  162. V4L2 drivers should not support multiple applications reading or writing
  163. the same data stream on a device by copying buffers, time multiplexing
  164. or similar means. This is better handled by a proxy application in user
  165. space.
  166. Functions
  167. =========
  168. To open and close V4L2 devices applications use the
  169. :c:func:`open()` and :c:func:`close()` function,
  170. respectively. Devices are programmed using the
  171. :ref:`ioctl() <func-ioctl>` function as explained in the following
  172. sections.
  173. .. [#f1]
  174. There are still some old and obscure drivers that have not been
  175. updated to allow for multiple opens. This implies that for such
  176. drivers :c:func:`open()` can return an ``EBUSY`` error code
  177. when the device is already in use.
  178. .. [#f2]
  179. Unfortunately, opening a radio device often switches the state of the
  180. device to radio mode in many drivers. This behavior should be fixed
  181. eventually as it violates the V4L2 specification.
  182. .. [#f3]
  183. Drivers could recognize the ``O_EXCL`` open flag. Presently this is
  184. not required, so applications cannot know if it really works.