drm: remove drm_file_t, drm_device_t and drm_head_t typedefs
[deliverable/linux.git] / drivers / char / drm / drm_ioctl.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_ioctl.c
1da177e4
LT
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.
b5e89ed5 43 *
1da177e4
LT
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 */
52int drm_getunique(struct inode *inode, struct file *filp,
b5e89ed5 53 unsigned int cmd, unsigned long arg)
1da177e4 54{
84b1fd10
DA
55 struct drm_file *priv = filp->private_data;
56 struct drm_device *dev = priv->head->dev;
c60ce623
DA
57 struct drm_unique __user *argp = (void __user *)arg;
58 struct drm_unique u;
1da177e4
LT
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.
b5e89ed5 74 *
1da177e4
LT
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 */
86int drm_setunique(struct inode *inode, struct file *filp,
b5e89ed5 87 unsigned int cmd, unsigned long arg)
1da177e4 88{
84b1fd10
DA
89 struct drm_file *priv = filp->private_data;
90 struct drm_device *dev = priv->head->dev;
c60ce623 91 struct drm_unique u;
b5e89ed5 92 int domain, bus, slot, func, ret;
1da177e4 93
b5e89ed5
DA
94 if (dev->unique_len || dev->unique)
95 return -EBUSY;
1da177e4 96
c60ce623 97 if (copy_from_user(&u, (struct drm_unique __user *) arg, sizeof(u)))
1da177e4
LT
98 return -EFAULT;
99
b5e89ed5
DA
100 if (!u.unique_len || u.unique_len > 1024)
101 return -EINVAL;
1da177e4
LT
102
103 dev->unique_len = u.unique_len;
b5e89ed5
DA
104 dev->unique = drm_alloc(u.unique_len + 1, DRM_MEM_DRIVER);
105 if (!dev->unique)
106 return -ENOMEM;
1da177e4
LT
107 if (copy_from_user(dev->unique, u.unique, dev->unique_len))
108 return -EFAULT;
109
110 dev->unique[dev->unique_len] = '\0';
111
b5e89ed5
DA
112 dev->devname =
113 drm_alloc(strlen(dev->driver->pci_driver.name) +
114 strlen(dev->unique) + 2, DRM_MEM_DRIVER);
1da177e4
LT
115 if (!dev->devname)
116 return -ENOMEM;
117
b5e89ed5
DA
118 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
119 dev->unique);
1da177e4
LT
120
121 /* Return error if the busid submitted doesn't match the device's actual
122 * busid.
123 */
124 ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
125 if (ret != 3)
126 return DRM_ERR(EINVAL);
127 domain = bus >> 8;
128 bus &= 0xff;
b5e89ed5 129
33229601 130 if ((domain != drm_get_pci_domain(dev)) ||
242ef0e1
D
131 (bus != dev->pdev->bus->number) ||
132 (slot != PCI_SLOT(dev->pdev->devfn)) ||
133 (func != PCI_FUNC(dev->pdev->devfn)))
1da177e4
LT
134 return -EINVAL;
135
136 return 0;
137}
138
84b1fd10 139static int drm_set_busid(struct drm_device * dev)
1da177e4 140{
fe34765b
DA
141 int len;
142
1da177e4 143 if (dev->unique != NULL)
1f27ce6a 144 return 0;
1da177e4 145
fe34765b 146 dev->unique_len = 40;
1da177e4
LT
147 dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
148 if (dev->unique == NULL)
1f27ce6a 149 return -ENOMEM;
1da177e4 150
fe34765b 151 len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
33229601 152 drm_get_pci_domain(dev), dev->pdev->bus->number,
242ef0e1
D
153 PCI_SLOT(dev->pdev->devfn),
154 PCI_FUNC(dev->pdev->devfn));
1da177e4 155
fe34765b
DA
156 if (len > dev->unique_len)
157 DRM_ERROR("Unique buffer overflowed\n");
158
b5e89ed5
DA
159 dev->devname =
160 drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
161 2, DRM_MEM_DRIVER);
1da177e4 162 if (dev->devname == NULL)
1f27ce6a 163 return -ENOMEM;
1da177e4 164
b5e89ed5
DA
165 sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
166 dev->unique);
1da177e4
LT
167
168 return 0;
169}
170
1da177e4
LT
171/**
172 * Get a mapping information.
173 *
174 * \param inode device inode.
175 * \param filp file pointer.
176 * \param cmd command.
177 * \param arg user argument, pointing to a drm_map structure.
b5e89ed5 178 *
1da177e4
LT
179 * \return zero on success or a negative number on failure.
180 *
181 * Searches for the mapping with the specified offset and copies its information
182 * into userspace
183 */
b5e89ed5
DA
184int drm_getmap(struct inode *inode, struct file *filp,
185 unsigned int cmd, unsigned long arg)
1da177e4 186{
84b1fd10
DA
187 struct drm_file *priv = filp->private_data;
188 struct drm_device *dev = priv->head->dev;
c60ce623
DA
189 struct drm_map __user *argp = (void __user *)arg;
190 struct drm_map map;
1da177e4
LT
191 drm_map_list_t *r_list = NULL;
192 struct list_head *list;
b5e89ed5
DA
193 int idx;
194 int i;
1da177e4
LT
195
196 if (copy_from_user(&map, argp, sizeof(map)))
197 return -EFAULT;
198 idx = map.offset;
199
30e2fb18 200 mutex_lock(&dev->struct_mutex);
1da177e4 201 if (idx < 0) {
30e2fb18 202 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
203 return -EINVAL;
204 }
205
206 i = 0;
bd1b331f 207 list_for_each(list, &dev->maplist) {
b5e89ed5 208 if (i == idx) {
1da177e4
LT
209 r_list = list_entry(list, drm_map_list_t, head);
210 break;
211 }
212 i++;
213 }
b5e89ed5 214 if (!r_list || !r_list->map) {
30e2fb18 215 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
216 return -EINVAL;
217 }
218
219 map.offset = r_list->map->offset;
b5e89ed5
DA
220 map.size = r_list->map->size;
221 map.type = r_list->map->type;
222 map.flags = r_list->map->flags;
223 map.handle = (void *)(unsigned long)r_list->user_token;
224 map.mtrr = r_list->map->mtrr;
30e2fb18 225 mutex_unlock(&dev->struct_mutex);
1da177e4 226
b5e89ed5
DA
227 if (copy_to_user(argp, &map, sizeof(map)))
228 return -EFAULT;
1da177e4
LT
229 return 0;
230}
231
232/**
233 * Get client information.
234 *
235 * \param inode device inode.
236 * \param filp file pointer.
237 * \param cmd command.
238 * \param arg user argument, pointing to a drm_client structure.
b5e89ed5 239 *
1da177e4
LT
240 * \return zero on success or a negative number on failure.
241 *
242 * Searches for the client with the specified index and copies its information
243 * into userspace
244 */
b5e89ed5
DA
245int drm_getclient(struct inode *inode, struct file *filp,
246 unsigned int cmd, unsigned long arg)
1da177e4 247{
84b1fd10
DA
248 struct drm_file *priv = filp->private_data;
249 struct drm_device *dev = priv->head->dev;
c60ce623
DA
250 struct drm_client __user *argp = (struct drm_client __user *)arg;
251 struct drm_client client;
84b1fd10 252 struct drm_file *pt;
b5e89ed5
DA
253 int idx;
254 int i;
1da177e4
LT
255
256 if (copy_from_user(&client, argp, sizeof(client)))
257 return -EFAULT;
258 idx = client.idx;
30e2fb18 259 mutex_lock(&dev->struct_mutex);
bd1b331f
DA
260
261 if (list_empty(&dev->filelist)) {
30e2fb18 262 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
263 return -EINVAL;
264 }
bd1b331f
DA
265
266 i = 0;
267 list_for_each_entry(pt, &dev->filelist, lhead) {
268 if (i++ >= idx)
269 break;
270 }
271
b5e89ed5
DA
272 client.auth = pt->authenticated;
273 client.pid = pt->pid;
274 client.uid = pt->uid;
1da177e4 275 client.magic = pt->magic;
b5e89ed5 276 client.iocs = pt->ioctl_count;
30e2fb18 277 mutex_unlock(&dev->struct_mutex);
1da177e4 278
fe34765b 279 if (copy_to_user(argp, &client, sizeof(client)))
1da177e4
LT
280 return -EFAULT;
281 return 0;
282}
283
b5e89ed5
DA
284/**
285 * Get statistics information.
286 *
1da177e4
LT
287 * \param inode device inode.
288 * \param filp file pointer.
289 * \param cmd command.
290 * \param arg user argument, pointing to a drm_stats structure.
b5e89ed5 291 *
1da177e4
LT
292 * \return zero on success or a negative number on failure.
293 */
b5e89ed5
DA
294int drm_getstats(struct inode *inode, struct file *filp,
295 unsigned int cmd, unsigned long arg)
1da177e4 296{
84b1fd10
DA
297 struct drm_file *priv = filp->private_data;
298 struct drm_device *dev = priv->head->dev;
c60ce623 299 struct drm_stats stats;
b5e89ed5 300 int i;
1da177e4
LT
301
302 memset(&stats, 0, sizeof(stats));
b5e89ed5 303
30e2fb18 304 mutex_lock(&dev->struct_mutex);
1da177e4
LT
305
306 for (i = 0; i < dev->counters; i++) {
307 if (dev->types[i] == _DRM_STAT_LOCK)
308 stats.data[i].value
b5e89ed5
DA
309 = (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
310 else
1da177e4 311 stats.data[i].value = atomic_read(&dev->counts[i]);
b5e89ed5 312 stats.data[i].type = dev->types[i];
1da177e4 313 }
b5e89ed5 314
1da177e4
LT
315 stats.count = dev->counters;
316
30e2fb18 317 mutex_unlock(&dev->struct_mutex);
1da177e4 318
c60ce623 319 if (copy_to_user((struct drm_stats __user *) arg, &stats, sizeof(stats)))
1da177e4
LT
320 return -EFAULT;
321 return 0;
322}
323
324/**
325 * Setversion ioctl.
326 *
327 * \param inode device inode.
328 * \param filp file pointer.
329 * \param cmd command.
330 * \param arg user argument, pointing to a drm_lock structure.
331 * \return zero on success or negative number on failure.
332 *
333 * Sets the requested interface version
334 */
335int drm_setversion(DRM_IOCTL_ARGS)
336{
337 DRM_DEVICE;
c60ce623
DA
338 struct drm_set_version sv;
339 struct drm_set_version retv;
1da177e4 340 int if_version;
c60ce623 341 struct drm_set_version __user *argp = (void __user *)data;
1f27ce6a 342 int ret;
1da177e4 343
3d77461e
DA
344 if (copy_from_user(&sv, argp, sizeof(sv)))
345 return -EFAULT;
1da177e4 346
1da177e4
LT
347 retv.drm_di_major = DRM_IF_MAJOR;
348 retv.drm_di_minor = DRM_IF_MINOR;
22eae947
DA
349 retv.drm_dd_major = dev->driver->major;
350 retv.drm_dd_minor = dev->driver->minor;
1da177e4 351
1f27ce6a 352 if (copy_to_user(argp, &retv, sizeof(retv)))
3d77461e 353 return -EFAULT;
1da177e4
LT
354
355 if (sv.drm_di_major != -1) {
356 if (sv.drm_di_major != DRM_IF_MAJOR ||
357 sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
1f27ce6a 358 return -EINVAL;
fe34765b 359 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_di_minor);
3d77461e 360 dev->if_version = max(if_version, dev->if_version);
1da177e4
LT
361 if (sv.drm_di_minor >= 1) {
362 /*
363 * Version 1.1 includes tying of DRM to specific device
364 */
1f27ce6a
DA
365 ret = drm_set_busid(dev);
366 if (ret)
367 return ret;
1da177e4
LT
368 }
369 }
370
371 if (sv.drm_dd_major != -1) {
22eae947 372 if (sv.drm_dd_major != dev->driver->major ||
b5e89ed5 373 sv.drm_dd_minor < 0
22eae947 374 || sv.drm_dd_minor > dev->driver->minor)
1f27ce6a 375 return -EINVAL;
1da177e4
LT
376
377 if (dev->driver->set_version)
378 dev->driver->set_version(dev, &sv);
379 }
380 return 0;
381}
382
383/** No-op ioctl. */
384int drm_noop(struct inode *inode, struct file *filp, unsigned int cmd,
b5e89ed5 385 unsigned long arg)
1da177e4
LT
386{
387 DRM_DEBUG("\n");
388 return 0;
389}
This page took 0.192663 seconds and 5 git commands to generate.