blob: bab592c6e785db3fbe7fdc6d24d2f1925074f94f [file] [log] [blame]
Laurent Pinchart53e269c2009-12-09 08:40:00 -03001/*
2 * Media entity
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -030023#include <linux/bitmap.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030024#include <linux/module.h>
25#include <linux/slab.h>
26#include <media/media-entity.h>
Laurent Pinchart503c3d822010-03-07 15:04:59 -030027#include <media/media-device.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030028
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030029static inline const char *gobj_type(enum media_gobj_type type)
30{
31 switch (type) {
32 case MEDIA_GRAPH_ENTITY:
33 return "entity";
34 case MEDIA_GRAPH_PAD:
35 return "pad";
36 case MEDIA_GRAPH_LINK:
37 return "link";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030038 case MEDIA_GRAPH_INTF_DEVNODE:
39 return "intf-devnode";
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030040 default:
41 return "unknown";
42 }
43}
44
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030045static inline const char *intf_type(struct media_interface *intf)
46{
47 switch (intf->type) {
48 case MEDIA_INTF_T_DVB_FE:
49 return "frontend";
50 case MEDIA_INTF_T_DVB_DEMUX:
51 return "demux";
52 case MEDIA_INTF_T_DVB_DVR:
53 return "DVR";
54 case MEDIA_INTF_T_DVB_CA:
55 return "CA";
56 case MEDIA_INTF_T_DVB_NET:
57 return "dvbnet";
58 case MEDIA_INTF_T_V4L_VIDEO:
59 return "video";
60 case MEDIA_INTF_T_V4L_VBI:
61 return "vbi";
62 case MEDIA_INTF_T_V4L_RADIO:
63 return "radio";
64 case MEDIA_INTF_T_V4L_SUBDEV:
65 return "v4l2-subdev";
66 case MEDIA_INTF_T_V4L_SWRADIO:
67 return "swradio";
Shuah Khan5af557a2016-02-11 21:41:18 -020068 case MEDIA_INTF_T_ALSA_PCM_CAPTURE:
69 return "pcm-capture";
70 case MEDIA_INTF_T_ALSA_PCM_PLAYBACK:
71 return "pcm-playback";
72 case MEDIA_INTF_T_ALSA_CONTROL:
73 return "alsa-control";
74 case MEDIA_INTF_T_ALSA_COMPRESS:
75 return "compress";
76 case MEDIA_INTF_T_ALSA_RAWMIDI:
77 return "rawmidi";
78 case MEDIA_INTF_T_ALSA_HWDEP:
79 return "hwdep";
80 case MEDIA_INTF_T_ALSA_SEQUENCER:
81 return "sequencer";
82 case MEDIA_INTF_T_ALSA_TIMER:
83 return "timer";
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030084 default:
85 return "unknown-intf";
86 }
87};
88
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020089__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
90 int idx_max)
91{
Sakari Ailusf7b5dff2016-01-27 12:47:54 -020092 idx_max = ALIGN(idx_max, BITS_PER_LONG);
93 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
94 GFP_KERNEL);
Sakari Ailus030e89e2015-12-16 11:32:36 -020095 if (!ent_enum->bmap)
96 return -ENOMEM;
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020097
98 bitmap_zero(ent_enum->bmap, idx_max);
99 ent_enum->idx_max = idx_max;
100
101 return 0;
102}
103EXPORT_SYMBOL_GPL(__media_entity_enum_init);
104
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200105void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
106{
Sakari Ailus030e89e2015-12-16 11:32:36 -0200107 kfree(ent_enum->bmap);
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200108}
109EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
110
111/**
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200112 * dev_dbg_obj - Prints in debug mode a change on some object
113 *
114 * @event_name: Name of the event to report. Could be __func__
115 * @gobj: Pointer to the object
116 *
117 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
118 * won't produce any code.
119 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300120static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
121{
122#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
123 switch (media_type(gobj)) {
124 case MEDIA_GRAPH_ENTITY:
125 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200126 "%s id %u: entity '%s'\n",
127 event_name, media_id(gobj),
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300128 gobj_to_entity(gobj)->name);
129 break;
130 case MEDIA_GRAPH_LINK:
131 {
132 struct media_link *link = gobj_to_link(gobj);
133
134 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200135 "%s id %u: %s link id %u ==> id %u\n",
136 event_name, media_id(gobj),
137 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
138 "data" : "interface",
139 media_id(link->gobj0),
140 media_id(link->gobj1));
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300141 break;
142 }
143 case MEDIA_GRAPH_PAD:
144 {
145 struct media_pad *pad = gobj_to_pad(gobj);
146
147 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200148 "%s id %u: %s%spad '%s':%d\n",
149 event_name, media_id(gobj),
150 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
Mauro Carvalho Chehab6c24d462015-08-21 18:26:42 -0300151 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300152 pad->entity->name, pad->index);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300153 break;
154 }
155 case MEDIA_GRAPH_INTF_DEVNODE:
156 {
157 struct media_interface *intf = gobj_to_intf(gobj);
158 struct media_intf_devnode *devnode = intf_to_devnode(intf);
159
160 dev_dbg(gobj->mdev->dev,
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200161 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
162 event_name, media_id(gobj),
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300163 intf_type(intf),
164 devnode->major, devnode->minor);
165 break;
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300166 }
167 }
168#endif
169}
170
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200171void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300172 enum media_gobj_type type,
173 struct media_gobj *gobj)
174{
Mauro Carvalho Chehab8f6d3682015-08-19 20:18:35 -0300175 BUG_ON(!mdev);
176
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300177 gobj->mdev = mdev;
178
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300179 /* Create a per-type unique object ID */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200180 gobj->id = media_gobj_gen_id(type, ++mdev->id);
181
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300182 switch (type) {
183 case MEDIA_GRAPH_ENTITY:
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -0300184 list_add_tail(&gobj->list, &mdev->entities);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300185 break;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300186 case MEDIA_GRAPH_PAD:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300187 list_add_tail(&gobj->list, &mdev->pads);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300188 break;
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300189 case MEDIA_GRAPH_LINK:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300190 list_add_tail(&gobj->list, &mdev->links);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300191 break;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300192 case MEDIA_GRAPH_INTF_DEVNODE:
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300193 list_add_tail(&gobj->list, &mdev->interfaces);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300194 break;
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300195 }
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300196
197 mdev->topology_version++;
198
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300199 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300200}
201
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200202void media_gobj_destroy(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300203{
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300204 dev_dbg_obj(__func__, gobj);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300205
Mauro Carvalho Chehab2521fda2015-08-23 09:40:26 -0300206 gobj->mdev->topology_version++;
207
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300208 /* Remove the object from mdev list */
209 list_del(&gobj->list);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300210}
211
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200212int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
213 struct media_pad *pads)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300214{
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300215 struct media_device *mdev = entity->graph_obj.mdev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300216 unsigned int i;
217
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300218 entity->num_pads = num_pads;
219 entity->pads = pads;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300220
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300221 if (mdev)
222 spin_lock(&mdev->lock);
223
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300224 for (i = 0; i < num_pads; i++) {
225 pads[i].entity = entity;
226 pads[i].index = i;
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300227 if (mdev)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200228 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300229 &entity->pads[i].graph_obj);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300230 }
231
Javier Martinez Canillasdb141a32015-09-08 14:10:56 -0300232 if (mdev)
233 spin_unlock(&mdev->lock);
234
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300235 return 0;
236}
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200237EXPORT_SYMBOL_GPL(media_entity_pads_init);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300238
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300239/* -----------------------------------------------------------------------------
240 * Graph traversal
241 */
242
243static struct media_entity *
244media_entity_other(struct media_entity *entity, struct media_link *link)
245{
246 if (link->source->entity == entity)
247 return link->sink->entity;
248 else
249 return link->source->entity;
250}
251
252/* push an entity to traversal stack */
253static void stack_push(struct media_entity_graph *graph,
254 struct media_entity *entity)
255{
256 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
257 WARN_ON(1);
258 return;
259 }
260 graph->top++;
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200261 graph->stack[graph->top].link = entity->links.next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300262 graph->stack[graph->top].entity = entity;
263}
264
265static struct media_entity *stack_pop(struct media_entity_graph *graph)
266{
267 struct media_entity *entity;
268
269 entity = graph->stack[graph->top].entity;
270 graph->top--;
271
272 return entity;
273}
274
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300275#define link_top(en) ((en)->stack[(en)->top].link)
276#define stack_top(en) ((en)->stack[(en)->top].entity)
277
Sakari Ailus0798ce42015-12-16 11:32:37 -0200278/*
279 * TODO: Get rid of this.
280 */
Mauro Carvalho Chehab430a6722015-12-16 15:18:25 -0200281#define MEDIA_ENTITY_MAX_PADS 512
Sakari Ailus0798ce42015-12-16 11:32:37 -0200282
Sakari Ailuse03d2202015-12-16 11:32:22 -0200283/**
284 * media_entity_graph_walk_init - Allocate resources for graph walk
285 * @graph: Media graph structure that will be used to walk the graph
286 * @mdev: Media device
287 *
288 * Reserve resources for graph walk in media device's current
289 * state. The memory must be released using
290 * media_entity_graph_walk_free().
291 *
292 * Returns error on failure, zero on success.
293 */
294__must_check int media_entity_graph_walk_init(
295 struct media_entity_graph *graph, struct media_device *mdev)
296{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200297 return media_entity_enum_init(&graph->ent_enum, mdev);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200298}
299EXPORT_SYMBOL_GPL(media_entity_graph_walk_init);
300
301/**
302 * media_entity_graph_walk_cleanup - Release resources related to graph walking
303 * @graph: Media graph structure that was used to walk the graph
304 */
305void media_entity_graph_walk_cleanup(struct media_entity_graph *graph)
306{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200307 media_entity_enum_cleanup(&graph->ent_enum);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200308}
309EXPORT_SYMBOL_GPL(media_entity_graph_walk_cleanup);
310
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300311void media_entity_graph_walk_start(struct media_entity_graph *graph,
312 struct media_entity *entity)
313{
Sakari Ailus29d8da02015-12-16 11:32:28 -0200314 media_entity_enum_zero(&graph->ent_enum);
315 media_entity_enum_set(&graph->ent_enum, entity);
316
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300317 graph->top = 0;
318 graph->stack[graph->top].entity = NULL;
319 stack_push(graph, entity);
320}
321EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
322
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300323struct media_entity *
324media_entity_graph_walk_next(struct media_entity_graph *graph)
325{
326 if (stack_top(graph) == NULL)
327 return NULL;
328
329 /*
330 * Depth first search. Push entity to stack and continue from
331 * top of the stack until no more entities on the level can be
332 * found.
333 */
Javier Martinez Canillas313895f2015-12-11 15:16:36 -0200334 while (link_top(graph) != &stack_top(graph)->links) {
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300335 struct media_entity *entity = stack_top(graph);
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300336 struct media_link *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300337 struct media_entity *next;
338
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300339 link = list_entry(link_top(graph), typeof(*link), list);
340
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300341 /* The link is not enabled so we do not follow. */
342 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300343 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300344 continue;
345 }
346
347 /* Get the entity in the other end of the link . */
348 next = media_entity_other(entity, link);
349
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300350 /* Has the entity already been visited? */
Sakari Ailus29d8da02015-12-16 11:32:28 -0200351 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300352 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300353 continue;
354 }
355
356 /* Push the new entity to stack and start over. */
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300357 link_top(graph) = link_top(graph)->next;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300358 stack_push(graph, next);
359 }
360
361 return stack_pop(graph);
362}
363EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
364
365/* -----------------------------------------------------------------------------
Laurent Pincharte02188c2010-08-25 09:00:41 -0300366 * Pipeline management
367 */
368
Sakari Ailusaf88be32012-01-11 06:25:15 -0300369__must_check int media_entity_pipeline_start(struct media_entity *entity,
370 struct media_pipeline *pipe)
Laurent Pincharte02188c2010-08-25 09:00:41 -0300371{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300372 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus5dd87752015-12-16 11:32:21 -0200373 struct media_entity_graph *graph = &pipe->graph;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300374 struct media_entity *entity_err = entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300375 struct media_link *link;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300376 int ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300377
378 mutex_lock(&mdev->graph_mutex);
379
Sakari Ailus74a41332015-12-16 11:32:29 -0200380 if (!pipe->streaming_count++) {
381 ret = media_entity_graph_walk_init(&pipe->graph, mdev);
382 if (ret)
383 goto error_graph_walk_start;
Sakari Ailus106b9902015-12-16 15:32:23 +0200384 }
385
386 media_entity_graph_walk_start(&pipe->graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300387
Sakari Ailus5dd87752015-12-16 11:32:21 -0200388 while ((entity = media_entity_graph_walk_next(graph))) {
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300389 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
390 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300391
Laurent Pincharte02188c2010-08-25 09:00:41 -0300392 entity->stream_count++;
Sakari Ailus8aaf62b2015-11-29 17:20:02 -0200393
394 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
395 ret = -EBUSY;
396 goto error;
397 }
398
Laurent Pincharte02188c2010-08-25 09:00:41 -0300399 entity->pipe = pipe;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300400
401 /* Already streaming --- no need to check. */
402 if (entity->stream_count > 1)
403 continue;
404
405 if (!entity->ops || !entity->ops->link_validate)
406 continue;
407
Sakari Ailusde49c282013-10-13 08:00:26 -0300408 bitmap_zero(active, entity->num_pads);
409 bitmap_fill(has_no_links, entity->num_pads);
410
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300411 list_for_each_entry(link, &entity->links, list) {
Sakari Ailusde49c282013-10-13 08:00:26 -0300412 struct media_pad *pad = link->sink->entity == entity
413 ? link->sink : link->source;
Sakari Ailusaf88be32012-01-11 06:25:15 -0300414
Sakari Ailusde49c282013-10-13 08:00:26 -0300415 /* Mark that a pad is connected by a link. */
416 bitmap_clear(has_no_links, pad->index, 1);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300417
Sakari Ailusde49c282013-10-13 08:00:26 -0300418 /*
419 * Pads that either do not need to connect or
420 * are connected through an enabled link are
421 * fine.
422 */
423 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
424 link->flags & MEDIA_LNK_FL_ENABLED)
425 bitmap_set(active, pad->index, 1);
426
427 /*
428 * Link validation will only take place for
429 * sink ends of the link that are enabled.
430 */
431 if (link->sink != pad ||
432 !(link->flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusaf88be32012-01-11 06:25:15 -0300433 continue;
434
435 ret = entity->ops->link_validate(link);
Sakari Ailusfab9d302014-10-28 20:35:04 -0300436 if (ret < 0 && ret != -ENOIOCTLCMD) {
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300437 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300438 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
Sakari Ailus823ea2a2015-02-12 11:43:11 -0200439 link->source->entity->name,
440 link->source->index,
441 entity->name, link->sink->index, ret);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300442 goto error;
Sakari Ailusfab9d302014-10-28 20:35:04 -0300443 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300444 }
Sakari Ailusde49c282013-10-13 08:00:26 -0300445
446 /* Either no links or validated links are fine. */
447 bitmap_or(active, active, has_no_links, entity->num_pads);
448
449 if (!bitmap_full(active, entity->num_pads)) {
450 ret = -EPIPE;
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300451 dev_dbg(entity->graph_obj.mdev->dev,
Sakari Ailusfab9d302014-10-28 20:35:04 -0300452 "\"%s\":%u must be connected by an enabled link\n",
453 entity->name,
Sakari Ailus094f1ca2014-11-03 17:55:51 -0300454 (unsigned)find_first_zero_bit(
455 active, entity->num_pads));
Sakari Ailusde49c282013-10-13 08:00:26 -0300456 goto error;
457 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300458 }
459
460 mutex_unlock(&mdev->graph_mutex);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300461
462 return 0;
463
464error:
465 /*
466 * Link validation on graph failed. We revert what we did and
467 * return the error.
468 */
Sakari Ailus5dd87752015-12-16 11:32:21 -0200469 media_entity_graph_walk_start(graph, entity_err);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300470
Sakari Ailus5dd87752015-12-16 11:32:21 -0200471 while ((entity_err = media_entity_graph_walk_next(graph))) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200472 /* don't let the stream_count go negative */
473 if (entity->stream_count > 0) {
474 entity_err->stream_count--;
475 if (entity_err->stream_count == 0)
476 entity_err->pipe = NULL;
477 }
Sakari Ailusaf88be32012-01-11 06:25:15 -0300478
479 /*
480 * We haven't increased stream_count further than this
481 * so we quit here.
482 */
483 if (entity_err == entity)
484 break;
485 }
486
Sakari Ailus74a41332015-12-16 11:32:29 -0200487error_graph_walk_start:
488 if (!--pipe->streaming_count)
489 media_entity_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200490
Sakari Ailusaf88be32012-01-11 06:25:15 -0300491 mutex_unlock(&mdev->graph_mutex);
492
493 return ret;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300494}
495EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
496
Laurent Pincharte02188c2010-08-25 09:00:41 -0300497void media_entity_pipeline_stop(struct media_entity *entity)
498{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300499 struct media_device *mdev = entity->graph_obj.mdev;
Sakari Ailus5dd87752015-12-16 11:32:21 -0200500 struct media_entity_graph *graph = &entity->pipe->graph;
Sakari Ailus74a41332015-12-16 11:32:29 -0200501 struct media_pipeline *pipe = entity->pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300502
503 mutex_lock(&mdev->graph_mutex);
504
Sakari Ailus74a41332015-12-16 11:32:29 -0200505 WARN_ON(!pipe->streaming_count);
Sakari Ailus5dd87752015-12-16 11:32:21 -0200506 media_entity_graph_walk_start(graph, entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300507
Sakari Ailus5dd87752015-12-16 11:32:21 -0200508 while ((entity = media_entity_graph_walk_next(graph))) {
Shuah Khan3801bc72016-01-30 18:10:52 -0200509 /* don't let the stream_count go negative */
510 if (entity->stream_count > 0) {
511 entity->stream_count--;
512 if (entity->stream_count == 0)
513 entity->pipe = NULL;
514 }
Laurent Pincharte02188c2010-08-25 09:00:41 -0300515 }
516
Sakari Ailus74a41332015-12-16 11:32:29 -0200517 if (!--pipe->streaming_count)
518 media_entity_graph_walk_cleanup(graph);
Sakari Ailus106b9902015-12-16 15:32:23 +0200519
Laurent Pincharte02188c2010-08-25 09:00:41 -0300520 mutex_unlock(&mdev->graph_mutex);
521}
522EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
523
524/* -----------------------------------------------------------------------------
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300525 * Module use count
526 */
527
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300528struct media_entity *media_entity_get(struct media_entity *entity)
529{
530 if (entity == NULL)
531 return NULL;
532
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300533 if (entity->graph_obj.mdev->dev &&
534 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300535 return NULL;
536
537 return entity;
538}
539EXPORT_SYMBOL_GPL(media_entity_get);
540
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300541void media_entity_put(struct media_entity *entity)
542{
543 if (entity == NULL)
544 return;
545
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300546 if (entity->graph_obj.mdev->dev)
547 module_put(entity->graph_obj.mdev->dev->driver->owner);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300548}
549EXPORT_SYMBOL_GPL(media_entity_put);
550
551/* -----------------------------------------------------------------------------
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300552 * Links management
553 */
554
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300555static struct media_link *media_add_link(struct list_head *head)
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300556{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300557 struct media_link *link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300558
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300559 link = kzalloc(sizeof(*link), GFP_KERNEL);
560 if (link == NULL)
561 return NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300562
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300563 list_add_tail(&link->list, head);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300564
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300565 return link;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300566}
567
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300568static void __media_entity_remove_link(struct media_entity *entity,
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200569 struct media_link *link)
570{
571 struct media_link *rlink, *tmp;
572 struct media_entity *remote;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200573
574 if (link->source->entity == entity)
575 remote = link->sink->entity;
576 else
577 remote = link->source->entity;
578
579 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
Mauro Carvalho Chehab58f69ee2015-12-11 11:25:23 -0200580 if (rlink != link->reverse)
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200581 continue;
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200582
583 if (link->source->entity == entity)
584 remote->num_backlinks--;
585
586 /* Remove the remote link */
587 list_del(&rlink->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200588 media_gobj_destroy(&rlink->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200589 kfree(rlink);
590
591 if (--remote->num_links == 0)
592 break;
593 }
594 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200595 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab5abad222015-12-11 11:19:38 -0200596 kfree(link);
597}
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300598
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300599int
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300600media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300601 struct media_entity *sink, u16 sink_pad, u32 flags)
602{
603 struct media_link *link;
604 struct media_link *backlink;
605
606 BUG_ON(source == NULL || sink == NULL);
607 BUG_ON(source_pad >= source->num_pads);
608 BUG_ON(sink_pad >= sink->num_pads);
609
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300610 link = media_add_link(&source->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300611 if (link == NULL)
612 return -ENOMEM;
613
614 link->source = &source->pads[source_pad];
615 link->sink = &sink->pads[sink_pad];
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200616 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300617
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300618 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200619 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300620 &link->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300621
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300622 /* Create the backlink. Backlinks are used to help graph traversal and
623 * are not reported to userspace.
624 */
Mauro Carvalho Chehab23615de2015-08-20 08:21:35 -0300625 backlink = media_add_link(&sink->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300626 if (backlink == NULL) {
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300627 __media_entity_remove_link(source, link);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300628 return -ENOMEM;
629 }
630
631 backlink->source = &source->pads[source_pad];
632 backlink->sink = &sink->pads[sink_pad];
633 backlink->flags = flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300634 backlink->is_backlink = true;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300635
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300636 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200637 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300638 &backlink->graph_obj);
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300639
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300640 link->reverse = backlink;
641 backlink->reverse = link;
642
643 sink->num_backlinks++;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300644 sink->num_links++;
645 source->num_links++;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300646
647 return 0;
648}
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300649EXPORT_SYMBOL_GPL(media_create_pad_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300650
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200651int media_create_pad_links(const struct media_device *mdev,
652 const u32 source_function,
653 struct media_entity *source,
654 const u16 source_pad,
655 const u32 sink_function,
656 struct media_entity *sink,
657 const u16 sink_pad,
658 u32 flags,
659 const bool allow_both_undefined)
660{
661 struct media_entity *entity;
662 unsigned function;
663 int ret;
664
665 /* Trivial case: 1:1 relation */
666 if (source && sink)
667 return media_create_pad_link(source, source_pad,
668 sink, sink_pad, flags);
669
670 /* Worse case scenario: n:n relation */
671 if (!source && !sink) {
672 if (!allow_both_undefined)
673 return 0;
674 media_device_for_each_entity(source, mdev) {
675 if (source->function != source_function)
676 continue;
677 media_device_for_each_entity(sink, mdev) {
678 if (sink->function != sink_function)
679 continue;
680 ret = media_create_pad_link(source, source_pad,
681 sink, sink_pad,
682 flags);
683 if (ret)
684 return ret;
685 flags &= ~(MEDIA_LNK_FL_ENABLED |
686 MEDIA_LNK_FL_IMMUTABLE);
687 }
688 }
689 return 0;
690 }
691
692 /* Handle 1:n and n:1 cases */
693 if (source)
694 function = sink_function;
695 else
696 function = source_function;
697
698 media_device_for_each_entity(entity, mdev) {
699 if (entity->function != function)
700 continue;
701
702 if (source)
703 ret = media_create_pad_link(source, source_pad,
704 entity, sink_pad, flags);
705 else
706 ret = media_create_pad_link(entity, source_pad,
707 sink, sink_pad, flags);
708 if (ret)
709 return ret;
710 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
711 }
712 return 0;
713}
714EXPORT_SYMBOL_GPL(media_create_pad_links);
715
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300716void __media_entity_remove_links(struct media_entity *entity)
717{
718 struct media_link *link, *tmp;
719
720 list_for_each_entry_safe(link, tmp, &entity->links, list)
721 __media_entity_remove_link(entity, link);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300722
723 entity->num_links = 0;
724 entity->num_backlinks = 0;
725}
726EXPORT_SYMBOL_GPL(__media_entity_remove_links);
727
728void media_entity_remove_links(struct media_entity *entity)
729{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200730 struct media_device *mdev = entity->graph_obj.mdev;
731
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300732 /* Do nothing if the entity is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200733 if (mdev == NULL)
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300734 return;
735
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200736 spin_lock(&mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300737 __media_entity_remove_links(entity);
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200738 spin_unlock(&mdev->lock);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300739}
740EXPORT_SYMBOL_GPL(media_entity_remove_links);
741
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300742static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
743{
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300744 int ret;
745
746 /* Notify both entities. */
747 ret = media_entity_call(link->source->entity, link_setup,
748 link->source, link->sink, flags);
749 if (ret < 0 && ret != -ENOIOCTLCMD)
750 return ret;
751
752 ret = media_entity_call(link->sink->entity, link_setup,
753 link->sink, link->source, flags);
754 if (ret < 0 && ret != -ENOIOCTLCMD) {
755 media_entity_call(link->source->entity, link_setup,
756 link->source, link->sink, link->flags);
757 return ret;
758 }
759
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300760 link->flags = flags;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300761 link->reverse->flags = link->flags;
762
763 return 0;
764}
765
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300766int __media_entity_setup_link(struct media_link *link, u32 flags)
767{
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300768 const u32 mask = MEDIA_LNK_FL_ENABLED;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300769 struct media_device *mdev;
770 struct media_entity *source, *sink;
771 int ret = -EBUSY;
772
773 if (link == NULL)
774 return -EINVAL;
775
Laurent Pinchart7a6f0b22011-03-11 11:34:35 -0300776 /* The non-modifiable link flags must not be modified. */
777 if ((link->flags & ~mask) != (flags & ~mask))
778 return -EINVAL;
779
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300780 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
781 return link->flags == flags ? 0 : -EINVAL;
782
783 if (link->flags == flags)
784 return 0;
785
786 source = link->source->entity;
787 sink = link->sink->entity;
788
Laurent Pincharte02188c2010-08-25 09:00:41 -0300789 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
790 (source->stream_count || sink->stream_count))
791 return -EBUSY;
792
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300793 mdev = source->graph_obj.mdev;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300794
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300795 if (mdev->link_notify) {
796 ret = mdev->link_notify(link, flags,
797 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300798 if (ret < 0)
799 return ret;
800 }
801
802 ret = __media_entity_setup_link_notify(link, flags);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300803
Sylwester Nawrocki813f5c02013-05-31 10:37:26 -0300804 if (mdev->link_notify)
805 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300806
807 return ret;
808}
Shuah Khanefc70272016-02-11 21:41:23 -0200809EXPORT_SYMBOL_GPL(__media_entity_setup_link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300810
811int media_entity_setup_link(struct media_link *link, u32 flags)
812{
813 int ret;
814
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200815 mutex_lock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300816 ret = __media_entity_setup_link(link, flags);
Mauro Carvalho Chehab5c883ed2015-12-15 07:58:18 -0200817 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300818
819 return ret;
820}
821EXPORT_SYMBOL_GPL(media_entity_setup_link);
822
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300823struct media_link *
824media_entity_find_link(struct media_pad *source, struct media_pad *sink)
825{
826 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300827
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300828 list_for_each_entry(link, &source->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300829 if (link->source->entity == source->entity &&
830 link->source->index == source->index &&
831 link->sink->entity == sink->entity &&
832 link->sink->index == sink->index)
833 return link;
834 }
835
836 return NULL;
837}
838EXPORT_SYMBOL_GPL(media_entity_find_link);
839
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300840struct media_pad *media_entity_remote_pad(struct media_pad *pad)
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300841{
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300842 struct media_link *link;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300843
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300844 list_for_each_entry(link, &pad->entity->links, list) {
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300845 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
846 continue;
847
848 if (link->source == pad)
849 return link->sink;
850
851 if (link->sink == pad)
852 return link->source;
853 }
854
855 return NULL;
856
857}
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300858EXPORT_SYMBOL_GPL(media_entity_remote_pad);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300859
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300860static void media_interface_init(struct media_device *mdev,
861 struct media_interface *intf,
862 u32 gobj_type,
863 u32 intf_type, u32 flags)
864{
865 intf->type = intf_type;
866 intf->flags = flags;
867 INIT_LIST_HEAD(&intf->links);
868
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200869 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300870}
871
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300872/* Functions related to the media interface via device nodes */
873
874struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
875 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300876 u32 major, u32 minor)
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300877{
878 struct media_intf_devnode *devnode;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300879
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300880 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300881 if (!devnode)
882 return NULL;
883
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300884 devnode->major = major;
885 devnode->minor = minor;
886
Mauro Carvalho Chehab1283f842015-08-28 15:43:36 -0300887 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
888 type, flags);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300889
890 return devnode;
891}
892EXPORT_SYMBOL_GPL(media_devnode_create);
893
894void media_devnode_remove(struct media_intf_devnode *devnode)
895{
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300896 media_remove_intf_links(&devnode->intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200897 media_gobj_destroy(&devnode->intf.graph_obj);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300898 kfree(devnode);
899}
900EXPORT_SYMBOL_GPL(media_devnode_remove);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300901
902struct media_link *media_create_intf_link(struct media_entity *entity,
903 struct media_interface *intf,
904 u32 flags)
905{
906 struct media_link *link;
907
908 link = media_add_link(&intf->links);
909 if (link == NULL)
910 return NULL;
911
912 link->intf = intf;
913 link->entity = entity;
Mauro Carvalho Chehab82ae2a52015-12-11 18:09:13 -0200914 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300915
916 /* Initialize graph object embedded at the new link */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200917 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300918 &link->graph_obj);
919
920 return link;
921}
922EXPORT_SYMBOL_GPL(media_create_intf_link);
923
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300924void __media_remove_intf_link(struct media_link *link)
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300925{
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300926 list_del(&link->list);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200927 media_gobj_destroy(&link->graph_obj);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300928 kfree(link);
929}
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300930EXPORT_SYMBOL_GPL(__media_remove_intf_link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300931
932void media_remove_intf_link(struct media_link *link)
933{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200934 struct media_device *mdev = link->graph_obj.mdev;
935
936 /* Do nothing if the intf is not registered. */
937 if (mdev == NULL)
938 return;
939
940 spin_lock(&mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300941 __media_remove_intf_link(link);
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200942 spin_unlock(&mdev->lock);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300943}
944EXPORT_SYMBOL_GPL(media_remove_intf_link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300945
946void __media_remove_intf_links(struct media_interface *intf)
947{
948 struct media_link *link, *tmp;
949
950 list_for_each_entry_safe(link, tmp, &intf->links, list)
951 __media_remove_intf_link(link);
952
953}
954EXPORT_SYMBOL_GPL(__media_remove_intf_links);
955
956void media_remove_intf_links(struct media_interface *intf)
957{
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200958 struct media_device *mdev = intf->graph_obj.mdev;
959
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300960 /* Do nothing if the intf is not registered. */
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200961 if (mdev == NULL)
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300962 return;
963
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200964 spin_lock(&mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300965 __media_remove_intf_links(intf);
Mauro Carvalho Chehabcc4a8252015-12-15 08:01:13 -0200966 spin_unlock(&mdev->lock);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300967}
968EXPORT_SYMBOL_GPL(media_remove_intf_links);