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