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