blob: bc44193efa4798b449bb0cd345fd4bdbd4c25b6e [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>
21#include <linux/slab.h>
22#include <media/media-entity.h>
Laurent Pinchart503c3d822010-03-07 15:04:59 -030023#include <media/media-device.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030024
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030025static inline const char *gobj_type(enum media_gobj_type type)
26{
27 switch (type) {
28 case MEDIA_GRAPH_ENTITY:
29 return "entity";
30 case MEDIA_GRAPH_PAD:
31 return "pad";
32 case MEDIA_GRAPH_LINK:
33 return "link";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030034 case MEDIA_GRAPH_INTF_DEVNODE:
35 return "intf-devnode";
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030036 default:
37 return "unknown";
38 }
39}
40
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030041static inline const char *intf_type(struct media_interface *intf)
42{
43 switch (intf->type) {
44 case MEDIA_INTF_T_DVB_FE:
Shuah Khan66c1db12016-03-04 18:14:05 -030045 return "dvb-frontend";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030046 case MEDIA_INTF_T_DVB_DEMUX:
Shuah Khan66c1db12016-03-04 18:14:05 -030047 return "dvb-demux";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030048 case MEDIA_INTF_T_DVB_DVR:
Shuah Khan66c1db12016-03-04 18:14:05 -030049 return "dvb-dvr";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030050 case MEDIA_INTF_T_DVB_CA:
Shuah Khan66c1db12016-03-04 18:14:05 -030051 return "dvb-ca";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030052 case MEDIA_INTF_T_DVB_NET:
Shuah Khan66c1db12016-03-04 18:14:05 -030053 return "dvb-net";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030054 case MEDIA_INTF_T_V4L_VIDEO:
Shuah Khan66c1db12016-03-04 18:14:05 -030055 return "v4l-video";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030056 case MEDIA_INTF_T_V4L_VBI:
Shuah Khan66c1db12016-03-04 18:14:05 -030057 return "v4l-vbi";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030058 case MEDIA_INTF_T_V4L_RADIO:
Shuah Khan66c1db12016-03-04 18:14:05 -030059 return "v4l-radio";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030060 case MEDIA_INTF_T_V4L_SUBDEV:
Shuah Khan66c1db12016-03-04 18:14:05 -030061 return "v4l-subdev";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030062 case MEDIA_INTF_T_V4L_SWRADIO:
Shuah Khan66c1db12016-03-04 18:14:05 -030063 return "v4l-swradio";
Nick Dyerb2fe22d2016-07-18 18:10:30 -030064 case MEDIA_INTF_T_V4L_TOUCH:
65 return "v4l-touch";
Shuah Khan5af557a2016-02-11 21:41:18 -020066 case MEDIA_INTF_T_ALSA_PCM_CAPTURE:
Shuah Khan66c1db12016-03-04 18:14:05 -030067 return "alsa-pcm-capture";
Shuah Khan5af557a2016-02-11 21:41:18 -020068 case MEDIA_INTF_T_ALSA_PCM_PLAYBACK:
Shuah Khan66c1db12016-03-04 18:14:05 -030069 return "alsa-pcm-playback";
Shuah Khan5af557a2016-02-11 21:41:18 -020070 case MEDIA_INTF_T_ALSA_CONTROL:
71 return "alsa-control";
72 case MEDIA_INTF_T_ALSA_COMPRESS:
Shuah Khan66c1db12016-03-04 18:14:05 -030073 return "alsa-compress";
Shuah Khan5af557a2016-02-11 21:41:18 -020074 case MEDIA_INTF_T_ALSA_RAWMIDI:
Shuah Khan66c1db12016-03-04 18:14:05 -030075 return "alsa-rawmidi";
Shuah Khan5af557a2016-02-11 21:41:18 -020076 case MEDIA_INTF_T_ALSA_HWDEP:
Shuah Khan66c1db12016-03-04 18:14:05 -030077 return "alsa-hwdep";
Shuah Khan5af557a2016-02-11 21:41:18 -020078 case MEDIA_INTF_T_ALSA_SEQUENCER:
Shuah Khan66c1db12016-03-04 18:14:05 -030079 return "alsa-sequencer";
Shuah Khan5af557a2016-02-11 21:41:18 -020080 case MEDIA_INTF_T_ALSA_TIMER:
Shuah Khan66c1db12016-03-04 18:14:05 -030081 return "alsa-timer";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030082 default:
83 return "unknown-intf";
84 }
85};
86
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020087__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
88 int idx_max)
89{
Sakari Ailusf7b5dff2016-01-27 12:47:54 -020090 idx_max = ALIGN(idx_max, BITS_PER_LONG);
91 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
92 GFP_KERNEL);
Sakari Ailus030e89e2015-12-16 11:32:36 -020093 if (!ent_enum->bmap)
94 return -ENOMEM;
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020095
96 bitmap_zero(ent_enum->bmap, idx_max);
97 ent_enum->idx_max = idx_max;
98
99 return 0;
100}
101EXPORT_SYMBOL_GPL(__media_entity_enum_init);
102
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200103void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
104{
Sakari Ailus030e89e2015-12-16 11:32:36 -0200105 kfree(ent_enum->bmap);
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200106}
107EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
108
109/**
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200110 * dev_dbg_obj - Prints in debug mode a change on some object
111 *
112 * @event_name: Name of the event to report. Could be __func__
113 * @gobj: Pointer to the object
114 *
115 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
116 * won't produce any code.
117 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300118static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
119{
120#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
121 switch (media_type(gobj)) {
122 case MEDIA_GRAPH_ENTITY:
123 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200124 "%s id %u: entity '%s'\n",
125 event_name, media_id(gobj),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300126 gobj_to_entity(gobj)->name);
127 break;
128 case MEDIA_GRAPH_LINK:
129 {
130 struct media_link *link = gobj_to_link(gobj);
131
132 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200133 "%s id %u: %s link id %u ==> id %u\n",
134 event_name, media_id(gobj),
135 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
136 "data" : "interface",
137 media_id(link->gobj0),
138 media_id(link->gobj1));
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300139 break;
140 }
141 case MEDIA_GRAPH_PAD:
142 {
143 struct media_pad *pad = gobj_to_pad(gobj);
144
145 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200146 "%s id %u: %s%spad '%s':%d\n",
147 event_name, media_id(gobj),
148 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
Mauro Carvalho Chehab6c24d462015-08-21 18:26:42 -0300149 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300150 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300151 break;
152 }
153 case MEDIA_GRAPH_INTF_DEVNODE:
154 {
155 struct media_interface *intf = gobj_to_intf(gobj);
156 struct media_intf_devnode *devnode = intf_to_devnode(intf);
157
158 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200159 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
160 event_name, media_id(gobj),
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300161 intf_type(intf),
162 devnode->major, devnode->minor);
163 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300164 }
165 }
166#endif
167}
168
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200169void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300170 enum media_gobj_type type,
171 struct media_gobj *gobj)
172{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300173 BUG_ON(!mdev);
174
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300175 gobj->mdev = mdev;
176
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300177 /* Create a per-type unique object ID */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200178 gobj->id = media_gobj_gen_id(type, ++mdev->id);
179
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300180 switch (type) {
181 case MEDIA_GRAPH_ENTITY:
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300182 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300183 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300184 case MEDIA_GRAPH_PAD:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300185 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300186 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300187 case MEDIA_GRAPH_LINK:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300188 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300189 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300190 case MEDIA_GRAPH_INTF_DEVNODE:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300191 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300192 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300193 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300194
195 mdev->topology_version++;
196
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300197 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300198}
199
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200200void media_gobj_destroy(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300201{
Max Kellermann67537432016-08-09 23:32:57 +0200202 /* Do nothing if the object is not linked. */
203 if (gobj->mdev == NULL)
204 return;
205
Helen Fornazier8d1d3d02017-04-06 16:25:15 -0300206 dev_dbg_obj(__func__, gobj);
207
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);
Max Kellermann67537432016-08-09 23:32:57 +0200212
213 gobj->mdev = NULL;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300214}
215
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200216int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
217 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300218{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300219 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300220 unsigned int i;
221
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300222 entity->num_pads = num_pads;
223 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300224
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300225 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300226 mutex_lock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300227
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300228 for (i = 0; i < num_pads; i++) {
229 pads[i].entity = entity;
230 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300231 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200232 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300233 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300234 }
235
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300236 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300237 mutex_unlock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300238
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300239 return 0;
240}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200241EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300242
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300243/* -----------------------------------------------------------------------------
244 * Graph traversal
245 */
246
247static struct media_entity *
248media_entity_other(struct media_entity *entity, struct media_link *link)
249{
250 if (link->source->entity == entity)
251 return link->sink->entity;
252 else
253 return link->source->entity;
254}
255
256/* push an entity to traversal stack */
Sakari Ailus20b85222016-11-21 14:48:30 -0200257static void stack_push(struct media_graph *graph,
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300258 struct media_entity *entity)
259{
260 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
261 WARN_ON(1);
262 return;
263 }
264 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200265 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300266 graph->stack[graph->top].entity = entity;
267}
268
Sakari Ailus20b85222016-11-21 14:48:30 -0200269static struct media_entity *stack_pop(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300270{
271 struct media_entity *entity;
272
273 entity = graph->stack[graph->top].entity;
274 graph->top--;
275
276 return entity;
277}
278
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300279#define link_top(en) ((en)->stack[(en)->top].link)
280#define stack_top(en) ((en)->stack[(en)->top].entity)
281
Sakari Ailus0798ce42015-12-16 11:32:37 -0200282/*
283 * TODO: Get rid of this.
284 */
Mauro Carvalho Chehab430a6722015-12-16 15:18:25 -0200285#define MEDIA_ENTITY_MAX_PADS 512
Sakari Ailus0798ce42015-12-16 11:32:37 -0200286
Sakari Ailuse03d2202015-12-16 11:32:22 -0200287/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200288 * media_graph_walk_init - Allocate resources for graph walk
Sakari Ailuse03d2202015-12-16 11:32:22 -0200289 * @graph: Media graph structure that will be used to walk the graph
290 * @mdev: Media device
291 *
292 * Reserve resources for graph walk in media device's current
293 * state. The memory must be released using
Sakari Ailus20b85222016-11-21 14:48:30 -0200294 * media_graph_walk_free().
Sakari Ailuse03d2202015-12-16 11:32:22 -0200295 *
296 * Returns error on failure, zero on success.
297 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200298__must_check int media_graph_walk_init(
299 struct media_graph *graph, struct media_device *mdev)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200300{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200301 return media_entity_enum_init(&graph->ent_enum, mdev);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200302}
Sakari Ailus20b85222016-11-21 14:48:30 -0200303EXPORT_SYMBOL_GPL(media_graph_walk_init);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200304
305/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200306 * media_graph_walk_cleanup - Release resources related to graph walking
Sakari Ailuse03d2202015-12-16 11:32:22 -0200307 * @graph: Media graph structure that was used to walk the graph
308 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200309void media_graph_walk_cleanup(struct media_graph *graph)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200310{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200311 media_entity_enum_cleanup(&graph->ent_enum);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200312}
Sakari Ailus20b85222016-11-21 14:48:30 -0200313EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200314
Sakari Ailus20b85222016-11-21 14:48:30 -0200315void media_graph_walk_start(struct media_graph *graph,
316 struct media_entity *entity)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300317{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200318 media_entity_enum_zero(&graph->ent_enum);
319 media_entity_enum_set(&graph->ent_enum, entity);
320
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300321 graph->top = 0;
322 graph->stack[graph->top].entity = NULL;
323 stack_push(graph, entity);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300324 dev_dbg(entity->graph_obj.mdev->dev,
325 "begin graph walk at '%s'\n", entity->name);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300326}
Sakari Ailus20b85222016-11-21 14:48:30 -0200327EXPORT_SYMBOL_GPL(media_graph_walk_start);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300328
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300329static void media_graph_walk_iter(struct media_graph *graph)
330{
331 struct media_entity *entity = stack_top(graph);
332 struct media_link *link;
333 struct media_entity *next;
334
335 link = list_entry(link_top(graph), typeof(*link), list);
336
337 /* The link is not enabled so we do not follow. */
338 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
339 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300340 dev_dbg(entity->graph_obj.mdev->dev,
341 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
342 link->source->entity->name, link->source->index,
343 link->sink->entity->name, link->sink->index);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300344 return;
345 }
346
347 /* Get the entity in the other end of the link . */
348 next = media_entity_other(entity, link);
349
350 /* Has the entity already been visited? */
351 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
352 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300353 dev_dbg(entity->graph_obj.mdev->dev,
354 "walk: skipping entity '%s' (already seen)\n",
355 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300356 return;
357 }
358
359 /* Push the new entity to stack and start over. */
360 link_top(graph) = link_top(graph)->next;
361 stack_push(graph, next);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300362 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
363 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300364}
365
Sakari Ailus20b85222016-11-21 14:48:30 -0200366struct media_entity *media_graph_walk_next(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300367{
Sakari Ailusaa79a842016-07-20 12:39:02 -0300368 struct media_entity *entity;
369
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300370 if (stack_top(graph) == NULL)
371 return NULL;
372
373 /*
374 * Depth first search. Push entity to stack and continue from
375 * top of the stack until no more entities on the level can be
376 * found.
377 */
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300378 while (link_top(graph) != &stack_top(graph)->links)
379 media_graph_walk_iter(graph);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300380
Sakari Ailusaa79a842016-07-20 12:39:02 -0300381 entity = stack_pop(graph);
382 dev_dbg(entity->graph_obj.mdev->dev,
383 "walk: returning entity '%s'\n", entity->name);
384
385 return entity;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300386}
Sakari Ailus20b85222016-11-21 14:48:30 -0200387EXPORT_SYMBOL_GPL(media_graph_walk_next);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300388
389/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300390 * Pipeline management
391 */
392
Sakari Ailus20b85222016-11-21 14:48:30 -0200393__must_check int __media_pipeline_start(struct media_entity *entity,
394 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300395{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300396 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus20b85222016-11-21 14:48:30 -0200397 struct media_graph *graph = &pipe->graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300398 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300399 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300400 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300401
Sakari Ailus74a41332015-12-16 11:32:29 -0200402 if (!pipe->streaming_count++) {
Sakari Ailus20b85222016-11-21 14:48:30 -0200403 ret = media_graph_walk_init(&pipe->graph, mdev);
Sakari Ailus74a41332015-12-16 11:32:29 -0200404 if (ret)
405 goto error_graph_walk_start;
Sakari Ailus106b9902015-12-16 15:32:23 +0200406 }
407
Sakari Ailus20b85222016-11-21 14:48:30 -0200408 media_graph_walk_start(&pipe->graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300409
Sakari Ailus20b85222016-11-21 14:48:30 -0200410 while ((entity = media_graph_walk_next(graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300411 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
412 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300413
Laurent Pincharte02188c2010-08-25 09:00:41 -0300414 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200415
416 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
417 ret = -EBUSY;
418 goto error;
419 }
420
Laurent Pincharte02188c2010-08-25 09:00:41 -0300421 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300422
423 /* Already streaming --- no need to check. */
424 if (entity->stream_count > 1)
425 continue;
426
427 if (!entity->ops || !entity->ops->link_validate)
428 continue;
429
Sakari Ailusde49c282013-10-13 08:00:26 -0300430 bitmap_zero(active, entity->num_pads);
431 bitmap_fill(has_no_links, entity->num_pads);
432
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300433 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300434 struct media_pad *pad = link->sink->entity == entity
435 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300436
Sakari Ailusde49c282013-10-13 08:00:26 -0300437 /* Mark that a pad is connected by a link. */
438 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300439
Sakari Ailusde49c282013-10-13 08:00:26 -0300440 /*
441 * Pads that either do not need to connect or
442 * are connected through an enabled link are
443 * fine.
444 */
445 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
446 link->flags & MEDIA_LNK_FL_ENABLED)
447 bitmap_set(active, pad->index, 1);
448
449 /*
450 * Link validation will only take place for
451 * sink ends of the link that are enabled.
452 */
453 if (link->sink != pad ||
454 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300455 continue;
456
457 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300458 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300459 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200460 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200461 link->source->entity->name,
462 link->source->index,
463 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300464 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300465 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300466 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300467
468 /* Either no links or validated links are fine. */
469 bitmap_or(active, active, has_no_links, entity->num_pads);
470
471 if (!bitmap_full(active, entity->num_pads)) {
Helen Mae Koike Fornazier47dfdb32015-06-28 16:14:10 -0300472 ret = -ENOLINK;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300473 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200474 "'%s':%u must be connected by an enabled link\n",
Sakari Ailusfab9d302014-10-28 20:35:04 -0300475 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300476 (unsigned)find_first_zero_bit(
477 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300478 goto error;
479 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300480 }
481
Sakari Ailusaf88be32012-01-11 06:25:15 -0300482 return 0;
483
484error:
485 /*
486 * Link validation on graph failed. We revert what we did and
487 * return the error.
488 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200489 media_graph_walk_start(graph, entity_err);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300490
Sakari Ailus20b85222016-11-21 14:48:30 -0200491 while ((entity_err = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300492 /* Sanity check for negative stream_count */
493 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200494 entity_err->stream_count--;
495 if (entity_err->stream_count == 0)
496 entity_err->pipe = NULL;
497 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300498
499 /*
500 * We haven't increased stream_count further than this
501 * so we quit here.
502 */
503 if (entity_err == entity)
504 break;
505 }
506
Sakari Ailus74a41332015-12-16 11:32:29 -0200507error_graph_walk_start:
508 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200509 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200510
Shuah Khanfb49f202016-02-11 21:41:24 -0200511 return ret;
512}
Sakari Ailus20b85222016-11-21 14:48:30 -0200513EXPORT_SYMBOL_GPL(__media_pipeline_start);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300514
Sakari Ailus20b85222016-11-21 14:48:30 -0200515__must_check int media_pipeline_start(struct media_entity *entity,
516 struct media_pipeline *pipe)
Shuah Khanfb49f202016-02-11 21:41:24 -0200517{
518 struct media_device *mdev = entity->graph_obj.mdev;
519 int ret;
520
521 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200522 ret = __media_pipeline_start(entity, pipe);
Shuah Khanfb49f202016-02-11 21:41:24 -0200523 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300524 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300525}
Sakari Ailus20b85222016-11-21 14:48:30 -0200526EXPORT_SYMBOL_GPL(media_pipeline_start);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300527
Sakari Ailus20b85222016-11-21 14:48:30 -0200528void __media_pipeline_stop(struct media_entity *entity)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300529{
Sakari Ailus20b85222016-11-21 14:48:30 -0200530 struct media_graph *graph = &entity->pipe->graph;
Sakari Ailus74a41332015-12-16 11:32:29 -0200531 struct media_pipeline *pipe = entity->pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300532
Laurent Pincharte02188c2010-08-25 09:00:41 -0300533
Sakari Ailus74a41332015-12-16 11:32:29 -0200534 WARN_ON(!pipe->streaming_count);
Sakari Ailus20b85222016-11-21 14:48:30 -0200535 media_graph_walk_start(graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300536
Sakari Ailus20b85222016-11-21 14:48:30 -0200537 while ((entity = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300538 /* Sanity check for negative stream_count */
539 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200540 entity->stream_count--;
541 if (entity->stream_count == 0)
542 entity->pipe = NULL;
543 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300544 }
545
Sakari Ailus74a41332015-12-16 11:32:29 -0200546 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200547 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200548
Shuah Khanfb49f202016-02-11 21:41:24 -0200549}
Sakari Ailus20b85222016-11-21 14:48:30 -0200550EXPORT_SYMBOL_GPL(__media_pipeline_stop);
Shuah Khanfb49f202016-02-11 21:41:24 -0200551
Sakari Ailus20b85222016-11-21 14:48:30 -0200552void media_pipeline_stop(struct media_entity *entity)
Shuah Khanfb49f202016-02-11 21:41:24 -0200553{
554 struct media_device *mdev = entity->graph_obj.mdev;
555
556 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200557 __media_pipeline_stop(entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300558 mutex_unlock(&mdev->graph_mutex);
559}
Sakari Ailus20b85222016-11-21 14:48:30 -0200560EXPORT_SYMBOL_GPL(media_pipeline_stop);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300561
562/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300563 * Module use count
564 */
565
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300566struct media_entity *media_entity_get(struct media_entity *entity)
567{
568 if (entity == NULL)
569 return NULL;
570
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300571 if (entity->graph_obj.mdev->dev &&
572 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300573 return NULL;
574
575 return entity;
576}
577EXPORT_SYMBOL_GPL(media_entity_get);
578
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300579void media_entity_put(struct media_entity *entity)
580{
581 if (entity == NULL)
582 return;
583
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300584 if (entity->graph_obj.mdev->dev)
585 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300586}
587EXPORT_SYMBOL_GPL(media_entity_put);
588
589/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300590 * Links management
591 */
592
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300593static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300594{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300595 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300596
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300597 link = kzalloc(sizeof(*link), GFP_KERNEL);
598 if (link == NULL)
599 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300600
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300601 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300602
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300603 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300604}
605
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300606static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200607 struct media_link *link)
608{
609 struct media_link *rlink, *tmp;
610 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200611
612 if (link->source->entity == entity)
613 remote = link->sink->entity;
614 else
615 remote = link->source->entity;
616
617 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200618 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200619 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200620
621 if (link->source->entity == entity)
622 remote->num_backlinks--;
623
624 /* Remove the remote link */
625 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200626 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200627 kfree(rlink);
628
629 if (--remote->num_links == 0)
630 break;
631 }
632 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200633 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200634 kfree(link);
635}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300636
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300637int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300638media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300639 struct media_entity *sink, u16 sink_pad, u32 flags)
640{
641 struct media_link *link;
642 struct media_link *backlink;
643
644 BUG_ON(source == NULL || sink == NULL);
645 BUG_ON(source_pad >= source->num_pads);
646 BUG_ON(sink_pad >= sink->num_pads);
647
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300648 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300649 if (link == NULL)
650 return -ENOMEM;
651
652 link->source = &source->pads[source_pad];
653 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200654 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300655
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300656 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200657 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300658 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300659
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300660 /* Create the backlink. Backlinks are used to help graph traversal and
661 * are not reported to userspace.
662 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300663 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300664 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300665 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300666 return -ENOMEM;
667 }
668
669 backlink->source = &source->pads[source_pad];
670 backlink->sink = &sink->pads[sink_pad];
671 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300672 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300673
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300674 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200675 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300676 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300677
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300678 link->reverse = backlink;
679 backlink->reverse = link;
680
681 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300682 sink->num_links++;
683 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300684
685 return 0;
686}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300687EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300688
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200689int media_create_pad_links(const struct media_device *mdev,
690 const u32 source_function,
691 struct media_entity *source,
692 const u16 source_pad,
693 const u32 sink_function,
694 struct media_entity *sink,
695 const u16 sink_pad,
696 u32 flags,
697 const bool allow_both_undefined)
698{
699 struct media_entity *entity;
700 unsigned function;
701 int ret;
702
703 /* Trivial case: 1:1 relation */
704 if (source && sink)
705 return media_create_pad_link(source, source_pad,
706 sink, sink_pad, flags);
707
708 /* Worse case scenario: n:n relation */
709 if (!source && !sink) {
710 if (!allow_both_undefined)
711 return 0;
712 media_device_for_each_entity(source, mdev) {
713 if (source->function != source_function)
714 continue;
715 media_device_for_each_entity(sink, mdev) {
716 if (sink->function != sink_function)
717 continue;
718 ret = media_create_pad_link(source, source_pad,
719 sink, sink_pad,
720 flags);
721 if (ret)
722 return ret;
723 flags &= ~(MEDIA_LNK_FL_ENABLED |
724 MEDIA_LNK_FL_IMMUTABLE);
725 }
726 }
727 return 0;
728 }
729
730 /* Handle 1:n and n:1 cases */
731 if (source)
732 function = sink_function;
733 else
734 function = source_function;
735
736 media_device_for_each_entity(entity, mdev) {
737 if (entity->function != function)
738 continue;
739
740 if (source)
741 ret = media_create_pad_link(source, source_pad,
742 entity, sink_pad, flags);
743 else
744 ret = media_create_pad_link(entity, source_pad,
745 sink, sink_pad, flags);
746 if (ret)
747 return ret;
748 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
749 }
750 return 0;
751}
752EXPORT_SYMBOL_GPL(media_create_pad_links);
753
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300754void __media_entity_remove_links(struct media_entity *entity)
755{
756 struct media_link *link, *tmp;
757
758 list_for_each_entry_safe(link, tmp, &entity->links, list)
759 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300760
761 entity->num_links = 0;
762 entity->num_backlinks = 0;
763}
764EXPORT_SYMBOL_GPL(__media_entity_remove_links);
765
766void media_entity_remove_links(struct media_entity *entity)
767{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200768 struct media_device *mdev = entity->graph_obj.mdev;
769
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300770 /* Do nothing if the entity is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200771 if (mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300772 return;
773
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300774 mutex_lock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300775 __media_entity_remove_links(entity);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300776 mutex_unlock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300777}
778EXPORT_SYMBOL_GPL(media_entity_remove_links);
779
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300780static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
781{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300782 int ret;
783
784 /* Notify both entities. */
785 ret = media_entity_call(link->source->entity, link_setup,
786 link->source, link->sink, flags);
787 if (ret < 0 && ret != -ENOIOCTLCMD)
788 return ret;
789
790 ret = media_entity_call(link->sink->entity, link_setup,
791 link->sink, link->source, flags);
792 if (ret < 0 && ret != -ENOIOCTLCMD) {
793 media_entity_call(link->source->entity, link_setup,
794 link->source, link->sink, link->flags);
795 return ret;
796 }
797
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300798 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300799 link->reverse->flags = link->flags;
800
801 return 0;
802}
803
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300804int __media_entity_setup_link(struct media_link *link, u32 flags)
805{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300806 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300807 struct media_device *mdev;
808 struct media_entity *source, *sink;
809 int ret = -EBUSY;
810
811 if (link == NULL)
812 return -EINVAL;
813
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300814 /* The non-modifiable link flags must not be modified. */
815 if ((link->flags & ~mask) != (flags & ~mask))
816 return -EINVAL;
817
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300818 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
819 return link->flags == flags ? 0 : -EINVAL;
820
821 if (link->flags == flags)
822 return 0;
823
824 source = link->source->entity;
825 sink = link->sink->entity;
826
Laurent Pincharte02188c2010-08-25 09:00:41 -0300827 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
828 (source->stream_count || sink->stream_count))
829 return -EBUSY;
830
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300831 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300832
Laurent Pinchart68429f52015-11-03 00:27:51 -0200833 if (mdev->ops && mdev->ops->link_notify) {
834 ret = mdev->ops->link_notify(link, flags,
835 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300836 if (ret < 0)
837 return ret;
838 }
839
840 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300841
Laurent Pinchart68429f52015-11-03 00:27:51 -0200842 if (mdev->ops && mdev->ops->link_notify)
843 mdev->ops->link_notify(link, flags,
844 MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300845
846 return ret;
847}
Shuah Khanefc70272016-02-11 21:41:23 -0200848EXPORT_SYMBOL_GPL(__media_entity_setup_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300849
850int media_entity_setup_link(struct media_link *link, u32 flags)
851{
852 int ret;
853
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200854 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300855 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200856 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300857
858 return ret;
859}
860EXPORT_SYMBOL_GPL(media_entity_setup_link);
861
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300862struct media_link *
863media_entity_find_link(struct media_pad *source, struct media_pad *sink)
864{
865 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300866
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300867 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300868 if (link->source->entity == source->entity &&
869 link->source->index == source->index &&
870 link->sink->entity == sink->entity &&
871 link->sink->index == sink->index)
872 return link;
873 }
874
875 return NULL;
876}
877EXPORT_SYMBOL_GPL(media_entity_find_link);
878
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300879struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300880{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300881 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300882
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300883 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300884 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
885 continue;
886
887 if (link->source == pad)
888 return link->sink;
889
890 if (link->sink == pad)
891 return link->source;
892 }
893
894 return NULL;
895
896}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300897EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300898
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300899static void media_interface_init(struct media_device *mdev,
900 struct media_interface *intf,
901 u32 gobj_type,
902 u32 intf_type, u32 flags)
903{
904 intf->type = intf_type;
905 intf->flags = flags;
906 INIT_LIST_HEAD(&intf->links);
907
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200908 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300909}
910
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300911/* Functions related to the media interface via device nodes */
912
913struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
914 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300915 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300916{
917 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300918
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300919 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300920 if (!devnode)
921 return NULL;
922
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300923 devnode->major = major;
924 devnode->minor = minor;
925
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300926 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
927 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300928
929 return devnode;
930}
931EXPORT_SYMBOL_GPL(media_devnode_create);
932
933void media_devnode_remove(struct media_intf_devnode *devnode)
934{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300935 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200936 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300937 kfree(devnode);
938}
939EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300940
941struct media_link *media_create_intf_link(struct media_entity *entity,
942 struct media_interface *intf,
943 u32 flags)
944{
945 struct media_link *link;
946
947 link = media_add_link(&intf->links);
948 if (link == NULL)
949 return NULL;
950
951 link->intf = intf;
952 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200953 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300954
955 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200956 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300957 &link->graph_obj);
958
959 return link;
960}
961EXPORT_SYMBOL_GPL(media_create_intf_link);
962
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300963void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300964{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300965 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200966 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300967 kfree(link);
968}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300969EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300970
971void media_remove_intf_link(struct media_link *link)
972{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200973 struct media_device *mdev = link->graph_obj.mdev;
974
975 /* Do nothing if the intf is not registered. */
976 if (mdev == NULL)
977 return;
978
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300979 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300980 __media_remove_intf_link(link);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300981 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300982}
983EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300984
985void __media_remove_intf_links(struct media_interface *intf)
986{
987 struct media_link *link, *tmp;
988
989 list_for_each_entry_safe(link, tmp, &intf->links, list)
990 __media_remove_intf_link(link);
991
992}
993EXPORT_SYMBOL_GPL(__media_remove_intf_links);
994
995void media_remove_intf_links(struct media_interface *intf)
996{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200997 struct media_device *mdev = intf->graph_obj.mdev;
998
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300999 /* Do nothing if the intf is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001000 if (mdev == NULL)
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001001 return;
1002
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001003 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001004 __media_remove_intf_links(intf);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001005 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001006}
1007EXPORT_SYMBOL_GPL(media_remove_intf_links);