blob: deae3b7c692dd6d3804a6d124fc2eca68f27cd16 [file] [log] [blame]
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -03001Media Controller devices
2------------------------
3
4Media Controller
5~~~~~~~~~~~~~~~~
6
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -03007The media controller userspace API is documented in
Mauro Carvalho Chehabda83c882016-07-20 10:36:18 -03008:ref:`the Media Controller uAPI book <media_controller>`. This document focus
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -03009on the kernel-side implementation of the media framework.
10
11Abstract media device model
12^^^^^^^^^^^^^^^^^^^^^^^^^^^
13
14Discovering a device internal topology, and configuring it at runtime, is one
15of the goals of the media framework. To achieve this, hardware devices are
16modelled as an oriented graph of building blocks called entities connected
17through pads.
18
19An entity is a basic media hardware building block. It can correspond to
20a large variety of logical blocks such as physical hardware devices
21(CMOS sensor for instance), logical hardware devices (a building block
22in a System-on-Chip image processing pipeline), DMA channels or physical
23connectors.
24
25A pad is a connection endpoint through which an entity can interact with
26other entities. Data (not restricted to video) produced by an entity
27flows from the entity's output to one or more entity inputs. Pads should
28not be confused with physical pins at chip boundaries.
29
30A link is a point-to-point oriented connection between two pads, either
31on the same entity or on different entities. Data flows from a source
32pad to a sink pad.
33
34Media device
35^^^^^^^^^^^^
36
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030037A media device is represented by a :c:type:`struct media_device <media_device>`
38instance, defined in ``include/media/media-device.h``.
39Allocation of the structure is handled by the media device driver, usually by
40embedding the :c:type:`media_device` instance in a larger driver-specific
41structure.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030042
43Drivers register media device instances by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030044:cpp:func:`__media_device_register()` via the macro ``media_device_register()``
45and unregistered by calling :cpp:func:`media_device_unregister()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030046
47Entities
48^^^^^^^^
49
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030050Entities are represented by a :c:type:`struct media_entity <media_entity>`
51instance, defined in ``include/media/media-entity.h``. The structure is usually
52embedded into a higher-level structure, such as
Mauro Carvalho Chehab041d8212016-07-21 15:29:11 -030053:c:type:`v4l2_subdev` or :c:type:`video_device`
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030054instances, although drivers can allocate entities directly.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030055
56Drivers initialize entity pads by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030057:cpp:func:`media_entity_pads_init()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030058
59Drivers register entities with a media device by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030060:cpp:func:`media_device_register_entity()`
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030061and unregistred by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030062:cpp:func:`media_device_unregister_entity()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030063
64Interfaces
65^^^^^^^^^^
66
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030067Interfaces are represented by a
68:c:type:`struct media_interface <media_interface>` instance, defined in
69``include/media/media-entity.h``. Currently, only one type of interface is
70defined: a device node. Such interfaces are represented by a
71:c:type:`struct media_intf_devnode <media_intf_devnode>`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030072
73Drivers initialize and create device node interfaces by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030074:cpp:func:`media_devnode_create()`
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030075and remove them by calling:
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030076:cpp:func:`media_devnode_remove()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030077
78Pads
79^^^^
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030080Pads are represented by a :c:type:`struct media_pad <media_pad>` instance,
81defined in ``include/media/media-entity.h``. Each entity stores its pads in
82a pads array managed by the entity driver. Drivers usually embed the array in
83a driver-specific structure.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030084
85Pads are identified by their entity and their 0-based index in the pads
86array.
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030087
88Both information are stored in the :c:type:`struct media_pad`, making the
89:c:type:`media_pad` pointer the canonical way to store and pass link references.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030090
91Pads have flags that describe the pad capabilities and state.
92
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030093``MEDIA_PAD_FL_SINK`` indicates that the pad supports sinking data.
94``MEDIA_PAD_FL_SOURCE`` indicates that the pad supports sourcing data.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -030095
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -030096.. note::
97
98 One and only one of ``MEDIA_PAD_FL_SINK`` or ``MEDIA_PAD_FL_SOURCE`` must
99 be set for each pad.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300100
101Links
102^^^^^
103
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300104Links are represented by a :c:type:`struct media_link <media_link>` instance,
105defined in ``include/media/media-entity.h``. There are two types of links:
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300106
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300107**1. pad to pad links**:
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300108
109Associate two entities via their PADs. Each entity has a list that points
110to all links originating at or targeting any of its pads.
111A given link is thus stored twice, once in the source entity and once in
112the target entity.
113
114Drivers create pad to pad links by calling:
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300115:cpp:func:`media_create_pad_link()` and remove with
116:cpp:func:`media_entity_remove_links()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300117
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300118**2. interface to entity links**:
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300119
120Associate one interface to a Link.
121
122Drivers create interface to entity links by calling:
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300123:cpp:func:`media_create_intf_link()` and remove with
124:cpp:func:`media_remove_intf_links()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300125
126.. note::
127
128 Links can only be created after having both ends already created.
129
130Links have flags that describe the link capabilities and state. The
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300131valid values are described at :cpp:func:`media_create_pad_link()` and
132:cpp:func:`media_create_intf_link()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300133
134Graph traversal
135^^^^^^^^^^^^^^^
136
137The media framework provides APIs to iterate over entities in a graph.
138
139To iterate over all entities belonging to a media device, drivers can use
140the media_device_for_each_entity macro, defined in
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300141``include/media/media-device.h``.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300142
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300143.. code-block:: c
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300144
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300145 struct media_entity *entity;
146
147 media_device_for_each_entity(entity, mdev) {
148 // entity will point to each entity in turn
149 ...
150 }
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300151
152Drivers might also need to iterate over all entities in a graph that can be
153reached only through enabled links starting at a given entity. The media
154framework provides a depth-first graph traversal API for that purpose.
155
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300156.. note::
157
158 Graphs with cycles (whether directed or undirected) are **NOT**
159 supported by the graph traversal API. To prevent infinite loops, the graph
160 traversal code limits the maximum depth to ``MEDIA_ENTITY_ENUM_MAX_DEPTH``,
161 currently defined as 16.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300162
163Drivers initiate a graph traversal by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300164:cpp:func:`media_entity_graph_walk_start()`
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300165
166The graph structure, provided by the caller, is initialized to start graph
167traversal at the given entity.
168
169Drivers can then retrieve the next entity by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300170:cpp:func:`media_entity_graph_walk_next()`
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300171
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300172When the graph traversal is complete the function will return ``NULL``.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300173
174Graph traversal can be interrupted at any moment. No cleanup function call
175is required and the graph structure can be freed normally.
176
177Helper functions can be used to find a link between two given pads, or a pad
178connected to another pad through an enabled link
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300179:cpp:func:`media_entity_find_link()` and
180:cpp:func:`media_entity_remote_pad()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300181
182Use count and power handling
183^^^^^^^^^^^^^^^^^^^^^^^^^^^^
184
185Due to the wide differences between drivers regarding power management
186needs, the media controller does not implement power management. However,
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300187the :c:type:`struct media_entity <media_entity>` includes a ``use_count``
188field that media drivers
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300189can use to track the number of users of every entity for power management
190needs.
191
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300192The :c:type:`media_entity<media_entity>`.\ ``use_count`` field is owned by
193media drivers and must not be
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300194touched by entity drivers. Access to the field must be protected by the
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300195:c:type:`media_device`.\ ``graph_mutex`` lock.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300196
197Links setup
198^^^^^^^^^^^
199
200Link properties can be modified at runtime by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300201:cpp:func:`media_entity_setup_link()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300202
203Pipelines and media streams
204^^^^^^^^^^^^^^^^^^^^^^^^^^^
205
206When starting streaming, drivers must notify all entities in the pipeline to
207prevent link states from being modified during streaming by calling
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300208:cpp:func:`media_entity_pipeline_start()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300209
210The function will mark all entities connected to the given entity through
211enabled links, either directly or indirectly, as streaming.
212
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300213The :c:type:`struct media_pipeline <media_pipeline>` instance pointed to by
214the pipe argument will be stored in every entity in the pipeline.
215Drivers should embed the :c:type:`struct media_pipeline <media_pipeline>`
216in higher-level pipeline structures and can then access the
217pipeline through the :c:type:`struct media_entity <media_entity>`
218pipe field.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300219
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300220Calls to :cpp:func:`media_entity_pipeline_start()` can be nested.
221The pipeline pointer must be identical for all nested calls to the function.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300222
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300223:cpp:func:`media_entity_pipeline_start()` may return an error. In that case,
224it will clean up any of the changes it did by itself.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300225
226When stopping the stream, drivers must notify the entities with
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300227:cpp:func:`media_entity_pipeline_stop()`.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300228
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300229If multiple calls to :cpp:func:`media_entity_pipeline_start()` have been
230made the same number of :cpp:func:`media_entity_pipeline_stop()` calls
231are required to stop streaming.
232The :c:type:`media_entity`.\ ``pipe`` field is reset to ``NULL`` on the last
233nested stop call.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300234
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300235Link configuration will fail with ``-EBUSY`` by default if either end of the
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300236link is a streaming entity. Links that can be modified while streaming must
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300237be marked with the ``MEDIA_LNK_FL_DYNAMIC`` flag.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300238
239If other operations need to be disallowed on streaming entities (such as
240changing entities configuration parameters) drivers can explicitly check the
241media_entity stream_count field to find out if an entity is streaming. This
242operation must be done with the media_device graph_mutex held.
243
244Link validation
245^^^^^^^^^^^^^^^
246
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300247Link validation is performed by :cpp:func:`media_entity_pipeline_start()`
248for any entity which has sink pads in the pipeline. The
249:c:type:`media_entity`.\ ``link_validate()`` callback is used for that
250purpose. In ``link_validate()`` callback, entity driver should check
251that the properties of the source pad of the connected entity and its own
252sink pad match. It is up to the type of the entity (and in the end, the
253properties of the hardware) what matching actually means.
Mauro Carvalho Chehab684ffa22016-07-17 08:22:23 -0300254
255Subsystems should facilitate link validation by providing subsystem specific
256helper functions to provide easy access for commonly needed information, and
257in the end provide a way to use driver-specific callbacks.
258
259.. kernel-doc:: include/media/media-device.h
260
261.. kernel-doc:: include/media/media-devnode.h
262
263.. kernel-doc:: include/media/media-entity.h