1/* drm_ioctl.h -- IOCTL processing for DRM -*- linux-c -*-
2 * Created: Fri Jan  8 09:01:26 1999 by faith@valinux.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 *    Rickard E. (Rik) Faith <faith@valinux.com>
29 *    Gareth Hughes <gareth@valinux.com>
30 */
31
32#include "drmP.h"
33
34int DRM(irq_busid)(struct inode *inode, struct file *filp,
35		   unsigned int cmd, unsigned long arg)
36{
37	drm_irq_busid_t p;
38	struct pci_dev	*dev;
39
40	if (copy_from_user(&p, (drm_irq_busid_t *)arg, sizeof(p)))
41		return -EFAULT;
42	dev = pci_find_slot(p.busnum, PCI_DEVFN(p.devnum, p.funcnum));
43	if (dev) p.irq = dev->irq;
44	else	 p.irq = 0;
45	DRM_DEBUG("%d:%d:%d => IRQ %d\n",
46		  p.busnum, p.devnum, p.funcnum, p.irq);
47	if (copy_to_user((drm_irq_busid_t *)arg, &p, sizeof(p)))
48		return -EFAULT;
49	return 0;
50}
51
52int DRM(getunique)(struct inode *inode, struct file *filp,
53		   unsigned int cmd, unsigned long arg)
54{
55	drm_file_t	 *priv	 = filp->private_data;
56	drm_device_t	 *dev	 = priv->dev;
57	drm_unique_t	 u;
58
59	if (copy_from_user(&u, (drm_unique_t *)arg, 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((drm_unique_t *)arg, &u, sizeof(u)))
67		return -EFAULT;
68	return 0;
69}
70
71int DRM(setunique)(struct inode *inode, struct file *filp,
72		   unsigned int cmd, unsigned long arg)
73{
74	drm_file_t	 *priv	 = filp->private_data;
75	drm_device_t	 *dev	 = priv->dev;
76	drm_unique_t	 u;
77
78	if (dev->unique_len || dev->unique) return -EBUSY;
79
80	if (copy_from_user(&u, (drm_unique_t *)arg, sizeof(u))) return -EFAULT;
81
82	if (!u.unique_len || u.unique_len > 1024) return -EINVAL;
83
84	dev->unique_len = u.unique_len;
85	dev->unique	= DRM(alloc)(u.unique_len + 1, DRM_MEM_DRIVER);
86	if(!dev->unique) return -ENOMEM;
87	if (copy_from_user(dev->unique, u.unique, dev->unique_len))
88		return -EFAULT;
89
90	dev->unique[dev->unique_len] = '\0';
91
92	dev->devname = DRM(alloc)(strlen(dev->name) + strlen(dev->unique) + 2,
93				  DRM_MEM_DRIVER);
94	if(!dev->devname) {
95		DRM(free)(dev->devname, sizeof(*dev->devname), DRM_MEM_DRIVER);
96		return -ENOMEM;
97	}
98	sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
99
100	do {
101		struct pci_dev *pci_dev;
102                int b, d, f;
103                char *p;
104
105                for(p = dev->unique; p && *p && *p != ':'; p++);
106                if (!p || !*p) break;
107                b = (int)simple_strtoul(p+1, &p, 10);
108                if (*p != ':') break;
109                d = (int)simple_strtoul(p+1, &p, 10);
110                if (*p != ':') break;
111                f = (int)simple_strtoul(p+1, &p, 10);
112                if (*p) break;
113
114                pci_dev = pci_find_slot(b, PCI_DEVFN(d,f));
115                if (pci_dev) {
116			dev->pdev = pci_dev;
117#ifdef __alpha__
118			dev->hose = pci_dev->sysdata;
119#endif
120		}
121        } while(0);
122
123	return 0;
124}
125
126
127int DRM(getmap)( struct inode *inode, struct file *filp,
128		 unsigned int cmd, unsigned long arg )
129{
130	drm_file_t   *priv = filp->private_data;
131	drm_device_t *dev  = priv->dev;
132	drm_map_t    map;
133	drm_map_list_t *r_list = NULL;
134	struct list_head *list;
135	int          idx;
136	int	     i;
137
138	if (copy_from_user(&map, (drm_map_t *)arg, sizeof(map)))
139		return -EFAULT;
140	idx = map.offset;
141
142	down(&dev->struct_sem);
143	if (idx < 0 || idx >= dev->map_count) {
144		up(&dev->struct_sem);
145		return -EINVAL;
146	}
147
148	i = 0;
149	list_for_each(list, &dev->maplist->head) {
150		if(i == idx) {
151			r_list = (drm_map_list_t *)list;
152			break;
153		}
154		i++;
155	}
156	if(!r_list || !r_list->map) {
157		up(&dev->struct_sem);
158		return -EINVAL;
159	}
160
161	map.offset = r_list->map->offset;
162	map.size   = r_list->map->size;
163	map.type   = r_list->map->type;
164	map.flags  = r_list->map->flags;
165	map.handle = r_list->map->handle;
166	map.mtrr   = r_list->map->mtrr;
167	up(&dev->struct_sem);
168
169	if (copy_to_user((drm_map_t *)arg, &map, sizeof(map))) return -EFAULT;
170	return 0;
171}
172
173int DRM(getclient)( struct inode *inode, struct file *filp,
174		    unsigned int cmd, unsigned long arg )
175{
176	drm_file_t   *priv = filp->private_data;
177	drm_device_t *dev  = priv->dev;
178	drm_client_t client;
179	drm_file_t   *pt;
180	int          idx;
181	int          i;
182
183	if (copy_from_user(&client, (drm_client_t *)arg, sizeof(client)))
184		return -EFAULT;
185	idx = client.idx;
186	down(&dev->struct_sem);
187	for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next)
188		;
189
190	if (!pt) {
191		up(&dev->struct_sem);
192		return -EINVAL;
193	}
194	client.auth  = pt->authenticated;
195	client.pid   = pt->pid;
196	client.uid   = pt->uid;
197	client.magic = pt->magic;
198	client.iocs  = pt->ioctl_count;
199	up(&dev->struct_sem);
200
201	if (copy_to_user((drm_client_t *)arg, &client, sizeof(client)))
202		return -EFAULT;
203	return 0;
204}
205
206int DRM(getstats)( struct inode *inode, struct file *filp,
207		   unsigned int cmd, unsigned long arg )
208{
209	drm_file_t   *priv = filp->private_data;
210	drm_device_t *dev  = priv->dev;
211	drm_stats_t  stats;
212	int          i;
213
214	memset(&stats, 0, sizeof(stats));
215
216	down(&dev->struct_sem);
217
218	for (i = 0; i < dev->counters; i++) {
219		if (dev->types[i] == _DRM_STAT_LOCK)
220			stats.data[i].value
221				= (dev->lock.hw_lock
222				   ? dev->lock.hw_lock->lock : 0);
223		else
224			stats.data[i].value = atomic_read(&dev->counts[i]);
225		stats.data[i].type  = dev->types[i];
226	}
227
228	stats.count = dev->counters;
229
230	up(&dev->struct_sem);
231
232	if (copy_to_user((drm_stats_t *)arg, &stats, sizeof(stats)))
233		return -EFAULT;
234	return 0;
235}
236