Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.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/property.h>
13 #include <linux/slab.h>
14 #include <media/media-entity.h>
15 #include <media/media-device.h>
16
17 static inline const char *gobj_type(enum media_gobj_type type)
18 {
19         switch (type) {
20         case MEDIA_GRAPH_ENTITY:
21                 return "entity";
22         case MEDIA_GRAPH_PAD:
23                 return "pad";
24         case MEDIA_GRAPH_LINK:
25                 return "link";
26         case MEDIA_GRAPH_INTF_DEVNODE:
27                 return "intf-devnode";
28         default:
29                 return "unknown";
30         }
31 }
32
33 static inline const char *intf_type(struct media_interface *intf)
34 {
35         switch (intf->type) {
36         case MEDIA_INTF_T_DVB_FE:
37                 return "dvb-frontend";
38         case MEDIA_INTF_T_DVB_DEMUX:
39                 return "dvb-demux";
40         case MEDIA_INTF_T_DVB_DVR:
41                 return "dvb-dvr";
42         case MEDIA_INTF_T_DVB_CA:
43                 return  "dvb-ca";
44         case MEDIA_INTF_T_DVB_NET:
45                 return "dvb-net";
46         case MEDIA_INTF_T_V4L_VIDEO:
47                 return "v4l-video";
48         case MEDIA_INTF_T_V4L_VBI:
49                 return "v4l-vbi";
50         case MEDIA_INTF_T_V4L_RADIO:
51                 return "v4l-radio";
52         case MEDIA_INTF_T_V4L_SUBDEV:
53                 return "v4l-subdev";
54         case MEDIA_INTF_T_V4L_SWRADIO:
55                 return "v4l-swradio";
56         case MEDIA_INTF_T_V4L_TOUCH:
57                 return "v4l-touch";
58         default:
59                 return "unknown-intf";
60         }
61 };
62
63 static inline const char *link_type_name(struct media_link *link)
64 {
65         switch (link->flags & MEDIA_LNK_FL_LINK_TYPE) {
66         case MEDIA_LNK_FL_DATA_LINK:
67                 return "data";
68         case MEDIA_LNK_FL_INTERFACE_LINK:
69                 return "interface";
70         case MEDIA_LNK_FL_ANCILLARY_LINK:
71                 return "ancillary";
72         default:
73                 return "unknown";
74         }
75 }
76
77 __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
78                                           int idx_max)
79 {
80         idx_max = ALIGN(idx_max, BITS_PER_LONG);
81         ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
82                                  GFP_KERNEL);
83         if (!ent_enum->bmap)
84                 return -ENOMEM;
85
86         bitmap_zero(ent_enum->bmap, idx_max);
87         ent_enum->idx_max = idx_max;
88
89         return 0;
90 }
91 EXPORT_SYMBOL_GPL(__media_entity_enum_init);
92
93 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
94 {
95         kfree(ent_enum->bmap);
96 }
97 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
98
99 /**
100  *  dev_dbg_obj - Prints in debug mode a change on some object
101  *
102  * @event_name: Name of the event to report. Could be __func__
103  * @gobj:       Pointer to the object
104  *
105  * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
106  * won't produce any code.
107  */
108 static void dev_dbg_obj(const char *event_name,  struct media_gobj *gobj)
109 {
110 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
111         switch (media_type(gobj)) {
112         case MEDIA_GRAPH_ENTITY:
113                 dev_dbg(gobj->mdev->dev,
114                         "%s id %u: entity '%s'\n",
115                         event_name, media_id(gobj),
116                         gobj_to_entity(gobj)->name);
117                 break;
118         case MEDIA_GRAPH_LINK:
119         {
120                 struct media_link *link = gobj_to_link(gobj);
121
122                 dev_dbg(gobj->mdev->dev,
123                         "%s id %u: %s link id %u ==> id %u\n",
124                         event_name, media_id(gobj), link_type_name(link),
125                         media_id(link->gobj0),
126                         media_id(link->gobj1));
127                 break;
128         }
129         case MEDIA_GRAPH_PAD:
130         {
131                 struct media_pad *pad = gobj_to_pad(gobj);
132
133                 dev_dbg(gobj->mdev->dev,
134                         "%s id %u: %s%spad '%s':%d\n",
135                         event_name, media_id(gobj),
136                         pad->flags & MEDIA_PAD_FL_SINK   ? "sink " : "",
137                         pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
138                         pad->entity->name, pad->index);
139                 break;
140         }
141         case MEDIA_GRAPH_INTF_DEVNODE:
142         {
143                 struct media_interface *intf = gobj_to_intf(gobj);
144                 struct media_intf_devnode *devnode = intf_to_devnode(intf);
145
146                 dev_dbg(gobj->mdev->dev,
147                         "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
148                         event_name, media_id(gobj),
149                         intf_type(intf),
150                         devnode->major, devnode->minor);
151                 break;
152         }
153         }
154 #endif
155 }
156
157 void media_gobj_create(struct media_device *mdev,
158                            enum media_gobj_type type,
159                            struct media_gobj *gobj)
160 {
161         BUG_ON(!mdev);
162
163         gobj->mdev = mdev;
164
165         /* Create a per-type unique object ID */
166         gobj->id = media_gobj_gen_id(type, ++mdev->id);
167
168         switch (type) {
169         case MEDIA_GRAPH_ENTITY:
170                 list_add_tail(&gobj->list, &mdev->entities);
171                 break;
172         case MEDIA_GRAPH_PAD:
173                 list_add_tail(&gobj->list, &mdev->pads);
174                 break;
175         case MEDIA_GRAPH_LINK:
176                 list_add_tail(&gobj->list, &mdev->links);
177                 break;
178         case MEDIA_GRAPH_INTF_DEVNODE:
179                 list_add_tail(&gobj->list, &mdev->interfaces);
180                 break;
181         }
182
183         mdev->topology_version++;
184
185         dev_dbg_obj(__func__, gobj);
186 }
187
188 void media_gobj_destroy(struct media_gobj *gobj)
189 {
190         /* Do nothing if the object is not linked. */
191         if (gobj->mdev == NULL)
192                 return;
193
194         dev_dbg_obj(__func__, gobj);
195
196         gobj->mdev->topology_version++;
197
198         /* Remove the object from mdev list */
199         list_del(&gobj->list);
200
201         gobj->mdev = NULL;
202 }
203
204 /*
205  * TODO: Get rid of this.
206  */
207 #define MEDIA_ENTITY_MAX_PADS           512
208
209 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
210                            struct media_pad *pads)
211 {
212         struct media_device *mdev = entity->graph_obj.mdev;
213         unsigned int i;
214
215         if (num_pads >= MEDIA_ENTITY_MAX_PADS)
216                 return -E2BIG;
217
218         entity->num_pads = num_pads;
219         entity->pads = pads;
220
221         if (mdev)
222                 mutex_lock(&mdev->graph_mutex);
223
224         for (i = 0; i < num_pads; i++) {
225                 pads[i].entity = entity;
226                 pads[i].index = i;
227                 if (mdev)
228                         media_gobj_create(mdev, MEDIA_GRAPH_PAD,
229                                         &entity->pads[i].graph_obj);
230         }
231
232         if (mdev)
233                 mutex_unlock(&mdev->graph_mutex);
234
235         return 0;
236 }
237 EXPORT_SYMBOL_GPL(media_entity_pads_init);
238
239 /* -----------------------------------------------------------------------------
240  * Graph traversal
241  */
242
243 static struct media_entity *
244 media_entity_other(struct media_entity *entity, struct media_link *link)
245 {
246         if (link->source->entity == entity)
247                 return link->sink->entity;
248         else
249                 return link->source->entity;
250 }
251
252 /* push an entity to traversal stack */
253 static void stack_push(struct media_graph *graph,
254                        struct media_entity *entity)
255 {
256         if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
257                 WARN_ON(1);
258                 return;
259         }
260         graph->top++;
261         graph->stack[graph->top].link = entity->links.next;
262         graph->stack[graph->top].entity = entity;
263 }
264
265 static struct media_entity *stack_pop(struct media_graph *graph)
266 {
267         struct media_entity *entity;
268
269         entity = graph->stack[graph->top].entity;
270         graph->top--;
271
272         return entity;
273 }
274
275 #define link_top(en)    ((en)->stack[(en)->top].link)
276 #define stack_top(en)   ((en)->stack[(en)->top].entity)
277
278 /**
279  * media_graph_walk_init - Allocate resources for graph walk
280  * @graph: Media graph structure that will be used to walk the graph
281  * @mdev: Media device
282  *
283  * Reserve resources for graph walk in media device's current
284  * state. The memory must be released using
285  * media_graph_walk_free().
286  *
287  * Returns error on failure, zero on success.
288  */
289 __must_check int media_graph_walk_init(
290         struct media_graph *graph, struct media_device *mdev)
291 {
292         return media_entity_enum_init(&graph->ent_enum, mdev);
293 }
294 EXPORT_SYMBOL_GPL(media_graph_walk_init);
295
296 /**
297  * media_graph_walk_cleanup - Release resources related to graph walking
298  * @graph: Media graph structure that was used to walk the graph
299  */
300 void media_graph_walk_cleanup(struct media_graph *graph)
301 {
302         media_entity_enum_cleanup(&graph->ent_enum);
303 }
304 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
305
306 void media_graph_walk_start(struct media_graph *graph,
307                             struct media_entity *entity)
308 {
309         media_entity_enum_zero(&graph->ent_enum);
310         media_entity_enum_set(&graph->ent_enum, entity);
311
312         graph->top = 0;
313         graph->stack[graph->top].entity = NULL;
314         stack_push(graph, entity);
315         dev_dbg(entity->graph_obj.mdev->dev,
316                 "begin graph walk at '%s'\n", entity->name);
317 }
318 EXPORT_SYMBOL_GPL(media_graph_walk_start);
319
320 static void media_graph_walk_iter(struct media_graph *graph)
321 {
322         struct media_entity *entity = stack_top(graph);
323         struct media_link *link;
324         struct media_entity *next;
325
326         link = list_entry(link_top(graph), typeof(*link), list);
327
328         /* If the link is not a data link, don't follow it */
329         if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) {
330                 link_top(graph) = link_top(graph)->next;
331                 return;
332         }
333
334         /* The link is not enabled so we do not follow. */
335         if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
336                 link_top(graph) = link_top(graph)->next;
337                 dev_dbg(entity->graph_obj.mdev->dev,
338                         "walk: skipping disabled link '%s':%u -> '%s':%u\n",
339                         link->source->entity->name, link->source->index,
340                         link->sink->entity->name, link->sink->index);
341                 return;
342         }
343
344         /* Get the entity at the other end of the link. */
345         next = media_entity_other(entity, link);
346
347         /* Has the entity already been visited? */
348         if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
349                 link_top(graph) = link_top(graph)->next;
350                 dev_dbg(entity->graph_obj.mdev->dev,
351                         "walk: skipping entity '%s' (already seen)\n",
352                         next->name);
353                 return;
354         }
355
356         /* Push the new entity to stack and start over. */
357         link_top(graph) = link_top(graph)->next;
358         stack_push(graph, next);
359         dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
360                 next->name);
361         lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex);
362 }
363
364 struct media_entity *media_graph_walk_next(struct media_graph *graph)
365 {
366         struct media_entity *entity;
367
368         if (stack_top(graph) == NULL)
369                 return NULL;
370
371         /*
372          * Depth first search. Push entity to stack and continue from
373          * top of the stack until no more entities on the level can be
374          * found.
375          */
376         while (link_top(graph) != &stack_top(graph)->links)
377                 media_graph_walk_iter(graph);
378
379         entity = stack_pop(graph);
380         dev_dbg(entity->graph_obj.mdev->dev,
381                 "walk: returning entity '%s'\n", entity->name);
382
383         return entity;
384 }
385 EXPORT_SYMBOL_GPL(media_graph_walk_next);
386
387 int media_entity_get_fwnode_pad(struct media_entity *entity,
388                                 struct fwnode_handle *fwnode,
389                                 unsigned long direction_flags)
390 {
391         struct fwnode_endpoint endpoint;
392         unsigned int i;
393         int ret;
394
395         if (!entity->ops || !entity->ops->get_fwnode_pad) {
396                 for (i = 0; i < entity->num_pads; i++) {
397                         if (entity->pads[i].flags & direction_flags)
398                                 return i;
399                 }
400
401                 return -ENXIO;
402         }
403
404         ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
405         if (ret)
406                 return ret;
407
408         ret = entity->ops->get_fwnode_pad(entity, &endpoint);
409         if (ret < 0)
410                 return ret;
411
412         if (ret >= entity->num_pads)
413                 return -ENXIO;
414
415         if (!(entity->pads[ret].flags & direction_flags))
416                 return -ENXIO;
417
418         return ret;
419 }
420 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
421
422 /* -----------------------------------------------------------------------------
423  * Pipeline management
424  */
425
426 __must_check int __media_pipeline_start(struct media_entity *entity,
427                                         struct media_pipeline *pipe)
428 {
429         struct media_device *mdev = entity->graph_obj.mdev;
430         struct media_graph *graph = &pipe->graph;
431         struct media_entity *entity_err = entity;
432         struct media_link *link;
433         int ret;
434
435         if (!pipe->streaming_count++) {
436                 ret = media_graph_walk_init(&pipe->graph, mdev);
437                 if (ret)
438                         goto error_graph_walk_start;
439         }
440
441         media_graph_walk_start(&pipe->graph, entity);
442
443         while ((entity = media_graph_walk_next(graph))) {
444                 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
445                 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
446
447                 entity->stream_count++;
448
449                 if (entity->pipe && entity->pipe != pipe) {
450                         pr_err("Pipe active for %s. Can't start for %s\n",
451                                 entity->name,
452                                 entity_err->name);
453                         ret = -EBUSY;
454                         goto error;
455                 }
456
457                 entity->pipe = pipe;
458
459                 /* Already streaming --- no need to check. */
460                 if (entity->stream_count > 1)
461                         continue;
462
463                 if (!entity->ops || !entity->ops->link_validate)
464                         continue;
465
466                 bitmap_zero(active, entity->num_pads);
467                 bitmap_fill(has_no_links, entity->num_pads);
468
469                 list_for_each_entry(link, &entity->links, list) {
470                         struct media_pad *pad = link->sink->entity == entity
471                                                 ? link->sink : link->source;
472
473                         /* Mark that a pad is connected by a link. */
474                         bitmap_clear(has_no_links, pad->index, 1);
475
476                         /*
477                          * Pads that either do not need to connect or
478                          * are connected through an enabled link are
479                          * fine.
480                          */
481                         if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
482                             link->flags & MEDIA_LNK_FL_ENABLED)
483                                 bitmap_set(active, pad->index, 1);
484
485                         /*
486                          * Link validation will only take place for
487                          * sink ends of the link that are enabled.
488                          */
489                         if (link->sink != pad ||
490                             !(link->flags & MEDIA_LNK_FL_ENABLED))
491                                 continue;
492
493                         ret = entity->ops->link_validate(link);
494                         if (ret < 0 && ret != -ENOIOCTLCMD) {
495                                 dev_dbg(entity->graph_obj.mdev->dev,
496                                         "link validation failed for '%s':%u -> '%s':%u, error %d\n",
497                                         link->source->entity->name,
498                                         link->source->index,
499                                         entity->name, link->sink->index, ret);
500                                 goto error;
501                         }
502                 }
503
504                 /* Either no links or validated links are fine. */
505                 bitmap_or(active, active, has_no_links, entity->num_pads);
506
507                 if (!bitmap_full(active, entity->num_pads)) {
508                         ret = -ENOLINK;
509                         dev_dbg(entity->graph_obj.mdev->dev,
510                                 "'%s':%u must be connected by an enabled link\n",
511                                 entity->name,
512                                 (unsigned)find_first_zero_bit(
513                                         active, entity->num_pads));
514                         goto error;
515                 }
516         }
517
518         return 0;
519
520 error:
521         /*
522          * Link validation on graph failed. We revert what we did and
523          * return the error.
524          */
525         media_graph_walk_start(graph, entity_err);
526
527         while ((entity_err = media_graph_walk_next(graph))) {
528                 /* Sanity check for negative stream_count */
529                 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
530                         entity_err->stream_count--;
531                         if (entity_err->stream_count == 0)
532                                 entity_err->pipe = NULL;
533                 }
534
535                 /*
536                  * We haven't increased stream_count further than this
537                  * so we quit here.
538                  */
539                 if (entity_err == entity)
540                         break;
541         }
542
543 error_graph_walk_start:
544         if (!--pipe->streaming_count)
545                 media_graph_walk_cleanup(graph);
546
547         return ret;
548 }
549 EXPORT_SYMBOL_GPL(__media_pipeline_start);
550
551 __must_check int media_pipeline_start(struct media_entity *entity,
552                                       struct media_pipeline *pipe)
553 {
554         struct media_device *mdev = entity->graph_obj.mdev;
555         int ret;
556
557         mutex_lock(&mdev->graph_mutex);
558         ret = __media_pipeline_start(entity, pipe);
559         mutex_unlock(&mdev->graph_mutex);
560         return ret;
561 }
562 EXPORT_SYMBOL_GPL(media_pipeline_start);
563
564 void __media_pipeline_stop(struct media_entity *entity)
565 {
566         struct media_graph *graph = &entity->pipe->graph;
567         struct media_pipeline *pipe = entity->pipe;
568
569         /*
570          * If the following check fails, the driver has performed an
571          * unbalanced call to media_pipeline_stop()
572          */
573         if (WARN_ON(!pipe))
574                 return;
575
576         media_graph_walk_start(graph, entity);
577
578         while ((entity = media_graph_walk_next(graph))) {
579                 /* Sanity check for negative stream_count */
580                 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
581                         entity->stream_count--;
582                         if (entity->stream_count == 0)
583                                 entity->pipe = NULL;
584                 }
585         }
586
587         if (!--pipe->streaming_count)
588                 media_graph_walk_cleanup(graph);
589
590 }
591 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
592
593 void media_pipeline_stop(struct media_entity *entity)
594 {
595         struct media_device *mdev = entity->graph_obj.mdev;
596
597         mutex_lock(&mdev->graph_mutex);
598         __media_pipeline_stop(entity);
599         mutex_unlock(&mdev->graph_mutex);
600 }
601 EXPORT_SYMBOL_GPL(media_pipeline_stop);
602
603 /* -----------------------------------------------------------------------------
604  * Links management
605  */
606
607 static struct media_link *media_add_link(struct list_head *head)
608 {
609         struct media_link *link;
610
611         link = kzalloc(sizeof(*link), GFP_KERNEL);
612         if (link == NULL)
613                 return NULL;
614
615         list_add_tail(&link->list, head);
616
617         return link;
618 }
619
620 static void __media_entity_remove_link(struct media_entity *entity,
621                                        struct media_link *link)
622 {
623         struct media_link *rlink, *tmp;
624         struct media_entity *remote;
625
626         if (link->source->entity == entity)
627                 remote = link->sink->entity;
628         else
629                 remote = link->source->entity;
630
631         list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
632                 if (rlink != link->reverse)
633                         continue;
634
635                 if (link->source->entity == entity)
636                         remote->num_backlinks--;
637
638                 /* Remove the remote link */
639                 list_del(&rlink->list);
640                 media_gobj_destroy(&rlink->graph_obj);
641                 kfree(rlink);
642
643                 if (--remote->num_links == 0)
644                         break;
645         }
646         list_del(&link->list);
647         media_gobj_destroy(&link->graph_obj);
648         kfree(link);
649 }
650
651 int media_get_pad_index(struct media_entity *entity, bool is_sink,
652                         enum media_pad_signal_type sig_type)
653 {
654         int i;
655         bool pad_is_sink;
656
657         if (!entity)
658                 return -EINVAL;
659
660         for (i = 0; i < entity->num_pads; i++) {
661                 if (entity->pads[i].flags & MEDIA_PAD_FL_SINK)
662                         pad_is_sink = true;
663                 else if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
664                         pad_is_sink = false;
665                 else
666                         continue;       /* This is an error! */
667
668                 if (pad_is_sink != is_sink)
669                         continue;
670                 if (entity->pads[i].sig_type == sig_type)
671                         return i;
672         }
673         return -EINVAL;
674 }
675 EXPORT_SYMBOL_GPL(media_get_pad_index);
676
677 int
678 media_create_pad_link(struct media_entity *source, u16 source_pad,
679                          struct media_entity *sink, u16 sink_pad, u32 flags)
680 {
681         struct media_link *link;
682         struct media_link *backlink;
683
684         if (WARN_ON(!source || !sink) ||
685             WARN_ON(source_pad >= source->num_pads) ||
686             WARN_ON(sink_pad >= sink->num_pads))
687                 return -EINVAL;
688         if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
689                 return -EINVAL;
690         if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
691                 return -EINVAL;
692
693         link = media_add_link(&source->links);
694         if (link == NULL)
695                 return -ENOMEM;
696
697         link->source = &source->pads[source_pad];
698         link->sink = &sink->pads[sink_pad];
699         link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
700
701         /* Initialize graph object embedded at the new link */
702         media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
703                         &link->graph_obj);
704
705         /* Create the backlink. Backlinks are used to help graph traversal and
706          * are not reported to userspace.
707          */
708         backlink = media_add_link(&sink->links);
709         if (backlink == NULL) {
710                 __media_entity_remove_link(source, link);
711                 return -ENOMEM;
712         }
713
714         backlink->source = &source->pads[source_pad];
715         backlink->sink = &sink->pads[sink_pad];
716         backlink->flags = flags;
717         backlink->is_backlink = true;
718
719         /* Initialize graph object embedded at the new link */
720         media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
721                         &backlink->graph_obj);
722
723         link->reverse = backlink;
724         backlink->reverse = link;
725
726         sink->num_backlinks++;
727         sink->num_links++;
728         source->num_links++;
729
730         return 0;
731 }
732 EXPORT_SYMBOL_GPL(media_create_pad_link);
733
734 int media_create_pad_links(const struct media_device *mdev,
735                            const u32 source_function,
736                            struct media_entity *source,
737                            const u16 source_pad,
738                            const u32 sink_function,
739                            struct media_entity *sink,
740                            const u16 sink_pad,
741                            u32 flags,
742                            const bool allow_both_undefined)
743 {
744         struct media_entity *entity;
745         unsigned function;
746         int ret;
747
748         /* Trivial case: 1:1 relation */
749         if (source && sink)
750                 return media_create_pad_link(source, source_pad,
751                                              sink, sink_pad, flags);
752
753         /* Worse case scenario: n:n relation */
754         if (!source && !sink) {
755                 if (!allow_both_undefined)
756                         return 0;
757                 media_device_for_each_entity(source, mdev) {
758                         if (source->function != source_function)
759                                 continue;
760                         media_device_for_each_entity(sink, mdev) {
761                                 if (sink->function != sink_function)
762                                         continue;
763                                 ret = media_create_pad_link(source, source_pad,
764                                                             sink, sink_pad,
765                                                             flags);
766                                 if (ret)
767                                         return ret;
768                                 flags &= ~(MEDIA_LNK_FL_ENABLED |
769                                            MEDIA_LNK_FL_IMMUTABLE);
770                         }
771                 }
772                 return 0;
773         }
774
775         /* Handle 1:n and n:1 cases */
776         if (source)
777                 function = sink_function;
778         else
779                 function = source_function;
780
781         media_device_for_each_entity(entity, mdev) {
782                 if (entity->function != function)
783                         continue;
784
785                 if (source)
786                         ret = media_create_pad_link(source, source_pad,
787                                                     entity, sink_pad, flags);
788                 else
789                         ret = media_create_pad_link(entity, source_pad,
790                                                     sink, sink_pad, flags);
791                 if (ret)
792                         return ret;
793                 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
794         }
795         return 0;
796 }
797 EXPORT_SYMBOL_GPL(media_create_pad_links);
798
799 void __media_entity_remove_links(struct media_entity *entity)
800 {
801         struct media_link *link, *tmp;
802
803         list_for_each_entry_safe(link, tmp, &entity->links, list)
804                 __media_entity_remove_link(entity, link);
805
806         entity->num_links = 0;
807         entity->num_backlinks = 0;
808 }
809 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
810
811 void media_entity_remove_links(struct media_entity *entity)
812 {
813         struct media_device *mdev = entity->graph_obj.mdev;
814
815         /* Do nothing if the entity is not registered. */
816         if (mdev == NULL)
817                 return;
818
819         mutex_lock(&mdev->graph_mutex);
820         __media_entity_remove_links(entity);
821         mutex_unlock(&mdev->graph_mutex);
822 }
823 EXPORT_SYMBOL_GPL(media_entity_remove_links);
824
825 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
826 {
827         int ret;
828
829         /* Notify both entities. */
830         ret = media_entity_call(link->source->entity, link_setup,
831                                 link->source, link->sink, flags);
832         if (ret < 0 && ret != -ENOIOCTLCMD)
833                 return ret;
834
835         ret = media_entity_call(link->sink->entity, link_setup,
836                                 link->sink, link->source, flags);
837         if (ret < 0 && ret != -ENOIOCTLCMD) {
838                 media_entity_call(link->source->entity, link_setup,
839                                   link->source, link->sink, link->flags);
840                 return ret;
841         }
842
843         link->flags = flags;
844         link->reverse->flags = link->flags;
845
846         return 0;
847 }
848
849 int __media_entity_setup_link(struct media_link *link, u32 flags)
850 {
851         const u32 mask = MEDIA_LNK_FL_ENABLED;
852         struct media_device *mdev;
853         struct media_entity *source, *sink;
854         int ret = -EBUSY;
855
856         if (link == NULL)
857                 return -EINVAL;
858
859         /* The non-modifiable link flags must not be modified. */
860         if ((link->flags & ~mask) != (flags & ~mask))
861                 return -EINVAL;
862
863         if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
864                 return link->flags == flags ? 0 : -EINVAL;
865
866         if (link->flags == flags)
867                 return 0;
868
869         source = link->source->entity;
870         sink = link->sink->entity;
871
872         if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
873             (source->stream_count || sink->stream_count))
874                 return -EBUSY;
875
876         mdev = source->graph_obj.mdev;
877
878         if (mdev->ops && mdev->ops->link_notify) {
879                 ret = mdev->ops->link_notify(link, flags,
880                                              MEDIA_DEV_NOTIFY_PRE_LINK_CH);
881                 if (ret < 0)
882                         return ret;
883         }
884
885         ret = __media_entity_setup_link_notify(link, flags);
886
887         if (mdev->ops && mdev->ops->link_notify)
888                 mdev->ops->link_notify(link, flags,
889                                        MEDIA_DEV_NOTIFY_POST_LINK_CH);
890
891         return ret;
892 }
893 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
894
895 int media_entity_setup_link(struct media_link *link, u32 flags)
896 {
897         int ret;
898
899         mutex_lock(&link->graph_obj.mdev->graph_mutex);
900         ret = __media_entity_setup_link(link, flags);
901         mutex_unlock(&link->graph_obj.mdev->graph_mutex);
902
903         return ret;
904 }
905 EXPORT_SYMBOL_GPL(media_entity_setup_link);
906
907 struct media_link *
908 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
909 {
910         struct media_link *link;
911
912         list_for_each_entry(link, &source->entity->links, list) {
913                 if (link->source->entity == source->entity &&
914                     link->source->index == source->index &&
915                     link->sink->entity == sink->entity &&
916                     link->sink->index == sink->index)
917                         return link;
918         }
919
920         return NULL;
921 }
922 EXPORT_SYMBOL_GPL(media_entity_find_link);
923
924 struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
925 {
926         struct media_link *link;
927
928         list_for_each_entry(link, &pad->entity->links, list) {
929                 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
930                         continue;
931
932                 if (link->source == pad)
933                         return link->sink;
934
935                 if (link->sink == pad)
936                         return link->source;
937         }
938
939         return NULL;
940
941 }
942 EXPORT_SYMBOL_GPL(media_entity_remote_pad);
943
944 static void media_interface_init(struct media_device *mdev,
945                                  struct media_interface *intf,
946                                  u32 gobj_type,
947                                  u32 intf_type, u32 flags)
948 {
949         intf->type = intf_type;
950         intf->flags = flags;
951         INIT_LIST_HEAD(&intf->links);
952
953         media_gobj_create(mdev, gobj_type, &intf->graph_obj);
954 }
955
956 /* Functions related to the media interface via device nodes */
957
958 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
959                                                 u32 type, u32 flags,
960                                                 u32 major, u32 minor)
961 {
962         struct media_intf_devnode *devnode;
963
964         devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
965         if (!devnode)
966                 return NULL;
967
968         devnode->major = major;
969         devnode->minor = minor;
970
971         media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
972                              type, flags);
973
974         return devnode;
975 }
976 EXPORT_SYMBOL_GPL(media_devnode_create);
977
978 void media_devnode_remove(struct media_intf_devnode *devnode)
979 {
980         media_remove_intf_links(&devnode->intf);
981         media_gobj_destroy(&devnode->intf.graph_obj);
982         kfree(devnode);
983 }
984 EXPORT_SYMBOL_GPL(media_devnode_remove);
985
986 struct media_link *media_create_intf_link(struct media_entity *entity,
987                                             struct media_interface *intf,
988                                             u32 flags)
989 {
990         struct media_link *link;
991
992         link = media_add_link(&intf->links);
993         if (link == NULL)
994                 return NULL;
995
996         link->intf = intf;
997         link->entity = entity;
998         link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
999
1000         /* Initialize graph object embedded at the new link */
1001         media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1002                         &link->graph_obj);
1003
1004         return link;
1005 }
1006 EXPORT_SYMBOL_GPL(media_create_intf_link);
1007
1008 void __media_remove_intf_link(struct media_link *link)
1009 {
1010         list_del(&link->list);
1011         media_gobj_destroy(&link->graph_obj);
1012         kfree(link);
1013 }
1014 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1015
1016 void media_remove_intf_link(struct media_link *link)
1017 {
1018         struct media_device *mdev = link->graph_obj.mdev;
1019
1020         /* Do nothing if the intf is not registered. */
1021         if (mdev == NULL)
1022                 return;
1023
1024         mutex_lock(&mdev->graph_mutex);
1025         __media_remove_intf_link(link);
1026         mutex_unlock(&mdev->graph_mutex);
1027 }
1028 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1029
1030 void __media_remove_intf_links(struct media_interface *intf)
1031 {
1032         struct media_link *link, *tmp;
1033
1034         list_for_each_entry_safe(link, tmp, &intf->links, list)
1035                 __media_remove_intf_link(link);
1036
1037 }
1038 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1039
1040 void media_remove_intf_links(struct media_interface *intf)
1041 {
1042         struct media_device *mdev = intf->graph_obj.mdev;
1043
1044         /* Do nothing if the intf is not registered. */
1045         if (mdev == NULL)
1046                 return;
1047
1048         mutex_lock(&mdev->graph_mutex);
1049         __media_remove_intf_links(intf);
1050         mutex_unlock(&mdev->graph_mutex);
1051 }
1052 EXPORT_SYMBOL_GPL(media_remove_intf_links);
1053
1054 struct media_link *media_create_ancillary_link(struct media_entity *primary,
1055                                                struct media_entity *ancillary)
1056 {
1057         struct media_link *link;
1058
1059         link = media_add_link(&primary->links);
1060         if (!link)
1061                 return ERR_PTR(-ENOMEM);
1062
1063         link->gobj0 = &primary->graph_obj;
1064         link->gobj1 = &ancillary->graph_obj;
1065         link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED |
1066                       MEDIA_LNK_FL_ANCILLARY_LINK;
1067
1068         /* Initialize graph object embedded in the new link */
1069         media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK,
1070                           &link->graph_obj);
1071
1072         return link;
1073 }
1074 EXPORT_SYMBOL_GPL(media_create_ancillary_link);