Home | History | Annotate | Line # | Download | only in common
linux_termios.c revision 1.33.6.2
      1  1.33.6.1       mjf /*	$NetBSD: linux_termios.c,v 1.33.6.2 2008/06/02 13:23:04 mjf Exp $	*/
      2       1.4       erh 
      3       1.4       erh /*-
      4  1.33.6.1       mjf  * 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.33.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: linux_termios.c,v 1.33.6.2 2008/06/02 13:23:04 mjf 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.1   mycroft 
     48       1.1   mycroft #include <sys/syscallargs.h>
     49       1.1   mycroft 
     50       1.5  christos #include <compat/linux/common/linux_types.h>
     51       1.5  christos #include <compat/linux/common/linux_ioctl.h>
     52       1.5  christos #include <compat/linux/common/linux_signal.h>
     53       1.5  christos #include <compat/linux/common/linux_util.h>
     54       1.5  christos #include <compat/linux/common/linux_termios.h>
     55      1.32     njoly #include <compat/linux/common/linux_ipc.h>
     56      1.32     njoly #include <compat/linux/common/linux_sem.h>
     57       1.5  christos 
     58       1.1   mycroft #include <compat/linux/linux_syscallargs.h>
     59       1.1   mycroft 
     60      1.26  christos #ifdef DEBUG_LINUX
     61      1.26  christos #define DPRINTF(a)	uprintf a
     62      1.26  christos #else
     63      1.26  christos #define DPRINTF(a)
     64      1.26  christos #endif
     65      1.26  christos 
     66       1.1   mycroft int
     67      1.33       dsl linux_ioctl_termios(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
     68      1.33       dsl {
     69      1.33       dsl 	/* {
     70       1.1   mycroft 		syscallarg(int) fd;
     71       1.1   mycroft 		syscallarg(u_long) com;
     72      1.29  christos 		syscallarg(void *) data;
     73      1.33       dsl 	} */
     74  1.33.6.1       mjf 	file_t *fp;
     75       1.1   mycroft 	u_long com;
     76       1.1   mycroft 	struct linux_termio tmplt;
     77       1.1   mycroft 	struct linux_termios tmplts;
     78       1.1   mycroft 	struct termios tmpbts;
     79       1.1   mycroft 	int idat;
     80       1.1   mycroft 	struct sys_ioctl_args ia;
     81       1.1   mycroft 	int error;
     82       1.8      fvdl 	char tioclinux;
     83  1.33.6.1       mjf 	int (*bsdioctl)(file_t *, u_long, void *);
     84       1.1   mycroft 
     85  1.33.6.1       mjf 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
     86       1.1   mycroft 		return (EBADF);
     87       1.1   mycroft 
     88      1.17      yamt 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
     89      1.17      yamt 		error = EBADF;
     90      1.17      yamt 		goto out;
     91      1.17      yamt 	}
     92      1.17      yamt 
     93       1.9      ross 	bsdioctl = fp->f_ops->fo_ioctl;
     94       1.1   mycroft 	com = SCARG(uap, com);
     95       1.1   mycroft 	retval[0] = 0;
     96      1.22     perry 
     97      1.30      manu 	switch (com) {
     98       1.1   mycroft 	case LINUX_TCGETS:
     99  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    100       1.1   mycroft 		if (error)
    101      1.17      yamt 			goto out;
    102       1.1   mycroft 		bsd_termios_to_linux_termios(&tmpbts, &tmplts);
    103       1.1   mycroft 		error = copyout(&tmplts, SCARG(uap, data), sizeof tmplts);
    104      1.17      yamt 		goto out;
    105       1.1   mycroft 	case LINUX_TCSETS:
    106       1.1   mycroft 	case LINUX_TCSETSW:
    107       1.1   mycroft 	case LINUX_TCSETSF:
    108       1.1   mycroft 		/*
    109       1.1   mycroft 		 * First fill in all fields, so that we keep the current
    110       1.1   mycroft 		 * values for fields that Linux doesn't know about.
    111       1.1   mycroft 		 */
    112  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    113       1.1   mycroft 		if (error)
    114      1.17      yamt 			goto out;
    115       1.1   mycroft 		error = copyin(SCARG(uap, data), &tmplts, sizeof tmplts);
    116       1.1   mycroft 		if (error)
    117      1.17      yamt 			goto out;
    118       1.1   mycroft 		linux_termios_to_bsd_termios(&tmplts, &tmpbts);
    119       1.1   mycroft 		switch (com) {
    120       1.1   mycroft 		case LINUX_TCSETS:
    121       1.1   mycroft 			com = TIOCSETA;
    122       1.1   mycroft 			break;
    123       1.1   mycroft 		case LINUX_TCSETSW:
    124       1.1   mycroft 			com = TIOCSETAW;
    125       1.1   mycroft 			break;
    126       1.1   mycroft 		case LINUX_TCSETSF:
    127       1.1   mycroft 			com = TIOCSETAF;
    128       1.1   mycroft 			break;
    129       1.1   mycroft 		}
    130  1.33.6.1       mjf 		error = (*bsdioctl)(fp, com, &tmpbts);
    131      1.17      yamt 		goto out;
    132       1.1   mycroft 	case LINUX_TCGETA:
    133  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    134       1.1   mycroft 		if (error)
    135      1.17      yamt 			goto out;
    136       1.1   mycroft 		bsd_termios_to_linux_termio(&tmpbts, &tmplt);
    137       1.1   mycroft 		error = copyout(&tmplt, SCARG(uap, data), sizeof tmplt);
    138      1.17      yamt 		goto out;
    139       1.1   mycroft 	case LINUX_TCSETA:
    140       1.1   mycroft 	case LINUX_TCSETAW:
    141       1.1   mycroft 	case LINUX_TCSETAF:
    142       1.1   mycroft 		/*
    143       1.1   mycroft 		 * First fill in all fields, so that we keep the current
    144       1.1   mycroft 		 * values for fields that Linux doesn't know about.
    145       1.1   mycroft 		 */
    146  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCGETA, &tmpbts);
    147       1.1   mycroft 		if (error)
    148      1.17      yamt 			goto out;
    149       1.1   mycroft 		error = copyin(SCARG(uap, data), &tmplt, sizeof tmplt);
    150       1.1   mycroft 		if (error)
    151      1.17      yamt 			goto out;
    152       1.1   mycroft 		linux_termio_to_bsd_termios(&tmplt, &tmpbts);
    153       1.1   mycroft 		switch (com) {
    154       1.1   mycroft 		case LINUX_TCSETA:
    155       1.1   mycroft 			com = TIOCSETA;
    156       1.1   mycroft 			break;
    157       1.1   mycroft 		case LINUX_TCSETAW:
    158       1.1   mycroft 			com = TIOCSETAW;
    159       1.1   mycroft 			break;
    160       1.1   mycroft 		case LINUX_TCSETAF:
    161       1.1   mycroft 			com = TIOCSETAF;
    162       1.1   mycroft 			break;
    163       1.1   mycroft 		}
    164  1.33.6.1       mjf 		error = (*bsdioctl)(fp, com, &tmpbts);
    165      1.17      yamt 		goto out;
    166       1.9      ross 	case LINUX_TCFLSH:
    167      1.10    itojun 		switch((u_long)SCARG(uap, data)) {
    168       1.9      ross 		case 0:
    169       1.9      ross 			idat = FREAD;
    170       1.9      ross 			break;
    171       1.9      ross 		case 1:
    172       1.9      ross 			idat = FWRITE;
    173       1.9      ross 			break;
    174       1.9      ross 		case 2:
    175       1.9      ross 			idat = 0;
    176       1.9      ross 			break;
    177       1.9      ross 		default:
    178      1.17      yamt 			error = EINVAL;
    179      1.17      yamt 			goto out;
    180       1.9      ross 		}
    181  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCFLUSH, &idat);
    182      1.17      yamt 		goto out;
    183       1.1   mycroft 	case LINUX_TIOCGETD:
    184  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCGETD, &idat);
    185       1.1   mycroft 		if (error)
    186      1.17      yamt 			goto out;
    187       1.1   mycroft 		switch (idat) {
    188       1.1   mycroft 		case TTYDISC:
    189       1.1   mycroft 			idat = LINUX_N_TTY;
    190       1.1   mycroft 			break;
    191       1.1   mycroft 		case SLIPDISC:
    192       1.1   mycroft 			idat = LINUX_N_SLIP;
    193       1.1   mycroft 			break;
    194       1.1   mycroft 		case PPPDISC:
    195       1.1   mycroft 			idat = LINUX_N_PPP;
    196       1.1   mycroft 			break;
    197       1.4       erh 		case STRIPDISC:
    198       1.4       erh 			idat = LINUX_N_STRIP;
    199       1.4       erh 			break;
    200       1.1   mycroft 		/*
    201       1.1   mycroft 		 * Linux does not have the tablet line discipline.
    202       1.1   mycroft 		 */
    203       1.1   mycroft 		case TABLDISC:
    204       1.1   mycroft 		default:
    205       1.1   mycroft 			idat = -1;	/* XXX What should this be? */
    206       1.1   mycroft 			break;
    207       1.1   mycroft 		}
    208       1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    209      1.17      yamt 		goto out;
    210       1.1   mycroft 	case LINUX_TIOCSETD:
    211       1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    212       1.1   mycroft 		if (error)
    213      1.17      yamt 			goto out;
    214       1.1   mycroft 		switch (idat) {
    215       1.1   mycroft 		case LINUX_N_TTY:
    216       1.1   mycroft 			idat = TTYDISC;
    217       1.1   mycroft 			break;
    218       1.1   mycroft 		case LINUX_N_SLIP:
    219       1.1   mycroft 			idat = SLIPDISC;
    220       1.1   mycroft 			break;
    221       1.1   mycroft 		case LINUX_N_PPP:
    222       1.1   mycroft 			idat = PPPDISC;
    223       1.1   mycroft 			break;
    224       1.4       erh 		case LINUX_N_STRIP:
    225       1.4       erh 			idat = STRIPDISC;
    226       1.4       erh 			break;
    227       1.1   mycroft 		/*
    228       1.1   mycroft 		 * We can't handle the mouse line discipline Linux has.
    229       1.1   mycroft 		 */
    230       1.1   mycroft 		case LINUX_N_MOUSE:
    231       1.4       erh 		case LINUX_N_AX25:
    232       1.4       erh 		case LINUX_N_X25:
    233       1.4       erh 		case LINUX_N_6PACK:
    234       1.1   mycroft 		default:
    235      1.17      yamt 			error = EINVAL;
    236      1.17      yamt 			goto out;
    237       1.1   mycroft 		}
    238  1.33.6.1       mjf 		error = (*bsdioctl)(fp, TIOCSETD, &idat);
    239      1.17      yamt 		goto out;
    240       1.8      fvdl 	case LINUX_TIOCLINUX:
    241       1.8      fvdl 		error = copyin(SCARG(uap, data), &tioclinux, sizeof tioclinux);
    242       1.8      fvdl 		if (error != 0)
    243      1.17      yamt 			goto out;
    244       1.8      fvdl 		switch (tioclinux) {
    245       1.8      fvdl 		case LINUX_TIOCLINUX_KERNMSG:
    246       1.8      fvdl 			/*
    247       1.8      fvdl 			 * XXX needed to not fail for some things. Could
    248       1.8      fvdl 			 * try to use TIOCCONS, but the char argument
    249       1.8      fvdl 			 * specifies the VT #, not an fd.
    250       1.8      fvdl 			 */
    251      1.17      yamt 			error = 0;
    252      1.17      yamt 			goto out;
    253       1.8      fvdl 		case LINUX_TIOCLINUX_COPY:
    254       1.8      fvdl 		case LINUX_TIOCLINUX_PASTE:
    255       1.8      fvdl 		case LINUX_TIOCLINUX_UNBLANK:
    256       1.8      fvdl 		case LINUX_TIOCLINUX_LOADLUT:
    257       1.8      fvdl 		case LINUX_TIOCLINUX_READSHIFT:
    258       1.8      fvdl 		case LINUX_TIOCLINUX_READMOUSE:
    259       1.8      fvdl 		case LINUX_TIOCLINUX_VESABLANK:
    260       1.8      fvdl 		case LINUX_TIOCLINUX_CURCONS:	/* could use VT_GETACTIVE */
    261      1.17      yamt 			error = EINVAL;
    262      1.17      yamt 			goto out;
    263       1.8      fvdl 		}
    264       1.8      fvdl 		break;
    265       1.1   mycroft 	case LINUX_TIOCGWINSZ:
    266       1.1   mycroft 		SCARG(&ia, com) = TIOCGWINSZ;
    267       1.1   mycroft 		break;
    268       1.1   mycroft 	case LINUX_TIOCSWINSZ:
    269       1.1   mycroft 		SCARG(&ia, com) = TIOCSWINSZ;
    270       1.1   mycroft 		break;
    271       1.1   mycroft 	case LINUX_TIOCGPGRP:
    272       1.1   mycroft 		SCARG(&ia, com) = TIOCGPGRP;
    273       1.1   mycroft 		break;
    274       1.1   mycroft 	case LINUX_TIOCSPGRP:
    275       1.1   mycroft 		SCARG(&ia, com) = TIOCSPGRP;
    276       1.1   mycroft 		break;
    277       1.1   mycroft 	case LINUX_FIONREAD:
    278       1.1   mycroft 		SCARG(&ia, com) = FIONREAD;
    279       1.1   mycroft 		break;
    280       1.1   mycroft 	case LINUX_FIONBIO:
    281       1.1   mycroft 		SCARG(&ia, com) = FIONBIO;
    282       1.1   mycroft 		break;
    283       1.1   mycroft 	case LINUX_FIOASYNC:
    284       1.1   mycroft 		SCARG(&ia, com) = FIOASYNC;
    285       1.1   mycroft 		break;
    286       1.1   mycroft 	case LINUX_TIOCEXCL:
    287       1.1   mycroft 		SCARG(&ia, com) = TIOCEXCL;
    288       1.1   mycroft 		break;
    289       1.1   mycroft 	case LINUX_TIOCNXCL:
    290       1.1   mycroft 		SCARG(&ia, com) = TIOCNXCL;
    291       1.1   mycroft 		break;
    292       1.1   mycroft 	case LINUX_TIOCCONS:
    293       1.1   mycroft 		SCARG(&ia, com) = TIOCCONS;
    294       1.1   mycroft 		break;
    295       1.1   mycroft 	case LINUX_TIOCNOTTY:
    296       1.1   mycroft 		SCARG(&ia, com) = TIOCNOTTY;
    297      1.15  augustss 		break;
    298      1.15  augustss 	case LINUX_TCSBRK:
    299      1.15  augustss 		SCARG(&ia, com) = SCARG(uap, data) ? TIOCDRAIN : TIOCSBRK;
    300      1.15  augustss 		break;
    301      1.15  augustss 	case LINUX_TIOCMGET:
    302      1.15  augustss 		SCARG(&ia, com) = TIOCMGET;
    303      1.15  augustss 		break;
    304      1.15  augustss 	case LINUX_TIOCMSET:
    305      1.15  augustss 		SCARG(&ia, com) = TIOCMSET;
    306       1.1   mycroft 		break;
    307      1.23  christos 	case LINUX_TIOCMBIC:
    308      1.23  christos 		SCARG(&ia, com) = TIOCMBIC;
    309      1.23  christos 		break;
    310      1.23  christos 	case LINUX_TIOCMBIS:
    311      1.23  christos 		SCARG(&ia, com) = TIOCMBIS;
    312      1.23  christos 		break;
    313      1.21  christos #ifdef LINUX_TIOCGPTN
    314      1.21  christos 	case LINUX_TIOCGPTN:
    315      1.21  christos #ifndef NO_DEV_PTM
    316      1.21  christos 		{
    317      1.31       dsl 			struct ptmget ptm;
    318      1.21  christos 
    319  1.33.6.1       mjf 			error = (*bsdioctl)(fp, TIOCPTSNAME, &ptm);
    320      1.31       dsl 			if (error != 0)
    321      1.21  christos 				goto out;
    322      1.21  christos 			error = copyout(&ptm.sfd, SCARG(uap, data),
    323      1.21  christos 			    sizeof(ptm.sfd));
    324      1.21  christos 			goto out;
    325      1.21  christos 		}
    326      1.21  christos #endif /* NO_DEV_PTM */
    327      1.21  christos #endif /* LINUX_TIOCGPTN */
    328      1.26  christos #ifdef LINUX_TIOCSPTLCK
    329      1.26  christos 	case LINUX_TIOCSPTLCK:
    330  1.33.6.1       mjf 			fd_putfile(SCARG(uap, fd));
    331      1.26  christos 			error = copyin(SCARG(uap, data), &idat, sizeof(idat));
    332      1.26  christos 			if (error)
    333      1.26  christos 				return error;
    334      1.26  christos 			DPRINTF(("TIOCSPTLCK %d\n", idat));
    335      1.26  christos 			return 0;
    336      1.26  christos #endif
    337       1.1   mycroft 	default:
    338      1.17      yamt 		error = EINVAL;
    339      1.17      yamt 		goto out;
    340       1.1   mycroft 	}
    341       1.1   mycroft 
    342       1.1   mycroft 	SCARG(&ia, fd) = SCARG(uap, fd);
    343       1.1   mycroft 	SCARG(&ia, data) = SCARG(uap, data);
    344      1.17      yamt 	error = sys_ioctl(curlwp, &ia, retval);
    345      1.17      yamt out:
    346  1.33.6.1       mjf 	fd_putfile(SCARG(uap, fd));
    347      1.17      yamt 	return error;
    348       1.1   mycroft }
    349