Home | History | Annotate | Line # | Download | only in common
linux32_termios.c revision 1.14.54.1
      1  1.14.54.1    martin /*	$NetBSD: linux32_termios.c,v 1.14.54.1 2020/01/21 19:23:37 martin Exp $ */
      2        1.1      manu 
      3        1.1      manu /*-
      4       1.10        ad  * Copyright (c) 1995-2006, 2008  The NetBSD Foundation, Inc.
      5        1.1      manu  * All rights reserved.
      6        1.1      manu  *
      7        1.1      manu  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1      manu  * by Frank van der Linden, Eric Haszlakiewicz, and Emmanuel Dreyfus.
      9        1.1      manu  *
     10        1.1      manu  * Redistribution and use in source and binary forms, with or without
     11        1.1      manu  * modification, are permitted provided that the following conditions
     12        1.1      manu  * are met:
     13        1.1      manu  * 1. Redistributions of source code must retain the above copyright
     14        1.1      manu  *    notice, this list of conditions and the following disclaimer.
     15        1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     17        1.1      manu  *    documentation and/or other materials provided with the distribution.
     18        1.1      manu  *
     19        1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1      manu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1      manu  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1      manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1      manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1      manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1      manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1      manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1      manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1      manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1      manu  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1      manu  */
     31        1.1      manu 
     32        1.1      manu #include <sys/cdefs.h>
     33  1.14.54.1    martin __KERNEL_RCSID(0, "$NetBSD: linux32_termios.c,v 1.14.54.1 2020/01/21 19:23:37 martin Exp $");
     34        1.1      manu 
     35       1.14        ad #ifdef _KERNEL_OPT
     36        1.1      manu #include "opt_compat_linux32.h"
     37       1.14        ad #endif
     38        1.1      manu 
     39        1.1      manu #include <sys/types.h>
     40        1.1      manu #include <sys/param.h>
     41        1.1      manu #include <sys/time.h>
     42        1.1      manu #include <sys/ucred.h>
     43        1.1      manu #include <sys/proc.h>
     44        1.1      manu #include <sys/lwp.h>
     45        1.1      manu #include <sys/file.h>
     46        1.1      manu #include <sys/filedesc.h>
     47        1.1      manu #include <sys/fcntl.h>
     48        1.1      manu #include <sys/termios.h>
     49       1.13  matthias #include <sys/kernel.h>
     50        1.1      manu 
     51        1.1      manu #include <compat/netbsd32/netbsd32.h>
     52        1.1      manu #include <compat/netbsd32/netbsd32_syscallargs.h>
     53        1.1      manu 
     54        1.1      manu #include <compat/linux32/common/linux32_types.h>
     55        1.1      manu #include <compat/linux32/common/linux32_signal.h>
     56        1.1      manu #include <compat/linux32/common/linux32_ioctl.h>
     57        1.1      manu #include <compat/linux32/common/linux32_machdep.h>
     58        1.1      manu #include <compat/linux32/common/linux32_termios.h>
     59        1.1      manu #include <compat/linux32/linux32_syscallargs.h>
     60        1.1      manu 
     61        1.1      manu #include <compat/linux/common/linux_types.h>
     62        1.1      manu #include <compat/linux/common/linux_signal.h>
     63        1.1      manu #include <compat/linux/common/linux_util.h>
     64        1.1      manu #include <compat/linux/common/linux_termios.h>
     65       1.14        ad #include <compat/linux/common/linux_ipc.h>
     66       1.14        ad #include <compat/linux/common/linux_sem.h>
     67        1.1      manu #include <compat/linux/linux_syscallargs.h>
     68        1.1      manu 
     69        1.1      manu int
     70        1.9       dsl linux32_ioctl_termios(struct lwp *l, const struct linux32_sys_ioctl_args *uap, register_t *retval)
     71        1.9       dsl {
     72        1.9       dsl 	/* {
     73        1.1      manu 		syscallarg(int) fd;
     74        1.1      manu 		syscallarg(netbsd32_u_long) com;
     75        1.1      manu 		syscallarg(netbsd32_charp) data;
     76        1.9       dsl 	} */
     77       1.10        ad 	file_t *fp;
     78        1.1      manu 	u_long com;
     79        1.1      manu 	struct linux32_termio tmplt;
     80        1.1      manu 	struct linux32_termios tmplts;
     81        1.1      manu 	struct termios tmpbts;
     82        1.1      manu 	int idat;
     83        1.2      manu 	struct netbsd32_ioctl_args ia;
     84        1.1      manu 	int error;
     85        1.1      manu 	char tioclinux;
     86       1.10        ad 	int (*bsdioctl)(file_t *, u_long, void *);
     87        1.1      manu 
     88       1.10        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
     89        1.1      manu 		return (EBADF);
     90        1.1      manu 
     91        1.1      manu 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
     92       1.10        ad 		fd_putfile(SCARG(uap, fd));
     93        1.1      manu 		error = EBADF;
     94        1.1      manu 		goto out;
     95        1.1      manu 	}
     96        1.1      manu 
     97        1.1      manu 	bsdioctl = fp->f_ops->fo_ioctl;
     98        1.1      manu 	com = SCARG(uap, com);
     99        1.1      manu 	retval[0] = 0;
    100        1.1      manu 
    101        1.7     njoly 	switch (com) {
    102        1.1      manu 	case LINUX32_TCGETS:
    103       1.10        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    104        1.1      manu 		if (error)
    105        1.1      manu 			goto out;
    106        1.1      manu 		bsd_termios_to_linux32_termios(&tmpbts, &tmplts);
    107        1.5       dsl 		error = copyout(&tmplts, SCARG_P32(uap, data), sizeof tmplts);
    108        1.1      manu 		goto out;
    109        1.1      manu 	case LINUX32_TCSETS:
    110        1.1      manu 	case LINUX32_TCSETSW:
    111        1.1      manu 	case LINUX32_TCSETSF:
    112        1.1      manu 		/*
    113        1.1      manu 		 * First fill in all fields, so that we keep the current
    114        1.1      manu 		 * values for fields that Linux doesn't know about.
    115        1.1      manu 		 */
    116       1.10        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    117        1.1      manu 		if (error)
    118        1.1      manu 			goto out;
    119        1.5       dsl 		if ((error = copyin(SCARG_P32(uap, data),
    120        1.1      manu 		    &tmplts, sizeof tmplts)) != 0)
    121        1.1      manu 			goto out;
    122        1.1      manu 		linux32_termios_to_bsd_termios(&tmplts, &tmpbts);
    123        1.1      manu 		switch (com) {
    124        1.1      manu 		case LINUX32_TCSETS:
    125        1.1      manu 			com = TIOCSETA;
    126        1.1      manu 			break;
    127        1.1      manu 		case LINUX32_TCSETSW:
    128        1.1      manu 			com = TIOCSETAW;
    129        1.1      manu 			break;
    130        1.1      manu 		case LINUX32_TCSETSF:
    131        1.1      manu 			com = TIOCSETAF;
    132        1.1      manu 			break;
    133        1.1      manu 		}
    134       1.10        ad 		error = (*bsdioctl)(fp, com, &tmpbts);
    135        1.1      manu 		goto out;
    136        1.1      manu 	case LINUX32_TCGETA:
    137       1.10        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    138        1.1      manu 		if (error)
    139        1.1      manu 			goto out;
    140        1.1      manu 		bsd_termios_to_linux32_termio(&tmpbts, &tmplt);
    141        1.5       dsl 		error = copyout(&tmplt, SCARG_P32(uap, data), sizeof tmplt);
    142        1.1      manu 		goto out;
    143        1.1      manu 	case LINUX32_TCSETA:
    144        1.1      manu 	case LINUX32_TCSETAW:
    145        1.1      manu 	case LINUX32_TCSETAF:
    146        1.1      manu 		/*
    147        1.1      manu 		 * First fill in all fields, so that we keep the current
    148        1.1      manu 		 * values for fields that Linux doesn't know about.
    149        1.1      manu 		 */
    150       1.10        ad 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    151        1.1      manu 		if (error)
    152        1.1      manu 			goto out;
    153        1.5       dsl 		if ((error = copyin(SCARG_P32(uap, data),
    154        1.1      manu 		    &tmplt, sizeof tmplt)) != 0)
    155        1.1      manu 			goto out;
    156        1.1      manu 		linux32_termio_to_bsd_termios(&tmplt, &tmpbts);
    157        1.1      manu 		switch (com) {
    158        1.1      manu 		case LINUX32_TCSETA:
    159        1.1      manu 			com = TIOCSETA;
    160        1.1      manu 			break;
    161        1.1      manu 		case LINUX32_TCSETAW:
    162        1.1      manu 			com = TIOCSETAW;
    163        1.1      manu 			break;
    164        1.1      manu 		case LINUX32_TCSETAF:
    165        1.1      manu 			com = TIOCSETAF;
    166        1.1      manu 			break;
    167        1.1      manu 		}
    168       1.10        ad 		error = (*bsdioctl)(fp, com, &tmpbts);
    169        1.1      manu 		goto out;
    170        1.1      manu 	case LINUX32_TCFLSH:
    171        1.5       dsl 		switch((u_long)SCARG_P32(uap, data)) {
    172        1.1      manu 		case 0:
    173        1.1      manu 			idat = FREAD;
    174        1.1      manu 			break;
    175        1.1      manu 		case 1:
    176        1.1      manu 			idat = FWRITE;
    177        1.1      manu 			break;
    178        1.1      manu 		case 2:
    179        1.1      manu 			idat = 0;
    180        1.1      manu 			break;
    181        1.1      manu 		default:
    182        1.1      manu 			error = EINVAL;
    183        1.1      manu 			goto out;
    184        1.1      manu 		}
    185       1.10        ad 		error = (*bsdioctl)(fp, TIOCFLUSH, &idat);
    186        1.1      manu 		goto out;
    187        1.1      manu 	case LINUX32_TIOCGETD:
    188       1.10        ad 		error = (*bsdioctl)(fp, TIOCGETD, &idat);
    189        1.1      manu 		if (error)
    190        1.1      manu 			goto out;
    191        1.1      manu 		switch (idat) {
    192        1.1      manu 		case TTYDISC:
    193        1.1      manu 			idat = LINUX_N_TTY;
    194        1.1      manu 			break;
    195        1.1      manu 		case SLIPDISC:
    196        1.1      manu 			idat = LINUX_N_SLIP;
    197        1.1      manu 			break;
    198        1.1      manu 		case PPPDISC:
    199        1.1      manu 			idat = LINUX_N_PPP;
    200        1.1      manu 			break;
    201        1.1      manu 		case STRIPDISC:
    202        1.1      manu 			idat = LINUX_N_STRIP;
    203        1.1      manu 			break;
    204        1.1      manu 		/*
    205        1.1      manu 		 * Linux does not have the tablet line discipline.
    206        1.1      manu 		 */
    207        1.1      manu 		case TABLDISC:
    208        1.1      manu 		default:
    209        1.1      manu 			idat = -1;	/* XXX What should this be? */
    210        1.1      manu 			break;
    211        1.1      manu 		}
    212        1.5       dsl 		error = copyout(&idat, SCARG_P32(uap, data), sizeof idat);
    213        1.1      manu 		goto out;
    214        1.1      manu 	case LINUX32_TIOCSETD:
    215        1.5       dsl 		if ((error = copyin(SCARG_P32(uap, data),
    216        1.1      manu 		    &idat, sizeof idat)) != 0)
    217        1.1      manu 			goto out;
    218        1.1      manu 		switch (idat) {
    219        1.1      manu 		case LINUX_N_TTY:
    220        1.1      manu 			idat = TTYDISC;
    221        1.1      manu 			break;
    222        1.1      manu 		case LINUX_N_SLIP:
    223        1.1      manu 			idat = SLIPDISC;
    224        1.1      manu 			break;
    225        1.1      manu 		case LINUX_N_PPP:
    226        1.1      manu 			idat = PPPDISC;
    227        1.1      manu 			break;
    228        1.1      manu 		case LINUX_N_STRIP:
    229        1.1      manu 			idat = STRIPDISC;
    230        1.1      manu 			break;
    231        1.1      manu 		/*
    232        1.1      manu 		 * We can't handle the mouse line discipline Linux has.
    233        1.1      manu 		 */
    234        1.1      manu 		case LINUX_N_MOUSE:
    235        1.1      manu 		case LINUX_N_AX25:
    236        1.1      manu 		case LINUX_N_X25:
    237        1.1      manu 		case LINUX_N_6PACK:
    238        1.1      manu 		default:
    239        1.1      manu 			error = EINVAL;
    240        1.1      manu 			goto out;
    241        1.1      manu 		}
    242       1.10        ad 		error = (*bsdioctl)(fp, TIOCSETD, &idat);
    243        1.1      manu 		goto out;
    244        1.1      manu 	case LINUX32_TIOCLINUX:
    245        1.5       dsl 		if ((error = copyin(SCARG_P32(uap, data),
    246        1.1      manu 		    &tioclinux, sizeof tioclinux)) != 0)
    247        1.1      manu 			goto out;
    248        1.1      manu 		switch (tioclinux) {
    249        1.1      manu 		case LINUX_TIOCLINUX_KERNMSG:
    250        1.1      manu 			/*
    251        1.1      manu 			 * XXX needed to not fail for some things. Could
    252        1.1      manu 			 * try to use TIOCCONS, but the char argument
    253        1.1      manu 			 * specifies the VT #, not an fd.
    254        1.1      manu 			 */
    255        1.1      manu 			error = 0;
    256        1.1      manu 			goto out;
    257        1.1      manu 		case LINUX_TIOCLINUX_COPY:
    258        1.1      manu 		case LINUX_TIOCLINUX_PASTE:
    259        1.1      manu 		case LINUX_TIOCLINUX_UNBLANK:
    260        1.1      manu 		case LINUX_TIOCLINUX_LOADLUT:
    261        1.1      manu 		case LINUX_TIOCLINUX_READSHIFT:
    262        1.1      manu 		case LINUX_TIOCLINUX_READMOUSE:
    263        1.1      manu 		case LINUX_TIOCLINUX_VESABLANK:
    264        1.1      manu 		case LINUX_TIOCLINUX_CURCONS:	/* could use VT_GETACTIVE */
    265  1.14.54.1    martin 		default:
    266        1.1      manu 			error = EINVAL;
    267        1.1      manu 			goto out;
    268        1.1      manu 		}
    269        1.1      manu 		break;
    270        1.1      manu 	case LINUX32_TIOCGWINSZ:
    271        1.1      manu 		SCARG(&ia, com) = TIOCGWINSZ;
    272        1.1      manu 		break;
    273        1.1      manu 	case LINUX32_TIOCSWINSZ:
    274        1.1      manu 		SCARG(&ia, com) = TIOCSWINSZ;
    275        1.1      manu 		break;
    276        1.1      manu 	case LINUX32_TIOCGPGRP:
    277        1.1      manu 		SCARG(&ia, com) = TIOCGPGRP;
    278        1.1      manu 		break;
    279        1.1      manu 	case LINUX32_TIOCSPGRP:
    280        1.1      manu 		SCARG(&ia, com) = TIOCSPGRP;
    281        1.1      manu 		break;
    282        1.1      manu 	case LINUX32_FIONREAD:
    283        1.1      manu 		SCARG(&ia, com) = FIONREAD;
    284        1.1      manu 		break;
    285        1.1      manu 	case LINUX32_FIONBIO:
    286        1.1      manu 		SCARG(&ia, com) = FIONBIO;
    287        1.1      manu 		break;
    288        1.1      manu 	case LINUX32_FIOASYNC:
    289        1.1      manu 		SCARG(&ia, com) = FIOASYNC;
    290        1.1      manu 		break;
    291        1.1      manu 	case LINUX32_TIOCEXCL:
    292        1.1      manu 		SCARG(&ia, com) = TIOCEXCL;
    293        1.1      manu 		break;
    294        1.1      manu 	case LINUX32_TIOCNXCL:
    295        1.1      manu 		SCARG(&ia, com) = TIOCNXCL;
    296        1.1      manu 		break;
    297        1.1      manu 	case LINUX32_TIOCCONS:
    298        1.1      manu 		SCARG(&ia, com) = TIOCCONS;
    299        1.1      manu 		break;
    300        1.1      manu 	case LINUX32_TIOCNOTTY:
    301        1.1      manu 		SCARG(&ia, com) = TIOCNOTTY;
    302        1.1      manu 		break;
    303        1.1      manu 	case LINUX32_TCSBRK:
    304       1.13  matthias 		idat = (u_long)SCARG_P32(uap, data);
    305       1.13  matthias 		if (idat != 0)
    306       1.13  matthias 			SCARG(&ia, com) = TIOCDRAIN;
    307       1.13  matthias 		else {
    308       1.13  matthias 			if ((error = (*bsdioctl)(fp, TIOCSBRK, NULL)) != 0)
    309       1.13  matthias 				goto out;
    310       1.13  matthias 			error = tsleep(&idat, PZERO | PCATCH, "linux_tcsbrk", hz / 4);
    311       1.13  matthias 			if (error == EINTR || error == ERESTART) {
    312       1.13  matthias 				(void)(*bsdioctl)(fp, TIOCCBRK, NULL);
    313       1.13  matthias 				error = EINTR;
    314       1.13  matthias 			} else
    315       1.13  matthias 				error = (*bsdioctl)(fp, TIOCCBRK, NULL);
    316       1.13  matthias 			goto out;
    317       1.13  matthias 		}
    318        1.1      manu 		break;
    319        1.1      manu 	case LINUX32_TIOCMGET:
    320        1.1      manu 		SCARG(&ia, com) = TIOCMGET;
    321        1.1      manu 		break;
    322        1.1      manu 	case LINUX32_TIOCMSET:
    323        1.1      manu 		SCARG(&ia, com) = TIOCMSET;
    324        1.1      manu 		break;
    325        1.1      manu 	case LINUX32_TIOCMBIC:
    326        1.1      manu 		SCARG(&ia, com) = TIOCMBIC;
    327        1.1      manu 		break;
    328        1.1      manu 	case LINUX32_TIOCMBIS:
    329        1.1      manu 		SCARG(&ia, com) = TIOCMBIS;
    330        1.1      manu 		break;
    331        1.1      manu #ifdef LINUX32_TIOCGPTN
    332        1.1      manu 	case LINUX32_TIOCGPTN:
    333        1.1      manu #ifndef NO_DEV_PTM
    334        1.1      manu 		{
    335        1.1      manu 			struct ptmget ptm;
    336        1.1      manu 
    337       1.10        ad 			error = (*bsdioctl)(fp, TIOCPTSNAME, &ptm);
    338        1.8       dsl 			if (error != 0)
    339        1.1      manu 				goto out;
    340        1.1      manu 
    341        1.5       dsl 			error = copyout(&ptm.sfd, SCARG_P32(uap, data),
    342        1.1      manu 			    sizeof(ptm.sfd));
    343        1.1      manu 			goto out;
    344        1.1      manu 		}
    345        1.1      manu #endif /* NO_DEV_PTM */
    346        1.1      manu #endif /* LINUX32_TIOCGPTN */
    347        1.1      manu 	default:
    348        1.1      manu 		error = EINVAL;
    349        1.1      manu 		goto out;
    350        1.1      manu 	}
    351        1.1      manu 
    352        1.1      manu 	SCARG(&ia, fd) = SCARG(uap, fd);
    353        1.2      manu 	SCARG(&ia, data) = SCARG(uap, data);
    354        1.1      manu 	error = netbsd32_ioctl(curlwp, &ia, retval);
    355        1.1      manu out:
    356       1.11       bjs 	fd_putfile(SCARG(uap, fd));
    357        1.1      manu 	return error;
    358        1.1      manu }
    359