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