]>
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 LP |
334 | { |
335 | struct media_device *mdev = entity->parent; | |
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 SA |
389 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
390 | dev_dbg(entity->parent->dev, | |
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; | |
fab9d30b SA |
404 | dev_dbg(entity->parent->dev, |
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 | { | |
457 | struct media_device *mdev = entity->parent; | |
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 | ||
493 | if (entity->parent->dev && | |
494 | !try_module_get(entity->parent->dev->driver->owner)) | |
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 | ||
514 | if (entity->parent->dev) | |
515 | module_put(entity->parent->dev->driver->owner); | |
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 | |
545 | media_entity_create_link(struct media_entity *source, u16 source_pad, | |
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 MCC |
563 | /* Initialize graph object embedded at the new link */ |
564 | media_gobj_init(source->parent, MEDIA_GRAPH_LINK, &link->graph_obj); | |
565 | ||
53e269c1 LP |
566 | /* Create the backlink. Backlinks are used to help graph traversal and |
567 | * are not reported to userspace. | |
568 | */ | |
569 | backlink = media_entity_add_link(sink); | |
570 | if (backlink == NULL) { | |
571 | source->num_links--; | |
572 | return -ENOMEM; | |
573 | } | |
574 | ||
575 | backlink->source = &source->pads[source_pad]; | |
576 | backlink->sink = &sink->pads[sink_pad]; | |
577 | backlink->flags = flags; | |
578 | ||
6b6a4278 MCC |
579 | /* Initialize graph object embedded at the new link */ |
580 | media_gobj_init(sink->parent, MEDIA_GRAPH_LINK, &backlink->graph_obj); | |
581 | ||
53e269c1 LP |
582 | link->reverse = backlink; |
583 | backlink->reverse = link; | |
584 | ||
585 | sink->num_backlinks++; | |
586 | ||
587 | return 0; | |
588 | } | |
589 | EXPORT_SYMBOL_GPL(media_entity_create_link); | |
97548ed4 | 590 | |
7349cec1 SN |
591 | void __media_entity_remove_links(struct media_entity *entity) |
592 | { | |
593 | unsigned int i; | |
594 | ||
595 | for (i = 0; i < entity->num_links; i++) { | |
596 | struct media_link *link = &entity->links[i]; | |
597 | struct media_entity *remote; | |
598 | unsigned int r = 0; | |
599 | ||
600 | if (link->source->entity == entity) | |
601 | remote = link->sink->entity; | |
602 | else | |
603 | remote = link->source->entity; | |
604 | ||
605 | while (r < remote->num_links) { | |
606 | struct media_link *rlink = &remote->links[r]; | |
607 | ||
608 | if (rlink != link->reverse) { | |
609 | r++; | |
610 | continue; | |
611 | } | |
612 | ||
613 | if (link->source->entity == entity) | |
614 | remote->num_backlinks--; | |
615 | ||
616 | if (--remote->num_links == 0) | |
617 | break; | |
618 | ||
619 | /* Insert last entry in place of the dropped link. */ | |
620 | *rlink = remote->links[remote->num_links]; | |
621 | } | |
622 | } | |
623 | ||
624 | entity->num_links = 0; | |
625 | entity->num_backlinks = 0; | |
626 | } | |
627 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); | |
628 | ||
629 | void media_entity_remove_links(struct media_entity *entity) | |
630 | { | |
631 | /* Do nothing if the entity is not registered. */ | |
632 | if (entity->parent == NULL) | |
633 | return; | |
634 | ||
635 | mutex_lock(&entity->parent->graph_mutex); | |
636 | __media_entity_remove_links(entity); | |
637 | mutex_unlock(&entity->parent->graph_mutex); | |
638 | } | |
639 | EXPORT_SYMBOL_GPL(media_entity_remove_links); | |
640 | ||
97548ed4 LP |
641 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
642 | { | |
97548ed4 LP |
643 | int ret; |
644 | ||
645 | /* Notify both entities. */ | |
646 | ret = media_entity_call(link->source->entity, link_setup, | |
647 | link->source, link->sink, flags); | |
648 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
649 | return ret; | |
650 | ||
651 | ret = media_entity_call(link->sink->entity, link_setup, | |
652 | link->sink, link->source, flags); | |
653 | if (ret < 0 && ret != -ENOIOCTLCMD) { | |
654 | media_entity_call(link->source->entity, link_setup, | |
655 | link->source, link->sink, link->flags); | |
656 | return ret; | |
657 | } | |
658 | ||
7a6f0b22 | 659 | link->flags = flags; |
97548ed4 LP |
660 | link->reverse->flags = link->flags; |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
665 | /** | |
666 | * __media_entity_setup_link - Configure a media link | |
667 | * @link: The link being configured | |
668 | * @flags: Link configuration flags | |
669 | * | |
670 | * The bulk of link setup is handled by the two entities connected through the | |
671 | * link. This function notifies both entities of the link configuration change. | |
672 | * | |
673 | * If the link is immutable or if the current and new configuration are | |
674 | * identical, return immediately. | |
675 | * | |
676 | * The user is expected to hold link->source->parent->mutex. If not, | |
677 | * media_entity_setup_link() should be used instead. | |
678 | */ | |
679 | int __media_entity_setup_link(struct media_link *link, u32 flags) | |
680 | { | |
7a6f0b22 | 681 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
97548ed4 LP |
682 | struct media_device *mdev; |
683 | struct media_entity *source, *sink; | |
684 | int ret = -EBUSY; | |
685 | ||
686 | if (link == NULL) | |
687 | return -EINVAL; | |
688 | ||
7a6f0b22 LP |
689 | /* The non-modifiable link flags must not be modified. */ |
690 | if ((link->flags & ~mask) != (flags & ~mask)) | |
691 | return -EINVAL; | |
692 | ||
97548ed4 LP |
693 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
694 | return link->flags == flags ? 0 : -EINVAL; | |
695 | ||
696 | if (link->flags == flags) | |
697 | return 0; | |
698 | ||
699 | source = link->source->entity; | |
700 | sink = link->sink->entity; | |
701 | ||
e02188c9 LP |
702 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
703 | (source->stream_count || sink->stream_count)) | |
704 | return -EBUSY; | |
705 | ||
97548ed4 LP |
706 | mdev = source->parent; |
707 | ||
813f5c0a SN |
708 | if (mdev->link_notify) { |
709 | ret = mdev->link_notify(link, flags, | |
710 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); | |
97548ed4 LP |
711 | if (ret < 0) |
712 | return ret; | |
713 | } | |
714 | ||
715 | ret = __media_entity_setup_link_notify(link, flags); | |
97548ed4 | 716 | |
813f5c0a SN |
717 | if (mdev->link_notify) |
718 | mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH); | |
97548ed4 LP |
719 | |
720 | return ret; | |
721 | } | |
722 | ||
723 | int media_entity_setup_link(struct media_link *link, u32 flags) | |
724 | { | |
725 | int ret; | |
726 | ||
727 | mutex_lock(&link->source->entity->parent->graph_mutex); | |
728 | ret = __media_entity_setup_link(link, flags); | |
729 | mutex_unlock(&link->source->entity->parent->graph_mutex); | |
730 | ||
731 | return ret; | |
732 | } | |
733 | EXPORT_SYMBOL_GPL(media_entity_setup_link); | |
734 | ||
735 | /** | |
736 | * media_entity_find_link - Find a link between two pads | |
737 | * @source: Source pad | |
738 | * @sink: Sink pad | |
739 | * | |
740 | * Return a pointer to the link between the two entities. If no such link | |
741 | * exists, return NULL. | |
742 | */ | |
743 | struct media_link * | |
744 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) | |
745 | { | |
746 | struct media_link *link; | |
747 | unsigned int i; | |
748 | ||
749 | for (i = 0; i < source->entity->num_links; ++i) { | |
750 | link = &source->entity->links[i]; | |
751 | ||
752 | if (link->source->entity == source->entity && | |
753 | link->source->index == source->index && | |
754 | link->sink->entity == sink->entity && | |
755 | link->sink->index == sink->index) | |
756 | return link; | |
757 | } | |
758 | ||
759 | return NULL; | |
760 | } | |
761 | EXPORT_SYMBOL_GPL(media_entity_find_link); | |
762 | ||
763 | /** | |
1bddf1b3 AH |
764 | * media_entity_remote_pad - Find the pad at the remote end of a link |
765 | * @pad: Pad at the local end of the link | |
97548ed4 | 766 | * |
1bddf1b3 AH |
767 | * Search for a remote pad connected to the given pad by iterating over all |
768 | * links originating or terminating at that pad until an enabled link is found. | |
97548ed4 LP |
769 | * |
770 | * Return a pointer to the pad at the remote end of the first found enabled | |
771 | * link, or NULL if no enabled link has been found. | |
772 | */ | |
1bddf1b3 | 773 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
97548ed4 LP |
774 | { |
775 | unsigned int i; | |
776 | ||
777 | for (i = 0; i < pad->entity->num_links; i++) { | |
778 | struct media_link *link = &pad->entity->links[i]; | |
779 | ||
780 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) | |
781 | continue; | |
782 | ||
783 | if (link->source == pad) | |
784 | return link->sink; | |
785 | ||
786 | if (link->sink == pad) | |
787 | return link->source; | |
788 | } | |
789 | ||
790 | return NULL; | |
791 | ||
792 | } | |
1bddf1b3 | 793 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |