[media] media: Entity graph traversal
[deliverable/linux.git] / drivers / media / media-entity.c
CommitLineData
53e269c1
LP
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 * 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
21 */
22
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <media/media-entity.h>
26
27/**
28 * media_entity_init - Initialize a media entity
29 *
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.
33 *
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.
39 *
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
42 * created.
43 *
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.
47 *
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.
50 */
51int
52media_entity_init(struct media_entity *entity, u16 num_pads,
53 struct media_pad *pads, u16 extra_links)
54{
55 struct media_link *links;
56 unsigned int max_links = num_pads + extra_links;
57 unsigned int i;
58
59 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
60 if (links == NULL)
61 return -ENOMEM;
62
63 entity->group_id = 0;
64 entity->max_links = max_links;
65 entity->num_links = 0;
66 entity->num_backlinks = 0;
67 entity->num_pads = num_pads;
68 entity->pads = pads;
69 entity->links = links;
70
71 for (i = 0; i < num_pads; i++) {
72 pads[i].entity = entity;
73 pads[i].index = i;
74 }
75
76 return 0;
77}
78EXPORT_SYMBOL_GPL(media_entity_init);
79
80void
81media_entity_cleanup(struct media_entity *entity)
82{
83 kfree(entity->links);
84}
85EXPORT_SYMBOL_GPL(media_entity_cleanup);
86
a5ccc48a
SA
87/* -----------------------------------------------------------------------------
88 * Graph traversal
89 */
90
91static struct media_entity *
92media_entity_other(struct media_entity *entity, struct media_link *link)
93{
94 if (link->source->entity == entity)
95 return link->sink->entity;
96 else
97 return link->source->entity;
98}
99
100/* push an entity to traversal stack */
101static void stack_push(struct media_entity_graph *graph,
102 struct media_entity *entity)
103{
104 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
105 WARN_ON(1);
106 return;
107 }
108 graph->top++;
109 graph->stack[graph->top].link = 0;
110 graph->stack[graph->top].entity = entity;
111}
112
113static struct media_entity *stack_pop(struct media_entity_graph *graph)
114{
115 struct media_entity *entity;
116
117 entity = graph->stack[graph->top].entity;
118 graph->top--;
119
120 return entity;
121}
122
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)
126
127/**
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
131 *
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
135 * safely be freed.
136 */
137void media_entity_graph_walk_start(struct media_entity_graph *graph,
138 struct media_entity *entity)
139{
140 graph->top = 0;
141 graph->stack[graph->top].entity = NULL;
142 stack_push(graph, entity);
143}
144EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
145
146/**
147 * media_entity_graph_walk_next - Get the next entity in the graph
148 * @graph: Media graph structure
149 *
150 * Perform a depth-first traversal of the given media entities graph.
151 *
152 * The graph structure must have been previously initialized with a call to
153 * media_entity_graph_walk_start().
154 *
155 * Return the next entity in the graph or NULL if the whole graph have been
156 * traversed.
157 */
158struct media_entity *
159media_entity_graph_walk_next(struct media_entity_graph *graph)
160{
161 if (stack_top(graph) == NULL)
162 return NULL;
163
164 /*
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
167 * found.
168 */
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;
173
174 /* The link is not enabled so we do not follow. */
175 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
176 link_top(graph)++;
177 continue;
178 }
179
180 /* Get the entity in the other end of the link . */
181 next = media_entity_other(entity, link);
182
183 /* Was it the entity we came here from? */
184 if (next == stack_peek(graph)) {
185 link_top(graph)++;
186 continue;
187 }
188
189 /* Push the new entity to stack and start over. */
190 link_top(graph)++;
191 stack_push(graph, next);
192 }
193
194 return stack_pop(graph);
195}
196EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
197
198/* -----------------------------------------------------------------------------
199 * Links management
200 */
201
53e269c1
LP
202static struct media_link *media_entity_add_link(struct media_entity *entity)
203{
204 if (entity->num_links >= entity->max_links) {
205 struct media_link *links = entity->links;
206 unsigned int max_links = entity->max_links + 2;
207 unsigned int i;
208
209 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
210 if (links == NULL)
211 return NULL;
212
213 for (i = 0; i < entity->num_links; i++)
214 links[i].reverse->reverse = &links[i];
215
216 entity->max_links = max_links;
217 entity->links = links;
218 }
219
220 return &entity->links[entity->num_links++];
221}
222
223int
224media_entity_create_link(struct media_entity *source, u16 source_pad,
225 struct media_entity *sink, u16 sink_pad, u32 flags)
226{
227 struct media_link *link;
228 struct media_link *backlink;
229
230 BUG_ON(source == NULL || sink == NULL);
231 BUG_ON(source_pad >= source->num_pads);
232 BUG_ON(sink_pad >= sink->num_pads);
233
234 link = media_entity_add_link(source);
235 if (link == NULL)
236 return -ENOMEM;
237
238 link->source = &source->pads[source_pad];
239 link->sink = &sink->pads[sink_pad];
240 link->flags = flags;
241
242 /* Create the backlink. Backlinks are used to help graph traversal and
243 * are not reported to userspace.
244 */
245 backlink = media_entity_add_link(sink);
246 if (backlink == NULL) {
247 source->num_links--;
248 return -ENOMEM;
249 }
250
251 backlink->source = &source->pads[source_pad];
252 backlink->sink = &sink->pads[sink_pad];
253 backlink->flags = flags;
254
255 link->reverse = backlink;
256 backlink->reverse = link;
257
258 sink->num_backlinks++;
259
260 return 0;
261}
262EXPORT_SYMBOL_GPL(media_entity_create_link);
This page took 0.034997 seconds and 5 git commands to generate.