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