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
++;
326 WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
);
329 /* Already streaming --- no need to check. */
330 if (entity
->stream_count
> 1)
333 if (!entity
->ops
|| !entity
->ops
->link_validate
)
336 bitmap_zero(active
, entity
->num_pads
);
337 bitmap_fill(has_no_links
, entity
->num_pads
);
339 list_for_each_entry(link
, &entity
->links
, list
) {
340 struct media_pad
*pad
= link
->sink
->entity
== entity
341 ? link
->sink
: link
->source
;
343 /* Mark that a pad is connected by a link. */
344 bitmap_clear(has_no_links
, pad
->index
, 1);
347 * Pads that either do not need to connect or
348 * are connected through an enabled link are
351 if (!(pad
->flags
& MEDIA_PAD_FL_MUST_CONNECT
) ||
352 link
->flags
& MEDIA_LNK_FL_ENABLED
)
353 bitmap_set(active
, pad
->index
, 1);
356 * Link validation will only take place for
357 * sink ends of the link that are enabled.
359 if (link
->sink
!= pad
||
360 !(link
->flags
& MEDIA_LNK_FL_ENABLED
))
363 ret
= entity
->ops
->link_validate(link
);
364 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
365 dev_dbg(entity
->graph_obj
.mdev
->dev
,
366 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
367 link
->source
->entity
->name
,
369 entity
->name
, link
->sink
->index
, ret
);
374 /* Either no links or validated links are fine. */
375 bitmap_or(active
, active
, has_no_links
, entity
->num_pads
);
377 if (!bitmap_full(active
, entity
->num_pads
)) {
379 dev_dbg(entity
->graph_obj
.mdev
->dev
,
380 "\"%s\":%u must be connected by an enabled link\n",
382 (unsigned)find_first_zero_bit(
383 active
, entity
->num_pads
));
388 mutex_unlock(&mdev
->graph_mutex
);
394 * Link validation on graph failed. We revert what we did and
397 media_entity_graph_walk_start(&graph
, entity_err
);
399 while ((entity_err
= media_entity_graph_walk_next(&graph
))) {
400 entity_err
->stream_count
--;
401 if (entity_err
->stream_count
== 0)
402 entity_err
->pipe
= NULL
;
405 * We haven't increased stream_count further than this
408 if (entity_err
== entity
)
412 mutex_unlock(&mdev
->graph_mutex
);
416 EXPORT_SYMBOL_GPL(media_entity_pipeline_start
);
418 void media_entity_pipeline_stop(struct media_entity
*entity
)
420 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
421 struct media_entity_graph graph
;
423 mutex_lock(&mdev
->graph_mutex
);
425 media_entity_graph_walk_start(&graph
, entity
);
427 while ((entity
= media_entity_graph_walk_next(&graph
))) {
428 entity
->stream_count
--;
429 if (entity
->stream_count
== 0)
433 mutex_unlock(&mdev
->graph_mutex
);
435 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop
);
437 /* -----------------------------------------------------------------------------
441 struct media_entity
*media_entity_get(struct media_entity
*entity
)
446 if (entity
->graph_obj
.mdev
->dev
&&
447 !try_module_get(entity
->graph_obj
.mdev
->dev
->driver
->owner
))
452 EXPORT_SYMBOL_GPL(media_entity_get
);
454 void media_entity_put(struct media_entity
*entity
)
459 if (entity
->graph_obj
.mdev
->dev
)
460 module_put(entity
->graph_obj
.mdev
->dev
->driver
->owner
);
462 EXPORT_SYMBOL_GPL(media_entity_put
);
464 /* -----------------------------------------------------------------------------
468 static struct media_link
*media_add_link(struct list_head
*head
)
470 struct media_link
*link
;
472 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
476 list_add_tail(&link
->list
, head
);
481 static void __media_entity_remove_link(struct media_entity
*entity
,
482 struct media_link
*link
)
484 struct media_link
*rlink
, *tmp
;
485 struct media_entity
*remote
;
487 if (link
->source
->entity
== entity
)
488 remote
= link
->sink
->entity
;
490 remote
= link
->source
->entity
;
492 list_for_each_entry_safe(rlink
, tmp
, &remote
->links
, list
) {
493 if (rlink
!= link
->reverse
)
496 if (link
->source
->entity
== entity
)
497 remote
->num_backlinks
--;
499 /* Remove the remote link */
500 list_del(&rlink
->list
);
501 media_gobj_destroy(&rlink
->graph_obj
);
504 if (--remote
->num_links
== 0)
507 list_del(&link
->list
);
508 media_gobj_destroy(&link
->graph_obj
);
513 media_create_pad_link(struct media_entity
*source
, u16 source_pad
,
514 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
516 struct media_link
*link
;
517 struct media_link
*backlink
;
519 BUG_ON(source
== NULL
|| sink
== NULL
);
520 BUG_ON(source_pad
>= source
->num_pads
);
521 BUG_ON(sink_pad
>= sink
->num_pads
);
523 link
= media_add_link(&source
->links
);
527 link
->source
= &source
->pads
[source_pad
];
528 link
->sink
= &sink
->pads
[sink_pad
];
531 /* Initialize graph object embedded at the new link */
532 media_gobj_create(source
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
535 /* Create the backlink. Backlinks are used to help graph traversal and
536 * are not reported to userspace.
538 backlink
= media_add_link(&sink
->links
);
539 if (backlink
== NULL
) {
540 __media_entity_remove_link(source
, link
);
544 backlink
->source
= &source
->pads
[source_pad
];
545 backlink
->sink
= &sink
->pads
[sink_pad
];
546 backlink
->flags
= flags
;
547 backlink
->is_backlink
= true;
549 /* Initialize graph object embedded at the new link */
550 media_gobj_create(sink
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
551 &backlink
->graph_obj
);
553 link
->reverse
= backlink
;
554 backlink
->reverse
= link
;
556 sink
->num_backlinks
++;
562 EXPORT_SYMBOL_GPL(media_create_pad_link
);
564 void __media_entity_remove_links(struct media_entity
*entity
)
566 struct media_link
*link
, *tmp
;
568 list_for_each_entry_safe(link
, tmp
, &entity
->links
, list
)
569 __media_entity_remove_link(entity
, link
);
571 entity
->num_links
= 0;
572 entity
->num_backlinks
= 0;
574 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
576 void media_entity_remove_links(struct media_entity
*entity
)
578 /* Do nothing if the entity is not registered. */
579 if (entity
->graph_obj
.mdev
== NULL
)
582 spin_lock(&entity
->graph_obj
.mdev
->lock
);
583 __media_entity_remove_links(entity
);
584 spin_unlock(&entity
->graph_obj
.mdev
->lock
);
586 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
588 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
592 /* Notify both entities. */
593 ret
= media_entity_call(link
->source
->entity
, link_setup
,
594 link
->source
, link
->sink
, flags
);
595 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
598 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
599 link
->sink
, link
->source
, flags
);
600 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
601 media_entity_call(link
->source
->entity
, link_setup
,
602 link
->source
, link
->sink
, link
->flags
);
607 link
->reverse
->flags
= link
->flags
;
612 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
614 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
615 struct media_device
*mdev
;
616 struct media_entity
*source
, *sink
;
622 /* The non-modifiable link flags must not be modified. */
623 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
626 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
627 return link
->flags
== flags
? 0 : -EINVAL
;
629 if (link
->flags
== flags
)
632 source
= link
->source
->entity
;
633 sink
= link
->sink
->entity
;
635 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
636 (source
->stream_count
|| sink
->stream_count
))
639 mdev
= source
->graph_obj
.mdev
;
641 if (mdev
->link_notify
) {
642 ret
= mdev
->link_notify(link
, flags
,
643 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
648 ret
= __media_entity_setup_link_notify(link
, flags
);
650 if (mdev
->link_notify
)
651 mdev
->link_notify(link
, flags
, MEDIA_DEV_NOTIFY_POST_LINK_CH
);
656 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
660 spin_lock(&link
->source
->entity
->graph_obj
.mdev
->lock
);
661 ret
= __media_entity_setup_link(link
, flags
);
662 spin_unlock(&link
->source
->entity
->graph_obj
.mdev
->lock
);
666 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
669 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
671 struct media_link
*link
;
673 list_for_each_entry(link
, &source
->entity
->links
, list
) {
674 if (link
->source
->entity
== source
->entity
&&
675 link
->source
->index
== source
->index
&&
676 link
->sink
->entity
== sink
->entity
&&
677 link
->sink
->index
== sink
->index
)
683 EXPORT_SYMBOL_GPL(media_entity_find_link
);
685 struct media_pad
*media_entity_remote_pad(struct media_pad
*pad
)
687 struct media_link
*link
;
689 list_for_each_entry(link
, &pad
->entity
->links
, list
) {
690 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
693 if (link
->source
== pad
)
696 if (link
->sink
== pad
)
703 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);
705 static void media_interface_init(struct media_device
*mdev
,
706 struct media_interface
*intf
,
708 u32 intf_type
, u32 flags
)
710 intf
->type
= intf_type
;
712 INIT_LIST_HEAD(&intf
->links
);
714 media_gobj_create(mdev
, gobj_type
, &intf
->graph_obj
);
717 /* Functions related to the media interface via device nodes */
719 struct media_intf_devnode
*media_devnode_create(struct media_device
*mdev
,
721 u32 major
, u32 minor
)
723 struct media_intf_devnode
*devnode
;
725 devnode
= kzalloc(sizeof(*devnode
), GFP_KERNEL
);
729 devnode
->major
= major
;
730 devnode
->minor
= minor
;
732 media_interface_init(mdev
, &devnode
->intf
, MEDIA_GRAPH_INTF_DEVNODE
,
737 EXPORT_SYMBOL_GPL(media_devnode_create
);
739 void media_devnode_remove(struct media_intf_devnode
*devnode
)
741 media_remove_intf_links(&devnode
->intf
);
742 media_gobj_destroy(&devnode
->intf
.graph_obj
);
745 EXPORT_SYMBOL_GPL(media_devnode_remove
);
747 struct media_link
*media_create_intf_link(struct media_entity
*entity
,
748 struct media_interface
*intf
,
751 struct media_link
*link
;
753 link
= media_add_link(&intf
->links
);
758 link
->entity
= entity
;
761 /* Initialize graph object embedded at the new link */
762 media_gobj_create(intf
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
767 EXPORT_SYMBOL_GPL(media_create_intf_link
);
769 void __media_remove_intf_link(struct media_link
*link
)
771 list_del(&link
->list
);
772 media_gobj_destroy(&link
->graph_obj
);
775 EXPORT_SYMBOL_GPL(__media_remove_intf_link
);
777 void media_remove_intf_link(struct media_link
*link
)
779 spin_lock(&link
->graph_obj
.mdev
->lock
);
780 __media_remove_intf_link(link
);
781 spin_unlock(&link
->graph_obj
.mdev
->lock
);
783 EXPORT_SYMBOL_GPL(media_remove_intf_link
);
785 void __media_remove_intf_links(struct media_interface
*intf
)
787 struct media_link
*link
, *tmp
;
789 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
)
790 __media_remove_intf_link(link
);
793 EXPORT_SYMBOL_GPL(__media_remove_intf_links
);
795 void media_remove_intf_links(struct media_interface
*intf
)
797 /* Do nothing if the intf is not registered. */
798 if (intf
->graph_obj
.mdev
== NULL
)
801 spin_lock(&intf
->graph_obj
.mdev
->lock
);
802 __media_remove_intf_links(intf
);
803 spin_unlock(&intf
->graph_obj
.mdev
->lock
);
805 EXPORT_SYMBOL_GPL(media_remove_intf_links
);