xref: /linux/include/media/media-entity.h (revision 97733180fafbeb7cc3fd1c8be60d05980615f5d6)
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/types.h>
21 
22 /* Enums used internally at the media controller to represent graphs */
23 
24 /**
25  * enum media_gobj_type - type of a graph object
26  *
27  * @MEDIA_GRAPH_ENTITY:		Identify a media entity
28  * @MEDIA_GRAPH_PAD:		Identify a media pad
29  * @MEDIA_GRAPH_LINK:		Identify a media link
30  * @MEDIA_GRAPH_INTF_DEVNODE:	Identify a media Kernel API interface via
31  *				a device node
32  */
33 enum media_gobj_type {
34 	MEDIA_GRAPH_ENTITY,
35 	MEDIA_GRAPH_PAD,
36 	MEDIA_GRAPH_LINK,
37 	MEDIA_GRAPH_INTF_DEVNODE,
38 };
39 
40 #define MEDIA_BITS_PER_TYPE		8
41 #define MEDIA_BITS_PER_ID		(32 - MEDIA_BITS_PER_TYPE)
42 #define MEDIA_ID_MASK			 GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)
43 
44 /* Structs to represent the objects that belong to a media graph */
45 
46 /**
47  * struct media_gobj - Define a graph object.
48  *
49  * @mdev:	Pointer to the struct &media_device that owns the object
50  * @id:		Non-zero object ID identifier. The ID should be unique
51  *		inside a media_device, as it is composed by
52  *		%MEDIA_BITS_PER_TYPE to store the type plus
53  *		%MEDIA_BITS_PER_ID to store the ID
54  * @list:	List entry stored in one of the per-type mdev object lists
55  *
56  * All objects on the media graph should have this struct embedded
57  */
58 struct media_gobj {
59 	struct media_device	*mdev;
60 	u32			id;
61 	struct list_head	list;
62 };
63 
64 #define MEDIA_ENTITY_ENUM_MAX_DEPTH	16
65 
66 /**
67  * struct media_entity_enum - An enumeration of media entities.
68  *
69  * @bmap:	Bit map in which each bit represents one entity at struct
70  *		media_entity->internal_idx.
71  * @idx_max:	Number of bits in bmap
72  */
73 struct media_entity_enum {
74 	unsigned long *bmap;
75 	int idx_max;
76 };
77 
78 /**
79  * struct media_graph - Media graph traversal state
80  *
81  * @stack:		Graph traversal stack; the stack contains information
82  *			on the path the media entities to be walked and the
83  *			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 entities
87  * @top:		The top of the stack
88  */
89 struct media_graph {
90 	struct {
91 		struct media_entity *entity;
92 		struct list_head *link;
93 	} stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
94 
95 	struct media_entity_enum ent_enum;
96 	int top;
97 };
98 
99 /**
100  * struct media_pipeline - Media pipeline related information
101  *
102  * @streaming_count:	Streaming start count - streaming stop count
103  * @graph:		Media graph walk during pipeline start / stop
104  */
105 struct media_pipeline {
106 	int streaming_count;
107 	struct media_graph graph;
108 };
109 
110 /**
111  * struct media_link - A link object part of a media graph.
112  *
113  * @graph_obj:	Embedded structure containing the media object common data
114  * @list:	Linked list associated with an entity or an interface that
115  *		owns the link.
116  * @gobj0:	Part of a union. Used to get the pointer for the first
117  *		graph_object of the link.
118  * @source:	Part of a union. Used only if the first object (gobj0) is
119  *		a pad. In that case, it represents the source pad.
120  * @intf:	Part of a union. Used only if the first object (gobj0) is
121  *		an interface.
122  * @gobj1:	Part of a union. Used to get the pointer for the second
123  *		graph_object of the link.
124  * @sink:	Part of a union. Used only if the second object (gobj1) is
125  *		a pad. In that case, it represents the sink pad.
126  * @entity:	Part of a union. Used only if the second object (gobj1) is
127  *		an entity.
128  * @reverse:	Pointer to the link for the reverse direction of a pad to pad
129  *		link.
130  * @flags:	Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
131  * @is_backlink: Indicate if the link is a backlink.
132  */
133 struct media_link {
134 	struct media_gobj graph_obj;
135 	struct list_head list;
136 	union {
137 		struct media_gobj *gobj0;
138 		struct media_pad *source;
139 		struct media_interface *intf;
140 	};
141 	union {
142 		struct media_gobj *gobj1;
143 		struct media_pad *sink;
144 		struct media_entity *entity;
145 	};
146 	struct media_link *reverse;
147 	unsigned long flags;
148 	bool is_backlink;
149 };
150 
151 /**
152  * enum media_pad_signal_type - type of the signal inside a media pad
153  *
154  * @PAD_SIGNAL_DEFAULT:
155  *	Default signal. Use this when all inputs or all outputs are
156  *	uniquely identified by the pad number.
157  * @PAD_SIGNAL_ANALOG:
158  *	The pad contains an analog signal. It can be Radio Frequency,
159  *	Intermediate Frequency, a baseband signal or sub-carriers.
160  *	Tuner inputs, IF-PLL demodulators, composite and s-video signals
161  *	should use it.
162  * @PAD_SIGNAL_DV:
163  *	Contains a digital video signal, with can be a bitstream of samples
164  *	taken from an analog TV video source. On such case, it usually
165  *	contains the VBI data on it.
166  * @PAD_SIGNAL_AUDIO:
167  *	Contains an Intermediate Frequency analog signal from an audio
168  *	sub-carrier or an audio bitstream. IF signals are provided by tuners
169  *	and consumed by	audio AM/FM decoders. Bitstream audio is provided by
170  *	an audio decoder.
171  */
172 enum media_pad_signal_type {
173 	PAD_SIGNAL_DEFAULT = 0,
174 	PAD_SIGNAL_ANALOG,
175 	PAD_SIGNAL_DV,
176 	PAD_SIGNAL_AUDIO,
177 };
178 
179 /**
180  * struct media_pad - A media pad graph object.
181  *
182  * @graph_obj:	Embedded structure containing the media object common data
183  * @entity:	Entity this pad belongs to
184  * @index:	Pad index in the entity pads array, numbered from 0 to n
185  * @sig_type:	Type of the signal inside a media pad
186  * @flags:	Pad flags, as defined in
187  *		:ref:`include/uapi/linux/media.h <media_header>`
188  *		(seek for ``MEDIA_PAD_FL_*``)
189  */
190 struct media_pad {
191 	struct media_gobj graph_obj;	/* must be first field in struct */
192 	struct media_entity *entity;
193 	u16 index;
194 	enum media_pad_signal_type sig_type;
195 	unsigned long flags;
196 };
197 
198 /**
199  * struct media_entity_operations - Media entity operations
200  * @get_fwnode_pad:	Return the pad number based on a fwnode endpoint or
201  *			a negative value on error. This operation can be used
202  *			to map a fwnode to a media pad number. Optional.
203  * @link_setup:		Notify the entity of link changes. The operation can
204  *			return an error, in which case link setup will be
205  *			cancelled. Optional.
206  * @link_validate:	Return whether a link is valid from the entity point of
207  *			view. The media_pipeline_start() function
208  *			validates all links by calling this operation. Optional.
209  *
210  * .. note::
211  *
212  *    Those these callbacks are called with struct &media_device.graph_mutex
213  *    mutex held.
214  */
215 struct media_entity_operations {
216 	int (*get_fwnode_pad)(struct media_entity *entity,
217 			      struct fwnode_endpoint *endpoint);
218 	int (*link_setup)(struct media_entity *entity,
219 			  const struct media_pad *local,
220 			  const struct media_pad *remote, u32 flags);
221 	int (*link_validate)(struct media_link *link);
222 };
223 
224 /**
225  * enum media_entity_type - Media entity type
226  *
227  * @MEDIA_ENTITY_TYPE_BASE:
228  *	The entity isn't embedded in another subsystem structure.
229  * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:
230  *	The entity is embedded in a struct video_device instance.
231  * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:
232  *	The entity is embedded in a struct v4l2_subdev instance.
233  *
234  * Media entity objects are often not instantiated directly, but the media
235  * entity structure is inherited by (through embedding) other subsystem-specific
236  * structures. The media entity type identifies the type of the subclass
237  * structure that implements a media entity instance.
238  *
239  * This allows runtime type identification of media entities and safe casting to
240  * the correct object type. For instance, a media entity structure instance
241  * embedded in a v4l2_subdev structure instance will have the type
242  * %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev
243  * structure using the container_of() macro.
244  */
245 enum media_entity_type {
246 	MEDIA_ENTITY_TYPE_BASE,
247 	MEDIA_ENTITY_TYPE_VIDEO_DEVICE,
248 	MEDIA_ENTITY_TYPE_V4L2_SUBDEV,
249 };
250 
251 /**
252  * struct media_entity - A media entity graph object.
253  *
254  * @graph_obj:	Embedded structure containing the media object common data.
255  * @name:	Entity name.
256  * @obj_type:	Type of the object that implements the media_entity.
257  * @function:	Entity main function, as defined in
258  *		:ref:`include/uapi/linux/media.h <media_header>`
259  *		(seek for ``MEDIA_ENT_F_*``)
260  * @flags:	Entity flags, as defined in
261  *		:ref:`include/uapi/linux/media.h <media_header>`
262  *		(seek for ``MEDIA_ENT_FL_*``)
263  * @num_pads:	Number of sink and source pads.
264  * @num_links:	Total number of links, forward and back, enabled and disabled.
265  * @num_backlinks: Number of backlinks
266  * @internal_idx: An unique internal entity specific number. The numbers are
267  *		re-used if entities are unregistered or registered again.
268  * @pads:	Pads array with the size defined by @num_pads.
269  * @links:	List of data links.
270  * @ops:	Entity operations.
271  * @stream_count: Stream count for the entity.
272  * @use_count:	Use count for the entity.
273  * @pipe:	Pipeline this entity belongs to.
274  * @info:	Union with devnode information.  Kept just for backward
275  *		compatibility.
276  * @info.dev:	Contains device major and minor info.
277  * @info.dev.major: device node major, if the device is a devnode.
278  * @info.dev.minor: device node minor, if the device is a devnode.
279  * @major:	Devnode major number (zero if not applicable). Kept just
280  *		for backward compatibility.
281  * @minor:	Devnode minor number (zero if not applicable). Kept just
282  *		for backward compatibility.
283  *
284  * .. note::
285  *
286  *    @stream_count and @use_count reference counts must never be
287  *    negative, but are signed integers on purpose: a simple ``WARN_ON(<0)``
288  *    check can be used to detect reference count bugs that would make them
289  *    negative.
290  */
291 struct media_entity {
292 	struct media_gobj graph_obj;	/* must be first field in struct */
293 	const char *name;
294 	enum media_entity_type obj_type;
295 	u32 function;
296 	unsigned long flags;
297 
298 	u16 num_pads;
299 	u16 num_links;
300 	u16 num_backlinks;
301 	int internal_idx;
302 
303 	struct media_pad *pads;
304 	struct list_head links;
305 
306 	const struct media_entity_operations *ops;
307 
308 	int stream_count;
309 	int use_count;
310 
311 	struct media_pipeline *pipe;
312 
313 	union {
314 		struct {
315 			u32 major;
316 			u32 minor;
317 		} dev;
318 	} info;
319 };
320 
321 /**
322  * struct media_interface - A media interface graph object.
323  *
324  * @graph_obj:		embedded graph object
325  * @links:		List of links pointing to graph entities
326  * @type:		Type of the interface as defined in
327  *			:ref:`include/uapi/linux/media.h <media_header>`
328  *			(seek for ``MEDIA_INTF_T_*``)
329  * @flags:		Interface flags as defined in
330  *			:ref:`include/uapi/linux/media.h <media_header>`
331  *			(seek for ``MEDIA_INTF_FL_*``)
332  *
333  * .. note::
334  *
335  *    Currently, no flags for &media_interface is defined.
336  */
337 struct media_interface {
338 	struct media_gobj		graph_obj;
339 	struct list_head		links;
340 	u32				type;
341 	u32				flags;
342 };
343 
344 /**
345  * struct media_intf_devnode - A media interface via a device node.
346  *
347  * @intf:	embedded interface object
348  * @major:	Major number of a device node
349  * @minor:	Minor number of a device node
350  */
351 struct media_intf_devnode {
352 	struct media_interface		intf;
353 
354 	/* Should match the fields at media_v2_intf_devnode */
355 	u32				major;
356 	u32				minor;
357 };
358 
359 /**
360  * media_entity_id() - return the media entity graph object id
361  *
362  * @entity:	pointer to &media_entity
363  */
364 static inline u32 media_entity_id(struct media_entity *entity)
365 {
366 	return entity->graph_obj.id;
367 }
368 
369 /**
370  * media_type() - return the media object type
371  *
372  * @gobj:	Pointer to the struct &media_gobj graph object
373  */
374 static inline enum media_gobj_type media_type(struct media_gobj *gobj)
375 {
376 	return gobj->id >> MEDIA_BITS_PER_ID;
377 }
378 
379 /**
380  * media_id() - return the media object ID
381  *
382  * @gobj:	Pointer to the struct &media_gobj graph object
383  */
384 static inline u32 media_id(struct media_gobj *gobj)
385 {
386 	return gobj->id & MEDIA_ID_MASK;
387 }
388 
389 /**
390  * media_gobj_gen_id() - encapsulates type and ID on at the object ID
391  *
392  * @type:	object type as define at enum &media_gobj_type.
393  * @local_id:	next ID, from struct &media_device.id.
394  */
395 static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
396 {
397 	u32 id;
398 
399 	id = type << MEDIA_BITS_PER_ID;
400 	id |= local_id & MEDIA_ID_MASK;
401 
402 	return id;
403 }
404 
405 /**
406  * is_media_entity_v4l2_video_device() - Check if the entity is a video_device
407  * @entity:	pointer to entity
408  *
409  * Return: %true if the entity is an instance of a video_device object and can
410  * safely be cast to a struct video_device using the container_of() macro, or
411  * %false otherwise.
412  */
413 static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity)
414 {
415 	return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
416 }
417 
418 /**
419  * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev
420  * @entity:	pointer to entity
421  *
422  * Return: %true if the entity is an instance of a &v4l2_subdev object and can
423  * safely be cast to a struct &v4l2_subdev using the container_of() macro, or
424  * %false otherwise.
425  */
426 static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
427 {
428 	return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
429 }
430 
431 /**
432  * __media_entity_enum_init - Initialise an entity enumeration
433  *
434  * @ent_enum: Entity enumeration to be initialised
435  * @idx_max: Maximum number of entities in the enumeration
436  *
437  * Return: Returns zero on success or a negative error code.
438  */
439 __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
440 					  int idx_max);
441 
442 /**
443  * media_entity_enum_cleanup - Release resources of an entity enumeration
444  *
445  * @ent_enum: Entity enumeration to be released
446  */
447 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
448 
449 /**
450  * media_entity_enum_zero - Clear the entire enum
451  *
452  * @ent_enum: Entity enumeration to be cleared
453  */
454 static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
455 {
456 	bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
457 }
458 
459 /**
460  * media_entity_enum_set - Mark a single entity in the enum
461  *
462  * @ent_enum: Entity enumeration
463  * @entity: Entity to be marked
464  */
465 static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
466 					 struct media_entity *entity)
467 {
468 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
469 		return;
470 
471 	__set_bit(entity->internal_idx, ent_enum->bmap);
472 }
473 
474 /**
475  * media_entity_enum_clear - Unmark a single entity in the enum
476  *
477  * @ent_enum: Entity enumeration
478  * @entity: Entity to be unmarked
479  */
480 static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
481 					   struct media_entity *entity)
482 {
483 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
484 		return;
485 
486 	__clear_bit(entity->internal_idx, ent_enum->bmap);
487 }
488 
489 /**
490  * media_entity_enum_test - Test whether the entity is marked
491  *
492  * @ent_enum: Entity enumeration
493  * @entity: Entity to be tested
494  *
495  * Returns %true if the entity was marked.
496  */
497 static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
498 					  struct media_entity *entity)
499 {
500 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
501 		return true;
502 
503 	return test_bit(entity->internal_idx, ent_enum->bmap);
504 }
505 
506 /**
507  * media_entity_enum_test_and_set - Test whether the entity is marked,
508  *	and mark it
509  *
510  * @ent_enum: Entity enumeration
511  * @entity: Entity to be tested
512  *
513  * Returns %true if the entity was marked, and mark it before doing so.
514  */
515 static inline bool
516 media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,
517 			       struct media_entity *entity)
518 {
519 	if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
520 		return true;
521 
522 	return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
523 }
524 
525 /**
526  * media_entity_enum_empty - Test whether the entire enum is empty
527  *
528  * @ent_enum: Entity enumeration
529  *
530  * Return: %true if the entity was empty.
531  */
532 static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
533 {
534 	return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
535 }
536 
537 /**
538  * media_entity_enum_intersects - Test whether two enums intersect
539  *
540  * @ent_enum1: First entity enumeration
541  * @ent_enum2: Second entity enumeration
542  *
543  * Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect,
544  * otherwise %false.
545  */
546 static inline bool media_entity_enum_intersects(
547 	struct media_entity_enum *ent_enum1,
548 	struct media_entity_enum *ent_enum2)
549 {
550 	WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
551 
552 	return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
553 				 min(ent_enum1->idx_max, ent_enum2->idx_max));
554 }
555 
556 /**
557  * gobj_to_entity - returns the struct &media_entity pointer from the
558  *	@gobj contained on it.
559  *
560  * @gobj: Pointer to the struct &media_gobj graph object
561  */
562 #define gobj_to_entity(gobj) \
563 		container_of(gobj, struct media_entity, graph_obj)
564 
565 /**
566  * gobj_to_pad - returns the struct &media_pad pointer from the
567  *	@gobj contained on it.
568  *
569  * @gobj: Pointer to the struct &media_gobj graph object
570  */
571 #define gobj_to_pad(gobj) \
572 		container_of(gobj, struct media_pad, graph_obj)
573 
574 /**
575  * gobj_to_link - returns the struct &media_link pointer from the
576  *	@gobj contained on it.
577  *
578  * @gobj: Pointer to the struct &media_gobj graph object
579  */
580 #define gobj_to_link(gobj) \
581 		container_of(gobj, struct media_link, graph_obj)
582 
583 /**
584  * gobj_to_intf - returns the struct &media_interface pointer from the
585  *	@gobj contained on it.
586  *
587  * @gobj: Pointer to the struct &media_gobj graph object
588  */
589 #define gobj_to_intf(gobj) \
590 		container_of(gobj, struct media_interface, graph_obj)
591 
592 /**
593  * intf_to_devnode - returns the struct media_intf_devnode pointer from the
594  *	@intf contained on it.
595  *
596  * @intf: Pointer to struct &media_intf_devnode
597  */
598 #define intf_to_devnode(intf) \
599 		container_of(intf, struct media_intf_devnode, intf)
600 
601 /**
602  *  media_gobj_create - Initialize a graph object
603  *
604  * @mdev:	Pointer to the &media_device that contains the object
605  * @type:	Type of the object
606  * @gobj:	Pointer to the struct &media_gobj graph object
607  *
608  * This routine initializes the embedded struct &media_gobj inside a
609  * media graph object. It is called automatically if ``media_*_create``
610  * function calls are used. However, if the object (entity, link, pad,
611  * interface) is embedded on some other object, this function should be
612  * called before registering the object at the media controller.
613  */
614 void media_gobj_create(struct media_device *mdev,
615 		    enum media_gobj_type type,
616 		    struct media_gobj *gobj);
617 
618 /**
619  *  media_gobj_destroy - Stop using a graph object on a media device
620  *
621  * @gobj:	Pointer to the struct &media_gobj graph object
622  *
623  * This should be called by all routines like media_device_unregister()
624  * that remove/destroy media graph objects.
625  */
626 void media_gobj_destroy(struct media_gobj *gobj);
627 
628 /**
629  * media_entity_pads_init() - Initialize the entity pads
630  *
631  * @entity:	entity where the pads belong
632  * @num_pads:	total number of sink and source pads
633  * @pads:	Array of @num_pads pads.
634  *
635  * The pads array is managed by the entity driver and passed to
636  * media_entity_pads_init() where its pointer will be stored in the
637  * &media_entity structure.
638  *
639  * If no pads are needed, drivers could either directly fill
640  * &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call
641  * this function that will do the same.
642  *
643  * As the number of pads is known in advance, the pads array is not allocated
644  * dynamically but is managed by the entity driver. Most drivers will embed the
645  * pads array in a driver-specific structure, avoiding dynamic allocation.
646  *
647  * Drivers must set the direction of every pad in the pads array before calling
648  * media_entity_pads_init(). The function will initialize the other pads fields.
649  */
650 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
651 		      struct media_pad *pads);
652 
653 /**
654  * media_entity_cleanup() - free resources associated with an entity
655  *
656  * @entity:	entity where the pads belong
657  *
658  * This function must be called during the cleanup phase after unregistering
659  * the entity (currently, it does nothing).
660  */
661 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
662 static inline void media_entity_cleanup(struct media_entity *entity) {}
663 #else
664 #define media_entity_cleanup(entity) do { } while (false)
665 #endif
666 
667 /**
668  * media_get_pad_index() - retrieves a pad index from an entity
669  *
670  * @entity:	entity where the pads belong
671  * @is_sink:	true if the pad is a sink, false if it is a source
672  * @sig_type:	type of signal of the pad to be search
673  *
674  * This helper function finds the first pad index inside an entity that
675  * satisfies both @is_sink and @sig_type conditions.
676  *
677  * Return:
678  *
679  * On success, return the pad number. If the pad was not found or the media
680  * entity is a NULL pointer, return -EINVAL.
681  */
682 int media_get_pad_index(struct media_entity *entity, bool is_sink,
683 			enum media_pad_signal_type sig_type);
684 
685 /**
686  * media_create_pad_link() - creates a link between two entities.
687  *
688  * @source:	pointer to &media_entity of the source pad.
689  * @source_pad:	number of the source pad in the pads array
690  * @sink:	pointer to &media_entity of the sink pad.
691  * @sink_pad:	number of the sink pad in the pads array.
692  * @flags:	Link flags, as defined in
693  *		:ref:`include/uapi/linux/media.h <media_header>`
694  *		( seek for ``MEDIA_LNK_FL_*``)
695  *
696  * Valid values for flags:
697  *
698  * %MEDIA_LNK_FL_ENABLED
699  *   Indicates that the link is enabled and can be used to transfer media data.
700  *   When two or more links target a sink pad, only one of them can be
701  *   enabled at a time.
702  *
703  * %MEDIA_LNK_FL_IMMUTABLE
704  *   Indicates that the link enabled state can't be modified at runtime. If
705  *   %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be
706  *   set, since an immutable link is always enabled.
707  *
708  * .. note::
709  *
710  *    Before calling this function, media_entity_pads_init() and
711  *    media_device_register_entity() should be called previously for both ends.
712  */
713 __must_check int media_create_pad_link(struct media_entity *source,
714 			u16 source_pad, struct media_entity *sink,
715 			u16 sink_pad, u32 flags);
716 
717 /**
718  * media_create_pad_links() - creates a link between two entities.
719  *
720  * @mdev: Pointer to the media_device that contains the object
721  * @source_function: Function of the source entities. Used only if @source is
722  *	NULL.
723  * @source: pointer to &media_entity of the source pad. If NULL, it will use
724  *	all entities that matches the @sink_function.
725  * @source_pad: number of the source pad in the pads array
726  * @sink_function: Function of the sink entities. Used only if @sink is NULL.
727  * @sink: pointer to &media_entity of the sink pad. If NULL, it will use
728  *	all entities that matches the @sink_function.
729  * @sink_pad: number of the sink pad in the pads array.
730  * @flags: Link flags, as defined in include/uapi/linux/media.h.
731  * @allow_both_undefined: if %true, then both @source and @sink can be NULL.
732  *	In such case, it will create a crossbar between all entities that
733  *	matches @source_function to all entities that matches @sink_function.
734  *	If %false, it will return 0 and won't create any link if both @source
735  *	and @sink are NULL.
736  *
737  * Valid values for flags:
738  *
739  * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
740  *	used to transfer media data. If multiple links are created and this
741  *	flag is passed as an argument, only the first created link will have
742  *	this flag.
743  *
744  * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
745  *	be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
746  *	%MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
747  *	always enabled.
748  *
749  * It is common for some devices to have multiple source and/or sink entities
750  * of the same type that should be linked. While media_create_pad_link()
751  * creates link by link, this function is meant to allow 1:n, n:1 and even
752  * cross-bar (n:n) links.
753  *
754  * .. note::
755  *
756  *    Before calling this function, media_entity_pads_init() and
757  *    media_device_register_entity() should be called previously for the
758  *    entities to be linked.
759  */
760 int media_create_pad_links(const struct media_device *mdev,
761 			   const u32 source_function,
762 			   struct media_entity *source,
763 			   const u16 source_pad,
764 			   const u32 sink_function,
765 			   struct media_entity *sink,
766 			   const u16 sink_pad,
767 			   u32 flags,
768 			   const bool allow_both_undefined);
769 
770 void __media_entity_remove_links(struct media_entity *entity);
771 
772 /**
773  * media_entity_remove_links() - remove all links associated with an entity
774  *
775  * @entity:	pointer to &media_entity
776  *
777  * .. note::
778  *
779  *    This is called automatically when an entity is unregistered via
780  *    media_device_register_entity().
781  */
782 void media_entity_remove_links(struct media_entity *entity);
783 
784 /**
785  * __media_entity_setup_link - Configure a media link without locking
786  * @link: The link being configured
787  * @flags: Link configuration flags
788  *
789  * The bulk of link setup is handled by the two entities connected through the
790  * link. This function notifies both entities of the link configuration change.
791  *
792  * If the link is immutable or if the current and new configuration are
793  * identical, return immediately.
794  *
795  * The user is expected to hold link->source->parent->mutex. If not,
796  * media_entity_setup_link() should be used instead.
797  */
798 int __media_entity_setup_link(struct media_link *link, u32 flags);
799 
800 /**
801  * media_entity_setup_link() - changes the link flags properties in runtime
802  *
803  * @link:	pointer to &media_link
804  * @flags:	the requested new link flags
805  *
806  * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
807  * to enable/disable a link. Links marked with the
808  * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
809  *
810  * When a link is enabled or disabled, the media framework calls the
811  * link_setup operation for the two entities at the source and sink of the
812  * link, in that order. If the second link_setup call fails, another
813  * link_setup call is made on the first entity to restore the original link
814  * flags.
815  *
816  * Media device drivers can be notified of link setup operations by setting the
817  * &media_device.link_notify pointer to a callback function. If provided, the
818  * notification callback will be called before enabling and after disabling
819  * links.
820  *
821  * Entity drivers must implement the link_setup operation if any of their links
822  * is non-immutable. The operation must either configure the hardware or store
823  * the configuration information to be applied later.
824  *
825  * Link configuration must not have any side effect on other links. If an
826  * enabled link at a sink pad prevents another link at the same pad from
827  * being enabled, the link_setup operation must return %-EBUSY and can't
828  * implicitly disable the first enabled link.
829  *
830  * .. note::
831  *
832  *    The valid values of the flags for the link is the same as described
833  *    on media_create_pad_link(), for pad to pad links or the same as described
834  *    on media_create_intf_link(), for interface to entity links.
835  */
836 int media_entity_setup_link(struct media_link *link, u32 flags);
837 
838 /**
839  * media_entity_find_link - Find a link between two pads
840  * @source: Source pad
841  * @sink: Sink pad
842  *
843  * Return: returns a pointer to the link between the two entities. If no
844  * such link exists, return %NULL.
845  */
846 struct media_link *media_entity_find_link(struct media_pad *source,
847 		struct media_pad *sink);
848 
849 /**
850  * media_entity_remote_pad - Find the pad at the remote end of a link
851  * @pad: Pad at the local end of the link
852  *
853  * Search for a remote pad connected to the given pad by iterating over all
854  * links originating or terminating at that pad until an enabled link is found.
855  *
856  * Return: returns a pointer to the pad at the remote end of the first found
857  * enabled link, or %NULL if no enabled link has been found.
858  */
859 struct media_pad *media_entity_remote_pad(const struct media_pad *pad);
860 
861 /**
862  * media_entity_get_fwnode_pad - Get pad number from fwnode
863  *
864  * @entity: The entity
865  * @fwnode: Pointer to the fwnode_handle which should be used to find the pad
866  * @direction_flags: Expected direction of the pad, as defined in
867  *		     :ref:`include/uapi/linux/media.h <media_header>`
868  *		     (seek for ``MEDIA_PAD_FL_*``)
869  *
870  * This function can be used to resolve the media pad number from
871  * a fwnode. This is useful for devices which use more complex
872  * mappings of media pads.
873  *
874  * If the entity does not implement the get_fwnode_pad() operation
875  * then this function searches the entity for the first pad that
876  * matches the @direction_flags.
877  *
878  * Return: returns the pad number on success or a negative error code.
879  */
880 int media_entity_get_fwnode_pad(struct media_entity *entity,
881 				struct fwnode_handle *fwnode,
882 				unsigned long direction_flags);
883 
884 /**
885  * media_graph_walk_init - Allocate resources used by graph walk.
886  *
887  * @graph: Media graph structure that will be used to walk the graph
888  * @mdev: Pointer to the &media_device that contains the object
889  *
890  * The caller is required to hold the media_device graph_mutex during the graph
891  * walk until the graph state is released.
892  *
893  * Returns zero on success or a negative error code otherwise.
894  */
895 __must_check int media_graph_walk_init(
896 	struct media_graph *graph, struct media_device *mdev);
897 
898 /**
899  * media_graph_walk_cleanup - Release resources used by graph walk.
900  *
901  * @graph: Media graph structure that will be used to walk the graph
902  */
903 void media_graph_walk_cleanup(struct media_graph *graph);
904 
905 /**
906  * media_graph_walk_start - Start walking the media graph at a
907  *	given entity
908  *
909  * @graph: Media graph structure that will be used to walk the graph
910  * @entity: Starting entity
911  *
912  * Before using this function, media_graph_walk_init() must be
913  * used to allocate resources used for walking the graph. This
914  * function initializes the graph traversal structure to walk the
915  * entities graph starting at the given entity. The traversal
916  * structure must not be modified by the caller during graph
917  * traversal. After the graph walk, the resources must be released
918  * using media_graph_walk_cleanup().
919  */
920 void media_graph_walk_start(struct media_graph *graph,
921 			    struct media_entity *entity);
922 
923 /**
924  * media_graph_walk_next - Get the next entity in the graph
925  * @graph: Media graph structure
926  *
927  * Perform a depth-first traversal of the given media entities graph.
928  *
929  * The graph structure must have been previously initialized with a call to
930  * media_graph_walk_start().
931  *
932  * Return: returns the next entity in the graph or %NULL if the whole graph
933  * have been traversed.
934  */
935 struct media_entity *media_graph_walk_next(struct media_graph *graph);
936 
937 /**
938  * media_pipeline_start - Mark a pipeline as streaming
939  * @entity: Starting entity
940  * @pipe: Media pipeline to be assigned to all entities in the pipeline.
941  *
942  * Mark all entities connected to a given entity through enabled links, either
943  * directly or indirectly, as streaming. The given pipeline object is assigned
944  * to every entity in the pipeline and stored in the media_entity pipe field.
945  *
946  * Calls to this function can be nested, in which case the same number of
947  * media_pipeline_stop() calls will be required to stop streaming. The
948  * pipeline pointer must be identical for all nested calls to
949  * media_pipeline_start().
950  */
951 __must_check int media_pipeline_start(struct media_entity *entity,
952 				      struct media_pipeline *pipe);
953 /**
954  * __media_pipeline_start - Mark a pipeline as streaming
955  *
956  * @entity: Starting entity
957  * @pipe: Media pipeline to be assigned to all entities in the pipeline.
958  *
959  * ..note:: This is the non-locking version of media_pipeline_start()
960  */
961 __must_check int __media_pipeline_start(struct media_entity *entity,
962 					struct media_pipeline *pipe);
963 
964 /**
965  * media_pipeline_stop - Mark a pipeline as not streaming
966  * @entity: Starting entity
967  *
968  * Mark all entities connected to a given entity through enabled links, either
969  * directly or indirectly, as not streaming. The media_entity pipe field is
970  * reset to %NULL.
971  *
972  * If multiple calls to media_pipeline_start() have been made, the same
973  * number of calls to this function are required to mark the pipeline as not
974  * streaming.
975  */
976 void media_pipeline_stop(struct media_entity *entity);
977 
978 /**
979  * __media_pipeline_stop - Mark a pipeline as not streaming
980  *
981  * @entity: Starting entity
982  *
983  * .. note:: This is the non-locking version of media_pipeline_stop()
984  */
985 void __media_pipeline_stop(struct media_entity *entity);
986 
987 /**
988  * media_devnode_create() - creates and initializes a device node interface
989  *
990  * @mdev:	pointer to struct &media_device
991  * @type:	type of the interface, as given by
992  *		:ref:`include/uapi/linux/media.h <media_header>`
993  *		( seek for ``MEDIA_INTF_T_*``) macros.
994  * @flags:	Interface flags, as defined in
995  *		:ref:`include/uapi/linux/media.h <media_header>`
996  *		( seek for ``MEDIA_INTF_FL_*``)
997  * @major:	Device node major number.
998  * @minor:	Device node minor number.
999  *
1000  * Return: if succeeded, returns a pointer to the newly allocated
1001  *	&media_intf_devnode pointer.
1002  *
1003  * .. note::
1004  *
1005  *    Currently, no flags for &media_interface is defined.
1006  */
1007 struct media_intf_devnode *
1008 __must_check media_devnode_create(struct media_device *mdev,
1009 				  u32 type, u32 flags,
1010 				  u32 major, u32 minor);
1011 /**
1012  * media_devnode_remove() - removes a device node interface
1013  *
1014  * @devnode:	pointer to &media_intf_devnode to be freed.
1015  *
1016  * When a device node interface is removed, all links to it are automatically
1017  * removed.
1018  */
1019 void media_devnode_remove(struct media_intf_devnode *devnode);
1020 struct media_link *
1021 
1022 /**
1023  * media_create_intf_link() - creates a link between an entity and an interface
1024  *
1025  * @entity:	pointer to %media_entity
1026  * @intf:	pointer to %media_interface
1027  * @flags:	Link flags, as defined in
1028  *		:ref:`include/uapi/linux/media.h <media_header>`
1029  *		( seek for ``MEDIA_LNK_FL_*``)
1030  *
1031  *
1032  * Valid values for flags:
1033  *
1034  * %MEDIA_LNK_FL_ENABLED
1035  *   Indicates that the interface is connected to the entity hardware.
1036  *   That's the default value for interfaces. An interface may be disabled if
1037  *   the hardware is busy due to the usage of some other interface that it is
1038  *   currently controlling the hardware.
1039  *
1040  *   A typical example is an hybrid TV device that handle only one type of
1041  *   stream on a given time. So, when the digital TV is streaming,
1042  *   the V4L2 interfaces won't be enabled, as such device is not able to
1043  *   also stream analog TV or radio.
1044  *
1045  * .. note::
1046  *
1047  *    Before calling this function, media_devnode_create() should be called for
1048  *    the interface and media_device_register_entity() should be called for the
1049  *    interface that will be part of the link.
1050  */
1051 __must_check media_create_intf_link(struct media_entity *entity,
1052 				    struct media_interface *intf,
1053 				    u32 flags);
1054 /**
1055  * __media_remove_intf_link() - remove a single interface link
1056  *
1057  * @link:	pointer to &media_link.
1058  *
1059  * .. note:: This is an unlocked version of media_remove_intf_link()
1060  */
1061 void __media_remove_intf_link(struct media_link *link);
1062 
1063 /**
1064  * media_remove_intf_link() - remove a single interface link
1065  *
1066  * @link:	pointer to &media_link.
1067  *
1068  * .. note:: Prefer to use this one, instead of __media_remove_intf_link()
1069  */
1070 void media_remove_intf_link(struct media_link *link);
1071 
1072 /**
1073  * __media_remove_intf_links() - remove all links associated with an interface
1074  *
1075  * @intf:	pointer to &media_interface
1076  *
1077  * .. note:: This is an unlocked version of media_remove_intf_links().
1078  */
1079 void __media_remove_intf_links(struct media_interface *intf);
1080 
1081 /**
1082  * media_remove_intf_links() - remove all links associated with an interface
1083  *
1084  * @intf:	pointer to &media_interface
1085  *
1086  * .. note::
1087  *
1088  *   #) This is called automatically when an entity is unregistered via
1089  *      media_device_register_entity() and by media_devnode_remove().
1090  *
1091  *   #) Prefer to use this one, instead of __media_remove_intf_links().
1092  */
1093 void media_remove_intf_links(struct media_interface *intf);
1094 
1095 /**
1096  * media_entity_call - Calls a struct media_entity_operations operation on
1097  *	an entity
1098  *
1099  * @entity: entity where the @operation will be called
1100  * @operation: type of the operation. Should be the name of a member of
1101  *	struct &media_entity_operations.
1102  *
1103  * This helper function will check if @operation is not %NULL. On such case,
1104  * it will issue a call to @operation\(@entity, @args\).
1105  */
1106 
1107 #define media_entity_call(entity, operation, args...)			\
1108 	(((entity)->ops && (entity)->ops->operation) ?			\
1109 	 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
1110 
1111 #endif
1112