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