blob: 2c286c307145b6828a7ac8c3b4bfc4f05ae18735 [file] [log] [blame]
Laurent Pinchart53e269c2009-12-09 08:40:00 -03001/*
2 * Media entity
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
8 *
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.
12 *
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.
17 *
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
21 */
22
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -030023#include <linux/bitmap.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030024#include <linux/module.h>
25#include <linux/slab.h>
26#include <media/media-entity.h>
Laurent Pinchart503c3d822010-03-07 15:04:59 -030027#include <media/media-device.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030028
29/**
30 * media_entity_init - Initialize a media entity
31 *
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.
35 *
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.
41 *
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
44 * created.
45 *
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.
49 *
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.
52 */
53int
54media_entity_init(struct media_entity *entity, u16 num_pads,
55 struct media_pad *pads, u16 extra_links)
56{
57 struct media_link *links;
58 unsigned int max_links = num_pads + extra_links;
59 unsigned int i;
60
61 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
62 if (links == NULL)
63 return -ENOMEM;
64
65 entity->group_id = 0;
66 entity->max_links = max_links;
67 entity->num_links = 0;
68 entity->num_backlinks = 0;
69 entity->num_pads = num_pads;
70 entity->pads = pads;
71 entity->links = links;
72
73 for (i = 0; i < num_pads; i++) {
74 pads[i].entity = entity;
75 pads[i].index = i;
76 }
77
78 return 0;
79}
80EXPORT_SYMBOL_GPL(media_entity_init);
81
82void
83media_entity_cleanup(struct media_entity *entity)
84{
85 kfree(entity->links);
86}
87EXPORT_SYMBOL_GPL(media_entity_cleanup);
88
Sakari Ailusa5ccc482010-03-07 16:14:14 -030089/* -----------------------------------------------------------------------------
90 * Graph traversal
91 */
92
93static struct media_entity *
94media_entity_other(struct media_entity *entity, struct media_link *link)
95{
96 if (link->source->entity == entity)
97 return link->sink->entity;
98 else
99 return link->source->entity;
100}
101
102/* push an entity to traversal stack */
103static void stack_push(struct media_entity_graph *graph,
104 struct media_entity *entity)
105{
106 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
107 WARN_ON(1);
108 return;
109 }
110 graph->top++;
111 graph->stack[graph->top].link = 0;
112 graph->stack[graph->top].entity = entity;
113}
114
115static struct media_entity *stack_pop(struct media_entity_graph *graph)
116{
117 struct media_entity *entity;
118
119 entity = graph->stack[graph->top].entity;
120 graph->top--;
121
122 return entity;
123}
124
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300125#define link_top(en) ((en)->stack[(en)->top].link)
126#define stack_top(en) ((en)->stack[(en)->top].entity)
127
128/**
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
132 *
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
136 * safely be freed.
137 */
138void media_entity_graph_walk_start(struct media_entity_graph *graph,
139 struct media_entity *entity)
140{
141 graph->top = 0;
142 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300143 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
144
145 if (WARN_ON(entity->id >= MEDIA_ENTITY_ENUM_MAX_ID))
146 return;
147
148 __set_bit(entity->id, graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300149 stack_push(graph, entity);
150}
151EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
152
153/**
154 * media_entity_graph_walk_next - Get the next entity in the graph
155 * @graph: Media graph structure
156 *
157 * Perform a depth-first traversal of the given media entities graph.
158 *
159 * The graph structure must have been previously initialized with a call to
160 * media_entity_graph_walk_start().
161 *
162 * Return the next entity in the graph or NULL if the whole graph have been
163 * traversed.
164 */
165struct media_entity *
166media_entity_graph_walk_next(struct media_entity_graph *graph)
167{
168 if (stack_top(graph) == NULL)
169 return NULL;
170
171 /*
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
174 * found.
175 */
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;
180
181 /* The link is not enabled so we do not follow. */
182 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
183 link_top(graph)++;
184 continue;
185 }
186
187 /* Get the entity in the other end of the link . */
188 next = media_entity_other(entity, link);
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300189 if (WARN_ON(next->id >= MEDIA_ENTITY_ENUM_MAX_ID))
190 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300191
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300192 /* Has the entity already been visited? */
193 if (__test_and_set_bit(next->id, graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300194 link_top(graph)++;
195 continue;
196 }
197
198 /* Push the new entity to stack and start over. */
199 link_top(graph)++;
200 stack_push(graph, next);
201 }
202
203 return stack_pop(graph);
204}
205EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
206
207/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300208 * Pipeline management
209 */
210
211/**
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.
215 *
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.
219 *
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().
224 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300225__must_check int media_entity_pipeline_start(struct media_entity *entity,
226 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300227{
228 struct media_device *mdev = entity->parent;
229 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300230 struct media_entity *entity_err = entity;
231 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300232
233 mutex_lock(&mdev->graph_mutex);
234
235 media_entity_graph_walk_start(&graph, entity);
236
237 while ((entity = media_entity_graph_walk_next(&graph))) {
Sakari Ailusaf88be32012-01-11 06:25:15 -0300238 unsigned int i;
239
Laurent Pincharte02188c2010-08-25 09:00:41 -0300240 entity->stream_count++;
241 WARN_ON(entity->pipe && entity->pipe != pipe);
242 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300243
244 /* Already streaming --- no need to check. */
245 if (entity->stream_count > 1)
246 continue;
247
248 if (!entity->ops || !entity->ops->link_validate)
249 continue;
250
251 for (i = 0; i < entity->num_links; i++) {
252 struct media_link *link = &entity->links[i];
253
254 /* Is this pad part of an enabled link? */
255 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
256 continue;
257
258 /* Are we the sink or not? */
259 if (link->sink->entity != entity)
260 continue;
261
262 ret = entity->ops->link_validate(link);
263 if (ret < 0 && ret != -ENOIOCTLCMD)
264 goto error;
265 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300266 }
267
268 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300269
270 return 0;
271
272error:
273 /*
274 * Link validation on graph failed. We revert what we did and
275 * return the error.
276 */
277 media_entity_graph_walk_start(&graph, entity_err);
278
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;
283
284 /*
285 * We haven't increased stream_count further than this
286 * so we quit here.
287 */
288 if (entity_err == entity)
289 break;
290 }
291
292 mutex_unlock(&mdev->graph_mutex);
293
294 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300295}
296EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
297
298/**
299 * media_entity_pipeline_stop - Mark a pipeline as not streaming
300 * @entity: Starting entity
301 *
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
304 * reset to NULL.
305 *
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
308 * streaming.
309 */
310void media_entity_pipeline_stop(struct media_entity *entity)
311{
312 struct media_device *mdev = entity->parent;
313 struct media_entity_graph graph;
314
315 mutex_lock(&mdev->graph_mutex);
316
317 media_entity_graph_walk_start(&graph, entity);
318
319 while ((entity = media_entity_graph_walk_next(&graph))) {
320 entity->stream_count--;
321 if (entity->stream_count == 0)
322 entity->pipe = NULL;
323 }
324
325 mutex_unlock(&mdev->graph_mutex);
326}
327EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
328
329/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300330 * Module use count
331 */
332
333/*
334 * media_entity_get - Get a reference to the parent module
335 * @entity: The entity
336 *
337 * Get a reference to the parent media device module.
338 *
339 * The function will return immediately if @entity is NULL.
340 *
341 * Return a pointer to the entity on success or NULL on failure.
342 */
343struct media_entity *media_entity_get(struct media_entity *entity)
344{
345 if (entity == NULL)
346 return NULL;
347
348 if (entity->parent->dev &&
349 !try_module_get(entity->parent->dev->driver->owner))
350 return NULL;
351
352 return entity;
353}
354EXPORT_SYMBOL_GPL(media_entity_get);
355
356/*
357 * media_entity_put - Release the reference to the parent module
358 * @entity: The entity
359 *
360 * Release the reference count acquired by media_entity_get().
361 *
362 * The function will return immediately if @entity is NULL.
363 */
364void media_entity_put(struct media_entity *entity)
365{
366 if (entity == NULL)
367 return;
368
369 if (entity->parent->dev)
370 module_put(entity->parent->dev->driver->owner);
371}
372EXPORT_SYMBOL_GPL(media_entity_put);
373
374/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300375 * Links management
376 */
377
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300378static struct media_link *media_entity_add_link(struct media_entity *entity)
379{
380 if (entity->num_links >= entity->max_links) {
381 struct media_link *links = entity->links;
382 unsigned int max_links = entity->max_links + 2;
383 unsigned int i;
384
385 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
386 if (links == NULL)
387 return NULL;
388
389 for (i = 0; i < entity->num_links; i++)
390 links[i].reverse->reverse = &links[i];
391
392 entity->max_links = max_links;
393 entity->links = links;
394 }
395
396 return &entity->links[entity->num_links++];
397}
398
399int
400media_entity_create_link(struct media_entity *source, u16 source_pad,
401 struct media_entity *sink, u16 sink_pad, u32 flags)
402{
403 struct media_link *link;
404 struct media_link *backlink;
405
406 BUG_ON(source == NULL || sink == NULL);
407 BUG_ON(source_pad >= source->num_pads);
408 BUG_ON(sink_pad >= sink->num_pads);
409
410 link = media_entity_add_link(source);
411 if (link == NULL)
412 return -ENOMEM;
413
414 link->source = &source->pads[source_pad];
415 link->sink = &sink->pads[sink_pad];
416 link->flags = flags;
417
418 /* Create the backlink. Backlinks are used to help graph traversal and
419 * are not reported to userspace.
420 */
421 backlink = media_entity_add_link(sink);
422 if (backlink == NULL) {
423 source->num_links--;
424 return -ENOMEM;
425 }
426
427 backlink->source = &source->pads[source_pad];
428 backlink->sink = &sink->pads[sink_pad];
429 backlink->flags = flags;
430
431 link->reverse = backlink;
432 backlink->reverse = link;
433
434 sink->num_backlinks++;
435
436 return 0;
437}
438EXPORT_SYMBOL_GPL(media_entity_create_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300439
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300440void __media_entity_remove_links(struct media_entity *entity)
441{
442 unsigned int i;
443
444 for (i = 0; i < entity->num_links; i++) {
445 struct media_link *link = &entity->links[i];
446 struct media_entity *remote;
447 unsigned int r = 0;
448
449 if (link->source->entity == entity)
450 remote = link->sink->entity;
451 else
452 remote = link->source->entity;
453
454 while (r < remote->num_links) {
455 struct media_link *rlink = &remote->links[r];
456
457 if (rlink != link->reverse) {
458 r++;
459 continue;
460 }
461
462 if (link->source->entity == entity)
463 remote->num_backlinks--;
464
465 if (--remote->num_links == 0)
466 break;
467
468 /* Insert last entry in place of the dropped link. */
469 *rlink = remote->links[remote->num_links];
470 }
471 }
472
473 entity->num_links = 0;
474 entity->num_backlinks = 0;
475}
476EXPORT_SYMBOL_GPL(__media_entity_remove_links);
477
478void media_entity_remove_links(struct media_entity *entity)
479{
480 /* Do nothing if the entity is not registered. */
481 if (entity->parent == NULL)
482 return;
483
484 mutex_lock(&entity->parent->graph_mutex);
485 __media_entity_remove_links(entity);
486 mutex_unlock(&entity->parent->graph_mutex);
487}
488EXPORT_SYMBOL_GPL(media_entity_remove_links);
489
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300490static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
491{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300492 int ret;
493
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)
498 return ret;
499
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);
505 return ret;
506 }
507
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300508 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300509 link->reverse->flags = link->flags;
510
511 return 0;
512}
513
514/**
515 * __media_entity_setup_link - Configure a media link
516 * @link: The link being configured
517 * @flags: Link configuration flags
518 *
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.
521 *
522 * If the link is immutable or if the current and new configuration are
523 * identical, return immediately.
524 *
525 * The user is expected to hold link->source->parent->mutex. If not,
526 * media_entity_setup_link() should be used instead.
527 */
528int __media_entity_setup_link(struct media_link *link, u32 flags)
529{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300530 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300531 struct media_device *mdev;
532 struct media_entity *source, *sink;
533 int ret = -EBUSY;
534
535 if (link == NULL)
536 return -EINVAL;
537
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300538 /* The non-modifiable link flags must not be modified. */
539 if ((link->flags & ~mask) != (flags & ~mask))
540 return -EINVAL;
541
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300542 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
543 return link->flags == flags ? 0 : -EINVAL;
544
545 if (link->flags == flags)
546 return 0;
547
548 source = link->source->entity;
549 sink = link->sink->entity;
550
Laurent Pincharte02188c2010-08-25 09:00:41 -0300551 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
552 (source->stream_count || sink->stream_count))
553 return -EBUSY;
554
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300555 mdev = source->parent;
556
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300557 if (mdev->link_notify) {
558 ret = mdev->link_notify(link, flags,
559 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300560 if (ret < 0)
561 return ret;
562 }
563
564 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300565
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300566 if (mdev->link_notify)
567 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300568
569 return ret;
570}
571
572int media_entity_setup_link(struct media_link *link, u32 flags)
573{
574 int ret;
575
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);
579
580 return ret;
581}
582EXPORT_SYMBOL_GPL(media_entity_setup_link);
583
584/**
585 * media_entity_find_link - Find a link between two pads
586 * @source: Source pad
587 * @sink: Sink pad
588 *
589 * Return a pointer to the link between the two entities. If no such link
590 * exists, return NULL.
591 */
592struct media_link *
593media_entity_find_link(struct media_pad *source, struct media_pad *sink)
594{
595 struct media_link *link;
596 unsigned int i;
597
598 for (i = 0; i < source->entity->num_links; ++i) {
599 link = &source->entity->links[i];
600
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)
605 return link;
606 }
607
608 return NULL;
609}
610EXPORT_SYMBOL_GPL(media_entity_find_link);
611
612/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300613 * media_entity_remote_pad - Find the pad at the remote end of a link
614 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300615 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300616 * 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.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300618 *
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.
621 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300622struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300623{
624 unsigned int i;
625
626 for (i = 0; i < pad->entity->num_links; i++) {
627 struct media_link *link = &pad->entity->links[i];
628
629 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
630 continue;
631
632 if (link->source == pad)
633 return link->sink;
634
635 if (link->sink == pad)
636 return link->source;
637 }
638
639 return NULL;
640
641}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300642EXPORT_SYMBOL_GPL(media_entity_remote_pad);