blob: 0b411d14d095a3abb82321dad7bd5d4cafe59bfb [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
David Dawes16bd1492002-01-27 18:23:04 +000032#include "drmP.h"
33
34static int DRM(hash_magic)(drm_magic_t magic)
35{
36 return magic & (DRM_HASH_SIZE-1);
37}
38
39static drm_file_t *DRM(find_file)(drm_device_t *dev, drm_magic_t magic)
40{
41 drm_file_t *retval = NULL;
42 drm_magic_entry_t *pt;
43 int hash = DRM(hash_magic)(magic);
44
Alan Hourihane74ef13f2002-07-05 08:31:11 +000045 DRM_LOCK;
David Dawes16bd1492002-01-27 18:23:04 +000046 for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
47 if (pt->magic == magic) {
48 retval = pt->priv;
49 break;
50 }
51 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +000052 DRM_UNLOCK;
David Dawes16bd1492002-01-27 18:23:04 +000053 return retval;
54}
55
56int DRM(add_magic)(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
57{
58 int hash;
59 drm_magic_entry_t *entry;
60
61 DRM_DEBUG("%d\n", magic);
62
63 hash = DRM(hash_magic)(magic);
64 entry = (drm_magic_entry_t*) DRM(alloc)(sizeof(*entry), DRM_MEM_MAGIC);
Alan Hourihane74ef13f2002-07-05 08:31:11 +000065 if (!entry) return DRM_ERR(ENOMEM);
Alan Hourihane46cacdc2002-03-06 19:30:45 +000066 memset(entry, 0, sizeof(*entry));
David Dawes16bd1492002-01-27 18:23:04 +000067 entry->magic = magic;
68 entry->priv = priv;
69 entry->next = NULL;
70
Alan Hourihane74ef13f2002-07-05 08:31:11 +000071 DRM_LOCK;
David Dawes16bd1492002-01-27 18:23:04 +000072 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 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +000079 DRM_UNLOCK;
David Dawes16bd1492002-01-27 18:23:04 +000080
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
Alan Hourihane74ef13f2002-07-05 08:31:11 +000093 DRM_LOCK;
David Dawes16bd1492002-01-27 18:23:04 +000094 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 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000105 DRM_UNLOCK;
David Dawes16bd1492002-01-27 18:23:04 +0000106 return 0;
107 }
108 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000109 DRM_UNLOCK;
David Dawes16bd1492002-01-27 18:23:04 +0000110
111 DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000112 return DRM_ERR(EINVAL);
David Dawes16bd1492002-01-27 18:23:04 +0000113}
114
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000115int DRM(getmagic)(DRM_IOCTL_ARGS)
David Dawes16bd1492002-01-27 18:23:04 +0000116{
117 static drm_magic_t sequence = 0;
118 drm_auth_t auth;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000119 static DRM_SPINTYPE lock;
David Dawes16bd1492002-01-27 18:23:04 +0000120 static int first = 1;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000121 DRM_DEVICE;
122 DRM_PRIV;
David Dawes16bd1492002-01-27 18:23:04 +0000123
124 if (first) {
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000125 DRM_SPININIT(lock, "drm getmagic");
David Dawes16bd1492002-01-27 18:23:04 +0000126 first = 0;
127 }
128
129 /* Find unique magic */
130 if (priv->magic) {
131 auth.magic = priv->magic;
132 } else {
133 do {
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000134 DRM_SPINLOCK(&lock);
David Dawes16bd1492002-01-27 18:23:04 +0000135 if (!sequence) ++sequence; /* reserve 0 */
136 auth.magic = sequence++;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000137 DRM_SPINUNLOCK(&lock);
David Dawes16bd1492002-01-27 18:23:04 +0000138 } while (DRM(find_file)(dev, auth.magic));
139 priv->magic = auth.magic;
140 DRM(add_magic)(dev, priv, auth.magic);
141 }
142
143 DRM_DEBUG("%u\n", auth.magic);
144
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000145 DRM_COPY_TO_USER_IOCTL((drm_auth_t *)data, auth, sizeof(auth));
David Dawes16bd1492002-01-27 18:23:04 +0000146
147 return 0;
148}
149
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000150int DRM(authmagic)(DRM_IOCTL_ARGS)
David Dawes16bd1492002-01-27 18:23:04 +0000151{
152 drm_auth_t auth;
153 drm_file_t *file;
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000154 DRM_DEVICE;
David Dawes16bd1492002-01-27 18:23:04 +0000155
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000156 DRM_COPY_FROM_USER_IOCTL(auth, (drm_auth_t *)data, sizeof(auth));
David Dawes16bd1492002-01-27 18:23:04 +0000157
158 DRM_DEBUG("%u\n", auth.magic);
159 if ((file = DRM(find_file)(dev, auth.magic))) {
160 file->authenticated = 1;
161 DRM(remove_magic)(dev, auth.magic);
162 return 0;
163 }
Alan Hourihane74ef13f2002-07-05 08:31:11 +0000164 return DRM_ERR(EINVAL);
David Dawes16bd1492002-01-27 18:23:04 +0000165}