blob: f85a711d49581f045c5ca8ceb7e4d71e40aa5c71 [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";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030047 case MEDIA_GRAPH_INTF_DEVNODE:
48 return "intf-devnode";
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030049 default:
50 return "unknown";
51 }
52}
53
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030054static inline const char *intf_type(struct media_interface *intf)
55{
56 switch (intf->type) {
57 case MEDIA_INTF_T_DVB_FE:
58 return "frontend";
59 case MEDIA_INTF_T_DVB_DEMUX:
60 return "demux";
61 case MEDIA_INTF_T_DVB_DVR:
62 return "DVR";
63 case MEDIA_INTF_T_DVB_CA:
64 return "CA";
65 case MEDIA_INTF_T_DVB_NET:
66 return "dvbnet";
67 case MEDIA_INTF_T_V4L_VIDEO:
68 return "video";
69 case MEDIA_INTF_T_V4L_VBI:
70 return "vbi";
71 case MEDIA_INTF_T_V4L_RADIO:
72 return "radio";
73 case MEDIA_INTF_T_V4L_SUBDEV:
74 return "v4l2-subdev";
75 case MEDIA_INTF_T_V4L_SWRADIO:
76 return "swradio";
77 default:
78 return "unknown-intf";
79 }
80};
81
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030082static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
83{
84#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
85 switch (media_type(gobj)) {
86 case MEDIA_GRAPH_ENTITY:
87 dev_dbg(gobj->mdev->dev,
88 "%s: id 0x%08x entity#%d: '%s'\n",
89 event_name, gobj->id, media_localid(gobj),
90 gobj_to_entity(gobj)->name);
91 break;
92 case MEDIA_GRAPH_LINK:
93 {
94 struct media_link *link = gobj_to_link(gobj);
95
96 dev_dbg(gobj->mdev->dev,
97 "%s: id 0x%08x link#%d: '%s' %s#%d ==> '%s' %s#%d\n",
98 event_name, gobj->id, media_localid(gobj),
99
100 link->source->entity->name,
101 gobj_type(media_type(&link->source->graph_obj)),
102 media_localid(&link->source->graph_obj),
103
104 link->sink->entity->name,
105 gobj_type(media_type(&link->sink->graph_obj)),
106 media_localid(&link->sink->graph_obj));
107 break;
108 }
109 case MEDIA_GRAPH_PAD:
110 {
111 struct media_pad *pad = gobj_to_pad(gobj);
112
113 dev_dbg(gobj->mdev->dev,
114 "%s: id 0x%08x pad#%d: '%s':%d\n",
115 event_name, gobj->id, media_localid(gobj),
116 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300117 break;
118 }
119 case MEDIA_GRAPH_INTF_DEVNODE:
120 {
121 struct media_interface *intf = gobj_to_intf(gobj);
122 struct media_intf_devnode *devnode = intf_to_devnode(intf);
123
124 dev_dbg(gobj->mdev->dev,
125 "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
126 event_name, gobj->id, media_localid(gobj),
127 intf_type(intf),
128 devnode->major, devnode->minor);
129 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300130 }
131 }
132#endif
133}
134
135/**
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300136 * media_gobj_init - Initialize a graph object
137 *
138 * @mdev: Pointer to the media_device that contains the object
139 * @type: Type of the object
140 * @gobj: Pointer to the object
141 *
142 * This routine initializes the embedded struct media_gobj inside a
143 * media graph object. It is called automatically if media_*_create()
144 * calls are used. However, if the object (entity, link, pad, interface)
145 * is embedded on some other object, this function should be called before
146 * registering the object at the media controller.
147 */
148void media_gobj_init(struct media_device *mdev,
149 enum media_gobj_type type,
150 struct media_gobj *gobj)
151{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300152 BUG_ON(!mdev);
153
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300154 gobj->mdev = mdev;
155
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300156 /* Create a per-type unique object ID */
157 switch (type) {
158 case MEDIA_GRAPH_ENTITY:
159 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
160 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300161 case MEDIA_GRAPH_PAD:
162 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
163 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300164 case MEDIA_GRAPH_LINK:
165 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
166 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300167 case MEDIA_GRAPH_INTF_DEVNODE:
168 gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
169 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300170 }
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300171 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300172}
173
174/**
175 * media_gobj_remove - Stop using a graph object on a media device
176 *
177 * @graph_obj: Pointer to the object
178 *
179 * This should be called at media_device_unregister_*() routines
180 */
181void media_gobj_remove(struct media_gobj *gobj)
182{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300183 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300184}
185
186/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300187 * media_entity_init - Initialize a media entity
188 *
189 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300190 * @pads: Array of 'num_pads' pads.
191 *
192 * The total number of pads is an intrinsic property of entities known by the
193 * entity driver, while the total number of links depends on hardware design
194 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300195 * use cases the number of links can safely be assumed to be equal to or
196 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300197 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300198 * For those reasons the links array can be preallocated based on the number
199 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300200 *
201 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300202 * 'num_pads' elements. The media_entity::max_links field will be set to the
203 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300204 *
205 * The pads array is managed by the entity driver and passed to
206 * media_entity_init() where its pointer will be stored in the entity structure.
207 */
208int
209media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300210 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300211{
212 struct media_link *links;
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300213 unsigned int max_links = num_pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300214 unsigned int i;
215
216 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
217 if (links == NULL)
218 return -ENOMEM;
219
220 entity->group_id = 0;
221 entity->max_links = max_links;
222 entity->num_links = 0;
223 entity->num_backlinks = 0;
224 entity->num_pads = num_pads;
225 entity->pads = pads;
226 entity->links = links;
227
228 for (i = 0; i < num_pads; i++) {
229 pads[i].entity = entity;
230 pads[i].index = i;
231 }
232
233 return 0;
234}
235EXPORT_SYMBOL_GPL(media_entity_init);
236
237void
238media_entity_cleanup(struct media_entity *entity)
239{
240 kfree(entity->links);
241}
242EXPORT_SYMBOL_GPL(media_entity_cleanup);
243
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300244/* -----------------------------------------------------------------------------
245 * Graph traversal
246 */
247
248static struct media_entity *
249media_entity_other(struct media_entity *entity, struct media_link *link)
250{
251 if (link->source->entity == entity)
252 return link->sink->entity;
253 else
254 return link->source->entity;
255}
256
257/* push an entity to traversal stack */
258static void stack_push(struct media_entity_graph *graph,
259 struct media_entity *entity)
260{
261 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
262 WARN_ON(1);
263 return;
264 }
265 graph->top++;
266 graph->stack[graph->top].link = 0;
267 graph->stack[graph->top].entity = entity;
268}
269
270static struct media_entity *stack_pop(struct media_entity_graph *graph)
271{
272 struct media_entity *entity;
273
274 entity = graph->stack[graph->top].entity;
275 graph->top--;
276
277 return entity;
278}
279
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300280#define link_top(en) ((en)->stack[(en)->top].link)
281#define stack_top(en) ((en)->stack[(en)->top].entity)
282
283/**
284 * media_entity_graph_walk_start - Start walking the media graph at a given entity
285 * @graph: Media graph structure that will be used to walk the graph
286 * @entity: Starting entity
287 *
288 * This function initializes the graph traversal structure to walk the entities
289 * graph starting at the given entity. The traversal structure must not be
290 * modified by the caller during graph traversal. When done the structure can
291 * safely be freed.
292 */
293void media_entity_graph_walk_start(struct media_entity_graph *graph,
294 struct media_entity *entity)
295{
296 graph->top = 0;
297 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300298 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
299
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300300 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300301 return;
302
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300303 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300304 stack_push(graph, entity);
305}
306EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
307
308/**
309 * media_entity_graph_walk_next - Get the next entity in the graph
310 * @graph: Media graph structure
311 *
312 * Perform a depth-first traversal of the given media entities graph.
313 *
314 * The graph structure must have been previously initialized with a call to
315 * media_entity_graph_walk_start().
316 *
317 * Return the next entity in the graph or NULL if the whole graph have been
318 * traversed.
319 */
320struct media_entity *
321media_entity_graph_walk_next(struct media_entity_graph *graph)
322{
323 if (stack_top(graph) == NULL)
324 return NULL;
325
326 /*
327 * Depth first search. Push entity to stack and continue from
328 * top of the stack until no more entities on the level can be
329 * found.
330 */
331 while (link_top(graph) < stack_top(graph)->num_links) {
332 struct media_entity *entity = stack_top(graph);
333 struct media_link *link = &entity->links[link_top(graph)];
334 struct media_entity *next;
335
336 /* The link is not enabled so we do not follow. */
337 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
338 link_top(graph)++;
339 continue;
340 }
341
342 /* Get the entity in the other end of the link . */
343 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300344 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300345 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300346
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300347 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300348 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300349 link_top(graph)++;
350 continue;
351 }
352
353 /* Push the new entity to stack and start over. */
354 link_top(graph)++;
355 stack_push(graph, next);
356 }
357
358 return stack_pop(graph);
359}
360EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
361
362/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300363 * Pipeline management
364 */
365
366/**
367 * media_entity_pipeline_start - Mark a pipeline as streaming
368 * @entity: Starting entity
369 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
370 *
371 * Mark all entities connected to a given entity through enabled links, either
372 * directly or indirectly, as streaming. The given pipeline object is assigned to
373 * every entity in the pipeline and stored in the media_entity pipe field.
374 *
375 * Calls to this function can be nested, in which case the same number of
376 * media_entity_pipeline_stop() calls will be required to stop streaming. The
377 * pipeline pointer must be identical for all nested calls to
378 * media_entity_pipeline_start().
379 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300380__must_check int media_entity_pipeline_start(struct media_entity *entity,
381 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300382{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300383 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300384 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300385 struct media_entity *entity_err = entity;
386 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300387
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))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300393 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
394 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300395 unsigned int i;
396
Laurent Pincharte02188c2010-08-25 09:00:41 -0300397 entity->stream_count++;
398 WARN_ON(entity->pipe && entity->pipe != pipe);
399 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300400
401 /* Already streaming --- no need to check. */
402 if (entity->stream_count > 1)
403 continue;
404
405 if (!entity->ops || !entity->ops->link_validate)
406 continue;
407
Sakari Ailusde49c282013-10-13 08:00:26 -0300408 bitmap_zero(active, entity->num_pads);
409 bitmap_fill(has_no_links, entity->num_pads);
410
Sakari Ailusaf88be32012-01-11 06:25:15 -0300411 for (i = 0; i < entity->num_links; i++) {
412 struct media_link *link = &entity->links[i];
Sakari Ailusde49c282013-10-13 08:00:26 -0300413 struct media_pad *pad = link->sink->entity == entity
414 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300415
Sakari Ailusde49c282013-10-13 08:00:26 -0300416 /* Mark that a pad is connected by a link. */
417 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300418
Sakari Ailusde49c282013-10-13 08:00:26 -0300419 /*
420 * Pads that either do not need to connect or
421 * are connected through an enabled link are
422 * fine.
423 */
424 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
425 link->flags & MEDIA_LNK_FL_ENABLED)
426 bitmap_set(active, pad->index, 1);
427
428 /*
429 * Link validation will only take place for
430 * sink ends of the link that are enabled.
431 */
432 if (link->sink != pad ||
433 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300434 continue;
435
436 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300437 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300438 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300439 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200440 link->source->entity->name,
441 link->source->index,
442 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300443 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300444 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300445 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300446
447 /* Either no links or validated links are fine. */
448 bitmap_or(active, active, has_no_links, entity->num_pads);
449
450 if (!bitmap_full(active, entity->num_pads)) {
451 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300452 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300453 "\"%s\":%u must be connected by an enabled link\n",
454 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300455 (unsigned)find_first_zero_bit(
456 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300457 goto error;
458 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300459 }
460
461 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300462
463 return 0;
464
465error:
466 /*
467 * Link validation on graph failed. We revert what we did and
468 * return the error.
469 */
470 media_entity_graph_walk_start(&graph, entity_err);
471
472 while ((entity_err = media_entity_graph_walk_next(&graph))) {
473 entity_err->stream_count--;
474 if (entity_err->stream_count == 0)
475 entity_err->pipe = NULL;
476
477 /*
478 * We haven't increased stream_count further than this
479 * so we quit here.
480 */
481 if (entity_err == entity)
482 break;
483 }
484
485 mutex_unlock(&mdev->graph_mutex);
486
487 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300488}
489EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
490
491/**
492 * media_entity_pipeline_stop - Mark a pipeline as not streaming
493 * @entity: Starting entity
494 *
495 * Mark all entities connected to a given entity through enabled links, either
496 * directly or indirectly, as not streaming. The media_entity pipe field is
497 * reset to NULL.
498 *
499 * If multiple calls to media_entity_pipeline_start() have been made, the same
500 * number of calls to this function are required to mark the pipeline as not
501 * streaming.
502 */
503void media_entity_pipeline_stop(struct media_entity *entity)
504{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300505 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300506 struct media_entity_graph graph;
507
508 mutex_lock(&mdev->graph_mutex);
509
510 media_entity_graph_walk_start(&graph, entity);
511
512 while ((entity = media_entity_graph_walk_next(&graph))) {
513 entity->stream_count--;
514 if (entity->stream_count == 0)
515 entity->pipe = NULL;
516 }
517
518 mutex_unlock(&mdev->graph_mutex);
519}
520EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
521
522/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300523 * Module use count
524 */
525
526/*
527 * media_entity_get - Get a reference to the parent module
528 * @entity: The entity
529 *
530 * Get a reference to the parent media device module.
531 *
532 * The function will return immediately if @entity is NULL.
533 *
534 * Return a pointer to the entity on success or NULL on failure.
535 */
536struct media_entity *media_entity_get(struct media_entity *entity)
537{
538 if (entity == NULL)
539 return NULL;
540
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300541 if (entity->graph_obj.mdev->dev &&
542 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300543 return NULL;
544
545 return entity;
546}
547EXPORT_SYMBOL_GPL(media_entity_get);
548
549/*
550 * media_entity_put - Release the reference to the parent module
551 * @entity: The entity
552 *
553 * Release the reference count acquired by media_entity_get().
554 *
555 * The function will return immediately if @entity is NULL.
556 */
557void media_entity_put(struct media_entity *entity)
558{
559 if (entity == NULL)
560 return;
561
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300562 if (entity->graph_obj.mdev->dev)
563 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300564}
565EXPORT_SYMBOL_GPL(media_entity_put);
566
567/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300568 * Links management
569 */
570
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300571static struct media_link *media_entity_add_link(struct media_entity *entity)
572{
573 if (entity->num_links >= entity->max_links) {
574 struct media_link *links = entity->links;
575 unsigned int max_links = entity->max_links + 2;
576 unsigned int i;
577
578 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
579 if (links == NULL)
580 return NULL;
581
582 for (i = 0; i < entity->num_links; i++)
583 links[i].reverse->reverse = &links[i];
584
585 entity->max_links = max_links;
586 entity->links = links;
587 }
588
589 return &entity->links[entity->num_links++];
590}
591
592int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300593media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300594 struct media_entity *sink, u16 sink_pad, u32 flags)
595{
596 struct media_link *link;
597 struct media_link *backlink;
598
599 BUG_ON(source == NULL || sink == NULL);
600 BUG_ON(source_pad >= source->num_pads);
601 BUG_ON(sink_pad >= sink->num_pads);
602
603 link = media_entity_add_link(source);
604 if (link == NULL)
605 return -ENOMEM;
606
607 link->source = &source->pads[source_pad];
608 link->sink = &sink->pads[sink_pad];
609 link->flags = flags;
610
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300611 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300612 media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
613 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300614
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300615 /* Create the backlink. Backlinks are used to help graph traversal and
616 * are not reported to userspace.
617 */
618 backlink = media_entity_add_link(sink);
619 if (backlink == NULL) {
620 source->num_links--;
621 return -ENOMEM;
622 }
623
624 backlink->source = &source->pads[source_pad];
625 backlink->sink = &sink->pads[sink_pad];
626 backlink->flags = flags;
627
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300628 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300629 media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
630 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300631
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300632 link->reverse = backlink;
633 backlink->reverse = link;
634
635 sink->num_backlinks++;
636
637 return 0;
638}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300639EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300640
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300641void __media_entity_remove_links(struct media_entity *entity)
642{
643 unsigned int i;
644
645 for (i = 0; i < entity->num_links; i++) {
646 struct media_link *link = &entity->links[i];
647 struct media_entity *remote;
648 unsigned int r = 0;
649
650 if (link->source->entity == entity)
651 remote = link->sink->entity;
652 else
653 remote = link->source->entity;
654
655 while (r < remote->num_links) {
656 struct media_link *rlink = &remote->links[r];
657
658 if (rlink != link->reverse) {
659 r++;
660 continue;
661 }
662
663 if (link->source->entity == entity)
664 remote->num_backlinks--;
665
666 if (--remote->num_links == 0)
667 break;
668
669 /* Insert last entry in place of the dropped link. */
670 *rlink = remote->links[remote->num_links];
671 }
672 }
673
674 entity->num_links = 0;
675 entity->num_backlinks = 0;
676}
677EXPORT_SYMBOL_GPL(__media_entity_remove_links);
678
679void media_entity_remove_links(struct media_entity *entity)
680{
681 /* Do nothing if the entity is not registered. */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300682 if (entity->graph_obj.mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300683 return;
684
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300685 mutex_lock(&entity->graph_obj.mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300686 __media_entity_remove_links(entity);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300687 mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300688}
689EXPORT_SYMBOL_GPL(media_entity_remove_links);
690
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300691static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
692{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300693 int ret;
694
695 /* Notify both entities. */
696 ret = media_entity_call(link->source->entity, link_setup,
697 link->source, link->sink, flags);
698 if (ret < 0 && ret != -ENOIOCTLCMD)
699 return ret;
700
701 ret = media_entity_call(link->sink->entity, link_setup,
702 link->sink, link->source, flags);
703 if (ret < 0 && ret != -ENOIOCTLCMD) {
704 media_entity_call(link->source->entity, link_setup,
705 link->source, link->sink, link->flags);
706 return ret;
707 }
708
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300709 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300710 link->reverse->flags = link->flags;
711
712 return 0;
713}
714
715/**
716 * __media_entity_setup_link - Configure a media link
717 * @link: The link being configured
718 * @flags: Link configuration flags
719 *
720 * The bulk of link setup is handled by the two entities connected through the
721 * link. This function notifies both entities of the link configuration change.
722 *
723 * If the link is immutable or if the current and new configuration are
724 * identical, return immediately.
725 *
726 * The user is expected to hold link->source->parent->mutex. If not,
727 * media_entity_setup_link() should be used instead.
728 */
729int __media_entity_setup_link(struct media_link *link, u32 flags)
730{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300731 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300732 struct media_device *mdev;
733 struct media_entity *source, *sink;
734 int ret = -EBUSY;
735
736 if (link == NULL)
737 return -EINVAL;
738
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300739 /* The non-modifiable link flags must not be modified. */
740 if ((link->flags & ~mask) != (flags & ~mask))
741 return -EINVAL;
742
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300743 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
744 return link->flags == flags ? 0 : -EINVAL;
745
746 if (link->flags == flags)
747 return 0;
748
749 source = link->source->entity;
750 sink = link->sink->entity;
751
Laurent Pincharte02188c2010-08-25 09:00:41 -0300752 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
753 (source->stream_count || sink->stream_count))
754 return -EBUSY;
755
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300756 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300757
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300758 if (mdev->link_notify) {
759 ret = mdev->link_notify(link, flags,
760 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300761 if (ret < 0)
762 return ret;
763 }
764
765 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300766
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300767 if (mdev->link_notify)
768 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300769
770 return ret;
771}
772
773int media_entity_setup_link(struct media_link *link, u32 flags)
774{
775 int ret;
776
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300777 mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300778 ret = __media_entity_setup_link(link, flags);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300779 mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300780
781 return ret;
782}
783EXPORT_SYMBOL_GPL(media_entity_setup_link);
784
785/**
786 * media_entity_find_link - Find a link between two pads
787 * @source: Source pad
788 * @sink: Sink pad
789 *
790 * Return a pointer to the link between the two entities. If no such link
791 * exists, return NULL.
792 */
793struct media_link *
794media_entity_find_link(struct media_pad *source, struct media_pad *sink)
795{
796 struct media_link *link;
797 unsigned int i;
798
799 for (i = 0; i < source->entity->num_links; ++i) {
800 link = &source->entity->links[i];
801
802 if (link->source->entity == source->entity &&
803 link->source->index == source->index &&
804 link->sink->entity == sink->entity &&
805 link->sink->index == sink->index)
806 return link;
807 }
808
809 return NULL;
810}
811EXPORT_SYMBOL_GPL(media_entity_find_link);
812
813/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300814 * media_entity_remote_pad - Find the pad at the remote end of a link
815 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300816 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300817 * Search for a remote pad connected to the given pad by iterating over all
818 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300819 *
820 * Return a pointer to the pad at the remote end of the first found enabled
821 * link, or NULL if no enabled link has been found.
822 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300823struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300824{
825 unsigned int i;
826
827 for (i = 0; i < pad->entity->num_links; i++) {
828 struct media_link *link = &pad->entity->links[i];
829
830 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
831 continue;
832
833 if (link->source == pad)
834 return link->sink;
835
836 if (link->sink == pad)
837 return link->source;
838 }
839
840 return NULL;
841
842}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300843EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300844
845
846/* Functions related to the media interface via device nodes */
847
848struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
849 u32 type, u32 flags,
850 u32 major, u32 minor,
851 gfp_t gfp_flags)
852{
853 struct media_intf_devnode *devnode;
854 struct media_interface *intf;
855
856 devnode = kzalloc(sizeof(*devnode), gfp_flags);
857 if (!devnode)
858 return NULL;
859
860 intf = &devnode->intf;
861
862 intf->type = type;
863 intf->flags = flags;
864
865 devnode->major = major;
866 devnode->minor = minor;
867
868 media_gobj_init(mdev, MEDIA_GRAPH_INTF_DEVNODE,
869 &devnode->intf.graph_obj);
870
871 return devnode;
872}
873EXPORT_SYMBOL_GPL(media_devnode_create);
874
875void media_devnode_remove(struct media_intf_devnode *devnode)
876{
877 media_gobj_remove(&devnode->intf.graph_obj);
878 kfree(devnode);
879}
880EXPORT_SYMBOL_GPL(media_devnode_remove);