Home | History | Annotate | Line # | Download | only in common
linux_termios.c revision 1.36.46.1
      1  1.36.46.1     rmind /*	$NetBSD: linux_termios.c,v 1.36.46.1 2014/05/18 17:45:32 rmind Exp $	*/
      2        1.4       erh 
      3        1.4       erh /*-
      4       1.34        ad  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
      5        1.4       erh  * All rights reserved.
      6        1.4       erh  *
      7        1.4       erh  * This code is derived from software contributed to The NetBSD Foundation
      8        1.6      fvdl  * by Frank van der Linden and Eric Haszlakiewicz.
      9        1.4       erh  *
     10        1.4       erh  * Redistribution and use in source and binary forms, with or without
     11        1.4       erh  * modification, are permitted provided that the following conditions
     12        1.4       erh  * are met:
     13        1.4       erh  * 1. Redistributions of source code must retain the above copyright
     14        1.4       erh  *    notice, this list of conditions and the following disclaimer.
     15        1.4       erh  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.4       erh  *    notice, this list of conditions and the following disclaimer in the
     17        1.4       erh  *    documentation and/or other materials provided with the distribution.
     18        1.4       erh  *
     19        1.4       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.4       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.4       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.4       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.4       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.4       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.4       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.4       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.4       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.4       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.4       erh  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1   mycroft  */
     31       1.14     lukem 
     32       1.14     lukem #include <sys/cdefs.h>
     33  1.36.46.1     rmind __KERNEL_RCSID(0, "$NetBSD: linux_termios.c,v 1.36.46.1 2014/05/18 17:45:32 rmind Exp $");
     34       1.21  christos 
     35       1.21  christos #if defined(_KERNEL_OPT)
     36       1.21  christos #include "opt_ptm.h"
     37       1.21  christos #endif
     38        1.1   mycroft 
     39        1.1   mycroft #include <sys/param.h>
     40        1.1   mycroft #include <sys/proc.h>
     41        1.1   mycroft #include <sys/systm.h>
     42        1.1   mycroft #include <sys/file.h>
     43        1.1   mycroft #include <sys/filedesc.h>
     44        1.1   mycroft #include <sys/ioctl.h>
     45        1.1   mycroft #include <sys/mount.h>
     46        1.1   mycroft #include <sys/termios.h>
     47       1.36  matthias #include <sys/kernel.h>
     48        1.1   mycroft 
     49        1.1   mycroft #include <sys/syscallargs.h>
     50        1.1   mycroft 
     51        1.5  christos #include <compat/linux/common/linux_types.h>
     52        1.5  christos #include <compat/linux/common/linux_ioctl.h>
     53        1.5  christos #include <compat/linux/common/linux_signal.h>
     54        1.5  christos #include <compat/linux/common/linux_util.h>
     55        1.5  christos #include <compat/linux/common/linux_termios.h>
     56       1.32     njoly #include <compat/linux/common/linux_ipc.h>
     57       1.32     njoly #include <compat/linux/common/linux_sem.h>
     58        1.5  christos 
     59        1.1   mycroft #include <compat/linux/linux_syscallargs.h>
     60        1.1   mycroft 
     61       1.26  christos #ifdef DEBUG_LINUX
     62       1.26  christos #define DPRINTF(a)	uprintf a
     63       1.26  christos #else
     64       1.26  christos #define DPRINTF(a)
     65       1.26  christos #endif
     66       1.26  christos 
     67        1.1   mycroft int
     68       1.33       dsl linux_ioctl_termios(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
     69       1.33       dsl {
     70       1.33       dsl 	/* {
     71        1.1   mycroft 		syscallarg(int) fd;
     72        1.1   mycroft 		syscallarg(u_long) com;
     73       1.29  christos 		syscallarg(void *) data;
     74       1.33       dsl 	} */
     75       1.34        ad 	file_t *fp;
     76        1.1   mycroft 	u_long com;
     77        1.1   mycroft 	struct linux_termio tmplt;
     78        1.1   mycroft 	struct linux_termios tmplts;
     79        1.1   mycroft 	struct termios tmpbts;
     80        1.1   mycroft 	int idat;
     81        1.1   mycroft 	struct sys_ioctl_args ia;
     82        1.1   mycroft 	int error;
     83        1.8      fvdl 	char tioclinux;
     84       1.34        ad 	int (*bsdioctl)(file_t *, u_long, void *);
     85        1.1   mycroft 
     86       1.34        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
     87        1.1   mycroft 		return (EBADF);
     88        1.1   mycroft 
     89       1.17      yamt 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
     90       1.17      yamt 		error = EBADF;
     91       1.17      yamt 		goto out;
     92       1.17      yamt 	}
     93       1.17      yamt 
     94        1.9      ross 	bsdioctl = fp->f_ops->fo_ioctl;
     95        1.1   mycroft 	com = SCARG(uap, com);
     96        1.1   mycroft 	retval[0] = 0;
     97       1.22     perry 
     98       1.30      manu 	switch (com) {
     99        1.1   mycroft 	case LINUX_TCGETS:
    100       1.34        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    101        1.1   mycroft 		if (error)
    102       1.17      yamt 			goto out;
    103        1.1   mycroft 		bsd_termios_to_linux_termios(&tmpbts, &tmplts);
    104        1.1   mycroft 		error = copyout(&tmplts, SCARG(uap, data), sizeof tmplts);
    105       1.17      yamt 		goto out;
    106        1.1   mycroft 	case LINUX_TCSETS:
    107        1.1   mycroft 	case LINUX_TCSETSW:
    108        1.1   mycroft 	case LINUX_TCSETSF:
    109        1.1   mycroft 		/*
    110        1.1   mycroft 		 * First fill in all fields, so that we keep the current
    111        1.1   mycroft 		 * values for fields that Linux doesn't know about.
    112        1.1   mycroft 		 */
    113       1.34        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    114        1.1   mycroft 		if (error)
    115       1.17      yamt 			goto out;
    116        1.1   mycroft 		error = copyin(SCARG(uap, data), &tmplts, sizeof tmplts);
    117        1.1   mycroft 		if (error)
    118       1.17      yamt 			goto out;
    119        1.1   mycroft 		linux_termios_to_bsd_termios(&tmplts, &tmpbts);
    120        1.1   mycroft 		switch (com) {
    121        1.1   mycroft 		case LINUX_TCSETS:
    122        1.1   mycroft 			com = TIOCSETA;
    123        1.1   mycroft 			break;
    124        1.1   mycroft 		case LINUX_TCSETSW:
    125        1.1   mycroft 			com = TIOCSETAW;
    126        1.1   mycroft 			break;
    127        1.1   mycroft 		case LINUX_TCSETSF:
    128        1.1   mycroft 			com = TIOCSETAF;
    129        1.1   mycroft 			break;
    130        1.1   mycroft 		}
    131       1.34        ad 		error = (*bsdioctl)(fp, com, &tmpbts);
    132       1.17      yamt 		goto out;
    133        1.1   mycroft 	case LINUX_TCGETA:
    134       1.34        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    135        1.1   mycroft 		if (error)
    136       1.17      yamt 			goto out;
    137        1.1   mycroft 		bsd_termios_to_linux_termio(&tmpbts, &tmplt);
    138        1.1   mycroft 		error = copyout(&tmplt, SCARG(uap, data), sizeof tmplt);
    139       1.17      yamt 		goto out;
    140        1.1   mycroft 	case LINUX_TCSETA:
    141        1.1   mycroft 	case LINUX_TCSETAW:
    142        1.1   mycroft 	case LINUX_TCSETAF:
    143        1.1   mycroft 		/*
    144        1.1   mycroft 		 * First fill in all fields, so that we keep the current
    145        1.1   mycroft 		 * values for fields that Linux doesn't know about.
    146        1.1   mycroft 		 */
    147       1.34        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    148        1.1   mycroft 		if (error)
    149       1.17      yamt 			goto out;
    150        1.1   mycroft 		error = copyin(SCARG(uap, data), &tmplt, sizeof tmplt);
    151        1.1   mycroft 		if (error)
    152       1.17      yamt 			goto out;
    153        1.1   mycroft 		linux_termio_to_bsd_termios(&tmplt, &tmpbts);
    154        1.1   mycroft 		switch (com) {
    155        1.1   mycroft 		case LINUX_TCSETA:
    156        1.1   mycroft 			com = TIOCSETA;
    157        1.1   mycroft 			break;
    158        1.1   mycroft 		case LINUX_TCSETAW:
    159        1.1   mycroft 			com = TIOCSETAW;
    160        1.1   mycroft 			break;
    161        1.1   mycroft 		case LINUX_TCSETAF:
    162        1.1   mycroft 			com = TIOCSETAF;
    163        1.1   mycroft 			break;
    164        1.1   mycroft 		}
    165       1.34        ad 		error = (*bsdioctl)(fp, com, &tmpbts);
    166       1.17      yamt 		goto out;
    167        1.9      ross 	case LINUX_TCFLSH:
    168       1.10    itojun 		switch((u_long)SCARG(uap, data)) {
    169        1.9      ross 		case 0:
    170        1.9      ross 			idat = FREAD;
    171        1.9      ross 			break;
    172        1.9      ross 		case 1:
    173        1.9      ross 			idat = FWRITE;
    174        1.9      ross 			break;
    175        1.9      ross 		case 2:
    176        1.9      ross 			idat = 0;
    177        1.9      ross 			break;
    178        1.9      ross 		default:
    179       1.17      yamt 			error = EINVAL;
    180       1.17      yamt 			goto out;
    181        1.9      ross 		}
    182       1.34        ad 		error = (*bsdioctl)(fp, TIOCFLUSH, &idat);
    183       1.17      yamt 		goto out;
    184        1.1   mycroft 	case LINUX_TIOCGETD:
    185       1.34        ad 		error = (*bsdioctl)(fp, TIOCGETD, &idat);
    186        1.1   mycroft 		if (error)
    187       1.17      yamt 			goto out;
    188        1.1   mycroft 		switch (idat) {
    189        1.1   mycroft 		case TTYDISC:
    190        1.1   mycroft 			idat = LINUX_N_TTY;
    191        1.1   mycroft 			break;
    192        1.1   mycroft 		case SLIPDISC:
    193        1.1   mycroft 			idat = LINUX_N_SLIP;
    194        1.1   mycroft 			break;
    195        1.1   mycroft 		case PPPDISC:
    196        1.1   mycroft 			idat = LINUX_N_PPP;
    197        1.1   mycroft 			break;
    198        1.4       erh 		case STRIPDISC:
    199        1.4       erh 			idat = LINUX_N_STRIP;
    200        1.4       erh 			break;
    201        1.1   mycroft 		/*
    202        1.1   mycroft 		 * Linux does not have the tablet line discipline.
    203        1.1   mycroft 		 */
    204        1.1   mycroft 		case TABLDISC:
    205        1.1   mycroft 		default:
    206        1.1   mycroft 			idat = -1;	/* XXX What should this be? */
    207        1.1   mycroft 			break;
    208        1.1   mycroft 		}
    209        1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    210       1.17      yamt 		goto out;
    211        1.1   mycroft 	case LINUX_TIOCSETD:
    212        1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    213        1.1   mycroft 		if (error)
    214       1.17      yamt 			goto out;
    215        1.1   mycroft 		switch (idat) {
    216        1.1   mycroft 		case LINUX_N_TTY:
    217        1.1   mycroft 			idat = TTYDISC;
    218        1.1   mycroft 			break;
    219        1.1   mycroft 		case LINUX_N_SLIP:
    220        1.1   mycroft 			idat = SLIPDISC;
    221        1.1   mycroft 			break;
    222        1.1   mycroft 		case LINUX_N_PPP:
    223        1.1   mycroft 			idat = PPPDISC;
    224        1.1   mycroft 			break;
    225        1.4       erh 		case LINUX_N_STRIP:
    226        1.4       erh 			idat = STRIPDISC;
    227        1.4       erh 			break;
    228        1.1   mycroft 		/*
    229        1.1   mycroft 		 * We can't handle the mouse line discipline Linux has.
    230        1.1   mycroft 		 */
    231        1.1   mycroft 		case LINUX_N_MOUSE:
    232        1.4       erh 		case LINUX_N_AX25:
    233        1.4       erh 		case LINUX_N_X25:
    234        1.4       erh 		case LINUX_N_6PACK:
    235        1.1   mycroft 		default:
    236       1.17      yamt 			error = EINVAL;
    237       1.17      yamt 			goto out;
    238        1.1   mycroft 		}
    239       1.34        ad 		error = (*bsdioctl)(fp, TIOCSETD, &idat);
    240       1.17      yamt 		goto out;
    241        1.8      fvdl 	case LINUX_TIOCLINUX:
    242        1.8      fvdl 		error = copyin(SCARG(uap, data), &tioclinux, sizeof tioclinux);
    243        1.8      fvdl 		if (error != 0)
    244       1.17      yamt 			goto out;
    245        1.8      fvdl 		switch (tioclinux) {
    246        1.8      fvdl 		case LINUX_TIOCLINUX_KERNMSG:
    247        1.8      fvdl 			/*
    248        1.8      fvdl 			 * XXX needed to not fail for some things. Could
    249        1.8      fvdl 			 * try to use TIOCCONS, but the char argument
    250        1.8      fvdl 			 * specifies the VT #, not an fd.
    251        1.8      fvdl 			 */
    252       1.17      yamt 			error = 0;
    253       1.17      yamt 			goto out;
    254        1.8      fvdl 		case LINUX_TIOCLINUX_COPY:
    255        1.8      fvdl 		case LINUX_TIOCLINUX_PASTE:
    256        1.8      fvdl 		case LINUX_TIOCLINUX_UNBLANK:
    257        1.8      fvdl 		case LINUX_TIOCLINUX_LOADLUT:
    258        1.8      fvdl 		case LINUX_TIOCLINUX_READSHIFT:
    259        1.8      fvdl 		case LINUX_TIOCLINUX_READMOUSE:
    260        1.8      fvdl 		case LINUX_TIOCLINUX_VESABLANK:
    261        1.8      fvdl 		case LINUX_TIOCLINUX_CURCONS:	/* could use VT_GETACTIVE */
    262       1.17      yamt 			error = EINVAL;
    263       1.17      yamt 			goto out;
    264        1.8      fvdl 		}
    265        1.8      fvdl 		break;
    266        1.1   mycroft 	case LINUX_TIOCGWINSZ:
    267        1.1   mycroft 		SCARG(&ia, com) = TIOCGWINSZ;
    268        1.1   mycroft 		break;
    269        1.1   mycroft 	case LINUX_TIOCSWINSZ:
    270        1.1   mycroft 		SCARG(&ia, com) = TIOCSWINSZ;
    271        1.1   mycroft 		break;
    272        1.1   mycroft 	case LINUX_TIOCGPGRP:
    273        1.1   mycroft 		SCARG(&ia, com) = TIOCGPGRP;
    274        1.1   mycroft 		break;
    275        1.1   mycroft 	case LINUX_TIOCSPGRP:
    276        1.1   mycroft 		SCARG(&ia, com) = TIOCSPGRP;
    277        1.1   mycroft 		break;
    278        1.1   mycroft 	case LINUX_FIONREAD:
    279        1.1   mycroft 		SCARG(&ia, com) = FIONREAD;
    280        1.1   mycroft 		break;
    281        1.1   mycroft 	case LINUX_FIONBIO:
    282        1.1   mycroft 		SCARG(&ia, com) = FIONBIO;
    283        1.1   mycroft 		break;
    284        1.1   mycroft 	case LINUX_FIOASYNC:
    285        1.1   mycroft 		SCARG(&ia, com) = FIOASYNC;
    286        1.1   mycroft 		break;
    287        1.1   mycroft 	case LINUX_TIOCEXCL:
    288        1.1   mycroft 		SCARG(&ia, com) = TIOCEXCL;
    289        1.1   mycroft 		break;
    290        1.1   mycroft 	case LINUX_TIOCNXCL:
    291        1.1   mycroft 		SCARG(&ia, com) = TIOCNXCL;
    292        1.1   mycroft 		break;
    293        1.1   mycroft 	case LINUX_TIOCCONS:
    294        1.1   mycroft 		SCARG(&ia, com) = TIOCCONS;
    295        1.1   mycroft 		break;
    296        1.1   mycroft 	case LINUX_TIOCNOTTY:
    297        1.1   mycroft 		SCARG(&ia, com) = TIOCNOTTY;
    298       1.15  augustss 		break;
    299       1.15  augustss 	case LINUX_TCSBRK:
    300       1.36  matthias 		idat = (u_long)SCARG(uap, data);
    301       1.36  matthias 		if (idat != 0)
    302       1.36  matthias 			SCARG(&ia, com) = TIOCDRAIN;
    303       1.36  matthias 		else {
    304       1.36  matthias 			if ((error = (*bsdioctl)(fp, TIOCSBRK, NULL)) != 0)
    305       1.36  matthias 				goto out;
    306       1.36  matthias 			error = tsleep(&idat, PZERO | PCATCH, "linux_tcsbrk", hz / 4);
    307       1.36  matthias 			if (error == EINTR || error == ERESTART) {
    308       1.36  matthias 				(void)(*bsdioctl)(fp, TIOCCBRK, NULL);
    309       1.36  matthias 				error = EINTR;
    310       1.36  matthias 			} else
    311       1.36  matthias 				error = (*bsdioctl)(fp, TIOCCBRK, NULL);
    312       1.36  matthias 			goto out;
    313       1.36  matthias 		}
    314       1.15  augustss 		break;
    315       1.15  augustss 	case LINUX_TIOCMGET:
    316       1.15  augustss 		SCARG(&ia, com) = TIOCMGET;
    317       1.15  augustss 		break;
    318       1.15  augustss 	case LINUX_TIOCMSET:
    319       1.15  augustss 		SCARG(&ia, com) = TIOCMSET;
    320        1.1   mycroft 		break;
    321       1.23  christos 	case LINUX_TIOCMBIC:
    322       1.23  christos 		SCARG(&ia, com) = TIOCMBIC;
    323       1.23  christos 		break;
    324       1.23  christos 	case LINUX_TIOCMBIS:
    325       1.23  christos 		SCARG(&ia, com) = TIOCMBIS;
    326       1.23  christos 		break;
    327       1.21  christos #ifdef LINUX_TIOCGPTN
    328       1.21  christos 	case LINUX_TIOCGPTN:
    329       1.21  christos #ifndef NO_DEV_PTM
    330       1.21  christos 		{
    331       1.31       dsl 			struct ptmget ptm;
    332       1.21  christos 
    333       1.34        ad 			error = (*bsdioctl)(fp, TIOCPTSNAME, &ptm);
    334       1.31       dsl 			if (error != 0)
    335       1.21  christos 				goto out;
    336       1.21  christos 			error = copyout(&ptm.sfd, SCARG(uap, data),
    337       1.21  christos 			    sizeof(ptm.sfd));
    338       1.21  christos 			goto out;
    339       1.21  christos 		}
    340       1.21  christos #endif /* NO_DEV_PTM */
    341       1.21  christos #endif /* LINUX_TIOCGPTN */
    342       1.26  christos #ifdef LINUX_TIOCSPTLCK
    343       1.26  christos 	case LINUX_TIOCSPTLCK:
    344       1.34        ad 			fd_putfile(SCARG(uap, fd));
    345       1.26  christos 			error = copyin(SCARG(uap, data), &idat, sizeof(idat));
    346       1.26  christos 			if (error)
    347       1.26  christos 				return error;
    348       1.26  christos 			DPRINTF(("TIOCSPTLCK %d\n", idat));
    349       1.26  christos 			return 0;
    350       1.26  christos #endif
    351  1.36.46.1     rmind 	case LINUX_TCXONC:
    352  1.36.46.1     rmind 		idat = (u_long)SCARG(uap, data);
    353  1.36.46.1     rmind 		switch (idat) {
    354  1.36.46.1     rmind 		case LINUX_TCOOFF:
    355  1.36.46.1     rmind 			SCARG(&ia, com) = TIOCSTOP;
    356  1.36.46.1     rmind 			break;
    357  1.36.46.1     rmind 		case LINUX_TCOON:
    358  1.36.46.1     rmind 			SCARG(&ia, com) = TIOCSTART;
    359  1.36.46.1     rmind 			break;
    360  1.36.46.1     rmind 		case LINUX_TCIOFF:
    361  1.36.46.1     rmind 		case LINUX_TCION:
    362  1.36.46.1     rmind 		default:
    363  1.36.46.1     rmind 			error = EINVAL;
    364  1.36.46.1     rmind 			goto out;
    365  1.36.46.1     rmind 		}
    366  1.36.46.1     rmind 		break;
    367        1.1   mycroft 	default:
    368       1.17      yamt 		error = EINVAL;
    369       1.17      yamt 		goto out;
    370        1.1   mycroft 	}
    371        1.1   mycroft 
    372        1.1   mycroft 	SCARG(&ia, fd) = SCARG(uap, fd);
    373        1.1   mycroft 	SCARG(&ia, data) = SCARG(uap, data);
    374       1.17      yamt 	error = sys_ioctl(curlwp, &ia, retval);
    375       1.17      yamt out:
    376       1.34        ad 	fd_putfile(SCARG(uap, fd));
    377       1.17      yamt 	return error;
    378        1.1   mycroft }
    379