xref: /netbsd-src/sys/compat/linux/common/linux_ioctl.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: linux_ioctl.c,v 1.44 2006/05/14 21:24:50 elad 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.44 2006/05/14 21:24:50 elad 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/sa.h>
61 #include <sys/syscallargs.h>
62 
63 #include <compat/linux/common/linux_types.h>
64 #include <compat/linux/common/linux_signal.h>
65 #include <compat/linux/common/linux_ioctl.h>
66 
67 #include <compat/linux/linux_syscallargs.h>
68 
69 #include <compat/ossaudio/ossaudio.h>
70 #define LINUX_TO_OSS(v) (v)	/* do nothing, same ioctl() encoding */
71 
72 /*
73  * Most ioctl command are just converted to their NetBSD values,
74  * and passed on. The ones that take structure pointers and (flag)
75  * values need some massaging. This is done the usual way by
76  * allocating stackgap memory, letting the actual ioctl call do its
77  * work there and converting back the data afterwards.
78  */
79 int
80 linux_sys_ioctl(l, v, retval)
81 	struct lwp *l;
82 	void *v;
83 	register_t *retval;
84 {
85 	struct linux_sys_ioctl_args /* {
86 		syscallarg(int) fd;
87 		syscallarg(u_long) com;
88 		syscallarg(caddr_t) data;
89 	} */ *uap = v;
90 	int error;
91 
92 	switch (LINUX_IOCGROUP(SCARG(uap, com))) {
93 	case 'M':
94 		error = oss_ioctl_mixer(l, LINUX_TO_OSS(v), retval);
95 		break;
96 	case 'Q':
97 		error = oss_ioctl_sequencer(l, LINUX_TO_OSS(v), retval);
98 		break;
99 	case 'P':
100 		error = oss_ioctl_audio(l, LINUX_TO_OSS(v), retval);
101 		break;
102 	case 'r': /* VFAT ioctls; not yet supported */
103 		error = ENOSYS;
104 		break;
105 	case 'S':
106 		error = linux_ioctl_cdrom(l, uap, retval);
107 		break;
108 	case 't':
109 	case 'f':
110 		error = linux_ioctl_termios(l, uap, retval);
111 		break;
112 	case 'm':
113 		error = linux_ioctl_mtio(l, uap, retval);
114 		break;
115 	case 'T':
116 	{
117 #if NSEQUENCER > 0
118 /* XXX XAX 2x check this. */
119 		/*
120 		 * Both termios and the MIDI sequncer use 'T' to identify
121 		 * the ioctl, so we have to differentiate them in another
122 		 * way.  We do it by indexing in the cdevsw with the major
123 		 * device number and check if that is the sequencer entry.
124 		 */
125 		struct file *fp;
126 		struct filedesc *fdp;
127 		struct vnode *vp;
128 		struct vattr va;
129 		extern const struct cdevsw sequencer_cdevsw;
130 		struct proc *p = l->l_proc;
131 
132 		fdp = p->p_fd;
133 		if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
134 			return EBADF;
135 		FILE_USE(fp);
136 		if (fp->f_type == DTYPE_VNODE &&
137 		    (vp = (struct vnode *)fp->f_data) != NULL &&
138 		    vp->v_type == VCHR &&
139 		    VOP_GETATTR(vp, &va, p->p_cred, l) == 0 &&
140 		    cdevsw_lookup(va.va_rdev) == &sequencer_cdevsw) {
141 			error = oss_ioctl_sequencer(l, (void*)LINUX_TO_OSS(uap),
142 						   retval);
143 		}
144 		else {
145 			error = linux_ioctl_termios(l, uap, retval);
146 		}
147 		FILE_UNUSE(fp, l);
148 #else
149 		error = linux_ioctl_termios(l, uap, retval);
150 #endif
151 	}
152 		break;
153 	case '"':
154 		error = linux_ioctl_sg(l, uap, retval);
155 		break;
156 	case 0x89:
157 		error = linux_ioctl_socket(l, uap, retval);
158 		break;
159 	case 0x03:
160 		error = linux_ioctl_hdio(l, uap, retval);
161 		break;
162 	case 0x02:
163 		error = linux_ioctl_fdio(l, uap, retval);
164 		break;
165 	case 0x12:
166 		error = linux_ioctl_blkio(l, uap, retval);
167 		break;
168 	default:
169 		error = linux_machdepioctl(l, uap, retval);
170 		break;
171 	}
172 	if (error == EPASSTHROUGH) {
173 		/*
174 		 * linux returns EINVAL or ENOTTY for not supported ioctls.
175 		 */
176 		error = EINVAL;
177 	}
178 
179 	return error;
180 }
181