xref: /netbsd-src/sys/compat/linux/common/linux_ioctl.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: linux_ioctl.c,v 1.56 2011/10/14 09:23:28 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_ioctl.c,v 1.56 2011/10/14 09:23:28 hannken Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "sequencer.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/conf.h>
43 #include <sys/ioctl.h>
44 #include <sys/mount.h>
45 #include <sys/file.h>
46 #include <sys/vnode.h>
47 #include <sys/filedesc.h>
48 
49 #include <sys/socket.h>
50 #include <net/if.h>
51 #include <sys/sockio.h>
52 
53 #include <sys/syscallargs.h>
54 
55 #include <compat/linux/common/linux_types.h>
56 #include <compat/linux/common/linux_signal.h>
57 #include <compat/linux/common/linux_ioctl.h>
58 #include <compat/linux/common/linux_ipc.h>
59 #include <compat/linux/common/linux_sem.h>
60 
61 #include <compat/linux/linux_syscallargs.h>
62 
63 #include <compat/ossaudio/ossaudio.h>
64 #define LINUX_TO_OSS(v) ((const void *)(v))	/* do nothing, same ioctl() encoding */
65 
66 /*
67  * Most ioctl command are just converted to their NetBSD values,
68  * and passed on. The ones that take structure pointers and (flag)
69  * values need some massaging.
70  */
71 int
72 linux_sys_ioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
73 {
74 	/* {
75 		syscallarg(int) fd;
76 		syscallarg(u_long) com;
77 		syscallarg(void *) data;
78 	} */
79 	int error;
80 
81 	switch (LINUX_IOCGROUP(SCARG(uap, com))) {
82 	case 'M':
83 		error = oss_ioctl_mixer(l, LINUX_TO_OSS(uap), retval);
84 		break;
85 	case 'Q':
86 		error = oss_ioctl_sequencer(l, LINUX_TO_OSS(uap), retval);
87 		break;
88 	case 'P':
89 		error = oss_ioctl_audio(l, LINUX_TO_OSS(uap), retval);
90 		break;
91 	case 'V':	/* video4linux2 */
92 	case 'd':	/* drm */
93 	{
94 		struct sys_ioctl_args ua;
95 		u_long com = 0;
96 		if (SCARG(uap, com) & IOC_IN)
97 			com |= IOC_OUT;
98 		if (SCARG(uap, com) & IOC_OUT)
99 			com |= IOC_IN;
100 		SCARG(&ua, fd) = SCARG(uap, fd);
101 		SCARG(&ua, com) = SCARG(uap, com);
102 		SCARG(&ua, com) &= ~IOC_DIRMASK;
103 		SCARG(&ua, com) |= com;
104 		SCARG(&ua, data) = SCARG(uap, data);
105 		error = sys_ioctl(l, (const void *)&ua, retval);
106 		break;
107 	}
108 	case 'r': /* VFAT ioctls; not yet supported */
109 		error = ENOSYS;
110 		break;
111 	case 'S':
112 		error = linux_ioctl_cdrom(l, uap, retval);
113 		break;
114 	case 't':
115 	case 'f':
116 		error = linux_ioctl_termios(l, uap, retval);
117 		break;
118 	case 'm':
119 		error = linux_ioctl_mtio(l, uap, retval);
120 		break;
121 	case 'T':
122 	{
123 #if NSEQUENCER > 0
124 /* XXX XAX 2x check this. */
125 		/*
126 		 * Both termios and the MIDI sequncer use 'T' to identify
127 		 * the ioctl, so we have to differentiate them in another
128 		 * way.  We do it by indexing in the cdevsw with the major
129 		 * device number and check if that is the sequencer entry.
130 		 */
131 		bool is_sequencer = false;
132 		struct file *fp;
133 		struct vnode *vp;
134 		struct vattr va;
135 		extern const struct cdevsw sequencer_cdevsw;
136 
137 		if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
138 			return EBADF;
139 		if (fp->f_type == DTYPE_VNODE &&
140 		    (vp = (struct vnode *)fp->f_data) != NULL &&
141 		    vp->v_type == VCHR) {
142 			vn_lock(vp, LK_SHARED | LK_RETRY);
143 			error = VOP_GETATTR(vp, &va, l->l_cred);
144 			VOP_UNLOCK(vp);
145 			if (error == 0 &&
146 			    cdevsw_lookup(va.va_rdev) == &sequencer_cdevsw)
147 				is_sequencer = true;
148 		}
149 		if (is_sequencer) {
150 			error = oss_ioctl_sequencer(l, (const void *)LINUX_TO_OSS(uap),
151 						   retval);
152 		}
153 		else {
154 			error = linux_ioctl_termios(l, uap, retval);
155 		}
156 		fd_putfile(SCARG(uap, fd));
157 #else
158 		error = linux_ioctl_termios(l, uap, retval);
159 #endif
160 	}
161 		break;
162 	case '"':
163 		error = linux_ioctl_sg(l, uap, retval);
164 		break;
165 	case 0x89:
166 		error = linux_ioctl_socket(l, uap, retval);
167 		break;
168 	case 0x03:
169 		error = linux_ioctl_hdio(l, uap, retval);
170 		break;
171 	case 0x02:
172 		error = linux_ioctl_fdio(l, uap, retval);
173 		break;
174 	case 0x12:
175 		error = linux_ioctl_blkio(l, uap, retval);
176 		break;
177 	default:
178 		error = linux_machdepioctl(l, uap, retval);
179 		break;
180 	}
181 	if (error == EPASSTHROUGH) {
182 		/*
183 		 * linux returns EINVAL or ENOTTY for not supported ioctls.
184 		 */
185 		error = EINVAL;
186 	}
187 
188 	return error;
189 }
190