18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ciIntroduction
48c2ecf20Sopenharmony_ci------------
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ciThe V4L2 drivers tend to be very complex due to the complexity of the
78c2ecf20Sopenharmony_cihardware: most devices have multiple ICs, export multiple device nodes in
88c2ecf20Sopenharmony_ci/dev, and create also non-V4L2 devices such as DVB, ALSA, FB, I2C and input
98c2ecf20Sopenharmony_ci(IR) devices.
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciEspecially the fact that V4L2 drivers have to setup supporting ICs to
128c2ecf20Sopenharmony_cido audio/video muxing/encoding/decoding makes it more complex than most.
138c2ecf20Sopenharmony_ciUsually these ICs are connected to the main bridge driver through one or
148c2ecf20Sopenharmony_cimore I2C buses, but other buses can also be used. Such devices are
158c2ecf20Sopenharmony_cicalled 'sub-devices'.
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciFor a long time the framework was limited to the video_device struct for
188c2ecf20Sopenharmony_cicreating V4L device nodes and video_buf for handling the video buffers
198c2ecf20Sopenharmony_ci(note that this document does not discuss the video_buf framework).
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciThis meant that all drivers had to do the setup of device instances and
228c2ecf20Sopenharmony_ciconnecting to sub-devices themselves. Some of this is quite complicated
238c2ecf20Sopenharmony_cito do right and many drivers never did do it correctly.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciThere is also a lot of common code that could never be refactored due to
268c2ecf20Sopenharmony_cithe lack of a framework.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciSo this framework sets up the basic building blocks that all drivers
298c2ecf20Sopenharmony_cineed and this same framework should make it much easier to refactor
308c2ecf20Sopenharmony_cicommon code into utility functions shared by all drivers.
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciA good example to look at as a reference is the v4l2-pci-skeleton.c
338c2ecf20Sopenharmony_cisource that is available in samples/v4l/. It is a skeleton driver for
348c2ecf20Sopenharmony_cia PCI capture card, and demonstrates how to use the V4L2 driver
358c2ecf20Sopenharmony_ciframework. It can be used as a template for real PCI video capture driver.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciStructure of a V4L driver
388c2ecf20Sopenharmony_ci-------------------------
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciAll drivers have the following structure:
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci1) A struct for each device instance containing the device state.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci2) A way of initializing and commanding sub-devices (if any).
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci3) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX and /dev/radioX)
478c2ecf20Sopenharmony_ci   and keeping track of device-node specific data.
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci4) Filehandle-specific structs containing per-filehandle data;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci5) video buffer handling.
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciThis is a rough schematic of how it all relates:
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci.. code-block:: none
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci    device instances
588c2ecf20Sopenharmony_ci      |
598c2ecf20Sopenharmony_ci      +-sub-device instances
608c2ecf20Sopenharmony_ci      |
618c2ecf20Sopenharmony_ci      \-V4L2 device nodes
628c2ecf20Sopenharmony_ci	  |
638c2ecf20Sopenharmony_ci	  \-filehandle instances
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciStructure of the V4L2 framework
678c2ecf20Sopenharmony_ci-------------------------------
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciThe framework closely resembles the driver structure: it has a v4l2_device
708c2ecf20Sopenharmony_cistruct for the device instance data, a v4l2_subdev struct to refer to
718c2ecf20Sopenharmony_cisub-device instances, the video_device struct stores V4L2 device node data
728c2ecf20Sopenharmony_ciand the v4l2_fh struct keeps track of filehandle instances.
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciThe V4L2 framework also optionally integrates with the media framework. If a
758c2ecf20Sopenharmony_cidriver sets the struct v4l2_device mdev field, sub-devices and video nodes
768c2ecf20Sopenharmony_ciwill automatically appear in the media framework as entities.
77