1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Media entity 4 * 5 * Copyright (C) 2010 Nokia Corporation 6 * 7 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 8 * Sakari Ailus <sakari.ailus@iki.fi> 9 */ 10 11 #include <linux/bitmap.h> 12 #include <linux/list.h> 13 #include <linux/property.h> 14 #include <linux/slab.h> 15 #include <media/media-entity.h> 16 #include <media/media-device.h> 17 18 static inline const char *intf_type(struct media_interface *intf) 19 { 20 switch (intf->type) { 21 case MEDIA_INTF_T_DVB_FE: 22 return "dvb-frontend"; 23 case MEDIA_INTF_T_DVB_DEMUX: 24 return "dvb-demux"; 25 case MEDIA_INTF_T_DVB_DVR: 26 return "dvb-dvr"; 27 case MEDIA_INTF_T_DVB_CA: 28 return "dvb-ca"; 29 case MEDIA_INTF_T_DVB_NET: 30 return "dvb-net"; 31 case MEDIA_INTF_T_V4L_VIDEO: 32 return "v4l-video"; 33 case MEDIA_INTF_T_V4L_VBI: 34 return "v4l-vbi"; 35 case MEDIA_INTF_T_V4L_RADIO: 36 return "v4l-radio"; 37 case MEDIA_INTF_T_V4L_SUBDEV: 38 return "v4l-subdev"; 39 case MEDIA_INTF_T_V4L_SWRADIO: 40 return "v4l-swradio"; 41 case MEDIA_INTF_T_V4L_TOUCH: 42 return "v4l-touch"; 43 default: 44 return "unknown-intf"; 45 } 46 }; 47 48 static inline const char *link_type_name(struct media_link *link) 49 { 50 switch (link->flags & MEDIA_LNK_FL_LINK_TYPE) { 51 case MEDIA_LNK_FL_DATA_LINK: 52 return "data"; 53 case MEDIA_LNK_FL_INTERFACE_LINK: 54 return "interface"; 55 case MEDIA_LNK_FL_ANCILLARY_LINK: 56 return "ancillary"; 57 default: 58 return "unknown"; 59 } 60 } 61 62 __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum, 63 struct media_device *mdev) 64 { 65 int idx_max; 66 67 idx_max = ALIGN(mdev->entity_internal_idx_max + 1, BITS_PER_LONG); 68 ent_enum->bmap = bitmap_zalloc(idx_max, GFP_KERNEL); 69 if (!ent_enum->bmap) 70 return -ENOMEM; 71 72 ent_enum->idx_max = idx_max; 73 74 return 0; 75 } 76 EXPORT_SYMBOL_GPL(media_entity_enum_init); 77 78 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum) 79 { 80 bitmap_free(ent_enum->bmap); 81 } 82 EXPORT_SYMBOL_GPL(media_entity_enum_cleanup); 83 84 /** 85 * dev_dbg_obj - Prints in debug mode a change on some object 86 * 87 * @event_name: Name of the event to report. Could be __func__ 88 * @gobj: Pointer to the object 89 * 90 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it 91 * won't produce any code. 92 */ 93 static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) 94 { 95 #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG) 96 switch (media_type(gobj)) { 97 case MEDIA_GRAPH_ENTITY: 98 dev_dbg(gobj->mdev->dev, 99 "%s id %u: entity '%s'\n", 100 event_name, media_id(gobj), 101 gobj_to_entity(gobj)->name); 102 break; 103 case MEDIA_GRAPH_LINK: 104 { 105 struct media_link *link = gobj_to_link(gobj); 106 107 dev_dbg(gobj->mdev->dev, 108 "%s id %u: %s link id %u ==> id %u\n", 109 event_name, media_id(gobj), link_type_name(link), 110 media_id(link->gobj0), 111 media_id(link->gobj1)); 112 break; 113 } 114 case MEDIA_GRAPH_PAD: 115 { 116 struct media_pad *pad = gobj_to_pad(gobj); 117 118 dev_dbg(gobj->mdev->dev, 119 "%s id %u: %s%spad '%s':%d\n", 120 event_name, media_id(gobj), 121 pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "", 122 pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "", 123 pad->entity->name, pad->index); 124 break; 125 } 126 case MEDIA_GRAPH_INTF_DEVNODE: 127 { 128 struct media_interface *intf = gobj_to_intf(gobj); 129 struct media_intf_devnode *devnode = intf_to_devnode(intf); 130 131 dev_dbg(gobj->mdev->dev, 132 "%s id %u: intf_devnode %s - major: %d, minor: %d\n", 133 event_name, media_id(gobj), 134 intf_type(intf), 135 devnode->major, devnode->minor); 136 break; 137 } 138 } 139 #endif 140 } 141 142 void media_gobj_create(struct media_device *mdev, 143 enum media_gobj_type type, 144 struct media_gobj *gobj) 145 { 146 BUG_ON(!mdev); 147 148 gobj->mdev = mdev; 149 150 /* Create a per-type unique object ID */ 151 gobj->id = media_gobj_gen_id(type, ++mdev->id); 152 153 switch (type) { 154 case MEDIA_GRAPH_ENTITY: 155 list_add_tail(&gobj->list, &mdev->entities); 156 break; 157 case MEDIA_GRAPH_PAD: 158 list_add_tail(&gobj->list, &mdev->pads); 159 break; 160 case MEDIA_GRAPH_LINK: 161 list_add_tail(&gobj->list, &mdev->links); 162 break; 163 case MEDIA_GRAPH_INTF_DEVNODE: 164 list_add_tail(&gobj->list, &mdev->interfaces); 165 break; 166 } 167 168 mdev->topology_version++; 169 170 dev_dbg_obj(__func__, gobj); 171 } 172 173 void media_gobj_destroy(struct media_gobj *gobj) 174 { 175 /* Do nothing if the object is not linked. */ 176 if (gobj->mdev == NULL) 177 return; 178 179 dev_dbg_obj(__func__, gobj); 180 181 gobj->mdev->topology_version++; 182 183 /* Remove the object from mdev list */ 184 list_del(&gobj->list); 185 186 gobj->mdev = NULL; 187 } 188 189 /* 190 * TODO: Get rid of this. 191 */ 192 #define MEDIA_ENTITY_MAX_PADS 512 193 194 int media_entity_pads_init(struct media_entity *entity, u16 num_pads, 195 struct media_pad *pads) 196 { 197 struct media_device *mdev = entity->graph_obj.mdev; 198 struct media_pad *iter; 199 unsigned int i = 0; 200 int ret = 0; 201 202 if (num_pads >= MEDIA_ENTITY_MAX_PADS) 203 return -E2BIG; 204 205 entity->num_pads = num_pads; 206 entity->pads = pads; 207 208 if (mdev) 209 mutex_lock(&mdev->graph_mutex); 210 211 media_entity_for_each_pad(entity, iter) { 212 const u32 pad_flags = iter->flags & (MEDIA_PAD_FL_SINK | 213 MEDIA_PAD_FL_SOURCE | 214 MEDIA_PAD_FL_INTERNAL); 215 216 iter->entity = entity; 217 iter->index = i++; 218 219 if (pad_flags != MEDIA_PAD_FL_SINK && 220 pad_flags != (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_INTERNAL) && 221 pad_flags != MEDIA_PAD_FL_SOURCE) { 222 ret = -EINVAL; 223 break; 224 } 225 226 if (mdev) 227 media_gobj_create(mdev, MEDIA_GRAPH_PAD, 228 &iter->graph_obj); 229 } 230 231 if (ret && mdev) { 232 media_entity_for_each_pad(entity, iter) 233 media_gobj_destroy(&iter->graph_obj); 234 } 235 236 if (mdev) 237 mutex_unlock(&mdev->graph_mutex); 238 239 return ret; 240 } 241 EXPORT_SYMBOL_GPL(media_entity_pads_init); 242 243 /* ----------------------------------------------------------------------------- 244 * Graph traversal 245 */ 246 247 /** 248 * media_entity_has_pad_interdep - Check interdependency between two pads 249 * 250 * @entity: The entity 251 * @pad0: The first pad index 252 * @pad1: The second pad index 253 * 254 * This function checks the interdependency inside the entity between @pad0 255 * and @pad1. If two pads are interdependent they are part of the same pipeline 256 * and enabling one of the pads means that the other pad will become "locked" 257 * and doesn't allow configuration changes. 258 * 259 * This function uses the &media_entity_operations.has_pad_interdep() operation 260 * to check the dependency inside the entity between @pad0 and @pad1. If the 261 * has_pad_interdep operation is not implemented, all pads of the entity are 262 * considered to be interdependent. 263 * 264 * One of @pad0 and @pad1 must be a sink pad and the other one a source pad. 265 * The function returns false if both pads are sinks or sources. 266 * 267 * The caller must hold entity->graph_obj.mdev->mutex. 268 * 269 * Return: true if the pads are connected internally and false otherwise. 270 */ 271 static bool media_entity_has_pad_interdep(struct media_entity *entity, 272 unsigned int pad0, unsigned int pad1) 273 { 274 if (pad0 >= entity->num_pads || pad1 >= entity->num_pads) 275 return false; 276 277 if (entity->pads[pad0].flags & entity->pads[pad1].flags & 278 (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) 279 return false; 280 281 if (!entity->ops || !entity->ops->has_pad_interdep) 282 return true; 283 284 return entity->ops->has_pad_interdep(entity, pad0, pad1); 285 } 286 287 static struct media_entity * 288 media_entity_other(struct media_entity *entity, struct media_link *link) 289 { 290 if (link->source->entity == entity) 291 return link->sink->entity; 292 else 293 return link->source->entity; 294 } 295 296 /* push an entity to traversal stack */ 297 static void stack_push(struct media_graph *graph, 298 struct media_entity *entity) 299 { 300 if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { 301 WARN_ON(1); 302 return; 303 } 304 graph->top++; 305 graph->stack[graph->top].link = entity->links.next; 306 graph->stack[graph->top].entity = entity; 307 } 308 309 static struct media_entity *stack_pop(struct media_graph *graph) 310 { 311 struct media_entity *entity; 312 313 entity = graph->stack[graph->top].entity; 314 graph->top--; 315 316 return entity; 317 } 318 319 #define link_top(en) ((en)->stack[(en)->top].link) 320 #define stack_top(en) ((en)->stack[(en)->top].entity) 321 322 /** 323 * media_graph_walk_init - Allocate resources for graph walk 324 * @graph: Media graph structure that will be used to walk the graph 325 * @mdev: Media device 326 * 327 * Reserve resources for graph walk in media device's current 328 * state. The memory must be released using 329 * media_graph_walk_cleanup(). 330 * 331 * Returns error on failure, zero on success. 332 */ 333 __must_check int media_graph_walk_init( 334 struct media_graph *graph, struct media_device *mdev) 335 { 336 return media_entity_enum_init(&graph->ent_enum, mdev); 337 } 338 EXPORT_SYMBOL_GPL(media_graph_walk_init); 339 340 /** 341 * media_graph_walk_cleanup - Release resources related to graph walking 342 * @graph: Media graph structure that was used to walk the graph 343 */ 344 void media_graph_walk_cleanup(struct media_graph *graph) 345 { 346 media_entity_enum_cleanup(&graph->ent_enum); 347 } 348 EXPORT_SYMBOL_GPL(media_graph_walk_cleanup); 349 350 void media_graph_walk_start(struct media_graph *graph, 351 struct media_entity *entity) 352 { 353 media_entity_enum_zero(&graph->ent_enum); 354 media_entity_enum_set(&graph->ent_enum, entity); 355 356 graph->top = 0; 357 graph->stack[graph->top].entity = NULL; 358 stack_push(graph, entity); 359 dev_dbg(entity->graph_obj.mdev->dev, 360 "begin graph walk at '%s'\n", entity->name); 361 } 362 EXPORT_SYMBOL_GPL(media_graph_walk_start); 363 364 static void media_graph_walk_iter(struct media_graph *graph) 365 { 366 struct media_entity *entity = stack_top(graph); 367 struct media_link *link; 368 struct media_entity *next; 369 370 link = list_entry(link_top(graph), typeof(*link), list); 371 372 /* If the link is not a data link, don't follow it */ 373 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) { 374 link_top(graph) = link_top(graph)->next; 375 return; 376 } 377 378 /* The link is not enabled so we do not follow. */ 379 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { 380 link_top(graph) = link_top(graph)->next; 381 dev_dbg(entity->graph_obj.mdev->dev, 382 "walk: skipping disabled link '%s':%u -> '%s':%u\n", 383 link->source->entity->name, link->source->index, 384 link->sink->entity->name, link->sink->index); 385 return; 386 } 387 388 /* Get the entity at the other end of the link. */ 389 next = media_entity_other(entity, link); 390 391 /* Has the entity already been visited? */ 392 if (media_entity_enum_test_and_set(&graph->ent_enum, next)) { 393 link_top(graph) = link_top(graph)->next; 394 dev_dbg(entity->graph_obj.mdev->dev, 395 "walk: skipping entity '%s' (already seen)\n", 396 next->name); 397 return; 398 } 399 400 /* Push the new entity to stack and start over. */ 401 link_top(graph) = link_top(graph)->next; 402 stack_push(graph, next); 403 dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n", 404 next->name); 405 lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex); 406 } 407 408 struct media_entity *media_graph_walk_next(struct media_graph *graph) 409 { 410 struct media_entity *entity; 411 412 if (stack_top(graph) == NULL) 413 return NULL; 414 415 /* 416 * Depth first search. Push entity to stack and continue from 417 * top of the stack until no more entities on the level can be 418 * found. 419 */ 420 while (link_top(graph) != &stack_top(graph)->links) 421 media_graph_walk_iter(graph); 422 423 entity = stack_pop(graph); 424 dev_dbg(entity->graph_obj.mdev->dev, 425 "walk: returning entity '%s'\n", entity->name); 426 427 return entity; 428 } 429 EXPORT_SYMBOL_GPL(media_graph_walk_next); 430 431 /* ----------------------------------------------------------------------------- 432 * Pipeline management 433 */ 434 435 /* 436 * The pipeline traversal stack stores pads that are reached during graph 437 * traversal, with a list of links to be visited to continue the traversal. 438 * When a new pad is reached, an entry is pushed on the top of the stack and 439 * points to the incoming pad and the first link of the entity. 440 * 441 * To find further pads in the pipeline, the traversal algorithm follows 442 * internal pad dependencies in the entity, and then links in the graph. It 443 * does so by iterating over all links of the entity, and following enabled 444 * links that originate from a pad that is internally connected to the incoming 445 * pad, as reported by the media_entity_has_pad_interdep() function. 446 */ 447 448 /** 449 * struct media_pipeline_walk_entry - Entry in the pipeline traversal stack 450 * 451 * @pad: The media pad being visited 452 * @links: Links left to be visited 453 */ 454 struct media_pipeline_walk_entry { 455 struct media_pad *pad; 456 struct list_head *links; 457 }; 458 459 /** 460 * struct media_pipeline_walk - State used by the media pipeline traversal 461 * algorithm 462 * 463 * @mdev: The media device 464 * @stack: Depth-first search stack 465 * @stack.size: Number of allocated entries in @stack.entries 466 * @stack.top: Index of the top stack entry (-1 if the stack is empty) 467 * @stack.entries: Stack entries 468 */ 469 struct media_pipeline_walk { 470 struct media_device *mdev; 471 472 struct { 473 unsigned int size; 474 int top; 475 struct media_pipeline_walk_entry *entries; 476 } stack; 477 }; 478 479 #define MEDIA_PIPELINE_STACK_GROW_STEP 16 480 481 static struct media_pipeline_walk_entry * 482 media_pipeline_walk_top(struct media_pipeline_walk *walk) 483 { 484 return &walk->stack.entries[walk->stack.top]; 485 } 486 487 static bool media_pipeline_walk_empty(struct media_pipeline_walk *walk) 488 { 489 return walk->stack.top == -1; 490 } 491 492 /* Increase the stack size by MEDIA_PIPELINE_STACK_GROW_STEP elements. */ 493 static int media_pipeline_walk_resize(struct media_pipeline_walk *walk) 494 { 495 struct media_pipeline_walk_entry *entries; 496 unsigned int new_size; 497 498 /* Safety check, to avoid stack overflows in case of bugs. */ 499 if (walk->stack.size >= 256) 500 return -E2BIG; 501 502 new_size = walk->stack.size + MEDIA_PIPELINE_STACK_GROW_STEP; 503 504 entries = krealloc(walk->stack.entries, 505 new_size * sizeof(*walk->stack.entries), 506 GFP_KERNEL); 507 if (!entries) 508 return -ENOMEM; 509 510 walk->stack.entries = entries; 511 walk->stack.size = new_size; 512 513 return 0; 514 } 515 516 /* Push a new entry on the stack. */ 517 static int media_pipeline_walk_push(struct media_pipeline_walk *walk, 518 struct media_pad *pad) 519 { 520 struct media_pipeline_walk_entry *entry; 521 int ret; 522 523 if (walk->stack.top + 1 >= walk->stack.size) { 524 ret = media_pipeline_walk_resize(walk); 525 if (ret) 526 return ret; 527 } 528 529 walk->stack.top++; 530 entry = media_pipeline_walk_top(walk); 531 entry->pad = pad; 532 entry->links = pad->entity->links.next; 533 534 dev_dbg(walk->mdev->dev, 535 "media pipeline: pushed entry %u: '%s':%u\n", 536 walk->stack.top, pad->entity->name, pad->index); 537 538 return 0; 539 } 540 541 /* 542 * Move the top entry link cursor to the next link. If all links of the entry 543 * have been visited, pop the entry itself. Return true if the entry has been 544 * popped. 545 */ 546 static bool media_pipeline_walk_pop(struct media_pipeline_walk *walk) 547 { 548 struct media_pipeline_walk_entry *entry; 549 550 if (WARN_ON(walk->stack.top < 0)) 551 return false; 552 553 entry = media_pipeline_walk_top(walk); 554 555 if (entry->links->next == &entry->pad->entity->links) { 556 dev_dbg(walk->mdev->dev, 557 "media pipeline: entry %u has no more links, popping\n", 558 walk->stack.top); 559 560 walk->stack.top--; 561 return true; 562 } 563 564 entry->links = entry->links->next; 565 566 dev_dbg(walk->mdev->dev, 567 "media pipeline: moved entry %u to next link\n", 568 walk->stack.top); 569 570 return false; 571 } 572 573 /* Free all memory allocated while walking the pipeline. */ 574 static void media_pipeline_walk_destroy(struct media_pipeline_walk *walk) 575 { 576 kfree(walk->stack.entries); 577 } 578 579 /* Add a pad to the pipeline and push it to the stack. */ 580 static int media_pipeline_add_pad(struct media_pipeline *pipe, 581 struct media_pipeline_walk *walk, 582 struct media_pad *pad) 583 { 584 struct media_pipeline_pad *ppad; 585 586 list_for_each_entry(ppad, &pipe->pads, list) { 587 if (ppad->pad == pad) { 588 dev_dbg(pad->graph_obj.mdev->dev, 589 "media pipeline: already contains pad '%s':%u\n", 590 pad->entity->name, pad->index); 591 return 0; 592 } 593 } 594 595 ppad = kzalloc_obj(*ppad); 596 if (!ppad) 597 return -ENOMEM; 598 599 ppad->pipe = pipe; 600 ppad->pad = pad; 601 602 list_add_tail(&ppad->list, &pipe->pads); 603 604 dev_dbg(pad->graph_obj.mdev->dev, 605 "media pipeline: added pad '%s':%u\n", 606 pad->entity->name, pad->index); 607 608 return media_pipeline_walk_push(walk, pad); 609 } 610 611 /* Explore the next link of the entity at the top of the stack. */ 612 static int media_pipeline_explore_next_link(struct media_pipeline *pipe, 613 struct media_pipeline_walk *walk) 614 { 615 struct media_pipeline_walk_entry *entry = media_pipeline_walk_top(walk); 616 struct media_pad *origin; 617 struct media_link *link; 618 struct media_pad *local; 619 struct media_pad *remote; 620 bool last_link; 621 int ret; 622 623 origin = entry->pad; 624 link = list_entry(entry->links, typeof(*link), list); 625 last_link = media_pipeline_walk_pop(walk); 626 627 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) { 628 dev_dbg(walk->mdev->dev, 629 "media pipeline: skipping link (not data-link)\n"); 630 return 0; 631 } 632 633 dev_dbg(walk->mdev->dev, 634 "media pipeline: exploring link '%s':%u -> '%s':%u\n", 635 link->source->entity->name, link->source->index, 636 link->sink->entity->name, link->sink->index); 637 638 /* Get the local pad and remote pad. */ 639 if (link->source->entity == origin->entity) { 640 local = link->source; 641 remote = link->sink; 642 } else { 643 local = link->sink; 644 remote = link->source; 645 } 646 647 /* 648 * Skip links that originate from a different pad than the incoming pad 649 * that is not connected internally in the entity to the incoming pad. 650 */ 651 if (origin != local && 652 !media_entity_has_pad_interdep(origin->entity, origin->index, 653 local->index)) { 654 dev_dbg(walk->mdev->dev, 655 "media pipeline: skipping link (no route)\n"); 656 goto done; 657 } 658 659 /* 660 * Add the local pad of the link to the pipeline and push it to the 661 * stack, if not already present. 662 */ 663 ret = media_pipeline_add_pad(pipe, walk, local); 664 if (ret) 665 return ret; 666 667 /* Similarly, add the remote pad, but only if the link is enabled. */ 668 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { 669 dev_dbg(walk->mdev->dev, 670 "media pipeline: skipping link (disabled)\n"); 671 goto done; 672 } 673 674 ret = media_pipeline_add_pad(pipe, walk, remote); 675 if (ret) 676 return ret; 677 678 done: 679 /* 680 * If we're done iterating over links, iterate over pads of the entity. 681 * This is necessary to discover pads that are not connected with any 682 * link. Those are dead ends from a pipeline exploration point of view, 683 * but are still part of the pipeline and need to be added to enable 684 * proper validation. 685 */ 686 if (!last_link) 687 return 0; 688 689 dev_dbg(walk->mdev->dev, 690 "media pipeline: adding unconnected pads of '%s' reachable from pad %u\n", 691 origin->entity->name, origin->index); 692 693 media_entity_for_each_pad(origin->entity, local) { 694 /* 695 * Skip the origin pad (already handled), pad that have links 696 * (already discovered through iterating over links) and pads 697 * not internally connected. 698 */ 699 if (origin == local || local->num_links || 700 !media_entity_has_pad_interdep(origin->entity, origin->index, 701 local->index)) 702 continue; 703 704 ret = media_pipeline_add_pad(pipe, walk, local); 705 if (ret) 706 return ret; 707 } 708 709 return 0; 710 } 711 712 static void media_pipeline_cleanup(struct media_pipeline *pipe) 713 { 714 while (!list_empty(&pipe->pads)) { 715 struct media_pipeline_pad *ppad; 716 717 ppad = list_first_entry(&pipe->pads, typeof(*ppad), list); 718 list_del(&ppad->list); 719 kfree(ppad); 720 } 721 } 722 723 static int media_pipeline_populate(struct media_pipeline *pipe, 724 struct media_pad *pad) 725 { 726 struct media_pipeline_walk walk = { }; 727 struct media_pipeline_pad *ppad; 728 int ret; 729 730 /* 731 * Populate the media pipeline by walking the media graph, starting 732 * from @pad. 733 */ 734 INIT_LIST_HEAD(&pipe->pads); 735 pipe->mdev = pad->graph_obj.mdev; 736 737 walk.mdev = pipe->mdev; 738 walk.stack.top = -1; 739 ret = media_pipeline_add_pad(pipe, &walk, pad); 740 if (ret) 741 goto done; 742 743 /* 744 * Use a depth-first search algorithm: as long as the stack is not 745 * empty, explore the next link of the top entry. The 746 * media_pipeline_explore_next_link() function will either move to the 747 * next link, pop the entry if fully visited, or add new entries on 748 * top. 749 */ 750 while (!media_pipeline_walk_empty(&walk)) { 751 ret = media_pipeline_explore_next_link(pipe, &walk); 752 if (ret) 753 goto done; 754 } 755 756 dev_dbg(pad->graph_obj.mdev->dev, 757 "media pipeline populated, found pads:\n"); 758 759 list_for_each_entry(ppad, &pipe->pads, list) 760 dev_dbg(pad->graph_obj.mdev->dev, "- '%s':%u\n", 761 ppad->pad->entity->name, ppad->pad->index); 762 763 WARN_ON(walk.stack.top != -1); 764 765 ret = 0; 766 767 done: 768 media_pipeline_walk_destroy(&walk); 769 770 if (ret) 771 media_pipeline_cleanup(pipe); 772 773 return ret; 774 } 775 776 __must_check int __media_pipeline_start(struct media_pad *origin, 777 struct media_pipeline *pipe) 778 { 779 struct media_device *mdev = origin->graph_obj.mdev; 780 struct media_pipeline_pad *err_ppad; 781 struct media_pipeline_pad *ppad; 782 int ret; 783 784 lockdep_assert_held(&mdev->graph_mutex); 785 786 /* 787 * If the pad is already part of a pipeline, that pipeline must be the 788 * same as the pipe given to media_pipeline_start(). 789 */ 790 if (WARN_ON(origin->pipe && origin->pipe != pipe)) 791 return -EINVAL; 792 793 /* 794 * If the pipeline has already been started, it is guaranteed to be 795 * valid, so just increase the start count. 796 */ 797 if (pipe->start_count) { 798 pipe->start_count++; 799 return 0; 800 } 801 802 /* 803 * Populate the pipeline. This populates the media_pipeline pads list 804 * with media_pipeline_pad instances for each pad found during graph 805 * walk. 806 */ 807 ret = media_pipeline_populate(pipe, origin); 808 if (ret) 809 return ret; 810 811 /* 812 * Now that all the pads in the pipeline have been gathered, perform 813 * the validation steps. 814 */ 815 816 list_for_each_entry(ppad, &pipe->pads, list) { 817 struct media_pad *pad = ppad->pad; 818 struct media_entity *entity = pad->entity; 819 bool has_enabled_link = false; 820 struct media_link *link; 821 822 dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name, 823 pad->index); 824 825 /* 826 * 1. Ensure that the pad doesn't already belong to a different 827 * pipeline. 828 */ 829 if (pad->pipe) { 830 dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n", 831 pad->entity->name, pad->index); 832 ret = -EBUSY; 833 goto error; 834 } 835 836 /* 837 * 2. Validate all active links whose sink is the current pad. 838 * Validation of the source pads is performed in the context of 839 * the connected sink pad to avoid duplicating checks. 840 */ 841 for_each_media_entity_data_link(entity, link) { 842 /* Skip links unrelated to the current pad. */ 843 if (link->sink != pad && link->source != pad) 844 continue; 845 846 /* Record if the pad has links and enabled links. */ 847 if (link->flags & MEDIA_LNK_FL_ENABLED) 848 has_enabled_link = true; 849 850 /* 851 * Validate the link if it's enabled and has the 852 * current pad as its sink. 853 */ 854 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) 855 continue; 856 857 if (link->sink != pad) 858 continue; 859 860 if (!entity->ops || !entity->ops->link_validate) 861 continue; 862 863 ret = entity->ops->link_validate(link); 864 if (ret) { 865 dev_dbg(mdev->dev, 866 "Link '%s':%u -> '%s':%u failed validation: %d\n", 867 link->source->entity->name, 868 link->source->index, 869 link->sink->entity->name, 870 link->sink->index, ret); 871 goto error; 872 } 873 874 dev_dbg(mdev->dev, 875 "Link '%s':%u -> '%s':%u is valid\n", 876 link->source->entity->name, 877 link->source->index, 878 link->sink->entity->name, 879 link->sink->index); 880 } 881 882 /* 883 * 3. If the pad has the MEDIA_PAD_FL_MUST_CONNECT flag set, 884 * ensure that it has either no link or an enabled link. 885 */ 886 if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) && 887 !has_enabled_link) { 888 dev_dbg(mdev->dev, 889 "Pad '%s':%u must be connected by an enabled link\n", 890 pad->entity->name, pad->index); 891 ret = -ENOLINK; 892 goto error; 893 } 894 895 /* Validation passed, store the pipe pointer in the pad. */ 896 pad->pipe = pipe; 897 } 898 899 pipe->start_count++; 900 901 return 0; 902 903 error: 904 /* 905 * Link validation on graph failed. We revert what we did and 906 * return the error. 907 */ 908 909 list_for_each_entry(err_ppad, &pipe->pads, list) { 910 if (err_ppad == ppad) 911 break; 912 913 err_ppad->pad->pipe = NULL; 914 } 915 916 media_pipeline_cleanup(pipe); 917 918 return ret; 919 } 920 EXPORT_SYMBOL_GPL(__media_pipeline_start); 921 922 __must_check int media_pipeline_start(struct media_pad *origin, 923 struct media_pipeline *pipe) 924 { 925 struct media_device *mdev = origin->graph_obj.mdev; 926 int ret; 927 928 mutex_lock(&mdev->graph_mutex); 929 ret = __media_pipeline_start(origin, pipe); 930 mutex_unlock(&mdev->graph_mutex); 931 return ret; 932 } 933 EXPORT_SYMBOL_GPL(media_pipeline_start); 934 935 void __media_pipeline_stop(struct media_pad *pad) 936 { 937 struct media_pipeline *pipe = pad->pipe; 938 struct media_pipeline_pad *ppad; 939 940 /* 941 * If the following check fails, the driver has performed an 942 * unbalanced call to media_pipeline_stop() 943 */ 944 if (WARN_ON(!pipe)) 945 return; 946 947 if (--pipe->start_count) 948 return; 949 950 list_for_each_entry(ppad, &pipe->pads, list) 951 ppad->pad->pipe = NULL; 952 953 media_pipeline_cleanup(pipe); 954 955 if (pipe->allocated) 956 kfree(pipe); 957 } 958 EXPORT_SYMBOL_GPL(__media_pipeline_stop); 959 960 void media_pipeline_stop(struct media_pad *pad) 961 { 962 struct media_device *mdev = pad->graph_obj.mdev; 963 964 mutex_lock(&mdev->graph_mutex); 965 __media_pipeline_stop(pad); 966 mutex_unlock(&mdev->graph_mutex); 967 } 968 EXPORT_SYMBOL_GPL(media_pipeline_stop); 969 970 __must_check int media_pipeline_alloc_start(struct media_pad *pad) 971 { 972 struct media_device *mdev = pad->graph_obj.mdev; 973 struct media_pipeline *new_pipe = NULL; 974 struct media_pipeline *pipe; 975 int ret; 976 977 mutex_lock(&mdev->graph_mutex); 978 979 /* 980 * Is the pad already part of a pipeline? If not, we need to allocate 981 * a pipe. 982 */ 983 pipe = media_pad_pipeline(pad); 984 if (!pipe) { 985 new_pipe = kzalloc_obj(*new_pipe); 986 if (!new_pipe) { 987 ret = -ENOMEM; 988 goto out; 989 } 990 991 pipe = new_pipe; 992 pipe->allocated = true; 993 } 994 995 ret = __media_pipeline_start(pad, pipe); 996 if (ret) 997 kfree(new_pipe); 998 999 out: 1000 mutex_unlock(&mdev->graph_mutex); 1001 1002 return ret; 1003 } 1004 EXPORT_SYMBOL_GPL(media_pipeline_alloc_start); 1005 1006 struct media_pad * 1007 __media_pipeline_pad_iter_next(struct media_pipeline *pipe, 1008 struct media_pipeline_pad_iter *iter, 1009 struct media_pad *pad) 1010 { 1011 if (!pad) 1012 iter->cursor = pipe->pads.next; 1013 1014 if (iter->cursor == &pipe->pads) 1015 return NULL; 1016 1017 pad = list_entry(iter->cursor, struct media_pipeline_pad, list)->pad; 1018 iter->cursor = iter->cursor->next; 1019 1020 return pad; 1021 } 1022 EXPORT_SYMBOL_GPL(__media_pipeline_pad_iter_next); 1023 1024 int media_pipeline_entity_iter_init(struct media_pipeline *pipe, 1025 struct media_pipeline_entity_iter *iter) 1026 { 1027 return media_entity_enum_init(&iter->ent_enum, pipe->mdev); 1028 } 1029 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_init); 1030 1031 void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter) 1032 { 1033 media_entity_enum_cleanup(&iter->ent_enum); 1034 } 1035 EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_cleanup); 1036 1037 struct media_entity * 1038 __media_pipeline_entity_iter_next(struct media_pipeline *pipe, 1039 struct media_pipeline_entity_iter *iter, 1040 struct media_entity *entity) 1041 { 1042 if (!entity) 1043 iter->cursor = pipe->pads.next; 1044 1045 while (iter->cursor != &pipe->pads) { 1046 struct media_pipeline_pad *ppad; 1047 struct media_entity *entity; 1048 1049 ppad = list_entry(iter->cursor, struct media_pipeline_pad, list); 1050 entity = ppad->pad->entity; 1051 iter->cursor = iter->cursor->next; 1052 1053 if (!media_entity_enum_test_and_set(&iter->ent_enum, entity)) 1054 return entity; 1055 } 1056 1057 return NULL; 1058 } 1059 EXPORT_SYMBOL_GPL(__media_pipeline_entity_iter_next); 1060 1061 /* ----------------------------------------------------------------------------- 1062 * Links management 1063 */ 1064 1065 static struct media_link *media_add_link(struct list_head *head) 1066 { 1067 struct media_link *link; 1068 1069 link = kzalloc_obj(*link); 1070 if (link == NULL) 1071 return NULL; 1072 1073 list_add_tail(&link->list, head); 1074 1075 return link; 1076 } 1077 1078 static void __media_entity_remove_link(struct media_entity *entity, 1079 struct media_link *link) 1080 { 1081 struct media_link *rlink, *tmp; 1082 struct media_entity *remote; 1083 1084 /* Remove the reverse links for a data link. */ 1085 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) { 1086 link->source->num_links--; 1087 link->sink->num_links--; 1088 1089 if (link->source->entity == entity) 1090 remote = link->sink->entity; 1091 else 1092 remote = link->source->entity; 1093 1094 list_for_each_entry_safe(rlink, tmp, &remote->links, list) { 1095 if (rlink != link->reverse) 1096 continue; 1097 1098 if (link->source->entity == entity) 1099 remote->num_backlinks--; 1100 1101 /* Remove the remote link */ 1102 list_del(&rlink->list); 1103 media_gobj_destroy(&rlink->graph_obj); 1104 kfree(rlink); 1105 1106 if (--remote->num_links == 0) 1107 break; 1108 } 1109 } 1110 1111 list_del(&link->list); 1112 media_gobj_destroy(&link->graph_obj); 1113 kfree(link); 1114 } 1115 1116 int media_get_pad_index(struct media_entity *entity, u32 pad_type, 1117 enum media_pad_signal_type sig_type) 1118 { 1119 unsigned int i; 1120 1121 if (!entity) 1122 return -EINVAL; 1123 1124 for (i = 0; i < entity->num_pads; i++) { 1125 if ((entity->pads[i].flags & 1126 (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE | 1127 MEDIA_PAD_FL_INTERNAL)) != pad_type) 1128 continue; 1129 1130 if (entity->pads[i].sig_type == sig_type) 1131 return i; 1132 } 1133 return -EINVAL; 1134 } 1135 EXPORT_SYMBOL_GPL(media_get_pad_index); 1136 1137 int 1138 media_create_pad_link(struct media_entity *source, u16 source_pad, 1139 struct media_entity *sink, u16 sink_pad, u32 flags) 1140 { 1141 struct media_link *link; 1142 struct media_link *backlink; 1143 1144 if (flags & MEDIA_LNK_FL_LINK_TYPE) 1145 return -EINVAL; 1146 1147 flags |= MEDIA_LNK_FL_DATA_LINK; 1148 1149 if (WARN_ON(!source || !sink) || 1150 WARN_ON(source_pad >= source->num_pads) || 1151 WARN_ON(sink_pad >= sink->num_pads)) 1152 return -EINVAL; 1153 if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE))) 1154 return -EINVAL; 1155 if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK))) 1156 return -EINVAL; 1157 if (WARN_ON(source->pads[source_pad].flags & MEDIA_PAD_FL_INTERNAL) || 1158 WARN_ON(sink->pads[sink_pad].flags & MEDIA_PAD_FL_INTERNAL)) 1159 return -EINVAL; 1160 1161 link = media_add_link(&source->links); 1162 if (link == NULL) 1163 return -ENOMEM; 1164 1165 link->source = &source->pads[source_pad]; 1166 link->sink = &sink->pads[sink_pad]; 1167 link->flags = flags; 1168 1169 /* Initialize graph object embedded at the new link */ 1170 media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK, 1171 &link->graph_obj); 1172 1173 /* Create the backlink. Backlinks are used to help graph traversal and 1174 * are not reported to userspace. 1175 */ 1176 backlink = media_add_link(&sink->links); 1177 if (backlink == NULL) { 1178 __media_entity_remove_link(source, link); 1179 return -ENOMEM; 1180 } 1181 1182 backlink->source = &source->pads[source_pad]; 1183 backlink->sink = &sink->pads[sink_pad]; 1184 backlink->flags = flags; 1185 backlink->is_backlink = true; 1186 1187 /* Initialize graph object embedded at the new link */ 1188 media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, 1189 &backlink->graph_obj); 1190 1191 link->reverse = backlink; 1192 backlink->reverse = link; 1193 1194 sink->num_backlinks++; 1195 sink->num_links++; 1196 source->num_links++; 1197 1198 link->source->num_links++; 1199 link->sink->num_links++; 1200 1201 return 0; 1202 } 1203 EXPORT_SYMBOL_GPL(media_create_pad_link); 1204 1205 int media_create_pad_links(const struct media_device *mdev, 1206 const u32 source_function, 1207 struct media_entity *source, 1208 const u16 source_pad, 1209 const u32 sink_function, 1210 struct media_entity *sink, 1211 const u16 sink_pad, 1212 u32 flags, 1213 const bool allow_both_undefined) 1214 { 1215 struct media_entity *entity; 1216 unsigned function; 1217 int ret; 1218 1219 /* Trivial case: 1:1 relation */ 1220 if (source && sink) 1221 return media_create_pad_link(source, source_pad, 1222 sink, sink_pad, flags); 1223 1224 /* Worse case scenario: n:n relation */ 1225 if (!source && !sink) { 1226 if (!allow_both_undefined) 1227 return 0; 1228 media_device_for_each_entity(source, mdev) { 1229 if (source->function != source_function) 1230 continue; 1231 media_device_for_each_entity(sink, mdev) { 1232 if (sink->function != sink_function) 1233 continue; 1234 ret = media_create_pad_link(source, source_pad, 1235 sink, sink_pad, 1236 flags); 1237 if (ret) 1238 return ret; 1239 flags &= ~(MEDIA_LNK_FL_ENABLED | 1240 MEDIA_LNK_FL_IMMUTABLE); 1241 } 1242 } 1243 return 0; 1244 } 1245 1246 /* Handle 1:n and n:1 cases */ 1247 if (source) 1248 function = sink_function; 1249 else 1250 function = source_function; 1251 1252 media_device_for_each_entity(entity, mdev) { 1253 if (entity->function != function) 1254 continue; 1255 1256 if (source) 1257 ret = media_create_pad_link(source, source_pad, 1258 entity, sink_pad, flags); 1259 else 1260 ret = media_create_pad_link(entity, source_pad, 1261 sink, sink_pad, flags); 1262 if (ret) 1263 return ret; 1264 flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); 1265 } 1266 return 0; 1267 } 1268 EXPORT_SYMBOL_GPL(media_create_pad_links); 1269 1270 void __media_entity_remove_links(struct media_entity *entity) 1271 { 1272 struct media_link *link, *tmp; 1273 1274 list_for_each_entry_safe(link, tmp, &entity->links, list) 1275 __media_entity_remove_link(entity, link); 1276 1277 entity->num_links = 0; 1278 entity->num_backlinks = 0; 1279 } 1280 EXPORT_SYMBOL_GPL(__media_entity_remove_links); 1281 1282 void media_entity_remove_links(struct media_entity *entity) 1283 { 1284 struct media_device *mdev = entity->graph_obj.mdev; 1285 1286 /* Do nothing if the entity is not registered. */ 1287 if (mdev == NULL) 1288 return; 1289 1290 mutex_lock(&mdev->graph_mutex); 1291 __media_entity_remove_links(entity); 1292 mutex_unlock(&mdev->graph_mutex); 1293 } 1294 EXPORT_SYMBOL_GPL(media_entity_remove_links); 1295 1296 static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) 1297 { 1298 int ret; 1299 1300 /* Notify both entities. */ 1301 ret = media_entity_call(link->source->entity, link_setup, 1302 link->source, link->sink, flags); 1303 if (ret < 0 && ret != -ENOIOCTLCMD) 1304 return ret; 1305 1306 ret = media_entity_call(link->sink->entity, link_setup, 1307 link->sink, link->source, flags); 1308 if (ret < 0 && ret != -ENOIOCTLCMD) { 1309 media_entity_call(link->source->entity, link_setup, 1310 link->source, link->sink, link->flags); 1311 return ret; 1312 } 1313 1314 link->flags = flags; 1315 link->reverse->flags = link->flags; 1316 1317 return 0; 1318 } 1319 1320 int __media_entity_setup_link(struct media_link *link, u32 flags) 1321 { 1322 const u32 mask = MEDIA_LNK_FL_ENABLED; 1323 struct media_device *mdev; 1324 struct media_pad *source, *sink; 1325 int ret = -EBUSY; 1326 1327 if (link == NULL) 1328 return -EINVAL; 1329 1330 /* The non-modifiable link flags must not be modified. */ 1331 if ((link->flags & ~mask) != (flags & ~mask)) 1332 return -EINVAL; 1333 1334 if (link->flags & MEDIA_LNK_FL_IMMUTABLE) 1335 return link->flags == flags ? 0 : -EINVAL; 1336 1337 if (link->flags == flags) 1338 return 0; 1339 1340 source = link->source; 1341 sink = link->sink; 1342 1343 if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && 1344 (media_pad_is_streaming(source) || media_pad_is_streaming(sink))) 1345 return -EBUSY; 1346 1347 mdev = source->graph_obj.mdev; 1348 1349 if (mdev->ops && mdev->ops->link_notify) { 1350 ret = mdev->ops->link_notify(link, flags, 1351 MEDIA_DEV_NOTIFY_PRE_LINK_CH); 1352 if (ret < 0) 1353 return ret; 1354 } 1355 1356 ret = __media_entity_setup_link_notify(link, flags); 1357 1358 if (mdev->ops && mdev->ops->link_notify) 1359 mdev->ops->link_notify(link, flags, 1360 MEDIA_DEV_NOTIFY_POST_LINK_CH); 1361 1362 return ret; 1363 } 1364 EXPORT_SYMBOL_GPL(__media_entity_setup_link); 1365 1366 int media_entity_setup_link(struct media_link *link, u32 flags) 1367 { 1368 int ret; 1369 1370 mutex_lock(&link->graph_obj.mdev->graph_mutex); 1371 ret = __media_entity_setup_link(link, flags); 1372 mutex_unlock(&link->graph_obj.mdev->graph_mutex); 1373 1374 return ret; 1375 } 1376 EXPORT_SYMBOL_GPL(media_entity_setup_link); 1377 1378 struct media_link * 1379 media_entity_find_link(struct media_pad *source, struct media_pad *sink) 1380 { 1381 struct media_link *link; 1382 1383 for_each_media_entity_data_link(source->entity, link) { 1384 if (link->source->entity == source->entity && 1385 link->source->index == source->index && 1386 link->sink->entity == sink->entity && 1387 link->sink->index == sink->index) 1388 return link; 1389 } 1390 1391 return NULL; 1392 } 1393 EXPORT_SYMBOL_GPL(media_entity_find_link); 1394 1395 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad) 1396 { 1397 struct media_link *link; 1398 1399 for_each_media_entity_data_link(pad->entity, link) { 1400 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) 1401 continue; 1402 1403 if (link->source == pad) 1404 return link->sink; 1405 1406 if (link->sink == pad) 1407 return link->source; 1408 } 1409 1410 return NULL; 1411 1412 } 1413 EXPORT_SYMBOL_GPL(media_pad_remote_pad_first); 1414 1415 struct media_pad * 1416 media_entity_remote_pad_unique(const struct media_entity *entity, 1417 unsigned int type) 1418 { 1419 struct media_pad *pad = NULL; 1420 struct media_link *link; 1421 1422 list_for_each_entry(link, &entity->links, list) { 1423 struct media_pad *local_pad; 1424 struct media_pad *remote_pad; 1425 1426 if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) != 1427 MEDIA_LNK_FL_DATA_LINK) || 1428 !(link->flags & MEDIA_LNK_FL_ENABLED)) 1429 continue; 1430 1431 if (type == MEDIA_PAD_FL_SOURCE) { 1432 local_pad = link->sink; 1433 remote_pad = link->source; 1434 } else { 1435 local_pad = link->source; 1436 remote_pad = link->sink; 1437 } 1438 1439 if (local_pad->entity == entity) { 1440 if (pad) 1441 return ERR_PTR(-ENOTUNIQ); 1442 1443 pad = remote_pad; 1444 } 1445 } 1446 1447 if (!pad) 1448 return ERR_PTR(-ENOLINK); 1449 1450 return pad; 1451 } 1452 EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique); 1453 1454 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad) 1455 { 1456 struct media_pad *found_pad = NULL; 1457 struct media_link *link; 1458 1459 list_for_each_entry(link, &pad->entity->links, list) { 1460 struct media_pad *remote_pad; 1461 1462 if (!(link->flags & MEDIA_LNK_FL_ENABLED)) 1463 continue; 1464 1465 if (link->sink == pad) 1466 remote_pad = link->source; 1467 else if (link->source == pad) 1468 remote_pad = link->sink; 1469 else 1470 continue; 1471 1472 if (found_pad) 1473 return ERR_PTR(-ENOTUNIQ); 1474 1475 found_pad = remote_pad; 1476 } 1477 1478 if (!found_pad) 1479 return ERR_PTR(-ENOLINK); 1480 1481 return found_pad; 1482 } 1483 EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique); 1484 1485 int media_entity_get_fwnode_pad(struct media_entity *entity, 1486 const struct fwnode_handle *fwnode, 1487 unsigned long direction_flags) 1488 { 1489 struct fwnode_endpoint endpoint; 1490 unsigned int i; 1491 int ret; 1492 1493 if (!entity->ops || !entity->ops->get_fwnode_pad) { 1494 for (i = 0; i < entity->num_pads; i++) { 1495 if (entity->pads[i].flags & direction_flags) 1496 return i; 1497 } 1498 1499 return -ENXIO; 1500 } 1501 1502 ret = fwnode_graph_parse_endpoint(fwnode, &endpoint); 1503 if (ret) 1504 return ret; 1505 1506 ret = entity->ops->get_fwnode_pad(entity, &endpoint); 1507 if (ret < 0) 1508 return ret; 1509 1510 if (ret >= entity->num_pads) 1511 return -ENXIO; 1512 1513 if (!(entity->pads[ret].flags & direction_flags)) 1514 return -ENXIO; 1515 1516 return ret; 1517 } 1518 EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad); 1519 1520 struct media_pipeline *media_entity_pipeline(struct media_entity *entity) 1521 { 1522 struct media_pad *pad; 1523 1524 media_entity_for_each_pad(entity, pad) { 1525 if (pad->pipe) 1526 return pad->pipe; 1527 } 1528 1529 return NULL; 1530 } 1531 EXPORT_SYMBOL_GPL(media_entity_pipeline); 1532 1533 struct media_pipeline *media_pad_pipeline(struct media_pad *pad) 1534 { 1535 return pad->pipe; 1536 } 1537 EXPORT_SYMBOL_GPL(media_pad_pipeline); 1538 1539 static void media_interface_init(struct media_device *mdev, 1540 struct media_interface *intf, 1541 u32 gobj_type, 1542 u32 intf_type, u32 flags) 1543 { 1544 intf->type = intf_type; 1545 intf->flags = flags; 1546 INIT_LIST_HEAD(&intf->links); 1547 1548 media_gobj_create(mdev, gobj_type, &intf->graph_obj); 1549 } 1550 1551 /* Functions related to the media interface via device nodes */ 1552 1553 struct media_intf_devnode *media_devnode_create(struct media_device *mdev, 1554 u32 type, u32 flags, 1555 u32 major, u32 minor) 1556 { 1557 struct media_intf_devnode *devnode; 1558 1559 devnode = kzalloc_obj(*devnode); 1560 if (!devnode) 1561 return NULL; 1562 1563 devnode->major = major; 1564 devnode->minor = minor; 1565 1566 media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE, 1567 type, flags); 1568 1569 return devnode; 1570 } 1571 EXPORT_SYMBOL_GPL(media_devnode_create); 1572 1573 void media_devnode_remove(struct media_intf_devnode *devnode) 1574 { 1575 media_remove_intf_links(&devnode->intf); 1576 media_gobj_destroy(&devnode->intf.graph_obj); 1577 kfree(devnode); 1578 } 1579 EXPORT_SYMBOL_GPL(media_devnode_remove); 1580 1581 struct media_link *media_create_intf_link(struct media_entity *entity, 1582 struct media_interface *intf, 1583 u32 flags) 1584 { 1585 struct media_link *link; 1586 1587 link = media_add_link(&intf->links); 1588 if (link == NULL) 1589 return NULL; 1590 1591 link->intf = intf; 1592 link->entity = entity; 1593 link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK; 1594 1595 /* Initialize graph object embedded at the new link */ 1596 media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK, 1597 &link->graph_obj); 1598 1599 return link; 1600 } 1601 EXPORT_SYMBOL_GPL(media_create_intf_link); 1602 1603 void __media_remove_intf_link(struct media_link *link) 1604 { 1605 list_del(&link->list); 1606 media_gobj_destroy(&link->graph_obj); 1607 kfree(link); 1608 } 1609 EXPORT_SYMBOL_GPL(__media_remove_intf_link); 1610 1611 void media_remove_intf_link(struct media_link *link) 1612 { 1613 struct media_device *mdev = link->graph_obj.mdev; 1614 1615 /* Do nothing if the intf is not registered. */ 1616 if (mdev == NULL) 1617 return; 1618 1619 mutex_lock(&mdev->graph_mutex); 1620 __media_remove_intf_link(link); 1621 mutex_unlock(&mdev->graph_mutex); 1622 } 1623 EXPORT_SYMBOL_GPL(media_remove_intf_link); 1624 1625 void __media_remove_intf_links(struct media_interface *intf) 1626 { 1627 struct media_link *link, *tmp; 1628 1629 list_for_each_entry_safe(link, tmp, &intf->links, list) 1630 __media_remove_intf_link(link); 1631 1632 } 1633 EXPORT_SYMBOL_GPL(__media_remove_intf_links); 1634 1635 void media_remove_intf_links(struct media_interface *intf) 1636 { 1637 struct media_device *mdev = intf->graph_obj.mdev; 1638 1639 /* Do nothing if the intf is not registered. */ 1640 if (mdev == NULL) 1641 return; 1642 1643 mutex_lock(&mdev->graph_mutex); 1644 __media_remove_intf_links(intf); 1645 mutex_unlock(&mdev->graph_mutex); 1646 } 1647 EXPORT_SYMBOL_GPL(media_remove_intf_links); 1648 1649 struct media_link *media_create_ancillary_link(struct media_entity *primary, 1650 struct media_entity *ancillary) 1651 { 1652 struct media_link *link; 1653 1654 link = media_add_link(&primary->links); 1655 if (!link) 1656 return ERR_PTR(-ENOMEM); 1657 1658 link->gobj0 = &primary->graph_obj; 1659 link->gobj1 = &ancillary->graph_obj; 1660 link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED | 1661 MEDIA_LNK_FL_ANCILLARY_LINK; 1662 1663 /* Initialize graph object embedded in the new link */ 1664 media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK, 1665 &link->graph_obj); 1666 1667 return link; 1668 } 1669 EXPORT_SYMBOL_GPL(media_create_ancillary_link); 1670 1671 struct media_link *__media_entity_next_link(struct media_entity *entity, 1672 struct media_link *link, 1673 unsigned long link_type) 1674 { 1675 link = link ? list_next_entry(link, list) 1676 : list_first_entry(&entity->links, typeof(*link), list); 1677 1678 list_for_each_entry_from(link, &entity->links, list) 1679 if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type) 1680 return link; 1681 1682 return NULL; 1683 } 1684 EXPORT_SYMBOL_GPL(__media_entity_next_link); 1685