subr_terminal.c revision 259016
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: head/sys/kern/subr_terminal.c 259016 2013-12-05 22:38:53Z ray $"); 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 46static 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 84static tsw_open_t termtty_open; 85static tsw_close_t termtty_close; 86static tsw_outwakeup_t termtty_outwakeup; 87static tsw_ioctl_t termtty_ioctl; 88 89static struct ttydevsw terminal_tty_class = { 90 .tsw_open = termtty_open, 91 .tsw_close = termtty_close, 92 .tsw_outwakeup = termtty_outwakeup, 93 .tsw_ioctl = termtty_ioctl, 94}; 95 96/* 97 * Terminal emulator routines. 98 */ 99 100static tf_bell_t termteken_bell; 101static tf_cursor_t termteken_cursor; 102static tf_putchar_t termteken_putchar; 103static tf_fill_t termteken_fill; 104static tf_copy_t termteken_copy; 105static tf_param_t termteken_param; 106static tf_respond_t termteken_respond; 107 108static teken_funcs_t terminal_drawmethods = { 109 .tf_bell = termteken_bell, 110 .tf_cursor = termteken_cursor, 111 .tf_putchar = termteken_putchar, 112 .tf_fill = termteken_fill, 113 .tf_copy = termteken_copy, 114 .tf_param = termteken_param, 115 .tf_respond = termteken_respond, 116}; 117 118/* Kernel message formatting. */ 119static const teken_attr_t kernel_message = { 120 .ta_fgcolor = TC_WHITE, 121 .ta_bgcolor = TC_BLACK, 122 .ta_format = TF_BOLD, 123}; 124 125static const teken_attr_t default_message = { 126 .ta_fgcolor = TC_WHITE, 127 .ta_bgcolor = TC_BLACK, 128}; 129 130#define TCHAR_CREATE(c, a) ((c) | \ 131 (a)->ta_format << 22 | \ 132 teken_256to8((a)->ta_fgcolor) << 26 | \ 133 teken_256to8((a)->ta_bgcolor) << 29) 134 135static void 136terminal_init(struct terminal *tm) 137{ 138 139 if (tm->tm_flags & TF_CONS) 140 mtx_init(&tm->tm_mtx, "trmlck", NULL, MTX_SPIN); 141 teken_init(&tm->tm_emulator, &terminal_drawmethods, tm); 142 teken_set_defattr(&tm->tm_emulator, &default_message); 143} 144 145struct terminal * 146terminal_alloc(const struct terminal_class *tc, void *softc) 147{ 148 struct terminal *tm; 149 150 tm = malloc(sizeof(struct terminal), M_TERMINAL, M_WAITOK|M_ZERO); 151 terminal_init(tm); 152 153 tm->tm_class = tc; 154 tm->tm_softc = softc; 155 156 return (tm); 157} 158 159static void 160terminal_sync_ttysize(struct terminal *tm) 161{ 162 struct tty *tp; 163 164 tp = tm->tm_tty; 165 if (tp == NULL) 166 return; 167 168 tty_lock(tp); 169 tty_set_winsize(tp, &tm->tm_winsize); 170 tty_unlock(tp); 171} 172 173void 174terminal_maketty(struct terminal *tm, const char *fmt, ...) 175{ 176 struct tty *tp; 177 char name[8]; 178 va_list ap; 179 180 va_start(ap, fmt); 181 vsnrprintf(name, sizeof name, 32, fmt, ap); 182 va_end(ap); 183 184 tp = tty_alloc(&terminal_tty_class, tm); 185 tty_makedev(tp, NULL, "%s", name); 186 tm->tm_tty = tp; 187 terminal_sync_ttysize(tm); 188} 189 190void 191terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size, 192 int blank) 193{ 194 term_rect_t r; 195 196 tm->tm_winsize = *size; 197 198 r.tr_begin.tp_row = r.tr_begin.tp_col = 0; 199 r.tr_end.tp_row = size->ws_row; 200 r.tr_end.tp_col = size->ws_col; 201 202 TERMINAL_LOCK(tm); 203 if (blank == 0) 204 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end); 205 else 206 teken_set_winsize(&tm->tm_emulator, &r.tr_end); 207 TERMINAL_UNLOCK(tm); 208 209 if (blank != 0) 210 tm->tm_class->tc_fill(tm, &r, TCHAR_CREATE((teken_char_t)' ', 211 &default_message)); 212 213 terminal_sync_ttysize(tm); 214} 215 216void 217terminal_set_winsize(struct terminal *tm, const struct winsize *size) 218{ 219 220 terminal_set_winsize_blank(tm, size, 1); 221} 222 223/* 224 * XXX: This function is a kludge. Drivers like vt(4) need to 225 * temporarily stop input when resizing, etc. This should ideally be 226 * handled within the driver. 227 */ 228 229void 230terminal_mute(struct terminal *tm, int yes) 231{ 232 233 TERMINAL_LOCK(tm); 234 if (yes) 235 tm->tm_flags |= TF_MUTE; 236 else 237 tm->tm_flags &= ~TF_MUTE; 238 TERMINAL_UNLOCK(tm); 239} 240 241void 242terminal_input_char(struct terminal *tm, term_char_t c) 243{ 244 struct tty *tp; 245 246 tp = tm->tm_tty; 247 if (tp == NULL) 248 return; 249 250 /* Strip off any attributes. */ 251 c = TCHAR_CHARACTER(c); 252 253 tty_lock(tp); 254 /* 255 * Conversion to UTF-8. 256 */ 257 if (c < 0x80) { 258 ttydisc_rint(tp, c, 0); 259 } else if (c < 0x800) { 260 char str[2] = { 261 0xc0 | (c >> 6), 262 0x80 | (c & 0x3f) 263 }; 264 265 ttydisc_rint_simple(tp, str, sizeof str); 266 } else if (c < 0x10000) { 267 char str[3] = { 268 0xe0 | (c >> 12), 269 0x80 | ((c >> 6) & 0x3f), 270 0x80 | (c & 0x3f) 271 }; 272 273 ttydisc_rint_simple(tp, str, sizeof str); 274 } else { 275 char str[4] = { 276 0xf0 | (c >> 18), 277 0x80 | ((c >> 12) & 0x3f), 278 0x80 | ((c >> 6) & 0x3f), 279 0x80 | (c & 0x3f) 280 }; 281 282 ttydisc_rint_simple(tp, str, sizeof str); 283 } 284 ttydisc_rint_done(tp); 285 tty_unlock(tp); 286} 287 288void 289terminal_input_raw(struct terminal *tm, char c) 290{ 291 struct tty *tp; 292 293 tp = tm->tm_tty; 294 if (tp == NULL) 295 return; 296 297 tty_lock(tp); 298 ttydisc_rint(tp, c, 0); 299 ttydisc_rint_done(tp); 300 tty_unlock(tp); 301} 302 303void 304terminal_input_special(struct terminal *tm, unsigned int k) 305{ 306 struct tty *tp; 307 const char *str; 308 309 tp = tm->tm_tty; 310 if (tp == NULL) 311 return; 312 313 str = teken_get_sequence(&tm->tm_emulator, k); 314 if (str == NULL) 315 return; 316 317 tty_lock(tp); 318 ttydisc_rint_simple(tp, str, strlen(str)); 319 ttydisc_rint_done(tp); 320 tty_unlock(tp); 321} 322 323/* 324 * Binding with the TTY layer. 325 */ 326 327static int 328termtty_open(struct tty *tp) 329{ 330 struct terminal *tm = tty_softc(tp); 331 332 tm->tm_class->tc_opened(tm, 1); 333 return (0); 334} 335 336static void 337termtty_close(struct tty *tp) 338{ 339 struct terminal *tm = tty_softc(tp); 340 341 tm->tm_class->tc_opened(tm, 0); 342} 343 344static void 345termtty_outwakeup(struct tty *tp) 346{ 347 struct terminal *tm = tty_softc(tp); 348 char obuf[128]; 349 size_t olen; 350 unsigned int flags = 0; 351 352 while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) { 353 TERMINAL_LOCK_TTY(tm); 354 if (!(tm->tm_flags & TF_MUTE)) { 355 tm->tm_flags &= ~TF_BELL; 356 teken_input(&tm->tm_emulator, obuf, olen); 357 flags |= tm->tm_flags; 358 } 359 TERMINAL_UNLOCK_TTY(tm); 360 } 361 362 tm->tm_class->tc_done(tm); 363 if (flags & TF_BELL) 364 tm->tm_class->tc_bell(tm); 365} 366 367static int 368termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 369{ 370 struct terminal *tm = tty_softc(tp); 371 int error; 372 373 switch (cmd) { 374 case CONS_GETINFO: { 375 vid_info_t *vi = (vid_info_t *)data; 376 const teken_pos_t *p; 377 int fg, bg; 378 379 if (vi->size != sizeof(vid_info_t)) 380 return (EINVAL); 381 382 /* Already help the console driver by filling in some data. */ 383 p = teken_get_cursor(&tm->tm_emulator); 384 vi->mv_row = p->tp_row; 385 vi->mv_col = p->tp_col; 386 387 p = teken_get_winsize(&tm->tm_emulator); 388 vi->mv_rsz = p->tp_row; 389 vi->mv_csz = p->tp_col; 390 391 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg); 392 vi->mv_norm.fore = fg; 393 vi->mv_norm.back = bg; 394 /* XXX: keep vidcontrol happy; bold backgrounds. */ 395 vi->mv_rev.fore = bg; 396 vi->mv_rev.back = fg & 0x7; 397 break; 398 } 399 } 400 401 /* 402 * Unlike various other drivers, this driver will never 403 * deallocate TTYs. This means it's safe to temporarily unlock 404 * the TTY when handling ioctls. 405 */ 406 tty_unlock(tp); 407 error = tm->tm_class->tc_ioctl(tm, cmd, data, td); 408 tty_lock(tp); 409 return (error); 410} 411 412/* 413 * Binding with the kernel and debug console. 414 */ 415 416static cn_probe_t termcn_cnprobe; 417static cn_init_t termcn_cninit; 418static cn_term_t termcn_cnterm; 419static cn_getc_t termcn_cngetc; 420static cn_putc_t termcn_cnputc; 421static cn_grab_t termcn_cngrab; 422static cn_ungrab_t termcn_cnungrab; 423 424const struct consdev_ops termcn_cnops = { 425 .cn_probe = termcn_cnprobe, 426 .cn_init = termcn_cninit, 427 .cn_term = termcn_cnterm, 428 .cn_getc = termcn_cngetc, 429 .cn_putc = termcn_cnputc, 430 .cn_grab = termcn_cngrab, 431 .cn_ungrab = termcn_cnungrab, 432}; 433 434void 435termcn_cnregister(struct terminal *tm) 436{ 437 struct consdev *cp; 438 439 cp = tm->consdev; 440 if (cp == NULL) { 441 cp = malloc(sizeof(struct consdev), M_TERMINAL, 442 M_WAITOK|M_ZERO); 443 cp->cn_ops = &termcn_cnops; 444 cp->cn_arg = tm; 445 cp->cn_pri = CN_INTERNAL; 446 sprintf(cp->cn_name, "ttyv0"); 447 448 tm->tm_flags = TF_CONS; 449 tm->consdev = cp; 450 451 terminal_init(tm); 452 } 453 454 /* Attach terminal as console. */ 455 cnadd(cp); 456} 457 458static void 459termcn_cngrab(struct consdev *cp) 460{ 461 462} 463 464static void 465termcn_cnungrab(struct consdev *cp) 466{ 467 468} 469 470static void 471termcn_cnprobe(struct consdev *cp) 472{ 473 struct terminal *tm = cp->cn_arg; 474 475 if (tm == NULL) { 476 cp->cn_pri = CN_DEAD; 477 return; 478 } 479 480 tm->consdev = cp; 481 terminal_init(tm); 482 483 tm->tm_class->tc_cnprobe(tm, cp); 484} 485 486static void 487termcn_cninit(struct consdev *cp) 488{ 489 490} 491 492static void 493termcn_cnterm(struct consdev *cp) 494{ 495 496} 497 498static int 499termcn_cngetc(struct consdev *cp) 500{ 501 struct terminal *tm = cp->cn_arg; 502 503 return (tm->tm_class->tc_cngetc(tm)); 504} 505 506static void 507termcn_cnputc(struct consdev *cp, int c) 508{ 509 struct terminal *tm = cp->cn_arg; 510 teken_attr_t backup; 511 char cv = c; 512 513 TERMINAL_LOCK_CONS(tm); 514 if (!(tm->tm_flags & TF_MUTE)) { 515 backup = *teken_get_curattr(&tm->tm_emulator); 516 teken_set_curattr(&tm->tm_emulator, &kernel_message); 517 teken_input(&tm->tm_emulator, &cv, 1); 518 teken_set_curattr(&tm->tm_emulator, &backup); 519 } 520 TERMINAL_UNLOCK_CONS(tm); 521 522 tm->tm_class->tc_done(tm); 523} 524 525/* 526 * Binding with the terminal emulator. 527 */ 528 529static void 530termteken_bell(void *softc) 531{ 532 struct terminal *tm = softc; 533 534 tm->tm_flags |= TF_BELL; 535} 536 537static void 538termteken_cursor(void *softc, const teken_pos_t *p) 539{ 540 struct terminal *tm = softc; 541 542 tm->tm_class->tc_cursor(tm, p); 543} 544 545static void 546termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c, 547 const teken_attr_t *a) 548{ 549 struct terminal *tm = softc; 550 551 tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a)); 552} 553 554static void 555termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c, 556 const teken_attr_t *a) 557{ 558 struct terminal *tm = softc; 559 560 tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a)); 561} 562 563static void 564termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p) 565{ 566 struct terminal *tm = softc; 567 568 tm->tm_class->tc_copy(tm, r, p); 569} 570 571static void 572termteken_param(void *softc, int cmd, unsigned int arg) 573{ 574 struct terminal *tm = softc; 575 576 tm->tm_class->tc_param(tm, cmd, arg); 577} 578 579static void 580termteken_respond(void *softc, const void *buf, size_t len) 581{ 582#if 0 583 struct terminal *tm = softc; 584 struct tty *tp; 585 586 /* 587 * Only inject a response into the TTY if the data actually 588 * originated from the TTY. 589 * 590 * XXX: This cannot be done right now. The TTY could pick up 591 * other locks. It could also in theory cause loops, when the 592 * TTY performs echoing of a command that generates even more 593 * input. 594 */ 595 tp = tm->tm_tty; 596 if (tp == NULL) 597 return; 598 599 ttydisc_rint_simple(tp, buf, len); 600 ttydisc_rint_done(tp); 601#endif 602} 603