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