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