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
39 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
42 enum media_gobj_type {
46 MEDIA_GRAPH_INTF_DEVNODE,
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)
53 /* Structs to represent the objects that belong to a media graph */
56 * struct media_gobj - Define a graph object.
58 * @id: Non-zero object ID identifier. The ID should be unique
59 * inside a media_device, as it is composed by
60 * MEDIA_BITS_PER_TYPE to store the type plus
61 * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID
62 * (called as "local ID").
64 * All objects on the media graph should have this struct embedded
67 struct media_device *mdev;
69 struct list_head list;
73 struct media_pipeline {
77 struct media_gobj graph_obj;
78 struct list_head list;
80 struct media_gobj *gobj0;
81 struct media_pad *source;
82 struct media_interface *intf;
85 struct media_gobj *gobj1;
86 struct media_pad *sink;
87 struct media_entity *entity;
89 struct media_link *reverse; /* Link in the reverse direction */
90 unsigned long flags; /* Link flags (MEDIA_LNK_FL_*) */
94 struct media_gobj graph_obj; /* must be first field in struct */
95 struct media_entity *entity; /* Entity this pad belongs to */
96 u16 index; /* Pad index in the entity pads array */
97 unsigned long flags; /* Pad flags (MEDIA_PAD_FL_*) */
101 * struct media_entity_operations - Media entity operations
102 * @link_setup: Notify the entity of link changes. The operation can
103 * return an error, in which case link setup will be
104 * cancelled. Optional.
105 * @link_validate: Return whether a link is valid from the entity point of
106 * view. The media_entity_pipeline_start() function
107 * validates all links by calling this operation. Optional.
109 struct media_entity_operations {
110 int (*link_setup)(struct media_entity *entity,
111 const struct media_pad *local,
112 const struct media_pad *remote, u32 flags);
113 int (*link_validate)(struct media_link *link);
116 struct media_entity {
117 struct media_gobj graph_obj; /* must be first field in struct */
118 const char *name; /* Entity name */
119 u32 type; /* Entity type (MEDIA_ENT_T_*) */
120 u32 revision; /* Entity revision, driver specific */
121 unsigned long flags; /* Entity flags (MEDIA_ENT_FL_*) */
122 u32 group_id; /* Entity group ID */
124 u16 num_pads; /* Number of sink and source pads */
125 u16 num_links; /* Number of existing links, both
126 * enabled and disabled */
127 u16 num_backlinks; /* Number of backlinks */
129 struct media_pad *pads; /* Pads array (num_pads objects) */
130 struct list_head links; /* Pad-to-pad links list */
132 const struct media_entity_operations *ops; /* Entity operations */
134 /* Reference counts must never be negative, but are signed integers on
135 * purpose: a simple WARN_ON(<0) check can be used to detect reference
136 * count bugs that would make them negative.
138 int stream_count; /* Stream count for the entity. */
139 int use_count; /* Use count for the entity. */
141 struct media_pipeline *pipe; /* Pipeline this entity belongs to. */
144 /* Node specifications */
150 /* Sub-device specifications */
151 /* Nothing needed yet */
156 * struct media_intf_devnode - Define a Kernel API interface
158 * @graph_obj: embedded graph object
159 * @list: Linked list used to find other interfaces that belong
160 * to the same media controller
161 * @links: List of links pointing to graph entities
162 * @type: Type of the interface as defined at the
163 * uapi/media/media.h header, e. g.
165 * @flags: Interface flags as defined at uapi/media/media.h
167 struct media_interface {
168 struct media_gobj graph_obj;
169 struct list_head links;
175 * struct media_intf_devnode - Define a Kernel API interface via a device node
177 * @intf: embedded interface object
178 * @major: Major number of a device node
179 * @minor: Minor number of a device node
181 struct media_intf_devnode {
182 struct media_interface intf;
184 /* Should match the fields at media_v2_intf_devnode */
189 static inline u32 media_entity_id(struct media_entity *entity)
191 return entity->graph_obj.id;
194 static inline enum media_gobj_type media_type(struct media_gobj *gobj)
196 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
199 static inline u32 media_localid(struct media_gobj *gobj)
201 return gobj->id & MEDIA_LOCAL_ID_MASK;
204 static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
208 id = type << MEDIA_BITS_PER_LOCAL_ID;
209 id |= local_id & MEDIA_LOCAL_ID_MASK;
214 static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
219 switch (entity->type) {
220 case MEDIA_ENT_T_V4L2_VIDEO:
221 case MEDIA_ENT_T_V4L2_VBI:
222 case MEDIA_ENT_T_V4L2_SWRADIO:
229 static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
234 switch (entity->type) {
235 case MEDIA_ENT_T_V4L2_SUBDEV_UNKNOWN:
236 case MEDIA_ENT_T_V4L2_SUBDEV_SENSOR:
237 case MEDIA_ENT_T_V4L2_SUBDEV_FLASH:
238 case MEDIA_ENT_T_V4L2_SUBDEV_LENS:
239 case MEDIA_ENT_T_V4L2_SUBDEV_DECODER:
240 case MEDIA_ENT_T_V4L2_SUBDEV_TUNER:
248 #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
249 #define MEDIA_ENTITY_ENUM_MAX_ID 64
252 * The number of pads can't be bigger than the number of entities,
253 * as the worse-case scenario is to have one entity linked up to
254 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
256 #define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
258 struct media_entity_graph {
260 struct media_entity *entity;
261 struct list_head *link;
262 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
264 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
268 #define gobj_to_entity(gobj) \
269 container_of(gobj, struct media_entity, graph_obj)
271 #define gobj_to_pad(gobj) \
272 container_of(gobj, struct media_pad, graph_obj)
274 #define gobj_to_link(gobj) \
275 container_of(gobj, struct media_link, graph_obj)
277 #define gobj_to_link(gobj) \
278 container_of(gobj, struct media_link, graph_obj)
280 #define gobj_to_pad(gobj) \
281 container_of(gobj, struct media_pad, graph_obj)
283 #define gobj_to_intf(gobj) \
284 container_of(gobj, struct media_interface, graph_obj)
286 #define intf_to_devnode(intf) \
287 container_of(intf, struct media_intf_devnode, intf)
289 void media_gobj_init(struct media_device *mdev,
290 enum media_gobj_type type,
291 struct media_gobj *gobj);
292 void media_gobj_remove(struct media_gobj *gobj);
294 int media_entity_init(struct media_entity *entity, u16 num_pads,
295 struct media_pad *pads);
296 void media_entity_cleanup(struct media_entity *entity);
298 int media_create_pad_link(struct media_entity *source, u16 source_pad,
299 struct media_entity *sink, u16 sink_pad, u32 flags);
300 void __media_entity_remove_links(struct media_entity *entity);
301 void media_entity_remove_links(struct media_entity *entity);
303 int __media_entity_setup_link(struct media_link *link, u32 flags);
304 int media_entity_setup_link(struct media_link *link, u32 flags);
305 struct media_link *media_entity_find_link(struct media_pad *source,
306 struct media_pad *sink);
307 struct media_pad *media_entity_remote_pad(struct media_pad *pad);
309 struct media_entity *media_entity_get(struct media_entity *entity);
310 void media_entity_put(struct media_entity *entity);
312 void media_entity_graph_walk_start(struct media_entity_graph *graph,
313 struct media_entity *entity);
314 struct media_entity *
315 media_entity_graph_walk_next(struct media_entity_graph *graph);
316 __must_check int media_entity_pipeline_start(struct media_entity *entity,
317 struct media_pipeline *pipe);
318 void media_entity_pipeline_stop(struct media_entity *entity);
320 struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
322 u32 major, u32 minor,
324 void media_devnode_remove(struct media_intf_devnode *devnode);
325 struct media_link *media_create_intf_link(struct media_entity *entity,
326 struct media_interface *intf,
328 void __media_remove_intf_link(struct media_link *link);
329 void media_remove_intf_link(struct media_link *link);
330 void __media_remove_intf_links(struct media_interface *intf);
331 void media_remove_intf_links(struct media_interface *intf);
334 #define media_entity_call(entity, operation, args...) \
335 (((entity)->ops && (entity)->ops->operation) ? \
336 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)