The media controller userspace API is documented in the Media Controller uAPI book. This document focus on the kernel-side implementation of the media framework.
Discovering a device internal topology, and configuring it at runtime, is one of the goals of the media framework. To achieve this, hardware devices are modelled as an oriented graph of building blocks called entities connected through pads.
An entity is a basic media hardware building block. It can correspond to a large variety of logical blocks such as physical hardware devices (CMOS sensor for instance), logical hardware devices (a building block in a System-on-Chip image processing pipeline), DMA channels or physical connectors.
A pad is a connection endpoint through which an entity can interact with other entities. Data (not restricted to video) produced by an entity flows from the entity’s output to one or more entity inputs. Pads should not be confused with physical pins at chip boundaries.
A link is a point-to-point oriented connection between two pads, either on the same entity or on different entities. Data flows from a source pad to a sink pad.
A media device is represented by a struct media_device instance, defined in include/media/media-device.h. Allocation of the structure is handled by the media device driver, usually by embedding the media_device instance in a larger driver-specific structure.
Drivers register media device instances by calling __media_device_register() via the macro media_device_register() and unregistered by calling media_device_unregister().
Entities are represented by a struct media_entity instance, defined in include/media/media-entity.h. The structure is usually embedded into a higher-level structure, such as v4l2_subdev or video_device instances, although drivers can allocate entities directly.
Drivers initialize entity pads by calling media_entity_pads_init().
Drivers register entities with a media device by calling media_device_register_entity() and unregistred by calling media_device_unregister_entity().
Interfaces are represented by a struct media_interface instance, defined in include/media/media-entity.h. Currently, only one type of interface is defined: a device node. Such interfaces are represented by a struct media_intf_devnode.
Drivers initialize and create device node interfaces by calling media_devnode_create() and remove them by calling: media_devnode_remove().
Pads are represented by a struct media_pad instance, defined in include/media/media-entity.h. Each entity stores its pads in a pads array managed by the entity driver. Drivers usually embed the array in a driver-specific structure.
Pads are identified by their entity and their 0-based index in the pads array.
Both information are stored in the struct media_pad, making the struct media_pad pointer the canonical way to store and pass link references.
Pads have flags that describe the pad capabilities and state.
MEDIA_PAD_FL_SINK indicates that the pad supports sinking data. MEDIA_PAD_FL_SOURCE indicates that the pad supports sourcing data.
Note
One and only one of MEDIA_PAD_FL_SINK or MEDIA_PAD_FL_SOURCE must be set for each pad.
Links are represented by a struct media_link instance, defined in include/media/media-entity.h. There are two types of links:
1. pad to pad links:
Associate two entities via their PADs. Each entity has a list that points to all links originating at or targeting any of its pads. A given link is thus stored twice, once in the source entity and once in the target entity.
Drivers create pad to pad links by calling: media_create_pad_link() and remove with media_entity_remove_links().
2. interface to entity links:
Associate one interface to a Link.
Drivers create interface to entity links by calling: media_create_intf_link() and remove with media_remove_intf_links().
Note
Links can only be created after having both ends already created.
Links have flags that describe the link capabilities and state. The valid values are described at media_create_pad_link() and media_create_intf_link().
The media framework provides APIs to iterate over entities in a graph.
To iterate over all entities belonging to a media device, drivers can use the media_device_for_each_entity macro, defined in include/media/media-device.h.
struct media_entity *entity;
media_device_for_each_entity(entity, mdev) {
// entity will point to each entity in turn
...
}
Drivers might also need to iterate over all entities in a graph that can be reached only through enabled links starting at a given entity. The media framework provides a depth-first graph traversal API for that purpose.
Note
Graphs with cycles (whether directed or undirected) are NOT supported by the graph traversal API. To prevent infinite loops, the graph traversal code limits the maximum depth to MEDIA_ENTITY_ENUM_MAX_DEPTH, currently defined as 16.
Drivers initiate a graph traversal by calling media_graph_walk_start()
The graph structure, provided by the caller, is initialized to start graph traversal at the given entity.
Drivers can then retrieve the next entity by calling media_graph_walk_next()
When the graph traversal is complete the function will return NULL.
Graph traversal can be interrupted at any moment. No cleanup function call is required and the graph structure can be freed normally.
Helper functions can be used to find a link between two given pads, or a pad connected to another pad through an enabled link media_entity_find_link() and media_entity_remote_pad().
Due to the wide differences between drivers regarding power management needs, the media controller does not implement power management. However, the struct media_entity includes a use_count field that media drivers can use to track the number of users of every entity for power management needs.
The media_entity.use_count field is owned by media drivers and must not be touched by entity drivers. Access to the field must be protected by the media_device.graph_mutex lock.
Link properties can be modified at runtime by calling media_entity_setup_link().
When starting streaming, drivers must notify all entities in the pipeline to prevent link states from being modified during streaming by calling media_pipeline_start().
The function will mark all entities connected to the given entity through enabled links, either directly or indirectly, as streaming.
The struct media_pipeline instance pointed to by the pipe argument will be stored in every entity in the pipeline. Drivers should embed the struct media_pipeline in higher-level pipeline structures and can then access the pipeline through the struct media_entity pipe field.
Calls to media_pipeline_start() can be nested. The pipeline pointer must be identical for all nested calls to the function.
media_pipeline_start() may return an error. In that case, it will clean up any of the changes it did by itself.
When stopping the stream, drivers must notify the entities with media_pipeline_stop().
If multiple calls to media_pipeline_start() have been made the same number of media_pipeline_stop() calls are required to stop streaming. The media_entity.pipe field is reset to NULL on the last nested stop call.
Link configuration will fail with -EBUSY by default if either end of the link is a streaming entity. Links that can be modified while streaming must be marked with the MEDIA_LNK_FL_DYNAMIC flag.
If other operations need to be disallowed on streaming entities (such as changing entities configuration parameters) drivers can explicitly check the media_entity stream_count field to find out if an entity is streaming. This operation must be done with the media_device graph_mutex held.
Link validation is performed by media_pipeline_start() for any entity which has sink pads in the pipeline. The media_entity.link_validate() callback is used for that purpose. In link_validate() callback, entity driver should check that the properties of the source pad of the connected entity and its own sink pad match. It is up to the type of the entity (and in the end, the properties of the hardware) what matching actually means.
Subsystems should facilitate link validation by providing subsystem specific helper functions to provide easy access for commonly needed information, and in the end provide a way to use driver-specific callbacks.
Media Entity Notify
Definition
struct media_entity_notify {
struct list_head list;
void * notify_data;
void (* notify) (struct media_entity *entity, void *notify_data);
};
Members
Description
Drivers may register a callback to take action when new entities get registered with the media device. This handler is intended for creating links between existing entities and should not create entities and register them.
Media device operations
Definition
struct media_device_ops {
int (* link_notify) (struct media_link *link, u32 flags,unsigned int notification);
};
Members
Media device
Definition
struct media_device {
struct device * dev;
struct media_devnode * devnode;
char model[32];
char driver_name[32];
char serial[40];
char bus_info[32];
u32 hw_revision;
u32 driver_version;
u64 topology_version;
u32 id;
struct ida entity_internal_idx;
int entity_internal_idx_max;
struct list_head entities;
struct list_head interfaces;
struct list_head pads;
struct list_head links;
struct list_head entity_notify;
struct mutex graph_mutex;
struct media_graph pm_count_walk;
void * source_priv;
int (* enable_source) (struct media_entity *entity,struct media_pipeline *pipe);
void (* disable_source) (struct media_entity *entity);
const struct media_device_ops * ops;
};
Members
Description
This structure represents an abstract high-level media device. It allows easy access to entities and provides basic media device-level support. The structure can be allocated directly or embedded in a larger structure.
The parent dev is a physical device. It must be set before registering the media device.
model is a descriptive model name exported through sysfs. It doesn’t have to be unique.
enable_source is a handler to find source entity for the sink entity and activate the link between them if source entity is free. Drivers should call this handler before accessing the source.
disable_source is a handler to find source entity for the sink entity and deactivate the link between them. Drivers should call this handler to release the source.
Use-case: find tuner entity connected to the decoder entity and check if it is available, and activate the the link between them from enable_source and deactivate from disable_source.
Note
Bridge driver is expected to implement and set the handler when media_device is registered or when bridge driver finds the media_device during probe. Bridge driver sets source_priv with information necessary to run enable_source and disable_source handlers. Callers should hold graph_mutex to access and call enable_source and disable_source handlers.
Initialise an entity enumeration
Parameters
Return
zero on success or a negative error code.
Initializes a media device element
Parameters
Description
This function initializes the media device prior to its registration. The media device initialization and registration is split in two functions to avoid race conditions and make the media device available to user-space before the media graph has been completed.
So drivers need to first initialize the media device, register any entity within the media device, create pad to pad links and then finally register the media device by calling media_device_register() as a final step.
Cleanups a media device element
Parameters
Description
This function that will destroy the graph_mutex that is initialized in media_device_init().
Registers a media device element
Parameters
Description
Users, should, instead, call the media_device_register() macro.
The caller is responsible for initializing the media_device structure before registration. The following fields of media_device must be set:
- media_entity.dev must point to the parent device (usually a pci_dev, usb_interface or platform_device instance).
- media_entity.model must be filled with the device model name as a NUL-terminated UTF-8 string. The device/model revision must not be stored in this field.
The following fields are optional:
- media_entity.serial is a unique serial number stored as a NUL-terminated ASCII string. The field is big enough to store a GUID in text form. If the hardware doesn’t provide a unique serial number this field must be left empty.
- media_entity.bus_info represents the location of the device in the system as a NUL-terminated ASCII string. For PCI/PCIe devices media_entity.bus_info must be set to “PCI:” (or “PCIe:”) followed by the value of pci_name(). For USB devices,the usb_make_path() function must be used. This field is used by applications to distinguish between otherwise identical devices that don’t provide a serial number.
- media_entity.hw_revision is the hardware device revision in a driver-specific format. When possible the revision should be formatted with the KERNEL_VERSION() macro.
- media_entity.driver_version is formatted with the KERNEL_VERSION() macro. The version minor must be incremented when new features are added to the userspace API without breaking binary compatibility. The version major must be incremented when binary compatibility is broken.
Note
Return
returns zero on success or a negative error code.
Registers a media device element
Parameters
Description
This macro calls __media_device_register() passing THIS_MODULE as the __media_device_register() second argument (owner).
Unregisters a media device element
Parameters
Description
It is safe to call this function on an unregistered (but initialised) media device.
registers a media entity inside a previously registered media device.
Parameters
Description
Entities are identified by a unique positive integer ID. The media controller framework will such ID automatically. IDs are not guaranteed to be contiguous, and the ID number can change on newer Kernel versions. So, neither the driver nor userspace should hardcode ID numbers to refer to the entities, but, instead, use the framework to find the ID, when needed.
The media_entity name, type and flags fields should be initialized before calling media_device_register_entity(). Entities embedded in higher-level standard structures can have some of those fields set by the higher-level framework.
If the device has pads, media_entity_pads_init() should be called before this function. Otherwise, the media_entity.pad and media_entity.num_pads should be zeroed before calling this function.
Entities have flags that describe the entity capabilities and state:
Note
Drivers should set the entity function before calling this function. Please notice that the values MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and MEDIA_ENT_F_UNKNOWN should not be used by the drivers.
unregisters a media entity.
Parameters
Description
All links associated with the entity and all PADs are automatically unregistered from the media_device when this function is called.
Unregistering an entity will not change the IDs of the other entities and the previoully used ID will never be reused for a newly registered entities.
When a media device is unregistered, all its entities are unregistered automatically. No manual entities unregistration is then required.
Note
The media_entity instance itself must be freed explicitly by the driver if required.
Registers a media entity_notify callback
Parameters
Description
Note
When a new entity is registered, all the registered media_entity_notify callbacks are invoked.
Unregister a media entity notify callback
Parameters
create and initialize a struct media_device from a PCI device.
Parameters
create and initialize a struct media_device from a PCI device.
Parameters
Description
Note
It is better to call media_device_usb_init() instead, as such macro fills driver_name with KBUILD_MODNAME.
create and initialize a struct media_device from a PCI device.
Parameters
Description
This macro calls media_device_usb_init() passing the media_device_usb_init() driver_name parameter filled with KBUILD_MODNAME.
Media device file operations
Definition
struct media_file_operations {
struct module * owner;
ssize_t (* read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (* write) (struct file *, const char __user *, size_t, loff_t *);
unsigned int (* poll) (struct file *, struct poll_table_struct *);
long (* ioctl) (struct file *, unsigned int, unsigned long);
long (* compat_ioctl) (struct file *, unsigned int, unsigned long);
int (* open) (struct file *);
int (* release) (struct file *);
};
Members
Media device node
Definition
struct media_devnode {
struct media_device * media_dev;
const struct media_file_operations * fops;
struct device dev;
struct cdev cdev;
struct device * parent;
int minor;
unsigned long flags;
void (* release) (struct media_devnode *devnode);
};
Members
Description
This structure represents a media-related device node.
The parent is a physical device. It must be set by core or device drivers before registering the node.
register a media device node
Parameters
Description
The registration code assigns minor numbers and registers the new device node with the kernel. An error is returned if no free minor number can be found, or if the registration of the device node fails.
Zero is returned on success.
Note that if the media_devnode_register call fails, the release() callback of the media_devnode structure is not called, so the caller is responsible for freeing any data.
clear the media device node register bit
Parameters
Description
This clears the passed device register bit. Future open calls will be met with errors. Should be called before media_devnode_unregister() to avoid races with unregister and device file open calls.
This function can safely be called if the device node has never been registered or has already been unregistered.
unregister a media device node
Parameters
Description
This unregisters the passed device. Future open calls will be met with errors.
Should be called after media_devnode_unregister_prepare()
returns a pointer to the media_devnode
Parameters
returns true if media_devnode is registered; false otherwise.
Parameters
Note
If mdev is NULL, it also returns false.
type of a graph object
Constants
Define a graph object.
Definition
struct media_gobj {
struct media_device * mdev;
u32 id;
struct list_head list;
};
Members
Description
All objects on the media graph should have this struct embedded
An enumeration of media entities.
Definition
struct media_entity_enum {
unsigned long * bmap;
int idx_max;
};
Members
Media graph traversal state
Definition
struct media_graph {
struct stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
struct media_entity_enum ent_enum;
int top;
};
Members
Media pipeline related information
Definition
struct media_pipeline {
int streaming_count;
struct media_graph graph;
};
Members
A link object part of a media graph.
Definition
struct media_link {
struct media_gobj graph_obj;
struct list_head list;
union {unnamed_union};
struct media_link * reverse;
unsigned long flags;
bool is_backlink;
};
Members
A media pad graph object.
Definition
struct media_pad {
struct media_gobj graph_obj;
struct media_entity * entity;
u16 index;
unsigned long flags;
};
Members
Media entity operations
Definition
struct media_entity_operations {
int (* link_setup) (struct media_entity *entity,const struct media_pad *local,const struct media_pad *remote, u32 flags);
int (* link_validate) (struct media_link *link);
};
Members
Description
Note
Those these callbacks are called with struct media_device.graph_mutex mutex held.
Media entity type
Constants
Description
Media entity objects are often not instantiated directly, but the media entity structure is inherited by (through embedding) other subsystem-specific structures. The media entity type identifies the type of the subclass structure that implements a media entity instance.
This allows runtime type identification of media entities and safe casting to the correct object type. For instance, a media entity structure instance embedded in a v4l2_subdev structure instance will have the type MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a v4l2_subdev structure using the container_of() macro.
A media entity graph object.
Definition
struct media_entity {
struct media_gobj graph_obj;
const char * name;
enum media_entity_type obj_type;
u32 function;
unsigned long flags;
u16 num_pads;
u16 num_links;
u16 num_backlinks;
int internal_idx;
struct media_pad * pads;
struct list_head links;
const struct media_entity_operations * ops;
int stream_count;
int use_count;
struct media_pipeline * pipe;
union info;
};
Members
Description
Note
stream_count and use_count reference counts must never be negative, but are signed integers on purpose: a simple WARN_ON(<0) check can be used to detect reference count bugs that would make them negative.
A media interface graph object.
Definition
struct media_interface {
struct media_gobj graph_obj;
struct list_head links;
u32 type;
u32 flags;
};
Members
Description
Note
Currently, no flags for media_interface is defined.
A media interface via a device node.
Definition
struct media_intf_devnode {
struct media_interface intf;
u32 major;
u32 minor;
};
Members
return the media entity graph object id
Parameters
return the media object type
Parameters
return the media object ID
Parameters
encapsulates type and ID on at the object ID
Parameters
Check if the entity is a video_device
Parameters
Return
true if the entity is an instance of a video_device object and can safely be cast to a struct video_device using the container_of() macro, or false otherwise.
Check if the entity is a v4l2_subdev
Parameters
Return
true if the entity is an instance of a v4l2_subdev object and can safely be cast to a struct v4l2_subdev using the container_of() macro, or false otherwise.
Initialise an entity enumeration
Parameters
Return
Returns zero on success or a negative error code.
Release resources of an entity enumeration
Parameters
Clear the entire enum
Parameters
Mark a single entity in the enum
Parameters
Unmark a single entity in the enum
Parameters
Test whether the entity is marked
Parameters
Description
Returns true if the entity was marked.
Test whether the entity is marked, and mark it
Parameters
Description
Returns true if the entity was marked, and mark it before doing so.
Test whether the entire enum is empty
Parameters
Return
true if the entity was empty.
Test whether two enums intersect
Parameters
Return
true if entity enumerations ent_enum1 and ent_enum2 intersect, otherwise false.
returns the struct media_entity pointer from the gobj contained on it.
Parameters
Parameters
returns the struct media_link pointer from the gobj contained on it.
Parameters
returns the struct media_interface pointer from the gobj contained on it.
Parameters
returns the struct media_intf_devnode pointer from the intf contained on it.
Parameters
Initialize a graph object
Parameters
Description
This routine initializes the embedded struct media_gobj inside a media graph object. It is called automatically if media_*_create function calls are used. However, if the object (entity, link, pad, interface) is embedded on some other object, this function should be called before registering the object at the media controller.
Stop using a graph object on a media device
Parameters
Description
This should be called by all routines like media_device_unregister() that remove/destroy media graph objects.
Initialize the entity pads
Parameters
Description
The pads array is managed by the entity driver and passed to media_entity_pads_init() where its pointer will be stored in the media_entity structure.
If no pads are needed, drivers could either directly fill media_entity->num_pads with 0 and media_entity->pads with NULL or call this function that will do the same.
As the number of pads is known in advance, the pads array is not allocated dynamically but is managed by the entity driver. Most drivers will embed the pads array in a driver-specific structure, avoiding dynamic allocation.
Drivers must set the direction of every pad in the pads array before calling media_entity_pads_init(). The function will initialize the other pads fields.
free resources associated with an entity
Parameters
Description
This function must be called during the cleanup phase after unregistering the entity (currently, it does nothing).
creates a link between two entities.
Parameters
Description
Valid values for flags:
Note
Before calling this function, media_entity_pads_init() and media_device_register_entity() should be called previously for both ends.
creates a link between two entities.
Parameters
Description
Valid values for flags:
It is common for some devices to have multiple source and/or sink entities of the same type that should be linked. While media_create_pad_link() creates link by link, this function is meant to allow 1:n, n:1 and even cross-bar (n:n) links.
Note
Before calling this function, media_entity_pads_init() and media_device_register_entity() should be called previously for the entities to be linked.
remove all links associated with an entity
Parameters
Description
Note
This is called automatically when an entity is unregistered via media_device_register_entity().
Configure a media link without locking
Parameters
Description
The bulk of link setup is handled by the two entities connected through the link. This function notifies both entities of the link configuration change.
If the link is immutable or if the current and new configuration are identical, return immediately.
The user is expected to hold link->source->parent->mutex. If not, media_entity_setup_link() should be used instead.
changes the link flags properties in runtime
Parameters
Description
The only configurable property is the MEDIA_LNK_FL_ENABLED link flag flag to enable/disable a link. Links marked with the MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
When a link is enabled or disabled, the media framework calls the link_setup operation for the two entities at the source and sink of the link, in that order. If the second link_setup call fails, another link_setup call is made on the first entity to restore the original link flags.
Media device drivers can be notified of link setup operations by setting the media_device.link_notify pointer to a callback function. If provided, the notification callback will be called before enabling and after disabling links.
Entity drivers must implement the link_setup operation if any of their links is non-immutable. The operation must either configure the hardware or store the configuration information to be applied later.
Link configuration must not have any side effect on other links. If an enabled link at a sink pad prevents another link at the same pad from being enabled, the link_setup operation must return -EBUSY and can’t implicitly disable the first enabled link.
Note
The valid values of the flags for the link is the same as described on media_create_pad_link(), for pad to pad links or the same as described on media_create_intf_link(), for interface to entity links.
Find a link between two pads
Parameters
Return
returns a pointer to the link between the two entities. If no such link exists, return NULL.
Find the pad at the remote end of a link
Parameters
Description
Search for a remote pad connected to the given pad by iterating over all links originating or terminating at that pad until an enabled link is found.
Return
returns a pointer to the pad at the remote end of the first found enabled link, or NULL if no enabled link has been found.
Get a reference to the parent module
Parameters
Description
Get a reference to the parent media device module.
The function will return immediately if entity is NULL.
Return
returns a pointer to the entity on success or NULL on failure.
Allocate resources used by graph walk.
Parameters
Release resources used by graph walk.
Parameters
Release the reference to the parent module
Parameters
Description
Release the reference count acquired by media_entity_get().
The function will return immediately if entity is NULL.
Start walking the media graph at a given entity
Parameters
Description
Before using this function, media_graph_walk_init() must be used to allocate resources used for walking the graph. This function initializes the graph traversal structure to walk the entities graph starting at the given entity. The traversal structure must not be modified by the caller during graph traversal. After the graph walk, the resources must be released using media_graph_walk_cleanup().
Get the next entity in the graph
Parameters
Description
Perform a depth-first traversal of the given media entities graph.
The graph structure must have been previously initialized with a call to media_graph_walk_start().
Return
returns the next entity in the graph or NULL if the whole graph have been traversed.
Mark a pipeline as streaming
Parameters
Description
Mark all entities connected to a given entity through enabled links, either directly or indirectly, as streaming. The given pipeline object is assigned to every entity in the pipeline and stored in the media_entity pipe field.
Calls to this function can be nested, in which case the same number of media_pipeline_stop() calls will be required to stop streaming. The pipeline pointer must be identical for all nested calls to media_pipeline_start().
Mark a pipeline as streaming
Parameters
Description
..note:: This is the non-locking version of media_pipeline_start()
Mark a pipeline as not streaming
Parameters
Description
Mark all entities connected to a given entity through enabled links, either directly or indirectly, as not streaming. The media_entity pipe field is reset to NULL.
If multiple calls to media_pipeline_start() have been made, the same number of calls to this function are required to mark the pipeline as not streaming.
Mark a pipeline as not streaming
Parameters
Description
Note
This is the non-locking version of media_pipeline_stop()
creates and initializes a device node interface
Parameters
Return
Note
Currently, no flags for media_interface is defined.
removes a device node interface
Parameters
Description
When a device node interface is removed, all links to it are automatically removed.
creates a link between an entity and an interface
Parameters
Description
Valid values for flags:
Indicates that the interface is connected to the entity hardware. That’s the default value for interfaces. An interface may be disabled if the hardware is busy due to the usage of some other interface that it is currently controlling the hardware.
A typical example is an hybrid TV device that handle only one type of stream on a given time. So, when the digital TV is streaming, the V4L2 interfaces won’t be enabled, as such device is not able to also stream analog TV or radio.
Note
Before calling this function, media_devnode_create() should be called for the interface and media_device_register_entity() should be called for the interface that will be part of the link.
remove a single interface link
Parameters
Description
Note
This is an unlocked version of media_remove_intf_link()
remove a single interface link
Parameters
Description
Note
Prefer to use this one, instead of __media_remove_intf_link()
remove all links associated with an interface
Parameters
Description
Note
This is an unlocked version of media_remove_intf_links().
remove all links associated with an interface
Parameters
Description
Note
Calls a struct media_entity_operations operation on an entity
Parameters
Description
This helper function will check if operation is not NULL. On such case, it will issue a call to operation(entity, args).