1/**
2 * \file drm_ioctl.c
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#include "drm_core.h"
38
39#include "linux/pci.h"
40
41/**
42 * Get the bus id.
43 *
44 * \param inode device inode.
45 * \param file_priv DRM file private.
46 * \param cmd command.
47 * \param arg user argument, pointing to a drm_unique structure.
48 * \return zero on success or a negative number on failure.
49 *
50 * Copies the bus id from drm_device::unique into user space.
51 */
52int drm_getunique(struct drm_device *dev, void *data,
53		  struct drm_file *file_priv)
54{
55	struct drm_unique *u = data;
56	struct drm_master *master = file_priv->master;
57
58	if (u->unique_len >= master->unique_len) {
59		if (copy_to_user(u->unique, master->unique, master->unique_len))
60			return -EFAULT;
61	}
62	u->unique_len = master->unique_len;
63
64	return 0;
65}
66
67static void
68drm_unset_busid(struct drm_device *dev,
69		struct drm_master *master)
70{
71	kfree(dev->devname);
72	dev->devname = NULL;
73
74	kfree(master->unique);
75	master->unique = NULL;
76	master->unique_len = 0;
77	master->unique_size = 0;
78}
79
80/**
81 * Set the bus id.
82 *
83 * \param inode device inode.
84 * \param file_priv DRM file private.
85 * \param cmd command.
86 * \param arg user argument, pointing to a drm_unique structure.
87 * \return zero on success or a negative number on failure.
88 *
89 * Copies the bus id from userspace into drm_device::unique, and verifies that
90 * it matches the device this DRM is attached to (EINVAL otherwise).  Deprecated
91 * in interface version 1.1 and will return EBUSY when setversion has requested
92 * version 1.1 or greater.
93 */
94int drm_setunique(struct drm_device *dev, void *data,
95		  struct drm_file *file_priv)
96{
97	struct drm_unique *u = data;
98	struct drm_master *master = file_priv->master;
99	int ret;
100
101	if (master->unique_len || master->unique)
102		return -EBUSY;
103
104	if (!u->unique_len || u->unique_len > 1024)
105		return -EINVAL;
106
107	if (!dev->driver->bus->set_unique)
108		return -EINVAL;
109
110	ret = dev->driver->bus->set_unique(dev, master, u);
111	if (ret)
112		goto err;
113
114	return 0;
115
116err:
117	drm_unset_busid(dev, master);
118	return ret;
119}
120
121static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
122{
123	struct drm_master *master = file_priv->master;
124	int ret;
125
126	if (master->unique != NULL)
127		drm_unset_busid(dev, master);
128
129	ret = dev->driver->bus->set_busid(dev, master);
130	if (ret)
131		goto err;
132	return 0;
133err:
134	drm_unset_busid(dev, master);
135	return ret;
136}
137
138/**
139 * Get a mapping information.
140 *
141 * \param inode device inode.
142 * \param file_priv DRM file private.
143 * \param cmd command.
144 * \param arg user argument, pointing to a drm_map structure.
145 *
146 * \return zero on success or a negative number on failure.
147 *
148 * Searches for the mapping with the specified offset and copies its information
149 * into userspace
150 */
151int drm_getmap(struct drm_device *dev, void *data,
152	       struct drm_file *file_priv)
153{
154	struct drm_map *map = data;
155	struct drm_map_list *r_list = NULL;
156	struct list_head *list;
157	int idx;
158	int i;
159
160	idx = map->offset;
161	if (idx < 0)
162		return -EINVAL;
163
164	i = 0;
165	mutex_lock(&dev->struct_mutex);
166	list_for_each(list, &dev->maplist) {
167		if (i == idx) {
168			r_list = list_entry(list, struct drm_map_list, head);
169			break;
170		}
171		i++;
172	}
173	if (!r_list || !r_list->map) {
174		mutex_unlock(&dev->struct_mutex);
175		return -EINVAL;
176	}
177
178	map->offset = r_list->map->offset;
179	map->size = r_list->map->size;
180	map->type = r_list->map->type;
181	map->flags = r_list->map->flags;
182	map->handle = (void *)(unsigned long) r_list->user_token;
183	map->mtrr = r_list->map->mtrr;
184	mutex_unlock(&dev->struct_mutex);
185
186	return 0;
187}
188
189/**
190 * Get client information.
191 *
192 * \param inode device inode.
193 * \param file_priv DRM file private.
194 * \param cmd command.
195 * \param arg user argument, pointing to a drm_client structure.
196 *
197 * \return zero on success or a negative number on failure.
198 *
199 * Searches for the client with the specified index and copies its information
200 * into userspace
201 */
202int drm_getclient(struct drm_device *dev, void *data,
203		  struct drm_file *file_priv)
204{
205	struct drm_client *client = data;
206	struct drm_file *pt;
207	int idx;
208	int i;
209
210	idx = client->idx;
211	i = 0;
212
213	mutex_lock(&dev->struct_mutex);
214	list_for_each_entry(pt, &dev->filelist, lhead) {
215		if (i++ >= idx) {
216			client->auth = pt->authenticated;
217			client->pid = pt->pid;
218			client->uid = pt->uid;
219			client->magic = pt->magic;
220			client->iocs = pt->ioctl_count;
221			mutex_unlock(&dev->struct_mutex);
222
223			return 0;
224		}
225	}
226	mutex_unlock(&dev->struct_mutex);
227
228	return -EINVAL;
229}
230
231/**
232 * Get statistics information.
233 *
234 * \param inode device inode.
235 * \param file_priv DRM file private.
236 * \param cmd command.
237 * \param arg user argument, pointing to a drm_stats structure.
238 *
239 * \return zero on success or a negative number on failure.
240 */
241int drm_getstats(struct drm_device *dev, void *data,
242		 struct drm_file *file_priv)
243{
244	struct drm_stats *stats = data;
245	int i;
246
247	memset(stats, 0, sizeof(*stats));
248
249	for (i = 0; i < dev->counters; i++) {
250		if (dev->types[i] == _DRM_STAT_LOCK)
251			stats->data[i].value =
252			    (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
253		else
254			stats->data[i].value = atomic_read(&dev->counts[i]);
255		stats->data[i].type = dev->types[i];
256	}
257
258	stats->count = dev->counters;
259
260	return 0;
261}
262
263/**
264 * Get device/driver capabilities
265 */
266int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
267{
268	struct drm_get_cap *req = data;
269
270	req->value = 0;
271	switch (req->capability) {
272	case DRM_CAP_DUMB_BUFFER:
273		if (dev->driver->dumb_create)
274			req->value = 1;
275		break;
276	case DRM_CAP_VBLANK_HIGH_CRTC:
277		req->value = 1;
278		break;
279	default:
280		return -EINVAL;
281	}
282	return 0;
283}
284
285/**
286 * Setversion ioctl.
287 *
288 * \param inode device inode.
289 * \param file_priv DRM file private.
290 * \param cmd command.
291 * \param arg user argument, pointing to a drm_lock structure.
292 * \return zero on success or negative number on failure.
293 *
294 * Sets the requested interface version
295 */
296int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
297{
298	struct drm_set_version *sv = data;
299	int if_version, retcode = 0;
300
301	if (sv->drm_di_major != -1) {
302		if (sv->drm_di_major != DRM_IF_MAJOR ||
303		    sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
304			retcode = -EINVAL;
305			goto done;
306		}
307		if_version = DRM_IF_VERSION(sv->drm_di_major,
308					    sv->drm_di_minor);
309		dev->if_version = max(if_version, dev->if_version);
310		if (sv->drm_di_minor >= 1) {
311			/*
312			 * Version 1.1 includes tying of DRM to specific device
313			 * Version 1.4 has proper PCI domain support
314			 */
315			retcode = drm_set_busid(dev, file_priv);
316			if (retcode)
317				goto done;
318		}
319	}
320
321	if (sv->drm_dd_major != -1) {
322		if (sv->drm_dd_major != dev->driver->major ||
323		    sv->drm_dd_minor < 0 || sv->drm_dd_minor >
324		    dev->driver->minor) {
325			retcode = -EINVAL;
326			goto done;
327		}
328
329		if (dev->driver->set_version)
330			dev->driver->set_version(dev, sv);
331	}
332
333done:
334	sv->drm_di_major = DRM_IF_MAJOR;
335	sv->drm_di_minor = DRM_IF_MINOR;
336	sv->drm_dd_major = dev->driver->major;
337	sv->drm_dd_minor = dev->driver->minor;
338
339	return retcode;
340}
341
342/** No-op ioctl. */
343int drm_noop(struct drm_device *dev, void *data,
344	     struct drm_file *file_priv)
345{
346	DRM_DEBUG("\n");
347	return 0;
348}
349