blob: 246d7e65adedbf363fd3f0aa3c8ca43fd34ab6df [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{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300245}
246EXPORT_SYMBOL_GPL(media_entity_cleanup);
247
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300248/* -----------------------------------------------------------------------------
249 * Graph traversal
250 */
251
252static struct media_entity *
253media_entity_other(struct media_entity *entity, struct media_link *link)
254{
255 if (link->source->entity == entity)
256 return link->sink->entity;
257 else
258 return link->source->entity;
259}
260
261/* push an entity to traversal stack */
262static void stack_push(struct media_entity_graph *graph,
263 struct media_entity *entity)
264{
265 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
266 WARN_ON(1);
267 return;
268 }
269 graph->top++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300270 graph->stack[graph->top].link = (&entity->links)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300271 graph->stack[graph->top].entity = entity;
272}
273
274static struct media_entity *stack_pop(struct media_entity_graph *graph)
275{
276 struct media_entity *entity;
277
278 entity = graph->stack[graph->top].entity;
279 graph->top--;
280
281 return entity;
282}
283
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300284#define link_top(en) ((en)->stack[(en)->top].link)
285#define stack_top(en) ((en)->stack[(en)->top].entity)
286
287/**
288 * media_entity_graph_walk_start - Start walking the media graph at a given entity
289 * @graph: Media graph structure that will be used to walk the graph
290 * @entity: Starting entity
291 *
292 * This function initializes the graph traversal structure to walk the entities
293 * graph starting at the given entity. The traversal structure must not be
294 * modified by the caller during graph traversal. When done the structure can
295 * safely be freed.
296 */
297void media_entity_graph_walk_start(struct media_entity_graph *graph,
298 struct media_entity *entity)
299{
300 graph->top = 0;
301 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300302 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
303
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300304 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300305 return;
306
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300307 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300308 stack_push(graph, entity);
309}
310EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
311
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300312
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300313/**
314 * media_entity_graph_walk_next - Get the next entity in the graph
315 * @graph: Media graph structure
316 *
317 * Perform a depth-first traversal of the given media entities graph.
318 *
319 * The graph structure must have been previously initialized with a call to
320 * media_entity_graph_walk_start().
321 *
322 * Return the next entity in the graph or NULL if the whole graph have been
323 * traversed.
324 */
325struct media_entity *
326media_entity_graph_walk_next(struct media_entity_graph *graph)
327{
328 if (stack_top(graph) == NULL)
329 return NULL;
330
331 /*
332 * Depth first search. Push entity to stack and continue from
333 * top of the stack until no more entities on the level can be
334 * found.
335 */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300336 while (link_top(graph) != &(stack_top(graph)->links)) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300337 struct media_entity *entity = stack_top(graph);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300338 struct media_link *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300339 struct media_entity *next;
340
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300341 link = list_entry(link_top(graph), typeof(*link), list);
342
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300343 /* The link is not enabled so we do not follow. */
344 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300345 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300346 continue;
347 }
348
349 /* Get the entity in the other end of the link . */
350 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300351 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300352 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300353
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300354 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300355 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300356 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300357 continue;
358 }
359
360 /* Push the new entity to stack and start over. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300361 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300362 stack_push(graph, next);
363 }
364
365 return stack_pop(graph);
366}
367EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
368
369/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300370 * Pipeline management
371 */
372
373/**
374 * media_entity_pipeline_start - Mark a pipeline as streaming
375 * @entity: Starting entity
376 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
377 *
378 * Mark all entities connected to a given entity through enabled links, either
379 * directly or indirectly, as streaming. The given pipeline object is assigned to
380 * every entity in the pipeline and stored in the media_entity pipe field.
381 *
382 * Calls to this function can be nested, in which case the same number of
383 * media_entity_pipeline_stop() calls will be required to stop streaming. The
384 * pipeline pointer must be identical for all nested calls to
385 * media_entity_pipeline_start().
386 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300387__must_check int media_entity_pipeline_start(struct media_entity *entity,
388 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300389{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300390 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300391 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300392 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300393 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300394 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300395
396 mutex_lock(&mdev->graph_mutex);
397
398 media_entity_graph_walk_start(&graph, entity);
399
400 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300401 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
402 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300403
Laurent Pincharte02188c2010-08-25 09:00:41 -0300404 entity->stream_count++;
405 WARN_ON(entity->pipe && entity->pipe != pipe);
406 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300407
408 /* Already streaming --- no need to check. */
409 if (entity->stream_count > 1)
410 continue;
411
412 if (!entity->ops || !entity->ops->link_validate)
413 continue;
414
Sakari Ailusde49c282013-10-13 08:00:26 -0300415 bitmap_zero(active, entity->num_pads);
416 bitmap_fill(has_no_links, entity->num_pads);
417
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300418 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300419 struct media_pad *pad = link->sink->entity == entity
420 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300421
Sakari Ailusde49c282013-10-13 08:00:26 -0300422 /* Mark that a pad is connected by a link. */
423 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300424
Sakari Ailusde49c282013-10-13 08:00:26 -0300425 /*
426 * Pads that either do not need to connect or
427 * are connected through an enabled link are
428 * fine.
429 */
430 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
431 link->flags & MEDIA_LNK_FL_ENABLED)
432 bitmap_set(active, pad->index, 1);
433
434 /*
435 * Link validation will only take place for
436 * sink ends of the link that are enabled.
437 */
438 if (link->sink != pad ||
439 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300440 continue;
441
442 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300443 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300444 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300445 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200446 link->source->entity->name,
447 link->source->index,
448 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300449 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300450 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300451 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300452
453 /* Either no links or validated links are fine. */
454 bitmap_or(active, active, has_no_links, entity->num_pads);
455
456 if (!bitmap_full(active, entity->num_pads)) {
457 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300458 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300459 "\"%s\":%u must be connected by an enabled link\n",
460 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300461 (unsigned)find_first_zero_bit(
462 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300463 goto error;
464 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300465 }
466
467 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300468
469 return 0;
470
471error:
472 /*
473 * Link validation on graph failed. We revert what we did and
474 * return the error.
475 */
476 media_entity_graph_walk_start(&graph, entity_err);
477
478 while ((entity_err = media_entity_graph_walk_next(&graph))) {
479 entity_err->stream_count--;
480 if (entity_err->stream_count == 0)
481 entity_err->pipe = NULL;
482
483 /*
484 * We haven't increased stream_count further than this
485 * so we quit here.
486 */
487 if (entity_err == entity)
488 break;
489 }
490
491 mutex_unlock(&mdev->graph_mutex);
492
493 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300494}
495EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
496
497/**
498 * media_entity_pipeline_stop - Mark a pipeline as not streaming
499 * @entity: Starting entity
500 *
501 * Mark all entities connected to a given entity through enabled links, either
502 * directly or indirectly, as not streaming. The media_entity pipe field is
503 * reset to NULL.
504 *
505 * If multiple calls to media_entity_pipeline_start() have been made, the same
506 * number of calls to this function are required to mark the pipeline as not
507 * streaming.
508 */
509void media_entity_pipeline_stop(struct media_entity *entity)
510{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300511 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300512 struct media_entity_graph graph;
513
514 mutex_lock(&mdev->graph_mutex);
515
516 media_entity_graph_walk_start(&graph, entity);
517
518 while ((entity = media_entity_graph_walk_next(&graph))) {
519 entity->stream_count--;
520 if (entity->stream_count == 0)
521 entity->pipe = NULL;
522 }
523
524 mutex_unlock(&mdev->graph_mutex);
525}
526EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
527
528/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300529 * Module use count
530 */
531
532/*
533 * media_entity_get - Get a reference to the parent module
534 * @entity: The entity
535 *
536 * Get a reference to the parent media device module.
537 *
538 * The function will return immediately if @entity is NULL.
539 *
540 * Return a pointer to the entity on success or NULL on failure.
541 */
542struct media_entity *media_entity_get(struct media_entity *entity)
543{
544 if (entity == NULL)
545 return NULL;
546
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300547 if (entity->graph_obj.mdev->dev &&
548 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300549 return NULL;
550
551 return entity;
552}
553EXPORT_SYMBOL_GPL(media_entity_get);
554
555/*
556 * media_entity_put - Release the reference to the parent module
557 * @entity: The entity
558 *
559 * Release the reference count acquired by media_entity_get().
560 *
561 * The function will return immediately if @entity is NULL.
562 */
563void media_entity_put(struct media_entity *entity)
564{
565 if (entity == NULL)
566 return;
567
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300568 if (entity->graph_obj.mdev->dev)
569 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300570}
571EXPORT_SYMBOL_GPL(media_entity_put);
572
573/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300574 * Links management
575 */
576
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300577static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300578{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300579 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300580
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300581 link = kzalloc(sizeof(*link), GFP_KERNEL);
582 if (link == NULL)
583 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300584
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300585 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300586
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300587 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300588}
589
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300590static void __media_entity_remove_link(struct media_entity *entity,
591 struct media_link *link);
592
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300593int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300594media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300595 struct media_entity *sink, u16 sink_pad, u32 flags)
596{
597 struct media_link *link;
598 struct media_link *backlink;
599
600 BUG_ON(source == NULL || sink == NULL);
601 BUG_ON(source_pad >= source->num_pads);
602 BUG_ON(sink_pad >= sink->num_pads);
603
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300604 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300605 if (link == NULL)
606 return -ENOMEM;
607
608 link->source = &source->pads[source_pad];
609 link->sink = &sink->pads[sink_pad];
610 link->flags = flags;
611
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300612 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300613 media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
614 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300615
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300616 /* Create the backlink. Backlinks are used to help graph traversal and
617 * are not reported to userspace.
618 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300619 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300620 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300621 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300622 return -ENOMEM;
623 }
624
625 backlink->source = &source->pads[source_pad];
626 backlink->sink = &sink->pads[sink_pad];
627 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300628 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300629
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300630 /* Initialize graph object embedded at the new link */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300631 media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
632 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300633
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300634 link->reverse = backlink;
635 backlink->reverse = link;
636
637 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300638 sink->num_links++;
639 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300640
641 return 0;
642}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300643EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300644
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300645static void __media_entity_remove_link(struct media_entity *entity,
646 struct media_link *link)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300647{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300648 struct media_link *rlink, *tmp;
649 struct media_entity *remote;
650 unsigned int r = 0;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300651
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300652 if (link->source->entity == entity)
653 remote = link->sink->entity;
654 else
655 remote = link->source->entity;
656
657 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
658 if (rlink != link->reverse) {
659 r++;
660 continue;
661 }
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300662
663 if (link->source->entity == entity)
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300664 remote->num_backlinks--;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300665
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300666 /* Remove the remote link */
667 list_del(&rlink->list);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300668 media_gobj_remove(&rlink->graph_obj);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300669 kfree(rlink);
Mauro Carvalho Chehabeb83a512015-12-10 15:29:22 -0200670
671 if (--remote->num_links == 0)
672 break;
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300673 }
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300674 list_del(&link->list);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300675 media_gobj_remove(&link->graph_obj);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300676 kfree(link);
677}
678
679void __media_entity_remove_links(struct media_entity *entity)
680{
681 struct media_link *link, *tmp;
682
683 list_for_each_entry_safe(link, tmp, &entity->links, list)
684 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300685
686 entity->num_links = 0;
687 entity->num_backlinks = 0;
688}
689EXPORT_SYMBOL_GPL(__media_entity_remove_links);
690
691void media_entity_remove_links(struct media_entity *entity)
692{
693 /* Do nothing if the entity is not registered. */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300694 if (entity->graph_obj.mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300695 return;
696
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200697 spin_lock(&entity->graph_obj.mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300698 __media_entity_remove_links(entity);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200699 spin_unlock(&entity->graph_obj.mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300700}
701EXPORT_SYMBOL_GPL(media_entity_remove_links);
702
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300703static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
704{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300705 int ret;
706
707 /* Notify both entities. */
708 ret = media_entity_call(link->source->entity, link_setup,
709 link->source, link->sink, flags);
710 if (ret < 0 && ret != -ENOIOCTLCMD)
711 return ret;
712
713 ret = media_entity_call(link->sink->entity, link_setup,
714 link->sink, link->source, flags);
715 if (ret < 0 && ret != -ENOIOCTLCMD) {
716 media_entity_call(link->source->entity, link_setup,
717 link->source, link->sink, link->flags);
718 return ret;
719 }
720
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300721 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300722 link->reverse->flags = link->flags;
723
724 return 0;
725}
726
727/**
728 * __media_entity_setup_link - Configure a media link
729 * @link: The link being configured
730 * @flags: Link configuration flags
731 *
732 * The bulk of link setup is handled by the two entities connected through the
733 * link. This function notifies both entities of the link configuration change.
734 *
735 * If the link is immutable or if the current and new configuration are
736 * identical, return immediately.
737 *
738 * The user is expected to hold link->source->parent->mutex. If not,
739 * media_entity_setup_link() should be used instead.
740 */
741int __media_entity_setup_link(struct media_link *link, u32 flags)
742{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300743 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300744 struct media_device *mdev;
745 struct media_entity *source, *sink;
746 int ret = -EBUSY;
747
748 if (link == NULL)
749 return -EINVAL;
750
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300751 /* The non-modifiable link flags must not be modified. */
752 if ((link->flags & ~mask) != (flags & ~mask))
753 return -EINVAL;
754
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300755 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
756 return link->flags == flags ? 0 : -EINVAL;
757
758 if (link->flags == flags)
759 return 0;
760
761 source = link->source->entity;
762 sink = link->sink->entity;
763
Laurent Pincharte02188c2010-08-25 09:00:41 -0300764 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
765 (source->stream_count || sink->stream_count))
766 return -EBUSY;
767
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300768 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300769
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300770 if (mdev->link_notify) {
771 ret = mdev->link_notify(link, flags,
772 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300773 if (ret < 0)
774 return ret;
775 }
776
777 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300778
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300779 if (mdev->link_notify)
780 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300781
782 return ret;
783}
784
785int media_entity_setup_link(struct media_link *link, u32 flags)
786{
787 int ret;
788
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200789 spin_lock(&link->source->entity->graph_obj.mdev->lock);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300790 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200791 spin_unlock(&link->source->entity->graph_obj.mdev->lock);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300792
793 return ret;
794}
795EXPORT_SYMBOL_GPL(media_entity_setup_link);
796
797/**
798 * media_entity_find_link - Find a link between two pads
799 * @source: Source pad
800 * @sink: Sink pad
801 *
802 * Return a pointer to the link between the two entities. If no such link
803 * exists, return NULL.
804 */
805struct media_link *
806media_entity_find_link(struct media_pad *source, struct media_pad *sink)
807{
808 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300809
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300810 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300811 if (link->source->entity == source->entity &&
812 link->source->index == source->index &&
813 link->sink->entity == sink->entity &&
814 link->sink->index == sink->index)
815 return link;
816 }
817
818 return NULL;
819}
820EXPORT_SYMBOL_GPL(media_entity_find_link);
821
822/**
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300823 * media_entity_remote_pad - Find the pad at the remote end of a link
824 * @pad: Pad at the local end of the link
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300825 *
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300826 * Search for a remote pad connected to the given pad by iterating over all
827 * links originating or terminating at that pad until an enabled link is found.
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300828 *
829 * Return a pointer to the pad at the remote end of the first found enabled
830 * link, or NULL if no enabled link has been found.
831 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300832struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300833{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300834 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300835
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300836 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300837 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
838 continue;
839
840 if (link->source == pad)
841 return link->sink;
842
843 if (link->sink == pad)
844 return link->source;
845 }
846
847 return NULL;
848
849}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300850EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300851
852
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300853static void media_interface_init(struct media_device *mdev,
854 struct media_interface *intf,
855 u32 gobj_type,
856 u32 intf_type, u32 flags)
857{
858 intf->type = intf_type;
859 intf->flags = flags;
860 INIT_LIST_HEAD(&intf->links);
861
862 media_gobj_init(mdev, gobj_type, &intf->graph_obj);
863}
864
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300865/* Functions related to the media interface via device nodes */
866
867struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
868 u32 type, u32 flags,
869 u32 major, u32 minor,
870 gfp_t gfp_flags)
871{
872 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300873
874 devnode = kzalloc(sizeof(*devnode), gfp_flags);
875 if (!devnode)
876 return NULL;
877
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300878 devnode->major = major;
879 devnode->minor = minor;
880
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300881 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
882 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300883
884 return devnode;
885}
886EXPORT_SYMBOL_GPL(media_devnode_create);
887
888void media_devnode_remove(struct media_intf_devnode *devnode)
889{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300890 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300891 media_gobj_remove(&devnode->intf.graph_obj);
892 kfree(devnode);
893}
894EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300895
896struct media_link *media_create_intf_link(struct media_entity *entity,
897 struct media_interface *intf,
898 u32 flags)
899{
900 struct media_link *link;
901
902 link = media_add_link(&intf->links);
903 if (link == NULL)
904 return NULL;
905
906 link->intf = intf;
907 link->entity = entity;
908 link->flags = flags;
909
910 /* Initialize graph object embedded at the new link */
911 media_gobj_init(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
912 &link->graph_obj);
913
914 return link;
915}
916EXPORT_SYMBOL_GPL(media_create_intf_link);
917
918
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300919void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300920{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300921 list_del(&link->list);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300922 media_gobj_remove(&link->graph_obj);
923 kfree(link);
924}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300925EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300926
927void media_remove_intf_link(struct media_link *link)
928{
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200929 spin_lock(&link->graph_obj.mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300930 __media_remove_intf_link(link);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200931 spin_unlock(&link->graph_obj.mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300932}
933EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300934
935void __media_remove_intf_links(struct media_interface *intf)
936{
937 struct media_link *link, *tmp;
938
939 list_for_each_entry_safe(link, tmp, &intf->links, list)
940 __media_remove_intf_link(link);
941
942}
943EXPORT_SYMBOL_GPL(__media_remove_intf_links);
944
945void media_remove_intf_links(struct media_interface *intf)
946{
947 /* Do nothing if the intf is not registered. */
948 if (intf->graph_obj.mdev == NULL)
949 return;
950
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200951 spin_lock(&intf->graph_obj.mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300952 __media_remove_intf_links(intf);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200953 spin_unlock(&intf->graph_obj.mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300954}
955EXPORT_SYMBOL_GPL(media_remove_intf_links);