blob: ef3663af1db3792786a63f5c2c484016aedc04aa [file] [log] [blame]
Laurent Pinchart176fb0d2009-12-09 08:39:58 -03001Linux kernel media framework
2============================
3
4This document describes the Linux kernel media framework, its data structures,
5functions and their usage.
6
7
8Introduction
9------------
10
11The media controller API is documented in DocBook format in
Paul Bolle395cf962011-08-15 02:02:26 +020012Documentation/DocBook/media/v4l/media-controller.xml. This document will focus
13on the kernel-side implementation of the media framework.
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030014
15
Laurent Pinchart53e269c2009-12-09 08:40:00 -030016Abstract media device model
17---------------------------
18
19Discovering a device internal topology, and configuring it at runtime, is one
20of the goals of the media framework. To achieve this, hardware devices are
Anatol Pomozovf884ab12013-05-08 16:56:16 -070021modelled as an oriented graph of building blocks called entities connected
Laurent Pinchart53e269c2009-12-09 08:40:00 -030022through pads.
23
24An entity is a basic media hardware building block. It can correspond to
25a large variety of logical blocks such as physical hardware devices
26(CMOS sensor for instance), logical hardware devices (a building block
27in a System-on-Chip image processing pipeline), DMA channels or physical
28connectors.
29
30A pad is a connection endpoint through which an entity can interact with
31other entities. Data (not restricted to video) produced by an entity
32flows from the entity's output to one or more entity inputs. Pads should
33not be confused with physical pins at chip boundaries.
34
35A link is a point-to-point oriented connection between two pads, either
36on the same entity or on different entities. Data flows from a source
37pad to a sink pad.
38
39
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030040Media device
41------------
42
43A media device is represented by a struct media_device instance, defined in
44include/media/media-device.h. Allocation of the structure is handled by the
45media device driver, usually by embedding the media_device instance in a
46larger driver-specific structure.
47
48Drivers register media device instances by calling
49
50 media_device_register(struct media_device *mdev);
51
52The caller is responsible for initializing the media_device structure before
53registration. The following fields must be set:
54
55 - dev must point to the parent device (usually a pci_dev, usb_interface or
56 platform_device instance).
57
58 - model must be filled with the device model name as a NUL-terminated UTF-8
59 string. The device/model revision must not be stored in this field.
60
61The following fields are optional:
62
63 - serial is a unique serial number stored as a NUL-terminated ASCII string.
64 The field is big enough to store a GUID in text form. If the hardware
65 doesn't provide a unique serial number this field must be left empty.
66
67 - bus_info represents the location of the device in the system as a
68 NUL-terminated ASCII string. For PCI/PCIe devices bus_info must be set to
69 "PCI:" (or "PCIe:") followed by the value of pci_name(). For USB devices,
70 the usb_make_path() function must be used. This field is used by
71 applications to distinguish between otherwise identical devices that don't
72 provide a serial number.
73
74 - hw_revision is the hardware device revision in a driver-specific format.
75 When possible the revision should be formatted with the KERNEL_VERSION
76 macro.
77
78 - driver_version is formatted with the KERNEL_VERSION macro. The version
79 minor must be incremented when new features are added to the userspace API
80 without breaking binary compatibility. The version major must be
81 incremented when binary compatibility is broken.
82
83Upon successful registration a character device named media[0-9]+ is created.
84The device major and minor numbers are dynamic. The model name is exported as
85a sysfs attribute.
86
87Drivers unregister media device instances by calling
88
89 media_device_unregister(struct media_device *mdev);
90
91Unregistering a media device that hasn't been registered is *NOT* safe.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030092
93
94Entities, pads and links
95------------------------
96
97- Entities
98
99Entities are represented by a struct media_entity instance, defined in
100include/media/media-entity.h. The structure is usually embedded into a
101higher-level structure, such as a v4l2_subdev or video_device instance,
102although drivers can allocate entities directly.
103
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200104Drivers initialize entity pads by calling
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300105
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200106 media_entity_pads_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300107 struct media_pad *pads);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300108
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200109If no pads are needed, drivers could directly fill entity->num_pads
110with 0 and entity->pads with NULL or to call the above function that
111will do the same.
112
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200113The media_entity name, type and flags fields should be initialized before
114calling media_device_register_entity(). Entities embedded in higher-level
115standard structures can have some of those fields set by the higher-level
116framework.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300117
118As the number of pads is known in advance, the pads array is not allocated
119dynamically but is managed by the entity driver. Most drivers will embed the
120pads array in a driver-specific structure, avoiding dynamic allocation.
121
122Drivers must set the direction of every pad in the pads array before calling
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200123media_entity_pads_init. The function will initialize the other pads fields.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300124
125Unlike the number of pads, the total number of links isn't always known in
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200126advance by the entity driver. As an initial estimate, media_entity_pads_init
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300127pre-allocates a number of links equal to the number of pads. The links array
128will be reallocated if it grows beyond the initial estimate.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300129
130Drivers register entities with a media device by calling
131
132 media_device_register_entity(struct media_device *mdev,
133 struct media_entity *entity);
134
135Entities are identified by a unique positive integer ID. Drivers can provide an
136ID by filling the media_entity id field prior to registration, or request the
137media controller framework to assign an ID automatically. Drivers that provide
138IDs manually must ensure that all IDs are unique. IDs are not guaranteed to be
139contiguous even when they are all assigned automatically by the framework.
140
141Drivers unregister entities by calling
142
143 media_device_unregister_entity(struct media_entity *entity);
144
145Unregistering an entity will not change the IDs of the other entities, and the
146ID will never be reused for a newly registered entity.
147
148When a media device is unregistered, all its entities are unregistered
149automatically. No manual entities unregistration is then required.
150
151Drivers free resources associated with an entity by calling
152
153 media_entity_cleanup(struct media_entity *entity);
154
155This function must be called during the cleanup phase after unregistering the
156entity. Note that the media_entity instance itself must be freed explicitly by
157the driver if required.
158
159Entities have flags that describe the entity capabilities and state.
160
161 MEDIA_ENT_FL_DEFAULT indicates the default entity for a given type.
162 This can be used to report the default audio and video devices or the
163 default camera sensor.
164
165Logical entity groups can be defined by setting the group ID of all member
166entities to the same non-zero value. An entity group serves no purpose in the
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200167kernel, but is reported to userspace during entities enumeration.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300168
169Media device drivers should define groups if several entities are logically
170bound together. Example usages include reporting
171
172 - ALSA, VBI and video nodes that carry the same media stream
173 - lens and flash controllers associated with a sensor
174
175- Pads
176
177Pads are represented by a struct media_pad instance, defined in
178include/media/media-entity.h. Each entity stores its pads in a pads array
179managed by the entity driver. Drivers usually embed the array in a
180driver-specific structure.
181
182Pads are identified by their entity and their 0-based index in the pads array.
183Both information are stored in the media_pad structure, making the media_pad
184pointer the canonical way to store and pass link references.
185
186Pads have flags that describe the pad capabilities and state.
187
188 MEDIA_PAD_FL_SINK indicates that the pad supports sinking data.
189 MEDIA_PAD_FL_SOURCE indicates that the pad supports sourcing data.
190
191One and only one of MEDIA_PAD_FL_SINK and MEDIA_PAD_FL_SOURCE must be set for
192each pad.
193
194- Links
195
196Links are represented by a struct media_link instance, defined in
197include/media/media-entity.h. Each entity stores all links originating at or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300198targeting any of its pads in a links array. A given link is thus stored
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300199twice, once in the source entity and once in the target entity. The array is
200pre-allocated and grows dynamically as needed.
201
202Drivers create links by calling
203
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300204 media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300205 struct media_entity *sink, u16 sink_pad,
206 u32 flags);
207
208An entry in the link array of each entity is allocated and stores pointers
209to source and sink pads.
210
211Links have flags that describe the link capabilities and state.
212
213 MEDIA_LNK_FL_ENABLED indicates that the link is enabled and can be used
214 to transfer media data. When two or more links target a sink pad, only
215 one of them can be enabled at a time.
216 MEDIA_LNK_FL_IMMUTABLE indicates that the link enabled state can't be
217 modified at runtime. If MEDIA_LNK_FL_IMMUTABLE is set, then
218 MEDIA_LNK_FL_ENABLED must also be set since an immutable link is always
219 enabled.
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300220
221
222Graph traversal
223---------------
224
225The media framework provides APIs to iterate over entities in a graph.
226
227To iterate over all entities belonging to a media device, drivers can use the
228media_device_for_each_entity macro, defined in include/media/media-device.h.
229
230 struct media_entity *entity;
231
232 media_device_for_each_entity(entity, mdev) {
233 /* entity will point to each entity in turn */
234 ...
235 }
236
237Drivers might also need to iterate over all entities in a graph that can be
238reached only through enabled links starting at a given entity. The media
239framework provides a depth-first graph traversal API for that purpose.
240
241Note that graphs with cycles (whether directed or undirected) are *NOT*
242supported by the graph traversal API. To prevent infinite loops, the graph
243traversal code limits the maximum depth to MEDIA_ENTITY_ENUM_MAX_DEPTH,
244currently defined as 16.
245
246Drivers initiate a graph traversal by calling
247
248 media_entity_graph_walk_start(struct media_entity_graph *graph,
249 struct media_entity *entity);
250
251The graph structure, provided by the caller, is initialized to start graph
252traversal at the given entity.
253
254Drivers can then retrieve the next entity by calling
255
256 media_entity_graph_walk_next(struct media_entity_graph *graph);
257
258When the graph traversal is complete the function will return NULL.
259
260Graph traversal can be interrupted at any moment. No cleanup function call is
261required and the graph structure can be freed normally.
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300262
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300263Helper functions can be used to find a link between two given pads, or a pad
264connected to another pad through an enabled link
265
266 media_entity_find_link(struct media_pad *source,
267 struct media_pad *sink);
268
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300269 media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300270
271Refer to the kerneldoc documentation for more information.
272
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300273
274Use count and power handling
275----------------------------
276
277Due to the wide differences between drivers regarding power management needs,
278the media controller does not implement power management. However, the
279media_entity structure includes a use_count field that media drivers can use to
280track the number of users of every entity for power management needs.
281
282The use_count field is owned by media drivers and must not be touched by entity
283drivers. Access to the field must be protected by the media device graph_mutex
284lock.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300285
286
287Links setup
288-----------
289
290Link properties can be modified at runtime by calling
291
292 media_entity_setup_link(struct media_link *link, u32 flags);
293
294The flags argument contains the requested new link flags.
295
296The only configurable property is the ENABLED link flag to enable/disable a
297link. Links marked with the IMMUTABLE link flag can not be enabled or disabled.
298
299When a link is enabled or disabled, the media framework calls the
300link_setup operation for the two entities at the source and sink of the link,
301in that order. If the second link_setup call fails, another link_setup call is
302made on the first entity to restore the original link flags.
303
304Media device drivers can be notified of link setup operations by setting the
305media_device::link_notify pointer to a callback function. If provided, the
306notification callback will be called before enabling and after disabling
307links.
308
309Entity drivers must implement the link_setup operation if any of their links
310is non-immutable. The operation must either configure the hardware or store
311the configuration information to be applied later.
312
313Link configuration must not have any side effect on other links. If an enabled
Sylwester Nawrocki3751e322011-06-18 05:52:21 -0300314link at a sink pad prevents another link at the same pad from being enabled,
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300315the link_setup operation must return -EBUSY and can't implicitly disable the
316first enabled link.
Laurent Pincharte02188c2010-08-25 09:00:41 -0300317
318
319Pipelines and media streams
320---------------------------
321
322When starting streaming, drivers must notify all entities in the pipeline to
323prevent link states from being modified during streaming by calling
324
325 media_entity_pipeline_start(struct media_entity *entity,
326 struct media_pipeline *pipe);
327
328The function will mark all entities connected to the given entity through
329enabled links, either directly or indirectly, as streaming.
330
331The media_pipeline instance pointed to by the pipe argument will be stored in
332every entity in the pipeline. Drivers should embed the media_pipeline structure
333in higher-level pipeline structures and can then access the pipeline through
334the media_entity pipe field.
335
336Calls to media_entity_pipeline_start() can be nested. The pipeline pointer must
337be identical for all nested calls to the function.
338
Sakari Ailusaf88be32012-01-11 06:25:15 -0300339media_entity_pipeline_start() may return an error. In that case, it will
Antonio Ospitec240ac92013-01-28 17:45:31 -0300340clean up any of the changes it did by itself.
Sakari Ailusaf88be32012-01-11 06:25:15 -0300341
Laurent Pincharte02188c2010-08-25 09:00:41 -0300342When stopping the stream, drivers must notify the entities with
343
344 media_entity_pipeline_stop(struct media_entity *entity);
345
346If multiple calls to media_entity_pipeline_start() have been made the same
347number of media_entity_pipeline_stop() calls are required to stop streaming. The
348media_entity pipe field is reset to NULL on the last nested stop call.
349
350Link configuration will fail with -EBUSY by default if either end of the link is
351a streaming entity. Links that can be modified while streaming must be marked
352with the MEDIA_LNK_FL_DYNAMIC flag.
353
354If other operations need to be disallowed on streaming entities (such as
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300355changing entities configuration parameters) drivers can explicitly check the
Laurent Pincharte02188c2010-08-25 09:00:41 -0300356media_entity stream_count field to find out if an entity is streaming. This
357operation must be done with the media_device graph_mutex held.
Sakari Ailusaf88be32012-01-11 06:25:15 -0300358
359
360Link validation
361---------------
362
363Link validation is performed by media_entity_pipeline_start() for any
364entity which has sink pads in the pipeline. The
365media_entity::link_validate() callback is used for that purpose. In
366link_validate() callback, entity driver should check that the properties of
367the source pad of the connected entity and its own sink pad match. It is up
368to the type of the entity (and in the end, the properties of the hardware)
369what matching actually means.
370
371Subsystems should facilitate link validation by providing subsystem specific
372helper functions to provide easy access for commonly needed information, and
373in the end provide a way to use driver-specific callbacks.