2 * Copyright (c) 1992-1998 Søren Schmidt
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_syscons.h"
37 #include "opt_splash.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/consio.h>
47 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
56 #include <sys/random.h>
57 #include <sys/reboot.h>
58 #include <sys/serial.h>
59 #include <sys/signalvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/power.h>
64 #include <machine/clock.h>
65 #if defined(__sparc64__) || defined(__powerpc__)
66 #include <machine/sc_machdep.h>
68 #include <machine/pc/display.h>
70 #if defined( __i386__) || defined(__amd64__)
71 #include <machine/psl.h>
72 #include <machine/frame.h>
74 #include <machine/stdarg.h>
76 #include <dev/kbd/kbdreg.h>
77 #include <dev/fb/fbreg.h>
78 #include <dev/fb/splashreg.h>
79 #include <dev/syscons/syscons.h>
84 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
85 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
87 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
89 typedef struct default_attr {
90 int std_color; /* normal hardware color */
91 int rev_color; /* reverse hardware color */
94 static default_attr user_default = {
99 static int sc_console_unit = -1;
100 static int sc_saver_keyb_only = 1;
101 static scr_stat *sc_console;
102 static struct consdev *sc_consptr;
103 static scr_stat main_console;
104 static struct tty *main_devs[MAXCONS];
106 static char init_done = COLD;
107 static int shutdown_in_progress = FALSE;
108 static int suspend_in_progress = FALSE;
109 static char sc_malloc = FALSE;
111 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
112 static int run_scrn_saver = FALSE; /* should run the saver? */
113 static int enable_bell = TRUE; /* enable beeper */
115 #ifndef SC_DISABLE_REBOOT
116 static int enable_reboot = TRUE; /* enable keyboard reboot */
119 #ifndef SC_DISABLE_KDBKEY
120 static int enable_kdbkey = TRUE; /* enable keyboard debug */
123 static long scrn_blank_time = 0; /* screen saver timeout value */
125 static int scrn_blanked; /* # of blanked screen */
126 static int sticky_splash = FALSE;
128 static void none_saver(sc_softc_t *sc, int blank) { }
129 static void (*current_saver)(sc_softc_t *, int) = none_saver;
132 #ifdef SC_NO_SUSPEND_VTYSWITCH
133 static int sc_no_suspend_vtswitch = 1;
135 static int sc_no_suspend_vtswitch = 0;
137 static int sc_susp_scr;
139 SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
140 SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
141 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
142 &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
143 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell,
145 #ifndef SC_DISABLE_REBOOT
146 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
147 0, "enable keyboard reboot");
149 #ifndef SC_DISABLE_KDBKEY
150 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
151 0, "enable keyboard debug");
153 TUNABLE_INT("hw.syscons.sc_no_suspend_vtswitch", &sc_no_suspend_vtswitch);
154 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RW,
155 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
156 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
160 tsw_ioctl_t *sc_user_ioctl;
162 static bios_values_t bios_value;
164 static int enable_panic_key;
165 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
166 0, "Enable panic via keypress specified in kbdmap(5)");
168 #define SC_CONSOLECTL 255
170 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
175 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
176 static int scvidprobe(int unit, int flags, int cons);
177 static int sckbdprobe(int unit, int flags, int cons);
178 static void scmeminit(void *arg);
179 static int scdevtounit(struct tty *tp);
180 static kbd_callback_func_t sckbdevent;
181 static void scinit(int unit, int flags);
182 static scr_stat *sc_get_stat(struct tty *tp);
183 static void scterm(int unit, int flags);
184 static void scshutdown(void *, int);
185 static void scsuspend(void *);
186 static void scresume(void *);
187 static u_int scgetc(sc_softc_t *sc, u_int flags);
189 #define SCGETC_NONBLOCK 2
190 static void sccnupdate(scr_stat *scp);
191 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
192 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
193 static timeout_t scrn_timer;
194 static int and_region(int *s1, int *e1, int s2, int e2);
195 static void scrn_update(scr_stat *scp, int show_cursor);
198 static int scsplash_callback(int event, void *arg);
199 static void scsplash_saver(sc_softc_t *sc, int show);
200 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
201 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
202 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
203 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
204 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
205 static int wait_scrn_saver_stop(sc_softc_t *sc);
206 #define scsplash_stick(stick) (sticky_splash = (stick))
207 #else /* !DEV_SPLASH */
208 #define scsplash_stick(stick)
209 #endif /* DEV_SPLASH */
211 static int do_switch_scr(sc_softc_t *sc, int s);
212 static int vt_proc_alive(scr_stat *scp);
213 static int signal_vt_rel(scr_stat *scp);
214 static int signal_vt_acq(scr_stat *scp);
215 static int finish_vt_rel(scr_stat *scp, int release, int *s);
216 static int finish_vt_acq(scr_stat *scp);
217 static void exchange_scr(sc_softc_t *sc);
218 static void update_cursor_image(scr_stat *scp);
219 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
220 static int save_kbd_state(scr_stat *scp);
221 static int update_kbd_state(scr_stat *scp, int state, int mask);
222 static int update_kbd_leds(scr_stat *scp, int which);
223 static timeout_t blink_screen;
224 static struct tty *sc_alloc_tty(int, int);
226 static cn_probe_t sc_cnprobe;
227 static cn_init_t sc_cninit;
228 static cn_term_t sc_cnterm;
229 static cn_getc_t sc_cngetc;
230 static cn_putc_t sc_cnputc;
234 static tsw_open_t sctty_open;
235 static tsw_close_t sctty_close;
236 static tsw_outwakeup_t sctty_outwakeup;
237 static tsw_ioctl_t sctty_ioctl;
238 static tsw_mmap_t sctty_mmap;
240 static struct ttydevsw sc_ttydevsw = {
241 .tsw_open = sctty_open,
242 .tsw_close = sctty_close,
243 .tsw_outwakeup = sctty_outwakeup,
244 .tsw_ioctl = sctty_ioctl,
245 .tsw_mmap = sctty_mmap,
248 static d_ioctl_t consolectl_ioctl;
250 static struct cdevsw consolectl_devsw = {
251 .d_version = D_VERSION,
252 .d_flags = D_NEEDGIANT,
253 .d_ioctl = consolectl_ioctl,
254 .d_name = "consolectl",
258 sc_probe_unit(int unit, int flags)
260 if (!scvidprobe(unit, flags, FALSE)) {
262 printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
266 /* syscons will be attached even when there is no keyboard */
267 sckbdprobe(unit, flags, FALSE);
272 /* probe video adapters, return TRUE if found */
274 scvidprobe(int unit, int flags, int cons)
277 * Access the video adapter driver through the back door!
278 * Video adapter drivers need to be configured before syscons.
279 * However, when syscons is being probed as the low-level console,
280 * they have not been initialized yet. We force them to initialize
281 * themselves here. XXX
283 vid_configure(cons ? VIO_PROBE_ONLY : 0);
285 return (vid_find_adapter("*", unit) >= 0);
288 /* probe the keyboard, return TRUE if found */
290 sckbdprobe(int unit, int flags, int cons)
292 /* access the keyboard driver through the backdoor! */
293 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
295 return (kbd_find_keyboard("*", unit) >= 0);
299 *adapter_name(video_adapter_t *adp)
305 { KD_MONO, { "MDA", "MDA" } },
306 { KD_HERCULES, { "Hercules", "Hercules" } },
307 { KD_CGA, { "CGA", "CGA" } },
308 { KD_EGA, { "EGA", "EGA (mono)" } },
309 { KD_VGA, { "VGA", "VGA (mono)" } },
310 { KD_PC98, { "PC-98x1", "PC-98x1" } },
311 { KD_TGA, { "TGA", "TGA" } },
312 { -1, { "Unknown", "Unknown" } },
316 for (i = 0; names[i].type != -1; ++i)
317 if (names[i].type == adp->va_type)
319 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
323 sctty_outwakeup(struct tty *tp)
327 scr_stat *scp = sc_get_stat(tp);
329 if (scp->status & SLKED ||
330 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
334 len = ttydisc_getc(tp, buf, sizeof buf);
337 sc_puts(scp, buf, len, 0);
342 sc_alloc_tty(int index, int devnum)
344 struct sc_ttysoftc *stc;
347 /* Allocate TTY object and softc to store unit number. */
348 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
349 stc->st_index = index;
351 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
353 /* Create device node. */
354 tty_makedev(tp, NULL, "v%r", devnum);
361 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
371 (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
372 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
373 vidd_get_info(sc->adp, vmode, &info) != 0 ||
374 !sc_support_pixel_mode(&info))
378 * If the mode is unset or unsupported, search for an available
379 * 800x600 graphics mode with the highest color depth.
382 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
383 if (vidd_get_info(sc->adp, i, &info) == 0 &&
384 info.vi_width == 800 && info.vi_height == 600 &&
385 sc_support_pixel_mode(&info) &&
386 info.vi_depth > depth) {
388 depth = info.vi_depth;
392 vidd_get_info(sc->adp, vmode, &info);
395 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
396 fontsize = info.vi_cheight;
398 fontsize = scp->font_size;
402 else if (fontsize >= 16)
406 #ifndef SC_NO_FONT_LOADING
409 if ((sc->fonts_loaded & FONT_8) == 0)
414 if ((sc->fonts_loaded & FONT_14) == 0)
419 if ((sc->fonts_loaded & FONT_16) == 0)
428 if ((sc->flags & SC_SPLASH_SCRN) != 0)
429 splash_term(sc->adp);
431 #ifndef SC_NO_HISTORY
432 if (scp->history != NULL) {
433 sc_vtb_append(&scp->vtb, 0, scp->history,
434 scp->ypos * scp->xsize + scp->xpos);
435 scp->history_pos = sc_vtb_tail(scp->history);
438 vidd_set_mode(sc->adp, vmode);
439 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
440 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
441 scp->xpixel = info.vi_width;
442 scp->ypixel = info.vi_height;
443 scp->xsize = scp->xpixel / 8;
444 scp->ysize = scp->ypixel / fontsize;
446 scp->ypos = scp->ysize - 1;
447 scp->xoff = scp->yoff = 0;
449 scp->font_size = fontsize;
451 scp->start = scp->xsize * scp->ysize - 1;
453 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
454 scp->mode = sc->initial_mode = vmode;
456 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
457 (void *)sc->adp->va_window, FALSE);
459 sc_alloc_scr_buffer(scp, FALSE, FALSE);
460 sc_init_emulator(scp, NULL);
461 #ifndef SC_NO_CUTPASTE
462 sc_alloc_cut_buffer(scp, FALSE);
464 #ifndef SC_NO_HISTORY
465 sc_alloc_history_buffer(scp, 0, 0, FALSE);
467 sc_set_border(scp, scp->border);
468 sc_set_cursor_image(scp);
469 scp->status &= ~UNKNOWN_MODE;
471 if ((sc->flags & SC_SPLASH_SCRN) != 0)
472 splash_init(sc->adp, scsplash_callback, sc);
478 sc_attach_unit(int unit, int flags)
485 flags &= ~SC_KERNEL_CONSOLE;
487 if (sc_console_unit == unit) {
489 * If this unit is being used as the system console, we need to
490 * adjust some variables and buffers before and after scinit().
492 /* assert(sc_console != NULL) */
493 flags |= SC_KERNEL_CONSOLE;
498 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
500 scp = sc_get_stat(sc->dev[0]);
501 if (sc_console == NULL) /* sc_console_unit < 0 */
505 if ((sc->config & SC_VESAMODE) != 0)
506 sc_set_vesa_mode(scp, sc, unit);
507 #endif /* SC_PIXEL_MODE */
509 /* initialize cursor */
511 update_cursor_image(scp);
513 /* get screen update going */
516 /* set up the keyboard */
517 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
518 update_kbd_state(scp, scp->status, LOCK_MASK);
520 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
521 SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
523 printf("%s%d:", SC_DRIVER_NAME, unit);
524 if (sc->adapter >= 0)
525 printf(" fb%d", sc->adapter);
526 if (sc->keyboard >= 0)
527 printf(", kbd%d", sc->keyboard);
529 printf(", terminal emulator: %s (%s)",
530 scp->tsw->te_name, scp->tsw->te_desc);
534 /* Register suspend/resume/shutdown callbacks for the kernel console. */
535 if (sc_console_unit == unit) {
536 EVENTHANDLER_REGISTER(power_suspend, scsuspend, NULL,
537 EVENTHANDLER_PRI_ANY);
538 EVENTHANDLER_REGISTER(power_resume, scresume, NULL,
539 EVENTHANDLER_PRI_ANY);
540 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL,
541 SHUTDOWN_PRI_DEFAULT);
544 for (vc = 0; vc < sc->vtys; vc++) {
545 if (sc->dev[vc] == NULL) {
546 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
547 if (vc == 0 && sc->dev == main_devs)
548 SC_STAT(sc->dev[0]) = &main_console;
551 * The first vty already has struct tty and scr_stat initialized
552 * in scinit(). The other vtys will have these structs when
557 dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600,
559 dev->si_drv1 = sc->dev[0];
572 * As soon as malloc() becomes functional, we had better allocate
573 * various buffers for the kernel console.
576 if (sc_console_unit < 0) /* sc_console == NULL */
579 /* copy the temporary buffer to the final buffer */
580 sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
582 #ifndef SC_NO_CUTPASTE
583 sc_alloc_cut_buffer(sc_console, FALSE);
586 #ifndef SC_NO_HISTORY
587 /* initialize history buffer & pointers */
588 sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
593 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
596 scdevtounit(struct tty *tp)
598 int vty = SC_VTY(tp);
600 if (vty == SC_CONSOLECTL)
601 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
602 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
609 sctty_open(struct tty *tp)
611 int unit = scdevtounit(tp);
618 DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
619 devtoname(tp->t_dev), unit, SC_VTY(tp)));
621 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
625 if (!tty_opened(tp)) {
626 /* Use the current setting of the <-- key as default VERASE. */
627 /* If the Delete key is preferable, an stty is necessary */
629 if (sc->kbd != NULL) {
630 key.keynum = KEYCODE_BS;
631 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
632 tp->t_termios.c_cc[VERASE] = key.key.map[0];
637 scp = sc_get_stat(tp);
639 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
641 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
643 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
644 tp->t_winsize.ws_col = scp->xsize;
645 tp->t_winsize.ws_row = scp->ysize;
652 sctty_close(struct tty *tp)
657 if (SC_VTY(tp) != SC_CONSOLECTL) {
658 scp = sc_get_stat(tp);
659 /* were we in the middle of the VT switching process? */
660 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
662 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
663 cnavailable(sc_consptr, TRUE);
664 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */
665 DPRINTF(5, ("reset WAIT_REL, "));
666 if (finish_vt_acq(scp) == 0) /* force acknowledge */
667 DPRINTF(5, ("reset WAIT_ACQ, "));
669 if (scp == &main_console) {
672 scp->smode.mode = VT_AUTO;
675 sc_vtb_destroy(&scp->vtb);
677 sc_vtb_destroy(&scp->scr);
679 sc_free_history_buffer(scp, scp->ysize);
686 scp->smode.mode = VT_AUTO;
688 scp->kbd_mode = K_XLATE;
689 if (scp == scp->sc->cur_scp)
690 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
691 DPRINTF(5, ("done.\n"));
695 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
697 scread(struct cdev *dev, struct uio *uio, int flag)
699 if (!sc_saver_keyb_only)
700 sc_touch_scrn_saver();
701 return ttyread(dev, uio, flag);
706 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
714 sc = (sc_softc_t *)arg;
715 /* assert(thiskbd == sc->kbd) */
722 case KBDIO_UNLOADING:
725 kbd_release(thiskbd, (void *)&sc->keyboard);
733 * Loop while there is still input to get from the keyboard.
734 * I don't think this is nessesary, and it doesn't fix
735 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
737 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
739 cur_tty = SC_DEV(sc, sc->cur_scp->index);
740 if (!tty_opened(cur_tty))
743 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
746 switch (KEYFLAGS(c)) {
747 case 0x0000: /* normal key */
748 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
750 case FKEY: /* function key, return string */
751 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
753 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
756 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
758 ttydisc_rint_simple(cur_tty, cp, len);
760 case MKEY: /* meta is active, prepend ESC */
761 ttydisc_rint(cur_tty, 0x1b, 0);
762 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
764 case BKEY: /* backtab fixed sequence (esc [ Z) */
765 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
769 ttydisc_rint_done(cur_tty);
772 sc->cur_scp->status |= MOUSE_HIDDEN;
780 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
787 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
788 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
792 /* If there is a user_ioctl function call that first */
794 error = (*sc_user_ioctl)(tp, cmd, data, td);
795 if (error != ENOIOCTL)
799 error = sc_vid_ioctl(tp, cmd, data, td);
800 if (error != ENOIOCTL)
803 #ifndef SC_NO_HISTORY
804 error = sc_hist_ioctl(tp, cmd, data, td);
805 if (error != ENOIOCTL)
809 #ifndef SC_NO_SYSMOUSE
810 error = sc_mouse_ioctl(tp, cmd, data, td);
811 if (error != ENOIOCTL)
815 scp = sc_get_stat(tp);
816 /* assert(scp != NULL) */
817 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
821 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
822 if (error != ENOIOCTL)
826 switch (cmd) { /* process console hardware related ioctl's */
828 case GIO_ATTR: /* get current attributes */
829 /* this ioctl is not processed here, but in the terminal emulator */
832 case GIO_COLOR: /* is this a color console ? */
833 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
836 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
837 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
840 scrn_blank_time = *(int *)data;
841 run_scrn_saver = (scrn_blank_time != 0);
845 case CONS_CURSORTYPE: /* set cursor type (obsolete) */
847 *(int *)data &= CONS_CURSOR_ATTRS;
848 sc_change_cursor_shape(scp, *(int *)data, -1, -1);
852 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */
853 if (((int *)data)[0] & CONS_LOCAL_CURSOR) {
854 ((int *)data)[0] = scp->curr_curs_attr.flags;
855 ((int *)data)[1] = scp->curr_curs_attr.base;
856 ((int *)data)[2] = scp->curr_curs_attr.height;
858 ((int *)data)[0] = sc->curs_attr.flags;
859 ((int *)data)[1] = sc->curs_attr.base;
860 ((int *)data)[2] = sc->curs_attr.height;
864 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */
866 sc_change_cursor_shape(scp, ((int *)data)[0],
867 ((int *)data)[1], ((int *)data)[2]);
871 case CONS_BELLTYPE: /* set bell type sound/visual */
872 if ((*(int *)data) & CONS_VISUAL_BELL)
873 sc->flags |= SC_VISUAL_BELL;
875 sc->flags &= ~SC_VISUAL_BELL;
876 if ((*(int *)data) & CONS_QUIET_BELL)
877 sc->flags |= SC_QUIET_BELL;
879 sc->flags &= ~SC_QUIET_BELL;
882 case CONS_GETINFO: /* get current (virtual) console info */
884 vid_info_t *ptr = (vid_info_t*)data;
885 if (ptr->size == sizeof(struct vid_info)) {
886 ptr->m_num = sc->cur_scp->index;
887 ptr->font_size = scp->font_size;
888 ptr->mv_col = scp->xpos;
889 ptr->mv_row = scp->ypos;
890 ptr->mv_csz = scp->xsize;
891 ptr->mv_rsz = scp->ysize;
892 ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
894 * The following fields are filled by the terminal emulator. XXX
901 ptr->mv_grfc.fore = 0; /* not supported */
902 ptr->mv_grfc.back = 0; /* not supported */
903 ptr->mv_ovscan = scp->border;
904 if (scp == sc->cur_scp)
906 ptr->mk_keylock = scp->status & LOCK_MASK;
912 case CONS_GETVERS: /* get version number */
913 *(int*)data = 0x200; /* version 2.0 */
916 case CONS_IDLE: /* see if the screen has been idle */
918 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
919 * the user process may have been writing something on the
920 * screen and syscons is not aware of it. Declare the screen
921 * is NOT idle if it is in one of these modes. But there is
922 * an exception to it; if a screen saver is running in the
923 * graphics mode in the current screen, we should say that the
924 * screen has been idle.
926 *(int *)data = (sc->flags & SC_SCRN_IDLE)
927 && (!ISGRAPHSC(sc->cur_scp)
928 || (sc->cur_scp->status & SAVER_RUNNING));
931 case CONS_SAVERMODE: /* set saver mode */
932 switch(*(int *)data) {
935 /* if a LKM screen saver is running, stop it first. */
936 scsplash_stick(FALSE);
937 saver_mode = *(int *)data;
940 if ((error = wait_scrn_saver_stop(NULL))) {
945 run_scrn_saver = TRUE;
946 if (saver_mode == CONS_USR_SAVER)
947 scp->status |= SAVER_RUNNING;
949 scp->status &= ~SAVER_RUNNING;
950 scsplash_stick(TRUE);
955 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
956 scp->status &= ~SAVER_RUNNING;
957 saver_mode = *(int *)data;
965 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
967 * Note that this ioctl does not guarantee the screen saver
968 * actually starts or stops. It merely attempts to do so...
971 run_scrn_saver = (*(int *)data != 0);
973 sc->scrn_time_stamp -= scrn_blank_time;
977 case CONS_SCRSHOT: /* get a screen shot */
979 int retval, hist_rsz;
981 vm_offset_t frbp, hstp;
983 scrshot_t *ptr = (scrshot_t *)data;
984 void *outp = ptr->buf;
986 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
989 if (ISGRAPHSC(scp)) {
993 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
994 if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
995 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1000 lsize = scp->xsize * sizeof(u_int16_t);
1001 csize = ptr->xsize * sizeof(u_int16_t);
1002 /* Pointer to the last line of framebuffer */
1003 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
1005 /* Pointer to the last line of target buffer */
1006 outp = (char *)outp + ptr->ysize * csize;
1007 /* Pointer to the last line of history buffer */
1008 if (scp->history != NULL)
1009 hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
1010 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
1015 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1016 if (lnum < scp->ysize) {
1020 if (hstp < scp->history->vtb_buffer)
1021 hstp += scp->history->vtb_rows * lsize;
1026 outp = (char *)outp - csize;
1027 retval = copyout((void *)frbp, outp, csize);
1035 case VT_SETMODE: /* set screen switcher mode */
1037 struct vt_mode *mode;
1040 mode = (struct vt_mode *)data;
1041 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1042 if (scp->smode.mode == VT_PROCESS) {
1043 p1 = pfind(scp->pid);
1044 if (scp->proc == p1 && scp->proc != td->td_proc) {
1047 DPRINTF(5, ("error EPERM\n"));
1054 if (mode->mode == VT_AUTO) {
1055 scp->smode.mode = VT_AUTO;
1058 DPRINTF(5, ("VT_AUTO, "));
1059 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1060 cnavailable(sc_consptr, TRUE);
1061 /* were we in the middle of the vty switching process? */
1062 if (finish_vt_rel(scp, TRUE, &s) == 0)
1063 DPRINTF(5, ("reset WAIT_REL, "));
1064 if (finish_vt_acq(scp) == 0)
1065 DPRINTF(5, ("reset WAIT_ACQ, "));
1067 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1068 || !ISSIGVALID(mode->frsig)) {
1070 DPRINTF(5, ("error EINVAL\n"));
1073 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1074 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1075 scp->proc = td->td_proc;
1076 scp->pid = scp->proc->p_pid;
1077 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1078 cnavailable(sc_consptr, FALSE);
1085 case VT_GETMODE: /* get screen switcher mode */
1086 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1089 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1090 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1092 ival = IOCPARM_IVAL(data);
1093 data = (caddr_t)&ival;
1096 case VT_RELDISP: /* screen switcher ioctl */
1099 * This must be the current vty which is in the VT_PROCESS
1102 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1106 /* ...and this process is controlling it. */
1107 if (scp->proc != td->td_proc) {
1112 switch(*(int *)data) {
1113 case VT_FALSE: /* user refuses to release screen, abort */
1114 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1115 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1117 case VT_TRUE: /* user has released screen, go on */
1118 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1119 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1121 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1122 if ((error = finish_vt_acq(scp)) == 0)
1123 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1131 case VT_OPENQRY: /* return free virtual console */
1132 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1134 if (!tty_opened(tp)) {
1135 *(int *)data = i + 1;
1141 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1142 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1144 ival = IOCPARM_IVAL(data);
1145 data = (caddr_t)&ival;
1148 case VT_ACTIVATE: /* switch to screen *data */
1149 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1151 error = sc_clean_up(sc->cur_scp);
1155 error = sc_switch_scr(sc, i);
1158 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1159 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1161 ival = IOCPARM_IVAL(data);
1162 data = (caddr_t)&ival;
1165 case VT_WAITACTIVE: /* wait for switch to occur */
1166 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1167 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1169 if (i == sc->cur_scp->index)
1171 error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1174 case VT_GETACTIVE: /* get active vty # */
1175 *(int *)data = sc->cur_scp->index + 1;
1178 case VT_GETINDEX: /* get this vty # */
1179 *(int *)data = scp->index + 1;
1182 case VT_LOCKSWITCH: /* prevent vty switching */
1183 if ((*(int *)data) & 0x01)
1184 sc->flags |= SC_SCRN_VTYLOCK;
1186 sc->flags &= ~SC_SCRN_VTYLOCK;
1189 case KDENABIO: /* allow io operations */
1190 error = priv_check(td, PRIV_IO);
1193 error = securelevel_gt(td->td_ucred, 0);
1197 td->td_frame->tf_eflags |= PSL_IOPL;
1198 #elif defined(__amd64__)
1199 td->td_frame->tf_rflags |= PSL_IOPL;
1203 case KDDISABIO: /* disallow io operations (default) */
1205 td->td_frame->tf_eflags &= ~PSL_IOPL;
1206 #elif defined(__amd64__)
1207 td->td_frame->tf_rflags &= ~PSL_IOPL;
1211 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1212 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1214 ival = IOCPARM_IVAL(data);
1215 data = (caddr_t)&ival;
1218 case KDSKBSTATE: /* set keyboard state (locks) */
1219 if (*(int *)data & ~LOCK_MASK)
1221 scp->status &= ~LOCK_MASK;
1222 scp->status |= *(int *)data;
1223 if (scp == sc->cur_scp)
1224 update_kbd_state(scp, scp->status, LOCK_MASK);
1227 case KDGKBSTATE: /* get keyboard state (locks) */
1228 if (scp == sc->cur_scp)
1229 save_kbd_state(scp);
1230 *(int *)data = scp->status & LOCK_MASK;
1233 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1234 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1235 error = kbdd_ioctl(sc->kbd, cmd, data);
1236 if (error == ENOIOCTL)
1240 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1241 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1243 ival = IOCPARM_IVAL(data);
1244 data = (caddr_t)&ival;
1247 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1248 if (*(int *)data & ~0x7f)
1250 error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1251 if (error == ENOIOCTL)
1255 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1256 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1258 ival = IOCPARM_IVAL(data);
1259 data = (caddr_t)&ival;
1262 case KDSKBMODE: /* set keyboard mode */
1263 switch (*(int *)data) {
1264 case K_XLATE: /* switch to XLT ascii mode */
1265 case K_RAW: /* switch to RAW scancode mode */
1266 case K_CODE: /* switch to CODE mode */
1267 scp->kbd_mode = *(int *)data;
1268 if (scp == sc->cur_scp)
1269 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1276 case KDGKBMODE: /* get keyboard mode */
1277 *(int *)data = scp->kbd_mode;
1281 error = kbdd_ioctl(sc->kbd, cmd, data);
1282 if (error == ENOIOCTL)
1286 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1287 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1289 ival = IOCPARM_IVAL(data);
1290 data = (caddr_t)&ival;
1293 case KDMKTONE: /* sound the bell */
1295 sc_bell(scp, (*(int*)data)&0xffff,
1296 (((*(int*)data)>>16)&0xffff)*hz/1000);
1298 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1301 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1302 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1304 ival = IOCPARM_IVAL(data);
1305 data = (caddr_t)&ival;
1308 case KIOCSOUND: /* make tone (*data) hz */
1309 if (scp == sc->cur_scp) {
1311 return sc_tone(*(int *)data);
1317 case KDGKBTYPE: /* get keyboard type */
1318 error = kbdd_ioctl(sc->kbd, cmd, data);
1319 if (error == ENOIOCTL) {
1320 /* always return something? XXX */
1325 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1326 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1328 ival = IOCPARM_IVAL(data);
1329 data = (caddr_t)&ival;
1332 case KDSETLED: /* set keyboard LED status */
1333 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1335 scp->status &= ~LED_MASK;
1336 scp->status |= *(int *)data;
1337 if (scp == sc->cur_scp)
1338 update_kbd_leds(scp, scp->status);
1341 case KDGETLED: /* get keyboard LED status */
1342 if (scp == sc->cur_scp)
1343 save_kbd_state(scp);
1344 *(int *)data = scp->status & LED_MASK;
1347 case KBADDKBD: /* add/remove keyboard to/from mux */
1349 error = kbdd_ioctl(sc->kbd, cmd, data);
1350 if (error == ENOIOCTL)
1354 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1355 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1357 ival = IOCPARM_IVAL(data);
1358 data = (caddr_t)&ival;
1361 case CONS_SETKBD: /* set the new keyboard */
1366 newkbd = kbd_get_keyboard(*(int *)data);
1367 if (newkbd == NULL) {
1372 if (sc->kbd != newkbd) {
1373 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1374 (void *)&sc->keyboard, sckbdevent, sc);
1375 /* i == newkbd->kb_index */
1377 if (sc->kbd != NULL) {
1378 save_kbd_state(sc->cur_scp);
1379 kbd_release(sc->kbd, (void *)&sc->keyboard);
1381 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1383 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1384 (caddr_t)&sc->cur_scp->kbd_mode);
1385 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1388 error = EPERM; /* XXX */
1395 case CONS_RELKBD: /* release the current keyboard */
1398 if (sc->kbd != NULL) {
1399 save_kbd_state(sc->cur_scp);
1400 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1409 case CONS_GETTERM: /* get the current terminal emulator info */
1413 if (((term_info_t *)data)->ti_index == 0) {
1416 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1419 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1420 sizeof(((term_info_t *)data)->ti_name));
1421 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1422 sizeof(((term_info_t *)data)->ti_desc));
1423 ((term_info_t *)data)->ti_flags = 0;
1426 ((term_info_t *)data)->ti_name[0] = '\0';
1427 ((term_info_t *)data)->ti_desc[0] = '\0';
1428 ((term_info_t *)data)->ti_flags = 0;
1433 case CONS_SETTERM: /* set the current terminal emulator */
1435 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1436 /* FIXME: what if scp == sc_console! XXX */
1440 case GIO_SCRNMAP: /* get output translation table */
1441 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1444 case PIO_SCRNMAP: /* set output translation table */
1445 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1446 for (i=0; i<sizeof(sc->scr_map); i++) {
1447 sc->scr_rmap[sc->scr_map[i]] = i;
1451 case GIO_KEYMAP: /* get keyboard translation table */
1452 case PIO_KEYMAP: /* set keyboard translation table */
1453 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
1454 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1455 case GIO_DEADKEYMAP: /* get accent key translation table */
1456 case PIO_DEADKEYMAP: /* set accent key translation table */
1457 case GETFKEY: /* get function key string */
1458 case SETFKEY: /* set function key string */
1459 error = kbdd_ioctl(sc->kbd, cmd, data);
1460 if (error == ENOIOCTL)
1464 #ifndef SC_NO_FONT_LOADING
1466 case PIO_FONT8x8: /* set 8x8 dot font */
1467 if (!ISFONTAVAIL(sc->adp->va_flags))
1469 bcopy(data, sc->font_8, 8*256);
1470 sc->fonts_loaded |= FONT_8;
1473 * Always use the font page #0. XXX
1474 * Don't load if the current font size is not 8x8.
1476 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1477 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1480 case GIO_FONT8x8: /* get 8x8 dot font */
1481 if (!ISFONTAVAIL(sc->adp->va_flags))
1483 if (sc->fonts_loaded & FONT_8) {
1484 bcopy(sc->font_8, data, 8*256);
1490 case PIO_FONT8x14: /* set 8x14 dot font */
1491 if (!ISFONTAVAIL(sc->adp->va_flags))
1493 bcopy(data, sc->font_14, 14*256);
1494 sc->fonts_loaded |= FONT_14;
1497 * Always use the font page #0. XXX
1498 * Don't load if the current font size is not 8x14.
1500 if (ISTEXTSC(sc->cur_scp)
1501 && (sc->cur_scp->font_size >= 14)
1502 && (sc->cur_scp->font_size < 16))
1503 sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1506 case GIO_FONT8x14: /* get 8x14 dot font */
1507 if (!ISFONTAVAIL(sc->adp->va_flags))
1509 if (sc->fonts_loaded & FONT_14) {
1510 bcopy(sc->font_14, data, 14*256);
1516 case PIO_FONT8x16: /* set 8x16 dot font */
1517 if (!ISFONTAVAIL(sc->adp->va_flags))
1519 bcopy(data, sc->font_16, 16*256);
1520 sc->fonts_loaded |= FONT_16;
1523 * Always use the font page #0. XXX
1524 * Don't load if the current font size is not 8x16.
1526 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1527 sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1530 case GIO_FONT8x16: /* get 8x16 dot font */
1531 if (!ISFONTAVAIL(sc->adp->va_flags))
1533 if (sc->fonts_loaded & FONT_16) {
1534 bcopy(sc->font_16, data, 16*256);
1540 #endif /* SC_NO_FONT_LOADING */
1550 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1554 return sctty_ioctl(dev->si_drv1, cmd, data, td);
1558 sc_cnprobe(struct consdev *cp)
1563 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1565 /* a video card is always required */
1566 if (!scvidprobe(unit, flags, TRUE))
1567 cp->cn_pri = CN_DEAD;
1569 /* syscons will become console even when there is no keyboard */
1570 sckbdprobe(unit, flags, TRUE);
1572 if (cp->cn_pri == CN_DEAD)
1575 /* initialize required fields */
1576 strcpy(cp->cn_name, "ttyv0");
1580 sc_cninit(struct consdev *cp)
1585 sc_get_cons_priority(&unit, &flags);
1586 scinit(unit, flags | SC_KERNEL_CONSOLE);
1587 sc_console_unit = unit;
1588 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1593 sc_cnterm(struct consdev *cp)
1595 /* we are not the kernel console any more, release everything */
1597 if (sc_console_unit < 0)
1598 return; /* shouldn't happen */
1601 sc_clear_screen(sc_console);
1602 sccnupdate(sc_console);
1605 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1606 sc_console_unit = -1;
1611 sc_cnputc(struct consdev *cd, int c)
1614 scr_stat *scp = sc_console;
1615 #ifndef SC_NO_HISTORY
1619 #endif /* !SC_NO_HISTORY */
1622 /* assert(sc_console != NULL) */
1624 #ifndef SC_NO_HISTORY
1625 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1626 scp->status &= ~SLKED;
1627 update_kbd_state(scp, scp->status, SLKED);
1628 if (scp->status & BUFFER_SAVED) {
1629 if (!sc_hist_restore(scp))
1630 sc_remove_cutmarking(scp);
1631 scp->status &= ~BUFFER_SAVED;
1632 scp->status |= CURSOR_ENABLED;
1633 sc_draw_cursor_image(scp);
1637 * XXX: Now that TTY's have their own locks, we cannot process
1638 * any data after disabling scroll lock. cnputs already holds a
1641 tp = SC_DEV(scp->sc, scp->index);
1644 sctty_outwakeup(tp);
1648 #endif /* !SC_NO_HISTORY */
1651 sc_puts(scp, buf, 1, 1);
1653 s = spltty(); /* block sckbdevent and scrn_timer */
1659 sc_cngetc(struct consdev *cd)
1661 static struct fkeytab fkey;
1666 int s = spltty(); /* block sckbdevent and scrn_timer while we poll */
1669 /* assert(sc_console != NULL) */
1672 * Stop the screen saver and update the screen if necessary.
1673 * What if we have been running in the screen saver code... XXX
1675 sc_touch_scrn_saver();
1676 scp = sc_console->sc->cur_scp; /* XXX */
1679 if (fkeycp < fkey.len) {
1681 return fkey.str[fkeycp++];
1684 if (scp->sc->kbd == NULL) {
1690 * Make sure the keyboard is accessible even when the kbd device
1691 * driver is disabled.
1693 kbdd_enable(scp->sc->kbd);
1695 /* we shall always use the keyboard in the XLATE mode here */
1696 cur_mode = scp->kbd_mode;
1697 scp->kbd_mode = K_XLATE;
1698 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1700 kbdd_poll(scp->sc->kbd, TRUE);
1701 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK);
1702 kbdd_poll(scp->sc->kbd, FALSE);
1704 scp->kbd_mode = cur_mode;
1705 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1706 kbdd_disable(scp->sc->kbd);
1709 switch (KEYFLAGS(c)) {
1710 case 0: /* normal char */
1712 case FKEY: /* function key */
1713 p = (*scp->tsw->te_fkeystr)(scp, c);
1715 fkey.len = strlen(p);
1716 bcopy(p, fkey.str, fkey.len);
1720 p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1722 if ((p != NULL) && (fkey.len > 0)) {
1723 bcopy(p, fkey.str, fkey.len);
1737 sccnupdate(scr_stat *scp)
1739 /* this is a cut-down version of scrn_timer()... */
1741 if (suspend_in_progress || scp->sc->font_loading_in_progress)
1744 if (debugger > 0 || panicstr || shutdown_in_progress) {
1745 sc_touch_scrn_saver();
1746 } else if (scp != scp->sc->cur_scp) {
1750 if (!run_scrn_saver)
1751 scp->sc->flags &= ~SC_SCRN_IDLE;
1753 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1754 if (scp->sc->flags & SC_SCRN_BLANKED)
1755 stop_scrn_saver(scp->sc, current_saver);
1758 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1759 || scp->sc->switch_in_progress)
1762 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1763 * when write_in_progress is non-zero. XXX
1766 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1767 scrn_update(scp, TRUE);
1771 scrn_timer(void *arg)
1774 static int kbd_interval = 0;
1782 again = (arg != NULL);
1784 sc = (sc_softc_t *)arg;
1785 else if (sc_console != NULL)
1786 sc = sc_console->sc;
1790 /* don't do anything when we are performing some I/O operations */
1791 if (suspend_in_progress || sc->font_loading_in_progress) {
1793 timeout(scrn_timer, sc, hz / 10);
1799 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1800 /* try to allocate a keyboard automatically */
1801 if (++kbd_interval >= 25) {
1802 sc->keyboard = sc_allocate_keyboard(sc, -1);
1803 if (sc->keyboard >= 0) {
1804 sc->kbd = kbd_get_keyboard(sc->keyboard);
1805 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1806 (caddr_t)&sc->cur_scp->kbd_mode);
1807 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1815 /* find the vty to update */
1818 /* should we stop the screen saver? */
1819 getmicrouptime(&tv);
1820 if (debugger > 0 || panicstr || shutdown_in_progress)
1821 sc_touch_scrn_saver();
1822 if (run_scrn_saver) {
1823 if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1824 sc->flags |= SC_SCRN_IDLE;
1826 sc->flags &= ~SC_SCRN_IDLE;
1828 sc->scrn_time_stamp = tv.tv_sec;
1829 sc->flags &= ~SC_SCRN_IDLE;
1830 if (scrn_blank_time > 0)
1831 run_scrn_saver = TRUE;
1834 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1835 if (sc->flags & SC_SCRN_BLANKED)
1836 stop_scrn_saver(sc, current_saver);
1839 /* should we just return ? */
1840 if (sc->blink_in_progress || sc->switch_in_progress
1841 || sc->write_in_progress) {
1843 timeout(scrn_timer, sc, hz / 10);
1848 /* Update the screen */
1849 scp = sc->cur_scp; /* cur_scp may have changed... */
1850 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1851 scrn_update(scp, TRUE);
1854 /* should we activate the screen saver? */
1855 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1856 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1857 (*current_saver)(sc, TRUE);
1861 timeout(scrn_timer, sc, hz / 25);
1866 and_region(int *s1, int *e1, int s2, int e2)
1868 if (*e1 < s2 || e2 < *s1)
1870 *s1 = imax(*s1, s2);
1871 *e1 = imin(*e1, e2);
1876 scrn_update(scr_stat *scp, int show_cursor)
1883 /* assert(scp == scp->sc->cur_scp) */
1885 SC_VIDEO_LOCK(scp->sc);
1887 #ifndef SC_NO_CUTPASTE
1888 /* remove the previous mouse pointer image if necessary */
1889 if (scp->status & MOUSE_VISIBLE) {
1891 e = scp->mouse_pos + scp->xsize + 1;
1892 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1893 || and_region(&s, &e, scp->start, scp->end)
1894 || ((scp->status & CURSOR_ENABLED) &&
1895 (scp->cursor_pos != scp->cursor_oldpos) &&
1896 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1897 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1898 sc_remove_mouse_image(scp);
1899 if (scp->end >= scp->xsize*scp->ysize)
1900 scp->end = scp->xsize*scp->ysize - 1;
1903 #endif /* !SC_NO_CUTPASTE */
1907 if (scp->end >= scp->xsize*scp->ysize) {
1908 printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1909 scp->end = scp->xsize*scp->ysize - 1;
1911 if (scp->start < 0) {
1912 printf("scrn_update(): scp->start %d < 0\n", scp->start);
1917 /* update screen image */
1918 if (scp->start <= scp->end) {
1919 if (scp->mouse_cut_end >= 0) {
1920 /* there is a marked region for cut & paste */
1921 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1922 start = scp->mouse_cut_start;
1923 end = scp->mouse_cut_end;
1925 start = scp->mouse_cut_end;
1926 end = scp->mouse_cut_start - 1;
1930 /* does the cut-mark region overlap with the update region? */
1931 if (and_region(&s, &e, scp->start, scp->end)) {
1932 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1935 if (and_region(&s, &e, scp->start, scp->end))
1936 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1938 e = scp->xsize*scp->ysize - 1;
1939 if (and_region(&s, &e, scp->start, scp->end))
1940 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1942 (*scp->rndr->draw)(scp, scp->start,
1943 scp->end - scp->start + 1, FALSE);
1946 (*scp->rndr->draw)(scp, scp->start,
1947 scp->end - scp->start + 1, FALSE);
1951 /* we are not to show the cursor and the mouse pointer... */
1954 scp->start = scp->xsize*scp->ysize - 1;
1955 SC_VIDEO_UNLOCK(scp->sc);
1959 /* update cursor image */
1960 if (scp->status & CURSOR_ENABLED) {
1963 /* did cursor move since last time ? */
1964 if (scp->cursor_pos != scp->cursor_oldpos) {
1965 /* do we need to remove old cursor image ? */
1966 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1967 sc_remove_cursor_image(scp);
1968 sc_draw_cursor_image(scp);
1970 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1971 /* cursor didn't move, but has been overwritten */
1972 sc_draw_cursor_image(scp);
1973 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
1974 /* if it's a blinking cursor, update it */
1975 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1976 sc_inside_cutmark(scp,
1981 #ifndef SC_NO_CUTPASTE
1982 /* update "pseudo" mouse pointer image */
1983 if (scp->sc->flags & SC_MOUSE_ENABLED) {
1984 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1985 scp->status &= ~MOUSE_MOVED;
1986 sc_draw_mouse_image(scp);
1989 #endif /* SC_NO_CUTPASTE */
1992 scp->start = scp->xsize*scp->ysize - 1;
1994 SC_VIDEO_UNLOCK(scp->sc);
1999 scsplash_callback(int event, void *arg)
2004 sc = (sc_softc_t *)arg;
2008 if (add_scrn_saver(scsplash_saver) == 0) {
2009 sc->flags &= ~SC_SAVER_FAILED;
2010 run_scrn_saver = TRUE;
2011 if (cold && !(boothowto & RB_VERBOSE)) {
2012 scsplash_stick(TRUE);
2013 (*current_saver)(sc, TRUE);
2019 if (current_saver == scsplash_saver) {
2020 scsplash_stick(FALSE);
2021 error = remove_scrn_saver(scsplash_saver);
2033 scsplash_saver(sc_softc_t *sc, int show)
2035 static int busy = FALSE;
2044 if (!(sc->flags & SC_SAVER_FAILED)) {
2045 if (!(sc->flags & SC_SCRN_BLANKED))
2046 set_scrn_saver_mode(scp, -1, NULL, 0);
2047 switch (splash(sc->adp, TRUE)) {
2048 case 0: /* succeeded */
2050 case EAGAIN: /* try later */
2051 restore_scrn_saver_mode(scp, FALSE);
2052 sc_touch_scrn_saver(); /* XXX */
2055 sc->flags |= SC_SAVER_FAILED;
2056 scsplash_stick(FALSE);
2057 restore_scrn_saver_mode(scp, TRUE);
2058 printf("scsplash_saver(): failed to put up the image\n");
2062 } else if (!sticky_splash) {
2063 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2064 restore_scrn_saver_mode(scp, TRUE);
2070 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2075 if (current_saver != none_saver) {
2076 error = remove_scrn_saver(current_saver);
2081 if (current_saver != none_saver)
2084 run_scrn_saver = FALSE;
2085 saver_mode = CONS_LKM_SAVER;
2086 current_saver = this_saver;
2091 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2093 if (current_saver != this_saver)
2098 * In order to prevent `current_saver' from being called by
2099 * the timeout routine `scrn_timer()' while we manipulate
2100 * the saver list, we shall set `current_saver' to `none_saver'
2101 * before stopping the current saver, rather than blocking by `splXX()'.
2103 current_saver = none_saver;
2105 stop_scrn_saver(this_saver);
2108 /* unblank all blanked screens */
2109 wait_scrn_saver_stop(NULL);
2113 current_saver = none_saver;
2118 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2122 /* assert(scp == scp->sc->cur_scp) */
2124 if (!ISGRAPHSC(scp))
2125 sc_remove_cursor_image(scp);
2126 scp->splash_save_mode = scp->mode;
2127 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2128 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2129 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2130 scp->sc->flags |= SC_SCRN_BLANKED;
2136 if (set_mode(scp) == 0) {
2137 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2138 scp->status |= GRAPHICS_MODE;
2139 #ifndef SC_NO_PALETTE_LOADING
2141 vidd_load_palette(scp->sc->adp, pal);
2143 sc_set_border(scp, border);
2147 scp->mode = scp->splash_save_mode;
2148 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2149 scp->status |= scp->splash_save_status;
2156 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2162 /* assert(scp == scp->sc->cur_scp) */
2165 status = scp->status;
2166 scp->mode = scp->splash_save_mode;
2167 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2168 scp->status |= scp->splash_save_status;
2169 scp->sc->flags &= ~SC_SCRN_BLANKED;
2171 if (!ISGRAPHSC(scp))
2172 sc_draw_cursor_image(scp);
2177 if (set_mode(scp) == 0) {
2178 #ifndef SC_NO_PALETTE_LOADING
2179 #ifdef SC_PIXEL_MODE
2180 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2181 vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2184 vidd_load_palette(scp->sc->adp, scp->sc->palette);
2191 scp->status = status;
2198 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2200 (*saver)(sc, FALSE);
2201 run_scrn_saver = FALSE;
2202 /* the screen saver may have chosen not to stop after all... */
2203 if (sc->flags & SC_SCRN_BLANKED)
2206 mark_all(sc->cur_scp);
2207 if (sc->delayed_next_scr)
2208 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2210 wakeup(&scrn_blanked);
2214 wait_scrn_saver_stop(sc_softc_t *sc)
2218 while (scrn_blanked > 0) {
2219 run_scrn_saver = FALSE;
2220 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2224 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2225 if ((error != 0) && (error != ERESTART))
2228 run_scrn_saver = FALSE;
2231 #endif /* DEV_SPLASH */
2234 sc_touch_scrn_saver(void)
2236 scsplash_stick(FALSE);
2237 run_scrn_saver = FALSE;
2241 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2248 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2250 if (sc->cur_scp == NULL)
2253 /* prevent switch if previously requested */
2254 if (sc->flags & SC_SCRN_VTYLOCK) {
2255 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2256 sc->cur_scp->bell_duration);
2260 /* delay switch if the screen is blanked or being updated */
2261 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2262 || sc->blink_in_progress) {
2263 sc->delayed_next_scr = next_scr + 1;
2264 sc_touch_scrn_saver();
2265 DPRINTF(5, ("switch delayed\n"));
2268 sc->delayed_next_scr = 0;
2271 cur_scp = sc->cur_scp;
2273 /* we are in the middle of the vty switching process... */
2274 if (sc->switch_in_progress
2275 && (cur_scp->smode.mode == VT_PROCESS)
2277 p = pfind(cur_scp->pid);
2278 if (cur_scp->proc != p) {
2282 * The controlling process has died!!. Do some clean up.
2283 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2284 * are not reset here yet; they will be cleared later.
2286 DPRINTF(5, ("cur_scp controlling process %d died, ",
2288 if (cur_scp->status & SWITCH_WAIT_REL) {
2290 * Force the previous switch to finish, but return now
2293 DPRINTF(5, ("reset WAIT_REL, "));
2294 finish_vt_rel(cur_scp, TRUE, &s);
2296 DPRINTF(5, ("finishing previous switch\n"));
2298 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2299 /* let's assume screen switch has been completed. */
2300 DPRINTF(5, ("reset WAIT_ACQ, "));
2301 finish_vt_acq(cur_scp);
2304 * We are in between screen release and acquisition, and
2305 * reached here via scgetc() or scrn_timer() which has
2306 * interrupted exchange_scr(). Don't do anything stupid.
2308 DPRINTF(5, ("waiting nothing, "));
2314 * The controlling process is alive, but not responding...
2315 * It is either buggy or it may be just taking time.
2316 * The following code is a gross kludge to cope with this
2317 * problem for which there is no clean solution. XXX
2319 if (cur_scp->status & SWITCH_WAIT_REL) {
2320 switch (sc->switch_in_progress++) {
2324 DPRINTF(5, ("sending relsig again, "));
2325 signal_vt_rel(cur_scp);
2332 * Act as if the controlling program returned
2335 DPRINTF(5, ("force reset WAIT_REL, "));
2336 finish_vt_rel(cur_scp, FALSE, &s);
2338 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2341 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2342 switch (sc->switch_in_progress++) {
2346 DPRINTF(5, ("sending acqsig again, "));
2347 signal_vt_acq(cur_scp);
2353 /* clear the flag and finish the previous switch */
2354 DPRINTF(5, ("force reset WAIT_ACQ, "));
2355 finish_vt_acq(cur_scp);
2363 * Return error if an invalid argument is given, or vty switch
2364 * is still in progress.
2366 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2367 || sc->switch_in_progress) {
2369 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2370 DPRINTF(5, ("error 1\n"));
2375 * Don't allow switching away from the graphics mode vty
2376 * if the switch mode is VT_AUTO, unless the next vty is the same
2377 * as the current or the current vty has been closed (but showing).
2379 tp = SC_DEV(sc, cur_scp->index);
2380 if ((cur_scp->index != next_scr)
2382 && (cur_scp->smode.mode == VT_AUTO)
2383 && ISGRAPHSC(cur_scp)) {
2385 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2386 DPRINTF(5, ("error, graphics mode\n"));
2391 * Is the wanted vty open? Don't allow switching to a closed vty.
2392 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2393 * Note that we always allow the user to switch to the kernel
2394 * console even if it is closed.
2396 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2397 tp = SC_DEV(sc, next_scr);
2398 if (!tty_opened(tp)) {
2400 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2401 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2404 if ((debugger > 0) && (SC_STAT(tp)->smode.mode == VT_PROCESS)) {
2406 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2411 /* this is the start of vty switching process... */
2412 ++sc->switch_in_progress;
2413 sc->old_scp = cur_scp;
2414 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2415 if (sc->new_scp == sc->old_scp) {
2416 sc->switch_in_progress = 0;
2418 * XXX wakeup() locks the scheduler lock which will hang if
2419 * the lock is in an in-between state, e.g., when we stop at
2420 * a breakpoint at fork_exit. It has always been wrong to call
2421 * wakeup() when the debugger is active. In RELENG_4, wakeup()
2422 * is supposed to be locked by splhigh(), but the debugger may
2423 * be invoked at splhigh().
2426 wakeup(VTY_WCHAN(sc,next_scr));
2428 DPRINTF(5, ("switch done (new == old)\n"));
2432 /* has controlling process died? */
2433 vt_proc_alive(sc->old_scp);
2434 vt_proc_alive(sc->new_scp);
2436 /* wait for the controlling process to release the screen, if necessary */
2437 if (signal_vt_rel(sc->old_scp)) {
2442 /* go set up the new vty screen */
2447 /* wake up processes waiting for this vty */
2449 wakeup(VTY_WCHAN(sc,next_scr));
2451 /* wait for the controlling process to acknowledge, if necessary */
2452 if (signal_vt_acq(sc->cur_scp)) {
2457 sc->switch_in_progress = 0;
2458 if (sc->unit == sc_console_unit)
2459 cnavailable(sc_consptr, TRUE);
2461 DPRINTF(5, ("switch done\n"));
2467 do_switch_scr(sc_softc_t *sc, int s)
2469 vt_proc_alive(sc->new_scp);
2474 /* sc->cur_scp == sc->new_scp */
2475 wakeup(VTY_WCHAN(sc,sc->cur_scp->index));
2477 /* wait for the controlling process to acknowledge, if necessary */
2478 if (!signal_vt_acq(sc->cur_scp)) {
2479 sc->switch_in_progress = 0;
2480 if (sc->unit == sc_console_unit)
2481 cnavailable(sc_consptr, TRUE);
2488 vt_proc_alive(scr_stat *scp)
2493 if ((p = pfind(scp->pid)) != NULL)
2498 scp->smode.mode = VT_AUTO;
2499 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2505 signal_vt_rel(scr_stat *scp)
2507 if (scp->smode.mode != VT_PROCESS)
2509 scp->status |= SWITCH_WAIT_REL;
2510 PROC_LOCK(scp->proc);
2511 kern_psignal(scp->proc, scp->smode.relsig);
2512 PROC_UNLOCK(scp->proc);
2513 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2518 signal_vt_acq(scr_stat *scp)
2520 if (scp->smode.mode != VT_PROCESS)
2522 if (scp->sc->unit == sc_console_unit)
2523 cnavailable(sc_consptr, FALSE);
2524 scp->status |= SWITCH_WAIT_ACQ;
2525 PROC_LOCK(scp->proc);
2526 kern_psignal(scp->proc, scp->smode.acqsig);
2527 PROC_UNLOCK(scp->proc);
2528 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2533 finish_vt_rel(scr_stat *scp, int release, int *s)
2535 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2536 scp->status &= ~SWITCH_WAIT_REL;
2538 *s = do_switch_scr(scp->sc, *s);
2540 scp->sc->switch_in_progress = 0;
2547 finish_vt_acq(scr_stat *scp)
2549 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2550 scp->status &= ~SWITCH_WAIT_ACQ;
2551 scp->sc->switch_in_progress = 0;
2558 exchange_scr(sc_softc_t *sc)
2562 /* save the current state of video and keyboard */
2563 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2564 if (!ISGRAPHSC(sc->old_scp))
2565 sc_remove_cursor_image(sc->old_scp);
2566 if (sc->old_scp->kbd_mode == K_XLATE)
2567 save_kbd_state(sc->old_scp);
2569 /* set up the video for the new screen */
2570 scp = sc->cur_scp = sc->new_scp;
2572 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp))
2574 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2579 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2580 (void *)sc->adp->va_window, FALSE);
2582 scp->status |= MOUSE_HIDDEN;
2583 sc_move_cursor(scp, scp->xpos, scp->ypos);
2584 if (!ISGRAPHSC(scp))
2585 sc_set_cursor_image(scp);
2586 #ifndef SC_NO_PALETTE_LOADING
2587 if (ISGRAPHSC(sc->old_scp)) {
2588 #ifdef SC_PIXEL_MODE
2589 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2590 vidd_load_palette(sc->adp, sc->palette2);
2593 vidd_load_palette(sc->adp, sc->palette);
2596 sc_set_border(scp, scp->border);
2598 /* set up the keyboard for the new screen */
2599 if (sc->old_scp->kbd_mode != scp->kbd_mode)
2600 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2601 update_kbd_state(scp, scp->status, LOCK_MASK);
2607 sc_puts(scr_stat *scp, u_char *buf, int len, int kernel)
2609 int need_unlock = 0;
2612 /* make screensaver happy */
2613 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2614 run_scrn_saver = FALSE;
2618 if (!kdb_active && !mtx_owned(&scp->scr_lock)) {
2620 mtx_lock_spin(&scp->scr_lock);
2622 (*scp->tsw->te_puts)(scp, buf, len, kernel);
2624 mtx_unlock_spin(&scp->scr_lock);
2627 if (scp->sc->delayed_next_scr)
2628 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2632 sc_draw_cursor_image(scr_stat *scp)
2634 /* assert(scp == scp->sc->cur_scp); */
2635 SC_VIDEO_LOCK(scp->sc);
2636 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2637 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2638 sc_inside_cutmark(scp, scp->cursor_pos));
2639 scp->cursor_oldpos = scp->cursor_pos;
2640 SC_VIDEO_UNLOCK(scp->sc);
2644 sc_remove_cursor_image(scr_stat *scp)
2646 /* assert(scp == scp->sc->cur_scp); */
2647 SC_VIDEO_LOCK(scp->sc);
2648 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2649 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2650 sc_inside_cutmark(scp, scp->cursor_oldpos));
2651 SC_VIDEO_UNLOCK(scp->sc);
2655 update_cursor_image(scr_stat *scp)
2657 /* assert(scp == scp->sc->cur_scp); */
2658 sc_remove_cursor_image(scp);
2659 sc_set_cursor_image(scp);
2660 sc_draw_cursor_image(scp);
2664 sc_set_cursor_image(scr_stat *scp)
2666 scp->curs_attr.flags = scp->curr_curs_attr.flags;
2667 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2668 /* hidden cursor is internally represented as zero-height underline */
2669 scp->curs_attr.flags = CONS_CHAR_CURSOR;
2670 scp->curs_attr.base = scp->curs_attr.height = 0;
2671 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2672 scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2673 scp->font_size - 1);
2674 scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2675 scp->font_size - scp->curs_attr.base);
2676 } else { /* block cursor */
2677 scp->curs_attr.base = 0;
2678 scp->curs_attr.height = scp->font_size;
2681 /* assert(scp == scp->sc->cur_scp); */
2682 SC_VIDEO_LOCK(scp->sc);
2683 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2684 scp->curs_attr.flags & CONS_BLINK_CURSOR);
2685 SC_VIDEO_UNLOCK(scp->sc);
2689 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2691 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2692 sc_remove_cursor_image(scp);
2695 scp->curr_curs_attr.base = base;
2697 scp->curr_curs_attr.height = height;
2698 if (flags & CONS_RESET_CURSOR)
2699 scp->curr_curs_attr = scp->dflt_curs_attr;
2701 scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2703 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2704 sc_set_cursor_image(scp);
2705 sc_draw_cursor_image(scp);
2710 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2718 if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2719 /* local (per vty) change */
2720 change_cursor_shape(scp, flags, base, height);
2728 sc->curs_attr.base = base;
2730 sc->curs_attr.height = height;
2732 if (flags & CONS_RESET_CURSOR)
2733 sc->curs_attr = sc->dflt_curs_attr;
2735 sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2738 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2739 if ((tp = SC_DEV(sc, i)) == NULL)
2741 if ((scp = sc_get_stat(tp)) == NULL)
2743 scp->dflt_curs_attr = sc->curs_attr;
2744 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2750 scinit(int unit, int flags)
2754 * When syscons is being initialized as the kernel console, malloc()
2755 * is not yet functional, because various kernel structures has not been
2756 * fully initialized yet. Therefore, we need to declare the following
2757 * static buffers for the console. This is less than ideal,
2758 * but is necessry evil for the time being. XXX
2761 static u_short sc_buffer[ROW*COL*2];/* XXX */
2763 static u_short sc_buffer[ROW*COL]; /* XXX */
2765 #ifndef SC_NO_FONT_LOADING
2766 static u_char font_8[256*8];
2767 static u_char font_14[256*14];
2768 static u_char font_16[256*16];
2773 video_adapter_t *adp;
2778 /* one time initialization */
2779 if (init_done == COLD)
2780 sc_get_bios_values(&bios_value);
2784 * Allocate resources. Even if we are being called for the second
2785 * time, we must allocate them again, because they might have
2788 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2789 if ((sc->flags & SC_INIT_DONE) == 0)
2790 SC_VIDEO_LOCKINIT(sc);
2793 if (sc->adapter >= 0) {
2794 vid_release(sc->adp, (void *)&sc->adapter);
2798 if (sc->keyboard >= 0) {
2799 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2800 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2801 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2802 if (sc->kbd != NULL) {
2803 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2804 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2808 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2809 sc->adp = vid_get_adapter(sc->adapter);
2810 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2812 sc->keyboard = sc_allocate_keyboard(sc, unit);
2813 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2815 sc->kbd = kbd_get_keyboard(sc->keyboard);
2816 if (sc->kbd != NULL) {
2817 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2818 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2821 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2823 sc->initial_mode = sc->adp->va_initial_mode;
2825 #ifndef SC_NO_FONT_LOADING
2826 if (flags & SC_KERNEL_CONSOLE) {
2827 sc->font_8 = font_8;
2828 sc->font_14 = font_14;
2829 sc->font_16 = font_16;
2830 } else if (sc->font_8 == NULL) {
2831 /* assert(sc_malloc) */
2832 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2833 sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2834 sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2838 /* extract the hardware cursor location and hide the cursor for now */
2839 vidd_read_hw_cursor(sc->adp, &col, &row);
2840 vidd_set_hw_cursor(sc->adp, -1, -1);
2842 /* set up the first console */
2843 sc->first_vty = unit*MAXCONS;
2844 sc->vtys = MAXCONS; /* XXX: should be configurable */
2845 if (flags & SC_KERNEL_CONSOLE) {
2847 * Set up devs structure but don't use it yet, calling make_dev()
2848 * might panic kernel. Wait for sc_attach_unit() to actually
2849 * create the devices.
2851 sc->dev = main_devs;
2852 scp = &main_console;
2853 init_scp(sc, sc->first_vty, scp);
2854 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2855 (void *)sc_buffer, FALSE);
2857 /* move cursors to the initial positions */
2858 if (col >= scp->xsize)
2860 if (row >= scp->ysize)
2861 row = scp->ysize - 1;
2864 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2866 if (sc_init_emulator(scp, SC_DFLT_TERM))
2867 sc_init_emulator(scp, "*");
2868 (*scp->tsw->te_default_attr)(scp,
2869 user_default.std_color,
2870 user_default.rev_color);
2872 /* assert(sc_malloc) */
2873 sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF,
2875 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
2876 scp = alloc_scp(sc, sc->first_vty);
2877 SC_STAT(sc->dev[0]) = scp;
2882 /* copy screen to temporary buffer */
2883 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2884 (void *)scp->sc->adp->va_window, FALSE);
2886 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2889 if (bios_value.cursor_end < scp->font_size)
2890 sc->dflt_curs_attr.base = scp->font_size -
2891 bios_value.cursor_end - 1;
2893 sc->dflt_curs_attr.base = 0;
2894 i = bios_value.cursor_end - bios_value.cursor_start + 1;
2895 sc->dflt_curs_attr.height = imin(i, scp->font_size);
2896 sc->dflt_curs_attr.flags = 0;
2897 sc->curs_attr = sc->dflt_curs_attr;
2898 scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2900 #ifndef SC_NO_SYSMOUSE
2901 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2903 if (!ISGRAPHSC(scp)) {
2904 sc_set_cursor_image(scp);
2905 sc_draw_cursor_image(scp);
2908 /* save font and palette */
2909 #ifndef SC_NO_FONT_LOADING
2910 sc->fonts_loaded = 0;
2911 if (ISFONTAVAIL(sc->adp->va_flags)) {
2913 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2914 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2915 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2916 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2917 if (scp->font_size < 14) {
2918 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2919 } else if (scp->font_size >= 16) {
2920 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2922 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2924 #else /* !SC_DFLT_FONT */
2925 if (scp->font_size < 14) {
2926 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2927 sc->fonts_loaded = FONT_8;
2928 } else if (scp->font_size >= 16) {
2929 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2930 sc->fonts_loaded = FONT_16;
2932 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2933 sc->fonts_loaded = FONT_14;
2935 #endif /* SC_DFLT_FONT */
2936 /* FONT KLUDGE: always use the font page #0. XXX */
2937 sc_show_font(scp, 0);
2939 #endif /* !SC_NO_FONT_LOADING */
2941 #ifndef SC_NO_PALETTE_LOADING
2942 vidd_save_palette(sc->adp, sc->palette);
2943 #ifdef SC_PIXEL_MODE
2944 for (i = 0; i < sizeof(sc->palette2); i++)
2945 sc->palette2[i] = i / 3;
2950 if (!(sc->flags & SC_SPLASH_SCRN)) {
2951 /* we are ready to put up the splash image! */
2952 splash_init(sc->adp, scsplash_callback, sc);
2953 sc->flags |= SC_SPLASH_SCRN;
2958 /* the rest is not necessary, if we have done it once */
2959 if (sc->flags & SC_INIT_DONE)
2962 /* initialize mapscrn arrays to a one to one map */
2963 for (i = 0; i < sizeof(sc->scr_map); i++)
2964 sc->scr_map[i] = sc->scr_rmap[i] = i;
2966 sc->scr_map[0x5c] = (u_char)0xfc; /* for backslash */
2969 sc->flags |= SC_INIT_DONE;
2973 scterm(int unit, int flags)
2978 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2980 return; /* shouldn't happen */
2983 /* this console is no longer available for the splash screen */
2984 if (sc->flags & SC_SPLASH_SCRN) {
2985 splash_term(sc->adp);
2986 sc->flags &= ~SC_SPLASH_SCRN;
2991 /* move the hardware cursor to the upper-left corner */
2992 vidd_set_hw_cursor(sc->adp, 0, 0);
2995 /* release the keyboard and the video card */
2996 if (sc->keyboard >= 0)
2997 kbd_release(sc->kbd, &sc->keyboard);
2998 if (sc->adapter >= 0)
2999 vid_release(sc->adp, &sc->adapter);
3001 /* stop the terminal emulator, if any */
3002 scp = sc_get_stat(sc->dev[0]);
3004 (*scp->tsw->te_term)(scp, &scp->ts);
3005 if (scp->ts != NULL)
3006 free(scp->ts, M_DEVBUF);
3007 mtx_destroy(&scp->scr_lock);
3009 /* clear the structure */
3010 if (!(flags & SC_KERNEL_CONSOLE)) {
3011 /* XXX: We need delete_dev() for this */
3012 free(sc->dev, M_DEVBUF);
3014 /* XXX: We need a ttyunregister for this */
3015 free(sc->tty, M_DEVBUF);
3017 #ifndef SC_NO_FONT_LOADING
3018 free(sc->font_8, M_DEVBUF);
3019 free(sc->font_14, M_DEVBUF);
3020 free(sc->font_16, M_DEVBUF);
3022 /* XXX vtb, history */
3024 bzero(sc, sizeof(*sc));
3030 scshutdown(__unused void *arg, __unused int howto)
3033 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3034 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3035 KASSERT(sc_console->sc->cur_scp != NULL,
3036 ("sc_console->sc->cur_scp != NULL"));
3038 sc_touch_scrn_saver();
3040 sc_console->sc->cur_scp->index != sc_console->index &&
3041 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3042 sc_console->smode.mode == VT_AUTO)
3043 sc_switch_scr(sc_console->sc, sc_console->index);
3044 shutdown_in_progress = TRUE;
3048 scsuspend(__unused void *arg)
3052 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3053 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3054 KASSERT(sc_console->sc->cur_scp != NULL,
3055 ("sc_console->sc->cur_scp != NULL"));
3057 sc_susp_scr = sc_console->sc->cur_scp->index;
3058 if (sc_no_suspend_vtswitch ||
3059 sc_susp_scr == sc_console->index) {
3060 sc_touch_scrn_saver();
3064 for (retry = 0; retry < 10; retry++) {
3065 sc_switch_scr(sc_console->sc, sc_console->index);
3066 if (!sc_console->sc->switch_in_progress)
3068 pause("scsuspend", hz);
3070 suspend_in_progress = TRUE;
3074 scresume(__unused void *arg)
3077 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3078 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3079 KASSERT(sc_console->sc->cur_scp != NULL,
3080 ("sc_console->sc->cur_scp != NULL"));
3082 suspend_in_progress = FALSE;
3083 if (sc_susp_scr < 0) {
3084 mark_all(sc_console->sc->cur_scp);
3087 sc_switch_scr(sc_console->sc, sc_susp_scr);
3091 sc_clean_up(scr_stat *scp)
3097 if (scp->sc->flags & SC_SCRN_BLANKED) {
3098 sc_touch_scrn_saver();
3100 if ((error = wait_scrn_saver_stop(scp->sc)))
3104 scp->status |= MOUSE_HIDDEN;
3105 sc_remove_mouse_image(scp);
3106 sc_remove_cutmarking(scp);
3111 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3117 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3118 if (!discard && (old.vtb_flags & VTB_VALID)) {
3119 /* retain the current cursor position and buffer contants */
3120 scp->cursor_oldpos = scp->cursor_pos;
3122 * This works only if the old buffer has the same size as or larger
3123 * than the new one. XXX
3125 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3129 sc_vtb_destroy(&old);
3132 #ifndef SC_NO_SYSMOUSE
3133 /* move the mouse cursor at the center of the screen */
3134 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3139 *alloc_scp(sc_softc_t *sc, int vty)
3143 /* assert(sc_malloc) */
3145 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3146 init_scp(sc, vty, scp);
3148 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3149 if (sc_init_emulator(scp, SC_DFLT_TERM))
3150 sc_init_emulator(scp, "*");
3152 #ifndef SC_NO_CUTPASTE
3153 sc_alloc_cut_buffer(scp, TRUE);
3156 #ifndef SC_NO_HISTORY
3157 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3164 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3168 bzero(scp, sizeof(*scp));
3173 scp->mode = sc->initial_mode;
3174 vidd_get_info(sc->adp, scp->mode, &info);
3175 if (info.vi_flags & V_INFO_GRAPHICS) {
3176 scp->status |= GRAPHICS_MODE;
3177 scp->xpixel = info.vi_width;
3178 scp->ypixel = info.vi_height;
3179 scp->xsize = info.vi_width/info.vi_cwidth;
3180 scp->ysize = info.vi_height/info.vi_cheight;
3184 scp->xsize = info.vi_width;
3185 scp->ysize = info.vi_height;
3186 scp->xpixel = scp->xsize*info.vi_cwidth;
3187 scp->ypixel = scp->ysize*info.vi_cheight;
3190 scp->font_size = info.vi_cheight;
3191 scp->font_width = info.vi_cwidth;
3192 #ifndef SC_NO_FONT_LOADING
3193 if (info.vi_cheight < 14)
3194 scp->font = sc->font_8;
3195 else if (info.vi_cheight >= 16)
3196 scp->font = sc->font_16;
3198 scp->font = sc->font_14;
3203 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3205 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3207 scp->xoff = scp->yoff = 0;
3208 scp->xpos = scp->ypos = 0;
3209 scp->start = scp->xsize * scp->ysize - 1;
3214 scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3215 scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3216 scp->mouse_cut_start = scp->xsize*scp->ysize;
3217 scp->mouse_cut_end = -1;
3218 scp->mouse_signal = 0;
3220 scp->mouse_proc = NULL;
3221 scp->kbd_mode = K_XLATE;
3222 scp->bell_pitch = bios_value.bell_pitch;
3223 scp->bell_duration = BELL_DURATION;
3224 scp->status |= (bios_value.shift_state & NLKED);
3225 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3228 scp->smode.mode = VT_AUTO;
3229 scp->history = NULL;
3230 scp->history_pos = 0;
3231 scp->history_size = 0;
3233 mtx_init(&scp->scr_lock, "scrlock", NULL, MTX_SPIN);
3237 sc_init_emulator(scr_stat *scp, char *name)
3244 if (name == NULL) /* if no name is given, use the current emulator */
3246 else /* ...otherwise find the named emulator */
3247 sw = sc_term_match(name);
3252 if (strcmp(sw->te_renderer, "*") != 0) {
3253 rndr = sc_render_match(scp, sw->te_renderer,
3254 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3257 rndr = sc_render_match(scp, scp->sc->adp->va_name,
3258 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3263 if (sw == scp->tsw) {
3264 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3266 scp->rndr->init(scp);
3267 sc_clear_screen(scp);
3268 /* assert(error == 0); */
3272 if (sc_malloc && (sw->te_size > 0))
3273 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3276 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3281 (*scp->tsw->te_term)(scp, &scp->ts);
3282 if (scp->ts != NULL)
3283 free(scp->ts, M_DEVBUF);
3287 scp->rndr->init(scp);
3290 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3291 sc_clear_screen(scp);
3297 * scgetc(flags) - get character from keyboard.
3298 * If flags & SCGETC_CN, then avoid harmful side effects.
3299 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3300 * return NOKEY if there is nothing there.
3303 scgetc(sc_softc_t *sc, u_int flags)
3306 #ifndef SC_NO_HISTORY
3314 if (sc->kbd == NULL)
3319 /* I don't like this, but... XXX */
3320 if (flags & SCGETC_CN)
3321 sccnupdate(sc->cur_scp);
3324 /* first see if there is something in the keyboard port */
3326 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3328 if (!(flags & SCGETC_CN))
3329 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3330 } else if (c == NOKEY)
3336 /* make screensaver happy */
3338 sc_touch_scrn_saver();
3340 if (!(flags & SCGETC_CN))
3341 random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD);
3343 if (scp->kbd_mode != K_XLATE)
3346 /* if scroll-lock pressed allow history browsing */
3347 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3349 scp->status &= ~CURSOR_ENABLED;
3350 sc_remove_cursor_image(scp);
3352 #ifndef SC_NO_HISTORY
3353 if (!(scp->status & BUFFER_SAVED)) {
3354 scp->status |= BUFFER_SAVED;
3358 /* FIXME: key codes */
3359 case SPCLKEY | FKEY | F(49): /* home key */
3360 sc_remove_cutmarking(scp);
3364 case SPCLKEY | FKEY | F(57): /* end key */
3365 sc_remove_cutmarking(scp);
3369 case SPCLKEY | FKEY | F(50): /* up arrow key */
3370 sc_remove_cutmarking(scp);
3371 if (sc_hist_up_line(scp))
3372 if (!(flags & SCGETC_CN))
3373 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3376 case SPCLKEY | FKEY | F(58): /* down arrow key */
3377 sc_remove_cutmarking(scp);
3378 if (sc_hist_down_line(scp))
3379 if (!(flags & SCGETC_CN))
3380 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3383 case SPCLKEY | FKEY | F(51): /* page up key */
3384 sc_remove_cutmarking(scp);
3385 for (i=0; i<scp->ysize; i++)
3386 if (sc_hist_up_line(scp)) {
3387 if (!(flags & SCGETC_CN))
3388 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3393 case SPCLKEY | FKEY | F(59): /* page down key */
3394 sc_remove_cutmarking(scp);
3395 for (i=0; i<scp->ysize; i++)
3396 if (sc_hist_down_line(scp)) {
3397 if (!(flags & SCGETC_CN))
3398 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3403 #endif /* SC_NO_HISTORY */
3407 * Process and consume special keys here. Return a plain char code
3408 * or a char code with the META flag or a function key code.
3412 /* goto next_code */
3417 switch (KEYCHAR(c)) {
3419 case NLK: case CLK: case ALK:
3422 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3424 scp->status |= SLKED;
3426 if (scp->status & SLKED) {
3427 scp->status &= ~SLKED;
3428 #ifndef SC_NO_HISTORY
3429 if (scp->status & BUFFER_SAVED) {
3430 if (!sc_hist_restore(scp))
3431 sc_remove_cutmarking(scp);
3432 scp->status &= ~BUFFER_SAVED;
3433 scp->status |= CURSOR_ENABLED;
3434 sc_draw_cursor_image(scp);
3436 tp = SC_DEV(sc, scp->index);
3437 if (!kdb_active && tty_opened(tp))
3438 sctty_outwakeup(tp);
3445 #ifndef SC_NO_CUTPASTE
3446 sc_mouse_paste(scp);
3450 /* NON-LOCKING KEYS */
3452 case LSH: case RSH: case LCTR: case RCTR:
3453 case LALT: case RALT: case ASH: case META:
3457 if (!(sc->flags & SC_SCRN_BLANKED))
3463 /* force activatation/deactivation of the screen saver */
3464 if (!(sc->flags & SC_SCRN_BLANKED)) {
3465 run_scrn_saver = TRUE;
3466 sc->scrn_time_stamp -= scrn_blank_time;
3470 * While devices are being probed, the screen saver need
3471 * to be invoked explictly. XXX
3473 if (sc->flags & SC_SCRN_BLANKED) {
3474 scsplash_stick(FALSE);
3475 stop_scrn_saver(sc, current_saver);
3477 if (!ISGRAPHSC(scp)) {
3478 scsplash_stick(TRUE);
3479 (*current_saver)(sc, TRUE);
3483 #endif /* DEV_SPLASH */
3487 #ifndef SC_DISABLE_REBOOT
3494 #ifndef SC_DISABLE_REBOOT
3496 shutdown_nice(RB_HALT);
3501 #ifndef SC_DISABLE_REBOOT
3503 shutdown_nice(RB_HALT|RB_POWEROFF);
3508 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3511 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3515 #ifndef SC_DISABLE_KDBKEY
3522 if (enable_panic_key)
3523 panic("Forced by the panic key");
3527 this_scr = scp->index;
3528 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3529 sc->first_vty + i != this_scr;
3530 i = (i + 1)%sc->vtys) {
3531 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3532 if (tty_opened(tp)) {
3533 sc_switch_scr(scp->sc, sc->first_vty + i);
3540 this_scr = scp->index;
3541 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3542 sc->first_vty + i != this_scr;
3543 i = (i + sc->vtys - 1)%sc->vtys) {
3544 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3545 if (tty_opened(tp)) {
3546 sc_switch_scr(scp->sc, sc->first_vty + i);
3553 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3554 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3557 /* assert(c & FKEY) */
3558 if (!(sc->flags & SC_SCRN_BLANKED))
3562 /* goto next_code */
3564 /* regular keys (maybe MKEY is set) */
3565 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
3567 kdb_alt_break(c, &sc->sc_altbrk);
3569 if (!(sc->flags & SC_SCRN_BLANKED))
3578 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
3579 int nprot, vm_memattr_t *memattr)
3583 scp = sc_get_stat(tp);
3584 if (scp != scp->sc->cur_scp)
3586 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
3590 save_kbd_state(scr_stat *scp)
3595 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3596 if (error == ENOIOCTL)
3599 scp->status &= ~LOCK_MASK;
3600 scp->status |= state;
3606 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3611 if (mask != LOCK_MASK) {
3612 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3613 if (error == ENOIOCTL)
3618 state |= new_bits & mask;
3620 state = new_bits & LOCK_MASK;
3622 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3623 if (error == ENOIOCTL)
3629 update_kbd_leds(scr_stat *scp, int which)
3634 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3635 if (error == ENOIOCTL)
3641 set_mode(scr_stat *scp)
3645 /* reject unsupported mode */
3646 if (vidd_get_info(scp->sc->adp, scp->mode, &info))
3649 /* if this vty is not currently showing, do nothing */
3650 if (scp != scp->sc->cur_scp)
3653 /* setup video hardware for the given mode */
3654 vidd_set_mode(scp->sc->adp, scp->mode);
3655 scp->rndr->init(scp);
3657 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3658 (void *)scp->sc->adp->va_window, FALSE);
3661 #ifndef SC_NO_FONT_LOADING
3662 /* load appropriate font */
3663 if (!(scp->status & GRAPHICS_MODE)) {
3664 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3665 if (scp->font_size < 14) {
3666 if (scp->sc->fonts_loaded & FONT_8)
3667 sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3668 } else if (scp->font_size >= 16) {
3669 if (scp->sc->fonts_loaded & FONT_16)
3670 sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3672 if (scp->sc->fonts_loaded & FONT_14)
3673 sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3677 * This is an interim kludge to display correct font.
3678 * Always use the font page #0 on the video plane 2.
3679 * Somehow we cannot show the font in other font pages on
3680 * some video cards... XXX
3682 sc_show_font(scp, 0);
3686 #endif /* !SC_NO_FONT_LOADING */
3688 sc_set_border(scp, scp->border);
3689 sc_set_cursor_image(scp);
3695 sc_set_border(scr_stat *scp, int color)
3697 SC_VIDEO_LOCK(scp->sc);
3698 (*scp->rndr->draw_border)(scp, color);
3699 SC_VIDEO_UNLOCK(scp->sc);
3702 #ifndef SC_NO_FONT_LOADING
3704 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3705 int base, int count)
3710 sc->font_loading_in_progress = TRUE;
3711 vidd_load_font(sc->adp, page, size, width, buf, base, count);
3712 sc->font_loading_in_progress = FALSE;
3716 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3717 int base, int count)
3722 sc->font_loading_in_progress = TRUE;
3723 vidd_save_font(sc->adp, page, size, width, buf, base, count);
3724 sc->font_loading_in_progress = FALSE;
3728 sc_show_font(scr_stat *scp, int page)
3730 vidd_show_font(scp->sc->adp, page);
3732 #endif /* !SC_NO_FONT_LOADING */
3735 sc_paste(scr_stat *scp, const u_char *p, int count)
3740 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3741 if (!tty_opened(tp))
3743 rmap = scp->sc->scr_rmap;
3744 for (; count > 0; --count)
3745 ttydisc_rint(tp, rmap[*p++], 0);
3746 ttydisc_rint_done(tp);
3750 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup)
3754 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3755 if (!tty_opened(tp))
3757 ttydisc_rint_simple(tp, p, count);
3759 /* XXX: we can't always call ttydisc_rint_done() here! */
3760 ttydisc_rint_done(tp);
3765 sc_bell(scr_stat *scp, int pitch, int duration)
3767 if (cold || shutdown_in_progress || !enable_bell)
3770 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3773 if (scp->sc->flags & SC_VISUAL_BELL) {
3774 if (scp->sc->blink_in_progress)
3776 scp->sc->blink_in_progress = 3;
3777 if (scp != scp->sc->cur_scp)
3778 scp->sc->blink_in_progress += 2;
3779 blink_screen(scp->sc->cur_scp);
3780 } else if (duration != 0 && pitch != 0) {
3781 if (scp != scp->sc->cur_scp)
3783 sysbeep(1193182 / pitch, duration);
3788 blink_screen(void *arg)
3790 scr_stat *scp = arg;
3793 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3794 scp->sc->blink_in_progress = 0;
3796 tp = SC_DEV(scp->sc, scp->index);
3798 sctty_outwakeup(tp);
3799 if (scp->sc->delayed_next_scr)
3800 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3803 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3804 scp->sc->blink_in_progress & 1);
3805 scp->sc->blink_in_progress--;
3806 timeout(blink_screen, scp, hz / 10);
3811 * Until sc_attach_unit() gets called no dev structures will be available
3812 * to store the per-screen current status. This is the case when the
3813 * kernel is initially booting and needs access to its console. During
3814 * this early phase of booting the console's current status is kept in
3815 * one statically defined scr_stat structure, and any pointers to the
3816 * dev structures will be NULL.
3820 sc_get_stat(struct tty *tp)
3823 return (&main_console);
3824 return (SC_STAT(tp));
3828 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3829 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3834 sc_allocate_keyboard(sc_softc_t *sc, int unit)
3840 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3842 k0 = kbd_get_keyboard(idx0);
3844 for (idx = kbd_find_keyboard2("*", -1, 0);
3846 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
3847 k = kbd_get_keyboard(idx);
3849 if (idx == idx0 || KBD_IS_BUSY(k))
3852 bzero(&ki, sizeof(ki));
3853 strcpy(ki.kb_name, k->kb_name);
3854 ki.kb_unit = k->kb_unit;
3856 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3859 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);