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