]>
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 | 199 | /** |
ab22e77c | 200 | * media_entity_pads_init - Initialize a media entity |
53e269c1 LP |
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 | |
ab22e77c | 219 | * media_entity_pads_init() where its pointer will be stored in the entity structure. |
53e269c1 LP |
220 | */ |
221 | int | |
ab22e77c | 222 | media_entity_pads_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 LP |
228 | entity->num_pads = num_pads; |
229 | entity->pads = pads; | |
53e269c1 | 230 | |
db141a35 JMC |
231 | if (mdev) |
232 | spin_lock(&mdev->lock); | |
233 | ||
53e269c1 LP |
234 | for (i = 0; i < num_pads; i++) { |
235 | pads[i].entity = entity; | |
236 | pads[i].index = i; | |
db141a35 JMC |
237 | if (mdev) |
238 | media_gobj_init(mdev, MEDIA_GRAPH_PAD, | |
239 | &entity->pads[i].graph_obj); | |
53e269c1 LP |
240 | } |
241 | ||
db141a35 JMC |
242 | if (mdev) |
243 | spin_unlock(&mdev->lock); | |
244 | ||
53e269c1 LP |
245 | return 0; |
246 | } | |
ab22e77c | 247 | EXPORT_SYMBOL_GPL(media_entity_pads_init); |
53e269c1 | 248 | |
a5ccc48a SA |
249 | /* ----------------------------------------------------------------------------- |
250 | * Graph traversal | |
251 | */ | |
252 | ||
253 | static struct media_entity * | |
254 | media_entity_other(struct media_entity *entity, struct media_link *link) | |
255 | { | |
256 | if (link->source->entity == entity) | |
257 | return link->sink->entity; | |
258 | else | |
259 | return link->source->entity; | |
260 | } | |
261 | ||
262 | /* push an entity to traversal stack */ | |
263 | static void stack_push(struct media_entity_graph *graph, | |
264 | struct media_entity *entity) | |
265 | { | |
266 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { | |
267 | WARN_ON(1); | |
268 | return; | |
269 | } | |
270 | graph->top++; | |
57208e5e | 271 | graph->stack[graph->top].link = (&entity->links)->next; |
a5ccc48a SA |
272 | graph->stack[graph->top].entity = entity; |
273 | } | |
274 | ||
275 | static struct media_entity *stack_pop(struct media_entity_graph *graph) | |
276 | { | |
277 | struct media_entity *entity; | |
278 | ||
279 | entity = graph->stack[graph->top].entity; | |
280 | graph->top--; | |
281 | ||
282 | return entity; | |
283 | } | |
284 | ||
a5ccc48a SA |
285 | #define link_top(en) ((en)->stack[(en)->top].link) |
286 | #define stack_top(en) ((en)->stack[(en)->top].entity) | |
287 | ||
288 | /** | |
289 | * media_entity_graph_walk_start - Start walking the media graph at a given entity | |
290 | * @graph: Media graph structure that will be used to walk the graph | |
291 | * @entity: Starting entity | |
292 | * | |
293 | * This function initializes the graph traversal structure to walk the entities | |
294 | * graph starting at the given entity. The traversal structure must not be | |
295 | * modified by the caller during graph traversal. When done the structure can | |
296 | * safely be freed. | |
297 | */ | |
298 | void media_entity_graph_walk_start(struct media_entity_graph *graph, | |
299 | struct media_entity *entity) | |
300 | { | |
301 | graph->top = 0; | |
302 | graph->stack[graph->top].entity = NULL; | |
5c7b25b9 LP |
303 | bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID); |
304 | ||
fa762394 | 305 | if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
5c7b25b9 LP |
306 | return; |
307 | ||
fa762394 | 308 | __set_bit(media_entity_id(entity), graph->entities); |
a5ccc48a SA |
309 | stack_push(graph, entity); |
310 | } | |
311 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_start); | |
312 | ||
313 | /** | |
314 | * media_entity_graph_walk_next - Get the next entity in the graph | |
315 | * @graph: Media graph structure | |
316 | * | |
317 | * Perform a depth-first traversal of the given media entities graph. | |
318 | * | |
319 | * The graph structure must have been previously initialized with a call to | |
320 | * media_entity_graph_walk_start(). | |
321 | * | |
322 | * Return the next entity in the graph or NULL if the whole graph have been | |
323 | * traversed. | |
324 | */ | |
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 | */ | |
57208e5e | 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); | |
fa762394 | 351 | if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
5c7b25b9 | 352 | return NULL; |
a5ccc48a | 353 | |
5c7b25b9 | 354 | /* Has the entity already been visited? */ |
fa762394 | 355 | if (__test_and_set_bit(media_entity_id(next), graph->entities)) { |
57208e5e | 356 | link_top(graph) = link_top(graph)->next; |
a5ccc48a SA |
357 | continue; |
358 | } | |
359 | ||
360 | /* Push the new entity to stack and start over. */ | |
57208e5e | 361 | link_top(graph) = link_top(graph)->next; |
a5ccc48a SA |
362 | stack_push(graph, next); |
363 | } | |
364 | ||
365 | return stack_pop(graph); | |
366 | } | |
367 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); | |
368 | ||
e02188c9 LP |
369 | /* ----------------------------------------------------------------------------- |
370 | * Pipeline management | |
371 | */ | |
372 | ||
373 | /** | |
374 | * media_entity_pipeline_start - Mark a pipeline as streaming | |
375 | * @entity: Starting entity | |
376 | * @pipe: Media pipeline to be assigned to all entities in the pipeline. | |
377 | * | |
378 | * Mark all entities connected to a given entity through enabled links, either | |
379 | * directly or indirectly, as streaming. The given pipeline object is assigned to | |
380 | * every entity in the pipeline and stored in the media_entity pipe field. | |
381 | * | |
382 | * Calls to this function can be nested, in which case the same number of | |
383 | * media_entity_pipeline_stop() calls will be required to stop streaming. The | |
384 | * pipeline pointer must be identical for all nested calls to | |
385 | * media_entity_pipeline_start(). | |
386 | */ | |
af88be38 SA |
387 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
388 | struct media_pipeline *pipe) | |
e02188c9 | 389 | { |
d10c9894 | 390 | struct media_device *mdev = entity->graph_obj.mdev; |
e02188c9 | 391 | struct media_entity_graph graph; |
af88be38 | 392 | struct media_entity *entity_err = entity; |
57208e5e | 393 | struct media_link *link; |
af88be38 | 394 | int ret; |
e02188c9 LP |
395 | |
396 | mutex_lock(&mdev->graph_mutex); | |
397 | ||
398 | media_entity_graph_walk_start(&graph, entity); | |
399 | ||
400 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
ef69ee1b MCC |
401 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
402 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); | |
af88be38 | 403 | |
e02188c9 LP |
404 | entity->stream_count++; |
405 | WARN_ON(entity->pipe && entity->pipe != pipe); | |
406 | entity->pipe = pipe; | |
af88be38 SA |
407 | |
408 | /* Already streaming --- no need to check. */ | |
409 | if (entity->stream_count > 1) | |
410 | continue; | |
411 | ||
412 | if (!entity->ops || !entity->ops->link_validate) | |
413 | continue; | |
414 | ||
de49c285 SA |
415 | bitmap_zero(active, entity->num_pads); |
416 | bitmap_fill(has_no_links, entity->num_pads); | |
417 | ||
57208e5e | 418 | list_for_each_entry(link, &entity->links, list) { |
de49c285 SA |
419 | struct media_pad *pad = link->sink->entity == entity |
420 | ? link->sink : link->source; | |
421 | ||
422 | /* Mark that a pad is connected by a link. */ | |
423 | bitmap_clear(has_no_links, pad->index, 1); | |
424 | ||
425 | /* | |
426 | * Pads that either do not need to connect or | |
427 | * are connected through an enabled link are | |
428 | * fine. | |
429 | */ | |
430 | if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || | |
431 | link->flags & MEDIA_LNK_FL_ENABLED) | |
432 | bitmap_set(active, pad->index, 1); | |
433 | ||
434 | /* | |
435 | * Link validation will only take place for | |
436 | * sink ends of the link that are enabled. | |
437 | */ | |
438 | if (link->sink != pad || | |
439 | !(link->flags & MEDIA_LNK_FL_ENABLED)) | |
af88be38 SA |
440 | continue; |
441 | ||
442 | ret = entity->ops->link_validate(link); | |
fab9d30b | 443 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
d10c9894 | 444 | dev_dbg(entity->graph_obj.mdev->dev, |
fab9d30b | 445 | "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n", |
823ea2a6 SA |
446 | link->source->entity->name, |
447 | link->source->index, | |
448 | entity->name, link->sink->index, ret); | |
af88be38 | 449 | goto error; |
fab9d30b | 450 | } |
af88be38 | 451 | } |
de49c285 SA |
452 | |
453 | /* Either no links or validated links are fine. */ | |
454 | bitmap_or(active, active, has_no_links, entity->num_pads); | |
455 | ||
456 | if (!bitmap_full(active, entity->num_pads)) { | |
457 | ret = -EPIPE; | |
d10c9894 | 458 | dev_dbg(entity->graph_obj.mdev->dev, |
fab9d30b SA |
459 | "\"%s\":%u must be connected by an enabled link\n", |
460 | entity->name, | |
094f1ca5 SA |
461 | (unsigned)find_first_zero_bit( |
462 | active, entity->num_pads)); | |
de49c285 SA |
463 | goto error; |
464 | } | |
e02188c9 LP |
465 | } |
466 | ||
467 | mutex_unlock(&mdev->graph_mutex); | |
af88be38 SA |
468 | |
469 | return 0; | |
470 | ||
471 | error: | |
472 | /* | |
473 | * Link validation on graph failed. We revert what we did and | |
474 | * return the error. | |
475 | */ | |
476 | media_entity_graph_walk_start(&graph, entity_err); | |
477 | ||
478 | while ((entity_err = media_entity_graph_walk_next(&graph))) { | |
479 | entity_err->stream_count--; | |
480 | if (entity_err->stream_count == 0) | |
481 | entity_err->pipe = NULL; | |
482 | ||
483 | /* | |
484 | * We haven't increased stream_count further than this | |
485 | * so we quit here. | |
486 | */ | |
487 | if (entity_err == entity) | |
488 | break; | |
489 | } | |
490 | ||
491 | mutex_unlock(&mdev->graph_mutex); | |
492 | ||
493 | return ret; | |
e02188c9 LP |
494 | } |
495 | EXPORT_SYMBOL_GPL(media_entity_pipeline_start); | |
496 | ||
497 | /** | |
498 | * media_entity_pipeline_stop - Mark a pipeline as not streaming | |
499 | * @entity: Starting entity | |
500 | * | |
501 | * Mark all entities connected to a given entity through enabled links, either | |
502 | * directly or indirectly, as not streaming. The media_entity pipe field is | |
503 | * reset to NULL. | |
504 | * | |
505 | * If multiple calls to media_entity_pipeline_start() have been made, the same | |
506 | * number of calls to this function are required to mark the pipeline as not | |
507 | * streaming. | |
508 | */ | |
509 | void media_entity_pipeline_stop(struct media_entity *entity) | |
510 | { | |
d10c9894 | 511 | struct media_device *mdev = entity->graph_obj.mdev; |
e02188c9 LP |
512 | struct media_entity_graph graph; |
513 | ||
514 | mutex_lock(&mdev->graph_mutex); | |
515 | ||
516 | media_entity_graph_walk_start(&graph, entity); | |
517 | ||
518 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
519 | entity->stream_count--; | |
520 | if (entity->stream_count == 0) | |
521 | entity->pipe = NULL; | |
522 | } | |
523 | ||
524 | mutex_unlock(&mdev->graph_mutex); | |
525 | } | |
526 | EXPORT_SYMBOL_GPL(media_entity_pipeline_stop); | |
527 | ||
503c3d82 LP |
528 | /* ----------------------------------------------------------------------------- |
529 | * Module use count | |
530 | */ | |
531 | ||
532 | /* | |
533 | * media_entity_get - Get a reference to the parent module | |
534 | * @entity: The entity | |
535 | * | |
536 | * Get a reference to the parent media device module. | |
537 | * | |
538 | * The function will return immediately if @entity is NULL. | |
539 | * | |
540 | * Return a pointer to the entity on success or NULL on failure. | |
541 | */ | |
542 | struct media_entity *media_entity_get(struct media_entity *entity) | |
543 | { | |
544 | if (entity == NULL) | |
545 | return NULL; | |
546 | ||
d10c9894 JMC |
547 | if (entity->graph_obj.mdev->dev && |
548 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) | |
503c3d82 LP |
549 | return NULL; |
550 | ||
551 | return entity; | |
552 | } | |
553 | EXPORT_SYMBOL_GPL(media_entity_get); | |
554 | ||
555 | /* | |
556 | * media_entity_put - Release the reference to the parent module | |
557 | * @entity: The entity | |
558 | * | |
559 | * Release the reference count acquired by media_entity_get(). | |
560 | * | |
561 | * The function will return immediately if @entity is NULL. | |
562 | */ | |
563 | void media_entity_put(struct media_entity *entity) | |
564 | { | |
565 | if (entity == NULL) | |
566 | return; | |
567 | ||
d10c9894 JMC |
568 | if (entity->graph_obj.mdev->dev) |
569 | module_put(entity->graph_obj.mdev->dev->driver->owner); | |
503c3d82 LP |
570 | } |
571 | EXPORT_SYMBOL_GPL(media_entity_put); | |
572 | ||
a5ccc48a SA |
573 | /* ----------------------------------------------------------------------------- |
574 | * Links management | |
575 | */ | |
576 | ||
23615de5 | 577 | static struct media_link *media_add_link(struct list_head *head) |
53e269c1 | 578 | { |
57208e5e | 579 | struct media_link *link; |
53e269c1 | 580 | |
57208e5e MCC |
581 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
582 | if (link == NULL) | |
583 | return NULL; | |
53e269c1 | 584 | |
23615de5 | 585 | list_add_tail(&link->list, head); |
53e269c1 | 586 | |
57208e5e | 587 | return link; |
53e269c1 LP |
588 | } |
589 | ||
57208e5e | 590 | static void __media_entity_remove_link(struct media_entity *entity, |
5abad22f MCC |
591 | struct media_link *link) |
592 | { | |
593 | struct media_link *rlink, *tmp; | |
594 | struct media_entity *remote; | |
595 | unsigned int r = 0; | |
596 | ||
597 | if (link->source->entity == entity) | |
598 | remote = link->sink->entity; | |
599 | else | |
600 | remote = link->source->entity; | |
601 | ||
602 | list_for_each_entry_safe(rlink, tmp, &remote->links, list) { | |
603 | if (rlink != link->reverse) { | |
604 | r++; | |
605 | continue; | |
606 | } | |
607 | ||
608 | if (link->source->entity == entity) | |
609 | remote->num_backlinks--; | |
610 | ||
611 | /* Remove the remote link */ | |
612 | list_del(&rlink->list); | |
613 | media_gobj_remove(&rlink->graph_obj); | |
614 | kfree(rlink); | |
615 | ||
616 | if (--remote->num_links == 0) | |
617 | break; | |
618 | } | |
619 | list_del(&link->list); | |
620 | media_gobj_remove(&link->graph_obj); | |
621 | kfree(link); | |
622 | } | |
57208e5e | 623 | |
53e269c1 | 624 | int |
8df00a15 | 625 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
53e269c1 LP |
626 | struct media_entity *sink, u16 sink_pad, u32 flags) |
627 | { | |
628 | struct media_link *link; | |
629 | struct media_link *backlink; | |
630 | ||
631 | BUG_ON(source == NULL || sink == NULL); | |
632 | BUG_ON(source_pad >= source->num_pads); | |
633 | BUG_ON(sink_pad >= sink->num_pads); | |
634 | ||
23615de5 | 635 | link = media_add_link(&source->links); |
53e269c1 LP |
636 | if (link == NULL) |
637 | return -ENOMEM; | |
638 | ||
639 | link->source = &source->pads[source_pad]; | |
640 | link->sink = &sink->pads[sink_pad]; | |
641 | link->flags = flags; | |
642 | ||
6b6a4278 | 643 | /* Initialize graph object embedded at the new link */ |
d10c9894 JMC |
644 | media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
645 | &link->graph_obj); | |
6b6a4278 | 646 | |
53e269c1 LP |
647 | /* Create the backlink. Backlinks are used to help graph traversal and |
648 | * are not reported to userspace. | |
649 | */ | |
23615de5 | 650 | backlink = media_add_link(&sink->links); |
53e269c1 | 651 | if (backlink == NULL) { |
57208e5e | 652 | __media_entity_remove_link(source, link); |
53e269c1 LP |
653 | return -ENOMEM; |
654 | } | |
655 | ||
656 | backlink->source = &source->pads[source_pad]; | |
657 | backlink->sink = &sink->pads[sink_pad]; | |
658 | backlink->flags = flags; | |
39d1ebc6 | 659 | backlink->is_backlink = true; |
53e269c1 | 660 | |
6b6a4278 | 661 | /* Initialize graph object embedded at the new link */ |
d10c9894 JMC |
662 | media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
663 | &backlink->graph_obj); | |
6b6a4278 | 664 | |
53e269c1 LP |
665 | link->reverse = backlink; |
666 | backlink->reverse = link; | |
667 | ||
668 | sink->num_backlinks++; | |
57208e5e MCC |
669 | sink->num_links++; |
670 | source->num_links++; | |
53e269c1 LP |
671 | |
672 | return 0; | |
673 | } | |
8df00a15 | 674 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
97548ed4 | 675 | |
57208e5e MCC |
676 | void __media_entity_remove_links(struct media_entity *entity) |
677 | { | |
678 | struct media_link *link, *tmp; | |
7349cec1 | 679 | |
57208e5e MCC |
680 | list_for_each_entry_safe(link, tmp, &entity->links, list) |
681 | __media_entity_remove_link(entity, link); | |
7349cec1 SN |
682 | |
683 | entity->num_links = 0; | |
684 | entity->num_backlinks = 0; | |
685 | } | |
686 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); | |
687 | ||
688 | void media_entity_remove_links(struct media_entity *entity) | |
689 | { | |
690 | /* Do nothing if the entity is not registered. */ | |
d10c9894 | 691 | if (entity->graph_obj.mdev == NULL) |
7349cec1 SN |
692 | return; |
693 | ||
a08fad1e | 694 | spin_lock(&entity->graph_obj.mdev->lock); |
7349cec1 | 695 | __media_entity_remove_links(entity); |
a08fad1e | 696 | spin_unlock(&entity->graph_obj.mdev->lock); |
7349cec1 SN |
697 | } |
698 | EXPORT_SYMBOL_GPL(media_entity_remove_links); | |
699 | ||
97548ed4 LP |
700 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
701 | { | |
97548ed4 LP |
702 | int ret; |
703 | ||
704 | /* Notify both entities. */ | |
705 | ret = media_entity_call(link->source->entity, link_setup, | |
706 | link->source, link->sink, flags); | |
707 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
708 | return ret; | |
709 | ||
710 | ret = media_entity_call(link->sink->entity, link_setup, | |
711 | link->sink, link->source, flags); | |
712 | if (ret < 0 && ret != -ENOIOCTLCMD) { | |
713 | media_entity_call(link->source->entity, link_setup, | |
714 | link->source, link->sink, link->flags); | |
715 | return ret; | |
716 | } | |
717 | ||
7a6f0b22 | 718 | link->flags = flags; |
97548ed4 LP |
719 | link->reverse->flags = link->flags; |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
724 | /** | |
725 | * __media_entity_setup_link - Configure a media link | |
726 | * @link: The link being configured | |
727 | * @flags: Link configuration flags | |
728 | * | |
729 | * The bulk of link setup is handled by the two entities connected through the | |
730 | * link. This function notifies both entities of the link configuration change. | |
731 | * | |
732 | * If the link is immutable or if the current and new configuration are | |
733 | * identical, return immediately. | |
734 | * | |
735 | * The user is expected to hold link->source->parent->mutex. If not, | |
736 | * media_entity_setup_link() should be used instead. | |
737 | */ | |
738 | int __media_entity_setup_link(struct media_link *link, u32 flags) | |
739 | { | |
7a6f0b22 | 740 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
97548ed4 LP |
741 | struct media_device *mdev; |
742 | struct media_entity *source, *sink; | |
743 | int ret = -EBUSY; | |
744 | ||
745 | if (link == NULL) | |
746 | return -EINVAL; | |
747 | ||
7a6f0b22 LP |
748 | /* The non-modifiable link flags must not be modified. */ |
749 | if ((link->flags & ~mask) != (flags & ~mask)) | |
750 | return -EINVAL; | |
751 | ||
97548ed4 LP |
752 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
753 | return link->flags == flags ? 0 : -EINVAL; | |
754 | ||
755 | if (link->flags == flags) | |
756 | return 0; | |
757 | ||
758 | source = link->source->entity; | |
759 | sink = link->sink->entity; | |
760 | ||
e02188c9 LP |
761 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
762 | (source->stream_count || sink->stream_count)) | |
763 | return -EBUSY; | |
764 | ||
d10c9894 | 765 | mdev = source->graph_obj.mdev; |
97548ed4 | 766 | |
813f5c0a SN |
767 | if (mdev->link_notify) { |
768 | ret = mdev->link_notify(link, flags, | |
769 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); | |
97548ed4 LP |
770 | if (ret < 0) |
771 | return ret; | |
772 | } | |
773 | ||
774 | ret = __media_entity_setup_link_notify(link, flags); | |
97548ed4 | 775 | |
813f5c0a SN |
776 | if (mdev->link_notify) |
777 | mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH); | |
97548ed4 LP |
778 | |
779 | return ret; | |
780 | } | |
781 | ||
782 | int media_entity_setup_link(struct media_link *link, u32 flags) | |
783 | { | |
784 | int ret; | |
785 | ||
a08fad1e | 786 | spin_lock(&link->source->entity->graph_obj.mdev->lock); |
97548ed4 | 787 | ret = __media_entity_setup_link(link, flags); |
a08fad1e | 788 | spin_unlock(&link->source->entity->graph_obj.mdev->lock); |
97548ed4 LP |
789 | |
790 | return ret; | |
791 | } | |
792 | EXPORT_SYMBOL_GPL(media_entity_setup_link); | |
793 | ||
794 | /** | |
795 | * media_entity_find_link - Find a link between two pads | |
796 | * @source: Source pad | |
797 | * @sink: Sink pad | |
798 | * | |
799 | * Return a pointer to the link between the two entities. If no such link | |
800 | * exists, return NULL. | |
801 | */ | |
802 | struct media_link * | |
803 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) | |
804 | { | |
805 | struct media_link *link; | |
97548ed4 | 806 | |
57208e5e | 807 | list_for_each_entry(link, &source->entity->links, list) { |
97548ed4 LP |
808 | if (link->source->entity == source->entity && |
809 | link->source->index == source->index && | |
810 | link->sink->entity == sink->entity && | |
811 | link->sink->index == sink->index) | |
812 | return link; | |
813 | } | |
814 | ||
815 | return NULL; | |
816 | } | |
817 | EXPORT_SYMBOL_GPL(media_entity_find_link); | |
818 | ||
819 | /** | |
1bddf1b3 AH |
820 | * media_entity_remote_pad - Find the pad at the remote end of a link |
821 | * @pad: Pad at the local end of the link | |
97548ed4 | 822 | * |
1bddf1b3 AH |
823 | * Search for a remote pad connected to the given pad by iterating over all |
824 | * links originating or terminating at that pad until an enabled link is found. | |
97548ed4 LP |
825 | * |
826 | * Return a pointer to the pad at the remote end of the first found enabled | |
827 | * link, or NULL if no enabled link has been found. | |
828 | */ | |
1bddf1b3 | 829 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
97548ed4 | 830 | { |
57208e5e | 831 | struct media_link *link; |
97548ed4 | 832 | |
57208e5e | 833 | list_for_each_entry(link, &pad->entity->links, list) { |
97548ed4 LP |
834 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
835 | continue; | |
836 | ||
837 | if (link->source == pad) | |
838 | return link->sink; | |
839 | ||
840 | if (link->sink == pad) | |
841 | return link->source; | |
842 | } | |
843 | ||
844 | return NULL; | |
845 | ||
846 | } | |
1bddf1b3 | 847 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |
27e543fa | 848 | |
1283f849 MCC |
849 | static void media_interface_init(struct media_device *mdev, |
850 | struct media_interface *intf, | |
851 | u32 gobj_type, | |
852 | u32 intf_type, u32 flags) | |
853 | { | |
854 | intf->type = intf_type; | |
855 | intf->flags = flags; | |
856 | INIT_LIST_HEAD(&intf->links); | |
857 | ||
858 | media_gobj_init(mdev, gobj_type, &intf->graph_obj); | |
859 | } | |
860 | ||
27e543fa MCC |
861 | /* Functions related to the media interface via device nodes */ |
862 | ||
863 | struct media_intf_devnode *media_devnode_create(struct media_device *mdev, | |
864 | u32 type, u32 flags, | |
0b3b72df | 865 | u32 major, u32 minor) |
27e543fa MCC |
866 | { |
867 | struct media_intf_devnode *devnode; | |
27e543fa | 868 | |
0b3b72df | 869 | devnode = kzalloc(sizeof(*devnode), GFP_KERNEL); |
27e543fa MCC |
870 | if (!devnode) |
871 | return NULL; | |
872 | ||
27e543fa MCC |
873 | devnode->major = major; |
874 | devnode->minor = minor; | |
875 | ||
1283f849 MCC |
876 | media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE, |
877 | type, flags); | |
27e543fa MCC |
878 | |
879 | return devnode; | |
880 | } | |
881 | EXPORT_SYMBOL_GPL(media_devnode_create); | |
882 | ||
883 | void media_devnode_remove(struct media_intf_devnode *devnode) | |
884 | { | |
7c4696a9 | 885 | media_remove_intf_links(&devnode->intf); |
27e543fa MCC |
886 | media_gobj_remove(&devnode->intf.graph_obj); |
887 | kfree(devnode); | |
888 | } | |
889 | EXPORT_SYMBOL_GPL(media_devnode_remove); | |
86e26620 MCC |
890 | |
891 | struct media_link *media_create_intf_link(struct media_entity *entity, | |
892 | struct media_interface *intf, | |
893 | u32 flags) | |
894 | { | |
895 | struct media_link *link; | |
896 | ||
897 | link = media_add_link(&intf->links); | |
898 | if (link == NULL) | |
899 | return NULL; | |
900 | ||
901 | link->intf = intf; | |
902 | link->entity = entity; | |
903 | link->flags = flags; | |
904 | ||
905 | /* Initialize graph object embedded at the new link */ | |
906 | media_gobj_init(intf->graph_obj.mdev, MEDIA_GRAPH_LINK, | |
907 | &link->graph_obj); | |
908 | ||
909 | return link; | |
910 | } | |
911 | EXPORT_SYMBOL_GPL(media_create_intf_link); | |
912 | ||
d47109fa | 913 | void __media_remove_intf_link(struct media_link *link) |
86e26620 | 914 | { |
d47109fa | 915 | list_del(&link->list); |
86e26620 MCC |
916 | media_gobj_remove(&link->graph_obj); |
917 | kfree(link); | |
918 | } | |
d47109fa | 919 | EXPORT_SYMBOL_GPL(__media_remove_intf_link); |
86e26620 MCC |
920 | |
921 | void media_remove_intf_link(struct media_link *link) | |
922 | { | |
a08fad1e | 923 | spin_lock(&link->graph_obj.mdev->lock); |
86e26620 | 924 | __media_remove_intf_link(link); |
a08fad1e | 925 | spin_unlock(&link->graph_obj.mdev->lock); |
86e26620 MCC |
926 | } |
927 | EXPORT_SYMBOL_GPL(media_remove_intf_link); | |
7c4696a9 MCC |
928 | |
929 | void __media_remove_intf_links(struct media_interface *intf) | |
930 | { | |
931 | struct media_link *link, *tmp; | |
932 | ||
933 | list_for_each_entry_safe(link, tmp, &intf->links, list) | |
934 | __media_remove_intf_link(link); | |
935 | ||
936 | } | |
937 | EXPORT_SYMBOL_GPL(__media_remove_intf_links); | |
938 | ||
939 | void media_remove_intf_links(struct media_interface *intf) | |
940 | { | |
941 | /* Do nothing if the intf is not registered. */ | |
942 | if (intf->graph_obj.mdev == NULL) | |
943 | return; | |
944 | ||
a08fad1e | 945 | spin_lock(&intf->graph_obj.mdev->lock); |
7c4696a9 | 946 | __media_remove_intf_links(intf); |
a08fad1e | 947 | spin_unlock(&intf->graph_obj.mdev->lock); |
7c4696a9 MCC |
948 | } |
949 | EXPORT_SYMBOL_GPL(media_remove_intf_links); |