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> |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 27 | #include <linux/types.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 28 | |
| 29 | #include <media/media-device.h> |
| 30 | #include <media/media-devnode.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 31 | #include <media/media-entity.h> |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 32 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 33 | /* ----------------------------------------------------------------------------- |
| 34 | * Userspace API |
| 35 | */ |
| 36 | |
| 37 | static int media_device_open(struct file *filp) |
| 38 | { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static int media_device_close(struct file *filp) |
| 43 | { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int media_device_get_info(struct media_device *dev, |
| 48 | struct media_device_info __user *__info) |
| 49 | { |
| 50 | struct media_device_info info; |
| 51 | |
| 52 | memset(&info, 0, sizeof(info)); |
| 53 | |
| 54 | strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver)); |
| 55 | strlcpy(info.model, dev->model, sizeof(info.model)); |
| 56 | strlcpy(info.serial, dev->serial, sizeof(info.serial)); |
| 57 | strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info)); |
| 58 | |
| 59 | info.media_version = MEDIA_API_VERSION; |
| 60 | info.hw_revision = dev->hw_revision; |
| 61 | info.driver_version = dev->driver_version; |
| 62 | |
Nicolas THERY | b17d394 | 2012-08-07 05:24:59 -0300 | [diff] [blame] | 63 | if (copy_to_user(__info, &info, sizeof(*__info))) |
| 64 | return -EFAULT; |
| 65 | return 0; |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 66 | } |
| 67 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 68 | static struct media_entity *find_entity(struct media_device *mdev, u32 id) |
| 69 | { |
| 70 | struct media_entity *entity; |
| 71 | int next = id & MEDIA_ENT_ID_FLAG_NEXT; |
| 72 | |
| 73 | id &= ~MEDIA_ENT_ID_FLAG_NEXT; |
| 74 | |
| 75 | spin_lock(&mdev->lock); |
| 76 | |
| 77 | media_device_for_each_entity(entity, mdev) { |
| 78 | if ((entity->id == id && !next) || |
| 79 | (entity->id > id && next)) { |
| 80 | spin_unlock(&mdev->lock); |
| 81 | return entity; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | spin_unlock(&mdev->lock); |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | static long media_device_enum_entities(struct media_device *mdev, |
| 91 | struct media_entity_desc __user *uent) |
| 92 | { |
| 93 | struct media_entity *ent; |
| 94 | struct media_entity_desc u_ent; |
| 95 | |
| 96 | if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id))) |
| 97 | return -EFAULT; |
| 98 | |
| 99 | ent = find_entity(mdev, u_ent.id); |
| 100 | |
| 101 | if (ent == NULL) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | u_ent.id = ent->id; |
| 105 | u_ent.name[0] = '\0'; |
| 106 | if (ent->name) |
| 107 | strlcpy(u_ent.name, ent->name, sizeof(u_ent.name)); |
| 108 | u_ent.type = ent->type; |
| 109 | u_ent.revision = ent->revision; |
| 110 | u_ent.flags = ent->flags; |
| 111 | u_ent.group_id = ent->group_id; |
| 112 | u_ent.pads = ent->num_pads; |
| 113 | u_ent.links = ent->num_links - ent->num_backlinks; |
Clemens Ladisch | fa5034c | 2011-11-05 18:42:01 -0300 | [diff] [blame] | 114 | memcpy(&u_ent.raw, &ent->info, sizeof(ent->info)); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 115 | if (copy_to_user(uent, &u_ent, sizeof(u_ent))) |
| 116 | return -EFAULT; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void media_device_kpad_to_upad(const struct media_pad *kpad, |
| 121 | struct media_pad_desc *upad) |
| 122 | { |
| 123 | upad->entity = kpad->entity->id; |
| 124 | upad->index = kpad->index; |
| 125 | upad->flags = kpad->flags; |
| 126 | } |
| 127 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 128 | static long __media_device_enum_links(struct media_device *mdev, |
| 129 | struct media_links_enum *links) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 130 | { |
| 131 | struct media_entity *entity; |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 132 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 133 | entity = find_entity(mdev, links->entity); |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 134 | if (entity == NULL) |
| 135 | return -EINVAL; |
| 136 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 137 | if (links->pads) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 138 | unsigned int p; |
| 139 | |
| 140 | for (p = 0; p < entity->num_pads; p++) { |
| 141 | struct media_pad_desc pad; |
| 142 | media_device_kpad_to_upad(&entity->pads[p], &pad); |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 143 | if (copy_to_user(&links->pads[p], &pad, sizeof(pad))) |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 144 | return -EFAULT; |
| 145 | } |
| 146 | } |
| 147 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 148 | if (links->links) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 149 | struct media_link_desc __user *ulink; |
| 150 | unsigned int l; |
| 151 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 152 | for (l = 0, ulink = links->links; l < entity->num_links; l++) { |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 153 | struct media_link_desc link; |
| 154 | |
| 155 | /* Ignore backlinks. */ |
| 156 | if (entity->links[l].source->entity != entity) |
| 157 | continue; |
| 158 | |
| 159 | media_device_kpad_to_upad(entity->links[l].source, |
| 160 | &link.source); |
| 161 | media_device_kpad_to_upad(entity->links[l].sink, |
| 162 | &link.sink); |
| 163 | link.flags = entity->links[l].flags; |
| 164 | if (copy_to_user(ulink, &link, sizeof(*ulink))) |
| 165 | return -EFAULT; |
| 166 | ulink++; |
| 167 | } |
| 168 | } |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static long media_device_enum_links(struct media_device *mdev, |
| 174 | struct media_links_enum __user *ulinks) |
| 175 | { |
| 176 | struct media_links_enum links; |
| 177 | int rval; |
| 178 | |
| 179 | if (copy_from_user(&links, ulinks, sizeof(links))) |
| 180 | return -EFAULT; |
| 181 | |
| 182 | rval = __media_device_enum_links(mdev, &links); |
| 183 | if (rval < 0) |
| 184 | return rval; |
| 185 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 186 | if (copy_to_user(ulinks, &links, sizeof(*ulinks))) |
| 187 | return -EFAULT; |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 188 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 192 | static long media_device_setup_link(struct media_device *mdev, |
| 193 | struct media_link_desc __user *_ulink) |
| 194 | { |
| 195 | struct media_link *link = NULL; |
| 196 | struct media_link_desc ulink; |
| 197 | struct media_entity *source; |
| 198 | struct media_entity *sink; |
| 199 | int ret; |
| 200 | |
| 201 | if (copy_from_user(&ulink, _ulink, sizeof(ulink))) |
| 202 | return -EFAULT; |
| 203 | |
| 204 | /* Find the source and sink entities and link. |
| 205 | */ |
| 206 | source = find_entity(mdev, ulink.source.entity); |
| 207 | sink = find_entity(mdev, ulink.sink.entity); |
| 208 | |
| 209 | if (source == NULL || sink == NULL) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | if (ulink.source.index >= source->num_pads || |
| 213 | ulink.sink.index >= sink->num_pads) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | link = media_entity_find_link(&source->pads[ulink.source.index], |
| 217 | &sink->pads[ulink.sink.index]); |
| 218 | if (link == NULL) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | /* Setup the link on both entities. */ |
| 222 | ret = __media_entity_setup_link(link, ulink.flags); |
| 223 | |
| 224 | if (copy_to_user(_ulink, &ulink, sizeof(ulink))) |
| 225 | return -EFAULT; |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 230 | static long media_device_ioctl(struct file *filp, unsigned int cmd, |
| 231 | unsigned long arg) |
| 232 | { |
| 233 | struct media_devnode *devnode = media_devnode_data(filp); |
| 234 | struct media_device *dev = to_media_device(devnode); |
| 235 | long ret; |
| 236 | |
| 237 | switch (cmd) { |
| 238 | case MEDIA_IOC_DEVICE_INFO: |
| 239 | ret = media_device_get_info(dev, |
| 240 | (struct media_device_info __user *)arg); |
| 241 | break; |
| 242 | |
Laurent Pinchart | 1651333 | 2009-12-09 08:40:01 -0300 | [diff] [blame] | 243 | case MEDIA_IOC_ENUM_ENTITIES: |
| 244 | ret = media_device_enum_entities(dev, |
| 245 | (struct media_entity_desc __user *)arg); |
| 246 | break; |
| 247 | |
| 248 | case MEDIA_IOC_ENUM_LINKS: |
| 249 | mutex_lock(&dev->graph_mutex); |
| 250 | ret = media_device_enum_links(dev, |
| 251 | (struct media_links_enum __user *)arg); |
| 252 | mutex_unlock(&dev->graph_mutex); |
| 253 | break; |
| 254 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 255 | case MEDIA_IOC_SETUP_LINK: |
| 256 | mutex_lock(&dev->graph_mutex); |
| 257 | ret = media_device_setup_link(dev, |
| 258 | (struct media_link_desc __user *)arg); |
| 259 | mutex_unlock(&dev->graph_mutex); |
| 260 | break; |
| 261 | |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 262 | default: |
| 263 | ret = -ENOIOCTLCMD; |
| 264 | } |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 269 | #ifdef CONFIG_COMPAT |
| 270 | |
| 271 | struct media_links_enum32 { |
| 272 | __u32 entity; |
| 273 | compat_uptr_t pads; /* struct media_pad_desc * */ |
| 274 | compat_uptr_t links; /* struct media_link_desc * */ |
| 275 | __u32 reserved[4]; |
| 276 | }; |
| 277 | |
| 278 | static long media_device_enum_links32(struct media_device *mdev, |
| 279 | struct media_links_enum32 __user *ulinks) |
| 280 | { |
| 281 | struct media_links_enum links; |
| 282 | compat_uptr_t pads_ptr, links_ptr; |
| 283 | |
| 284 | memset(&links, 0, sizeof(links)); |
| 285 | |
| 286 | if (get_user(links.entity, &ulinks->entity) |
| 287 | || get_user(pads_ptr, &ulinks->pads) |
| 288 | || get_user(links_ptr, &ulinks->links)) |
| 289 | return -EFAULT; |
| 290 | |
| 291 | links.pads = compat_ptr(pads_ptr); |
| 292 | links.links = compat_ptr(links_ptr); |
| 293 | |
| 294 | return __media_device_enum_links(mdev, &links); |
| 295 | } |
| 296 | |
| 297 | #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32) |
| 298 | |
| 299 | static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, |
| 300 | unsigned long arg) |
| 301 | { |
| 302 | struct media_devnode *devnode = media_devnode_data(filp); |
| 303 | struct media_device *dev = to_media_device(devnode); |
| 304 | long ret; |
| 305 | |
| 306 | switch (cmd) { |
| 307 | case MEDIA_IOC_DEVICE_INFO: |
| 308 | case MEDIA_IOC_ENUM_ENTITIES: |
| 309 | case MEDIA_IOC_SETUP_LINK: |
| 310 | return media_device_ioctl(filp, cmd, arg); |
| 311 | |
| 312 | case MEDIA_IOC_ENUM_LINKS32: |
| 313 | mutex_lock(&dev->graph_mutex); |
| 314 | ret = media_device_enum_links32(dev, |
| 315 | (struct media_links_enum32 __user *)arg); |
| 316 | mutex_unlock(&dev->graph_mutex); |
| 317 | break; |
| 318 | |
| 319 | default: |
| 320 | ret = -ENOIOCTLCMD; |
| 321 | } |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | #endif /* CONFIG_COMPAT */ |
| 326 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 327 | static const struct media_file_operations media_device_fops = { |
| 328 | .owner = THIS_MODULE, |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 329 | .open = media_device_open, |
| 330 | .ioctl = media_device_ioctl, |
Sakari Ailus | b0a1f2a | 2013-01-22 12:27:56 -0300 | [diff] [blame] | 331 | #ifdef CONFIG_COMPAT |
| 332 | .compat_ioctl = media_device_compat_ioctl, |
| 333 | #endif /* CONFIG_COMPAT */ |
Laurent Pinchart | 140d881 | 2010-08-18 11:41:22 -0300 | [diff] [blame] | 334 | .release = media_device_close, |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | /* ----------------------------------------------------------------------------- |
| 338 | * sysfs |
| 339 | */ |
| 340 | |
| 341 | static ssize_t show_model(struct device *cd, |
| 342 | struct device_attribute *attr, char *buf) |
| 343 | { |
| 344 | struct media_device *mdev = to_media_device(to_media_devnode(cd)); |
| 345 | |
| 346 | return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); |
| 347 | } |
| 348 | |
| 349 | static DEVICE_ATTR(model, S_IRUGO, show_model, NULL); |
| 350 | |
| 351 | /* ----------------------------------------------------------------------------- |
| 352 | * Registration/unregistration |
| 353 | */ |
| 354 | |
| 355 | static void media_device_release(struct media_devnode *mdev) |
| 356 | { |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * media_device_register - register a media device |
| 361 | * @mdev: The media device |
| 362 | * |
| 363 | * The caller is responsible for initializing the media device before |
| 364 | * registration. The following fields must be set: |
| 365 | * |
| 366 | * - dev must point to the parent device |
| 367 | * - model must be filled with the device model name |
| 368 | */ |
| 369 | int __must_check media_device_register(struct media_device *mdev) |
| 370 | { |
| 371 | int ret; |
| 372 | |
| 373 | if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0)) |
| 374 | return -EINVAL; |
| 375 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 376 | mdev->entity_id = 1; |
| 377 | INIT_LIST_HEAD(&mdev->entities); |
| 378 | spin_lock_init(&mdev->lock); |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 379 | mutex_init(&mdev->graph_mutex); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 380 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 381 | /* Register the device node. */ |
| 382 | mdev->devnode.fops = &media_device_fops; |
| 383 | mdev->devnode.parent = mdev->dev; |
| 384 | mdev->devnode.release = media_device_release; |
| 385 | ret = media_devnode_register(&mdev->devnode); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
| 389 | ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); |
| 390 | if (ret < 0) { |
| 391 | media_devnode_unregister(&mdev->devnode); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(media_device_register); |
| 398 | |
| 399 | /** |
| 400 | * media_device_unregister - unregister a media device |
| 401 | * @mdev: The media device |
| 402 | * |
| 403 | */ |
| 404 | void media_device_unregister(struct media_device *mdev) |
| 405 | { |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 406 | struct media_entity *entity; |
| 407 | struct media_entity *next; |
| 408 | |
| 409 | list_for_each_entry_safe(entity, next, &mdev->entities, list) |
| 410 | media_device_unregister_entity(entity); |
| 411 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 412 | device_remove_file(&mdev->devnode.dev, &dev_attr_model); |
| 413 | media_devnode_unregister(&mdev->devnode); |
| 414 | } |
| 415 | EXPORT_SYMBOL_GPL(media_device_unregister); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * media_device_register_entity - Register an entity with a media device |
| 419 | * @mdev: The media device |
| 420 | * @entity: The entity |
| 421 | */ |
| 422 | int __must_check media_device_register_entity(struct media_device *mdev, |
| 423 | struct media_entity *entity) |
| 424 | { |
| 425 | /* Warn if we apparently re-register an entity */ |
| 426 | WARN_ON(entity->parent != NULL); |
| 427 | entity->parent = mdev; |
| 428 | |
| 429 | spin_lock(&mdev->lock); |
| 430 | if (entity->id == 0) |
| 431 | entity->id = mdev->entity_id++; |
| 432 | else |
| 433 | mdev->entity_id = max(entity->id + 1, mdev->entity_id); |
| 434 | list_add_tail(&entity->list, &mdev->entities); |
| 435 | spin_unlock(&mdev->lock); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | EXPORT_SYMBOL_GPL(media_device_register_entity); |
| 440 | |
| 441 | /** |
| 442 | * media_device_unregister_entity - Unregister an entity |
| 443 | * @entity: The entity |
| 444 | * |
| 445 | * If the entity has never been registered this function will return |
| 446 | * immediately. |
| 447 | */ |
| 448 | void media_device_unregister_entity(struct media_entity *entity) |
| 449 | { |
| 450 | struct media_device *mdev = entity->parent; |
| 451 | |
| 452 | if (mdev == NULL) |
| 453 | return; |
| 454 | |
| 455 | spin_lock(&mdev->lock); |
| 456 | list_del(&entity->list); |
| 457 | spin_unlock(&mdev->lock); |
| 458 | entity->parent = NULL; |
| 459 | } |
| 460 | EXPORT_SYMBOL_GPL(media_device_unregister_entity); |