xref: /freebsd/sys/dev/vt/vt_core.c (revision fe01740653ed066369f3c892b7aa18a710c608c5)
1 /*-
2  * Copyright (c) 2009, 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Ed Schouten under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Portions of this software were developed by Oleksandr Rybalko
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_compat.h"
37 
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
41 #include <sys/fbio.h>
42 #include <sys/kbio.h>
43 #include <sys/kdb.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/reboot.h>
51 #include <sys/systm.h>
52 #include <sys/terminal.h>
53 
54 #include <dev/kbd/kbdreg.h>
55 #include <dev/vt/vt.h>
56 
57 #if defined(__i386__) || defined(__amd64__)
58 #include <machine/psl.h>
59 #include <machine/frame.h>
60 #endif
61 
62 static tc_bell_t	vtterm_bell;
63 static tc_cursor_t	vtterm_cursor;
64 static tc_putchar_t	vtterm_putchar;
65 static tc_fill_t	vtterm_fill;
66 static tc_copy_t	vtterm_copy;
67 static tc_param_t	vtterm_param;
68 static tc_done_t	vtterm_done;
69 
70 static tc_cnprobe_t	vtterm_cnprobe;
71 static tc_cngetc_t	vtterm_cngetc;
72 
73 static tc_opened_t	vtterm_opened;
74 static tc_ioctl_t	vtterm_ioctl;
75 static tc_mmap_t	vtterm_mmap;
76 
77 const struct terminal_class vt_termclass = {
78 	.tc_bell	= vtterm_bell,
79 	.tc_cursor	= vtterm_cursor,
80 	.tc_putchar	= vtterm_putchar,
81 	.tc_fill	= vtterm_fill,
82 	.tc_copy	= vtterm_copy,
83 	.tc_param	= vtterm_param,
84 	.tc_done	= vtterm_done,
85 
86 	.tc_cnprobe	= vtterm_cnprobe,
87 	.tc_cngetc	= vtterm_cngetc,
88 
89 	.tc_opened	= vtterm_opened,
90 	.tc_ioctl	= vtterm_ioctl,
91 	.tc_mmap	= vtterm_mmap,
92 };
93 
94 /*
95  * Use a constant timer of 25 Hz to redraw the screen.
96  *
97  * XXX: In theory we should only fire up the timer when there is really
98  * activity. Unfortunately we cannot always start timers. We really
99  * don't want to process kernel messages synchronously, because it
100  * really slows down the system.
101  */
102 #define	VT_TIMERFREQ	25
103 
104 /* Bell pitch/duration. */
105 #define VT_BELLDURATION	((5 * hz + 99) / 100)
106 #define VT_BELLPITCH	800
107 
108 #define	VT_LOCK(vd)	mtx_lock(&(vd)->vd_lock)
109 #define	VT_UNLOCK(vd)	mtx_unlock(&(vd)->vd_lock)
110 
111 #define	VT_UNIT(vw)	((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
112 			(vw)->vw_number)
113 
114 /* XXX while syscons is here. */
115 int sc_txtmouse_no_retrace_wait;
116 
117 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
118 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
119 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
120 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
121 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
122 
123 static struct vt_device	vt_consdev;
124 static unsigned int vt_unit = 0;
125 static MALLOC_DEFINE(M_VT, "vt", "vt device");
126 struct vt_device *main_vd = &vt_consdev;
127 
128 /* Boot logo. */
129 extern unsigned int vt_logo_width;
130 extern unsigned int vt_logo_height;
131 extern unsigned int vt_logo_depth;
132 extern unsigned char vt_logo_image[];
133 
134 /* Font. */
135 extern struct vt_font vt_font_default;
136 #ifndef SC_NO_CUTPASTE
137 extern struct mouse_cursor vt_default_mouse_pointer;
138 #endif
139 
140 static int signal_vt_rel(struct vt_window *);
141 static int signal_vt_acq(struct vt_window *);
142 static int finish_vt_rel(struct vt_window *, int, int *);
143 static int finish_vt_acq(struct vt_window *);
144 static int vt_window_switch(struct vt_window *);
145 static int vt_late_window_switch(struct vt_window *);
146 static int vt_proc_alive(struct vt_window *);
147 static void vt_resize(struct vt_device *);
148 static void vt_update_static(void *);
149 
150 SET_DECLARE(vt_drv_set, struct vt_driver);
151 
152 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
153 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
154 
155 static struct terminal	vt_consterm;
156 static struct vt_window	vt_conswindow;
157 static struct vt_device	vt_consdev = {
158 	.vd_driver = NULL,
159 	.vd_softc = NULL,
160 	.vd_flags = VDF_INVALID,
161 	.vd_windows = { [VT_CONSWINDOW] =  &vt_conswindow, },
162 	.vd_curwindow = &vt_conswindow,
163 	.vd_markedwin = NULL,
164 	.vd_kbstate = 0,
165 };
166 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
167 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
168 static struct vt_window	vt_conswindow = {
169 	.vw_number = VT_CONSWINDOW,
170 	.vw_flags = VWF_CONSOLE,
171 	.vw_buf = {
172 		.vb_buffer = vt_constextbuf,
173 		.vb_rows = vt_constextbufrows,
174 		.vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
175 		.vb_curroffset = 0,
176 		.vb_roffset = 0,
177 		.vb_flags = VBF_STATIC,
178 		.vb_mark_start = {.tp_row = 0, .tp_col = 0,},
179 		.vb_mark_end = {.tp_row = 0, .tp_col = 0,},
180 		.vb_scr_size = {
181 			.tp_row = _VTDEFH,
182 			.tp_col = _VTDEFW,
183 		},
184 	},
185 	.vw_device = &vt_consdev,
186 	.vw_terminal = &vt_consterm,
187 	.vw_kbdmode = K_XLATE,
188 };
189 static struct terminal vt_consterm = {
190 	.tm_class = &vt_termclass,
191 	.tm_softc = &vt_conswindow,
192 	.tm_flags = TF_CONS,
193 };
194 static struct consdev vt_consterm_consdev = {
195 	.cn_ops = &termcn_cnops,
196 	.cn_arg = &vt_consterm,
197 	.cn_name = "ttyv0",
198 };
199 
200 /* Add to set of consoles. */
201 DATA_SET(cons_set, vt_consterm_consdev);
202 
203 /*
204  * Right after kmem is done to allow early drivers to use locking and allocate
205  * memory.
206  */
207 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
208     &vt_consdev);
209 /* Delay until all devices attached, to not waste time. */
210 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
211     &vt_consdev);
212 
213 /* Initialize locks/mem depended members. */
214 static void
215 vt_update_static(void *dummy)
216 {
217 
218 	if (getenv("kern.vt.disable"))
219 		return;
220 	if (main_vd->vd_driver != NULL)
221 		printf("VT: running with driver \"%s\".\n",
222 		    main_vd->vd_driver->vd_name);
223 	else
224 		printf("VT: init without driver.\n");
225 
226 	mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
227 	cv_init(&main_vd->vd_winswitch, "vtwswt");
228 }
229 
230 static void
231 vt_switch_timer(void *arg)
232 {
233 
234 	vt_late_window_switch((struct vt_window *)arg);
235 }
236 
237 static int
238 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
239 {
240 
241 	DPRINTF(40, "%s\n", __func__);
242 	curvw->vw_switch_to = vw;
243 	/* Set timer to allow switch in case when process hang. */
244 	callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
245 	    vt_switch_timer, (void *)vw);
246 	/* Notify process about vt switch attempt. */
247 	DPRINTF(30, "%s: Notify process.\n", __func__);
248 	signal_vt_rel(curvw);
249 
250 	return (0);
251 }
252 
253 static int
254 vt_window_postswitch(struct vt_window *vw)
255 {
256 
257 	signal_vt_acq(vw);
258 	return (0);
259 }
260 
261 /* vt_late_window_switch will done VT switching for regular case. */
262 static int
263 vt_late_window_switch(struct vt_window *vw)
264 {
265 	int ret;
266 
267 	callout_stop(&vw->vw_proc_dead_timer);
268 
269 	ret = vt_window_switch(vw);
270 	if (ret)
271 		return (ret);
272 
273 	/* Notify owner process about terminal availability. */
274 	if (vw->vw_smode.mode == VT_PROCESS) {
275 		ret = vt_window_postswitch(vw);
276 	}
277 	return (ret);
278 }
279 
280 /* Switch window. */
281 static int
282 vt_proc_window_switch(struct vt_window *vw)
283 {
284 	struct vt_window *curvw;
285 	struct vt_device *vd;
286 	int ret;
287 
288 	vd = vw->vw_device;
289 	curvw = vd->vd_curwindow;
290 
291 	if (curvw->vw_flags & VWF_VTYLOCK)
292 		return (EBUSY);
293 
294 	/* Ask current process permitions to switch away. */
295 	if (curvw->vw_smode.mode == VT_PROCESS) {
296 		DPRINTF(30, "%s: VT_PROCESS ", __func__);
297 		if (vt_proc_alive(curvw) == FALSE) {
298 			DPRINTF(30, "Dead. Cleaning.");
299 			/* Dead */
300 		} else {
301 			DPRINTF(30, "%s: Signaling process.\n", __func__);
302 			/* Alive, try to ask him. */
303 			ret = vt_window_preswitch(vw, curvw);
304 			/* Wait for process answer or timeout. */
305 			return (ret);
306 		}
307 		DPRINTF(30, "\n");
308 	}
309 
310 	ret = vt_late_window_switch(vw);
311 	return (ret);
312 }
313 
314 /* Switch window ignoring process locking. */
315 static int
316 vt_window_switch(struct vt_window *vw)
317 {
318 	struct vt_device *vd = vw->vw_device;
319 	struct vt_window *curvw = vd->vd_curwindow;
320 	keyboard_t *kbd;
321 
322 	VT_LOCK(vd);
323 	if (curvw == vw) {
324 		/* Nothing to do. */
325 		VT_UNLOCK(vd);
326 		return (0);
327 	}
328 	if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
329 		VT_UNLOCK(vd);
330 		return (EINVAL);
331 	}
332 
333 	vd->vd_curwindow = vw;
334 	vd->vd_flags |= VDF_INVALID;
335 	cv_broadcast(&vd->vd_winswitch);
336 	VT_UNLOCK(vd);
337 
338 	if (vd->vd_driver->vd_postswitch)
339 		vd->vd_driver->vd_postswitch(vd);
340 
341 	/* Restore per-window keyboard mode. */
342 	mtx_lock(&Giant);
343 	kbd = kbd_get_keyboard(vd->vd_keyboard);
344 	if (kbd != NULL) {
345 		kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
346 	}
347 	mtx_unlock(&Giant);
348 	DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
349 
350 	return (0);
351 }
352 
353 static inline void
354 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
355 {
356 
357 	size->tp_row = vd->vd_height;
358 	size->tp_col = vd->vd_width;
359 	if (vf != NULL) {
360 		size->tp_row /= vf->vf_height;
361 		size->tp_col /= vf->vf_width;
362 	}
363 }
364 
365 static inline void
366 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
367 {
368 
369 	size->ws_row = size->ws_ypixel = vd->vd_height;
370 	size->ws_col = size->ws_xpixel = vd->vd_width;
371 	if (vf != NULL) {
372 		size->ws_row /= vf->vf_height;
373 		size->ws_col /= vf->vf_width;
374 	}
375 }
376 
377 static void
378 vt_scroll(struct vt_window *vw, int offset, int whence)
379 {
380 	int diff;
381 	term_pos_t size;
382 
383 	if ((vw->vw_flags & VWF_SCROLL) == 0)
384 		return;
385 
386 	vt_termsize(vw->vw_device, vw->vw_font, &size);
387 
388 	diff = vthistory_seek(&vw->vw_buf, offset, whence);
389 	/*
390 	 * Offset changed, please update Nth lines on sceen.
391 	 * +N - Nth lines at top;
392 	 * -N - Nth lines at bottom.
393 	 */
394 
395 	if (diff < -size.tp_row || diff > size.tp_row) {
396 		vw->vw_device->vd_flags |= VDF_INVALID;
397 		return;
398 	}
399 	vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
400 }
401 
402 static int
403 vt_machine_kbdevent(int c)
404 {
405 
406 	switch (c) {
407 	case SPCLKEY | DBG:
408 		kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
409 		return (1);
410 	case SPCLKEY | RBT:
411 		/* XXX: Make this configurable! */
412 		shutdown_nice(0);
413 		return (1);
414 	case SPCLKEY | HALT:
415 		shutdown_nice(RB_HALT);
416 		return (1);
417 	case SPCLKEY | PDWN:
418 		shutdown_nice(RB_HALT|RB_POWEROFF);
419 		return (1);
420 	};
421 
422 	return (0);
423 }
424 
425 static void
426 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
427 {
428 	struct vt_device *vd;
429 	term_pos_t size;
430 
431 	vd = vw->vw_device;
432 	/* Only special keys handled in ScrollLock mode */
433 	if ((c & SPCLKEY) == 0)
434 		return;
435 
436 	c &= ~SPCLKEY;
437 
438 	if (console == 0) {
439 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
440 			vw = vd->vd_windows[c - F_SCR];
441 			if (vw != NULL)
442 				vt_proc_window_switch(vw);
443 			return;
444 		}
445 		VT_LOCK(vd);
446 	}
447 
448 	switch (c) {
449 	case SLK: {
450 		/* Turn scrolling off. */
451 		vt_scroll(vw, 0, VHS_END);
452 		VTBUF_SLCK_DISABLE(&vw->vw_buf);
453 		vw->vw_flags &= ~VWF_SCROLL;
454 		break;
455 	}
456 	case FKEY | F(49): /* Home key. */
457 		vt_scroll(vw, 0, VHS_SET);
458 		break;
459 	case FKEY | F(50): /* Arrow up. */
460 		vt_scroll(vw, -1, VHS_CUR);
461 		break;
462 	case FKEY | F(51): /* Page up. */
463 		vt_termsize(vd, vw->vw_font, &size);
464 		vt_scroll(vw, -size.tp_row, VHS_CUR);
465 		break;
466 	case FKEY | F(57): /* End key. */
467 		vt_scroll(vw, 0, VHS_END);
468 		break;
469 	case FKEY | F(58): /* Arrow down. */
470 		vt_scroll(vw, 1, VHS_CUR);
471 		break;
472 	case FKEY | F(59): /* Page down. */
473 		vt_termsize(vd, vw->vw_font, &size);
474 		vt_scroll(vw, size.tp_row, VHS_CUR);
475 		break;
476 	}
477 
478 	if (console == 0)
479 		VT_UNLOCK(vd);
480 }
481 
482 static int
483 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
484 {
485 	struct vt_window *vw = vd->vd_curwindow;
486 	int state = 0;
487 
488 #if VT_ALT_TO_ESC_HACK
489 	if (c & RELKEY) {
490 		switch (c & ~RELKEY) {
491 		case (SPCLKEY | RALT):
492 			if (vt_enable_altgr != 0)
493 				break;
494 		case (SPCLKEY | LALT):
495 			vd->vd_kbstate &= ~ALKED;
496 		}
497 		/* Other keys ignored for RELKEY event. */
498 		return (0);
499 	} else {
500 		switch (c & ~RELKEY) {
501 		case (SPCLKEY | RALT):
502 			if (vt_enable_altgr != 0)
503 				break;
504 		case (SPCLKEY | LALT):
505 			vd->vd_kbstate |= ALKED;
506 		}
507 	}
508 #else
509 	if (c & RELKEY)
510 		/* Other keys ignored for RELKEY event. */
511 		return (0);
512 #endif
513 
514 	if (vt_machine_kbdevent(c))
515 		return (0);
516 
517 	if (vw->vw_flags & VWF_SCROLL) {
518 		vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
519 		/* Scroll mode keys handled, nothing to do more. */
520 		return (0);
521 	}
522 
523 	if (c & SPCLKEY) {
524 		c &= ~SPCLKEY;
525 
526 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
527 			vw = vd->vd_windows[c - F_SCR];
528 			if (vw != NULL)
529 				vt_proc_window_switch(vw);
530 			return (0);
531 		}
532 
533 		switch (c) {
534 		case SLK: {
535 
536 			kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
537 			VT_LOCK(vd);
538 			if (state & SLKED) {
539 				/* Turn scrolling on. */
540 				vw->vw_flags |= VWF_SCROLL;
541 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
542 			} else {
543 				/* Turn scrolling off. */
544 				vw->vw_flags &= ~VWF_SCROLL;
545 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
546 				vt_scroll(vw, 0, VHS_END);
547 			}
548 			VT_UNLOCK(vd);
549 			break;
550 		}
551 		case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
552 		case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
553 		case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
554 		case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
555 			/* F1 through F12 keys. */
556 			terminal_input_special(vw->vw_terminal,
557 			    TKEY_F1 + c - (FKEY | F(1)));
558 			break;
559 		case FKEY | F(49): /* Home key. */
560 			terminal_input_special(vw->vw_terminal, TKEY_HOME);
561 			break;
562 		case FKEY | F(50): /* Arrow up. */
563 			terminal_input_special(vw->vw_terminal, TKEY_UP);
564 			break;
565 		case FKEY | F(51): /* Page up. */
566 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
567 			break;
568 		case FKEY | F(53): /* Arrow left. */
569 			terminal_input_special(vw->vw_terminal, TKEY_LEFT);
570 			break;
571 		case FKEY | F(55): /* Arrow right. */
572 			terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
573 			break;
574 		case FKEY | F(57): /* End key. */
575 			terminal_input_special(vw->vw_terminal, TKEY_END);
576 			break;
577 		case FKEY | F(58): /* Arrow down. */
578 			terminal_input_special(vw->vw_terminal, TKEY_DOWN);
579 			break;
580 		case FKEY | F(59): /* Page down. */
581 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
582 			break;
583 		case FKEY | F(60): /* Insert key. */
584 			terminal_input_special(vw->vw_terminal, TKEY_INSERT);
585 			break;
586 		case FKEY | F(61): /* Delete key. */
587 			terminal_input_special(vw->vw_terminal, TKEY_DELETE);
588 			break;
589 		}
590 	} else if (KEYFLAGS(c) == 0) {
591 		/* Don't do UTF-8 conversion when doing raw mode. */
592 		if (vw->vw_kbdmode == K_XLATE) {
593 #if VT_ALT_TO_ESC_HACK
594 			if (vd->vd_kbstate & ALKED) {
595 				/*
596 				 * Prepend ESC sequence if one of ALT keys down.
597 				 */
598 				terminal_input_char(vw->vw_terminal, 0x1b);
599 			}
600 #endif
601 
602 			terminal_input_char(vw->vw_terminal, KEYCHAR(c));
603 		} else
604 			terminal_input_raw(vw->vw_terminal, c);
605 	}
606 	return (0);
607 }
608 
609 static int
610 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
611 {
612 	struct vt_device *vd = arg;
613 	int c;
614 
615 	switch (event) {
616 	case KBDIO_KEYINPUT:
617 		break;
618 	case KBDIO_UNLOADING:
619 		mtx_lock(&Giant);
620 		vd->vd_keyboard = -1;
621 		kbd_release(kbd, (void *)&vd->vd_keyboard);
622 		mtx_unlock(&Giant);
623 		return (0);
624 	default:
625 		return (EINVAL);
626 	}
627 
628 	while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
629 		vt_processkey(kbd, vd, c);
630 
631 	return (0);
632 }
633 
634 static int
635 vt_allocate_keyboard(struct vt_device *vd)
636 {
637 	int		 idx0, idx;
638 	keyboard_t	*k0, *k;
639 	keyboard_info_t	 ki;
640 
641 	idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
642 	vd->vd_keyboard = idx0;
643 	if (idx0 >= 0) {
644 		DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
645 		k0 = kbd_get_keyboard(idx0);
646 
647 		for (idx = kbd_find_keyboard2("*", -1, 0);
648 		     idx != -1;
649 		     idx = kbd_find_keyboard2("*", -1, idx + 1)) {
650 			k = kbd_get_keyboard(idx);
651 
652 			if (idx == idx0 || KBD_IS_BUSY(k))
653 				continue;
654 
655 			bzero(&ki, sizeof(ki));
656 			strcpy(ki.kb_name, k->kb_name);
657 			ki.kb_unit = k->kb_unit;
658 
659 			kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
660 		}
661 	} else {
662 		DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
663 		idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
664 		if (idx0 < 0) {
665 			DPRINTF(10, "%s: No keyboard found.\n", __func__);
666 			return (-1);
667 		}
668 	}
669 	DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
670 
671 	return (idx0);
672 }
673 
674 static void
675 vtterm_bell(struct terminal *tm)
676 {
677 	struct vt_window *vw = tm->tm_softc;
678 	struct vt_device *vd = vw->vw_device;
679 
680 	if (vd->vd_flags & VDF_QUIET_BELL)
681 		return;
682 
683 	sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
684 }
685 
686 static void
687 vtterm_beep(struct terminal *tm, u_int param)
688 {
689 	u_int freq, period;
690 
691 	if ((param == 0) || ((param & 0xffff) == 0)) {
692 		vtterm_bell(tm);
693 		return;
694 	}
695 
696 	period = ((param >> 16) & 0xffff) * hz / 1000;
697 	freq = 1193182 / (param & 0xffff);
698 
699 	sysbeep(freq, period);
700 }
701 
702 static void
703 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
704 {
705 	struct vt_window *vw = tm->tm_softc;
706 
707 	vtbuf_cursor_position(&vw->vw_buf, p);
708 }
709 
710 static void
711 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
712 {
713 	struct vt_window *vw = tm->tm_softc;
714 
715 	vtbuf_putchar(&vw->vw_buf, p, c);
716 }
717 
718 static void
719 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
720 {
721 	struct vt_window *vw = tm->tm_softc;
722 
723 	vtbuf_fill_locked(&vw->vw_buf, r, c);
724 }
725 
726 static void
727 vtterm_copy(struct terminal *tm, const term_rect_t *r,
728     const term_pos_t *p)
729 {
730 	struct vt_window *vw = tm->tm_softc;
731 
732 	vtbuf_copy(&vw->vw_buf, r, p);
733 }
734 
735 static void
736 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
737 {
738 	struct vt_window *vw = tm->tm_softc;
739 
740 	switch (cmd) {
741 	case TP_SHOWCURSOR:
742 		vtbuf_cursor_visibility(&vw->vw_buf, arg);
743 		break;
744 	case TP_MOUSE:
745 		vw->vw_mouse_level = arg;
746 		break;
747 	}
748 }
749 
750 static inline void
751 vt_determine_colors(term_char_t c, int cursor,
752     term_color_t *fg, term_color_t *bg)
753 {
754 	term_color_t tmp;
755 	int invert;
756 
757 	invert = 0;
758 
759 	*fg = TCHAR_FGCOLOR(c);
760 	if (TCHAR_FORMAT(c) & TF_BOLD)
761 		*fg = TCOLOR_LIGHT(*fg);
762 	*bg = TCHAR_BGCOLOR(c);
763 
764 	if (TCHAR_FORMAT(c) & TF_REVERSE)
765 		invert ^= 1;
766 	if (cursor)
767 		invert ^= 1;
768 
769 	if (invert) {
770 		tmp = *fg;
771 		*fg = *bg;
772 		*bg = tmp;
773 	}
774 }
775 
776 static void
777 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
778     int iscursor, unsigned int row, unsigned int col)
779 {
780 	term_color_t fg, bg;
781 
782 	vt_determine_colors(c, iscursor, &fg, &bg);
783 
784 	if (vf != NULL) {
785 		const uint8_t *src;
786 		vt_axis_t top, left;
787 
788 		src = vtfont_lookup(vf, c);
789 
790 		/*
791 		 * Align the terminal to the centre of the screen.
792 		 * Fonts may not always be able to fill the entire
793 		 * screen.
794 		 */
795 		top = row * vf->vf_height + vd->vd_offset.tp_row;
796 		left = col * vf->vf_width + vd->vd_offset.tp_col;
797 
798 		vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
799 		    vf->vf_width, vf->vf_height, fg, bg);
800 	} else {
801 		vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
802 		    row, col, fg, bg);
803 	}
804 }
805 
806 static void
807 vt_flush(struct vt_device *vd)
808 {
809 	struct vt_window *vw;
810 	struct vt_font *vf;
811 	struct vt_bufmask tmask;
812 	unsigned int row, col;
813 	term_rect_t tarea;
814 	term_pos_t size;
815 	term_char_t *r;
816 #ifndef SC_NO_CUTPASTE
817 	struct mouse_cursor *m;
818 	int bpl, h, w;
819 #endif
820 
821 	vw = vd->vd_curwindow;
822 	if (vw == NULL)
823 		return;
824 	vf = vw->vw_font;
825 	if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
826 		return;
827 
828 	if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
829 		return;
830 
831 	vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
832 	vt_termsize(vd, vf, &size);
833 
834 	/* Force a full redraw when the screen contents are invalid. */
835 	if (vd->vd_flags & VDF_INVALID) {
836 		tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
837 		tarea.tr_end = size;
838 		tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
839 
840 		vd->vd_flags &= ~VDF_INVALID;
841 	}
842 
843 #ifndef SC_NO_CUTPASTE
844 	if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
845 		/* Mark last mouse position as dirty to erase. */
846 		vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
847 		    vd->vd_mdirtyy);
848 	}
849 #endif
850 
851 	for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
852 		if (!VTBUF_DIRTYROW(&tmask, row))
853 			continue;
854 		r = VTBUF_GET_ROW(&vw->vw_buf, row);
855 		for (col = tarea.tr_begin.tp_col;
856 		    col < tarea.tr_end.tp_col; col++) {
857 			if (!VTBUF_DIRTYCOL(&tmask, col))
858 				continue;
859 
860 			vt_bitblt_char(vd, vf, r[col],
861 			    VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
862 		}
863 	}
864 
865 #ifndef SC_NO_CUTPASTE
866 	/* Mouse disabled. */
867 	if (vw->vw_flags & VWF_MOUSE_HIDE)
868 		return;
869 
870 	/* No mouse for DDB. */
871 	if (kdb_active || panicstr != NULL)
872 		return;
873 
874 	if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
875 	    VDF_MOUSECURSOR) {
876 		m = &vt_default_mouse_pointer;
877 		bpl = (m->w + 7) >> 3; /* Bytes per source line. */
878 		w = m->w;
879 		h = m->h;
880 
881 		if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
882 			w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
883 		if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
884 			h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
885 
886 		vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
887 		    vd->vd_offset.tp_row + vd->vd_my,
888 		    vd->vd_offset.tp_col + vd->vd_mx,
889 		    w, h, TC_WHITE, TC_BLACK);
890 		/* Save point of last mouse cursor to erase it later. */
891 		vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
892 		vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
893 	}
894 #endif
895 }
896 
897 static void
898 vt_timer(void *arg)
899 {
900 	struct vt_device *vd;
901 
902 	vd = arg;
903 	/* Update screen if required. */
904 	vt_flush(vd);
905 
906 	/* Schedule for next update. */
907 	callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
908 }
909 
910 static void
911 vtterm_done(struct terminal *tm)
912 {
913 	struct vt_window *vw = tm->tm_softc;
914 	struct vt_device *vd = vw->vw_device;
915 
916 	if (kdb_active || panicstr != NULL) {
917 		/* Switch to the debugger. */
918 		if (vd->vd_curwindow != vw) {
919 			vd->vd_curwindow = vw;
920 			vd->vd_flags |= VDF_INVALID;
921 			if (vd->vd_driver->vd_postswitch)
922 				vd->vd_driver->vd_postswitch(vd);
923 		}
924 		vd->vd_flags &= ~VDF_SPLASH;
925 		vt_flush(vd);
926 	} else if (!(vd->vd_flags & VDF_ASYNC)) {
927 		vt_flush(vd);
928 	}
929 }
930 
931 #ifdef DEV_SPLASH
932 static void
933 vtterm_splash(struct vt_device *vd)
934 {
935 	vt_axis_t top, left;
936 
937 	/* Display a nice boot splash. */
938 	if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
939 
940 		top = (vd->vd_height - vt_logo_height) / 2;
941 		left = (vd->vd_width - vt_logo_width) / 2;
942 		switch (vt_logo_depth) {
943 		case 1:
944 			/* XXX: Unhardcode colors! */
945 			vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
946 			    top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
947 		}
948 		vd->vd_flags |= VDF_SPLASH;
949 	}
950 }
951 #endif
952 
953 
954 static void
955 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
956 {
957 	struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
958 	struct vt_window *vw = tm->tm_softc;
959 	struct vt_device *vd = vw->vw_device;
960 	struct winsize wsz;
961 
962 	if (getenv("kern.vt.disable"))
963 		return;
964 
965 	if (vd->vd_flags & VDF_INITIALIZED)
966 		/* Initialization already done. */
967 		return;
968 
969 	SET_FOREACH(vtdlist, vt_drv_set) {
970 		vtd = *vtdlist;
971 		if (vtd->vd_probe == NULL)
972 			continue;
973 		if (vtd->vd_probe(vd) == CN_DEAD)
974 			continue;
975 		if ((vtdbest == NULL) ||
976 		    (vtd->vd_priority > vtdbest->vd_priority))
977 			vtdbest = vtd;
978 	}
979 	if (vtdbest == NULL) {
980 		cp->cn_pri = CN_DEAD;
981 		vd->vd_flags |= VDF_DEAD;
982 	} else {
983 		vd->vd_driver = vtdbest;
984 		cp->cn_pri = vd->vd_driver->vd_init(vd);
985 	}
986 
987 	/* Check if driver's vt_init return CN_DEAD. */
988 	if (cp->cn_pri == CN_DEAD) {
989 		vd->vd_flags |= VDF_DEAD;
990 	}
991 
992 	/* Initialize any early-boot keyboard drivers */
993 	kbd_configure(KB_CONF_PROBE_ONLY);
994 
995 	vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
996 	vd->vd_windows[VT_CONSWINDOW] = vw;
997 	sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
998 
999 	/* Attach default font if not in TEXTMODE. */
1000 	if (!(vd->vd_flags & VDF_TEXTMODE))
1001 		vw->vw_font = vtfont_ref(&vt_font_default);
1002 
1003 	vtbuf_init_early(&vw->vw_buf);
1004 	vt_winsize(vd, vw->vw_font, &wsz);
1005 	terminal_set_winsize(tm, &wsz);
1006 
1007 	if (vtdbest != NULL) {
1008 #ifdef DEV_SPLASH
1009 		vtterm_splash(vd);
1010 #endif
1011 		vd->vd_flags |= VDF_INITIALIZED;
1012 	}
1013 }
1014 
1015 static int
1016 vtterm_cngetc(struct terminal *tm)
1017 {
1018 	struct vt_window *vw = tm->tm_softc;
1019 	struct vt_device *vd = vw->vw_device;
1020 	keyboard_t *kbd;
1021 	int state;
1022 	u_int c;
1023 
1024 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
1025 		return (*vw->vw_kbdsq++);
1026 
1027 	state = 0;
1028 	/* Make sure the splash screen is not there. */
1029 	if (vd->vd_flags & VDF_SPLASH) {
1030 		/* Remove splash */
1031 		vd->vd_flags &= ~VDF_SPLASH;
1032 		/* Mark screen as invalid to force update */
1033 		vd->vd_flags |= VDF_INVALID;
1034 		vt_flush(vd);
1035 	}
1036 
1037 	/* Stripped down keyboard handler. */
1038 	kbd = kbd_get_keyboard(vd->vd_keyboard);
1039 	if (kbd == NULL)
1040 		return (-1);
1041 
1042 	/* Force keyboard input mode to K_XLATE */
1043 	c = K_XLATE;
1044 	kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1045 
1046 	/* Switch the keyboard to polling to make it work here. */
1047 	kbdd_poll(kbd, TRUE);
1048 	c = kbdd_read_char(kbd, 0);
1049 	kbdd_poll(kbd, FALSE);
1050 	if (c & RELKEY)
1051 		return (-1);
1052 
1053 	if (vw->vw_flags & VWF_SCROLL) {
1054 		vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1055 		vt_flush(vd);
1056 		return (-1);
1057 	}
1058 
1059 	/* Stripped down handling of vt_kbdevent(), without locking, etc. */
1060 	if (c & SPCLKEY) {
1061 		switch (c) {
1062 		case SPCLKEY | SLK:
1063 			kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1064 			if (state & SLKED) {
1065 				/* Turn scrolling on. */
1066 				vw->vw_flags |= VWF_SCROLL;
1067 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
1068 			} else {
1069 				/* Turn scrolling off. */
1070 				vt_scroll(vw, 0, VHS_END);
1071 				vw->vw_flags &= ~VWF_SCROLL;
1072 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
1073 			}
1074 			break;
1075 		/* XXX: KDB can handle history. */
1076 		case SPCLKEY | FKEY | F(50): /* Arrow up. */
1077 			vw->vw_kbdsq = "\x1b[A";
1078 			break;
1079 		case SPCLKEY | FKEY | F(58): /* Arrow down. */
1080 			vw->vw_kbdsq = "\x1b[B";
1081 			break;
1082 		case SPCLKEY | FKEY | F(55): /* Arrow right. */
1083 			vw->vw_kbdsq = "\x1b[C";
1084 			break;
1085 		case SPCLKEY | FKEY | F(53): /* Arrow left. */
1086 			vw->vw_kbdsq = "\x1b[D";
1087 			break;
1088 		}
1089 
1090 		/* Force refresh to make scrollback work. */
1091 		vt_flush(vd);
1092 	} else if (KEYFLAGS(c) == 0) {
1093 		return (KEYCHAR(c));
1094 	}
1095 
1096 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
1097 		return (*vw->vw_kbdsq++);
1098 
1099 	return (-1);
1100 }
1101 
1102 static void
1103 vtterm_opened(struct terminal *tm, int opened)
1104 {
1105 	struct vt_window *vw = tm->tm_softc;
1106 	struct vt_device *vd = vw->vw_device;
1107 
1108 	VT_LOCK(vd);
1109 	vd->vd_flags &= ~VDF_SPLASH;
1110 	if (opened)
1111 		vw->vw_flags |= VWF_OPENED;
1112 	else {
1113 		vw->vw_flags &= ~VWF_OPENED;
1114 		/* TODO: finish ACQ/REL */
1115 	}
1116 	VT_UNLOCK(vd);
1117 }
1118 
1119 static int
1120 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1121 {
1122 	struct vt_device *vd = vw->vw_device;
1123 	struct terminal *tm = vw->vw_terminal;
1124 	term_pos_t size;
1125 	struct winsize wsz;
1126 
1127 	/*
1128 	 * Changing fonts.
1129 	 *
1130 	 * Changing fonts is a little tricky.  We must prevent
1131 	 * simultaneous access to the device, so we must stop
1132 	 * the display timer and the terminal from accessing.
1133 	 * We need to switch fonts and grow our screen buffer.
1134 	 *
1135 	 * XXX: Right now the code uses terminal_mute() to
1136 	 * prevent data from reaching the console driver while
1137 	 * resizing the screen buffer.  This isn't elegant...
1138 	 */
1139 
1140 	VT_LOCK(vd);
1141 	if (vw->vw_flags & VWF_BUSY) {
1142 		/* Another process is changing the font. */
1143 		VT_UNLOCK(vd);
1144 		return (EBUSY);
1145 	}
1146 	if (vw->vw_font == NULL) {
1147 		/* Our device doesn't need fonts. */
1148 		VT_UNLOCK(vd);
1149 		return (ENOTTY);
1150 	}
1151 	vw->vw_flags |= VWF_BUSY;
1152 	VT_UNLOCK(vd);
1153 
1154 	vt_termsize(vd, vf, &size);
1155 	vt_winsize(vd, vf, &wsz);
1156 	/* Save offset to font aligned area. */
1157 	vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1158 	vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1159 
1160 	/* Grow the screen buffer and terminal. */
1161 	terminal_mute(tm, 1);
1162 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1163 	terminal_set_winsize_blank(tm, &wsz, 0);
1164 	terminal_mute(tm, 0);
1165 
1166 	/* Actually apply the font to the current window. */
1167 	VT_LOCK(vd);
1168 	vtfont_unref(vw->vw_font);
1169 	vw->vw_font = vtfont_ref(vf);
1170 
1171 	/* Force a full redraw the next timer tick. */
1172 	if (vd->vd_curwindow == vw)
1173 		vd->vd_flags |= VDF_INVALID;
1174 	vw->vw_flags &= ~VWF_BUSY;
1175 	VT_UNLOCK(vd);
1176 	return (0);
1177 }
1178 
1179 static int
1180 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1181 {
1182 	struct vt_device *vd = vw->vw_device;
1183 	int x, y, off_x, off_y;
1184 
1185 	if (vd->vd_driver->vd_drawrect == NULL)
1186 		return (ENOTSUP);
1187 
1188 	x = vd->vd_width - 1;
1189 	y = vd->vd_height - 1;
1190 	off_x = vd->vd_offset.tp_col;
1191 	off_y = vd->vd_offset.tp_row;
1192 
1193 	/* Top bar. */
1194 	if (off_y > 0)
1195 		vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1196 	/* Left bar. */
1197 	if (off_x > 0)
1198 		vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1199 		    1, c);
1200 	/* Right bar.  May be 1 pixel wider than necessary due to rounding. */
1201 	vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1202 	/* Bottom bar.  May be 1 mixel taller than necessary due to rounding. */
1203 	vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1204 
1205 	return (0);
1206 }
1207 
1208 static int
1209 vt_proc_alive(struct vt_window *vw)
1210 {
1211 	struct proc *p;
1212 
1213 	if (vw->vw_smode.mode != VT_PROCESS)
1214 		return (FALSE);
1215 
1216 	if (vw->vw_proc) {
1217 		if ((p = pfind(vw->vw_pid)) != NULL)
1218 			PROC_UNLOCK(p);
1219 		if (vw->vw_proc == p)
1220 			return (TRUE);
1221 		vw->vw_proc = NULL;
1222 		vw->vw_smode.mode = VT_AUTO;
1223 		DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1224 		vw->vw_pid = 0;
1225 	}
1226 	return (FALSE);
1227 }
1228 
1229 static int
1230 signal_vt_rel(struct vt_window *vw)
1231 {
1232 
1233 	if (vw->vw_smode.mode != VT_PROCESS)
1234 		return (FALSE);
1235 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1236 		vw->vw_proc = NULL;
1237 		vw->vw_pid = 0;
1238 		return (TRUE);
1239 	}
1240 	vw->vw_flags |= VWF_SWWAIT_REL;
1241 	PROC_LOCK(vw->vw_proc);
1242 	kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1243 	PROC_UNLOCK(vw->vw_proc);
1244 	DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1245 	return (TRUE);
1246 }
1247 
1248 static int
1249 signal_vt_acq(struct vt_window *vw)
1250 {
1251 
1252 	if (vw->vw_smode.mode != VT_PROCESS)
1253 		return (FALSE);
1254 	if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1255 		cnavailable(vw->vw_terminal->consdev, FALSE);
1256 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1257 		vw->vw_proc = NULL;
1258 		vw->vw_pid = 0;
1259 		return (TRUE);
1260 	}
1261 	vw->vw_flags |= VWF_SWWAIT_ACQ;
1262 	PROC_LOCK(vw->vw_proc);
1263 	kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1264 	PROC_UNLOCK(vw->vw_proc);
1265 	DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1266 	return (TRUE);
1267 }
1268 
1269 static int
1270 finish_vt_rel(struct vt_window *vw, int release, int *s)
1271 {
1272 
1273 	if (vw->vw_flags & VWF_SWWAIT_REL) {
1274 		vw->vw_flags &= ~VWF_SWWAIT_REL;
1275 		if (release) {
1276 			callout_drain(&vw->vw_proc_dead_timer);
1277 			vt_late_window_switch(vw->vw_switch_to);
1278 		}
1279 		return (0);
1280 	}
1281 	return (EINVAL);
1282 }
1283 
1284 static int
1285 finish_vt_acq(struct vt_window *vw)
1286 {
1287 
1288 	if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1289 		vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1290 		return (0);
1291 	}
1292 	return (EINVAL);
1293 }
1294 
1295 #ifndef SC_NO_CUTPASTE
1296 static void
1297 vt_mouse_terminput_button(struct vt_device *vd, int button)
1298 {
1299 	struct vt_window *vw;
1300 	struct vt_font *vf;
1301 	char mouseb[6] = "\x1B[M";
1302 	int i, x, y;
1303 
1304 	vw = vd->vd_curwindow;
1305 	vf = vw->vw_font;
1306 
1307 	/* Translate to char position. */
1308 	x = vd->vd_mx / vf->vf_width;
1309 	y = vd->vd_my / vf->vf_height;
1310 	/* Avoid overflow. */
1311 	x = MIN(x, 255 - '!');
1312 	y = MIN(y, 255 - '!');
1313 
1314 	mouseb[3] = ' ' + button;
1315 	mouseb[4] = '!' + x;
1316 	mouseb[5] = '!' + y;
1317 
1318 	for (i = 0; i < sizeof(mouseb); i++ )
1319 		terminal_input_char(vw->vw_terminal, mouseb[i]);
1320 }
1321 
1322 static void
1323 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1324     int cnt)
1325 {
1326 
1327 	switch (type) {
1328 	case MOUSE_BUTTON_EVENT:
1329 		if (cnt > 0) {
1330 			/* Mouse button pressed. */
1331 			if (event & MOUSE_BUTTON1DOWN)
1332 				vt_mouse_terminput_button(vd, 0);
1333 			if (event & MOUSE_BUTTON2DOWN)
1334 				vt_mouse_terminput_button(vd, 1);
1335 			if (event & MOUSE_BUTTON3DOWN)
1336 				vt_mouse_terminput_button(vd, 2);
1337 		} else {
1338 			/* Mouse button released. */
1339 			vt_mouse_terminput_button(vd, 3);
1340 		}
1341 		break;
1342 #ifdef notyet
1343 	case MOUSE_MOTION_EVENT:
1344 		if (mouse->u.data.z < 0) {
1345 			/* Scroll up. */
1346 			sc_mouse_input_button(vd, 64);
1347 		} else if (mouse->u.data.z > 0) {
1348 			/* Scroll down. */
1349 			sc_mouse_input_button(vd, 65);
1350 		}
1351 		break;
1352 #endif
1353 	}
1354 }
1355 
1356 void
1357 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1358 {
1359 	struct vt_device *vd;
1360 	struct vt_window *vw;
1361 	struct vt_font *vf;
1362 	term_pos_t size;
1363 	term_char_t *buf;
1364 	int i, len, mark;
1365 
1366 	vd = main_vd;
1367 	vw = vd->vd_curwindow;
1368 	vf = vw->vw_font;
1369 	mark = 0;
1370 
1371 	if (vw->vw_flags & VWF_MOUSE_HIDE)
1372 		return; /* Mouse disabled. */
1373 
1374 	if (vf == NULL)	/* Text mode. */
1375 		return;
1376 
1377 	/*
1378 	 * TODO: add flag about pointer position changed, to not redraw chars
1379 	 * under mouse pointer when nothing changed.
1380 	 */
1381 
1382 	if (vw->vw_mouse_level > 0)
1383 		vt_mouse_terminput(vd, type, x, y, event, cnt);
1384 
1385 	switch (type) {
1386 	case MOUSE_ACTION:
1387 	case MOUSE_MOTION_EVENT:
1388 		/* Movement */
1389 		x += vd->vd_mx;
1390 		y += vd->vd_my;
1391 
1392 		vt_termsize(vd, vf, &size);
1393 
1394 		/* Apply limits. */
1395 		x = MAX(x, 0);
1396 		y = MAX(y, 0);
1397 		x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1398 		y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1399 
1400 		vd->vd_mx = x;
1401 		vd->vd_my = y;
1402 		if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1403 		    (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1404 			vd->vd_mx / vf->vf_width,
1405 			vd->vd_my / vf->vf_height) == 1)) {
1406 
1407 			/*
1408 			 * We have something marked to copy, so update pointer
1409 			 * to window with selection.
1410 			 */
1411 			vd->vd_markedwin = vw;
1412 		}
1413 		return; /* Done */
1414 	case MOUSE_BUTTON_EVENT:
1415 		/* Buttons */
1416 		break;
1417 	default:
1418 		return; /* Done */
1419 	}
1420 
1421 	switch (event) {
1422 	case MOUSE_BUTTON1DOWN:
1423 		switch (cnt % 4) {
1424 		case 0:	/* up */
1425 			mark = VTB_MARK_END;
1426 			break;
1427 		case 1: /* single click: start cut operation */
1428 			mark = VTB_MARK_START;
1429 			break;
1430 		case 2:	/* double click: cut a word */
1431 			mark = VTB_MARK_WORD;
1432 			break;
1433 		case 3:	/* triple click: cut a line */
1434 			mark = VTB_MARK_ROW;
1435 			break;
1436 		}
1437 		break;
1438 	case VT_MOUSE_PASTEBUTTON:
1439 		switch (cnt) {
1440 		case 0:	/* up */
1441 			break;
1442 		default:
1443 			if (vd->vd_markedwin == NULL)
1444 				return;
1445 			/* Get current selecton size in bytes. */
1446 			len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1447 			if (len <= 0)
1448 				return;
1449 
1450 			buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1451 			/* Request cupy/paste buffer data, no more than `len' */
1452 			vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1453 			    len);
1454 
1455 			len /= sizeof(term_char_t);
1456 			for (i = 0; i < len; i++ ) {
1457 				if (buf[i] == '\0')
1458 					continue;
1459 				terminal_input_char(vw->vw_terminal, buf[i]);
1460 			}
1461 
1462 			/* Done, so cleanup. */
1463 			free(buf, M_VT);
1464 			break;
1465 		}
1466 		return; /* Done */
1467 	case VT_MOUSE_EXTENDBUTTON:
1468 		switch (cnt) {
1469 		case 0:	/* up */
1470 			if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1471 				mark = VTB_MARK_EXTEND;
1472 			else
1473 				mark = 0;
1474 			break;
1475 		default:
1476 			mark = VTB_MARK_EXTEND;
1477 			break;
1478 		}
1479 		break;
1480 	default:
1481 		return; /* Done */
1482 	}
1483 
1484 	/* Save buttons state. */
1485 	if (cnt > 0)
1486 		vd->vd_mstate |= event;
1487 	else
1488 		vd->vd_mstate &= ~event;
1489 
1490 	if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1491 	    vd->vd_my / vf->vf_height) == 1) {
1492 		/*
1493 		 * We have something marked to copy, so update pointer to
1494 		 * window with selection.
1495 		 */
1496 		vd->vd_markedwin = vw;
1497 	}
1498 }
1499 
1500 void
1501 vt_mouse_state(int show)
1502 {
1503 	struct vt_device *vd;
1504 	struct vt_window *vw;
1505 
1506 	vd = main_vd;
1507 	vw = vd->vd_curwindow;
1508 
1509 	switch (show) {
1510 	case VT_MOUSE_HIDE:
1511 		vw->vw_flags |= VWF_MOUSE_HIDE;
1512 		break;
1513 	case VT_MOUSE_SHOW:
1514 		vw->vw_flags &= ~VWF_MOUSE_HIDE;
1515 		break;
1516 	}
1517 }
1518 #endif
1519 
1520 static int
1521 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1522     int nprot, vm_memattr_t *memattr)
1523 {
1524 	struct vt_window *vw = tm->tm_softc;
1525 	struct vt_device *vd = vw->vw_device;
1526 
1527 	if (vd->vd_driver->vd_fb_mmap)
1528 		return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1529 		    memattr));
1530 
1531 	return (ENXIO);
1532 }
1533 
1534 static int
1535 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1536     struct thread *td)
1537 {
1538 	struct vt_window *vw = tm->tm_softc;
1539 	struct vt_device *vd = vw->vw_device;
1540 	keyboard_t *kbd;
1541 	int error, i, s;
1542 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1543     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1544 	int ival;
1545 
1546 	switch (cmd) {
1547 	case _IO('v', 4):
1548 		cmd = VT_RELDISP;
1549 		break;
1550 	case _IO('v', 5):
1551 		cmd = VT_ACTIVATE;
1552 		break;
1553 	case _IO('v', 6):
1554 		cmd = VT_WAITACTIVE;
1555 		break;
1556 	case _IO('K', 20):
1557 		cmd = KDSKBSTATE;
1558 		break;
1559 	case _IO('K', 67):
1560 		cmd = KDSETRAD;
1561 		break;
1562 	case _IO('K', 7):
1563 		cmd = KDSKBMODE;
1564 		break;
1565 	case _IO('K', 8):
1566 		cmd = KDMKTONE;
1567 		break;
1568 	case _IO('K', 63):
1569 		cmd = KIOCSOUND;
1570 		break;
1571 	case _IO('K', 66):
1572 		cmd = KDSETLED;
1573 		break;
1574 	case _IO('c', 110):
1575 		cmd = CONS_SETKBD;
1576 		break;
1577 	default:
1578 		goto skip_thunk;
1579 	}
1580 	ival = IOCPARM_IVAL(data);
1581 	data = (caddr_t)&ival;
1582 skip_thunk:
1583 #endif
1584 
1585 	switch (cmd) {
1586 	case KDSETRAD:		/* set keyboard repeat & delay rates (old) */
1587 		if (*(int *)data & ~0x7f)
1588 			return (EINVAL);
1589 	case GIO_KEYMAP:
1590 	case PIO_KEYMAP:
1591 	case GIO_DEADKEYMAP:
1592 	case PIO_DEADKEYMAP:
1593 	case GETFKEY:
1594 	case SETFKEY:
1595 	case KDGKBINFO:
1596 	case KDGKBTYPE:
1597 	case KDSKBSTATE:	/* set keyboard state (locks) */
1598 	case KDGKBSTATE:	/* get keyboard state (locks) */
1599 	case KDGETREPEAT:	/* get keyboard repeat & delay rates */
1600 	case KDSETREPEAT:	/* set keyboard repeat & delay rates (new) */
1601 	case KDSETLED:		/* set keyboard LED status */
1602 	case KDGETLED:		/* get keyboard LED status */
1603 	case KBADDKBD:		/* add/remove keyboard to/from mux */
1604 	case KBRELKBD: {
1605 		error = 0;
1606 
1607 		mtx_lock(&Giant);
1608 		kbd = kbd_get_keyboard(vd->vd_keyboard);
1609 		if (kbd != NULL)
1610 			error = kbdd_ioctl(kbd, cmd, data);
1611 		mtx_unlock(&Giant);
1612 		if (error == ENOIOCTL) {
1613 			if (cmd == KDGKBTYPE) {
1614 				/* always return something? XXX */
1615 				*(int *)data = 0;
1616 			} else {
1617 				return (ENODEV);
1618 			}
1619 		}
1620 		return (error);
1621 	}
1622 	case KDGKBMODE: {
1623 		int mode = -1;
1624 
1625 		mtx_lock(&Giant);
1626 		kbd = kbd_get_keyboard(vd->vd_keyboard);
1627 		if (kbd != NULL) {
1628 			kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1629 		}
1630 		mtx_unlock(&Giant);
1631 		DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1632 		*(int *)data = mode;
1633 		return (0);
1634 	}
1635 	case KDSKBMODE: {
1636 		int mode;
1637 
1638 		mode = *(int *)data;
1639 		switch (mode) {
1640 		case K_XLATE:
1641 		case K_RAW:
1642 		case K_CODE:
1643 			vw->vw_kbdmode = mode;
1644 			if (vw == vd->vd_curwindow) {
1645 				keyboard_t *kbd;
1646 				error = 0;
1647 
1648 				mtx_lock(&Giant);
1649 				kbd = kbd_get_keyboard(vd->vd_keyboard);
1650 				if (kbd != NULL) {
1651 					error = kbdd_ioctl(kbd, KDSKBMODE,
1652 					    (void *)&mode);
1653 				}
1654 				mtx_unlock(&Giant);
1655 			}
1656 			return (0);
1657 		default:
1658 			return (EINVAL);
1659 		}
1660 	}
1661 	case FBIOGTYPE:
1662 	case FBIO_GETWINORG:	/* get frame buffer window origin */
1663 	case FBIO_GETDISPSTART:	/* get display start address */
1664 	case FBIO_GETLINEWIDTH:	/* get scan line width in bytes */
1665 	case FBIO_BLANK:	/* blank display */
1666 		if (vd->vd_driver->vd_fb_ioctl)
1667 			return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1668 		break;
1669 	case CONS_BLANKTIME:
1670 		/* XXX */
1671 		return (0);
1672 	case CONS_GET:
1673 		/* XXX */
1674 		*(int *)data = M_CG640x480;
1675 		return (0);
1676 	case CONS_BELLTYPE: 	/* set bell type sound */
1677 		if ((*(int *)data) & CONS_QUIET_BELL)
1678 			vd->vd_flags |= VDF_QUIET_BELL;
1679 		else
1680 			vd->vd_flags &= ~VDF_QUIET_BELL;
1681 		return (0);
1682 	case CONS_GETINFO: {
1683 		vid_info_t *vi = (vid_info_t *)data;
1684 
1685 		vi->m_num = vd->vd_curwindow->vw_number + 1;
1686 		/* XXX: other fields! */
1687 		return (0);
1688 	}
1689 	case CONS_GETVERS:
1690 		*(int *)data = 0x200;
1691 		return (0);
1692 	case CONS_MODEINFO:
1693 		/* XXX */
1694 		return (0);
1695 	case CONS_MOUSECTL: {
1696 		mouse_info_t *mouse = (mouse_info_t*)data;
1697 
1698 		/*
1699 		 * This has no effect on vt(4).  We don't draw any mouse
1700 		 * cursor.  Just ignore MOUSE_HIDE and MOUSE_SHOW to
1701 		 * prevent excessive errors.  All the other commands
1702 		 * should not be applied to individual TTYs, but only to
1703 		 * consolectl.
1704 		 */
1705 		switch (mouse->operation) {
1706 		case MOUSE_HIDE:
1707 			vd->vd_flags &= ~VDF_MOUSECURSOR;
1708 			return (0);
1709 		case MOUSE_SHOW:
1710 			vd->vd_mx = vd->vd_width / 2;
1711 			vd->vd_my = vd->vd_height / 2;
1712 			vd->vd_flags |= VDF_MOUSECURSOR;
1713 			return (0);
1714 		default:
1715 			return (EINVAL);
1716 		}
1717 	}
1718 	case PIO_VFONT: {
1719 		struct vt_font *vf;
1720 
1721 		error = vtfont_load((void *)data, &vf);
1722 		if (error != 0)
1723 			return (error);
1724 
1725 		error = vt_change_font(vw, vf);
1726 		if (error == 0) {
1727 			/* XXX: replace 0 with current bg color. */
1728 			vt_set_border(vw, vf, 0);
1729 		}
1730 		vtfont_unref(vf);
1731 		return (error);
1732 	}
1733 	case GIO_SCRNMAP: {
1734 		scrmap_t *sm = (scrmap_t *)data;
1735 		int i;
1736 
1737 		/* We don't have screen maps, so return a handcrafted one. */
1738 		for (i = 0; i < 256; i++)
1739 			sm->scrmap[i] = i;
1740 		return (0);
1741 	}
1742 	case KDSETMODE:
1743 		/* XXX */
1744 		return (0);
1745 	case KDENABIO:      	/* allow io operations */
1746 		error = priv_check(td, PRIV_IO);
1747 		if (error != 0)
1748 			return (error);
1749 		error = securelevel_gt(td->td_ucred, 0);
1750 		if (error != 0)
1751 			return (error);
1752 #if defined(__i386__)
1753 		td->td_frame->tf_eflags |= PSL_IOPL;
1754 #elif defined(__amd64__)
1755 		td->td_frame->tf_rflags |= PSL_IOPL;
1756 #endif
1757 		return (0);
1758 	case KDDISABIO:     	/* disallow io operations (default) */
1759 #if defined(__i386__)
1760 		td->td_frame->tf_eflags &= ~PSL_IOPL;
1761 #elif defined(__amd64__)
1762 		td->td_frame->tf_rflags &= ~PSL_IOPL;
1763 #endif
1764 		return (0);
1765 	case KDMKTONE:      	/* sound the bell */
1766 		vtterm_beep(tm, *(u_int *)data);
1767 		return (0);
1768 	case KIOCSOUND:     	/* make tone (*data) hz */
1769 		/* TODO */
1770 		return (0);
1771 	case CONS_SETKBD: 		/* set the new keyboard */
1772 		mtx_lock(&Giant);
1773 		error = 0;
1774 		if (vd->vd_keyboard != *(int *)data) {
1775 			kbd = kbd_get_keyboard(*(int *)data);
1776 			if (kbd == NULL) {
1777 				mtx_unlock(&Giant);
1778 				return (EINVAL);
1779 			}
1780 			i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1781 			    (void *)&vd->vd_keyboard, vt_kbdevent, vd);
1782 			if (i >= 0) {
1783 				if (vd->vd_keyboard != -1) {
1784 					kbd_release(kbd,
1785 					    (void *)&vd->vd_keyboard);
1786 				}
1787 				kbd = kbd_get_keyboard(i);
1788 				vd->vd_keyboard = i;
1789 
1790 				(void)kbdd_ioctl(kbd, KDSKBMODE,
1791 				    (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1792 			} else {
1793 				error = EPERM;	/* XXX */
1794 			}
1795 		}
1796 		mtx_unlock(&Giant);
1797 		return (error);
1798 	case CONS_RELKBD: 		/* release the current keyboard */
1799 		mtx_lock(&Giant);
1800 		error = 0;
1801 		if (vd->vd_keyboard != -1) {
1802 			kbd = kbd_get_keyboard(vd->vd_keyboard);
1803 			if (kbd == NULL) {
1804 				mtx_unlock(&Giant);
1805 				return (EINVAL);
1806 			}
1807 			error = kbd_release(kbd, (void *)&vd->vd_keyboard);
1808 			if (error == 0) {
1809 				vd->vd_keyboard = -1;
1810 			}
1811 		}
1812 		mtx_unlock(&Giant);
1813 		return (error);
1814 	case VT_ACTIVATE: {
1815 		int win;
1816 		win = *(int *)data - 1;
1817 		DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1818 		    VT_UNIT(vw), win);
1819 		if ((win > VT_MAXWINDOWS) || (win < 0))
1820 			return (EINVAL);
1821 		return (vt_proc_window_switch(vd->vd_windows[win]));
1822 	}
1823 	case VT_GETACTIVE:
1824 		*(int *)data = vd->vd_curwindow->vw_number + 1;
1825 		return (0);
1826 	case VT_GETINDEX:
1827 		*(int *)data = vw->vw_number + 1;
1828 		return (0);
1829 	case VT_LOCKSWITCH:
1830 		/* TODO: Check current state, switching can be in progress. */
1831 		if ((*(int *)data) == 0x01)
1832 			vw->vw_flags |= VWF_VTYLOCK;
1833 		else if ((*(int *)data) == 0x02)
1834 			vw->vw_flags &= ~VWF_VTYLOCK;
1835 		else
1836 			return (EINVAL);
1837 		return (0);
1838 	case VT_OPENQRY:
1839 		VT_LOCK(vd);
1840 		for (i = 0; i < VT_MAXWINDOWS; i++) {
1841 			vw = vd->vd_windows[i];
1842 			if (vw == NULL)
1843 				continue;
1844 			if (!(vw->vw_flags & VWF_OPENED)) {
1845 				*(int *)data = vw->vw_number + 1;
1846 				VT_UNLOCK(vd);
1847 				return (0);
1848 			}
1849 		}
1850 		VT_UNLOCK(vd);
1851 		return (EINVAL);
1852 	case VT_WAITACTIVE:
1853 		error = 0;
1854 
1855 		i = *(unsigned int *)data;
1856 		if (i > VT_MAXWINDOWS)
1857 			return (EINVAL);
1858 		if (i != 0)
1859 			vw = vd->vd_windows[i - 1];
1860 
1861 		VT_LOCK(vd);
1862 		while (vd->vd_curwindow != vw && error == 0)
1863 			error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1864 		VT_UNLOCK(vd);
1865 		return (error);
1866 	case VT_SETMODE: {    	/* set screen switcher mode */
1867 		struct vt_mode *mode;
1868 		struct proc *p1;
1869 
1870 		mode = (struct vt_mode *)data;
1871 		DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1872 		if (vw->vw_smode.mode == VT_PROCESS) {
1873 			p1 = pfind(vw->vw_pid);
1874 			if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1875 				if (p1)
1876 					PROC_UNLOCK(p1);
1877 				DPRINTF(5, "error EPERM\n");
1878 				return (EPERM);
1879 			}
1880 			if (p1)
1881 				PROC_UNLOCK(p1);
1882 		}
1883 		if (mode->mode == VT_AUTO) {
1884 			vw->vw_smode.mode = VT_AUTO;
1885 			vw->vw_proc = NULL;
1886 			vw->vw_pid = 0;
1887 			DPRINTF(5, "VT_AUTO, ");
1888 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1889 				cnavailable(vw->vw_terminal->consdev, TRUE);
1890 			/* were we in the middle of the vty switching process? */
1891 			if (finish_vt_rel(vw, TRUE, &s) == 0)
1892 				DPRINTF(5, "reset WAIT_REL, ");
1893 			if (finish_vt_acq(vw) == 0)
1894 				DPRINTF(5, "reset WAIT_ACQ, ");
1895 			return (0);
1896 		} else if (mode->mode == VT_PROCESS) {
1897 			if (!ISSIGVALID(mode->relsig) ||
1898 			    !ISSIGVALID(mode->acqsig) ||
1899 			    !ISSIGVALID(mode->frsig)) {
1900 				DPRINTF(5, "error EINVAL\n");
1901 				return (EINVAL);
1902 			}
1903 			DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1904 			bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1905 			vw->vw_proc = td->td_proc;
1906 			vw->vw_pid = vw->vw_proc->p_pid;
1907 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1908 				cnavailable(vw->vw_terminal->consdev, FALSE);
1909 		} else {
1910 			DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1911 			    mode->mode);
1912 			return (EINVAL);
1913 		}
1914 		DPRINTF(5, "\n");
1915 		return (0);
1916 	}
1917 	case VT_GETMODE:	/* get screen switcher mode */
1918 		bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1919 		return (0);
1920 
1921 	case VT_RELDISP:	/* screen switcher ioctl */
1922 		/*
1923 		 * This must be the current vty which is in the VT_PROCESS
1924 		 * switching mode...
1925 		 */
1926 		if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1927 		    VT_PROCESS)) {
1928 			return (EINVAL);
1929 		}
1930 		/* ...and this process is controlling it. */
1931 		if (vw->vw_proc != td->td_proc) {
1932 			return (EPERM);
1933 		}
1934 		error = EINVAL;
1935 		switch(*(int *)data) {
1936 		case VT_FALSE:	/* user refuses to release screen, abort */
1937 			if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1938 				DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1939 				    SC_DRIVER_NAME, VT_UNIT(vw));
1940 			break;
1941 		case VT_TRUE:	/* user has released screen, go on */
1942 			/* finish_vt_rel(..., TRUE, ...) should not be locked */
1943 			if (vw->vw_flags & VWF_SWWAIT_REL) {
1944 				if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1945 					DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1946 					    SC_DRIVER_NAME, VT_UNIT(vw));
1947 			} else {
1948 				error = EINVAL;
1949 			}
1950 			return (error);
1951 		case VT_ACKACQ:	/* acquire acknowledged, switch completed */
1952 			if ((error = finish_vt_acq(vw)) == 0)
1953 				DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1954 				    SC_DRIVER_NAME, VT_UNIT(vw));
1955 			break;
1956 		default:
1957 			break;
1958 		}
1959 		return (error);
1960 	}
1961 
1962 	return (ENOIOCTL);
1963 }
1964 
1965 static struct vt_window *
1966 vt_allocate_window(struct vt_device *vd, unsigned int window)
1967 {
1968 	struct vt_window *vw;
1969 	struct terminal *tm;
1970 	term_pos_t size;
1971 	struct winsize wsz;
1972 
1973 	vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1974 	vw->vw_device = vd;
1975 	vw->vw_number = window;
1976 	vw->vw_kbdmode = K_XLATE;
1977 
1978 	if (!(vd->vd_flags & VDF_TEXTMODE))
1979 		vw->vw_font = vtfont_ref(&vt_font_default);
1980 
1981 	vt_termsize(vd, vw->vw_font, &size);
1982 	vt_winsize(vd, vw->vw_font, &wsz);
1983 	vtbuf_init(&vw->vw_buf, &size);
1984 
1985 	tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1986 	terminal_set_winsize(tm, &wsz);
1987 	vd->vd_windows[window] = vw;
1988 	callout_init(&vw->vw_proc_dead_timer, 0);
1989 
1990 	return (vw);
1991 }
1992 
1993 void
1994 vt_upgrade(struct vt_device *vd)
1995 {
1996 	struct vt_window *vw;
1997 	unsigned int i;
1998 
1999 	if (getenv("kern.vt.disable"))
2000 		return;
2001 
2002 	for (i = 0; i < VT_MAXWINDOWS; i++) {
2003 		vw = vd->vd_windows[i];
2004 		if (vw == NULL) {
2005 			/* New window. */
2006 			vw = vt_allocate_window(vd, i);
2007 		}
2008 		if (!(vw->vw_flags & VWF_READY)) {
2009 			callout_init(&vw->vw_proc_dead_timer, 0);
2010 			terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2011 			vw->vw_flags |= VWF_READY;
2012 			if (vw->vw_flags & VWF_CONSOLE) {
2013 				/* For existing console window. */
2014 				EVENTHANDLER_REGISTER(shutdown_pre_sync,
2015 				    vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2016 			}
2017 		}
2018 
2019 	}
2020 	VT_LOCK(vd);
2021 	if (vd->vd_curwindow == NULL)
2022 		vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2023 
2024 	if (!(vd->vd_flags & VDF_ASYNC)) {
2025 	/* Attach keyboard. */
2026 	vt_allocate_keyboard(vd);
2027 	DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
2028 
2029 		/* Init 25 Hz timer. */
2030 		callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2031 
2032 		/* Start timer when everything ready. */
2033 		vd->vd_flags |= VDF_ASYNC;
2034 		callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2035 	}
2036 
2037 	VT_UNLOCK(vd);
2038 
2039 	/* Refill settings with new sizes. */
2040 	vt_resize(vd);
2041 }
2042 
2043 static void
2044 vt_resize(struct vt_device *vd)
2045 {
2046 	struct vt_window *vw;
2047 	int i;
2048 
2049 	for (i = 0; i < VT_MAXWINDOWS; i++) {
2050 		vw = vd->vd_windows[i];
2051 		VT_LOCK(vd);
2052 		/* Assign default font to window, if not textmode. */
2053 		if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2054 			vw->vw_font = vtfont_ref(&vt_font_default);
2055 		VT_UNLOCK(vd);
2056 		/* Resize terminal windows */
2057 		vt_change_font(vw, vw->vw_font);
2058 	}
2059 }
2060 
2061 void
2062 vt_allocate(struct vt_driver *drv, void *softc)
2063 {
2064 	struct vt_device *vd;
2065 	struct winsize wsz;
2066 
2067 	if (getenv("kern.vt.disable"))
2068 		return;
2069 
2070 	if (main_vd->vd_driver == NULL) {
2071 		main_vd->vd_driver = drv;
2072 		printf("VT: initialize with new VT driver \"%s\".\n",
2073 		    drv->vd_name);
2074 	} else {
2075 		/*
2076 		 * Check if have rights to replace current driver. For example:
2077 		 * it is bad idea to replace KMS driver with generic VGA one.
2078 		 */
2079 		if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2080 			printf("VT: Driver priority %d too low. Current %d\n ",
2081 			    drv->vd_priority, main_vd->vd_driver->vd_priority);
2082 			return;
2083 		}
2084 		printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2085 		    main_vd->vd_driver->vd_name, drv->vd_name);
2086 	}
2087 	vd = main_vd;
2088 	VT_LOCK(vd);
2089 	if (drv->vd_maskbitbltchr == NULL)
2090 		drv->vd_maskbitbltchr = drv->vd_bitbltchr;
2091 
2092 	if (vd->vd_flags & VDF_ASYNC) {
2093 		/* Stop vt_flush periodic task. */
2094 		callout_drain(&vd->vd_timer);
2095 		/*
2096 		 * Mute current terminal until we done. vt_change_font (called
2097 		 * from vt_resize) will unmute it.
2098 		 */
2099 		terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2100 	}
2101 
2102 	/*
2103 	 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2104 	 * set it.
2105 	 */
2106 	vd->vd_flags &= ~VDF_TEXTMODE;
2107 
2108 	vd->vd_driver = drv;
2109 	vd->vd_softc = softc;
2110 	vd->vd_driver->vd_init(vd);
2111 	VT_UNLOCK(vd);
2112 
2113 	vt_upgrade(vd);
2114 
2115 #ifdef DEV_SPLASH
2116 	if (vd->vd_flags & VDF_SPLASH)
2117 		vtterm_splash(vd);
2118 #endif
2119 
2120 	if (vd->vd_flags & VDF_ASYNC) {
2121 		terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2122 		callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
2123 	}
2124 
2125 	termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2126 
2127 	/* Update console window sizes to actual. */
2128 	vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
2129 	terminal_set_winsize_blank(vd->vd_windows[VT_CONSWINDOW]->vw_terminal,
2130 	    &wsz, 0);
2131 }
2132 
2133 void
2134 vt_suspend()
2135 {
2136 
2137 	if (vt_suspendswitch == 0)
2138 		return;
2139 	/* Save current window. */
2140 	main_vd->vd_savedwindow = main_vd->vd_curwindow;
2141 	/* Ask holding process to free window and switch to console window */
2142 	vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2143 }
2144 
2145 void
2146 vt_resume()
2147 {
2148 
2149 	if (vt_suspendswitch == 0)
2150 		return;
2151 	/* Switch back to saved window */
2152 	if (main_vd->vd_savedwindow != NULL)
2153 		vt_proc_window_switch(main_vd->vd_savedwindow);
2154 	main_vd->vd_savedwindow = NULL;
2155 }
2156