drm: drop unused 'magicfree' list
[deliverable/linux.git] / drivers / gpu / drm / drm_auth.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_auth.c
1da177e4
LT
3 * IOCTLs for authentication
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
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:
22 *
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.
26 *
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.
34 */
35
760285e7 36#include <drm/drmP.h>
67d0ec4e 37#include "drm_internal.h"
1da177e4 38
71d39483 39struct drm_magic_entry {
71d39483
DH
40 struct drm_hash_item hash_item;
41 struct drm_file *priv;
42};
43
1da177e4
LT
44/**
45 * Find the file with the given magic number.
46 *
47 * \param dev DRM device.
48 * \param magic magic number.
49 *
50 * Searches in drm_device::magiclist within all files with the same hash key
30e2fb18 51 * the one with matching magic number, while holding the drm_device::struct_mutex
1da177e4
LT
52 * lock.
53 */
7c1c2871 54static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
1da177e4 55{
84b1fd10 56 struct drm_file *retval = NULL;
8fc2fdf4 57 struct drm_magic_entry *pt;
e0be428e 58 struct drm_hash_item *hash;
7c1c2871 59 struct drm_device *dev = master->minor->dev;
1da177e4 60
30e2fb18 61 mutex_lock(&dev->struct_mutex);
7c1c2871 62 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
8fc2fdf4 63 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
8669cbc5 64 retval = pt->priv;
1da177e4 65 }
30e2fb18 66 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
67 return retval;
68}
69
70/**
71 * Adds a magic number.
b5e89ed5 72 *
1da177e4
LT
73 * \param dev DRM device.
74 * \param priv file private data.
75 * \param magic magic number.
76 *
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
30e2fb18 79 * the drm_device::struct_mutex lock.
1da177e4 80 */
7c1c2871 81static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
b5e89ed5 82 drm_magic_t magic)
1da177e4 83{
8fc2fdf4 84 struct drm_magic_entry *entry;
7c1c2871 85 struct drm_device *dev = master->minor->dev;
1da177e4
LT
86 DRM_DEBUG("%d\n", magic);
87
6ebc22e6 88 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
b5e89ed5
DA
89 if (!entry)
90 return -ENOMEM;
b5e89ed5 91 entry->priv = priv;
8669cbc5 92 entry->hash_item.key = (unsigned long)magic;
30e2fb18 93 mutex_lock(&dev->struct_mutex);
7c1c2871 94 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
30e2fb18 95 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
96
97 return 0;
98}
99
100/**
101 * Remove a magic number.
b5e89ed5 102 *
1da177e4
LT
103 * \param dev DRM device.
104 * \param magic magic number.
105 *
106 * Searches and unlinks the entry in drm_device::magiclist with the magic
30e2fb18 107 * number hash key, while holding the drm_device::struct_mutex lock.
1da177e4 108 */
598781d7 109int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
1da177e4 110{
8fc2fdf4 111 struct drm_magic_entry *pt;
e0be428e 112 struct drm_hash_item *hash;
7c1c2871 113 struct drm_device *dev = master->minor->dev;
1da177e4
LT
114
115 DRM_DEBUG("%d\n", magic);
1da177e4 116
30e2fb18 117 mutex_lock(&dev->struct_mutex);
7c1c2871 118 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
8669cbc5
TH
119 mutex_unlock(&dev->struct_mutex);
120 return -EINVAL;
1da177e4 121 }
8fc2fdf4 122 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
7c1c2871 123 drm_ht_remove_item(&master->magiclist, hash);
30e2fb18 124 mutex_unlock(&dev->struct_mutex);
1da177e4 125
9a298b2a 126 kfree(pt);
1da177e4 127
572225be 128 return 0;
1da177e4
LT
129}
130
131/**
132 * Get a unique magic number (ioctl).
133 *
134 * \param inode device inode.
6c340eac 135 * \param file_priv DRM file private.
1da177e4
LT
136 * \param cmd command.
137 * \param arg pointer to a resulting drm_auth structure.
138 * \return zero on success, or a negative number on failure.
139 *
140 * If there is a magic number in drm_file::magic then use it, otherwise
141 * searches an unique non-zero magic number and add it associating it with \p
6c340eac 142 * file_priv.
598781d7
TH
143 * This ioctl needs protection by the drm_global_mutex, which protects
144 * struct drm_file::magic and struct drm_magic_entry::priv.
1da177e4 145 */
c153f45f 146int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4
LT
147{
148 static drm_magic_t sequence = 0;
149 static DEFINE_SPINLOCK(lock);
c153f45f 150 struct drm_auth *auth = data;
1da177e4 151
b5e89ed5 152 /* Find unique magic */
6c340eac 153 if (file_priv->magic) {
c153f45f 154 auth->magic = file_priv->magic;
1da177e4
LT
155 } else {
156 do {
157 spin_lock(&lock);
b5e89ed5
DA
158 if (!sequence)
159 ++sequence; /* reserve 0 */
c153f45f 160 auth->magic = sequence++;
1da177e4 161 spin_unlock(&lock);
7c1c2871 162 } while (drm_find_file(file_priv->master, auth->magic));
c153f45f 163 file_priv->magic = auth->magic;
7c1c2871 164 drm_add_magic(file_priv->master, file_priv, auth->magic);
1da177e4
LT
165 }
166
c153f45f
EA
167 DRM_DEBUG("%u\n", auth->magic);
168
1da177e4
LT
169 return 0;
170}
171
172/**
173 * Authenticate with a magic.
174 *
175 * \param inode device inode.
6c340eac 176 * \param file_priv DRM file private.
1da177e4
LT
177 * \param cmd command.
178 * \param arg pointer to a drm_auth structure.
179 * \return zero if authentication successed, or a negative number otherwise.
180 *
6c340eac 181 * Checks if \p file_priv is associated with the magic number passed in \arg.
598781d7
TH
182 * This ioctl needs protection by the drm_global_mutex, which protects
183 * struct drm_file::magic and struct drm_magic_entry::priv.
1da177e4 184 */
c153f45f
EA
185int drm_authmagic(struct drm_device *dev, void *data,
186 struct drm_file *file_priv)
1da177e4 187{
c153f45f 188 struct drm_auth *auth = data;
84b1fd10 189 struct drm_file *file;
1da177e4 190
c153f45f 191 DRM_DEBUG("%u\n", auth->magic);
7c1c2871 192 if ((file = drm_find_file(file_priv->master, auth->magic))) {
1da177e4 193 file->authenticated = 1;
7c1c2871 194 drm_remove_magic(file_priv->master, auth->magic);
1da177e4
LT
195 return 0;
196 }
197 return -EINVAL;
198}
This page took 0.73581 seconds and 5 git commands to generate.