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 | static inline const char *gobj_type(enum media_gobj_type type) |
30 | { | |
31 | switch (type) { | |
32 | case MEDIA_GRAPH_ENTITY: | |
33 | return "entity"; | |
34 | case MEDIA_GRAPH_PAD: | |
35 | return "pad"; | |
36 | case MEDIA_GRAPH_LINK: | |
37 | return "link"; | |
27e543fa MCC |
38 | case MEDIA_GRAPH_INTF_DEVNODE: |
39 | return "intf-devnode"; | |
39a956c4 MCC |
40 | default: |
41 | return "unknown"; | |
42 | } | |
43 | } | |
44 | ||
27e543fa MCC |
45 | static inline const char *intf_type(struct media_interface *intf) |
46 | { | |
47 | switch (intf->type) { | |
48 | case MEDIA_INTF_T_DVB_FE: | |
49 | return "frontend"; | |
50 | case MEDIA_INTF_T_DVB_DEMUX: | |
51 | return "demux"; | |
52 | case MEDIA_INTF_T_DVB_DVR: | |
53 | return "DVR"; | |
54 | case MEDIA_INTF_T_DVB_CA: | |
55 | return "CA"; | |
56 | case MEDIA_INTF_T_DVB_NET: | |
57 | return "dvbnet"; | |
58 | case MEDIA_INTF_T_V4L_VIDEO: | |
59 | return "video"; | |
60 | case MEDIA_INTF_T_V4L_VBI: | |
61 | return "vbi"; | |
62 | case MEDIA_INTF_T_V4L_RADIO: | |
63 | return "radio"; | |
64 | case MEDIA_INTF_T_V4L_SUBDEV: | |
65 | return "v4l2-subdev"; | |
66 | case MEDIA_INTF_T_V4L_SWRADIO: | |
67 | return "swradio"; | |
68 | default: | |
69 | return "unknown-intf"; | |
70 | } | |
71 | }; | |
72 | ||
c8d54cd5 SA |
73 | __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum, |
74 | int idx_max) | |
75 | { | |
f7b5dff0 SA |
76 | idx_max = ALIGN(idx_max, BITS_PER_LONG); |
77 | ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long), | |
78 | GFP_KERNEL); | |
030e89ec SA |
79 | if (!ent_enum->bmap) |
80 | return -ENOMEM; | |
c8d54cd5 SA |
81 | |
82 | bitmap_zero(ent_enum->bmap, idx_max); | |
83 | ent_enum->idx_max = idx_max; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | EXPORT_SYMBOL_GPL(__media_entity_enum_init); | |
88 | ||
c8d54cd5 SA |
89 | void media_entity_enum_cleanup(struct media_entity_enum *ent_enum) |
90 | { | |
030e89ec | 91 | kfree(ent_enum->bmap); |
c8d54cd5 SA |
92 | } |
93 | EXPORT_SYMBOL_GPL(media_entity_enum_cleanup); | |
94 | ||
1fc25d30 MCC |
95 | /** |
96 | * dev_dbg_obj - Prints in debug mode a change on some object | |
97 | * | |
98 | * @event_name: Name of the event to report. Could be __func__ | |
99 | * @gobj: Pointer to the object | |
100 | * | |
101 | * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it | |
102 | * won't produce any code. | |
103 | */ | |
39a956c4 MCC |
104 | static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) |
105 | { | |
106 | #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG) | |
107 | switch (media_type(gobj)) { | |
108 | case MEDIA_GRAPH_ENTITY: | |
109 | dev_dbg(gobj->mdev->dev, | |
05b3b77c MCC |
110 | "%s id %u: entity '%s'\n", |
111 | event_name, media_id(gobj), | |
39a956c4 MCC |
112 | gobj_to_entity(gobj)->name); |
113 | break; | |
114 | case MEDIA_GRAPH_LINK: | |
115 | { | |
116 | struct media_link *link = gobj_to_link(gobj); | |
117 | ||
118 | dev_dbg(gobj->mdev->dev, | |
05b3b77c MCC |
119 | "%s id %u: %s link id %u ==> id %u\n", |
120 | event_name, media_id(gobj), | |
121 | media_type(link->gobj0) == MEDIA_GRAPH_PAD ? | |
122 | "data" : "interface", | |
123 | media_id(link->gobj0), | |
124 | media_id(link->gobj1)); | |
39a956c4 MCC |
125 | break; |
126 | } | |
127 | case MEDIA_GRAPH_PAD: | |
128 | { | |
129 | struct media_pad *pad = gobj_to_pad(gobj); | |
130 | ||
131 | dev_dbg(gobj->mdev->dev, | |
05b3b77c MCC |
132 | "%s id %u: %s%spad '%s':%d\n", |
133 | event_name, media_id(gobj), | |
134 | pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "", | |
6c24d460 | 135 | pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "", |
39a956c4 | 136 | pad->entity->name, pad->index); |
27e543fa MCC |
137 | break; |
138 | } | |
139 | case MEDIA_GRAPH_INTF_DEVNODE: | |
140 | { | |
141 | struct media_interface *intf = gobj_to_intf(gobj); | |
142 | struct media_intf_devnode *devnode = intf_to_devnode(intf); | |
143 | ||
144 | dev_dbg(gobj->mdev->dev, | |
05b3b77c MCC |
145 | "%s id %u: intf_devnode %s - major: %d, minor: %d\n", |
146 | event_name, media_id(gobj), | |
27e543fa MCC |
147 | intf_type(intf), |
148 | devnode->major, devnode->minor); | |
149 | break; | |
39a956c4 MCC |
150 | } |
151 | } | |
152 | #endif | |
153 | } | |
154 | ||
c350ef83 | 155 | void media_gobj_create(struct media_device *mdev, |
ec6e4c95 MCC |
156 | enum media_gobj_type type, |
157 | struct media_gobj *gobj) | |
158 | { | |
8f6d368f MCC |
159 | BUG_ON(!mdev); |
160 | ||
39a956c4 MCC |
161 | gobj->mdev = mdev; |
162 | ||
bfab2aac | 163 | /* Create a per-type unique object ID */ |
05b3b77c MCC |
164 | gobj->id = media_gobj_gen_id(type, ++mdev->id); |
165 | ||
bfab2aac MCC |
166 | switch (type) { |
167 | case MEDIA_GRAPH_ENTITY: | |
05bfa9fa | 168 | list_add_tail(&gobj->list, &mdev->entities); |
bfab2aac | 169 | break; |
18710dc6 | 170 | case MEDIA_GRAPH_PAD: |
9155d859 | 171 | list_add_tail(&gobj->list, &mdev->pads); |
18710dc6 | 172 | break; |
6b6a4278 | 173 | case MEDIA_GRAPH_LINK: |
9155d859 | 174 | list_add_tail(&gobj->list, &mdev->links); |
6b6a4278 | 175 | break; |
27e543fa | 176 | case MEDIA_GRAPH_INTF_DEVNODE: |
9155d859 | 177 | list_add_tail(&gobj->list, &mdev->interfaces); |
27e543fa | 178 | break; |
bfab2aac | 179 | } |
2521fdac MCC |
180 | |
181 | mdev->topology_version++; | |
182 | ||
39a956c4 | 183 | dev_dbg_obj(__func__, gobj); |
ec6e4c95 MCC |
184 | } |
185 | ||
c350ef83 | 186 | void media_gobj_destroy(struct media_gobj *gobj) |
ec6e4c95 | 187 | { |
39a956c4 | 188 | dev_dbg_obj(__func__, gobj); |
9155d859 | 189 | |
2521fdac MCC |
190 | gobj->mdev->topology_version++; |
191 | ||
9155d859 MCC |
192 | /* Remove the object from mdev list */ |
193 | list_del(&gobj->list); | |
ec6e4c95 MCC |
194 | } |
195 | ||
1fc25d30 MCC |
196 | int media_entity_pads_init(struct media_entity *entity, u16 num_pads, |
197 | struct media_pad *pads) | |
53e269c1 | 198 | { |
db141a35 | 199 | struct media_device *mdev = entity->graph_obj.mdev; |
53e269c1 LP |
200 | unsigned int i; |
201 | ||
53e269c1 LP |
202 | entity->num_pads = num_pads; |
203 | entity->pads = pads; | |
53e269c1 | 204 | |
db141a35 JMC |
205 | if (mdev) |
206 | spin_lock(&mdev->lock); | |
207 | ||
53e269c1 LP |
208 | for (i = 0; i < num_pads; i++) { |
209 | pads[i].entity = entity; | |
210 | pads[i].index = i; | |
db141a35 | 211 | if (mdev) |
c350ef83 | 212 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, |
db141a35 | 213 | &entity->pads[i].graph_obj); |
53e269c1 LP |
214 | } |
215 | ||
db141a35 JMC |
216 | if (mdev) |
217 | spin_unlock(&mdev->lock); | |
218 | ||
53e269c1 LP |
219 | return 0; |
220 | } | |
ab22e77c | 221 | EXPORT_SYMBOL_GPL(media_entity_pads_init); |
53e269c1 | 222 | |
a5ccc48a SA |
223 | /* ----------------------------------------------------------------------------- |
224 | * Graph traversal | |
225 | */ | |
226 | ||
227 | static struct media_entity * | |
228 | media_entity_other(struct media_entity *entity, struct media_link *link) | |
229 | { | |
230 | if (link->source->entity == entity) | |
231 | return link->sink->entity; | |
232 | else | |
233 | return link->source->entity; | |
234 | } | |
235 | ||
236 | /* push an entity to traversal stack */ | |
237 | static void stack_push(struct media_entity_graph *graph, | |
238 | struct media_entity *entity) | |
239 | { | |
240 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { | |
241 | WARN_ON(1); | |
242 | return; | |
243 | } | |
244 | graph->top++; | |
313895fb | 245 | graph->stack[graph->top].link = entity->links.next; |
a5ccc48a SA |
246 | graph->stack[graph->top].entity = entity; |
247 | } | |
248 | ||
249 | static struct media_entity *stack_pop(struct media_entity_graph *graph) | |
250 | { | |
251 | struct media_entity *entity; | |
252 | ||
253 | entity = graph->stack[graph->top].entity; | |
254 | graph->top--; | |
255 | ||
256 | return entity; | |
257 | } | |
258 | ||
a5ccc48a SA |
259 | #define link_top(en) ((en)->stack[(en)->top].link) |
260 | #define stack_top(en) ((en)->stack[(en)->top].entity) | |
261 | ||
0798ce4a SA |
262 | /* |
263 | * TODO: Get rid of this. | |
264 | */ | |
430a672c | 265 | #define MEDIA_ENTITY_MAX_PADS 512 |
0798ce4a | 266 | |
e03d2203 SA |
267 | /** |
268 | * media_entity_graph_walk_init - Allocate resources for graph walk | |
269 | * @graph: Media graph structure that will be used to walk the graph | |
270 | * @mdev: Media device | |
271 | * | |
272 | * Reserve resources for graph walk in media device's current | |
273 | * state. The memory must be released using | |
274 | * media_entity_graph_walk_free(). | |
275 | * | |
276 | * Returns error on failure, zero on success. | |
277 | */ | |
278 | __must_check int media_entity_graph_walk_init( | |
279 | struct media_entity_graph *graph, struct media_device *mdev) | |
280 | { | |
29d8da02 | 281 | return media_entity_enum_init(&graph->ent_enum, mdev); |
e03d2203 SA |
282 | } |
283 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_init); | |
284 | ||
285 | /** | |
286 | * media_entity_graph_walk_cleanup - Release resources related to graph walking | |
287 | * @graph: Media graph structure that was used to walk the graph | |
288 | */ | |
289 | void media_entity_graph_walk_cleanup(struct media_entity_graph *graph) | |
290 | { | |
29d8da02 | 291 | media_entity_enum_cleanup(&graph->ent_enum); |
e03d2203 SA |
292 | } |
293 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_cleanup); | |
294 | ||
a5ccc48a SA |
295 | void media_entity_graph_walk_start(struct media_entity_graph *graph, |
296 | struct media_entity *entity) | |
297 | { | |
29d8da02 SA |
298 | media_entity_enum_zero(&graph->ent_enum); |
299 | media_entity_enum_set(&graph->ent_enum, entity); | |
300 | ||
a5ccc48a SA |
301 | graph->top = 0; |
302 | graph->stack[graph->top].entity = NULL; | |
303 | stack_push(graph, entity); | |
304 | } | |
305 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_start); | |
306 | ||
a5ccc48a SA |
307 | struct media_entity * |
308 | media_entity_graph_walk_next(struct media_entity_graph *graph) | |
309 | { | |
310 | if (stack_top(graph) == NULL) | |
311 | return NULL; | |
312 | ||
313 | /* | |
314 | * Depth first search. Push entity to stack and continue from | |
315 | * top of the stack until no more entities on the level can be | |
316 | * found. | |
317 | */ | |
313895fb | 318 | while (link_top(graph) != &stack_top(graph)->links) { |
a5ccc48a | 319 | struct media_entity *entity = stack_top(graph); |
57208e5e | 320 | struct media_link *link; |
a5ccc48a SA |
321 | struct media_entity *next; |
322 | ||
57208e5e MCC |
323 | link = list_entry(link_top(graph), typeof(*link), list); |
324 | ||
a5ccc48a SA |
325 | /* The link is not enabled so we do not follow. */ |
326 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { | |
57208e5e | 327 | link_top(graph) = link_top(graph)->next; |
a5ccc48a SA |
328 | continue; |
329 | } | |
330 | ||
331 | /* Get the entity in the other end of the link . */ | |
332 | next = media_entity_other(entity, link); | |
333 | ||
5c7b25b9 | 334 | /* Has the entity already been visited? */ |
29d8da02 | 335 | if (media_entity_enum_test_and_set(&graph->ent_enum, next)) { |
57208e5e | 336 | link_top(graph) = link_top(graph)->next; |
a5ccc48a SA |
337 | continue; |
338 | } | |
339 | ||
340 | /* Push the new entity to stack and start over. */ | |
57208e5e | 341 | link_top(graph) = link_top(graph)->next; |
a5ccc48a SA |
342 | stack_push(graph, next); |
343 | } | |
344 | ||
345 | return stack_pop(graph); | |
346 | } | |
347 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); | |
348 | ||
e02188c9 LP |
349 | /* ----------------------------------------------------------------------------- |
350 | * Pipeline management | |
351 | */ | |
352 | ||
af88be38 SA |
353 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
354 | struct media_pipeline *pipe) | |
e02188c9 | 355 | { |
d10c9894 | 356 | struct media_device *mdev = entity->graph_obj.mdev; |
5dd8775d | 357 | struct media_entity_graph *graph = &pipe->graph; |
af88be38 | 358 | struct media_entity *entity_err = entity; |
57208e5e | 359 | struct media_link *link; |
af88be38 | 360 | int ret; |
e02188c9 LP |
361 | |
362 | mutex_lock(&mdev->graph_mutex); | |
363 | ||
74a41330 SA |
364 | if (!pipe->streaming_count++) { |
365 | ret = media_entity_graph_walk_init(&pipe->graph, mdev); | |
366 | if (ret) | |
367 | goto error_graph_walk_start; | |
106b9907 SA |
368 | } |
369 | ||
370 | media_entity_graph_walk_start(&pipe->graph, entity); | |
e02188c9 | 371 | |
5dd8775d | 372 | while ((entity = media_entity_graph_walk_next(graph))) { |
ef69ee1b MCC |
373 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
374 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); | |
af88be38 | 375 | |
e02188c9 | 376 | entity->stream_count++; |
8aaf62b5 SA |
377 | |
378 | if (WARN_ON(entity->pipe && entity->pipe != pipe)) { | |
379 | ret = -EBUSY; | |
380 | goto error; | |
381 | } | |
382 | ||
e02188c9 | 383 | entity->pipe = pipe; |
af88be38 SA |
384 | |
385 | /* Already streaming --- no need to check. */ | |
386 | if (entity->stream_count > 1) | |
387 | continue; | |
388 | ||
389 | if (!entity->ops || !entity->ops->link_validate) | |
390 | continue; | |
391 | ||
de49c285 SA |
392 | bitmap_zero(active, entity->num_pads); |
393 | bitmap_fill(has_no_links, entity->num_pads); | |
394 | ||
57208e5e | 395 | list_for_each_entry(link, &entity->links, list) { |
de49c285 SA |
396 | struct media_pad *pad = link->sink->entity == entity |
397 | ? link->sink : link->source; | |
398 | ||
399 | /* Mark that a pad is connected by a link. */ | |
400 | bitmap_clear(has_no_links, pad->index, 1); | |
401 | ||
402 | /* | |
403 | * Pads that either do not need to connect or | |
404 | * are connected through an enabled link are | |
405 | * fine. | |
406 | */ | |
407 | if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || | |
408 | link->flags & MEDIA_LNK_FL_ENABLED) | |
409 | bitmap_set(active, pad->index, 1); | |
410 | ||
411 | /* | |
412 | * Link validation will only take place for | |
413 | * sink ends of the link that are enabled. | |
414 | */ | |
415 | if (link->sink != pad || | |
416 | !(link->flags & MEDIA_LNK_FL_ENABLED)) | |
af88be38 SA |
417 | continue; |
418 | ||
419 | ret = entity->ops->link_validate(link); | |
fab9d30b | 420 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
d10c9894 | 421 | dev_dbg(entity->graph_obj.mdev->dev, |
fab9d30b | 422 | "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n", |
823ea2a6 SA |
423 | link->source->entity->name, |
424 | link->source->index, | |
425 | entity->name, link->sink->index, ret); | |
af88be38 | 426 | goto error; |
fab9d30b | 427 | } |
af88be38 | 428 | } |
de49c285 SA |
429 | |
430 | /* Either no links or validated links are fine. */ | |
431 | bitmap_or(active, active, has_no_links, entity->num_pads); | |
432 | ||
433 | if (!bitmap_full(active, entity->num_pads)) { | |
434 | ret = -EPIPE; | |
d10c9894 | 435 | dev_dbg(entity->graph_obj.mdev->dev, |
fab9d30b SA |
436 | "\"%s\":%u must be connected by an enabled link\n", |
437 | entity->name, | |
094f1ca5 SA |
438 | (unsigned)find_first_zero_bit( |
439 | active, entity->num_pads)); | |
de49c285 SA |
440 | goto error; |
441 | } | |
e02188c9 LP |
442 | } |
443 | ||
444 | mutex_unlock(&mdev->graph_mutex); | |
af88be38 SA |
445 | |
446 | return 0; | |
447 | ||
448 | error: | |
449 | /* | |
450 | * Link validation on graph failed. We revert what we did and | |
451 | * return the error. | |
452 | */ | |
5dd8775d | 453 | media_entity_graph_walk_start(graph, entity_err); |
af88be38 | 454 | |
5dd8775d | 455 | while ((entity_err = media_entity_graph_walk_next(graph))) { |
3801bc7d SK |
456 | /* don't let the stream_count go negative */ |
457 | if (entity->stream_count > 0) { | |
458 | entity_err->stream_count--; | |
459 | if (entity_err->stream_count == 0) | |
460 | entity_err->pipe = NULL; | |
461 | } | |
af88be38 SA |
462 | |
463 | /* | |
464 | * We haven't increased stream_count further than this | |
465 | * so we quit here. | |
466 | */ | |
467 | if (entity_err == entity) | |
468 | break; | |
469 | } | |
470 | ||
74a41330 SA |
471 | error_graph_walk_start: |
472 | if (!--pipe->streaming_count) | |
473 | media_entity_graph_walk_cleanup(graph); | |
106b9907 | 474 | |
af88be38 SA |
475 | mutex_unlock(&mdev->graph_mutex); |
476 | ||
477 | return ret; | |
e02188c9 LP |
478 | } |
479 | EXPORT_SYMBOL_GPL(media_entity_pipeline_start); | |
480 | ||
e02188c9 LP |
481 | void media_entity_pipeline_stop(struct media_entity *entity) |
482 | { | |
d10c9894 | 483 | struct media_device *mdev = entity->graph_obj.mdev; |
5dd8775d | 484 | struct media_entity_graph *graph = &entity->pipe->graph; |
74a41330 | 485 | struct media_pipeline *pipe = entity->pipe; |
e02188c9 LP |
486 | |
487 | mutex_lock(&mdev->graph_mutex); | |
488 | ||
74a41330 | 489 | WARN_ON(!pipe->streaming_count); |
5dd8775d | 490 | media_entity_graph_walk_start(graph, entity); |
e02188c9 | 491 | |
5dd8775d | 492 | while ((entity = media_entity_graph_walk_next(graph))) { |
3801bc7d SK |
493 | /* don't let the stream_count go negative */ |
494 | if (entity->stream_count > 0) { | |
495 | entity->stream_count--; | |
496 | if (entity->stream_count == 0) | |
497 | entity->pipe = NULL; | |
498 | } | |
e02188c9 LP |
499 | } |
500 | ||
74a41330 SA |
501 | if (!--pipe->streaming_count) |
502 | media_entity_graph_walk_cleanup(graph); | |
106b9907 | 503 | |
e02188c9 LP |
504 | mutex_unlock(&mdev->graph_mutex); |
505 | } | |
506 | EXPORT_SYMBOL_GPL(media_entity_pipeline_stop); | |
507 | ||
503c3d82 LP |
508 | /* ----------------------------------------------------------------------------- |
509 | * Module use count | |
510 | */ | |
511 | ||
503c3d82 LP |
512 | struct media_entity *media_entity_get(struct media_entity *entity) |
513 | { | |
514 | if (entity == NULL) | |
515 | return NULL; | |
516 | ||
d10c9894 JMC |
517 | if (entity->graph_obj.mdev->dev && |
518 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) | |
503c3d82 LP |
519 | return NULL; |
520 | ||
521 | return entity; | |
522 | } | |
523 | EXPORT_SYMBOL_GPL(media_entity_get); | |
524 | ||
503c3d82 LP |
525 | void media_entity_put(struct media_entity *entity) |
526 | { | |
527 | if (entity == NULL) | |
528 | return; | |
529 | ||
d10c9894 JMC |
530 | if (entity->graph_obj.mdev->dev) |
531 | module_put(entity->graph_obj.mdev->dev->driver->owner); | |
503c3d82 LP |
532 | } |
533 | EXPORT_SYMBOL_GPL(media_entity_put); | |
534 | ||
a5ccc48a SA |
535 | /* ----------------------------------------------------------------------------- |
536 | * Links management | |
537 | */ | |
538 | ||
23615de5 | 539 | static struct media_link *media_add_link(struct list_head *head) |
53e269c1 | 540 | { |
57208e5e | 541 | struct media_link *link; |
53e269c1 | 542 | |
57208e5e MCC |
543 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
544 | if (link == NULL) | |
545 | return NULL; | |
53e269c1 | 546 | |
23615de5 | 547 | list_add_tail(&link->list, head); |
53e269c1 | 548 | |
57208e5e | 549 | return link; |
53e269c1 LP |
550 | } |
551 | ||
57208e5e | 552 | static void __media_entity_remove_link(struct media_entity *entity, |
5abad22f MCC |
553 | struct media_link *link) |
554 | { | |
555 | struct media_link *rlink, *tmp; | |
556 | struct media_entity *remote; | |
5abad22f MCC |
557 | |
558 | if (link->source->entity == entity) | |
559 | remote = link->sink->entity; | |
560 | else | |
561 | remote = link->source->entity; | |
562 | ||
563 | list_for_each_entry_safe(rlink, tmp, &remote->links, list) { | |
58f69ee9 | 564 | if (rlink != link->reverse) |
5abad22f | 565 | continue; |
5abad22f MCC |
566 | |
567 | if (link->source->entity == entity) | |
568 | remote->num_backlinks--; | |
569 | ||
570 | /* Remove the remote link */ | |
571 | list_del(&rlink->list); | |
c350ef83 | 572 | media_gobj_destroy(&rlink->graph_obj); |
5abad22f MCC |
573 | kfree(rlink); |
574 | ||
575 | if (--remote->num_links == 0) | |
576 | break; | |
577 | } | |
578 | list_del(&link->list); | |
c350ef83 | 579 | media_gobj_destroy(&link->graph_obj); |
5abad22f MCC |
580 | kfree(link); |
581 | } | |
57208e5e | 582 | |
53e269c1 | 583 | int |
8df00a15 | 584 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
53e269c1 LP |
585 | struct media_entity *sink, u16 sink_pad, u32 flags) |
586 | { | |
587 | struct media_link *link; | |
588 | struct media_link *backlink; | |
589 | ||
590 | BUG_ON(source == NULL || sink == NULL); | |
591 | BUG_ON(source_pad >= source->num_pads); | |
592 | BUG_ON(sink_pad >= sink->num_pads); | |
593 | ||
23615de5 | 594 | link = media_add_link(&source->links); |
53e269c1 LP |
595 | if (link == NULL) |
596 | return -ENOMEM; | |
597 | ||
598 | link->source = &source->pads[source_pad]; | |
599 | link->sink = &sink->pads[sink_pad]; | |
82ae2a50 | 600 | link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK; |
53e269c1 | 601 | |
6b6a4278 | 602 | /* Initialize graph object embedded at the new link */ |
c350ef83 | 603 | media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
d10c9894 | 604 | &link->graph_obj); |
6b6a4278 | 605 | |
53e269c1 LP |
606 | /* Create the backlink. Backlinks are used to help graph traversal and |
607 | * are not reported to userspace. | |
608 | */ | |
23615de5 | 609 | backlink = media_add_link(&sink->links); |
53e269c1 | 610 | if (backlink == NULL) { |
57208e5e | 611 | __media_entity_remove_link(source, link); |
53e269c1 LP |
612 | return -ENOMEM; |
613 | } | |
614 | ||
615 | backlink->source = &source->pads[source_pad]; | |
616 | backlink->sink = &sink->pads[sink_pad]; | |
617 | backlink->flags = flags; | |
39d1ebc6 | 618 | backlink->is_backlink = true; |
53e269c1 | 619 | |
6b6a4278 | 620 | /* Initialize graph object embedded at the new link */ |
c350ef83 | 621 | media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
d10c9894 | 622 | &backlink->graph_obj); |
6b6a4278 | 623 | |
53e269c1 LP |
624 | link->reverse = backlink; |
625 | backlink->reverse = link; | |
626 | ||
627 | sink->num_backlinks++; | |
57208e5e MCC |
628 | sink->num_links++; |
629 | source->num_links++; | |
53e269c1 LP |
630 | |
631 | return 0; | |
632 | } | |
8df00a15 | 633 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
97548ed4 | 634 | |
b01cc9ce MCC |
635 | int media_create_pad_links(const struct media_device *mdev, |
636 | const u32 source_function, | |
637 | struct media_entity *source, | |
638 | const u16 source_pad, | |
639 | const u32 sink_function, | |
640 | struct media_entity *sink, | |
641 | const u16 sink_pad, | |
642 | u32 flags, | |
643 | const bool allow_both_undefined) | |
644 | { | |
645 | struct media_entity *entity; | |
646 | unsigned function; | |
647 | int ret; | |
648 | ||
649 | /* Trivial case: 1:1 relation */ | |
650 | if (source && sink) | |
651 | return media_create_pad_link(source, source_pad, | |
652 | sink, sink_pad, flags); | |
653 | ||
654 | /* Worse case scenario: n:n relation */ | |
655 | if (!source && !sink) { | |
656 | if (!allow_both_undefined) | |
657 | return 0; | |
658 | media_device_for_each_entity(source, mdev) { | |
659 | if (source->function != source_function) | |
660 | continue; | |
661 | media_device_for_each_entity(sink, mdev) { | |
662 | if (sink->function != sink_function) | |
663 | continue; | |
664 | ret = media_create_pad_link(source, source_pad, | |
665 | sink, sink_pad, | |
666 | flags); | |
667 | if (ret) | |
668 | return ret; | |
669 | flags &= ~(MEDIA_LNK_FL_ENABLED | | |
670 | MEDIA_LNK_FL_IMMUTABLE); | |
671 | } | |
672 | } | |
673 | return 0; | |
674 | } | |
675 | ||
676 | /* Handle 1:n and n:1 cases */ | |
677 | if (source) | |
678 | function = sink_function; | |
679 | else | |
680 | function = source_function; | |
681 | ||
682 | media_device_for_each_entity(entity, mdev) { | |
683 | if (entity->function != function) | |
684 | continue; | |
685 | ||
686 | if (source) | |
687 | ret = media_create_pad_link(source, source_pad, | |
688 | entity, sink_pad, flags); | |
689 | else | |
690 | ret = media_create_pad_link(entity, source_pad, | |
691 | sink, sink_pad, flags); | |
692 | if (ret) | |
693 | return ret; | |
694 | flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); | |
695 | } | |
696 | return 0; | |
697 | } | |
698 | EXPORT_SYMBOL_GPL(media_create_pad_links); | |
699 | ||
57208e5e MCC |
700 | void __media_entity_remove_links(struct media_entity *entity) |
701 | { | |
702 | struct media_link *link, *tmp; | |
7349cec1 | 703 | |
57208e5e MCC |
704 | list_for_each_entry_safe(link, tmp, &entity->links, list) |
705 | __media_entity_remove_link(entity, link); | |
7349cec1 SN |
706 | |
707 | entity->num_links = 0; | |
708 | entity->num_backlinks = 0; | |
709 | } | |
710 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); | |
711 | ||
712 | void media_entity_remove_links(struct media_entity *entity) | |
713 | { | |
cc4a8258 MCC |
714 | struct media_device *mdev = entity->graph_obj.mdev; |
715 | ||
7349cec1 | 716 | /* Do nothing if the entity is not registered. */ |
cc4a8258 | 717 | if (mdev == NULL) |
7349cec1 SN |
718 | return; |
719 | ||
cc4a8258 | 720 | spin_lock(&mdev->lock); |
7349cec1 | 721 | __media_entity_remove_links(entity); |
cc4a8258 | 722 | spin_unlock(&mdev->lock); |
7349cec1 SN |
723 | } |
724 | EXPORT_SYMBOL_GPL(media_entity_remove_links); | |
725 | ||
97548ed4 LP |
726 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
727 | { | |
97548ed4 LP |
728 | int ret; |
729 | ||
730 | /* Notify both entities. */ | |
731 | ret = media_entity_call(link->source->entity, link_setup, | |
732 | link->source, link->sink, flags); | |
733 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
734 | return ret; | |
735 | ||
736 | ret = media_entity_call(link->sink->entity, link_setup, | |
737 | link->sink, link->source, flags); | |
738 | if (ret < 0 && ret != -ENOIOCTLCMD) { | |
739 | media_entity_call(link->source->entity, link_setup, | |
740 | link->source, link->sink, link->flags); | |
741 | return ret; | |
742 | } | |
743 | ||
7a6f0b22 | 744 | link->flags = flags; |
97548ed4 LP |
745 | link->reverse->flags = link->flags; |
746 | ||
747 | return 0; | |
748 | } | |
749 | ||
97548ed4 LP |
750 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
751 | { | |
7a6f0b22 | 752 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
97548ed4 LP |
753 | struct media_device *mdev; |
754 | struct media_entity *source, *sink; | |
755 | int ret = -EBUSY; | |
756 | ||
757 | if (link == NULL) | |
758 | return -EINVAL; | |
759 | ||
7a6f0b22 LP |
760 | /* The non-modifiable link flags must not be modified. */ |
761 | if ((link->flags & ~mask) != (flags & ~mask)) | |
762 | return -EINVAL; | |
763 | ||
97548ed4 LP |
764 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
765 | return link->flags == flags ? 0 : -EINVAL; | |
766 | ||
767 | if (link->flags == flags) | |
768 | return 0; | |
769 | ||
770 | source = link->source->entity; | |
771 | sink = link->sink->entity; | |
772 | ||
e02188c9 LP |
773 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
774 | (source->stream_count || sink->stream_count)) | |
775 | return -EBUSY; | |
776 | ||
d10c9894 | 777 | mdev = source->graph_obj.mdev; |
97548ed4 | 778 | |
813f5c0a SN |
779 | if (mdev->link_notify) { |
780 | ret = mdev->link_notify(link, flags, | |
781 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); | |
97548ed4 LP |
782 | if (ret < 0) |
783 | return ret; | |
784 | } | |
785 | ||
786 | ret = __media_entity_setup_link_notify(link, flags); | |
97548ed4 | 787 | |
813f5c0a SN |
788 | if (mdev->link_notify) |
789 | mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH); | |
97548ed4 LP |
790 | |
791 | return ret; | |
792 | } | |
793 | ||
794 | int media_entity_setup_link(struct media_link *link, u32 flags) | |
795 | { | |
796 | int ret; | |
797 | ||
5c883edb | 798 | mutex_lock(&link->graph_obj.mdev->graph_mutex); |
97548ed4 | 799 | ret = __media_entity_setup_link(link, flags); |
5c883edb | 800 | mutex_unlock(&link->graph_obj.mdev->graph_mutex); |
97548ed4 LP |
801 | |
802 | return ret; | |
803 | } | |
804 | EXPORT_SYMBOL_GPL(media_entity_setup_link); | |
805 | ||
97548ed4 LP |
806 | struct media_link * |
807 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) | |
808 | { | |
809 | struct media_link *link; | |
97548ed4 | 810 | |
57208e5e | 811 | list_for_each_entry(link, &source->entity->links, list) { |
97548ed4 LP |
812 | if (link->source->entity == source->entity && |
813 | link->source->index == source->index && | |
814 | link->sink->entity == sink->entity && | |
815 | link->sink->index == sink->index) | |
816 | return link; | |
817 | } | |
818 | ||
819 | return NULL; | |
820 | } | |
821 | EXPORT_SYMBOL_GPL(media_entity_find_link); | |
822 | ||
1bddf1b3 | 823 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
97548ed4 | 824 | { |
57208e5e | 825 | struct media_link *link; |
97548ed4 | 826 | |
57208e5e | 827 | list_for_each_entry(link, &pad->entity->links, list) { |
97548ed4 LP |
828 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
829 | continue; | |
830 | ||
831 | if (link->source == pad) | |
832 | return link->sink; | |
833 | ||
834 | if (link->sink == pad) | |
835 | return link->source; | |
836 | } | |
837 | ||
838 | return NULL; | |
839 | ||
840 | } | |
1bddf1b3 | 841 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |
27e543fa | 842 | |
1283f849 MCC |
843 | static void media_interface_init(struct media_device *mdev, |
844 | struct media_interface *intf, | |
845 | u32 gobj_type, | |
846 | u32 intf_type, u32 flags) | |
847 | { | |
848 | intf->type = intf_type; | |
849 | intf->flags = flags; | |
850 | INIT_LIST_HEAD(&intf->links); | |
851 | ||
c350ef83 | 852 | media_gobj_create(mdev, gobj_type, &intf->graph_obj); |
1283f849 MCC |
853 | } |
854 | ||
27e543fa MCC |
855 | /* Functions related to the media interface via device nodes */ |
856 | ||
857 | struct media_intf_devnode *media_devnode_create(struct media_device *mdev, | |
858 | u32 type, u32 flags, | |
0b3b72df | 859 | u32 major, u32 minor) |
27e543fa MCC |
860 | { |
861 | struct media_intf_devnode *devnode; | |
27e543fa | 862 | |
0b3b72df | 863 | devnode = kzalloc(sizeof(*devnode), GFP_KERNEL); |
27e543fa MCC |
864 | if (!devnode) |
865 | return NULL; | |
866 | ||
27e543fa MCC |
867 | devnode->major = major; |
868 | devnode->minor = minor; | |
869 | ||
1283f849 MCC |
870 | media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE, |
871 | type, flags); | |
27e543fa MCC |
872 | |
873 | return devnode; | |
874 | } | |
875 | EXPORT_SYMBOL_GPL(media_devnode_create); | |
876 | ||
877 | void media_devnode_remove(struct media_intf_devnode *devnode) | |
878 | { | |
7c4696a9 | 879 | media_remove_intf_links(&devnode->intf); |
c350ef83 | 880 | media_gobj_destroy(&devnode->intf.graph_obj); |
27e543fa MCC |
881 | kfree(devnode); |
882 | } | |
883 | EXPORT_SYMBOL_GPL(media_devnode_remove); | |
86e26620 MCC |
884 | |
885 | struct media_link *media_create_intf_link(struct media_entity *entity, | |
886 | struct media_interface *intf, | |
887 | u32 flags) | |
888 | { | |
889 | struct media_link *link; | |
890 | ||
891 | link = media_add_link(&intf->links); | |
892 | if (link == NULL) | |
893 | return NULL; | |
894 | ||
895 | link->intf = intf; | |
896 | link->entity = entity; | |
82ae2a50 | 897 | link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK; |
86e26620 MCC |
898 | |
899 | /* Initialize graph object embedded at the new link */ | |
c350ef83 | 900 | media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK, |
86e26620 MCC |
901 | &link->graph_obj); |
902 | ||
903 | return link; | |
904 | } | |
905 | EXPORT_SYMBOL_GPL(media_create_intf_link); | |
906 | ||
d47109fa | 907 | void __media_remove_intf_link(struct media_link *link) |
86e26620 | 908 | { |
d47109fa | 909 | list_del(&link->list); |
c350ef83 | 910 | media_gobj_destroy(&link->graph_obj); |
86e26620 MCC |
911 | kfree(link); |
912 | } | |
d47109fa | 913 | EXPORT_SYMBOL_GPL(__media_remove_intf_link); |
86e26620 MCC |
914 | |
915 | void media_remove_intf_link(struct media_link *link) | |
916 | { | |
cc4a8258 MCC |
917 | struct media_device *mdev = link->graph_obj.mdev; |
918 | ||
919 | /* Do nothing if the intf is not registered. */ | |
920 | if (mdev == NULL) | |
921 | return; | |
922 | ||
923 | spin_lock(&mdev->lock); | |
86e26620 | 924 | __media_remove_intf_link(link); |
cc4a8258 | 925 | spin_unlock(&mdev->lock); |
86e26620 MCC |
926 | } |
927 | EXPORT_SYMBOL_GPL(media_remove_intf_link); | |
7c4696a9 MCC |
928 | |
929 | void __media_remove_intf_links(struct media_interface *intf) | |
930 | { | |
931 | struct media_link *link, *tmp; | |
932 | ||
933 | list_for_each_entry_safe(link, tmp, &intf->links, list) | |
934 | __media_remove_intf_link(link); | |
935 | ||
936 | } | |
937 | EXPORT_SYMBOL_GPL(__media_remove_intf_links); | |
938 | ||
939 | void media_remove_intf_links(struct media_interface *intf) | |
940 | { | |
cc4a8258 MCC |
941 | struct media_device *mdev = intf->graph_obj.mdev; |
942 | ||
7c4696a9 | 943 | /* Do nothing if the intf is not registered. */ |
cc4a8258 | 944 | if (mdev == NULL) |
7c4696a9 MCC |
945 | return; |
946 | ||
cc4a8258 | 947 | spin_lock(&mdev->lock); |
7c4696a9 | 948 | __media_remove_intf_links(intf); |
cc4a8258 | 949 | spin_unlock(&mdev->lock); |
7c4696a9 MCC |
950 | } |
951 | EXPORT_SYMBOL_GPL(media_remove_intf_links); |