Merge remote-tracking branch 'asoc/topic/rt5645' into asoc-next
[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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifndef _MEDIA_ENTITY_H
24#define _MEDIA_ENTITY_H
25
c8d54cd5 26#include <linux/bitmap.h>
0149a2e1 27#include <linux/kernel.h>
53e269c1 28#include <linux/list.h>
1651333b 29#include <linux/media.h>
53e269c1 30
ec6e4c95
MCC
31/* Enums used internally at the media controller to represent graphs */
32
33/**
34 * enum media_gobj_type - type of a graph object
35 *
bfab2aac 36 * @MEDIA_GRAPH_ENTITY: Identify a media entity
18710dc6 37 * @MEDIA_GRAPH_PAD: Identify a media pad
6b6a4278 38 * @MEDIA_GRAPH_LINK: Identify a media link
27e543fa
MCC
39 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
40 * a device node
ec6e4c95
MCC
41 */
42enum media_gobj_type {
bfab2aac 43 MEDIA_GRAPH_ENTITY,
18710dc6 44 MEDIA_GRAPH_PAD,
6b6a4278 45 MEDIA_GRAPH_LINK,
27e543fa 46 MEDIA_GRAPH_INTF_DEVNODE,
ec6e4c95
MCC
47};
48
49#define MEDIA_BITS_PER_TYPE 8
05b3b77c
MCC
50#define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE)
51#define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)
ec6e4c95
MCC
52
53/* Structs to represent the objects that belong to a media graph */
54
55/**
56 * struct media_gobj - Define a graph object.
57 *
c358e80d 58 * @mdev: Pointer to the struct media_device that owns the object
ec6e4c95
MCC
59 * @id: Non-zero object ID identifier. The ID should be unique
60 * inside a media_device, as it is composed by
05b3b77c
MCC
61 * %MEDIA_BITS_PER_TYPE to store the type plus
62 * %MEDIA_BITS_PER_ID to store the ID
c358e80d 63 * @list: List entry stored in one of the per-type mdev object lists
ec6e4c95
MCC
64 *
65 * All objects on the media graph should have this struct embedded
66 */
67struct media_gobj {
39a956c4 68 struct media_device *mdev;
ec6e4c95 69 u32 id;
05bfa9fa 70 struct list_head list;
ec6e4c95
MCC
71};
72
c8d54cd5 73#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
c8d54cd5 74
c8d54cd5
SA
75/**
76 * struct media_entity_enum - An enumeration of media entities.
77 *
c8d54cd5
SA
78 * @bmap: Bit map in which each bit represents one entity at struct
79 * media_entity->internal_idx.
80 * @idx_max: Number of bits in bmap
81 */
82struct media_entity_enum {
c8d54cd5
SA
83 unsigned long *bmap;
84 int idx_max;
85};
86
434257f1
SA
87/**
88 * struct media_entity_graph - Media graph traversal state
89 *
90 * @stack: Graph traversal stack; the stack contains information
91 * on the path the media entities to be walked and the
92 * links through which they were reached.
29d8da02 93 * @ent_enum: Visited entities
434257f1
SA
94 * @top: The top of the stack
95 */
82c68290
SA
96struct media_entity_graph {
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
5dd8775d
SA
106/*
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;
5dd8775d 114 struct media_entity_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.
131 * @source: Part of a union. Used only if the second object (gobj1) is
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
164 * @flags: Pad flags, as defined in uapi/media.h (MEDIA_PAD_FL_*)
165 */
53e269c1 166struct media_pad {
4b8a3c08 167 struct media_gobj graph_obj; /* must be first field in struct */
c358e80d
MCC
168 struct media_entity *entity;
169 u16 index;
170 unsigned long flags;
53e269c1
LP
171};
172
5a5394be
LP
173/**
174 * struct media_entity_operations - Media entity operations
175 * @link_setup: Notify the entity of link changes. The operation can
176 * return an error, in which case link setup will be
177 * cancelled. Optional.
178 * @link_validate: Return whether a link is valid from the entity point of
179 * view. The media_entity_pipeline_start() function
180 * validates all links by calling this operation. Optional.
181 */
97548ed4
LP
182struct media_entity_operations {
183 int (*link_setup)(struct media_entity *entity,
184 const struct media_pad *local,
185 const struct media_pad *remote, u32 flags);
af88be38 186 int (*link_validate)(struct media_link *link);
97548ed4
LP
187};
188
c358e80d
MCC
189/**
190 * struct media_entity - A media entity graph object.
191 *
192 * @graph_obj: Embedded structure containing the media object common data.
193 * @name: Entity name.
0e576b76
MCC
194 * @function: Entity main function, as defined in uapi/media.h
195 * (MEDIA_ENT_F_*)
c358e80d 196 * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
c358e80d
MCC
197 * @num_pads: Number of sink and source pads.
198 * @num_links: Total number of links, forward and back, enabled and disabled.
199 * @num_backlinks: Number of backlinks
665faa97
SA
200 * @internal_idx: An unique internal entity specific number. The numbers are
201 * re-used if entities are unregistered or registered again.
c358e80d
MCC
202 * @pads: Pads array with the size defined by @num_pads.
203 * @links: List of data links.
204 * @ops: Entity operations.
205 * @stream_count: Stream count for the entity.
206 * @use_count: Use count for the entity.
207 * @pipe: Pipeline this entity belongs to.
208 * @info: Union with devnode information. Kept just for backward
209 * compatibility.
210 * @major: Devnode major number (zero if not applicable). Kept just
211 * for backward compatibility.
212 * @minor: Devnode minor number (zero if not applicable). Kept just
213 * for backward compatibility.
214 *
215 * NOTE: @stream_count and @use_count reference counts must never be
216 * negative, but are signed integers on purpose: a simple WARN_ON(<0) check
217 * can be used to detect reference count bugs that would make them negative.
218 */
53e269c1 219struct media_entity {
4b8a3c08 220 struct media_gobj graph_obj; /* must be first field in struct */
c358e80d 221 const char *name;
0e576b76 222 u32 function;
c358e80d 223 unsigned long flags;
53e269c1 224
c358e80d
MCC
225 u16 num_pads;
226 u16 num_links;
227 u16 num_backlinks;
665faa97 228 int internal_idx;
53e269c1 229
c358e80d
MCC
230 struct media_pad *pads;
231 struct list_head links;
53e269c1 232
c358e80d 233 const struct media_entity_operations *ops;
97548ed4 234
503c3d82
LP
235 /* Reference counts must never be negative, but are signed integers on
236 * purpose: a simple WARN_ON(<0) check can be used to detect reference
237 * count bugs that would make them negative.
238 */
c358e80d
MCC
239 int stream_count;
240 int use_count;
503c3d82 241
c358e80d 242 struct media_pipeline *pipe;
e02188c9 243
53e269c1 244 union {
53e269c1
LP
245 struct {
246 u32 major;
247 u32 minor;
e31a0ba7 248 } dev;
fa5034c6 249 } info;
53e269c1
LP
250};
251
27e543fa 252/**
c358e80d 253 * struct media_interface - A media interface graph object.
27e543fa
MCC
254 *
255 * @graph_obj: embedded graph object
86e26620 256 * @links: List of links pointing to graph entities
c358e80d 257 * @type: Type of the interface as defined in the
27e543fa
MCC
258 * uapi/media/media.h header, e. g.
259 * MEDIA_INTF_T_*
c358e80d 260 * @flags: Interface flags as defined in uapi/media/media.h
27e543fa
MCC
261 */
262struct media_interface {
263 struct media_gobj graph_obj;
86e26620 264 struct list_head links;
27e543fa
MCC
265 u32 type;
266 u32 flags;
267};
268
269/**
c358e80d 270 * struct media_intf_devnode - A media interface via a device node.
27e543fa
MCC
271 *
272 * @intf: embedded interface object
273 * @major: Major number of a device node
274 * @minor: Minor number of a device node
275 */
276struct media_intf_devnode {
277 struct media_interface intf;
c398bb64
MCC
278
279 /* Should match the fields at media_v2_intf_devnode */
27e543fa
MCC
280 u32 major;
281 u32 minor;
282};
283
60266185
MCC
284/**
285 * media_entity_id() - return the media entity graph object id
286 *
287 * @entity: pointer to entity
288 */
fa762394
MCC
289static inline u32 media_entity_id(struct media_entity *entity)
290{
bfab2aac 291 return entity->graph_obj.id;
fa762394
MCC
292}
293
60266185
MCC
294/**
295 * media_type() - return the media object type
296 *
297 * @gobj: pointer to the media graph object
298 */
ec6e4c95
MCC
299static inline enum media_gobj_type media_type(struct media_gobj *gobj)
300{
05b3b77c 301 return gobj->id >> MEDIA_BITS_PER_ID;
ec6e4c95
MCC
302}
303
630c0e80
MCC
304/**
305 * media_id() - return the media object ID
306 *
307 * @gobj: pointer to the media graph object
308 */
05b3b77c 309static inline u32 media_id(struct media_gobj *gobj)
ec6e4c95 310{
05b3b77c 311 return gobj->id & MEDIA_ID_MASK;
ec6e4c95
MCC
312}
313
630c0e80
MCC
314/**
315 * media_gobj_gen_id() - encapsulates type and ID on at the object ID
316 *
317 * @type: object type as define at enum &media_gobj_type.
318 * @local_id: next ID, from struct &media_device.@id.
319 */
05b3b77c 320static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
ec6e4c95
MCC
321{
322 u32 id;
323
05b3b77c
MCC
324 id = type << MEDIA_BITS_PER_ID;
325 id |= local_id & MEDIA_ID_MASK;
ec6e4c95
MCC
326
327 return id;
328}
329
60266185
MCC
330/**
331 * is_media_entity_v4l2_io() - identify if the entity main function
332 * is a V4L2 I/O
333 *
334 * @entity: pointer to entity
335 *
336 * Return: true if the entity main function is one of the V4L2 I/O types
337 * (video, VBI or SDR radio); false otherwise.
338 */
fa17b46a
MCC
339static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
340{
341 if (!entity)
342 return false;
343
0e576b76 344 switch (entity->function) {
4ca72efa
MCC
345 case MEDIA_ENT_F_IO_V4L:
346 case MEDIA_ENT_F_IO_VBI:
347 case MEDIA_ENT_F_IO_SWRADIO:
fa17b46a
MCC
348 return true;
349 default:
350 return false;
351 }
352}
353
60266185
MCC
354/**
355 * is_media_entity_v4l2_subdev - return true if the entity main function is
356 * associated with the V4L2 API subdev usage
357 *
358 * @entity: pointer to entity
359 *
360 * This is an ancillary function used by subdev-based V4L2 drivers.
361 * It checks if the entity function is one of functions used by a V4L2 subdev,
362 * e. g. camera-relatef functions, analog TV decoder, TV tuner, V4L2 DSPs.
363 */
fa17b46a
MCC
364static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
365{
366 if (!entity)
367 return false;
368
0e576b76 369 switch (entity->function) {
4ca72efa
MCC
370 case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
371 case MEDIA_ENT_F_CAM_SENSOR:
372 case MEDIA_ENT_F_FLASH:
373 case MEDIA_ENT_F_LENS:
374 case MEDIA_ENT_F_ATV_DECODER:
375 case MEDIA_ENT_F_TUNER:
fa17b46a
MCC
376 return true;
377
378 default:
379 return false;
380 }
381}
382
92777994
MCC
383/**
384 * __media_entity_enum_init - Initialise an entity enumeration
385 *
386 * @ent_enum: Entity enumeration to be initialised
387 * @idx_max: Maximum number of entities in the enumeration
388 *
389 * Return: Returns zero on success or a negative error code.
390 */
c8d54cd5
SA
391__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
392 int idx_max);
92777994
MCC
393
394/**
395 * media_entity_enum_cleanup - Release resources of an entity enumeration
396 *
397 * @ent_enum: Entity enumeration to be released
398 */
399void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
a5ccc48a 400
c8d54cd5
SA
401/**
402 * media_entity_enum_zero - Clear the entire enum
403 *
03e49338 404 * @ent_enum: Entity enumeration to be cleared
ef69ee1b 405 */
c8d54cd5
SA
406static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
407{
408 bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
409}
410
411/**
412 * media_entity_enum_set - Mark a single entity in the enum
413 *
03e49338 414 * @ent_enum: Entity enumeration
c8d54cd5
SA
415 * @entity: Entity to be marked
416 */
417static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
418 struct media_entity *entity)
419{
420 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
421 return;
422
423 __set_bit(entity->internal_idx, ent_enum->bmap);
424}
425
426/**
427 * media_entity_enum_clear - Unmark a single entity in the enum
428 *
03e49338 429 * @ent_enum: Entity enumeration
c8d54cd5
SA
430 * @entity: Entity to be unmarked
431 */
432static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
433 struct media_entity *entity)
434{
435 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
436 return;
437
438 __clear_bit(entity->internal_idx, ent_enum->bmap);
439}
440
441/**
442 * media_entity_enum_test - Test whether the entity is marked
443 *
03e49338 444 * @ent_enum: Entity enumeration
c8d54cd5
SA
445 * @entity: Entity to be tested
446 *
447 * Returns true if the entity was marked.
448 */
449static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
450 struct media_entity *entity)
451{
452 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
453 return true;
454
455 return test_bit(entity->internal_idx, ent_enum->bmap);
456}
457
458/**
459 * media_entity_enum_test - Test whether the entity is marked, and mark it
460 *
03e49338 461 * @ent_enum: Entity enumeration
c8d54cd5
SA
462 * @entity: Entity to be tested
463 *
464 * Returns true if the entity was marked, and mark it before doing so.
465 */
03e49338
MCC
466static inline bool
467media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,
468 struct media_entity *entity)
c8d54cd5
SA
469{
470 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
471 return true;
472
473 return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
474}
475
476/**
03e49338 477 * media_entity_enum_empty - Test whether the entire enum is empty
c8d54cd5 478 *
03e49338 479 * @ent_enum: Entity enumeration
c8d54cd5
SA
480 *
481 * Returns true if the entity was marked.
482 */
483static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
484{
485 return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
486}
487
488/**
489 * media_entity_enum_intersects - Test whether two enums intersect
490 *
03e49338
MCC
491 * @ent_enum1: First entity enumeration
492 * @ent_enum2: Second entity enumeration
c8d54cd5
SA
493 *
494 * Returns true if entity enumerations e and f intersect, otherwise false.
495 */
496static inline bool media_entity_enum_intersects(
497 struct media_entity_enum *ent_enum1,
498 struct media_entity_enum *ent_enum2)
499{
500 WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
501
502 return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
503 min(ent_enum1->idx_max, ent_enum2->idx_max));
504}
ef69ee1b 505
ec6e4c95
MCC
506#define gobj_to_entity(gobj) \
507 container_of(gobj, struct media_entity, graph_obj)
508
39a956c4
MCC
509#define gobj_to_pad(gobj) \
510 container_of(gobj, struct media_pad, graph_obj)
511
512#define gobj_to_link(gobj) \
513 container_of(gobj, struct media_link, graph_obj)
514
27e543fa
MCC
515#define gobj_to_link(gobj) \
516 container_of(gobj, struct media_link, graph_obj)
517
518#define gobj_to_pad(gobj) \
519 container_of(gobj, struct media_pad, graph_obj)
520
521#define gobj_to_intf(gobj) \
522 container_of(gobj, struct media_interface, graph_obj)
523
524#define intf_to_devnode(intf) \
525 container_of(intf, struct media_intf_devnode, intf)
526
1fc25d30
MCC
527/**
528 * media_gobj_create - Initialize a graph object
529 *
530 * @mdev: Pointer to the media_device that contains the object
531 * @type: Type of the object
532 * @gobj: Pointer to the graph object
533 *
534 * This routine initializes the embedded struct media_gobj inside a
535 * media graph object. It is called automatically if media_*_create()
536 * calls are used. However, if the object (entity, link, pad, interface)
537 * is embedded on some other object, this function should be called before
538 * registering the object at the media controller.
539 */
c350ef83 540void media_gobj_create(struct media_device *mdev,
ec6e4c95
MCC
541 enum media_gobj_type type,
542 struct media_gobj *gobj);
1fc25d30
MCC
543
544/**
545 * media_gobj_destroy - Stop using a graph object on a media device
546 *
547 * @gobj: Pointer to the graph object
548 *
549 * This should be called by all routines like media_device_unregister()
550 * that remove/destroy media graph objects.
551 */
c350ef83 552void media_gobj_destroy(struct media_gobj *gobj);
ec6e4c95 553
db7ee32a
MCC
554/**
555 * media_entity_pads_init() - Initialize the entity pads
556 *
557 * @entity: entity where the pads belong
1fc25d30
MCC
558 * @num_pads: total number of sink and source pads
559 * @pads: Array of @num_pads pads.
560 *
561 * The pads array is managed by the entity driver and passed to
562 * media_entity_pads_init() where its pointer will be stored in the entity
563 * structure.
db7ee32a
MCC
564 *
565 * If no pads are needed, drivers could either directly fill
566 * &media_entity->@num_pads with 0 and &media_entity->@pads with NULL or call
567 * this function that will do the same.
568 *
569 * As the number of pads is known in advance, the pads array is not allocated
570 * dynamically but is managed by the entity driver. Most drivers will embed the
571 * pads array in a driver-specific structure, avoiding dynamic allocation.
572 *
573 * Drivers must set the direction of every pad in the pads array before calling
574 * media_entity_pads_init(). The function will initialize the other pads fields.
575 */
ab22e77c 576int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
57208e5e 577 struct media_pad *pads);
f1fd3289 578
db7ee32a
MCC
579/**
580 * media_entity_cleanup() - free resources associated with an entity
581 *
582 * @entity: entity where the pads belong
583 *
584 * This function must be called during the cleanup phase after unregistering
585 * the entity (currently, it does nothing).
586 */
f1fd3289 587static inline void media_entity_cleanup(struct media_entity *entity) {};
e02188c9 588
db7ee32a
MCC
589/**
590 * media_create_pad_link() - creates a link between two entities.
591 *
592 * @source: pointer to &media_entity of the source pad.
593 * @source_pad: number of the source pad in the pads array
594 * @sink: pointer to &media_entity of the sink pad.
595 * @sink_pad: number of the sink pad in the pads array.
596 * @flags: Link flags, as defined in include/uapi/linux/media.h.
597 *
598 * Valid values for flags:
599 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
600 * used to transfer media data. When two or more links target a sink pad,
601 * only one of them can be enabled at a time.
602 *
603 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
604 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
605 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
606 * always enabled.
607 *
608 * NOTE:
609 *
610 * Before calling this function, media_entity_pads_init() and
611 * media_device_register_entity() should be called previously for both ends.
612 */
77328043
MCC
613__must_check int media_create_pad_link(struct media_entity *source,
614 u16 source_pad, struct media_entity *sink,
615 u16 sink_pad, u32 flags);
b01cc9ce
MCC
616
617/**
618 * media_create_pad_links() - creates a link between two entities.
619 *
620 * @mdev: Pointer to the media_device that contains the object
621 * @source_function: Function of the source entities. Used only if @source is
622 * NULL.
623 * @source: pointer to &media_entity of the source pad. If NULL, it will use
624 * all entities that matches the @sink_function.
625 * @source_pad: number of the source pad in the pads array
626 * @sink_function: Function of the sink entities. Used only if @sink is NULL.
627 * @sink: pointer to &media_entity of the sink pad. If NULL, it will use
628 * all entities that matches the @sink_function.
629 * @sink_pad: number of the sink pad in the pads array.
630 * @flags: Link flags, as defined in include/uapi/linux/media.h.
631 * @allow_both_undefined: if true, then both @source and @sink can be NULL.
632 * In such case, it will create a crossbar between all entities that
633 * matches @source_function to all entities that matches @sink_function.
634 * If false, it will return 0 and won't create any link if both @source
635 * and @sink are NULL.
636 *
637 * Valid values for flags:
638 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
639 * used to transfer media data. If multiple links are created and this
640 * flag is passed as an argument, only the first created link will have
641 * this flag.
642 *
643 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
644 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
645 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
646 * always enabled.
647 *
648 * It is common for some devices to have multiple source and/or sink entities
649 * of the same type that should be linked. While media_create_pad_link()
650 * creates link by link, this function is meant to allow 1:n, n:1 and even
651 * cross-bar (n:n) links.
652 *
653 * NOTE: Before calling this function, media_entity_pads_init() and
654 * media_device_register_entity() should be called previously for the entities
655 * to be linked.
656 */
657int media_create_pad_links(const struct media_device *mdev,
658 const u32 source_function,
659 struct media_entity *source,
660 const u16 source_pad,
661 const u32 sink_function,
662 struct media_entity *sink,
663 const u16 sink_pad,
664 u32 flags,
665 const bool allow_both_undefined);
666
7349cec1 667void __media_entity_remove_links(struct media_entity *entity);
db7ee32a
MCC
668
669/**
670 * media_entity_remove_links() - remove all links associated with an entity
671 *
672 * @entity: pointer to &media_entity
673 *
674 * Note: this is called automatically when an entity is unregistered via
675 * media_device_register_entity().
676 */
7349cec1
SN
677void media_entity_remove_links(struct media_entity *entity);
678
1fc25d30
MCC
679/**
680 * __media_entity_setup_link - Configure a media link without locking
681 * @link: The link being configured
682 * @flags: Link configuration flags
683 *
684 * The bulk of link setup is handled by the two entities connected through the
685 * link. This function notifies both entities of the link configuration change.
686 *
687 * If the link is immutable or if the current and new configuration are
688 * identical, return immediately.
689 *
690 * The user is expected to hold link->source->parent->mutex. If not,
691 * media_entity_setup_link() should be used instead.
692 */
97548ed4 693int __media_entity_setup_link(struct media_link *link, u32 flags);
db7ee32a
MCC
694
695/**
696 * media_entity_setup_link() - changes the link flags properties in runtime
697 *
698 * @link: pointer to &media_link
699 * @flags: the requested new link flags
700 *
701 * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
702 * flag to enable/disable a link. Links marked with the
703 * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
704 *
705 * When a link is enabled or disabled, the media framework calls the
706 * link_setup operation for the two entities at the source and sink of the
707 * link, in that order. If the second link_setup call fails, another
708 * link_setup call is made on the first entity to restore the original link
709 * flags.
710 *
711 * Media device drivers can be notified of link setup operations by setting the
712 * media_device::link_notify pointer to a callback function. If provided, the
713 * notification callback will be called before enabling and after disabling
714 * links.
715 *
716 * Entity drivers must implement the link_setup operation if any of their links
717 * is non-immutable. The operation must either configure the hardware or store
718 * the configuration information to be applied later.
719 *
720 * Link configuration must not have any side effect on other links. If an
721 * enabled link at a sink pad prevents another link at the same pad from
722 * being enabled, the link_setup operation must return -EBUSY and can't
723 * implicitly disable the first enabled link.
724 *
725 * NOTE: the valid values of the flags for the link is the same as described
726 * on media_create_pad_link(), for pad to pad links or the same as described
727 * on media_create_intf_link(), for interface to entity links.
728 */
97548ed4 729int media_entity_setup_link(struct media_link *link, u32 flags);
1fc25d30
MCC
730
731/**
732 * media_entity_find_link - Find a link between two pads
733 * @source: Source pad
734 * @sink: Sink pad
735 *
736 * Return a pointer to the link between the two entities. If no such link
737 * exists, return NULL.
738 */
97548ed4
LP
739struct media_link *media_entity_find_link(struct media_pad *source,
740 struct media_pad *sink);
1fc25d30
MCC
741
742/**
743 * media_entity_remote_pad - Find the pad at the remote end of a link
744 * @pad: Pad at the local end of the link
745 *
746 * Search for a remote pad connected to the given pad by iterating over all
747 * links originating or terminating at that pad until an enabled link is found.
748 *
749 * Return a pointer to the pad at the remote end of the first found enabled
750 * link, or NULL if no enabled link has been found.
751 */
1bddf1b3 752struct media_pad *media_entity_remote_pad(struct media_pad *pad);
53e269c1 753
1fc25d30
MCC
754/**
755 * media_entity_get - Get a reference to the parent module
756 *
757 * @entity: The entity
758 *
759 * Get a reference to the parent media device module.
760 *
761 * The function will return immediately if @entity is NULL.
762 *
763 * Return a pointer to the entity on success or NULL on failure.
764 */
503c3d82 765struct media_entity *media_entity_get(struct media_entity *entity);
1fc25d30 766
e03d2203
SA
767__must_check int media_entity_graph_walk_init(
768 struct media_entity_graph *graph, struct media_device *mdev);
aa360d3d
MCC
769
770/**
771 * media_entity_graph_walk_cleanup - Release resources used by graph walk.
772 *
773 * @graph: Media graph structure that will be used to walk the graph
774 */
e03d2203
SA
775void media_entity_graph_walk_cleanup(struct media_entity_graph *graph);
776
1fc25d30
MCC
777/**
778 * media_entity_put - Release the reference to the parent module
779 *
780 * @entity: The entity
781 *
782 * Release the reference count acquired by media_entity_get().
783 *
784 * The function will return immediately if @entity is NULL.
785 */
503c3d82
LP
786void media_entity_put(struct media_entity *entity);
787
1fc25d30
MCC
788/**
789 * media_entity_graph_walk_start - Start walking the media graph at a given entity
790 * @graph: Media graph structure that will be used to walk the graph
791 * @entity: Starting entity
792 *
e03d2203
SA
793 * Before using this function, media_entity_graph_walk_init() must be
794 * used to allocate resources used for walking the graph. This
795 * function initializes the graph traversal structure to walk the
796 * entities graph starting at the given entity. The traversal
797 * structure must not be modified by the caller during graph
798 * traversal. After the graph walk, the resources must be released
799 * using media_entity_graph_walk_cleanup().
1fc25d30 800 */
a5ccc48a 801void media_entity_graph_walk_start(struct media_entity_graph *graph,
e03d2203 802 struct media_entity *entity);
1fc25d30
MCC
803
804/**
805 * media_entity_graph_walk_next - Get the next entity in the graph
806 * @graph: Media graph structure
807 *
808 * Perform a depth-first traversal of the given media entities graph.
809 *
810 * The graph structure must have been previously initialized with a call to
811 * media_entity_graph_walk_start().
812 *
813 * Return the next entity in the graph or NULL if the whole graph have been
814 * traversed.
815 */
a5ccc48a
SA
816struct media_entity *
817media_entity_graph_walk_next(struct media_entity_graph *graph);
1fc25d30
MCC
818
819/**
820 * media_entity_pipeline_start - Mark a pipeline as streaming
821 * @entity: Starting entity
822 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
823 *
824 * Mark all entities connected to a given entity through enabled links, either
825 * directly or indirectly, as streaming. The given pipeline object is assigned to
826 * every entity in the pipeline and stored in the media_entity pipe field.
827 *
828 * Calls to this function can be nested, in which case the same number of
829 * media_entity_pipeline_stop() calls will be required to stop streaming. The
830 * pipeline pointer must be identical for all nested calls to
831 * media_entity_pipeline_start().
832 */
af88be38
SA
833__must_check int media_entity_pipeline_start(struct media_entity *entity,
834 struct media_pipeline *pipe);
1fc25d30
MCC
835
836/**
837 * media_entity_pipeline_stop - Mark a pipeline as not streaming
838 * @entity: Starting entity
839 *
840 * Mark all entities connected to a given entity through enabled links, either
841 * directly or indirectly, as not streaming. The media_entity pipe field is
842 * reset to NULL.
843 *
844 * If multiple calls to media_entity_pipeline_start() have been made, the same
845 * number of calls to this function are required to mark the pipeline as not
846 * streaming.
847 */
e02188c9 848void media_entity_pipeline_stop(struct media_entity *entity);
a5ccc48a 849
db7ee32a
MCC
850/**
851 * media_devnode_create() - creates and initializes a device node interface
852 *
853 * @mdev: pointer to struct &media_device
854 * @type: type of the interface, as given by MEDIA_INTF_T_* macros
855 * as defined in the uapi/media/media.h header.
856 * @flags: Interface flags as defined in uapi/media/media.h.
857 * @major: Device node major number.
858 * @minor: Device node minor number.
859 *
860 * Return: if succeeded, returns a pointer to the newly allocated
861 * &media_intf_devnode pointer.
862 */
5e5387df
MCC
863struct media_intf_devnode *
864__must_check media_devnode_create(struct media_device *mdev,
865 u32 type, u32 flags,
0b3b72df 866 u32 major, u32 minor);
db7ee32a
MCC
867/**
868 * media_devnode_remove() - removes a device node interface
869 *
870 * @devnode: pointer to &media_intf_devnode to be freed.
871 *
872 * When a device node interface is removed, all links to it are automatically
873 * removed.
874 */
27e543fa 875void media_devnode_remove(struct media_intf_devnode *devnode);
5e5387df 876struct media_link *
db7ee32a
MCC
877
878/**
879 * media_create_intf_link() - creates a link between an entity and an interface
880 *
881 * @entity: pointer to %media_entity
882 * @intf: pointer to %media_interface
883 * @flags: Link flags, as defined in include/uapi/linux/media.h.
884 *
885 *
886 * Valid values for flags:
887 * The %MEDIA_LNK_FL_ENABLED flag indicates that the interface is connected to
888 * the entity hardware. That's the default value for interfaces. An
889 * interface may be disabled if the hardware is busy due to the usage
890 * of some other interface that it is currently controlling the hardware.
891 * A typical example is an hybrid TV device that handle only one type of
892 * stream on a given time. So, when the digital TV is streaming,
893 * the V4L2 interfaces won't be enabled, as such device is not able to
894 * also stream analog TV or radio.
895 *
896 * Note:
897 *
898 * Before calling this function, media_devnode_create() should be called for
899 * the interface and media_device_register_entity() should be called for the
900 * interface that will be part of the link.
901 */
5e5387df
MCC
902__must_check media_create_intf_link(struct media_entity *entity,
903 struct media_interface *intf,
904 u32 flags);
60266185
MCC
905/**
906 * __media_remove_intf_link() - remove a single interface link
907 *
908 * @link: pointer to &media_link.
909 *
910 * Note: this is an unlocked version of media_remove_intf_link()
911 */
d47109fa 912void __media_remove_intf_link(struct media_link *link);
60266185
MCC
913
914/**
915 * media_remove_intf_link() - remove a single interface link
916 *
917 * @link: pointer to &media_link.
918 *
919 * Note: prefer to use this one, instead of __media_remove_intf_link()
920 */
86e26620 921void media_remove_intf_link(struct media_link *link);
60266185
MCC
922
923/**
924 * __media_remove_intf_links() - remove all links associated with an interface
925 *
926 * @intf: pointer to &media_interface
927 *
928 * Note: this is an unlocked version of media_remove_intf_links().
929 */
7c4696a9 930void __media_remove_intf_links(struct media_interface *intf);
92777994 931
db7ee32a
MCC
932/**
933 * media_remove_intf_links() - remove all links associated with an interface
934 *
935 * @intf: pointer to &media_interface
936 *
60266185
MCC
937 * Notes:
938 *
939 * this is called automatically when an entity is unregistered via
db7ee32a 940 * media_device_register_entity() and by media_devnode_remove().
60266185
MCC
941 *
942 * Prefer to use this one, instead of __media_remove_intf_links().
db7ee32a 943 */
7c4696a9
MCC
944void media_remove_intf_links(struct media_interface *intf);
945
97548ed4
LP
946#define media_entity_call(entity, operation, args...) \
947 (((entity)->ops && (entity)->ops->operation) ? \
948 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
949
53e269c1 950#endif