Linux 4.1.16
[linux/fpc-iii.git] / drivers / gpu / drm / drm_auth.c
blobfc8e8aaa34fb37f44e338bb237867078d84c020f
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 struct drm_magic_entry {
40 struct list_head head;
41 struct drm_hash_item hash_item;
42 struct drm_file *priv;
45 /**
46 * Find the file with the given magic number.
48 * \param dev DRM device.
49 * \param magic magic number.
51 * Searches in drm_device::magiclist within all files with the same hash key
52 * the one with matching magic number, while holding the drm_device::struct_mutex
53 * lock.
55 static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
57 struct drm_file *retval = NULL;
58 struct drm_magic_entry *pt;
59 struct drm_hash_item *hash;
60 struct drm_device *dev = master->minor->dev;
62 mutex_lock(&dev->struct_mutex);
63 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
64 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
65 retval = pt->priv;
67 mutex_unlock(&dev->struct_mutex);
68 return retval;
71 /**
72 * Adds a magic number.
74 * \param dev DRM device.
75 * \param priv file private data.
76 * \param magic magic number.
78 * Creates a drm_magic_entry structure and appends to the linked list
79 * associated the magic number hash key in drm_device::magiclist, while holding
80 * the drm_device::struct_mutex lock.
82 static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
83 drm_magic_t magic)
85 struct drm_magic_entry *entry;
86 struct drm_device *dev = master->minor->dev;
87 DRM_DEBUG("%d\n", magic);
89 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
90 if (!entry)
91 return -ENOMEM;
92 entry->priv = priv;
93 entry->hash_item.key = (unsigned long)magic;
94 mutex_lock(&dev->struct_mutex);
95 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
96 list_add_tail(&entry->head, &master->magicfree);
97 mutex_unlock(&dev->struct_mutex);
99 return 0;
103 * Remove a magic number.
105 * \param dev DRM device.
106 * \param magic magic number.
108 * Searches and unlinks the entry in drm_device::magiclist with the magic
109 * number hash key, while holding the drm_device::struct_mutex lock.
111 int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
113 struct drm_magic_entry *pt;
114 struct drm_hash_item *hash;
115 struct drm_device *dev = master->minor->dev;
117 DRM_DEBUG("%d\n", magic);
119 mutex_lock(&dev->struct_mutex);
120 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
121 mutex_unlock(&dev->struct_mutex);
122 return -EINVAL;
124 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
125 drm_ht_remove_item(&master->magiclist, hash);
126 list_del(&pt->head);
127 mutex_unlock(&dev->struct_mutex);
129 kfree(pt);
131 return 0;
135 * Get a unique magic number (ioctl).
137 * \param inode device inode.
138 * \param file_priv DRM file private.
139 * \param cmd command.
140 * \param arg pointer to a resulting drm_auth structure.
141 * \return zero on success, or a negative number on failure.
143 * If there is a magic number in drm_file::magic then use it, otherwise
144 * searches an unique non-zero magic number and add it associating it with \p
145 * file_priv.
146 * This ioctl needs protection by the drm_global_mutex, which protects
147 * struct drm_file::magic and struct drm_magic_entry::priv.
149 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
151 static drm_magic_t sequence = 0;
152 static DEFINE_SPINLOCK(lock);
153 struct drm_auth *auth = data;
155 /* Find unique magic */
156 if (file_priv->magic) {
157 auth->magic = file_priv->magic;
158 } else {
159 do {
160 spin_lock(&lock);
161 if (!sequence)
162 ++sequence; /* reserve 0 */
163 auth->magic = sequence++;
164 spin_unlock(&lock);
165 } while (drm_find_file(file_priv->master, auth->magic));
166 file_priv->magic = auth->magic;
167 drm_add_magic(file_priv->master, file_priv, auth->magic);
170 DRM_DEBUG("%u\n", auth->magic);
172 return 0;
176 * Authenticate with a magic.
178 * \param inode device inode.
179 * \param file_priv DRM file private.
180 * \param cmd command.
181 * \param arg pointer to a drm_auth structure.
182 * \return zero if authentication successed, or a negative number otherwise.
184 * Checks if \p file_priv is associated with the magic number passed in \arg.
185 * This ioctl needs protection by the drm_global_mutex, which protects
186 * struct drm_file::magic and struct drm_magic_entry::priv.
188 int drm_authmagic(struct drm_device *dev, void *data,
189 struct drm_file *file_priv)
191 struct drm_auth *auth = data;
192 struct drm_file *file;
194 DRM_DEBUG("%u\n", auth->magic);
195 if ((file = drm_find_file(file_priv->master, auth->magic))) {
196 file->authenticated = 1;
197 drm_remove_magic(file_priv->master, auth->magic);
198 return 0;
200 return -EINVAL;