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_mutex
48
static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
50
struct drm_file *retval = NULL;
51
struct drm_magic_entry *pt;
52
struct drm_hash_item *hash;
53
struct drm_device *dev = master->minor->dev;
55
mutex_lock(&dev->struct_mutex);
56
if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
57
pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
60
mutex_unlock(&dev->struct_mutex);
65
* Adds a magic number.
67
* \param dev DRM device.
68
* \param priv file private data.
69
* \param magic magic number.
71
* Creates a drm_magic_entry structure and appends to the linked list
72
* associated the magic number hash key in drm_device::magiclist, while holding
73
* the drm_device::struct_mutex lock.
75
static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
78
struct drm_magic_entry *entry;
79
struct drm_device *dev = master->minor->dev;
80
DRM_DEBUG("%d\n", magic);
82
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
86
entry->hash_item.key = (unsigned long)magic;
87
mutex_lock(&dev->struct_mutex);
88
drm_ht_insert_item(&master->magiclist, &entry->hash_item);
89
list_add_tail(&entry->head, &master->magicfree);
90
mutex_unlock(&dev->struct_mutex);
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_mutex lock.
104
static int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
106
struct drm_magic_entry *pt;
107
struct drm_hash_item *hash;
108
struct drm_device *dev = master->minor->dev;
110
DRM_DEBUG("%d\n", magic);
112
mutex_lock(&dev->struct_mutex);
113
if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
114
mutex_unlock(&dev->struct_mutex);
117
pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
118
drm_ht_remove_item(&master->magiclist, hash);
120
mutex_unlock(&dev->struct_mutex);
128
* Get a unique magic number (ioctl).
130
* \param inode device inode.
131
* \param file_priv DRM file private.
132
* \param cmd command.
133
* \param arg pointer to a resulting drm_auth structure.
134
* \return zero on success, or a negative number on failure.
136
* If there is a magic number in drm_file::magic then use it, otherwise
137
* searches an unique non-zero magic number and add it associating it with \p
140
int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
142
static drm_magic_t sequence = 0;
143
static DEFINE_SPINLOCK(lock);
144
struct drm_auth *auth = data;
146
/* Find unique magic */
147
if (file_priv->magic) {
148
auth->magic = file_priv->magic;
153
++sequence; /* reserve 0 */
154
auth->magic = sequence++;
156
} while (drm_find_file(file_priv->master, auth->magic));
157
file_priv->magic = auth->magic;
158
drm_add_magic(file_priv->master, file_priv, auth->magic);
161
DRM_DEBUG("%u\n", auth->magic);
167
* Authenticate with a magic.
169
* \param inode device inode.
170
* \param file_priv DRM file private.
171
* \param cmd command.
172
* \param arg pointer to a drm_auth structure.
173
* \return zero if authentication successed, or a negative number otherwise.
175
* Checks if \p file_priv is associated with the magic number passed in \arg.
177
int drm_authmagic(struct drm_device *dev, void *data,
178
struct drm_file *file_priv)
180
struct drm_auth *auth = data;
181
struct drm_file *file;
183
DRM_DEBUG("%u\n", auth->magic);
184
if ((file = drm_find_file(file_priv->master, auth->magic))) {
185
file->authenticated = 1;
186
drm_remove_magic(file_priv->master, auth->magic);