xref: /freebsd/sys/dev/vt/vt_core.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 	for (unsigned i = 0; i < VFNT_MAPS; i++) {
1491 		if (fi->fi_map_count[i] == 0)
1492 			continue;
1493 		vfp->vf_map_count[i] = fi->fi_map_count[i];
1494 		vfp->vf_map[i] = (vfnt_map_t *)ptr;
1495 		ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t));
1496 		ptr = roundup2(ptr, 8);
1497 	}
1498 	vfp->vf_bytes = (uint8_t *)ptr;
1499 	return (vfp);
1500 }
1501 
1502 static struct vt_font *
1503 parse_font_info(struct font_info *fi)
1504 {
1505 	struct vt_font *vfp;
1506 	uintptr_t ptr;
1507 	uint32_t checksum;
1508 	size_t size;
1509 
1510 	if (fi == NULL)
1511 		return (NULL);
1512 
1513 	ptr = (uintptr_t)fi;
1514 	/*
1515 	 * Compute and verify checksum. The total sum of all the fields
1516 	 * must be 0.
1517 	 */
1518 	checksum = fi->fi_width;
1519 	checksum += fi->fi_height;
1520 	checksum += fi->fi_bitmap_size;
1521 	for (unsigned i = 0; i < VFNT_MAPS; i++)
1522 		checksum += fi->fi_map_count[i];
1523 
1524 	if (checksum + fi->fi_checksum != 0)
1525 		return (NULL);
1526 
1527 	ptr += sizeof(struct font_info);
1528 	ptr = roundup2(ptr, 8);
1529 
1530 	vfp = &vt_font_loader;
1531 	vfp->vf_height = fi->fi_height;
1532 	vfp->vf_width = fi->fi_width;
1533 	for (unsigned i = 0; i < VFNT_MAPS; i++) {
1534 		if (fi->fi_map_count[i] == 0)
1535 			continue;
1536 		vfp->vf_map_count[i] = fi->fi_map_count[i];
1537 		size = fi->fi_map_count[i] * sizeof(vfnt_map_t);
1538 		vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO);
1539 		bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size);
1540 		ptr += size;
1541 		ptr = roundup2(ptr, 8);
1542 	}
1543 	vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO);
1544 	bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size);
1545 	return (vfp);
1546 }
1547 
1548 static void
1549 vt_init_font(void *arg)
1550 {
1551 	caddr_t kmdp;
1552 	struct font_info *fi;
1553 	struct vt_font *font;
1554 
1555 	kmdp = preload_search_by_type("elf kernel");
1556 	if (kmdp == NULL)
1557 		kmdp = preload_search_by_type("elf64 kernel");
1558 	fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
1559 
1560 	font = parse_font_info(fi);
1561 	if (font != NULL)
1562 		vt_font_assigned = font;
1563 }
1564 
1565 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev);
1566 
1567 static void
1568 vt_init_font_static(void)
1569 {
1570 	caddr_t kmdp;
1571 	struct font_info *fi;
1572 	struct vt_font *font;
1573 
1574 	kmdp = preload_search_by_type("elf kernel");
1575 	if (kmdp == NULL)
1576 		kmdp = preload_search_by_type("elf64 kernel");
1577 	fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
1578 
1579 	font = parse_font_info_static(fi);
1580 	if (font != NULL)
1581 		vt_font_assigned = font;
1582 }
1583 
1584 static void
1585 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1586 {
1587 	struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1588 	struct vt_window *vw = tm->tm_softc;
1589 	struct vt_device *vd = vw->vw_device;
1590 	struct winsize wsz;
1591 	const term_attr_t *a;
1592 
1593 	if (!vty_enabled(VTY_VT))
1594 		return;
1595 
1596 	if (vd->vd_flags & VDF_INITIALIZED)
1597 		/* Initialization already done. */
1598 		return;
1599 
1600 	SET_FOREACH(vtdlist, vt_drv_set) {
1601 		vtd = *vtdlist;
1602 		if (vtd->vd_probe == NULL)
1603 			continue;
1604 		if (vtd->vd_probe(vd) == CN_DEAD)
1605 			continue;
1606 		if ((vtdbest == NULL) ||
1607 		    (vtd->vd_priority > vtdbest->vd_priority))
1608 			vtdbest = vtd;
1609 	}
1610 	if (vtdbest == NULL) {
1611 		cp->cn_pri = CN_DEAD;
1612 		vd->vd_flags |= VDF_DEAD;
1613 	} else {
1614 		vd->vd_driver = vtdbest;
1615 		cp->cn_pri = vd->vd_driver->vd_init(vd);
1616 	}
1617 
1618 	/* Check if driver's vt_init return CN_DEAD. */
1619 	if (cp->cn_pri == CN_DEAD) {
1620 		vd->vd_flags |= VDF_DEAD;
1621 	}
1622 
1623 	/* Initialize any early-boot keyboard drivers */
1624 	kbd_configure(KB_CONF_PROBE_ONLY);
1625 
1626 	vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1627 	vd->vd_windows[VT_CONSWINDOW] = vw;
1628 	sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1629 
1630 	vt_init_font_static();
1631 
1632 	/* Attach default font if not in TEXTMODE. */
1633 	if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1634 		vw->vw_font = vtfont_ref(vt_font_assigned);
1635 		vt_compute_drawable_area(vw);
1636 	}
1637 
1638 	/*
1639 	 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1640 	 * that we have the real viewable size, fix it in the static
1641 	 * buffer.
1642 	 */
1643 	if (vd->vd_width != 0 && vd->vd_height != 0)
1644 		vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1645 
1646 	/* We need to access terminal attributes from vtbuf */
1647 	vw->vw_buf.vb_terminal = tm;
1648 	vtbuf_init_early(&vw->vw_buf);
1649 	vt_winsize(vd, vw->vw_font, &wsz);
1650 	a = teken_get_curattr(&tm->tm_emulator);
1651 	terminal_set_winsize_blank(tm, &wsz, 1, a);
1652 
1653 	if (vtdbest != NULL) {
1654 #ifdef DEV_SPLASH
1655 		if (!vt_splash_cpu)
1656 			vtterm_splash(vd);
1657 #endif
1658 		vd->vd_flags |= VDF_INITIALIZED;
1659 	}
1660 }
1661 
1662 static int
1663 vtterm_cngetc(struct terminal *tm)
1664 {
1665 	struct vt_window *vw = tm->tm_softc;
1666 	struct vt_device *vd = vw->vw_device;
1667 	keyboard_t *kbd;
1668 	u_int c;
1669 
1670 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
1671 		return (*vw->vw_kbdsq++);
1672 
1673 	/* Make sure the splash screen is not there. */
1674 	if (vd->vd_flags & VDF_SPLASH) {
1675 		/* Remove splash */
1676 		vd->vd_flags &= ~VDF_SPLASH;
1677 		/* Mark screen as invalid to force update */
1678 		vd->vd_flags |= VDF_INVALID;
1679 		vt_flush(vd);
1680 	}
1681 
1682 	/* Stripped down keyboard handler. */
1683 	if ((kbd = vd->vd_keyboard) == NULL)
1684 		return (-1);
1685 
1686 	/* Force keyboard input mode to K_XLATE */
1687 	vw->vw_kbdmode = K_XLATE;
1688 	vt_update_kbd_mode(vw, kbd);
1689 
1690 	/* Switch the keyboard to polling to make it work here. */
1691 	kbdd_poll(kbd, TRUE);
1692 	c = kbdd_read_char(kbd, 0);
1693 	kbdd_poll(kbd, FALSE);
1694 	if (c & RELKEY)
1695 		return (-1);
1696 
1697 	if (vw->vw_flags & VWF_SCROLL) {
1698 		vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1699 		vt_flush(vd);
1700 		return (-1);
1701 	}
1702 
1703 	/* Stripped down handling of vt_kbdevent(), without locking, etc. */
1704 	if (c & SPCLKEY) {
1705 		switch (c) {
1706 		case SPCLKEY | SLK:
1707 			vt_save_kbd_state(vw, kbd);
1708 			if (vw->vw_kbdstate & SLKED) {
1709 				/* Turn scrolling on. */
1710 				vw->vw_flags |= VWF_SCROLL;
1711 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
1712 			} else {
1713 				/* Turn scrolling off. */
1714 				vt_scroll(vw, 0, VHS_END);
1715 				vw->vw_flags &= ~VWF_SCROLL;
1716 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
1717 			}
1718 			break;
1719 		/* XXX: KDB can handle history. */
1720 		case SPCLKEY | FKEY | F(50): /* Arrow up. */
1721 			vw->vw_kbdsq = "\x1b[A";
1722 			break;
1723 		case SPCLKEY | FKEY | F(58): /* Arrow down. */
1724 			vw->vw_kbdsq = "\x1b[B";
1725 			break;
1726 		case SPCLKEY | FKEY | F(55): /* Arrow right. */
1727 			vw->vw_kbdsq = "\x1b[C";
1728 			break;
1729 		case SPCLKEY | FKEY | F(53): /* Arrow left. */
1730 			vw->vw_kbdsq = "\x1b[D";
1731 			break;
1732 		}
1733 
1734 		/* Force refresh to make scrollback work. */
1735 		vt_flush(vd);
1736 	} else if (KEYFLAGS(c) == 0) {
1737 		return (KEYCHAR(c));
1738 	}
1739 
1740 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
1741 		return (*vw->vw_kbdsq++);
1742 
1743 	return (-1);
1744 }
1745 
1746 /*
1747  * These two do most of what we want to do in vtterm_cnungrab, but without
1748  * actually switching windows.  This is necessary for, e.g.,
1749  * vt_allocate_keyboard() to get the current keyboard into the state it needs to
1750  * be in without damaging the device's window state.
1751  *
1752  * Both return the current grab count, though it's only used in vtterm_cnungrab.
1753  */
1754 static int
1755 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw)
1756 {
1757 	keyboard_t *kbd;
1758 
1759 	if (vw->vw_grabbed++ > 0)
1760 		return (vw->vw_grabbed);
1761 
1762 	if ((kbd = vd->vd_keyboard) == NULL)
1763 		return (1);
1764 
1765 	/*
1766 	 * Make sure the keyboard is accessible even when the kbd device
1767 	 * driver is disabled.
1768 	 */
1769 	kbdd_enable(kbd);
1770 
1771 	/* We shall always use the keyboard in the XLATE mode here. */
1772 	vw->vw_prev_kbdmode = vw->vw_kbdmode;
1773 	vw->vw_kbdmode = K_XLATE;
1774 	vt_update_kbd_mode(vw, kbd);
1775 
1776 	kbdd_poll(kbd, TRUE);
1777 	return (1);
1778 }
1779 
1780 static int
1781 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw)
1782 {
1783 	keyboard_t *kbd;
1784 
1785 	if (--vw->vw_grabbed > 0)
1786 		return (vw->vw_grabbed);
1787 
1788 	if ((kbd = vd->vd_keyboard) == NULL)
1789 		return (0);
1790 
1791 	kbdd_poll(kbd, FALSE);
1792 
1793 	vw->vw_kbdmode = vw->vw_prev_kbdmode;
1794 	vt_update_kbd_mode(vw, kbd);
1795 	kbdd_disable(kbd);
1796 	return (0);
1797 }
1798 
1799 static void
1800 vtterm_cngrab(struct terminal *tm)
1801 {
1802 	struct vt_device *vd;
1803 	struct vt_window *vw;
1804 
1805 	vw = tm->tm_softc;
1806 	vd = vw->vw_device;
1807 
1808 	/* To be restored after we ungrab. */
1809 	if (vd->vd_grabwindow == NULL)
1810 		vd->vd_grabwindow = vd->vd_curwindow;
1811 
1812 	if (!cold)
1813 		vt_window_switch(vw);
1814 
1815 	vtterm_cngrab_noswitch(vd, vw);
1816 }
1817 
1818 static void
1819 vtterm_cnungrab(struct terminal *tm)
1820 {
1821 	struct vt_device *vd;
1822 	struct vt_window *vw;
1823 
1824 	vw = tm->tm_softc;
1825 	vd = vw->vw_device;
1826 
1827 	MPASS(vd->vd_grabwindow != NULL);
1828 	if (vtterm_cnungrab_noswitch(vd, vw) != 0)
1829 		return;
1830 
1831 	if (!cold && vd->vd_grabwindow != vw)
1832 		vt_window_switch(vd->vd_grabwindow);
1833 
1834 	vd->vd_grabwindow = NULL;
1835 }
1836 
1837 static void
1838 vtterm_opened(struct terminal *tm, int opened)
1839 {
1840 	struct vt_window *vw = tm->tm_softc;
1841 	struct vt_device *vd = vw->vw_device;
1842 
1843 	VT_LOCK(vd);
1844 	vd->vd_flags &= ~VDF_SPLASH;
1845 	if (opened)
1846 		vw->vw_flags |= VWF_OPENED;
1847 	else {
1848 		vw->vw_flags &= ~VWF_OPENED;
1849 		/* TODO: finish ACQ/REL */
1850 	}
1851 	VT_UNLOCK(vd);
1852 }
1853 
1854 static int
1855 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1856 {
1857 	struct vt_device *vd = vw->vw_device;
1858 	struct terminal *tm = vw->vw_terminal;
1859 	term_pos_t size;
1860 	struct winsize wsz;
1861 
1862 	/*
1863 	 * Changing fonts.
1864 	 *
1865 	 * Changing fonts is a little tricky.  We must prevent
1866 	 * simultaneous access to the device, so we must stop
1867 	 * the display timer and the terminal from accessing.
1868 	 * We need to switch fonts and grow our screen buffer.
1869 	 *
1870 	 * XXX: Right now the code uses terminal_mute() to
1871 	 * prevent data from reaching the console driver while
1872 	 * resizing the screen buffer.  This isn't elegant...
1873 	 */
1874 
1875 	VT_LOCK(vd);
1876 	if (vw->vw_flags & VWF_BUSY) {
1877 		/* Another process is changing the font. */
1878 		VT_UNLOCK(vd);
1879 		return (EBUSY);
1880 	}
1881 	vw->vw_flags |= VWF_BUSY;
1882 	VT_UNLOCK(vd);
1883 
1884 	vt_termsize(vd, vf, &size);
1885 	vt_winsize(vd, vf, &wsz);
1886 
1887 	/* Grow the screen buffer and terminal. */
1888 	terminal_mute(tm, 1);
1889 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1890 	terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1891 	terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1892 	terminal_mute(tm, 0);
1893 
1894 	/* Actually apply the font to the current window. */
1895 	VT_LOCK(vd);
1896 	if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1897 		/*
1898 		 * In case vt_change_font called to update size we don't need
1899 		 * to update font link.
1900 		 */
1901 		vtfont_unref(vw->vw_font);
1902 		vw->vw_font = vtfont_ref(vf);
1903 	}
1904 
1905 	/*
1906 	 * Compute the drawable area and move the mouse cursor inside
1907 	 * it, in case the new area is smaller than the previous one.
1908 	 */
1909 	vt_compute_drawable_area(vw);
1910 	vd->vd_mx = min(vd->vd_mx,
1911 	    vw->vw_draw_area.tr_end.tp_col -
1912 	    vw->vw_draw_area.tr_begin.tp_col - 1);
1913 	vd->vd_my = min(vd->vd_my,
1914 	    vw->vw_draw_area.tr_end.tp_row -
1915 	    vw->vw_draw_area.tr_begin.tp_row - 1);
1916 
1917 	/* Force a full redraw the next timer tick. */
1918 	if (vd->vd_curwindow == vw) {
1919 		vd->vd_flags |= VDF_INVALID;
1920 		vt_resume_flush_timer(vw, 0);
1921 	}
1922 	vw->vw_flags &= ~VWF_BUSY;
1923 	VT_UNLOCK(vd);
1924 	return (0);
1925 }
1926 
1927 static int
1928 vt_proc_alive(struct vt_window *vw)
1929 {
1930 	struct proc *p;
1931 
1932 	if (vw->vw_smode.mode != VT_PROCESS)
1933 		return (FALSE);
1934 
1935 	if (vw->vw_proc) {
1936 		if ((p = pfind(vw->vw_pid)) != NULL)
1937 			PROC_UNLOCK(p);
1938 		if (vw->vw_proc == p)
1939 			return (TRUE);
1940 		vw->vw_proc = NULL;
1941 		vw->vw_smode.mode = VT_AUTO;
1942 		DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1943 		vw->vw_pid = 0;
1944 	}
1945 	return (FALSE);
1946 }
1947 
1948 static int
1949 signal_vt_rel(struct vt_window *vw)
1950 {
1951 
1952 	if (vw->vw_smode.mode != VT_PROCESS)
1953 		return (FALSE);
1954 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1955 		vw->vw_proc = NULL;
1956 		vw->vw_pid = 0;
1957 		return (TRUE);
1958 	}
1959 	vw->vw_flags |= VWF_SWWAIT_REL;
1960 	PROC_LOCK(vw->vw_proc);
1961 	kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1962 	PROC_UNLOCK(vw->vw_proc);
1963 	DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1964 	return (TRUE);
1965 }
1966 
1967 static int
1968 signal_vt_acq(struct vt_window *vw)
1969 {
1970 
1971 	if (vw->vw_smode.mode != VT_PROCESS)
1972 		return (FALSE);
1973 	if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1974 		cnavailable(vw->vw_terminal->consdev, FALSE);
1975 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1976 		vw->vw_proc = NULL;
1977 		vw->vw_pid = 0;
1978 		return (TRUE);
1979 	}
1980 	vw->vw_flags |= VWF_SWWAIT_ACQ;
1981 	PROC_LOCK(vw->vw_proc);
1982 	kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1983 	PROC_UNLOCK(vw->vw_proc);
1984 	DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1985 	return (TRUE);
1986 }
1987 
1988 static int
1989 finish_vt_rel(struct vt_window *vw, int release, int *s)
1990 {
1991 
1992 	if (vw->vw_flags & VWF_SWWAIT_REL) {
1993 		vw->vw_flags &= ~VWF_SWWAIT_REL;
1994 		if (release) {
1995 			callout_drain(&vw->vw_proc_dead_timer);
1996 			(void)vt_late_window_switch(vw->vw_switch_to);
1997 		}
1998 		return (0);
1999 	}
2000 	return (EINVAL);
2001 }
2002 
2003 static int
2004 finish_vt_acq(struct vt_window *vw)
2005 {
2006 
2007 	if (vw->vw_flags & VWF_SWWAIT_ACQ) {
2008 		vw->vw_flags &= ~VWF_SWWAIT_ACQ;
2009 		return (0);
2010 	}
2011 	return (EINVAL);
2012 }
2013 
2014 #ifndef SC_NO_CUTPASTE
2015 static void
2016 vt_mouse_terminput_button(struct vt_device *vd, int button)
2017 {
2018 	struct vt_window *vw;
2019 	struct vt_font *vf;
2020 	char mouseb[6] = "\x1B[M";
2021 	int i, x, y;
2022 
2023 	vw = vd->vd_curwindow;
2024 	vf = vw->vw_font;
2025 
2026 	/* Translate to char position. */
2027 	x = vd->vd_mx / vf->vf_width;
2028 	y = vd->vd_my / vf->vf_height;
2029 	/* Avoid overflow. */
2030 	x = MIN(x, 255 - '!');
2031 	y = MIN(y, 255 - '!');
2032 
2033 	mouseb[3] = ' ' + button;
2034 	mouseb[4] = '!' + x;
2035 	mouseb[5] = '!' + y;
2036 
2037 	for (i = 0; i < sizeof(mouseb); i++)
2038 		terminal_input_char(vw->vw_terminal, mouseb[i]);
2039 }
2040 
2041 static void
2042 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
2043     int cnt)
2044 {
2045 
2046 	switch (type) {
2047 	case MOUSE_BUTTON_EVENT:
2048 		if (cnt > 0) {
2049 			/* Mouse button pressed. */
2050 			if (event & MOUSE_BUTTON1DOWN)
2051 				vt_mouse_terminput_button(vd, 0);
2052 			if (event & MOUSE_BUTTON2DOWN)
2053 				vt_mouse_terminput_button(vd, 1);
2054 			if (event & MOUSE_BUTTON3DOWN)
2055 				vt_mouse_terminput_button(vd, 2);
2056 		} else {
2057 			/* Mouse button released. */
2058 			vt_mouse_terminput_button(vd, 3);
2059 		}
2060 		break;
2061 #ifdef notyet
2062 	case MOUSE_MOTION_EVENT:
2063 		if (mouse->u.data.z < 0) {
2064 			/* Scroll up. */
2065 			sc_mouse_input_button(vd, 64);
2066 		} else if (mouse->u.data.z > 0) {
2067 			/* Scroll down. */
2068 			sc_mouse_input_button(vd, 65);
2069 		}
2070 		break;
2071 #endif
2072 	}
2073 }
2074 
2075 static void
2076 vt_mouse_paste()
2077 {
2078 	term_char_t *buf;
2079 	int i, len;
2080 
2081 	len = VD_PASTEBUFLEN(main_vd);
2082 	buf = VD_PASTEBUF(main_vd);
2083 	len /= sizeof(term_char_t);
2084 	for (i = 0; i < len; i++) {
2085 		if (buf[i] == '\0')
2086 			continue;
2087 		terminal_input_char(main_vd->vd_curwindow->vw_terminal,
2088 		    buf[i]);
2089 	}
2090 }
2091 
2092 void
2093 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
2094 {
2095 	struct vt_device *vd;
2096 	struct vt_window *vw;
2097 	struct vt_font *vf;
2098 	term_pos_t size;
2099 	int len, mark;
2100 
2101 	vd = main_vd;
2102 	vw = vd->vd_curwindow;
2103 	vf = vw->vw_font;
2104 	mark = 0;
2105 
2106 	if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
2107 		/*
2108 		 * Either the mouse is disabled, or the window is in
2109 		 * "graphics mode". The graphics mode is usually set by
2110 		 * an X server, using the KDSETMODE ioctl.
2111 		 */
2112 		return;
2113 
2114 	if (vf == NULL)	/* Text mode. */
2115 		return;
2116 
2117 	/*
2118 	 * TODO: add flag about pointer position changed, to not redraw chars
2119 	 * under mouse pointer when nothing changed.
2120 	 */
2121 
2122 	if (vw->vw_mouse_level > 0)
2123 		vt_mouse_terminput(vd, type, x, y, event, cnt);
2124 
2125 	switch (type) {
2126 	case MOUSE_ACTION:
2127 	case MOUSE_MOTION_EVENT:
2128 		/* Movement */
2129 		x += vd->vd_mx;
2130 		y += vd->vd_my;
2131 
2132 		vt_termsize(vd, vf, &size);
2133 
2134 		/* Apply limits. */
2135 		x = MAX(x, 0);
2136 		y = MAX(y, 0);
2137 		x = MIN(x, (size.tp_col * vf->vf_width) - 1);
2138 		y = MIN(y, (size.tp_row * vf->vf_height) - 1);
2139 
2140 		vd->vd_mx = x;
2141 		vd->vd_my = y;
2142 		if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
2143 			vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
2144 			    vd->vd_mx / vf->vf_width,
2145 			    vd->vd_my / vf->vf_height);
2146 
2147 		vt_resume_flush_timer(vw, 0);
2148 		return; /* Done */
2149 	case MOUSE_BUTTON_EVENT:
2150 		/* Buttons */
2151 		break;
2152 	default:
2153 		return; /* Done */
2154 	}
2155 
2156 	switch (event) {
2157 	case MOUSE_BUTTON1DOWN:
2158 		switch (cnt % 4) {
2159 		case 0:	/* up */
2160 			mark = VTB_MARK_END;
2161 			break;
2162 		case 1: /* single click: start cut operation */
2163 			mark = VTB_MARK_START;
2164 			break;
2165 		case 2:	/* double click: cut a word */
2166 			mark = VTB_MARK_WORD;
2167 			break;
2168 		case 3:	/* triple click: cut a line */
2169 			mark = VTB_MARK_ROW;
2170 			break;
2171 		}
2172 		break;
2173 	case VT_MOUSE_PASTEBUTTON:
2174 		switch (cnt) {
2175 		case 0:	/* up */
2176 			break;
2177 		default:
2178 			vt_mouse_paste();
2179 			break;
2180 		}
2181 		return; /* Done */
2182 	case VT_MOUSE_EXTENDBUTTON:
2183 		switch (cnt) {
2184 		case 0:	/* up */
2185 			if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
2186 				mark = VTB_MARK_EXTEND;
2187 			else
2188 				mark = 0;
2189 			break;
2190 		default:
2191 			mark = VTB_MARK_EXTEND;
2192 			break;
2193 		}
2194 		break;
2195 	default:
2196 		return; /* Done */
2197 	}
2198 
2199 	/* Save buttons state. */
2200 	if (cnt > 0)
2201 		vd->vd_mstate |= event;
2202 	else
2203 		vd->vd_mstate &= ~event;
2204 
2205 	if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
2206 	    vd->vd_my / vf->vf_height) == 1) {
2207 		/*
2208 		 * We have something marked to copy, so update pointer to
2209 		 * window with selection.
2210 		 */
2211 		vt_resume_flush_timer(vw, 0);
2212 
2213 		switch (mark) {
2214 		case VTB_MARK_END:
2215 		case VTB_MARK_WORD:
2216 		case VTB_MARK_ROW:
2217 		case VTB_MARK_EXTEND:
2218 			break;
2219 		default:
2220 			/* Other types of mark do not require to copy data. */
2221 			return;
2222 		}
2223 
2224 		/* Get current selection size in bytes. */
2225 		len = vtbuf_get_marked_len(&vw->vw_buf);
2226 		if (len <= 0)
2227 			return;
2228 
2229 		/* Reallocate buffer only if old one is too small. */
2230 		if (len > VD_PASTEBUFSZ(vd)) {
2231 			VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2232 			    M_WAITOK | M_ZERO);
2233 			/* Update buffer size. */
2234 			VD_PASTEBUFSZ(vd) = len;
2235 		}
2236 		/* Request copy/paste buffer data, no more than `len' */
2237 		vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
2238 		    VD_PASTEBUFSZ(vd));
2239 
2240 		VD_PASTEBUFLEN(vd) = len;
2241 
2242 		/* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2243 	}
2244 }
2245 
2246 void
2247 vt_mouse_state(int show)
2248 {
2249 	struct vt_device *vd;
2250 	struct vt_window *vw;
2251 
2252 	vd = main_vd;
2253 	vw = vd->vd_curwindow;
2254 
2255 	switch (show) {
2256 	case VT_MOUSE_HIDE:
2257 		vw->vw_flags |= VWF_MOUSE_HIDE;
2258 		break;
2259 	case VT_MOUSE_SHOW:
2260 		vw->vw_flags &= ~VWF_MOUSE_HIDE;
2261 		break;
2262 	}
2263 
2264 	/* Mark mouse position as dirty. */
2265 	vt_mark_mouse_position_as_dirty(vd, false);
2266 	vt_resume_flush_timer(vw, 0);
2267 }
2268 #endif
2269 
2270 static int
2271 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2272     int nprot, vm_memattr_t *memattr)
2273 {
2274 	struct vt_window *vw = tm->tm_softc;
2275 	struct vt_device *vd = vw->vw_device;
2276 
2277 	if (vd->vd_driver->vd_fb_mmap)
2278 		return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2279 		    memattr));
2280 
2281 	return (ENXIO);
2282 }
2283 
2284 static int
2285 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2286     struct thread *td)
2287 {
2288 	struct vt_window *vw = tm->tm_softc;
2289 	struct vt_device *vd = vw->vw_device;
2290 	keyboard_t *kbd;
2291 	int error, i, s;
2292 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2293     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2294 	int ival;
2295 
2296 	switch (cmd) {
2297 	case _IO('v', 4):
2298 		cmd = VT_RELDISP;
2299 		break;
2300 	case _IO('v', 5):
2301 		cmd = VT_ACTIVATE;
2302 		break;
2303 	case _IO('v', 6):
2304 		cmd = VT_WAITACTIVE;
2305 		break;
2306 	case _IO('K', 20):
2307 		cmd = KDSKBSTATE;
2308 		break;
2309 	case _IO('K', 67):
2310 		cmd = KDSETRAD;
2311 		break;
2312 	case _IO('K', 7):
2313 		cmd = KDSKBMODE;
2314 		break;
2315 	case _IO('K', 8):
2316 		cmd = KDMKTONE;
2317 		break;
2318 	case _IO('K', 10):
2319 		cmd = KDSETMODE;
2320 		break;
2321 	case _IO('K', 13):
2322 		cmd = KDSBORDER;
2323 		break;
2324 	case _IO('K', 63):
2325 		cmd = KIOCSOUND;
2326 		break;
2327 	case _IO('K', 66):
2328 		cmd = KDSETLED;
2329 		break;
2330 	case _IO('c', 104):
2331 		cmd = CONS_SETWINORG;
2332 		break;
2333 	case _IO('c', 110):
2334 		cmd = CONS_SETKBD;
2335 		break;
2336 	default:
2337 		goto skip_thunk;
2338 	}
2339 	ival = IOCPARM_IVAL(data);
2340 	data = (caddr_t)&ival;
2341 skip_thunk:
2342 #endif
2343 
2344 	switch (cmd) {
2345 	case KDSETRAD:		/* set keyboard repeat & delay rates (old) */
2346 		if (*(int *)data & ~0x7f)
2347 			return (EINVAL);
2348 		/* FALLTHROUGH */
2349 	case GIO_KEYMAP:
2350 	case PIO_KEYMAP:
2351 	case GIO_DEADKEYMAP:
2352 	case PIO_DEADKEYMAP:
2353 	case GETFKEY:
2354 	case SETFKEY:
2355 	case KDGKBINFO:
2356 	case KDGKBTYPE:
2357 	case KDGETREPEAT:	/* get keyboard repeat & delay rates */
2358 	case KDSETREPEAT:	/* set keyboard repeat & delay rates (new) */
2359 	case KBADDKBD:		/* add/remove keyboard to/from mux */
2360 	case KBRELKBD: {
2361 		error = 0;
2362 
2363 		mtx_lock(&Giant);
2364 		if ((kbd = vd->vd_keyboard) != NULL)
2365 			error = kbdd_ioctl(kbd, cmd, data);
2366 		mtx_unlock(&Giant);
2367 		if (error == ENOIOCTL) {
2368 			if (cmd == KDGKBTYPE) {
2369 				/* always return something? XXX */
2370 				*(int *)data = 0;
2371 			} else {
2372 				return (ENODEV);
2373 			}
2374 		}
2375 		return (error);
2376 	}
2377 	case KDGKBSTATE: {	/* get keyboard state (locks) */
2378 		error = 0;
2379 
2380 		if (vw == vd->vd_curwindow) {
2381 			mtx_lock(&Giant);
2382 			if ((kbd = vd->vd_keyboard) != NULL)
2383 				error = vt_save_kbd_state(vw, kbd);
2384 			mtx_unlock(&Giant);
2385 
2386 			if (error != 0)
2387 				return (error);
2388 		}
2389 
2390 		*(int *)data = vw->vw_kbdstate & LOCK_MASK;
2391 
2392 		return (error);
2393 	}
2394 	case KDSKBSTATE: {	/* set keyboard state (locks) */
2395 		int state;
2396 
2397 		state = *(int *)data;
2398 		if (state & ~LOCK_MASK)
2399 			return (EINVAL);
2400 
2401 		vw->vw_kbdstate &= ~LOCK_MASK;
2402 		vw->vw_kbdstate |= state;
2403 
2404 		error = 0;
2405 		if (vw == vd->vd_curwindow) {
2406 			mtx_lock(&Giant);
2407 			if ((kbd = vd->vd_keyboard) != NULL)
2408 				error = vt_update_kbd_state(vw, kbd);
2409 			mtx_unlock(&Giant);
2410 		}
2411 
2412 		return (error);
2413 	}
2414 	case KDGETLED: {	/* get keyboard LED status */
2415 		error = 0;
2416 
2417 		if (vw == vd->vd_curwindow) {
2418 			mtx_lock(&Giant);
2419 			if ((kbd = vd->vd_keyboard) != NULL)
2420 				error = vt_save_kbd_leds(vw, kbd);
2421 			mtx_unlock(&Giant);
2422 
2423 			if (error != 0)
2424 				return (error);
2425 		}
2426 
2427 		*(int *)data = vw->vw_kbdstate & LED_MASK;
2428 
2429 		return (error);
2430 	}
2431 	case KDSETLED: {	/* set keyboard LED status */
2432 		int leds;
2433 
2434 		leds = *(int *)data;
2435 		if (leds & ~LED_MASK)
2436 			return (EINVAL);
2437 
2438 		vw->vw_kbdstate &= ~LED_MASK;
2439 		vw->vw_kbdstate |= leds;
2440 
2441 		error = 0;
2442 		if (vw == vd->vd_curwindow) {
2443 			mtx_lock(&Giant);
2444 			if ((kbd = vd->vd_keyboard) != NULL)
2445 				error = vt_update_kbd_leds(vw, kbd);
2446 			mtx_unlock(&Giant);
2447 		}
2448 
2449 		return (error);
2450 	}
2451 	case KDGETMODE:
2452 		*(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2453 		    KD_GRAPHICS : KD_TEXT;
2454 		return (0);
2455 	case KDGKBMODE: {
2456 		error = 0;
2457 
2458 		if (vw == vd->vd_curwindow) {
2459 			mtx_lock(&Giant);
2460 			if ((kbd = vd->vd_keyboard) != NULL)
2461 				error = vt_save_kbd_mode(vw, kbd);
2462 			mtx_unlock(&Giant);
2463 
2464 			if (error != 0)
2465 				return (error);
2466 		}
2467 
2468 		*(int *)data = vw->vw_kbdmode;
2469 
2470 		return (error);
2471 	}
2472 	case KDSKBMODE: {
2473 		int mode;
2474 
2475 		mode = *(int *)data;
2476 		switch (mode) {
2477 		case K_XLATE:
2478 		case K_RAW:
2479 		case K_CODE:
2480 			vw->vw_kbdmode = mode;
2481 
2482 			error = 0;
2483 			if (vw == vd->vd_curwindow) {
2484 				mtx_lock(&Giant);
2485 				if ((kbd = vd->vd_keyboard) != NULL)
2486 					error = vt_update_kbd_mode(vw, kbd);
2487 				mtx_unlock(&Giant);
2488 			}
2489 
2490 			return (error);
2491 		default:
2492 			return (EINVAL);
2493 		}
2494 	}
2495 	case FBIOGTYPE:
2496 	case FBIO_GETWINORG:	/* get frame buffer window origin */
2497 	case FBIO_GETDISPSTART:	/* get display start address */
2498 	case FBIO_GETLINEWIDTH:	/* get scan line width in bytes */
2499 	case FBIO_BLANK:	/* blank display */
2500 		if (vd->vd_driver->vd_fb_ioctl)
2501 			return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2502 		break;
2503 	case CONS_BLANKTIME:
2504 		/* XXX */
2505 		return (0);
2506 	case CONS_HISTORY:
2507 		if (*(int *)data < 0)
2508 			return EINVAL;
2509 		if (*(int *)data != vw->vw_buf.vb_history_size)
2510 			vtbuf_sethistory_size(&vw->vw_buf, *(int *)data);
2511 		return (0);
2512 	case CONS_CLRHIST:
2513 		vtbuf_clearhistory(&vw->vw_buf);
2514 		/*
2515 		 * Invalidate the entire visible window; it is not guaranteed
2516 		 * that this operation will be immediately followed by a scroll
2517 		 * event, so it would otherwise be possible for prior artifacts
2518 		 * to remain visible.
2519 		 */
2520 		VT_LOCK(vd);
2521 		if (vw == vd->vd_curwindow) {
2522 			vd->vd_flags |= VDF_INVALID;
2523 			vt_resume_flush_timer(vw, 0);
2524 		}
2525 		VT_UNLOCK(vd);
2526 		return (0);
2527 	case CONS_GET:
2528 		/* XXX */
2529 		*(int *)data = M_CG640x480;
2530 		return (0);
2531 	case CONS_BELLTYPE:	/* set bell type sound */
2532 		if ((*(int *)data) & CONS_QUIET_BELL)
2533 			vd->vd_flags |= VDF_QUIET_BELL;
2534 		else
2535 			vd->vd_flags &= ~VDF_QUIET_BELL;
2536 		return (0);
2537 	case CONS_GETINFO: {
2538 		vid_info_t *vi = (vid_info_t *)data;
2539 		if (vi->size != sizeof(struct vid_info))
2540 			return (EINVAL);
2541 
2542 		if (vw == vd->vd_curwindow) {
2543 			mtx_lock(&Giant);
2544 			if ((kbd = vd->vd_keyboard) != NULL)
2545 				vt_save_kbd_state(vw, kbd);
2546 			mtx_unlock(&Giant);
2547 		}
2548 
2549 		vi->m_num = vd->vd_curwindow->vw_number + 1;
2550 		vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2551 		/* XXX: other fields! */
2552 		return (0);
2553 	}
2554 	case CONS_GETVERS:
2555 		*(int *)data = 0x200;
2556 		return (0);
2557 	case CONS_MODEINFO:
2558 		/* XXX */
2559 		return (0);
2560 	case CONS_MOUSECTL: {
2561 		mouse_info_t *mouse = (mouse_info_t*)data;
2562 
2563 		/*
2564 		 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2565 		 * should not be applied to individual TTYs, but only to
2566 		 * consolectl.
2567 		 */
2568 		switch (mouse->operation) {
2569 		case MOUSE_HIDE:
2570 			if (vd->vd_flags & VDF_MOUSECURSOR) {
2571 				vd->vd_flags &= ~VDF_MOUSECURSOR;
2572 #ifndef SC_NO_CUTPASTE
2573 				vt_mouse_state(VT_MOUSE_HIDE);
2574 #endif
2575 			}
2576 			return (0);
2577 		case MOUSE_SHOW:
2578 			if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2579 				vd->vd_flags |= VDF_MOUSECURSOR;
2580 				vd->vd_mx = vd->vd_width / 2;
2581 				vd->vd_my = vd->vd_height / 2;
2582 #ifndef SC_NO_CUTPASTE
2583 				vt_mouse_state(VT_MOUSE_SHOW);
2584 #endif
2585 			}
2586 			return (0);
2587 		default:
2588 			return (EINVAL);
2589 		}
2590 	}
2591 	case PIO_VFONT: {
2592 		struct vt_font *vf;
2593 
2594 		if (vd->vd_flags & VDF_TEXTMODE)
2595 			return (ENOTSUP);
2596 
2597 		error = vtfont_load((void *)data, &vf);
2598 		if (error != 0)
2599 			return (error);
2600 
2601 		error = vt_change_font(vw, vf);
2602 		vtfont_unref(vf);
2603 		return (error);
2604 	}
2605 	case PIO_VFONT_DEFAULT: {
2606 		/* Reset to default font. */
2607 		error = vt_change_font(vw, vt_font_assigned);
2608 		return (error);
2609 	}
2610 	case GIO_SCRNMAP: {
2611 		scrmap_t *sm = (scrmap_t *)data;
2612 
2613 		/* We don't have screen maps, so return a handcrafted one. */
2614 		for (i = 0; i < 256; i++)
2615 			sm->scrmap[i] = i;
2616 		return (0);
2617 	}
2618 	case KDSETMODE:
2619 		/*
2620 		 * FIXME: This implementation is incomplete compared to
2621 		 * syscons.
2622 		 */
2623 		switch (*(int *)data) {
2624 		case KD_TEXT:
2625 		case KD_TEXT1:
2626 		case KD_PIXEL:
2627 			vw->vw_flags &= ~VWF_GRAPHICS;
2628 			break;
2629 		case KD_GRAPHICS:
2630 			vw->vw_flags |= VWF_GRAPHICS;
2631 			break;
2632 		}
2633 		return (0);
2634 	case KDENABIO:		/* allow io operations */
2635 		error = priv_check(td, PRIV_IO);
2636 		if (error != 0)
2637 			return (error);
2638 		error = securelevel_gt(td->td_ucred, 0);
2639 		if (error != 0)
2640 			return (error);
2641 #if defined(__i386__)
2642 		td->td_frame->tf_eflags |= PSL_IOPL;
2643 #elif defined(__amd64__)
2644 		td->td_frame->tf_rflags |= PSL_IOPL;
2645 #endif
2646 		return (0);
2647 	case KDDISABIO:		/* disallow io operations (default) */
2648 #if defined(__i386__)
2649 		td->td_frame->tf_eflags &= ~PSL_IOPL;
2650 #elif defined(__amd64__)
2651 		td->td_frame->tf_rflags &= ~PSL_IOPL;
2652 #endif
2653 		return (0);
2654 	case KDMKTONE:		/* sound the bell */
2655 		vtterm_beep(tm, *(u_int *)data);
2656 		return (0);
2657 	case KIOCSOUND:		/* make tone (*data) hz */
2658 		/* TODO */
2659 		return (0);
2660 	case CONS_SETKBD:	/* set the new keyboard */
2661 		mtx_lock(&Giant);
2662 		error = 0;
2663 		if (vd->vd_keyboard == NULL ||
2664 		    vd->vd_keyboard->kb_index != *(int *)data) {
2665 			kbd = kbd_get_keyboard(*(int *)data);
2666 			if (kbd == NULL) {
2667 				mtx_unlock(&Giant);
2668 				return (EINVAL);
2669 			}
2670 			i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2671 			    (void *)vd, vt_kbdevent, vd);
2672 			if (i >= 0) {
2673 				if ((kbd = vd->vd_keyboard) != NULL) {
2674 					vt_save_kbd_state(vd->vd_curwindow, kbd);
2675 					kbd_release(kbd, (void *)vd);
2676 				}
2677 				kbd = vd->vd_keyboard = kbd_get_keyboard(i);
2678 
2679 				vt_update_kbd_mode(vd->vd_curwindow, kbd);
2680 				vt_update_kbd_state(vd->vd_curwindow, kbd);
2681 			} else {
2682 				error = EPERM;	/* XXX */
2683 			}
2684 		}
2685 		mtx_unlock(&Giant);
2686 		return (error);
2687 	case CONS_RELKBD:	/* release the current keyboard */
2688 		mtx_lock(&Giant);
2689 		error = 0;
2690 		if ((kbd = vd->vd_keyboard) != NULL) {
2691 			vt_save_kbd_state(vd->vd_curwindow, kbd);
2692 			error = kbd_release(kbd, (void *)vd);
2693 			if (error == 0) {
2694 				vd->vd_keyboard = NULL;
2695 			}
2696 		}
2697 		mtx_unlock(&Giant);
2698 		return (error);
2699 	case VT_ACTIVATE: {
2700 		int win;
2701 		win = *(int *)data - 1;
2702 		DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2703 		    VT_UNIT(vw), win);
2704 		if ((win >= VT_MAXWINDOWS) || (win < 0))
2705 			return (EINVAL);
2706 		return (vt_proc_window_switch(vd->vd_windows[win]));
2707 	}
2708 	case VT_GETACTIVE:
2709 		*(int *)data = vd->vd_curwindow->vw_number + 1;
2710 		return (0);
2711 	case VT_GETINDEX:
2712 		*(int *)data = vw->vw_number + 1;
2713 		return (0);
2714 	case VT_LOCKSWITCH:
2715 		/* TODO: Check current state, switching can be in progress. */
2716 		if ((*(int *)data) == 0x01)
2717 			vw->vw_flags |= VWF_VTYLOCK;
2718 		else if ((*(int *)data) == 0x02)
2719 			vw->vw_flags &= ~VWF_VTYLOCK;
2720 		else
2721 			return (EINVAL);
2722 		return (0);
2723 	case VT_OPENQRY:
2724 		VT_LOCK(vd);
2725 		for (i = 0; i < VT_MAXWINDOWS; i++) {
2726 			vw = vd->vd_windows[i];
2727 			if (vw == NULL)
2728 				continue;
2729 			if (!(vw->vw_flags & VWF_OPENED)) {
2730 				*(int *)data = vw->vw_number + 1;
2731 				VT_UNLOCK(vd);
2732 				return (0);
2733 			}
2734 		}
2735 		VT_UNLOCK(vd);
2736 		return (EINVAL);
2737 	case VT_WAITACTIVE: {
2738 		unsigned int idx;
2739 
2740 		error = 0;
2741 
2742 		idx = *(unsigned int *)data;
2743 		if (idx > VT_MAXWINDOWS)
2744 			return (EINVAL);
2745 		if (idx > 0)
2746 			vw = vd->vd_windows[idx - 1];
2747 
2748 		VT_LOCK(vd);
2749 		while (vd->vd_curwindow != vw && error == 0)
2750 			error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2751 		VT_UNLOCK(vd);
2752 		return (error);
2753 	}
2754 	case VT_SETMODE: {	/* set screen switcher mode */
2755 		struct vt_mode *mode;
2756 		struct proc *p1;
2757 
2758 		mode = (struct vt_mode *)data;
2759 		DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2760 		if (vw->vw_smode.mode == VT_PROCESS) {
2761 			p1 = pfind(vw->vw_pid);
2762 			if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2763 				if (p1)
2764 					PROC_UNLOCK(p1);
2765 				DPRINTF(5, "error EPERM\n");
2766 				return (EPERM);
2767 			}
2768 			if (p1)
2769 				PROC_UNLOCK(p1);
2770 		}
2771 		if (mode->mode == VT_AUTO) {
2772 			vw->vw_smode.mode = VT_AUTO;
2773 			vw->vw_proc = NULL;
2774 			vw->vw_pid = 0;
2775 			DPRINTF(5, "VT_AUTO, ");
2776 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2777 				cnavailable(vw->vw_terminal->consdev, TRUE);
2778 			/* were we in the middle of the vty switching process? */
2779 			if (finish_vt_rel(vw, TRUE, &s) == 0)
2780 				DPRINTF(5, "reset WAIT_REL, ");
2781 			if (finish_vt_acq(vw) == 0)
2782 				DPRINTF(5, "reset WAIT_ACQ, ");
2783 			return (0);
2784 		} else if (mode->mode == VT_PROCESS) {
2785 			if (!ISSIGVALID(mode->relsig) ||
2786 			    !ISSIGVALID(mode->acqsig) ||
2787 			    !ISSIGVALID(mode->frsig)) {
2788 				DPRINTF(5, "error EINVAL\n");
2789 				return (EINVAL);
2790 			}
2791 			DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2792 			bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2793 			vw->vw_proc = td->td_proc;
2794 			vw->vw_pid = vw->vw_proc->p_pid;
2795 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2796 				cnavailable(vw->vw_terminal->consdev, FALSE);
2797 		} else {
2798 			DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2799 			    mode->mode);
2800 			return (EINVAL);
2801 		}
2802 		DPRINTF(5, "\n");
2803 		return (0);
2804 	}
2805 	case VT_GETMODE:	/* get screen switcher mode */
2806 		bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2807 		return (0);
2808 
2809 	case VT_RELDISP:	/* screen switcher ioctl */
2810 		/*
2811 		 * This must be the current vty which is in the VT_PROCESS
2812 		 * switching mode...
2813 		 */
2814 		if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2815 		    VT_PROCESS)) {
2816 			return (EINVAL);
2817 		}
2818 		/* ...and this process is controlling it. */
2819 		if (vw->vw_proc != td->td_proc) {
2820 			return (EPERM);
2821 		}
2822 		error = EINVAL;
2823 		switch(*(int *)data) {
2824 		case VT_FALSE:	/* user refuses to release screen, abort */
2825 			if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2826 				DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2827 				    SC_DRIVER_NAME, VT_UNIT(vw));
2828 			break;
2829 		case VT_TRUE:	/* user has released screen, go on */
2830 			/* finish_vt_rel(..., TRUE, ...) should not be locked */
2831 			if (vw->vw_flags & VWF_SWWAIT_REL) {
2832 				if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2833 					DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2834 					    SC_DRIVER_NAME, VT_UNIT(vw));
2835 			} else {
2836 				error = EINVAL;
2837 			}
2838 			return (error);
2839 		case VT_ACKACQ:	/* acquire acknowledged, switch completed */
2840 			if ((error = finish_vt_acq(vw)) == 0)
2841 				DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2842 				    SC_DRIVER_NAME, VT_UNIT(vw));
2843 			break;
2844 		default:
2845 			break;
2846 		}
2847 		return (error);
2848 	}
2849 
2850 	return (ENOIOCTL);
2851 }
2852 
2853 static struct vt_window *
2854 vt_allocate_window(struct vt_device *vd, unsigned int window)
2855 {
2856 	struct vt_window *vw;
2857 	struct terminal *tm;
2858 	term_pos_t size;
2859 	struct winsize wsz;
2860 
2861 	vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2862 	vw->vw_device = vd;
2863 	vw->vw_number = window;
2864 	vw->vw_kbdmode = K_XLATE;
2865 
2866 	if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2867 		vw->vw_font = vtfont_ref(vt_font_assigned);
2868 		vt_compute_drawable_area(vw);
2869 	}
2870 
2871 	vt_termsize(vd, vw->vw_font, &size);
2872 	vt_winsize(vd, vw->vw_font, &wsz);
2873 	tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2874 	vw->vw_buf.vb_terminal = tm;	/* must be set before vtbuf_init() */
2875 	vtbuf_init(&vw->vw_buf, &size);
2876 
2877 	terminal_set_winsize(tm, &wsz);
2878 	vd->vd_windows[window] = vw;
2879 	callout_init(&vw->vw_proc_dead_timer, 0);
2880 
2881 	return (vw);
2882 }
2883 
2884 void
2885 vt_upgrade(struct vt_device *vd)
2886 {
2887 	struct vt_window *vw;
2888 	unsigned int i;
2889 	int register_handlers;
2890 
2891 	if (!vty_enabled(VTY_VT))
2892 		return;
2893 	if (main_vd->vd_driver == NULL)
2894 		return;
2895 
2896 	for (i = 0; i < VT_MAXWINDOWS; i++) {
2897 		vw = vd->vd_windows[i];
2898 		if (vw == NULL) {
2899 			/* New window. */
2900 			vw = vt_allocate_window(vd, i);
2901 		}
2902 		if (!(vw->vw_flags & VWF_READY)) {
2903 			callout_init(&vw->vw_proc_dead_timer, 0);
2904 			terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2905 			vw->vw_flags |= VWF_READY;
2906 			if (vw->vw_flags & VWF_CONSOLE) {
2907 				/* For existing console window. */
2908 				EVENTHANDLER_REGISTER(shutdown_pre_sync,
2909 				    vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2910 			}
2911 		}
2912 	}
2913 	VT_LOCK(vd);
2914 	if (vd->vd_curwindow == NULL)
2915 		vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2916 
2917 	register_handlers = 0;
2918 	if (!(vd->vd_flags & VDF_ASYNC)) {
2919 		/* Attach keyboard. */
2920 		vt_allocate_keyboard(vd);
2921 
2922 		/* Init 25 Hz timer. */
2923 		callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2924 
2925 		/*
2926 		 * Start timer when everything ready.
2927 		 * Note that the operations here are purposefully ordered.
2928 		 * We need to ensure vd_timer_armed is non-zero before we set
2929 		 * the VDF_ASYNC flag. That prevents this function from
2930 		 * racing with vt_resume_flush_timer() to update the
2931 		 * callout structure.
2932 		 */
2933 		atomic_add_acq_int(&vd->vd_timer_armed, 1);
2934 		vd->vd_flags |= VDF_ASYNC;
2935 		callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2936 		register_handlers = 1;
2937 	}
2938 
2939 	VT_UNLOCK(vd);
2940 
2941 	/* Refill settings with new sizes. */
2942 	vt_resize(vd);
2943 
2944 	if (register_handlers) {
2945 		/* Register suspend/resume handlers. */
2946 		EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2947 		    vd, EVENTHANDLER_PRI_ANY);
2948 		EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2949 		    EVENTHANDLER_PRI_ANY);
2950 	}
2951 }
2952 
2953 static void
2954 vt_resize(struct vt_device *vd)
2955 {
2956 	struct vt_window *vw;
2957 	int i;
2958 
2959 	for (i = 0; i < VT_MAXWINDOWS; i++) {
2960 		vw = vd->vd_windows[i];
2961 		VT_LOCK(vd);
2962 		/* Assign default font to window, if not textmode. */
2963 		if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2964 			vw->vw_font = vtfont_ref(vt_font_assigned);
2965 		VT_UNLOCK(vd);
2966 
2967 		/* Resize terminal windows */
2968 		while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2969 			DPRINTF(100, "%s: vt_change_font() is busy, "
2970 			    "window %d\n", __func__, i);
2971 		}
2972 	}
2973 }
2974 
2975 static void
2976 vt_replace_backend(const struct vt_driver *drv, void *softc)
2977 {
2978 	struct vt_device *vd;
2979 
2980 	vd = main_vd;
2981 
2982 	if (vd->vd_flags & VDF_ASYNC) {
2983 		/* Stop vt_flush periodic task. */
2984 		VT_LOCK(vd);
2985 		vt_suspend_flush_timer(vd);
2986 		VT_UNLOCK(vd);
2987 		/*
2988 		 * Mute current terminal until we done. vt_change_font (called
2989 		 * from vt_resize) will unmute it.
2990 		 */
2991 		terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2992 	}
2993 
2994 	/*
2995 	 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2996 	 * set it.
2997 	 */
2998 	VT_LOCK(vd);
2999 	vd->vd_flags &= ~VDF_TEXTMODE;
3000 
3001 	if (drv != NULL) {
3002 		/*
3003 		 * We want to upgrade from the current driver to the
3004 		 * given driver.
3005 		 */
3006 
3007 		vd->vd_prev_driver = vd->vd_driver;
3008 		vd->vd_prev_softc = vd->vd_softc;
3009 		vd->vd_driver = drv;
3010 		vd->vd_softc = softc;
3011 
3012 		vd->vd_driver->vd_init(vd);
3013 	} else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
3014 		/*
3015 		 * No driver given: we want to downgrade to the previous
3016 		 * driver.
3017 		 */
3018 		const struct vt_driver *old_drv;
3019 		void *old_softc;
3020 
3021 		old_drv = vd->vd_driver;
3022 		old_softc = vd->vd_softc;
3023 
3024 		vd->vd_driver = vd->vd_prev_driver;
3025 		vd->vd_softc = vd->vd_prev_softc;
3026 		vd->vd_prev_driver = NULL;
3027 		vd->vd_prev_softc = NULL;
3028 
3029 		vd->vd_flags |= VDF_DOWNGRADE;
3030 
3031 		vd->vd_driver->vd_init(vd);
3032 
3033 		if (old_drv->vd_fini)
3034 			old_drv->vd_fini(vd, old_softc);
3035 
3036 		vd->vd_flags &= ~VDF_DOWNGRADE;
3037 	}
3038 
3039 	VT_UNLOCK(vd);
3040 
3041 	/* Update windows sizes and initialize last items. */
3042 	vt_upgrade(vd);
3043 
3044 #ifdef DEV_SPLASH
3045 	if (vd->vd_flags & VDF_SPLASH)
3046 		vtterm_splash(vd);
3047 #endif
3048 
3049 	if (vd->vd_flags & VDF_ASYNC) {
3050 		/* Allow to put chars now. */
3051 		terminal_mute(vd->vd_curwindow->vw_terminal, 0);
3052 		/* Rerun timer for screen updates. */
3053 		vt_resume_flush_timer(vd->vd_curwindow, 0);
3054 	}
3055 
3056 	/*
3057 	 * Register as console. If it already registered, cnadd() will ignore
3058 	 * it.
3059 	 */
3060 	termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
3061 }
3062 
3063 static void
3064 vt_suspend_handler(void *priv)
3065 {
3066 	struct vt_device *vd;
3067 
3068 	vd = priv;
3069 	vd->vd_flags |= VDF_SUSPENDED;
3070 	if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
3071 		vd->vd_driver->vd_suspend(vd);
3072 }
3073 
3074 static void
3075 vt_resume_handler(void *priv)
3076 {
3077 	struct vt_device *vd;
3078 
3079 	vd = priv;
3080 	if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
3081 		vd->vd_driver->vd_resume(vd);
3082 	vd->vd_flags &= ~VDF_SUSPENDED;
3083 }
3084 
3085 void
3086 vt_allocate(const struct vt_driver *drv, void *softc)
3087 {
3088 
3089 	if (!vty_enabled(VTY_VT))
3090 		return;
3091 
3092 	if (main_vd->vd_driver == NULL) {
3093 		main_vd->vd_driver = drv;
3094 		printf("VT: initialize with new VT driver \"%s\".\n",
3095 		    drv->vd_name);
3096 	} else {
3097 		/*
3098 		 * Check if have rights to replace current driver. For example:
3099 		 * it is bad idea to replace KMS driver with generic VGA one.
3100 		 */
3101 		if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
3102 			printf("VT: Driver priority %d too low. Current %d\n ",
3103 			    drv->vd_priority, main_vd->vd_driver->vd_priority);
3104 			return;
3105 		}
3106 		printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
3107 		    main_vd->vd_driver->vd_name, drv->vd_name);
3108 	}
3109 
3110 	vt_replace_backend(drv, softc);
3111 }
3112 
3113 void
3114 vt_deallocate(const struct vt_driver *drv, void *softc)
3115 {
3116 
3117 	if (!vty_enabled(VTY_VT))
3118 		return;
3119 
3120 	if (main_vd->vd_prev_driver == NULL ||
3121 	    main_vd->vd_driver != drv ||
3122 	    main_vd->vd_softc != softc)
3123 		return;
3124 
3125 	printf("VT: Switching back from \"%s\" to \"%s\".\n",
3126 	    main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
3127 
3128 	vt_replace_backend(NULL, NULL);
3129 }
3130 
3131 void
3132 vt_suspend(struct vt_device *vd)
3133 {
3134 	int error;
3135 
3136 	if (vt_suspendswitch == 0)
3137 		return;
3138 	/* Save current window. */
3139 	vd->vd_savedwindow = vd->vd_curwindow;
3140 	/* Ask holding process to free window and switch to console window */
3141 	vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
3142 
3143 	/* Wait for the window switch to complete. */
3144 	error = 0;
3145 	VT_LOCK(vd);
3146 	while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
3147 		error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
3148 	VT_UNLOCK(vd);
3149 }
3150 
3151 void
3152 vt_resume(struct vt_device *vd)
3153 {
3154 
3155 	if (vt_suspendswitch == 0)
3156 		return;
3157 	/* Switch back to saved window, if any */
3158 	vt_proc_window_switch(vd->vd_savedwindow);
3159 	vd->vd_savedwindow = NULL;
3160 }
3161