]> git.karo-electronics.de Git - karo-tx-linux.git/blob - Documentation/media-framework.txt
[media] media framework: rename pads init function to media_entity_pads_init()
[karo-tx-linux.git] / Documentation / media-framework.txt
1 Linux kernel media framework
2 ============================
3
4 This document describes the Linux kernel media framework, its data structures,
5 functions and their usage.
6
7
8 Introduction
9 ------------
10
11 The media controller API is documented in DocBook format in
12 Documentation/DocBook/media/v4l/media-controller.xml. This document will focus
13 on the kernel-side implementation of the media framework.
14
15
16 Abstract media device model
17 ---------------------------
18
19 Discovering a device internal topology, and configuring it at runtime, is one
20 of the goals of the media framework. To achieve this, hardware devices are
21 modelled as an oriented graph of building blocks called entities connected
22 through pads.
23
24 An entity is a basic media hardware building block. It can correspond to
25 a large variety of logical blocks such as physical hardware devices
26 (CMOS sensor for instance), logical hardware devices (a building block
27 in a System-on-Chip image processing pipeline), DMA channels or physical
28 connectors.
29
30 A pad is a connection endpoint through which an entity can interact with
31 other entities. Data (not restricted to video) produced by an entity
32 flows from the entity's output to one or more entity inputs. Pads should
33 not be confused with physical pins at chip boundaries.
34
35 A link is a point-to-point oriented connection between two pads, either
36 on the same entity or on different entities. Data flows from a source
37 pad to a sink pad.
38
39
40 Media device
41 ------------
42
43 A media device is represented by a struct media_device instance, defined in
44 include/media/media-device.h. Allocation of the structure is handled by the
45 media device driver, usually by embedding the media_device instance in a
46 larger driver-specific structure.
47
48 Drivers register media device instances by calling
49
50         media_device_register(struct media_device *mdev);
51
52 The caller is responsible for initializing the media_device structure before
53 registration. 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
61 The 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
83 Upon successful registration a character device named media[0-9]+ is created.
84 The device major and minor numbers are dynamic. The model name is exported as
85 a sysfs attribute.
86
87 Drivers unregister media device instances by calling
88
89         media_device_unregister(struct media_device *mdev);
90
91 Unregistering a media device that hasn't been registered is *NOT* safe.
92
93
94 Entities, pads and links
95 ------------------------
96
97 - Entities
98
99 Entities are represented by a struct media_entity instance, defined in
100 include/media/media-entity.h. The structure is usually embedded into a
101 higher-level structure, such as a v4l2_subdev or video_device instance,
102 although drivers can allocate entities directly.
103
104 Drivers initialize entity pads by calling
105
106         media_entity_pads_init(struct media_entity *entity, u16 num_pads,
107                           struct media_pad *pads);
108
109 If no pads are needed, drivers could directly fill entity->num_pads
110 with 0 and entity->pads with NULL or to call the above function that
111 will do the same.
112
113 The media_entity name, type, flags, revision and group_id fields should be
114 initialized before calling media_device_register_entity(). Entities embedded
115 in higher-level standard structures can have some of those fields set by the
116 higher-level framework.
117
118 As the number of pads is known in advance, the pads array is not allocated
119 dynamically but is managed by the entity driver. Most drivers will embed the
120 pads array in a driver-specific structure, avoiding dynamic allocation.
121
122 Drivers must set the direction of every pad in the pads array before calling
123 media_entity_pads_init. The function will initialize the other pads fields.
124
125 Unlike the number of pads, the total number of links isn't always known in
126 advance by the entity driver. As an initial estimate, media_entity_pads_init
127 pre-allocates a number of links equal to the number of pads. The links array
128 will be reallocated if it grows beyond the initial estimate.
129
130 Drivers register entities with a media device by calling
131
132         media_device_register_entity(struct media_device *mdev,
133                                      struct media_entity *entity);
134
135 Entities are identified by a unique positive integer ID. Drivers can provide an
136 ID by filling the media_entity id field prior to registration, or request the
137 media controller framework to assign an ID automatically. Drivers that provide
138 IDs manually must ensure that all IDs are unique. IDs are not guaranteed to be
139 contiguous even when they are all assigned automatically by the framework.
140
141 Drivers unregister entities by calling
142
143         media_device_unregister_entity(struct media_entity *entity);
144
145 Unregistering an entity will not change the IDs of the other entities, and the
146 ID will never be reused for a newly registered entity.
147
148 When a media device is unregistered, all its entities are unregistered
149 automatically. No manual entities unregistration is then required.
150
151 Drivers free resources associated with an entity by calling
152
153         media_entity_cleanup(struct media_entity *entity);
154
155 This function must be called during the cleanup phase after unregistering the
156 entity. Note that the media_entity instance itself must be freed explicitly by
157 the driver if required.
158
159 Entities 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
165 Logical entity groups can be defined by setting the group ID of all member
166 entities to the same non-zero value. An entity group serves no purpose in the
167 kernel, but is reported to userspace during entities enumeration. The group_id
168 field belongs to the media device driver and must not by touched by entity
169 drivers.
170
171 Media device drivers should define groups if several entities are logically
172 bound together. Example usages include reporting
173
174         - ALSA, VBI and video nodes that carry the same media stream
175         - lens and flash controllers associated with a sensor
176
177 - Pads
178
179 Pads are represented by a struct media_pad instance, defined in
180 include/media/media-entity.h. Each entity stores its pads in a pads array
181 managed by the entity driver. Drivers usually embed the array in a
182 driver-specific structure.
183
184 Pads are identified by their entity and their 0-based index in the pads array.
185 Both information are stored in the media_pad structure, making the media_pad
186 pointer the canonical way to store and pass link references.
187
188 Pads have flags that describe the pad capabilities and state.
189
190         MEDIA_PAD_FL_SINK indicates that the pad supports sinking data.
191         MEDIA_PAD_FL_SOURCE indicates that the pad supports sourcing data.
192
193 One and only one of MEDIA_PAD_FL_SINK and MEDIA_PAD_FL_SOURCE must be set for
194 each pad.
195
196 - Links
197
198 Links are represented by a struct media_link instance, defined in
199 include/media/media-entity.h. Each entity stores all links originating at or
200 targeting any of its pads in a links array. A given link is thus stored
201 twice, once in the source entity and once in the target entity. The array is
202 pre-allocated and grows dynamically as needed.
203
204 Drivers create links by calling
205
206         media_create_pad_link(struct media_entity *source, u16 source_pad,
207                                  struct media_entity *sink,   u16 sink_pad,
208                                  u32 flags);
209
210 An entry in the link array of each entity is allocated and stores pointers
211 to source and sink pads.
212
213 Links have flags that describe the link capabilities and state.
214
215         MEDIA_LNK_FL_ENABLED indicates that the link is enabled and can be used
216         to transfer media data. When two or more links target a sink pad, only
217         one of them can be enabled at a time.
218         MEDIA_LNK_FL_IMMUTABLE indicates that the link enabled state can't be
219         modified at runtime. If MEDIA_LNK_FL_IMMUTABLE is set, then
220         MEDIA_LNK_FL_ENABLED must also be set since an immutable link is always
221         enabled.
222
223
224 Graph traversal
225 ---------------
226
227 The media framework provides APIs to iterate over entities in a graph.
228
229 To iterate over all entities belonging to a media device, drivers can use the
230 media_device_for_each_entity macro, defined in include/media/media-device.h.
231
232         struct media_entity *entity;
233
234         media_device_for_each_entity(entity, mdev) {
235                 /* entity will point to each entity in turn */
236                 ...
237         }
238
239 Drivers might also need to iterate over all entities in a graph that can be
240 reached only through enabled links starting at a given entity. The media
241 framework provides a depth-first graph traversal API for that purpose.
242
243 Note that graphs with cycles (whether directed or undirected) are *NOT*
244 supported by the graph traversal API. To prevent infinite loops, the graph
245 traversal code limits the maximum depth to MEDIA_ENTITY_ENUM_MAX_DEPTH,
246 currently defined as 16.
247
248 Drivers initiate a graph traversal by calling
249
250         media_entity_graph_walk_start(struct media_entity_graph *graph,
251                                       struct media_entity *entity);
252
253 The graph structure, provided by the caller, is initialized to start graph
254 traversal at the given entity.
255
256 Drivers can then retrieve the next entity by calling
257
258         media_entity_graph_walk_next(struct media_entity_graph *graph);
259
260 When the graph traversal is complete the function will return NULL.
261
262 Graph traversal can be interrupted at any moment. No cleanup function call is
263 required and the graph structure can be freed normally.
264
265 Helper functions can be used to find a link between two given pads, or a pad
266 connected to another pad through an enabled link
267
268         media_entity_find_link(struct media_pad *source,
269                                struct media_pad *sink);
270
271         media_entity_remote_pad(struct media_pad *pad);
272
273 Refer to the kerneldoc documentation for more information.
274
275
276 Use count and power handling
277 ----------------------------
278
279 Due to the wide differences between drivers regarding power management needs,
280 the media controller does not implement power management. However, the
281 media_entity structure includes a use_count field that media drivers can use to
282 track the number of users of every entity for power management needs.
283
284 The use_count field is owned by media drivers and must not be touched by entity
285 drivers. Access to the field must be protected by the media device graph_mutex
286 lock.
287
288
289 Links setup
290 -----------
291
292 Link properties can be modified at runtime by calling
293
294         media_entity_setup_link(struct media_link *link, u32 flags);
295
296 The flags argument contains the requested new link flags.
297
298 The only configurable property is the ENABLED link flag to enable/disable a
299 link. Links marked with the IMMUTABLE link flag can not be enabled or disabled.
300
301 When a link is enabled or disabled, the media framework calls the
302 link_setup operation for the two entities at the source and sink of the link,
303 in that order. If the second link_setup call fails, another link_setup call is
304 made on the first entity to restore the original link flags.
305
306 Media device drivers can be notified of link setup operations by setting the
307 media_device::link_notify pointer to a callback function. If provided, the
308 notification callback will be called before enabling and after disabling
309 links.
310
311 Entity drivers must implement the link_setup operation if any of their links
312 is non-immutable. The operation must either configure the hardware or store
313 the configuration information to be applied later.
314
315 Link configuration must not have any side effect on other links. If an enabled
316 link at a sink pad prevents another link at the same pad from being enabled,
317 the link_setup operation must return -EBUSY and can't implicitly disable the
318 first enabled link.
319
320
321 Pipelines and media streams
322 ---------------------------
323
324 When starting streaming, drivers must notify all entities in the pipeline to
325 prevent link states from being modified during streaming by calling
326
327         media_entity_pipeline_start(struct media_entity *entity,
328                                     struct media_pipeline *pipe);
329
330 The function will mark all entities connected to the given entity through
331 enabled links, either directly or indirectly, as streaming.
332
333 The media_pipeline instance pointed to by the pipe argument will be stored in
334 every entity in the pipeline. Drivers should embed the media_pipeline structure
335 in higher-level pipeline structures and can then access the pipeline through
336 the media_entity pipe field.
337
338 Calls to media_entity_pipeline_start() can be nested. The pipeline pointer must
339 be identical for all nested calls to the function.
340
341 media_entity_pipeline_start() may return an error. In that case, it will
342 clean up any of the changes it did by itself.
343
344 When stopping the stream, drivers must notify the entities with
345
346         media_entity_pipeline_stop(struct media_entity *entity);
347
348 If multiple calls to media_entity_pipeline_start() have been made the same
349 number of media_entity_pipeline_stop() calls are required to stop streaming. The
350 media_entity pipe field is reset to NULL on the last nested stop call.
351
352 Link configuration will fail with -EBUSY by default if either end of the link is
353 a streaming entity. Links that can be modified while streaming must be marked
354 with the MEDIA_LNK_FL_DYNAMIC flag.
355
356 If other operations need to be disallowed on streaming entities (such as
357 changing entities configuration parameters) drivers can explicitly check the
358 media_entity stream_count field to find out if an entity is streaming. This
359 operation must be done with the media_device graph_mutex held.
360
361
362 Link validation
363 ---------------
364
365 Link validation is performed by media_entity_pipeline_start() for any
366 entity which has sink pads in the pipeline. The
367 media_entity::link_validate() callback is used for that purpose. In
368 link_validate() callback, entity driver should check that the properties of
369 the source pad of the connected entity and its own sink pad match. It is up
370 to the type of the entity (and in the end, the properties of the hardware)
371 what matching actually means.
372
373 Subsystems should facilitate link validation by providing subsystem specific
374 helper functions to provide easy access for commonly needed information, and
375 in the end provide a way to use driver-specific callbacks.