rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/bitmap.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/property.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <media/media-entity.h> |
| 24 | #include <media/media-device.h> |
| 25 | |
| 26 | static 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"; |
| 35 | case MEDIA_GRAPH_INTF_DEVNODE: |
| 36 | return "intf-devnode"; |
| 37 | default: |
| 38 | return "unknown"; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static inline const char *intf_type(struct media_interface *intf) |
| 43 | { |
| 44 | switch (intf->type) { |
| 45 | case MEDIA_INTF_T_DVB_FE: |
| 46 | return "dvb-frontend"; |
| 47 | case MEDIA_INTF_T_DVB_DEMUX: |
| 48 | return "dvb-demux"; |
| 49 | case MEDIA_INTF_T_DVB_DVR: |
| 50 | return "dvb-dvr"; |
| 51 | case MEDIA_INTF_T_DVB_CA: |
| 52 | return "dvb-ca"; |
| 53 | case MEDIA_INTF_T_DVB_NET: |
| 54 | return "dvb-net"; |
| 55 | case MEDIA_INTF_T_V4L_VIDEO: |
| 56 | return "v4l-video"; |
| 57 | case MEDIA_INTF_T_V4L_VBI: |
| 58 | return "v4l-vbi"; |
| 59 | case MEDIA_INTF_T_V4L_RADIO: |
| 60 | return "v4l-radio"; |
| 61 | case MEDIA_INTF_T_V4L_SUBDEV: |
| 62 | return "v4l-subdev"; |
| 63 | case MEDIA_INTF_T_V4L_SWRADIO: |
| 64 | return "v4l-swradio"; |
| 65 | case MEDIA_INTF_T_V4L_TOUCH: |
| 66 | return "v4l-touch"; |
| 67 | case MEDIA_INTF_T_ALSA_PCM_CAPTURE: |
| 68 | return "alsa-pcm-capture"; |
| 69 | case MEDIA_INTF_T_ALSA_PCM_PLAYBACK: |
| 70 | return "alsa-pcm-playback"; |
| 71 | case MEDIA_INTF_T_ALSA_CONTROL: |
| 72 | return "alsa-control"; |
| 73 | case MEDIA_INTF_T_ALSA_COMPRESS: |
| 74 | return "alsa-compress"; |
| 75 | case MEDIA_INTF_T_ALSA_RAWMIDI: |
| 76 | return "alsa-rawmidi"; |
| 77 | case MEDIA_INTF_T_ALSA_HWDEP: |
| 78 | return "alsa-hwdep"; |
| 79 | case MEDIA_INTF_T_ALSA_SEQUENCER: |
| 80 | return "alsa-sequencer"; |
| 81 | case MEDIA_INTF_T_ALSA_TIMER: |
| 82 | return "alsa-timer"; |
| 83 | default: |
| 84 | return "unknown-intf"; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum, |
| 89 | int idx_max) |
| 90 | { |
| 91 | idx_max = ALIGN(idx_max, BITS_PER_LONG); |
| 92 | ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long), |
| 93 | GFP_KERNEL); |
| 94 | if (!ent_enum->bmap) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | bitmap_zero(ent_enum->bmap, idx_max); |
| 98 | ent_enum->idx_max = idx_max; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(__media_entity_enum_init); |
| 103 | |
| 104 | void media_entity_enum_cleanup(struct media_entity_enum *ent_enum) |
| 105 | { |
| 106 | kfree(ent_enum->bmap); |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(media_entity_enum_cleanup); |
| 109 | |
| 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 | */ |
| 119 | static 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, |
| 125 | "%s id %u: entity '%s'\n", |
| 126 | event_name, media_id(gobj), |
| 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, |
| 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)); |
| 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, |
| 147 | "%s id %u: %s%spad '%s':%d\n", |
| 148 | event_name, media_id(gobj), |
| 149 | pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "", |
| 150 | pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "", |
| 151 | pad->entity->name, pad->index); |
| 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, |
| 160 | "%s id %u: intf_devnode %s - major: %d, minor: %d\n", |
| 161 | event_name, media_id(gobj), |
| 162 | intf_type(intf), |
| 163 | devnode->major, devnode->minor); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | void media_gobj_create(struct media_device *mdev, |
| 171 | enum media_gobj_type type, |
| 172 | struct media_gobj *gobj) |
| 173 | { |
| 174 | BUG_ON(!mdev); |
| 175 | |
| 176 | gobj->mdev = mdev; |
| 177 | |
| 178 | /* Create a per-type unique object ID */ |
| 179 | gobj->id = media_gobj_gen_id(type, ++mdev->id); |
| 180 | |
| 181 | switch (type) { |
| 182 | case MEDIA_GRAPH_ENTITY: |
| 183 | list_add_tail(&gobj->list, &mdev->entities); |
| 184 | break; |
| 185 | case MEDIA_GRAPH_PAD: |
| 186 | list_add_tail(&gobj->list, &mdev->pads); |
| 187 | break; |
| 188 | case MEDIA_GRAPH_LINK: |
| 189 | list_add_tail(&gobj->list, &mdev->links); |
| 190 | break; |
| 191 | case MEDIA_GRAPH_INTF_DEVNODE: |
| 192 | list_add_tail(&gobj->list, &mdev->interfaces); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | mdev->topology_version++; |
| 197 | |
| 198 | dev_dbg_obj(__func__, gobj); |
| 199 | } |
| 200 | |
| 201 | void media_gobj_destroy(struct media_gobj *gobj) |
| 202 | { |
| 203 | /* Do nothing if the object is not linked. */ |
| 204 | if (gobj->mdev == NULL) |
| 205 | return; |
| 206 | |
| 207 | dev_dbg_obj(__func__, gobj); |
| 208 | |
| 209 | gobj->mdev->topology_version++; |
| 210 | |
| 211 | /* Remove the object from mdev list */ |
| 212 | list_del(&gobj->list); |
| 213 | |
| 214 | gobj->mdev = NULL; |
| 215 | } |
| 216 | |
| 217 | int media_entity_pads_init(struct media_entity *entity, u16 num_pads, |
| 218 | struct media_pad *pads) |
| 219 | { |
| 220 | struct media_device *mdev = entity->graph_obj.mdev; |
| 221 | unsigned int i; |
| 222 | |
| 223 | entity->num_pads = num_pads; |
| 224 | entity->pads = pads; |
| 225 | |
| 226 | if (mdev) |
| 227 | mutex_lock(&mdev->graph_mutex); |
| 228 | |
| 229 | for (i = 0; i < num_pads; i++) { |
| 230 | pads[i].entity = entity; |
| 231 | pads[i].index = i; |
| 232 | if (mdev) |
| 233 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, |
| 234 | &entity->pads[i].graph_obj); |
| 235 | } |
| 236 | |
| 237 | if (mdev) |
| 238 | mutex_unlock(&mdev->graph_mutex); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | EXPORT_SYMBOL_GPL(media_entity_pads_init); |
| 243 | |
| 244 | /* ----------------------------------------------------------------------------- |
| 245 | * Graph traversal |
| 246 | */ |
| 247 | |
| 248 | static struct media_entity * |
| 249 | media_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 */ |
| 258 | static void stack_push(struct media_graph *graph, |
| 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++; |
| 266 | graph->stack[graph->top].link = entity->links.next; |
| 267 | graph->stack[graph->top].entity = entity; |
| 268 | } |
| 269 | |
| 270 | static struct media_entity *stack_pop(struct media_graph *graph) |
| 271 | { |
| 272 | struct media_entity *entity; |
| 273 | |
| 274 | entity = graph->stack[graph->top].entity; |
| 275 | graph->top--; |
| 276 | |
| 277 | return entity; |
| 278 | } |
| 279 | |
| 280 | #define link_top(en) ((en)->stack[(en)->top].link) |
| 281 | #define stack_top(en) ((en)->stack[(en)->top].entity) |
| 282 | |
| 283 | /* |
| 284 | * TODO: Get rid of this. |
| 285 | */ |
| 286 | #define MEDIA_ENTITY_MAX_PADS 512 |
| 287 | |
| 288 | /** |
| 289 | * media_graph_walk_init - Allocate resources for graph walk |
| 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 |
| 295 | * media_graph_walk_free(). |
| 296 | * |
| 297 | * Returns error on failure, zero on success. |
| 298 | */ |
| 299 | __must_check int media_graph_walk_init( |
| 300 | struct media_graph *graph, struct media_device *mdev) |
| 301 | { |
| 302 | return media_entity_enum_init(&graph->ent_enum, mdev); |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(media_graph_walk_init); |
| 305 | |
| 306 | /** |
| 307 | * media_graph_walk_cleanup - Release resources related to graph walking |
| 308 | * @graph: Media graph structure that was used to walk the graph |
| 309 | */ |
| 310 | void media_graph_walk_cleanup(struct media_graph *graph) |
| 311 | { |
| 312 | media_entity_enum_cleanup(&graph->ent_enum); |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(media_graph_walk_cleanup); |
| 315 | |
| 316 | void media_graph_walk_start(struct media_graph *graph, |
| 317 | struct media_entity *entity) |
| 318 | { |
| 319 | media_entity_enum_zero(&graph->ent_enum); |
| 320 | media_entity_enum_set(&graph->ent_enum, entity); |
| 321 | |
| 322 | graph->top = 0; |
| 323 | graph->stack[graph->top].entity = NULL; |
| 324 | stack_push(graph, entity); |
| 325 | dev_dbg(entity->graph_obj.mdev->dev, |
| 326 | "begin graph walk at '%s'\n", entity->name); |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(media_graph_walk_start); |
| 329 | |
| 330 | static 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; |
| 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); |
| 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; |
| 354 | dev_dbg(entity->graph_obj.mdev->dev, |
| 355 | "walk: skipping entity '%s' (already seen)\n", |
| 356 | next->name); |
| 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); |
| 363 | dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n", |
| 364 | next->name); |
| 365 | } |
| 366 | |
| 367 | struct media_entity *media_graph_walk_next(struct media_graph *graph) |
| 368 | { |
| 369 | struct media_entity *entity; |
| 370 | |
| 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 | */ |
| 379 | while (link_top(graph) != &stack_top(graph)->links) |
| 380 | media_graph_walk_iter(graph); |
| 381 | |
| 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; |
| 387 | } |
| 388 | EXPORT_SYMBOL_GPL(media_graph_walk_next); |
| 389 | |
| 390 | int 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 | } |
| 423 | EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad); |
| 424 | |
| 425 | /* ----------------------------------------------------------------------------- |
| 426 | * Pipeline management |
| 427 | */ |
| 428 | |
| 429 | __must_check int __media_pipeline_start(struct media_entity *entity, |
| 430 | struct media_pipeline *pipe) |
| 431 | { |
| 432 | struct media_device *mdev = entity->graph_obj.mdev; |
| 433 | struct media_graph *graph = &pipe->graph; |
| 434 | struct media_entity *entity_err = entity; |
| 435 | struct media_link *link; |
| 436 | int ret; |
| 437 | |
| 438 | if (!pipe->streaming_count++) { |
| 439 | ret = media_graph_walk_init(&pipe->graph, mdev); |
| 440 | if (ret) |
| 441 | goto error_graph_walk_start; |
| 442 | } |
| 443 | |
| 444 | media_graph_walk_start(&pipe->graph, entity); |
| 445 | |
| 446 | while ((entity = media_graph_walk_next(graph))) { |
| 447 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
| 448 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); |
| 449 | |
| 450 | entity->stream_count++; |
| 451 | |
| 452 | if (WARN_ON(entity->pipe && entity->pipe != pipe)) { |
| 453 | ret = -EBUSY; |
| 454 | goto error; |
| 455 | } |
| 456 | |
| 457 | entity->pipe = pipe; |
| 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 | |
| 466 | bitmap_zero(active, entity->num_pads); |
| 467 | bitmap_fill(has_no_links, entity->num_pads); |
| 468 | |
| 469 | list_for_each_entry(link, &entity->links, list) { |
| 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)) |
| 491 | continue; |
| 492 | |
| 493 | ret = entity->ops->link_validate(link); |
| 494 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 495 | dev_dbg(entity->graph_obj.mdev->dev, |
| 496 | "link validation failed for '%s':%u -> '%s':%u, error %d\n", |
| 497 | link->source->entity->name, |
| 498 | link->source->index, |
| 499 | entity->name, link->sink->index, ret); |
| 500 | goto error; |
| 501 | } |
| 502 | } |
| 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)) { |
| 508 | ret = -ENOLINK; |
| 509 | dev_dbg(entity->graph_obj.mdev->dev, |
| 510 | "'%s':%u must be connected by an enabled link\n", |
| 511 | entity->name, |
| 512 | (unsigned)find_first_zero_bit( |
| 513 | active, entity->num_pads)); |
| 514 | goto error; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | |
| 520 | error: |
| 521 | /* |
| 522 | * Link validation on graph failed. We revert what we did and |
| 523 | * return the error. |
| 524 | */ |
| 525 | media_graph_walk_start(graph, entity_err); |
| 526 | |
| 527 | while ((entity_err = media_graph_walk_next(graph))) { |
| 528 | /* Sanity check for negative stream_count */ |
| 529 | if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) { |
| 530 | entity_err->stream_count--; |
| 531 | if (entity_err->stream_count == 0) |
| 532 | entity_err->pipe = NULL; |
| 533 | } |
| 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 | |
| 543 | error_graph_walk_start: |
| 544 | if (!--pipe->streaming_count) |
| 545 | media_graph_walk_cleanup(graph); |
| 546 | |
| 547 | return ret; |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(__media_pipeline_start); |
| 550 | |
| 551 | __must_check int media_pipeline_start(struct media_entity *entity, |
| 552 | struct media_pipeline *pipe) |
| 553 | { |
| 554 | struct media_device *mdev = entity->graph_obj.mdev; |
| 555 | int ret; |
| 556 | |
| 557 | mutex_lock(&mdev->graph_mutex); |
| 558 | ret = __media_pipeline_start(entity, pipe); |
| 559 | mutex_unlock(&mdev->graph_mutex); |
| 560 | return ret; |
| 561 | } |
| 562 | EXPORT_SYMBOL_GPL(media_pipeline_start); |
| 563 | |
| 564 | void __media_pipeline_stop(struct media_entity *entity) |
| 565 | { |
| 566 | struct media_graph *graph = &entity->pipe->graph; |
| 567 | struct media_pipeline *pipe = entity->pipe; |
| 568 | |
| 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; |
| 575 | |
| 576 | media_graph_walk_start(graph, entity); |
| 577 | |
| 578 | while ((entity = media_graph_walk_next(graph))) { |
| 579 | /* Sanity check for negative stream_count */ |
| 580 | if (!WARN_ON_ONCE(entity->stream_count <= 0)) { |
| 581 | entity->stream_count--; |
| 582 | if (entity->stream_count == 0) |
| 583 | entity->pipe = NULL; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (!--pipe->streaming_count) |
| 588 | media_graph_walk_cleanup(graph); |
| 589 | |
| 590 | } |
| 591 | EXPORT_SYMBOL_GPL(__media_pipeline_stop); |
| 592 | |
| 593 | void media_pipeline_stop(struct media_entity *entity) |
| 594 | { |
| 595 | struct media_device *mdev = entity->graph_obj.mdev; |
| 596 | |
| 597 | mutex_lock(&mdev->graph_mutex); |
| 598 | __media_pipeline_stop(entity); |
| 599 | mutex_unlock(&mdev->graph_mutex); |
| 600 | } |
| 601 | EXPORT_SYMBOL_GPL(media_pipeline_stop); |
| 602 | |
| 603 | /* ----------------------------------------------------------------------------- |
| 604 | * Module use count |
| 605 | */ |
| 606 | |
| 607 | struct media_entity *media_entity_get(struct media_entity *entity) |
| 608 | { |
| 609 | if (entity == NULL) |
| 610 | return NULL; |
| 611 | |
| 612 | if (entity->graph_obj.mdev->dev && |
| 613 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) |
| 614 | return NULL; |
| 615 | |
| 616 | return entity; |
| 617 | } |
| 618 | EXPORT_SYMBOL_GPL(media_entity_get); |
| 619 | |
| 620 | void media_entity_put(struct media_entity *entity) |
| 621 | { |
| 622 | if (entity == NULL) |
| 623 | return; |
| 624 | |
| 625 | if (entity->graph_obj.mdev->dev) |
| 626 | module_put(entity->graph_obj.mdev->dev->driver->owner); |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(media_entity_put); |
| 629 | |
| 630 | /* ----------------------------------------------------------------------------- |
| 631 | * Links management |
| 632 | */ |
| 633 | |
| 634 | static struct media_link *media_add_link(struct list_head *head) |
| 635 | { |
| 636 | struct media_link *link; |
| 637 | |
| 638 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 639 | if (link == NULL) |
| 640 | return NULL; |
| 641 | |
| 642 | list_add_tail(&link->list, head); |
| 643 | |
| 644 | return link; |
| 645 | } |
| 646 | |
| 647 | static void __media_entity_remove_link(struct media_entity *entity, |
| 648 | struct media_link *link) |
| 649 | { |
| 650 | struct media_link *rlink, *tmp; |
| 651 | struct media_entity *remote; |
| 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) { |
| 659 | if (rlink != link->reverse) |
| 660 | continue; |
| 661 | |
| 662 | if (link->source->entity == entity) |
| 663 | remote->num_backlinks--; |
| 664 | |
| 665 | /* Remove the remote link */ |
| 666 | list_del(&rlink->list); |
| 667 | media_gobj_destroy(&rlink->graph_obj); |
| 668 | kfree(rlink); |
| 669 | |
| 670 | if (--remote->num_links == 0) |
| 671 | break; |
| 672 | } |
| 673 | list_del(&link->list); |
| 674 | media_gobj_destroy(&link->graph_obj); |
| 675 | kfree(link); |
| 676 | } |
| 677 | |
| 678 | int |
| 679 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
| 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 | |
| 689 | link = media_add_link(&source->links); |
| 690 | if (link == NULL) |
| 691 | return -ENOMEM; |
| 692 | |
| 693 | link->source = &source->pads[source_pad]; |
| 694 | link->sink = &sink->pads[sink_pad]; |
| 695 | link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK; |
| 696 | |
| 697 | /* Initialize graph object embedded at the new link */ |
| 698 | media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 699 | &link->graph_obj); |
| 700 | |
| 701 | /* Create the backlink. Backlinks are used to help graph traversal and |
| 702 | * are not reported to userspace. |
| 703 | */ |
| 704 | backlink = media_add_link(&sink->links); |
| 705 | if (backlink == NULL) { |
| 706 | __media_entity_remove_link(source, link); |
| 707 | return -ENOMEM; |
| 708 | } |
| 709 | |
| 710 | backlink->source = &source->pads[source_pad]; |
| 711 | backlink->sink = &sink->pads[sink_pad]; |
| 712 | backlink->flags = flags; |
| 713 | backlink->is_backlink = true; |
| 714 | |
| 715 | /* Initialize graph object embedded at the new link */ |
| 716 | media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 717 | &backlink->graph_obj); |
| 718 | |
| 719 | link->reverse = backlink; |
| 720 | backlink->reverse = link; |
| 721 | |
| 722 | sink->num_backlinks++; |
| 723 | sink->num_links++; |
| 724 | source->num_links++; |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
| 729 | |
| 730 | int 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 | } |
| 793 | EXPORT_SYMBOL_GPL(media_create_pad_links); |
| 794 | |
| 795 | void __media_entity_remove_links(struct media_entity *entity) |
| 796 | { |
| 797 | struct media_link *link, *tmp; |
| 798 | |
| 799 | list_for_each_entry_safe(link, tmp, &entity->links, list) |
| 800 | __media_entity_remove_link(entity, link); |
| 801 | |
| 802 | entity->num_links = 0; |
| 803 | entity->num_backlinks = 0; |
| 804 | } |
| 805 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); |
| 806 | |
| 807 | void media_entity_remove_links(struct media_entity *entity) |
| 808 | { |
| 809 | struct media_device *mdev = entity->graph_obj.mdev; |
| 810 | |
| 811 | /* Do nothing if the entity is not registered. */ |
| 812 | if (mdev == NULL) |
| 813 | return; |
| 814 | |
| 815 | mutex_lock(&mdev->graph_mutex); |
| 816 | __media_entity_remove_links(entity); |
| 817 | mutex_unlock(&mdev->graph_mutex); |
| 818 | } |
| 819 | EXPORT_SYMBOL_GPL(media_entity_remove_links); |
| 820 | |
| 821 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
| 822 | { |
| 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 | |
| 839 | link->flags = flags; |
| 840 | link->reverse->flags = link->flags; |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
| 846 | { |
| 847 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
| 848 | struct media_device *mdev; |
| 849 | struct media_entity *source, *sink; |
| 850 | int ret = -EBUSY; |
| 851 | |
| 852 | if (link == NULL) |
| 853 | return -EINVAL; |
| 854 | |
| 855 | /* The non-modifiable link flags must not be modified. */ |
| 856 | if ((link->flags & ~mask) != (flags & ~mask)) |
| 857 | return -EINVAL; |
| 858 | |
| 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 | |
| 868 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
| 869 | (source->stream_count || sink->stream_count)) |
| 870 | return -EBUSY; |
| 871 | |
| 872 | mdev = source->graph_obj.mdev; |
| 873 | |
| 874 | if (mdev->ops && mdev->ops->link_notify) { |
| 875 | ret = mdev->ops->link_notify(link, flags, |
| 876 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); |
| 877 | if (ret < 0) |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | ret = __media_entity_setup_link_notify(link, flags); |
| 882 | |
| 883 | if (mdev->ops && mdev->ops->link_notify) |
| 884 | mdev->ops->link_notify(link, flags, |
| 885 | MEDIA_DEV_NOTIFY_POST_LINK_CH); |
| 886 | |
| 887 | return ret; |
| 888 | } |
| 889 | EXPORT_SYMBOL_GPL(__media_entity_setup_link); |
| 890 | |
| 891 | int media_entity_setup_link(struct media_link *link, u32 flags) |
| 892 | { |
| 893 | int ret; |
| 894 | |
| 895 | mutex_lock(&link->graph_obj.mdev->graph_mutex); |
| 896 | ret = __media_entity_setup_link(link, flags); |
| 897 | mutex_unlock(&link->graph_obj.mdev->graph_mutex); |
| 898 | |
| 899 | return ret; |
| 900 | } |
| 901 | EXPORT_SYMBOL_GPL(media_entity_setup_link); |
| 902 | |
| 903 | struct media_link * |
| 904 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) |
| 905 | { |
| 906 | struct media_link *link; |
| 907 | |
| 908 | list_for_each_entry(link, &source->entity->links, list) { |
| 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 | } |
| 918 | EXPORT_SYMBOL_GPL(media_entity_find_link); |
| 919 | |
| 920 | struct media_pad *media_entity_remote_pad(const struct media_pad *pad) |
| 921 | { |
| 922 | struct media_link *link; |
| 923 | |
| 924 | list_for_each_entry(link, &pad->entity->links, list) { |
| 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 | } |
| 938 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |
| 939 | |
| 940 | static 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 | |
| 949 | media_gobj_create(mdev, gobj_type, &intf->graph_obj); |
| 950 | } |
| 951 | |
| 952 | /* Functions related to the media interface via device nodes */ |
| 953 | |
| 954 | struct media_intf_devnode *media_devnode_create(struct media_device *mdev, |
| 955 | u32 type, u32 flags, |
| 956 | u32 major, u32 minor) |
| 957 | { |
| 958 | struct media_intf_devnode *devnode; |
| 959 | |
| 960 | devnode = kzalloc(sizeof(*devnode), GFP_KERNEL); |
| 961 | if (!devnode) |
| 962 | return NULL; |
| 963 | |
| 964 | devnode->major = major; |
| 965 | devnode->minor = minor; |
| 966 | |
| 967 | media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE, |
| 968 | type, flags); |
| 969 | |
| 970 | return devnode; |
| 971 | } |
| 972 | EXPORT_SYMBOL_GPL(media_devnode_create); |
| 973 | |
| 974 | void media_devnode_remove(struct media_intf_devnode *devnode) |
| 975 | { |
| 976 | media_remove_intf_links(&devnode->intf); |
| 977 | media_gobj_destroy(&devnode->intf.graph_obj); |
| 978 | kfree(devnode); |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(media_devnode_remove); |
| 981 | |
| 982 | struct 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; |
| 994 | link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK; |
| 995 | |
| 996 | /* Initialize graph object embedded at the new link */ |
| 997 | media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 998 | &link->graph_obj); |
| 999 | |
| 1000 | return link; |
| 1001 | } |
| 1002 | EXPORT_SYMBOL_GPL(media_create_intf_link); |
| 1003 | |
| 1004 | void __media_remove_intf_link(struct media_link *link) |
| 1005 | { |
| 1006 | list_del(&link->list); |
| 1007 | media_gobj_destroy(&link->graph_obj); |
| 1008 | kfree(link); |
| 1009 | } |
| 1010 | EXPORT_SYMBOL_GPL(__media_remove_intf_link); |
| 1011 | |
| 1012 | void media_remove_intf_link(struct media_link *link) |
| 1013 | { |
| 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 | |
| 1020 | mutex_lock(&mdev->graph_mutex); |
| 1021 | __media_remove_intf_link(link); |
| 1022 | mutex_unlock(&mdev->graph_mutex); |
| 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(media_remove_intf_link); |
| 1025 | |
| 1026 | void __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 | } |
| 1034 | EXPORT_SYMBOL_GPL(__media_remove_intf_links); |
| 1035 | |
| 1036 | void media_remove_intf_links(struct media_interface *intf) |
| 1037 | { |
| 1038 | struct media_device *mdev = intf->graph_obj.mdev; |
| 1039 | |
| 1040 | /* Do nothing if the intf is not registered. */ |
| 1041 | if (mdev == NULL) |
| 1042 | return; |
| 1043 | |
| 1044 | mutex_lock(&mdev->graph_mutex); |
| 1045 | __media_remove_intf_links(intf); |
| 1046 | mutex_unlock(&mdev->graph_mutex); |
| 1047 | } |
| 1048 | EXPORT_SYMBOL_GPL(media_remove_intf_links); |