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