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
217__must_check int 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 struct media_entity *entity_err = entity;
223 int ret;
224
225 mutex_lock(&mdev->graph_mutex);
226
227 media_entity_graph_walk_start(&graph, entity);
228
229 while ((entity = media_entity_graph_walk_next(&graph))) {
230 unsigned int i;
231
232 entity->stream_count++;
233 WARN_ON(entity->pipe && entity->pipe != pipe);
234 entity->pipe = pipe;
235
236
237 if (entity->stream_count > 1)
238 continue;
239
240 if (!entity->ops || !entity->ops->link_validate)
241 continue;
242
243 for (i = 0; i < entity->num_links; i++) {
244 struct media_link *link = &entity->links[i];
245
246
247 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
248 continue;
249
250
251 if (link->sink->entity != entity)
252 continue;
253
254 ret = entity->ops->link_validate(link);
255 if (ret < 0 && ret != -ENOIOCTLCMD)
256 goto error;
257 }
258 }
259
260 mutex_unlock(&mdev->graph_mutex);
261
262 return 0;
263
264error:
265
266
267
268
269 media_entity_graph_walk_start(&graph, entity_err);
270
271 while ((entity_err = media_entity_graph_walk_next(&graph))) {
272 entity_err->stream_count--;
273 if (entity_err->stream_count == 0)
274 entity_err->pipe = NULL;
275
276
277
278
279
280 if (entity_err == entity)
281 break;
282 }
283
284 mutex_unlock(&mdev->graph_mutex);
285
286 return ret;
287}
288EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
289
290
291
292
293
294
295
296
297
298
299
300
301
302void media_entity_pipeline_stop(struct media_entity *entity)
303{
304 struct media_device *mdev = entity->parent;
305 struct media_entity_graph graph;
306
307 mutex_lock(&mdev->graph_mutex);
308
309 media_entity_graph_walk_start(&graph, entity);
310
311 while ((entity = media_entity_graph_walk_next(&graph))) {
312 entity->stream_count--;
313 if (entity->stream_count == 0)
314 entity->pipe = NULL;
315 }
316
317 mutex_unlock(&mdev->graph_mutex);
318}
319EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335struct media_entity *media_entity_get(struct media_entity *entity)
336{
337 if (entity == NULL)
338 return NULL;
339
340 if (entity->parent->dev &&
341 !try_module_get(entity->parent->dev->driver->owner))
342 return NULL;
343
344 return entity;
345}
346EXPORT_SYMBOL_GPL(media_entity_get);
347
348
349
350
351
352
353
354
355
356void media_entity_put(struct media_entity *entity)
357{
358 if (entity == NULL)
359 return;
360
361 if (entity->parent->dev)
362 module_put(entity->parent->dev->driver->owner);
363}
364EXPORT_SYMBOL_GPL(media_entity_put);
365
366
367
368
369
370static struct media_link *media_entity_add_link(struct media_entity *entity)
371{
372 if (entity->num_links >= entity->max_links) {
373 struct media_link *links = entity->links;
374 unsigned int max_links = entity->max_links + 2;
375 unsigned int i;
376
377 links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
378 if (links == NULL)
379 return NULL;
380
381 for (i = 0; i < entity->num_links; i++)
382 links[i].reverse->reverse = &links[i];
383
384 entity->max_links = max_links;
385 entity->links = links;
386 }
387
388 return &entity->links[entity->num_links++];
389}
390
391int
392media_entity_create_link(struct media_entity *source, u16 source_pad,
393 struct media_entity *sink, u16 sink_pad, u32 flags)
394{
395 struct media_link *link;
396 struct media_link *backlink;
397
398 BUG_ON(source == NULL || sink == NULL);
399 BUG_ON(source_pad >= source->num_pads);
400 BUG_ON(sink_pad >= sink->num_pads);
401
402 link = media_entity_add_link(source);
403 if (link == NULL)
404 return -ENOMEM;
405
406 link->source = &source->pads[source_pad];
407 link->sink = &sink->pads[sink_pad];
408 link->flags = flags;
409
410
411
412
413 backlink = media_entity_add_link(sink);
414 if (backlink == NULL) {
415 source->num_links--;
416 return -ENOMEM;
417 }
418
419 backlink->source = &source->pads[source_pad];
420 backlink->sink = &sink->pads[sink_pad];
421 backlink->flags = flags;
422
423 link->reverse = backlink;
424 backlink->reverse = link;
425
426 sink->num_backlinks++;
427
428 return 0;
429}
430EXPORT_SYMBOL_GPL(media_entity_create_link);
431
432static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
433{
434 int ret;
435
436
437 ret = media_entity_call(link->source->entity, link_setup,
438 link->source, link->sink, flags);
439 if (ret < 0 && ret != -ENOIOCTLCMD)
440 return ret;
441
442 ret = media_entity_call(link->sink->entity, link_setup,
443 link->sink, link->source, flags);
444 if (ret < 0 && ret != -ENOIOCTLCMD) {
445 media_entity_call(link->source->entity, link_setup,
446 link->source, link->sink, link->flags);
447 return ret;
448 }
449
450 link->flags = flags;
451 link->reverse->flags = link->flags;
452
453 return 0;
454}
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470int __media_entity_setup_link(struct media_link *link, u32 flags)
471{
472 const u32 mask = MEDIA_LNK_FL_ENABLED;
473 struct media_device *mdev;
474 struct media_entity *source, *sink;
475 int ret = -EBUSY;
476
477 if (link == NULL)
478 return -EINVAL;
479
480
481 if ((link->flags & ~mask) != (flags & ~mask))
482 return -EINVAL;
483
484 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
485 return link->flags == flags ? 0 : -EINVAL;
486
487 if (link->flags == flags)
488 return 0;
489
490 source = link->source->entity;
491 sink = link->sink->entity;
492
493 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
494 (source->stream_count || sink->stream_count))
495 return -EBUSY;
496
497 mdev = source->parent;
498
499 if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) {
500 ret = mdev->link_notify(link->source, link->sink,
501 MEDIA_LNK_FL_ENABLED);
502 if (ret < 0)
503 return ret;
504 }
505
506 ret = __media_entity_setup_link_notify(link, flags);
507 if (ret < 0)
508 goto err;
509
510 if (!(flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
511 mdev->link_notify(link->source, link->sink, 0);
512
513 return 0;
514
515err:
516 if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
517 mdev->link_notify(link->source, link->sink, 0);
518
519 return ret;
520}
521
522int media_entity_setup_link(struct media_link *link, u32 flags)
523{
524 int ret;
525
526 mutex_lock(&link->source->entity->parent->graph_mutex);
527 ret = __media_entity_setup_link(link, flags);
528 mutex_unlock(&link->source->entity->parent->graph_mutex);
529
530 return ret;
531}
532EXPORT_SYMBOL_GPL(media_entity_setup_link);
533
534
535
536
537
538
539
540
541
542struct media_link *
543media_entity_find_link(struct media_pad *source, struct media_pad *sink)
544{
545 struct media_link *link;
546 unsigned int i;
547
548 for (i = 0; i < source->entity->num_links; ++i) {
549 link = &source->entity->links[i];
550
551 if (link->source->entity == source->entity &&
552 link->source->index == source->index &&
553 link->sink->entity == sink->entity &&
554 link->sink->index == sink->index)
555 return link;
556 }
557
558 return NULL;
559}
560EXPORT_SYMBOL_GPL(media_entity_find_link);
561
562
563
564
565
566
567
568
569
570
571
572
573struct media_pad *media_entity_remote_source(struct media_pad *pad)
574{
575 unsigned int i;
576
577 for (i = 0; i < pad->entity->num_links; i++) {
578 struct media_link *link = &pad->entity->links[i];
579
580 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
581 continue;
582
583 if (link->source == pad)
584 return link->sink;
585
586 if (link->sink == pad)
587 return link->source;
588 }
589
590 return NULL;
591
592}
593EXPORT_SYMBOL_GPL(media_entity_remote_source);
594