162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci===================================
462306a36Sopenharmony_ciBuilding support for a media device
562306a36Sopenharmony_ci===================================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciThe first step is to download the Kernel's source code, either via a
862306a36Sopenharmony_cidistribution-specific source file or via the Kernel's main git tree\ [1]_.
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciPlease notice, however, that, if:
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci- you're a braveheart and want to experiment with new stuff;
1362306a36Sopenharmony_ci- if you want to report a bug;
1462306a36Sopenharmony_ci- if you're developing new patches
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciyou should use the main media development tree ``master`` branch:
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci    https://git.linuxtv.org/media_tree.git/
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciIn this case, you may find some useful information at the
2162306a36Sopenharmony_ci`LinuxTv wiki pages <https://linuxtv.org/wiki>`_:
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci    https://linuxtv.org/wiki/index.php/How_to_Obtain,_Build_and_Install_V4L-DVB_Device_Drivers
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci.. [1] The upstream Linux Kernel development tree is located at
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci       https://git.kernel.org/pub/scm/li  nux/kernel/git/torvalds/linux.git/
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciConfiguring the Linux Kernel
3062306a36Sopenharmony_ci============================
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciYou can access a menu of Kernel building options with::
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci    $ make menuconfig
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciThen, select all desired options and exit it, saving the configuration.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciThe changed configuration will be at the ``.config`` file. It would
3962306a36Sopenharmony_cilook like::
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci    ...
4262306a36Sopenharmony_ci    # CONFIG_RC_CORE is not set
4362306a36Sopenharmony_ci    # CONFIG_CEC_CORE is not set
4462306a36Sopenharmony_ci    CONFIG_MEDIA_SUPPORT=m
4562306a36Sopenharmony_ci    CONFIG_MEDIA_SUPPORT_FILTER=y
4662306a36Sopenharmony_ci    ...
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciThe media subsystem is controlled by those menu configuration options::
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci    Device Drivers --->
5162306a36Sopenharmony_ci	<M> Remote Controller support  --->
5262306a36Sopenharmony_ci	[ ] HDMI CEC RC integration
5362306a36Sopenharmony_ci	[ ] Enable CEC error injection support
5462306a36Sopenharmony_ci	[*] HDMI CEC drivers  --->
5562306a36Sopenharmony_ci	<*> Multimedia support  --->
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciThe ``Remote Controller support`` option enables the core support for
5862306a36Sopenharmony_ciremote controllers\ [2]_.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciThe ``HDMI CEC RC integration`` option enables integration of HDMI CEC
6162306a36Sopenharmony_ciwith Linux, allowing to receive data via HDMI CEC as if it were produced
6262306a36Sopenharmony_ciby a remote controller directly connected to the machine.
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciThe ``HDMI CEC drivers`` option allow selecting platform and USB drivers
6562306a36Sopenharmony_cithat receives and/or transmits CEC codes via HDMI interfaces\ [3]_.
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciThe last option (``Multimedia support``) enables support for cameras,
6862306a36Sopenharmony_ciaudio/video grabbers and TV.
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciThe media subsystem support can either be built together with the main
7162306a36Sopenharmony_ciKernel or as a module. For most use cases, it is preferred to have it
7262306a36Sopenharmony_cibuilt as modules.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci.. note::
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci   Instead of using a menu, the Kernel provides a script with allows
7762306a36Sopenharmony_ci   enabling configuration options directly. To enable media support
7862306a36Sopenharmony_ci   and remote controller support using Kernel modules, you could use::
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	$ scripts/config -m RC_CORE
8162306a36Sopenharmony_ci	$ scripts/config -m MEDIA_SUPPORT
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci.. [2] ``Remote Controller support`` should also be enabled if you
8462306a36Sopenharmony_ci       want to use some TV card drivers that may depend on the remote
8562306a36Sopenharmony_ci       controller core support.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci.. [3] Please notice that the DRM subsystem also have drivers for GPUs
8862306a36Sopenharmony_ci       that use the media HDMI CEC support.
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci       Those GPU-specific drivers are selected via the ``Graphics support``
9162306a36Sopenharmony_ci       menu, under ``Device Drivers``.
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci       When a GPU driver supports HDMI CEC, it will automatically
9462306a36Sopenharmony_ci       enable the CEC core support at the media subsystem.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciMedia dependencies
9762306a36Sopenharmony_ci------------------
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciIt should be noticed that enabling the above from a clean config is
10062306a36Sopenharmony_ciusually not enough. The media subsystem depends on several other Linux
10162306a36Sopenharmony_cicore support in order to work.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciFor example, most media devices use a serial communication bus in
10462306a36Sopenharmony_ciorder to talk with some peripherals. Such bus is called I²C
10562306a36Sopenharmony_ci(Inter-Integrated Circuit). In order to be able to build support
10662306a36Sopenharmony_cifor such hardware, the I²C bus support should be enabled, either via
10762306a36Sopenharmony_cimenu or with::
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci    ./scripts/config -m I2C
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciAnother example: the remote controller core requires support for
11262306a36Sopenharmony_ciinput devices, with can be enabled with::
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci    ./scripts/config -m INPUT
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciOther core functionality may also be needed (like PCI and/or USB support),
11762306a36Sopenharmony_cidepending on the specific driver(s) you would like to enable.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciEnabling Remote Controller Support
12062306a36Sopenharmony_ci----------------------------------
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ciThe remote controller menu allows selecting drivers for specific devices.
12362306a36Sopenharmony_ciIt's menu looks like this::
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci         --- Remote Controller support
12662306a36Sopenharmony_ci         <M>   Compile Remote Controller keymap modules
12762306a36Sopenharmony_ci         [*]   LIRC user interface
12862306a36Sopenharmony_ci         [*]     Support for eBPF programs attached to lirc devices
12962306a36Sopenharmony_ci         [*]   Remote controller decoders  --->
13062306a36Sopenharmony_ci         [*]   Remote Controller devices  --->
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciThe ``Compile Remote Controller keymap modules`` option creates key maps for
13362306a36Sopenharmony_ciseveral popular remote controllers.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciThe ``LIRC user interface`` option adds enhanced functionality when using the
13662306a36Sopenharmony_ci``lirc`` program, by enabling an API that allows userspace to receive raw data
13762306a36Sopenharmony_cifrom remote controllers.
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciThe ``Support for eBPF programs attached to lirc devices`` option allows
14062306a36Sopenharmony_cithe usage of special programs (called eBPF) that would allow applications
14162306a36Sopenharmony_cito add extra remote controller decoding functionality to the Linux Kernel.
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciThe ``Remote controller decoders`` option allows selecting the
14462306a36Sopenharmony_ciprotocols that will be recognized by the Linux Kernel. Except if you
14562306a36Sopenharmony_ciwant to disable some specific decoder, it is suggested to keep all
14662306a36Sopenharmony_cisub-options enabled.
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciThe ``Remote Controller devices`` allows you to select the drivers
14962306a36Sopenharmony_cithat would be needed to support your device.
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciThe same configuration can also be set via the ``script/config``
15262306a36Sopenharmony_ciscript. So, for instance, in order to support the ITE remote controller
15362306a36Sopenharmony_cidriver (found on Intel NUCs and on some ASUS x86 desktops), you could do::
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	$ scripts/config -e INPUT
15662306a36Sopenharmony_ci	$ scripts/config -e ACPI
15762306a36Sopenharmony_ci	$ scripts/config -e MODULES
15862306a36Sopenharmony_ci	$ scripts/config -m RC_CORE
15962306a36Sopenharmony_ci	$ scripts/config -e RC_DEVICES
16062306a36Sopenharmony_ci	$ scripts/config -e RC_DECODERS
16162306a36Sopenharmony_ci	$ scripts/config -m IR_RC5_DECODER
16262306a36Sopenharmony_ci	$ scripts/config -m IR_ITE_CIR
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciEnabling HDMI CEC Support
16562306a36Sopenharmony_ci-------------------------
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciThe HDMI CEC support is set automatically when a driver requires it. So,
16862306a36Sopenharmony_ciall you need to do is to enable support either for a graphics card
16962306a36Sopenharmony_cithat needs it or by one of the existing HDMI drivers.
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciThe HDMI-specific drivers are available at the ``HDMI CEC drivers``
17262306a36Sopenharmony_cimenu\ [4]_::
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	--- HDMI CEC drivers
17562306a36Sopenharmony_ci	< >   ChromeOS EC CEC driver
17662306a36Sopenharmony_ci	< >   Amlogic Meson AO CEC driver
17762306a36Sopenharmony_ci	< >   Amlogic Meson G12A AO CEC driver
17862306a36Sopenharmony_ci	< >   Generic GPIO-based CEC driver
17962306a36Sopenharmony_ci	< >   Samsung S5P CEC driver
18062306a36Sopenharmony_ci	< >   STMicroelectronics STiH4xx HDMI CEC driver
18162306a36Sopenharmony_ci	< >   STMicroelectronics STM32 HDMI CEC driver
18262306a36Sopenharmony_ci	< >   Tegra HDMI CEC driver
18362306a36Sopenharmony_ci	< >   SECO Boards HDMI CEC driver
18462306a36Sopenharmony_ci	[ ]     SECO Boards IR RC5 support
18562306a36Sopenharmony_ci	< >   Pulse Eight HDMI CEC
18662306a36Sopenharmony_ci	< >   RainShadow Tech HDMI CEC
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci.. [4] The above contents is just an example. The actual options for
18962306a36Sopenharmony_ci       HDMI devices depends on the system's architecture and may vary
19062306a36Sopenharmony_ci       on new Kernels.
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciEnabling Media Support
19362306a36Sopenharmony_ci----------------------
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciThe Media menu has a lot more options than the remote controller menu.
19662306a36Sopenharmony_ciOnce selected, you should see the following options::
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	--- Media support
19962306a36Sopenharmony_ci	[ ] Filter media drivers
20062306a36Sopenharmony_ci	[*] Autoselect ancillary drivers
20162306a36Sopenharmony_ci	    Media device types --->
20262306a36Sopenharmony_ci	    Media core support --->
20362306a36Sopenharmony_ci	    Video4Linux options --->
20462306a36Sopenharmony_ci	    Media controller options --->
20562306a36Sopenharmony_ci	    Digital TV options --->
20662306a36Sopenharmony_ci	    HDMI CEC options --->
20762306a36Sopenharmony_ci	    Media drivers --->
20862306a36Sopenharmony_ci	    Media ancillary drivers --->
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciExcept if you know exactly what you're doing, or if you want to build
21162306a36Sopenharmony_cia driver for a SoC platform, it is strongly recommended to keep the
21262306a36Sopenharmony_ci``Autoselect ancillary drivers`` option turned on, as it will auto-select
21362306a36Sopenharmony_cithe needed I²C ancillary drivers.
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ciThere are now two ways to select media device drivers, as described
21662306a36Sopenharmony_cibelow.
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci``Filter media drivers`` menu
21962306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ciThis menu is meant to easy setup for PC and Laptop hardware. It works
22262306a36Sopenharmony_ciby letting the user to specify what kind of media drivers are desired,
22362306a36Sopenharmony_ciwith those options::
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	[ ] Cameras and video grabbers
22662306a36Sopenharmony_ci	[ ] Analog TV
22762306a36Sopenharmony_ci	[ ] Digital TV
22862306a36Sopenharmony_ci	[ ] AM/FM radio receivers/transmitters
22962306a36Sopenharmony_ci	[ ] Software defined radio
23062306a36Sopenharmony_ci	[ ] Platform-specific devices
23162306a36Sopenharmony_ci	[ ] Test drivers
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ciSo, if you want to add support to a camera or video grabber only,
23462306a36Sopenharmony_ciselect just the first option. Multiple options are allowed.
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ciOnce the options on this menu are selected, the building system will
23762306a36Sopenharmony_ciauto-select the needed core drivers in order to support the selected
23862306a36Sopenharmony_cifunctionality.
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci.. note::
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci   Most TV cards are hybrid: they support both Analog TV and Digital TV.
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci   If you have an hybrid card, you may need to enable both ``Analog TV``
24562306a36Sopenharmony_ci   and ``Digital TV`` at the menu.
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ciWhen using this option, the defaults for the media support core
24862306a36Sopenharmony_cifunctionality are usually good enough to provide the basic functionality
24962306a36Sopenharmony_cifor the driver. Yet, you could manually enable some desired extra (optional)
25062306a36Sopenharmony_cifunctionality using the settings under each of the following
25162306a36Sopenharmony_ci``Media support`` sub-menus::
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	    Media core support --->
25462306a36Sopenharmony_ci	    Video4Linux options --->
25562306a36Sopenharmony_ci	    Media controller options --->
25662306a36Sopenharmony_ci	    Digital TV options --->
25762306a36Sopenharmony_ci	    HDMI CEC options --->
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ciOnce you select the desired filters, the drivers that matches the filtering
26062306a36Sopenharmony_cicriteria will be available at the ``Media support->Media drivers`` sub-menu.
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci``Media Core Support`` menu without filtering
26362306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ciIf you disable the ``Filter media drivers`` menu, all drivers available
26662306a36Sopenharmony_cifor your system whose dependencies are met should be shown at the
26762306a36Sopenharmony_ci``Media drivers`` menu.
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ciPlease notice, however, that you should first ensure that the
27062306a36Sopenharmony_ci``Media Core Support`` menu has all the core functionality your drivers
27162306a36Sopenharmony_ciwould need, as otherwise the corresponding device drivers won't be shown.
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ciExample
27462306a36Sopenharmony_ci-------
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ciIn order to enable modular support for one of the boards listed on
27762306a36Sopenharmony_ci:doc:`this table <cx231xx-cardlist>`, with modular media core modules, the
27862306a36Sopenharmony_ci``.config`` file should contain those lines::
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci    CONFIG_MODULES=y
28162306a36Sopenharmony_ci    CONFIG_USB=y
28262306a36Sopenharmony_ci    CONFIG_I2C=y
28362306a36Sopenharmony_ci    CONFIG_INPUT=y
28462306a36Sopenharmony_ci    CONFIG_RC_CORE=m
28562306a36Sopenharmony_ci    CONFIG_MEDIA_SUPPORT=m
28662306a36Sopenharmony_ci    CONFIG_MEDIA_SUPPORT_FILTER=y
28762306a36Sopenharmony_ci    CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
28862306a36Sopenharmony_ci    CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
28962306a36Sopenharmony_ci    CONFIG_MEDIA_USB_SUPPORT=y
29062306a36Sopenharmony_ci    CONFIG_VIDEO_CX231XX=y
29162306a36Sopenharmony_ci    CONFIG_VIDEO_CX231XX_DVB=y
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ciBuilding and installing a new Kernel
29462306a36Sopenharmony_ci====================================
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ciOnce the ``.config`` file has everything needed, all it takes to build
29762306a36Sopenharmony_ciis to run the ``make`` command::
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci    $ make
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ciAnd then install the new Kernel and its modules::
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci    $ sudo make modules_install
30462306a36Sopenharmony_ci    $ sudo make install
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ciBuilding just the new media drivers and core
30762306a36Sopenharmony_ci============================================
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ciRunning a new development Kernel from the development tree is usually risky,
31062306a36Sopenharmony_cibecause it may have experimental changes that may have bugs. So, there are
31162306a36Sopenharmony_cisome ways to build just the new drivers, using alternative trees.
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ciThere is the `Linux Kernel backports project
31462306a36Sopenharmony_ci<https://backports.wiki.kernel.org/index.php/Main_Page>`_, with contains
31562306a36Sopenharmony_cinewer drivers meant to be compiled against stable Kernels.
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ciThe LinuxTV developers, with are responsible for maintaining the media
31862306a36Sopenharmony_cisubsystem also maintains a backport tree, with just the media drivers
31962306a36Sopenharmony_cidaily updated from the newest kernel. Such tree is available at:
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_cihttps://git.linuxtv.org/media_build.git/
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ciIt should be noticed that, while it should be relatively safe to use the
32462306a36Sopenharmony_ci``media_build`` tree for testing purposes, there are not warranties that
32562306a36Sopenharmony_ciit would work (or even build) on a random Kernel. This tree is maintained
32662306a36Sopenharmony_ciusing a "best-efforts" principle, as time permits us to fix issues there.
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ciIf you notice anything wrong on it, feel free to submit patches at the
32962306a36Sopenharmony_ciLinux media subsystem's mailing list: media@vger.kernel.org. Please
33062306a36Sopenharmony_ciadd ``[PATCH media-build]`` at the e-mail's subject if you submit a new
33162306a36Sopenharmony_cipatch for the media-build.
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ciBefore using it, you should run::
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci    $ ./build
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci.. note::
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci    1) you may need to run it twice if the ``media-build`` tree gets
34062306a36Sopenharmony_ci       updated;
34162306a36Sopenharmony_ci    2) you may need to do a ``make distclean`` if you had built it
34262306a36Sopenharmony_ci       in the past for a different Kernel version than the one you're
34362306a36Sopenharmony_ci       currently using;
34462306a36Sopenharmony_ci    3) by default, it will use the same config options for media as
34562306a36Sopenharmony_ci       the ones defined on the Kernel you're running.
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ciIn order to select different drivers or different config options,
34862306a36Sopenharmony_ciuse::
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci    $ make menuconfig
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ciThen, you can build and install the new drivers::
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci    $ make && sudo make install
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ciThis will override the previous media drivers that your Kernel were
35762306a36Sopenharmony_ciusing.
358