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