2 * Copyright (c) 2009 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/consio.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <sys/terminal.h>
44 #include <machine/stdarg.h>
46 static MALLOC_DEFINE(M_TERMINAL, "terminal", "terminal device");
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.
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); \
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); \
69 #define TERMINAL_LOCK_TTY(tm) do { \
70 if ((tm)->tm_flags & TF_CONS) \
71 mtx_lock_spin(&(tm)->tm_mtx); \
73 #define TERMINAL_UNLOCK_TTY(tm) do { \
74 if ((tm)->tm_flags & TF_CONS) \
75 mtx_unlock_spin(&(tm)->tm_mtx); \
77 #define TERMINAL_LOCK_CONS(tm) mtx_lock_spin(&(tm)->tm_mtx)
78 #define TERMINAL_UNLOCK_CONS(tm) mtx_unlock_spin(&(tm)->tm_mtx)
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;
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,
99 * Terminal emulator routines.
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;
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,
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)
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)
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)))
138 terminal_init(struct terminal *tm)
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);
148 terminal_alloc(const struct terminal_class *tc, void *softc)
152 tm = malloc(sizeof(struct terminal), M_TERMINAL, M_WAITOK|M_ZERO);
156 tm->tm_softc = softc;
162 terminal_sync_ttysize(struct terminal *tm)
171 tty_set_winsize(tp, &tm->tm_winsize);
176 terminal_maketty(struct terminal *tm, const char *fmt, ...)
183 vsnrprintf(name, sizeof name, 32, fmt, ap);
186 tp = tty_alloc(&terminal_tty_class, tm);
187 tty_makedev(tp, NULL, "%s", name);
189 terminal_sync_ttysize(tm);
193 terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size,
194 int blank, const term_attr_t *attr)
198 tm->tm_winsize = *size;
200 r.tr_begin.tp_row = r.tr_begin.tp_col = 0;
201 r.tr_end.tp_row = size->ws_row;
202 r.tr_end.tp_col = size->ws_col;
206 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end);
208 teken_set_winsize(&tm->tm_emulator, &r.tr_end);
211 if ((blank != 0) && !(tm->tm_flags & TF_MUTE))
212 tm->tm_class->tc_fill(tm, &r,
213 TCHAR_CREATE((teken_char_t)' ', attr));
215 terminal_sync_ttysize(tm);
219 terminal_set_winsize(struct terminal *tm, const struct winsize *size)
222 terminal_set_winsize_blank(tm, size, 1,
223 (const term_attr_t *)&default_message);
227 * XXX: This function is a kludge. Drivers like vt(4) need to
228 * temporarily stop input when resizing, etc. This should ideally be
229 * handled within the driver.
233 terminal_mute(struct terminal *tm, int yes)
238 tm->tm_flags |= TF_MUTE;
240 tm->tm_flags &= ~TF_MUTE;
245 terminal_input_char(struct terminal *tm, term_char_t c)
254 * Strip off any attributes. Also ignore input of second part of
255 * CJK fullwidth characters, as we don't want to return these
258 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT)
260 c = TCHAR_CHARACTER(c);
264 * Conversion to UTF-8.
267 ttydisc_rint(tp, c, 0);
268 } else if (c < 0x800) {
274 ttydisc_rint_simple(tp, str, sizeof str);
275 } else if (c < 0x10000) {
278 0x80 | ((c >> 6) & 0x3f),
282 ttydisc_rint_simple(tp, str, sizeof str);
286 0x80 | ((c >> 12) & 0x3f),
287 0x80 | ((c >> 6) & 0x3f),
291 ttydisc_rint_simple(tp, str, sizeof str);
293 ttydisc_rint_done(tp);
298 terminal_input_raw(struct terminal *tm, char c)
307 ttydisc_rint(tp, c, 0);
308 ttydisc_rint_done(tp);
313 terminal_input_special(struct terminal *tm, unsigned int k)
322 str = teken_get_sequence(&tm->tm_emulator, k);
327 ttydisc_rint_simple(tp, str, strlen(str));
328 ttydisc_rint_done(tp);
333 * Binding with the TTY layer.
337 termtty_open(struct tty *tp)
339 struct terminal *tm = tty_softc(tp);
341 tm->tm_class->tc_opened(tm, 1);
346 termtty_close(struct tty *tp)
348 struct terminal *tm = tty_softc(tp);
350 tm->tm_class->tc_opened(tm, 0);
354 termtty_outwakeup(struct tty *tp)
356 struct terminal *tm = tty_softc(tp);
359 unsigned int flags = 0;
361 while ((olen = ttydisc_getc(tp, obuf, sizeof obuf)) > 0) {
362 TERMINAL_LOCK_TTY(tm);
363 if (!(tm->tm_flags & TF_MUTE)) {
364 tm->tm_flags &= ~TF_BELL;
365 teken_input(&tm->tm_emulator, obuf, olen);
366 flags |= tm->tm_flags;
368 TERMINAL_UNLOCK_TTY(tm);
371 tm->tm_class->tc_done(tm);
373 tm->tm_class->tc_bell(tm);
377 termtty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
379 struct terminal *tm = tty_softc(tp);
384 vid_info_t *vi = (vid_info_t *)data;
385 const teken_pos_t *p;
388 if (vi->size != sizeof(vid_info_t))
391 /* Already help the console driver by filling in some data. */
392 p = teken_get_cursor(&tm->tm_emulator);
393 vi->mv_row = p->tp_row;
394 vi->mv_col = p->tp_col;
396 p = teken_get_winsize(&tm->tm_emulator);
397 vi->mv_rsz = p->tp_row;
398 vi->mv_csz = p->tp_col;
400 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg);
401 vi->mv_norm.fore = fg;
402 vi->mv_norm.back = bg;
403 /* XXX: keep vidcontrol happy; bold backgrounds. */
404 vi->mv_rev.fore = bg;
405 vi->mv_rev.back = fg & 0x7;
411 * Unlike various other drivers, this driver will never
412 * deallocate TTYs. This means it's safe to temporarily unlock
413 * the TTY when handling ioctls.
416 error = tm->tm_class->tc_ioctl(tm, cmd, data, td);
422 termtty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t * paddr,
423 int nprot, vm_memattr_t *memattr)
425 struct terminal *tm = tty_softc(tp);
427 return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr));
431 * Binding with the kernel and debug console.
434 static cn_probe_t termcn_cnprobe;
435 static cn_init_t termcn_cninit;
436 static cn_term_t termcn_cnterm;
437 static cn_getc_t termcn_cngetc;
438 static cn_putc_t termcn_cnputc;
439 static cn_grab_t termcn_cngrab;
440 static cn_ungrab_t termcn_cnungrab;
442 const struct consdev_ops termcn_cnops = {
443 .cn_probe = termcn_cnprobe,
444 .cn_init = termcn_cninit,
445 .cn_term = termcn_cnterm,
446 .cn_getc = termcn_cngetc,
447 .cn_putc = termcn_cnputc,
448 .cn_grab = termcn_cngrab,
449 .cn_ungrab = termcn_cnungrab,
453 termcn_cnregister(struct terminal *tm)
459 cp = malloc(sizeof(struct consdev), M_TERMINAL,
461 cp->cn_ops = &termcn_cnops;
463 cp->cn_pri = CN_INTERNAL;
464 sprintf(cp->cn_name, "ttyv0");
466 tm->tm_flags = TF_CONS;
472 /* Attach terminal as console. */
477 termcn_cngrab(struct consdev *cp)
479 struct terminal *tm = cp->cn_arg;
481 tm->tm_class->tc_cngrab(tm);
485 termcn_cnungrab(struct consdev *cp)
487 struct terminal *tm = cp->cn_arg;
489 tm->tm_class->tc_cnungrab(tm);
493 termcn_cnprobe(struct consdev *cp)
495 struct terminal *tm = cp->cn_arg;
498 cp->cn_pri = CN_DEAD;
505 tm->tm_class->tc_cnprobe(tm, cp);
509 termcn_cninit(struct consdev *cp)
515 termcn_cnterm(struct consdev *cp)
521 termcn_cngetc(struct consdev *cp)
523 struct terminal *tm = cp->cn_arg;
525 return (tm->tm_class->tc_cngetc(tm));
529 termcn_cnputc(struct consdev *cp, int c)
531 struct terminal *tm = cp->cn_arg;
535 TERMINAL_LOCK_CONS(tm);
536 if (!(tm->tm_flags & TF_MUTE)) {
537 backup = *teken_get_curattr(&tm->tm_emulator);
538 teken_set_curattr(&tm->tm_emulator, &kernel_message);
539 teken_input(&tm->tm_emulator, &cv, 1);
540 teken_set_curattr(&tm->tm_emulator, &backup);
542 TERMINAL_UNLOCK_CONS(tm);
544 tm->tm_class->tc_done(tm);
548 * Binding with the terminal emulator.
552 termteken_bell(void *softc)
554 struct terminal *tm = softc;
556 tm->tm_flags |= TF_BELL;
560 termteken_cursor(void *softc, const teken_pos_t *p)
562 struct terminal *tm = softc;
564 tm->tm_class->tc_cursor(tm, p);
568 termteken_putchar(void *softc, const teken_pos_t *p, teken_char_t c,
569 const teken_attr_t *a)
571 struct terminal *tm = softc;
573 tm->tm_class->tc_putchar(tm, p, TCHAR_CREATE(c, a));
577 termteken_fill(void *softc, const teken_rect_t *r, teken_char_t c,
578 const teken_attr_t *a)
580 struct terminal *tm = softc;
582 tm->tm_class->tc_fill(tm, r, TCHAR_CREATE(c, a));
586 termteken_copy(void *softc, const teken_rect_t *r, const teken_pos_t *p)
588 struct terminal *tm = softc;
590 tm->tm_class->tc_copy(tm, r, p);
594 termteken_param(void *softc, int cmd, unsigned int arg)
596 struct terminal *tm = softc;
598 tm->tm_class->tc_param(tm, cmd, arg);
602 termteken_respond(void *softc, const void *buf, size_t len)
605 struct terminal *tm = softc;
609 * Only inject a response into the TTY if the data actually
610 * originated from the TTY.
612 * XXX: This cannot be done right now. The TTY could pick up
613 * other locks. It could also in theory cause loops, when the
614 * TTY performs echoing of a command that generates even more
621 ttydisc_rint_simple(tp, buf, len);
622 ttydisc_rint_done(tp);