xref: /linux/Documentation/driver-api/media/v4l2-intro.rst (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1*ff768f59SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
2*ff768f59SMauro Carvalho Chehab
3*ff768f59SMauro Carvalho ChehabIntroduction
4*ff768f59SMauro Carvalho Chehab------------
5*ff768f59SMauro Carvalho Chehab
6*ff768f59SMauro Carvalho ChehabThe V4L2 drivers tend to be very complex due to the complexity of the
7*ff768f59SMauro Carvalho Chehabhardware: most devices have multiple ICs, export multiple device nodes in
8*ff768f59SMauro Carvalho Chehab/dev, and create also non-V4L2 devices such as DVB, ALSA, FB, I2C and input
9*ff768f59SMauro Carvalho Chehab(IR) devices.
10*ff768f59SMauro Carvalho Chehab
11*ff768f59SMauro Carvalho ChehabEspecially the fact that V4L2 drivers have to setup supporting ICs to
12*ff768f59SMauro Carvalho Chehabdo audio/video muxing/encoding/decoding makes it more complex than most.
13*ff768f59SMauro Carvalho ChehabUsually these ICs are connected to the main bridge driver through one or
14*ff768f59SMauro Carvalho Chehabmore I2C buses, but other buses can also be used. Such devices are
15*ff768f59SMauro Carvalho Chehabcalled 'sub-devices'.
16*ff768f59SMauro Carvalho Chehab
17*ff768f59SMauro Carvalho ChehabFor a long time the framework was limited to the video_device struct for
18*ff768f59SMauro Carvalho Chehabcreating V4L device nodes and video_buf for handling the video buffers
19*ff768f59SMauro Carvalho Chehab(note that this document does not discuss the video_buf framework).
20*ff768f59SMauro Carvalho Chehab
21*ff768f59SMauro Carvalho ChehabThis meant that all drivers had to do the setup of device instances and
22*ff768f59SMauro Carvalho Chehabconnecting to sub-devices themselves. Some of this is quite complicated
23*ff768f59SMauro Carvalho Chehabto do right and many drivers never did do it correctly.
24*ff768f59SMauro Carvalho Chehab
25*ff768f59SMauro Carvalho ChehabThere is also a lot of common code that could never be refactored due to
26*ff768f59SMauro Carvalho Chehabthe lack of a framework.
27*ff768f59SMauro Carvalho Chehab
28*ff768f59SMauro Carvalho ChehabSo this framework sets up the basic building blocks that all drivers
29*ff768f59SMauro Carvalho Chehabneed and this same framework should make it much easier to refactor
30*ff768f59SMauro Carvalho Chehabcommon code into utility functions shared by all drivers.
31*ff768f59SMauro Carvalho Chehab
32*ff768f59SMauro Carvalho ChehabA good example to look at as a reference is the v4l2-pci-skeleton.c
33*ff768f59SMauro Carvalho Chehabsource that is available in samples/v4l/. It is a skeleton driver for
34*ff768f59SMauro Carvalho Chehaba PCI capture card, and demonstrates how to use the V4L2 driver
35*ff768f59SMauro Carvalho Chehabframework. It can be used as a template for real PCI video capture driver.
36*ff768f59SMauro Carvalho Chehab
37*ff768f59SMauro Carvalho ChehabStructure of a V4L driver
38*ff768f59SMauro Carvalho Chehab-------------------------
39*ff768f59SMauro Carvalho Chehab
40*ff768f59SMauro Carvalho ChehabAll drivers have the following structure:
41*ff768f59SMauro Carvalho Chehab
42*ff768f59SMauro Carvalho Chehab1) A struct for each device instance containing the device state.
43*ff768f59SMauro Carvalho Chehab
44*ff768f59SMauro Carvalho Chehab2) A way of initializing and commanding sub-devices (if any).
45*ff768f59SMauro Carvalho Chehab
46*ff768f59SMauro Carvalho Chehab3) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX and /dev/radioX)
47*ff768f59SMauro Carvalho Chehab   and keeping track of device-node specific data.
48*ff768f59SMauro Carvalho Chehab
49*ff768f59SMauro Carvalho Chehab4) Filehandle-specific structs containing per-filehandle data;
50*ff768f59SMauro Carvalho Chehab
51*ff768f59SMauro Carvalho Chehab5) video buffer handling.
52*ff768f59SMauro Carvalho Chehab
53*ff768f59SMauro Carvalho ChehabThis is a rough schematic of how it all relates:
54*ff768f59SMauro Carvalho Chehab
55*ff768f59SMauro Carvalho Chehab.. code-block:: none
56*ff768f59SMauro Carvalho Chehab
57*ff768f59SMauro Carvalho Chehab    device instances
58*ff768f59SMauro Carvalho Chehab      |
59*ff768f59SMauro Carvalho Chehab      +-sub-device instances
60*ff768f59SMauro Carvalho Chehab      |
61*ff768f59SMauro Carvalho Chehab      \-V4L2 device nodes
62*ff768f59SMauro Carvalho Chehab	  |
63*ff768f59SMauro Carvalho Chehab	  \-filehandle instances
64*ff768f59SMauro Carvalho Chehab
65*ff768f59SMauro Carvalho Chehab
66*ff768f59SMauro Carvalho ChehabStructure of the V4L2 framework
67*ff768f59SMauro Carvalho Chehab-------------------------------
68*ff768f59SMauro Carvalho Chehab
69*ff768f59SMauro Carvalho ChehabThe framework closely resembles the driver structure: it has a v4l2_device
70*ff768f59SMauro Carvalho Chehabstruct for the device instance data, a v4l2_subdev struct to refer to
71*ff768f59SMauro Carvalho Chehabsub-device instances, the video_device struct stores V4L2 device node data
72*ff768f59SMauro Carvalho Chehaband the v4l2_fh struct keeps track of filehandle instances.
73*ff768f59SMauro Carvalho Chehab
74*ff768f59SMauro Carvalho ChehabThe V4L2 framework also optionally integrates with the media framework. If a
75*ff768f59SMauro Carvalho Chehabdriver sets the struct v4l2_device mdev field, sub-devices and video nodes
76*ff768f59SMauro Carvalho Chehabwill automatically appear in the media framework as entities.
77