xref: /linux/include/media/media-entity.h (revision 0e01357e94435649263cdfdb62f1cc7f6669ec21)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Media entity
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  *
7  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8  *	     Sakari Ailus <sakari.ailus@iki.fi>
9  */
10 
11 #ifndef _MEDIA_ENTITY_H
12 #define _MEDIA_ENTITY_H
13 
14 #include <linux/bitmap.h>
15 #include <linux/bug.h>
16 #include <linux/container_of.h>
17 #include <linux/fwnode.h>
18 #include <linux/list.h>
19 #include <linux/media.h>
20 #include <linux/minmax.h>
21 #include <linux/types.h>
22 
23 /* Enums used internally at the media controller to represent graphs */
24 
25 /**
26  * enum media_gobj_type - type of a graph object
27  *
28  * @MEDIA_GRAPH_ENTITY:		Identify a media entity
29  * @MEDIA_GRAPH_PAD:		Identify a media pad
30  * @MEDIA_GRAPH_LINK:		Identify a media link
31  * @MEDIA_GRAPH_INTF_DEVNODE:	Identify a media Kernel API interface via
32  *				a device node
33  */
34 enum media_gobj_type {
35 	MEDIA_GRAPH_ENTITY,
36 	MEDIA_GRAPH_PAD,
37 	MEDIA_GRAPH_LINK,
38 	MEDIA_GRAPH_INTF_DEVNODE,
39 };
40 
41 #define MEDIA_BITS_PER_TYPE		8
42 #define MEDIA_BITS_PER_ID		(32 - MEDIA_BITS_PER_TYPE)
43 #define MEDIA_ID_MASK			 GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)
44 
45 /* Structs to represent the objects that belong to a media graph */
46 
47 /**
48  * struct media_gobj - Define a graph object.
49  *
50  * @mdev:	Pointer to the struct &media_device that owns the object
51  * @id:		Non-zero object ID identifier. The ID should be unique
52  *		inside a media_device, as it is composed by
53  *		%MEDIA_BITS_PER_TYPE to store the type plus
54  *		%MEDIA_BITS_PER_ID to store the ID
55  * @list:	List entry stored in one of the per-type mdev object lists
56  *
57  * All objects on the media graph should have this struct embedded
58  */
59 struct media_gobj {
60 	struct media_device	*mdev;
61 	u32			id;
62 	struct list_head	list;
63 };
64 
65 #define MEDIA_ENTITY_ENUM_MAX_DEPTH	16
66 
67 /**
68  * struct media_entity_enum - An enumeration of media entities.
69  *
70  * @bmap:	Bit map in which each bit represents one entity at struct
71  *		media_entity->internal_idx.
72  * @idx_max:	Number of bits in bmap
73  */
74 struct media_entity_enum {
75 	unsigned long *bmap;
76 	int idx_max;
77 };
78 
79 /**
80  * struct media_graph - Media graph traversal state
81  *
82  * @stack:		Graph traversal stack; the stack contains information
83  *			on the path the media entities to be walked and the
84  *			links through which they were reached.
85  * @stack.entity:	pointer to &struct media_entity at the graph.
86  * @stack.link:		pointer to &struct list_head.
87  * @ent_enum:		Visited entities
88  * @top:		The top of the stack
89  */
90 struct media_graph {
91 	struct {
92 		struct media_entity *entity;
93 		struct list_head *link;
94 	} stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
95 
96 	struct media_entity_enum ent_enum;
97 	int top;
98 };
99 
100 /**
101  * struct media_pipeline - Media pipeline related information
102  *
103  * @allocated:		Media pipeline allocated and freed by the framework
104  * @mdev:		The media device the pipeline is part of
105  * @pads:		List of media_pipeline_pad
106  * @start_count:	Media pipeline start - stop count
107  */
108 struct media_pipeline {
109 	bool allocated;
110 	struct media_device *mdev;
111 	struct list_head pads;
112 	int start_count;
113 };
114 
115 /**
116  * struct media_pipeline_pad - A pad part of a media pipeline
117  *
118  * @list:		Entry in the media_pad pads list
119  * @pipe:		The media_pipeline that the pad is part of
120  * @pad:		The media pad
121  *
122  * This structure associate a pad with a media pipeline. Instances of
123  * media_pipeline_pad are created by media_pipeline_start() when it builds the
124  * pipeline, and stored in the &media_pad.pads list. media_pipeline_stop()
125  * removes the entries from the list and deletes them.
126  */
127 struct media_pipeline_pad {
128 	struct list_head list;
129 	struct media_pipeline *pipe;
130 	struct media_pad *pad;
131 };
132 
133 /**
134  * struct media_pipeline_pad_iter - Iterator for media_pipeline_for_each_pad
135  *
136  * @cursor: The current element
137  */
138 struct media_pipeline_pad_iter {
139 	struct list_head *cursor;
140 };
141 
142 /**
143  * struct media_pipeline_entity_iter - Iterator for media_pipeline_for_each_entity
144  *
145  * @ent_enum: The entity enumeration tracker
146  * @cursor: The current element
147  */
148 struct media_pipeline_entity_iter {
149 	struct media_entity_enum ent_enum;
150 	struct list_head *cursor;
151 };
152 
153 /**
154  * struct media_link - A link object part of a media graph.
155  *
156  * @graph_obj:	Embedded structure containing the media object common data
157  * @list:	Linked list associated with an entity or an interface that
158  *		owns the link.
159  * @gobj0:	Part of a union. Used to get the pointer for the first
160  *		graph_object of the link.
161  * @source:	Part of a union. Used only if the first object (gobj0) is
162  *		a pad. In that case, it represents the source pad.
163  * @intf:	Part of a union. Used only if the first object (gobj0) is
164  *		an interface.
165  * @gobj1:	Part of a union. Used to get the pointer for the second
166  *		graph_object of the link.
167  * @sink:	Part of a union. Used only if the second object (gobj1) is
168  *		a pad. In that case, it represents the sink pad.
169  * @entity:	Part of a union. Used only if the second object (gobj1) is
170  *		an entity.
171  * @reverse:	Pointer to the link for the reverse direction of a pad to pad
172  *		link.
173  * @flags:	Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
174  * @is_backlink: Indicate if the link is a backlink.
175  */
176 struct media_link {
177 	struct media_gobj graph_obj;
178 	struct list_head list;
179 	union {
180 		struct media_gobj *gobj0;
181 		struct media_pad *source;
182 		struct media_interface *intf;
183 	};
184 	union {
185 		struct media_gobj *gobj1;
186 		struct media_pad *sink;
187 		struct media_entity *entity;
188 	};
189 	struct media_link *reverse;
190 	unsigned long flags;
191 	bool is_backlink;
192 };
193 
194 /**
195  * enum media_pad_signal_type - type of the signal inside a media pad
196  *
197  * @PAD_SIGNAL_DEFAULT:
198  *	Default signal. Use this when all inputs or all outputs are
199  *	uniquely identified by the pad number.
200  * @PAD_SIGNAL_ANALOG:
201  *	The pad contains an analog signal. It can be Radio Frequency,
202  *	Intermediate Frequency, a baseband signal or sub-carriers.
203  *	Tuner inputs, IF-PLL demodulators, composite and s-video signals
204  *	should use it.
205  * @PAD_SIGNAL_DV:
206  *	Contains a digital video signal, with can be a bitstream of samples
207  *	taken from an analog TV video source. On such case, it usually
208  *	contains the VBI data on it.
209  * @PAD_SIGNAL_AUDIO:
210  *	Contains an Intermediate Frequency analog signal from an audio
211  *	sub-carrier or an audio bitstream. IF signals are provided by tuners
212  *	and consumed by	audio AM/FM decoders. Bitstream audio is provided by
213  *	an audio decoder.
214  */
215 enum media_pad_signal_type {
216 	PAD_SIGNAL_DEFAULT = 0,
217 	PAD_SIGNAL_ANALOG,
218 	PAD_SIGNAL_DV,
219 	PAD_SIGNAL_AUDIO,
220 };
221 
222 /**
223  * struct media_pad - A media pad graph object.
224  *
225  * @graph_obj:	Embedded structure containing the media object common data
226  * @entity:	Entity this pad belongs to
227  * @index:	Pad index in the entity pads array, numbered from 0 to n
228  * @sig_type:	Type of the signal inside a media pad
229  * @flags:	Pad flags, as defined in
230  *		: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() to
233  *		access this field.
234  */
235 struct media_pad {
236 	struct media_gobj graph_obj;	/* must be first field in struct */
237 	struct media_entity *entity;
238 	u16 index;
239 	enum media_pad_signal_type sig_type;
240 	unsigned long flags;
241 
242 	/*
243 	 * The fields below are private, and should only be accessed via
244 	 * appropriate functions.
245 	 */
246 	struct media_pipeline *pipe;
247 };
248 
249 /**
250  * struct media_entity_operations - Media entity operations
251  * @get_fwnode_pad:	Return the pad number based on a fwnode endpoint or
252  *			a negative value on error. This operation can be used
253  *			to map a fwnode to a media pad number. Optional.
254  * @link_setup:		Notify the entity of link changes. The operation can
255  *			return an error, in which case link setup will be
256  *			cancelled. Optional.
257  * @link_validate:	Return whether a link is valid from the entity point of
258  *			view. The media_pipeline_start() function
259  *			validates all links by calling this operation. Optional.
260  * @has_pad_interdep:	Return whether two pads of the entity are
261  *			interdependent. If two pads are interdependent they are
262  *			part of the same pipeline and enabling one of the pads
263  *			means that the other pad will become "locked" and
264  *			doesn't allow configuration changes. pad0 and pad1 are
265  *			guaranteed to not both be sinks or sources. Never call
266  *			the .has_pad_interdep() operation directly, always use
267  *			media_entity_has_pad_interdep().
268  *			Optional: If the operation isn't implemented all pads
269  *			will be considered as interdependent.
270  *
271  * .. note::
272  *
273  *    Those these callbacks are called with struct &media_device.graph_mutex
274  *    mutex held.
275  */
276 struct media_entity_operations {
277 	int (*get_fwnode_pad)(struct media_entity *entity,
278 			      struct fwnode_endpoint *endpoint);
279 	int (*link_setup)(struct media_entity *entity,
280 			  const struct media_pad *local,
281 			  const struct media_pad *remote, u32 flags);
282 	int (*link_validate)(struct media_link *link);
283 	bool (*has_pad_interdep)(struct media_entity *entity, unsigned int pad0,
284 				 unsigned int pad1);
285 };
286 
287 /**
288  * enum media_entity_type - Media entity type
289  *
290  * @MEDIA_ENTITY_TYPE_BASE:
291  *	The entity isn't embedded in another subsystem structure.
292  * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:
293  *	The entity is embedded in a struct video_device instance.
294  * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:
295  *	The entity is embedded in a struct v4l2_subdev instance.
296  *
297  * Media entity objects are often not instantiated directly, but the media
298  * entity structure is inherited by (through embedding) other subsystem-specific
299  * structures. The media entity type identifies the type of the subclass
300  * structure that implements a media entity instance.
301  *
302  * This allows runtime type identification of media entities and safe casting to
303  * the correct object type. For instance, a media entity structure instance
304  * embedded in a v4l2_subdev structure instance will have the type
305  * %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev
306  * structure using the container_of() macro.
307  */
308 enum media_entity_type {
309 	MEDIA_ENTITY_TYPE_BASE,
310 	MEDIA_ENTITY_TYPE_VIDEO_DEVICE,
311 	MEDIA_ENTITY_TYPE_V4L2_SUBDEV,
312 };
313 
314 /**
315  * struct media_entity - A media entity graph object.
316  *
317  * @graph_obj:	Embedded structure containing the media object common data.
318  * @name:	Entity name.
319  * @obj_type:	Type of the object that implements the media_entity.
320  * @function:	Entity main function, as defined in
321  *		:ref:`include/uapi/linux/media.h <media_header>`
322  *		(seek for ``MEDIA_ENT_F_*``)
323  * @flags:	Entity flags, as defined in
324  *		:ref:`include/uapi/linux/media.h <media_header>`
325  *		(seek for ``MEDIA_ENT_FL_*``)
326  * @num_pads:	Number of sink and source pads.
327  * @num_links:	Total number of links, forward and back, enabled and disabled.
328  * @num_backlinks: Number of backlinks
329  * @internal_idx: An unique internal entity specific number. The numbers are
330  *		re-used if entities are unregistered or registered again.
331  * @pads:	Pads array with the size defined by @num_pads.
332  * @links:	List of data links.
333  * @ops:	Entity operations.
334  * @use_count:	Use count for the entity.
335  * @info:	Union with devnode information.  Kept just for backward
336  *		compatibility.
337  * @info.dev:	Contains device major and minor info.
338  * @info.dev.major: device node major, if the device is a devnode.
339  * @info.dev.minor: device node minor, if the device is a devnode.
340  *
341  * .. note::
342  *
343  *    The @use_count reference count must never be negative, but is a signed
344  *    integer on purpose: a simple ``WARN_ON(<0)`` check can be used to detect
345  *    reference count bugs that would make it negative.
346  */
347 struct media_entity {
348 	struct media_gobj graph_obj;	/* must be first field in struct */
349 	const char *name;
350 	enum media_entity_type obj_type;
351 	u32 function;
352 	unsigned long flags;
353 
354 	u16 num_pads;
355 	u16 num_links;
356 	u16 num_backlinks;
357 	int internal_idx;
358 
359 	struct media_pad *pads;
360 	struct list_head links;
361 
362 	const struct media_entity_operations *ops;
363 
364 	int use_count;
365 
366 	union {
367 		struct {
368 			u32 major;
369 			u32 minor;
370 		} dev;
371 	} info;
372 };
373 
374 /**
375  * media_entity_for_each_pad - Iterate on all pads in an entity
376  * @entity: The entity the pads belong to
377  * @iter: The iterator pad
378  *
379  * Iterate on all pads in a media entity.
380  */
381 #define media_entity_for_each_pad(entity, iter)			\
382 	for (iter = (entity)->pads;				\
383 	     iter < &(entity)->pads[(entity)->num_pads];	\
384 	     ++iter)
385 
386 /**
387  * struct media_interface - A media interface graph object.
388  *
389  * @graph_obj:		embedded graph object
390  * @links:		List of links pointing to graph entities
391  * @type:		Type of the interface as defined in
392  *			:ref:`include/uapi/linux/media.h <media_header>`
393  *			(seek for ``MEDIA_INTF_T_*``)
394  * @flags:		Interface flags as defined in
395  *			:ref:`include/uapi/linux/media.h <media_header>`
396  *			(seek for ``MEDIA_INTF_FL_*``)
397  *
398  * .. note::
399  *
400  *    Currently, no flags for &media_interface is defined.
401  */
402 struct media_interface {
403 	struct media_gobj		graph_obj;
404 	struct list_head		links;
405 	u32				type;
406 	u32				flags;
407 };
408 
409 /**
410  * struct media_intf_devnode - A media interface via a device node.
411  *
412  * @intf:	embedded interface object
413  * @major:	Major number of a device node
414  * @minor:	Minor number of a device node
415  */
416 struct media_intf_devnode {
417 	struct media_interface		intf;
418 
419 	/* Should match the fields at media_v2_intf_devnode */
420 	u32				major;
421 	u32				minor;
422 };
423 
424 /**
425  * media_entity_id() - return the media entity graph object id
426  *
427  * @entity:	pointer to &media_entity
428  */
429 static inline u32 media_entity_id(struct media_entity *entity)
430 {
431 	return entity->graph_obj.id;
432 }
433 
434 /**
435  * media_type() - return the media object type
436  *
437  * @gobj:	Pointer to the struct &media_gobj graph object
438  */
439 static inline enum media_gobj_type media_type(struct media_gobj *gobj)
440 {
441 	return gobj->id >> MEDIA_BITS_PER_ID;
442 }
443 
444 /**
445  * media_id() - return the media object ID
446  *
447  * @gobj:	Pointer to the struct &media_gobj graph object
448  */
449 static inline u32 media_id(struct media_gobj *gobj)
450 {
451 	return gobj->id & MEDIA_ID_MASK;
452 }
453 
454 /**
455  * media_gobj_gen_id() - encapsulates type and ID on at the object ID
456  *
457  * @type:	object type as define at enum &media_gobj_type.
458  * @local_id:	next ID, from struct &media_device.id.
459  */
460 static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
461 {
462 	u32 id;
463 
464 	id = type << MEDIA_BITS_PER_ID;
465 	id |= local_id & MEDIA_ID_MASK;
466 
467 	return id;
468 }
469 
470 /**
471  * is_media_entity_v4l2_video_device() - Check if the entity is a video_device
472  * @entity:	pointer to entity
473  *
474  * Return: %true if the entity is an instance of a video_device object and can
475  * safely be cast to a struct video_device using the container_of() macro, or
476  * %false otherwise.
477  */
478 static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity)
479 {
480 	return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
481 }
482 
483 /**
484  * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev
485  * @entity:	pointer to entity
486  *
487  * Return: %true if the entity is an instance of a &v4l2_subdev object and can
488  * safely be cast to a struct &v4l2_subdev using the container_of() macro, or
489  * %false otherwise.
490  */
491 static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
492 {
493 	return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
494 }
495 
496 /**
497  * media_entity_enum_init - Initialise an entity enumeration
498  *
499  * @ent_enum: Entity enumeration to be initialised
500  * @mdev: The related media device
501  *
502  * Return: zero on success or a negative error code.
503  */
504 __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum,
505 					struct media_device *mdev);
506 
507 /**
508  * media_entity_enum_cleanup - Release resources of an entity enumeration
509  *
510  * @ent_enum: Entity enumeration to be released
511  */
512 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
513 
514 /**
515  * media_entity_enum_zero - Clear the entire enum
516  *
517  * @ent_enum: Entity enumeration to be cleared
518  */
519 static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
520 {
521 	bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
522 }
523 
524 /**
525  * media_entity_enum_set - Mark a single entity in the enum
526  *
527  * @ent_enum: Entity enumeration
528  * @entity: Entity to be marked
529  */
530 static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
531 					 struct media_entity *entity)
532 {
533 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
534 		return;
535 
536 	__set_bit(entity->internal_idx, ent_enum->bmap);
537 }
538 
539 /**
540  * media_entity_enum_clear - Unmark a single entity in the enum
541  *
542  * @ent_enum: Entity enumeration
543  * @entity: Entity to be unmarked
544  */
545 static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
546 					   struct media_entity *entity)
547 {
548 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
549 		return;
550 
551 	__clear_bit(entity->internal_idx, ent_enum->bmap);
552 }
553 
554 /**
555  * media_entity_enum_test - Test whether the entity is marked
556  *
557  * @ent_enum: Entity enumeration
558  * @entity: Entity to be tested
559  *
560  * Returns %true if the entity was marked.
561  */
562 static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
563 					  struct media_entity *entity)
564 {
565 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
566 		return true;
567 
568 	return test_bit(entity->internal_idx, ent_enum->bmap);
569 }
570 
571 /**
572  * media_entity_enum_test_and_set - Test whether the entity is marked,
573  *	and mark it
574  *
575  * @ent_enum: Entity enumeration
576  * @entity: Entity to be tested
577  *
578  * Returns %true if the entity was marked, and mark it before doing so.
579  */
580 static inline bool
581 media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,
582 			       struct media_entity *entity)
583 {
584 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
585 		return true;
586 
587 	return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
588 }
589 
590 /**
591  * media_entity_enum_empty - Test whether the entire enum is empty
592  *
593  * @ent_enum: Entity enumeration
594  *
595  * Return: %true if the entity was empty.
596  */
597 static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
598 {
599 	return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
600 }
601 
602 /**
603  * media_entity_enum_intersects - Test whether two enums intersect
604  *
605  * @ent_enum1: First entity enumeration
606  * @ent_enum2: Second entity enumeration
607  *
608  * Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect,
609  * otherwise %false.
610  */
611 static inline bool media_entity_enum_intersects(
612 	struct media_entity_enum *ent_enum1,
613 	struct media_entity_enum *ent_enum2)
614 {
615 	WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
616 
617 	return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
618 				 min(ent_enum1->idx_max, ent_enum2->idx_max));
619 }
620 
621 /**
622  * gobj_to_entity - returns the struct &media_entity pointer from the
623  *	@gobj contained on it.
624  *
625  * @gobj: Pointer to the struct &media_gobj graph object
626  */
627 #define gobj_to_entity(gobj) \
628 		container_of(gobj, struct media_entity, graph_obj)
629 
630 /**
631  * gobj_to_pad - returns the struct &media_pad pointer from the
632  *	@gobj contained on it.
633  *
634  * @gobj: Pointer to the struct &media_gobj graph object
635  */
636 #define gobj_to_pad(gobj) \
637 		container_of(gobj, struct media_pad, graph_obj)
638 
639 /**
640  * gobj_to_link - returns the struct &media_link pointer from the
641  *	@gobj contained on it.
642  *
643  * @gobj: Pointer to the struct &media_gobj graph object
644  */
645 #define gobj_to_link(gobj) \
646 		container_of(gobj, struct media_link, graph_obj)
647 
648 /**
649  * gobj_to_intf - returns the struct &media_interface pointer from the
650  *	@gobj contained on it.
651  *
652  * @gobj: Pointer to the struct &media_gobj graph object
653  */
654 #define gobj_to_intf(gobj) \
655 		container_of(gobj, struct media_interface, graph_obj)
656 
657 /**
658  * intf_to_devnode - returns the struct media_intf_devnode pointer from the
659  *	@intf contained on it.
660  *
661  * @intf: Pointer to struct &media_intf_devnode
662  */
663 #define intf_to_devnode(intf) \
664 		container_of(intf, struct media_intf_devnode, intf)
665 
666 /**
667  *  media_gobj_create - Initialize a graph object
668  *
669  * @mdev:	Pointer to the &media_device that contains the object
670  * @type:	Type of the object
671  * @gobj:	Pointer to the struct &media_gobj graph object
672  *
673  * This routine initializes the embedded struct &media_gobj inside a
674  * media graph object. It is called automatically if ``media_*_create``
675  * function calls are used. However, if the object (entity, link, pad,
676  * interface) is embedded on some other object, this function should be
677  * called before registering the object at the media controller.
678  */
679 void media_gobj_create(struct media_device *mdev,
680 		    enum media_gobj_type type,
681 		    struct media_gobj *gobj);
682 
683 /**
684  *  media_gobj_destroy - Stop using a graph object on a media device
685  *
686  * @gobj:	Pointer to the struct &media_gobj graph object
687  *
688  * This should be called by all routines like media_device_unregister()
689  * that remove/destroy media graph objects.
690  */
691 void media_gobj_destroy(struct media_gobj *gobj);
692 
693 /**
694  * media_entity_pads_init() - Initialize the entity pads
695  *
696  * @entity:	entity where the pads belong
697  * @num_pads:	total number of sink and source pads
698  * @pads:	Array of @num_pads pads.
699  *
700  * The pads array is managed by the entity driver and passed to
701  * media_entity_pads_init() where its pointer will be stored in the
702  * &media_entity structure.
703  *
704  * If no pads are needed, drivers could either directly fill
705  * &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call
706  * this function that will do the same.
707  *
708  * As the number of pads is known in advance, the pads array is not allocated
709  * dynamically but is managed by the entity driver. Most drivers will embed the
710  * pads array in a driver-specific structure, avoiding dynamic allocation.
711  *
712  * Drivers must set the direction of every pad in the pads array before calling
713  * media_entity_pads_init(). The function will initialize the other pads fields.
714  */
715 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
716 		      struct media_pad *pads);
717 
718 /**
719  * media_entity_cleanup() - free resources associated with an entity
720  *
721  * @entity:	entity where the pads belong
722  *
723  * This function must be called during the cleanup phase after unregistering
724  * the entity (currently, it does nothing).
725  *
726  * Calling media_entity_cleanup() on a media_entity whose memory has been
727  * zeroed but that has not been initialized with media_entity_pad_init() is
728  * valid and is a no-op.
729  */
730 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
731 static inline void media_entity_cleanup(struct media_entity *entity) {}
732 #else
733 #define media_entity_cleanup(entity) do { } while (false)
734 #endif
735 
736 /**
737  * media_get_pad_index() - retrieves a pad index from an entity
738  *
739  * @entity:	entity where the pads belong
740  * @pad_type:	the type of the pad, one of MEDIA_PAD_FL_* pad types
741  * @sig_type:	type of signal of the pad to be search
742  *
743  * This helper function finds the first pad index inside an entity that
744  * satisfies both @is_sink and @sig_type conditions.
745  *
746  * Return:
747  *
748  * On success, return the pad number. If the pad was not found or the media
749  * entity is a NULL pointer, return -EINVAL.
750  */
751 int media_get_pad_index(struct media_entity *entity, u32 pad_type,
752 			enum media_pad_signal_type sig_type);
753 
754 /**
755  * media_create_pad_link() - creates a link between two entities.
756  *
757  * @source:	pointer to &media_entity of the source pad.
758  * @source_pad:	number of the source pad in the pads array
759  * @sink:	pointer to &media_entity of the sink pad.
760  * @sink_pad:	number of the sink pad in the pads array.
761  * @flags:	Link flags, as defined in
762  *		:ref:`include/uapi/linux/media.h <media_header>`
763  *		( seek for ``MEDIA_LNK_FL_*``)
764  *
765  * Valid values for flags:
766  *
767  * %MEDIA_LNK_FL_ENABLED
768  *   Indicates that the link is enabled and can be used to transfer media data.
769  *   When two or more links target a sink pad, only one of them can be
770  *   enabled at a time.
771  *
772  * %MEDIA_LNK_FL_IMMUTABLE
773  *   Indicates that the link enabled state can't be modified at runtime. If
774  *   %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be
775  *   set, since an immutable link is always enabled.
776  *
777  * .. note::
778  *
779  *    Before calling this function, media_entity_pads_init() and
780  *    media_device_register_entity() should be called previously for both ends.
781  */
782 __must_check int media_create_pad_link(struct media_entity *source,
783 			u16 source_pad, struct media_entity *sink,
784 			u16 sink_pad, u32 flags);
785 
786 /**
787  * media_create_pad_links() - creates a link between two entities.
788  *
789  * @mdev: Pointer to the media_device that contains the object
790  * @source_function: Function of the source entities. Used only if @source is
791  *	NULL.
792  * @source: pointer to &media_entity of the source pad. If NULL, it will use
793  *	all entities that matches the @sink_function.
794  * @source_pad: number of the source pad in the pads array
795  * @sink_function: Function of the sink entities. Used only if @sink is NULL.
796  * @sink: pointer to &media_entity of the sink pad. If NULL, it will use
797  *	all entities that matches the @sink_function.
798  * @sink_pad: number of the sink pad in the pads array.
799  * @flags: Link flags, as defined in include/uapi/linux/media.h.
800  * @allow_both_undefined: if %true, then both @source and @sink can be NULL.
801  *	In such case, it will create a crossbar between all entities that
802  *	matches @source_function to all entities that matches @sink_function.
803  *	If %false, it will return 0 and won't create any link if both @source
804  *	and @sink are NULL.
805  *
806  * Valid values for flags:
807  *
808  * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
809  *	used to transfer media data. If multiple links are created and this
810  *	flag is passed as an argument, only the first created link will have
811  *	this flag.
812  *
813  * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
814  *	be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
815  *	%MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
816  *	always enabled.
817  *
818  * It is common for some devices to have multiple source and/or sink entities
819  * of the same type that should be linked. While media_create_pad_link()
820  * creates link by link, this function is meant to allow 1:n, n:1 and even
821  * cross-bar (n:n) links.
822  *
823  * .. note::
824  *
825  *    Before calling this function, media_entity_pads_init() and
826  *    media_device_register_entity() should be called previously for the
827  *    entities to be linked.
828  */
829 int media_create_pad_links(const struct media_device *mdev,
830 			   const u32 source_function,
831 			   struct media_entity *source,
832 			   const u16 source_pad,
833 			   const u32 sink_function,
834 			   struct media_entity *sink,
835 			   const u16 sink_pad,
836 			   u32 flags,
837 			   const bool allow_both_undefined);
838 
839 void __media_entity_remove_links(struct media_entity *entity);
840 
841 /**
842  * media_entity_remove_links() - remove all links associated with an entity
843  *
844  * @entity:	pointer to &media_entity
845  *
846  * .. note::
847  *
848  *    This is called automatically when an entity is unregistered via
849  *    media_device_register_entity().
850  */
851 void media_entity_remove_links(struct media_entity *entity);
852 
853 /**
854  * __media_entity_setup_link - Configure a media link without locking
855  * @link: The link being configured
856  * @flags: Link configuration flags
857  *
858  * The bulk of link setup is handled by the two entities connected through the
859  * link. This function notifies both entities of the link configuration change.
860  *
861  * If the link is immutable or if the current and new configuration are
862  * identical, return immediately.
863  *
864  * The user is expected to hold link->source->parent->mutex. If not,
865  * media_entity_setup_link() should be used instead.
866  */
867 int __media_entity_setup_link(struct media_link *link, u32 flags);
868 
869 /**
870  * media_entity_setup_link() - changes the link flags properties in runtime
871  *
872  * @link:	pointer to &media_link
873  * @flags:	the requested new link flags
874  *
875  * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
876  * to enable/disable a link. Links marked with the
877  * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
878  *
879  * When a link is enabled or disabled, the media framework calls the
880  * link_setup operation for the two entities at the source and sink of the
881  * link, in that order. If the second link_setup call fails, another
882  * link_setup call is made on the first entity to restore the original link
883  * flags.
884  *
885  * Media device drivers can be notified of link setup operations by setting the
886  * &media_device.link_notify pointer to a callback function. If provided, the
887  * notification callback will be called before enabling and after disabling
888  * links.
889  *
890  * Entity drivers must implement the link_setup operation if any of their links
891  * is non-immutable. The operation must either configure the hardware or store
892  * the configuration information to be applied later.
893  *
894  * Link configuration must not have any side effect on other links. If an
895  * enabled link at a sink pad prevents another link at the same pad from
896  * being enabled, the link_setup operation must return %-EBUSY and can't
897  * implicitly disable the first enabled link.
898  *
899  * .. note::
900  *
901  *    The valid values of the flags for the link is the same as described
902  *    on media_create_pad_link(), for pad to pad links or the same as described
903  *    on media_create_intf_link(), for interface to entity links.
904  */
905 int media_entity_setup_link(struct media_link *link, u32 flags);
906 
907 /**
908  * media_entity_find_link - Find a link between two pads
909  * @source: Source pad
910  * @sink: Sink pad
911  *
912  * Return: returns a pointer to the link between the two entities. If no
913  * such link exists, return %NULL.
914  */
915 struct media_link *media_entity_find_link(struct media_pad *source,
916 		struct media_pad *sink);
917 
918 /**
919  * media_pad_remote_pad_first - Find the first pad at the remote end of a link
920  * @pad: Pad at the local end of the link
921  *
922  * Search for a remote pad connected to the given pad by iterating over all
923  * links originating or terminating at that pad until an enabled link is found.
924  *
925  * Return: returns a pointer to the pad at the remote end of the first found
926  * enabled link, or %NULL if no enabled link has been found.
927  */
928 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad);
929 
930 /**
931  * media_pad_remote_pad_unique - Find a remote pad connected to a pad
932  * @pad: The pad
933  *
934  * Search for and return a remote pad connected to @pad through an enabled
935  * link. If multiple (or no) remote pads are found, an error is returned.
936  *
937  * The uniqueness constraint makes this helper function suitable for entities
938  * that support a single active source at a time on a given pad.
939  *
940  * Return: A pointer to the remote pad, or one of the following error pointers
941  * if an error occurs:
942  *
943  * * -ENOTUNIQ - Multiple links are enabled
944  * * -ENOLINK - No connected pad found
945  */
946 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad);
947 
948 /**
949  * media_entity_remote_pad_unique - Find a remote pad connected to an entity
950  * @entity: The entity
951  * @type: The type of pad to find (MEDIA_PAD_FL_SINK or MEDIA_PAD_FL_SOURCE)
952  *
953  * Search for and return a remote pad of @type connected to @entity through an
954  * enabled link. If multiple (or no) remote pads match these criteria, an error
955  * is returned.
956  *
957  * The uniqueness constraint makes this helper function suitable for entities
958  * that support a single active source or sink at a time.
959  *
960  * Return: A pointer to the remote pad, or one of the following error pointers
961  * if an error occurs:
962  *
963  * * -ENOTUNIQ - Multiple links are enabled
964  * * -ENOLINK - No connected pad found
965  */
966 struct media_pad *
967 media_entity_remote_pad_unique(const struct media_entity *entity,
968 			       unsigned int type);
969 
970 /**
971  * media_entity_remote_source_pad_unique - Find a remote source pad connected to
972  *	an entity
973  * @entity: The entity
974  *
975  * Search for and return a remote source pad connected to @entity through an
976  * enabled link. If multiple (or no) remote pads match these criteria, an error
977  * is returned.
978  *
979  * The uniqueness constraint makes this helper function suitable for entities
980  * that support a single active source at a time.
981  *
982  * Return: A pointer to the remote pad, or one of the following error pointers
983  * if an error occurs:
984  *
985  * * -ENOTUNIQ - Multiple links are enabled
986  * * -ENOLINK - No connected pad found
987  */
988 static inline struct media_pad *
989 media_entity_remote_source_pad_unique(const struct media_entity *entity)
990 {
991 	return media_entity_remote_pad_unique(entity, MEDIA_PAD_FL_SOURCE);
992 }
993 
994 /**
995  * media_pad_is_streaming - Test if a pad is part of a streaming pipeline
996  * @pad: The pad
997  *
998  * Return: True if the pad is part of a pipeline started with the
999  * media_pipeline_start() function, false otherwise.
1000  */
1001 static inline bool media_pad_is_streaming(const struct media_pad *pad)
1002 {
1003 	return pad->pipe;
1004 }
1005 
1006 /**
1007  * media_entity_is_streaming - Test if an entity is part of a streaming pipeline
1008  * @entity: The entity
1009  *
1010  * Return: True if the entity is part of a pipeline started with the
1011  * media_pipeline_start() function, false otherwise.
1012  */
1013 static inline bool media_entity_is_streaming(const struct media_entity *entity)
1014 {
1015 	struct media_pad *pad;
1016 
1017 	media_entity_for_each_pad(entity, pad) {
1018 		if (media_pad_is_streaming(pad))
1019 			return true;
1020 	}
1021 
1022 	return false;
1023 }
1024 
1025 /**
1026  * media_entity_pipeline - Get the media pipeline an entity is part of
1027  * @entity: The entity
1028  *
1029  * DEPRECATED: use media_pad_pipeline() instead.
1030  *
1031  * This function returns the media pipeline that an entity has been associated
1032  * with when constructing the pipeline with media_pipeline_start(). The pointer
1033  * remains valid until media_pipeline_stop() is called.
1034  *
1035  * In general, entities can be part of multiple pipelines, when carrying
1036  * multiple streams (either on different pads, or on the same pad using
1037  * multiplexed streams). This function is to be used only for entities that
1038  * do not support multiple pipelines.
1039  *
1040  * Return: The media_pipeline the entity is part of, or NULL if the entity is
1041  * not part of any pipeline.
1042  */
1043 struct media_pipeline *media_entity_pipeline(struct media_entity *entity);
1044 
1045 /**
1046  * media_pad_pipeline - Get the media pipeline a pad is part of
1047  * @pad: The pad
1048  *
1049  * This function returns the media pipeline that a pad has been associated
1050  * with when constructing the pipeline with media_pipeline_start(). The pointer
1051  * remains valid until media_pipeline_stop() is called.
1052  *
1053  * Return: The media_pipeline the pad is part of, or NULL if the pad is
1054  * not part of any pipeline.
1055  */
1056 struct media_pipeline *media_pad_pipeline(struct media_pad *pad);
1057 
1058 /**
1059  * media_entity_get_fwnode_pad - Get pad number from fwnode
1060  *
1061  * @entity: The entity
1062  * @fwnode: Pointer to the fwnode_handle which should be used to find the pad
1063  * @direction_flags: Expected direction of the pad, as defined in
1064  *		     :ref:`include/uapi/linux/media.h <media_header>`
1065  *		     (seek for ``MEDIA_PAD_FL_*``)
1066  *
1067  * This function can be used to resolve the media pad number from
1068  * a fwnode. This is useful for devices which use more complex
1069  * mappings of media pads.
1070  *
1071  * If the entity does not implement the get_fwnode_pad() operation
1072  * then this function searches the entity for the first pad that
1073  * matches the @direction_flags.
1074  *
1075  * Return: returns the pad number on success or a negative error code.
1076  */
1077 int media_entity_get_fwnode_pad(struct media_entity *entity,
1078 				const struct fwnode_handle *fwnode,
1079 				unsigned long direction_flags);
1080 
1081 /**
1082  * media_graph_walk_init - Allocate resources used by graph walk.
1083  *
1084  * @graph: Media graph structure that will be used to walk the graph
1085  * @mdev: Pointer to the &media_device that contains the object
1086  *
1087  * This function is deprecated, use media_pipeline_for_each_pad() instead.
1088  *
1089  * The caller is required to hold the media_device graph_mutex during the graph
1090  * walk until the graph state is released.
1091  *
1092  * Returns zero on success or a negative error code otherwise.
1093  */
1094 __must_check int media_graph_walk_init(
1095 	struct media_graph *graph, struct media_device *mdev);
1096 
1097 /**
1098  * media_graph_walk_cleanup - Release resources used by graph walk.
1099  *
1100  * @graph: Media graph structure that will be used to walk the graph
1101  *
1102  * This function is deprecated, use media_pipeline_for_each_pad() instead.
1103  */
1104 void media_graph_walk_cleanup(struct media_graph *graph);
1105 
1106 /**
1107  * media_graph_walk_start - Start walking the media graph at a
1108  *	given entity
1109  *
1110  * @graph: Media graph structure that will be used to walk the graph
1111  * @entity: Starting entity
1112  *
1113  * This function is deprecated, use media_pipeline_for_each_pad() instead.
1114  *
1115  * Before using this function, media_graph_walk_init() must be
1116  * used to allocate resources used for walking the graph. This
1117  * function initializes the graph traversal structure to walk the
1118  * entities graph starting at the given entity. The traversal
1119  * structure must not be modified by the caller during graph
1120  * traversal. After the graph walk, the resources must be released
1121  * using media_graph_walk_cleanup().
1122  */
1123 void media_graph_walk_start(struct media_graph *graph,
1124 			    struct media_entity *entity);
1125 
1126 /**
1127  * media_graph_walk_next - Get the next entity in the graph
1128  * @graph: Media graph structure
1129  *
1130  * This function is deprecated, use media_pipeline_for_each_pad() instead.
1131  *
1132  * Perform a depth-first traversal of the given media entities graph.
1133  *
1134  * The graph structure must have been previously initialized with a call to
1135  * media_graph_walk_start().
1136  *
1137  * Return: returns the next entity in the graph or %NULL if the whole graph
1138  * have been traversed.
1139  */
1140 struct media_entity *media_graph_walk_next(struct media_graph *graph);
1141 
1142 /**
1143  * media_pipeline_start - Mark a pipeline as streaming
1144  * @pad: Starting pad
1145  * @pipe: Media pipeline to be assigned to all pads in the pipeline.
1146  *
1147  * Mark all pads connected to a given pad through enabled links, either
1148  * directly or indirectly, as streaming. The given pipeline object is assigned
1149  * to every pad in the pipeline and stored in the media_pad pipe field.
1150  *
1151  * Calls to this function can be nested, in which case the same number of
1152  * media_pipeline_stop() calls will be required to stop streaming. The
1153  * pipeline pointer must be identical for all nested calls to
1154  * media_pipeline_start().
1155  */
1156 __must_check int media_pipeline_start(struct media_pad *pad,
1157 				      struct media_pipeline *pipe);
1158 /**
1159  * __media_pipeline_start - Mark a pipeline as streaming
1160  *
1161  * @pad: Starting pad
1162  * @pipe: Media pipeline to be assigned to all pads in the pipeline.
1163  *
1164  * ..note:: This is the non-locking version of media_pipeline_start()
1165  */
1166 __must_check int __media_pipeline_start(struct media_pad *pad,
1167 					struct media_pipeline *pipe);
1168 
1169 /**
1170  * media_pipeline_stop - Mark a pipeline as not streaming
1171  * @pad: Starting pad
1172  *
1173  * Mark all pads connected to a given pad through enabled links, either
1174  * directly or indirectly, as not streaming. The media_pad pipe field is
1175  * reset to %NULL.
1176  *
1177  * If multiple calls to media_pipeline_start() have been made, the same
1178  * number of calls to this function are required to mark the pipeline as not
1179  * streaming.
1180  */
1181 void media_pipeline_stop(struct media_pad *pad);
1182 
1183 /**
1184  * __media_pipeline_stop - Mark a pipeline as not streaming
1185  *
1186  * @pad: Starting pad
1187  *
1188  * .. note:: This is the non-locking version of media_pipeline_stop()
1189  */
1190 void __media_pipeline_stop(struct media_pad *pad);
1191 
1192 struct media_pad *
1193 __media_pipeline_pad_iter_next(struct media_pipeline *pipe,
1194 			       struct media_pipeline_pad_iter *iter,
1195 			       struct media_pad *pad);
1196 
1197 /**
1198  * media_pipeline_for_each_pad - Iterate on all pads in a media pipeline
1199  * @pipe: The pipeline
1200  * @iter: The iterator (struct media_pipeline_pad_iter)
1201  * @pad: The iterator pad
1202  *
1203  * Iterate on all pads in a media pipeline. This is only valid after the
1204  * pipeline has been built with media_pipeline_start() and before it gets
1205  * destroyed with media_pipeline_stop().
1206  */
1207 #define media_pipeline_for_each_pad(pipe, iter, pad)			\
1208 	for (pad = __media_pipeline_pad_iter_next((pipe), iter, NULL);	\
1209 	     pad != NULL;						\
1210 	     pad = __media_pipeline_pad_iter_next((pipe), iter, pad))
1211 
1212 /**
1213  * media_pipeline_entity_iter_init - Initialize a pipeline entity iterator
1214  * @pipe: The pipeline
1215  * @iter: The iterator
1216  *
1217  * This function must be called to initialize the iterator before using it in a
1218  * media_pipeline_for_each_entity() loop. The iterator must be destroyed by a
1219  * call to media_pipeline_entity_iter_cleanup after the loop (including in code
1220  * paths that break from the loop).
1221  *
1222  * The same iterator can be used in multiple consecutive loops without being
1223  * destroyed and reinitialized.
1224  *
1225  * Return: 0 on success or a negative error code otherwise.
1226  */
1227 int media_pipeline_entity_iter_init(struct media_pipeline *pipe,
1228 				    struct media_pipeline_entity_iter *iter);
1229 
1230 /**
1231  * media_pipeline_entity_iter_cleanup - Destroy a pipeline entity iterator
1232  * @iter: The iterator
1233  *
1234  * This function must be called to destroy iterators initialized with
1235  * media_pipeline_entity_iter_init().
1236  */
1237 void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter);
1238 
1239 struct media_entity *
1240 __media_pipeline_entity_iter_next(struct media_pipeline *pipe,
1241 				  struct media_pipeline_entity_iter *iter,
1242 				  struct media_entity *entity);
1243 
1244 /**
1245  * media_pipeline_for_each_entity - Iterate on all entities in a media pipeline
1246  * @pipe: The pipeline
1247  * @iter: The iterator (struct media_pipeline_entity_iter)
1248  * @entity: The iterator entity
1249  *
1250  * Iterate on all entities in a media pipeline. This is only valid after the
1251  * pipeline has been built with media_pipeline_start() and before it gets
1252  * destroyed with media_pipeline_stop(). The iterator must be initialized with
1253  * media_pipeline_entity_iter_init() before iteration, and destroyed with
1254  * media_pipeline_entity_iter_cleanup() after (including in code paths that
1255  * break from the loop).
1256  */
1257 #define media_pipeline_for_each_entity(pipe, iter, entity)			\
1258 	for (entity = __media_pipeline_entity_iter_next((pipe), iter, NULL);	\
1259 	     entity != NULL;							\
1260 	     entity = __media_pipeline_entity_iter_next((pipe), iter, entity))
1261 
1262 /**
1263  * media_pipeline_alloc_start - Mark a pipeline as streaming
1264  * @pad: Starting pad
1265  *
1266  * media_pipeline_alloc_start() is similar to media_pipeline_start() but instead
1267  * of working on a given pipeline the function will use an existing pipeline if
1268  * the pad is already part of a pipeline, or allocate a new pipeline.
1269  *
1270  * Calls to media_pipeline_alloc_start() must be matched with
1271  * media_pipeline_stop().
1272  */
1273 __must_check int media_pipeline_alloc_start(struct media_pad *pad);
1274 
1275 /**
1276  * media_devnode_create() - creates and initializes a device node interface
1277  *
1278  * @mdev:	pointer to struct &media_device
1279  * @type:	type of the interface, as given by
1280  *		:ref:`include/uapi/linux/media.h <media_header>`
1281  *		( seek for ``MEDIA_INTF_T_*``) macros.
1282  * @flags:	Interface flags, as defined in
1283  *		:ref:`include/uapi/linux/media.h <media_header>`
1284  *		( seek for ``MEDIA_INTF_FL_*``)
1285  * @major:	Device node major number.
1286  * @minor:	Device node minor number.
1287  *
1288  * Return: if succeeded, returns a pointer to the newly allocated
1289  *	&media_intf_devnode pointer.
1290  *
1291  * .. note::
1292  *
1293  *    Currently, no flags for &media_interface is defined.
1294  */
1295 struct media_intf_devnode *
1296 __must_check media_devnode_create(struct media_device *mdev,
1297 				  u32 type, u32 flags,
1298 				  u32 major, u32 minor);
1299 /**
1300  * media_devnode_remove() - removes a device node interface
1301  *
1302  * @devnode:	pointer to &media_intf_devnode to be freed.
1303  *
1304  * When a device node interface is removed, all links to it are automatically
1305  * removed.
1306  */
1307 void media_devnode_remove(struct media_intf_devnode *devnode);
1308 
1309 /**
1310  * media_create_intf_link() - creates a link between an entity and an interface
1311  *
1312  * @entity:	pointer to %media_entity
1313  * @intf:	pointer to %media_interface
1314  * @flags:	Link flags, as defined in
1315  *		:ref:`include/uapi/linux/media.h <media_header>`
1316  *		( seek for ``MEDIA_LNK_FL_*``)
1317  *
1318  *
1319  * Valid values for flags:
1320  *
1321  * %MEDIA_LNK_FL_ENABLED
1322  *   Indicates that the interface is connected to the entity hardware.
1323  *   That's the default value for interfaces. An interface may be disabled if
1324  *   the hardware is busy due to the usage of some other interface that it is
1325  *   currently controlling the hardware.
1326  *
1327  *   A typical example is an hybrid TV device that handle only one type of
1328  *   stream on a given time. So, when the digital TV is streaming,
1329  *   the V4L2 interfaces won't be enabled, as such device is not able to
1330  *   also stream analog TV or radio.
1331  *
1332  * .. note::
1333  *
1334  *    Before calling this function, media_devnode_create() should be called for
1335  *    the interface and media_device_register_entity() should be called for the
1336  *    interface that will be part of the link.
1337  */
1338 struct media_link *
1339 __must_check media_create_intf_link(struct media_entity *entity,
1340 				    struct media_interface *intf,
1341 				    u32 flags);
1342 /**
1343  * __media_remove_intf_link() - remove a single interface link
1344  *
1345  * @link:	pointer to &media_link.
1346  *
1347  * .. note:: This is an unlocked version of media_remove_intf_link()
1348  */
1349 void __media_remove_intf_link(struct media_link *link);
1350 
1351 /**
1352  * media_remove_intf_link() - remove a single interface link
1353  *
1354  * @link:	pointer to &media_link.
1355  *
1356  * .. note:: Prefer to use this one, instead of __media_remove_intf_link()
1357  */
1358 void media_remove_intf_link(struct media_link *link);
1359 
1360 /**
1361  * __media_remove_intf_links() - remove all links associated with an interface
1362  *
1363  * @intf:	pointer to &media_interface
1364  *
1365  * .. note:: This is an unlocked version of media_remove_intf_links().
1366  */
1367 void __media_remove_intf_links(struct media_interface *intf);
1368 
1369 /**
1370  * media_remove_intf_links() - remove all links associated with an interface
1371  *
1372  * @intf:	pointer to &media_interface
1373  *
1374  * .. note::
1375  *
1376  *   #) This is called automatically when an entity is unregistered via
1377  *      media_device_register_entity() and by media_devnode_remove().
1378  *
1379  *   #) Prefer to use this one, instead of __media_remove_intf_links().
1380  */
1381 void media_remove_intf_links(struct media_interface *intf);
1382 
1383 /**
1384  * media_entity_call - Calls a struct media_entity_operations operation on
1385  *	an entity
1386  *
1387  * @entity: entity where the @operation will be called
1388  * @operation: type of the operation. Should be the name of a member of
1389  *	struct &media_entity_operations.
1390  *
1391  * This helper function will check if @operation is not %NULL. On such case,
1392  * it will issue a call to @operation\(@entity, @args\).
1393  */
1394 
1395 #define media_entity_call(entity, operation, args...)			\
1396 	(((entity)->ops && (entity)->ops->operation) ?			\
1397 	 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
1398 
1399 /**
1400  * media_create_ancillary_link() - create an ancillary link between two
1401  *				   instances of &media_entity
1402  *
1403  * @primary:	pointer to the primary &media_entity
1404  * @ancillary:	pointer to the ancillary &media_entity
1405  *
1406  * Create an ancillary link between two entities, indicating that they
1407  * represent two connected pieces of hardware that form a single logical unit.
1408  * A typical example is a camera lens controller being linked to the sensor that
1409  * it is supporting.
1410  *
1411  * The function sets both MEDIA_LNK_FL_ENABLED and MEDIA_LNK_FL_IMMUTABLE for
1412  * the new link.
1413  */
1414 struct media_link *
1415 media_create_ancillary_link(struct media_entity *primary,
1416 			    struct media_entity *ancillary);
1417 
1418 /**
1419  * __media_entity_next_link() - Iterate through a &media_entity's links
1420  *
1421  * @entity:	pointer to the &media_entity
1422  * @link:	pointer to a &media_link to hold the iterated values
1423  * @link_type:	one of the MEDIA_LNK_FL_LINK_TYPE flags
1424  *
1425  * Return the next link against an entity matching a specific link type. This
1426  * allows iteration through an entity's links whilst guaranteeing all of the
1427  * returned links are of the given type.
1428  */
1429 struct media_link *__media_entity_next_link(struct media_entity *entity,
1430 					    struct media_link *link,
1431 					    unsigned long link_type);
1432 
1433 /**
1434  * for_each_media_entity_data_link() - Iterate through an entity's data links
1435  *
1436  * @entity:	pointer to the &media_entity
1437  * @link:	pointer to a &media_link to hold the iterated values
1438  *
1439  * Iterate over a &media_entity's data links
1440  */
1441 #define for_each_media_entity_data_link(entity, link)			\
1442 	for (link = __media_entity_next_link(entity, NULL,		\
1443 					     MEDIA_LNK_FL_DATA_LINK);	\
1444 	     link;							\
1445 	     link = __media_entity_next_link(entity, link,		\
1446 					     MEDIA_LNK_FL_DATA_LINK))
1447 
1448 #endif
1449