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