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