4 * Copyright (C) 2010 Nokia Corporation
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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
23 #ifndef _MEDIA_ENTITY_H
24 #define _MEDIA_ENTITY_H
26 #include <linux/bitops.h>
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/media.h>
31 /* Enums used internally at the media controller to represent graphs */
34 * enum media_gobj_type - type of a graph object
36 * @MEDIA_GRAPH_ENTITY: Identify a media entity
37 * @MEDIA_GRAPH_PAD: Identify a media pad
38 * @MEDIA_GRAPH_LINK: Identify a media link
40 enum media_gobj_type
{
46 #define MEDIA_BITS_PER_TYPE 8
47 #define MEDIA_BITS_PER_LOCAL_ID (32 - MEDIA_BITS_PER_TYPE)
48 #define MEDIA_LOCAL_ID_MASK GENMASK(MEDIA_BITS_PER_LOCAL_ID - 1, 0)
50 /* Structs to represent the objects that belong to a media graph */
53 * struct media_gobj - Define a graph object.
55 * @id: Non-zero object ID identifier. The ID should be unique
56 * inside a media_device, as it is composed by
57 * MEDIA_BITS_PER_TYPE to store the type plus
58 * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID
59 * (called as "local ID").
61 * All objects on the media graph should have this struct embedded
64 struct media_device
*mdev
;
69 struct media_pipeline
{
73 struct media_gobj graph_obj
;
74 struct media_pad
*source
; /* Source pad */
75 struct media_pad
*sink
; /* Sink pad */
76 struct media_link
*reverse
; /* Link in the reverse direction */
77 unsigned long flags
; /* Link flags (MEDIA_LNK_FL_*) */
81 struct media_gobj graph_obj
;
82 struct media_entity
*entity
; /* Entity this pad belongs to */
83 u16 index
; /* Pad index in the entity pads array */
84 unsigned long flags
; /* Pad flags (MEDIA_PAD_FL_*) */
88 * struct media_entity_operations - Media entity operations
89 * @link_setup: Notify the entity of link changes. The operation can
90 * return an error, in which case link setup will be
91 * cancelled. Optional.
92 * @link_validate: Return whether a link is valid from the entity point of
93 * view. The media_entity_pipeline_start() function
94 * validates all links by calling this operation. Optional.
96 struct media_entity_operations
{
97 int (*link_setup
)(struct media_entity
*entity
,
98 const struct media_pad
*local
,
99 const struct media_pad
*remote
, u32 flags
);
100 int (*link_validate
)(struct media_link
*link
);
103 struct media_entity
{
104 struct media_gobj graph_obj
;
105 struct list_head list
;
106 struct media_device
*parent
; /* Media device this entity belongs to*/
107 const char *name
; /* Entity name */
108 u32 type
; /* Entity type (MEDIA_ENT_T_*) */
109 u32 revision
; /* Entity revision, driver specific */
110 unsigned long flags
; /* Entity flags (MEDIA_ENT_FL_*) */
111 u32 group_id
; /* Entity group ID */
113 u16 num_pads
; /* Number of sink and source pads */
114 u16 num_links
; /* Number of existing links, both
115 * enabled and disabled */
116 u16 num_backlinks
; /* Number of backlinks */
117 u16 max_links
; /* Maximum number of links */
119 struct media_pad
*pads
; /* Pads array (num_pads elements) */
120 struct media_link
*links
; /* Links array (max_links elements)*/
122 const struct media_entity_operations
*ops
; /* Entity operations */
124 /* Reference counts must never be negative, but are signed integers on
125 * purpose: a simple WARN_ON(<0) check can be used to detect reference
126 * count bugs that would make them negative.
128 int stream_count
; /* Stream count for the entity. */
129 int use_count
; /* Use count for the entity. */
131 struct media_pipeline
*pipe
; /* Pipeline this entity belongs to. */
134 /* Node specifications */
140 /* Sub-device specifications */
141 /* Nothing needed yet */
145 static inline u32
media_entity_type(struct media_entity
*entity
)
147 return entity
->type
& MEDIA_ENT_TYPE_MASK
;
150 static inline u32
media_entity_subtype(struct media_entity
*entity
)
152 return entity
->type
& MEDIA_ENT_SUBTYPE_MASK
;
155 static inline u32
media_entity_id(struct media_entity
*entity
)
157 return entity
->graph_obj
.id
;
160 static inline enum media_gobj_type
media_type(struct media_gobj
*gobj
)
162 return gobj
->id
>> MEDIA_BITS_PER_LOCAL_ID
;
165 static inline u32
media_localid(struct media_gobj
*gobj
)
167 return gobj
->id
& MEDIA_LOCAL_ID_MASK
;
170 static inline u32
media_gobj_gen_id(enum media_gobj_type type
, u32 local_id
)
174 id
= type
<< MEDIA_BITS_PER_LOCAL_ID
;
175 id
|= local_id
& MEDIA_LOCAL_ID_MASK
;
180 #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
181 #define MEDIA_ENTITY_ENUM_MAX_ID 64
184 * The number of pads can't be bigger than the number of entities,
185 * as the worse-case scenario is to have one entity linked up to
186 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
188 #define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
190 struct media_entity_graph
{
192 struct media_entity
*entity
;
194 } stack
[MEDIA_ENTITY_ENUM_MAX_DEPTH
];
196 DECLARE_BITMAP(entities
, MEDIA_ENTITY_ENUM_MAX_ID
);
200 #define gobj_to_entity(gobj) \
201 container_of(gobj, struct media_entity, graph_obj)
203 #define gobj_to_pad(gobj) \
204 container_of(gobj, struct media_pad, graph_obj)
206 #define gobj_to_link(gobj) \
207 container_of(gobj, struct media_link, graph_obj)
209 void media_gobj_init(struct media_device
*mdev
,
210 enum media_gobj_type type
,
211 struct media_gobj
*gobj
);
212 void media_gobj_remove(struct media_gobj
*gobj
);
214 int media_entity_init(struct media_entity
*entity
, u16 num_pads
,
215 struct media_pad
*pads
);
216 void media_entity_cleanup(struct media_entity
*entity
);
218 int media_entity_create_link(struct media_entity
*source
, u16 source_pad
,
219 struct media_entity
*sink
, u16 sink_pad
, u32 flags
);
220 void __media_entity_remove_links(struct media_entity
*entity
);
221 void media_entity_remove_links(struct media_entity
*entity
);
223 int __media_entity_setup_link(struct media_link
*link
, u32 flags
);
224 int media_entity_setup_link(struct media_link
*link
, u32 flags
);
225 struct media_link
*media_entity_find_link(struct media_pad
*source
,
226 struct media_pad
*sink
);
227 struct media_pad
*media_entity_remote_pad(struct media_pad
*pad
);
229 struct media_entity
*media_entity_get(struct media_entity
*entity
);
230 void media_entity_put(struct media_entity
*entity
);
232 void media_entity_graph_walk_start(struct media_entity_graph
*graph
,
233 struct media_entity
*entity
);
234 struct media_entity
*
235 media_entity_graph_walk_next(struct media_entity_graph
*graph
);
236 __must_check
int media_entity_pipeline_start(struct media_entity
*entity
,
237 struct media_pipeline
*pipe
);
238 void media_entity_pipeline_stop(struct media_entity
*entity
);
240 #define media_entity_call(entity, operation, args...) \
241 (((entity)->ops && (entity)->ops->operation) ? \
242 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)