Linux Audio

Check our new training course

Loading...
v4.6
 
 
 
 
 
 
 
 
 1/*
 2 * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
 3 *
 4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 6 * All Rights Reserved.
 7 *
 8 * Author Rickard E. (Rik) Faith <faith@valinux.com>
 9 * Author Gareth Hughes <gareth@valinux.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
20 * Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#include <drm/drmP.h>
32#include "drm_internal.h"
33
34/**
35 * drm_getmagic - Get unique magic of a client
36 * @dev: DRM device to operate on
37 * @data: ioctl data containing the drm_auth object
38 * @file_priv: DRM file that performs the operation
39 *
40 * This looks up the unique magic of the passed client and returns it. If the
41 * client did not have a magic assigned, yet, a new one is registered. The magic
42 * is stored in the passed drm_auth object.
43 *
44 * Returns: 0 on success, negative error code on failure.
 
 
45 */
46int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
47{
48	struct drm_auth *auth = data;
49	int ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
51	mutex_lock(&dev->struct_mutex);
52	if (!file_priv->magic) {
53		ret = idr_alloc(&file_priv->master->magic_map, file_priv,
54				1, 0, GFP_KERNEL);
55		if (ret >= 0)
56			file_priv->magic = ret;
57	}
58	auth->magic = file_priv->magic;
 
 
59	mutex_unlock(&dev->struct_mutex);
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61	DRM_DEBUG("%u\n", auth->magic);
62
63	return ret < 0 ? ret : 0;
64}
65
66/**
67 * drm_authmagic - Authenticate client with a magic
68 * @dev: DRM device to operate on
69 * @data: ioctl data containing the drm_auth object
70 * @file_priv: DRM file that performs the operation
71 *
72 * This looks up a DRM client by the passed magic and authenticates it.
 
 
 
 
73 *
74 * Returns: 0 on success, negative error code on failure.
75 */
76int drm_authmagic(struct drm_device *dev, void *data,
77		  struct drm_file *file_priv)
78{
79	struct drm_auth *auth = data;
80	struct drm_file *file;
81
82	DRM_DEBUG("%u\n", auth->magic);
83
84	mutex_lock(&dev->struct_mutex);
85	file = idr_find(&file_priv->master->magic_map, auth->magic);
86	if (file) {
87		file->authenticated = 1;
88		idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
 
89	}
90	mutex_unlock(&dev->struct_mutex);
91
92	return file ? 0 : -EINVAL;
93}
v3.1
  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 */
 48static 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
 64/**
 65 * Adds a magic number.
 66 *
 67 * \param dev DRM device.
 68 * \param priv file private data.
 69 * \param magic magic number.
 70 *
 71 * Creates a drm_magic_entry structure and appends to the linked list
 72 * associated the magic number hash key in drm_device::magiclist, while holding
 73 * the drm_device::struct_mutex lock.
 74 */
 75static 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
 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 */
104static 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
127/**
128 * Get a unique magic number (ioctl).
129 *
130 * \param inode device inode.
131 * \param file_priv DRM file private.
132 * \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
138 * file_priv.
139 */
140int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
141{
142	static drm_magic_t sequence = 0;
143	static DEFINE_SPINLOCK(lock);
144	struct drm_auth *auth = data;
145
146	/* Find unique magic */
147	if (file_priv->magic) {
148		auth->magic = file_priv->magic;
149	} else {
150		do {
151			spin_lock(&lock);
152			if (!sequence)
153				++sequence;	/* reserve 0 */
154			auth->magic = sequence++;
155			spin_unlock(&lock);
156		} while (drm_find_file(file_priv->master, auth->magic));
157		file_priv->magic = auth->magic;
158		drm_add_magic(file_priv->master, file_priv, auth->magic);
159	}
160
161	DRM_DEBUG("%u\n", auth->magic);
162
163	return 0;
164}
165
166/**
167 * Authenticate with a magic.
 
 
 
168 *
169 * \param inode device inode.
170 * \param file_priv DRM file private.
171 * \param cmd command.
172 * \param arg pointer to a drm_auth structure.
173 * \return zero if authentication successed, or a negative number otherwise.
174 *
175 * Checks if \p file_priv is associated with the magic number passed in \arg.
176 */
177int drm_authmagic(struct drm_device *dev, void *data,
178		  struct drm_file *file_priv)
179{
180	struct drm_auth *auth = data;
181	struct drm_file *file;
182
183	DRM_DEBUG("%u\n", auth->magic);
184	if ((file = drm_find_file(file_priv->master, auth->magic))) {
 
 
 
185		file->authenticated = 1;
186		drm_remove_magic(file_priv->master, auth->magic);
187		return 0;
188	}
189	return -EINVAL;
 
 
190}