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 #ifndef _MEDIA_ENTITY_H 12 #define _MEDIA_ENTITY_H 13 14 #include <linux/bitmap.h> 15 #include <linux/bug.h> 16 #include <linux/container_of.h> 17 #include <linux/fwnode.h> 18 #include <linux/list.h> 19 #include <linux/media.h> 20 #include <linux/minmax.h> 21 #include <linux/types.h> 22 23 /* Enums used internally at the media controller to represent graphs */ 24 25 /** 26 * enum media_gobj_type - type of a graph object 27 * 28 * @MEDIA_GRAPH_ENTITY: Identify a media entity 29 * @MEDIA_GRAPH_PAD: Identify a media pad 30 * @MEDIA_GRAPH_LINK: Identify a media link 31 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via 32 * a device node 33 */ 34 enum media_gobj_type { 35 MEDIA_GRAPH_ENTITY, 36 MEDIA_GRAPH_PAD, 37 MEDIA_GRAPH_LINK, 38 MEDIA_GRAPH_INTF_DEVNODE, 39 }; 40 41 #define MEDIA_BITS_PER_TYPE 8 42 #define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE) 43 #define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0) 44 45 /* Structs to represent the objects that belong to a media graph */ 46 47 /** 48 * struct media_gobj - Define a graph object. 49 * 50 * @mdev: Pointer to the struct &media_device that owns the object 51 * @id: Non-zero object ID identifier. The ID should be unique 52 * inside a media_device, as it is composed by 53 * %MEDIA_BITS_PER_TYPE to store the type plus 54 * %MEDIA_BITS_PER_ID to store the ID 55 * @list: List entry stored in one of the per-type mdev object lists 56 * 57 * All objects on the media graph should have this struct embedded 58 */ 59 struct media_gobj { 60 struct media_device *mdev; 61 u32 id; 62 struct list_head list; 63 }; 64 65 #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16 66 67 /** 68 * struct media_entity_enum - An enumeration of media entities. 69 * 70 * @bmap: Bit map in which each bit represents one entity at struct 71 * media_entity->internal_idx. 72 * @idx_max: Number of bits in bmap 73 */ 74 struct media_entity_enum { 75 unsigned long *bmap; 76 int idx_max; 77 }; 78 79 /** 80 * struct media_graph - Media graph traversal state 81 * 82 * @stack: Graph traversal stack; the stack contains information 83 * on the path the media entities to be walked and the 84 * links through which they were reached. 85 * @stack.entity: pointer to &struct media_entity at the graph. 86 * @stack.link: pointer to &struct list_head. 87 * @ent_enum: Visited entities 88 * @top: The top of the stack 89 */ 90 struct media_graph { 91 struct { 92 struct media_entity *entity; 93 struct list_head *link; 94 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH]; 95 96 struct media_entity_enum ent_enum; 97 int top; 98 }; 99 100 /** 101 * struct media_pipeline - Media pipeline related information 102 * 103 * @allocated: Media pipeline allocated and freed by the framework 104 * @start_count: Media pipeline start - stop count 105 * @graph: Media graph walk during pipeline start / stop 106 */ 107 struct media_pipeline { 108 bool allocated; 109 int start_count; 110 struct media_graph graph; 111 }; 112 113 /** 114 * struct media_link - A link object part of a media graph. 115 * 116 * @graph_obj: Embedded structure containing the media object common data 117 * @list: Linked list associated with an entity or an interface that 118 * owns the link. 119 * @gobj0: Part of a union. Used to get the pointer for the first 120 * graph_object of the link. 121 * @source: Part of a union. Used only if the first object (gobj0) is 122 * a pad. In that case, it represents the source pad. 123 * @intf: Part of a union. Used only if the first object (gobj0) is 124 * an interface. 125 * @gobj1: Part of a union. Used to get the pointer for the second 126 * graph_object of the link. 127 * @sink: Part of a union. Used only if the second object (gobj1) is 128 * a pad. In that case, it represents the sink pad. 129 * @entity: Part of a union. Used only if the second object (gobj1) is 130 * an entity. 131 * @reverse: Pointer to the link for the reverse direction of a pad to pad 132 * link. 133 * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*) 134 * @is_backlink: Indicate if the link is a backlink. 135 */ 136 struct media_link { 137 struct media_gobj graph_obj; 138 struct list_head list; 139 union { 140 struct media_gobj *gobj0; 141 struct media_pad *source; 142 struct media_interface *intf; 143 }; 144 union { 145 struct media_gobj *gobj1; 146 struct media_pad *sink; 147 struct media_entity *entity; 148 }; 149 struct media_link *reverse; 150 unsigned long flags; 151 bool is_backlink; 152 }; 153 154 /** 155 * enum media_pad_signal_type - type of the signal inside a media pad 156 * 157 * @PAD_SIGNAL_DEFAULT: 158 * Default signal. Use this when all inputs or all outputs are 159 * uniquely identified by the pad number. 160 * @PAD_SIGNAL_ANALOG: 161 * The pad contains an analog signal. It can be Radio Frequency, 162 * Intermediate Frequency, a baseband signal or sub-carriers. 163 * Tuner inputs, IF-PLL demodulators, composite and s-video signals 164 * should use it. 165 * @PAD_SIGNAL_DV: 166 * Contains a digital video signal, with can be a bitstream of samples 167 * taken from an analog TV video source. On such case, it usually 168 * contains the VBI data on it. 169 * @PAD_SIGNAL_AUDIO: 170 * Contains an Intermediate Frequency analog signal from an audio 171 * sub-carrier or an audio bitstream. IF signals are provided by tuners 172 * and consumed by audio AM/FM decoders. Bitstream audio is provided by 173 * an audio decoder. 174 */ 175 enum media_pad_signal_type { 176 PAD_SIGNAL_DEFAULT = 0, 177 PAD_SIGNAL_ANALOG, 178 PAD_SIGNAL_DV, 179 PAD_SIGNAL_AUDIO, 180 }; 181 182 /** 183 * struct media_pad - A media pad graph object. 184 * 185 * @graph_obj: Embedded structure containing the media object common data 186 * @entity: Entity this pad belongs to 187 * @index: Pad index in the entity pads array, numbered from 0 to n 188 * @sig_type: Type of the signal inside a media pad 189 * @flags: Pad flags, as defined in 190 * :ref:`include/uapi/linux/media.h <media_header>` 191 * (seek for ``MEDIA_PAD_FL_*``) 192 */ 193 struct media_pad { 194 struct media_gobj graph_obj; /* must be first field in struct */ 195 struct media_entity *entity; 196 u16 index; 197 enum media_pad_signal_type sig_type; 198 unsigned long flags; 199 }; 200 201 /** 202 * struct media_entity_operations - Media entity operations 203 * @get_fwnode_pad: Return the pad number based on a fwnode endpoint or 204 * a negative value on error. This operation can be used 205 * to map a fwnode to a media pad number. Optional. 206 * @link_setup: Notify the entity of link changes. The operation can 207 * return an error, in which case link setup will be 208 * cancelled. Optional. 209 * @link_validate: Return whether a link is valid from the entity point of 210 * view. The media_pipeline_start() function 211 * validates all links by calling this operation. Optional. 212 * 213 * .. note:: 214 * 215 * Those these callbacks are called with struct &media_device.graph_mutex 216 * mutex held. 217 */ 218 struct media_entity_operations { 219 int (*get_fwnode_pad)(struct media_entity *entity, 220 struct fwnode_endpoint *endpoint); 221 int (*link_setup)(struct media_entity *entity, 222 const struct media_pad *local, 223 const struct media_pad *remote, u32 flags); 224 int (*link_validate)(struct media_link *link); 225 }; 226 227 /** 228 * enum media_entity_type - Media entity type 229 * 230 * @MEDIA_ENTITY_TYPE_BASE: 231 * The entity isn't embedded in another subsystem structure. 232 * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE: 233 * The entity is embedded in a struct video_device instance. 234 * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV: 235 * The entity is embedded in a struct v4l2_subdev instance. 236 * 237 * Media entity objects are often not instantiated directly, but the media 238 * entity structure is inherited by (through embedding) other subsystem-specific 239 * structures. The media entity type identifies the type of the subclass 240 * structure that implements a media entity instance. 241 * 242 * This allows runtime type identification of media entities and safe casting to 243 * the correct object type. For instance, a media entity structure instance 244 * embedded in a v4l2_subdev structure instance will have the type 245 * %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev 246 * structure using the container_of() macro. 247 */ 248 enum media_entity_type { 249 MEDIA_ENTITY_TYPE_BASE, 250 MEDIA_ENTITY_TYPE_VIDEO_DEVICE, 251 MEDIA_ENTITY_TYPE_V4L2_SUBDEV, 252 }; 253 254 /** 255 * struct media_entity - A media entity graph object. 256 * 257 * @graph_obj: Embedded structure containing the media object common data. 258 * @name: Entity name. 259 * @obj_type: Type of the object that implements the media_entity. 260 * @function: Entity main function, as defined in 261 * :ref:`include/uapi/linux/media.h <media_header>` 262 * (seek for ``MEDIA_ENT_F_*``) 263 * @flags: Entity flags, as defined in 264 * :ref:`include/uapi/linux/media.h <media_header>` 265 * (seek for ``MEDIA_ENT_FL_*``) 266 * @num_pads: Number of sink and source pads. 267 * @num_links: Total number of links, forward and back, enabled and disabled. 268 * @num_backlinks: Number of backlinks 269 * @internal_idx: An unique internal entity specific number. The numbers are 270 * re-used if entities are unregistered or registered again. 271 * @pads: Pads array with the size defined by @num_pads. 272 * @links: List of data links. 273 * @ops: Entity operations. 274 * @use_count: Use count for the entity. 275 * @pipe: Pipeline this entity belongs to. 276 * @info: Union with devnode information. Kept just for backward 277 * compatibility. 278 * @info.dev: Contains device major and minor info. 279 * @info.dev.major: device node major, if the device is a devnode. 280 * @info.dev.minor: device node minor, if the device is a devnode. 281 * @major: Devnode major number (zero if not applicable). Kept just 282 * for backward compatibility. 283 * @minor: Devnode minor number (zero if not applicable). Kept just 284 * for backward compatibility. 285 * 286 * .. note:: 287 * 288 * The @use_count reference count must never be negative, but is a signed 289 * integer on purpose: a simple ``WARN_ON(<0)`` check can be used to detect 290 * reference count bugs that would make it negative. 291 */ 292 struct media_entity { 293 struct media_gobj graph_obj; /* must be first field in struct */ 294 const char *name; 295 enum media_entity_type obj_type; 296 u32 function; 297 unsigned long flags; 298 299 u16 num_pads; 300 u16 num_links; 301 u16 num_backlinks; 302 int internal_idx; 303 304 struct media_pad *pads; 305 struct list_head links; 306 307 const struct media_entity_operations *ops; 308 309 int use_count; 310 311 struct media_pipeline *pipe; 312 313 union { 314 struct { 315 u32 major; 316 u32 minor; 317 } dev; 318 } info; 319 }; 320 321 /** 322 * media_entity_for_each_pad - Iterate on all pads in an entity 323 * @entity: The entity the pads belong to 324 * @iter: The iterator pad 325 * 326 * Iterate on all pads in a media entity. 327 */ 328 #define media_entity_for_each_pad(entity, iter) \ 329 for (iter = (entity)->pads; \ 330 iter < &(entity)->pads[(entity)->num_pads]; \ 331 ++iter) 332 333 /** 334 * struct media_interface - A media interface graph object. 335 * 336 * @graph_obj: embedded graph object 337 * @links: List of links pointing to graph entities 338 * @type: Type of the interface as defined in 339 * :ref:`include/uapi/linux/media.h <media_header>` 340 * (seek for ``MEDIA_INTF_T_*``) 341 * @flags: Interface flags as defined in 342 * :ref:`include/uapi/linux/media.h <media_header>` 343 * (seek for ``MEDIA_INTF_FL_*``) 344 * 345 * .. note:: 346 * 347 * Currently, no flags for &media_interface is defined. 348 */ 349 struct media_interface { 350 struct media_gobj graph_obj; 351 struct list_head links; 352 u32 type; 353 u32 flags; 354 }; 355 356 /** 357 * struct media_intf_devnode - A media interface via a device node. 358 * 359 * @intf: embedded interface object 360 * @major: Major number of a device node 361 * @minor: Minor number of a device node 362 */ 363 struct media_intf_devnode { 364 struct media_interface intf; 365 366 /* Should match the fields at media_v2_intf_devnode */ 367 u32 major; 368 u32 minor; 369 }; 370 371 /** 372 * media_entity_id() - return the media entity graph object id 373 * 374 * @entity: pointer to &media_entity 375 */ 376 static inline u32 media_entity_id(struct media_entity *entity) 377 { 378 return entity->graph_obj.id; 379 } 380 381 /** 382 * media_type() - return the media object type 383 * 384 * @gobj: Pointer to the struct &media_gobj graph object 385 */ 386 static inline enum media_gobj_type media_type(struct media_gobj *gobj) 387 { 388 return gobj->id >> MEDIA_BITS_PER_ID; 389 } 390 391 /** 392 * media_id() - return the media object ID 393 * 394 * @gobj: Pointer to the struct &media_gobj graph object 395 */ 396 static inline u32 media_id(struct media_gobj *gobj) 397 { 398 return gobj->id & MEDIA_ID_MASK; 399 } 400 401 /** 402 * media_gobj_gen_id() - encapsulates type and ID on at the object ID 403 * 404 * @type: object type as define at enum &media_gobj_type. 405 * @local_id: next ID, from struct &media_device.id. 406 */ 407 static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id) 408 { 409 u32 id; 410 411 id = type << MEDIA_BITS_PER_ID; 412 id |= local_id & MEDIA_ID_MASK; 413 414 return id; 415 } 416 417 /** 418 * is_media_entity_v4l2_video_device() - Check if the entity is a video_device 419 * @entity: pointer to entity 420 * 421 * Return: %true if the entity is an instance of a video_device object and can 422 * safely be cast to a struct video_device using the container_of() macro, or 423 * %false otherwise. 424 */ 425 static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity) 426 { 427 return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE; 428 } 429 430 /** 431 * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev 432 * @entity: pointer to entity 433 * 434 * Return: %true if the entity is an instance of a &v4l2_subdev object and can 435 * safely be cast to a struct &v4l2_subdev using the container_of() macro, or 436 * %false otherwise. 437 */ 438 static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity) 439 { 440 return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 441 } 442 443 /** 444 * media_entity_enum_init - Initialise an entity enumeration 445 * 446 * @ent_enum: Entity enumeration to be initialised 447 * @mdev: The related media device 448 * 449 * Return: zero on success or a negative error code. 450 */ 451 __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum, 452 struct media_device *mdev); 453 454 /** 455 * media_entity_enum_cleanup - Release resources of an entity enumeration 456 * 457 * @ent_enum: Entity enumeration to be released 458 */ 459 void media_entity_enum_cleanup(struct media_entity_enum *ent_enum); 460 461 /** 462 * media_entity_enum_zero - Clear the entire enum 463 * 464 * @ent_enum: Entity enumeration to be cleared 465 */ 466 static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum) 467 { 468 bitmap_zero(ent_enum->bmap, ent_enum->idx_max); 469 } 470 471 /** 472 * media_entity_enum_set - Mark a single entity in the enum 473 * 474 * @ent_enum: Entity enumeration 475 * @entity: Entity to be marked 476 */ 477 static inline void media_entity_enum_set(struct media_entity_enum *ent_enum, 478 struct media_entity *entity) 479 { 480 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) 481 return; 482 483 __set_bit(entity->internal_idx, ent_enum->bmap); 484 } 485 486 /** 487 * media_entity_enum_clear - Unmark a single entity in the enum 488 * 489 * @ent_enum: Entity enumeration 490 * @entity: Entity to be unmarked 491 */ 492 static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum, 493 struct media_entity *entity) 494 { 495 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) 496 return; 497 498 __clear_bit(entity->internal_idx, ent_enum->bmap); 499 } 500 501 /** 502 * media_entity_enum_test - Test whether the entity is marked 503 * 504 * @ent_enum: Entity enumeration 505 * @entity: Entity to be tested 506 * 507 * Returns %true if the entity was marked. 508 */ 509 static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum, 510 struct media_entity *entity) 511 { 512 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) 513 return true; 514 515 return test_bit(entity->internal_idx, ent_enum->bmap); 516 } 517 518 /** 519 * media_entity_enum_test_and_set - Test whether the entity is marked, 520 * and mark it 521 * 522 * @ent_enum: Entity enumeration 523 * @entity: Entity to be tested 524 * 525 * Returns %true if the entity was marked, and mark it before doing so. 526 */ 527 static inline bool 528 media_entity_enum_test_and_set(struct media_entity_enum *ent_enum, 529 struct media_entity *entity) 530 { 531 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) 532 return true; 533 534 return __test_and_set_bit(entity->internal_idx, ent_enum->bmap); 535 } 536 537 /** 538 * media_entity_enum_empty - Test whether the entire enum is empty 539 * 540 * @ent_enum: Entity enumeration 541 * 542 * Return: %true if the entity was empty. 543 */ 544 static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum) 545 { 546 return bitmap_empty(ent_enum->bmap, ent_enum->idx_max); 547 } 548 549 /** 550 * media_entity_enum_intersects - Test whether two enums intersect 551 * 552 * @ent_enum1: First entity enumeration 553 * @ent_enum2: Second entity enumeration 554 * 555 * Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect, 556 * otherwise %false. 557 */ 558 static inline bool media_entity_enum_intersects( 559 struct media_entity_enum *ent_enum1, 560 struct media_entity_enum *ent_enum2) 561 { 562 WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max); 563 564 return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap, 565 min(ent_enum1->idx_max, ent_enum2->idx_max)); 566 } 567 568 /** 569 * gobj_to_entity - returns the struct &media_entity pointer from the 570 * @gobj contained on it. 571 * 572 * @gobj: Pointer to the struct &media_gobj graph object 573 */ 574 #define gobj_to_entity(gobj) \ 575 container_of(gobj, struct media_entity, graph_obj) 576 577 /** 578 * gobj_to_pad - returns the struct &media_pad pointer from the 579 * @gobj contained on it. 580 * 581 * @gobj: Pointer to the struct &media_gobj graph object 582 */ 583 #define gobj_to_pad(gobj) \ 584 container_of(gobj, struct media_pad, graph_obj) 585 586 /** 587 * gobj_to_link - returns the struct &media_link pointer from the 588 * @gobj contained on it. 589 * 590 * @gobj: Pointer to the struct &media_gobj graph object 591 */ 592 #define gobj_to_link(gobj) \ 593 container_of(gobj, struct media_link, graph_obj) 594 595 /** 596 * gobj_to_intf - returns the struct &media_interface pointer from the 597 * @gobj contained on it. 598 * 599 * @gobj: Pointer to the struct &media_gobj graph object 600 */ 601 #define gobj_to_intf(gobj) \ 602 container_of(gobj, struct media_interface, graph_obj) 603 604 /** 605 * intf_to_devnode - returns the struct media_intf_devnode pointer from the 606 * @intf contained on it. 607 * 608 * @intf: Pointer to struct &media_intf_devnode 609 */ 610 #define intf_to_devnode(intf) \ 611 container_of(intf, struct media_intf_devnode, intf) 612 613 /** 614 * media_gobj_create - Initialize a graph object 615 * 616 * @mdev: Pointer to the &media_device that contains the object 617 * @type: Type of the object 618 * @gobj: Pointer to the struct &media_gobj graph object 619 * 620 * This routine initializes the embedded struct &media_gobj inside a 621 * media graph object. It is called automatically if ``media_*_create`` 622 * function calls are used. However, if the object (entity, link, pad, 623 * interface) is embedded on some other object, this function should be 624 * called before registering the object at the media controller. 625 */ 626 void media_gobj_create(struct media_device *mdev, 627 enum media_gobj_type type, 628 struct media_gobj *gobj); 629 630 /** 631 * media_gobj_destroy - Stop using a graph object on a media device 632 * 633 * @gobj: Pointer to the struct &media_gobj graph object 634 * 635 * This should be called by all routines like media_device_unregister() 636 * that remove/destroy media graph objects. 637 */ 638 void media_gobj_destroy(struct media_gobj *gobj); 639 640 /** 641 * media_entity_pads_init() - Initialize the entity pads 642 * 643 * @entity: entity where the pads belong 644 * @num_pads: total number of sink and source pads 645 * @pads: Array of @num_pads pads. 646 * 647 * The pads array is managed by the entity driver and passed to 648 * media_entity_pads_init() where its pointer will be stored in the 649 * &media_entity structure. 650 * 651 * If no pads are needed, drivers could either directly fill 652 * &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call 653 * this function that will do the same. 654 * 655 * As the number of pads is known in advance, the pads array is not allocated 656 * dynamically but is managed by the entity driver. Most drivers will embed the 657 * pads array in a driver-specific structure, avoiding dynamic allocation. 658 * 659 * Drivers must set the direction of every pad in the pads array before calling 660 * media_entity_pads_init(). The function will initialize the other pads fields. 661 */ 662 int media_entity_pads_init(struct media_entity *entity, u16 num_pads, 663 struct media_pad *pads); 664 665 /** 666 * media_entity_cleanup() - free resources associated with an entity 667 * 668 * @entity: entity where the pads belong 669 * 670 * This function must be called during the cleanup phase after unregistering 671 * the entity (currently, it does nothing). 672 * 673 * Calling media_entity_cleanup() on a media_entity whose memory has been 674 * zeroed but that has not been initialized with media_entity_pad_init() is 675 * valid and is a no-op. 676 */ 677 #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER) 678 static inline void media_entity_cleanup(struct media_entity *entity) {} 679 #else 680 #define media_entity_cleanup(entity) do { } while (false) 681 #endif 682 683 /** 684 * media_get_pad_index() - retrieves a pad index from an entity 685 * 686 * @entity: entity where the pads belong 687 * @is_sink: true if the pad is a sink, false if it is a source 688 * @sig_type: type of signal of the pad to be search 689 * 690 * This helper function finds the first pad index inside an entity that 691 * satisfies both @is_sink and @sig_type conditions. 692 * 693 * Return: 694 * 695 * On success, return the pad number. If the pad was not found or the media 696 * entity is a NULL pointer, return -EINVAL. 697 */ 698 int media_get_pad_index(struct media_entity *entity, bool is_sink, 699 enum media_pad_signal_type sig_type); 700 701 /** 702 * media_create_pad_link() - creates a link between two entities. 703 * 704 * @source: pointer to &media_entity of the source pad. 705 * @source_pad: number of the source pad in the pads array 706 * @sink: pointer to &media_entity of the sink pad. 707 * @sink_pad: number of the sink pad in the pads array. 708 * @flags: Link flags, as defined in 709 * :ref:`include/uapi/linux/media.h <media_header>` 710 * ( seek for ``MEDIA_LNK_FL_*``) 711 * 712 * Valid values for flags: 713 * 714 * %MEDIA_LNK_FL_ENABLED 715 * Indicates that the link is enabled and can be used to transfer media data. 716 * When two or more links target a sink pad, only one of them can be 717 * enabled at a time. 718 * 719 * %MEDIA_LNK_FL_IMMUTABLE 720 * Indicates that the link enabled state can't be modified at runtime. If 721 * %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be 722 * set, since an immutable link is always enabled. 723 * 724 * .. note:: 725 * 726 * Before calling this function, media_entity_pads_init() and 727 * media_device_register_entity() should be called previously for both ends. 728 */ 729 __must_check int media_create_pad_link(struct media_entity *source, 730 u16 source_pad, struct media_entity *sink, 731 u16 sink_pad, u32 flags); 732 733 /** 734 * media_create_pad_links() - creates a link between two entities. 735 * 736 * @mdev: Pointer to the media_device that contains the object 737 * @source_function: Function of the source entities. Used only if @source is 738 * NULL. 739 * @source: pointer to &media_entity of the source pad. If NULL, it will use 740 * all entities that matches the @sink_function. 741 * @source_pad: number of the source pad in the pads array 742 * @sink_function: Function of the sink entities. Used only if @sink is NULL. 743 * @sink: pointer to &media_entity of the sink pad. If NULL, it will use 744 * all entities that matches the @sink_function. 745 * @sink_pad: number of the sink pad in the pads array. 746 * @flags: Link flags, as defined in include/uapi/linux/media.h. 747 * @allow_both_undefined: if %true, then both @source and @sink can be NULL. 748 * In such case, it will create a crossbar between all entities that 749 * matches @source_function to all entities that matches @sink_function. 750 * If %false, it will return 0 and won't create any link if both @source 751 * and @sink are NULL. 752 * 753 * Valid values for flags: 754 * 755 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be 756 * used to transfer media data. If multiple links are created and this 757 * flag is passed as an argument, only the first created link will have 758 * this flag. 759 * 760 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't 761 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then 762 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is 763 * always enabled. 764 * 765 * It is common for some devices to have multiple source and/or sink entities 766 * of the same type that should be linked. While media_create_pad_link() 767 * creates link by link, this function is meant to allow 1:n, n:1 and even 768 * cross-bar (n:n) links. 769 * 770 * .. note:: 771 * 772 * Before calling this function, media_entity_pads_init() and 773 * media_device_register_entity() should be called previously for the 774 * entities to be linked. 775 */ 776 int media_create_pad_links(const struct media_device *mdev, 777 const u32 source_function, 778 struct media_entity *source, 779 const u16 source_pad, 780 const u32 sink_function, 781 struct media_entity *sink, 782 const u16 sink_pad, 783 u32 flags, 784 const bool allow_both_undefined); 785 786 void __media_entity_remove_links(struct media_entity *entity); 787 788 /** 789 * media_entity_remove_links() - remove all links associated with an entity 790 * 791 * @entity: pointer to &media_entity 792 * 793 * .. note:: 794 * 795 * This is called automatically when an entity is unregistered via 796 * media_device_register_entity(). 797 */ 798 void media_entity_remove_links(struct media_entity *entity); 799 800 /** 801 * __media_entity_setup_link - Configure a media link without locking 802 * @link: The link being configured 803 * @flags: Link configuration flags 804 * 805 * The bulk of link setup is handled by the two entities connected through the 806 * link. This function notifies both entities of the link configuration change. 807 * 808 * If the link is immutable or if the current and new configuration are 809 * identical, return immediately. 810 * 811 * The user is expected to hold link->source->parent->mutex. If not, 812 * media_entity_setup_link() should be used instead. 813 */ 814 int __media_entity_setup_link(struct media_link *link, u32 flags); 815 816 /** 817 * media_entity_setup_link() - changes the link flags properties in runtime 818 * 819 * @link: pointer to &media_link 820 * @flags: the requested new link flags 821 * 822 * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag 823 * to enable/disable a link. Links marked with the 824 * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled. 825 * 826 * When a link is enabled or disabled, the media framework calls the 827 * link_setup operation for the two entities at the source and sink of the 828 * link, in that order. If the second link_setup call fails, another 829 * link_setup call is made on the first entity to restore the original link 830 * flags. 831 * 832 * Media device drivers can be notified of link setup operations by setting the 833 * &media_device.link_notify pointer to a callback function. If provided, the 834 * notification callback will be called before enabling and after disabling 835 * links. 836 * 837 * Entity drivers must implement the link_setup operation if any of their links 838 * is non-immutable. The operation must either configure the hardware or store 839 * the configuration information to be applied later. 840 * 841 * Link configuration must not have any side effect on other links. If an 842 * enabled link at a sink pad prevents another link at the same pad from 843 * being enabled, the link_setup operation must return %-EBUSY and can't 844 * implicitly disable the first enabled link. 845 * 846 * .. note:: 847 * 848 * The valid values of the flags for the link is the same as described 849 * on media_create_pad_link(), for pad to pad links or the same as described 850 * on media_create_intf_link(), for interface to entity links. 851 */ 852 int media_entity_setup_link(struct media_link *link, u32 flags); 853 854 /** 855 * media_entity_find_link - Find a link between two pads 856 * @source: Source pad 857 * @sink: Sink pad 858 * 859 * Return: returns a pointer to the link between the two entities. If no 860 * such link exists, return %NULL. 861 */ 862 struct media_link *media_entity_find_link(struct media_pad *source, 863 struct media_pad *sink); 864 865 /** 866 * media_pad_remote_pad_first - Find the first pad at the remote end of a link 867 * @pad: Pad at the local end of the link 868 * 869 * Search for a remote pad connected to the given pad by iterating over all 870 * links originating or terminating at that pad until an enabled link is found. 871 * 872 * Return: returns a pointer to the pad at the remote end of the first found 873 * enabled link, or %NULL if no enabled link has been found. 874 */ 875 struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad); 876 877 /** 878 * media_pad_remote_pad_unique - Find a remote pad connected to a pad 879 * @pad: The pad 880 * 881 * Search for and return a remote pad connected to @pad through an enabled 882 * link. If multiple (or no) remote pads are found, an error is returned. 883 * 884 * The uniqueness constraint makes this helper function suitable for entities 885 * that support a single active source at a time on a given pad. 886 * 887 * Return: A pointer to the remote pad, or one of the following error pointers 888 * if an error occurs: 889 * 890 * * -ENOTUNIQ - Multiple links are enabled 891 * * -ENOLINK - No connected pad found 892 */ 893 struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad); 894 895 /** 896 * media_entity_remote_pad_unique - Find a remote pad connected to an entity 897 * @entity: The entity 898 * @type: The type of pad to find (MEDIA_PAD_FL_SINK or MEDIA_PAD_FL_SOURCE) 899 * 900 * Search for and return a remote pad of @type connected to @entity through an 901 * enabled link. If multiple (or no) remote pads match these criteria, an error 902 * is returned. 903 * 904 * The uniqueness constraint makes this helper function suitable for entities 905 * that support a single active source or sink at a time. 906 * 907 * Return: A pointer to the remote pad, or one of the following error pointers 908 * if an error occurs: 909 * 910 * * -ENOTUNIQ - Multiple links are enabled 911 * * -ENOLINK - No connected pad found 912 */ 913 struct media_pad * 914 media_entity_remote_pad_unique(const struct media_entity *entity, 915 unsigned int type); 916 917 /** 918 * media_entity_remote_source_pad_unique - Find a remote source pad connected to 919 * an entity 920 * @entity: The entity 921 * 922 * Search for and return a remote source pad connected to @entity through an 923 * enabled link. If multiple (or no) remote pads match these criteria, an error 924 * is returned. 925 * 926 * The uniqueness constraint makes this helper function suitable for entities 927 * that support a single active source at a time. 928 * 929 * Return: A pointer to the remote pad, or one of the following error pointers 930 * if an error occurs: 931 * 932 * * -ENOTUNIQ - Multiple links are enabled 933 * * -ENOLINK - No connected pad found 934 */ 935 static inline struct media_pad * 936 media_entity_remote_source_pad_unique(const struct media_entity *entity) 937 { 938 return media_entity_remote_pad_unique(entity, MEDIA_PAD_FL_SOURCE); 939 } 940 941 /** 942 * media_entity_is_streaming - Test if an entity is part of a streaming pipeline 943 * @entity: The entity 944 * 945 * Return: True if the entity is part of a pipeline started with the 946 * media_pipeline_start() function, false otherwise. 947 */ 948 static inline bool media_entity_is_streaming(const struct media_entity *entity) 949 { 950 return entity->pipe; 951 } 952 953 /** 954 * media_entity_pipeline - Get the media pipeline an entity is part of 955 * @entity: The entity 956 * 957 * This function returns the media pipeline that an entity has been associated 958 * with when constructing the pipeline with media_pipeline_start(). The pointer 959 * remains valid until media_pipeline_stop() is called. 960 * 961 * In general, entities can be part of multiple pipelines, when carrying 962 * multiple streams (either on different pads, or on the same pad using 963 * multiplexed streams). This function is to be used only for entities that 964 * do not support multiple pipelines. 965 * 966 * Return: The media_pipeline the entity is part of, or NULL if the entity is 967 * not part of any pipeline. 968 */ 969 struct media_pipeline *media_entity_pipeline(struct media_entity *entity); 970 971 /** 972 * media_entity_get_fwnode_pad - Get pad number from fwnode 973 * 974 * @entity: The entity 975 * @fwnode: Pointer to the fwnode_handle which should be used to find the pad 976 * @direction_flags: Expected direction of the pad, as defined in 977 * :ref:`include/uapi/linux/media.h <media_header>` 978 * (seek for ``MEDIA_PAD_FL_*``) 979 * 980 * This function can be used to resolve the media pad number from 981 * a fwnode. This is useful for devices which use more complex 982 * mappings of media pads. 983 * 984 * If the entity does not implement the get_fwnode_pad() operation 985 * then this function searches the entity for the first pad that 986 * matches the @direction_flags. 987 * 988 * Return: returns the pad number on success or a negative error code. 989 */ 990 int media_entity_get_fwnode_pad(struct media_entity *entity, 991 struct fwnode_handle *fwnode, 992 unsigned long direction_flags); 993 994 /** 995 * media_graph_walk_init - Allocate resources used by graph walk. 996 * 997 * @graph: Media graph structure that will be used to walk the graph 998 * @mdev: Pointer to the &media_device that contains the object 999 * 1000 * The caller is required to hold the media_device graph_mutex during the graph 1001 * walk until the graph state is released. 1002 * 1003 * Returns zero on success or a negative error code otherwise. 1004 */ 1005 __must_check int media_graph_walk_init( 1006 struct media_graph *graph, struct media_device *mdev); 1007 1008 /** 1009 * media_graph_walk_cleanup - Release resources used by graph walk. 1010 * 1011 * @graph: Media graph structure that will be used to walk the graph 1012 */ 1013 void media_graph_walk_cleanup(struct media_graph *graph); 1014 1015 /** 1016 * media_graph_walk_start - Start walking the media graph at a 1017 * given entity 1018 * 1019 * @graph: Media graph structure that will be used to walk the graph 1020 * @entity: Starting entity 1021 * 1022 * Before using this function, media_graph_walk_init() must be 1023 * used to allocate resources used for walking the graph. This 1024 * function initializes the graph traversal structure to walk the 1025 * entities graph starting at the given entity. The traversal 1026 * structure must not be modified by the caller during graph 1027 * traversal. After the graph walk, the resources must be released 1028 * using media_graph_walk_cleanup(). 1029 */ 1030 void media_graph_walk_start(struct media_graph *graph, 1031 struct media_entity *entity); 1032 1033 /** 1034 * media_graph_walk_next - Get the next entity in the graph 1035 * @graph: Media graph structure 1036 * 1037 * Perform a depth-first traversal of the given media entities graph. 1038 * 1039 * The graph structure must have been previously initialized with a call to 1040 * media_graph_walk_start(). 1041 * 1042 * Return: returns the next entity in the graph or %NULL if the whole graph 1043 * have been traversed. 1044 */ 1045 struct media_entity *media_graph_walk_next(struct media_graph *graph); 1046 1047 /** 1048 * media_pipeline_start - Mark a pipeline as streaming 1049 * @entity: Starting entity 1050 * @pipe: Media pipeline to be assigned to all entities in the pipeline. 1051 * 1052 * Mark all entities connected to a given entity through enabled links, either 1053 * directly or indirectly, as streaming. The given pipeline object is assigned 1054 * to every entity in the pipeline and stored in the media_entity pipe field. 1055 * 1056 * Calls to this function can be nested, in which case the same number of 1057 * media_pipeline_stop() calls will be required to stop streaming. The 1058 * pipeline pointer must be identical for all nested calls to 1059 * media_pipeline_start(). 1060 */ 1061 __must_check int media_pipeline_start(struct media_entity *entity, 1062 struct media_pipeline *pipe); 1063 /** 1064 * __media_pipeline_start - Mark a pipeline as streaming 1065 * 1066 * @entity: Starting entity 1067 * @pipe: Media pipeline to be assigned to all entities in the pipeline. 1068 * 1069 * ..note:: This is the non-locking version of media_pipeline_start() 1070 */ 1071 __must_check int __media_pipeline_start(struct media_entity *entity, 1072 struct media_pipeline *pipe); 1073 1074 /** 1075 * media_pipeline_stop - Mark a pipeline as not streaming 1076 * @entity: Starting entity 1077 * 1078 * Mark all entities connected to a given entity through enabled links, either 1079 * directly or indirectly, as not streaming. The media_entity pipe field is 1080 * reset to %NULL. 1081 * 1082 * If multiple calls to media_pipeline_start() have been made, the same 1083 * number of calls to this function are required to mark the pipeline as not 1084 * streaming. 1085 */ 1086 void media_pipeline_stop(struct media_entity *entity); 1087 1088 /** 1089 * __media_pipeline_stop - Mark a pipeline as not streaming 1090 * 1091 * @entity: Starting entity 1092 * 1093 * .. note:: This is the non-locking version of media_pipeline_stop() 1094 */ 1095 void __media_pipeline_stop(struct media_entity *entity); 1096 1097 /** 1098 * media_pipeline_alloc_start - Mark a pipeline as streaming 1099 * @entity: Starting entity 1100 * 1101 * media_pipeline_alloc_start() is similar to media_pipeline_start() but instead 1102 * of working on a given pipeline the function will use an existing pipeline if 1103 * the entity is already part of a pipeline, or allocate a new pipeline. 1104 * 1105 * Calls to media_pipeline_alloc_start() must be matched with 1106 * media_pipeline_stop(). 1107 */ 1108 __must_check int media_pipeline_alloc_start(struct media_entity *entity); 1109 1110 /** 1111 * media_devnode_create() - creates and initializes a device node interface 1112 * 1113 * @mdev: pointer to struct &media_device 1114 * @type: type of the interface, as given by 1115 * :ref:`include/uapi/linux/media.h <media_header>` 1116 * ( seek for ``MEDIA_INTF_T_*``) macros. 1117 * @flags: Interface flags, as defined in 1118 * :ref:`include/uapi/linux/media.h <media_header>` 1119 * ( seek for ``MEDIA_INTF_FL_*``) 1120 * @major: Device node major number. 1121 * @minor: Device node minor number. 1122 * 1123 * Return: if succeeded, returns a pointer to the newly allocated 1124 * &media_intf_devnode pointer. 1125 * 1126 * .. note:: 1127 * 1128 * Currently, no flags for &media_interface is defined. 1129 */ 1130 struct media_intf_devnode * 1131 __must_check media_devnode_create(struct media_device *mdev, 1132 u32 type, u32 flags, 1133 u32 major, u32 minor); 1134 /** 1135 * media_devnode_remove() - removes a device node interface 1136 * 1137 * @devnode: pointer to &media_intf_devnode to be freed. 1138 * 1139 * When a device node interface is removed, all links to it are automatically 1140 * removed. 1141 */ 1142 void media_devnode_remove(struct media_intf_devnode *devnode); 1143 1144 /** 1145 * media_create_intf_link() - creates a link between an entity and an interface 1146 * 1147 * @entity: pointer to %media_entity 1148 * @intf: pointer to %media_interface 1149 * @flags: Link flags, as defined in 1150 * :ref:`include/uapi/linux/media.h <media_header>` 1151 * ( seek for ``MEDIA_LNK_FL_*``) 1152 * 1153 * 1154 * Valid values for flags: 1155 * 1156 * %MEDIA_LNK_FL_ENABLED 1157 * Indicates that the interface is connected to the entity hardware. 1158 * That's the default value for interfaces. An interface may be disabled if 1159 * the hardware is busy due to the usage of some other interface that it is 1160 * currently controlling the hardware. 1161 * 1162 * A typical example is an hybrid TV device that handle only one type of 1163 * stream on a given time. So, when the digital TV is streaming, 1164 * the V4L2 interfaces won't be enabled, as such device is not able to 1165 * also stream analog TV or radio. 1166 * 1167 * .. note:: 1168 * 1169 * Before calling this function, media_devnode_create() should be called for 1170 * the interface and media_device_register_entity() should be called for the 1171 * interface that will be part of the link. 1172 */ 1173 struct media_link * 1174 __must_check media_create_intf_link(struct media_entity *entity, 1175 struct media_interface *intf, 1176 u32 flags); 1177 /** 1178 * __media_remove_intf_link() - remove a single interface link 1179 * 1180 * @link: pointer to &media_link. 1181 * 1182 * .. note:: This is an unlocked version of media_remove_intf_link() 1183 */ 1184 void __media_remove_intf_link(struct media_link *link); 1185 1186 /** 1187 * media_remove_intf_link() - remove a single interface link 1188 * 1189 * @link: pointer to &media_link. 1190 * 1191 * .. note:: Prefer to use this one, instead of __media_remove_intf_link() 1192 */ 1193 void media_remove_intf_link(struct media_link *link); 1194 1195 /** 1196 * __media_remove_intf_links() - remove all links associated with an interface 1197 * 1198 * @intf: pointer to &media_interface 1199 * 1200 * .. note:: This is an unlocked version of media_remove_intf_links(). 1201 */ 1202 void __media_remove_intf_links(struct media_interface *intf); 1203 1204 /** 1205 * media_remove_intf_links() - remove all links associated with an interface 1206 * 1207 * @intf: pointer to &media_interface 1208 * 1209 * .. note:: 1210 * 1211 * #) This is called automatically when an entity is unregistered via 1212 * media_device_register_entity() and by media_devnode_remove(). 1213 * 1214 * #) Prefer to use this one, instead of __media_remove_intf_links(). 1215 */ 1216 void media_remove_intf_links(struct media_interface *intf); 1217 1218 /** 1219 * media_entity_call - Calls a struct media_entity_operations operation on 1220 * an entity 1221 * 1222 * @entity: entity where the @operation will be called 1223 * @operation: type of the operation. Should be the name of a member of 1224 * struct &media_entity_operations. 1225 * 1226 * This helper function will check if @operation is not %NULL. On such case, 1227 * it will issue a call to @operation\(@entity, @args\). 1228 */ 1229 1230 #define media_entity_call(entity, operation, args...) \ 1231 (((entity)->ops && (entity)->ops->operation) ? \ 1232 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD) 1233 1234 /** 1235 * media_create_ancillary_link() - create an ancillary link between two 1236 * instances of &media_entity 1237 * 1238 * @primary: pointer to the primary &media_entity 1239 * @ancillary: pointer to the ancillary &media_entity 1240 * 1241 * Create an ancillary link between two entities, indicating that they 1242 * represent two connected pieces of hardware that form a single logical unit. 1243 * A typical example is a camera lens controller being linked to the sensor that 1244 * it is supporting. 1245 * 1246 * The function sets both MEDIA_LNK_FL_ENABLED and MEDIA_LNK_FL_IMMUTABLE for 1247 * the new link. 1248 */ 1249 struct media_link * 1250 media_create_ancillary_link(struct media_entity *primary, 1251 struct media_entity *ancillary); 1252 1253 /** 1254 * __media_entity_next_link() - Iterate through a &media_entity's links 1255 * 1256 * @entity: pointer to the &media_entity 1257 * @link: pointer to a &media_link to hold the iterated values 1258 * @link_type: one of the MEDIA_LNK_FL_LINK_TYPE flags 1259 * 1260 * Return the next link against an entity matching a specific link type. This 1261 * allows iteration through an entity's links whilst guaranteeing all of the 1262 * returned links are of the given type. 1263 */ 1264 struct media_link *__media_entity_next_link(struct media_entity *entity, 1265 struct media_link *link, 1266 unsigned long link_type); 1267 1268 /** 1269 * for_each_media_entity_data_link() - Iterate through an entity's data links 1270 * 1271 * @entity: pointer to the &media_entity 1272 * @link: pointer to a &media_link to hold the iterated values 1273 * 1274 * Iterate over a &media_entity's data links 1275 */ 1276 #define for_each_media_entity_data_link(entity, link) \ 1277 for (link = __media_entity_next_link(entity, NULL, \ 1278 MEDIA_LNK_FL_DATA_LINK); \ 1279 link; \ 1280 link = __media_entity_next_link(entity, link, \ 1281 MEDIA_LNK_FL_DATA_LINK)) 1282 1283 #endif 1284