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