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