Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * \file drm_ioctl.h |
| 3 | * IOCTL processing for DRM |
| 4 | * |
| 5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 6 | * \author Gareth Hughes <gareth@valinux.com> |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com |
| 11 | * |
| 12 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 13 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 14 | * All Rights Reserved. |
| 15 | * |
| 16 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 17 | * copy of this software and associated documentation files (the "Software"), |
| 18 | * to deal in the Software without restriction, including without limitation |
| 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 20 | * and/or sell copies of the Software, and to permit persons to whom the |
| 21 | * Software is furnished to do so, subject to the following conditions: |
| 22 | * |
| 23 | * The above copyright notice and this permission notice (including the next |
| 24 | * paragraph) shall be included in all copies or substantial portions of the |
| 25 | * Software. |
| 26 | * |
| 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 30 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 31 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 32 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 33 | * OTHER DEALINGS IN THE SOFTWARE. |
| 34 | */ |
| 35 | |
| 36 | #include "drmP.h" |
| 37 | #include "drm_core.h" |
| 38 | |
| 39 | #include "linux/pci.h" |
| 40 | |
| 41 | /** |
| 42 | * Get the bus id. |
| 43 | * |
| 44 | * \param inode device inode. |
| 45 | * \param filp file pointer. |
| 46 | * \param cmd command. |
| 47 | * \param arg user argument, pointing to a drm_unique structure. |
| 48 | * \return zero on success or a negative number on failure. |
| 49 | * |
| 50 | * Copies the bus id from drm_device::unique into user space. |
| 51 | */ |
| 52 | int drm_getunique(struct inode *inode, struct file *filp, |
| 53 | unsigned int cmd, unsigned long arg) |
| 54 | { |
| 55 | drm_file_t *priv = filp->private_data; |
| 56 | drm_device_t *dev = priv->head->dev; |
| 57 | drm_unique_t __user *argp = (void __user *)arg; |
| 58 | drm_unique_t u; |
| 59 | |
| 60 | if (copy_from_user(&u, argp, sizeof(u))) |
| 61 | return -EFAULT; |
| 62 | if (u.unique_len >= dev->unique_len) { |
| 63 | if (copy_to_user(u.unique, dev->unique, dev->unique_len)) |
| 64 | return -EFAULT; |
| 65 | } |
| 66 | u.unique_len = dev->unique_len; |
| 67 | if (copy_to_user(argp, &u, sizeof(u))) |
| 68 | return -EFAULT; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Set the bus id. |
| 74 | * |
| 75 | * \param inode device inode. |
| 76 | * \param filp file pointer. |
| 77 | * \param cmd command. |
| 78 | * \param arg user argument, pointing to a drm_unique structure. |
| 79 | * \return zero on success or a negative number on failure. |
| 80 | * |
| 81 | * Copies the bus id from userspace into drm_device::unique, and verifies that |
| 82 | * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated |
| 83 | * in interface version 1.1 and will return EBUSY when setversion has requested |
| 84 | * version 1.1 or greater. |
| 85 | */ |
| 86 | int drm_setunique(struct inode *inode, struct file *filp, |
| 87 | unsigned int cmd, unsigned long arg) |
| 88 | { |
| 89 | drm_file_t *priv = filp->private_data; |
| 90 | drm_device_t *dev = priv->head->dev; |
| 91 | drm_unique_t u; |
| 92 | int domain, bus, slot, func, ret; |
| 93 | |
| 94 | if (dev->unique_len || dev->unique) return -EBUSY; |
| 95 | |
| 96 | if (copy_from_user(&u, (drm_unique_t __user *)arg, sizeof(u))) |
| 97 | return -EFAULT; |
| 98 | |
| 99 | if (!u.unique_len || u.unique_len > 1024) return -EINVAL; |
| 100 | |
| 101 | dev->unique_len = u.unique_len; |
| 102 | dev->unique = drm_alloc(u.unique_len + 1, DRM_MEM_DRIVER); |
| 103 | if(!dev->unique) return -ENOMEM; |
| 104 | if (copy_from_user(dev->unique, u.unique, dev->unique_len)) |
| 105 | return -EFAULT; |
| 106 | |
| 107 | dev->unique[dev->unique_len] = '\0'; |
| 108 | |
| 109 | dev->devname = drm_alloc(strlen(dev->driver->pci_driver.name) + strlen(dev->unique) + 2, |
| 110 | DRM_MEM_DRIVER); |
| 111 | if (!dev->devname) |
| 112 | return -ENOMEM; |
| 113 | |
| 114 | sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name, dev->unique); |
| 115 | |
| 116 | /* Return error if the busid submitted doesn't match the device's actual |
| 117 | * busid. |
| 118 | */ |
| 119 | ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func); |
| 120 | if (ret != 3) |
| 121 | return DRM_ERR(EINVAL); |
| 122 | domain = bus >> 8; |
| 123 | bus &= 0xff; |
| 124 | |
| 125 | if ((domain != dev->pci_domain) || |
| 126 | (bus != dev->pci_bus) || |
| 127 | (slot != dev->pci_slot) || |
| 128 | (func != dev->pci_func)) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int |
| 135 | drm_set_busid(drm_device_t *dev) |
| 136 | { |
| 137 | if (dev->unique != NULL) |
| 138 | return EBUSY; |
| 139 | |
| 140 | dev->unique_len = 20; |
| 141 | dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER); |
| 142 | if (dev->unique == NULL) |
| 143 | return ENOMEM; |
| 144 | |
| 145 | snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d", |
| 146 | dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func); |
| 147 | |
| 148 | dev->devname = drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len + 2, |
| 149 | DRM_MEM_DRIVER); |
| 150 | if (dev->devname == NULL) |
| 151 | return ENOMEM; |
| 152 | |
| 153 | sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name, dev->unique); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Get a mapping information. |
| 161 | * |
| 162 | * \param inode device inode. |
| 163 | * \param filp file pointer. |
| 164 | * \param cmd command. |
| 165 | * \param arg user argument, pointing to a drm_map structure. |
| 166 | * |
| 167 | * \return zero on success or a negative number on failure. |
| 168 | * |
| 169 | * Searches for the mapping with the specified offset and copies its information |
| 170 | * into userspace |
| 171 | */ |
| 172 | int drm_getmap( struct inode *inode, struct file *filp, |
| 173 | unsigned int cmd, unsigned long arg ) |
| 174 | { |
| 175 | drm_file_t *priv = filp->private_data; |
| 176 | drm_device_t *dev = priv->head->dev; |
| 177 | drm_map_t __user *argp = (void __user *)arg; |
| 178 | drm_map_t map; |
| 179 | drm_map_list_t *r_list = NULL; |
| 180 | struct list_head *list; |
| 181 | int idx; |
| 182 | int i; |
| 183 | |
| 184 | if (copy_from_user(&map, argp, sizeof(map))) |
| 185 | return -EFAULT; |
| 186 | idx = map.offset; |
| 187 | |
| 188 | down(&dev->struct_sem); |
| 189 | if (idx < 0) { |
| 190 | up(&dev->struct_sem); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | i = 0; |
| 195 | list_for_each(list, &dev->maplist->head) { |
| 196 | if(i == idx) { |
| 197 | r_list = list_entry(list, drm_map_list_t, head); |
| 198 | break; |
| 199 | } |
| 200 | i++; |
| 201 | } |
| 202 | if(!r_list || !r_list->map) { |
| 203 | up(&dev->struct_sem); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | map.offset = r_list->map->offset; |
| 208 | map.size = r_list->map->size; |
| 209 | map.type = r_list->map->type; |
| 210 | map.flags = r_list->map->flags; |
| 211 | map.handle = r_list->map->handle; |
| 212 | map.mtrr = r_list->map->mtrr; |
| 213 | up(&dev->struct_sem); |
| 214 | |
| 215 | if (copy_to_user(argp, &map, sizeof(map))) return -EFAULT; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get client information. |
| 221 | * |
| 222 | * \param inode device inode. |
| 223 | * \param filp file pointer. |
| 224 | * \param cmd command. |
| 225 | * \param arg user argument, pointing to a drm_client structure. |
| 226 | * |
| 227 | * \return zero on success or a negative number on failure. |
| 228 | * |
| 229 | * Searches for the client with the specified index and copies its information |
| 230 | * into userspace |
| 231 | */ |
| 232 | int drm_getclient( struct inode *inode, struct file *filp, |
| 233 | unsigned int cmd, unsigned long arg ) |
| 234 | { |
| 235 | drm_file_t *priv = filp->private_data; |
| 236 | drm_device_t *dev = priv->head->dev; |
| 237 | drm_client_t __user *argp = (void __user *)arg; |
| 238 | drm_client_t client; |
| 239 | drm_file_t *pt; |
| 240 | int idx; |
| 241 | int i; |
| 242 | |
| 243 | if (copy_from_user(&client, argp, sizeof(client))) |
| 244 | return -EFAULT; |
| 245 | idx = client.idx; |
| 246 | down(&dev->struct_sem); |
| 247 | for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next) |
| 248 | ; |
| 249 | |
| 250 | if (!pt) { |
| 251 | up(&dev->struct_sem); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | client.auth = pt->authenticated; |
| 255 | client.pid = pt->pid; |
| 256 | client.uid = pt->uid; |
| 257 | client.magic = pt->magic; |
| 258 | client.iocs = pt->ioctl_count; |
| 259 | up(&dev->struct_sem); |
| 260 | |
| 261 | if (copy_to_user((drm_client_t __user *)arg, &client, sizeof(client))) |
| 262 | return -EFAULT; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get statistics information. |
| 268 | * |
| 269 | * \param inode device inode. |
| 270 | * \param filp file pointer. |
| 271 | * \param cmd command. |
| 272 | * \param arg user argument, pointing to a drm_stats structure. |
| 273 | * |
| 274 | * \return zero on success or a negative number on failure. |
| 275 | */ |
| 276 | int drm_getstats( struct inode *inode, struct file *filp, |
| 277 | unsigned int cmd, unsigned long arg ) |
| 278 | { |
| 279 | drm_file_t *priv = filp->private_data; |
| 280 | drm_device_t *dev = priv->head->dev; |
| 281 | drm_stats_t stats; |
| 282 | int i; |
| 283 | |
| 284 | memset(&stats, 0, sizeof(stats)); |
| 285 | |
| 286 | down(&dev->struct_sem); |
| 287 | |
| 288 | for (i = 0; i < dev->counters; i++) { |
| 289 | if (dev->types[i] == _DRM_STAT_LOCK) |
| 290 | stats.data[i].value |
| 291 | = (dev->lock.hw_lock |
| 292 | ? dev->lock.hw_lock->lock : 0); |
| 293 | else |
| 294 | stats.data[i].value = atomic_read(&dev->counts[i]); |
| 295 | stats.data[i].type = dev->types[i]; |
| 296 | } |
| 297 | |
| 298 | stats.count = dev->counters; |
| 299 | |
| 300 | up(&dev->struct_sem); |
| 301 | |
| 302 | if (copy_to_user((drm_stats_t __user *)arg, &stats, sizeof(stats))) |
| 303 | return -EFAULT; |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Setversion ioctl. |
| 309 | * |
| 310 | * \param inode device inode. |
| 311 | * \param filp file pointer. |
| 312 | * \param cmd command. |
| 313 | * \param arg user argument, pointing to a drm_lock structure. |
| 314 | * \return zero on success or negative number on failure. |
| 315 | * |
| 316 | * Sets the requested interface version |
| 317 | */ |
| 318 | int drm_setversion(DRM_IOCTL_ARGS) |
| 319 | { |
| 320 | DRM_DEVICE; |
| 321 | drm_set_version_t sv; |
| 322 | drm_set_version_t retv; |
| 323 | int if_version; |
| 324 | drm_set_version_t __user *argp = (void __user *)data; |
| 325 | drm_version_t version; |
| 326 | |
| 327 | DRM_COPY_FROM_USER_IOCTL(sv, argp, sizeof(sv)); |
| 328 | |
| 329 | memset(&version, 0, sizeof(version)); |
| 330 | |
| 331 | dev->driver->version(&version); |
| 332 | retv.drm_di_major = DRM_IF_MAJOR; |
| 333 | retv.drm_di_minor = DRM_IF_MINOR; |
| 334 | retv.drm_dd_major = version.version_major; |
| 335 | retv.drm_dd_minor = version.version_minor; |
| 336 | |
| 337 | DRM_COPY_TO_USER_IOCTL(argp, retv, sizeof(sv)); |
| 338 | |
| 339 | if (sv.drm_di_major != -1) { |
| 340 | if (sv.drm_di_major != DRM_IF_MAJOR || |
| 341 | sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR) |
| 342 | return EINVAL; |
| 343 | if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor); |
| 344 | dev->if_version = DRM_MAX(if_version, dev->if_version); |
| 345 | if (sv.drm_di_minor >= 1) { |
| 346 | /* |
| 347 | * Version 1.1 includes tying of DRM to specific device |
| 348 | */ |
| 349 | drm_set_busid(dev); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if (sv.drm_dd_major != -1) { |
| 354 | if (sv.drm_dd_major != version.version_major || |
| 355 | sv.drm_dd_minor < 0 || sv.drm_dd_minor > version.version_minor) |
| 356 | return EINVAL; |
| 357 | |
| 358 | if (dev->driver->set_version) |
| 359 | dev->driver->set_version(dev, &sv); |
| 360 | } |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /** No-op ioctl. */ |
| 365 | int drm_noop(struct inode *inode, struct file *filp, unsigned int cmd, |
| 366 | unsigned long arg) |
| 367 | { |
| 368 | DRM_DEBUG("\n"); |
| 369 | return 0; |
| 370 | } |