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 | |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 23 | #include <linux/bitmap.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <media/media-entity.h> |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 27 | #include <media/media-device.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 28 | |
| 29 | /** |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 30 | * dev_dbg_obj - Prints in debug mode a change on some object |
| 31 | * |
| 32 | * @event_name: Name of the event to report. Could be __func__ |
| 33 | * @gobj: Pointer to the object |
| 34 | * |
| 35 | * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it |
| 36 | * won't produce any code. |
| 37 | */ |
| 38 | static inline const char *gobj_type(enum media_gobj_type type) |
| 39 | { |
| 40 | switch (type) { |
| 41 | case MEDIA_GRAPH_ENTITY: |
| 42 | return "entity"; |
| 43 | case MEDIA_GRAPH_PAD: |
| 44 | return "pad"; |
| 45 | case MEDIA_GRAPH_LINK: |
| 46 | return "link"; |
| 47 | default: |
| 48 | return "unknown"; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) |
| 53 | { |
| 54 | #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG) |
| 55 | switch (media_type(gobj)) { |
| 56 | case MEDIA_GRAPH_ENTITY: |
| 57 | dev_dbg(gobj->mdev->dev, |
| 58 | "%s: id 0x%08x entity#%d: '%s'\n", |
| 59 | event_name, gobj->id, media_localid(gobj), |
| 60 | gobj_to_entity(gobj)->name); |
| 61 | break; |
| 62 | case MEDIA_GRAPH_LINK: |
| 63 | { |
| 64 | struct media_link *link = gobj_to_link(gobj); |
| 65 | |
| 66 | dev_dbg(gobj->mdev->dev, |
| 67 | "%s: id 0x%08x link#%d: '%s' %s#%d ==> '%s' %s#%d\n", |
| 68 | event_name, gobj->id, media_localid(gobj), |
| 69 | |
| 70 | link->source->entity->name, |
| 71 | gobj_type(media_type(&link->source->graph_obj)), |
| 72 | media_localid(&link->source->graph_obj), |
| 73 | |
| 74 | link->sink->entity->name, |
| 75 | gobj_type(media_type(&link->sink->graph_obj)), |
| 76 | media_localid(&link->sink->graph_obj)); |
| 77 | break; |
| 78 | } |
| 79 | case MEDIA_GRAPH_PAD: |
| 80 | { |
| 81 | struct media_pad *pad = gobj_to_pad(gobj); |
| 82 | |
| 83 | dev_dbg(gobj->mdev->dev, |
| 84 | "%s: id 0x%08x pad#%d: '%s':%d\n", |
| 85 | event_name, gobj->id, media_localid(gobj), |
| 86 | pad->entity->name, pad->index); |
| 87 | } |
| 88 | } |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | /** |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 93 | * media_gobj_init - Initialize a graph object |
| 94 | * |
| 95 | * @mdev: Pointer to the media_device that contains the object |
| 96 | * @type: Type of the object |
| 97 | * @gobj: Pointer to the object |
| 98 | * |
| 99 | * This routine initializes the embedded struct media_gobj inside a |
| 100 | * media graph object. It is called automatically if media_*_create() |
| 101 | * calls are used. However, if the object (entity, link, pad, interface) |
| 102 | * is embedded on some other object, this function should be called before |
| 103 | * registering the object at the media controller. |
| 104 | */ |
| 105 | void media_gobj_init(struct media_device *mdev, |
| 106 | enum media_gobj_type type, |
| 107 | struct media_gobj *gobj) |
| 108 | { |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 109 | gobj->mdev = mdev; |
| 110 | |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 111 | /* Create a per-type unique object ID */ |
| 112 | switch (type) { |
| 113 | case MEDIA_GRAPH_ENTITY: |
| 114 | gobj->id = media_gobj_gen_id(type, ++mdev->entity_id); |
| 115 | break; |
Mauro Carvalho Chehab | 18710dc | 2015-08-14 12:50:08 -0300 | [diff] [blame] | 116 | case MEDIA_GRAPH_PAD: |
| 117 | gobj->id = media_gobj_gen_id(type, ++mdev->pad_id); |
| 118 | break; |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 119 | case MEDIA_GRAPH_LINK: |
| 120 | gobj->id = media_gobj_gen_id(type, ++mdev->link_id); |
| 121 | break; |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 122 | } |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 123 | dev_dbg_obj(__func__, gobj); |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * media_gobj_remove - Stop using a graph object on a media device |
| 128 | * |
| 129 | * @graph_obj: Pointer to the object |
| 130 | * |
| 131 | * This should be called at media_device_unregister_*() routines |
| 132 | */ |
| 133 | void media_gobj_remove(struct media_gobj *gobj) |
| 134 | { |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 135 | dev_dbg_obj(__func__, gobj); |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 139 | * media_entity_init - Initialize a media entity |
| 140 | * |
| 141 | * @num_pads: Total number of sink and source pads. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 142 | * @pads: Array of 'num_pads' pads. |
| 143 | * |
| 144 | * The total number of pads is an intrinsic property of entities known by the |
| 145 | * entity driver, while the total number of links depends on hardware design |
| 146 | * and is an extrinsic property unknown to the entity driver. However, in most |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 147 | * use cases the number of links can safely be assumed to be equal to or |
| 148 | * larger than the number of pads. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 149 | * |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 150 | * For those reasons the links array can be preallocated based on the number |
| 151 | * of pads and will be reallocated later if extra links need to be created. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 152 | * |
| 153 | * This function allocates a links array with enough space to hold at least |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 154 | * 'num_pads' elements. The media_entity::max_links field will be set to the |
| 155 | * number of allocated elements. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 156 | * |
| 157 | * The pads array is managed by the entity driver and passed to |
| 158 | * media_entity_init() where its pointer will be stored in the entity structure. |
| 159 | */ |
| 160 | int |
| 161 | media_entity_init(struct media_entity *entity, u16 num_pads, |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 162 | struct media_pad *pads) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 163 | { |
| 164 | struct media_link *links; |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 165 | unsigned int max_links = num_pads; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 166 | unsigned int i; |
| 167 | |
| 168 | links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL); |
| 169 | if (links == NULL) |
| 170 | return -ENOMEM; |
| 171 | |
| 172 | entity->group_id = 0; |
| 173 | entity->max_links = max_links; |
| 174 | entity->num_links = 0; |
| 175 | entity->num_backlinks = 0; |
| 176 | entity->num_pads = num_pads; |
| 177 | entity->pads = pads; |
| 178 | entity->links = links; |
| 179 | |
| 180 | for (i = 0; i < num_pads; i++) { |
| 181 | pads[i].entity = entity; |
| 182 | pads[i].index = i; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(media_entity_init); |
| 188 | |
| 189 | void |
| 190 | media_entity_cleanup(struct media_entity *entity) |
| 191 | { |
| 192 | kfree(entity->links); |
| 193 | } |
| 194 | EXPORT_SYMBOL_GPL(media_entity_cleanup); |
| 195 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 196 | /* ----------------------------------------------------------------------------- |
| 197 | * Graph traversal |
| 198 | */ |
| 199 | |
| 200 | static struct media_entity * |
| 201 | media_entity_other(struct media_entity *entity, struct media_link *link) |
| 202 | { |
| 203 | if (link->source->entity == entity) |
| 204 | return link->sink->entity; |
| 205 | else |
| 206 | return link->source->entity; |
| 207 | } |
| 208 | |
| 209 | /* push an entity to traversal stack */ |
| 210 | static void stack_push(struct media_entity_graph *graph, |
| 211 | struct media_entity *entity) |
| 212 | { |
| 213 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { |
| 214 | WARN_ON(1); |
| 215 | return; |
| 216 | } |
| 217 | graph->top++; |
| 218 | graph->stack[graph->top].link = 0; |
| 219 | graph->stack[graph->top].entity = entity; |
| 220 | } |
| 221 | |
| 222 | static struct media_entity *stack_pop(struct media_entity_graph *graph) |
| 223 | { |
| 224 | struct media_entity *entity; |
| 225 | |
| 226 | entity = graph->stack[graph->top].entity; |
| 227 | graph->top--; |
| 228 | |
| 229 | return entity; |
| 230 | } |
| 231 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 232 | #define link_top(en) ((en)->stack[(en)->top].link) |
| 233 | #define stack_top(en) ((en)->stack[(en)->top].entity) |
| 234 | |
| 235 | /** |
| 236 | * media_entity_graph_walk_start - Start walking the media graph at a given entity |
| 237 | * @graph: Media graph structure that will be used to walk the graph |
| 238 | * @entity: Starting entity |
| 239 | * |
| 240 | * This function initializes the graph traversal structure to walk the entities |
| 241 | * graph starting at the given entity. The traversal structure must not be |
| 242 | * modified by the caller during graph traversal. When done the structure can |
| 243 | * safely be freed. |
| 244 | */ |
| 245 | void media_entity_graph_walk_start(struct media_entity_graph *graph, |
| 246 | struct media_entity *entity) |
| 247 | { |
| 248 | graph->top = 0; |
| 249 | graph->stack[graph->top].entity = NULL; |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 250 | bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID); |
| 251 | |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 252 | if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 253 | return; |
| 254 | |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 255 | __set_bit(media_entity_id(entity), graph->entities); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 256 | stack_push(graph, entity); |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_start); |
| 259 | |
| 260 | /** |
| 261 | * media_entity_graph_walk_next - Get the next entity in the graph |
| 262 | * @graph: Media graph structure |
| 263 | * |
| 264 | * Perform a depth-first traversal of the given media entities graph. |
| 265 | * |
| 266 | * The graph structure must have been previously initialized with a call to |
| 267 | * media_entity_graph_walk_start(). |
| 268 | * |
| 269 | * Return the next entity in the graph or NULL if the whole graph have been |
| 270 | * traversed. |
| 271 | */ |
| 272 | struct media_entity * |
| 273 | media_entity_graph_walk_next(struct media_entity_graph *graph) |
| 274 | { |
| 275 | if (stack_top(graph) == NULL) |
| 276 | return NULL; |
| 277 | |
| 278 | /* |
| 279 | * Depth first search. Push entity to stack and continue from |
| 280 | * top of the stack until no more entities on the level can be |
| 281 | * found. |
| 282 | */ |
| 283 | while (link_top(graph) < stack_top(graph)->num_links) { |
| 284 | struct media_entity *entity = stack_top(graph); |
| 285 | struct media_link *link = &entity->links[link_top(graph)]; |
| 286 | struct media_entity *next; |
| 287 | |
| 288 | /* The link is not enabled so we do not follow. */ |
| 289 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { |
| 290 | link_top(graph)++; |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | /* Get the entity in the other end of the link . */ |
| 295 | next = media_entity_other(entity, link); |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 296 | if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 297 | return NULL; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 298 | |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 299 | /* Has the entity already been visited? */ |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 300 | if (__test_and_set_bit(media_entity_id(next), graph->entities)) { |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 301 | link_top(graph)++; |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | /* Push the new entity to stack and start over. */ |
| 306 | link_top(graph)++; |
| 307 | stack_push(graph, next); |
| 308 | } |
| 309 | |
| 310 | return stack_pop(graph); |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); |
| 313 | |
| 314 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 315 | * Pipeline management |
| 316 | */ |
| 317 | |
| 318 | /** |
| 319 | * media_entity_pipeline_start - Mark a pipeline as streaming |
| 320 | * @entity: Starting entity |
| 321 | * @pipe: Media pipeline to be assigned to all entities in the pipeline. |
| 322 | * |
| 323 | * Mark all entities connected to a given entity through enabled links, either |
| 324 | * directly or indirectly, as streaming. The given pipeline object is assigned to |
| 325 | * every entity in the pipeline and stored in the media_entity pipe field. |
| 326 | * |
| 327 | * Calls to this function can be nested, in which case the same number of |
| 328 | * media_entity_pipeline_stop() calls will be required to stop streaming. The |
| 329 | * pipeline pointer must be identical for all nested calls to |
| 330 | * media_entity_pipeline_start(). |
| 331 | */ |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 332 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
| 333 | struct media_pipeline *pipe) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 334 | { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 335 | struct media_device *mdev = entity->graph_obj.mdev; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 336 | struct media_entity_graph graph; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 337 | struct media_entity *entity_err = entity; |
| 338 | int ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 339 | |
| 340 | mutex_lock(&mdev->graph_mutex); |
| 341 | |
| 342 | media_entity_graph_walk_start(&graph, entity); |
| 343 | |
| 344 | while ((entity = media_entity_graph_walk_next(&graph))) { |
Mauro Carvalho Chehab | ef69ee1 | 2015-10-01 18:07:53 -0300 | [diff] [blame] | 345 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
| 346 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 347 | unsigned int i; |
| 348 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 349 | entity->stream_count++; |
| 350 | WARN_ON(entity->pipe && entity->pipe != pipe); |
| 351 | entity->pipe = pipe; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 352 | |
| 353 | /* Already streaming --- no need to check. */ |
| 354 | if (entity->stream_count > 1) |
| 355 | continue; |
| 356 | |
| 357 | if (!entity->ops || !entity->ops->link_validate) |
| 358 | continue; |
| 359 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 360 | bitmap_zero(active, entity->num_pads); |
| 361 | bitmap_fill(has_no_links, entity->num_pads); |
| 362 | |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 363 | for (i = 0; i < entity->num_links; i++) { |
| 364 | struct media_link *link = &entity->links[i]; |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 365 | struct media_pad *pad = link->sink->entity == entity |
| 366 | ? link->sink : link->source; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 367 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 368 | /* Mark that a pad is connected by a link. */ |
| 369 | bitmap_clear(has_no_links, pad->index, 1); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 370 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 371 | /* |
| 372 | * Pads that either do not need to connect or |
| 373 | * are connected through an enabled link are |
| 374 | * fine. |
| 375 | */ |
| 376 | if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || |
| 377 | link->flags & MEDIA_LNK_FL_ENABLED) |
| 378 | bitmap_set(active, pad->index, 1); |
| 379 | |
| 380 | /* |
| 381 | * Link validation will only take place for |
| 382 | * sink ends of the link that are enabled. |
| 383 | */ |
| 384 | if (link->sink != pad || |
| 385 | !(link->flags & MEDIA_LNK_FL_ENABLED)) |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 386 | continue; |
| 387 | |
| 388 | ret = entity->ops->link_validate(link); |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 389 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 390 | dev_dbg(entity->graph_obj.mdev->dev, |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 391 | "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n", |
Sakari Ailus | 823ea2a | 2015-02-12 11:43:11 -0200 | [diff] [blame] | 392 | link->source->entity->name, |
| 393 | link->source->index, |
| 394 | entity->name, link->sink->index, ret); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 395 | goto error; |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 396 | } |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 397 | } |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 398 | |
| 399 | /* Either no links or validated links are fine. */ |
| 400 | bitmap_or(active, active, has_no_links, entity->num_pads); |
| 401 | |
| 402 | if (!bitmap_full(active, entity->num_pads)) { |
| 403 | ret = -EPIPE; |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 404 | dev_dbg(entity->graph_obj.mdev->dev, |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 405 | "\"%s\":%u must be connected by an enabled link\n", |
| 406 | entity->name, |
Sakari Ailus | 094f1ca | 2014-11-03 17:55:51 -0300 | [diff] [blame] | 407 | (unsigned)find_first_zero_bit( |
| 408 | active, entity->num_pads)); |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 409 | goto error; |
| 410 | } |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | mutex_unlock(&mdev->graph_mutex); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | |
| 417 | error: |
| 418 | /* |
| 419 | * Link validation on graph failed. We revert what we did and |
| 420 | * return the error. |
| 421 | */ |
| 422 | media_entity_graph_walk_start(&graph, entity_err); |
| 423 | |
| 424 | while ((entity_err = media_entity_graph_walk_next(&graph))) { |
| 425 | entity_err->stream_count--; |
| 426 | if (entity_err->stream_count == 0) |
| 427 | entity_err->pipe = NULL; |
| 428 | |
| 429 | /* |
| 430 | * We haven't increased stream_count further than this |
| 431 | * so we quit here. |
| 432 | */ |
| 433 | if (entity_err == entity) |
| 434 | break; |
| 435 | } |
| 436 | |
| 437 | mutex_unlock(&mdev->graph_mutex); |
| 438 | |
| 439 | return ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 440 | } |
| 441 | EXPORT_SYMBOL_GPL(media_entity_pipeline_start); |
| 442 | |
| 443 | /** |
| 444 | * media_entity_pipeline_stop - Mark a pipeline as not streaming |
| 445 | * @entity: Starting entity |
| 446 | * |
| 447 | * Mark all entities connected to a given entity through enabled links, either |
| 448 | * directly or indirectly, as not streaming. The media_entity pipe field is |
| 449 | * reset to NULL. |
| 450 | * |
| 451 | * If multiple calls to media_entity_pipeline_start() have been made, the same |
| 452 | * number of calls to this function are required to mark the pipeline as not |
| 453 | * streaming. |
| 454 | */ |
| 455 | void media_entity_pipeline_stop(struct media_entity *entity) |
| 456 | { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 457 | struct media_device *mdev = entity->graph_obj.mdev; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 458 | struct media_entity_graph graph; |
| 459 | |
| 460 | mutex_lock(&mdev->graph_mutex); |
| 461 | |
| 462 | media_entity_graph_walk_start(&graph, entity); |
| 463 | |
| 464 | while ((entity = media_entity_graph_walk_next(&graph))) { |
| 465 | entity->stream_count--; |
| 466 | if (entity->stream_count == 0) |
| 467 | entity->pipe = NULL; |
| 468 | } |
| 469 | |
| 470 | mutex_unlock(&mdev->graph_mutex); |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(media_entity_pipeline_stop); |
| 473 | |
| 474 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 475 | * Module use count |
| 476 | */ |
| 477 | |
| 478 | /* |
| 479 | * media_entity_get - Get a reference to the parent module |
| 480 | * @entity: The entity |
| 481 | * |
| 482 | * Get a reference to the parent media device module. |
| 483 | * |
| 484 | * The function will return immediately if @entity is NULL. |
| 485 | * |
| 486 | * Return a pointer to the entity on success or NULL on failure. |
| 487 | */ |
| 488 | struct media_entity *media_entity_get(struct media_entity *entity) |
| 489 | { |
| 490 | if (entity == NULL) |
| 491 | return NULL; |
| 492 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 493 | if (entity->graph_obj.mdev->dev && |
| 494 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 495 | return NULL; |
| 496 | |
| 497 | return entity; |
| 498 | } |
| 499 | EXPORT_SYMBOL_GPL(media_entity_get); |
| 500 | |
| 501 | /* |
| 502 | * media_entity_put - Release the reference to the parent module |
| 503 | * @entity: The entity |
| 504 | * |
| 505 | * Release the reference count acquired by media_entity_get(). |
| 506 | * |
| 507 | * The function will return immediately if @entity is NULL. |
| 508 | */ |
| 509 | void media_entity_put(struct media_entity *entity) |
| 510 | { |
| 511 | if (entity == NULL) |
| 512 | return; |
| 513 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 514 | if (entity->graph_obj.mdev->dev) |
| 515 | module_put(entity->graph_obj.mdev->dev->driver->owner); |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 516 | } |
| 517 | EXPORT_SYMBOL_GPL(media_entity_put); |
| 518 | |
| 519 | /* ----------------------------------------------------------------------------- |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 520 | * Links management |
| 521 | */ |
| 522 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 523 | static struct media_link *media_entity_add_link(struct media_entity *entity) |
| 524 | { |
| 525 | if (entity->num_links >= entity->max_links) { |
| 526 | struct media_link *links = entity->links; |
| 527 | unsigned int max_links = entity->max_links + 2; |
| 528 | unsigned int i; |
| 529 | |
| 530 | links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL); |
| 531 | if (links == NULL) |
| 532 | return NULL; |
| 533 | |
| 534 | for (i = 0; i < entity->num_links; i++) |
| 535 | links[i].reverse->reverse = &links[i]; |
| 536 | |
| 537 | entity->max_links = max_links; |
| 538 | entity->links = links; |
| 539 | } |
| 540 | |
| 541 | return &entity->links[entity->num_links++]; |
| 542 | } |
| 543 | |
| 544 | int |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 545 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 546 | struct media_entity *sink, u16 sink_pad, u32 flags) |
| 547 | { |
| 548 | struct media_link *link; |
| 549 | struct media_link *backlink; |
| 550 | |
| 551 | BUG_ON(source == NULL || sink == NULL); |
| 552 | BUG_ON(source_pad >= source->num_pads); |
| 553 | BUG_ON(sink_pad >= sink->num_pads); |
| 554 | |
| 555 | link = media_entity_add_link(source); |
| 556 | if (link == NULL) |
| 557 | return -ENOMEM; |
| 558 | |
| 559 | link->source = &source->pads[source_pad]; |
| 560 | link->sink = &sink->pads[sink_pad]; |
| 561 | link->flags = flags; |
| 562 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 563 | /* Initialize graph object embedded at the new link */ |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 564 | media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 565 | &link->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 566 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 567 | /* Create the backlink. Backlinks are used to help graph traversal and |
| 568 | * are not reported to userspace. |
| 569 | */ |
| 570 | backlink = media_entity_add_link(sink); |
| 571 | if (backlink == NULL) { |
| 572 | source->num_links--; |
| 573 | return -ENOMEM; |
| 574 | } |
| 575 | |
| 576 | backlink->source = &source->pads[source_pad]; |
| 577 | backlink->sink = &sink->pads[sink_pad]; |
| 578 | backlink->flags = flags; |
| 579 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 580 | /* Initialize graph object embedded at the new link */ |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 581 | media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 582 | &backlink->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 583 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 584 | link->reverse = backlink; |
| 585 | backlink->reverse = link; |
| 586 | |
| 587 | sink->num_backlinks++; |
| 588 | |
| 589 | return 0; |
| 590 | } |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 591 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 592 | |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 593 | void __media_entity_remove_links(struct media_entity *entity) |
| 594 | { |
| 595 | unsigned int i; |
| 596 | |
| 597 | for (i = 0; i < entity->num_links; i++) { |
| 598 | struct media_link *link = &entity->links[i]; |
| 599 | struct media_entity *remote; |
| 600 | unsigned int r = 0; |
| 601 | |
| 602 | if (link->source->entity == entity) |
| 603 | remote = link->sink->entity; |
| 604 | else |
| 605 | remote = link->source->entity; |
| 606 | |
| 607 | while (r < remote->num_links) { |
| 608 | struct media_link *rlink = &remote->links[r]; |
| 609 | |
| 610 | if (rlink != link->reverse) { |
| 611 | r++; |
| 612 | continue; |
| 613 | } |
| 614 | |
| 615 | if (link->source->entity == entity) |
| 616 | remote->num_backlinks--; |
| 617 | |
| 618 | if (--remote->num_links == 0) |
| 619 | break; |
| 620 | |
| 621 | /* Insert last entry in place of the dropped link. */ |
| 622 | *rlink = remote->links[remote->num_links]; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | entity->num_links = 0; |
| 627 | entity->num_backlinks = 0; |
| 628 | } |
| 629 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); |
| 630 | |
| 631 | void media_entity_remove_links(struct media_entity *entity) |
| 632 | { |
| 633 | /* Do nothing if the entity is not registered. */ |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 634 | if (entity->graph_obj.mdev == NULL) |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 635 | return; |
| 636 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 637 | mutex_lock(&entity->graph_obj.mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 638 | __media_entity_remove_links(entity); |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 639 | mutex_unlock(&entity->graph_obj.mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 640 | } |
| 641 | EXPORT_SYMBOL_GPL(media_entity_remove_links); |
| 642 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 643 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
| 644 | { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 645 | int ret; |
| 646 | |
| 647 | /* Notify both entities. */ |
| 648 | ret = media_entity_call(link->source->entity, link_setup, |
| 649 | link->source, link->sink, flags); |
| 650 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 651 | return ret; |
| 652 | |
| 653 | ret = media_entity_call(link->sink->entity, link_setup, |
| 654 | link->sink, link->source, flags); |
| 655 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 656 | media_entity_call(link->source->entity, link_setup, |
| 657 | link->source, link->sink, link->flags); |
| 658 | return ret; |
| 659 | } |
| 660 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 661 | link->flags = flags; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 662 | link->reverse->flags = link->flags; |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * __media_entity_setup_link - Configure a media link |
| 669 | * @link: The link being configured |
| 670 | * @flags: Link configuration flags |
| 671 | * |
| 672 | * The bulk of link setup is handled by the two entities connected through the |
| 673 | * link. This function notifies both entities of the link configuration change. |
| 674 | * |
| 675 | * If the link is immutable or if the current and new configuration are |
| 676 | * identical, return immediately. |
| 677 | * |
| 678 | * The user is expected to hold link->source->parent->mutex. If not, |
| 679 | * media_entity_setup_link() should be used instead. |
| 680 | */ |
| 681 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
| 682 | { |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 683 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 684 | struct media_device *mdev; |
| 685 | struct media_entity *source, *sink; |
| 686 | int ret = -EBUSY; |
| 687 | |
| 688 | if (link == NULL) |
| 689 | return -EINVAL; |
| 690 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 691 | /* The non-modifiable link flags must not be modified. */ |
| 692 | if ((link->flags & ~mask) != (flags & ~mask)) |
| 693 | return -EINVAL; |
| 694 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 695 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
| 696 | return link->flags == flags ? 0 : -EINVAL; |
| 697 | |
| 698 | if (link->flags == flags) |
| 699 | return 0; |
| 700 | |
| 701 | source = link->source->entity; |
| 702 | sink = link->sink->entity; |
| 703 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 704 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
| 705 | (source->stream_count || sink->stream_count)) |
| 706 | return -EBUSY; |
| 707 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 708 | mdev = source->graph_obj.mdev; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 709 | |
Sylwester Nawrocki | 813f5c0 | 2013-05-31 10:37:26 -0300 | [diff] [blame] | 710 | if (mdev->link_notify) { |
| 711 | ret = mdev->link_notify(link, flags, |
| 712 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 713 | if (ret < 0) |
| 714 | return ret; |
| 715 | } |
| 716 | |
| 717 | ret = __media_entity_setup_link_notify(link, flags); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 718 | |
Sylwester Nawrocki | 813f5c0 | 2013-05-31 10:37:26 -0300 | [diff] [blame] | 719 | if (mdev->link_notify) |
| 720 | mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 721 | |
| 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | int media_entity_setup_link(struct media_link *link, u32 flags) |
| 726 | { |
| 727 | int ret; |
| 728 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 729 | mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 730 | ret = __media_entity_setup_link(link, flags); |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame^] | 731 | mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 732 | |
| 733 | return ret; |
| 734 | } |
| 735 | EXPORT_SYMBOL_GPL(media_entity_setup_link); |
| 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 | */ |
| 745 | struct media_link * |
| 746 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) |
| 747 | { |
| 748 | struct media_link *link; |
| 749 | unsigned int i; |
| 750 | |
| 751 | for (i = 0; i < source->entity->num_links; ++i) { |
| 752 | link = &source->entity->links[i]; |
| 753 | |
| 754 | if (link->source->entity == source->entity && |
| 755 | link->source->index == source->index && |
| 756 | link->sink->entity == sink->entity && |
| 757 | link->sink->index == sink->index) |
| 758 | return link; |
| 759 | } |
| 760 | |
| 761 | return NULL; |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(media_entity_find_link); |
| 764 | |
| 765 | /** |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 766 | * media_entity_remote_pad - Find the pad at the remote end of a link |
| 767 | * @pad: Pad at the local end of the link |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 768 | * |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 769 | * Search for a remote pad connected to the given pad by iterating over all |
| 770 | * links originating or terminating at that pad until an enabled link is found. |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 771 | * |
| 772 | * Return a pointer to the pad at the remote end of the first found enabled |
| 773 | * link, or NULL if no enabled link has been found. |
| 774 | */ |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 775 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 776 | { |
| 777 | unsigned int i; |
| 778 | |
| 779 | for (i = 0; i < pad->entity->num_links; i++) { |
| 780 | struct media_link *link = &pad->entity->links[i]; |
| 781 | |
| 782 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 783 | continue; |
| 784 | |
| 785 | if (link->source == pad) |
| 786 | return link->sink; |
| 787 | |
| 788 | if (link->sink == pad) |
| 789 | return link->source; |
| 790 | } |
| 791 | |
| 792 | return NULL; |
| 793 | |
| 794 | } |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 795 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |