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 | ||
c8d54cd5 | 26 | #include <linux/bitmap.h> |
2899d35d | 27 | #include <linux/bug.h> |
0149a2e1 | 28 | #include <linux/kernel.h> |
53e269c1 | 29 | #include <linux/list.h> |
1651333b | 30 | #include <linux/media.h> |
53e269c1 | 31 | |
ec6e4c95 MCC |
32 | /* Enums used internally at the media controller to represent graphs */ |
33 | ||
34 | /** | |
35 | * enum media_gobj_type - type of a graph object | |
36 | * | |
bfab2aac | 37 | * @MEDIA_GRAPH_ENTITY: Identify a media entity |
18710dc6 | 38 | * @MEDIA_GRAPH_PAD: Identify a media pad |
6b6a4278 | 39 | * @MEDIA_GRAPH_LINK: Identify a media link |
27e543fa MCC |
40 | * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via |
41 | * a device node | |
ec6e4c95 MCC |
42 | */ |
43 | enum media_gobj_type { | |
bfab2aac | 44 | MEDIA_GRAPH_ENTITY, |
18710dc6 | 45 | MEDIA_GRAPH_PAD, |
6b6a4278 | 46 | MEDIA_GRAPH_LINK, |
27e543fa | 47 | MEDIA_GRAPH_INTF_DEVNODE, |
ec6e4c95 MCC |
48 | }; |
49 | ||
50 | #define MEDIA_BITS_PER_TYPE 8 | |
05b3b77c MCC |
51 | #define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE) |
52 | #define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0) | |
ec6e4c95 MCC |
53 | |
54 | /* Structs to represent the objects that belong to a media graph */ | |
55 | ||
56 | /** | |
57 | * struct media_gobj - Define a graph object. | |
58 | * | |
c358e80d | 59 | * @mdev: Pointer to the struct media_device that owns the object |
ec6e4c95 MCC |
60 | * @id: Non-zero object ID identifier. The ID should be unique |
61 | * inside a media_device, as it is composed by | |
05b3b77c MCC |
62 | * %MEDIA_BITS_PER_TYPE to store the type plus |
63 | * %MEDIA_BITS_PER_ID to store the 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 | */ | |
68 | struct media_gobj { | |
39a956c4 | 69 | struct media_device *mdev; |
ec6e4c95 | 70 | u32 id; |
05bfa9fa | 71 | struct list_head list; |
ec6e4c95 MCC |
72 | }; |
73 | ||
c8d54cd5 | 74 | #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16 |
c8d54cd5 | 75 | |
c8d54cd5 SA |
76 | /** |
77 | * struct media_entity_enum - An enumeration of media entities. | |
78 | * | |
c8d54cd5 SA |
79 | * @bmap: Bit map in which each bit represents one entity at struct |
80 | * media_entity->internal_idx. | |
81 | * @idx_max: Number of bits in bmap | |
82 | */ | |
83 | struct media_entity_enum { | |
c8d54cd5 SA |
84 | unsigned long *bmap; |
85 | int idx_max; | |
86 | }; | |
87 | ||
434257f1 SA |
88 | /** |
89 | * struct media_entity_graph - Media graph traversal state | |
90 | * | |
91 | * @stack: Graph traversal stack; the stack contains information | |
92 | * on the path the media entities to be walked and the | |
93 | * links through which they were reached. | |
29d8da02 | 94 | * @ent_enum: Visited entities |
434257f1 SA |
95 | * @top: The top of the stack |
96 | */ | |
82c68290 SA |
97 | struct media_entity_graph { |
98 | struct { | |
99 | struct media_entity *entity; | |
100 | struct list_head *link; | |
101 | } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH]; | |
102 | ||
29d8da02 | 103 | struct media_entity_enum ent_enum; |
82c68290 SA |
104 | int top; |
105 | }; | |
106 | ||
5dd8775d SA |
107 | /* |
108 | * struct media_pipeline - Media pipeline related information | |
109 | * | |
74a41330 SA |
110 | * @streaming_count: Streaming start count - streaming stop count |
111 | * @graph: Media graph walk during pipeline start / stop | |
5dd8775d | 112 | */ |
e02188c9 | 113 | struct media_pipeline { |
74a41330 | 114 | int streaming_count; |
5dd8775d | 115 | struct media_entity_graph graph; |
e02188c9 LP |
116 | }; |
117 | ||
c358e80d MCC |
118 | /** |
119 | * struct media_link - A link object part of a media graph. | |
120 | * | |
121 | * @graph_obj: Embedded structure containing the media object common data | |
122 | * @list: Linked list associated with an entity or an interface that | |
123 | * owns the link. | |
124 | * @gobj0: Part of a union. Used to get the pointer for the first | |
125 | * graph_object of the link. | |
126 | * @source: Part of a union. Used only if the first object (gobj0) is | |
127 | * a pad. In that case, it represents the source pad. | |
128 | * @intf: Part of a union. Used only if the first object (gobj0) is | |
129 | * an interface. | |
130 | * @gobj1: Part of a union. Used to get the pointer for the second | |
131 | * graph_object of the link. | |
132 | * @source: Part of a union. Used only if the second object (gobj1) is | |
133 | * a pad. In that case, it represents the sink pad. | |
134 | * @entity: Part of a union. Used only if the second object (gobj1) is | |
135 | * an entity. | |
136 | * @reverse: Pointer to the link for the reverse direction of a pad to pad | |
137 | * link. | |
138 | * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*) | |
39d1ebc6 | 139 | * @is_backlink: Indicate if the link is a backlink. |
c358e80d | 140 | */ |
53e269c1 | 141 | struct media_link { |
6b6a4278 | 142 | struct media_gobj graph_obj; |
57208e5e | 143 | struct list_head list; |
4b8a3c08 MCC |
144 | union { |
145 | struct media_gobj *gobj0; | |
146 | struct media_pad *source; | |
86e26620 | 147 | struct media_interface *intf; |
4b8a3c08 MCC |
148 | }; |
149 | union { | |
150 | struct media_gobj *gobj1; | |
151 | struct media_pad *sink; | |
86e26620 | 152 | struct media_entity *entity; |
4b8a3c08 | 153 | }; |
c358e80d MCC |
154 | struct media_link *reverse; |
155 | unsigned long flags; | |
39d1ebc6 | 156 | bool is_backlink; |
53e269c1 LP |
157 | }; |
158 | ||
c358e80d MCC |
159 | /** |
160 | * struct media_pad - A media pad graph object. | |
161 | * | |
162 | * @graph_obj: Embedded structure containing the media object common data | |
163 | * @entity: Entity this pad belongs to | |
164 | * @index: Pad index in the entity pads array, numbered from 0 to n | |
165 | * @flags: Pad flags, as defined in uapi/media.h (MEDIA_PAD_FL_*) | |
166 | */ | |
53e269c1 | 167 | struct media_pad { |
4b8a3c08 | 168 | struct media_gobj graph_obj; /* must be first field in struct */ |
c358e80d MCC |
169 | struct media_entity *entity; |
170 | u16 index; | |
171 | unsigned long flags; | |
53e269c1 LP |
172 | }; |
173 | ||
5a5394be LP |
174 | /** |
175 | * struct media_entity_operations - Media entity operations | |
176 | * @link_setup: Notify the entity of link changes. The operation can | |
177 | * return an error, in which case link setup will be | |
178 | * cancelled. Optional. | |
179 | * @link_validate: Return whether a link is valid from the entity point of | |
180 | * view. The media_entity_pipeline_start() function | |
181 | * validates all links by calling this operation. Optional. | |
5ed470fe MCC |
182 | * |
183 | * Note: Those these callbacks are called with struct media_device.@graph_mutex | |
184 | * mutex held. | |
5a5394be | 185 | */ |
97548ed4 LP |
186 | struct media_entity_operations { |
187 | int (*link_setup)(struct media_entity *entity, | |
188 | const struct media_pad *local, | |
189 | const struct media_pad *remote, u32 flags); | |
af88be38 | 190 | int (*link_validate)(struct media_link *link); |
97548ed4 LP |
191 | }; |
192 | ||
b76a2a8c LP |
193 | /** |
194 | * enum media_entity_type - Media entity type | |
195 | * | |
196 | * @MEDIA_ENTITY_TYPE_BASE: | |
197 | * The entity isn't embedded in another subsystem structure. | |
198 | * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE: | |
199 | * The entity is embedded in a struct video_device instance. | |
200 | * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV: | |
201 | * The entity is embedded in a struct v4l2_subdev instance. | |
202 | * | |
203 | * Media entity objects are often not instantiated directly, but the media | |
204 | * entity structure is inherited by (through embedding) other subsystem-specific | |
205 | * structures. The media entity type identifies the type of the subclass | |
206 | * structure that implements a media entity instance. | |
207 | * | |
208 | * This allows runtime type identification of media entities and safe casting to | |
209 | * the correct object type. For instance, a media entity structure instance | |
210 | * embedded in a v4l2_subdev structure instance will have the type | |
211 | * MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a v4l2_subdev | |
212 | * structure using the container_of() macro. | |
213 | */ | |
214 | enum media_entity_type { | |
215 | MEDIA_ENTITY_TYPE_BASE, | |
216 | MEDIA_ENTITY_TYPE_VIDEO_DEVICE, | |
217 | MEDIA_ENTITY_TYPE_V4L2_SUBDEV, | |
218 | }; | |
219 | ||
c358e80d MCC |
220 | /** |
221 | * struct media_entity - A media entity graph object. | |
222 | * | |
223 | * @graph_obj: Embedded structure containing the media object common data. | |
224 | * @name: Entity name. | |
b76a2a8c | 225 | * @obj_type: Type of the object that implements the media_entity. |
0e576b76 MCC |
226 | * @function: Entity main function, as defined in uapi/media.h |
227 | * (MEDIA_ENT_F_*) | |
c358e80d | 228 | * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*) |
c358e80d MCC |
229 | * @num_pads: Number of sink and source pads. |
230 | * @num_links: Total number of links, forward and back, enabled and disabled. | |
231 | * @num_backlinks: Number of backlinks | |
665faa97 SA |
232 | * @internal_idx: An unique internal entity specific number. The numbers are |
233 | * re-used if entities are unregistered or registered again. | |
c358e80d MCC |
234 | * @pads: Pads array with the size defined by @num_pads. |
235 | * @links: List of data links. | |
236 | * @ops: Entity operations. | |
237 | * @stream_count: Stream count for the entity. | |
238 | * @use_count: Use count for the entity. | |
239 | * @pipe: Pipeline this entity belongs to. | |
240 | * @info: Union with devnode information. Kept just for backward | |
241 | * compatibility. | |
242 | * @major: Devnode major number (zero if not applicable). Kept just | |
243 | * for backward compatibility. | |
244 | * @minor: Devnode minor number (zero if not applicable). Kept just | |
245 | * for backward compatibility. | |
246 | * | |
247 | * NOTE: @stream_count and @use_count reference counts must never be | |
248 | * negative, but are signed integers on purpose: a simple WARN_ON(<0) check | |
249 | * can be used to detect reference count bugs that would make them negative. | |
250 | */ | |
53e269c1 | 251 | struct media_entity { |
4b8a3c08 | 252 | struct media_gobj graph_obj; /* must be first field in struct */ |
c358e80d | 253 | const char *name; |
b76a2a8c | 254 | enum media_entity_type obj_type; |
0e576b76 | 255 | u32 function; |
c358e80d | 256 | unsigned long flags; |
53e269c1 | 257 | |
c358e80d MCC |
258 | u16 num_pads; |
259 | u16 num_links; | |
260 | u16 num_backlinks; | |
665faa97 | 261 | int internal_idx; |
53e269c1 | 262 | |
c358e80d MCC |
263 | struct media_pad *pads; |
264 | struct list_head links; | |
53e269c1 | 265 | |
c358e80d | 266 | const struct media_entity_operations *ops; |
97548ed4 | 267 | |
503c3d82 LP |
268 | /* Reference counts must never be negative, but are signed integers on |
269 | * purpose: a simple WARN_ON(<0) check can be used to detect reference | |
270 | * count bugs that would make them negative. | |
271 | */ | |
c358e80d MCC |
272 | int stream_count; |
273 | int use_count; | |
503c3d82 | 274 | |
c358e80d | 275 | struct media_pipeline *pipe; |
e02188c9 | 276 | |
53e269c1 | 277 | union { |
53e269c1 LP |
278 | struct { |
279 | u32 major; | |
280 | u32 minor; | |
e31a0ba7 | 281 | } dev; |
fa5034c6 | 282 | } info; |
53e269c1 LP |
283 | }; |
284 | ||
27e543fa | 285 | /** |
c358e80d | 286 | * struct media_interface - A media interface graph object. |
27e543fa MCC |
287 | * |
288 | * @graph_obj: embedded graph object | |
86e26620 | 289 | * @links: List of links pointing to graph entities |
c358e80d | 290 | * @type: Type of the interface as defined in the |
27e543fa MCC |
291 | * uapi/media/media.h header, e. g. |
292 | * MEDIA_INTF_T_* | |
c358e80d | 293 | * @flags: Interface flags as defined in uapi/media/media.h |
27e543fa MCC |
294 | */ |
295 | struct media_interface { | |
296 | struct media_gobj graph_obj; | |
86e26620 | 297 | struct list_head links; |
27e543fa MCC |
298 | u32 type; |
299 | u32 flags; | |
300 | }; | |
301 | ||
302 | /** | |
c358e80d | 303 | * struct media_intf_devnode - A media interface via a device node. |
27e543fa MCC |
304 | * |
305 | * @intf: embedded interface object | |
306 | * @major: Major number of a device node | |
307 | * @minor: Minor number of a device node | |
308 | */ | |
309 | struct media_intf_devnode { | |
310 | struct media_interface intf; | |
c398bb64 MCC |
311 | |
312 | /* Should match the fields at media_v2_intf_devnode */ | |
27e543fa MCC |
313 | u32 major; |
314 | u32 minor; | |
315 | }; | |
316 | ||
60266185 MCC |
317 | /** |
318 | * media_entity_id() - return the media entity graph object id | |
319 | * | |
320 | * @entity: pointer to entity | |
321 | */ | |
fa762394 MCC |
322 | static inline u32 media_entity_id(struct media_entity *entity) |
323 | { | |
bfab2aac | 324 | return entity->graph_obj.id; |
fa762394 MCC |
325 | } |
326 | ||
60266185 MCC |
327 | /** |
328 | * media_type() - return the media object type | |
329 | * | |
330 | * @gobj: pointer to the media graph object | |
331 | */ | |
ec6e4c95 MCC |
332 | static inline enum media_gobj_type media_type(struct media_gobj *gobj) |
333 | { | |
05b3b77c | 334 | return gobj->id >> MEDIA_BITS_PER_ID; |
ec6e4c95 MCC |
335 | } |
336 | ||
630c0e80 MCC |
337 | /** |
338 | * media_id() - return the media object ID | |
339 | * | |
340 | * @gobj: pointer to the media graph object | |
341 | */ | |
05b3b77c | 342 | static inline u32 media_id(struct media_gobj *gobj) |
ec6e4c95 | 343 | { |
05b3b77c | 344 | return gobj->id & MEDIA_ID_MASK; |
ec6e4c95 MCC |
345 | } |
346 | ||
630c0e80 MCC |
347 | /** |
348 | * media_gobj_gen_id() - encapsulates type and ID on at the object ID | |
349 | * | |
350 | * @type: object type as define at enum &media_gobj_type. | |
351 | * @local_id: next ID, from struct &media_device.@id. | |
352 | */ | |
05b3b77c | 353 | static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id) |
ec6e4c95 MCC |
354 | { |
355 | u32 id; | |
356 | ||
05b3b77c MCC |
357 | id = type << MEDIA_BITS_PER_ID; |
358 | id |= local_id & MEDIA_ID_MASK; | |
ec6e4c95 MCC |
359 | |
360 | return id; | |
361 | } | |
362 | ||
60266185 | 363 | /** |
45b46879 | 364 | * is_media_entity_v4l2_video_device() - Check if the entity is a video_device |
60266185 MCC |
365 | * @entity: pointer to entity |
366 | * | |
b76a2a8c LP |
367 | * Return: true if the entity is an instance of a video_device object and can |
368 | * safely be cast to a struct video_device using the container_of() macro, or | |
369 | * false otherwise. | |
60266185 | 370 | */ |
45b46879 | 371 | static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity) |
fa17b46a | 372 | { |
b76a2a8c | 373 | return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE; |
fa17b46a MCC |
374 | } |
375 | ||
60266185 | 376 | /** |
b76a2a8c | 377 | * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev |
60266185 MCC |
378 | * @entity: pointer to entity |
379 | * | |
b76a2a8c LP |
380 | * Return: true if the entity is an instance of a v4l2_subdev object and can |
381 | * safely be cast to a struct v4l2_subdev using the container_of() macro, or | |
382 | * false otherwise. | |
60266185 | 383 | */ |
fa17b46a MCC |
384 | static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity) |
385 | { | |
b76a2a8c | 386 | return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV; |
fa17b46a MCC |
387 | } |
388 | ||
92777994 MCC |
389 | /** |
390 | * __media_entity_enum_init - Initialise an entity enumeration | |
391 | * | |
392 | * @ent_enum: Entity enumeration to be initialised | |
393 | * @idx_max: Maximum number of entities in the enumeration | |
394 | * | |
395 | * Return: Returns zero on success or a negative error code. | |
396 | */ | |
c8d54cd5 SA |
397 | __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum, |
398 | int idx_max); | |
92777994 MCC |
399 | |
400 | /** | |
401 | * media_entity_enum_cleanup - Release resources of an entity enumeration | |
402 | * | |
403 | * @ent_enum: Entity enumeration to be released | |
404 | */ | |
405 | void media_entity_enum_cleanup(struct media_entity_enum *ent_enum); | |
a5ccc48a | 406 | |
c8d54cd5 SA |
407 | /** |
408 | * media_entity_enum_zero - Clear the entire enum | |
409 | * | |
03e49338 | 410 | * @ent_enum: Entity enumeration to be cleared |
ef69ee1b | 411 | */ |
c8d54cd5 SA |
412 | static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum) |
413 | { | |
414 | bitmap_zero(ent_enum->bmap, ent_enum->idx_max); | |
415 | } | |
416 | ||
417 | /** | |
418 | * media_entity_enum_set - Mark a single entity in the enum | |
419 | * | |
03e49338 | 420 | * @ent_enum: Entity enumeration |
c8d54cd5 SA |
421 | * @entity: Entity to be marked |
422 | */ | |
423 | static inline void media_entity_enum_set(struct media_entity_enum *ent_enum, | |
424 | struct media_entity *entity) | |
425 | { | |
426 | if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) | |
427 | return; | |
428 | ||
429 | __set_bit(entity->internal_idx, ent_enum->bmap); | |
430 | } | |
431 | ||
432 | /** | |
433 | * media_entity_enum_clear - Unmark a single entity in the enum | |
434 | * | |
03e49338 | 435 | * @ent_enum: Entity enumeration |
c8d54cd5 SA |
436 | * @entity: Entity to be unmarked |
437 | */ | |
438 | static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum, | |
439 | struct media_entity *entity) | |
440 | { | |
441 | if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) | |
442 | return; | |
443 | ||
444 | __clear_bit(entity->internal_idx, ent_enum->bmap); | |
445 | } | |
446 | ||
447 | /** | |
448 | * media_entity_enum_test - Test whether the entity is marked | |
449 | * | |
03e49338 | 450 | * @ent_enum: Entity enumeration |
c8d54cd5 SA |
451 | * @entity: Entity to be tested |
452 | * | |
453 | * Returns true if the entity was marked. | |
454 | */ | |
455 | static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum, | |
456 | struct media_entity *entity) | |
457 | { | |
458 | if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) | |
459 | return true; | |
460 | ||
461 | return test_bit(entity->internal_idx, ent_enum->bmap); | |
462 | } | |
463 | ||
464 | /** | |
465 | * media_entity_enum_test - Test whether the entity is marked, and mark it | |
466 | * | |
03e49338 | 467 | * @ent_enum: Entity enumeration |
c8d54cd5 SA |
468 | * @entity: Entity to be tested |
469 | * | |
470 | * Returns true if the entity was marked, and mark it before doing so. | |
471 | */ | |
03e49338 MCC |
472 | static inline bool |
473 | media_entity_enum_test_and_set(struct media_entity_enum *ent_enum, | |
474 | struct media_entity *entity) | |
c8d54cd5 SA |
475 | { |
476 | if (WARN_ON(entity->internal_idx >= ent_enum->idx_max)) | |
477 | return true; | |
478 | ||
479 | return __test_and_set_bit(entity->internal_idx, ent_enum->bmap); | |
480 | } | |
481 | ||
482 | /** | |
03e49338 | 483 | * media_entity_enum_empty - Test whether the entire enum is empty |
c8d54cd5 | 484 | * |
03e49338 | 485 | * @ent_enum: Entity enumeration |
c8d54cd5 SA |
486 | * |
487 | * Returns true if the entity was marked. | |
488 | */ | |
489 | static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum) | |
490 | { | |
491 | return bitmap_empty(ent_enum->bmap, ent_enum->idx_max); | |
492 | } | |
493 | ||
494 | /** | |
495 | * media_entity_enum_intersects - Test whether two enums intersect | |
496 | * | |
03e49338 MCC |
497 | * @ent_enum1: First entity enumeration |
498 | * @ent_enum2: Second entity enumeration | |
c8d54cd5 SA |
499 | * |
500 | * Returns true if entity enumerations e and f intersect, otherwise false. | |
501 | */ | |
502 | static inline bool media_entity_enum_intersects( | |
503 | struct media_entity_enum *ent_enum1, | |
504 | struct media_entity_enum *ent_enum2) | |
505 | { | |
506 | WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max); | |
507 | ||
508 | return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap, | |
509 | min(ent_enum1->idx_max, ent_enum2->idx_max)); | |
510 | } | |
ef69ee1b | 511 | |
ec6e4c95 MCC |
512 | #define gobj_to_entity(gobj) \ |
513 | container_of(gobj, struct media_entity, graph_obj) | |
514 | ||
39a956c4 MCC |
515 | #define gobj_to_pad(gobj) \ |
516 | container_of(gobj, struct media_pad, graph_obj) | |
517 | ||
518 | #define gobj_to_link(gobj) \ | |
519 | container_of(gobj, struct media_link, graph_obj) | |
520 | ||
27e543fa MCC |
521 | #define gobj_to_link(gobj) \ |
522 | container_of(gobj, struct media_link, graph_obj) | |
523 | ||
524 | #define gobj_to_pad(gobj) \ | |
525 | container_of(gobj, struct media_pad, graph_obj) | |
526 | ||
527 | #define gobj_to_intf(gobj) \ | |
528 | container_of(gobj, struct media_interface, graph_obj) | |
529 | ||
530 | #define intf_to_devnode(intf) \ | |
531 | container_of(intf, struct media_intf_devnode, intf) | |
532 | ||
1fc25d30 MCC |
533 | /** |
534 | * media_gobj_create - Initialize a graph object | |
535 | * | |
536 | * @mdev: Pointer to the media_device that contains the object | |
537 | * @type: Type of the object | |
538 | * @gobj: Pointer to the graph object | |
539 | * | |
540 | * This routine initializes the embedded struct media_gobj inside a | |
541 | * media graph object. It is called automatically if media_*_create() | |
542 | * calls are used. However, if the object (entity, link, pad, interface) | |
543 | * is embedded on some other object, this function should be called before | |
544 | * registering the object at the media controller. | |
545 | */ | |
c350ef83 | 546 | void media_gobj_create(struct media_device *mdev, |
ec6e4c95 MCC |
547 | enum media_gobj_type type, |
548 | struct media_gobj *gobj); | |
1fc25d30 MCC |
549 | |
550 | /** | |
551 | * media_gobj_destroy - Stop using a graph object on a media device | |
552 | * | |
553 | * @gobj: Pointer to the graph object | |
554 | * | |
555 | * This should be called by all routines like media_device_unregister() | |
556 | * that remove/destroy media graph objects. | |
557 | */ | |
c350ef83 | 558 | void media_gobj_destroy(struct media_gobj *gobj); |
ec6e4c95 | 559 | |
db7ee32a MCC |
560 | /** |
561 | * media_entity_pads_init() - Initialize the entity pads | |
562 | * | |
563 | * @entity: entity where the pads belong | |
1fc25d30 MCC |
564 | * @num_pads: total number of sink and source pads |
565 | * @pads: Array of @num_pads pads. | |
566 | * | |
567 | * The pads array is managed by the entity driver and passed to | |
568 | * media_entity_pads_init() where its pointer will be stored in the entity | |
569 | * structure. | |
db7ee32a MCC |
570 | * |
571 | * If no pads are needed, drivers could either directly fill | |
572 | * &media_entity->@num_pads with 0 and &media_entity->@pads with NULL or call | |
573 | * this function that will do the same. | |
574 | * | |
575 | * As the number of pads is known in advance, the pads array is not allocated | |
576 | * dynamically but is managed by the entity driver. Most drivers will embed the | |
577 | * pads array in a driver-specific structure, avoiding dynamic allocation. | |
578 | * | |
579 | * Drivers must set the direction of every pad in the pads array before calling | |
580 | * media_entity_pads_init(). The function will initialize the other pads fields. | |
581 | */ | |
ab22e77c | 582 | int media_entity_pads_init(struct media_entity *entity, u16 num_pads, |
57208e5e | 583 | struct media_pad *pads); |
f1fd3289 | 584 | |
db7ee32a MCC |
585 | /** |
586 | * media_entity_cleanup() - free resources associated with an entity | |
587 | * | |
588 | * @entity: entity where the pads belong | |
589 | * | |
590 | * This function must be called during the cleanup phase after unregistering | |
591 | * the entity (currently, it does nothing). | |
592 | */ | |
f1fd3289 | 593 | static inline void media_entity_cleanup(struct media_entity *entity) {}; |
e02188c9 | 594 | |
db7ee32a MCC |
595 | /** |
596 | * media_create_pad_link() - creates a link between two entities. | |
597 | * | |
598 | * @source: pointer to &media_entity of the source pad. | |
599 | * @source_pad: number of the source pad in the pads array | |
600 | * @sink: pointer to &media_entity of the sink pad. | |
601 | * @sink_pad: number of the sink pad in the pads array. | |
602 | * @flags: Link flags, as defined in include/uapi/linux/media.h. | |
603 | * | |
604 | * Valid values for flags: | |
605 | * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be | |
606 | * used to transfer media data. When two or more links target a sink pad, | |
607 | * only one of them can be enabled at a time. | |
608 | * | |
609 | * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't | |
610 | * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then | |
611 | * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is | |
612 | * always enabled. | |
613 | * | |
614 | * NOTE: | |
615 | * | |
616 | * Before calling this function, media_entity_pads_init() and | |
617 | * media_device_register_entity() should be called previously for both ends. | |
618 | */ | |
77328043 MCC |
619 | __must_check int media_create_pad_link(struct media_entity *source, |
620 | u16 source_pad, struct media_entity *sink, | |
621 | u16 sink_pad, u32 flags); | |
b01cc9ce MCC |
622 | |
623 | /** | |
624 | * media_create_pad_links() - creates a link between two entities. | |
625 | * | |
626 | * @mdev: Pointer to the media_device that contains the object | |
627 | * @source_function: Function of the source entities. Used only if @source is | |
628 | * NULL. | |
629 | * @source: pointer to &media_entity of the source pad. If NULL, it will use | |
630 | * all entities that matches the @sink_function. | |
631 | * @source_pad: number of the source pad in the pads array | |
632 | * @sink_function: Function of the sink entities. Used only if @sink is NULL. | |
633 | * @sink: pointer to &media_entity of the sink pad. If NULL, it will use | |
634 | * all entities that matches the @sink_function. | |
635 | * @sink_pad: number of the sink pad in the pads array. | |
636 | * @flags: Link flags, as defined in include/uapi/linux/media.h. | |
637 | * @allow_both_undefined: if true, then both @source and @sink can be NULL. | |
638 | * In such case, it will create a crossbar between all entities that | |
639 | * matches @source_function to all entities that matches @sink_function. | |
640 | * If false, it will return 0 and won't create any link if both @source | |
641 | * and @sink are NULL. | |
642 | * | |
643 | * Valid values for flags: | |
644 | * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be | |
645 | * used to transfer media data. If multiple links are created and this | |
646 | * flag is passed as an argument, only the first created link will have | |
647 | * this flag. | |
648 | * | |
649 | * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't | |
650 | * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then | |
651 | * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is | |
652 | * always enabled. | |
653 | * | |
654 | * It is common for some devices to have multiple source and/or sink entities | |
655 | * of the same type that should be linked. While media_create_pad_link() | |
656 | * creates link by link, this function is meant to allow 1:n, n:1 and even | |
657 | * cross-bar (n:n) links. | |
658 | * | |
659 | * NOTE: Before calling this function, media_entity_pads_init() and | |
660 | * media_device_register_entity() should be called previously for the entities | |
661 | * to be linked. | |
662 | */ | |
663 | int media_create_pad_links(const struct media_device *mdev, | |
664 | const u32 source_function, | |
665 | struct media_entity *source, | |
666 | const u16 source_pad, | |
667 | const u32 sink_function, | |
668 | struct media_entity *sink, | |
669 | const u16 sink_pad, | |
670 | u32 flags, | |
671 | const bool allow_both_undefined); | |
672 | ||
7349cec1 | 673 | void __media_entity_remove_links(struct media_entity *entity); |
db7ee32a MCC |
674 | |
675 | /** | |
676 | * media_entity_remove_links() - remove all links associated with an entity | |
677 | * | |
678 | * @entity: pointer to &media_entity | |
679 | * | |
680 | * Note: this is called automatically when an entity is unregistered via | |
681 | * media_device_register_entity(). | |
682 | */ | |
7349cec1 SN |
683 | void media_entity_remove_links(struct media_entity *entity); |
684 | ||
1fc25d30 MCC |
685 | /** |
686 | * __media_entity_setup_link - Configure a media link without locking | |
687 | * @link: The link being configured | |
688 | * @flags: Link configuration flags | |
689 | * | |
690 | * The bulk of link setup is handled by the two entities connected through the | |
691 | * link. This function notifies both entities of the link configuration change. | |
692 | * | |
693 | * If the link is immutable or if the current and new configuration are | |
694 | * identical, return immediately. | |
695 | * | |
696 | * The user is expected to hold link->source->parent->mutex. If not, | |
697 | * media_entity_setup_link() should be used instead. | |
698 | */ | |
97548ed4 | 699 | int __media_entity_setup_link(struct media_link *link, u32 flags); |
db7ee32a MCC |
700 | |
701 | /** | |
702 | * media_entity_setup_link() - changes the link flags properties in runtime | |
703 | * | |
704 | * @link: pointer to &media_link | |
705 | * @flags: the requested new link flags | |
706 | * | |
707 | * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag | |
708 | * flag to enable/disable a link. Links marked with the | |
709 | * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled. | |
710 | * | |
711 | * When a link is enabled or disabled, the media framework calls the | |
712 | * link_setup operation for the two entities at the source and sink of the | |
713 | * link, in that order. If the second link_setup call fails, another | |
714 | * link_setup call is made on the first entity to restore the original link | |
715 | * flags. | |
716 | * | |
717 | * Media device drivers can be notified of link setup operations by setting the | |
718 | * media_device::link_notify pointer to a callback function. If provided, the | |
719 | * notification callback will be called before enabling and after disabling | |
720 | * links. | |
721 | * | |
722 | * Entity drivers must implement the link_setup operation if any of their links | |
723 | * is non-immutable. The operation must either configure the hardware or store | |
724 | * the configuration information to be applied later. | |
725 | * | |
726 | * Link configuration must not have any side effect on other links. If an | |
727 | * enabled link at a sink pad prevents another link at the same pad from | |
728 | * being enabled, the link_setup operation must return -EBUSY and can't | |
729 | * implicitly disable the first enabled link. | |
730 | * | |
731 | * NOTE: the valid values of the flags for the link is the same as described | |
732 | * on media_create_pad_link(), for pad to pad links or the same as described | |
733 | * on media_create_intf_link(), for interface to entity links. | |
734 | */ | |
97548ed4 | 735 | int media_entity_setup_link(struct media_link *link, u32 flags); |
1fc25d30 MCC |
736 | |
737 | /** | |
738 | * media_entity_find_link - Find a link between two pads | |
739 | * @source: Source pad | |
740 | * @sink: Sink pad | |
741 | * | |
742 | * Return a pointer to the link between the two entities. If no such link | |
743 | * exists, return NULL. | |
744 | */ | |
97548ed4 LP |
745 | struct media_link *media_entity_find_link(struct media_pad *source, |
746 | struct media_pad *sink); | |
1fc25d30 MCC |
747 | |
748 | /** | |
749 | * media_entity_remote_pad - Find the pad at the remote end of a link | |
750 | * @pad: Pad at the local end of the link | |
751 | * | |
752 | * Search for a remote pad connected to the given pad by iterating over all | |
753 | * links originating or terminating at that pad until an enabled link is found. | |
754 | * | |
755 | * Return a pointer to the pad at the remote end of the first found enabled | |
756 | * link, or NULL if no enabled link has been found. | |
757 | */ | |
1bddf1b3 | 758 | struct media_pad *media_entity_remote_pad(struct media_pad *pad); |
53e269c1 | 759 | |
1fc25d30 MCC |
760 | /** |
761 | * media_entity_get - Get a reference to the parent module | |
762 | * | |
763 | * @entity: The entity | |
764 | * | |
765 | * Get a reference to the parent media device module. | |
766 | * | |
767 | * The function will return immediately if @entity is NULL. | |
768 | * | |
769 | * Return a pointer to the entity on success or NULL on failure. | |
770 | */ | |
503c3d82 | 771 | struct media_entity *media_entity_get(struct media_entity *entity); |
1fc25d30 | 772 | |
e03d2203 SA |
773 | __must_check int media_entity_graph_walk_init( |
774 | struct media_entity_graph *graph, struct media_device *mdev); | |
aa360d3d MCC |
775 | |
776 | /** | |
777 | * media_entity_graph_walk_cleanup - Release resources used by graph walk. | |
778 | * | |
779 | * @graph: Media graph structure that will be used to walk the graph | |
780 | */ | |
e03d2203 SA |
781 | void media_entity_graph_walk_cleanup(struct media_entity_graph *graph); |
782 | ||
1fc25d30 MCC |
783 | /** |
784 | * media_entity_put - Release the reference to the parent module | |
785 | * | |
786 | * @entity: The entity | |
787 | * | |
788 | * Release the reference count acquired by media_entity_get(). | |
789 | * | |
790 | * The function will return immediately if @entity is NULL. | |
791 | */ | |
503c3d82 LP |
792 | void media_entity_put(struct media_entity *entity); |
793 | ||
1fc25d30 MCC |
794 | /** |
795 | * media_entity_graph_walk_start - Start walking the media graph at a given entity | |
796 | * @graph: Media graph structure that will be used to walk the graph | |
797 | * @entity: Starting entity | |
798 | * | |
e03d2203 SA |
799 | * Before using this function, media_entity_graph_walk_init() must be |
800 | * used to allocate resources used for walking the graph. This | |
801 | * function initializes the graph traversal structure to walk the | |
802 | * entities graph starting at the given entity. The traversal | |
803 | * structure must not be modified by the caller during graph | |
804 | * traversal. After the graph walk, the resources must be released | |
805 | * using media_entity_graph_walk_cleanup(). | |
1fc25d30 | 806 | */ |
a5ccc48a | 807 | void media_entity_graph_walk_start(struct media_entity_graph *graph, |
e03d2203 | 808 | struct media_entity *entity); |
1fc25d30 MCC |
809 | |
810 | /** | |
811 | * media_entity_graph_walk_next - Get the next entity in the graph | |
812 | * @graph: Media graph structure | |
813 | * | |
814 | * Perform a depth-first traversal of the given media entities graph. | |
815 | * | |
816 | * The graph structure must have been previously initialized with a call to | |
817 | * media_entity_graph_walk_start(). | |
818 | * | |
819 | * Return the next entity in the graph or NULL if the whole graph have been | |
820 | * traversed. | |
821 | */ | |
a5ccc48a SA |
822 | struct media_entity * |
823 | media_entity_graph_walk_next(struct media_entity_graph *graph); | |
1fc25d30 MCC |
824 | |
825 | /** | |
826 | * media_entity_pipeline_start - Mark a pipeline as streaming | |
827 | * @entity: Starting entity | |
828 | * @pipe: Media pipeline to be assigned to all entities in the pipeline. | |
829 | * | |
830 | * Mark all entities connected to a given entity through enabled links, either | |
831 | * directly or indirectly, as streaming. The given pipeline object is assigned to | |
832 | * every entity in the pipeline and stored in the media_entity pipe field. | |
833 | * | |
834 | * Calls to this function can be nested, in which case the same number of | |
835 | * media_entity_pipeline_stop() calls will be required to stop streaming. The | |
836 | * pipeline pointer must be identical for all nested calls to | |
837 | * media_entity_pipeline_start(). | |
838 | */ | |
af88be38 SA |
839 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
840 | struct media_pipeline *pipe); | |
fb49f204 SK |
841 | /** |
842 | * __media_entity_pipeline_start - Mark a pipeline as streaming | |
843 | * | |
844 | * @entity: Starting entity | |
845 | * @pipe: Media pipeline to be assigned to all entities in the pipeline. | |
846 | * | |
847 | * Note: This is the non-locking version of media_entity_pipeline_start() | |
848 | */ | |
849 | __must_check int __media_entity_pipeline_start(struct media_entity *entity, | |
850 | struct media_pipeline *pipe); | |
1fc25d30 MCC |
851 | |
852 | /** | |
853 | * media_entity_pipeline_stop - Mark a pipeline as not streaming | |
854 | * @entity: Starting entity | |
855 | * | |
856 | * Mark all entities connected to a given entity through enabled links, either | |
857 | * directly or indirectly, as not streaming. The media_entity pipe field is | |
858 | * reset to NULL. | |
859 | * | |
860 | * If multiple calls to media_entity_pipeline_start() have been made, the same | |
861 | * number of calls to this function are required to mark the pipeline as not | |
862 | * streaming. | |
863 | */ | |
e02188c9 | 864 | void media_entity_pipeline_stop(struct media_entity *entity); |
a5ccc48a | 865 | |
fb49f204 SK |
866 | /** |
867 | * __media_entity_pipeline_stop - Mark a pipeline as not streaming | |
868 | * | |
869 | * @entity: Starting entity | |
870 | * | |
871 | * Note: This is the non-locking version of media_entity_pipeline_stop() | |
872 | */ | |
873 | void __media_entity_pipeline_stop(struct media_entity *entity); | |
874 | ||
db7ee32a MCC |
875 | /** |
876 | * media_devnode_create() - creates and initializes a device node interface | |
877 | * | |
878 | * @mdev: pointer to struct &media_device | |
879 | * @type: type of the interface, as given by MEDIA_INTF_T_* macros | |
880 | * as defined in the uapi/media/media.h header. | |
881 | * @flags: Interface flags as defined in uapi/media/media.h. | |
882 | * @major: Device node major number. | |
883 | * @minor: Device node minor number. | |
884 | * | |
885 | * Return: if succeeded, returns a pointer to the newly allocated | |
886 | * &media_intf_devnode pointer. | |
887 | */ | |
5e5387df MCC |
888 | struct media_intf_devnode * |
889 | __must_check media_devnode_create(struct media_device *mdev, | |
890 | u32 type, u32 flags, | |
0b3b72df | 891 | u32 major, u32 minor); |
db7ee32a MCC |
892 | /** |
893 | * media_devnode_remove() - removes a device node interface | |
894 | * | |
895 | * @devnode: pointer to &media_intf_devnode to be freed. | |
896 | * | |
897 | * When a device node interface is removed, all links to it are automatically | |
898 | * removed. | |
899 | */ | |
27e543fa | 900 | void media_devnode_remove(struct media_intf_devnode *devnode); |
5e5387df | 901 | struct media_link * |
db7ee32a MCC |
902 | |
903 | /** | |
904 | * media_create_intf_link() - creates a link between an entity and an interface | |
905 | * | |
906 | * @entity: pointer to %media_entity | |
907 | * @intf: pointer to %media_interface | |
908 | * @flags: Link flags, as defined in include/uapi/linux/media.h. | |
909 | * | |
910 | * | |
911 | * Valid values for flags: | |
912 | * The %MEDIA_LNK_FL_ENABLED flag indicates that the interface is connected to | |
913 | * the entity hardware. That's the default value for interfaces. An | |
914 | * interface may be disabled if the hardware is busy due to the usage | |
915 | * of some other interface that it is currently controlling the hardware. | |
916 | * A typical example is an hybrid TV device that handle only one type of | |
917 | * stream on a given time. So, when the digital TV is streaming, | |
918 | * the V4L2 interfaces won't be enabled, as such device is not able to | |
919 | * also stream analog TV or radio. | |
920 | * | |
921 | * Note: | |
922 | * | |
923 | * Before calling this function, media_devnode_create() should be called for | |
924 | * the interface and media_device_register_entity() should be called for the | |
925 | * interface that will be part of the link. | |
926 | */ | |
5e5387df MCC |
927 | __must_check media_create_intf_link(struct media_entity *entity, |
928 | struct media_interface *intf, | |
929 | u32 flags); | |
60266185 MCC |
930 | /** |
931 | * __media_remove_intf_link() - remove a single interface link | |
932 | * | |
933 | * @link: pointer to &media_link. | |
934 | * | |
935 | * Note: this is an unlocked version of media_remove_intf_link() | |
936 | */ | |
d47109fa | 937 | void __media_remove_intf_link(struct media_link *link); |
60266185 MCC |
938 | |
939 | /** | |
940 | * media_remove_intf_link() - remove a single interface link | |
941 | * | |
942 | * @link: pointer to &media_link. | |
943 | * | |
944 | * Note: prefer to use this one, instead of __media_remove_intf_link() | |
945 | */ | |
86e26620 | 946 | void media_remove_intf_link(struct media_link *link); |
60266185 MCC |
947 | |
948 | /** | |
949 | * __media_remove_intf_links() - remove all links associated with an interface | |
950 | * | |
951 | * @intf: pointer to &media_interface | |
952 | * | |
953 | * Note: this is an unlocked version of media_remove_intf_links(). | |
954 | */ | |
7c4696a9 | 955 | void __media_remove_intf_links(struct media_interface *intf); |
92777994 | 956 | |
db7ee32a MCC |
957 | /** |
958 | * media_remove_intf_links() - remove all links associated with an interface | |
959 | * | |
960 | * @intf: pointer to &media_interface | |
961 | * | |
60266185 MCC |
962 | * Notes: |
963 | * | |
964 | * this is called automatically when an entity is unregistered via | |
db7ee32a | 965 | * media_device_register_entity() and by media_devnode_remove(). |
60266185 MCC |
966 | * |
967 | * Prefer to use this one, instead of __media_remove_intf_links(). | |
db7ee32a | 968 | */ |
7c4696a9 MCC |
969 | void media_remove_intf_links(struct media_interface *intf); |
970 | ||
97548ed4 LP |
971 | #define media_entity_call(entity, operation, args...) \ |
972 | (((entity)->ops && (entity)->ops->operation) ? \ | |
973 | (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD) | |
974 | ||
53e269c1 | 975 | #endif |