/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Media entity3*4* Copyright (C) 2010 Nokia Corporation5*6* Contacts: Laurent Pinchart <[email protected]>7* Sakari Ailus <[email protected]>8*/910#ifndef _MEDIA_ENTITY_H11#define _MEDIA_ENTITY_H1213#include <linux/bitmap.h>14#include <linux/bug.h>15#include <linux/container_of.h>16#include <linux/fwnode.h>17#include <linux/list.h>18#include <linux/media.h>19#include <linux/minmax.h>20#include <linux/types.h>2122/* Enums used internally at the media controller to represent graphs */2324/**25* enum media_gobj_type - type of a graph object26*27* @MEDIA_GRAPH_ENTITY: Identify a media entity28* @MEDIA_GRAPH_PAD: Identify a media pad29* @MEDIA_GRAPH_LINK: Identify a media link30* @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via31* a device node32*/33enum media_gobj_type {34MEDIA_GRAPH_ENTITY,35MEDIA_GRAPH_PAD,36MEDIA_GRAPH_LINK,37MEDIA_GRAPH_INTF_DEVNODE,38};3940#define MEDIA_BITS_PER_TYPE 841#define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE)42#define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)4344/* Structs to represent the objects that belong to a media graph */4546/**47* struct media_gobj - Define a graph object.48*49* @mdev: Pointer to the struct &media_device that owns the object50* @id: Non-zero object ID identifier. The ID should be unique51* inside a media_device, as it is composed by52* %MEDIA_BITS_PER_TYPE to store the type plus53* %MEDIA_BITS_PER_ID to store the ID54* @list: List entry stored in one of the per-type mdev object lists55*56* All objects on the media graph should have this struct embedded57*/58struct media_gobj {59struct media_device *mdev;60u32 id;61struct list_head list;62};6364#define MEDIA_ENTITY_ENUM_MAX_DEPTH 166566/**67* struct media_entity_enum - An enumeration of media entities.68*69* @bmap: Bit map in which each bit represents one entity at struct70* media_entity->internal_idx.71* @idx_max: Number of bits in bmap72*/73struct media_entity_enum {74unsigned long *bmap;75int idx_max;76};7778/**79* struct media_graph - Media graph traversal state80*81* @stack: Graph traversal stack; the stack contains information82* on the path the media entities to be walked and the83* links through which they were reached.84* @stack.entity: pointer to &struct media_entity at the graph.85* @stack.link: pointer to &struct list_head.86* @ent_enum: Visited entities87* @top: The top of the stack88*/89struct media_graph {90struct {91struct media_entity *entity;92struct list_head *link;93} stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];9495struct media_entity_enum ent_enum;96int top;97};9899/**100* struct media_pipeline - Media pipeline related information101*102* @allocated: Media pipeline allocated and freed by the framework103* @mdev: The media device the pipeline is part of104* @pads: List of media_pipeline_pad105* @start_count: Media pipeline start - stop count106*/107struct media_pipeline {108bool allocated;109struct media_device *mdev;110struct list_head pads;111int start_count;112};113114/**115* struct media_pipeline_pad - A pad part of a media pipeline116*117* @list: Entry in the media_pad pads list118* @pipe: The media_pipeline that the pad is part of119* @pad: The media pad120*121* This structure associate a pad with a media pipeline. Instances of122* media_pipeline_pad are created by media_pipeline_start() when it builds the123* pipeline, and stored in the &media_pad.pads list. media_pipeline_stop()124* removes the entries from the list and deletes them.125*/126struct media_pipeline_pad {127struct list_head list;128struct media_pipeline *pipe;129struct media_pad *pad;130};131132/**133* struct media_pipeline_pad_iter - Iterator for media_pipeline_for_each_pad134*135* @cursor: The current element136*/137struct media_pipeline_pad_iter {138struct list_head *cursor;139};140141/**142* struct media_pipeline_entity_iter - Iterator for media_pipeline_for_each_entity143*144* @ent_enum: The entity enumeration tracker145* @cursor: The current element146*/147struct media_pipeline_entity_iter {148struct media_entity_enum ent_enum;149struct list_head *cursor;150};151152/**153* struct media_link - A link object part of a media graph.154*155* @graph_obj: Embedded structure containing the media object common data156* @list: Linked list associated with an entity or an interface that157* owns the link.158* @gobj0: Part of a union. Used to get the pointer for the first159* graph_object of the link.160* @source: Part of a union. Used only if the first object (gobj0) is161* a pad. In that case, it represents the source pad.162* @intf: Part of a union. Used only if the first object (gobj0) is163* an interface.164* @gobj1: Part of a union. Used to get the pointer for the second165* graph_object of the link.166* @sink: Part of a union. Used only if the second object (gobj1) is167* a pad. In that case, it represents the sink pad.168* @entity: Part of a union. Used only if the second object (gobj1) is169* an entity.170* @reverse: Pointer to the link for the reverse direction of a pad to pad171* link.172* @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)173* @is_backlink: Indicate if the link is a backlink.174*/175struct media_link {176struct media_gobj graph_obj;177struct list_head list;178union {179struct media_gobj *gobj0;180struct media_pad *source;181struct media_interface *intf;182};183union {184struct media_gobj *gobj1;185struct media_pad *sink;186struct media_entity *entity;187};188struct media_link *reverse;189unsigned long flags;190bool is_backlink;191};192193/**194* enum media_pad_signal_type - type of the signal inside a media pad195*196* @PAD_SIGNAL_DEFAULT:197* Default signal. Use this when all inputs or all outputs are198* uniquely identified by the pad number.199* @PAD_SIGNAL_ANALOG:200* The pad contains an analog signal. It can be Radio Frequency,201* Intermediate Frequency, a baseband signal or sub-carriers.202* Tuner inputs, IF-PLL demodulators, composite and s-video signals203* should use it.204* @PAD_SIGNAL_DV:205* Contains a digital video signal, with can be a bitstream of samples206* taken from an analog TV video source. On such case, it usually207* contains the VBI data on it.208* @PAD_SIGNAL_AUDIO:209* Contains an Intermediate Frequency analog signal from an audio210* sub-carrier or an audio bitstream. IF signals are provided by tuners211* and consumed by audio AM/FM decoders. Bitstream audio is provided by212* an audio decoder.213*/214enum media_pad_signal_type {215PAD_SIGNAL_DEFAULT = 0,216PAD_SIGNAL_ANALOG,217PAD_SIGNAL_DV,218PAD_SIGNAL_AUDIO,219};220221/**222* struct media_pad - A media pad graph object.223*224* @graph_obj: Embedded structure containing the media object common data225* @entity: Entity this pad belongs to226* @index: Pad index in the entity pads array, numbered from 0 to n227* @num_links: Number of links connected to this pad228* @sig_type: Type of the signal inside a media pad229* @flags: Pad flags, as defined in230* :ref:`include/uapi/linux/media.h <media_header>`231* (seek for ``MEDIA_PAD_FL_*``)232* @pipe: Pipeline this pad belongs to. Use media_entity_pipeline() to233* access this field.234*/235struct media_pad {236struct media_gobj graph_obj; /* must be first field in struct */237struct media_entity *entity;238u16 index;239u16 num_links;240enum media_pad_signal_type sig_type;241unsigned long flags;242243/*244* The fields below are private, and should only be accessed via245* appropriate functions.246*/247struct media_pipeline *pipe;248};249250/**251* struct media_entity_operations - Media entity operations252* @get_fwnode_pad: Return the pad number based on a fwnode endpoint or253* a negative value on error. This operation can be used254* to map a fwnode to a media pad number. Optional.255* @link_setup: Notify the entity of link changes. The operation can256* return an error, in which case link setup will be257* cancelled. Optional.258* @link_validate: Return whether a link is valid from the entity point of259* view. The media_pipeline_start() function260* validates all links by calling this operation. Optional.261* @has_pad_interdep: Return whether two pads of the entity are262* interdependent. If two pads are interdependent they are263* part of the same pipeline and enabling one of the pads264* means that the other pad will become "locked" and265* doesn't allow configuration changes. pad0 and pad1 are266* guaranteed to not both be sinks or sources. Never call267* the .has_pad_interdep() operation directly, always use268* media_entity_has_pad_interdep().269* Optional: If the operation isn't implemented all pads270* will be considered as interdependent.271*272* .. note::273*274* Those these callbacks are called with struct &media_device.graph_mutex275* mutex held.276*/277struct media_entity_operations {278int (*get_fwnode_pad)(struct media_entity *entity,279struct fwnode_endpoint *endpoint);280int (*link_setup)(struct media_entity *entity,281const struct media_pad *local,282const struct media_pad *remote, u32 flags);283int (*link_validate)(struct media_link *link);284bool (*has_pad_interdep)(struct media_entity *entity, unsigned int pad0,285unsigned int pad1);286};287288/**289* enum media_entity_type - Media entity type290*291* @MEDIA_ENTITY_TYPE_BASE:292* The entity isn't embedded in another subsystem structure.293* @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:294* The entity is embedded in a struct video_device instance.295* @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:296* The entity is embedded in a struct v4l2_subdev instance.297*298* Media entity objects are often not instantiated directly, but the media299* entity structure is inherited by (through embedding) other subsystem-specific300* structures. The media entity type identifies the type of the subclass301* structure that implements a media entity instance.302*303* This allows runtime type identification of media entities and safe casting to304* the correct object type. For instance, a media entity structure instance305* embedded in a v4l2_subdev structure instance will have the type306* %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev307* structure using the container_of() macro.308*/309enum media_entity_type {310MEDIA_ENTITY_TYPE_BASE,311MEDIA_ENTITY_TYPE_VIDEO_DEVICE,312MEDIA_ENTITY_TYPE_V4L2_SUBDEV,313};314315/**316* struct media_entity - A media entity graph object.317*318* @graph_obj: Embedded structure containing the media object common data.319* @name: Entity name.320* @obj_type: Type of the object that implements the media_entity.321* @function: Entity main function, as defined in322* :ref:`include/uapi/linux/media.h <media_header>`323* (seek for ``MEDIA_ENT_F_*``)324* @flags: Entity flags, as defined in325* :ref:`include/uapi/linux/media.h <media_header>`326* (seek for ``MEDIA_ENT_FL_*``)327* @num_pads: Number of sink and source pads.328* @num_links: Total number of links, forward and back, enabled and disabled.329* @num_backlinks: Number of backlinks330* @internal_idx: An unique internal entity specific number. The numbers are331* re-used if entities are unregistered or registered again.332* @pads: Pads array with the size defined by @num_pads.333* @links: List of data links.334* @ops: Entity operations.335* @use_count: Use count for the entity.336* @info: Union with devnode information. Kept just for backward337* compatibility.338* @info.dev: Contains device major and minor info.339* @info.dev.major: device node major, if the device is a devnode.340* @info.dev.minor: device node minor, if the device is a devnode.341*342* .. note::343*344* The @use_count reference count must never be negative, but is a signed345* integer on purpose: a simple ``WARN_ON(<0)`` check can be used to detect346* reference count bugs that would make it negative.347*/348struct media_entity {349struct media_gobj graph_obj; /* must be first field in struct */350const char *name;351enum media_entity_type obj_type;352u32 function;353unsigned long flags;354355u16 num_pads;356u16 num_links;357u16 num_backlinks;358int internal_idx;359360struct media_pad *pads;361struct list_head links;362363const struct media_entity_operations *ops;364365int use_count;366367union {368struct {369u32 major;370u32 minor;371} dev;372} info;373};374375/**376* media_entity_for_each_pad - Iterate on all pads in an entity377* @entity: The entity the pads belong to378* @iter: The iterator pad379*380* Iterate on all pads in a media entity.381*/382#define media_entity_for_each_pad(entity, iter) \383for (iter = (entity)->pads; \384iter < &(entity)->pads[(entity)->num_pads]; \385++iter)386387/**388* struct media_interface - A media interface graph object.389*390* @graph_obj: embedded graph object391* @links: List of links pointing to graph entities392* @type: Type of the interface as defined in393* :ref:`include/uapi/linux/media.h <media_header>`394* (seek for ``MEDIA_INTF_T_*``)395* @flags: Interface flags as defined in396* :ref:`include/uapi/linux/media.h <media_header>`397* (seek for ``MEDIA_INTF_FL_*``)398*399* .. note::400*401* Currently, no flags for &media_interface is defined.402*/403struct media_interface {404struct media_gobj graph_obj;405struct list_head links;406u32 type;407u32 flags;408};409410/**411* struct media_intf_devnode - A media interface via a device node.412*413* @intf: embedded interface object414* @major: Major number of a device node415* @minor: Minor number of a device node416*/417struct media_intf_devnode {418struct media_interface intf;419420/* Should match the fields at media_v2_intf_devnode */421u32 major;422u32 minor;423};424425/**426* media_entity_id() - return the media entity graph object id427*428* @entity: pointer to &media_entity429*/430static inline u32 media_entity_id(struct media_entity *entity)431{432return entity->graph_obj.id;433}434435/**436* media_type() - return the media object type437*438* @gobj: Pointer to the struct &media_gobj graph object439*/440static inline enum media_gobj_type media_type(struct media_gobj *gobj)441{442return gobj->id >> MEDIA_BITS_PER_ID;443}444445/**446* media_id() - return the media object ID447*448* @gobj: Pointer to the struct &media_gobj graph object449*/450static inline u32 media_id(struct media_gobj *gobj)451{452return gobj->id & MEDIA_ID_MASK;453}454455/**456* media_gobj_gen_id() - encapsulates type and ID on at the object ID457*458* @type: object type as define at enum &media_gobj_type.459* @local_id: next ID, from struct &media_device.id.460*/461static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)462{463u32 id;464465id = type << MEDIA_BITS_PER_ID;466id |= local_id & MEDIA_ID_MASK;467468return id;469}470471/**472* is_media_entity_v4l2_video_device() - Check if the entity is a video_device473* @entity: pointer to entity474*475* Return: %true if the entity is an instance of a video_device object and can476* safely be cast to a struct video_device using the container_of() macro, or477* %false otherwise.478*/479static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity)480{481return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;482}483484/**485* is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev486* @entity: pointer to entity487*488* Return: %true if the entity is an instance of a &v4l2_subdev object and can489* safely be cast to a struct &v4l2_subdev using the container_of() macro, or490* %false otherwise.491*/492static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)493{494return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;495}496497/**498* media_entity_enum_init - Initialise an entity enumeration499*500* @ent_enum: Entity enumeration to be initialised501* @mdev: The related media device502*503* Return: zero on success or a negative error code.504*/505__must_check int media_entity_enum_init(struct media_entity_enum *ent_enum,506struct media_device *mdev);507508/**509* media_entity_enum_cleanup - Release resources of an entity enumeration510*511* @ent_enum: Entity enumeration to be released512*/513void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);514515/**516* media_entity_enum_zero - Clear the entire enum517*518* @ent_enum: Entity enumeration to be cleared519*/520static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)521{522bitmap_zero(ent_enum->bmap, ent_enum->idx_max);523}524525/**526* media_entity_enum_set - Mark a single entity in the enum527*528* @ent_enum: Entity enumeration529* @entity: Entity to be marked530*/531static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,532struct media_entity *entity)533{534if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))535return;536537__set_bit(entity->internal_idx, ent_enum->bmap);538}539540/**541* media_entity_enum_clear - Unmark a single entity in the enum542*543* @ent_enum: Entity enumeration544* @entity: Entity to be unmarked545*/546static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,547struct media_entity *entity)548{549if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))550return;551552__clear_bit(entity->internal_idx, ent_enum->bmap);553}554555/**556* media_entity_enum_test - Test whether the entity is marked557*558* @ent_enum: Entity enumeration559* @entity: Entity to be tested560*561* Returns %true if the entity was marked.562*/563static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,564struct media_entity *entity)565{566if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))567return true;568569return test_bit(entity->internal_idx, ent_enum->bmap);570}571572/**573* media_entity_enum_test_and_set - Test whether the entity is marked,574* and mark it575*576* @ent_enum: Entity enumeration577* @entity: Entity to be tested578*579* Returns %true if the entity was marked, and mark it before doing so.580*/581static inline bool582media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,583struct media_entity *entity)584{585if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))586return true;587588return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);589}590591/**592* media_entity_enum_empty - Test whether the entire enum is empty593*594* @ent_enum: Entity enumeration595*596* Return: %true if the entity was empty.597*/598static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)599{600return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);601}602603/**604* media_entity_enum_intersects - Test whether two enums intersect605*606* @ent_enum1: First entity enumeration607* @ent_enum2: Second entity enumeration608*609* Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect,610* otherwise %false.611*/612static inline bool media_entity_enum_intersects(613struct media_entity_enum *ent_enum1,614struct media_entity_enum *ent_enum2)615{616WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);617618return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,619min(ent_enum1->idx_max, ent_enum2->idx_max));620}621622/**623* gobj_to_entity - returns the struct &media_entity pointer from the624* @gobj contained on it.625*626* @gobj: Pointer to the struct &media_gobj graph object627*/628#define gobj_to_entity(gobj) \629container_of(gobj, struct media_entity, graph_obj)630631/**632* gobj_to_pad - returns the struct &media_pad pointer from the633* @gobj contained on it.634*635* @gobj: Pointer to the struct &media_gobj graph object636*/637#define gobj_to_pad(gobj) \638container_of(gobj, struct media_pad, graph_obj)639640/**641* gobj_to_link - returns the struct &media_link pointer from the642* @gobj contained on it.643*644* @gobj: Pointer to the struct &media_gobj graph object645*/646#define gobj_to_link(gobj) \647container_of(gobj, struct media_link, graph_obj)648649/**650* gobj_to_intf - returns the struct &media_interface pointer from the651* @gobj contained on it.652*653* @gobj: Pointer to the struct &media_gobj graph object654*/655#define gobj_to_intf(gobj) \656container_of(gobj, struct media_interface, graph_obj)657658/**659* intf_to_devnode - returns the struct media_intf_devnode pointer from the660* @intf contained on it.661*662* @intf: Pointer to struct &media_intf_devnode663*/664#define intf_to_devnode(intf) \665container_of(intf, struct media_intf_devnode, intf)666667/**668* media_gobj_create - Initialize a graph object669*670* @mdev: Pointer to the &media_device that contains the object671* @type: Type of the object672* @gobj: Pointer to the struct &media_gobj graph object673*674* This routine initializes the embedded struct &media_gobj inside a675* media graph object. It is called automatically if ``media_*_create``676* function calls are used. However, if the object (entity, link, pad,677* interface) is embedded on some other object, this function should be678* called before registering the object at the media controller.679*/680void media_gobj_create(struct media_device *mdev,681enum media_gobj_type type,682struct media_gobj *gobj);683684/**685* media_gobj_destroy - Stop using a graph object on a media device686*687* @gobj: Pointer to the struct &media_gobj graph object688*689* This should be called by all routines like media_device_unregister()690* that remove/destroy media graph objects.691*/692void media_gobj_destroy(struct media_gobj *gobj);693694/**695* media_entity_pads_init() - Initialize the entity pads696*697* @entity: entity where the pads belong698* @num_pads: total number of sink and source pads699* @pads: Array of @num_pads pads.700*701* The pads array is managed by the entity driver and passed to702* media_entity_pads_init() where its pointer will be stored in the703* &media_entity structure.704*705* If no pads are needed, drivers could either directly fill706* &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call707* this function that will do the same.708*709* As the number of pads is known in advance, the pads array is not allocated710* dynamically but is managed by the entity driver. Most drivers will embed the711* pads array in a driver-specific structure, avoiding dynamic allocation.712*713* Drivers must set the direction of every pad in the pads array before calling714* media_entity_pads_init(). The function will initialize the other pads fields.715*/716int media_entity_pads_init(struct media_entity *entity, u16 num_pads,717struct media_pad *pads);718719/**720* media_entity_cleanup() - free resources associated with an entity721*722* @entity: entity where the pads belong723*724* This function must be called during the cleanup phase after unregistering725* the entity (currently, it does nothing).726*727* Calling media_entity_cleanup() on a media_entity whose memory has been728* zeroed but that has not been initialized with media_entity_pad_init() is729* valid and is a no-op.730*/731#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)732static inline void media_entity_cleanup(struct media_entity *entity) {}733#else734#define media_entity_cleanup(entity) do { } while (false)735#endif736737/**738* media_get_pad_index() - retrieves a pad index from an entity739*740* @entity: entity where the pads belong741* @pad_type: the type of the pad, one of MEDIA_PAD_FL_* pad types742* @sig_type: type of signal of the pad to be search743*744* This helper function finds the first pad index inside an entity that745* satisfies both @is_sink and @sig_type conditions.746*747* Return:748*749* On success, return the pad number. If the pad was not found or the media750* entity is a NULL pointer, return -EINVAL.751*/752int media_get_pad_index(struct media_entity *entity, u32 pad_type,753enum media_pad_signal_type sig_type);754755/**756* media_create_pad_link() - creates a link between two entities.757*758* @source: pointer to &media_entity of the source pad.759* @source_pad: number of the source pad in the pads array760* @sink: pointer to &media_entity of the sink pad.761* @sink_pad: number of the sink pad in the pads array.762* @flags: Link flags, as defined in763* :ref:`include/uapi/linux/media.h <media_header>`764* ( seek for ``MEDIA_LNK_FL_*``)765*766* Valid values for flags:767*768* %MEDIA_LNK_FL_ENABLED769* Indicates that the link is enabled and can be used to transfer media data.770* When two or more links target a sink pad, only one of them can be771* enabled at a time.772*773* %MEDIA_LNK_FL_IMMUTABLE774* Indicates that the link enabled state can't be modified at runtime. If775* %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be776* set, since an immutable link is always enabled.777*778* .. note::779*780* Before calling this function, media_entity_pads_init() and781* media_device_register_entity() should be called previously for both ends.782*/783__must_check int media_create_pad_link(struct media_entity *source,784u16 source_pad, struct media_entity *sink,785u16 sink_pad, u32 flags);786787/**788* media_create_pad_links() - creates a link between two entities.789*790* @mdev: Pointer to the media_device that contains the object791* @source_function: Function of the source entities. Used only if @source is792* NULL.793* @source: pointer to &media_entity of the source pad. If NULL, it will use794* all entities that matches the @sink_function.795* @source_pad: number of the source pad in the pads array796* @sink_function: Function of the sink entities. Used only if @sink is NULL.797* @sink: pointer to &media_entity of the sink pad. If NULL, it will use798* all entities that matches the @sink_function.799* @sink_pad: number of the sink pad in the pads array.800* @flags: Link flags, as defined in include/uapi/linux/media.h.801* @allow_both_undefined: if %true, then both @source and @sink can be NULL.802* In such case, it will create a crossbar between all entities that803* matches @source_function to all entities that matches @sink_function.804* If %false, it will return 0 and won't create any link if both @source805* and @sink are NULL.806*807* Valid values for flags:808*809* A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be810* used to transfer media data. If multiple links are created and this811* flag is passed as an argument, only the first created link will have812* this flag.813*814* A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't815* be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then816* %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is817* always enabled.818*819* It is common for some devices to have multiple source and/or sink entities820* of the same type that should be linked. While media_create_pad_link()821* creates link by link, this function is meant to allow 1:n, n:1 and even822* cross-bar (n:n) links.823*824* .. note::825*826* Before calling this function, media_entity_pads_init() and827* media_device_register_entity() should be called previously for the828* entities to be linked.829*/830int media_create_pad_links(const struct media_device *mdev,831const u32 source_function,832struct media_entity *source,833const u16 source_pad,834const u32 sink_function,835struct media_entity *sink,836const u16 sink_pad,837u32 flags,838const bool allow_both_undefined);839840void __media_entity_remove_links(struct media_entity *entity);841842/**843* media_entity_remove_links() - remove all links associated with an entity844*845* @entity: pointer to &media_entity846*847* .. note::848*849* This is called automatically when an entity is unregistered via850* media_device_register_entity().851*/852void media_entity_remove_links(struct media_entity *entity);853854/**855* __media_entity_setup_link - Configure a media link without locking856* @link: The link being configured857* @flags: Link configuration flags858*859* The bulk of link setup is handled by the two entities connected through the860* link. This function notifies both entities of the link configuration change.861*862* If the link is immutable or if the current and new configuration are863* identical, return immediately.864*865* The user is expected to hold link->source->parent->mutex. If not,866* media_entity_setup_link() should be used instead.867*/868int __media_entity_setup_link(struct media_link *link, u32 flags);869870/**871* media_entity_setup_link() - changes the link flags properties in runtime872*873* @link: pointer to &media_link874* @flags: the requested new link flags875*876* The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag877* to enable/disable a link. Links marked with the878* %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.879*880* When a link is enabled or disabled, the media framework calls the881* link_setup operation for the two entities at the source and sink of the882* link, in that order. If the second link_setup call fails, another883* link_setup call is made on the first entity to restore the original link884* flags.885*886* Media device drivers can be notified of link setup operations by setting the887* &media_device.link_notify pointer to a callback function. If provided, the888* notification callback will be called before enabling and after disabling889* links.890*891* Entity drivers must implement the link_setup operation if any of their links892* is non-immutable. The operation must either configure the hardware or store893* the configuration information to be applied later.894*895* Link configuration must not have any side effect on other links. If an896* enabled link at a sink pad prevents another link at the same pad from897* being enabled, the link_setup operation must return %-EBUSY and can't898* implicitly disable the first enabled link.899*900* .. note::901*902* The valid values of the flags for the link is the same as described903* on media_create_pad_link(), for pad to pad links or the same as described904* on media_create_intf_link(), for interface to entity links.905*/906int media_entity_setup_link(struct media_link *link, u32 flags);907908/**909* media_entity_find_link - Find a link between two pads910* @source: Source pad911* @sink: Sink pad912*913* Return: returns a pointer to the link between the two entities. If no914* such link exists, return %NULL.915*/916struct media_link *media_entity_find_link(struct media_pad *source,917struct media_pad *sink);918919/**920* media_pad_remote_pad_first - Find the first pad at the remote end of a link921* @pad: Pad at the local end of the link922*923* Search for a remote pad connected to the given pad by iterating over all924* links originating or terminating at that pad until an enabled link is found.925*926* Return: returns a pointer to the pad at the remote end of the first found927* enabled link, or %NULL if no enabled link has been found.928*/929struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad);930931/**932* media_pad_remote_pad_unique - Find a remote pad connected to a pad933* @pad: The pad934*935* Search for and return a remote pad connected to @pad through an enabled936* link. If multiple (or no) remote pads are found, an error is returned.937*938* The uniqueness constraint makes this helper function suitable for entities939* that support a single active source at a time on a given pad.940*941* Return: A pointer to the remote pad, or one of the following error pointers942* if an error occurs:943*944* * -ENOTUNIQ - Multiple links are enabled945* * -ENOLINK - No connected pad found946*/947struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad);948949/**950* media_entity_remote_pad_unique - Find a remote pad connected to an entity951* @entity: The entity952* @type: The type of pad to find (MEDIA_PAD_FL_SINK or MEDIA_PAD_FL_SOURCE)953*954* Search for and return a remote pad of @type connected to @entity through an955* enabled link. If multiple (or no) remote pads match these criteria, an error956* is returned.957*958* The uniqueness constraint makes this helper function suitable for entities959* that support a single active source or sink at a time.960*961* Return: A pointer to the remote pad, or one of the following error pointers962* if an error occurs:963*964* * -ENOTUNIQ - Multiple links are enabled965* * -ENOLINK - No connected pad found966*/967struct media_pad *968media_entity_remote_pad_unique(const struct media_entity *entity,969unsigned int type);970971/**972* media_entity_remote_source_pad_unique - Find a remote source pad connected to973* an entity974* @entity: The entity975*976* Search for and return a remote source pad connected to @entity through an977* enabled link. If multiple (or no) remote pads match these criteria, an error978* is returned.979*980* The uniqueness constraint makes this helper function suitable for entities981* that support a single active source at a time.982*983* Return: A pointer to the remote pad, or one of the following error pointers984* if an error occurs:985*986* * -ENOTUNIQ - Multiple links are enabled987* * -ENOLINK - No connected pad found988*/989static inline struct media_pad *990media_entity_remote_source_pad_unique(const struct media_entity *entity)991{992return media_entity_remote_pad_unique(entity, MEDIA_PAD_FL_SOURCE);993}994995/**996* media_pad_is_streaming - Test if a pad is part of a streaming pipeline997* @pad: The pad998*999* Return: True if the pad is part of a pipeline started with the1000* media_pipeline_start() function, false otherwise.1001*/1002static inline bool media_pad_is_streaming(const struct media_pad *pad)1003{1004return pad->pipe;1005}10061007/**1008* media_entity_is_streaming - Test if an entity is part of a streaming pipeline1009* @entity: The entity1010*1011* Return: True if the entity is part of a pipeline started with the1012* media_pipeline_start() function, false otherwise.1013*/1014static inline bool media_entity_is_streaming(const struct media_entity *entity)1015{1016struct media_pad *pad;10171018media_entity_for_each_pad(entity, pad) {1019if (media_pad_is_streaming(pad))1020return true;1021}10221023return false;1024}10251026/**1027* media_entity_pipeline - Get the media pipeline an entity is part of1028* @entity: The entity1029*1030* DEPRECATED: use media_pad_pipeline() instead.1031*1032* This function returns the media pipeline that an entity has been associated1033* with when constructing the pipeline with media_pipeline_start(). The pointer1034* remains valid until media_pipeline_stop() is called.1035*1036* In general, entities can be part of multiple pipelines, when carrying1037* multiple streams (either on different pads, or on the same pad using1038* multiplexed streams). This function is to be used only for entities that1039* do not support multiple pipelines.1040*1041* Return: The media_pipeline the entity is part of, or NULL if the entity is1042* not part of any pipeline.1043*/1044struct media_pipeline *media_entity_pipeline(struct media_entity *entity);10451046/**1047* media_pad_pipeline - Get the media pipeline a pad is part of1048* @pad: The pad1049*1050* This function returns the media pipeline that a pad has been associated1051* with when constructing the pipeline with media_pipeline_start(). The pointer1052* remains valid until media_pipeline_stop() is called.1053*1054* Return: The media_pipeline the pad is part of, or NULL if the pad is1055* not part of any pipeline.1056*/1057struct media_pipeline *media_pad_pipeline(struct media_pad *pad);10581059/**1060* media_entity_get_fwnode_pad - Get pad number from fwnode1061*1062* @entity: The entity1063* @fwnode: Pointer to the fwnode_handle which should be used to find the pad1064* @direction_flags: Expected direction of the pad, as defined in1065* :ref:`include/uapi/linux/media.h <media_header>`1066* (seek for ``MEDIA_PAD_FL_*``)1067*1068* This function can be used to resolve the media pad number from1069* a fwnode. This is useful for devices which use more complex1070* mappings of media pads.1071*1072* If the entity does not implement the get_fwnode_pad() operation1073* then this function searches the entity for the first pad that1074* matches the @direction_flags.1075*1076* Return: returns the pad number on success or a negative error code.1077*/1078int media_entity_get_fwnode_pad(struct media_entity *entity,1079const struct fwnode_handle *fwnode,1080unsigned long direction_flags);10811082/**1083* media_graph_walk_init - Allocate resources used by graph walk.1084*1085* @graph: Media graph structure that will be used to walk the graph1086* @mdev: Pointer to the &media_device that contains the object1087*1088* This function is deprecated, use media_pipeline_for_each_pad() instead.1089*1090* The caller is required to hold the media_device graph_mutex during the graph1091* walk until the graph state is released.1092*1093* Returns zero on success or a negative error code otherwise.1094*/1095__must_check int media_graph_walk_init(1096struct media_graph *graph, struct media_device *mdev);10971098/**1099* media_graph_walk_cleanup - Release resources used by graph walk.1100*1101* @graph: Media graph structure that will be used to walk the graph1102*1103* This function is deprecated, use media_pipeline_for_each_pad() instead.1104*/1105void media_graph_walk_cleanup(struct media_graph *graph);11061107/**1108* media_graph_walk_start - Start walking the media graph at a1109* given entity1110*1111* @graph: Media graph structure that will be used to walk the graph1112* @entity: Starting entity1113*1114* This function is deprecated, use media_pipeline_for_each_pad() instead.1115*1116* Before using this function, media_graph_walk_init() must be1117* used to allocate resources used for walking the graph. This1118* function initializes the graph traversal structure to walk the1119* entities graph starting at the given entity. The traversal1120* structure must not be modified by the caller during graph1121* traversal. After the graph walk, the resources must be released1122* using media_graph_walk_cleanup().1123*/1124void media_graph_walk_start(struct media_graph *graph,1125struct media_entity *entity);11261127/**1128* media_graph_walk_next - Get the next entity in the graph1129* @graph: Media graph structure1130*1131* This function is deprecated, use media_pipeline_for_each_pad() instead.1132*1133* Perform a depth-first traversal of the given media entities graph.1134*1135* The graph structure must have been previously initialized with a call to1136* media_graph_walk_start().1137*1138* Return: returns the next entity in the graph or %NULL if the whole graph1139* have been traversed.1140*/1141struct media_entity *media_graph_walk_next(struct media_graph *graph);11421143/**1144* media_pipeline_start - Mark a pipeline as streaming1145* @origin: Starting pad1146* @pipe: Media pipeline to be assigned to all pads in the pipeline.1147*1148* Mark all pads connected to pad @origin through enabled links, either1149* directly or indirectly, as streaming. The given pipeline object is assigned1150* to every pad in the pipeline and stored in the media_pad pipe field.1151*1152* Calls to this function can be nested, in which case the same number of1153* media_pipeline_stop() calls will be required to stop streaming. The1154* pipeline pointer must be identical for all nested calls to1155* media_pipeline_start().1156*/1157__must_check int media_pipeline_start(struct media_pad *origin,1158struct media_pipeline *pipe);1159/**1160* __media_pipeline_start - Mark a pipeline as streaming1161*1162* @origin: Starting pad1163* @pipe: Media pipeline to be assigned to all pads in the pipeline.1164*1165* ..note:: This is the non-locking version of media_pipeline_start()1166*/1167__must_check int __media_pipeline_start(struct media_pad *origin,1168struct media_pipeline *pipe);11691170/**1171* media_pipeline_stop - Mark a pipeline as not streaming1172* @pad: Starting pad1173*1174* Mark all pads connected to a given pad through enabled links, either1175* directly or indirectly, as not streaming. The media_pad pipe field is1176* reset to %NULL.1177*1178* If multiple calls to media_pipeline_start() have been made, the same1179* number of calls to this function are required to mark the pipeline as not1180* streaming.1181*/1182void media_pipeline_stop(struct media_pad *pad);11831184/**1185* __media_pipeline_stop - Mark a pipeline as not streaming1186*1187* @pad: Starting pad1188*1189* .. note:: This is the non-locking version of media_pipeline_stop()1190*/1191void __media_pipeline_stop(struct media_pad *pad);11921193struct media_pad *1194__media_pipeline_pad_iter_next(struct media_pipeline *pipe,1195struct media_pipeline_pad_iter *iter,1196struct media_pad *pad);11971198/**1199* media_pipeline_for_each_pad - Iterate on all pads in a media pipeline1200* @pipe: The pipeline1201* @iter: The iterator (struct media_pipeline_pad_iter)1202* @pad: The iterator pad1203*1204* Iterate on all pads in a media pipeline. This is only valid after the1205* pipeline has been built with media_pipeline_start() and before it gets1206* destroyed with media_pipeline_stop().1207*/1208#define media_pipeline_for_each_pad(pipe, iter, pad) \1209for (pad = __media_pipeline_pad_iter_next((pipe), iter, NULL); \1210pad != NULL; \1211pad = __media_pipeline_pad_iter_next((pipe), iter, pad))12121213/**1214* media_pipeline_entity_iter_init - Initialize a pipeline entity iterator1215* @pipe: The pipeline1216* @iter: The iterator1217*1218* This function must be called to initialize the iterator before using it in a1219* media_pipeline_for_each_entity() loop. The iterator must be destroyed by a1220* call to media_pipeline_entity_iter_cleanup after the loop (including in code1221* paths that break from the loop).1222*1223* The same iterator can be used in multiple consecutive loops without being1224* destroyed and reinitialized.1225*1226* Return: 0 on success or a negative error code otherwise.1227*/1228int media_pipeline_entity_iter_init(struct media_pipeline *pipe,1229struct media_pipeline_entity_iter *iter);12301231/**1232* media_pipeline_entity_iter_cleanup - Destroy a pipeline entity iterator1233* @iter: The iterator1234*1235* This function must be called to destroy iterators initialized with1236* media_pipeline_entity_iter_init().1237*/1238void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter);12391240struct media_entity *1241__media_pipeline_entity_iter_next(struct media_pipeline *pipe,1242struct media_pipeline_entity_iter *iter,1243struct media_entity *entity);12441245/**1246* media_pipeline_for_each_entity - Iterate on all entities in a media pipeline1247* @pipe: The pipeline1248* @iter: The iterator (struct media_pipeline_entity_iter)1249* @entity: The iterator entity1250*1251* Iterate on all entities in a media pipeline. This is only valid after the1252* pipeline has been built with media_pipeline_start() and before it gets1253* destroyed with media_pipeline_stop(). The iterator must be initialized with1254* media_pipeline_entity_iter_init() before iteration, and destroyed with1255* media_pipeline_entity_iter_cleanup() after (including in code paths that1256* break from the loop).1257*/1258#define media_pipeline_for_each_entity(pipe, iter, entity) \1259for (entity = __media_pipeline_entity_iter_next((pipe), iter, NULL); \1260entity != NULL; \1261entity = __media_pipeline_entity_iter_next((pipe), iter, entity))12621263/**1264* media_pipeline_alloc_start - Mark a pipeline as streaming1265* @pad: Starting pad1266*1267* media_pipeline_alloc_start() is similar to media_pipeline_start() but instead1268* of working on a given pipeline the function will use an existing pipeline if1269* the pad is already part of a pipeline, or allocate a new pipeline.1270*1271* Calls to media_pipeline_alloc_start() must be matched with1272* media_pipeline_stop().1273*/1274__must_check int media_pipeline_alloc_start(struct media_pad *pad);12751276/**1277* media_devnode_create() - creates and initializes a device node interface1278*1279* @mdev: pointer to struct &media_device1280* @type: type of the interface, as given by1281* :ref:`include/uapi/linux/media.h <media_header>`1282* ( seek for ``MEDIA_INTF_T_*``) macros.1283* @flags: Interface flags, as defined in1284* :ref:`include/uapi/linux/media.h <media_header>`1285* ( seek for ``MEDIA_INTF_FL_*``)1286* @major: Device node major number.1287* @minor: Device node minor number.1288*1289* Return: if succeeded, returns a pointer to the newly allocated1290* &media_intf_devnode pointer.1291*1292* .. note::1293*1294* Currently, no flags for &media_interface is defined.1295*/1296struct media_intf_devnode *1297__must_check media_devnode_create(struct media_device *mdev,1298u32 type, u32 flags,1299u32 major, u32 minor);1300/**1301* media_devnode_remove() - removes a device node interface1302*1303* @devnode: pointer to &media_intf_devnode to be freed.1304*1305* When a device node interface is removed, all links to it are automatically1306* removed.1307*/1308void media_devnode_remove(struct media_intf_devnode *devnode);13091310/**1311* media_create_intf_link() - creates a link between an entity and an interface1312*1313* @entity: pointer to %media_entity1314* @intf: pointer to %media_interface1315* @flags: Link flags, as defined in1316* :ref:`include/uapi/linux/media.h <media_header>`1317* ( seek for ``MEDIA_LNK_FL_*``)1318*1319*1320* Valid values for flags:1321*1322* %MEDIA_LNK_FL_ENABLED1323* Indicates that the interface is connected to the entity hardware.1324* That's the default value for interfaces. An interface may be disabled if1325* the hardware is busy due to the usage of some other interface that it is1326* currently controlling the hardware.1327*1328* A typical example is an hybrid TV device that handle only one type of1329* stream on a given time. So, when the digital TV is streaming,1330* the V4L2 interfaces won't be enabled, as such device is not able to1331* also stream analog TV or radio.1332*1333* .. note::1334*1335* Before calling this function, media_devnode_create() should be called for1336* the interface and media_device_register_entity() should be called for the1337* interface that will be part of the link.1338*/1339struct media_link *1340__must_check media_create_intf_link(struct media_entity *entity,1341struct media_interface *intf,1342u32 flags);1343/**1344* __media_remove_intf_link() - remove a single interface link1345*1346* @link: pointer to &media_link.1347*1348* .. note:: This is an unlocked version of media_remove_intf_link()1349*/1350void __media_remove_intf_link(struct media_link *link);13511352/**1353* media_remove_intf_link() - remove a single interface link1354*1355* @link: pointer to &media_link.1356*1357* .. note:: Prefer to use this one, instead of __media_remove_intf_link()1358*/1359void media_remove_intf_link(struct media_link *link);13601361/**1362* __media_remove_intf_links() - remove all links associated with an interface1363*1364* @intf: pointer to &media_interface1365*1366* .. note:: This is an unlocked version of media_remove_intf_links().1367*/1368void __media_remove_intf_links(struct media_interface *intf);13691370/**1371* media_remove_intf_links() - remove all links associated with an interface1372*1373* @intf: pointer to &media_interface1374*1375* .. note::1376*1377* #) This is called automatically when an entity is unregistered via1378* media_device_register_entity() and by media_devnode_remove().1379*1380* #) Prefer to use this one, instead of __media_remove_intf_links().1381*/1382void media_remove_intf_links(struct media_interface *intf);13831384/**1385* media_entity_call - Calls a struct media_entity_operations operation on1386* an entity1387*1388* @entity: entity where the @operation will be called1389* @operation: type of the operation. Should be the name of a member of1390* struct &media_entity_operations.1391*1392* This helper function will check if @operation is not %NULL. On such case,1393* it will issue a call to @operation\(@entity, @args\).1394*/13951396#define media_entity_call(entity, operation, args...) \1397(((entity)->ops && (entity)->ops->operation) ? \1398(entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)13991400/**1401* media_create_ancillary_link() - create an ancillary link between two1402* instances of &media_entity1403*1404* @primary: pointer to the primary &media_entity1405* @ancillary: pointer to the ancillary &media_entity1406*1407* Create an ancillary link between two entities, indicating that they1408* represent two connected pieces of hardware that form a single logical unit.1409* A typical example is a camera lens controller being linked to the sensor that1410* it is supporting.1411*1412* The function sets both MEDIA_LNK_FL_ENABLED and MEDIA_LNK_FL_IMMUTABLE for1413* the new link.1414*/1415struct media_link *1416media_create_ancillary_link(struct media_entity *primary,1417struct media_entity *ancillary);14181419/**1420* __media_entity_next_link() - Iterate through a &media_entity's links1421*1422* @entity: pointer to the &media_entity1423* @link: pointer to a &media_link to hold the iterated values1424* @link_type: one of the MEDIA_LNK_FL_LINK_TYPE flags1425*1426* Return the next link against an entity matching a specific link type. This1427* allows iteration through an entity's links whilst guaranteeing all of the1428* returned links are of the given type.1429*/1430struct media_link *__media_entity_next_link(struct media_entity *entity,1431struct media_link *link,1432unsigned long link_type);14331434/**1435* for_each_media_entity_data_link() - Iterate through an entity's data links1436*1437* @entity: pointer to the &media_entity1438* @link: pointer to a &media_link to hold the iterated values1439*1440* Iterate over a &media_entity's data links1441*/1442#define for_each_media_entity_data_link(entity, link) \1443for (link = __media_entity_next_link(entity, NULL, \1444MEDIA_LNK_FL_DATA_LINK); \1445link; \1446link = __media_entity_next_link(entity, link, \1447MEDIA_LNK_FL_DATA_LINK))14481449#endif145014511452