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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/bitmap.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <media/media-entity.h>
27 #include <media/media-device.h>
29 static inline const char *gobj_type(enum media_gobj_type type)
32 case MEDIA_GRAPH_ENTITY:
36 case MEDIA_GRAPH_LINK:
38 case MEDIA_GRAPH_INTF_DEVNODE:
39 return "intf-devnode";
45 static inline const char *intf_type(struct media_interface *intf)
48 case MEDIA_INTF_T_DVB_FE:
50 case MEDIA_INTF_T_DVB_DEMUX:
52 case MEDIA_INTF_T_DVB_DVR:
54 case MEDIA_INTF_T_DVB_CA:
56 case MEDIA_INTF_T_DVB_NET:
58 case MEDIA_INTF_T_V4L_VIDEO:
60 case MEDIA_INTF_T_V4L_VBI:
62 case MEDIA_INTF_T_V4L_RADIO:
64 case MEDIA_INTF_T_V4L_SUBDEV:
66 case MEDIA_INTF_T_V4L_SWRADIO:
69 return "unknown-intf";
73 __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
76 ent_enum->bmap = kcalloc(DIV_ROUND_UP(idx_max, BITS_PER_LONG),
77 sizeof(long), GFP_KERNEL);
81 bitmap_zero(ent_enum->bmap, idx_max);
82 ent_enum->idx_max = idx_max;
86 EXPORT_SYMBOL_GPL(__media_entity_enum_init);
88 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
90 kfree(ent_enum->bmap);
92 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
95 * dev_dbg_obj - Prints in debug mode a change on some object
97 * @event_name: Name of the event to report. Could be __func__
98 * @gobj: Pointer to the object
100 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
101 * won't produce any code.
103 static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
105 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
106 switch (media_type(gobj)) {
107 case MEDIA_GRAPH_ENTITY:
108 dev_dbg(gobj->mdev->dev,
109 "%s id %u: entity '%s'\n",
110 event_name, media_id(gobj),
111 gobj_to_entity(gobj)->name);
113 case MEDIA_GRAPH_LINK:
115 struct media_link *link = gobj_to_link(gobj);
117 dev_dbg(gobj->mdev->dev,
118 "%s id %u: %s link id %u ==> id %u\n",
119 event_name, media_id(gobj),
120 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
121 "data" : "interface",
122 media_id(link->gobj0),
123 media_id(link->gobj1));
126 case MEDIA_GRAPH_PAD:
128 struct media_pad *pad = gobj_to_pad(gobj);
130 dev_dbg(gobj->mdev->dev,
131 "%s id %u: %s%spad '%s':%d\n",
132 event_name, media_id(gobj),
133 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
134 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
135 pad->entity->name, pad->index);
138 case MEDIA_GRAPH_INTF_DEVNODE:
140 struct media_interface *intf = gobj_to_intf(gobj);
141 struct media_intf_devnode *devnode = intf_to_devnode(intf);
143 dev_dbg(gobj->mdev->dev,
144 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
145 event_name, media_id(gobj),
147 devnode->major, devnode->minor);
154 void media_gobj_create(struct media_device *mdev,
155 enum media_gobj_type type,
156 struct media_gobj *gobj)
162 /* Create a per-type unique object ID */
163 gobj->id = media_gobj_gen_id(type, ++mdev->id);
166 case MEDIA_GRAPH_ENTITY:
167 list_add_tail(&gobj->list, &mdev->entities);
169 case MEDIA_GRAPH_PAD:
170 list_add_tail(&gobj->list, &mdev->pads);
172 case MEDIA_GRAPH_LINK:
173 list_add_tail(&gobj->list, &mdev->links);
175 case MEDIA_GRAPH_INTF_DEVNODE:
176 list_add_tail(&gobj->list, &mdev->interfaces);
180 mdev->topology_version++;
182 dev_dbg_obj(__func__, gobj);
185 void media_gobj_destroy(struct media_gobj *gobj)
187 dev_dbg_obj(__func__, gobj);
189 gobj->mdev->topology_version++;
191 /* Remove the object from mdev list */
192 list_del(&gobj->list);
195 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
196 struct media_pad *pads)
198 struct media_device *mdev = entity->graph_obj.mdev;
201 entity->num_pads = num_pads;
205 spin_lock(&mdev->lock);
207 for (i = 0; i < num_pads; i++) {
208 pads[i].entity = entity;
211 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
212 &entity->pads[i].graph_obj);
216 spin_unlock(&mdev->lock);
220 EXPORT_SYMBOL_GPL(media_entity_pads_init);
222 /* -----------------------------------------------------------------------------
226 static struct media_entity *
227 media_entity_other(struct media_entity *entity, struct media_link *link)
229 if (link->source->entity == entity)
230 return link->sink->entity;
232 return link->source->entity;
235 /* push an entity to traversal stack */
236 static void stack_push(struct media_entity_graph *graph,
237 struct media_entity *entity)
239 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
244 graph->stack[graph->top].link = entity->links.next;
245 graph->stack[graph->top].entity = entity;
248 static struct media_entity *stack_pop(struct media_entity_graph *graph)
250 struct media_entity *entity;
252 entity = graph->stack[graph->top].entity;
258 #define link_top(en) ((en)->stack[(en)->top].link)
259 #define stack_top(en) ((en)->stack[(en)->top].entity)
262 * TODO: Get rid of this.
264 #define MEDIA_ENTITY_MAX_PADS 512
267 * media_entity_graph_walk_init - Allocate resources for graph walk
268 * @graph: Media graph structure that will be used to walk the graph
269 * @mdev: Media device
271 * Reserve resources for graph walk in media device's current
272 * state. The memory must be released using
273 * media_entity_graph_walk_free().
275 * Returns error on failure, zero on success.
277 __must_check int media_entity_graph_walk_init(
278 struct media_entity_graph *graph, struct media_device *mdev)
280 return media_entity_enum_init(&graph->ent_enum, mdev);
282 EXPORT_SYMBOL_GPL(media_entity_graph_walk_init);
285 * media_entity_graph_walk_cleanup - Release resources related to graph walking
286 * @graph: Media graph structure that was used to walk the graph
288 void media_entity_graph_walk_cleanup(struct media_entity_graph *graph)
290 media_entity_enum_cleanup(&graph->ent_enum);
292 EXPORT_SYMBOL_GPL(media_entity_graph_walk_cleanup);
294 void media_entity_graph_walk_start(struct media_entity_graph *graph,
295 struct media_entity *entity)
297 media_entity_enum_zero(&graph->ent_enum);
298 media_entity_enum_set(&graph->ent_enum, entity);
301 graph->stack[graph->top].entity = NULL;
302 stack_push(graph, entity);
304 EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
306 struct media_entity *
307 media_entity_graph_walk_next(struct media_entity_graph *graph)
309 if (stack_top(graph) == NULL)
313 * Depth first search. Push entity to stack and continue from
314 * top of the stack until no more entities on the level can be
317 while (link_top(graph) != &stack_top(graph)->links) {
318 struct media_entity *entity = stack_top(graph);
319 struct media_link *link;
320 struct media_entity *next;
322 link = list_entry(link_top(graph), typeof(*link), list);
324 /* The link is not enabled so we do not follow. */
325 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
326 link_top(graph) = link_top(graph)->next;
330 /* Get the entity in the other end of the link . */
331 next = media_entity_other(entity, link);
333 /* Has the entity already been visited? */
334 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
335 link_top(graph) = link_top(graph)->next;
339 /* Push the new entity to stack and start over. */
340 link_top(graph) = link_top(graph)->next;
341 stack_push(graph, next);
344 return stack_pop(graph);
346 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
348 /* -----------------------------------------------------------------------------
349 * Pipeline management
352 __must_check int media_entity_pipeline_start(struct media_entity *entity,
353 struct media_pipeline *pipe)
355 struct media_device *mdev = entity->graph_obj.mdev;
356 struct media_entity_graph *graph = &pipe->graph;
357 struct media_entity *entity_err = entity;
358 struct media_link *link;
361 mutex_lock(&mdev->graph_mutex);
363 if (!pipe->streaming_count++) {
364 ret = media_entity_graph_walk_init(&pipe->graph, mdev);
366 goto error_graph_walk_start;
369 media_entity_graph_walk_start(&pipe->graph, entity);
371 while ((entity = media_entity_graph_walk_next(graph))) {
372 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
373 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
375 entity->stream_count++;
377 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
384 /* Already streaming --- no need to check. */
385 if (entity->stream_count > 1)
388 if (!entity->ops || !entity->ops->link_validate)
391 bitmap_zero(active, entity->num_pads);
392 bitmap_fill(has_no_links, entity->num_pads);
394 list_for_each_entry(link, &entity->links, list) {
395 struct media_pad *pad = link->sink->entity == entity
396 ? link->sink : link->source;
398 /* Mark that a pad is connected by a link. */
399 bitmap_clear(has_no_links, pad->index, 1);
402 * Pads that either do not need to connect or
403 * are connected through an enabled link are
406 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
407 link->flags & MEDIA_LNK_FL_ENABLED)
408 bitmap_set(active, pad->index, 1);
411 * Link validation will only take place for
412 * sink ends of the link that are enabled.
414 if (link->sink != pad ||
415 !(link->flags & MEDIA_LNK_FL_ENABLED))
418 ret = entity->ops->link_validate(link);
419 if (ret < 0 && ret != -ENOIOCTLCMD) {
420 dev_dbg(entity->graph_obj.mdev->dev,
421 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
422 link->source->entity->name,
424 entity->name, link->sink->index, ret);
429 /* Either no links or validated links are fine. */
430 bitmap_or(active, active, has_no_links, entity->num_pads);
432 if (!bitmap_full(active, entity->num_pads)) {
434 dev_dbg(entity->graph_obj.mdev->dev,
435 "\"%s\":%u must be connected by an enabled link\n",
437 (unsigned)find_first_zero_bit(
438 active, entity->num_pads));
443 mutex_unlock(&mdev->graph_mutex);
449 * Link validation on graph failed. We revert what we did and
452 media_entity_graph_walk_start(graph, entity_err);
454 while ((entity_err = media_entity_graph_walk_next(graph))) {
455 /* don't let the stream_count go negative */
456 if (entity->stream_count > 0) {
457 entity_err->stream_count--;
458 if (entity_err->stream_count == 0)
459 entity_err->pipe = NULL;
463 * We haven't increased stream_count further than this
466 if (entity_err == entity)
470 error_graph_walk_start:
471 if (!--pipe->streaming_count)
472 media_entity_graph_walk_cleanup(graph);
474 mutex_unlock(&mdev->graph_mutex);
478 EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
480 void media_entity_pipeline_stop(struct media_entity *entity)
482 struct media_device *mdev = entity->graph_obj.mdev;
483 struct media_entity_graph *graph = &entity->pipe->graph;
484 struct media_pipeline *pipe = entity->pipe;
486 mutex_lock(&mdev->graph_mutex);
488 WARN_ON(!pipe->streaming_count);
489 media_entity_graph_walk_start(graph, entity);
491 while ((entity = media_entity_graph_walk_next(graph))) {
492 /* don't let the stream_count go negative */
493 if (entity->stream_count > 0) {
494 entity->stream_count--;
495 if (entity->stream_count == 0)
500 if (!--pipe->streaming_count)
501 media_entity_graph_walk_cleanup(graph);
503 mutex_unlock(&mdev->graph_mutex);
505 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
507 /* -----------------------------------------------------------------------------
511 struct media_entity *media_entity_get(struct media_entity *entity)
516 if (entity->graph_obj.mdev->dev &&
517 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
522 EXPORT_SYMBOL_GPL(media_entity_get);
524 void media_entity_put(struct media_entity *entity)
529 if (entity->graph_obj.mdev->dev)
530 module_put(entity->graph_obj.mdev->dev->driver->owner);
532 EXPORT_SYMBOL_GPL(media_entity_put);
534 /* -----------------------------------------------------------------------------
538 static struct media_link *media_add_link(struct list_head *head)
540 struct media_link *link;
542 link = kzalloc(sizeof(*link), GFP_KERNEL);
546 list_add_tail(&link->list, head);
551 static void __media_entity_remove_link(struct media_entity *entity,
552 struct media_link *link)
554 struct media_link *rlink, *tmp;
555 struct media_entity *remote;
557 if (link->source->entity == entity)
558 remote = link->sink->entity;
560 remote = link->source->entity;
562 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
563 if (rlink != link->reverse)
566 if (link->source->entity == entity)
567 remote->num_backlinks--;
569 /* Remove the remote link */
570 list_del(&rlink->list);
571 media_gobj_destroy(&rlink->graph_obj);
574 if (--remote->num_links == 0)
577 list_del(&link->list);
578 media_gobj_destroy(&link->graph_obj);
583 media_create_pad_link(struct media_entity *source, u16 source_pad,
584 struct media_entity *sink, u16 sink_pad, u32 flags)
586 struct media_link *link;
587 struct media_link *backlink;
589 BUG_ON(source == NULL || sink == NULL);
590 BUG_ON(source_pad >= source->num_pads);
591 BUG_ON(sink_pad >= sink->num_pads);
593 link = media_add_link(&source->links);
597 link->source = &source->pads[source_pad];
598 link->sink = &sink->pads[sink_pad];
599 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
601 /* Initialize graph object embedded at the new link */
602 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
605 /* Create the backlink. Backlinks are used to help graph traversal and
606 * are not reported to userspace.
608 backlink = media_add_link(&sink->links);
609 if (backlink == NULL) {
610 __media_entity_remove_link(source, link);
614 backlink->source = &source->pads[source_pad];
615 backlink->sink = &sink->pads[sink_pad];
616 backlink->flags = flags;
617 backlink->is_backlink = true;
619 /* Initialize graph object embedded at the new link */
620 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
621 &backlink->graph_obj);
623 link->reverse = backlink;
624 backlink->reverse = link;
626 sink->num_backlinks++;
632 EXPORT_SYMBOL_GPL(media_create_pad_link);
634 int media_create_pad_links(const struct media_device *mdev,
635 const u32 source_function,
636 struct media_entity *source,
637 const u16 source_pad,
638 const u32 sink_function,
639 struct media_entity *sink,
642 const bool allow_both_undefined)
644 struct media_entity *entity;
648 /* Trivial case: 1:1 relation */
650 return media_create_pad_link(source, source_pad,
651 sink, sink_pad, flags);
653 /* Worse case scenario: n:n relation */
654 if (!source && !sink) {
655 if (!allow_both_undefined)
657 media_device_for_each_entity(source, mdev) {
658 if (source->function != source_function)
660 media_device_for_each_entity(sink, mdev) {
661 if (sink->function != sink_function)
663 ret = media_create_pad_link(source, source_pad,
668 flags &= ~(MEDIA_LNK_FL_ENABLED |
669 MEDIA_LNK_FL_IMMUTABLE);
675 /* Handle 1:n and n:1 cases */
677 function = sink_function;
679 function = source_function;
681 media_device_for_each_entity(entity, mdev) {
682 if (entity->function != function)
686 ret = media_create_pad_link(source, source_pad,
687 entity, sink_pad, flags);
689 ret = media_create_pad_link(entity, source_pad,
690 sink, sink_pad, flags);
693 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
697 EXPORT_SYMBOL_GPL(media_create_pad_links);
699 void __media_entity_remove_links(struct media_entity *entity)
701 struct media_link *link, *tmp;
703 list_for_each_entry_safe(link, tmp, &entity->links, list)
704 __media_entity_remove_link(entity, link);
706 entity->num_links = 0;
707 entity->num_backlinks = 0;
709 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
711 void media_entity_remove_links(struct media_entity *entity)
713 struct media_device *mdev = entity->graph_obj.mdev;
715 /* Do nothing if the entity is not registered. */
719 spin_lock(&mdev->lock);
720 __media_entity_remove_links(entity);
721 spin_unlock(&mdev->lock);
723 EXPORT_SYMBOL_GPL(media_entity_remove_links);
725 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
729 /* Notify both entities. */
730 ret = media_entity_call(link->source->entity, link_setup,
731 link->source, link->sink, flags);
732 if (ret < 0 && ret != -ENOIOCTLCMD)
735 ret = media_entity_call(link->sink->entity, link_setup,
736 link->sink, link->source, flags);
737 if (ret < 0 && ret != -ENOIOCTLCMD) {
738 media_entity_call(link->source->entity, link_setup,
739 link->source, link->sink, link->flags);
744 link->reverse->flags = link->flags;
749 int __media_entity_setup_link(struct media_link *link, u32 flags)
751 const u32 mask = MEDIA_LNK_FL_ENABLED;
752 struct media_device *mdev;
753 struct media_entity *source, *sink;
759 /* The non-modifiable link flags must not be modified. */
760 if ((link->flags & ~mask) != (flags & ~mask))
763 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
764 return link->flags == flags ? 0 : -EINVAL;
766 if (link->flags == flags)
769 source = link->source->entity;
770 sink = link->sink->entity;
772 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
773 (source->stream_count || sink->stream_count))
776 mdev = source->graph_obj.mdev;
778 if (mdev->link_notify) {
779 ret = mdev->link_notify(link, flags,
780 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
785 ret = __media_entity_setup_link_notify(link, flags);
787 if (mdev->link_notify)
788 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
793 int media_entity_setup_link(struct media_link *link, u32 flags)
797 mutex_lock(&link->graph_obj.mdev->graph_mutex);
798 ret = __media_entity_setup_link(link, flags);
799 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
803 EXPORT_SYMBOL_GPL(media_entity_setup_link);
806 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
808 struct media_link *link;
810 list_for_each_entry(link, &source->entity->links, list) {
811 if (link->source->entity == source->entity &&
812 link->source->index == source->index &&
813 link->sink->entity == sink->entity &&
814 link->sink->index == sink->index)
820 EXPORT_SYMBOL_GPL(media_entity_find_link);
822 struct media_pad *media_entity_remote_pad(struct media_pad *pad)
824 struct media_link *link;
826 list_for_each_entry(link, &pad->entity->links, list) {
827 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
830 if (link->source == pad)
833 if (link->sink == pad)
840 EXPORT_SYMBOL_GPL(media_entity_remote_pad);
842 static void media_interface_init(struct media_device *mdev,
843 struct media_interface *intf,
845 u32 intf_type, u32 flags)
847 intf->type = intf_type;
849 INIT_LIST_HEAD(&intf->links);
851 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
854 /* Functions related to the media interface via device nodes */
856 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
858 u32 major, u32 minor)
860 struct media_intf_devnode *devnode;
862 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
866 devnode->major = major;
867 devnode->minor = minor;
869 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
874 EXPORT_SYMBOL_GPL(media_devnode_create);
876 void media_devnode_remove(struct media_intf_devnode *devnode)
878 media_remove_intf_links(&devnode->intf);
879 media_gobj_destroy(&devnode->intf.graph_obj);
882 EXPORT_SYMBOL_GPL(media_devnode_remove);
884 struct media_link *media_create_intf_link(struct media_entity *entity,
885 struct media_interface *intf,
888 struct media_link *link;
890 link = media_add_link(&intf->links);
895 link->entity = entity;
896 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
898 /* Initialize graph object embedded at the new link */
899 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
904 EXPORT_SYMBOL_GPL(media_create_intf_link);
906 void __media_remove_intf_link(struct media_link *link)
908 list_del(&link->list);
909 media_gobj_destroy(&link->graph_obj);
912 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
914 void media_remove_intf_link(struct media_link *link)
916 struct media_device *mdev = link->graph_obj.mdev;
918 /* Do nothing if the intf is not registered. */
922 spin_lock(&mdev->lock);
923 __media_remove_intf_link(link);
924 spin_unlock(&mdev->lock);
926 EXPORT_SYMBOL_GPL(media_remove_intf_link);
928 void __media_remove_intf_links(struct media_interface *intf)
930 struct media_link *link, *tmp;
932 list_for_each_entry_safe(link, tmp, &intf->links, list)
933 __media_remove_intf_link(link);
936 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
938 void media_remove_intf_links(struct media_interface *intf)
940 struct media_device *mdev = intf->graph_obj.mdev;
942 /* Do nothing if the intf is not registered. */
946 spin_lock(&mdev->lock);
947 __media_remove_intf_links(intf);
948 spin_unlock(&mdev->lock);
950 EXPORT_SYMBOL_GPL(media_remove_intf_links);