]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/media-entity.c
[media] media/uapi/v4l: clarify cropcap/crop/selection behavior
[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
e02188c9 569
74a41330 570 WARN_ON(!pipe->streaming_count);
20b85227 571 media_graph_walk_start(graph, entity);
e02188c9 572
20b85227 573 while ((entity = media_graph_walk_next(graph))) {
12030f48
SA
574 /* Sanity check for negative stream_count */
575 if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
3801bc7d
SK
576 entity->stream_count--;
577 if (entity->stream_count == 0)
578 entity->pipe = NULL;
579 }
e02188c9
LP
580 }
581
74a41330 582 if (!--pipe->streaming_count)
20b85227 583 media_graph_walk_cleanup(graph);
106b9907 584
fb49f204 585}
20b85227 586EXPORT_SYMBOL_GPL(__media_pipeline_stop);
fb49f204 587
20b85227 588void media_pipeline_stop(struct media_entity *entity)
fb49f204
SK
589{
590 struct media_device *mdev = entity->graph_obj.mdev;
591
592 mutex_lock(&mdev->graph_mutex);
20b85227 593 __media_pipeline_stop(entity);
e02188c9
LP
594 mutex_unlock(&mdev->graph_mutex);
595}
20b85227 596EXPORT_SYMBOL_GPL(media_pipeline_stop);
e02188c9 597
503c3d82
LP
598/* -----------------------------------------------------------------------------
599 * Module use count
600 */
601
503c3d82
LP
602struct media_entity *media_entity_get(struct media_entity *entity)
603{
604 if (entity == NULL)
605 return NULL;
606
d10c9894
JMC
607 if (entity->graph_obj.mdev->dev &&
608 !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
503c3d82
LP
609 return NULL;
610
611 return entity;
612}
613EXPORT_SYMBOL_GPL(media_entity_get);
614
503c3d82
LP
615void media_entity_put(struct media_entity *entity)
616{
617 if (entity == NULL)
618 return;
619
d10c9894
JMC
620 if (entity->graph_obj.mdev->dev)
621 module_put(entity->graph_obj.mdev->dev->driver->owner);
503c3d82
LP
622}
623EXPORT_SYMBOL_GPL(media_entity_put);
624
a5ccc48a
SA
625/* -----------------------------------------------------------------------------
626 * Links management
627 */
628
23615de5 629static struct media_link *media_add_link(struct list_head *head)
53e269c1 630{
57208e5e 631 struct media_link *link;
53e269c1 632
57208e5e
MCC
633 link = kzalloc(sizeof(*link), GFP_KERNEL);
634 if (link == NULL)
635 return NULL;
53e269c1 636
23615de5 637 list_add_tail(&link->list, head);
53e269c1 638
57208e5e 639 return link;
53e269c1
LP
640}
641
57208e5e 642static void __media_entity_remove_link(struct media_entity *entity,
5abad22f
MCC
643 struct media_link *link)
644{
645 struct media_link *rlink, *tmp;
646 struct media_entity *remote;
5abad22f
MCC
647
648 if (link->source->entity == entity)
649 remote = link->sink->entity;
650 else
651 remote = link->source->entity;
652
653 list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
58f69ee9 654 if (rlink != link->reverse)
5abad22f 655 continue;
5abad22f
MCC
656
657 if (link->source->entity == entity)
658 remote->num_backlinks--;
659
660 /* Remove the remote link */
661 list_del(&rlink->list);
c350ef83 662 media_gobj_destroy(&rlink->graph_obj);
5abad22f
MCC
663 kfree(rlink);
664
665 if (--remote->num_links == 0)
666 break;
667 }
668 list_del(&link->list);
c350ef83 669 media_gobj_destroy(&link->graph_obj);
5abad22f
MCC
670 kfree(link);
671}
57208e5e 672
53e269c1 673int
8df00a15 674media_create_pad_link(struct media_entity *source, u16 source_pad,
53e269c1
LP
675 struct media_entity *sink, u16 sink_pad, u32 flags)
676{
677 struct media_link *link;
678 struct media_link *backlink;
679
680 BUG_ON(source == NULL || sink == NULL);
681 BUG_ON(source_pad >= source->num_pads);
682 BUG_ON(sink_pad >= sink->num_pads);
683
23615de5 684 link = media_add_link(&source->links);
53e269c1
LP
685 if (link == NULL)
686 return -ENOMEM;
687
688 link->source = &source->pads[source_pad];
689 link->sink = &sink->pads[sink_pad];
82ae2a50 690 link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
53e269c1 691
6b6a4278 692 /* Initialize graph object embedded at the new link */
c350ef83 693 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 694 &link->graph_obj);
6b6a4278 695
53e269c1
LP
696 /* Create the backlink. Backlinks are used to help graph traversal and
697 * are not reported to userspace.
698 */
23615de5 699 backlink = media_add_link(&sink->links);
53e269c1 700 if (backlink == NULL) {
57208e5e 701 __media_entity_remove_link(source, link);
53e269c1
LP
702 return -ENOMEM;
703 }
704
705 backlink->source = &source->pads[source_pad];
706 backlink->sink = &sink->pads[sink_pad];
707 backlink->flags = flags;
39d1ebc6 708 backlink->is_backlink = true;
53e269c1 709
6b6a4278 710 /* Initialize graph object embedded at the new link */
c350ef83 711 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
d10c9894 712 &backlink->graph_obj);
6b6a4278 713
53e269c1
LP
714 link->reverse = backlink;
715 backlink->reverse = link;
716
717 sink->num_backlinks++;
57208e5e
MCC
718 sink->num_links++;
719 source->num_links++;
53e269c1
LP
720
721 return 0;
722}
8df00a15 723EXPORT_SYMBOL_GPL(media_create_pad_link);
97548ed4 724
b01cc9ce
MCC
725int media_create_pad_links(const struct media_device *mdev,
726 const u32 source_function,
727 struct media_entity *source,
728 const u16 source_pad,
729 const u32 sink_function,
730 struct media_entity *sink,
731 const u16 sink_pad,
732 u32 flags,
733 const bool allow_both_undefined)
734{
735 struct media_entity *entity;
736 unsigned function;
737 int ret;
738
739 /* Trivial case: 1:1 relation */
740 if (source && sink)
741 return media_create_pad_link(source, source_pad,
742 sink, sink_pad, flags);
743
744 /* Worse case scenario: n:n relation */
745 if (!source && !sink) {
746 if (!allow_both_undefined)
747 return 0;
748 media_device_for_each_entity(source, mdev) {
749 if (source->function != source_function)
750 continue;
751 media_device_for_each_entity(sink, mdev) {
752 if (sink->function != sink_function)
753 continue;
754 ret = media_create_pad_link(source, source_pad,
755 sink, sink_pad,
756 flags);
757 if (ret)
758 return ret;
759 flags &= ~(MEDIA_LNK_FL_ENABLED |
760 MEDIA_LNK_FL_IMMUTABLE);
761 }
762 }
763 return 0;
764 }
765
766 /* Handle 1:n and n:1 cases */
767 if (source)
768 function = sink_function;
769 else
770 function = source_function;
771
772 media_device_for_each_entity(entity, mdev) {
773 if (entity->function != function)
774 continue;
775
776 if (source)
777 ret = media_create_pad_link(source, source_pad,
778 entity, sink_pad, flags);
779 else
780 ret = media_create_pad_link(entity, source_pad,
781 sink, sink_pad, flags);
782 if (ret)
783 return ret;
784 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
785 }
786 return 0;
787}
788EXPORT_SYMBOL_GPL(media_create_pad_links);
789
57208e5e
MCC
790void __media_entity_remove_links(struct media_entity *entity)
791{
792 struct media_link *link, *tmp;
7349cec1 793
57208e5e
MCC
794 list_for_each_entry_safe(link, tmp, &entity->links, list)
795 __media_entity_remove_link(entity, link);
7349cec1
SN
796
797 entity->num_links = 0;
798 entity->num_backlinks = 0;
799}
800EXPORT_SYMBOL_GPL(__media_entity_remove_links);
801
802void media_entity_remove_links(struct media_entity *entity)
803{
cc4a8258
MCC
804 struct media_device *mdev = entity->graph_obj.mdev;
805
7349cec1 806 /* Do nothing if the entity is not registered. */
cc4a8258 807 if (mdev == NULL)
7349cec1
SN
808 return;
809
e2c91d4d 810 mutex_lock(&mdev->graph_mutex);
7349cec1 811 __media_entity_remove_links(entity);
e2c91d4d 812 mutex_unlock(&mdev->graph_mutex);
7349cec1
SN
813}
814EXPORT_SYMBOL_GPL(media_entity_remove_links);
815
97548ed4
LP
816static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
817{
97548ed4
LP
818 int ret;
819
820 /* Notify both entities. */
821 ret = media_entity_call(link->source->entity, link_setup,
822 link->source, link->sink, flags);
823 if (ret < 0 && ret != -ENOIOCTLCMD)
824 return ret;
825
826 ret = media_entity_call(link->sink->entity, link_setup,
827 link->sink, link->source, flags);
828 if (ret < 0 && ret != -ENOIOCTLCMD) {
829 media_entity_call(link->source->entity, link_setup,
830 link->source, link->sink, link->flags);
831 return ret;
832 }
833
7a6f0b22 834 link->flags = flags;
97548ed4
LP
835 link->reverse->flags = link->flags;
836
837 return 0;
838}
839
97548ed4
LP
840int __media_entity_setup_link(struct media_link *link, u32 flags)
841{
7a6f0b22 842 const u32 mask = MEDIA_LNK_FL_ENABLED;
97548ed4
LP
843 struct media_device *mdev;
844 struct media_entity *source, *sink;
845 int ret = -EBUSY;
846
847 if (link == NULL)
848 return -EINVAL;
849
7a6f0b22
LP
850 /* The non-modifiable link flags must not be modified. */
851 if ((link->flags & ~mask) != (flags & ~mask))
852 return -EINVAL;
853
97548ed4
LP
854 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
855 return link->flags == flags ? 0 : -EINVAL;
856
857 if (link->flags == flags)
858 return 0;
859
860 source = link->source->entity;
861 sink = link->sink->entity;
862
e02188c9
LP
863 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
864 (source->stream_count || sink->stream_count))
865 return -EBUSY;
866
d10c9894 867 mdev = source->graph_obj.mdev;
97548ed4 868
68429f50
LP
869 if (mdev->ops && mdev->ops->link_notify) {
870 ret = mdev->ops->link_notify(link, flags,
871 MEDIA_DEV_NOTIFY_PRE_LINK_CH);
97548ed4
LP
872 if (ret < 0)
873 return ret;
874 }
875
876 ret = __media_entity_setup_link_notify(link, flags);
97548ed4 877
68429f50
LP
878 if (mdev->ops && mdev->ops->link_notify)
879 mdev->ops->link_notify(link, flags,
880 MEDIA_DEV_NOTIFY_POST_LINK_CH);
97548ed4
LP
881
882 return ret;
883}
efc70278 884EXPORT_SYMBOL_GPL(__media_entity_setup_link);
97548ed4
LP
885
886int media_entity_setup_link(struct media_link *link, u32 flags)
887{
888 int ret;
889
5c883edb 890 mutex_lock(&link->graph_obj.mdev->graph_mutex);
97548ed4 891 ret = __media_entity_setup_link(link, flags);
5c883edb 892 mutex_unlock(&link->graph_obj.mdev->graph_mutex);
97548ed4
LP
893
894 return ret;
895}
896EXPORT_SYMBOL_GPL(media_entity_setup_link);
897
97548ed4
LP
898struct media_link *
899media_entity_find_link(struct media_pad *source, struct media_pad *sink)
900{
901 struct media_link *link;
97548ed4 902
57208e5e 903 list_for_each_entry(link, &source->entity->links, list) {
97548ed4
LP
904 if (link->source->entity == source->entity &&
905 link->source->index == source->index &&
906 link->sink->entity == sink->entity &&
907 link->sink->index == sink->index)
908 return link;
909 }
910
911 return NULL;
912}
913EXPORT_SYMBOL_GPL(media_entity_find_link);
914
1bddf1b3 915struct media_pad *media_entity_remote_pad(struct media_pad *pad)
97548ed4 916{
57208e5e 917 struct media_link *link;
97548ed4 918
57208e5e 919 list_for_each_entry(link, &pad->entity->links, list) {
97548ed4
LP
920 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
921 continue;
922
923 if (link->source == pad)
924 return link->sink;
925
926 if (link->sink == pad)
927 return link->source;
928 }
929
930 return NULL;
931
932}
1bddf1b3 933EXPORT_SYMBOL_GPL(media_entity_remote_pad);
27e543fa 934
1283f849
MCC
935static void media_interface_init(struct media_device *mdev,
936 struct media_interface *intf,
937 u32 gobj_type,
938 u32 intf_type, u32 flags)
939{
940 intf->type = intf_type;
941 intf->flags = flags;
942 INIT_LIST_HEAD(&intf->links);
943
c350ef83 944 media_gobj_create(mdev, gobj_type, &intf->graph_obj);
1283f849
MCC
945}
946
27e543fa
MCC
947/* Functions related to the media interface via device nodes */
948
949struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
950 u32 type, u32 flags,
0b3b72df 951 u32 major, u32 minor)
27e543fa
MCC
952{
953 struct media_intf_devnode *devnode;
27e543fa 954
0b3b72df 955 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
27e543fa
MCC
956 if (!devnode)
957 return NULL;
958
27e543fa
MCC
959 devnode->major = major;
960 devnode->minor = minor;
961
1283f849
MCC
962 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
963 type, flags);
27e543fa
MCC
964
965 return devnode;
966}
967EXPORT_SYMBOL_GPL(media_devnode_create);
968
969void media_devnode_remove(struct media_intf_devnode *devnode)
970{
7c4696a9 971 media_remove_intf_links(&devnode->intf);
c350ef83 972 media_gobj_destroy(&devnode->intf.graph_obj);
27e543fa
MCC
973 kfree(devnode);
974}
975EXPORT_SYMBOL_GPL(media_devnode_remove);
86e26620
MCC
976
977struct media_link *media_create_intf_link(struct media_entity *entity,
978 struct media_interface *intf,
979 u32 flags)
980{
981 struct media_link *link;
982
983 link = media_add_link(&intf->links);
984 if (link == NULL)
985 return NULL;
986
987 link->intf = intf;
988 link->entity = entity;
82ae2a50 989 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
86e26620
MCC
990
991 /* Initialize graph object embedded at the new link */
c350ef83 992 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
86e26620
MCC
993 &link->graph_obj);
994
995 return link;
996}
997EXPORT_SYMBOL_GPL(media_create_intf_link);
998
d47109fa 999void __media_remove_intf_link(struct media_link *link)
86e26620 1000{
d47109fa 1001 list_del(&link->list);
c350ef83 1002 media_gobj_destroy(&link->graph_obj);
86e26620
MCC
1003 kfree(link);
1004}
d47109fa 1005EXPORT_SYMBOL_GPL(__media_remove_intf_link);
86e26620
MCC
1006
1007void media_remove_intf_link(struct media_link *link)
1008{
cc4a8258
MCC
1009 struct media_device *mdev = link->graph_obj.mdev;
1010
1011 /* Do nothing if the intf is not registered. */
1012 if (mdev == NULL)
1013 return;
1014
e2c91d4d 1015 mutex_lock(&mdev->graph_mutex);
86e26620 1016 __media_remove_intf_link(link);
e2c91d4d 1017 mutex_unlock(&mdev->graph_mutex);
86e26620
MCC
1018}
1019EXPORT_SYMBOL_GPL(media_remove_intf_link);
7c4696a9
MCC
1020
1021void __media_remove_intf_links(struct media_interface *intf)
1022{
1023 struct media_link *link, *tmp;
1024
1025 list_for_each_entry_safe(link, tmp, &intf->links, list)
1026 __media_remove_intf_link(link);
1027
1028}
1029EXPORT_SYMBOL_GPL(__media_remove_intf_links);
1030
1031void media_remove_intf_links(struct media_interface *intf)
1032{
cc4a8258
MCC
1033 struct media_device *mdev = intf->graph_obj.mdev;
1034
7c4696a9 1035 /* Do nothing if the intf is not registered. */
cc4a8258 1036 if (mdev == NULL)
7c4696a9
MCC
1037 return;
1038
e2c91d4d 1039 mutex_lock(&mdev->graph_mutex);
7c4696a9 1040 __media_remove_intf_links(intf);
e2c91d4d 1041 mutex_unlock(&mdev->graph_mutex);
7c4696a9
MCC
1042}
1043EXPORT_SYMBOL_GPL(media_remove_intf_links);