blob: 3498551e618e543799e42ecfff441a597ede5967 [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.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030017 */
18
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -030019#include <linux/bitmap.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030020#include <linux/module.h>
Niklas Söderlundd295c6a2017-06-15 06:17:26 -030021#include <linux/property.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030022#include <linux/slab.h>
23#include <media/media-entity.h>
Laurent Pinchart503c3d822010-03-07 15:04:59 -030024#include <media/media-device.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030025
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030026static inline const char *gobj_type(enum media_gobj_type type)
27{
28 switch (type) {
29 case MEDIA_GRAPH_ENTITY:
30 return "entity";
31 case MEDIA_GRAPH_PAD:
32 return "pad";
33 case MEDIA_GRAPH_LINK:
34 return "link";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030035 case MEDIA_GRAPH_INTF_DEVNODE:
36 return "intf-devnode";
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030037 default:
38 return "unknown";
39 }
40}
41
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030042static inline const char *intf_type(struct media_interface *intf)
43{
44 switch (intf->type) {
45 case MEDIA_INTF_T_DVB_FE:
Shuah Khan66c1db12016-03-04 18:14:05 -030046 return "dvb-frontend";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030047 case MEDIA_INTF_T_DVB_DEMUX:
Shuah Khan66c1db12016-03-04 18:14:05 -030048 return "dvb-demux";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030049 case MEDIA_INTF_T_DVB_DVR:
Shuah Khan66c1db12016-03-04 18:14:05 -030050 return "dvb-dvr";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030051 case MEDIA_INTF_T_DVB_CA:
Shuah Khan66c1db12016-03-04 18:14:05 -030052 return "dvb-ca";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030053 case MEDIA_INTF_T_DVB_NET:
Shuah Khan66c1db12016-03-04 18:14:05 -030054 return "dvb-net";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030055 case MEDIA_INTF_T_V4L_VIDEO:
Shuah Khan66c1db12016-03-04 18:14:05 -030056 return "v4l-video";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030057 case MEDIA_INTF_T_V4L_VBI:
Shuah Khan66c1db12016-03-04 18:14:05 -030058 return "v4l-vbi";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030059 case MEDIA_INTF_T_V4L_RADIO:
Shuah Khan66c1db12016-03-04 18:14:05 -030060 return "v4l-radio";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030061 case MEDIA_INTF_T_V4L_SUBDEV:
Shuah Khan66c1db12016-03-04 18:14:05 -030062 return "v4l-subdev";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030063 case MEDIA_INTF_T_V4L_SWRADIO:
Shuah Khan66c1db12016-03-04 18:14:05 -030064 return "v4l-swradio";
Nick Dyerb2fe22d2016-07-18 18:10:30 -030065 case MEDIA_INTF_T_V4L_TOUCH:
66 return "v4l-touch";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030067 default:
68 return "unknown-intf";
69 }
70};
71
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020072__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
73 int idx_max)
74{
Sakari Ailusf7b5dff2016-01-27 12:47:54 -020075 idx_max = ALIGN(idx_max, BITS_PER_LONG);
76 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
77 GFP_KERNEL);
Sakari Ailus030e89e2015-12-16 11:32:36 -020078 if (!ent_enum->bmap)
79 return -ENOMEM;
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020080
81 bitmap_zero(ent_enum->bmap, idx_max);
82 ent_enum->idx_max = idx_max;
83
84 return 0;
85}
86EXPORT_SYMBOL_GPL(__media_entity_enum_init);
87
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020088void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
89{
Sakari Ailus030e89e2015-12-16 11:32:36 -020090 kfree(ent_enum->bmap);
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020091}
92EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
93
94/**
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -020095 * dev_dbg_obj - Prints in debug mode a change on some object
96 *
97 * @event_name: Name of the event to report. Could be __func__
98 * @gobj: Pointer to the object
99 *
100 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
101 * won't produce any code.
102 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300103static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
104{
105#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
106 switch (media_type(gobj)) {
107 case MEDIA_GRAPH_ENTITY:
108 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200109 "%s id %u: entity '%s'\n",
110 event_name, media_id(gobj),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300111 gobj_to_entity(gobj)->name);
112 break;
113 case MEDIA_GRAPH_LINK:
114 {
115 struct media_link *link = gobj_to_link(gobj);
116
117 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200118 "%s id %u: %s link id %u ==> id %u\n",
119 event_name, media_id(gobj),
120 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
121 "data" : "interface",
122 media_id(link->gobj0),
123 media_id(link->gobj1));
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300124 break;
125 }
126 case MEDIA_GRAPH_PAD:
127 {
128 struct media_pad *pad = gobj_to_pad(gobj);
129
130 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200131 "%s id %u: %s%spad '%s':%d\n",
132 event_name, media_id(gobj),
133 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
Mauro Carvalho Chehab6c24d462015-08-21 18:26:42 -0300134 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300135 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300136 break;
137 }
138 case MEDIA_GRAPH_INTF_DEVNODE:
139 {
140 struct media_interface *intf = gobj_to_intf(gobj);
141 struct media_intf_devnode *devnode = intf_to_devnode(intf);
142
143 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200144 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
145 event_name, media_id(gobj),
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300146 intf_type(intf),
147 devnode->major, devnode->minor);
148 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300149 }
150 }
151#endif
152}
153
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200154void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300155 enum media_gobj_type type,
156 struct media_gobj *gobj)
157{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300158 BUG_ON(!mdev);
159
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300160 gobj->mdev = mdev;
161
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300162 /* Create a per-type unique object ID */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200163 gobj->id = media_gobj_gen_id(type, ++mdev->id);
164
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300165 switch (type) {
166 case MEDIA_GRAPH_ENTITY:
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300167 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300168 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300169 case MEDIA_GRAPH_PAD:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300170 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300171 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300172 case MEDIA_GRAPH_LINK:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300173 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300174 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300175 case MEDIA_GRAPH_INTF_DEVNODE:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300176 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300177 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300178 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300179
180 mdev->topology_version++;
181
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300182 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300183}
184
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200185void media_gobj_destroy(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300186{
Max Kellermann67537432016-08-09 23:32:57 +0200187 /* Do nothing if the object is not linked. */
188 if (gobj->mdev == NULL)
189 return;
190
Helen Fornazier8d1d3d02017-04-06 16:25:15 -0300191 dev_dbg_obj(__func__, gobj);
192
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);
Max Kellermann67537432016-08-09 23:32:57 +0200197
198 gobj->mdev = NULL;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300199}
200
Sakari Ailus885ca802015-10-05 12:45:29 -0300201/*
202 * TODO: Get rid of this.
203 */
204#define MEDIA_ENTITY_MAX_PADS 512
205
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200206int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
207 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300208{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300209 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300210 unsigned int i;
211
Sakari Ailus885ca802015-10-05 12:45:29 -0300212 if (num_pads >= MEDIA_ENTITY_MAX_PADS)
213 return -E2BIG;
214
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300215 entity->num_pads = num_pads;
216 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300217
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300218 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300219 mutex_lock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300220
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300221 for (i = 0; i < num_pads; i++) {
222 pads[i].entity = entity;
223 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300224 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200225 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300226 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300227 }
228
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300229 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300230 mutex_unlock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300231
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300232 return 0;
233}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200234EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300235
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300236/* -----------------------------------------------------------------------------
237 * Graph traversal
238 */
239
240static struct media_entity *
241media_entity_other(struct media_entity *entity, struct media_link *link)
242{
243 if (link->source->entity == entity)
244 return link->sink->entity;
245 else
246 return link->source->entity;
247}
248
249/* push an entity to traversal stack */
Sakari Ailus20b85222016-11-21 14:48:30 -0200250static void stack_push(struct media_graph *graph,
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300251 struct media_entity *entity)
252{
253 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
254 WARN_ON(1);
255 return;
256 }
257 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200258 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300259 graph->stack[graph->top].entity = entity;
260}
261
Sakari Ailus20b85222016-11-21 14:48:30 -0200262static struct media_entity *stack_pop(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300263{
264 struct media_entity *entity;
265
266 entity = graph->stack[graph->top].entity;
267 graph->top--;
268
269 return entity;
270}
271
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300272#define link_top(en) ((en)->stack[(en)->top].link)
273#define stack_top(en) ((en)->stack[(en)->top].entity)
274
Sakari Ailuse03d2202015-12-16 11:32:22 -0200275/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200276 * media_graph_walk_init - Allocate resources for graph walk
Sakari Ailuse03d2202015-12-16 11:32:22 -0200277 * @graph: Media graph structure that will be used to walk the graph
278 * @mdev: Media device
279 *
280 * Reserve resources for graph walk in media device's current
281 * state. The memory must be released using
Sakari Ailus20b85222016-11-21 14:48:30 -0200282 * media_graph_walk_free().
Sakari Ailuse03d2202015-12-16 11:32:22 -0200283 *
284 * Returns error on failure, zero on success.
285 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200286__must_check int media_graph_walk_init(
287 struct media_graph *graph, struct media_device *mdev)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200288{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200289 return media_entity_enum_init(&graph->ent_enum, mdev);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200290}
Sakari Ailus20b85222016-11-21 14:48:30 -0200291EXPORT_SYMBOL_GPL(media_graph_walk_init);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200292
293/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200294 * media_graph_walk_cleanup - Release resources related to graph walking
Sakari Ailuse03d2202015-12-16 11:32:22 -0200295 * @graph: Media graph structure that was used to walk the graph
296 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200297void media_graph_walk_cleanup(struct media_graph *graph)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200298{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200299 media_entity_enum_cleanup(&graph->ent_enum);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200300}
Sakari Ailus20b85222016-11-21 14:48:30 -0200301EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200302
Sakari Ailus20b85222016-11-21 14:48:30 -0200303void media_graph_walk_start(struct media_graph *graph,
304 struct media_entity *entity)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300305{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200306 media_entity_enum_zero(&graph->ent_enum);
307 media_entity_enum_set(&graph->ent_enum, entity);
308
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300309 graph->top = 0;
310 graph->stack[graph->top].entity = NULL;
311 stack_push(graph, entity);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300312 dev_dbg(entity->graph_obj.mdev->dev,
313 "begin graph walk at '%s'\n", entity->name);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300314}
Sakari Ailus20b85222016-11-21 14:48:30 -0200315EXPORT_SYMBOL_GPL(media_graph_walk_start);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300316
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300317static void media_graph_walk_iter(struct media_graph *graph)
318{
319 struct media_entity *entity = stack_top(graph);
320 struct media_link *link;
321 struct media_entity *next;
322
323 link = list_entry(link_top(graph), typeof(*link), list);
324
325 /* The link is not enabled so we do not follow. */
326 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
327 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300328 dev_dbg(entity->graph_obj.mdev->dev,
329 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
330 link->source->entity->name, link->source->index,
331 link->sink->entity->name, link->sink->index);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300332 return;
333 }
334
335 /* Get the entity in the other end of the link . */
336 next = media_entity_other(entity, link);
337
338 /* Has the entity already been visited? */
339 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
340 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300341 dev_dbg(entity->graph_obj.mdev->dev,
342 "walk: skipping entity '%s' (already seen)\n",
343 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300344 return;
345 }
346
347 /* Push the new entity to stack and start over. */
348 link_top(graph) = link_top(graph)->next;
349 stack_push(graph, next);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300350 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
351 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300352}
353
Sakari Ailus20b85222016-11-21 14:48:30 -0200354struct media_entity *media_graph_walk_next(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300355{
Sakari Ailusaa79a842016-07-20 12:39:02 -0300356 struct media_entity *entity;
357
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300358 if (stack_top(graph) == NULL)
359 return NULL;
360
361 /*
362 * Depth first search. Push entity to stack and continue from
363 * top of the stack until no more entities on the level can be
364 * found.
365 */
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300366 while (link_top(graph) != &stack_top(graph)->links)
367 media_graph_walk_iter(graph);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300368
Sakari Ailusaa79a842016-07-20 12:39:02 -0300369 entity = stack_pop(graph);
370 dev_dbg(entity->graph_obj.mdev->dev,
371 "walk: returning entity '%s'\n", entity->name);
372
373 return entity;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300374}
Sakari Ailus20b85222016-11-21 14:48:30 -0200375EXPORT_SYMBOL_GPL(media_graph_walk_next);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300376
Niklas Söderlundd295c6a2017-06-15 06:17:26 -0300377int media_entity_get_fwnode_pad(struct media_entity *entity,
378 struct fwnode_handle *fwnode,
379 unsigned long direction_flags)
380{
381 struct fwnode_endpoint endpoint;
382 unsigned int i;
383 int ret;
384
385 if (!entity->ops || !entity->ops->get_fwnode_pad) {
386 for (i = 0; i < entity->num_pads; i++) {
387 if (entity->pads[i].flags & direction_flags)
388 return i;
389 }
390
391 return -ENXIO;
392 }
393
394 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
395 if (ret)
396 return ret;
397
398 ret = entity->ops->get_fwnode_pad(&endpoint);
399 if (ret < 0)
400 return ret;
401
402 if (ret >= entity->num_pads)
403 return -ENXIO;
404
405 if (!(entity->pads[ret].flags & direction_flags))
406 return -ENXIO;
407
408 return ret;
409}
410EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
411
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300412/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300413 * Pipeline management
414 */
415
Sakari Ailus20b85222016-11-21 14:48:30 -0200416__must_check int __media_pipeline_start(struct media_entity *entity,
417 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300418{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300419 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus20b85222016-11-21 14:48:30 -0200420 struct media_graph *graph = &pipe->graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300421 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300422 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300423 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300424
Sakari Ailus74a41332015-12-16 11:32:29 -0200425 if (!pipe->streaming_count++) {
Sakari Ailus20b85222016-11-21 14:48:30 -0200426 ret = media_graph_walk_init(&pipe->graph, mdev);
Sakari Ailus74a41332015-12-16 11:32:29 -0200427 if (ret)
428 goto error_graph_walk_start;
Sakari Ailus106b9902015-12-16 15:32:23 +0200429 }
430
Sakari Ailus20b85222016-11-21 14:48:30 -0200431 media_graph_walk_start(&pipe->graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300432
Sakari Ailus20b85222016-11-21 14:48:30 -0200433 while ((entity = media_graph_walk_next(graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300434 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
435 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300436
Laurent Pincharte02188c2010-08-25 09:00:41 -0300437 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200438
439 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
440 ret = -EBUSY;
441 goto error;
442 }
443
Laurent Pincharte02188c2010-08-25 09:00:41 -0300444 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300445
446 /* Already streaming --- no need to check. */
447 if (entity->stream_count > 1)
448 continue;
449
450 if (!entity->ops || !entity->ops->link_validate)
451 continue;
452
Sakari Ailusde49c282013-10-13 08:00:26 -0300453 bitmap_zero(active, entity->num_pads);
454 bitmap_fill(has_no_links, entity->num_pads);
455
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300456 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300457 struct media_pad *pad = link->sink->entity == entity
458 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300459
Sakari Ailusde49c282013-10-13 08:00:26 -0300460 /* Mark that a pad is connected by a link. */
461 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300462
Sakari Ailusde49c282013-10-13 08:00:26 -0300463 /*
464 * Pads that either do not need to connect or
465 * are connected through an enabled link are
466 * fine.
467 */
468 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
469 link->flags & MEDIA_LNK_FL_ENABLED)
470 bitmap_set(active, pad->index, 1);
471
472 /*
473 * Link validation will only take place for
474 * sink ends of the link that are enabled.
475 */
476 if (link->sink != pad ||
477 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300478 continue;
479
480 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300481 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300482 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200483 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200484 link->source->entity->name,
485 link->source->index,
486 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300487 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300488 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300489 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300490
491 /* Either no links or validated links are fine. */
492 bitmap_or(active, active, has_no_links, entity->num_pads);
493
494 if (!bitmap_full(active, entity->num_pads)) {
Helen Mae Koike Fornazier47dfdb32015-06-28 16:14:10 -0300495 ret = -ENOLINK;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300496 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200497 "'%s':%u must be connected by an enabled link\n",
Sakari Ailusfab9d302014-10-28 20:35:04 -0300498 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300499 (unsigned)find_first_zero_bit(
500 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300501 goto error;
502 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300503 }
504
Sakari Ailusaf88be32012-01-11 06:25:15 -0300505 return 0;
506
507error:
508 /*
509 * Link validation on graph failed. We revert what we did and
510 * return the error.
511 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200512 media_graph_walk_start(graph, entity_err);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300513
Sakari Ailus20b85222016-11-21 14:48:30 -0200514 while ((entity_err = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300515 /* Sanity check for negative stream_count */
516 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200517 entity_err->stream_count--;
518 if (entity_err->stream_count == 0)
519 entity_err->pipe = NULL;
520 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300521
522 /*
523 * We haven't increased stream_count further than this
524 * so we quit here.
525 */
526 if (entity_err == entity)
527 break;
528 }
529
Sakari Ailus74a41332015-12-16 11:32:29 -0200530error_graph_walk_start:
531 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200532 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200533
Shuah Khanfb49f202016-02-11 21:41:24 -0200534 return ret;
535}
Sakari Ailus20b85222016-11-21 14:48:30 -0200536EXPORT_SYMBOL_GPL(__media_pipeline_start);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300537
Sakari Ailus20b85222016-11-21 14:48:30 -0200538__must_check int media_pipeline_start(struct media_entity *entity,
539 struct media_pipeline *pipe)
Shuah Khanfb49f202016-02-11 21:41:24 -0200540{
541 struct media_device *mdev = entity->graph_obj.mdev;
542 int ret;
543
544 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200545 ret = __media_pipeline_start(entity, pipe);
Shuah Khanfb49f202016-02-11 21:41:24 -0200546 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300547 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300548}
Sakari Ailus20b85222016-11-21 14:48:30 -0200549EXPORT_SYMBOL_GPL(media_pipeline_start);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300550
Sakari Ailus20b85222016-11-21 14:48:30 -0200551void __media_pipeline_stop(struct media_entity *entity)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300552{
Sakari Ailus20b85222016-11-21 14:48:30 -0200553 struct media_graph *graph = &entity->pipe->graph;
Sakari Ailus74a41332015-12-16 11:32:29 -0200554 struct media_pipeline *pipe = entity->pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300555
Kieran Bingham2a2599c2017-01-03 11:12:11 -0200556 /*
557 * If the following check fails, the driver has performed an
558 * unbalanced call to media_pipeline_stop()
559 */
560 if (WARN_ON(!pipe))
561 return;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300562
Sakari Ailus20b85222016-11-21 14:48:30 -0200563 media_graph_walk_start(graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300564
Sakari Ailus20b85222016-11-21 14:48:30 -0200565 while ((entity = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300566 /* Sanity check for negative stream_count */
567 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200568 entity->stream_count--;
569 if (entity->stream_count == 0)
570 entity->pipe = NULL;
571 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300572 }
573
Sakari Ailus74a41332015-12-16 11:32:29 -0200574 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200575 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200576
Shuah Khanfb49f202016-02-11 21:41:24 -0200577}
Sakari Ailus20b85222016-11-21 14:48:30 -0200578EXPORT_SYMBOL_GPL(__media_pipeline_stop);
Shuah Khanfb49f202016-02-11 21:41:24 -0200579
Sakari Ailus20b85222016-11-21 14:48:30 -0200580void media_pipeline_stop(struct media_entity *entity)
Shuah Khanfb49f202016-02-11 21:41:24 -0200581{
582 struct media_device *mdev = entity->graph_obj.mdev;
583
584 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200585 __media_pipeline_stop(entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300586 mutex_unlock(&mdev->graph_mutex);
587}
Sakari Ailus20b85222016-11-21 14:48:30 -0200588EXPORT_SYMBOL_GPL(media_pipeline_stop);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300589
590/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300591 * Module use count
592 */
593
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300594struct media_entity *media_entity_get(struct media_entity *entity)
595{
596 if (entity == NULL)
597 return NULL;
598
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300599 if (entity->graph_obj.mdev->dev &&
600 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300601 return NULL;
602
603 return entity;
604}
605EXPORT_SYMBOL_GPL(media_entity_get);
606
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300607void media_entity_put(struct media_entity *entity)
608{
609 if (entity == NULL)
610 return;
611
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300612 if (entity->graph_obj.mdev->dev)
613 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300614}
615EXPORT_SYMBOL_GPL(media_entity_put);
616
617/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300618 * Links management
619 */
620
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300621static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300622{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300623 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300624
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300625 link = kzalloc(sizeof(*link), GFP_KERNEL);
626 if (link == NULL)
627 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300628
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300629 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300630
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300631 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300632}
633
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300634static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200635 struct media_link *link)
636{
637 struct media_link *rlink, *tmp;
638 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200639
640 if (link->source->entity == entity)
641 remote = link->sink->entity;
642 else
643 remote = link->source->entity;
644
645 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200646 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200647 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200648
649 if (link->source->entity == entity)
650 remote->num_backlinks--;
651
652 /* Remove the remote link */
653 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200654 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200655 kfree(rlink);
656
657 if (--remote->num_links == 0)
658 break;
659 }
660 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200661 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200662 kfree(link);
663}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300664
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300665int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300666media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300667 struct media_entity *sink, u16 sink_pad, u32 flags)
668{
669 struct media_link *link;
670 struct media_link *backlink;
671
672 BUG_ON(source == NULL || sink == NULL);
673 BUG_ON(source_pad >= source->num_pads);
674 BUG_ON(sink_pad >= sink->num_pads);
675
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300676 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300677 if (link == NULL)
678 return -ENOMEM;
679
680 link->source = &source->pads[source_pad];
681 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200682 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300683
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300684 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200685 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300686 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300687
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300688 /* Create the backlink. Backlinks are used to help graph traversal and
689 * are not reported to userspace.
690 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300691 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300692 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300693 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300694 return -ENOMEM;
695 }
696
697 backlink->source = &source->pads[source_pad];
698 backlink->sink = &sink->pads[sink_pad];
699 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300700 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300701
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300702 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200703 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300704 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300705
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300706 link->reverse = backlink;
707 backlink->reverse = link;
708
709 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300710 sink->num_links++;
711 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300712
713 return 0;
714}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300715EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300716
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200717int media_create_pad_links(const struct media_device *mdev,
718 const u32 source_function,
719 struct media_entity *source,
720 const u16 source_pad,
721 const u32 sink_function,
722 struct media_entity *sink,
723 const u16 sink_pad,
724 u32 flags,
725 const bool allow_both_undefined)
726{
727 struct media_entity *entity;
728 unsigned function;
729 int ret;
730
731 /* Trivial case: 1:1 relation */
732 if (source && sink)
733 return media_create_pad_link(source, source_pad,
734 sink, sink_pad, flags);
735
736 /* Worse case scenario: n:n relation */
737 if (!source && !sink) {
738 if (!allow_both_undefined)
739 return 0;
740 media_device_for_each_entity(source, mdev) {
741 if (source->function != source_function)
742 continue;
743 media_device_for_each_entity(sink, mdev) {
744 if (sink->function != sink_function)
745 continue;
746 ret = media_create_pad_link(source, source_pad,
747 sink, sink_pad,
748 flags);
749 if (ret)
750 return ret;
751 flags &= ~(MEDIA_LNK_FL_ENABLED |
752 MEDIA_LNK_FL_IMMUTABLE);
753 }
754 }
755 return 0;
756 }
757
758 /* Handle 1:n and n:1 cases */
759 if (source)
760 function = sink_function;
761 else
762 function = source_function;
763
764 media_device_for_each_entity(entity, mdev) {
765 if (entity->function != function)
766 continue;
767
768 if (source)
769 ret = media_create_pad_link(source, source_pad,
770 entity, sink_pad, flags);
771 else
772 ret = media_create_pad_link(entity, source_pad,
773 sink, sink_pad, flags);
774 if (ret)
775 return ret;
776 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
777 }
778 return 0;
779}
780EXPORT_SYMBOL_GPL(media_create_pad_links);
781
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300782void __media_entity_remove_links(struct media_entity *entity)
783{
784 struct media_link *link, *tmp;
785
786 list_for_each_entry_safe(link, tmp, &entity->links, list)
787 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300788
789 entity->num_links = 0;
790 entity->num_backlinks = 0;
791}
792EXPORT_SYMBOL_GPL(__media_entity_remove_links);
793
794void media_entity_remove_links(struct media_entity *entity)
795{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200796 struct media_device *mdev = entity->graph_obj.mdev;
797
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300798 /* Do nothing if the entity is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200799 if (mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300800 return;
801
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300802 mutex_lock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300803 __media_entity_remove_links(entity);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300804 mutex_unlock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300805}
806EXPORT_SYMBOL_GPL(media_entity_remove_links);
807
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300808static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
809{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300810 int ret;
811
812 /* Notify both entities. */
813 ret = media_entity_call(link->source->entity, link_setup,
814 link->source, link->sink, flags);
815 if (ret < 0 && ret != -ENOIOCTLCMD)
816 return ret;
817
818 ret = media_entity_call(link->sink->entity, link_setup,
819 link->sink, link->source, flags);
820 if (ret < 0 && ret != -ENOIOCTLCMD) {
821 media_entity_call(link->source->entity, link_setup,
822 link->source, link->sink, link->flags);
823 return ret;
824 }
825
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300826 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300827 link->reverse->flags = link->flags;
828
829 return 0;
830}
831
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300832int __media_entity_setup_link(struct media_link *link, u32 flags)
833{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300834 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300835 struct media_device *mdev;
836 struct media_entity *source, *sink;
837 int ret = -EBUSY;
838
839 if (link == NULL)
840 return -EINVAL;
841
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300842 /* The non-modifiable link flags must not be modified. */
843 if ((link->flags & ~mask) != (flags & ~mask))
844 return -EINVAL;
845
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300846 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
847 return link->flags == flags ? 0 : -EINVAL;
848
849 if (link->flags == flags)
850 return 0;
851
852 source = link->source->entity;
853 sink = link->sink->entity;
854
Laurent Pincharte02188c2010-08-25 09:00:41 -0300855 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
856 (source->stream_count || sink->stream_count))
857 return -EBUSY;
858
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300859 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300860
Laurent Pinchart68429f52015-11-03 00:27:51 -0200861 if (mdev->ops && mdev->ops->link_notify) {
862 ret = mdev->ops->link_notify(link, flags,
863 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300864 if (ret < 0)
865 return ret;
866 }
867
868 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300869
Laurent Pinchart68429f52015-11-03 00:27:51 -0200870 if (mdev->ops && mdev->ops->link_notify)
871 mdev->ops->link_notify(link, flags,
872 MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300873
874 return ret;
875}
Shuah Khanefc70272016-02-11 21:41:23 -0200876EXPORT_SYMBOL_GPL(__media_entity_setup_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300877
878int media_entity_setup_link(struct media_link *link, u32 flags)
879{
880 int ret;
881
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200882 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300883 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200884 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300885
886 return ret;
887}
888EXPORT_SYMBOL_GPL(media_entity_setup_link);
889
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300890struct media_link *
891media_entity_find_link(struct media_pad *source, struct media_pad *sink)
892{
893 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300894
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300895 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300896 if (link->source->entity == source->entity &&
897 link->source->index == source->index &&
898 link->sink->entity == sink->entity &&
899 link->sink->index == sink->index)
900 return link;
901 }
902
903 return NULL;
904}
905EXPORT_SYMBOL_GPL(media_entity_find_link);
906
Todor Tomov6538b022017-07-03 08:08:11 -0400907struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300908{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300909 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300910
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300911 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300912 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
913 continue;
914
915 if (link->source == pad)
916 return link->sink;
917
918 if (link->sink == pad)
919 return link->source;
920 }
921
922 return NULL;
923
924}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300925EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300926
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300927static void media_interface_init(struct media_device *mdev,
928 struct media_interface *intf,
929 u32 gobj_type,
930 u32 intf_type, u32 flags)
931{
932 intf->type = intf_type;
933 intf->flags = flags;
934 INIT_LIST_HEAD(&intf->links);
935
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200936 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300937}
938
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300939/* Functions related to the media interface via device nodes */
940
941struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
942 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300943 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300944{
945 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300946
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300947 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300948 if (!devnode)
949 return NULL;
950
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300951 devnode->major = major;
952 devnode->minor = minor;
953
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300954 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
955 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300956
957 return devnode;
958}
959EXPORT_SYMBOL_GPL(media_devnode_create);
960
961void media_devnode_remove(struct media_intf_devnode *devnode)
962{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300963 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200964 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300965 kfree(devnode);
966}
967EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300968
969struct media_link *media_create_intf_link(struct media_entity *entity,
970 struct media_interface *intf,
971 u32 flags)
972{
973 struct media_link *link;
974
975 link = media_add_link(&intf->links);
976 if (link == NULL)
977 return NULL;
978
979 link->intf = intf;
980 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200981 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300982
983 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200984 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300985 &link->graph_obj);
986
987 return link;
988}
989EXPORT_SYMBOL_GPL(media_create_intf_link);
990
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300991void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300992{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300993 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200994 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300995 kfree(link);
996}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300997EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300998
999void media_remove_intf_link(struct media_link *link)
1000{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001001 struct media_device *mdev = link->graph_obj.mdev;
1002
1003 /* Do nothing if the intf is not registered. */
1004 if (mdev == NULL)
1005 return;
1006
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001007 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001008 __media_remove_intf_link(link);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001009 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001010}
1011EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001012
1013void __media_remove_intf_links(struct media_interface *intf)
1014{
1015 struct media_link *link, *tmp;
1016
1017 list_for_each_entry_safe(link, tmp, &intf->links, list)
1018 __media_remove_intf_link(link);
1019
1020}
1021EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1022
1023void media_remove_intf_links(struct media_interface *intf)
1024{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001025 struct media_device *mdev = intf->graph_obj.mdev;
1026
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001027 /* Do nothing if the intf is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001028 if (mdev == NULL)
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001029 return;
1030
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001031 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001032 __media_remove_intf_links(intf);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001033 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001034}
1035EXPORT_SYMBOL_GPL(media_remove_intf_links);