Merge branch 'for-5.2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[linux-2.6-block.git] / drivers / media / media-entity.c
CommitLineData
53e269c1
LP
1/*
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.
53e269c1
LP
17 */
18
5c7b25b9 19#include <linux/bitmap.h>
d295c6a4 20#include <linux/property.h>
53e269c1
LP
21#include <linux/slab.h>
22#include <media/media-entity.h>
503c3d82 23#include <media/media-device.h>
53e269c1 24
39a956c4
MCC
25static 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";
27e543fa
MCC
34 case MEDIA_GRAPH_INTF_DEVNODE:
35 return "intf-devnode";
39a956c4
MCC
36 default:
37 return "unknown";
38 }
39}
40
27e543fa
MCC
41static inline const char *intf_type(struct media_interface *intf)
42{
43 switch (intf->type) {
44 case MEDIA_INTF_T_DVB_FE:
66c1db1b 45 return "dvb-frontend";
27e543fa 46 case MEDIA_INTF_T_DVB_DEMUX:
66c1db1b 47 return "dvb-demux";
27e543fa 48 case MEDIA_INTF_T_DVB_DVR:
66c1db1b 49 return "dvb-dvr";
27e543fa 50 case MEDIA_INTF_T_DVB_CA:
66c1db1b 51 return "dvb-ca";
27e543fa 52 case MEDIA_INTF_T_DVB_NET:
66c1db1b 53 return "dvb-net";
27e543fa 54 case MEDIA_INTF_T_V4L_VIDEO:
66c1db1b 55 return "v4l-video";
27e543fa 56 case MEDIA_INTF_T_V4L_VBI:
66c1db1b 57 return "v4l-vbi";
27e543fa 58 case MEDIA_INTF_T_V4L_RADIO:
66c1db1b 59 return "v4l-radio";
27e543fa 60 case MEDIA_INTF_T_V4L_SUBDEV:
66c1db1b 61 return "v4l-subdev";
27e543fa 62 case MEDIA_INTF_T_V4L_SWRADIO:
66c1db1b 63 return "v4l-swradio";
b2fe22d0
ND
64 case MEDIA_INTF_T_V4L_TOUCH:
65 return "v4l-touch";
27e543fa
MCC
66 default:
67 return "unknown-intf";
68 }
69};
70
c8d54cd5
SA
71__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
72 int idx_max)
73{
f7b5dff0
SA
74 idx_max = ALIGN(idx_max, BITS_PER_LONG);
75 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
76 GFP_KERNEL);
030e89ec
SA
77 if (!ent_enum->bmap)
78 return -ENOMEM;
c8d54cd5
SA
79
80 bitmap_zero(ent_enum->bmap, idx_max);
81 ent_enum->idx_max = idx_max;
82
83 return 0;
84}
85EXPORT_SYMBOL_GPL(__media_entity_enum_init);
86
c8d54cd5
SA
87void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
88{
030e89ec 89 kfree(ent_enum->bmap);
c8d54cd5
SA
90}
91EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
92
1fc25d30
MCC
93/**
94 * dev_dbg_obj - Prints in debug mode a change on some object
95 *
96 * @event_name: Name of the event to report. Could be __func__
97 * @gobj: Pointer to the object
98 *
99 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
100 * won't produce any code.
101 */
39a956c4
MCC
102static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
103{
104#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
105 switch (media_type(gobj)) {
106 case MEDIA_GRAPH_ENTITY:
107 dev_dbg(gobj->mdev->dev,
05b3b77c
MCC
108 "%s id %u: entity '%s'\n",
109 event_name, media_id(gobj),
39a956c4
MCC
110 gobj_to_entity(gobj)->name);
111 break;
112 case MEDIA_GRAPH_LINK:
113 {
114 struct media_link *link = gobj_to_link(gobj);
115
116 dev_dbg(gobj->mdev->dev,
05b3b77c
MCC
117 "%s id %u: %s link id %u ==> id %u\n",
118 event_name, media_id(gobj),
119 media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
120 "data" : "interface",
121 media_id(link->gobj0),
122 media_id(link->gobj1));
39a956c4
MCC
123 break;
124 }
125 case MEDIA_GRAPH_PAD:
126 {
127 struct media_pad *pad = gobj_to_pad(gobj);
128
129 dev_dbg(gobj->mdev->dev,
05b3b77c
MCC
130 "%s id %u: %s%spad '%s':%d\n",
131 event_name, media_id(gobj),
132 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
6c24d460 133 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
39a956c4 134 pad->entity->name, pad->index);
27e543fa
MCC
135 break;
136 }
137 case MEDIA_GRAPH_INTF_DEVNODE:
138 {
139 struct media_interface *intf = gobj_to_intf(gobj);
140 struct media_intf_devnode *devnode = intf_to_devnode(intf);
141
142 dev_dbg(gobj->mdev->dev,
05b3b77c
MCC
143 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
144 event_name, media_id(gobj),
27e543fa
MCC
145 intf_type(intf),
146 devnode->major, devnode->minor);
147 break;
39a956c4
MCC
148 }
149 }
150#endif
151}
152
c350ef83 153void media_gobj_create(struct media_device *mdev,
ec6e4c95
MCC
154 enum media_gobj_type type,
155 struct media_gobj *gobj)
156{
8f6d368f
MCC
157 BUG_ON(!mdev);
158
39a956c4
MCC
159 gobj->mdev = mdev;
160
bfab2aac 161 /* Create a per-type unique object ID */
05b3b77c
MCC
162 gobj->id = media_gobj_gen_id(type, ++mdev->id);
163
bfab2aac
MCC
164 switch (type) {
165 case MEDIA_GRAPH_ENTITY:
05bfa9fa 166 list_add_tail(&gobj->list, &mdev->entities);
bfab2aac 167 break;
18710dc6 168 case MEDIA_GRAPH_PAD:
9155d859 169 list_add_tail(&gobj->list, &mdev->pads);
18710dc6 170 break;
6b6a4278 171 case MEDIA_GRAPH_LINK:
9155d859 172 list_add_tail(&gobj->list, &mdev->links);
6b6a4278 173 break;
27e543fa 174 case MEDIA_GRAPH_INTF_DEVNODE:
9155d859 175 list_add_tail(&gobj->list, &mdev->interfaces);
27e543fa 176 break;
bfab2aac 177 }
2521fdac
MCC
178
179 mdev->topology_version++;
180
39a956c4 181 dev_dbg_obj(__func__, gobj);
ec6e4c95
MCC
182}
183
c350ef83 184void media_gobj_destroy(struct media_gobj *gobj)
ec6e4c95 185{
6753743e
MK
186 /* Do nothing if the object is not linked. */
187 if (gobj->mdev == NULL)
188 return;
189
8d1d3d00
HF
190 dev_dbg_obj(__func__, gobj);
191
2521fdac
MCC
192 gobj->mdev->topology_version++;
193
9155d859
MCC
194 /* Remove the object from mdev list */
195 list_del(&gobj->list);
6753743e
MK
196
197 gobj->mdev = NULL;
ec6e4c95
MCC
198}
199
885ca801
SA
200/*
201 * TODO: Get rid of this.
202 */
203#define MEDIA_ENTITY_MAX_PADS 512
204
1fc25d30
MCC
205int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
206 struct media_pad *pads)
53e269c1 207{
db141a35 208 struct media_device *mdev = entity->graph_obj.mdev;
53e269c1
LP
209 unsigned int i;
210
885ca801
SA
211 if (num_pads >= MEDIA_ENTITY_MAX_PADS)
212 return -E2BIG;
213
53e269c1
LP
214 entity->num_pads = num_pads;
215 entity->pads = pads;
53e269c1 216
db141a35 217 if (mdev)
e2c91d4d 218 mutex_lock(&mdev->graph_mutex);
db141a35 219
53e269c1
LP
220 for (i = 0; i < num_pads; i++) {
221 pads[i].entity = entity;
222 pads[i].index = i;
db141a35 223 if (mdev)
c350ef83 224 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
db141a35 225 &entity->pads[i].graph_obj);
53e269c1
LP
226 }
227
db141a35 228 if (mdev)
e2c91d4d 229 mutex_unlock(&mdev->graph_mutex);
db141a35 230
53e269c1
LP
231 return 0;
232}
ab22e77c 233EXPORT_SYMBOL_GPL(media_entity_pads_init);
53e269c1 234
a5ccc48a
SA
235/* -----------------------------------------------------------------------------
236 * Graph traversal
237 */
238
239static struct media_entity *
240media_entity_other(struct media_entity *entity, struct media_link *link)
241{
242 if (link->source->entity == entity)
243 return link->sink->entity;
244 else
245 return link->source->entity;
246}
247
248/* push an entity to traversal stack */
20b85227 249static void stack_push(struct media_graph *graph,
a5ccc48a
SA
250 struct media_entity *entity)
251{
252 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
253 WARN_ON(1);
254 return;
255 }
256 graph->top++;
313895fb 257 graph->stack[graph->top].link = entity->links.next;
a5ccc48a
SA
258 graph->stack[graph->top].entity = entity;
259}
260
20b85227 261static struct media_entity *stack_pop(struct media_graph *graph)
a5ccc48a
SA
262{
263 struct media_entity *entity;
264
265 entity = graph->stack[graph->top].entity;
266 graph->top--;
267
268 return entity;
269}
270
a5ccc48a
SA
271#define link_top(en) ((en)->stack[(en)->top].link)
272#define stack_top(en) ((en)->stack[(en)->top].entity)
273
e03d2203 274/**
20b85227 275 * media_graph_walk_init - Allocate resources for graph walk
e03d2203
SA
276 * @graph: Media graph structure that will be used to walk the graph
277 * @mdev: Media device
278 *
279 * Reserve resources for graph walk in media device's current
280 * state. The memory must be released using
20b85227 281 * media_graph_walk_free().
e03d2203
SA
282 *
283 * Returns error on failure, zero on success.
284 */
20b85227
SA
285__must_check int media_graph_walk_init(
286 struct media_graph *graph, struct media_device *mdev)
e03d2203 287{
29d8da02 288 return media_entity_enum_init(&graph->ent_enum, mdev);
e03d2203 289}
20b85227 290EXPORT_SYMBOL_GPL(media_graph_walk_init);
e03d2203
SA
291
292/**
20b85227 293 * media_graph_walk_cleanup - Release resources related to graph walking
e03d2203
SA
294 * @graph: Media graph structure that was used to walk the graph
295 */
20b85227 296void media_graph_walk_cleanup(struct media_graph *graph)
e03d2203 297{
29d8da02 298 media_entity_enum_cleanup(&graph->ent_enum);
e03d2203 299}
20b85227 300EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
e03d2203 301
20b85227
SA
302void media_graph_walk_start(struct media_graph *graph,
303 struct media_entity *entity)
a5ccc48a 304{
29d8da02
SA
305 media_entity_enum_zero(&graph->ent_enum);
306 media_entity_enum_set(&graph->ent_enum, entity);
307
a5ccc48a
SA
308 graph->top = 0;
309 graph->stack[graph->top].entity = NULL;
310 stack_push(graph, entity);
aa79a84f
SA
311 dev_dbg(entity->graph_obj.mdev->dev,
312 "begin graph walk at '%s'\n", entity->name);
a5ccc48a 313}
20b85227 314EXPORT_SYMBOL_GPL(media_graph_walk_start);
a5ccc48a 315
5b1f8329
SA
316static void media_graph_walk_iter(struct media_graph *graph)
317{
318 struct media_entity *entity = stack_top(graph);
319 struct media_link *link;
320 struct media_entity *next;
321
322 link = list_entry(link_top(graph), typeof(*link), list);
323
324 /* The link is not enabled so we do not follow. */
325 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
326 link_top(graph) = link_top(graph)->next;
aa79a84f
SA
327 dev_dbg(entity->graph_obj.mdev->dev,
328 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
329 link->source->entity->name, link->source->index,
330 link->sink->entity->name, link->sink->index);
5b1f8329
SA
331 return;
332 }
333
334 /* Get the entity in the other end of the link . */
335 next = media_entity_other(entity, link);
336
337 /* Has the entity already been visited? */
338 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
339 link_top(graph) = link_top(graph)->next;
aa79a84f
SA
340 dev_dbg(entity->graph_obj.mdev->dev,
341 "walk: skipping entity '%s' (already seen)\n",
342 next->name);
5b1f8329
SA
343 return;
344 }
345
346 /* Push the new entity to stack and start over. */
347 link_top(graph) = link_top(graph)->next;
348 stack_push(graph, next);
aa79a84f
SA
349 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
350 next->name);
5b1f8329
SA
351}
352
20b85227 353struct media_entity *media_graph_walk_next(struct media_graph *graph)
a5ccc48a 354{
aa79a84f
SA
355 struct media_entity *entity;
356
a5ccc48a
SA
357 if (stack_top(graph) == NULL)
358 return NULL;
359
360 /*
361 * Depth first search. Push entity to stack and continue from
362 * top of the stack until no more entities on the level can be
363 * found.
364 */
5b1f8329
SA
365 while (link_top(graph) != &stack_top(graph)->links)
366 media_graph_walk_iter(graph);
a5ccc48a 367
aa79a84f
SA
368 entity = stack_pop(graph);
369 dev_dbg(entity->graph_obj.mdev->dev,
370 "walk: returning entity '%s'\n", entity->name);
371
372 return entity;
a5ccc48a 373}
20b85227 374EXPORT_SYMBOL_GPL(media_graph_walk_next);
a5ccc48a 375
d295c6a4
NS
376int media_entity_get_fwnode_pad(struct media_entity *entity,
377 struct fwnode_handle *fwnode,
378 unsigned long direction_flags)
379{
380 struct fwnode_endpoint endpoint;
381 unsigned int i;
382 int ret;
383
384 if (!entity->ops || !entity->ops->get_fwnode_pad) {
385 for (i = 0; i < entity->num_pads; i++) {
386 if (entity->pads[i].flags & direction_flags)
387 return i;
388 }
389
390 return -ENXIO;
391 }
392
393 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
394 if (ret)
395 return ret;
396
397 ret = entity->ops->get_fwnode_pad(&endpoint);
398 if (ret < 0)
399 return ret;
400
401 if (ret >= entity->num_pads)
402 return -ENXIO;
403
404 if (!(entity->pads[ret].flags & direction_flags))
405 return -ENXIO;
406
407 return ret;
408}
409EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
410
e02188c9
LP
411/* -----------------------------------------------------------------------------
412 * Pipeline management
413 */
414
20b85227
SA
415__must_check int __media_pipeline_start(struct media_entity *entity,
416 struct media_pipeline *pipe)
e02188c9 417{
d10c9894 418 struct media_device *mdev = entity->graph_obj.mdev;
20b85227 419 struct media_graph *graph = &pipe->graph;
af88be38 420 struct media_entity *entity_err = entity;
57208e5e 421 struct media_link *link;
af88be38 422 int ret;
e02188c9 423
74a41330 424 if (!pipe->streaming_count++) {
20b85227 425 ret = media_graph_walk_init(&pipe->graph, mdev);
74a41330
SA
426 if (ret)
427 goto error_graph_walk_start;
106b9907
SA
428 }
429
20b85227 430 media_graph_walk_start(&pipe->graph, entity);
e02188c9 431
20b85227 432 while ((entity = media_graph_walk_next(graph))) {
ef69ee1b
MCC
433 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
434 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
af88be38 435
e02188c9 436 entity->stream_count++;
8aaf62b5 437
3eb87773
SK
438 if (entity->pipe && entity->pipe != pipe) {
439 pr_err("Pipe active for %s. Can't start for %s\n",
440 entity->name,
441 entity_err->name);
8aaf62b5
SA
442 ret = -EBUSY;
443 goto error;
444 }
445
e02188c9 446 entity->pipe = pipe;
af88be38
SA
447
448 /* Already streaming --- no need to check. */
449 if (entity->stream_count > 1)
450 continue;
451
452 if (!entity->ops || !entity->ops->link_validate)
453 continue;
454
de49c285
SA
455 bitmap_zero(active, entity->num_pads);
456 bitmap_fill(has_no_links, entity->num_pads);
457
57208e5e 458 list_for_each_entry(link, &entity->links, list) {
de49c285
SA
459 struct media_pad *pad = link->sink->entity == entity
460 ? link->sink : link->source;
461
462 /* Mark that a pad is connected by a link. */
463 bitmap_clear(has_no_links, pad->index, 1);
464
465 /*
466 * Pads that either do not need to connect or
467 * are connected through an enabled link are
468 * fine.
469 */
470 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
471 link->flags & MEDIA_LNK_FL_ENABLED)
472 bitmap_set(active, pad->index, 1);
473
474 /*
475 * Link validation will only take place for
476 * sink ends of the link that are enabled.
477 */
478 if (link->sink != pad ||
479 !(link->flags & MEDIA_LNK_FL_ENABLED))
af88be38
SA
480 continue;
481
482 ret = entity->ops->link_validate(link);
fab9d30b 483 if (ret < 0 && ret != -ENOIOCTLCMD) {
d10c9894 484 dev_dbg(entity->graph_obj.mdev->dev,
91b619ad 485 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
823ea2a6
SA
486 link->source->entity->name,
487 link->source->index,
488 entity->name, link->sink->index, ret);
af88be38 489 goto error;
fab9d30b 490 }
af88be38 491 }
de49c285
SA
492
493 /* Either no links or validated links are fine. */
494 bitmap_or(active, active, has_no_links, entity->num_pads);
495
496 if (!bitmap_full(active, entity->num_pads)) {
47dfdb3a 497 ret = -ENOLINK;
d10c9894 498 dev_dbg(entity->graph_obj.mdev->dev,
91b619ad 499 "'%s':%u must be connected by an enabled link\n",
fab9d30b 500 entity->name,
094f1ca5
SA
501 (unsigned)find_first_zero_bit(
502 active, entity->num_pads));
de49c285
SA
503 goto error;
504 }
e02188c9
LP
505 }
506
af88be38
SA
507 return 0;
508
509error:
510 /*
511 * Link validation on graph failed. We revert what we did and
512 * return the error.
513 */
20b85227 514 media_graph_walk_start(graph, entity_err);
af88be38 515
20b85227 516 while ((entity_err = media_graph_walk_next(graph))) {
12030f48
SA
517 /* Sanity check for negative stream_count */
518 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
3801bc7d
SK
519 entity_err->stream_count--;
520 if (entity_err->stream_count == 0)
521 entity_err->pipe = NULL;
522 }
af88be38
SA
523
524 /*
525 * We haven't increased stream_count further than this
526 * so we quit here.
527 */
528 if (entity_err == entity)
529 break;
530 }
531
74a41330
SA
532error_graph_walk_start:
533 if (!--pipe->streaming_count)
20b85227 534 media_graph_walk_cleanup(graph);
106b9907 535
fb49f204
SK
536 return ret;
537}
20b85227 538EXPORT_SYMBOL_GPL(__media_pipeline_start);
af88be38 539
20b85227
SA
540__must_check int media_pipeline_start(struct media_entity *entity,
541 struct media_pipeline *pipe)
fb49f204
SK
542{
543 struct media_device *mdev = entity->graph_obj.mdev;
544 int ret;
545
546 mutex_lock(&mdev->graph_mutex);
20b85227 547 ret = __media_pipeline_start(entity, pipe);
fb49f204 548 mutex_unlock(&mdev->graph_mutex);
af88be38 549 return ret;
e02188c9 550}
20b85227 551EXPORT_SYMBOL_GPL(media_pipeline_start);
e02188c9 552
20b85227 553void __media_pipeline_stop(struct media_entity *entity)
e02188c9 554{
20b85227 555 struct media_graph *graph = &entity->pipe->graph;
74a41330 556 struct media_pipeline *pipe = entity->pipe;
e02188c9 557
2a2599c6
KB
558 /*
559 * If the following check fails, the driver has performed an
560 * unbalanced call to media_pipeline_stop()
561 */
562 if (WARN_ON(!pipe))
563 return;
e02188c9 564
20b85227 565 media_graph_walk_start(graph, entity);
e02188c9 566
20b85227 567 while ((entity = media_graph_walk_next(graph))) {
12030f48
SA
568 /* Sanity check for negative stream_count */
569 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
3801bc7d
SK
570 entity->stream_count--;
571 if (entity->stream_count == 0)
572 entity->pipe = NULL;
573 }
e02188c9
LP
574 }
575
74a41330 576 if (!--pipe->streaming_count)
20b85227 577 media_graph_walk_cleanup(graph);
106b9907 578
fb49f204 579}
20b85227 580EXPORT_SYMBOL_GPL(__media_pipeline_stop);
fb49f204 581
20b85227 582void media_pipeline_stop(struct media_entity *entity)
fb49f204
SK
583{
584 struct media_device *mdev = entity->graph_obj.mdev;
585
586 mutex_lock(&mdev->graph_mutex);
20b85227 587 __media_pipeline_stop(entity);
e02188c9
LP
588 mutex_unlock(&mdev->graph_mutex);
589}
20b85227 590EXPORT_SYMBOL_GPL(media_pipeline_stop);
e02188c9 591
a5ccc48a
SA
592/* -----------------------------------------------------------------------------
593 * Links management
594 */
595
23615de5 596static struct media_link *media_add_link(struct list_head *head)
53e269c1 597{
57208e5e 598 struct media_link *link;
53e269c1 599
57208e5e
MCC
600 link = kzalloc(sizeof(*link), GFP_KERNEL);
601 if (link == NULL)
602 return NULL;
53e269c1 603
23615de5 604 list_add_tail(&link->list, head);
53e269c1 605
57208e5e 606 return link;
53e269c1
LP
607}
608
57208e5e 609static void __media_entity_remove_link(struct media_entity *entity,
5abad22f
MCC
610 struct media_link *link)
611{
612 struct media_link *rlink, *tmp;
613 struct media_entity *remote;
5abad22f
MCC
614
615 if (link->source->entity == entity)
616 remote = link->sink->entity;
617 else
618 remote = link->source->entity;
619
620 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
58f69ee9 621 if (rlink != link->reverse)
5abad22f 622 continue;
5abad22f
MCC
623
624 if (link->source->entity == entity)
625 remote->num_backlinks--;
626
627 /* Remove the remote link */
628 list_del(&rlink->list);
c350ef83 629 media_gobj_destroy(&rlink->graph_obj);
5abad22f
MCC
630 kfree(rlink);
631
632 if (--remote->num_links == 0)
633 break;
634 }
635 list_del(&link->list);
c350ef83 636 media_gobj_destroy(&link->graph_obj);
5abad22f
MCC
637 kfree(link);
638}
57208e5e 639
9d6d20e6
MCC
640int media_get_pad_index(struct media_entity *entity, bool is_sink,
641 enum media_pad_signal_type sig_type)
642{
643 int i;
644 bool pad_is_sink;
645
646 if (!entity)
647 return -EINVAL;
648
649 for (i = 0; i < entity->num_pads; i++) {
650 if (entity->pads[i].flags == MEDIA_PAD_FL_SINK)
651 pad_is_sink = true;
652 else if (entity->pads[i].flags == MEDIA_PAD_FL_SOURCE)
653 pad_is_sink = false;
654 else
655 continue; /* This is an error! */
656
657 if (pad_is_sink != is_sink)
658 continue;
659 if (entity->pads[i].sig_type == sig_type)
660 return i;
661 }
662 return -EINVAL;
663}
664EXPORT_SYMBOL_GPL(media_get_pad_index);
665
53e269c1 666int
8df00a15 667media_create_pad_link(struct media_entity *source, u16 source_pad,
53e269c1
LP
668 struct media_entity *sink, u16 sink_pad, u32 flags)
669{
670 struct media_link *link;
671 struct media_link *backlink;
672
673 BUG_ON(source == NULL || sink == NULL);
674 BUG_ON(source_pad >= source->num_pads);
675 BUG_ON(sink_pad >= sink->num_pads);
676
23615de5 677 link = media_add_link(&source->links);
53e269c1
LP
678 if (link == NULL)
679 return -ENOMEM;
680
681 link->source = &source->pads[source_pad];
682 link->sink = &sink->pads[sink_pad];
82ae2a50 683 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
53e269c1 684
6b6a4278 685 /* Initialize graph object embedded at the new link */
c350ef83 686 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 687 &link->graph_obj);
6b6a4278 688
53e269c1
LP
689 /* Create the backlink. Backlinks are used to help graph traversal and
690 * are not reported to userspace.
691 */
23615de5 692 backlink = media_add_link(&sink->links);
53e269c1 693 if (backlink == NULL) {
57208e5e 694 __media_entity_remove_link(source, link);
53e269c1
LP
695 return -ENOMEM;
696 }
697
698 backlink->source = &source->pads[source_pad];
699 backlink->sink = &sink->pads[sink_pad];
700 backlink->flags = flags;
39d1ebc6 701 backlink->is_backlink = true;
53e269c1 702
6b6a4278 703 /* Initialize graph object embedded at the new link */
c350ef83 704 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 705 &backlink->graph_obj);
6b6a4278 706
53e269c1
LP
707 link->reverse = backlink;
708 backlink->reverse = link;
709
710 sink->num_backlinks++;
57208e5e
MCC
711 sink->num_links++;
712 source->num_links++;
53e269c1
LP
713
714 return 0;
715}
8df00a15 716EXPORT_SYMBOL_GPL(media_create_pad_link);
97548ed4 717
b01cc9ce
MCC
718int media_create_pad_links(const struct media_device *mdev,
719 const u32 source_function,
720 struct media_entity *source,
721 const u16 source_pad,
722 const u32 sink_function,
723 struct media_entity *sink,
724 const u16 sink_pad,
725 u32 flags,
726 const bool allow_both_undefined)
727{
728 struct media_entity *entity;
729 unsigned function;
730 int ret;
731
732 /* Trivial case: 1:1 relation */
733 if (source && sink)
734 return media_create_pad_link(source, source_pad,
735 sink, sink_pad, flags);
736
737 /* Worse case scenario: n:n relation */
738 if (!source && !sink) {
739 if (!allow_both_undefined)
740 return 0;
741 media_device_for_each_entity(source, mdev) {
742 if (source->function != source_function)
743 continue;
744 media_device_for_each_entity(sink, mdev) {
745 if (sink->function != sink_function)
746 continue;
747 ret = media_create_pad_link(source, source_pad,
748 sink, sink_pad,
749 flags);
750 if (ret)
751 return ret;
752 flags &= ~(MEDIA_LNK_FL_ENABLED |
753 MEDIA_LNK_FL_IMMUTABLE);
754 }
755 }
756 return 0;
757 }
758
759 /* Handle 1:n and n:1 cases */
760 if (source)
761 function = sink_function;
762 else
763 function = source_function;
764
765 media_device_for_each_entity(entity, mdev) {
766 if (entity->function != function)
767 continue;
768
769 if (source)
770 ret = media_create_pad_link(source, source_pad,
771 entity, sink_pad, flags);
772 else
773 ret = media_create_pad_link(entity, source_pad,
774 sink, sink_pad, flags);
775 if (ret)
776 return ret;
777 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
778 }
779 return 0;
780}
781EXPORT_SYMBOL_GPL(media_create_pad_links);
782
57208e5e
MCC
783void __media_entity_remove_links(struct media_entity *entity)
784{
785 struct media_link *link, *tmp;
7349cec1 786
57208e5e
MCC
787 list_for_each_entry_safe(link, tmp, &entity->links, list)
788 __media_entity_remove_link(entity, link);
7349cec1
SN
789
790 entity->num_links = 0;
791 entity->num_backlinks = 0;
792}
793EXPORT_SYMBOL_GPL(__media_entity_remove_links);
794
795void media_entity_remove_links(struct media_entity *entity)
796{
cc4a8258
MCC
797 struct media_device *mdev = entity->graph_obj.mdev;
798
7349cec1 799 /* Do nothing if the entity is not registered. */
cc4a8258 800 if (mdev == NULL)
7349cec1
SN
801 return;
802
e2c91d4d 803 mutex_lock(&mdev->graph_mutex);
7349cec1 804 __media_entity_remove_links(entity);
e2c91d4d 805 mutex_unlock(&mdev->graph_mutex);
7349cec1
SN
806}
807EXPORT_SYMBOL_GPL(media_entity_remove_links);
808
97548ed4
LP
809static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
810{
97548ed4
LP
811 int ret;
812
813 /* Notify both entities. */
814 ret = media_entity_call(link->source->entity, link_setup,
815 link->source, link->sink, flags);
816 if (ret < 0 && ret != -ENOIOCTLCMD)
817 return ret;
818
819 ret = media_entity_call(link->sink->entity, link_setup,
820 link->sink, link->source, flags);
821 if (ret < 0 && ret != -ENOIOCTLCMD) {
822 media_entity_call(link->source->entity, link_setup,
823 link->source, link->sink, link->flags);
824 return ret;
825 }
826
7a6f0b22 827 link->flags = flags;
97548ed4
LP
828 link->reverse->flags = link->flags;
829
830 return 0;
831}
832
97548ed4
LP
833int __media_entity_setup_link(struct media_link *link, u32 flags)
834{
7a6f0b22 835 const u32 mask = MEDIA_LNK_FL_ENABLED;
97548ed4
LP
836 struct media_device *mdev;
837 struct media_entity *source, *sink;
838 int ret = -EBUSY;
839
840 if (link == NULL)
841 return -EINVAL;
842
7a6f0b22
LP
843 /* The non-modifiable link flags must not be modified. */
844 if ((link->flags & ~mask) != (flags & ~mask))
845 return -EINVAL;
846
97548ed4
LP
847 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
848 return link->flags == flags ? 0 : -EINVAL;
849
850 if (link->flags == flags)
851 return 0;
852
853 source = link->source->entity;
854 sink = link->sink->entity;
855
e02188c9
LP
856 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
857 (source->stream_count || sink->stream_count))
858 return -EBUSY;
859
d10c9894 860 mdev = source->graph_obj.mdev;
97548ed4 861
68429f50
LP
862 if (mdev->ops && mdev->ops->link_notify) {
863 ret = mdev->ops->link_notify(link, flags,
864 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
97548ed4
LP
865 if (ret < 0)
866 return ret;
867 }
868
869 ret = __media_entity_setup_link_notify(link, flags);
97548ed4 870
68429f50
LP
871 if (mdev->ops && mdev->ops->link_notify)
872 mdev->ops->link_notify(link, flags,
873 MEDIA_DEV_NOTIFY_POST_LINK_CH);
97548ed4
LP
874
875 return ret;
876}
efc70278 877EXPORT_SYMBOL_GPL(__media_entity_setup_link);
97548ed4
LP
878
879int media_entity_setup_link(struct media_link *link, u32 flags)
880{
881 int ret;
882
5c883edb 883 mutex_lock(&link->graph_obj.mdev->graph_mutex);
97548ed4 884 ret = __media_entity_setup_link(link, flags);
5c883edb 885 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
97548ed4
LP
886
887 return ret;
888}
889EXPORT_SYMBOL_GPL(media_entity_setup_link);
890
97548ed4
LP
891struct media_link *
892media_entity_find_link(struct media_pad *source, struct media_pad *sink)
893{
894 struct media_link *link;
97548ed4 895
57208e5e 896 list_for_each_entry(link, &source->entity->links, list) {
97548ed4
LP
897 if (link->source->entity == source->entity &&
898 link->source->index == source->index &&
899 link->sink->entity == sink->entity &&
900 link->sink->index == sink->index)
901 return link;
902 }
903
904 return NULL;
905}
906EXPORT_SYMBOL_GPL(media_entity_find_link);
907
6538b02d 908struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
97548ed4 909{
57208e5e 910 struct media_link *link;
97548ed4 911
57208e5e 912 list_for_each_entry(link, &pad->entity->links, list) {
97548ed4
LP
913 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
914 continue;
915
916 if (link->source == pad)
917 return link->sink;
918
919 if (link->sink == pad)
920 return link->source;
921 }
922
923 return NULL;
924
925}
1bddf1b3 926EXPORT_SYMBOL_GPL(media_entity_remote_pad);
27e543fa 927
1283f849
MCC
928static void media_interface_init(struct media_device *mdev,
929 struct media_interface *intf,
930 u32 gobj_type,
931 u32 intf_type, u32 flags)
932{
933 intf->type = intf_type;
934 intf->flags = flags;
935 INIT_LIST_HEAD(&intf->links);
936
c350ef83 937 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1283f849
MCC
938}
939
27e543fa
MCC
940/* Functions related to the media interface via device nodes */
941
942struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
943 u32 type, u32 flags,
0b3b72df 944 u32 major, u32 minor)
27e543fa
MCC
945{
946 struct media_intf_devnode *devnode;
27e543fa 947
0b3b72df 948 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
27e543fa
MCC
949 if (!devnode)
950 return NULL;
951
27e543fa
MCC
952 devnode->major = major;
953 devnode->minor = minor;
954
1283f849
MCC
955 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
956 type, flags);
27e543fa
MCC
957
958 return devnode;
959}
960EXPORT_SYMBOL_GPL(media_devnode_create);
961
962void media_devnode_remove(struct media_intf_devnode *devnode)
963{
7c4696a9 964 media_remove_intf_links(&devnode->intf);
c350ef83 965 media_gobj_destroy(&devnode->intf.graph_obj);
27e543fa
MCC
966 kfree(devnode);
967}
968EXPORT_SYMBOL_GPL(media_devnode_remove);
86e26620
MCC
969
970struct media_link *media_create_intf_link(struct media_entity *entity,
971 struct media_interface *intf,
972 u32 flags)
973{
974 struct media_link *link;
975
976 link = media_add_link(&intf->links);
977 if (link == NULL)
978 return NULL;
979
980 link->intf = intf;
981 link->entity = entity;
82ae2a50 982 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
86e26620
MCC
983
984 /* Initialize graph object embedded at the new link */
c350ef83 985 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
86e26620
MCC
986 &link->graph_obj);
987
988 return link;
989}
990EXPORT_SYMBOL_GPL(media_create_intf_link);
991
d47109fa 992void __media_remove_intf_link(struct media_link *link)
86e26620 993{
d47109fa 994 list_del(&link->list);
c350ef83 995 media_gobj_destroy(&link->graph_obj);
86e26620
MCC
996 kfree(link);
997}
d47109fa 998EXPORT_SYMBOL_GPL(__media_remove_intf_link);
86e26620
MCC
999
1000void media_remove_intf_link(struct media_link *link)
1001{
cc4a8258
MCC
1002 struct media_device *mdev = link->graph_obj.mdev;
1003
1004 /* Do nothing if the intf is not registered. */
1005 if (mdev == NULL)
1006 return;
1007
e2c91d4d 1008 mutex_lock(&mdev->graph_mutex);
86e26620 1009 __media_remove_intf_link(link);
e2c91d4d 1010 mutex_unlock(&mdev->graph_mutex);
86e26620
MCC
1011}
1012EXPORT_SYMBOL_GPL(media_remove_intf_link);
7c4696a9
MCC
1013
1014void __media_remove_intf_links(struct media_interface *intf)
1015{
1016 struct media_link *link, *tmp;
1017
1018 list_for_each_entry_safe(link, tmp, &intf->links, list)
1019 __media_remove_intf_link(link);
1020
1021}
1022EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1023
1024void media_remove_intf_links(struct media_interface *intf)
1025{
cc4a8258
MCC
1026 struct media_device *mdev = intf->graph_obj.mdev;
1027
7c4696a9 1028 /* Do nothing if the intf is not registered. */
cc4a8258 1029 if (mdev == NULL)
7c4696a9
MCC
1030 return;
1031
e2c91d4d 1032 mutex_lock(&mdev->graph_mutex);
7c4696a9 1033 __media_remove_intf_links(intf);
e2c91d4d 1034 mutex_unlock(&mdev->graph_mutex);
7c4696a9
MCC
1035}
1036EXPORT_SYMBOL_GPL(media_remove_intf_links);