Commit | Line | Data |
---|---|---|
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 |
ec6e4c95 MCC |
38 | */ |
39 | enum media_gobj_type { | |
bfab2aac | 40 | MEDIA_GRAPH_ENTITY, |
18710dc6 | 41 | MEDIA_GRAPH_PAD, |
ec6e4c95 MCC |
42 | }; |
43 | ||
44 | #define MEDIA_BITS_PER_TYPE 8 | |
45 | #define MEDIA_BITS_PER_LOCAL_ID (32 - MEDIA_BITS_PER_TYPE) | |
46 | #define MEDIA_LOCAL_ID_MASK GENMASK(MEDIA_BITS_PER_LOCAL_ID - 1, 0) | |
47 | ||
48 | /* Structs to represent the objects that belong to a media graph */ | |
49 | ||
50 | /** | |
51 | * struct media_gobj - Define a graph object. | |
52 | * | |
53 | * @id: Non-zero object ID identifier. The ID should be unique | |
54 | * inside a media_device, as it is composed by | |
55 | * MEDIA_BITS_PER_TYPE to store the type plus | |
56 | * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID | |
57 | * (called as "local ID"). | |
58 | * | |
59 | * All objects on the media graph should have this struct embedded | |
60 | */ | |
61 | struct media_gobj { | |
62 | u32 id; | |
63 | }; | |
64 | ||
65 | ||
e02188c9 LP |
66 | struct media_pipeline { |
67 | }; | |
68 | ||
53e269c1 LP |
69 | struct media_link { |
70 | struct media_pad *source; /* Source pad */ | |
71 | struct media_pad *sink; /* Sink pad */ | |
72 | struct media_link *reverse; /* Link in the reverse direction */ | |
73 | unsigned long flags; /* Link flags (MEDIA_LNK_FL_*) */ | |
74 | }; | |
75 | ||
76 | struct media_pad { | |
18710dc6 | 77 | struct media_gobj graph_obj; |
53e269c1 LP |
78 | struct media_entity *entity; /* Entity this pad belongs to */ |
79 | u16 index; /* Pad index in the entity pads array */ | |
80 | unsigned long flags; /* Pad flags (MEDIA_PAD_FL_*) */ | |
81 | }; | |
82 | ||
5a5394be LP |
83 | /** |
84 | * struct media_entity_operations - Media entity operations | |
85 | * @link_setup: Notify the entity of link changes. The operation can | |
86 | * return an error, in which case link setup will be | |
87 | * cancelled. Optional. | |
88 | * @link_validate: Return whether a link is valid from the entity point of | |
89 | * view. The media_entity_pipeline_start() function | |
90 | * validates all links by calling this operation. Optional. | |
91 | */ | |
97548ed4 LP |
92 | struct media_entity_operations { |
93 | int (*link_setup)(struct media_entity *entity, | |
94 | const struct media_pad *local, | |
95 | const struct media_pad *remote, u32 flags); | |
af88be38 | 96 | int (*link_validate)(struct media_link *link); |
97548ed4 LP |
97 | }; |
98 | ||
53e269c1 | 99 | struct media_entity { |
bfab2aac | 100 | struct media_gobj graph_obj; |
53e269c1 LP |
101 | struct list_head list; |
102 | struct media_device *parent; /* Media device this entity belongs to*/ | |
53e269c1 LP |
103 | const char *name; /* Entity name */ |
104 | u32 type; /* Entity type (MEDIA_ENT_T_*) */ | |
105 | u32 revision; /* Entity revision, driver specific */ | |
106 | unsigned long flags; /* Entity flags (MEDIA_ENT_FL_*) */ | |
107 | u32 group_id; /* Entity group ID */ | |
108 | ||
109 | u16 num_pads; /* Number of sink and source pads */ | |
110 | u16 num_links; /* Number of existing links, both | |
111 | * enabled and disabled */ | |
112 | u16 num_backlinks; /* Number of backlinks */ | |
113 | u16 max_links; /* Maximum number of links */ | |
114 | ||
115 | struct media_pad *pads; /* Pads array (num_pads elements) */ | |
116 | struct media_link *links; /* Links array (max_links elements)*/ | |
117 | ||
97548ed4 LP |
118 | const struct media_entity_operations *ops; /* Entity operations */ |
119 | ||
503c3d82 LP |
120 | /* Reference counts must never be negative, but are signed integers on |
121 | * purpose: a simple WARN_ON(<0) check can be used to detect reference | |
122 | * count bugs that would make them negative. | |
123 | */ | |
e02188c9 | 124 | int stream_count; /* Stream count for the entity. */ |
503c3d82 LP |
125 | int use_count; /* Use count for the entity. */ |
126 | ||
e02188c9 LP |
127 | struct media_pipeline *pipe; /* Pipeline this entity belongs to. */ |
128 | ||
53e269c1 LP |
129 | union { |
130 | /* Node specifications */ | |
131 | struct { | |
132 | u32 major; | |
133 | u32 minor; | |
e31a0ba7 | 134 | } dev; |
53e269c1 LP |
135 | |
136 | /* Sub-device specifications */ | |
137 | /* Nothing needed yet */ | |
fa5034c6 | 138 | } info; |
53e269c1 LP |
139 | }; |
140 | ||
141 | static inline u32 media_entity_type(struct media_entity *entity) | |
142 | { | |
143 | return entity->type & MEDIA_ENT_TYPE_MASK; | |
144 | } | |
145 | ||
146 | static inline u32 media_entity_subtype(struct media_entity *entity) | |
147 | { | |
148 | return entity->type & MEDIA_ENT_SUBTYPE_MASK; | |
149 | } | |
150 | ||
fa762394 MCC |
151 | static inline u32 media_entity_id(struct media_entity *entity) |
152 | { | |
bfab2aac | 153 | return entity->graph_obj.id; |
fa762394 MCC |
154 | } |
155 | ||
ec6e4c95 MCC |
156 | static inline enum media_gobj_type media_type(struct media_gobj *gobj) |
157 | { | |
158 | return gobj->id >> MEDIA_BITS_PER_LOCAL_ID; | |
159 | } | |
160 | ||
161 | static inline u32 media_localid(struct media_gobj *gobj) | |
162 | { | |
163 | return gobj->id & MEDIA_LOCAL_ID_MASK; | |
164 | } | |
165 | ||
166 | static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id) | |
167 | { | |
168 | u32 id; | |
169 | ||
170 | id = type << MEDIA_BITS_PER_LOCAL_ID; | |
171 | id |= local_id & MEDIA_LOCAL_ID_MASK; | |
172 | ||
173 | return id; | |
174 | } | |
175 | ||
a5ccc48a | 176 | #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16 |
5c7b25b9 | 177 | #define MEDIA_ENTITY_ENUM_MAX_ID 64 |
a5ccc48a | 178 | |
ef69ee1b MCC |
179 | /* |
180 | * The number of pads can't be bigger than the number of entities, | |
181 | * as the worse-case scenario is to have one entity linked up to | |
182 | * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities. | |
183 | */ | |
184 | #define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1) | |
185 | ||
a5ccc48a SA |
186 | struct media_entity_graph { |
187 | struct { | |
188 | struct media_entity *entity; | |
189 | int link; | |
190 | } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH]; | |
5c7b25b9 LP |
191 | |
192 | DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID); | |
a5ccc48a SA |
193 | int top; |
194 | }; | |
195 | ||
ec6e4c95 MCC |
196 | #define gobj_to_entity(gobj) \ |
197 | container_of(gobj, struct media_entity, graph_obj) | |
198 | ||
199 | void media_gobj_init(struct media_device *mdev, | |
200 | enum media_gobj_type type, | |
201 | struct media_gobj *gobj); | |
202 | void media_gobj_remove(struct media_gobj *gobj); | |
203 | ||
53e269c1 | 204 | int media_entity_init(struct media_entity *entity, u16 num_pads, |
18095107 | 205 | struct media_pad *pads); |
53e269c1 | 206 | void media_entity_cleanup(struct media_entity *entity); |
e02188c9 | 207 | |
53e269c1 LP |
208 | int media_entity_create_link(struct media_entity *source, u16 source_pad, |
209 | struct media_entity *sink, u16 sink_pad, u32 flags); | |
7349cec1 SN |
210 | void __media_entity_remove_links(struct media_entity *entity); |
211 | void media_entity_remove_links(struct media_entity *entity); | |
212 | ||
97548ed4 LP |
213 | int __media_entity_setup_link(struct media_link *link, u32 flags); |
214 | int media_entity_setup_link(struct media_link *link, u32 flags); | |
215 | struct media_link *media_entity_find_link(struct media_pad *source, | |
216 | struct media_pad *sink); | |
1bddf1b3 | 217 | struct media_pad *media_entity_remote_pad(struct media_pad *pad); |
53e269c1 | 218 | |
503c3d82 LP |
219 | struct media_entity *media_entity_get(struct media_entity *entity); |
220 | void media_entity_put(struct media_entity *entity); | |
221 | ||
a5ccc48a SA |
222 | void media_entity_graph_walk_start(struct media_entity_graph *graph, |
223 | struct media_entity *entity); | |
224 | struct media_entity * | |
225 | media_entity_graph_walk_next(struct media_entity_graph *graph); | |
af88be38 SA |
226 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
227 | struct media_pipeline *pipe); | |
e02188c9 | 228 | void media_entity_pipeline_stop(struct media_entity *entity); |
a5ccc48a | 229 | |
97548ed4 LP |
230 | #define media_entity_call(entity, operation, args...) \ |
231 | (((entity)->ops && (entity)->ops->operation) ? \ | |
232 | (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD) | |
233 | ||
53e269c1 | 234 | #endif |