xref: /OK3568_Linux_fs/kernel/Documentation/userspace-api/media/v4l/open.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2*4882a593Smuzhiyun.. c:namespace:: V4L
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun.. _open:
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun***************************
7*4882a593SmuzhiyunOpening and Closing Devices
8*4882a593Smuzhiyun***************************
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun.. _v4l2_hardware_control:
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunControlling a hardware peripheral via V4L2
13*4882a593Smuzhiyun==========================================
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunHardware that is supported using the V4L2 uAPI often consists of multiple
16*4882a593Smuzhiyundevices or peripherals, each of which have their own driver.
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunThe bridge driver exposes one or more V4L2 device nodes
19*4882a593Smuzhiyun(see :ref:`v4l2_device_naming`).
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunThere are other drivers providing support for other components of
22*4882a593Smuzhiyunthe hardware, which may also expose device nodes, called V4L2 sub-devices.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunWhen such V4L2 sub-devices are exposed, they allow controlling those
25*4882a593Smuzhiyunother hardware components - usually connected via a serial bus (like
26*4882a593SmuzhiyunI²C, SMBus or SPI). Depending on the bridge driver, those sub-devices
27*4882a593Smuzhiyuncan be controlled indirectly via the bridge driver or explicitly via
28*4882a593Smuzhiyunthe :ref:`Media Controller <media_controller>` and via the
29*4882a593Smuzhiyun:ref:`V4L2 sub-devices <subdev>`.
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunThe devices that require the use of the
32*4882a593Smuzhiyun:ref:`Media Controller <media_controller>` are called **MC-centric**
33*4882a593Smuzhiyundevices. The devices that are fully controlled via V4L2 device nodes
34*4882a593Smuzhiyunare called **video-node-centric**.
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunUserspace can check if a V4L2 hardware peripheral is MC-centric by
37*4882a593Smuzhiyuncalling :ref:`VIDIOC_QUERYCAP` and checking the
38*4882a593Smuzhiyun:ref:`device_caps field <device-capabilities>`.
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunIf the device returns ``V4L2_CAP_IO_MC`` flag at ``device_caps``,
41*4882a593Smuzhiyunthen it is MC-centric, otherwise, it is video-node-centric.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunIt is required for MC-centric drivers to identify the V4L2
44*4882a593Smuzhiyunsub-devices and to configure the pipelines via the
45*4882a593Smuzhiyun:ref:`media controller API <media_controller>` before using the peripheral.
46*4882a593SmuzhiyunAlso, the sub-devices' configuration shall be controlled via the
47*4882a593Smuzhiyun:ref:`sub-device API <subdev>`.
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun.. note::
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun   A video-node-centric may still provide media-controller and
52*4882a593Smuzhiyun   sub-device interfaces as well.
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun  However, in that case the media-controller and the sub-device
55*4882a593Smuzhiyun  interfaces are read-only and just provide information about the
56*4882a593Smuzhiyun  device. The actual configuration is done via the video nodes.
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun.. _v4l2_device_naming:
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunV4L2 Device Node Naming
61*4882a593Smuzhiyun=======================
62*4882a593Smuzhiyun
63*4882a593SmuzhiyunV4L2 drivers are implemented as kernel modules, loaded manually by the
64*4882a593Smuzhiyunsystem administrator or automatically when a device is first discovered.
65*4882a593SmuzhiyunThe driver modules plug into the ``videodev`` kernel module. It provides
66*4882a593Smuzhiyunhelper functions and a common application interface specified in this
67*4882a593Smuzhiyundocument.
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunEach driver thus loaded registers one or more device nodes with major
70*4882a593Smuzhiyunnumber 81. Minor numbers are allocated dynamically unless the kernel
71*4882a593Smuzhiyunis compiled with the kernel option CONFIG_VIDEO_FIXED_MINOR_RANGES.
72*4882a593SmuzhiyunIn that case minor numbers are allocated in ranges depending on the
73*4882a593Smuzhiyundevice node type.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunThe device nodes supported by the Video4Linux subsystem are:
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun======================== ====================================================
78*4882a593SmuzhiyunDefault device node name Usage
79*4882a593Smuzhiyun======================== ====================================================
80*4882a593Smuzhiyun``/dev/videoX``		 Video and metadata for capture/output devices
81*4882a593Smuzhiyun``/dev/vbiX``		 Vertical blank data (i.e. closed captions, teletext)
82*4882a593Smuzhiyun``/dev/radioX``		 Radio tuners and modulators
83*4882a593Smuzhiyun``/dev/swradioX``	 Software Defined Radio tuners and modulators
84*4882a593Smuzhiyun``/dev/v4l-touchX``	 Touch sensors
85*4882a593Smuzhiyun``/dev/v4l-subdevX``	 Video sub-devices (used by sensors and other
86*4882a593Smuzhiyun			 components of the hardware peripheral)\ [#]_
87*4882a593Smuzhiyun======================== ====================================================
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunWhere ``X`` is a non-negative integer.
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun.. note::
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun   1. The actual device node name is system-dependent, as udev rules may apply.
94*4882a593Smuzhiyun   2. There is no guarantee that ``X`` will remain the same for the same
95*4882a593Smuzhiyun      device, as the number depends on the device driver's probe order.
96*4882a593Smuzhiyun      If you need an unique name, udev default rules produce
97*4882a593Smuzhiyun      ``/dev/v4l/by-id/`` and ``/dev/v4l/by-path/`` directories containing
98*4882a593Smuzhiyun      links that can be used uniquely to identify a V4L2 device node::
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun	$ tree /dev/v4l
101*4882a593Smuzhiyun	/dev/v4l
102*4882a593Smuzhiyun	├── by-id
103*4882a593Smuzhiyun	│   └── usb-OmniVision._USB_Camera-B4.04.27.1-video-index0 -> ../../video0
104*4882a593Smuzhiyun	└── by-path
105*4882a593Smuzhiyun	    └── pci-0000:00:14.0-usb-0:2:1.0-video-index0 -> ../../video0
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun.. [#] **V4L2 sub-device nodes** (e. g. ``/dev/v4l-subdevX``) use a different
108*4882a593Smuzhiyun       set of system calls, as covered at :ref:`subdev`.
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunMany drivers support "video_nr", "radio_nr" or "vbi_nr" module
111*4882a593Smuzhiyunoptions to select specific video/radio/vbi node numbers. This allows the
112*4882a593Smuzhiyunuser to request that the device node is named e.g. /dev/video5 instead
113*4882a593Smuzhiyunof leaving it to chance. When the driver supports multiple devices of
114*4882a593Smuzhiyunthe same type more than one device node number can be assigned,
115*4882a593Smuzhiyunseparated by commas:
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun.. code-block:: none
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun   # modprobe mydriver video_nr=0,1 radio_nr=0,1
120*4882a593Smuzhiyun
121*4882a593SmuzhiyunIn ``/etc/modules.conf`` this may be written as:
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun::
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun    options mydriver video_nr=0,1 radio_nr=0,1
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunWhen no device node number is given as module option the driver supplies
128*4882a593Smuzhiyuna default.
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunNormally udev will create the device nodes in /dev automatically for
131*4882a593Smuzhiyunyou. If udev is not installed, then you need to enable the
132*4882a593SmuzhiyunCONFIG_VIDEO_FIXED_MINOR_RANGES kernel option in order to be able to
133*4882a593Smuzhiyuncorrectly relate a minor number to a device node number. I.e., you need
134*4882a593Smuzhiyunto be certain that minor number 5 maps to device node name video5. With
135*4882a593Smuzhiyunthis kernel option different device types have different minor number
136*4882a593Smuzhiyunranges. These ranges are listed in :ref:`devices`.
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunThe creation of character special files (with mknod) is a privileged
139*4882a593Smuzhiyunoperation and devices cannot be opened by major and minor number. That
140*4882a593Smuzhiyunmeans applications cannot *reliably* scan for loaded or installed
141*4882a593Smuzhiyundrivers. The user must enter a device name, or the application can try
142*4882a593Smuzhiyunthe conventional device names.
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun.. _related:
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunRelated Devices
147*4882a593Smuzhiyun===============
148*4882a593Smuzhiyun
149*4882a593SmuzhiyunDevices can support several functions. For example video capturing, VBI
150*4882a593Smuzhiyuncapturing and radio support.
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunThe V4L2 API creates different V4L2 device nodes for each of these functions.
153*4882a593Smuzhiyun
154*4882a593SmuzhiyunThe V4L2 API was designed with the idea that one device node could
155*4882a593Smuzhiyunsupport all functions. However, in practice this never worked: this
156*4882a593Smuzhiyun'feature' was never used by applications and many drivers did not
157*4882a593Smuzhiyunsupport it and if they did it was certainly never tested. In addition,
158*4882a593Smuzhiyunswitching a device node between different functions only works when
159*4882a593Smuzhiyunusing the streaming I/O API, not with the
160*4882a593Smuzhiyun:c:func:`read()`/\ :c:func:`write()` API.
161*4882a593Smuzhiyun
162*4882a593SmuzhiyunToday each V4L2 device node supports just one function.
163*4882a593Smuzhiyun
164*4882a593SmuzhiyunBesides video input or output the hardware may also support audio
165*4882a593Smuzhiyunsampling or playback. If so, these functions are implemented as ALSA PCM
166*4882a593Smuzhiyundevices with optional ALSA audio mixer devices.
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunOne problem with all these devices is that the V4L2 API makes no
169*4882a593Smuzhiyunprovisions to find these related V4L2 device nodes. Some really complex
170*4882a593Smuzhiyunhardware use the Media Controller (see :ref:`media_controller`) which can
171*4882a593Smuzhiyunbe used for this purpose. But several drivers do not use it, and while some
172*4882a593Smuzhiyuncode exists that uses sysfs to discover related V4L2 device nodes (see
173*4882a593Smuzhiyunlibmedia_dev in the
174*4882a593Smuzhiyun`v4l-utils <http://git.linuxtv.org/cgit.cgi/v4l-utils.git/>`__ git
175*4882a593Smuzhiyunrepository), there is no library yet that can provide a single API
176*4882a593Smuzhiyuntowards both Media Controller-based devices and devices that do not use
177*4882a593Smuzhiyunthe Media Controller. If you want to work on this please write to the
178*4882a593Smuzhiyunlinux-media mailing list:
179*4882a593Smuzhiyun`https://linuxtv.org/lists.php <https://linuxtv.org/lists.php>`__.
180*4882a593Smuzhiyun
181*4882a593SmuzhiyunMultiple Opens
182*4882a593Smuzhiyun==============
183*4882a593Smuzhiyun
184*4882a593SmuzhiyunV4L2 devices can be opened more than once. [#f1]_ When this is supported
185*4882a593Smuzhiyunby the driver, users can for example start a "panel" application to
186*4882a593Smuzhiyunchange controls like brightness or audio volume, while another
187*4882a593Smuzhiyunapplication captures video and audio. In other words, panel applications
188*4882a593Smuzhiyunare comparable to an ALSA audio mixer application. Just opening a V4L2
189*4882a593Smuzhiyundevice should not change the state of the device. [#f2]_
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunOnce an application has allocated the memory buffers needed for
192*4882a593Smuzhiyunstreaming data (by calling the :ref:`VIDIOC_REQBUFS`
193*4882a593Smuzhiyunor :ref:`VIDIOC_CREATE_BUFS` ioctls, or
194*4882a593Smuzhiyunimplicitly by calling the :c:func:`read()` or
195*4882a593Smuzhiyun:c:func:`write()` functions) that application (filehandle)
196*4882a593Smuzhiyunbecomes the owner of the device. It is no longer allowed to make changes
197*4882a593Smuzhiyunthat would affect the buffer sizes (e.g. by calling the
198*4882a593Smuzhiyun:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl) and other applications are
199*4882a593Smuzhiyunno longer allowed to allocate buffers or start or stop streaming. The
200*4882a593SmuzhiyunEBUSY error code will be returned instead.
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunMerely opening a V4L2 device does not grant exclusive access. [#f3]_
203*4882a593SmuzhiyunInitiating data exchange however assigns the right to read or write the
204*4882a593Smuzhiyunrequested type of data, and to change related properties, to this file
205*4882a593Smuzhiyundescriptor. Applications can request additional access privileges using
206*4882a593Smuzhiyunthe priority mechanism described in :ref:`app-pri`.
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunShared Data Streams
209*4882a593Smuzhiyun===================
210*4882a593Smuzhiyun
211*4882a593SmuzhiyunV4L2 drivers should not support multiple applications reading or writing
212*4882a593Smuzhiyunthe same data stream on a device by copying buffers, time multiplexing
213*4882a593Smuzhiyunor similar means. This is better handled by a proxy application in user
214*4882a593Smuzhiyunspace.
215*4882a593Smuzhiyun
216*4882a593SmuzhiyunFunctions
217*4882a593Smuzhiyun=========
218*4882a593Smuzhiyun
219*4882a593SmuzhiyunTo open and close V4L2 devices applications use the
220*4882a593Smuzhiyun:c:func:`open()` and :c:func:`close()` function,
221*4882a593Smuzhiyunrespectively. Devices are programmed using the
222*4882a593Smuzhiyun:ref:`ioctl() <func-ioctl>` function as explained in the following
223*4882a593Smuzhiyunsections.
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun.. [#f1]
226*4882a593Smuzhiyun   There are still some old and obscure drivers that have not been
227*4882a593Smuzhiyun   updated to allow for multiple opens. This implies that for such
228*4882a593Smuzhiyun   drivers :c:func:`open()` can return an ``EBUSY`` error code
229*4882a593Smuzhiyun   when the device is already in use.
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun.. [#f2]
232*4882a593Smuzhiyun   Unfortunately, opening a radio device often switches the state of the
233*4882a593Smuzhiyun   device to radio mode in many drivers. This behavior should be fixed
234*4882a593Smuzhiyun   eventually as it violates the V4L2 specification.
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun.. [#f3]
237*4882a593Smuzhiyun   Drivers could recognize the ``O_EXCL`` open flag. Presently this is
238*4882a593Smuzhiyun   not required, so applications cannot know if it really works.
239