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