Use a globaldata_t instead of a cpuid in the lwkt_token structure. The
[dragonfly.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  * $FreeBSD: src/sys/dev/drm/drm_auth.h,v 1.3.2.1 2003/04/26 07:05:28 anholt Exp $
31  * $DragonFly: src/sys/dev/drm/Attic/drm_auth.h,v 1.2 2003/06/17 04:28:24 dillon Exp $
32  */
33
34 #include "dev/drm/drmP.h"
35
36 static int DRM(hash_magic)(drm_magic_t magic)
37 {
38         return magic & (DRM_HASH_SIZE-1);
39 }
40
41 static drm_file_t *DRM(find_file)(drm_device_t *dev, drm_magic_t magic)
42 {
43         drm_file_t        *retval = NULL;
44         drm_magic_entry_t *pt;
45         int               hash    = DRM(hash_magic)(magic);
46
47         DRM_LOCK;
48         for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
49                 if (pt->magic == magic) {
50                         retval = pt->priv;
51                         break;
52                 }
53         }
54         DRM_UNLOCK;
55         return retval;
56 }
57
58 int DRM(add_magic)(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
59 {
60         int               hash;
61         drm_magic_entry_t *entry;
62
63         DRM_DEBUG("%d\n", magic);
64
65         hash         = DRM(hash_magic)(magic);
66         entry        = (drm_magic_entry_t*) DRM(alloc)(sizeof(*entry), DRM_MEM_MAGIC);
67         if (!entry) return DRM_ERR(ENOMEM);
68         memset(entry, 0, sizeof(*entry));
69         entry->magic = magic;
70         entry->priv  = priv;
71         entry->next  = NULL;
72
73         DRM_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_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_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_UNLOCK;
108                         return 0;
109                 }
110         }
111         DRM_UNLOCK;
112
113         DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
114         return DRM_ERR(EINVAL);
115 }
116
117 int DRM(getmagic)(DRM_IOCTL_ARGS)
118 {
119         static drm_magic_t sequence = 0;
120         drm_auth_t         auth;
121         DRM_DEVICE;
122         DRM_PRIV;
123
124                                 /* Find unique magic */
125         if (priv->magic) {
126                 auth.magic = priv->magic;
127         } else {
128                 do {
129                         int old = sequence;
130                         
131                         auth.magic = old+1;
132                         
133                         if (!atomic_cmpset_int(&sequence, old, auth.magic))
134                                 continue;
135                 } while (DRM(find_file)(dev, auth.magic));
136                 priv->magic = auth.magic;
137                 DRM(add_magic)(dev, priv, auth.magic);
138         }
139
140         DRM_DEBUG("%u\n", auth.magic);
141
142         DRM_COPY_TO_USER_IOCTL((drm_auth_t *)data, auth, sizeof(auth));
143
144         return 0;
145 }
146
147 int DRM(authmagic)(DRM_IOCTL_ARGS)
148 {
149         drm_auth_t         auth;
150         drm_file_t         *file;
151         DRM_DEVICE;
152
153         DRM_COPY_FROM_USER_IOCTL(auth, (drm_auth_t *)data, sizeof(auth));
154
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;
160         }
161         return DRM_ERR(EINVAL);
162 }