ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
39 /**
40  * Get interrupt from bus id.
41  * 
42  * \param inode device inode.
43  * \param filp file pointer.
44  * \param cmd command.
45  * \param arg user argument, pointing to a drm_irq_busid structure.
46  * \return zero on success or a negative number on failure.
47  * 
48  * Finds the PCI device with the specified bus id and gets its IRQ number.
49  */
50 int DRM(irq_busid)(struct inode *inode, struct file *filp,
51                    unsigned int cmd, unsigned long arg)
52 {
53         drm_irq_busid_t p;
54         struct pci_dev  *dev;
55
56         if (copy_from_user(&p, (drm_irq_busid_t *)arg, sizeof(p)))
57                 return -EFAULT;
58 #ifdef __alpha__
59         {
60                 int domain = p.busnum >> 8;
61                 p.busnum &= 0xff;
62
63                 /*
64                  * Find the hose the device is on (the domain number is the
65                  * hose index) and offset the bus by the root bus of that
66                  * hose.
67                  */
68                 for(dev = pci_find_device(PCI_ANY_ID,PCI_ANY_ID,NULL);
69                     dev;
70                     dev = pci_find_device(PCI_ANY_ID,PCI_ANY_ID,dev)) {
71                         struct pci_controller *hose = dev->sysdata;
72                         
73                         if (hose->index == domain) {
74                                 p.busnum += hose->bus->number;
75                                 break;
76                         }
77                 }
78         }
79 #endif
80         dev = pci_find_slot(p.busnum, PCI_DEVFN(p.devnum, p.funcnum));
81         if (!dev) {
82                 DRM_ERROR("pci_find_slot failed for %d:%d:%d\n",
83                           p.busnum, p.devnum, p.funcnum);
84                 p.irq = 0;
85                 goto out;
86         }                       
87         if (pci_enable_device(dev) != 0) {
88                 DRM_ERROR("pci_enable_device failed for %d:%d:%d\n",
89                           p.busnum, p.devnum, p.funcnum);
90                 p.irq = 0;
91                 goto out;
92         }               
93         p.irq = dev->irq;
94  out:
95         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
96                   p.busnum, p.devnum, p.funcnum, p.irq);
97         if (copy_to_user((drm_irq_busid_t *)arg, &p, sizeof(p)))
98                 return -EFAULT;
99         return 0;
100 }
101
102 /**
103  * Get the bus id.
104  * 
105  * \param inode device inode.
106  * \param filp file pointer.
107  * \param cmd command.
108  * \param arg user argument, pointing to a drm_unique structure.
109  * \return zero on success or a negative number on failure.
110  *
111  * Copies the bus id from drm_device::unique into user space.
112  */
113 int DRM(getunique)(struct inode *inode, struct file *filp,
114                    unsigned int cmd, unsigned long arg)
115 {
116         drm_file_t       *priv   = filp->private_data;
117         drm_device_t     *dev    = priv->dev;
118         drm_unique_t     u;
119
120         if (copy_from_user(&u, (drm_unique_t *)arg, sizeof(u)))
121                 return -EFAULT;
122         if (u.unique_len >= dev->unique_len) {
123                 if (copy_to_user(u.unique, dev->unique, dev->unique_len))
124                         return -EFAULT;
125         }
126         u.unique_len = dev->unique_len;
127         if (copy_to_user((drm_unique_t *)arg, &u, sizeof(u)))
128                 return -EFAULT;
129         return 0;
130 }
131
132 /**
133  * Set the bus id.
134  * 
135  * \param inode device inode.
136  * \param filp file pointer.
137  * \param cmd command.
138  * \param arg user argument, pointing to a drm_unique structure.
139  * \return zero on success or a negative number on failure.
140  *
141  * Copies the bus id from userspace into drm_device::unique, and searches for
142  * the respective PCI device, updating drm_device::pdev.
143  */
144 int DRM(setunique)(struct inode *inode, struct file *filp,
145                    unsigned int cmd, unsigned long arg)
146 {
147         drm_file_t       *priv   = filp->private_data;
148         drm_device_t     *dev    = priv->dev;
149         drm_unique_t     u;
150
151         if (dev->unique_len || dev->unique) return -EBUSY;
152
153         if (copy_from_user(&u, (drm_unique_t *)arg, sizeof(u))) return -EFAULT;
154
155         if (!u.unique_len || u.unique_len > 1024) return -EINVAL;
156
157         dev->unique_len = u.unique_len;
158         dev->unique     = DRM(alloc)(u.unique_len + 1, DRM_MEM_DRIVER);
159         if(!dev->unique) return -ENOMEM;
160         if (copy_from_user(dev->unique, u.unique, dev->unique_len))
161                 return -EFAULT;
162
163         dev->unique[dev->unique_len] = '\0';
164
165         dev->devname = DRM(alloc)(strlen(dev->name) + strlen(dev->unique) + 2,
166                                   DRM_MEM_DRIVER);
167         if(!dev->devname) {
168                 DRM(free)(dev->devname, sizeof(*dev->devname), DRM_MEM_DRIVER);
169                 return -ENOMEM;
170         }
171         sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
172
173         do {
174                 struct pci_dev *pci_dev;
175                 int domain, b, d, f;
176                 char *p;
177  
178                 for(p = dev->unique; p && *p && *p != ':'; p++);
179                 if (!p || !*p) break;
180                 b = (int)simple_strtoul(p+1, &p, 10);
181                 if (*p != ':') break;
182                 d = (int)simple_strtoul(p+1, &p, 10);
183                 if (*p != ':') break;
184                 f = (int)simple_strtoul(p+1, &p, 10);
185                 if (*p) break;
186  
187                 domain = b >> 8;
188                 b &= 0xff;
189
190 #ifdef __alpha__
191                 /*
192                  * Find the hose the device is on (the domain number is the
193                  * hose index) and offset the bus by the root bus of that
194                  * hose.
195                  */
196                 for(pci_dev = pci_find_device(PCI_ANY_ID,PCI_ANY_ID,NULL);
197                     pci_dev;
198                     pci_dev = pci_find_device(PCI_ANY_ID,PCI_ANY_ID,pci_dev)) {
199                         struct pci_controller *hose = pci_dev->sysdata;
200                         
201                         if (hose->index == domain) {
202                                 b += hose->bus->number;
203                                 break;
204                         }
205                 }
206 #endif
207
208                 pci_dev = pci_find_slot(b, PCI_DEVFN(d,f));
209                 if (pci_dev) {
210                         dev->pdev = pci_dev;
211 #ifdef __alpha__
212                         dev->hose = pci_dev->sysdata;
213 #endif
214                 }
215         } while(0);
216
217         return 0;
218 }
219
220
221 /**
222  * Get a mapping information.
223  *
224  * \param inode device inode.
225  * \param filp file pointer.
226  * \param cmd command.
227  * \param arg user argument, pointing to a drm_map structure.
228  * 
229  * \return zero on success or a negative number on failure.
230  *
231  * Searches for the mapping with the specified offset and copies its information
232  * into userspace
233  */
234 int DRM(getmap)( struct inode *inode, struct file *filp,
235                  unsigned int cmd, unsigned long arg )
236 {
237         drm_file_t   *priv = filp->private_data;
238         drm_device_t *dev  = priv->dev;
239         drm_map_t    map;
240         drm_map_list_t *r_list = NULL;
241         struct list_head *list;
242         int          idx;
243         int          i;
244
245         if (copy_from_user(&map, (drm_map_t *)arg, sizeof(map)))
246                 return -EFAULT;
247         idx = map.offset;
248
249         down(&dev->struct_sem);
250         if (idx < 0) {
251                 up(&dev->struct_sem);
252                 return -EINVAL;
253         }
254
255         i = 0;
256         list_for_each(list, &dev->maplist->head) {
257                 if(i == idx) {
258                         r_list = list_entry(list, drm_map_list_t, head);
259                         break;
260                 }
261                 i++;
262         }
263         if(!r_list || !r_list->map) {
264                 up(&dev->struct_sem);
265                 return -EINVAL;
266         }
267
268         map.offset = r_list->map->offset;
269         map.size   = r_list->map->size;
270         map.type   = r_list->map->type;
271         map.flags  = r_list->map->flags;
272         map.handle = r_list->map->handle;
273         map.mtrr   = r_list->map->mtrr;
274         up(&dev->struct_sem);
275
276         if (copy_to_user((drm_map_t *)arg, &map, sizeof(map))) return -EFAULT;
277         return 0;
278 }
279
280 /**
281  * Get client information.
282  *
283  * \param inode device inode.
284  * \param filp file pointer.
285  * \param cmd command.
286  * \param arg user argument, pointing to a drm_client structure.
287  * 
288  * \return zero on success or a negative number on failure.
289  *
290  * Searches for the client with the specified index and copies its information
291  * into userspace
292  */
293 int DRM(getclient)( struct inode *inode, struct file *filp,
294                     unsigned int cmd, unsigned long arg )
295 {
296         drm_file_t   *priv = filp->private_data;
297         drm_device_t *dev  = priv->dev;
298         drm_client_t client;
299         drm_file_t   *pt;
300         int          idx;
301         int          i;
302
303         if (copy_from_user(&client, (drm_client_t *)arg, sizeof(client)))
304                 return -EFAULT;
305         idx = client.idx;
306         down(&dev->struct_sem);
307         for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next)
308                 ;
309
310         if (!pt) {
311                 up(&dev->struct_sem);
312                 return -EINVAL;
313         }
314         client.auth  = pt->authenticated;
315         client.pid   = pt->pid;
316         client.uid   = pt->uid;
317         client.magic = pt->magic;
318         client.iocs  = pt->ioctl_count;
319         up(&dev->struct_sem);
320
321         if (copy_to_user((drm_client_t *)arg, &client, sizeof(client)))
322                 return -EFAULT;
323         return 0;
324 }
325
326 /** 
327  * Get statistics information. 
328  * 
329  * \param inode device inode.
330  * \param filp file pointer.
331  * \param cmd command.
332  * \param arg user argument, pointing to a drm_stats structure.
333  * 
334  * \return zero on success or a negative number on failure.
335  */
336 int DRM(getstats)( struct inode *inode, struct file *filp,
337                    unsigned int cmd, unsigned long arg )
338 {
339         drm_file_t   *priv = filp->private_data;
340         drm_device_t *dev  = priv->dev;
341         drm_stats_t  stats;
342         int          i;
343
344         memset(&stats, 0, sizeof(stats));
345         
346         down(&dev->struct_sem);
347
348         for (i = 0; i < dev->counters; i++) {
349                 if (dev->types[i] == _DRM_STAT_LOCK)
350                         stats.data[i].value
351                                 = (dev->lock.hw_lock
352                                    ? dev->lock.hw_lock->lock : 0);
353                 else 
354                         stats.data[i].value = atomic_read(&dev->counts[i]);
355                 stats.data[i].type  = dev->types[i];
356         }
357         
358         stats.count = dev->counters;
359
360         up(&dev->struct_sem);
361
362         if (copy_to_user((drm_stats_t *)arg, &stats, sizeof(stats)))
363                 return -EFAULT;
364         return 0;
365 }