blob: f2c2d8da438b3012adc077505333ffb5b02dffe0 [file] [log] [blame]
David Dawes16bd1492002-01-27 18:23:04 +00001/* drm_auth.h -- IOCTLs for authentication -*- linux-c -*-
2 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
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:
14 *
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.
18 *
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 * VA LINUX SYSTEMS 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
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
30 */
31
32#define __NO_VERSION__
33#include "drmP.h"
34
35static int DRM(hash_magic)(drm_magic_t magic)
36{
37 return magic & (DRM_HASH_SIZE-1);
38}
39
40static drm_file_t *DRM(find_file)(drm_device_t *dev, drm_magic_t magic)
41{
42 drm_file_t *retval = NULL;
43 drm_magic_entry_t *pt;
44 int hash = DRM(hash_magic)(magic);
45
46 DRM_OS_LOCK;
47 for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
48 if (pt->magic == magic) {
49 retval = pt->priv;
50 break;
51 }
52 }
53 DRM_OS_UNLOCK;
54 return retval;
55}
56
57int DRM(add_magic)(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
58{
59 int hash;
60 drm_magic_entry_t *entry;
61
62 DRM_DEBUG("%d\n", magic);
63
64 hash = DRM(hash_magic)(magic);
65 entry = (drm_magic_entry_t*) DRM(alloc)(sizeof(*entry), DRM_MEM_MAGIC);
66 if (!entry) DRM_OS_RETURN(ENOMEM);
67 entry->magic = magic;
68 entry->priv = priv;
69 entry->next = NULL;
70
71 DRM_OS_LOCK;
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;
78 }
79 DRM_OS_UNLOCK;
80
81 return 0;
82}
83
84int DRM(remove_magic)(drm_device_t *dev, drm_magic_t magic)
85{
86 drm_magic_entry_t *prev = NULL;
87 drm_magic_entry_t *pt;
88 int hash;
89
90 DRM_DEBUG("%d\n", magic);
91 hash = DRM(hash_magic)(magic);
92
93 DRM_OS_LOCK;
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;
98 }
99 if (dev->magiclist[hash].tail == pt) {
100 dev->magiclist[hash].tail = prev;
101 }
102 if (prev) {
103 prev->next = pt->next;
104 }
105 DRM_OS_UNLOCK;
106 DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
107 return 0;
108 }
109 }
110 DRM_OS_UNLOCK;
111
112 DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
113 DRM_OS_RETURN(EINVAL);
114}
115
116int DRM(getmagic)(DRM_OS_IOCTL)
117{
118 static drm_magic_t sequence = 0;
119 drm_auth_t auth;
120 static DRM_OS_SPINTYPE lock;
121 static int first = 1;
122 DRM_OS_DEVICE;
123 DRM_OS_PRIV;
124
125 if (first) {
126 DRM_OS_SPININIT(lock, "drm getmagic");
127 first = 0;
128 }
129
130 /* Find unique magic */
131 if (priv->magic) {
132 auth.magic = priv->magic;
133 } else {
134 do {
135 DRM_OS_SPINLOCK(&lock);
136 if (!sequence) ++sequence; /* reserve 0 */
137 auth.magic = sequence++;
138 DRM_OS_SPINUNLOCK(&lock);
139 } while (DRM(find_file)(dev, auth.magic));
140 priv->magic = auth.magic;
141 DRM(add_magic)(dev, priv, auth.magic);
142 }
143
144 DRM_DEBUG("%u\n", auth.magic);
145
146 DRM_OS_KRNTOUSR((drm_auth_t *)data, auth, sizeof(auth));
147
148 return 0;
149}
150
151int DRM(authmagic)(DRM_OS_IOCTL)
152{
153 drm_auth_t auth;
154 drm_file_t *file;
155 DRM_OS_DEVICE;
156
157 DRM_OS_KRNFROMUSR(auth, (drm_auth_t *)data, sizeof(auth));
158
159 DRM_DEBUG("%u\n", auth.magic);
160 if ((file = DRM(find_file)(dev, auth.magic))) {
161 file->authenticated = 1;
162 DRM(remove_magic)(dev, auth.magic);
163 return 0;
164 }
165 DRM_OS_RETURN(EINVAL);
166}