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