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