blob: b1390d843909ba236d7093602d85fd0e380e8f56 [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,
Mauro Carvalho Chehab6c24d462015-08-21 18:26:42 -0300112 "%s: id 0x%08x %s%spad#%d: '%s':%d\n",
113 event_name, gobj->id,
114 pad->flags & MEDIA_PAD_FL_SINK ? " sink " : "",
115 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
116 media_localid(gobj),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300117 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300118 break;
119 }
120 case MEDIA_GRAPH_INTF_DEVNODE:
121 {
122 struct media_interface *intf = gobj_to_intf(gobj);
123 struct media_intf_devnode *devnode = intf_to_devnode(intf);
124
125 dev_dbg(gobj->mdev->dev,
126 "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
127 event_name, gobj->id, media_localid(gobj),
128 intf_type(intf),
129 devnode->major, devnode->minor);
130 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300131 }
132 }
133#endif
134}
135
136/**
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300137 * media_gobj_init - Initialize a graph object
138 *
139 * @mdev: Pointer to the media_device that contains the object
140 * @type: Type of the object
141 * @gobj: Pointer to the object
142 *
143 * This routine initializes the embedded struct media_gobj inside a
144 * media graph object. It is called automatically if media_*_create()
145 * calls are used. However, if the object (entity, link, pad, interface)
146 * is embedded on some other object, this function should be called before
147 * registering the object at the media controller.
148 */
149void media_gobj_init(struct media_device *mdev,
150 enum media_gobj_type type,
151 struct media_gobj *gobj)
152{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300153 BUG_ON(!mdev);
154
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300155 gobj->mdev = mdev;
156
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300157 /* Create a per-type unique object ID */
158 switch (type) {
159 case MEDIA_GRAPH_ENTITY:
160 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300161 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300162 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300163 case MEDIA_GRAPH_PAD:
164 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300165 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300166 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300167 case MEDIA_GRAPH_LINK:
168 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300169 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300170 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300171 case MEDIA_GRAPH_INTF_DEVNODE:
172 gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300173 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300174 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300175 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300176
177 mdev->topology_version++;
178
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300179 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300180}
181
182/**
183 * media_gobj_remove - Stop using a graph object on a media device
184 *
185 * @graph_obj: Pointer to the object
186 *
187 * This should be called at media_device_unregister_*() routines
188 */
189void media_gobj_remove(struct media_gobj *gobj)
190{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300191 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300192
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300193 gobj->mdev->topology_version++;
194
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300195 /* Remove the object from mdev list */
196 list_del(&gobj->list);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300197}
198
199/**
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300200 * media_entity_init - Initialize a media entity
201 *
202 * @num_pads: Total number of sink and source pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300203 * @pads: Array of 'num_pads' pads.
204 *
205 * The total number of pads is an intrinsic property of entities known by the
206 * entity driver, while the total number of links depends on hardware design
207 * and is an extrinsic property unknown to the entity driver. However, in most
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300208 * use cases the number of links can safely be assumed to be equal to or
209 * larger than the number of pads.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300210 *
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300211 * For those reasons the links array can be preallocated based on the number
212 * of pads and will be reallocated later if extra links need to be created.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300213 *
214 * This function allocates a links array with enough space to hold at least
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300215 * 'num_pads' elements. The media_entity::max_links field will be set to the
216 * number of allocated elements.
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300217 *
218 * The pads array is managed by the entity driver and passed to
219 * media_entity_init() where its pointer will be stored in the entity structure.
220 */
221int
222media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300223 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300224{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300225 unsigned int i;
226
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300227 entity->group_id = 0;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300228 entity->num_links = 0;
229 entity->num_backlinks = 0;
230 entity->num_pads = num_pads;
231 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300232
233 for (i = 0; i < num_pads; i++) {
234 pads[i].entity = entity;
235 pads[i].index = i;
236 }
237
238 return 0;
239}
240EXPORT_SYMBOL_GPL(media_entity_init);
241
242void
243media_entity_cleanup(struct media_entity *entity)
244{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300245 struct media_link *link, *tmp;
246
247 list_for_each_entry_safe(link, tmp, &entity->links, list) {
248 media_gobj_remove(&link->graph_obj);
249 list_del(&link->list);
250 kfree(link);
251 }
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300252}
253EXPORT_SYMBOL_GPL(media_entity_cleanup);
254
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300255/* -----------------------------------------------------------------------------
256 * Graph traversal
257 */
258
259static struct media_entity *
260media_entity_other(struct media_entity *entity, struct media_link *link)
261{
262 if (link->source->entity == entity)
263 return link->sink->entity;
264 else
265 return link->source->entity;
266}
267
268/* push an entity to traversal stack */
269static void stack_push(struct media_entity_graph *graph,
270 struct media_entity *entity)
271{
272 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
273 WARN_ON(1);
274 return;
275 }
276 graph->top++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300277 graph->stack[graph->top].link = (&entity->links)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300278 graph->stack[graph->top].entity = entity;
279}
280
281static struct media_entity *stack_pop(struct media_entity_graph *graph)
282{
283 struct media_entity *entity;
284
285 entity = graph->stack[graph->top].entity;
286 graph->top--;
287
288 return entity;
289}
290
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300291#define link_top(en) ((en)->stack[(en)->top].link)
292#define stack_top(en) ((en)->stack[(en)->top].entity)
293
294/**
295 * media_entity_graph_walk_start - Start walking the media graph at a given entity
296 * @graph: Media graph structure that will be used to walk the graph
297 * @entity: Starting entity
298 *
299 * This function initializes the graph traversal structure to walk the entities
300 * graph starting at the given entity. The traversal structure must not be
301 * modified by the caller during graph traversal. When done the structure can
302 * safely be freed.
303 */
304void media_entity_graph_walk_start(struct media_entity_graph *graph,
305 struct media_entity *entity)
306{
307 graph->top = 0;
308 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300309 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
310
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300311 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300312 return;
313
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300314 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300315 stack_push(graph, entity);
316}
317EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
318
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300319
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300320/**
321 * media_entity_graph_walk_next - Get the next entity in the graph
322 * @graph: Media graph structure
323 *
324 * Perform a depth-first traversal of the given media entities graph.
325 *
326 * The graph structure must have been previously initialized with a call to
327 * media_entity_graph_walk_start().
328 *
329 * Return the next entity in the graph or NULL if the whole graph have been
330 * traversed.
331 */
332struct media_entity *
333media_entity_graph_walk_next(struct media_entity_graph *graph)
334{
335 if (stack_top(graph) == NULL)
336 return NULL;
337
338 /*
339 * Depth first search. Push entity to stack and continue from
340 * top of the stack until no more entities on the level can be
341 * found.
342 */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300343 while (link_top(graph) != &(stack_top(graph)->links)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300344 struct media_entity *entity = stack_top(graph);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300345 struct media_link *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300346 struct media_entity *next;
347
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300348 link = list_entry(link_top(graph), typeof(*link), list);
349
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300350 /* The link is not enabled so we do not follow. */
351 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300352 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300353 continue;
354 }
355
356 /* Get the entity in the other end of the link . */
357 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300358 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300359 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300360
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300361 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300362 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300363 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300364 continue;
365 }
366
367 /* Push the new entity to stack and start over. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300368 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300369 stack_push(graph, next);
370 }
371
372 return stack_pop(graph);
373}
374EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
375
376/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300377 * Pipeline management
378 */
379
380/**
381 * media_entity_pipeline_start - Mark a pipeline as streaming
382 * @entity: Starting entity
383 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
384 *
385 * Mark all entities connected to a given entity through enabled links, either
386 * directly or indirectly, as streaming. The given pipeline object is assigned to
387 * every entity in the pipeline and stored in the media_entity pipe field.
388 *
389 * Calls to this function can be nested, in which case the same number of
390 * media_entity_pipeline_stop() calls will be required to stop streaming. The
391 * pipeline pointer must be identical for all nested calls to
392 * media_entity_pipeline_start().
393 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300394__must_check int media_entity_pipeline_start(struct media_entity *entity,
395 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300396{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300397 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300398 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300399 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300400 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300401 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300402
403 mutex_lock(&mdev->graph_mutex);
404
405 media_entity_graph_walk_start(&graph, entity);
406
407 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300408 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
409 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300410
Laurent Pincharte02188c2010-08-25 09:00:41 -0300411 entity->stream_count++;
412 WARN_ON(entity->pipe && entity->pipe != pipe);
413 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300414
415 /* Already streaming --- no need to check. */
416 if (entity->stream_count > 1)
417 continue;
418
419 if (!entity->ops || !entity->ops->link_validate)
420 continue;
421
Sakari Ailusde49c282013-10-13 08:00:26 -0300422 bitmap_zero(active, entity->num_pads);
423 bitmap_fill(has_no_links, entity->num_pads);
424
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300425 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300426 struct media_pad *pad = link->sink->entity == entity
427 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300428
Sakari Ailusde49c282013-10-13 08:00:26 -0300429 /* Mark that a pad is connected by a link. */
430 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300431
Sakari Ailusde49c282013-10-13 08:00:26 -0300432 /*
433 * Pads that either do not need to connect or
434 * are connected through an enabled link are
435 * fine.
436 */
437 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
438 link->flags & MEDIA_LNK_FL_ENABLED)
439 bitmap_set(active, pad->index, 1);
440
441 /*
442 * Link validation will only take place for
443 * sink ends of the link that are enabled.
444 */
445 if (link->sink != pad ||
446 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300447 continue;
448
449 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300450 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300451 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300452 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200453 link->source->entity->name,
454 link->source->index,
455 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300456 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300457 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300458 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300459
460 /* Either no links or validated links are fine. */
461 bitmap_or(active, active, has_no_links, entity->num_pads);
462
463 if (!bitmap_full(active, entity->num_pads)) {
464 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300465 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300466 "\"%s\":%u must be connected by an enabled link\n",
467 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300468 (unsigned)find_first_zero_bit(
469 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300470 goto error;
471 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300472 }
473
474 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300475
476 return 0;
477
478error:
479 /*
480 * Link validation on graph failed. We revert what we did and
481 * return the error.
482 */
483 media_entity_graph_walk_start(&graph, entity_err);
484
485 while ((entity_err = media_entity_graph_walk_next(&graph))) {
486 entity_err->stream_count--;
487 if (entity_err->stream_count == 0)
488 entity_err->pipe = NULL;
489
490 /*
491 * We haven't increased stream_count further than this
492 * so we quit here.
493 */
494 if (entity_err == entity)
495 break;
496 }
497
498 mutex_unlock(&mdev->graph_mutex);
499
500 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300501}
502EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
503
504/**
505 * media_entity_pipeline_stop - Mark a pipeline as not streaming
506 * @entity: Starting entity
507 *
508 * Mark all entities connected to a given entity through enabled links, either
509 * directly or indirectly, as not streaming. The media_entity pipe field is
510 * reset to NULL.
511 *
512 * If multiple calls to media_entity_pipeline_start() have been made, the same
513 * number of calls to this function are required to mark the pipeline as not
514 * streaming.
515 */
516void media_entity_pipeline_stop(struct media_entity *entity)
517{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300518 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300519 struct media_entity_graph graph;
520
521 mutex_lock(&mdev->graph_mutex);
522
523 media_entity_graph_walk_start(&graph, entity);
524
525 while ((entity = media_entity_graph_walk_next(&graph))) {
526 entity->stream_count--;
527 if (entity->stream_count == 0)
528 entity->pipe = NULL;
529 }
530
531 mutex_unlock(&mdev->graph_mutex);
532}
533EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
534
535/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300536 * Module use count
537 */
538
539/*
540 * media_entity_get - Get a reference to the parent module
541 * @entity: The entity
542 *
543 * Get a reference to the parent media device module.
544 *
545 * The function will return immediately if @entity is NULL.
546 *
547 * Return a pointer to the entity on success or NULL on failure.
548 */
549struct media_entity *media_entity_get(struct media_entity *entity)
550{
551 if (entity == NULL)
552 return NULL;
553
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300554 if (entity->graph_obj.mdev->dev &&
555 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300556 return NULL;
557
558 return entity;
559}
560EXPORT_SYMBOL_GPL(media_entity_get);
561
562/*
563 * media_entity_put - Release the reference to the parent module
564 * @entity: The entity
565 *
566 * Release the reference count acquired by media_entity_get().
567 *
568 * The function will return immediately if @entity is NULL.
569 */
570void media_entity_put(struct media_entity *entity)
571{
572 if (entity == NULL)
573 return;
574
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300575 if (entity->graph_obj.mdev->dev)
576 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300577}
578EXPORT_SYMBOL_GPL(media_entity_put);
579
580/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300581 * Links management
582 */
583
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300584static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300585{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300586 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300587
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300588 link = kzalloc(sizeof(*link), GFP_KERNEL);
589 if (link == NULL)
590 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300591
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300592 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300593
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300594 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300595}
596
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300597static void __media_entity_remove_link(struct media_entity *entity,
598 struct media_link *link);
599
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300600int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300601media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300602 struct media_entity *sink, u16 sink_pad, u32 flags)
603{
604 struct media_link *link;
605 struct media_link *backlink;
606
607 BUG_ON(source == NULL || sink == NULL);
608 BUG_ON(source_pad >= source->num_pads);
609 BUG_ON(sink_pad >= sink->num_pads);
610
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300611 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300612 if (link == NULL)
613 return -ENOMEM;
614
615 link->source = &source->pads[source_pad];
616 link->sink = &sink->pads[sink_pad];
617 link->flags = flags;
618
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300619 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300620 media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
621 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300622
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300623 /* Create the backlink. Backlinks are used to help graph traversal and
624 * are not reported to userspace.
625 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300626 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300627 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300628 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300629 return -ENOMEM;
630 }
631
632 backlink->source = &source->pads[source_pad];
633 backlink->sink = &sink->pads[sink_pad];
634 backlink->flags = flags;
635
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300636 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300637 media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
638 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300639
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300640 link->reverse = backlink;
641 backlink->reverse = link;
642
643 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300644 sink->num_links++;
645 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300646
647 return 0;
648}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300649EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300650
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300651static void __media_entity_remove_link(struct media_entity *entity,
652 struct media_link *link)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300653{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300654 struct media_link *rlink, *tmp;
655 struct media_entity *remote;
656 unsigned int r = 0;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300657
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300658 if (link->source->entity == entity)
659 remote = link->sink->entity;
660 else
661 remote = link->source->entity;
662
663 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
664 if (rlink != link->reverse) {
665 r++;
666 continue;
667 }
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300668
669 if (link->source->entity == entity)
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300670 remote->num_backlinks--;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300671
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300672 if (--remote->num_links == 0)
673 break;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300674
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300675 /* Remove the remote link */
676 list_del(&rlink->list);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300677 media_gobj_remove(&rlink->graph_obj);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300678 kfree(rlink);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300679 }
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300680 list_del(&link->list);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300681 media_gobj_remove(&link->graph_obj);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300682 kfree(link);
683}
684
685void __media_entity_remove_links(struct media_entity *entity)
686{
687 struct media_link *link, *tmp;
688
689 list_for_each_entry_safe(link, tmp, &entity->links, list)
690 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300691
692 entity->num_links = 0;
693 entity->num_backlinks = 0;
694}
695EXPORT_SYMBOL_GPL(__media_entity_remove_links);
696
697void media_entity_remove_links(struct media_entity *entity)
698{
699 /* Do nothing if the entity is not registered. */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300700 if (entity->graph_obj.mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300701 return;
702
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200703 spin_lock(&entity->graph_obj.mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300704 __media_entity_remove_links(entity);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200705 spin_unlock(&entity->graph_obj.mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300706}
707EXPORT_SYMBOL_GPL(media_entity_remove_links);
708
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300709static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
710{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300711 int ret;
712
713 /* Notify both entities. */
714 ret = media_entity_call(link->source->entity, link_setup,
715 link->source, link->sink, flags);
716 if (ret < 0 && ret != -ENOIOCTLCMD)
717 return ret;
718
719 ret = media_entity_call(link->sink->entity, link_setup,
720 link->sink, link->source, flags);
721 if (ret < 0 && ret != -ENOIOCTLCMD) {
722 media_entity_call(link->source->entity, link_setup,
723 link->source, link->sink, link->flags);
724 return ret;
725 }
726
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300727 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300728 link->reverse->flags = link->flags;
729
730 return 0;
731}
732
733/**
734 * __media_entity_setup_link - Configure a media link
735 * @link: The link being configured
736 * @flags: Link configuration flags
737 *
738 * The bulk of link setup is handled by the two entities connected through the
739 * link. This function notifies both entities of the link configuration change.
740 *
741 * If the link is immutable or if the current and new configuration are
742 * identical, return immediately.
743 *
744 * The user is expected to hold link->source->parent->mutex. If not,
745 * media_entity_setup_link() should be used instead.
746 */
747int __media_entity_setup_link(struct media_link *link, u32 flags)
748{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300749 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300750 struct media_device *mdev;
751 struct media_entity *source, *sink;
752 int ret = -EBUSY;
753
754 if (link == NULL)
755 return -EINVAL;
756
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300757 /* The non-modifiable link flags must not be modified. */
758 if ((link->flags & ~mask) != (flags & ~mask))
759 return -EINVAL;
760
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300761 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
762 return link->flags == flags ? 0 : -EINVAL;
763
764 if (link->flags == flags)
765 return 0;
766
767 source = link->source->entity;
768 sink = link->sink->entity;
769
Laurent Pincharte02188c2010-08-25 09:00:41 -0300770 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
771 (source->stream_count || sink->stream_count))
772 return -EBUSY;
773
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300774 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300775
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300776 if (mdev->link_notify) {
777 ret = mdev->link_notify(link, flags,
778 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300779 if (ret < 0)
780 return ret;
781 }
782
783 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300784
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300785 if (mdev->link_notify)
786 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300787
788 return ret;
789}
790
791int media_entity_setup_link(struct media_link *link, u32 flags)
792{
793 int ret;
794
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200795 spin_lock(&link->source->entity->graph_obj.mdev->lock);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300796 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200797 spin_unlock(&link->source->entity->graph_obj.mdev->lock);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300798
799 return ret;
800}
801EXPORT_SYMBOL_GPL(media_entity_setup_link);
802
803/**
804 * media_entity_find_link - Find a link between two pads
805 * @source: Source pad
806 * @sink: Sink pad
807 *
808 * Return a pointer to the link between the two entities. If no such link
809 * exists, return NULL.
810 */
811struct media_link *
812media_entity_find_link(struct media_pad *source, struct media_pad *sink)
813{
814 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300815
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300816 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300817 if (link->source->entity == source->entity &&
818 link->source->index == source->index &&
819 link->sink->entity == sink->entity &&
820 link->sink->index == sink->index)
821 return link;
822 }
823
824 return NULL;
825}
826EXPORT_SYMBOL_GPL(media_entity_find_link);
827
828/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300829 * media_entity_remote_pad - Find the pad at the remote end of a link
830 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300831 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300832 * Search for a remote pad connected to the given pad by iterating over all
833 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300834 *
835 * Return a pointer to the pad at the remote end of the first found enabled
836 * link, or NULL if no enabled link has been found.
837 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300838struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300839{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300840 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300841
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300842 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300843 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
844 continue;
845
846 if (link->source == pad)
847 return link->sink;
848
849 if (link->sink == pad)
850 return link->source;
851 }
852
853 return NULL;
854
855}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300856EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300857
858
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300859static void media_interface_init(struct media_device *mdev,
860 struct media_interface *intf,
861 u32 gobj_type,
862 u32 intf_type, u32 flags)
863{
864 intf->type = intf_type;
865 intf->flags = flags;
866 INIT_LIST_HEAD(&intf->links);
867
868 media_gobj_init(mdev, gobj_type, &intf->graph_obj);
869}
870
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300871/* Functions related to the media interface via device nodes */
872
873struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
874 u32 type, u32 flags,
875 u32 major, u32 minor,
876 gfp_t gfp_flags)
877{
878 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300879
880 devnode = kzalloc(sizeof(*devnode), gfp_flags);
881 if (!devnode)
882 return NULL;
883
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300884 devnode->major = major;
885 devnode->minor = minor;
886
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300887 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
888 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300889
890 return devnode;
891}
892EXPORT_SYMBOL_GPL(media_devnode_create);
893
894void media_devnode_remove(struct media_intf_devnode *devnode)
895{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300896 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300897 media_gobj_remove(&devnode->intf.graph_obj);
898 kfree(devnode);
899}
900EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300901
902struct media_link *media_create_intf_link(struct media_entity *entity,
903 struct media_interface *intf,
904 u32 flags)
905{
906 struct media_link *link;
907
908 link = media_add_link(&intf->links);
909 if (link == NULL)
910 return NULL;
911
912 link->intf = intf;
913 link->entity = entity;
914 link->flags = flags;
915
916 /* Initialize graph object embedded at the new link */
917 media_gobj_init(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
918 &link->graph_obj);
919
920 return link;
921}
922EXPORT_SYMBOL_GPL(media_create_intf_link);
923
924
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300925void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300926{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300927 list_del(&link->list);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300928 media_gobj_remove(&link->graph_obj);
929 kfree(link);
930}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300931EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300932
933void media_remove_intf_link(struct media_link *link)
934{
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200935 spin_lock(&link->graph_obj.mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300936 __media_remove_intf_link(link);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200937 spin_unlock(&link->graph_obj.mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300938}
939EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300940
941void __media_remove_intf_links(struct media_interface *intf)
942{
943 struct media_link *link, *tmp;
944
945 list_for_each_entry_safe(link, tmp, &intf->links, list)
946 __media_remove_intf_link(link);
947
948}
949EXPORT_SYMBOL_GPL(__media_remove_intf_links);
950
951void media_remove_intf_links(struct media_interface *intf)
952{
953 /* Do nothing if the intf is not registered. */
954 if (intf->graph_obj.mdev == NULL)
955 return;
956
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200957 spin_lock(&intf->graph_obj.mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300958 __media_remove_intf_links(intf);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200959 spin_unlock(&intf->graph_obj.mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300960}
961EXPORT_SYMBOL_GPL(media_remove_intf_links);