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