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