3 * IOCTL processing for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
38 #include "linux/pci.h"
43 * \param inode device inode.
44 * \param filp file pointer.
46 * \param arg user argument, pointing to a drm_unique structure.
47 * \return zero on success or a negative number on failure.
49 * Copies the bus id from drm_device::unique into user space.
51 int DRM(getunique)(struct inode *inode, struct file *filp,
52 unsigned int cmd, unsigned long arg)
54 drm_file_t *priv = filp->private_data;
55 drm_device_t *dev = priv->dev;
58 if (copy_from_user(&u, (drm_unique_t *)arg, sizeof(u)))
60 if (u.unique_len >= dev->unique_len) {
61 if (copy_to_user(u.unique, dev->unique, dev->unique_len))
64 u.unique_len = dev->unique_len;
65 if (copy_to_user((drm_unique_t *)arg, &u, sizeof(u)))
73 * \param inode device inode.
74 * \param filp file pointer.
76 * \param arg user argument, pointing to a drm_unique structure.
77 * \return zero on success or a negative number on failure.
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.
84 int DRM(setunique)(struct inode *inode, struct file *filp,
85 unsigned int cmd, unsigned long arg)
87 drm_file_t *priv = filp->private_data;
88 drm_device_t *dev = priv->dev;
90 int domain, bus, slot, func, ret;
92 if (dev->unique_len || dev->unique) return -EBUSY;
94 if (copy_from_user(&u, (drm_unique_t *)arg, sizeof(u))) return -EFAULT;
96 if (!u.unique_len || u.unique_len > 1024) return -EINVAL;
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))
104 dev->unique[dev->unique_len] = '\0';
106 dev->devname = DRM(alloc)(strlen(dev->name) + strlen(dev->unique) + 2,
111 sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
113 /* Return error if the busid submitted doesn't match the device's actual
116 ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
118 return DRM_ERR(EINVAL);
122 if ((domain != dev->pci_domain) ||
123 (bus != dev->pci_bus) ||
124 (slot != dev->pci_slot) ||
125 (func != dev->pci_func))
132 DRM(set_busid)(drm_device_t *dev)
134 if (dev->unique != NULL)
137 dev->unique_len = 20;
138 dev->unique = DRM(alloc)(dev->unique_len + 1, DRM_MEM_DRIVER);
139 if (dev->unique == NULL)
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);
145 dev->devname = DRM(alloc)(strlen(dev->name) + dev->unique_len + 2,
147 if (dev->devname == NULL)
150 sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
157 * Get a mapping information.
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.
164 * \return zero on success or a negative number on failure.
166 * Searches for the mapping with the specified offset and copies its information
169 int DRM(getmap)( struct inode *inode, struct file *filp,
170 unsigned int cmd, unsigned long arg )
172 drm_file_t *priv = filp->private_data;
173 drm_device_t *dev = priv->dev;
175 drm_map_list_t *r_list = NULL;
176 struct list_head *list;
180 if (copy_from_user(&map, (drm_map_t *)arg, sizeof(map)))
184 down(&dev->struct_sem);
186 up(&dev->struct_sem);
191 list_for_each(list, &dev->maplist->head) {
193 r_list = list_entry(list, drm_map_list_t, head);
198 if(!r_list || !r_list->map) {
199 up(&dev->struct_sem);
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);
211 if (copy_to_user((drm_map_t *)arg, &map, sizeof(map))) return -EFAULT;
216 * Get client information.
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.
223 * \return zero on success or a negative number on failure.
225 * Searches for the client with the specified index and copies its information
228 int DRM(getclient)( struct inode *inode, struct file *filp,
229 unsigned int cmd, unsigned long arg )
231 drm_file_t *priv = filp->private_data;
232 drm_device_t *dev = priv->dev;
238 if (copy_from_user(&client, (drm_client_t *)arg, sizeof(client)))
241 down(&dev->struct_sem);
242 for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next)
246 up(&dev->struct_sem);
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);
256 if (copy_to_user((drm_client_t *)arg, &client, sizeof(client)))
262 * Get statistics information.
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.
269 * \return zero on success or a negative number on failure.
271 int DRM(getstats)( struct inode *inode, struct file *filp,
272 unsigned int cmd, unsigned long arg )
274 drm_file_t *priv = filp->private_data;
275 drm_device_t *dev = priv->dev;
279 memset(&stats, 0, sizeof(stats));
281 down(&dev->struct_sem);
283 for (i = 0; i < dev->counters; i++) {
284 if (dev->types[i] == _DRM_STAT_LOCK)
287 ? dev->lock.hw_lock->lock : 0);
289 stats.data[i].value = atomic_read(&dev->counts[i]);
290 stats.data[i].type = dev->types[i];
293 stats.count = dev->counters;
295 up(&dev->struct_sem);
297 if (copy_to_user((drm_stats_t *)arg, &stats, sizeof(stats)))
302 #define DRM_IF_MAJOR 1
303 #define DRM_IF_MINOR 2
305 int DRM(setversion)(DRM_IOCTL_ARGS)
308 drm_set_version_t sv;
309 drm_set_version_t retv;
312 DRM_COPY_FROM_USER_IOCTL(sv, (drm_set_version_t *)data, sizeof(sv));
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;
319 DRM_COPY_TO_USER_IOCTL((drm_set_version_t *)data, retv, sizeof(sv));
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)
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) {
329 * Version 1.1 includes tying of DRM to specific device
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)
339 #ifdef DRIVER_SETVERSION
340 DRIVER_SETVERSION(dev, &sv);