blob: dc66cfef7ec358c8424ceeaba17041e1d9b26eff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_auth.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
36#include "drmP.h"
37
38/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * Find the file with the given magic number.
40 *
41 * \param dev DRM device.
42 * \param magic magic number.
43 *
44 * Searches in drm_device::magiclist within all files with the same hash key
Dave Airlie30e2fb12006-02-02 19:37:46 +110045 * the one with matching magic number, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * lock.
47 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100048static struct drm_file *drm_find_file(struct drm_device * dev, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Dave Airlie84b1fd12007-07-11 15:53:27 +100050 struct drm_file *retval = NULL;
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100051 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +100052 struct drm_hash_item *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Dave Airlie30e2fb12006-02-02 19:37:46 +110054 mutex_lock(&dev->struct_mutex);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100055 if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100056 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100057 retval = pt->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Dave Airlie30e2fb12006-02-02 19:37:46 +110059 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return retval;
61}
62
63/**
64 * Adds a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * \param dev DRM device.
67 * \param priv file private data.
68 * \param magic magic number.
69 *
70 * Creates a drm_magic_entry structure and appends to the linked list
71 * associated the magic number hash key in drm_device::magiclist, while holding
Dave Airlie30e2fb12006-02-02 19:37:46 +110072 * the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100074static int drm_add_magic(struct drm_device * dev, struct drm_file * priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +100077 struct drm_magic_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 DRM_DEBUG("%d\n", magic);
80
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
82 if (!entry)
83 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 memset(entry, 0, sizeof(*entry));
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 entry->priv = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100087 entry->hash_item.key = (unsigned long)magic;
Dave Airlie30e2fb12006-02-02 19:37:46 +110088 mutex_lock(&dev->struct_mutex);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100089 drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
90 list_add_tail(&entry->head, &dev->magicfree);
Dave Airlie30e2fb12006-02-02 19:37:46 +110091 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 return 0;
94}
95
96/**
97 * Remove a magic number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * \param dev DRM device.
100 * \param magic magic number.
101 *
102 * Searches and unlinks the entry in drm_device::magiclist with the magic
Dave Airlie30e2fb12006-02-02 19:37:46 +1100103 * number hash key, while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000105static int drm_remove_magic(struct drm_device * dev, drm_magic_t magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000107 struct drm_magic_entry *pt;
Dave Airliee0be4282007-07-12 10:26:44 +1000108 struct drm_hash_item *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 DRM_DEBUG("%d\n", magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Dave Airlie30e2fb12006-02-02 19:37:46 +1100112 mutex_lock(&dev->struct_mutex);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000113 if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
114 mutex_unlock(&dev->struct_mutex);
115 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000117 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +1000118 drm_ht_remove_item(&dev->magiclist, hash);
119 list_del(&pt->head);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100120 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
123
Dave Airlie572225b2006-08-08 22:17:02 +1000124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/**
128 * Get a unique magic number (ioctl).
129 *
130 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000131 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * \param cmd command.
133 * \param arg pointer to a resulting drm_auth structure.
134 * \return zero on success, or a negative number on failure.
135 *
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
Eric Anholt6c340ea2007-08-25 20:23:09 +1000138 * file_priv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000140int drm_getmagic(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 static drm_magic_t sequence = 0;
144 static DEFINE_SPINLOCK(lock);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000145 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000146 struct drm_auth auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 /* Find unique magic */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000149 if (file_priv->magic) {
150 auth.magic = file_priv->magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
152 do {
153 spin_lock(&lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 if (!sequence)
155 ++sequence; /* reserve 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 auth.magic = sequence++;
157 spin_unlock(&lock);
158 } while (drm_find_file(dev, auth.magic));
Eric Anholt6c340ea2007-08-25 20:23:09 +1000159 file_priv->magic = auth.magic;
160 drm_add_magic(dev, file_priv, auth.magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 DRM_DEBUG("%u\n", auth.magic);
Dave Airliec60ce622007-07-11 15:27:12 +1000164 if (copy_to_user((struct drm_auth __user *) arg, &auth, sizeof(auth)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -EFAULT;
166 return 0;
167}
168
169/**
170 * Authenticate with a magic.
171 *
172 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000173 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * \param cmd command.
175 * \param arg pointer to a drm_auth structure.
176 * \return zero if authentication successed, or a negative number otherwise.
177 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000178 * Checks if \p file_priv is associated with the magic number passed in \arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000180int drm_authmagic(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000183 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000184 struct drm_auth auth;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000185 struct drm_file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Dave Airliec60ce622007-07-11 15:27:12 +1000187 if (copy_from_user(&auth, (struct drm_auth __user *) arg, sizeof(auth)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EFAULT;
189 DRM_DEBUG("%u\n", auth.magic);
190 if ((file = drm_find_file(dev, auth.magic))) {
191 file->authenticated = 1;
192 drm_remove_magic(dev, auth.magic);
193 return 0;
194 }
195 return -EINVAL;
196}