1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation
7 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * Sakari Ailus <sakari.ailus@iki.fi>
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>
18 static inline const char *intf_type(struct media_interface *intf)
21 case MEDIA_INTF_T_DVB_FE:
22 return "dvb-frontend";
23 case MEDIA_INTF_T_DVB_DEMUX:
25 case MEDIA_INTF_T_DVB_DVR:
27 case MEDIA_INTF_T_DVB_CA:
29 case MEDIA_INTF_T_DVB_NET:
31 case MEDIA_INTF_T_V4L_VIDEO:
33 case MEDIA_INTF_T_V4L_VBI:
35 case MEDIA_INTF_T_V4L_RADIO:
37 case MEDIA_INTF_T_V4L_SUBDEV:
39 case MEDIA_INTF_T_V4L_SWRADIO:
41 case MEDIA_INTF_T_V4L_TOUCH:
44 return "unknown-intf";
48 static inline const char *link_type_name(struct media_link *link)
50 switch (link->flags & MEDIA_LNK_FL_LINK_TYPE) {
51 case MEDIA_LNK_FL_DATA_LINK:
53 case MEDIA_LNK_FL_INTERFACE_LINK:
55 case MEDIA_LNK_FL_ANCILLARY_LINK:
62 __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum,
63 struct media_device *mdev)
67 idx_max = ALIGN(mdev->entity_internal_idx_max + 1, BITS_PER_LONG);
68 ent_enum->bmap = bitmap_zalloc(idx_max, GFP_KERNEL);
72 ent_enum->idx_max = idx_max;
76 EXPORT_SYMBOL_GPL(media_entity_enum_init);
78 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
80 bitmap_free(ent_enum->bmap);
82 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
85 * dev_dbg_obj - Prints in debug mode a change on some object
87 * @event_name: Name of the event to report. Could be __func__
88 * @gobj: Pointer to the object
90 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
91 * won't produce any code.
93 static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
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);
103 case MEDIA_GRAPH_LINK:
105 struct media_link *link = gobj_to_link(gobj);
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));
114 case MEDIA_GRAPH_PAD:
116 struct media_pad *pad = gobj_to_pad(gobj);
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);
126 case MEDIA_GRAPH_INTF_DEVNODE:
128 struct media_interface *intf = gobj_to_intf(gobj);
129 struct media_intf_devnode *devnode = intf_to_devnode(intf);
131 dev_dbg(gobj->mdev->dev,
132 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
133 event_name, media_id(gobj),
135 devnode->major, devnode->minor);
142 void media_gobj_create(struct media_device *mdev,
143 enum media_gobj_type type,
144 struct media_gobj *gobj)
150 /* Create a per-type unique object ID */
151 gobj->id = media_gobj_gen_id(type, ++mdev->id);
154 case MEDIA_GRAPH_ENTITY:
155 list_add_tail(&gobj->list, &mdev->entities);
157 case MEDIA_GRAPH_PAD:
158 list_add_tail(&gobj->list, &mdev->pads);
160 case MEDIA_GRAPH_LINK:
161 list_add_tail(&gobj->list, &mdev->links);
163 case MEDIA_GRAPH_INTF_DEVNODE:
164 list_add_tail(&gobj->list, &mdev->interfaces);
168 mdev->topology_version++;
170 dev_dbg_obj(__func__, gobj);
173 void media_gobj_destroy(struct media_gobj *gobj)
175 /* Do nothing if the object is not linked. */
176 if (gobj->mdev == NULL)
179 dev_dbg_obj(__func__, gobj);
181 gobj->mdev->topology_version++;
183 /* Remove the object from mdev list */
184 list_del(&gobj->list);
190 * TODO: Get rid of this.
192 #define MEDIA_ENTITY_MAX_PADS 512
194 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
195 struct media_pad *pads)
197 struct media_device *mdev = entity->graph_obj.mdev;
198 struct media_pad *iter;
201 if (num_pads >= MEDIA_ENTITY_MAX_PADS)
204 entity->num_pads = num_pads;
208 mutex_lock(&mdev->graph_mutex);
210 media_entity_for_each_pad(entity, iter) {
211 iter->entity = entity;
214 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
219 mutex_unlock(&mdev->graph_mutex);
223 EXPORT_SYMBOL_GPL(media_entity_pads_init);
225 /* -----------------------------------------------------------------------------
230 * This function checks the interdependency inside the entity between @pad0
231 * and @pad1. If two pads are interdependent they are part of the same pipeline
232 * and enabling one of the pads means that the other pad will become "locked"
233 * and doesn't allow configuration changes.
235 * This function uses the &media_entity_operations.has_pad_interdep() operation
236 * to check the dependency inside the entity between @pad0 and @pad1. If the
237 * has_pad_interdep operation is not implemented, all pads of the entity are
238 * considered to be interdependent.
240 static bool media_entity_has_pad_interdep(struct media_entity *entity,
241 unsigned int pad0, unsigned int pad1)
243 if (pad0 >= entity->num_pads || pad1 >= entity->num_pads)
246 if (entity->pads[pad0].flags & entity->pads[pad1].flags &
247 (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE))
250 if (!entity->ops || !entity->ops->has_pad_interdep)
253 return entity->ops->has_pad_interdep(entity, pad0, pad1);
256 static struct media_entity *
257 media_entity_other(struct media_entity *entity, struct media_link *link)
259 if (link->source->entity == entity)
260 return link->sink->entity;
262 return link->source->entity;
265 /* push an entity to traversal stack */
266 static void stack_push(struct media_graph *graph,
267 struct media_entity *entity)
269 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
274 graph->stack[graph->top].link = entity->links.next;
275 graph->stack[graph->top].entity = entity;
278 static struct media_entity *stack_pop(struct media_graph *graph)
280 struct media_entity *entity;
282 entity = graph->stack[graph->top].entity;
288 #define link_top(en) ((en)->stack[(en)->top].link)
289 #define stack_top(en) ((en)->stack[(en)->top].entity)
292 * media_graph_walk_init - Allocate resources for graph walk
293 * @graph: Media graph structure that will be used to walk the graph
294 * @mdev: Media device
296 * Reserve resources for graph walk in media device's current
297 * state. The memory must be released using
298 * media_graph_walk_free().
300 * Returns error on failure, zero on success.
302 __must_check int media_graph_walk_init(
303 struct media_graph *graph, struct media_device *mdev)
305 return media_entity_enum_init(&graph->ent_enum, mdev);
307 EXPORT_SYMBOL_GPL(media_graph_walk_init);
310 * media_graph_walk_cleanup - Release resources related to graph walking
311 * @graph: Media graph structure that was used to walk the graph
313 void media_graph_walk_cleanup(struct media_graph *graph)
315 media_entity_enum_cleanup(&graph->ent_enum);
317 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
319 void media_graph_walk_start(struct media_graph *graph,
320 struct media_entity *entity)
322 media_entity_enum_zero(&graph->ent_enum);
323 media_entity_enum_set(&graph->ent_enum, entity);
326 graph->stack[graph->top].entity = NULL;
327 stack_push(graph, entity);
328 dev_dbg(entity->graph_obj.mdev->dev,
329 "begin graph walk at '%s'\n", entity->name);
331 EXPORT_SYMBOL_GPL(media_graph_walk_start);
333 static void media_graph_walk_iter(struct media_graph *graph)
335 struct media_entity *entity = stack_top(graph);
336 struct media_link *link;
337 struct media_entity *next;
339 link = list_entry(link_top(graph), typeof(*link), list);
341 /* If the link is not a data link, don't follow it */
342 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) {
343 link_top(graph) = link_top(graph)->next;
347 /* The link is not enabled so we do not follow. */
348 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
349 link_top(graph) = link_top(graph)->next;
350 dev_dbg(entity->graph_obj.mdev->dev,
351 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
352 link->source->entity->name, link->source->index,
353 link->sink->entity->name, link->sink->index);
357 /* Get the entity at the other end of the link. */
358 next = media_entity_other(entity, link);
360 /* Has the entity already been visited? */
361 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
362 link_top(graph) = link_top(graph)->next;
363 dev_dbg(entity->graph_obj.mdev->dev,
364 "walk: skipping entity '%s' (already seen)\n",
369 /* Push the new entity to stack and start over. */
370 link_top(graph) = link_top(graph)->next;
371 stack_push(graph, next);
372 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
374 lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex);
377 struct media_entity *media_graph_walk_next(struct media_graph *graph)
379 struct media_entity *entity;
381 if (stack_top(graph) == NULL)
385 * Depth first search. Push entity to stack and continue from
386 * top of the stack until no more entities on the level can be
389 while (link_top(graph) != &stack_top(graph)->links)
390 media_graph_walk_iter(graph);
392 entity = stack_pop(graph);
393 dev_dbg(entity->graph_obj.mdev->dev,
394 "walk: returning entity '%s'\n", entity->name);
398 EXPORT_SYMBOL_GPL(media_graph_walk_next);
400 /* -----------------------------------------------------------------------------
401 * Pipeline management
405 * The pipeline traversal stack stores pads that are reached during graph
406 * traversal, with a list of links to be visited to continue the traversal.
407 * When a new pad is reached, an entry is pushed on the top of the stack and
408 * points to the incoming pad and the first link of the entity.
410 * To find further pads in the pipeline, the traversal algorithm follows
411 * internal pad dependencies in the entity, and then links in the graph. It
412 * does so by iterating over all links of the entity, and following enabled
413 * links that originate from a pad that is internally connected to the incoming
414 * pad, as reported by the media_entity_has_pad_interdep() function.
418 * struct media_pipeline_walk_entry - Entry in the pipeline traversal stack
420 * @pad: The media pad being visited
421 * @links: Links left to be visited
423 struct media_pipeline_walk_entry {
424 struct media_pad *pad;
425 struct list_head *links;
429 * struct media_pipeline_walk - State used by the media pipeline traversal
432 * @mdev: The media device
433 * @stack: Depth-first search stack
434 * @stack.size: Number of allocated entries in @stack.entries
435 * @stack.top: Index of the top stack entry (-1 if the stack is empty)
436 * @stack.entries: Stack entries
438 struct media_pipeline_walk {
439 struct media_device *mdev;
444 struct media_pipeline_walk_entry *entries;
448 #define MEDIA_PIPELINE_STACK_GROW_STEP 16
450 static struct media_pipeline_walk_entry *
451 media_pipeline_walk_top(struct media_pipeline_walk *walk)
453 return &walk->stack.entries[walk->stack.top];
456 static bool media_pipeline_walk_empty(struct media_pipeline_walk *walk)
458 return walk->stack.top == -1;
461 /* Increase the stack size by MEDIA_PIPELINE_STACK_GROW_STEP elements. */
462 static int media_pipeline_walk_resize(struct media_pipeline_walk *walk)
464 struct media_pipeline_walk_entry *entries;
465 unsigned int new_size;
467 /* Safety check, to avoid stack overflows in case of bugs. */
468 if (walk->stack.size >= 256)
471 new_size = walk->stack.size + MEDIA_PIPELINE_STACK_GROW_STEP;
473 entries = krealloc(walk->stack.entries,
474 new_size * sizeof(*walk->stack.entries),
479 walk->stack.entries = entries;
480 walk->stack.size = new_size;
485 /* Push a new entry on the stack. */
486 static int media_pipeline_walk_push(struct media_pipeline_walk *walk,
487 struct media_pad *pad)
489 struct media_pipeline_walk_entry *entry;
492 if (walk->stack.top + 1 >= walk->stack.size) {
493 ret = media_pipeline_walk_resize(walk);
499 entry = media_pipeline_walk_top(walk);
501 entry->links = pad->entity->links.next;
503 dev_dbg(walk->mdev->dev,
504 "media pipeline: pushed entry %u: '%s':%u\n",
505 walk->stack.top, pad->entity->name, pad->index);
511 * Move the top entry link cursor to the next link. If all links of the entry
512 * have been visited, pop the entry itself.
514 static void media_pipeline_walk_pop(struct media_pipeline_walk *walk)
516 struct media_pipeline_walk_entry *entry;
518 if (WARN_ON(walk->stack.top < 0))
521 entry = media_pipeline_walk_top(walk);
523 if (entry->links->next == &entry->pad->entity->links) {
524 dev_dbg(walk->mdev->dev,
525 "media pipeline: entry %u has no more links, popping\n",
532 entry->links = entry->links->next;
534 dev_dbg(walk->mdev->dev,
535 "media pipeline: moved entry %u to next link\n",
539 /* Free all memory allocated while walking the pipeline. */
540 static void media_pipeline_walk_destroy(struct media_pipeline_walk *walk)
542 kfree(walk->stack.entries);
545 /* Add a pad to the pipeline and push it to the stack. */
546 static int media_pipeline_add_pad(struct media_pipeline *pipe,
547 struct media_pipeline_walk *walk,
548 struct media_pad *pad)
550 struct media_pipeline_pad *ppad;
552 list_for_each_entry(ppad, &pipe->pads, list) {
553 if (ppad->pad == pad) {
554 dev_dbg(pad->graph_obj.mdev->dev,
555 "media pipeline: already contains pad '%s':%u\n",
556 pad->entity->name, pad->index);
561 ppad = kzalloc(sizeof(*ppad), GFP_KERNEL);
568 list_add_tail(&ppad->list, &pipe->pads);
570 dev_dbg(pad->graph_obj.mdev->dev,
571 "media pipeline: added pad '%s':%u\n",
572 pad->entity->name, pad->index);
574 return media_pipeline_walk_push(walk, pad);
577 /* Explore the next link of the entity at the top of the stack. */
578 static int media_pipeline_explore_next_link(struct media_pipeline *pipe,
579 struct media_pipeline_walk *walk)
581 struct media_pipeline_walk_entry *entry = media_pipeline_walk_top(walk);
582 struct media_pad *pad;
583 struct media_link *link;
584 struct media_pad *local;
585 struct media_pad *remote;
589 link = list_entry(entry->links, typeof(*link), list);
590 media_pipeline_walk_pop(walk);
592 dev_dbg(walk->mdev->dev,
593 "media pipeline: exploring link '%s':%u -> '%s':%u\n",
594 link->source->entity->name, link->source->index,
595 link->sink->entity->name, link->sink->index);
597 /* Skip links that are not enabled. */
598 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
599 dev_dbg(walk->mdev->dev,
600 "media pipeline: skipping link (disabled)\n");
604 /* Get the local pad and remote pad. */
605 if (link->source->entity == pad->entity) {
606 local = link->source;
610 remote = link->source;
614 * Skip links that originate from a different pad than the incoming pad
615 * that is not connected internally in the entity to the incoming pad.
618 !media_entity_has_pad_interdep(pad->entity, pad->index, local->index)) {
619 dev_dbg(walk->mdev->dev,
620 "media pipeline: skipping link (no route)\n");
625 * Add the local and remote pads of the link to the pipeline and push
626 * them to the stack, if they're not already present.
628 ret = media_pipeline_add_pad(pipe, walk, local);
632 ret = media_pipeline_add_pad(pipe, walk, remote);
639 static void media_pipeline_cleanup(struct media_pipeline *pipe)
641 while (!list_empty(&pipe->pads)) {
642 struct media_pipeline_pad *ppad;
644 ppad = list_first_entry(&pipe->pads, typeof(*ppad), list);
645 list_del(&ppad->list);
650 static int media_pipeline_populate(struct media_pipeline *pipe,
651 struct media_pad *pad)
653 struct media_pipeline_walk walk = { };
654 struct media_pipeline_pad *ppad;
658 * Populate the media pipeline by walking the media graph, starting
661 INIT_LIST_HEAD(&pipe->pads);
662 pipe->mdev = pad->graph_obj.mdev;
664 walk.mdev = pipe->mdev;
666 ret = media_pipeline_add_pad(pipe, &walk, pad);
671 * Use a depth-first search algorithm: as long as the stack is not
672 * empty, explore the next link of the top entry. The
673 * media_pipeline_explore_next_link() function will either move to the
674 * next link, pop the entry if fully visited, or add new entries on
677 while (!media_pipeline_walk_empty(&walk)) {
678 ret = media_pipeline_explore_next_link(pipe, &walk);
683 dev_dbg(pad->graph_obj.mdev->dev,
684 "media pipeline populated, found pads:\n");
686 list_for_each_entry(ppad, &pipe->pads, list)
687 dev_dbg(pad->graph_obj.mdev->dev, "- '%s':%u\n",
688 ppad->pad->entity->name, ppad->pad->index);
690 WARN_ON(walk.stack.top != -1);
695 media_pipeline_walk_destroy(&walk);
698 media_pipeline_cleanup(pipe);
703 __must_check int __media_pipeline_start(struct media_pad *pad,
704 struct media_pipeline *pipe)
706 struct media_device *mdev = pad->entity->graph_obj.mdev;
707 struct media_pipeline_pad *err_ppad;
708 struct media_pipeline_pad *ppad;
711 lockdep_assert_held(&mdev->graph_mutex);
714 * If the entity is already part of a pipeline, that pipeline must
715 * be the same as the pipe given to media_pipeline_start().
717 if (WARN_ON(pad->pipe && pad->pipe != pipe))
721 * If the pipeline has already been started, it is guaranteed to be
722 * valid, so just increase the start count.
724 if (pipe->start_count) {
730 * Populate the pipeline. This populates the media_pipeline pads list
731 * with media_pipeline_pad instances for each pad found during graph
734 ret = media_pipeline_populate(pipe, pad);
739 * Now that all the pads in the pipeline have been gathered, perform
740 * the validation steps.
743 list_for_each_entry(ppad, &pipe->pads, list) {
744 struct media_pad *pad = ppad->pad;
745 struct media_entity *entity = pad->entity;
746 bool has_enabled_link = false;
747 bool has_link = false;
748 struct media_link *link;
750 dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name,
754 * 1. Ensure that the pad doesn't already belong to a different
758 dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n",
759 pad->entity->name, pad->index);
765 * 2. Validate all active links whose sink is the current pad.
766 * Validation of the source pads is performed in the context of
767 * the connected sink pad to avoid duplicating checks.
769 for_each_media_entity_data_link(entity, link) {
770 /* Skip links unrelated to the current pad. */
771 if (link->sink != pad && link->source != pad)
774 /* Record if the pad has links and enabled links. */
775 if (link->flags & MEDIA_LNK_FL_ENABLED)
776 has_enabled_link = true;
780 * Validate the link if it's enabled and has the
781 * current pad as its sink.
783 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
786 if (link->sink != pad)
789 if (!entity->ops || !entity->ops->link_validate)
792 ret = entity->ops->link_validate(link);
795 "Link '%s':%u -> '%s':%u failed validation: %d\n",
796 link->source->entity->name,
798 link->sink->entity->name,
799 link->sink->index, ret);
804 "Link '%s':%u -> '%s':%u is valid\n",
805 link->source->entity->name,
807 link->sink->entity->name,
812 * 3. If the pad has the MEDIA_PAD_FL_MUST_CONNECT flag set,
813 * ensure that it has either no link or an enabled link.
815 if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) && has_link &&
818 "Pad '%s':%u must be connected by an enabled link\n",
819 pad->entity->name, pad->index);
824 /* Validation passed, store the pipe pointer in the pad. */
834 * Link validation on graph failed. We revert what we did and
838 list_for_each_entry(err_ppad, &pipe->pads, list) {
839 if (err_ppad == ppad)
842 err_ppad->pad->pipe = NULL;
845 media_pipeline_cleanup(pipe);
849 EXPORT_SYMBOL_GPL(__media_pipeline_start);
851 __must_check int media_pipeline_start(struct media_pad *pad,
852 struct media_pipeline *pipe)
854 struct media_device *mdev = pad->entity->graph_obj.mdev;
857 mutex_lock(&mdev->graph_mutex);
858 ret = __media_pipeline_start(pad, pipe);
859 mutex_unlock(&mdev->graph_mutex);
862 EXPORT_SYMBOL_GPL(media_pipeline_start);
864 void __media_pipeline_stop(struct media_pad *pad)
866 struct media_pipeline *pipe = pad->pipe;
867 struct media_pipeline_pad *ppad;
870 * If the following check fails, the driver has performed an
871 * unbalanced call to media_pipeline_stop()
876 if (--pipe->start_count)
879 list_for_each_entry(ppad, &pipe->pads, list)
880 ppad->pad->pipe = NULL;
882 media_pipeline_cleanup(pipe);
887 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
889 void media_pipeline_stop(struct media_pad *pad)
891 struct media_device *mdev = pad->entity->graph_obj.mdev;
893 mutex_lock(&mdev->graph_mutex);
894 __media_pipeline_stop(pad);
895 mutex_unlock(&mdev->graph_mutex);
897 EXPORT_SYMBOL_GPL(media_pipeline_stop);
899 __must_check int media_pipeline_alloc_start(struct media_pad *pad)
901 struct media_device *mdev = pad->entity->graph_obj.mdev;
902 struct media_pipeline *new_pipe = NULL;
903 struct media_pipeline *pipe;
906 mutex_lock(&mdev->graph_mutex);
909 * Is the entity already part of a pipeline? If not, we need to allocate
912 pipe = media_pad_pipeline(pad);
914 new_pipe = kzalloc(sizeof(*new_pipe), GFP_KERNEL);
921 pipe->allocated = true;
924 ret = __media_pipeline_start(pad, pipe);
929 mutex_unlock(&mdev->graph_mutex);
933 EXPORT_SYMBOL_GPL(media_pipeline_alloc_start);
935 /* -----------------------------------------------------------------------------
939 static struct media_link *media_add_link(struct list_head *head)
941 struct media_link *link;
943 link = kzalloc(sizeof(*link), GFP_KERNEL);
947 list_add_tail(&link->list, head);
952 static void __media_entity_remove_link(struct media_entity *entity,
953 struct media_link *link)
955 struct media_link *rlink, *tmp;
956 struct media_entity *remote;
958 /* Remove the reverse links for a data link. */
959 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
960 if (link->source->entity == entity)
961 remote = link->sink->entity;
963 remote = link->source->entity;
965 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
966 if (rlink != link->reverse)
969 if (link->source->entity == entity)
970 remote->num_backlinks--;
972 /* Remove the remote link */
973 list_del(&rlink->list);
974 media_gobj_destroy(&rlink->graph_obj);
977 if (--remote->num_links == 0)
982 list_del(&link->list);
983 media_gobj_destroy(&link->graph_obj);
987 int media_get_pad_index(struct media_entity *entity, bool is_sink,
988 enum media_pad_signal_type sig_type)
996 for (i = 0; i < entity->num_pads; i++) {
997 if (entity->pads[i].flags & MEDIA_PAD_FL_SINK)
999 else if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
1000 pad_is_sink = false;
1002 continue; /* This is an error! */
1004 if (pad_is_sink != is_sink)
1006 if (entity->pads[i].sig_type == sig_type)
1011 EXPORT_SYMBOL_GPL(media_get_pad_index);
1014 media_create_pad_link(struct media_entity *source, u16 source_pad,
1015 struct media_entity *sink, u16 sink_pad, u32 flags)
1017 struct media_link *link;
1018 struct media_link *backlink;
1020 if (WARN_ON(!source || !sink) ||
1021 WARN_ON(source_pad >= source->num_pads) ||
1022 WARN_ON(sink_pad >= sink->num_pads))
1024 if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
1026 if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
1029 link = media_add_link(&source->links);
1033 link->source = &source->pads[source_pad];
1034 link->sink = &sink->pads[sink_pad];
1035 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
1037 /* Initialize graph object embedded at the new link */
1038 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
1041 /* Create the backlink. Backlinks are used to help graph traversal and
1042 * are not reported to userspace.
1044 backlink = media_add_link(&sink->links);
1045 if (backlink == NULL) {
1046 __media_entity_remove_link(source, link);
1050 backlink->source = &source->pads[source_pad];
1051 backlink->sink = &sink->pads[sink_pad];
1052 backlink->flags = flags;
1053 backlink->is_backlink = true;
1055 /* Initialize graph object embedded at the new link */
1056 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
1057 &backlink->graph_obj);
1059 link->reverse = backlink;
1060 backlink->reverse = link;
1062 sink->num_backlinks++;
1064 source->num_links++;
1068 EXPORT_SYMBOL_GPL(media_create_pad_link);
1070 int media_create_pad_links(const struct media_device *mdev,
1071 const u32 source_function,
1072 struct media_entity *source,
1073 const u16 source_pad,
1074 const u32 sink_function,
1075 struct media_entity *sink,
1078 const bool allow_both_undefined)
1080 struct media_entity *entity;
1084 /* Trivial case: 1:1 relation */
1086 return media_create_pad_link(source, source_pad,
1087 sink, sink_pad, flags);
1089 /* Worse case scenario: n:n relation */
1090 if (!source && !sink) {
1091 if (!allow_both_undefined)
1093 media_device_for_each_entity(source, mdev) {
1094 if (source->function != source_function)
1096 media_device_for_each_entity(sink, mdev) {
1097 if (sink->function != sink_function)
1099 ret = media_create_pad_link(source, source_pad,
1104 flags &= ~(MEDIA_LNK_FL_ENABLED |
1105 MEDIA_LNK_FL_IMMUTABLE);
1111 /* Handle 1:n and n:1 cases */
1113 function = sink_function;
1115 function = source_function;
1117 media_device_for_each_entity(entity, mdev) {
1118 if (entity->function != function)
1122 ret = media_create_pad_link(source, source_pad,
1123 entity, sink_pad, flags);
1125 ret = media_create_pad_link(entity, source_pad,
1126 sink, sink_pad, flags);
1129 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1133 EXPORT_SYMBOL_GPL(media_create_pad_links);
1135 void __media_entity_remove_links(struct media_entity *entity)
1137 struct media_link *link, *tmp;
1139 list_for_each_entry_safe(link, tmp, &entity->links, list)
1140 __media_entity_remove_link(entity, link);
1142 entity->num_links = 0;
1143 entity->num_backlinks = 0;
1145 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
1147 void media_entity_remove_links(struct media_entity *entity)
1149 struct media_device *mdev = entity->graph_obj.mdev;
1151 /* Do nothing if the entity is not registered. */
1155 mutex_lock(&mdev->graph_mutex);
1156 __media_entity_remove_links(entity);
1157 mutex_unlock(&mdev->graph_mutex);
1159 EXPORT_SYMBOL_GPL(media_entity_remove_links);
1161 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
1165 /* Notify both entities. */
1166 ret = media_entity_call(link->source->entity, link_setup,
1167 link->source, link->sink, flags);
1168 if (ret < 0 && ret != -ENOIOCTLCMD)
1171 ret = media_entity_call(link->sink->entity, link_setup,
1172 link->sink, link->source, flags);
1173 if (ret < 0 && ret != -ENOIOCTLCMD) {
1174 media_entity_call(link->source->entity, link_setup,
1175 link->source, link->sink, link->flags);
1179 link->flags = flags;
1180 link->reverse->flags = link->flags;
1185 int __media_entity_setup_link(struct media_link *link, u32 flags)
1187 const u32 mask = MEDIA_LNK_FL_ENABLED;
1188 struct media_device *mdev;
1189 struct media_pad *source, *sink;
1195 /* The non-modifiable link flags must not be modified. */
1196 if ((link->flags & ~mask) != (flags & ~mask))
1199 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
1200 return link->flags == flags ? 0 : -EINVAL;
1202 if (link->flags == flags)
1205 source = link->source;
1208 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
1209 (media_pad_is_streaming(source) || media_pad_is_streaming(sink)))
1212 mdev = source->graph_obj.mdev;
1214 if (mdev->ops && mdev->ops->link_notify) {
1215 ret = mdev->ops->link_notify(link, flags,
1216 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
1221 ret = __media_entity_setup_link_notify(link, flags);
1223 if (mdev->ops && mdev->ops->link_notify)
1224 mdev->ops->link_notify(link, flags,
1225 MEDIA_DEV_NOTIFY_POST_LINK_CH);
1229 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
1231 int media_entity_setup_link(struct media_link *link, u32 flags)
1235 mutex_lock(&link->graph_obj.mdev->graph_mutex);
1236 ret = __media_entity_setup_link(link, flags);
1237 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
1241 EXPORT_SYMBOL_GPL(media_entity_setup_link);
1244 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
1246 struct media_link *link;
1248 for_each_media_entity_data_link(source->entity, link) {
1249 if (link->source->entity == source->entity &&
1250 link->source->index == source->index &&
1251 link->sink->entity == sink->entity &&
1252 link->sink->index == sink->index)
1258 EXPORT_SYMBOL_GPL(media_entity_find_link);
1260 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad)
1262 struct media_link *link;
1264 for_each_media_entity_data_link(pad->entity, link) {
1265 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1268 if (link->source == pad)
1271 if (link->sink == pad)
1272 return link->source;
1278 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first);
1281 media_entity_remote_pad_unique(const struct media_entity *entity,
1284 struct media_pad *pad = NULL;
1285 struct media_link *link;
1287 list_for_each_entry(link, &entity->links, list) {
1288 struct media_pad *local_pad;
1289 struct media_pad *remote_pad;
1291 if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) !=
1292 MEDIA_LNK_FL_DATA_LINK) ||
1293 !(link->flags & MEDIA_LNK_FL_ENABLED))
1296 if (type == MEDIA_PAD_FL_SOURCE) {
1297 local_pad = link->sink;
1298 remote_pad = link->source;
1300 local_pad = link->source;
1301 remote_pad = link->sink;
1304 if (local_pad->entity == entity) {
1306 return ERR_PTR(-ENOTUNIQ);
1313 return ERR_PTR(-ENOLINK);
1317 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique);
1319 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad)
1321 struct media_pad *found_pad = NULL;
1322 struct media_link *link;
1324 list_for_each_entry(link, &pad->entity->links, list) {
1325 struct media_pad *remote_pad;
1327 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1330 if (link->sink == pad)
1331 remote_pad = link->source;
1332 else if (link->source == pad)
1333 remote_pad = link->sink;
1338 return ERR_PTR(-ENOTUNIQ);
1340 found_pad = remote_pad;
1344 return ERR_PTR(-ENOLINK);
1348 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique);
1350 int media_entity_get_fwnode_pad(struct media_entity *entity,
1351 struct fwnode_handle *fwnode,
1352 unsigned long direction_flags)
1354 struct fwnode_endpoint endpoint;
1358 if (!entity->ops || !entity->ops->get_fwnode_pad) {
1359 for (i = 0; i < entity->num_pads; i++) {
1360 if (entity->pads[i].flags & direction_flags)
1367 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
1371 ret = entity->ops->get_fwnode_pad(entity, &endpoint);
1375 if (ret >= entity->num_pads)
1378 if (!(entity->pads[ret].flags & direction_flags))
1383 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
1385 struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
1387 struct media_pad *pad;
1389 media_entity_for_each_pad(entity, pad) {
1396 EXPORT_SYMBOL_GPL(media_entity_pipeline);
1398 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
1402 EXPORT_SYMBOL_GPL(media_pad_pipeline);
1404 static void media_interface_init(struct media_device *mdev,
1405 struct media_interface *intf,
1407 u32 intf_type, u32 flags)
1409 intf->type = intf_type;
1410 intf->flags = flags;
1411 INIT_LIST_HEAD(&intf->links);
1413 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1416 /* Functions related to the media interface via device nodes */
1418 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
1419 u32 type, u32 flags,
1420 u32 major, u32 minor)
1422 struct media_intf_devnode *devnode;
1424 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
1428 devnode->major = major;
1429 devnode->minor = minor;
1431 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
1436 EXPORT_SYMBOL_GPL(media_devnode_create);
1438 void media_devnode_remove(struct media_intf_devnode *devnode)
1440 media_remove_intf_links(&devnode->intf);
1441 media_gobj_destroy(&devnode->intf.graph_obj);
1444 EXPORT_SYMBOL_GPL(media_devnode_remove);
1446 struct media_link *media_create_intf_link(struct media_entity *entity,
1447 struct media_interface *intf,
1450 struct media_link *link;
1452 link = media_add_link(&intf->links);
1457 link->entity = entity;
1458 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
1460 /* Initialize graph object embedded at the new link */
1461 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1466 EXPORT_SYMBOL_GPL(media_create_intf_link);
1468 void __media_remove_intf_link(struct media_link *link)
1470 list_del(&link->list);
1471 media_gobj_destroy(&link->graph_obj);
1474 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1476 void media_remove_intf_link(struct media_link *link)
1478 struct media_device *mdev = link->graph_obj.mdev;
1480 /* Do nothing if the intf is not registered. */
1484 mutex_lock(&mdev->graph_mutex);
1485 __media_remove_intf_link(link);
1486 mutex_unlock(&mdev->graph_mutex);
1488 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1490 void __media_remove_intf_links(struct media_interface *intf)
1492 struct media_link *link, *tmp;
1494 list_for_each_entry_safe(link, tmp, &intf->links, list)
1495 __media_remove_intf_link(link);
1498 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1500 void media_remove_intf_links(struct media_interface *intf)
1502 struct media_device *mdev = intf->graph_obj.mdev;
1504 /* Do nothing if the intf is not registered. */
1508 mutex_lock(&mdev->graph_mutex);
1509 __media_remove_intf_links(intf);
1510 mutex_unlock(&mdev->graph_mutex);
1512 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1514 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1515 struct media_entity *ancillary)
1517 struct media_link *link;
1519 link = media_add_link(&primary->links);
1521 return ERR_PTR(-ENOMEM);
1523 link->gobj0 = &primary->graph_obj;
1524 link->gobj1 = &ancillary->graph_obj;
1525 link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1526 MEDIA_LNK_FL_ANCILLARY_LINK;
1528 /* Initialize graph object embedded in the new link */
1529 media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1534 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
1536 struct media_link *__media_entity_next_link(struct media_entity *entity,
1537 struct media_link *link,
1538 unsigned long link_type)
1540 link = link ? list_next_entry(link, list)
1541 : list_first_entry(&entity->links, typeof(*link), list);
1543 list_for_each_entry_from(link, &entity->links, list)
1544 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
1549 EXPORT_SYMBOL_GPL(__media_entity_next_link);