Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
drm_auth.c
Go to the documentation of this file.
1 
9 /*
10  * Created: Tue Feb 2 08:37:54 1999 by [email protected]
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 <drm/drmP.h>
37 
48 static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
49 {
50  struct drm_file *retval = NULL;
51  struct drm_magic_entry *pt;
52  struct drm_hash_item *hash;
53  struct drm_device *dev = master->minor->dev;
54 
55  mutex_lock(&dev->struct_mutex);
56  if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
57  pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
58  retval = pt->priv;
59  }
60  mutex_unlock(&dev->struct_mutex);
61  return retval;
62 }
63 
75 static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
76  drm_magic_t magic)
77 {
78  struct drm_magic_entry *entry;
79  struct drm_device *dev = master->minor->dev;
80  DRM_DEBUG("%d\n", magic);
81 
82  entry = kzalloc(sizeof(*entry), GFP_KERNEL);
83  if (!entry)
84  return -ENOMEM;
85  entry->priv = priv;
86  entry->hash_item.key = (unsigned long)magic;
87  mutex_lock(&dev->struct_mutex);
88  drm_ht_insert_item(&master->magiclist, &entry->hash_item);
89  list_add_tail(&entry->head, &master->magicfree);
90  mutex_unlock(&dev->struct_mutex);
91 
92  return 0;
93 }
94 
104 int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
105 {
106  struct drm_magic_entry *pt;
107  struct drm_hash_item *hash;
108  struct drm_device *dev = master->minor->dev;
109 
110  DRM_DEBUG("%d\n", magic);
111 
112  mutex_lock(&dev->struct_mutex);
113  if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
114  mutex_unlock(&dev->struct_mutex);
115  return -EINVAL;
116  }
117  pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
118  drm_ht_remove_item(&master->magiclist, hash);
119  list_del(&pt->head);
120  mutex_unlock(&dev->struct_mutex);
121 
122  kfree(pt);
123 
124  return 0;
125 }
126 
142 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
143 {
144  static drm_magic_t sequence = 0;
145  static DEFINE_SPINLOCK(lock);
146  struct drm_auth *auth = data;
147 
148  /* Find unique magic */
149  if (file_priv->magic) {
150  auth->magic = file_priv->magic;
151  } else {
152  do {
153  spin_lock(&lock);
154  if (!sequence)
155  ++sequence; /* reserve 0 */
156  auth->magic = sequence++;
157  spin_unlock(&lock);
158  } while (drm_find_file(file_priv->master, auth->magic));
159  file_priv->magic = auth->magic;
160  drm_add_magic(file_priv->master, file_priv, auth->magic);
161  }
162 
163  DRM_DEBUG("%u\n", auth->magic);
164 
165  return 0;
166 }
167 
181 int drm_authmagic(struct drm_device *dev, void *data,
182  struct drm_file *file_priv)
183 {
184  struct drm_auth *auth = data;
185  struct drm_file *file;
186 
187  DRM_DEBUG("%u\n", auth->magic);
188  if ((file = drm_find_file(file_priv->master, auth->magic))) {
189  file->authenticated = 1;
190  drm_remove_magic(file_priv->master, auth->magic);
191  return 0;
192  }
193  return -EINVAL;
194 }