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