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