subr_terminal.c revision 256897
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: user/ed/newcons/sys/kern/subr_terminal.c 256897 2013-10-22 14:00:46Z 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 teken_set_winsize(&tm->tm_emulator, &r.tr_end); 204 TERMINAL_UNLOCK(tm); 205 206 if (blank) 207 tm->tm_class->tc_fill(tm, &r, 208 TCHAR_CREATE((teken_char_t)' ', &default_message)); 209 210 terminal_sync_ttysize(tm); 211} 212 213void 214terminal_set_winsize(struct terminal *tm, const struct winsize *size) 215{ 216 217 terminal_set_winsize_blank(tm, size, 1); 218} 219 220/* 221 * XXX: This function is a kludge. Drivers like vt(4) need to 222 * temporarily stop input when resizing, etc. This should ideally be 223 * handled within the driver. 224 */ 225 226void 227terminal_mute(struct terminal *tm, int yes) 228{ 229 230 TERMINAL_LOCK(tm); 231 if (yes) 232 tm->tm_flags |= TF_MUTE; 233 else 234 tm->tm_flags &= ~TF_MUTE; 235 TERMINAL_UNLOCK(tm); 236} 237 238void 239terminal_input_char(struct terminal *tm, term_char_t c) 240{ 241 struct tty *tp; 242 243 tp = tm->tm_tty; 244 if (tp == NULL) 245 return; 246 247 /* Strip off any attributes. */ 248 c = TCHAR_CHARACTER(c); 249 250 tty_lock(tp); 251 /* 252 * Conversion to UTF-8. 253 */ 254 if (c < 0x80) { 255 ttydisc_rint(tp, c, 0); 256 } else if (c < 0x800) { 257 char str[2] = { 258 0xc0 | (c >> 6), 259 0x80 | (c & 0x3f) 260 }; 261 262 ttydisc_rint_simple(tp, str, sizeof str); 263 } else if (c < 0x10000) { 264 char str[3] = { 265 0xe0 | (c >> 12), 266 0x80 | ((c >> 6) & 0x3f), 267 0x80 | (c & 0x3f) 268 }; 269 270 ttydisc_rint_simple(tp, str, sizeof str); 271 } else { 272 char str[4] = { 273 0xf0 | (c >> 18), 274 0x80 | ((c >> 12) & 0x3f), 275 0x80 | ((c >> 6) & 0x3f), 276 0x80 | (c & 0x3f) 277 }; 278 279 ttydisc_rint_simple(tp, str, sizeof str); 280 } 281 ttydisc_rint_done(tp); 282 tty_unlock(tp); 283} 284 285void 286terminal_input_raw(struct terminal *tm, char c) 287{ 288 struct tty *tp; 289 290 tp = tm->tm_tty; 291 if (tp == NULL) 292 return; 293 294 tty_lock(tp); 295 ttydisc_rint(tp, c, 0); 296 ttydisc_rint_done(tp); 297 tty_unlock(tp); 298} 299 300void 301terminal_input_special(struct terminal *tm, unsigned int k) 302{ 303 struct tty *tp; 304 const char *str; 305 306 tp = tm->tm_tty; 307 if (tp == NULL) 308 return; 309 310 str = teken_get_sequence(&tm->tm_emulator, k); 311 if (str == NULL) 312 return; 313 314 tty_lock(tp); 315 ttydisc_rint_simple(tp, str, strlen(str)); 316 ttydisc_rint_done(tp); 317 tty_unlock(tp); 318} 319 320/* 321 * Binding with the TTY layer. 322 */ 323 324static int 325termtty_open(struct tty *tp) 326{ 327 struct terminal *tm = tty_softc(tp); 328 329 tm->tm_class->tc_opened(tm, 1); 330 return (0); 331} 332 333static void 334termtty_close(struct tty *tp) 335{ 336 struct terminal *tm = tty_softc(tp); 337 338 tm->tm_class->tc_opened(tm, 0); 339} 340 341static void 342termtty_outwakeup(struct tty *tp) 343{ 344 struct terminal *tm = tty_softc(tp); 345 char obuf[128]; 346 size_t olen; 347 unsigned int flags = 0; 348 349 while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) { 350 TERMINAL_LOCK_TTY(tm); 351 if (!(tm->tm_flags & TF_MUTE)) { 352 tm->tm_flags &= ~TF_BELL; 353 teken_input(&tm->tm_emulator, obuf, olen); 354 flags |= tm->tm_flags; 355 } 356 TERMINAL_UNLOCK_TTY(tm); 357 } 358 359 tm->tm_class->tc_done(tm); 360 if (flags & TF_BELL) 361 tm->tm_class->tc_bell(tm); 362} 363 364static int 365termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 366{ 367 struct terminal *tm = tty_softc(tp); 368 int error; 369 370 switch (cmd) { 371 case CONS_GETINFO: { 372 vid_info_t *vi = (vid_info_t *)data; 373 const teken_pos_t *p; 374 int fg, bg; 375 376 if (vi->size != sizeof(vid_info_t)) 377 return (EINVAL); 378 379 /* Already help the console driver by filling in some data. */ 380 p = teken_get_cursor(&tm->tm_emulator); 381 vi->mv_row = p->tp_row; 382 vi->mv_col = p->tp_col; 383 384 p = teken_get_winsize(&tm->tm_emulator); 385 vi->mv_rsz = p->tp_row; 386 vi->mv_csz = p->tp_col; 387 388 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg); 389 vi->mv_norm.fore = fg; 390 vi->mv_norm.back = bg; 391 /* XXX: keep vidcontrol happy; bold backgrounds. */ 392 vi->mv_rev.fore = bg; 393 vi->mv_rev.back = fg & 0x7; 394 break; 395 } 396 } 397 398 /* 399 * Unlike various other drivers, this driver will never 400 * deallocate TTYs. This means it's safe to temporarily unlock 401 * the TTY when handling ioctls. 402 */ 403 tty_unlock(tp); 404 error = tm->tm_class->tc_ioctl(tm, cmd, data, td); 405 tty_lock(tp); 406 return (error); 407} 408 409/* 410 * Binding with the kernel and debug console. 411 */ 412 413static cn_probe_t termcn_cnprobe; 414static cn_init_t termcn_cninit; 415static cn_term_t termcn_cnterm; 416static cn_getc_t termcn_cngetc; 417static cn_putc_t termcn_cnputc; 418static cn_grab_t termcn_cngrab; 419static cn_ungrab_t termcn_cnungrab; 420 421const struct consdev_ops termcn_cnops = { 422 .cn_probe = termcn_cnprobe, 423 .cn_init = termcn_cninit, 424 .cn_term = termcn_cnterm, 425 .cn_getc = termcn_cngetc, 426 .cn_putc = termcn_cnputc, 427 .cn_grab = termcn_cngrab, 428 .cn_ungrab = termcn_cnungrab, 429}; 430 431void 432termcn_cnregister(struct terminal *tm) 433{ 434 struct consdev *cp; 435 436 cp = tm->consdev; 437 if (cp == NULL) { 438 cp = malloc(sizeof(struct consdev), M_TERMINAL, 439 M_WAITOK|M_ZERO); 440 cp->cn_ops = &termcn_cnops; 441 cp->cn_arg = tm; 442 cp->cn_pri = CN_INTERNAL; 443 sprintf(cp->cn_name, "ttyv0"); 444 445 tm->tm_flags = TF_CONS; 446 tm->consdev = cp; 447 448 terminal_init(tm); 449 } 450 451 /* Attach terminal as console. */ 452 cnadd(cp); 453} 454 455static void 456termcn_cngrab(struct consdev *cp) 457{ 458 459} 460 461static void 462termcn_cnungrab(struct consdev *cp) 463{ 464 465} 466 467static void 468termcn_cnprobe(struct consdev *cp) 469{ 470 struct terminal *tm = cp->cn_arg; 471 472 if (tm == NULL) { 473 cp->cn_pri = CN_DEAD; 474 return; 475 } 476 477 tm->consdev = cp; 478 terminal_init(tm); 479 480 tm->tm_class->tc_cnprobe(tm, cp); 481} 482 483static void 484termcn_cninit(struct consdev *cp) 485{ 486 487} 488 489static void 490termcn_cnterm(struct consdev *cp) 491{ 492 493} 494 495static int 496termcn_cngetc(struct consdev *cp) 497{ 498 struct terminal *tm = cp->cn_arg; 499 500 return (tm->tm_class->tc_cngetc(tm)); 501} 502 503static void 504termcn_cnputc(struct consdev *cp, int c) 505{ 506 struct terminal *tm = cp->cn_arg; 507 teken_attr_t backup; 508 char cv = c; 509 510 TERMINAL_LOCK_CONS(tm); 511 if (!(tm->tm_flags & TF_MUTE)) { 512 backup = *teken_get_curattr(&tm->tm_emulator); 513 teken_set_curattr(&tm->tm_emulator, &kernel_message); 514 teken_input(&tm->tm_emulator, &cv, 1); 515 teken_set_curattr(&tm->tm_emulator, &backup); 516 } 517 TERMINAL_UNLOCK_CONS(tm); 518 519 tm->tm_class->tc_done(tm); 520} 521 522/* 523 * Binding with the terminal emulator. 524 */ 525 526static void 527termteken_bell(void *softc) 528{ 529 struct terminal *tm = softc; 530 531 tm->tm_flags |= TF_BELL; 532} 533 534static void 535termteken_cursor(void *softc, const teken_pos_t *p) 536{ 537 struct terminal *tm = softc; 538 539 tm->tm_class->tc_cursor(tm, p); 540} 541 542static void 543termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c, 544 const teken_attr_t *a) 545{ 546 struct terminal *tm = softc; 547 548 tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a)); 549} 550 551static void 552termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c, 553 const teken_attr_t *a) 554{ 555 struct terminal *tm = softc; 556 557 tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a)); 558} 559 560static void 561termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p) 562{ 563 struct terminal *tm = softc; 564 565 tm->tm_class->tc_copy(tm, r, p); 566} 567 568static void 569termteken_param(void *softc, int cmd, unsigned int arg) 570{ 571 struct terminal *tm = softc; 572 573 tm->tm_class->tc_param(tm, cmd, arg); 574} 575 576static void 577termteken_respond(void *softc, const void *buf, size_t len) 578{ 579#if 0 580 struct terminal *tm = softc; 581 struct tty *tp; 582 583 /* 584 * Only inject a response into the TTY if the data actually 585 * originated from the TTY. 586 * 587 * XXX: This cannot be done right now. The TTY could pick up 588 * other locks. It could also in theory cause loops, when the 589 * TTY performs echoing of a command that generates even more 590 * input. 591 */ 592 tp = tm->tm_tty; 593 if (tp == NULL) 594 return; 595 596 ttydisc_rint_simple(tp, buf, len); 597 ttydisc_rint_done(tp); 598#endif 599} 600