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