Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Media device |
| 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 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 23 | #include <linux/compat.h> |
| 24 | #include <linux/export.h> |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 25 | #include <linux/idr.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 26 | #include <linux/ioctl.h> |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 27 | #include <linux/media.h> |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 28 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 497e80c | 2015-12-11 07:23:09 -0200 | [diff] [blame] | 29 | #include <linux/types.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 30 | |
| 31 | #include <media/media-device.h> |
| 32 | #include <media/media-devnode.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 33 | #include <media/media-entity.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 34 | |
Shuah Khan | e576d60 | 2015-06-05 17:11:54 -0300 | [diff] [blame] | 35 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 36 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 37 | /* ----------------------------------------------------------------------------- |
| 38 | * Userspace API |
| 39 | */ |
| 40 | |
| 41 | static int media_device_open(struct file *filp) |
| 42 | { |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int media_device_close(struct file *filp) |
| 47 | { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int media_device_get_info(struct media_device *dev, |
| 52 | struct media_device_info __user *__info) |
| 53 | { |
| 54 | struct media_device_info info; |
| 55 | |
| 56 | memset(&info, 0, sizeof(info)); |
| 57 | |
| 58 | strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver)); |
| 59 | strlcpy(info.model, dev->model, sizeof(info.model)); |
| 60 | strlcpy(info.serial, dev->serial, sizeof(info.serial)); |
| 61 | strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info)); |
| 62 | |
| 63 | info.media_version = MEDIA_API_VERSION; |
| 64 | info.hw_revision = dev->hw_revision; |
| 65 | info.driver_version = dev->driver_version; |
| 66 | |
Nicolas THERY | b17d394 | 2012-08-07 05:24:59 -0300 | [diff] [blame] | 67 | if (copy_to_user(__info, &info, sizeof(*__info))) |
| 68 | return -EFAULT; |
| 69 | return 0; |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 70 | } |
| 71 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 72 | static struct media_entity *find_entity(struct media_device *mdev, u32 id) |
| 73 | { |
| 74 | struct media_entity *entity; |
| 75 | int next = id & MEDIA_ENT_ID_FLAG_NEXT; |
| 76 | |
| 77 | id &= ~MEDIA_ENT_ID_FLAG_NEXT; |
| 78 | |
| 79 | spin_lock(&mdev->lock); |
| 80 | |
| 81 | media_device_for_each_entity(entity, mdev) { |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 82 | if (((media_entity_id(entity) == id) && !next) || |
| 83 | ((media_entity_id(entity) > id) && next)) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 84 | spin_unlock(&mdev->lock); |
| 85 | return entity; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | spin_unlock(&mdev->lock); |
| 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | static long media_device_enum_entities(struct media_device *mdev, |
| 95 | struct media_entity_desc __user *uent) |
| 96 | { |
| 97 | struct media_entity *ent; |
| 98 | struct media_entity_desc u_ent; |
| 99 | |
Salva Peiró | e6a6234 | 2014-04-30 19:48:02 +0200 | [diff] [blame] | 100 | memset(&u_ent, 0, sizeof(u_ent)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 101 | if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id))) |
| 102 | return -EFAULT; |
| 103 | |
| 104 | ent = find_entity(mdev, u_ent.id); |
| 105 | |
| 106 | if (ent == NULL) |
| 107 | return -EINVAL; |
| 108 | |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 109 | u_ent.id = media_entity_id(ent); |
Laurent Pinchart | de8eae3 | 2014-07-17 08:52:08 -0300 | [diff] [blame] | 110 | if (ent->name) |
| 111 | strlcpy(u_ent.name, ent->name, sizeof(u_ent.name)); |
Mauro Carvalho Chehab | 0e576b7 | 2015-09-06 09:33:39 -0300 | [diff] [blame] | 112 | u_ent.type = ent->function; |
Mauro Carvalho Chehab | 8ed8c88 | 2015-12-11 08:02:19 -0200 | [diff] [blame] | 113 | u_ent.revision = 0; /* Unused */ |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 114 | u_ent.flags = ent->flags; |
Mauro Carvalho Chehab | 8ed8c88 | 2015-12-11 08:02:19 -0200 | [diff] [blame] | 115 | u_ent.group_id = 0; /* Unused */ |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 116 | u_ent.pads = ent->num_pads; |
| 117 | u_ent.links = ent->num_links - ent->num_backlinks; |
Clemens Ladisch | fa5034c | 2011-11-05 18:42:01 -0300 | [diff] [blame] | 118 | memcpy(&u_ent.raw, &ent->info, sizeof(ent->info)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 119 | if (copy_to_user(uent, &u_ent, sizeof(u_ent))) |
| 120 | return -EFAULT; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static void media_device_kpad_to_upad(const struct media_pad *kpad, |
| 125 | struct media_pad_desc *upad) |
| 126 | { |
Mauro Carvalho Chehab | fa76239 | 2015-08-14 10:42:05 -0300 | [diff] [blame] | 127 | upad->entity = media_entity_id(kpad->entity); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 128 | upad->index = kpad->index; |
| 129 | upad->flags = kpad->flags; |
| 130 | } |
| 131 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 132 | static long __media_device_enum_links(struct media_device *mdev, |
| 133 | struct media_links_enum *links) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 134 | { |
| 135 | struct media_entity *entity; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 136 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 137 | entity = find_entity(mdev, links->entity); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 138 | if (entity == NULL) |
| 139 | return -EINVAL; |
| 140 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 141 | if (links->pads) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 142 | unsigned int p; |
| 143 | |
| 144 | for (p = 0; p < entity->num_pads; p++) { |
| 145 | struct media_pad_desc pad; |
Dan Carpenter | c88e739 | 2013-04-13 06:32:15 -0300 | [diff] [blame] | 146 | |
| 147 | memset(&pad, 0, sizeof(pad)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 148 | media_device_kpad_to_upad(&entity->pads[p], &pad); |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 149 | if (copy_to_user(&links->pads[p], &pad, sizeof(pad))) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 150 | return -EFAULT; |
| 151 | } |
| 152 | } |
| 153 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 154 | if (links->links) { |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 155 | struct media_link *link; |
| 156 | struct media_link_desc __user *ulink_desc = links->links; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 157 | |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 158 | list_for_each_entry(link, &entity->links, list) { |
| 159 | struct media_link_desc klink_desc; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 160 | |
| 161 | /* Ignore backlinks. */ |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 162 | if (link->source->entity != entity) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 163 | continue; |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 164 | memset(&klink_desc, 0, sizeof(klink_desc)); |
| 165 | media_device_kpad_to_upad(link->source, |
| 166 | &klink_desc.source); |
| 167 | media_device_kpad_to_upad(link->sink, |
| 168 | &klink_desc.sink); |
| 169 | klink_desc.flags = link->flags; |
| 170 | if (copy_to_user(ulink_desc, &klink_desc, |
| 171 | sizeof(*ulink_desc))) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 172 | return -EFAULT; |
Mauro Carvalho Chehab | b83e250 | 2015-12-11 07:25:01 -0200 | [diff] [blame] | 173 | ulink_desc++; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 174 | } |
| 175 | } |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static long media_device_enum_links(struct media_device *mdev, |
| 181 | struct media_links_enum __user *ulinks) |
| 182 | { |
| 183 | struct media_links_enum links; |
| 184 | int rval; |
| 185 | |
| 186 | if (copy_from_user(&links, ulinks, sizeof(links))) |
| 187 | return -EFAULT; |
| 188 | |
| 189 | rval = __media_device_enum_links(mdev, &links); |
| 190 | if (rval < 0) |
| 191 | return rval; |
| 192 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 193 | if (copy_to_user(ulinks, &links, sizeof(*ulinks))) |
| 194 | return -EFAULT; |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 195 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 199 | static long media_device_setup_link(struct media_device *mdev, |
| 200 | struct media_link_desc __user *_ulink) |
| 201 | { |
| 202 | struct media_link *link = NULL; |
| 203 | struct media_link_desc ulink; |
| 204 | struct media_entity *source; |
| 205 | struct media_entity *sink; |
| 206 | int ret; |
| 207 | |
| 208 | if (copy_from_user(&ulink, _ulink, sizeof(ulink))) |
| 209 | return -EFAULT; |
| 210 | |
| 211 | /* Find the source and sink entities and link. |
| 212 | */ |
| 213 | source = find_entity(mdev, ulink.source.entity); |
| 214 | sink = find_entity(mdev, ulink.sink.entity); |
| 215 | |
| 216 | if (source == NULL || sink == NULL) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | if (ulink.source.index >= source->num_pads || |
| 220 | ulink.sink.index >= sink->num_pads) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | link = media_entity_find_link(&source->pads[ulink.source.index], |
| 224 | &sink->pads[ulink.sink.index]); |
| 225 | if (link == NULL) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | /* Setup the link on both entities. */ |
| 229 | ret = __media_entity_setup_link(link, ulink.flags); |
| 230 | |
| 231 | if (copy_to_user(_ulink, &ulink, sizeof(ulink))) |
| 232 | return -EFAULT; |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
Mauro Carvalho Chehab | be0270ec | 2015-12-28 12:03:47 -0200 | [diff] [blame] | 237 | #if 0 /* Let's postpone it to Kernel 4.6 */ |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 238 | static long __media_device_get_topology(struct media_device *mdev, |
| 239 | struct media_v2_topology *topo) |
| 240 | { |
| 241 | struct media_entity *entity; |
| 242 | struct media_interface *intf; |
| 243 | struct media_pad *pad; |
| 244 | struct media_link *link; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 245 | struct media_v2_entity kentity, *uentity; |
| 246 | struct media_v2_interface kintf, *uintf; |
| 247 | struct media_v2_pad kpad, *upad; |
| 248 | struct media_v2_link klink, *ulink; |
Mauro Carvalho Chehab | 9033b1a | 2015-09-09 08:23:51 -0300 | [diff] [blame] | 249 | unsigned int i; |
| 250 | int ret = 0; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 251 | |
| 252 | topo->topology_version = mdev->topology_version; |
| 253 | |
| 254 | /* Get entities and number of entities */ |
| 255 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 256 | uentity = media_get_uptr(topo->ptr_entities); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 257 | media_device_for_each_entity(entity, mdev) { |
| 258 | i++; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 259 | if (ret || !uentity) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 260 | continue; |
| 261 | |
| 262 | if (i > topo->num_entities) { |
| 263 | ret = -ENOSPC; |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | /* Copy fields to userspace struct if not error */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 268 | memset(&kentity, 0, sizeof(kentity)); |
| 269 | kentity.id = entity->graph_obj.id; |
| 270 | kentity.function = entity->function; |
| 271 | strncpy(kentity.name, entity->name, |
| 272 | sizeof(kentity.name)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 273 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 274 | if (copy_to_user(uentity, &kentity, sizeof(kentity))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 275 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 276 | uentity++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 277 | } |
| 278 | topo->num_entities = i; |
| 279 | |
| 280 | /* Get interfaces and number of interfaces */ |
| 281 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 282 | uintf = media_get_uptr(topo->ptr_interfaces); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 283 | media_device_for_each_intf(intf, mdev) { |
| 284 | i++; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 285 | if (ret || !uintf) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 286 | continue; |
| 287 | |
| 288 | if (i > topo->num_interfaces) { |
| 289 | ret = -ENOSPC; |
| 290 | continue; |
| 291 | } |
| 292 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 293 | memset(&kintf, 0, sizeof(kintf)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 294 | |
| 295 | /* Copy intf fields to userspace struct */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 296 | kintf.id = intf->graph_obj.id; |
| 297 | kintf.intf_type = intf->type; |
| 298 | kintf.flags = intf->flags; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 299 | |
| 300 | if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) { |
| 301 | struct media_intf_devnode *devnode; |
| 302 | |
| 303 | devnode = intf_to_devnode(intf); |
| 304 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 305 | kintf.devnode.major = devnode->major; |
| 306 | kintf.devnode.minor = devnode->minor; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 307 | } |
| 308 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 309 | if (copy_to_user(uintf, &kintf, sizeof(kintf))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 310 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 311 | uintf++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 312 | } |
| 313 | topo->num_interfaces = i; |
| 314 | |
| 315 | /* Get pads and number of pads */ |
| 316 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 317 | upad = media_get_uptr(topo->ptr_pads); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 318 | media_device_for_each_pad(pad, mdev) { |
| 319 | i++; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 320 | if (ret || !upad) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 321 | continue; |
| 322 | |
| 323 | if (i > topo->num_pads) { |
| 324 | ret = -ENOSPC; |
| 325 | continue; |
| 326 | } |
| 327 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 328 | memset(&kpad, 0, sizeof(kpad)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 329 | |
| 330 | /* Copy pad fields to userspace struct */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 331 | kpad.id = pad->graph_obj.id; |
| 332 | kpad.entity_id = pad->entity->graph_obj.id; |
| 333 | kpad.flags = pad->flags; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 334 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 335 | if (copy_to_user(upad, &kpad, sizeof(kpad))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 336 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 337 | upad++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 338 | } |
| 339 | topo->num_pads = i; |
| 340 | |
| 341 | /* Get links and number of links */ |
| 342 | i = 0; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 343 | ulink = media_get_uptr(topo->ptr_links); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 344 | media_device_for_each_link(link, mdev) { |
Mauro Carvalho Chehab | 39d1ebc6 | 2015-08-30 09:53:57 -0300 | [diff] [blame] | 345 | if (link->is_backlink) |
| 346 | continue; |
| 347 | |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 348 | i++; |
| 349 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 350 | if (ret || !ulink) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 351 | continue; |
| 352 | |
| 353 | if (i > topo->num_links) { |
| 354 | ret = -ENOSPC; |
| 355 | continue; |
| 356 | } |
| 357 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 358 | memset(&klink, 0, sizeof(klink)); |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 359 | |
| 360 | /* Copy link fields to userspace struct */ |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 361 | klink.id = link->graph_obj.id; |
| 362 | klink.source_id = link->gobj0->id; |
| 363 | klink.sink_id = link->gobj1->id; |
| 364 | klink.flags = link->flags; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 365 | |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 366 | if (copy_to_user(ulink, &klink, sizeof(klink))) |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 367 | ret = -EFAULT; |
Mauro Carvalho Chehab | b3b7a9f | 2015-12-11 16:07:57 -0200 | [diff] [blame] | 368 | ulink++; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 369 | } |
| 370 | topo->num_links = i; |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static long media_device_get_topology(struct media_device *mdev, |
| 376 | struct media_v2_topology __user *utopo) |
| 377 | { |
| 378 | struct media_v2_topology ktopo; |
| 379 | int ret; |
| 380 | |
Dan Carpenter | a5c82e5 | 2015-12-15 09:00:40 -0200 | [diff] [blame] | 381 | if (copy_from_user(&ktopo, utopo, sizeof(ktopo))) |
| 382 | return -EFAULT; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 383 | |
| 384 | ret = __media_device_get_topology(mdev, &ktopo); |
| 385 | if (ret < 0) |
| 386 | return ret; |
| 387 | |
Dan Carpenter | a5c82e5 | 2015-12-15 09:00:40 -0200 | [diff] [blame] | 388 | if (copy_to_user(utopo, &ktopo, sizeof(*utopo))) |
| 389 | return -EFAULT; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 390 | |
Dan Carpenter | a5c82e5 | 2015-12-15 09:00:40 -0200 | [diff] [blame] | 391 | return 0; |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 392 | } |
Mauro Carvalho Chehab | be0270ec | 2015-12-28 12:03:47 -0200 | [diff] [blame] | 393 | #endif |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 394 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 395 | static long media_device_ioctl(struct file *filp, unsigned int cmd, |
| 396 | unsigned long arg) |
| 397 | { |
| 398 | struct media_devnode *devnode = media_devnode_data(filp); |
| 399 | struct media_device *dev = to_media_device(devnode); |
| 400 | long ret; |
| 401 | |
| 402 | switch (cmd) { |
| 403 | case MEDIA_IOC_DEVICE_INFO: |
| 404 | ret = media_device_get_info(dev, |
| 405 | (struct media_device_info __user *)arg); |
| 406 | break; |
| 407 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 408 | case MEDIA_IOC_ENUM_ENTITIES: |
| 409 | ret = media_device_enum_entities(dev, |
| 410 | (struct media_entity_desc __user *)arg); |
| 411 | break; |
| 412 | |
| 413 | case MEDIA_IOC_ENUM_LINKS: |
| 414 | mutex_lock(&dev->graph_mutex); |
| 415 | ret = media_device_enum_links(dev, |
| 416 | (struct media_links_enum __user *)arg); |
| 417 | mutex_unlock(&dev->graph_mutex); |
| 418 | break; |
| 419 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 420 | case MEDIA_IOC_SETUP_LINK: |
| 421 | mutex_lock(&dev->graph_mutex); |
| 422 | ret = media_device_setup_link(dev, |
| 423 | (struct media_link_desc __user *)arg); |
| 424 | mutex_unlock(&dev->graph_mutex); |
| 425 | break; |
| 426 | |
Mauro Carvalho Chehab | be0270ec | 2015-12-28 12:03:47 -0200 | [diff] [blame] | 427 | #if 0 /* Let's postpone it to Kernel 4.6 */ |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 428 | case MEDIA_IOC_G_TOPOLOGY: |
| 429 | mutex_lock(&dev->graph_mutex); |
| 430 | ret = media_device_get_topology(dev, |
| 431 | (struct media_v2_topology __user *)arg); |
| 432 | mutex_unlock(&dev->graph_mutex); |
| 433 | break; |
Mauro Carvalho Chehab | be0270ec | 2015-12-28 12:03:47 -0200 | [diff] [blame] | 434 | #endif |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 435 | default: |
| 436 | ret = -ENOIOCTLCMD; |
| 437 | } |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 442 | #ifdef CONFIG_COMPAT |
| 443 | |
| 444 | struct media_links_enum32 { |
| 445 | __u32 entity; |
| 446 | compat_uptr_t pads; /* struct media_pad_desc * */ |
| 447 | compat_uptr_t links; /* struct media_link_desc * */ |
| 448 | __u32 reserved[4]; |
| 449 | }; |
| 450 | |
| 451 | static long media_device_enum_links32(struct media_device *mdev, |
| 452 | struct media_links_enum32 __user *ulinks) |
| 453 | { |
| 454 | struct media_links_enum links; |
| 455 | compat_uptr_t pads_ptr, links_ptr; |
| 456 | |
| 457 | memset(&links, 0, sizeof(links)); |
| 458 | |
| 459 | if (get_user(links.entity, &ulinks->entity) |
| 460 | || get_user(pads_ptr, &ulinks->pads) |
| 461 | || get_user(links_ptr, &ulinks->links)) |
| 462 | return -EFAULT; |
| 463 | |
| 464 | links.pads = compat_ptr(pads_ptr); |
| 465 | links.links = compat_ptr(links_ptr); |
| 466 | |
| 467 | return __media_device_enum_links(mdev, &links); |
| 468 | } |
| 469 | |
| 470 | #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32) |
| 471 | |
| 472 | static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, |
| 473 | unsigned long arg) |
| 474 | { |
| 475 | struct media_devnode *devnode = media_devnode_data(filp); |
| 476 | struct media_device *dev = to_media_device(devnode); |
| 477 | long ret; |
| 478 | |
| 479 | switch (cmd) { |
| 480 | case MEDIA_IOC_DEVICE_INFO: |
| 481 | case MEDIA_IOC_ENUM_ENTITIES: |
| 482 | case MEDIA_IOC_SETUP_LINK: |
Mauro Carvalho Chehab | be0270ec | 2015-12-28 12:03:47 -0200 | [diff] [blame] | 483 | #if 0 /* Let's postpone it to Kernel 4.6 */ |
Mauro Carvalho Chehab | 8309f47 | 2015-08-23 10:36:41 -0300 | [diff] [blame] | 484 | case MEDIA_IOC_G_TOPOLOGY: |
Mauro Carvalho Chehab | be0270ec | 2015-12-28 12:03:47 -0200 | [diff] [blame] | 485 | #endif |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 486 | return media_device_ioctl(filp, cmd, arg); |
| 487 | |
| 488 | case MEDIA_IOC_ENUM_LINKS32: |
| 489 | mutex_lock(&dev->graph_mutex); |
| 490 | ret = media_device_enum_links32(dev, |
| 491 | (struct media_links_enum32 __user *)arg); |
| 492 | mutex_unlock(&dev->graph_mutex); |
| 493 | break; |
| 494 | |
| 495 | default: |
| 496 | ret = -ENOIOCTLCMD; |
| 497 | } |
| 498 | |
| 499 | return ret; |
| 500 | } |
| 501 | #endif /* CONFIG_COMPAT */ |
| 502 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 503 | static const struct media_file_operations media_device_fops = { |
| 504 | .owner = THIS_MODULE, |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 505 | .open = media_device_open, |
| 506 | .ioctl = media_device_ioctl, |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 507 | #ifdef CONFIG_COMPAT |
| 508 | .compat_ioctl = media_device_compat_ioctl, |
| 509 | #endif /* CONFIG_COMPAT */ |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 510 | .release = media_device_close, |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | /* ----------------------------------------------------------------------------- |
| 514 | * sysfs |
| 515 | */ |
| 516 | |
| 517 | static ssize_t show_model(struct device *cd, |
| 518 | struct device_attribute *attr, char *buf) |
| 519 | { |
| 520 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); |
| 521 | |
| 522 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); |
| 523 | } |
| 524 | |
| 525 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); |
| 526 | |
| 527 | /* ----------------------------------------------------------------------------- |
| 528 | * Registration/unregistration |
| 529 | */ |
| 530 | |
| 531 | static void media_device_release(struct media_devnode *mdev) |
| 532 | { |
Mauro Carvalho Chehab | 8f5a318 | 2015-08-13 15:22:24 -0300 | [diff] [blame] | 533 | dev_dbg(mdev->parent, "Media device released\n"); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /** |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 537 | * media_device_register_entity - Register an entity with a media device |
| 538 | * @mdev: The media device |
| 539 | * @entity: The entity |
| 540 | */ |
| 541 | int __must_check media_device_register_entity(struct media_device *mdev, |
| 542 | struct media_entity *entity) |
| 543 | { |
| 544 | unsigned int i; |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 545 | int ret; |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 546 | |
| 547 | if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN || |
| 548 | entity->function == MEDIA_ENT_F_UNKNOWN) |
| 549 | dev_warn(mdev->dev, |
| 550 | "Entity type for entity %s was not initialized!\n", |
| 551 | entity->name); |
| 552 | |
| 553 | /* Warn if we apparently re-register an entity */ |
| 554 | WARN_ON(entity->graph_obj.mdev != NULL); |
| 555 | entity->graph_obj.mdev = mdev; |
| 556 | INIT_LIST_HEAD(&entity->links); |
| 557 | entity->num_links = 0; |
| 558 | entity->num_backlinks = 0; |
| 559 | |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 560 | if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL)) |
| 561 | return -ENOMEM; |
| 562 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 563 | spin_lock(&mdev->lock); |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 564 | |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 565 | ret = ida_get_new_above(&mdev->entity_internal_idx, 1, |
| 566 | &entity->internal_idx); |
| 567 | if (ret < 0) { |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 568 | spin_unlock(&mdev->lock); |
Mauro Carvalho Chehab | ba0dd72 | 2015-12-16 15:33:54 -0200 | [diff] [blame] | 569 | return ret; |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | mdev->entity_internal_idx_max = |
| 573 | max(mdev->entity_internal_idx_max, entity->internal_idx); |
| 574 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 575 | /* Initialize media_gobj embedded at the entity */ |
| 576 | media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj); |
| 577 | |
| 578 | /* Initialize objects at the pads */ |
| 579 | for (i = 0; i < entity->num_pads; i++) |
| 580 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, |
| 581 | &entity->pads[i].graph_obj); |
| 582 | |
| 583 | spin_unlock(&mdev->lock); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | EXPORT_SYMBOL_GPL(media_device_register_entity); |
| 588 | |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 589 | static void __media_device_unregister_entity(struct media_entity *entity) |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 590 | { |
| 591 | struct media_device *mdev = entity->graph_obj.mdev; |
| 592 | struct media_link *link, *tmp; |
| 593 | struct media_interface *intf; |
| 594 | unsigned int i; |
| 595 | |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 596 | ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx); |
| 597 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 598 | /* Remove all interface links pointing to this entity */ |
| 599 | list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) { |
| 600 | list_for_each_entry_safe(link, tmp, &intf->links, list) { |
| 601 | if (link->entity == entity) |
| 602 | __media_remove_intf_link(link); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /* Remove all data links that belong to this entity */ |
| 607 | __media_entity_remove_links(entity); |
| 608 | |
| 609 | /* Remove all pads that belong to this entity */ |
| 610 | for (i = 0; i < entity->num_pads; i++) |
| 611 | media_gobj_destroy(&entity->pads[i].graph_obj); |
| 612 | |
| 613 | /* Remove the entity */ |
| 614 | media_gobj_destroy(&entity->graph_obj); |
| 615 | |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 616 | entity->graph_obj.mdev = NULL; |
| 617 | } |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 618 | |
| 619 | void media_device_unregister_entity(struct media_entity *entity) |
| 620 | { |
| 621 | struct media_device *mdev = entity->graph_obj.mdev; |
| 622 | |
| 623 | if (mdev == NULL) |
| 624 | return; |
| 625 | |
| 626 | spin_lock(&mdev->lock); |
| 627 | __media_device_unregister_entity(entity); |
| 628 | spin_unlock(&mdev->lock); |
| 629 | } |
Mauro Carvalho Chehab | b2ed8af | 2015-12-15 08:26:52 -0200 | [diff] [blame] | 630 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |
| 631 | |
| 632 | /** |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 633 | * media_device_init() - initialize a media device |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 634 | * @mdev: The media device |
| 635 | * |
| 636 | * The caller is responsible for initializing the media device before |
| 637 | * registration. The following fields must be set: |
| 638 | * |
| 639 | * - dev must point to the parent device |
| 640 | * - model must be filled with the device model name |
| 641 | */ |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 642 | void media_device_init(struct media_device *mdev) |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 643 | { |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 644 | INIT_LIST_HEAD(&mdev->entities); |
Mauro Carvalho Chehab | 57cf79b | 2015-08-21 09:23:22 -0300 | [diff] [blame] | 645 | INIT_LIST_HEAD(&mdev->interfaces); |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 646 | INIT_LIST_HEAD(&mdev->pads); |
| 647 | INIT_LIST_HEAD(&mdev->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 648 | spin_lock_init(&mdev->lock); |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 649 | mutex_init(&mdev->graph_mutex); |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 650 | ida_init(&mdev->entity_internal_idx); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 651 | |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 652 | dev_dbg(mdev->dev, "Media device initialized\n"); |
| 653 | } |
| 654 | EXPORT_SYMBOL_GPL(media_device_init); |
| 655 | |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 656 | void media_device_cleanup(struct media_device *mdev) |
| 657 | { |
Sakari Ailus | 665faa9 | 2015-12-16 11:32:17 -0200 | [diff] [blame] | 658 | ida_destroy(&mdev->entity_internal_idx); |
| 659 | mdev->entity_internal_idx_max = 0; |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 660 | mutex_destroy(&mdev->graph_mutex); |
| 661 | } |
| 662 | EXPORT_SYMBOL_GPL(media_device_cleanup); |
| 663 | |
Javier Martinez Canillas | 9832e15 | 2015-12-11 20:57:08 -0200 | [diff] [blame] | 664 | int __must_check __media_device_register(struct media_device *mdev, |
| 665 | struct module *owner) |
| 666 | { |
| 667 | int ret; |
| 668 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 669 | /* Register the device node. */ |
| 670 | mdev->devnode.fops = &media_device_fops; |
| 671 | mdev->devnode.parent = mdev->dev; |
| 672 | mdev->devnode.release = media_device_release; |
Javier Martinez Canillas | 8a95079 | 2015-12-11 20:57:09 -0200 | [diff] [blame] | 673 | |
| 674 | /* Set version 0 to indicate user-space that the graph is static */ |
| 675 | mdev->topology_version = 0; |
| 676 | |
Sakari Ailus | 85de721 | 2013-12-12 12:38:17 -0300 | [diff] [blame] | 677 | ret = media_devnode_register(&mdev->devnode, owner); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 678 | if (ret < 0) |
| 679 | return ret; |
| 680 | |
| 681 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); |
| 682 | if (ret < 0) { |
| 683 | media_devnode_unregister(&mdev->devnode); |
| 684 | return ret; |
| 685 | } |
| 686 | |
Mauro Carvalho Chehab | 8f5a318 | 2015-08-13 15:22:24 -0300 | [diff] [blame] | 687 | dev_dbg(mdev->dev, "Media device registered\n"); |
| 688 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 689 | return 0; |
| 690 | } |
Sakari Ailus | 85de721 | 2013-12-12 12:38:17 -0300 | [diff] [blame] | 691 | EXPORT_SYMBOL_GPL(__media_device_register); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 692 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 693 | void media_device_unregister(struct media_device *mdev) |
| 694 | { |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 695 | struct media_entity *entity; |
| 696 | struct media_entity *next; |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 697 | struct media_interface *intf, *tmp_intf; |
| 698 | |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 699 | if (mdev == NULL) |
Javier Martinez Canillas | 223d19c | 2015-12-11 20:57:07 -0200 | [diff] [blame] | 700 | return; |
| 701 | |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 702 | spin_lock(&mdev->lock); |
| 703 | |
| 704 | /* Check if mdev was ever registered at all */ |
| 705 | if (!media_devnode_is_registered(&mdev->devnode)) { |
| 706 | spin_unlock(&mdev->lock); |
| 707 | return; |
| 708 | } |
| 709 | |
Mauro Carvalho Chehab | f50d516 | 2015-09-04 15:10:29 -0300 | [diff] [blame] | 710 | /* Remove all entities from the media device */ |
| 711 | list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 712 | __media_device_unregister_entity(entity); |
Mauro Carvalho Chehab | f50d516 | 2015-09-04 15:10:29 -0300 | [diff] [blame] | 713 | |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 714 | /* Remove all interfaces from the media device */ |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 715 | list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces, |
| 716 | graph_obj.list) { |
| 717 | __media_remove_intf_links(intf); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 718 | media_gobj_destroy(&intf->graph_obj); |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 719 | kfree(intf); |
| 720 | } |
Mauro Carvalho Chehab | 821ed36 | 2015-12-15 08:36:51 -0200 | [diff] [blame] | 721 | |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 722 | spin_unlock(&mdev->lock); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 723 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 724 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); |
| 725 | media_devnode_unregister(&mdev->devnode); |
Mauro Carvalho Chehab | 8f5a318 | 2015-08-13 15:22:24 -0300 | [diff] [blame] | 726 | |
| 727 | dev_dbg(mdev->dev, "Media device unregistered\n"); |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 728 | } |
| 729 | EXPORT_SYMBOL_GPL(media_device_unregister); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 730 | |
Shuah Khan | d062f91 | 2015-06-03 12:12:53 -0300 | [diff] [blame] | 731 | static void media_device_release_devres(struct device *dev, void *res) |
| 732 | { |
| 733 | } |
| 734 | |
Shuah Khan | d062f91 | 2015-06-03 12:12:53 -0300 | [diff] [blame] | 735 | struct media_device *media_device_get_devres(struct device *dev) |
| 736 | { |
| 737 | struct media_device *mdev; |
| 738 | |
| 739 | mdev = devres_find(dev, media_device_release_devres, NULL, NULL); |
| 740 | if (mdev) |
| 741 | return mdev; |
| 742 | |
| 743 | mdev = devres_alloc(media_device_release_devres, |
| 744 | sizeof(struct media_device), GFP_KERNEL); |
| 745 | if (!mdev) |
| 746 | return NULL; |
| 747 | return devres_get(dev, mdev, NULL, NULL); |
| 748 | } |
| 749 | EXPORT_SYMBOL_GPL(media_device_get_devres); |
| 750 | |
Shuah Khan | d062f91 | 2015-06-03 12:12:53 -0300 | [diff] [blame] | 751 | struct media_device *media_device_find_devres(struct device *dev) |
| 752 | { |
| 753 | return devres_find(dev, media_device_release_devres, NULL, NULL); |
| 754 | } |
| 755 | EXPORT_SYMBOL_GPL(media_device_find_devres); |
Shuah Khan | e576d60 | 2015-06-05 17:11:54 -0300 | [diff] [blame] | 756 | |
| 757 | #endif /* CONFIG_MEDIA_CONTROLLER */ |