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