blob: 0753f3029d063da0d2130c9c10df7f63d7f25dbb [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
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -030026#include <linux/bitops.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 *
58 * @id: Non-zero object ID identifier. The ID should be unique
59 * inside a media_device, as it is composed by
60 * MEDIA_BITS_PER_TYPE to store the type plus
61 * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID
62 * (called as "local ID").
63 *
64 * All objects on the media graph should have this struct embedded
65 */
66struct media_gobj {
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030067 struct media_device *mdev;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030068 u32 id;
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -030069 struct list_head list;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030070};
71
72
Laurent Pincharte02188c2010-08-25 09:00:41 -030073struct media_pipeline {
74};
75
Laurent Pinchart53e269c2009-12-09 08:40:00 -030076struct media_link {
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030077 struct media_gobj graph_obj;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -030078 struct list_head list;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -030079 union {
80 struct media_gobj *gobj0;
81 struct media_pad *source;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -030082 struct media_interface *intf;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -030083 };
84 union {
85 struct media_gobj *gobj1;
86 struct media_pad *sink;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -030087 struct media_entity *entity;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -030088 };
Laurent Pinchart53e269c2009-12-09 08:40:00 -030089 struct media_link *reverse; /* Link in the reverse direction */
90 unsigned long flags; /* Link flags (MEDIA_LNK_FL_*) */
91};
92
93struct media_pad {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -030094 struct media_gobj graph_obj; /* must be first field in struct */
Laurent Pinchart53e269c2009-12-09 08:40:00 -030095 struct media_entity *entity; /* Entity this pad belongs to */
96 u16 index; /* Pad index in the entity pads array */
97 unsigned long flags; /* Pad flags (MEDIA_PAD_FL_*) */
98};
99
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300100/**
101 * struct media_entity_operations - Media entity operations
102 * @link_setup: Notify the entity of link changes. The operation can
103 * return an error, in which case link setup will be
104 * cancelled. Optional.
105 * @link_validate: Return whether a link is valid from the entity point of
106 * view. The media_entity_pipeline_start() function
107 * validates all links by calling this operation. Optional.
108 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300109struct media_entity_operations {
110 int (*link_setup)(struct media_entity *entity,
111 const struct media_pad *local,
112 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300113 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300114};
115
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300116struct media_entity {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300117 struct media_gobj graph_obj; /* must be first field in struct */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300118 const char *name; /* Entity name */
119 u32 type; /* Entity type (MEDIA_ENT_T_*) */
120 u32 revision; /* Entity revision, driver specific */
121 unsigned long flags; /* Entity flags (MEDIA_ENT_FL_*) */
122 u32 group_id; /* Entity group ID */
123
124 u16 num_pads; /* Number of sink and source pads */
125 u16 num_links; /* Number of existing links, both
126 * enabled and disabled */
127 u16 num_backlinks; /* Number of backlinks */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300128
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300129 struct media_pad *pads; /* Pads array (num_pads objects) */
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300130 struct list_head links; /* Pad-to-pad links list */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300131
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300132 const struct media_entity_operations *ops; /* Entity operations */
133
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300134 /* Reference counts must never be negative, but are signed integers on
135 * purpose: a simple WARN_ON(<0) check can be used to detect reference
136 * count bugs that would make them negative.
137 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300138 int stream_count; /* Stream count for the entity. */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300139 int use_count; /* Use count for the entity. */
140
Laurent Pincharte02188c2010-08-25 09:00:41 -0300141 struct media_pipeline *pipe; /* Pipeline this entity belongs to. */
142
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300143 union {
144 /* Node specifications */
145 struct {
146 u32 major;
147 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300148 } dev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300149
150 /* Sub-device specifications */
151 /* Nothing needed yet */
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300152 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300153};
154
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300155/**
156 * struct media_intf_devnode - Define a Kernel API interface
157 *
158 * @graph_obj: embedded graph object
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300159 * @list: Linked list used to find other interfaces that belong
160 * to the same media controller
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300161 * @links: List of links pointing to graph entities
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300162 * @type: Type of the interface as defined at the
163 * uapi/media/media.h header, e. g.
164 * MEDIA_INTF_T_*
165 * @flags: Interface flags as defined at uapi/media/media.h
166 */
167struct media_interface {
168 struct media_gobj graph_obj;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300169 struct list_head links;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300170 u32 type;
171 u32 flags;
172};
173
174/**
175 * struct media_intf_devnode - Define a Kernel API interface via a device node
176 *
177 * @intf: embedded interface object
178 * @major: Major number of a device node
179 * @minor: Minor number of a device node
180 */
181struct media_intf_devnode {
182 struct media_interface intf;
Mauro Carvalho Chehabc398bb62015-08-23 08:28:21 -0300183
184 /* Should match the fields at media_v2_intf_devnode */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300185 u32 major;
186 u32 minor;
187};
188
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300189static inline u32 media_entity_id(struct media_entity *entity)
190{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300191 return entity->graph_obj.id;
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300192}
193
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300194static inline enum media_gobj_type media_type(struct media_gobj *gobj)
195{
196 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
197}
198
199static inline u32 media_localid(struct media_gobj *gobj)
200{
201 return gobj->id & MEDIA_LOCAL_ID_MASK;
202}
203
204static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
205{
206 u32 id;
207
208 id = type << MEDIA_BITS_PER_LOCAL_ID;
209 id |= local_id & MEDIA_LOCAL_ID_MASK;
210
211 return id;
212}
213
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300214static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
215{
216 if (!entity)
217 return false;
218
219 switch (entity->type) {
220 case MEDIA_ENT_T_V4L2_VIDEO:
221 case MEDIA_ENT_T_V4L2_VBI:
222 case MEDIA_ENT_T_V4L2_SWRADIO:
223 return true;
224 default:
225 return false;
226 }
227}
228
229static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
230{
231 if (!entity)
232 return false;
233
234 switch (entity->type) {
235 case MEDIA_ENT_T_V4L2_SUBDEV_UNKNOWN:
236 case MEDIA_ENT_T_V4L2_SUBDEV_SENSOR:
237 case MEDIA_ENT_T_V4L2_SUBDEV_FLASH:
238 case MEDIA_ENT_T_V4L2_SUBDEV_LENS:
239 case MEDIA_ENT_T_V4L2_SUBDEV_DECODER:
240 case MEDIA_ENT_T_V4L2_SUBDEV_TUNER:
241 return true;
242
243 default:
244 return false;
245 }
246}
247
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300248#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300249#define MEDIA_ENTITY_ENUM_MAX_ID 64
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300250
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300251/*
252 * The number of pads can't be bigger than the number of entities,
253 * as the worse-case scenario is to have one entity linked up to
254 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
255 */
256#define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
257
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300258struct media_entity_graph {
259 struct {
260 struct media_entity *entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300261 struct list_head *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300262 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300263
264 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300265 int top;
266};
267
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300268#define gobj_to_entity(gobj) \
269 container_of(gobj, struct media_entity, graph_obj)
270
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300271#define gobj_to_pad(gobj) \
272 container_of(gobj, struct media_pad, graph_obj)
273
274#define gobj_to_link(gobj) \
275 container_of(gobj, struct media_link, graph_obj)
276
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300277#define gobj_to_link(gobj) \
278 container_of(gobj, struct media_link, graph_obj)
279
280#define gobj_to_pad(gobj) \
281 container_of(gobj, struct media_pad, graph_obj)
282
283#define gobj_to_intf(gobj) \
284 container_of(gobj, struct media_interface, graph_obj)
285
286#define intf_to_devnode(intf) \
287 container_of(intf, struct media_intf_devnode, intf)
288
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300289void media_gobj_init(struct media_device *mdev,
290 enum media_gobj_type type,
291 struct media_gobj *gobj);
292void media_gobj_remove(struct media_gobj *gobj);
293
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300294int media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300295 struct media_pad *pads);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300296void media_entity_cleanup(struct media_entity *entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300297
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300298int media_create_pad_link(struct media_entity *source, u16 source_pad,
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300299 struct media_entity *sink, u16 sink_pad, u32 flags);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300300void __media_entity_remove_links(struct media_entity *entity);
301void media_entity_remove_links(struct media_entity *entity);
302
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300303int __media_entity_setup_link(struct media_link *link, u32 flags);
304int media_entity_setup_link(struct media_link *link, u32 flags);
305struct media_link *media_entity_find_link(struct media_pad *source,
306 struct media_pad *sink);
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300307struct media_pad *media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300308
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300309struct media_entity *media_entity_get(struct media_entity *entity);
310void media_entity_put(struct media_entity *entity);
311
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300312void media_entity_graph_walk_start(struct media_entity_graph *graph,
313 struct media_entity *entity);
314struct media_entity *
315media_entity_graph_walk_next(struct media_entity_graph *graph);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300316__must_check int media_entity_pipeline_start(struct media_entity *entity,
317 struct media_pipeline *pipe);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300318void media_entity_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300319
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300320struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
321 u32 type, u32 flags,
322 u32 major, u32 minor,
323 gfp_t gfp_flags);
324void media_devnode_remove(struct media_intf_devnode *devnode);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300325struct media_link *media_create_intf_link(struct media_entity *entity,
326 struct media_interface *intf,
327 u32 flags);
328void media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300329void __media_remove_intf_links(struct media_interface *intf);
330void media_remove_intf_links(struct media_interface *intf);
331
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300332
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300333#define media_entity_call(entity, operation, args...) \
334 (((entity)->ops && (entity)->ops->operation) ? \
335 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
336
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300337#endif