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