blob: d15797e8a14216bf403c9130b0d623c116869c27 [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,
Mauro Carvalho Chehab3c0e2662015-08-21 08:45:34 -030097 "%s: id 0x%08x link#%d: %s#%d ==> %s#%d\n",
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030098 event_name, gobj->id, media_localid(gobj),
99
Mauro Carvalho Chehab3c0e2662015-08-21 08:45:34 -0300100 gobj_type(media_type(link->gobj0)),
101 media_localid(link->gobj0),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300102
Mauro Carvalho Chehab3c0e2662015-08-21 08:45:34 -0300103 gobj_type(media_type(link->gobj1)),
104 media_localid(link->gobj1));
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300105 break;
106 }
107 case MEDIA_GRAPH_PAD:
108 {
109 struct media_pad *pad = gobj_to_pad(gobj);
110
111 dev_dbg(gobj->mdev->dev,
112 "%s: id 0x%08x pad#%d: '%s':%d\n",
113 event_name, gobj->id, media_localid(gobj),
114 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300115 break;
116 }
117 case MEDIA_GRAPH_INTF_DEVNODE:
118 {
119 struct media_interface *intf = gobj_to_intf(gobj);
120 struct media_intf_devnode *devnode = intf_to_devnode(intf);
121
122 dev_dbg(gobj->mdev->dev,
123 "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
124 event_name, gobj->id, media_localid(gobj),
125 intf_type(intf),
126 devnode->major, devnode->minor);
127 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300128 }
129 }
130#endif
131}
132
133/**
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300134 * media_gobj_init - Initialize a graph object
135 *
136 * @mdev: Pointer to the media_device that contains the object
137 * @type: Type of the object
138 * @gobj: Pointer to the object
139 *
140 * This routine initializes the embedded struct media_gobj inside a
141 * media graph object. It is called automatically if media_*_create()
142 * calls are used. However, if the object (entity, link, pad, interface)
143 * is embedded on some other object, this function should be called before
144 * registering the object at the media controller.
145 */
146void media_gobj_init(struct media_device *mdev,
147 enum media_gobj_type type,
148 struct media_gobj *gobj)
149{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300150 BUG_ON(!mdev);
151
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300152 gobj->mdev = mdev;
153
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300154 /* Create a per-type unique object ID */
155 switch (type) {
156 case MEDIA_GRAPH_ENTITY:
157 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
158 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300159 case MEDIA_GRAPH_PAD:
160 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
161 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300162 case MEDIA_GRAPH_LINK:
163 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
164 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300165 case MEDIA_GRAPH_INTF_DEVNODE:
166 gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
167 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300168 }
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300169 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300170}
171
172/**
173 * media_gobj_remove - Stop using a graph object on a media device
174 *
175 * @graph_obj: Pointer to the object
176 *
177 * This should be called at media_device_unregister_*() routines
178 */
179void media_gobj_remove(struct media_gobj *gobj)
180{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300181 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300182}
183
184/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300185 * media_entity_init - Initialize a media entity
186 *
187 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300188 * @pads: Array of 'num_pads' pads.
189 *
190 * The total number of pads is an intrinsic property of entities known by the
191 * entity driver, while the total number of links depends on hardware design
192 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300193 * use cases the number of links can safely be assumed to be equal to or
194 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300195 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300196 * For those reasons the links array can be preallocated based on the number
197 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300198 *
199 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300200 * 'num_pads' elements. The media_entity::max_links field will be set to the
201 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300202 *
203 * The pads array is managed by the entity driver and passed to
204 * media_entity_init() where its pointer will be stored in the entity structure.
205 */
206int
207media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300208 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300209{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300210 unsigned int i;
211
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300212 entity->group_id = 0;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300213 entity->num_links = 0;
214 entity->num_backlinks = 0;
215 entity->num_pads = num_pads;
216 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300217
218 for (i = 0; i < num_pads; i++) {
219 pads[i].entity = entity;
220 pads[i].index = i;
221 }
222
223 return 0;
224}
225EXPORT_SYMBOL_GPL(media_entity_init);
226
227void
228media_entity_cleanup(struct media_entity *entity)
229{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300230 struct media_link *link, *tmp;
231
232 list_for_each_entry_safe(link, tmp, &entity->links, list) {
233 media_gobj_remove(&link->graph_obj);
234 list_del(&link->list);
235 kfree(link);
236 }
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300237}
238EXPORT_SYMBOL_GPL(media_entity_cleanup);
239
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300240/* -----------------------------------------------------------------------------
241 * Graph traversal
242 */
243
244static struct media_entity *
245media_entity_other(struct media_entity *entity, struct media_link *link)
246{
247 if (link->source->entity == entity)
248 return link->sink->entity;
249 else
250 return link->source->entity;
251}
252
253/* push an entity to traversal stack */
254static void stack_push(struct media_entity_graph *graph,
255 struct media_entity *entity)
256{
257 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
258 WARN_ON(1);
259 return;
260 }
261 graph->top++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300262 graph->stack[graph->top].link = (&entity->links)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300263 graph->stack[graph->top].entity = entity;
264}
265
266static struct media_entity *stack_pop(struct media_entity_graph *graph)
267{
268 struct media_entity *entity;
269
270 entity = graph->stack[graph->top].entity;
271 graph->top--;
272
273 return entity;
274}
275
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300276#define link_top(en) ((en)->stack[(en)->top].link)
277#define stack_top(en) ((en)->stack[(en)->top].entity)
278
279/**
280 * media_entity_graph_walk_start - Start walking the media graph at a given entity
281 * @graph: Media graph structure that will be used to walk the graph
282 * @entity: Starting entity
283 *
284 * This function initializes the graph traversal structure to walk the entities
285 * graph starting at the given entity. The traversal structure must not be
286 * modified by the caller during graph traversal. When done the structure can
287 * safely be freed.
288 */
289void media_entity_graph_walk_start(struct media_entity_graph *graph,
290 struct media_entity *entity)
291{
292 graph->top = 0;
293 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300294 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
295
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300296 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300297 return;
298
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300299 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300300 stack_push(graph, entity);
301}
302EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
303
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300304
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300305/**
306 * media_entity_graph_walk_next - Get the next entity in the graph
307 * @graph: Media graph structure
308 *
309 * Perform a depth-first traversal of the given media entities graph.
310 *
311 * The graph structure must have been previously initialized with a call to
312 * media_entity_graph_walk_start().
313 *
314 * Return the next entity in the graph or NULL if the whole graph have been
315 * traversed.
316 */
317struct media_entity *
318media_entity_graph_walk_next(struct media_entity_graph *graph)
319{
320 if (stack_top(graph) == NULL)
321 return NULL;
322
323 /*
324 * Depth first search. Push entity to stack and continue from
325 * top of the stack until no more entities on the level can be
326 * found.
327 */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300328 while (link_top(graph) != &(stack_top(graph)->links)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300329 struct media_entity *entity = stack_top(graph);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300330 struct media_link *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300331 struct media_entity *next;
332
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300333 link = list_entry(link_top(graph), typeof(*link), list);
334
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300335 /* The link is not enabled so we do not follow. */
336 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300337 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300338 continue;
339 }
340
341 /* Get the entity in the other end of the link . */
342 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300343 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300344 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300345
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300346 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300347 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300348 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300349 continue;
350 }
351
352 /* Push the new entity to stack and start over. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300353 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300354 stack_push(graph, next);
355 }
356
357 return stack_pop(graph);
358}
359EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
360
361/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300362 * Pipeline management
363 */
364
365/**
366 * media_entity_pipeline_start - Mark a pipeline as streaming
367 * @entity: Starting entity
368 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
369 *
370 * Mark all entities connected to a given entity through enabled links, either
371 * directly or indirectly, as streaming. The given pipeline object is assigned to
372 * every entity in the pipeline and stored in the media_entity pipe field.
373 *
374 * Calls to this function can be nested, in which case the same number of
375 * media_entity_pipeline_stop() calls will be required to stop streaming. The
376 * pipeline pointer must be identical for all nested calls to
377 * media_entity_pipeline_start().
378 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300379__must_check int media_entity_pipeline_start(struct media_entity *entity,
380 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300381{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300382 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300383 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300384 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300385 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300386 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
Laurent Pincharte02188c2010-08-25 09:00:41 -0300396 entity->stream_count++;
397 WARN_ON(entity->pipe && entity->pipe != pipe);
398 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300399
400 /* Already streaming --- no need to check. */
401 if (entity->stream_count > 1)
402 continue;
403
404 if (!entity->ops || !entity->ops->link_validate)
405 continue;
406
Sakari Ailusde49c282013-10-13 08:00:26 -0300407 bitmap_zero(active, entity->num_pads);
408 bitmap_fill(has_no_links, entity->num_pads);
409
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300410 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300411 struct media_pad *pad = link->sink->entity == entity
412 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300413
Sakari Ailusde49c282013-10-13 08:00:26 -0300414 /* Mark that a pad is connected by a link. */
415 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300416
Sakari Ailusde49c282013-10-13 08:00:26 -0300417 /*
418 * Pads that either do not need to connect or
419 * are connected through an enabled link are
420 * fine.
421 */
422 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
423 link->flags & MEDIA_LNK_FL_ENABLED)
424 bitmap_set(active, pad->index, 1);
425
426 /*
427 * Link validation will only take place for
428 * sink ends of the link that are enabled.
429 */
430 if (link->sink != pad ||
431 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300432 continue;
433
434 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300435 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300436 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300437 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200438 link->source->entity->name,
439 link->source->index,
440 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300441 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300442 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300443 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300444
445 /* Either no links or validated links are fine. */
446 bitmap_or(active, active, has_no_links, entity->num_pads);
447
448 if (!bitmap_full(active, entity->num_pads)) {
449 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300450 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300451 "\"%s\":%u must be connected by an enabled link\n",
452 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300453 (unsigned)find_first_zero_bit(
454 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300455 goto error;
456 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300457 }
458
459 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300460
461 return 0;
462
463error:
464 /*
465 * Link validation on graph failed. We revert what we did and
466 * return the error.
467 */
468 media_entity_graph_walk_start(&graph, entity_err);
469
470 while ((entity_err = media_entity_graph_walk_next(&graph))) {
471 entity_err->stream_count--;
472 if (entity_err->stream_count == 0)
473 entity_err->pipe = NULL;
474
475 /*
476 * We haven't increased stream_count further than this
477 * so we quit here.
478 */
479 if (entity_err == entity)
480 break;
481 }
482
483 mutex_unlock(&mdev->graph_mutex);
484
485 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300486}
487EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
488
489/**
490 * media_entity_pipeline_stop - Mark a pipeline as not streaming
491 * @entity: Starting entity
492 *
493 * Mark all entities connected to a given entity through enabled links, either
494 * directly or indirectly, as not streaming. The media_entity pipe field is
495 * reset to NULL.
496 *
497 * If multiple calls to media_entity_pipeline_start() have been made, the same
498 * number of calls to this function are required to mark the pipeline as not
499 * streaming.
500 */
501void media_entity_pipeline_stop(struct media_entity *entity)
502{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300503 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300504 struct media_entity_graph graph;
505
506 mutex_lock(&mdev->graph_mutex);
507
508 media_entity_graph_walk_start(&graph, entity);
509
510 while ((entity = media_entity_graph_walk_next(&graph))) {
511 entity->stream_count--;
512 if (entity->stream_count == 0)
513 entity->pipe = NULL;
514 }
515
516 mutex_unlock(&mdev->graph_mutex);
517}
518EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
519
520/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300521 * Module use count
522 */
523
524/*
525 * media_entity_get - Get a reference to the parent module
526 * @entity: The entity
527 *
528 * Get a reference to the parent media device module.
529 *
530 * The function will return immediately if @entity is NULL.
531 *
532 * Return a pointer to the entity on success or NULL on failure.
533 */
534struct media_entity *media_entity_get(struct media_entity *entity)
535{
536 if (entity == NULL)
537 return NULL;
538
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300539 if (entity->graph_obj.mdev->dev &&
540 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300541 return NULL;
542
543 return entity;
544}
545EXPORT_SYMBOL_GPL(media_entity_get);
546
547/*
548 * media_entity_put - Release the reference to the parent module
549 * @entity: The entity
550 *
551 * Release the reference count acquired by media_entity_get().
552 *
553 * The function will return immediately if @entity is NULL.
554 */
555void media_entity_put(struct media_entity *entity)
556{
557 if (entity == NULL)
558 return;
559
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300560 if (entity->graph_obj.mdev->dev)
561 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300562}
563EXPORT_SYMBOL_GPL(media_entity_put);
564
565/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300566 * Links management
567 */
568
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300569static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300570{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300571 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300572
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300573 link = kzalloc(sizeof(*link), GFP_KERNEL);
574 if (link == NULL)
575 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300576
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300577 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300578
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300579 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300580}
581
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300582static void __media_entity_remove_link(struct media_entity *entity,
583 struct media_link *link);
584
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300585int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300586media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300587 struct media_entity *sink, u16 sink_pad, u32 flags)
588{
589 struct media_link *link;
590 struct media_link *backlink;
591
592 BUG_ON(source == NULL || sink == NULL);
593 BUG_ON(source_pad >= source->num_pads);
594 BUG_ON(sink_pad >= sink->num_pads);
595
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300596 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300597 if (link == NULL)
598 return -ENOMEM;
599
600 link->source = &source->pads[source_pad];
601 link->sink = &sink->pads[sink_pad];
602 link->flags = flags;
603
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300604 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300605 media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
606 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300607
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300608 /* Create the backlink. Backlinks are used to help graph traversal and
609 * are not reported to userspace.
610 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300611 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300612 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300613 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300614 return -ENOMEM;
615 }
616
617 backlink->source = &source->pads[source_pad];
618 backlink->sink = &sink->pads[sink_pad];
619 backlink->flags = flags;
620
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300621 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300622 media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
623 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300624
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300625 link->reverse = backlink;
626 backlink->reverse = link;
627
628 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300629 sink->num_links++;
630 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300631
632 return 0;
633}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300634EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300635
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300636static void __media_entity_remove_link(struct media_entity *entity,
637 struct media_link *link)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300638{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300639 struct media_link *rlink, *tmp;
640 struct media_entity *remote;
641 unsigned int r = 0;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300642
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300643 if (link->source->entity == entity)
644 remote = link->sink->entity;
645 else
646 remote = link->source->entity;
647
648 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
649 if (rlink != link->reverse) {
650 r++;
651 continue;
652 }
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300653
654 if (link->source->entity == entity)
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300655 remote->num_backlinks--;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300656
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300657 if (--remote->num_links == 0)
658 break;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300659
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300660 /* Remove the remote link */
661 list_del(&rlink->list);
662 kfree(rlink);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300663 }
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300664 list_del(&link->list);
665 kfree(link);
666}
667
668void __media_entity_remove_links(struct media_entity *entity)
669{
670 struct media_link *link, *tmp;
671
672 list_for_each_entry_safe(link, tmp, &entity->links, list)
673 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300674
675 entity->num_links = 0;
676 entity->num_backlinks = 0;
677}
678EXPORT_SYMBOL_GPL(__media_entity_remove_links);
679
680void media_entity_remove_links(struct media_entity *entity)
681{
682 /* Do nothing if the entity is not registered. */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300683 if (entity->graph_obj.mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300684 return;
685
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300686 mutex_lock(&entity->graph_obj.mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300687 __media_entity_remove_links(entity);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300688 mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300689}
690EXPORT_SYMBOL_GPL(media_entity_remove_links);
691
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300692static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
693{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300694 int ret;
695
696 /* Notify both entities. */
697 ret = media_entity_call(link->source->entity, link_setup,
698 link->source, link->sink, flags);
699 if (ret < 0 && ret != -ENOIOCTLCMD)
700 return ret;
701
702 ret = media_entity_call(link->sink->entity, link_setup,
703 link->sink, link->source, flags);
704 if (ret < 0 && ret != -ENOIOCTLCMD) {
705 media_entity_call(link->source->entity, link_setup,
706 link->source, link->sink, link->flags);
707 return ret;
708 }
709
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300710 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300711 link->reverse->flags = link->flags;
712
713 return 0;
714}
715
716/**
717 * __media_entity_setup_link - Configure a media link
718 * @link: The link being configured
719 * @flags: Link configuration flags
720 *
721 * The bulk of link setup is handled by the two entities connected through the
722 * link. This function notifies both entities of the link configuration change.
723 *
724 * If the link is immutable or if the current and new configuration are
725 * identical, return immediately.
726 *
727 * The user is expected to hold link->source->parent->mutex. If not,
728 * media_entity_setup_link() should be used instead.
729 */
730int __media_entity_setup_link(struct media_link *link, u32 flags)
731{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300732 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300733 struct media_device *mdev;
734 struct media_entity *source, *sink;
735 int ret = -EBUSY;
736
737 if (link == NULL)
738 return -EINVAL;
739
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300740 /* The non-modifiable link flags must not be modified. */
741 if ((link->flags & ~mask) != (flags & ~mask))
742 return -EINVAL;
743
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300744 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
745 return link->flags == flags ? 0 : -EINVAL;
746
747 if (link->flags == flags)
748 return 0;
749
750 source = link->source->entity;
751 sink = link->sink->entity;
752
Laurent Pincharte02188c2010-08-25 09:00:41 -0300753 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
754 (source->stream_count || sink->stream_count))
755 return -EBUSY;
756
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300757 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300758
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300759 if (mdev->link_notify) {
760 ret = mdev->link_notify(link, flags,
761 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300762 if (ret < 0)
763 return ret;
764 }
765
766 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300767
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300768 if (mdev->link_notify)
769 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300770
771 return ret;
772}
773
774int media_entity_setup_link(struct media_link *link, u32 flags)
775{
776 int ret;
777
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300778 mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300779 ret = __media_entity_setup_link(link, flags);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300780 mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300781
782 return ret;
783}
784EXPORT_SYMBOL_GPL(media_entity_setup_link);
785
786/**
787 * media_entity_find_link - Find a link between two pads
788 * @source: Source pad
789 * @sink: Sink pad
790 *
791 * Return a pointer to the link between the two entities. If no such link
792 * exists, return NULL.
793 */
794struct media_link *
795media_entity_find_link(struct media_pad *source, struct media_pad *sink)
796{
797 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300798
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300799 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300800 if (link->source->entity == source->entity &&
801 link->source->index == source->index &&
802 link->sink->entity == sink->entity &&
803 link->sink->index == sink->index)
804 return link;
805 }
806
807 return NULL;
808}
809EXPORT_SYMBOL_GPL(media_entity_find_link);
810
811/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300812 * media_entity_remote_pad - Find the pad at the remote end of a link
813 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300814 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300815 * Search for a remote pad connected to the given pad by iterating over all
816 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300817 *
818 * Return a pointer to the pad at the remote end of the first found enabled
819 * link, or NULL if no enabled link has been found.
820 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300821struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300822{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300823 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300824
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300825 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300826 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
827 continue;
828
829 if (link->source == pad)
830 return link->sink;
831
832 if (link->sink == pad)
833 return link->source;
834 }
835
836 return NULL;
837
838}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300839EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300840
841
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300842static void media_interface_init(struct media_device *mdev,
843 struct media_interface *intf,
844 u32 gobj_type,
845 u32 intf_type, u32 flags)
846{
847 intf->type = intf_type;
848 intf->flags = flags;
849 INIT_LIST_HEAD(&intf->links);
850
851 media_gobj_init(mdev, gobj_type, &intf->graph_obj);
852}
853
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300854/* Functions related to the media interface via device nodes */
855
856struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
857 u32 type, u32 flags,
858 u32 major, u32 minor,
859 gfp_t gfp_flags)
860{
861 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300862
863 devnode = kzalloc(sizeof(*devnode), gfp_flags);
864 if (!devnode)
865 return NULL;
866
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300867 devnode->major = major;
868 devnode->minor = minor;
869
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300870 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
871 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300872
873 return devnode;
874}
875EXPORT_SYMBOL_GPL(media_devnode_create);
876
877void media_devnode_remove(struct media_intf_devnode *devnode)
878{
879 media_gobj_remove(&devnode->intf.graph_obj);
880 kfree(devnode);
881}
882EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300883
884struct media_link *media_create_intf_link(struct media_entity *entity,
885 struct media_interface *intf,
886 u32 flags)
887{
888 struct media_link *link;
889
890 link = media_add_link(&intf->links);
891 if (link == NULL)
892 return NULL;
893
894 link->intf = intf;
895 link->entity = entity;
896 link->flags = flags;
897
898 /* Initialize graph object embedded at the new link */
899 media_gobj_init(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
900 &link->graph_obj);
901
902 return link;
903}
904EXPORT_SYMBOL_GPL(media_create_intf_link);
905
906
907static void __media_remove_intf_link(struct media_link *link)
908{
909 media_gobj_remove(&link->graph_obj);
910 kfree(link);
911}
912
913void media_remove_intf_link(struct media_link *link)
914{
915 mutex_lock(&link->graph_obj.mdev->graph_mutex);
916 __media_remove_intf_link(link);
917 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
918}
919EXPORT_SYMBOL_GPL(media_remove_intf_link);