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