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