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