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
68 struct media_pipeline
{
72 struct media_gobj graph_obj
;
73 struct media_pad
*source
; /* Source pad */
74 struct media_pad
*sink
; /* Sink pad */
75 struct media_link
*reverse
; /* Link in the reverse direction */
76 unsigned long flags
; /* Link flags (MEDIA_LNK_FL_*) */
80 struct media_gobj graph_obj
;
81 struct media_entity
*entity
; /* Entity this pad belongs to */
82 u16 index
; /* Pad index in the entity pads array */
83 unsigned long flags
; /* Pad flags (MEDIA_PAD_FL_*) */
87 * struct media_entity_operations - Media entity operations
88 * @link_setup: Notify the entity of link changes. The operation can
89 * return an error, in which case link setup will be
90 * cancelled. Optional.
91 * @link_validate: Return whether a link is valid from the entity point of
92 * view. The media_entity_pipeline_start() function
93 * validates all links by calling this operation. Optional.
95 struct media_entity_operations
{
96 int (*link_setup
)(struct media_entity
*entity
,
97 const struct media_pad
*local
,
98 const struct media_pad
*remote
, u32 flags
);
99 int (*link_validate
)(struct media_link
*link
);
102 struct media_entity
{
103 struct media_gobj graph_obj
;
104 struct list_head list
;
105 struct media_device
*parent
; /* Media device this entity belongs to*/
106 const char *name
; /* Entity name */
107 u32 type
; /* Entity type (MEDIA_ENT_T_*) */
108 u32 revision
; /* Entity revision, driver specific */
109 unsigned long flags
; /* Entity flags (MEDIA_ENT_FL_*) */
110 u32 group_id
; /* Entity group ID */
112 u16 num_pads
; /* Number of sink and source pads */
113 u16 num_links
; /* Number of existing links, both
114 * enabled and disabled */
115 u16 num_backlinks
; /* Number of backlinks */
116 u16 max_links
; /* Maximum number of links */
118 struct media_pad
*pads
; /* Pads array (num_pads elements) */
119 struct media_link
*links
; /* Links array (max_links elements)*/
121 const struct media_entity_operations
*ops
; /* Entity operations */
123 /* Reference counts must never be negative, but are signed integers on
124 * purpose: a simple WARN_ON(<0) check can be used to detect reference
125 * count bugs that would make them negative.
127 int stream_count
; /* Stream count for the entity. */
128 int use_count
; /* Use count for the entity. */
130 struct media_pipeline
*pipe
; /* Pipeline this entity belongs to. */
133 /* Node specifications */
139 /* Sub-device specifications */
140 /* Nothing needed yet */
144 static inline u32
media_entity_type(struct media_entity
*entity
)
146 return entity
->type
& MEDIA_ENT_TYPE_MASK
;
149 static inline u32
media_entity_subtype(struct media_entity
*entity
)
151 return entity
->type
& MEDIA_ENT_SUBTYPE_MASK
;
154 static inline u32
media_entity_id(struct media_entity
*entity
)
156 return entity
->graph_obj
.id
;
159 static inline enum media_gobj_type
media_type(struct media_gobj
*gobj
)
161 return gobj
->id
>> MEDIA_BITS_PER_LOCAL_ID
;
164 static inline u32
media_localid(struct media_gobj
*gobj
)
166 return gobj
->id
& MEDIA_LOCAL_ID_MASK
;
169 static inline u32
media_gobj_gen_id(enum media_gobj_type type
, u32 local_id
)
173 id
= type
<< MEDIA_BITS_PER_LOCAL_ID
;
174 id
|= local_id
& MEDIA_LOCAL_ID_MASK
;
179 #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
180 #define MEDIA_ENTITY_ENUM_MAX_ID 64
183 * The number of pads can't be bigger than the number of entities,
184 * as the worse-case scenario is to have one entity linked up to
185 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
187 #define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
189 struct media_entity_graph
{
191 struct media_entity
*entity
;
193 } stack
[MEDIA_ENTITY_ENUM_MAX_DEPTH
];
195 DECLARE_BITMAP(entities
, MEDIA_ENTITY_ENUM_MAX_ID
);
199 #define gobj_to_entity(gobj) \
200 container_of(gobj, struct media_entity, graph_obj)
202 void media_gobj_init(struct media_device
*mdev
,
203 enum media_gobj_type type
,
204 struct media_gobj
*gobj
);
205 void media_gobj_remove(struct media_gobj
*gobj
);
207 int media_entity_init(struct media_entity
*entity
, u16 num_pads
,
208 struct media_pad
*pads
);
209 void media_entity_cleanup(struct media_entity
*entity
);
211 int media_entity_create_link(struct media_entity
*source
, u16 source_pad
,
212 struct media_entity
*sink
, u16 sink_pad
, u32 flags
);
213 void __media_entity_remove_links(struct media_entity
*entity
);
214 void media_entity_remove_links(struct media_entity
*entity
);
216 int __media_entity_setup_link(struct media_link
*link
, u32 flags
);
217 int media_entity_setup_link(struct media_link
*link
, u32 flags
);
218 struct media_link
*media_entity_find_link(struct media_pad
*source
,
219 struct media_pad
*sink
);
220 struct media_pad
*media_entity_remote_pad(struct media_pad
*pad
);
222 struct media_entity
*media_entity_get(struct media_entity
*entity
);
223 void media_entity_put(struct media_entity
*entity
);
225 void media_entity_graph_walk_start(struct media_entity_graph
*graph
,
226 struct media_entity
*entity
);
227 struct media_entity
*
228 media_entity_graph_walk_next(struct media_entity_graph
*graph
);
229 __must_check
int media_entity_pipeline_start(struct media_entity
*entity
,
230 struct media_pipeline
*pipe
);
231 void media_entity_pipeline_stop(struct media_entity
*entity
);
233 #define media_entity_call(entity, operation, args...) \
234 (((entity)->ops && (entity)->ops->operation) ? \
235 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)