xref: /freebsd/sys/dev/vt/vt_core.c (revision bc96366c864c07ef352edb92017357917c75b36c)
1 /*-
2  * Copyright (c) 2009, 2013 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  * Portions of this software were developed by Oleksandr Rybalko
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_compat.h"
37 
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
41 #include <sys/fbio.h>
42 #include <sys/kbio.h>
43 #include <sys/kdb.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/power.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/systm.h>
53 #include <sys/terminal.h>
54 
55 #include <dev/kbd/kbdreg.h>
56 #include <dev/vt/vt.h>
57 
58 #if defined(__i386__) || defined(__amd64__)
59 #include <machine/psl.h>
60 #include <machine/frame.h>
61 #endif
62 
63 static tc_bell_t	vtterm_bell;
64 static tc_cursor_t	vtterm_cursor;
65 static tc_putchar_t	vtterm_putchar;
66 static tc_fill_t	vtterm_fill;
67 static tc_copy_t	vtterm_copy;
68 static tc_param_t	vtterm_param;
69 static tc_done_t	vtterm_done;
70 
71 static tc_cnprobe_t	vtterm_cnprobe;
72 static tc_cngetc_t	vtterm_cngetc;
73 
74 static tc_cngrab_t	vtterm_cngrab;
75 static tc_cnungrab_t	vtterm_cnungrab;
76 
77 static tc_opened_t	vtterm_opened;
78 static tc_ioctl_t	vtterm_ioctl;
79 static tc_mmap_t	vtterm_mmap;
80 
81 const struct terminal_class vt_termclass = {
82 	.tc_bell	= vtterm_bell,
83 	.tc_cursor	= vtterm_cursor,
84 	.tc_putchar	= vtterm_putchar,
85 	.tc_fill	= vtterm_fill,
86 	.tc_copy	= vtterm_copy,
87 	.tc_param	= vtterm_param,
88 	.tc_done	= vtterm_done,
89 
90 	.tc_cnprobe	= vtterm_cnprobe,
91 	.tc_cngetc	= vtterm_cngetc,
92 
93 	.tc_cngrab	= vtterm_cngrab,
94 	.tc_cnungrab	= vtterm_cnungrab,
95 
96 	.tc_opened	= vtterm_opened,
97 	.tc_ioctl	= vtterm_ioctl,
98 	.tc_mmap	= vtterm_mmap,
99 };
100 
101 /*
102  * Use a constant timer of 25 Hz to redraw the screen.
103  *
104  * XXX: In theory we should only fire up the timer when there is really
105  * activity. Unfortunately we cannot always start timers. We really
106  * don't want to process kernel messages synchronously, because it
107  * really slows down the system.
108  */
109 #define	VT_TIMERFREQ	25
110 
111 /* Bell pitch/duration. */
112 #define VT_BELLDURATION	((5 * hz + 99) / 100)
113 #define VT_BELLPITCH	800
114 
115 #define	VT_LOCK(vd)	mtx_lock(&(vd)->vd_lock)
116 #define	VT_UNLOCK(vd)	mtx_unlock(&(vd)->vd_lock)
117 #define	VT_LOCK_ASSERT(vd, what)	mtx_assert(&(vd)->vd_lock, what)
118 
119 #define	VT_UNIT(vw)	((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
120 			(vw)->vw_number)
121 
122 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
123 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
124 VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
125 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
126 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
127 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
128 
129 /* Allow to disable some keyboard combinations. */
130 VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination.  "
131     "See kbdmap(5) to configure.");
132 VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination.  "
133     "See kbdmap(5) to configure.");
134 VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination.  "
135     "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
136 VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger.  "
137     "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
138 VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic.  "
139     "See kbdmap(5) to configure.");
140 
141 static struct vt_device	vt_consdev;
142 static unsigned int vt_unit = 0;
143 static MALLOC_DEFINE(M_VT, "vt", "vt device");
144 struct vt_device *main_vd = &vt_consdev;
145 
146 /* Boot logo. */
147 extern unsigned int vt_logo_width;
148 extern unsigned int vt_logo_height;
149 extern unsigned int vt_logo_depth;
150 extern unsigned char vt_logo_image[];
151 
152 /* Font. */
153 extern struct vt_font vt_font_default;
154 #ifndef SC_NO_CUTPASTE
155 extern struct vt_mouse_cursor vt_default_mouse_pointer;
156 #endif
157 
158 static int signal_vt_rel(struct vt_window *);
159 static int signal_vt_acq(struct vt_window *);
160 static int finish_vt_rel(struct vt_window *, int, int *);
161 static int finish_vt_acq(struct vt_window *);
162 static int vt_window_switch(struct vt_window *);
163 static int vt_late_window_switch(struct vt_window *);
164 static int vt_proc_alive(struct vt_window *);
165 static void vt_resize(struct vt_device *);
166 static void vt_update_static(void *);
167 #ifndef SC_NO_CUTPASTE
168 static void vt_mouse_paste(void);
169 #endif
170 
171 SET_DECLARE(vt_drv_set, struct vt_driver);
172 
173 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
174 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
175 
176 static struct terminal	vt_consterm;
177 static struct vt_window	vt_conswindow;
178 static struct vt_device	vt_consdev = {
179 	.vd_driver = NULL,
180 	.vd_softc = NULL,
181 	.vd_flags = VDF_INVALID,
182 	.vd_windows = { [VT_CONSWINDOW] =  &vt_conswindow, },
183 	.vd_curwindow = &vt_conswindow,
184 	.vd_kbstate = 0,
185 
186 #ifndef SC_NO_CUTPASTE
187 	.vd_pastebuf = {
188 		.vpb_buf = NULL,
189 		.vpb_bufsz = 0,
190 		.vpb_len = 0
191 	},
192 	.vd_mcursor = &vt_default_mouse_pointer,
193 	.vd_mcursor_fg = TC_WHITE,
194 	.vd_mcursor_bg = TC_BLACK,
195 #endif
196 };
197 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
198 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
199 static struct vt_window	vt_conswindow = {
200 	.vw_number = VT_CONSWINDOW,
201 	.vw_flags = VWF_CONSOLE,
202 	.vw_buf = {
203 		.vb_buffer = &vt_constextbuf[0],
204 		.vb_rows = &vt_constextbufrows[0],
205 		.vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
206 		.vb_curroffset = 0,
207 		.vb_roffset = 0,
208 		.vb_flags = VBF_STATIC,
209 		.vb_mark_start = {.tp_row = 0, .tp_col = 0,},
210 		.vb_mark_end = {.tp_row = 0, .tp_col = 0,},
211 		.vb_scr_size = {
212 			.tp_row = _VTDEFH,
213 			.tp_col = _VTDEFW,
214 		},
215 	},
216 	.vw_device = &vt_consdev,
217 	.vw_terminal = &vt_consterm,
218 	.vw_kbdmode = K_XLATE,
219 	.vw_grabbed = 0,
220 };
221 static struct terminal vt_consterm = {
222 	.tm_class = &vt_termclass,
223 	.tm_softc = &vt_conswindow,
224 	.tm_flags = TF_CONS,
225 };
226 static struct consdev vt_consterm_consdev = {
227 	.cn_ops = &termcn_cnops,
228 	.cn_arg = &vt_consterm,
229 	.cn_name = "ttyv0",
230 };
231 
232 /* Add to set of consoles. */
233 DATA_SET(cons_set, vt_consterm_consdev);
234 
235 /*
236  * Right after kmem is done to allow early drivers to use locking and allocate
237  * memory.
238  */
239 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
240     &vt_consdev);
241 /* Delay until all devices attached, to not waste time. */
242 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
243     &vt_consdev);
244 
245 /* Initialize locks/mem depended members. */
246 static void
247 vt_update_static(void *dummy)
248 {
249 
250 	if (!vty_enabled(VTY_VT))
251 		return;
252 	if (main_vd->vd_driver != NULL)
253 		printf("VT: running with driver \"%s\".\n",
254 		    main_vd->vd_driver->vd_name);
255 	else
256 		printf("VT: init without driver.\n");
257 
258 	mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
259 	cv_init(&main_vd->vd_winswitch, "vtwswt");
260 }
261 
262 static void
263 vt_schedule_flush(struct vt_device *vd, int ms)
264 {
265 
266 	if (ms <= 0)
267 		/* Default to initial value. */
268 		ms = 1000 / VT_TIMERFREQ;
269 
270 	callout_schedule(&vd->vd_timer, hz / (1000 / ms));
271 }
272 
273 static void
274 vt_resume_flush_timer(struct vt_device *vd, int ms)
275 {
276 
277 	if (!(vd->vd_flags & VDF_ASYNC) ||
278 	    !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
279 		return;
280 
281 	vt_schedule_flush(vd, ms);
282 }
283 
284 static void
285 vt_suspend_flush_timer(struct vt_device *vd)
286 {
287 	/*
288 	 * As long as this function is called locked, callout_stop()
289 	 * has the same effect like callout_drain() with regard to
290 	 * preventing the callback function from executing.
291 	 */
292 	VT_LOCK_ASSERT(vd, MA_OWNED);
293 
294 	if (!(vd->vd_flags & VDF_ASYNC) ||
295 	    !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
296 		return;
297 
298 	callout_stop(&vd->vd_timer);
299 }
300 
301 static void
302 vt_switch_timer(void *arg)
303 {
304 
305 	vt_late_window_switch((struct vt_window *)arg);
306 }
307 
308 static int
309 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
310 {
311 	int mode, ret;
312 
313 	mode = 0;
314 	ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
315 	if (ret == ENOIOCTL)
316 		ret = ENODEV;
317 	if (ret != 0)
318 		return (ret);
319 
320 	vw->vw_kbdmode = mode;
321 
322 	return (0);
323 }
324 
325 static int
326 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
327 {
328 	int ret;
329 
330 	ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
331 	if (ret == ENOIOCTL)
332 		ret = ENODEV;
333 
334 	return (ret);
335 }
336 
337 static int
338 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
339 {
340 	int state, ret;
341 
342 	state = 0;
343 	ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
344 	if (ret == ENOIOCTL)
345 		ret = ENODEV;
346 	if (ret != 0)
347 		return (ret);
348 
349 	vw->vw_kbdstate &= ~LOCK_MASK;
350 	vw->vw_kbdstate |= state & LOCK_MASK;
351 
352 	return (0);
353 }
354 
355 static int
356 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
357 {
358 	int state, ret;
359 
360 	state = vw->vw_kbdstate & LOCK_MASK;
361 	ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
362 	if (ret == ENOIOCTL)
363 		ret = ENODEV;
364 
365 	return (ret);
366 }
367 
368 static int
369 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
370 {
371 	int leds, ret;
372 
373 	leds = 0;
374 	ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
375 	if (ret == ENOIOCTL)
376 		ret = ENODEV;
377 	if (ret != 0)
378 		return (ret);
379 
380 	vw->vw_kbdstate &= ~LED_MASK;
381 	vw->vw_kbdstate |= leds & LED_MASK;
382 
383 	return (0);
384 }
385 
386 static int
387 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
388 {
389 	int leds, ret;
390 
391 	leds = vw->vw_kbdstate & LED_MASK;
392 	ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
393 	if (ret == ENOIOCTL)
394 		ret = ENODEV;
395 
396 	return (ret);
397 }
398 
399 static int
400 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
401 {
402 
403 	DPRINTF(40, "%s\n", __func__);
404 	curvw->vw_switch_to = vw;
405 	/* Set timer to allow switch in case when process hang. */
406 	callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
407 	    vt_switch_timer, (void *)vw);
408 	/* Notify process about vt switch attempt. */
409 	DPRINTF(30, "%s: Notify process.\n", __func__);
410 	signal_vt_rel(curvw);
411 
412 	return (0);
413 }
414 
415 static int
416 vt_window_postswitch(struct vt_window *vw)
417 {
418 
419 	signal_vt_acq(vw);
420 	return (0);
421 }
422 
423 /* vt_late_window_switch will done VT switching for regular case. */
424 static int
425 vt_late_window_switch(struct vt_window *vw)
426 {
427 	int ret;
428 
429 	callout_stop(&vw->vw_proc_dead_timer);
430 
431 	ret = vt_window_switch(vw);
432 	if (ret)
433 		return (ret);
434 
435 	/* Notify owner process about terminal availability. */
436 	if (vw->vw_smode.mode == VT_PROCESS) {
437 		ret = vt_window_postswitch(vw);
438 	}
439 	return (ret);
440 }
441 
442 /* Switch window. */
443 static int
444 vt_proc_window_switch(struct vt_window *vw)
445 {
446 	struct vt_window *curvw;
447 	struct vt_device *vd;
448 	int ret;
449 
450 	vd = vw->vw_device;
451 	curvw = vd->vd_curwindow;
452 
453 	if (curvw->vw_flags & VWF_VTYLOCK)
454 		return (EBUSY);
455 
456 	/* Ask current process permission to switch away. */
457 	if (curvw->vw_smode.mode == VT_PROCESS) {
458 		DPRINTF(30, "%s: VT_PROCESS ", __func__);
459 		if (vt_proc_alive(curvw) == FALSE) {
460 			DPRINTF(30, "Dead. Cleaning.");
461 			/* Dead */
462 		} else {
463 			DPRINTF(30, "%s: Signaling process.\n", __func__);
464 			/* Alive, try to ask him. */
465 			ret = vt_window_preswitch(vw, curvw);
466 			/* Wait for process answer or timeout. */
467 			return (ret);
468 		}
469 		DPRINTF(30, "\n");
470 	}
471 
472 	ret = vt_late_window_switch(vw);
473 	return (ret);
474 }
475 
476 /* Switch window ignoring process locking. */
477 static int
478 vt_window_switch(struct vt_window *vw)
479 {
480 	struct vt_device *vd = vw->vw_device;
481 	struct vt_window *curvw = vd->vd_curwindow;
482 	keyboard_t *kbd;
483 
484 	VT_LOCK(vd);
485 	if (curvw == vw) {
486 		/* Nothing to do. */
487 		VT_UNLOCK(vd);
488 		return (0);
489 	}
490 	if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
491 		VT_UNLOCK(vd);
492 		return (EINVAL);
493 	}
494 
495 	vt_suspend_flush_timer(vd);
496 
497 	vd->vd_curwindow = vw;
498 	vd->vd_flags |= VDF_INVALID;
499 	cv_broadcast(&vd->vd_winswitch);
500 	VT_UNLOCK(vd);
501 
502 	if (vd->vd_driver->vd_postswitch)
503 		vd->vd_driver->vd_postswitch(vd);
504 
505 	vt_resume_flush_timer(vd, 0);
506 
507 	/* Restore per-window keyboard mode. */
508 	mtx_lock(&Giant);
509 	kbd = kbd_get_keyboard(vd->vd_keyboard);
510 	if (kbd != NULL) {
511 		if (curvw->vw_kbdmode == K_XLATE)
512 			vt_save_kbd_state(curvw, kbd);
513 
514 		vt_update_kbd_mode(vw, kbd);
515 		vt_update_kbd_state(vw, kbd);
516 	}
517 	mtx_unlock(&Giant);
518 	DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
519 
520 	return (0);
521 }
522 
523 static inline void
524 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
525 {
526 
527 	size->tp_row = vd->vd_height;
528 	size->tp_col = vd->vd_width;
529 	if (vf != NULL) {
530 		size->tp_row /= vf->vf_height;
531 		size->tp_col /= vf->vf_width;
532 	}
533 }
534 
535 static inline void
536 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
537 {
538 
539 	size->ws_row = size->ws_ypixel = vd->vd_height;
540 	size->ws_col = size->ws_xpixel = vd->vd_width;
541 	if (vf != NULL) {
542 		size->ws_row /= vf->vf_height;
543 		size->ws_col /= vf->vf_width;
544 	}
545 }
546 
547 static inline void
548 vt_compute_drawable_area(struct vt_window *vw)
549 {
550 	struct vt_device *vd;
551 	struct vt_font *vf;
552 
553 	vd = vw->vw_device;
554 
555 	if (vw->vw_font == NULL) {
556 		vw->vw_draw_area.tr_begin.tp_col = 0;
557 		vw->vw_draw_area.tr_begin.tp_row = 0;
558 		vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
559 		vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
560 		return;
561 	}
562 
563 	vf = vw->vw_font;
564 
565 	/*
566 	 * Compute the drawable area, so that the text is centered on
567 	 * the screen.
568 	 */
569 
570 	vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
571 	vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
572 	vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
573 	    vd->vd_width / vf->vf_width * vf->vf_width;
574 	vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
575 	    vd->vd_height / vf->vf_height * vf->vf_height;
576 }
577 
578 static void
579 vt_scroll(struct vt_window *vw, int offset, int whence)
580 {
581 	int diff;
582 	term_pos_t size;
583 
584 	if ((vw->vw_flags & VWF_SCROLL) == 0)
585 		return;
586 
587 	vt_termsize(vw->vw_device, vw->vw_font, &size);
588 
589 	diff = vthistory_seek(&vw->vw_buf, offset, whence);
590 	if (diff)
591 		vw->vw_device->vd_flags |= VDF_INVALID;
592 	vt_resume_flush_timer(vw->vw_device, 0);
593 }
594 
595 static int
596 vt_machine_kbdevent(int c)
597 {
598 
599 	switch (c) {
600 	case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
601 		if (vt_kbd_debug)
602 			kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
603 		return (1);
604 	case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
605 		if (vt_kbd_halt)
606 			shutdown_nice(RB_HALT);
607 		return (1);
608 	case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
609 #ifndef SC_NO_CUTPASTE
610 		/* Insert text from cut-paste buffer. */
611 		vt_mouse_paste();
612 #endif
613 		break;
614 	case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
615 		if (vt_kbd_poweroff)
616 			shutdown_nice(RB_HALT|RB_POWEROFF);
617 		return (1);
618 	case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
619 		/*
620 		 * Request to immediate panic if sysctl
621 		 * kern.vt.enable_panic_key allow it.
622 		 */
623 		if (vt_kbd_panic)
624 			panic("Forced by the panic key");
625 		return (1);
626 	case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
627 		if (vt_kbd_reboot)
628 			shutdown_nice(RB_AUTOBOOT);
629 		return (1);
630 	case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
631 		/* Force activatation/deactivation of the screen saver. */
632 		/* TODO */
633 		return (1);
634 	case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
635 		/* Put machine into Stand-By mode. */
636 		power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
637 		return (1);
638 	case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
639 		/* Suspend machine. */
640 		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
641 		return (1);
642 	};
643 
644 	return (0);
645 }
646 
647 static void
648 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
649 {
650 	struct vt_device *vd;
651 	term_pos_t size;
652 
653 	vd = vw->vw_device;
654 	/* Only special keys handled in ScrollLock mode */
655 	if ((c & SPCLKEY) == 0)
656 		return;
657 
658 	c &= ~SPCLKEY;
659 
660 	if (console == 0) {
661 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
662 			vw = vd->vd_windows[c - F_SCR];
663 			if (vw != NULL)
664 				vt_proc_window_switch(vw);
665 			return;
666 		}
667 		VT_LOCK(vd);
668 	}
669 
670 	switch (c) {
671 	case SLK: {
672 		/* Turn scrolling off. */
673 		vt_scroll(vw, 0, VHS_END);
674 		VTBUF_SLCK_DISABLE(&vw->vw_buf);
675 		vw->vw_flags &= ~VWF_SCROLL;
676 		break;
677 	}
678 	case FKEY | F(49): /* Home key. */
679 		vt_scroll(vw, 0, VHS_SET);
680 		break;
681 	case FKEY | F(50): /* Arrow up. */
682 		vt_scroll(vw, -1, VHS_CUR);
683 		break;
684 	case FKEY | F(51): /* Page up. */
685 		vt_termsize(vd, vw->vw_font, &size);
686 		vt_scroll(vw, -size.tp_row, VHS_CUR);
687 		break;
688 	case FKEY | F(57): /* End key. */
689 		vt_scroll(vw, 0, VHS_END);
690 		break;
691 	case FKEY | F(58): /* Arrow down. */
692 		vt_scroll(vw, 1, VHS_CUR);
693 		break;
694 	case FKEY | F(59): /* Page down. */
695 		vt_termsize(vd, vw->vw_font, &size);
696 		vt_scroll(vw, size.tp_row, VHS_CUR);
697 		break;
698 	}
699 
700 	if (console == 0)
701 		VT_UNLOCK(vd);
702 }
703 
704 static int
705 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
706 {
707 	struct vt_window *vw = vd->vd_curwindow;
708 
709 #if VT_ALT_TO_ESC_HACK
710 	if (c & RELKEY) {
711 		switch (c & ~RELKEY) {
712 		case (SPCLKEY | RALT):
713 			if (vt_enable_altgr != 0)
714 				break;
715 		case (SPCLKEY | LALT):
716 			vd->vd_kbstate &= ~ALKED;
717 		}
718 		/* Other keys ignored for RELKEY event. */
719 		return (0);
720 	} else {
721 		switch (c & ~RELKEY) {
722 		case (SPCLKEY | RALT):
723 			if (vt_enable_altgr != 0)
724 				break;
725 		case (SPCLKEY | LALT):
726 			vd->vd_kbstate |= ALKED;
727 		}
728 	}
729 #else
730 	if (c & RELKEY)
731 		/* Other keys ignored for RELKEY event. */
732 		return (0);
733 #endif
734 
735 	if (vt_machine_kbdevent(c))
736 		return (0);
737 
738 	if (vw->vw_flags & VWF_SCROLL) {
739 		vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
740 		/* Scroll mode keys handled, nothing to do more. */
741 		return (0);
742 	}
743 
744 	if (c & SPCLKEY) {
745 		c &= ~SPCLKEY;
746 
747 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
748 			vw = vd->vd_windows[c - F_SCR];
749 			if (vw != NULL)
750 				vt_proc_window_switch(vw);
751 			return (0);
752 		}
753 
754 		switch (c) {
755 		case NEXT:
756 			/* Switch to next VT. */
757 			c = (vw->vw_number + 1) % VT_MAXWINDOWS;
758 			vw = vd->vd_windows[c];
759 			if (vw != NULL)
760 				vt_proc_window_switch(vw);
761 			return (0);
762 		case PREV:
763 			/* Switch to previous VT. */
764 			c = (vw->vw_number - 1) % VT_MAXWINDOWS;
765 			vw = vd->vd_windows[c];
766 			if (vw != NULL)
767 				vt_proc_window_switch(vw);
768 			return (0);
769 		case SLK: {
770 			vt_save_kbd_state(vw, kbd);
771 			VT_LOCK(vd);
772 			if (vw->vw_kbdstate & SLKED) {
773 				/* Turn scrolling on. */
774 				vw->vw_flags |= VWF_SCROLL;
775 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
776 			} else {
777 				/* Turn scrolling off. */
778 				vw->vw_flags &= ~VWF_SCROLL;
779 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
780 				vt_scroll(vw, 0, VHS_END);
781 			}
782 			VT_UNLOCK(vd);
783 			break;
784 		}
785 		case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
786 		case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
787 		case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
788 		case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
789 			/* F1 through F12 keys. */
790 			terminal_input_special(vw->vw_terminal,
791 			    TKEY_F1 + c - (FKEY | F(1)));
792 			break;
793 		case FKEY | F(49): /* Home key. */
794 			terminal_input_special(vw->vw_terminal, TKEY_HOME);
795 			break;
796 		case FKEY | F(50): /* Arrow up. */
797 			terminal_input_special(vw->vw_terminal, TKEY_UP);
798 			break;
799 		case FKEY | F(51): /* Page up. */
800 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
801 			break;
802 		case FKEY | F(53): /* Arrow left. */
803 			terminal_input_special(vw->vw_terminal, TKEY_LEFT);
804 			break;
805 		case FKEY | F(55): /* Arrow right. */
806 			terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
807 			break;
808 		case FKEY | F(57): /* End key. */
809 			terminal_input_special(vw->vw_terminal, TKEY_END);
810 			break;
811 		case FKEY | F(58): /* Arrow down. */
812 			terminal_input_special(vw->vw_terminal, TKEY_DOWN);
813 			break;
814 		case FKEY | F(59): /* Page down. */
815 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
816 			break;
817 		case FKEY | F(60): /* Insert key. */
818 			terminal_input_special(vw->vw_terminal, TKEY_INSERT);
819 			break;
820 		case FKEY | F(61): /* Delete key. */
821 			terminal_input_special(vw->vw_terminal, TKEY_DELETE);
822 			break;
823 		}
824 	} else if (KEYFLAGS(c) == 0) {
825 		/* Don't do UTF-8 conversion when doing raw mode. */
826 		if (vw->vw_kbdmode == K_XLATE) {
827 #if VT_ALT_TO_ESC_HACK
828 			if (vd->vd_kbstate & ALKED) {
829 				/*
830 				 * Prepend ESC sequence if one of ALT keys down.
831 				 */
832 				terminal_input_char(vw->vw_terminal, 0x1b);
833 			}
834 #endif
835 #if defined(KDB)
836 			kdb_alt_break(c, &vd->vd_altbrk);
837 #endif
838 			terminal_input_char(vw->vw_terminal, KEYCHAR(c));
839 		} else
840 			terminal_input_raw(vw->vw_terminal, c);
841 	}
842 	return (0);
843 }
844 
845 static int
846 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
847 {
848 	struct vt_device *vd = arg;
849 	int c;
850 
851 	switch (event) {
852 	case KBDIO_KEYINPUT:
853 		break;
854 	case KBDIO_UNLOADING:
855 		mtx_lock(&Giant);
856 		vd->vd_keyboard = -1;
857 		kbd_release(kbd, (void *)vd);
858 		mtx_unlock(&Giant);
859 		return (0);
860 	default:
861 		return (EINVAL);
862 	}
863 
864 	while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
865 		vt_processkey(kbd, vd, c);
866 
867 	return (0);
868 }
869 
870 static int
871 vt_allocate_keyboard(struct vt_device *vd)
872 {
873 	int		 idx0, idx;
874 	keyboard_t	*k0, *k;
875 	keyboard_info_t	 ki;
876 
877 	idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
878 	if (idx0 >= 0) {
879 		DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
880 		k0 = kbd_get_keyboard(idx0);
881 
882 		for (idx = kbd_find_keyboard2("*", -1, 0);
883 		     idx != -1;
884 		     idx = kbd_find_keyboard2("*", -1, idx + 1)) {
885 			k = kbd_get_keyboard(idx);
886 
887 			if (idx == idx0 || KBD_IS_BUSY(k))
888 				continue;
889 
890 			bzero(&ki, sizeof(ki));
891 			strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
892 			ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
893 			ki.kb_unit = k->kb_unit;
894 
895 			kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
896 		}
897 	} else {
898 		DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
899 		idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
900 		if (idx0 < 0) {
901 			DPRINTF(10, "%s: No keyboard found.\n", __func__);
902 			return (-1);
903 		}
904 	}
905 	vd->vd_keyboard = idx0;
906 	DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
907 
908 	return (idx0);
909 }
910 
911 static void
912 vtterm_bell(struct terminal *tm)
913 {
914 	struct vt_window *vw = tm->tm_softc;
915 	struct vt_device *vd = vw->vw_device;
916 
917 	if (!vt_enable_bell)
918 		return;
919 
920 	if (vd->vd_flags & VDF_QUIET_BELL)
921 		return;
922 
923 	sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
924 }
925 
926 static void
927 vtterm_beep(struct terminal *tm, u_int param)
928 {
929 	u_int freq, period;
930 
931 	if (!vt_enable_bell)
932 		return;
933 
934 	if ((param == 0) || ((param & 0xffff) == 0)) {
935 		vtterm_bell(tm);
936 		return;
937 	}
938 
939 	period = ((param >> 16) & 0xffff) * hz / 1000;
940 	freq = 1193182 / (param & 0xffff);
941 
942 	sysbeep(freq, period);
943 }
944 
945 static void
946 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
947 {
948 	struct vt_window *vw = tm->tm_softc;
949 
950 	vtbuf_cursor_position(&vw->vw_buf, p);
951 	vt_resume_flush_timer(vw->vw_device, 0);
952 }
953 
954 static void
955 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
956 {
957 	struct vt_window *vw = tm->tm_softc;
958 
959 	vtbuf_putchar(&vw->vw_buf, p, c);
960 	vt_resume_flush_timer(vw->vw_device, 0);
961 }
962 
963 static void
964 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
965 {
966 	struct vt_window *vw = tm->tm_softc;
967 
968 	vtbuf_fill_locked(&vw->vw_buf, r, c);
969 	vt_resume_flush_timer(vw->vw_device, 0);
970 }
971 
972 static void
973 vtterm_copy(struct terminal *tm, const term_rect_t *r,
974     const term_pos_t *p)
975 {
976 	struct vt_window *vw = tm->tm_softc;
977 
978 	vtbuf_copy(&vw->vw_buf, r, p);
979 	vt_resume_flush_timer(vw->vw_device, 0);
980 }
981 
982 static void
983 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
984 {
985 	struct vt_window *vw = tm->tm_softc;
986 
987 	switch (cmd) {
988 	case TP_SHOWCURSOR:
989 		vtbuf_cursor_visibility(&vw->vw_buf, arg);
990 		vt_resume_flush_timer(vw->vw_device, 0);
991 		break;
992 	case TP_MOUSE:
993 		vw->vw_mouse_level = arg;
994 		break;
995 	}
996 }
997 
998 void
999 vt_determine_colors(term_char_t c, int cursor,
1000     term_color_t *fg, term_color_t *bg)
1001 {
1002 	term_color_t tmp;
1003 	int invert;
1004 
1005 	invert = 0;
1006 
1007 	*fg = TCHAR_FGCOLOR(c);
1008 	if (TCHAR_FORMAT(c) & TF_BOLD)
1009 		*fg = TCOLOR_LIGHT(*fg);
1010 	*bg = TCHAR_BGCOLOR(c);
1011 
1012 	if (TCHAR_FORMAT(c) & TF_REVERSE)
1013 		invert ^= 1;
1014 	if (cursor)
1015 		invert ^= 1;
1016 
1017 	if (invert) {
1018 		tmp = *fg;
1019 		*fg = *bg;
1020 		*bg = tmp;
1021 	}
1022 }
1023 
1024 #ifndef SC_NO_CUTPASTE
1025 int
1026 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1027 {
1028 	unsigned int mx, my, x1, y1, x2, y2;
1029 
1030 	/*
1031 	 * We use the cursor position saved during the current refresh,
1032 	 * in case the cursor moved since.
1033 	 */
1034 	mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1035 	my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1036 
1037 	x1 = area->tr_begin.tp_col;
1038 	y1 = area->tr_begin.tp_row;
1039 	x2 = area->tr_end.tp_col;
1040 	y2 = area->tr_end.tp_row;
1041 
1042 	if (((mx >= x1 && x2 - 1 >= mx) ||
1043 	     (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
1044 	    ((my >= y1 && y2 - 1 >= my) ||
1045 	     (my < y1 && my + vd->vd_mcursor->height >= y1)))
1046 		return (1);
1047 
1048 	return (0);
1049 }
1050 
1051 static void
1052 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1053 {
1054 	term_rect_t area;
1055 	struct vt_window *vw;
1056 	struct vt_font *vf;
1057 	int x, y;
1058 
1059 	vw = vd->vd_curwindow;
1060 	vf = vw->vw_font;
1061 
1062 	x = vd->vd_mx_drawn;
1063 	y = vd->vd_my_drawn;
1064 
1065 	if (vf != NULL) {
1066 		area.tr_begin.tp_col = x / vf->vf_width;
1067 		area.tr_begin.tp_row = y / vf->vf_height;
1068 		area.tr_end.tp_col =
1069 		    ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1070 		area.tr_end.tp_row =
1071 		    ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1072 	} else {
1073 		/*
1074 		 * No font loaded (ie. vt_vga operating in textmode).
1075 		 *
1076 		 * FIXME: This fake area needs to be revisited once the
1077 		 * mouse cursor is supported in vt_vga's textmode.
1078 		 */
1079 		area.tr_begin.tp_col = x;
1080 		area.tr_begin.tp_row = y;
1081 		area.tr_end.tp_col = x + 2;
1082 		area.tr_end.tp_row = y + 2;
1083 	}
1084 
1085 	vtbuf_dirty(&vw->vw_buf, &area);
1086 }
1087 #endif
1088 
1089 static int
1090 vt_flush(struct vt_device *vd)
1091 {
1092 	struct vt_window *vw;
1093 	struct vt_font *vf;
1094 	term_rect_t tarea;
1095 	term_pos_t size;
1096 #ifndef SC_NO_CUTPASTE
1097 	int cursor_was_shown, cursor_moved;
1098 #endif
1099 
1100 	vw = vd->vd_curwindow;
1101 	if (vw == NULL)
1102 		return (0);
1103 
1104 	if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1105 		return (0);
1106 
1107 	vf = vw->vw_font;
1108 	if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1109 		return (0);
1110 
1111 #ifndef SC_NO_CUTPASTE
1112 	cursor_was_shown = vd->vd_mshown;
1113 	cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1114 	    vd->vd_my != vd->vd_my_drawn);
1115 
1116 	/* Check if the cursor should be displayed or not. */
1117 	if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1118 	    !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed.      */
1119 	    !kdb_active && panicstr == NULL) {  /* DDB inactive.          */
1120 		vd->vd_mshown = 1;
1121 	} else {
1122 		vd->vd_mshown = 0;
1123 	}
1124 
1125 	/*
1126 	 * If the cursor changed display state or moved, we must mark
1127 	 * the old position as dirty, so that it's erased.
1128 	 */
1129 	if (cursor_was_shown != vd->vd_mshown ||
1130 	    (vd->vd_mshown && cursor_moved))
1131 		vt_mark_mouse_position_as_dirty(vd);
1132 
1133 	/*
1134          * Save position of the mouse cursor. It's used by backends to
1135          * know where to draw the cursor and during the next refresh to
1136          * erase the previous position.
1137 	 */
1138 	vd->vd_mx_drawn = vd->vd_mx;
1139 	vd->vd_my_drawn = vd->vd_my;
1140 
1141 	/*
1142 	 * If the cursor is displayed and has moved since last refresh,
1143 	 * mark the new position as dirty.
1144 	 */
1145 	if (vd->vd_mshown && cursor_moved)
1146 		vt_mark_mouse_position_as_dirty(vd);
1147 #endif
1148 
1149 	vtbuf_undirty(&vw->vw_buf, &tarea);
1150 	vt_termsize(vd, vf, &size);
1151 
1152 	/* Force a full redraw when the screen contents are invalid. */
1153 	if (vd->vd_flags & VDF_INVALID) {
1154 		tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1155 		tarea.tr_end = size;
1156 
1157 		vd->vd_flags &= ~VDF_INVALID;
1158 	}
1159 
1160 	if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1161 		vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1162 		return (1);
1163 	}
1164 
1165 	return (0);
1166 }
1167 
1168 static void
1169 vt_timer(void *arg)
1170 {
1171 	struct vt_device *vd;
1172 	int changed;
1173 
1174 	vd = arg;
1175 	/* Update screen if required. */
1176 	changed = vt_flush(vd);
1177 
1178 	/* Schedule for next update. */
1179 	if (changed)
1180 		vt_schedule_flush(vd, 0);
1181 	else
1182 		vd->vd_timer_armed = 0;
1183 }
1184 
1185 static void
1186 vtterm_done(struct terminal *tm)
1187 {
1188 	struct vt_window *vw = tm->tm_softc;
1189 	struct vt_device *vd = vw->vw_device;
1190 
1191 	if (kdb_active || panicstr != NULL) {
1192 		/* Switch to the debugger. */
1193 		if (vd->vd_curwindow != vw) {
1194 			vd->vd_curwindow = vw;
1195 			vd->vd_flags |= VDF_INVALID;
1196 			if (vd->vd_driver->vd_postswitch)
1197 				vd->vd_driver->vd_postswitch(vd);
1198 		}
1199 		vd->vd_flags &= ~VDF_SPLASH;
1200 		vt_flush(vd);
1201 	} else if (!(vd->vd_flags & VDF_ASYNC)) {
1202 		vt_flush(vd);
1203 	}
1204 }
1205 
1206 #ifdef DEV_SPLASH
1207 static void
1208 vtterm_splash(struct vt_device *vd)
1209 {
1210 	vt_axis_t top, left;
1211 
1212 	/* Display a nice boot splash. */
1213 	if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1214 
1215 		top = (vd->vd_height - vt_logo_height) / 2;
1216 		left = (vd->vd_width - vt_logo_width) / 2;
1217 		switch (vt_logo_depth) {
1218 		case 1:
1219 			/* XXX: Unhardcode colors! */
1220 			vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1221 			    vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1222 			    left, top, TC_WHITE, TC_BLACK);
1223 		}
1224 		vd->vd_flags |= VDF_SPLASH;
1225 	}
1226 }
1227 #endif
1228 
1229 
1230 static void
1231 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1232 {
1233 	struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1234 	struct vt_window *vw = tm->tm_softc;
1235 	struct vt_device *vd = vw->vw_device;
1236 	struct winsize wsz;
1237 	term_attr_t attr;
1238 	term_char_t c;
1239 
1240 	if (!vty_enabled(VTY_VT))
1241 		return;
1242 
1243 	if (vd->vd_flags & VDF_INITIALIZED)
1244 		/* Initialization already done. */
1245 		return;
1246 
1247 	SET_FOREACH(vtdlist, vt_drv_set) {
1248 		vtd = *vtdlist;
1249 		if (vtd->vd_probe == NULL)
1250 			continue;
1251 		if (vtd->vd_probe(vd) == CN_DEAD)
1252 			continue;
1253 		if ((vtdbest == NULL) ||
1254 		    (vtd->vd_priority > vtdbest->vd_priority))
1255 			vtdbest = vtd;
1256 	}
1257 	if (vtdbest == NULL) {
1258 		cp->cn_pri = CN_DEAD;
1259 		vd->vd_flags |= VDF_DEAD;
1260 	} else {
1261 		vd->vd_driver = vtdbest;
1262 		cp->cn_pri = vd->vd_driver->vd_init(vd);
1263 	}
1264 
1265 	/* Check if driver's vt_init return CN_DEAD. */
1266 	if (cp->cn_pri == CN_DEAD) {
1267 		vd->vd_flags |= VDF_DEAD;
1268 	}
1269 
1270 	/* Initialize any early-boot keyboard drivers */
1271 	kbd_configure(KB_CONF_PROBE_ONLY);
1272 
1273 	vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1274 	vd->vd_windows[VT_CONSWINDOW] = vw;
1275 	sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1276 
1277 	/* Attach default font if not in TEXTMODE. */
1278 	if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1279 		vw->vw_font = vtfont_ref(&vt_font_default);
1280 		vt_compute_drawable_area(vw);
1281 	}
1282 
1283 	/*
1284 	 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1285 	 * that we have the real viewable size, fix it in the static
1286 	 * buffer.
1287 	 */
1288 	if (vd->vd_width != 0 && vd->vd_height != 0)
1289 		vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1290 
1291 	vtbuf_init_early(&vw->vw_buf);
1292 	vt_winsize(vd, vw->vw_font, &wsz);
1293 	c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1294 	    TERMINAL_NORM_ATTR;
1295 	attr.ta_format = TCHAR_FORMAT(c);
1296 	attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1297 	attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1298 	terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1299 
1300 	if (vtdbest != NULL) {
1301 #ifdef DEV_SPLASH
1302 		vtterm_splash(vd);
1303 #endif
1304 		vd->vd_flags |= VDF_INITIALIZED;
1305 	}
1306 }
1307 
1308 static int
1309 vtterm_cngetc(struct terminal *tm)
1310 {
1311 	struct vt_window *vw = tm->tm_softc;
1312 	struct vt_device *vd = vw->vw_device;
1313 	keyboard_t *kbd;
1314 	u_int c;
1315 
1316 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
1317 		return (*vw->vw_kbdsq++);
1318 
1319 	/* Make sure the splash screen is not there. */
1320 	if (vd->vd_flags & VDF_SPLASH) {
1321 		/* Remove splash */
1322 		vd->vd_flags &= ~VDF_SPLASH;
1323 		/* Mark screen as invalid to force update */
1324 		vd->vd_flags |= VDF_INVALID;
1325 		vt_flush(vd);
1326 	}
1327 
1328 	/* Stripped down keyboard handler. */
1329 	kbd = kbd_get_keyboard(vd->vd_keyboard);
1330 	if (kbd == NULL)
1331 		return (-1);
1332 
1333 	/* Force keyboard input mode to K_XLATE */
1334 	vw->vw_kbdmode = K_XLATE;
1335 	vt_update_kbd_mode(vw, kbd);
1336 
1337 	/* Switch the keyboard to polling to make it work here. */
1338 	kbdd_poll(kbd, TRUE);
1339 	c = kbdd_read_char(kbd, 0);
1340 	kbdd_poll(kbd, FALSE);
1341 	if (c & RELKEY)
1342 		return (-1);
1343 
1344 	if (vw->vw_flags & VWF_SCROLL) {
1345 		vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1346 		vt_flush(vd);
1347 		return (-1);
1348 	}
1349 
1350 	/* Stripped down handling of vt_kbdevent(), without locking, etc. */
1351 	if (c & SPCLKEY) {
1352 		switch (c) {
1353 		case SPCLKEY | SLK:
1354 			vt_save_kbd_state(vw, kbd);
1355 			if (vw->vw_kbdstate & SLKED) {
1356 				/* Turn scrolling on. */
1357 				vw->vw_flags |= VWF_SCROLL;
1358 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
1359 			} else {
1360 				/* Turn scrolling off. */
1361 				vt_scroll(vw, 0, VHS_END);
1362 				vw->vw_flags &= ~VWF_SCROLL;
1363 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
1364 			}
1365 			break;
1366 		/* XXX: KDB can handle history. */
1367 		case SPCLKEY | FKEY | F(50): /* Arrow up. */
1368 			vw->vw_kbdsq = "\x1b[A";
1369 			break;
1370 		case SPCLKEY | FKEY | F(58): /* Arrow down. */
1371 			vw->vw_kbdsq = "\x1b[B";
1372 			break;
1373 		case SPCLKEY | FKEY | F(55): /* Arrow right. */
1374 			vw->vw_kbdsq = "\x1b[C";
1375 			break;
1376 		case SPCLKEY | FKEY | F(53): /* Arrow left. */
1377 			vw->vw_kbdsq = "\x1b[D";
1378 			break;
1379 		}
1380 
1381 		/* Force refresh to make scrollback work. */
1382 		vt_flush(vd);
1383 	} else if (KEYFLAGS(c) == 0) {
1384 		return (KEYCHAR(c));
1385 	}
1386 
1387 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
1388 		return (*vw->vw_kbdsq++);
1389 
1390 	return (-1);
1391 }
1392 
1393 static void
1394 vtterm_cngrab(struct terminal *tm)
1395 {
1396 	struct vt_device *vd;
1397 	struct vt_window *vw;
1398 	keyboard_t *kbd;
1399 
1400 	vw = tm->tm_softc;
1401 	vd = vw->vw_device;
1402 
1403 	if (!cold)
1404 		vt_window_switch(vw);
1405 
1406 	kbd = kbd_get_keyboard(vd->vd_keyboard);
1407 	if (kbd == NULL)
1408 		return;
1409 
1410 	if (vw->vw_grabbed++ > 0)
1411 		return;
1412 
1413 	/*
1414 	 * Make sure the keyboard is accessible even when the kbd device
1415 	 * driver is disabled.
1416 	 */
1417 	kbdd_enable(kbd);
1418 
1419 	/* We shall always use the keyboard in the XLATE mode here. */
1420 	vw->vw_prev_kbdmode = vw->vw_kbdmode;
1421 	vw->vw_kbdmode = K_XLATE;
1422 	vt_update_kbd_mode(vw, kbd);
1423 
1424 	kbdd_poll(kbd, TRUE);
1425 }
1426 
1427 static void
1428 vtterm_cnungrab(struct terminal *tm)
1429 {
1430 	struct vt_device *vd;
1431 	struct vt_window *vw;
1432 	keyboard_t *kbd;
1433 
1434 	vw = tm->tm_softc;
1435 	vd = vw->vw_device;
1436 
1437 	kbd = kbd_get_keyboard(vd->vd_keyboard);
1438 	if (kbd == NULL)
1439 		return;
1440 
1441 	if (--vw->vw_grabbed > 0)
1442 		return;
1443 
1444 	kbdd_poll(kbd, FALSE);
1445 
1446 	vw->vw_kbdmode = vw->vw_prev_kbdmode;
1447 	vt_update_kbd_mode(vw, kbd);
1448 	kbdd_disable(kbd);
1449 }
1450 
1451 static void
1452 vtterm_opened(struct terminal *tm, int opened)
1453 {
1454 	struct vt_window *vw = tm->tm_softc;
1455 	struct vt_device *vd = vw->vw_device;
1456 
1457 	VT_LOCK(vd);
1458 	vd->vd_flags &= ~VDF_SPLASH;
1459 	if (opened)
1460 		vw->vw_flags |= VWF_OPENED;
1461 	else {
1462 		vw->vw_flags &= ~VWF_OPENED;
1463 		/* TODO: finish ACQ/REL */
1464 	}
1465 	VT_UNLOCK(vd);
1466 }
1467 
1468 static int
1469 vt_set_border(struct vt_window *vw, term_color_t c)
1470 {
1471 	struct vt_device *vd = vw->vw_device;
1472 
1473 	if (vd->vd_driver->vd_drawrect == NULL)
1474 		return (ENOTSUP);
1475 
1476 	/* Top bar. */
1477 	if (vw->vw_draw_area.tr_begin.tp_row > 0)
1478 		vd->vd_driver->vd_drawrect(vd,
1479 		    0, 0,
1480 		    vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1481 		    1, c);
1482 
1483 	/* Left bar. */
1484 	if (vw->vw_draw_area.tr_begin.tp_col > 0)
1485 		vd->vd_driver->vd_drawrect(vd,
1486 		    0, 0,
1487 		    vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1488 		    1, c);
1489 
1490 	/* Right bar. */
1491 	if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1492 		vd->vd_driver->vd_drawrect(vd,
1493 		    vw->vw_draw_area.tr_end.tp_col - 1, 0,
1494 		    vd->vd_width - 1, vd->vd_height - 1,
1495 		    1, c);
1496 
1497 	/* Bottom bar. */
1498 	if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1499 		vd->vd_driver->vd_drawrect(vd,
1500 		    0, vw->vw_draw_area.tr_end.tp_row - 1,
1501 		    vd->vd_width - 1, vd->vd_height - 1,
1502 		    1, c);
1503 
1504 	return (0);
1505 }
1506 
1507 static int
1508 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1509 {
1510 	struct vt_device *vd = vw->vw_device;
1511 	struct terminal *tm = vw->vw_terminal;
1512 	term_pos_t size;
1513 	struct winsize wsz;
1514 
1515 	/*
1516 	 * Changing fonts.
1517 	 *
1518 	 * Changing fonts is a little tricky.  We must prevent
1519 	 * simultaneous access to the device, so we must stop
1520 	 * the display timer and the terminal from accessing.
1521 	 * We need to switch fonts and grow our screen buffer.
1522 	 *
1523 	 * XXX: Right now the code uses terminal_mute() to
1524 	 * prevent data from reaching the console driver while
1525 	 * resizing the screen buffer.  This isn't elegant...
1526 	 */
1527 
1528 	VT_LOCK(vd);
1529 	if (vw->vw_flags & VWF_BUSY) {
1530 		/* Another process is changing the font. */
1531 		VT_UNLOCK(vd);
1532 		return (EBUSY);
1533 	}
1534 	vw->vw_flags |= VWF_BUSY;
1535 	VT_UNLOCK(vd);
1536 
1537 	vt_termsize(vd, vf, &size);
1538 	vt_winsize(vd, vf, &wsz);
1539 
1540 	/* Grow the screen buffer and terminal. */
1541 	terminal_mute(tm, 1);
1542 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1543 	terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1544 	terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1545 	terminal_mute(tm, 0);
1546 
1547 	/* Actually apply the font to the current window. */
1548 	VT_LOCK(vd);
1549 	if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1550 		/*
1551 		 * In case vt_change_font called to update size we don't need
1552 		 * to update font link.
1553 		 */
1554 		vtfont_unref(vw->vw_font);
1555 		vw->vw_font = vtfont_ref(vf);
1556 	}
1557 
1558 	/*
1559 	 * Compute the drawable area and move the mouse cursor inside
1560 	 * it, in case the new area is smaller than the previous one.
1561 	 */
1562 	vt_compute_drawable_area(vw);
1563 	vd->vd_mx = min(vd->vd_mx,
1564 	    vw->vw_draw_area.tr_end.tp_col -
1565 	    vw->vw_draw_area.tr_begin.tp_col - 1);
1566 	vd->vd_my = min(vd->vd_my,
1567 	    vw->vw_draw_area.tr_end.tp_row -
1568 	    vw->vw_draw_area.tr_begin.tp_row - 1);
1569 
1570 	/* Force a full redraw the next timer tick. */
1571 	if (vd->vd_curwindow == vw) {
1572 		vt_set_border(vw, TC_BLACK);
1573 		vd->vd_flags |= VDF_INVALID;
1574 		vt_resume_flush_timer(vw->vw_device, 0);
1575 	}
1576 	vw->vw_flags &= ~VWF_BUSY;
1577 	VT_UNLOCK(vd);
1578 	return (0);
1579 }
1580 
1581 static int
1582 vt_proc_alive(struct vt_window *vw)
1583 {
1584 	struct proc *p;
1585 
1586 	if (vw->vw_smode.mode != VT_PROCESS)
1587 		return (FALSE);
1588 
1589 	if (vw->vw_proc) {
1590 		if ((p = pfind(vw->vw_pid)) != NULL)
1591 			PROC_UNLOCK(p);
1592 		if (vw->vw_proc == p)
1593 			return (TRUE);
1594 		vw->vw_proc = NULL;
1595 		vw->vw_smode.mode = VT_AUTO;
1596 		DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1597 		vw->vw_pid = 0;
1598 	}
1599 	return (FALSE);
1600 }
1601 
1602 static int
1603 signal_vt_rel(struct vt_window *vw)
1604 {
1605 
1606 	if (vw->vw_smode.mode != VT_PROCESS)
1607 		return (FALSE);
1608 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1609 		vw->vw_proc = NULL;
1610 		vw->vw_pid = 0;
1611 		return (TRUE);
1612 	}
1613 	vw->vw_flags |= VWF_SWWAIT_REL;
1614 	PROC_LOCK(vw->vw_proc);
1615 	kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1616 	PROC_UNLOCK(vw->vw_proc);
1617 	DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1618 	return (TRUE);
1619 }
1620 
1621 static int
1622 signal_vt_acq(struct vt_window *vw)
1623 {
1624 
1625 	if (vw->vw_smode.mode != VT_PROCESS)
1626 		return (FALSE);
1627 	if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1628 		cnavailable(vw->vw_terminal->consdev, FALSE);
1629 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1630 		vw->vw_proc = NULL;
1631 		vw->vw_pid = 0;
1632 		return (TRUE);
1633 	}
1634 	vw->vw_flags |= VWF_SWWAIT_ACQ;
1635 	PROC_LOCK(vw->vw_proc);
1636 	kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1637 	PROC_UNLOCK(vw->vw_proc);
1638 	DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1639 	return (TRUE);
1640 }
1641 
1642 static int
1643 finish_vt_rel(struct vt_window *vw, int release, int *s)
1644 {
1645 
1646 	if (vw->vw_flags & VWF_SWWAIT_REL) {
1647 		vw->vw_flags &= ~VWF_SWWAIT_REL;
1648 		if (release) {
1649 			callout_drain(&vw->vw_proc_dead_timer);
1650 			vt_late_window_switch(vw->vw_switch_to);
1651 		}
1652 		return (0);
1653 	}
1654 	return (EINVAL);
1655 }
1656 
1657 static int
1658 finish_vt_acq(struct vt_window *vw)
1659 {
1660 
1661 	if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1662 		vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1663 		return (0);
1664 	}
1665 	return (EINVAL);
1666 }
1667 
1668 #ifndef SC_NO_CUTPASTE
1669 static void
1670 vt_mouse_terminput_button(struct vt_device *vd, int button)
1671 {
1672 	struct vt_window *vw;
1673 	struct vt_font *vf;
1674 	char mouseb[6] = "\x1B[M";
1675 	int i, x, y;
1676 
1677 	vw = vd->vd_curwindow;
1678 	vf = vw->vw_font;
1679 
1680 	/* Translate to char position. */
1681 	x = vd->vd_mx / vf->vf_width;
1682 	y = vd->vd_my / vf->vf_height;
1683 	/* Avoid overflow. */
1684 	x = MIN(x, 255 - '!');
1685 	y = MIN(y, 255 - '!');
1686 
1687 	mouseb[3] = ' ' + button;
1688 	mouseb[4] = '!' + x;
1689 	mouseb[5] = '!' + y;
1690 
1691 	for (i = 0; i < sizeof(mouseb); i++)
1692 		terminal_input_char(vw->vw_terminal, mouseb[i]);
1693 }
1694 
1695 static void
1696 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1697     int cnt)
1698 {
1699 
1700 	switch (type) {
1701 	case MOUSE_BUTTON_EVENT:
1702 		if (cnt > 0) {
1703 			/* Mouse button pressed. */
1704 			if (event & MOUSE_BUTTON1DOWN)
1705 				vt_mouse_terminput_button(vd, 0);
1706 			if (event & MOUSE_BUTTON2DOWN)
1707 				vt_mouse_terminput_button(vd, 1);
1708 			if (event & MOUSE_BUTTON3DOWN)
1709 				vt_mouse_terminput_button(vd, 2);
1710 		} else {
1711 			/* Mouse button released. */
1712 			vt_mouse_terminput_button(vd, 3);
1713 		}
1714 		break;
1715 #ifdef notyet
1716 	case MOUSE_MOTION_EVENT:
1717 		if (mouse->u.data.z < 0) {
1718 			/* Scroll up. */
1719 			sc_mouse_input_button(vd, 64);
1720 		} else if (mouse->u.data.z > 0) {
1721 			/* Scroll down. */
1722 			sc_mouse_input_button(vd, 65);
1723 		}
1724 		break;
1725 #endif
1726 	}
1727 }
1728 
1729 static void
1730 vt_mouse_paste()
1731 {
1732 	term_char_t *buf;
1733 	int i, len;
1734 
1735 	len = VD_PASTEBUFLEN(main_vd);
1736 	buf = VD_PASTEBUF(main_vd);
1737 	len /= sizeof(term_char_t);
1738 	for (i = 0; i < len; i++) {
1739 		if (buf[i] == '\0')
1740 			continue;
1741 		terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1742 		    buf[i]);
1743 	}
1744 }
1745 
1746 void
1747 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1748 {
1749 	struct vt_device *vd;
1750 	struct vt_window *vw;
1751 	struct vt_font *vf;
1752 	term_pos_t size;
1753 	int len, mark;
1754 
1755 	vd = main_vd;
1756 	vw = vd->vd_curwindow;
1757 	vf = vw->vw_font;
1758 	mark = 0;
1759 
1760 	if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1761 		/*
1762 		 * Either the mouse is disabled, or the window is in
1763 		 * "graphics mode". The graphics mode is usually set by
1764 		 * an X server, using the KDSETMODE ioctl.
1765 		 */
1766 		return;
1767 
1768 	if (vf == NULL)	/* Text mode. */
1769 		return;
1770 
1771 	/*
1772 	 * TODO: add flag about pointer position changed, to not redraw chars
1773 	 * under mouse pointer when nothing changed.
1774 	 */
1775 
1776 	if (vw->vw_mouse_level > 0)
1777 		vt_mouse_terminput(vd, type, x, y, event, cnt);
1778 
1779 	switch (type) {
1780 	case MOUSE_ACTION:
1781 	case MOUSE_MOTION_EVENT:
1782 		/* Movement */
1783 		x += vd->vd_mx;
1784 		y += vd->vd_my;
1785 
1786 		vt_termsize(vd, vf, &size);
1787 
1788 		/* Apply limits. */
1789 		x = MAX(x, 0);
1790 		y = MAX(y, 0);
1791 		x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1792 		y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1793 
1794 		vd->vd_mx = x;
1795 		vd->vd_my = y;
1796 		if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1797 			vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1798 			    vd->vd_mx / vf->vf_width,
1799 			    vd->vd_my / vf->vf_height);
1800 
1801 		vt_resume_flush_timer(vw->vw_device, 0);
1802 		return; /* Done */
1803 	case MOUSE_BUTTON_EVENT:
1804 		/* Buttons */
1805 		break;
1806 	default:
1807 		return; /* Done */
1808 	}
1809 
1810 	switch (event) {
1811 	case MOUSE_BUTTON1DOWN:
1812 		switch (cnt % 4) {
1813 		case 0:	/* up */
1814 			mark = VTB_MARK_END;
1815 			break;
1816 		case 1: /* single click: start cut operation */
1817 			mark = VTB_MARK_START;
1818 			break;
1819 		case 2:	/* double click: cut a word */
1820 			mark = VTB_MARK_WORD;
1821 			break;
1822 		case 3:	/* triple click: cut a line */
1823 			mark = VTB_MARK_ROW;
1824 			break;
1825 		}
1826 		break;
1827 	case VT_MOUSE_PASTEBUTTON:
1828 		switch (cnt) {
1829 		case 0:	/* up */
1830 			break;
1831 		default:
1832 			vt_mouse_paste();
1833 			break;
1834 		}
1835 		return; /* Done */
1836 	case VT_MOUSE_EXTENDBUTTON:
1837 		switch (cnt) {
1838 		case 0:	/* up */
1839 			if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1840 				mark = VTB_MARK_EXTEND;
1841 			else
1842 				mark = 0;
1843 			break;
1844 		default:
1845 			mark = VTB_MARK_EXTEND;
1846 			break;
1847 		}
1848 		break;
1849 	default:
1850 		return; /* Done */
1851 	}
1852 
1853 	/* Save buttons state. */
1854 	if (cnt > 0)
1855 		vd->vd_mstate |= event;
1856 	else
1857 		vd->vd_mstate &= ~event;
1858 
1859 	if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1860 	    vd->vd_my / vf->vf_height) == 1) {
1861 		/*
1862 		 * We have something marked to copy, so update pointer to
1863 		 * window with selection.
1864 		 */
1865 		vt_resume_flush_timer(vw->vw_device, 0);
1866 
1867 		switch (mark) {
1868 		case VTB_MARK_END:
1869 		case VTB_MARK_WORD:
1870 		case VTB_MARK_ROW:
1871 		case VTB_MARK_EXTEND:
1872 			break;
1873 		default:
1874 			/* Other types of mark do not require to copy data. */
1875 			return;
1876 		}
1877 
1878 		/* Get current selection size in bytes. */
1879 		len = vtbuf_get_marked_len(&vw->vw_buf);
1880 		if (len <= 0)
1881 			return;
1882 
1883 		/* Reallocate buffer only if old one is too small. */
1884 		if (len > VD_PASTEBUFSZ(vd)) {
1885 			VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1886 			    M_WAITOK | M_ZERO);
1887 			/* Update buffer size. */
1888 			VD_PASTEBUFSZ(vd) = len;
1889 		}
1890 		/* Request copy/paste buffer data, no more than `len' */
1891 		vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1892 		    VD_PASTEBUFSZ(vd));
1893 
1894 		VD_PASTEBUFLEN(vd) = len;
1895 
1896 		/* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1897 	}
1898 }
1899 
1900 void
1901 vt_mouse_state(int show)
1902 {
1903 	struct vt_device *vd;
1904 	struct vt_window *vw;
1905 
1906 	vd = main_vd;
1907 	vw = vd->vd_curwindow;
1908 
1909 	switch (show) {
1910 	case VT_MOUSE_HIDE:
1911 		vw->vw_flags |= VWF_MOUSE_HIDE;
1912 		break;
1913 	case VT_MOUSE_SHOW:
1914 		vw->vw_flags &= ~VWF_MOUSE_HIDE;
1915 		break;
1916 	}
1917 
1918 	/* Mark mouse position as dirty. */
1919 	vt_mark_mouse_position_as_dirty(vd);
1920 	vt_resume_flush_timer(vw->vw_device, 0);
1921 }
1922 #endif
1923 
1924 static int
1925 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1926     int nprot, vm_memattr_t *memattr)
1927 {
1928 	struct vt_window *vw = tm->tm_softc;
1929 	struct vt_device *vd = vw->vw_device;
1930 
1931 	if (vd->vd_driver->vd_fb_mmap)
1932 		return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1933 		    memattr));
1934 
1935 	return (ENXIO);
1936 }
1937 
1938 static int
1939 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1940     struct thread *td)
1941 {
1942 	struct vt_window *vw = tm->tm_softc;
1943 	struct vt_device *vd = vw->vw_device;
1944 	keyboard_t *kbd;
1945 	int error, i, s;
1946 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1947     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1948 	int ival;
1949 
1950 	switch (cmd) {
1951 	case _IO('v', 4):
1952 		cmd = VT_RELDISP;
1953 		break;
1954 	case _IO('v', 5):
1955 		cmd = VT_ACTIVATE;
1956 		break;
1957 	case _IO('v', 6):
1958 		cmd = VT_WAITACTIVE;
1959 		break;
1960 	case _IO('K', 20):
1961 		cmd = KDSKBSTATE;
1962 		break;
1963 	case _IO('K', 67):
1964 		cmd = KDSETRAD;
1965 		break;
1966 	case _IO('K', 7):
1967 		cmd = KDSKBMODE;
1968 		break;
1969 	case _IO('K', 8):
1970 		cmd = KDMKTONE;
1971 		break;
1972 	case _IO('K', 63):
1973 		cmd = KIOCSOUND;
1974 		break;
1975 	case _IO('K', 66):
1976 		cmd = KDSETLED;
1977 		break;
1978 	case _IO('c', 110):
1979 		cmd = CONS_SETKBD;
1980 		break;
1981 	default:
1982 		goto skip_thunk;
1983 	}
1984 	ival = IOCPARM_IVAL(data);
1985 	data = (caddr_t)&ival;
1986 skip_thunk:
1987 #endif
1988 
1989 	switch (cmd) {
1990 	case KDSETRAD:		/* set keyboard repeat & delay rates (old) */
1991 		if (*(int *)data & ~0x7f)
1992 			return (EINVAL);
1993 		/* FALLTHROUGH */
1994 	case GIO_KEYMAP:
1995 	case PIO_KEYMAP:
1996 	case GIO_DEADKEYMAP:
1997 	case PIO_DEADKEYMAP:
1998 	case GETFKEY:
1999 	case SETFKEY:
2000 	case KDGKBINFO:
2001 	case KDGKBTYPE:
2002 	case KDGETREPEAT:	/* get keyboard repeat & delay rates */
2003 	case KDSETREPEAT:	/* set keyboard repeat & delay rates (new) */
2004 	case KBADDKBD:		/* add/remove keyboard to/from mux */
2005 	case KBRELKBD: {
2006 		error = 0;
2007 
2008 		mtx_lock(&Giant);
2009 		kbd = kbd_get_keyboard(vd->vd_keyboard);
2010 		if (kbd != NULL)
2011 			error = kbdd_ioctl(kbd, cmd, data);
2012 		mtx_unlock(&Giant);
2013 		if (error == ENOIOCTL) {
2014 			if (cmd == KDGKBTYPE) {
2015 				/* always return something? XXX */
2016 				*(int *)data = 0;
2017 			} else {
2018 				return (ENODEV);
2019 			}
2020 		}
2021 		return (error);
2022 	}
2023 	case KDGKBSTATE: {	/* get keyboard state (locks) */
2024 		error = 0;
2025 
2026 		if (vw == vd->vd_curwindow) {
2027 			mtx_lock(&Giant);
2028 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2029 			if (kbd != NULL)
2030 				error = vt_save_kbd_state(vw, kbd);
2031 			mtx_unlock(&Giant);
2032 
2033 			if (error != 0)
2034 				return (error);
2035 		}
2036 
2037 		*(int *)data = vw->vw_kbdstate & LOCK_MASK;
2038 
2039 		return (error);
2040 	}
2041 	case KDSKBSTATE: {	/* set keyboard state (locks) */
2042 		int state;
2043 
2044 		state = *(int *)data;
2045 		if (state & ~LOCK_MASK)
2046 			return (EINVAL);
2047 
2048 		vw->vw_kbdstate &= ~LOCK_MASK;
2049 		vw->vw_kbdstate |= state;
2050 
2051 		error = 0;
2052 		if (vw == vd->vd_curwindow) {
2053 			mtx_lock(&Giant);
2054 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2055 			if (kbd != NULL)
2056 				error = vt_update_kbd_state(vw, kbd);
2057 			mtx_unlock(&Giant);
2058 		}
2059 
2060 		return (error);
2061 	}
2062 	case KDGETLED: {	/* get keyboard LED status */
2063 		error = 0;
2064 
2065 		if (vw == vd->vd_curwindow) {
2066 			mtx_lock(&Giant);
2067 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2068 			if (kbd != NULL)
2069 				error = vt_save_kbd_leds(vw, kbd);
2070 			mtx_unlock(&Giant);
2071 
2072 			if (error != 0)
2073 				return (error);
2074 		}
2075 
2076 		*(int *)data = vw->vw_kbdstate & LED_MASK;
2077 
2078 		return (error);
2079 	}
2080 	case KDSETLED: {	/* set keyboard LED status */
2081 		int leds;
2082 
2083 		leds = *(int *)data;
2084 		if (leds & ~LED_MASK)
2085 			return (EINVAL);
2086 
2087 		vw->vw_kbdstate &= ~LED_MASK;
2088 		vw->vw_kbdstate |= leds;
2089 
2090 		error = 0;
2091 		if (vw == vd->vd_curwindow) {
2092 			mtx_lock(&Giant);
2093 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2094 			if (kbd != NULL)
2095 				error = vt_update_kbd_leds(vw, kbd);
2096 			mtx_unlock(&Giant);
2097 		}
2098 
2099 		return (error);
2100 	}
2101 	case KDGKBMODE: {
2102 		error = 0;
2103 
2104 		if (vw == vd->vd_curwindow) {
2105 			mtx_lock(&Giant);
2106 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2107 			if (kbd != NULL)
2108 				error = vt_save_kbd_mode(vw, kbd);
2109 			mtx_unlock(&Giant);
2110 
2111 			if (error != 0)
2112 				return (error);
2113 		}
2114 
2115 		*(int *)data = vw->vw_kbdmode;
2116 
2117 		return (error);
2118 	}
2119 	case KDSKBMODE: {
2120 		int mode;
2121 
2122 		mode = *(int *)data;
2123 		switch (mode) {
2124 		case K_XLATE:
2125 		case K_RAW:
2126 		case K_CODE:
2127 			vw->vw_kbdmode = mode;
2128 
2129 			error = 0;
2130 			if (vw == vd->vd_curwindow) {
2131 				mtx_lock(&Giant);
2132 				kbd = kbd_get_keyboard(vd->vd_keyboard);
2133 				if (kbd != NULL)
2134 					error = vt_update_kbd_mode(vw, kbd);
2135 				mtx_unlock(&Giant);
2136 			}
2137 
2138 			return (error);
2139 		default:
2140 			return (EINVAL);
2141 		}
2142 	}
2143 	case FBIOGTYPE:
2144 	case FBIO_GETWINORG:	/* get frame buffer window origin */
2145 	case FBIO_GETDISPSTART:	/* get display start address */
2146 	case FBIO_GETLINEWIDTH:	/* get scan line width in bytes */
2147 	case FBIO_BLANK:	/* blank display */
2148 		if (vd->vd_driver->vd_fb_ioctl)
2149 			return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2150 		break;
2151 	case CONS_BLANKTIME:
2152 		/* XXX */
2153 		return (0);
2154 	case CONS_GET:
2155 		/* XXX */
2156 		*(int *)data = M_CG640x480;
2157 		return (0);
2158 	case CONS_BELLTYPE: 	/* set bell type sound */
2159 		if ((*(int *)data) & CONS_QUIET_BELL)
2160 			vd->vd_flags |= VDF_QUIET_BELL;
2161 		else
2162 			vd->vd_flags &= ~VDF_QUIET_BELL;
2163 		return (0);
2164 	case CONS_GETINFO: {
2165 		vid_info_t *vi = (vid_info_t *)data;
2166 		if (vi->size != sizeof(struct vid_info))
2167 			return (EINVAL);
2168 
2169 		if (vw == vd->vd_curwindow) {
2170 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2171 			if (kbd != NULL)
2172 				vt_save_kbd_state(vw, kbd);
2173 		}
2174 
2175 		vi->m_num = vd->vd_curwindow->vw_number + 1;
2176 		vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2177 		/* XXX: other fields! */
2178 		return (0);
2179 	}
2180 	case CONS_GETVERS:
2181 		*(int *)data = 0x200;
2182 		return (0);
2183 	case CONS_MODEINFO:
2184 		/* XXX */
2185 		return (0);
2186 	case CONS_MOUSECTL: {
2187 		mouse_info_t *mouse = (mouse_info_t*)data;
2188 
2189 		/*
2190 		 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2191 		 * should not be applied to individual TTYs, but only to
2192 		 * consolectl.
2193 		 */
2194 		switch (mouse->operation) {
2195 		case MOUSE_HIDE:
2196 			if (vd->vd_flags & VDF_MOUSECURSOR) {
2197 				vd->vd_flags &= ~VDF_MOUSECURSOR;
2198 #ifndef SC_NO_CUTPASTE
2199 				vt_mouse_state(VT_MOUSE_HIDE);
2200 #endif
2201 			}
2202 			return (0);
2203 		case MOUSE_SHOW:
2204 			if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2205 				vd->vd_flags |= VDF_MOUSECURSOR;
2206 				vd->vd_mx = vd->vd_width / 2;
2207 				vd->vd_my = vd->vd_height / 2;
2208 #ifndef SC_NO_CUTPASTE
2209 				vt_mouse_state(VT_MOUSE_SHOW);
2210 #endif
2211 			}
2212 			return (0);
2213 		default:
2214 			return (EINVAL);
2215 		}
2216 	}
2217 	case PIO_VFONT: {
2218 		struct vt_font *vf;
2219 
2220 		if (vd->vd_flags & VDF_TEXTMODE)
2221 			return (ENOTSUP);
2222 
2223 		error = vtfont_load((void *)data, &vf);
2224 		if (error != 0)
2225 			return (error);
2226 
2227 		error = vt_change_font(vw, vf);
2228 		vtfont_unref(vf);
2229 		return (error);
2230 	}
2231 	case PIO_VFONT_DEFAULT: {
2232 		/* Reset to default font. */
2233 		error = vt_change_font(vw, &vt_font_default);
2234 		return (error);
2235 	}
2236 	case GIO_SCRNMAP: {
2237 		scrmap_t *sm = (scrmap_t *)data;
2238 
2239 		/* We don't have screen maps, so return a handcrafted one. */
2240 		for (i = 0; i < 256; i++)
2241 			sm->scrmap[i] = i;
2242 		return (0);
2243 	}
2244 	case KDSETMODE:
2245 		/*
2246 		 * FIXME: This implementation is incomplete compared to
2247 		 * syscons.
2248 		 */
2249 		switch (*(int *)data) {
2250 		case KD_TEXT:
2251 		case KD_TEXT1:
2252 		case KD_PIXEL:
2253 			vw->vw_flags &= ~VWF_GRAPHICS;
2254 			break;
2255 		case KD_GRAPHICS:
2256 			vw->vw_flags |= VWF_GRAPHICS;
2257 			break;
2258 		}
2259 		return (0);
2260 	case KDENABIO:      	/* allow io operations */
2261 		error = priv_check(td, PRIV_IO);
2262 		if (error != 0)
2263 			return (error);
2264 		error = securelevel_gt(td->td_ucred, 0);
2265 		if (error != 0)
2266 			return (error);
2267 #if defined(__i386__)
2268 		td->td_frame->tf_eflags |= PSL_IOPL;
2269 #elif defined(__amd64__)
2270 		td->td_frame->tf_rflags |= PSL_IOPL;
2271 #endif
2272 		return (0);
2273 	case KDDISABIO:     	/* disallow io operations (default) */
2274 #if defined(__i386__)
2275 		td->td_frame->tf_eflags &= ~PSL_IOPL;
2276 #elif defined(__amd64__)
2277 		td->td_frame->tf_rflags &= ~PSL_IOPL;
2278 #endif
2279 		return (0);
2280 	case KDMKTONE:      	/* sound the bell */
2281 		vtterm_beep(tm, *(u_int *)data);
2282 		return (0);
2283 	case KIOCSOUND:     	/* make tone (*data) hz */
2284 		/* TODO */
2285 		return (0);
2286 	case CONS_SETKBD: 		/* set the new keyboard */
2287 		mtx_lock(&Giant);
2288 		error = 0;
2289 		if (vd->vd_keyboard != *(int *)data) {
2290 			kbd = kbd_get_keyboard(*(int *)data);
2291 			if (kbd == NULL) {
2292 				mtx_unlock(&Giant);
2293 				return (EINVAL);
2294 			}
2295 			i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2296 			    (void *)vd, vt_kbdevent, vd);
2297 			if (i >= 0) {
2298 				if (vd->vd_keyboard != -1) {
2299 					vt_save_kbd_state(vd->vd_curwindow, kbd);
2300 					kbd_release(kbd, (void *)vd);
2301 				}
2302 				kbd = kbd_get_keyboard(i);
2303 				vd->vd_keyboard = i;
2304 
2305 				vt_update_kbd_mode(vd->vd_curwindow, kbd);
2306 				vt_update_kbd_state(vd->vd_curwindow, kbd);
2307 			} else {
2308 				error = EPERM;	/* XXX */
2309 			}
2310 		}
2311 		mtx_unlock(&Giant);
2312 		return (error);
2313 	case CONS_RELKBD: 		/* release the current keyboard */
2314 		mtx_lock(&Giant);
2315 		error = 0;
2316 		if (vd->vd_keyboard != -1) {
2317 			kbd = kbd_get_keyboard(vd->vd_keyboard);
2318 			if (kbd == NULL) {
2319 				mtx_unlock(&Giant);
2320 				return (EINVAL);
2321 			}
2322 			vt_save_kbd_state(vd->vd_curwindow, kbd);
2323 			error = kbd_release(kbd, (void *)vd);
2324 			if (error == 0) {
2325 				vd->vd_keyboard = -1;
2326 			}
2327 		}
2328 		mtx_unlock(&Giant);
2329 		return (error);
2330 	case VT_ACTIVATE: {
2331 		int win;
2332 		win = *(int *)data - 1;
2333 		DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2334 		    VT_UNIT(vw), win);
2335 		if ((win >= VT_MAXWINDOWS) || (win < 0))
2336 			return (EINVAL);
2337 		return (vt_proc_window_switch(vd->vd_windows[win]));
2338 	}
2339 	case VT_GETACTIVE:
2340 		*(int *)data = vd->vd_curwindow->vw_number + 1;
2341 		return (0);
2342 	case VT_GETINDEX:
2343 		*(int *)data = vw->vw_number + 1;
2344 		return (0);
2345 	case VT_LOCKSWITCH:
2346 		/* TODO: Check current state, switching can be in progress. */
2347 		if ((*(int *)data) == 0x01)
2348 			vw->vw_flags |= VWF_VTYLOCK;
2349 		else if ((*(int *)data) == 0x02)
2350 			vw->vw_flags &= ~VWF_VTYLOCK;
2351 		else
2352 			return (EINVAL);
2353 		return (0);
2354 	case VT_OPENQRY:
2355 		VT_LOCK(vd);
2356 		for (i = 0; i < VT_MAXWINDOWS; i++) {
2357 			vw = vd->vd_windows[i];
2358 			if (vw == NULL)
2359 				continue;
2360 			if (!(vw->vw_flags & VWF_OPENED)) {
2361 				*(int *)data = vw->vw_number + 1;
2362 				VT_UNLOCK(vd);
2363 				return (0);
2364 			}
2365 		}
2366 		VT_UNLOCK(vd);
2367 		return (EINVAL);
2368 	case VT_WAITACTIVE:
2369 		error = 0;
2370 
2371 		i = *(unsigned int *)data;
2372 		if (i > VT_MAXWINDOWS)
2373 			return (EINVAL);
2374 		if (i != 0)
2375 			vw = vd->vd_windows[i - 1];
2376 
2377 		VT_LOCK(vd);
2378 		while (vd->vd_curwindow != vw && error == 0)
2379 			error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2380 		VT_UNLOCK(vd);
2381 		return (error);
2382 	case VT_SETMODE: {    	/* set screen switcher mode */
2383 		struct vt_mode *mode;
2384 		struct proc *p1;
2385 
2386 		mode = (struct vt_mode *)data;
2387 		DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2388 		if (vw->vw_smode.mode == VT_PROCESS) {
2389 			p1 = pfind(vw->vw_pid);
2390 			if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2391 				if (p1)
2392 					PROC_UNLOCK(p1);
2393 				DPRINTF(5, "error EPERM\n");
2394 				return (EPERM);
2395 			}
2396 			if (p1)
2397 				PROC_UNLOCK(p1);
2398 		}
2399 		if (mode->mode == VT_AUTO) {
2400 			vw->vw_smode.mode = VT_AUTO;
2401 			vw->vw_proc = NULL;
2402 			vw->vw_pid = 0;
2403 			DPRINTF(5, "VT_AUTO, ");
2404 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2405 				cnavailable(vw->vw_terminal->consdev, TRUE);
2406 			/* were we in the middle of the vty switching process? */
2407 			if (finish_vt_rel(vw, TRUE, &s) == 0)
2408 				DPRINTF(5, "reset WAIT_REL, ");
2409 			if (finish_vt_acq(vw) == 0)
2410 				DPRINTF(5, "reset WAIT_ACQ, ");
2411 			return (0);
2412 		} else if (mode->mode == VT_PROCESS) {
2413 			if (!ISSIGVALID(mode->relsig) ||
2414 			    !ISSIGVALID(mode->acqsig) ||
2415 			    !ISSIGVALID(mode->frsig)) {
2416 				DPRINTF(5, "error EINVAL\n");
2417 				return (EINVAL);
2418 			}
2419 			DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2420 			bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2421 			vw->vw_proc = td->td_proc;
2422 			vw->vw_pid = vw->vw_proc->p_pid;
2423 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2424 				cnavailable(vw->vw_terminal->consdev, FALSE);
2425 		} else {
2426 			DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2427 			    mode->mode);
2428 			return (EINVAL);
2429 		}
2430 		DPRINTF(5, "\n");
2431 		return (0);
2432 	}
2433 	case VT_GETMODE:	/* get screen switcher mode */
2434 		bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2435 		return (0);
2436 
2437 	case VT_RELDISP:	/* screen switcher ioctl */
2438 		/*
2439 		 * This must be the current vty which is in the VT_PROCESS
2440 		 * switching mode...
2441 		 */
2442 		if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2443 		    VT_PROCESS)) {
2444 			return (EINVAL);
2445 		}
2446 		/* ...and this process is controlling it. */
2447 		if (vw->vw_proc != td->td_proc) {
2448 			return (EPERM);
2449 		}
2450 		error = EINVAL;
2451 		switch(*(int *)data) {
2452 		case VT_FALSE:	/* user refuses to release screen, abort */
2453 			if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2454 				DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2455 				    SC_DRIVER_NAME, VT_UNIT(vw));
2456 			break;
2457 		case VT_TRUE:	/* user has released screen, go on */
2458 			/* finish_vt_rel(..., TRUE, ...) should not be locked */
2459 			if (vw->vw_flags & VWF_SWWAIT_REL) {
2460 				if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2461 					DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2462 					    SC_DRIVER_NAME, VT_UNIT(vw));
2463 			} else {
2464 				error = EINVAL;
2465 			}
2466 			return (error);
2467 		case VT_ACKACQ:	/* acquire acknowledged, switch completed */
2468 			if ((error = finish_vt_acq(vw)) == 0)
2469 				DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2470 				    SC_DRIVER_NAME, VT_UNIT(vw));
2471 			break;
2472 		default:
2473 			break;
2474 		}
2475 		return (error);
2476 	}
2477 
2478 	return (ENOIOCTL);
2479 }
2480 
2481 static struct vt_window *
2482 vt_allocate_window(struct vt_device *vd, unsigned int window)
2483 {
2484 	struct vt_window *vw;
2485 	struct terminal *tm;
2486 	term_pos_t size;
2487 	struct winsize wsz;
2488 
2489 	vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2490 	vw->vw_device = vd;
2491 	vw->vw_number = window;
2492 	vw->vw_kbdmode = K_XLATE;
2493 
2494 	if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2495 		vw->vw_font = vtfont_ref(&vt_font_default);
2496 		vt_compute_drawable_area(vw);
2497 	}
2498 
2499 	vt_termsize(vd, vw->vw_font, &size);
2500 	vt_winsize(vd, vw->vw_font, &wsz);
2501 	vtbuf_init(&vw->vw_buf, &size);
2502 
2503 	tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2504 	terminal_set_winsize(tm, &wsz);
2505 	vd->vd_windows[window] = vw;
2506 	callout_init(&vw->vw_proc_dead_timer, 0);
2507 
2508 	return (vw);
2509 }
2510 
2511 void
2512 vt_upgrade(struct vt_device *vd)
2513 {
2514 	struct vt_window *vw;
2515 	unsigned int i;
2516 
2517 	if (!vty_enabled(VTY_VT))
2518 		return;
2519 	if (main_vd->vd_driver == NULL)
2520 		return;
2521 
2522 	for (i = 0; i < VT_MAXWINDOWS; i++) {
2523 		vw = vd->vd_windows[i];
2524 		if (vw == NULL) {
2525 			/* New window. */
2526 			vw = vt_allocate_window(vd, i);
2527 		}
2528 		if (!(vw->vw_flags & VWF_READY)) {
2529 			callout_init(&vw->vw_proc_dead_timer, 0);
2530 			terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2531 			vw->vw_flags |= VWF_READY;
2532 			if (vw->vw_flags & VWF_CONSOLE) {
2533 				/* For existing console window. */
2534 				EVENTHANDLER_REGISTER(shutdown_pre_sync,
2535 				    vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2536 			}
2537 		}
2538 
2539 	}
2540 	VT_LOCK(vd);
2541 	if (vd->vd_curwindow == NULL)
2542 		vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2543 
2544 	if (!(vd->vd_flags & VDF_ASYNC)) {
2545 		/* Attach keyboard. */
2546 		vt_allocate_keyboard(vd);
2547 
2548 		/* Init 25 Hz timer. */
2549 		callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2550 
2551 		/* Start timer when everything ready. */
2552 		vd->vd_flags |= VDF_ASYNC;
2553 		callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2554 		vd->vd_timer_armed = 1;
2555 	}
2556 
2557 	VT_UNLOCK(vd);
2558 
2559 	/* Refill settings with new sizes. */
2560 	vt_resize(vd);
2561 }
2562 
2563 static void
2564 vt_resize(struct vt_device *vd)
2565 {
2566 	struct vt_window *vw;
2567 	int i;
2568 
2569 	for (i = 0; i < VT_MAXWINDOWS; i++) {
2570 		vw = vd->vd_windows[i];
2571 		VT_LOCK(vd);
2572 		/* Assign default font to window, if not textmode. */
2573 		if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2574 			vw->vw_font = vtfont_ref(&vt_font_default);
2575 		VT_UNLOCK(vd);
2576 
2577 		/* Resize terminal windows */
2578 		while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2579 			DPRINTF(100, "%s: vt_change_font() is busy, "
2580 			    "window %d\n", __func__, i);
2581 		}
2582 	}
2583 }
2584 
2585 void
2586 vt_allocate(struct vt_driver *drv, void *softc)
2587 {
2588 	struct vt_device *vd;
2589 
2590 	if (!vty_enabled(VTY_VT))
2591 		return;
2592 
2593 	if (main_vd->vd_driver == NULL) {
2594 		main_vd->vd_driver = drv;
2595 		printf("VT: initialize with new VT driver \"%s\".\n",
2596 		    drv->vd_name);
2597 	} else {
2598 		/*
2599 		 * Check if have rights to replace current driver. For example:
2600 		 * it is bad idea to replace KMS driver with generic VGA one.
2601 		 */
2602 		if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2603 			printf("VT: Driver priority %d too low. Current %d\n ",
2604 			    drv->vd_priority, main_vd->vd_driver->vd_priority);
2605 			return;
2606 		}
2607 		printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2608 		    main_vd->vd_driver->vd_name, drv->vd_name);
2609 	}
2610 	vd = main_vd;
2611 
2612 	if (vd->vd_flags & VDF_ASYNC) {
2613 		/* Stop vt_flush periodic task. */
2614 		VT_LOCK(vd);
2615 		vt_suspend_flush_timer(vd);
2616 		VT_UNLOCK(vd);
2617 		/*
2618 		 * Mute current terminal until we done. vt_change_font (called
2619 		 * from vt_resize) will unmute it.
2620 		 */
2621 		terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2622 	}
2623 
2624 	/*
2625 	 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2626 	 * set it.
2627 	 */
2628 	VT_LOCK(vd);
2629 	vd->vd_flags &= ~VDF_TEXTMODE;
2630 
2631 	vd->vd_driver = drv;
2632 	vd->vd_softc = softc;
2633 	vd->vd_driver->vd_init(vd);
2634 	VT_UNLOCK(vd);
2635 
2636 	/* Update windows sizes and initialize last items. */
2637 	vt_upgrade(vd);
2638 
2639 #ifdef DEV_SPLASH
2640 	if (vd->vd_flags & VDF_SPLASH)
2641 		vtterm_splash(vd);
2642 #endif
2643 
2644 	if (vd->vd_flags & VDF_ASYNC) {
2645 		/* Allow to put chars now. */
2646 		terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2647 		/* Rerun timer for screen updates. */
2648 		vt_resume_flush_timer(vd, 0);
2649 	}
2650 
2651 	/*
2652 	 * Register as console. If it already registered, cnadd() will ignore
2653 	 * it.
2654 	 */
2655 	termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2656 }
2657 
2658 void
2659 vt_suspend()
2660 {
2661 
2662 	if (vt_suspendswitch == 0)
2663 		return;
2664 	/* Save current window. */
2665 	main_vd->vd_savedwindow = main_vd->vd_curwindow;
2666 	/* Ask holding process to free window and switch to console window */
2667 	vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2668 }
2669 
2670 void
2671 vt_resume()
2672 {
2673 
2674 	if (vt_suspendswitch == 0)
2675 		return;
2676 	/* Switch back to saved window */
2677 	if (main_vd->vd_savedwindow != NULL)
2678 		vt_proc_window_switch(main_vd->vd_savedwindow);
2679 	main_vd->vd_savedwindow = NULL;
2680 }
2681