blob: a732af1dbba099d493af5884c293bd923669260a [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.
Laurent Pinchart53e269c2009-12-09 08:40:00 -030017 */
18
19#ifndef _MEDIA_ENTITY_H
20#define _MEDIA_ENTITY_H
21
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020022#include <linux/bitmap.h>
Philipp Zabel2899d352016-02-15 09:08:06 -020023#include <linux/bug.h>
Niklas Söderlundae45cd52017-06-15 06:17:25 -030024#include <linux/fwnode.h>
Sakari Ailus0149a2e2013-12-13 08:58:37 -030025#include <linux/kernel.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030026#include <linux/list.h>
Laurent Pinchart16513332009-12-09 08:40:01 -030027#include <linux/media.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030028
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030029/* Enums used internally at the media controller to represent graphs */
30
31/**
32 * enum media_gobj_type - type of a graph object
33 *
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030034 * @MEDIA_GRAPH_ENTITY: Identify a media entity
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030035 * @MEDIA_GRAPH_PAD: Identify a media pad
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030036 * @MEDIA_GRAPH_LINK: Identify a media link
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030037 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
38 * a device node
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030039 */
40enum media_gobj_type {
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030041 MEDIA_GRAPH_ENTITY,
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030042 MEDIA_GRAPH_PAD,
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030043 MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030044 MEDIA_GRAPH_INTF_DEVNODE,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030045};
46
47#define MEDIA_BITS_PER_TYPE 8
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -020048#define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE)
49#define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030050
51/* Structs to represent the objects that belong to a media graph */
52
53/**
54 * struct media_gobj - Define a graph object.
55 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -030056 * @mdev: Pointer to the struct &media_device that owns the object
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030057 * @id: Non-zero object ID identifier. The ID should be unique
58 * inside a media_device, as it is composed by
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -020059 * %MEDIA_BITS_PER_TYPE to store the type plus
60 * %MEDIA_BITS_PER_ID to store the ID
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030061 * @list: List entry stored in one of the per-type mdev object lists
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030062 *
63 * All objects on the media graph should have this struct embedded
64 */
65struct media_gobj {
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030066 struct media_device *mdev;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030067 u32 id;
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -030068 struct list_head list;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030069};
70
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020071#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020072
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020073/**
74 * struct media_entity_enum - An enumeration of media entities.
75 *
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020076 * @bmap: Bit map in which each bit represents one entity at struct
77 * media_entity->internal_idx.
78 * @idx_max: Number of bits in bmap
79 */
80struct media_entity_enum {
Sakari Ailusc8d54cd2015-12-16 11:44:32 -020081 unsigned long *bmap;
82 int idx_max;
83};
84
Sakari Ailus434257f2015-12-16 11:32:20 -020085/**
Sakari Ailus20b85222016-11-21 14:48:30 -020086 * struct media_graph - Media graph traversal state
Sakari Ailus434257f2015-12-16 11:32:20 -020087 *
88 * @stack: Graph traversal stack; the stack contains information
89 * on the path the media entities to be walked and the
90 * links through which they were reached.
Mauro Carvalho Chehabbd945e42017-09-27 15:54:43 -040091 * @stack.entity: pointer to &struct media_entity at the graph.
92 * @stack.link: pointer to &struct list_head.
Sakari Ailus29d8da02015-12-16 11:32:28 -020093 * @ent_enum: Visited entities
Sakari Ailus434257f2015-12-16 11:32:20 -020094 * @top: The top of the stack
95 */
Sakari Ailus20b85222016-11-21 14:48:30 -020096struct media_graph {
Sakari Ailus82c68292015-12-16 11:32:19 -020097 struct {
98 struct media_entity *entity;
99 struct list_head *link;
100 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
101
Sakari Ailus29d8da02015-12-16 11:32:28 -0200102 struct media_entity_enum ent_enum;
Sakari Ailus82c68292015-12-16 11:32:19 -0200103 int top;
104};
105
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300106/**
Sakari Ailus5dd87752015-12-16 11:32:21 -0200107 * struct media_pipeline - Media pipeline related information
108 *
Sakari Ailus74a413302015-12-16 11:32:29 -0200109 * @streaming_count: Streaming start count - streaming stop count
110 * @graph: Media graph walk during pipeline start / stop
Sakari Ailus5dd87752015-12-16 11:32:21 -0200111 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300112struct media_pipeline {
Sakari Ailus74a413302015-12-16 11:32:29 -0200113 int streaming_count;
Sakari Ailus20b85222016-11-21 14:48:30 -0200114 struct media_graph graph;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300115};
116
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300117/**
118 * struct media_link - A link object part of a media graph.
119 *
120 * @graph_obj: Embedded structure containing the media object common data
121 * @list: Linked list associated with an entity or an interface that
122 * owns the link.
123 * @gobj0: Part of a union. Used to get the pointer for the first
124 * graph_object of the link.
125 * @source: Part of a union. Used only if the first object (gobj0) is
126 * a pad. In that case, it represents the source pad.
127 * @intf: Part of a union. Used only if the first object (gobj0) is
128 * an interface.
129 * @gobj1: Part of a union. Used to get the pointer for the second
130 * graph_object of the link.
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300131 * @sink: Part of a union. Used only if the second object (gobj1) is
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300132 * a pad. In that case, it represents the sink pad.
133 * @entity: Part of a union. Used only if the second object (gobj1) is
134 * an entity.
135 * @reverse: Pointer to the link for the reverse direction of a pad to pad
136 * link.
137 * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300138 * @is_backlink: Indicate if the link is a backlink.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300139 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300140struct media_link {
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300141 struct media_gobj graph_obj;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300142 struct list_head list;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300143 union {
144 struct media_gobj *gobj0;
145 struct media_pad *source;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300146 struct media_interface *intf;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300147 };
148 union {
149 struct media_gobj *gobj1;
150 struct media_pad *sink;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300151 struct media_entity *entity;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300152 };
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300153 struct media_link *reverse;
154 unsigned long flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300155 bool is_backlink;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300156};
157
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300158/**
159 * struct media_pad - A media pad graph object.
160 *
161 * @graph_obj: Embedded structure containing the media object common data
162 * @entity: Entity this pad belongs to
163 * @index: Pad index in the entity pads array, numbered from 0 to n
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300164 * @flags: Pad flags, as defined in
165 * :ref:`include/uapi/linux/media.h <media_header>`
166 * (seek for ``MEDIA_PAD_FL_*``)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300167 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300168struct media_pad {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300169 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300170 struct media_entity *entity;
171 u16 index;
172 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300173};
174
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300175/**
176 * struct media_entity_operations - Media entity operations
Niklas Söderlundae45cd52017-06-15 06:17:25 -0300177 * @get_fwnode_pad: Return the pad number based on a fwnode endpoint or
178 * a negative value on error. This operation can be used
179 * to map a fwnode to a media pad number. Optional.
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300180 * @link_setup: Notify the entity of link changes. The operation can
181 * return an error, in which case link setup will be
182 * cancelled. Optional.
183 * @link_validate: Return whether a link is valid from the entity point of
Sakari Ailus20b85222016-11-21 14:48:30 -0200184 * view. The media_pipeline_start() function
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300185 * validates all links by calling this operation. Optional.
Mauro Carvalho Chehab5ed470f2016-04-06 10:55:25 -0300186 *
Mauro Carvalho Chehab5b8700e2016-07-20 09:22:38 -0300187 * .. note::
188 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300189 * Those these callbacks are called with struct &media_device.graph_mutex
Mauro Carvalho Chehab5b8700e2016-07-20 09:22:38 -0300190 * mutex held.
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300191 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300192struct media_entity_operations {
Niklas Söderlundae45cd52017-06-15 06:17:25 -0300193 int (*get_fwnode_pad)(struct fwnode_endpoint *endpoint);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300194 int (*link_setup)(struct media_entity *entity,
195 const struct media_pad *local,
196 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300197 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300198};
199
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300200/**
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300201 * enum media_entity_type - Media entity type
202 *
203 * @MEDIA_ENTITY_TYPE_BASE:
204 * The entity isn't embedded in another subsystem structure.
205 * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:
206 * The entity is embedded in a struct video_device instance.
207 * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:
208 * The entity is embedded in a struct v4l2_subdev instance.
209 *
210 * Media entity objects are often not instantiated directly, but the media
211 * entity structure is inherited by (through embedding) other subsystem-specific
212 * structures. The media entity type identifies the type of the subclass
213 * structure that implements a media entity instance.
214 *
215 * This allows runtime type identification of media entities and safe casting to
216 * the correct object type. For instance, a media entity structure instance
217 * embedded in a v4l2_subdev structure instance will have the type
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300218 * %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300219 * structure using the container_of() macro.
220 */
221enum media_entity_type {
222 MEDIA_ENTITY_TYPE_BASE,
223 MEDIA_ENTITY_TYPE_VIDEO_DEVICE,
224 MEDIA_ENTITY_TYPE_V4L2_SUBDEV,
225};
226
227/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300228 * struct media_entity - A media entity graph object.
229 *
230 * @graph_obj: Embedded structure containing the media object common data.
231 * @name: Entity name.
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300232 * @obj_type: Type of the object that implements the media_entity.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300233 * @function: Entity main function, as defined in
234 * :ref:`include/uapi/linux/media.h <media_header>`
235 * (seek for ``MEDIA_ENT_F_*``)
236 * @flags: Entity flags, as defined in
237 * :ref:`include/uapi/linux/media.h <media_header>`
238 * (seek for ``MEDIA_ENT_FL_*``)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300239 * @num_pads: Number of sink and source pads.
240 * @num_links: Total number of links, forward and back, enabled and disabled.
241 * @num_backlinks: Number of backlinks
Sakari Ailus665faa92015-12-16 11:32:17 -0200242 * @internal_idx: An unique internal entity specific number. The numbers are
243 * re-used if entities are unregistered or registered again.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300244 * @pads: Pads array with the size defined by @num_pads.
245 * @links: List of data links.
246 * @ops: Entity operations.
247 * @stream_count: Stream count for the entity.
248 * @use_count: Use count for the entity.
249 * @pipe: Pipeline this entity belongs to.
250 * @info: Union with devnode information. Kept just for backward
251 * compatibility.
Mauro Carvalho Chehabbd945e42017-09-27 15:54:43 -0400252 * @info.dev: Contains device major and minor info.
253 * @info.dev.major: device node major, if the device is a devnode.
254 * @info.dev.minor: device node minor, if the device is a devnode.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300255 * @major: Devnode major number (zero if not applicable). Kept just
256 * for backward compatibility.
257 * @minor: Devnode minor number (zero if not applicable). Kept just
258 * for backward compatibility.
259 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300260 * .. note::
261 *
262 * @stream_count and @use_count reference counts must never be
263 * negative, but are signed integers on purpose: a simple ``WARN_ON(<0)``
264 * check can be used to detect reference count bugs that would make them
265 * negative.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300266 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300267struct media_entity {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300268 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300269 const char *name;
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300270 enum media_entity_type obj_type;
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300271 u32 function;
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300272 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300273
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300274 u16 num_pads;
275 u16 num_links;
276 u16 num_backlinks;
Sakari Ailus665faa92015-12-16 11:32:17 -0200277 int internal_idx;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300278
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300279 struct media_pad *pads;
280 struct list_head links;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300281
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300282 const struct media_entity_operations *ops;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300283
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300284 int stream_count;
285 int use_count;
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300286
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300287 struct media_pipeline *pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300288
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300289 union {
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300290 struct {
291 u32 major;
292 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300293 } dev;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300294 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300295};
296
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300297/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300298 * struct media_interface - A media interface graph object.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300299 *
300 * @graph_obj: embedded graph object
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300301 * @links: List of links pointing to graph entities
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300302 * @type: Type of the interface as defined in
303 * :ref:`include/uapi/linux/media.h <media_header>`
304 * (seek for ``MEDIA_INTF_T_*``)
305 * @flags: Interface flags as defined in
306 * :ref:`include/uapi/linux/media.h <media_header>`
307 * (seek for ``MEDIA_INTF_FL_*``)
308 *
309 * .. note::
310 *
311 * Currently, no flags for &media_interface is defined.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300312 */
313struct media_interface {
314 struct media_gobj graph_obj;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300315 struct list_head links;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300316 u32 type;
317 u32 flags;
318};
319
320/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300321 * struct media_intf_devnode - A media interface via a device node.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300322 *
323 * @intf: embedded interface object
324 * @major: Major number of a device node
325 * @minor: Minor number of a device node
326 */
327struct media_intf_devnode {
328 struct media_interface intf;
Mauro Carvalho Chehabc398bb62015-08-23 08:28:21 -0300329
330 /* Should match the fields at media_v2_intf_devnode */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300331 u32 major;
332 u32 minor;
333};
334
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200335/**
336 * media_entity_id() - return the media entity graph object id
337 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300338 * @entity: pointer to &media_entity
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200339 */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300340static inline u32 media_entity_id(struct media_entity *entity)
341{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300342 return entity->graph_obj.id;
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300343}
344
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200345/**
346 * media_type() - return the media object type
347 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300348 * @gobj: Pointer to the struct &media_gobj graph object
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200349 */
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300350static inline enum media_gobj_type media_type(struct media_gobj *gobj)
351{
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200352 return gobj->id >> MEDIA_BITS_PER_ID;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300353}
354
Mauro Carvalho Chehab630c0e82015-12-16 15:15:18 -0200355/**
356 * media_id() - return the media object ID
357 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300358 * @gobj: Pointer to the struct &media_gobj graph object
Mauro Carvalho Chehab630c0e82015-12-16 15:15:18 -0200359 */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200360static inline u32 media_id(struct media_gobj *gobj)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300361{
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200362 return gobj->id & MEDIA_ID_MASK;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300363}
364
Mauro Carvalho Chehab630c0e82015-12-16 15:15:18 -0200365/**
366 * media_gobj_gen_id() - encapsulates type and ID on at the object ID
367 *
368 * @type: object type as define at enum &media_gobj_type.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300369 * @local_id: next ID, from struct &media_device.id.
Mauro Carvalho Chehab630c0e82015-12-16 15:15:18 -0200370 */
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200371static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300372{
373 u32 id;
374
Mauro Carvalho Chehab05b3b772015-12-16 14:28:01 -0200375 id = type << MEDIA_BITS_PER_ID;
376 id |= local_id & MEDIA_ID_MASK;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300377
378 return id;
379}
380
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200381/**
Laurent Pinchart45b46872016-02-29 08:45:45 -0300382 * is_media_entity_v4l2_video_device() - Check if the entity is a video_device
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200383 * @entity: pointer to entity
384 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300385 * Return: %true if the entity is an instance of a video_device object and can
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300386 * safely be cast to a struct video_device using the container_of() macro, or
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300387 * %false otherwise.
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200388 */
Laurent Pinchart45b46872016-02-29 08:45:45 -0300389static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity)
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300390{
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300391 return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300392}
393
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200394/**
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300395 * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200396 * @entity: pointer to entity
397 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300398 * Return: %true if the entity is an instance of a &v4l2_subdev object and can
399 * safely be cast to a struct &v4l2_subdev using the container_of() macro, or
400 * %false otherwise.
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200401 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300402static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
403{
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300404 return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300405}
406
Mauro Carvalho Chehab92777992015-12-16 13:53:04 -0200407/**
408 * __media_entity_enum_init - Initialise an entity enumeration
409 *
410 * @ent_enum: Entity enumeration to be initialised
411 * @idx_max: Maximum number of entities in the enumeration
412 *
413 * Return: Returns zero on success or a negative error code.
414 */
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200415__must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
416 int idx_max);
Mauro Carvalho Chehab92777992015-12-16 13:53:04 -0200417
418/**
419 * media_entity_enum_cleanup - Release resources of an entity enumeration
420 *
421 * @ent_enum: Entity enumeration to be released
422 */
423void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300424
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200425/**
426 * media_entity_enum_zero - Clear the entire enum
427 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200428 * @ent_enum: Entity enumeration to be cleared
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300429 */
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200430static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
431{
432 bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
433}
434
435/**
436 * media_entity_enum_set - Mark a single entity in the enum
437 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200438 * @ent_enum: Entity enumeration
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200439 * @entity: Entity to be marked
440 */
441static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
442 struct media_entity *entity)
443{
444 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
445 return;
446
447 __set_bit(entity->internal_idx, ent_enum->bmap);
448}
449
450/**
451 * media_entity_enum_clear - Unmark a single entity in the enum
452 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200453 * @ent_enum: Entity enumeration
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200454 * @entity: Entity to be unmarked
455 */
456static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
457 struct media_entity *entity)
458{
459 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
460 return;
461
462 __clear_bit(entity->internal_idx, ent_enum->bmap);
463}
464
465/**
466 * media_entity_enum_test - Test whether the entity is marked
467 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200468 * @ent_enum: Entity enumeration
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200469 * @entity: Entity to be tested
470 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300471 * Returns %true if the entity was marked.
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200472 */
473static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
474 struct media_entity *entity)
475{
476 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
477 return true;
478
479 return test_bit(entity->internal_idx, ent_enum->bmap);
480}
481
482/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300483 * media_entity_enum_test_and_set - Test whether the entity is marked,
484 * and mark it
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200485 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200486 * @ent_enum: Entity enumeration
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200487 * @entity: Entity to be tested
488 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300489 * Returns %true if the entity was marked, and mark it before doing so.
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200490 */
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200491static inline bool
492media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,
493 struct media_entity *entity)
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200494{
495 if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
496 return true;
497
498 return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
499}
500
501/**
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200502 * media_entity_enum_empty - Test whether the entire enum is empty
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200503 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200504 * @ent_enum: Entity enumeration
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200505 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300506 * Return: %true if the entity was empty.
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200507 */
508static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
509{
510 return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
511}
512
513/**
514 * media_entity_enum_intersects - Test whether two enums intersect
515 *
Mauro Carvalho Chehab03e49332015-12-16 13:58:31 -0200516 * @ent_enum1: First entity enumeration
517 * @ent_enum2: Second entity enumeration
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200518 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300519 * Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect,
520 * otherwise %false.
Sakari Ailusc8d54cd2015-12-16 11:44:32 -0200521 */
522static inline bool media_entity_enum_intersects(
523 struct media_entity_enum *ent_enum1,
524 struct media_entity_enum *ent_enum2)
525{
526 WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
527
528 return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
529 min(ent_enum1->idx_max, ent_enum2->idx_max));
530}
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300531
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300532/**
533 * gobj_to_entity - returns the struct &media_entity pointer from the
534 * @gobj contained on it.
535 *
536 * @gobj: Pointer to the struct &media_gobj graph object
537 */
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300538#define gobj_to_entity(gobj) \
539 container_of(gobj, struct media_entity, graph_obj)
540
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300541/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300542 * gobj_to_pad - returns the struct &media_pad pointer from the
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300543 * @gobj contained on it.
544 *
545 * @gobj: Pointer to the struct &media_gobj graph object
546 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300547#define gobj_to_pad(gobj) \
548 container_of(gobj, struct media_pad, graph_obj)
549
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300550/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300551 * gobj_to_link - returns the struct &media_link pointer from the
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300552 * @gobj contained on it.
553 *
554 * @gobj: Pointer to the struct &media_gobj graph object
555 */
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300556#define gobj_to_link(gobj) \
557 container_of(gobj, struct media_link, graph_obj)
558
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300559/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300560 * gobj_to_intf - returns the struct &media_interface pointer from the
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300561 * @gobj contained on it.
562 *
563 * @gobj: Pointer to the struct &media_gobj graph object
564 */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300565#define gobj_to_intf(gobj) \
566 container_of(gobj, struct media_interface, graph_obj)
567
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300568/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300569 * intf_to_devnode - returns the struct media_intf_devnode pointer from the
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300570 * @intf contained on it.
571 *
572 * @intf: Pointer to struct &media_intf_devnode
573 */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300574#define intf_to_devnode(intf) \
575 container_of(intf, struct media_intf_devnode, intf)
576
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200577/**
578 * media_gobj_create - Initialize a graph object
579 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300580 * @mdev: Pointer to the &media_device that contains the object
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200581 * @type: Type of the object
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300582 * @gobj: Pointer to the struct &media_gobj graph object
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200583 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300584 * This routine initializes the embedded struct &media_gobj inside a
585 * media graph object. It is called automatically if ``media_*_create``
586 * function calls are used. However, if the object (entity, link, pad,
587 * interface) is embedded on some other object, this function should be
588 * called before registering the object at the media controller.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200589 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200590void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300591 enum media_gobj_type type,
592 struct media_gobj *gobj);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200593
594/**
595 * media_gobj_destroy - Stop using a graph object on a media device
596 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300597 * @gobj: Pointer to the struct &media_gobj graph object
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200598 *
599 * This should be called by all routines like media_device_unregister()
600 * that remove/destroy media graph objects.
601 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200602void media_gobj_destroy(struct media_gobj *gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300603
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200604/**
605 * media_entity_pads_init() - Initialize the entity pads
606 *
607 * @entity: entity where the pads belong
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200608 * @num_pads: total number of sink and source pads
609 * @pads: Array of @num_pads pads.
610 *
611 * The pads array is managed by the entity driver and passed to
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300612 * media_entity_pads_init() where its pointer will be stored in the
613 * &media_entity structure.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200614 *
615 * If no pads are needed, drivers could either directly fill
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300616 * &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200617 * this function that will do the same.
618 *
619 * As the number of pads is known in advance, the pads array is not allocated
620 * dynamically but is managed by the entity driver. Most drivers will embed the
621 * pads array in a driver-specific structure, avoiding dynamic allocation.
622 *
623 * Drivers must set the direction of every pad in the pads array before calling
624 * media_entity_pads_init(). The function will initialize the other pads fields.
625 */
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200626int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300627 struct media_pad *pads);
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200628
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200629/**
630 * media_entity_cleanup() - free resources associated with an entity
631 *
632 * @entity: entity where the pads belong
633 *
634 * This function must be called during the cleanup phase after unregistering
635 * the entity (currently, it does nothing).
636 */
Sakari Ailus35801122018-01-09 05:20:34 -0500637#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
638static inline void media_entity_cleanup(struct media_entity *entity) {}
639#else
640#define media_entity_cleanup(entity) do { } while (false)
641#endif
Laurent Pincharte02188c2010-08-25 09:00:41 -0300642
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200643/**
644 * media_create_pad_link() - creates a link between two entities.
645 *
646 * @source: pointer to &media_entity of the source pad.
647 * @source_pad: number of the source pad in the pads array
648 * @sink: pointer to &media_entity of the sink pad.
649 * @sink_pad: number of the sink pad in the pads array.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300650 * @flags: Link flags, as defined in
651 * :ref:`include/uapi/linux/media.h <media_header>`
652 * ( seek for ``MEDIA_LNK_FL_*``)
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200653 *
654 * Valid values for flags:
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200655 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300656 * %MEDIA_LNK_FL_ENABLED
657 * Indicates that the link is enabled and can be used to transfer media data.
658 * When two or more links target a sink pad, only one of them can be
659 * enabled at a time.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200660 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300661 * %MEDIA_LNK_FL_IMMUTABLE
662 * Indicates that the link enabled state can't be modified at runtime. If
663 * %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be
664 * set, since an immutable link is always enabled.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200665 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300666 * .. note::
667 *
668 * Before calling this function, media_entity_pads_init() and
669 * media_device_register_entity() should be called previously for both ends.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200670 */
Mauro Carvalho Chehab77328042015-09-04 16:08:24 -0300671__must_check int media_create_pad_link(struct media_entity *source,
672 u16 source_pad, struct media_entity *sink,
673 u16 sink_pad, u32 flags);
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200674
675/**
676 * media_create_pad_links() - creates a link between two entities.
677 *
678 * @mdev: Pointer to the media_device that contains the object
679 * @source_function: Function of the source entities. Used only if @source is
680 * NULL.
681 * @source: pointer to &media_entity of the source pad. If NULL, it will use
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300682 * all entities that matches the @sink_function.
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200683 * @source_pad: number of the source pad in the pads array
684 * @sink_function: Function of the sink entities. Used only if @sink is NULL.
685 * @sink: pointer to &media_entity of the sink pad. If NULL, it will use
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300686 * all entities that matches the @sink_function.
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200687 * @sink_pad: number of the sink pad in the pads array.
688 * @flags: Link flags, as defined in include/uapi/linux/media.h.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300689 * @allow_both_undefined: if %true, then both @source and @sink can be NULL.
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200690 * In such case, it will create a crossbar between all entities that
691 * matches @source_function to all entities that matches @sink_function.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300692 * If %false, it will return 0 and won't create any link if both @source
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200693 * and @sink are NULL.
694 *
695 * Valid values for flags:
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300696 *
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200697 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
698 * used to transfer media data. If multiple links are created and this
699 * flag is passed as an argument, only the first created link will have
700 * this flag.
701 *
702 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
703 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
704 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
705 * always enabled.
706 *
707 * It is common for some devices to have multiple source and/or sink entities
708 * of the same type that should be linked. While media_create_pad_link()
709 * creates link by link, this function is meant to allow 1:n, n:1 and even
710 * cross-bar (n:n) links.
711 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300712 * .. note::
713 *
714 * Before calling this function, media_entity_pads_init() and
715 * media_device_register_entity() should be called previously for the
716 * entities to be linked.
Mauro Carvalho Chehabb01cc9c2015-12-30 09:45:48 -0200717 */
718int media_create_pad_links(const struct media_device *mdev,
719 const u32 source_function,
720 struct media_entity *source,
721 const u16 source_pad,
722 const u32 sink_function,
723 struct media_entity *sink,
724 const u16 sink_pad,
725 u32 flags,
726 const bool allow_both_undefined);
727
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300728void __media_entity_remove_links(struct media_entity *entity);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200729
730/**
731 * media_entity_remove_links() - remove all links associated with an entity
732 *
733 * @entity: pointer to &media_entity
734 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300735 * .. note::
736 *
737 * This is called automatically when an entity is unregistered via
738 * media_device_register_entity().
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200739 */
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300740void media_entity_remove_links(struct media_entity *entity);
741
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200742/**
743 * __media_entity_setup_link - Configure a media link without locking
744 * @link: The link being configured
745 * @flags: Link configuration flags
746 *
747 * The bulk of link setup is handled by the two entities connected through the
748 * link. This function notifies both entities of the link configuration change.
749 *
750 * If the link is immutable or if the current and new configuration are
751 * identical, return immediately.
752 *
753 * The user is expected to hold link->source->parent->mutex. If not,
754 * media_entity_setup_link() should be used instead.
755 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300756int __media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200757
758/**
759 * media_entity_setup_link() - changes the link flags properties in runtime
760 *
761 * @link: pointer to &media_link
762 * @flags: the requested new link flags
763 *
764 * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
765 * flag to enable/disable a link. Links marked with the
766 * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
767 *
768 * When a link is enabled or disabled, the media framework calls the
769 * link_setup operation for the two entities at the source and sink of the
770 * link, in that order. If the second link_setup call fails, another
771 * link_setup call is made on the first entity to restore the original link
772 * flags.
773 *
774 * Media device drivers can be notified of link setup operations by setting the
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300775 * &media_device.link_notify pointer to a callback function. If provided, the
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200776 * notification callback will be called before enabling and after disabling
777 * links.
778 *
779 * Entity drivers must implement the link_setup operation if any of their links
780 * is non-immutable. The operation must either configure the hardware or store
781 * the configuration information to be applied later.
782 *
783 * Link configuration must not have any side effect on other links. If an
784 * enabled link at a sink pad prevents another link at the same pad from
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300785 * being enabled, the link_setup operation must return %-EBUSY and can't
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200786 * implicitly disable the first enabled link.
787 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -0300788 * .. note::
789 *
790 * The valid values of the flags for the link is the same as described
791 * on media_create_pad_link(), for pad to pad links or the same as described
792 * on media_create_intf_link(), for interface to entity links.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200793 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300794int media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200795
796/**
797 * media_entity_find_link - Find a link between two pads
798 * @source: Source pad
799 * @sink: Sink pad
800 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300801 * Return: returns a pointer to the link between the two entities. If no
802 * such link exists, return %NULL.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200803 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300804struct media_link *media_entity_find_link(struct media_pad *source,
805 struct media_pad *sink);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200806
807/**
808 * media_entity_remote_pad - Find the pad at the remote end of a link
809 * @pad: Pad at the local end of the link
810 *
811 * Search for a remote pad connected to the given pad by iterating over all
812 * links originating or terminating at that pad until an enabled link is found.
813 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300814 * Return: returns a pointer to the pad at the remote end of the first found
815 * enabled link, or %NULL if no enabled link has been found.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200816 */
Todor Tomov6538b022017-07-03 08:08:11 -0400817struct media_pad *media_entity_remote_pad(const struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300818
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200819/**
820 * media_entity_get - Get a reference to the parent module
821 *
822 * @entity: The entity
823 *
824 * Get a reference to the parent media device module.
825 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300826 * The function will return immediately if @entity is %NULL.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200827 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300828 * Return: returns a pointer to the entity on success or %NULL on failure.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200829 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300830struct media_entity *media_entity_get(struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200831
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300832/**
Niklas Söderlundd295c6a2017-06-15 06:17:26 -0300833 * media_entity_get_fwnode_pad - Get pad number from fwnode
834 *
835 * @entity: The entity
836 * @fwnode: Pointer to the fwnode_handle which should be used to find the pad
837 * @direction_flags: Expected direction of the pad, as defined in
838 * :ref:`include/uapi/linux/media.h <media_header>`
839 * (seek for ``MEDIA_PAD_FL_*``)
840 *
841 * This function can be used to resolve the media pad number from
842 * a fwnode. This is useful for devices which use more complex
843 * mappings of media pads.
844 *
845 * If the entity dose not implement the get_fwnode_pad() operation
846 * then this function searches the entity for the first pad that
847 * matches the @direction_flags.
848 *
849 * Return: returns the pad number on success or a negative error code.
850 */
851int media_entity_get_fwnode_pad(struct media_entity *entity,
852 struct fwnode_handle *fwnode,
853 unsigned long direction_flags);
854
855/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200856 * media_graph_walk_init - Allocate resources used by graph walk.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300857 *
858 * @graph: Media graph structure that will be used to walk the graph
859 * @mdev: Pointer to the &media_device that contains the object
860 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200861__must_check int media_graph_walk_init(
862 struct media_graph *graph, struct media_device *mdev);
Mauro Carvalho Chehabaa360d32015-12-16 13:56:14 -0200863
864/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200865 * media_graph_walk_cleanup - Release resources used by graph walk.
Mauro Carvalho Chehabaa360d32015-12-16 13:56:14 -0200866 *
867 * @graph: Media graph structure that will be used to walk the graph
868 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200869void media_graph_walk_cleanup(struct media_graph *graph);
Sakari Ailuse03d2202015-12-16 11:32:22 -0200870
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200871/**
872 * media_entity_put - Release the reference to the parent module
873 *
874 * @entity: The entity
875 *
876 * Release the reference count acquired by media_entity_get().
877 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300878 * The function will return immediately if @entity is %NULL.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200879 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300880void media_entity_put(struct media_entity *entity);
881
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200882/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200883 * media_graph_walk_start - Start walking the media graph at a
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300884 * given entity
885 *
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200886 * @graph: Media graph structure that will be used to walk the graph
887 * @entity: Starting entity
888 *
Sakari Ailus20b85222016-11-21 14:48:30 -0200889 * Before using this function, media_graph_walk_init() must be
Sakari Ailuse03d2202015-12-16 11:32:22 -0200890 * used to allocate resources used for walking the graph. This
891 * function initializes the graph traversal structure to walk the
892 * entities graph starting at the given entity. The traversal
893 * structure must not be modified by the caller during graph
894 * traversal. After the graph walk, the resources must be released
Sakari Ailus20b85222016-11-21 14:48:30 -0200895 * using media_graph_walk_cleanup().
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200896 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200897void media_graph_walk_start(struct media_graph *graph,
898 struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200899
900/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200901 * media_graph_walk_next - Get the next entity in the graph
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200902 * @graph: Media graph structure
903 *
904 * Perform a depth-first traversal of the given media entities graph.
905 *
906 * The graph structure must have been previously initialized with a call to
Sakari Ailus20b85222016-11-21 14:48:30 -0200907 * media_graph_walk_start().
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200908 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300909 * Return: returns the next entity in the graph or %NULL if the whole graph
910 * have been traversed.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200911 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200912struct media_entity *media_graph_walk_next(struct media_graph *graph);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200913
914/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200915 * media_pipeline_start - Mark a pipeline as streaming
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200916 * @entity: Starting entity
917 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
918 *
919 * Mark all entities connected to a given entity through enabled links, either
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300920 * directly or indirectly, as streaming. The given pipeline object is assigned
921 * to every entity in the pipeline and stored in the media_entity pipe field.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200922 *
923 * Calls to this function can be nested, in which case the same number of
Sakari Ailus20b85222016-11-21 14:48:30 -0200924 * media_pipeline_stop() calls will be required to stop streaming. The
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200925 * pipeline pointer must be identical for all nested calls to
Sakari Ailus20b85222016-11-21 14:48:30 -0200926 * media_pipeline_start().
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200927 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200928__must_check int media_pipeline_start(struct media_entity *entity,
929 struct media_pipeline *pipe);
Shuah Khanfb49f202016-02-11 21:41:24 -0200930/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200931 * __media_pipeline_start - Mark a pipeline as streaming
Shuah Khanfb49f202016-02-11 21:41:24 -0200932 *
933 * @entity: Starting entity
934 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
935 *
Sakari Ailus20b85222016-11-21 14:48:30 -0200936 * ..note:: This is the non-locking version of media_pipeline_start()
Shuah Khanfb49f202016-02-11 21:41:24 -0200937 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200938__must_check int __media_pipeline_start(struct media_entity *entity,
939 struct media_pipeline *pipe);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200940
941/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200942 * media_pipeline_stop - Mark a pipeline as not streaming
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200943 * @entity: Starting entity
944 *
945 * Mark all entities connected to a given entity through enabled links, either
946 * directly or indirectly, as not streaming. The media_entity pipe field is
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300947 * reset to %NULL.
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200948 *
Sakari Ailus20b85222016-11-21 14:48:30 -0200949 * If multiple calls to media_pipeline_start() have been made, the same
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200950 * number of calls to this function are required to mark the pipeline as not
951 * streaming.
952 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200953void media_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300954
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200955/**
Sakari Ailus20b85222016-11-21 14:48:30 -0200956 * __media_pipeline_stop - Mark a pipeline as not streaming
Shuah Khanfb49f202016-02-11 21:41:24 -0200957 *
958 * @entity: Starting entity
959 *
Sakari Ailus20b85222016-11-21 14:48:30 -0200960 * .. note:: This is the non-locking version of media_pipeline_stop()
Shuah Khanfb49f202016-02-11 21:41:24 -0200961 */
Sakari Ailus20b85222016-11-21 14:48:30 -0200962void __media_pipeline_stop(struct media_entity *entity);
Shuah Khanfb49f202016-02-11 21:41:24 -0200963
964/**
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200965 * media_devnode_create() - creates and initializes a device node interface
966 *
967 * @mdev: pointer to struct &media_device
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300968 * @type: type of the interface, as given by
969 * :ref:`include/uapi/linux/media.h <media_header>`
970 * ( seek for ``MEDIA_INTF_T_*``) macros.
971 * @flags: Interface flags, as defined in
972 * :ref:`include/uapi/linux/media.h <media_header>`
973 * ( seek for ``MEDIA_INTF_FL_*``)
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200974 * @major: Device node major number.
975 * @minor: Device node minor number.
976 *
977 * Return: if succeeded, returns a pointer to the newly allocated
978 * &media_intf_devnode pointer.
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -0300979 *
980 * .. note::
981 *
982 * Currently, no flags for &media_interface is defined.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200983 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300984struct media_intf_devnode *
985__must_check media_devnode_create(struct media_device *mdev,
986 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300987 u32 major, u32 minor);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200988/**
989 * media_devnode_remove() - removes a device node interface
990 *
991 * @devnode: pointer to &media_intf_devnode to be freed.
992 *
993 * When a device node interface is removed, all links to it are automatically
994 * removed.
995 */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300996void media_devnode_remove(struct media_intf_devnode *devnode);
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300997struct media_link *
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200998
999/**
1000 * media_create_intf_link() - creates a link between an entity and an interface
1001 *
1002 * @entity: pointer to %media_entity
1003 * @intf: pointer to %media_interface
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -03001004 * @flags: Link flags, as defined in
1005 * :ref:`include/uapi/linux/media.h <media_header>`
1006 * ( seek for ``MEDIA_LNK_FL_*``)
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -02001007 *
1008 *
1009 * Valid values for flags:
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -02001010 *
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -03001011 * %MEDIA_LNK_FL_ENABLED
1012 * Indicates that the interface is connected to the entity hardware.
1013 * That's the default value for interfaces. An interface may be disabled if
1014 * the hardware is busy due to the usage of some other interface that it is
1015 * currently controlling the hardware.
1016 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -03001017 * A typical example is an hybrid TV device that handle only one type of
1018 * stream on a given time. So, when the digital TV is streaming,
1019 * the V4L2 interfaces won't be enabled, as such device is not able to
1020 * also stream analog TV or radio.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -02001021 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -03001022 * .. note::
1023 *
1024 * Before calling this function, media_devnode_create() should be called for
1025 * the interface and media_device_register_entity() should be called for the
1026 * interface that will be part of the link.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -02001027 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -03001028__must_check media_create_intf_link(struct media_entity *entity,
1029 struct media_interface *intf,
1030 u32 flags);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001031/**
1032 * __media_remove_intf_link() - remove a single interface link
1033 *
1034 * @link: pointer to &media_link.
1035 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -03001036 * .. note:: This is an unlocked version of media_remove_intf_link()
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001037 */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -03001038void __media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001039
1040/**
1041 * media_remove_intf_link() - remove a single interface link
1042 *
1043 * @link: pointer to &media_link.
1044 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -03001045 * .. note:: Prefer to use this one, instead of __media_remove_intf_link()
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001046 */
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -03001047void media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001048
1049/**
1050 * __media_remove_intf_links() - remove all links associated with an interface
1051 *
1052 * @intf: pointer to &media_interface
1053 *
Mauro Carvalho Chehab74604b72016-07-17 09:18:03 -03001054 * .. note:: This is an unlocked version of media_remove_intf_links().
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001055 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001056void __media_remove_intf_links(struct media_interface *intf);
Mauro Carvalho Chehab92777992015-12-16 13:53:04 -02001057
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -02001058/**
1059 * media_remove_intf_links() - remove all links associated with an interface
1060 *
1061 * @intf: pointer to &media_interface
1062 *
Mauro Carvalho Chehabf58cad22016-07-20 11:15:31 -03001063 * .. note::
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001064 *
Mauro Carvalho Chehabf58cad22016-07-20 11:15:31 -03001065 * #) This is called automatically when an entity is unregistered via
1066 * media_device_register_entity() and by media_devnode_remove().
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -02001067 *
Mauro Carvalho Chehabf58cad22016-07-20 11:15:31 -03001068 * #) Prefer to use this one, instead of __media_remove_intf_links().
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -02001069 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -03001070void media_remove_intf_links(struct media_interface *intf);
1071
Mauro Carvalho Chehab48a7c4b2016-08-29 16:09:11 -03001072/**
1073 * media_entity_call - Calls a struct media_entity_operations operation on
1074 * an entity
1075 *
1076 * @entity: entity where the @operation will be called
1077 * @operation: type of the operation. Should be the name of a member of
1078 * struct &media_entity_operations.
1079 *
1080 * This helper function will check if @operation is not %NULL. On such case,
1081 * it will issue a call to @operation\(@entity, @args\).
1082 */
1083
Laurent Pinchart97548ed2009-12-09 08:40:03 -03001084#define media_entity_call(entity, operation, args...) \
1085 (((entity)->ops && (entity)->ops->operation) ? \
1086 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
1087
Laurent Pinchart53e269c2009-12-09 08:40:00 -03001088#endif