3 * IOCTLs for authentication
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
39 * Find the file with the given magic number.
41 * \param dev DRM device.
42 * \param magic magic number.
44 * Searches in drm_device::magiclist within all files with the same hash key
45 * the one with matching magic number, while holding the drm_device::struct_sem
48 static drm_file_t *drm_find_file(drm_device_t * dev, drm_magic_t magic)
50 drm_file_t *retval = NULL;
51 drm_magic_entry_t *pt;
52 drm_hash_item_t *hash;
54 down(&dev->struct_sem);
55 if (!drm_ht_find_item(&dev->magiclist, (unsigned long) magic, &hash)) {
56 pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item);
64 * Adds a magic number.
66 * \param dev DRM device.
67 * \param priv file private data.
68 * \param magic magic number.
70 * Creates a drm_magic_entry structure and appends to the linked list
71 * associated the magic number hash key in drm_device::magiclist, while holding
72 * the drm_device::struct_sem lock.
74 static int drm_add_magic(drm_device_t *dev, drm_file_t *priv,
77 drm_magic_entry_t *entry;
79 DRM_DEBUG("%d\n", magic);
81 entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
84 memset(entry, 0, sizeof(*entry));
86 entry->hash_item.key = (unsigned long) magic;
87 down(&dev->struct_sem);
88 drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
89 list_add_tail(&entry->head, &dev->magicfree);
96 * Remove a magic number.
98 * \param dev DRM device.
99 * \param magic magic number.
101 * Searches and unlinks the entry in drm_device::magiclist with the magic
102 * number hash key, while holding the drm_device::struct_sem lock.
104 static int drm_remove_magic(drm_device_t * dev, drm_magic_t magic)
106 drm_magic_entry_t *pt;
107 drm_hash_item_t *hash;
109 DRM_DEBUG("%d\n", magic);
111 down(&dev->struct_sem);
112 if (drm_ht_find_item(&dev->magiclist, (unsigned long) magic, &hash)) {
113 up(&dev->struct_sem);
116 pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item);
117 drm_ht_remove_item(&dev->magiclist, hash);
119 up(&dev->struct_sem);
121 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
127 * Get a unique magic number (ioctl).
129 * \param inode device inode.
130 * \param filp file pointer.
131 * \param cmd command.
132 * \param arg pointer to a resulting drm_auth structure.
133 * \return zero on success, or a negative number on failure.
135 * If there is a magic number in drm_file::magic then use it, otherwise
136 * searches an unique non-zero magic number and add it associating it with \p
139 int drm_getmagic(struct inode *inode, struct file *filp,
140 unsigned int cmd, unsigned long arg)
142 static drm_magic_t sequence = 0;
143 #ifndef DEFINE_SPINLOCK
144 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
146 static DEFINE_SPINLOCK(lock);
147 drm_file_t *priv = filp->private_data;
148 drm_device_t *dev = priv->head->dev;
151 /* Find unique magic */
153 auth.magic = priv->magic;
158 ++sequence; /* reserve 0 */
159 auth.magic = sequence++;
161 } while (drm_find_file(dev, auth.magic));
162 priv->magic = auth.magic;
163 drm_add_magic(dev, priv, auth.magic);
166 DRM_DEBUG("%u\n", auth.magic);
167 if (copy_to_user((drm_auth_t __user *) arg, &auth, sizeof(auth)))
173 * Authenticate with a magic.
175 * \param inode device inode.
176 * \param filp file pointer.
177 * \param cmd command.
178 * \param arg pointer to a drm_auth structure.
179 * \return zero if authentication successed, or a negative number otherwise.
181 * Checks if \p filp is associated with the magic number passed in \arg.
183 int drm_authmagic(struct inode *inode, struct file *filp,
184 unsigned int cmd, unsigned long arg)
186 drm_file_t *priv = filp->private_data;
187 drm_device_t *dev = priv->head->dev;
191 if (copy_from_user(&auth, (drm_auth_t __user *) arg, sizeof(auth)))
193 DRM_DEBUG("%u\n", auth.magic);
194 if ((file = drm_find_file(dev, auth.magic))) {
195 file->authenticated = 1;
196 drm_remove_magic(dev, auth.magic);