]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_auth.c
This commit was generated by cvs2svn to compensate for changes in r157016,
[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  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "dev/drm/drmP.h"
38
39 static int drm_hash_magic(drm_magic_t magic)
40 {
41         return magic & (DRM_HASH_SIZE-1);
42 }
43
44 static drm_file_t *drm_find_file(drm_device_t *dev, drm_magic_t magic)
45 {
46         drm_file_t        *retval = NULL;
47         drm_magic_entry_t *pt;
48         int               hash;
49
50         hash = drm_hash_magic(magic);
51
52         DRM_LOCK();
53         for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
54                 if (pt->magic == magic) {
55                         retval = pt->priv;
56                         break;
57                 }
58         }
59         DRM_UNLOCK();
60         return retval;
61 }
62
63 static int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
64 {
65         int               hash;
66         drm_magic_entry_t *entry;
67
68         DRM_DEBUG("%d\n", magic);
69
70         hash = drm_hash_magic(magic);
71         entry = malloc(sizeof(*entry), M_DRM, M_ZERO | M_NOWAIT);
72         if (!entry) return DRM_ERR(ENOMEM);
73         entry->magic = magic;
74         entry->priv  = priv;
75         entry->next  = NULL;
76
77         DRM_LOCK();
78         if (dev->magiclist[hash].tail) {
79                 dev->magiclist[hash].tail->next = entry;
80                 dev->magiclist[hash].tail       = entry;
81         } else {
82                 dev->magiclist[hash].head       = entry;
83                 dev->magiclist[hash].tail       = entry;
84         }
85         DRM_UNLOCK();
86
87         return 0;
88 }
89
90 static int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
91 {
92         drm_magic_entry_t *prev = NULL;
93         drm_magic_entry_t *pt;
94         int               hash;
95
96         DRM_DEBUG("%d\n", magic);
97         hash = drm_hash_magic(magic);
98
99         DRM_LOCK();
100         for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
101                 if (pt->magic == magic) {
102                         if (dev->magiclist[hash].head == pt) {
103                                 dev->magiclist[hash].head = pt->next;
104                         }
105                         if (dev->magiclist[hash].tail == pt) {
106                                 dev->magiclist[hash].tail = prev;
107                         }
108                         if (prev) {
109                                 prev->next = pt->next;
110                         }
111                         DRM_UNLOCK();
112                         return 0;
113                 }
114         }
115         DRM_UNLOCK();
116
117         free(pt, M_DRM);
118         return DRM_ERR(EINVAL);
119 }
120
121 int drm_getmagic(DRM_IOCTL_ARGS)
122 {
123         DRM_DEVICE;
124         static drm_magic_t sequence = 0;
125         drm_auth_t auth;
126         drm_file_t *priv;
127
128         DRM_LOCK();
129         priv = drm_find_file_by_proc(dev, p);
130         DRM_UNLOCK();
131         if (priv == NULL) {
132                 DRM_ERROR("can't find authenticator\n");
133                 return EINVAL;
134         }
135
136                                 /* Find unique magic */
137         if (priv->magic) {
138                 auth.magic = priv->magic;
139         } else {
140                 do {
141                         int old = sequence;
142                         
143                         auth.magic = old+1;
144                         
145                         if (!atomic_cmpset_int(&sequence, old, auth.magic))
146                                 continue;
147                 } while (drm_find_file(dev, auth.magic));
148                 priv->magic = auth.magic;
149                 drm_add_magic(dev, priv, auth.magic);
150         }
151
152         DRM_DEBUG("%u\n", auth.magic);
153
154         DRM_COPY_TO_USER_IOCTL((drm_auth_t *)data, auth, sizeof(auth));
155
156         return 0;
157 }
158
159 int drm_authmagic(DRM_IOCTL_ARGS)
160 {
161         drm_auth_t         auth;
162         drm_file_t         *file;
163         DRM_DEVICE;
164
165         DRM_COPY_FROM_USER_IOCTL(auth, (drm_auth_t *)data, sizeof(auth));
166
167         DRM_DEBUG("%u\n", auth.magic);
168
169         if ((file = drm_find_file(dev, auth.magic))) {
170                 file->authenticated = 1;
171                 drm_remove_magic(dev, auth.magic);
172                 return 0;
173         }
174         return DRM_ERR(EINVAL);
175 }