1 /* $NetBSD: tty_43.c,v 1.39 2020/10/10 15:59:41 christos 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.39 2020/10/10 15:59:41 christos 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 int argbits, flags; 224 225 argbits = *(int *)data; 226 if (argbits < 0) 227 return EINVAL; 228 229 mutex_spin_enter(&tty_lock); 230 term = tp->t_termios; 231 flags = ttcompatgetflags(tp); 232 switch (com) { 233 case TIOCLSET: 234 tp->t_flags = (flags & 0xffff) | (argbits << 16); 235 break; 236 case TIOCLBIS: 237 tp->t_flags = flags | (argbits << 16); 238 break; 239 case TIOCLBIC: 240 tp->t_flags = flags & ~(argbits << 16); 241 break; 242 } 243 ttcompatsetlflags(tp, &term); 244 mutex_spin_exit(&tty_lock); 245 return (ttioctl(tp, TIOCSETA, (void *)&term, flag, l)); 246 } 247 case TIOCLGET: 248 mutex_spin_enter(&tty_lock); 249 *(int *)data = ttcompatgetflags(tp)>>16; 250 mutex_spin_exit(&tty_lock); 251 if (ttydebug) 252 printf("CLGET: returning %x\n", *(int *)data); 253 break; 254 255 case OTIOCGETD: 256 mutex_spin_enter(&tty_lock); 257 *(int *)data = (tp->t_linesw == NULL || tp->t_linesw->l_no == 0) 258 ? 2 /* XXX old NTTYDISC */ : tp->t_linesw->l_no; 259 mutex_spin_exit(&tty_lock); 260 break; 261 262 case OTIOCSETD: { 263 int ldisczero = 0; 264 265 return (ttioctl(tp, TIOCSETD, 266 *(int *)data == 2 ? (void *)&ldisczero : data, flag, 267 l)); 268 } 269 270 case OTIOCCONS: 271 *(int *)data = 1; 272 return (ttioctl(tp, TIOCCONS, data, flag, l)); 273 274 case TIOCHPCL: 275 mutex_spin_enter(&tty_lock); 276 SET(tp->t_cflag, HUPCL); 277 mutex_spin_exit(&tty_lock); 278 break; 279 280 default: 281 return (EPASSTHROUGH); 282 } 283 return (0); 284 } 285 286 static int 287 ttcompatgetflags(struct tty *tp) 288 { 289 tcflag_t iflag = tp->t_iflag; 290 tcflag_t lflag = tp->t_lflag; 291 tcflag_t oflag = tp->t_oflag; 292 tcflag_t cflag = tp->t_cflag; 293 int flags = 0; 294 295 KASSERT(mutex_owned(&tty_lock)); 296 297 if (ISSET(iflag, IXOFF)) 298 SET(flags, TANDEM); 299 if (ISSET(iflag, ICRNL) || ISSET(oflag, ONLCR)) 300 SET(flags, CRMOD); 301 if (ISSET(cflag, PARENB)) { 302 if (ISSET(iflag, INPCK)) { 303 if (ISSET(cflag, PARODD)) 304 SET(flags, ODDP); 305 else 306 SET(flags, EVENP); 307 } else 308 SET(flags, ANYP); 309 } 310 311 if (!ISSET(lflag, ICANON)) { 312 /* fudge */ 313 if (ISSET(iflag, IXON) || ISSET(lflag, ISIG|IEXTEN) || 314 ISSET(cflag, PARENB)) 315 SET(flags, CBREAK); 316 else 317 SET(flags, RAW); 318 } 319 320 if (ISSET(flags, RAW)) 321 SET(flags, ISSET(tp->t_flags, LITOUT|PASS8)); 322 else if (ISSET(cflag, CSIZE) == CS8) { 323 if (!ISSET(oflag, OPOST)) 324 SET(flags, LITOUT); 325 if (!ISSET(iflag, ISTRIP)) 326 SET(flags, PASS8); 327 } 328 329 if (ISSET(cflag, MDMBUF)) 330 SET(flags, MDMBUF); 331 if (!ISSET(cflag, HUPCL)) 332 SET(flags, NOHANG); 333 if (ISSET(oflag, OXTABS)) 334 SET(flags, XTABS); 335 if (ISSET(lflag, ECHOE)) 336 SET(flags, CRTERA|CRTBS); 337 if (ISSET(lflag, ECHOKE)) 338 SET(flags, CRTKIL|CRTBS); 339 if (ISSET(lflag, ECHOPRT)) 340 SET(flags, PRTERA); 341 if (ISSET(lflag, ECHOCTL)) 342 SET(flags, CTLECH); 343 if (!ISSET(iflag, IXANY)) 344 SET(flags, DECCTQ); 345 SET(flags, ISSET(lflag, ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH)); 346 if (ttydebug) 347 printf("getflags: %x\n", flags); 348 return (flags); 349 } 350 351 static void 352 ttcompatsetflags(struct tty *tp, struct termios *t) 353 { 354 int flags = tp->t_flags; 355 356 KASSERT(mutex_owned(&tty_lock)); 357 358 tcflag_t iflag = t->c_iflag; 359 tcflag_t oflag = t->c_oflag; 360 tcflag_t lflag = t->c_lflag; 361 tcflag_t cflag = t->c_cflag; 362 363 if (ISSET(flags, TANDEM)) 364 SET(iflag, IXOFF); 365 else 366 CLR(iflag, IXOFF); 367 if (ISSET(flags, ECHO)) 368 SET(lflag, ECHO); 369 else 370 CLR(lflag, ECHO); 371 if (ISSET(flags, CRMOD)) { 372 SET(iflag, ICRNL); 373 SET(oflag, ONLCR); 374 } else { 375 CLR(iflag, ICRNL); 376 CLR(oflag, ONLCR); 377 } 378 if (ISSET(flags, XTABS)) 379 SET(oflag, OXTABS); 380 else 381 CLR(oflag, OXTABS); 382 383 384 if (ISSET(flags, RAW)) { 385 iflag &= IXOFF; 386 CLR(lflag, ISIG|ICANON|IEXTEN); 387 CLR(cflag, PARENB); 388 } else { 389 SET(iflag, BRKINT|IXON|IMAXBEL); 390 SET(lflag, ISIG|IEXTEN); 391 if (ISSET(flags, CBREAK)) 392 CLR(lflag, ICANON); 393 else 394 SET(lflag, ICANON); 395 switch (ISSET(flags, ANYP)) { 396 case 0: 397 CLR(cflag, PARENB); 398 break; 399 case ANYP: 400 SET(cflag, PARENB); 401 CLR(iflag, INPCK); 402 break; 403 case EVENP: 404 SET(cflag, PARENB); 405 SET(iflag, INPCK); 406 CLR(cflag, PARODD); 407 break; 408 case ODDP: 409 SET(cflag, PARENB); 410 SET(iflag, INPCK); 411 SET(cflag, PARODD); 412 break; 413 } 414 } 415 416 if (ISSET(flags, RAW|LITOUT|PASS8)) { 417 CLR(cflag, CSIZE); 418 SET(cflag, CS8); 419 if (!ISSET(flags, RAW|PASS8)) 420 SET(iflag, ISTRIP); 421 else 422 CLR(iflag, ISTRIP); 423 if (!ISSET(flags, RAW|LITOUT)) 424 SET(oflag, OPOST); 425 else 426 CLR(oflag, OPOST); 427 } else { 428 CLR(cflag, CSIZE); 429 SET(cflag, CS7); 430 SET(iflag, ISTRIP); 431 SET(oflag, OPOST); 432 } 433 434 t->c_iflag = iflag; 435 t->c_oflag = oflag; 436 t->c_lflag = lflag; 437 t->c_cflag = cflag; 438 } 439 440 static void 441 ttcompatsetlflags(struct tty *tp, struct termios *t) 442 { 443 int flags = tp->t_flags; 444 tcflag_t iflag = t->c_iflag; 445 tcflag_t oflag = t->c_oflag; 446 tcflag_t lflag = t->c_lflag; 447 tcflag_t cflag = t->c_cflag; 448 449 KASSERT(mutex_owned(&tty_lock)); 450 451 /* Nothing we can do with CRTBS. */ 452 if (ISSET(flags, PRTERA)) 453 SET(lflag, ECHOPRT); 454 else 455 CLR(lflag, ECHOPRT); 456 if (ISSET(flags, CRTERA)) 457 SET(lflag, ECHOE); 458 else 459 CLR(lflag, ECHOE); 460 /* Nothing we can do with TILDE. */ 461 if (ISSET(flags, MDMBUF)) 462 SET(cflag, MDMBUF); 463 else 464 CLR(cflag, MDMBUF); 465 if (ISSET(flags, NOHANG)) 466 CLR(cflag, HUPCL); 467 else 468 SET(cflag, HUPCL); 469 if (ISSET(flags, CRTKIL)) 470 SET(lflag, ECHOKE); 471 else 472 CLR(lflag, ECHOKE); 473 if (ISSET(flags, CTLECH)) 474 SET(lflag, ECHOCTL); 475 else 476 CLR(lflag, ECHOCTL); 477 if (!ISSET(flags, DECCTQ)) 478 SET(iflag, IXANY); 479 else 480 CLR(iflag, IXANY); 481 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH); 482 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH)); 483 484 if (ISSET(flags, RAW|LITOUT|PASS8)) { 485 CLR(cflag, CSIZE); 486 SET(cflag, CS8); 487 if (!ISSET(flags, RAW|PASS8)) 488 SET(iflag, ISTRIP); 489 else 490 CLR(iflag, ISTRIP); 491 if (!ISSET(flags, RAW|LITOUT)) 492 SET(oflag, OPOST); 493 else 494 CLR(oflag, OPOST); 495 } else { 496 CLR(cflag, CSIZE); 497 SET(cflag, CS7); 498 SET(iflag, ISTRIP); 499 SET(oflag, OPOST); 500 } 501 502 t->c_iflag = iflag; 503 t->c_oflag = oflag; 504 t->c_lflag = lflag; 505 t->c_cflag = cflag; 506 } 507 508 int 509 kern_tty_43_init(void) 510 { 511 MODULE_HOOK_SET(tty_ttioctl_43_hook, compat_43_ttioctl); 512 return 0; 513 } 514 515 int 516 kern_tty_43_fini(void) 517 { 518 MODULE_HOOK_UNSET(tty_ttioctl_43_hook); 519 return 0; 520 } 521