xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/i915/i915_ioc32.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: i915_ioc32.c,v 1.3 2021/12/18 23:45:28 riastradh Exp $	*/
2 
3 /*
4  * 32-bit ioctl compatibility routines for the i915 DRM.
5  *
6  * Copyright (C) Paul Mackerras 2005
7  * Copyright (C) Alan Hourihane 2005
8  * All Rights Reserved.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the next
18  * paragraph) shall be included in all copies or substantial portions of the
19  * Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27  * IN THE SOFTWARE.
28  *
29  * Author: Alan Hourihane <alanh@fairlite.demon.co.uk>
30  */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: i915_ioc32.c,v 1.3 2021/12/18 23:45:28 riastradh Exp $");
33 
34 #include <linux/compat.h>
35 
36 #include <drm/i915_drm.h>
37 #include <drm/drm_ioctl.h>
38 #include "i915_drv.h"
39 
40 struct drm_i915_getparam32 {
41 	s32 param;
42 	/*
43 	 * We screwed up the generic ioctl struct here and used a variable-sized
44 	 * pointer. Use u32 in the compat struct to match the 32bit pointer
45 	 * userspace expects.
46 	 */
47 	u32 value;
48 };
49 
compat_i915_getparam(struct file * file,unsigned int cmd,unsigned long arg)50 static int compat_i915_getparam(struct file *file, unsigned int cmd,
51 				unsigned long arg)
52 {
53 	struct drm_i915_getparam32 req32;
54 	drm_i915_getparam_t __user *request;
55 
56 	if (copy_from_user(&req32, (void __user *)arg, sizeof(req32)))
57 		return -EFAULT;
58 
59 	request = compat_alloc_user_space(sizeof(*request));
60 	if (!access_ok(request, sizeof(*request)) ||
61 	    __put_user(req32.param, &request->param) ||
62 	    __put_user((void __user *)(unsigned long)req32.value,
63 		       &request->value))
64 		return -EFAULT;
65 
66 	return drm_ioctl(file, DRM_IOCTL_I915_GETPARAM,
67 			 (unsigned long)request);
68 }
69 
70 static drm_ioctl_compat_t *i915_compat_ioctls[] = {
71 	[DRM_I915_GETPARAM] = compat_i915_getparam,
72 };
73 
74 /**
75  * i915_compat_ioctl - handle the mistakes of the past
76  * @filp: the file pointer
77  * @cmd: the ioctl command (and encoded flags)
78  * @arg: the ioctl argument (from userspace)
79  *
80  * Called whenever a 32-bit process running under a 64-bit kernel
81  * performs an ioctl on /dev/dri/card<n>.
82  */
i915_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)83 long i915_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
84 {
85 	unsigned int nr = DRM_IOCTL_NR(cmd);
86 	drm_ioctl_compat_t *fn = NULL;
87 	int ret;
88 
89 	if (nr < DRM_COMMAND_BASE || nr >= DRM_COMMAND_END)
90 		return drm_compat_ioctl(filp, cmd, arg);
91 
92 	if (nr < DRM_COMMAND_BASE + ARRAY_SIZE(i915_compat_ioctls))
93 		fn = i915_compat_ioctls[nr - DRM_COMMAND_BASE];
94 
95 	if (fn != NULL)
96 		ret = (*fn) (filp, cmd, arg);
97 	else
98 		ret = drm_ioctl(filp, cmd, arg);
99 
100 	return ret;
101 }
102