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