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