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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <media/media-entity.h>
28 * media_entity_init - Initialize a media entity
30 * @num_pads: Total number of sink and source pads.
31 * @extra_links: Initial estimate of the number of extra links.
32 * @pads: Array of 'num_pads' pads.
34 * The total number of pads is an intrinsic property of entities known by the
35 * entity driver, while the total number of links depends on hardware design
36 * and is an extrinsic property unknown to the entity driver. However, in most
37 * use cases the entity driver can guess the number of links which can safely
38 * be assumed to be equal to or larger than the number of pads.
40 * For those reasons the links array can be preallocated based on the entity
41 * driver guess and will be reallocated later if extra links need to be
44 * This function allocates a links array with enough space to hold at least
45 * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will
46 * be set to the number of allocated elements.
48 * The pads array is managed by the entity driver and passed to
49 * media_entity_init() where its pointer will be stored in the entity structure.
52 media_entity_init(struct media_entity *entity, u16 num_pads,
53 struct media_pad *pads, u16 extra_links)
55 struct media_link *links;
56 unsigned int max_links = num_pads + extra_links;
59 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
64 entity->max_links = max_links;
65 entity->num_links = 0;
66 entity->num_backlinks = 0;
67 entity->num_pads = num_pads;
69 entity->links = links;
71 for (i = 0; i < num_pads; i++) {
72 pads[i].entity = entity;
78 EXPORT_SYMBOL_GPL(media_entity_init);
81 media_entity_cleanup(struct media_entity *entity)
85 EXPORT_SYMBOL_GPL(media_entity_cleanup);
87 /* -----------------------------------------------------------------------------
91 static struct media_entity *
92 media_entity_other(struct media_entity *entity, struct media_link *link)
94 if (link->source->entity == entity)
95 return link->sink->entity;
97 return link->source->entity;
100 /* push an entity to traversal stack */
101 static void stack_push(struct media_entity_graph *graph,
102 struct media_entity *entity)
104 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
109 graph->stack[graph->top].link = 0;
110 graph->stack[graph->top].entity = entity;
113 static struct media_entity *stack_pop(struct media_entity_graph *graph)
115 struct media_entity *entity;
117 entity = graph->stack[graph->top].entity;
123 #define stack_peek(en) ((en)->stack[(en)->top - 1].entity)
124 #define link_top(en) ((en)->stack[(en)->top].link)
125 #define stack_top(en) ((en)->stack[(en)->top].entity)
128 * media_entity_graph_walk_start - Start walking the media graph at a given entity
129 * @graph: Media graph structure that will be used to walk the graph
130 * @entity: Starting entity
132 * This function initializes the graph traversal structure to walk the entities
133 * graph starting at the given entity. The traversal structure must not be
134 * modified by the caller during graph traversal. When done the structure can
137 void media_entity_graph_walk_start(struct media_entity_graph *graph,
138 struct media_entity *entity)
141 graph->stack[graph->top].entity = NULL;
142 stack_push(graph, entity);
144 EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
147 * media_entity_graph_walk_next - Get the next entity in the graph
148 * @graph: Media graph structure
150 * Perform a depth-first traversal of the given media entities graph.
152 * The graph structure must have been previously initialized with a call to
153 * media_entity_graph_walk_start().
155 * Return the next entity in the graph or NULL if the whole graph have been
158 struct media_entity *
159 media_entity_graph_walk_next(struct media_entity_graph *graph)
161 if (stack_top(graph) == NULL)
165 * Depth first search. Push entity to stack and continue from
166 * top of the stack until no more entities on the level can be
169 while (link_top(graph) < stack_top(graph)->num_links) {
170 struct media_entity *entity = stack_top(graph);
171 struct media_link *link = &entity->links[link_top(graph)];
172 struct media_entity *next;
174 /* The link is not enabled so we do not follow. */
175 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
180 /* Get the entity in the other end of the link . */
181 next = media_entity_other(entity, link);
183 /* Was it the entity we came here from? */
184 if (next == stack_peek(graph)) {
189 /* Push the new entity to stack and start over. */
191 stack_push(graph, next);
194 return stack_pop(graph);
196 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
198 /* -----------------------------------------------------------------------------
202 static struct media_link *media_entity_add_link(struct media_entity *entity)
204 if (entity->num_links >= entity->max_links) {
205 struct media_link *links = entity->links;
206 unsigned int max_links = entity->max_links + 2;
209 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
213 for (i = 0; i < entity->num_links; i++)
214 links[i].reverse->reverse = &links[i];
216 entity->max_links = max_links;
217 entity->links = links;
220 return &entity->links[entity->num_links++];
224 media_entity_create_link(struct media_entity *source, u16 source_pad,
225 struct media_entity *sink, u16 sink_pad, u32 flags)
227 struct media_link *link;
228 struct media_link *backlink;
230 BUG_ON(source == NULL || sink == NULL);
231 BUG_ON(source_pad >= source->num_pads);
232 BUG_ON(sink_pad >= sink->num_pads);
234 link = media_entity_add_link(source);
238 link->source = &source->pads[source_pad];
239 link->sink = &sink->pads[sink_pad];
242 /* Create the backlink. Backlinks are used to help graph traversal and
243 * are not reported to userspace.
245 backlink = media_entity_add_link(sink);
246 if (backlink == NULL) {
251 backlink->source = &source->pads[source_pad];
252 backlink->sink = &sink->pads[sink_pad];
253 backlink->flags = flags;
255 link->reverse = backlink;
256 backlink->reverse = link;
258 sink->num_backlinks++;
262 EXPORT_SYMBOL_GPL(media_entity_create_link);