xref: /dflybsd-src/sys/dev/drm/drm_auth.c (revision c90492810f56e1f820f5df990e3718c512424be9)
12c9916cdSFrançois Tigeot /*
22c9916cdSFrançois Tigeot  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
32c9916cdSFrançois Tigeot  *
47f3c3d6fSHasso Tepper  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
57f3c3d6fSHasso Tepper  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
67f3c3d6fSHasso Tepper  * All Rights Reserved.
77f3c3d6fSHasso Tepper  *
8*c9049281SFrançois Tigeot  * Author Rickard E. (Rik) Faith <faith@valinux.com>
9*c9049281SFrançois Tigeot  * Author Gareth Hughes <gareth@valinux.com>
10*c9049281SFrançois Tigeot  *
117f3c3d6fSHasso Tepper  * Permission is hereby granted, free of charge, to any person obtaining a
127f3c3d6fSHasso Tepper  * copy of this software and associated documentation files (the "Software"),
137f3c3d6fSHasso Tepper  * to deal in the Software without restriction, including without limitation
147f3c3d6fSHasso Tepper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
157f3c3d6fSHasso Tepper  * and/or sell copies of the Software, and to permit persons to whom the
167f3c3d6fSHasso Tepper  * Software is furnished to do so, subject to the following conditions:
177f3c3d6fSHasso Tepper  *
187f3c3d6fSHasso Tepper  * The above copyright notice and this permission notice (including the next
197f3c3d6fSHasso Tepper  * paragraph) shall be included in all copies or substantial portions of the
207f3c3d6fSHasso Tepper  * Software.
217f3c3d6fSHasso Tepper  *
227f3c3d6fSHasso Tepper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237f3c3d6fSHasso Tepper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247f3c3d6fSHasso Tepper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
257f3c3d6fSHasso Tepper  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
267f3c3d6fSHasso Tepper  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
277f3c3d6fSHasso Tepper  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
287f3c3d6fSHasso Tepper  * OTHER DEALINGS IN THE SOFTWARE.
297f3c3d6fSHasso Tepper  */
307f3c3d6fSHasso Tepper 
3118e26a6dSFrançois Tigeot #include <drm/drmP.h>
322c9916cdSFrançois Tigeot #include "drm_internal.h"
337f3c3d6fSHasso Tepper 
347f3c3d6fSHasso Tepper /**
35*c9049281SFrançois Tigeot  * drm_getmagic - Get unique magic of a client
36*c9049281SFrançois Tigeot  * @dev: DRM device to operate on
37*c9049281SFrançois Tigeot  * @data: ioctl data containing the drm_auth object
38*c9049281SFrançois Tigeot  * @file_priv: DRM file that performs the operation
399edbd4a0SFrançois Tigeot  *
40*c9049281SFrançois Tigeot  * This looks up the unique magic of the passed client and returns it. If the
41*c9049281SFrançois Tigeot  * client did not have a magic assigned, yet, a new one is registered. The magic
42*c9049281SFrançois Tigeot  * is stored in the passed drm_auth object.
439edbd4a0SFrançois Tigeot  *
44*c9049281SFrançois Tigeot  * Returns: 0 on success, negative error code on failure.
457f3c3d6fSHasso Tepper  */
46b3705d71SHasso Tepper int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
477f3c3d6fSHasso Tepper {
48b3705d71SHasso Tepper 	struct drm_auth *auth = data;
49*c9049281SFrançois Tigeot 	int ret = 0;
507f3c3d6fSHasso Tepper 
51*c9049281SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
52*c9049281SFrançois Tigeot 	if (!file_priv->magic) {
53*c9049281SFrançois Tigeot 		ret = idr_alloc(&dev->magic_map, file_priv,
54*c9049281SFrançois Tigeot 				1, 0, GFP_KERNEL);
55*c9049281SFrançois Tigeot 		if (ret >= 0)
56*c9049281SFrançois Tigeot 			file_priv->magic = ret;
577f3c3d6fSHasso Tepper 	}
58*c9049281SFrançois Tigeot 	auth->magic = file_priv->magic;
59*c9049281SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
607f3c3d6fSHasso Tepper 
617f3c3d6fSHasso Tepper 	DRM_DEBUG("%u\n", auth->magic);
627f3c3d6fSHasso Tepper 
63*c9049281SFrançois Tigeot 	return ret < 0 ? ret : 0;
647f3c3d6fSHasso Tepper }
657f3c3d6fSHasso Tepper 
667f3c3d6fSHasso Tepper /**
678b983a81SFrançois Tigeot  * drm_authmagic - Authenticate client with a magic
688b983a81SFrançois Tigeot  * @dev: DRM device to operate on
698b983a81SFrançois Tigeot  * @data: ioctl data containing the drm_auth object
708b983a81SFrançois Tigeot  * @file_priv: DRM file that performs the operation
71ba55f2f5SFrançois Tigeot  *
728b983a81SFrançois Tigeot  * This looks up a DRM client by the passed magic and authenticates it.
73ba55f2f5SFrançois Tigeot  *
748b983a81SFrançois Tigeot  * Returns: 0 on success, negative error code on failure.
757f3c3d6fSHasso Tepper  */
76b3705d71SHasso Tepper int drm_authmagic(struct drm_device *dev, void *data,
77b3705d71SHasso Tepper 		  struct drm_file *file_priv)
787f3c3d6fSHasso Tepper {
79b3705d71SHasso Tepper 	struct drm_auth *auth = data;
80ba55f2f5SFrançois Tigeot 	struct drm_file *file;
817f3c3d6fSHasso Tepper 
827f3c3d6fSHasso Tepper 	DRM_DEBUG("%u\n", auth->magic);
838b983a81SFrançois Tigeot 
848b983a81SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
85*c9049281SFrançois Tigeot 	file = idr_find(&dev->magic_map, auth->magic);
868b983a81SFrançois Tigeot 	if (file) {
87ba55f2f5SFrançois Tigeot 		file->authenticated = 1;
88*c9049281SFrançois Tigeot 		idr_replace(&dev->magic_map, NULL, auth->magic);
897f3c3d6fSHasso Tepper 	}
908b983a81SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
918b983a81SFrançois Tigeot 
928b983a81SFrançois Tigeot 	return file ? 0 : -EINVAL;
937f3c3d6fSHasso Tepper }
94