blob: f7c6d64e60310c2fe83916fb83c9a162dc607d36 [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";
Shuah Khan5af557a2016-02-11 21:41:18 -020067 case MEDIA_INTF_T_ALSA_PCM_CAPTURE:
Shuah Khan66c1db12016-03-04 18:14:05 -030068 return "alsa-pcm-capture";
Shuah Khan5af557a2016-02-11 21:41:18 -020069 case MEDIA_INTF_T_ALSA_PCM_PLAYBACK:
Shuah Khan66c1db12016-03-04 18:14:05 -030070 return "alsa-pcm-playback";
Shuah Khan5af557a2016-02-11 21:41:18 -020071 case MEDIA_INTF_T_ALSA_CONTROL:
72 return "alsa-control";
73 case MEDIA_INTF_T_ALSA_COMPRESS:
Shuah Khan66c1db12016-03-04 18:14:05 -030074 return "alsa-compress";
Shuah Khan5af557a2016-02-11 21:41:18 -020075 case MEDIA_INTF_T_ALSA_RAWMIDI:
Shuah Khan66c1db12016-03-04 18:14:05 -030076 return "alsa-rawmidi";
Shuah Khan5af557a2016-02-11 21:41:18 -020077 case MEDIA_INTF_T_ALSA_HWDEP:
Shuah Khan66c1db12016-03-04 18:14:05 -030078 return "alsa-hwdep";
Shuah Khan5af557a2016-02-11 21:41:18 -020079 case MEDIA_INTF_T_ALSA_SEQUENCER:
Shuah Khan66c1db12016-03-04 18:14:05 -030080 return "alsa-sequencer";
Shuah Khan5af557a2016-02-11 21:41:18 -020081 case MEDIA_INTF_T_ALSA_TIMER:
Shuah Khan66c1db12016-03-04 18:14:05 -030082 return "alsa-timer";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030083 default:
84 return "unknown-intf";
85 }
86};
87
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020088__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
89 int idx_max)
90{
Sakari Ailusf7b5dff2016-01-27 12:47:54 -020091 idx_max = ALIGN(idx_max, BITS_PER_LONG);
92 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
93 GFP_KERNEL);
Sakari Ailus030e89e2015-12-16 11:32:36 -020094 if (!ent_enum->bmap)
95 return -ENOMEM;
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020096
97 bitmap_zero(ent_enum->bmap, idx_max);
98 ent_enum->idx_max = idx_max;
99
100 return 0;
101}
102EXPORT_SYMBOL_GPL(__media_entity_enum_init);
103
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200104void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
105{
Sakari Ailus030e89e2015-12-16 11:32:36 -0200106 kfree(ent_enum->bmap);
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200107}
108EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
109
110/**
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200111 * dev_dbg_obj - Prints in debug mode a change on some object
112 *
113 * @event_name: Name of the event to report. Could be __func__
114 * @gobj: Pointer to the object
115 *
116 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
117 * won't produce any code.
118 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300119static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
120{
121#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
122 switch (media_type(gobj)) {
123 case MEDIA_GRAPH_ENTITY:
124 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200125 "%s id %u: entity '%s'\n",
126 event_name, media_id(gobj),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300127 gobj_to_entity(gobj)->name);
128 break;
129 case MEDIA_GRAPH_LINK:
130 {
131 struct media_link *link = gobj_to_link(gobj);
132
133 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200134 "%s id %u: %s link id %u ==> id %u\n",
135 event_name, media_id(gobj),
136 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
137 "data" : "interface",
138 media_id(link->gobj0),
139 media_id(link->gobj1));
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300140 break;
141 }
142 case MEDIA_GRAPH_PAD:
143 {
144 struct media_pad *pad = gobj_to_pad(gobj);
145
146 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200147 "%s id %u: %s%spad '%s':%d\n",
148 event_name, media_id(gobj),
149 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
Mauro Carvalho Chehab6c24d462015-08-21 18:26:42 -0300150 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300151 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300152 break;
153 }
154 case MEDIA_GRAPH_INTF_DEVNODE:
155 {
156 struct media_interface *intf = gobj_to_intf(gobj);
157 struct media_intf_devnode *devnode = intf_to_devnode(intf);
158
159 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200160 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
161 event_name, media_id(gobj),
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300162 intf_type(intf),
163 devnode->major, devnode->minor);
164 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300165 }
166 }
167#endif
168}
169
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200170void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300171 enum media_gobj_type type,
172 struct media_gobj *gobj)
173{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300174 BUG_ON(!mdev);
175
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300176 gobj->mdev = mdev;
177
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300178 /* Create a per-type unique object ID */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200179 gobj->id = media_gobj_gen_id(type, ++mdev->id);
180
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300181 switch (type) {
182 case MEDIA_GRAPH_ENTITY:
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300183 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300184 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300185 case MEDIA_GRAPH_PAD:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300186 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300187 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300188 case MEDIA_GRAPH_LINK:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300189 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300190 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300191 case MEDIA_GRAPH_INTF_DEVNODE:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300192 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300193 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300194 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300195
196 mdev->topology_version++;
197
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300198 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300199}
200
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200201void media_gobj_destroy(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300202{
Max Kellermann67537432016-08-09 23:32:57 +0200203 /* Do nothing if the object is not linked. */
204 if (gobj->mdev == NULL)
205 return;
206
Helen Fornazier8d1d3d02017-04-06 16:25:15 -0300207 dev_dbg_obj(__func__, gobj);
208
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300209 gobj->mdev->topology_version++;
210
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300211 /* Remove the object from mdev list */
212 list_del(&gobj->list);
Max Kellermann67537432016-08-09 23:32:57 +0200213
214 gobj->mdev = NULL;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300215}
216
Sakari Ailus885ca802015-10-05 12:45:29 -0300217/*
218 * TODO: Get rid of this.
219 */
220#define MEDIA_ENTITY_MAX_PADS 512
221
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200222int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
223 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300224{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300225 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300226 unsigned int i;
227
Sakari Ailus885ca802015-10-05 12:45:29 -0300228 if (num_pads >= MEDIA_ENTITY_MAX_PADS)
229 return -E2BIG;
230
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300231 entity->num_pads = num_pads;
232 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300233
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300234 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300235 mutex_lock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300236
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300237 for (i = 0; i < num_pads; i++) {
238 pads[i].entity = entity;
239 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300240 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200241 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300242 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300243 }
244
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300245 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300246 mutex_unlock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300247
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300248 return 0;
249}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200250EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300251
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300252/* -----------------------------------------------------------------------------
253 * Graph traversal
254 */
255
256static struct media_entity *
257media_entity_other(struct media_entity *entity, struct media_link *link)
258{
259 if (link->source->entity == entity)
260 return link->sink->entity;
261 else
262 return link->source->entity;
263}
264
265/* push an entity to traversal stack */
Sakari Ailus20b85222016-11-21 14:48:30 -0200266static void stack_push(struct media_graph *graph,
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300267 struct media_entity *entity)
268{
269 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
270 WARN_ON(1);
271 return;
272 }
273 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200274 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300275 graph->stack[graph->top].entity = entity;
276}
277
Sakari Ailus20b85222016-11-21 14:48:30 -0200278static struct media_entity *stack_pop(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300279{
280 struct media_entity *entity;
281
282 entity = graph->stack[graph->top].entity;
283 graph->top--;
284
285 return entity;
286}
287
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300288#define link_top(en) ((en)->stack[(en)->top].link)
289#define stack_top(en) ((en)->stack[(en)->top].entity)
290
Sakari Ailuse03d2202015-12-16 11:32:22 -0200291/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200292 * media_graph_walk_init - Allocate resources for graph walk
Sakari Ailuse03d2202015-12-16 11:32:22 -0200293 * @graph: Media graph structure that will be used to walk the graph
294 * @mdev: Media device
295 *
296 * Reserve resources for graph walk in media device's current
297 * state. The memory must be released using
Sakari Ailus20b85222016-11-21 14:48:30 -0200298 * media_graph_walk_free().
Sakari Ailuse03d2202015-12-16 11:32:22 -0200299 *
300 * Returns error on failure, zero on success.
301 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200302__must_check int media_graph_walk_init(
303 struct media_graph *graph, struct media_device *mdev)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200304{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200305 return media_entity_enum_init(&graph->ent_enum, mdev);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200306}
Sakari Ailus20b85222016-11-21 14:48:30 -0200307EXPORT_SYMBOL_GPL(media_graph_walk_init);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200308
309/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200310 * media_graph_walk_cleanup - Release resources related to graph walking
Sakari Ailuse03d2202015-12-16 11:32:22 -0200311 * @graph: Media graph structure that was used to walk the graph
312 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200313void media_graph_walk_cleanup(struct media_graph *graph)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200314{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200315 media_entity_enum_cleanup(&graph->ent_enum);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200316}
Sakari Ailus20b85222016-11-21 14:48:30 -0200317EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200318
Sakari Ailus20b85222016-11-21 14:48:30 -0200319void media_graph_walk_start(struct media_graph *graph,
320 struct media_entity *entity)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300321{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200322 media_entity_enum_zero(&graph->ent_enum);
323 media_entity_enum_set(&graph->ent_enum, entity);
324
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300325 graph->top = 0;
326 graph->stack[graph->top].entity = NULL;
327 stack_push(graph, entity);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300328 dev_dbg(entity->graph_obj.mdev->dev,
329 "begin graph walk at '%s'\n", entity->name);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300330}
Sakari Ailus20b85222016-11-21 14:48:30 -0200331EXPORT_SYMBOL_GPL(media_graph_walk_start);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300332
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300333static void media_graph_walk_iter(struct media_graph *graph)
334{
335 struct media_entity *entity = stack_top(graph);
336 struct media_link *link;
337 struct media_entity *next;
338
339 link = list_entry(link_top(graph), typeof(*link), list);
340
341 /* The link is not enabled so we do not follow. */
342 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
343 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300344 dev_dbg(entity->graph_obj.mdev->dev,
345 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
346 link->source->entity->name, link->source->index,
347 link->sink->entity->name, link->sink->index);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300348 return;
349 }
350
351 /* Get the entity in the other end of the link . */
352 next = media_entity_other(entity, link);
353
354 /* Has the entity already been visited? */
355 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
356 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300357 dev_dbg(entity->graph_obj.mdev->dev,
358 "walk: skipping entity '%s' (already seen)\n",
359 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300360 return;
361 }
362
363 /* Push the new entity to stack and start over. */
364 link_top(graph) = link_top(graph)->next;
365 stack_push(graph, next);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300366 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
367 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300368}
369
Sakari Ailus20b85222016-11-21 14:48:30 -0200370struct media_entity *media_graph_walk_next(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300371{
Sakari Ailusaa79a842016-07-20 12:39:02 -0300372 struct media_entity *entity;
373
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300374 if (stack_top(graph) == NULL)
375 return NULL;
376
377 /*
378 * Depth first search. Push entity to stack and continue from
379 * top of the stack until no more entities on the level can be
380 * found.
381 */
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300382 while (link_top(graph) != &stack_top(graph)->links)
383 media_graph_walk_iter(graph);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300384
Sakari Ailusaa79a842016-07-20 12:39:02 -0300385 entity = stack_pop(graph);
386 dev_dbg(entity->graph_obj.mdev->dev,
387 "walk: returning entity '%s'\n", entity->name);
388
389 return entity;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300390}
Sakari Ailus20b85222016-11-21 14:48:30 -0200391EXPORT_SYMBOL_GPL(media_graph_walk_next);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300392
Niklas Söderlundd295c6a2017-06-15 06:17:26 -0300393int media_entity_get_fwnode_pad(struct media_entity *entity,
394 struct fwnode_handle *fwnode,
395 unsigned long direction_flags)
396{
397 struct fwnode_endpoint endpoint;
398 unsigned int i;
399 int ret;
400
401 if (!entity->ops || !entity->ops->get_fwnode_pad) {
402 for (i = 0; i < entity->num_pads; i++) {
403 if (entity->pads[i].flags & direction_flags)
404 return i;
405 }
406
407 return -ENXIO;
408 }
409
410 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
411 if (ret)
412 return ret;
413
414 ret = entity->ops->get_fwnode_pad(&endpoint);
415 if (ret < 0)
416 return ret;
417
418 if (ret >= entity->num_pads)
419 return -ENXIO;
420
421 if (!(entity->pads[ret].flags & direction_flags))
422 return -ENXIO;
423
424 return ret;
425}
426EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
427
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300428/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300429 * Pipeline management
430 */
431
Sakari Ailus20b85222016-11-21 14:48:30 -0200432__must_check int __media_pipeline_start(struct media_entity *entity,
433 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300434{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300435 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus20b85222016-11-21 14:48:30 -0200436 struct media_graph *graph = &pipe->graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300437 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300438 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300439 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300440
Sakari Ailus74a413302015-12-16 11:32:29 -0200441 if (!pipe->streaming_count++) {
Sakari Ailus20b85222016-11-21 14:48:30 -0200442 ret = media_graph_walk_init(&pipe->graph, mdev);
Sakari Ailus74a413302015-12-16 11:32:29 -0200443 if (ret)
444 goto error_graph_walk_start;
Sakari Ailus106b9902015-12-16 15:32:23 +0200445 }
446
Sakari Ailus20b85222016-11-21 14:48:30 -0200447 media_graph_walk_start(&pipe->graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300448
Sakari Ailus20b85222016-11-21 14:48:30 -0200449 while ((entity = media_graph_walk_next(graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300450 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
451 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300452
Laurent Pincharte02188c2010-08-25 09:00:41 -0300453 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200454
455 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
456 ret = -EBUSY;
457 goto error;
458 }
459
Laurent Pincharte02188c2010-08-25 09:00:41 -0300460 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300461
462 /* Already streaming --- no need to check. */
463 if (entity->stream_count > 1)
464 continue;
465
466 if (!entity->ops || !entity->ops->link_validate)
467 continue;
468
Sakari Ailusde49c282013-10-13 08:00:26 -0300469 bitmap_zero(active, entity->num_pads);
470 bitmap_fill(has_no_links, entity->num_pads);
471
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300472 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300473 struct media_pad *pad = link->sink->entity == entity
474 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300475
Sakari Ailusde49c282013-10-13 08:00:26 -0300476 /* Mark that a pad is connected by a link. */
477 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300478
Sakari Ailusde49c282013-10-13 08:00:26 -0300479 /*
480 * Pads that either do not need to connect or
481 * are connected through an enabled link are
482 * fine.
483 */
484 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
485 link->flags & MEDIA_LNK_FL_ENABLED)
486 bitmap_set(active, pad->index, 1);
487
488 /*
489 * Link validation will only take place for
490 * sink ends of the link that are enabled.
491 */
492 if (link->sink != pad ||
493 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300494 continue;
495
496 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300497 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300498 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200499 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200500 link->source->entity->name,
501 link->source->index,
502 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300503 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300504 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300505 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300506
507 /* Either no links or validated links are fine. */
508 bitmap_or(active, active, has_no_links, entity->num_pads);
509
510 if (!bitmap_full(active, entity->num_pads)) {
Helen Mae Koike Fornazier47dfdb32015-06-28 16:14:10 -0300511 ret = -ENOLINK;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300512 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200513 "'%s':%u must be connected by an enabled link\n",
Sakari Ailusfab9d302014-10-28 20:35:04 -0300514 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300515 (unsigned)find_first_zero_bit(
516 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300517 goto error;
518 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300519 }
520
Sakari Ailusaf88be32012-01-11 06:25:15 -0300521 return 0;
522
523error:
524 /*
525 * Link validation on graph failed. We revert what we did and
526 * return the error.
527 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200528 media_graph_walk_start(graph, entity_err);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300529
Sakari Ailus20b85222016-11-21 14:48:30 -0200530 while ((entity_err = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300531 /* Sanity check for negative stream_count */
532 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200533 entity_err->stream_count--;
534 if (entity_err->stream_count == 0)
535 entity_err->pipe = NULL;
536 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300537
538 /*
539 * We haven't increased stream_count further than this
540 * so we quit here.
541 */
542 if (entity_err == entity)
543 break;
544 }
545
Sakari Ailus74a413302015-12-16 11:32:29 -0200546error_graph_walk_start:
547 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200548 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200549
Shuah Khanfb49f202016-02-11 21:41:24 -0200550 return ret;
551}
Sakari Ailus20b85222016-11-21 14:48:30 -0200552EXPORT_SYMBOL_GPL(__media_pipeline_start);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300553
Sakari Ailus20b85222016-11-21 14:48:30 -0200554__must_check int media_pipeline_start(struct media_entity *entity,
555 struct media_pipeline *pipe)
Shuah Khanfb49f202016-02-11 21:41:24 -0200556{
557 struct media_device *mdev = entity->graph_obj.mdev;
558 int ret;
559
560 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200561 ret = __media_pipeline_start(entity, pipe);
Shuah Khanfb49f202016-02-11 21:41:24 -0200562 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300563 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300564}
Sakari Ailus20b85222016-11-21 14:48:30 -0200565EXPORT_SYMBOL_GPL(media_pipeline_start);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300566
Sakari Ailus20b85222016-11-21 14:48:30 -0200567void __media_pipeline_stop(struct media_entity *entity)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300568{
Sakari Ailus20b85222016-11-21 14:48:30 -0200569 struct media_graph *graph = &entity->pipe->graph;
Sakari Ailus74a413302015-12-16 11:32:29 -0200570 struct media_pipeline *pipe = entity->pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300571
Kieran Bingham2a2599c2017-01-03 11:12:11 -0200572 /*
573 * If the following check fails, the driver has performed an
574 * unbalanced call to media_pipeline_stop()
575 */
576 if (WARN_ON(!pipe))
577 return;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300578
Sakari Ailus20b85222016-11-21 14:48:30 -0200579 media_graph_walk_start(graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300580
Sakari Ailus20b85222016-11-21 14:48:30 -0200581 while ((entity = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300582 /* Sanity check for negative stream_count */
583 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200584 entity->stream_count--;
585 if (entity->stream_count == 0)
586 entity->pipe = NULL;
587 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300588 }
589
Sakari Ailus74a413302015-12-16 11:32:29 -0200590 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200591 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200592
Shuah Khanfb49f202016-02-11 21:41:24 -0200593}
Sakari Ailus20b85222016-11-21 14:48:30 -0200594EXPORT_SYMBOL_GPL(__media_pipeline_stop);
Shuah Khanfb49f202016-02-11 21:41:24 -0200595
Sakari Ailus20b85222016-11-21 14:48:30 -0200596void media_pipeline_stop(struct media_entity *entity)
Shuah Khanfb49f202016-02-11 21:41:24 -0200597{
598 struct media_device *mdev = entity->graph_obj.mdev;
599
600 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200601 __media_pipeline_stop(entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300602 mutex_unlock(&mdev->graph_mutex);
603}
Sakari Ailus20b85222016-11-21 14:48:30 -0200604EXPORT_SYMBOL_GPL(media_pipeline_stop);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300605
606/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300607 * Module use count
608 */
609
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300610struct media_entity *media_entity_get(struct media_entity *entity)
611{
612 if (entity == NULL)
613 return NULL;
614
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300615 if (entity->graph_obj.mdev->dev &&
616 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300617 return NULL;
618
619 return entity;
620}
621EXPORT_SYMBOL_GPL(media_entity_get);
622
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300623void media_entity_put(struct media_entity *entity)
624{
625 if (entity == NULL)
626 return;
627
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300628 if (entity->graph_obj.mdev->dev)
629 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300630}
631EXPORT_SYMBOL_GPL(media_entity_put);
632
633/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300634 * Links management
635 */
636
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300637static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300638{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300639 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300640
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300641 link = kzalloc(sizeof(*link), GFP_KERNEL);
642 if (link == NULL)
643 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300644
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300645 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300646
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300647 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300648}
649
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300650static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200651 struct media_link *link)
652{
653 struct media_link *rlink, *tmp;
654 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200655
656 if (link->source->entity == entity)
657 remote = link->sink->entity;
658 else
659 remote = link->source->entity;
660
661 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200662 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200663 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200664
665 if (link->source->entity == entity)
666 remote->num_backlinks--;
667
668 /* Remove the remote link */
669 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200670 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200671 kfree(rlink);
672
673 if (--remote->num_links == 0)
674 break;
675 }
676 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200677 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200678 kfree(link);
679}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300680
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300681int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300682media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300683 struct media_entity *sink, u16 sink_pad, u32 flags)
684{
685 struct media_link *link;
686 struct media_link *backlink;
687
688 BUG_ON(source == NULL || sink == NULL);
689 BUG_ON(source_pad >= source->num_pads);
690 BUG_ON(sink_pad >= sink->num_pads);
691
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300692 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300693 if (link == NULL)
694 return -ENOMEM;
695
696 link->source = &source->pads[source_pad];
697 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200698 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300699
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300700 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200701 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300702 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300703
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300704 /* Create the backlink. Backlinks are used to help graph traversal and
705 * are not reported to userspace.
706 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300707 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300708 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300709 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300710 return -ENOMEM;
711 }
712
713 backlink->source = &source->pads[source_pad];
714 backlink->sink = &sink->pads[sink_pad];
715 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300716 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300717
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300718 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200719 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300720 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300721
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300722 link->reverse = backlink;
723 backlink->reverse = link;
724
725 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300726 sink->num_links++;
727 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300728
729 return 0;
730}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300731EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300732
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200733int media_create_pad_links(const struct media_device *mdev,
734 const u32 source_function,
735 struct media_entity *source,
736 const u16 source_pad,
737 const u32 sink_function,
738 struct media_entity *sink,
739 const u16 sink_pad,
740 u32 flags,
741 const bool allow_both_undefined)
742{
743 struct media_entity *entity;
744 unsigned function;
745 int ret;
746
747 /* Trivial case: 1:1 relation */
748 if (source && sink)
749 return media_create_pad_link(source, source_pad,
750 sink, sink_pad, flags);
751
752 /* Worse case scenario: n:n relation */
753 if (!source && !sink) {
754 if (!allow_both_undefined)
755 return 0;
756 media_device_for_each_entity(source, mdev) {
757 if (source->function != source_function)
758 continue;
759 media_device_for_each_entity(sink, mdev) {
760 if (sink->function != sink_function)
761 continue;
762 ret = media_create_pad_link(source, source_pad,
763 sink, sink_pad,
764 flags);
765 if (ret)
766 return ret;
767 flags &= ~(MEDIA_LNK_FL_ENABLED |
768 MEDIA_LNK_FL_IMMUTABLE);
769 }
770 }
771 return 0;
772 }
773
774 /* Handle 1:n and n:1 cases */
775 if (source)
776 function = sink_function;
777 else
778 function = source_function;
779
780 media_device_for_each_entity(entity, mdev) {
781 if (entity->function != function)
782 continue;
783
784 if (source)
785 ret = media_create_pad_link(source, source_pad,
786 entity, sink_pad, flags);
787 else
788 ret = media_create_pad_link(entity, source_pad,
789 sink, sink_pad, flags);
790 if (ret)
791 return ret;
792 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
793 }
794 return 0;
795}
796EXPORT_SYMBOL_GPL(media_create_pad_links);
797
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300798void __media_entity_remove_links(struct media_entity *entity)
799{
800 struct media_link *link, *tmp;
801
802 list_for_each_entry_safe(link, tmp, &entity->links, list)
803 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300804
805 entity->num_links = 0;
806 entity->num_backlinks = 0;
807}
808EXPORT_SYMBOL_GPL(__media_entity_remove_links);
809
810void media_entity_remove_links(struct media_entity *entity)
811{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200812 struct media_device *mdev = entity->graph_obj.mdev;
813
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300814 /* Do nothing if the entity is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200815 if (mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300816 return;
817
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300818 mutex_lock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300819 __media_entity_remove_links(entity);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300820 mutex_unlock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300821}
822EXPORT_SYMBOL_GPL(media_entity_remove_links);
823
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300824static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
825{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300826 int ret;
827
828 /* Notify both entities. */
829 ret = media_entity_call(link->source->entity, link_setup,
830 link->source, link->sink, flags);
831 if (ret < 0 && ret != -ENOIOCTLCMD)
832 return ret;
833
834 ret = media_entity_call(link->sink->entity, link_setup,
835 link->sink, link->source, flags);
836 if (ret < 0 && ret != -ENOIOCTLCMD) {
837 media_entity_call(link->source->entity, link_setup,
838 link->source, link->sink, link->flags);
839 return ret;
840 }
841
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300842 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300843 link->reverse->flags = link->flags;
844
845 return 0;
846}
847
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300848int __media_entity_setup_link(struct media_link *link, u32 flags)
849{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300850 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300851 struct media_device *mdev;
852 struct media_entity *source, *sink;
853 int ret = -EBUSY;
854
855 if (link == NULL)
856 return -EINVAL;
857
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300858 /* The non-modifiable link flags must not be modified. */
859 if ((link->flags & ~mask) != (flags & ~mask))
860 return -EINVAL;
861
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300862 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
863 return link->flags == flags ? 0 : -EINVAL;
864
865 if (link->flags == flags)
866 return 0;
867
868 source = link->source->entity;
869 sink = link->sink->entity;
870
Laurent Pincharte02188c2010-08-25 09:00:41 -0300871 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
872 (source->stream_count || sink->stream_count))
873 return -EBUSY;
874
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300875 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300876
Laurent Pinchart68429f52015-11-03 00:27:51 -0200877 if (mdev->ops && mdev->ops->link_notify) {
878 ret = mdev->ops->link_notify(link, flags,
879 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300880 if (ret < 0)
881 return ret;
882 }
883
884 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300885
Laurent Pinchart68429f52015-11-03 00:27:51 -0200886 if (mdev->ops && mdev->ops->link_notify)
887 mdev->ops->link_notify(link, flags,
888 MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300889
890 return ret;
891}
Shuah Khanefc70272016-02-11 21:41:23 -0200892EXPORT_SYMBOL_GPL(__media_entity_setup_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300893
894int media_entity_setup_link(struct media_link *link, u32 flags)
895{
896 int ret;
897
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200898 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300899 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200900 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300901
902 return ret;
903}
904EXPORT_SYMBOL_GPL(media_entity_setup_link);
905
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300906struct media_link *
907media_entity_find_link(struct media_pad *source, struct media_pad *sink)
908{
909 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, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300912 if (link->source->entity == source->entity &&
913 link->source->index == source->index &&
914 link->sink->entity == sink->entity &&
915 link->sink->index == sink->index)
916 return link;
917 }
918
919 return NULL;
920}
921EXPORT_SYMBOL_GPL(media_entity_find_link);
922
Todor Tomov6538b022017-07-03 08:08:11 -0400923struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300924{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300925 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300926
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300927 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300928 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
929 continue;
930
931 if (link->source == pad)
932 return link->sink;
933
934 if (link->sink == pad)
935 return link->source;
936 }
937
938 return NULL;
939
940}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300941EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300942
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300943static void media_interface_init(struct media_device *mdev,
944 struct media_interface *intf,
945 u32 gobj_type,
946 u32 intf_type, u32 flags)
947{
948 intf->type = intf_type;
949 intf->flags = flags;
950 INIT_LIST_HEAD(&intf->links);
951
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200952 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300953}
954
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300955/* Functions related to the media interface via device nodes */
956
957struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
958 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300959 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300960{
961 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300962
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300963 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300964 if (!devnode)
965 return NULL;
966
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300967 devnode->major = major;
968 devnode->minor = minor;
969
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300970 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
971 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300972
973 return devnode;
974}
975EXPORT_SYMBOL_GPL(media_devnode_create);
976
977void media_devnode_remove(struct media_intf_devnode *devnode)
978{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300979 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200980 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300981 kfree(devnode);
982}
983EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300984
985struct media_link *media_create_intf_link(struct media_entity *entity,
986 struct media_interface *intf,
987 u32 flags)
988{
989 struct media_link *link;
990
991 link = media_add_link(&intf->links);
992 if (link == NULL)
993 return NULL;
994
995 link->intf = intf;
996 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200997 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300998
999 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -02001000 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001001 &link->graph_obj);
1002
1003 return link;
1004}
1005EXPORT_SYMBOL_GPL(media_create_intf_link);
1006
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001007void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001008{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001009 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -02001010 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001011 kfree(link);
1012}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001013EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001014
1015void media_remove_intf_link(struct media_link *link)
1016{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001017 struct media_device *mdev = link->graph_obj.mdev;
1018
1019 /* Do nothing if the intf is not registered. */
1020 if (mdev == NULL)
1021 return;
1022
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001023 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001024 __media_remove_intf_link(link);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001025 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001026}
1027EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001028
1029void __media_remove_intf_links(struct media_interface *intf)
1030{
1031 struct media_link *link, *tmp;
1032
1033 list_for_each_entry_safe(link, tmp, &intf->links, list)
1034 __media_remove_intf_link(link);
1035
1036}
1037EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1038
1039void media_remove_intf_links(struct media_interface *intf)
1040{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001041 struct media_device *mdev = intf->graph_obj.mdev;
1042
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001043 /* Do nothing if the intf is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001044 if (mdev == NULL)
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001045 return;
1046
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001047 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001048 __media_remove_intf_links(intf);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001049 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001050}
1051EXPORT_SYMBOL_GPL(media_remove_intf_links);