blob: 32fef503d95066b71771052c1d29c841a5164347 [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 *
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
Laurent Pincharte02188c2010-08-25 09:00:41 -030074struct media_pipeline {
75};
76
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030077/**
78 * struct media_link - A link object part of a media graph.
79 *
80 * @graph_obj: Embedded structure containing the media object common data
81 * @list: Linked list associated with an entity or an interface that
82 * owns the link.
83 * @gobj0: Part of a union. Used to get the pointer for the first
84 * graph_object of the link.
85 * @source: Part of a union. Used only if the first object (gobj0) is
86 * a pad. In that case, it represents the source pad.
87 * @intf: Part of a union. Used only if the first object (gobj0) is
88 * an interface.
89 * @gobj1: Part of a union. Used to get the pointer for the second
90 * graph_object of the link.
91 * @source: Part of a union. Used only if the second object (gobj1) is
92 * a pad. In that case, it represents the sink pad.
93 * @entity: Part of a union. Used only if the second object (gobj1) is
94 * an entity.
95 * @reverse: Pointer to the link for the reverse direction of a pad to pad
96 * link.
97 * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -030098 * @is_backlink: Indicate if the link is a backlink.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030099 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300100struct media_link {
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300101 struct media_gobj graph_obj;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300102 struct list_head list;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300103 union {
104 struct media_gobj *gobj0;
105 struct media_pad *source;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300106 struct media_interface *intf;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300107 };
108 union {
109 struct media_gobj *gobj1;
110 struct media_pad *sink;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300111 struct media_entity *entity;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300112 };
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300113 struct media_link *reverse;
114 unsigned long flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300115 bool is_backlink;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300116};
117
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300118/**
119 * struct media_pad - A media pad graph object.
120 *
121 * @graph_obj: Embedded structure containing the media object common data
122 * @entity: Entity this pad belongs to
123 * @index: Pad index in the entity pads array, numbered from 0 to n
124 * @flags: Pad flags, as defined in uapi/media.h (MEDIA_PAD_FL_*)
125 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300126struct media_pad {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300127 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300128 struct media_entity *entity;
129 u16 index;
130 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300131};
132
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300133/**
134 * struct media_entity_operations - Media entity operations
135 * @link_setup: Notify the entity of link changes. The operation can
136 * return an error, in which case link setup will be
137 * cancelled. Optional.
138 * @link_validate: Return whether a link is valid from the entity point of
139 * view. The media_entity_pipeline_start() function
140 * validates all links by calling this operation. Optional.
141 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300142struct media_entity_operations {
143 int (*link_setup)(struct media_entity *entity,
144 const struct media_pad *local,
145 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300146 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300147};
148
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300149/**
150 * struct media_entity - A media entity graph object.
151 *
152 * @graph_obj: Embedded structure containing the media object common data.
153 * @name: Entity name.
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300154 * @function: Entity main function, as defined in uapi/media.h
155 * (MEDIA_ENT_F_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300156 * @revision: Entity revision - OBSOLETE - should be removed soon.
157 * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
158 * @group_id: Entity group ID - OBSOLETE - should be removed soon.
159 * @num_pads: Number of sink and source pads.
160 * @num_links: Total number of links, forward and back, enabled and disabled.
161 * @num_backlinks: Number of backlinks
162 * @pads: Pads array with the size defined by @num_pads.
163 * @links: List of data links.
164 * @ops: Entity operations.
165 * @stream_count: Stream count for the entity.
166 * @use_count: Use count for the entity.
167 * @pipe: Pipeline this entity belongs to.
168 * @info: Union with devnode information. Kept just for backward
169 * compatibility.
170 * @major: Devnode major number (zero if not applicable). Kept just
171 * for backward compatibility.
172 * @minor: Devnode minor number (zero if not applicable). Kept just
173 * for backward compatibility.
174 *
175 * NOTE: @stream_count and @use_count reference counts must never be
176 * negative, but are signed integers on purpose: a simple WARN_ON(<0) check
177 * can be used to detect reference count bugs that would make them negative.
178 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300179struct media_entity {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300180 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300181 const char *name;
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300182 u32 function;
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300183 u32 revision;
184 unsigned long flags;
185 u32 group_id;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300186
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300187 u16 num_pads;
188 u16 num_links;
189 u16 num_backlinks;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300190
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300191 struct media_pad *pads;
192 struct list_head links;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300193
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300194 const struct media_entity_operations *ops;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300195
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300196 /* Reference counts must never be negative, but are signed integers on
197 * purpose: a simple WARN_ON(<0) check can be used to detect reference
198 * count bugs that would make them negative.
199 */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300200 int stream_count;
201 int use_count;
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300202
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300203 struct media_pipeline *pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300204
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300205 union {
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300206 struct {
207 u32 major;
208 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300209 } dev;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300210 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300211};
212
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300213/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300214 * struct media_interface - A media interface graph object.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300215 *
216 * @graph_obj: embedded graph object
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300217 * @links: List of links pointing to graph entities
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300218 * @type: Type of the interface as defined in the
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300219 * uapi/media/media.h header, e. g.
220 * MEDIA_INTF_T_*
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300221 * @flags: Interface flags as defined in uapi/media/media.h
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300222 */
223struct media_interface {
224 struct media_gobj graph_obj;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300225 struct list_head links;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300226 u32 type;
227 u32 flags;
228};
229
230/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300231 * struct media_intf_devnode - A media interface via a device node.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300232 *
233 * @intf: embedded interface object
234 * @major: Major number of a device node
235 * @minor: Minor number of a device node
236 */
237struct media_intf_devnode {
238 struct media_interface intf;
Mauro Carvalho Chehabc398bb62015-08-23 08:28:21 -0300239
240 /* Should match the fields at media_v2_intf_devnode */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300241 u32 major;
242 u32 minor;
243};
244
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300245static inline u32 media_entity_id(struct media_entity *entity)
246{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300247 return entity->graph_obj.id;
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300248}
249
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300250static inline enum media_gobj_type media_type(struct media_gobj *gobj)
251{
252 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
253}
254
255static inline u32 media_localid(struct media_gobj *gobj)
256{
257 return gobj->id & MEDIA_LOCAL_ID_MASK;
258}
259
260static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
261{
262 u32 id;
263
264 id = type << MEDIA_BITS_PER_LOCAL_ID;
265 id |= local_id & MEDIA_LOCAL_ID_MASK;
266
267 return id;
268}
269
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300270static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
271{
272 if (!entity)
273 return false;
274
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300275 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200276 case MEDIA_ENT_F_IO_V4L:
277 case MEDIA_ENT_F_IO_VBI:
278 case MEDIA_ENT_F_IO_SWRADIO:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300279 return true;
280 default:
281 return false;
282 }
283}
284
285static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
286{
287 if (!entity)
288 return false;
289
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300290 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200291 case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
292 case MEDIA_ENT_F_CAM_SENSOR:
293 case MEDIA_ENT_F_FLASH:
294 case MEDIA_ENT_F_LENS:
295 case MEDIA_ENT_F_ATV_DECODER:
296 case MEDIA_ENT_F_TUNER:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300297 return true;
298
299 default:
300 return false;
301 }
302}
303
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300304#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300305#define MEDIA_ENTITY_ENUM_MAX_ID 64
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300306
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300307/*
308 * The number of pads can't be bigger than the number of entities,
309 * as the worse-case scenario is to have one entity linked up to
310 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
311 */
312#define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
313
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300314struct media_entity_graph {
315 struct {
316 struct media_entity *entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300317 struct list_head *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300318 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300319
320 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300321 int top;
322};
323
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300324#define gobj_to_entity(gobj) \
325 container_of(gobj, struct media_entity, graph_obj)
326
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300327#define gobj_to_pad(gobj) \
328 container_of(gobj, struct media_pad, graph_obj)
329
330#define gobj_to_link(gobj) \
331 container_of(gobj, struct media_link, graph_obj)
332
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300333#define gobj_to_link(gobj) \
334 container_of(gobj, struct media_link, graph_obj)
335
336#define gobj_to_pad(gobj) \
337 container_of(gobj, struct media_pad, graph_obj)
338
339#define gobj_to_intf(gobj) \
340 container_of(gobj, struct media_interface, graph_obj)
341
342#define intf_to_devnode(intf) \
343 container_of(intf, struct media_intf_devnode, intf)
344
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300345void media_gobj_init(struct media_device *mdev,
346 enum media_gobj_type type,
347 struct media_gobj *gobj);
348void media_gobj_remove(struct media_gobj *gobj);
349
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200350int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300351 struct media_pad *pads);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300352void media_entity_cleanup(struct media_entity *entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300353
Mauro Carvalho Chehab77328042015-09-04 16:08:24 -0300354__must_check int media_create_pad_link(struct media_entity *source,
355 u16 source_pad, struct media_entity *sink,
356 u16 sink_pad, u32 flags);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300357void __media_entity_remove_links(struct media_entity *entity);
358void media_entity_remove_links(struct media_entity *entity);
359
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300360int __media_entity_setup_link(struct media_link *link, u32 flags);
361int media_entity_setup_link(struct media_link *link, u32 flags);
362struct media_link *media_entity_find_link(struct media_pad *source,
363 struct media_pad *sink);
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300364struct media_pad *media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300365
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300366struct media_entity *media_entity_get(struct media_entity *entity);
367void media_entity_put(struct media_entity *entity);
368
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300369void media_entity_graph_walk_start(struct media_entity_graph *graph,
370 struct media_entity *entity);
371struct media_entity *
372media_entity_graph_walk_next(struct media_entity_graph *graph);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300373__must_check int media_entity_pipeline_start(struct media_entity *entity,
374 struct media_pipeline *pipe);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300375void media_entity_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300376
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300377struct media_intf_devnode *
378__must_check media_devnode_create(struct media_device *mdev,
379 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300380 u32 major, u32 minor);
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300381void media_devnode_remove(struct media_intf_devnode *devnode);
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300382struct media_link *
383__must_check media_create_intf_link(struct media_entity *entity,
384 struct media_interface *intf,
385 u32 flags);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300386void __media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300387void media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300388void __media_remove_intf_links(struct media_interface *intf);
389void media_remove_intf_links(struct media_interface *intf);
390
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300391
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300392#define media_entity_call(entity, operation, args...) \
393 (((entity)->ops && (entity)->ops->operation) ? \
394 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
395
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300396#endif