blob: 9f6f056eaeb01095b7354c77a087411f164c9211 [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;
51 }
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030052}
53
54/**
55 * media_gobj_remove - Stop using a graph object on a media device
56 *
57 * @graph_obj: Pointer to the object
58 *
59 * This should be called at media_device_unregister_*() routines
60 */
61void media_gobj_remove(struct media_gobj *gobj)
62{
63 /* For now, nothing to do */
64}
65
66/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -030067 * media_entity_init - Initialize a media entity
68 *
69 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030070 * @pads: Array of 'num_pads' pads.
71 *
72 * The total number of pads is an intrinsic property of entities known by the
73 * entity driver, while the total number of links depends on hardware design
74 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030075 * use cases the number of links can safely be assumed to be equal to or
76 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030077 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030078 * For those reasons the links array can be preallocated based on the number
79 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030080 *
81 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030082 * 'num_pads' elements. The media_entity::max_links field will be set to the
83 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030084 *
85 * The pads array is managed by the entity driver and passed to
86 * media_entity_init() where its pointer will be stored in the entity structure.
87 */
88int
89media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030090 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -030091{
92 struct media_link *links;
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -030093 unsigned int max_links = num_pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -030094 unsigned int i;
95
96 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
97 if (links == NULL)
98 return -ENOMEM;
99
100 entity->group_id = 0;
101 entity->max_links = max_links;
102 entity->num_links = 0;
103 entity->num_backlinks = 0;
104 entity->num_pads = num_pads;
105 entity->pads = pads;
106 entity->links = links;
107
108 for (i = 0; i < num_pads; i++) {
109 pads[i].entity = entity;
110 pads[i].index = i;
111 }
112
113 return 0;
114}
115EXPORT_SYMBOL_GPL(media_entity_init);
116
117void
118media_entity_cleanup(struct media_entity *entity)
119{
120 kfree(entity->links);
121}
122EXPORT_SYMBOL_GPL(media_entity_cleanup);
123
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300124/* -----------------------------------------------------------------------------
125 * Graph traversal
126 */
127
128static struct media_entity *
129media_entity_other(struct media_entity *entity, struct media_link *link)
130{
131 if (link->source->entity == entity)
132 return link->sink->entity;
133 else
134 return link->source->entity;
135}
136
137/* push an entity to traversal stack */
138static void stack_push(struct media_entity_graph *graph,
139 struct media_entity *entity)
140{
141 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
142 WARN_ON(1);
143 return;
144 }
145 graph->top++;
146 graph->stack[graph->top].link = 0;
147 graph->stack[graph->top].entity = entity;
148}
149
150static struct media_entity *stack_pop(struct media_entity_graph *graph)
151{
152 struct media_entity *entity;
153
154 entity = graph->stack[graph->top].entity;
155 graph->top--;
156
157 return entity;
158}
159
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300160#define link_top(en) ((en)->stack[(en)->top].link)
161#define stack_top(en) ((en)->stack[(en)->top].entity)
162
163/**
164 * media_entity_graph_walk_start - Start walking the media graph at a given entity
165 * @graph: Media graph structure that will be used to walk the graph
166 * @entity: Starting entity
167 *
168 * This function initializes the graph traversal structure to walk the entities
169 * graph starting at the given entity. The traversal structure must not be
170 * modified by the caller during graph traversal. When done the structure can
171 * safely be freed.
172 */
173void media_entity_graph_walk_start(struct media_entity_graph *graph,
174 struct media_entity *entity)
175{
176 graph->top = 0;
177 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300178 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
179
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300180 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300181 return;
182
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300183 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300184 stack_push(graph, entity);
185}
186EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
187
188/**
189 * media_entity_graph_walk_next - Get the next entity in the graph
190 * @graph: Media graph structure
191 *
192 * Perform a depth-first traversal of the given media entities graph.
193 *
194 * The graph structure must have been previously initialized with a call to
195 * media_entity_graph_walk_start().
196 *
197 * Return the next entity in the graph or NULL if the whole graph have been
198 * traversed.
199 */
200struct media_entity *
201media_entity_graph_walk_next(struct media_entity_graph *graph)
202{
203 if (stack_top(graph) == NULL)
204 return NULL;
205
206 /*
207 * Depth first search. Push entity to stack and continue from
208 * top of the stack until no more entities on the level can be
209 * found.
210 */
211 while (link_top(graph) < stack_top(graph)->num_links) {
212 struct media_entity *entity = stack_top(graph);
213 struct media_link *link = &entity->links[link_top(graph)];
214 struct media_entity *next;
215
216 /* The link is not enabled so we do not follow. */
217 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
218 link_top(graph)++;
219 continue;
220 }
221
222 /* Get the entity in the other end of the link . */
223 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300224 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300225 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300226
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300227 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300228 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300229 link_top(graph)++;
230 continue;
231 }
232
233 /* Push the new entity to stack and start over. */
234 link_top(graph)++;
235 stack_push(graph, next);
236 }
237
238 return stack_pop(graph);
239}
240EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
241
242/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300243 * Pipeline management
244 */
245
246/**
247 * media_entity_pipeline_start - Mark a pipeline as streaming
248 * @entity: Starting entity
249 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
250 *
251 * Mark all entities connected to a given entity through enabled links, either
252 * directly or indirectly, as streaming. The given pipeline object is assigned to
253 * every entity in the pipeline and stored in the media_entity pipe field.
254 *
255 * Calls to this function can be nested, in which case the same number of
256 * media_entity_pipeline_stop() calls will be required to stop streaming. The
257 * pipeline pointer must be identical for all nested calls to
258 * media_entity_pipeline_start().
259 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300260__must_check int media_entity_pipeline_start(struct media_entity *entity,
261 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300262{
263 struct media_device *mdev = entity->parent;
264 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300265 struct media_entity *entity_err = entity;
266 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300267
268 mutex_lock(&mdev->graph_mutex);
269
270 media_entity_graph_walk_start(&graph, entity);
271
272 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300273 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
274 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300275 unsigned int i;
276
Laurent Pincharte02188c2010-08-25 09:00:41 -0300277 entity->stream_count++;
278 WARN_ON(entity->pipe && entity->pipe != pipe);
279 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300280
281 /* Already streaming --- no need to check. */
282 if (entity->stream_count > 1)
283 continue;
284
285 if (!entity->ops || !entity->ops->link_validate)
286 continue;
287
Sakari Ailusde49c282013-10-13 08:00:26 -0300288 bitmap_zero(active, entity->num_pads);
289 bitmap_fill(has_no_links, entity->num_pads);
290
Sakari Ailusaf88be32012-01-11 06:25:15 -0300291 for (i = 0; i < entity->num_links; i++) {
292 struct media_link *link = &entity->links[i];
Sakari Ailusde49c282013-10-13 08:00:26 -0300293 struct media_pad *pad = link->sink->entity == entity
294 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300295
Sakari Ailusde49c282013-10-13 08:00:26 -0300296 /* Mark that a pad is connected by a link. */
297 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300298
Sakari Ailusde49c282013-10-13 08:00:26 -0300299 /*
300 * Pads that either do not need to connect or
301 * are connected through an enabled link are
302 * fine.
303 */
304 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
305 link->flags & MEDIA_LNK_FL_ENABLED)
306 bitmap_set(active, pad->index, 1);
307
308 /*
309 * Link validation will only take place for
310 * sink ends of the link that are enabled.
311 */
312 if (link->sink != pad ||
313 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300314 continue;
315
316 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300317 if (ret < 0 && ret != -ENOIOCTLCMD) {
318 dev_dbg(entity->parent->dev,
319 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200320 link->source->entity->name,
321 link->source->index,
322 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300323 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300324 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300325 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300326
327 /* Either no links or validated links are fine. */
328 bitmap_or(active, active, has_no_links, entity->num_pads);
329
330 if (!bitmap_full(active, entity->num_pads)) {
331 ret = -EPIPE;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300332 dev_dbg(entity->parent->dev,
333 "\"%s\":%u must be connected by an enabled link\n",
334 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300335 (unsigned)find_first_zero_bit(
336 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300337 goto error;
338 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300339 }
340
341 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300342
343 return 0;
344
345error:
346 /*
347 * Link validation on graph failed. We revert what we did and
348 * return the error.
349 */
350 media_entity_graph_walk_start(&graph, entity_err);
351
352 while ((entity_err = media_entity_graph_walk_next(&graph))) {
353 entity_err->stream_count--;
354 if (entity_err->stream_count == 0)
355 entity_err->pipe = NULL;
356
357 /*
358 * We haven't increased stream_count further than this
359 * so we quit here.
360 */
361 if (entity_err == entity)
362 break;
363 }
364
365 mutex_unlock(&mdev->graph_mutex);
366
367 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300368}
369EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
370
371/**
372 * media_entity_pipeline_stop - Mark a pipeline as not streaming
373 * @entity: Starting entity
374 *
375 * Mark all entities connected to a given entity through enabled links, either
376 * directly or indirectly, as not streaming. The media_entity pipe field is
377 * reset to NULL.
378 *
379 * If multiple calls to media_entity_pipeline_start() have been made, the same
380 * number of calls to this function are required to mark the pipeline as not
381 * streaming.
382 */
383void media_entity_pipeline_stop(struct media_entity *entity)
384{
385 struct media_device *mdev = entity->parent;
386 struct media_entity_graph graph;
387
388 mutex_lock(&mdev->graph_mutex);
389
390 media_entity_graph_walk_start(&graph, entity);
391
392 while ((entity = media_entity_graph_walk_next(&graph))) {
393 entity->stream_count--;
394 if (entity->stream_count == 0)
395 entity->pipe = NULL;
396 }
397
398 mutex_unlock(&mdev->graph_mutex);
399}
400EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
401
402/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300403 * Module use count
404 */
405
406/*
407 * media_entity_get - Get a reference to the parent module
408 * @entity: The entity
409 *
410 * Get a reference to the parent media device module.
411 *
412 * The function will return immediately if @entity is NULL.
413 *
414 * Return a pointer to the entity on success or NULL on failure.
415 */
416struct media_entity *media_entity_get(struct media_entity *entity)
417{
418 if (entity == NULL)
419 return NULL;
420
421 if (entity->parent->dev &&
422 !try_module_get(entity->parent->dev->driver->owner))
423 return NULL;
424
425 return entity;
426}
427EXPORT_SYMBOL_GPL(media_entity_get);
428
429/*
430 * media_entity_put - Release the reference to the parent module
431 * @entity: The entity
432 *
433 * Release the reference count acquired by media_entity_get().
434 *
435 * The function will return immediately if @entity is NULL.
436 */
437void media_entity_put(struct media_entity *entity)
438{
439 if (entity == NULL)
440 return;
441
442 if (entity->parent->dev)
443 module_put(entity->parent->dev->driver->owner);
444}
445EXPORT_SYMBOL_GPL(media_entity_put);
446
447/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300448 * Links management
449 */
450
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300451static struct media_link *media_entity_add_link(struct media_entity *entity)
452{
453 if (entity->num_links >= entity->max_links) {
454 struct media_link *links = entity->links;
455 unsigned int max_links = entity->max_links + 2;
456 unsigned int i;
457
458 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
459 if (links == NULL)
460 return NULL;
461
462 for (i = 0; i < entity->num_links; i++)
463 links[i].reverse->reverse = &links[i];
464
465 entity->max_links = max_links;
466 entity->links = links;
467 }
468
469 return &entity->links[entity->num_links++];
470}
471
472int
473media_entity_create_link(struct media_entity *source, u16 source_pad,
474 struct media_entity *sink, u16 sink_pad, u32 flags)
475{
476 struct media_link *link;
477 struct media_link *backlink;
478
479 BUG_ON(source == NULL || sink == NULL);
480 BUG_ON(source_pad >= source->num_pads);
481 BUG_ON(sink_pad >= sink->num_pads);
482
483 link = media_entity_add_link(source);
484 if (link == NULL)
485 return -ENOMEM;
486
487 link->source = &source->pads[source_pad];
488 link->sink = &sink->pads[sink_pad];
489 link->flags = flags;
490
491 /* Create the backlink. Backlinks are used to help graph traversal and
492 * are not reported to userspace.
493 */
494 backlink = media_entity_add_link(sink);
495 if (backlink == NULL) {
496 source->num_links--;
497 return -ENOMEM;
498 }
499
500 backlink->source = &source->pads[source_pad];
501 backlink->sink = &sink->pads[sink_pad];
502 backlink->flags = flags;
503
504 link->reverse = backlink;
505 backlink->reverse = link;
506
507 sink->num_backlinks++;
508
509 return 0;
510}
511EXPORT_SYMBOL_GPL(media_entity_create_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300512
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300513void __media_entity_remove_links(struct media_entity *entity)
514{
515 unsigned int i;
516
517 for (i = 0; i < entity->num_links; i++) {
518 struct media_link *link = &entity->links[i];
519 struct media_entity *remote;
520 unsigned int r = 0;
521
522 if (link->source->entity == entity)
523 remote = link->sink->entity;
524 else
525 remote = link->source->entity;
526
527 while (r < remote->num_links) {
528 struct media_link *rlink = &remote->links[r];
529
530 if (rlink != link->reverse) {
531 r++;
532 continue;
533 }
534
535 if (link->source->entity == entity)
536 remote->num_backlinks--;
537
538 if (--remote->num_links == 0)
539 break;
540
541 /* Insert last entry in place of the dropped link. */
542 *rlink = remote->links[remote->num_links];
543 }
544 }
545
546 entity->num_links = 0;
547 entity->num_backlinks = 0;
548}
549EXPORT_SYMBOL_GPL(__media_entity_remove_links);
550
551void media_entity_remove_links(struct media_entity *entity)
552{
553 /* Do nothing if the entity is not registered. */
554 if (entity->parent == NULL)
555 return;
556
557 mutex_lock(&entity->parent->graph_mutex);
558 __media_entity_remove_links(entity);
559 mutex_unlock(&entity->parent->graph_mutex);
560}
561EXPORT_SYMBOL_GPL(media_entity_remove_links);
562
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300563static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
564{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300565 int ret;
566
567 /* Notify both entities. */
568 ret = media_entity_call(link->source->entity, link_setup,
569 link->source, link->sink, flags);
570 if (ret < 0 && ret != -ENOIOCTLCMD)
571 return ret;
572
573 ret = media_entity_call(link->sink->entity, link_setup,
574 link->sink, link->source, flags);
575 if (ret < 0 && ret != -ENOIOCTLCMD) {
576 media_entity_call(link->source->entity, link_setup,
577 link->source, link->sink, link->flags);
578 return ret;
579 }
580
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300581 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300582 link->reverse->flags = link->flags;
583
584 return 0;
585}
586
587/**
588 * __media_entity_setup_link - Configure a media link
589 * @link: The link being configured
590 * @flags: Link configuration flags
591 *
592 * The bulk of link setup is handled by the two entities connected through the
593 * link. This function notifies both entities of the link configuration change.
594 *
595 * If the link is immutable or if the current and new configuration are
596 * identical, return immediately.
597 *
598 * The user is expected to hold link->source->parent->mutex. If not,
599 * media_entity_setup_link() should be used instead.
600 */
601int __media_entity_setup_link(struct media_link *link, u32 flags)
602{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300603 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300604 struct media_device *mdev;
605 struct media_entity *source, *sink;
606 int ret = -EBUSY;
607
608 if (link == NULL)
609 return -EINVAL;
610
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300611 /* The non-modifiable link flags must not be modified. */
612 if ((link->flags & ~mask) != (flags & ~mask))
613 return -EINVAL;
614
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300615 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
616 return link->flags == flags ? 0 : -EINVAL;
617
618 if (link->flags == flags)
619 return 0;
620
621 source = link->source->entity;
622 sink = link->sink->entity;
623
Laurent Pincharte02188c2010-08-25 09:00:41 -0300624 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
625 (source->stream_count || sink->stream_count))
626 return -EBUSY;
627
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300628 mdev = source->parent;
629
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300630 if (mdev->link_notify) {
631 ret = mdev->link_notify(link, flags,
632 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300633 if (ret < 0)
634 return ret;
635 }
636
637 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300638
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300639 if (mdev->link_notify)
640 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300641
642 return ret;
643}
644
645int media_entity_setup_link(struct media_link *link, u32 flags)
646{
647 int ret;
648
649 mutex_lock(&link->source->entity->parent->graph_mutex);
650 ret = __media_entity_setup_link(link, flags);
651 mutex_unlock(&link->source->entity->parent->graph_mutex);
652
653 return ret;
654}
655EXPORT_SYMBOL_GPL(media_entity_setup_link);
656
657/**
658 * media_entity_find_link - Find a link between two pads
659 * @source: Source pad
660 * @sink: Sink pad
661 *
662 * Return a pointer to the link between the two entities. If no such link
663 * exists, return NULL.
664 */
665struct media_link *
666media_entity_find_link(struct media_pad *source, struct media_pad *sink)
667{
668 struct media_link *link;
669 unsigned int i;
670
671 for (i = 0; i < source->entity->num_links; ++i) {
672 link = &source->entity->links[i];
673
674 if (link->source->entity == source->entity &&
675 link->source->index == source->index &&
676 link->sink->entity == sink->entity &&
677 link->sink->index == sink->index)
678 return link;
679 }
680
681 return NULL;
682}
683EXPORT_SYMBOL_GPL(media_entity_find_link);
684
685/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300686 * media_entity_remote_pad - Find the pad at the remote end of a link
687 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300688 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300689 * Search for a remote pad connected to the given pad by iterating over all
690 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300691 *
692 * Return a pointer to the pad at the remote end of the first found enabled
693 * link, or NULL if no enabled link has been found.
694 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300695struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300696{
697 unsigned int i;
698
699 for (i = 0; i < pad->entity->num_links; i++) {
700 struct media_link *link = &pad->entity->links[i];
701
702 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
703 continue;
704
705 if (link->source == pad)
706 return link->sink;
707
708 if (link->sink == pad)
709 return link->source;
710 }
711
712 return NULL;
713
714}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300715EXPORT_SYMBOL_GPL(media_entity_remote_pad);