blob: c4aaeb85229c1dda9db257d2b6ffbc93b4b42431 [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
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020075
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020076/**
77 * struct media_entity_enum - An enumeration of media entities.
78 *
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020079 * @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 */
83struct media_entity_enum {
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020084 unsigned long *bmap;
85 int idx_max;
86};
87
Sakari Ailus434257f2015-12-16 11:32:20 -020088/**
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.
Sakari Ailus29d8da02015-12-16 11:32:28 -020094 * @ent_enum: Visited entities
Sakari Ailus434257f2015-12-16 11:32:20 -020095 * @top: The top of the stack
96 */
Sakari Ailus82c68292015-12-16 11:32:19 -020097struct media_entity_graph {
98 struct {
99 struct media_entity *entity;
100 struct list_head *link;
101 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
102
Sakari Ailus29d8da02015-12-16 11:32:28 -0200103 struct media_entity_enum ent_enum;
Sakari Ailus82c68292015-12-16 11:32:19 -0200104 int top;
105};
106
Sakari Ailus5dd87752015-12-16 11:32:21 -0200107/*
108 * struct media_pipeline - Media pipeline related information
109 *
Sakari Ailus74a413302015-12-16 11:32:29 -0200110 * @streaming_count: Streaming start count - streaming stop count
111 * @graph: Media graph walk during pipeline start / stop
Sakari Ailus5dd87752015-12-16 11:32:21 -0200112 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300113struct media_pipeline {
Sakari Ailus74a413302015-12-16 11:32:29 -0200114 int streaming_count;
Sakari Ailus5dd87752015-12-16 11:32:21 -0200115 struct media_entity_graph graph;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300116};
117
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300118/**
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_*)
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300139 * @is_backlink: Indicate if the link is a backlink.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300140 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300141struct media_link {
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300142 struct media_gobj graph_obj;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300143 struct list_head list;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300144 union {
145 struct media_gobj *gobj0;
146 struct media_pad *source;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300147 struct media_interface *intf;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300148 };
149 union {
150 struct media_gobj *gobj1;
151 struct media_pad *sink;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300152 struct media_entity *entity;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300153 };
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300154 struct media_link *reverse;
155 unsigned long flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300156 bool is_backlink;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300157};
158
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300159/**
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 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300167struct media_pad {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300168 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300169 struct media_entity *entity;
170 u16 index;
171 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300172};
173
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300174/**
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 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300183struct media_entity_operations {
184 int (*link_setup)(struct media_entity *entity,
185 const struct media_pad *local,
186 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300187 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300188};
189
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300190/**
191 * struct media_entity - A media entity graph object.
192 *
193 * @graph_obj: Embedded structure containing the media object common data.
194 * @name: Entity name.
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300195 * @function: Entity main function, as defined in uapi/media.h
196 * (MEDIA_ENT_F_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300197 * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300198 * @num_pads: Number of sink and source pads.
199 * @num_links: Total number of links, forward and back, enabled and disabled.
200 * @num_backlinks: Number of backlinks
Sakari Ailus665faa92015-12-16 11:32:17 -0200201 * @internal_idx: An unique internal entity specific number. The numbers are
202 * re-used if entities are unregistered or registered again.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300203 * @pads: Pads array with the size defined by @num_pads.
204 * @links: List of data links.
205 * @ops: Entity operations.
206 * @stream_count: Stream count for the entity.
207 * @use_count: Use count for the entity.
208 * @pipe: Pipeline this entity belongs to.
209 * @info: Union with devnode information. Kept just for backward
210 * compatibility.
211 * @major: Devnode major number (zero if not applicable). Kept just
212 * for backward compatibility.
213 * @minor: Devnode minor number (zero if not applicable). Kept just
214 * for backward compatibility.
215 *
216 * NOTE: @stream_count and @use_count reference counts must never be
217 * negative, but are signed integers on purpose: a simple WARN_ON(<0) check
218 * can be used to detect reference count bugs that would make them negative.
219 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300220struct media_entity {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300221 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300222 const char *name;
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300223 u32 function;
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300224 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300225
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300226 u16 num_pads;
227 u16 num_links;
228 u16 num_backlinks;
Sakari Ailus665faa92015-12-16 11:32:17 -0200229 int internal_idx;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300230
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300231 struct media_pad *pads;
232 struct list_head links;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300233
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300234 const struct media_entity_operations *ops;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300235
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300236 /* Reference counts must never be negative, but are signed integers on
237 * purpose: a simple WARN_ON(<0) check can be used to detect reference
238 * count bugs that would make them negative.
239 */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300240 int stream_count;
241 int use_count;
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300242
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300243 struct media_pipeline *pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300244
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300245 union {
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300246 struct {
247 u32 major;
248 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300249 } dev;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300250 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300251};
252
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300253/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300254 * struct media_interface - A media interface graph object.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300255 *
256 * @graph_obj: embedded graph object
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300257 * @links: List of links pointing to graph entities
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300258 * @type: Type of the interface as defined in the
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300259 * uapi/media/media.h header, e. g.
260 * MEDIA_INTF_T_*
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300261 * @flags: Interface flags as defined in uapi/media/media.h
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300262 */
263struct media_interface {
264 struct media_gobj graph_obj;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300265 struct list_head links;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300266 u32 type;
267 u32 flags;
268};
269
270/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300271 * struct media_intf_devnode - A media interface via a device node.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300272 *
273 * @intf: embedded interface object
274 * @major: Major number of a device node
275 * @minor: Minor number of a device node
276 */
277struct media_intf_devnode {
278 struct media_interface intf;
Mauro Carvalho Chehabc398bb62015-08-23 08:28:21 -0300279
280 /* Should match the fields at media_v2_intf_devnode */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300281 u32 major;
282 u32 minor;
283};
284
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200285/**
286 * media_entity_id() - return the media entity graph object id
287 *
288 * @entity: pointer to entity
289 */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300290static inline u32 media_entity_id(struct media_entity *entity)
291{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300292 return entity->graph_obj.id;
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300293}
294
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200295/**
296 * media_type() - return the media object type
297 *
298 * @gobj: pointer to the media graph object
299 */
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300300static inline enum media_gobj_type media_type(struct media_gobj *gobj)
301{
302 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
303}
304
305static inline u32 media_localid(struct media_gobj *gobj)
306{
307 return gobj->id & MEDIA_LOCAL_ID_MASK;
308}
309
310static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
311{
312 u32 id;
313
314 id = type << MEDIA_BITS_PER_LOCAL_ID;
315 id |= local_id & MEDIA_LOCAL_ID_MASK;
316
317 return id;
318}
319
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200320/**
321 * is_media_entity_v4l2_io() - identify if the entity main function
322 * is a V4L2 I/O
323 *
324 * @entity: pointer to entity
325 *
326 * Return: true if the entity main function is one of the V4L2 I/O types
327 * (video, VBI or SDR radio); false otherwise.
328 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300329static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
330{
331 if (!entity)
332 return false;
333
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300334 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200335 case MEDIA_ENT_F_IO_V4L:
336 case MEDIA_ENT_F_IO_VBI:
337 case MEDIA_ENT_F_IO_SWRADIO:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300338 return true;
339 default:
340 return false;
341 }
342}
343
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200344/**
345 * is_media_entity_v4l2_subdev - return true if the entity main function is
346 * associated with the V4L2 API subdev usage
347 *
348 * @entity: pointer to entity
349 *
350 * This is an ancillary function used by subdev-based V4L2 drivers.
351 * It checks if the entity function is one of functions used by a V4L2 subdev,
352 * e. g. camera-relatef functions, analog TV decoder, TV tuner, V4L2 DSPs.
353 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300354static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
355{
356 if (!entity)
357 return false;
358
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300359 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200360 case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
361 case MEDIA_ENT_F_CAM_SENSOR:
362 case MEDIA_ENT_F_FLASH:
363 case MEDIA_ENT_F_LENS:
364 case MEDIA_ENT_F_ATV_DECODER:
365 case MEDIA_ENT_F_TUNER:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300366 return true;
367
368 default:
369 return false;
370 }
371}
372
Mauro Carvalho Chehab92777992015-12-16 13:53:04 -0200373/**
374 * __media_entity_enum_init - Initialise an entity enumeration
375 *
376 * @ent_enum: Entity enumeration to be initialised
377 * @idx_max: Maximum number of entities in the enumeration
378 *
379 * Return: Returns zero on success or a negative error code.
380 */
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200381__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
382 int idx_max);
Mauro Carvalho Chehab92777992015-12-16 13:53:04 -0200383
384/**
385 * media_entity_enum_cleanup - Release resources of an entity enumeration
386 *
387 * @ent_enum: Entity enumeration to be released
388 */
389void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300390
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200391/**
392 * media_entity_enum_zero - Clear the entire enum
393 *
394 * @e: Entity enumeration to be cleared
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300395 */
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200396static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
397{
398 bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
399}
400
401/**
402 * media_entity_enum_set - Mark a single entity in the enum
403 *
404 * @e: Entity enumeration
405 * @entity: Entity to be marked
406 */
407static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
408 struct media_entity *entity)
409{
410 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
411 return;
412
413 __set_bit(entity->internal_idx, ent_enum->bmap);
414}
415
416/**
417 * media_entity_enum_clear - Unmark a single entity in the enum
418 *
419 * @e: Entity enumeration
420 * @entity: Entity to be unmarked
421 */
422static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
423 struct media_entity *entity)
424{
425 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
426 return;
427
428 __clear_bit(entity->internal_idx, ent_enum->bmap);
429}
430
431/**
432 * media_entity_enum_test - Test whether the entity is marked
433 *
434 * @e: Entity enumeration
435 * @entity: Entity to be tested
436 *
437 * Returns true if the entity was marked.
438 */
439static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
440 struct media_entity *entity)
441{
442 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
443 return true;
444
445 return test_bit(entity->internal_idx, ent_enum->bmap);
446}
447
448/**
449 * media_entity_enum_test - Test whether the entity is marked, and mark it
450 *
451 * @e: Entity enumeration
452 * @entity: Entity to be tested
453 *
454 * Returns true if the entity was marked, and mark it before doing so.
455 */
456static inline bool media_entity_enum_test_and_set(
457 struct media_entity_enum *ent_enum, struct media_entity *entity)
458{
459 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
460 return true;
461
462 return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
463}
464
465/**
466 * media_entity_enum_test - Test whether the entire enum is empty
467 *
468 * @e: Entity enumeration
469 * @entity: Entity to be tested
470 *
471 * Returns true if the entity was marked.
472 */
473static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
474{
475 return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
476}
477
478/**
479 * media_entity_enum_intersects - Test whether two enums intersect
480 *
481 * @e: First entity enumeration
482 * @f: Second entity enumeration
483 *
484 * Returns true if entity enumerations e and f intersect, otherwise false.
485 */
486static inline bool media_entity_enum_intersects(
487 struct media_entity_enum *ent_enum1,
488 struct media_entity_enum *ent_enum2)
489{
490 WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
491
492 return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
493 min(ent_enum1->idx_max, ent_enum2->idx_max));
494}
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300495
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300496#define gobj_to_entity(gobj) \
497 container_of(gobj, struct media_entity, graph_obj)
498
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300499#define gobj_to_pad(gobj) \
500 container_of(gobj, struct media_pad, graph_obj)
501
502#define gobj_to_link(gobj) \
503 container_of(gobj, struct media_link, graph_obj)
504
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300505#define gobj_to_link(gobj) \
506 container_of(gobj, struct media_link, graph_obj)
507
508#define gobj_to_pad(gobj) \
509 container_of(gobj, struct media_pad, graph_obj)
510
511#define gobj_to_intf(gobj) \
512 container_of(gobj, struct media_interface, graph_obj)
513
514#define intf_to_devnode(intf) \
515 container_of(intf, struct media_intf_devnode, intf)
516
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200517/**
518 * media_gobj_create - Initialize a graph object
519 *
520 * @mdev: Pointer to the media_device that contains the object
521 * @type: Type of the object
522 * @gobj: Pointer to the graph object
523 *
524 * This routine initializes the embedded struct media_gobj inside a
525 * media graph object. It is called automatically if media_*_create()
526 * calls are used. However, if the object (entity, link, pad, interface)
527 * is embedded on some other object, this function should be called before
528 * registering the object at the media controller.
529 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200530void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300531 enum media_gobj_type type,
532 struct media_gobj *gobj);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200533
534/**
535 * media_gobj_destroy - Stop using a graph object on a media device
536 *
537 * @gobj: Pointer to the graph object
538 *
539 * This should be called by all routines like media_device_unregister()
540 * that remove/destroy media graph objects.
541 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200542void media_gobj_destroy(struct media_gobj *gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300543
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200544/**
545 * media_entity_pads_init() - Initialize the entity pads
546 *
547 * @entity: entity where the pads belong
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200548 * @num_pads: total number of sink and source pads
549 * @pads: Array of @num_pads pads.
550 *
551 * The pads array is managed by the entity driver and passed to
552 * media_entity_pads_init() where its pointer will be stored in the entity
553 * structure.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200554 *
555 * If no pads are needed, drivers could either directly fill
556 * &media_entity->@num_pads with 0 and &media_entity->@pads with NULL or call
557 * this function that will do the same.
558 *
559 * As the number of pads is known in advance, the pads array is not allocated
560 * dynamically but is managed by the entity driver. Most drivers will embed the
561 * pads array in a driver-specific structure, avoiding dynamic allocation.
562 *
563 * Drivers must set the direction of every pad in the pads array before calling
564 * media_entity_pads_init(). The function will initialize the other pads fields.
565 */
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200566int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300567 struct media_pad *pads);
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200568
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200569/**
570 * media_entity_cleanup() - free resources associated with an entity
571 *
572 * @entity: entity where the pads belong
573 *
574 * This function must be called during the cleanup phase after unregistering
575 * the entity (currently, it does nothing).
576 */
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200577static inline void media_entity_cleanup(struct media_entity *entity) {};
Laurent Pincharte02188c2010-08-25 09:00:41 -0300578
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200579/**
580 * media_create_pad_link() - creates a link between two entities.
581 *
582 * @source: pointer to &media_entity of the source pad.
583 * @source_pad: number of the source pad in the pads array
584 * @sink: pointer to &media_entity of the sink pad.
585 * @sink_pad: number of the sink pad in the pads array.
586 * @flags: Link flags, as defined in include/uapi/linux/media.h.
587 *
588 * Valid values for flags:
589 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
590 * used to transfer media data. When two or more links target a sink pad,
591 * only one of them can be enabled at a time.
592 *
593 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
594 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
595 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
596 * always enabled.
597 *
598 * NOTE:
599 *
600 * Before calling this function, media_entity_pads_init() and
601 * media_device_register_entity() should be called previously for both ends.
602 */
Mauro Carvalho Chehab77328042015-09-04 16:08:24 -0300603__must_check int media_create_pad_link(struct media_entity *source,
604 u16 source_pad, struct media_entity *sink,
605 u16 sink_pad, u32 flags);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300606void __media_entity_remove_links(struct media_entity *entity);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200607
608/**
609 * media_entity_remove_links() - remove all links associated with an entity
610 *
611 * @entity: pointer to &media_entity
612 *
613 * Note: this is called automatically when an entity is unregistered via
614 * media_device_register_entity().
615 */
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300616void media_entity_remove_links(struct media_entity *entity);
617
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200618/**
619 * __media_entity_setup_link - Configure a media link without locking
620 * @link: The link being configured
621 * @flags: Link configuration flags
622 *
623 * The bulk of link setup is handled by the two entities connected through the
624 * link. This function notifies both entities of the link configuration change.
625 *
626 * If the link is immutable or if the current and new configuration are
627 * identical, return immediately.
628 *
629 * The user is expected to hold link->source->parent->mutex. If not,
630 * media_entity_setup_link() should be used instead.
631 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300632int __media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200633
634/**
635 * media_entity_setup_link() - changes the link flags properties in runtime
636 *
637 * @link: pointer to &media_link
638 * @flags: the requested new link flags
639 *
640 * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
641 * flag to enable/disable a link. Links marked with the
642 * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
643 *
644 * When a link is enabled or disabled, the media framework calls the
645 * link_setup operation for the two entities at the source and sink of the
646 * link, in that order. If the second link_setup call fails, another
647 * link_setup call is made on the first entity to restore the original link
648 * flags.
649 *
650 * Media device drivers can be notified of link setup operations by setting the
651 * media_device::link_notify pointer to a callback function. If provided, the
652 * notification callback will be called before enabling and after disabling
653 * links.
654 *
655 * Entity drivers must implement the link_setup operation if any of their links
656 * is non-immutable. The operation must either configure the hardware or store
657 * the configuration information to be applied later.
658 *
659 * Link configuration must not have any side effect on other links. If an
660 * enabled link at a sink pad prevents another link at the same pad from
661 * being enabled, the link_setup operation must return -EBUSY and can't
662 * implicitly disable the first enabled link.
663 *
664 * NOTE: the valid values of the flags for the link is the same as described
665 * on media_create_pad_link(), for pad to pad links or the same as described
666 * on media_create_intf_link(), for interface to entity links.
667 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300668int media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200669
670/**
671 * media_entity_find_link - Find a link between two pads
672 * @source: Source pad
673 * @sink: Sink pad
674 *
675 * Return a pointer to the link between the two entities. If no such link
676 * exists, return NULL.
677 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300678struct media_link *media_entity_find_link(struct media_pad *source,
679 struct media_pad *sink);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200680
681/**
682 * media_entity_remote_pad - Find the pad at the remote end of a link
683 * @pad: Pad at the local end of the link
684 *
685 * Search for a remote pad connected to the given pad by iterating over all
686 * links originating or terminating at that pad until an enabled link is found.
687 *
688 * Return a pointer to the pad at the remote end of the first found enabled
689 * link, or NULL if no enabled link has been found.
690 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300691struct media_pad *media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300692
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200693/**
694 * media_entity_get - Get a reference to the parent module
695 *
696 * @entity: The entity
697 *
698 * Get a reference to the parent media device module.
699 *
700 * The function will return immediately if @entity is NULL.
701 *
702 * Return a pointer to the entity on success or NULL on failure.
703 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300704struct media_entity *media_entity_get(struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200705
Sakari Ailuse03d2202015-12-16 11:32:22 -0200706__must_check int media_entity_graph_walk_init(
707 struct media_entity_graph *graph, struct media_device *mdev);
708void media_entity_graph_walk_cleanup(struct media_entity_graph *graph);
709
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200710/**
711 * media_entity_put - Release the reference to the parent module
712 *
713 * @entity: The entity
714 *
715 * Release the reference count acquired by media_entity_get().
716 *
717 * The function will return immediately if @entity is NULL.
718 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300719void media_entity_put(struct media_entity *entity);
720
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200721/**
722 * media_entity_graph_walk_start - Start walking the media graph at a given entity
723 * @graph: Media graph structure that will be used to walk the graph
724 * @entity: Starting entity
725 *
Sakari Ailuse03d2202015-12-16 11:32:22 -0200726 * Before using this function, media_entity_graph_walk_init() must be
727 * used to allocate resources used for walking the graph. This
728 * function initializes the graph traversal structure to walk the
729 * entities graph starting at the given entity. The traversal
730 * structure must not be modified by the caller during graph
731 * traversal. After the graph walk, the resources must be released
732 * using media_entity_graph_walk_cleanup().
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200733 */
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300734void media_entity_graph_walk_start(struct media_entity_graph *graph,
Sakari Ailuse03d2202015-12-16 11:32:22 -0200735 struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200736
737/**
738 * media_entity_graph_walk_next - Get the next entity in the graph
739 * @graph: Media graph structure
740 *
741 * Perform a depth-first traversal of the given media entities graph.
742 *
743 * The graph structure must have been previously initialized with a call to
744 * media_entity_graph_walk_start().
745 *
746 * Return the next entity in the graph or NULL if the whole graph have been
747 * traversed.
748 */
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300749struct media_entity *
750media_entity_graph_walk_next(struct media_entity_graph *graph);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200751
752/**
753 * media_entity_pipeline_start - Mark a pipeline as streaming
754 * @entity: Starting entity
755 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
756 *
757 * Mark all entities connected to a given entity through enabled links, either
758 * directly or indirectly, as streaming. The given pipeline object is assigned to
759 * every entity in the pipeline and stored in the media_entity pipe field.
760 *
761 * Calls to this function can be nested, in which case the same number of
762 * media_entity_pipeline_stop() calls will be required to stop streaming. The
763 * pipeline pointer must be identical for all nested calls to
764 * media_entity_pipeline_start().
765 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300766__must_check int media_entity_pipeline_start(struct media_entity *entity,
767 struct media_pipeline *pipe);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200768
769/**
770 * media_entity_pipeline_stop - Mark a pipeline as not streaming
771 * @entity: Starting entity
772 *
773 * Mark all entities connected to a given entity through enabled links, either
774 * directly or indirectly, as not streaming. The media_entity pipe field is
775 * reset to NULL.
776 *
777 * If multiple calls to media_entity_pipeline_start() have been made, the same
778 * number of calls to this function are required to mark the pipeline as not
779 * streaming.
780 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300781void media_entity_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300782
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200783/**
784 * media_devnode_create() - creates and initializes a device node interface
785 *
786 * @mdev: pointer to struct &media_device
787 * @type: type of the interface, as given by MEDIA_INTF_T_* macros
788 * as defined in the uapi/media/media.h header.
789 * @flags: Interface flags as defined in uapi/media/media.h.
790 * @major: Device node major number.
791 * @minor: Device node minor number.
792 *
793 * Return: if succeeded, returns a pointer to the newly allocated
794 * &media_intf_devnode pointer.
795 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300796struct media_intf_devnode *
797__must_check media_devnode_create(struct media_device *mdev,
798 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300799 u32 major, u32 minor);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200800/**
801 * media_devnode_remove() - removes a device node interface
802 *
803 * @devnode: pointer to &media_intf_devnode to be freed.
804 *
805 * When a device node interface is removed, all links to it are automatically
806 * removed.
807 */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300808void media_devnode_remove(struct media_intf_devnode *devnode);
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300809struct media_link *
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200810
811/**
812 * media_create_intf_link() - creates a link between an entity and an interface
813 *
814 * @entity: pointer to %media_entity
815 * @intf: pointer to %media_interface
816 * @flags: Link flags, as defined in include/uapi/linux/media.h.
817 *
818 *
819 * Valid values for flags:
820 * The %MEDIA_LNK_FL_ENABLED flag indicates that the interface is connected to
821 * the entity hardware. That's the default value for interfaces. An
822 * interface may be disabled if the hardware is busy due to the usage
823 * of some other interface that it is currently controlling the hardware.
824 * A typical example is an hybrid TV device that handle only one type of
825 * stream on a given time. So, when the digital TV is streaming,
826 * the V4L2 interfaces won't be enabled, as such device is not able to
827 * also stream analog TV or radio.
828 *
829 * Note:
830 *
831 * Before calling this function, media_devnode_create() should be called for
832 * the interface and media_device_register_entity() should be called for the
833 * interface that will be part of the link.
834 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300835__must_check media_create_intf_link(struct media_entity *entity,
836 struct media_interface *intf,
837 u32 flags);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200838/**
839 * __media_remove_intf_link() - remove a single interface link
840 *
841 * @link: pointer to &media_link.
842 *
843 * Note: this is an unlocked version of media_remove_intf_link()
844 */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300845void __media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200846
847/**
848 * media_remove_intf_link() - remove a single interface link
849 *
850 * @link: pointer to &media_link.
851 *
852 * Note: prefer to use this one, instead of __media_remove_intf_link()
853 */
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300854void media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200855
856/**
857 * __media_remove_intf_links() - remove all links associated with an interface
858 *
859 * @intf: pointer to &media_interface
860 *
861 * Note: this is an unlocked version of media_remove_intf_links().
862 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300863void __media_remove_intf_links(struct media_interface *intf);
Mauro Carvalho Chehab92777992015-12-16 13:53:04 -0200864
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200865/**
866 * media_remove_intf_links() - remove all links associated with an interface
867 *
868 * @intf: pointer to &media_interface
869 *
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200870 * Notes:
871 *
872 * this is called automatically when an entity is unregistered via
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200873 * media_device_register_entity() and by media_devnode_remove().
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200874 *
875 * Prefer to use this one, instead of __media_remove_intf_links().
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200876 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300877void media_remove_intf_links(struct media_interface *intf);
878
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300879#define media_entity_call(entity, operation, args...) \
880 (((entity)->ops && (entity)->ops->operation) ? \
881 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
882
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300883#endif