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