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