2016-07-17 18:22:23 +07:00
|
|
|
Media Controller devices
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
Media Controller
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
The media controller userspace API is documented in
|
2016-07-20 20:36:18 +07:00
|
|
|
:ref:`the Media Controller uAPI book <media_controller>`. This document focus
|
2016-07-17 18:22:23 +07:00
|
|
|
on the kernel-side implementation of the media framework.
|
|
|
|
|
|
|
|
Abstract media device model
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Discovering a device internal topology, and configuring it at runtime, is one
|
|
|
|
of the goals of the media framework. To achieve this, hardware devices are
|
|
|
|
modelled as an oriented graph of building blocks called entities connected
|
|
|
|
through pads.
|
|
|
|
|
|
|
|
An entity is a basic media hardware building block. It can correspond to
|
|
|
|
a large variety of logical blocks such as physical hardware devices
|
|
|
|
(CMOS sensor for instance), logical hardware devices (a building block
|
|
|
|
in a System-on-Chip image processing pipeline), DMA channels or physical
|
|
|
|
connectors.
|
|
|
|
|
|
|
|
A pad is a connection endpoint through which an entity can interact with
|
|
|
|
other entities. Data (not restricted to video) produced by an entity
|
|
|
|
flows from the entity's output to one or more entity inputs. Pads should
|
|
|
|
not be confused with physical pins at chip boundaries.
|
|
|
|
|
|
|
|
A link is a point-to-point oriented connection between two pads, either
|
|
|
|
on the same entity or on different entities. Data flows from a source
|
|
|
|
pad to a sink pad.
|
|
|
|
|
|
|
|
Media device
|
|
|
|
^^^^^^^^^^^^
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
A media device is represented by a :c:type:`struct media_device <media_device>`
|
|
|
|
instance, defined in ``include/media/media-device.h``.
|
|
|
|
Allocation of the structure is handled by the media device driver, usually by
|
|
|
|
embedding the :c:type:`media_device` instance in a larger driver-specific
|
|
|
|
structure.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Drivers register media device instances by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`__media_device_register()` via the macro ``media_device_register()``
|
|
|
|
and unregistered by calling :cpp:func:`media_device_unregister()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Entities
|
|
|
|
^^^^^^^^
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
Entities are represented by a :c:type:`struct media_entity <media_entity>`
|
|
|
|
instance, defined in ``include/media/media-entity.h``. The structure is usually
|
|
|
|
embedded into a higher-level structure, such as
|
|
|
|
:ref:`v4l2_subdev` or :ref:`video_device`
|
|
|
|
instances, although drivers can allocate entities directly.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Drivers initialize entity pads by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_pads_init()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Drivers register entities with a media device by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_device_register_entity()`
|
2016-07-17 18:22:23 +07:00
|
|
|
and unregistred by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_device_unregister_entity()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Interfaces
|
|
|
|
^^^^^^^^^^
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
Interfaces are represented by a
|
|
|
|
:c:type:`struct media_interface <media_interface>` instance, defined in
|
|
|
|
``include/media/media-entity.h``. Currently, only one type of interface is
|
|
|
|
defined: a device node. Such interfaces are represented by a
|
|
|
|
:c:type:`struct media_intf_devnode <media_intf_devnode>`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Drivers initialize and create device node interfaces by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_devnode_create()`
|
2016-07-17 18:22:23 +07:00
|
|
|
and remove them by calling:
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_devnode_remove()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Pads
|
|
|
|
^^^^
|
2016-07-17 19:18:03 +07:00
|
|
|
Pads are represented by a :c:type:`struct media_pad <media_pad>` instance,
|
|
|
|
defined in ``include/media/media-entity.h``. Each entity stores its pads in
|
|
|
|
a pads array managed by the entity driver. Drivers usually embed the array in
|
|
|
|
a driver-specific structure.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Pads are identified by their entity and their 0-based index in the pads
|
|
|
|
array.
|
2016-07-17 19:18:03 +07:00
|
|
|
|
|
|
|
Both information are stored in the :c:type:`struct media_pad`, making the
|
|
|
|
:c:type:`media_pad` pointer the canonical way to store and pass link references.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Pads have flags that describe the pad capabilities and state.
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
``MEDIA_PAD_FL_SINK`` indicates that the pad supports sinking data.
|
|
|
|
``MEDIA_PAD_FL_SOURCE`` indicates that the pad supports sourcing data.
|
|
|
|
|
|
|
|
.. note::
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
One and only one of ``MEDIA_PAD_FL_SINK`` or ``MEDIA_PAD_FL_SOURCE`` must
|
|
|
|
be set for each pad.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Links
|
|
|
|
^^^^^
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
Links are represented by a :c:type:`struct media_link <media_link>` instance,
|
|
|
|
defined in ``include/media/media-entity.h``. There are two types of links:
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
**1. pad to pad links**:
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Associate two entities via their PADs. Each entity has a list that points
|
|
|
|
to all links originating at or targeting any of its pads.
|
|
|
|
A given link is thus stored twice, once in the source entity and once in
|
|
|
|
the target entity.
|
|
|
|
|
|
|
|
Drivers create pad to pad links by calling:
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_create_pad_link()` and remove with
|
|
|
|
:cpp:func:`media_entity_remove_links()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
**2. interface to entity links**:
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Associate one interface to a Link.
|
|
|
|
|
|
|
|
Drivers create interface to entity links by calling:
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_create_intf_link()` and remove with
|
|
|
|
:cpp:func:`media_remove_intf_links()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Links can only be created after having both ends already created.
|
|
|
|
|
|
|
|
Links have flags that describe the link capabilities and state. The
|
2016-07-17 19:18:03 +07:00
|
|
|
valid values are described at :cpp:func:`media_create_pad_link()` and
|
|
|
|
:cpp:func:`media_create_intf_link()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Graph traversal
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
The media framework provides APIs to iterate over entities in a graph.
|
|
|
|
|
|
|
|
To iterate over all entities belonging to a media device, drivers can use
|
|
|
|
the media_device_for_each_entity macro, defined in
|
2016-07-17 19:18:03 +07:00
|
|
|
``include/media/media-device.h``.
|
|
|
|
|
|
|
|
.. code-block:: c
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
struct media_entity *entity;
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
media_device_for_each_entity(entity, mdev) {
|
|
|
|
// entity will point to each entity in turn
|
|
|
|
...
|
|
|
|
}
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Drivers might also need to iterate over all entities in a graph that can be
|
|
|
|
reached only through enabled links starting at a given entity. The media
|
|
|
|
framework provides a depth-first graph traversal API for that purpose.
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Graphs with cycles (whether directed or undirected) are **NOT**
|
|
|
|
supported by the graph traversal API. To prevent infinite loops, the graph
|
|
|
|
traversal code limits the maximum depth to ``MEDIA_ENTITY_ENUM_MAX_DEPTH``,
|
|
|
|
currently defined as 16.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Drivers initiate a graph traversal by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_graph_walk_start()`
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
The graph structure, provided by the caller, is initialized to start graph
|
|
|
|
traversal at the given entity.
|
|
|
|
|
|
|
|
Drivers can then retrieve the next entity by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_graph_walk_next()`
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
When the graph traversal is complete the function will return ``NULL``.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Graph traversal can be interrupted at any moment. No cleanup function call
|
|
|
|
is required and the graph structure can be freed normally.
|
|
|
|
|
|
|
|
Helper functions can be used to find a link between two given pads, or a pad
|
|
|
|
connected to another pad through an enabled link
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_find_link()` and
|
|
|
|
:cpp:func:`media_entity_remote_pad()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Use count and power handling
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Due to the wide differences between drivers regarding power management
|
|
|
|
needs, the media controller does not implement power management. However,
|
2016-07-17 19:18:03 +07:00
|
|
|
the :c:type:`struct media_entity <media_entity>` includes a ``use_count``
|
|
|
|
field that media drivers
|
2016-07-17 18:22:23 +07:00
|
|
|
can use to track the number of users of every entity for power management
|
|
|
|
needs.
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
The :c:type:`media_entity<media_entity>`.\ ``use_count`` field is owned by
|
|
|
|
media drivers and must not be
|
2016-07-17 18:22:23 +07:00
|
|
|
touched by entity drivers. Access to the field must be protected by the
|
2016-07-17 19:18:03 +07:00
|
|
|
:c:type:`media_device`.\ ``graph_mutex`` lock.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Links setup
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
|
|
|
Link properties can be modified at runtime by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_setup_link()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Pipelines and media streams
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
When starting streaming, drivers must notify all entities in the pipeline to
|
|
|
|
prevent link states from being modified during streaming by calling
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_pipeline_start()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
The function will mark all entities connected to the given entity through
|
|
|
|
enabled links, either directly or indirectly, as streaming.
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
The :c:type:`struct media_pipeline <media_pipeline>` instance pointed to by
|
|
|
|
the pipe argument will be stored in every entity in the pipeline.
|
|
|
|
Drivers should embed the :c:type:`struct media_pipeline <media_pipeline>`
|
|
|
|
in higher-level pipeline structures and can then access the
|
|
|
|
pipeline through the :c:type:`struct media_entity <media_entity>`
|
|
|
|
pipe field.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
Calls to :cpp:func:`media_entity_pipeline_start()` can be nested.
|
|
|
|
The pipeline pointer must be identical for all nested calls to the function.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_pipeline_start()` may return an error. In that case,
|
|
|
|
it will clean up any of the changes it did by itself.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
When stopping the stream, drivers must notify the entities with
|
2016-07-17 19:18:03 +07:00
|
|
|
:cpp:func:`media_entity_pipeline_stop()`.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
If multiple calls to :cpp:func:`media_entity_pipeline_start()` have been
|
|
|
|
made the same number of :cpp:func:`media_entity_pipeline_stop()` calls
|
|
|
|
are required to stop streaming.
|
|
|
|
The :c:type:`media_entity`.\ ``pipe`` field is reset to ``NULL`` on the last
|
|
|
|
nested stop call.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
Link configuration will fail with ``-EBUSY`` by default if either end of the
|
2016-07-17 18:22:23 +07:00
|
|
|
link is a streaming entity. Links that can be modified while streaming must
|
2016-07-17 19:18:03 +07:00
|
|
|
be marked with the ``MEDIA_LNK_FL_DYNAMIC`` flag.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
If other operations need to be disallowed on streaming entities (such as
|
|
|
|
changing entities configuration parameters) drivers can explicitly check the
|
|
|
|
media_entity stream_count field to find out if an entity is streaming. This
|
|
|
|
operation must be done with the media_device graph_mutex held.
|
|
|
|
|
|
|
|
Link validation
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
2016-07-17 19:18:03 +07:00
|
|
|
Link validation is performed by :cpp:func:`media_entity_pipeline_start()`
|
|
|
|
for any entity which has sink pads in the pipeline. The
|
|
|
|
:c:type:`media_entity`.\ ``link_validate()`` callback is used for that
|
|
|
|
purpose. In ``link_validate()`` callback, entity driver should check
|
|
|
|
that the properties of the source pad of the connected entity and its own
|
|
|
|
sink pad match. It is up to the type of the entity (and in the end, the
|
|
|
|
properties of the hardware) what matching actually means.
|
2016-07-17 18:22:23 +07:00
|
|
|
|
|
|
|
Subsystems should facilitate link validation by providing subsystem specific
|
|
|
|
helper functions to provide easy access for commonly needed information, and
|
|
|
|
in the end provide a way to use driver-specific callbacks.
|
|
|
|
|
|
|
|
.. kernel-doc:: include/media/media-device.h
|
|
|
|
|
|
|
|
.. kernel-doc:: include/media/media-devnode.h
|
|
|
|
|
|
|
|
.. kernel-doc:: include/media/media-entity.h
|