2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992-1998 Søren Schmidt
7 * This code is derived from software contributed to The DragonFly Project
8 * by Sascha Wildner <saw@online.de>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_syscons.h"
38 #include "opt_splash.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
46 #include <sys/consio.h>
48 #include <sys/eventhandler.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
58 #include <sys/random.h>
59 #include <sys/reboot.h>
60 #include <sys/serial.h>
61 #include <sys/signalvar.h>
63 #include <sys/sysctl.h>
65 #include <sys/power.h>
67 #include <machine/clock.h>
68 #if defined(__arm__) || defined(__mips__) || defined(__powerpc__)
69 #include <machine/sc_machdep.h>
71 #include <machine/pc/display.h>
73 #if defined(__i386__) || defined(__amd64__)
74 #include <machine/psl.h>
75 #include <machine/frame.h>
77 #include <machine/stdarg.h>
79 #if defined(__amd64__) || defined(__i386__)
80 #include <machine/vmparam.h>
86 #include <dev/kbd/kbdreg.h>
87 #include <dev/fb/fbreg.h>
88 #include <dev/fb/splashreg.h>
89 #include <dev/syscons/syscons.h>
94 #define DEFAULT_BLANKTIME (5 * 60) /* 5 minutes */
95 #define MAX_BLANKTIME (7 * 24 * 60 * 60) /* 7 days!? */
97 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
99 /* NULL-safe version of "tty_opened()" */
100 #define tty_opened_ns(tp) ((tp) != NULL && tty_opened(tp))
102 static u_char sc_kattrtab[MAXCPU];
104 static int sc_console_unit = -1;
105 static int sc_saver_keyb_only = 1;
106 static scr_stat *sc_console;
107 static struct consdev *sc_consptr;
108 static void *sc_kts[MAXCPU];
109 static struct sc_term_sw *sc_ktsw;
110 static scr_stat main_console;
111 static struct tty *main_devs[MAXCONS];
113 static char init_done = COLD;
114 static int shutdown_in_progress = FALSE;
115 static int suspend_in_progress = FALSE;
116 static char sc_malloc = FALSE;
118 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
119 static int run_scrn_saver = FALSE; /* should run the saver? */
120 static int enable_bell = TRUE; /* enable beeper */
122 #ifndef SC_DISABLE_REBOOT
123 static int enable_reboot = TRUE; /* enable keyboard reboot */
126 #ifndef SC_DISABLE_KDBKEY
127 static int enable_kdbkey = TRUE; /* enable keyboard debug */
130 static long scrn_blank_time = 0; /* screen saver timeout value */
132 static int scrn_blanked; /* # of blanked screen */
133 static int sticky_splash = FALSE;
136 none_saver(sc_softc_t *sc, int blank)
139 static void (*current_saver)(sc_softc_t *, int) = none_saver;
142 #ifdef SC_NO_SUSPEND_VTYSWITCH
143 static int sc_no_suspend_vtswitch = 1;
145 static int sc_no_suspend_vtswitch = 0;
147 static int sc_susp_scr;
149 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
151 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver,
152 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
154 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
155 &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
157 _hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell, 0, "enable bell");
158 #ifndef SC_DISABLE_REBOOT
159 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW | CTLFLAG_SECURE,
160 &enable_reboot, 0, "enable keyboard reboot");
162 #ifndef SC_DISABLE_KDBKEY
163 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW | CTLFLAG_SECURE,
164 &enable_kdbkey, 0, "enable keyboard debug");
166 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN,
167 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
168 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
172 tsw_ioctl_t *sc_user_ioctl;
174 static bios_values_t bios_value;
176 static int enable_panic_key;
177 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
178 0, "Enable panic via keypress specified in kbdmap(5)");
180 #define SC_CONSOLECTL 255
182 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
185 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
186 static int scvidprobe(int unit, int flags, int cons);
187 static int sckbdprobe(int unit, int flags, int cons);
188 static void scmeminit(void *arg);
189 static int scdevtounit(struct tty *tp);
190 static kbd_callback_func_t sckbdevent;
191 static void scinit(int unit, int flags);
192 static scr_stat *sc_get_stat(struct tty *tp);
193 static void scterm(int unit, int flags);
194 static void scshutdown(void *, int);
195 static void scsuspend(void *);
196 static void scresume(void *);
197 static u_int scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp);
198 static void sc_puts(scr_stat *scp, u_char *buf, int len);
200 #define SCGETC_NONBLOCK 2
201 static void sccnupdate(scr_stat *scp);
202 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
203 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
204 static callout_func_t scrn_timer;
205 static int and_region(int *s1, int *e1, int s2, int e2);
206 static void scrn_update(scr_stat *scp, int show_cursor);
209 static int scsplash_callback(int event, void *arg);
210 static void scsplash_saver(sc_softc_t *sc, int show);
211 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
212 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
213 static int set_scrn_saver_mode(
214 scr_stat *scp, int mode, u_char *pal, int border);
215 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
216 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
217 static int wait_scrn_saver_stop(sc_softc_t *sc);
218 #define scsplash_stick(stick) (sticky_splash = (stick))
219 #else /* !DEV_SPLASH */
220 #define scsplash_stick(stick)
221 #endif /* DEV_SPLASH */
223 static int do_switch_scr(sc_softc_t *sc, int s);
224 static int vt_proc_alive(scr_stat *scp);
225 static int signal_vt_rel(scr_stat *scp);
226 static int signal_vt_acq(scr_stat *scp);
227 static int finish_vt_rel(scr_stat *scp, int release, int *s);
228 static int finish_vt_acq(scr_stat *scp);
229 static void exchange_scr(sc_softc_t *sc);
230 static void update_cursor_image(scr_stat *scp);
231 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
232 static void update_font(scr_stat *);
233 static int save_kbd_state(scr_stat *scp);
234 static int update_kbd_state(scr_stat *scp, int state, int mask);
235 static int update_kbd_leds(scr_stat *scp, int which);
236 static int sc_kattr(void);
237 static callout_func_t blink_screen;
238 static struct tty *sc_alloc_tty(int, int);
240 static cn_probe_t sc_cnprobe;
241 static cn_init_t sc_cninit;
242 static cn_term_t sc_cnterm;
243 static cn_getc_t sc_cngetc;
244 static cn_putc_t sc_cnputc;
245 static cn_grab_t sc_cngrab;
246 static cn_ungrab_t sc_cnungrab;
250 static tsw_open_t sctty_open;
251 static tsw_close_t sctty_close;
252 static tsw_outwakeup_t sctty_outwakeup;
253 static tsw_ioctl_t sctty_ioctl;
254 static tsw_mmap_t sctty_mmap;
256 static struct ttydevsw sc_ttydevsw = {
257 .tsw_open = sctty_open,
258 .tsw_close = sctty_close,
259 .tsw_outwakeup = sctty_outwakeup,
260 .tsw_ioctl = sctty_ioctl,
261 .tsw_mmap = sctty_mmap,
264 static d_ioctl_t consolectl_ioctl;
265 static d_close_t consolectl_close;
267 static struct cdevsw consolectl_devsw = {
268 .d_version = D_VERSION,
269 .d_flags = D_NEEDGIANT | D_TRACKCLOSE,
270 .d_ioctl = consolectl_ioctl,
271 .d_close = consolectl_close,
272 .d_name = "consolectl",
275 /* ec -- emergency console. */
277 static u_int ec_scroffset;
285 int attr, column, mysize, width, xsize, yborder, ysize;
287 if (c < 0 || c > 0xff || c == '\a')
289 if (sc_console == NULL) {
290 #if !defined(__amd64__) && !defined(__i386__)
294 * This is enough for ec_putc() to work very early on x86
295 * if the kernel starts in normal color text mode.
298 fb = KERNBASE + 0xb8000;
300 fb = pmap_get_map_low() + 0xb8000;
306 if (!ISTEXTSC(&main_console))
308 fb = main_console.sc->adp->va_window;
309 xsize = main_console.xsize;
310 ysize = main_console.ysize;
313 scrptr = (u_short *)(void *)fb + xsize * yborder;
314 mysize = xsize * (ysize - 2 * yborder);
317 column = ind % xsize;
322 c == '\r' ? -column : c == '\n' ? xsize - column : 1);
323 if (width == 0 || (width < 0 && ind < -width))
325 } while (atomic_cmpset_rel_int(&ec_scroffset, ind, ind + width) == 0);
326 if (c == '\b' || c == '\r')
329 ind += xsize; /* XXX clearing from new pos is not atomic */
332 if (c == '\t' || c == '\n')
335 scrptr[ind++ % mysize] = (attr << 8) | c;
336 while (--width != 0);
340 sc_probe_unit(int unit, int flags)
342 if (!vty_enabled(VTY_SC))
344 if (!scvidprobe(unit, flags, FALSE)) {
346 printf("%s%d: no video adapter found.\n",
347 SC_DRIVER_NAME, unit);
351 /* syscons will be attached even when there is no keyboard */
352 sckbdprobe(unit, flags, FALSE);
357 /* probe video adapters, return TRUE if found */
359 scvidprobe(int unit, int flags, int cons)
362 * Access the video adapter driver through the back door!
363 * Video adapter drivers need to be configured before syscons.
364 * However, when syscons is being probed as the low-level console,
365 * they have not been initialized yet. We force them to initialize
366 * themselves here. XXX
368 vid_configure(cons ? VIO_PROBE_ONLY : 0);
370 return (vid_find_adapter("*", unit) >= 0);
373 /* probe the keyboard, return TRUE if found */
375 sckbdprobe(int unit, int flags, int cons)
377 /* access the keyboard driver through the backdoor! */
378 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
380 return (kbd_find_keyboard("*", unit) >= 0);
384 adapter_name(video_adapter_t *adp)
390 { KD_MONO, { "MDA", "MDA" } },
391 { KD_HERCULES, { "Hercules", "Hercules" } },
392 { KD_CGA, { "CGA", "CGA" } },
393 { KD_EGA, { "EGA", "EGA (mono)" } },
394 { KD_VGA, { "VGA", "VGA (mono)" } },
395 { KD_TGA, { "TGA", "TGA" } },
396 { -1, { "Unknown", "Unknown" } },
400 for (i = 0; names[i].type != -1; ++i)
401 if (names[i].type == adp->va_type)
403 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
407 sctty_outwakeup(struct tty *tp)
411 scr_stat *scp = sc_get_stat(tp);
413 if (scp->status & SLKED ||
414 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
418 len = ttydisc_getc(tp, buf, sizeof buf);
421 SC_VIDEO_LOCK(scp->sc);
422 sc_puts(scp, buf, len);
423 SC_VIDEO_UNLOCK(scp->sc);
428 sc_alloc_tty(int index, int devnum)
430 struct sc_ttysoftc *stc;
433 /* Allocate TTY object and softc to store unit number. */
434 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
435 stc->st_index = index;
437 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
439 /* Create device node. */
440 tty_makedev(tp, NULL, "v%r", devnum);
447 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
457 (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
458 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
459 vidd_get_info(sc->adp, vmode, &info) != 0 ||
460 !sc_support_pixel_mode(&info))
464 * If the mode is unset or unsupported, search for an available
465 * 800x600 graphics mode with the highest color depth.
468 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
469 if (vidd_get_info(sc->adp, i, &info) == 0 &&
470 info.vi_width == 800 && info.vi_height == 600 &&
471 sc_support_pixel_mode(&info) &&
472 info.vi_depth > depth) {
474 depth = info.vi_depth;
478 vidd_get_info(sc->adp, vmode, &info);
481 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
482 fontsize = info.vi_cheight;
484 fontsize = scp->font_size;
488 else if (fontsize >= 16)
492 #ifndef SC_NO_FONT_LOADING
495 if ((sc->fonts_loaded & FONT_8) == 0)
500 if ((sc->fonts_loaded & FONT_14) == 0)
505 if ((sc->fonts_loaded & FONT_16) == 0)
514 if ((sc->flags & SC_SPLASH_SCRN) != 0)
515 splash_term(sc->adp);
517 #ifndef SC_NO_HISTORY
518 if (scp->history != NULL) {
519 sc_vtb_append(&scp->vtb, 0, scp->history,
520 scp->ypos * scp->xsize + scp->xpos);
521 scp->history_pos = sc_vtb_tail(scp->history);
524 vidd_set_mode(sc->adp, vmode);
525 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
526 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
527 scp->xpixel = info.vi_width;
528 scp->ypixel = info.vi_height;
529 scp->xsize = scp->xpixel / 8;
530 scp->ysize = scp->ypixel / fontsize;
532 scp->ypos = scp->ysize - 1;
533 scp->xoff = scp->yoff = 0;
535 scp->font_size = fontsize;
537 scp->start = scp->xsize * scp->ysize - 1;
539 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
540 scp->mode = sc->initial_mode = vmode;
541 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
542 (void *)sc->adp->va_window, FALSE);
543 sc_alloc_scr_buffer(scp, FALSE, FALSE);
544 sc_init_emulator(scp, NULL);
545 #ifndef SC_NO_CUTPASTE
546 sc_alloc_cut_buffer(scp, FALSE);
548 #ifndef SC_NO_HISTORY
549 sc_alloc_history_buffer(scp, 0, 0, FALSE);
551 sc_set_border(scp, scp->border);
552 sc_set_cursor_image(scp);
553 scp->status &= ~UNKNOWN_MODE;
555 if ((sc->flags & SC_SPLASH_SCRN) != 0)
556 splash_init(sc->adp, scsplash_callback, sc);
562 sc_attach_unit(int unit, int flags)
570 if (!vty_enabled(VTY_SC))
573 flags &= ~SC_KERNEL_CONSOLE;
575 if (sc_console_unit == unit) {
577 * If this unit is being used as the system console, we need to
578 * adjust some variables and buffers before and after scinit().
580 /* assert(sc_console != NULL) */
581 flags |= SC_KERNEL_CONSOLE;
586 if (sc_console->tsw->te_size > 0) {
587 sc_ktsw = sc_console->tsw;
588 /* assert(sc_console->ts != NULL); */
589 oldts = sc_console->ts;
590 for (i = 0; i <= mp_maxid; i++) {
591 ts = malloc(sc_console->tsw->te_size, M_DEVBUF,
593 (*sc_console->tsw->te_init)(
594 sc_console, &ts, SC_TE_COLD_INIT);
596 (*sc_console->tsw->te_default_attr)(sc_console,
597 sc_kattrtab[i], SC_KERNEL_CONS_REV_ATTR);
600 sc_console->ts = oldts;
601 (*sc_console->tsw->te_default_attr)(
602 sc_console, SC_NORM_ATTR, SC_NORM_REV_ATTR);
608 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
610 callout_init(&sc->ctimeout, 0);
611 callout_init(&sc->cblink, 0);
612 scp = sc_get_stat(sc->dev[0]);
613 if (sc_console == NULL) /* sc_console_unit < 0 */
617 if ((sc->config & SC_VESAMODE) != 0)
618 sc_set_vesa_mode(scp, sc, unit);
619 #endif /* SC_PIXEL_MODE */
621 /* initialize cursor */
623 update_cursor_image(scp);
625 /* get screen update going */
628 /* set up the keyboard */
629 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
630 update_kbd_state(scp, scp->status, LOCK_MASK);
632 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n", SC_DRIVER_NAME,
633 unit, adapter_name(sc->adp), sc->vtys, sc->config);
635 printf("%s%d:", SC_DRIVER_NAME, unit);
636 if (sc->adapter >= 0)
637 printf(" fb%d", sc->adapter);
639 printf(", kbd%d", sc->kbd->kb_index);
641 printf(", terminal emulator: %s (%s)",
642 scp->tsw->te_name, scp->tsw->te_desc);
646 /* Register suspend/resume/shutdown callbacks for the kernel console. */
647 if (sc_console_unit == unit) {
648 EVENTHANDLER_REGISTER(
649 power_suspend_early, scsuspend, NULL, EVENTHANDLER_PRI_ANY);
650 EVENTHANDLER_REGISTER(
651 power_resume, scresume, NULL, EVENTHANDLER_PRI_ANY);
652 EVENTHANDLER_REGISTER(
653 shutdown_pre_sync, scshutdown, NULL, SHUTDOWN_PRI_DEFAULT);
656 for (vc = 0; vc < sc->vtys; vc++) {
657 if (sc->dev[vc] == NULL) {
658 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
659 if (vc == 0 && sc->dev == main_devs)
660 SC_STAT(sc->dev[0]) = &main_console;
663 * The first vty already has struct tty and scr_stat initialized
664 * in scinit(). The other vtys will have these structs when
670 &consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "consolectl");
671 dev->si_drv1 = sc->dev[0];
679 if (!vty_enabled(VTY_SC))
686 * As soon as malloc() becomes functional, we had better allocate
687 * various buffers for the kernel console.
690 if (sc_console_unit < 0) /* sc_console == NULL */
693 /* copy the temporary buffer to the final buffer */
694 sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
696 #ifndef SC_NO_CUTPASTE
697 sc_alloc_cut_buffer(sc_console, FALSE);
700 #ifndef SC_NO_HISTORY
701 /* initialize history buffer & pointers */
702 sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
707 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
710 scdevtounit(struct tty *tp)
712 int vty = SC_VTY(tp);
714 if (vty == SC_CONSOLECTL)
715 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
716 else if ((vty < 0) || (vty >= MAXCONS * sc_max_unit()))
719 return vty / MAXCONS;
723 sctty_open(struct tty *tp)
725 int unit = scdevtounit(tp);
731 ("scopen: dev:%s, unit:%d, vty:%d\n", devtoname(tp->t_dev), unit,
735 unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
739 if (!tty_opened(tp)) {
740 /* Use the current setting of the <-- key as default VERASE. */
741 /* If the Delete key is preferable, an stty is necessary */
742 if (sc->kbd != NULL) {
743 key.keynum = KEYCODE_BS;
744 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
745 tp->t_termios.c_cc[VERASE] = key.key.map[0];
749 scp = sc_get_stat(tp);
751 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
753 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
755 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
756 tp->t_winsize.ws_col = scp->xsize;
757 tp->t_winsize.ws_row = scp->ysize;
764 sctty_close(struct tty *tp)
769 if (SC_VTY(tp) != SC_CONSOLECTL) {
770 scp = sc_get_stat(tp);
771 /* were we in the middle of the VT switching process? */
772 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
774 if ((scp == scp->sc->cur_scp) &&
775 (scp->sc->unit == sc_console_unit))
776 cnavailable(sc_consptr, TRUE);
777 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */
778 DPRINTF(5, ("reset WAIT_REL, "));
779 if (finish_vt_acq(scp) == 0) /* force acknowledge */
780 DPRINTF(5, ("reset WAIT_ACQ, "));
782 if (scp == &main_console) {
785 scp->smode.mode = VT_AUTO;
787 sc_vtb_destroy(&scp->vtb);
788 sc_vtb_destroy(&scp->scr);
789 sc_free_history_buffer(scp, scp->ysize);
796 scp->smode.mode = VT_AUTO;
798 scp->kbd_mode = K_XLATE;
799 if (scp == scp->sc->cur_scp)
801 scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
802 DPRINTF(5, ("done.\n"));
806 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
808 scread(struct cdev *dev, struct uio *uio, int flag)
810 if (!sc_saver_keyb_only)
811 sc_touch_scrn_saver();
812 return ttyread(dev, uio, flag);
817 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
825 sc = (sc_softc_t *)arg;
826 /* assert(thiskbd == sc->kbd) */
833 case KBDIO_UNLOADING:
835 kbd_release(thiskbd, (void *)&sc->kbd);
843 * Loop while there is still input to get from the keyboard.
844 * I don't think this is nessesary, and it doesn't fix
845 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
847 while ((c = scgetc(sc, SCGETC_NONBLOCK, NULL)) != NOKEY) {
848 cur_tty = SC_DEV(sc, sc->cur_scp->index);
849 if (!tty_opened_ns(cur_tty))
852 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
855 switch (KEYFLAGS(c)) {
856 case 0x0000: /* normal key */
857 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
859 case FKEY: /* function key, return string */
860 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
862 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
865 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
867 ttydisc_rint_simple(cur_tty, cp, len);
869 case MKEY: /* meta is active, prepend ESC */
870 ttydisc_rint(cur_tty, 0x1b, 0);
871 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
873 case BKEY: /* backtab fixed sequence (esc [ Z) */
874 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
878 ttydisc_rint_done(cur_tty);
881 sc->cur_scp->status |= MOUSE_HIDDEN;
889 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
893 struct cursor_attr *cap;
897 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
898 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
902 /* If there is a user_ioctl function call that first */
904 error = (*sc_user_ioctl)(tp, cmd, data, td);
905 if (error != ENOIOCTL)
909 error = sc_vid_ioctl(tp, cmd, data, td);
910 if (error != ENOIOCTL)
913 #ifndef SC_NO_HISTORY
914 error = sc_hist_ioctl(tp, cmd, data, td);
915 if (error != ENOIOCTL)
919 #ifndef SC_NO_SYSMOUSE
920 error = sc_mouse_ioctl(tp, cmd, data, td);
921 if (error != ENOIOCTL)
925 scp = sc_get_stat(tp);
926 /* assert(scp != NULL) */
927 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
931 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
932 if (error != ENOIOCTL)
936 switch (cmd) { /* process console hardware related ioctl's */
938 case GIO_ATTR: /* get current attributes */
939 /* this ioctl is not processed here, but in the terminal
943 case GIO_COLOR: /* is this a color console ? */
944 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
947 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
948 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
951 scrn_blank_time = *(int *)data;
952 run_scrn_saver = (scrn_blank_time != 0);
956 case CONS_CURSORTYPE: /* set cursor type (old interface + HIDDEN) */
958 *(int *)data &= CONS_CURSOR_ATTRS;
959 sc_change_cursor_shape(scp, *(int *)data, -1, -1);
963 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */
964 switch (((int *)data)[0] &
965 (CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR)) {
967 cap = &sc->curs_attr;
969 case CONS_LOCAL_CURSOR:
970 cap = &scp->base_curs_attr;
972 case CONS_DEFAULT_CURSOR:
973 cap = &sc->dflt_curs_attr;
975 case CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR:
976 cap = &scp->dflt_curs_attr;
979 if (((int *)data)[0] & CONS_CHARCURSOR_COLORS) {
980 ((int *)data)[1] = cap->bg[0];
981 ((int *)data)[2] = cap->bg[1];
982 } else if (((int *)data)[0] & CONS_MOUSECURSOR_COLORS) {
983 ((int *)data)[1] = cap->mouse_ba;
984 ((int *)data)[2] = cap->mouse_ia;
986 ((int *)data)[1] = cap->base;
987 ((int *)data)[2] = cap->height;
989 ((int *)data)[0] = cap->flags;
992 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */
994 sc_change_cursor_shape(
995 scp, ((int *)data)[0], ((int *)data)[1], ((int *)data)[2]);
999 case CONS_BELLTYPE: /* set bell type sound/visual */
1000 if ((*(int *)data) & CONS_VISUAL_BELL)
1001 sc->flags |= SC_VISUAL_BELL;
1003 sc->flags &= ~SC_VISUAL_BELL;
1004 if ((*(int *)data) & CONS_QUIET_BELL)
1005 sc->flags |= SC_QUIET_BELL;
1007 sc->flags &= ~SC_QUIET_BELL;
1010 case CONS_GETINFO: /* get current (virtual) console info */
1012 vid_info_t *ptr = (vid_info_t *)data;
1013 if (ptr->size == sizeof(struct vid_info)) {
1014 ptr->m_num = sc->cur_scp->index;
1015 ptr->font_size = scp->font_size;
1016 ptr->mv_col = scp->xpos;
1017 ptr->mv_row = scp->ypos;
1018 ptr->mv_csz = scp->xsize;
1019 ptr->mv_rsz = scp->ysize;
1021 (scp->history != NULL) ? scp->history->vtb_rows : 0;
1023 * The following fields are filled by the terminal
1031 ptr->mv_grfc.fore = 0; /* not supported */
1032 ptr->mv_grfc.back = 0; /* not supported */
1033 ptr->mv_ovscan = scp->border;
1034 if (scp == sc->cur_scp)
1035 save_kbd_state(scp);
1036 ptr->mk_keylock = scp->status & LOCK_MASK;
1042 case CONS_GETVERS: /* get version number */
1043 *(int *)data = 0x200; /* version 2.0 */
1046 case CONS_IDLE: /* see if the screen has been idle */
1048 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
1049 * the user process may have been writing something on the
1050 * screen and syscons is not aware of it. Declare the screen
1051 * is NOT idle if it is in one of these modes. But there is
1052 * an exception to it; if a screen saver is running in the
1053 * graphics mode in the current screen, we should say that the
1054 * screen has been idle.
1056 *(int *)data = (sc->flags & SC_SCRN_IDLE) &&
1057 (!ISGRAPHSC(sc->cur_scp) ||
1058 (sc->cur_scp->status & SAVER_RUNNING));
1061 case CONS_SAVERMODE: /* set saver mode */
1062 switch (*(int *)data) {
1064 case CONS_USR_SAVER:
1065 /* if a LKM screen saver is running, stop it first. */
1066 scsplash_stick(FALSE);
1067 saver_mode = *(int *)data;
1070 if ((error = wait_scrn_saver_stop(NULL))) {
1075 run_scrn_saver = TRUE;
1076 if (saver_mode == CONS_USR_SAVER)
1077 scp->status |= SAVER_RUNNING;
1079 scp->status &= ~SAVER_RUNNING;
1080 scsplash_stick(TRUE);
1083 case CONS_LKM_SAVER:
1085 if ((saver_mode == CONS_USR_SAVER) &&
1086 (scp->status & SAVER_RUNNING))
1087 scp->status &= ~SAVER_RUNNING;
1088 saver_mode = *(int *)data;
1096 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
1098 * Note that this ioctl does not guarantee the screen saver
1099 * actually starts or stops. It merely attempts to do so...
1102 run_scrn_saver = (*(int *)data != 0);
1104 sc->scrn_time_stamp -= scrn_blank_time;
1108 case CONS_SCRSHOT: /* get a screen shot */
1110 int retval, hist_rsz;
1111 size_t lsize, csize;
1112 vm_offset_t frbp, hstp;
1114 scrshot_t *ptr = (scrshot_t *)data;
1115 void *outp = ptr->buf;
1117 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 ||
1121 if (ISGRAPHSC(scp)) {
1125 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1126 if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
1127 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1132 lsize = scp->xsize * sizeof(u_int16_t);
1133 csize = ptr->xsize * sizeof(u_int16_t);
1134 /* Pointer to the last line of framebuffer */
1135 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize +
1136 ptr->x * sizeof(u_int16_t);
1137 /* Pointer to the last line of target buffer */
1138 outp = (char *)outp + ptr->ysize * csize;
1139 /* Pointer to the last line of history buffer */
1140 if (scp->history != NULL)
1141 hstp = scp->history->vtb_buffer +
1142 sc_vtb_tail(scp->history) * sizeof(u_int16_t) +
1143 ptr->x * sizeof(u_int16_t);
1148 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1149 if (lnum < scp->ysize) {
1153 if (hstp < scp->history->vtb_buffer)
1154 hstp += scp->history->vtb_rows * lsize;
1159 outp = (char *)outp - csize;
1160 retval = copyout((void *)frbp, outp, csize);
1168 case VT_SETMODE: /* set screen switcher mode */
1170 struct vt_mode *mode;
1173 mode = (struct vt_mode *)data;
1174 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1175 if (scp->smode.mode == VT_PROCESS) {
1176 p1 = pfind(scp->pid);
1177 if (scp->proc == p1 && scp->proc != td->td_proc) {
1180 DPRINTF(5, ("error EPERM\n"));
1187 if (mode->mode == VT_AUTO) {
1188 scp->smode.mode = VT_AUTO;
1191 DPRINTF(5, ("VT_AUTO, "));
1192 if ((scp == sc->cur_scp) &&
1193 (sc->unit == sc_console_unit))
1194 cnavailable(sc_consptr, TRUE);
1195 /* were we in the middle of the vty switching process?
1197 if (finish_vt_rel(scp, TRUE, &s) == 0)
1198 DPRINTF(5, ("reset WAIT_REL, "));
1199 if (finish_vt_acq(scp) == 0)
1200 DPRINTF(5, ("reset WAIT_ACQ, "));
1202 if (!ISSIGVALID(mode->relsig) ||
1203 !ISSIGVALID(mode->acqsig) ||
1204 !ISSIGVALID(mode->frsig)) {
1206 DPRINTF(5, ("error EINVAL\n"));
1209 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1210 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1211 scp->proc = td->td_proc;
1212 scp->pid = scp->proc->p_pid;
1213 if ((scp == sc->cur_scp) &&
1214 (sc->unit == sc_console_unit))
1215 cnavailable(sc_consptr, FALSE);
1222 case VT_GETMODE: /* get screen switcher mode */
1223 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1226 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1227 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1229 ival = IOCPARM_IVAL(data);
1230 data = (caddr_t)&ival;
1233 case VT_RELDISP: /* screen switcher ioctl */
1236 * This must be the current vty which is in the VT_PROCESS
1239 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1243 /* ...and this process is controlling it. */
1244 if (scp->proc != td->td_proc) {
1249 switch (*(int *)data) {
1250 case VT_FALSE: /* user refuses to release screen, abort */
1251 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1253 ("%s%d: VT_FALSE\n", SC_DRIVER_NAME,
1256 case VT_TRUE: /* user has released screen, go on */
1257 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1259 ("%s%d: VT_TRUE\n", SC_DRIVER_NAME,
1262 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1263 if ((error = finish_vt_acq(scp)) == 0)
1265 ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME,
1274 case VT_OPENQRY: /* return free virtual console */
1275 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1277 if (!tty_opened_ns(tp)) {
1278 *(int *)data = i + 1;
1284 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1285 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1287 ival = IOCPARM_IVAL(data);
1288 data = (caddr_t)&ival;
1291 case VT_ACTIVATE: /* switch to screen *data */
1292 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1294 error = sc_clean_up(sc->cur_scp);
1298 error = sc_switch_scr(sc, i);
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 VT_WAITACTIVE: /* wait for switch to occur */
1309 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1310 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1312 if (i == sc->cur_scp->index)
1315 tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1318 case VT_GETACTIVE: /* get active vty # */
1319 *(int *)data = sc->cur_scp->index + 1;
1322 case VT_GETINDEX: /* get this vty # */
1323 *(int *)data = scp->index + 1;
1326 case VT_LOCKSWITCH: /* prevent vty switching */
1327 if ((*(int *)data) & 0x01)
1328 sc->flags |= SC_SCRN_VTYLOCK;
1330 sc->flags &= ~SC_SCRN_VTYLOCK;
1333 case KDENABIO: /* allow io operations */
1334 error = priv_check(td, PRIV_IO);
1337 error = securelevel_gt(td->td_ucred, 0);
1341 td->td_frame->tf_eflags |= PSL_IOPL;
1342 #elif defined(__amd64__)
1343 td->td_frame->tf_rflags |= PSL_IOPL;
1347 case KDDISABIO: /* disallow io operations (default) */
1349 td->td_frame->tf_eflags &= ~PSL_IOPL;
1350 #elif defined(__amd64__)
1351 td->td_frame->tf_rflags &= ~PSL_IOPL;
1355 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1356 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1358 ival = IOCPARM_IVAL(data);
1359 data = (caddr_t)&ival;
1362 case KDSKBSTATE: /* set keyboard state (locks) */
1363 if (*(int *)data & ~LOCK_MASK)
1365 scp->status &= ~LOCK_MASK;
1366 scp->status |= *(int *)data;
1367 if (scp == sc->cur_scp)
1368 update_kbd_state(scp, scp->status, LOCK_MASK);
1371 case KDGKBSTATE: /* get keyboard state (locks) */
1372 if (scp == sc->cur_scp)
1373 save_kbd_state(scp);
1374 *(int *)data = scp->status & LOCK_MASK;
1377 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1378 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1379 error = kbdd_ioctl(sc->kbd, cmd, data);
1380 if (error == ENOIOCTL)
1384 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1385 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1387 ival = IOCPARM_IVAL(data);
1388 data = (caddr_t)&ival;
1391 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1392 if (*(int *)data & ~0x7f)
1394 error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1395 if (error == ENOIOCTL)
1399 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1400 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1402 ival = IOCPARM_IVAL(data);
1403 data = (caddr_t)&ival;
1406 case KDSKBMODE: /* set keyboard mode */
1407 switch (*(int *)data) {
1408 case K_XLATE: /* switch to XLT ascii mode */
1409 case K_RAW: /* switch to RAW scancode mode */
1410 case K_CODE: /* switch to CODE mode */
1411 scp->kbd_mode = *(int *)data;
1412 if (scp == sc->cur_scp)
1413 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1420 case KDGKBMODE: /* get keyboard mode */
1421 *(int *)data = scp->kbd_mode;
1425 error = kbdd_ioctl(sc->kbd, cmd, data);
1426 if (error == ENOIOCTL)
1430 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1431 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1433 ival = IOCPARM_IVAL(data);
1434 data = (caddr_t)&ival;
1437 case KDMKTONE: /* sound the bell */
1439 sc_bell(scp, (*(int *)data) & 0xffff,
1440 (((*(int *)data) >> 16) & 0xffff) * hz / 1000);
1442 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1445 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1446 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1448 ival = IOCPARM_IVAL(data);
1449 data = (caddr_t)&ival;
1452 case KIOCSOUND: /* make tone (*data) hz */
1453 if (scp == sc->cur_scp) {
1455 return sc_tone(*(int *)data);
1461 case KDGKBTYPE: /* get keyboard type */
1462 error = kbdd_ioctl(sc->kbd, cmd, data);
1463 if (error == ENOIOCTL) {
1464 /* always return something? XXX */
1469 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1470 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1472 ival = IOCPARM_IVAL(data);
1473 data = (caddr_t)&ival;
1476 case KDSETLED: /* set keyboard LED status */
1477 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1479 scp->status &= ~LED_MASK;
1480 scp->status |= *(int *)data;
1481 if (scp == sc->cur_scp)
1482 update_kbd_leds(scp, scp->status);
1485 case KDGETLED: /* get keyboard LED status */
1486 if (scp == sc->cur_scp)
1487 save_kbd_state(scp);
1488 *(int *)data = scp->status & LED_MASK;
1491 case KBADDKBD: /* add/remove keyboard to/from mux */
1493 error = kbdd_ioctl(sc->kbd, cmd, data);
1494 if (error == ENOIOCTL)
1498 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1499 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1501 ival = IOCPARM_IVAL(data);
1502 data = (caddr_t)&ival;
1505 case CONS_SETKBD: /* set the new keyboard */
1510 newkbd = kbd_get_keyboard(*(int *)data);
1511 if (newkbd == NULL) {
1516 if (sc->kbd != newkbd) {
1517 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1518 (void *)&sc->kbd, sckbdevent, sc);
1519 /* i == newkbd->kb_index */
1521 if (sc->kbd != NULL) {
1522 save_kbd_state(sc->cur_scp);
1524 sc->kbd, (void *)&sc->kbd);
1527 kbd_get_keyboard(i); /* sc->kbd == newkbd */
1528 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1529 (caddr_t)&sc->cur_scp->kbd_mode);
1530 update_kbd_state(sc->cur_scp,
1531 sc->cur_scp->status, LOCK_MASK);
1533 error = EPERM; /* XXX */
1540 case CONS_RELKBD: /* release the current keyboard */
1543 if (sc->kbd != NULL) {
1544 save_kbd_state(sc->cur_scp);
1545 error = kbd_release(sc->kbd, (void *)&sc->kbd);
1552 case CONS_GETTERM: /* get the current terminal emulator info */
1556 if (((term_info_t *)data)->ti_index == 0) {
1559 sw = sc_term_match_by_number(
1560 ((term_info_t *)data)->ti_index);
1563 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1564 sizeof(((term_info_t *)data)->ti_name));
1565 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1566 sizeof(((term_info_t *)data)->ti_desc));
1567 ((term_info_t *)data)->ti_flags = 0;
1570 ((term_info_t *)data)->ti_name[0] = '\0';
1571 ((term_info_t *)data)->ti_desc[0] = '\0';
1572 ((term_info_t *)data)->ti_flags = 0;
1577 case CONS_SETTERM: /* set the current terminal emulator */
1579 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1580 /* FIXME: what if scp == sc_console! XXX */
1584 case GIO_SCRNMAP: /* get output translation table */
1585 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1588 case PIO_SCRNMAP: /* set output translation table */
1589 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1590 for (i = 0; i < sizeof(sc->scr_map); i++) {
1591 sc->scr_rmap[sc->scr_map[i]] = i;
1595 case GIO_KEYMAP: /* get keyboard translation table */
1596 case PIO_KEYMAP: /* set keyboard translation table */
1597 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
1598 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1599 case GIO_DEADKEYMAP: /* get accent key translation table */
1600 case PIO_DEADKEYMAP: /* set accent key translation table */
1601 case GETFKEY: /* get function key string */
1602 case SETFKEY: /* set function key string */
1603 error = kbdd_ioctl(sc->kbd, cmd, data);
1604 if (error == ENOIOCTL)
1608 #ifndef SC_NO_FONT_LOADING
1610 case PIO_FONT8x8: /* set 8x8 dot font */
1611 if (!ISFONTAVAIL(sc->adp->va_flags))
1613 bcopy(data, sc->font_8, 8 * 256);
1614 sc->fonts_loaded |= FONT_8;
1617 * Always use the font page #0. XXX
1618 * Don't load if the current font size is not 8x8.
1620 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1621 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1624 case GIO_FONT8x8: /* get 8x8 dot font */
1625 if (!ISFONTAVAIL(sc->adp->va_flags))
1627 if (sc->fonts_loaded & FONT_8) {
1628 bcopy(sc->font_8, data, 8 * 256);
1633 case PIO_FONT8x14: /* set 8x14 dot font */
1634 if (!ISFONTAVAIL(sc->adp->va_flags))
1636 bcopy(data, sc->font_14, 14 * 256);
1637 sc->fonts_loaded |= FONT_14;
1640 * Always use the font page #0. XXX
1641 * Don't load if the current font size is not 8x14.
1643 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 14) &&
1644 (sc->cur_scp->font_size < 16))
1646 sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1649 case GIO_FONT8x14: /* get 8x14 dot font */
1650 if (!ISFONTAVAIL(sc->adp->va_flags))
1652 if (sc->fonts_loaded & FONT_14) {
1653 bcopy(sc->font_14, data, 14 * 256);
1658 case PIO_FONT8x16: /* set 8x16 dot font */
1659 if (!ISFONTAVAIL(sc->adp->va_flags))
1661 bcopy(data, sc->font_16, 16 * 256);
1662 sc->fonts_loaded |= FONT_16;
1665 * Always use the font page #0. XXX
1666 * Don't load if the current font size is not 8x16.
1668 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1670 sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1673 case GIO_FONT8x16: /* get 8x16 dot font */
1674 if (!ISFONTAVAIL(sc->adp->va_flags))
1676 if (sc->fonts_loaded & FONT_16) {
1677 bcopy(sc->font_16, data, 16 * 256);
1682 #endif /* SC_NO_FONT_LOADING */
1693 struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1696 return sctty_ioctl(dev->si_drv1, cmd, data, td);
1700 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td)
1702 #ifndef SC_NO_SYSMOUSE
1704 memset(&info, 0, sizeof(info));
1705 info.operation = MOUSE_ACTION;
1708 * Make sure all buttons are released when moused and other
1709 * console daemons exit, so that no buttons are left pressed.
1711 (void)sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td);
1717 sc_cnprobe(struct consdev *cp)
1722 if (!vty_enabled(VTY_SC)) {
1723 cp->cn_pri = CN_DEAD;
1727 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1729 /* a video card is always required */
1730 if (!scvidprobe(unit, flags, TRUE))
1731 cp->cn_pri = CN_DEAD;
1733 /* syscons will become console even when there is no keyboard */
1734 sckbdprobe(unit, flags, TRUE);
1736 if (cp->cn_pri == CN_DEAD)
1739 /* initialize required fields */
1740 strcpy(cp->cn_name, "ttyv0");
1744 sc_cninit(struct consdev *cp)
1749 sc_get_cons_priority(&unit, &flags);
1750 scinit(unit, flags | SC_KERNEL_CONSOLE);
1751 sc_console_unit = unit;
1752 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1757 sc_cnterm(struct consdev *cp)
1762 /* we are not the kernel console any more, release everything */
1764 if (sc_console_unit < 0)
1765 return; /* shouldn't happen */
1768 sc_clear_screen(sc_console);
1769 sccnupdate(sc_console);
1772 if (sc_ktsw != NULL) {
1773 for (i = 0; i <= mp_maxid; i++) {
1776 (*sc_ktsw->te_term)(sc_console, &ts);
1781 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1782 sc_console_unit = -1;
1786 static void sccnclose(sc_softc_t *sc, struct sc_cnstate *sp);
1787 static int sc_cngetc_locked(struct sc_cnstate *sp);
1788 static void sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp);
1789 static void sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp);
1790 static void sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags);
1791 static void sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp);
1792 static void sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp);
1795 sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp)
1798 * Locking method: hope for the best.
1799 * The keyboard is supposed to be Giant locked. We can't handle that
1800 * in general. The kdb_active case here is not safe, and we will
1801 * proceed without the lock in all cases.
1803 sp->kbd_locked = !kdb_active && mtx_trylock(&Giant);
1807 sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp)
1811 sp->kbd_locked = FALSE;
1815 sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp)
1821 * - if kdb_active and video_mtx is not owned by anyone, then lock
1822 * by kdb remaining active
1823 * - if !kdb_active, try to acquire video_mtx without blocking or
1824 * recursing; if we get it then it works normally.
1825 * Note that video_mtx is especially unusable if we already own it,
1826 * since then it is protecting something and syscons is not reentrant
1827 * enough to ignore the protection even in the kdb_active case.
1830 sp->kdb_locked = sc->video_mtx.mtx_lock == MTX_UNOWNED ||
1831 SCHEDULER_STOPPED();
1832 sp->mtx_locked = FALSE;
1834 sp->kdb_locked = FALSE;
1835 for (retries = 0; retries < 1000; retries++) {
1836 sp->mtx_locked = mtx_trylock_spin_flags(
1837 &sc->video_mtx, MTX_QUIET) != 0;
1838 if (SCHEDULER_STOPPED()) {
1839 sp->kdb_locked = TRUE;
1840 sp->mtx_locked = FALSE;
1851 sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp)
1854 mtx_unlock_spin(&sc->video_mtx);
1855 sp->mtx_locked = sp->kdb_locked = FALSE;
1859 sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags)
1863 /* assert(sc_console_unit >= 0) */
1865 sp->kbd_opened = FALSE;
1866 sp->scr_opened = FALSE;
1867 sp->kbd_locked = FALSE;
1869 /* Opening the keyboard is optional. */
1870 if (!(flags & 1) || sc->kbd == NULL)
1873 sccnkbdlock(sc, sp);
1876 * Make sure the keyboard is accessible even when the kbd device
1877 * driver is disabled.
1879 kbdd_enable(sc->kbd);
1881 /* Switch the keyboard to console mode (K_XLATE, polled) on all scp's.
1884 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&kbd_mode);
1885 sc->kbd_open_level++;
1886 kbdd_poll(sc->kbd, TRUE);
1888 sp->kbd_opened = TRUE;
1891 /* The screen is opened iff locking it succeeds. */
1892 sccnscrlock(sc, sp);
1893 if (!sp->kdb_locked && !sp->mtx_locked)
1895 sp->scr_opened = TRUE;
1897 /* The screen switch is optional. */
1901 /* try to switch to the kernel console screen */
1902 if (!cold && sc->cur_scp->index != sc_console->index &&
1903 sc->cur_scp->smode.mode == VT_AUTO &&
1904 sc_console->smode.mode == VT_AUTO)
1905 sc_switch_scr(sc, sc_console->index);
1909 sccnclose(sc_softc_t *sc, struct sc_cnstate *sp)
1911 sp->scr_opened = FALSE;
1912 sccnscrunlock(sc, sp);
1914 if (!sp->kbd_opened)
1917 /* Restore keyboard mode (for the current, possibly-changed scp). */
1918 kbdd_poll(sc->kbd, FALSE);
1919 if (--sc->kbd_open_level == 0)
1921 sc->kbd, KDSKBMODE, (caddr_t)&sc->cur_scp->kbd_mode);
1923 kbdd_disable(sc->kbd);
1924 sp->kbd_opened = FALSE;
1925 sccnkbdunlock(sc, sp);
1929 * Grabbing switches the screen and keyboard focus to sc_console and the
1930 * keyboard mode to (K_XLATE, polled). Only switching to polled mode is
1931 * essential (for preventing the interrupt handler from eating input
1932 * between polls). Focus is part of the UI, and the other switches are
1933 * work just was well when they are done on every entry and exit.
1935 * Screen switches while grabbed are supported, and to maintain focus for
1936 * this ungrabbing and closing only restore the polling state and then
1937 * the keyboard mode if on the original screen.
1941 sc_cngrab(struct consdev *cp)
1946 sc = sc_console->sc;
1947 lev = atomic_fetchadd_int(&sc->grab_level, 1);
1948 if (lev >= 0 && lev < 2) {
1949 sccnopen(sc, &sc->grab_state[lev], 1 | 2);
1950 sccnscrunlock(sc, &sc->grab_state[lev]);
1951 sccnkbdunlock(sc, &sc->grab_state[lev]);
1956 sc_cnungrab(struct consdev *cp)
1961 sc = sc_console->sc;
1962 lev = atomic_load_acq_int(&sc->grab_level) - 1;
1963 if (lev >= 0 && lev < 2) {
1964 sccnkbdlock(sc, &sc->grab_state[lev]);
1965 sccnscrlock(sc, &sc->grab_state[lev]);
1966 sccnclose(sc, &sc->grab_state[lev]);
1968 atomic_add_int(&sc->grab_level, -1);
1971 static char sc_cnputc_log[0x1000];
1972 static u_int sc_cnputc_loghead;
1973 static u_int sc_cnputc_logtail;
1976 sc_cnputc(struct consdev *cd, int c)
1978 struct sc_cnstate st;
1980 scr_stat *scp = sc_console;
1982 struct sc_term_sw *oldtsw;
1983 #ifndef SC_NO_HISTORY
1987 #endif /* !SC_NO_HISTORY */
1991 /* assert(sc_console != NULL) */
1993 sccnopen(scp->sc, &st, 0);
1998 * In the unlocked case, the logging is intentionally only
1999 * perfectly atomic for the indexes.
2001 head = atomic_fetchadd_int(&sc_cnputc_loghead, 1);
2002 sc_cnputc_log[head % sizeof(sc_cnputc_log)] = c;
2005 * If we couldn't open, do special reentrant output and return to defer
2008 if (!st.scr_opened) {
2013 #ifndef SC_NO_HISTORY
2014 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
2015 scp->status &= ~SLKED;
2016 update_kbd_state(scp, scp->status, SLKED);
2017 if (scp->status & BUFFER_SAVED) {
2018 if (!sc_hist_restore(scp))
2019 sc_remove_cutmarking(scp);
2020 scp->status &= ~BUFFER_SAVED;
2021 scp->status |= CURSOR_ENABLED;
2022 sc_draw_cursor_image(scp);
2026 * XXX: Now that TTY's have their own locks, we cannot process
2027 * any data after disabling scroll lock. cnputs already holds a
2030 tp = SC_DEV(scp->sc, scp->index);
2031 /* XXX "tp" can be NULL */
2034 sctty_outwakeup(tp);
2038 #endif /* !SC_NO_HISTORY */
2040 /* Play any output still in the log (our char may already be done). */
2041 while (sc_cnputc_logtail != atomic_load_acq_int(&sc_cnputc_loghead)) {
2043 sc_cnputc_log[sc_cnputc_logtail++ % sizeof(sc_cnputc_log)];
2044 if (atomic_load_acq_int(&sc_cnputc_loghead) -
2045 sc_cnputc_logtail >=
2046 sizeof(sc_cnputc_log))
2048 /* Console output has a per-CPU "input" state. Switch for it.
2050 ts = sc_kts[curcpu];
2056 (*scp->tsw->te_sync)(scp);
2058 /* Only 1 tsw early. Switch only its attr. */
2059 (*scp->tsw->te_default_attr)(
2060 scp, sc_kattrtab[curcpu], SC_KERNEL_CONS_REV_ATTR);
2062 sc_puts(scp, buf, 1);
2066 (*scp->tsw->te_sync)(scp);
2068 (*scp->tsw->te_default_attr)(
2069 scp, SC_KERNEL_CONS_ATTR, SC_KERNEL_CONS_REV_ATTR);
2073 s = spltty(); /* block sckbdevent and scrn_timer */
2076 sccnclose(scp->sc, &st);
2080 sc_cngetc(struct consdev *cd)
2082 struct sc_cnstate st;
2085 /* assert(sc_console != NULL) */
2086 sccnopen(sc_console->sc, &st, 1);
2087 s = spltty(); /* block sckbdevent and scrn_timer while we poll */
2088 if (!st.kbd_opened) {
2090 sccnclose(sc_console->sc, &st);
2091 return -1; /* means no keyboard since we fudged the locking */
2093 c = sc_cngetc_locked(&st);
2095 sccnclose(sc_console->sc, &st);
2100 sc_cngetc_locked(struct sc_cnstate *sp)
2102 static struct fkeytab fkey;
2109 * Stop the screen saver and update the screen if necessary.
2110 * What if we have been running in the screen saver code... XXX
2113 sc_touch_scrn_saver();
2114 scp = sc_console->sc->cur_scp; /* XXX */
2118 if (fkeycp < fkey.len)
2119 return fkey.str[fkeycp++];
2121 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK, sp);
2123 switch (KEYFLAGS(c)) {
2124 case 0: /* normal char */
2126 case FKEY: /* function key */
2127 p = (*scp->tsw->te_fkeystr)(scp, c);
2129 fkey.len = strlen(p);
2130 bcopy(p, fkey.str, fkey.len);
2134 p = kbdd_get_fkeystr(
2135 scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
2137 if ((p != NULL) && (fkey.len > 0)) {
2138 bcopy(p, fkey.str, fkey.len);
2152 sccnupdate(scr_stat *scp)
2154 /* this is a cut-down version of scrn_timer()... */
2156 if (suspend_in_progress || scp->sc->font_loading_in_progress)
2159 if (kdb_active || KERNEL_PANICKED() || shutdown_in_progress) {
2160 sc_touch_scrn_saver();
2161 } else if (scp != scp->sc->cur_scp) {
2165 if (!run_scrn_saver)
2166 scp->sc->flags &= ~SC_SCRN_IDLE;
2168 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
2169 if (scp->sc->flags & SC_SCRN_BLANKED)
2170 stop_scrn_saver(scp->sc, current_saver);
2173 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress ||
2174 scp->sc->switch_in_progress)
2177 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
2178 * when write_in_progress is non-zero. XXX
2181 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
2182 scrn_update(scp, TRUE);
2186 scrn_timer(void *arg)
2188 static time_t kbd_time_stamp = 0;
2194 again = (arg != NULL);
2196 sc = (sc_softc_t *)arg;
2197 else if (sc_console != NULL)
2198 sc = sc_console->sc;
2202 /* find the vty to update */
2205 /* don't do anything when we are performing some I/O operations */
2206 if (suspend_in_progress || sc->font_loading_in_progress)
2209 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
2210 /* try to allocate a keyboard automatically */
2211 if (kbd_time_stamp != time_uptime) {
2212 kbd_time_stamp = time_uptime;
2213 kbdidx = sc_allocate_keyboard(sc, -1);
2215 sc->kbd = kbd_get_keyboard(kbdidx);
2216 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
2217 (caddr_t)&sc->cur_scp->kbd_mode);
2218 update_kbd_state(sc->cur_scp,
2219 sc->cur_scp->status, LOCK_MASK);
2224 /* should we stop the screen saver? */
2225 if (kdb_active || KERNEL_PANICKED() || shutdown_in_progress)
2226 sc_touch_scrn_saver();
2227 if (run_scrn_saver) {
2228 if (time_uptime > sc->scrn_time_stamp + scrn_blank_time)
2229 sc->flags |= SC_SCRN_IDLE;
2231 sc->flags &= ~SC_SCRN_IDLE;
2233 sc->scrn_time_stamp = time_uptime;
2234 sc->flags &= ~SC_SCRN_IDLE;
2235 if (scrn_blank_time > 0)
2236 run_scrn_saver = TRUE;
2239 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
2240 if (sc->flags & SC_SCRN_BLANKED)
2241 stop_scrn_saver(sc, current_saver);
2244 /* should we just return ? */
2245 if (sc->blink_in_progress || sc->switch_in_progress ||
2246 sc->write_in_progress)
2249 /* Update the screen */
2250 scp = sc->cur_scp; /* cur_scp may have changed... */
2251 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2252 scrn_update(scp, TRUE);
2255 /* should we activate the screen saver? */
2256 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
2257 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
2258 (*current_saver)(sc, TRUE);
2264 * Use reduced "refresh" rate if we are in graphics and that is
2265 * not a graphical screen saver. In such case we just have
2268 if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2273 &sc->ctimeout, SBT_1S / rate, 0, scrn_timer, sc, C_PREL(1));
2278 and_region(int *s1, int *e1, int s2, int e2)
2280 if (*e1 < s2 || e2 < *s1)
2282 *s1 = imax(*s1, s2);
2283 *e1 = imin(*e1, e2);
2288 scrn_update(scr_stat *scp, int show_cursor)
2295 /* assert(scp == scp->sc->cur_scp) */
2297 SC_VIDEO_LOCK(scp->sc);
2299 #ifndef SC_NO_CUTPASTE
2300 /* remove the previous mouse pointer image if necessary */
2301 if (scp->status & MOUSE_VISIBLE) {
2303 e = scp->mouse_pos + scp->xsize + 1;
2304 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN)) ||
2305 and_region(&s, &e, scp->start, scp->end) ||
2306 ((scp->status & CURSOR_ENABLED) &&
2307 (scp->cursor_pos != scp->cursor_oldpos) &&
2308 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos) ||
2309 and_region(&s, &e, scp->cursor_oldpos,
2310 scp->cursor_oldpos)))) {
2311 sc_remove_mouse_image(scp);
2312 if (scp->end >= scp->xsize * scp->ysize)
2313 scp->end = scp->xsize * scp->ysize - 1;
2316 #endif /* !SC_NO_CUTPASTE */
2320 if (scp->end >= scp->xsize * scp->ysize) {
2321 printf("scrn_update(): scp->end %d > size_of_screen!!\n",
2323 scp->end = scp->xsize * scp->ysize - 1;
2325 if (scp->start < 0) {
2326 printf("scrn_update(): scp->start %d < 0\n", scp->start);
2331 /* update screen image */
2332 if (scp->start <= scp->end) {
2333 if (scp->mouse_cut_end >= 0) {
2334 /* there is a marked region for cut & paste */
2335 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
2336 start = scp->mouse_cut_start;
2337 end = scp->mouse_cut_end;
2339 start = scp->mouse_cut_end;
2340 end = scp->mouse_cut_start - 1;
2344 /* does the cut-mark region overlap with the update
2346 if (and_region(&s, &e, scp->start, scp->end)) {
2347 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2350 if (and_region(&s, &e, scp->start, scp->end))
2352 scp, s, e - s + 1, FALSE);
2354 e = scp->xsize * scp->ysize - 1;
2355 if (and_region(&s, &e, scp->start, scp->end))
2357 scp, s, e - s + 1, FALSE);
2359 (*scp->rndr->draw)(scp, scp->start,
2360 scp->end - scp->start + 1, FALSE);
2364 scp, scp->start, scp->end - scp->start + 1, FALSE);
2368 /* we are not to show the cursor and the mouse pointer... */
2371 scp->start = scp->xsize * scp->ysize - 1;
2372 SC_VIDEO_UNLOCK(scp->sc);
2376 /* update cursor image */
2377 if (scp->status & CURSOR_ENABLED) {
2380 /* did cursor move since last time ? */
2381 if (scp->cursor_pos != scp->cursor_oldpos) {
2382 /* do we need to remove old cursor image ? */
2384 &s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2385 sc_remove_cursor_image(scp);
2386 sc_draw_cursor_image(scp);
2389 &s, &e, scp->cursor_pos, scp->cursor_pos))
2390 /* cursor didn't move, but has been overwritten
2392 sc_draw_cursor_image(scp);
2393 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
2394 /* if it's a blinking cursor, update it */
2395 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2396 sc_inside_cutmark(scp, scp->cursor_pos));
2400 #ifndef SC_NO_CUTPASTE
2401 /* update "pseudo" mouse pointer image */
2402 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2403 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2404 scp->status &= ~MOUSE_MOVED;
2405 sc_draw_mouse_image(scp);
2408 #endif /* SC_NO_CUTPASTE */
2411 scp->start = scp->xsize * scp->ysize - 1;
2413 SC_VIDEO_UNLOCK(scp->sc);
2418 scsplash_callback(int event, void *arg)
2423 sc = (sc_softc_t *)arg;
2427 if (add_scrn_saver(scsplash_saver) == 0) {
2428 sc->flags &= ~SC_SAVER_FAILED;
2429 run_scrn_saver = TRUE;
2430 if (cold && !(boothowto & RB_VERBOSE)) {
2431 scsplash_stick(TRUE);
2432 (*current_saver)(sc, TRUE);
2438 if (current_saver == scsplash_saver) {
2439 scsplash_stick(FALSE);
2440 error = remove_scrn_saver(scsplash_saver);
2452 scsplash_saver(sc_softc_t *sc, int show)
2454 static int busy = FALSE;
2463 if (!(sc->flags & SC_SAVER_FAILED)) {
2464 if (!(sc->flags & SC_SCRN_BLANKED))
2465 set_scrn_saver_mode(scp, -1, NULL, 0);
2466 switch (splash(sc->adp, TRUE)) {
2467 case 0: /* succeeded */
2469 case EAGAIN: /* try later */
2470 restore_scrn_saver_mode(scp, FALSE);
2471 sc_touch_scrn_saver(); /* XXX */
2474 sc->flags |= SC_SAVER_FAILED;
2475 scsplash_stick(FALSE);
2476 restore_scrn_saver_mode(scp, TRUE);
2478 "scsplash_saver(): failed to put up the image\n");
2482 } else if (!sticky_splash) {
2483 if ((sc->flags & SC_SCRN_BLANKED) &&
2484 (splash(sc->adp, FALSE) == 0))
2485 restore_scrn_saver_mode(scp, TRUE);
2491 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2496 if (current_saver != none_saver) {
2497 error = remove_scrn_saver(current_saver);
2502 if (current_saver != none_saver)
2505 run_scrn_saver = FALSE;
2506 saver_mode = CONS_LKM_SAVER;
2507 current_saver = this_saver;
2512 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2514 if (current_saver != this_saver)
2519 * In order to prevent `current_saver' from being called by
2520 * the timeout routine `scrn_timer()' while we manipulate
2521 * the saver list, we shall set `current_saver' to `none_saver'
2522 * before stopping the current saver, rather than blocking by `splXX()'.
2524 current_saver = none_saver;
2526 stop_scrn_saver(this_saver);
2529 /* unblank all blanked screens */
2530 wait_scrn_saver_stop(NULL);
2534 current_saver = none_saver;
2539 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2543 /* assert(scp == scp->sc->cur_scp) */
2545 if (!ISGRAPHSC(scp))
2546 sc_remove_cursor_image(scp);
2547 scp->splash_save_mode = scp->mode;
2548 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2549 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2550 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2551 scp->sc->flags |= SC_SCRN_BLANKED;
2557 if (set_mode(scp) == 0) {
2558 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2559 scp->status |= GRAPHICS_MODE;
2560 #ifndef SC_NO_PALETTE_LOADING
2562 vidd_load_palette(scp->sc->adp, pal);
2564 sc_set_border(scp, border);
2568 scp->mode = scp->splash_save_mode;
2569 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2570 scp->status |= scp->splash_save_status;
2577 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2583 /* assert(scp == scp->sc->cur_scp) */
2586 status = scp->status;
2587 scp->mode = scp->splash_save_mode;
2588 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2589 scp->status |= scp->splash_save_status;
2590 scp->sc->flags &= ~SC_SCRN_BLANKED;
2592 if (!ISGRAPHSC(scp))
2593 sc_draw_cursor_image(scp);
2598 if (set_mode(scp) == 0) {
2599 #ifndef SC_NO_PALETTE_LOADING
2600 #ifdef SC_PIXEL_MODE
2601 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2602 vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2605 vidd_load_palette(scp->sc->adp, scp->sc->palette);
2612 scp->status = status;
2619 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2621 (*saver)(sc, FALSE);
2622 run_scrn_saver = FALSE;
2623 /* the screen saver may have chosen not to stop after all... */
2624 if (sc->flags & SC_SCRN_BLANKED)
2627 mark_all(sc->cur_scp);
2628 if (sc->delayed_next_scr)
2629 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2631 wakeup(&scrn_blanked);
2635 wait_scrn_saver_stop(sc_softc_t *sc)
2639 while (scrn_blanked > 0) {
2640 run_scrn_saver = FALSE;
2641 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2645 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2646 if ((error != 0) && (error != ERESTART))
2649 run_scrn_saver = FALSE;
2652 #endif /* DEV_SPLASH */
2655 sc_touch_scrn_saver(void)
2657 scsplash_stick(FALSE);
2658 run_scrn_saver = FALSE;
2662 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2669 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2671 if (sc->cur_scp == NULL)
2674 /* prevent switch if previously requested */
2675 if (sc->flags & SC_SCRN_VTYLOCK) {
2676 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2677 sc->cur_scp->bell_duration);
2681 /* delay switch if the screen is blanked or being updated */
2682 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress ||
2683 sc->blink_in_progress) {
2684 sc->delayed_next_scr = next_scr + 1;
2685 sc_touch_scrn_saver();
2686 DPRINTF(5, ("switch delayed\n"));
2689 sc->delayed_next_scr = 0;
2692 cur_scp = sc->cur_scp;
2694 /* we are in the middle of the vty switching process... */
2695 if (sc->switch_in_progress && (cur_scp->smode.mode == VT_PROCESS) &&
2697 p = pfind(cur_scp->pid);
2698 if (cur_scp->proc != p) {
2702 * The controlling process has died!!. Do some clean
2703 * up. NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2704 * are not reset here yet; they will be cleared later.
2707 ("cur_scp controlling process %d died, ",
2709 if (cur_scp->status & SWITCH_WAIT_REL) {
2711 * Force the previous switch to finish, but
2712 * return now with error.
2714 DPRINTF(5, ("reset WAIT_REL, "));
2715 finish_vt_rel(cur_scp, TRUE, &s);
2717 DPRINTF(5, ("finishing previous switch\n"));
2719 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2720 /* let's assume screen switch has been
2722 DPRINTF(5, ("reset WAIT_ACQ, "));
2723 finish_vt_acq(cur_scp);
2726 * We are in between screen release and
2727 * acquisition, and reached here via scgetc() or
2728 * scrn_timer() which has interrupted
2729 * exchange_scr(). Don't do anything stupid.
2731 DPRINTF(5, ("waiting nothing, "));
2737 * The controlling process is alive, but not
2738 * responding... It is either buggy or it may be just
2739 * taking time. The following code is a gross kludge to
2740 * cope with this problem for which there is no clean
2743 if (cur_scp->status & SWITCH_WAIT_REL) {
2744 switch (sc->switch_in_progress++) {
2748 DPRINTF(5, ("sending relsig again, "));
2749 signal_vt_rel(cur_scp);
2756 * Act as if the controlling program
2757 * returned VT_FALSE.
2759 DPRINTF(5, ("force reset WAIT_REL, "));
2760 finish_vt_rel(cur_scp, FALSE, &s);
2763 ("act as if VT_FALSE was seen\n"));
2766 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2767 switch (sc->switch_in_progress++) {
2771 DPRINTF(5, ("sending acqsig again, "));
2772 signal_vt_acq(cur_scp);
2778 /* clear the flag and finish the
2779 * previous switch */
2780 DPRINTF(5, ("force reset WAIT_ACQ, "));
2781 finish_vt_acq(cur_scp);
2789 * Return error if an invalid argument is given, or vty switch
2790 * is still in progress.
2792 if ((next_scr < sc->first_vty) ||
2793 (next_scr >= sc->first_vty + sc->vtys) || sc->switch_in_progress) {
2795 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2796 DPRINTF(5, ("error 1\n"));
2801 * Don't allow switching away from the graphics mode vty
2802 * if the switch mode is VT_AUTO, unless the next vty is the same
2803 * as the current or the current vty has been closed (but showing).
2805 tp = SC_DEV(sc, cur_scp->index);
2806 if ((cur_scp->index != next_scr) && tty_opened_ns(tp) &&
2807 (cur_scp->smode.mode == VT_AUTO) && ISGRAPHSC(cur_scp)) {
2809 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2810 DPRINTF(5, ("error, graphics mode\n"));
2815 * Is the wanted vty open? Don't allow switching to a closed vty.
2816 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2817 * Note that we always allow the user to switch to the kernel
2818 * console even if it is closed.
2820 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2821 tp = SC_DEV(sc, next_scr);
2822 if (!tty_opened_ns(tp)) {
2824 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2825 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2828 if (kdb_active && SC_STAT(tp)->smode.mode == VT_PROCESS) {
2831 ("error 3, requested vty is in the VT_PROCESS mode\n"));
2836 /* this is the start of vty switching process... */
2837 ++sc->switch_in_progress;
2838 sc->old_scp = cur_scp;
2839 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2840 if (sc->new_scp == sc->old_scp) {
2841 sc->switch_in_progress = 0;
2843 * XXX wakeup() locks the scheduler lock which will hang if
2844 * the lock is in an in-between state, e.g., when we stop at
2845 * a breakpoint at fork_exit. It has always been wrong to call
2846 * wakeup() when the debugger is active. In RELENG_4, wakeup()
2847 * is supposed to be locked by splhigh(), but the debugger may
2848 * be invoked at splhigh().
2851 wakeup(VTY_WCHAN(sc, next_scr));
2853 DPRINTF(5, ("switch done (new == old)\n"));
2857 /* has controlling process died? */
2858 vt_proc_alive(sc->old_scp);
2859 vt_proc_alive(sc->new_scp);
2861 /* wait for the controlling process to release the screen, if necessary
2863 if (signal_vt_rel(sc->old_scp)) {
2868 /* go set up the new vty screen */
2873 /* wake up processes waiting for this vty */
2875 wakeup(VTY_WCHAN(sc, next_scr));
2877 /* wait for the controlling process to acknowledge, if necessary */
2878 if (signal_vt_acq(sc->cur_scp)) {
2883 sc->switch_in_progress = 0;
2884 if (sc->unit == sc_console_unit)
2885 cnavailable(sc_consptr, TRUE);
2887 DPRINTF(5, ("switch done\n"));
2893 do_switch_scr(sc_softc_t *sc, int s)
2895 vt_proc_alive(sc->new_scp);
2900 /* sc->cur_scp == sc->new_scp */
2901 wakeup(VTY_WCHAN(sc, sc->cur_scp->index));
2903 /* wait for the controlling process to acknowledge, if necessary */
2904 if (!signal_vt_acq(sc->cur_scp)) {
2905 sc->switch_in_progress = 0;
2906 if (sc->unit == sc_console_unit)
2907 cnavailable(sc_consptr, TRUE);
2914 vt_proc_alive(scr_stat *scp)
2919 if ((p = pfind(scp->pid)) != NULL)
2924 scp->smode.mode = VT_AUTO;
2925 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2931 signal_vt_rel(scr_stat *scp)
2933 if (scp->smode.mode != VT_PROCESS)
2935 scp->status |= SWITCH_WAIT_REL;
2936 PROC_LOCK(scp->proc);
2937 kern_psignal(scp->proc, scp->smode.relsig);
2938 PROC_UNLOCK(scp->proc);
2939 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2944 signal_vt_acq(scr_stat *scp)
2946 if (scp->smode.mode != VT_PROCESS)
2948 if (scp->sc->unit == sc_console_unit)
2949 cnavailable(sc_consptr, FALSE);
2950 scp->status |= SWITCH_WAIT_ACQ;
2951 PROC_LOCK(scp->proc);
2952 kern_psignal(scp->proc, scp->smode.acqsig);
2953 PROC_UNLOCK(scp->proc);
2954 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2959 finish_vt_rel(scr_stat *scp, int release, int *s)
2961 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2962 scp->status &= ~SWITCH_WAIT_REL;
2964 *s = do_switch_scr(scp->sc, *s);
2966 scp->sc->switch_in_progress = 0;
2973 finish_vt_acq(scr_stat *scp)
2975 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2976 scp->status &= ~SWITCH_WAIT_ACQ;
2977 scp->sc->switch_in_progress = 0;
2984 exchange_scr(sc_softc_t *sc)
2988 /* save the current state of video and keyboard */
2989 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2990 if (!ISGRAPHSC(sc->old_scp))
2991 sc_remove_cursor_image(sc->old_scp);
2992 if (sc->old_scp->kbd_mode == K_XLATE)
2993 save_kbd_state(sc->old_scp);
2995 /* set up the video for the new screen */
2996 scp = sc->cur_scp = sc->new_scp;
2997 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
3000 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3001 (void *)sc->adp->va_window, FALSE);
3002 scp->status |= MOUSE_HIDDEN;
3003 sc_move_cursor(scp, scp->xpos, scp->ypos);
3004 if (!ISGRAPHSC(scp))
3005 sc_set_cursor_image(scp);
3006 #ifndef SC_NO_PALETTE_LOADING
3007 if (ISGRAPHSC(sc->old_scp)) {
3008 #ifdef SC_PIXEL_MODE
3009 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
3010 vidd_load_palette(sc->adp, sc->palette2);
3013 vidd_load_palette(sc->adp, sc->palette);
3016 sc_set_border(scp, scp->border);
3018 /* set up the keyboard for the new screen */
3019 if (sc->kbd_open_level == 0 && sc->old_scp->kbd_mode != scp->kbd_mode)
3020 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
3021 update_kbd_state(scp, scp->status, LOCK_MASK);
3027 sc_puts(scr_stat *scp, u_char *buf, int len)
3030 /* make screensaver happy */
3031 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
3032 run_scrn_saver = FALSE;
3036 (*scp->tsw->te_puts)(scp, buf, len);
3037 if (scp->sc->delayed_next_scr)
3038 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3042 sc_draw_cursor_image(scr_stat *scp)
3044 /* assert(scp == scp->sc->cur_scp); */
3045 SC_VIDEO_LOCK(scp->sc);
3046 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
3047 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
3048 sc_inside_cutmark(scp, scp->cursor_pos));
3049 scp->cursor_oldpos = scp->cursor_pos;
3050 SC_VIDEO_UNLOCK(scp->sc);
3054 sc_remove_cursor_image(scr_stat *scp)
3056 /* assert(scp == scp->sc->cur_scp); */
3057 SC_VIDEO_LOCK(scp->sc);
3058 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
3059 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
3060 sc_inside_cutmark(scp, scp->cursor_oldpos));
3061 SC_VIDEO_UNLOCK(scp->sc);
3065 update_cursor_image(scr_stat *scp)
3067 /* assert(scp == scp->sc->cur_scp); */
3068 sc_remove_cursor_image(scp);
3069 sc_set_cursor_image(scp);
3070 sc_draw_cursor_image(scp);
3074 sc_set_cursor_image(scr_stat *scp)
3076 scp->curs_attr = scp->base_curs_attr;
3077 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
3078 /* hidden cursor is internally represented as zero-height
3080 scp->curs_attr.flags = CONS_CHAR_CURSOR;
3081 scp->curs_attr.base = scp->curs_attr.height = 0;
3082 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
3083 scp->curs_attr.base =
3084 imin(scp->base_curs_attr.base, scp->font_size - 1);
3085 scp->curs_attr.height = imin(scp->base_curs_attr.height,
3086 scp->font_size - scp->curs_attr.base);
3087 } else { /* block cursor */
3088 scp->curs_attr.base = 0;
3089 scp->curs_attr.height = scp->font_size;
3092 /* assert(scp == scp->sc->cur_scp); */
3093 SC_VIDEO_LOCK(scp->sc);
3094 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base,
3095 scp->curs_attr.height, scp->curs_attr.flags & CONS_BLINK_CURSOR);
3096 SC_VIDEO_UNLOCK(scp->sc);
3100 sc_adjust_ca(struct cursor_attr *cap, int flags, int base, int height)
3102 if (flags & CONS_CHARCURSOR_COLORS) {
3103 cap->bg[0] = base & 0xff;
3104 cap->bg[1] = height & 0xff;
3105 } else if (flags & CONS_MOUSECURSOR_COLORS) {
3106 cap->mouse_ba = base & 0xff;
3107 cap->mouse_ia = height & 0xff;
3112 cap->height = height;
3113 if (!(flags & CONS_SHAPEONLY_CURSOR))
3114 cap->flags = flags & CONS_CURSOR_ATTRS;
3119 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
3121 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
3122 sc_remove_cursor_image(scp);
3124 if (flags & CONS_RESET_CURSOR)
3125 scp->base_curs_attr = scp->dflt_curs_attr;
3126 else if (flags & CONS_DEFAULT_CURSOR) {
3127 sc_adjust_ca(&scp->dflt_curs_attr, flags, base, height);
3128 scp->base_curs_attr = scp->dflt_curs_attr;
3130 sc_adjust_ca(&scp->base_curs_attr, flags, base, height);
3132 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
3133 sc_set_cursor_image(scp);
3134 sc_draw_cursor_image(scp);
3139 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
3147 flags = CONS_SHAPEONLY_CURSOR;
3150 if (flags & CONS_LOCAL_CURSOR) {
3151 /* local (per vty) change */
3152 change_cursor_shape(scp, flags, base, height);
3159 if (flags & CONS_RESET_CURSOR)
3160 sc->curs_attr = sc->dflt_curs_attr;
3161 else if (flags & CONS_DEFAULT_CURSOR) {
3162 sc_adjust_ca(&sc->dflt_curs_attr, flags, base, height);
3163 sc->curs_attr = sc->dflt_curs_attr;
3165 sc_adjust_ca(&sc->curs_attr, flags, base, height);
3167 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
3168 if ((tp = SC_DEV(sc, i)) == NULL)
3170 if ((scp = sc_get_stat(tp)) == NULL)
3172 scp->dflt_curs_attr = sc->curs_attr;
3173 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
3179 scinit(int unit, int flags)
3183 * When syscons is being initialized as the kernel console, malloc()
3184 * is not yet functional, because various kernel structures has not been
3185 * fully initialized yet. Therefore, we need to declare the following
3186 * static buffers for the console. This is less than ideal,
3187 * but is necessry evil for the time being. XXX
3189 static u_short sc_buffer[ROW * COL]; /* XXX */
3190 #ifndef SC_NO_FONT_LOADING
3191 static u_char font_8[256 * 8];
3192 static u_char font_14[256 * 14];
3193 static u_char font_16[256 * 16];
3196 #ifdef SC_KERNEL_CONS_ATTRS
3197 static const u_char dflt_kattrtab[] = SC_KERNEL_CONS_ATTRS;
3198 #elif SC_KERNEL_CONS_ATTR == FG_WHITE
3199 static const u_char dflt_kattrtab[] = {
3211 static const u_char dflt_kattrtab[] = {
3218 video_adapter_t *adp;
3224 /* one time initialization */
3225 if (init_done == COLD) {
3226 sc_get_bios_values(&bios_value);
3227 for (i = 0; i < nitems(sc_kattrtab); i++)
3229 dflt_kattrtab[i % (nitems(dflt_kattrtab) - 1)];
3234 * Allocate resources. Even if we are being called for the second
3235 * time, we must allocate them again, because they might have
3238 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3239 if ((sc->flags & SC_INIT_DONE) == 0)
3240 SC_VIDEO_LOCKINIT(sc);
3243 if (sc->adapter >= 0) {
3244 vid_release(sc->adp, (void *)&sc->adapter);
3248 if (sc->kbd != NULL) {
3249 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit,
3250 sc->kbd->kb_index));
3251 i = kbd_release(sc->kbd, (void *)&sc->kbd);
3252 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
3253 if (sc->kbd != NULL) {
3255 ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
3256 unit, sc->kbd->kb_index, sc->kbd->kb_unit,
3257 sc->kbd->kb_flags));
3261 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
3262 sc->adp = vid_get_adapter(sc->adapter);
3263 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
3265 kbdidx = sc_allocate_keyboard(sc, unit);
3266 DPRINTF(1, ("sc%d: keyboard %d\n", unit, kbdidx));
3268 sc->kbd = kbd_get_keyboard(kbdidx);
3269 if (sc->kbd != NULL) {
3271 ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n", unit,
3272 sc->kbd->kb_index, sc->kbd->kb_unit,
3273 sc->kbd->kb_flags));
3276 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
3277 sc->initial_mode = sc->adp->va_initial_mode;
3279 #ifndef SC_NO_FONT_LOADING
3280 if (flags & SC_KERNEL_CONSOLE) {
3281 sc->font_8 = font_8;
3282 sc->font_14 = font_14;
3283 sc->font_16 = font_16;
3284 } else if (sc->font_8 == NULL) {
3285 /* assert(sc_malloc) */
3286 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
3288 malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
3290 malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
3294 /* extract the hardware cursor location and hide the cursor for
3296 vidd_read_hw_cursor(sc->adp, &col, &row);
3297 vidd_set_hw_cursor(sc->adp, -1, -1);
3299 /* set up the first console */
3300 sc->first_vty = unit * MAXCONS;
3301 sc->vtys = MAXCONS; /* XXX: should be configurable */
3302 if (flags & SC_KERNEL_CONSOLE) {
3304 * Set up devs structure but don't use it yet, calling
3305 * make_dev() might panic kernel. Wait for
3306 * sc_attach_unit() to actually create the devices.
3308 sc->dev = main_devs;
3309 scp = &main_console;
3310 init_scp(sc, sc->first_vty, scp);
3311 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize,
3312 scp->ysize, (void *)sc_buffer, FALSE);
3313 if (sc_init_emulator(scp, SC_DFLT_TERM))
3314 sc_init_emulator(scp, "*");
3315 (*scp->tsw->te_default_attr)(
3316 scp, SC_KERNEL_CONS_ATTR, SC_KERNEL_CONS_REV_ATTR);
3318 /* assert(sc_malloc) */
3319 sc->dev = malloc(sizeof(struct tty *) * sc->vtys,
3320 M_DEVBUF, M_WAITOK | M_ZERO);
3321 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
3322 scp = alloc_scp(sc, sc->first_vty);
3323 SC_STAT(sc->dev[0]) = scp;
3327 /* copy screen to temporary buffer */
3328 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3329 (void *)scp->sc->adp->va_window, FALSE);
3331 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0,
3332 scp->xsize * scp->ysize);
3334 /* Sync h/w cursor position to s/w (sc and teken). */
3335 if (col >= scp->xsize)
3337 if (row >= scp->ysize)
3338 row = scp->ysize - 1;
3341 scp->cursor_pos = scp->cursor_oldpos = row * scp->xsize + col;
3342 (*scp->tsw->te_sync)(scp);
3344 sc->dflt_curs_attr.base = 0;
3345 sc->dflt_curs_attr.height = howmany(scp->font_size, 8);
3346 sc->dflt_curs_attr.flags = 0;
3347 sc->dflt_curs_attr.bg[0] = FG_RED;
3348 sc->dflt_curs_attr.bg[1] = FG_LIGHTGREY;
3349 sc->dflt_curs_attr.bg[2] = FG_BLUE;
3350 sc->dflt_curs_attr.mouse_ba = FG_WHITE;
3351 sc->dflt_curs_attr.mouse_ia = FG_RED;
3352 sc->curs_attr = sc->dflt_curs_attr;
3353 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3354 scp->curs_attr = scp->base_curs_attr;
3356 #ifndef SC_NO_SYSMOUSE
3357 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3359 if (!ISGRAPHSC(scp)) {
3360 sc_set_cursor_image(scp);
3361 sc_draw_cursor_image(scp);
3364 /* save font and palette */
3365 #ifndef SC_NO_FONT_LOADING
3366 sc->fonts_loaded = 0;
3367 if (ISFONTAVAIL(sc->adp->va_flags)) {
3369 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
3370 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
3371 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
3372 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
3373 if (scp->font_size < 14) {
3374 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3375 } else if (scp->font_size >= 16) {
3377 scp, 0, 16, 8, sc->font_16, 0, 256);
3380 scp, 0, 14, 8, sc->font_14, 0, 256);
3382 #else /* !SC_DFLT_FONT */
3383 if (scp->font_size < 14) {
3384 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3385 sc->fonts_loaded = FONT_8;
3386 } else if (scp->font_size >= 16) {
3388 scp, 0, 16, 8, sc->font_16, 0, 256);
3389 sc->fonts_loaded = FONT_16;
3392 scp, 0, 14, 8, sc->font_14, 0, 256);
3393 sc->fonts_loaded = FONT_14;
3395 #endif /* SC_DFLT_FONT */
3396 /* FONT KLUDGE: always use the font page #0. XXX */
3397 sc_show_font(scp, 0);
3399 #endif /* !SC_NO_FONT_LOADING */
3401 #ifndef SC_NO_PALETTE_LOADING
3402 vidd_save_palette(sc->adp, sc->palette);
3403 #ifdef SC_PIXEL_MODE
3404 for (i = 0; i < sizeof(sc->palette2); i++)
3405 sc->palette2[i] = i / 3;
3410 if (!(sc->flags & SC_SPLASH_SCRN)) {
3411 /* we are ready to put up the splash image! */
3412 splash_init(sc->adp, scsplash_callback, sc);
3413 sc->flags |= SC_SPLASH_SCRN;
3418 /* the rest is not necessary, if we have done it once */
3419 if (sc->flags & SC_INIT_DONE)
3422 /* initialize mapscrn arrays to a one to one map */
3423 for (i = 0; i < sizeof(sc->scr_map); i++)
3424 sc->scr_map[i] = sc->scr_rmap[i] = i;
3426 sc->flags |= SC_INIT_DONE;
3430 scterm(int unit, int flags)
3435 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3437 return; /* shouldn't happen */
3440 /* this console is no longer available for the splash screen */
3441 if (sc->flags & SC_SPLASH_SCRN) {
3442 splash_term(sc->adp);
3443 sc->flags &= ~SC_SPLASH_SCRN;
3448 /* move the hardware cursor to the upper-left corner */
3449 vidd_set_hw_cursor(sc->adp, 0, 0);
3452 /* release the keyboard and the video card */
3453 if (sc->kbd != NULL)
3454 kbd_release(sc->kbd, &sc->kbd);
3455 if (sc->adapter >= 0)
3456 vid_release(sc->adp, &sc->adapter);
3458 /* stop the terminal emulator, if any */
3459 scp = sc_get_stat(sc->dev[0]);
3461 (*scp->tsw->te_term)(scp, &scp->ts);
3462 mtx_destroy(&sc->video_mtx);
3464 /* clear the structure */
3465 if (!(flags & SC_KERNEL_CONSOLE)) {
3466 free(scp->ts, M_DEVBUF);
3467 /* XXX: We need delete_dev() for this */
3468 free(sc->dev, M_DEVBUF);
3470 /* XXX: We need a ttyunregister for this */
3471 free(sc->tty, M_DEVBUF);
3473 #ifndef SC_NO_FONT_LOADING
3474 free(sc->font_8, M_DEVBUF);
3475 free(sc->font_14, M_DEVBUF);
3476 free(sc->font_16, M_DEVBUF);
3478 /* XXX vtb, history */
3480 bzero(sc, sizeof(*sc));
3485 scshutdown(__unused void *arg, __unused int howto)
3488 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3489 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3490 KASSERT(sc_console->sc->cur_scp != NULL,
3491 ("sc_console->sc->cur_scp != NULL"));
3493 sc_touch_scrn_saver();
3494 if (!cold && sc_console->sc->cur_scp->index != sc_console->index &&
3495 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3496 sc_console->smode.mode == VT_AUTO)
3497 sc_switch_scr(sc_console->sc, sc_console->index);
3498 shutdown_in_progress = TRUE;
3502 scsuspend(__unused void *arg)
3506 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3507 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3508 KASSERT(sc_console->sc->cur_scp != NULL,
3509 ("sc_console->sc->cur_scp != NULL"));
3511 sc_susp_scr = sc_console->sc->cur_scp->index;
3512 if (sc_no_suspend_vtswitch || sc_susp_scr == sc_console->index) {
3513 sc_touch_scrn_saver();
3517 for (retry = 0; retry < 10; retry++) {
3518 sc_switch_scr(sc_console->sc, sc_console->index);
3519 if (!sc_console->sc->switch_in_progress)
3521 pause("scsuspend", hz);
3523 suspend_in_progress = TRUE;
3527 scresume(__unused void *arg)
3530 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3531 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3532 KASSERT(sc_console->sc->cur_scp != NULL,
3533 ("sc_console->sc->cur_scp != NULL"));
3535 suspend_in_progress = FALSE;
3536 if (sc_susp_scr < 0) {
3537 update_font(sc_console->sc->cur_scp);
3540 sc_switch_scr(sc_console->sc, sc_susp_scr);
3544 sc_clean_up(scr_stat *scp)
3550 if (scp->sc->flags & SC_SCRN_BLANKED) {
3551 sc_touch_scrn_saver();
3553 if ((error = wait_scrn_saver_stop(scp->sc)))
3557 scp->status |= MOUSE_HIDDEN;
3558 sc_remove_mouse_image(scp);
3559 sc_remove_cutmarking(scp);
3564 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3570 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3571 if (!discard && (old.vtb_flags & VTB_VALID)) {
3572 /* retain the current cursor position and buffer contants */
3573 scp->cursor_oldpos = scp->cursor_pos;
3575 * This works only if the old buffer has the same size as or
3576 * larger than the new one. XXX
3578 sc_vtb_copy(&old, 0, &new, 0, scp->xsize * scp->ysize);
3582 sc_vtb_destroy(&old);
3585 #ifndef SC_NO_SYSMOUSE
3586 /* move the mouse cursor at the center of the screen */
3587 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3592 alloc_scp(sc_softc_t *sc, int vty)
3596 /* assert(sc_malloc) */
3598 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3599 init_scp(sc, vty, scp);
3601 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3602 if (sc_init_emulator(scp, SC_DFLT_TERM))
3603 sc_init_emulator(scp, "*");
3605 #ifndef SC_NO_CUTPASTE
3606 sc_alloc_cut_buffer(scp, TRUE);
3609 #ifndef SC_NO_HISTORY
3610 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3617 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3621 bzero(scp, sizeof(*scp));
3626 scp->mode = sc->initial_mode;
3627 vidd_get_info(sc->adp, scp->mode, &info);
3628 if (info.vi_flags & V_INFO_GRAPHICS) {
3629 scp->status |= GRAPHICS_MODE;
3630 scp->xpixel = info.vi_width;
3631 scp->ypixel = info.vi_height;
3632 scp->xsize = info.vi_width / info.vi_cwidth;
3633 scp->ysize = info.vi_height / info.vi_cheight;
3637 scp->xsize = info.vi_width;
3638 scp->ysize = info.vi_height;
3639 scp->xpixel = scp->xsize * info.vi_cwidth;
3640 scp->ypixel = scp->ysize * info.vi_cheight;
3643 scp->font_size = info.vi_cheight;
3644 scp->font_width = info.vi_cwidth;
3645 #ifndef SC_NO_FONT_LOADING
3646 if (info.vi_cheight < 14)
3647 scp->font = sc->font_8;
3648 else if (info.vi_cheight >= 16)
3649 scp->font = sc->font_16;
3651 scp->font = sc->font_14;
3656 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3657 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3658 scp->xoff = scp->yoff = 0;
3659 scp->xpos = scp->ypos = 0;
3660 scp->start = scp->xsize * scp->ysize - 1;
3665 scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3666 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3667 scp->mouse_cut_start = scp->xsize * scp->ysize;
3668 scp->mouse_cut_end = -1;
3669 scp->mouse_signal = 0;
3671 scp->mouse_proc = NULL;
3672 scp->kbd_mode = K_XLATE;
3673 scp->bell_pitch = bios_value.bell_pitch;
3674 scp->bell_duration = BELL_DURATION;
3675 scp->status |= (bios_value.shift_state & NLKED);
3676 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3679 scp->smode.mode = VT_AUTO;
3680 scp->history = NULL;
3681 scp->history_pos = 0;
3682 scp->history_size = 0;
3686 sc_init_emulator(scr_stat *scp, char *name)
3693 if (name == NULL) /* if no name is given, use the current emulator */
3695 else /* ...otherwise find the named emulator */
3696 sw = sc_term_match(name);
3701 if (strcmp(sw->te_renderer, "*") != 0) {
3702 rndr = sc_render_match(scp, sw->te_renderer,
3703 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3706 rndr = sc_render_match(scp, scp->sc->adp->va_name,
3707 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3712 if (sw == scp->tsw) {
3713 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3715 scp->rndr->init(scp);
3716 sc_clear_screen(scp);
3717 /* assert(error == 0); */
3721 if (sc_malloc && (sw->te_size > 0))
3722 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3725 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3730 (*scp->tsw->te_term)(scp, &scp->ts);
3731 if (scp->ts != NULL)
3732 free(scp->ts, M_DEVBUF);
3736 scp->rndr->init(scp);
3738 (*sw->te_default_attr)(scp, SC_NORM_ATTR, SC_NORM_REV_ATTR);
3739 sc_clear_screen(scp);
3745 * scgetc(flags) - get character from keyboard.
3746 * If flags & SCGETC_CN, then avoid harmful side effects.
3747 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3748 * return NOKEY if there is nothing there.
3751 scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp)
3754 #ifndef SC_NO_HISTORY
3762 if (sc->kbd == NULL)
3767 /* I don't like this, but... XXX */
3768 if (flags & SCGETC_CN)
3769 sccnupdate(sc->cur_scp);
3772 /* first see if there is something in the keyboard port */
3774 if (flags & SCGETC_CN)
3775 sccnscrunlock(sc, sp);
3776 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3777 if (flags & SCGETC_CN)
3778 sccnscrlock(sc, sp);
3780 if (!(flags & SCGETC_CN))
3782 scp, bios_value.bell_pitch, BELL_DURATION);
3783 } else if (c == NOKEY)
3789 /* make screensaver happy */
3791 sc_touch_scrn_saver();
3793 if (!(flags & SCGETC_CN))
3794 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD);
3796 if (sc->kbd_open_level == 0 && scp->kbd_mode != K_XLATE)
3799 /* if scroll-lock pressed allow history browsing */
3800 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3801 scp->status &= ~CURSOR_ENABLED;
3802 sc_remove_cursor_image(scp);
3804 #ifndef SC_NO_HISTORY
3805 if (!(scp->status & BUFFER_SAVED)) {
3806 scp->status |= BUFFER_SAVED;
3810 /* FIXME: key codes */
3811 case SPCLKEY | FKEY | F(49): /* home key */
3812 sc_remove_cutmarking(scp);
3816 case SPCLKEY | FKEY | F(57): /* end key */
3817 sc_remove_cutmarking(scp);
3821 case SPCLKEY | FKEY | F(50): /* up arrow key */
3822 sc_remove_cutmarking(scp);
3823 if (sc_hist_up_line(scp))
3824 if (!(flags & SCGETC_CN))
3825 sc_bell(scp, bios_value.bell_pitch,
3829 case SPCLKEY | FKEY | F(58): /* down arrow key */
3830 sc_remove_cutmarking(scp);
3831 if (sc_hist_down_line(scp))
3832 if (!(flags & SCGETC_CN))
3833 sc_bell(scp, bios_value.bell_pitch,
3837 case SPCLKEY | FKEY | F(51): /* page up key */
3838 sc_remove_cutmarking(scp);
3839 for (i = 0; i < scp->ysize; i++)
3840 if (sc_hist_up_line(scp)) {
3841 if (!(flags & SCGETC_CN))
3843 bios_value.bell_pitch,
3849 case SPCLKEY | FKEY | F(59): /* page down key */
3850 sc_remove_cutmarking(scp);
3851 for (i = 0; i < scp->ysize; i++)
3852 if (sc_hist_down_line(scp)) {
3853 if (!(flags & SCGETC_CN))
3855 bios_value.bell_pitch,
3861 #endif /* SC_NO_HISTORY */
3865 * Process and consume special keys here. Return a plain char code
3866 * or a char code with the META flag or a function key code.
3870 /* goto next_code */
3875 switch (KEYCHAR(c)) {
3883 sc->kbd, KDGKBSTATE, (caddr_t)&f);
3885 scp->status |= SLKED;
3887 if (scp->status & SLKED) {
3888 scp->status &= ~SLKED;
3889 #ifndef SC_NO_HISTORY
3892 if (!sc_hist_restore(
3894 sc_remove_cutmarking(
3900 sc_draw_cursor_image(
3903 /* Only safe in Giant-locked
3905 tp = SC_DEV(sc, scp->index);
3906 if (!(flags & SCGETC_CN) &&
3908 sctty_outwakeup(tp);
3915 #ifndef SC_NO_CUTPASTE
3916 sc_mouse_paste(scp);
3920 /* NON-LOCKING KEYS */
3933 if (!(sc->flags & SC_SCRN_BLANKED))
3939 /* force activatation/deactivation of the screen
3941 if (!(sc->flags & SC_SCRN_BLANKED)) {
3942 run_scrn_saver = TRUE;
3943 sc->scrn_time_stamp -= scrn_blank_time;
3947 * While devices are being probed, the
3948 * screen saver need to be invoked
3951 if (sc->flags & SC_SCRN_BLANKED) {
3952 scsplash_stick(FALSE);
3956 if (!ISGRAPHSC(scp)) {
3957 scsplash_stick(TRUE);
3963 #endif /* DEV_SPLASH */
3967 #ifndef SC_DISABLE_REBOOT
3968 if (enable_reboot && !(flags & SCGETC_CN))
3974 #ifndef SC_DISABLE_REBOOT
3975 if (enable_reboot && !(flags & SCGETC_CN))
3976 shutdown_nice(RB_HALT);
3981 #ifndef SC_DISABLE_REBOOT
3982 if (enable_reboot && !(flags & SCGETC_CN))
3983 shutdown_nice(RB_HALT | RB_POWEROFF);
3988 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3991 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3995 #ifndef SC_DISABLE_KDBKEY
4002 if (enable_panic_key)
4003 panic("Forced by the panic key");
4007 this_scr = scp->index;
4008 for (i = (this_scr - sc->first_vty + 1) %
4010 sc->first_vty + i != this_scr;
4011 i = (i + 1) % sc->vtys) {
4013 SC_DEV(sc, sc->first_vty + i);
4014 if (tty_opened_ns(tp)) {
4016 scp->sc, sc->first_vty + i);
4023 this_scr = scp->index;
4024 for (i = (this_scr - sc->first_vty + sc->vtys -
4027 sc->first_vty + i != this_scr;
4028 i = (i + sc->vtys - 1) % sc->vtys) {
4030 SC_DEV(sc, sc->first_vty + i);
4031 if (tty_opened_ns(tp)) {
4033 scp->sc, sc->first_vty + i);
4040 if (KEYCHAR(c) >= F_SCR &&
4041 KEYCHAR(c) <= L_SCR) {
4042 sc_switch_scr(scp->sc,
4043 sc->first_vty + KEYCHAR(c) - F_SCR);
4046 /* assert(c & FKEY) */
4047 if (!(sc->flags & SC_SCRN_BLANKED))
4051 /* goto next_code */
4053 /* regular keys (maybe MKEY is set) */
4054 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
4056 kdb_alt_break(c, &sc->sc_altbrk);
4058 if (!(sc->flags & SC_SCRN_BLANKED))
4067 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
4068 vm_memattr_t *memattr)
4072 scp = sc_get_stat(tp);
4073 if (scp != scp->sc->cur_scp)
4075 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
4079 update_font(scr_stat *scp)
4081 #ifndef SC_NO_FONT_LOADING
4082 /* load appropriate font */
4083 if (!(scp->status & GRAPHICS_MODE)) {
4084 if (!(scp->status & PIXEL_MODE) &&
4085 ISFONTAVAIL(scp->sc->adp->va_flags)) {
4086 if (scp->font_size < 14) {
4087 if (scp->sc->fonts_loaded & FONT_8)
4088 sc_load_font(scp, 0, 8, 8,
4089 scp->sc->font_8, 0, 256);
4090 } else if (scp->font_size >= 16) {
4091 if (scp->sc->fonts_loaded & FONT_16)
4092 sc_load_font(scp, 0, 16, 8,
4093 scp->sc->font_16, 0, 256);
4095 if (scp->sc->fonts_loaded & FONT_14)
4096 sc_load_font(scp, 0, 14, 8,
4097 scp->sc->font_14, 0, 256);
4101 * This is an interim kludge to display correct font.
4102 * Always use the font page #0 on the video plane 2.
4103 * Somehow we cannot show the font in other font pages
4104 * on some video cards... XXX
4106 sc_show_font(scp, 0);
4110 #endif /* !SC_NO_FONT_LOADING */
4114 save_kbd_state(scr_stat *scp)
4119 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4120 if (error == ENOIOCTL)
4123 scp->status &= ~LOCK_MASK;
4124 scp->status |= state;
4130 update_kbd_state(scr_stat *scp, int new_bits, int mask)
4135 if (mask != LOCK_MASK) {
4136 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4137 if (error == ENOIOCTL)
4142 state |= new_bits & mask;
4144 state = new_bits & LOCK_MASK;
4146 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
4147 if (error == ENOIOCTL)
4153 update_kbd_leds(scr_stat *scp, int which)
4158 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
4159 if (error == ENOIOCTL)
4165 set_mode(scr_stat *scp)
4169 /* reject unsupported mode */
4170 if (vidd_get_info(scp->sc->adp, scp->mode, &info))
4173 /* if this vty is not currently showing, do nothing */
4174 if (scp != scp->sc->cur_scp)
4177 /* setup video hardware for the given mode */
4178 vidd_set_mode(scp->sc->adp, scp->mode);
4179 scp->rndr->init(scp);
4180 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
4181 (void *)scp->sc->adp->va_window, FALSE);
4185 sc_set_border(scp, scp->border);
4186 sc_set_cursor_image(scp);
4192 sc_set_border(scr_stat *scp, int color)
4194 SC_VIDEO_LOCK(scp->sc);
4195 (*scp->rndr->draw_border)(scp, color);
4196 SC_VIDEO_UNLOCK(scp->sc);
4199 #ifndef SC_NO_FONT_LOADING
4201 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
4202 int base, int count)
4207 sc->font_loading_in_progress = TRUE;
4208 vidd_load_font(sc->adp, page, size, width, buf, base, count);
4209 sc->font_loading_in_progress = FALSE;
4213 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
4214 int base, int count)
4219 sc->font_loading_in_progress = TRUE;
4220 vidd_save_font(sc->adp, page, size, width, buf, base, count);
4221 sc->font_loading_in_progress = FALSE;
4225 sc_show_font(scr_stat *scp, int page)
4227 vidd_show_font(scp->sc->adp, page);
4229 #endif /* !SC_NO_FONT_LOADING */
4232 sc_paste(scr_stat *scp, const u_char *p, int count)
4237 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
4238 if (!tty_opened_ns(tp))
4240 rmap = scp->sc->scr_rmap;
4241 for (; count > 0; --count)
4242 ttydisc_rint(tp, rmap[*p++], 0);
4243 ttydisc_rint_done(tp);
4247 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup)
4251 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
4252 if (!tty_opened_ns(tp))
4254 ttydisc_rint_simple(tp, p, count);
4256 /* XXX: we can't always call ttydisc_rint_done() here! */
4257 ttydisc_rint_done(tp);
4262 * pitch is the divisor for 1.193182MHz PIT clock. By dividing 1193172 / pitch,
4263 * we convert it back to Hz.
4264 * duration is in ticks of 1/hz.
4267 sc_bell(scr_stat *scp, int pitch, int duration)
4269 if (cold || kdb_active || shutdown_in_progress || !enable_bell)
4272 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
4275 if (scp->sc->flags & SC_VISUAL_BELL) {
4276 if (scp->sc->blink_in_progress)
4278 scp->sc->blink_in_progress = 3;
4279 if (scp != scp->sc->cur_scp)
4280 scp->sc->blink_in_progress += 2;
4281 blink_screen(scp->sc->cur_scp);
4282 } else if (duration != 0 && pitch != 0) {
4283 if (scp != scp->sc->cur_scp)
4285 sysbeep(1193182 / pitch, SBT_1S * duration / hz);
4292 if (sc_console == NULL)
4293 return (SC_KERNEL_CONS_ATTR); /* for very early, before pcpu */
4294 return (sc_kattrtab[curcpu % nitems(sc_kattrtab)]);
4298 blink_screen(void *arg)
4300 scr_stat *scp = arg;
4303 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
4304 scp->sc->blink_in_progress = 0;
4306 tp = SC_DEV(scp->sc, scp->index);
4307 if (tty_opened_ns(tp))
4308 sctty_outwakeup(tp);
4309 if (scp->sc->delayed_next_scr)
4310 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
4312 (*scp->rndr->draw)(scp, 0, scp->xsize * scp->ysize,
4313 scp->sc->blink_in_progress & 1);
4314 scp->sc->blink_in_progress--;
4315 callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0,
4316 blink_screen, scp, C_PREL(0));
4321 * Until sc_attach_unit() gets called no dev structures will be available
4322 * to store the per-screen current status. This is the case when the
4323 * kernel is initially booting and needs access to its console. During
4324 * this early phase of booting the console's current status is kept in
4325 * one statically defined scr_stat structure, and any pointers to the
4326 * dev structures will be NULL.
4330 sc_get_stat(struct tty *tp)
4333 return (&main_console);
4334 return (SC_STAT(tp));
4338 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
4339 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
4344 sc_allocate_keyboard(sc_softc_t *sc, int unit)
4351 kbd_allocate("kbdmux", -1, (void *)&sc->kbd, sckbdevent, sc);
4353 k0 = kbd_get_keyboard(idx0);
4355 for (idx = kbd_find_keyboard2("*", -1, 0); idx != -1;
4356 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
4357 k = kbd_get_keyboard(idx);
4359 if (idx == idx0 || KBD_IS_BUSY(k))
4362 bzero(&ki, sizeof(ki));
4363 strcpy(ki.kb_name, k->kb_name);
4364 ki.kb_unit = k->kb_unit;
4366 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t)&ki);
4369 idx0 = kbd_allocate(
4370 "*", unit, (void *)&sc->kbd, sckbdevent, sc);