open.rst 6.2 KB

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