blob: a47a7c8a93cfdfbe0c3967813e8f23456600a9ae [file] [log] [blame]
Laurent Pinchart53e269c2009-12-09 08:40:00 -03001/*
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
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020026#include <linux/bitmap.h>
Sakari Ailus0149a2e2013-12-13 08:58:37 -030027#include <linux/kernel.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030028#include <linux/list.h>
Laurent Pinchart16513332009-12-09 08:40:01 -030029#include <linux/media.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030030
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030031/* Enums used internally at the media controller to represent graphs */
32
33/**
34 * enum media_gobj_type - type of a graph object
35 *
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030036 * @MEDIA_GRAPH_ENTITY: Identify a media entity
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030037 * @MEDIA_GRAPH_PAD: Identify a media pad
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030038 * @MEDIA_GRAPH_LINK: Identify a media link
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030039 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
40 * a device node
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030041 */
42enum media_gobj_type {
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030043 MEDIA_GRAPH_ENTITY,
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030044 MEDIA_GRAPH_PAD,
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030045 MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030046 MEDIA_GRAPH_INTF_DEVNODE,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030047};
48
49#define MEDIA_BITS_PER_TYPE 8
50#define MEDIA_BITS_PER_LOCAL_ID (32 - MEDIA_BITS_PER_TYPE)
51#define MEDIA_LOCAL_ID_MASK GENMASK(MEDIA_BITS_PER_LOCAL_ID - 1, 0)
52
53/* Structs to represent the objects that belong to a media graph */
54
55/**
56 * struct media_gobj - Define a graph object.
57 *
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030058 * @mdev: Pointer to the struct media_device that owns the object
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030059 * @id: Non-zero object ID identifier. The ID should be unique
60 * inside a media_device, as it is composed by
61 * MEDIA_BITS_PER_TYPE to store the type plus
62 * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID
63 * (called as "local ID").
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030064 * @list: List entry stored in one of the per-type mdev object lists
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030065 *
66 * All objects on the media graph should have this struct embedded
67 */
68struct media_gobj {
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030069 struct media_device *mdev;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030070 u32 id;
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -030071 struct list_head list;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030072};
73
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020074#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
75#define MEDIA_ENTITY_ENUM_MAX_ID 64
76
77/*
78 * The number of pads can't be bigger than the number of entities,
79 * as the worse-case scenario is to have one entity linked up to
80 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
81 */
82#define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
83
84/**
85 * struct media_entity_enum - An enumeration of media entities.
86 *
87 * @prealloc_bmap: Pre-allocated space reserved for media entities if the
88 * total number of entities does not exceed
89 * MEDIA_ENTITY_ENUM_MAX_ID.
90 * @bmap: Bit map in which each bit represents one entity at struct
91 * media_entity->internal_idx.
92 * @idx_max: Number of bits in bmap
93 */
94struct media_entity_enum {
95 DECLARE_BITMAP(prealloc_bmap, MEDIA_ENTITY_ENUM_MAX_ID);
96 unsigned long *bmap;
97 int idx_max;
98};
99
Sakari Ailus434257f2015-12-16 11:32:20 -0200100/**
101 * struct media_entity_graph - Media graph traversal state
102 *
103 * @stack: Graph traversal stack; the stack contains information
104 * on the path the media entities to be walked and the
105 * links through which they were reached.
Sakari Ailus29d8da02015-12-16 11:32:28 -0200106 * @ent_enum: Visited entities
Sakari Ailus434257f2015-12-16 11:32:20 -0200107 * @top: The top of the stack
108 */
Sakari Ailus82c68292015-12-16 11:32:19 -0200109struct media_entity_graph {
110 struct {
111 struct media_entity *entity;
112 struct list_head *link;
113 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
114
Sakari Ailus29d8da02015-12-16 11:32:28 -0200115 struct media_entity_enum ent_enum;
Sakari Ailus82c68292015-12-16 11:32:19 -0200116 int top;
117};
118
Sakari Ailus5dd87752015-12-16 11:32:21 -0200119/*
120 * struct media_pipeline - Media pipeline related information
121 *
Sakari Ailus74a413302015-12-16 11:32:29 -0200122 * @streaming_count: Streaming start count - streaming stop count
123 * @graph: Media graph walk during pipeline start / stop
Sakari Ailus5dd87752015-12-16 11:32:21 -0200124 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300125struct media_pipeline {
Sakari Ailus74a413302015-12-16 11:32:29 -0200126 int streaming_count;
Sakari Ailus5dd87752015-12-16 11:32:21 -0200127 struct media_entity_graph graph;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300128};
129
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300130/**
131 * struct media_link - A link object part of a media graph.
132 *
133 * @graph_obj: Embedded structure containing the media object common data
134 * @list: Linked list associated with an entity or an interface that
135 * owns the link.
136 * @gobj0: Part of a union. Used to get the pointer for the first
137 * graph_object of the link.
138 * @source: Part of a union. Used only if the first object (gobj0) is
139 * a pad. In that case, it represents the source pad.
140 * @intf: Part of a union. Used only if the first object (gobj0) is
141 * an interface.
142 * @gobj1: Part of a union. Used to get the pointer for the second
143 * graph_object of the link.
144 * @source: Part of a union. Used only if the second object (gobj1) is
145 * a pad. In that case, it represents the sink pad.
146 * @entity: Part of a union. Used only if the second object (gobj1) is
147 * an entity.
148 * @reverse: Pointer to the link for the reverse direction of a pad to pad
149 * link.
150 * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300151 * @is_backlink: Indicate if the link is a backlink.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300152 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300153struct media_link {
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300154 struct media_gobj graph_obj;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300155 struct list_head list;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300156 union {
157 struct media_gobj *gobj0;
158 struct media_pad *source;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300159 struct media_interface *intf;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300160 };
161 union {
162 struct media_gobj *gobj1;
163 struct media_pad *sink;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300164 struct media_entity *entity;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300165 };
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300166 struct media_link *reverse;
167 unsigned long flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300168 bool is_backlink;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300169};
170
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300171/**
172 * struct media_pad - A media pad graph object.
173 *
174 * @graph_obj: Embedded structure containing the media object common data
175 * @entity: Entity this pad belongs to
176 * @index: Pad index in the entity pads array, numbered from 0 to n
177 * @flags: Pad flags, as defined in uapi/media.h (MEDIA_PAD_FL_*)
178 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300179struct media_pad {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300180 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300181 struct media_entity *entity;
182 u16 index;
183 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300184};
185
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300186/**
187 * struct media_entity_operations - Media entity operations
188 * @link_setup: Notify the entity of link changes. The operation can
189 * return an error, in which case link setup will be
190 * cancelled. Optional.
191 * @link_validate: Return whether a link is valid from the entity point of
192 * view. The media_entity_pipeline_start() function
193 * validates all links by calling this operation. Optional.
194 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300195struct media_entity_operations {
196 int (*link_setup)(struct media_entity *entity,
197 const struct media_pad *local,
198 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300199 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300200};
201
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300202/**
203 * struct media_entity - A media entity graph object.
204 *
205 * @graph_obj: Embedded structure containing the media object common data.
206 * @name: Entity name.
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300207 * @function: Entity main function, as defined in uapi/media.h
208 * (MEDIA_ENT_F_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300209 * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300210 * @num_pads: Number of sink and source pads.
211 * @num_links: Total number of links, forward and back, enabled and disabled.
212 * @num_backlinks: Number of backlinks
Sakari Ailus665faa92015-12-16 11:32:17 -0200213 * @internal_idx: An unique internal entity specific number. The numbers are
214 * re-used if entities are unregistered or registered again.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300215 * @pads: Pads array with the size defined by @num_pads.
216 * @links: List of data links.
217 * @ops: Entity operations.
218 * @stream_count: Stream count for the entity.
219 * @use_count: Use count for the entity.
220 * @pipe: Pipeline this entity belongs to.
221 * @info: Union with devnode information. Kept just for backward
222 * compatibility.
223 * @major: Devnode major number (zero if not applicable). Kept just
224 * for backward compatibility.
225 * @minor: Devnode minor number (zero if not applicable). Kept just
226 * for backward compatibility.
227 *
228 * NOTE: @stream_count and @use_count reference counts must never be
229 * negative, but are signed integers on purpose: a simple WARN_ON(<0) check
230 * can be used to detect reference count bugs that would make them negative.
231 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300232struct media_entity {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300233 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300234 const char *name;
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300235 u32 function;
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300236 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300237
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300238 u16 num_pads;
239 u16 num_links;
240 u16 num_backlinks;
Sakari Ailus665faa92015-12-16 11:32:17 -0200241 int internal_idx;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300242
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300243 struct media_pad *pads;
244 struct list_head links;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300245
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300246 const struct media_entity_operations *ops;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300247
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300248 /* Reference counts must never be negative, but are signed integers on
249 * purpose: a simple WARN_ON(<0) check can be used to detect reference
250 * count bugs that would make them negative.
251 */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300252 int stream_count;
253 int use_count;
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300254
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300255 struct media_pipeline *pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300256
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300257 union {
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300258 struct {
259 u32 major;
260 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300261 } dev;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300262 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300263};
264
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300265/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300266 * struct media_interface - A media interface graph object.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300267 *
268 * @graph_obj: embedded graph object
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300269 * @links: List of links pointing to graph entities
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300270 * @type: Type of the interface as defined in the
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300271 * uapi/media/media.h header, e. g.
272 * MEDIA_INTF_T_*
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300273 * @flags: Interface flags as defined in uapi/media/media.h
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300274 */
275struct media_interface {
276 struct media_gobj graph_obj;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300277 struct list_head links;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300278 u32 type;
279 u32 flags;
280};
281
282/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300283 * struct media_intf_devnode - A media interface via a device node.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300284 *
285 * @intf: embedded interface object
286 * @major: Major number of a device node
287 * @minor: Minor number of a device node
288 */
289struct media_intf_devnode {
290 struct media_interface intf;
Mauro Carvalho Chehabc398bb62015-08-23 08:28:21 -0300291
292 /* Should match the fields at media_v2_intf_devnode */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300293 u32 major;
294 u32 minor;
295};
296
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200297/**
298 * media_entity_id() - return the media entity graph object id
299 *
300 * @entity: pointer to entity
301 */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300302static inline u32 media_entity_id(struct media_entity *entity)
303{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300304 return entity->graph_obj.id;
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300305}
306
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200307/**
308 * media_type() - return the media object type
309 *
310 * @gobj: pointer to the media graph object
311 */
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300312static inline enum media_gobj_type media_type(struct media_gobj *gobj)
313{
314 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
315}
316
317static inline u32 media_localid(struct media_gobj *gobj)
318{
319 return gobj->id & MEDIA_LOCAL_ID_MASK;
320}
321
322static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
323{
324 u32 id;
325
326 id = type << MEDIA_BITS_PER_LOCAL_ID;
327 id |= local_id & MEDIA_LOCAL_ID_MASK;
328
329 return id;
330}
331
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200332/**
333 * is_media_entity_v4l2_io() - identify if the entity main function
334 * is a V4L2 I/O
335 *
336 * @entity: pointer to entity
337 *
338 * Return: true if the entity main function is one of the V4L2 I/O types
339 * (video, VBI or SDR radio); false otherwise.
340 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300341static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
342{
343 if (!entity)
344 return false;
345
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300346 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200347 case MEDIA_ENT_F_IO_V4L:
348 case MEDIA_ENT_F_IO_VBI:
349 case MEDIA_ENT_F_IO_SWRADIO:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300350 return true;
351 default:
352 return false;
353 }
354}
355
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200356/**
357 * is_media_entity_v4l2_subdev - return true if the entity main function is
358 * associated with the V4L2 API subdev usage
359 *
360 * @entity: pointer to entity
361 *
362 * This is an ancillary function used by subdev-based V4L2 drivers.
363 * It checks if the entity function is one of functions used by a V4L2 subdev,
364 * e. g. camera-relatef functions, analog TV decoder, TV tuner, V4L2 DSPs.
365 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300366static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
367{
368 if (!entity)
369 return false;
370
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300371 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200372 case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
373 case MEDIA_ENT_F_CAM_SENSOR:
374 case MEDIA_ENT_F_FLASH:
375 case MEDIA_ENT_F_LENS:
376 case MEDIA_ENT_F_ATV_DECODER:
377 case MEDIA_ENT_F_TUNER:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300378 return true;
379
380 default:
381 return false;
382 }
383}
384
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200385__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
386 int idx_max);
387void media_entity_enum_cleanup(struct media_entity_enum *e);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300388
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200389/**
390 * media_entity_enum_zero - Clear the entire enum
391 *
392 * @e: Entity enumeration to be cleared
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300393 */
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200394static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
395{
396 bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
397}
398
399/**
400 * media_entity_enum_set - Mark a single entity in the enum
401 *
402 * @e: Entity enumeration
403 * @entity: Entity to be marked
404 */
405static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
406 struct media_entity *entity)
407{
408 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
409 return;
410
411 __set_bit(entity->internal_idx, ent_enum->bmap);
412}
413
414/**
415 * media_entity_enum_clear - Unmark a single entity in the enum
416 *
417 * @e: Entity enumeration
418 * @entity: Entity to be unmarked
419 */
420static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
421 struct media_entity *entity)
422{
423 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
424 return;
425
426 __clear_bit(entity->internal_idx, ent_enum->bmap);
427}
428
429/**
430 * media_entity_enum_test - Test whether the entity is marked
431 *
432 * @e: Entity enumeration
433 * @entity: Entity to be tested
434 *
435 * Returns true if the entity was marked.
436 */
437static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
438 struct media_entity *entity)
439{
440 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
441 return true;
442
443 return test_bit(entity->internal_idx, ent_enum->bmap);
444}
445
446/**
447 * media_entity_enum_test - Test whether the entity is marked, and mark it
448 *
449 * @e: Entity enumeration
450 * @entity: Entity to be tested
451 *
452 * Returns true if the entity was marked, and mark it before doing so.
453 */
454static inline bool media_entity_enum_test_and_set(
455 struct media_entity_enum *ent_enum, struct media_entity *entity)
456{
457 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
458 return true;
459
460 return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
461}
462
463/**
464 * media_entity_enum_test - Test whether the entire enum is empty
465 *
466 * @e: Entity enumeration
467 * @entity: Entity to be tested
468 *
469 * Returns true if the entity was marked.
470 */
471static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
472{
473 return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
474}
475
476/**
477 * media_entity_enum_intersects - Test whether two enums intersect
478 *
479 * @e: First entity enumeration
480 * @f: Second entity enumeration
481 *
482 * Returns true if entity enumerations e and f intersect, otherwise false.
483 */
484static inline bool media_entity_enum_intersects(
485 struct media_entity_enum *ent_enum1,
486 struct media_entity_enum *ent_enum2)
487{
488 WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
489
490 return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
491 min(ent_enum1->idx_max, ent_enum2->idx_max));
492}
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300493
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300494#define gobj_to_entity(gobj) \
495 container_of(gobj, struct media_entity, graph_obj)
496
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300497#define gobj_to_pad(gobj) \
498 container_of(gobj, struct media_pad, graph_obj)
499
500#define gobj_to_link(gobj) \
501 container_of(gobj, struct media_link, graph_obj)
502
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300503#define gobj_to_link(gobj) \
504 container_of(gobj, struct media_link, graph_obj)
505
506#define gobj_to_pad(gobj) \
507 container_of(gobj, struct media_pad, graph_obj)
508
509#define gobj_to_intf(gobj) \
510 container_of(gobj, struct media_interface, graph_obj)
511
512#define intf_to_devnode(intf) \
513 container_of(intf, struct media_intf_devnode, intf)
514
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200515/**
516 * media_gobj_create - Initialize a graph object
517 *
518 * @mdev: Pointer to the media_device that contains the object
519 * @type: Type of the object
520 * @gobj: Pointer to the graph object
521 *
522 * This routine initializes the embedded struct media_gobj inside a
523 * media graph object. It is called automatically if media_*_create()
524 * calls are used. However, if the object (entity, link, pad, interface)
525 * is embedded on some other object, this function should be called before
526 * registering the object at the media controller.
527 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200528void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300529 enum media_gobj_type type,
530 struct media_gobj *gobj);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200531
532/**
533 * media_gobj_destroy - Stop using a graph object on a media device
534 *
535 * @gobj: Pointer to the graph object
536 *
537 * This should be called by all routines like media_device_unregister()
538 * that remove/destroy media graph objects.
539 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200540void media_gobj_destroy(struct media_gobj *gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300541
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200542/**
543 * media_entity_pads_init() - Initialize the entity pads
544 *
545 * @entity: entity where the pads belong
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200546 * @num_pads: total number of sink and source pads
547 * @pads: Array of @num_pads pads.
548 *
549 * The pads array is managed by the entity driver and passed to
550 * media_entity_pads_init() where its pointer will be stored in the entity
551 * structure.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200552 *
553 * If no pads are needed, drivers could either directly fill
554 * &media_entity->@num_pads with 0 and &media_entity->@pads with NULL or call
555 * this function that will do the same.
556 *
557 * As the number of pads is known in advance, the pads array is not allocated
558 * dynamically but is managed by the entity driver. Most drivers will embed the
559 * pads array in a driver-specific structure, avoiding dynamic allocation.
560 *
561 * Drivers must set the direction of every pad in the pads array before calling
562 * media_entity_pads_init(). The function will initialize the other pads fields.
563 */
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200564int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300565 struct media_pad *pads);
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200566
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200567/**
568 * media_entity_cleanup() - free resources associated with an entity
569 *
570 * @entity: entity where the pads belong
571 *
572 * This function must be called during the cleanup phase after unregistering
573 * the entity (currently, it does nothing).
574 */
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200575static inline void media_entity_cleanup(struct media_entity *entity) {};
Laurent Pincharte02188c2010-08-25 09:00:41 -0300576
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200577/**
578 * media_create_pad_link() - creates a link between two entities.
579 *
580 * @source: pointer to &media_entity of the source pad.
581 * @source_pad: number of the source pad in the pads array
582 * @sink: pointer to &media_entity of the sink pad.
583 * @sink_pad: number of the sink pad in the pads array.
584 * @flags: Link flags, as defined in include/uapi/linux/media.h.
585 *
586 * Valid values for flags:
587 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
588 * used to transfer media data. When two or more links target a sink pad,
589 * only one of them can be enabled at a time.
590 *
591 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
592 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
593 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
594 * always enabled.
595 *
596 * NOTE:
597 *
598 * Before calling this function, media_entity_pads_init() and
599 * media_device_register_entity() should be called previously for both ends.
600 */
Mauro Carvalho Chehab77328042015-09-04 16:08:24 -0300601__must_check int media_create_pad_link(struct media_entity *source,
602 u16 source_pad, struct media_entity *sink,
603 u16 sink_pad, u32 flags);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300604void __media_entity_remove_links(struct media_entity *entity);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200605
606/**
607 * media_entity_remove_links() - remove all links associated with an entity
608 *
609 * @entity: pointer to &media_entity
610 *
611 * Note: this is called automatically when an entity is unregistered via
612 * media_device_register_entity().
613 */
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300614void media_entity_remove_links(struct media_entity *entity);
615
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200616/**
617 * __media_entity_setup_link - Configure a media link without locking
618 * @link: The link being configured
619 * @flags: Link configuration flags
620 *
621 * The bulk of link setup is handled by the two entities connected through the
622 * link. This function notifies both entities of the link configuration change.
623 *
624 * If the link is immutable or if the current and new configuration are
625 * identical, return immediately.
626 *
627 * The user is expected to hold link->source->parent->mutex. If not,
628 * media_entity_setup_link() should be used instead.
629 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300630int __media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200631
632/**
633 * media_entity_setup_link() - changes the link flags properties in runtime
634 *
635 * @link: pointer to &media_link
636 * @flags: the requested new link flags
637 *
638 * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
639 * flag to enable/disable a link. Links marked with the
640 * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
641 *
642 * When a link is enabled or disabled, the media framework calls the
643 * link_setup operation for the two entities at the source and sink of the
644 * link, in that order. If the second link_setup call fails, another
645 * link_setup call is made on the first entity to restore the original link
646 * flags.
647 *
648 * Media device drivers can be notified of link setup operations by setting the
649 * media_device::link_notify pointer to a callback function. If provided, the
650 * notification callback will be called before enabling and after disabling
651 * links.
652 *
653 * Entity drivers must implement the link_setup operation if any of their links
654 * is non-immutable. The operation must either configure the hardware or store
655 * the configuration information to be applied later.
656 *
657 * Link configuration must not have any side effect on other links. If an
658 * enabled link at a sink pad prevents another link at the same pad from
659 * being enabled, the link_setup operation must return -EBUSY and can't
660 * implicitly disable the first enabled link.
661 *
662 * NOTE: the valid values of the flags for the link is the same as described
663 * on media_create_pad_link(), for pad to pad links or the same as described
664 * on media_create_intf_link(), for interface to entity links.
665 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300666int media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200667
668/**
669 * media_entity_find_link - Find a link between two pads
670 * @source: Source pad
671 * @sink: Sink pad
672 *
673 * Return a pointer to the link between the two entities. If no such link
674 * exists, return NULL.
675 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300676struct media_link *media_entity_find_link(struct media_pad *source,
677 struct media_pad *sink);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200678
679/**
680 * media_entity_remote_pad - Find the pad at the remote end of a link
681 * @pad: Pad at the local end of the link
682 *
683 * Search for a remote pad connected to the given pad by iterating over all
684 * links originating or terminating at that pad until an enabled link is found.
685 *
686 * Return a pointer to the pad at the remote end of the first found enabled
687 * link, or NULL if no enabled link has been found.
688 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300689struct media_pad *media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300690
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200691/**
692 * media_entity_get - Get a reference to the parent module
693 *
694 * @entity: The entity
695 *
696 * Get a reference to the parent media device module.
697 *
698 * The function will return immediately if @entity is NULL.
699 *
700 * Return a pointer to the entity on success or NULL on failure.
701 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300702struct media_entity *media_entity_get(struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200703
Sakari Ailuse03d2202015-12-16 11:32:22 -0200704__must_check int media_entity_graph_walk_init(
705 struct media_entity_graph *graph, struct media_device *mdev);
706void media_entity_graph_walk_cleanup(struct media_entity_graph *graph);
707
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200708/**
709 * media_entity_put - Release the reference to the parent module
710 *
711 * @entity: The entity
712 *
713 * Release the reference count acquired by media_entity_get().
714 *
715 * The function will return immediately if @entity is NULL.
716 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300717void media_entity_put(struct media_entity *entity);
718
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200719/**
720 * media_entity_graph_walk_start - Start walking the media graph at a given entity
721 * @graph: Media graph structure that will be used to walk the graph
722 * @entity: Starting entity
723 *
Sakari Ailuse03d2202015-12-16 11:32:22 -0200724 * Before using this function, media_entity_graph_walk_init() must be
725 * used to allocate resources used for walking the graph. This
726 * function initializes the graph traversal structure to walk the
727 * entities graph starting at the given entity. The traversal
728 * structure must not be modified by the caller during graph
729 * traversal. After the graph walk, the resources must be released
730 * using media_entity_graph_walk_cleanup().
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200731 */
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300732void media_entity_graph_walk_start(struct media_entity_graph *graph,
Sakari Ailuse03d2202015-12-16 11:32:22 -0200733 struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200734
735/**
736 * media_entity_graph_walk_next - Get the next entity in the graph
737 * @graph: Media graph structure
738 *
739 * Perform a depth-first traversal of the given media entities graph.
740 *
741 * The graph structure must have been previously initialized with a call to
742 * media_entity_graph_walk_start().
743 *
744 * Return the next entity in the graph or NULL if the whole graph have been
745 * traversed.
746 */
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300747struct media_entity *
748media_entity_graph_walk_next(struct media_entity_graph *graph);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200749
750/**
751 * media_entity_pipeline_start - Mark a pipeline as streaming
752 * @entity: Starting entity
753 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
754 *
755 * Mark all entities connected to a given entity through enabled links, either
756 * directly or indirectly, as streaming. The given pipeline object is assigned to
757 * every entity in the pipeline and stored in the media_entity pipe field.
758 *
759 * Calls to this function can be nested, in which case the same number of
760 * media_entity_pipeline_stop() calls will be required to stop streaming. The
761 * pipeline pointer must be identical for all nested calls to
762 * media_entity_pipeline_start().
763 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300764__must_check int media_entity_pipeline_start(struct media_entity *entity,
765 struct media_pipeline *pipe);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200766
767/**
768 * media_entity_pipeline_stop - Mark a pipeline as not streaming
769 * @entity: Starting entity
770 *
771 * Mark all entities connected to a given entity through enabled links, either
772 * directly or indirectly, as not streaming. The media_entity pipe field is
773 * reset to NULL.
774 *
775 * If multiple calls to media_entity_pipeline_start() have been made, the same
776 * number of calls to this function are required to mark the pipeline as not
777 * streaming.
778 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300779void media_entity_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300780
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200781/**
782 * media_devnode_create() - creates and initializes a device node interface
783 *
784 * @mdev: pointer to struct &media_device
785 * @type: type of the interface, as given by MEDIA_INTF_T_* macros
786 * as defined in the uapi/media/media.h header.
787 * @flags: Interface flags as defined in uapi/media/media.h.
788 * @major: Device node major number.
789 * @minor: Device node minor number.
790 *
791 * Return: if succeeded, returns a pointer to the newly allocated
792 * &media_intf_devnode pointer.
793 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300794struct media_intf_devnode *
795__must_check media_devnode_create(struct media_device *mdev,
796 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300797 u32 major, u32 minor);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200798/**
799 * media_devnode_remove() - removes a device node interface
800 *
801 * @devnode: pointer to &media_intf_devnode to be freed.
802 *
803 * When a device node interface is removed, all links to it are automatically
804 * removed.
805 */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300806void media_devnode_remove(struct media_intf_devnode *devnode);
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300807struct media_link *
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200808
809/**
810 * media_create_intf_link() - creates a link between an entity and an interface
811 *
812 * @entity: pointer to %media_entity
813 * @intf: pointer to %media_interface
814 * @flags: Link flags, as defined in include/uapi/linux/media.h.
815 *
816 *
817 * Valid values for flags:
818 * The %MEDIA_LNK_FL_ENABLED flag indicates that the interface is connected to
819 * the entity hardware. That's the default value for interfaces. An
820 * interface may be disabled if the hardware is busy due to the usage
821 * of some other interface that it is currently controlling the hardware.
822 * A typical example is an hybrid TV device that handle only one type of
823 * stream on a given time. So, when the digital TV is streaming,
824 * the V4L2 interfaces won't be enabled, as such device is not able to
825 * also stream analog TV or radio.
826 *
827 * Note:
828 *
829 * Before calling this function, media_devnode_create() should be called for
830 * the interface and media_device_register_entity() should be called for the
831 * interface that will be part of the link.
832 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300833__must_check media_create_intf_link(struct media_entity *entity,
834 struct media_interface *intf,
835 u32 flags);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200836/**
837 * __media_remove_intf_link() - remove a single interface link
838 *
839 * @link: pointer to &media_link.
840 *
841 * Note: this is an unlocked version of media_remove_intf_link()
842 */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300843void __media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200844
845/**
846 * media_remove_intf_link() - remove a single interface link
847 *
848 * @link: pointer to &media_link.
849 *
850 * Note: prefer to use this one, instead of __media_remove_intf_link()
851 */
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300852void media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200853
854/**
855 * __media_remove_intf_links() - remove all links associated with an interface
856 *
857 * @intf: pointer to &media_interface
858 *
859 * Note: this is an unlocked version of media_remove_intf_links().
860 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300861void __media_remove_intf_links(struct media_interface *intf);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200862/**
863 * media_remove_intf_links() - remove all links associated with an interface
864 *
865 * @intf: pointer to &media_interface
866 *
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200867 * Notes:
868 *
869 * this is called automatically when an entity is unregistered via
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200870 * media_device_register_entity() and by media_devnode_remove().
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200871 *
872 * Prefer to use this one, instead of __media_remove_intf_links().
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200873 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300874void media_remove_intf_links(struct media_interface *intf);
875
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300876
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300877#define media_entity_call(entity, operation, args...) \
878 (((entity)->ops && (entity)->ops->operation) ? \
879 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
880
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300881#endif