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