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