Merge commit 'cb41b9c565d4eec9e1f06e24d429696f59f2f07d'
[unleashed.git] / usr / src / uts / common / io / drm / drm_auth.c
blob23fec5e112c6aba5b97e2070caa8ed1f727c50da
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * drm_auth.h -- IOCTLs for authentication -*- linux-c -*-
8 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
9 */
11 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
12 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
13 * All Rights Reserved.
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
22 * The above copyright notice and this permission notice (including the next
23 * paragraph) shall be included in all copies or substantial portions of the
24 * Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 * OTHER DEALINGS IN THE SOFTWARE.
34 * Authors:
35 * Rickard E. (Rik) Faith <faith@valinux.com>
36 * Gareth Hughes <gareth@valinux.com>
40 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include "drmP.h"
44 static int
45 drm_hash_magic(drm_magic_t magic)
47 return (magic & (DRM_HASH_SIZE-1));
50 drm_file_t *
51 drm_find_file(drm_device_t *dev, drm_magic_t magic)
53 drm_file_t *retval = NULL;
54 drm_magic_entry_t *pt;
55 int hash;
57 hash = drm_hash_magic(magic);
58 for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
59 if (pt->magic == magic) {
60 retval = pt->priv;
61 break;
65 return (retval);
68 static int
69 drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
71 int hash;
72 drm_magic_entry_t *entry;
74 hash = drm_hash_magic(magic);
75 entry = drm_alloc(sizeof (*entry), DRM_MEM_MAGIC);
76 if (!entry)
77 return (ENOMEM);
78 entry->magic = magic;
79 entry->priv = priv;
80 entry->next = NULL;
82 DRM_LOCK();
83 if (dev->magiclist[hash].tail) {
84 dev->magiclist[hash].tail->next = entry;
85 dev->magiclist[hash].tail = entry;
86 } else {
87 dev->magiclist[hash].head = entry;
88 dev->magiclist[hash].tail = entry;
90 DRM_UNLOCK();
92 return (0);
95 int
96 drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
98 drm_magic_entry_t *prev = NULL;
99 drm_magic_entry_t *pt;
100 int hash;
102 DRM_DEBUG("drm_remove_magic : %d", magic);
103 hash = drm_hash_magic(magic);
105 DRM_LOCK();
106 for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
107 if (pt->magic == magic) {
108 if (dev->magiclist[hash].head == pt) {
109 dev->magiclist[hash].head = pt->next;
111 if (dev->magiclist[hash].tail == pt) {
112 dev->magiclist[hash].tail = prev;
114 if (prev) {
115 prev->next = pt->next;
117 DRM_UNLOCK();
118 drm_free(pt, sizeof (*pt), DRM_MEM_MAGIC);
119 return (0);
122 DRM_UNLOCK();
124 return (EINVAL);
127 /*ARGSUSED*/
129 drm_getmagic(DRM_IOCTL_ARGS)
131 DRM_DEVICE;
132 static drm_magic_t sequence = 0;
133 drm_auth_t auth;
135 /* Find unique magic */
136 if (fpriv->magic) {
137 auth.magic = fpriv->magic;
138 } else {
139 do {
140 int old = sequence;
141 auth.magic = old+1;
142 if (!atomic_cmpset_int(&sequence, old, auth.magic))
143 continue;
144 } while (drm_find_file(dev, auth.magic));
145 fpriv->magic = auth.magic;
146 (void) drm_add_magic(dev, fpriv, auth.magic);
150 DRM_DEBUG("drm_getmagic: %u", auth.magic);
152 DRM_COPYTO_WITH_RETURN((void *)data, &auth, sizeof (auth));
154 return (0);
157 /*ARGSUSED*/
159 drm_authmagic(DRM_IOCTL_ARGS)
161 drm_auth_t auth;
162 drm_file_t *file;
163 DRM_DEVICE;
165 DRM_COPYFROM_WITH_RETURN(&auth, (void *)data, sizeof (auth));
167 if ((file = drm_find_file(dev, auth.magic))) {
168 file->authenticated = 1;
169 (void) drm_remove_magic(dev, auth.magic);
170 return (0);
172 return (EINVAL);