[media] dvbdev: Don't create indirect links
[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.
0e576b76
MCC
155 * @function: Entity main function, as defined in uapi/media.h
156 * (MEDIA_ENT_F_*)
c358e80d
MCC
157 * @revision: Entity revision - OBSOLETE - should be removed soon.
158 * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
159 * @group_id: Entity group ID - OBSOLETE - should be removed soon.
160 * @num_pads: Number of sink and source pads.
161 * @num_links: Total number of links, forward and back, enabled and disabled.
162 * @num_backlinks: Number of backlinks
163 * @pads: Pads array with the size defined by @num_pads.
164 * @links: List of data links.
165 * @ops: Entity operations.
166 * @stream_count: Stream count for the entity.
167 * @use_count: Use count for the entity.
168 * @pipe: Pipeline this entity belongs to.
169 * @info: Union with devnode information. Kept just for backward
170 * compatibility.
171 * @major: Devnode major number (zero if not applicable). Kept just
172 * for backward compatibility.
173 * @minor: Devnode minor number (zero if not applicable). Kept just
174 * for backward compatibility.
175 *
176 * NOTE: @stream_count and @use_count reference counts must never be
177 * negative, but are signed integers on purpose: a simple WARN_ON(<0) check
178 * can be used to detect reference count bugs that would make them negative.
179 */
53e269c1 180struct media_entity {
4b8a3c08 181 struct media_gobj graph_obj; /* must be first field in struct */
c358e80d 182 const char *name;
0e576b76 183 u32 function;
c358e80d
MCC
184 u32 revision;
185 unsigned long flags;
186 u32 group_id;
53e269c1 187
c358e80d
MCC
188 u16 num_pads;
189 u16 num_links;
190 u16 num_backlinks;
53e269c1 191
c358e80d
MCC
192 struct media_pad *pads;
193 struct list_head links;
53e269c1 194
c358e80d 195 const struct media_entity_operations *ops;
97548ed4 196
503c3d82
LP
197 /* Reference counts must never be negative, but are signed integers on
198 * purpose: a simple WARN_ON(<0) check can be used to detect reference
199 * count bugs that would make them negative.
200 */
c358e80d
MCC
201 int stream_count;
202 int use_count;
503c3d82 203
c358e80d 204 struct media_pipeline *pipe;
e02188c9 205
53e269c1 206 union {
53e269c1
LP
207 struct {
208 u32 major;
209 u32 minor;
e31a0ba7 210 } dev;
fa5034c6 211 } info;
53e269c1
LP
212};
213
27e543fa 214/**
c358e80d 215 * struct media_interface - A media interface graph object.
27e543fa
MCC
216 *
217 * @graph_obj: embedded graph object
86e26620 218 * @links: List of links pointing to graph entities
c358e80d 219 * @type: Type of the interface as defined in the
27e543fa
MCC
220 * uapi/media/media.h header, e. g.
221 * MEDIA_INTF_T_*
c358e80d 222 * @flags: Interface flags as defined in uapi/media/media.h
27e543fa
MCC
223 */
224struct media_interface {
225 struct media_gobj graph_obj;
86e26620 226 struct list_head links;
27e543fa
MCC
227 u32 type;
228 u32 flags;
229};
230
231/**
c358e80d 232 * struct media_intf_devnode - A media interface via a device node.
27e543fa
MCC
233 *
234 * @intf: embedded interface object
235 * @major: Major number of a device node
236 * @minor: Minor number of a device node
237 */
238struct media_intf_devnode {
239 struct media_interface intf;
c398bb64
MCC
240
241 /* Should match the fields at media_v2_intf_devnode */
27e543fa
MCC
242 u32 major;
243 u32 minor;
244};
245
fa762394
MCC
246static inline u32 media_entity_id(struct media_entity *entity)
247{
bfab2aac 248 return entity->graph_obj.id;
fa762394
MCC
249}
250
ec6e4c95
MCC
251static inline enum media_gobj_type media_type(struct media_gobj *gobj)
252{
253 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
254}
255
256static inline u32 media_localid(struct media_gobj *gobj)
257{
258 return gobj->id & MEDIA_LOCAL_ID_MASK;
259}
260
261static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
262{
263 u32 id;
264
265 id = type << MEDIA_BITS_PER_LOCAL_ID;
266 id |= local_id & MEDIA_LOCAL_ID_MASK;
267
268 return id;
269}
270
fa17b46a
MCC
271static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
272{
273 if (!entity)
274 return false;
275
0e576b76 276 switch (entity->function) {
4ca72efa
MCC
277 case MEDIA_ENT_F_IO_V4L:
278 case MEDIA_ENT_F_IO_VBI:
279 case MEDIA_ENT_F_IO_SWRADIO:
fa17b46a
MCC
280 return true;
281 default:
282 return false;
283 }
284}
285
286static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
287{
288 if (!entity)
289 return false;
290
0e576b76 291 switch (entity->function) {
4ca72efa
MCC
292 case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
293 case MEDIA_ENT_F_CAM_SENSOR:
294 case MEDIA_ENT_F_FLASH:
295 case MEDIA_ENT_F_LENS:
296 case MEDIA_ENT_F_ATV_DECODER:
297 case MEDIA_ENT_F_TUNER:
fa17b46a
MCC
298 return true;
299
300 default:
301 return false;
302 }
303}
304
a5ccc48a 305#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
5c7b25b9 306#define MEDIA_ENTITY_ENUM_MAX_ID 64
a5ccc48a 307
ef69ee1b
MCC
308/*
309 * The number of pads can't be bigger than the number of entities,
310 * as the worse-case scenario is to have one entity linked up to
311 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
312 */
313#define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
314
a5ccc48a
SA
315struct media_entity_graph {
316 struct {
317 struct media_entity *entity;
57208e5e 318 struct list_head *link;
a5ccc48a 319 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
5c7b25b9
LP
320
321 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
a5ccc48a
SA
322 int top;
323};
324
ec6e4c95
MCC
325#define gobj_to_entity(gobj) \
326 container_of(gobj, struct media_entity, graph_obj)
327
39a956c4
MCC
328#define gobj_to_pad(gobj) \
329 container_of(gobj, struct media_pad, graph_obj)
330
331#define gobj_to_link(gobj) \
332 container_of(gobj, struct media_link, graph_obj)
333
27e543fa
MCC
334#define gobj_to_link(gobj) \
335 container_of(gobj, struct media_link, graph_obj)
336
337#define gobj_to_pad(gobj) \
338 container_of(gobj, struct media_pad, graph_obj)
339
340#define gobj_to_intf(gobj) \
341 container_of(gobj, struct media_interface, graph_obj)
342
343#define intf_to_devnode(intf) \
344 container_of(intf, struct media_intf_devnode, intf)
345
ec6e4c95
MCC
346void media_gobj_init(struct media_device *mdev,
347 enum media_gobj_type type,
348 struct media_gobj *gobj);
349void media_gobj_remove(struct media_gobj *gobj);
350
53e269c1 351int media_entity_init(struct media_entity *entity, u16 num_pads,
57208e5e 352 struct media_pad *pads);
53e269c1 353void media_entity_cleanup(struct media_entity *entity);
e02188c9 354
77328043
MCC
355__must_check int media_create_pad_link(struct media_entity *source,
356 u16 source_pad, struct media_entity *sink,
357 u16 sink_pad, u32 flags);
7349cec1
SN
358void __media_entity_remove_links(struct media_entity *entity);
359void media_entity_remove_links(struct media_entity *entity);
360
97548ed4
LP
361int __media_entity_setup_link(struct media_link *link, u32 flags);
362int media_entity_setup_link(struct media_link *link, u32 flags);
363struct media_link *media_entity_find_link(struct media_pad *source,
364 struct media_pad *sink);
1bddf1b3 365struct media_pad *media_entity_remote_pad(struct media_pad *pad);
53e269c1 366
503c3d82
LP
367struct media_entity *media_entity_get(struct media_entity *entity);
368void media_entity_put(struct media_entity *entity);
369
a5ccc48a
SA
370void media_entity_graph_walk_start(struct media_entity_graph *graph,
371 struct media_entity *entity);
372struct media_entity *
373media_entity_graph_walk_next(struct media_entity_graph *graph);
af88be38
SA
374__must_check int media_entity_pipeline_start(struct media_entity *entity,
375 struct media_pipeline *pipe);
e02188c9 376void media_entity_pipeline_stop(struct media_entity *entity);
a5ccc48a 377
5e5387df
MCC
378struct media_intf_devnode *
379__must_check media_devnode_create(struct media_device *mdev,
380 u32 type, u32 flags,
381 u32 major, u32 minor,
382 gfp_t gfp_flags);
27e543fa 383void media_devnode_remove(struct media_intf_devnode *devnode);
5e5387df
MCC
384struct media_link *
385__must_check media_create_intf_link(struct media_entity *entity,
386 struct media_interface *intf,
387 u32 flags);
d47109fa 388void __media_remove_intf_link(struct media_link *link);
86e26620 389void media_remove_intf_link(struct media_link *link);
7c4696a9
MCC
390void __media_remove_intf_links(struct media_interface *intf);
391void media_remove_intf_links(struct media_interface *intf);
392
86e26620 393
97548ed4
LP
394#define media_entity_call(entity, operation, args...) \
395 (((entity)->ops && (entity)->ops->operation) ? \
396 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
397
53e269c1 398#endif
This page took 0.220105 seconds and 5 git commands to generate.