xref: /netbsd-src/sys/compat/linux/common/linux_ioctl.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: linux_ioctl.c,v 1.27 2001/06/14 20:32:43 thorpej 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 #if defined(_KERNEL_OPT)
40 #include "sequencer.h"
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/ioctl.h>
48 #include <sys/mount.h>
49 #include <sys/file.h>
50 #include <sys/vnode.h>
51 #include <sys/filedesc.h>
52 
53 #include <sys/socket.h>
54 #include <net/if.h>
55 #include <sys/sockio.h>
56 
57 #include <sys/syscallargs.h>
58 
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_ioctl.h>
62 
63 #include <compat/linux/linux_syscallargs.h>
64 
65 #include <compat/ossaudio/ossaudio.h>
66 #define LINUX_TO_OSS(v) (v)	/* do nothing, same ioctl() encoding */
67 
68 /*
69  * Most ioctl command are just converted to their NetBSD values,
70  * and passed on. The ones that take structure pointers and (flag)
71  * values need some massaging. This is done the usual way by
72  * allocating stackgap memory, letting the actual ioctl call do its
73  * work there and converting back the data afterwards.
74  */
75 int
76 linux_sys_ioctl(p, v, retval)
77 	struct proc *p;
78 	void *v;
79 	register_t *retval;
80 {
81 	struct linux_sys_ioctl_args /* {
82 		syscallarg(int) fd;
83 		syscallarg(u_long) com;
84 		syscallarg(caddr_t) data;
85 	} */ *uap = v;
86 
87 	switch (LINUX_IOCGROUP(SCARG(uap, com))) {
88 	case 'M':
89 		return oss_ioctl_mixer(p, LINUX_TO_OSS(v), retval);
90 	case 'Q':
91 		return oss_ioctl_sequencer(p, LINUX_TO_OSS(v), retval);
92 	case 'P':
93 		return oss_ioctl_audio(p, LINUX_TO_OSS(v), retval);
94 	case 'S':
95 		return linux_ioctl_cdrom(p, uap, retval);
96 	case 't':
97 	case 'f':
98 		return linux_ioctl_termios(p, uap, retval);
99 	case 'T':
100 	{
101 #if NSEQUENCER > 0
102 /* XXX XAX 2x check this. */
103 		/*
104 		 * Both termios and the MIDI sequncer use 'T' to identify
105 		 * the ioctl, so we have to differentiate them in another
106 		 * way.  We do it by indexing in the cdevsw with the major
107 		 * device number and check if that is the sequencer entry.
108 		 */
109 		struct file *fp;
110 		struct filedesc *fdp;
111 		struct vnode *vp;
112 		struct vattr va;
113 		extern int sequencerioctl
114 			__P((dev_t, u_long, caddr_t, int, struct proc *));
115 
116 		fdp = p->p_fd;
117 		if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
118 			return EBADF;
119 		if (fp->f_type == DTYPE_VNODE &&
120 		    (vp = (struct vnode *)fp->f_data) != NULL &&
121 		    vp->v_type == VCHR &&
122 		    VOP_GETATTR(vp, &va, p->p_ucred, p) == 0 &&
123 		    major(va.va_rdev) < nchrdev &&
124 		    cdevsw[major(va.va_rdev)].d_ioctl == &sequencerioctl)
125 			return oss_ioctl_sequencer(p, (void*)LINUX_TO_OSS(uap),
126 						   retval);
127 		else
128 #endif
129 			return linux_ioctl_termios(p, uap, retval);
130 	}
131 	case 0x89:
132 		return linux_ioctl_socket(p, uap, retval);
133 	case 0x03:
134 		return linux_ioctl_hdio(p, uap, retval);
135 	case 0x02:
136 		return linux_ioctl_fdio(p, uap, retval);
137 	case 0x12:
138 		return linux_ioctl_blkio(p, uap, retval);
139 	default:
140 		return linux_machdepioctl(p, uap, retval);
141 	}
142 }
143