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 | #include <linux/module.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <media/media-entity.h> |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 26 | #include <media/media-device.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * media_entity_init - Initialize a media entity |
| 30 | * |
| 31 | * @num_pads: Total number of sink and source pads. |
| 32 | * @extra_links: Initial estimate of the number of extra links. |
| 33 | * @pads: Array of 'num_pads' pads. |
| 34 | * |
| 35 | * The total number of pads is an intrinsic property of entities known by the |
| 36 | * entity driver, while the total number of links depends on hardware design |
| 37 | * and is an extrinsic property unknown to the entity driver. However, in most |
| 38 | * use cases the entity driver can guess the number of links which can safely |
| 39 | * be assumed to be equal to or larger than the number of pads. |
| 40 | * |
| 41 | * For those reasons the links array can be preallocated based on the entity |
| 42 | * driver guess and will be reallocated later if extra links need to be |
| 43 | * created. |
| 44 | * |
| 45 | * This function allocates a links array with enough space to hold at least |
| 46 | * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will |
| 47 | * be set to the number of allocated elements. |
| 48 | * |
| 49 | * The pads array is managed by the entity driver and passed to |
| 50 | * media_entity_init() where its pointer will be stored in the entity structure. |
| 51 | */ |
| 52 | int |
| 53 | media_entity_init(struct media_entity *entity, u16 num_pads, |
| 54 | struct media_pad *pads, u16 extra_links) |
| 55 | { |
| 56 | struct media_link *links; |
| 57 | unsigned int max_links = num_pads + extra_links; |
| 58 | unsigned int i; |
| 59 | |
| 60 | links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL); |
| 61 | if (links == NULL) |
| 62 | return -ENOMEM; |
| 63 | |
| 64 | entity->group_id = 0; |
| 65 | entity->max_links = max_links; |
| 66 | entity->num_links = 0; |
| 67 | entity->num_backlinks = 0; |
| 68 | entity->num_pads = num_pads; |
| 69 | entity->pads = pads; |
| 70 | entity->links = links; |
| 71 | |
| 72 | for (i = 0; i < num_pads; i++) { |
| 73 | pads[i].entity = entity; |
| 74 | pads[i].index = i; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(media_entity_init); |
| 80 | |
| 81 | void |
| 82 | media_entity_cleanup(struct media_entity *entity) |
| 83 | { |
| 84 | kfree(entity->links); |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(media_entity_cleanup); |
| 87 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 88 | /* ----------------------------------------------------------------------------- |
| 89 | * Graph traversal |
| 90 | */ |
| 91 | |
| 92 | static struct media_entity * |
| 93 | media_entity_other(struct media_entity *entity, struct media_link *link) |
| 94 | { |
| 95 | if (link->source->entity == entity) |
| 96 | return link->sink->entity; |
| 97 | else |
| 98 | return link->source->entity; |
| 99 | } |
| 100 | |
| 101 | /* push an entity to traversal stack */ |
| 102 | static void stack_push(struct media_entity_graph *graph, |
| 103 | struct media_entity *entity) |
| 104 | { |
| 105 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { |
| 106 | WARN_ON(1); |
| 107 | return; |
| 108 | } |
| 109 | graph->top++; |
| 110 | graph->stack[graph->top].link = 0; |
| 111 | graph->stack[graph->top].entity = entity; |
| 112 | } |
| 113 | |
| 114 | static struct media_entity *stack_pop(struct media_entity_graph *graph) |
| 115 | { |
| 116 | struct media_entity *entity; |
| 117 | |
| 118 | entity = graph->stack[graph->top].entity; |
| 119 | graph->top--; |
| 120 | |
| 121 | return entity; |
| 122 | } |
| 123 | |
| 124 | #define stack_peek(en) ((en)->stack[(en)->top - 1].entity) |
| 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; |
| 143 | stack_push(graph, entity); |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_start); |
| 146 | |
| 147 | /** |
| 148 | * media_entity_graph_walk_next - Get the next entity in the graph |
| 149 | * @graph: Media graph structure |
| 150 | * |
| 151 | * Perform a depth-first traversal of the given media entities graph. |
| 152 | * |
| 153 | * The graph structure must have been previously initialized with a call to |
| 154 | * media_entity_graph_walk_start(). |
| 155 | * |
| 156 | * Return the next entity in the graph or NULL if the whole graph have been |
| 157 | * traversed. |
| 158 | */ |
| 159 | struct media_entity * |
| 160 | media_entity_graph_walk_next(struct media_entity_graph *graph) |
| 161 | { |
| 162 | if (stack_top(graph) == NULL) |
| 163 | return NULL; |
| 164 | |
| 165 | /* |
| 166 | * Depth first search. Push entity to stack and continue from |
| 167 | * top of the stack until no more entities on the level can be |
| 168 | * found. |
| 169 | */ |
| 170 | while (link_top(graph) < stack_top(graph)->num_links) { |
| 171 | struct media_entity *entity = stack_top(graph); |
| 172 | struct media_link *link = &entity->links[link_top(graph)]; |
| 173 | struct media_entity *next; |
| 174 | |
| 175 | /* The link is not enabled so we do not follow. */ |
| 176 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { |
| 177 | link_top(graph)++; |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | /* Get the entity in the other end of the link . */ |
| 182 | next = media_entity_other(entity, link); |
| 183 | |
| 184 | /* Was it the entity we came here from? */ |
| 185 | if (next == stack_peek(graph)) { |
| 186 | link_top(graph)++; |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | /* Push the new entity to stack and start over. */ |
| 191 | link_top(graph)++; |
| 192 | stack_push(graph, next); |
| 193 | } |
| 194 | |
| 195 | return stack_pop(graph); |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); |
| 198 | |
| 199 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 200 | * Module use count |
| 201 | */ |
| 202 | |
| 203 | /* |
| 204 | * media_entity_get - Get a reference to the parent module |
| 205 | * @entity: The entity |
| 206 | * |
| 207 | * Get a reference to the parent media device module. |
| 208 | * |
| 209 | * The function will return immediately if @entity is NULL. |
| 210 | * |
| 211 | * Return a pointer to the entity on success or NULL on failure. |
| 212 | */ |
| 213 | struct media_entity *media_entity_get(struct media_entity *entity) |
| 214 | { |
| 215 | if (entity == NULL) |
| 216 | return NULL; |
| 217 | |
| 218 | if (entity->parent->dev && |
| 219 | !try_module_get(entity->parent->dev->driver->owner)) |
| 220 | return NULL; |
| 221 | |
| 222 | return entity; |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(media_entity_get); |
| 225 | |
| 226 | /* |
| 227 | * media_entity_put - Release the reference to the parent module |
| 228 | * @entity: The entity |
| 229 | * |
| 230 | * Release the reference count acquired by media_entity_get(). |
| 231 | * |
| 232 | * The function will return immediately if @entity is NULL. |
| 233 | */ |
| 234 | void media_entity_put(struct media_entity *entity) |
| 235 | { |
| 236 | if (entity == NULL) |
| 237 | return; |
| 238 | |
| 239 | if (entity->parent->dev) |
| 240 | module_put(entity->parent->dev->driver->owner); |
| 241 | } |
| 242 | EXPORT_SYMBOL_GPL(media_entity_put); |
| 243 | |
| 244 | /* ----------------------------------------------------------------------------- |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 245 | * Links management |
| 246 | */ |
| 247 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 248 | static struct media_link *media_entity_add_link(struct media_entity *entity) |
| 249 | { |
| 250 | if (entity->num_links >= entity->max_links) { |
| 251 | struct media_link *links = entity->links; |
| 252 | unsigned int max_links = entity->max_links + 2; |
| 253 | unsigned int i; |
| 254 | |
| 255 | links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL); |
| 256 | if (links == NULL) |
| 257 | return NULL; |
| 258 | |
| 259 | for (i = 0; i < entity->num_links; i++) |
| 260 | links[i].reverse->reverse = &links[i]; |
| 261 | |
| 262 | entity->max_links = max_links; |
| 263 | entity->links = links; |
| 264 | } |
| 265 | |
| 266 | return &entity->links[entity->num_links++]; |
| 267 | } |
| 268 | |
| 269 | int |
| 270 | media_entity_create_link(struct media_entity *source, u16 source_pad, |
| 271 | struct media_entity *sink, u16 sink_pad, u32 flags) |
| 272 | { |
| 273 | struct media_link *link; |
| 274 | struct media_link *backlink; |
| 275 | |
| 276 | BUG_ON(source == NULL || sink == NULL); |
| 277 | BUG_ON(source_pad >= source->num_pads); |
| 278 | BUG_ON(sink_pad >= sink->num_pads); |
| 279 | |
| 280 | link = media_entity_add_link(source); |
| 281 | if (link == NULL) |
| 282 | return -ENOMEM; |
| 283 | |
| 284 | link->source = &source->pads[source_pad]; |
| 285 | link->sink = &sink->pads[sink_pad]; |
| 286 | link->flags = flags; |
| 287 | |
| 288 | /* Create the backlink. Backlinks are used to help graph traversal and |
| 289 | * are not reported to userspace. |
| 290 | */ |
| 291 | backlink = media_entity_add_link(sink); |
| 292 | if (backlink == NULL) { |
| 293 | source->num_links--; |
| 294 | return -ENOMEM; |
| 295 | } |
| 296 | |
| 297 | backlink->source = &source->pads[source_pad]; |
| 298 | backlink->sink = &sink->pads[sink_pad]; |
| 299 | backlink->flags = flags; |
| 300 | |
| 301 | link->reverse = backlink; |
| 302 | backlink->reverse = link; |
| 303 | |
| 304 | sink->num_backlinks++; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(media_entity_create_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame^] | 309 | |
| 310 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
| 311 | { |
| 312 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
| 313 | int ret; |
| 314 | |
| 315 | /* Notify both entities. */ |
| 316 | ret = media_entity_call(link->source->entity, link_setup, |
| 317 | link->source, link->sink, flags); |
| 318 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 319 | return ret; |
| 320 | |
| 321 | ret = media_entity_call(link->sink->entity, link_setup, |
| 322 | link->sink, link->source, flags); |
| 323 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 324 | media_entity_call(link->source->entity, link_setup, |
| 325 | link->source, link->sink, link->flags); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | link->flags = (link->flags & ~mask) | (flags & mask); |
| 330 | link->reverse->flags = link->flags; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * __media_entity_setup_link - Configure a media link |
| 337 | * @link: The link being configured |
| 338 | * @flags: Link configuration flags |
| 339 | * |
| 340 | * The bulk of link setup is handled by the two entities connected through the |
| 341 | * link. This function notifies both entities of the link configuration change. |
| 342 | * |
| 343 | * If the link is immutable or if the current and new configuration are |
| 344 | * identical, return immediately. |
| 345 | * |
| 346 | * The user is expected to hold link->source->parent->mutex. If not, |
| 347 | * media_entity_setup_link() should be used instead. |
| 348 | */ |
| 349 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
| 350 | { |
| 351 | struct media_device *mdev; |
| 352 | struct media_entity *source, *sink; |
| 353 | int ret = -EBUSY; |
| 354 | |
| 355 | if (link == NULL) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
| 359 | return link->flags == flags ? 0 : -EINVAL; |
| 360 | |
| 361 | if (link->flags == flags) |
| 362 | return 0; |
| 363 | |
| 364 | source = link->source->entity; |
| 365 | sink = link->sink->entity; |
| 366 | |
| 367 | mdev = source->parent; |
| 368 | |
| 369 | if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) { |
| 370 | ret = mdev->link_notify(link->source, link->sink, |
| 371 | MEDIA_LNK_FL_ENABLED); |
| 372 | if (ret < 0) |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | ret = __media_entity_setup_link_notify(link, flags); |
| 377 | if (ret < 0) |
| 378 | goto err; |
| 379 | |
| 380 | if (!(flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) |
| 381 | mdev->link_notify(link->source, link->sink, 0); |
| 382 | |
| 383 | return 0; |
| 384 | |
| 385 | err: |
| 386 | if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) |
| 387 | mdev->link_notify(link->source, link->sink, 0); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | int media_entity_setup_link(struct media_link *link, u32 flags) |
| 393 | { |
| 394 | int ret; |
| 395 | |
| 396 | mutex_lock(&link->source->entity->parent->graph_mutex); |
| 397 | ret = __media_entity_setup_link(link, flags); |
| 398 | mutex_unlock(&link->source->entity->parent->graph_mutex); |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | EXPORT_SYMBOL_GPL(media_entity_setup_link); |
| 403 | |
| 404 | /** |
| 405 | * media_entity_find_link - Find a link between two pads |
| 406 | * @source: Source pad |
| 407 | * @sink: Sink pad |
| 408 | * |
| 409 | * Return a pointer to the link between the two entities. If no such link |
| 410 | * exists, return NULL. |
| 411 | */ |
| 412 | struct media_link * |
| 413 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) |
| 414 | { |
| 415 | struct media_link *link; |
| 416 | unsigned int i; |
| 417 | |
| 418 | for (i = 0; i < source->entity->num_links; ++i) { |
| 419 | link = &source->entity->links[i]; |
| 420 | |
| 421 | if (link->source->entity == source->entity && |
| 422 | link->source->index == source->index && |
| 423 | link->sink->entity == sink->entity && |
| 424 | link->sink->index == sink->index) |
| 425 | return link; |
| 426 | } |
| 427 | |
| 428 | return NULL; |
| 429 | } |
| 430 | EXPORT_SYMBOL_GPL(media_entity_find_link); |
| 431 | |
| 432 | /** |
| 433 | * media_entity_remote_source - Find the source pad at the remote end of a link |
| 434 | * @pad: Sink pad at the local end of the link |
| 435 | * |
| 436 | * Search for a remote source pad connected to the given sink pad by iterating |
| 437 | * over all links originating or terminating at that pad until an enabled link |
| 438 | * is found. |
| 439 | * |
| 440 | * Return a pointer to the pad at the remote end of the first found enabled |
| 441 | * link, or NULL if no enabled link has been found. |
| 442 | */ |
| 443 | struct media_pad *media_entity_remote_source(struct media_pad *pad) |
| 444 | { |
| 445 | unsigned int i; |
| 446 | |
| 447 | for (i = 0; i < pad->entity->num_links; i++) { |
| 448 | struct media_link *link = &pad->entity->links[i]; |
| 449 | |
| 450 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 451 | continue; |
| 452 | |
| 453 | if (link->source == pad) |
| 454 | return link->sink; |
| 455 | |
| 456 | if (link->sink == pad) |
| 457 | return link->source; |
| 458 | } |
| 459 | |
| 460 | return NULL; |
| 461 | |
| 462 | } |
| 463 | EXPORT_SYMBOL_GPL(media_entity_remote_source); |