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