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 | 8f6d368 | 2015-08-19 20:18:35 -0300 | [diff] [blame] | 152 | BUG_ON(!mdev); |
| 153 | |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 154 | gobj->mdev = mdev; |
| 155 | |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 156 | /* Create a per-type unique object ID */ |
| 157 | switch (type) { |
| 158 | case MEDIA_GRAPH_ENTITY: |
| 159 | gobj->id = media_gobj_gen_id(type, ++mdev->entity_id); |
| 160 | break; |
Mauro Carvalho Chehab | 18710dc | 2015-08-14 12:50:08 -0300 | [diff] [blame] | 161 | case MEDIA_GRAPH_PAD: |
| 162 | gobj->id = media_gobj_gen_id(type, ++mdev->pad_id); |
| 163 | break; |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 164 | case MEDIA_GRAPH_LINK: |
| 165 | gobj->id = media_gobj_gen_id(type, ++mdev->link_id); |
| 166 | break; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 167 | case MEDIA_GRAPH_INTF_DEVNODE: |
| 168 | gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id); |
| 169 | break; |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 170 | } |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 171 | dev_dbg_obj(__func__, gobj); |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * media_gobj_remove - Stop using a graph object on a media device |
| 176 | * |
| 177 | * @graph_obj: Pointer to the object |
| 178 | * |
| 179 | * This should be called at media_device_unregister_*() routines |
| 180 | */ |
| 181 | void media_gobj_remove(struct media_gobj *gobj) |
| 182 | { |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 183 | dev_dbg_obj(__func__, gobj); |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 187 | * media_entity_init - Initialize a media entity |
| 188 | * |
| 189 | * @num_pads: Total number of sink and source pads. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 190 | * @pads: Array of 'num_pads' pads. |
| 191 | * |
| 192 | * The total number of pads is an intrinsic property of entities known by the |
| 193 | * entity driver, while the total number of links depends on hardware design |
| 194 | * 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] | 195 | * use cases the number of links can safely be assumed to be equal to or |
| 196 | * larger than the number of pads. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 197 | * |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 198 | * For those reasons the links array can be preallocated based on the number |
| 199 | * 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] | 200 | * |
| 201 | * 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] | 202 | * 'num_pads' elements. The media_entity::max_links field will be set to the |
| 203 | * number of allocated elements. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 204 | * |
| 205 | * The pads array is managed by the entity driver and passed to |
| 206 | * media_entity_init() where its pointer will be stored in the entity structure. |
| 207 | */ |
| 208 | int |
| 209 | media_entity_init(struct media_entity *entity, u16 num_pads, |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 210 | struct media_pad *pads) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 211 | { |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 212 | unsigned int i; |
| 213 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 214 | entity->group_id = 0; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 215 | entity->num_links = 0; |
| 216 | entity->num_backlinks = 0; |
| 217 | entity->num_pads = num_pads; |
| 218 | entity->pads = pads; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 219 | |
| 220 | for (i = 0; i < num_pads; i++) { |
| 221 | pads[i].entity = entity; |
| 222 | pads[i].index = i; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(media_entity_init); |
| 228 | |
| 229 | void |
| 230 | media_entity_cleanup(struct media_entity *entity) |
| 231 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 232 | struct media_link *link, *tmp; |
| 233 | |
| 234 | list_for_each_entry_safe(link, tmp, &entity->links, list) { |
| 235 | media_gobj_remove(&link->graph_obj); |
| 236 | list_del(&link->list); |
| 237 | kfree(link); |
| 238 | } |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 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++; |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 264 | graph->stack[graph->top].link = (&entity->links)->next; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 306 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 307 | /** |
| 308 | * media_entity_graph_walk_next - Get the next entity in the graph |
| 309 | * @graph: Media graph structure |
| 310 | * |
| 311 | * Perform a depth-first traversal of the given media entities graph. |
| 312 | * |
| 313 | * The graph structure must have been previously initialized with a call to |
| 314 | * media_entity_graph_walk_start(). |
| 315 | * |
| 316 | * Return the next entity in the graph or NULL if the whole graph have been |
| 317 | * traversed. |
| 318 | */ |
| 319 | struct media_entity * |
| 320 | media_entity_graph_walk_next(struct media_entity_graph *graph) |
| 321 | { |
| 322 | if (stack_top(graph) == NULL) |
| 323 | return NULL; |
| 324 | |
| 325 | /* |
| 326 | * Depth first search. Push entity to stack and continue from |
| 327 | * top of the stack until no more entities on the level can be |
| 328 | * found. |
| 329 | */ |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 330 | while (link_top(graph) != &(stack_top(graph)->links)) { |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 331 | struct media_entity *entity = stack_top(graph); |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 332 | struct media_link *link; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 333 | struct media_entity *next; |
| 334 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 335 | link = list_entry(link_top(graph), typeof(*link), list); |
| 336 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 337 | /* The link is not enabled so we do not follow. */ |
| 338 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 339 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 340 | continue; |
| 341 | } |
| 342 | |
| 343 | /* Get the entity in the other end of the link . */ |
| 344 | next = media_entity_other(entity, link); |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 345 | if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID)) |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 346 | return NULL; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 347 | |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 348 | /* Has the entity already been visited? */ |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 349 | if (__test_and_set_bit(media_entity_id(next), graph->entities)) { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 350 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 351 | continue; |
| 352 | } |
| 353 | |
| 354 | /* Push the new entity to stack and start over. */ |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 355 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 356 | stack_push(graph, next); |
| 357 | } |
| 358 | |
| 359 | return stack_pop(graph); |
| 360 | } |
| 361 | EXPORT_SYMBOL_GPL(media_entity_graph_walk_next); |
| 362 | |
| 363 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 364 | * Pipeline management |
| 365 | */ |
| 366 | |
| 367 | /** |
| 368 | * media_entity_pipeline_start - Mark a pipeline as streaming |
| 369 | * @entity: Starting entity |
| 370 | * @pipe: Media pipeline to be assigned to all entities in the pipeline. |
| 371 | * |
| 372 | * Mark all entities connected to a given entity through enabled links, either |
| 373 | * directly or indirectly, as streaming. The given pipeline object is assigned to |
| 374 | * every entity in the pipeline and stored in the media_entity pipe field. |
| 375 | * |
| 376 | * Calls to this function can be nested, in which case the same number of |
| 377 | * media_entity_pipeline_stop() calls will be required to stop streaming. The |
| 378 | * pipeline pointer must be identical for all nested calls to |
| 379 | * media_entity_pipeline_start(). |
| 380 | */ |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 381 | __must_check int media_entity_pipeline_start(struct media_entity *entity, |
| 382 | struct media_pipeline *pipe) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 383 | { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 384 | struct media_device *mdev = entity->graph_obj.mdev; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 385 | struct media_entity_graph graph; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 386 | struct media_entity *entity_err = entity; |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 387 | struct media_link *link; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 388 | int ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 389 | |
| 390 | mutex_lock(&mdev->graph_mutex); |
| 391 | |
| 392 | media_entity_graph_walk_start(&graph, entity); |
| 393 | |
| 394 | while ((entity = media_entity_graph_walk_next(&graph))) { |
Mauro Carvalho Chehab | ef69ee1 | 2015-10-01 18:07:53 -0300 | [diff] [blame] | 395 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
| 396 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 397 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 398 | entity->stream_count++; |
| 399 | WARN_ON(entity->pipe && entity->pipe != pipe); |
| 400 | entity->pipe = pipe; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 401 | |
| 402 | /* Already streaming --- no need to check. */ |
| 403 | if (entity->stream_count > 1) |
| 404 | continue; |
| 405 | |
| 406 | if (!entity->ops || !entity->ops->link_validate) |
| 407 | continue; |
| 408 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 409 | bitmap_zero(active, entity->num_pads); |
| 410 | bitmap_fill(has_no_links, entity->num_pads); |
| 411 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 412 | list_for_each_entry(link, &entity->links, list) { |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 413 | struct media_pad *pad = link->sink->entity == entity |
| 414 | ? link->sink : link->source; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 415 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 416 | /* Mark that a pad is connected by a link. */ |
| 417 | bitmap_clear(has_no_links, pad->index, 1); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 418 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 419 | /* |
| 420 | * Pads that either do not need to connect or |
| 421 | * are connected through an enabled link are |
| 422 | * fine. |
| 423 | */ |
| 424 | if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || |
| 425 | link->flags & MEDIA_LNK_FL_ENABLED) |
| 426 | bitmap_set(active, pad->index, 1); |
| 427 | |
| 428 | /* |
| 429 | * Link validation will only take place for |
| 430 | * sink ends of the link that are enabled. |
| 431 | */ |
| 432 | if (link->sink != pad || |
| 433 | !(link->flags & MEDIA_LNK_FL_ENABLED)) |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 434 | continue; |
| 435 | |
| 436 | ret = entity->ops->link_validate(link); |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 437 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 438 | dev_dbg(entity->graph_obj.mdev->dev, |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 439 | "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n", |
Sakari Ailus | 823ea2a | 2015-02-12 11:43:11 -0200 | [diff] [blame] | 440 | link->source->entity->name, |
| 441 | link->source->index, |
| 442 | entity->name, link->sink->index, ret); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 443 | goto error; |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 444 | } |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 445 | } |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 446 | |
| 447 | /* Either no links or validated links are fine. */ |
| 448 | bitmap_or(active, active, has_no_links, entity->num_pads); |
| 449 | |
| 450 | if (!bitmap_full(active, entity->num_pads)) { |
| 451 | ret = -EPIPE; |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 452 | dev_dbg(entity->graph_obj.mdev->dev, |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 453 | "\"%s\":%u must be connected by an enabled link\n", |
| 454 | entity->name, |
Sakari Ailus | 094f1ca | 2014-11-03 17:55:51 -0300 | [diff] [blame] | 455 | (unsigned)find_first_zero_bit( |
| 456 | active, entity->num_pads)); |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 457 | goto error; |
| 458 | } |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | mutex_unlock(&mdev->graph_mutex); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | |
| 465 | error: |
| 466 | /* |
| 467 | * Link validation on graph failed. We revert what we did and |
| 468 | * return the error. |
| 469 | */ |
| 470 | media_entity_graph_walk_start(&graph, entity_err); |
| 471 | |
| 472 | while ((entity_err = media_entity_graph_walk_next(&graph))) { |
| 473 | entity_err->stream_count--; |
| 474 | if (entity_err->stream_count == 0) |
| 475 | entity_err->pipe = NULL; |
| 476 | |
| 477 | /* |
| 478 | * We haven't increased stream_count further than this |
| 479 | * so we quit here. |
| 480 | */ |
| 481 | if (entity_err == entity) |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | mutex_unlock(&mdev->graph_mutex); |
| 486 | |
| 487 | return ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 488 | } |
| 489 | EXPORT_SYMBOL_GPL(media_entity_pipeline_start); |
| 490 | |
| 491 | /** |
| 492 | * media_entity_pipeline_stop - Mark a pipeline as not streaming |
| 493 | * @entity: Starting entity |
| 494 | * |
| 495 | * Mark all entities connected to a given entity through enabled links, either |
| 496 | * directly or indirectly, as not streaming. The media_entity pipe field is |
| 497 | * reset to NULL. |
| 498 | * |
| 499 | * If multiple calls to media_entity_pipeline_start() have been made, the same |
| 500 | * number of calls to this function are required to mark the pipeline as not |
| 501 | * streaming. |
| 502 | */ |
| 503 | void media_entity_pipeline_stop(struct media_entity *entity) |
| 504 | { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 505 | struct media_device *mdev = entity->graph_obj.mdev; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 506 | struct media_entity_graph graph; |
| 507 | |
| 508 | mutex_lock(&mdev->graph_mutex); |
| 509 | |
| 510 | media_entity_graph_walk_start(&graph, entity); |
| 511 | |
| 512 | while ((entity = media_entity_graph_walk_next(&graph))) { |
| 513 | entity->stream_count--; |
| 514 | if (entity->stream_count == 0) |
| 515 | entity->pipe = NULL; |
| 516 | } |
| 517 | |
| 518 | mutex_unlock(&mdev->graph_mutex); |
| 519 | } |
| 520 | EXPORT_SYMBOL_GPL(media_entity_pipeline_stop); |
| 521 | |
| 522 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 523 | * Module use count |
| 524 | */ |
| 525 | |
| 526 | /* |
| 527 | * media_entity_get - Get a reference to the parent module |
| 528 | * @entity: The entity |
| 529 | * |
| 530 | * Get a reference to the parent media device module. |
| 531 | * |
| 532 | * The function will return immediately if @entity is NULL. |
| 533 | * |
| 534 | * Return a pointer to the entity on success or NULL on failure. |
| 535 | */ |
| 536 | struct media_entity *media_entity_get(struct media_entity *entity) |
| 537 | { |
| 538 | if (entity == NULL) |
| 539 | return NULL; |
| 540 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 541 | if (entity->graph_obj.mdev->dev && |
| 542 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 543 | return NULL; |
| 544 | |
| 545 | return entity; |
| 546 | } |
| 547 | EXPORT_SYMBOL_GPL(media_entity_get); |
| 548 | |
| 549 | /* |
| 550 | * media_entity_put - Release the reference to the parent module |
| 551 | * @entity: The entity |
| 552 | * |
| 553 | * Release the reference count acquired by media_entity_get(). |
| 554 | * |
| 555 | * The function will return immediately if @entity is NULL. |
| 556 | */ |
| 557 | void media_entity_put(struct media_entity *entity) |
| 558 | { |
| 559 | if (entity == NULL) |
| 560 | return; |
| 561 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 562 | if (entity->graph_obj.mdev->dev) |
| 563 | module_put(entity->graph_obj.mdev->dev->driver->owner); |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 564 | } |
| 565 | EXPORT_SYMBOL_GPL(media_entity_put); |
| 566 | |
| 567 | /* ----------------------------------------------------------------------------- |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 568 | * Links management |
| 569 | */ |
| 570 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 571 | static struct media_link *media_add_link(struct list_head *head) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 572 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 573 | struct media_link *link; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 574 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 575 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 576 | if (link == NULL) |
| 577 | return NULL; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 578 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 579 | list_add_tail(&link->list, head); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 580 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 581 | return link; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 582 | } |
| 583 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 584 | static void __media_entity_remove_link(struct media_entity *entity, |
| 585 | struct media_link *link); |
| 586 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 587 | int |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 588 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 589 | struct media_entity *sink, u16 sink_pad, u32 flags) |
| 590 | { |
| 591 | struct media_link *link; |
| 592 | struct media_link *backlink; |
| 593 | |
| 594 | BUG_ON(source == NULL || sink == NULL); |
| 595 | BUG_ON(source_pad >= source->num_pads); |
| 596 | BUG_ON(sink_pad >= sink->num_pads); |
| 597 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 598 | link = media_add_link(&source->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 599 | if (link == NULL) |
| 600 | return -ENOMEM; |
| 601 | |
| 602 | link->source = &source->pads[source_pad]; |
| 603 | link->sink = &sink->pads[sink_pad]; |
| 604 | link->flags = flags; |
| 605 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 606 | /* Initialize graph object embedded at the new link */ |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 607 | media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 608 | &link->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 609 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 610 | /* Create the backlink. Backlinks are used to help graph traversal and |
| 611 | * are not reported to userspace. |
| 612 | */ |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 613 | backlink = media_add_link(&sink->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 614 | if (backlink == NULL) { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 615 | __media_entity_remove_link(source, link); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 616 | return -ENOMEM; |
| 617 | } |
| 618 | |
| 619 | backlink->source = &source->pads[source_pad]; |
| 620 | backlink->sink = &sink->pads[sink_pad]; |
| 621 | backlink->flags = flags; |
| 622 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 623 | /* Initialize graph object embedded at the new link */ |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 624 | media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 625 | &backlink->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 626 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 627 | link->reverse = backlink; |
| 628 | backlink->reverse = link; |
| 629 | |
| 630 | sink->num_backlinks++; |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 631 | sink->num_links++; |
| 632 | source->num_links++; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 636 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 637 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 638 | static void __media_entity_remove_link(struct media_entity *entity, |
| 639 | struct media_link *link) |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 640 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 641 | struct media_link *rlink, *tmp; |
| 642 | struct media_entity *remote; |
| 643 | unsigned int r = 0; |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 644 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 645 | if (link->source->entity == entity) |
| 646 | remote = link->sink->entity; |
| 647 | else |
| 648 | remote = link->source->entity; |
| 649 | |
| 650 | list_for_each_entry_safe(rlink, tmp, &remote->links, list) { |
| 651 | if (rlink != link->reverse) { |
| 652 | r++; |
| 653 | continue; |
| 654 | } |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 655 | |
| 656 | if (link->source->entity == entity) |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 657 | remote->num_backlinks--; |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 658 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 659 | if (--remote->num_links == 0) |
| 660 | break; |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 661 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 662 | /* Remove the remote link */ |
| 663 | list_del(&rlink->list); |
| 664 | kfree(rlink); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 665 | } |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 666 | list_del(&link->list); |
| 667 | kfree(link); |
| 668 | } |
| 669 | |
| 670 | void __media_entity_remove_links(struct media_entity *entity) |
| 671 | { |
| 672 | struct media_link *link, *tmp; |
| 673 | |
| 674 | list_for_each_entry_safe(link, tmp, &entity->links, list) |
| 675 | __media_entity_remove_link(entity, link); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 676 | |
| 677 | entity->num_links = 0; |
| 678 | entity->num_backlinks = 0; |
| 679 | } |
| 680 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); |
| 681 | |
| 682 | void media_entity_remove_links(struct media_entity *entity) |
| 683 | { |
| 684 | /* Do nothing if the entity is not registered. */ |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 685 | if (entity->graph_obj.mdev == NULL) |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 686 | return; |
| 687 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 688 | mutex_lock(&entity->graph_obj.mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 689 | __media_entity_remove_links(entity); |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 690 | mutex_unlock(&entity->graph_obj.mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 691 | } |
| 692 | EXPORT_SYMBOL_GPL(media_entity_remove_links); |
| 693 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 694 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
| 695 | { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 696 | int ret; |
| 697 | |
| 698 | /* Notify both entities. */ |
| 699 | ret = media_entity_call(link->source->entity, link_setup, |
| 700 | link->source, link->sink, flags); |
| 701 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 702 | return ret; |
| 703 | |
| 704 | ret = media_entity_call(link->sink->entity, link_setup, |
| 705 | link->sink, link->source, flags); |
| 706 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 707 | media_entity_call(link->source->entity, link_setup, |
| 708 | link->source, link->sink, link->flags); |
| 709 | return ret; |
| 710 | } |
| 711 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 712 | link->flags = flags; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 713 | link->reverse->flags = link->flags; |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * __media_entity_setup_link - Configure a media link |
| 720 | * @link: The link being configured |
| 721 | * @flags: Link configuration flags |
| 722 | * |
| 723 | * The bulk of link setup is handled by the two entities connected through the |
| 724 | * link. This function notifies both entities of the link configuration change. |
| 725 | * |
| 726 | * If the link is immutable or if the current and new configuration are |
| 727 | * identical, return immediately. |
| 728 | * |
| 729 | * The user is expected to hold link->source->parent->mutex. If not, |
| 730 | * media_entity_setup_link() should be used instead. |
| 731 | */ |
| 732 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
| 733 | { |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 734 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 735 | struct media_device *mdev; |
| 736 | struct media_entity *source, *sink; |
| 737 | int ret = -EBUSY; |
| 738 | |
| 739 | if (link == NULL) |
| 740 | return -EINVAL; |
| 741 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 742 | /* The non-modifiable link flags must not be modified. */ |
| 743 | if ((link->flags & ~mask) != (flags & ~mask)) |
| 744 | return -EINVAL; |
| 745 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 746 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
| 747 | return link->flags == flags ? 0 : -EINVAL; |
| 748 | |
| 749 | if (link->flags == flags) |
| 750 | return 0; |
| 751 | |
| 752 | source = link->source->entity; |
| 753 | sink = link->sink->entity; |
| 754 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 755 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
| 756 | (source->stream_count || sink->stream_count)) |
| 757 | return -EBUSY; |
| 758 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 759 | mdev = source->graph_obj.mdev; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 760 | |
Sylwester Nawrocki | 813f5c0 | 2013-05-31 10:37:26 -0300 | [diff] [blame] | 761 | if (mdev->link_notify) { |
| 762 | ret = mdev->link_notify(link, flags, |
| 763 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 764 | if (ret < 0) |
| 765 | return ret; |
| 766 | } |
| 767 | |
| 768 | ret = __media_entity_setup_link_notify(link, flags); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 769 | |
Sylwester Nawrocki | 813f5c0 | 2013-05-31 10:37:26 -0300 | [diff] [blame] | 770 | if (mdev->link_notify) |
| 771 | mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 772 | |
| 773 | return ret; |
| 774 | } |
| 775 | |
| 776 | int media_entity_setup_link(struct media_link *link, u32 flags) |
| 777 | { |
| 778 | int ret; |
| 779 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 780 | mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 781 | ret = __media_entity_setup_link(link, flags); |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 782 | mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | EXPORT_SYMBOL_GPL(media_entity_setup_link); |
| 787 | |
| 788 | /** |
| 789 | * media_entity_find_link - Find a link between two pads |
| 790 | * @source: Source pad |
| 791 | * @sink: Sink pad |
| 792 | * |
| 793 | * Return a pointer to the link between the two entities. If no such link |
| 794 | * exists, return NULL. |
| 795 | */ |
| 796 | struct media_link * |
| 797 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) |
| 798 | { |
| 799 | struct media_link *link; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 800 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 801 | list_for_each_entry(link, &source->entity->links, list) { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 802 | if (link->source->entity == source->entity && |
| 803 | link->source->index == source->index && |
| 804 | link->sink->entity == sink->entity && |
| 805 | link->sink->index == sink->index) |
| 806 | return link; |
| 807 | } |
| 808 | |
| 809 | return NULL; |
| 810 | } |
| 811 | EXPORT_SYMBOL_GPL(media_entity_find_link); |
| 812 | |
| 813 | /** |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 814 | * media_entity_remote_pad - Find the pad at the remote end of a link |
| 815 | * @pad: Pad at the local end of the link |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 816 | * |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 817 | * Search for a remote pad connected to the given pad by iterating over all |
| 818 | * 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] | 819 | * |
| 820 | * Return a pointer to the pad at the remote end of the first found enabled |
| 821 | * link, or NULL if no enabled link has been found. |
| 822 | */ |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 823 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 824 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 825 | struct media_link *link; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 826 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 827 | list_for_each_entry(link, &pad->entity->links, list) { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 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); |