blob: 98b067b712b3a98229cf99a18786333fb5671f2d [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:
Shuah Khan66c1db12016-03-04 18:14:05 -030049 return "dvb-frontend";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030050 case MEDIA_INTF_T_DVB_DEMUX:
Shuah Khan66c1db12016-03-04 18:14:05 -030051 return "dvb-demux";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030052 case MEDIA_INTF_T_DVB_DVR:
Shuah Khan66c1db12016-03-04 18:14:05 -030053 return "dvb-dvr";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030054 case MEDIA_INTF_T_DVB_CA:
Shuah Khan66c1db12016-03-04 18:14:05 -030055 return "dvb-ca";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030056 case MEDIA_INTF_T_DVB_NET:
Shuah Khan66c1db12016-03-04 18:14:05 -030057 return "dvb-net";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030058 case MEDIA_INTF_T_V4L_VIDEO:
Shuah Khan66c1db12016-03-04 18:14:05 -030059 return "v4l-video";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030060 case MEDIA_INTF_T_V4L_VBI:
Shuah Khan66c1db12016-03-04 18:14:05 -030061 return "v4l-vbi";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030062 case MEDIA_INTF_T_V4L_RADIO:
Shuah Khan66c1db12016-03-04 18:14:05 -030063 return "v4l-radio";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030064 case MEDIA_INTF_T_V4L_SUBDEV:
Shuah Khan66c1db12016-03-04 18:14:05 -030065 return "v4l-subdev";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030066 case MEDIA_INTF_T_V4L_SWRADIO:
Shuah Khan66c1db12016-03-04 18:14:05 -030067 return "v4l-swradio";
Nick Dyerb2fe22d2016-07-18 18:10:30 -030068 case MEDIA_INTF_T_V4L_TOUCH:
69 return "v4l-touch";
Shuah Khan5af557a2016-02-11 21:41:18 -020070 case MEDIA_INTF_T_ALSA_PCM_CAPTURE:
Shuah Khan66c1db12016-03-04 18:14:05 -030071 return "alsa-pcm-capture";
Shuah Khan5af557a2016-02-11 21:41:18 -020072 case MEDIA_INTF_T_ALSA_PCM_PLAYBACK:
Shuah Khan66c1db12016-03-04 18:14:05 -030073 return "alsa-pcm-playback";
Shuah Khan5af557a2016-02-11 21:41:18 -020074 case MEDIA_INTF_T_ALSA_CONTROL:
75 return "alsa-control";
76 case MEDIA_INTF_T_ALSA_COMPRESS:
Shuah Khan66c1db12016-03-04 18:14:05 -030077 return "alsa-compress";
Shuah Khan5af557a2016-02-11 21:41:18 -020078 case MEDIA_INTF_T_ALSA_RAWMIDI:
Shuah Khan66c1db12016-03-04 18:14:05 -030079 return "alsa-rawmidi";
Shuah Khan5af557a2016-02-11 21:41:18 -020080 case MEDIA_INTF_T_ALSA_HWDEP:
Shuah Khan66c1db12016-03-04 18:14:05 -030081 return "alsa-hwdep";
Shuah Khan5af557a2016-02-11 21:41:18 -020082 case MEDIA_INTF_T_ALSA_SEQUENCER:
Shuah Khan66c1db12016-03-04 18:14:05 -030083 return "alsa-sequencer";
Shuah Khan5af557a2016-02-11 21:41:18 -020084 case MEDIA_INTF_T_ALSA_TIMER:
Shuah Khan66c1db12016-03-04 18:14:05 -030085 return "alsa-timer";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030086 default:
87 return "unknown-intf";
88 }
89};
90
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020091__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
92 int idx_max)
93{
Sakari Ailusf7b5dff2016-01-27 12:47:54 -020094 idx_max = ALIGN(idx_max, BITS_PER_LONG);
95 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
96 GFP_KERNEL);
Sakari Ailus030e89e2015-12-16 11:32:36 -020097 if (!ent_enum->bmap)
98 return -ENOMEM;
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020099
100 bitmap_zero(ent_enum->bmap, idx_max);
101 ent_enum->idx_max = idx_max;
102
103 return 0;
104}
105EXPORT_SYMBOL_GPL(__media_entity_enum_init);
106
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200107void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
108{
Sakari Ailus030e89e2015-12-16 11:32:36 -0200109 kfree(ent_enum->bmap);
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200110}
111EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
112
113/**
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200114 * dev_dbg_obj - Prints in debug mode a change on some object
115 *
116 * @event_name: Name of the event to report. Could be __func__
117 * @gobj: Pointer to the object
118 *
119 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
120 * won't produce any code.
121 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300122static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
123{
124#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
125 switch (media_type(gobj)) {
126 case MEDIA_GRAPH_ENTITY:
127 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200128 "%s id %u: entity '%s'\n",
129 event_name, media_id(gobj),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300130 gobj_to_entity(gobj)->name);
131 break;
132 case MEDIA_GRAPH_LINK:
133 {
134 struct media_link *link = gobj_to_link(gobj);
135
136 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200137 "%s id %u: %s link id %u ==> id %u\n",
138 event_name, media_id(gobj),
139 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
140 "data" : "interface",
141 media_id(link->gobj0),
142 media_id(link->gobj1));
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300143 break;
144 }
145 case MEDIA_GRAPH_PAD:
146 {
147 struct media_pad *pad = gobj_to_pad(gobj);
148
149 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200150 "%s id %u: %s%spad '%s':%d\n",
151 event_name, media_id(gobj),
152 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
Mauro Carvalho Chehab6c24d462015-08-21 18:26:42 -0300153 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300154 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300155 break;
156 }
157 case MEDIA_GRAPH_INTF_DEVNODE:
158 {
159 struct media_interface *intf = gobj_to_intf(gobj);
160 struct media_intf_devnode *devnode = intf_to_devnode(intf);
161
162 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200163 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
164 event_name, media_id(gobj),
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300165 intf_type(intf),
166 devnode->major, devnode->minor);
167 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300168 }
169 }
170#endif
171}
172
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200173void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300174 enum media_gobj_type type,
175 struct media_gobj *gobj)
176{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300177 BUG_ON(!mdev);
178
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300179 gobj->mdev = mdev;
180
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300181 /* Create a per-type unique object ID */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200182 gobj->id = media_gobj_gen_id(type, ++mdev->id);
183
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300184 switch (type) {
185 case MEDIA_GRAPH_ENTITY:
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300186 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300187 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300188 case MEDIA_GRAPH_PAD:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300189 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300190 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300191 case MEDIA_GRAPH_LINK:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300192 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300193 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300194 case MEDIA_GRAPH_INTF_DEVNODE:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300195 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300196 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300197 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300198
199 mdev->topology_version++;
200
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300201 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300202}
203
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200204void media_gobj_destroy(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300205{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300206 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300207
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300208 gobj->mdev->topology_version++;
209
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300210 /* Remove the object from mdev list */
211 list_del(&gobj->list);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300212}
213
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200214int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
215 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300216{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300217 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300218 unsigned int i;
219
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300220 entity->num_pads = num_pads;
221 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300222
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300223 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300224 mutex_lock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300225
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300226 for (i = 0; i < num_pads; i++) {
227 pads[i].entity = entity;
228 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300229 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200230 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300231 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300232 }
233
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300234 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300235 mutex_unlock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300236
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300237 return 0;
238}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200239EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300240
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300241/* -----------------------------------------------------------------------------
242 * Graph traversal
243 */
244
245static struct media_entity *
246media_entity_other(struct media_entity *entity, struct media_link *link)
247{
248 if (link->source->entity == entity)
249 return link->sink->entity;
250 else
251 return link->source->entity;
252}
253
254/* push an entity to traversal stack */
255static void stack_push(struct media_entity_graph *graph,
256 struct media_entity *entity)
257{
258 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
259 WARN_ON(1);
260 return;
261 }
262 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200263 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300264 graph->stack[graph->top].entity = entity;
265}
266
267static struct media_entity *stack_pop(struct media_entity_graph *graph)
268{
269 struct media_entity *entity;
270
271 entity = graph->stack[graph->top].entity;
272 graph->top--;
273
274 return entity;
275}
276
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300277#define link_top(en) ((en)->stack[(en)->top].link)
278#define stack_top(en) ((en)->stack[(en)->top].entity)
279
Sakari Ailus0798ce42015-12-16 11:32:37 -0200280/*
281 * TODO: Get rid of this.
282 */
Mauro Carvalho Chehab430a6722015-12-16 15:18:25 -0200283#define MEDIA_ENTITY_MAX_PADS 512
Sakari Ailus0798ce42015-12-16 11:32:37 -0200284
Sakari Ailuse03d2202015-12-16 11:32:22 -0200285/**
286 * media_entity_graph_walk_init - Allocate resources for graph walk
287 * @graph: Media graph structure that will be used to walk the graph
288 * @mdev: Media device
289 *
290 * Reserve resources for graph walk in media device's current
291 * state. The memory must be released using
292 * media_entity_graph_walk_free().
293 *
294 * Returns error on failure, zero on success.
295 */
296__must_check int media_entity_graph_walk_init(
297 struct media_entity_graph *graph, struct media_device *mdev)
298{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200299 return media_entity_enum_init(&graph->ent_enum, mdev);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200300}
301EXPORT_SYMBOL_GPL(media_entity_graph_walk_init);
302
303/**
304 * media_entity_graph_walk_cleanup - Release resources related to graph walking
305 * @graph: Media graph structure that was used to walk the graph
306 */
307void media_entity_graph_walk_cleanup(struct media_entity_graph *graph)
308{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200309 media_entity_enum_cleanup(&graph->ent_enum);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200310}
311EXPORT_SYMBOL_GPL(media_entity_graph_walk_cleanup);
312
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300313void media_entity_graph_walk_start(struct media_entity_graph *graph,
314 struct media_entity *entity)
315{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200316 media_entity_enum_zero(&graph->ent_enum);
317 media_entity_enum_set(&graph->ent_enum, entity);
318
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300319 graph->top = 0;
320 graph->stack[graph->top].entity = NULL;
321 stack_push(graph, entity);
322}
323EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
324
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300325struct 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 */
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200336 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);
351
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300352 /* Has the entity already been visited? */
Sakari Ailus29d8da02015-12-16 11:32:28 -0200353 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300354 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300355 continue;
356 }
357
358 /* Push the new entity to stack and start over. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300359 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300360 stack_push(graph, next);
361 }
362
363 return stack_pop(graph);
364}
365EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
366
367/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300368 * Pipeline management
369 */
370
Shuah Khanfb49f202016-02-11 21:41:24 -0200371__must_check int __media_entity_pipeline_start(struct media_entity *entity,
372 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300373{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300374 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus5dd87752015-12-16 11:32:21 -0200375 struct media_entity_graph *graph = &pipe->graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300376 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300377 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300378 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300379
Sakari Ailus74a41332015-12-16 11:32:29 -0200380 if (!pipe->streaming_count++) {
381 ret = media_entity_graph_walk_init(&pipe->graph, mdev);
382 if (ret)
383 goto error_graph_walk_start;
Sakari Ailus106b9902015-12-16 15:32:23 +0200384 }
385
386 media_entity_graph_walk_start(&pipe->graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300387
Sakari Ailus5dd87752015-12-16 11:32:21 -0200388 while ((entity = media_entity_graph_walk_next(graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300389 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
390 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300391
Laurent Pincharte02188c2010-08-25 09:00:41 -0300392 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200393
394 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
395 ret = -EBUSY;
396 goto error;
397 }
398
Laurent Pincharte02188c2010-08-25 09:00:41 -0300399 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300400
401 /* Already streaming --- no need to check. */
402 if (entity->stream_count > 1)
403 continue;
404
405 if (!entity->ops || !entity->ops->link_validate)
406 continue;
407
Sakari Ailusde49c282013-10-13 08:00:26 -0300408 bitmap_zero(active, entity->num_pads);
409 bitmap_fill(has_no_links, entity->num_pads);
410
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300411 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300412 struct media_pad *pad = link->sink->entity == entity
413 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300414
Sakari Ailusde49c282013-10-13 08:00:26 -0300415 /* Mark that a pad is connected by a link. */
416 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300417
Sakari Ailusde49c282013-10-13 08:00:26 -0300418 /*
419 * Pads that either do not need to connect or
420 * are connected through an enabled link are
421 * fine.
422 */
423 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
424 link->flags & MEDIA_LNK_FL_ENABLED)
425 bitmap_set(active, pad->index, 1);
426
427 /*
428 * Link validation will only take place for
429 * sink ends of the link that are enabled.
430 */
431 if (link->sink != pad ||
432 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300433 continue;
434
435 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300436 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300437 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300438 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200439 link->source->entity->name,
440 link->source->index,
441 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300442 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300443 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300444 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300445
446 /* Either no links or validated links are fine. */
447 bitmap_or(active, active, has_no_links, entity->num_pads);
448
449 if (!bitmap_full(active, entity->num_pads)) {
Helen Mae Koike Fornazier47dfdb32015-06-28 16:14:10 -0300450 ret = -ENOLINK;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300451 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300452 "\"%s\":%u must be connected by an enabled link\n",
453 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300454 (unsigned)find_first_zero_bit(
455 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300456 goto error;
457 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300458 }
459
Sakari Ailusaf88be32012-01-11 06:25:15 -0300460 return 0;
461
462error:
463 /*
464 * Link validation on graph failed. We revert what we did and
465 * return the error.
466 */
Sakari Ailus5dd87752015-12-16 11:32:21 -0200467 media_entity_graph_walk_start(graph, entity_err);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300468
Sakari Ailus5dd87752015-12-16 11:32:21 -0200469 while ((entity_err = media_entity_graph_walk_next(graph))) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200470 /* don't let the stream_count go negative */
Sakari Ailusb96976c2016-07-20 08:32:49 -0300471 if (entity_err->stream_count > 0) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200472 entity_err->stream_count--;
473 if (entity_err->stream_count == 0)
474 entity_err->pipe = NULL;
475 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300476
477 /*
478 * We haven't increased stream_count further than this
479 * so we quit here.
480 */
481 if (entity_err == entity)
482 break;
483 }
484
Sakari Ailus74a41332015-12-16 11:32:29 -0200485error_graph_walk_start:
486 if (!--pipe->streaming_count)
487 media_entity_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200488
Shuah Khanfb49f202016-02-11 21:41:24 -0200489 return ret;
490}
491EXPORT_SYMBOL_GPL(__media_entity_pipeline_start);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300492
Shuah Khanfb49f202016-02-11 21:41:24 -0200493__must_check int media_entity_pipeline_start(struct media_entity *entity,
494 struct media_pipeline *pipe)
495{
496 struct media_device *mdev = entity->graph_obj.mdev;
497 int ret;
498
499 mutex_lock(&mdev->graph_mutex);
500 ret = __media_entity_pipeline_start(entity, pipe);
501 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300502 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300503}
504EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
505
Shuah Khanfb49f202016-02-11 21:41:24 -0200506void __media_entity_pipeline_stop(struct media_entity *entity)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300507{
Sakari Ailus5dd87752015-12-16 11:32:21 -0200508 struct media_entity_graph *graph = &entity->pipe->graph;
Sakari Ailus74a41332015-12-16 11:32:29 -0200509 struct media_pipeline *pipe = entity->pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300510
Laurent Pincharte02188c2010-08-25 09:00:41 -0300511
Sakari Ailus74a41332015-12-16 11:32:29 -0200512 WARN_ON(!pipe->streaming_count);
Sakari Ailus5dd87752015-12-16 11:32:21 -0200513 media_entity_graph_walk_start(graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300514
Sakari Ailus5dd87752015-12-16 11:32:21 -0200515 while ((entity = media_entity_graph_walk_next(graph))) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200516 /* don't let the stream_count go negative */
517 if (entity->stream_count > 0) {
518 entity->stream_count--;
519 if (entity->stream_count == 0)
520 entity->pipe = NULL;
521 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300522 }
523
Sakari Ailus74a41332015-12-16 11:32:29 -0200524 if (!--pipe->streaming_count)
525 media_entity_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200526
Shuah Khanfb49f202016-02-11 21:41:24 -0200527}
528EXPORT_SYMBOL_GPL(__media_entity_pipeline_stop);
529
530void media_entity_pipeline_stop(struct media_entity *entity)
531{
532 struct media_device *mdev = entity->graph_obj.mdev;
533
534 mutex_lock(&mdev->graph_mutex);
535 __media_entity_pipeline_stop(entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300536 mutex_unlock(&mdev->graph_mutex);
537}
538EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
539
540/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300541 * Module use count
542 */
543
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300544struct media_entity *media_entity_get(struct media_entity *entity)
545{
546 if (entity == NULL)
547 return NULL;
548
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300549 if (entity->graph_obj.mdev->dev &&
550 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300551 return NULL;
552
553 return entity;
554}
555EXPORT_SYMBOL_GPL(media_entity_get);
556
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300557void media_entity_put(struct media_entity *entity)
558{
559 if (entity == NULL)
560 return;
561
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300562 if (entity->graph_obj.mdev->dev)
563 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300564}
565EXPORT_SYMBOL_GPL(media_entity_put);
566
567/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300568 * Links management
569 */
570
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300571static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300572{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300573 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300574
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300575 link = kzalloc(sizeof(*link), GFP_KERNEL);
576 if (link == NULL)
577 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300578
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300579 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300580
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300581 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300582}
583
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300584static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200585 struct media_link *link)
586{
587 struct media_link *rlink, *tmp;
588 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200589
590 if (link->source->entity == entity)
591 remote = link->sink->entity;
592 else
593 remote = link->source->entity;
594
595 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200596 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200597 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200598
599 if (link->source->entity == entity)
600 remote->num_backlinks--;
601
602 /* Remove the remote link */
603 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200604 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200605 kfree(rlink);
606
607 if (--remote->num_links == 0)
608 break;
609 }
610 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200611 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200612 kfree(link);
613}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300614
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300615int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300616media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300617 struct media_entity *sink, u16 sink_pad, u32 flags)
618{
619 struct media_link *link;
620 struct media_link *backlink;
621
622 BUG_ON(source == NULL || sink == NULL);
623 BUG_ON(source_pad >= source->num_pads);
624 BUG_ON(sink_pad >= sink->num_pads);
625
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300626 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300627 if (link == NULL)
628 return -ENOMEM;
629
630 link->source = &source->pads[source_pad];
631 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200632 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300633
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300634 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200635 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300636 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300637
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300638 /* Create the backlink. Backlinks are used to help graph traversal and
639 * are not reported to userspace.
640 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300641 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300642 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300643 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300644 return -ENOMEM;
645 }
646
647 backlink->source = &source->pads[source_pad];
648 backlink->sink = &sink->pads[sink_pad];
649 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300650 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300651
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300652 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200653 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300654 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300655
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300656 link->reverse = backlink;
657 backlink->reverse = link;
658
659 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300660 sink->num_links++;
661 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300662
663 return 0;
664}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300665EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300666
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200667int media_create_pad_links(const struct media_device *mdev,
668 const u32 source_function,
669 struct media_entity *source,
670 const u16 source_pad,
671 const u32 sink_function,
672 struct media_entity *sink,
673 const u16 sink_pad,
674 u32 flags,
675 const bool allow_both_undefined)
676{
677 struct media_entity *entity;
678 unsigned function;
679 int ret;
680
681 /* Trivial case: 1:1 relation */
682 if (source && sink)
683 return media_create_pad_link(source, source_pad,
684 sink, sink_pad, flags);
685
686 /* Worse case scenario: n:n relation */
687 if (!source && !sink) {
688 if (!allow_both_undefined)
689 return 0;
690 media_device_for_each_entity(source, mdev) {
691 if (source->function != source_function)
692 continue;
693 media_device_for_each_entity(sink, mdev) {
694 if (sink->function != sink_function)
695 continue;
696 ret = media_create_pad_link(source, source_pad,
697 sink, sink_pad,
698 flags);
699 if (ret)
700 return ret;
701 flags &= ~(MEDIA_LNK_FL_ENABLED |
702 MEDIA_LNK_FL_IMMUTABLE);
703 }
704 }
705 return 0;
706 }
707
708 /* Handle 1:n and n:1 cases */
709 if (source)
710 function = sink_function;
711 else
712 function = source_function;
713
714 media_device_for_each_entity(entity, mdev) {
715 if (entity->function != function)
716 continue;
717
718 if (source)
719 ret = media_create_pad_link(source, source_pad,
720 entity, sink_pad, flags);
721 else
722 ret = media_create_pad_link(entity, source_pad,
723 sink, sink_pad, flags);
724 if (ret)
725 return ret;
726 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
727 }
728 return 0;
729}
730EXPORT_SYMBOL_GPL(media_create_pad_links);
731
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300732void __media_entity_remove_links(struct media_entity *entity)
733{
734 struct media_link *link, *tmp;
735
736 list_for_each_entry_safe(link, tmp, &entity->links, list)
737 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300738
739 entity->num_links = 0;
740 entity->num_backlinks = 0;
741}
742EXPORT_SYMBOL_GPL(__media_entity_remove_links);
743
744void media_entity_remove_links(struct media_entity *entity)
745{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200746 struct media_device *mdev = entity->graph_obj.mdev;
747
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300748 /* Do nothing if the entity is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200749 if (mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300750 return;
751
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300752 mutex_lock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300753 __media_entity_remove_links(entity);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300754 mutex_unlock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300755}
756EXPORT_SYMBOL_GPL(media_entity_remove_links);
757
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300758static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
759{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300760 int ret;
761
762 /* Notify both entities. */
763 ret = media_entity_call(link->source->entity, link_setup,
764 link->source, link->sink, flags);
765 if (ret < 0 && ret != -ENOIOCTLCMD)
766 return ret;
767
768 ret = media_entity_call(link->sink->entity, link_setup,
769 link->sink, link->source, flags);
770 if (ret < 0 && ret != -ENOIOCTLCMD) {
771 media_entity_call(link->source->entity, link_setup,
772 link->source, link->sink, link->flags);
773 return ret;
774 }
775
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300776 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300777 link->reverse->flags = link->flags;
778
779 return 0;
780}
781
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300782int __media_entity_setup_link(struct media_link *link, u32 flags)
783{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300784 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300785 struct media_device *mdev;
786 struct media_entity *source, *sink;
787 int ret = -EBUSY;
788
789 if (link == NULL)
790 return -EINVAL;
791
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300792 /* The non-modifiable link flags must not be modified. */
793 if ((link->flags & ~mask) != (flags & ~mask))
794 return -EINVAL;
795
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300796 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
797 return link->flags == flags ? 0 : -EINVAL;
798
799 if (link->flags == flags)
800 return 0;
801
802 source = link->source->entity;
803 sink = link->sink->entity;
804
Laurent Pincharte02188c2010-08-25 09:00:41 -0300805 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
806 (source->stream_count || sink->stream_count))
807 return -EBUSY;
808
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300809 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300810
Laurent Pinchart68429f52015-11-03 00:27:51 -0200811 if (mdev->ops && mdev->ops->link_notify) {
812 ret = mdev->ops->link_notify(link, flags,
813 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300814 if (ret < 0)
815 return ret;
816 }
817
818 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300819
Laurent Pinchart68429f52015-11-03 00:27:51 -0200820 if (mdev->ops && mdev->ops->link_notify)
821 mdev->ops->link_notify(link, flags,
822 MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300823
824 return ret;
825}
Shuah Khanefc70272016-02-11 21:41:23 -0200826EXPORT_SYMBOL_GPL(__media_entity_setup_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300827
828int media_entity_setup_link(struct media_link *link, u32 flags)
829{
830 int ret;
831
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200832 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300833 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200834 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300835
836 return ret;
837}
838EXPORT_SYMBOL_GPL(media_entity_setup_link);
839
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300840struct media_link *
841media_entity_find_link(struct media_pad *source, struct media_pad *sink)
842{
843 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300844
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300845 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300846 if (link->source->entity == source->entity &&
847 link->source->index == source->index &&
848 link->sink->entity == sink->entity &&
849 link->sink->index == sink->index)
850 return link;
851 }
852
853 return NULL;
854}
855EXPORT_SYMBOL_GPL(media_entity_find_link);
856
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300857struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300858{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300859 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300860
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300861 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300862 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
863 continue;
864
865 if (link->source == pad)
866 return link->sink;
867
868 if (link->sink == pad)
869 return link->source;
870 }
871
872 return NULL;
873
874}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300875EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300876
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300877static void media_interface_init(struct media_device *mdev,
878 struct media_interface *intf,
879 u32 gobj_type,
880 u32 intf_type, u32 flags)
881{
882 intf->type = intf_type;
883 intf->flags = flags;
884 INIT_LIST_HEAD(&intf->links);
885
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200886 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300887}
888
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300889/* Functions related to the media interface via device nodes */
890
891struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
892 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300893 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300894{
895 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300896
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300897 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300898 if (!devnode)
899 return NULL;
900
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300901 devnode->major = major;
902 devnode->minor = minor;
903
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300904 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
905 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300906
907 return devnode;
908}
909EXPORT_SYMBOL_GPL(media_devnode_create);
910
911void media_devnode_remove(struct media_intf_devnode *devnode)
912{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300913 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200914 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300915 kfree(devnode);
916}
917EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300918
919struct media_link *media_create_intf_link(struct media_entity *entity,
920 struct media_interface *intf,
921 u32 flags)
922{
923 struct media_link *link;
924
925 link = media_add_link(&intf->links);
926 if (link == NULL)
927 return NULL;
928
929 link->intf = intf;
930 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200931 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300932
933 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200934 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300935 &link->graph_obj);
936
937 return link;
938}
939EXPORT_SYMBOL_GPL(media_create_intf_link);
940
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300941void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300942{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300943 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200944 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300945 kfree(link);
946}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300947EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300948
949void media_remove_intf_link(struct media_link *link)
950{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200951 struct media_device *mdev = link->graph_obj.mdev;
952
953 /* Do nothing if the intf is not registered. */
954 if (mdev == NULL)
955 return;
956
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300957 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300958 __media_remove_intf_link(link);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300959 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300960}
961EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300962
963void __media_remove_intf_links(struct media_interface *intf)
964{
965 struct media_link *link, *tmp;
966
967 list_for_each_entry_safe(link, tmp, &intf->links, list)
968 __media_remove_intf_link(link);
969
970}
971EXPORT_SYMBOL_GPL(__media_remove_intf_links);
972
973void media_remove_intf_links(struct media_interface *intf)
974{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200975 struct media_device *mdev = intf->graph_obj.mdev;
976
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300977 /* Do nothing if the intf is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200978 if (mdev == NULL)
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300979 return;
980
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300981 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300982 __media_remove_intf_links(intf);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300983 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300984}
985EXPORT_SYMBOL_GPL(media_remove_intf_links);