blob: eabcbacfe387c577af8b6677345613b4e95ce8dc [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.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030033 * @pads: Array of 'num_pads' pads.
34 *
35 * The total number of pads is an intrinsic property of entities known by the
36 * entity driver, while the total number of links depends on hardware design
37 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030038 * use cases the number of links can safely be assumed to be equal to or
39 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030040 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030041 * For those reasons the links array can be preallocated based on the number
42 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030043 *
44 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030045 * 'num_pads' elements. The media_entity::max_links field will be set to the
46 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030047 *
48 * The pads array is managed by the entity driver and passed to
49 * media_entity_init() where its pointer will be stored in the entity structure.
50 */
51int
52media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030053 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -030054{
55 struct media_link *links;
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030056 unsigned int max_links = num_pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -030057 unsigned int i;
58
59 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
60 if (links == NULL)
61 return -ENOMEM;
62
63 entity->group_id = 0;
64 entity->max_links = max_links;
65 entity->num_links = 0;
66 entity->num_backlinks = 0;
67 entity->num_pads = num_pads;
68 entity->pads = pads;
69 entity->links = links;
70
71 for (i = 0; i < num_pads; i++) {
72 pads[i].entity = entity;
73 pads[i].index = i;
74 }
75
76 return 0;
77}
78EXPORT_SYMBOL_GPL(media_entity_init);
79
80void
81media_entity_cleanup(struct media_entity *entity)
82{
83 kfree(entity->links);
84}
85EXPORT_SYMBOL_GPL(media_entity_cleanup);
86
Sakari Ailusa5ccc482010-03-07 16:14:14 -030087/* -----------------------------------------------------------------------------
88 * Graph traversal
89 */
90
91static struct media_entity *
92media_entity_other(struct media_entity *entity, struct media_link *link)
93{
94 if (link->source->entity == entity)
95 return link->sink->entity;
96 else
97 return link->source->entity;
98}
99
100/* push an entity to traversal stack */
101static void stack_push(struct media_entity_graph *graph,
102 struct media_entity *entity)
103{
104 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
105 WARN_ON(1);
106 return;
107 }
108 graph->top++;
109 graph->stack[graph->top].link = 0;
110 graph->stack[graph->top].entity = entity;
111}
112
113static struct media_entity *stack_pop(struct media_entity_graph *graph)
114{
115 struct media_entity *entity;
116
117 entity = graph->stack[graph->top].entity;
118 graph->top--;
119
120 return entity;
121}
122
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300123#define link_top(en) ((en)->stack[(en)->top].link)
124#define stack_top(en) ((en)->stack[(en)->top].entity)
125
126/**
127 * media_entity_graph_walk_start - Start walking the media graph at a given entity
128 * @graph: Media graph structure that will be used to walk the graph
129 * @entity: Starting entity
130 *
131 * This function initializes the graph traversal structure to walk the entities
132 * graph starting at the given entity. The traversal structure must not be
133 * modified by the caller during graph traversal. When done the structure can
134 * safely be freed.
135 */
136void media_entity_graph_walk_start(struct media_entity_graph *graph,
137 struct media_entity *entity)
138{
139 graph->top = 0;
140 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300141 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
142
143 if (WARN_ON(entity->id >= MEDIA_ENTITY_ENUM_MAX_ID))
144 return;
145
146 __set_bit(entity->id, graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300147 stack_push(graph, entity);
148}
149EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
150
151/**
152 * media_entity_graph_walk_next - Get the next entity in the graph
153 * @graph: Media graph structure
154 *
155 * Perform a depth-first traversal of the given media entities graph.
156 *
157 * The graph structure must have been previously initialized with a call to
158 * media_entity_graph_walk_start().
159 *
160 * Return the next entity in the graph or NULL if the whole graph have been
161 * traversed.
162 */
163struct media_entity *
164media_entity_graph_walk_next(struct media_entity_graph *graph)
165{
166 if (stack_top(graph) == NULL)
167 return NULL;
168
169 /*
170 * Depth first search. Push entity to stack and continue from
171 * top of the stack until no more entities on the level can be
172 * found.
173 */
174 while (link_top(graph) < stack_top(graph)->num_links) {
175 struct media_entity *entity = stack_top(graph);
176 struct media_link *link = &entity->links[link_top(graph)];
177 struct media_entity *next;
178
179 /* The link is not enabled so we do not follow. */
180 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
181 link_top(graph)++;
182 continue;
183 }
184
185 /* Get the entity in the other end of the link . */
186 next = media_entity_other(entity, link);
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300187 if (WARN_ON(next->id >= MEDIA_ENTITY_ENUM_MAX_ID))
188 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300189
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300190 /* Has the entity already been visited? */
191 if (__test_and_set_bit(next->id, graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300192 link_top(graph)++;
193 continue;
194 }
195
196 /* Push the new entity to stack and start over. */
197 link_top(graph)++;
198 stack_push(graph, next);
199 }
200
201 return stack_pop(graph);
202}
203EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
204
205/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300206 * Pipeline management
207 */
208
209/**
210 * media_entity_pipeline_start - Mark a pipeline as streaming
211 * @entity: Starting entity
212 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
213 *
214 * Mark all entities connected to a given entity through enabled links, either
215 * directly or indirectly, as streaming. The given pipeline object is assigned to
216 * every entity in the pipeline and stored in the media_entity pipe field.
217 *
218 * Calls to this function can be nested, in which case the same number of
219 * media_entity_pipeline_stop() calls will be required to stop streaming. The
220 * pipeline pointer must be identical for all nested calls to
221 * media_entity_pipeline_start().
222 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300223__must_check int media_entity_pipeline_start(struct media_entity *entity,
224 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300225{
226 struct media_device *mdev = entity->parent;
227 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300228 struct media_entity *entity_err = entity;
229 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300230
231 mutex_lock(&mdev->graph_mutex);
232
233 media_entity_graph_walk_start(&graph, entity);
234
235 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300236 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
237 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
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
Sakari Ailusde49c282013-10-13 08:00:26 -0300251 bitmap_zero(active, entity->num_pads);
252 bitmap_fill(has_no_links, entity->num_pads);
253
Sakari Ailusaf88be32012-01-11 06:25:15 -0300254 for (i = 0; i < entity->num_links; i++) {
255 struct media_link *link = &entity->links[i];
Sakari Ailusde49c282013-10-13 08:00:26 -0300256 struct media_pad *pad = link->sink->entity == entity
257 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300258
Sakari Ailusde49c282013-10-13 08:00:26 -0300259 /* Mark that a pad is connected by a link. */
260 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300261
Sakari Ailusde49c282013-10-13 08:00:26 -0300262 /*
263 * Pads that either do not need to connect or
264 * are connected through an enabled link are
265 * fine.
266 */
267 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
268 link->flags & MEDIA_LNK_FL_ENABLED)
269 bitmap_set(active, pad->index, 1);
270
271 /*
272 * Link validation will only take place for
273 * sink ends of the link that are enabled.
274 */
275 if (link->sink != pad ||
276 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300277 continue;
278
279 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300280 if (ret < 0 && ret != -ENOIOCTLCMD) {
281 dev_dbg(entity->parent->dev,
282 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200283 link->source->entity->name,
284 link->source->index,
285 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300286 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300287 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300288 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300289
290 /* Either no links or validated links are fine. */
291 bitmap_or(active, active, has_no_links, entity->num_pads);
292
293 if (!bitmap_full(active, entity->num_pads)) {
294 ret = -EPIPE;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300295 dev_dbg(entity->parent->dev,
296 "\"%s\":%u must be connected by an enabled link\n",
297 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300298 (unsigned)find_first_zero_bit(
299 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300300 goto error;
301 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300302 }
303
304 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300305
306 return 0;
307
308error:
309 /*
310 * Link validation on graph failed. We revert what we did and
311 * return the error.
312 */
313 media_entity_graph_walk_start(&graph, entity_err);
314
315 while ((entity_err = media_entity_graph_walk_next(&graph))) {
316 entity_err->stream_count--;
317 if (entity_err->stream_count == 0)
318 entity_err->pipe = NULL;
319
320 /*
321 * We haven't increased stream_count further than this
322 * so we quit here.
323 */
324 if (entity_err == entity)
325 break;
326 }
327
328 mutex_unlock(&mdev->graph_mutex);
329
330 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300331}
332EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
333
334/**
335 * media_entity_pipeline_stop - Mark a pipeline as not streaming
336 * @entity: Starting entity
337 *
338 * Mark all entities connected to a given entity through enabled links, either
339 * directly or indirectly, as not streaming. The media_entity pipe field is
340 * reset to NULL.
341 *
342 * If multiple calls to media_entity_pipeline_start() have been made, the same
343 * number of calls to this function are required to mark the pipeline as not
344 * streaming.
345 */
346void media_entity_pipeline_stop(struct media_entity *entity)
347{
348 struct media_device *mdev = entity->parent;
349 struct media_entity_graph graph;
350
351 mutex_lock(&mdev->graph_mutex);
352
353 media_entity_graph_walk_start(&graph, entity);
354
355 while ((entity = media_entity_graph_walk_next(&graph))) {
356 entity->stream_count--;
357 if (entity->stream_count == 0)
358 entity->pipe = NULL;
359 }
360
361 mutex_unlock(&mdev->graph_mutex);
362}
363EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
364
365/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300366 * Module use count
367 */
368
369/*
370 * media_entity_get - Get a reference to the parent module
371 * @entity: The entity
372 *
373 * Get a reference to the parent media device module.
374 *
375 * The function will return immediately if @entity is NULL.
376 *
377 * Return a pointer to the entity on success or NULL on failure.
378 */
379struct media_entity *media_entity_get(struct media_entity *entity)
380{
381 if (entity == NULL)
382 return NULL;
383
384 if (entity->parent->dev &&
385 !try_module_get(entity->parent->dev->driver->owner))
386 return NULL;
387
388 return entity;
389}
390EXPORT_SYMBOL_GPL(media_entity_get);
391
392/*
393 * media_entity_put - Release the reference to the parent module
394 * @entity: The entity
395 *
396 * Release the reference count acquired by media_entity_get().
397 *
398 * The function will return immediately if @entity is NULL.
399 */
400void media_entity_put(struct media_entity *entity)
401{
402 if (entity == NULL)
403 return;
404
405 if (entity->parent->dev)
406 module_put(entity->parent->dev->driver->owner);
407}
408EXPORT_SYMBOL_GPL(media_entity_put);
409
410/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300411 * Links management
412 */
413
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300414static struct media_link *media_entity_add_link(struct media_entity *entity)
415{
416 if (entity->num_links >= entity->max_links) {
417 struct media_link *links = entity->links;
418 unsigned int max_links = entity->max_links + 2;
419 unsigned int i;
420
421 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
422 if (links == NULL)
423 return NULL;
424
425 for (i = 0; i < entity->num_links; i++)
426 links[i].reverse->reverse = &links[i];
427
428 entity->max_links = max_links;
429 entity->links = links;
430 }
431
432 return &entity->links[entity->num_links++];
433}
434
435int
436media_entity_create_link(struct media_entity *source, u16 source_pad,
437 struct media_entity *sink, u16 sink_pad, u32 flags)
438{
439 struct media_link *link;
440 struct media_link *backlink;
441
442 BUG_ON(source == NULL || sink == NULL);
443 BUG_ON(source_pad >= source->num_pads);
444 BUG_ON(sink_pad >= sink->num_pads);
445
446 link = media_entity_add_link(source);
447 if (link == NULL)
448 return -ENOMEM;
449
450 link->source = &source->pads[source_pad];
451 link->sink = &sink->pads[sink_pad];
452 link->flags = flags;
453
454 /* Create the backlink. Backlinks are used to help graph traversal and
455 * are not reported to userspace.
456 */
457 backlink = media_entity_add_link(sink);
458 if (backlink == NULL) {
459 source->num_links--;
460 return -ENOMEM;
461 }
462
463 backlink->source = &source->pads[source_pad];
464 backlink->sink = &sink->pads[sink_pad];
465 backlink->flags = flags;
466
467 link->reverse = backlink;
468 backlink->reverse = link;
469
470 sink->num_backlinks++;
471
472 return 0;
473}
474EXPORT_SYMBOL_GPL(media_entity_create_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300475
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300476void __media_entity_remove_links(struct media_entity *entity)
477{
478 unsigned int i;
479
480 for (i = 0; i < entity->num_links; i++) {
481 struct media_link *link = &entity->links[i];
482 struct media_entity *remote;
483 unsigned int r = 0;
484
485 if (link->source->entity == entity)
486 remote = link->sink->entity;
487 else
488 remote = link->source->entity;
489
490 while (r < remote->num_links) {
491 struct media_link *rlink = &remote->links[r];
492
493 if (rlink != link->reverse) {
494 r++;
495 continue;
496 }
497
498 if (link->source->entity == entity)
499 remote->num_backlinks--;
500
501 if (--remote->num_links == 0)
502 break;
503
504 /* Insert last entry in place of the dropped link. */
505 *rlink = remote->links[remote->num_links];
506 }
507 }
508
509 entity->num_links = 0;
510 entity->num_backlinks = 0;
511}
512EXPORT_SYMBOL_GPL(__media_entity_remove_links);
513
514void media_entity_remove_links(struct media_entity *entity)
515{
516 /* Do nothing if the entity is not registered. */
517 if (entity->parent == NULL)
518 return;
519
520 mutex_lock(&entity->parent->graph_mutex);
521 __media_entity_remove_links(entity);
522 mutex_unlock(&entity->parent->graph_mutex);
523}
524EXPORT_SYMBOL_GPL(media_entity_remove_links);
525
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300526static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
527{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300528 int ret;
529
530 /* Notify both entities. */
531 ret = media_entity_call(link->source->entity, link_setup,
532 link->source, link->sink, flags);
533 if (ret < 0 && ret != -ENOIOCTLCMD)
534 return ret;
535
536 ret = media_entity_call(link->sink->entity, link_setup,
537 link->sink, link->source, flags);
538 if (ret < 0 && ret != -ENOIOCTLCMD) {
539 media_entity_call(link->source->entity, link_setup,
540 link->source, link->sink, link->flags);
541 return ret;
542 }
543
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300544 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300545 link->reverse->flags = link->flags;
546
547 return 0;
548}
549
550/**
551 * __media_entity_setup_link - Configure a media link
552 * @link: The link being configured
553 * @flags: Link configuration flags
554 *
555 * The bulk of link setup is handled by the two entities connected through the
556 * link. This function notifies both entities of the link configuration change.
557 *
558 * If the link is immutable or if the current and new configuration are
559 * identical, return immediately.
560 *
561 * The user is expected to hold link->source->parent->mutex. If not,
562 * media_entity_setup_link() should be used instead.
563 */
564int __media_entity_setup_link(struct media_link *link, u32 flags)
565{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300566 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300567 struct media_device *mdev;
568 struct media_entity *source, *sink;
569 int ret = -EBUSY;
570
571 if (link == NULL)
572 return -EINVAL;
573
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300574 /* The non-modifiable link flags must not be modified. */
575 if ((link->flags & ~mask) != (flags & ~mask))
576 return -EINVAL;
577
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300578 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
579 return link->flags == flags ? 0 : -EINVAL;
580
581 if (link->flags == flags)
582 return 0;
583
584 source = link->source->entity;
585 sink = link->sink->entity;
586
Laurent Pincharte02188c2010-08-25 09:00:41 -0300587 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
588 (source->stream_count || sink->stream_count))
589 return -EBUSY;
590
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300591 mdev = source->parent;
592
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300593 if (mdev->link_notify) {
594 ret = mdev->link_notify(link, flags,
595 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300596 if (ret < 0)
597 return ret;
598 }
599
600 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300601
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300602 if (mdev->link_notify)
603 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300604
605 return ret;
606}
607
608int media_entity_setup_link(struct media_link *link, u32 flags)
609{
610 int ret;
611
612 mutex_lock(&link->source->entity->parent->graph_mutex);
613 ret = __media_entity_setup_link(link, flags);
614 mutex_unlock(&link->source->entity->parent->graph_mutex);
615
616 return ret;
617}
618EXPORT_SYMBOL_GPL(media_entity_setup_link);
619
620/**
621 * media_entity_find_link - Find a link between two pads
622 * @source: Source pad
623 * @sink: Sink pad
624 *
625 * Return a pointer to the link between the two entities. If no such link
626 * exists, return NULL.
627 */
628struct media_link *
629media_entity_find_link(struct media_pad *source, struct media_pad *sink)
630{
631 struct media_link *link;
632 unsigned int i;
633
634 for (i = 0; i < source->entity->num_links; ++i) {
635 link = &source->entity->links[i];
636
637 if (link->source->entity == source->entity &&
638 link->source->index == source->index &&
639 link->sink->entity == sink->entity &&
640 link->sink->index == sink->index)
641 return link;
642 }
643
644 return NULL;
645}
646EXPORT_SYMBOL_GPL(media_entity_find_link);
647
648/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300649 * media_entity_remote_pad - Find the pad at the remote end of a link
650 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300651 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300652 * Search for a remote pad connected to the given pad by iterating over all
653 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300654 *
655 * Return a pointer to the pad at the remote end of the first found enabled
656 * link, or NULL if no enabled link has been found.
657 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300658struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300659{
660 unsigned int i;
661
662 for (i = 0; i < pad->entity->num_links; i++) {
663 struct media_link *link = &pad->entity->links[i];
664
665 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
666 continue;
667
668 if (link->source == pad)
669 return link->sink;
670
671 if (link->sink == pad)
672 return link->source;
673 }
674
675 return NULL;
676
677}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300678EXPORT_SYMBOL_GPL(media_entity_remote_pad);