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 * media_entity_has_pad_interdep - Check interdependency between two pads
232 * @entity: The entity
233 * @pad0: The first pad index
234 * @pad1: The second pad index
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.
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.
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.
249 * The caller must hold entity->graph_obj.mdev->mutex.
251 * Return: true if the pads are connected internally and false otherwise.
253 static bool media_entity_has_pad_interdep(struct media_entity *entity,
254 unsigned int pad0, unsigned int pad1)
256 if (pad0 >= entity->num_pads || pad1 >= entity->num_pads)
259 if (entity->pads[pad0].flags & entity->pads[pad1].flags &
260 (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE))
263 if (!entity->ops || !entity->ops->has_pad_interdep)
266 return entity->ops->has_pad_interdep(entity, pad0, pad1);
269 static struct media_entity *
270 media_entity_other(struct media_entity *entity, struct media_link *link)
272 if (link->source->entity == entity)
273 return link->sink->entity;
275 return link->source->entity;
278 /* push an entity to traversal stack */
279 static void stack_push(struct media_graph *graph,
280 struct media_entity *entity)
282 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
287 graph->stack[graph->top].link = entity->links.next;
288 graph->stack[graph->top].entity = entity;
291 static struct media_entity *stack_pop(struct media_graph *graph)
293 struct media_entity *entity;
295 entity = graph->stack[graph->top].entity;
301 #define link_top(en) ((en)->stack[(en)->top].link)
302 #define stack_top(en) ((en)->stack[(en)->top].entity)
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
309 * Reserve resources for graph walk in media device's current
310 * state. The memory must be released using
311 * media_graph_walk_cleanup().
313 * Returns error on failure, zero on success.
315 __must_check int media_graph_walk_init(
316 struct media_graph *graph, struct media_device *mdev)
318 return media_entity_enum_init(&graph->ent_enum, mdev);
320 EXPORT_SYMBOL_GPL(media_graph_walk_init);
323 * media_graph_walk_cleanup - Release resources related to graph walking
324 * @graph: Media graph structure that was used to walk the graph
326 void media_graph_walk_cleanup(struct media_graph *graph)
328 media_entity_enum_cleanup(&graph->ent_enum);
330 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
332 void media_graph_walk_start(struct media_graph *graph,
333 struct media_entity *entity)
335 media_entity_enum_zero(&graph->ent_enum);
336 media_entity_enum_set(&graph->ent_enum, entity);
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);
344 EXPORT_SYMBOL_GPL(media_graph_walk_start);
346 static void media_graph_walk_iter(struct media_graph *graph)
348 struct media_entity *entity = stack_top(graph);
349 struct media_link *link;
350 struct media_entity *next;
352 link = list_entry(link_top(graph), typeof(*link), list);
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;
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);
370 /* Get the entity at the other end of the link. */
371 next = media_entity_other(entity, link);
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",
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",
387 lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex);
390 struct media_entity *media_graph_walk_next(struct media_graph *graph)
392 struct media_entity *entity;
394 if (stack_top(graph) == NULL)
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
402 while (link_top(graph) != &stack_top(graph)->links)
403 media_graph_walk_iter(graph);
405 entity = stack_pop(graph);
406 dev_dbg(entity->graph_obj.mdev->dev,
407 "walk: returning entity '%s'\n", entity->name);
411 EXPORT_SYMBOL_GPL(media_graph_walk_next);
413 /* -----------------------------------------------------------------------------
414 * Pipeline management
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.
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.
431 * struct media_pipeline_walk_entry - Entry in the pipeline traversal stack
433 * @pad: The media pad being visited
434 * @links: Links left to be visited
436 struct media_pipeline_walk_entry {
437 struct media_pad *pad;
438 struct list_head *links;
442 * struct media_pipeline_walk - State used by the media pipeline traversal
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
451 struct media_pipeline_walk {
452 struct media_device *mdev;
457 struct media_pipeline_walk_entry *entries;
461 #define MEDIA_PIPELINE_STACK_GROW_STEP 16
463 static struct media_pipeline_walk_entry *
464 media_pipeline_walk_top(struct media_pipeline_walk *walk)
466 return &walk->stack.entries[walk->stack.top];
469 static bool media_pipeline_walk_empty(struct media_pipeline_walk *walk)
471 return walk->stack.top == -1;
474 /* Increase the stack size by MEDIA_PIPELINE_STACK_GROW_STEP elements. */
475 static int media_pipeline_walk_resize(struct media_pipeline_walk *walk)
477 struct media_pipeline_walk_entry *entries;
478 unsigned int new_size;
480 /* Safety check, to avoid stack overflows in case of bugs. */
481 if (walk->stack.size >= 256)
484 new_size = walk->stack.size + MEDIA_PIPELINE_STACK_GROW_STEP;
486 entries = krealloc(walk->stack.entries,
487 new_size * sizeof(*walk->stack.entries),
492 walk->stack.entries = entries;
493 walk->stack.size = new_size;
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)
502 struct media_pipeline_walk_entry *entry;
505 if (walk->stack.top + 1 >= walk->stack.size) {
506 ret = media_pipeline_walk_resize(walk);
512 entry = media_pipeline_walk_top(walk);
514 entry->links = pad->entity->links.next;
516 dev_dbg(walk->mdev->dev,
517 "media pipeline: pushed entry %u: '%s':%u\n",
518 walk->stack.top, pad->entity->name, pad->index);
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.
527 static void media_pipeline_walk_pop(struct media_pipeline_walk *walk)
529 struct media_pipeline_walk_entry *entry;
531 if (WARN_ON(walk->stack.top < 0))
534 entry = media_pipeline_walk_top(walk);
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",
545 entry->links = entry->links->next;
547 dev_dbg(walk->mdev->dev,
548 "media pipeline: moved entry %u to next link\n",
552 /* Free all memory allocated while walking the pipeline. */
553 static void media_pipeline_walk_destroy(struct media_pipeline_walk *walk)
555 kfree(walk->stack.entries);
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)
563 struct media_pipeline_pad *ppad;
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);
574 ppad = kzalloc(sizeof(*ppad), GFP_KERNEL);
581 list_add_tail(&ppad->list, &pipe->pads);
583 dev_dbg(pad->graph_obj.mdev->dev,
584 "media pipeline: added pad '%s':%u\n",
585 pad->entity->name, pad->index);
587 return media_pipeline_walk_push(walk, pad);
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)
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;
602 link = list_entry(entry->links, typeof(*link), list);
603 media_pipeline_walk_pop(walk);
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);
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");
617 /* Get the local pad and remote pad. */
618 if (link->source->entity == pad->entity) {
619 local = link->source;
623 remote = link->source;
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.
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");
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.
641 ret = media_pipeline_add_pad(pipe, walk, local);
645 ret = media_pipeline_add_pad(pipe, walk, remote);
652 static void media_pipeline_cleanup(struct media_pipeline *pipe)
654 while (!list_empty(&pipe->pads)) {
655 struct media_pipeline_pad *ppad;
657 ppad = list_first_entry(&pipe->pads, typeof(*ppad), list);
658 list_del(&ppad->list);
663 static int media_pipeline_populate(struct media_pipeline *pipe,
664 struct media_pad *pad)
666 struct media_pipeline_walk walk = { };
667 struct media_pipeline_pad *ppad;
671 * Populate the media pipeline by walking the media graph, starting
674 INIT_LIST_HEAD(&pipe->pads);
675 pipe->mdev = pad->graph_obj.mdev;
677 walk.mdev = pipe->mdev;
679 ret = media_pipeline_add_pad(pipe, &walk, pad);
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
690 while (!media_pipeline_walk_empty(&walk)) {
691 ret = media_pipeline_explore_next_link(pipe, &walk);
696 dev_dbg(pad->graph_obj.mdev->dev,
697 "media pipeline populated, found pads:\n");
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);
703 WARN_ON(walk.stack.top != -1);
708 media_pipeline_walk_destroy(&walk);
711 media_pipeline_cleanup(pipe);
716 __must_check int __media_pipeline_start(struct media_pad *pad,
717 struct media_pipeline *pipe)
719 struct media_device *mdev = pad->graph_obj.mdev;
720 struct media_pipeline_pad *err_ppad;
721 struct media_pipeline_pad *ppad;
724 lockdep_assert_held(&mdev->graph_mutex);
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().
730 if (WARN_ON(pad->pipe && pad->pipe != pipe))
734 * If the pipeline has already been started, it is guaranteed to be
735 * valid, so just increase the start count.
737 if (pipe->start_count) {
743 * Populate the pipeline. This populates the media_pipeline pads list
744 * with media_pipeline_pad instances for each pad found during graph
747 ret = media_pipeline_populate(pipe, pad);
752 * Now that all the pads in the pipeline have been gathered, perform
753 * the validation steps.
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;
763 dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name,
767 * 1. Ensure that the pad doesn't already belong to a different
771 dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n",
772 pad->entity->name, pad->index);
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.
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)
787 /* Record if the pad has links and enabled links. */
788 if (link->flags & MEDIA_LNK_FL_ENABLED)
789 has_enabled_link = true;
793 * Validate the link if it's enabled and has the
794 * current pad as its sink.
796 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
799 if (link->sink != pad)
802 if (!entity->ops || !entity->ops->link_validate)
805 ret = entity->ops->link_validate(link);
808 "Link '%s':%u -> '%s':%u failed validation: %d\n",
809 link->source->entity->name,
811 link->sink->entity->name,
812 link->sink->index, ret);
817 "Link '%s':%u -> '%s':%u is valid\n",
818 link->source->entity->name,
820 link->sink->entity->name,
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.
828 if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) && has_link &&
831 "Pad '%s':%u must be connected by an enabled link\n",
832 pad->entity->name, pad->index);
837 /* Validation passed, store the pipe pointer in the pad. */
847 * Link validation on graph failed. We revert what we did and
851 list_for_each_entry(err_ppad, &pipe->pads, list) {
852 if (err_ppad == ppad)
855 err_ppad->pad->pipe = NULL;
858 media_pipeline_cleanup(pipe);
862 EXPORT_SYMBOL_GPL(__media_pipeline_start);
864 __must_check int media_pipeline_start(struct media_pad *pad,
865 struct media_pipeline *pipe)
867 struct media_device *mdev = pad->graph_obj.mdev;
870 mutex_lock(&mdev->graph_mutex);
871 ret = __media_pipeline_start(pad, pipe);
872 mutex_unlock(&mdev->graph_mutex);
875 EXPORT_SYMBOL_GPL(media_pipeline_start);
877 void __media_pipeline_stop(struct media_pad *pad)
879 struct media_pipeline *pipe = pad->pipe;
880 struct media_pipeline_pad *ppad;
883 * If the following check fails, the driver has performed an
884 * unbalanced call to media_pipeline_stop()
889 if (--pipe->start_count)
892 list_for_each_entry(ppad, &pipe->pads, list)
893 ppad->pad->pipe = NULL;
895 media_pipeline_cleanup(pipe);
900 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
902 void media_pipeline_stop(struct media_pad *pad)
904 struct media_device *mdev = pad->graph_obj.mdev;
906 mutex_lock(&mdev->graph_mutex);
907 __media_pipeline_stop(pad);
908 mutex_unlock(&mdev->graph_mutex);
910 EXPORT_SYMBOL_GPL(media_pipeline_stop);
912 __must_check int media_pipeline_alloc_start(struct media_pad *pad)
914 struct media_device *mdev = pad->graph_obj.mdev;
915 struct media_pipeline *new_pipe = NULL;
916 struct media_pipeline *pipe;
919 mutex_lock(&mdev->graph_mutex);
922 * Is the pad already part of a pipeline? If not, we need to allocate
925 pipe = media_pad_pipeline(pad);
927 new_pipe = kzalloc(sizeof(*new_pipe), GFP_KERNEL);
934 pipe->allocated = true;
937 ret = __media_pipeline_start(pad, pipe);
942 mutex_unlock(&mdev->graph_mutex);
946 EXPORT_SYMBOL_GPL(media_pipeline_alloc_start);
949 __media_pipeline_pad_iter_next(struct media_pipeline *pipe,
950 struct media_pipeline_pad_iter *iter,
951 struct media_pad *pad)
954 iter->cursor = pipe->pads.next;
956 if (iter->cursor == &pipe->pads)
959 pad = list_entry(iter->cursor, struct media_pipeline_pad, list)->pad;
960 iter->cursor = iter->cursor->next;
964 EXPORT_SYMBOL_GPL(__media_pipeline_pad_iter_next);
966 int media_pipeline_entity_iter_init(struct media_pipeline *pipe,
967 struct media_pipeline_entity_iter *iter)
969 return media_entity_enum_init(&iter->ent_enum, pipe->mdev);
971 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_init);
973 void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter)
975 media_entity_enum_cleanup(&iter->ent_enum);
977 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_cleanup);
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)
985 iter->cursor = pipe->pads.next;
987 while (iter->cursor != &pipe->pads) {
988 struct media_pipeline_pad *ppad;
989 struct media_entity *entity;
991 ppad = list_entry(iter->cursor, struct media_pipeline_pad, list);
992 entity = ppad->pad->entity;
993 iter->cursor = iter->cursor->next;
995 if (!media_entity_enum_test_and_set(&iter->ent_enum, entity))
1001 EXPORT_SYMBOL_GPL(__media_pipeline_entity_iter_next);
1003 /* -----------------------------------------------------------------------------
1007 static struct media_link *media_add_link(struct list_head *head)
1009 struct media_link *link;
1011 link = kzalloc(sizeof(*link), GFP_KERNEL);
1015 list_add_tail(&link->list, head);
1020 static void __media_entity_remove_link(struct media_entity *entity,
1021 struct media_link *link)
1023 struct media_link *rlink, *tmp;
1024 struct media_entity *remote;
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;
1031 remote = link->source->entity;
1033 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
1034 if (rlink != link->reverse)
1037 if (link->source->entity == entity)
1038 remote->num_backlinks--;
1040 /* Remove the remote link */
1041 list_del(&rlink->list);
1042 media_gobj_destroy(&rlink->graph_obj);
1045 if (--remote->num_links == 0)
1050 list_del(&link->list);
1051 media_gobj_destroy(&link->graph_obj);
1055 int media_get_pad_index(struct media_entity *entity, bool is_sink,
1056 enum media_pad_signal_type sig_type)
1064 for (i = 0; i < entity->num_pads; i++) {
1065 if (entity->pads[i].flags & MEDIA_PAD_FL_SINK)
1067 else if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
1068 pad_is_sink = false;
1070 continue; /* This is an error! */
1072 if (pad_is_sink != is_sink)
1074 if (entity->pads[i].sig_type == sig_type)
1079 EXPORT_SYMBOL_GPL(media_get_pad_index);
1082 media_create_pad_link(struct media_entity *source, u16 source_pad,
1083 struct media_entity *sink, u16 sink_pad, u32 flags)
1085 struct media_link *link;
1086 struct media_link *backlink;
1088 if (WARN_ON(!source || !sink) ||
1089 WARN_ON(source_pad >= source->num_pads) ||
1090 WARN_ON(sink_pad >= sink->num_pads))
1092 if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
1094 if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
1097 link = media_add_link(&source->links);
1101 link->source = &source->pads[source_pad];
1102 link->sink = &sink->pads[sink_pad];
1103 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
1105 /* Initialize graph object embedded at the new link */
1106 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
1109 /* Create the backlink. Backlinks are used to help graph traversal and
1110 * are not reported to userspace.
1112 backlink = media_add_link(&sink->links);
1113 if (backlink == NULL) {
1114 __media_entity_remove_link(source, link);
1118 backlink->source = &source->pads[source_pad];
1119 backlink->sink = &sink->pads[sink_pad];
1120 backlink->flags = flags;
1121 backlink->is_backlink = true;
1123 /* Initialize graph object embedded at the new link */
1124 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
1125 &backlink->graph_obj);
1127 link->reverse = backlink;
1128 backlink->reverse = link;
1130 sink->num_backlinks++;
1132 source->num_links++;
1136 EXPORT_SYMBOL_GPL(media_create_pad_link);
1138 int media_create_pad_links(const struct media_device *mdev,
1139 const u32 source_function,
1140 struct media_entity *source,
1141 const u16 source_pad,
1142 const u32 sink_function,
1143 struct media_entity *sink,
1146 const bool allow_both_undefined)
1148 struct media_entity *entity;
1152 /* Trivial case: 1:1 relation */
1154 return media_create_pad_link(source, source_pad,
1155 sink, sink_pad, flags);
1157 /* Worse case scenario: n:n relation */
1158 if (!source && !sink) {
1159 if (!allow_both_undefined)
1161 media_device_for_each_entity(source, mdev) {
1162 if (source->function != source_function)
1164 media_device_for_each_entity(sink, mdev) {
1165 if (sink->function != sink_function)
1167 ret = media_create_pad_link(source, source_pad,
1172 flags &= ~(MEDIA_LNK_FL_ENABLED |
1173 MEDIA_LNK_FL_IMMUTABLE);
1179 /* Handle 1:n and n:1 cases */
1181 function = sink_function;
1183 function = source_function;
1185 media_device_for_each_entity(entity, mdev) {
1186 if (entity->function != function)
1190 ret = media_create_pad_link(source, source_pad,
1191 entity, sink_pad, flags);
1193 ret = media_create_pad_link(entity, source_pad,
1194 sink, sink_pad, flags);
1197 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1201 EXPORT_SYMBOL_GPL(media_create_pad_links);
1203 void __media_entity_remove_links(struct media_entity *entity)
1205 struct media_link *link, *tmp;
1207 list_for_each_entry_safe(link, tmp, &entity->links, list)
1208 __media_entity_remove_link(entity, link);
1210 entity->num_links = 0;
1211 entity->num_backlinks = 0;
1213 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
1215 void media_entity_remove_links(struct media_entity *entity)
1217 struct media_device *mdev = entity->graph_obj.mdev;
1219 /* Do nothing if the entity is not registered. */
1223 mutex_lock(&mdev->graph_mutex);
1224 __media_entity_remove_links(entity);
1225 mutex_unlock(&mdev->graph_mutex);
1227 EXPORT_SYMBOL_GPL(media_entity_remove_links);
1229 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
1233 /* Notify both entities. */
1234 ret = media_entity_call(link->source->entity, link_setup,
1235 link->source, link->sink, flags);
1236 if (ret < 0 && ret != -ENOIOCTLCMD)
1239 ret = media_entity_call(link->sink->entity, link_setup,
1240 link->sink, link->source, flags);
1241 if (ret < 0 && ret != -ENOIOCTLCMD) {
1242 media_entity_call(link->source->entity, link_setup,
1243 link->source, link->sink, link->flags);
1247 link->flags = flags;
1248 link->reverse->flags = link->flags;
1253 int __media_entity_setup_link(struct media_link *link, u32 flags)
1255 const u32 mask = MEDIA_LNK_FL_ENABLED;
1256 struct media_device *mdev;
1257 struct media_pad *source, *sink;
1263 /* The non-modifiable link flags must not be modified. */
1264 if ((link->flags & ~mask) != (flags & ~mask))
1267 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
1268 return link->flags == flags ? 0 : -EINVAL;
1270 if (link->flags == flags)
1273 source = link->source;
1276 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
1277 (media_pad_is_streaming(source) || media_pad_is_streaming(sink)))
1280 mdev = source->graph_obj.mdev;
1282 if (mdev->ops && mdev->ops->link_notify) {
1283 ret = mdev->ops->link_notify(link, flags,
1284 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
1289 ret = __media_entity_setup_link_notify(link, flags);
1291 if (mdev->ops && mdev->ops->link_notify)
1292 mdev->ops->link_notify(link, flags,
1293 MEDIA_DEV_NOTIFY_POST_LINK_CH);
1297 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
1299 int media_entity_setup_link(struct media_link *link, u32 flags)
1303 mutex_lock(&link->graph_obj.mdev->graph_mutex);
1304 ret = __media_entity_setup_link(link, flags);
1305 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
1309 EXPORT_SYMBOL_GPL(media_entity_setup_link);
1312 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
1314 struct media_link *link;
1316 for_each_media_entity_data_link(source->entity, link) {
1317 if (link->source->entity == source->entity &&
1318 link->source->index == source->index &&
1319 link->sink->entity == sink->entity &&
1320 link->sink->index == sink->index)
1326 EXPORT_SYMBOL_GPL(media_entity_find_link);
1328 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad)
1330 struct media_link *link;
1332 for_each_media_entity_data_link(pad->entity, link) {
1333 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1336 if (link->source == pad)
1339 if (link->sink == pad)
1340 return link->source;
1346 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first);
1349 media_entity_remote_pad_unique(const struct media_entity *entity,
1352 struct media_pad *pad = NULL;
1353 struct media_link *link;
1355 list_for_each_entry(link, &entity->links, list) {
1356 struct media_pad *local_pad;
1357 struct media_pad *remote_pad;
1359 if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) !=
1360 MEDIA_LNK_FL_DATA_LINK) ||
1361 !(link->flags & MEDIA_LNK_FL_ENABLED))
1364 if (type == MEDIA_PAD_FL_SOURCE) {
1365 local_pad = link->sink;
1366 remote_pad = link->source;
1368 local_pad = link->source;
1369 remote_pad = link->sink;
1372 if (local_pad->entity == entity) {
1374 return ERR_PTR(-ENOTUNIQ);
1381 return ERR_PTR(-ENOLINK);
1385 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique);
1387 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad)
1389 struct media_pad *found_pad = NULL;
1390 struct media_link *link;
1392 list_for_each_entry(link, &pad->entity->links, list) {
1393 struct media_pad *remote_pad;
1395 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1398 if (link->sink == pad)
1399 remote_pad = link->source;
1400 else if (link->source == pad)
1401 remote_pad = link->sink;
1406 return ERR_PTR(-ENOTUNIQ);
1408 found_pad = remote_pad;
1412 return ERR_PTR(-ENOLINK);
1416 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique);
1418 int media_entity_get_fwnode_pad(struct media_entity *entity,
1419 struct fwnode_handle *fwnode,
1420 unsigned long direction_flags)
1422 struct fwnode_endpoint endpoint;
1426 if (!entity->ops || !entity->ops->get_fwnode_pad) {
1427 for (i = 0; i < entity->num_pads; i++) {
1428 if (entity->pads[i].flags & direction_flags)
1435 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
1439 ret = entity->ops->get_fwnode_pad(entity, &endpoint);
1443 if (ret >= entity->num_pads)
1446 if (!(entity->pads[ret].flags & direction_flags))
1451 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
1453 struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
1455 struct media_pad *pad;
1457 media_entity_for_each_pad(entity, pad) {
1464 EXPORT_SYMBOL_GPL(media_entity_pipeline);
1466 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
1470 EXPORT_SYMBOL_GPL(media_pad_pipeline);
1472 static void media_interface_init(struct media_device *mdev,
1473 struct media_interface *intf,
1475 u32 intf_type, u32 flags)
1477 intf->type = intf_type;
1478 intf->flags = flags;
1479 INIT_LIST_HEAD(&intf->links);
1481 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1484 /* Functions related to the media interface via device nodes */
1486 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
1487 u32 type, u32 flags,
1488 u32 major, u32 minor)
1490 struct media_intf_devnode *devnode;
1492 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
1496 devnode->major = major;
1497 devnode->minor = minor;
1499 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
1504 EXPORT_SYMBOL_GPL(media_devnode_create);
1506 void media_devnode_remove(struct media_intf_devnode *devnode)
1508 media_remove_intf_links(&devnode->intf);
1509 media_gobj_destroy(&devnode->intf.graph_obj);
1512 EXPORT_SYMBOL_GPL(media_devnode_remove);
1514 struct media_link *media_create_intf_link(struct media_entity *entity,
1515 struct media_interface *intf,
1518 struct media_link *link;
1520 link = media_add_link(&intf->links);
1525 link->entity = entity;
1526 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
1528 /* Initialize graph object embedded at the new link */
1529 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1534 EXPORT_SYMBOL_GPL(media_create_intf_link);
1536 void __media_remove_intf_link(struct media_link *link)
1538 list_del(&link->list);
1539 media_gobj_destroy(&link->graph_obj);
1542 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1544 void media_remove_intf_link(struct media_link *link)
1546 struct media_device *mdev = link->graph_obj.mdev;
1548 /* Do nothing if the intf is not registered. */
1552 mutex_lock(&mdev->graph_mutex);
1553 __media_remove_intf_link(link);
1554 mutex_unlock(&mdev->graph_mutex);
1556 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1558 void __media_remove_intf_links(struct media_interface *intf)
1560 struct media_link *link, *tmp;
1562 list_for_each_entry_safe(link, tmp, &intf->links, list)
1563 __media_remove_intf_link(link);
1566 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1568 void media_remove_intf_links(struct media_interface *intf)
1570 struct media_device *mdev = intf->graph_obj.mdev;
1572 /* Do nothing if the intf is not registered. */
1576 mutex_lock(&mdev->graph_mutex);
1577 __media_remove_intf_links(intf);
1578 mutex_unlock(&mdev->graph_mutex);
1580 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1582 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1583 struct media_entity *ancillary)
1585 struct media_link *link;
1587 link = media_add_link(&primary->links);
1589 return ERR_PTR(-ENOMEM);
1591 link->gobj0 = &primary->graph_obj;
1592 link->gobj1 = &ancillary->graph_obj;
1593 link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1594 MEDIA_LNK_FL_ANCILLARY_LINK;
1596 /* Initialize graph object embedded in the new link */
1597 media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1602 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
1604 struct media_link *__media_entity_next_link(struct media_entity *entity,
1605 struct media_link *link,
1606 unsigned long link_type)
1608 link = link ? list_next_entry(link, list)
1609 : list_first_entry(&entity->links, typeof(*link), list);
1611 list_for_each_entry_from(link, &entity->links, list)
1612 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
1617 EXPORT_SYMBOL_GPL(__media_entity_next_link);