[media] media: Media Controller export non locking __media_entity_setup_link()
[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";
5af557a6
SK
68 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";
27e543fa
MCC
84 default:
85 return "unknown-intf";
86 }
87};
88
c8d54cd5
SA
89__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
90 int idx_max)
91{
f7b5dff0
SA
92 idx_max = ALIGN(idx_max, BITS_PER_LONG);
93 ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
94 GFP_KERNEL);
030e89ec
SA
95 if (!ent_enum->bmap)
96 return -ENOMEM;
c8d54cd5
SA
97
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
c8d54cd5
SA
105void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
106{
030e89ec 107 kfree(ent_enum->bmap);
c8d54cd5
SA
108}
109EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
110
1fc25d30
MCC
111/**
112 * 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 */
39a956c4
MCC
120static 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,
05b3b77c
MCC
126 "%s id %u: entity '%s'\n",
127 event_name, media_id(gobj),
39a956c4
MCC
128 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,
05b3b77c
MCC
135 "%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));
39a956c4
MCC
141 break;
142 }
143 case MEDIA_GRAPH_PAD:
144 {
145 struct media_pad *pad = gobj_to_pad(gobj);
146
147 dev_dbg(gobj->mdev->dev,
05b3b77c
MCC
148 "%s id %u: %s%spad '%s':%d\n",
149 event_name, media_id(gobj),
150 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
6c24d460 151 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
39a956c4 152 pad->entity->name, pad->index);
27e543fa
MCC
153 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,
05b3b77c
MCC
161 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
162 event_name, media_id(gobj),
27e543fa
MCC
163 intf_type(intf),
164 devnode->major, devnode->minor);
165 break;
39a956c4
MCC
166 }
167 }
168#endif
169}
170
c350ef83 171void media_gobj_create(struct media_device *mdev,
ec6e4c95
MCC
172 enum media_gobj_type type,
173 struct media_gobj *gobj)
174{
8f6d368f
MCC
175 BUG_ON(!mdev);
176
39a956c4
MCC
177 gobj->mdev = mdev;
178
bfab2aac 179 /* Create a per-type unique object ID */
05b3b77c
MCC
180 gobj->id = media_gobj_gen_id(type, ++mdev->id);
181
bfab2aac
MCC
182 switch (type) {
183 case MEDIA_GRAPH_ENTITY:
05bfa9fa 184 list_add_tail(&gobj->list, &mdev->entities);
bfab2aac 185 break;
18710dc6 186 case MEDIA_GRAPH_PAD:
9155d859 187 list_add_tail(&gobj->list, &mdev->pads);
18710dc6 188 break;
6b6a4278 189 case MEDIA_GRAPH_LINK:
9155d859 190 list_add_tail(&gobj->list, &mdev->links);
6b6a4278 191 break;
27e543fa 192 case MEDIA_GRAPH_INTF_DEVNODE:
9155d859 193 list_add_tail(&gobj->list, &mdev->interfaces);
27e543fa 194 break;
bfab2aac 195 }
2521fdac
MCC
196
197 mdev->topology_version++;
198
39a956c4 199 dev_dbg_obj(__func__, gobj);
ec6e4c95
MCC
200}
201
c350ef83 202void media_gobj_destroy(struct media_gobj *gobj)
ec6e4c95 203{
39a956c4 204 dev_dbg_obj(__func__, gobj);
9155d859 205
2521fdac
MCC
206 gobj->mdev->topology_version++;
207
9155d859
MCC
208 /* Remove the object from mdev list */
209 list_del(&gobj->list);
ec6e4c95
MCC
210}
211
1fc25d30
MCC
212int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
213 struct media_pad *pads)
53e269c1 214{
db141a35 215 struct media_device *mdev = entity->graph_obj.mdev;
53e269c1
LP
216 unsigned int i;
217
53e269c1
LP
218 entity->num_pads = num_pads;
219 entity->pads = pads;
53e269c1 220
db141a35
JMC
221 if (mdev)
222 spin_lock(&mdev->lock);
223
53e269c1
LP
224 for (i = 0; i < num_pads; i++) {
225 pads[i].entity = entity;
226 pads[i].index = i;
db141a35 227 if (mdev)
c350ef83 228 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
db141a35 229 &entity->pads[i].graph_obj);
53e269c1
LP
230 }
231
db141a35
JMC
232 if (mdev)
233 spin_unlock(&mdev->lock);
234
53e269c1
LP
235 return 0;
236}
ab22e77c 237EXPORT_SYMBOL_GPL(media_entity_pads_init);
53e269c1 238
a5ccc48a
SA
239/* -----------------------------------------------------------------------------
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++;
313895fb 261 graph->stack[graph->top].link = entity->links.next;
a5ccc48a
SA
262 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
a5ccc48a
SA
275#define link_top(en) ((en)->stack[(en)->top].link)
276#define stack_top(en) ((en)->stack[(en)->top].entity)
277
0798ce4a
SA
278/*
279 * TODO: Get rid of this.
280 */
430a672c 281#define MEDIA_ENTITY_MAX_PADS 512
0798ce4a 282
e03d2203
SA
283/**
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{
29d8da02 297 return media_entity_enum_init(&graph->ent_enum, mdev);
e03d2203
SA
298}
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{
29d8da02 307 media_entity_enum_cleanup(&graph->ent_enum);
e03d2203
SA
308}
309EXPORT_SYMBOL_GPL(media_entity_graph_walk_cleanup);
310
a5ccc48a
SA
311void media_entity_graph_walk_start(struct media_entity_graph *graph,
312 struct media_entity *entity)
313{
29d8da02
SA
314 media_entity_enum_zero(&graph->ent_enum);
315 media_entity_enum_set(&graph->ent_enum, entity);
316
a5ccc48a
SA
317 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
a5ccc48a
SA
323struct 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 */
313895fb 334 while (link_top(graph) != &stack_top(graph)->links) {
a5ccc48a 335 struct media_entity *entity = stack_top(graph);
57208e5e 336 struct media_link *link;
a5ccc48a
SA
337 struct media_entity *next;
338
57208e5e
MCC
339 link = list_entry(link_top(graph), typeof(*link), list);
340
a5ccc48a
SA
341 /* The link is not enabled so we do not follow. */
342 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
57208e5e 343 link_top(graph) = link_top(graph)->next;
a5ccc48a
SA
344 continue;
345 }
346
347 /* Get the entity in the other end of the link . */
348 next = media_entity_other(entity, link);
349
5c7b25b9 350 /* Has the entity already been visited? */
29d8da02 351 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
57208e5e 352 link_top(graph) = link_top(graph)->next;
a5ccc48a
SA
353 continue;
354 }
355
356 /* Push the new entity to stack and start over. */
57208e5e 357 link_top(graph) = link_top(graph)->next;
a5ccc48a
SA
358 stack_push(graph, next);
359 }
360
361 return stack_pop(graph);
362}
363EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
364
e02188c9
LP
365/* -----------------------------------------------------------------------------
366 * Pipeline management
367 */
368
af88be38
SA
369__must_check int media_entity_pipeline_start(struct media_entity *entity,
370 struct media_pipeline *pipe)
e02188c9 371{
d10c9894 372 struct media_device *mdev = entity->graph_obj.mdev;
5dd8775d 373 struct media_entity_graph *graph = &pipe->graph;
af88be38 374 struct media_entity *entity_err = entity;
57208e5e 375 struct media_link *link;
af88be38 376 int ret;
e02188c9
LP
377
378 mutex_lock(&mdev->graph_mutex);
379
74a41330
SA
380 if (!pipe->streaming_count++) {
381 ret = media_entity_graph_walk_init(&pipe->graph, mdev);
382 if (ret)
383 goto error_graph_walk_start;
106b9907
SA
384 }
385
386 media_entity_graph_walk_start(&pipe->graph, entity);
e02188c9 387
5dd8775d 388 while ((entity = media_entity_graph_walk_next(graph))) {
ef69ee1b
MCC
389 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
390 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
af88be38 391
e02188c9 392 entity->stream_count++;
8aaf62b5
SA
393
394 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
395 ret = -EBUSY;
396 goto error;
397 }
398
e02188c9 399 entity->pipe = pipe;
af88be38
SA
400
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
de49c285
SA
408 bitmap_zero(active, entity->num_pads);
409 bitmap_fill(has_no_links, entity->num_pads);
410
57208e5e 411 list_for_each_entry(link, &entity->links, list) {
de49c285
SA
412 struct media_pad *pad = link->sink->entity == entity
413 ? link->sink : link->source;
414
415 /* Mark that a pad is connected by a link. */
416 bitmap_clear(has_no_links, pad->index, 1);
417
418 /*
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))
af88be38
SA
433 continue;
434
435 ret = entity->ops->link_validate(link);
fab9d30b 436 if (ret < 0 && ret != -ENOIOCTLCMD) {
d10c9894 437 dev_dbg(entity->graph_obj.mdev->dev,
fab9d30b 438 "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
823ea2a6
SA
439 link->source->entity->name,
440 link->source->index,
441 entity->name, link->sink->index, ret);
af88be38 442 goto error;
fab9d30b 443 }
af88be38 444 }
de49c285
SA
445
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;
d10c9894 451 dev_dbg(entity->graph_obj.mdev->dev,
fab9d30b
SA
452 "\"%s\":%u must be connected by an enabled link\n",
453 entity->name,
094f1ca5
SA
454 (unsigned)find_first_zero_bit(
455 active, entity->num_pads));
de49c285
SA
456 goto error;
457 }
e02188c9
LP
458 }
459
460 mutex_unlock(&mdev->graph_mutex);
af88be38
SA
461
462 return 0;
463
464error:
465 /*
466 * Link validation on graph failed. We revert what we did and
467 * return the error.
468 */
5dd8775d 469 media_entity_graph_walk_start(graph, entity_err);
af88be38 470
5dd8775d 471 while ((entity_err = media_entity_graph_walk_next(graph))) {
3801bc7d
SK
472 /* 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 }
af88be38
SA
478
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
74a41330
SA
487error_graph_walk_start:
488 if (!--pipe->streaming_count)
489 media_entity_graph_walk_cleanup(graph);
106b9907 490
af88be38
SA
491 mutex_unlock(&mdev->graph_mutex);
492
493 return ret;
e02188c9
LP
494}
495EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
496
e02188c9
LP
497void media_entity_pipeline_stop(struct media_entity *entity)
498{
d10c9894 499 struct media_device *mdev = entity->graph_obj.mdev;
5dd8775d 500 struct media_entity_graph *graph = &entity->pipe->graph;
74a41330 501 struct media_pipeline *pipe = entity->pipe;
e02188c9
LP
502
503 mutex_lock(&mdev->graph_mutex);
504
74a41330 505 WARN_ON(!pipe->streaming_count);
5dd8775d 506 media_entity_graph_walk_start(graph, entity);
e02188c9 507
5dd8775d 508 while ((entity = media_entity_graph_walk_next(graph))) {
3801bc7d
SK
509 /* 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 }
e02188c9
LP
515 }
516
74a41330
SA
517 if (!--pipe->streaming_count)
518 media_entity_graph_walk_cleanup(graph);
106b9907 519
e02188c9
LP
520 mutex_unlock(&mdev->graph_mutex);
521}
522EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
523
503c3d82
LP
524/* -----------------------------------------------------------------------------
525 * Module use count
526 */
527
503c3d82
LP
528struct media_entity *media_entity_get(struct media_entity *entity)
529{
530 if (entity == NULL)
531 return NULL;
532
d10c9894
JMC
533 if (entity->graph_obj.mdev->dev &&
534 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
503c3d82
LP
535 return NULL;
536
537 return entity;
538}
539EXPORT_SYMBOL_GPL(media_entity_get);
540
503c3d82
LP
541void media_entity_put(struct media_entity *entity)
542{
543 if (entity == NULL)
544 return;
545
d10c9894
JMC
546 if (entity->graph_obj.mdev->dev)
547 module_put(entity->graph_obj.mdev->dev->driver->owner);
503c3d82
LP
548}
549EXPORT_SYMBOL_GPL(media_entity_put);
550
a5ccc48a
SA
551/* -----------------------------------------------------------------------------
552 * Links management
553 */
554
23615de5 555static struct media_link *media_add_link(struct list_head *head)
53e269c1 556{
57208e5e 557 struct media_link *link;
53e269c1 558
57208e5e
MCC
559 link = kzalloc(sizeof(*link), GFP_KERNEL);
560 if (link == NULL)
561 return NULL;
53e269c1 562
23615de5 563 list_add_tail(&link->list, head);
53e269c1 564
57208e5e 565 return link;
53e269c1
LP
566}
567
57208e5e 568static void __media_entity_remove_link(struct media_entity *entity,
5abad22f
MCC
569 struct media_link *link)
570{
571 struct media_link *rlink, *tmp;
572 struct media_entity *remote;
5abad22f
MCC
573
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) {
58f69ee9 580 if (rlink != link->reverse)
5abad22f 581 continue;
5abad22f
MCC
582
583 if (link->source->entity == entity)
584 remote->num_backlinks--;
585
586 /* Remove the remote link */
587 list_del(&rlink->list);
c350ef83 588 media_gobj_destroy(&rlink->graph_obj);
5abad22f
MCC
589 kfree(rlink);
590
591 if (--remote->num_links == 0)
592 break;
593 }
594 list_del(&link->list);
c350ef83 595 media_gobj_destroy(&link->graph_obj);
5abad22f
MCC
596 kfree(link);
597}
57208e5e 598
53e269c1 599int
8df00a15 600media_create_pad_link(struct media_entity *source, u16 source_pad,
53e269c1
LP
601 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
23615de5 610 link = media_add_link(&source->links);
53e269c1
LP
611 if (link == NULL)
612 return -ENOMEM;
613
614 link->source = &source->pads[source_pad];
615 link->sink = &sink->pads[sink_pad];
82ae2a50 616 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
53e269c1 617
6b6a4278 618 /* Initialize graph object embedded at the new link */
c350ef83 619 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 620 &link->graph_obj);
6b6a4278 621
53e269c1
LP
622 /* Create the backlink. Backlinks are used to help graph traversal and
623 * are not reported to userspace.
624 */
23615de5 625 backlink = media_add_link(&sink->links);
53e269c1 626 if (backlink == NULL) {
57208e5e 627 __media_entity_remove_link(source, link);
53e269c1
LP
628 return -ENOMEM;
629 }
630
631 backlink->source = &source->pads[source_pad];
632 backlink->sink = &sink->pads[sink_pad];
633 backlink->flags = flags;
39d1ebc6 634 backlink->is_backlink = true;
53e269c1 635
6b6a4278 636 /* Initialize graph object embedded at the new link */
c350ef83 637 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 638 &backlink->graph_obj);
6b6a4278 639
53e269c1
LP
640 link->reverse = backlink;
641 backlink->reverse = link;
642
643 sink->num_backlinks++;
57208e5e
MCC
644 sink->num_links++;
645 source->num_links++;
53e269c1
LP
646
647 return 0;
648}
8df00a15 649EXPORT_SYMBOL_GPL(media_create_pad_link);
97548ed4 650
b01cc9ce
MCC
651int 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
57208e5e
MCC
716void __media_entity_remove_links(struct media_entity *entity)
717{
718 struct media_link *link, *tmp;
7349cec1 719
57208e5e
MCC
720 list_for_each_entry_safe(link, tmp, &entity->links, list)
721 __media_entity_remove_link(entity, link);
7349cec1
SN
722
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{
cc4a8258
MCC
730 struct media_device *mdev = entity->graph_obj.mdev;
731
7349cec1 732 /* Do nothing if the entity is not registered. */
cc4a8258 733 if (mdev == NULL)
7349cec1
SN
734 return;
735
cc4a8258 736 spin_lock(&mdev->lock);
7349cec1 737 __media_entity_remove_links(entity);
cc4a8258 738 spin_unlock(&mdev->lock);
7349cec1
SN
739}
740EXPORT_SYMBOL_GPL(media_entity_remove_links);
741
97548ed4
LP
742static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
743{
97548ed4
LP
744 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
7a6f0b22 760 link->flags = flags;
97548ed4
LP
761 link->reverse->flags = link->flags;
762
763 return 0;
764}
765
97548ed4
LP
766int __media_entity_setup_link(struct media_link *link, u32 flags)
767{
7a6f0b22 768 const u32 mask = MEDIA_LNK_FL_ENABLED;
97548ed4
LP
769 struct media_device *mdev;
770 struct media_entity *source, *sink;
771 int ret = -EBUSY;
772
773 if (link == NULL)
774 return -EINVAL;
775
7a6f0b22
LP
776 /* The non-modifiable link flags must not be modified. */
777 if ((link->flags & ~mask) != (flags & ~mask))
778 return -EINVAL;
779
97548ed4
LP
780 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
e02188c9
LP
789 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
790 (source->stream_count || sink->stream_count))
791 return -EBUSY;
792
d10c9894 793 mdev = source->graph_obj.mdev;
97548ed4 794
813f5c0a
SN
795 if (mdev->link_notify) {
796 ret = mdev->link_notify(link, flags,
797 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
97548ed4
LP
798 if (ret < 0)
799 return ret;
800 }
801
802 ret = __media_entity_setup_link_notify(link, flags);
97548ed4 803
813f5c0a
SN
804 if (mdev->link_notify)
805 mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
97548ed4
LP
806
807 return ret;
808}
efc70278 809EXPORT_SYMBOL_GPL(__media_entity_setup_link);
97548ed4
LP
810
811int media_entity_setup_link(struct media_link *link, u32 flags)
812{
813 int ret;
814
5c883edb 815 mutex_lock(&link->graph_obj.mdev->graph_mutex);
97548ed4 816 ret = __media_entity_setup_link(link, flags);
5c883edb 817 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
97548ed4
LP
818
819 return ret;
820}
821EXPORT_SYMBOL_GPL(media_entity_setup_link);
822
97548ed4
LP
823struct media_link *
824media_entity_find_link(struct media_pad *source, struct media_pad *sink)
825{
826 struct media_link *link;
97548ed4 827
57208e5e 828 list_for_each_entry(link, &source->entity->links, list) {
97548ed4
LP
829 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
1bddf1b3 840struct media_pad *media_entity_remote_pad(struct media_pad *pad)
97548ed4 841{
57208e5e 842 struct media_link *link;
97548ed4 843
57208e5e 844 list_for_each_entry(link, &pad->entity->links, list) {
97548ed4
LP
845 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}
1bddf1b3 858EXPORT_SYMBOL_GPL(media_entity_remote_pad);
27e543fa 859
1283f849
MCC
860static 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
c350ef83 869 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1283f849
MCC
870}
871
27e543fa
MCC
872/* 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,
0b3b72df 876 u32 major, u32 minor)
27e543fa
MCC
877{
878 struct media_intf_devnode *devnode;
27e543fa 879
0b3b72df 880 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
27e543fa
MCC
881 if (!devnode)
882 return NULL;
883
27e543fa
MCC
884 devnode->major = major;
885 devnode->minor = minor;
886
1283f849
MCC
887 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
888 type, flags);
27e543fa
MCC
889
890 return devnode;
891}
892EXPORT_SYMBOL_GPL(media_devnode_create);
893
894void media_devnode_remove(struct media_intf_devnode *devnode)
895{
7c4696a9 896 media_remove_intf_links(&devnode->intf);
c350ef83 897 media_gobj_destroy(&devnode->intf.graph_obj);
27e543fa
MCC
898 kfree(devnode);
899}
900EXPORT_SYMBOL_GPL(media_devnode_remove);
86e26620
MCC
901
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;
82ae2a50 914 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
86e26620
MCC
915
916 /* Initialize graph object embedded at the new link */
c350ef83 917 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
86e26620
MCC
918 &link->graph_obj);
919
920 return link;
921}
922EXPORT_SYMBOL_GPL(media_create_intf_link);
923
d47109fa 924void __media_remove_intf_link(struct media_link *link)
86e26620 925{
d47109fa 926 list_del(&link->list);
c350ef83 927 media_gobj_destroy(&link->graph_obj);
86e26620
MCC
928 kfree(link);
929}
d47109fa 930EXPORT_SYMBOL_GPL(__media_remove_intf_link);
86e26620
MCC
931
932void media_remove_intf_link(struct media_link *link)
933{
cc4a8258
MCC
934 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);
86e26620 941 __media_remove_intf_link(link);
cc4a8258 942 spin_unlock(&mdev->lock);
86e26620
MCC
943}
944EXPORT_SYMBOL_GPL(media_remove_intf_link);
7c4696a9
MCC
945
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{
cc4a8258
MCC
958 struct media_device *mdev = intf->graph_obj.mdev;
959
7c4696a9 960 /* Do nothing if the intf is not registered. */
cc4a8258 961 if (mdev == NULL)
7c4696a9
MCC
962 return;
963
cc4a8258 964 spin_lock(&mdev->lock);
7c4696a9 965 __media_remove_intf_links(intf);
cc4a8258 966 spin_unlock(&mdev->lock);
7c4696a9
MCC
967}
968EXPORT_SYMBOL_GPL(media_remove_intf_links);
This page took 0.364057 seconds and 5 git commands to generate.