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