blob: dd0f0ead951644505b361100d36911fce881733c [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
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200217int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
218 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300219{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300220 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300221 unsigned int i;
222
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300223 entity->num_pads = num_pads;
224 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300225
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300226 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300227 mutex_lock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300228
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300229 for (i = 0; i < num_pads; i++) {
230 pads[i].entity = entity;
231 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300232 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200233 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300234 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300235 }
236
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300237 if (mdev)
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300238 mutex_unlock(&mdev->graph_mutex);
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300239
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300240 return 0;
241}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200242EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300243
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300244/* -----------------------------------------------------------------------------
245 * Graph traversal
246 */
247
248static struct media_entity *
249media_entity_other(struct media_entity *entity, struct media_link *link)
250{
251 if (link->source->entity == entity)
252 return link->sink->entity;
253 else
254 return link->source->entity;
255}
256
257/* push an entity to traversal stack */
Sakari Ailus20b85222016-11-21 14:48:30 -0200258static void stack_push(struct media_graph *graph,
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300259 struct media_entity *entity)
260{
261 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
262 WARN_ON(1);
263 return;
264 }
265 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200266 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300267 graph->stack[graph->top].entity = entity;
268}
269
Sakari Ailus20b85222016-11-21 14:48:30 -0200270static struct media_entity *stack_pop(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300271{
272 struct media_entity *entity;
273
274 entity = graph->stack[graph->top].entity;
275 graph->top--;
276
277 return entity;
278}
279
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300280#define link_top(en) ((en)->stack[(en)->top].link)
281#define stack_top(en) ((en)->stack[(en)->top].entity)
282
Sakari Ailus0798ce42015-12-16 11:32:37 -0200283/*
284 * TODO: Get rid of this.
285 */
Mauro Carvalho Chehab430a6722015-12-16 15:18:25 -0200286#define MEDIA_ENTITY_MAX_PADS 512
Sakari Ailus0798ce42015-12-16 11:32:37 -0200287
Sakari Ailuse03d2202015-12-16 11:32:22 -0200288/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200289 * media_graph_walk_init - Allocate resources for graph walk
Sakari Ailuse03d2202015-12-16 11:32:22 -0200290 * @graph: Media graph structure that will be used to walk the graph
291 * @mdev: Media device
292 *
293 * Reserve resources for graph walk in media device's current
294 * state. The memory must be released using
Sakari Ailus20b85222016-11-21 14:48:30 -0200295 * media_graph_walk_free().
Sakari Ailuse03d2202015-12-16 11:32:22 -0200296 *
297 * Returns error on failure, zero on success.
298 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200299__must_check int media_graph_walk_init(
300 struct media_graph *graph, struct media_device *mdev)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200301{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200302 return media_entity_enum_init(&graph->ent_enum, mdev);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200303}
Sakari Ailus20b85222016-11-21 14:48:30 -0200304EXPORT_SYMBOL_GPL(media_graph_walk_init);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200305
306/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200307 * media_graph_walk_cleanup - Release resources related to graph walking
Sakari Ailuse03d2202015-12-16 11:32:22 -0200308 * @graph: Media graph structure that was used to walk the graph
309 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200310void media_graph_walk_cleanup(struct media_graph *graph)
Sakari Ailuse03d2202015-12-16 11:32:22 -0200311{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200312 media_entity_enum_cleanup(&graph->ent_enum);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200313}
Sakari Ailus20b85222016-11-21 14:48:30 -0200314EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200315
Sakari Ailus20b85222016-11-21 14:48:30 -0200316void media_graph_walk_start(struct media_graph *graph,
317 struct media_entity *entity)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300318{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200319 media_entity_enum_zero(&graph->ent_enum);
320 media_entity_enum_set(&graph->ent_enum, entity);
321
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300322 graph->top = 0;
323 graph->stack[graph->top].entity = NULL;
324 stack_push(graph, entity);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300325 dev_dbg(entity->graph_obj.mdev->dev,
326 "begin graph walk at '%s'\n", entity->name);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300327}
Sakari Ailus20b85222016-11-21 14:48:30 -0200328EXPORT_SYMBOL_GPL(media_graph_walk_start);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300329
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300330static void media_graph_walk_iter(struct media_graph *graph)
331{
332 struct media_entity *entity = stack_top(graph);
333 struct media_link *link;
334 struct media_entity *next;
335
336 link = list_entry(link_top(graph), typeof(*link), list);
337
338 /* The link is not enabled so we do not follow. */
339 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
340 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300341 dev_dbg(entity->graph_obj.mdev->dev,
342 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
343 link->source->entity->name, link->source->index,
344 link->sink->entity->name, link->sink->index);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300345 return;
346 }
347
348 /* Get the entity in the other end of the link . */
349 next = media_entity_other(entity, link);
350
351 /* Has the entity already been visited? */
352 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
353 link_top(graph) = link_top(graph)->next;
Sakari Ailusaa79a842016-07-20 12:39:02 -0300354 dev_dbg(entity->graph_obj.mdev->dev,
355 "walk: skipping entity '%s' (already seen)\n",
356 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300357 return;
358 }
359
360 /* Push the new entity to stack and start over. */
361 link_top(graph) = link_top(graph)->next;
362 stack_push(graph, next);
Sakari Ailusaa79a842016-07-20 12:39:02 -0300363 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
364 next->name);
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300365}
366
Sakari Ailus20b85222016-11-21 14:48:30 -0200367struct media_entity *media_graph_walk_next(struct media_graph *graph)
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300368{
Sakari Ailusaa79a842016-07-20 12:39:02 -0300369 struct media_entity *entity;
370
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300371 if (stack_top(graph) == NULL)
372 return NULL;
373
374 /*
375 * Depth first search. Push entity to stack and continue from
376 * top of the stack until no more entities on the level can be
377 * found.
378 */
Sakari Ailus5b1f8322016-07-20 12:31:39 -0300379 while (link_top(graph) != &stack_top(graph)->links)
380 media_graph_walk_iter(graph);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300381
Sakari Ailusaa79a842016-07-20 12:39:02 -0300382 entity = stack_pop(graph);
383 dev_dbg(entity->graph_obj.mdev->dev,
384 "walk: returning entity '%s'\n", entity->name);
385
386 return entity;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300387}
Sakari Ailus20b85222016-11-21 14:48:30 -0200388EXPORT_SYMBOL_GPL(media_graph_walk_next);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300389
Niklas Söderlundd295c6a2017-06-15 06:17:26 -0300390int media_entity_get_fwnode_pad(struct media_entity *entity,
391 struct fwnode_handle *fwnode,
392 unsigned long direction_flags)
393{
394 struct fwnode_endpoint endpoint;
395 unsigned int i;
396 int ret;
397
398 if (!entity->ops || !entity->ops->get_fwnode_pad) {
399 for (i = 0; i < entity->num_pads; i++) {
400 if (entity->pads[i].flags & direction_flags)
401 return i;
402 }
403
404 return -ENXIO;
405 }
406
407 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
408 if (ret)
409 return ret;
410
411 ret = entity->ops->get_fwnode_pad(&endpoint);
412 if (ret < 0)
413 return ret;
414
415 if (ret >= entity->num_pads)
416 return -ENXIO;
417
418 if (!(entity->pads[ret].flags & direction_flags))
419 return -ENXIO;
420
421 return ret;
422}
423EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
424
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300425/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300426 * Pipeline management
427 */
428
Sakari Ailus20b85222016-11-21 14:48:30 -0200429__must_check int __media_pipeline_start(struct media_entity *entity,
430 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300431{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300432 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus20b85222016-11-21 14:48:30 -0200433 struct media_graph *graph = &pipe->graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300434 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300435 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300436 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300437
Sakari Ailus74a41332015-12-16 11:32:29 -0200438 if (!pipe->streaming_count++) {
Sakari Ailus20b85222016-11-21 14:48:30 -0200439 ret = media_graph_walk_init(&pipe->graph, mdev);
Sakari Ailus74a41332015-12-16 11:32:29 -0200440 if (ret)
441 goto error_graph_walk_start;
Sakari Ailus106b9902015-12-16 15:32:23 +0200442 }
443
Sakari Ailus20b85222016-11-21 14:48:30 -0200444 media_graph_walk_start(&pipe->graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300445
Sakari Ailus20b85222016-11-21 14:48:30 -0200446 while ((entity = media_graph_walk_next(graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300447 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
448 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300449
Laurent Pincharte02188c2010-08-25 09:00:41 -0300450 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200451
452 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
453 ret = -EBUSY;
454 goto error;
455 }
456
Laurent Pincharte02188c2010-08-25 09:00:41 -0300457 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300458
459 /* Already streaming --- no need to check. */
460 if (entity->stream_count > 1)
461 continue;
462
463 if (!entity->ops || !entity->ops->link_validate)
464 continue;
465
Sakari Ailusde49c282013-10-13 08:00:26 -0300466 bitmap_zero(active, entity->num_pads);
467 bitmap_fill(has_no_links, entity->num_pads);
468
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300469 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300470 struct media_pad *pad = link->sink->entity == entity
471 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300472
Sakari Ailusde49c282013-10-13 08:00:26 -0300473 /* Mark that a pad is connected by a link. */
474 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300475
Sakari Ailusde49c282013-10-13 08:00:26 -0300476 /*
477 * Pads that either do not need to connect or
478 * are connected through an enabled link are
479 * fine.
480 */
481 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
482 link->flags & MEDIA_LNK_FL_ENABLED)
483 bitmap_set(active, pad->index, 1);
484
485 /*
486 * Link validation will only take place for
487 * sink ends of the link that are enabled.
488 */
489 if (link->sink != pad ||
490 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300491 continue;
492
493 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300494 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300495 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200496 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200497 link->source->entity->name,
498 link->source->index,
499 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300500 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300501 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300502 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300503
504 /* Either no links or validated links are fine. */
505 bitmap_or(active, active, has_no_links, entity->num_pads);
506
507 if (!bitmap_full(active, entity->num_pads)) {
Helen Mae Koike Fornazier47dfdb32015-06-28 16:14:10 -0300508 ret = -ENOLINK;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300509 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailus91b619a2016-12-08 13:22:29 -0200510 "'%s':%u must be connected by an enabled link\n",
Sakari Ailusfab9d302014-10-28 20:35:04 -0300511 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300512 (unsigned)find_first_zero_bit(
513 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300514 goto error;
515 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300516 }
517
Sakari Ailusaf88be32012-01-11 06:25:15 -0300518 return 0;
519
520error:
521 /*
522 * Link validation on graph failed. We revert what we did and
523 * return the error.
524 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200525 media_graph_walk_start(graph, entity_err);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300526
Sakari Ailus20b85222016-11-21 14:48:30 -0200527 while ((entity_err = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300528 /* Sanity check for negative stream_count */
529 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200530 entity_err->stream_count--;
531 if (entity_err->stream_count == 0)
532 entity_err->pipe = NULL;
533 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300534
535 /*
536 * We haven't increased stream_count further than this
537 * so we quit here.
538 */
539 if (entity_err == entity)
540 break;
541 }
542
Sakari Ailus74a41332015-12-16 11:32:29 -0200543error_graph_walk_start:
544 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200545 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200546
Shuah Khanfb49f202016-02-11 21:41:24 -0200547 return ret;
548}
Sakari Ailus20b85222016-11-21 14:48:30 -0200549EXPORT_SYMBOL_GPL(__media_pipeline_start);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300550
Sakari Ailus20b85222016-11-21 14:48:30 -0200551__must_check int media_pipeline_start(struct media_entity *entity,
552 struct media_pipeline *pipe)
Shuah Khanfb49f202016-02-11 21:41:24 -0200553{
554 struct media_device *mdev = entity->graph_obj.mdev;
555 int ret;
556
557 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200558 ret = __media_pipeline_start(entity, pipe);
Shuah Khanfb49f202016-02-11 21:41:24 -0200559 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300560 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300561}
Sakari Ailus20b85222016-11-21 14:48:30 -0200562EXPORT_SYMBOL_GPL(media_pipeline_start);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300563
Sakari Ailus20b85222016-11-21 14:48:30 -0200564void __media_pipeline_stop(struct media_entity *entity)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300565{
Sakari Ailus20b85222016-11-21 14:48:30 -0200566 struct media_graph *graph = &entity->pipe->graph;
Sakari Ailus74a41332015-12-16 11:32:29 -0200567 struct media_pipeline *pipe = entity->pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300568
Kieran Bingham2a2599c2017-01-03 11:12:11 -0200569 /*
570 * If the following check fails, the driver has performed an
571 * unbalanced call to media_pipeline_stop()
572 */
573 if (WARN_ON(!pipe))
574 return;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300575
Sakari Ailus20b85222016-11-21 14:48:30 -0200576 media_graph_walk_start(graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300577
Sakari Ailus20b85222016-11-21 14:48:30 -0200578 while ((entity = media_graph_walk_next(graph))) {
Sakari Ailus12030f42016-07-20 09:19:03 -0300579 /* Sanity check for negative stream_count */
580 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200581 entity->stream_count--;
582 if (entity->stream_count == 0)
583 entity->pipe = NULL;
584 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300585 }
586
Sakari Ailus74a41332015-12-16 11:32:29 -0200587 if (!--pipe->streaming_count)
Sakari Ailus20b85222016-11-21 14:48:30 -0200588 media_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200589
Shuah Khanfb49f202016-02-11 21:41:24 -0200590}
Sakari Ailus20b85222016-11-21 14:48:30 -0200591EXPORT_SYMBOL_GPL(__media_pipeline_stop);
Shuah Khanfb49f202016-02-11 21:41:24 -0200592
Sakari Ailus20b85222016-11-21 14:48:30 -0200593void media_pipeline_stop(struct media_entity *entity)
Shuah Khanfb49f202016-02-11 21:41:24 -0200594{
595 struct media_device *mdev = entity->graph_obj.mdev;
596
597 mutex_lock(&mdev->graph_mutex);
Sakari Ailus20b85222016-11-21 14:48:30 -0200598 __media_pipeline_stop(entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300599 mutex_unlock(&mdev->graph_mutex);
600}
Sakari Ailus20b85222016-11-21 14:48:30 -0200601EXPORT_SYMBOL_GPL(media_pipeline_stop);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300602
603/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300604 * Module use count
605 */
606
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300607struct media_entity *media_entity_get(struct media_entity *entity)
608{
609 if (entity == NULL)
610 return NULL;
611
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300612 if (entity->graph_obj.mdev->dev &&
613 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300614 return NULL;
615
616 return entity;
617}
618EXPORT_SYMBOL_GPL(media_entity_get);
619
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300620void media_entity_put(struct media_entity *entity)
621{
622 if (entity == NULL)
623 return;
624
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300625 if (entity->graph_obj.mdev->dev)
626 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300627}
628EXPORT_SYMBOL_GPL(media_entity_put);
629
630/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300631 * Links management
632 */
633
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300634static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300635{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300636 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300637
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300638 link = kzalloc(sizeof(*link), GFP_KERNEL);
639 if (link == NULL)
640 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300641
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300642 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300643
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300644 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300645}
646
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300647static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200648 struct media_link *link)
649{
650 struct media_link *rlink, *tmp;
651 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200652
653 if (link->source->entity == entity)
654 remote = link->sink->entity;
655 else
656 remote = link->source->entity;
657
658 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200659 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200660 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200661
662 if (link->source->entity == entity)
663 remote->num_backlinks--;
664
665 /* Remove the remote link */
666 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200667 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200668 kfree(rlink);
669
670 if (--remote->num_links == 0)
671 break;
672 }
673 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200674 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200675 kfree(link);
676}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300677
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300678int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300679media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300680 struct media_entity *sink, u16 sink_pad, u32 flags)
681{
682 struct media_link *link;
683 struct media_link *backlink;
684
685 BUG_ON(source == NULL || sink == NULL);
686 BUG_ON(source_pad >= source->num_pads);
687 BUG_ON(sink_pad >= sink->num_pads);
688
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300689 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300690 if (link == NULL)
691 return -ENOMEM;
692
693 link->source = &source->pads[source_pad];
694 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200695 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300696
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300697 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200698 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300699 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300700
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300701 /* Create the backlink. Backlinks are used to help graph traversal and
702 * are not reported to userspace.
703 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300704 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300705 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300706 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300707 return -ENOMEM;
708 }
709
710 backlink->source = &source->pads[source_pad];
711 backlink->sink = &sink->pads[sink_pad];
712 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300713 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300714
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300715 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200716 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300717 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300718
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300719 link->reverse = backlink;
720 backlink->reverse = link;
721
722 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300723 sink->num_links++;
724 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300725
726 return 0;
727}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300728EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300729
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200730int media_create_pad_links(const struct media_device *mdev,
731 const u32 source_function,
732 struct media_entity *source,
733 const u16 source_pad,
734 const u32 sink_function,
735 struct media_entity *sink,
736 const u16 sink_pad,
737 u32 flags,
738 const bool allow_both_undefined)
739{
740 struct media_entity *entity;
741 unsigned function;
742 int ret;
743
744 /* Trivial case: 1:1 relation */
745 if (source && sink)
746 return media_create_pad_link(source, source_pad,
747 sink, sink_pad, flags);
748
749 /* Worse case scenario: n:n relation */
750 if (!source && !sink) {
751 if (!allow_both_undefined)
752 return 0;
753 media_device_for_each_entity(source, mdev) {
754 if (source->function != source_function)
755 continue;
756 media_device_for_each_entity(sink, mdev) {
757 if (sink->function != sink_function)
758 continue;
759 ret = media_create_pad_link(source, source_pad,
760 sink, sink_pad,
761 flags);
762 if (ret)
763 return ret;
764 flags &= ~(MEDIA_LNK_FL_ENABLED |
765 MEDIA_LNK_FL_IMMUTABLE);
766 }
767 }
768 return 0;
769 }
770
771 /* Handle 1:n and n:1 cases */
772 if (source)
773 function = sink_function;
774 else
775 function = source_function;
776
777 media_device_for_each_entity(entity, mdev) {
778 if (entity->function != function)
779 continue;
780
781 if (source)
782 ret = media_create_pad_link(source, source_pad,
783 entity, sink_pad, flags);
784 else
785 ret = media_create_pad_link(entity, source_pad,
786 sink, sink_pad, flags);
787 if (ret)
788 return ret;
789 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
790 }
791 return 0;
792}
793EXPORT_SYMBOL_GPL(media_create_pad_links);
794
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300795void __media_entity_remove_links(struct media_entity *entity)
796{
797 struct media_link *link, *tmp;
798
799 list_for_each_entry_safe(link, tmp, &entity->links, list)
800 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300801
802 entity->num_links = 0;
803 entity->num_backlinks = 0;
804}
805EXPORT_SYMBOL_GPL(__media_entity_remove_links);
806
807void media_entity_remove_links(struct media_entity *entity)
808{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200809 struct media_device *mdev = entity->graph_obj.mdev;
810
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300811 /* Do nothing if the entity is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200812 if (mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300813 return;
814
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300815 mutex_lock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300816 __media_entity_remove_links(entity);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -0300817 mutex_unlock(&mdev->graph_mutex);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300818}
819EXPORT_SYMBOL_GPL(media_entity_remove_links);
820
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300821static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
822{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300823 int ret;
824
825 /* Notify both entities. */
826 ret = media_entity_call(link->source->entity, link_setup,
827 link->source, link->sink, flags);
828 if (ret < 0 && ret != -ENOIOCTLCMD)
829 return ret;
830
831 ret = media_entity_call(link->sink->entity, link_setup,
832 link->sink, link->source, flags);
833 if (ret < 0 && ret != -ENOIOCTLCMD) {
834 media_entity_call(link->source->entity, link_setup,
835 link->source, link->sink, link->flags);
836 return ret;
837 }
838
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300839 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300840 link->reverse->flags = link->flags;
841
842 return 0;
843}
844
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300845int __media_entity_setup_link(struct media_link *link, u32 flags)
846{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300847 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300848 struct media_device *mdev;
849 struct media_entity *source, *sink;
850 int ret = -EBUSY;
851
852 if (link == NULL)
853 return -EINVAL;
854
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300855 /* The non-modifiable link flags must not be modified. */
856 if ((link->flags & ~mask) != (flags & ~mask))
857 return -EINVAL;
858
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300859 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
860 return link->flags == flags ? 0 : -EINVAL;
861
862 if (link->flags == flags)
863 return 0;
864
865 source = link->source->entity;
866 sink = link->sink->entity;
867
Laurent Pincharte02188c2010-08-25 09:00:41 -0300868 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
869 (source->stream_count || sink->stream_count))
870 return -EBUSY;
871
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300872 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300873
Laurent Pinchart68429f52015-11-03 00:27:51 -0200874 if (mdev->ops && mdev->ops->link_notify) {
875 ret = mdev->ops->link_notify(link, flags,
876 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300877 if (ret < 0)
878 return ret;
879 }
880
881 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300882
Laurent Pinchart68429f52015-11-03 00:27:51 -0200883 if (mdev->ops && mdev->ops->link_notify)
884 mdev->ops->link_notify(link, flags,
885 MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300886
887 return ret;
888}
Shuah Khanefc70272016-02-11 21:41:23 -0200889EXPORT_SYMBOL_GPL(__media_entity_setup_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300890
891int media_entity_setup_link(struct media_link *link, u32 flags)
892{
893 int ret;
894
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200895 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300896 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200897 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300898
899 return ret;
900}
901EXPORT_SYMBOL_GPL(media_entity_setup_link);
902
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300903struct media_link *
904media_entity_find_link(struct media_pad *source, struct media_pad *sink)
905{
906 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300907
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300908 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300909 if (link->source->entity == source->entity &&
910 link->source->index == source->index &&
911 link->sink->entity == sink->entity &&
912 link->sink->index == sink->index)
913 return link;
914 }
915
916 return NULL;
917}
918EXPORT_SYMBOL_GPL(media_entity_find_link);
919
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300920struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300921{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300922 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300923
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300924 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300925 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
926 continue;
927
928 if (link->source == pad)
929 return link->sink;
930
931 if (link->sink == pad)
932 return link->source;
933 }
934
935 return NULL;
936
937}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300938EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300939
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300940static void media_interface_init(struct media_device *mdev,
941 struct media_interface *intf,
942 u32 gobj_type,
943 u32 intf_type, u32 flags)
944{
945 intf->type = intf_type;
946 intf->flags = flags;
947 INIT_LIST_HEAD(&intf->links);
948
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200949 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300950}
951
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300952/* Functions related to the media interface via device nodes */
953
954struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
955 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300956 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300957{
958 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300959
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300960 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300961 if (!devnode)
962 return NULL;
963
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300964 devnode->major = major;
965 devnode->minor = minor;
966
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300967 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
968 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300969
970 return devnode;
971}
972EXPORT_SYMBOL_GPL(media_devnode_create);
973
974void media_devnode_remove(struct media_intf_devnode *devnode)
975{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300976 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200977 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300978 kfree(devnode);
979}
980EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300981
982struct media_link *media_create_intf_link(struct media_entity *entity,
983 struct media_interface *intf,
984 u32 flags)
985{
986 struct media_link *link;
987
988 link = media_add_link(&intf->links);
989 if (link == NULL)
990 return NULL;
991
992 link->intf = intf;
993 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200994 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300995
996 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200997 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300998 &link->graph_obj);
999
1000 return link;
1001}
1002EXPORT_SYMBOL_GPL(media_create_intf_link);
1003
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001004void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001005{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001006 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -02001007 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001008 kfree(link);
1009}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001010EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001011
1012void media_remove_intf_link(struct media_link *link)
1013{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001014 struct media_device *mdev = link->graph_obj.mdev;
1015
1016 /* Do nothing if the intf is not registered. */
1017 if (mdev == NULL)
1018 return;
1019
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001020 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001021 __media_remove_intf_link(link);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001022 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001023}
1024EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001025
1026void __media_remove_intf_links(struct media_interface *intf)
1027{
1028 struct media_link *link, *tmp;
1029
1030 list_for_each_entry_safe(link, tmp, &intf->links, list)
1031 __media_remove_intf_link(link);
1032
1033}
1034EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1035
1036void media_remove_intf_links(struct media_interface *intf)
1037{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001038 struct media_device *mdev = intf->graph_obj.mdev;
1039
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001040 /* Do nothing if the intf is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -02001041 if (mdev == NULL)
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001042 return;
1043
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001044 mutex_lock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001045 __media_remove_intf_links(intf);
Mauro Carvalho Chehabe2c91d42016-04-06 10:55:24 -03001046 mutex_unlock(&mdev->graph_mutex);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001047}
1048EXPORT_SYMBOL_GPL(media_remove_intf_links);