xref: /openbmc/linux/drivers/media/mc/mc-entity.c (revision 93696d8f)
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 #include <linux/bitmap.h>
12 #include <linux/list.h>
13 #include <linux/property.h>
14 #include <linux/slab.h>
15 #include <media/media-entity.h>
16 #include <media/media-device.h>
17 
18 static inline const char *intf_type(struct media_interface *intf)
19 {
20 	switch (intf->type) {
21 	case MEDIA_INTF_T_DVB_FE:
22 		return "dvb-frontend";
23 	case MEDIA_INTF_T_DVB_DEMUX:
24 		return "dvb-demux";
25 	case MEDIA_INTF_T_DVB_DVR:
26 		return "dvb-dvr";
27 	case MEDIA_INTF_T_DVB_CA:
28 		return  "dvb-ca";
29 	case MEDIA_INTF_T_DVB_NET:
30 		return "dvb-net";
31 	case MEDIA_INTF_T_V4L_VIDEO:
32 		return "v4l-video";
33 	case MEDIA_INTF_T_V4L_VBI:
34 		return "v4l-vbi";
35 	case MEDIA_INTF_T_V4L_RADIO:
36 		return "v4l-radio";
37 	case MEDIA_INTF_T_V4L_SUBDEV:
38 		return "v4l-subdev";
39 	case MEDIA_INTF_T_V4L_SWRADIO:
40 		return "v4l-swradio";
41 	case MEDIA_INTF_T_V4L_TOUCH:
42 		return "v4l-touch";
43 	default:
44 		return "unknown-intf";
45 	}
46 };
47 
48 static inline const char *link_type_name(struct media_link *link)
49 {
50 	switch (link->flags & MEDIA_LNK_FL_LINK_TYPE) {
51 	case MEDIA_LNK_FL_DATA_LINK:
52 		return "data";
53 	case MEDIA_LNK_FL_INTERFACE_LINK:
54 		return "interface";
55 	case MEDIA_LNK_FL_ANCILLARY_LINK:
56 		return "ancillary";
57 	default:
58 		return "unknown";
59 	}
60 }
61 
62 __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum,
63 					struct media_device *mdev)
64 {
65 	int idx_max;
66 
67 	idx_max = ALIGN(mdev->entity_internal_idx_max + 1, BITS_PER_LONG);
68 	ent_enum->bmap = bitmap_zalloc(idx_max, GFP_KERNEL);
69 	if (!ent_enum->bmap)
70 		return -ENOMEM;
71 
72 	ent_enum->idx_max = idx_max;
73 
74 	return 0;
75 }
76 EXPORT_SYMBOL_GPL(media_entity_enum_init);
77 
78 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
79 {
80 	bitmap_free(ent_enum->bmap);
81 }
82 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
83 
84 /**
85  *  dev_dbg_obj - Prints in debug mode a change on some object
86  *
87  * @event_name:	Name of the event to report. Could be __func__
88  * @gobj:	Pointer to the object
89  *
90  * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
91  * won't produce any code.
92  */
93 static void dev_dbg_obj(const char *event_name,  struct media_gobj *gobj)
94 {
95 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
96 	switch (media_type(gobj)) {
97 	case MEDIA_GRAPH_ENTITY:
98 		dev_dbg(gobj->mdev->dev,
99 			"%s id %u: entity '%s'\n",
100 			event_name, media_id(gobj),
101 			gobj_to_entity(gobj)->name);
102 		break;
103 	case MEDIA_GRAPH_LINK:
104 	{
105 		struct media_link *link = gobj_to_link(gobj);
106 
107 		dev_dbg(gobj->mdev->dev,
108 			"%s id %u: %s link id %u ==> id %u\n",
109 			event_name, media_id(gobj), link_type_name(link),
110 			media_id(link->gobj0),
111 			media_id(link->gobj1));
112 		break;
113 	}
114 	case MEDIA_GRAPH_PAD:
115 	{
116 		struct media_pad *pad = gobj_to_pad(gobj);
117 
118 		dev_dbg(gobj->mdev->dev,
119 			"%s id %u: %s%spad '%s':%d\n",
120 			event_name, media_id(gobj),
121 			pad->flags & MEDIA_PAD_FL_SINK   ? "sink " : "",
122 			pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
123 			pad->entity->name, pad->index);
124 		break;
125 	}
126 	case MEDIA_GRAPH_INTF_DEVNODE:
127 	{
128 		struct media_interface *intf = gobj_to_intf(gobj);
129 		struct media_intf_devnode *devnode = intf_to_devnode(intf);
130 
131 		dev_dbg(gobj->mdev->dev,
132 			"%s id %u: intf_devnode %s - major: %d, minor: %d\n",
133 			event_name, media_id(gobj),
134 			intf_type(intf),
135 			devnode->major, devnode->minor);
136 		break;
137 	}
138 	}
139 #endif
140 }
141 
142 void media_gobj_create(struct media_device *mdev,
143 			   enum media_gobj_type type,
144 			   struct media_gobj *gobj)
145 {
146 	BUG_ON(!mdev);
147 
148 	gobj->mdev = mdev;
149 
150 	/* Create a per-type unique object ID */
151 	gobj->id = media_gobj_gen_id(type, ++mdev->id);
152 
153 	switch (type) {
154 	case MEDIA_GRAPH_ENTITY:
155 		list_add_tail(&gobj->list, &mdev->entities);
156 		break;
157 	case MEDIA_GRAPH_PAD:
158 		list_add_tail(&gobj->list, &mdev->pads);
159 		break;
160 	case MEDIA_GRAPH_LINK:
161 		list_add_tail(&gobj->list, &mdev->links);
162 		break;
163 	case MEDIA_GRAPH_INTF_DEVNODE:
164 		list_add_tail(&gobj->list, &mdev->interfaces);
165 		break;
166 	}
167 
168 	mdev->topology_version++;
169 
170 	dev_dbg_obj(__func__, gobj);
171 }
172 
173 void media_gobj_destroy(struct media_gobj *gobj)
174 {
175 	/* Do nothing if the object is not linked. */
176 	if (gobj->mdev == NULL)
177 		return;
178 
179 	dev_dbg_obj(__func__, gobj);
180 
181 	gobj->mdev->topology_version++;
182 
183 	/* Remove the object from mdev list */
184 	list_del(&gobj->list);
185 
186 	gobj->mdev = NULL;
187 }
188 
189 /*
190  * TODO: Get rid of this.
191  */
192 #define MEDIA_ENTITY_MAX_PADS		512
193 
194 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
195 			   struct media_pad *pads)
196 {
197 	struct media_device *mdev = entity->graph_obj.mdev;
198 	struct media_pad *iter;
199 	unsigned int i = 0;
200 
201 	if (num_pads >= MEDIA_ENTITY_MAX_PADS)
202 		return -E2BIG;
203 
204 	entity->num_pads = num_pads;
205 	entity->pads = pads;
206 
207 	if (mdev)
208 		mutex_lock(&mdev->graph_mutex);
209 
210 	media_entity_for_each_pad(entity, iter) {
211 		iter->entity = entity;
212 		iter->index = i++;
213 		if (mdev)
214 			media_gobj_create(mdev, MEDIA_GRAPH_PAD,
215 					  &iter->graph_obj);
216 	}
217 
218 	if (mdev)
219 		mutex_unlock(&mdev->graph_mutex);
220 
221 	return 0;
222 }
223 EXPORT_SYMBOL_GPL(media_entity_pads_init);
224 
225 /* -----------------------------------------------------------------------------
226  * Graph traversal
227  */
228 
229 /**
230  * media_entity_has_pad_interdep - Check interdependency between two pads
231  *
232  * @entity: The entity
233  * @pad0: The first pad index
234  * @pad1: The second pad index
235  *
236  * This function checks the interdependency inside the entity between @pad0
237  * and @pad1. If two pads are interdependent they are part of the same pipeline
238  * and enabling one of the pads means that the other pad will become "locked"
239  * and doesn't allow configuration changes.
240  *
241  * This function uses the &media_entity_operations.has_pad_interdep() operation
242  * to check the dependency inside the entity between @pad0 and @pad1. If the
243  * has_pad_interdep operation is not implemented, all pads of the entity are
244  * considered to be interdependent.
245  *
246  * One of @pad0 and @pad1 must be a sink pad and the other one a source pad.
247  * The function returns false if both pads are sinks or sources.
248  *
249  * The caller must hold entity->graph_obj.mdev->mutex.
250  *
251  * Return: true if the pads are connected internally and false otherwise.
252  */
253 static bool media_entity_has_pad_interdep(struct media_entity *entity,
254 					  unsigned int pad0, unsigned int pad1)
255 {
256 	if (pad0 >= entity->num_pads || pad1 >= entity->num_pads)
257 		return false;
258 
259 	if (entity->pads[pad0].flags & entity->pads[pad1].flags &
260 	    (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE))
261 		return false;
262 
263 	if (!entity->ops || !entity->ops->has_pad_interdep)
264 		return true;
265 
266 	return entity->ops->has_pad_interdep(entity, pad0, pad1);
267 }
268 
269 static struct media_entity *
270 media_entity_other(struct media_entity *entity, struct media_link *link)
271 {
272 	if (link->source->entity == entity)
273 		return link->sink->entity;
274 	else
275 		return link->source->entity;
276 }
277 
278 /* push an entity to traversal stack */
279 static void stack_push(struct media_graph *graph,
280 		       struct media_entity *entity)
281 {
282 	if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
283 		WARN_ON(1);
284 		return;
285 	}
286 	graph->top++;
287 	graph->stack[graph->top].link = entity->links.next;
288 	graph->stack[graph->top].entity = entity;
289 }
290 
291 static struct media_entity *stack_pop(struct media_graph *graph)
292 {
293 	struct media_entity *entity;
294 
295 	entity = graph->stack[graph->top].entity;
296 	graph->top--;
297 
298 	return entity;
299 }
300 
301 #define link_top(en)	((en)->stack[(en)->top].link)
302 #define stack_top(en)	((en)->stack[(en)->top].entity)
303 
304 /**
305  * media_graph_walk_init - Allocate resources for graph walk
306  * @graph: Media graph structure that will be used to walk the graph
307  * @mdev: Media device
308  *
309  * Reserve resources for graph walk in media device's current
310  * state. The memory must be released using
311  * media_graph_walk_cleanup().
312  *
313  * Returns error on failure, zero on success.
314  */
315 __must_check int media_graph_walk_init(
316 	struct media_graph *graph, struct media_device *mdev)
317 {
318 	return media_entity_enum_init(&graph->ent_enum, mdev);
319 }
320 EXPORT_SYMBOL_GPL(media_graph_walk_init);
321 
322 /**
323  * media_graph_walk_cleanup - Release resources related to graph walking
324  * @graph: Media graph structure that was used to walk the graph
325  */
326 void media_graph_walk_cleanup(struct media_graph *graph)
327 {
328 	media_entity_enum_cleanup(&graph->ent_enum);
329 }
330 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
331 
332 void media_graph_walk_start(struct media_graph *graph,
333 			    struct media_entity *entity)
334 {
335 	media_entity_enum_zero(&graph->ent_enum);
336 	media_entity_enum_set(&graph->ent_enum, entity);
337 
338 	graph->top = 0;
339 	graph->stack[graph->top].entity = NULL;
340 	stack_push(graph, entity);
341 	dev_dbg(entity->graph_obj.mdev->dev,
342 		"begin graph walk at '%s'\n", entity->name);
343 }
344 EXPORT_SYMBOL_GPL(media_graph_walk_start);
345 
346 static void media_graph_walk_iter(struct media_graph *graph)
347 {
348 	struct media_entity *entity = stack_top(graph);
349 	struct media_link *link;
350 	struct media_entity *next;
351 
352 	link = list_entry(link_top(graph), typeof(*link), list);
353 
354 	/* If the link is not a data link, don't follow it */
355 	if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) {
356 		link_top(graph) = link_top(graph)->next;
357 		return;
358 	}
359 
360 	/* The link is not enabled so we do not follow. */
361 	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
362 		link_top(graph) = link_top(graph)->next;
363 		dev_dbg(entity->graph_obj.mdev->dev,
364 			"walk: skipping disabled link '%s':%u -> '%s':%u\n",
365 			link->source->entity->name, link->source->index,
366 			link->sink->entity->name, link->sink->index);
367 		return;
368 	}
369 
370 	/* Get the entity at the other end of the link. */
371 	next = media_entity_other(entity, link);
372 
373 	/* Has the entity already been visited? */
374 	if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
375 		link_top(graph) = link_top(graph)->next;
376 		dev_dbg(entity->graph_obj.mdev->dev,
377 			"walk: skipping entity '%s' (already seen)\n",
378 			next->name);
379 		return;
380 	}
381 
382 	/* Push the new entity to stack and start over. */
383 	link_top(graph) = link_top(graph)->next;
384 	stack_push(graph, next);
385 	dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
386 		next->name);
387 	lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex);
388 }
389 
390 struct media_entity *media_graph_walk_next(struct media_graph *graph)
391 {
392 	struct media_entity *entity;
393 
394 	if (stack_top(graph) == NULL)
395 		return NULL;
396 
397 	/*
398 	 * Depth first search. Push entity to stack and continue from
399 	 * top of the stack until no more entities on the level can be
400 	 * found.
401 	 */
402 	while (link_top(graph) != &stack_top(graph)->links)
403 		media_graph_walk_iter(graph);
404 
405 	entity = stack_pop(graph);
406 	dev_dbg(entity->graph_obj.mdev->dev,
407 		"walk: returning entity '%s'\n", entity->name);
408 
409 	return entity;
410 }
411 EXPORT_SYMBOL_GPL(media_graph_walk_next);
412 
413 /* -----------------------------------------------------------------------------
414  * Pipeline management
415  */
416 
417 /*
418  * The pipeline traversal stack stores pads that are reached during graph
419  * traversal, with a list of links to be visited to continue the traversal.
420  * When a new pad is reached, an entry is pushed on the top of the stack and
421  * points to the incoming pad and the first link of the entity.
422  *
423  * To find further pads in the pipeline, the traversal algorithm follows
424  * internal pad dependencies in the entity, and then links in the graph. It
425  * does so by iterating over all links of the entity, and following enabled
426  * links that originate from a pad that is internally connected to the incoming
427  * pad, as reported by the media_entity_has_pad_interdep() function.
428  */
429 
430 /**
431  * struct media_pipeline_walk_entry - Entry in the pipeline traversal stack
432  *
433  * @pad: The media pad being visited
434  * @links: Links left to be visited
435  */
436 struct media_pipeline_walk_entry {
437 	struct media_pad *pad;
438 	struct list_head *links;
439 };
440 
441 /**
442  * struct media_pipeline_walk - State used by the media pipeline traversal
443  *				algorithm
444  *
445  * @mdev: The media device
446  * @stack: Depth-first search stack
447  * @stack.size: Number of allocated entries in @stack.entries
448  * @stack.top: Index of the top stack entry (-1 if the stack is empty)
449  * @stack.entries: Stack entries
450  */
451 struct media_pipeline_walk {
452 	struct media_device *mdev;
453 
454 	struct {
455 		unsigned int size;
456 		int top;
457 		struct media_pipeline_walk_entry *entries;
458 	} stack;
459 };
460 
461 #define MEDIA_PIPELINE_STACK_GROW_STEP		16
462 
463 static struct media_pipeline_walk_entry *
464 media_pipeline_walk_top(struct media_pipeline_walk *walk)
465 {
466 	return &walk->stack.entries[walk->stack.top];
467 }
468 
469 static bool media_pipeline_walk_empty(struct media_pipeline_walk *walk)
470 {
471 	return walk->stack.top == -1;
472 }
473 
474 /* Increase the stack size by MEDIA_PIPELINE_STACK_GROW_STEP elements. */
475 static int media_pipeline_walk_resize(struct media_pipeline_walk *walk)
476 {
477 	struct media_pipeline_walk_entry *entries;
478 	unsigned int new_size;
479 
480 	/* Safety check, to avoid stack overflows in case of bugs. */
481 	if (walk->stack.size >= 256)
482 		return -E2BIG;
483 
484 	new_size = walk->stack.size + MEDIA_PIPELINE_STACK_GROW_STEP;
485 
486 	entries = krealloc(walk->stack.entries,
487 			   new_size * sizeof(*walk->stack.entries),
488 			   GFP_KERNEL);
489 	if (!entries)
490 		return -ENOMEM;
491 
492 	walk->stack.entries = entries;
493 	walk->stack.size = new_size;
494 
495 	return 0;
496 }
497 
498 /* Push a new entry on the stack. */
499 static int media_pipeline_walk_push(struct media_pipeline_walk *walk,
500 				    struct media_pad *pad)
501 {
502 	struct media_pipeline_walk_entry *entry;
503 	int ret;
504 
505 	if (walk->stack.top + 1 >= walk->stack.size) {
506 		ret = media_pipeline_walk_resize(walk);
507 		if (ret)
508 			return ret;
509 	}
510 
511 	walk->stack.top++;
512 	entry = media_pipeline_walk_top(walk);
513 	entry->pad = pad;
514 	entry->links = pad->entity->links.next;
515 
516 	dev_dbg(walk->mdev->dev,
517 		"media pipeline: pushed entry %u: '%s':%u\n",
518 		walk->stack.top, pad->entity->name, pad->index);
519 
520 	return 0;
521 }
522 
523 /*
524  * Move the top entry link cursor to the next link. If all links of the entry
525  * have been visited, pop the entry itself. Return true if the entry has been
526  * popped.
527  */
528 static bool media_pipeline_walk_pop(struct media_pipeline_walk *walk)
529 {
530 	struct media_pipeline_walk_entry *entry;
531 
532 	if (WARN_ON(walk->stack.top < 0))
533 		return false;
534 
535 	entry = media_pipeline_walk_top(walk);
536 
537 	if (entry->links->next == &entry->pad->entity->links) {
538 		dev_dbg(walk->mdev->dev,
539 			"media pipeline: entry %u has no more links, popping\n",
540 			walk->stack.top);
541 
542 		walk->stack.top--;
543 		return true;
544 	}
545 
546 	entry->links = entry->links->next;
547 
548 	dev_dbg(walk->mdev->dev,
549 		"media pipeline: moved entry %u to next link\n",
550 		walk->stack.top);
551 
552 	return false;
553 }
554 
555 /* Free all memory allocated while walking the pipeline. */
556 static void media_pipeline_walk_destroy(struct media_pipeline_walk *walk)
557 {
558 	kfree(walk->stack.entries);
559 }
560 
561 /* Add a pad to the pipeline and push it to the stack. */
562 static int media_pipeline_add_pad(struct media_pipeline *pipe,
563 				  struct media_pipeline_walk *walk,
564 				  struct media_pad *pad)
565 {
566 	struct media_pipeline_pad *ppad;
567 
568 	list_for_each_entry(ppad, &pipe->pads, list) {
569 		if (ppad->pad == pad) {
570 			dev_dbg(pad->graph_obj.mdev->dev,
571 				"media pipeline: already contains pad '%s':%u\n",
572 				pad->entity->name, pad->index);
573 			return 0;
574 		}
575 	}
576 
577 	ppad = kzalloc(sizeof(*ppad), GFP_KERNEL);
578 	if (!ppad)
579 		return -ENOMEM;
580 
581 	ppad->pipe = pipe;
582 	ppad->pad = pad;
583 
584 	list_add_tail(&ppad->list, &pipe->pads);
585 
586 	dev_dbg(pad->graph_obj.mdev->dev,
587 		"media pipeline: added pad '%s':%u\n",
588 		pad->entity->name, pad->index);
589 
590 	return media_pipeline_walk_push(walk, pad);
591 }
592 
593 /* Explore the next link of the entity at the top of the stack. */
594 static int media_pipeline_explore_next_link(struct media_pipeline *pipe,
595 					    struct media_pipeline_walk *walk)
596 {
597 	struct media_pipeline_walk_entry *entry = media_pipeline_walk_top(walk);
598 	struct media_pad *origin;
599 	struct media_link *link;
600 	struct media_pad *local;
601 	struct media_pad *remote;
602 	bool last_link;
603 	int ret;
604 
605 	origin = entry->pad;
606 	link = list_entry(entry->links, typeof(*link), list);
607 	last_link = media_pipeline_walk_pop(walk);
608 
609 	if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) {
610 		dev_dbg(walk->mdev->dev,
611 			"media pipeline: skipping link (not data-link)\n");
612 		return 0;
613 	}
614 
615 	dev_dbg(walk->mdev->dev,
616 		"media pipeline: exploring link '%s':%u -> '%s':%u\n",
617 		link->source->entity->name, link->source->index,
618 		link->sink->entity->name, link->sink->index);
619 
620 	/* Get the local pad and remote pad. */
621 	if (link->source->entity == origin->entity) {
622 		local = link->source;
623 		remote = link->sink;
624 	} else {
625 		local = link->sink;
626 		remote = link->source;
627 	}
628 
629 	/*
630 	 * Skip links that originate from a different pad than the incoming pad
631 	 * that is not connected internally in the entity to the incoming pad.
632 	 */
633 	if (origin != local &&
634 	    !media_entity_has_pad_interdep(origin->entity, origin->index,
635 					   local->index)) {
636 		dev_dbg(walk->mdev->dev,
637 			"media pipeline: skipping link (no route)\n");
638 		goto done;
639 	}
640 
641 	/*
642 	 * Add the local pad of the link to the pipeline and push it to the
643 	 * stack, if not already present.
644 	 */
645 	ret = media_pipeline_add_pad(pipe, walk, local);
646 	if (ret)
647 		return ret;
648 
649 	/* Similarly, add the remote pad, but only if the link is enabled. */
650 	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
651 		dev_dbg(walk->mdev->dev,
652 			"media pipeline: skipping link (disabled)\n");
653 		goto done;
654 	}
655 
656 	ret = media_pipeline_add_pad(pipe, walk, remote);
657 	if (ret)
658 		return ret;
659 
660 done:
661 	/*
662 	 * If we're done iterating over links, iterate over pads of the entity.
663 	 * This is necessary to discover pads that are not connected with any
664 	 * link. Those are dead ends from a pipeline exploration point of view,
665 	 * but are still part of the pipeline and need to be added to enable
666 	 * proper validation.
667 	 */
668 	if (!last_link)
669 		return 0;
670 
671 	dev_dbg(walk->mdev->dev,
672 		"media pipeline: adding unconnected pads of '%s'\n",
673 		local->entity->name);
674 
675 	media_entity_for_each_pad(origin->entity, local) {
676 		/*
677 		 * Skip the origin pad (already handled), pad that have links
678 		 * (already discovered through iterating over links) and pads
679 		 * not internally connected.
680 		 */
681 		if (origin == local || !local->num_links ||
682 		    !media_entity_has_pad_interdep(origin->entity, origin->index,
683 						   local->index))
684 			continue;
685 
686 		ret = media_pipeline_add_pad(pipe, walk, local);
687 		if (ret)
688 			return ret;
689 	}
690 
691 	return 0;
692 }
693 
694 static void media_pipeline_cleanup(struct media_pipeline *pipe)
695 {
696 	while (!list_empty(&pipe->pads)) {
697 		struct media_pipeline_pad *ppad;
698 
699 		ppad = list_first_entry(&pipe->pads, typeof(*ppad), list);
700 		list_del(&ppad->list);
701 		kfree(ppad);
702 	}
703 }
704 
705 static int media_pipeline_populate(struct media_pipeline *pipe,
706 				   struct media_pad *pad)
707 {
708 	struct media_pipeline_walk walk = { };
709 	struct media_pipeline_pad *ppad;
710 	int ret;
711 
712 	/*
713 	 * Populate the media pipeline by walking the media graph, starting
714 	 * from @pad.
715 	 */
716 	INIT_LIST_HEAD(&pipe->pads);
717 	pipe->mdev = pad->graph_obj.mdev;
718 
719 	walk.mdev = pipe->mdev;
720 	walk.stack.top = -1;
721 	ret = media_pipeline_add_pad(pipe, &walk, pad);
722 	if (ret)
723 		goto done;
724 
725 	/*
726 	 * Use a depth-first search algorithm: as long as the stack is not
727 	 * empty, explore the next link of the top entry. The
728 	 * media_pipeline_explore_next_link() function will either move to the
729 	 * next link, pop the entry if fully visited, or add new entries on
730 	 * top.
731 	 */
732 	while (!media_pipeline_walk_empty(&walk)) {
733 		ret = media_pipeline_explore_next_link(pipe, &walk);
734 		if (ret)
735 			goto done;
736 	}
737 
738 	dev_dbg(pad->graph_obj.mdev->dev,
739 		"media pipeline populated, found pads:\n");
740 
741 	list_for_each_entry(ppad, &pipe->pads, list)
742 		dev_dbg(pad->graph_obj.mdev->dev, "- '%s':%u\n",
743 			ppad->pad->entity->name, ppad->pad->index);
744 
745 	WARN_ON(walk.stack.top != -1);
746 
747 	ret = 0;
748 
749 done:
750 	media_pipeline_walk_destroy(&walk);
751 
752 	if (ret)
753 		media_pipeline_cleanup(pipe);
754 
755 	return ret;
756 }
757 
758 __must_check int __media_pipeline_start(struct media_pad *pad,
759 					struct media_pipeline *pipe)
760 {
761 	struct media_device *mdev = pad->graph_obj.mdev;
762 	struct media_pipeline_pad *err_ppad;
763 	struct media_pipeline_pad *ppad;
764 	int ret;
765 
766 	lockdep_assert_held(&mdev->graph_mutex);
767 
768 	/*
769 	 * If the pad is already part of a pipeline, that pipeline must be the
770 	 * same as the pipe given to media_pipeline_start().
771 	 */
772 	if (WARN_ON(pad->pipe && pad->pipe != pipe))
773 		return -EINVAL;
774 
775 	/*
776 	 * If the pipeline has already been started, it is guaranteed to be
777 	 * valid, so just increase the start count.
778 	 */
779 	if (pipe->start_count) {
780 		pipe->start_count++;
781 		return 0;
782 	}
783 
784 	/*
785 	 * Populate the pipeline. This populates the media_pipeline pads list
786 	 * with media_pipeline_pad instances for each pad found during graph
787 	 * walk.
788 	 */
789 	ret = media_pipeline_populate(pipe, pad);
790 	if (ret)
791 		return ret;
792 
793 	/*
794 	 * Now that all the pads in the pipeline have been gathered, perform
795 	 * the validation steps.
796 	 */
797 
798 	list_for_each_entry(ppad, &pipe->pads, list) {
799 		struct media_pad *pad = ppad->pad;
800 		struct media_entity *entity = pad->entity;
801 		bool has_enabled_link = false;
802 		struct media_link *link;
803 
804 		dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name,
805 			pad->index);
806 
807 		/*
808 		 * 1. Ensure that the pad doesn't already belong to a different
809 		 * pipeline.
810 		 */
811 		if (pad->pipe) {
812 			dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n",
813 				pad->entity->name, pad->index);
814 			ret = -EBUSY;
815 			goto error;
816 		}
817 
818 		/*
819 		 * 2. Validate all active links whose sink is the current pad.
820 		 * Validation of the source pads is performed in the context of
821 		 * the connected sink pad to avoid duplicating checks.
822 		 */
823 		for_each_media_entity_data_link(entity, link) {
824 			/* Skip links unrelated to the current pad. */
825 			if (link->sink != pad && link->source != pad)
826 				continue;
827 
828 			/* Record if the pad has links and enabled links. */
829 			if (link->flags & MEDIA_LNK_FL_ENABLED)
830 				has_enabled_link = true;
831 
832 			/*
833 			 * Validate the link if it's enabled and has the
834 			 * current pad as its sink.
835 			 */
836 			if (!(link->flags & MEDIA_LNK_FL_ENABLED))
837 				continue;
838 
839 			if (link->sink != pad)
840 				continue;
841 
842 			if (!entity->ops || !entity->ops->link_validate)
843 				continue;
844 
845 			ret = entity->ops->link_validate(link);
846 			if (ret) {
847 				dev_dbg(mdev->dev,
848 					"Link '%s':%u -> '%s':%u failed validation: %d\n",
849 					link->source->entity->name,
850 					link->source->index,
851 					link->sink->entity->name,
852 					link->sink->index, ret);
853 				goto error;
854 			}
855 
856 			dev_dbg(mdev->dev,
857 				"Link '%s':%u -> '%s':%u is valid\n",
858 				link->source->entity->name,
859 				link->source->index,
860 				link->sink->entity->name,
861 				link->sink->index);
862 		}
863 
864 		/*
865 		 * 3. If the pad has the MEDIA_PAD_FL_MUST_CONNECT flag set,
866 		 * ensure that it has either no link or an enabled link.
867 		 */
868 		if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) &&
869 		    !has_enabled_link) {
870 			dev_dbg(mdev->dev,
871 				"Pad '%s':%u must be connected by an enabled link\n",
872 				pad->entity->name, pad->index);
873 			ret = -ENOLINK;
874 			goto error;
875 		}
876 
877 		/* Validation passed, store the pipe pointer in the pad. */
878 		pad->pipe = pipe;
879 	}
880 
881 	pipe->start_count++;
882 
883 	return 0;
884 
885 error:
886 	/*
887 	 * Link validation on graph failed. We revert what we did and
888 	 * return the error.
889 	 */
890 
891 	list_for_each_entry(err_ppad, &pipe->pads, list) {
892 		if (err_ppad == ppad)
893 			break;
894 
895 		err_ppad->pad->pipe = NULL;
896 	}
897 
898 	media_pipeline_cleanup(pipe);
899 
900 	return ret;
901 }
902 EXPORT_SYMBOL_GPL(__media_pipeline_start);
903 
904 __must_check int media_pipeline_start(struct media_pad *pad,
905 				      struct media_pipeline *pipe)
906 {
907 	struct media_device *mdev = pad->graph_obj.mdev;
908 	int ret;
909 
910 	mutex_lock(&mdev->graph_mutex);
911 	ret = __media_pipeline_start(pad, pipe);
912 	mutex_unlock(&mdev->graph_mutex);
913 	return ret;
914 }
915 EXPORT_SYMBOL_GPL(media_pipeline_start);
916 
917 void __media_pipeline_stop(struct media_pad *pad)
918 {
919 	struct media_pipeline *pipe = pad->pipe;
920 	struct media_pipeline_pad *ppad;
921 
922 	/*
923 	 * If the following check fails, the driver has performed an
924 	 * unbalanced call to media_pipeline_stop()
925 	 */
926 	if (WARN_ON(!pipe))
927 		return;
928 
929 	if (--pipe->start_count)
930 		return;
931 
932 	list_for_each_entry(ppad, &pipe->pads, list)
933 		ppad->pad->pipe = NULL;
934 
935 	media_pipeline_cleanup(pipe);
936 
937 	if (pipe->allocated)
938 		kfree(pipe);
939 }
940 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
941 
942 void media_pipeline_stop(struct media_pad *pad)
943 {
944 	struct media_device *mdev = pad->graph_obj.mdev;
945 
946 	mutex_lock(&mdev->graph_mutex);
947 	__media_pipeline_stop(pad);
948 	mutex_unlock(&mdev->graph_mutex);
949 }
950 EXPORT_SYMBOL_GPL(media_pipeline_stop);
951 
952 __must_check int media_pipeline_alloc_start(struct media_pad *pad)
953 {
954 	struct media_device *mdev = pad->graph_obj.mdev;
955 	struct media_pipeline *new_pipe = NULL;
956 	struct media_pipeline *pipe;
957 	int ret;
958 
959 	mutex_lock(&mdev->graph_mutex);
960 
961 	/*
962 	 * Is the pad already part of a pipeline? If not, we need to allocate
963 	 * a pipe.
964 	 */
965 	pipe = media_pad_pipeline(pad);
966 	if (!pipe) {
967 		new_pipe = kzalloc(sizeof(*new_pipe), GFP_KERNEL);
968 		if (!new_pipe) {
969 			ret = -ENOMEM;
970 			goto out;
971 		}
972 
973 		pipe = new_pipe;
974 		pipe->allocated = true;
975 	}
976 
977 	ret = __media_pipeline_start(pad, pipe);
978 	if (ret)
979 		kfree(new_pipe);
980 
981 out:
982 	mutex_unlock(&mdev->graph_mutex);
983 
984 	return ret;
985 }
986 EXPORT_SYMBOL_GPL(media_pipeline_alloc_start);
987 
988 struct media_pad *
989 __media_pipeline_pad_iter_next(struct media_pipeline *pipe,
990 			       struct media_pipeline_pad_iter *iter,
991 			       struct media_pad *pad)
992 {
993 	if (!pad)
994 		iter->cursor = pipe->pads.next;
995 
996 	if (iter->cursor == &pipe->pads)
997 		return NULL;
998 
999 	pad = list_entry(iter->cursor, struct media_pipeline_pad, list)->pad;
1000 	iter->cursor = iter->cursor->next;
1001 
1002 	return pad;
1003 }
1004 EXPORT_SYMBOL_GPL(__media_pipeline_pad_iter_next);
1005 
1006 int media_pipeline_entity_iter_init(struct media_pipeline *pipe,
1007 				    struct media_pipeline_entity_iter *iter)
1008 {
1009 	return media_entity_enum_init(&iter->ent_enum, pipe->mdev);
1010 }
1011 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_init);
1012 
1013 void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter)
1014 {
1015 	media_entity_enum_cleanup(&iter->ent_enum);
1016 }
1017 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_cleanup);
1018 
1019 struct media_entity *
1020 __media_pipeline_entity_iter_next(struct media_pipeline *pipe,
1021 				  struct media_pipeline_entity_iter *iter,
1022 				  struct media_entity *entity)
1023 {
1024 	if (!entity)
1025 		iter->cursor = pipe->pads.next;
1026 
1027 	while (iter->cursor != &pipe->pads) {
1028 		struct media_pipeline_pad *ppad;
1029 		struct media_entity *entity;
1030 
1031 		ppad = list_entry(iter->cursor, struct media_pipeline_pad, list);
1032 		entity = ppad->pad->entity;
1033 		iter->cursor = iter->cursor->next;
1034 
1035 		if (!media_entity_enum_test_and_set(&iter->ent_enum, entity))
1036 			return entity;
1037 	}
1038 
1039 	return NULL;
1040 }
1041 EXPORT_SYMBOL_GPL(__media_pipeline_entity_iter_next);
1042 
1043 /* -----------------------------------------------------------------------------
1044  * Links management
1045  */
1046 
1047 static struct media_link *media_add_link(struct list_head *head)
1048 {
1049 	struct media_link *link;
1050 
1051 	link = kzalloc(sizeof(*link), GFP_KERNEL);
1052 	if (link == NULL)
1053 		return NULL;
1054 
1055 	list_add_tail(&link->list, head);
1056 
1057 	return link;
1058 }
1059 
1060 static void __media_entity_remove_link(struct media_entity *entity,
1061 				       struct media_link *link)
1062 {
1063 	struct media_link *rlink, *tmp;
1064 	struct media_entity *remote;
1065 
1066 	/* Remove the reverse links for a data link. */
1067 	if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
1068 		link->source->num_links--;
1069 		link->sink->num_links--;
1070 
1071 		if (link->source->entity == entity)
1072 			remote = link->sink->entity;
1073 		else
1074 			remote = link->source->entity;
1075 
1076 		list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
1077 			if (rlink != link->reverse)
1078 				continue;
1079 
1080 			if (link->source->entity == entity)
1081 				remote->num_backlinks--;
1082 
1083 			/* Remove the remote link */
1084 			list_del(&rlink->list);
1085 			media_gobj_destroy(&rlink->graph_obj);
1086 			kfree(rlink);
1087 
1088 			if (--remote->num_links == 0)
1089 				break;
1090 		}
1091 	}
1092 
1093 	list_del(&link->list);
1094 	media_gobj_destroy(&link->graph_obj);
1095 	kfree(link);
1096 }
1097 
1098 int media_get_pad_index(struct media_entity *entity, u32 pad_type,
1099 			enum media_pad_signal_type sig_type)
1100 {
1101 	unsigned int i;
1102 
1103 	if (!entity)
1104 		return -EINVAL;
1105 
1106 	for (i = 0; i < entity->num_pads; i++) {
1107 		if ((entity->pads[i].flags &
1108 		     (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) != pad_type)
1109 			continue;
1110 
1111 		if (entity->pads[i].sig_type == sig_type)
1112 			return i;
1113 	}
1114 	return -EINVAL;
1115 }
1116 EXPORT_SYMBOL_GPL(media_get_pad_index);
1117 
1118 int
1119 media_create_pad_link(struct media_entity *source, u16 source_pad,
1120 			 struct media_entity *sink, u16 sink_pad, u32 flags)
1121 {
1122 	struct media_link *link;
1123 	struct media_link *backlink;
1124 
1125 	if (flags & MEDIA_LNK_FL_LINK_TYPE)
1126 		return -EINVAL;
1127 
1128 	flags |= MEDIA_LNK_FL_DATA_LINK;
1129 
1130 	if (WARN_ON(!source || !sink) ||
1131 	    WARN_ON(source_pad >= source->num_pads) ||
1132 	    WARN_ON(sink_pad >= sink->num_pads))
1133 		return -EINVAL;
1134 	if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
1135 		return -EINVAL;
1136 	if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
1137 		return -EINVAL;
1138 
1139 	link = media_add_link(&source->links);
1140 	if (link == NULL)
1141 		return -ENOMEM;
1142 
1143 	link->source = &source->pads[source_pad];
1144 	link->sink = &sink->pads[sink_pad];
1145 	link->flags = flags;
1146 
1147 	/* Initialize graph object embedded at the new link */
1148 	media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
1149 			&link->graph_obj);
1150 
1151 	/* Create the backlink. Backlinks are used to help graph traversal and
1152 	 * are not reported to userspace.
1153 	 */
1154 	backlink = media_add_link(&sink->links);
1155 	if (backlink == NULL) {
1156 		__media_entity_remove_link(source, link);
1157 		return -ENOMEM;
1158 	}
1159 
1160 	backlink->source = &source->pads[source_pad];
1161 	backlink->sink = &sink->pads[sink_pad];
1162 	backlink->flags = flags;
1163 	backlink->is_backlink = true;
1164 
1165 	/* Initialize graph object embedded at the new link */
1166 	media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
1167 			&backlink->graph_obj);
1168 
1169 	link->reverse = backlink;
1170 	backlink->reverse = link;
1171 
1172 	sink->num_backlinks++;
1173 	sink->num_links++;
1174 	source->num_links++;
1175 
1176 	link->source->num_links++;
1177 	link->sink->num_links++;
1178 
1179 	return 0;
1180 }
1181 EXPORT_SYMBOL_GPL(media_create_pad_link);
1182 
1183 int media_create_pad_links(const struct media_device *mdev,
1184 			   const u32 source_function,
1185 			   struct media_entity *source,
1186 			   const u16 source_pad,
1187 			   const u32 sink_function,
1188 			   struct media_entity *sink,
1189 			   const u16 sink_pad,
1190 			   u32 flags,
1191 			   const bool allow_both_undefined)
1192 {
1193 	struct media_entity *entity;
1194 	unsigned function;
1195 	int ret;
1196 
1197 	/* Trivial case: 1:1 relation */
1198 	if (source && sink)
1199 		return media_create_pad_link(source, source_pad,
1200 					     sink, sink_pad, flags);
1201 
1202 	/* Worse case scenario: n:n relation */
1203 	if (!source && !sink) {
1204 		if (!allow_both_undefined)
1205 			return 0;
1206 		media_device_for_each_entity(source, mdev) {
1207 			if (source->function != source_function)
1208 				continue;
1209 			media_device_for_each_entity(sink, mdev) {
1210 				if (sink->function != sink_function)
1211 					continue;
1212 				ret = media_create_pad_link(source, source_pad,
1213 							    sink, sink_pad,
1214 							    flags);
1215 				if (ret)
1216 					return ret;
1217 				flags &= ~(MEDIA_LNK_FL_ENABLED |
1218 					   MEDIA_LNK_FL_IMMUTABLE);
1219 			}
1220 		}
1221 		return 0;
1222 	}
1223 
1224 	/* Handle 1:n and n:1 cases */
1225 	if (source)
1226 		function = sink_function;
1227 	else
1228 		function = source_function;
1229 
1230 	media_device_for_each_entity(entity, mdev) {
1231 		if (entity->function != function)
1232 			continue;
1233 
1234 		if (source)
1235 			ret = media_create_pad_link(source, source_pad,
1236 						    entity, sink_pad, flags);
1237 		else
1238 			ret = media_create_pad_link(entity, source_pad,
1239 						    sink, sink_pad, flags);
1240 		if (ret)
1241 			return ret;
1242 		flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1243 	}
1244 	return 0;
1245 }
1246 EXPORT_SYMBOL_GPL(media_create_pad_links);
1247 
1248 void __media_entity_remove_links(struct media_entity *entity)
1249 {
1250 	struct media_link *link, *tmp;
1251 
1252 	list_for_each_entry_safe(link, tmp, &entity->links, list)
1253 		__media_entity_remove_link(entity, link);
1254 
1255 	entity->num_links = 0;
1256 	entity->num_backlinks = 0;
1257 }
1258 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
1259 
1260 void media_entity_remove_links(struct media_entity *entity)
1261 {
1262 	struct media_device *mdev = entity->graph_obj.mdev;
1263 
1264 	/* Do nothing if the entity is not registered. */
1265 	if (mdev == NULL)
1266 		return;
1267 
1268 	mutex_lock(&mdev->graph_mutex);
1269 	__media_entity_remove_links(entity);
1270 	mutex_unlock(&mdev->graph_mutex);
1271 }
1272 EXPORT_SYMBOL_GPL(media_entity_remove_links);
1273 
1274 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
1275 {
1276 	int ret;
1277 
1278 	/* Notify both entities. */
1279 	ret = media_entity_call(link->source->entity, link_setup,
1280 				link->source, link->sink, flags);
1281 	if (ret < 0 && ret != -ENOIOCTLCMD)
1282 		return ret;
1283 
1284 	ret = media_entity_call(link->sink->entity, link_setup,
1285 				link->sink, link->source, flags);
1286 	if (ret < 0 && ret != -ENOIOCTLCMD) {
1287 		media_entity_call(link->source->entity, link_setup,
1288 				  link->source, link->sink, link->flags);
1289 		return ret;
1290 	}
1291 
1292 	link->flags = flags;
1293 	link->reverse->flags = link->flags;
1294 
1295 	return 0;
1296 }
1297 
1298 int __media_entity_setup_link(struct media_link *link, u32 flags)
1299 {
1300 	const u32 mask = MEDIA_LNK_FL_ENABLED;
1301 	struct media_device *mdev;
1302 	struct media_pad *source, *sink;
1303 	int ret = -EBUSY;
1304 
1305 	if (link == NULL)
1306 		return -EINVAL;
1307 
1308 	/* The non-modifiable link flags must not be modified. */
1309 	if ((link->flags & ~mask) != (flags & ~mask))
1310 		return -EINVAL;
1311 
1312 	if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
1313 		return link->flags == flags ? 0 : -EINVAL;
1314 
1315 	if (link->flags == flags)
1316 		return 0;
1317 
1318 	source = link->source;
1319 	sink = link->sink;
1320 
1321 	if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
1322 	    (media_pad_is_streaming(source) || media_pad_is_streaming(sink)))
1323 		return -EBUSY;
1324 
1325 	mdev = source->graph_obj.mdev;
1326 
1327 	if (mdev->ops && mdev->ops->link_notify) {
1328 		ret = mdev->ops->link_notify(link, flags,
1329 					     MEDIA_DEV_NOTIFY_PRE_LINK_CH);
1330 		if (ret < 0)
1331 			return ret;
1332 	}
1333 
1334 	ret = __media_entity_setup_link_notify(link, flags);
1335 
1336 	if (mdev->ops && mdev->ops->link_notify)
1337 		mdev->ops->link_notify(link, flags,
1338 				       MEDIA_DEV_NOTIFY_POST_LINK_CH);
1339 
1340 	return ret;
1341 }
1342 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
1343 
1344 int media_entity_setup_link(struct media_link *link, u32 flags)
1345 {
1346 	int ret;
1347 
1348 	mutex_lock(&link->graph_obj.mdev->graph_mutex);
1349 	ret = __media_entity_setup_link(link, flags);
1350 	mutex_unlock(&link->graph_obj.mdev->graph_mutex);
1351 
1352 	return ret;
1353 }
1354 EXPORT_SYMBOL_GPL(media_entity_setup_link);
1355 
1356 struct media_link *
1357 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
1358 {
1359 	struct media_link *link;
1360 
1361 	for_each_media_entity_data_link(source->entity, link) {
1362 		if (link->source->entity == source->entity &&
1363 		    link->source->index == source->index &&
1364 		    link->sink->entity == sink->entity &&
1365 		    link->sink->index == sink->index)
1366 			return link;
1367 	}
1368 
1369 	return NULL;
1370 }
1371 EXPORT_SYMBOL_GPL(media_entity_find_link);
1372 
1373 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad)
1374 {
1375 	struct media_link *link;
1376 
1377 	for_each_media_entity_data_link(pad->entity, link) {
1378 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1379 			continue;
1380 
1381 		if (link->source == pad)
1382 			return link->sink;
1383 
1384 		if (link->sink == pad)
1385 			return link->source;
1386 	}
1387 
1388 	return NULL;
1389 
1390 }
1391 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first);
1392 
1393 struct media_pad *
1394 media_entity_remote_pad_unique(const struct media_entity *entity,
1395 			       unsigned int type)
1396 {
1397 	struct media_pad *pad = NULL;
1398 	struct media_link *link;
1399 
1400 	list_for_each_entry(link, &entity->links, list) {
1401 		struct media_pad *local_pad;
1402 		struct media_pad *remote_pad;
1403 
1404 		if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) !=
1405 		     MEDIA_LNK_FL_DATA_LINK) ||
1406 		    !(link->flags & MEDIA_LNK_FL_ENABLED))
1407 			continue;
1408 
1409 		if (type == MEDIA_PAD_FL_SOURCE) {
1410 			local_pad = link->sink;
1411 			remote_pad = link->source;
1412 		} else {
1413 			local_pad = link->source;
1414 			remote_pad = link->sink;
1415 		}
1416 
1417 		if (local_pad->entity == entity) {
1418 			if (pad)
1419 				return ERR_PTR(-ENOTUNIQ);
1420 
1421 			pad = remote_pad;
1422 		}
1423 	}
1424 
1425 	if (!pad)
1426 		return ERR_PTR(-ENOLINK);
1427 
1428 	return pad;
1429 }
1430 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique);
1431 
1432 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad)
1433 {
1434 	struct media_pad *found_pad = NULL;
1435 	struct media_link *link;
1436 
1437 	list_for_each_entry(link, &pad->entity->links, list) {
1438 		struct media_pad *remote_pad;
1439 
1440 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1441 			continue;
1442 
1443 		if (link->sink == pad)
1444 			remote_pad = link->source;
1445 		else if (link->source == pad)
1446 			remote_pad = link->sink;
1447 		else
1448 			continue;
1449 
1450 		if (found_pad)
1451 			return ERR_PTR(-ENOTUNIQ);
1452 
1453 		found_pad = remote_pad;
1454 	}
1455 
1456 	if (!found_pad)
1457 		return ERR_PTR(-ENOLINK);
1458 
1459 	return found_pad;
1460 }
1461 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique);
1462 
1463 int media_entity_get_fwnode_pad(struct media_entity *entity,
1464 				const struct fwnode_handle *fwnode,
1465 				unsigned long direction_flags)
1466 {
1467 	struct fwnode_endpoint endpoint;
1468 	unsigned int i;
1469 	int ret;
1470 
1471 	if (!entity->ops || !entity->ops->get_fwnode_pad) {
1472 		for (i = 0; i < entity->num_pads; i++) {
1473 			if (entity->pads[i].flags & direction_flags)
1474 				return i;
1475 		}
1476 
1477 		return -ENXIO;
1478 	}
1479 
1480 	ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
1481 	if (ret)
1482 		return ret;
1483 
1484 	ret = entity->ops->get_fwnode_pad(entity, &endpoint);
1485 	if (ret < 0)
1486 		return ret;
1487 
1488 	if (ret >= entity->num_pads)
1489 		return -ENXIO;
1490 
1491 	if (!(entity->pads[ret].flags & direction_flags))
1492 		return -ENXIO;
1493 
1494 	return ret;
1495 }
1496 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
1497 
1498 struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
1499 {
1500 	struct media_pad *pad;
1501 
1502 	media_entity_for_each_pad(entity, pad) {
1503 		if (pad->pipe)
1504 			return pad->pipe;
1505 	}
1506 
1507 	return NULL;
1508 }
1509 EXPORT_SYMBOL_GPL(media_entity_pipeline);
1510 
1511 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
1512 {
1513 	return pad->pipe;
1514 }
1515 EXPORT_SYMBOL_GPL(media_pad_pipeline);
1516 
1517 static void media_interface_init(struct media_device *mdev,
1518 				 struct media_interface *intf,
1519 				 u32 gobj_type,
1520 				 u32 intf_type, u32 flags)
1521 {
1522 	intf->type = intf_type;
1523 	intf->flags = flags;
1524 	INIT_LIST_HEAD(&intf->links);
1525 
1526 	media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1527 }
1528 
1529 /* Functions related to the media interface via device nodes */
1530 
1531 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
1532 						u32 type, u32 flags,
1533 						u32 major, u32 minor)
1534 {
1535 	struct media_intf_devnode *devnode;
1536 
1537 	devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
1538 	if (!devnode)
1539 		return NULL;
1540 
1541 	devnode->major = major;
1542 	devnode->minor = minor;
1543 
1544 	media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
1545 			     type, flags);
1546 
1547 	return devnode;
1548 }
1549 EXPORT_SYMBOL_GPL(media_devnode_create);
1550 
1551 void media_devnode_remove(struct media_intf_devnode *devnode)
1552 {
1553 	media_remove_intf_links(&devnode->intf);
1554 	media_gobj_destroy(&devnode->intf.graph_obj);
1555 	kfree(devnode);
1556 }
1557 EXPORT_SYMBOL_GPL(media_devnode_remove);
1558 
1559 struct media_link *media_create_intf_link(struct media_entity *entity,
1560 					    struct media_interface *intf,
1561 					    u32 flags)
1562 {
1563 	struct media_link *link;
1564 
1565 	link = media_add_link(&intf->links);
1566 	if (link == NULL)
1567 		return NULL;
1568 
1569 	link->intf = intf;
1570 	link->entity = entity;
1571 	link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
1572 
1573 	/* Initialize graph object embedded at the new link */
1574 	media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1575 			&link->graph_obj);
1576 
1577 	return link;
1578 }
1579 EXPORT_SYMBOL_GPL(media_create_intf_link);
1580 
1581 void __media_remove_intf_link(struct media_link *link)
1582 {
1583 	list_del(&link->list);
1584 	media_gobj_destroy(&link->graph_obj);
1585 	kfree(link);
1586 }
1587 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1588 
1589 void media_remove_intf_link(struct media_link *link)
1590 {
1591 	struct media_device *mdev = link->graph_obj.mdev;
1592 
1593 	/* Do nothing if the intf is not registered. */
1594 	if (mdev == NULL)
1595 		return;
1596 
1597 	mutex_lock(&mdev->graph_mutex);
1598 	__media_remove_intf_link(link);
1599 	mutex_unlock(&mdev->graph_mutex);
1600 }
1601 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1602 
1603 void __media_remove_intf_links(struct media_interface *intf)
1604 {
1605 	struct media_link *link, *tmp;
1606 
1607 	list_for_each_entry_safe(link, tmp, &intf->links, list)
1608 		__media_remove_intf_link(link);
1609 
1610 }
1611 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1612 
1613 void media_remove_intf_links(struct media_interface *intf)
1614 {
1615 	struct media_device *mdev = intf->graph_obj.mdev;
1616 
1617 	/* Do nothing if the intf is not registered. */
1618 	if (mdev == NULL)
1619 		return;
1620 
1621 	mutex_lock(&mdev->graph_mutex);
1622 	__media_remove_intf_links(intf);
1623 	mutex_unlock(&mdev->graph_mutex);
1624 }
1625 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1626 
1627 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1628 					       struct media_entity *ancillary)
1629 {
1630 	struct media_link *link;
1631 
1632 	link = media_add_link(&primary->links);
1633 	if (!link)
1634 		return ERR_PTR(-ENOMEM);
1635 
1636 	link->gobj0 = &primary->graph_obj;
1637 	link->gobj1 = &ancillary->graph_obj;
1638 	link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1639 		      MEDIA_LNK_FL_ANCILLARY_LINK;
1640 
1641 	/* Initialize graph object embedded in the new link */
1642 	media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1643 			  &link->graph_obj);
1644 
1645 	return link;
1646 }
1647 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
1648 
1649 struct media_link *__media_entity_next_link(struct media_entity *entity,
1650 					    struct media_link *link,
1651 					    unsigned long link_type)
1652 {
1653 	link = link ? list_next_entry(link, list)
1654 		    : list_first_entry(&entity->links, typeof(*link), list);
1655 
1656 	list_for_each_entry_from(link, &entity->links, list)
1657 		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
1658 			return link;
1659 
1660 	return NULL;
1661 }
1662 EXPORT_SYMBOL_GPL(__media_entity_next_link);
1663