Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / char / drm / auth.c
blob80bb4b651b3142940f96a35df0a51077e82e0d45
1 /* auth.c -- IOCTLs for authentication -*- linux-c -*-
2 * Created: Tue Feb 2 08:37:54 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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 * Authors:
28 * Rickard E. (Rik) Faith <faith@valinux.com>
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->magic == magic) {
49 retval = pt->priv;
50 break;
53 up(&dev->struct_sem);
54 return retval;
57 int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
59 int hash;
60 drm_magic_entry_t *entry;
62 DRM_DEBUG("%d\n", magic);
64 hash = drm_hash_magic(magic);
65 entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
66 if (!entry) return -ENOMEM;
67 entry->magic = magic;
68 entry->priv = priv;
69 entry->next = NULL;
71 down(&dev->struct_sem);
72 if (dev->magiclist[hash].tail) {
73 dev->magiclist[hash].tail->next = entry;
74 dev->magiclist[hash].tail = entry;
75 } else {
76 dev->magiclist[hash].head = entry;
77 dev->magiclist[hash].tail = entry;
79 up(&dev->struct_sem);
81 return 0;
84 int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
86 drm_magic_entry_t *prev = NULL;
87 drm_magic_entry_t *pt;
88 int hash;
90 DRM_DEBUG("%d\n", magic);
91 hash = drm_hash_magic(magic);
93 down(&dev->struct_sem);
94 for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
95 if (pt->magic == magic) {
96 if (dev->magiclist[hash].head == pt) {
97 dev->magiclist[hash].head = pt->next;
99 if (dev->magiclist[hash].tail == pt) {
100 dev->magiclist[hash].tail = prev;
102 if (prev) {
103 prev->next = pt->next;
105 up(&dev->struct_sem);
106 return 0;
109 up(&dev->struct_sem);
111 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
113 return -EINVAL;
116 int drm_getmagic(struct inode *inode, struct file *filp, unsigned int cmd,
117 unsigned long arg)
119 static drm_magic_t sequence = 0;
120 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
121 drm_file_t *priv = filp->private_data;
122 drm_device_t *dev = priv->dev;
123 drm_auth_t auth;
125 /* Find unique magic */
126 if (priv->magic) {
127 auth.magic = priv->magic;
128 } else {
129 do {
130 spin_lock(&lock);
131 if (!sequence) ++sequence; /* reserve 0 */
132 auth.magic = sequence++;
133 spin_unlock(&lock);
134 } while (drm_find_file(dev, auth.magic));
135 priv->magic = auth.magic;
136 drm_add_magic(dev, priv, auth.magic);
139 DRM_DEBUG("%u\n", auth.magic);
140 if (copy_to_user((drm_auth_t *)arg, &auth, sizeof(auth)))
141 return -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 if (copy_from_user(&auth, (drm_auth_t *)arg, sizeof(auth)))
154 return -EFAULT;
155 DRM_DEBUG("%u\n", auth.magic);
156 if ((file = drm_find_file(dev, auth.magic))) {
157 file->authenticated = 1;
158 drm_remove_magic(dev, auth.magic);
159 return 0;
161 return -EINVAL;