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