]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_auth.c
This commit was generated by cvs2svn to compensate for changes in r150920,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_auth.c
1 /* drm_auth.h -- IOCTLs for authentication -*- linux-c -*-
2  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
3  */
4 /*-
5  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7  * All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * Authors:
29  *    Rickard E. (Rik) Faith <faith@valinux.com>
30  *    Gareth Hughes <gareth@valinux.com>
31  *
32  * $FreeBSD$
33  */
34
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;
47
48         hash = drm_hash_magic(magic);
49
50         DRM_LOCK();
51         for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
52                 if (pt->magic == magic) {
53                         retval = pt->priv;
54                         break;
55                 }
56         }
57         DRM_UNLOCK();
58         return retval;
59 }
60
61 static int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
62 {
63         int               hash;
64         drm_magic_entry_t *entry;
65
66         DRM_DEBUG("%d\n", magic);
67
68         hash = drm_hash_magic(magic);
69         entry = malloc(sizeof(*entry), M_DRM, M_ZERO | M_NOWAIT);
70         if (!entry) return DRM_ERR(ENOMEM);
71         entry->magic = magic;
72         entry->priv  = priv;
73         entry->next  = NULL;
74
75         DRM_LOCK();
76         if (dev->magiclist[hash].tail) {
77                 dev->magiclist[hash].tail->next = entry;
78                 dev->magiclist[hash].tail       = entry;
79         } else {
80                 dev->magiclist[hash].head       = entry;
81                 dev->magiclist[hash].tail       = entry;
82         }
83         DRM_UNLOCK();
84
85         return 0;
86 }
87
88 static int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
89 {
90         drm_magic_entry_t *prev = NULL;
91         drm_magic_entry_t *pt;
92         int               hash;
93
94         DRM_DEBUG("%d\n", magic);
95         hash = drm_hash_magic(magic);
96
97         DRM_LOCK();
98         for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
99                 if (pt->magic == magic) {
100                         if (dev->magiclist[hash].head == pt) {
101                                 dev->magiclist[hash].head = pt->next;
102                         }
103                         if (dev->magiclist[hash].tail == pt) {
104                                 dev->magiclist[hash].tail = prev;
105                         }
106                         if (prev) {
107                                 prev->next = pt->next;
108                         }
109                         DRM_UNLOCK();
110                         return 0;
111                 }
112         }
113         DRM_UNLOCK();
114
115         free(pt, M_DRM);
116         return DRM_ERR(EINVAL);
117 }
118
119 int drm_getmagic(DRM_IOCTL_ARGS)
120 {
121         DRM_DEVICE;
122         static drm_magic_t sequence = 0;
123         drm_auth_t auth;
124         drm_file_t *priv;
125
126         DRM_LOCK();
127         priv = drm_find_file_by_proc(dev, p);
128         DRM_UNLOCK();
129         if (priv == NULL) {
130                 DRM_ERROR("can't find authenticator\n");
131                 return EINVAL;
132         }
133
134                                 /* Find unique magic */
135         if (priv->magic) {
136                 auth.magic = priv->magic;
137         } else {
138                 do {
139                         int old = sequence;
140                         
141                         auth.magic = old+1;
142                         
143                         if (!atomic_cmpset_int(&sequence, old, auth.magic))
144                                 continue;
145                 } while (drm_find_file(dev, auth.magic));
146                 priv->magic = auth.magic;
147                 drm_add_magic(dev, priv, auth.magic);
148         }
149
150         DRM_DEBUG("%u\n", auth.magic);
151
152         DRM_COPY_TO_USER_IOCTL((drm_auth_t *)data, auth, sizeof(auth));
153
154         return 0;
155 }
156
157 int drm_authmagic(DRM_IOCTL_ARGS)
158 {
159         drm_auth_t         auth;
160         drm_file_t         *file;
161         DRM_DEVICE;
162
163         DRM_COPY_FROM_USER_IOCTL(auth, (drm_auth_t *)data, sizeof(auth));
164
165         DRM_DEBUG("%u\n", auth.magic);
166
167         if ((file = drm_find_file(dev, auth.magic))) {
168                 file->authenticated = 1;
169                 drm_remove_magic(dev, auth.magic);
170                 return 0;
171         }
172         return DRM_ERR(EINVAL);
173 }