]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/syscons/syscons.c
Change all uses of 'debugger' to kdb_active and remove this variable. This
[FreeBSD/FreeBSD.git] / sys / dev / syscons / syscons.c
1 /*-
2  * Copyright (c) 1992-1998 Søren Schmidt
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Sascha Wildner <saw@online.de>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_compat.h"
36 #include "opt_syscons.h"
37 #include "opt_splash.h"
38 #include "opt_ddb.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/cons.h>
45 #include <sys/consio.h>
46 #include <sys/kdb.h>
47 #include <sys/eventhandler.h>
48 #include <sys/fbio.h>
49 #include <sys/kbio.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/random.h>
57 #include <sys/reboot.h>
58 #include <sys/serial.h>
59 #include <sys/signalvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/tty.h>
62 #include <sys/power.h>
63
64 #include <machine/clock.h>
65 #if defined(__arm__) || defined(__mips__) || \
66         defined(__powerpc__) || defined(__sparc64__)
67 #include <machine/sc_machdep.h>
68 #else
69 #include <machine/pc/display.h>
70 #endif
71 #if defined( __i386__) || defined(__amd64__)
72 #include <machine/psl.h>
73 #include <machine/frame.h>
74 #endif
75 #include <machine/stdarg.h>
76
77 #include <dev/kbd/kbdreg.h>
78 #include <dev/fb/fbreg.h>
79 #include <dev/fb/splashreg.h>
80 #include <dev/syscons/syscons.h>
81
82 #define COLD 0
83 #define WARM 1
84
85 #define DEFAULT_BLANKTIME       (5*60)          /* 5 minutes */
86 #define MAX_BLANKTIME           (7*24*60*60)    /* 7 days!? */
87
88 #define KEYCODE_BS              0x0e            /* "<-- Backspace" key, XXX */
89
90 /* NULL-safe version of "tty_opened()" */
91 #define tty_opened_ns(tp)       ((tp) != NULL && tty_opened(tp))
92
93 typedef struct default_attr {
94         int             std_color;              /* normal hardware color */
95         int             rev_color;              /* reverse hardware color */
96 } default_attr;
97
98 static default_attr user_default = {
99     SC_NORM_ATTR,
100     SC_NORM_REV_ATTR,
101 };
102
103 static  int             sc_console_unit = -1;
104 static  int             sc_saver_keyb_only = 1;
105 static  scr_stat        *sc_console;
106 static  struct consdev  *sc_consptr;
107 static  scr_stat        main_console;
108 static  struct tty      *main_devs[MAXCONS];
109
110 static  char            init_done = COLD;
111 static  int             shutdown_in_progress = FALSE;
112 static  int             suspend_in_progress = FALSE;
113 static  char            sc_malloc = FALSE;
114
115 static  int             saver_mode = CONS_NO_SAVER; /* LKM/user saver */
116 static  int             run_scrn_saver = FALSE; /* should run the saver? */
117 static  int             enable_bell = TRUE; /* enable beeper */
118
119 #ifndef SC_DISABLE_REBOOT
120 static  int             enable_reboot = TRUE; /* enable keyboard reboot */
121 #endif
122
123 #ifndef SC_DISABLE_KDBKEY
124 static  int             enable_kdbkey = TRUE; /* enable keyboard debug */
125 #endif
126
127 static  long            scrn_blank_time = 0;    /* screen saver timeout value */
128 #ifdef DEV_SPLASH
129 static  int             scrn_blanked;           /* # of blanked screen */
130 static  int             sticky_splash = FALSE;
131
132 static  void            none_saver(sc_softc_t *sc, int blank) { }
133 static  void            (*current_saver)(sc_softc_t *, int) = none_saver;
134 #endif
135
136 #ifdef SC_NO_SUSPEND_VTYSWITCH
137 static  int             sc_no_suspend_vtswitch = 1;
138 #else
139 static  int             sc_no_suspend_vtswitch = 0;
140 #endif
141 static  int             sc_susp_scr;
142
143 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
144 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
145 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
146     &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
147 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell, 
148     0, "enable bell");
149 #ifndef SC_DISABLE_REBOOT
150 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
151     0, "enable keyboard reboot");
152 #endif
153 #ifndef SC_DISABLE_KDBKEY
154 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
155     0, "enable keyboard debug");
156 #endif
157 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN,
158     &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
159 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
160 #include "font.h"
161 #endif
162
163         tsw_ioctl_t     *sc_user_ioctl;
164
165 static  bios_values_t   bios_value;
166
167 static  int             enable_panic_key;
168 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
169            0, "Enable panic via keypress specified in kbdmap(5)");
170
171 #define SC_CONSOLECTL   255
172
173 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
174
175 /* prototypes */
176 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
177 static int scvidprobe(int unit, int flags, int cons);
178 static int sckbdprobe(int unit, int flags, int cons);
179 static void scmeminit(void *arg);
180 static int scdevtounit(struct tty *tp);
181 static kbd_callback_func_t sckbdevent;
182 static void scinit(int unit, int flags);
183 static scr_stat *sc_get_stat(struct tty *tp);
184 static void scterm(int unit, int flags);
185 static void scshutdown(void *, int);
186 static void scsuspend(void *);
187 static void scresume(void *);
188 static u_int scgetc(sc_softc_t *sc, u_int flags);
189 static void sc_puts(scr_stat *scp, u_char *buf, int len, int kernel);
190 #define SCGETC_CN       1
191 #define SCGETC_NONBLOCK 2
192 static void sccnupdate(scr_stat *scp);
193 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
194 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
195 static timeout_t scrn_timer;
196 static int and_region(int *s1, int *e1, int s2, int e2);
197 static void scrn_update(scr_stat *scp, int show_cursor);
198
199 #ifdef DEV_SPLASH
200 static int scsplash_callback(int event, void *arg);
201 static void scsplash_saver(sc_softc_t *sc, int show);
202 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
203 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
204 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
205 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
206 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
207 static int wait_scrn_saver_stop(sc_softc_t *sc);
208 #define scsplash_stick(stick)           (sticky_splash = (stick))
209 #else /* !DEV_SPLASH */
210 #define scsplash_stick(stick)
211 #endif /* DEV_SPLASH */
212
213 static int do_switch_scr(sc_softc_t *sc, int s);
214 static int vt_proc_alive(scr_stat *scp);
215 static int signal_vt_rel(scr_stat *scp);
216 static int signal_vt_acq(scr_stat *scp);
217 static int finish_vt_rel(scr_stat *scp, int release, int *s);
218 static int finish_vt_acq(scr_stat *scp);
219 static void exchange_scr(sc_softc_t *sc);
220 static void update_cursor_image(scr_stat *scp);
221 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
222 static void update_font(scr_stat *);
223 static int save_kbd_state(scr_stat *scp);
224 static int update_kbd_state(scr_stat *scp, int state, int mask);
225 static int update_kbd_leds(scr_stat *scp, int which);
226 static timeout_t blink_screen;
227 static struct tty *sc_alloc_tty(int, int);
228
229 static cn_probe_t       sc_cnprobe;
230 static cn_init_t        sc_cninit;
231 static cn_term_t        sc_cnterm;
232 static cn_getc_t        sc_cngetc;
233 static cn_putc_t        sc_cnputc;
234 static cn_grab_t        sc_cngrab;
235 static cn_ungrab_t      sc_cnungrab;
236
237 CONSOLE_DRIVER(sc);
238
239 static  tsw_open_t      sctty_open;
240 static  tsw_close_t     sctty_close;
241 static  tsw_outwakeup_t sctty_outwakeup;
242 static  tsw_ioctl_t     sctty_ioctl;
243 static  tsw_mmap_t      sctty_mmap;
244
245 static struct ttydevsw sc_ttydevsw = {
246         .tsw_open       = sctty_open,
247         .tsw_close      = sctty_close,
248         .tsw_outwakeup  = sctty_outwakeup,
249         .tsw_ioctl      = sctty_ioctl,
250         .tsw_mmap       = sctty_mmap,
251 };
252
253 static d_ioctl_t        consolectl_ioctl;
254 static d_close_t        consolectl_close;
255
256 static struct cdevsw consolectl_devsw = {
257         .d_version      = D_VERSION,
258         .d_flags        = D_NEEDGIANT | D_TRACKCLOSE,
259         .d_ioctl        = consolectl_ioctl,
260         .d_close        = consolectl_close,
261         .d_name         = "consolectl",
262 };
263
264 int
265 sc_probe_unit(int unit, int flags)
266 {
267     if (!vty_enabled(VTY_SC))
268         return ENXIO;
269     if (!scvidprobe(unit, flags, FALSE)) {
270         if (bootverbose)
271             printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
272         return ENXIO;
273     }
274
275     /* syscons will be attached even when there is no keyboard */
276     sckbdprobe(unit, flags, FALSE);
277
278     return 0;
279 }
280
281 /* probe video adapters, return TRUE if found */ 
282 static int
283 scvidprobe(int unit, int flags, int cons)
284 {
285     /*
286      * Access the video adapter driver through the back door!
287      * Video adapter drivers need to be configured before syscons.
288      * However, when syscons is being probed as the low-level console,
289      * they have not been initialized yet.  We force them to initialize
290      * themselves here. XXX
291      */
292     vid_configure(cons ? VIO_PROBE_ONLY : 0);
293
294     return (vid_find_adapter("*", unit) >= 0);
295 }
296
297 /* probe the keyboard, return TRUE if found */
298 static int
299 sckbdprobe(int unit, int flags, int cons)
300 {
301     /* access the keyboard driver through the backdoor! */
302     kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
303
304     return (kbd_find_keyboard("*", unit) >= 0);
305 }
306
307 static char
308 *adapter_name(video_adapter_t *adp)
309 {
310     static struct {
311         int type;
312         char *name[2];
313     } names[] = {
314         { KD_MONO,      { "MDA",        "MDA" } },
315         { KD_HERCULES,  { "Hercules",   "Hercules" } },
316         { KD_CGA,       { "CGA",        "CGA" } },
317         { KD_EGA,       { "EGA",        "EGA (mono)" } },
318         { KD_VGA,       { "VGA",        "VGA (mono)" } },
319         { KD_PC98,      { "PC-98x1",    "PC-98x1" } },
320         { KD_TGA,       { "TGA",        "TGA" } },
321         { -1,           { "Unknown",    "Unknown" } },
322     };
323     int i;
324
325     for (i = 0; names[i].type != -1; ++i)
326         if (names[i].type == adp->va_type)
327             break;
328     return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
329 }
330
331 static void
332 sctty_outwakeup(struct tty *tp)
333 {
334     size_t len;
335     u_char buf[PCBURST];
336     scr_stat *scp = sc_get_stat(tp);
337
338     if (scp->status & SLKED ||
339         (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
340         return;
341
342     for (;;) {
343         len = ttydisc_getc(tp, buf, sizeof buf);
344         if (len == 0)
345             break;
346         sc_puts(scp, buf, len, 0);
347     }
348 }
349
350 static struct tty *
351 sc_alloc_tty(int index, int devnum)
352 {
353         struct sc_ttysoftc *stc;
354         struct tty *tp;
355
356         /* Allocate TTY object and softc to store unit number. */
357         stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
358         stc->st_index = index;
359         stc->st_stat = NULL;
360         tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
361
362         /* Create device node. */
363         tty_makedev(tp, NULL, "v%r", devnum);
364
365         return (tp);
366 }
367
368 #ifdef SC_PIXEL_MODE
369 static void
370 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
371 {
372         video_info_t info;
373         u_char *font;
374         int depth;
375         int fontsize;
376         int i;
377         int vmode;
378
379         vmode = 0;
380         (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
381         if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
382             vidd_get_info(sc->adp, vmode, &info) != 0 ||
383             !sc_support_pixel_mode(&info))
384                 vmode = 0;
385
386         /*
387          * If the mode is unset or unsupported, search for an available
388          * 800x600 graphics mode with the highest color depth.
389          */
390         if (vmode == 0) {
391                 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
392                         if (vidd_get_info(sc->adp, i, &info) == 0 &&
393                             info.vi_width == 800 && info.vi_height == 600 &&
394                             sc_support_pixel_mode(&info) &&
395                             info.vi_depth > depth) {
396                                 vmode = i;
397                                 depth = info.vi_depth;
398                         }
399                 if (vmode == 0)
400                         return;
401                 vidd_get_info(sc->adp, vmode, &info);
402         }
403
404 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
405         fontsize = info.vi_cheight;
406 #else
407         fontsize = scp->font_size;
408 #endif
409         if (fontsize < 14)
410                 fontsize = 8;
411         else if (fontsize >= 16)
412                 fontsize = 16;
413         else
414                 fontsize = 14;
415 #ifndef SC_NO_FONT_LOADING
416         switch (fontsize) {
417         case 8:
418                 if ((sc->fonts_loaded & FONT_8) == 0)
419                         return;
420                 font = sc->font_8;
421                 break;
422         case 14:
423                 if ((sc->fonts_loaded & FONT_14) == 0)
424                         return;
425                 font = sc->font_14;
426                 break;
427         case 16:
428                 if ((sc->fonts_loaded & FONT_16) == 0)
429                         return;
430                 font = sc->font_16;
431                 break;
432         }
433 #else
434         font = NULL;
435 #endif
436 #ifdef DEV_SPLASH
437         if ((sc->flags & SC_SPLASH_SCRN) != 0)
438                 splash_term(sc->adp);
439 #endif
440 #ifndef SC_NO_HISTORY
441         if (scp->history != NULL) {
442                 sc_vtb_append(&scp->vtb, 0, scp->history,
443                     scp->ypos * scp->xsize + scp->xpos);
444                 scp->history_pos = sc_vtb_tail(scp->history);
445         }
446 #endif
447         vidd_set_mode(sc->adp, vmode);
448         scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
449         scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
450         scp->xpixel = info.vi_width;
451         scp->ypixel = info.vi_height;
452         scp->xsize = scp->xpixel / 8;
453         scp->ysize = scp->ypixel / fontsize;
454         scp->xpos = 0;
455         scp->ypos = scp->ysize - 1;
456         scp->xoff = scp->yoff = 0;
457         scp->font = font;
458         scp->font_size = fontsize;
459         scp->font_width = 8;
460         scp->start = scp->xsize * scp->ysize - 1;
461         scp->end = 0;
462         scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
463         scp->mode = sc->initial_mode = vmode;
464 #ifndef __sparc64__
465         sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
466             (void *)sc->adp->va_window, FALSE);
467 #endif
468         sc_alloc_scr_buffer(scp, FALSE, FALSE);
469         sc_init_emulator(scp, NULL);
470 #ifndef SC_NO_CUTPASTE
471         sc_alloc_cut_buffer(scp, FALSE);
472 #endif
473 #ifndef SC_NO_HISTORY
474         sc_alloc_history_buffer(scp, 0, 0, FALSE);
475 #endif
476         sc_set_border(scp, scp->border);
477         sc_set_cursor_image(scp);
478         scp->status &= ~UNKNOWN_MODE;
479 #ifdef DEV_SPLASH
480         if ((sc->flags & SC_SPLASH_SCRN) != 0)
481                 splash_init(sc->adp, scsplash_callback, sc);
482 #endif
483 }
484 #endif
485
486 int
487 sc_attach_unit(int unit, int flags)
488 {
489     sc_softc_t *sc;
490     scr_stat *scp;
491     struct cdev *dev;
492     int vc;
493
494     if (!vty_enabled(VTY_SC))
495         return ENXIO;
496
497     flags &= ~SC_KERNEL_CONSOLE;
498
499     if (sc_console_unit == unit) {
500         /*
501          * If this unit is being used as the system console, we need to
502          * adjust some variables and buffers before and after scinit().
503          */
504         /* assert(sc_console != NULL) */
505         flags |= SC_KERNEL_CONSOLE;
506         scmeminit(NULL);
507     }
508     scinit(unit, flags);
509
510     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
511     sc->config = flags;
512     callout_init(&sc->ctimeout, 0);
513     callout_init(&sc->cblink, 0);
514     scp = sc_get_stat(sc->dev[0]);
515     if (sc_console == NULL)     /* sc_console_unit < 0 */
516         sc_console = scp;
517
518 #ifdef SC_PIXEL_MODE
519     if ((sc->config & SC_VESAMODE) != 0)
520         sc_set_vesa_mode(scp, sc, unit);
521 #endif /* SC_PIXEL_MODE */
522
523     /* initialize cursor */
524     if (!ISGRAPHSC(scp))
525         update_cursor_image(scp);
526
527     /* get screen update going */
528     scrn_timer(sc);
529
530     /* set up the keyboard */
531     (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
532     update_kbd_state(scp, scp->status, LOCK_MASK);
533
534     printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
535            SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
536     if (bootverbose) {
537         printf("%s%d:", SC_DRIVER_NAME, unit);
538         if (sc->adapter >= 0)
539             printf(" fb%d", sc->adapter);
540         if (sc->keyboard >= 0)
541             printf(", kbd%d", sc->keyboard);
542         if (scp->tsw)
543             printf(", terminal emulator: %s (%s)",
544                    scp->tsw->te_name, scp->tsw->te_desc);
545         printf("\n");
546     }
547
548     /* Register suspend/resume/shutdown callbacks for the kernel console. */
549     if (sc_console_unit == unit) {
550         EVENTHANDLER_REGISTER(power_suspend_early, scsuspend, NULL,
551                               EVENTHANDLER_PRI_ANY);
552         EVENTHANDLER_REGISTER(power_resume, scresume, NULL,
553                               EVENTHANDLER_PRI_ANY);
554         EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL,
555                               SHUTDOWN_PRI_DEFAULT);
556     }
557
558     for (vc = 0; vc < sc->vtys; vc++) {
559         if (sc->dev[vc] == NULL) {
560                 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
561                 if (vc == 0 && sc->dev == main_devs)
562                         SC_STAT(sc->dev[0]) = &main_console;
563         }
564         /*
565          * The first vty already has struct tty and scr_stat initialized
566          * in scinit().  The other vtys will have these structs when
567          * first opened.
568          */
569     }
570
571     dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600,
572         "consolectl");
573     dev->si_drv1 = sc->dev[0];
574
575     return 0;
576 }
577
578 static void
579 scmeminit(void *arg)
580 {
581     if (!vty_enabled(VTY_SC))
582         return;
583     if (sc_malloc)
584         return;
585     sc_malloc = TRUE;
586
587     /*
588      * As soon as malloc() becomes functional, we had better allocate
589      * various buffers for the kernel console.
590      */
591
592     if (sc_console_unit < 0)    /* sc_console == NULL */
593         return;
594
595     /* copy the temporary buffer to the final buffer */
596     sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
597
598 #ifndef SC_NO_CUTPASTE
599     sc_alloc_cut_buffer(sc_console, FALSE);
600 #endif
601
602 #ifndef SC_NO_HISTORY
603     /* initialize history buffer & pointers */
604     sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
605 #endif
606 }
607
608 /* XXX */
609 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
610
611 static int
612 scdevtounit(struct tty *tp)
613 {
614     int vty = SC_VTY(tp);
615
616     if (vty == SC_CONSOLECTL)
617         return ((sc_console != NULL) ? sc_console->sc->unit : -1);
618     else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
619         return -1;
620     else
621         return vty/MAXCONS;
622 }
623
624 static int
625 sctty_open(struct tty *tp)
626 {
627     int unit = scdevtounit(tp);
628     sc_softc_t *sc;
629     scr_stat *scp;
630 #ifndef __sparc64__
631     keyarg_t key;
632 #endif
633
634     DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
635                 devtoname(tp->t_dev), unit, SC_VTY(tp)));
636
637     sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
638     if (sc == NULL)
639         return ENXIO;
640
641     if (!tty_opened(tp)) {
642         /* Use the current setting of the <-- key as default VERASE. */  
643         /* If the Delete key is preferable, an stty is necessary     */
644 #ifndef __sparc64__
645         if (sc->kbd != NULL) {
646             key.keynum = KEYCODE_BS;
647             (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
648             tp->t_termios.c_cc[VERASE] = key.key.map[0];
649         }
650 #endif
651     }
652
653     scp = sc_get_stat(tp);
654     if (scp == NULL) {
655         scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
656         if (ISGRAPHSC(scp))
657             sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
658     }
659     if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
660         tp->t_winsize.ws_col = scp->xsize;
661         tp->t_winsize.ws_row = scp->ysize;
662     }
663
664     return (0);
665 }
666
667 static void
668 sctty_close(struct tty *tp)
669 {
670     scr_stat *scp;
671     int s;
672
673     if (SC_VTY(tp) != SC_CONSOLECTL) {
674         scp = sc_get_stat(tp);
675         /* were we in the middle of the VT switching process? */
676         DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
677         s = spltty();
678         if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
679             cnavailable(sc_consptr, TRUE);
680         if (finish_vt_rel(scp, TRUE, &s) == 0)  /* force release */
681             DPRINTF(5, ("reset WAIT_REL, "));
682         if (finish_vt_acq(scp) == 0)            /* force acknowledge */
683             DPRINTF(5, ("reset WAIT_ACQ, "));
684 #ifdef not_yet_done
685         if (scp == &main_console) {
686             scp->pid = 0;
687             scp->proc = NULL;
688             scp->smode.mode = VT_AUTO;
689         }
690         else {
691             sc_vtb_destroy(&scp->vtb);
692 #ifndef __sparc64__
693             sc_vtb_destroy(&scp->scr);
694 #endif
695             sc_free_history_buffer(scp, scp->ysize);
696             SC_STAT(tp) = NULL;
697             free(scp, M_DEVBUF);
698         }
699 #else
700         scp->pid = 0;
701         scp->proc = NULL;
702         scp->smode.mode = VT_AUTO;
703 #endif
704         scp->kbd_mode = K_XLATE;
705         if (scp == scp->sc->cur_scp)
706             (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
707         DPRINTF(5, ("done.\n"));
708     }
709 }
710
711 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
712 static int
713 scread(struct cdev *dev, struct uio *uio, int flag)
714 {
715     if (!sc_saver_keyb_only)
716         sc_touch_scrn_saver();
717     return ttyread(dev, uio, flag);
718 }
719 #endif
720
721 static int
722 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
723 {
724     sc_softc_t *sc;
725     struct tty *cur_tty;
726     int c, error = 0; 
727     size_t len;
728     const u_char *cp;
729
730     sc = (sc_softc_t *)arg;
731     /* assert(thiskbd == sc->kbd) */
732
733     mtx_lock(&Giant);
734
735     switch (event) {
736     case KBDIO_KEYINPUT:
737         break;
738     case KBDIO_UNLOADING:
739         sc->kbd = NULL;
740         sc->keyboard = -1;
741         kbd_release(thiskbd, (void *)&sc->keyboard);
742         goto done;
743     default:
744         error = EINVAL;
745         goto done;
746     }
747
748     /* 
749      * Loop while there is still input to get from the keyboard.
750      * I don't think this is nessesary, and it doesn't fix
751      * the Xaccel-2.1 keyboard hang, but it can't hurt.         XXX
752      */
753     while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
754
755         cur_tty = SC_DEV(sc, sc->cur_scp->index);
756         if (!tty_opened_ns(cur_tty))
757             continue;
758
759         if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
760             continue;
761
762         switch (KEYFLAGS(c)) {
763         case 0x0000: /* normal key */
764             ttydisc_rint(cur_tty, KEYCHAR(c), 0);
765             break;
766         case FKEY:  /* function key, return string */
767             cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
768             if (cp != NULL) {
769                 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
770                 break;
771             }
772             cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
773             if (cp != NULL)
774                 ttydisc_rint_simple(cur_tty, cp, len);
775             break;
776         case MKEY:  /* meta is active, prepend ESC */
777             ttydisc_rint(cur_tty, 0x1b, 0);
778             ttydisc_rint(cur_tty, KEYCHAR(c), 0);
779             break;
780         case BKEY:  /* backtab fixed sequence (esc [ Z) */
781             ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
782             break;
783         }
784
785         ttydisc_rint_done(cur_tty);
786     }
787
788     sc->cur_scp->status |= MOUSE_HIDDEN;
789
790 done:
791     mtx_unlock(&Giant);
792     return (error);
793 }
794
795 static int
796 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
797 {
798     int error;
799     int i;
800     sc_softc_t *sc;
801     scr_stat *scp;
802     int s;
803 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
804     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
805     int ival;
806 #endif
807
808     /* If there is a user_ioctl function call that first */
809     if (sc_user_ioctl) {
810         error = (*sc_user_ioctl)(tp, cmd, data, td);
811         if (error != ENOIOCTL)
812             return error;
813     }
814
815     error = sc_vid_ioctl(tp, cmd, data, td);
816     if (error != ENOIOCTL)
817         return error;
818
819 #ifndef SC_NO_HISTORY
820     error = sc_hist_ioctl(tp, cmd, data, td);
821     if (error != ENOIOCTL)
822         return error;
823 #endif
824
825 #ifndef SC_NO_SYSMOUSE
826     error = sc_mouse_ioctl(tp, cmd, data, td);
827     if (error != ENOIOCTL)
828         return error;
829 #endif
830
831     scp = sc_get_stat(tp);
832     /* assert(scp != NULL) */
833     /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
834     sc = scp->sc;
835
836     if (scp->tsw) {
837         error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
838         if (error != ENOIOCTL)
839             return error;
840     }
841
842     switch (cmd) {              /* process console hardware related ioctl's */
843
844     case GIO_ATTR:              /* get current attributes */
845         /* this ioctl is not processed here, but in the terminal emulator */
846         return ENOTTY;
847
848     case GIO_COLOR:             /* is this a color console ? */
849         *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
850         return 0;
851
852     case CONS_BLANKTIME:        /* set screen saver timeout (0 = no saver) */
853         if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
854             return EINVAL;
855         s = spltty();
856         scrn_blank_time = *(int *)data;
857         run_scrn_saver = (scrn_blank_time != 0);
858         splx(s);
859         return 0;
860
861     case CONS_CURSORTYPE:       /* set cursor type (obsolete) */
862         s = spltty();
863         *(int *)data &= CONS_CURSOR_ATTRS;
864         sc_change_cursor_shape(scp, *(int *)data, -1, -1);
865         splx(s);
866         return 0;
867
868     case CONS_GETCURSORSHAPE:   /* get cursor shape (new interface) */
869         if (((int *)data)[0] & CONS_LOCAL_CURSOR) {
870             ((int *)data)[0] = scp->curr_curs_attr.flags;
871             ((int *)data)[1] = scp->curr_curs_attr.base;
872             ((int *)data)[2] = scp->curr_curs_attr.height;
873         } else {
874             ((int *)data)[0] = sc->curs_attr.flags;
875             ((int *)data)[1] = sc->curs_attr.base;
876             ((int *)data)[2] = sc->curs_attr.height;
877         }
878         return 0;
879
880     case CONS_SETCURSORSHAPE:   /* set cursor shape (new interface) */
881         s = spltty();
882         sc_change_cursor_shape(scp, ((int *)data)[0],
883             ((int *)data)[1], ((int *)data)[2]);
884         splx(s);
885         return 0;
886
887     case CONS_BELLTYPE:         /* set bell type sound/visual */
888         if ((*(int *)data) & CONS_VISUAL_BELL)
889             sc->flags |= SC_VISUAL_BELL;
890         else
891             sc->flags &= ~SC_VISUAL_BELL;
892         if ((*(int *)data) & CONS_QUIET_BELL)
893             sc->flags |= SC_QUIET_BELL;
894         else
895             sc->flags &= ~SC_QUIET_BELL;
896         return 0;
897
898     case CONS_GETINFO:          /* get current (virtual) console info */
899     {
900         vid_info_t *ptr = (vid_info_t*)data;
901         if (ptr->size == sizeof(struct vid_info)) {
902             ptr->m_num = sc->cur_scp->index;
903             ptr->font_size = scp->font_size;
904             ptr->mv_col = scp->xpos;
905             ptr->mv_row = scp->ypos;
906             ptr->mv_csz = scp->xsize;
907             ptr->mv_rsz = scp->ysize;
908             ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
909             /*
910              * The following fields are filled by the terminal emulator. XXX
911              *
912              * ptr->mv_norm.fore
913              * ptr->mv_norm.back
914              * ptr->mv_rev.fore
915              * ptr->mv_rev.back
916              */
917             ptr->mv_grfc.fore = 0;      /* not supported */
918             ptr->mv_grfc.back = 0;      /* not supported */
919             ptr->mv_ovscan = scp->border;
920             if (scp == sc->cur_scp)
921                 save_kbd_state(scp);
922             ptr->mk_keylock = scp->status & LOCK_MASK;
923             return 0;
924         }
925         return EINVAL;
926     }
927
928     case CONS_GETVERS:          /* get version number */
929         *(int*)data = 0x200;    /* version 2.0 */
930         return 0;
931
932     case CONS_IDLE:             /* see if the screen has been idle */
933         /*
934          * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
935          * the user process may have been writing something on the
936          * screen and syscons is not aware of it. Declare the screen
937          * is NOT idle if it is in one of these modes. But there is
938          * an exception to it; if a screen saver is running in the 
939          * graphics mode in the current screen, we should say that the
940          * screen has been idle.
941          */
942         *(int *)data = (sc->flags & SC_SCRN_IDLE)
943                        && (!ISGRAPHSC(sc->cur_scp)
944                            || (sc->cur_scp->status & SAVER_RUNNING));
945         return 0;
946
947     case CONS_SAVERMODE:        /* set saver mode */
948         switch(*(int *)data) {
949         case CONS_NO_SAVER:
950         case CONS_USR_SAVER:
951             /* if a LKM screen saver is running, stop it first. */
952             scsplash_stick(FALSE);
953             saver_mode = *(int *)data;
954             s = spltty();
955 #ifdef DEV_SPLASH
956             if ((error = wait_scrn_saver_stop(NULL))) {
957                 splx(s);
958                 return error;
959             }
960 #endif
961             run_scrn_saver = TRUE;
962             if (saver_mode == CONS_USR_SAVER)
963                 scp->status |= SAVER_RUNNING;
964             else
965                 scp->status &= ~SAVER_RUNNING;
966             scsplash_stick(TRUE);
967             splx(s);
968             break;
969         case CONS_LKM_SAVER:
970             s = spltty();
971             if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
972                 scp->status &= ~SAVER_RUNNING;
973             saver_mode = *(int *)data;
974             splx(s);
975             break;
976         default:
977             return EINVAL;
978         }
979         return 0;
980
981     case CONS_SAVERSTART:       /* immediately start/stop the screen saver */
982         /*
983          * Note that this ioctl does not guarantee the screen saver 
984          * actually starts or stops. It merely attempts to do so...
985          */
986         s = spltty();
987         run_scrn_saver = (*(int *)data != 0);
988         if (run_scrn_saver)
989             sc->scrn_time_stamp -= scrn_blank_time;
990         splx(s);
991         return 0;
992
993     case CONS_SCRSHOT:          /* get a screen shot */
994     {
995         int retval, hist_rsz;
996         size_t lsize, csize;
997         vm_offset_t frbp, hstp;
998         unsigned lnum;
999         scrshot_t *ptr = (scrshot_t *)data;
1000         void *outp = ptr->buf;
1001
1002         if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
1003                 return EINVAL;
1004         s = spltty();
1005         if (ISGRAPHSC(scp)) {
1006             splx(s);
1007             return EOPNOTSUPP;
1008         }
1009         hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1010         if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
1011             ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1012             splx(s);
1013             return EINVAL;
1014         }
1015
1016         lsize = scp->xsize * sizeof(u_int16_t);
1017         csize = ptr->xsize * sizeof(u_int16_t);
1018         /* Pointer to the last line of framebuffer */
1019         frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
1020                sizeof(u_int16_t);
1021         /* Pointer to the last line of target buffer */
1022         outp = (char *)outp + ptr->ysize * csize;
1023         /* Pointer to the last line of history buffer */
1024         if (scp->history != NULL)
1025             hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
1026                 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
1027         else
1028             hstp = 0;
1029
1030         retval = 0;
1031         for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1032             if (lnum < scp->ysize) {
1033                 frbp -= lsize;
1034             } else {
1035                 hstp -= lsize;
1036                 if (hstp < scp->history->vtb_buffer)
1037                     hstp += scp->history->vtb_rows * lsize;
1038                 frbp = hstp;
1039             }
1040             if (lnum < ptr->y)
1041                 continue;
1042             outp = (char *)outp - csize;
1043             retval = copyout((void *)frbp, outp, csize);
1044             if (retval != 0)
1045                 break;
1046         }
1047         splx(s);
1048         return retval;
1049     }
1050
1051     case VT_SETMODE:            /* set screen switcher mode */
1052     {
1053         struct vt_mode *mode;
1054         struct proc *p1;
1055
1056         mode = (struct vt_mode *)data;
1057         DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1058         if (scp->smode.mode == VT_PROCESS) {
1059             p1 = pfind(scp->pid);
1060             if (scp->proc == p1 && scp->proc != td->td_proc) {
1061                 if (p1)
1062                     PROC_UNLOCK(p1);
1063                 DPRINTF(5, ("error EPERM\n"));
1064                 return EPERM;
1065             }
1066             if (p1)
1067                 PROC_UNLOCK(p1);
1068         }
1069         s = spltty();
1070         if (mode->mode == VT_AUTO) {
1071             scp->smode.mode = VT_AUTO;
1072             scp->proc = NULL;
1073             scp->pid = 0;
1074             DPRINTF(5, ("VT_AUTO, "));
1075             if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1076                 cnavailable(sc_consptr, TRUE);
1077             /* were we in the middle of the vty switching process? */
1078             if (finish_vt_rel(scp, TRUE, &s) == 0)
1079                 DPRINTF(5, ("reset WAIT_REL, "));
1080             if (finish_vt_acq(scp) == 0)
1081                 DPRINTF(5, ("reset WAIT_ACQ, "));
1082         } else {
1083             if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1084                 || !ISSIGVALID(mode->frsig)) {
1085                 splx(s);
1086                 DPRINTF(5, ("error EINVAL\n"));
1087                 return EINVAL;
1088             }
1089             DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1090             bcopy(data, &scp->smode, sizeof(struct vt_mode));
1091             scp->proc = td->td_proc;
1092             scp->pid = scp->proc->p_pid;
1093             if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1094                 cnavailable(sc_consptr, FALSE);
1095         }
1096         splx(s);
1097         DPRINTF(5, ("\n"));
1098         return 0;
1099     }
1100
1101     case VT_GETMODE:            /* get screen switcher mode */
1102         bcopy(&scp->smode, data, sizeof(struct vt_mode));
1103         return 0;
1104
1105 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1106     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1107     case _IO('v', 4):
1108         ival = IOCPARM_IVAL(data);
1109         data = (caddr_t)&ival;
1110         /* FALLTHROUGH */
1111 #endif
1112     case VT_RELDISP:            /* screen switcher ioctl */
1113         s = spltty();
1114         /*
1115          * This must be the current vty which is in the VT_PROCESS
1116          * switching mode...
1117          */
1118         if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1119             splx(s);
1120             return EINVAL;
1121         }
1122         /* ...and this process is controlling it. */
1123         if (scp->proc != td->td_proc) {
1124             splx(s);
1125             return EPERM;
1126         }
1127         error = EINVAL;
1128         switch(*(int *)data) {
1129         case VT_FALSE:          /* user refuses to release screen, abort */
1130             if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1131                 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1132             break;
1133         case VT_TRUE:           /* user has released screen, go on */
1134             if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1135                 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1136             break;
1137         case VT_ACKACQ:         /* acquire acknowledged, switch completed */
1138             if ((error = finish_vt_acq(scp)) == 0)
1139                 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1140             break;
1141         default:
1142             break;
1143         }
1144         splx(s);
1145         return error;
1146
1147     case VT_OPENQRY:            /* return free virtual console */
1148         for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1149             tp = SC_DEV(sc, i);
1150             if (!tty_opened_ns(tp)) {
1151                 *(int *)data = i + 1;
1152                 return 0;
1153             }
1154         }
1155         return EINVAL;
1156
1157 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1158     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1159     case _IO('v', 5):
1160         ival = IOCPARM_IVAL(data);
1161         data = (caddr_t)&ival;
1162         /* FALLTHROUGH */
1163 #endif
1164     case VT_ACTIVATE:           /* switch to screen *data */
1165         i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1166         s = spltty();
1167         error = sc_clean_up(sc->cur_scp);
1168         splx(s);
1169         if (error)
1170             return error;
1171         error = sc_switch_scr(sc, i);
1172         return (error);
1173
1174 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1175     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1176     case _IO('v', 6):
1177         ival = IOCPARM_IVAL(data);
1178         data = (caddr_t)&ival;
1179         /* FALLTHROUGH */
1180 #endif
1181     case VT_WAITACTIVE:         /* wait for switch to occur */
1182         i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1183         if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1184             return EINVAL;
1185         if (i == sc->cur_scp->index)
1186             return 0;
1187         error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1188         return error;
1189
1190     case VT_GETACTIVE:          /* get active vty # */
1191         *(int *)data = sc->cur_scp->index + 1;
1192         return 0;
1193
1194     case VT_GETINDEX:           /* get this vty # */
1195         *(int *)data = scp->index + 1;
1196         return 0;
1197
1198     case VT_LOCKSWITCH:         /* prevent vty switching */
1199         if ((*(int *)data) & 0x01)
1200             sc->flags |= SC_SCRN_VTYLOCK;
1201         else
1202             sc->flags &= ~SC_SCRN_VTYLOCK;
1203         return 0;
1204
1205     case KDENABIO:              /* allow io operations */
1206         error = priv_check(td, PRIV_IO);
1207         if (error != 0)
1208             return error;
1209         error = securelevel_gt(td->td_ucred, 0);
1210         if (error != 0)
1211                 return error;
1212 #ifdef __i386__
1213         td->td_frame->tf_eflags |= PSL_IOPL;
1214 #elif defined(__amd64__)
1215         td->td_frame->tf_rflags |= PSL_IOPL;
1216 #endif
1217         return 0;
1218
1219     case KDDISABIO:             /* disallow io operations (default) */
1220 #ifdef __i386__
1221         td->td_frame->tf_eflags &= ~PSL_IOPL;
1222 #elif defined(__amd64__)
1223         td->td_frame->tf_rflags &= ~PSL_IOPL;
1224 #endif
1225         return 0;
1226
1227 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1228     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1229     case _IO('K', 20):
1230         ival = IOCPARM_IVAL(data);
1231         data = (caddr_t)&ival;
1232         /* FALLTHROUGH */
1233 #endif
1234     case KDSKBSTATE:            /* set keyboard state (locks) */
1235         if (*(int *)data & ~LOCK_MASK)
1236             return EINVAL;
1237         scp->status &= ~LOCK_MASK;
1238         scp->status |= *(int *)data;
1239         if (scp == sc->cur_scp)
1240             update_kbd_state(scp, scp->status, LOCK_MASK);
1241         return 0;
1242
1243     case KDGKBSTATE:            /* get keyboard state (locks) */
1244         if (scp == sc->cur_scp)
1245             save_kbd_state(scp);
1246         *(int *)data = scp->status & LOCK_MASK;
1247         return 0;
1248
1249     case KDGETREPEAT:           /* get keyboard repeat & delay rates */
1250     case KDSETREPEAT:           /* set keyboard repeat & delay rates (new) */
1251         error = kbdd_ioctl(sc->kbd, cmd, data);
1252         if (error == ENOIOCTL)
1253             error = ENODEV;
1254         return error;
1255
1256 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1257     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1258     case _IO('K', 67):
1259         ival = IOCPARM_IVAL(data);
1260         data = (caddr_t)&ival;
1261         /* FALLTHROUGH */
1262 #endif
1263     case KDSETRAD:              /* set keyboard repeat & delay rates (old) */
1264         if (*(int *)data & ~0x7f)
1265             return EINVAL;
1266         error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1267         if (error == ENOIOCTL)
1268             error = ENODEV;
1269         return error;
1270
1271 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1272     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1273     case _IO('K', 7):
1274         ival = IOCPARM_IVAL(data);
1275         data = (caddr_t)&ival;
1276         /* FALLTHROUGH */
1277 #endif
1278     case KDSKBMODE:             /* set keyboard mode */
1279         switch (*(int *)data) {
1280         case K_XLATE:           /* switch to XLT ascii mode */
1281         case K_RAW:             /* switch to RAW scancode mode */
1282         case K_CODE:            /* switch to CODE mode */
1283             scp->kbd_mode = *(int *)data;
1284             if (scp == sc->cur_scp)
1285                 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1286             return 0;
1287         default:
1288             return EINVAL;
1289         }
1290         /* NOT REACHED */
1291
1292     case KDGKBMODE:             /* get keyboard mode */
1293         *(int *)data = scp->kbd_mode;
1294         return 0;
1295
1296     case KDGKBINFO:
1297         error = kbdd_ioctl(sc->kbd, cmd, data);
1298         if (error == ENOIOCTL)
1299             error = ENODEV;
1300         return error;
1301
1302 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1303     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1304     case _IO('K', 8):
1305         ival = IOCPARM_IVAL(data);
1306         data = (caddr_t)&ival;
1307         /* FALLTHROUGH */
1308 #endif
1309     case KDMKTONE:              /* sound the bell */
1310         if (*(int*)data)
1311             sc_bell(scp, (*(int*)data)&0xffff,
1312                     (((*(int*)data)>>16)&0xffff)*hz/1000);
1313         else
1314             sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1315         return 0;
1316
1317 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1318     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1319     case _IO('K', 63):
1320         ival = IOCPARM_IVAL(data);
1321         data = (caddr_t)&ival;
1322         /* FALLTHROUGH */
1323 #endif
1324     case KIOCSOUND:             /* make tone (*data) hz */
1325         if (scp == sc->cur_scp) {
1326             if (*(int *)data)
1327                 return sc_tone(*(int *)data);
1328             else
1329                 return sc_tone(0);
1330         }
1331         return 0;
1332
1333     case KDGKBTYPE:             /* get keyboard type */
1334         error = kbdd_ioctl(sc->kbd, cmd, data);
1335         if (error == ENOIOCTL) {
1336             /* always return something? XXX */
1337             *(int *)data = 0;
1338         }
1339         return 0;
1340
1341 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1342     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1343     case _IO('K', 66):
1344         ival = IOCPARM_IVAL(data);
1345         data = (caddr_t)&ival;
1346         /* FALLTHROUGH */
1347 #endif
1348     case KDSETLED:              /* set keyboard LED status */
1349         if (*(int *)data & ~LED_MASK)   /* FIXME: LOCK_MASK? */
1350             return EINVAL;
1351         scp->status &= ~LED_MASK;
1352         scp->status |= *(int *)data;
1353         if (scp == sc->cur_scp)
1354             update_kbd_leds(scp, scp->status);
1355         return 0;
1356
1357     case KDGETLED:              /* get keyboard LED status */
1358         if (scp == sc->cur_scp)
1359             save_kbd_state(scp);
1360         *(int *)data = scp->status & LED_MASK;
1361         return 0;
1362
1363     case KBADDKBD:              /* add/remove keyboard to/from mux */
1364     case KBRELKBD:
1365         error = kbdd_ioctl(sc->kbd, cmd, data);
1366         if (error == ENOIOCTL)
1367             error = ENODEV;
1368         return error;
1369
1370 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1371     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1372     case _IO('c', 110):
1373         ival = IOCPARM_IVAL(data);
1374         data = (caddr_t)&ival;
1375         /* FALLTHROUGH */
1376 #endif
1377     case CONS_SETKBD:           /* set the new keyboard */
1378         {
1379             keyboard_t *newkbd;
1380
1381             s = spltty();
1382             newkbd = kbd_get_keyboard(*(int *)data);
1383             if (newkbd == NULL) {
1384                 splx(s);
1385                 return EINVAL;
1386             }
1387             error = 0;
1388             if (sc->kbd != newkbd) {
1389                 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1390                                  (void *)&sc->keyboard, sckbdevent, sc);
1391                 /* i == newkbd->kb_index */
1392                 if (i >= 0) {
1393                     if (sc->kbd != NULL) {
1394                         save_kbd_state(sc->cur_scp);
1395                         kbd_release(sc->kbd, (void *)&sc->keyboard);
1396                     }
1397                     sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1398                     sc->keyboard = i;
1399                     (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1400                               (caddr_t)&sc->cur_scp->kbd_mode);
1401                     update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1402                                      LOCK_MASK);
1403                 } else {
1404                     error = EPERM;      /* XXX */
1405                 }
1406             }
1407             splx(s);
1408             return error;
1409         }
1410
1411     case CONS_RELKBD:           /* release the current keyboard */
1412         s = spltty();
1413         error = 0;
1414         if (sc->kbd != NULL) {
1415             save_kbd_state(sc->cur_scp);
1416             error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1417             if (error == 0) {
1418                 sc->kbd = NULL;
1419                 sc->keyboard = -1;
1420             }
1421         }
1422         splx(s);
1423         return error;
1424
1425     case CONS_GETTERM:          /* get the current terminal emulator info */
1426         {
1427             sc_term_sw_t *sw;
1428
1429             if (((term_info_t *)data)->ti_index == 0) {
1430                 sw = scp->tsw;
1431             } else {
1432                 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1433             }
1434             if (sw != NULL) {
1435                 strncpy(((term_info_t *)data)->ti_name, sw->te_name, 
1436                         sizeof(((term_info_t *)data)->ti_name));
1437                 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc, 
1438                         sizeof(((term_info_t *)data)->ti_desc));
1439                 ((term_info_t *)data)->ti_flags = 0;
1440                 return 0;
1441             } else {
1442                 ((term_info_t *)data)->ti_name[0] = '\0';
1443                 ((term_info_t *)data)->ti_desc[0] = '\0';
1444                 ((term_info_t *)data)->ti_flags = 0;
1445                 return EINVAL;
1446             }
1447         }
1448
1449     case CONS_SETTERM:          /* set the current terminal emulator */
1450         s = spltty();
1451         error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1452         /* FIXME: what if scp == sc_console! XXX */
1453         splx(s);
1454         return error;
1455
1456     case GIO_SCRNMAP:           /* get output translation table */
1457         bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1458         return 0;
1459
1460     case PIO_SCRNMAP:           /* set output translation table */
1461         bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1462         for (i=0; i<sizeof(sc->scr_map); i++) {
1463             sc->scr_rmap[sc->scr_map[i]] = i;
1464         }
1465         return 0;
1466
1467     case GIO_KEYMAP:            /* get keyboard translation table */
1468     case PIO_KEYMAP:            /* set keyboard translation table */
1469     case OGIO_KEYMAP:           /* get keyboard translation table (compat) */
1470     case OPIO_KEYMAP:           /* set keyboard translation table (compat) */
1471     case GIO_DEADKEYMAP:        /* get accent key translation table */
1472     case PIO_DEADKEYMAP:        /* set accent key translation table */
1473     case GETFKEY:               /* get function key string */
1474     case SETFKEY:               /* set function key string */
1475         error = kbdd_ioctl(sc->kbd, cmd, data);
1476         if (error == ENOIOCTL)
1477             error = ENODEV;
1478         return error;
1479
1480 #ifndef SC_NO_FONT_LOADING
1481
1482     case PIO_FONT8x8:           /* set 8x8 dot font */
1483         if (!ISFONTAVAIL(sc->adp->va_flags))
1484             return ENXIO;
1485         bcopy(data, sc->font_8, 8*256);
1486         sc->fonts_loaded |= FONT_8;
1487         /*
1488          * FONT KLUDGE
1489          * Always use the font page #0. XXX
1490          * Don't load if the current font size is not 8x8.
1491          */
1492         if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1493             sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1494         return 0;
1495
1496     case GIO_FONT8x8:           /* get 8x8 dot font */
1497         if (!ISFONTAVAIL(sc->adp->va_flags))
1498             return ENXIO;
1499         if (sc->fonts_loaded & FONT_8) {
1500             bcopy(sc->font_8, data, 8*256);
1501             return 0;
1502         }
1503         else
1504             return ENXIO;
1505
1506     case PIO_FONT8x14:          /* set 8x14 dot font */
1507         if (!ISFONTAVAIL(sc->adp->va_flags))
1508             return ENXIO;
1509         bcopy(data, sc->font_14, 14*256);
1510         sc->fonts_loaded |= FONT_14;
1511         /*
1512          * FONT KLUDGE
1513          * Always use the font page #0. XXX
1514          * Don't load if the current font size is not 8x14.
1515          */
1516         if (ISTEXTSC(sc->cur_scp)
1517             && (sc->cur_scp->font_size >= 14)
1518             && (sc->cur_scp->font_size < 16))
1519             sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1520         return 0;
1521
1522     case GIO_FONT8x14:          /* get 8x14 dot font */
1523         if (!ISFONTAVAIL(sc->adp->va_flags))
1524             return ENXIO;
1525         if (sc->fonts_loaded & FONT_14) {
1526             bcopy(sc->font_14, data, 14*256);
1527             return 0;
1528         }
1529         else
1530             return ENXIO;
1531
1532     case PIO_FONT8x16:          /* set 8x16 dot font */
1533         if (!ISFONTAVAIL(sc->adp->va_flags))
1534             return ENXIO;
1535         bcopy(data, sc->font_16, 16*256);
1536         sc->fonts_loaded |= FONT_16;
1537         /*
1538          * FONT KLUDGE
1539          * Always use the font page #0. XXX
1540          * Don't load if the current font size is not 8x16.
1541          */
1542         if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1543             sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1544         return 0;
1545
1546     case GIO_FONT8x16:          /* get 8x16 dot font */
1547         if (!ISFONTAVAIL(sc->adp->va_flags))
1548             return ENXIO;
1549         if (sc->fonts_loaded & FONT_16) {
1550             bcopy(sc->font_16, data, 16*256);
1551             return 0;
1552         }
1553         else
1554             return ENXIO;
1555
1556 #endif /* SC_NO_FONT_LOADING */
1557
1558     default:
1559         break;
1560     }
1561
1562     return (ENOIOCTL);
1563 }
1564
1565 static int
1566 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1567     struct thread *td)
1568 {
1569
1570         return sctty_ioctl(dev->si_drv1, cmd, data, td);
1571 }
1572
1573 static int
1574 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td)
1575 {
1576 #ifndef SC_NO_SYSMOUSE
1577         mouse_info_t info;
1578         memset(&info, 0, sizeof(info));
1579         info.operation = MOUSE_ACTION;
1580
1581         /*
1582          * Make sure all buttons are released when moused and other
1583          * console daemons exit, so that no buttons are left pressed.
1584          */
1585         (void) sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td);
1586 #endif
1587         return (0);
1588 }
1589
1590 static void
1591 sc_cnprobe(struct consdev *cp)
1592 {
1593     int unit;
1594     int flags;
1595
1596     if (!vty_enabled(VTY_SC)) {
1597         cp->cn_pri = CN_DEAD;
1598         return;
1599     }
1600
1601     cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1602
1603     /* a video card is always required */
1604     if (!scvidprobe(unit, flags, TRUE))
1605         cp->cn_pri = CN_DEAD;
1606
1607     /* syscons will become console even when there is no keyboard */
1608     sckbdprobe(unit, flags, TRUE);
1609
1610     if (cp->cn_pri == CN_DEAD)
1611         return;
1612
1613     /* initialize required fields */
1614     strcpy(cp->cn_name, "ttyv0");
1615 }
1616
1617 static void
1618 sc_cninit(struct consdev *cp)
1619 {
1620     int unit;
1621     int flags;
1622
1623     sc_get_cons_priority(&unit, &flags);
1624     scinit(unit, flags | SC_KERNEL_CONSOLE);
1625     sc_console_unit = unit;
1626     sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1627     sc_consptr = cp;
1628 }
1629
1630 static void
1631 sc_cnterm(struct consdev *cp)
1632 {
1633     /* we are not the kernel console any more, release everything */
1634
1635     if (sc_console_unit < 0)
1636         return;                 /* shouldn't happen */
1637
1638 #if 0 /* XXX */
1639     sc_clear_screen(sc_console);
1640     sccnupdate(sc_console);
1641 #endif
1642
1643     scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1644     sc_console_unit = -1;
1645     sc_console = NULL;
1646 }
1647
1648 static void
1649 sc_cngrab(struct consdev *cp)
1650 {
1651     scr_stat *scp;
1652
1653     if (!cold &&
1654         sc_console->sc->cur_scp->index != sc_console->index &&
1655         sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
1656         sc_console->smode.mode == VT_AUTO)
1657             sc_switch_scr(sc_console->sc, sc_console->index);
1658
1659     scp = sc_console->sc->cur_scp;
1660
1661     if (scp->sc->kbd == NULL)
1662         return;
1663
1664     if (scp->grabbed++ > 0)
1665         return;
1666
1667     /*
1668      * Make sure the keyboard is accessible even when the kbd device
1669      * driver is disabled.
1670      */
1671     kbdd_enable(scp->sc->kbd);
1672
1673     /* we shall always use the keyboard in the XLATE mode here */
1674     scp->kbd_prev_mode = scp->kbd_mode;
1675     scp->kbd_mode = K_XLATE;
1676     (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1677
1678     kbdd_poll(scp->sc->kbd, TRUE);
1679 }
1680
1681 static void
1682 sc_cnungrab(struct consdev *cp)
1683 {
1684     scr_stat *scp;
1685
1686     scp = sc_console->sc->cur_scp;      /* XXX */
1687     if (scp->sc->kbd == NULL)
1688         return;
1689
1690     if (--scp->grabbed > 0)
1691         return;
1692
1693     kbdd_poll(scp->sc->kbd, FALSE);
1694
1695     scp->kbd_mode = scp->kbd_prev_mode;
1696     (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1697     kbdd_disable(scp->sc->kbd);
1698 }
1699
1700 static void
1701 sc_cnputc(struct consdev *cd, int c)
1702 {
1703     u_char buf[1];
1704     scr_stat *scp = sc_console;
1705 #ifndef SC_NO_HISTORY
1706 #if 0
1707     struct tty *tp;
1708 #endif
1709 #endif /* !SC_NO_HISTORY */
1710     int s;
1711
1712     /* assert(sc_console != NULL) */
1713
1714 #ifndef SC_NO_HISTORY
1715     if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1716         scp->status &= ~SLKED;
1717         update_kbd_state(scp, scp->status, SLKED);
1718         if (scp->status & BUFFER_SAVED) {
1719             if (!sc_hist_restore(scp))
1720                 sc_remove_cutmarking(scp);
1721             scp->status &= ~BUFFER_SAVED;
1722             scp->status |= CURSOR_ENABLED;
1723             sc_draw_cursor_image(scp);
1724         }
1725 #if 0
1726         /*
1727          * XXX: Now that TTY's have their own locks, we cannot process
1728          * any data after disabling scroll lock. cnputs already holds a
1729          * spinlock.
1730          */
1731         tp = SC_DEV(scp->sc, scp->index);
1732         /* XXX "tp" can be NULL */
1733         tty_lock(tp);
1734         if (tty_opened(tp))
1735             sctty_outwakeup(tp);
1736         tty_unlock(tp);
1737 #endif
1738     }
1739 #endif /* !SC_NO_HISTORY */
1740
1741     buf[0] = c;
1742     sc_puts(scp, buf, 1, 1);
1743
1744     s = spltty();       /* block sckbdevent and scrn_timer */
1745     sccnupdate(scp);
1746     splx(s);
1747 }
1748
1749 static int
1750 sc_cngetc(struct consdev *cd)
1751 {
1752     static struct fkeytab fkey;
1753     static int fkeycp;
1754     scr_stat *scp;
1755     const u_char *p;
1756     int s = spltty();   /* block sckbdevent and scrn_timer while we poll */
1757     int c;
1758
1759     /* assert(sc_console != NULL) */
1760
1761     /* 
1762      * Stop the screen saver and update the screen if necessary.
1763      * What if we have been running in the screen saver code... XXX
1764      */
1765     sc_touch_scrn_saver();
1766     scp = sc_console->sc->cur_scp;      /* XXX */
1767     sccnupdate(scp);
1768
1769     if (fkeycp < fkey.len) {
1770         splx(s);
1771         return fkey.str[fkeycp++];
1772     }
1773
1774     if (scp->sc->kbd == NULL) {
1775         splx(s);
1776         return -1;
1777     }
1778
1779     c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK);
1780
1781     switch (KEYFLAGS(c)) {
1782     case 0:     /* normal char */
1783         return KEYCHAR(c);
1784     case FKEY:  /* function key */
1785         p = (*scp->tsw->te_fkeystr)(scp, c);
1786         if (p != NULL) {
1787             fkey.len = strlen(p);
1788             bcopy(p, fkey.str, fkey.len);
1789             fkeycp = 1;
1790             return fkey.str[0];
1791         }
1792         p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1793         fkey.len = fkeycp;
1794         if ((p != NULL) && (fkey.len > 0)) {
1795             bcopy(p, fkey.str, fkey.len);
1796             fkeycp = 1;
1797             return fkey.str[0];
1798         }
1799         return c;       /* XXX */
1800     case NOKEY:
1801     case ERRKEY:
1802     default:
1803         return -1;
1804     }
1805     /* NOT REACHED */
1806 }
1807
1808 static void
1809 sccnupdate(scr_stat *scp)
1810 {
1811     /* this is a cut-down version of scrn_timer()... */
1812
1813     if (suspend_in_progress || scp->sc->font_loading_in_progress)
1814         return;
1815
1816     if (kdb_active || panicstr || shutdown_in_progress) {
1817         sc_touch_scrn_saver();
1818     } else if (scp != scp->sc->cur_scp) {
1819         return;
1820     }
1821
1822     if (!run_scrn_saver)
1823         scp->sc->flags &= ~SC_SCRN_IDLE;
1824 #ifdef DEV_SPLASH
1825     if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1826         if (scp->sc->flags & SC_SCRN_BLANKED)
1827             stop_scrn_saver(scp->sc, current_saver);
1828 #endif
1829
1830     if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1831         || scp->sc->switch_in_progress)
1832         return;
1833     /*
1834      * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1835      * when write_in_progress is non-zero.  XXX
1836      */
1837
1838     if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1839         scrn_update(scp, TRUE);
1840 }
1841
1842 static void
1843 scrn_timer(void *arg)
1844 {
1845 #ifndef PC98
1846     static time_t kbd_time_stamp = 0;
1847 #endif
1848     sc_softc_t *sc;
1849     scr_stat *scp;
1850     int again, rate;
1851
1852     again = (arg != NULL);
1853     if (arg != NULL)
1854         sc = (sc_softc_t *)arg;
1855     else if (sc_console != NULL)
1856         sc = sc_console->sc;
1857     else
1858         return;
1859
1860     /* find the vty to update */
1861     scp = sc->cur_scp;
1862
1863     /* don't do anything when we are performing some I/O operations */
1864     if (suspend_in_progress || sc->font_loading_in_progress)
1865         goto done;
1866
1867 #ifndef PC98
1868     if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1869         /* try to allocate a keyboard automatically */
1870         if (kbd_time_stamp != time_uptime) {
1871             kbd_time_stamp = time_uptime;
1872             sc->keyboard = sc_allocate_keyboard(sc, -1);
1873             if (sc->keyboard >= 0) {
1874                 sc->kbd = kbd_get_keyboard(sc->keyboard);
1875                 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1876                           (caddr_t)&sc->cur_scp->kbd_mode);
1877                 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1878                                  LOCK_MASK);
1879             }
1880         }
1881     }
1882 #endif /* PC98 */
1883
1884     /* should we stop the screen saver? */
1885     if (kdb_active || panicstr || shutdown_in_progress)
1886         sc_touch_scrn_saver();
1887     if (run_scrn_saver) {
1888         if (time_uptime > sc->scrn_time_stamp + scrn_blank_time)
1889             sc->flags |= SC_SCRN_IDLE;
1890         else
1891             sc->flags &= ~SC_SCRN_IDLE;
1892     } else {
1893         sc->scrn_time_stamp = time_uptime;
1894         sc->flags &= ~SC_SCRN_IDLE;
1895         if (scrn_blank_time > 0)
1896             run_scrn_saver = TRUE;
1897     }
1898 #ifdef DEV_SPLASH
1899     if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1900         if (sc->flags & SC_SCRN_BLANKED)
1901             stop_scrn_saver(sc, current_saver);
1902 #endif
1903
1904     /* should we just return ? */
1905     if (sc->blink_in_progress || sc->switch_in_progress
1906         || sc->write_in_progress)
1907         goto done;
1908
1909     /* Update the screen */
1910     scp = sc->cur_scp;          /* cur_scp may have changed... */
1911     if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1912         scrn_update(scp, TRUE);
1913
1914 #ifdef DEV_SPLASH
1915     /* should we activate the screen saver? */
1916     if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1917         if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1918             (*current_saver)(sc, TRUE);
1919 #endif
1920
1921 done:
1922     if (again) {
1923         /*
1924          * Use reduced "refresh" rate if we are in graphics and that is not a
1925          * graphical screen saver.  In such case we just have nothing to do.
1926          */
1927         if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1928             rate = 2;
1929         else
1930             rate = 30;
1931         callout_reset_sbt(&sc->ctimeout, SBT_1S / rate, 0,
1932             scrn_timer, sc, C_PREL(1));
1933     }
1934 }
1935
1936 static int
1937 and_region(int *s1, int *e1, int s2, int e2)
1938 {
1939     if (*e1 < s2 || e2 < *s1)
1940         return FALSE;
1941     *s1 = imax(*s1, s2);
1942     *e1 = imin(*e1, e2);
1943     return TRUE;
1944 }
1945
1946 static void 
1947 scrn_update(scr_stat *scp, int show_cursor)
1948 {
1949     int start;
1950     int end;
1951     int s;
1952     int e;
1953
1954     /* assert(scp == scp->sc->cur_scp) */
1955
1956     SC_VIDEO_LOCK(scp->sc);
1957
1958 #ifndef SC_NO_CUTPASTE
1959     /* remove the previous mouse pointer image if necessary */
1960     if (scp->status & MOUSE_VISIBLE) {
1961         s = scp->mouse_pos;
1962         e = scp->mouse_pos + scp->xsize + 1;
1963         if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1964             || and_region(&s, &e, scp->start, scp->end)
1965             || ((scp->status & CURSOR_ENABLED) && 
1966                 (scp->cursor_pos != scp->cursor_oldpos) &&
1967                 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1968                  || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1969             sc_remove_mouse_image(scp);
1970             if (scp->end >= scp->xsize*scp->ysize)
1971                 scp->end = scp->xsize*scp->ysize - 1;
1972         }
1973     }
1974 #endif /* !SC_NO_CUTPASTE */
1975
1976 #if 1
1977     /* debug: XXX */
1978     if (scp->end >= scp->xsize*scp->ysize) {
1979         printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1980         scp->end = scp->xsize*scp->ysize - 1;
1981     }
1982     if (scp->start < 0) {
1983         printf("scrn_update(): scp->start %d < 0\n", scp->start);
1984         scp->start = 0;
1985     }
1986 #endif
1987
1988     /* update screen image */
1989     if (scp->start <= scp->end)  {
1990         if (scp->mouse_cut_end >= 0) {
1991             /* there is a marked region for cut & paste */
1992             if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1993                 start = scp->mouse_cut_start;
1994                 end = scp->mouse_cut_end;
1995             } else {
1996                 start = scp->mouse_cut_end;
1997                 end = scp->mouse_cut_start - 1;
1998             }
1999             s = start;
2000             e = end;
2001             /* does the cut-mark region overlap with the update region? */
2002             if (and_region(&s, &e, scp->start, scp->end)) {
2003                 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2004                 s = 0;
2005                 e = start - 1;
2006                 if (and_region(&s, &e, scp->start, scp->end))
2007                     (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2008                 s = end + 1;
2009                 e = scp->xsize*scp->ysize - 1;
2010                 if (and_region(&s, &e, scp->start, scp->end))
2011                     (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2012             } else {
2013                 (*scp->rndr->draw)(scp, scp->start,
2014                                    scp->end - scp->start + 1, FALSE);
2015             }
2016         } else {
2017             (*scp->rndr->draw)(scp, scp->start,
2018                                scp->end - scp->start + 1, FALSE);
2019         }
2020     }
2021
2022     /* we are not to show the cursor and the mouse pointer... */
2023     if (!show_cursor) {
2024         scp->end = 0;
2025         scp->start = scp->xsize*scp->ysize - 1;
2026         SC_VIDEO_UNLOCK(scp->sc);
2027         return;
2028     }
2029
2030     /* update cursor image */
2031     if (scp->status & CURSOR_ENABLED) {
2032         s = scp->start;
2033         e = scp->end;
2034         /* did cursor move since last time ? */
2035         if (scp->cursor_pos != scp->cursor_oldpos) {
2036             /* do we need to remove old cursor image ? */
2037             if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2038                 sc_remove_cursor_image(scp);
2039             sc_draw_cursor_image(scp);
2040         } else {
2041             if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
2042                 /* cursor didn't move, but has been overwritten */
2043                 sc_draw_cursor_image(scp);
2044             else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
2045                 /* if it's a blinking cursor, update it */
2046                 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2047                                            sc_inside_cutmark(scp,
2048                                                scp->cursor_pos));
2049         }
2050     }
2051
2052 #ifndef SC_NO_CUTPASTE
2053     /* update "pseudo" mouse pointer image */
2054     if (scp->sc->flags & SC_MOUSE_ENABLED) {
2055         if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2056             scp->status &= ~MOUSE_MOVED;
2057             sc_draw_mouse_image(scp);
2058         }
2059     }
2060 #endif /* SC_NO_CUTPASTE */
2061
2062     scp->end = 0;
2063     scp->start = scp->xsize*scp->ysize - 1;
2064
2065     SC_VIDEO_UNLOCK(scp->sc);
2066 }
2067
2068 #ifdef DEV_SPLASH
2069 static int
2070 scsplash_callback(int event, void *arg)
2071 {
2072     sc_softc_t *sc;
2073     int error;
2074
2075     sc = (sc_softc_t *)arg;
2076
2077     switch (event) {
2078     case SPLASH_INIT:
2079         if (add_scrn_saver(scsplash_saver) == 0) {
2080             sc->flags &= ~SC_SAVER_FAILED;
2081             run_scrn_saver = TRUE;
2082             if (cold && !(boothowto & RB_VERBOSE)) {
2083                 scsplash_stick(TRUE);
2084                 (*current_saver)(sc, TRUE);
2085             }
2086         }
2087         return 0;
2088
2089     case SPLASH_TERM:
2090         if (current_saver == scsplash_saver) {
2091             scsplash_stick(FALSE);
2092             error = remove_scrn_saver(scsplash_saver);
2093             if (error)
2094                 return error;
2095         }
2096         return 0;
2097
2098     default:
2099         return EINVAL;
2100     }
2101 }
2102
2103 static void
2104 scsplash_saver(sc_softc_t *sc, int show)
2105 {
2106     static int busy = FALSE;
2107     scr_stat *scp;
2108
2109     if (busy)
2110         return;
2111     busy = TRUE;
2112
2113     scp = sc->cur_scp;
2114     if (show) {
2115         if (!(sc->flags & SC_SAVER_FAILED)) {
2116             if (!(sc->flags & SC_SCRN_BLANKED))
2117                 set_scrn_saver_mode(scp, -1, NULL, 0);
2118             switch (splash(sc->adp, TRUE)) {
2119             case 0:             /* succeeded */
2120                 break;
2121             case EAGAIN:        /* try later */
2122                 restore_scrn_saver_mode(scp, FALSE);
2123                 sc_touch_scrn_saver();          /* XXX */
2124                 break;
2125             default:
2126                 sc->flags |= SC_SAVER_FAILED;
2127                 scsplash_stick(FALSE);
2128                 restore_scrn_saver_mode(scp, TRUE);
2129                 printf("scsplash_saver(): failed to put up the image\n");
2130                 break;
2131             }
2132         }
2133     } else if (!sticky_splash) {
2134         if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2135             restore_scrn_saver_mode(scp, TRUE);
2136     }
2137     busy = FALSE;
2138 }
2139
2140 static int
2141 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2142 {
2143 #if 0
2144     int error;
2145
2146     if (current_saver != none_saver) {
2147         error = remove_scrn_saver(current_saver);
2148         if (error)
2149             return error;
2150     }
2151 #endif
2152     if (current_saver != none_saver)
2153         return EBUSY;
2154
2155     run_scrn_saver = FALSE;
2156     saver_mode = CONS_LKM_SAVER;
2157     current_saver = this_saver;
2158     return 0;
2159 }
2160
2161 static int
2162 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2163 {
2164     if (current_saver != this_saver)
2165         return EINVAL;
2166
2167 #if 0
2168     /*
2169      * In order to prevent `current_saver' from being called by
2170      * the timeout routine `scrn_timer()' while we manipulate 
2171      * the saver list, we shall set `current_saver' to `none_saver' 
2172      * before stopping the current saver, rather than blocking by `splXX()'.
2173      */
2174     current_saver = none_saver;
2175     if (scrn_blanked)
2176         stop_scrn_saver(this_saver);
2177 #endif
2178
2179     /* unblank all blanked screens */
2180     wait_scrn_saver_stop(NULL);
2181     if (scrn_blanked)
2182         return EBUSY;
2183
2184     current_saver = none_saver;
2185     return 0;
2186 }
2187
2188 static int
2189 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2190 {
2191     int s;
2192
2193     /* assert(scp == scp->sc->cur_scp) */
2194     s = spltty();
2195     if (!ISGRAPHSC(scp))
2196         sc_remove_cursor_image(scp);
2197     scp->splash_save_mode = scp->mode;
2198     scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2199     scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2200     scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2201     scp->sc->flags |= SC_SCRN_BLANKED;
2202     ++scrn_blanked;
2203     splx(s);
2204     if (mode < 0)
2205         return 0;
2206     scp->mode = mode;
2207     if (set_mode(scp) == 0) {
2208         if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2209             scp->status |= GRAPHICS_MODE;
2210 #ifndef SC_NO_PALETTE_LOADING
2211         if (pal != NULL)
2212             vidd_load_palette(scp->sc->adp, pal);
2213 #endif
2214         sc_set_border(scp, border);
2215         return 0;
2216     } else {
2217         s = spltty();
2218         scp->mode = scp->splash_save_mode;
2219         scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2220         scp->status |= scp->splash_save_status;
2221         splx(s);
2222         return 1;
2223     }
2224 }
2225
2226 static int
2227 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2228 {
2229     int mode;
2230     int status;
2231     int s;
2232
2233     /* assert(scp == scp->sc->cur_scp) */
2234     s = spltty();
2235     mode = scp->mode;
2236     status = scp->status;
2237     scp->mode = scp->splash_save_mode;
2238     scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2239     scp->status |= scp->splash_save_status;
2240     scp->sc->flags &= ~SC_SCRN_BLANKED;
2241     if (!changemode) {
2242         if (!ISGRAPHSC(scp))
2243             sc_draw_cursor_image(scp);
2244         --scrn_blanked;
2245         splx(s);
2246         return 0;
2247     }
2248     if (set_mode(scp) == 0) {
2249 #ifndef SC_NO_PALETTE_LOADING
2250 #ifdef SC_PIXEL_MODE
2251         if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2252             vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2253         else
2254 #endif
2255         vidd_load_palette(scp->sc->adp, scp->sc->palette);
2256 #endif
2257         --scrn_blanked;
2258         splx(s);
2259         return 0;
2260     } else {
2261         scp->mode = mode;
2262         scp->status = status;
2263         splx(s);
2264         return 1;
2265     }
2266 }
2267
2268 static void
2269 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2270 {
2271     (*saver)(sc, FALSE);
2272     run_scrn_saver = FALSE;
2273     /* the screen saver may have chosen not to stop after all... */
2274     if (sc->flags & SC_SCRN_BLANKED)
2275         return;
2276
2277     mark_all(sc->cur_scp);
2278     if (sc->delayed_next_scr)
2279         sc_switch_scr(sc, sc->delayed_next_scr - 1);
2280     if (!kdb_active)
2281         wakeup(&scrn_blanked);
2282 }
2283
2284 static int
2285 wait_scrn_saver_stop(sc_softc_t *sc)
2286 {
2287     int error = 0;
2288
2289     while (scrn_blanked > 0) {
2290         run_scrn_saver = FALSE;
2291         if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2292             error = 0;
2293             break;
2294         }
2295         error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2296         if ((error != 0) && (error != ERESTART))
2297             break;
2298     }
2299     run_scrn_saver = FALSE;
2300     return error;
2301 }
2302 #endif /* DEV_SPLASH */
2303
2304 void
2305 sc_touch_scrn_saver(void)
2306 {
2307     scsplash_stick(FALSE);
2308     run_scrn_saver = FALSE;
2309 }
2310
2311 int
2312 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2313 {
2314     scr_stat *cur_scp;
2315     struct tty *tp;
2316     struct proc *p;
2317     int s;
2318
2319     DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2320
2321     if (sc->cur_scp == NULL)
2322         return (0);
2323
2324     /* prevent switch if previously requested */
2325     if (sc->flags & SC_SCRN_VTYLOCK) {
2326             sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2327                 sc->cur_scp->bell_duration);
2328             return EPERM;
2329     }
2330
2331     /* delay switch if the screen is blanked or being updated */
2332     if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2333         || sc->blink_in_progress) {
2334         sc->delayed_next_scr = next_scr + 1;
2335         sc_touch_scrn_saver();
2336         DPRINTF(5, ("switch delayed\n"));
2337         return 0;
2338     }
2339     sc->delayed_next_scr = 0;
2340
2341     s = spltty();
2342     cur_scp = sc->cur_scp;
2343
2344     /* we are in the middle of the vty switching process... */
2345     if (sc->switch_in_progress
2346         && (cur_scp->smode.mode == VT_PROCESS)
2347         && cur_scp->proc) {
2348         p = pfind(cur_scp->pid);
2349         if (cur_scp->proc != p) {
2350             if (p)
2351                 PROC_UNLOCK(p);
2352             /* 
2353              * The controlling process has died!!.  Do some clean up.
2354              * NOTE:`cur_scp->proc' and `cur_scp->smode.mode' 
2355              * are not reset here yet; they will be cleared later.
2356              */
2357             DPRINTF(5, ("cur_scp controlling process %d died, ",
2358                cur_scp->pid));
2359             if (cur_scp->status & SWITCH_WAIT_REL) {
2360                 /*
2361                  * Force the previous switch to finish, but return now 
2362                  * with error.
2363                  */
2364                 DPRINTF(5, ("reset WAIT_REL, "));
2365                 finish_vt_rel(cur_scp, TRUE, &s);
2366                 splx(s);
2367                 DPRINTF(5, ("finishing previous switch\n"));
2368                 return EINVAL;
2369             } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2370                 /* let's assume screen switch has been completed. */
2371                 DPRINTF(5, ("reset WAIT_ACQ, "));
2372                 finish_vt_acq(cur_scp);
2373             } else {
2374                 /* 
2375                  * We are in between screen release and acquisition, and
2376                  * reached here via scgetc() or scrn_timer() which has 
2377                  * interrupted exchange_scr(). Don't do anything stupid.
2378                  */
2379                 DPRINTF(5, ("waiting nothing, "));
2380             }
2381         } else {
2382             if (p)
2383                 PROC_UNLOCK(p);
2384             /*
2385              * The controlling process is alive, but not responding... 
2386              * It is either buggy or it may be just taking time.
2387              * The following code is a gross kludge to cope with this
2388              * problem for which there is no clean solution. XXX
2389              */
2390             if (cur_scp->status & SWITCH_WAIT_REL) {
2391                 switch (sc->switch_in_progress++) {
2392                 case 1:
2393                     break;
2394                 case 2:
2395                     DPRINTF(5, ("sending relsig again, "));
2396                     signal_vt_rel(cur_scp);
2397                     break;
2398                 case 3:
2399                     break;
2400                 case 4:
2401                 default:
2402                     /*
2403                      * Act as if the controlling program returned
2404                      * VT_FALSE.
2405                      */
2406                     DPRINTF(5, ("force reset WAIT_REL, "));
2407                     finish_vt_rel(cur_scp, FALSE, &s);
2408                     splx(s);
2409                     DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2410                     return EINVAL;
2411                 }
2412             } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2413                 switch (sc->switch_in_progress++) {
2414                 case 1:
2415                     break;
2416                 case 2:
2417                     DPRINTF(5, ("sending acqsig again, "));
2418                     signal_vt_acq(cur_scp);
2419                     break;
2420                 case 3:
2421                     break;
2422                 case 4:
2423                 default:
2424                      /* clear the flag and finish the previous switch */
2425                     DPRINTF(5, ("force reset WAIT_ACQ, "));
2426                     finish_vt_acq(cur_scp);
2427                     break;
2428                 }
2429             }
2430         }
2431     }
2432
2433     /*
2434      * Return error if an invalid argument is given, or vty switch
2435      * is still in progress.
2436      */
2437     if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2438         || sc->switch_in_progress) {
2439         splx(s);
2440         sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2441         DPRINTF(5, ("error 1\n"));
2442         return EINVAL;
2443     }
2444
2445     /*
2446      * Don't allow switching away from the graphics mode vty
2447      * if the switch mode is VT_AUTO, unless the next vty is the same 
2448      * as the current or the current vty has been closed (but showing).
2449      */
2450     tp = SC_DEV(sc, cur_scp->index);
2451     if ((cur_scp->index != next_scr)
2452         && tty_opened_ns(tp)
2453         && (cur_scp->smode.mode == VT_AUTO)
2454         && ISGRAPHSC(cur_scp)) {
2455         splx(s);
2456         sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2457         DPRINTF(5, ("error, graphics mode\n"));
2458         return EINVAL;
2459     }
2460
2461     /*
2462      * Is the wanted vty open? Don't allow switching to a closed vty.
2463      * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2464      * Note that we always allow the user to switch to the kernel 
2465      * console even if it is closed.
2466      */
2467     if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2468         tp = SC_DEV(sc, next_scr);
2469         if (!tty_opened_ns(tp)) {
2470             splx(s);
2471             sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2472             DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2473             return EINVAL;
2474         }
2475         if (kdb_active && SC_STAT(tp)->smode.mode == VT_PROCESS) {
2476             splx(s);
2477             DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2478             return EINVAL;
2479         }
2480     }
2481
2482     /* this is the start of vty switching process... */
2483     ++sc->switch_in_progress;
2484     sc->old_scp = cur_scp;
2485     sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2486     if (sc->new_scp == sc->old_scp) {
2487         sc->switch_in_progress = 0;
2488         /*
2489          * XXX wakeup() locks the scheduler lock which will hang if
2490          * the lock is in an in-between state, e.g., when we stop at
2491          * a breakpoint at fork_exit.  It has always been wrong to call
2492          * wakeup() when the debugger is active.  In RELENG_4, wakeup()
2493          * is supposed to be locked by splhigh(), but the debugger may
2494          * be invoked at splhigh().
2495          */
2496         if (!kdb_active)
2497             wakeup(VTY_WCHAN(sc,next_scr));
2498         splx(s);
2499         DPRINTF(5, ("switch done (new == old)\n"));
2500         return 0;
2501     }
2502
2503     /* has controlling process died? */
2504     vt_proc_alive(sc->old_scp);
2505     vt_proc_alive(sc->new_scp);
2506
2507     /* wait for the controlling process to release the screen, if necessary */
2508     if (signal_vt_rel(sc->old_scp)) {
2509         splx(s);
2510         return 0;
2511     }
2512
2513     /* go set up the new vty screen */
2514     splx(s);
2515     exchange_scr(sc);
2516     s = spltty();
2517
2518     /* wake up processes waiting for this vty */
2519     if (!kdb_active)
2520         wakeup(VTY_WCHAN(sc,next_scr));
2521
2522     /* wait for the controlling process to acknowledge, if necessary */
2523     if (signal_vt_acq(sc->cur_scp)) {
2524         splx(s);
2525         return 0;
2526     }
2527
2528     sc->switch_in_progress = 0;
2529     if (sc->unit == sc_console_unit)
2530         cnavailable(sc_consptr,  TRUE);
2531     splx(s);
2532     DPRINTF(5, ("switch done\n"));
2533
2534     return 0;
2535 }
2536
2537 static int
2538 do_switch_scr(sc_softc_t *sc, int s)
2539 {
2540     vt_proc_alive(sc->new_scp);
2541
2542     splx(s);
2543     exchange_scr(sc);
2544     s = spltty();
2545     /* sc->cur_scp == sc->new_scp */
2546     wakeup(VTY_WCHAN(sc,sc->cur_scp->index));
2547
2548     /* wait for the controlling process to acknowledge, if necessary */
2549     if (!signal_vt_acq(sc->cur_scp)) {
2550         sc->switch_in_progress = 0;
2551         if (sc->unit == sc_console_unit)
2552             cnavailable(sc_consptr,  TRUE);
2553     }
2554
2555     return s;
2556 }
2557
2558 static int
2559 vt_proc_alive(scr_stat *scp)
2560 {
2561     struct proc *p;
2562
2563     if (scp->proc) {
2564         if ((p = pfind(scp->pid)) != NULL)
2565             PROC_UNLOCK(p);
2566         if (scp->proc == p)
2567             return TRUE;
2568         scp->proc = NULL;
2569         scp->smode.mode = VT_AUTO;
2570         DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2571     }
2572     return FALSE;
2573 }
2574
2575 static int
2576 signal_vt_rel(scr_stat *scp)
2577 {
2578     if (scp->smode.mode != VT_PROCESS)
2579         return FALSE;
2580     scp->status |= SWITCH_WAIT_REL;
2581     PROC_LOCK(scp->proc);
2582     kern_psignal(scp->proc, scp->smode.relsig);
2583     PROC_UNLOCK(scp->proc);
2584     DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2585     return TRUE;
2586 }
2587
2588 static int
2589 signal_vt_acq(scr_stat *scp)
2590 {
2591     if (scp->smode.mode != VT_PROCESS)
2592         return FALSE;
2593     if (scp->sc->unit == sc_console_unit)
2594         cnavailable(sc_consptr,  FALSE);
2595     scp->status |= SWITCH_WAIT_ACQ;
2596     PROC_LOCK(scp->proc);
2597     kern_psignal(scp->proc, scp->smode.acqsig);
2598     PROC_UNLOCK(scp->proc);
2599     DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2600     return TRUE;
2601 }
2602
2603 static int
2604 finish_vt_rel(scr_stat *scp, int release, int *s)
2605 {
2606     if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2607         scp->status &= ~SWITCH_WAIT_REL;
2608         if (release)
2609             *s = do_switch_scr(scp->sc, *s);
2610         else
2611             scp->sc->switch_in_progress = 0;
2612         return 0;
2613     }
2614     return EINVAL;
2615 }
2616
2617 static int
2618 finish_vt_acq(scr_stat *scp)
2619 {
2620     if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2621         scp->status &= ~SWITCH_WAIT_ACQ;
2622         scp->sc->switch_in_progress = 0;
2623         return 0;
2624     }
2625     return EINVAL;
2626 }
2627
2628 static void
2629 exchange_scr(sc_softc_t *sc)
2630 {
2631     scr_stat *scp;
2632
2633     /* save the current state of video and keyboard */
2634     sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2635     if (!ISGRAPHSC(sc->old_scp))
2636         sc_remove_cursor_image(sc->old_scp);
2637     if (sc->old_scp->kbd_mode == K_XLATE)
2638         save_kbd_state(sc->old_scp);
2639
2640     /* set up the video for the new screen */
2641     scp = sc->cur_scp = sc->new_scp;
2642 #ifdef PC98
2643     if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp))
2644 #else
2645     if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2646 #endif
2647         set_mode(scp);
2648 #ifndef __sparc64__
2649     else
2650         sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2651                     (void *)sc->adp->va_window, FALSE);
2652 #endif
2653     scp->status |= MOUSE_HIDDEN;
2654     sc_move_cursor(scp, scp->xpos, scp->ypos);
2655     if (!ISGRAPHSC(scp))
2656         sc_set_cursor_image(scp);
2657 #ifndef SC_NO_PALETTE_LOADING
2658     if (ISGRAPHSC(sc->old_scp)) {
2659 #ifdef SC_PIXEL_MODE
2660         if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2661             vidd_load_palette(sc->adp, sc->palette2);
2662         else
2663 #endif
2664         vidd_load_palette(sc->adp, sc->palette);
2665     }
2666 #endif
2667     sc_set_border(scp, scp->border);
2668
2669     /* set up the keyboard for the new screen */
2670     if (sc->old_scp->kbd_mode != scp->kbd_mode)
2671         (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2672     update_kbd_state(scp, scp->status, LOCK_MASK);
2673
2674     mark_all(scp);
2675 }
2676
2677 static void
2678 sc_puts(scr_stat *scp, u_char *buf, int len, int kernel)
2679 {
2680     int need_unlock = 0;
2681
2682 #ifdef DEV_SPLASH
2683     /* make screensaver happy */
2684     if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2685         run_scrn_saver = FALSE;
2686 #endif
2687
2688     if (scp->tsw) {
2689         if (!kdb_active && !mtx_owned(&scp->sc->scr_lock)) {
2690                 need_unlock = 1;
2691                 mtx_lock_spin(&scp->sc->scr_lock);
2692         }
2693         (*scp->tsw->te_puts)(scp, buf, len, kernel);
2694         if (need_unlock)
2695                 mtx_unlock_spin(&scp->sc->scr_lock);
2696     }
2697
2698     if (scp->sc->delayed_next_scr)
2699         sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2700 }
2701
2702 void
2703 sc_draw_cursor_image(scr_stat *scp)
2704 {
2705     /* assert(scp == scp->sc->cur_scp); */
2706     SC_VIDEO_LOCK(scp->sc);
2707     (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2708                               scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2709                               sc_inside_cutmark(scp, scp->cursor_pos));
2710     scp->cursor_oldpos = scp->cursor_pos;
2711     SC_VIDEO_UNLOCK(scp->sc);
2712 }
2713
2714 void
2715 sc_remove_cursor_image(scr_stat *scp)
2716 {
2717     /* assert(scp == scp->sc->cur_scp); */
2718     SC_VIDEO_LOCK(scp->sc);
2719     (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2720                               scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2721                               sc_inside_cutmark(scp, scp->cursor_oldpos));
2722     SC_VIDEO_UNLOCK(scp->sc);
2723 }
2724
2725 static void
2726 update_cursor_image(scr_stat *scp)
2727 {
2728     /* assert(scp == scp->sc->cur_scp); */
2729     sc_remove_cursor_image(scp);
2730     sc_set_cursor_image(scp);
2731     sc_draw_cursor_image(scp);
2732 }
2733
2734 void
2735 sc_set_cursor_image(scr_stat *scp)
2736 {
2737     scp->curs_attr.flags = scp->curr_curs_attr.flags;
2738     if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2739         /* hidden cursor is internally represented as zero-height underline */
2740         scp->curs_attr.flags = CONS_CHAR_CURSOR;
2741         scp->curs_attr.base = scp->curs_attr.height = 0;
2742     } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2743         scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2744                                   scp->font_size - 1);
2745         scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2746                                     scp->font_size - scp->curs_attr.base);
2747     } else {    /* block cursor */
2748         scp->curs_attr.base = 0;
2749         scp->curs_attr.height = scp->font_size;
2750     }
2751
2752     /* assert(scp == scp->sc->cur_scp); */
2753     SC_VIDEO_LOCK(scp->sc);
2754     (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2755                              scp->curs_attr.flags & CONS_BLINK_CURSOR);
2756     SC_VIDEO_UNLOCK(scp->sc);
2757 }
2758
2759 static void
2760 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2761 {
2762     if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2763         sc_remove_cursor_image(scp);
2764
2765     if (base >= 0)
2766         scp->curr_curs_attr.base = base;
2767     if (height >= 0)
2768         scp->curr_curs_attr.height = height;
2769     if (flags & CONS_RESET_CURSOR)
2770         scp->curr_curs_attr = scp->dflt_curs_attr;
2771     else
2772         scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2773
2774     if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2775         sc_set_cursor_image(scp);
2776         sc_draw_cursor_image(scp);
2777     }
2778 }
2779
2780 void
2781 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2782 {
2783     sc_softc_t *sc;
2784     struct tty *tp;
2785     int s;
2786     int i;
2787
2788     s = spltty();
2789     if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2790         /* local (per vty) change */
2791         change_cursor_shape(scp, flags, base, height);
2792         splx(s);
2793         return;
2794     }
2795
2796     /* global change */
2797     sc = scp->sc;
2798     if (base >= 0)
2799         sc->curs_attr.base = base;
2800     if (height >= 0)
2801         sc->curs_attr.height = height;
2802     if (flags != -1) {
2803         if (flags & CONS_RESET_CURSOR)
2804             sc->curs_attr = sc->dflt_curs_attr;
2805         else
2806             sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2807     }
2808
2809     for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2810         if ((tp = SC_DEV(sc, i)) == NULL)
2811             continue;
2812         if ((scp = sc_get_stat(tp)) == NULL)
2813             continue;
2814         scp->dflt_curs_attr = sc->curs_attr;
2815         change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2816     }
2817     splx(s);
2818 }
2819
2820 static void
2821 scinit(int unit, int flags)
2822 {
2823
2824     /*
2825      * When syscons is being initialized as the kernel console, malloc()
2826      * is not yet functional, because various kernel structures has not been
2827      * fully initialized yet.  Therefore, we need to declare the following
2828      * static buffers for the console.  This is less than ideal, 
2829      * but is necessry evil for the time being.  XXX
2830      */
2831 #ifdef PC98
2832     static u_short sc_buffer[ROW*COL*2];/* XXX */
2833 #else
2834     static u_short sc_buffer[ROW*COL];  /* XXX */
2835 #endif
2836 #ifndef SC_NO_FONT_LOADING
2837     static u_char font_8[256*8];
2838     static u_char font_14[256*14];
2839     static u_char font_16[256*16];
2840 #endif
2841
2842     sc_softc_t *sc;
2843     scr_stat *scp;
2844     video_adapter_t *adp;
2845     int col;
2846     int row;
2847     int i;
2848
2849     /* one time initialization */
2850     if (init_done == COLD)
2851         sc_get_bios_values(&bios_value);
2852     init_done = WARM;
2853
2854     /*
2855      * Allocate resources.  Even if we are being called for the second
2856      * time, we must allocate them again, because they might have 
2857      * disappeared...
2858      */
2859     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2860     if ((sc->flags & SC_INIT_DONE) == 0) {
2861         mtx_init(&sc->scr_lock, "scrlock", NULL, MTX_SPIN);
2862         SC_VIDEO_LOCKINIT(sc);
2863     }
2864
2865     adp = NULL;
2866     if (sc->adapter >= 0) {
2867         vid_release(sc->adp, (void *)&sc->adapter);
2868         adp = sc->adp;
2869         sc->adp = NULL;
2870     }
2871     if (sc->keyboard >= 0) {
2872         DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2873         i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2874         DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2875         if (sc->kbd != NULL) {
2876             DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2877                 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2878         }
2879         sc->kbd = NULL;
2880     }
2881     sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2882     sc->adp = vid_get_adapter(sc->adapter);
2883     /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2884
2885     sc->keyboard = sc_allocate_keyboard(sc, unit);
2886     DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2887
2888     sc->kbd = kbd_get_keyboard(sc->keyboard);
2889     if (sc->kbd != NULL) {
2890         DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2891                 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2892     }
2893
2894     if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2895
2896         sc->initial_mode = sc->adp->va_initial_mode;
2897
2898 #ifndef SC_NO_FONT_LOADING
2899         if (flags & SC_KERNEL_CONSOLE) {
2900             sc->font_8 = font_8;
2901             sc->font_14 = font_14;
2902             sc->font_16 = font_16;
2903         } else if (sc->font_8 == NULL) {
2904             /* assert(sc_malloc) */
2905             sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2906             sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2907             sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2908         }
2909 #endif
2910
2911         /* extract the hardware cursor location and hide the cursor for now */
2912         vidd_read_hw_cursor(sc->adp, &col, &row);
2913         vidd_set_hw_cursor(sc->adp, -1, -1);
2914
2915         /* set up the first console */
2916         sc->first_vty = unit*MAXCONS;
2917         sc->vtys = MAXCONS;             /* XXX: should be configurable */
2918         if (flags & SC_KERNEL_CONSOLE) {
2919             /*
2920              * Set up devs structure but don't use it yet, calling make_dev()
2921              * might panic kernel.  Wait for sc_attach_unit() to actually
2922              * create the devices.
2923              */
2924             sc->dev = main_devs;
2925             scp = &main_console;
2926             init_scp(sc, sc->first_vty, scp);
2927             sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2928                         (void *)sc_buffer, FALSE);
2929
2930             /* move cursors to the initial positions */
2931             if (col >= scp->xsize)
2932                 col = 0;
2933             if (row >= scp->ysize)
2934                 row = scp->ysize - 1;
2935             scp->xpos = col;
2936             scp->ypos = row;
2937             scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2938
2939             if (sc_init_emulator(scp, SC_DFLT_TERM))
2940                 sc_init_emulator(scp, "*");
2941             (*scp->tsw->te_default_attr)(scp,
2942                                          user_default.std_color,
2943                                          user_default.rev_color);
2944         } else {
2945             /* assert(sc_malloc) */
2946             sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF,
2947                 M_WAITOK|M_ZERO);
2948             sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
2949             scp = alloc_scp(sc, sc->first_vty);
2950             SC_STAT(sc->dev[0]) = scp;
2951         }
2952         sc->cur_scp = scp;
2953
2954 #ifndef __sparc64__
2955         /* copy screen to temporary buffer */
2956         sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2957                     (void *)scp->sc->adp->va_window, FALSE);
2958         if (ISTEXTSC(scp))
2959             sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2960 #endif
2961
2962         if (bios_value.cursor_end < scp->font_size)
2963             sc->dflt_curs_attr.base = scp->font_size - 
2964                                           bios_value.cursor_end - 1;
2965         else
2966             sc->dflt_curs_attr.base = 0;
2967         i = bios_value.cursor_end - bios_value.cursor_start + 1;
2968         sc->dflt_curs_attr.height = imin(i, scp->font_size);
2969         sc->dflt_curs_attr.flags = 0;
2970         sc->curs_attr = sc->dflt_curs_attr;
2971         scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2972
2973 #ifndef SC_NO_SYSMOUSE
2974         sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2975 #endif
2976         if (!ISGRAPHSC(scp)) {
2977             sc_set_cursor_image(scp);
2978             sc_draw_cursor_image(scp);
2979         }
2980
2981         /* save font and palette */
2982 #ifndef SC_NO_FONT_LOADING
2983         sc->fonts_loaded = 0;
2984         if (ISFONTAVAIL(sc->adp->va_flags)) {
2985 #ifdef SC_DFLT_FONT
2986             bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2987             bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2988             bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2989             sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2990             if (scp->font_size < 14) {
2991                 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2992             } else if (scp->font_size >= 16) {
2993                 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2994             } else {
2995                 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2996             }
2997 #else /* !SC_DFLT_FONT */
2998             if (scp->font_size < 14) {
2999                 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3000                 sc->fonts_loaded = FONT_8;
3001             } else if (scp->font_size >= 16) {
3002                 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3003                 sc->fonts_loaded = FONT_16;
3004             } else {
3005                 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3006                 sc->fonts_loaded = FONT_14;
3007             }
3008 #endif /* SC_DFLT_FONT */
3009             /* FONT KLUDGE: always use the font page #0. XXX */
3010             sc_show_font(scp, 0);
3011         }
3012 #endif /* !SC_NO_FONT_LOADING */
3013
3014 #ifndef SC_NO_PALETTE_LOADING
3015         vidd_save_palette(sc->adp, sc->palette);
3016 #ifdef SC_PIXEL_MODE
3017         for (i = 0; i < sizeof(sc->palette2); i++)
3018                 sc->palette2[i] = i / 3;
3019 #endif
3020 #endif
3021
3022 #ifdef DEV_SPLASH
3023         if (!(sc->flags & SC_SPLASH_SCRN)) {
3024             /* we are ready to put up the splash image! */
3025             splash_init(sc->adp, scsplash_callback, sc);
3026             sc->flags |= SC_SPLASH_SCRN;
3027         }
3028 #endif
3029     }
3030
3031     /* the rest is not necessary, if we have done it once */
3032     if (sc->flags & SC_INIT_DONE)
3033         return;
3034
3035     /* initialize mapscrn arrays to a one to one map */
3036     for (i = 0; i < sizeof(sc->scr_map); i++)
3037         sc->scr_map[i] = sc->scr_rmap[i] = i;
3038 #ifdef PC98
3039     sc->scr_map[0x5c] = (u_char)0xfc;   /* for backslash */
3040 #endif
3041
3042     sc->flags |= SC_INIT_DONE;
3043 }
3044
3045 static void
3046 scterm(int unit, int flags)
3047 {
3048     sc_softc_t *sc;
3049     scr_stat *scp;
3050
3051     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3052     if (sc == NULL)
3053         return;                 /* shouldn't happen */
3054
3055 #ifdef DEV_SPLASH
3056     /* this console is no longer available for the splash screen */
3057     if (sc->flags & SC_SPLASH_SCRN) {
3058         splash_term(sc->adp);
3059         sc->flags &= ~SC_SPLASH_SCRN;
3060     }
3061 #endif
3062
3063 #if 0 /* XXX */
3064     /* move the hardware cursor to the upper-left corner */
3065     vidd_set_hw_cursor(sc->adp, 0, 0);
3066 #endif
3067
3068     /* release the keyboard and the video card */
3069     if (sc->keyboard >= 0)
3070         kbd_release(sc->kbd, &sc->keyboard);
3071     if (sc->adapter >= 0)
3072         vid_release(sc->adp, &sc->adapter);
3073
3074     /* stop the terminal emulator, if any */
3075     scp = sc_get_stat(sc->dev[0]);
3076     if (scp->tsw)
3077         (*scp->tsw->te_term)(scp, &scp->ts);
3078     if (scp->ts != NULL)
3079         free(scp->ts, M_DEVBUF);
3080     mtx_destroy(&sc->scr_lock);
3081     mtx_destroy(&sc->video_mtx);
3082
3083     /* clear the structure */
3084     if (!(flags & SC_KERNEL_CONSOLE)) {
3085         /* XXX: We need delete_dev() for this */
3086         free(sc->dev, M_DEVBUF);
3087 #if 0
3088         /* XXX: We need a ttyunregister for this */
3089         free(sc->tty, M_DEVBUF);
3090 #endif
3091 #ifndef SC_NO_FONT_LOADING
3092         free(sc->font_8, M_DEVBUF);
3093         free(sc->font_14, M_DEVBUF);
3094         free(sc->font_16, M_DEVBUF);
3095 #endif
3096         /* XXX vtb, history */
3097     }
3098     bzero(sc, sizeof(*sc));
3099     sc->keyboard = -1;
3100     sc->adapter = -1;
3101 }
3102
3103 static void
3104 scshutdown(__unused void *arg, __unused int howto)
3105 {
3106
3107         KASSERT(sc_console != NULL, ("sc_console != NULL"));
3108         KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3109         KASSERT(sc_console->sc->cur_scp != NULL,
3110             ("sc_console->sc->cur_scp != NULL"));
3111
3112         sc_touch_scrn_saver();
3113         if (!cold &&
3114             sc_console->sc->cur_scp->index != sc_console->index &&
3115             sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3116             sc_console->smode.mode == VT_AUTO)
3117                 sc_switch_scr(sc_console->sc, sc_console->index);
3118         shutdown_in_progress = TRUE;
3119 }
3120
3121 static void
3122 scsuspend(__unused void *arg)
3123 {
3124         int retry;
3125
3126         KASSERT(sc_console != NULL, ("sc_console != NULL"));
3127         KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3128         KASSERT(sc_console->sc->cur_scp != NULL,
3129             ("sc_console->sc->cur_scp != NULL"));
3130
3131         sc_susp_scr = sc_console->sc->cur_scp->index;
3132         if (sc_no_suspend_vtswitch ||
3133             sc_susp_scr == sc_console->index) {
3134                 sc_touch_scrn_saver();
3135                 sc_susp_scr = -1;
3136                 return;
3137         }
3138         for (retry = 0; retry < 10; retry++) {
3139                 sc_switch_scr(sc_console->sc, sc_console->index);
3140                 if (!sc_console->sc->switch_in_progress)
3141                         break;
3142                 pause("scsuspend", hz);
3143         }
3144         suspend_in_progress = TRUE;
3145 }
3146
3147 static void
3148 scresume(__unused void *arg)
3149 {
3150
3151         KASSERT(sc_console != NULL, ("sc_console != NULL"));
3152         KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3153         KASSERT(sc_console->sc->cur_scp != NULL,
3154             ("sc_console->sc->cur_scp != NULL"));
3155
3156         suspend_in_progress = FALSE;
3157         if (sc_susp_scr < 0) {
3158                 update_font(sc_console->sc->cur_scp);
3159                 return;
3160         }
3161         sc_switch_scr(sc_console->sc, sc_susp_scr);
3162 }
3163
3164 int
3165 sc_clean_up(scr_stat *scp)
3166 {
3167 #ifdef DEV_SPLASH
3168     int error;
3169 #endif
3170
3171     if (scp->sc->flags & SC_SCRN_BLANKED) {
3172         sc_touch_scrn_saver();
3173 #ifdef DEV_SPLASH
3174         if ((error = wait_scrn_saver_stop(scp->sc)))
3175             return error;
3176 #endif
3177     }
3178     scp->status |= MOUSE_HIDDEN;
3179     sc_remove_mouse_image(scp);
3180     sc_remove_cutmarking(scp);
3181     return 0;
3182 }
3183
3184 void
3185 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3186 {
3187     sc_vtb_t new;
3188     sc_vtb_t old;
3189
3190     old = scp->vtb;
3191     sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3192     if (!discard && (old.vtb_flags & VTB_VALID)) {
3193         /* retain the current cursor position and buffer contants */
3194         scp->cursor_oldpos = scp->cursor_pos;
3195         /* 
3196          * This works only if the old buffer has the same size as or larger 
3197          * than the new one. XXX
3198          */
3199         sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3200         scp->vtb = new;
3201     } else {
3202         scp->vtb = new;
3203         sc_vtb_destroy(&old);
3204     }
3205
3206 #ifndef SC_NO_SYSMOUSE
3207     /* move the mouse cursor at the center of the screen */
3208     sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3209 #endif
3210 }
3211
3212 static scr_stat
3213 *alloc_scp(sc_softc_t *sc, int vty)
3214 {
3215     scr_stat *scp;
3216
3217     /* assert(sc_malloc) */
3218
3219     scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3220     init_scp(sc, vty, scp);
3221
3222     sc_alloc_scr_buffer(scp, TRUE, TRUE);
3223     if (sc_init_emulator(scp, SC_DFLT_TERM))
3224         sc_init_emulator(scp, "*");
3225
3226 #ifndef SC_NO_CUTPASTE
3227     sc_alloc_cut_buffer(scp, TRUE);
3228 #endif
3229
3230 #ifndef SC_NO_HISTORY
3231     sc_alloc_history_buffer(scp, 0, 0, TRUE);
3232 #endif
3233
3234     return scp;
3235 }
3236
3237 static void
3238 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3239 {
3240     video_info_t info;
3241
3242     bzero(scp, sizeof(*scp));
3243
3244     scp->index = vty;
3245     scp->sc = sc;
3246     scp->status = 0;
3247     scp->mode = sc->initial_mode;
3248     vidd_get_info(sc->adp, scp->mode, &info);
3249     if (info.vi_flags & V_INFO_GRAPHICS) {
3250         scp->status |= GRAPHICS_MODE;
3251         scp->xpixel = info.vi_width;
3252         scp->ypixel = info.vi_height;
3253         scp->xsize = info.vi_width/info.vi_cwidth;
3254         scp->ysize = info.vi_height/info.vi_cheight;
3255         scp->font_size = 0;
3256         scp->font = NULL;
3257     } else {
3258         scp->xsize = info.vi_width;
3259         scp->ysize = info.vi_height;
3260         scp->xpixel = scp->xsize*info.vi_cwidth;
3261         scp->ypixel = scp->ysize*info.vi_cheight;
3262     }
3263
3264     scp->font_size = info.vi_cheight;
3265     scp->font_width = info.vi_cwidth;
3266 #ifndef SC_NO_FONT_LOADING
3267     if (info.vi_cheight < 14)
3268         scp->font = sc->font_8;
3269     else if (info.vi_cheight >= 16)
3270         scp->font = sc->font_16;
3271     else
3272         scp->font = sc->font_14;
3273 #else
3274     scp->font = NULL;
3275 #endif
3276
3277     sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3278 #ifndef __sparc64__
3279     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3280 #endif
3281     scp->xoff = scp->yoff = 0;
3282     scp->xpos = scp->ypos = 0;
3283     scp->start = scp->xsize * scp->ysize - 1;
3284     scp->end = 0;
3285     scp->tsw = NULL;
3286     scp->ts = NULL;
3287     scp->rndr = NULL;
3288     scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3289     scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3290     scp->mouse_cut_start = scp->xsize*scp->ysize;
3291     scp->mouse_cut_end = -1;
3292     scp->mouse_signal = 0;
3293     scp->mouse_pid = 0;
3294     scp->mouse_proc = NULL;
3295     scp->kbd_mode = K_XLATE;
3296     scp->bell_pitch = bios_value.bell_pitch;
3297     scp->bell_duration = BELL_DURATION;
3298     scp->status |= (bios_value.shift_state & NLKED);
3299     scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3300     scp->pid = 0;
3301     scp->proc = NULL;
3302     scp->smode.mode = VT_AUTO;
3303     scp->history = NULL;
3304     scp->history_pos = 0;
3305     scp->history_size = 0;
3306 }
3307
3308 int
3309 sc_init_emulator(scr_stat *scp, char *name)
3310 {
3311     sc_term_sw_t *sw;
3312     sc_rndr_sw_t *rndr;
3313     void *p;
3314     int error;
3315
3316     if (name == NULL)   /* if no name is given, use the current emulator */
3317         sw = scp->tsw;
3318     else                /* ...otherwise find the named emulator */
3319         sw = sc_term_match(name);
3320     if (sw == NULL)
3321         return EINVAL;
3322
3323     rndr = NULL;
3324     if (strcmp(sw->te_renderer, "*") != 0) {
3325         rndr = sc_render_match(scp, sw->te_renderer,
3326                                scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3327     }
3328     if (rndr == NULL) {
3329         rndr = sc_render_match(scp, scp->sc->adp->va_name,
3330                                scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3331         if (rndr == NULL)
3332             return ENODEV;
3333     }
3334
3335     if (sw == scp->tsw) {
3336         error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3337         scp->rndr = rndr;
3338         scp->rndr->init(scp);
3339         sc_clear_screen(scp);
3340         /* assert(error == 0); */
3341         return error;
3342     }
3343
3344     if (sc_malloc && (sw->te_size > 0))
3345         p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3346     else
3347         p = NULL;
3348     error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3349     if (error)
3350         return error;
3351
3352     if (scp->tsw)
3353         (*scp->tsw->te_term)(scp, &scp->ts);
3354     if (scp->ts != NULL)
3355         free(scp->ts, M_DEVBUF);
3356     scp->tsw = sw;
3357     scp->ts = p;
3358     scp->rndr = rndr;
3359     scp->rndr->init(scp);
3360
3361     /* XXX */
3362     (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3363     sc_clear_screen(scp);
3364
3365     return 0;
3366 }
3367
3368 /*
3369  * scgetc(flags) - get character from keyboard.
3370  * If flags & SCGETC_CN, then avoid harmful side effects.
3371  * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3372  * return NOKEY if there is nothing there.
3373  */
3374 static u_int
3375 scgetc(sc_softc_t *sc, u_int flags)
3376 {
3377     scr_stat *scp;
3378 #ifndef SC_NO_HISTORY
3379     struct tty *tp;
3380 #endif
3381     u_int c;
3382     int this_scr;
3383     int f;
3384     int i;
3385
3386     if (sc->kbd == NULL)
3387         return NOKEY;
3388
3389 next_code:
3390 #if 1
3391     /* I don't like this, but... XXX */
3392     if (flags & SCGETC_CN)
3393         sccnupdate(sc->cur_scp);
3394 #endif
3395     scp = sc->cur_scp;
3396     /* first see if there is something in the keyboard port */
3397     for (;;) {
3398         c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3399         if (c == ERRKEY) {
3400             if (!(flags & SCGETC_CN))
3401                 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3402         } else if (c == NOKEY)
3403             return c;
3404         else
3405             break;
3406     }
3407
3408     /* make screensaver happy */
3409     if (!(c & RELKEY))
3410         sc_touch_scrn_saver();
3411
3412     if (!(flags & SCGETC_CN))
3413         random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
3414
3415     if (scp->kbd_mode != K_XLATE)
3416         return KEYCHAR(c);
3417
3418     /* if scroll-lock pressed allow history browsing */
3419     if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3420
3421         scp->status &= ~CURSOR_ENABLED;
3422         sc_remove_cursor_image(scp);
3423
3424 #ifndef SC_NO_HISTORY
3425         if (!(scp->status & BUFFER_SAVED)) {
3426             scp->status |= BUFFER_SAVED;
3427             sc_hist_save(scp);
3428         }
3429         switch (c) {
3430         /* FIXME: key codes */
3431         case SPCLKEY | FKEY | F(49):  /* home key */
3432             sc_remove_cutmarking(scp);
3433             sc_hist_home(scp);
3434             goto next_code;
3435
3436         case SPCLKEY | FKEY | F(57):  /* end key */
3437             sc_remove_cutmarking(scp);
3438             sc_hist_end(scp);
3439             goto next_code;
3440
3441         case SPCLKEY | FKEY | F(50):  /* up arrow key */
3442             sc_remove_cutmarking(scp);
3443             if (sc_hist_up_line(scp))
3444                 if (!(flags & SCGETC_CN))
3445                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3446             goto next_code;
3447
3448         case SPCLKEY | FKEY | F(58):  /* down arrow key */
3449             sc_remove_cutmarking(scp);
3450             if (sc_hist_down_line(scp))
3451                 if (!(flags & SCGETC_CN))
3452                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3453             goto next_code;
3454
3455         case SPCLKEY | FKEY | F(51):  /* page up key */
3456             sc_remove_cutmarking(scp);
3457             for (i=0; i<scp->ysize; i++)
3458             if (sc_hist_up_line(scp)) {
3459                 if (!(flags & SCGETC_CN))
3460                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3461                 break;
3462             }
3463             goto next_code;
3464
3465         case SPCLKEY | FKEY | F(59):  /* page down key */
3466             sc_remove_cutmarking(scp);
3467             for (i=0; i<scp->ysize; i++)
3468             if (sc_hist_down_line(scp)) {
3469                 if (!(flags & SCGETC_CN))
3470                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3471                 break;
3472             }
3473             goto next_code;
3474         }
3475 #endif /* SC_NO_HISTORY */
3476     }
3477
3478     /* 
3479      * Process and consume special keys here.  Return a plain char code
3480      * or a char code with the META flag or a function key code.
3481      */
3482     if (c & RELKEY) {
3483         /* key released */
3484         /* goto next_code */
3485     } else {
3486         /* key pressed */
3487         if (c & SPCLKEY) {
3488             c &= ~SPCLKEY;
3489             switch (KEYCHAR(c)) {
3490             /* LOCKING KEYS */
3491             case NLK: case CLK: case ALK:
3492                 break;
3493             case SLK:
3494                 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3495                 if (f & SLKED) {
3496                     scp->status |= SLKED;
3497                 } else {
3498                     if (scp->status & SLKED) {
3499                         scp->status &= ~SLKED;
3500 #ifndef SC_NO_HISTORY
3501                         if (scp->status & BUFFER_SAVED) {
3502                             if (!sc_hist_restore(scp))
3503                                 sc_remove_cutmarking(scp);
3504                             scp->status &= ~BUFFER_SAVED;
3505                             scp->status |= CURSOR_ENABLED;
3506                             sc_draw_cursor_image(scp);
3507                         }
3508                         tp = SC_DEV(sc, scp->index);
3509                         if (!kdb_active && tty_opened_ns(tp))
3510                             sctty_outwakeup(tp);
3511 #endif
3512                     }
3513                 }
3514                 break;
3515
3516             case PASTE:
3517 #ifndef SC_NO_CUTPASTE
3518                 sc_mouse_paste(scp);
3519 #endif
3520                 break;
3521
3522             /* NON-LOCKING KEYS */
3523             case NOP:
3524             case LSH:  case RSH:  case LCTR: case RCTR:
3525             case LALT: case RALT: case ASH:  case META:
3526                 break;
3527
3528             case BTAB:
3529                 if (!(sc->flags & SC_SCRN_BLANKED))
3530                     return c;
3531                 break;
3532
3533             case SPSC:
3534 #ifdef DEV_SPLASH
3535                 /* force activatation/deactivation of the screen saver */
3536                 if (!(sc->flags & SC_SCRN_BLANKED)) {
3537                     run_scrn_saver = TRUE;
3538                     sc->scrn_time_stamp -= scrn_blank_time;
3539                 }
3540                 if (cold) {
3541                     /*
3542                      * While devices are being probed, the screen saver need
3543                      * to be invoked explicitly. XXX
3544                      */
3545                     if (sc->flags & SC_SCRN_BLANKED) {
3546                         scsplash_stick(FALSE);
3547                         stop_scrn_saver(sc, current_saver);
3548                     } else {
3549                         if (!ISGRAPHSC(scp)) {
3550                             scsplash_stick(TRUE);
3551                             (*current_saver)(sc, TRUE);
3552                         }
3553                     }
3554                 }
3555 #endif /* DEV_SPLASH */
3556                 break;
3557
3558             case RBT:
3559 #ifndef SC_DISABLE_REBOOT
3560                 if (enable_reboot)
3561                         shutdown_nice(0);
3562 #endif
3563                 break;
3564
3565             case HALT:
3566 #ifndef SC_DISABLE_REBOOT
3567                 if (enable_reboot)
3568                         shutdown_nice(RB_HALT);
3569 #endif
3570                 break;
3571
3572             case PDWN:
3573 #ifndef SC_DISABLE_REBOOT
3574                 if (enable_reboot)
3575                         shutdown_nice(RB_HALT|RB_POWEROFF);
3576 #endif
3577                 break;
3578
3579             case SUSP:
3580                 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3581                 break;
3582             case STBY:
3583                 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3584                 break;
3585
3586             case DBG:
3587 #ifndef SC_DISABLE_KDBKEY
3588                 if (enable_kdbkey)
3589                         kdb_break();
3590 #endif
3591                 break;
3592
3593             case PNC:
3594                 if (enable_panic_key)
3595                         panic("Forced by the panic key");
3596                 break;
3597
3598             case NEXT:
3599                 this_scr = scp->index;
3600                 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3601                         sc->first_vty + i != this_scr; 
3602                         i = (i + 1)%sc->vtys) {
3603                     struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3604                     if (tty_opened_ns(tp)) {
3605                         sc_switch_scr(scp->sc, sc->first_vty + i);
3606                         break;
3607                     }
3608                 }
3609                 break;
3610
3611             case PREV:
3612                 this_scr = scp->index;
3613                 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3614                         sc->first_vty + i != this_scr;
3615                         i = (i + sc->vtys - 1)%sc->vtys) {
3616                     struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3617                     if (tty_opened_ns(tp)) {
3618                         sc_switch_scr(scp->sc, sc->first_vty + i);
3619                         break;
3620                     }
3621                 }
3622                 break;
3623
3624             default:
3625                 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3626                     sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3627                     break;
3628                 }
3629                 /* assert(c & FKEY) */
3630                 if (!(sc->flags & SC_SCRN_BLANKED))
3631                     return c;
3632                 break;
3633             }
3634             /* goto next_code */
3635         } else {
3636             /* regular keys (maybe MKEY is set) */
3637 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
3638             if (enable_kdbkey)
3639                 kdb_alt_break(c, &sc->sc_altbrk);
3640 #endif
3641             if (!(sc->flags & SC_SCRN_BLANKED))
3642                 return c;
3643         }
3644     }
3645
3646     goto next_code;
3647 }
3648
3649 static int
3650 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
3651     int nprot, vm_memattr_t *memattr)
3652 {
3653     scr_stat *scp;
3654
3655     scp = sc_get_stat(tp);
3656     if (scp != scp->sc->cur_scp)
3657         return -1;
3658     return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
3659 }
3660
3661 static void
3662 update_font(scr_stat *scp)
3663 {
3664 #ifndef SC_NO_FONT_LOADING
3665     /* load appropriate font */
3666     if (!(scp->status & GRAPHICS_MODE)) {
3667         if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3668             if (scp->font_size < 14) {
3669                 if (scp->sc->fonts_loaded & FONT_8)
3670                     sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3671             } else if (scp->font_size >= 16) {
3672                 if (scp->sc->fonts_loaded & FONT_16)
3673                     sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3674             } else {
3675                 if (scp->sc->fonts_loaded & FONT_14)
3676                     sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3677             }
3678             /*
3679              * FONT KLUDGE:
3680              * This is an interim kludge to display correct font.
3681              * Always use the font page #0 on the video plane 2.
3682              * Somehow we cannot show the font in other font pages on
3683              * some video cards... XXX
3684              */ 
3685             sc_show_font(scp, 0);
3686         }
3687         mark_all(scp);
3688     }
3689 #endif /* !SC_NO_FONT_LOADING */
3690 }
3691
3692 static int
3693 save_kbd_state(scr_stat *scp)
3694 {
3695     int state;
3696     int error;
3697
3698     error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3699     if (error == ENOIOCTL)
3700         error = ENODEV;
3701     if (error == 0) {
3702         scp->status &= ~LOCK_MASK;
3703         scp->status |= state;
3704     }
3705     return error;
3706 }
3707
3708 static int
3709 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3710 {
3711     int state;
3712     int error;
3713
3714     if (mask != LOCK_MASK) {
3715         error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3716         if (error == ENOIOCTL)
3717             error = ENODEV;
3718         if (error)
3719             return error;
3720         state &= ~mask;
3721         state |= new_bits & mask;
3722     } else {
3723         state = new_bits & LOCK_MASK;
3724     }
3725     error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3726     if (error == ENOIOCTL)
3727         error = ENODEV;
3728     return error;
3729 }
3730
3731 static int
3732 update_kbd_leds(scr_stat *scp, int which)
3733 {
3734     int error;
3735
3736     which &= LOCK_MASK;
3737     error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3738     if (error == ENOIOCTL)
3739         error = ENODEV;
3740     return error;
3741 }
3742
3743 int
3744 set_mode(scr_stat *scp)
3745 {
3746     video_info_t info;
3747
3748     /* reject unsupported mode */
3749     if (vidd_get_info(scp->sc->adp, scp->mode, &info))
3750         return 1;
3751
3752     /* if this vty is not currently showing, do nothing */
3753     if (scp != scp->sc->cur_scp)
3754         return 0;
3755
3756     /* setup video hardware for the given mode */
3757     vidd_set_mode(scp->sc->adp, scp->mode);
3758     scp->rndr->init(scp);
3759 #ifndef __sparc64__
3760     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3761                 (void *)scp->sc->adp->va_window, FALSE);
3762 #endif
3763
3764     update_font(scp);
3765
3766     sc_set_border(scp, scp->border);
3767     sc_set_cursor_image(scp);
3768
3769     return 0;
3770 }
3771
3772 void
3773 sc_set_border(scr_stat *scp, int color)
3774 {
3775     SC_VIDEO_LOCK(scp->sc);
3776     (*scp->rndr->draw_border)(scp, color);
3777     SC_VIDEO_UNLOCK(scp->sc);
3778 }
3779
3780 #ifndef SC_NO_FONT_LOADING
3781 void
3782 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3783              int base, int count)
3784 {
3785     sc_softc_t *sc;
3786
3787     sc = scp->sc;
3788     sc->font_loading_in_progress = TRUE;
3789     vidd_load_font(sc->adp, page, size, width, buf, base, count);
3790     sc->font_loading_in_progress = FALSE;
3791 }
3792
3793 void
3794 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3795              int base, int count)
3796 {
3797     sc_softc_t *sc;
3798
3799     sc = scp->sc;
3800     sc->font_loading_in_progress = TRUE;
3801     vidd_save_font(sc->adp, page, size, width, buf, base, count);
3802     sc->font_loading_in_progress = FALSE;
3803 }
3804
3805 void
3806 sc_show_font(scr_stat *scp, int page)
3807 {
3808     vidd_show_font(scp->sc->adp, page);
3809 }
3810 #endif /* !SC_NO_FONT_LOADING */
3811
3812 void
3813 sc_paste(scr_stat *scp, const u_char *p, int count) 
3814 {
3815     struct tty *tp;
3816     u_char *rmap;
3817
3818     tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3819     if (!tty_opened_ns(tp))
3820         return;
3821     rmap = scp->sc->scr_rmap;
3822     for (; count > 0; --count)
3823         ttydisc_rint(tp, rmap[*p++], 0);
3824     ttydisc_rint_done(tp);
3825 }
3826
3827 void
3828 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup) 
3829 {
3830     struct tty *tp;
3831
3832     tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3833     if (!tty_opened_ns(tp))
3834         return;
3835     ttydisc_rint_simple(tp, p, count);
3836     if (wakeup) {
3837         /* XXX: we can't always call ttydisc_rint_done() here! */
3838         ttydisc_rint_done(tp);
3839     }
3840 }
3841
3842 void
3843 sc_bell(scr_stat *scp, int pitch, int duration)
3844 {
3845     if (cold || shutdown_in_progress || !enable_bell)
3846         return;
3847
3848     if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3849         return;
3850
3851     if (scp->sc->flags & SC_VISUAL_BELL) {
3852         if (scp->sc->blink_in_progress)
3853             return;
3854         scp->sc->blink_in_progress = 3;
3855         if (scp != scp->sc->cur_scp)
3856             scp->sc->blink_in_progress += 2;
3857         blink_screen(scp->sc->cur_scp);
3858     } else if (duration != 0 && pitch != 0) {
3859         if (scp != scp->sc->cur_scp)
3860             pitch *= 2;
3861         sysbeep(1193182 / pitch, duration);
3862     }
3863 }
3864
3865 static void
3866 blink_screen(void *arg)
3867 {
3868     scr_stat *scp = arg;
3869     struct tty *tp;
3870
3871     if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3872         scp->sc->blink_in_progress = 0;
3873         mark_all(scp);
3874         tp = SC_DEV(scp->sc, scp->index);
3875         if (tty_opened_ns(tp))
3876             sctty_outwakeup(tp);
3877         if (scp->sc->delayed_next_scr)
3878             sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3879     }
3880     else {
3881         (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize, 
3882                            scp->sc->blink_in_progress & 1);
3883         scp->sc->blink_in_progress--;
3884         callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0,
3885             blink_screen, scp, C_PREL(0));
3886     }
3887 }
3888
3889 /*
3890  * Until sc_attach_unit() gets called no dev structures will be available
3891  * to store the per-screen current status.  This is the case when the
3892  * kernel is initially booting and needs access to its console.  During
3893  * this early phase of booting the console's current status is kept in
3894  * one statically defined scr_stat structure, and any pointers to the
3895  * dev structures will be NULL.
3896  */
3897
3898 static scr_stat *
3899 sc_get_stat(struct tty *tp)
3900 {
3901         if (tp == NULL)
3902                 return (&main_console);
3903         return (SC_STAT(tp));
3904 }
3905
3906 /*
3907  * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3908  * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3909  * any keyboard.
3910  */
3911
3912 static int
3913 sc_allocate_keyboard(sc_softc_t *sc, int unit)
3914 {
3915         int              idx0, idx;
3916         keyboard_t      *k0, *k;
3917         keyboard_info_t  ki;
3918
3919         idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3920         if (idx0 != -1) {
3921                 k0 = kbd_get_keyboard(idx0);
3922
3923                 for (idx = kbd_find_keyboard2("*", -1, 0);
3924                      idx != -1;
3925                      idx = kbd_find_keyboard2("*", -1, idx + 1)) {
3926                         k = kbd_get_keyboard(idx);
3927
3928                         if (idx == idx0 || KBD_IS_BUSY(k))
3929                                 continue;
3930
3931                         bzero(&ki, sizeof(ki));
3932                         strcpy(ki.kb_name, k->kb_name);
3933                         ki.kb_unit = k->kb_unit;
3934
3935                         (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3936                 }
3937         } else
3938                 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);
3939
3940         return (idx0);
3941 }