xref: /openbmc/linux/drivers/media/mc/mc-entity.c (revision 997a5310)
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 	dev_dbg(walk->mdev->dev,
610 		"media pipeline: exploring link '%s':%u -> '%s':%u\n",
611 		link->source->entity->name, link->source->index,
612 		link->sink->entity->name, link->sink->index);
613 
614 	/* Get the local pad and remote pad. */
615 	if (link->source->entity == origin->entity) {
616 		local = link->source;
617 		remote = link->sink;
618 	} else {
619 		local = link->sink;
620 		remote = link->source;
621 	}
622 
623 	/*
624 	 * Skip links that originate from a different pad than the incoming pad
625 	 * that is not connected internally in the entity to the incoming pad.
626 	 */
627 	if (origin != local &&
628 	    !media_entity_has_pad_interdep(origin->entity, origin->index,
629 					   local->index)) {
630 		dev_dbg(walk->mdev->dev,
631 			"media pipeline: skipping link (no route)\n");
632 		goto done;
633 	}
634 
635 	/*
636 	 * Add the local pad of the link to the pipeline and push it to the
637 	 * stack, if not already present.
638 	 */
639 	ret = media_pipeline_add_pad(pipe, walk, local);
640 	if (ret)
641 		return ret;
642 
643 	/* Similarly, add the remote pad, but only if the link is enabled. */
644 	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
645 		dev_dbg(walk->mdev->dev,
646 			"media pipeline: skipping link (disabled)\n");
647 		goto done;
648 	}
649 
650 	ret = media_pipeline_add_pad(pipe, walk, remote);
651 	if (ret)
652 		return ret;
653 
654 done:
655 	/*
656 	 * If we're done iterating over links, iterate over pads of the entity.
657 	 * This is necessary to discover pads that are not connected with any
658 	 * link. Those are dead ends from a pipeline exploration point of view,
659 	 * but are still part of the pipeline and need to be added to enable
660 	 * proper validation.
661 	 */
662 	if (!last_link)
663 		return 0;
664 
665 	dev_dbg(walk->mdev->dev,
666 		"media pipeline: adding unconnected pads of '%s'\n",
667 		local->entity->name);
668 
669 	media_entity_for_each_pad(origin->entity, local) {
670 		/*
671 		 * Skip the origin pad (already handled), pad that have links
672 		 * (already discovered through iterating over links) and pads
673 		 * not internally connected.
674 		 */
675 		if (origin == local || !local->num_links ||
676 		    !media_entity_has_pad_interdep(origin->entity, origin->index,
677 						   local->index))
678 			continue;
679 
680 		ret = media_pipeline_add_pad(pipe, walk, local);
681 		if (ret)
682 			return ret;
683 	}
684 
685 	return 0;
686 }
687 
688 static void media_pipeline_cleanup(struct media_pipeline *pipe)
689 {
690 	while (!list_empty(&pipe->pads)) {
691 		struct media_pipeline_pad *ppad;
692 
693 		ppad = list_first_entry(&pipe->pads, typeof(*ppad), list);
694 		list_del(&ppad->list);
695 		kfree(ppad);
696 	}
697 }
698 
699 static int media_pipeline_populate(struct media_pipeline *pipe,
700 				   struct media_pad *pad)
701 {
702 	struct media_pipeline_walk walk = { };
703 	struct media_pipeline_pad *ppad;
704 	int ret;
705 
706 	/*
707 	 * Populate the media pipeline by walking the media graph, starting
708 	 * from @pad.
709 	 */
710 	INIT_LIST_HEAD(&pipe->pads);
711 	pipe->mdev = pad->graph_obj.mdev;
712 
713 	walk.mdev = pipe->mdev;
714 	walk.stack.top = -1;
715 	ret = media_pipeline_add_pad(pipe, &walk, pad);
716 	if (ret)
717 		goto done;
718 
719 	/*
720 	 * Use a depth-first search algorithm: as long as the stack is not
721 	 * empty, explore the next link of the top entry. The
722 	 * media_pipeline_explore_next_link() function will either move to the
723 	 * next link, pop the entry if fully visited, or add new entries on
724 	 * top.
725 	 */
726 	while (!media_pipeline_walk_empty(&walk)) {
727 		ret = media_pipeline_explore_next_link(pipe, &walk);
728 		if (ret)
729 			goto done;
730 	}
731 
732 	dev_dbg(pad->graph_obj.mdev->dev,
733 		"media pipeline populated, found pads:\n");
734 
735 	list_for_each_entry(ppad, &pipe->pads, list)
736 		dev_dbg(pad->graph_obj.mdev->dev, "- '%s':%u\n",
737 			ppad->pad->entity->name, ppad->pad->index);
738 
739 	WARN_ON(walk.stack.top != -1);
740 
741 	ret = 0;
742 
743 done:
744 	media_pipeline_walk_destroy(&walk);
745 
746 	if (ret)
747 		media_pipeline_cleanup(pipe);
748 
749 	return ret;
750 }
751 
752 __must_check int __media_pipeline_start(struct media_pad *pad,
753 					struct media_pipeline *pipe)
754 {
755 	struct media_device *mdev = pad->graph_obj.mdev;
756 	struct media_pipeline_pad *err_ppad;
757 	struct media_pipeline_pad *ppad;
758 	int ret;
759 
760 	lockdep_assert_held(&mdev->graph_mutex);
761 
762 	/*
763 	 * If the pad is already part of a pipeline, that pipeline must be the
764 	 * same as the pipe given to media_pipeline_start().
765 	 */
766 	if (WARN_ON(pad->pipe && pad->pipe != pipe))
767 		return -EINVAL;
768 
769 	/*
770 	 * If the pipeline has already been started, it is guaranteed to be
771 	 * valid, so just increase the start count.
772 	 */
773 	if (pipe->start_count) {
774 		pipe->start_count++;
775 		return 0;
776 	}
777 
778 	/*
779 	 * Populate the pipeline. This populates the media_pipeline pads list
780 	 * with media_pipeline_pad instances for each pad found during graph
781 	 * walk.
782 	 */
783 	ret = media_pipeline_populate(pipe, pad);
784 	if (ret)
785 		return ret;
786 
787 	/*
788 	 * Now that all the pads in the pipeline have been gathered, perform
789 	 * the validation steps.
790 	 */
791 
792 	list_for_each_entry(ppad, &pipe->pads, list) {
793 		struct media_pad *pad = ppad->pad;
794 		struct media_entity *entity = pad->entity;
795 		bool has_enabled_link = false;
796 		struct media_link *link;
797 
798 		dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name,
799 			pad->index);
800 
801 		/*
802 		 * 1. Ensure that the pad doesn't already belong to a different
803 		 * pipeline.
804 		 */
805 		if (pad->pipe) {
806 			dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n",
807 				pad->entity->name, pad->index);
808 			ret = -EBUSY;
809 			goto error;
810 		}
811 
812 		/*
813 		 * 2. Validate all active links whose sink is the current pad.
814 		 * Validation of the source pads is performed in the context of
815 		 * the connected sink pad to avoid duplicating checks.
816 		 */
817 		for_each_media_entity_data_link(entity, link) {
818 			/* Skip links unrelated to the current pad. */
819 			if (link->sink != pad && link->source != pad)
820 				continue;
821 
822 			/* Record if the pad has links and enabled links. */
823 			if (link->flags & MEDIA_LNK_FL_ENABLED)
824 				has_enabled_link = true;
825 
826 			/*
827 			 * Validate the link if it's enabled and has the
828 			 * current pad as its sink.
829 			 */
830 			if (!(link->flags & MEDIA_LNK_FL_ENABLED))
831 				continue;
832 
833 			if (link->sink != pad)
834 				continue;
835 
836 			if (!entity->ops || !entity->ops->link_validate)
837 				continue;
838 
839 			ret = entity->ops->link_validate(link);
840 			if (ret) {
841 				dev_dbg(mdev->dev,
842 					"Link '%s':%u -> '%s':%u failed validation: %d\n",
843 					link->source->entity->name,
844 					link->source->index,
845 					link->sink->entity->name,
846 					link->sink->index, ret);
847 				goto error;
848 			}
849 
850 			dev_dbg(mdev->dev,
851 				"Link '%s':%u -> '%s':%u is valid\n",
852 				link->source->entity->name,
853 				link->source->index,
854 				link->sink->entity->name,
855 				link->sink->index);
856 		}
857 
858 		/*
859 		 * 3. If the pad has the MEDIA_PAD_FL_MUST_CONNECT flag set,
860 		 * ensure that it has either no link or an enabled link.
861 		 */
862 		if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) &&
863 		    !has_enabled_link) {
864 			dev_dbg(mdev->dev,
865 				"Pad '%s':%u must be connected by an enabled link\n",
866 				pad->entity->name, pad->index);
867 			ret = -ENOLINK;
868 			goto error;
869 		}
870 
871 		/* Validation passed, store the pipe pointer in the pad. */
872 		pad->pipe = pipe;
873 	}
874 
875 	pipe->start_count++;
876 
877 	return 0;
878 
879 error:
880 	/*
881 	 * Link validation on graph failed. We revert what we did and
882 	 * return the error.
883 	 */
884 
885 	list_for_each_entry(err_ppad, &pipe->pads, list) {
886 		if (err_ppad == ppad)
887 			break;
888 
889 		err_ppad->pad->pipe = NULL;
890 	}
891 
892 	media_pipeline_cleanup(pipe);
893 
894 	return ret;
895 }
896 EXPORT_SYMBOL_GPL(__media_pipeline_start);
897 
898 __must_check int media_pipeline_start(struct media_pad *pad,
899 				      struct media_pipeline *pipe)
900 {
901 	struct media_device *mdev = pad->graph_obj.mdev;
902 	int ret;
903 
904 	mutex_lock(&mdev->graph_mutex);
905 	ret = __media_pipeline_start(pad, pipe);
906 	mutex_unlock(&mdev->graph_mutex);
907 	return ret;
908 }
909 EXPORT_SYMBOL_GPL(media_pipeline_start);
910 
911 void __media_pipeline_stop(struct media_pad *pad)
912 {
913 	struct media_pipeline *pipe = pad->pipe;
914 	struct media_pipeline_pad *ppad;
915 
916 	/*
917 	 * If the following check fails, the driver has performed an
918 	 * unbalanced call to media_pipeline_stop()
919 	 */
920 	if (WARN_ON(!pipe))
921 		return;
922 
923 	if (--pipe->start_count)
924 		return;
925 
926 	list_for_each_entry(ppad, &pipe->pads, list)
927 		ppad->pad->pipe = NULL;
928 
929 	media_pipeline_cleanup(pipe);
930 
931 	if (pipe->allocated)
932 		kfree(pipe);
933 }
934 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
935 
936 void media_pipeline_stop(struct media_pad *pad)
937 {
938 	struct media_device *mdev = pad->graph_obj.mdev;
939 
940 	mutex_lock(&mdev->graph_mutex);
941 	__media_pipeline_stop(pad);
942 	mutex_unlock(&mdev->graph_mutex);
943 }
944 EXPORT_SYMBOL_GPL(media_pipeline_stop);
945 
946 __must_check int media_pipeline_alloc_start(struct media_pad *pad)
947 {
948 	struct media_device *mdev = pad->graph_obj.mdev;
949 	struct media_pipeline *new_pipe = NULL;
950 	struct media_pipeline *pipe;
951 	int ret;
952 
953 	mutex_lock(&mdev->graph_mutex);
954 
955 	/*
956 	 * Is the pad already part of a pipeline? If not, we need to allocate
957 	 * a pipe.
958 	 */
959 	pipe = media_pad_pipeline(pad);
960 	if (!pipe) {
961 		new_pipe = kzalloc(sizeof(*new_pipe), GFP_KERNEL);
962 		if (!new_pipe) {
963 			ret = -ENOMEM;
964 			goto out;
965 		}
966 
967 		pipe = new_pipe;
968 		pipe->allocated = true;
969 	}
970 
971 	ret = __media_pipeline_start(pad, pipe);
972 	if (ret)
973 		kfree(new_pipe);
974 
975 out:
976 	mutex_unlock(&mdev->graph_mutex);
977 
978 	return ret;
979 }
980 EXPORT_SYMBOL_GPL(media_pipeline_alloc_start);
981 
982 struct media_pad *
983 __media_pipeline_pad_iter_next(struct media_pipeline *pipe,
984 			       struct media_pipeline_pad_iter *iter,
985 			       struct media_pad *pad)
986 {
987 	if (!pad)
988 		iter->cursor = pipe->pads.next;
989 
990 	if (iter->cursor == &pipe->pads)
991 		return NULL;
992 
993 	pad = list_entry(iter->cursor, struct media_pipeline_pad, list)->pad;
994 	iter->cursor = iter->cursor->next;
995 
996 	return pad;
997 }
998 EXPORT_SYMBOL_GPL(__media_pipeline_pad_iter_next);
999 
1000 int media_pipeline_entity_iter_init(struct media_pipeline *pipe,
1001 				    struct media_pipeline_entity_iter *iter)
1002 {
1003 	return media_entity_enum_init(&iter->ent_enum, pipe->mdev);
1004 }
1005 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_init);
1006 
1007 void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter)
1008 {
1009 	media_entity_enum_cleanup(&iter->ent_enum);
1010 }
1011 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_cleanup);
1012 
1013 struct media_entity *
1014 __media_pipeline_entity_iter_next(struct media_pipeline *pipe,
1015 				  struct media_pipeline_entity_iter *iter,
1016 				  struct media_entity *entity)
1017 {
1018 	if (!entity)
1019 		iter->cursor = pipe->pads.next;
1020 
1021 	while (iter->cursor != &pipe->pads) {
1022 		struct media_pipeline_pad *ppad;
1023 		struct media_entity *entity;
1024 
1025 		ppad = list_entry(iter->cursor, struct media_pipeline_pad, list);
1026 		entity = ppad->pad->entity;
1027 		iter->cursor = iter->cursor->next;
1028 
1029 		if (!media_entity_enum_test_and_set(&iter->ent_enum, entity))
1030 			return entity;
1031 	}
1032 
1033 	return NULL;
1034 }
1035 EXPORT_SYMBOL_GPL(__media_pipeline_entity_iter_next);
1036 
1037 /* -----------------------------------------------------------------------------
1038  * Links management
1039  */
1040 
1041 static struct media_link *media_add_link(struct list_head *head)
1042 {
1043 	struct media_link *link;
1044 
1045 	link = kzalloc(sizeof(*link), GFP_KERNEL);
1046 	if (link == NULL)
1047 		return NULL;
1048 
1049 	list_add_tail(&link->list, head);
1050 
1051 	return link;
1052 }
1053 
1054 static void __media_entity_remove_link(struct media_entity *entity,
1055 				       struct media_link *link)
1056 {
1057 	struct media_link *rlink, *tmp;
1058 	struct media_entity *remote;
1059 
1060 	/* Remove the reverse links for a data link. */
1061 	if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
1062 		link->source->num_links--;
1063 		link->sink->num_links--;
1064 
1065 		if (link->source->entity == entity)
1066 			remote = link->sink->entity;
1067 		else
1068 			remote = link->source->entity;
1069 
1070 		list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
1071 			if (rlink != link->reverse)
1072 				continue;
1073 
1074 			if (link->source->entity == entity)
1075 				remote->num_backlinks--;
1076 
1077 			/* Remove the remote link */
1078 			list_del(&rlink->list);
1079 			media_gobj_destroy(&rlink->graph_obj);
1080 			kfree(rlink);
1081 
1082 			if (--remote->num_links == 0)
1083 				break;
1084 		}
1085 	}
1086 
1087 	list_del(&link->list);
1088 	media_gobj_destroy(&link->graph_obj);
1089 	kfree(link);
1090 }
1091 
1092 int media_get_pad_index(struct media_entity *entity, u32 pad_type,
1093 			enum media_pad_signal_type sig_type)
1094 {
1095 	unsigned int i;
1096 
1097 	if (!entity)
1098 		return -EINVAL;
1099 
1100 	for (i = 0; i < entity->num_pads; i++) {
1101 		if ((entity->pads[i].flags &
1102 		     (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) != pad_type)
1103 			continue;
1104 
1105 		if (entity->pads[i].sig_type == sig_type)
1106 			return i;
1107 	}
1108 	return -EINVAL;
1109 }
1110 EXPORT_SYMBOL_GPL(media_get_pad_index);
1111 
1112 int
1113 media_create_pad_link(struct media_entity *source, u16 source_pad,
1114 			 struct media_entity *sink, u16 sink_pad, u32 flags)
1115 {
1116 	struct media_link *link;
1117 	struct media_link *backlink;
1118 
1119 	if (flags & MEDIA_LNK_FL_LINK_TYPE)
1120 		return -EINVAL;
1121 
1122 	flags |= MEDIA_LNK_FL_DATA_LINK;
1123 
1124 	if (WARN_ON(!source || !sink) ||
1125 	    WARN_ON(source_pad >= source->num_pads) ||
1126 	    WARN_ON(sink_pad >= sink->num_pads))
1127 		return -EINVAL;
1128 	if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
1129 		return -EINVAL;
1130 	if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
1131 		return -EINVAL;
1132 
1133 	link = media_add_link(&source->links);
1134 	if (link == NULL)
1135 		return -ENOMEM;
1136 
1137 	link->source = &source->pads[source_pad];
1138 	link->sink = &sink->pads[sink_pad];
1139 	link->flags = flags;
1140 
1141 	/* Initialize graph object embedded at the new link */
1142 	media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
1143 			&link->graph_obj);
1144 
1145 	/* Create the backlink. Backlinks are used to help graph traversal and
1146 	 * are not reported to userspace.
1147 	 */
1148 	backlink = media_add_link(&sink->links);
1149 	if (backlink == NULL) {
1150 		__media_entity_remove_link(source, link);
1151 		return -ENOMEM;
1152 	}
1153 
1154 	backlink->source = &source->pads[source_pad];
1155 	backlink->sink = &sink->pads[sink_pad];
1156 	backlink->flags = flags;
1157 	backlink->is_backlink = true;
1158 
1159 	/* Initialize graph object embedded at the new link */
1160 	media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
1161 			&backlink->graph_obj);
1162 
1163 	link->reverse = backlink;
1164 	backlink->reverse = link;
1165 
1166 	sink->num_backlinks++;
1167 	sink->num_links++;
1168 	source->num_links++;
1169 
1170 	link->source->num_links++;
1171 	link->sink->num_links++;
1172 
1173 	return 0;
1174 }
1175 EXPORT_SYMBOL_GPL(media_create_pad_link);
1176 
1177 int media_create_pad_links(const struct media_device *mdev,
1178 			   const u32 source_function,
1179 			   struct media_entity *source,
1180 			   const u16 source_pad,
1181 			   const u32 sink_function,
1182 			   struct media_entity *sink,
1183 			   const u16 sink_pad,
1184 			   u32 flags,
1185 			   const bool allow_both_undefined)
1186 {
1187 	struct media_entity *entity;
1188 	unsigned function;
1189 	int ret;
1190 
1191 	/* Trivial case: 1:1 relation */
1192 	if (source && sink)
1193 		return media_create_pad_link(source, source_pad,
1194 					     sink, sink_pad, flags);
1195 
1196 	/* Worse case scenario: n:n relation */
1197 	if (!source && !sink) {
1198 		if (!allow_both_undefined)
1199 			return 0;
1200 		media_device_for_each_entity(source, mdev) {
1201 			if (source->function != source_function)
1202 				continue;
1203 			media_device_for_each_entity(sink, mdev) {
1204 				if (sink->function != sink_function)
1205 					continue;
1206 				ret = media_create_pad_link(source, source_pad,
1207 							    sink, sink_pad,
1208 							    flags);
1209 				if (ret)
1210 					return ret;
1211 				flags &= ~(MEDIA_LNK_FL_ENABLED |
1212 					   MEDIA_LNK_FL_IMMUTABLE);
1213 			}
1214 		}
1215 		return 0;
1216 	}
1217 
1218 	/* Handle 1:n and n:1 cases */
1219 	if (source)
1220 		function = sink_function;
1221 	else
1222 		function = source_function;
1223 
1224 	media_device_for_each_entity(entity, mdev) {
1225 		if (entity->function != function)
1226 			continue;
1227 
1228 		if (source)
1229 			ret = media_create_pad_link(source, source_pad,
1230 						    entity, sink_pad, flags);
1231 		else
1232 			ret = media_create_pad_link(entity, source_pad,
1233 						    sink, sink_pad, flags);
1234 		if (ret)
1235 			return ret;
1236 		flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1237 	}
1238 	return 0;
1239 }
1240 EXPORT_SYMBOL_GPL(media_create_pad_links);
1241 
1242 void __media_entity_remove_links(struct media_entity *entity)
1243 {
1244 	struct media_link *link, *tmp;
1245 
1246 	list_for_each_entry_safe(link, tmp, &entity->links, list)
1247 		__media_entity_remove_link(entity, link);
1248 
1249 	entity->num_links = 0;
1250 	entity->num_backlinks = 0;
1251 }
1252 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
1253 
1254 void media_entity_remove_links(struct media_entity *entity)
1255 {
1256 	struct media_device *mdev = entity->graph_obj.mdev;
1257 
1258 	/* Do nothing if the entity is not registered. */
1259 	if (mdev == NULL)
1260 		return;
1261 
1262 	mutex_lock(&mdev->graph_mutex);
1263 	__media_entity_remove_links(entity);
1264 	mutex_unlock(&mdev->graph_mutex);
1265 }
1266 EXPORT_SYMBOL_GPL(media_entity_remove_links);
1267 
1268 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
1269 {
1270 	int ret;
1271 
1272 	/* Notify both entities. */
1273 	ret = media_entity_call(link->source->entity, link_setup,
1274 				link->source, link->sink, flags);
1275 	if (ret < 0 && ret != -ENOIOCTLCMD)
1276 		return ret;
1277 
1278 	ret = media_entity_call(link->sink->entity, link_setup,
1279 				link->sink, link->source, flags);
1280 	if (ret < 0 && ret != -ENOIOCTLCMD) {
1281 		media_entity_call(link->source->entity, link_setup,
1282 				  link->source, link->sink, link->flags);
1283 		return ret;
1284 	}
1285 
1286 	link->flags = flags;
1287 	link->reverse->flags = link->flags;
1288 
1289 	return 0;
1290 }
1291 
1292 int __media_entity_setup_link(struct media_link *link, u32 flags)
1293 {
1294 	const u32 mask = MEDIA_LNK_FL_ENABLED;
1295 	struct media_device *mdev;
1296 	struct media_pad *source, *sink;
1297 	int ret = -EBUSY;
1298 
1299 	if (link == NULL)
1300 		return -EINVAL;
1301 
1302 	/* The non-modifiable link flags must not be modified. */
1303 	if ((link->flags & ~mask) != (flags & ~mask))
1304 		return -EINVAL;
1305 
1306 	if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
1307 		return link->flags == flags ? 0 : -EINVAL;
1308 
1309 	if (link->flags == flags)
1310 		return 0;
1311 
1312 	source = link->source;
1313 	sink = link->sink;
1314 
1315 	if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
1316 	    (media_pad_is_streaming(source) || media_pad_is_streaming(sink)))
1317 		return -EBUSY;
1318 
1319 	mdev = source->graph_obj.mdev;
1320 
1321 	if (mdev->ops && mdev->ops->link_notify) {
1322 		ret = mdev->ops->link_notify(link, flags,
1323 					     MEDIA_DEV_NOTIFY_PRE_LINK_CH);
1324 		if (ret < 0)
1325 			return ret;
1326 	}
1327 
1328 	ret = __media_entity_setup_link_notify(link, flags);
1329 
1330 	if (mdev->ops && mdev->ops->link_notify)
1331 		mdev->ops->link_notify(link, flags,
1332 				       MEDIA_DEV_NOTIFY_POST_LINK_CH);
1333 
1334 	return ret;
1335 }
1336 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
1337 
1338 int media_entity_setup_link(struct media_link *link, u32 flags)
1339 {
1340 	int ret;
1341 
1342 	mutex_lock(&link->graph_obj.mdev->graph_mutex);
1343 	ret = __media_entity_setup_link(link, flags);
1344 	mutex_unlock(&link->graph_obj.mdev->graph_mutex);
1345 
1346 	return ret;
1347 }
1348 EXPORT_SYMBOL_GPL(media_entity_setup_link);
1349 
1350 struct media_link *
1351 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
1352 {
1353 	struct media_link *link;
1354 
1355 	for_each_media_entity_data_link(source->entity, link) {
1356 		if (link->source->entity == source->entity &&
1357 		    link->source->index == source->index &&
1358 		    link->sink->entity == sink->entity &&
1359 		    link->sink->index == sink->index)
1360 			return link;
1361 	}
1362 
1363 	return NULL;
1364 }
1365 EXPORT_SYMBOL_GPL(media_entity_find_link);
1366 
1367 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad)
1368 {
1369 	struct media_link *link;
1370 
1371 	for_each_media_entity_data_link(pad->entity, link) {
1372 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1373 			continue;
1374 
1375 		if (link->source == pad)
1376 			return link->sink;
1377 
1378 		if (link->sink == pad)
1379 			return link->source;
1380 	}
1381 
1382 	return NULL;
1383 
1384 }
1385 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first);
1386 
1387 struct media_pad *
1388 media_entity_remote_pad_unique(const struct media_entity *entity,
1389 			       unsigned int type)
1390 {
1391 	struct media_pad *pad = NULL;
1392 	struct media_link *link;
1393 
1394 	list_for_each_entry(link, &entity->links, list) {
1395 		struct media_pad *local_pad;
1396 		struct media_pad *remote_pad;
1397 
1398 		if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) !=
1399 		     MEDIA_LNK_FL_DATA_LINK) ||
1400 		    !(link->flags & MEDIA_LNK_FL_ENABLED))
1401 			continue;
1402 
1403 		if (type == MEDIA_PAD_FL_SOURCE) {
1404 			local_pad = link->sink;
1405 			remote_pad = link->source;
1406 		} else {
1407 			local_pad = link->source;
1408 			remote_pad = link->sink;
1409 		}
1410 
1411 		if (local_pad->entity == entity) {
1412 			if (pad)
1413 				return ERR_PTR(-ENOTUNIQ);
1414 
1415 			pad = remote_pad;
1416 		}
1417 	}
1418 
1419 	if (!pad)
1420 		return ERR_PTR(-ENOLINK);
1421 
1422 	return pad;
1423 }
1424 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique);
1425 
1426 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad)
1427 {
1428 	struct media_pad *found_pad = NULL;
1429 	struct media_link *link;
1430 
1431 	list_for_each_entry(link, &pad->entity->links, list) {
1432 		struct media_pad *remote_pad;
1433 
1434 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1435 			continue;
1436 
1437 		if (link->sink == pad)
1438 			remote_pad = link->source;
1439 		else if (link->source == pad)
1440 			remote_pad = link->sink;
1441 		else
1442 			continue;
1443 
1444 		if (found_pad)
1445 			return ERR_PTR(-ENOTUNIQ);
1446 
1447 		found_pad = remote_pad;
1448 	}
1449 
1450 	if (!found_pad)
1451 		return ERR_PTR(-ENOLINK);
1452 
1453 	return found_pad;
1454 }
1455 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique);
1456 
1457 int media_entity_get_fwnode_pad(struct media_entity *entity,
1458 				const struct fwnode_handle *fwnode,
1459 				unsigned long direction_flags)
1460 {
1461 	struct fwnode_endpoint endpoint;
1462 	unsigned int i;
1463 	int ret;
1464 
1465 	if (!entity->ops || !entity->ops->get_fwnode_pad) {
1466 		for (i = 0; i < entity->num_pads; i++) {
1467 			if (entity->pads[i].flags & direction_flags)
1468 				return i;
1469 		}
1470 
1471 		return -ENXIO;
1472 	}
1473 
1474 	ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
1475 	if (ret)
1476 		return ret;
1477 
1478 	ret = entity->ops->get_fwnode_pad(entity, &endpoint);
1479 	if (ret < 0)
1480 		return ret;
1481 
1482 	if (ret >= entity->num_pads)
1483 		return -ENXIO;
1484 
1485 	if (!(entity->pads[ret].flags & direction_flags))
1486 		return -ENXIO;
1487 
1488 	return ret;
1489 }
1490 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
1491 
1492 struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
1493 {
1494 	struct media_pad *pad;
1495 
1496 	media_entity_for_each_pad(entity, pad) {
1497 		if (pad->pipe)
1498 			return pad->pipe;
1499 	}
1500 
1501 	return NULL;
1502 }
1503 EXPORT_SYMBOL_GPL(media_entity_pipeline);
1504 
1505 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
1506 {
1507 	return pad->pipe;
1508 }
1509 EXPORT_SYMBOL_GPL(media_pad_pipeline);
1510 
1511 static void media_interface_init(struct media_device *mdev,
1512 				 struct media_interface *intf,
1513 				 u32 gobj_type,
1514 				 u32 intf_type, u32 flags)
1515 {
1516 	intf->type = intf_type;
1517 	intf->flags = flags;
1518 	INIT_LIST_HEAD(&intf->links);
1519 
1520 	media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1521 }
1522 
1523 /* Functions related to the media interface via device nodes */
1524 
1525 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
1526 						u32 type, u32 flags,
1527 						u32 major, u32 minor)
1528 {
1529 	struct media_intf_devnode *devnode;
1530 
1531 	devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
1532 	if (!devnode)
1533 		return NULL;
1534 
1535 	devnode->major = major;
1536 	devnode->minor = minor;
1537 
1538 	media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
1539 			     type, flags);
1540 
1541 	return devnode;
1542 }
1543 EXPORT_SYMBOL_GPL(media_devnode_create);
1544 
1545 void media_devnode_remove(struct media_intf_devnode *devnode)
1546 {
1547 	media_remove_intf_links(&devnode->intf);
1548 	media_gobj_destroy(&devnode->intf.graph_obj);
1549 	kfree(devnode);
1550 }
1551 EXPORT_SYMBOL_GPL(media_devnode_remove);
1552 
1553 struct media_link *media_create_intf_link(struct media_entity *entity,
1554 					    struct media_interface *intf,
1555 					    u32 flags)
1556 {
1557 	struct media_link *link;
1558 
1559 	link = media_add_link(&intf->links);
1560 	if (link == NULL)
1561 		return NULL;
1562 
1563 	link->intf = intf;
1564 	link->entity = entity;
1565 	link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
1566 
1567 	/* Initialize graph object embedded at the new link */
1568 	media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1569 			&link->graph_obj);
1570 
1571 	return link;
1572 }
1573 EXPORT_SYMBOL_GPL(media_create_intf_link);
1574 
1575 void __media_remove_intf_link(struct media_link *link)
1576 {
1577 	list_del(&link->list);
1578 	media_gobj_destroy(&link->graph_obj);
1579 	kfree(link);
1580 }
1581 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1582 
1583 void media_remove_intf_link(struct media_link *link)
1584 {
1585 	struct media_device *mdev = link->graph_obj.mdev;
1586 
1587 	/* Do nothing if the intf is not registered. */
1588 	if (mdev == NULL)
1589 		return;
1590 
1591 	mutex_lock(&mdev->graph_mutex);
1592 	__media_remove_intf_link(link);
1593 	mutex_unlock(&mdev->graph_mutex);
1594 }
1595 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1596 
1597 void __media_remove_intf_links(struct media_interface *intf)
1598 {
1599 	struct media_link *link, *tmp;
1600 
1601 	list_for_each_entry_safe(link, tmp, &intf->links, list)
1602 		__media_remove_intf_link(link);
1603 
1604 }
1605 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1606 
1607 void media_remove_intf_links(struct media_interface *intf)
1608 {
1609 	struct media_device *mdev = intf->graph_obj.mdev;
1610 
1611 	/* Do nothing if the intf is not registered. */
1612 	if (mdev == NULL)
1613 		return;
1614 
1615 	mutex_lock(&mdev->graph_mutex);
1616 	__media_remove_intf_links(intf);
1617 	mutex_unlock(&mdev->graph_mutex);
1618 }
1619 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1620 
1621 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1622 					       struct media_entity *ancillary)
1623 {
1624 	struct media_link *link;
1625 
1626 	link = media_add_link(&primary->links);
1627 	if (!link)
1628 		return ERR_PTR(-ENOMEM);
1629 
1630 	link->gobj0 = &primary->graph_obj;
1631 	link->gobj1 = &ancillary->graph_obj;
1632 	link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1633 		      MEDIA_LNK_FL_ANCILLARY_LINK;
1634 
1635 	/* Initialize graph object embedded in the new link */
1636 	media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1637 			  &link->graph_obj);
1638 
1639 	return link;
1640 }
1641 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
1642 
1643 struct media_link *__media_entity_next_link(struct media_entity *entity,
1644 					    struct media_link *link,
1645 					    unsigned long link_type)
1646 {
1647 	link = link ? list_next_entry(link, list)
1648 		    : list_first_entry(&entity->links, typeof(*link), list);
1649 
1650 	list_for_each_entry_from(link, &entity->links, list)
1651 		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
1652 			return link;
1653 
1654 	return NULL;
1655 }
1656 EXPORT_SYMBOL_GPL(__media_entity_next_link);
1657