drm/tegra: Atomic conversion, phase 2
[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
DH
39struct drm_magic_entry {
40 struct list_head head;
41 struct drm_hash_item hash_item;
42 struct drm_file *priv;
43};
44
1da177e4
LT
45/**
46 * Find the file with the given magic number.
47 *
48 * \param dev DRM device.
49 * \param magic magic number.
50 *
51 * Searches in drm_device::magiclist within all files with the same hash key
30e2fb18 52 * the one with matching magic number, while holding the drm_device::struct_mutex
1da177e4
LT
53 * lock.
54 */
7c1c2871 55static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
1da177e4 56{
84b1fd10 57 struct drm_file *retval = NULL;
8fc2fdf4 58 struct drm_magic_entry *pt;
e0be428e 59 struct drm_hash_item *hash;
7c1c2871 60 struct drm_device *dev = master->minor->dev;
1da177e4 61
30e2fb18 62 mutex_lock(&dev->struct_mutex);
7c1c2871 63 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
8fc2fdf4 64 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
8669cbc5 65 retval = pt->priv;
1da177e4 66 }
30e2fb18 67 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
68 return retval;
69}
70
71/**
72 * Adds a magic number.
b5e89ed5 73 *
1da177e4
LT
74 * \param dev DRM device.
75 * \param priv file private data.
76 * \param magic magic number.
77 *
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
30e2fb18 80 * the drm_device::struct_mutex lock.
1da177e4 81 */
7c1c2871 82static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
b5e89ed5 83 drm_magic_t magic)
1da177e4 84{
8fc2fdf4 85 struct drm_magic_entry *entry;
7c1c2871 86 struct drm_device *dev = master->minor->dev;
1da177e4
LT
87 DRM_DEBUG("%d\n", magic);
88
6ebc22e6 89 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
b5e89ed5
DA
90 if (!entry)
91 return -ENOMEM;
b5e89ed5 92 entry->priv = priv;
8669cbc5 93 entry->hash_item.key = (unsigned long)magic;
30e2fb18 94 mutex_lock(&dev->struct_mutex);
7c1c2871
DA
95 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
96 list_add_tail(&entry->head, &master->magicfree);
30e2fb18 97 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
98
99 return 0;
100}
101
102/**
103 * Remove a magic number.
b5e89ed5 104 *
1da177e4
LT
105 * \param dev DRM device.
106 * \param magic magic number.
107 *
108 * Searches and unlinks the entry in drm_device::magiclist with the magic
30e2fb18 109 * number hash key, while holding the drm_device::struct_mutex lock.
1da177e4 110 */
598781d7 111int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
1da177e4 112{
8fc2fdf4 113 struct drm_magic_entry *pt;
e0be428e 114 struct drm_hash_item *hash;
7c1c2871 115 struct drm_device *dev = master->minor->dev;
1da177e4
LT
116
117 DRM_DEBUG("%d\n", magic);
1da177e4 118
30e2fb18 119 mutex_lock(&dev->struct_mutex);
7c1c2871 120 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
8669cbc5
TH
121 mutex_unlock(&dev->struct_mutex);
122 return -EINVAL;
1da177e4 123 }
8fc2fdf4 124 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
7c1c2871 125 drm_ht_remove_item(&master->magiclist, hash);
8669cbc5 126 list_del(&pt->head);
30e2fb18 127 mutex_unlock(&dev->struct_mutex);
1da177e4 128
9a298b2a 129 kfree(pt);
1da177e4 130
572225be 131 return 0;
1da177e4
LT
132}
133
134/**
135 * Get a unique magic number (ioctl).
136 *
137 * \param inode device inode.
6c340eac 138 * \param file_priv DRM file private.
1da177e4
LT
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.
142 *
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
6c340eac 145 * file_priv.
598781d7
TH
146 * This ioctl needs protection by the drm_global_mutex, which protects
147 * struct drm_file::magic and struct drm_magic_entry::priv.
1da177e4 148 */
c153f45f 149int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4
LT
150{
151 static drm_magic_t sequence = 0;
152 static DEFINE_SPINLOCK(lock);
c153f45f 153 struct drm_auth *auth = data;
1da177e4 154
b5e89ed5 155 /* Find unique magic */
6c340eac 156 if (file_priv->magic) {
c153f45f 157 auth->magic = file_priv->magic;
1da177e4
LT
158 } else {
159 do {
160 spin_lock(&lock);
b5e89ed5
DA
161 if (!sequence)
162 ++sequence; /* reserve 0 */
c153f45f 163 auth->magic = sequence++;
1da177e4 164 spin_unlock(&lock);
7c1c2871 165 } while (drm_find_file(file_priv->master, auth->magic));
c153f45f 166 file_priv->magic = auth->magic;
7c1c2871 167 drm_add_magic(file_priv->master, file_priv, auth->magic);
1da177e4
LT
168 }
169
c153f45f
EA
170 DRM_DEBUG("%u\n", auth->magic);
171
1da177e4
LT
172 return 0;
173}
174
175/**
176 * Authenticate with a magic.
177 *
178 * \param inode device inode.
6c340eac 179 * \param file_priv DRM file private.
1da177e4
LT
180 * \param cmd command.
181 * \param arg pointer to a drm_auth structure.
182 * \return zero if authentication successed, or a negative number otherwise.
183 *
6c340eac 184 * Checks if \p file_priv is associated with the magic number passed in \arg.
598781d7
TH
185 * This ioctl needs protection by the drm_global_mutex, which protects
186 * struct drm_file::magic and struct drm_magic_entry::priv.
1da177e4 187 */
c153f45f
EA
188int drm_authmagic(struct drm_device *dev, void *data,
189 struct drm_file *file_priv)
1da177e4 190{
c153f45f 191 struct drm_auth *auth = data;
84b1fd10 192 struct drm_file *file;
1da177e4 193
c153f45f 194 DRM_DEBUG("%u\n", auth->magic);
7c1c2871 195 if ((file = drm_find_file(file_priv->master, auth->magic))) {
1da177e4 196 file->authenticated = 1;
7c1c2871 197 drm_remove_magic(file_priv->master, auth->magic);
1da177e4
LT
198 return 0;
199 }
200 return -EINVAL;
201}
This page took 0.733585 seconds and 5 git commands to generate.