blob: a76655c2ddef2b268930616fe5ec8efcc3dd701b [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{
46 /* For now, nothing to do */
47}
48
49/**
50 * media_gobj_remove - Stop using a graph object on a media device
51 *
52 * @graph_obj: Pointer to the object
53 *
54 * This should be called at media_device_unregister_*() routines
55 */
56void media_gobj_remove(struct media_gobj *gobj)
57{
58 /* For now, nothing to do */
59}
60
61/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -030062 * media_entity_init - Initialize a media entity
63 *
64 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030065 * @pads: Array of 'num_pads' pads.
66 *
67 * The total number of pads is an intrinsic property of entities known by the
68 * entity driver, while the total number of links depends on hardware design
69 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030070 * use cases the number of links can safely be assumed to be equal to or
71 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030072 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030073 * For those reasons the links array can be preallocated based on the number
74 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030075 *
76 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030077 * 'num_pads' elements. The media_entity::max_links field will be set to the
78 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030079 *
80 * The pads array is managed by the entity driver and passed to
81 * media_entity_init() where its pointer will be stored in the entity structure.
82 */
83int
84media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030085 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -030086{
87 struct media_link *links;
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030088 unsigned int max_links = num_pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -030089 unsigned int i;
90
91 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
92 if (links == NULL)
93 return -ENOMEM;
94
95 entity->group_id = 0;
96 entity->max_links = max_links;
97 entity->num_links = 0;
98 entity->num_backlinks = 0;
99 entity->num_pads = num_pads;
100 entity->pads = pads;
101 entity->links = links;
102
103 for (i = 0; i < num_pads; i++) {
104 pads[i].entity = entity;
105 pads[i].index = i;
106 }
107
108 return 0;
109}
110EXPORT_SYMBOL_GPL(media_entity_init);
111
112void
113media_entity_cleanup(struct media_entity *entity)
114{
115 kfree(entity->links);
116}
117EXPORT_SYMBOL_GPL(media_entity_cleanup);
118
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300119/* -----------------------------------------------------------------------------
120 * Graph traversal
121 */
122
123static struct media_entity *
124media_entity_other(struct media_entity *entity, struct media_link *link)
125{
126 if (link->source->entity == entity)
127 return link->sink->entity;
128 else
129 return link->source->entity;
130}
131
132/* push an entity to traversal stack */
133static void stack_push(struct media_entity_graph *graph,
134 struct media_entity *entity)
135{
136 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
137 WARN_ON(1);
138 return;
139 }
140 graph->top++;
141 graph->stack[graph->top].link = 0;
142 graph->stack[graph->top].entity = entity;
143}
144
145static struct media_entity *stack_pop(struct media_entity_graph *graph)
146{
147 struct media_entity *entity;
148
149 entity = graph->stack[graph->top].entity;
150 graph->top--;
151
152 return entity;
153}
154
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300155#define link_top(en) ((en)->stack[(en)->top].link)
156#define stack_top(en) ((en)->stack[(en)->top].entity)
157
158/**
159 * media_entity_graph_walk_start - Start walking the media graph at a given entity
160 * @graph: Media graph structure that will be used to walk the graph
161 * @entity: Starting entity
162 *
163 * This function initializes the graph traversal structure to walk the entities
164 * graph starting at the given entity. The traversal structure must not be
165 * modified by the caller during graph traversal. When done the structure can
166 * safely be freed.
167 */
168void media_entity_graph_walk_start(struct media_entity_graph *graph,
169 struct media_entity *entity)
170{
171 graph->top = 0;
172 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300173 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
174
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300175 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300176 return;
177
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300178 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300179 stack_push(graph, entity);
180}
181EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
182
183/**
184 * media_entity_graph_walk_next - Get the next entity in the graph
185 * @graph: Media graph structure
186 *
187 * Perform a depth-first traversal of the given media entities graph.
188 *
189 * The graph structure must have been previously initialized with a call to
190 * media_entity_graph_walk_start().
191 *
192 * Return the next entity in the graph or NULL if the whole graph have been
193 * traversed.
194 */
195struct media_entity *
196media_entity_graph_walk_next(struct media_entity_graph *graph)
197{
198 if (stack_top(graph) == NULL)
199 return NULL;
200
201 /*
202 * Depth first search. Push entity to stack and continue from
203 * top of the stack until no more entities on the level can be
204 * found.
205 */
206 while (link_top(graph) < stack_top(graph)->num_links) {
207 struct media_entity *entity = stack_top(graph);
208 struct media_link *link = &entity->links[link_top(graph)];
209 struct media_entity *next;
210
211 /* The link is not enabled so we do not follow. */
212 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
213 link_top(graph)++;
214 continue;
215 }
216
217 /* Get the entity in the other end of the link . */
218 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300219 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300220 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300221
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300222 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300223 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300224 link_top(graph)++;
225 continue;
226 }
227
228 /* Push the new entity to stack and start over. */
229 link_top(graph)++;
230 stack_push(graph, next);
231 }
232
233 return stack_pop(graph);
234}
235EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
236
237/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300238 * Pipeline management
239 */
240
241/**
242 * media_entity_pipeline_start - Mark a pipeline as streaming
243 * @entity: Starting entity
244 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
245 *
246 * Mark all entities connected to a given entity through enabled links, either
247 * directly or indirectly, as streaming. The given pipeline object is assigned to
248 * every entity in the pipeline and stored in the media_entity pipe field.
249 *
250 * Calls to this function can be nested, in which case the same number of
251 * media_entity_pipeline_stop() calls will be required to stop streaming. The
252 * pipeline pointer must be identical for all nested calls to
253 * media_entity_pipeline_start().
254 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300255__must_check int media_entity_pipeline_start(struct media_entity *entity,
256 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300257{
258 struct media_device *mdev = entity->parent;
259 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300260 struct media_entity *entity_err = entity;
261 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300262
263 mutex_lock(&mdev->graph_mutex);
264
265 media_entity_graph_walk_start(&graph, entity);
266
267 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300268 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
269 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300270 unsigned int i;
271
Laurent Pincharte02188c2010-08-25 09:00:41 -0300272 entity->stream_count++;
273 WARN_ON(entity->pipe && entity->pipe != pipe);
274 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300275
276 /* Already streaming --- no need to check. */
277 if (entity->stream_count > 1)
278 continue;
279
280 if (!entity->ops || !entity->ops->link_validate)
281 continue;
282
Sakari Ailusde49c282013-10-13 08:00:26 -0300283 bitmap_zero(active, entity->num_pads);
284 bitmap_fill(has_no_links, entity->num_pads);
285
Sakari Ailusaf88be32012-01-11 06:25:15 -0300286 for (i = 0; i < entity->num_links; i++) {
287 struct media_link *link = &entity->links[i];
Sakari Ailusde49c282013-10-13 08:00:26 -0300288 struct media_pad *pad = link->sink->entity == entity
289 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300290
Sakari Ailusde49c282013-10-13 08:00:26 -0300291 /* Mark that a pad is connected by a link. */
292 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300293
Sakari Ailusde49c282013-10-13 08:00:26 -0300294 /*
295 * Pads that either do not need to connect or
296 * are connected through an enabled link are
297 * fine.
298 */
299 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
300 link->flags & MEDIA_LNK_FL_ENABLED)
301 bitmap_set(active, pad->index, 1);
302
303 /*
304 * Link validation will only take place for
305 * sink ends of the link that are enabled.
306 */
307 if (link->sink != pad ||
308 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300309 continue;
310
311 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300312 if (ret < 0 && ret != -ENOIOCTLCMD) {
313 dev_dbg(entity->parent->dev,
314 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200315 link->source->entity->name,
316 link->source->index,
317 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300318 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300319 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300320 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300321
322 /* Either no links or validated links are fine. */
323 bitmap_or(active, active, has_no_links, entity->num_pads);
324
325 if (!bitmap_full(active, entity->num_pads)) {
326 ret = -EPIPE;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300327 dev_dbg(entity->parent->dev,
328 "\"%s\":%u must be connected by an enabled link\n",
329 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300330 (unsigned)find_first_zero_bit(
331 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300332 goto error;
333 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300334 }
335
336 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300337
338 return 0;
339
340error:
341 /*
342 * Link validation on graph failed. We revert what we did and
343 * return the error.
344 */
345 media_entity_graph_walk_start(&graph, entity_err);
346
347 while ((entity_err = media_entity_graph_walk_next(&graph))) {
348 entity_err->stream_count--;
349 if (entity_err->stream_count == 0)
350 entity_err->pipe = NULL;
351
352 /*
353 * We haven't increased stream_count further than this
354 * so we quit here.
355 */
356 if (entity_err == entity)
357 break;
358 }
359
360 mutex_unlock(&mdev->graph_mutex);
361
362 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300363}
364EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
365
366/**
367 * media_entity_pipeline_stop - Mark a pipeline as not streaming
368 * @entity: Starting entity
369 *
370 * Mark all entities connected to a given entity through enabled links, either
371 * directly or indirectly, as not streaming. The media_entity pipe field is
372 * reset to NULL.
373 *
374 * If multiple calls to media_entity_pipeline_start() have been made, the same
375 * number of calls to this function are required to mark the pipeline as not
376 * streaming.
377 */
378void media_entity_pipeline_stop(struct media_entity *entity)
379{
380 struct media_device *mdev = entity->parent;
381 struct media_entity_graph graph;
382
383 mutex_lock(&mdev->graph_mutex);
384
385 media_entity_graph_walk_start(&graph, entity);
386
387 while ((entity = media_entity_graph_walk_next(&graph))) {
388 entity->stream_count--;
389 if (entity->stream_count == 0)
390 entity->pipe = NULL;
391 }
392
393 mutex_unlock(&mdev->graph_mutex);
394}
395EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
396
397/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300398 * Module use count
399 */
400
401/*
402 * media_entity_get - Get a reference to the parent module
403 * @entity: The entity
404 *
405 * Get a reference to the parent media device module.
406 *
407 * The function will return immediately if @entity is NULL.
408 *
409 * Return a pointer to the entity on success or NULL on failure.
410 */
411struct media_entity *media_entity_get(struct media_entity *entity)
412{
413 if (entity == NULL)
414 return NULL;
415
416 if (entity->parent->dev &&
417 !try_module_get(entity->parent->dev->driver->owner))
418 return NULL;
419
420 return entity;
421}
422EXPORT_SYMBOL_GPL(media_entity_get);
423
424/*
425 * media_entity_put - Release the reference to the parent module
426 * @entity: The entity
427 *
428 * Release the reference count acquired by media_entity_get().
429 *
430 * The function will return immediately if @entity is NULL.
431 */
432void media_entity_put(struct media_entity *entity)
433{
434 if (entity == NULL)
435 return;
436
437 if (entity->parent->dev)
438 module_put(entity->parent->dev->driver->owner);
439}
440EXPORT_SYMBOL_GPL(media_entity_put);
441
442/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300443 * Links management
444 */
445
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300446static struct media_link *media_entity_add_link(struct media_entity *entity)
447{
448 if (entity->num_links >= entity->max_links) {
449 struct media_link *links = entity->links;
450 unsigned int max_links = entity->max_links + 2;
451 unsigned int i;
452
453 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
454 if (links == NULL)
455 return NULL;
456
457 for (i = 0; i < entity->num_links; i++)
458 links[i].reverse->reverse = &links[i];
459
460 entity->max_links = max_links;
461 entity->links = links;
462 }
463
464 return &entity->links[entity->num_links++];
465}
466
467int
468media_entity_create_link(struct media_entity *source, u16 source_pad,
469 struct media_entity *sink, u16 sink_pad, u32 flags)
470{
471 struct media_link *link;
472 struct media_link *backlink;
473
474 BUG_ON(source == NULL || sink == NULL);
475 BUG_ON(source_pad >= source->num_pads);
476 BUG_ON(sink_pad >= sink->num_pads);
477
478 link = media_entity_add_link(source);
479 if (link == NULL)
480 return -ENOMEM;
481
482 link->source = &source->pads[source_pad];
483 link->sink = &sink->pads[sink_pad];
484 link->flags = flags;
485
486 /* Create the backlink. Backlinks are used to help graph traversal and
487 * are not reported to userspace.
488 */
489 backlink = media_entity_add_link(sink);
490 if (backlink == NULL) {
491 source->num_links--;
492 return -ENOMEM;
493 }
494
495 backlink->source = &source->pads[source_pad];
496 backlink->sink = &sink->pads[sink_pad];
497 backlink->flags = flags;
498
499 link->reverse = backlink;
500 backlink->reverse = link;
501
502 sink->num_backlinks++;
503
504 return 0;
505}
506EXPORT_SYMBOL_GPL(media_entity_create_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300507
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300508void __media_entity_remove_links(struct media_entity *entity)
509{
510 unsigned int i;
511
512 for (i = 0; i < entity->num_links; i++) {
513 struct media_link *link = &entity->links[i];
514 struct media_entity *remote;
515 unsigned int r = 0;
516
517 if (link->source->entity == entity)
518 remote = link->sink->entity;
519 else
520 remote = link->source->entity;
521
522 while (r < remote->num_links) {
523 struct media_link *rlink = &remote->links[r];
524
525 if (rlink != link->reverse) {
526 r++;
527 continue;
528 }
529
530 if (link->source->entity == entity)
531 remote->num_backlinks--;
532
533 if (--remote->num_links == 0)
534 break;
535
536 /* Insert last entry in place of the dropped link. */
537 *rlink = remote->links[remote->num_links];
538 }
539 }
540
541 entity->num_links = 0;
542 entity->num_backlinks = 0;
543}
544EXPORT_SYMBOL_GPL(__media_entity_remove_links);
545
546void media_entity_remove_links(struct media_entity *entity)
547{
548 /* Do nothing if the entity is not registered. */
549 if (entity->parent == NULL)
550 return;
551
552 mutex_lock(&entity->parent->graph_mutex);
553 __media_entity_remove_links(entity);
554 mutex_unlock(&entity->parent->graph_mutex);
555}
556EXPORT_SYMBOL_GPL(media_entity_remove_links);
557
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300558static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
559{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300560 int ret;
561
562 /* Notify both entities. */
563 ret = media_entity_call(link->source->entity, link_setup,
564 link->source, link->sink, flags);
565 if (ret < 0 && ret != -ENOIOCTLCMD)
566 return ret;
567
568 ret = media_entity_call(link->sink->entity, link_setup,
569 link->sink, link->source, flags);
570 if (ret < 0 && ret != -ENOIOCTLCMD) {
571 media_entity_call(link->source->entity, link_setup,
572 link->source, link->sink, link->flags);
573 return ret;
574 }
575
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300576 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300577 link->reverse->flags = link->flags;
578
579 return 0;
580}
581
582/**
583 * __media_entity_setup_link - Configure a media link
584 * @link: The link being configured
585 * @flags: Link configuration flags
586 *
587 * The bulk of link setup is handled by the two entities connected through the
588 * link. This function notifies both entities of the link configuration change.
589 *
590 * If the link is immutable or if the current and new configuration are
591 * identical, return immediately.
592 *
593 * The user is expected to hold link->source->parent->mutex. If not,
594 * media_entity_setup_link() should be used instead.
595 */
596int __media_entity_setup_link(struct media_link *link, u32 flags)
597{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300598 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300599 struct media_device *mdev;
600 struct media_entity *source, *sink;
601 int ret = -EBUSY;
602
603 if (link == NULL)
604 return -EINVAL;
605
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300606 /* The non-modifiable link flags must not be modified. */
607 if ((link->flags & ~mask) != (flags & ~mask))
608 return -EINVAL;
609
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300610 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
611 return link->flags == flags ? 0 : -EINVAL;
612
613 if (link->flags == flags)
614 return 0;
615
616 source = link->source->entity;
617 sink = link->sink->entity;
618
Laurent Pincharte02188c2010-08-25 09:00:41 -0300619 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
620 (source->stream_count || sink->stream_count))
621 return -EBUSY;
622
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300623 mdev = source->parent;
624
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300625 if (mdev->link_notify) {
626 ret = mdev->link_notify(link, flags,
627 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300628 if (ret < 0)
629 return ret;
630 }
631
632 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300633
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300634 if (mdev->link_notify)
635 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300636
637 return ret;
638}
639
640int media_entity_setup_link(struct media_link *link, u32 flags)
641{
642 int ret;
643
644 mutex_lock(&link->source->entity->parent->graph_mutex);
645 ret = __media_entity_setup_link(link, flags);
646 mutex_unlock(&link->source->entity->parent->graph_mutex);
647
648 return ret;
649}
650EXPORT_SYMBOL_GPL(media_entity_setup_link);
651
652/**
653 * media_entity_find_link - Find a link between two pads
654 * @source: Source pad
655 * @sink: Sink pad
656 *
657 * Return a pointer to the link between the two entities. If no such link
658 * exists, return NULL.
659 */
660struct media_link *
661media_entity_find_link(struct media_pad *source, struct media_pad *sink)
662{
663 struct media_link *link;
664 unsigned int i;
665
666 for (i = 0; i < source->entity->num_links; ++i) {
667 link = &source->entity->links[i];
668
669 if (link->source->entity == source->entity &&
670 link->source->index == source->index &&
671 link->sink->entity == sink->entity &&
672 link->sink->index == sink->index)
673 return link;
674 }
675
676 return NULL;
677}
678EXPORT_SYMBOL_GPL(media_entity_find_link);
679
680/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300681 * media_entity_remote_pad - Find the pad at the remote end of a link
682 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300683 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300684 * Search for a remote pad connected to the given pad by iterating over all
685 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300686 *
687 * Return a pointer to the pad at the remote end of the first found enabled
688 * link, or NULL if no enabled link has been found.
689 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300690struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300691{
692 unsigned int i;
693
694 for (i = 0; i < pad->entity->num_links; i++) {
695 struct media_link *link = &pad->entity->links[i];
696
697 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
698 continue;
699
700 if (link->source == pad)
701 return link->sink;
702
703 if (link->sink == pad)
704 return link->source;
705 }
706
707 return NULL;
708
709}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300710EXPORT_SYMBOL_GPL(media_entity_remote_pad);