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