Commit | Line | Data |
---|---|---|
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 | ||
5c7b25b9 | 23 | #include <linux/bitmap.h> |
53e269c1 LP |
24 | #include <linux/module.h> |
25 | #include <linux/slab.h> | |
26 | #include <media/media-entity.h> | |
503c3d82 | 27 | #include <media/media-device.h> |
53e269c1 | 28 | |
39a956c4 MCC |
29 | /** |
30 | * dev_dbg_obj - Prints in debug mode a change on some object | |
31 | * | |
32 | * @event_name: Name of the event to report. Could be __func__ | |
33 | * @gobj: Pointer to the object | |
34 | * | |
35 | * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it | |
36 | * won't produce any code. | |
37 | */ | |
38 | static inline const char *gobj_type(enum media_gobj_type type) | |
39 | { | |
40 | switch (type) { | |
41 | case MEDIA_GRAPH_ENTITY: | |
42 | return "entity"; | |
43 | case MEDIA_GRAPH_PAD: | |
44 | return "pad"; | |
45 | case MEDIA_GRAPH_LINK: | |
46 | return "link"; | |
47 | default: | |
48 | return "unknown"; | |
49 | } | |
50 | } | |
51 | ||
52 | static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) | |
53 | { | |
54 | #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG) | |
55 | switch (media_type(gobj)) { | |
56 | case MEDIA_GRAPH_ENTITY: | |
57 | dev_dbg(gobj->mdev->dev, | |
58 | "%s: id 0x%08x entity#%d: '%s'\n", | |
59 | event_name, gobj->id, media_localid(gobj), | |
60 | gobj_to_entity(gobj)->name); | |
61 | break; | |
62 | case MEDIA_GRAPH_LINK: | |
63 | { | |
64 | struct media_link *link = gobj_to_link(gobj); | |
65 | ||
66 | dev_dbg(gobj->mdev->dev, | |
67 | "%s: id 0x%08x link#%d: '%s' %s#%d ==> '%s' %s#%d\n", | |
68 | event_name, gobj->id, media_localid(gobj), | |
69 | ||
70 | link->source->entity->name, | |
71 | gobj_type(media_type(&link->source->graph_obj)), | |
72 | media_localid(&link->source->graph_obj), | |
73 | ||
74 | link->sink->entity->name, | |
75 | gobj_type(media_type(&link->sink->graph_obj)), | |
76 | media_localid(&link->sink->graph_obj)); | |
77 | break; | |
78 | } | |
79 | case MEDIA_GRAPH_PAD: | |
80 | { | |
81 | struct media_pad *pad = gobj_to_pad(gobj); | |
82 | ||
83 | dev_dbg(gobj->mdev->dev, | |
84 | "%s: id 0x%08x pad#%d: '%s':%d\n", | |
85 | event_name, gobj->id, media_localid(gobj), | |
86 | pad->entity->name, pad->index); | |
87 | } | |
88 | } | |
89 | #endif | |
90 | } | |
91 | ||
ec6e4c95 MCC |
92 | /** |
93 | * media_gobj_init - Initialize a graph object | |
94 | * | |
95 | * @mdev: Pointer to the media_device that contains the object | |
96 | * @type: Type of the object | |
97 | * @gobj: Pointer to the object | |
98 | * | |
99 | * This routine initializes the embedded struct media_gobj inside a | |
100 | * media graph object. It is called automatically if media_*_create() | |
101 | * calls are used. However, if the object (entity, link, pad, interface) | |
102 | * is embedded on some other object, this function should be called before | |
103 | * registering the object at the media controller. | |
104 | */ | |
105 | void media_gobj_init(struct media_device *mdev, | |
106 | enum media_gobj_type type, | |
107 | struct media_gobj *gobj) | |
108 | { | |
39a956c4 MCC |
109 | gobj->mdev = mdev; |
110 | ||
bfab2aac MCC |
111 | /* Create a per-type unique object ID */ |
112 | switch (type) { | |
113 | case MEDIA_GRAPH_ENTITY: | |
114 | gobj->id = media_gobj_gen_id(type, ++mdev->entity_id); | |
115 | break; | |
18710dc6 MCC |
116 | case MEDIA_GRAPH_PAD: |
117 | gobj->id = media_gobj_gen_id(type, ++mdev->pad_id); | |
118 | break; | |
6b6a4278 MCC |
119 | case MEDIA_GRAPH_LINK: |
120 | gobj->id = media_gobj_gen_id(type, ++mdev->link_id); | |
121 | break; | |
bfab2aac | 122 | } |
39a956c4 | 123 | dev_dbg_obj(__func__, gobj); |
ec6e4c95 MCC |
124 | } |
125 | ||
126 | /** | |
127 | * media_gobj_remove - Stop using a graph object on a media device | |
128 | * | |
129 | * @graph_obj: Pointer to the object | |
130 | * | |
131 | * This should be called at media_device_unregister_*() routines | |
132 | */ | |
133 | void media_gobj_remove(struct media_gobj *gobj) | |
134 | { | |
39a956c4 | 135 | dev_dbg_obj(__func__, gobj); |
ec6e4c95 MCC |
136 | } |
137 | ||
53e269c1 LP |
138 | /** |
139 | * media_entity_init - Initialize a media entity | |
140 | * | |
141 | * @num_pads: Total number of sink and source pads. | |
53e269c1 LP |
142 | * @pads: Array of 'num_pads' pads. |
143 | * | |
144 | * The total number of pads is an intrinsic property of entities known by the | |
145 | * entity driver, while the total number of links depends on hardware design | |
146 | * and is an extrinsic property unknown to the entity driver. However, in most | |
18095107 MCC |
147 | * use cases the number of links can safely be assumed to be equal to or |
148 | * larger than the number of pads. | |
53e269c1 | 149 | * |
18095107 MCC |
150 | * For those reasons the links array can be preallocated based on the number |
151 | * of pads and will be reallocated later if extra links need to be created. | |
53e269c1 LP |
152 | * |
153 | * This function allocates a links array with enough space to hold at least | |
18095107 MCC |
154 | * 'num_pads' elements. The media_entity::max_links field will be set to the |
155 | * number of allocated elements. | |
53e269c1 LP |
156 | * |
157 | * The pads array is managed by the entity driver and passed to | |
158 | * media_entity_init() where its pointer will be stored in the entity structure. | |
159 | */ | |
160 | int | |
161 | media_entity_init(struct media_entity *entity, u16 num_pads, | |
18095107 | 162 | struct media_pad *pads) |
53e269c1 LP |
163 | { |
164 | struct media_link *links; | |
18095107 | 165 | unsigned int max_links = num_pads; |
53e269c1 LP |
166 | unsigned int i; |
167 | ||
168 | links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL); | |
169 | if (links == NULL) | |
170 | return -ENOMEM; | |
171 | ||
172 | entity->group_id = 0; | |
173 | entity->max_links = max_links; | |
174 | entity->num_links = 0; | |
175 | entity->num_backlinks = 0; | |
176 | entity->num_pads = num_pads; | |
177 | entity->pads = pads; | |
178 | entity->links = links; | |
179 | ||
180 | for (i = 0; i < num_pads; i++) { | |
181 | pads[i].entity = entity; | |
182 | pads[i].index = i; | |
183 | } | |
184 | ||
185 | return 0; | |
186 | } | |
187 | EXPORT_SYMBOL_GPL(media_entity_init); | |
188 | ||
189 | void | |
190 | media_entity_cleanup(struct media_entity *entity) | |
191 | { | |
192 | kfree(entity->links); | |
193 | } | |
194 | EXPORT_SYMBOL_GPL(media_entity_cleanup); | |
195 | ||
a5ccc48a SA |
196 | /* ----------------------------------------------------------------------------- |
197 | * Graph traversal | |
198 | */ | |
199 | ||
200 | static struct media_entity * | |
201 | media_entity_other(struct media_entity *entity, struct media_link *link) | |
202 | { | |
203 | if (link->source->entity == entity) | |
204 | return link->sink->entity; | |
205 | else | |
206 | return link->source->entity; | |
207 | } | |
208 | ||
209 | /* push an entity to traversal stack */ | |
210 | static void stack_push(struct media_entity_graph *graph, | |
211 | struct media_entity *entity) | |
212 | { | |
213 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { | |
214 | WARN_ON(1); | |
215 | return; | |
216 | } | |
217 | graph->top++; | |
218 | graph->stack[graph->top].link = 0; | |
219 | graph->stack[graph->top].entity = entity; | |
220 | } | |
221 | ||
222 | static struct media_entity *stack_pop(struct media_entity_graph *graph) | |
223 | { | |
224 | struct media_entity *entity; | |
225 | ||
226 | entity = graph->stack[graph->top].entity; | |
227 | graph->top--; | |
228 | ||
229 | return entity; | |
230 | } | |
231 | ||
a5ccc48a SA |
232 | #define link_top(en) ((en)->stack[(en)->top].link) |
233 | #define stack_top(en) ((en)->stack[(en)->top].entity) | |
234 | ||
235 | /** | |
236 | * media_entity_graph_walk_start - Start walking the media graph at a given entity | |
237 | * @graph: Media graph structure that will be used to walk the graph | |
238 | * @entity: Starting entity | |
239 | * | |
240 | * This function initializes the graph traversal structure to walk the entities | |
241 | * graph starting at the given entity. The traversal structure must not be | |
242 | * modified by the caller during graph traversal. When done the structure can | |
243 | * safely be freed. | |
244 | */ | |
245 | void media_entity_graph_walk_start(struct media_entity_graph *graph, | |
246 | struct media_entity *entity) | |
247 | { | |
248 | graph->top = 0; | |
249 | graph->stack[graph->top].entity = NULL; | |
5c7b25b9 LP |
250 | bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID); |
251 | ||
fa762394 | 252 | if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
5c7b25b9 LP |
253 | return; |
254 | ||
fa762394 | 255 | __set_bit(media_entity_id(entity), graph->entities); |
a5ccc48a SA |
256 | stack_push(graph, entity); |
257 | } | |
258 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_start); | |
259 | ||
260 | /** | |
261 | * media_entity_graph_walk_next - Get the next entity in the graph | |
262 | * @graph: Media graph structure | |
263 | * | |
264 | * Perform a depth-first traversal of the given media entities graph. | |
265 | * | |
266 | * The graph structure must have been previously initialized with a call to | |
267 | * media_entity_graph_walk_start(). | |
268 | * | |
269 | * Return the next entity in the graph or NULL if the whole graph have been | |
270 | * traversed. | |
271 | */ | |
272 | struct media_entity * | |
273 | media_entity_graph_walk_next(struct media_entity_graph *graph) | |
274 | { | |
275 | if (stack_top(graph) == NULL) | |
276 | return NULL; | |
277 | ||
278 | /* | |
279 | * Depth first search. Push entity to stack and continue from | |
280 | * top of the stack until no more entities on the level can be | |
281 | * found. | |
282 | */ | |
283 | while (link_top(graph) < stack_top(graph)->num_links) { | |
284 | struct media_entity *entity = stack_top(graph); | |
285 | struct media_link *link = &entity->links[link_top(graph)]; | |
286 | struct media_entity *next; | |
287 | ||
288 | /* The link is not enabled so we do not follow. */ | |
289 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { | |
290 | link_top(graph)++; | |
291 | continue; | |
292 | } | |
293 | ||
294 | /* Get the entity in the other end of the link . */ | |
295 | next = media_entity_other(entity, link); | |
fa762394 | 296 | if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
5c7b25b9 | 297 | return NULL; |
a5ccc48a | 298 | |
5c7b25b9 | 299 | /* Has the entity already been visited? */ |
fa762394 | 300 | if (__test_and_set_bit(media_entity_id(next), graph->entities)) { |
a5ccc48a SA |
301 | link_top(graph)++; |
302 | continue; | |
303 | } | |
304 | ||
305 | /* Push the new entity to stack and start over. */ | |
306 | link_top(graph)++; | |
307 | stack_push(graph, next); | |
308 | } | |
309 | ||
310 | return stack_pop(graph); | |
311 | } | |
312 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); | |
313 | ||
e02188c9 LP |
314 | /* ----------------------------------------------------------------------------- |
315 | * Pipeline management | |
316 | */ | |
317 | ||
318 | /** | |
319 | * media_entity_pipeline_start - Mark a pipeline as streaming | |
320 | * @entity: Starting entity | |
321 | * @pipe: Media pipeline to be assigned to all entities in the pipeline. | |
322 | * | |
323 | * Mark all entities connected to a given entity through enabled links, either | |
324 | * directly or indirectly, as streaming. The given pipeline object is assigned to | |
325 | * every entity in the pipeline and stored in the media_entity pipe field. | |
326 | * | |
327 | * Calls to this function can be nested, in which case the same number of | |
328 | * media_entity_pipeline_stop() calls will be required to stop streaming. The | |
329 | * pipeline pointer must be identical for all nested calls to | |
330 | * media_entity_pipeline_start(). | |
331 | */ | |
af88be38 SA |
332 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
333 | struct media_pipeline *pipe) | |
e02188c9 | 334 | { |
d10c9894 | 335 | struct media_device *mdev = entity->graph_obj.mdev; |
e02188c9 | 336 | struct media_entity_graph graph; |
af88be38 SA |
337 | struct media_entity *entity_err = entity; |
338 | int ret; | |
e02188c9 LP |
339 | |
340 | mutex_lock(&mdev->graph_mutex); | |
341 | ||
342 | media_entity_graph_walk_start(&graph, entity); | |
343 | ||
344 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
ef69ee1b MCC |
345 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
346 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); | |
af88be38 SA |
347 | unsigned int i; |
348 | ||
e02188c9 LP |
349 | entity->stream_count++; |
350 | WARN_ON(entity->pipe && entity->pipe != pipe); | |
351 | entity->pipe = pipe; | |
af88be38 SA |
352 | |
353 | /* Already streaming --- no need to check. */ | |
354 | if (entity->stream_count > 1) | |
355 | continue; | |
356 | ||
357 | if (!entity->ops || !entity->ops->link_validate) | |
358 | continue; | |
359 | ||
de49c285 SA |
360 | bitmap_zero(active, entity->num_pads); |
361 | bitmap_fill(has_no_links, entity->num_pads); | |
362 | ||
af88be38 SA |
363 | for (i = 0; i < entity->num_links; i++) { |
364 | struct media_link *link = &entity->links[i]; | |
de49c285 SA |
365 | struct media_pad *pad = link->sink->entity == entity |
366 | ? link->sink : link->source; | |
367 | ||
368 | /* Mark that a pad is connected by a link. */ | |
369 | bitmap_clear(has_no_links, pad->index, 1); | |
370 | ||
371 | /* | |
372 | * Pads that either do not need to connect or | |
373 | * are connected through an enabled link are | |
374 | * fine. | |
375 | */ | |
376 | if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || | |
377 | link->flags & MEDIA_LNK_FL_ENABLED) | |
378 | bitmap_set(active, pad->index, 1); | |
379 | ||
380 | /* | |
381 | * Link validation will only take place for | |
382 | * sink ends of the link that are enabled. | |
383 | */ | |
384 | if (link->sink != pad || | |
385 | !(link->flags & MEDIA_LNK_FL_ENABLED)) | |
af88be38 SA |
386 | continue; |
387 | ||
388 | ret = entity->ops->link_validate(link); | |
fab9d30b | 389 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
d10c9894 | 390 | dev_dbg(entity->graph_obj.mdev->dev, |
fab9d30b | 391 | "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n", |
823ea2a6 SA |
392 | link->source->entity->name, |
393 | link->source->index, | |
394 | entity->name, link->sink->index, ret); | |
af88be38 | 395 | goto error; |
fab9d30b | 396 | } |
af88be38 | 397 | } |
de49c285 SA |
398 | |
399 | /* Either no links or validated links are fine. */ | |
400 | bitmap_or(active, active, has_no_links, entity->num_pads); | |
401 | ||
402 | if (!bitmap_full(active, entity->num_pads)) { | |
403 | ret = -EPIPE; | |
d10c9894 | 404 | dev_dbg(entity->graph_obj.mdev->dev, |
fab9d30b SA |
405 | "\"%s\":%u must be connected by an enabled link\n", |
406 | entity->name, | |
094f1ca5 SA |
407 | (unsigned)find_first_zero_bit( |
408 | active, entity->num_pads)); | |
de49c285 SA |
409 | goto error; |
410 | } | |
e02188c9 LP |
411 | } |
412 | ||
413 | mutex_unlock(&mdev->graph_mutex); | |
af88be38 SA |
414 | |
415 | return 0; | |
416 | ||
417 | error: | |
418 | /* | |
419 | * Link validation on graph failed. We revert what we did and | |
420 | * return the error. | |
421 | */ | |
422 | media_entity_graph_walk_start(&graph, entity_err); | |
423 | ||
424 | while ((entity_err = media_entity_graph_walk_next(&graph))) { | |
425 | entity_err->stream_count--; | |
426 | if (entity_err->stream_count == 0) | |
427 | entity_err->pipe = NULL; | |
428 | ||
429 | /* | |
430 | * We haven't increased stream_count further than this | |
431 | * so we quit here. | |
432 | */ | |
433 | if (entity_err == entity) | |
434 | break; | |
435 | } | |
436 | ||
437 | mutex_unlock(&mdev->graph_mutex); | |
438 | ||
439 | return ret; | |
e02188c9 LP |
440 | } |
441 | EXPORT_SYMBOL_GPL(media_entity_pipeline_start); | |
442 | ||
443 | /** | |
444 | * media_entity_pipeline_stop - Mark a pipeline as not streaming | |
445 | * @entity: Starting entity | |
446 | * | |
447 | * Mark all entities connected to a given entity through enabled links, either | |
448 | * directly or indirectly, as not streaming. The media_entity pipe field is | |
449 | * reset to NULL. | |
450 | * | |
451 | * If multiple calls to media_entity_pipeline_start() have been made, the same | |
452 | * number of calls to this function are required to mark the pipeline as not | |
453 | * streaming. | |
454 | */ | |
455 | void media_entity_pipeline_stop(struct media_entity *entity) | |
456 | { | |
d10c9894 | 457 | struct media_device *mdev = entity->graph_obj.mdev; |
e02188c9 LP |
458 | struct media_entity_graph graph; |
459 | ||
460 | mutex_lock(&mdev->graph_mutex); | |
461 | ||
462 | media_entity_graph_walk_start(&graph, entity); | |
463 | ||
464 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
465 | entity->stream_count--; | |
466 | if (entity->stream_count == 0) | |
467 | entity->pipe = NULL; | |
468 | } | |
469 | ||
470 | mutex_unlock(&mdev->graph_mutex); | |
471 | } | |
472 | EXPORT_SYMBOL_GPL(media_entity_pipeline_stop); | |
473 | ||
503c3d82 LP |
474 | /* ----------------------------------------------------------------------------- |
475 | * Module use count | |
476 | */ | |
477 | ||
478 | /* | |
479 | * media_entity_get - Get a reference to the parent module | |
480 | * @entity: The entity | |
481 | * | |
482 | * Get a reference to the parent media device module. | |
483 | * | |
484 | * The function will return immediately if @entity is NULL. | |
485 | * | |
486 | * Return a pointer to the entity on success or NULL on failure. | |
487 | */ | |
488 | struct media_entity *media_entity_get(struct media_entity *entity) | |
489 | { | |
490 | if (entity == NULL) | |
491 | return NULL; | |
492 | ||
d10c9894 JMC |
493 | if (entity->graph_obj.mdev->dev && |
494 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) | |
503c3d82 LP |
495 | return NULL; |
496 | ||
497 | return entity; | |
498 | } | |
499 | EXPORT_SYMBOL_GPL(media_entity_get); | |
500 | ||
501 | /* | |
502 | * media_entity_put - Release the reference to the parent module | |
503 | * @entity: The entity | |
504 | * | |
505 | * Release the reference count acquired by media_entity_get(). | |
506 | * | |
507 | * The function will return immediately if @entity is NULL. | |
508 | */ | |
509 | void media_entity_put(struct media_entity *entity) | |
510 | { | |
511 | if (entity == NULL) | |
512 | return; | |
513 | ||
d10c9894 JMC |
514 | if (entity->graph_obj.mdev->dev) |
515 | module_put(entity->graph_obj.mdev->dev->driver->owner); | |
503c3d82 LP |
516 | } |
517 | EXPORT_SYMBOL_GPL(media_entity_put); | |
518 | ||
a5ccc48a SA |
519 | /* ----------------------------------------------------------------------------- |
520 | * Links management | |
521 | */ | |
522 | ||
53e269c1 LP |
523 | static struct media_link *media_entity_add_link(struct media_entity *entity) |
524 | { | |
525 | if (entity->num_links >= entity->max_links) { | |
526 | struct media_link *links = entity->links; | |
527 | unsigned int max_links = entity->max_links + 2; | |
528 | unsigned int i; | |
529 | ||
530 | links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL); | |
531 | if (links == NULL) | |
532 | return NULL; | |
533 | ||
534 | for (i = 0; i < entity->num_links; i++) | |
535 | links[i].reverse->reverse = &links[i]; | |
536 | ||
537 | entity->max_links = max_links; | |
538 | entity->links = links; | |
539 | } | |
540 | ||
541 | return &entity->links[entity->num_links++]; | |
542 | } | |
543 | ||
544 | int | |
8df00a15 | 545 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
53e269c1 LP |
546 | struct media_entity *sink, u16 sink_pad, u32 flags) |
547 | { | |
548 | struct media_link *link; | |
549 | struct media_link *backlink; | |
550 | ||
551 | BUG_ON(source == NULL || sink == NULL); | |
552 | BUG_ON(source_pad >= source->num_pads); | |
553 | BUG_ON(sink_pad >= sink->num_pads); | |
554 | ||
555 | link = media_entity_add_link(source); | |
556 | if (link == NULL) | |
557 | return -ENOMEM; | |
558 | ||
559 | link->source = &source->pads[source_pad]; | |
560 | link->sink = &sink->pads[sink_pad]; | |
561 | link->flags = flags; | |
562 | ||
6b6a4278 | 563 | /* Initialize graph object embedded at the new link */ |
d10c9894 JMC |
564 | media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
565 | &link->graph_obj); | |
6b6a4278 | 566 | |
53e269c1 LP |
567 | /* Create the backlink. Backlinks are used to help graph traversal and |
568 | * are not reported to userspace. | |
569 | */ | |
570 | backlink = media_entity_add_link(sink); | |
571 | if (backlink == NULL) { | |
572 | source->num_links--; | |
573 | return -ENOMEM; | |
574 | } | |
575 | ||
576 | backlink->source = &source->pads[source_pad]; | |
577 | backlink->sink = &sink->pads[sink_pad]; | |
578 | backlink->flags = flags; | |
579 | ||
6b6a4278 | 580 | /* Initialize graph object embedded at the new link */ |
d10c9894 JMC |
581 | media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
582 | &backlink->graph_obj); | |
6b6a4278 | 583 | |
53e269c1 LP |
584 | link->reverse = backlink; |
585 | backlink->reverse = link; | |
586 | ||
587 | sink->num_backlinks++; | |
588 | ||
589 | return 0; | |
590 | } | |
8df00a15 | 591 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
97548ed4 | 592 | |
7349cec1 SN |
593 | void __media_entity_remove_links(struct media_entity *entity) |
594 | { | |
595 | unsigned int i; | |
596 | ||
597 | for (i = 0; i < entity->num_links; i++) { | |
598 | struct media_link *link = &entity->links[i]; | |
599 | struct media_entity *remote; | |
600 | unsigned int r = 0; | |
601 | ||
602 | if (link->source->entity == entity) | |
603 | remote = link->sink->entity; | |
604 | else | |
605 | remote = link->source->entity; | |
606 | ||
607 | while (r < remote->num_links) { | |
608 | struct media_link *rlink = &remote->links[r]; | |
609 | ||
610 | if (rlink != link->reverse) { | |
611 | r++; | |
612 | continue; | |
613 | } | |
614 | ||
615 | if (link->source->entity == entity) | |
616 | remote->num_backlinks--; | |
617 | ||
618 | if (--remote->num_links == 0) | |
619 | break; | |
620 | ||
621 | /* Insert last entry in place of the dropped link. */ | |
622 | *rlink = remote->links[remote->num_links]; | |
623 | } | |
624 | } | |
625 | ||
626 | entity->num_links = 0; | |
627 | entity->num_backlinks = 0; | |
628 | } | |
629 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); | |
630 | ||
631 | void media_entity_remove_links(struct media_entity *entity) | |
632 | { | |
633 | /* Do nothing if the entity is not registered. */ | |
d10c9894 | 634 | if (entity->graph_obj.mdev == NULL) |
7349cec1 SN |
635 | return; |
636 | ||
d10c9894 | 637 | mutex_lock(&entity->graph_obj.mdev->graph_mutex); |
7349cec1 | 638 | __media_entity_remove_links(entity); |
d10c9894 | 639 | mutex_unlock(&entity->graph_obj.mdev->graph_mutex); |
7349cec1 SN |
640 | } |
641 | EXPORT_SYMBOL_GPL(media_entity_remove_links); | |
642 | ||
97548ed4 LP |
643 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
644 | { | |
97548ed4 LP |
645 | int ret; |
646 | ||
647 | /* Notify both entities. */ | |
648 | ret = media_entity_call(link->source->entity, link_setup, | |
649 | link->source, link->sink, flags); | |
650 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
651 | return ret; | |
652 | ||
653 | ret = media_entity_call(link->sink->entity, link_setup, | |
654 | link->sink, link->source, flags); | |
655 | if (ret < 0 && ret != -ENOIOCTLCMD) { | |
656 | media_entity_call(link->source->entity, link_setup, | |
657 | link->source, link->sink, link->flags); | |
658 | return ret; | |
659 | } | |
660 | ||
7a6f0b22 | 661 | link->flags = flags; |
97548ed4 LP |
662 | link->reverse->flags = link->flags; |
663 | ||
664 | return 0; | |
665 | } | |
666 | ||
667 | /** | |
668 | * __media_entity_setup_link - Configure a media link | |
669 | * @link: The link being configured | |
670 | * @flags: Link configuration flags | |
671 | * | |
672 | * The bulk of link setup is handled by the two entities connected through the | |
673 | * link. This function notifies both entities of the link configuration change. | |
674 | * | |
675 | * If the link is immutable or if the current and new configuration are | |
676 | * identical, return immediately. | |
677 | * | |
678 | * The user is expected to hold link->source->parent->mutex. If not, | |
679 | * media_entity_setup_link() should be used instead. | |
680 | */ | |
681 | int __media_entity_setup_link(struct media_link *link, u32 flags) | |
682 | { | |
7a6f0b22 | 683 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
97548ed4 LP |
684 | struct media_device *mdev; |
685 | struct media_entity *source, *sink; | |
686 | int ret = -EBUSY; | |
687 | ||
688 | if (link == NULL) | |
689 | return -EINVAL; | |
690 | ||
7a6f0b22 LP |
691 | /* The non-modifiable link flags must not be modified. */ |
692 | if ((link->flags & ~mask) != (flags & ~mask)) | |
693 | return -EINVAL; | |
694 | ||
97548ed4 LP |
695 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
696 | return link->flags == flags ? 0 : -EINVAL; | |
697 | ||
698 | if (link->flags == flags) | |
699 | return 0; | |
700 | ||
701 | source = link->source->entity; | |
702 | sink = link->sink->entity; | |
703 | ||
e02188c9 LP |
704 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
705 | (source->stream_count || sink->stream_count)) | |
706 | return -EBUSY; | |
707 | ||
d10c9894 | 708 | mdev = source->graph_obj.mdev; |
97548ed4 | 709 | |
813f5c0a SN |
710 | if (mdev->link_notify) { |
711 | ret = mdev->link_notify(link, flags, | |
712 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); | |
97548ed4 LP |
713 | if (ret < 0) |
714 | return ret; | |
715 | } | |
716 | ||
717 | ret = __media_entity_setup_link_notify(link, flags); | |
97548ed4 | 718 | |
813f5c0a SN |
719 | if (mdev->link_notify) |
720 | mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH); | |
97548ed4 LP |
721 | |
722 | return ret; | |
723 | } | |
724 | ||
725 | int media_entity_setup_link(struct media_link *link, u32 flags) | |
726 | { | |
727 | int ret; | |
728 | ||
d10c9894 | 729 | mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex); |
97548ed4 | 730 | ret = __media_entity_setup_link(link, flags); |
d10c9894 | 731 | mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex); |
97548ed4 LP |
732 | |
733 | return ret; | |
734 | } | |
735 | EXPORT_SYMBOL_GPL(media_entity_setup_link); | |
736 | ||
737 | /** | |
738 | * media_entity_find_link - Find a link between two pads | |
739 | * @source: Source pad | |
740 | * @sink: Sink pad | |
741 | * | |
742 | * Return a pointer to the link between the two entities. If no such link | |
743 | * exists, return NULL. | |
744 | */ | |
745 | struct media_link * | |
746 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) | |
747 | { | |
748 | struct media_link *link; | |
749 | unsigned int i; | |
750 | ||
751 | for (i = 0; i < source->entity->num_links; ++i) { | |
752 | link = &source->entity->links[i]; | |
753 | ||
754 | if (link->source->entity == source->entity && | |
755 | link->source->index == source->index && | |
756 | link->sink->entity == sink->entity && | |
757 | link->sink->index == sink->index) | |
758 | return link; | |
759 | } | |
760 | ||
761 | return NULL; | |
762 | } | |
763 | EXPORT_SYMBOL_GPL(media_entity_find_link); | |
764 | ||
765 | /** | |
1bddf1b3 AH |
766 | * media_entity_remote_pad - Find the pad at the remote end of a link |
767 | * @pad: Pad at the local end of the link | |
97548ed4 | 768 | * |
1bddf1b3 AH |
769 | * Search for a remote pad connected to the given pad by iterating over all |
770 | * links originating or terminating at that pad until an enabled link is found. | |
97548ed4 LP |
771 | * |
772 | * Return a pointer to the pad at the remote end of the first found enabled | |
773 | * link, or NULL if no enabled link has been found. | |
774 | */ | |
1bddf1b3 | 775 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
97548ed4 LP |
776 | { |
777 | unsigned int i; | |
778 | ||
779 | for (i = 0; i < pad->entity->num_links; i++) { | |
780 | struct media_link *link = &pad->entity->links[i]; | |
781 | ||
782 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) | |
783 | continue; | |
784 | ||
785 | if (link->source == pad) | |
786 | return link->sink; | |
787 | ||
788 | if (link->sink == pad) | |
789 | return link->source; | |
790 | } | |
791 | ||
792 | return NULL; | |
793 | ||
794 | } | |
1bddf1b3 | 795 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |