patch-2_6_7-vs1_9_1_12
[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         dev->devname = DRM(alloc)(strlen(dev->name) + dev->unique_len + 2,
146                                 DRM_MEM_DRIVER);
147         if (dev->devname == NULL)
148                 return ENOMEM;
149
150         sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
151
152         return 0;
153 }
154
155
156 /**
157  * Get a mapping information.
158  *
159  * \param inode device inode.
160  * \param filp file pointer.
161  * \param cmd command.
162  * \param arg user argument, pointing to a drm_map structure.
163  * 
164  * \return zero on success or a negative number on failure.
165  *
166  * Searches for the mapping with the specified offset and copies its information
167  * into userspace
168  */
169 int DRM(getmap)( struct inode *inode, struct file *filp,
170                  unsigned int cmd, unsigned long arg )
171 {
172         drm_file_t   *priv = filp->private_data;
173         drm_device_t *dev  = priv->dev;
174         drm_map_t    map;
175         drm_map_list_t *r_list = NULL;
176         struct list_head *list;
177         int          idx;
178         int          i;
179
180         if (copy_from_user(&map, (drm_map_t *)arg, sizeof(map)))
181                 return -EFAULT;
182         idx = map.offset;
183
184         down(&dev->struct_sem);
185         if (idx < 0) {
186                 up(&dev->struct_sem);
187                 return -EINVAL;
188         }
189
190         i = 0;
191         list_for_each(list, &dev->maplist->head) {
192                 if(i == idx) {
193                         r_list = list_entry(list, drm_map_list_t, head);
194                         break;
195                 }
196                 i++;
197         }
198         if(!r_list || !r_list->map) {
199                 up(&dev->struct_sem);
200                 return -EINVAL;
201         }
202
203         map.offset = r_list->map->offset;
204         map.size   = r_list->map->size;
205         map.type   = r_list->map->type;
206         map.flags  = r_list->map->flags;
207         map.handle = r_list->map->handle;
208         map.mtrr   = r_list->map->mtrr;
209         up(&dev->struct_sem);
210
211         if (copy_to_user((drm_map_t *)arg, &map, sizeof(map))) return -EFAULT;
212         return 0;
213 }
214
215 /**
216  * Get client information.
217  *
218  * \param inode device inode.
219  * \param filp file pointer.
220  * \param cmd command.
221  * \param arg user argument, pointing to a drm_client structure.
222  * 
223  * \return zero on success or a negative number on failure.
224  *
225  * Searches for the client with the specified index and copies its information
226  * into userspace
227  */
228 int DRM(getclient)( struct inode *inode, struct file *filp,
229                     unsigned int cmd, unsigned long arg )
230 {
231         drm_file_t   *priv = filp->private_data;
232         drm_device_t *dev  = priv->dev;
233         drm_client_t client;
234         drm_file_t   *pt;
235         int          idx;
236         int          i;
237
238         if (copy_from_user(&client, (drm_client_t *)arg, sizeof(client)))
239                 return -EFAULT;
240         idx = client.idx;
241         down(&dev->struct_sem);
242         for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next)
243                 ;
244
245         if (!pt) {
246                 up(&dev->struct_sem);
247                 return -EINVAL;
248         }
249         client.auth  = pt->authenticated;
250         client.pid   = pt->pid;
251         client.uid   = pt->uid;
252         client.magic = pt->magic;
253         client.iocs  = pt->ioctl_count;
254         up(&dev->struct_sem);
255
256         if (copy_to_user((drm_client_t *)arg, &client, sizeof(client)))
257                 return -EFAULT;
258         return 0;
259 }
260
261 /** 
262  * Get statistics information. 
263  * 
264  * \param inode device inode.
265  * \param filp file pointer.
266  * \param cmd command.
267  * \param arg user argument, pointing to a drm_stats structure.
268  * 
269  * \return zero on success or a negative number on failure.
270  */
271 int DRM(getstats)( struct inode *inode, struct file *filp,
272                    unsigned int cmd, unsigned long arg )
273 {
274         drm_file_t   *priv = filp->private_data;
275         drm_device_t *dev  = priv->dev;
276         drm_stats_t  stats;
277         int          i;
278
279         memset(&stats, 0, sizeof(stats));
280         
281         down(&dev->struct_sem);
282
283         for (i = 0; i < dev->counters; i++) {
284                 if (dev->types[i] == _DRM_STAT_LOCK)
285                         stats.data[i].value
286                                 = (dev->lock.hw_lock
287                                    ? dev->lock.hw_lock->lock : 0);
288                 else 
289                         stats.data[i].value = atomic_read(&dev->counts[i]);
290                 stats.data[i].type  = dev->types[i];
291         }
292         
293         stats.count = dev->counters;
294
295         up(&dev->struct_sem);
296
297         if (copy_to_user((drm_stats_t *)arg, &stats, sizeof(stats)))
298                 return -EFAULT;
299         return 0;
300 }
301
302 #define DRM_IF_MAJOR    1
303 #define DRM_IF_MINOR    2
304
305 int DRM(setversion)(DRM_IOCTL_ARGS)
306 {
307         DRM_DEVICE;
308         drm_set_version_t sv;
309         drm_set_version_t retv;
310         int if_version;
311
312         DRM_COPY_FROM_USER_IOCTL(sv, (drm_set_version_t *)data, sizeof(sv));
313
314         retv.drm_di_major = DRM_IF_MAJOR;
315         retv.drm_di_minor = DRM_IF_MINOR;
316         retv.drm_dd_major = DRIVER_MAJOR;
317         retv.drm_dd_minor = DRIVER_MINOR;
318
319         DRM_COPY_TO_USER_IOCTL((drm_set_version_t *)data, retv, sizeof(sv));
320
321         if (sv.drm_di_major != -1) {
322                 if (sv.drm_di_major != DRM_IF_MAJOR ||
323                     sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
324                         return EINVAL;
325                 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
326                 dev->if_version = DRM_MAX(if_version, dev->if_version);
327                 if (sv.drm_di_minor >= 1) {
328                         /*
329                          * Version 1.1 includes tying of DRM to specific device
330                          */
331                         DRM(set_busid)(dev);
332                 }
333         }
334
335         if (sv.drm_dd_major != -1) {
336                 if (sv.drm_dd_major != DRIVER_MAJOR ||
337                     sv.drm_dd_minor < 0 || sv.drm_dd_minor > DRIVER_MINOR)
338                         return EINVAL;
339 #ifdef DRIVER_SETVERSION
340                 DRIVER_SETVERSION(dev, &sv);
341 #endif
342         }
343         return 0;
344 }