Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: tty_43.c,v 1.40 2022/07/10 13:57:14 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*-
     30  * Copyright (c) 1982, 1986, 1991, 1993
     31  *	The Regents of the University of California.  All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  * 3. Neither the name of the University nor the names of its contributors
     42  *    may be used to endorse or promote products derived from this software
     43  *    without specific prior written permission.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  *
     57  *	@(#)tty_compat.c	8.2 (Berkeley) 1/9/95
     58  */
     59 
     60 /*
     61  * mapping routines for old line discipline (yuck)
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: tty_43.c,v 1.40 2022/07/10 13:57:14 riastradh Exp $");
     66 
     67 #if defined(_KERNEL_OPT)
     68 #include "opt_compat_netbsd.h"
     69 #endif
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/ioctl.h>
     74 #include <sys/proc.h>
     75 #include <sys/conf.h>
     76 #include <sys/tty.h>
     77 #include <sys/termios.h>
     78 #include <sys/file.h>
     79 #include <sys/kernel.h>
     80 #include <sys/syslog.h>
     81 #include <sys/compat_stub.h>
     82 #include <sys/module_hook.h>
     83 #include <sys/ioctl_compat.h>
     84 
     85 #include <compat/common/compat_mod.h>
     86 #include <compat/sys/ttycom.h>
     87 
     88 int ttydebug = 0;
     89 
     90 static const struct speedtab compatspeeds[] = {
     91 #define MAX_SPEED	17
     92 	{ 115200, 17 },
     93 	{ 57600, 16 },
     94 	{ 38400, 15 },
     95 	{ 19200, 14 },
     96 	{ 9600,	13 },
     97 	{ 4800,	12 },
     98 	{ 2400,	11 },
     99 	{ 1800,	10 },
    100 	{ 1200,	9 },
    101 	{ 600,	8 },
    102 	{ 300,	7 },
    103 	{ 200,	6 },
    104 	{ 150,	5 },
    105 	{ 134,	4 },
    106 	{ 110,	3 },
    107 	{ 75,	2 },
    108 	{ 50,	1 },
    109 	{ 0,	0 },
    110 	{ -1,	-1 },
    111 };
    112 static const int compatspcodes[] = {
    113 	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
    114 	1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200
    115 };
    116 
    117 static int ttcompatgetflags(struct tty *);
    118 static void ttcompatsetflags(struct tty *, struct termios *);
    119 static void ttcompatsetlflags(struct tty *, struct termios *);
    120 
    121 /*ARGSUSED*/
    122 int
    123 compat_43_ttioctl(struct tty *tp, u_long com, void *data, int flag,
    124     struct lwp *l)
    125 {
    126 
    127 	switch (com) {
    128 	case TIOCGETP: {
    129 		struct sgttyb *sg = (struct sgttyb *)data;
    130 		int speed;
    131 
    132 		mutex_spin_enter(&tty_lock);
    133 		speed = ttspeedtab(tp->t_ospeed, compatspeeds);
    134 		sg->sg_ospeed = (speed == -1) ? MAX_SPEED : speed;
    135 		if (tp->t_ispeed == 0)
    136 			sg->sg_ispeed = sg->sg_ospeed;
    137 		else {
    138 			speed = ttspeedtab(tp->t_ispeed, compatspeeds);
    139 			sg->sg_ispeed = (speed == -1) ? MAX_SPEED : speed;
    140 		}
    141 		sg->sg_erase = tty_getctrlchar(tp, VERASE);
    142 		sg->sg_kill = tty_getctrlchar(tp, VKILL);
    143 		sg->sg_flags = ttcompatgetflags(tp);
    144 		mutex_spin_exit(&tty_lock);
    145 		break;
    146 	}
    147 
    148 	case TIOCSETP:
    149 	case TIOCSETN: {
    150 		struct sgttyb *sg = (struct sgttyb *)data;
    151 		struct termios term;
    152 		int speed;
    153 
    154 		mutex_spin_enter(&tty_lock);
    155 		term = tp->t_termios;
    156 		if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
    157 			term.c_ispeed = speed;
    158 		else
    159 			term.c_ispeed = compatspcodes[speed];
    160 		if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
    161 			term.c_ospeed = speed;
    162 		else
    163 			term.c_ospeed = compatspcodes[speed];
    164 		term.c_cc[VERASE] = sg->sg_erase;
    165 		term.c_cc[VKILL] = sg->sg_kill;
    166 		tp->t_flags = (ttcompatgetflags(tp)&0xffff0000) | (sg->sg_flags&0xffff);
    167 		ttcompatsetflags(tp, &term);
    168 		mutex_spin_exit(&tty_lock);
    169 		return (ttioctl(tp, com == TIOCSETP ? TIOCSETAF : TIOCSETA,
    170 			(void *)&term, flag, l));
    171 	}
    172 
    173 	case TIOCGETC: {
    174 		struct tchars *tc = (struct tchars *)data;
    175 
    176 		tc->t_intrc = tty_getctrlchar(tp, VINTR);
    177 		tc->t_quitc = tty_getctrlchar(tp, VQUIT);
    178 		tc->t_startc = tty_getctrlchar(tp, VSTART);
    179 		tc->t_stopc = tty_getctrlchar(tp, VSTOP);
    180 		tc->t_eofc = tty_getctrlchar(tp, VEOF);
    181 		tc->t_brkc = tty_getctrlchar(tp, VEOL);
    182 		break;
    183 	}
    184 	case TIOCSETC: {
    185 		struct tchars *tc = (struct tchars *)data;
    186 
    187 		tty_setctrlchar(tp, VINTR, tc->t_intrc);
    188 		tty_setctrlchar(tp, VQUIT, tc->t_quitc);
    189 		tty_setctrlchar(tp, VSTART, tc->t_startc);
    190 		tty_setctrlchar(tp, VSTOP, tc->t_stopc);
    191 		tty_setctrlchar(tp, VEOF, tc->t_eofc);
    192 		tty_setctrlchar(tp, VEOL, tc->t_brkc);
    193 		if (tc->t_brkc == (char)-1)
    194 			tty_setctrlchar(tp, VEOL2, _POSIX_VDISABLE);
    195 		break;
    196 	}
    197 	case TIOCSLTC: {
    198 		struct ltchars *ltc = (struct ltchars *)data;
    199 
    200 		tty_setctrlchar(tp, VSUSP, ltc->t_suspc);
    201 		tty_setctrlchar(tp, VDSUSP, ltc->t_dsuspc);
    202 		tty_setctrlchar(tp, VREPRINT, ltc->t_rprntc);
    203 		tty_setctrlchar(tp, VDISCARD, ltc->t_flushc);
    204 		tty_setctrlchar(tp, VWERASE, ltc->t_werasc);
    205 		tty_setctrlchar(tp, VLNEXT, ltc->t_lnextc);
    206 		break;
    207 	}
    208 	case TIOCGLTC: {
    209 		struct ltchars *ltc = (struct ltchars *)data;
    210 
    211 		ltc->t_suspc = tty_getctrlchar(tp, VSUSP);
    212 		ltc->t_dsuspc = tty_getctrlchar(tp, VDSUSP);
    213 		ltc->t_rprntc = tty_getctrlchar(tp, VREPRINT);
    214 		ltc->t_flushc = tty_getctrlchar(tp, VDISCARD);
    215 		ltc->t_werasc = tty_getctrlchar(tp, VWERASE);
    216 		ltc->t_lnextc = tty_getctrlchar(tp, VLNEXT);
    217 		break;
    218 	}
    219 	case TIOCLBIS:
    220 	case TIOCLBIC:
    221 	case TIOCLSET: {
    222 		struct termios term;
    223 		unsigned argbits, flags;
    224 
    225 		argbits = *(int *)data;
    226 
    227 		mutex_spin_enter(&tty_lock);
    228 		term = tp->t_termios;
    229 		flags = ttcompatgetflags(tp);
    230 		switch (com) {
    231 		case TIOCLSET:
    232 			tp->t_flags = (flags & 0xffff) | (argbits << 16);
    233 			break;
    234 		case TIOCLBIS:
    235 			tp->t_flags = flags | (argbits << 16);
    236 			break;
    237 		case TIOCLBIC:
    238 			tp->t_flags = flags & ~(argbits << 16);
    239 			break;
    240 		}
    241 		ttcompatsetlflags(tp, &term);
    242 		mutex_spin_exit(&tty_lock);
    243 		return (ttioctl(tp, TIOCSETA, (void *)&term, flag, l));
    244 	}
    245 	case TIOCLGET:
    246 		mutex_spin_enter(&tty_lock);
    247 		*(int *)data = ttcompatgetflags(tp)>>16;
    248 		mutex_spin_exit(&tty_lock);
    249 		if (ttydebug)
    250 			printf("CLGET: returning %x\n", *(int *)data);
    251 		break;
    252 
    253 	case OTIOCGETD:
    254 		mutex_spin_enter(&tty_lock);
    255 		*(int *)data = (tp->t_linesw == NULL || tp->t_linesw->l_no == 0)
    256 		    ? 2 /* XXX old NTTYDISC */ : tp->t_linesw->l_no;
    257 		mutex_spin_exit(&tty_lock);
    258 		break;
    259 
    260 	case OTIOCSETD: {
    261 		int ldisczero = 0;
    262 
    263 		return (ttioctl(tp, TIOCSETD,
    264 			*(int *)data == 2 ? (void *)&ldisczero : data, flag,
    265 			l));
    266 	    }
    267 
    268 	case OTIOCCONS:
    269 		*(int *)data = 1;
    270 		return (ttioctl(tp, TIOCCONS, data, flag, l));
    271 
    272 	case TIOCHPCL:
    273 		mutex_spin_enter(&tty_lock);
    274 		SET(tp->t_cflag, HUPCL);
    275 		mutex_spin_exit(&tty_lock);
    276 		break;
    277 
    278 	default:
    279 		return (EPASSTHROUGH);
    280 	}
    281 	return (0);
    282 }
    283 
    284 static int
    285 ttcompatgetflags(struct tty *tp)
    286 {
    287 	tcflag_t iflag = tp->t_iflag;
    288 	tcflag_t lflag = tp->t_lflag;
    289 	tcflag_t oflag = tp->t_oflag;
    290 	tcflag_t cflag = tp->t_cflag;
    291 	int flags = 0;
    292 
    293 	KASSERT(mutex_owned(&tty_lock));
    294 
    295 	if (ISSET(iflag, IXOFF))
    296 		SET(flags, TANDEM);
    297 	if (ISSET(iflag, ICRNL) || ISSET(oflag, ONLCR))
    298 		SET(flags, CRMOD);
    299 	if (ISSET(cflag, PARENB)) {
    300 		if (ISSET(iflag, INPCK)) {
    301 			if (ISSET(cflag, PARODD))
    302 				SET(flags, ODDP);
    303 			else
    304 				SET(flags, EVENP);
    305 		} else
    306 			SET(flags, ANYP);
    307 	}
    308 
    309 	if (!ISSET(lflag, ICANON)) {
    310 		/* fudge */
    311 		if (ISSET(iflag, IXON) || ISSET(lflag, ISIG|IEXTEN) ||
    312 		    ISSET(cflag, PARENB))
    313 			SET(flags, CBREAK);
    314 		else
    315 			SET(flags, RAW);
    316 	}
    317 
    318 	if (ISSET(flags, RAW))
    319 		SET(flags, ISSET(tp->t_flags, LITOUT|PASS8));
    320 	else if (ISSET(cflag, CSIZE) == CS8) {
    321 		if (!ISSET(oflag, OPOST))
    322 			SET(flags, LITOUT);
    323 		if (!ISSET(iflag, ISTRIP))
    324 			SET(flags, PASS8);
    325 	}
    326 
    327 	if (ISSET(cflag, MDMBUF))
    328 		SET(flags, MDMBUF);
    329 	if (!ISSET(cflag, HUPCL))
    330 		SET(flags, NOHANG);
    331 	if (ISSET(oflag, OXTABS))
    332 		SET(flags, XTABS);
    333 	if (ISSET(lflag, ECHOE))
    334 		SET(flags, CRTERA|CRTBS);
    335 	if (ISSET(lflag, ECHOKE))
    336 		SET(flags, CRTKIL|CRTBS);
    337 	if (ISSET(lflag, ECHOPRT))
    338 		SET(flags, PRTERA);
    339 	if (ISSET(lflag, ECHOCTL))
    340 		SET(flags, CTLECH);
    341 	if (!ISSET(iflag, IXANY))
    342 		SET(flags, DECCTQ);
    343 	SET(flags, ISSET(lflag, ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH));
    344 	if (ttydebug)
    345 		printf("getflags: %x\n", flags);
    346 	return (flags);
    347 }
    348 
    349 static void
    350 ttcompatsetflags(struct tty *tp, struct termios *t)
    351 {
    352 	int flags = tp->t_flags;
    353 
    354 	KASSERT(mutex_owned(&tty_lock));
    355 
    356 	tcflag_t iflag = t->c_iflag;
    357 	tcflag_t oflag = t->c_oflag;
    358 	tcflag_t lflag = t->c_lflag;
    359 	tcflag_t cflag = t->c_cflag;
    360 
    361 	if (ISSET(flags, TANDEM))
    362 		SET(iflag, IXOFF);
    363 	else
    364 		CLR(iflag, IXOFF);
    365 	if (ISSET(flags, ECHO))
    366 		SET(lflag, ECHO);
    367 	else
    368 		CLR(lflag, ECHO);
    369 	if (ISSET(flags, CRMOD)) {
    370 		SET(iflag, ICRNL);
    371 		SET(oflag, ONLCR);
    372 	} else {
    373 		CLR(iflag, ICRNL);
    374 		CLR(oflag, ONLCR);
    375 	}
    376 	if (ISSET(flags, XTABS))
    377 		SET(oflag, OXTABS);
    378 	else
    379 		CLR(oflag, OXTABS);
    380 
    381 
    382 	if (ISSET(flags, RAW)) {
    383 		iflag &= IXOFF;
    384 		CLR(lflag, ISIG|ICANON|IEXTEN);
    385 		CLR(cflag, PARENB);
    386 	} else {
    387 		SET(iflag, BRKINT|IXON|IMAXBEL);
    388 		SET(lflag, ISIG|IEXTEN);
    389 		if (ISSET(flags, CBREAK))
    390 			CLR(lflag, ICANON);
    391 		else
    392 			SET(lflag, ICANON);
    393 		switch (ISSET(flags, ANYP)) {
    394 		case 0:
    395 			CLR(cflag, PARENB);
    396 			break;
    397 		case ANYP:
    398 			SET(cflag, PARENB);
    399 			CLR(iflag, INPCK);
    400 			break;
    401 		case EVENP:
    402 			SET(cflag, PARENB);
    403 			SET(iflag, INPCK);
    404 			CLR(cflag, PARODD);
    405 			break;
    406 		case ODDP:
    407 			SET(cflag, PARENB);
    408 			SET(iflag, INPCK);
    409 			SET(cflag, PARODD);
    410 			break;
    411 		}
    412 	}
    413 
    414 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    415 		CLR(cflag, CSIZE);
    416 		SET(cflag, CS8);
    417 		if (!ISSET(flags, RAW|PASS8))
    418 			SET(iflag, ISTRIP);
    419 		else
    420 			CLR(iflag, ISTRIP);
    421 		if (!ISSET(flags, RAW|LITOUT))
    422 			SET(oflag, OPOST);
    423 		else
    424 			CLR(oflag, OPOST);
    425 	} else {
    426 		CLR(cflag, CSIZE);
    427 		SET(cflag, CS7);
    428 		SET(iflag, ISTRIP);
    429 		SET(oflag, OPOST);
    430 	}
    431 
    432 	t->c_iflag = iflag;
    433 	t->c_oflag = oflag;
    434 	t->c_lflag = lflag;
    435 	t->c_cflag = cflag;
    436 }
    437 
    438 static void
    439 ttcompatsetlflags(struct tty *tp, struct termios *t)
    440 {
    441 	int flags = tp->t_flags;
    442 	tcflag_t iflag = t->c_iflag;
    443 	tcflag_t oflag = t->c_oflag;
    444 	tcflag_t lflag = t->c_lflag;
    445 	tcflag_t cflag = t->c_cflag;
    446 
    447 	KASSERT(mutex_owned(&tty_lock));
    448 
    449 	/* Nothing we can do with CRTBS. */
    450 	if (ISSET(flags, PRTERA))
    451 		SET(lflag, ECHOPRT);
    452 	else
    453 		CLR(lflag, ECHOPRT);
    454 	if (ISSET(flags, CRTERA))
    455 		SET(lflag, ECHOE);
    456 	else
    457 		CLR(lflag, ECHOE);
    458 	/* Nothing we can do with TILDE. */
    459 	if (ISSET(flags, MDMBUF))
    460 		SET(cflag, MDMBUF);
    461 	else
    462 		CLR(cflag, MDMBUF);
    463 	if (ISSET(flags, NOHANG))
    464 		CLR(cflag, HUPCL);
    465 	else
    466 		SET(cflag, HUPCL);
    467 	if (ISSET(flags, CRTKIL))
    468 		SET(lflag, ECHOKE);
    469 	else
    470 		CLR(lflag, ECHOKE);
    471 	if (ISSET(flags, CTLECH))
    472 		SET(lflag, ECHOCTL);
    473 	else
    474 		CLR(lflag, ECHOCTL);
    475 	if (!ISSET(flags, DECCTQ))
    476 		SET(iflag, IXANY);
    477 	else
    478 		CLR(iflag, IXANY);
    479 	CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
    480 	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
    481 
    482 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    483 		CLR(cflag, CSIZE);
    484 		SET(cflag, CS8);
    485 		if (!ISSET(flags, RAW|PASS8))
    486 			SET(iflag, ISTRIP);
    487 		else
    488 			CLR(iflag, ISTRIP);
    489 		if (!ISSET(flags, RAW|LITOUT))
    490 			SET(oflag, OPOST);
    491 		else
    492 			CLR(oflag, OPOST);
    493 	} else {
    494 		CLR(cflag, CSIZE);
    495 		SET(cflag, CS7);
    496 		SET(iflag, ISTRIP);
    497 		SET(oflag, OPOST);
    498 	}
    499 
    500 	t->c_iflag = iflag;
    501 	t->c_oflag = oflag;
    502 	t->c_lflag = lflag;
    503 	t->c_cflag = cflag;
    504 }
    505 
    506 int
    507 kern_tty_43_init(void)
    508 {
    509 	MODULE_HOOK_SET(tty_ttioctl_43_hook, compat_43_ttioctl);
    510 	return 0;
    511 }
    512 
    513 int
    514 kern_tty_43_fini(void)
    515 {
    516 	MODULE_HOOK_UNSET(tty_ttioctl_43_hook);
    517 	return 0;
    518 }
    519