[intel] Quirk away MSI support on 945G/GM.
[platform/upstream/libdrm.git] / linux-core / drm_auth.c
1 /**
2  * \file drm_auth.c
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
36 #include "drmP.h"
37
38 /**
39  * 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
45  * the one with matching magic number, while holding the drm_device::struct_mutex
46  * lock.
47  */
48 static struct drm_file *drm_find_file(struct drm_device * dev, drm_magic_t magic)
49 {
50         struct drm_file *retval = NULL;
51         struct drm_magic_entry *pt;
52         struct drm_hash_item *hash;
53
54         mutex_lock(&dev->struct_mutex);
55         if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
56                 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
57                 retval = pt->priv;
58         }
59         mutex_unlock(&dev->struct_mutex);
60         return retval;
61 }
62
63 /**
64  * Adds a magic number.
65  *
66  * \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
72  * the drm_device::struct_mutex lock.
73  */
74 static int drm_add_magic(struct drm_device * dev, struct drm_file * priv,
75                          drm_magic_t magic)
76 {
77         struct drm_magic_entry *entry;
78
79         DRM_DEBUG("%d\n", magic);
80
81         entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
82         if (!entry)
83                 return -ENOMEM;
84         memset(entry, 0, sizeof(*entry));
85         entry->priv = priv;
86         entry->hash_item.key = (unsigned long)magic;
87         mutex_lock(&dev->struct_mutex);
88         drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
89         list_add_tail(&entry->head, &dev->magicfree);
90         mutex_unlock(&dev->struct_mutex);
91
92         return 0;
93 }
94
95 /**
96  * Remove a magic number.
97  *
98  * \param dev DRM device.
99  * \param magic magic number.
100  *
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.
103  */
104 static int drm_remove_magic(struct drm_device * dev, drm_magic_t magic)
105 {
106         struct drm_magic_entry *pt;
107         struct drm_hash_item *hash;
108
109         DRM_DEBUG("%d\n", magic);
110
111         mutex_lock(&dev->struct_mutex);
112         if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
113                 mutex_unlock(&dev->struct_mutex);
114                 return -EINVAL;
115         }
116         pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
117         drm_ht_remove_item(&dev->magiclist, hash);
118         list_del(&pt->head);
119         mutex_unlock(&dev->struct_mutex);
120
121         drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
122
123         return 0;
124 }
125
126 /**
127  * Get a unique magic number (ioctl).
128  *
129  * \param inode device inode.
130  * \param file_priv DRM file private.
131  * \param cmd command.
132  * \param arg pointer to a resulting drm_auth structure.
133  * \return zero on success, or a negative number on failure.
134  *
135  * If there is a magic number in drm_file::magic then use it, otherwise
136  * searches an unique non-zero magic number and add it associating it with \p
137  * file_priv.
138  */
139 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
140 {
141         static drm_magic_t sequence = 0;
142         static DEFINE_SPINLOCK(lock);
143         struct drm_auth *auth = data;
144
145         /* Find unique magic */
146         if (file_priv->magic) {
147                 auth->magic = file_priv->magic;
148         } else {
149                 do {
150                         spin_lock(&lock);
151                         if (!sequence)
152                                 ++sequence;     /* reserve 0 */
153                         auth->magic = sequence++;
154                         spin_unlock(&lock);
155                 } while (drm_find_file(dev, auth->magic));
156                 file_priv->magic = auth->magic;
157                 drm_add_magic(dev, file_priv, auth->magic);
158         }
159
160         DRM_DEBUG("%u\n", auth->magic);
161
162         return 0;
163 }
164
165 /**
166  * Authenticate with a magic.
167  *
168  * \param inode device inode.
169  * \param file_priv DRM file private.
170  * \param cmd command.
171  * \param arg pointer to a drm_auth structure.
172  * \return zero if authentication successed, or a negative number otherwise.
173  *
174  * Checks if \p file_priv is associated with the magic number passed in \arg.
175  */
176 int drm_authmagic(struct drm_device *dev, void *data,
177                   struct drm_file *file_priv)
178 {
179         struct drm_auth *auth = data;
180         struct drm_file *file;
181
182         DRM_DEBUG("%u\n", auth->magic);
183         if ((file = drm_find_file(dev, auth->magic))) {
184                 file->authenticated = 1;
185                 drm_remove_magic(dev, auth->magic);
186                 return 0;
187         }
188         return -EINVAL;
189 }