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