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.
38 struct drm_magic_entry {
39 struct list_head head;
40 struct drm_hash_item hash_item;
41 struct drm_file *priv;
45 * Find the file with the given magic number.
47 * \param dev DRM device.
48 * \param magic magic number.
50 * Searches in drm_device::magiclist within all files with the same hash key
51 * the one with matching magic number, while holding the drm_device::struct_mutex
54 static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
56 struct drm_file *retval = NULL;
57 struct drm_magic_entry *pt;
58 struct drm_hash_item *hash;
59 struct drm_device *dev = master->minor->dev;
61 mutex_lock(&dev->struct_mutex);
62 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
63 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
66 mutex_unlock(&dev->struct_mutex);
71 * Adds a magic number.
73 * \param dev DRM device.
74 * \param priv file private data.
75 * \param magic magic number.
77 * Creates a drm_magic_entry structure and appends to the linked list
78 * associated the magic number hash key in drm_device::magiclist, while holding
79 * the drm_device::struct_mutex lock.
81 static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
84 struct drm_magic_entry *entry;
85 struct drm_device *dev = master->minor->dev;
86 DRM_DEBUG("%d\n", magic);
88 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
92 entry->hash_item.key = (unsigned long)magic;
93 mutex_lock(&dev->struct_mutex);
94 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
95 list_add_tail(&entry->head, &master->magicfree);
96 mutex_unlock(&dev->struct_mutex);
102 * Remove a magic number.
104 * \param dev DRM device.
105 * \param magic magic number.
107 * Searches and unlinks the entry in drm_device::magiclist with the magic
108 * number hash key, while holding the drm_device::struct_mutex lock.
110 int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
112 struct drm_magic_entry *pt;
113 struct drm_hash_item *hash;
114 struct drm_device *dev = master->minor->dev;
116 DRM_DEBUG("%d\n", magic);
118 mutex_lock(&dev->struct_mutex);
119 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
120 mutex_unlock(&dev->struct_mutex);
123 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
124 drm_ht_remove_item(&master->magiclist, hash);
126 mutex_unlock(&dev->struct_mutex);
134 * Get a unique magic number (ioctl).
136 * \param inode device inode.
137 * \param file_priv DRM file private.
138 * \param cmd command.
139 * \param arg pointer to a resulting drm_auth structure.
140 * \return zero on success, or a negative number on failure.
142 * If there is a magic number in drm_file::magic then use it, otherwise
143 * searches an unique non-zero magic number and add it associating it with \p
145 * This ioctl needs protection by the drm_global_mutex, which protects
146 * struct drm_file::magic and struct drm_magic_entry::priv.
148 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
150 static drm_magic_t sequence = 0;
151 static DEFINE_SPINLOCK(lock);
152 struct drm_auth *auth = data;
154 /* Find unique magic */
155 if (file_priv->magic) {
156 auth->magic = file_priv->magic;
161 ++sequence; /* reserve 0 */
162 auth->magic = sequence++;
164 } while (drm_find_file(file_priv->master, auth->magic));
165 file_priv->magic = auth->magic;
166 drm_add_magic(file_priv->master, file_priv, auth->magic);
169 DRM_DEBUG("%u\n", auth->magic);
175 * Authenticate with a magic.
177 * \param inode device inode.
178 * \param file_priv DRM file private.
179 * \param cmd command.
180 * \param arg pointer to a drm_auth structure.
181 * \return zero if authentication successed, or a negative number otherwise.
183 * Checks if \p file_priv is associated with the magic number passed in \arg.
184 * This ioctl needs protection by the drm_global_mutex, which protects
185 * struct drm_file::magic and struct drm_magic_entry::priv.
187 int drm_authmagic(struct drm_device *dev, void *data,
188 struct drm_file *file_priv)
190 struct drm_auth *auth = data;
191 struct drm_file *file;
193 DRM_DEBUG("%u\n", auth->magic);
194 if ((file = drm_find_file(file_priv->master, auth->magic))) {
195 file->authenticated = 1;
196 drm_remove_magic(file_priv->master, auth->magic);