]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/drm/drm_ioctl.c
drm: Replace filp in ioctl arguments with drm_file *file_priv.
[linux-2.6-omap-h63xx.git] / drivers / char / drm / drm_ioctl.c
1 /**
2  * \file drm_ioctl.c
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 file_priv DRM file private.
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 drm_file *file_priv,
53                   unsigned int cmd, unsigned long arg)
54 {
55         struct drm_device *dev = file_priv->head->dev;
56         struct drm_unique __user *argp = (void __user *)arg;
57         struct drm_unique u;
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.
73  *
74  * \param inode device inode.
75  * \param file_priv DRM file private.
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  */
85 int drm_setunique(struct inode *inode, struct drm_file *file_priv,
86                   unsigned int cmd, unsigned long arg)
87 {
88         struct drm_device *dev = file_priv->head->dev;
89         struct drm_unique u;
90         int domain, bus, slot, func, ret;
91
92         if (dev->unique_len || dev->unique)
93                 return -EBUSY;
94
95         if (copy_from_user(&u, (struct drm_unique __user *) arg, sizeof(u)))
96                 return -EFAULT;
97
98         if (!u.unique_len || u.unique_len > 1024)
99                 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)
104                 return -ENOMEM;
105         if (copy_from_user(dev->unique, u.unique, dev->unique_len))
106                 return -EFAULT;
107
108         dev->unique[dev->unique_len] = '\0';
109
110         dev->devname =
111             drm_alloc(strlen(dev->driver->pci_driver.name) +
112                       strlen(dev->unique) + 2, DRM_MEM_DRIVER);
113         if (!dev->devname)
114                 return -ENOMEM;
115
116         sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
117                 dev->unique);
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)
124                 return -EINVAL;
125         domain = bus >> 8;
126         bus &= 0xff;
127
128         if ((domain != drm_get_pci_domain(dev)) ||
129             (bus != dev->pdev->bus->number) ||
130             (slot != PCI_SLOT(dev->pdev->devfn)) ||
131             (func != PCI_FUNC(dev->pdev->devfn)))
132                 return -EINVAL;
133
134         return 0;
135 }
136
137 static int drm_set_busid(struct drm_device * dev)
138 {
139         int len;
140
141         if (dev->unique != NULL)
142                 return 0;
143
144         dev->unique_len = 40;
145         dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
146         if (dev->unique == NULL)
147                 return -ENOMEM;
148
149         len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
150                        drm_get_pci_domain(dev), dev->pdev->bus->number,
151                        PCI_SLOT(dev->pdev->devfn),
152                        PCI_FUNC(dev->pdev->devfn));
153
154         if (len > dev->unique_len)
155                 DRM_ERROR("Unique buffer overflowed\n");
156
157         dev->devname =
158             drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
159                       2, DRM_MEM_DRIVER);
160         if (dev->devname == NULL)
161                 return -ENOMEM;
162
163         sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
164                 dev->unique);
165
166         return 0;
167 }
168
169 /**
170  * Get a mapping information.
171  *
172  * \param inode device inode.
173  * \param file_priv DRM file private.
174  * \param cmd command.
175  * \param arg user argument, pointing to a drm_map structure.
176  *
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  */
182 int drm_getmap(struct inode *inode, struct drm_file *file_priv,
183                unsigned int cmd, unsigned long arg)
184 {
185         struct drm_device *dev = file_priv->head->dev;
186         struct drm_map __user *argp = (void __user *)arg;
187         struct drm_map map;
188         struct drm_map_list *r_list = NULL;
189         struct list_head *list;
190         int idx;
191         int i;
192
193         if (copy_from_user(&map, argp, sizeof(map)))
194                 return -EFAULT;
195         idx = map.offset;
196
197         mutex_lock(&dev->struct_mutex);
198         if (idx < 0) {
199                 mutex_unlock(&dev->struct_mutex);
200                 return -EINVAL;
201         }
202
203         i = 0;
204         list_for_each(list, &dev->maplist) {
205                 if (i == idx) {
206                         r_list = list_entry(list, struct drm_map_list, head);
207                         break;
208                 }
209                 i++;
210         }
211         if (!r_list || !r_list->map) {
212                 mutex_unlock(&dev->struct_mutex);
213                 return -EINVAL;
214         }
215
216         map.offset = r_list->map->offset;
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;
222         mutex_unlock(&dev->struct_mutex);
223
224         if (copy_to_user(argp, &map, sizeof(map)))
225                 return -EFAULT;
226         return 0;
227 }
228
229 /**
230  * Get client information.
231  *
232  * \param inode device inode.
233  * \param file_priv DRM file private.
234  * \param cmd command.
235  * \param arg user argument, pointing to a drm_client structure.
236  *
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  */
242 int drm_getclient(struct inode *inode, struct drm_file *file_priv,
243                   unsigned int cmd, unsigned long arg)
244 {
245         struct drm_device *dev = file_priv->head->dev;
246         struct drm_client __user *argp = (struct drm_client __user *)arg;
247         struct drm_client client;
248         struct drm_file *pt;
249         int idx;
250         int i;
251
252         if (copy_from_user(&client, argp, sizeof(client)))
253                 return -EFAULT;
254         idx = client.idx;
255         mutex_lock(&dev->struct_mutex);
256         
257         if (list_empty(&dev->filelist)) {
258                 mutex_unlock(&dev->struct_mutex);
259                 return -EINVAL;
260         }
261
262         i = 0;
263         list_for_each_entry(pt, &dev->filelist, lhead) {
264                 if (i++ >= idx)
265                         break;
266         }
267
268         client.auth = pt->authenticated;
269         client.pid = pt->pid;
270         client.uid = pt->uid;
271         client.magic = pt->magic;
272         client.iocs = pt->ioctl_count;
273         mutex_unlock(&dev->struct_mutex);
274
275         if (copy_to_user(argp, &client, sizeof(client)))
276                 return -EFAULT;
277         return 0;
278 }
279
280 /**
281  * Get statistics information.
282  *
283  * \param inode device inode.
284  * \param file_priv DRM file private.
285  * \param cmd command.
286  * \param arg user argument, pointing to a drm_stats structure.
287  *
288  * \return zero on success or a negative number on failure.
289  */
290 int drm_getstats(struct inode *inode, struct drm_file *file_priv,
291                  unsigned int cmd, unsigned long arg)
292 {
293         struct drm_device *dev = file_priv->head->dev;
294         struct drm_stats stats;
295         int i;
296
297         memset(&stats, 0, sizeof(stats));
298
299         mutex_lock(&dev->struct_mutex);
300
301         for (i = 0; i < dev->counters; i++) {
302                 if (dev->types[i] == _DRM_STAT_LOCK)
303                         stats.data[i].value
304                             = (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
305                 else
306                         stats.data[i].value = atomic_read(&dev->counts[i]);
307                 stats.data[i].type = dev->types[i];
308         }
309
310         stats.count = dev->counters;
311
312         mutex_unlock(&dev->struct_mutex);
313
314         if (copy_to_user((struct drm_stats __user *) arg, &stats, sizeof(stats)))
315                 return -EFAULT;
316         return 0;
317 }
318
319 /**
320  * Setversion ioctl.
321  *
322  * \param inode device inode.
323  * \param file_priv DRM file private.
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  */
330 int drm_setversion(DRM_IOCTL_ARGS)
331 {
332         DRM_DEVICE;
333         struct drm_set_version sv;
334         struct drm_set_version retv;
335         int if_version;
336         struct drm_set_version __user *argp = (void __user *)data;
337         int ret;
338
339         if (copy_from_user(&sv, argp, sizeof(sv)))
340                 return -EFAULT;
341
342         retv.drm_di_major = DRM_IF_MAJOR;
343         retv.drm_di_minor = DRM_IF_MINOR;
344         retv.drm_dd_major = dev->driver->major;
345         retv.drm_dd_minor = dev->driver->minor;
346
347         if (copy_to_user(argp, &retv, sizeof(retv)))
348                 return -EFAULT;
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)
353                         return -EINVAL;
354                 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_di_minor);
355                 dev->if_version = max(if_version, dev->if_version);
356                 if (sv.drm_di_minor >= 1) {
357                         /*
358                          * Version 1.1 includes tying of DRM to specific device
359                          */
360                         ret = drm_set_busid(dev);
361                         if (ret)
362                                 return ret;
363                 }
364         }
365
366         if (sv.drm_dd_major != -1) {
367                 if (sv.drm_dd_major != dev->driver->major ||
368                     sv.drm_dd_minor < 0
369                     || sv.drm_dd_minor > dev->driver->minor)
370                         return -EINVAL;
371
372                 if (dev->driver->set_version)
373                         dev->driver->set_version(dev, &sv);
374         }
375         return 0;
376 }
377
378 /** No-op ioctl. */
379 int drm_noop(struct inode *inode, struct drm_file *file_priv, unsigned int cmd,
380              unsigned long arg)
381 {
382         DRM_DEBUG("\n");
383         return 0;
384 }