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>
30 * dev_dbg_obj - Prints in debug mode a change on some object
32 * @event_name: Name of the event to report. Could be __func__
33 * @gobj: Pointer to the object
35 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
36 * won't produce any code.
38 static inline const char *gobj_type(enum media_gobj_type type
)
41 case MEDIA_GRAPH_ENTITY
:
45 case MEDIA_GRAPH_LINK
:
52 static void dev_dbg_obj(const char *event_name
, struct media_gobj
*gobj
)
54 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
55 switch (media_type(gobj
)) {
56 case MEDIA_GRAPH_ENTITY
:
57 dev_dbg(gobj
->mdev
->dev
,
58 "%s: id 0x%08x entity#%d: '%s'\n",
59 event_name
, gobj
->id
, media_localid(gobj
),
60 gobj_to_entity(gobj
)->name
);
62 case MEDIA_GRAPH_LINK
:
64 struct media_link
*link
= gobj_to_link(gobj
);
66 dev_dbg(gobj
->mdev
->dev
,
67 "%s: id 0x%08x link#%d: '%s' %s#%d ==> '%s' %s#%d\n",
68 event_name
, gobj
->id
, media_localid(gobj
),
70 link
->source
->entity
->name
,
71 gobj_type(media_type(&link
->source
->graph_obj
)),
72 media_localid(&link
->source
->graph_obj
),
74 link
->sink
->entity
->name
,
75 gobj_type(media_type(&link
->sink
->graph_obj
)),
76 media_localid(&link
->sink
->graph_obj
));
81 struct media_pad
*pad
= gobj_to_pad(gobj
);
83 dev_dbg(gobj
->mdev
->dev
,
84 "%s: id 0x%08x pad#%d: '%s':%d\n",
85 event_name
, gobj
->id
, media_localid(gobj
),
86 pad
->entity
->name
, pad
->index
);
93 * media_gobj_init - Initialize a graph object
95 * @mdev: Pointer to the media_device that contains the object
96 * @type: Type of the object
97 * @gobj: Pointer to the object
99 * This routine initializes the embedded struct media_gobj inside a
100 * media graph object. It is called automatically if media_*_create()
101 * calls are used. However, if the object (entity, link, pad, interface)
102 * is embedded on some other object, this function should be called before
103 * registering the object at the media controller.
105 void media_gobj_init(struct media_device
*mdev
,
106 enum media_gobj_type type
,
107 struct media_gobj
*gobj
)
111 /* Create a per-type unique object ID */
113 case MEDIA_GRAPH_ENTITY
:
114 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->entity_id
);
116 case MEDIA_GRAPH_PAD
:
117 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->pad_id
);
119 case MEDIA_GRAPH_LINK
:
120 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->link_id
);
123 dev_dbg_obj(__func__
, gobj
);
127 * media_gobj_remove - Stop using a graph object on a media device
129 * @graph_obj: Pointer to the object
131 * This should be called at media_device_unregister_*() routines
133 void media_gobj_remove(struct media_gobj
*gobj
)
135 dev_dbg_obj(__func__
, gobj
);
139 * media_entity_init - Initialize a media entity
141 * @num_pads: Total number of sink and source pads.
142 * @pads: Array of 'num_pads' pads.
144 * The total number of pads is an intrinsic property of entities known by the
145 * entity driver, while the total number of links depends on hardware design
146 * and is an extrinsic property unknown to the entity driver. However, in most
147 * use cases the number of links can safely be assumed to be equal to or
148 * larger than the number of pads.
150 * For those reasons the links array can be preallocated based on the number
151 * of pads and will be reallocated later if extra links need to be created.
153 * This function allocates a links array with enough space to hold at least
154 * 'num_pads' elements. The media_entity::max_links field will be set to the
155 * number of allocated elements.
157 * The pads array is managed by the entity driver and passed to
158 * media_entity_init() where its pointer will be stored in the entity structure.
161 media_entity_init(struct media_entity
*entity
, u16 num_pads
,
162 struct media_pad
*pads
)
164 struct media_link
*links
;
165 unsigned int max_links
= num_pads
;
168 links
= kzalloc(max_links
* sizeof(links
[0]), GFP_KERNEL
);
172 entity
->group_id
= 0;
173 entity
->max_links
= max_links
;
174 entity
->num_links
= 0;
175 entity
->num_backlinks
= 0;
176 entity
->num_pads
= num_pads
;
178 entity
->links
= links
;
180 for (i
= 0; i
< num_pads
; i
++) {
181 pads
[i
].entity
= entity
;
187 EXPORT_SYMBOL_GPL(media_entity_init
);
190 media_entity_cleanup(struct media_entity
*entity
)
192 kfree(entity
->links
);
194 EXPORT_SYMBOL_GPL(media_entity_cleanup
);
196 /* -----------------------------------------------------------------------------
200 static struct media_entity
*
201 media_entity_other(struct media_entity
*entity
, struct media_link
*link
)
203 if (link
->source
->entity
== entity
)
204 return link
->sink
->entity
;
206 return link
->source
->entity
;
209 /* push an entity to traversal stack */
210 static void stack_push(struct media_entity_graph
*graph
,
211 struct media_entity
*entity
)
213 if (graph
->top
== MEDIA_ENTITY_ENUM_MAX_DEPTH
- 1) {
218 graph
->stack
[graph
->top
].link
= 0;
219 graph
->stack
[graph
->top
].entity
= entity
;
222 static struct media_entity
*stack_pop(struct media_entity_graph
*graph
)
224 struct media_entity
*entity
;
226 entity
= graph
->stack
[graph
->top
].entity
;
232 #define link_top(en) ((en)->stack[(en)->top].link)
233 #define stack_top(en) ((en)->stack[(en)->top].entity)
236 * media_entity_graph_walk_start - Start walking the media graph at a given entity
237 * @graph: Media graph structure that will be used to walk the graph
238 * @entity: Starting entity
240 * This function initializes the graph traversal structure to walk the entities
241 * graph starting at the given entity. The traversal structure must not be
242 * modified by the caller during graph traversal. When done the structure can
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
);
261 * media_entity_graph_walk_next - Get the next entity in the graph
262 * @graph: Media graph structure
264 * Perform a depth-first traversal of the given media entities graph.
266 * The graph structure must have been previously initialized with a call to
267 * media_entity_graph_walk_start().
269 * Return the next entity in the graph or NULL if the whole graph have been
272 struct media_entity
*
273 media_entity_graph_walk_next(struct media_entity_graph
*graph
)
275 if (stack_top(graph
) == NULL
)
279 * Depth first search. Push entity to stack and continue from
280 * top of the stack until no more entities on the level can be
283 while (link_top(graph
) < stack_top(graph
)->num_links
) {
284 struct media_entity
*entity
= stack_top(graph
);
285 struct media_link
*link
= &entity
->links
[link_top(graph
)];
286 struct media_entity
*next
;
288 /* The link is not enabled so we do not follow. */
289 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
294 /* Get the entity in the other end of the link . */
295 next
= media_entity_other(entity
, link
);
296 if (WARN_ON(media_entity_id(next
) >= MEDIA_ENTITY_ENUM_MAX_ID
))
299 /* Has the entity already been visited? */
300 if (__test_and_set_bit(media_entity_id(next
), graph
->entities
)) {
305 /* Push the new entity to stack and start over. */
307 stack_push(graph
, next
);
310 return stack_pop(graph
);
312 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next
);
314 /* -----------------------------------------------------------------------------
315 * Pipeline management
319 * media_entity_pipeline_start - Mark a pipeline as streaming
320 * @entity: Starting entity
321 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
323 * Mark all entities connected to a given entity through enabled links, either
324 * directly or indirectly, as streaming. The given pipeline object is assigned to
325 * every entity in the pipeline and stored in the media_entity pipe field.
327 * Calls to this function can be nested, in which case the same number of
328 * media_entity_pipeline_stop() calls will be required to stop streaming. The
329 * pipeline pointer must be identical for all nested calls to
330 * media_entity_pipeline_start().
332 __must_check
int media_entity_pipeline_start(struct media_entity
*entity
,
333 struct media_pipeline
*pipe
)
335 struct media_device
*mdev
= entity
->parent
;
336 struct media_entity_graph graph
;
337 struct media_entity
*entity_err
= entity
;
340 mutex_lock(&mdev
->graph_mutex
);
342 media_entity_graph_walk_start(&graph
, entity
);
344 while ((entity
= media_entity_graph_walk_next(&graph
))) {
345 DECLARE_BITMAP(active
, MEDIA_ENTITY_MAX_PADS
);
346 DECLARE_BITMAP(has_no_links
, MEDIA_ENTITY_MAX_PADS
);
349 entity
->stream_count
++;
350 WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
);
353 /* Already streaming --- no need to check. */
354 if (entity
->stream_count
> 1)
357 if (!entity
->ops
|| !entity
->ops
->link_validate
)
360 bitmap_zero(active
, entity
->num_pads
);
361 bitmap_fill(has_no_links
, entity
->num_pads
);
363 for (i
= 0; i
< entity
->num_links
; i
++) {
364 struct media_link
*link
= &entity
->links
[i
];
365 struct media_pad
*pad
= link
->sink
->entity
== entity
366 ? link
->sink
: link
->source
;
368 /* Mark that a pad is connected by a link. */
369 bitmap_clear(has_no_links
, pad
->index
, 1);
372 * Pads that either do not need to connect or
373 * are connected through an enabled link are
376 if (!(pad
->flags
& MEDIA_PAD_FL_MUST_CONNECT
) ||
377 link
->flags
& MEDIA_LNK_FL_ENABLED
)
378 bitmap_set(active
, pad
->index
, 1);
381 * Link validation will only take place for
382 * sink ends of the link that are enabled.
384 if (link
->sink
!= pad
||
385 !(link
->flags
& MEDIA_LNK_FL_ENABLED
))
388 ret
= entity
->ops
->link_validate(link
);
389 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
390 dev_dbg(entity
->parent
->dev
,
391 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
392 link
->source
->entity
->name
,
394 entity
->name
, link
->sink
->index
, ret
);
399 /* Either no links or validated links are fine. */
400 bitmap_or(active
, active
, has_no_links
, entity
->num_pads
);
402 if (!bitmap_full(active
, entity
->num_pads
)) {
404 dev_dbg(entity
->parent
->dev
,
405 "\"%s\":%u must be connected by an enabled link\n",
407 (unsigned)find_first_zero_bit(
408 active
, entity
->num_pads
));
413 mutex_unlock(&mdev
->graph_mutex
);
419 * Link validation on graph failed. We revert what we did and
422 media_entity_graph_walk_start(&graph
, entity_err
);
424 while ((entity_err
= media_entity_graph_walk_next(&graph
))) {
425 entity_err
->stream_count
--;
426 if (entity_err
->stream_count
== 0)
427 entity_err
->pipe
= NULL
;
430 * We haven't increased stream_count further than this
433 if (entity_err
== entity
)
437 mutex_unlock(&mdev
->graph_mutex
);
441 EXPORT_SYMBOL_GPL(media_entity_pipeline_start
);
444 * media_entity_pipeline_stop - Mark a pipeline as not streaming
445 * @entity: Starting entity
447 * Mark all entities connected to a given entity through enabled links, either
448 * directly or indirectly, as not streaming. The media_entity pipe field is
451 * If multiple calls to media_entity_pipeline_start() have been made, the same
452 * number of calls to this function are required to mark the pipeline as not
455 void media_entity_pipeline_stop(struct media_entity
*entity
)
457 struct media_device
*mdev
= entity
->parent
;
458 struct media_entity_graph graph
;
460 mutex_lock(&mdev
->graph_mutex
);
462 media_entity_graph_walk_start(&graph
, entity
);
464 while ((entity
= media_entity_graph_walk_next(&graph
))) {
465 entity
->stream_count
--;
466 if (entity
->stream_count
== 0)
470 mutex_unlock(&mdev
->graph_mutex
);
472 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop
);
474 /* -----------------------------------------------------------------------------
479 * media_entity_get - Get a reference to the parent module
480 * @entity: The entity
482 * Get a reference to the parent media device module.
484 * The function will return immediately if @entity is NULL.
486 * Return a pointer to the entity on success or NULL on failure.
488 struct media_entity
*media_entity_get(struct media_entity
*entity
)
493 if (entity
->parent
->dev
&&
494 !try_module_get(entity
->parent
->dev
->driver
->owner
))
499 EXPORT_SYMBOL_GPL(media_entity_get
);
502 * media_entity_put - Release the reference to the parent module
503 * @entity: The entity
505 * Release the reference count acquired by media_entity_get().
507 * The function will return immediately if @entity is NULL.
509 void media_entity_put(struct media_entity
*entity
)
514 if (entity
->parent
->dev
)
515 module_put(entity
->parent
->dev
->driver
->owner
);
517 EXPORT_SYMBOL_GPL(media_entity_put
);
519 /* -----------------------------------------------------------------------------
523 static struct media_link
*media_entity_add_link(struct media_entity
*entity
)
525 if (entity
->num_links
>= entity
->max_links
) {
526 struct media_link
*links
= entity
->links
;
527 unsigned int max_links
= entity
->max_links
+ 2;
530 links
= krealloc(links
, max_links
* sizeof(*links
), GFP_KERNEL
);
534 for (i
= 0; i
< entity
->num_links
; i
++)
535 links
[i
].reverse
->reverse
= &links
[i
];
537 entity
->max_links
= max_links
;
538 entity
->links
= links
;
541 return &entity
->links
[entity
->num_links
++];
545 media_create_pad_link(struct media_entity
*source
, u16 source_pad
,
546 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
548 struct media_link
*link
;
549 struct media_link
*backlink
;
551 BUG_ON(source
== NULL
|| sink
== NULL
);
552 BUG_ON(source_pad
>= source
->num_pads
);
553 BUG_ON(sink_pad
>= sink
->num_pads
);
555 link
= media_entity_add_link(source
);
559 link
->source
= &source
->pads
[source_pad
];
560 link
->sink
= &sink
->pads
[sink_pad
];
563 /* Initialize graph object embedded at the new link */
564 media_gobj_init(source
->parent
, MEDIA_GRAPH_LINK
, &link
->graph_obj
);
566 /* Create the backlink. Backlinks are used to help graph traversal and
567 * are not reported to userspace.
569 backlink
= media_entity_add_link(sink
);
570 if (backlink
== NULL
) {
575 backlink
->source
= &source
->pads
[source_pad
];
576 backlink
->sink
= &sink
->pads
[sink_pad
];
577 backlink
->flags
= flags
;
579 /* Initialize graph object embedded at the new link */
580 media_gobj_init(sink
->parent
, MEDIA_GRAPH_LINK
, &backlink
->graph_obj
);
582 link
->reverse
= backlink
;
583 backlink
->reverse
= link
;
585 sink
->num_backlinks
++;
589 EXPORT_SYMBOL_GPL(media_create_pad_link
);
591 void __media_entity_remove_links(struct media_entity
*entity
)
595 for (i
= 0; i
< entity
->num_links
; i
++) {
596 struct media_link
*link
= &entity
->links
[i
];
597 struct media_entity
*remote
;
600 if (link
->source
->entity
== entity
)
601 remote
= link
->sink
->entity
;
603 remote
= link
->source
->entity
;
605 while (r
< remote
->num_links
) {
606 struct media_link
*rlink
= &remote
->links
[r
];
608 if (rlink
!= link
->reverse
) {
613 if (link
->source
->entity
== entity
)
614 remote
->num_backlinks
--;
616 if (--remote
->num_links
== 0)
619 /* Insert last entry in place of the dropped link. */
620 *rlink
= remote
->links
[remote
->num_links
];
624 entity
->num_links
= 0;
625 entity
->num_backlinks
= 0;
627 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
629 void media_entity_remove_links(struct media_entity
*entity
)
631 /* Do nothing if the entity is not registered. */
632 if (entity
->parent
== NULL
)
635 mutex_lock(&entity
->parent
->graph_mutex
);
636 __media_entity_remove_links(entity
);
637 mutex_unlock(&entity
->parent
->graph_mutex
);
639 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
641 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
645 /* Notify both entities. */
646 ret
= media_entity_call(link
->source
->entity
, link_setup
,
647 link
->source
, link
->sink
, flags
);
648 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
651 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
652 link
->sink
, link
->source
, flags
);
653 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
654 media_entity_call(link
->source
->entity
, link_setup
,
655 link
->source
, link
->sink
, link
->flags
);
660 link
->reverse
->flags
= link
->flags
;
666 * __media_entity_setup_link - Configure a media link
667 * @link: The link being configured
668 * @flags: Link configuration flags
670 * The bulk of link setup is handled by the two entities connected through the
671 * link. This function notifies both entities of the link configuration change.
673 * If the link is immutable or if the current and new configuration are
674 * identical, return immediately.
676 * The user is expected to hold link->source->parent->mutex. If not,
677 * media_entity_setup_link() should be used instead.
679 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
681 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
682 struct media_device
*mdev
;
683 struct media_entity
*source
, *sink
;
689 /* The non-modifiable link flags must not be modified. */
690 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
693 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
694 return link
->flags
== flags
? 0 : -EINVAL
;
696 if (link
->flags
== flags
)
699 source
= link
->source
->entity
;
700 sink
= link
->sink
->entity
;
702 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
703 (source
->stream_count
|| sink
->stream_count
))
706 mdev
= source
->parent
;
708 if (mdev
->link_notify
) {
709 ret
= mdev
->link_notify(link
, flags
,
710 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
715 ret
= __media_entity_setup_link_notify(link
, flags
);
717 if (mdev
->link_notify
)
718 mdev
->link_notify(link
, flags
, MEDIA_DEV_NOTIFY_POST_LINK_CH
);
723 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
727 mutex_lock(&link
->source
->entity
->parent
->graph_mutex
);
728 ret
= __media_entity_setup_link(link
, flags
);
729 mutex_unlock(&link
->source
->entity
->parent
->graph_mutex
);
733 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
736 * media_entity_find_link - Find a link between two pads
737 * @source: Source pad
740 * Return a pointer to the link between the two entities. If no such link
741 * exists, return NULL.
744 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
746 struct media_link
*link
;
749 for (i
= 0; i
< source
->entity
->num_links
; ++i
) {
750 link
= &source
->entity
->links
[i
];
752 if (link
->source
->entity
== source
->entity
&&
753 link
->source
->index
== source
->index
&&
754 link
->sink
->entity
== sink
->entity
&&
755 link
->sink
->index
== sink
->index
)
761 EXPORT_SYMBOL_GPL(media_entity_find_link
);
764 * media_entity_remote_pad - Find the pad at the remote end of a link
765 * @pad: Pad at the local end of the link
767 * Search for a remote pad connected to the given pad by iterating over all
768 * links originating or terminating at that pad until an enabled link is found.
770 * Return a pointer to the pad at the remote end of the first found enabled
771 * link, or NULL if no enabled link has been found.
773 struct media_pad
*media_entity_remote_pad(struct media_pad
*pad
)
777 for (i
= 0; i
< pad
->entity
->num_links
; i
++) {
778 struct media_link
*link
= &pad
->entity
->links
[i
];
780 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
783 if (link
->source
== pad
)
786 if (link
->sink
== pad
)
793 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);