net: add skb_dst_force() in sock_queue_err_skb()
[linux/fpc-iii.git] / drivers / media / media-entity.c
blob23640ed44d8536a20b7c9ee56a572c704c3a0050
1 /*
2 * Media entity
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>
26 #include <media/media-device.h>
28 /**
29 * media_entity_init - Initialize a media entity
31 * @num_pads: Total number of sink and source pads.
32 * @extra_links: Initial estimate of the number of extra links.
33 * @pads: Array of 'num_pads' pads.
35 * The total number of pads is an intrinsic property of entities known by the
36 * entity driver, while the total number of links depends on hardware design
37 * and is an extrinsic property unknown to the entity driver. However, in most
38 * use cases the entity driver can guess the number of links which can safely
39 * be assumed to be equal to or larger than the number of pads.
41 * For those reasons the links array can be preallocated based on the entity
42 * driver guess and will be reallocated later if extra links need to be
43 * created.
45 * This function allocates a links array with enough space to hold at least
46 * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will
47 * be set to the number of allocated elements.
49 * The pads array is managed by the entity driver and passed to
50 * media_entity_init() where its pointer will be stored in the entity structure.
52 int
53 media_entity_init(struct media_entity *entity, u16 num_pads,
54 struct media_pad *pads, u16 extra_links)
56 struct media_link *links;
57 unsigned int max_links = num_pads + extra_links;
58 unsigned int i;
60 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
61 if (links == NULL)
62 return -ENOMEM;
64 entity->group_id = 0;
65 entity->max_links = max_links;
66 entity->num_links = 0;
67 entity->num_backlinks = 0;
68 entity->num_pads = num_pads;
69 entity->pads = pads;
70 entity->links = links;
72 for (i = 0; i < num_pads; i++) {
73 pads[i].entity = entity;
74 pads[i].index = i;
77 return 0;
79 EXPORT_SYMBOL_GPL(media_entity_init);
81 void
82 media_entity_cleanup(struct media_entity *entity)
84 kfree(entity->links);
86 EXPORT_SYMBOL_GPL(media_entity_cleanup);
88 /* -----------------------------------------------------------------------------
89 * Graph traversal
92 static struct media_entity *
93 media_entity_other(struct media_entity *entity, struct media_link *link)
95 if (link->source->entity == entity)
96 return link->sink->entity;
97 else
98 return link->source->entity;
101 /* push an entity to traversal stack */
102 static void stack_push(struct media_entity_graph *graph,
103 struct media_entity *entity)
105 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
106 WARN_ON(1);
107 return;
109 graph->top++;
110 graph->stack[graph->top].link = 0;
111 graph->stack[graph->top].entity = entity;
114 static struct media_entity *stack_pop(struct media_entity_graph *graph)
116 struct media_entity *entity;
118 entity = graph->stack[graph->top].entity;
119 graph->top--;
121 return entity;
124 #define stack_peek(en) ((en)->stack[(en)->top - 1].entity)
125 #define link_top(en) ((en)->stack[(en)->top].link)
126 #define stack_top(en) ((en)->stack[(en)->top].entity)
129 * media_entity_graph_walk_start - Start walking the media graph at a given entity
130 * @graph: Media graph structure that will be used to walk the graph
131 * @entity: Starting entity
133 * This function initializes the graph traversal structure to walk the entities
134 * graph starting at the given entity. The traversal structure must not be
135 * modified by the caller during graph traversal. When done the structure can
136 * safely be freed.
138 void media_entity_graph_walk_start(struct media_entity_graph *graph,
139 struct media_entity *entity)
141 graph->top = 0;
142 graph->stack[graph->top].entity = NULL;
143 stack_push(graph, entity);
145 EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
148 * media_entity_graph_walk_next - Get the next entity in the graph
149 * @graph: Media graph structure
151 * Perform a depth-first traversal of the given media entities graph.
153 * The graph structure must have been previously initialized with a call to
154 * media_entity_graph_walk_start().
156 * Return the next entity in the graph or NULL if the whole graph have been
157 * traversed.
159 struct media_entity *
160 media_entity_graph_walk_next(struct media_entity_graph *graph)
162 if (stack_top(graph) == NULL)
163 return NULL;
166 * Depth first search. Push entity to stack and continue from
167 * top of the stack until no more entities on the level can be
168 * found.
170 while (link_top(graph) < stack_top(graph)->num_links) {
171 struct media_entity *entity = stack_top(graph);
172 struct media_link *link = &entity->links[link_top(graph)];
173 struct media_entity *next;
175 /* The link is not enabled so we do not follow. */
176 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
177 link_top(graph)++;
178 continue;
181 /* Get the entity in the other end of the link . */
182 next = media_entity_other(entity, link);
184 /* Was it the entity we came here from? */
185 if (next == stack_peek(graph)) {
186 link_top(graph)++;
187 continue;
190 /* Push the new entity to stack and start over. */
191 link_top(graph)++;
192 stack_push(graph, next);
195 return stack_pop(graph);
197 EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
199 /* -----------------------------------------------------------------------------
200 * Pipeline management
204 * media_entity_pipeline_start - Mark a pipeline as streaming
205 * @entity: Starting entity
206 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
208 * Mark all entities connected to a given entity through enabled links, either
209 * directly or indirectly, as streaming. The given pipeline object is assigned to
210 * every entity in the pipeline and stored in the media_entity pipe field.
212 * Calls to this function can be nested, in which case the same number of
213 * media_entity_pipeline_stop() calls will be required to stop streaming. The
214 * pipeline pointer must be identical for all nested calls to
215 * media_entity_pipeline_start().
217 void media_entity_pipeline_start(struct media_entity *entity,
218 struct media_pipeline *pipe)
220 struct media_device *mdev = entity->parent;
221 struct media_entity_graph graph;
223 mutex_lock(&mdev->graph_mutex);
225 media_entity_graph_walk_start(&graph, entity);
227 while ((entity = media_entity_graph_walk_next(&graph))) {
228 entity->stream_count++;
229 WARN_ON(entity->pipe && entity->pipe != pipe);
230 entity->pipe = pipe;
233 mutex_unlock(&mdev->graph_mutex);
235 EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
238 * media_entity_pipeline_stop - Mark a pipeline as not streaming
239 * @entity: Starting entity
241 * Mark all entities connected to a given entity through enabled links, either
242 * directly or indirectly, as not streaming. The media_entity pipe field is
243 * reset to NULL.
245 * If multiple calls to media_entity_pipeline_start() have been made, the same
246 * number of calls to this function are required to mark the pipeline as not
247 * streaming.
249 void media_entity_pipeline_stop(struct media_entity *entity)
251 struct media_device *mdev = entity->parent;
252 struct media_entity_graph graph;
254 mutex_lock(&mdev->graph_mutex);
256 media_entity_graph_walk_start(&graph, entity);
258 while ((entity = media_entity_graph_walk_next(&graph))) {
259 entity->stream_count--;
260 if (entity->stream_count == 0)
261 entity->pipe = NULL;
264 mutex_unlock(&mdev->graph_mutex);
266 EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
268 /* -----------------------------------------------------------------------------
269 * Module use count
273 * media_entity_get - Get a reference to the parent module
274 * @entity: The entity
276 * Get a reference to the parent media device module.
278 * The function will return immediately if @entity is NULL.
280 * Return a pointer to the entity on success or NULL on failure.
282 struct media_entity *media_entity_get(struct media_entity *entity)
284 if (entity == NULL)
285 return NULL;
287 if (entity->parent->dev &&
288 !try_module_get(entity->parent->dev->driver->owner))
289 return NULL;
291 return entity;
293 EXPORT_SYMBOL_GPL(media_entity_get);
296 * media_entity_put - Release the reference to the parent module
297 * @entity: The entity
299 * Release the reference count acquired by media_entity_get().
301 * The function will return immediately if @entity is NULL.
303 void media_entity_put(struct media_entity *entity)
305 if (entity == NULL)
306 return;
308 if (entity->parent->dev)
309 module_put(entity->parent->dev->driver->owner);
311 EXPORT_SYMBOL_GPL(media_entity_put);
313 /* -----------------------------------------------------------------------------
314 * Links management
317 static struct media_link *media_entity_add_link(struct media_entity *entity)
319 if (entity->num_links >= entity->max_links) {
320 struct media_link *links = entity->links;
321 unsigned int max_links = entity->max_links + 2;
322 unsigned int i;
324 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
325 if (links == NULL)
326 return NULL;
328 for (i = 0; i < entity->num_links; i++)
329 links[i].reverse->reverse = &links[i];
331 entity->max_links = max_links;
332 entity->links = links;
335 return &entity->links[entity->num_links++];
339 media_entity_create_link(struct media_entity *source, u16 source_pad,
340 struct media_entity *sink, u16 sink_pad, u32 flags)
342 struct media_link *link;
343 struct media_link *backlink;
345 BUG_ON(source == NULL || sink == NULL);
346 BUG_ON(source_pad >= source->num_pads);
347 BUG_ON(sink_pad >= sink->num_pads);
349 link = media_entity_add_link(source);
350 if (link == NULL)
351 return -ENOMEM;
353 link->source = &source->pads[source_pad];
354 link->sink = &sink->pads[sink_pad];
355 link->flags = flags;
357 /* Create the backlink. Backlinks are used to help graph traversal and
358 * are not reported to userspace.
360 backlink = media_entity_add_link(sink);
361 if (backlink == NULL) {
362 source->num_links--;
363 return -ENOMEM;
366 backlink->source = &source->pads[source_pad];
367 backlink->sink = &sink->pads[sink_pad];
368 backlink->flags = flags;
370 link->reverse = backlink;
371 backlink->reverse = link;
373 sink->num_backlinks++;
375 return 0;
377 EXPORT_SYMBOL_GPL(media_entity_create_link);
379 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
381 const u32 mask = MEDIA_LNK_FL_ENABLED;
382 int ret;
384 /* Notify both entities. */
385 ret = media_entity_call(link->source->entity, link_setup,
386 link->source, link->sink, flags);
387 if (ret < 0 && ret != -ENOIOCTLCMD)
388 return ret;
390 ret = media_entity_call(link->sink->entity, link_setup,
391 link->sink, link->source, flags);
392 if (ret < 0 && ret != -ENOIOCTLCMD) {
393 media_entity_call(link->source->entity, link_setup,
394 link->source, link->sink, link->flags);
395 return ret;
398 link->flags = (link->flags & ~mask) | (flags & mask);
399 link->reverse->flags = link->flags;
401 return 0;
405 * __media_entity_setup_link - Configure a media link
406 * @link: The link being configured
407 * @flags: Link configuration flags
409 * The bulk of link setup is handled by the two entities connected through the
410 * link. This function notifies both entities of the link configuration change.
412 * If the link is immutable or if the current and new configuration are
413 * identical, return immediately.
415 * The user is expected to hold link->source->parent->mutex. If not,
416 * media_entity_setup_link() should be used instead.
418 int __media_entity_setup_link(struct media_link *link, u32 flags)
420 struct media_device *mdev;
421 struct media_entity *source, *sink;
422 int ret = -EBUSY;
424 if (link == NULL)
425 return -EINVAL;
427 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
428 return link->flags == flags ? 0 : -EINVAL;
430 if (link->flags == flags)
431 return 0;
433 source = link->source->entity;
434 sink = link->sink->entity;
436 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
437 (source->stream_count || sink->stream_count))
438 return -EBUSY;
440 mdev = source->parent;
442 if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) {
443 ret = mdev->link_notify(link->source, link->sink,
444 MEDIA_LNK_FL_ENABLED);
445 if (ret < 0)
446 return ret;
449 ret = __media_entity_setup_link_notify(link, flags);
450 if (ret < 0)
451 goto err;
453 if (!(flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
454 mdev->link_notify(link->source, link->sink, 0);
456 return 0;
458 err:
459 if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
460 mdev->link_notify(link->source, link->sink, 0);
462 return ret;
465 int media_entity_setup_link(struct media_link *link, u32 flags)
467 int ret;
469 mutex_lock(&link->source->entity->parent->graph_mutex);
470 ret = __media_entity_setup_link(link, flags);
471 mutex_unlock(&link->source->entity->parent->graph_mutex);
473 return ret;
475 EXPORT_SYMBOL_GPL(media_entity_setup_link);
478 * media_entity_find_link - Find a link between two pads
479 * @source: Source pad
480 * @sink: Sink pad
482 * Return a pointer to the link between the two entities. If no such link
483 * exists, return NULL.
485 struct media_link *
486 media_entity_find_link(struct media_pad *source, struct media_pad *sink)
488 struct media_link *link;
489 unsigned int i;
491 for (i = 0; i < source->entity->num_links; ++i) {
492 link = &source->entity->links[i];
494 if (link->source->entity == source->entity &&
495 link->source->index == source->index &&
496 link->sink->entity == sink->entity &&
497 link->sink->index == sink->index)
498 return link;
501 return NULL;
503 EXPORT_SYMBOL_GPL(media_entity_find_link);
506 * media_entity_remote_source - Find the source pad at the remote end of a link
507 * @pad: Sink pad at the local end of the link
509 * Search for a remote source pad connected to the given sink pad by iterating
510 * over all links originating or terminating at that pad until an enabled link
511 * is found.
513 * Return a pointer to the pad at the remote end of the first found enabled
514 * link, or NULL if no enabled link has been found.
516 struct media_pad *media_entity_remote_source(struct media_pad *pad)
518 unsigned int i;
520 for (i = 0; i < pad->entity->num_links; i++) {
521 struct media_link *link = &pad->entity->links[i];
523 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
524 continue;
526 if (link->source == pad)
527 return link->sink;
529 if (link->sink == pad)
530 return link->source;
533 return NULL;
536 EXPORT_SYMBOL_GPL(media_entity_remote_source);