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