[media] DocBook: Move media-framework.txt contents to media-device.h
[linux-2.6-block.git] / include / media / media-device.h
1 /*
2  * Media device
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *           Sakari Ailus <sakari.ailus@iki.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef _MEDIA_DEVICE_H
24 #define _MEDIA_DEVICE_H
25
26 #include <linux/list.h>
27 #include <linux/mutex.h>
28 #include <linux/spinlock.h>
29
30 #include <media/media-devnode.h>
31 #include <media/media-entity.h>
32
33 /**
34  * DOC: Media Controller
35  *
36  * Linux kernel media framework
37  * ============================
38  *
39  * This document describes the Linux kernel media framework, its data structures,
40  * functions and their usage.
41  *
42  *
43  * Introduction
44  * ------------
45  *
46  * The media controller API is documented in DocBook format in
47  * Documentation/DocBook/media/v4l/media-controller.xml. This document will focus
48  * on the kernel-side implementation of the media framework.
49  *
50  *
51  * Abstract media device model
52  * ---------------------------
53  *
54  * Discovering a device internal topology, and configuring it at runtime, is one
55  * of the goals of the media framework. To achieve this, hardware devices are
56  * modelled as an oriented graph of building blocks called entities connected
57  * through pads.
58  *
59  * An entity is a basic media hardware building block. It can correspond to
60  * a large variety of logical blocks such as physical hardware devices
61  * (CMOS sensor for instance), logical hardware devices (a building block
62  * in a System-on-Chip image processing pipeline), DMA channels or physical
63  * connectors.
64  *
65  * A pad is a connection endpoint through which an entity can interact with
66  * other entities. Data (not restricted to video) produced by an entity
67  * flows from the entity's output to one or more entity inputs. Pads should
68  * not be confused with physical pins at chip boundaries.
69  *
70  * A link is a point-to-point oriented connection between two pads, either
71  * on the same entity or on different entities. Data flows from a source
72  * pad to a sink pad.
73  *
74  *
75  * Media device
76  * ------------
77  *
78  * A media device is represented by a struct media_device instance, defined in
79  * include/media/media-device.h. Allocation of the structure is handled by the
80  * media device driver, usually by embedding the media_device instance in a
81  * larger driver-specific structure.
82  *
83  * Drivers register media device instances by calling
84  *
85  *      media_device_register(struct media_device *mdev);
86  *
87  * The caller is responsible for initializing the media_device structure before
88  * registration. The following fields must be set:
89  *
90  *  - dev must point to the parent device (usually a pci_dev, usb_interface or
91  *    platform_device instance).
92  *
93  *  - model must be filled with the device model name as a NUL-terminated UTF-8
94  *    string. The device/model revision must not be stored in this field.
95  *
96  * The following fields are optional:
97  *
98  *  - serial is a unique serial number stored as a NUL-terminated ASCII string.
99  *    The field is big enough to store a GUID in text form. If the hardware
100  *    doesn't provide a unique serial number this field must be left empty.
101  *
102  *  - bus_info represents the location of the device in the system as a
103  *    NUL-terminated ASCII string. For PCI/PCIe devices bus_info must be set to
104  *    "PCI:" (or "PCIe:") followed by the value of pci_name(). For USB devices,
105  *    the usb_make_path() function must be used. This field is used by
106  *    applications to distinguish between otherwise identical devices that don't
107  *    provide a serial number.
108  *
109  *  - hw_revision is the hardware device revision in a driver-specific format.
110  *    When possible the revision should be formatted with the KERNEL_VERSION
111  *    macro.
112  *
113  *  - driver_version is formatted with the KERNEL_VERSION macro. The version
114  *    minor must be incremented when new features are added to the userspace API
115  *    without breaking binary compatibility. The version major must be
116  *    incremented when binary compatibility is broken.
117  *
118  * Upon successful registration a character device named media[0-9]+ is created.
119  * The device major and minor numbers are dynamic. The model name is exported as
120  * a sysfs attribute.
121  *
122  * Drivers unregister media device instances by calling
123  *
124  *      media_device_unregister(struct media_device *mdev);
125  *
126  * Unregistering a media device that hasn't been registered is *NOT* safe.
127  *
128  *
129  * Entities, pads and links
130  * ------------------------
131  *
132  * - Entities
133  *
134  * Entities are represented by a struct media_entity instance, defined in
135  * include/media/media-entity.h. The structure is usually embedded into a
136  * higher-level structure, such as a v4l2_subdev or video_device instance,
137  * although drivers can allocate entities directly.
138  *
139  * Drivers initialize entity pads by calling
140  *
141  *      media_entity_pads_init(struct media_entity *entity, u16 num_pads,
142  *                        struct media_pad *pads);
143  *
144  * If no pads are needed, drivers could directly fill entity->num_pads
145  * with 0 and entity->pads with NULL or to call the above function that
146  * will do the same.
147  *
148  * The media_entity name, type and flags fields should be initialized before
149  * calling media_device_register_entity(). Entities embedded in higher-level
150  * standard structures can have some of those fields set by the higher-level
151  * framework.
152  *
153  * As the number of pads is known in advance, the pads array is not allocated
154  * dynamically but is managed by the entity driver. Most drivers will embed the
155  * pads array in a driver-specific structure, avoiding dynamic allocation.
156  *
157  * Drivers must set the direction of every pad in the pads array before calling
158  * media_entity_pads_init. The function will initialize the other pads fields.
159  *
160  * Unlike the number of pads, the total number of links isn't always known in
161  * advance by the entity driver. As an initial estimate, media_entity_pads_init
162  * pre-allocates a number of links equal to the number of pads. The links array
163  * will be reallocated if it grows beyond the initial estimate.
164  *
165  * Drivers register entities with a media device by calling
166  *
167  *      media_device_register_entity(struct media_device *mdev,
168  *                                   struct media_entity *entity);
169  *
170  * Entities are identified by a unique positive integer ID. Drivers can provide an
171  * ID by filling the media_entity id field prior to registration, or request the
172  * media controller framework to assign an ID automatically. Drivers that provide
173  * IDs manually must ensure that all IDs are unique. IDs are not guaranteed to be
174  * contiguous even when they are all assigned automatically by the framework.
175  *
176  * Drivers unregister entities by calling
177  *
178  *      media_device_unregister_entity(struct media_entity *entity);
179  *
180  * Unregistering an entity will not change the IDs of the other entities, and the
181  * ID will never be reused for a newly registered entity.
182  *
183  * When a media device is unregistered, all its entities are unregistered
184  * automatically. No manual entities unregistration is then required.
185  *
186  * Drivers free resources associated with an entity by calling
187  *
188  *      media_entity_cleanup(struct media_entity *entity);
189  *
190  * This function must be called during the cleanup phase after unregistering the
191  * entity. Note that the media_entity instance itself must be freed explicitly by
192  * the driver if required.
193  *
194  * Entities have flags that describe the entity capabilities and state.
195  *
196  *      MEDIA_ENT_FL_DEFAULT indicates the default entity for a given type.
197  *      This can be used to report the default audio and video devices or the
198  *      default camera sensor.
199  *
200  * Logical entity groups can be defined by setting the group ID of all member
201  * entities to the same non-zero value. An entity group serves no purpose in the
202  * kernel, but is reported to userspace during entities enumeration.
203  *
204  * Media device drivers should define groups if several entities are logically
205  * bound together. Example usages include reporting
206  *
207  *      - ALSA, VBI and video nodes that carry the same media stream
208  *      - lens and flash controllers associated with a sensor
209  *
210  * - Pads
211  *
212  * Pads are represented by a struct media_pad instance, defined in
213  * include/media/media-entity.h. Each entity stores its pads in a pads array
214  * managed by the entity driver. Drivers usually embed the array in a
215  * driver-specific structure.
216  *
217  * Pads are identified by their entity and their 0-based index in the pads array.
218  * Both information are stored in the media_pad structure, making the media_pad
219  * pointer the canonical way to store and pass link references.
220  *
221  * Pads have flags that describe the pad capabilities and state.
222  *
223  *      MEDIA_PAD_FL_SINK indicates that the pad supports sinking data.
224  *      MEDIA_PAD_FL_SOURCE indicates that the pad supports sourcing data.
225  *
226  * One and only one of MEDIA_PAD_FL_SINK and MEDIA_PAD_FL_SOURCE must be set for
227  * each pad.
228  *
229  * - Links
230  *
231  * Links are represented by a struct media_link instance, defined in
232  * include/media/media-entity.h. Each entity stores all links originating at or
233  * targeting any of its pads in a links array. A given link is thus stored
234  * twice, once in the source entity and once in the target entity. The array is
235  * pre-allocated and grows dynamically as needed.
236  *
237  * Drivers create links by calling
238  *
239  *      media_create_pad_link(struct media_entity *source, u16 source_pad,
240  *                               struct media_entity *sink,   u16 sink_pad,
241  *                               u32 flags);
242  *
243  * An entry in the link array of each entity is allocated and stores pointers
244  * to source and sink pads.
245  *
246  * Links have flags that describe the link capabilities and state.
247  *
248  *      MEDIA_LNK_FL_ENABLED indicates that the link is enabled and can be used
249  *      to transfer media data. When two or more links target a sink pad, only
250  *      one of them can be enabled at a time.
251  *      MEDIA_LNK_FL_IMMUTABLE indicates that the link enabled state can't be
252  *      modified at runtime. If MEDIA_LNK_FL_IMMUTABLE is set, then
253  *      MEDIA_LNK_FL_ENABLED must also be set since an immutable link is always
254  *      enabled.
255  *
256  *
257  * Graph traversal
258  * ---------------
259  *
260  * The media framework provides APIs to iterate over entities in a graph.
261  *
262  * To iterate over all entities belonging to a media device, drivers can use the
263  * media_device_for_each_entity macro, defined in include/media/media-device.h.
264  *
265  *      struct media_entity *entity;
266  *
267  *      media_device_for_each_entity(entity, mdev) {
268  *              // entity will point to each entity in turn
269  *              ...
270  *      }
271  *
272  * Drivers might also need to iterate over all entities in a graph that can be
273  * reached only through enabled links starting at a given entity. The media
274  * framework provides a depth-first graph traversal API for that purpose.
275  *
276  * Note that graphs with cycles (whether directed or undirected) are *NOT*
277  * supported by the graph traversal API. To prevent infinite loops, the graph
278  * traversal code limits the maximum depth to MEDIA_ENTITY_ENUM_MAX_DEPTH,
279  * currently defined as 16.
280  *
281  * Drivers initiate a graph traversal by calling
282  *
283  *      media_entity_graph_walk_start(struct media_entity_graph *graph,
284  *                                    struct media_entity *entity);
285  *
286  * The graph structure, provided by the caller, is initialized to start graph
287  * traversal at the given entity.
288  *
289  * Drivers can then retrieve the next entity by calling
290  *
291  *      media_entity_graph_walk_next(struct media_entity_graph *graph);
292  *
293  * When the graph traversal is complete the function will return NULL.
294  *
295  * Graph traversal can be interrupted at any moment. No cleanup function call is
296  * required and the graph structure can be freed normally.
297  *
298  * Helper functions can be used to find a link between two given pads, or a pad
299  * connected to another pad through an enabled link
300  *
301  *      media_entity_find_link(struct media_pad *source,
302  *                             struct media_pad *sink);
303  *
304  *      media_entity_remote_pad(struct media_pad *pad);
305  *
306  * Refer to the kerneldoc documentation for more information.
307  *
308  *
309  * Use count and power handling
310  * ----------------------------
311  *
312  * Due to the wide differences between drivers regarding power management needs,
313  * the media controller does not implement power management. However, the
314  * media_entity structure includes a use_count field that media drivers can use to
315  * track the number of users of every entity for power management needs.
316  *
317  * The use_count field is owned by media drivers and must not be touched by entity
318  * drivers. Access to the field must be protected by the media device graph_mutex
319  * lock.
320  *
321  *
322  * Links setup
323  * -----------
324  *
325  * Link properties can be modified at runtime by calling
326  *
327  *      media_entity_setup_link(struct media_link *link, u32 flags);
328  *
329  * The flags argument contains the requested new link flags.
330  *
331  * The only configurable property is the ENABLED link flag to enable/disable a
332  * link. Links marked with the IMMUTABLE link flag can not be enabled or disabled.
333  *
334  * When a link is enabled or disabled, the media framework calls the
335  * link_setup operation for the two entities at the source and sink of the link,
336  * in that order. If the second link_setup call fails, another link_setup call is
337  * made on the first entity to restore the original link flags.
338  *
339  * Media device drivers can be notified of link setup operations by setting the
340  * media_device::link_notify pointer to a callback function. If provided, the
341  * notification callback will be called before enabling and after disabling
342  * links.
343  *
344  * Entity drivers must implement the link_setup operation if any of their links
345  * is non-immutable. The operation must either configure the hardware or store
346  * the configuration information to be applied later.
347  *
348  * Link configuration must not have any side effect on other links. If an enabled
349  * link at a sink pad prevents another link at the same pad from being enabled,
350  * the link_setup operation must return -EBUSY and can't implicitly disable the
351  * first enabled link.
352  *
353  *
354  * Pipelines and media streams
355  * ---------------------------
356  *
357  * When starting streaming, drivers must notify all entities in the pipeline to
358  * prevent link states from being modified during streaming by calling
359  *
360  *      media_entity_pipeline_start(struct media_entity *entity,
361  *                                  struct media_pipeline *pipe);
362  *
363  * The function will mark all entities connected to the given entity through
364  * enabled links, either directly or indirectly, as streaming.
365  *
366  * The media_pipeline instance pointed to by the pipe argument will be stored in
367  * every entity in the pipeline. Drivers should embed the media_pipeline structure
368  * in higher-level pipeline structures and can then access the pipeline through
369  * the media_entity pipe field.
370  *
371  * Calls to media_entity_pipeline_start() can be nested. The pipeline pointer must
372  * be identical for all nested calls to the function.
373  *
374  * media_entity_pipeline_start() may return an error. In that case, it will
375  * clean up any of the changes it did by itself.
376  *
377  * When stopping the stream, drivers must notify the entities with
378  *
379  *      media_entity_pipeline_stop(struct media_entity *entity);
380  *
381  * If multiple calls to media_entity_pipeline_start() have been made the same
382  * number of media_entity_pipeline_stop() calls are required to stop streaming. The
383  * media_entity pipe field is reset to NULL on the last nested stop call.
384  *
385  * Link configuration will fail with -EBUSY by default if either end of the link is
386  * a streaming entity. Links that can be modified while streaming must be marked
387  * with the MEDIA_LNK_FL_DYNAMIC flag.
388  *
389  * If other operations need to be disallowed on streaming entities (such as
390  * changing entities configuration parameters) drivers can explicitly check the
391  * media_entity stream_count field to find out if an entity is streaming. This
392  * operation must be done with the media_device graph_mutex held.
393  *
394  *
395  * Link validation
396  * ---------------
397  *
398  * Link validation is performed by media_entity_pipeline_start() for any
399  * entity which has sink pads in the pipeline. The
400  * media_entity::link_validate() callback is used for that purpose. In
401  * link_validate() callback, entity driver should check that the properties of
402  * the source pad of the connected entity and its own sink pad match. It is up
403  * to the type of the entity (and in the end, the properties of the hardware)
404  * what matching actually means.
405  *
406  * Subsystems should facilitate link validation by providing subsystem specific
407  * helper functions to provide easy access for commonly needed information, and
408  * in the end provide a way to use driver-specific callbacks.
409  */
410
411 struct device;
412
413 /**
414  * struct media_device - Media device
415  * @dev:        Parent device
416  * @devnode:    Media device node
417  * @model:      Device model name
418  * @serial:     Device serial number (optional)
419  * @bus_info:   Unique and stable device location identifier
420  * @hw_revision: Hardware device revision
421  * @driver_version: Device driver version
422  * @topology_version: Monotonic counter for storing the version of the graph
423  *              topology. Should be incremented each time the topology changes.
424  * @entity_id:  Unique ID used on the last entity registered
425  * @pad_id:     Unique ID used on the last pad registered
426  * @link_id:    Unique ID used on the last link registered
427  * @intf_devnode_id: Unique ID used on the last interface devnode registered
428  * @entities:   List of registered entities
429  * @interfaces: List of registered interfaces
430  * @pads:       List of registered pads
431  * @links:      List of registered links
432  * @lock:       Entities list lock
433  * @graph_mutex: Entities graph operation lock
434  * @link_notify: Link state change notification callback
435  *
436  * This structure represents an abstract high-level media device. It allows easy
437  * access to entities and provides basic media device-level support. The
438  * structure can be allocated directly or embedded in a larger structure.
439  *
440  * The parent @dev is a physical device. It must be set before registering the
441  * media device.
442  *
443  * @model is a descriptive model name exported through sysfs. It doesn't have to
444  * be unique.
445  */
446 struct media_device {
447         /* dev->driver_data points to this struct. */
448         struct device *dev;
449         struct media_devnode devnode;
450
451         char model[32];
452         char serial[40];
453         char bus_info[32];
454         u32 hw_revision;
455         u32 driver_version;
456
457         u32 topology_version;
458
459         u32 entity_id;
460         u32 pad_id;
461         u32 link_id;
462         u32 intf_devnode_id;
463
464         struct list_head entities;
465         struct list_head interfaces;
466         struct list_head pads;
467         struct list_head links;
468
469         /* Protects the graph objects creation/removal */
470         spinlock_t lock;
471         /* Serializes graph operations. */
472         struct mutex graph_mutex;
473
474         int (*link_notify)(struct media_link *link, u32 flags,
475                            unsigned int notification);
476 };
477
478 #ifdef CONFIG_MEDIA_CONTROLLER
479
480 /* Supported link_notify @notification values. */
481 #define MEDIA_DEV_NOTIFY_PRE_LINK_CH    0
482 #define MEDIA_DEV_NOTIFY_POST_LINK_CH   1
483
484 /* media_devnode to media_device */
485 #define to_media_device(node) container_of(node, struct media_device, devnode)
486
487 int __must_check __media_device_register(struct media_device *mdev,
488                                          struct module *owner);
489 #define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)
490 void media_device_unregister(struct media_device *mdev);
491
492 int __must_check media_device_register_entity(struct media_device *mdev,
493                                               struct media_entity *entity);
494 void media_device_unregister_entity(struct media_entity *entity);
495 struct media_device *media_device_get_devres(struct device *dev);
496 struct media_device *media_device_find_devres(struct device *dev);
497
498 /* Iterate over all entities. */
499 #define media_device_for_each_entity(entity, mdev)                      \
500         list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
501
502 /* Iterate over all interfaces. */
503 #define media_device_for_each_intf(intf, mdev)                  \
504         list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
505
506 /* Iterate over all pads. */
507 #define media_device_for_each_pad(pad, mdev)                    \
508         list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
509
510 /* Iterate over all links. */
511 #define media_device_for_each_link(link, mdev)                  \
512         list_for_each_entry(link, &(mdev)->links, graph_obj.list)
513
514
515 #else
516 static inline int media_device_register(struct media_device *mdev)
517 {
518         return 0;
519 }
520 static inline void media_device_unregister(struct media_device *mdev)
521 {
522 }
523 static inline int media_device_register_entity(struct media_device *mdev,
524                                                 struct media_entity *entity)
525 {
526         return 0;
527 }
528 static inline void media_device_unregister_entity(struct media_entity *entity)
529 {
530 }
531 static inline struct media_device *media_device_get_devres(struct device *dev)
532 {
533         return NULL;
534 }
535 static inline struct media_device *media_device_find_devres(struct device *dev)
536 {
537         return NULL;
538 }
539 #endif /* CONFIG_MEDIA_CONTROLLER */
540 #endif