2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
29 * $FreeBSD: src/sys/dev/drm2/drm_auth.c,v 1.1 2012/05/22 11:07:44 kib Exp $
33 * Implementation of the get/authmagic ioctls implementing the authentication
34 * scheme between the master and clients.
40 * Find the file with the given magic number.
42 * \param dev DRM device.
43 * \param magic magic number.
45 * Searches in drm_device::magiclist within all files with the same hash key
46 * the one with matching magic number, while holding the drm_device::struct_mutex
49 static struct drm_file *drm_find_file(struct drm_device *dev, drm_magic_t magic)
51 struct drm_file *retval = NULL;
52 struct drm_magic_entry *pt;
53 struct drm_hash_item *hash;
55 mutex_lock(&dev->struct_mutex);
56 if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
57 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
60 mutex_unlock(&dev->struct_mutex);
65 * Inserts the given magic number into the hash table of used magic number
68 static int drm_add_magic(struct drm_device *dev, struct drm_file *priv,
71 struct drm_magic_entry *entry;
73 DRM_DEBUG("%d\n", magic);
75 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
79 entry->hash_item.key = (unsigned long)magic;
80 mutex_lock(&dev->struct_mutex);
81 drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
82 list_add_tail(&entry->head, &dev->magicfree);
83 mutex_unlock(&dev->struct_mutex);
89 * Removes the given magic number from the hash table of used magic number
92 static int drm_remove_magic(struct drm_device *dev, drm_magic_t magic)
94 struct drm_magic_entry *pt;
95 struct drm_hash_item *hash;
97 DRM_DEBUG("%d\n", magic);
99 mutex_lock(&dev->struct_mutex);
100 if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
101 mutex_unlock(&dev->struct_mutex);
104 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
105 drm_ht_remove_item(&dev->magiclist, hash);
107 mutex_unlock(&dev->struct_mutex);
115 * Get a unique magic number (ioctl).
117 * \param inode device inode.
118 * \param file_priv DRM file private.
119 * \param cmd command.
120 * \param arg pointer to a resulting drm_auth structure.
121 * \return zero on success, or a negative number on failure.
123 * If there is a magic number in drm_file::magic then use it, otherwise
124 * searches an unique non-zero magic number and add it associating it with \p
126 * This ioctl needs protection by the drm_global_mutex, which protects
127 * struct drm_file::magic and struct drm_magic_entry::priv.
129 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
131 static drm_magic_t sequence = 0;
132 static struct spinlock lock = SPINLOCK_INITIALIZER(&lock, "drm_gm");
133 struct drm_auth *auth = data;
135 /* Find unique magic */
136 if (file_priv->magic) {
137 auth->magic = file_priv->magic;
142 ++sequence; /* reserve 0 */
143 auth->magic = sequence++;
145 } while (drm_find_file(dev, auth->magic));
146 file_priv->magic = auth->magic;
147 drm_add_magic(dev, file_priv, auth->magic);
150 DRM_DEBUG("%u\n", auth->magic);
156 * Authenticate with a magic.
158 * \param inode device inode.
159 * \param file_priv DRM file private.
160 * \param cmd command.
161 * \param arg pointer to a drm_auth structure.
162 * \return zero if authentication successed, or a negative number otherwise.
164 * Checks if \p file_priv is associated with the magic number passed in \arg.
165 * This ioctl needs protection by the drm_global_mutex, which protects
166 * struct drm_file::magic and struct drm_magic_entry::priv.
168 int drm_authmagic(struct drm_device *dev, void *data,
169 struct drm_file *file_priv)
171 struct drm_auth *auth = data;
172 struct drm_file *file;
174 DRM_DEBUG("%u\n", auth->magic);
175 if ((file = drm_find_file(dev, auth->magic))) {
176 file->authenticated = 1;
177 drm_remove_magic(dev, auth->magic);