]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/media-entity.c
ipv4: convert dst_metrics.refcnt from atomic_t to refcount_t
[mirror_ubuntu-artful-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
1fc25d30
MCC
217int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
218 struct media_pad *pads)
53e269c1 219{
db141a35 220 struct media_device *mdev = entity->graph_obj.mdev;
53e269c1
LP
221 unsigned int i;
222
53e269c1
LP
223 entity->num_pads = num_pads;
224 entity->pads = pads;
53e269c1 225
db141a35 226 if (mdev)
e2c91d4d 227 mutex_lock(&mdev->graph_mutex);
db141a35 228
53e269c1
LP
229 for (i = 0; i < num_pads; i++) {
230 pads[i].entity = entity;
231 pads[i].index = i;
db141a35 232 if (mdev)
c350ef83 233 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
db141a35 234 &entity->pads[i].graph_obj);
53e269c1
LP
235 }
236
db141a35 237 if (mdev)
e2c91d4d 238 mutex_unlock(&mdev->graph_mutex);
db141a35 239
53e269c1
LP
240 return 0;
241}
ab22e77c 242EXPORT_SYMBOL_GPL(media_entity_pads_init);
53e269c1 243
a5ccc48a
SA
244/* -----------------------------------------------------------------------------
245 * Graph traversal
246 */
247
248static struct media_entity *
249media_entity_other(struct media_entity *entity, struct media_link *link)
250{
251 if (link->source->entity == entity)
252 return link->sink->entity;
253 else
254 return link->source->entity;
255}
256
257/* push an entity to traversal stack */
20b85227 258static void stack_push(struct media_graph *graph,
a5ccc48a
SA
259 struct media_entity *entity)
260{
261 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
262 WARN_ON(1);
263 return;
264 }
265 graph->top++;
313895fb 266 graph->stack[graph->top].link = entity->links.next;
a5ccc48a
SA
267 graph->stack[graph->top].entity = entity;
268}
269
20b85227 270static struct media_entity *stack_pop(struct media_graph *graph)
a5ccc48a
SA
271{
272 struct media_entity *entity;
273
274 entity = graph->stack[graph->top].entity;
275 graph->top--;
276
277 return entity;
278}
279
a5ccc48a
SA
280#define link_top(en) ((en)->stack[(en)->top].link)
281#define stack_top(en) ((en)->stack[(en)->top].entity)
282
0798ce4a
SA
283/*
284 * TODO: Get rid of this.
285 */
430a672c 286#define MEDIA_ENTITY_MAX_PADS 512
0798ce4a 287
e03d2203 288/**
20b85227 289 * media_graph_walk_init - Allocate resources for graph walk
e03d2203
SA
290 * @graph: Media graph structure that will be used to walk the graph
291 * @mdev: Media device
292 *
293 * Reserve resources for graph walk in media device's current
294 * state. The memory must be released using
20b85227 295 * media_graph_walk_free().
e03d2203
SA
296 *
297 * Returns error on failure, zero on success.
298 */
20b85227
SA
299__must_check int media_graph_walk_init(
300 struct media_graph *graph, struct media_device *mdev)
e03d2203 301{
29d8da02 302 return media_entity_enum_init(&graph->ent_enum, mdev);
e03d2203 303}
20b85227 304EXPORT_SYMBOL_GPL(media_graph_walk_init);
e03d2203
SA
305
306/**
20b85227 307 * media_graph_walk_cleanup - Release resources related to graph walking
e03d2203
SA
308 * @graph: Media graph structure that was used to walk the graph
309 */
20b85227 310void media_graph_walk_cleanup(struct media_graph *graph)
e03d2203 311{
29d8da02 312 media_entity_enum_cleanup(&graph->ent_enum);
e03d2203 313}
20b85227 314EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
e03d2203 315
20b85227
SA
316void media_graph_walk_start(struct media_graph *graph,
317 struct media_entity *entity)
a5ccc48a 318{
29d8da02
SA
319 media_entity_enum_zero(&graph->ent_enum);
320 media_entity_enum_set(&graph->ent_enum, entity);
321
a5ccc48a
SA
322 graph->top = 0;
323 graph->stack[graph->top].entity = NULL;
324 stack_push(graph, entity);
aa79a84f
SA
325 dev_dbg(entity->graph_obj.mdev->dev,
326 "begin graph walk at '%s'\n", entity->name);
a5ccc48a 327}
20b85227 328EXPORT_SYMBOL_GPL(media_graph_walk_start);
a5ccc48a 329
5b1f8329
SA
330static void media_graph_walk_iter(struct media_graph *graph)
331{
332 struct media_entity *entity = stack_top(graph);
333 struct media_link *link;
334 struct media_entity *next;
335
336 link = list_entry(link_top(graph), typeof(*link), list);
337
338 /* The link is not enabled so we do not follow. */
339 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
340 link_top(graph) = link_top(graph)->next;
aa79a84f
SA
341 dev_dbg(entity->graph_obj.mdev->dev,
342 "walk: skipping disabled link '%s':%u -> '%s':%u\n",
343 link->source->entity->name, link->source->index,
344 link->sink->entity->name, link->sink->index);
5b1f8329
SA
345 return;
346 }
347
348 /* Get the entity in the other end of the link . */
349 next = media_entity_other(entity, link);
350
351 /* Has the entity already been visited? */
352 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
353 link_top(graph) = link_top(graph)->next;
aa79a84f
SA
354 dev_dbg(entity->graph_obj.mdev->dev,
355 "walk: skipping entity '%s' (already seen)\n",
356 next->name);
5b1f8329
SA
357 return;
358 }
359
360 /* Push the new entity to stack and start over. */
361 link_top(graph) = link_top(graph)->next;
362 stack_push(graph, next);
aa79a84f
SA
363 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
364 next->name);
5b1f8329
SA
365}
366
20b85227 367struct media_entity *media_graph_walk_next(struct media_graph *graph)
a5ccc48a 368{
aa79a84f
SA
369 struct media_entity *entity;
370
a5ccc48a
SA
371 if (stack_top(graph) == NULL)
372 return NULL;
373
374 /*
375 * Depth first search. Push entity to stack and continue from
376 * top of the stack until no more entities on the level can be
377 * found.
378 */
5b1f8329
SA
379 while (link_top(graph) != &stack_top(graph)->links)
380 media_graph_walk_iter(graph);
a5ccc48a 381
aa79a84f
SA
382 entity = stack_pop(graph);
383 dev_dbg(entity->graph_obj.mdev->dev,
384 "walk: returning entity '%s'\n", entity->name);
385
386 return entity;
a5ccc48a 387}
20b85227 388EXPORT_SYMBOL_GPL(media_graph_walk_next);
a5ccc48a 389
d295c6a4
NS
390int media_entity_get_fwnode_pad(struct media_entity *entity,
391 struct fwnode_handle *fwnode,
392 unsigned long direction_flags)
393{
394 struct fwnode_endpoint endpoint;
395 unsigned int i;
396 int ret;
397
398 if (!entity->ops || !entity->ops->get_fwnode_pad) {
399 for (i = 0; i < entity->num_pads; i++) {
400 if (entity->pads[i].flags & direction_flags)
401 return i;
402 }
403
404 return -ENXIO;
405 }
406
407 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
408 if (ret)
409 return ret;
410
411 ret = entity->ops->get_fwnode_pad(&endpoint);
412 if (ret < 0)
413 return ret;
414
415 if (ret >= entity->num_pads)
416 return -ENXIO;
417
418 if (!(entity->pads[ret].flags & direction_flags))
419 return -ENXIO;
420
421 return ret;
422}
423EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
424
e02188c9
LP
425/* -----------------------------------------------------------------------------
426 * Pipeline management
427 */
428
20b85227
SA
429__must_check int __media_pipeline_start(struct media_entity *entity,
430 struct media_pipeline *pipe)
e02188c9 431{
d10c9894 432 struct media_device *mdev = entity->graph_obj.mdev;
20b85227 433 struct media_graph *graph = &pipe->graph;
af88be38 434 struct media_entity *entity_err = entity;
57208e5e 435 struct media_link *link;
af88be38 436 int ret;
e02188c9 437
74a41330 438 if (!pipe->streaming_count++) {
20b85227 439 ret = media_graph_walk_init(&pipe->graph, mdev);
74a41330
SA
440 if (ret)
441 goto error_graph_walk_start;
106b9907
SA
442 }
443
20b85227 444 media_graph_walk_start(&pipe->graph, entity);
e02188c9 445
20b85227 446 while ((entity = media_graph_walk_next(graph))) {
ef69ee1b
MCC
447 DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
448 DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
af88be38 449
e02188c9 450 entity->stream_count++;
8aaf62b5
SA
451
452 if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
453 ret = -EBUSY;
454 goto error;
455 }
456
e02188c9 457 entity->pipe = pipe;
af88be38
SA
458
459 /* Already streaming --- no need to check. */
460 if (entity->stream_count > 1)
461 continue;
462
463 if (!entity->ops || !entity->ops->link_validate)
464 continue;
465
de49c285
SA
466 bitmap_zero(active, entity->num_pads);
467 bitmap_fill(has_no_links, entity->num_pads);
468
57208e5e 469 list_for_each_entry(link, &entity->links, list) {
de49c285
SA
470 struct media_pad *pad = link->sink->entity == entity
471 ? link->sink : link->source;
472
473 /* Mark that a pad is connected by a link. */
474 bitmap_clear(has_no_links, pad->index, 1);
475
476 /*
477 * Pads that either do not need to connect or
478 * are connected through an enabled link are
479 * fine.
480 */
481 if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
482 link->flags & MEDIA_LNK_FL_ENABLED)
483 bitmap_set(active, pad->index, 1);
484
485 /*
486 * Link validation will only take place for
487 * sink ends of the link that are enabled.
488 */
489 if (link->sink != pad ||
490 !(link->flags & MEDIA_LNK_FL_ENABLED))
af88be38
SA
491 continue;
492
493 ret = entity->ops->link_validate(link);
fab9d30b 494 if (ret < 0 && ret != -ENOIOCTLCMD) {
d10c9894 495 dev_dbg(entity->graph_obj.mdev->dev,
91b619ad 496 "link validation failed for '%s':%u -> '%s':%u, error %d\n",
823ea2a6
SA
497 link->source->entity->name,
498 link->source->index,
499 entity->name, link->sink->index, ret);
af88be38 500 goto error;
fab9d30b 501 }
af88be38 502 }
de49c285
SA
503
504 /* Either no links or validated links are fine. */
505 bitmap_or(active, active, has_no_links, entity->num_pads);
506
507 if (!bitmap_full(active, entity->num_pads)) {
47dfdb3a 508 ret = -ENOLINK;
d10c9894 509 dev_dbg(entity->graph_obj.mdev->dev,
91b619ad 510 "'%s':%u must be connected by an enabled link\n",
fab9d30b 511 entity->name,
094f1ca5
SA
512 (unsigned)find_first_zero_bit(
513 active, entity->num_pads));
de49c285
SA
514 goto error;
515 }
e02188c9
LP
516 }
517
af88be38
SA
518 return 0;
519
520error:
521 /*
522 * Link validation on graph failed. We revert what we did and
523 * return the error.
524 */
20b85227 525 media_graph_walk_start(graph, entity_err);
af88be38 526
20b85227 527 while ((entity_err = media_graph_walk_next(graph))) {
12030f48
SA
528 /* Sanity check for negative stream_count */
529 if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
3801bc7d
SK
530 entity_err->stream_count--;
531 if (entity_err->stream_count == 0)
532 entity_err->pipe = NULL;
533 }
af88be38
SA
534
535 /*
536 * We haven't increased stream_count further than this
537 * so we quit here.
538 */
539 if (entity_err == entity)
540 break;
541 }
542
74a41330
SA
543error_graph_walk_start:
544 if (!--pipe->streaming_count)
20b85227 545 media_graph_walk_cleanup(graph);
106b9907 546
fb49f204
SK
547 return ret;
548}
20b85227 549EXPORT_SYMBOL_GPL(__media_pipeline_start);
af88be38 550
20b85227
SA
551__must_check int media_pipeline_start(struct media_entity *entity,
552 struct media_pipeline *pipe)
fb49f204
SK
553{
554 struct media_device *mdev = entity->graph_obj.mdev;
555 int ret;
556
557 mutex_lock(&mdev->graph_mutex);
20b85227 558 ret = __media_pipeline_start(entity, pipe);
fb49f204 559 mutex_unlock(&mdev->graph_mutex);
af88be38 560 return ret;
e02188c9 561}
20b85227 562EXPORT_SYMBOL_GPL(media_pipeline_start);
e02188c9 563
20b85227 564void __media_pipeline_stop(struct media_entity *entity)
e02188c9 565{
20b85227 566 struct media_graph *graph = &entity->pipe->graph;
74a41330 567 struct media_pipeline *pipe = entity->pipe;
e02188c9 568
2a2599c6
KB
569 /*
570 * If the following check fails, the driver has performed an
571 * unbalanced call to media_pipeline_stop()
572 */
573 if (WARN_ON(!pipe))
574 return;
e02188c9 575
20b85227 576 media_graph_walk_start(graph, entity);
e02188c9 577
20b85227 578 while ((entity = media_graph_walk_next(graph))) {
12030f48
SA
579 /* Sanity check for negative stream_count */
580 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
3801bc7d
SK
581 entity->stream_count--;
582 if (entity->stream_count == 0)
583 entity->pipe = NULL;
584 }
e02188c9
LP
585 }
586
74a41330 587 if (!--pipe->streaming_count)
20b85227 588 media_graph_walk_cleanup(graph);
106b9907 589
fb49f204 590}
20b85227 591EXPORT_SYMBOL_GPL(__media_pipeline_stop);
fb49f204 592
20b85227 593void media_pipeline_stop(struct media_entity *entity)
fb49f204
SK
594{
595 struct media_device *mdev = entity->graph_obj.mdev;
596
597 mutex_lock(&mdev->graph_mutex);
20b85227 598 __media_pipeline_stop(entity);
e02188c9
LP
599 mutex_unlock(&mdev->graph_mutex);
600}
20b85227 601EXPORT_SYMBOL_GPL(media_pipeline_stop);
e02188c9 602
503c3d82
LP
603/* -----------------------------------------------------------------------------
604 * Module use count
605 */
606
503c3d82
LP
607struct media_entity *media_entity_get(struct media_entity *entity)
608{
609 if (entity == NULL)
610 return NULL;
611
d10c9894
JMC
612 if (entity->graph_obj.mdev->dev &&
613 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
503c3d82
LP
614 return NULL;
615
616 return entity;
617}
618EXPORT_SYMBOL_GPL(media_entity_get);
619
503c3d82
LP
620void media_entity_put(struct media_entity *entity)
621{
622 if (entity == NULL)
623 return;
624
d10c9894
JMC
625 if (entity->graph_obj.mdev->dev)
626 module_put(entity->graph_obj.mdev->dev->driver->owner);
503c3d82
LP
627}
628EXPORT_SYMBOL_GPL(media_entity_put);
629
a5ccc48a
SA
630/* -----------------------------------------------------------------------------
631 * Links management
632 */
633
23615de5 634static struct media_link *media_add_link(struct list_head *head)
53e269c1 635{
57208e5e 636 struct media_link *link;
53e269c1 637
57208e5e
MCC
638 link = kzalloc(sizeof(*link), GFP_KERNEL);
639 if (link == NULL)
640 return NULL;
53e269c1 641
23615de5 642 list_add_tail(&link->list, head);
53e269c1 643
57208e5e 644 return link;
53e269c1
LP
645}
646
57208e5e 647static void __media_entity_remove_link(struct media_entity *entity,
5abad22f
MCC
648 struct media_link *link)
649{
650 struct media_link *rlink, *tmp;
651 struct media_entity *remote;
5abad22f
MCC
652
653 if (link->source->entity == entity)
654 remote = link->sink->entity;
655 else
656 remote = link->source->entity;
657
658 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
58f69ee9 659 if (rlink != link->reverse)
5abad22f 660 continue;
5abad22f
MCC
661
662 if (link->source->entity == entity)
663 remote->num_backlinks--;
664
665 /* Remove the remote link */
666 list_del(&rlink->list);
c350ef83 667 media_gobj_destroy(&rlink->graph_obj);
5abad22f
MCC
668 kfree(rlink);
669
670 if (--remote->num_links == 0)
671 break;
672 }
673 list_del(&link->list);
c350ef83 674 media_gobj_destroy(&link->graph_obj);
5abad22f
MCC
675 kfree(link);
676}
57208e5e 677
53e269c1 678int
8df00a15 679media_create_pad_link(struct media_entity *source, u16 source_pad,
53e269c1
LP
680 struct media_entity *sink, u16 sink_pad, u32 flags)
681{
682 struct media_link *link;
683 struct media_link *backlink;
684
685 BUG_ON(source == NULL || sink == NULL);
686 BUG_ON(source_pad >= source->num_pads);
687 BUG_ON(sink_pad >= sink->num_pads);
688
23615de5 689 link = media_add_link(&source->links);
53e269c1
LP
690 if (link == NULL)
691 return -ENOMEM;
692
693 link->source = &source->pads[source_pad];
694 link->sink = &sink->pads[sink_pad];
82ae2a50 695 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
53e269c1 696
6b6a4278 697 /* Initialize graph object embedded at the new link */
c350ef83 698 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 699 &link->graph_obj);
6b6a4278 700
53e269c1
LP
701 /* Create the backlink. Backlinks are used to help graph traversal and
702 * are not reported to userspace.
703 */
23615de5 704 backlink = media_add_link(&sink->links);
53e269c1 705 if (backlink == NULL) {
57208e5e 706 __media_entity_remove_link(source, link);
53e269c1
LP
707 return -ENOMEM;
708 }
709
710 backlink->source = &source->pads[source_pad];
711 backlink->sink = &sink->pads[sink_pad];
712 backlink->flags = flags;
39d1ebc6 713 backlink->is_backlink = true;
53e269c1 714
6b6a4278 715 /* Initialize graph object embedded at the new link */
c350ef83 716 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 717 &backlink->graph_obj);
6b6a4278 718
53e269c1
LP
719 link->reverse = backlink;
720 backlink->reverse = link;
721
722 sink->num_backlinks++;
57208e5e
MCC
723 sink->num_links++;
724 source->num_links++;
53e269c1
LP
725
726 return 0;
727}
8df00a15 728EXPORT_SYMBOL_GPL(media_create_pad_link);
97548ed4 729
b01cc9ce
MCC
730int media_create_pad_links(const struct media_device *mdev,
731 const u32 source_function,
732 struct media_entity *source,
733 const u16 source_pad,
734 const u32 sink_function,
735 struct media_entity *sink,
736 const u16 sink_pad,
737 u32 flags,
738 const bool allow_both_undefined)
739{
740 struct media_entity *entity;
741 unsigned function;
742 int ret;
743
744 /* Trivial case: 1:1 relation */
745 if (source && sink)
746 return media_create_pad_link(source, source_pad,
747 sink, sink_pad, flags);
748
749 /* Worse case scenario: n:n relation */
750 if (!source && !sink) {
751 if (!allow_both_undefined)
752 return 0;
753 media_device_for_each_entity(source, mdev) {
754 if (source->function != source_function)
755 continue;
756 media_device_for_each_entity(sink, mdev) {
757 if (sink->function != sink_function)
758 continue;
759 ret = media_create_pad_link(source, source_pad,
760 sink, sink_pad,
761 flags);
762 if (ret)
763 return ret;
764 flags &= ~(MEDIA_LNK_FL_ENABLED |
765 MEDIA_LNK_FL_IMMUTABLE);
766 }
767 }
768 return 0;
769 }
770
771 /* Handle 1:n and n:1 cases */
772 if (source)
773 function = sink_function;
774 else
775 function = source_function;
776
777 media_device_for_each_entity(entity, mdev) {
778 if (entity->function != function)
779 continue;
780
781 if (source)
782 ret = media_create_pad_link(source, source_pad,
783 entity, sink_pad, flags);
784 else
785 ret = media_create_pad_link(entity, source_pad,
786 sink, sink_pad, flags);
787 if (ret)
788 return ret;
789 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
790 }
791 return 0;
792}
793EXPORT_SYMBOL_GPL(media_create_pad_links);
794
57208e5e
MCC
795void __media_entity_remove_links(struct media_entity *entity)
796{
797 struct media_link *link, *tmp;
7349cec1 798
57208e5e
MCC
799 list_for_each_entry_safe(link, tmp, &entity->links, list)
800 __media_entity_remove_link(entity, link);
7349cec1
SN
801
802 entity->num_links = 0;
803 entity->num_backlinks = 0;
804}
805EXPORT_SYMBOL_GPL(__media_entity_remove_links);
806
807void media_entity_remove_links(struct media_entity *entity)
808{
cc4a8258
MCC
809 struct media_device *mdev = entity->graph_obj.mdev;
810
7349cec1 811 /* Do nothing if the entity is not registered. */
cc4a8258 812 if (mdev == NULL)
7349cec1
SN
813 return;
814
e2c91d4d 815 mutex_lock(&mdev->graph_mutex);
7349cec1 816 __media_entity_remove_links(entity);
e2c91d4d 817 mutex_unlock(&mdev->graph_mutex);
7349cec1
SN
818}
819EXPORT_SYMBOL_GPL(media_entity_remove_links);
820
97548ed4
LP
821static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
822{
97548ed4
LP
823 int ret;
824
825 /* Notify both entities. */
826 ret = media_entity_call(link->source->entity, link_setup,
827 link->source, link->sink, flags);
828 if (ret < 0 && ret != -ENOIOCTLCMD)
829 return ret;
830
831 ret = media_entity_call(link->sink->entity, link_setup,
832 link->sink, link->source, flags);
833 if (ret < 0 && ret != -ENOIOCTLCMD) {
834 media_entity_call(link->source->entity, link_setup,
835 link->source, link->sink, link->flags);
836 return ret;
837 }
838
7a6f0b22 839 link->flags = flags;
97548ed4
LP
840 link->reverse->flags = link->flags;
841
842 return 0;
843}
844
97548ed4
LP
845int __media_entity_setup_link(struct media_link *link, u32 flags)
846{
7a6f0b22 847 const u32 mask = MEDIA_LNK_FL_ENABLED;
97548ed4
LP
848 struct media_device *mdev;
849 struct media_entity *source, *sink;
850 int ret = -EBUSY;
851
852 if (link == NULL)
853 return -EINVAL;
854
7a6f0b22
LP
855 /* The non-modifiable link flags must not be modified. */
856 if ((link->flags & ~mask) != (flags & ~mask))
857 return -EINVAL;
858
97548ed4
LP
859 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
860 return link->flags == flags ? 0 : -EINVAL;
861
862 if (link->flags == flags)
863 return 0;
864
865 source = link->source->entity;
866 sink = link->sink->entity;
867
e02188c9
LP
868 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
869 (source->stream_count || sink->stream_count))
870 return -EBUSY;
871
d10c9894 872 mdev = source->graph_obj.mdev;
97548ed4 873
68429f50
LP
874 if (mdev->ops && mdev->ops->link_notify) {
875 ret = mdev->ops->link_notify(link, flags,
876 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
97548ed4
LP
877 if (ret < 0)
878 return ret;
879 }
880
881 ret = __media_entity_setup_link_notify(link, flags);
97548ed4 882
68429f50
LP
883 if (mdev->ops && mdev->ops->link_notify)
884 mdev->ops->link_notify(link, flags,
885 MEDIA_DEV_NOTIFY_POST_LINK_CH);
97548ed4
LP
886
887 return ret;
888}
efc70278 889EXPORT_SYMBOL_GPL(__media_entity_setup_link);
97548ed4
LP
890
891int media_entity_setup_link(struct media_link *link, u32 flags)
892{
893 int ret;
894
5c883edb 895 mutex_lock(&link->graph_obj.mdev->graph_mutex);
97548ed4 896 ret = __media_entity_setup_link(link, flags);
5c883edb 897 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
97548ed4
LP
898
899 return ret;
900}
901EXPORT_SYMBOL_GPL(media_entity_setup_link);
902
97548ed4
LP
903struct media_link *
904media_entity_find_link(struct media_pad *source, struct media_pad *sink)
905{
906 struct media_link *link;
97548ed4 907
57208e5e 908 list_for_each_entry(link, &source->entity->links, list) {
97548ed4
LP
909 if (link->source->entity == source->entity &&
910 link->source->index == source->index &&
911 link->sink->entity == sink->entity &&
912 link->sink->index == sink->index)
913 return link;
914 }
915
916 return NULL;
917}
918EXPORT_SYMBOL_GPL(media_entity_find_link);
919
1bddf1b3 920struct media_pad *media_entity_remote_pad(struct media_pad *pad)
97548ed4 921{
57208e5e 922 struct media_link *link;
97548ed4 923
57208e5e 924 list_for_each_entry(link, &pad->entity->links, list) {
97548ed4
LP
925 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
926 continue;
927
928 if (link->source == pad)
929 return link->sink;
930
931 if (link->sink == pad)
932 return link->source;
933 }
934
935 return NULL;
936
937}
1bddf1b3 938EXPORT_SYMBOL_GPL(media_entity_remote_pad);
27e543fa 939
1283f849
MCC
940static void media_interface_init(struct media_device *mdev,
941 struct media_interface *intf,
942 u32 gobj_type,
943 u32 intf_type, u32 flags)
944{
945 intf->type = intf_type;
946 intf->flags = flags;
947 INIT_LIST_HEAD(&intf->links);
948
c350ef83 949 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1283f849
MCC
950}
951
27e543fa
MCC
952/* Functions related to the media interface via device nodes */
953
954struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
955 u32 type, u32 flags,
0b3b72df 956 u32 major, u32 minor)
27e543fa
MCC
957{
958 struct media_intf_devnode *devnode;
27e543fa 959
0b3b72df 960 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
27e543fa
MCC
961 if (!devnode)
962 return NULL;
963
27e543fa
MCC
964 devnode->major = major;
965 devnode->minor = minor;
966
1283f849
MCC
967 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
968 type, flags);
27e543fa
MCC
969
970 return devnode;
971}
972EXPORT_SYMBOL_GPL(media_devnode_create);
973
974void media_devnode_remove(struct media_intf_devnode *devnode)
975{
7c4696a9 976 media_remove_intf_links(&devnode->intf);
c350ef83 977 media_gobj_destroy(&devnode->intf.graph_obj);
27e543fa
MCC
978 kfree(devnode);
979}
980EXPORT_SYMBOL_GPL(media_devnode_remove);
86e26620
MCC
981
982struct media_link *media_create_intf_link(struct media_entity *entity,
983 struct media_interface *intf,
984 u32 flags)
985{
986 struct media_link *link;
987
988 link = media_add_link(&intf->links);
989 if (link == NULL)
990 return NULL;
991
992 link->intf = intf;
993 link->entity = entity;
82ae2a50 994 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
86e26620
MCC
995
996 /* Initialize graph object embedded at the new link */
c350ef83 997 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
86e26620
MCC
998 &link->graph_obj);
999
1000 return link;
1001}
1002EXPORT_SYMBOL_GPL(media_create_intf_link);
1003
d47109fa 1004void __media_remove_intf_link(struct media_link *link)
86e26620 1005{
d47109fa 1006 list_del(&link->list);
c350ef83 1007 media_gobj_destroy(&link->graph_obj);
86e26620
MCC
1008 kfree(link);
1009}
d47109fa 1010EXPORT_SYMBOL_GPL(__media_remove_intf_link);
86e26620
MCC
1011
1012void media_remove_intf_link(struct media_link *link)
1013{
cc4a8258
MCC
1014 struct media_device *mdev = link->graph_obj.mdev;
1015
1016 /* Do nothing if the intf is not registered. */
1017 if (mdev == NULL)
1018 return;
1019
e2c91d4d 1020 mutex_lock(&mdev->graph_mutex);
86e26620 1021 __media_remove_intf_link(link);
e2c91d4d 1022 mutex_unlock(&mdev->graph_mutex);
86e26620
MCC
1023}
1024EXPORT_SYMBOL_GPL(media_remove_intf_link);
7c4696a9
MCC
1025
1026void __media_remove_intf_links(struct media_interface *intf)
1027{
1028 struct media_link *link, *tmp;
1029
1030 list_for_each_entry_safe(link, tmp, &intf->links, list)
1031 __media_remove_intf_link(link);
1032
1033}
1034EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1035
1036void media_remove_intf_links(struct media_interface *intf)
1037{
cc4a8258
MCC
1038 struct media_device *mdev = intf->graph_obj.mdev;
1039
7c4696a9 1040 /* Do nothing if the intf is not registered. */
cc4a8258 1041 if (mdev == NULL)
7c4696a9
MCC
1042 return;
1043
e2c91d4d 1044 mutex_lock(&mdev->graph_mutex);
7c4696a9 1045 __media_remove_intf_links(intf);
e2c91d4d 1046 mutex_unlock(&mdev->graph_mutex);
7c4696a9
MCC
1047}
1048EXPORT_SYMBOL_GPL(media_remove_intf_links);