1 /* auth.c -- IOCTLs for authentication -*- linux-c -*-
2 * Created: Tue Feb 2 08:37:54 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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 * PRECISION INSIGHT 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 OTHER
25 * DEALINGS IN THE SOFTWARE.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
32 #define __NO_VERSION__
35 static int drm_hash_magic(drm_magic_t magic
)
37 return magic
& (DRM_HASH_SIZE
-1);
40 static drm_file_t
*drm_find_file(drm_device_t
*dev
, drm_magic_t magic
)
42 drm_file_t
*retval
= NULL
;
43 drm_magic_entry_t
*pt
;
44 int hash
= drm_hash_magic(magic
);
46 down(&dev
->struct_sem
);
47 for (pt
= dev
->magiclist
[hash
].head
; pt
; pt
= pt
->next
) {
48 if (pt
->magic
== magic
) {
57 int drm_add_magic(drm_device_t
*dev
, drm_file_t
*priv
, drm_magic_t magic
)
60 drm_magic_entry_t
*entry
;
62 DRM_DEBUG("%d\n", magic
);
64 hash
= drm_hash_magic(magic
);
65 entry
= drm_alloc(sizeof(*entry
), DRM_MEM_MAGIC
);
66 if (!entry
) return -ENOMEM
;
71 down(&dev
->struct_sem
);
72 if (dev
->magiclist
[hash
].tail
) {
73 dev
->magiclist
[hash
].tail
->next
= entry
;
74 dev
->magiclist
[hash
].tail
= entry
;
76 dev
->magiclist
[hash
].head
= entry
;
77 dev
->magiclist
[hash
].tail
= entry
;
84 int drm_remove_magic(drm_device_t
*dev
, drm_magic_t magic
)
86 drm_magic_entry_t
*prev
= NULL
;
87 drm_magic_entry_t
*pt
;
90 DRM_DEBUG("%d\n", magic
);
91 hash
= drm_hash_magic(magic
);
93 down(&dev
->struct_sem
);
94 for (pt
= dev
->magiclist
[hash
].head
; pt
; prev
= pt
, pt
= pt
->next
) {
95 if (pt
->magic
== magic
) {
96 if (dev
->magiclist
[hash
].head
== pt
) {
97 dev
->magiclist
[hash
].head
= pt
->next
;
99 if (dev
->magiclist
[hash
].tail
== pt
) {
100 dev
->magiclist
[hash
].tail
= prev
;
103 prev
->next
= pt
->next
;
105 up(&dev
->struct_sem
);
109 up(&dev
->struct_sem
);
111 drm_free(pt
, sizeof(*pt
), DRM_MEM_MAGIC
);
116 int drm_getmagic(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
119 static drm_magic_t sequence
= 0;
120 static spinlock_t lock
= SPIN_LOCK_UNLOCKED
;
121 drm_file_t
*priv
= filp
->private_data
;
122 drm_device_t
*dev
= priv
->dev
;
125 /* Find unique magic */
127 auth
.magic
= priv
->magic
;
131 if (!sequence
) ++sequence
; /* reserve 0 */
132 auth
.magic
= sequence
++;
134 } while (drm_find_file(dev
, auth
.magic
));
135 priv
->magic
= auth
.magic
;
136 drm_add_magic(dev
, priv
, auth
.magic
);
139 DRM_DEBUG("%u\n", auth
.magic
);
140 if (copy_to_user((drm_auth_t
*)arg
, &auth
, sizeof(auth
)))
145 int drm_authmagic(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
148 drm_file_t
*priv
= filp
->private_data
;
149 drm_device_t
*dev
= priv
->dev
;
153 if (copy_from_user(&auth
, (drm_auth_t
*)arg
, sizeof(auth
)))
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
);