Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 Media Controller devices
0004 ------------------------
0005 
0006 Media Controller
0007 ~~~~~~~~~~~~~~~~
0008 
0009 The media controller userspace API is documented in
0010 :ref:`the Media Controller uAPI book <media_controller>`. This document focus
0011 on the kernel-side implementation of the media framework.
0012 
0013 Abstract media device model
0014 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
0015 
0016 Discovering a device internal topology, and configuring it at runtime, is one
0017 of the goals of the media framework. To achieve this, hardware devices are
0018 modelled as an oriented graph of building blocks called entities connected
0019 through pads.
0020 
0021 An entity is a basic media hardware building block. It can correspond to
0022 a large variety of logical blocks such as physical hardware devices
0023 (CMOS sensor for instance), logical hardware devices (a building block
0024 in a System-on-Chip image processing pipeline), DMA channels or physical
0025 connectors.
0026 
0027 A pad is a connection endpoint through which an entity can interact with
0028 other entities. Data (not restricted to video) produced by an entity
0029 flows from the entity's output to one or more entity inputs. Pads should
0030 not be confused with physical pins at chip boundaries.
0031 
0032 A link is a point-to-point oriented connection between two pads, either
0033 on the same entity or on different entities. Data flows from a source
0034 pad to a sink pad.
0035 
0036 Media device
0037 ^^^^^^^^^^^^
0038 
0039 A media device is represented by a struct media_device
0040 instance, defined in ``include/media/media-device.h``.
0041 Allocation of the structure is handled by the media device driver, usually by
0042 embedding the :c:type:`media_device` instance in a larger driver-specific
0043 structure.
0044 
0045 Drivers initialise media device instances by calling
0046 :c:func:`media_device_init()`. After initialising a media device instance, it is
0047 registered by calling :c:func:`__media_device_register()` via the macro
0048 ``media_device_register()`` and unregistered by calling
0049 :c:func:`media_device_unregister()`. An initialised media device must be
0050 eventually cleaned up by calling :c:func:`media_device_cleanup()`.
0051 
0052 Note that it is not allowed to unregister a media device instance that was not
0053 previously registered, or clean up a media device instance that was not
0054 previously initialised.
0055 
0056 Entities
0057 ^^^^^^^^
0058 
0059 Entities are represented by a struct media_entity
0060 instance, defined in ``include/media/media-entity.h``. The structure is usually
0061 embedded into a higher-level structure, such as
0062 :c:type:`v4l2_subdev` or :c:type:`video_device`
0063 instances, although drivers can allocate entities directly.
0064 
0065 Drivers initialize entity pads by calling
0066 :c:func:`media_entity_pads_init()`.
0067 
0068 Drivers register entities with a media device by calling
0069 :c:func:`media_device_register_entity()`
0070 and unregistered by calling
0071 :c:func:`media_device_unregister_entity()`.
0072 
0073 Interfaces
0074 ^^^^^^^^^^
0075 
0076 Interfaces are represented by a
0077 struct media_interface instance, defined in
0078 ``include/media/media-entity.h``. Currently, only one type of interface is
0079 defined: a device node. Such interfaces are represented by a
0080 struct media_intf_devnode.
0081 
0082 Drivers initialize and create device node interfaces by calling
0083 :c:func:`media_devnode_create()`
0084 and remove them by calling:
0085 :c:func:`media_devnode_remove()`.
0086 
0087 Pads
0088 ^^^^
0089 Pads are represented by a struct media_pad instance,
0090 defined in ``include/media/media-entity.h``. Each entity stores its pads in
0091 a pads array managed by the entity driver. Drivers usually embed the array in
0092 a driver-specific structure.
0093 
0094 Pads are identified by their entity and their 0-based index in the pads
0095 array.
0096 
0097 Both information are stored in the struct media_pad,
0098 making the struct media_pad pointer the canonical way
0099 to store and pass link references.
0100 
0101 Pads have flags that describe the pad capabilities and state.
0102 
0103 ``MEDIA_PAD_FL_SINK`` indicates that the pad supports sinking data.
0104 ``MEDIA_PAD_FL_SOURCE`` indicates that the pad supports sourcing data.
0105 
0106 .. note::
0107 
0108   One and only one of ``MEDIA_PAD_FL_SINK`` or ``MEDIA_PAD_FL_SOURCE`` must
0109   be set for each pad.
0110 
0111 Links
0112 ^^^^^
0113 
0114 Links are represented by a struct media_link instance,
0115 defined in ``include/media/media-entity.h``. There are two types of links:
0116 
0117 **1. pad to pad links**:
0118 
0119 Associate two entities via their PADs. Each entity has a list that points
0120 to all links originating at or targeting any of its pads.
0121 A given link is thus stored twice, once in the source entity and once in
0122 the target entity.
0123 
0124 Drivers create pad to pad links by calling:
0125 :c:func:`media_create_pad_link()` and remove with
0126 :c:func:`media_entity_remove_links()`.
0127 
0128 **2. interface to entity links**:
0129 
0130 Associate one interface to a Link.
0131 
0132 Drivers create interface to entity links by calling:
0133 :c:func:`media_create_intf_link()` and remove with
0134 :c:func:`media_remove_intf_links()`.
0135 
0136 .. note::
0137 
0138    Links can only be created after having both ends already created.
0139 
0140 Links have flags that describe the link capabilities and state. The
0141 valid values are described at :c:func:`media_create_pad_link()` and
0142 :c:func:`media_create_intf_link()`.
0143 
0144 Graph traversal
0145 ^^^^^^^^^^^^^^^
0146 
0147 The media framework provides APIs to iterate over entities in a graph.
0148 
0149 To iterate over all entities belonging to a media device, drivers can use
0150 the media_device_for_each_entity macro, defined in
0151 ``include/media/media-device.h``.
0152 
0153 ..  code-block:: c
0154 
0155     struct media_entity *entity;
0156 
0157     media_device_for_each_entity(entity, mdev) {
0158     // entity will point to each entity in turn
0159     ...
0160     }
0161 
0162 Drivers might also need to iterate over all entities in a graph that can be
0163 reached only through enabled links starting at a given entity. The media
0164 framework provides a depth-first graph traversal API for that purpose.
0165 
0166 .. note::
0167 
0168    Graphs with cycles (whether directed or undirected) are **NOT**
0169    supported by the graph traversal API. To prevent infinite loops, the graph
0170    traversal code limits the maximum depth to ``MEDIA_ENTITY_ENUM_MAX_DEPTH``,
0171    currently defined as 16.
0172 
0173 Drivers initiate a graph traversal by calling
0174 :c:func:`media_graph_walk_start()`
0175 
0176 The graph structure, provided by the caller, is initialized to start graph
0177 traversal at the given entity.
0178 
0179 Drivers can then retrieve the next entity by calling
0180 :c:func:`media_graph_walk_next()`
0181 
0182 When the graph traversal is complete the function will return ``NULL``.
0183 
0184 Graph traversal can be interrupted at any moment. No cleanup function call
0185 is required and the graph structure can be freed normally.
0186 
0187 Helper functions can be used to find a link between two given pads, or a pad
0188 connected to another pad through an enabled link
0189 (:c:func:`media_entity_find_link()`, :c:func:`media_pad_remote_pad_first()`,
0190 :c:func:`media_entity_remote_source_pad_unique()` and
0191 :c:func:`media_pad_remote_pad_unique()`).
0192 
0193 Use count and power handling
0194 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0195 
0196 Due to the wide differences between drivers regarding power management
0197 needs, the media controller does not implement power management. However,
0198 the struct media_entity includes a ``use_count``
0199 field that media drivers
0200 can use to track the number of users of every entity for power management
0201 needs.
0202 
0203 The :c:type:`media_entity<media_entity>`.\ ``use_count`` field is owned by
0204 media drivers and must not be
0205 touched by entity drivers. Access to the field must be protected by the
0206 :c:type:`media_device`.\ ``graph_mutex`` lock.
0207 
0208 Links setup
0209 ^^^^^^^^^^^
0210 
0211 Link properties can be modified at runtime by calling
0212 :c:func:`media_entity_setup_link()`.
0213 
0214 Pipelines and media streams
0215 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
0216 
0217 When starting streaming, drivers must notify all entities in the pipeline to
0218 prevent link states from being modified during streaming by calling
0219 :c:func:`media_pipeline_start()`.
0220 
0221 The function will mark all entities connected to the given entity through
0222 enabled links, either directly or indirectly, as streaming.
0223 
0224 The struct media_pipeline instance pointed to by
0225 the pipe argument will be stored in every entity in the pipeline.
0226 Drivers should embed the struct media_pipeline
0227 in higher-level pipeline structures and can then access the
0228 pipeline through the struct media_entity
0229 pipe field.
0230 
0231 Calls to :c:func:`media_pipeline_start()` can be nested.
0232 The pipeline pointer must be identical for all nested calls to the function.
0233 
0234 :c:func:`media_pipeline_start()` may return an error. In that case,
0235 it will clean up any of the changes it did by itself.
0236 
0237 When stopping the stream, drivers must notify the entities with
0238 :c:func:`media_pipeline_stop()`.
0239 
0240 If multiple calls to :c:func:`media_pipeline_start()` have been
0241 made the same number of :c:func:`media_pipeline_stop()` calls
0242 are required to stop streaming.
0243 The :c:type:`media_entity`.\ ``pipe`` field is reset to ``NULL`` on the last
0244 nested stop call.
0245 
0246 Link configuration will fail with ``-EBUSY`` by default if either end of the
0247 link is a streaming entity. Links that can be modified while streaming must
0248 be marked with the ``MEDIA_LNK_FL_DYNAMIC`` flag.
0249 
0250 If other operations need to be disallowed on streaming entities (such as
0251 changing entities configuration parameters) drivers can explicitly check the
0252 media_entity stream_count field to find out if an entity is streaming. This
0253 operation must be done with the media_device graph_mutex held.
0254 
0255 Link validation
0256 ^^^^^^^^^^^^^^^
0257 
0258 Link validation is performed by :c:func:`media_pipeline_start()`
0259 for any entity which has sink pads in the pipeline. The
0260 :c:type:`media_entity`.\ ``link_validate()`` callback is used for that
0261 purpose. In ``link_validate()`` callback, entity driver should check
0262 that the properties of the source pad of the connected entity and its own
0263 sink pad match. It is up to the type of the entity (and in the end, the
0264 properties of the hardware) what matching actually means.
0265 
0266 Subsystems should facilitate link validation by providing subsystem specific
0267 helper functions to provide easy access for commonly needed information, and
0268 in the end provide a way to use driver-specific callbacks.
0269 
0270 Media Controller Device Allocator API
0271 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0272 
0273 When the media device belongs to more than one driver, the shared media
0274 device is allocated with the shared struct device as the key for look ups.
0275 
0276 The shared media device should stay in registered state until the last
0277 driver unregisters it. In addition, the media device should be released when
0278 all the references are released. Each driver gets a reference to the media
0279 device during probe, when it allocates the media device. If media device is
0280 already allocated, the allocate API bumps up the refcount and returns the
0281 existing media device. The driver puts the reference back in its disconnect
0282 routine when it calls :c:func:`media_device_delete()`.
0283 
0284 The media device is unregistered and cleaned up from the kref put handler to
0285 ensure that the media device stays in registered state until the last driver
0286 unregisters the media device.
0287 
0288 **Driver Usage**
0289 
0290 Drivers should use the appropriate media-core routines to manage the shared
0291 media device life-time handling the two states:
0292 1. allocate -> register -> delete
0293 2. get reference to already registered device -> delete
0294 
0295 call :c:func:`media_device_delete()` routine to make sure the shared media
0296 device delete is handled correctly.
0297 
0298 **driver probe:**
0299 Call :c:func:`media_device_usb_allocate()` to allocate or get a reference
0300 Call :c:func:`media_device_register()`, if media devnode isn't registered
0301 
0302 **driver disconnect:**
0303 Call :c:func:`media_device_delete()` to free the media_device. Freeing is
0304 handled by the kref put handler.
0305 
0306 API Definitions
0307 ^^^^^^^^^^^^^^^
0308 
0309 .. kernel-doc:: include/media/media-device.h
0310 
0311 .. kernel-doc:: include/media/media-devnode.h
0312 
0313 .. kernel-doc:: include/media/media-entity.h
0314 
0315 .. kernel-doc:: include/media/media-request.h
0316 
0317 .. kernel-doc:: include/media/media-dev-allocator.h