Home | History | Annotate | Line # | Download | only in common
linux_ioctl.c revision 1.58.10.1
      1 /*	$NetBSD: linux_ioctl.c,v 1.58.10.1 2016/07/20 23:47:55 pgoyette 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.58.10.1 2016/07/20 23:47:55 pgoyette 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 #include <dev/ic/mfiio.h>
     67 #define LINUX_MEGARAID_CMD	_LINUX_IOWR('M', 1, struct mfi_ioc_packet)
     68 #define LINUX_MEGARAID_GET_AEN	_LINUX_IOW('M', 3, struct mfi_ioc_aen)
     69 
     70 /*
     71  * Most ioctl command are just converted to their NetBSD values,
     72  * and passed on. The ones that take structure pointers and (flag)
     73  * values need some massaging.
     74  */
     75 int
     76 linux_sys_ioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
     77 {
     78 	/* {
     79 		syscallarg(int) fd;
     80 		syscallarg(u_long) com;
     81 		syscallarg(void *) data;
     82 	} */
     83 	int error;
     84 
     85 	switch (LINUX_IOCGROUP(SCARG(uap, com))) {
     86 	case 'M':
     87 		switch(SCARG(uap, com)) {
     88 		case LINUX_MEGARAID_CMD:
     89 		case LINUX_MEGARAID_GET_AEN:
     90 		{
     91 			struct sys_ioctl_args ua;
     92 			u_long com = 0;
     93 			if (SCARG(uap, com) & IOC_IN)
     94 				com |= IOC_OUT;
     95 			if (SCARG(uap, com) & IOC_OUT)
     96 				com |= IOC_IN;
     97 			SCARG(&ua, fd) = SCARG(uap, fd);
     98 			SCARG(&ua, com) = SCARG(uap, com);
     99 			SCARG(&ua, com) &= ~IOC_DIRMASK;
    100 			SCARG(&ua, com) |= com;
    101 			SCARG(&ua, data) = SCARG(uap, data);
    102 			error = sys_ioctl(l, (const void *)&ua, retval);
    103 			break;
    104 		}
    105 		default:
    106 			error = oss_ioctl_mixer(l, LINUX_TO_OSS(uap), retval);
    107 			break;
    108 		}
    109 		break;
    110 	case 'Q':
    111 		error = oss_ioctl_sequencer(l, LINUX_TO_OSS(uap), retval);
    112 		break;
    113 	case 'P':
    114 		error = oss_ioctl_audio(l, LINUX_TO_OSS(uap), retval);
    115 		break;
    116 	case 'V':	/* video4linux2 */
    117 	case 'd':	/* drm */
    118 	{
    119 		struct sys_ioctl_args ua;
    120 		u_long com = 0;
    121 		if (SCARG(uap, com) & IOC_IN)
    122 			com |= IOC_OUT;
    123 		if (SCARG(uap, com) & IOC_OUT)
    124 			com |= IOC_IN;
    125 		SCARG(&ua, fd) = SCARG(uap, fd);
    126 		SCARG(&ua, com) = SCARG(uap, com);
    127 		SCARG(&ua, com) &= ~IOC_DIRMASK;
    128 		SCARG(&ua, com) |= com;
    129 		SCARG(&ua, data) = SCARG(uap, data);
    130 		error = sys_ioctl(l, (const void *)&ua, retval);
    131 		break;
    132 	}
    133 	case 'r': /* VFAT ioctls; not yet supported */
    134 		error = ENOSYS;
    135 		break;
    136 	case 'S':
    137 		error = linux_ioctl_cdrom(l, uap, retval);
    138 		break;
    139 	case 't':
    140 	case 'f':
    141 		error = linux_ioctl_termios(l, uap, retval);
    142 		break;
    143 	case 'm':
    144 		error = linux_ioctl_mtio(l, uap, retval);
    145 		break;
    146 	case 'T':
    147 	{
    148 #if NSEQUENCER > 0
    149 /* XXX XAX 2x check this. */
    150 		/*
    151 		 * Both termios and the MIDI sequencer use 'T' to identify
    152 		 * the ioctl, so we have to differentiate them in another
    153 		 * way.  We do it by indexing in the cdevsw with the major
    154 		 * device number and check if that is the sequencer entry.
    155 		 */
    156 		bool is_sequencer = false;
    157 		struct file *fp;
    158 		struct vnode *vp;
    159 		struct vattr va;
    160 		extern const struct cdevsw sequencer_cdevsw;
    161 		const struct cdevsw *cdev = NULL;
    162 
    163 		if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    164 			return EBADF;
    165 		if (fp->f_type == DTYPE_VNODE &&
    166 		    (vp = (struct vnode *)fp->f_data) != NULL &&
    167 		    vp->v_type == VCHR) {
    168 			vn_lock(vp, LK_SHARED | LK_RETRY);
    169 			error = VOP_GETATTR(vp, &va, l->l_cred);
    170 			VOP_UNLOCK(vp);
    171 			if (error == 0 &&
    172 			    (cdev = cdevsw_lookup_acquire(va.va_rdev)) ==
    173 					&sequencer_cdevsw)
    174 				is_sequencer = true;
    175 		}
    176 		if (is_sequencer) {
    177 			error = oss_ioctl_sequencer(l,
    178 					(const void *)LINUX_TO_OSS(uap),
    179 					retval);
    180 		}
    181 		else {
    182 			error = linux_ioctl_termios(l, uap, retval);
    183 		}
    184 		fd_putfile(SCARG(uap, fd));
    185 		if (cdev != NULL)
    186 			cdevsw_release(cdev);
    187 #else
    188 		error = linux_ioctl_termios(l, uap, retval);
    189 #endif
    190 	}
    191 		break;
    192 	case '"':
    193 		error = linux_ioctl_sg(l, uap, retval);
    194 		break;
    195 	case 0x89:
    196 		error = linux_ioctl_socket(l, uap, retval);
    197 		break;
    198 	case 0x03:
    199 		error = linux_ioctl_hdio(l, uap, retval);
    200 		break;
    201 	case 0x02:
    202 		error = linux_ioctl_fdio(l, uap, retval);
    203 		break;
    204 	case 0x12:
    205 		error = linux_ioctl_blkio(l, uap, retval);
    206 		break;
    207 	default:
    208 		error = linux_machdepioctl(l, uap, retval);
    209 		break;
    210 	}
    211 	if (error == EPASSTHROUGH) {
    212 		/*
    213 		 * linux returns EINVAL or ENOTTY for not supported ioctls.
    214 		 */
    215 		error = EINVAL;
    216 	}
    217 
    218 	return error;
    219 }
    220