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