162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ciIntroduction 462306a36Sopenharmony_ci------------ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ciThe V4L2 drivers tend to be very complex due to the complexity of the 762306a36Sopenharmony_cihardware: most devices have multiple ICs, export multiple device nodes in 862306a36Sopenharmony_ci/dev, and create also non-V4L2 devices such as DVB, ALSA, FB, I2C and input 962306a36Sopenharmony_ci(IR) devices. 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ciEspecially the fact that V4L2 drivers have to setup supporting ICs to 1262306a36Sopenharmony_cido audio/video muxing/encoding/decoding makes it more complex than most. 1362306a36Sopenharmony_ciUsually these ICs are connected to the main bridge driver through one or 1462306a36Sopenharmony_cimore I2C buses, but other buses can also be used. Such devices are 1562306a36Sopenharmony_cicalled 'sub-devices'. 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ciFor a long time the framework was limited to the video_device struct for 1862306a36Sopenharmony_cicreating V4L device nodes and video_buf for handling the video buffers 1962306a36Sopenharmony_ci(note that this document does not discuss the video_buf framework). 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciThis meant that all drivers had to do the setup of device instances and 2262306a36Sopenharmony_ciconnecting to sub-devices themselves. Some of this is quite complicated 2362306a36Sopenharmony_cito do right and many drivers never did do it correctly. 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciThere is also a lot of common code that could never be refactored due to 2662306a36Sopenharmony_cithe lack of a framework. 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciSo this framework sets up the basic building blocks that all drivers 2962306a36Sopenharmony_cineed and this same framework should make it much easier to refactor 3062306a36Sopenharmony_cicommon code into utility functions shared by all drivers. 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ciA good example to look at as a reference is the v4l2-pci-skeleton.c 3362306a36Sopenharmony_cisource that is available in samples/v4l/. It is a skeleton driver for 3462306a36Sopenharmony_cia PCI capture card, and demonstrates how to use the V4L2 driver 3562306a36Sopenharmony_ciframework. It can be used as a template for real PCI video capture driver. 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ciStructure of a V4L driver 3862306a36Sopenharmony_ci------------------------- 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciAll drivers have the following structure: 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci1) A struct for each device instance containing the device state. 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci2) A way of initializing and commanding sub-devices (if any). 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci3) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX and /dev/radioX) 4762306a36Sopenharmony_ci and keeping track of device-node specific data. 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci4) Filehandle-specific structs containing per-filehandle data; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci5) video buffer handling. 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ciThis is a rough schematic of how it all relates: 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci.. code-block:: none 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci device instances 5862306a36Sopenharmony_ci | 5962306a36Sopenharmony_ci +-sub-device instances 6062306a36Sopenharmony_ci | 6162306a36Sopenharmony_ci \-V4L2 device nodes 6262306a36Sopenharmony_ci | 6362306a36Sopenharmony_ci \-filehandle instances 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciStructure of the V4L2 framework 6762306a36Sopenharmony_ci------------------------------- 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ciThe framework closely resembles the driver structure: it has a v4l2_device 7062306a36Sopenharmony_cistruct for the device instance data, a v4l2_subdev struct to refer to 7162306a36Sopenharmony_cisub-device instances, the video_device struct stores V4L2 device node data 7262306a36Sopenharmony_ciand the v4l2_fh struct keeps track of filehandle instances. 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ciThe V4L2 framework also optionally integrates with the media framework. If a 7562306a36Sopenharmony_cidriver sets the struct v4l2_device mdev field, sub-devices and video nodes 7662306a36Sopenharmony_ciwill automatically appear in the media framework as entities. 77