drm: Sync a few bits with Linux 4.7.10
[dragonfly.git] / sys / dev / drm / drm_auth.c
blob8a47ba4cb51ce7191a4c60dcd68bf5da710641c5
1 /**
2 * \file drm_auth.c
3 * IOCTLs for authentication
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
9 /*
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
25 * Software.
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.
36 #include <drm/drmP.h>
37 #include "drm_internal.h"
39 /**
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
47 * lock.
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);
58 retval = pt->priv;
60 mutex_unlock(&dev->struct_mutex);
61 return retval;
64 /**
65 * Inserts the given magic number into the hash table of used magic number
66 * lists.
68 static int drm_add_magic(struct drm_device *dev, struct drm_file *priv,
69 drm_magic_t magic)
71 struct drm_magic_entry *entry;
73 DRM_DEBUG("%d\n", magic);
75 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
76 if (!entry)
77 return -ENOMEM;
78 entry->priv = priv;
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);
85 return 0;
88 /**
89 * Removes the given magic number from the hash table of used magic number
90 * lists.
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 if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
100 return -EINVAL;
102 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
103 drm_ht_remove_item(&dev->magiclist, hash);
104 list_del(&pt->head);
106 kfree(pt);
108 return 0;
112 * Get a unique magic number (ioctl).
114 * \param inode device inode.
115 * \param file_priv DRM file private.
116 * \param cmd command.
117 * \param arg pointer to a resulting drm_auth structure.
118 * \return zero on success, or a negative number on failure.
120 * If there is a magic number in drm_file::magic then use it, otherwise
121 * searches an unique non-zero magic number and add it associating it with \p
122 * file_priv.
123 * This ioctl needs protection by the drm_global_mutex, which protects
124 * struct drm_file::magic and struct drm_magic_entry::priv.
126 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
128 static drm_magic_t sequence = 0;
129 static struct spinlock lock = SPINLOCK_INITIALIZER(&lock, "drm_gm");
130 struct drm_auth *auth = data;
132 /* Find unique magic */
133 if (file_priv->magic) {
134 auth->magic = file_priv->magic;
135 } else {
136 do {
137 spin_lock(&lock);
138 if (!sequence)
139 ++sequence; /* reserve 0 */
140 auth->magic = sequence++;
141 spin_unlock(&lock);
142 } while (drm_find_file(dev, auth->magic));
143 file_priv->magic = auth->magic;
144 drm_add_magic(dev, file_priv, auth->magic);
147 DRM_DEBUG("%u\n", auth->magic);
149 return 0;
153 * drm_authmagic - Authenticate client with a magic
154 * @dev: DRM device to operate on
155 * @data: ioctl data containing the drm_auth object
156 * @file_priv: DRM file that performs the operation
158 * This looks up a DRM client by the passed magic and authenticates it.
160 * Returns: 0 on success, negative error code on failure.
162 int drm_authmagic(struct drm_device *dev, void *data,
163 struct drm_file *file_priv)
165 struct drm_auth *auth = data;
166 struct drm_file *file;
168 DRM_DEBUG("%u\n", auth->magic);
170 mutex_lock(&dev->struct_mutex);
171 file = drm_find_file(dev, auth->magic);
172 if (file) {
173 file->authenticated = 1;
174 drm_remove_magic(dev, auth->magic);
176 mutex_unlock(&dev->struct_mutex);
178 return file ? 0 : -EINVAL;