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";
74 * dev_dbg_obj - Prints in debug mode a change on some object
76 * @event_name: Name of the event to report. Could be __func__
77 * @gobj: Pointer to the object
79 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
80 * won't produce any code.
82 static void dev_dbg_obj(const char *event_name
, struct media_gobj
*gobj
)
84 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
85 switch (media_type(gobj
)) {
86 case MEDIA_GRAPH_ENTITY
:
87 dev_dbg(gobj
->mdev
->dev
,
88 "%s: id 0x%08x entity#%d: '%s'\n",
89 event_name
, gobj
->id
, media_localid(gobj
),
90 gobj_to_entity(gobj
)->name
);
92 case MEDIA_GRAPH_LINK
:
94 struct media_link
*link
= gobj_to_link(gobj
);
96 dev_dbg(gobj
->mdev
->dev
,
97 "%s: id 0x%08x link#%d: %s#%d ==> %s#%d\n",
98 event_name
, gobj
->id
, media_localid(gobj
),
100 gobj_type(media_type(link
->gobj0
)),
101 media_localid(link
->gobj0
),
103 gobj_type(media_type(link
->gobj1
)),
104 media_localid(link
->gobj1
));
107 case MEDIA_GRAPH_PAD
:
109 struct media_pad
*pad
= gobj_to_pad(gobj
);
111 dev_dbg(gobj
->mdev
->dev
,
112 "%s: id 0x%08x %s%spad#%d: '%s':%d\n",
113 event_name
, gobj
->id
,
114 pad
->flags
& MEDIA_PAD_FL_SINK
? " sink " : "",
115 pad
->flags
& MEDIA_PAD_FL_SOURCE
? "source " : "",
117 pad
->entity
->name
, pad
->index
);
120 case MEDIA_GRAPH_INTF_DEVNODE
:
122 struct media_interface
*intf
= gobj_to_intf(gobj
);
123 struct media_intf_devnode
*devnode
= intf_to_devnode(intf
);
125 dev_dbg(gobj
->mdev
->dev
,
126 "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
127 event_name
, gobj
->id
, media_localid(gobj
),
129 devnode
->major
, devnode
->minor
);
136 void media_gobj_create(struct media_device
*mdev
,
137 enum media_gobj_type type
,
138 struct media_gobj
*gobj
)
144 /* Create a per-type unique object ID */
146 case MEDIA_GRAPH_ENTITY
:
147 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->entity_id
);
148 list_add_tail(&gobj
->list
, &mdev
->entities
);
150 case MEDIA_GRAPH_PAD
:
151 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->pad_id
);
152 list_add_tail(&gobj
->list
, &mdev
->pads
);
154 case MEDIA_GRAPH_LINK
:
155 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->link_id
);
156 list_add_tail(&gobj
->list
, &mdev
->links
);
158 case MEDIA_GRAPH_INTF_DEVNODE
:
159 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->intf_devnode_id
);
160 list_add_tail(&gobj
->list
, &mdev
->interfaces
);
164 mdev
->topology_version
++;
166 dev_dbg_obj(__func__
, gobj
);
169 void media_gobj_destroy(struct media_gobj
*gobj
)
171 dev_dbg_obj(__func__
, gobj
);
173 gobj
->mdev
->topology_version
++;
175 /* Remove the object from mdev list */
176 list_del(&gobj
->list
);
179 int media_entity_pads_init(struct media_entity
*entity
, u16 num_pads
,
180 struct media_pad
*pads
)
182 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
185 entity
->num_pads
= num_pads
;
189 spin_lock(&mdev
->lock
);
191 for (i
= 0; i
< num_pads
; i
++) {
192 pads
[i
].entity
= entity
;
195 media_gobj_create(mdev
, MEDIA_GRAPH_PAD
,
196 &entity
->pads
[i
].graph_obj
);
200 spin_unlock(&mdev
->lock
);
204 EXPORT_SYMBOL_GPL(media_entity_pads_init
);
206 /* -----------------------------------------------------------------------------
210 static struct media_entity
*
211 media_entity_other(struct media_entity
*entity
, struct media_link
*link
)
213 if (link
->source
->entity
== entity
)
214 return link
->sink
->entity
;
216 return link
->source
->entity
;
219 /* push an entity to traversal stack */
220 static void stack_push(struct media_entity_graph
*graph
,
221 struct media_entity
*entity
)
223 if (graph
->top
== MEDIA_ENTITY_ENUM_MAX_DEPTH
- 1) {
228 graph
->stack
[graph
->top
].link
= entity
->links
.next
;
229 graph
->stack
[graph
->top
].entity
= entity
;
232 static struct media_entity
*stack_pop(struct media_entity_graph
*graph
)
234 struct media_entity
*entity
;
236 entity
= graph
->stack
[graph
->top
].entity
;
242 #define link_top(en) ((en)->stack[(en)->top].link)
243 #define stack_top(en) ((en)->stack[(en)->top].entity)
245 void media_entity_graph_walk_start(struct media_entity_graph
*graph
,
246 struct media_entity
*entity
)
249 graph
->stack
[graph
->top
].entity
= NULL
;
250 bitmap_zero(graph
->entities
, MEDIA_ENTITY_ENUM_MAX_ID
);
252 if (WARN_ON(media_entity_id(entity
) >= MEDIA_ENTITY_ENUM_MAX_ID
))
255 __set_bit(media_entity_id(entity
), graph
->entities
);
256 stack_push(graph
, entity
);
258 EXPORT_SYMBOL_GPL(media_entity_graph_walk_start
);
260 struct media_entity
*
261 media_entity_graph_walk_next(struct media_entity_graph
*graph
)
263 if (stack_top(graph
) == NULL
)
267 * Depth first search. Push entity to stack and continue from
268 * top of the stack until no more entities on the level can be
271 while (link_top(graph
) != &stack_top(graph
)->links
) {
272 struct media_entity
*entity
= stack_top(graph
);
273 struct media_link
*link
;
274 struct media_entity
*next
;
276 link
= list_entry(link_top(graph
), typeof(*link
), list
);
278 /* The link is not enabled so we do not follow. */
279 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
280 link_top(graph
) = link_top(graph
)->next
;
284 /* Get the entity in the other end of the link . */
285 next
= media_entity_other(entity
, link
);
286 if (WARN_ON(media_entity_id(next
) >= MEDIA_ENTITY_ENUM_MAX_ID
))
289 /* Has the entity already been visited? */
290 if (__test_and_set_bit(media_entity_id(next
), graph
->entities
)) {
291 link_top(graph
) = link_top(graph
)->next
;
295 /* Push the new entity to stack and start over. */
296 link_top(graph
) = link_top(graph
)->next
;
297 stack_push(graph
, next
);
300 return stack_pop(graph
);
302 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next
);
304 /* -----------------------------------------------------------------------------
305 * Pipeline management
308 __must_check
int media_entity_pipeline_start(struct media_entity
*entity
,
309 struct media_pipeline
*pipe
)
311 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
312 struct media_entity_graph graph
;
313 struct media_entity
*entity_err
= entity
;
314 struct media_link
*link
;
317 mutex_lock(&mdev
->graph_mutex
);
319 media_entity_graph_walk_start(&graph
, entity
);
321 while ((entity
= media_entity_graph_walk_next(&graph
))) {
322 DECLARE_BITMAP(active
, MEDIA_ENTITY_MAX_PADS
);
323 DECLARE_BITMAP(has_no_links
, MEDIA_ENTITY_MAX_PADS
);
325 entity
->stream_count
++;
327 if (WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
)) {
334 /* Already streaming --- no need to check. */
335 if (entity
->stream_count
> 1)
338 if (!entity
->ops
|| !entity
->ops
->link_validate
)
341 bitmap_zero(active
, entity
->num_pads
);
342 bitmap_fill(has_no_links
, entity
->num_pads
);
344 list_for_each_entry(link
, &entity
->links
, list
) {
345 struct media_pad
*pad
= link
->sink
->entity
== entity
346 ? link
->sink
: link
->source
;
348 /* Mark that a pad is connected by a link. */
349 bitmap_clear(has_no_links
, pad
->index
, 1);
352 * Pads that either do not need to connect or
353 * are connected through an enabled link are
356 if (!(pad
->flags
& MEDIA_PAD_FL_MUST_CONNECT
) ||
357 link
->flags
& MEDIA_LNK_FL_ENABLED
)
358 bitmap_set(active
, pad
->index
, 1);
361 * Link validation will only take place for
362 * sink ends of the link that are enabled.
364 if (link
->sink
!= pad
||
365 !(link
->flags
& MEDIA_LNK_FL_ENABLED
))
368 ret
= entity
->ops
->link_validate(link
);
369 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
370 dev_dbg(entity
->graph_obj
.mdev
->dev
,
371 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
372 link
->source
->entity
->name
,
374 entity
->name
, link
->sink
->index
, ret
);
379 /* Either no links or validated links are fine. */
380 bitmap_or(active
, active
, has_no_links
, entity
->num_pads
);
382 if (!bitmap_full(active
, entity
->num_pads
)) {
384 dev_dbg(entity
->graph_obj
.mdev
->dev
,
385 "\"%s\":%u must be connected by an enabled link\n",
387 (unsigned)find_first_zero_bit(
388 active
, entity
->num_pads
));
393 mutex_unlock(&mdev
->graph_mutex
);
399 * Link validation on graph failed. We revert what we did and
402 media_entity_graph_walk_start(&graph
, entity_err
);
404 while ((entity_err
= media_entity_graph_walk_next(&graph
))) {
405 entity_err
->stream_count
--;
406 if (entity_err
->stream_count
== 0)
407 entity_err
->pipe
= NULL
;
410 * We haven't increased stream_count further than this
413 if (entity_err
== entity
)
417 mutex_unlock(&mdev
->graph_mutex
);
421 EXPORT_SYMBOL_GPL(media_entity_pipeline_start
);
423 void media_entity_pipeline_stop(struct media_entity
*entity
)
425 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
426 struct media_entity_graph graph
;
428 mutex_lock(&mdev
->graph_mutex
);
430 media_entity_graph_walk_start(&graph
, entity
);
432 while ((entity
= media_entity_graph_walk_next(&graph
))) {
433 entity
->stream_count
--;
434 if (entity
->stream_count
== 0)
438 mutex_unlock(&mdev
->graph_mutex
);
440 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop
);
442 /* -----------------------------------------------------------------------------
446 struct media_entity
*media_entity_get(struct media_entity
*entity
)
451 if (entity
->graph_obj
.mdev
->dev
&&
452 !try_module_get(entity
->graph_obj
.mdev
->dev
->driver
->owner
))
457 EXPORT_SYMBOL_GPL(media_entity_get
);
459 void media_entity_put(struct media_entity
*entity
)
464 if (entity
->graph_obj
.mdev
->dev
)
465 module_put(entity
->graph_obj
.mdev
->dev
->driver
->owner
);
467 EXPORT_SYMBOL_GPL(media_entity_put
);
469 /* -----------------------------------------------------------------------------
473 static struct media_link
*media_add_link(struct list_head
*head
)
475 struct media_link
*link
;
477 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
481 list_add_tail(&link
->list
, head
);
486 static void __media_entity_remove_link(struct media_entity
*entity
,
487 struct media_link
*link
)
489 struct media_link
*rlink
, *tmp
;
490 struct media_entity
*remote
;
492 if (link
->source
->entity
== entity
)
493 remote
= link
->sink
->entity
;
495 remote
= link
->source
->entity
;
497 list_for_each_entry_safe(rlink
, tmp
, &remote
->links
, list
) {
498 if (rlink
!= link
->reverse
)
501 if (link
->source
->entity
== entity
)
502 remote
->num_backlinks
--;
504 /* Remove the remote link */
505 list_del(&rlink
->list
);
506 media_gobj_destroy(&rlink
->graph_obj
);
509 if (--remote
->num_links
== 0)
512 list_del(&link
->list
);
513 media_gobj_destroy(&link
->graph_obj
);
518 media_create_pad_link(struct media_entity
*source
, u16 source_pad
,
519 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
521 struct media_link
*link
;
522 struct media_link
*backlink
;
524 BUG_ON(source
== NULL
|| sink
== NULL
);
525 BUG_ON(source_pad
>= source
->num_pads
);
526 BUG_ON(sink_pad
>= sink
->num_pads
);
528 link
= media_add_link(&source
->links
);
532 link
->source
= &source
->pads
[source_pad
];
533 link
->sink
= &sink
->pads
[sink_pad
];
534 link
->flags
= flags
& ~MEDIA_LNK_FL_INTERFACE_LINK
;
536 /* Initialize graph object embedded at the new link */
537 media_gobj_create(source
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
540 /* Create the backlink. Backlinks are used to help graph traversal and
541 * are not reported to userspace.
543 backlink
= media_add_link(&sink
->links
);
544 if (backlink
== NULL
) {
545 __media_entity_remove_link(source
, link
);
549 backlink
->source
= &source
->pads
[source_pad
];
550 backlink
->sink
= &sink
->pads
[sink_pad
];
551 backlink
->flags
= flags
;
552 backlink
->is_backlink
= true;
554 /* Initialize graph object embedded at the new link */
555 media_gobj_create(sink
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
556 &backlink
->graph_obj
);
558 link
->reverse
= backlink
;
559 backlink
->reverse
= link
;
561 sink
->num_backlinks
++;
567 EXPORT_SYMBOL_GPL(media_create_pad_link
);
569 void __media_entity_remove_links(struct media_entity
*entity
)
571 struct media_link
*link
, *tmp
;
573 list_for_each_entry_safe(link
, tmp
, &entity
->links
, list
)
574 __media_entity_remove_link(entity
, link
);
576 entity
->num_links
= 0;
577 entity
->num_backlinks
= 0;
579 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
581 void media_entity_remove_links(struct media_entity
*entity
)
583 /* Do nothing if the entity is not registered. */
584 if (entity
->graph_obj
.mdev
== NULL
)
587 spin_lock(&entity
->graph_obj
.mdev
->lock
);
588 __media_entity_remove_links(entity
);
589 spin_unlock(&entity
->graph_obj
.mdev
->lock
);
591 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
593 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
597 /* Notify both entities. */
598 ret
= media_entity_call(link
->source
->entity
, link_setup
,
599 link
->source
, link
->sink
, flags
);
600 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
603 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
604 link
->sink
, link
->source
, flags
);
605 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
606 media_entity_call(link
->source
->entity
, link_setup
,
607 link
->source
, link
->sink
, link
->flags
);
612 link
->reverse
->flags
= link
->flags
;
617 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
619 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
620 struct media_device
*mdev
;
621 struct media_entity
*source
, *sink
;
627 /* The non-modifiable link flags must not be modified. */
628 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
631 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
632 return link
->flags
== flags
? 0 : -EINVAL
;
634 if (link
->flags
== flags
)
637 source
= link
->source
->entity
;
638 sink
= link
->sink
->entity
;
640 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
641 (source
->stream_count
|| sink
->stream_count
))
644 mdev
= source
->graph_obj
.mdev
;
646 if (mdev
->link_notify
) {
647 ret
= mdev
->link_notify(link
, flags
,
648 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
653 ret
= __media_entity_setup_link_notify(link
, flags
);
655 if (mdev
->link_notify
)
656 mdev
->link_notify(link
, flags
, MEDIA_DEV_NOTIFY_POST_LINK_CH
);
661 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
665 mutex_lock(&link
->graph_obj
.mdev
->graph_mutex
);
666 ret
= __media_entity_setup_link(link
, flags
);
667 mutex_unlock(&link
->graph_obj
.mdev
->graph_mutex
);
671 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
674 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
676 struct media_link
*link
;
678 list_for_each_entry(link
, &source
->entity
->links
, list
) {
679 if (link
->source
->entity
== source
->entity
&&
680 link
->source
->index
== source
->index
&&
681 link
->sink
->entity
== sink
->entity
&&
682 link
->sink
->index
== sink
->index
)
688 EXPORT_SYMBOL_GPL(media_entity_find_link
);
690 struct media_pad
*media_entity_remote_pad(struct media_pad
*pad
)
692 struct media_link
*link
;
694 list_for_each_entry(link
, &pad
->entity
->links
, list
) {
695 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
698 if (link
->source
== pad
)
701 if (link
->sink
== pad
)
708 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);
710 static void media_interface_init(struct media_device
*mdev
,
711 struct media_interface
*intf
,
713 u32 intf_type
, u32 flags
)
715 intf
->type
= intf_type
;
717 INIT_LIST_HEAD(&intf
->links
);
719 media_gobj_create(mdev
, gobj_type
, &intf
->graph_obj
);
722 /* Functions related to the media interface via device nodes */
724 struct media_intf_devnode
*media_devnode_create(struct media_device
*mdev
,
726 u32 major
, u32 minor
)
728 struct media_intf_devnode
*devnode
;
730 devnode
= kzalloc(sizeof(*devnode
), GFP_KERNEL
);
734 devnode
->major
= major
;
735 devnode
->minor
= minor
;
737 media_interface_init(mdev
, &devnode
->intf
, MEDIA_GRAPH_INTF_DEVNODE
,
742 EXPORT_SYMBOL_GPL(media_devnode_create
);
744 void media_devnode_remove(struct media_intf_devnode
*devnode
)
746 media_remove_intf_links(&devnode
->intf
);
747 media_gobj_destroy(&devnode
->intf
.graph_obj
);
750 EXPORT_SYMBOL_GPL(media_devnode_remove
);
752 struct media_link
*media_create_intf_link(struct media_entity
*entity
,
753 struct media_interface
*intf
,
756 struct media_link
*link
;
758 link
= media_add_link(&intf
->links
);
763 link
->entity
= entity
;
764 link
->flags
= flags
| MEDIA_LNK_FL_INTERFACE_LINK
;
766 /* Initialize graph object embedded at the new link */
767 media_gobj_create(intf
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
772 EXPORT_SYMBOL_GPL(media_create_intf_link
);
774 void __media_remove_intf_link(struct media_link
*link
)
776 list_del(&link
->list
);
777 media_gobj_destroy(&link
->graph_obj
);
780 EXPORT_SYMBOL_GPL(__media_remove_intf_link
);
782 void media_remove_intf_link(struct media_link
*link
)
784 spin_lock(&link
->graph_obj
.mdev
->lock
);
785 __media_remove_intf_link(link
);
786 spin_unlock(&link
->graph_obj
.mdev
->lock
);
788 EXPORT_SYMBOL_GPL(media_remove_intf_link
);
790 void __media_remove_intf_links(struct media_interface
*intf
)
792 struct media_link
*link
, *tmp
;
794 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
)
795 __media_remove_intf_link(link
);
798 EXPORT_SYMBOL_GPL(__media_remove_intf_links
);
800 void media_remove_intf_links(struct media_interface
*intf
)
802 /* Do nothing if the intf is not registered. */
803 if (intf
->graph_obj
.mdev
== NULL
)
806 spin_lock(&intf
->graph_obj
.mdev
->lock
);
807 __media_remove_intf_links(intf
);
808 spin_unlock(&intf
->graph_obj
.mdev
->lock
);
810 EXPORT_SYMBOL_GPL(media_remove_intf_links
);