Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 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 | |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Sakari Ailus | 0149a2e | 2013-12-13 08:58:37 -0300 | [diff] [blame] | 27 | #include <linux/kernel.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 28 | #include <linux/list.h> |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 29 | #include <linux/media.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 30 | |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame^] | 31 | /* Enums used internally at the media controller to represent graphs */ |
| 32 | |
| 33 | /** |
| 34 | * enum media_gobj_type - type of a graph object |
| 35 | * |
| 36 | */ |
| 37 | enum media_gobj_type { |
| 38 | /* FIXME: add the types here, as we embed media_gobj */ |
| 39 | MEDIA_GRAPH_NONE |
| 40 | }; |
| 41 | |
| 42 | #define MEDIA_BITS_PER_TYPE 8 |
| 43 | #define MEDIA_BITS_PER_LOCAL_ID (32 - MEDIA_BITS_PER_TYPE) |
| 44 | #define MEDIA_LOCAL_ID_MASK GENMASK(MEDIA_BITS_PER_LOCAL_ID - 1, 0) |
| 45 | |
| 46 | /* Structs to represent the objects that belong to a media graph */ |
| 47 | |
| 48 | /** |
| 49 | * struct media_gobj - Define a graph object. |
| 50 | * |
| 51 | * @id: Non-zero object ID identifier. The ID should be unique |
| 52 | * inside a media_device, as it is composed by |
| 53 | * MEDIA_BITS_PER_TYPE to store the type plus |
| 54 | * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID |
| 55 | * (called as "local ID"). |
| 56 | * |
| 57 | * All objects on the media graph should have this struct embedded |
| 58 | */ |
| 59 | struct media_gobj { |
| 60 | u32 id; |
| 61 | }; |
| 62 | |
| 63 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 64 | struct media_pipeline { |
| 65 | }; |
| 66 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 67 | struct media_link { |
| 68 | struct media_pad *source; /* Source pad */ |
| 69 | struct media_pad *sink; /* Sink pad */ |
| 70 | struct media_link *reverse; /* Link in the reverse direction */ |
| 71 | unsigned long flags; /* Link flags (MEDIA_LNK_FL_*) */ |
| 72 | }; |
| 73 | |
| 74 | struct media_pad { |
| 75 | struct media_entity *entity; /* Entity this pad belongs to */ |
| 76 | u16 index; /* Pad index in the entity pads array */ |
| 77 | unsigned long flags; /* Pad flags (MEDIA_PAD_FL_*) */ |
| 78 | }; |
| 79 | |
Laurent Pinchart | 5a5394b | 2014-03-26 00:01:44 -0300 | [diff] [blame] | 80 | /** |
| 81 | * struct media_entity_operations - Media entity operations |
| 82 | * @link_setup: Notify the entity of link changes. The operation can |
| 83 | * return an error, in which case link setup will be |
| 84 | * cancelled. Optional. |
| 85 | * @link_validate: Return whether a link is valid from the entity point of |
| 86 | * view. The media_entity_pipeline_start() function |
| 87 | * validates all links by calling this operation. Optional. |
| 88 | */ |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 89 | struct media_entity_operations { |
| 90 | int (*link_setup)(struct media_entity *entity, |
| 91 | const struct media_pad *local, |
| 92 | const struct media_pad *remote, u32 flags); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 93 | int (*link_validate)(struct media_link *link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 94 | }; |
| 95 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 96 | struct media_entity { |
| 97 | struct list_head list; |
| 98 | struct media_device *parent; /* Media device this entity belongs to*/ |
| 99 | u32 id; /* Entity ID, unique in the parent media |
| 100 | * device context */ |
| 101 | const char *name; /* Entity name */ |
| 102 | u32 type; /* Entity type (MEDIA_ENT_T_*) */ |
| 103 | u32 revision; /* Entity revision, driver specific */ |
| 104 | unsigned long flags; /* Entity flags (MEDIA_ENT_FL_*) */ |
| 105 | u32 group_id; /* Entity group ID */ |
| 106 | |
| 107 | u16 num_pads; /* Number of sink and source pads */ |
| 108 | u16 num_links; /* Number of existing links, both |
| 109 | * enabled and disabled */ |
| 110 | u16 num_backlinks; /* Number of backlinks */ |
| 111 | u16 max_links; /* Maximum number of links */ |
| 112 | |
| 113 | struct media_pad *pads; /* Pads array (num_pads elements) */ |
| 114 | struct media_link *links; /* Links array (max_links elements)*/ |
| 115 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 116 | const struct media_entity_operations *ops; /* Entity operations */ |
| 117 | |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 118 | /* Reference counts must never be negative, but are signed integers on |
| 119 | * purpose: a simple WARN_ON(<0) check can be used to detect reference |
| 120 | * count bugs that would make them negative. |
| 121 | */ |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 122 | int stream_count; /* Stream count for the entity. */ |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 123 | int use_count; /* Use count for the entity. */ |
| 124 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 125 | struct media_pipeline *pipe; /* Pipeline this entity belongs to. */ |
| 126 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 127 | union { |
| 128 | /* Node specifications */ |
| 129 | struct { |
| 130 | u32 major; |
| 131 | u32 minor; |
Mauro Carvalho Chehab | e31a0ba | 2015-01-02 12:18:23 -0300 | [diff] [blame] | 132 | } dev; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 133 | |
| 134 | /* Sub-device specifications */ |
| 135 | /* Nothing needed yet */ |
Clemens Ladisch | fa5034c | 2011-11-05 18:42:01 -0300 | [diff] [blame] | 136 | } info; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | static inline u32 media_entity_type(struct media_entity *entity) |
| 140 | { |
| 141 | return entity->type & MEDIA_ENT_TYPE_MASK; |
| 142 | } |
| 143 | |
| 144 | static inline u32 media_entity_subtype(struct media_entity *entity) |
| 145 | { |
| 146 | return entity->type & MEDIA_ENT_SUBTYPE_MASK; |
| 147 | } |
| 148 | |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 149 | static inline u32 media_entity_id(struct media_entity *entity) |
| 150 | { |
| 151 | return entity->id; |
| 152 | } |
| 153 | |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame^] | 154 | static inline enum media_gobj_type media_type(struct media_gobj *gobj) |
| 155 | { |
| 156 | return gobj->id >> MEDIA_BITS_PER_LOCAL_ID; |
| 157 | } |
| 158 | |
| 159 | static inline u32 media_localid(struct media_gobj *gobj) |
| 160 | { |
| 161 | return gobj->id & MEDIA_LOCAL_ID_MASK; |
| 162 | } |
| 163 | |
| 164 | static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id) |
| 165 | { |
| 166 | u32 id; |
| 167 | |
| 168 | id = type << MEDIA_BITS_PER_LOCAL_ID; |
| 169 | id |= local_id & MEDIA_LOCAL_ID_MASK; |
| 170 | |
| 171 | return id; |
| 172 | } |
| 173 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 174 | #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16 |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 175 | #define MEDIA_ENTITY_ENUM_MAX_ID 64 |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 176 | |
Mauro Carvalho Chehab | ef69ee1 | 2015-10-01 18:07:53 -0300 | [diff] [blame] | 177 | /* |
| 178 | * The number of pads can't be bigger than the number of entities, |
| 179 | * as the worse-case scenario is to have one entity linked up to |
| 180 | * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities. |
| 181 | */ |
| 182 | #define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1) |
| 183 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 184 | struct media_entity_graph { |
| 185 | struct { |
| 186 | struct media_entity *entity; |
| 187 | int link; |
| 188 | } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH]; |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 189 | |
| 190 | DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 191 | int top; |
| 192 | }; |
| 193 | |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame^] | 194 | #define gobj_to_entity(gobj) \ |
| 195 | container_of(gobj, struct media_entity, graph_obj) |
| 196 | |
| 197 | void media_gobj_init(struct media_device *mdev, |
| 198 | enum media_gobj_type type, |
| 199 | struct media_gobj *gobj); |
| 200 | void media_gobj_remove(struct media_gobj *gobj); |
| 201 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 202 | int media_entity_init(struct media_entity *entity, u16 num_pads, |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 203 | struct media_pad *pads); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 204 | void media_entity_cleanup(struct media_entity *entity); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 205 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 206 | int media_entity_create_link(struct media_entity *source, u16 source_pad, |
| 207 | struct media_entity *sink, u16 sink_pad, u32 flags); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 208 | void __media_entity_remove_links(struct media_entity *entity); |
| 209 | void media_entity_remove_links(struct media_entity *entity); |
| 210 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 211 | int __media_entity_setup_link(struct media_link *link, u32 flags); |
| 212 | int media_entity_setup_link(struct media_link *link, u32 flags); |
| 213 | struct media_link *media_entity_find_link(struct media_pad *source, |
| 214 | struct media_pad *sink); |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 215 | struct media_pad *media_entity_remote_pad(struct media_pad *pad); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 216 | |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 217 | struct media_entity *media_entity_get(struct media_entity *entity); |
| 218 | void media_entity_put(struct media_entity *entity); |
| 219 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 220 | void media_entity_graph_walk_start(struct media_entity_graph *graph, |
| 221 | struct media_entity *entity); |
| 222 | struct media_entity * |
| 223 | media_entity_graph_walk_next(struct media_entity_graph *graph); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 224 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
| 225 | struct media_pipeline *pipe); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 226 | void media_entity_pipeline_stop(struct media_entity *entity); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 227 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 228 | #define media_entity_call(entity, operation, args...) \ |
| 229 | (((entity)->ops && (entity)->ops->operation) ? \ |
| 230 | (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD) |
| 231 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 232 | #endif |