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 0x%08x entity#%d: '%s'\n",
110 event_name, gobj->id, media_localid(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 0x%08x link#%d: %s#%d ==> %s#%d\n",
119 event_name, gobj->id, media_localid(gobj),
121 gobj_type(media_type(link->gobj0)),
122 media_localid(link->gobj0),
124 gobj_type(media_type(link->gobj1)),
125 media_localid(link->gobj1));
128 case MEDIA_GRAPH_PAD:
130 struct media_pad *pad = gobj_to_pad(gobj);
132 dev_dbg(gobj->mdev->dev,
133 "%s: id 0x%08x %s%spad#%d: '%s':%d\n",
134 event_name, gobj->id,
135 pad->flags & MEDIA_PAD_FL_SINK ? " sink " : "",
136 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
138 pad->entity->name, pad->index);
141 case MEDIA_GRAPH_INTF_DEVNODE:
143 struct media_interface *intf = gobj_to_intf(gobj);
144 struct media_intf_devnode *devnode = intf_to_devnode(intf);
146 dev_dbg(gobj->mdev->dev,
147 "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
148 event_name, gobj->id, media_localid(gobj),
150 devnode->major, devnode->minor);
157 void media_gobj_create(struct media_device *mdev,
158 enum media_gobj_type type,
159 struct media_gobj *gobj)
165 /* Create a per-type unique object ID */
167 case MEDIA_GRAPH_ENTITY:
168 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
169 list_add_tail(&gobj->list, &mdev->entities);
171 case MEDIA_GRAPH_PAD:
172 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
173 list_add_tail(&gobj->list, &mdev->pads);
175 case MEDIA_GRAPH_LINK:
176 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
177 list_add_tail(&gobj->list, &mdev->links);
179 case MEDIA_GRAPH_INTF_DEVNODE:
180 gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
181 list_add_tail(&gobj->list, &mdev->interfaces);
185 mdev->topology_version++;
187 dev_dbg_obj(__func__, gobj);
190 void media_gobj_destroy(struct media_gobj *gobj)
192 dev_dbg_obj(__func__, gobj);
194 gobj->mdev->topology_version++;
196 /* Remove the object from mdev list */
197 list_del(&gobj->list);
200 int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
201 struct media_pad *pads)
203 struct media_device *mdev = entity->graph_obj.mdev;
206 entity->num_pads = num_pads;
210 spin_lock(&mdev->lock);
212 for (i = 0; i < num_pads; i++) {
213 pads[i].entity = entity;
216 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
217 &entity->pads[i].graph_obj);
221 spin_unlock(&mdev->lock);
225 EXPORT_SYMBOL_GPL(media_entity_pads_init);
227 /* -----------------------------------------------------------------------------
231 static struct media_entity *
232 media_entity_other(struct media_entity *entity, struct media_link *link)
234 if (link->source->entity == entity)
235 return link->sink->entity;
237 return link->source->entity;
240 /* push an entity to traversal stack */
241 static void stack_push(struct media_entity_graph *graph,
242 struct media_entity *entity)
244 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
249 graph->stack[graph->top].link = entity->links.next;
250 graph->stack[graph->top].entity = entity;
253 static struct media_entity *stack_pop(struct media_entity_graph *graph)
255 struct media_entity *entity;
257 entity = graph->stack[graph->top].entity;
263 #define link_top(en) ((en)->stack[(en)->top].link)
264 #define stack_top(en) ((en)->stack[(en)->top].entity)
267 * TODO: Get rid of this.
269 #define MEDIA_ENTITY_MAX_PADS 63
272 * media_entity_graph_walk_init - Allocate resources for graph walk
273 * @graph: Media graph structure that will be used to walk the graph
274 * @mdev: Media device
276 * Reserve resources for graph walk in media device's current
277 * state. The memory must be released using
278 * media_entity_graph_walk_free().
280 * Returns error on failure, zero on success.
282 __must_check int media_entity_graph_walk_init(
283 struct media_entity_graph *graph, struct media_device *mdev)
285 return media_entity_enum_init(&graph->ent_enum, mdev);
287 EXPORT_SYMBOL_GPL(media_entity_graph_walk_init);
290 * media_entity_graph_walk_cleanup - Release resources related to graph walking
291 * @graph: Media graph structure that was used to walk the graph
293 void media_entity_graph_walk_cleanup(struct media_entity_graph *graph)
295 media_entity_enum_cleanup(&graph->ent_enum);
297 EXPORT_SYMBOL_GPL(media_entity_graph_walk_cleanup);
299 void media_entity_graph_walk_start(struct media_entity_graph *graph,
300 struct media_entity *entity)
302 media_entity_enum_zero(&graph->ent_enum);
303 media_entity_enum_set(&graph->ent_enum, entity);
306 graph->stack[graph->top].entity = NULL;
307 stack_push(graph, entity);
309 EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
311 struct media_entity *
312 media_entity_graph_walk_next(struct media_entity_graph *graph)
314 if (stack_top(graph) == NULL)
318 * Depth first search. Push entity to stack and continue from
319 * top of the stack until no more entities on the level can be
322 while (link_top(graph) != &stack_top(graph)->links) {
323 struct media_entity *entity = stack_top(graph);
324 struct media_link *link;
325 struct media_entity *next;
327 link = list_entry(link_top(graph), typeof(*link), list);
329 /* The link is not enabled so we do not follow. */
330 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
331 link_top(graph) = link_top(graph)->next;
335 /* Get the entity in the other end of the link . */
336 next = media_entity_other(entity, link);
338 /* Has the entity already been visited? */
339 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
340 link_top(graph) = link_top(graph)->next;
344 /* Push the new entity to stack and start over. */
345 link_top(graph) = link_top(graph)->next;
346 stack_push(graph, next);
349 return stack_pop(graph);
351 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
353 /* -----------------------------------------------------------------------------
354 * Pipeline management
357 __must_check int media_entity_pipeline_start(struct media_entity *entity,
358 struct media_pipeline *pipe)
360 struct media_device *mdev = entity->graph_obj.mdev;
361 struct media_entity_graph *graph = &pipe->graph;
362 struct media_entity *entity_err = entity;
363 struct media_link *link;
366 mutex_lock(&mdev->graph_mutex);
368 if (!pipe->streaming_count++) {
369 ret = media_entity_graph_walk_init(&pipe->graph, mdev);
371 goto error_graph_walk_start;
374 media_entity_graph_walk_start(&pipe->graph, entity);
376 while ((entity = media_entity_graph_walk_next(graph))) {
377 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
378 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
380 entity->stream_count++;
382 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
389 /* Already streaming --- no need to check. */
390 if (entity->stream_count > 1)
393 if (!entity->ops || !entity->ops->link_validate)
396 bitmap_zero(active, entity->num_pads);
397 bitmap_fill(has_no_links, entity->num_pads);
399 list_for_each_entry(link, &entity->links, list) {
400 struct media_pad *pad = link->sink->entity == entity
401 ? link->sink : link->source;
403 /* Mark that a pad is connected by a link. */
404 bitmap_clear(has_no_links, pad->index, 1);
407 * Pads that either do not need to connect or
408 * are connected through an enabled link are
411 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
412 link->flags & MEDIA_LNK_FL_ENABLED)
413 bitmap_set(active, pad->index, 1);
416 * Link validation will only take place for
417 * sink ends of the link that are enabled.
419 if (link->sink != pad ||
420 !(link->flags & MEDIA_LNK_FL_ENABLED))
423 ret = entity->ops->link_validate(link);
424 if (ret < 0 && ret != -ENOIOCTLCMD) {
425 dev_dbg(entity->graph_obj.mdev->dev,
426 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
427 link->source->entity->name,
429 entity->name, link->sink->index, ret);
434 /* Either no links or validated links are fine. */
435 bitmap_or(active, active, has_no_links, entity->num_pads);
437 if (!bitmap_full(active, entity->num_pads)) {
439 dev_dbg(entity->graph_obj.mdev->dev,
440 "\"%s\":%u must be connected by an enabled link\n",
442 (unsigned)find_first_zero_bit(
443 active, entity->num_pads));
448 mutex_unlock(&mdev->graph_mutex);
454 * Link validation on graph failed. We revert what we did and
457 media_entity_graph_walk_start(graph, entity_err);
459 while ((entity_err = media_entity_graph_walk_next(graph))) {
460 entity_err->stream_count--;
461 if (entity_err->stream_count == 0)
462 entity_err->pipe = NULL;
465 * We haven't increased stream_count further than this
468 if (entity_err == entity)
472 error_graph_walk_start:
473 if (!--pipe->streaming_count)
474 media_entity_graph_walk_cleanup(graph);
476 mutex_unlock(&mdev->graph_mutex);
480 EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
482 void media_entity_pipeline_stop(struct media_entity *entity)
484 struct media_device *mdev = entity->graph_obj.mdev;
485 struct media_entity_graph *graph = &entity->pipe->graph;
486 struct media_pipeline *pipe = entity->pipe;
488 mutex_lock(&mdev->graph_mutex);
490 WARN_ON(!pipe->streaming_count);
491 media_entity_graph_walk_start(graph, entity);
493 while ((entity = media_entity_graph_walk_next(graph))) {
494 entity->stream_count--;
495 if (entity->stream_count == 0)
499 if (!--pipe->streaming_count)
500 media_entity_graph_walk_cleanup(graph);
502 mutex_unlock(&mdev->graph_mutex);
504 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
506 /* -----------------------------------------------------------------------------
510 struct media_entity *media_entity_get(struct media_entity *entity)
515 if (entity->graph_obj.mdev->dev &&
516 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
521 EXPORT_SYMBOL_GPL(media_entity_get);
523 void media_entity_put(struct media_entity *entity)
528 if (entity->graph_obj.mdev->dev)
529 module_put(entity->graph_obj.mdev->dev->driver->owner);
531 EXPORT_SYMBOL_GPL(media_entity_put);
533 /* -----------------------------------------------------------------------------
537 static struct media_link *media_add_link(struct list_head *head)
539 struct media_link *link;
541 link = kzalloc(sizeof(*link), GFP_KERNEL);
545 list_add_tail(&link->list, head);
550 static void __media_entity_remove_link(struct media_entity *entity,
551 struct media_link *link)
553 struct media_link *rlink, *tmp;
554 struct media_entity *remote;
556 if (link->source->entity == entity)
557 remote = link->sink->entity;
559 remote = link->source->entity;
561 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
562 if (rlink != link->reverse)
565 if (link->source->entity == entity)
566 remote->num_backlinks--;
568 /* Remove the remote link */
569 list_del(&rlink->list);
570 media_gobj_destroy(&rlink->graph_obj);
573 if (--remote->num_links == 0)
576 list_del(&link->list);
577 media_gobj_destroy(&link->graph_obj);
582 media_create_pad_link(struct media_entity *source, u16 source_pad,
583 struct media_entity *sink, u16 sink_pad, u32 flags)
585 struct media_link *link;
586 struct media_link *backlink;
588 BUG_ON(source == NULL || sink == NULL);
589 BUG_ON(source_pad >= source->num_pads);
590 BUG_ON(sink_pad >= sink->num_pads);
592 link = media_add_link(&source->links);
596 link->source = &source->pads[source_pad];
597 link->sink = &sink->pads[sink_pad];
598 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
600 /* Initialize graph object embedded at the new link */
601 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
604 /* Create the backlink. Backlinks are used to help graph traversal and
605 * are not reported to userspace.
607 backlink = media_add_link(&sink->links);
608 if (backlink == NULL) {
609 __media_entity_remove_link(source, link);
613 backlink->source = &source->pads[source_pad];
614 backlink->sink = &sink->pads[sink_pad];
615 backlink->flags = flags;
616 backlink->is_backlink = true;
618 /* Initialize graph object embedded at the new link */
619 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
620 &backlink->graph_obj);
622 link->reverse = backlink;
623 backlink->reverse = link;
625 sink->num_backlinks++;
631 EXPORT_SYMBOL_GPL(media_create_pad_link);
633 void __media_entity_remove_links(struct media_entity *entity)
635 struct media_link *link, *tmp;
637 list_for_each_entry_safe(link, tmp, &entity->links, list)
638 __media_entity_remove_link(entity, link);
640 entity->num_links = 0;
641 entity->num_backlinks = 0;
643 EXPORT_SYMBOL_GPL(__media_entity_remove_links);
645 void media_entity_remove_links(struct media_entity *entity)
647 struct media_device *mdev = entity->graph_obj.mdev;
649 /* Do nothing if the entity is not registered. */
653 spin_lock(&mdev->lock);
654 __media_entity_remove_links(entity);
655 spin_unlock(&mdev->lock);
657 EXPORT_SYMBOL_GPL(media_entity_remove_links);
659 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
663 /* Notify both entities. */
664 ret = media_entity_call(link->source->entity, link_setup,
665 link->source, link->sink, flags);
666 if (ret < 0 && ret != -ENOIOCTLCMD)
669 ret = media_entity_call(link->sink->entity, link_setup,
670 link->sink, link->source, flags);
671 if (ret < 0 && ret != -ENOIOCTLCMD) {
672 media_entity_call(link->source->entity, link_setup,
673 link->source, link->sink, link->flags);
678 link->reverse->flags = link->flags;
683 int __media_entity_setup_link(struct media_link *link, u32 flags)
685 const u32 mask = MEDIA_LNK_FL_ENABLED;
686 struct media_device *mdev;
687 struct media_entity *source, *sink;
693 /* The non-modifiable link flags must not be modified. */
694 if ((link->flags & ~mask) != (flags & ~mask))
697 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
698 return link->flags == flags ? 0 : -EINVAL;
700 if (link->flags == flags)
703 source = link->source->entity;
704 sink = link->sink->entity;
706 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
707 (source->stream_count || sink->stream_count))
710 mdev = source->graph_obj.mdev;
712 if (mdev->link_notify) {
713 ret = mdev->link_notify(link, flags,
714 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
719 ret = __media_entity_setup_link_notify(link, flags);
721 if (mdev->link_notify)
722 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
727 int media_entity_setup_link(struct media_link *link, u32 flags)
731 mutex_lock(&link->graph_obj.mdev->graph_mutex);
732 ret = __media_entity_setup_link(link, flags);
733 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
737 EXPORT_SYMBOL_GPL(media_entity_setup_link);
740 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
742 struct media_link *link;
744 list_for_each_entry(link, &source->entity->links, list) {
745 if (link->source->entity == source->entity &&
746 link->source->index == source->index &&
747 link->sink->entity == sink->entity &&
748 link->sink->index == sink->index)
754 EXPORT_SYMBOL_GPL(media_entity_find_link);
756 struct media_pad *media_entity_remote_pad(struct media_pad *pad)
758 struct media_link *link;
760 list_for_each_entry(link, &pad->entity->links, list) {
761 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
764 if (link->source == pad)
767 if (link->sink == pad)
774 EXPORT_SYMBOL_GPL(media_entity_remote_pad);
776 static void media_interface_init(struct media_device *mdev,
777 struct media_interface *intf,
779 u32 intf_type, u32 flags)
781 intf->type = intf_type;
783 INIT_LIST_HEAD(&intf->links);
785 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
788 /* Functions related to the media interface via device nodes */
790 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
792 u32 major, u32 minor)
794 struct media_intf_devnode *devnode;
796 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
800 devnode->major = major;
801 devnode->minor = minor;
803 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
808 EXPORT_SYMBOL_GPL(media_devnode_create);
810 void media_devnode_remove(struct media_intf_devnode *devnode)
812 media_remove_intf_links(&devnode->intf);
813 media_gobj_destroy(&devnode->intf.graph_obj);
816 EXPORT_SYMBOL_GPL(media_devnode_remove);
818 struct media_link *media_create_intf_link(struct media_entity *entity,
819 struct media_interface *intf,
822 struct media_link *link;
824 link = media_add_link(&intf->links);
829 link->entity = entity;
830 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
832 /* Initialize graph object embedded at the new link */
833 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
838 EXPORT_SYMBOL_GPL(media_create_intf_link);
840 void __media_remove_intf_link(struct media_link *link)
842 list_del(&link->list);
843 media_gobj_destroy(&link->graph_obj);
846 EXPORT_SYMBOL_GPL(__media_remove_intf_link);
848 void media_remove_intf_link(struct media_link *link)
850 struct media_device *mdev = link->graph_obj.mdev;
852 /* Do nothing if the intf is not registered. */
856 spin_lock(&mdev->lock);
857 __media_remove_intf_link(link);
858 spin_unlock(&mdev->lock);
860 EXPORT_SYMBOL_GPL(media_remove_intf_link);
862 void __media_remove_intf_links(struct media_interface *intf)
864 struct media_link *link, *tmp;
866 list_for_each_entry_safe(link, tmp, &intf->links, list)
867 __media_remove_intf_link(link);
870 EXPORT_SYMBOL_GPL(__media_remove_intf_links);
872 void media_remove_intf_links(struct media_interface *intf)
874 struct media_device *mdev = intf->graph_obj.mdev;
876 /* Do nothing if the intf is not registered. */
880 spin_lock(&mdev->lock);
881 __media_remove_intf_links(intf);
882 spin_unlock(&mdev->lock);
884 EXPORT_SYMBOL_GPL(media_remove_intf_links);