blob: 63fd293a3fb884859516c7520e36741a2d5f2ef3 [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/**
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030030 * media_gobj_init - Initialize a graph object
31 *
32 * @mdev: Pointer to the media_device that contains the object
33 * @type: Type of the object
34 * @gobj: Pointer to the object
35 *
36 * This routine initializes the embedded struct media_gobj inside a
37 * media graph object. It is called automatically if media_*_create()
38 * calls are used. However, if the object (entity, link, pad, interface)
39 * is embedded on some other object, this function should be called before
40 * registering the object at the media controller.
41 */
42void media_gobj_init(struct media_device *mdev,
43 enum media_gobj_type type,
44 struct media_gobj *gobj)
45{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030046 /* Create a per-type unique object ID */
47 switch (type) {
48 case MEDIA_GRAPH_ENTITY:
49 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
50 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030051 case MEDIA_GRAPH_PAD:
52 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
53 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030054 case MEDIA_GRAPH_LINK:
55 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
56 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030057 }
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030058}
59
60/**
61 * media_gobj_remove - Stop using a graph object on a media device
62 *
63 * @graph_obj: Pointer to the object
64 *
65 * This should be called at media_device_unregister_*() routines
66 */
67void media_gobj_remove(struct media_gobj *gobj)
68{
69 /* For now, nothing to do */
70}
71
72/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -030073 * media_entity_init - Initialize a media entity
74 *
75 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030076 * @pads: Array of 'num_pads' pads.
77 *
78 * The total number of pads is an intrinsic property of entities known by the
79 * entity driver, while the total number of links depends on hardware design
80 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030081 * use cases the number of links can safely be assumed to be equal to or
82 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030083 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030084 * For those reasons the links array can be preallocated based on the number
85 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030086 *
87 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030088 * 'num_pads' elements. The media_entity::max_links field will be set to the
89 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030090 *
91 * The pads array is managed by the entity driver and passed to
92 * media_entity_init() where its pointer will be stored in the entity structure.
93 */
94int
95media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030096 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -030097{
98 struct media_link *links;
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030099 unsigned int max_links = num_pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300100 unsigned int i;
101
102 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
103 if (links == NULL)
104 return -ENOMEM;
105
106 entity->group_id = 0;
107 entity->max_links = max_links;
108 entity->num_links = 0;
109 entity->num_backlinks = 0;
110 entity->num_pads = num_pads;
111 entity->pads = pads;
112 entity->links = links;
113
114 for (i = 0; i < num_pads; i++) {
115 pads[i].entity = entity;
116 pads[i].index = i;
117 }
118
119 return 0;
120}
121EXPORT_SYMBOL_GPL(media_entity_init);
122
123void
124media_entity_cleanup(struct media_entity *entity)
125{
126 kfree(entity->links);
127}
128EXPORT_SYMBOL_GPL(media_entity_cleanup);
129
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300130/* -----------------------------------------------------------------------------
131 * Graph traversal
132 */
133
134static struct media_entity *
135media_entity_other(struct media_entity *entity, struct media_link *link)
136{
137 if (link->source->entity == entity)
138 return link->sink->entity;
139 else
140 return link->source->entity;
141}
142
143/* push an entity to traversal stack */
144static void stack_push(struct media_entity_graph *graph,
145 struct media_entity *entity)
146{
147 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
148 WARN_ON(1);
149 return;
150 }
151 graph->top++;
152 graph->stack[graph->top].link = 0;
153 graph->stack[graph->top].entity = entity;
154}
155
156static struct media_entity *stack_pop(struct media_entity_graph *graph)
157{
158 struct media_entity *entity;
159
160 entity = graph->stack[graph->top].entity;
161 graph->top--;
162
163 return entity;
164}
165
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300166#define link_top(en) ((en)->stack[(en)->top].link)
167#define stack_top(en) ((en)->stack[(en)->top].entity)
168
169/**
170 * media_entity_graph_walk_start - Start walking the media graph at a given entity
171 * @graph: Media graph structure that will be used to walk the graph
172 * @entity: Starting entity
173 *
174 * This function initializes the graph traversal structure to walk the entities
175 * graph starting at the given entity. The traversal structure must not be
176 * modified by the caller during graph traversal. When done the structure can
177 * safely be freed.
178 */
179void media_entity_graph_walk_start(struct media_entity_graph *graph,
180 struct media_entity *entity)
181{
182 graph->top = 0;
183 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300184 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
185
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300186 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300187 return;
188
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300189 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300190 stack_push(graph, entity);
191}
192EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
193
194/**
195 * media_entity_graph_walk_next - Get the next entity in the graph
196 * @graph: Media graph structure
197 *
198 * Perform a depth-first traversal of the given media entities graph.
199 *
200 * The graph structure must have been previously initialized with a call to
201 * media_entity_graph_walk_start().
202 *
203 * Return the next entity in the graph or NULL if the whole graph have been
204 * traversed.
205 */
206struct media_entity *
207media_entity_graph_walk_next(struct media_entity_graph *graph)
208{
209 if (stack_top(graph) == NULL)
210 return NULL;
211
212 /*
213 * Depth first search. Push entity to stack and continue from
214 * top of the stack until no more entities on the level can be
215 * found.
216 */
217 while (link_top(graph) < stack_top(graph)->num_links) {
218 struct media_entity *entity = stack_top(graph);
219 struct media_link *link = &entity->links[link_top(graph)];
220 struct media_entity *next;
221
222 /* The link is not enabled so we do not follow. */
223 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
224 link_top(graph)++;
225 continue;
226 }
227
228 /* Get the entity in the other end of the link . */
229 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300230 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300231 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300232
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300233 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300234 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300235 link_top(graph)++;
236 continue;
237 }
238
239 /* Push the new entity to stack and start over. */
240 link_top(graph)++;
241 stack_push(graph, next);
242 }
243
244 return stack_pop(graph);
245}
246EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
247
248/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300249 * Pipeline management
250 */
251
252/**
253 * media_entity_pipeline_start - Mark a pipeline as streaming
254 * @entity: Starting entity
255 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
256 *
257 * Mark all entities connected to a given entity through enabled links, either
258 * directly or indirectly, as streaming. The given pipeline object is assigned to
259 * every entity in the pipeline and stored in the media_entity pipe field.
260 *
261 * Calls to this function can be nested, in which case the same number of
262 * media_entity_pipeline_stop() calls will be required to stop streaming. The
263 * pipeline pointer must be identical for all nested calls to
264 * media_entity_pipeline_start().
265 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300266__must_check int media_entity_pipeline_start(struct media_entity *entity,
267 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300268{
269 struct media_device *mdev = entity->parent;
270 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300271 struct media_entity *entity_err = entity;
272 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300273
274 mutex_lock(&mdev->graph_mutex);
275
276 media_entity_graph_walk_start(&graph, entity);
277
278 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300279 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
280 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300281 unsigned int i;
282
Laurent Pincharte02188c2010-08-25 09:00:41 -0300283 entity->stream_count++;
284 WARN_ON(entity->pipe && entity->pipe != pipe);
285 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300286
287 /* Already streaming --- no need to check. */
288 if (entity->stream_count > 1)
289 continue;
290
291 if (!entity->ops || !entity->ops->link_validate)
292 continue;
293
Sakari Ailusde49c282013-10-13 08:00:26 -0300294 bitmap_zero(active, entity->num_pads);
295 bitmap_fill(has_no_links, entity->num_pads);
296
Sakari Ailusaf88be32012-01-11 06:25:15 -0300297 for (i = 0; i < entity->num_links; i++) {
298 struct media_link *link = &entity->links[i];
Sakari Ailusde49c282013-10-13 08:00:26 -0300299 struct media_pad *pad = link->sink->entity == entity
300 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300301
Sakari Ailusde49c282013-10-13 08:00:26 -0300302 /* Mark that a pad is connected by a link. */
303 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300304
Sakari Ailusde49c282013-10-13 08:00:26 -0300305 /*
306 * Pads that either do not need to connect or
307 * are connected through an enabled link are
308 * fine.
309 */
310 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
311 link->flags & MEDIA_LNK_FL_ENABLED)
312 bitmap_set(active, pad->index, 1);
313
314 /*
315 * Link validation will only take place for
316 * sink ends of the link that are enabled.
317 */
318 if (link->sink != pad ||
319 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300320 continue;
321
322 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300323 if (ret < 0 && ret != -ENOIOCTLCMD) {
324 dev_dbg(entity->parent->dev,
325 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200326 link->source->entity->name,
327 link->source->index,
328 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300329 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300330 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300331 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300332
333 /* Either no links or validated links are fine. */
334 bitmap_or(active, active, has_no_links, entity->num_pads);
335
336 if (!bitmap_full(active, entity->num_pads)) {
337 ret = -EPIPE;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300338 dev_dbg(entity->parent->dev,
339 "\"%s\":%u must be connected by an enabled link\n",
340 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300341 (unsigned)find_first_zero_bit(
342 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300343 goto error;
344 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300345 }
346
347 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300348
349 return 0;
350
351error:
352 /*
353 * Link validation on graph failed. We revert what we did and
354 * return the error.
355 */
356 media_entity_graph_walk_start(&graph, entity_err);
357
358 while ((entity_err = media_entity_graph_walk_next(&graph))) {
359 entity_err->stream_count--;
360 if (entity_err->stream_count == 0)
361 entity_err->pipe = NULL;
362
363 /*
364 * We haven't increased stream_count further than this
365 * so we quit here.
366 */
367 if (entity_err == entity)
368 break;
369 }
370
371 mutex_unlock(&mdev->graph_mutex);
372
373 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300374}
375EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
376
377/**
378 * media_entity_pipeline_stop - Mark a pipeline as not streaming
379 * @entity: Starting entity
380 *
381 * Mark all entities connected to a given entity through enabled links, either
382 * directly or indirectly, as not streaming. The media_entity pipe field is
383 * reset to NULL.
384 *
385 * If multiple calls to media_entity_pipeline_start() have been made, the same
386 * number of calls to this function are required to mark the pipeline as not
387 * streaming.
388 */
389void media_entity_pipeline_stop(struct media_entity *entity)
390{
391 struct media_device *mdev = entity->parent;
392 struct media_entity_graph graph;
393
394 mutex_lock(&mdev->graph_mutex);
395
396 media_entity_graph_walk_start(&graph, entity);
397
398 while ((entity = media_entity_graph_walk_next(&graph))) {
399 entity->stream_count--;
400 if (entity->stream_count == 0)
401 entity->pipe = NULL;
402 }
403
404 mutex_unlock(&mdev->graph_mutex);
405}
406EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
407
408/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300409 * Module use count
410 */
411
412/*
413 * media_entity_get - Get a reference to the parent module
414 * @entity: The entity
415 *
416 * Get a reference to the parent media device module.
417 *
418 * The function will return immediately if @entity is NULL.
419 *
420 * Return a pointer to the entity on success or NULL on failure.
421 */
422struct media_entity *media_entity_get(struct media_entity *entity)
423{
424 if (entity == NULL)
425 return NULL;
426
427 if (entity->parent->dev &&
428 !try_module_get(entity->parent->dev->driver->owner))
429 return NULL;
430
431 return entity;
432}
433EXPORT_SYMBOL_GPL(media_entity_get);
434
435/*
436 * media_entity_put - Release the reference to the parent module
437 * @entity: The entity
438 *
439 * Release the reference count acquired by media_entity_get().
440 *
441 * The function will return immediately if @entity is NULL.
442 */
443void media_entity_put(struct media_entity *entity)
444{
445 if (entity == NULL)
446 return;
447
448 if (entity->parent->dev)
449 module_put(entity->parent->dev->driver->owner);
450}
451EXPORT_SYMBOL_GPL(media_entity_put);
452
453/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300454 * Links management
455 */
456
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300457static struct media_link *media_entity_add_link(struct media_entity *entity)
458{
459 if (entity->num_links >= entity->max_links) {
460 struct media_link *links = entity->links;
461 unsigned int max_links = entity->max_links + 2;
462 unsigned int i;
463
464 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
465 if (links == NULL)
466 return NULL;
467
468 for (i = 0; i < entity->num_links; i++)
469 links[i].reverse->reverse = &links[i];
470
471 entity->max_links = max_links;
472 entity->links = links;
473 }
474
475 return &entity->links[entity->num_links++];
476}
477
478int
479media_entity_create_link(struct media_entity *source, u16 source_pad,
480 struct media_entity *sink, u16 sink_pad, u32 flags)
481{
482 struct media_link *link;
483 struct media_link *backlink;
484
485 BUG_ON(source == NULL || sink == NULL);
486 BUG_ON(source_pad >= source->num_pads);
487 BUG_ON(sink_pad >= sink->num_pads);
488
489 link = media_entity_add_link(source);
490 if (link == NULL)
491 return -ENOMEM;
492
493 link->source = &source->pads[source_pad];
494 link->sink = &sink->pads[sink_pad];
495 link->flags = flags;
496
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300497 /* Initialize graph object embedded at the new link */
498 media_gobj_init(source->parent, MEDIA_GRAPH_LINK, &link->graph_obj);
499
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300500 /* Create the backlink. Backlinks are used to help graph traversal and
501 * are not reported to userspace.
502 */
503 backlink = media_entity_add_link(sink);
504 if (backlink == NULL) {
505 source->num_links--;
506 return -ENOMEM;
507 }
508
509 backlink->source = &source->pads[source_pad];
510 backlink->sink = &sink->pads[sink_pad];
511 backlink->flags = flags;
512
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300513 /* Initialize graph object embedded at the new link */
514 media_gobj_init(sink->parent, MEDIA_GRAPH_LINK, &backlink->graph_obj);
515
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300516 link->reverse = backlink;
517 backlink->reverse = link;
518
519 sink->num_backlinks++;
520
521 return 0;
522}
523EXPORT_SYMBOL_GPL(media_entity_create_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300524
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300525void __media_entity_remove_links(struct media_entity *entity)
526{
527 unsigned int i;
528
529 for (i = 0; i < entity->num_links; i++) {
530 struct media_link *link = &entity->links[i];
531 struct media_entity *remote;
532 unsigned int r = 0;
533
534 if (link->source->entity == entity)
535 remote = link->sink->entity;
536 else
537 remote = link->source->entity;
538
539 while (r < remote->num_links) {
540 struct media_link *rlink = &remote->links[r];
541
542 if (rlink != link->reverse) {
543 r++;
544 continue;
545 }
546
547 if (link->source->entity == entity)
548 remote->num_backlinks--;
549
550 if (--remote->num_links == 0)
551 break;
552
553 /* Insert last entry in place of the dropped link. */
554 *rlink = remote->links[remote->num_links];
555 }
556 }
557
558 entity->num_links = 0;
559 entity->num_backlinks = 0;
560}
561EXPORT_SYMBOL_GPL(__media_entity_remove_links);
562
563void media_entity_remove_links(struct media_entity *entity)
564{
565 /* Do nothing if the entity is not registered. */
566 if (entity->parent == NULL)
567 return;
568
569 mutex_lock(&entity->parent->graph_mutex);
570 __media_entity_remove_links(entity);
571 mutex_unlock(&entity->parent->graph_mutex);
572}
573EXPORT_SYMBOL_GPL(media_entity_remove_links);
574
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300575static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
576{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300577 int ret;
578
579 /* Notify both entities. */
580 ret = media_entity_call(link->source->entity, link_setup,
581 link->source, link->sink, flags);
582 if (ret < 0 && ret != -ENOIOCTLCMD)
583 return ret;
584
585 ret = media_entity_call(link->sink->entity, link_setup,
586 link->sink, link->source, flags);
587 if (ret < 0 && ret != -ENOIOCTLCMD) {
588 media_entity_call(link->source->entity, link_setup,
589 link->source, link->sink, link->flags);
590 return ret;
591 }
592
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300593 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300594 link->reverse->flags = link->flags;
595
596 return 0;
597}
598
599/**
600 * __media_entity_setup_link - Configure a media link
601 * @link: The link being configured
602 * @flags: Link configuration flags
603 *
604 * The bulk of link setup is handled by the two entities connected through the
605 * link. This function notifies both entities of the link configuration change.
606 *
607 * If the link is immutable or if the current and new configuration are
608 * identical, return immediately.
609 *
610 * The user is expected to hold link->source->parent->mutex. If not,
611 * media_entity_setup_link() should be used instead.
612 */
613int __media_entity_setup_link(struct media_link *link, u32 flags)
614{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300615 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300616 struct media_device *mdev;
617 struct media_entity *source, *sink;
618 int ret = -EBUSY;
619
620 if (link == NULL)
621 return -EINVAL;
622
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300623 /* The non-modifiable link flags must not be modified. */
624 if ((link->flags & ~mask) != (flags & ~mask))
625 return -EINVAL;
626
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300627 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
628 return link->flags == flags ? 0 : -EINVAL;
629
630 if (link->flags == flags)
631 return 0;
632
633 source = link->source->entity;
634 sink = link->sink->entity;
635
Laurent Pincharte02188c2010-08-25 09:00:41 -0300636 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
637 (source->stream_count || sink->stream_count))
638 return -EBUSY;
639
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300640 mdev = source->parent;
641
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300642 if (mdev->link_notify) {
643 ret = mdev->link_notify(link, flags,
644 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300645 if (ret < 0)
646 return ret;
647 }
648
649 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300650
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300651 if (mdev->link_notify)
652 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300653
654 return ret;
655}
656
657int media_entity_setup_link(struct media_link *link, u32 flags)
658{
659 int ret;
660
661 mutex_lock(&link->source->entity->parent->graph_mutex);
662 ret = __media_entity_setup_link(link, flags);
663 mutex_unlock(&link->source->entity->parent->graph_mutex);
664
665 return ret;
666}
667EXPORT_SYMBOL_GPL(media_entity_setup_link);
668
669/**
670 * media_entity_find_link - Find a link between two pads
671 * @source: Source pad
672 * @sink: Sink pad
673 *
674 * Return a pointer to the link between the two entities. If no such link
675 * exists, return NULL.
676 */
677struct media_link *
678media_entity_find_link(struct media_pad *source, struct media_pad *sink)
679{
680 struct media_link *link;
681 unsigned int i;
682
683 for (i = 0; i < source->entity->num_links; ++i) {
684 link = &source->entity->links[i];
685
686 if (link->source->entity == source->entity &&
687 link->source->index == source->index &&
688 link->sink->entity == sink->entity &&
689 link->sink->index == sink->index)
690 return link;
691 }
692
693 return NULL;
694}
695EXPORT_SYMBOL_GPL(media_entity_find_link);
696
697/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300698 * media_entity_remote_pad - Find the pad at the remote end of a link
699 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300700 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300701 * Search for a remote pad connected to the given pad by iterating over all
702 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300703 *
704 * Return a pointer to the pad at the remote end of the first found enabled
705 * link, or NULL if no enabled link has been found.
706 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300707struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300708{
709 unsigned int i;
710
711 for (i = 0; i < pad->entity->num_links; i++) {
712 struct media_link *link = &pad->entity->links[i];
713
714 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
715 continue;
716
717 if (link->source == pad)
718 return link->sink;
719
720 if (link->sink == pad)
721 return link->source;
722 }
723
724 return NULL;
725
726}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300727EXPORT_SYMBOL_GPL(media_entity_remote_pad);