vserver 1.9.3
[linux-2.6.git] / drivers / char / drm / drm_ioctl.h
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
38 #include "linux/pci.h"
39
40 /**
41  * Get the bus id.
42  * 
43  * \param inode device inode.
44  * \param filp file pointer.
45  * \param cmd command.
46  * \param arg user argument, pointing to a drm_unique structure.
47  * \return zero on success or a negative number on failure.
48  *
49  * Copies the bus id from drm_device::unique into user space.
50  */
51 int DRM(getunique)(struct inode *inode, struct file *filp,
52                    unsigned int cmd, unsigned long arg)
53 {
54         drm_file_t       *priv   = filp->private_data;
55         drm_device_t     *dev    = priv->dev;
56         drm_unique_t     __user *argp = (void __user *)arg;
57         drm_unique_t     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 filp file pointer.
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 file *filp,
86                    unsigned int cmd, unsigned long arg)
87 {
88         drm_file_t       *priv   = filp->private_data;
89         drm_device_t     *dev    = priv->dev;
90         drm_unique_t     u;
91         int              domain, bus, slot, func, ret;
92
93         if (dev->unique_len || dev->unique) return -EBUSY;
94
95         if (copy_from_user(&u, (drm_unique_t __user *)arg, sizeof(u)))
96                 return -EFAULT;
97
98         if (!u.unique_len || u.unique_len > 1024) return -EINVAL;
99
100         dev->unique_len = u.unique_len;
101         dev->unique     = DRM(alloc)(u.unique_len + 1, DRM_MEM_DRIVER);
102         if(!dev->unique) return -ENOMEM;
103         if (copy_from_user(dev->unique, u.unique, dev->unique_len))
104                 return -EFAULT;
105
106         dev->unique[dev->unique_len] = '\0';
107
108         dev->devname = DRM(alloc)(strlen(dev->name) + strlen(dev->unique) + 2,
109                                   DRM_MEM_DRIVER);
110         if (!dev->devname)
111                 return -ENOMEM;
112
113         sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
114
115         /* Return error if the busid submitted doesn't match the device's actual
116          * busid.
117          */
118         ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
119         if (ret != 3)
120                 return DRM_ERR(EINVAL);
121         domain = bus >> 8;
122         bus &= 0xff;
123         
124         if ((domain != dev->pci_domain) ||
125             (bus != dev->pci_bus) ||
126             (slot != dev->pci_slot) ||
127             (func != dev->pci_func))
128                 return -EINVAL;
129
130         return 0;
131 }
132
133 static int
134 DRM(set_busid)(drm_device_t *dev)
135 {
136         if (dev->unique != NULL)
137                 return EBUSY;
138
139         dev->unique_len = 20;
140         dev->unique = DRM(alloc)(dev->unique_len + 1, DRM_MEM_DRIVER);
141         if (dev->unique == NULL)
142                 return ENOMEM;
143
144         snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
145                 dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
146
147         dev->devname = DRM(alloc)(strlen(dev->name) + dev->unique_len + 2,
148                                 DRM_MEM_DRIVER);
149         if (dev->devname == NULL)
150                 return ENOMEM;
151
152         sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
153
154         return 0;
155 }
156
157
158 /**
159  * Get a mapping information.
160  *
161  * \param inode device inode.
162  * \param filp file pointer.
163  * \param cmd command.
164  * \param arg user argument, pointing to a drm_map structure.
165  * 
166  * \return zero on success or a negative number on failure.
167  *
168  * Searches for the mapping with the specified offset and copies its information
169  * into userspace
170  */
171 int DRM(getmap)( struct inode *inode, struct file *filp,
172                  unsigned int cmd, unsigned long arg )
173 {
174         drm_file_t   *priv = filp->private_data;
175         drm_device_t *dev  = priv->dev;
176         drm_map_t    __user *argp = (void __user *)arg;
177         drm_map_t    map;
178         drm_map_list_t *r_list = NULL;
179         struct list_head *list;
180         int          idx;
181         int          i;
182
183         if (copy_from_user(&map, argp, sizeof(map)))
184                 return -EFAULT;
185         idx = map.offset;
186
187         down(&dev->struct_sem);
188         if (idx < 0) {
189                 up(&dev->struct_sem);
190                 return -EINVAL;
191         }
192
193         i = 0;
194         list_for_each(list, &dev->maplist->head) {
195                 if(i == idx) {
196                         r_list = list_entry(list, drm_map_list_t, head);
197                         break;
198                 }
199                 i++;
200         }
201         if(!r_list || !r_list->map) {
202                 up(&dev->struct_sem);
203                 return -EINVAL;
204         }
205
206         map.offset = r_list->map->offset;
207         map.size   = r_list->map->size;
208         map.type   = r_list->map->type;
209         map.flags  = r_list->map->flags;
210         map.handle = r_list->map->handle;
211         map.mtrr   = r_list->map->mtrr;
212         up(&dev->struct_sem);
213
214         if (copy_to_user(argp, &map, sizeof(map))) return -EFAULT;
215         return 0;
216 }
217
218 /**
219  * Get client information.
220  *
221  * \param inode device inode.
222  * \param filp file pointer.
223  * \param cmd command.
224  * \param arg user argument, pointing to a drm_client structure.
225  * 
226  * \return zero on success or a negative number on failure.
227  *
228  * Searches for the client with the specified index and copies its information
229  * into userspace
230  */
231 int DRM(getclient)( struct inode *inode, struct file *filp,
232                     unsigned int cmd, unsigned long arg )
233 {
234         drm_file_t   *priv = filp->private_data;
235         drm_device_t *dev  = priv->dev;
236         drm_client_t __user *argp = (void __user *)arg;
237         drm_client_t client;
238         drm_file_t   *pt;
239         int          idx;
240         int          i;
241
242         if (copy_from_user(&client, argp, sizeof(client)))
243                 return -EFAULT;
244         idx = client.idx;
245         down(&dev->struct_sem);
246         for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next)
247                 ;
248
249         if (!pt) {
250                 up(&dev->struct_sem);
251                 return -EINVAL;
252         }
253         client.auth  = pt->authenticated;
254         client.pid   = pt->pid;
255         client.uid   = pt->uid;
256         client.magic = pt->magic;
257         client.iocs  = pt->ioctl_count;
258         up(&dev->struct_sem);
259
260         if (copy_to_user((drm_client_t __user *)arg, &client, sizeof(client)))
261                 return -EFAULT;
262         return 0;
263 }
264
265 /** 
266  * Get statistics information. 
267  * 
268  * \param inode device inode.
269  * \param filp file pointer.
270  * \param cmd command.
271  * \param arg user argument, pointing to a drm_stats structure.
272  * 
273  * \return zero on success or a negative number on failure.
274  */
275 int DRM(getstats)( struct inode *inode, struct file *filp,
276                    unsigned int cmd, unsigned long arg )
277 {
278         drm_file_t   *priv = filp->private_data;
279         drm_device_t *dev  = priv->dev;
280         drm_stats_t  stats;
281         int          i;
282
283         memset(&stats, 0, sizeof(stats));
284         
285         down(&dev->struct_sem);
286
287         for (i = 0; i < dev->counters; i++) {
288                 if (dev->types[i] == _DRM_STAT_LOCK)
289                         stats.data[i].value
290                                 = (dev->lock.hw_lock
291                                    ? dev->lock.hw_lock->lock : 0);
292                 else 
293                         stats.data[i].value = atomic_read(&dev->counts[i]);
294                 stats.data[i].type  = dev->types[i];
295         }
296         
297         stats.count = dev->counters;
298
299         up(&dev->struct_sem);
300
301         if (copy_to_user((drm_stats_t __user *)arg, &stats, sizeof(stats)))
302                 return -EFAULT;
303         return 0;
304 }
305
306 #define DRM_IF_MAJOR    1
307 #define DRM_IF_MINOR    2
308
309 int DRM(setversion)(DRM_IOCTL_ARGS)
310 {
311         DRM_DEVICE;
312         drm_set_version_t sv;
313         drm_set_version_t retv;
314         int if_version;
315         drm_set_version_t __user *argp = (void __user *)data;
316
317         DRM_COPY_FROM_USER_IOCTL(sv, argp, sizeof(sv));
318
319         retv.drm_di_major = DRM_IF_MAJOR;
320         retv.drm_di_minor = DRM_IF_MINOR;
321         retv.drm_dd_major = DRIVER_MAJOR;
322         retv.drm_dd_minor = DRIVER_MINOR;
323
324         DRM_COPY_TO_USER_IOCTL(argp, retv, sizeof(sv));
325
326         if (sv.drm_di_major != -1) {
327                 if (sv.drm_di_major != DRM_IF_MAJOR ||
328                     sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
329                         return EINVAL;
330                 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
331                 dev->if_version = DRM_MAX(if_version, dev->if_version);
332                 if (sv.drm_di_minor >= 1) {
333                         /*
334                          * Version 1.1 includes tying of DRM to specific device
335                          */
336                         DRM(set_busid)(dev);
337                 }
338         }
339
340         if (sv.drm_dd_major != -1) {
341                 if (sv.drm_dd_major != DRIVER_MAJOR ||
342                     sv.drm_dd_minor < 0 || sv.drm_dd_minor > DRIVER_MINOR)
343                         return EINVAL;
344
345                 if (dev->fn_tbl.set_version)
346                         dev->fn_tbl.set_version(dev, &sv);
347         }
348         return 0;
349 }