xref: /openbmc/linux/drivers/media/mc/mc-entity.c (revision c4c3c32d)
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, u32 pad_type,
1056 			enum media_pad_signal_type sig_type)
1057 {
1058 	unsigned int i;
1059 
1060 	if (!entity)
1061 		return -EINVAL;
1062 
1063 	for (i = 0; i < entity->num_pads; i++) {
1064 		if ((entity->pads[i].flags &
1065 		     (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) != pad_type)
1066 			continue;
1067 
1068 		if (entity->pads[i].sig_type == sig_type)
1069 			return i;
1070 	}
1071 	return -EINVAL;
1072 }
1073 EXPORT_SYMBOL_GPL(media_get_pad_index);
1074 
1075 int
1076 media_create_pad_link(struct media_entity *source, u16 source_pad,
1077 			 struct media_entity *sink, u16 sink_pad, u32 flags)
1078 {
1079 	struct media_link *link;
1080 	struct media_link *backlink;
1081 
1082 	if (WARN_ON(!source || !sink) ||
1083 	    WARN_ON(source_pad >= source->num_pads) ||
1084 	    WARN_ON(sink_pad >= sink->num_pads))
1085 		return -EINVAL;
1086 	if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
1087 		return -EINVAL;
1088 	if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
1089 		return -EINVAL;
1090 
1091 	link = media_add_link(&source->links);
1092 	if (link == NULL)
1093 		return -ENOMEM;
1094 
1095 	link->source = &source->pads[source_pad];
1096 	link->sink = &sink->pads[sink_pad];
1097 	link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
1098 
1099 	/* Initialize graph object embedded at the new link */
1100 	media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
1101 			&link->graph_obj);
1102 
1103 	/* Create the backlink. Backlinks are used to help graph traversal and
1104 	 * are not reported to userspace.
1105 	 */
1106 	backlink = media_add_link(&sink->links);
1107 	if (backlink == NULL) {
1108 		__media_entity_remove_link(source, link);
1109 		return -ENOMEM;
1110 	}
1111 
1112 	backlink->source = &source->pads[source_pad];
1113 	backlink->sink = &sink->pads[sink_pad];
1114 	backlink->flags = flags;
1115 	backlink->is_backlink = true;
1116 
1117 	/* Initialize graph object embedded at the new link */
1118 	media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
1119 			&backlink->graph_obj);
1120 
1121 	link->reverse = backlink;
1122 	backlink->reverse = link;
1123 
1124 	sink->num_backlinks++;
1125 	sink->num_links++;
1126 	source->num_links++;
1127 
1128 	return 0;
1129 }
1130 EXPORT_SYMBOL_GPL(media_create_pad_link);
1131 
1132 int media_create_pad_links(const struct media_device *mdev,
1133 			   const u32 source_function,
1134 			   struct media_entity *source,
1135 			   const u16 source_pad,
1136 			   const u32 sink_function,
1137 			   struct media_entity *sink,
1138 			   const u16 sink_pad,
1139 			   u32 flags,
1140 			   const bool allow_both_undefined)
1141 {
1142 	struct media_entity *entity;
1143 	unsigned function;
1144 	int ret;
1145 
1146 	/* Trivial case: 1:1 relation */
1147 	if (source && sink)
1148 		return media_create_pad_link(source, source_pad,
1149 					     sink, sink_pad, flags);
1150 
1151 	/* Worse case scenario: n:n relation */
1152 	if (!source && !sink) {
1153 		if (!allow_both_undefined)
1154 			return 0;
1155 		media_device_for_each_entity(source, mdev) {
1156 			if (source->function != source_function)
1157 				continue;
1158 			media_device_for_each_entity(sink, mdev) {
1159 				if (sink->function != sink_function)
1160 					continue;
1161 				ret = media_create_pad_link(source, source_pad,
1162 							    sink, sink_pad,
1163 							    flags);
1164 				if (ret)
1165 					return ret;
1166 				flags &= ~(MEDIA_LNK_FL_ENABLED |
1167 					   MEDIA_LNK_FL_IMMUTABLE);
1168 			}
1169 		}
1170 		return 0;
1171 	}
1172 
1173 	/* Handle 1:n and n:1 cases */
1174 	if (source)
1175 		function = sink_function;
1176 	else
1177 		function = source_function;
1178 
1179 	media_device_for_each_entity(entity, mdev) {
1180 		if (entity->function != function)
1181 			continue;
1182 
1183 		if (source)
1184 			ret = media_create_pad_link(source, source_pad,
1185 						    entity, sink_pad, flags);
1186 		else
1187 			ret = media_create_pad_link(entity, source_pad,
1188 						    sink, sink_pad, flags);
1189 		if (ret)
1190 			return ret;
1191 		flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1192 	}
1193 	return 0;
1194 }
1195 EXPORT_SYMBOL_GPL(media_create_pad_links);
1196 
1197 void __media_entity_remove_links(struct media_entity *entity)
1198 {
1199 	struct media_link *link, *tmp;
1200 
1201 	list_for_each_entry_safe(link, tmp, &entity->links, list)
1202 		__media_entity_remove_link(entity, link);
1203 
1204 	entity->num_links = 0;
1205 	entity->num_backlinks = 0;
1206 }
1207 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
1208 
1209 void media_entity_remove_links(struct media_entity *entity)
1210 {
1211 	struct media_device *mdev = entity->graph_obj.mdev;
1212 
1213 	/* Do nothing if the entity is not registered. */
1214 	if (mdev == NULL)
1215 		return;
1216 
1217 	mutex_lock(&mdev->graph_mutex);
1218 	__media_entity_remove_links(entity);
1219 	mutex_unlock(&mdev->graph_mutex);
1220 }
1221 EXPORT_SYMBOL_GPL(media_entity_remove_links);
1222 
1223 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
1224 {
1225 	int ret;
1226 
1227 	/* Notify both entities. */
1228 	ret = media_entity_call(link->source->entity, link_setup,
1229 				link->source, link->sink, flags);
1230 	if (ret < 0 && ret != -ENOIOCTLCMD)
1231 		return ret;
1232 
1233 	ret = media_entity_call(link->sink->entity, link_setup,
1234 				link->sink, link->source, flags);
1235 	if (ret < 0 && ret != -ENOIOCTLCMD) {
1236 		media_entity_call(link->source->entity, link_setup,
1237 				  link->source, link->sink, link->flags);
1238 		return ret;
1239 	}
1240 
1241 	link->flags = flags;
1242 	link->reverse->flags = link->flags;
1243 
1244 	return 0;
1245 }
1246 
1247 int __media_entity_setup_link(struct media_link *link, u32 flags)
1248 {
1249 	const u32 mask = MEDIA_LNK_FL_ENABLED;
1250 	struct media_device *mdev;
1251 	struct media_pad *source, *sink;
1252 	int ret = -EBUSY;
1253 
1254 	if (link == NULL)
1255 		return -EINVAL;
1256 
1257 	/* The non-modifiable link flags must not be modified. */
1258 	if ((link->flags & ~mask) != (flags & ~mask))
1259 		return -EINVAL;
1260 
1261 	if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
1262 		return link->flags == flags ? 0 : -EINVAL;
1263 
1264 	if (link->flags == flags)
1265 		return 0;
1266 
1267 	source = link->source;
1268 	sink = link->sink;
1269 
1270 	if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
1271 	    (media_pad_is_streaming(source) || media_pad_is_streaming(sink)))
1272 		return -EBUSY;
1273 
1274 	mdev = source->graph_obj.mdev;
1275 
1276 	if (mdev->ops && mdev->ops->link_notify) {
1277 		ret = mdev->ops->link_notify(link, flags,
1278 					     MEDIA_DEV_NOTIFY_PRE_LINK_CH);
1279 		if (ret < 0)
1280 			return ret;
1281 	}
1282 
1283 	ret = __media_entity_setup_link_notify(link, flags);
1284 
1285 	if (mdev->ops && mdev->ops->link_notify)
1286 		mdev->ops->link_notify(link, flags,
1287 				       MEDIA_DEV_NOTIFY_POST_LINK_CH);
1288 
1289 	return ret;
1290 }
1291 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
1292 
1293 int media_entity_setup_link(struct media_link *link, u32 flags)
1294 {
1295 	int ret;
1296 
1297 	mutex_lock(&link->graph_obj.mdev->graph_mutex);
1298 	ret = __media_entity_setup_link(link, flags);
1299 	mutex_unlock(&link->graph_obj.mdev->graph_mutex);
1300 
1301 	return ret;
1302 }
1303 EXPORT_SYMBOL_GPL(media_entity_setup_link);
1304 
1305 struct media_link *
1306 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
1307 {
1308 	struct media_link *link;
1309 
1310 	for_each_media_entity_data_link(source->entity, link) {
1311 		if (link->source->entity == source->entity &&
1312 		    link->source->index == source->index &&
1313 		    link->sink->entity == sink->entity &&
1314 		    link->sink->index == sink->index)
1315 			return link;
1316 	}
1317 
1318 	return NULL;
1319 }
1320 EXPORT_SYMBOL_GPL(media_entity_find_link);
1321 
1322 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad)
1323 {
1324 	struct media_link *link;
1325 
1326 	for_each_media_entity_data_link(pad->entity, link) {
1327 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1328 			continue;
1329 
1330 		if (link->source == pad)
1331 			return link->sink;
1332 
1333 		if (link->sink == pad)
1334 			return link->source;
1335 	}
1336 
1337 	return NULL;
1338 
1339 }
1340 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first);
1341 
1342 struct media_pad *
1343 media_entity_remote_pad_unique(const struct media_entity *entity,
1344 			       unsigned int type)
1345 {
1346 	struct media_pad *pad = NULL;
1347 	struct media_link *link;
1348 
1349 	list_for_each_entry(link, &entity->links, list) {
1350 		struct media_pad *local_pad;
1351 		struct media_pad *remote_pad;
1352 
1353 		if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) !=
1354 		     MEDIA_LNK_FL_DATA_LINK) ||
1355 		    !(link->flags & MEDIA_LNK_FL_ENABLED))
1356 			continue;
1357 
1358 		if (type == MEDIA_PAD_FL_SOURCE) {
1359 			local_pad = link->sink;
1360 			remote_pad = link->source;
1361 		} else {
1362 			local_pad = link->source;
1363 			remote_pad = link->sink;
1364 		}
1365 
1366 		if (local_pad->entity == entity) {
1367 			if (pad)
1368 				return ERR_PTR(-ENOTUNIQ);
1369 
1370 			pad = remote_pad;
1371 		}
1372 	}
1373 
1374 	if (!pad)
1375 		return ERR_PTR(-ENOLINK);
1376 
1377 	return pad;
1378 }
1379 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique);
1380 
1381 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad)
1382 {
1383 	struct media_pad *found_pad = NULL;
1384 	struct media_link *link;
1385 
1386 	list_for_each_entry(link, &pad->entity->links, list) {
1387 		struct media_pad *remote_pad;
1388 
1389 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1390 			continue;
1391 
1392 		if (link->sink == pad)
1393 			remote_pad = link->source;
1394 		else if (link->source == pad)
1395 			remote_pad = link->sink;
1396 		else
1397 			continue;
1398 
1399 		if (found_pad)
1400 			return ERR_PTR(-ENOTUNIQ);
1401 
1402 		found_pad = remote_pad;
1403 	}
1404 
1405 	if (!found_pad)
1406 		return ERR_PTR(-ENOLINK);
1407 
1408 	return found_pad;
1409 }
1410 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique);
1411 
1412 int media_entity_get_fwnode_pad(struct media_entity *entity,
1413 				const struct fwnode_handle *fwnode,
1414 				unsigned long direction_flags)
1415 {
1416 	struct fwnode_endpoint endpoint;
1417 	unsigned int i;
1418 	int ret;
1419 
1420 	if (!entity->ops || !entity->ops->get_fwnode_pad) {
1421 		for (i = 0; i < entity->num_pads; i++) {
1422 			if (entity->pads[i].flags & direction_flags)
1423 				return i;
1424 		}
1425 
1426 		return -ENXIO;
1427 	}
1428 
1429 	ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
1430 	if (ret)
1431 		return ret;
1432 
1433 	ret = entity->ops->get_fwnode_pad(entity, &endpoint);
1434 	if (ret < 0)
1435 		return ret;
1436 
1437 	if (ret >= entity->num_pads)
1438 		return -ENXIO;
1439 
1440 	if (!(entity->pads[ret].flags & direction_flags))
1441 		return -ENXIO;
1442 
1443 	return ret;
1444 }
1445 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
1446 
1447 struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
1448 {
1449 	struct media_pad *pad;
1450 
1451 	media_entity_for_each_pad(entity, pad) {
1452 		if (pad->pipe)
1453 			return pad->pipe;
1454 	}
1455 
1456 	return NULL;
1457 }
1458 EXPORT_SYMBOL_GPL(media_entity_pipeline);
1459 
1460 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
1461 {
1462 	return pad->pipe;
1463 }
1464 EXPORT_SYMBOL_GPL(media_pad_pipeline);
1465 
1466 static void media_interface_init(struct media_device *mdev,
1467 				 struct media_interface *intf,
1468 				 u32 gobj_type,
1469 				 u32 intf_type, u32 flags)
1470 {
1471 	intf->type = intf_type;
1472 	intf->flags = flags;
1473 	INIT_LIST_HEAD(&intf->links);
1474 
1475 	media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1476 }
1477 
1478 /* Functions related to the media interface via device nodes */
1479 
1480 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
1481 						u32 type, u32 flags,
1482 						u32 major, u32 minor)
1483 {
1484 	struct media_intf_devnode *devnode;
1485 
1486 	devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
1487 	if (!devnode)
1488 		return NULL;
1489 
1490 	devnode->major = major;
1491 	devnode->minor = minor;
1492 
1493 	media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
1494 			     type, flags);
1495 
1496 	return devnode;
1497 }
1498 EXPORT_SYMBOL_GPL(media_devnode_create);
1499 
1500 void media_devnode_remove(struct media_intf_devnode *devnode)
1501 {
1502 	media_remove_intf_links(&devnode->intf);
1503 	media_gobj_destroy(&devnode->intf.graph_obj);
1504 	kfree(devnode);
1505 }
1506 EXPORT_SYMBOL_GPL(media_devnode_remove);
1507 
1508 struct media_link *media_create_intf_link(struct media_entity *entity,
1509 					    struct media_interface *intf,
1510 					    u32 flags)
1511 {
1512 	struct media_link *link;
1513 
1514 	link = media_add_link(&intf->links);
1515 	if (link == NULL)
1516 		return NULL;
1517 
1518 	link->intf = intf;
1519 	link->entity = entity;
1520 	link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
1521 
1522 	/* Initialize graph object embedded at the new link */
1523 	media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1524 			&link->graph_obj);
1525 
1526 	return link;
1527 }
1528 EXPORT_SYMBOL_GPL(media_create_intf_link);
1529 
1530 void __media_remove_intf_link(struct media_link *link)
1531 {
1532 	list_del(&link->list);
1533 	media_gobj_destroy(&link->graph_obj);
1534 	kfree(link);
1535 }
1536 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1537 
1538 void media_remove_intf_link(struct media_link *link)
1539 {
1540 	struct media_device *mdev = link->graph_obj.mdev;
1541 
1542 	/* Do nothing if the intf is not registered. */
1543 	if (mdev == NULL)
1544 		return;
1545 
1546 	mutex_lock(&mdev->graph_mutex);
1547 	__media_remove_intf_link(link);
1548 	mutex_unlock(&mdev->graph_mutex);
1549 }
1550 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1551 
1552 void __media_remove_intf_links(struct media_interface *intf)
1553 {
1554 	struct media_link *link, *tmp;
1555 
1556 	list_for_each_entry_safe(link, tmp, &intf->links, list)
1557 		__media_remove_intf_link(link);
1558 
1559 }
1560 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1561 
1562 void media_remove_intf_links(struct media_interface *intf)
1563 {
1564 	struct media_device *mdev = intf->graph_obj.mdev;
1565 
1566 	/* Do nothing if the intf is not registered. */
1567 	if (mdev == NULL)
1568 		return;
1569 
1570 	mutex_lock(&mdev->graph_mutex);
1571 	__media_remove_intf_links(intf);
1572 	mutex_unlock(&mdev->graph_mutex);
1573 }
1574 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1575 
1576 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1577 					       struct media_entity *ancillary)
1578 {
1579 	struct media_link *link;
1580 
1581 	link = media_add_link(&primary->links);
1582 	if (!link)
1583 		return ERR_PTR(-ENOMEM);
1584 
1585 	link->gobj0 = &primary->graph_obj;
1586 	link->gobj1 = &ancillary->graph_obj;
1587 	link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1588 		      MEDIA_LNK_FL_ANCILLARY_LINK;
1589 
1590 	/* Initialize graph object embedded in the new link */
1591 	media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1592 			  &link->graph_obj);
1593 
1594 	return link;
1595 }
1596 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
1597 
1598 struct media_link *__media_entity_next_link(struct media_entity *entity,
1599 					    struct media_link *link,
1600 					    unsigned long link_type)
1601 {
1602 	link = link ? list_next_entry(link, list)
1603 		    : list_first_entry(&entity->links, typeof(*link), list);
1604 
1605 	list_for_each_entry_from(link, &entity->links, list)
1606 		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
1607 			return link;
1608 
1609 	return NULL;
1610 }
1611 EXPORT_SYMBOL_GPL(__media_entity_next_link);
1612