Import 2.3.18pre1
[davej-history.git] / drivers / char / drm / auth.c
blob2561264ff25d52bd6eadf320bbdb439a3e0d40c3
1 /* auth.c -- IOCTLs for authentication -*- linux-c -*-
2 * Created: Tue Feb 2 08:37:54 1999 by faith@precisioninsight.com
3 * Revised: Fri Aug 20 11:31:48 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
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:
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.
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 * PRECISION INSIGHT 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 OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/generic/auth.c,v 1.4 1999/08/30 13:05:00 faith Exp $
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/generic/gen_ioctl.c,v 1.2 1999/06/27 14:08:27 dawes Exp $
32 #define __NO_VERSION__
33 #include "drmP.h"
35 static int drm_hash_magic(drm_magic_t magic)
37 return magic & (DRM_HASH_SIZE-1);
40 static drm_file_t *drm_find_file(drm_device_t *dev, drm_magic_t magic)
42 drm_file_t *retval = NULL;
43 drm_magic_entry_t *pt;
44 int hash = drm_hash_magic(magic);
46 down(&dev->struct_sem);
47 for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
48 if (pt->priv->authenticated) continue;
49 if (pt->magic == magic) {
50 retval = pt->priv;
51 break;
54 up(&dev->struct_sem);
55 return retval;
58 int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
60 int hash;
61 drm_magic_entry_t *entry;
63 DRM_DEBUG("%d\n", magic);
65 hash = drm_hash_magic(magic);
66 entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
67 if (!entry) return -ENOMEM;
68 entry->magic = magic;
69 entry->priv = priv;
70 entry->next = NULL;
72 down(&dev->struct_sem);
73 if (dev->magiclist[hash].tail) {
74 dev->magiclist[hash].tail->next = entry;
75 dev->magiclist[hash].tail = entry;
76 } else {
77 dev->magiclist[hash].head = entry;
78 dev->magiclist[hash].tail = entry;
80 up(&dev->struct_sem);
82 return 0;
85 int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
87 drm_magic_entry_t *prev = NULL;
88 drm_magic_entry_t *pt;
89 int hash;
91 DRM_DEBUG("%d\n", magic);
92 hash = drm_hash_magic(magic);
94 down(&dev->struct_sem);
95 for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
96 if (pt->magic == magic) {
97 if (dev->magiclist[hash].head == pt) {
98 dev->magiclist[hash].head = pt->next;
100 if (dev->magiclist[hash].tail == pt) {
101 dev->magiclist[hash].tail = prev;
103 if (prev) {
104 prev->next = pt->next;
106 up(&dev->struct_sem);
107 return 0;
110 up(&dev->struct_sem);
112 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
114 return -EINVAL;
117 int drm_getmagic(struct inode *inode, struct file *filp, unsigned int cmd,
118 unsigned long arg)
120 static drm_magic_t sequence = 0;
121 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
122 drm_file_t *priv = filp->private_data;
123 drm_device_t *dev = priv->dev;
124 drm_auth_t auth;
126 /* Find unique magic */
127 if (priv->magic) {
128 auth.magic = priv->magic;
129 } else {
130 spin_lock(&lock);
131 do {
132 if (!sequence) ++sequence; /* reserve 0 */
133 auth.magic = sequence++;
134 } while (drm_find_file(dev, auth.magic));
135 spin_unlock(&lock);
136 priv->magic = auth.magic;
137 drm_add_magic(dev, priv, auth.magic);
140 DRM_DEBUG("%u\n", auth.magic);
141 copy_to_user_ret((drm_auth_t *)arg, &auth, sizeof(auth), -EFAULT);
142 return 0;
145 int drm_authmagic(struct inode *inode, struct file *filp, unsigned int cmd,
146 unsigned long arg)
148 drm_file_t *priv = filp->private_data;
149 drm_device_t *dev = priv->dev;
150 drm_auth_t auth;
151 drm_file_t *file;
153 copy_from_user_ret(&auth, (drm_auth_t *)arg, sizeof(auth), -EFAULT);
154 DRM_DEBUG("%u\n", auth.magic);
155 if ((file = drm_find_file(dev, auth.magic))) {
156 file->authenticated = 1;
157 drm_remove_magic(dev, auth.magic);
158 return 0;
160 return -EINVAL;