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