blob: f88740b12879d7311a0c811d07e70d78a4a90181 [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
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030029static inline const char *gobj_type(enum media_gobj_type type)
30{
31 switch (type) {
32 case MEDIA_GRAPH_ENTITY:
33 return "entity";
34 case MEDIA_GRAPH_PAD:
35 return "pad";
36 case MEDIA_GRAPH_LINK:
37 return "link";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030038 case MEDIA_GRAPH_INTF_DEVNODE:
39 return "intf-devnode";
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030040 default:
41 return "unknown";
42 }
43}
44
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030045static inline const char *intf_type(struct media_interface *intf)
46{
47 switch (intf->type) {
48 case MEDIA_INTF_T_DVB_FE:
49 return "frontend";
50 case MEDIA_INTF_T_DVB_DEMUX:
51 return "demux";
52 case MEDIA_INTF_T_DVB_DVR:
53 return "DVR";
54 case MEDIA_INTF_T_DVB_CA:
55 return "CA";
56 case MEDIA_INTF_T_DVB_NET:
57 return "dvbnet";
58 case MEDIA_INTF_T_V4L_VIDEO:
59 return "video";
60 case MEDIA_INTF_T_V4L_VBI:
61 return "vbi";
62 case MEDIA_INTF_T_V4L_RADIO:
63 return "radio";
64 case MEDIA_INTF_T_V4L_SUBDEV:
65 return "v4l2-subdev";
66 case MEDIA_INTF_T_V4L_SWRADIO:
67 return "swradio";
68 default:
69 return "unknown-intf";
70 }
71};
72
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -020073/**
74 * dev_dbg_obj - Prints in debug mode a change on some object
75 *
76 * @event_name: Name of the event to report. Could be __func__
77 * @gobj: Pointer to the object
78 *
79 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
80 * won't produce any code.
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
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200136void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300137 enum media_gobj_type type,
138 struct media_gobj *gobj)
139{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300140 BUG_ON(!mdev);
141
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300142 gobj->mdev = mdev;
143
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300144 /* Create a per-type unique object ID */
145 switch (type) {
146 case MEDIA_GRAPH_ENTITY:
147 gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300148 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300149 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300150 case MEDIA_GRAPH_PAD:
151 gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300152 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300153 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300154 case MEDIA_GRAPH_LINK:
155 gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300156 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300157 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300158 case MEDIA_GRAPH_INTF_DEVNODE:
159 gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300160 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300161 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300162 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300163
164 mdev->topology_version++;
165
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300166 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300167}
168
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200169void media_gobj_destroy(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300170{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300171 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300172
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300173 gobj->mdev->topology_version++;
174
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300175 /* Remove the object from mdev list */
176 list_del(&gobj->list);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300177}
178
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200179int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
180 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300181{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300182 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300183 unsigned int i;
184
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300185 entity->num_pads = num_pads;
186 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300187
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300188 if (mdev)
189 spin_lock(&mdev->lock);
190
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300191 for (i = 0; i < num_pads; i++) {
192 pads[i].entity = entity;
193 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300194 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200195 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300196 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300197 }
198
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300199 if (mdev)
200 spin_unlock(&mdev->lock);
201
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300202 return 0;
203}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200204EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300205
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300206/* -----------------------------------------------------------------------------
207 * Graph traversal
208 */
209
210static struct media_entity *
211media_entity_other(struct media_entity *entity, struct media_link *link)
212{
213 if (link->source->entity == entity)
214 return link->sink->entity;
215 else
216 return link->source->entity;
217}
218
219/* push an entity to traversal stack */
220static void stack_push(struct media_entity_graph *graph,
221 struct media_entity *entity)
222{
223 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
224 WARN_ON(1);
225 return;
226 }
227 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200228 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300229 graph->stack[graph->top].entity = entity;
230}
231
232static struct media_entity *stack_pop(struct media_entity_graph *graph)
233{
234 struct media_entity *entity;
235
236 entity = graph->stack[graph->top].entity;
237 graph->top--;
238
239 return entity;
240}
241
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300242#define link_top(en) ((en)->stack[(en)->top].link)
243#define stack_top(en) ((en)->stack[(en)->top].entity)
244
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300245void media_entity_graph_walk_start(struct media_entity_graph *graph,
246 struct media_entity *entity)
247{
248 graph->top = 0;
249 graph->stack[graph->top].entity = NULL;
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300250 bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
251
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300252 if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300253 return;
254
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300255 __set_bit(media_entity_id(entity), graph->entities);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300256 stack_push(graph, entity);
257}
258EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
259
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300260struct media_entity *
261media_entity_graph_walk_next(struct media_entity_graph *graph)
262{
263 if (stack_top(graph) == NULL)
264 return NULL;
265
266 /*
267 * Depth first search. Push entity to stack and continue from
268 * top of the stack until no more entities on the level can be
269 * found.
270 */
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200271 while (link_top(graph) != &stack_top(graph)->links) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300272 struct media_entity *entity = stack_top(graph);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300273 struct media_link *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300274 struct media_entity *next;
275
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300276 link = list_entry(link_top(graph), typeof(*link), list);
277
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300278 /* The link is not enabled so we do not follow. */
279 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300280 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300281 continue;
282 }
283
284 /* Get the entity in the other end of the link . */
285 next = media_entity_other(entity, link);
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300286 if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300287 return NULL;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300288
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300289 /* Has the entity already been visited? */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300290 if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300291 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300292 continue;
293 }
294
295 /* Push the new entity to stack and start over. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300296 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300297 stack_push(graph, next);
298 }
299
300 return stack_pop(graph);
301}
302EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
303
304/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300305 * Pipeline management
306 */
307
Sakari Ailusaf88be32012-01-11 06:25:15 -0300308__must_check int media_entity_pipeline_start(struct media_entity *entity,
309 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300310{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300311 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300312 struct media_entity_graph graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300313 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300314 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300315 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300316
317 mutex_lock(&mdev->graph_mutex);
318
319 media_entity_graph_walk_start(&graph, entity);
320
321 while ((entity = media_entity_graph_walk_next(&graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300322 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
323 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300324
Laurent Pincharte02188c2010-08-25 09:00:41 -0300325 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200326
327 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
328 ret = -EBUSY;
329 goto error;
330 }
331
Laurent Pincharte02188c2010-08-25 09:00:41 -0300332 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300333
334 /* Already streaming --- no need to check. */
335 if (entity->stream_count > 1)
336 continue;
337
338 if (!entity->ops || !entity->ops->link_validate)
339 continue;
340
Sakari Ailusde49c282013-10-13 08:00:26 -0300341 bitmap_zero(active, entity->num_pads);
342 bitmap_fill(has_no_links, entity->num_pads);
343
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300344 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300345 struct media_pad *pad = link->sink->entity == entity
346 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300347
Sakari Ailusde49c282013-10-13 08:00:26 -0300348 /* Mark that a pad is connected by a link. */
349 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300350
Sakari Ailusde49c282013-10-13 08:00:26 -0300351 /*
352 * Pads that either do not need to connect or
353 * are connected through an enabled link are
354 * fine.
355 */
356 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
357 link->flags & MEDIA_LNK_FL_ENABLED)
358 bitmap_set(active, pad->index, 1);
359
360 /*
361 * Link validation will only take place for
362 * sink ends of the link that are enabled.
363 */
364 if (link->sink != pad ||
365 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300366 continue;
367
368 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300369 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300370 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300371 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200372 link->source->entity->name,
373 link->source->index,
374 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300375 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300376 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300377 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300378
379 /* Either no links or validated links are fine. */
380 bitmap_or(active, active, has_no_links, entity->num_pads);
381
382 if (!bitmap_full(active, entity->num_pads)) {
383 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300384 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300385 "\"%s\":%u must be connected by an enabled link\n",
386 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300387 (unsigned)find_first_zero_bit(
388 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300389 goto error;
390 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300391 }
392
393 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300394
395 return 0;
396
397error:
398 /*
399 * Link validation on graph failed. We revert what we did and
400 * return the error.
401 */
402 media_entity_graph_walk_start(&graph, entity_err);
403
404 while ((entity_err = media_entity_graph_walk_next(&graph))) {
405 entity_err->stream_count--;
406 if (entity_err->stream_count == 0)
407 entity_err->pipe = NULL;
408
409 /*
410 * We haven't increased stream_count further than this
411 * so we quit here.
412 */
413 if (entity_err == entity)
414 break;
415 }
416
417 mutex_unlock(&mdev->graph_mutex);
418
419 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300420}
421EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
422
Laurent Pincharte02188c2010-08-25 09:00:41 -0300423void media_entity_pipeline_stop(struct media_entity *entity)
424{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300425 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300426 struct media_entity_graph graph;
427
428 mutex_lock(&mdev->graph_mutex);
429
430 media_entity_graph_walk_start(&graph, entity);
431
432 while ((entity = media_entity_graph_walk_next(&graph))) {
433 entity->stream_count--;
434 if (entity->stream_count == 0)
435 entity->pipe = NULL;
436 }
437
438 mutex_unlock(&mdev->graph_mutex);
439}
440EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
441
442/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300443 * Module use count
444 */
445
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300446struct media_entity *media_entity_get(struct media_entity *entity)
447{
448 if (entity == NULL)
449 return NULL;
450
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300451 if (entity->graph_obj.mdev->dev &&
452 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300453 return NULL;
454
455 return entity;
456}
457EXPORT_SYMBOL_GPL(media_entity_get);
458
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300459void media_entity_put(struct media_entity *entity)
460{
461 if (entity == NULL)
462 return;
463
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300464 if (entity->graph_obj.mdev->dev)
465 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300466}
467EXPORT_SYMBOL_GPL(media_entity_put);
468
469/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300470 * Links management
471 */
472
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300473static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300474{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300475 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300476
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300477 link = kzalloc(sizeof(*link), GFP_KERNEL);
478 if (link == NULL)
479 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300480
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300481 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300482
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300483 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300484}
485
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300486static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200487 struct media_link *link)
488{
489 struct media_link *rlink, *tmp;
490 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200491
492 if (link->source->entity == entity)
493 remote = link->sink->entity;
494 else
495 remote = link->source->entity;
496
497 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200498 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200499 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200500
501 if (link->source->entity == entity)
502 remote->num_backlinks--;
503
504 /* Remove the remote link */
505 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200506 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200507 kfree(rlink);
508
509 if (--remote->num_links == 0)
510 break;
511 }
512 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200513 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200514 kfree(link);
515}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300516
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300517int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300518media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300519 struct media_entity *sink, u16 sink_pad, u32 flags)
520{
521 struct media_link *link;
522 struct media_link *backlink;
523
524 BUG_ON(source == NULL || sink == NULL);
525 BUG_ON(source_pad >= source->num_pads);
526 BUG_ON(sink_pad >= sink->num_pads);
527
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300528 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300529 if (link == NULL)
530 return -ENOMEM;
531
532 link->source = &source->pads[source_pad];
533 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200534 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300535
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300536 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200537 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300538 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300539
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300540 /* Create the backlink. Backlinks are used to help graph traversal and
541 * are not reported to userspace.
542 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300543 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300544 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300545 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300546 return -ENOMEM;
547 }
548
549 backlink->source = &source->pads[source_pad];
550 backlink->sink = &sink->pads[sink_pad];
551 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300552 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300553
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300554 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200555 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300556 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300557
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300558 link->reverse = backlink;
559 backlink->reverse = link;
560
561 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300562 sink->num_links++;
563 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300564
565 return 0;
566}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300567EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300568
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300569void __media_entity_remove_links(struct media_entity *entity)
570{
571 struct media_link *link, *tmp;
572
573 list_for_each_entry_safe(link, tmp, &entity->links, list)
574 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300575
576 entity->num_links = 0;
577 entity->num_backlinks = 0;
578}
579EXPORT_SYMBOL_GPL(__media_entity_remove_links);
580
581void media_entity_remove_links(struct media_entity *entity)
582{
583 /* Do nothing if the entity is not registered. */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300584 if (entity->graph_obj.mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300585 return;
586
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200587 spin_lock(&entity->graph_obj.mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300588 __media_entity_remove_links(entity);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200589 spin_unlock(&entity->graph_obj.mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300590}
591EXPORT_SYMBOL_GPL(media_entity_remove_links);
592
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300593static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
594{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300595 int ret;
596
597 /* Notify both entities. */
598 ret = media_entity_call(link->source->entity, link_setup,
599 link->source, link->sink, flags);
600 if (ret < 0 && ret != -ENOIOCTLCMD)
601 return ret;
602
603 ret = media_entity_call(link->sink->entity, link_setup,
604 link->sink, link->source, flags);
605 if (ret < 0 && ret != -ENOIOCTLCMD) {
606 media_entity_call(link->source->entity, link_setup,
607 link->source, link->sink, link->flags);
608 return ret;
609 }
610
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300611 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300612 link->reverse->flags = link->flags;
613
614 return 0;
615}
616
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300617int __media_entity_setup_link(struct media_link *link, u32 flags)
618{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300619 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300620 struct media_device *mdev;
621 struct media_entity *source, *sink;
622 int ret = -EBUSY;
623
624 if (link == NULL)
625 return -EINVAL;
626
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300627 /* The non-modifiable link flags must not be modified. */
628 if ((link->flags & ~mask) != (flags & ~mask))
629 return -EINVAL;
630
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300631 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
632 return link->flags == flags ? 0 : -EINVAL;
633
634 if (link->flags == flags)
635 return 0;
636
637 source = link->source->entity;
638 sink = link->sink->entity;
639
Laurent Pincharte02188c2010-08-25 09:00:41 -0300640 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
641 (source->stream_count || sink->stream_count))
642 return -EBUSY;
643
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300644 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300645
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300646 if (mdev->link_notify) {
647 ret = mdev->link_notify(link, flags,
648 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300649 if (ret < 0)
650 return ret;
651 }
652
653 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300654
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300655 if (mdev->link_notify)
656 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300657
658 return ret;
659}
660
661int media_entity_setup_link(struct media_link *link, u32 flags)
662{
663 int ret;
664
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200665 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300666 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200667 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300668
669 return ret;
670}
671EXPORT_SYMBOL_GPL(media_entity_setup_link);
672
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300673struct media_link *
674media_entity_find_link(struct media_pad *source, struct media_pad *sink)
675{
676 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300677
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300678 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300679 if (link->source->entity == source->entity &&
680 link->source->index == source->index &&
681 link->sink->entity == sink->entity &&
682 link->sink->index == sink->index)
683 return link;
684 }
685
686 return NULL;
687}
688EXPORT_SYMBOL_GPL(media_entity_find_link);
689
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300690struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300691{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300692 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300693
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300694 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300695 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
696 continue;
697
698 if (link->source == pad)
699 return link->sink;
700
701 if (link->sink == pad)
702 return link->source;
703 }
704
705 return NULL;
706
707}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300708EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300709
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300710static void media_interface_init(struct media_device *mdev,
711 struct media_interface *intf,
712 u32 gobj_type,
713 u32 intf_type, u32 flags)
714{
715 intf->type = intf_type;
716 intf->flags = flags;
717 INIT_LIST_HEAD(&intf->links);
718
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200719 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300720}
721
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300722/* Functions related to the media interface via device nodes */
723
724struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
725 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300726 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300727{
728 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300729
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300730 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300731 if (!devnode)
732 return NULL;
733
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300734 devnode->major = major;
735 devnode->minor = minor;
736
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300737 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
738 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300739
740 return devnode;
741}
742EXPORT_SYMBOL_GPL(media_devnode_create);
743
744void media_devnode_remove(struct media_intf_devnode *devnode)
745{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300746 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200747 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300748 kfree(devnode);
749}
750EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300751
752struct media_link *media_create_intf_link(struct media_entity *entity,
753 struct media_interface *intf,
754 u32 flags)
755{
756 struct media_link *link;
757
758 link = media_add_link(&intf->links);
759 if (link == NULL)
760 return NULL;
761
762 link->intf = intf;
763 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200764 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300765
766 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200767 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300768 &link->graph_obj);
769
770 return link;
771}
772EXPORT_SYMBOL_GPL(media_create_intf_link);
773
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300774void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300775{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300776 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200777 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300778 kfree(link);
779}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300780EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300781
782void media_remove_intf_link(struct media_link *link)
783{
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200784 spin_lock(&link->graph_obj.mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300785 __media_remove_intf_link(link);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200786 spin_unlock(&link->graph_obj.mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300787}
788EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300789
790void __media_remove_intf_links(struct media_interface *intf)
791{
792 struct media_link *link, *tmp;
793
794 list_for_each_entry_safe(link, tmp, &intf->links, list)
795 __media_remove_intf_link(link);
796
797}
798EXPORT_SYMBOL_GPL(__media_remove_intf_links);
799
800void media_remove_intf_links(struct media_interface *intf)
801{
802 /* Do nothing if the intf is not registered. */
803 if (intf->graph_obj.mdev == NULL)
804 return;
805
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200806 spin_lock(&intf->graph_obj.mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300807 __media_remove_intf_links(intf);
Mauro Carvalho Chehaba08fad12015-12-09 19:47:35 -0200808 spin_unlock(&intf->graph_obj.mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300809}
810EXPORT_SYMBOL_GPL(media_remove_intf_links);