blob: 6ed4a19b0be95db932b09251efce1ce48105917b [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 Chehab39a956c2015-08-13 14:42:42 -030030 * dev_dbg_obj - Prints in debug mode a change on some object
31 *
32 * @event_name: Name of the event to report. Could be __func__
33 * @gobj: Pointer to the object
34 *
35 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
36 * won't produce any code.
37 */
38static inline const char *gobj_type(enum media_gobj_type type)
39{
40 switch (type) {
41 case MEDIA_GRAPH_ENTITY:
42 return "entity";
43 case MEDIA_GRAPH_PAD:
44 return "pad";
45 case MEDIA_GRAPH_LINK:
46 return "link";
47 default:
48 return "unknown";
49 }
50}
51
52static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
53{
54#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
55 switch (media_type(gobj)) {
56 case MEDIA_GRAPH_ENTITY:
57 dev_dbg(gobj->mdev->dev,
58 "%s: id 0x%08x entity#%d: '%s'\n",
59 event_name, gobj->id, media_localid(gobj),
60 gobj_to_entity(gobj)->name);
61 break;
62 case MEDIA_GRAPH_LINK:
63 {
64 struct media_link *link = gobj_to_link(gobj);
65
66 dev_dbg(gobj->mdev->dev,
67 "%s: id 0x%08x link#%d: '%s' %s#%d ==> '%s' %s#%d\n",
68 event_name, gobj->id, media_localid(gobj),
69
70 link->source->entity->name,
71 gobj_type(media_type(&link->source->graph_obj)),
72 media_localid(&link->source->graph_obj),
73
74 link->sink->entity->name,
75 gobj_type(media_type(&link->sink->graph_obj)),
76 media_localid(&link->sink->graph_obj));
77 break;
78 }
79 case MEDIA_GRAPH_PAD:
80 {
81 struct media_pad *pad = gobj_to_pad(gobj);
82
83 dev_dbg(gobj->mdev->dev,
84 "%s: id 0x%08x pad#%d: '%s':%d\n",
85 event_name, gobj->id, media_localid(gobj),
86 pad->entity->name, pad->index);
87 }
88 }
89#endif
90}
91
92/**
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030093 * media_gobj_init - Initialize a graph object
94 *
95 * @mdev: Pointer to the media_device that contains the object
96 * @type: Type of the object
97 * @gobj: Pointer to the object
98 *
99 * This routine initializes the embedded struct media_gobj inside a
100 * media graph object. It is called automatically if media_*_create()
101 * calls are used. However, if the object (entity, link, pad, interface)
102 * is embedded on some other object, this function should be called before
103 * registering the object at the media controller.
104 */
105void media_gobj_init(struct media_device *mdev,
106 enum media_gobj_type type,
107 struct media_gobj *gobj)
108{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300109 gobj->mdev = mdev;
110
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300111 /* Create a per-type unique object ID */
112 switch (type) {
113 case MEDIA_GRAPH_ENTITY:
114 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
115 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300116 case MEDIA_GRAPH_PAD:
117 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
118 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300119 case MEDIA_GRAPH_LINK:
120 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
121 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300122 }
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300123 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300124}
125
126/**
127 * media_gobj_remove - Stop using a graph object on a media device
128 *
129 * @graph_obj: Pointer to the object
130 *
131 * This should be called at media_device_unregister_*() routines
132 */
133void media_gobj_remove(struct media_gobj *gobj)
134{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300135 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300136}
137
138/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300139 * media_entity_init - Initialize a media entity
140 *
141 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300142 * @pads: Array of 'num_pads' pads.
143 *
144 * The total number of pads is an intrinsic property of entities known by the
145 * entity driver, while the total number of links depends on hardware design
146 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300147 * use cases the number of links can safely be assumed to be equal to or
148 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300149 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300150 * For those reasons the links array can be preallocated based on the number
151 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300152 *
153 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300154 * 'num_pads' elements. The media_entity::max_links field will be set to the
155 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300156 *
157 * The pads array is managed by the entity driver and passed to
158 * media_entity_init() where its pointer will be stored in the entity structure.
159 */
160int
161media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300162 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300163{
164 struct media_link *links;
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300165 unsigned int max_links = num_pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300166 unsigned int i;
167
168 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
169 if (links == NULL)
170 return -ENOMEM;
171
172 entity->group_id = 0;
173 entity->max_links = max_links;
174 entity->num_links = 0;
175 entity->num_backlinks = 0;
176 entity->num_pads = num_pads;
177 entity->pads = pads;
178 entity->links = links;
179
180 for (i = 0; i < num_pads; i++) {
181 pads[i].entity = entity;
182 pads[i].index = i;
183 }
184
185 return 0;
186}
187EXPORT_SYMBOL_GPL(media_entity_init);
188
189void
190media_entity_cleanup(struct media_entity *entity)
191{
192 kfree(entity->links);
193}
194EXPORT_SYMBOL_GPL(media_entity_cleanup);
195
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300196/* -----------------------------------------------------------------------------
197 * Graph traversal
198 */
199
200static struct media_entity *
201media_entity_other(struct media_entity *entity, struct media_link *link)
202{
203 if (link->source->entity == entity)
204 return link->sink->entity;
205 else
206 return link->source->entity;
207}
208
209/* push an entity to traversal stack */
210static void stack_push(struct media_entity_graph *graph,
211 struct media_entity *entity)
212{
213 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
214 WARN_ON(1);
215 return;
216 }
217 graph->top++;
218 graph->stack[graph->top].link = 0;
219 graph->stack[graph->top].entity = entity;
220}
221
222static struct media_entity *stack_pop(struct media_entity_graph *graph)
223{
224 struct media_entity *entity;
225
226 entity = graph->stack[graph->top].entity;
227 graph->top--;
228
229 return entity;
230}
231
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300232#define link_top(en) ((en)->stack[(en)->top].link)
233#define stack_top(en) ((en)->stack[(en)->top].entity)
234
235/**
236 * media_entity_graph_walk_start - Start walking the media graph at a given entity
237 * @graph: Media graph structure that will be used to walk the graph
238 * @entity: Starting entity
239 *
240 * This function initializes the graph traversal structure to walk the entities
241 * graph starting at the given entity. The traversal structure must not be
242 * modified by the caller during graph traversal. When done the structure can
243 * safely be freed.
244 */
245void media_entity_graph_walk_start(struct media_entity_graph *graph,
246 struct media_entity *entity)
247{
248 graph->top = 0;
249 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300250 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
251
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300252 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300253 return;
254
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300255 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300256 stack_push(graph, entity);
257}
258EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
259
260/**
261 * media_entity_graph_walk_next - Get the next entity in the graph
262 * @graph: Media graph structure
263 *
264 * Perform a depth-first traversal of the given media entities graph.
265 *
266 * The graph structure must have been previously initialized with a call to
267 * media_entity_graph_walk_start().
268 *
269 * Return the next entity in the graph or NULL if the whole graph have been
270 * traversed.
271 */
272struct media_entity *
273media_entity_graph_walk_next(struct media_entity_graph *graph)
274{
275 if (stack_top(graph) == NULL)
276 return NULL;
277
278 /*
279 * Depth first search. Push entity to stack and continue from
280 * top of the stack until no more entities on the level can be
281 * found.
282 */
283 while (link_top(graph) < stack_top(graph)->num_links) {
284 struct media_entity *entity = stack_top(graph);
285 struct media_link *link = &entity->links[link_top(graph)];
286 struct media_entity *next;
287
288 /* The link is not enabled so we do not follow. */
289 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
290 link_top(graph)++;
291 continue;
292 }
293
294 /* Get the entity in the other end of the link . */
295 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300296 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300297 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300298
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300299 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300300 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300301 link_top(graph)++;
302 continue;
303 }
304
305 /* Push the new entity to stack and start over. */
306 link_top(graph)++;
307 stack_push(graph, next);
308 }
309
310 return stack_pop(graph);
311}
312EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
313
314/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300315 * Pipeline management
316 */
317
318/**
319 * media_entity_pipeline_start - Mark a pipeline as streaming
320 * @entity: Starting entity
321 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
322 *
323 * Mark all entities connected to a given entity through enabled links, either
324 * directly or indirectly, as streaming. The given pipeline object is assigned to
325 * every entity in the pipeline and stored in the media_entity pipe field.
326 *
327 * Calls to this function can be nested, in which case the same number of
328 * media_entity_pipeline_stop() calls will be required to stop streaming. The
329 * pipeline pointer must be identical for all nested calls to
330 * media_entity_pipeline_start().
331 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300332__must_check int media_entity_pipeline_start(struct media_entity *entity,
333 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300334{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300335 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300336 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300337 struct media_entity *entity_err = entity;
338 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300339
340 mutex_lock(&mdev->graph_mutex);
341
342 media_entity_graph_walk_start(&graph, entity);
343
344 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300345 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
346 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300347 unsigned int i;
348
Laurent Pincharte02188c2010-08-25 09:00:41 -0300349 entity->stream_count++;
350 WARN_ON(entity->pipe && entity->pipe != pipe);
351 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300352
353 /* Already streaming --- no need to check. */
354 if (entity->stream_count > 1)
355 continue;
356
357 if (!entity->ops || !entity->ops->link_validate)
358 continue;
359
Sakari Ailusde49c282013-10-13 08:00:26 -0300360 bitmap_zero(active, entity->num_pads);
361 bitmap_fill(has_no_links, entity->num_pads);
362
Sakari Ailusaf88be32012-01-11 06:25:15 -0300363 for (i = 0; i < entity->num_links; i++) {
364 struct media_link *link = &entity->links[i];
Sakari Ailusde49c282013-10-13 08:00:26 -0300365 struct media_pad *pad = link->sink->entity == entity
366 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300367
Sakari Ailusde49c282013-10-13 08:00:26 -0300368 /* Mark that a pad is connected by a link. */
369 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300370
Sakari Ailusde49c282013-10-13 08:00:26 -0300371 /*
372 * Pads that either do not need to connect or
373 * are connected through an enabled link are
374 * fine.
375 */
376 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
377 link->flags & MEDIA_LNK_FL_ENABLED)
378 bitmap_set(active, pad->index, 1);
379
380 /*
381 * Link validation will only take place for
382 * sink ends of the link that are enabled.
383 */
384 if (link->sink != pad ||
385 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300386 continue;
387
388 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300389 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300390 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300391 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200392 link->source->entity->name,
393 link->source->index,
394 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300395 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300396 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300397 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300398
399 /* Either no links or validated links are fine. */
400 bitmap_or(active, active, has_no_links, entity->num_pads);
401
402 if (!bitmap_full(active, entity->num_pads)) {
403 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300404 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300405 "\"%s\":%u must be connected by an enabled link\n",
406 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300407 (unsigned)find_first_zero_bit(
408 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300409 goto error;
410 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300411 }
412
413 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300414
415 return 0;
416
417error:
418 /*
419 * Link validation on graph failed. We revert what we did and
420 * return the error.
421 */
422 media_entity_graph_walk_start(&graph, entity_err);
423
424 while ((entity_err = media_entity_graph_walk_next(&graph))) {
425 entity_err->stream_count--;
426 if (entity_err->stream_count == 0)
427 entity_err->pipe = NULL;
428
429 /*
430 * We haven't increased stream_count further than this
431 * so we quit here.
432 */
433 if (entity_err == entity)
434 break;
435 }
436
437 mutex_unlock(&mdev->graph_mutex);
438
439 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300440}
441EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
442
443/**
444 * media_entity_pipeline_stop - Mark a pipeline as not streaming
445 * @entity: Starting entity
446 *
447 * Mark all entities connected to a given entity through enabled links, either
448 * directly or indirectly, as not streaming. The media_entity pipe field is
449 * reset to NULL.
450 *
451 * If multiple calls to media_entity_pipeline_start() have been made, the same
452 * number of calls to this function are required to mark the pipeline as not
453 * streaming.
454 */
455void media_entity_pipeline_stop(struct media_entity *entity)
456{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300457 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300458 struct media_entity_graph graph;
459
460 mutex_lock(&mdev->graph_mutex);
461
462 media_entity_graph_walk_start(&graph, entity);
463
464 while ((entity = media_entity_graph_walk_next(&graph))) {
465 entity->stream_count--;
466 if (entity->stream_count == 0)
467 entity->pipe = NULL;
468 }
469
470 mutex_unlock(&mdev->graph_mutex);
471}
472EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
473
474/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300475 * Module use count
476 */
477
478/*
479 * media_entity_get - Get a reference to the parent module
480 * @entity: The entity
481 *
482 * Get a reference to the parent media device module.
483 *
484 * The function will return immediately if @entity is NULL.
485 *
486 * Return a pointer to the entity on success or NULL on failure.
487 */
488struct media_entity *media_entity_get(struct media_entity *entity)
489{
490 if (entity == NULL)
491 return NULL;
492
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300493 if (entity->graph_obj.mdev->dev &&
494 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300495 return NULL;
496
497 return entity;
498}
499EXPORT_SYMBOL_GPL(media_entity_get);
500
501/*
502 * media_entity_put - Release the reference to the parent module
503 * @entity: The entity
504 *
505 * Release the reference count acquired by media_entity_get().
506 *
507 * The function will return immediately if @entity is NULL.
508 */
509void media_entity_put(struct media_entity *entity)
510{
511 if (entity == NULL)
512 return;
513
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300514 if (entity->graph_obj.mdev->dev)
515 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300516}
517EXPORT_SYMBOL_GPL(media_entity_put);
518
519/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300520 * Links management
521 */
522
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300523static struct media_link *media_entity_add_link(struct media_entity *entity)
524{
525 if (entity->num_links >= entity->max_links) {
526 struct media_link *links = entity->links;
527 unsigned int max_links = entity->max_links + 2;
528 unsigned int i;
529
530 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
531 if (links == NULL)
532 return NULL;
533
534 for (i = 0; i < entity->num_links; i++)
535 links[i].reverse->reverse = &links[i];
536
537 entity->max_links = max_links;
538 entity->links = links;
539 }
540
541 return &entity->links[entity->num_links++];
542}
543
544int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300545media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300546 struct media_entity *sink, u16 sink_pad, u32 flags)
547{
548 struct media_link *link;
549 struct media_link *backlink;
550
551 BUG_ON(source == NULL || sink == NULL);
552 BUG_ON(source_pad >= source->num_pads);
553 BUG_ON(sink_pad >= sink->num_pads);
554
555 link = media_entity_add_link(source);
556 if (link == NULL)
557 return -ENOMEM;
558
559 link->source = &source->pads[source_pad];
560 link->sink = &sink->pads[sink_pad];
561 link->flags = flags;
562
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300563 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300564 media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
565 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300566
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300567 /* Create the backlink. Backlinks are used to help graph traversal and
568 * are not reported to userspace.
569 */
570 backlink = media_entity_add_link(sink);
571 if (backlink == NULL) {
572 source->num_links--;
573 return -ENOMEM;
574 }
575
576 backlink->source = &source->pads[source_pad];
577 backlink->sink = &sink->pads[sink_pad];
578 backlink->flags = flags;
579
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300580 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300581 media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
582 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300583
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300584 link->reverse = backlink;
585 backlink->reverse = link;
586
587 sink->num_backlinks++;
588
589 return 0;
590}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300591EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300592
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300593void __media_entity_remove_links(struct media_entity *entity)
594{
595 unsigned int i;
596
597 for (i = 0; i < entity->num_links; i++) {
598 struct media_link *link = &entity->links[i];
599 struct media_entity *remote;
600 unsigned int r = 0;
601
602 if (link->source->entity == entity)
603 remote = link->sink->entity;
604 else
605 remote = link->source->entity;
606
607 while (r < remote->num_links) {
608 struct media_link *rlink = &remote->links[r];
609
610 if (rlink != link->reverse) {
611 r++;
612 continue;
613 }
614
615 if (link->source->entity == entity)
616 remote->num_backlinks--;
617
618 if (--remote->num_links == 0)
619 break;
620
621 /* Insert last entry in place of the dropped link. */
622 *rlink = remote->links[remote->num_links];
623 }
624 }
625
626 entity->num_links = 0;
627 entity->num_backlinks = 0;
628}
629EXPORT_SYMBOL_GPL(__media_entity_remove_links);
630
631void media_entity_remove_links(struct media_entity *entity)
632{
633 /* Do nothing if the entity is not registered. */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300634 if (entity->graph_obj.mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300635 return;
636
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300637 mutex_lock(&entity->graph_obj.mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300638 __media_entity_remove_links(entity);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300639 mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300640}
641EXPORT_SYMBOL_GPL(media_entity_remove_links);
642
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300643static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
644{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300645 int ret;
646
647 /* Notify both entities. */
648 ret = media_entity_call(link->source->entity, link_setup,
649 link->source, link->sink, flags);
650 if (ret < 0 && ret != -ENOIOCTLCMD)
651 return ret;
652
653 ret = media_entity_call(link->sink->entity, link_setup,
654 link->sink, link->source, flags);
655 if (ret < 0 && ret != -ENOIOCTLCMD) {
656 media_entity_call(link->source->entity, link_setup,
657 link->source, link->sink, link->flags);
658 return ret;
659 }
660
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300661 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300662 link->reverse->flags = link->flags;
663
664 return 0;
665}
666
667/**
668 * __media_entity_setup_link - Configure a media link
669 * @link: The link being configured
670 * @flags: Link configuration flags
671 *
672 * The bulk of link setup is handled by the two entities connected through the
673 * link. This function notifies both entities of the link configuration change.
674 *
675 * If the link is immutable or if the current and new configuration are
676 * identical, return immediately.
677 *
678 * The user is expected to hold link->source->parent->mutex. If not,
679 * media_entity_setup_link() should be used instead.
680 */
681int __media_entity_setup_link(struct media_link *link, u32 flags)
682{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300683 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300684 struct media_device *mdev;
685 struct media_entity *source, *sink;
686 int ret = -EBUSY;
687
688 if (link == NULL)
689 return -EINVAL;
690
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300691 /* The non-modifiable link flags must not be modified. */
692 if ((link->flags & ~mask) != (flags & ~mask))
693 return -EINVAL;
694
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300695 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
696 return link->flags == flags ? 0 : -EINVAL;
697
698 if (link->flags == flags)
699 return 0;
700
701 source = link->source->entity;
702 sink = link->sink->entity;
703
Laurent Pincharte02188c2010-08-25 09:00:41 -0300704 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
705 (source->stream_count || sink->stream_count))
706 return -EBUSY;
707
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300708 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300709
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300710 if (mdev->link_notify) {
711 ret = mdev->link_notify(link, flags,
712 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300713 if (ret < 0)
714 return ret;
715 }
716
717 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300718
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300719 if (mdev->link_notify)
720 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300721
722 return ret;
723}
724
725int media_entity_setup_link(struct media_link *link, u32 flags)
726{
727 int ret;
728
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300729 mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300730 ret = __media_entity_setup_link(link, flags);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300731 mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300732
733 return ret;
734}
735EXPORT_SYMBOL_GPL(media_entity_setup_link);
736
737/**
738 * media_entity_find_link - Find a link between two pads
739 * @source: Source pad
740 * @sink: Sink pad
741 *
742 * Return a pointer to the link between the two entities. If no such link
743 * exists, return NULL.
744 */
745struct media_link *
746media_entity_find_link(struct media_pad *source, struct media_pad *sink)
747{
748 struct media_link *link;
749 unsigned int i;
750
751 for (i = 0; i < source->entity->num_links; ++i) {
752 link = &source->entity->links[i];
753
754 if (link->source->entity == source->entity &&
755 link->source->index == source->index &&
756 link->sink->entity == sink->entity &&
757 link->sink->index == sink->index)
758 return link;
759 }
760
761 return NULL;
762}
763EXPORT_SYMBOL_GPL(media_entity_find_link);
764
765/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300766 * media_entity_remote_pad - Find the pad at the remote end of a link
767 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300768 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300769 * Search for a remote pad connected to the given pad by iterating over all
770 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300771 *
772 * Return a pointer to the pad at the remote end of the first found enabled
773 * link, or NULL if no enabled link has been found.
774 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300775struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300776{
777 unsigned int i;
778
779 for (i = 0; i < pad->entity->num_links; i++) {
780 struct media_link *link = &pad->entity->links[i];
781
782 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
783 continue;
784
785 if (link->source == pad)
786 return link->sink;
787
788 if (link->sink == pad)
789 return link->source;
790 }
791
792 return NULL;
793
794}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300795EXPORT_SYMBOL_GPL(media_entity_remote_pad);