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
:
67 case MEDIA_INTF_T_ALSA_PCM_CAPTURE
:
68 return "alsa-pcm-capture";
69 case MEDIA_INTF_T_ALSA_PCM_PLAYBACK
:
70 return "alsa-pcm-playback";
71 case MEDIA_INTF_T_ALSA_CONTROL
:
72 return "alsa-control";
73 case MEDIA_INTF_T_ALSA_COMPRESS
:
74 return "alsa-compress";
75 case MEDIA_INTF_T_ALSA_RAWMIDI
:
76 return "alsa-rawmidi";
77 case MEDIA_INTF_T_ALSA_HWDEP
:
79 case MEDIA_INTF_T_ALSA_SEQUENCER
:
80 return "alsa-sequencer";
81 case MEDIA_INTF_T_ALSA_TIMER
:
84 return "unknown-intf";
88 __must_check
int __media_entity_enum_init(struct media_entity_enum
*ent_enum
,
91 idx_max
= ALIGN(idx_max
, BITS_PER_LONG
);
92 ent_enum
->bmap
= kcalloc(idx_max
/ BITS_PER_LONG
, sizeof(long),
97 bitmap_zero(ent_enum
->bmap
, idx_max
);
98 ent_enum
->idx_max
= idx_max
;
102 EXPORT_SYMBOL_GPL(__media_entity_enum_init
);
104 void media_entity_enum_cleanup(struct media_entity_enum
*ent_enum
)
106 kfree(ent_enum
->bmap
);
108 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup
);
111 * dev_dbg_obj - Prints in debug mode a change on some object
113 * @event_name: Name of the event to report. Could be __func__
114 * @gobj: Pointer to the object
116 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
117 * won't produce any code.
119 static void dev_dbg_obj(const char *event_name
, struct media_gobj
*gobj
)
121 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
122 switch (media_type(gobj
)) {
123 case MEDIA_GRAPH_ENTITY
:
124 dev_dbg(gobj
->mdev
->dev
,
125 "%s id %u: entity '%s'\n",
126 event_name
, media_id(gobj
),
127 gobj_to_entity(gobj
)->name
);
129 case MEDIA_GRAPH_LINK
:
131 struct media_link
*link
= gobj_to_link(gobj
);
133 dev_dbg(gobj
->mdev
->dev
,
134 "%s id %u: %s link id %u ==> id %u\n",
135 event_name
, media_id(gobj
),
136 media_type(link
->gobj0
) == MEDIA_GRAPH_PAD
?
137 "data" : "interface",
138 media_id(link
->gobj0
),
139 media_id(link
->gobj1
));
142 case MEDIA_GRAPH_PAD
:
144 struct media_pad
*pad
= gobj_to_pad(gobj
);
146 dev_dbg(gobj
->mdev
->dev
,
147 "%s id %u: %s%spad '%s':%d\n",
148 event_name
, media_id(gobj
),
149 pad
->flags
& MEDIA_PAD_FL_SINK
? "sink " : "",
150 pad
->flags
& MEDIA_PAD_FL_SOURCE
? "source " : "",
151 pad
->entity
->name
, pad
->index
);
154 case MEDIA_GRAPH_INTF_DEVNODE
:
156 struct media_interface
*intf
= gobj_to_intf(gobj
);
157 struct media_intf_devnode
*devnode
= intf_to_devnode(intf
);
159 dev_dbg(gobj
->mdev
->dev
,
160 "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
161 event_name
, media_id(gobj
),
163 devnode
->major
, devnode
->minor
);
170 void media_gobj_create(struct media_device
*mdev
,
171 enum media_gobj_type type
,
172 struct media_gobj
*gobj
)
178 /* Create a per-type unique object ID */
179 gobj
->id
= media_gobj_gen_id(type
, ++mdev
->id
);
182 case MEDIA_GRAPH_ENTITY
:
183 list_add_tail(&gobj
->list
, &mdev
->entities
);
185 case MEDIA_GRAPH_PAD
:
186 list_add_tail(&gobj
->list
, &mdev
->pads
);
188 case MEDIA_GRAPH_LINK
:
189 list_add_tail(&gobj
->list
, &mdev
->links
);
191 case MEDIA_GRAPH_INTF_DEVNODE
:
192 list_add_tail(&gobj
->list
, &mdev
->interfaces
);
196 mdev
->topology_version
++;
198 dev_dbg_obj(__func__
, gobj
);
201 void media_gobj_destroy(struct media_gobj
*gobj
)
203 /* Do nothing if the object is not linked. */
204 if (gobj
->mdev
== NULL
)
207 dev_dbg_obj(__func__
, gobj
);
209 gobj
->mdev
->topology_version
++;
211 /* Remove the object from mdev list */
212 list_del(&gobj
->list
);
218 * TODO: Get rid of this.
220 #define MEDIA_ENTITY_MAX_PADS 512
222 int media_entity_pads_init(struct media_entity
*entity
, u16 num_pads
,
223 struct media_pad
*pads
)
225 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
228 if (num_pads
>= MEDIA_ENTITY_MAX_PADS
)
231 entity
->num_pads
= num_pads
;
235 mutex_lock(&mdev
->graph_mutex
);
237 for (i
= 0; i
< num_pads
; i
++) {
238 pads
[i
].entity
= entity
;
241 media_gobj_create(mdev
, MEDIA_GRAPH_PAD
,
242 &entity
->pads
[i
].graph_obj
);
246 mutex_unlock(&mdev
->graph_mutex
);
250 EXPORT_SYMBOL_GPL(media_entity_pads_init
);
252 /* -----------------------------------------------------------------------------
256 static struct media_entity
*
257 media_entity_other(struct media_entity
*entity
, struct media_link
*link
)
259 if (link
->source
->entity
== entity
)
260 return link
->sink
->entity
;
262 return link
->source
->entity
;
265 /* push an entity to traversal stack */
266 static void stack_push(struct media_graph
*graph
,
267 struct media_entity
*entity
)
269 if (graph
->top
== MEDIA_ENTITY_ENUM_MAX_DEPTH
- 1) {
274 graph
->stack
[graph
->top
].link
= entity
->links
.next
;
275 graph
->stack
[graph
->top
].entity
= entity
;
278 static struct media_entity
*stack_pop(struct media_graph
*graph
)
280 struct media_entity
*entity
;
282 entity
= graph
->stack
[graph
->top
].entity
;
288 #define link_top(en) ((en)->stack[(en)->top].link)
289 #define stack_top(en) ((en)->stack[(en)->top].entity)
292 * media_graph_walk_init - Allocate resources for graph walk
293 * @graph: Media graph structure that will be used to walk the graph
294 * @mdev: Media device
296 * Reserve resources for graph walk in media device's current
297 * state. The memory must be released using
298 * media_graph_walk_free().
300 * Returns error on failure, zero on success.
302 __must_check
int media_graph_walk_init(
303 struct media_graph
*graph
, struct media_device
*mdev
)
305 return media_entity_enum_init(&graph
->ent_enum
, mdev
);
307 EXPORT_SYMBOL_GPL(media_graph_walk_init
);
310 * media_graph_walk_cleanup - Release resources related to graph walking
311 * @graph: Media graph structure that was used to walk the graph
313 void media_graph_walk_cleanup(struct media_graph
*graph
)
315 media_entity_enum_cleanup(&graph
->ent_enum
);
317 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup
);
319 void media_graph_walk_start(struct media_graph
*graph
,
320 struct media_entity
*entity
)
322 media_entity_enum_zero(&graph
->ent_enum
);
323 media_entity_enum_set(&graph
->ent_enum
, entity
);
326 graph
->stack
[graph
->top
].entity
= NULL
;
327 stack_push(graph
, entity
);
328 dev_dbg(entity
->graph_obj
.mdev
->dev
,
329 "begin graph walk at '%s'\n", entity
->name
);
331 EXPORT_SYMBOL_GPL(media_graph_walk_start
);
333 static void media_graph_walk_iter(struct media_graph
*graph
)
335 struct media_entity
*entity
= stack_top(graph
);
336 struct media_link
*link
;
337 struct media_entity
*next
;
339 link
= list_entry(link_top(graph
), typeof(*link
), list
);
341 /* The link is not enabled so we do not follow. */
342 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
343 link_top(graph
) = link_top(graph
)->next
;
344 dev_dbg(entity
->graph_obj
.mdev
->dev
,
345 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
346 link
->source
->entity
->name
, link
->source
->index
,
347 link
->sink
->entity
->name
, link
->sink
->index
);
351 /* Get the entity in the other end of the link . */
352 next
= media_entity_other(entity
, link
);
354 /* Has the entity already been visited? */
355 if (media_entity_enum_test_and_set(&graph
->ent_enum
, next
)) {
356 link_top(graph
) = link_top(graph
)->next
;
357 dev_dbg(entity
->graph_obj
.mdev
->dev
,
358 "walk: skipping entity '%s' (already seen)\n",
363 /* Push the new entity to stack and start over. */
364 link_top(graph
) = link_top(graph
)->next
;
365 stack_push(graph
, next
);
366 dev_dbg(entity
->graph_obj
.mdev
->dev
, "walk: pushing '%s' on stack\n",
370 struct media_entity
*media_graph_walk_next(struct media_graph
*graph
)
372 struct media_entity
*entity
;
374 if (stack_top(graph
) == NULL
)
378 * Depth first search. Push entity to stack and continue from
379 * top of the stack until no more entities on the level can be
382 while (link_top(graph
) != &stack_top(graph
)->links
)
383 media_graph_walk_iter(graph
);
385 entity
= stack_pop(graph
);
386 dev_dbg(entity
->graph_obj
.mdev
->dev
,
387 "walk: returning entity '%s'\n", entity
->name
);
391 EXPORT_SYMBOL_GPL(media_graph_walk_next
);
393 int media_entity_get_fwnode_pad(struct media_entity
*entity
,
394 struct fwnode_handle
*fwnode
,
395 unsigned long direction_flags
)
397 struct fwnode_endpoint endpoint
;
401 if (!entity
->ops
|| !entity
->ops
->get_fwnode_pad
) {
402 for (i
= 0; i
< entity
->num_pads
; i
++) {
403 if (entity
->pads
[i
].flags
& direction_flags
)
410 ret
= fwnode_graph_parse_endpoint(fwnode
, &endpoint
);
414 ret
= entity
->ops
->get_fwnode_pad(&endpoint
);
418 if (ret
>= entity
->num_pads
)
421 if (!(entity
->pads
[ret
].flags
& direction_flags
))
426 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad
);
428 /* -----------------------------------------------------------------------------
429 * Pipeline management
432 __must_check
int __media_pipeline_start(struct media_entity
*entity
,
433 struct media_pipeline
*pipe
)
435 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
436 struct media_graph
*graph
= &pipe
->graph
;
437 struct media_entity
*entity_err
= entity
;
438 struct media_link
*link
;
441 if (!pipe
->streaming_count
++) {
442 ret
= media_graph_walk_init(&pipe
->graph
, mdev
);
444 goto error_graph_walk_start
;
447 media_graph_walk_start(&pipe
->graph
, entity
);
449 while ((entity
= media_graph_walk_next(graph
))) {
450 DECLARE_BITMAP(active
, MEDIA_ENTITY_MAX_PADS
);
451 DECLARE_BITMAP(has_no_links
, MEDIA_ENTITY_MAX_PADS
);
453 entity
->stream_count
++;
455 if (WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
)) {
462 /* Already streaming --- no need to check. */
463 if (entity
->stream_count
> 1)
466 if (!entity
->ops
|| !entity
->ops
->link_validate
)
469 bitmap_zero(active
, entity
->num_pads
);
470 bitmap_fill(has_no_links
, entity
->num_pads
);
472 list_for_each_entry(link
, &entity
->links
, list
) {
473 struct media_pad
*pad
= link
->sink
->entity
== entity
474 ? link
->sink
: link
->source
;
476 /* Mark that a pad is connected by a link. */
477 bitmap_clear(has_no_links
, pad
->index
, 1);
480 * Pads that either do not need to connect or
481 * are connected through an enabled link are
484 if (!(pad
->flags
& MEDIA_PAD_FL_MUST_CONNECT
) ||
485 link
->flags
& MEDIA_LNK_FL_ENABLED
)
486 bitmap_set(active
, pad
->index
, 1);
489 * Link validation will only take place for
490 * sink ends of the link that are enabled.
492 if (link
->sink
!= pad
||
493 !(link
->flags
& MEDIA_LNK_FL_ENABLED
))
496 ret
= entity
->ops
->link_validate(link
);
497 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
498 dev_dbg(entity
->graph_obj
.mdev
->dev
,
499 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
500 link
->source
->entity
->name
,
502 entity
->name
, link
->sink
->index
, ret
);
507 /* Either no links or validated links are fine. */
508 bitmap_or(active
, active
, has_no_links
, entity
->num_pads
);
510 if (!bitmap_full(active
, entity
->num_pads
)) {
512 dev_dbg(entity
->graph_obj
.mdev
->dev
,
513 "'%s':%u must be connected by an enabled link\n",
515 (unsigned)find_first_zero_bit(
516 active
, entity
->num_pads
));
525 * Link validation on graph failed. We revert what we did and
528 media_graph_walk_start(graph
, entity_err
);
530 while ((entity_err
= media_graph_walk_next(graph
))) {
531 /* Sanity check for negative stream_count */
532 if (!WARN_ON_ONCE(entity_err
->stream_count
<= 0)) {
533 entity_err
->stream_count
--;
534 if (entity_err
->stream_count
== 0)
535 entity_err
->pipe
= NULL
;
539 * We haven't increased stream_count further than this
542 if (entity_err
== entity
)
546 error_graph_walk_start
:
547 if (!--pipe
->streaming_count
)
548 media_graph_walk_cleanup(graph
);
552 EXPORT_SYMBOL_GPL(__media_pipeline_start
);
554 __must_check
int media_pipeline_start(struct media_entity
*entity
,
555 struct media_pipeline
*pipe
)
557 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
560 mutex_lock(&mdev
->graph_mutex
);
561 ret
= __media_pipeline_start(entity
, pipe
);
562 mutex_unlock(&mdev
->graph_mutex
);
565 EXPORT_SYMBOL_GPL(media_pipeline_start
);
567 void __media_pipeline_stop(struct media_entity
*entity
)
569 struct media_graph
*graph
= &entity
->pipe
->graph
;
570 struct media_pipeline
*pipe
= entity
->pipe
;
573 * If the following check fails, the driver has performed an
574 * unbalanced call to media_pipeline_stop()
579 media_graph_walk_start(graph
, entity
);
581 while ((entity
= media_graph_walk_next(graph
))) {
582 /* Sanity check for negative stream_count */
583 if (!WARN_ON_ONCE(entity
->stream_count
<= 0)) {
584 entity
->stream_count
--;
585 if (entity
->stream_count
== 0)
590 if (!--pipe
->streaming_count
)
591 media_graph_walk_cleanup(graph
);
594 EXPORT_SYMBOL_GPL(__media_pipeline_stop
);
596 void media_pipeline_stop(struct media_entity
*entity
)
598 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
600 mutex_lock(&mdev
->graph_mutex
);
601 __media_pipeline_stop(entity
);
602 mutex_unlock(&mdev
->graph_mutex
);
604 EXPORT_SYMBOL_GPL(media_pipeline_stop
);
606 /* -----------------------------------------------------------------------------
610 struct media_entity
*media_entity_get(struct media_entity
*entity
)
615 if (entity
->graph_obj
.mdev
->dev
&&
616 !try_module_get(entity
->graph_obj
.mdev
->dev
->driver
->owner
))
621 EXPORT_SYMBOL_GPL(media_entity_get
);
623 void media_entity_put(struct media_entity
*entity
)
628 if (entity
->graph_obj
.mdev
->dev
)
629 module_put(entity
->graph_obj
.mdev
->dev
->driver
->owner
);
631 EXPORT_SYMBOL_GPL(media_entity_put
);
633 /* -----------------------------------------------------------------------------
637 static struct media_link
*media_add_link(struct list_head
*head
)
639 struct media_link
*link
;
641 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
645 list_add_tail(&link
->list
, head
);
650 static void __media_entity_remove_link(struct media_entity
*entity
,
651 struct media_link
*link
)
653 struct media_link
*rlink
, *tmp
;
654 struct media_entity
*remote
;
656 if (link
->source
->entity
== entity
)
657 remote
= link
->sink
->entity
;
659 remote
= link
->source
->entity
;
661 list_for_each_entry_safe(rlink
, tmp
, &remote
->links
, list
) {
662 if (rlink
!= link
->reverse
)
665 if (link
->source
->entity
== entity
)
666 remote
->num_backlinks
--;
668 /* Remove the remote link */
669 list_del(&rlink
->list
);
670 media_gobj_destroy(&rlink
->graph_obj
);
673 if (--remote
->num_links
== 0)
676 list_del(&link
->list
);
677 media_gobj_destroy(&link
->graph_obj
);
682 media_create_pad_link(struct media_entity
*source
, u16 source_pad
,
683 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
685 struct media_link
*link
;
686 struct media_link
*backlink
;
688 BUG_ON(source
== NULL
|| sink
== NULL
);
689 BUG_ON(source_pad
>= source
->num_pads
);
690 BUG_ON(sink_pad
>= sink
->num_pads
);
692 link
= media_add_link(&source
->links
);
696 link
->source
= &source
->pads
[source_pad
];
697 link
->sink
= &sink
->pads
[sink_pad
];
698 link
->flags
= flags
& ~MEDIA_LNK_FL_INTERFACE_LINK
;
700 /* Initialize graph object embedded at the new link */
701 media_gobj_create(source
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
704 /* Create the backlink. Backlinks are used to help graph traversal and
705 * are not reported to userspace.
707 backlink
= media_add_link(&sink
->links
);
708 if (backlink
== NULL
) {
709 __media_entity_remove_link(source
, link
);
713 backlink
->source
= &source
->pads
[source_pad
];
714 backlink
->sink
= &sink
->pads
[sink_pad
];
715 backlink
->flags
= flags
;
716 backlink
->is_backlink
= true;
718 /* Initialize graph object embedded at the new link */
719 media_gobj_create(sink
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
720 &backlink
->graph_obj
);
722 link
->reverse
= backlink
;
723 backlink
->reverse
= link
;
725 sink
->num_backlinks
++;
731 EXPORT_SYMBOL_GPL(media_create_pad_link
);
733 int media_create_pad_links(const struct media_device
*mdev
,
734 const u32 source_function
,
735 struct media_entity
*source
,
736 const u16 source_pad
,
737 const u32 sink_function
,
738 struct media_entity
*sink
,
741 const bool allow_both_undefined
)
743 struct media_entity
*entity
;
747 /* Trivial case: 1:1 relation */
749 return media_create_pad_link(source
, source_pad
,
750 sink
, sink_pad
, flags
);
752 /* Worse case scenario: n:n relation */
753 if (!source
&& !sink
) {
754 if (!allow_both_undefined
)
756 media_device_for_each_entity(source
, mdev
) {
757 if (source
->function
!= source_function
)
759 media_device_for_each_entity(sink
, mdev
) {
760 if (sink
->function
!= sink_function
)
762 ret
= media_create_pad_link(source
, source_pad
,
767 flags
&= ~(MEDIA_LNK_FL_ENABLED
|
768 MEDIA_LNK_FL_IMMUTABLE
);
774 /* Handle 1:n and n:1 cases */
776 function
= sink_function
;
778 function
= source_function
;
780 media_device_for_each_entity(entity
, mdev
) {
781 if (entity
->function
!= function
)
785 ret
= media_create_pad_link(source
, source_pad
,
786 entity
, sink_pad
, flags
);
788 ret
= media_create_pad_link(entity
, source_pad
,
789 sink
, sink_pad
, flags
);
792 flags
&= ~(MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
796 EXPORT_SYMBOL_GPL(media_create_pad_links
);
798 void __media_entity_remove_links(struct media_entity
*entity
)
800 struct media_link
*link
, *tmp
;
802 list_for_each_entry_safe(link
, tmp
, &entity
->links
, list
)
803 __media_entity_remove_link(entity
, link
);
805 entity
->num_links
= 0;
806 entity
->num_backlinks
= 0;
808 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
810 void media_entity_remove_links(struct media_entity
*entity
)
812 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
814 /* Do nothing if the entity is not registered. */
818 mutex_lock(&mdev
->graph_mutex
);
819 __media_entity_remove_links(entity
);
820 mutex_unlock(&mdev
->graph_mutex
);
822 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
824 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
828 /* Notify both entities. */
829 ret
= media_entity_call(link
->source
->entity
, link_setup
,
830 link
->source
, link
->sink
, flags
);
831 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
834 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
835 link
->sink
, link
->source
, flags
);
836 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
837 media_entity_call(link
->source
->entity
, link_setup
,
838 link
->source
, link
->sink
, link
->flags
);
843 link
->reverse
->flags
= link
->flags
;
848 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
850 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
851 struct media_device
*mdev
;
852 struct media_entity
*source
, *sink
;
858 /* The non-modifiable link flags must not be modified. */
859 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
862 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
863 return link
->flags
== flags
? 0 : -EINVAL
;
865 if (link
->flags
== flags
)
868 source
= link
->source
->entity
;
869 sink
= link
->sink
->entity
;
871 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
872 (source
->stream_count
|| sink
->stream_count
))
875 mdev
= source
->graph_obj
.mdev
;
877 if (mdev
->ops
&& mdev
->ops
->link_notify
) {
878 ret
= mdev
->ops
->link_notify(link
, flags
,
879 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
884 ret
= __media_entity_setup_link_notify(link
, flags
);
886 if (mdev
->ops
&& mdev
->ops
->link_notify
)
887 mdev
->ops
->link_notify(link
, flags
,
888 MEDIA_DEV_NOTIFY_POST_LINK_CH
);
892 EXPORT_SYMBOL_GPL(__media_entity_setup_link
);
894 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
898 mutex_lock(&link
->graph_obj
.mdev
->graph_mutex
);
899 ret
= __media_entity_setup_link(link
, flags
);
900 mutex_unlock(&link
->graph_obj
.mdev
->graph_mutex
);
904 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
907 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
909 struct media_link
*link
;
911 list_for_each_entry(link
, &source
->entity
->links
, list
) {
912 if (link
->source
->entity
== source
->entity
&&
913 link
->source
->index
== source
->index
&&
914 link
->sink
->entity
== sink
->entity
&&
915 link
->sink
->index
== sink
->index
)
921 EXPORT_SYMBOL_GPL(media_entity_find_link
);
923 struct media_pad
*media_entity_remote_pad(const struct media_pad
*pad
)
925 struct media_link
*link
;
927 list_for_each_entry(link
, &pad
->entity
->links
, list
) {
928 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
931 if (link
->source
== pad
)
934 if (link
->sink
== pad
)
941 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);
943 static void media_interface_init(struct media_device
*mdev
,
944 struct media_interface
*intf
,
946 u32 intf_type
, u32 flags
)
948 intf
->type
= intf_type
;
950 INIT_LIST_HEAD(&intf
->links
);
952 media_gobj_create(mdev
, gobj_type
, &intf
->graph_obj
);
955 /* Functions related to the media interface via device nodes */
957 struct media_intf_devnode
*media_devnode_create(struct media_device
*mdev
,
959 u32 major
, u32 minor
)
961 struct media_intf_devnode
*devnode
;
963 devnode
= kzalloc(sizeof(*devnode
), GFP_KERNEL
);
967 devnode
->major
= major
;
968 devnode
->minor
= minor
;
970 media_interface_init(mdev
, &devnode
->intf
, MEDIA_GRAPH_INTF_DEVNODE
,
975 EXPORT_SYMBOL_GPL(media_devnode_create
);
977 void media_devnode_remove(struct media_intf_devnode
*devnode
)
979 media_remove_intf_links(&devnode
->intf
);
980 media_gobj_destroy(&devnode
->intf
.graph_obj
);
983 EXPORT_SYMBOL_GPL(media_devnode_remove
);
985 struct media_link
*media_create_intf_link(struct media_entity
*entity
,
986 struct media_interface
*intf
,
989 struct media_link
*link
;
991 link
= media_add_link(&intf
->links
);
996 link
->entity
= entity
;
997 link
->flags
= flags
| MEDIA_LNK_FL_INTERFACE_LINK
;
999 /* Initialize graph object embedded at the new link */
1000 media_gobj_create(intf
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
1005 EXPORT_SYMBOL_GPL(media_create_intf_link
);
1007 void __media_remove_intf_link(struct media_link
*link
)
1009 list_del(&link
->list
);
1010 media_gobj_destroy(&link
->graph_obj
);
1013 EXPORT_SYMBOL_GPL(__media_remove_intf_link
);
1015 void media_remove_intf_link(struct media_link
*link
)
1017 struct media_device
*mdev
= link
->graph_obj
.mdev
;
1019 /* Do nothing if the intf is not registered. */
1023 mutex_lock(&mdev
->graph_mutex
);
1024 __media_remove_intf_link(link
);
1025 mutex_unlock(&mdev
->graph_mutex
);
1027 EXPORT_SYMBOL_GPL(media_remove_intf_link
);
1029 void __media_remove_intf_links(struct media_interface
*intf
)
1031 struct media_link
*link
, *tmp
;
1033 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
)
1034 __media_remove_intf_link(link
);
1037 EXPORT_SYMBOL_GPL(__media_remove_intf_links
);
1039 void media_remove_intf_links(struct media_interface
*intf
)
1041 struct media_device
*mdev
= intf
->graph_obj
.mdev
;
1043 /* Do nothing if the intf is not registered. */
1047 mutex_lock(&mdev
->graph_mutex
);
1048 __media_remove_intf_links(intf
);
1049 mutex_unlock(&mdev
->graph_mutex
);
1051 EXPORT_SYMBOL_GPL(media_remove_intf_links
);