4 * Copyright (C) 2010 Nokia Corporation
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
19 #include <linux/bitmap.h>
20 #include <linux/module.h>
21 #include <linux/property.h>
22 #include <linux/slab.h>
23 #include <media/media-entity.h>
24 #include <media/media-device.h>
26 static inline const char *gobj_type(enum media_gobj_type type
)
29 case MEDIA_GRAPH_ENTITY
:
33 case MEDIA_GRAPH_LINK
:
35 case MEDIA_GRAPH_INTF_DEVNODE
:
36 return "intf-devnode";
42 static inline const char *intf_type(struct media_interface
*intf
)
45 case MEDIA_INTF_T_DVB_FE
:
46 return "dvb-frontend";
47 case MEDIA_INTF_T_DVB_DEMUX
:
49 case MEDIA_INTF_T_DVB_DVR
:
51 case MEDIA_INTF_T_DVB_CA
:
53 case MEDIA_INTF_T_DVB_NET
:
55 case MEDIA_INTF_T_V4L_VIDEO
:
57 case MEDIA_INTF_T_V4L_VBI
:
59 case MEDIA_INTF_T_V4L_RADIO
:
61 case MEDIA_INTF_T_V4L_SUBDEV
:
63 case MEDIA_INTF_T_V4L_SWRADIO
:
65 case MEDIA_INTF_T_V4L_TOUCH
:
68 return "unknown-intf";
72 __must_check
int __media_entity_enum_init(struct media_entity_enum
*ent_enum
,
75 idx_max
= ALIGN(idx_max
, BITS_PER_LONG
);
76 ent_enum
->bmap
= kcalloc(idx_max
/ BITS_PER_LONG
, sizeof(long),
81 bitmap_zero(ent_enum
->bmap
, idx_max
);
82 ent_enum
->idx_max
= idx_max
;
86 EXPORT_SYMBOL_GPL(__media_entity_enum_init
);
88 void media_entity_enum_cleanup(struct media_entity_enum
*ent_enum
)
90 kfree(ent_enum
->bmap
);
92 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup
);
95 * dev_dbg_obj - Prints in debug mode a change on some object
97 * @event_name: Name of the event to report. Could be __func__
98 * @gobj: Pointer to the object
100 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
101 * won't produce any code.
103 static void dev_dbg_obj(const char *event_name
, struct media_gobj
*gobj
)
105 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
106 switch (media_type(gobj
)) {
107 case MEDIA_GRAPH_ENTITY
:
108 dev_dbg(gobj
->mdev
->dev
,
109 "%s id %u: entity '%s'\n",
110 event_name
, media_id(gobj
),
111 gobj_to_entity(gobj
)->name
);
113 case MEDIA_GRAPH_LINK
:
115 struct media_link
*link
= gobj_to_link(gobj
);
117 dev_dbg(gobj
->mdev
->dev
,
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
));
126 case MEDIA_GRAPH_PAD
:
128 struct media_pad
*pad
= gobj_to_pad(gobj
);
130 dev_dbg(gobj
->mdev
->dev
,
131 "%s id %u: %s%spad '%s':%d\n",
132 event_name
, media_id(gobj
),
133 pad
->flags
& MEDIA_PAD_FL_SINK
? "sink " : "",
134 pad
->flags
& MEDIA_PAD_FL_SOURCE
? "source " : "",
135 pad
->entity
->name
, pad
->index
);
138 case MEDIA_GRAPH_INTF_DEVNODE
:
140 struct media_interface
*intf
= gobj_to_intf(gobj
);
141 struct media_intf_devnode
*devnode
= intf_to_devnode(intf
);
143 dev_dbg(gobj
->mdev
->dev
,
144 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
145 event_name
, media_id(gobj
),
147 devnode
->major
, devnode
->minor
);
154 void media_gobj_create(struct media_device
*mdev
,
155 enum media_gobj_type type
,
156 struct media_gobj
*gobj
)
162 /* Create a per-type unique object ID */
163 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->id
);
166 case MEDIA_GRAPH_ENTITY
:
167 list_add_tail(&gobj
->list
, &mdev
->entities
);
169 case MEDIA_GRAPH_PAD
:
170 list_add_tail(&gobj
->list
, &mdev
->pads
);
172 case MEDIA_GRAPH_LINK
:
173 list_add_tail(&gobj
->list
, &mdev
->links
);
175 case MEDIA_GRAPH_INTF_DEVNODE
:
176 list_add_tail(&gobj
->list
, &mdev
->interfaces
);
180 mdev
->topology_version
++;
182 dev_dbg_obj(__func__
, gobj
);
185 void media_gobj_destroy(struct media_gobj
*gobj
)
187 /* Do nothing if the object is not linked. */
188 if (gobj
->mdev
== NULL
)
191 dev_dbg_obj(__func__
, gobj
);
193 gobj
->mdev
->topology_version
++;
195 /* Remove the object from mdev list */
196 list_del(&gobj
->list
);
202 * TODO: Get rid of this.
204 #define MEDIA_ENTITY_MAX_PADS 512
206 int media_entity_pads_init(struct media_entity
*entity
, u16 num_pads
,
207 struct media_pad
*pads
)
209 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
212 if (num_pads
>= MEDIA_ENTITY_MAX_PADS
)
215 entity
->num_pads
= num_pads
;
219 mutex_lock(&mdev
->graph_mutex
);
221 for (i
= 0; i
< num_pads
; i
++) {
222 pads
[i
].entity
= entity
;
225 media_gobj_create(mdev
, MEDIA_GRAPH_PAD
,
226 &entity
->pads
[i
].graph_obj
);
230 mutex_unlock(&mdev
->graph_mutex
);
234 EXPORT_SYMBOL_GPL(media_entity_pads_init
);
236 /* -----------------------------------------------------------------------------
240 static struct media_entity
*
241 media_entity_other(struct media_entity
*entity
, struct media_link
*link
)
243 if (link
->source
->entity
== entity
)
244 return link
->sink
->entity
;
246 return link
->source
->entity
;
249 /* push an entity to traversal stack */
250 static void stack_push(struct media_graph
*graph
,
251 struct media_entity
*entity
)
253 if (graph
->top
== MEDIA_ENTITY_ENUM_MAX_DEPTH
- 1) {
258 graph
->stack
[graph
->top
].link
= entity
->links
.next
;
259 graph
->stack
[graph
->top
].entity
= entity
;
262 static struct media_entity
*stack_pop(struct media_graph
*graph
)
264 struct media_entity
*entity
;
266 entity
= graph
->stack
[graph
->top
].entity
;
272 #define link_top(en) ((en)->stack[(en)->top].link)
273 #define stack_top(en) ((en)->stack[(en)->top].entity)
276 * media_graph_walk_init - Allocate resources for graph walk
277 * @graph: Media graph structure that will be used to walk the graph
278 * @mdev: Media device
280 * Reserve resources for graph walk in media device's current
281 * state. The memory must be released using
282 * media_graph_walk_free().
284 * Returns error on failure, zero on success.
286 __must_check
int media_graph_walk_init(
287 struct media_graph
*graph
, struct media_device
*mdev
)
289 return media_entity_enum_init(&graph
->ent_enum
, mdev
);
291 EXPORT_SYMBOL_GPL(media_graph_walk_init
);
294 * media_graph_walk_cleanup - Release resources related to graph walking
295 * @graph: Media graph structure that was used to walk the graph
297 void media_graph_walk_cleanup(struct media_graph
*graph
)
299 media_entity_enum_cleanup(&graph
->ent_enum
);
301 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup
);
303 void media_graph_walk_start(struct media_graph
*graph
,
304 struct media_entity
*entity
)
306 media_entity_enum_zero(&graph
->ent_enum
);
307 media_entity_enum_set(&graph
->ent_enum
, entity
);
310 graph
->stack
[graph
->top
].entity
= NULL
;
311 stack_push(graph
, entity
);
312 dev_dbg(entity
->graph_obj
.mdev
->dev
,
313 "begin graph walk at '%s'\n", entity
->name
);
315 EXPORT_SYMBOL_GPL(media_graph_walk_start
);
317 static void media_graph_walk_iter(struct media_graph
*graph
)
319 struct media_entity
*entity
= stack_top(graph
);
320 struct media_link
*link
;
321 struct media_entity
*next
;
323 link
= list_entry(link_top(graph
), typeof(*link
), list
);
325 /* The link is not enabled so we do not follow. */
326 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
327 link_top(graph
) = link_top(graph
)->next
;
328 dev_dbg(entity
->graph_obj
.mdev
->dev
,
329 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
330 link
->source
->entity
->name
, link
->source
->index
,
331 link
->sink
->entity
->name
, link
->sink
->index
);
335 /* Get the entity in the other end of the link . */
336 next
= media_entity_other(entity
, link
);
338 /* Has the entity already been visited? */
339 if (media_entity_enum_test_and_set(&graph
->ent_enum
, next
)) {
340 link_top(graph
) = link_top(graph
)->next
;
341 dev_dbg(entity
->graph_obj
.mdev
->dev
,
342 "walk: skipping entity '%s' (already seen)\n",
347 /* Push the new entity to stack and start over. */
348 link_top(graph
) = link_top(graph
)->next
;
349 stack_push(graph
, next
);
350 dev_dbg(entity
->graph_obj
.mdev
->dev
, "walk: pushing '%s' on stack\n",
354 struct media_entity
*media_graph_walk_next(struct media_graph
*graph
)
356 struct media_entity
*entity
;
358 if (stack_top(graph
) == NULL
)
362 * Depth first search. Push entity to stack and continue from
363 * top of the stack until no more entities on the level can be
366 while (link_top(graph
) != &stack_top(graph
)->links
)
367 media_graph_walk_iter(graph
);
369 entity
= stack_pop(graph
);
370 dev_dbg(entity
->graph_obj
.mdev
->dev
,
371 "walk: returning entity '%s'\n", entity
->name
);
375 EXPORT_SYMBOL_GPL(media_graph_walk_next
);
377 int media_entity_get_fwnode_pad(struct media_entity
*entity
,
378 struct fwnode_handle
*fwnode
,
379 unsigned long direction_flags
)
381 struct fwnode_endpoint endpoint
;
385 if (!entity
->ops
|| !entity
->ops
->get_fwnode_pad
) {
386 for (i
= 0; i
< entity
->num_pads
; i
++) {
387 if (entity
->pads
[i
].flags
& direction_flags
)
394 ret
= fwnode_graph_parse_endpoint(fwnode
, &endpoint
);
398 ret
= entity
->ops
->get_fwnode_pad(&endpoint
);
402 if (ret
>= entity
->num_pads
)
405 if (!(entity
->pads
[ret
].flags
& direction_flags
))
410 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad
);
412 /* -----------------------------------------------------------------------------
413 * Pipeline management
416 __must_check
int __media_pipeline_start(struct media_entity
*entity
,
417 struct media_pipeline
*pipe
)
419 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
420 struct media_graph
*graph
= &pipe
->graph
;
421 struct media_entity
*entity_err
= entity
;
422 struct media_link
*link
;
425 if (!pipe
->streaming_count
++) {
426 ret
= media_graph_walk_init(&pipe
->graph
, mdev
);
428 goto error_graph_walk_start
;
431 media_graph_walk_start(&pipe
->graph
, entity
);
433 while ((entity
= media_graph_walk_next(graph
))) {
434 DECLARE_BITMAP(active
, MEDIA_ENTITY_MAX_PADS
);
435 DECLARE_BITMAP(has_no_links
, MEDIA_ENTITY_MAX_PADS
);
437 entity
->stream_count
++;
439 if (WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
)) {
446 /* Already streaming --- no need to check. */
447 if (entity
->stream_count
> 1)
450 if (!entity
->ops
|| !entity
->ops
->link_validate
)
453 bitmap_zero(active
, entity
->num_pads
);
454 bitmap_fill(has_no_links
, entity
->num_pads
);
456 list_for_each_entry(link
, &entity
->links
, list
) {
457 struct media_pad
*pad
= link
->sink
->entity
== entity
458 ? link
->sink
: link
->source
;
460 /* Mark that a pad is connected by a link. */
461 bitmap_clear(has_no_links
, pad
->index
, 1);
464 * Pads that either do not need to connect or
465 * are connected through an enabled link are
468 if (!(pad
->flags
& MEDIA_PAD_FL_MUST_CONNECT
) ||
469 link
->flags
& MEDIA_LNK_FL_ENABLED
)
470 bitmap_set(active
, pad
->index
, 1);
473 * Link validation will only take place for
474 * sink ends of the link that are enabled.
476 if (link
->sink
!= pad
||
477 !(link
->flags
& MEDIA_LNK_FL_ENABLED
))
480 ret
= entity
->ops
->link_validate(link
);
481 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
482 dev_dbg(entity
->graph_obj
.mdev
->dev
,
483 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
484 link
->source
->entity
->name
,
486 entity
->name
, link
->sink
->index
, ret
);
491 /* Either no links or validated links are fine. */
492 bitmap_or(active
, active
, has_no_links
, entity
->num_pads
);
494 if (!bitmap_full(active
, entity
->num_pads
)) {
496 dev_dbg(entity
->graph_obj
.mdev
->dev
,
497 "'%s':%u must be connected by an enabled link\n",
499 (unsigned)find_first_zero_bit(
500 active
, entity
->num_pads
));
509 * Link validation on graph failed. We revert what we did and
512 media_graph_walk_start(graph
, entity_err
);
514 while ((entity_err
= media_graph_walk_next(graph
))) {
515 /* Sanity check for negative stream_count */
516 if (!WARN_ON_ONCE(entity_err
->stream_count
<= 0)) {
517 entity_err
->stream_count
--;
518 if (entity_err
->stream_count
== 0)
519 entity_err
->pipe
= NULL
;
523 * We haven't increased stream_count further than this
526 if (entity_err
== entity
)
530 error_graph_walk_start
:
531 if (!--pipe
->streaming_count
)
532 media_graph_walk_cleanup(graph
);
536 EXPORT_SYMBOL_GPL(__media_pipeline_start
);
538 __must_check
int media_pipeline_start(struct media_entity
*entity
,
539 struct media_pipeline
*pipe
)
541 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
544 mutex_lock(&mdev
->graph_mutex
);
545 ret
= __media_pipeline_start(entity
, pipe
);
546 mutex_unlock(&mdev
->graph_mutex
);
549 EXPORT_SYMBOL_GPL(media_pipeline_start
);
551 void __media_pipeline_stop(struct media_entity
*entity
)
553 struct media_graph
*graph
= &entity
->pipe
->graph
;
554 struct media_pipeline
*pipe
= entity
->pipe
;
557 * If the following check fails, the driver has performed an
558 * unbalanced call to media_pipeline_stop()
563 media_graph_walk_start(graph
, entity
);
565 while ((entity
= media_graph_walk_next(graph
))) {
566 /* Sanity check for negative stream_count */
567 if (!WARN_ON_ONCE(entity
->stream_count
<= 0)) {
568 entity
->stream_count
--;
569 if (entity
->stream_count
== 0)
574 if (!--pipe
->streaming_count
)
575 media_graph_walk_cleanup(graph
);
578 EXPORT_SYMBOL_GPL(__media_pipeline_stop
);
580 void media_pipeline_stop(struct media_entity
*entity
)
582 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
584 mutex_lock(&mdev
->graph_mutex
);
585 __media_pipeline_stop(entity
);
586 mutex_unlock(&mdev
->graph_mutex
);
588 EXPORT_SYMBOL_GPL(media_pipeline_stop
);
590 /* -----------------------------------------------------------------------------
594 struct media_entity
*media_entity_get(struct media_entity
*entity
)
599 if (entity
->graph_obj
.mdev
->dev
&&
600 !try_module_get(entity
->graph_obj
.mdev
->dev
->driver
->owner
))
605 EXPORT_SYMBOL_GPL(media_entity_get
);
607 void media_entity_put(struct media_entity
*entity
)
612 if (entity
->graph_obj
.mdev
->dev
)
613 module_put(entity
->graph_obj
.mdev
->dev
->driver
->owner
);
615 EXPORT_SYMBOL_GPL(media_entity_put
);
617 /* -----------------------------------------------------------------------------
621 static struct media_link
*media_add_link(struct list_head
*head
)
623 struct media_link
*link
;
625 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
629 list_add_tail(&link
->list
, head
);
634 static void __media_entity_remove_link(struct media_entity
*entity
,
635 struct media_link
*link
)
637 struct media_link
*rlink
, *tmp
;
638 struct media_entity
*remote
;
640 if (link
->source
->entity
== entity
)
641 remote
= link
->sink
->entity
;
643 remote
= link
->source
->entity
;
645 list_for_each_entry_safe(rlink
, tmp
, &remote
->links
, list
) {
646 if (rlink
!= link
->reverse
)
649 if (link
->source
->entity
== entity
)
650 remote
->num_backlinks
--;
652 /* Remove the remote link */
653 list_del(&rlink
->list
);
654 media_gobj_destroy(&rlink
->graph_obj
);
657 if (--remote
->num_links
== 0)
660 list_del(&link
->list
);
661 media_gobj_destroy(&link
->graph_obj
);
665 int media_get_pad_index(struct media_entity
*entity
, bool is_sink
,
666 enum media_pad_signal_type sig_type
)
674 for (i
= 0; i
< entity
->num_pads
; i
++) {
675 if (entity
->pads
[i
].flags
== MEDIA_PAD_FL_SINK
)
677 else if (entity
->pads
[i
].flags
== MEDIA_PAD_FL_SOURCE
)
680 continue; /* This is an error! */
682 if (pad_is_sink
!= is_sink
)
684 if (entity
->pads
[i
].sig_type
== sig_type
)
689 EXPORT_SYMBOL_GPL(media_get_pad_index
);
692 media_create_pad_link(struct media_entity
*source
, u16 source_pad
,
693 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
695 struct media_link
*link
;
696 struct media_link
*backlink
;
698 BUG_ON(source
== NULL
|| sink
== NULL
);
699 BUG_ON(source_pad
>= source
->num_pads
);
700 BUG_ON(sink_pad
>= sink
->num_pads
);
702 link
= media_add_link(&source
->links
);
706 link
->source
= &source
->pads
[source_pad
];
707 link
->sink
= &sink
->pads
[sink_pad
];
708 link
->flags
= flags
& ~MEDIA_LNK_FL_INTERFACE_LINK
;
710 /* Initialize graph object embedded at the new link */
711 media_gobj_create(source
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
714 /* Create the backlink. Backlinks are used to help graph traversal and
715 * are not reported to userspace.
717 backlink
= media_add_link(&sink
->links
);
718 if (backlink
== NULL
) {
719 __media_entity_remove_link(source
, link
);
723 backlink
->source
= &source
->pads
[source_pad
];
724 backlink
->sink
= &sink
->pads
[sink_pad
];
725 backlink
->flags
= flags
;
726 backlink
->is_backlink
= true;
728 /* Initialize graph object embedded at the new link */
729 media_gobj_create(sink
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
730 &backlink
->graph_obj
);
732 link
->reverse
= backlink
;
733 backlink
->reverse
= link
;
735 sink
->num_backlinks
++;
741 EXPORT_SYMBOL_GPL(media_create_pad_link
);
743 int media_create_pad_links(const struct media_device
*mdev
,
744 const u32 source_function
,
745 struct media_entity
*source
,
746 const u16 source_pad
,
747 const u32 sink_function
,
748 struct media_entity
*sink
,
751 const bool allow_both_undefined
)
753 struct media_entity
*entity
;
757 /* Trivial case: 1:1 relation */
759 return media_create_pad_link(source
, source_pad
,
760 sink
, sink_pad
, flags
);
762 /* Worse case scenario: n:n relation */
763 if (!source
&& !sink
) {
764 if (!allow_both_undefined
)
766 media_device_for_each_entity(source
, mdev
) {
767 if (source
->function
!= source_function
)
769 media_device_for_each_entity(sink
, mdev
) {
770 if (sink
->function
!= sink_function
)
772 ret
= media_create_pad_link(source
, source_pad
,
777 flags
&= ~(MEDIA_LNK_FL_ENABLED
|
778 MEDIA_LNK_FL_IMMUTABLE
);
784 /* Handle 1:n and n:1 cases */
786 function
= sink_function
;
788 function
= source_function
;
790 media_device_for_each_entity(entity
, mdev
) {
791 if (entity
->function
!= function
)
795 ret
= media_create_pad_link(source
, source_pad
,
796 entity
, sink_pad
, flags
);
798 ret
= media_create_pad_link(entity
, source_pad
,
799 sink
, sink_pad
, flags
);
802 flags
&= ~(MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
806 EXPORT_SYMBOL_GPL(media_create_pad_links
);
808 void __media_entity_remove_links(struct media_entity
*entity
)
810 struct media_link
*link
, *tmp
;
812 list_for_each_entry_safe(link
, tmp
, &entity
->links
, list
)
813 __media_entity_remove_link(entity
, link
);
815 entity
->num_links
= 0;
816 entity
->num_backlinks
= 0;
818 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
820 void media_entity_remove_links(struct media_entity
*entity
)
822 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
824 /* Do nothing if the entity is not registered. */
828 mutex_lock(&mdev
->graph_mutex
);
829 __media_entity_remove_links(entity
);
830 mutex_unlock(&mdev
->graph_mutex
);
832 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
834 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
838 /* Notify both entities. */
839 ret
= media_entity_call(link
->source
->entity
, link_setup
,
840 link
->source
, link
->sink
, flags
);
841 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
844 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
845 link
->sink
, link
->source
, flags
);
846 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
847 media_entity_call(link
->source
->entity
, link_setup
,
848 link
->source
, link
->sink
, link
->flags
);
853 link
->reverse
->flags
= link
->flags
;
858 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
860 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
861 struct media_device
*mdev
;
862 struct media_entity
*source
, *sink
;
868 /* The non-modifiable link flags must not be modified. */
869 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
872 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
873 return link
->flags
== flags
? 0 : -EINVAL
;
875 if (link
->flags
== flags
)
878 source
= link
->source
->entity
;
879 sink
= link
->sink
->entity
;
881 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
882 (source
->stream_count
|| sink
->stream_count
))
885 mdev
= source
->graph_obj
.mdev
;
887 if (mdev
->ops
&& mdev
->ops
->link_notify
) {
888 ret
= mdev
->ops
->link_notify(link
, flags
,
889 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
894 ret
= __media_entity_setup_link_notify(link
, flags
);
896 if (mdev
->ops
&& mdev
->ops
->link_notify
)
897 mdev
->ops
->link_notify(link
, flags
,
898 MEDIA_DEV_NOTIFY_POST_LINK_CH
);
902 EXPORT_SYMBOL_GPL(__media_entity_setup_link
);
904 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
908 mutex_lock(&link
->graph_obj
.mdev
->graph_mutex
);
909 ret
= __media_entity_setup_link(link
, flags
);
910 mutex_unlock(&link
->graph_obj
.mdev
->graph_mutex
);
914 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
917 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
919 struct media_link
*link
;
921 list_for_each_entry(link
, &source
->entity
->links
, list
) {
922 if (link
->source
->entity
== source
->entity
&&
923 link
->source
->index
== source
->index
&&
924 link
->sink
->entity
== sink
->entity
&&
925 link
->sink
->index
== sink
->index
)
931 EXPORT_SYMBOL_GPL(media_entity_find_link
);
933 struct media_pad
*media_entity_remote_pad(const struct media_pad
*pad
)
935 struct media_link
*link
;
937 list_for_each_entry(link
, &pad
->entity
->links
, list
) {
938 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
941 if (link
->source
== pad
)
944 if (link
->sink
== pad
)
951 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);
953 static void media_interface_init(struct media_device
*mdev
,
954 struct media_interface
*intf
,
956 u32 intf_type
, u32 flags
)
958 intf
->type
= intf_type
;
960 INIT_LIST_HEAD(&intf
->links
);
962 media_gobj_create(mdev
, gobj_type
, &intf
->graph_obj
);
965 /* Functions related to the media interface via device nodes */
967 struct media_intf_devnode
*media_devnode_create(struct media_device
*mdev
,
969 u32 major
, u32 minor
)
971 struct media_intf_devnode
*devnode
;
973 devnode
= kzalloc(sizeof(*devnode
), GFP_KERNEL
);
977 devnode
->major
= major
;
978 devnode
->minor
= minor
;
980 media_interface_init(mdev
, &devnode
->intf
, MEDIA_GRAPH_INTF_DEVNODE
,
985 EXPORT_SYMBOL_GPL(media_devnode_create
);
987 void media_devnode_remove(struct media_intf_devnode
*devnode
)
989 media_remove_intf_links(&devnode
->intf
);
990 media_gobj_destroy(&devnode
->intf
.graph_obj
);
993 EXPORT_SYMBOL_GPL(media_devnode_remove
);
995 struct media_link
*media_create_intf_link(struct media_entity
*entity
,
996 struct media_interface
*intf
,
999 struct media_link
*link
;
1001 link
= media_add_link(&intf
->links
);
1006 link
->entity
= entity
;
1007 link
->flags
= flags
| MEDIA_LNK_FL_INTERFACE_LINK
;
1009 /* Initialize graph object embedded at the new link */
1010 media_gobj_create(intf
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
1015 EXPORT_SYMBOL_GPL(media_create_intf_link
);
1017 void __media_remove_intf_link(struct media_link
*link
)
1019 list_del(&link
->list
);
1020 media_gobj_destroy(&link
->graph_obj
);
1023 EXPORT_SYMBOL_GPL(__media_remove_intf_link
);
1025 void media_remove_intf_link(struct media_link
*link
)
1027 struct media_device
*mdev
= link
->graph_obj
.mdev
;
1029 /* Do nothing if the intf is not registered. */
1033 mutex_lock(&mdev
->graph_mutex
);
1034 __media_remove_intf_link(link
);
1035 mutex_unlock(&mdev
->graph_mutex
);
1037 EXPORT_SYMBOL_GPL(media_remove_intf_link
);
1039 void __media_remove_intf_links(struct media_interface
*intf
)
1041 struct media_link
*link
, *tmp
;
1043 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
)
1044 __media_remove_intf_link(link
);
1047 EXPORT_SYMBOL_GPL(__media_remove_intf_links
);
1049 void media_remove_intf_links(struct media_interface
*intf
)
1051 struct media_device
*mdev
= intf
->graph_obj
.mdev
;
1053 /* Do nothing if the intf is not registered. */
1057 mutex_lock(&mdev
->graph_mutex
);
1058 __media_remove_intf_links(intf
);
1059 mutex_unlock(&mdev
->graph_mutex
);
1061 EXPORT_SYMBOL_GPL(media_remove_intf_links
);