[media] media-entity: must check media_create_pad_link()
[deliverable/linux.git] / include / media / media-entity.h
CommitLineData
53e269c1
LP
1/*
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
5c7b25b9 26#include <linux/bitops.h>
0149a2e1 27#include <linux/kernel.h>
53e269c1 28#include <linux/list.h>
1651333b 29#include <linux/media.h>
53e269c1 30
ec6e4c95
MCC
31/* Enums used internally at the media controller to represent graphs */
32
33/**
34 * enum media_gobj_type - type of a graph object
35 *
bfab2aac 36 * @MEDIA_GRAPH_ENTITY: Identify a media entity
18710dc6 37 * @MEDIA_GRAPH_PAD: Identify a media pad
6b6a4278 38 * @MEDIA_GRAPH_LINK: Identify a media link
27e543fa
MCC
39 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
40 * a device node
ec6e4c95
MCC
41 */
42enum media_gobj_type {
bfab2aac 43 MEDIA_GRAPH_ENTITY,
18710dc6 44 MEDIA_GRAPH_PAD,
6b6a4278 45 MEDIA_GRAPH_LINK,
27e543fa 46 MEDIA_GRAPH_INTF_DEVNODE,
ec6e4c95
MCC
47};
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 *
c358e80d 58 * @mdev: Pointer to the struct media_device that owns the object
ec6e4c95
MCC
59 * @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").
c358e80d 64 * @list: List entry stored in one of the per-type mdev object lists
ec6e4c95
MCC
65 *
66 * All objects on the media graph should have this struct embedded
67 */
68struct media_gobj {
39a956c4 69 struct media_device *mdev;
ec6e4c95 70 u32 id;
05bfa9fa 71 struct list_head list;
ec6e4c95
MCC
72};
73
74
e02188c9
LP
75struct media_pipeline {
76};
77
c358e80d
MCC
78/**
79 * struct media_link - A link object part of a media graph.
80 *
81 * @graph_obj: Embedded structure containing the media object common data
82 * @list: Linked list associated with an entity or an interface that
83 * owns the link.
84 * @gobj0: Part of a union. Used to get the pointer for the first
85 * graph_object of the link.
86 * @source: Part of a union. Used only if the first object (gobj0) is
87 * a pad. In that case, it represents the source pad.
88 * @intf: Part of a union. Used only if the first object (gobj0) is
89 * an interface.
90 * @gobj1: Part of a union. Used to get the pointer for the second
91 * graph_object of the link.
92 * @source: Part of a union. Used only if the second object (gobj1) is
93 * a pad. In that case, it represents the sink pad.
94 * @entity: Part of a union. Used only if the second object (gobj1) is
95 * an entity.
96 * @reverse: Pointer to the link for the reverse direction of a pad to pad
97 * link.
98 * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
39d1ebc6 99 * @is_backlink: Indicate if the link is a backlink.
c358e80d 100 */
53e269c1 101struct media_link {
6b6a4278 102 struct media_gobj graph_obj;
57208e5e 103 struct list_head list;
4b8a3c08
MCC
104 union {
105 struct media_gobj *gobj0;
106 struct media_pad *source;
86e26620 107 struct media_interface *intf;
4b8a3c08
MCC
108 };
109 union {
110 struct media_gobj *gobj1;
111 struct media_pad *sink;
86e26620 112 struct media_entity *entity;
4b8a3c08 113 };
c358e80d
MCC
114 struct media_link *reverse;
115 unsigned long flags;
39d1ebc6 116 bool is_backlink;
53e269c1
LP
117};
118
c358e80d
MCC
119/**
120 * struct media_pad - A media pad graph object.
121 *
122 * @graph_obj: Embedded structure containing the media object common data
123 * @entity: Entity this pad belongs to
124 * @index: Pad index in the entity pads array, numbered from 0 to n
125 * @flags: Pad flags, as defined in uapi/media.h (MEDIA_PAD_FL_*)
126 */
53e269c1 127struct media_pad {
4b8a3c08 128 struct media_gobj graph_obj; /* must be first field in struct */
c358e80d
MCC
129 struct media_entity *entity;
130 u16 index;
131 unsigned long flags;
53e269c1
LP
132};
133
5a5394be
LP
134/**
135 * struct media_entity_operations - Media entity operations
136 * @link_setup: Notify the entity of link changes. The operation can
137 * return an error, in which case link setup will be
138 * cancelled. Optional.
139 * @link_validate: Return whether a link is valid from the entity point of
140 * view. The media_entity_pipeline_start() function
141 * validates all links by calling this operation. Optional.
142 */
97548ed4
LP
143struct media_entity_operations {
144 int (*link_setup)(struct media_entity *entity,
145 const struct media_pad *local,
146 const struct media_pad *remote, u32 flags);
af88be38 147 int (*link_validate)(struct media_link *link);
97548ed4
LP
148};
149
c358e80d
MCC
150/**
151 * struct media_entity - A media entity graph object.
152 *
153 * @graph_obj: Embedded structure containing the media object common data.
154 * @name: Entity name.
155 * @type: Entity type, as defined in uapi/media.h (MEDIA_ENT_T_*)
156 * @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 */
53e269c1 179struct media_entity {
4b8a3c08 180 struct media_gobj graph_obj; /* must be first field in struct */
c358e80d
MCC
181 const char *name;
182 u32 type;
183 u32 revision;
184 unsigned long flags;
185 u32 group_id;
53e269c1 186
c358e80d
MCC
187 u16 num_pads;
188 u16 num_links;
189 u16 num_backlinks;
53e269c1 190
c358e80d
MCC
191 struct media_pad *pads;
192 struct list_head links;
53e269c1 193
c358e80d 194 const struct media_entity_operations *ops;
97548ed4 195
503c3d82
LP
196 /* 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 */
c358e80d
MCC
200 int stream_count;
201 int use_count;
503c3d82 202
c358e80d 203 struct media_pipeline *pipe;
e02188c9 204
53e269c1 205 union {
53e269c1
LP
206 struct {
207 u32 major;
208 u32 minor;
e31a0ba7 209 } dev;
fa5034c6 210 } info;
53e269c1
LP
211};
212
27e543fa 213/**
c358e80d 214 * struct media_interface - A media interface graph object.
27e543fa
MCC
215 *
216 * @graph_obj: embedded graph object
86e26620 217 * @links: List of links pointing to graph entities
c358e80d 218 * @type: Type of the interface as defined in the
27e543fa
MCC
219 * uapi/media/media.h header, e. g.
220 * MEDIA_INTF_T_*
c358e80d 221 * @flags: Interface flags as defined in uapi/media/media.h
27e543fa
MCC
222 */
223struct media_interface {
224 struct media_gobj graph_obj;
86e26620 225 struct list_head links;
27e543fa
MCC
226 u32 type;
227 u32 flags;
228};
229
230/**
c358e80d 231 * struct media_intf_devnode - A media interface via a device node.
27e543fa
MCC
232 *
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;
c398bb64
MCC
239
240 /* Should match the fields at media_v2_intf_devnode */
27e543fa
MCC
241 u32 major;
242 u32 minor;
243};
244
fa762394
MCC
245static inline u32 media_entity_id(struct media_entity *entity)
246{
bfab2aac 247 return entity->graph_obj.id;
fa762394
MCC
248}
249
ec6e4c95
MCC
250static 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
fa17b46a
MCC
270static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
271{
272 if (!entity)
273 return false;
274
275 switch (entity->type) {
276 case MEDIA_ENT_T_V4L2_VIDEO:
277 case MEDIA_ENT_T_V4L2_VBI:
278 case MEDIA_ENT_T_V4L2_SWRADIO:
279 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
290 switch (entity->type) {
291 case MEDIA_ENT_T_V4L2_SUBDEV_UNKNOWN:
292 case MEDIA_ENT_T_V4L2_SUBDEV_SENSOR:
293 case MEDIA_ENT_T_V4L2_SUBDEV_FLASH:
294 case MEDIA_ENT_T_V4L2_SUBDEV_LENS:
295 case MEDIA_ENT_T_V4L2_SUBDEV_DECODER:
296 case MEDIA_ENT_T_V4L2_SUBDEV_TUNER:
297 return true;
298
299 default:
300 return false;
301 }
302}
303
a5ccc48a 304#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
5c7b25b9 305#define MEDIA_ENTITY_ENUM_MAX_ID 64
a5ccc48a 306
ef69ee1b
MCC
307/*
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
a5ccc48a
SA
314struct media_entity_graph {
315 struct {
316 struct media_entity *entity;
57208e5e 317 struct list_head *link;
a5ccc48a 318 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
5c7b25b9
LP
319
320 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
a5ccc48a
SA
321 int top;
322};
323
ec6e4c95
MCC
324#define gobj_to_entity(gobj) \
325 container_of(gobj, struct media_entity, graph_obj)
326
39a956c4
MCC
327#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
27e543fa
MCC
333#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
ec6e4c95
MCC
345void 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
53e269c1 350int media_entity_init(struct media_entity *entity, u16 num_pads,
57208e5e 351 struct media_pad *pads);
53e269c1 352void media_entity_cleanup(struct media_entity *entity);
e02188c9 353
77328043
MCC
354__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);
7349cec1
SN
357void __media_entity_remove_links(struct media_entity *entity);
358void media_entity_remove_links(struct media_entity *entity);
359
97548ed4
LP
360int __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);
1bddf1b3 364struct media_pad *media_entity_remote_pad(struct media_pad *pad);
53e269c1 365
503c3d82
LP
366struct media_entity *media_entity_get(struct media_entity *entity);
367void media_entity_put(struct media_entity *entity);
368
a5ccc48a
SA
369void 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);
af88be38
SA
373__must_check int media_entity_pipeline_start(struct media_entity *entity,
374 struct media_pipeline *pipe);
e02188c9 375void media_entity_pipeline_stop(struct media_entity *entity);
a5ccc48a 376
5e5387df
MCC
377struct media_intf_devnode *
378__must_check media_devnode_create(struct media_device *mdev,
379 u32 type, u32 flags,
380 u32 major, u32 minor,
381 gfp_t gfp_flags);
27e543fa 382void media_devnode_remove(struct media_intf_devnode *devnode);
5e5387df
MCC
383struct media_link *
384__must_check media_create_intf_link(struct media_entity *entity,
385 struct media_interface *intf,
386 u32 flags);
d47109fa 387void __media_remove_intf_link(struct media_link *link);
86e26620 388void media_remove_intf_link(struct media_link *link);
7c4696a9
MCC
389void __media_remove_intf_links(struct media_interface *intf);
390void media_remove_intf_links(struct media_interface *intf);
391
86e26620 392
97548ed4
LP
393#define media_entity_call(entity, operation, args...) \
394 (((entity)->ops && (entity)->ops->operation) ? \
395 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
396
53e269c1 397#endif
This page took 0.205908 seconds and 5 git commands to generate.