1*5390d158Sthorpej /*	$NetBSD: linux32_ioctl.c,v 1.15 2021/09/19 23:51:37 thorpej Exp $ */
2ee0c5b44Smanu 
3ee0c5b44Smanu /*-
4ee0c5b44Smanu  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5ee0c5b44Smanu  *
6ee0c5b44Smanu  * Redistribution and use in source and binary forms, with or without
7ee0c5b44Smanu  * modification, are permitted provided that the following conditions
8ee0c5b44Smanu  * are met:
9ee0c5b44Smanu  * 1. Redistributions of source code must retain the above copyright
10ee0c5b44Smanu  *    notice, this list of conditions and the following disclaimer.
11ee0c5b44Smanu  * 2. Redistributions in binary form must reproduce the above copyright
12ee0c5b44Smanu  *    notice, this list of conditions and the following disclaimer in the
13ee0c5b44Smanu  *    documentation and/or other materials provided with the distribution.
14ee0c5b44Smanu  * 3. All advertising materials mentioning features or use of this software
15ee0c5b44Smanu  *    must display the following acknowledgement:
16ee0c5b44Smanu  *	This product includes software developed by Emmanuel Dreyfus
17ee0c5b44Smanu  * 4. The name of the author may not be used to endorse or promote
18ee0c5b44Smanu  *    products derived from this software without specific prior written
19ee0c5b44Smanu  *    permission.
20ee0c5b44Smanu  *
21ee0c5b44Smanu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22ee0c5b44Smanu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23ee0c5b44Smanu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24ee0c5b44Smanu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25ee0c5b44Smanu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26ee0c5b44Smanu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27ee0c5b44Smanu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28ee0c5b44Smanu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29ee0c5b44Smanu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30ee0c5b44Smanu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31ee0c5b44Smanu  * POSSIBILITY OF SUCH DAMAGE.
32ee0c5b44Smanu  */
33ee0c5b44Smanu 
34ee0c5b44Smanu #include <sys/cdefs.h>
35*5390d158Sthorpej __KERNEL_RCSID(0, "$NetBSD: linux32_ioctl.c,v 1.15 2021/09/19 23:51:37 thorpej Exp $");
36ee0c5b44Smanu 
37ee0c5b44Smanu #include <sys/types.h>
38ee0c5b44Smanu #include <sys/param.h>
39ee0c5b44Smanu #include <sys/time.h>
40ee0c5b44Smanu #include <sys/ucred.h>
41fc0d883cSchristos #include <sys/ioctl.h>
42*5390d158Sthorpej #include <sys/file.h>
43*5390d158Sthorpej #include <sys/filedesc.h>
44ee0c5b44Smanu 
45ee0c5b44Smanu #include <compat/netbsd32/netbsd32.h>
46ee0c5b44Smanu #include <compat/netbsd32/netbsd32_syscallargs.h>
47ee0c5b44Smanu 
48ee0c5b44Smanu #include <compat/linux/common/linux_types.h>
49ee0c5b44Smanu #include <compat/linux/common/linux_signal.h>
5092ce8c6aSad #include <compat/linux/common/linux_ipc.h>
51*5390d158Sthorpej #include <compat/linux/common/linux_ioctl.h>
5292ce8c6aSad #include <compat/linux/common/linux_sem.h>
53ee0c5b44Smanu #include <compat/linux/linux_syscallargs.h>
54ee0c5b44Smanu 
55ee0c5b44Smanu #include <compat/linux32/common/linux32_types.h>
56ee0c5b44Smanu #include <compat/linux32/common/linux32_signal.h>
57f806470fSmanu #include <compat/linux32/common/linux32_ioctl.h>
58f806470fSmanu #include <compat/linux32/common/linux32_termios.h>
59ee0c5b44Smanu #include <compat/linux32/common/linux32_sysctl.h>
60ee0c5b44Smanu #include <compat/linux32/linux32_syscallargs.h>
61ee0c5b44Smanu 
62c0e43ab0Sjmcneill #include <compat/ossaudio/ossaudio.h>
63c0e43ab0Sjmcneill #include <compat/ossaudio/ossaudiovar.h>
64c0e43ab0Sjmcneill 
65ee0c5b44Smanu int
linux32_sys_ioctl(struct lwp * l,const struct linux32_sys_ioctl_args * uap,register_t * retval)667e2790cfSdsl linux32_sys_ioctl(struct lwp *l, const struct linux32_sys_ioctl_args *uap, register_t *retval)
67ee0c5b44Smanu {
687e2790cfSdsl 	/* {
69ee0c5b44Smanu 		syscallarg(int) fd;
70ee0c5b44Smanu 		syscallarg(netbsd32_u_long) com;
71ee0c5b44Smanu 		syscallarg(netbsd32_charp) data;
727e2790cfSdsl 	} */
73c0e43ab0Sjmcneill 	struct oss_sys_ioctl_args ossuap;
74f806470fSmanu 	int group;
75f806470fSmanu 	int error;
76f806470fSmanu 
77f806470fSmanu 	group = LINUX32_IOCGROUP((int)SCARG(uap, com));
78ee0c5b44Smanu 
79717102f4Smanu #ifdef DEBUG_LINUX
80ee0c5b44Smanu 	printf("linux32_sys_ioctl(%d, 0x%x/\'%c\', %p)\n", SCARG(uap, fd),
81d364d308Sdsl 	    SCARG(uap, com), (char)group, SCARG_P32(uap, data));
82717102f4Smanu #endif
83ee0c5b44Smanu 
84f806470fSmanu 	switch(group) {
85f806470fSmanu 	case 'T':
86*5390d158Sthorpej 	    {
87*5390d158Sthorpej 		/*
88*5390d158Sthorpej 		 * Termios, the MIDI sequencer, and timerfd use 'T' to
89*5390d158Sthorpej 		 * identify the ioctl, so we have to differentiate them
90*5390d158Sthorpej 		 * in another way.
91*5390d158Sthorpej 		 *
92*5390d158Sthorpej 		 * (XXX We don't bother with MIDI here.)
93*5390d158Sthorpej 		 */
94*5390d158Sthorpej 		struct file *fp;
95*5390d158Sthorpej 
96*5390d158Sthorpej 		if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
97*5390d158Sthorpej 			return EBADF;
98*5390d158Sthorpej 
99*5390d158Sthorpej 		if (fp->f_type == DTYPE_TIMERFD) {
100*5390d158Sthorpej 			struct linux_sys_ioctl_args ua;
101*5390d158Sthorpej 			SCARG(&ua, fd) = SCARG(uap, fd);
102*5390d158Sthorpej 			SCARG(&ua, com) = SCARG(uap, com);
103*5390d158Sthorpej 			SCARG(&ua, data) = SCARG_P32(uap, data);
104*5390d158Sthorpej 			error = linux_ioctl_timerfd(l, &ua, retval);
105*5390d158Sthorpej 		} else {
106f806470fSmanu 			error = linux32_ioctl_termios(l, uap, retval);
107*5390d158Sthorpej 		}
108*5390d158Sthorpej 		fd_putfile(SCARG(uap, fd));
109f806470fSmanu 		break;
110*5390d158Sthorpej 	    }
111c0e43ab0Sjmcneill 	case 'M':
112c0e43ab0Sjmcneill 	case 'Q':
113c0e43ab0Sjmcneill 	case 'P':
114c0e43ab0Sjmcneill 		SCARG(&ossuap, fd) = SCARG(uap, fd);
115c0e43ab0Sjmcneill 		SCARG(&ossuap, com) = (u_long)SCARG(uap, com);
116c0e43ab0Sjmcneill 		SCARG(&ossuap, data) = SCARG_P32(uap, data);
117c0e43ab0Sjmcneill 		switch (group) {
118c0e43ab0Sjmcneill 		case 'M':
119c0e43ab0Sjmcneill 			error = oss_ioctl_mixer(l, &ossuap, retval);
120c0e43ab0Sjmcneill 			break;
121c0e43ab0Sjmcneill 		case 'Q':
122c0e43ab0Sjmcneill 			error = oss_ioctl_sequencer(l, &ossuap, retval);
123c0e43ab0Sjmcneill 			break;
124c0e43ab0Sjmcneill 		case 'P':
125c0e43ab0Sjmcneill 			error = oss_ioctl_audio(l, &ossuap, retval);
126c0e43ab0Sjmcneill 			break;
127c0e43ab0Sjmcneill 		default:
128c0e43ab0Sjmcneill 			error = EINVAL; /* shutup gcc */
129c0e43ab0Sjmcneill 			break;
130c0e43ab0Sjmcneill 		}
131c0e43ab0Sjmcneill 		break;
132fc0d883cSchristos 	case 'V':	/* video4linux2 */
133fc0d883cSchristos 	case 'd':	/* drm */
134fc0d883cSchristos 	{
135fc0d883cSchristos 		struct sys_ioctl_args ua;
136fc0d883cSchristos 		u_long com = 0;
137fc0d883cSchristos 		if (SCARG(uap, com) & IOC_IN)
138fc0d883cSchristos 			com |= IOC_OUT;
139fc0d883cSchristos 		if (SCARG(uap, com) & IOC_OUT)
140fc0d883cSchristos 			com |= IOC_IN;
141fc0d883cSchristos 		SCARG(&ua, fd) = SCARG(uap, fd);
142fc0d883cSchristos 		SCARG(&ua, com) = SCARG(uap, com);
143fc0d883cSchristos 		SCARG(&ua, com) &= ~IOC_DIRMASK;
144fc0d883cSchristos 		SCARG(&ua, com) |= com;
145fc0d883cSchristos 		SCARG(&ua, data) = SCARG_P32(uap, data);
146fc0d883cSchristos 		error = sys_ioctl(l, (const void *)&ua, retval);
147fc0d883cSchristos 		break;
148fc0d883cSchristos 	}
149d83ff0cdSchristos 	case 0x89:
150d83ff0cdSchristos 		error = linux32_ioctl_socket(l, uap, retval);
15101f2e16aSmanu 		break;
152f806470fSmanu 	default:
153e2300e1bSmaxv #ifdef DEBUG_LINUX
154f806470fSmanu 		printf("Not yet implemented ioctl group \'%c\'\n", group);
155e2300e1bSmaxv #endif
156f806470fSmanu 		error = EINVAL;
157f806470fSmanu 		break;
158f806470fSmanu 	}
159f806470fSmanu 
160f806470fSmanu 	if (error == EPASSTHROUGH)
161f806470fSmanu 		error = EINVAL;
162f806470fSmanu 
163f806470fSmanu 	return error;
164ee0c5b44Smanu }
165