xref: /netbsd-src/sys/compat/linux/common/linux_ioctl.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: linux_ioctl.c,v 1.50 2007/11/26 19:01:31 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: linux_ioctl.c,v 1.50 2007/11/26 19:01:31 pooka Exp $");
41 
42 #if defined(_KERNEL_OPT)
43 #include "sequencer.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/conf.h>
50 #include <sys/ioctl.h>
51 #include <sys/mount.h>
52 #include <sys/file.h>
53 #include <sys/vnode.h>
54 #include <sys/filedesc.h>
55 
56 #include <sys/socket.h>
57 #include <net/if.h>
58 #include <sys/sockio.h>
59 
60 #include <sys/syscallargs.h>
61 
62 #include <compat/linux/common/linux_types.h>
63 #include <compat/linux/common/linux_signal.h>
64 #include <compat/linux/common/linux_ioctl.h>
65 #include <compat/linux/common/linux_ipc.h>
66 #include <compat/linux/common/linux_sem.h>
67 
68 #include <compat/linux/linux_syscallargs.h>
69 
70 #include <compat/ossaudio/ossaudio.h>
71 #define LINUX_TO_OSS(v) (v)	/* do nothing, same ioctl() encoding */
72 
73 /*
74  * Most ioctl command are just converted to their NetBSD values,
75  * and passed on. The ones that take structure pointers and (flag)
76  * values need some massaging.
77  */
78 int
79 linux_sys_ioctl(l, v, retval)
80 	struct lwp *l;
81 	void *v;
82 	register_t *retval;
83 {
84 	struct linux_sys_ioctl_args /* {
85 		syscallarg(int) fd;
86 		syscallarg(u_long) com;
87 		syscallarg(void *) data;
88 	} */ *uap = v;
89 	int error;
90 
91 	switch (LINUX_IOCGROUP(SCARG(uap, com))) {
92 	case 'M':
93 		error = oss_ioctl_mixer(l, LINUX_TO_OSS(v), retval);
94 		break;
95 	case 'Q':
96 		error = oss_ioctl_sequencer(l, LINUX_TO_OSS(v), retval);
97 		break;
98 	case 'P':
99 		error = oss_ioctl_audio(l, LINUX_TO_OSS(v), retval);
100 		break;
101 	case 'r': /* VFAT ioctls; not yet supported */
102 		error = ENOSYS;
103 		break;
104 	case 'S':
105 		error = linux_ioctl_cdrom(l, uap, retval);
106 		break;
107 	case 't':
108 	case 'f':
109 		error = linux_ioctl_termios(l, uap, retval);
110 		break;
111 	case 'm':
112 		error = linux_ioctl_mtio(l, uap, retval);
113 		break;
114 	case 'T':
115 	{
116 #if NSEQUENCER > 0
117 /* XXX XAX 2x check this. */
118 		/*
119 		 * Both termios and the MIDI sequncer use 'T' to identify
120 		 * the ioctl, so we have to differentiate them in another
121 		 * way.  We do it by indexing in the cdevsw with the major
122 		 * device number and check if that is the sequencer entry.
123 		 */
124 		struct file *fp;
125 		struct filedesc *fdp;
126 		struct vnode *vp;
127 		struct vattr va;
128 		extern const struct cdevsw sequencer_cdevsw;
129 
130 		fdp = l->l_proc->p_fd;
131 		if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
132 			return EBADF;
133 		FILE_USE(fp);
134 		if (fp->f_type == DTYPE_VNODE &&
135 		    (vp = (struct vnode *)fp->f_data) != NULL &&
136 		    vp->v_type == VCHR &&
137 		    VOP_GETATTR(vp, &va, l->l_cred) == 0 &&
138 		    cdevsw_lookup(va.va_rdev) == &sequencer_cdevsw) {
139 			error = oss_ioctl_sequencer(l, (void*)LINUX_TO_OSS(uap),
140 						   retval);
141 		}
142 		else {
143 			error = linux_ioctl_termios(l, uap, retval);
144 		}
145 		FILE_UNUSE(fp, l);
146 #else
147 		error = linux_ioctl_termios(l, uap, retval);
148 #endif
149 	}
150 		break;
151 	case '"':
152 		error = linux_ioctl_sg(l, uap, retval);
153 		break;
154 	case 0x89:
155 		error = linux_ioctl_socket(l, uap, retval);
156 		break;
157 	case 0x03:
158 		error = linux_ioctl_hdio(l, uap, retval);
159 		break;
160 	case 0x02:
161 		error = linux_ioctl_fdio(l, uap, retval);
162 		break;
163 	case 0x12:
164 		error = linux_ioctl_blkio(l, uap, retval);
165 		break;
166 	default:
167 		error = linux_machdepioctl(l, uap, retval);
168 		break;
169 	}
170 	if (error == EPASSTHROUGH) {
171 		/*
172 		 * linux returns EINVAL or ENOTTY for not supported ioctls.
173 		 */
174 		error = EINVAL;
175 	}
176 
177 	return error;
178 }
179