1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <media/media-entity.h>
26#include <media/media-device.h>
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52int
53media_entity_init(struct media_entity *entity, u16 num_pads,
54 struct media_pad *pads, u16 extra_links)
55{
56 struct media_link *links;
57 unsigned int max_links = num_pads + extra_links;
58 unsigned int i;
59
60 links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
61 if (links == NULL)
62 return -ENOMEM;
63
64 entity->group_id = 0;
65 entity->max_links = max_links;
66 entity->num_links = 0;
67 entity->num_backlinks = 0;
68 entity->num_pads = num_pads;
69 entity->pads = pads;
70 entity->links = links;
71
72 for (i = 0; i < num_pads; i++) {
73 pads[i].entity = entity;
74 pads[i].index = i;
75 }
76
77 return 0;
78}
79EXPORT_SYMBOL_GPL(media_entity_init);
80
81void
82media_entity_cleanup(struct media_entity *entity)
83{
84 kfree(entity->links);
85}
86EXPORT_SYMBOL_GPL(media_entity_cleanup);
87
88
89
90
91
92static struct media_entity *
93media_entity_other(struct media_entity *entity, struct media_link *link)
94{
95 if (link->source->entity == entity)
96 return link->sink->entity;
97 else
98 return link->source->entity;
99}
100
101
102static void stack_push(struct media_entity_graph *graph,
103 struct media_entity *entity)
104{
105 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
106 WARN_ON(1);
107 return;
108 }
109 graph->top++;
110 graph->stack[graph->top].link = 0;
111 graph->stack[graph->top].entity = entity;
112}
113
114static struct media_entity *stack_pop(struct media_entity_graph *graph)
115{
116 struct media_entity *entity;
117
118 entity = graph->stack[graph->top].entity;
119 graph->top--;
120
121 return entity;
122}
123
124#define stack_peek(en) ((en)->stack[(en)->top - 1].entity)
125#define link_top(en) ((en)->stack[(en)->top].link)
126#define stack_top(en) ((en)->stack[(en)->top].entity)
127
128
129
130
131
132
133
134
135
136
137
138void media_entity_graph_walk_start(struct media_entity_graph *graph,
139 struct media_entity *entity)
140{
141 graph->top = 0;
142 graph->stack[graph->top].entity = NULL;
143 stack_push(graph, entity);
144}
145EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
146
147
148
149
150
151
152
153
154
155
156
157
158
159struct media_entity *
160media_entity_graph_walk_next(struct media_entity_graph *graph)
161{
162 if (stack_top(graph) == NULL)
163 return NULL;
164
165
166
167
168
169
170 while (link_top(graph) < stack_top(graph)->num_links) {
171 struct media_entity *entity = stack_top(graph);
172 struct media_link *link = &entity->links[link_top(graph)];
173 struct media_entity *next;
174
175
176 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
177 link_top(graph)++;
178 continue;
179 }
180
181
182 next = media_entity_other(entity, link);
183
184
185 if (next == stack_peek(graph)) {
186 link_top(graph)++;
187 continue;
188 }
189
190
191 link_top(graph)++;
192 stack_push(graph, next);
193 }
194
195 return stack_pop(graph);
196}
197EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217void media_entity_pipeline_start(struct media_entity *entity,
218 struct media_pipeline *pipe)
219{
220 struct media_device *mdev = entity->parent;
221 struct media_entity_graph graph;
222
223 mutex_lock(&mdev->graph_mutex);
224
225 media_entity_graph_walk_start(&graph, entity);
226
227 while ((entity = media_entity_graph_walk_next(&graph))) {
228 entity->stream_count++;
229 WARN_ON(entity->pipe && entity->pipe != pipe);
230 entity->pipe = pipe;
231 }
232
233 mutex_unlock(&mdev->graph_mutex);
234}
235EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
236
237
238
239
240
241
242
243
244
245
246
247
248
249void media_entity_pipeline_stop(struct media_entity *entity)
250{
251 struct media_device *mdev = entity->parent;
252 struct media_entity_graph graph;
253
254 mutex_lock(&mdev->graph_mutex);
255
256 media_entity_graph_walk_start(&graph, entity);
257
258 while ((entity = media_entity_graph_walk_next(&graph))) {
259 entity->stream_count--;
260 if (entity->stream_count == 0)
261 entity->pipe = NULL;
262 }
263
264 mutex_unlock(&mdev->graph_mutex);
265}
266EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282struct media_entity *media_entity_get(struct media_entity *entity)
283{
284 if (entity == NULL)
285 return NULL;
286
287 if (entity->parent->dev &&
288 !try_module_get(entity->parent->dev->driver->owner))
289 return NULL;
290
291 return entity;
292}
293EXPORT_SYMBOL_GPL(media_entity_get);
294
295
296
297
298
299
300
301
302
303void media_entity_put(struct media_entity *entity)
304{
305 if (entity == NULL)
306 return;
307
308 if (entity->parent->dev)
309 module_put(entity->parent->dev->driver->owner);
310}
311EXPORT_SYMBOL_GPL(media_entity_put);
312
313
314
315
316
317static struct media_link *media_entity_add_link(struct media_entity *entity)
318{
319 if (entity->num_links >= entity->max_links) {
320 struct media_link *links = entity->links;
321 unsigned int max_links = entity->max_links + 2;
322 unsigned int i;
323
324 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
325 if (links == NULL)
326 return NULL;
327
328 for (i = 0; i < entity->num_links; i++)
329 links[i].reverse->reverse = &links[i];
330
331 entity->max_links = max_links;
332 entity->links = links;
333 }
334
335 return &entity->links[entity->num_links++];
336}
337
338int
339media_entity_create_link(struct media_entity *source, u16 source_pad,
340 struct media_entity *sink, u16 sink_pad, u32 flags)
341{
342 struct media_link *link;
343 struct media_link *backlink;
344
345 BUG_ON(source == NULL || sink == NULL);
346 BUG_ON(source_pad >= source->num_pads);
347 BUG_ON(sink_pad >= sink->num_pads);
348
349 link = media_entity_add_link(source);
350 if (link == NULL)
351 return -ENOMEM;
352
353 link->source = &source->pads[source_pad];
354 link->sink = &sink->pads[sink_pad];
355 link->flags = flags;
356
357
358
359
360 backlink = media_entity_add_link(sink);
361 if (backlink == NULL) {
362 source->num_links--;
363 return -ENOMEM;
364 }
365
366 backlink->source = &source->pads[source_pad];
367 backlink->sink = &sink->pads[sink_pad];
368 backlink->flags = flags;
369
370 link->reverse = backlink;
371 backlink->reverse = link;
372
373 sink->num_backlinks++;
374
375 return 0;
376}
377EXPORT_SYMBOL_GPL(media_entity_create_link);
378
379static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
380{
381 int ret;
382
383
384 ret = media_entity_call(link->source->entity, link_setup,
385 link->source, link->sink, flags);
386 if (ret < 0 && ret != -ENOIOCTLCMD)
387 return ret;
388
389 ret = media_entity_call(link->sink->entity, link_setup,
390 link->sink, link->source, flags);
391 if (ret < 0 && ret != -ENOIOCTLCMD) {
392 media_entity_call(link->source->entity, link_setup,
393 link->source, link->sink, link->flags);
394 return ret;
395 }
396
397 link->flags = flags;
398 link->reverse->flags = link->flags;
399
400 return 0;
401}
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417int __media_entity_setup_link(struct media_link *link, u32 flags)
418{
419 const u32 mask = MEDIA_LNK_FL_ENABLED;
420 struct media_device *mdev;
421 struct media_entity *source, *sink;
422 int ret = -EBUSY;
423
424 if (link == NULL)
425 return -EINVAL;
426
427
428 if ((link->flags & ~mask) != (flags & ~mask))
429 return -EINVAL;
430
431 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
432 return link->flags == flags ? 0 : -EINVAL;
433
434 if (link->flags == flags)
435 return 0;
436
437 source = link->source->entity;
438 sink = link->sink->entity;
439
440 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
441 (source->stream_count || sink->stream_count))
442 return -EBUSY;
443
444 mdev = source->parent;
445
446 if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) {
447 ret = mdev->link_notify(link->source, link->sink,
448 MEDIA_LNK_FL_ENABLED);
449 if (ret < 0)
450 return ret;
451 }
452
453 ret = __media_entity_setup_link_notify(link, flags);
454 if (ret < 0)
455 goto err;
456
457 if (!(flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
458 mdev->link_notify(link->source, link->sink, 0);
459
460 return 0;
461
462err:
463 if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
464 mdev->link_notify(link->source, link->sink, 0);
465
466 return ret;
467}
468
469int media_entity_setup_link(struct media_link *link, u32 flags)
470{
471 int ret;
472
473 mutex_lock(&link->source->entity->parent->graph_mutex);
474 ret = __media_entity_setup_link(link, flags);
475 mutex_unlock(&link->source->entity->parent->graph_mutex);
476
477 return ret;
478}
479EXPORT_SYMBOL_GPL(media_entity_setup_link);
480
481
482
483
484
485
486
487
488
489struct media_link *
490media_entity_find_link(struct media_pad *source, struct media_pad *sink)
491{
492 struct media_link *link;
493 unsigned int i;
494
495 for (i = 0; i < source->entity->num_links; ++i) {
496 link = &source->entity->links[i];
497
498 if (link->source->entity == source->entity &&
499 link->source->index == source->index &&
500 link->sink->entity == sink->entity &&
501 link->sink->index == sink->index)
502 return link;
503 }
504
505 return NULL;
506}
507EXPORT_SYMBOL_GPL(media_entity_find_link);
508
509
510
511
512
513
514
515
516
517
518
519
520struct media_pad *media_entity_remote_source(struct media_pad *pad)
521{
522 unsigned int i;
523
524 for (i = 0; i < pad->entity->num_links; i++) {
525 struct media_link *link = &pad->entity->links[i];
526
527 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
528 continue;
529
530 if (link->source == pad)
531 return link->sink;
532
533 if (link->sink == pad)
534 return link->source;
535 }
536
537 return NULL;
538
539}
540EXPORT_SYMBOL_GPL(media_entity_remote_source);
541