GNU Linux-libre 6.8.7-gnu
[releases.git] / drivers / media / mc / mc-entity.c
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         int ret = 0;
201
202         if (num_pads >= MEDIA_ENTITY_MAX_PADS)
203                 return -E2BIG;
204
205         entity->num_pads = num_pads;
206         entity->pads = pads;
207
208         if (mdev)
209                 mutex_lock(&mdev->graph_mutex);
210
211         media_entity_for_each_pad(entity, iter) {
212                 iter->entity = entity;
213                 iter->index = i++;
214
215                 if (hweight32(iter->flags & (MEDIA_PAD_FL_SINK |
216                                              MEDIA_PAD_FL_SOURCE)) != 1) {
217                         ret = -EINVAL;
218                         break;
219                 }
220
221                 if (mdev)
222                         media_gobj_create(mdev, MEDIA_GRAPH_PAD,
223                                           &iter->graph_obj);
224         }
225
226         if (ret && mdev) {
227                 media_entity_for_each_pad(entity, iter)
228                         media_gobj_destroy(&iter->graph_obj);
229         }
230
231         if (mdev)
232                 mutex_unlock(&mdev->graph_mutex);
233
234         return ret;
235 }
236 EXPORT_SYMBOL_GPL(media_entity_pads_init);
237
238 /* -----------------------------------------------------------------------------
239  * Graph traversal
240  */
241
242 /**
243  * media_entity_has_pad_interdep - Check interdependency between two pads
244  *
245  * @entity: The entity
246  * @pad0: The first pad index
247  * @pad1: The second pad index
248  *
249  * This function checks the interdependency inside the entity between @pad0
250  * and @pad1. If two pads are interdependent they are part of the same pipeline
251  * and enabling one of the pads means that the other pad will become "locked"
252  * and doesn't allow configuration changes.
253  *
254  * This function uses the &media_entity_operations.has_pad_interdep() operation
255  * to check the dependency inside the entity between @pad0 and @pad1. If the
256  * has_pad_interdep operation is not implemented, all pads of the entity are
257  * considered to be interdependent.
258  *
259  * One of @pad0 and @pad1 must be a sink pad and the other one a source pad.
260  * The function returns false if both pads are sinks or sources.
261  *
262  * The caller must hold entity->graph_obj.mdev->mutex.
263  *
264  * Return: true if the pads are connected internally and false otherwise.
265  */
266 static bool media_entity_has_pad_interdep(struct media_entity *entity,
267                                           unsigned int pad0, unsigned int pad1)
268 {
269         if (pad0 >= entity->num_pads || pad1 >= entity->num_pads)
270                 return false;
271
272         if (entity->pads[pad0].flags & entity->pads[pad1].flags &
273             (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE))
274                 return false;
275
276         if (!entity->ops || !entity->ops->has_pad_interdep)
277                 return true;
278
279         return entity->ops->has_pad_interdep(entity, pad0, pad1);
280 }
281
282 static struct media_entity *
283 media_entity_other(struct media_entity *entity, struct media_link *link)
284 {
285         if (link->source->entity == entity)
286                 return link->sink->entity;
287         else
288                 return link->source->entity;
289 }
290
291 /* push an entity to traversal stack */
292 static void stack_push(struct media_graph *graph,
293                        struct media_entity *entity)
294 {
295         if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
296                 WARN_ON(1);
297                 return;
298         }
299         graph->top++;
300         graph->stack[graph->top].link = entity->links.next;
301         graph->stack[graph->top].entity = entity;
302 }
303
304 static struct media_entity *stack_pop(struct media_graph *graph)
305 {
306         struct media_entity *entity;
307
308         entity = graph->stack[graph->top].entity;
309         graph->top--;
310
311         return entity;
312 }
313
314 #define link_top(en)    ((en)->stack[(en)->top].link)
315 #define stack_top(en)   ((en)->stack[(en)->top].entity)
316
317 /**
318  * media_graph_walk_init - Allocate resources for graph walk
319  * @graph: Media graph structure that will be used to walk the graph
320  * @mdev: Media device
321  *
322  * Reserve resources for graph walk in media device's current
323  * state. The memory must be released using
324  * media_graph_walk_cleanup().
325  *
326  * Returns error on failure, zero on success.
327  */
328 __must_check int media_graph_walk_init(
329         struct media_graph *graph, struct media_device *mdev)
330 {
331         return media_entity_enum_init(&graph->ent_enum, mdev);
332 }
333 EXPORT_SYMBOL_GPL(media_graph_walk_init);
334
335 /**
336  * media_graph_walk_cleanup - Release resources related to graph walking
337  * @graph: Media graph structure that was used to walk the graph
338  */
339 void media_graph_walk_cleanup(struct media_graph *graph)
340 {
341         media_entity_enum_cleanup(&graph->ent_enum);
342 }
343 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
344
345 void media_graph_walk_start(struct media_graph *graph,
346                             struct media_entity *entity)
347 {
348         media_entity_enum_zero(&graph->ent_enum);
349         media_entity_enum_set(&graph->ent_enum, entity);
350
351         graph->top = 0;
352         graph->stack[graph->top].entity = NULL;
353         stack_push(graph, entity);
354         dev_dbg(entity->graph_obj.mdev->dev,
355                 "begin graph walk at '%s'\n", entity->name);
356 }
357 EXPORT_SYMBOL_GPL(media_graph_walk_start);
358
359 static void media_graph_walk_iter(struct media_graph *graph)
360 {
361         struct media_entity *entity = stack_top(graph);
362         struct media_link *link;
363         struct media_entity *next;
364
365         link = list_entry(link_top(graph), typeof(*link), list);
366
367         /* If the link is not a data link, don't follow it */
368         if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) {
369                 link_top(graph) = link_top(graph)->next;
370                 return;
371         }
372
373         /* The link is not enabled so we do not follow. */
374         if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
375                 link_top(graph) = link_top(graph)->next;
376                 dev_dbg(entity->graph_obj.mdev->dev,
377                         "walk: skipping disabled link '%s':%u -> '%s':%u\n",
378                         link->source->entity->name, link->source->index,
379                         link->sink->entity->name, link->sink->index);
380                 return;
381         }
382
383         /* Get the entity at the other end of the link. */
384         next = media_entity_other(entity, link);
385
386         /* Has the entity already been visited? */
387         if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
388                 link_top(graph) = link_top(graph)->next;
389                 dev_dbg(entity->graph_obj.mdev->dev,
390                         "walk: skipping entity '%s' (already seen)\n",
391                         next->name);
392                 return;
393         }
394
395         /* Push the new entity to stack and start over. */
396         link_top(graph) = link_top(graph)->next;
397         stack_push(graph, next);
398         dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
399                 next->name);
400         lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex);
401 }
402
403 struct media_entity *media_graph_walk_next(struct media_graph *graph)
404 {
405         struct media_entity *entity;
406
407         if (stack_top(graph) == NULL)
408                 return NULL;
409
410         /*
411          * Depth first search. Push entity to stack and continue from
412          * top of the stack until no more entities on the level can be
413          * found.
414          */
415         while (link_top(graph) != &stack_top(graph)->links)
416                 media_graph_walk_iter(graph);
417
418         entity = stack_pop(graph);
419         dev_dbg(entity->graph_obj.mdev->dev,
420                 "walk: returning entity '%s'\n", entity->name);
421
422         return entity;
423 }
424 EXPORT_SYMBOL_GPL(media_graph_walk_next);
425
426 /* -----------------------------------------------------------------------------
427  * Pipeline management
428  */
429
430 /*
431  * The pipeline traversal stack stores pads that are reached during graph
432  * traversal, with a list of links to be visited to continue the traversal.
433  * When a new pad is reached, an entry is pushed on the top of the stack and
434  * points to the incoming pad and the first link of the entity.
435  *
436  * To find further pads in the pipeline, the traversal algorithm follows
437  * internal pad dependencies in the entity, and then links in the graph. It
438  * does so by iterating over all links of the entity, and following enabled
439  * links that originate from a pad that is internally connected to the incoming
440  * pad, as reported by the media_entity_has_pad_interdep() function.
441  */
442
443 /**
444  * struct media_pipeline_walk_entry - Entry in the pipeline traversal stack
445  *
446  * @pad: The media pad being visited
447  * @links: Links left to be visited
448  */
449 struct media_pipeline_walk_entry {
450         struct media_pad *pad;
451         struct list_head *links;
452 };
453
454 /**
455  * struct media_pipeline_walk - State used by the media pipeline traversal
456  *                              algorithm
457  *
458  * @mdev: The media device
459  * @stack: Depth-first search stack
460  * @stack.size: Number of allocated entries in @stack.entries
461  * @stack.top: Index of the top stack entry (-1 if the stack is empty)
462  * @stack.entries: Stack entries
463  */
464 struct media_pipeline_walk {
465         struct media_device *mdev;
466
467         struct {
468                 unsigned int size;
469                 int top;
470                 struct media_pipeline_walk_entry *entries;
471         } stack;
472 };
473
474 #define MEDIA_PIPELINE_STACK_GROW_STEP          16
475
476 static struct media_pipeline_walk_entry *
477 media_pipeline_walk_top(struct media_pipeline_walk *walk)
478 {
479         return &walk->stack.entries[walk->stack.top];
480 }
481
482 static bool media_pipeline_walk_empty(struct media_pipeline_walk *walk)
483 {
484         return walk->stack.top == -1;
485 }
486
487 /* Increase the stack size by MEDIA_PIPELINE_STACK_GROW_STEP elements. */
488 static int media_pipeline_walk_resize(struct media_pipeline_walk *walk)
489 {
490         struct media_pipeline_walk_entry *entries;
491         unsigned int new_size;
492
493         /* Safety check, to avoid stack overflows in case of bugs. */
494         if (walk->stack.size >= 256)
495                 return -E2BIG;
496
497         new_size = walk->stack.size + MEDIA_PIPELINE_STACK_GROW_STEP;
498
499         entries = krealloc(walk->stack.entries,
500                            new_size * sizeof(*walk->stack.entries),
501                            GFP_KERNEL);
502         if (!entries)
503                 return -ENOMEM;
504
505         walk->stack.entries = entries;
506         walk->stack.size = new_size;
507
508         return 0;
509 }
510
511 /* Push a new entry on the stack. */
512 static int media_pipeline_walk_push(struct media_pipeline_walk *walk,
513                                     struct media_pad *pad)
514 {
515         struct media_pipeline_walk_entry *entry;
516         int ret;
517
518         if (walk->stack.top + 1 >= walk->stack.size) {
519                 ret = media_pipeline_walk_resize(walk);
520                 if (ret)
521                         return ret;
522         }
523
524         walk->stack.top++;
525         entry = media_pipeline_walk_top(walk);
526         entry->pad = pad;
527         entry->links = pad->entity->links.next;
528
529         dev_dbg(walk->mdev->dev,
530                 "media pipeline: pushed entry %u: '%s':%u\n",
531                 walk->stack.top, pad->entity->name, pad->index);
532
533         return 0;
534 }
535
536 /*
537  * Move the top entry link cursor to the next link. If all links of the entry
538  * have been visited, pop the entry itself. Return true if the entry has been
539  * popped.
540  */
541 static bool media_pipeline_walk_pop(struct media_pipeline_walk *walk)
542 {
543         struct media_pipeline_walk_entry *entry;
544
545         if (WARN_ON(walk->stack.top < 0))
546                 return false;
547
548         entry = media_pipeline_walk_top(walk);
549
550         if (entry->links->next == &entry->pad->entity->links) {
551                 dev_dbg(walk->mdev->dev,
552                         "media pipeline: entry %u has no more links, popping\n",
553                         walk->stack.top);
554
555                 walk->stack.top--;
556                 return true;
557         }
558
559         entry->links = entry->links->next;
560
561         dev_dbg(walk->mdev->dev,
562                 "media pipeline: moved entry %u to next link\n",
563                 walk->stack.top);
564
565         return false;
566 }
567
568 /* Free all memory allocated while walking the pipeline. */
569 static void media_pipeline_walk_destroy(struct media_pipeline_walk *walk)
570 {
571         kfree(walk->stack.entries);
572 }
573
574 /* Add a pad to the pipeline and push it to the stack. */
575 static int media_pipeline_add_pad(struct media_pipeline *pipe,
576                                   struct media_pipeline_walk *walk,
577                                   struct media_pad *pad)
578 {
579         struct media_pipeline_pad *ppad;
580
581         list_for_each_entry(ppad, &pipe->pads, list) {
582                 if (ppad->pad == pad) {
583                         dev_dbg(pad->graph_obj.mdev->dev,
584                                 "media pipeline: already contains pad '%s':%u\n",
585                                 pad->entity->name, pad->index);
586                         return 0;
587                 }
588         }
589
590         ppad = kzalloc(sizeof(*ppad), GFP_KERNEL);
591         if (!ppad)
592                 return -ENOMEM;
593
594         ppad->pipe = pipe;
595         ppad->pad = pad;
596
597         list_add_tail(&ppad->list, &pipe->pads);
598
599         dev_dbg(pad->graph_obj.mdev->dev,
600                 "media pipeline: added pad '%s':%u\n",
601                 pad->entity->name, pad->index);
602
603         return media_pipeline_walk_push(walk, pad);
604 }
605
606 /* Explore the next link of the entity at the top of the stack. */
607 static int media_pipeline_explore_next_link(struct media_pipeline *pipe,
608                                             struct media_pipeline_walk *walk)
609 {
610         struct media_pipeline_walk_entry *entry = media_pipeline_walk_top(walk);
611         struct media_pad *origin;
612         struct media_link *link;
613         struct media_pad *local;
614         struct media_pad *remote;
615         bool last_link;
616         int ret;
617
618         origin = entry->pad;
619         link = list_entry(entry->links, typeof(*link), list);
620         last_link = media_pipeline_walk_pop(walk);
621
622         dev_dbg(walk->mdev->dev,
623                 "media pipeline: exploring link '%s':%u -> '%s':%u\n",
624                 link->source->entity->name, link->source->index,
625                 link->sink->entity->name, link->sink->index);
626
627         /* Get the local pad and remote pad. */
628         if (link->source->entity == origin->entity) {
629                 local = link->source;
630                 remote = link->sink;
631         } else {
632                 local = link->sink;
633                 remote = link->source;
634         }
635
636         /*
637          * Skip links that originate from a different pad than the incoming pad
638          * that is not connected internally in the entity to the incoming pad.
639          */
640         if (origin != local &&
641             !media_entity_has_pad_interdep(origin->entity, origin->index,
642                                            local->index)) {
643                 dev_dbg(walk->mdev->dev,
644                         "media pipeline: skipping link (no route)\n");
645                 goto done;
646         }
647
648         /*
649          * Add the local pad of the link to the pipeline and push it to the
650          * stack, if not already present.
651          */
652         ret = media_pipeline_add_pad(pipe, walk, local);
653         if (ret)
654                 return ret;
655
656         /* Similarly, add the remote pad, but only if the link is enabled. */
657         if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
658                 dev_dbg(walk->mdev->dev,
659                         "media pipeline: skipping link (disabled)\n");
660                 goto done;
661         }
662
663         ret = media_pipeline_add_pad(pipe, walk, remote);
664         if (ret)
665                 return ret;
666
667 done:
668         /*
669          * If we're done iterating over links, iterate over pads of the entity.
670          * This is necessary to discover pads that are not connected with any
671          * link. Those are dead ends from a pipeline exploration point of view,
672          * but are still part of the pipeline and need to be added to enable
673          * proper validation.
674          */
675         if (!last_link)
676                 return 0;
677
678         dev_dbg(walk->mdev->dev,
679                 "media pipeline: adding unconnected pads of '%s'\n",
680                 local->entity->name);
681
682         media_entity_for_each_pad(origin->entity, local) {
683                 /*
684                  * Skip the origin pad (already handled), pad that have links
685                  * (already discovered through iterating over links) and pads
686                  * not internally connected.
687                  */
688                 if (origin == local || !local->num_links ||
689                     !media_entity_has_pad_interdep(origin->entity, origin->index,
690                                                    local->index))
691                         continue;
692
693                 ret = media_pipeline_add_pad(pipe, walk, local);
694                 if (ret)
695                         return ret;
696         }
697
698         return 0;
699 }
700
701 static void media_pipeline_cleanup(struct media_pipeline *pipe)
702 {
703         while (!list_empty(&pipe->pads)) {
704                 struct media_pipeline_pad *ppad;
705
706                 ppad = list_first_entry(&pipe->pads, typeof(*ppad), list);
707                 list_del(&ppad->list);
708                 kfree(ppad);
709         }
710 }
711
712 static int media_pipeline_populate(struct media_pipeline *pipe,
713                                    struct media_pad *pad)
714 {
715         struct media_pipeline_walk walk = { };
716         struct media_pipeline_pad *ppad;
717         int ret;
718
719         /*
720          * Populate the media pipeline by walking the media graph, starting
721          * from @pad.
722          */
723         INIT_LIST_HEAD(&pipe->pads);
724         pipe->mdev = pad->graph_obj.mdev;
725
726         walk.mdev = pipe->mdev;
727         walk.stack.top = -1;
728         ret = media_pipeline_add_pad(pipe, &walk, pad);
729         if (ret)
730                 goto done;
731
732         /*
733          * Use a depth-first search algorithm: as long as the stack is not
734          * empty, explore the next link of the top entry. The
735          * media_pipeline_explore_next_link() function will either move to the
736          * next link, pop the entry if fully visited, or add new entries on
737          * top.
738          */
739         while (!media_pipeline_walk_empty(&walk)) {
740                 ret = media_pipeline_explore_next_link(pipe, &walk);
741                 if (ret)
742                         goto done;
743         }
744
745         dev_dbg(pad->graph_obj.mdev->dev,
746                 "media pipeline populated, found pads:\n");
747
748         list_for_each_entry(ppad, &pipe->pads, list)
749                 dev_dbg(pad->graph_obj.mdev->dev, "- '%s':%u\n",
750                         ppad->pad->entity->name, ppad->pad->index);
751
752         WARN_ON(walk.stack.top != -1);
753
754         ret = 0;
755
756 done:
757         media_pipeline_walk_destroy(&walk);
758
759         if (ret)
760                 media_pipeline_cleanup(pipe);
761
762         return ret;
763 }
764
765 __must_check int __media_pipeline_start(struct media_pad *pad,
766                                         struct media_pipeline *pipe)
767 {
768         struct media_device *mdev = pad->graph_obj.mdev;
769         struct media_pipeline_pad *err_ppad;
770         struct media_pipeline_pad *ppad;
771         int ret;
772
773         lockdep_assert_held(&mdev->graph_mutex);
774
775         /*
776          * If the pad is already part of a pipeline, that pipeline must be the
777          * same as the pipe given to media_pipeline_start().
778          */
779         if (WARN_ON(pad->pipe && pad->pipe != pipe))
780                 return -EINVAL;
781
782         /*
783          * If the pipeline has already been started, it is guaranteed to be
784          * valid, so just increase the start count.
785          */
786         if (pipe->start_count) {
787                 pipe->start_count++;
788                 return 0;
789         }
790
791         /*
792          * Populate the pipeline. This populates the media_pipeline pads list
793          * with media_pipeline_pad instances for each pad found during graph
794          * walk.
795          */
796         ret = media_pipeline_populate(pipe, pad);
797         if (ret)
798                 return ret;
799
800         /*
801          * Now that all the pads in the pipeline have been gathered, perform
802          * the validation steps.
803          */
804
805         list_for_each_entry(ppad, &pipe->pads, list) {
806                 struct media_pad *pad = ppad->pad;
807                 struct media_entity *entity = pad->entity;
808                 bool has_enabled_link = false;
809                 struct media_link *link;
810
811                 dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name,
812                         pad->index);
813
814                 /*
815                  * 1. Ensure that the pad doesn't already belong to a different
816                  * pipeline.
817                  */
818                 if (pad->pipe) {
819                         dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n",
820                                 pad->entity->name, pad->index);
821                         ret = -EBUSY;
822                         goto error;
823                 }
824
825                 /*
826                  * 2. Validate all active links whose sink is the current pad.
827                  * Validation of the source pads is performed in the context of
828                  * the connected sink pad to avoid duplicating checks.
829                  */
830                 for_each_media_entity_data_link(entity, link) {
831                         /* Skip links unrelated to the current pad. */
832                         if (link->sink != pad && link->source != pad)
833                                 continue;
834
835                         /* Record if the pad has links and enabled links. */
836                         if (link->flags & MEDIA_LNK_FL_ENABLED)
837                                 has_enabled_link = true;
838
839                         /*
840                          * Validate the link if it's enabled and has the
841                          * current pad as its sink.
842                          */
843                         if (!(link->flags & MEDIA_LNK_FL_ENABLED))
844                                 continue;
845
846                         if (link->sink != pad)
847                                 continue;
848
849                         if (!entity->ops || !entity->ops->link_validate)
850                                 continue;
851
852                         ret = entity->ops->link_validate(link);
853                         if (ret) {
854                                 dev_dbg(mdev->dev,
855                                         "Link '%s':%u -> '%s':%u failed validation: %d\n",
856                                         link->source->entity->name,
857                                         link->source->index,
858                                         link->sink->entity->name,
859                                         link->sink->index, ret);
860                                 goto error;
861                         }
862
863                         dev_dbg(mdev->dev,
864                                 "Link '%s':%u -> '%s':%u is valid\n",
865                                 link->source->entity->name,
866                                 link->source->index,
867                                 link->sink->entity->name,
868                                 link->sink->index);
869                 }
870
871                 /*
872                  * 3. If the pad has the MEDIA_PAD_FL_MUST_CONNECT flag set,
873                  * ensure that it has either no link or an enabled link.
874                  */
875                 if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) &&
876                     !has_enabled_link) {
877                         dev_dbg(mdev->dev,
878                                 "Pad '%s':%u must be connected by an enabled link\n",
879                                 pad->entity->name, pad->index);
880                         ret = -ENOLINK;
881                         goto error;
882                 }
883
884                 /* Validation passed, store the pipe pointer in the pad. */
885                 pad->pipe = pipe;
886         }
887
888         pipe->start_count++;
889
890         return 0;
891
892 error:
893         /*
894          * Link validation on graph failed. We revert what we did and
895          * return the error.
896          */
897
898         list_for_each_entry(err_ppad, &pipe->pads, list) {
899                 if (err_ppad == ppad)
900                         break;
901
902                 err_ppad->pad->pipe = NULL;
903         }
904
905         media_pipeline_cleanup(pipe);
906
907         return ret;
908 }
909 EXPORT_SYMBOL_GPL(__media_pipeline_start);
910
911 __must_check int media_pipeline_start(struct media_pad *pad,
912                                       struct media_pipeline *pipe)
913 {
914         struct media_device *mdev = pad->graph_obj.mdev;
915         int ret;
916
917         mutex_lock(&mdev->graph_mutex);
918         ret = __media_pipeline_start(pad, pipe);
919         mutex_unlock(&mdev->graph_mutex);
920         return ret;
921 }
922 EXPORT_SYMBOL_GPL(media_pipeline_start);
923
924 void __media_pipeline_stop(struct media_pad *pad)
925 {
926         struct media_pipeline *pipe = pad->pipe;
927         struct media_pipeline_pad *ppad;
928
929         /*
930          * If the following check fails, the driver has performed an
931          * unbalanced call to media_pipeline_stop()
932          */
933         if (WARN_ON(!pipe))
934                 return;
935
936         if (--pipe->start_count)
937                 return;
938
939         list_for_each_entry(ppad, &pipe->pads, list)
940                 ppad->pad->pipe = NULL;
941
942         media_pipeline_cleanup(pipe);
943
944         if (pipe->allocated)
945                 kfree(pipe);
946 }
947 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
948
949 void media_pipeline_stop(struct media_pad *pad)
950 {
951         struct media_device *mdev = pad->graph_obj.mdev;
952
953         mutex_lock(&mdev->graph_mutex);
954         __media_pipeline_stop(pad);
955         mutex_unlock(&mdev->graph_mutex);
956 }
957 EXPORT_SYMBOL_GPL(media_pipeline_stop);
958
959 __must_check int media_pipeline_alloc_start(struct media_pad *pad)
960 {
961         struct media_device *mdev = pad->graph_obj.mdev;
962         struct media_pipeline *new_pipe = NULL;
963         struct media_pipeline *pipe;
964         int ret;
965
966         mutex_lock(&mdev->graph_mutex);
967
968         /*
969          * Is the pad already part of a pipeline? If not, we need to allocate
970          * a pipe.
971          */
972         pipe = media_pad_pipeline(pad);
973         if (!pipe) {
974                 new_pipe = kzalloc(sizeof(*new_pipe), GFP_KERNEL);
975                 if (!new_pipe) {
976                         ret = -ENOMEM;
977                         goto out;
978                 }
979
980                 pipe = new_pipe;
981                 pipe->allocated = true;
982         }
983
984         ret = __media_pipeline_start(pad, pipe);
985         if (ret)
986                 kfree(new_pipe);
987
988 out:
989         mutex_unlock(&mdev->graph_mutex);
990
991         return ret;
992 }
993 EXPORT_SYMBOL_GPL(media_pipeline_alloc_start);
994
995 struct media_pad *
996 __media_pipeline_pad_iter_next(struct media_pipeline *pipe,
997                                struct media_pipeline_pad_iter *iter,
998                                struct media_pad *pad)
999 {
1000         if (!pad)
1001                 iter->cursor = pipe->pads.next;
1002
1003         if (iter->cursor == &pipe->pads)
1004                 return NULL;
1005
1006         pad = list_entry(iter->cursor, struct media_pipeline_pad, list)->pad;
1007         iter->cursor = iter->cursor->next;
1008
1009         return pad;
1010 }
1011 EXPORT_SYMBOL_GPL(__media_pipeline_pad_iter_next);
1012
1013 int media_pipeline_entity_iter_init(struct media_pipeline *pipe,
1014                                     struct media_pipeline_entity_iter *iter)
1015 {
1016         return media_entity_enum_init(&iter->ent_enum, pipe->mdev);
1017 }
1018 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_init);
1019
1020 void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter)
1021 {
1022         media_entity_enum_cleanup(&iter->ent_enum);
1023 }
1024 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_cleanup);
1025
1026 struct media_entity *
1027 __media_pipeline_entity_iter_next(struct media_pipeline *pipe,
1028                                   struct media_pipeline_entity_iter *iter,
1029                                   struct media_entity *entity)
1030 {
1031         if (!entity)
1032                 iter->cursor = pipe->pads.next;
1033
1034         while (iter->cursor != &pipe->pads) {
1035                 struct media_pipeline_pad *ppad;
1036                 struct media_entity *entity;
1037
1038                 ppad = list_entry(iter->cursor, struct media_pipeline_pad, list);
1039                 entity = ppad->pad->entity;
1040                 iter->cursor = iter->cursor->next;
1041
1042                 if (!media_entity_enum_test_and_set(&iter->ent_enum, entity))
1043                         return entity;
1044         }
1045
1046         return NULL;
1047 }
1048 EXPORT_SYMBOL_GPL(__media_pipeline_entity_iter_next);
1049
1050 /* -----------------------------------------------------------------------------
1051  * Links management
1052  */
1053
1054 static struct media_link *media_add_link(struct list_head *head)
1055 {
1056         struct media_link *link;
1057
1058         link = kzalloc(sizeof(*link), GFP_KERNEL);
1059         if (link == NULL)
1060                 return NULL;
1061
1062         list_add_tail(&link->list, head);
1063
1064         return link;
1065 }
1066
1067 static void __media_entity_remove_link(struct media_entity *entity,
1068                                        struct media_link *link)
1069 {
1070         struct media_link *rlink, *tmp;
1071         struct media_entity *remote;
1072
1073         /* Remove the reverse links for a data link. */
1074         if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
1075                 link->source->num_links--;
1076                 link->sink->num_links--;
1077
1078                 if (link->source->entity == entity)
1079                         remote = link->sink->entity;
1080                 else
1081                         remote = link->source->entity;
1082
1083                 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
1084                         if (rlink != link->reverse)
1085                                 continue;
1086
1087                         if (link->source->entity == entity)
1088                                 remote->num_backlinks--;
1089
1090                         /* Remove the remote link */
1091                         list_del(&rlink->list);
1092                         media_gobj_destroy(&rlink->graph_obj);
1093                         kfree(rlink);
1094
1095                         if (--remote->num_links == 0)
1096                                 break;
1097                 }
1098         }
1099
1100         list_del(&link->list);
1101         media_gobj_destroy(&link->graph_obj);
1102         kfree(link);
1103 }
1104
1105 int media_get_pad_index(struct media_entity *entity, u32 pad_type,
1106                         enum media_pad_signal_type sig_type)
1107 {
1108         unsigned int i;
1109
1110         if (!entity)
1111                 return -EINVAL;
1112
1113         for (i = 0; i < entity->num_pads; i++) {
1114                 if ((entity->pads[i].flags &
1115                      (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) != pad_type)
1116                         continue;
1117
1118                 if (entity->pads[i].sig_type == sig_type)
1119                         return i;
1120         }
1121         return -EINVAL;
1122 }
1123 EXPORT_SYMBOL_GPL(media_get_pad_index);
1124
1125 int
1126 media_create_pad_link(struct media_entity *source, u16 source_pad,
1127                          struct media_entity *sink, u16 sink_pad, u32 flags)
1128 {
1129         struct media_link *link;
1130         struct media_link *backlink;
1131
1132         if (flags & MEDIA_LNK_FL_LINK_TYPE)
1133                 return -EINVAL;
1134
1135         flags |= MEDIA_LNK_FL_DATA_LINK;
1136
1137         if (WARN_ON(!source || !sink) ||
1138             WARN_ON(source_pad >= source->num_pads) ||
1139             WARN_ON(sink_pad >= sink->num_pads))
1140                 return -EINVAL;
1141         if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
1142                 return -EINVAL;
1143         if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
1144                 return -EINVAL;
1145
1146         link = media_add_link(&source->links);
1147         if (link == NULL)
1148                 return -ENOMEM;
1149
1150         link->source = &source->pads[source_pad];
1151         link->sink = &sink->pads[sink_pad];
1152         link->flags = flags;
1153
1154         /* Initialize graph object embedded at the new link */
1155         media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
1156                         &link->graph_obj);
1157
1158         /* Create the backlink. Backlinks are used to help graph traversal and
1159          * are not reported to userspace.
1160          */
1161         backlink = media_add_link(&sink->links);
1162         if (backlink == NULL) {
1163                 __media_entity_remove_link(source, link);
1164                 return -ENOMEM;
1165         }
1166
1167         backlink->source = &source->pads[source_pad];
1168         backlink->sink = &sink->pads[sink_pad];
1169         backlink->flags = flags;
1170         backlink->is_backlink = true;
1171
1172         /* Initialize graph object embedded at the new link */
1173         media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
1174                         &backlink->graph_obj);
1175
1176         link->reverse = backlink;
1177         backlink->reverse = link;
1178
1179         sink->num_backlinks++;
1180         sink->num_links++;
1181         source->num_links++;
1182
1183         link->source->num_links++;
1184         link->sink->num_links++;
1185
1186         return 0;
1187 }
1188 EXPORT_SYMBOL_GPL(media_create_pad_link);
1189
1190 int media_create_pad_links(const struct media_device *mdev,
1191                            const u32 source_function,
1192                            struct media_entity *source,
1193                            const u16 source_pad,
1194                            const u32 sink_function,
1195                            struct media_entity *sink,
1196                            const u16 sink_pad,
1197                            u32 flags,
1198                            const bool allow_both_undefined)
1199 {
1200         struct media_entity *entity;
1201         unsigned function;
1202         int ret;
1203
1204         /* Trivial case: 1:1 relation */
1205         if (source && sink)
1206                 return media_create_pad_link(source, source_pad,
1207                                              sink, sink_pad, flags);
1208
1209         /* Worse case scenario: n:n relation */
1210         if (!source && !sink) {
1211                 if (!allow_both_undefined)
1212                         return 0;
1213                 media_device_for_each_entity(source, mdev) {
1214                         if (source->function != source_function)
1215                                 continue;
1216                         media_device_for_each_entity(sink, mdev) {
1217                                 if (sink->function != sink_function)
1218                                         continue;
1219                                 ret = media_create_pad_link(source, source_pad,
1220                                                             sink, sink_pad,
1221                                                             flags);
1222                                 if (ret)
1223                                         return ret;
1224                                 flags &= ~(MEDIA_LNK_FL_ENABLED |
1225                                            MEDIA_LNK_FL_IMMUTABLE);
1226                         }
1227                 }
1228                 return 0;
1229         }
1230
1231         /* Handle 1:n and n:1 cases */
1232         if (source)
1233                 function = sink_function;
1234         else
1235                 function = source_function;
1236
1237         media_device_for_each_entity(entity, mdev) {
1238                 if (entity->function != function)
1239                         continue;
1240
1241                 if (source)
1242                         ret = media_create_pad_link(source, source_pad,
1243                                                     entity, sink_pad, flags);
1244                 else
1245                         ret = media_create_pad_link(entity, source_pad,
1246                                                     sink, sink_pad, flags);
1247                 if (ret)
1248                         return ret;
1249                 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1250         }
1251         return 0;
1252 }
1253 EXPORT_SYMBOL_GPL(media_create_pad_links);
1254
1255 void __media_entity_remove_links(struct media_entity *entity)
1256 {
1257         struct media_link *link, *tmp;
1258
1259         list_for_each_entry_safe(link, tmp, &entity->links, list)
1260                 __media_entity_remove_link(entity, link);
1261
1262         entity->num_links = 0;
1263         entity->num_backlinks = 0;
1264 }
1265 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
1266
1267 void media_entity_remove_links(struct media_entity *entity)
1268 {
1269         struct media_device *mdev = entity->graph_obj.mdev;
1270
1271         /* Do nothing if the entity is not registered. */
1272         if (mdev == NULL)
1273                 return;
1274
1275         mutex_lock(&mdev->graph_mutex);
1276         __media_entity_remove_links(entity);
1277         mutex_unlock(&mdev->graph_mutex);
1278 }
1279 EXPORT_SYMBOL_GPL(media_entity_remove_links);
1280
1281 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
1282 {
1283         int ret;
1284
1285         /* Notify both entities. */
1286         ret = media_entity_call(link->source->entity, link_setup,
1287                                 link->source, link->sink, flags);
1288         if (ret < 0 && ret != -ENOIOCTLCMD)
1289                 return ret;
1290
1291         ret = media_entity_call(link->sink->entity, link_setup,
1292                                 link->sink, link->source, flags);
1293         if (ret < 0 && ret != -ENOIOCTLCMD) {
1294                 media_entity_call(link->source->entity, link_setup,
1295                                   link->source, link->sink, link->flags);
1296                 return ret;
1297         }
1298
1299         link->flags = flags;
1300         link->reverse->flags = link->flags;
1301
1302         return 0;
1303 }
1304
1305 int __media_entity_setup_link(struct media_link *link, u32 flags)
1306 {
1307         const u32 mask = MEDIA_LNK_FL_ENABLED;
1308         struct media_device *mdev;
1309         struct media_pad *source, *sink;
1310         int ret = -EBUSY;
1311
1312         if (link == NULL)
1313                 return -EINVAL;
1314
1315         /* The non-modifiable link flags must not be modified. */
1316         if ((link->flags & ~mask) != (flags & ~mask))
1317                 return -EINVAL;
1318
1319         if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
1320                 return link->flags == flags ? 0 : -EINVAL;
1321
1322         if (link->flags == flags)
1323                 return 0;
1324
1325         source = link->source;
1326         sink = link->sink;
1327
1328         if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
1329             (media_pad_is_streaming(source) || media_pad_is_streaming(sink)))
1330                 return -EBUSY;
1331
1332         mdev = source->graph_obj.mdev;
1333
1334         if (mdev->ops && mdev->ops->link_notify) {
1335                 ret = mdev->ops->link_notify(link, flags,
1336                                              MEDIA_DEV_NOTIFY_PRE_LINK_CH);
1337                 if (ret < 0)
1338                         return ret;
1339         }
1340
1341         ret = __media_entity_setup_link_notify(link, flags);
1342
1343         if (mdev->ops && mdev->ops->link_notify)
1344                 mdev->ops->link_notify(link, flags,
1345                                        MEDIA_DEV_NOTIFY_POST_LINK_CH);
1346
1347         return ret;
1348 }
1349 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
1350
1351 int media_entity_setup_link(struct media_link *link, u32 flags)
1352 {
1353         int ret;
1354
1355         mutex_lock(&link->graph_obj.mdev->graph_mutex);
1356         ret = __media_entity_setup_link(link, flags);
1357         mutex_unlock(&link->graph_obj.mdev->graph_mutex);
1358
1359         return ret;
1360 }
1361 EXPORT_SYMBOL_GPL(media_entity_setup_link);
1362
1363 struct media_link *
1364 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
1365 {
1366         struct media_link *link;
1367
1368         for_each_media_entity_data_link(source->entity, link) {
1369                 if (link->source->entity == source->entity &&
1370                     link->source->index == source->index &&
1371                     link->sink->entity == sink->entity &&
1372                     link->sink->index == sink->index)
1373                         return link;
1374         }
1375
1376         return NULL;
1377 }
1378 EXPORT_SYMBOL_GPL(media_entity_find_link);
1379
1380 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad)
1381 {
1382         struct media_link *link;
1383
1384         for_each_media_entity_data_link(pad->entity, link) {
1385                 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1386                         continue;
1387
1388                 if (link->source == pad)
1389                         return link->sink;
1390
1391                 if (link->sink == pad)
1392                         return link->source;
1393         }
1394
1395         return NULL;
1396
1397 }
1398 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first);
1399
1400 struct media_pad *
1401 media_entity_remote_pad_unique(const struct media_entity *entity,
1402                                unsigned int type)
1403 {
1404         struct media_pad *pad = NULL;
1405         struct media_link *link;
1406
1407         list_for_each_entry(link, &entity->links, list) {
1408                 struct media_pad *local_pad;
1409                 struct media_pad *remote_pad;
1410
1411                 if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) !=
1412                      MEDIA_LNK_FL_DATA_LINK) ||
1413                     !(link->flags & MEDIA_LNK_FL_ENABLED))
1414                         continue;
1415
1416                 if (type == MEDIA_PAD_FL_SOURCE) {
1417                         local_pad = link->sink;
1418                         remote_pad = link->source;
1419                 } else {
1420                         local_pad = link->source;
1421                         remote_pad = link->sink;
1422                 }
1423
1424                 if (local_pad->entity == entity) {
1425                         if (pad)
1426                                 return ERR_PTR(-ENOTUNIQ);
1427
1428                         pad = remote_pad;
1429                 }
1430         }
1431
1432         if (!pad)
1433                 return ERR_PTR(-ENOLINK);
1434
1435         return pad;
1436 }
1437 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique);
1438
1439 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad)
1440 {
1441         struct media_pad *found_pad = NULL;
1442         struct media_link *link;
1443
1444         list_for_each_entry(link, &pad->entity->links, list) {
1445                 struct media_pad *remote_pad;
1446
1447                 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1448                         continue;
1449
1450                 if (link->sink == pad)
1451                         remote_pad = link->source;
1452                 else if (link->source == pad)
1453                         remote_pad = link->sink;
1454                 else
1455                         continue;
1456
1457                 if (found_pad)
1458                         return ERR_PTR(-ENOTUNIQ);
1459
1460                 found_pad = remote_pad;
1461         }
1462
1463         if (!found_pad)
1464                 return ERR_PTR(-ENOLINK);
1465
1466         return found_pad;
1467 }
1468 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique);
1469
1470 int media_entity_get_fwnode_pad(struct media_entity *entity,
1471                                 const struct fwnode_handle *fwnode,
1472                                 unsigned long direction_flags)
1473 {
1474         struct fwnode_endpoint endpoint;
1475         unsigned int i;
1476         int ret;
1477
1478         if (!entity->ops || !entity->ops->get_fwnode_pad) {
1479                 for (i = 0; i < entity->num_pads; i++) {
1480                         if (entity->pads[i].flags & direction_flags)
1481                                 return i;
1482                 }
1483
1484                 return -ENXIO;
1485         }
1486
1487         ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
1488         if (ret)
1489                 return ret;
1490
1491         ret = entity->ops->get_fwnode_pad(entity, &endpoint);
1492         if (ret < 0)
1493                 return ret;
1494
1495         if (ret >= entity->num_pads)
1496                 return -ENXIO;
1497
1498         if (!(entity->pads[ret].flags & direction_flags))
1499                 return -ENXIO;
1500
1501         return ret;
1502 }
1503 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
1504
1505 struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
1506 {
1507         struct media_pad *pad;
1508
1509         media_entity_for_each_pad(entity, pad) {
1510                 if (pad->pipe)
1511                         return pad->pipe;
1512         }
1513
1514         return NULL;
1515 }
1516 EXPORT_SYMBOL_GPL(media_entity_pipeline);
1517
1518 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
1519 {
1520         return pad->pipe;
1521 }
1522 EXPORT_SYMBOL_GPL(media_pad_pipeline);
1523
1524 static void media_interface_init(struct media_device *mdev,
1525                                  struct media_interface *intf,
1526                                  u32 gobj_type,
1527                                  u32 intf_type, u32 flags)
1528 {
1529         intf->type = intf_type;
1530         intf->flags = flags;
1531         INIT_LIST_HEAD(&intf->links);
1532
1533         media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1534 }
1535
1536 /* Functions related to the media interface via device nodes */
1537
1538 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
1539                                                 u32 type, u32 flags,
1540                                                 u32 major, u32 minor)
1541 {
1542         struct media_intf_devnode *devnode;
1543
1544         devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
1545         if (!devnode)
1546                 return NULL;
1547
1548         devnode->major = major;
1549         devnode->minor = minor;
1550
1551         media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
1552                              type, flags);
1553
1554         return devnode;
1555 }
1556 EXPORT_SYMBOL_GPL(media_devnode_create);
1557
1558 void media_devnode_remove(struct media_intf_devnode *devnode)
1559 {
1560         media_remove_intf_links(&devnode->intf);
1561         media_gobj_destroy(&devnode->intf.graph_obj);
1562         kfree(devnode);
1563 }
1564 EXPORT_SYMBOL_GPL(media_devnode_remove);
1565
1566 struct media_link *media_create_intf_link(struct media_entity *entity,
1567                                             struct media_interface *intf,
1568                                             u32 flags)
1569 {
1570         struct media_link *link;
1571
1572         link = media_add_link(&intf->links);
1573         if (link == NULL)
1574                 return NULL;
1575
1576         link->intf = intf;
1577         link->entity = entity;
1578         link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
1579
1580         /* Initialize graph object embedded at the new link */
1581         media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1582                         &link->graph_obj);
1583
1584         return link;
1585 }
1586 EXPORT_SYMBOL_GPL(media_create_intf_link);
1587
1588 void __media_remove_intf_link(struct media_link *link)
1589 {
1590         list_del(&link->list);
1591         media_gobj_destroy(&link->graph_obj);
1592         kfree(link);
1593 }
1594 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1595
1596 void media_remove_intf_link(struct media_link *link)
1597 {
1598         struct media_device *mdev = link->graph_obj.mdev;
1599
1600         /* Do nothing if the intf is not registered. */
1601         if (mdev == NULL)
1602                 return;
1603
1604         mutex_lock(&mdev->graph_mutex);
1605         __media_remove_intf_link(link);
1606         mutex_unlock(&mdev->graph_mutex);
1607 }
1608 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1609
1610 void __media_remove_intf_links(struct media_interface *intf)
1611 {
1612         struct media_link *link, *tmp;
1613
1614         list_for_each_entry_safe(link, tmp, &intf->links, list)
1615                 __media_remove_intf_link(link);
1616
1617 }
1618 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1619
1620 void media_remove_intf_links(struct media_interface *intf)
1621 {
1622         struct media_device *mdev = intf->graph_obj.mdev;
1623
1624         /* Do nothing if the intf is not registered. */
1625         if (mdev == NULL)
1626                 return;
1627
1628         mutex_lock(&mdev->graph_mutex);
1629         __media_remove_intf_links(intf);
1630         mutex_unlock(&mdev->graph_mutex);
1631 }
1632 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1633
1634 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1635                                                struct media_entity *ancillary)
1636 {
1637         struct media_link *link;
1638
1639         link = media_add_link(&primary->links);
1640         if (!link)
1641                 return ERR_PTR(-ENOMEM);
1642
1643         link->gobj0 = &primary->graph_obj;
1644         link->gobj1 = &ancillary->graph_obj;
1645         link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1646                       MEDIA_LNK_FL_ANCILLARY_LINK;
1647
1648         /* Initialize graph object embedded in the new link */
1649         media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1650                           &link->graph_obj);
1651
1652         return link;
1653 }
1654 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
1655
1656 struct media_link *__media_entity_next_link(struct media_entity *entity,
1657                                             struct media_link *link,
1658                                             unsigned long link_type)
1659 {
1660         link = link ? list_next_entry(link, list)
1661                     : list_first_entry(&entity->links, typeof(*link), list);
1662
1663         list_for_each_entry_from(link, &entity->links, list)
1664                 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
1665                         return link;
1666
1667         return NULL;
1668 }
1669 EXPORT_SYMBOL_GPL(__media_entity_next_link);