Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / media-entity.c
blobf7c6d64e60310c2fe83916fb83c9a162dc607d36
1 /*
2 * Media entity
4 * Copyright (C) 2010 Nokia Corporation
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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>
26 static inline const char *gobj_type(enum media_gobj_type type)
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";
42 static inline const char *intf_type(struct media_interface *intf)
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";
88 __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
89 int idx_max)
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;
97 bitmap_zero(ent_enum->bmap, idx_max);
98 ent_enum->idx_max = idx_max;
100 return 0;
102 EXPORT_SYMBOL_GPL(__media_entity_enum_init);
104 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
106 kfree(ent_enum->bmap);
108 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
111 * dev_dbg_obj - Prints in debug mode a change on some object
113 * @event_name: Name of the event to report. Could be __func__
114 * @gobj: Pointer to the object
116 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
117 * won't produce any code.
119 static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
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:
131 struct media_link *link = gobj_to_link(gobj);
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;
142 case MEDIA_GRAPH_PAD:
144 struct media_pad *pad = gobj_to_pad(gobj);
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;
154 case MEDIA_GRAPH_INTF_DEVNODE:
156 struct media_interface *intf = gobj_to_intf(gobj);
157 struct media_intf_devnode *devnode = intf_to_devnode(intf);
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;
167 #endif
170 void media_gobj_create(struct media_device *mdev,
171 enum media_gobj_type type,
172 struct media_gobj *gobj)
174 BUG_ON(!mdev);
176 gobj->mdev = mdev;
178 /* Create a per-type unique object ID */
179 gobj->id = media_gobj_gen_id(type, ++mdev->id);
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;
196 mdev->topology_version++;
198 dev_dbg_obj(__func__, gobj);
201 void media_gobj_destroy(struct media_gobj *gobj)
203 /* Do nothing if the object is not linked. */
204 if (gobj->mdev == NULL)
205 return;
207 dev_dbg_obj(__func__, gobj);
209 gobj->mdev->topology_version++;
211 /* Remove the object from mdev list */
212 list_del(&gobj->list);
214 gobj->mdev = NULL;
218 * TODO: Get rid of this.
220 #define MEDIA_ENTITY_MAX_PADS 512
222 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
223 struct media_pad *pads)
225 struct media_device *mdev = entity->graph_obj.mdev;
226 unsigned int i;
228 if (num_pads >= MEDIA_ENTITY_MAX_PADS)
229 return -E2BIG;
231 entity->num_pads = num_pads;
232 entity->pads = pads;
234 if (mdev)
235 mutex_lock(&mdev->graph_mutex);
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);
245 if (mdev)
246 mutex_unlock(&mdev->graph_mutex);
248 return 0;
250 EXPORT_SYMBOL_GPL(media_entity_pads_init);
252 /* -----------------------------------------------------------------------------
253 * Graph traversal
256 static struct media_entity *
257 media_entity_other(struct media_entity *entity, struct media_link *link)
259 if (link->source->entity == entity)
260 return link->sink->entity;
261 else
262 return link->source->entity;
265 /* push an entity to traversal stack */
266 static void stack_push(struct media_graph *graph,
267 struct media_entity *entity)
269 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
270 WARN_ON(1);
271 return;
273 graph->top++;
274 graph->stack[graph->top].link = entity->links.next;
275 graph->stack[graph->top].entity = entity;
278 static struct media_entity *stack_pop(struct media_graph *graph)
280 struct media_entity *entity;
282 entity = graph->stack[graph->top].entity;
283 graph->top--;
285 return entity;
288 #define link_top(en) ((en)->stack[(en)->top].link)
289 #define stack_top(en) ((en)->stack[(en)->top].entity)
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
296 * Reserve resources for graph walk in media device's current
297 * state. The memory must be released using
298 * media_graph_walk_free().
300 * Returns error on failure, zero on success.
302 __must_check int media_graph_walk_init(
303 struct media_graph *graph, struct media_device *mdev)
305 return media_entity_enum_init(&graph->ent_enum, mdev);
307 EXPORT_SYMBOL_GPL(media_graph_walk_init);
310 * media_graph_walk_cleanup - Release resources related to graph walking
311 * @graph: Media graph structure that was used to walk the graph
313 void media_graph_walk_cleanup(struct media_graph *graph)
315 media_entity_enum_cleanup(&graph->ent_enum);
317 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
319 void media_graph_walk_start(struct media_graph *graph,
320 struct media_entity *entity)
322 media_entity_enum_zero(&graph->ent_enum);
323 media_entity_enum_set(&graph->ent_enum, entity);
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);
331 EXPORT_SYMBOL_GPL(media_graph_walk_start);
333 static void media_graph_walk_iter(struct media_graph *graph)
335 struct media_entity *entity = stack_top(graph);
336 struct media_link *link;
337 struct media_entity *next;
339 link = list_entry(link_top(graph), typeof(*link), list);
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;
351 /* Get the entity in the other end of the link . */
352 next = media_entity_other(entity, link);
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;
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);
370 struct media_entity *media_graph_walk_next(struct media_graph *graph)
372 struct media_entity *entity;
374 if (stack_top(graph) == NULL)
375 return NULL;
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.
382 while (link_top(graph) != &stack_top(graph)->links)
383 media_graph_walk_iter(graph);
385 entity = stack_pop(graph);
386 dev_dbg(entity->graph_obj.mdev->dev,
387 "walk: returning entity '%s'\n", entity->name);
389 return entity;
391 EXPORT_SYMBOL_GPL(media_graph_walk_next);
393 int media_entity_get_fwnode_pad(struct media_entity *entity,
394 struct fwnode_handle *fwnode,
395 unsigned long direction_flags)
397 struct fwnode_endpoint endpoint;
398 unsigned int i;
399 int ret;
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;
407 return -ENXIO;
410 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
411 if (ret)
412 return ret;
414 ret = entity->ops->get_fwnode_pad(&endpoint);
415 if (ret < 0)
416 return ret;
418 if (ret >= entity->num_pads)
419 return -ENXIO;
421 if (!(entity->pads[ret].flags & direction_flags))
422 return -ENXIO;
424 return ret;
426 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
428 /* -----------------------------------------------------------------------------
429 * Pipeline management
432 __must_check int __media_pipeline_start(struct media_entity *entity,
433 struct media_pipeline *pipe)
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;
441 if (!pipe->streaming_count++) {
442 ret = media_graph_walk_init(&pipe->graph, mdev);
443 if (ret)
444 goto error_graph_walk_start;
447 media_graph_walk_start(&pipe->graph, entity);
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);
453 entity->stream_count++;
455 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
456 ret = -EBUSY;
457 goto error;
460 entity->pipe = pipe;
462 /* Already streaming --- no need to check. */
463 if (entity->stream_count > 1)
464 continue;
466 if (!entity->ops || !entity->ops->link_validate)
467 continue;
469 bitmap_zero(active, entity->num_pads);
470 bitmap_fill(has_no_links, entity->num_pads);
472 list_for_each_entry(link, &entity->links, list) {
473 struct media_pad *pad = link->sink->entity == entity
474 ? link->sink : link->source;
476 /* Mark that a pad is connected by a link. */
477 bitmap_clear(has_no_links, pad->index, 1);
480 * Pads that either do not need to connect or
481 * are connected through an enabled link are
482 * fine.
484 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
485 link->flags & MEDIA_LNK_FL_ENABLED)
486 bitmap_set(active, pad->index, 1);
489 * Link validation will only take place for
490 * sink ends of the link that are enabled.
492 if (link->sink != pad ||
493 !(link->flags & MEDIA_LNK_FL_ENABLED))
494 continue;
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;
507 /* Either no links or validated links are fine. */
508 bitmap_or(active, active, has_no_links, entity->num_pads);
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;
521 return 0;
523 error:
525 * Link validation on graph failed. We revert what we did and
526 * return the error.
528 media_graph_walk_start(graph, entity_err);
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;
539 * We haven't increased stream_count further than this
540 * so we quit here.
542 if (entity_err == entity)
543 break;
546 error_graph_walk_start:
547 if (!--pipe->streaming_count)
548 media_graph_walk_cleanup(graph);
550 return ret;
552 EXPORT_SYMBOL_GPL(__media_pipeline_start);
554 __must_check int media_pipeline_start(struct media_entity *entity,
555 struct media_pipeline *pipe)
557 struct media_device *mdev = entity->graph_obj.mdev;
558 int ret;
560 mutex_lock(&mdev->graph_mutex);
561 ret = __media_pipeline_start(entity, pipe);
562 mutex_unlock(&mdev->graph_mutex);
563 return ret;
565 EXPORT_SYMBOL_GPL(media_pipeline_start);
567 void __media_pipeline_stop(struct media_entity *entity)
569 struct media_graph *graph = &entity->pipe->graph;
570 struct media_pipeline *pipe = entity->pipe;
573 * If the following check fails, the driver has performed an
574 * unbalanced call to media_pipeline_stop()
576 if (WARN_ON(!pipe))
577 return;
579 media_graph_walk_start(graph, entity);
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;
590 if (!--pipe->streaming_count)
591 media_graph_walk_cleanup(graph);
594 EXPORT_SYMBOL_GPL(__media_pipeline_stop);
596 void media_pipeline_stop(struct media_entity *entity)
598 struct media_device *mdev = entity->graph_obj.mdev;
600 mutex_lock(&mdev->graph_mutex);
601 __media_pipeline_stop(entity);
602 mutex_unlock(&mdev->graph_mutex);
604 EXPORT_SYMBOL_GPL(media_pipeline_stop);
606 /* -----------------------------------------------------------------------------
607 * Module use count
610 struct media_entity *media_entity_get(struct media_entity *entity)
612 if (entity == NULL)
613 return NULL;
615 if (entity->graph_obj.mdev->dev &&
616 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
617 return NULL;
619 return entity;
621 EXPORT_SYMBOL_GPL(media_entity_get);
623 void media_entity_put(struct media_entity *entity)
625 if (entity == NULL)
626 return;
628 if (entity->graph_obj.mdev->dev)
629 module_put(entity->graph_obj.mdev->dev->driver->owner);
631 EXPORT_SYMBOL_GPL(media_entity_put);
633 /* -----------------------------------------------------------------------------
634 * Links management
637 static struct media_link *media_add_link(struct list_head *head)
639 struct media_link *link;
641 link = kzalloc(sizeof(*link), GFP_KERNEL);
642 if (link == NULL)
643 return NULL;
645 list_add_tail(&link->list, head);
647 return link;
650 static void __media_entity_remove_link(struct media_entity *entity,
651 struct media_link *link)
653 struct media_link *rlink, *tmp;
654 struct media_entity *remote;
656 if (link->source->entity == entity)
657 remote = link->sink->entity;
658 else
659 remote = link->source->entity;
661 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
662 if (rlink != link->reverse)
663 continue;
665 if (link->source->entity == entity)
666 remote->num_backlinks--;
668 /* Remove the remote link */
669 list_del(&rlink->list);
670 media_gobj_destroy(&rlink->graph_obj);
671 kfree(rlink);
673 if (--remote->num_links == 0)
674 break;
676 list_del(&link->list);
677 media_gobj_destroy(&link->graph_obj);
678 kfree(link);
682 media_create_pad_link(struct media_entity *source, u16 source_pad,
683 struct media_entity *sink, u16 sink_pad, u32 flags)
685 struct media_link *link;
686 struct media_link *backlink;
688 BUG_ON(source == NULL || sink == NULL);
689 BUG_ON(source_pad >= source->num_pads);
690 BUG_ON(sink_pad >= sink->num_pads);
692 link = media_add_link(&source->links);
693 if (link == NULL)
694 return -ENOMEM;
696 link->source = &source->pads[source_pad];
697 link->sink = &sink->pads[sink_pad];
698 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
700 /* Initialize graph object embedded at the new link */
701 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
702 &link->graph_obj);
704 /* Create the backlink. Backlinks are used to help graph traversal and
705 * are not reported to userspace.
707 backlink = media_add_link(&sink->links);
708 if (backlink == NULL) {
709 __media_entity_remove_link(source, link);
710 return -ENOMEM;
713 backlink->source = &source->pads[source_pad];
714 backlink->sink = &sink->pads[sink_pad];
715 backlink->flags = flags;
716 backlink->is_backlink = true;
718 /* Initialize graph object embedded at the new link */
719 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
720 &backlink->graph_obj);
722 link->reverse = backlink;
723 backlink->reverse = link;
725 sink->num_backlinks++;
726 sink->num_links++;
727 source->num_links++;
729 return 0;
731 EXPORT_SYMBOL_GPL(media_create_pad_link);
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)
743 struct media_entity *entity;
744 unsigned function;
745 int ret;
747 /* Trivial case: 1:1 relation */
748 if (source && sink)
749 return media_create_pad_link(source, source_pad,
750 sink, sink_pad, flags);
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);
771 return 0;
774 /* Handle 1:n and n:1 cases */
775 if (source)
776 function = sink_function;
777 else
778 function = source_function;
780 media_device_for_each_entity(entity, mdev) {
781 if (entity->function != function)
782 continue;
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);
794 return 0;
796 EXPORT_SYMBOL_GPL(media_create_pad_links);
798 void __media_entity_remove_links(struct media_entity *entity)
800 struct media_link *link, *tmp;
802 list_for_each_entry_safe(link, tmp, &entity->links, list)
803 __media_entity_remove_link(entity, link);
805 entity->num_links = 0;
806 entity->num_backlinks = 0;
808 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
810 void media_entity_remove_links(struct media_entity *entity)
812 struct media_device *mdev = entity->graph_obj.mdev;
814 /* Do nothing if the entity is not registered. */
815 if (mdev == NULL)
816 return;
818 mutex_lock(&mdev->graph_mutex);
819 __media_entity_remove_links(entity);
820 mutex_unlock(&mdev->graph_mutex);
822 EXPORT_SYMBOL_GPL(media_entity_remove_links);
824 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
826 int ret;
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;
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;
842 link->flags = flags;
843 link->reverse->flags = link->flags;
845 return 0;
848 int __media_entity_setup_link(struct media_link *link, u32 flags)
850 const u32 mask = MEDIA_LNK_FL_ENABLED;
851 struct media_device *mdev;
852 struct media_entity *source, *sink;
853 int ret = -EBUSY;
855 if (link == NULL)
856 return -EINVAL;
858 /* The non-modifiable link flags must not be modified. */
859 if ((link->flags & ~mask) != (flags & ~mask))
860 return -EINVAL;
862 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
863 return link->flags == flags ? 0 : -EINVAL;
865 if (link->flags == flags)
866 return 0;
868 source = link->source->entity;
869 sink = link->sink->entity;
871 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
872 (source->stream_count || sink->stream_count))
873 return -EBUSY;
875 mdev = source->graph_obj.mdev;
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;
884 ret = __media_entity_setup_link_notify(link, flags);
886 if (mdev->ops && mdev->ops->link_notify)
887 mdev->ops->link_notify(link, flags,
888 MEDIA_DEV_NOTIFY_POST_LINK_CH);
890 return ret;
892 EXPORT_SYMBOL_GPL(__media_entity_setup_link);
894 int media_entity_setup_link(struct media_link *link, u32 flags)
896 int ret;
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);
902 return ret;
904 EXPORT_SYMBOL_GPL(media_entity_setup_link);
906 struct media_link *
907 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
909 struct media_link *link;
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;
919 return NULL;
921 EXPORT_SYMBOL_GPL(media_entity_find_link);
923 struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
925 struct media_link *link;
927 list_for_each_entry(link, &pad->entity->links, list) {
928 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
929 continue;
931 if (link->source == pad)
932 return link->sink;
934 if (link->sink == pad)
935 return link->source;
938 return NULL;
941 EXPORT_SYMBOL_GPL(media_entity_remote_pad);
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)
948 intf->type = intf_type;
949 intf->flags = flags;
950 INIT_LIST_HEAD(&intf->links);
952 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
955 /* Functions related to the media interface via device nodes */
957 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
958 u32 type, u32 flags,
959 u32 major, u32 minor)
961 struct media_intf_devnode *devnode;
963 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
964 if (!devnode)
965 return NULL;
967 devnode->major = major;
968 devnode->minor = minor;
970 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
971 type, flags);
973 return devnode;
975 EXPORT_SYMBOL_GPL(media_devnode_create);
977 void media_devnode_remove(struct media_intf_devnode *devnode)
979 media_remove_intf_links(&devnode->intf);
980 media_gobj_destroy(&devnode->intf.graph_obj);
981 kfree(devnode);
983 EXPORT_SYMBOL_GPL(media_devnode_remove);
985 struct media_link *media_create_intf_link(struct media_entity *entity,
986 struct media_interface *intf,
987 u32 flags)
989 struct media_link *link;
991 link = media_add_link(&intf->links);
992 if (link == NULL)
993 return NULL;
995 link->intf = intf;
996 link->entity = entity;
997 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
999 /* Initialize graph object embedded at the new link */
1000 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
1001 &link->graph_obj);
1003 return link;
1005 EXPORT_SYMBOL_GPL(media_create_intf_link);
1007 void __media_remove_intf_link(struct media_link *link)
1009 list_del(&link->list);
1010 media_gobj_destroy(&link->graph_obj);
1011 kfree(link);
1013 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
1015 void media_remove_intf_link(struct media_link *link)
1017 struct media_device *mdev = link->graph_obj.mdev;
1019 /* Do nothing if the intf is not registered. */
1020 if (mdev == NULL)
1021 return;
1023 mutex_lock(&mdev->graph_mutex);
1024 __media_remove_intf_link(link);
1025 mutex_unlock(&mdev->graph_mutex);
1027 EXPORT_SYMBOL_GPL(media_remove_intf_link);
1029 void __media_remove_intf_links(struct media_interface *intf)
1031 struct media_link *link, *tmp;
1033 list_for_each_entry_safe(link, tmp, &intf->links, list)
1034 __media_remove_intf_link(link);
1037 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1039 void media_remove_intf_links(struct media_interface *intf)
1041 struct media_device *mdev = intf->graph_obj.mdev;
1043 /* Do nothing if the intf is not registered. */
1044 if (mdev == NULL)
1045 return;
1047 mutex_lock(&mdev->graph_mutex);
1048 __media_remove_intf_links(intf);
1049 mutex_unlock(&mdev->graph_mutex);
1051 EXPORT_SYMBOL_GPL(media_remove_intf_links);