xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_auth.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: drm_auth.c,v 1.4 2018/08/27 14:14:29 riastradh Exp $	*/
2 
3 /*
4  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
5  *
6  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8  * All Rights Reserved.
9  *
10  * Author Rickard E. (Rik) Faith <faith@valinux.com>
11  * Author Gareth Hughes <gareth@valinux.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice (including the next
21  * paragraph) shall be included in all copies or substantial portions of the
22  * Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  * OTHER DEALINGS IN THE SOFTWARE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: drm_auth.c,v 1.4 2018/08/27 14:14:29 riastradh Exp $");
35 
36 #include <drm/drmP.h>
37 #include "drm_internal.h"
38 
39 /**
40  * drm_getmagic - Get unique magic of a client
41  * @dev: DRM device to operate on
42  * @data: ioctl data containing the drm_auth object
43  * @file_priv: DRM file that performs the operation
44  *
45  * This looks up the unique magic of the passed client and returns it. If the
46  * client did not have a magic assigned, yet, a new one is registered. The magic
47  * is stored in the passed drm_auth object.
48  *
49  * Returns: 0 on success, negative error code on failure.
50  */
51 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
52 {
53 	struct drm_auth *auth = data;
54 	int ret = 0;
55 
56 	idr_preload(GFP_KERNEL);
57 	mutex_lock(&dev->struct_mutex);
58 	if (!file_priv->magic) {
59 		ret = idr_alloc(&file_priv->master->magic_map, file_priv,
60 				1, 0, GFP_KERNEL);
61 		if (ret >= 0)
62 			file_priv->magic = ret;
63 	}
64 	auth->magic = file_priv->magic;
65 	mutex_unlock(&dev->struct_mutex);
66 	idr_preload_end();
67 
68 	DRM_DEBUG("%u\n", auth->magic);
69 
70 	return ret < 0 ? ret : 0;
71 }
72 
73 /**
74  * drm_authmagic - Authenticate client with a magic
75  * @dev: DRM device to operate on
76  * @data: ioctl data containing the drm_auth object
77  * @file_priv: DRM file that performs the operation
78  *
79  * This looks up a DRM client by the passed magic and authenticates it.
80  *
81  * Returns: 0 on success, negative error code on failure.
82  */
83 int drm_authmagic(struct drm_device *dev, void *data,
84 		  struct drm_file *file_priv)
85 {
86 	struct drm_auth *auth = data;
87 	struct drm_file *file;
88 
89 	DRM_DEBUG("%u\n", auth->magic);
90 
91 	mutex_lock(&dev->struct_mutex);
92 	file = idr_find(&file_priv->master->magic_map, auth->magic);
93 	if (file) {
94 		file->authenticated = 1;
95 		idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
96 	}
97 	mutex_unlock(&dev->struct_mutex);
98 
99 	return file ? 0 : -EINVAL;
100 }
101