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
);
217 int media_entity_pads_init(struct media_entity
*entity
, u16 num_pads
,
218 struct media_pad
*pads
)
220 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
223 entity
->num_pads
= num_pads
;
227 mutex_lock(&mdev
->graph_mutex
);
229 for (i
= 0; i
< num_pads
; i
++) {
230 pads
[i
].entity
= entity
;
233 media_gobj_create(mdev
, MEDIA_GRAPH_PAD
,
234 &entity
->pads
[i
].graph_obj
);
238 mutex_unlock(&mdev
->graph_mutex
);
242 EXPORT_SYMBOL_GPL(media_entity_pads_init
);
244 /* -----------------------------------------------------------------------------
248 static struct media_entity
*
249 media_entity_other(struct media_entity
*entity
, struct media_link
*link
)
251 if (link
->source
->entity
== entity
)
252 return link
->sink
->entity
;
254 return link
->source
->entity
;
257 /* push an entity to traversal stack */
258 static void stack_push(struct media_graph
*graph
,
259 struct media_entity
*entity
)
261 if (graph
->top
== MEDIA_ENTITY_ENUM_MAX_DEPTH
- 1) {
266 graph
->stack
[graph
->top
].link
= entity
->links
.next
;
267 graph
->stack
[graph
->top
].entity
= entity
;
270 static struct media_entity
*stack_pop(struct media_graph
*graph
)
272 struct media_entity
*entity
;
274 entity
= graph
->stack
[graph
->top
].entity
;
280 #define link_top(en) ((en)->stack[(en)->top].link)
281 #define stack_top(en) ((en)->stack[(en)->top].entity)
284 * TODO: Get rid of this.
286 #define MEDIA_ENTITY_MAX_PADS 512
289 * media_graph_walk_init - Allocate resources for graph walk
290 * @graph: Media graph structure that will be used to walk the graph
291 * @mdev: Media device
293 * Reserve resources for graph walk in media device's current
294 * state. The memory must be released using
295 * media_graph_walk_free().
297 * Returns error on failure, zero on success.
299 __must_check
int media_graph_walk_init(
300 struct media_graph
*graph
, struct media_device
*mdev
)
302 return media_entity_enum_init(&graph
->ent_enum
, mdev
);
304 EXPORT_SYMBOL_GPL(media_graph_walk_init
);
307 * media_graph_walk_cleanup - Release resources related to graph walking
308 * @graph: Media graph structure that was used to walk the graph
310 void media_graph_walk_cleanup(struct media_graph
*graph
)
312 media_entity_enum_cleanup(&graph
->ent_enum
);
314 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup
);
316 void media_graph_walk_start(struct media_graph
*graph
,
317 struct media_entity
*entity
)
319 media_entity_enum_zero(&graph
->ent_enum
);
320 media_entity_enum_set(&graph
->ent_enum
, entity
);
323 graph
->stack
[graph
->top
].entity
= NULL
;
324 stack_push(graph
, entity
);
325 dev_dbg(entity
->graph_obj
.mdev
->dev
,
326 "begin graph walk at '%s'\n", entity
->name
);
328 EXPORT_SYMBOL_GPL(media_graph_walk_start
);
330 static void media_graph_walk_iter(struct media_graph
*graph
)
332 struct media_entity
*entity
= stack_top(graph
);
333 struct media_link
*link
;
334 struct media_entity
*next
;
336 link
= list_entry(link_top(graph
), typeof(*link
), list
);
338 /* The link is not enabled so we do not follow. */
339 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
)) {
340 link_top(graph
) = link_top(graph
)->next
;
341 dev_dbg(entity
->graph_obj
.mdev
->dev
,
342 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
343 link
->source
->entity
->name
, link
->source
->index
,
344 link
->sink
->entity
->name
, link
->sink
->index
);
348 /* Get the entity in the other end of the link . */
349 next
= media_entity_other(entity
, link
);
351 /* Has the entity already been visited? */
352 if (media_entity_enum_test_and_set(&graph
->ent_enum
, next
)) {
353 link_top(graph
) = link_top(graph
)->next
;
354 dev_dbg(entity
->graph_obj
.mdev
->dev
,
355 "walk: skipping entity '%s' (already seen)\n",
360 /* Push the new entity to stack and start over. */
361 link_top(graph
) = link_top(graph
)->next
;
362 stack_push(graph
, next
);
363 dev_dbg(entity
->graph_obj
.mdev
->dev
, "walk: pushing '%s' on stack\n",
367 struct media_entity
*media_graph_walk_next(struct media_graph
*graph
)
369 struct media_entity
*entity
;
371 if (stack_top(graph
) == NULL
)
375 * Depth first search. Push entity to stack and continue from
376 * top of the stack until no more entities on the level can be
379 while (link_top(graph
) != &stack_top(graph
)->links
)
380 media_graph_walk_iter(graph
);
382 entity
= stack_pop(graph
);
383 dev_dbg(entity
->graph_obj
.mdev
->dev
,
384 "walk: returning entity '%s'\n", entity
->name
);
388 EXPORT_SYMBOL_GPL(media_graph_walk_next
);
390 int media_entity_get_fwnode_pad(struct media_entity
*entity
,
391 struct fwnode_handle
*fwnode
,
392 unsigned long direction_flags
)
394 struct fwnode_endpoint endpoint
;
398 if (!entity
->ops
|| !entity
->ops
->get_fwnode_pad
) {
399 for (i
= 0; i
< entity
->num_pads
; i
++) {
400 if (entity
->pads
[i
].flags
& direction_flags
)
407 ret
= fwnode_graph_parse_endpoint(fwnode
, &endpoint
);
411 ret
= entity
->ops
->get_fwnode_pad(&endpoint
);
415 if (ret
>= entity
->num_pads
)
418 if (!(entity
->pads
[ret
].flags
& direction_flags
))
423 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad
);
425 /* -----------------------------------------------------------------------------
426 * Pipeline management
429 __must_check
int __media_pipeline_start(struct media_entity
*entity
,
430 struct media_pipeline
*pipe
)
432 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
433 struct media_graph
*graph
= &pipe
->graph
;
434 struct media_entity
*entity_err
= entity
;
435 struct media_link
*link
;
438 if (!pipe
->streaming_count
++) {
439 ret
= media_graph_walk_init(&pipe
->graph
, mdev
);
441 goto error_graph_walk_start
;
444 media_graph_walk_start(&pipe
->graph
, entity
);
446 while ((entity
= media_graph_walk_next(graph
))) {
447 DECLARE_BITMAP(active
, MEDIA_ENTITY_MAX_PADS
);
448 DECLARE_BITMAP(has_no_links
, MEDIA_ENTITY_MAX_PADS
);
450 entity
->stream_count
++;
452 if (WARN_ON(entity
->pipe
&& entity
->pipe
!= pipe
)) {
459 /* Already streaming --- no need to check. */
460 if (entity
->stream_count
> 1)
463 if (!entity
->ops
|| !entity
->ops
->link_validate
)
466 bitmap_zero(active
, entity
->num_pads
);
467 bitmap_fill(has_no_links
, entity
->num_pads
);
469 list_for_each_entry(link
, &entity
->links
, list
) {
470 struct media_pad
*pad
= link
->sink
->entity
== entity
471 ? link
->sink
: link
->source
;
473 /* Mark that a pad is connected by a link. */
474 bitmap_clear(has_no_links
, pad
->index
, 1);
477 * Pads that either do not need to connect or
478 * are connected through an enabled link are
481 if (!(pad
->flags
& MEDIA_PAD_FL_MUST_CONNECT
) ||
482 link
->flags
& MEDIA_LNK_FL_ENABLED
)
483 bitmap_set(active
, pad
->index
, 1);
486 * Link validation will only take place for
487 * sink ends of the link that are enabled.
489 if (link
->sink
!= pad
||
490 !(link
->flags
& MEDIA_LNK_FL_ENABLED
))
493 ret
= entity
->ops
->link_validate(link
);
494 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
495 dev_dbg(entity
->graph_obj
.mdev
->dev
,
496 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
497 link
->source
->entity
->name
,
499 entity
->name
, link
->sink
->index
, ret
);
504 /* Either no links or validated links are fine. */
505 bitmap_or(active
, active
, has_no_links
, entity
->num_pads
);
507 if (!bitmap_full(active
, entity
->num_pads
)) {
509 dev_dbg(entity
->graph_obj
.mdev
->dev
,
510 "'%s':%u must be connected by an enabled link\n",
512 (unsigned)find_first_zero_bit(
513 active
, entity
->num_pads
));
522 * Link validation on graph failed. We revert what we did and
525 media_graph_walk_start(graph
, entity_err
);
527 while ((entity_err
= media_graph_walk_next(graph
))) {
528 /* Sanity check for negative stream_count */
529 if (!WARN_ON_ONCE(entity_err
->stream_count
<= 0)) {
530 entity_err
->stream_count
--;
531 if (entity_err
->stream_count
== 0)
532 entity_err
->pipe
= NULL
;
536 * We haven't increased stream_count further than this
539 if (entity_err
== entity
)
543 error_graph_walk_start
:
544 if (!--pipe
->streaming_count
)
545 media_graph_walk_cleanup(graph
);
549 EXPORT_SYMBOL_GPL(__media_pipeline_start
);
551 __must_check
int media_pipeline_start(struct media_entity
*entity
,
552 struct media_pipeline
*pipe
)
554 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
557 mutex_lock(&mdev
->graph_mutex
);
558 ret
= __media_pipeline_start(entity
, pipe
);
559 mutex_unlock(&mdev
->graph_mutex
);
562 EXPORT_SYMBOL_GPL(media_pipeline_start
);
564 void __media_pipeline_stop(struct media_entity
*entity
)
566 struct media_graph
*graph
= &entity
->pipe
->graph
;
567 struct media_pipeline
*pipe
= entity
->pipe
;
570 * If the following check fails, the driver has performed an
571 * unbalanced call to media_pipeline_stop()
576 media_graph_walk_start(graph
, entity
);
578 while ((entity
= media_graph_walk_next(graph
))) {
579 /* Sanity check for negative stream_count */
580 if (!WARN_ON_ONCE(entity
->stream_count
<= 0)) {
581 entity
->stream_count
--;
582 if (entity
->stream_count
== 0)
587 if (!--pipe
->streaming_count
)
588 media_graph_walk_cleanup(graph
);
591 EXPORT_SYMBOL_GPL(__media_pipeline_stop
);
593 void media_pipeline_stop(struct media_entity
*entity
)
595 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
597 mutex_lock(&mdev
->graph_mutex
);
598 __media_pipeline_stop(entity
);
599 mutex_unlock(&mdev
->graph_mutex
);
601 EXPORT_SYMBOL_GPL(media_pipeline_stop
);
603 /* -----------------------------------------------------------------------------
607 struct media_entity
*media_entity_get(struct media_entity
*entity
)
612 if (entity
->graph_obj
.mdev
->dev
&&
613 !try_module_get(entity
->graph_obj
.mdev
->dev
->driver
->owner
))
618 EXPORT_SYMBOL_GPL(media_entity_get
);
620 void media_entity_put(struct media_entity
*entity
)
625 if (entity
->graph_obj
.mdev
->dev
)
626 module_put(entity
->graph_obj
.mdev
->dev
->driver
->owner
);
628 EXPORT_SYMBOL_GPL(media_entity_put
);
630 /* -----------------------------------------------------------------------------
634 static struct media_link
*media_add_link(struct list_head
*head
)
636 struct media_link
*link
;
638 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
642 list_add_tail(&link
->list
, head
);
647 static void __media_entity_remove_link(struct media_entity
*entity
,
648 struct media_link
*link
)
650 struct media_link
*rlink
, *tmp
;
651 struct media_entity
*remote
;
653 if (link
->source
->entity
== entity
)
654 remote
= link
->sink
->entity
;
656 remote
= link
->source
->entity
;
658 list_for_each_entry_safe(rlink
, tmp
, &remote
->links
, list
) {
659 if (rlink
!= link
->reverse
)
662 if (link
->source
->entity
== entity
)
663 remote
->num_backlinks
--;
665 /* Remove the remote link */
666 list_del(&rlink
->list
);
667 media_gobj_destroy(&rlink
->graph_obj
);
670 if (--remote
->num_links
== 0)
673 list_del(&link
->list
);
674 media_gobj_destroy(&link
->graph_obj
);
679 media_create_pad_link(struct media_entity
*source
, u16 source_pad
,
680 struct media_entity
*sink
, u16 sink_pad
, u32 flags
)
682 struct media_link
*link
;
683 struct media_link
*backlink
;
685 BUG_ON(source
== NULL
|| sink
== NULL
);
686 BUG_ON(source_pad
>= source
->num_pads
);
687 BUG_ON(sink_pad
>= sink
->num_pads
);
689 link
= media_add_link(&source
->links
);
693 link
->source
= &source
->pads
[source_pad
];
694 link
->sink
= &sink
->pads
[sink_pad
];
695 link
->flags
= flags
& ~MEDIA_LNK_FL_INTERFACE_LINK
;
697 /* Initialize graph object embedded at the new link */
698 media_gobj_create(source
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
701 /* Create the backlink. Backlinks are used to help graph traversal and
702 * are not reported to userspace.
704 backlink
= media_add_link(&sink
->links
);
705 if (backlink
== NULL
) {
706 __media_entity_remove_link(source
, link
);
710 backlink
->source
= &source
->pads
[source_pad
];
711 backlink
->sink
= &sink
->pads
[sink_pad
];
712 backlink
->flags
= flags
;
713 backlink
->is_backlink
= true;
715 /* Initialize graph object embedded at the new link */
716 media_gobj_create(sink
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
717 &backlink
->graph_obj
);
719 link
->reverse
= backlink
;
720 backlink
->reverse
= link
;
722 sink
->num_backlinks
++;
728 EXPORT_SYMBOL_GPL(media_create_pad_link
);
730 int media_create_pad_links(const struct media_device
*mdev
,
731 const u32 source_function
,
732 struct media_entity
*source
,
733 const u16 source_pad
,
734 const u32 sink_function
,
735 struct media_entity
*sink
,
738 const bool allow_both_undefined
)
740 struct media_entity
*entity
;
744 /* Trivial case: 1:1 relation */
746 return media_create_pad_link(source
, source_pad
,
747 sink
, sink_pad
, flags
);
749 /* Worse case scenario: n:n relation */
750 if (!source
&& !sink
) {
751 if (!allow_both_undefined
)
753 media_device_for_each_entity(source
, mdev
) {
754 if (source
->function
!= source_function
)
756 media_device_for_each_entity(sink
, mdev
) {
757 if (sink
->function
!= sink_function
)
759 ret
= media_create_pad_link(source
, source_pad
,
764 flags
&= ~(MEDIA_LNK_FL_ENABLED
|
765 MEDIA_LNK_FL_IMMUTABLE
);
771 /* Handle 1:n and n:1 cases */
773 function
= sink_function
;
775 function
= source_function
;
777 media_device_for_each_entity(entity
, mdev
) {
778 if (entity
->function
!= function
)
782 ret
= media_create_pad_link(source
, source_pad
,
783 entity
, sink_pad
, flags
);
785 ret
= media_create_pad_link(entity
, source_pad
,
786 sink
, sink_pad
, flags
);
789 flags
&= ~(MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
);
793 EXPORT_SYMBOL_GPL(media_create_pad_links
);
795 void __media_entity_remove_links(struct media_entity
*entity
)
797 struct media_link
*link
, *tmp
;
799 list_for_each_entry_safe(link
, tmp
, &entity
->links
, list
)
800 __media_entity_remove_link(entity
, link
);
802 entity
->num_links
= 0;
803 entity
->num_backlinks
= 0;
805 EXPORT_SYMBOL_GPL(__media_entity_remove_links
);
807 void media_entity_remove_links(struct media_entity
*entity
)
809 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
811 /* Do nothing if the entity is not registered. */
815 mutex_lock(&mdev
->graph_mutex
);
816 __media_entity_remove_links(entity
);
817 mutex_unlock(&mdev
->graph_mutex
);
819 EXPORT_SYMBOL_GPL(media_entity_remove_links
);
821 static int __media_entity_setup_link_notify(struct media_link
*link
, u32 flags
)
825 /* Notify both entities. */
826 ret
= media_entity_call(link
->source
->entity
, link_setup
,
827 link
->source
, link
->sink
, flags
);
828 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
831 ret
= media_entity_call(link
->sink
->entity
, link_setup
,
832 link
->sink
, link
->source
, flags
);
833 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
834 media_entity_call(link
->source
->entity
, link_setup
,
835 link
->source
, link
->sink
, link
->flags
);
840 link
->reverse
->flags
= link
->flags
;
845 int __media_entity_setup_link(struct media_link
*link
, u32 flags
)
847 const u32 mask
= MEDIA_LNK_FL_ENABLED
;
848 struct media_device
*mdev
;
849 struct media_entity
*source
, *sink
;
855 /* The non-modifiable link flags must not be modified. */
856 if ((link
->flags
& ~mask
) != (flags
& ~mask
))
859 if (link
->flags
& MEDIA_LNK_FL_IMMUTABLE
)
860 return link
->flags
== flags
? 0 : -EINVAL
;
862 if (link
->flags
== flags
)
865 source
= link
->source
->entity
;
866 sink
= link
->sink
->entity
;
868 if (!(link
->flags
& MEDIA_LNK_FL_DYNAMIC
) &&
869 (source
->stream_count
|| sink
->stream_count
))
872 mdev
= source
->graph_obj
.mdev
;
874 if (mdev
->ops
&& mdev
->ops
->link_notify
) {
875 ret
= mdev
->ops
->link_notify(link
, flags
,
876 MEDIA_DEV_NOTIFY_PRE_LINK_CH
);
881 ret
= __media_entity_setup_link_notify(link
, flags
);
883 if (mdev
->ops
&& mdev
->ops
->link_notify
)
884 mdev
->ops
->link_notify(link
, flags
,
885 MEDIA_DEV_NOTIFY_POST_LINK_CH
);
889 EXPORT_SYMBOL_GPL(__media_entity_setup_link
);
891 int media_entity_setup_link(struct media_link
*link
, u32 flags
)
895 mutex_lock(&link
->graph_obj
.mdev
->graph_mutex
);
896 ret
= __media_entity_setup_link(link
, flags
);
897 mutex_unlock(&link
->graph_obj
.mdev
->graph_mutex
);
901 EXPORT_SYMBOL_GPL(media_entity_setup_link
);
904 media_entity_find_link(struct media_pad
*source
, struct media_pad
*sink
)
906 struct media_link
*link
;
908 list_for_each_entry(link
, &source
->entity
->links
, list
) {
909 if (link
->source
->entity
== source
->entity
&&
910 link
->source
->index
== source
->index
&&
911 link
->sink
->entity
== sink
->entity
&&
912 link
->sink
->index
== sink
->index
)
918 EXPORT_SYMBOL_GPL(media_entity_find_link
);
920 struct media_pad
*media_entity_remote_pad(const struct media_pad
*pad
)
922 struct media_link
*link
;
924 list_for_each_entry(link
, &pad
->entity
->links
, list
) {
925 if (!(link
->flags
& MEDIA_LNK_FL_ENABLED
))
928 if (link
->source
== pad
)
931 if (link
->sink
== pad
)
938 EXPORT_SYMBOL_GPL(media_entity_remote_pad
);
940 static void media_interface_init(struct media_device
*mdev
,
941 struct media_interface
*intf
,
943 u32 intf_type
, u32 flags
)
945 intf
->type
= intf_type
;
947 INIT_LIST_HEAD(&intf
->links
);
949 media_gobj_create(mdev
, gobj_type
, &intf
->graph_obj
);
952 /* Functions related to the media interface via device nodes */
954 struct media_intf_devnode
*media_devnode_create(struct media_device
*mdev
,
956 u32 major
, u32 minor
)
958 struct media_intf_devnode
*devnode
;
960 devnode
= kzalloc(sizeof(*devnode
), GFP_KERNEL
);
964 devnode
->major
= major
;
965 devnode
->minor
= minor
;
967 media_interface_init(mdev
, &devnode
->intf
, MEDIA_GRAPH_INTF_DEVNODE
,
972 EXPORT_SYMBOL_GPL(media_devnode_create
);
974 void media_devnode_remove(struct media_intf_devnode
*devnode
)
976 media_remove_intf_links(&devnode
->intf
);
977 media_gobj_destroy(&devnode
->intf
.graph_obj
);
980 EXPORT_SYMBOL_GPL(media_devnode_remove
);
982 struct media_link
*media_create_intf_link(struct media_entity
*entity
,
983 struct media_interface
*intf
,
986 struct media_link
*link
;
988 link
= media_add_link(&intf
->links
);
993 link
->entity
= entity
;
994 link
->flags
= flags
| MEDIA_LNK_FL_INTERFACE_LINK
;
996 /* Initialize graph object embedded at the new link */
997 media_gobj_create(intf
->graph_obj
.mdev
, MEDIA_GRAPH_LINK
,
1002 EXPORT_SYMBOL_GPL(media_create_intf_link
);
1004 void __media_remove_intf_link(struct media_link
*link
)
1006 list_del(&link
->list
);
1007 media_gobj_destroy(&link
->graph_obj
);
1010 EXPORT_SYMBOL_GPL(__media_remove_intf_link
);
1012 void media_remove_intf_link(struct media_link
*link
)
1014 struct media_device
*mdev
= link
->graph_obj
.mdev
;
1016 /* Do nothing if the intf is not registered. */
1020 mutex_lock(&mdev
->graph_mutex
);
1021 __media_remove_intf_link(link
);
1022 mutex_unlock(&mdev
->graph_mutex
);
1024 EXPORT_SYMBOL_GPL(media_remove_intf_link
);
1026 void __media_remove_intf_links(struct media_interface
*intf
)
1028 struct media_link
*link
, *tmp
;
1030 list_for_each_entry_safe(link
, tmp
, &intf
->links
, list
)
1031 __media_remove_intf_link(link
);
1034 EXPORT_SYMBOL_GPL(__media_remove_intf_links
);
1036 void media_remove_intf_links(struct media_interface
*intf
)
1038 struct media_device
*mdev
= intf
->graph_obj
.mdev
;
1040 /* Do nothing if the intf is not registered. */
1044 mutex_lock(&mdev
->graph_mutex
);
1045 __media_remove_intf_links(intf
);
1046 mutex_unlock(&mdev
->graph_mutex
);
1048 EXPORT_SYMBOL_GPL(media_remove_intf_links
);