1 /*- 2 * Copyright (c) 2009 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Ed Schouten under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/cons.h> 35 #include <sys/consio.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/systm.h> 41 #include <sys/terminal.h> 42 #include <sys/tty.h> 43 44 #include <machine/stdarg.h> 45 46 static MALLOC_DEFINE(M_TERMINAL, "terminal", "terminal device"); 47 48 /* 49 * Locking. 50 * 51 * Normally we don't need to lock down the terminal emulator, because 52 * the TTY lock is already held when calling teken_input(). 53 * Unfortunately this is not the case when the terminal acts as a 54 * console device, because cnputc() can be called at the same time. 55 * This means terminals may need to be locked down using a spin lock. 56 */ 57 #define TERMINAL_LOCK(tm) do { \ 58 if ((tm)->tm_flags & TF_CONS) \ 59 mtx_lock_spin(&(tm)->tm_mtx); \ 60 else if ((tm)->tm_tty != NULL) \ 61 tty_lock((tm)->tm_tty); \ 62 } while (0) 63 #define TERMINAL_UNLOCK(tm) do { \ 64 if ((tm)->tm_flags & TF_CONS) \ 65 mtx_unlock_spin(&(tm)->tm_mtx); \ 66 else if ((tm)->tm_tty != NULL) \ 67 tty_unlock((tm)->tm_tty); \ 68 } while (0) 69 #define TERMINAL_LOCK_TTY(tm) do { \ 70 if ((tm)->tm_flags & TF_CONS) \ 71 mtx_lock_spin(&(tm)->tm_mtx); \ 72 } while (0) 73 #define TERMINAL_UNLOCK_TTY(tm) do { \ 74 if ((tm)->tm_flags & TF_CONS) \ 75 mtx_unlock_spin(&(tm)->tm_mtx); \ 76 } while (0) 77 #define TERMINAL_LOCK_CONS(tm) mtx_lock_spin(&(tm)->tm_mtx) 78 #define TERMINAL_UNLOCK_CONS(tm) mtx_unlock_spin(&(tm)->tm_mtx) 79 80 /* 81 * TTY routines. 82 */ 83 84 static tsw_open_t termtty_open; 85 static tsw_close_t termtty_close; 86 static tsw_outwakeup_t termtty_outwakeup; 87 static tsw_ioctl_t termtty_ioctl; 88 static tsw_mmap_t termtty_mmap; 89 90 static struct ttydevsw terminal_tty_class = { 91 .tsw_open = termtty_open, 92 .tsw_close = termtty_close, 93 .tsw_outwakeup = termtty_outwakeup, 94 .tsw_ioctl = termtty_ioctl, 95 .tsw_mmap = termtty_mmap, 96 }; 97 98 /* 99 * Terminal emulator routines. 100 */ 101 102 static tf_bell_t termteken_bell; 103 static tf_cursor_t termteken_cursor; 104 static tf_putchar_t termteken_putchar; 105 static tf_fill_t termteken_fill; 106 static tf_copy_t termteken_copy; 107 static tf_param_t termteken_param; 108 static tf_respond_t termteken_respond; 109 110 static teken_funcs_t terminal_drawmethods = { 111 .tf_bell = termteken_bell, 112 .tf_cursor = termteken_cursor, 113 .tf_putchar = termteken_putchar, 114 .tf_fill = termteken_fill, 115 .tf_copy = termteken_copy, 116 .tf_param = termteken_param, 117 .tf_respond = termteken_respond, 118 }; 119 120 /* Kernel message formatting. */ 121 static const teken_attr_t kernel_message = { 122 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_KERN_ATTR), 123 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_KERN_ATTR), 124 .ta_format = TCHAR_FORMAT(TERMINAL_KERN_ATTR) 125 }; 126 127 static const teken_attr_t default_message = { 128 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_NORM_ATTR), 129 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_NORM_ATTR), 130 .ta_format = TCHAR_FORMAT(TERMINAL_NORM_ATTR) 131 }; 132 133 #define TCHAR_CREATE(c, a) ((c) | TFORMAT((a)->ta_format) | \ 134 TCOLOR_FG(teken_256to8((a)->ta_fgcolor)) | \ 135 TCOLOR_BG(teken_256to8((a)->ta_bgcolor))) 136 137 static void 138 terminal_init(struct terminal *tm) 139 { 140 141 if (tm->tm_flags & TF_CONS) 142 mtx_init(&tm->tm_mtx, "trmlck", NULL, MTX_SPIN); 143 teken_init(&tm->tm_emulator, &terminal_drawmethods, tm); 144 teken_set_defattr(&tm->tm_emulator, &default_message); 145 } 146 147 struct terminal * 148 terminal_alloc(const struct terminal_class *tc, void *softc) 149 { 150 struct terminal *tm; 151 152 tm = malloc(sizeof(struct terminal), M_TERMINAL, M_WAITOK|M_ZERO); 153 terminal_init(tm); 154 155 tm->tm_class = tc; 156 tm->tm_softc = softc; 157 158 return (tm); 159 } 160 161 static void 162 terminal_sync_ttysize(struct terminal *tm) 163 { 164 struct tty *tp; 165 166 tp = tm->tm_tty; 167 if (tp == NULL) 168 return; 169 170 tty_lock(tp); 171 tty_set_winsize(tp, &tm->tm_winsize); 172 tty_unlock(tp); 173 } 174 175 void 176 terminal_maketty(struct terminal *tm, const char *fmt, ...) 177 { 178 struct tty *tp; 179 char name[8]; 180 va_list ap; 181 182 va_start(ap, fmt); 183 vsnrprintf(name, sizeof name, 32, fmt, ap); 184 va_end(ap); 185 186 tp = tty_alloc(&terminal_tty_class, tm); 187 tty_makedev(tp, NULL, "%s", name); 188 tm->tm_tty = tp; 189 terminal_sync_ttysize(tm); 190 } 191 192 void 193 terminal_set_cursor(struct terminal *tm, const term_pos_t *pos) 194 { 195 196 teken_set_cursor(&tm->tm_emulator, pos); 197 } 198 199 void 200 terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size, 201 int blank, const term_attr_t *attr) 202 { 203 term_rect_t r; 204 205 tm->tm_winsize = *size; 206 207 r.tr_begin.tp_row = r.tr_begin.tp_col = 0; 208 r.tr_end.tp_row = size->ws_row; 209 r.tr_end.tp_col = size->ws_col; 210 211 TERMINAL_LOCK(tm); 212 if (blank == 0) 213 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end); 214 else 215 teken_set_winsize(&tm->tm_emulator, &r.tr_end); 216 TERMINAL_UNLOCK(tm); 217 218 if ((blank != 0) && !(tm->tm_flags & TF_MUTE)) 219 tm->tm_class->tc_fill(tm, &r, 220 TCHAR_CREATE((teken_char_t)' ', attr)); 221 222 terminal_sync_ttysize(tm); 223 } 224 225 void 226 terminal_set_winsize(struct terminal *tm, const struct winsize *size) 227 { 228 229 terminal_set_winsize_blank(tm, size, 1, 230 (const term_attr_t *)&default_message); 231 } 232 233 /* 234 * XXX: This function is a kludge. Drivers like vt(4) need to 235 * temporarily stop input when resizing, etc. This should ideally be 236 * handled within the driver. 237 */ 238 239 void 240 terminal_mute(struct terminal *tm, int yes) 241 { 242 243 TERMINAL_LOCK(tm); 244 if (yes) 245 tm->tm_flags |= TF_MUTE; 246 else 247 tm->tm_flags &= ~TF_MUTE; 248 TERMINAL_UNLOCK(tm); 249 } 250 251 void 252 terminal_input_char(struct terminal *tm, term_char_t c) 253 { 254 struct tty *tp; 255 256 tp = tm->tm_tty; 257 if (tp == NULL) 258 return; 259 260 /* 261 * Strip off any attributes. Also ignore input of second part of 262 * CJK fullwidth characters, as we don't want to return these 263 * characters twice. 264 */ 265 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT) 266 return; 267 c = TCHAR_CHARACTER(c); 268 269 tty_lock(tp); 270 /* 271 * Conversion to UTF-8. 272 */ 273 if (c < 0x80) { 274 ttydisc_rint(tp, c, 0); 275 } else if (c < 0x800) { 276 char str[2] = { 277 0xc0 | (c >> 6), 278 0x80 | (c & 0x3f) 279 }; 280 281 ttydisc_rint_simple(tp, str, sizeof str); 282 } else if (c < 0x10000) { 283 char str[3] = { 284 0xe0 | (c >> 12), 285 0x80 | ((c >> 6) & 0x3f), 286 0x80 | (c & 0x3f) 287 }; 288 289 ttydisc_rint_simple(tp, str, sizeof str); 290 } else { 291 char str[4] = { 292 0xf0 | (c >> 18), 293 0x80 | ((c >> 12) & 0x3f), 294 0x80 | ((c >> 6) & 0x3f), 295 0x80 | (c & 0x3f) 296 }; 297 298 ttydisc_rint_simple(tp, str, sizeof str); 299 } 300 ttydisc_rint_done(tp); 301 tty_unlock(tp); 302 } 303 304 void 305 terminal_input_raw(struct terminal *tm, char c) 306 { 307 struct tty *tp; 308 309 tp = tm->tm_tty; 310 if (tp == NULL) 311 return; 312 313 tty_lock(tp); 314 ttydisc_rint(tp, c, 0); 315 ttydisc_rint_done(tp); 316 tty_unlock(tp); 317 } 318 319 void 320 terminal_input_special(struct terminal *tm, unsigned int k) 321 { 322 struct tty *tp; 323 const char *str; 324 325 tp = tm->tm_tty; 326 if (tp == NULL) 327 return; 328 329 str = teken_get_sequence(&tm->tm_emulator, k); 330 if (str == NULL) 331 return; 332 333 tty_lock(tp); 334 ttydisc_rint_simple(tp, str, strlen(str)); 335 ttydisc_rint_done(tp); 336 tty_unlock(tp); 337 } 338 339 /* 340 * Binding with the TTY layer. 341 */ 342 343 static int 344 termtty_open(struct tty *tp) 345 { 346 struct terminal *tm = tty_softc(tp); 347 348 tm->tm_class->tc_opened(tm, 1); 349 return (0); 350 } 351 352 static void 353 termtty_close(struct tty *tp) 354 { 355 struct terminal *tm = tty_softc(tp); 356 357 tm->tm_class->tc_opened(tm, 0); 358 } 359 360 static void 361 termtty_outwakeup(struct tty *tp) 362 { 363 struct terminal *tm = tty_softc(tp); 364 char obuf[128]; 365 size_t olen; 366 unsigned int flags = 0; 367 368 while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) { 369 TERMINAL_LOCK_TTY(tm); 370 if (!(tm->tm_flags & TF_MUTE)) { 371 tm->tm_flags &= ~TF_BELL; 372 teken_input(&tm->tm_emulator, obuf, olen); 373 flags |= tm->tm_flags; 374 } 375 TERMINAL_UNLOCK_TTY(tm); 376 } 377 378 TERMINAL_LOCK_TTY(tm); 379 if (!(tm->tm_flags & TF_MUTE)) 380 tm->tm_class->tc_done(tm); 381 TERMINAL_UNLOCK_TTY(tm); 382 if (flags & TF_BELL) 383 tm->tm_class->tc_bell(tm); 384 } 385 386 static int 387 termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 388 { 389 struct terminal *tm = tty_softc(tp); 390 int error; 391 392 switch (cmd) { 393 case CONS_GETINFO: { 394 vid_info_t *vi = (vid_info_t *)data; 395 const teken_pos_t *p; 396 int fg, bg; 397 398 if (vi->size != sizeof(vid_info_t)) 399 return (EINVAL); 400 401 /* Already help the console driver by filling in some data. */ 402 p = teken_get_cursor(&tm->tm_emulator); 403 vi->mv_row = p->tp_row; 404 vi->mv_col = p->tp_col; 405 406 p = teken_get_winsize(&tm->tm_emulator); 407 vi->mv_rsz = p->tp_row; 408 vi->mv_csz = p->tp_col; 409 410 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg); 411 vi->mv_norm.fore = fg; 412 vi->mv_norm.back = bg; 413 /* XXX: keep vidcontrol happy; bold backgrounds. */ 414 vi->mv_rev.fore = bg; 415 vi->mv_rev.back = fg & 0x7; 416 break; 417 } 418 } 419 420 /* 421 * Unlike various other drivers, this driver will never 422 * deallocate TTYs. This means it's safe to temporarily unlock 423 * the TTY when handling ioctls. 424 */ 425 tty_unlock(tp); 426 error = tm->tm_class->tc_ioctl(tm, cmd, data, td); 427 tty_lock(tp); 428 return (error); 429 } 430 431 static int 432 termtty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t * paddr, 433 int nprot, vm_memattr_t *memattr) 434 { 435 struct terminal *tm = tty_softc(tp); 436 437 return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr)); 438 } 439 440 /* 441 * Binding with the kernel and debug console. 442 */ 443 444 static cn_probe_t termcn_cnprobe; 445 static cn_init_t termcn_cninit; 446 static cn_term_t termcn_cnterm; 447 static cn_getc_t termcn_cngetc; 448 static cn_putc_t termcn_cnputc; 449 static cn_grab_t termcn_cngrab; 450 static cn_ungrab_t termcn_cnungrab; 451 452 const struct consdev_ops termcn_cnops = { 453 .cn_probe = termcn_cnprobe, 454 .cn_init = termcn_cninit, 455 .cn_term = termcn_cnterm, 456 .cn_getc = termcn_cngetc, 457 .cn_putc = termcn_cnputc, 458 .cn_grab = termcn_cngrab, 459 .cn_ungrab = termcn_cnungrab, 460 }; 461 462 void 463 termcn_cnregister(struct terminal *tm) 464 { 465 struct consdev *cp; 466 467 cp = tm->consdev; 468 if (cp == NULL) { 469 cp = malloc(sizeof(struct consdev), M_TERMINAL, 470 M_WAITOK|M_ZERO); 471 cp->cn_ops = &termcn_cnops; 472 cp->cn_arg = tm; 473 cp->cn_pri = CN_INTERNAL; 474 sprintf(cp->cn_name, "ttyv0"); 475 476 tm->tm_flags = TF_CONS; 477 tm->consdev = cp; 478 479 terminal_init(tm); 480 } 481 482 /* Attach terminal as console. */ 483 cnadd(cp); 484 } 485 486 static void 487 termcn_cngrab(struct consdev *cp) 488 { 489 struct terminal *tm = cp->cn_arg; 490 491 tm->tm_class->tc_cngrab(tm); 492 } 493 494 static void 495 termcn_cnungrab(struct consdev *cp) 496 { 497 struct terminal *tm = cp->cn_arg; 498 499 tm->tm_class->tc_cnungrab(tm); 500 } 501 502 static void 503 termcn_cnprobe(struct consdev *cp) 504 { 505 struct terminal *tm = cp->cn_arg; 506 507 if (tm == NULL) { 508 cp->cn_pri = CN_DEAD; 509 return; 510 } 511 512 tm->consdev = cp; 513 terminal_init(tm); 514 515 tm->tm_class->tc_cnprobe(tm, cp); 516 } 517 518 static void 519 termcn_cninit(struct consdev *cp) 520 { 521 522 } 523 524 static void 525 termcn_cnterm(struct consdev *cp) 526 { 527 528 } 529 530 static int 531 termcn_cngetc(struct consdev *cp) 532 { 533 struct terminal *tm = cp->cn_arg; 534 535 return (tm->tm_class->tc_cngetc(tm)); 536 } 537 538 static void 539 termcn_cnputc(struct consdev *cp, int c) 540 { 541 struct terminal *tm = cp->cn_arg; 542 teken_attr_t backup; 543 char cv = c; 544 545 TERMINAL_LOCK_CONS(tm); 546 if (!(tm->tm_flags & TF_MUTE)) { 547 backup = *teken_get_curattr(&tm->tm_emulator); 548 teken_set_curattr(&tm->tm_emulator, &kernel_message); 549 teken_input(&tm->tm_emulator, &cv, 1); 550 teken_set_curattr(&tm->tm_emulator, &backup); 551 tm->tm_class->tc_done(tm); 552 } 553 TERMINAL_UNLOCK_CONS(tm); 554 } 555 556 /* 557 * Binding with the terminal emulator. 558 */ 559 560 static void 561 termteken_bell(void *softc) 562 { 563 struct terminal *tm = softc; 564 565 tm->tm_flags |= TF_BELL; 566 } 567 568 static void 569 termteken_cursor(void *softc, const teken_pos_t *p) 570 { 571 struct terminal *tm = softc; 572 573 tm->tm_class->tc_cursor(tm, p); 574 } 575 576 static void 577 termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c, 578 const teken_attr_t *a) 579 { 580 struct terminal *tm = softc; 581 582 tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a)); 583 } 584 585 static void 586 termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c, 587 const teken_attr_t *a) 588 { 589 struct terminal *tm = softc; 590 591 tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a)); 592 } 593 594 static void 595 termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p) 596 { 597 struct terminal *tm = softc; 598 599 tm->tm_class->tc_copy(tm, r, p); 600 } 601 602 static void 603 termteken_param(void *softc, int cmd, unsigned int arg) 604 { 605 struct terminal *tm = softc; 606 607 tm->tm_class->tc_param(tm, cmd, arg); 608 } 609 610 static void 611 termteken_respond(void *softc, const void *buf, size_t len) 612 { 613 #if 0 614 struct terminal *tm = softc; 615 struct tty *tp; 616 617 /* 618 * Only inject a response into the TTY if the data actually 619 * originated from the TTY. 620 * 621 * XXX: This cannot be done right now. The TTY could pick up 622 * other locks. It could also in theory cause loops, when the 623 * TTY performs echoing of a command that generates even more 624 * input. 625 */ 626 tp = tm->tm_tty; 627 if (tp == NULL) 628 return; 629 630 ttydisc_rint_simple(tp, buf, len); 631 ttydisc_rint_done(tp); 632 #endif 633 } 634