]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_auth.h
This commit was generated by cvs2svn to compensate for changes in r99146,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_auth.h
1 /* 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  * $FreeBSD$
32  */
33
34 #define __NO_VERSION__
35 #include "dev/drm/drmP.h"
36
37 static int DRM(hash_magic)(drm_magic_t magic)
38 {
39         return magic & (DRM_HASH_SIZE-1);
40 }
41
42 static drm_file_t *DRM(find_file)(drm_device_t *dev, drm_magic_t magic)
43 {
44         drm_file_t        *retval = NULL;
45         drm_magic_entry_t *pt;
46         int               hash    = DRM(hash_magic)(magic);
47
48         DRM_OS_LOCK;
49         for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
50                 if (pt->magic == magic) {
51                         retval = pt->priv;
52                         break;
53                 }
54         }
55         DRM_OS_UNLOCK;
56         return retval;
57 }
58
59 int DRM(add_magic)(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
60 {
61         int               hash;
62         drm_magic_entry_t *entry;
63
64         DRM_DEBUG("%d\n", magic);
65
66         hash         = DRM(hash_magic)(magic);
67         entry        = (drm_magic_entry_t*) DRM(alloc)(sizeof(*entry), DRM_MEM_MAGIC);
68         if (!entry) return DRM_OS_ERR(ENOMEM);
69         entry->magic = magic;
70         entry->priv  = priv;
71         entry->next  = NULL;
72
73         DRM_OS_LOCK;
74         if (dev->magiclist[hash].tail) {
75                 dev->magiclist[hash].tail->next = entry;
76                 dev->magiclist[hash].tail       = entry;
77         } else {
78                 dev->magiclist[hash].head       = entry;
79                 dev->magiclist[hash].tail       = entry;
80         }
81         DRM_OS_UNLOCK;
82
83         return 0;
84 }
85
86 int DRM(remove_magic)(drm_device_t *dev, drm_magic_t magic)
87 {
88         drm_magic_entry_t *prev = NULL;
89         drm_magic_entry_t *pt;
90         int               hash;
91
92         DRM_DEBUG("%d\n", magic);
93         hash = DRM(hash_magic)(magic);
94
95         DRM_OS_LOCK;
96         for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
97                 if (pt->magic == magic) {
98                         if (dev->magiclist[hash].head == pt) {
99                                 dev->magiclist[hash].head = pt->next;
100                         }
101                         if (dev->magiclist[hash].tail == pt) {
102                                 dev->magiclist[hash].tail = prev;
103                         }
104                         if (prev) {
105                                 prev->next = pt->next;
106                         }
107                         DRM_OS_UNLOCK;
108 #ifdef __FreeBSD__
109                         DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
110 #endif /* __FreeBSD__ */
111                         return 0;
112                 }
113         }
114         DRM_OS_UNLOCK;
115
116         DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
117         return DRM_OS_ERR(EINVAL);
118 }
119
120 int DRM(getmagic)(DRM_OS_IOCTL)
121 {
122         static drm_magic_t sequence = 0;
123         drm_auth_t         auth;
124 #ifdef __linux__
125         static spinlock_t  lock     = SPIN_LOCK_UNLOCKED;
126 #endif /* __linux__ */
127 #ifdef __FreeBSD__
128         static DRM_OS_SPINTYPE lock;
129         static int         first = 1;
130 #endif /* __FreeBSD__ */
131         DRM_OS_DEVICE;
132         DRM_OS_PRIV;
133
134 #ifdef __FreeBSD__
135         if (first) {
136                 DRM_OS_SPININIT(lock, "drm getmagic");
137                 first = 0;
138         }
139 #endif /* __FreeBSD__ */
140
141                                 /* Find unique magic */
142         if (priv->magic) {
143                 auth.magic = priv->magic;
144         } else {
145                 do {
146                         DRM_OS_SPINLOCK(&lock);
147                         if (!sequence) ++sequence; /* reserve 0 */
148                         auth.magic = sequence++;
149                         DRM_OS_SPINUNLOCK(&lock);
150                 } while (DRM(find_file)(dev, auth.magic));
151                 priv->magic = auth.magic;
152                 DRM(add_magic)(dev, priv, auth.magic);
153         }
154
155         DRM_DEBUG("%u\n", auth.magic);
156
157         DRM_OS_KRNTOUSR((drm_auth_t *)data, auth, sizeof(auth));
158
159         return 0;
160 }
161
162 int DRM(authmagic)(DRM_OS_IOCTL)
163 {
164         drm_auth_t         auth;
165         drm_file_t         *file;
166         DRM_OS_DEVICE;
167
168         DRM_OS_KRNFROMUSR(auth, (drm_auth_t *)data, sizeof(auth));
169
170         DRM_DEBUG("%u\n", auth.magic);
171         if ((file = DRM(find_file)(dev, auth.magic))) {
172                 file->authenticated = 1;
173                 DRM(remove_magic)(dev, auth.magic);
174                 return 0;
175         }
176         return DRM_OS_ERR(EINVAL);
177 }