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