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;
56 drm_unique_t __user *argp = (void __user *)arg;
59 if (copy_from_user(&u, argp, sizeof(u)))
61 if (u.unique_len >= dev->unique_len) {
62 if (copy_to_user(u.unique, dev->unique, dev->unique_len))
65 u.unique_len = dev->unique_len;
66 if (copy_to_user(argp, &u, sizeof(u)))
74 * \param inode device inode.
75 * \param filp file pointer.
77 * \param arg user argument, pointing to a drm_unique structure.
78 * \return zero on success or a negative number on failure.
80 * Copies the bus id from userspace into drm_device::unique, and verifies that
81 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
82 * in interface version 1.1 and will return EBUSY when setversion has requested
83 * version 1.1 or greater.
85 int DRM(setunique)(struct inode *inode, struct file *filp,
86 unsigned int cmd, unsigned long arg)
88 drm_file_t *priv = filp->private_data;
89 drm_device_t *dev = priv->dev;
91 int domain, bus, slot, func, ret;
93 if (dev->unique_len || dev->unique) return -EBUSY;
95 if (copy_from_user(&u, (drm_unique_t __user *)arg, sizeof(u)))
98 if (!u.unique_len || u.unique_len > 1024) return -EINVAL;
100 dev->unique_len = u.unique_len;
101 dev->unique = DRM(alloc)(u.unique_len + 1, DRM_MEM_DRIVER);
102 if(!dev->unique) return -ENOMEM;
103 if (copy_from_user(dev->unique, u.unique, dev->unique_len))
106 dev->unique[dev->unique_len] = '\0';
108 dev->devname = DRM(alloc)(strlen(dev->name) + strlen(dev->unique) + 2,
113 sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
115 /* Return error if the busid submitted doesn't match the device's actual
118 ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
120 return DRM_ERR(EINVAL);
124 if ((domain != dev->pci_domain) ||
125 (bus != dev->pci_bus) ||
126 (slot != dev->pci_slot) ||
127 (func != dev->pci_func))
134 DRM(set_busid)(drm_device_t *dev)
136 if (dev->unique != NULL)
139 dev->unique_len = 20;
140 dev->unique = DRM(alloc)(dev->unique_len + 1, DRM_MEM_DRIVER);
141 if (dev->unique == NULL)
144 snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
145 dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
147 dev->devname = DRM(alloc)(strlen(dev->name) + dev->unique_len + 2,
149 if (dev->devname == NULL)
152 sprintf(dev->devname, "%s@%s", dev->name, dev->unique);
159 * Get a mapping information.
161 * \param inode device inode.
162 * \param filp file pointer.
163 * \param cmd command.
164 * \param arg user argument, pointing to a drm_map structure.
166 * \return zero on success or a negative number on failure.
168 * Searches for the mapping with the specified offset and copies its information
171 int DRM(getmap)( struct inode *inode, struct file *filp,
172 unsigned int cmd, unsigned long arg )
174 drm_file_t *priv = filp->private_data;
175 drm_device_t *dev = priv->dev;
176 drm_map_t __user *argp = (void __user *)arg;
178 drm_map_list_t *r_list = NULL;
179 struct list_head *list;
183 if (copy_from_user(&map, argp, sizeof(map)))
187 down(&dev->struct_sem);
189 up(&dev->struct_sem);
194 list_for_each(list, &dev->maplist->head) {
196 r_list = list_entry(list, drm_map_list_t, head);
201 if(!r_list || !r_list->map) {
202 up(&dev->struct_sem);
206 map.offset = r_list->map->offset;
207 map.size = r_list->map->size;
208 map.type = r_list->map->type;
209 map.flags = r_list->map->flags;
210 map.handle = r_list->map->handle;
211 map.mtrr = r_list->map->mtrr;
212 up(&dev->struct_sem);
214 if (copy_to_user(argp, &map, sizeof(map))) return -EFAULT;
219 * Get client information.
221 * \param inode device inode.
222 * \param filp file pointer.
223 * \param cmd command.
224 * \param arg user argument, pointing to a drm_client structure.
226 * \return zero on success or a negative number on failure.
228 * Searches for the client with the specified index and copies its information
231 int DRM(getclient)( struct inode *inode, struct file *filp,
232 unsigned int cmd, unsigned long arg )
234 drm_file_t *priv = filp->private_data;
235 drm_device_t *dev = priv->dev;
236 drm_client_t __user *argp = (void __user *)arg;
242 if (copy_from_user(&client, argp, sizeof(client)))
245 down(&dev->struct_sem);
246 for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next)
250 up(&dev->struct_sem);
253 client.auth = pt->authenticated;
254 client.pid = pt->pid;
255 client.uid = pt->uid;
256 client.magic = pt->magic;
257 client.iocs = pt->ioctl_count;
258 up(&dev->struct_sem);
260 if (copy_to_user((drm_client_t __user *)arg, &client, sizeof(client)))
266 * Get statistics information.
268 * \param inode device inode.
269 * \param filp file pointer.
270 * \param cmd command.
271 * \param arg user argument, pointing to a drm_stats structure.
273 * \return zero on success or a negative number on failure.
275 int DRM(getstats)( struct inode *inode, struct file *filp,
276 unsigned int cmd, unsigned long arg )
278 drm_file_t *priv = filp->private_data;
279 drm_device_t *dev = priv->dev;
283 memset(&stats, 0, sizeof(stats));
285 down(&dev->struct_sem);
287 for (i = 0; i < dev->counters; i++) {
288 if (dev->types[i] == _DRM_STAT_LOCK)
291 ? dev->lock.hw_lock->lock : 0);
293 stats.data[i].value = atomic_read(&dev->counts[i]);
294 stats.data[i].type = dev->types[i];
297 stats.count = dev->counters;
299 up(&dev->struct_sem);
301 if (copy_to_user((drm_stats_t __user *)arg, &stats, sizeof(stats)))
306 #define DRM_IF_MAJOR 1
307 #define DRM_IF_MINOR 2
309 int DRM(setversion)(DRM_IOCTL_ARGS)
312 drm_set_version_t sv;
313 drm_set_version_t retv;
315 drm_set_version_t __user *argp = (void __user *)data;
317 DRM_COPY_FROM_USER_IOCTL(sv, argp, sizeof(sv));
319 retv.drm_di_major = DRM_IF_MAJOR;
320 retv.drm_di_minor = DRM_IF_MINOR;
321 retv.drm_dd_major = DRIVER_MAJOR;
322 retv.drm_dd_minor = DRIVER_MINOR;
324 DRM_COPY_TO_USER_IOCTL(argp, retv, sizeof(sv));
326 if (sv.drm_di_major != -1) {
327 if (sv.drm_di_major != DRM_IF_MAJOR ||
328 sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
330 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
331 dev->if_version = DRM_MAX(if_version, dev->if_version);
332 if (sv.drm_di_minor >= 1) {
334 * Version 1.1 includes tying of DRM to specific device
340 if (sv.drm_dd_major != -1) {
341 if (sv.drm_dd_major != DRIVER_MAJOR ||
342 sv.drm_dd_minor < 0 || sv.drm_dd_minor > DRIVER_MINOR)
345 if (dev->fn_tbl.set_version)
346 dev->fn_tbl.set_version(dev, &sv);