2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009 The FreeBSD Foundation
6 * This software was developed by Ed Schouten under sponsorship from the
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/consio.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39 #include <sys/terminal.h>
42 #include <machine/stdarg.h>
44 static MALLOC_DEFINE(M_TERMINAL, "terminal", "terminal device");
49 * Normally we don't need to lock down the terminal emulator, because
50 * the TTY lock is already held when calling teken_input().
51 * Unfortunately this is not the case when the terminal acts as a
52 * console device, because cnputc() can be called at the same time.
53 * This means terminals may need to be locked down using a spin lock.
55 #define TERMINAL_LOCK(tm) do { \
56 if ((tm)->tm_flags & TF_CONS) \
57 mtx_lock_spin(&(tm)->tm_mtx); \
58 else if ((tm)->tm_tty != NULL) \
59 tty_lock((tm)->tm_tty); \
61 #define TERMINAL_UNLOCK(tm) do { \
62 if ((tm)->tm_flags & TF_CONS) \
63 mtx_unlock_spin(&(tm)->tm_mtx); \
64 else if ((tm)->tm_tty != NULL) \
65 tty_unlock((tm)->tm_tty); \
67 #define TERMINAL_LOCK_TTY(tm) do { \
68 if ((tm)->tm_flags & TF_CONS) \
69 mtx_lock_spin(&(tm)->tm_mtx); \
71 #define TERMINAL_UNLOCK_TTY(tm) do { \
72 if ((tm)->tm_flags & TF_CONS) \
73 mtx_unlock_spin(&(tm)->tm_mtx); \
75 #define TERMINAL_LOCK_CONS(tm) mtx_lock_spin(&(tm)->tm_mtx)
76 #define TERMINAL_UNLOCK_CONS(tm) mtx_unlock_spin(&(tm)->tm_mtx)
82 static tsw_open_t termtty_open;
83 static tsw_close_t termtty_close;
84 static tsw_outwakeup_t termtty_outwakeup;
85 static tsw_ioctl_t termtty_ioctl;
86 static tsw_mmap_t termtty_mmap;
88 static struct ttydevsw terminal_tty_class = {
89 .tsw_open = termtty_open,
90 .tsw_close = termtty_close,
91 .tsw_outwakeup = termtty_outwakeup,
92 .tsw_ioctl = termtty_ioctl,
93 .tsw_mmap = termtty_mmap,
97 * Terminal emulator routines.
100 static tf_bell_t termteken_bell;
101 static tf_cursor_t termteken_cursor;
102 static tf_putchar_t termteken_putchar;
103 static tf_fill_t termteken_fill;
104 static tf_copy_t termteken_copy;
105 static tf_pre_input_t termteken_pre_input;
106 static tf_post_input_t termteken_post_input;
107 static tf_param_t termteken_param;
108 static tf_respond_t termteken_respond;
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_pre_input = termteken_pre_input,
117 .tf_post_input = termteken_post_input,
118 .tf_param = termteken_param,
119 .tf_respond = termteken_respond,
122 /* Kernel message formatting. */
123 static teken_attr_t kernel_message = {
124 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_KERN_ATTR),
125 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_KERN_ATTR),
126 .ta_format = TCHAR_FORMAT(TERMINAL_KERN_ATTR)
129 static teken_attr_t default_message = {
130 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_NORM_ATTR),
131 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_NORM_ATTR),
132 .ta_format = TCHAR_FORMAT(TERMINAL_NORM_ATTR)
135 /* Fudge fg brightness as TF_BOLD (shifted). */
136 #define TCOLOR_FG_FUDGED(color) __extension__ ({ \
140 TCOLOR_FG(_c & 7) | ((_c & 8) << 18); \
143 /* Fudge bg brightness as TF_BLINK (shifted). */
144 #define TCOLOR_BG_FUDGED(color) __extension__ ({ \
148 TCOLOR_BG(_c & 7) | ((_c & 8) << 20); \
151 #define TCOLOR_256TO16(color) __extension__ ({ \
156 _c = teken_256to16(_c); \
160 #define TCHAR_CREATE(c, a) ((c) | TFORMAT((a)->ta_format) | \
161 TCOLOR_FG_FUDGED(TCOLOR_256TO16((a)->ta_fgcolor)) | \
162 TCOLOR_BG_FUDGED(TCOLOR_256TO16((a)->ta_bgcolor)))
165 terminal_init(struct terminal *tm)
169 if (tm->tm_flags & TF_CONS)
170 mtx_init(&tm->tm_mtx, "trmlck", NULL, MTX_SPIN);
172 teken_init(&tm->tm_emulator, &terminal_drawmethods, tm);
175 TUNABLE_INT_FETCH("teken.fg_color", &fg);
176 TUNABLE_INT_FETCH("teken.bg_color", &bg);
179 default_message.ta_fgcolor = fg;
180 kernel_message.ta_fgcolor = fg;
183 default_message.ta_bgcolor = bg;
184 kernel_message.ta_bgcolor = bg;
187 if (default_message.ta_bgcolor == TC_WHITE) {
188 default_message.ta_bgcolor |= TC_LIGHT;
189 kernel_message.ta_bgcolor |= TC_LIGHT;
192 if (default_message.ta_bgcolor == TC_BLACK &&
193 default_message.ta_fgcolor < TC_NCOLORS)
194 kernel_message.ta_fgcolor |= TC_LIGHT;
195 teken_set_defattr(&tm->tm_emulator, &default_message);
199 terminal_alloc(const struct terminal_class *tc, void *softc)
203 tm = malloc(sizeof(struct terminal), M_TERMINAL, M_WAITOK|M_ZERO);
207 tm->tm_softc = softc;
213 terminal_sync_ttysize(struct terminal *tm)
222 tty_set_winsize(tp, &tm->tm_winsize);
227 terminal_maketty(struct terminal *tm, const char *fmt, ...)
234 vsnrprintf(name, sizeof name, 32, fmt, ap);
237 tp = tty_alloc(&terminal_tty_class, tm);
238 tty_makedev(tp, NULL, "%s", name);
240 terminal_sync_ttysize(tm);
244 terminal_set_cursor(struct terminal *tm, const term_pos_t *pos)
247 teken_set_cursor(&tm->tm_emulator, pos);
251 terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size,
252 int blank, const term_attr_t *attr)
256 tm->tm_winsize = *size;
258 r.tr_begin.tp_row = r.tr_begin.tp_col = 0;
259 r.tr_end.tp_row = size->ws_row;
260 r.tr_end.tp_col = size->ws_col;
264 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end);
266 teken_set_winsize(&tm->tm_emulator, &r.tr_end);
269 if ((blank != 0) && !(tm->tm_flags & TF_MUTE))
270 tm->tm_class->tc_fill(tm, &r,
271 TCHAR_CREATE((teken_char_t)' ', attr));
273 terminal_sync_ttysize(tm);
277 terminal_set_winsize(struct terminal *tm, const struct winsize *size)
280 terminal_set_winsize_blank(tm, size, 1,
281 (const term_attr_t *)&default_message);
285 * XXX: This function is a kludge. Drivers like vt(4) need to
286 * temporarily stop input when resizing, etc. This should ideally be
287 * handled within the driver.
291 terminal_mute(struct terminal *tm, int yes)
296 tm->tm_flags |= TF_MUTE;
298 tm->tm_flags &= ~TF_MUTE;
303 terminal_input_char(struct terminal *tm, term_char_t c)
312 * Strip off any attributes. Also ignore input of second part of
313 * CJK fullwidth characters, as we don't want to return these
316 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT)
318 c = TCHAR_CHARACTER(c);
322 * Conversion to UTF-8.
325 ttydisc_rint(tp, c, 0);
326 } else if (c < 0x800) {
332 ttydisc_rint_simple(tp, str, sizeof str);
333 } else if (c < 0x10000) {
336 0x80 | ((c >> 6) & 0x3f),
340 ttydisc_rint_simple(tp, str, sizeof str);
344 0x80 | ((c >> 12) & 0x3f),
345 0x80 | ((c >> 6) & 0x3f),
349 ttydisc_rint_simple(tp, str, sizeof str);
351 ttydisc_rint_done(tp);
356 terminal_input_raw(struct terminal *tm, char c)
365 ttydisc_rint(tp, c, 0);
366 ttydisc_rint_done(tp);
371 terminal_input_special(struct terminal *tm, unsigned int k)
380 str = teken_get_sequence(&tm->tm_emulator, k);
385 ttydisc_rint_simple(tp, str, strlen(str));
386 ttydisc_rint_done(tp);
391 * Binding with the TTY layer.
395 termtty_open(struct tty *tp)
397 struct terminal *tm = tty_softc(tp);
399 tm->tm_class->tc_opened(tm, 1);
404 termtty_close(struct tty *tp)
406 struct terminal *tm = tty_softc(tp);
408 tm->tm_class->tc_opened(tm, 0);
412 termtty_outwakeup(struct tty *tp)
414 struct terminal *tm = tty_softc(tp);
417 unsigned int flags = 0;
419 while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) {
420 TERMINAL_LOCK_TTY(tm);
421 if (!(tm->tm_flags & TF_MUTE)) {
422 tm->tm_flags &= ~TF_BELL;
423 teken_input(&tm->tm_emulator, obuf, olen);
424 flags |= tm->tm_flags;
426 TERMINAL_UNLOCK_TTY(tm);
429 TERMINAL_LOCK_TTY(tm);
430 if (!(tm->tm_flags & TF_MUTE))
431 tm->tm_class->tc_done(tm);
432 TERMINAL_UNLOCK_TTY(tm);
434 tm->tm_class->tc_bell(tm);
438 termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
440 struct terminal *tm = tty_softc(tp);
445 vid_info_t *vi = (vid_info_t *)data;
446 const teken_pos_t *p;
449 if (vi->size != sizeof(vid_info_t))
452 /* Already help the console driver by filling in some data. */
453 p = teken_get_cursor(&tm->tm_emulator);
454 vi->mv_row = p->tp_row;
455 vi->mv_col = p->tp_col;
457 p = teken_get_winsize(&tm->tm_emulator);
458 vi->mv_rsz = p->tp_row;
459 vi->mv_csz = p->tp_col;
461 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg);
462 vi->mv_norm.fore = fg;
463 vi->mv_norm.back = bg;
464 /* XXX: keep vidcontrol happy; bold backgrounds. */
465 vi->mv_rev.fore = bg;
466 vi->mv_rev.back = fg & 0x7;
472 * Unlike various other drivers, this driver will never
473 * deallocate TTYs. This means it's safe to temporarily unlock
474 * the TTY when handling ioctls.
477 error = tm->tm_class->tc_ioctl(tm, cmd, data, td);
479 if ((error == 0) && (cmd == CONS_CLRHIST)) {
481 * Scrollback history has been successfully cleared,
482 * so reset the cursor position to the top left of the screen.
487 teken_set_cursor(&tm->tm_emulator, &p);
493 termtty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t * paddr,
494 int nprot, vm_memattr_t *memattr)
496 struct terminal *tm = tty_softc(tp);
498 return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr));
502 * Binding with the kernel and debug console.
505 static cn_probe_t termcn_cnprobe;
506 static cn_init_t termcn_cninit;
507 static cn_term_t termcn_cnterm;
508 static cn_getc_t termcn_cngetc;
509 static cn_putc_t termcn_cnputc;
510 static cn_grab_t termcn_cngrab;
511 static cn_ungrab_t termcn_cnungrab;
513 const struct consdev_ops termcn_cnops = {
514 .cn_probe = termcn_cnprobe,
515 .cn_init = termcn_cninit,
516 .cn_term = termcn_cnterm,
517 .cn_getc = termcn_cngetc,
518 .cn_putc = termcn_cnputc,
519 .cn_grab = termcn_cngrab,
520 .cn_ungrab = termcn_cnungrab,
524 termcn_cnregister(struct terminal *tm)
530 cp = malloc(sizeof(struct consdev), M_TERMINAL,
532 cp->cn_ops = &termcn_cnops;
534 cp->cn_pri = CN_INTERNAL;
535 sprintf(cp->cn_name, "ttyv0");
537 tm->tm_flags = TF_CONS;
543 /* Attach terminal as console. */
548 termcn_cngrab(struct consdev *cp)
550 struct terminal *tm = cp->cn_arg;
552 tm->tm_class->tc_cngrab(tm);
556 termcn_cnungrab(struct consdev *cp)
558 struct terminal *tm = cp->cn_arg;
560 tm->tm_class->tc_cnungrab(tm);
564 termcn_cnprobe(struct consdev *cp)
566 struct terminal *tm = cp->cn_arg;
569 cp->cn_pri = CN_DEAD;
576 tm->tm_class->tc_cnprobe(tm, cp);
580 termcn_cninit(struct consdev *cp)
586 termcn_cnterm(struct consdev *cp)
592 termcn_cngetc(struct consdev *cp)
594 struct terminal *tm = cp->cn_arg;
596 return (tm->tm_class->tc_cngetc(tm));
600 termcn_cnputc(struct consdev *cp, int c)
602 struct terminal *tm = cp->cn_arg;
606 TERMINAL_LOCK_CONS(tm);
607 if (!(tm->tm_flags & TF_MUTE)) {
608 backup = *teken_get_curattr(&tm->tm_emulator);
609 teken_set_curattr(&tm->tm_emulator, &kernel_message);
610 teken_input(&tm->tm_emulator, &cv, 1);
611 teken_set_curattr(&tm->tm_emulator, &backup);
612 tm->tm_class->tc_done(tm);
614 TERMINAL_UNLOCK_CONS(tm);
618 * Binding with the terminal emulator.
622 termteken_bell(void *softc)
624 struct terminal *tm = softc;
626 tm->tm_flags |= TF_BELL;
630 termteken_cursor(void *softc, const teken_pos_t *p)
632 struct terminal *tm = softc;
634 tm->tm_class->tc_cursor(tm, p);
638 termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c,
639 const teken_attr_t *a)
641 struct terminal *tm = softc;
643 tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a));
647 termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c,
648 const teken_attr_t *a)
650 struct terminal *tm = softc;
652 tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a));
656 termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p)
658 struct terminal *tm = softc;
660 tm->tm_class->tc_copy(tm, r, p);
664 termteken_pre_input(void *softc)
666 struct terminal *tm = softc;
668 tm->tm_class->tc_pre_input(tm);
672 termteken_post_input(void *softc)
674 struct terminal *tm = softc;
676 tm->tm_class->tc_post_input(tm);
680 termteken_param(void *softc, int cmd, unsigned int arg)
682 struct terminal *tm = softc;
684 tm->tm_class->tc_param(tm, cmd, arg);
688 termteken_respond(void *softc, const void *buf, size_t len)
691 struct terminal *tm = softc;
695 * Only inject a response into the TTY if the data actually
696 * originated from the TTY.
698 * XXX: This cannot be done right now. The TTY could pick up
699 * other locks. It could also in theory cause loops, when the
700 * TTY performs echoing of a command that generates even more
707 ttydisc_rint_simple(tp, buf, len);
708 ttydisc_rint_done(tp);