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