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