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 * media_entity_init - Initialize a media entity
32 * @num_pads: Total number of sink and source pads.
33 * @extra_links: Initial estimate of the number of extra links.
34 * @pads: Array of 'num_pads' pads.
36 * The total number of pads is an intrinsic property of entities known by the
37 * entity driver, while the total number of links depends on hardware design
38 * and is an extrinsic property unknown to the entity driver. However, in most
39 * use cases the entity driver can guess the number of links which can safely
40 * be assumed to be equal to or larger than the number of pads.
42 * For those reasons the links array can be preallocated based on the entity
43 * driver guess and will be reallocated later if extra links need to be
46 * This function allocates a links array with enough space to hold at least
47 * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will
48 * be set to the number of allocated elements.
50 * The pads array is managed by the entity driver and passed to
51 * media_entity_init() where its pointer will be stored in the entity structure.
54 media_entity_init(struct media_entity
*entity
, u16 num_pads
,
55 struct media_pad
*pads
, u16 extra_links
)
57 struct media_link
*links
;
58 unsigned int max_links
= num_pads
+ extra_links
;
61 links
= kzalloc(max_links
* sizeof(links
[0]), GFP_KERNEL
);
66 entity
->max_links
= max_links
;
67 entity
->num_links
= 0;
68 entity
->num_backlinks
= 0;
69 entity
->num_pads
= num_pads
;
71 entity
->links
= links
;
73 for (i
= 0; i
< num_pads
; i
++) {
74 pads
[i
].entity
= entity
;
80 EXPORT_SYMBOL_GPL(media_entity_init
);
83 media_entity_cleanup(struct media_entity
*entity
)
87 EXPORT_SYMBOL_GPL(media_entity_cleanup
);
89 /* -----------------------------------------------------------------------------
93 static struct media_entity
*
94 media_entity_other(struct media_entity
*entity
, struct media_link
*link
)
96 if (link
->source
->entity
== entity
)
97 return link
->sink
->entity
;
99 return link
->source
->entity
;
102 /* push an entity to traversal stack */
103 static void stack_push(struct media_entity_graph
*graph
,
104 struct media_entity
*entity
)
106 if (graph
->top
== MEDIA_ENTITY_ENUM_MAX_DEPTH
- 1) {
111 graph
->stack
[graph
->top
].link
= 0;
112 graph
->stack
[graph
->top
].entity
= entity
;
115 static struct media_entity
*stack_pop(struct media_entity_graph
*graph
)
117 struct media_entity
*entity
;
119 entity
= graph
->stack
[graph
->top
].entity
;
125 #define link_top(en) ((en)->stack[(en)->top].link)
126 #define stack_top(en) ((en)->stack[(en)->top].entity)
129 * media_entity_graph_walk_start - Start walking the media graph at a given entity
130 * @graph: Media graph structure that will be used to walk the graph
131 * @entity: Starting entity
133 * This function initializes the graph traversal structure to walk the entities
134 * graph starting at the given entity. The traversal structure must not be
135 * modified by the caller during graph traversal. When done the structure can
138 void media_entity_graph_walk_start(struct media_entity_graph
*graph
,
139 struct media_entity
*entity
)
142 graph
->stack
[graph
->top
].entity
= NULL
;
143 bitmap_zero(graph
->entities
, MEDIA_ENTITY_ENUM_MAX_ID
);
145 if (WARN_ON(entity
->id
>= MEDIA_ENTITY_ENUM_MAX_ID
))
148 __set_bit(entity
->id
, graph
->entities
);
149 stack_push(graph
, entity
);
151 EXPORT_SYMBOL_GPL(media_entity_graph_walk_start
);
154 * media_entity_graph_walk_next - Get the next entity in the graph
155 * @graph: Media graph structure
157 * Perform a depth-first traversal of the given media entities graph.
159 * The graph structure must have been previously initialized with a call to
160 * media_entity_graph_walk_start().
162 * Return the next entity in the graph or NULL if the whole graph have been
165 struct media_entity
*
166 media_entity_graph_walk_next(struct media_entity_graph
*graph
)
168 if (stack_top(graph
) == NULL
)
172 * Depth first search. Push entity to stack and continue from
173 * top of the stack until no more entities on the level can be
176 while (link_top(graph
) < stack_top(graph
)->num_links
) {
177 struct media_entity
*entity
= stack_top(graph
);
178 struct media_link
*link
= &entity
->links
[link_top(graph
)];
179 struct media_entity
*next
;
181 /* The link is not enabled so we do not follow. */
182 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
187 /* Get the entity in the other end of the link . */
188 next
= media_entity_other(entity
, link
);
189 if (WARN_ON(next
->id
>= MEDIA_ENTITY_ENUM_MAX_ID
))
192 /* Has the entity already been visited? */
193 if (__test_and_set_bit(next
->id
, graph
->entities
)) {
198 /* Push the new entity to stack and start over. */
200 stack_push(graph
, next
);
203 return stack_pop(graph
);
205 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next
);
207 /* -----------------------------------------------------------------------------
208 * Pipeline management
212 * media_entity_pipeline_start - Mark a pipeline as streaming
213 * @entity: Starting entity
214 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
216 * Mark all entities connected to a given entity through enabled links, either
217 * directly or indirectly, as streaming. The given pipeline object is assigned to
218 * every entity in the pipeline and stored in the media_entity pipe field.
220 * Calls to this function can be nested, in which case the same number of
221 * media_entity_pipeline_stop() calls will be required to stop streaming. The
222 * pipeline pointer must be identical for all nested calls to
223 * media_entity_pipeline_start().
225 __must_check
int media_entity_pipeline_start(struct media_entity
*entity
,
226 struct media_pipeline
*pipe
)
228 struct media_device
*mdev
= entity
->parent
;
229 struct media_entity_graph graph
;
230 struct media_entity
*entity_err
= entity
;
233 mutex_lock(&mdev
->graph_mutex
);
235 media_entity_graph_walk_start(&graph
, entity
);
237 while ((entity
= media_entity_graph_walk_next(&graph
))) {
240 entity
->stream_count
++;
241 WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
);
244 /* Already streaming --- no need to check. */
245 if (entity
->stream_count
> 1)
248 if (!entity
->ops
|| !entity
->ops
->link_validate
)
251 for (i
= 0; i
< entity
->num_links
; i
++) {
252 struct media_link
*link
= &entity
->links
[i
];
254 /* Is this pad part of an enabled link? */
255 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
258 /* Are we the sink or not? */
259 if (link
->sink
->entity
!= entity
)
262 ret
= entity
->ops
->link_validate(link
);
263 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
268 mutex_unlock(&mdev
->graph_mutex
);
274 * Link validation on graph failed. We revert what we did and
277 media_entity_graph_walk_start(&graph
, entity_err
);
279 while ((entity_err
= media_entity_graph_walk_next(&graph
))) {
280 entity_err
->stream_count
--;
281 if (entity_err
->stream_count
== 0)
282 entity_err
->pipe
= NULL
;
285 * We haven't increased stream_count further than this
288 if (entity_err
== entity
)
292 mutex_unlock(&mdev
->graph_mutex
);
296 EXPORT_SYMBOL_GPL(media_entity_pipeline_start
);
299 * media_entity_pipeline_stop - Mark a pipeline as not streaming
300 * @entity: Starting entity
302 * Mark all entities connected to a given entity through enabled links, either
303 * directly or indirectly, as not streaming. The media_entity pipe field is
306 * If multiple calls to media_entity_pipeline_start() have been made, the same
307 * number of calls to this function are required to mark the pipeline as not
310 void media_entity_pipeline_stop(struct media_entity
*entity
)
312 struct media_device
*mdev
= entity
->parent
;
313 struct media_entity_graph graph
;
315 mutex_lock(&mdev
->graph_mutex
);
317 media_entity_graph_walk_start(&graph
, entity
);
319 while ((entity
= media_entity_graph_walk_next(&graph
))) {
320 entity
->stream_count
--;
321 if (entity
->stream_count
== 0)
325 mutex_unlock(&mdev
->graph_mutex
);
327 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop
);
329 /* -----------------------------------------------------------------------------
334 * media_entity_get - Get a reference to the parent module
335 * @entity: The entity
337 * Get a reference to the parent media device module.
339 * The function will return immediately if @entity is NULL.
341 * Return a pointer to the entity on success or NULL on failure.
343 struct media_entity
*media_entity_get(struct media_entity
*entity
)
348 if (entity
->parent
->dev
&&
349 !try_module_get(entity
->parent
->dev
->driver
->owner
))
354 EXPORT_SYMBOL_GPL(media_entity_get
);
357 * media_entity_put - Release the reference to the parent module
358 * @entity: The entity
360 * Release the reference count acquired by media_entity_get().
362 * The function will return immediately if @entity is NULL.
364 void media_entity_put(struct media_entity
*entity
)
369 if (entity
->parent
->dev
)
370 module_put(entity
->parent
->dev
->driver
->owner
);
372 EXPORT_SYMBOL_GPL(media_entity_put
);
374 /* -----------------------------------------------------------------------------
378 static struct media_link
*media_entity_add_link(struct media_entity
*entity
)
380 if (entity
->num_links
>= entity
->max_links
) {
381 struct media_link
*links
= entity
->links
;
382 unsigned int max_links
= entity
->max_links
+ 2;
385 links
= krealloc(links
, max_links
* sizeof(*links
), GFP_KERNEL
);
389 for (i
= 0; i
< entity
->num_links
; i
++)
390 links
[i
].reverse
->reverse
= &links
[i
];
392 entity
->max_links
= max_links
;
393 entity
->links
= links
;
396 return &entity
->links
[entity
->num_links
++];
400 media_entity_create_link(struct media_entity
*source
, u16 source_pad
,
401 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
403 struct media_link
*link
;
404 struct media_link
*backlink
;
406 BUG_ON(source
== NULL
|| sink
== NULL
);
407 BUG_ON(source_pad
>= source
->num_pads
);
408 BUG_ON(sink_pad
>= sink
->num_pads
);
410 link
= media_entity_add_link(source
);
414 link
->source
= &source
->pads
[source_pad
];
415 link
->sink
= &sink
->pads
[sink_pad
];
418 /* Create the backlink. Backlinks are used to help graph traversal and
419 * are not reported to userspace.
421 backlink
= media_entity_add_link(sink
);
422 if (backlink
== NULL
) {
427 backlink
->source
= &source
->pads
[source_pad
];
428 backlink
->sink
= &sink
->pads
[sink_pad
];
429 backlink
->flags
= flags
;
431 link
->reverse
= backlink
;
432 backlink
->reverse
= link
;
434 sink
->num_backlinks
++;
438 EXPORT_SYMBOL_GPL(media_entity_create_link
);
440 void __media_entity_remove_links(struct media_entity
*entity
)
444 for (i
= 0; i
< entity
->num_links
; i
++) {
445 struct media_link
*link
= &entity
->links
[i
];
446 struct media_entity
*remote
;
449 if (link
->source
->entity
== entity
)
450 remote
= link
->sink
->entity
;
452 remote
= link
->source
->entity
;
454 while (r
< remote
->num_links
) {
455 struct media_link
*rlink
= &remote
->links
[r
];
457 if (rlink
!= link
->reverse
) {
462 if (link
->source
->entity
== entity
)
463 remote
->num_backlinks
--;
465 if (--remote
->num_links
== 0)
468 /* Insert last entry in place of the dropped link. */
469 *rlink
= remote
->links
[remote
->num_links
];
473 entity
->num_links
= 0;
474 entity
->num_backlinks
= 0;
476 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
478 void media_entity_remove_links(struct media_entity
*entity
)
480 /* Do nothing if the entity is not registered. */
481 if (entity
->parent
== NULL
)
484 mutex_lock(&entity
->parent
->graph_mutex
);
485 __media_entity_remove_links(entity
);
486 mutex_unlock(&entity
->parent
->graph_mutex
);
488 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
490 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
494 /* Notify both entities. */
495 ret
= media_entity_call(link
->source
->entity
, link_setup
,
496 link
->source
, link
->sink
, flags
);
497 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
500 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
501 link
->sink
, link
->source
, flags
);
502 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
503 media_entity_call(link
->source
->entity
, link_setup
,
504 link
->source
, link
->sink
, link
->flags
);
509 link
->reverse
->flags
= link
->flags
;
515 * __media_entity_setup_link - Configure a media link
516 * @link: The link being configured
517 * @flags: Link configuration flags
519 * The bulk of link setup is handled by the two entities connected through the
520 * link. This function notifies both entities of the link configuration change.
522 * If the link is immutable or if the current and new configuration are
523 * identical, return immediately.
525 * The user is expected to hold link->source->parent->mutex. If not,
526 * media_entity_setup_link() should be used instead.
528 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
530 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
531 struct media_device
*mdev
;
532 struct media_entity
*source
, *sink
;
538 /* The non-modifiable link flags must not be modified. */
539 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
542 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
543 return link
->flags
== flags
? 0 : -EINVAL
;
545 if (link
->flags
== flags
)
548 source
= link
->source
->entity
;
549 sink
= link
->sink
->entity
;
551 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
552 (source
->stream_count
|| sink
->stream_count
))
555 mdev
= source
->parent
;
557 if (mdev
->link_notify
) {
558 ret
= mdev
->link_notify(link
, flags
,
559 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
564 ret
= __media_entity_setup_link_notify(link
, flags
);
566 if (mdev
->link_notify
)
567 mdev
->link_notify(link
, flags
, MEDIA_DEV_NOTIFY_POST_LINK_CH
);
572 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
576 mutex_lock(&link
->source
->entity
->parent
->graph_mutex
);
577 ret
= __media_entity_setup_link(link
, flags
);
578 mutex_unlock(&link
->source
->entity
->parent
->graph_mutex
);
582 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
585 * media_entity_find_link - Find a link between two pads
586 * @source: Source pad
589 * Return a pointer to the link between the two entities. If no such link
590 * exists, return NULL.
593 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
595 struct media_link
*link
;
598 for (i
= 0; i
< source
->entity
->num_links
; ++i
) {
599 link
= &source
->entity
->links
[i
];
601 if (link
->source
->entity
== source
->entity
&&
602 link
->source
->index
== source
->index
&&
603 link
->sink
->entity
== sink
->entity
&&
604 link
->sink
->index
== sink
->index
)
610 EXPORT_SYMBOL_GPL(media_entity_find_link
);
613 * media_entity_remote_pad - Find the pad at the remote end of a link
614 * @pad: Pad at the local end of the link
616 * Search for a remote pad connected to the given pad by iterating over all
617 * links originating or terminating at that pad until an enabled link is found.
619 * Return a pointer to the pad at the remote end of the first found enabled
620 * link, or NULL if no enabled link has been found.
622 struct media_pad
*media_entity_remote_pad(struct media_pad
*pad
)
626 for (i
= 0; i
< pad
->entity
->num_links
; i
++) {
627 struct media_link
*link
= &pad
->entity
->links
[i
];
629 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
632 if (link
->source
== pad
)
635 if (link
->sink
== pad
)
642 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);