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_compat.h"
38 #include "opt_syscons.h"
39 #include "opt_splash.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
47 #include <sys/consio.h>
49 #include <sys/eventhandler.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
59 #include <sys/random.h>
60 #include <sys/reboot.h>
61 #include <sys/serial.h>
62 #include <sys/signalvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/power.h>
68 #include <machine/clock.h>
69 #if defined(__arm__) || defined(__mips__) || \
70 defined(__powerpc__) || defined(__sparc64__)
71 #include <machine/sc_machdep.h>
73 #include <machine/pc/display.h>
75 #if defined( __i386__) || defined(__amd64__)
76 #include <machine/psl.h>
77 #include <machine/frame.h>
79 #include <machine/stdarg.h>
81 #if defined(__amd64__) || defined(__i386__)
82 #include <machine/vmparam.h>
88 #include <dev/kbd/kbdreg.h>
89 #include <dev/fb/fbreg.h>
90 #include <dev/fb/splashreg.h>
91 #include <dev/syscons/syscons.h>
96 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
97 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
99 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
101 /* NULL-safe version of "tty_opened()" */
102 #define tty_opened_ns(tp) ((tp) != NULL && tty_opened(tp))
104 static u_char sc_kattrtab[MAXCPU];
106 static int sc_console_unit = -1;
107 static int sc_saver_keyb_only = 1;
108 static scr_stat *sc_console;
109 static struct consdev *sc_consptr;
110 static void *sc_kts[MAXCPU];
111 static struct sc_term_sw *sc_ktsw;
112 static scr_stat main_console;
113 static struct tty *main_devs[MAXCONS];
115 static char init_done = COLD;
116 static int shutdown_in_progress = FALSE;
117 static int suspend_in_progress = FALSE;
118 static char sc_malloc = FALSE;
120 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
121 static int run_scrn_saver = FALSE; /* should run the saver? */
122 static int enable_bell = TRUE; /* enable beeper */
124 #ifndef SC_DISABLE_REBOOT
125 static int enable_reboot = TRUE; /* enable keyboard reboot */
128 #ifndef SC_DISABLE_KDBKEY
129 static int enable_kdbkey = TRUE; /* enable keyboard debug */
132 static long scrn_blank_time = 0; /* screen saver timeout value */
134 static int scrn_blanked; /* # of blanked screen */
135 static int sticky_splash = FALSE;
137 static void none_saver(sc_softc_t *sc, int blank) { }
138 static void (*current_saver)(sc_softc_t *, int) = none_saver;
141 #ifdef SC_NO_SUSPEND_VTYSWITCH
142 static int sc_no_suspend_vtswitch = 1;
144 static int sc_no_suspend_vtswitch = 0;
146 static int sc_susp_scr;
148 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
149 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
150 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
151 &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
152 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell,
154 #ifndef SC_DISABLE_REBOOT
155 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
156 0, "enable keyboard reboot");
158 #ifndef SC_DISABLE_KDBKEY
159 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
160 0, "enable keyboard debug");
162 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN,
163 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
164 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
168 tsw_ioctl_t *sc_user_ioctl;
170 static bios_values_t bios_value;
172 static int enable_panic_key;
173 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
174 0, "Enable panic via keypress specified in kbdmap(5)");
176 #define SC_CONSOLECTL 255
178 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
181 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
182 static int scvidprobe(int unit, int flags, int cons);
183 static int sckbdprobe(int unit, int flags, int cons);
184 static void scmeminit(void *arg);
185 static int scdevtounit(struct tty *tp);
186 static kbd_callback_func_t sckbdevent;
187 static void scinit(int unit, int flags);
188 static scr_stat *sc_get_stat(struct tty *tp);
189 static void scterm(int unit, int flags);
190 static void scshutdown(void *, int);
191 static void scsuspend(void *);
192 static void scresume(void *);
193 static u_int scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp);
194 static void sc_puts(scr_stat *scp, u_char *buf, int len);
196 #define SCGETC_NONBLOCK 2
197 static void sccnupdate(scr_stat *scp);
198 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
199 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
200 static timeout_t scrn_timer;
201 static int and_region(int *s1, int *e1, int s2, int e2);
202 static void scrn_update(scr_stat *scp, int show_cursor);
205 static int scsplash_callback(int event, void *arg);
206 static void scsplash_saver(sc_softc_t *sc, int show);
207 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
208 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
209 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
210 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
211 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
212 static int wait_scrn_saver_stop(sc_softc_t *sc);
213 #define scsplash_stick(stick) (sticky_splash = (stick))
214 #else /* !DEV_SPLASH */
215 #define scsplash_stick(stick)
216 #endif /* DEV_SPLASH */
218 static int do_switch_scr(sc_softc_t *sc, int s);
219 static int vt_proc_alive(scr_stat *scp);
220 static int signal_vt_rel(scr_stat *scp);
221 static int signal_vt_acq(scr_stat *scp);
222 static int finish_vt_rel(scr_stat *scp, int release, int *s);
223 static int finish_vt_acq(scr_stat *scp);
224 static void exchange_scr(sc_softc_t *sc);
225 static void update_cursor_image(scr_stat *scp);
226 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
227 static void update_font(scr_stat *);
228 static int save_kbd_state(scr_stat *scp);
229 static int update_kbd_state(scr_stat *scp, int state, int mask);
230 static int update_kbd_leds(scr_stat *scp, int which);
231 static int sc_kattr(void);
232 static timeout_t blink_screen;
233 static struct tty *sc_alloc_tty(int, int);
235 static cn_probe_t sc_cnprobe;
236 static cn_init_t sc_cninit;
237 static cn_term_t sc_cnterm;
238 static cn_getc_t sc_cngetc;
239 static cn_putc_t sc_cnputc;
240 static cn_grab_t sc_cngrab;
241 static cn_ungrab_t sc_cnungrab;
245 static tsw_open_t sctty_open;
246 static tsw_close_t sctty_close;
247 static tsw_outwakeup_t sctty_outwakeup;
248 static tsw_ioctl_t sctty_ioctl;
249 static tsw_mmap_t sctty_mmap;
251 static struct ttydevsw sc_ttydevsw = {
252 .tsw_open = sctty_open,
253 .tsw_close = sctty_close,
254 .tsw_outwakeup = sctty_outwakeup,
255 .tsw_ioctl = sctty_ioctl,
256 .tsw_mmap = sctty_mmap,
259 static d_ioctl_t consolectl_ioctl;
260 static d_close_t consolectl_close;
262 static struct cdevsw consolectl_devsw = {
263 .d_version = D_VERSION,
264 .d_flags = D_NEEDGIANT | D_TRACKCLOSE,
265 .d_ioctl = consolectl_ioctl,
266 .d_close = consolectl_close,
267 .d_name = "consolectl",
270 /* ec -- emergency console. */
272 static u_int ec_scroffset;
280 int attr, column, mysize, width, xsize, yborder, ysize;
282 if (c < 0 || c > 0xff || c == '\a')
284 if (sc_console == NULL) {
285 #if !defined(__amd64__) && !defined(__i386__)
289 * This is enough for ec_putc() to work very early on x86
290 * if the kernel starts in normal color text mode.
292 fb = KERNBASE + 0xb8000;
297 if (!ISTEXTSC(&main_console))
299 fb = main_console.sc->adp->va_window;
300 xsize = main_console.xsize;
301 ysize = main_console.ysize;
304 scrptr = (u_short *)(void *)fb + xsize * yborder;
305 mysize = xsize * (ysize - 2 * yborder);
308 column = ind % xsize;
309 width = (c == '\b' ? -1 : c == '\t' ? (column + 8) & ~7 :
310 c == '\r' ? -column : c == '\n' ? xsize - column : 1);
311 if (width == 0 || (width < 0 && ind < -width))
313 } while (atomic_cmpset_rel_int(&ec_scroffset, ind, ind + width) == 0);
314 if (c == '\b' || c == '\r')
317 ind += xsize; /* XXX clearing from new pos is not atomic */
320 if (c == '\t' || c == '\n')
323 scrptr[ind++ % mysize] = (attr << 8) | c;
324 while (--width != 0);
328 sc_probe_unit(int unit, int flags)
330 if (!vty_enabled(VTY_SC))
332 if (!scvidprobe(unit, flags, FALSE)) {
334 printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
338 /* syscons will be attached even when there is no keyboard */
339 sckbdprobe(unit, flags, FALSE);
344 /* probe video adapters, return TRUE if found */
346 scvidprobe(int unit, int flags, int cons)
349 * Access the video adapter driver through the back door!
350 * Video adapter drivers need to be configured before syscons.
351 * However, when syscons is being probed as the low-level console,
352 * they have not been initialized yet. We force them to initialize
353 * themselves here. XXX
355 vid_configure(cons ? VIO_PROBE_ONLY : 0);
357 return (vid_find_adapter("*", unit) >= 0);
360 /* probe the keyboard, return TRUE if found */
362 sckbdprobe(int unit, int flags, int cons)
364 /* access the keyboard driver through the backdoor! */
365 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
367 return (kbd_find_keyboard("*", unit) >= 0);
371 *adapter_name(video_adapter_t *adp)
377 { KD_MONO, { "MDA", "MDA" } },
378 { KD_HERCULES, { "Hercules", "Hercules" } },
379 { KD_CGA, { "CGA", "CGA" } },
380 { KD_EGA, { "EGA", "EGA (mono)" } },
381 { KD_VGA, { "VGA", "VGA (mono)" } },
382 { KD_TGA, { "TGA", "TGA" } },
383 { -1, { "Unknown", "Unknown" } },
387 for (i = 0; names[i].type != -1; ++i)
388 if (names[i].type == adp->va_type)
390 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
394 sctty_outwakeup(struct tty *tp)
398 scr_stat *scp = sc_get_stat(tp);
400 if (scp->status & SLKED ||
401 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
405 len = ttydisc_getc(tp, buf, sizeof buf);
408 SC_VIDEO_LOCK(scp->sc);
409 sc_puts(scp, buf, len);
410 SC_VIDEO_UNLOCK(scp->sc);
415 sc_alloc_tty(int index, int devnum)
417 struct sc_ttysoftc *stc;
420 /* Allocate TTY object and softc to store unit number. */
421 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
422 stc->st_index = index;
424 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
426 /* Create device node. */
427 tty_makedev(tp, NULL, "v%r", devnum);
434 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
444 (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
445 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
446 vidd_get_info(sc->adp, vmode, &info) != 0 ||
447 !sc_support_pixel_mode(&info))
451 * If the mode is unset or unsupported, search for an available
452 * 800x600 graphics mode with the highest color depth.
455 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
456 if (vidd_get_info(sc->adp, i, &info) == 0 &&
457 info.vi_width == 800 && info.vi_height == 600 &&
458 sc_support_pixel_mode(&info) &&
459 info.vi_depth > depth) {
461 depth = info.vi_depth;
465 vidd_get_info(sc->adp, vmode, &info);
468 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
469 fontsize = info.vi_cheight;
471 fontsize = scp->font_size;
475 else if (fontsize >= 16)
479 #ifndef SC_NO_FONT_LOADING
482 if ((sc->fonts_loaded & FONT_8) == 0)
487 if ((sc->fonts_loaded & FONT_14) == 0)
492 if ((sc->fonts_loaded & FONT_16) == 0)
501 if ((sc->flags & SC_SPLASH_SCRN) != 0)
502 splash_term(sc->adp);
504 #ifndef SC_NO_HISTORY
505 if (scp->history != NULL) {
506 sc_vtb_append(&scp->vtb, 0, scp->history,
507 scp->ypos * scp->xsize + scp->xpos);
508 scp->history_pos = sc_vtb_tail(scp->history);
511 vidd_set_mode(sc->adp, vmode);
512 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
513 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
514 scp->xpixel = info.vi_width;
515 scp->ypixel = info.vi_height;
516 scp->xsize = scp->xpixel / 8;
517 scp->ysize = scp->ypixel / fontsize;
519 scp->ypos = scp->ysize - 1;
520 scp->xoff = scp->yoff = 0;
522 scp->font_size = fontsize;
524 scp->start = scp->xsize * scp->ysize - 1;
526 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
527 scp->mode = sc->initial_mode = vmode;
529 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
530 (void *)sc->adp->va_window, FALSE);
532 sc_alloc_scr_buffer(scp, FALSE, FALSE);
533 sc_init_emulator(scp, NULL);
534 #ifndef SC_NO_CUTPASTE
535 sc_alloc_cut_buffer(scp, FALSE);
537 #ifndef SC_NO_HISTORY
538 sc_alloc_history_buffer(scp, 0, 0, FALSE);
540 sc_set_border(scp, scp->border);
541 sc_set_cursor_image(scp);
542 scp->status &= ~UNKNOWN_MODE;
544 if ((sc->flags & SC_SPLASH_SCRN) != 0)
545 splash_init(sc->adp, scsplash_callback, sc);
551 sc_attach_unit(int unit, int flags)
559 if (!vty_enabled(VTY_SC))
562 flags &= ~SC_KERNEL_CONSOLE;
564 if (sc_console_unit == unit) {
566 * If this unit is being used as the system console, we need to
567 * adjust some variables and buffers before and after scinit().
569 /* assert(sc_console != NULL) */
570 flags |= SC_KERNEL_CONSOLE;
575 if (sc_console->tsw->te_size > 0) {
576 sc_ktsw = sc_console->tsw;
577 /* assert(sc_console->ts != NULL); */
578 oldts = sc_console->ts;
579 for (i = 0; i <= mp_maxid; i++) {
580 ts = malloc(sc_console->tsw->te_size, M_DEVBUF,
582 (*sc_console->tsw->te_init)(sc_console, &ts, SC_TE_COLD_INIT);
584 (*sc_console->tsw->te_default_attr)(sc_console, sc_kattrtab[i],
585 SC_KERNEL_CONS_REV_ATTR);
588 sc_console->ts = oldts;
589 (*sc_console->tsw->te_default_attr)(sc_console, SC_NORM_ATTR,
596 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
598 callout_init(&sc->ctimeout, 0);
599 callout_init(&sc->cblink, 0);
600 scp = sc_get_stat(sc->dev[0]);
601 if (sc_console == NULL) /* sc_console_unit < 0 */
605 if ((sc->config & SC_VESAMODE) != 0)
606 sc_set_vesa_mode(scp, sc, unit);
607 #endif /* SC_PIXEL_MODE */
609 /* initialize cursor */
611 update_cursor_image(scp);
613 /* get screen update going */
616 /* set up the keyboard */
617 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
618 update_kbd_state(scp, scp->status, LOCK_MASK);
620 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
621 SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
623 printf("%s%d:", SC_DRIVER_NAME, unit);
624 if (sc->adapter >= 0)
625 printf(" fb%d", sc->adapter);
626 if (sc->keyboard >= 0)
627 printf(", kbd%d", sc->keyboard);
629 printf(", terminal emulator: %s (%s)",
630 scp->tsw->te_name, scp->tsw->te_desc);
634 /* Register suspend/resume/shutdown callbacks for the kernel console. */
635 if (sc_console_unit == unit) {
636 EVENTHANDLER_REGISTER(power_suspend_early, scsuspend, NULL,
637 EVENTHANDLER_PRI_ANY);
638 EVENTHANDLER_REGISTER(power_resume, scresume, NULL,
639 EVENTHANDLER_PRI_ANY);
640 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL,
641 SHUTDOWN_PRI_DEFAULT);
644 for (vc = 0; vc < sc->vtys; vc++) {
645 if (sc->dev[vc] == NULL) {
646 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
647 if (vc == 0 && sc->dev == main_devs)
648 SC_STAT(sc->dev[0]) = &main_console;
651 * The first vty already has struct tty and scr_stat initialized
652 * in scinit(). The other vtys will have these structs when
657 dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600,
659 dev->si_drv1 = sc->dev[0];
667 if (!vty_enabled(VTY_SC))
674 * As soon as malloc() becomes functional, we had better allocate
675 * various buffers for the kernel console.
678 if (sc_console_unit < 0) /* sc_console == NULL */
681 /* copy the temporary buffer to the final buffer */
682 sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
684 #ifndef SC_NO_CUTPASTE
685 sc_alloc_cut_buffer(sc_console, FALSE);
688 #ifndef SC_NO_HISTORY
689 /* initialize history buffer & pointers */
690 sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
695 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
698 scdevtounit(struct tty *tp)
700 int vty = SC_VTY(tp);
702 if (vty == SC_CONSOLECTL)
703 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
704 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
711 sctty_open(struct tty *tp)
713 int unit = scdevtounit(tp);
720 DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
721 devtoname(tp->t_dev), unit, SC_VTY(tp)));
723 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
727 if (!tty_opened(tp)) {
728 /* Use the current setting of the <-- key as default VERASE. */
729 /* If the Delete key is preferable, an stty is necessary */
731 if (sc->kbd != NULL) {
732 key.keynum = KEYCODE_BS;
733 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
734 tp->t_termios.c_cc[VERASE] = key.key.map[0];
739 scp = sc_get_stat(tp);
741 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
743 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
745 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
746 tp->t_winsize.ws_col = scp->xsize;
747 tp->t_winsize.ws_row = scp->ysize;
754 sctty_close(struct tty *tp)
759 if (SC_VTY(tp) != SC_CONSOLECTL) {
760 scp = sc_get_stat(tp);
761 /* were we in the middle of the VT switching process? */
762 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
764 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
765 cnavailable(sc_consptr, TRUE);
766 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */
767 DPRINTF(5, ("reset WAIT_REL, "));
768 if (finish_vt_acq(scp) == 0) /* force acknowledge */
769 DPRINTF(5, ("reset WAIT_ACQ, "));
771 if (scp == &main_console) {
774 scp->smode.mode = VT_AUTO;
777 sc_vtb_destroy(&scp->vtb);
779 sc_vtb_destroy(&scp->scr);
781 sc_free_history_buffer(scp, scp->ysize);
788 scp->smode.mode = VT_AUTO;
790 scp->kbd_mode = K_XLATE;
791 if (scp == scp->sc->cur_scp)
792 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
793 DPRINTF(5, ("done.\n"));
797 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
799 scread(struct cdev *dev, struct uio *uio, int flag)
801 if (!sc_saver_keyb_only)
802 sc_touch_scrn_saver();
803 return ttyread(dev, uio, flag);
808 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
816 sc = (sc_softc_t *)arg;
817 /* assert(thiskbd == sc->kbd) */
824 case KBDIO_UNLOADING:
827 kbd_release(thiskbd, (void *)&sc->keyboard);
835 * Loop while there is still input to get from the keyboard.
836 * I don't think this is nessesary, and it doesn't fix
837 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
839 while ((c = scgetc(sc, SCGETC_NONBLOCK, NULL)) != NOKEY) {
841 cur_tty = SC_DEV(sc, sc->cur_scp->index);
842 if (!tty_opened_ns(cur_tty))
845 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
848 switch (KEYFLAGS(c)) {
849 case 0x0000: /* normal key */
850 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
852 case FKEY: /* function key, return string */
853 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
855 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
858 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
860 ttydisc_rint_simple(cur_tty, cp, len);
862 case MKEY: /* meta is active, prepend ESC */
863 ttydisc_rint(cur_tty, 0x1b, 0);
864 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
866 case BKEY: /* backtab fixed sequence (esc [ Z) */
867 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
871 ttydisc_rint_done(cur_tty);
874 sc->cur_scp->status |= MOUSE_HIDDEN;
882 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
886 struct cursor_attr *cap;
890 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
891 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
895 /* If there is a user_ioctl function call that first */
897 error = (*sc_user_ioctl)(tp, cmd, data, td);
898 if (error != ENOIOCTL)
902 error = sc_vid_ioctl(tp, cmd, data, td);
903 if (error != ENOIOCTL)
906 #ifndef SC_NO_HISTORY
907 error = sc_hist_ioctl(tp, cmd, data, td);
908 if (error != ENOIOCTL)
912 #ifndef SC_NO_SYSMOUSE
913 error = sc_mouse_ioctl(tp, cmd, data, td);
914 if (error != ENOIOCTL)
918 scp = sc_get_stat(tp);
919 /* assert(scp != NULL) */
920 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
924 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
925 if (error != ENOIOCTL)
929 switch (cmd) { /* process console hardware related ioctl's */
931 case GIO_ATTR: /* get current attributes */
932 /* this ioctl is not processed here, but in the terminal emulator */
935 case GIO_COLOR: /* is this a color console ? */
936 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
939 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
940 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
943 scrn_blank_time = *(int *)data;
944 run_scrn_saver = (scrn_blank_time != 0);
948 case CONS_CURSORTYPE: /* set cursor type (old interface + HIDDEN) */
950 *(int *)data &= CONS_CURSOR_ATTRS;
951 sc_change_cursor_shape(scp, *(int *)data, -1, -1);
955 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */
956 switch (((int *)data)[0] & (CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR)) {
958 cap = &sc->curs_attr;
960 case CONS_LOCAL_CURSOR:
961 cap = &scp->base_curs_attr;
963 case CONS_DEFAULT_CURSOR:
964 cap = &sc->dflt_curs_attr;
966 case CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR:
967 cap = &scp->dflt_curs_attr;
970 if (((int *)data)[0] & CONS_CHARCURSOR_COLORS) {
971 ((int *)data)[1] = cap->bg[0];
972 ((int *)data)[2] = cap->bg[1];
973 } else if (((int *)data)[0] & CONS_MOUSECURSOR_COLORS) {
974 ((int *)data)[1] = cap->mouse_ba;
975 ((int *)data)[2] = cap->mouse_ia;
977 ((int *)data)[1] = cap->base;
978 ((int *)data)[2] = cap->height;
980 ((int *)data)[0] = cap->flags;
983 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */
985 sc_change_cursor_shape(scp, ((int *)data)[0],
986 ((int *)data)[1], ((int *)data)[2]);
990 case CONS_BELLTYPE: /* set bell type sound/visual */
991 if ((*(int *)data) & CONS_VISUAL_BELL)
992 sc->flags |= SC_VISUAL_BELL;
994 sc->flags &= ~SC_VISUAL_BELL;
995 if ((*(int *)data) & CONS_QUIET_BELL)
996 sc->flags |= SC_QUIET_BELL;
998 sc->flags &= ~SC_QUIET_BELL;
1001 case CONS_GETINFO: /* get current (virtual) console info */
1003 vid_info_t *ptr = (vid_info_t*)data;
1004 if (ptr->size == sizeof(struct vid_info)) {
1005 ptr->m_num = sc->cur_scp->index;
1006 ptr->font_size = scp->font_size;
1007 ptr->mv_col = scp->xpos;
1008 ptr->mv_row = scp->ypos;
1009 ptr->mv_csz = scp->xsize;
1010 ptr->mv_rsz = scp->ysize;
1011 ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1013 * The following fields are filled by the terminal emulator. XXX
1020 ptr->mv_grfc.fore = 0; /* not supported */
1021 ptr->mv_grfc.back = 0; /* not supported */
1022 ptr->mv_ovscan = scp->border;
1023 if (scp == sc->cur_scp)
1024 save_kbd_state(scp);
1025 ptr->mk_keylock = scp->status & LOCK_MASK;
1031 case CONS_GETVERS: /* get version number */
1032 *(int*)data = 0x200; /* version 2.0 */
1035 case CONS_IDLE: /* see if the screen has been idle */
1037 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
1038 * the user process may have been writing something on the
1039 * screen and syscons is not aware of it. Declare the screen
1040 * is NOT idle if it is in one of these modes. But there is
1041 * an exception to it; if a screen saver is running in the
1042 * graphics mode in the current screen, we should say that the
1043 * screen has been idle.
1045 *(int *)data = (sc->flags & SC_SCRN_IDLE)
1046 && (!ISGRAPHSC(sc->cur_scp)
1047 || (sc->cur_scp->status & SAVER_RUNNING));
1050 case CONS_SAVERMODE: /* set saver mode */
1051 switch(*(int *)data) {
1053 case CONS_USR_SAVER:
1054 /* if a LKM screen saver is running, stop it first. */
1055 scsplash_stick(FALSE);
1056 saver_mode = *(int *)data;
1059 if ((error = wait_scrn_saver_stop(NULL))) {
1064 run_scrn_saver = TRUE;
1065 if (saver_mode == CONS_USR_SAVER)
1066 scp->status |= SAVER_RUNNING;
1068 scp->status &= ~SAVER_RUNNING;
1069 scsplash_stick(TRUE);
1072 case CONS_LKM_SAVER:
1074 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
1075 scp->status &= ~SAVER_RUNNING;
1076 saver_mode = *(int *)data;
1084 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
1086 * Note that this ioctl does not guarantee the screen saver
1087 * actually starts or stops. It merely attempts to do so...
1090 run_scrn_saver = (*(int *)data != 0);
1092 sc->scrn_time_stamp -= scrn_blank_time;
1096 case CONS_SCRSHOT: /* get a screen shot */
1098 int retval, hist_rsz;
1099 size_t lsize, csize;
1100 vm_offset_t frbp, hstp;
1102 scrshot_t *ptr = (scrshot_t *)data;
1103 void *outp = ptr->buf;
1105 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
1108 if (ISGRAPHSC(scp)) {
1112 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1113 if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
1114 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1119 lsize = scp->xsize * sizeof(u_int16_t);
1120 csize = ptr->xsize * sizeof(u_int16_t);
1121 /* Pointer to the last line of framebuffer */
1122 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
1124 /* Pointer to the last line of target buffer */
1125 outp = (char *)outp + ptr->ysize * csize;
1126 /* Pointer to the last line of history buffer */
1127 if (scp->history != NULL)
1128 hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
1129 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
1134 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1135 if (lnum < scp->ysize) {
1139 if (hstp < scp->history->vtb_buffer)
1140 hstp += scp->history->vtb_rows * lsize;
1145 outp = (char *)outp - csize;
1146 retval = copyout((void *)frbp, outp, csize);
1154 case VT_SETMODE: /* set screen switcher mode */
1156 struct vt_mode *mode;
1159 mode = (struct vt_mode *)data;
1160 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1161 if (scp->smode.mode == VT_PROCESS) {
1162 p1 = pfind(scp->pid);
1163 if (scp->proc == p1 && scp->proc != td->td_proc) {
1166 DPRINTF(5, ("error EPERM\n"));
1173 if (mode->mode == VT_AUTO) {
1174 scp->smode.mode = VT_AUTO;
1177 DPRINTF(5, ("VT_AUTO, "));
1178 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1179 cnavailable(sc_consptr, TRUE);
1180 /* were we in the middle of the vty switching process? */
1181 if (finish_vt_rel(scp, TRUE, &s) == 0)
1182 DPRINTF(5, ("reset WAIT_REL, "));
1183 if (finish_vt_acq(scp) == 0)
1184 DPRINTF(5, ("reset WAIT_ACQ, "));
1186 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1187 || !ISSIGVALID(mode->frsig)) {
1189 DPRINTF(5, ("error EINVAL\n"));
1192 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1193 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1194 scp->proc = td->td_proc;
1195 scp->pid = scp->proc->p_pid;
1196 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1197 cnavailable(sc_consptr, FALSE);
1204 case VT_GETMODE: /* get screen switcher mode */
1205 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1208 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1209 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1211 ival = IOCPARM_IVAL(data);
1212 data = (caddr_t)&ival;
1215 case VT_RELDISP: /* screen switcher ioctl */
1218 * This must be the current vty which is in the VT_PROCESS
1221 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1225 /* ...and this process is controlling it. */
1226 if (scp->proc != td->td_proc) {
1231 switch(*(int *)data) {
1232 case VT_FALSE: /* user refuses to release screen, abort */
1233 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1234 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1236 case VT_TRUE: /* user has released screen, go on */
1237 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1238 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1240 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1241 if ((error = finish_vt_acq(scp)) == 0)
1242 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1250 case VT_OPENQRY: /* return free virtual console */
1251 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1253 if (!tty_opened_ns(tp)) {
1254 *(int *)data = i + 1;
1260 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1261 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1263 ival = IOCPARM_IVAL(data);
1264 data = (caddr_t)&ival;
1267 case VT_ACTIVATE: /* switch to screen *data */
1268 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1270 error = sc_clean_up(sc->cur_scp);
1274 error = sc_switch_scr(sc, i);
1277 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1278 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1280 ival = IOCPARM_IVAL(data);
1281 data = (caddr_t)&ival;
1284 case VT_WAITACTIVE: /* wait for switch to occur */
1285 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1286 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1288 if (i == sc->cur_scp->index)
1290 error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1293 case VT_GETACTIVE: /* get active vty # */
1294 *(int *)data = sc->cur_scp->index + 1;
1297 case VT_GETINDEX: /* get this vty # */
1298 *(int *)data = scp->index + 1;
1301 case VT_LOCKSWITCH: /* prevent vty switching */
1302 if ((*(int *)data) & 0x01)
1303 sc->flags |= SC_SCRN_VTYLOCK;
1305 sc->flags &= ~SC_SCRN_VTYLOCK;
1308 case KDENABIO: /* allow io operations */
1309 error = priv_check(td, PRIV_IO);
1312 error = securelevel_gt(td->td_ucred, 0);
1316 td->td_frame->tf_eflags |= PSL_IOPL;
1317 #elif defined(__amd64__)
1318 td->td_frame->tf_rflags |= PSL_IOPL;
1322 case KDDISABIO: /* disallow io operations (default) */
1324 td->td_frame->tf_eflags &= ~PSL_IOPL;
1325 #elif defined(__amd64__)
1326 td->td_frame->tf_rflags &= ~PSL_IOPL;
1330 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1331 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1333 ival = IOCPARM_IVAL(data);
1334 data = (caddr_t)&ival;
1337 case KDSKBSTATE: /* set keyboard state (locks) */
1338 if (*(int *)data & ~LOCK_MASK)
1340 scp->status &= ~LOCK_MASK;
1341 scp->status |= *(int *)data;
1342 if (scp == sc->cur_scp)
1343 update_kbd_state(scp, scp->status, LOCK_MASK);
1346 case KDGKBSTATE: /* get keyboard state (locks) */
1347 if (scp == sc->cur_scp)
1348 save_kbd_state(scp);
1349 *(int *)data = scp->status & LOCK_MASK;
1352 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1353 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1354 error = kbdd_ioctl(sc->kbd, cmd, data);
1355 if (error == ENOIOCTL)
1359 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1360 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1362 ival = IOCPARM_IVAL(data);
1363 data = (caddr_t)&ival;
1366 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1367 if (*(int *)data & ~0x7f)
1369 error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1370 if (error == ENOIOCTL)
1374 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1375 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1377 ival = IOCPARM_IVAL(data);
1378 data = (caddr_t)&ival;
1381 case KDSKBMODE: /* set keyboard mode */
1382 switch (*(int *)data) {
1383 case K_XLATE: /* switch to XLT ascii mode */
1384 case K_RAW: /* switch to RAW scancode mode */
1385 case K_CODE: /* switch to CODE mode */
1386 scp->kbd_mode = *(int *)data;
1387 if (scp == sc->cur_scp)
1388 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1395 case KDGKBMODE: /* get keyboard mode */
1396 *(int *)data = scp->kbd_mode;
1400 error = kbdd_ioctl(sc->kbd, cmd, data);
1401 if (error == ENOIOCTL)
1405 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1406 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1408 ival = IOCPARM_IVAL(data);
1409 data = (caddr_t)&ival;
1412 case KDMKTONE: /* sound the bell */
1414 sc_bell(scp, (*(int*)data)&0xffff,
1415 (((*(int*)data)>>16)&0xffff)*hz/1000);
1417 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1420 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1421 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1423 ival = IOCPARM_IVAL(data);
1424 data = (caddr_t)&ival;
1427 case KIOCSOUND: /* make tone (*data) hz */
1428 if (scp == sc->cur_scp) {
1430 return sc_tone(*(int *)data);
1436 case KDGKBTYPE: /* get keyboard type */
1437 error = kbdd_ioctl(sc->kbd, cmd, data);
1438 if (error == ENOIOCTL) {
1439 /* always return something? XXX */
1444 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1445 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1447 ival = IOCPARM_IVAL(data);
1448 data = (caddr_t)&ival;
1451 case KDSETLED: /* set keyboard LED status */
1452 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1454 scp->status &= ~LED_MASK;
1455 scp->status |= *(int *)data;
1456 if (scp == sc->cur_scp)
1457 update_kbd_leds(scp, scp->status);
1460 case KDGETLED: /* get keyboard LED status */
1461 if (scp == sc->cur_scp)
1462 save_kbd_state(scp);
1463 *(int *)data = scp->status & LED_MASK;
1466 case KBADDKBD: /* add/remove keyboard to/from mux */
1468 error = kbdd_ioctl(sc->kbd, cmd, data);
1469 if (error == ENOIOCTL)
1473 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1474 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1476 ival = IOCPARM_IVAL(data);
1477 data = (caddr_t)&ival;
1480 case CONS_SETKBD: /* set the new keyboard */
1485 newkbd = kbd_get_keyboard(*(int *)data);
1486 if (newkbd == NULL) {
1491 if (sc->kbd != newkbd) {
1492 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1493 (void *)&sc->keyboard, sckbdevent, sc);
1494 /* i == newkbd->kb_index */
1496 if (sc->kbd != NULL) {
1497 save_kbd_state(sc->cur_scp);
1498 kbd_release(sc->kbd, (void *)&sc->keyboard);
1500 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1502 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1503 (caddr_t)&sc->cur_scp->kbd_mode);
1504 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1507 error = EPERM; /* XXX */
1514 case CONS_RELKBD: /* release the current keyboard */
1517 if (sc->kbd != NULL) {
1518 save_kbd_state(sc->cur_scp);
1519 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1528 case CONS_GETTERM: /* get the current terminal emulator info */
1532 if (((term_info_t *)data)->ti_index == 0) {
1535 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1538 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1539 sizeof(((term_info_t *)data)->ti_name));
1540 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1541 sizeof(((term_info_t *)data)->ti_desc));
1542 ((term_info_t *)data)->ti_flags = 0;
1545 ((term_info_t *)data)->ti_name[0] = '\0';
1546 ((term_info_t *)data)->ti_desc[0] = '\0';
1547 ((term_info_t *)data)->ti_flags = 0;
1552 case CONS_SETTERM: /* set the current terminal emulator */
1554 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1555 /* FIXME: what if scp == sc_console! XXX */
1559 case GIO_SCRNMAP: /* get output translation table */
1560 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1563 case PIO_SCRNMAP: /* set output translation table */
1564 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1565 for (i=0; i<sizeof(sc->scr_map); i++) {
1566 sc->scr_rmap[sc->scr_map[i]] = i;
1570 case GIO_KEYMAP: /* get keyboard translation table */
1571 case PIO_KEYMAP: /* set keyboard translation table */
1572 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
1573 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1574 case GIO_DEADKEYMAP: /* get accent key translation table */
1575 case PIO_DEADKEYMAP: /* set accent key translation table */
1576 case GETFKEY: /* get function key string */
1577 case SETFKEY: /* set function key string */
1578 error = kbdd_ioctl(sc->kbd, cmd, data);
1579 if (error == ENOIOCTL)
1583 #ifndef SC_NO_FONT_LOADING
1585 case PIO_FONT8x8: /* set 8x8 dot font */
1586 if (!ISFONTAVAIL(sc->adp->va_flags))
1588 bcopy(data, sc->font_8, 8*256);
1589 sc->fonts_loaded |= FONT_8;
1592 * Always use the font page #0. XXX
1593 * Don't load if the current font size is not 8x8.
1595 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1596 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1599 case GIO_FONT8x8: /* get 8x8 dot font */
1600 if (!ISFONTAVAIL(sc->adp->va_flags))
1602 if (sc->fonts_loaded & FONT_8) {
1603 bcopy(sc->font_8, data, 8*256);
1609 case PIO_FONT8x14: /* set 8x14 dot font */
1610 if (!ISFONTAVAIL(sc->adp->va_flags))
1612 bcopy(data, sc->font_14, 14*256);
1613 sc->fonts_loaded |= FONT_14;
1616 * Always use the font page #0. XXX
1617 * Don't load if the current font size is not 8x14.
1619 if (ISTEXTSC(sc->cur_scp)
1620 && (sc->cur_scp->font_size >= 14)
1621 && (sc->cur_scp->font_size < 16))
1622 sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1625 case GIO_FONT8x14: /* get 8x14 dot font */
1626 if (!ISFONTAVAIL(sc->adp->va_flags))
1628 if (sc->fonts_loaded & FONT_14) {
1629 bcopy(sc->font_14, data, 14*256);
1635 case PIO_FONT8x16: /* set 8x16 dot font */
1636 if (!ISFONTAVAIL(sc->adp->va_flags))
1638 bcopy(data, sc->font_16, 16*256);
1639 sc->fonts_loaded |= FONT_16;
1642 * Always use the font page #0. XXX
1643 * Don't load if the current font size is not 8x16.
1645 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1646 sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1649 case GIO_FONT8x16: /* get 8x16 dot font */
1650 if (!ISFONTAVAIL(sc->adp->va_flags))
1652 if (sc->fonts_loaded & FONT_16) {
1653 bcopy(sc->font_16, data, 16*256);
1659 #endif /* SC_NO_FONT_LOADING */
1669 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1673 return sctty_ioctl(dev->si_drv1, cmd, data, td);
1677 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td)
1679 #ifndef SC_NO_SYSMOUSE
1681 memset(&info, 0, sizeof(info));
1682 info.operation = MOUSE_ACTION;
1685 * Make sure all buttons are released when moused and other
1686 * console daemons exit, so that no buttons are left pressed.
1688 (void) sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td);
1694 sc_cnprobe(struct consdev *cp)
1699 if (!vty_enabled(VTY_SC)) {
1700 cp->cn_pri = CN_DEAD;
1704 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1706 /* a video card is always required */
1707 if (!scvidprobe(unit, flags, TRUE))
1708 cp->cn_pri = CN_DEAD;
1710 /* syscons will become console even when there is no keyboard */
1711 sckbdprobe(unit, flags, TRUE);
1713 if (cp->cn_pri == CN_DEAD)
1716 /* initialize required fields */
1717 strcpy(cp->cn_name, "ttyv0");
1721 sc_cninit(struct consdev *cp)
1726 sc_get_cons_priority(&unit, &flags);
1727 scinit(unit, flags | SC_KERNEL_CONSOLE);
1728 sc_console_unit = unit;
1729 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1734 sc_cnterm(struct consdev *cp)
1739 /* we are not the kernel console any more, release everything */
1741 if (sc_console_unit < 0)
1742 return; /* shouldn't happen */
1745 sc_clear_screen(sc_console);
1746 sccnupdate(sc_console);
1749 if (sc_ktsw != NULL) {
1750 for (i = 0; i <= mp_maxid; i++) {
1753 (*sc_ktsw->te_term)(sc_console, &ts);
1758 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1759 sc_console_unit = -1;
1763 static void sccnclose(sc_softc_t *sc, struct sc_cnstate *sp);
1764 static int sc_cngetc_locked(struct sc_cnstate *sp);
1765 static void sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp);
1766 static void sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp);
1767 static void sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags);
1768 static void sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp);
1769 static void sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp);
1772 sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp)
1775 * Locking method: hope for the best.
1776 * The keyboard is supposed to be Giant locked. We can't handle that
1777 * in general. The kdb_active case here is not safe, and we will
1778 * proceed without the lock in all cases.
1780 sp->kbd_locked = !kdb_active && mtx_trylock(&Giant);
1784 sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp)
1788 sp->kbd_locked = FALSE;
1792 sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp)
1798 * - if kdb_active and video_mtx is not owned by anyone, then lock
1799 * by kdb remaining active
1800 * - if !kdb_active, try to acquire video_mtx without blocking or
1801 * recursing; if we get it then it works normally.
1802 * Note that video_mtx is especially unusable if we already own it,
1803 * since then it is protecting something and syscons is not reentrant
1804 * enough to ignore the protection even in the kdb_active case.
1807 sp->kdb_locked = sc->video_mtx.mtx_lock == MTX_UNOWNED || panicstr;
1808 sp->mtx_locked = FALSE;
1810 sp->kdb_locked = FALSE;
1811 for (retries = 0; retries < 1000; retries++) {
1812 sp->mtx_locked = mtx_trylock_spin_flags(&sc->video_mtx,
1813 MTX_QUIET) != 0 || panicstr;
1822 sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp)
1825 mtx_unlock_spin(&sc->video_mtx);
1826 sp->mtx_locked = sp->kdb_locked = FALSE;
1830 sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags)
1834 /* assert(sc_console_unit >= 0) */
1836 sp->kbd_opened = FALSE;
1837 sp->scr_opened = FALSE;
1838 sp->kbd_locked = FALSE;
1840 /* Opening the keyboard is optional. */
1841 if (!(flags & 1) || sc->kbd == NULL)
1844 sccnkbdlock(sc, sp);
1847 * Make sure the keyboard is accessible even when the kbd device
1848 * driver is disabled.
1850 kbdd_enable(sc->kbd);
1852 /* Switch the keyboard to console mode (K_XLATE, polled) on all scp's. */
1854 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&kbd_mode);
1855 sc->kbd_open_level++;
1856 kbdd_poll(sc->kbd, TRUE);
1858 sp->kbd_opened = TRUE;
1861 /* The screen is opened iff locking it succeeds. */
1862 sccnscrlock(sc, sp);
1863 if (!sp->kdb_locked && !sp->mtx_locked)
1865 sp->scr_opened = TRUE;
1867 /* The screen switch is optional. */
1871 /* try to switch to the kernel console screen */
1873 sc->cur_scp->index != sc_console->index &&
1874 sc->cur_scp->smode.mode == VT_AUTO &&
1875 sc_console->smode.mode == VT_AUTO)
1876 sc_switch_scr(sc, sc_console->index);
1880 sccnclose(sc_softc_t *sc, struct sc_cnstate *sp)
1882 sp->scr_opened = FALSE;
1883 sccnscrunlock(sc, sp);
1885 if (!sp->kbd_opened)
1888 /* Restore keyboard mode (for the current, possibly-changed scp). */
1889 kbdd_poll(sc->kbd, FALSE);
1890 if (--sc->kbd_open_level == 0)
1891 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&sc->cur_scp->kbd_mode);
1893 kbdd_disable(sc->kbd);
1894 sp->kbd_opened = FALSE;
1895 sccnkbdunlock(sc, sp);
1899 * Grabbing switches the screen and keyboard focus to sc_console and the
1900 * keyboard mode to (K_XLATE, polled). Only switching to polled mode is
1901 * essential (for preventing the interrupt handler from eating input
1902 * between polls). Focus is part of the UI, and the other switches are
1903 * work just was well when they are done on every entry and exit.
1905 * Screen switches while grabbed are supported, and to maintain focus for
1906 * this ungrabbing and closing only restore the polling state and then
1907 * the keyboard mode if on the original screen.
1911 sc_cngrab(struct consdev *cp)
1916 sc = sc_console->sc;
1917 lev = atomic_fetchadd_int(&sc->grab_level, 1);
1918 if (lev >= 0 && lev < 2) {
1919 sccnopen(sc, &sc->grab_state[lev], 1 | 2);
1920 sccnscrunlock(sc, &sc->grab_state[lev]);
1921 sccnkbdunlock(sc, &sc->grab_state[lev]);
1926 sc_cnungrab(struct consdev *cp)
1931 sc = sc_console->sc;
1932 lev = atomic_load_acq_int(&sc->grab_level) - 1;
1933 if (lev >= 0 && lev < 2) {
1934 sccnkbdlock(sc, &sc->grab_state[lev]);
1935 sccnscrlock(sc, &sc->grab_state[lev]);
1936 sccnclose(sc, &sc->grab_state[lev]);
1938 atomic_add_int(&sc->grab_level, -1);
1941 static char sc_cnputc_log[0x1000];
1942 static u_int sc_cnputc_loghead;
1943 static u_int sc_cnputc_logtail;
1946 sc_cnputc(struct consdev *cd, int c)
1948 struct sc_cnstate st;
1950 scr_stat *scp = sc_console;
1952 struct sc_term_sw *oldtsw;
1953 #ifndef SC_NO_HISTORY
1957 #endif /* !SC_NO_HISTORY */
1961 /* assert(sc_console != NULL) */
1963 sccnopen(scp->sc, &st, 0);
1968 * In the unlocked case, the logging is intentionally only
1969 * perfectly atomic for the indexes.
1971 head = atomic_fetchadd_int(&sc_cnputc_loghead, 1);
1972 sc_cnputc_log[head % sizeof(sc_cnputc_log)] = c;
1975 * If we couldn't open, do special reentrant output and return to defer
1978 if (!st.scr_opened) {
1983 #ifndef SC_NO_HISTORY
1984 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1985 scp->status &= ~SLKED;
1986 update_kbd_state(scp, scp->status, SLKED);
1987 if (scp->status & BUFFER_SAVED) {
1988 if (!sc_hist_restore(scp))
1989 sc_remove_cutmarking(scp);
1990 scp->status &= ~BUFFER_SAVED;
1991 scp->status |= CURSOR_ENABLED;
1992 sc_draw_cursor_image(scp);
1996 * XXX: Now that TTY's have their own locks, we cannot process
1997 * any data after disabling scroll lock. cnputs already holds a
2000 tp = SC_DEV(scp->sc, scp->index);
2001 /* XXX "tp" can be NULL */
2004 sctty_outwakeup(tp);
2008 #endif /* !SC_NO_HISTORY */
2010 /* Play any output still in the log (our char may already be done). */
2011 while (sc_cnputc_logtail != atomic_load_acq_int(&sc_cnputc_loghead)) {
2012 buf[0] = sc_cnputc_log[sc_cnputc_logtail++ % sizeof(sc_cnputc_log)];
2013 if (atomic_load_acq_int(&sc_cnputc_loghead) - sc_cnputc_logtail >=
2014 sizeof(sc_cnputc_log))
2016 /* Console output has a per-CPU "input" state. Switch for it. */
2019 ts = sc_kts[PCPU_GET(cpuid)];
2023 (*scp->tsw->te_sync)(scp);
2025 sc_puts(scp, buf, 1);
2028 (*scp->tsw->te_sync)(scp);
2031 s = spltty(); /* block sckbdevent and scrn_timer */
2034 sccnclose(scp->sc, &st);
2038 sc_cngetc(struct consdev *cd)
2040 struct sc_cnstate st;
2043 /* assert(sc_console != NULL) */
2044 sccnopen(sc_console->sc, &st, 1);
2045 s = spltty(); /* block sckbdevent and scrn_timer while we poll */
2046 if (!st.kbd_opened) {
2048 sccnclose(sc_console->sc, &st);
2049 return -1; /* means no keyboard since we fudged the locking */
2051 c = sc_cngetc_locked(&st);
2053 sccnclose(sc_console->sc, &st);
2058 sc_cngetc_locked(struct sc_cnstate *sp)
2060 static struct fkeytab fkey;
2067 * Stop the screen saver and update the screen if necessary.
2068 * What if we have been running in the screen saver code... XXX
2071 sc_touch_scrn_saver();
2072 scp = sc_console->sc->cur_scp; /* XXX */
2076 if (fkeycp < fkey.len)
2077 return fkey.str[fkeycp++];
2079 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK, sp);
2081 switch (KEYFLAGS(c)) {
2082 case 0: /* normal char */
2084 case FKEY: /* function key */
2085 p = (*scp->tsw->te_fkeystr)(scp, c);
2087 fkey.len = strlen(p);
2088 bcopy(p, fkey.str, fkey.len);
2092 p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
2094 if ((p != NULL) && (fkey.len > 0)) {
2095 bcopy(p, fkey.str, fkey.len);
2109 sccnupdate(scr_stat *scp)
2111 /* this is a cut-down version of scrn_timer()... */
2113 if (suspend_in_progress || scp->sc->font_loading_in_progress)
2116 if (kdb_active || panicstr || shutdown_in_progress) {
2117 sc_touch_scrn_saver();
2118 } else if (scp != scp->sc->cur_scp) {
2122 if (!run_scrn_saver)
2123 scp->sc->flags &= ~SC_SCRN_IDLE;
2125 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
2126 if (scp->sc->flags & SC_SCRN_BLANKED)
2127 stop_scrn_saver(scp->sc, current_saver);
2130 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
2131 || scp->sc->switch_in_progress)
2134 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
2135 * when write_in_progress is non-zero. XXX
2138 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
2139 scrn_update(scp, TRUE);
2143 scrn_timer(void *arg)
2145 static time_t kbd_time_stamp = 0;
2150 again = (arg != NULL);
2152 sc = (sc_softc_t *)arg;
2153 else if (sc_console != NULL)
2154 sc = sc_console->sc;
2158 /* find the vty to update */
2161 /* don't do anything when we are performing some I/O operations */
2162 if (suspend_in_progress || sc->font_loading_in_progress)
2165 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
2166 /* try to allocate a keyboard automatically */
2167 if (kbd_time_stamp != time_uptime) {
2168 kbd_time_stamp = time_uptime;
2169 sc->keyboard = sc_allocate_keyboard(sc, -1);
2170 if (sc->keyboard >= 0) {
2171 sc->kbd = kbd_get_keyboard(sc->keyboard);
2172 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
2173 (caddr_t)&sc->cur_scp->kbd_mode);
2174 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
2180 /* should we stop the screen saver? */
2181 if (kdb_active || panicstr || shutdown_in_progress)
2182 sc_touch_scrn_saver();
2183 if (run_scrn_saver) {
2184 if (time_uptime > sc->scrn_time_stamp + scrn_blank_time)
2185 sc->flags |= SC_SCRN_IDLE;
2187 sc->flags &= ~SC_SCRN_IDLE;
2189 sc->scrn_time_stamp = time_uptime;
2190 sc->flags &= ~SC_SCRN_IDLE;
2191 if (scrn_blank_time > 0)
2192 run_scrn_saver = TRUE;
2195 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
2196 if (sc->flags & SC_SCRN_BLANKED)
2197 stop_scrn_saver(sc, current_saver);
2200 /* should we just return ? */
2201 if (sc->blink_in_progress || sc->switch_in_progress
2202 || sc->write_in_progress)
2205 /* Update the screen */
2206 scp = sc->cur_scp; /* cur_scp may have changed... */
2207 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2208 scrn_update(scp, TRUE);
2211 /* should we activate the screen saver? */
2212 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
2213 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
2214 (*current_saver)(sc, TRUE);
2220 * Use reduced "refresh" rate if we are in graphics and that is not a
2221 * graphical screen saver. In such case we just have nothing to do.
2223 if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2227 callout_reset_sbt(&sc->ctimeout, SBT_1S / rate, 0,
2228 scrn_timer, sc, C_PREL(1));
2233 and_region(int *s1, int *e1, int s2, int e2)
2235 if (*e1 < s2 || e2 < *s1)
2237 *s1 = imax(*s1, s2);
2238 *e1 = imin(*e1, e2);
2243 scrn_update(scr_stat *scp, int show_cursor)
2250 /* assert(scp == scp->sc->cur_scp) */
2252 SC_VIDEO_LOCK(scp->sc);
2254 #ifndef SC_NO_CUTPASTE
2255 /* remove the previous mouse pointer image if necessary */
2256 if (scp->status & MOUSE_VISIBLE) {
2258 e = scp->mouse_pos + scp->xsize + 1;
2259 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
2260 || and_region(&s, &e, scp->start, scp->end)
2261 || ((scp->status & CURSOR_ENABLED) &&
2262 (scp->cursor_pos != scp->cursor_oldpos) &&
2263 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
2264 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
2265 sc_remove_mouse_image(scp);
2266 if (scp->end >= scp->xsize*scp->ysize)
2267 scp->end = scp->xsize*scp->ysize - 1;
2270 #endif /* !SC_NO_CUTPASTE */
2274 if (scp->end >= scp->xsize*scp->ysize) {
2275 printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
2276 scp->end = scp->xsize*scp->ysize - 1;
2278 if (scp->start < 0) {
2279 printf("scrn_update(): scp->start %d < 0\n", scp->start);
2284 /* update screen image */
2285 if (scp->start <= scp->end) {
2286 if (scp->mouse_cut_end >= 0) {
2287 /* there is a marked region for cut & paste */
2288 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
2289 start = scp->mouse_cut_start;
2290 end = scp->mouse_cut_end;
2292 start = scp->mouse_cut_end;
2293 end = scp->mouse_cut_start - 1;
2297 /* does the cut-mark region overlap with the update region? */
2298 if (and_region(&s, &e, scp->start, scp->end)) {
2299 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2302 if (and_region(&s, &e, scp->start, scp->end))
2303 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2305 e = scp->xsize*scp->ysize - 1;
2306 if (and_region(&s, &e, scp->start, scp->end))
2307 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2309 (*scp->rndr->draw)(scp, scp->start,
2310 scp->end - scp->start + 1, FALSE);
2313 (*scp->rndr->draw)(scp, scp->start,
2314 scp->end - scp->start + 1, FALSE);
2318 /* we are not to show the cursor and the mouse pointer... */
2321 scp->start = scp->xsize*scp->ysize - 1;
2322 SC_VIDEO_UNLOCK(scp->sc);
2326 /* update cursor image */
2327 if (scp->status & CURSOR_ENABLED) {
2330 /* did cursor move since last time ? */
2331 if (scp->cursor_pos != scp->cursor_oldpos) {
2332 /* do we need to remove old cursor image ? */
2333 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2334 sc_remove_cursor_image(scp);
2335 sc_draw_cursor_image(scp);
2337 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
2338 /* cursor didn't move, but has been overwritten */
2339 sc_draw_cursor_image(scp);
2340 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
2341 /* if it's a blinking cursor, update it */
2342 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2343 sc_inside_cutmark(scp,
2348 #ifndef SC_NO_CUTPASTE
2349 /* update "pseudo" mouse pointer image */
2350 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2351 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2352 scp->status &= ~MOUSE_MOVED;
2353 sc_draw_mouse_image(scp);
2356 #endif /* SC_NO_CUTPASTE */
2359 scp->start = scp->xsize*scp->ysize - 1;
2361 SC_VIDEO_UNLOCK(scp->sc);
2366 scsplash_callback(int event, void *arg)
2371 sc = (sc_softc_t *)arg;
2375 if (add_scrn_saver(scsplash_saver) == 0) {
2376 sc->flags &= ~SC_SAVER_FAILED;
2377 run_scrn_saver = TRUE;
2378 if (cold && !(boothowto & RB_VERBOSE)) {
2379 scsplash_stick(TRUE);
2380 (*current_saver)(sc, TRUE);
2386 if (current_saver == scsplash_saver) {
2387 scsplash_stick(FALSE);
2388 error = remove_scrn_saver(scsplash_saver);
2400 scsplash_saver(sc_softc_t *sc, int show)
2402 static int busy = FALSE;
2411 if (!(sc->flags & SC_SAVER_FAILED)) {
2412 if (!(sc->flags & SC_SCRN_BLANKED))
2413 set_scrn_saver_mode(scp, -1, NULL, 0);
2414 switch (splash(sc->adp, TRUE)) {
2415 case 0: /* succeeded */
2417 case EAGAIN: /* try later */
2418 restore_scrn_saver_mode(scp, FALSE);
2419 sc_touch_scrn_saver(); /* XXX */
2422 sc->flags |= SC_SAVER_FAILED;
2423 scsplash_stick(FALSE);
2424 restore_scrn_saver_mode(scp, TRUE);
2425 printf("scsplash_saver(): failed to put up the image\n");
2429 } else if (!sticky_splash) {
2430 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2431 restore_scrn_saver_mode(scp, TRUE);
2437 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2442 if (current_saver != none_saver) {
2443 error = remove_scrn_saver(current_saver);
2448 if (current_saver != none_saver)
2451 run_scrn_saver = FALSE;
2452 saver_mode = CONS_LKM_SAVER;
2453 current_saver = this_saver;
2458 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2460 if (current_saver != this_saver)
2465 * In order to prevent `current_saver' from being called by
2466 * the timeout routine `scrn_timer()' while we manipulate
2467 * the saver list, we shall set `current_saver' to `none_saver'
2468 * before stopping the current saver, rather than blocking by `splXX()'.
2470 current_saver = none_saver;
2472 stop_scrn_saver(this_saver);
2475 /* unblank all blanked screens */
2476 wait_scrn_saver_stop(NULL);
2480 current_saver = none_saver;
2485 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2489 /* assert(scp == scp->sc->cur_scp) */
2491 if (!ISGRAPHSC(scp))
2492 sc_remove_cursor_image(scp);
2493 scp->splash_save_mode = scp->mode;
2494 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2495 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2496 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2497 scp->sc->flags |= SC_SCRN_BLANKED;
2503 if (set_mode(scp) == 0) {
2504 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2505 scp->status |= GRAPHICS_MODE;
2506 #ifndef SC_NO_PALETTE_LOADING
2508 vidd_load_palette(scp->sc->adp, pal);
2510 sc_set_border(scp, border);
2514 scp->mode = scp->splash_save_mode;
2515 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2516 scp->status |= scp->splash_save_status;
2523 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2529 /* assert(scp == scp->sc->cur_scp) */
2532 status = scp->status;
2533 scp->mode = scp->splash_save_mode;
2534 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2535 scp->status |= scp->splash_save_status;
2536 scp->sc->flags &= ~SC_SCRN_BLANKED;
2538 if (!ISGRAPHSC(scp))
2539 sc_draw_cursor_image(scp);
2544 if (set_mode(scp) == 0) {
2545 #ifndef SC_NO_PALETTE_LOADING
2546 #ifdef SC_PIXEL_MODE
2547 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2548 vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2551 vidd_load_palette(scp->sc->adp, scp->sc->palette);
2558 scp->status = status;
2565 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2567 (*saver)(sc, FALSE);
2568 run_scrn_saver = FALSE;
2569 /* the screen saver may have chosen not to stop after all... */
2570 if (sc->flags & SC_SCRN_BLANKED)
2573 mark_all(sc->cur_scp);
2574 if (sc->delayed_next_scr)
2575 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2577 wakeup(&scrn_blanked);
2581 wait_scrn_saver_stop(sc_softc_t *sc)
2585 while (scrn_blanked > 0) {
2586 run_scrn_saver = FALSE;
2587 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2591 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2592 if ((error != 0) && (error != ERESTART))
2595 run_scrn_saver = FALSE;
2598 #endif /* DEV_SPLASH */
2601 sc_touch_scrn_saver(void)
2603 scsplash_stick(FALSE);
2604 run_scrn_saver = FALSE;
2608 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2615 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2617 if (sc->cur_scp == NULL)
2620 /* prevent switch if previously requested */
2621 if (sc->flags & SC_SCRN_VTYLOCK) {
2622 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2623 sc->cur_scp->bell_duration);
2627 /* delay switch if the screen is blanked or being updated */
2628 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2629 || sc->blink_in_progress) {
2630 sc->delayed_next_scr = next_scr + 1;
2631 sc_touch_scrn_saver();
2632 DPRINTF(5, ("switch delayed\n"));
2635 sc->delayed_next_scr = 0;
2638 cur_scp = sc->cur_scp;
2640 /* we are in the middle of the vty switching process... */
2641 if (sc->switch_in_progress
2642 && (cur_scp->smode.mode == VT_PROCESS)
2644 p = pfind(cur_scp->pid);
2645 if (cur_scp->proc != p) {
2649 * The controlling process has died!!. Do some clean up.
2650 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2651 * are not reset here yet; they will be cleared later.
2653 DPRINTF(5, ("cur_scp controlling process %d died, ",
2655 if (cur_scp->status & SWITCH_WAIT_REL) {
2657 * Force the previous switch to finish, but return now
2660 DPRINTF(5, ("reset WAIT_REL, "));
2661 finish_vt_rel(cur_scp, TRUE, &s);
2663 DPRINTF(5, ("finishing previous switch\n"));
2665 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2666 /* let's assume screen switch has been completed. */
2667 DPRINTF(5, ("reset WAIT_ACQ, "));
2668 finish_vt_acq(cur_scp);
2671 * We are in between screen release and acquisition, and
2672 * reached here via scgetc() or scrn_timer() which has
2673 * interrupted exchange_scr(). Don't do anything stupid.
2675 DPRINTF(5, ("waiting nothing, "));
2681 * The controlling process is alive, but not responding...
2682 * It is either buggy or it may be just taking time.
2683 * The following code is a gross kludge to cope with this
2684 * problem for which there is no clean solution. XXX
2686 if (cur_scp->status & SWITCH_WAIT_REL) {
2687 switch (sc->switch_in_progress++) {
2691 DPRINTF(5, ("sending relsig again, "));
2692 signal_vt_rel(cur_scp);
2699 * Act as if the controlling program returned
2702 DPRINTF(5, ("force reset WAIT_REL, "));
2703 finish_vt_rel(cur_scp, FALSE, &s);
2705 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2708 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2709 switch (sc->switch_in_progress++) {
2713 DPRINTF(5, ("sending acqsig again, "));
2714 signal_vt_acq(cur_scp);
2720 /* clear the flag and finish the previous switch */
2721 DPRINTF(5, ("force reset WAIT_ACQ, "));
2722 finish_vt_acq(cur_scp);
2730 * Return error if an invalid argument is given, or vty switch
2731 * is still in progress.
2733 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2734 || sc->switch_in_progress) {
2736 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2737 DPRINTF(5, ("error 1\n"));
2742 * Don't allow switching away from the graphics mode vty
2743 * if the switch mode is VT_AUTO, unless the next vty is the same
2744 * as the current or the current vty has been closed (but showing).
2746 tp = SC_DEV(sc, cur_scp->index);
2747 if ((cur_scp->index != next_scr)
2748 && tty_opened_ns(tp)
2749 && (cur_scp->smode.mode == VT_AUTO)
2750 && ISGRAPHSC(cur_scp)) {
2752 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2753 DPRINTF(5, ("error, graphics mode\n"));
2758 * Is the wanted vty open? Don't allow switching to a closed vty.
2759 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2760 * Note that we always allow the user to switch to the kernel
2761 * console even if it is closed.
2763 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2764 tp = SC_DEV(sc, next_scr);
2765 if (!tty_opened_ns(tp)) {
2767 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2768 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2771 if (kdb_active && SC_STAT(tp)->smode.mode == VT_PROCESS) {
2773 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2778 /* this is the start of vty switching process... */
2779 ++sc->switch_in_progress;
2780 sc->old_scp = cur_scp;
2781 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2782 if (sc->new_scp == sc->old_scp) {
2783 sc->switch_in_progress = 0;
2785 * XXX wakeup() locks the scheduler lock which will hang if
2786 * the lock is in an in-between state, e.g., when we stop at
2787 * a breakpoint at fork_exit. It has always been wrong to call
2788 * wakeup() when the debugger is active. In RELENG_4, wakeup()
2789 * is supposed to be locked by splhigh(), but the debugger may
2790 * be invoked at splhigh().
2793 wakeup(VTY_WCHAN(sc,next_scr));
2795 DPRINTF(5, ("switch done (new == old)\n"));
2799 /* has controlling process died? */
2800 vt_proc_alive(sc->old_scp);
2801 vt_proc_alive(sc->new_scp);
2803 /* wait for the controlling process to release the screen, if necessary */
2804 if (signal_vt_rel(sc->old_scp)) {
2809 /* go set up the new vty screen */
2814 /* wake up processes waiting for this vty */
2816 wakeup(VTY_WCHAN(sc,next_scr));
2818 /* wait for the controlling process to acknowledge, if necessary */
2819 if (signal_vt_acq(sc->cur_scp)) {
2824 sc->switch_in_progress = 0;
2825 if (sc->unit == sc_console_unit)
2826 cnavailable(sc_consptr, TRUE);
2828 DPRINTF(5, ("switch done\n"));
2834 do_switch_scr(sc_softc_t *sc, int s)
2836 vt_proc_alive(sc->new_scp);
2841 /* sc->cur_scp == sc->new_scp */
2842 wakeup(VTY_WCHAN(sc,sc->cur_scp->index));
2844 /* wait for the controlling process to acknowledge, if necessary */
2845 if (!signal_vt_acq(sc->cur_scp)) {
2846 sc->switch_in_progress = 0;
2847 if (sc->unit == sc_console_unit)
2848 cnavailable(sc_consptr, TRUE);
2855 vt_proc_alive(scr_stat *scp)
2860 if ((p = pfind(scp->pid)) != NULL)
2865 scp->smode.mode = VT_AUTO;
2866 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2872 signal_vt_rel(scr_stat *scp)
2874 if (scp->smode.mode != VT_PROCESS)
2876 scp->status |= SWITCH_WAIT_REL;
2877 PROC_LOCK(scp->proc);
2878 kern_psignal(scp->proc, scp->smode.relsig);
2879 PROC_UNLOCK(scp->proc);
2880 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2885 signal_vt_acq(scr_stat *scp)
2887 if (scp->smode.mode != VT_PROCESS)
2889 if (scp->sc->unit == sc_console_unit)
2890 cnavailable(sc_consptr, FALSE);
2891 scp->status |= SWITCH_WAIT_ACQ;
2892 PROC_LOCK(scp->proc);
2893 kern_psignal(scp->proc, scp->smode.acqsig);
2894 PROC_UNLOCK(scp->proc);
2895 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2900 finish_vt_rel(scr_stat *scp, int release, int *s)
2902 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2903 scp->status &= ~SWITCH_WAIT_REL;
2905 *s = do_switch_scr(scp->sc, *s);
2907 scp->sc->switch_in_progress = 0;
2914 finish_vt_acq(scr_stat *scp)
2916 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2917 scp->status &= ~SWITCH_WAIT_ACQ;
2918 scp->sc->switch_in_progress = 0;
2925 exchange_scr(sc_softc_t *sc)
2929 /* save the current state of video and keyboard */
2930 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2931 if (!ISGRAPHSC(sc->old_scp))
2932 sc_remove_cursor_image(sc->old_scp);
2933 if (sc->old_scp->kbd_mode == K_XLATE)
2934 save_kbd_state(sc->old_scp);
2936 /* set up the video for the new screen */
2937 scp = sc->cur_scp = sc->new_scp;
2938 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2942 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2943 (void *)sc->adp->va_window, FALSE);
2945 scp->status |= MOUSE_HIDDEN;
2946 sc_move_cursor(scp, scp->xpos, scp->ypos);
2947 if (!ISGRAPHSC(scp))
2948 sc_set_cursor_image(scp);
2949 #ifndef SC_NO_PALETTE_LOADING
2950 if (ISGRAPHSC(sc->old_scp)) {
2951 #ifdef SC_PIXEL_MODE
2952 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2953 vidd_load_palette(sc->adp, sc->palette2);
2956 vidd_load_palette(sc->adp, sc->palette);
2959 sc_set_border(scp, scp->border);
2961 /* set up the keyboard for the new screen */
2962 if (sc->kbd_open_level == 0 && sc->old_scp->kbd_mode != scp->kbd_mode)
2963 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2964 update_kbd_state(scp, scp->status, LOCK_MASK);
2970 sc_puts(scr_stat *scp, u_char *buf, int len)
2973 /* make screensaver happy */
2974 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2975 run_scrn_saver = FALSE;
2979 (*scp->tsw->te_puts)(scp, buf, len);
2980 if (scp->sc->delayed_next_scr)
2981 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2985 sc_draw_cursor_image(scr_stat *scp)
2987 /* assert(scp == scp->sc->cur_scp); */
2988 SC_VIDEO_LOCK(scp->sc);
2989 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2990 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2991 sc_inside_cutmark(scp, scp->cursor_pos));
2992 scp->cursor_oldpos = scp->cursor_pos;
2993 SC_VIDEO_UNLOCK(scp->sc);
2997 sc_remove_cursor_image(scr_stat *scp)
2999 /* assert(scp == scp->sc->cur_scp); */
3000 SC_VIDEO_LOCK(scp->sc);
3001 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
3002 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
3003 sc_inside_cutmark(scp, scp->cursor_oldpos));
3004 SC_VIDEO_UNLOCK(scp->sc);
3008 update_cursor_image(scr_stat *scp)
3010 /* assert(scp == scp->sc->cur_scp); */
3011 sc_remove_cursor_image(scp);
3012 sc_set_cursor_image(scp);
3013 sc_draw_cursor_image(scp);
3017 sc_set_cursor_image(scr_stat *scp)
3019 scp->curs_attr = scp->base_curs_attr;
3020 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
3021 /* hidden cursor is internally represented as zero-height underline */
3022 scp->curs_attr.flags = CONS_CHAR_CURSOR;
3023 scp->curs_attr.base = scp->curs_attr.height = 0;
3024 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
3025 scp->curs_attr.base = imin(scp->base_curs_attr.base,
3026 scp->font_size - 1);
3027 scp->curs_attr.height = imin(scp->base_curs_attr.height,
3028 scp->font_size - scp->curs_attr.base);
3029 } else { /* block cursor */
3030 scp->curs_attr.base = 0;
3031 scp->curs_attr.height = scp->font_size;
3034 /* assert(scp == scp->sc->cur_scp); */
3035 SC_VIDEO_LOCK(scp->sc);
3036 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
3037 scp->curs_attr.flags & CONS_BLINK_CURSOR);
3038 SC_VIDEO_UNLOCK(scp->sc);
3042 sc_adjust_ca(struct cursor_attr *cap, int flags, int base, int height)
3044 if (flags & CONS_CHARCURSOR_COLORS) {
3045 cap->bg[0] = base & 0xff;
3046 cap->bg[1] = height & 0xff;
3047 } else if (flags & CONS_MOUSECURSOR_COLORS) {
3048 cap->mouse_ba = base & 0xff;
3049 cap->mouse_ia = height & 0xff;
3054 cap->height = height;
3055 if (!(flags & CONS_SHAPEONLY_CURSOR))
3056 cap->flags = flags & CONS_CURSOR_ATTRS;
3061 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
3063 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
3064 sc_remove_cursor_image(scp);
3066 if (flags & CONS_RESET_CURSOR)
3067 scp->base_curs_attr = scp->dflt_curs_attr;
3068 else if (flags & CONS_DEFAULT_CURSOR) {
3069 sc_adjust_ca(&scp->dflt_curs_attr, flags, base, height);
3070 scp->base_curs_attr = scp->dflt_curs_attr;
3072 sc_adjust_ca(&scp->base_curs_attr, flags, base, height);
3074 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
3075 sc_set_cursor_image(scp);
3076 sc_draw_cursor_image(scp);
3081 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
3089 flags = CONS_SHAPEONLY_CURSOR;
3092 if (flags & CONS_LOCAL_CURSOR) {
3093 /* local (per vty) change */
3094 change_cursor_shape(scp, flags, base, height);
3101 if (flags & CONS_RESET_CURSOR)
3102 sc->curs_attr = sc->dflt_curs_attr;
3103 else if (flags & CONS_DEFAULT_CURSOR) {
3104 sc_adjust_ca(&sc->dflt_curs_attr, flags, base, height);
3105 sc->curs_attr = sc->dflt_curs_attr;
3107 sc_adjust_ca(&sc->curs_attr, flags, base, height);
3109 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
3110 if ((tp = SC_DEV(sc, i)) == NULL)
3112 if ((scp = sc_get_stat(tp)) == NULL)
3114 scp->dflt_curs_attr = sc->curs_attr;
3115 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
3121 scinit(int unit, int flags)
3125 * When syscons is being initialized as the kernel console, malloc()
3126 * is not yet functional, because various kernel structures has not been
3127 * fully initialized yet. Therefore, we need to declare the following
3128 * static buffers for the console. This is less than ideal,
3129 * but is necessry evil for the time being. XXX
3131 static u_short sc_buffer[ROW*COL]; /* XXX */
3132 #ifndef SC_NO_FONT_LOADING
3133 static u_char font_8[256*8];
3134 static u_char font_14[256*14];
3135 static u_char font_16[256*16];
3140 video_adapter_t *adp;
3145 /* one time initialization */
3146 if (init_done == COLD) {
3147 sc_get_bios_values(&bios_value);
3148 for (i = 0; i < nitems(sc_kattrtab); i++) {
3149 #if SC_KERNEL_CONS_ATTR == FG_WHITE
3150 sc_kattrtab[i] = 8 + (i + FG_WHITE) % 8U;
3152 sc_kattrtab[i] = SC_KERNEL_CONS_ATTR;
3159 * Allocate resources. Even if we are being called for the second
3160 * time, we must allocate them again, because they might have
3163 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3164 if ((sc->flags & SC_INIT_DONE) == 0)
3165 SC_VIDEO_LOCKINIT(sc);
3168 if (sc->adapter >= 0) {
3169 vid_release(sc->adp, (void *)&sc->adapter);
3173 if (sc->keyboard >= 0) {
3174 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
3175 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
3176 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
3177 if (sc->kbd != NULL) {
3178 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
3179 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
3183 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
3184 sc->adp = vid_get_adapter(sc->adapter);
3185 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
3187 sc->keyboard = sc_allocate_keyboard(sc, unit);
3188 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
3190 sc->kbd = kbd_get_keyboard(sc->keyboard);
3191 if (sc->kbd != NULL) {
3192 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
3193 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
3196 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
3198 sc->initial_mode = sc->adp->va_initial_mode;
3200 #ifndef SC_NO_FONT_LOADING
3201 if (flags & SC_KERNEL_CONSOLE) {
3202 sc->font_8 = font_8;
3203 sc->font_14 = font_14;
3204 sc->font_16 = font_16;
3205 } else if (sc->font_8 == NULL) {
3206 /* assert(sc_malloc) */
3207 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
3208 sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
3209 sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
3213 /* extract the hardware cursor location and hide the cursor for now */
3214 vidd_read_hw_cursor(sc->adp, &col, &row);
3215 vidd_set_hw_cursor(sc->adp, -1, -1);
3217 /* set up the first console */
3218 sc->first_vty = unit*MAXCONS;
3219 sc->vtys = MAXCONS; /* XXX: should be configurable */
3220 if (flags & SC_KERNEL_CONSOLE) {
3222 * Set up devs structure but don't use it yet, calling make_dev()
3223 * might panic kernel. Wait for sc_attach_unit() to actually
3224 * create the devices.
3226 sc->dev = main_devs;
3227 scp = &main_console;
3228 init_scp(sc, sc->first_vty, scp);
3229 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
3230 (void *)sc_buffer, FALSE);
3231 if (sc_init_emulator(scp, SC_DFLT_TERM))
3232 sc_init_emulator(scp, "*");
3233 (*scp->tsw->te_default_attr)(scp, SC_KERNEL_CONS_ATTR,
3234 SC_KERNEL_CONS_REV_ATTR);
3236 /* assert(sc_malloc) */
3237 sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF,
3239 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
3240 scp = alloc_scp(sc, sc->first_vty);
3241 SC_STAT(sc->dev[0]) = scp;
3246 /* copy screen to temporary buffer */
3247 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3248 (void *)scp->sc->adp->va_window, FALSE);
3250 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
3253 /* Sync h/w cursor position to s/w (sc and teken). */
3254 if (col >= scp->xsize)
3256 if (row >= scp->ysize)
3257 row = scp->ysize - 1;
3260 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
3261 (*scp->tsw->te_sync)(scp);
3263 sc->dflt_curs_attr.base = 0;
3264 sc->dflt_curs_attr.height = howmany(scp->font_size, 8);
3265 sc->dflt_curs_attr.flags = 0;
3266 sc->dflt_curs_attr.bg[0] = FG_RED;
3267 sc->dflt_curs_attr.bg[1] = FG_LIGHTGREY;
3268 sc->dflt_curs_attr.bg[2] = FG_BLUE;
3269 sc->dflt_curs_attr.mouse_ba = FG_WHITE;
3270 sc->dflt_curs_attr.mouse_ia = FG_RED;
3271 sc->curs_attr = sc->dflt_curs_attr;
3272 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3273 scp->curs_attr = scp->base_curs_attr;
3275 #ifndef SC_NO_SYSMOUSE
3276 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
3278 if (!ISGRAPHSC(scp)) {
3279 sc_set_cursor_image(scp);
3280 sc_draw_cursor_image(scp);
3283 /* save font and palette */
3284 #ifndef SC_NO_FONT_LOADING
3285 sc->fonts_loaded = 0;
3286 if (ISFONTAVAIL(sc->adp->va_flags)) {
3288 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
3289 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
3290 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
3291 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
3292 if (scp->font_size < 14) {
3293 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3294 } else if (scp->font_size >= 16) {
3295 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3297 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3299 #else /* !SC_DFLT_FONT */
3300 if (scp->font_size < 14) {
3301 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3302 sc->fonts_loaded = FONT_8;
3303 } else if (scp->font_size >= 16) {
3304 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3305 sc->fonts_loaded = FONT_16;
3307 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3308 sc->fonts_loaded = FONT_14;
3310 #endif /* SC_DFLT_FONT */
3311 /* FONT KLUDGE: always use the font page #0. XXX */
3312 sc_show_font(scp, 0);
3314 #endif /* !SC_NO_FONT_LOADING */
3316 #ifndef SC_NO_PALETTE_LOADING
3317 vidd_save_palette(sc->adp, sc->palette);
3318 #ifdef SC_PIXEL_MODE
3319 for (i = 0; i < sizeof(sc->palette2); i++)
3320 sc->palette2[i] = i / 3;
3325 if (!(sc->flags & SC_SPLASH_SCRN)) {
3326 /* we are ready to put up the splash image! */
3327 splash_init(sc->adp, scsplash_callback, sc);
3328 sc->flags |= SC_SPLASH_SCRN;
3333 /* the rest is not necessary, if we have done it once */
3334 if (sc->flags & SC_INIT_DONE)
3337 /* initialize mapscrn arrays to a one to one map */
3338 for (i = 0; i < sizeof(sc->scr_map); i++)
3339 sc->scr_map[i] = sc->scr_rmap[i] = i;
3341 sc->flags |= SC_INIT_DONE;
3345 scterm(int unit, int flags)
3350 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3352 return; /* shouldn't happen */
3355 /* this console is no longer available for the splash screen */
3356 if (sc->flags & SC_SPLASH_SCRN) {
3357 splash_term(sc->adp);
3358 sc->flags &= ~SC_SPLASH_SCRN;
3363 /* move the hardware cursor to the upper-left corner */
3364 vidd_set_hw_cursor(sc->adp, 0, 0);
3367 /* release the keyboard and the video card */
3368 if (sc->keyboard >= 0)
3369 kbd_release(sc->kbd, &sc->keyboard);
3370 if (sc->adapter >= 0)
3371 vid_release(sc->adp, &sc->adapter);
3373 /* stop the terminal emulator, if any */
3374 scp = sc_get_stat(sc->dev[0]);
3376 (*scp->tsw->te_term)(scp, &scp->ts);
3377 mtx_destroy(&sc->video_mtx);
3379 /* clear the structure */
3380 if (!(flags & SC_KERNEL_CONSOLE)) {
3381 free(scp->ts, M_DEVBUF);
3382 /* XXX: We need delete_dev() for this */
3383 free(sc->dev, M_DEVBUF);
3385 /* XXX: We need a ttyunregister for this */
3386 free(sc->tty, M_DEVBUF);
3388 #ifndef SC_NO_FONT_LOADING
3389 free(sc->font_8, M_DEVBUF);
3390 free(sc->font_14, M_DEVBUF);
3391 free(sc->font_16, M_DEVBUF);
3393 /* XXX vtb, history */
3395 bzero(sc, sizeof(*sc));
3401 scshutdown(__unused void *arg, __unused int howto)
3404 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3405 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3406 KASSERT(sc_console->sc->cur_scp != NULL,
3407 ("sc_console->sc->cur_scp != NULL"));
3409 sc_touch_scrn_saver();
3411 sc_console->sc->cur_scp->index != sc_console->index &&
3412 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3413 sc_console->smode.mode == VT_AUTO)
3414 sc_switch_scr(sc_console->sc, sc_console->index);
3415 shutdown_in_progress = TRUE;
3419 scsuspend(__unused void *arg)
3423 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3424 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3425 KASSERT(sc_console->sc->cur_scp != NULL,
3426 ("sc_console->sc->cur_scp != NULL"));
3428 sc_susp_scr = sc_console->sc->cur_scp->index;
3429 if (sc_no_suspend_vtswitch ||
3430 sc_susp_scr == sc_console->index) {
3431 sc_touch_scrn_saver();
3435 for (retry = 0; retry < 10; retry++) {
3436 sc_switch_scr(sc_console->sc, sc_console->index);
3437 if (!sc_console->sc->switch_in_progress)
3439 pause("scsuspend", hz);
3441 suspend_in_progress = TRUE;
3445 scresume(__unused void *arg)
3448 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3449 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3450 KASSERT(sc_console->sc->cur_scp != NULL,
3451 ("sc_console->sc->cur_scp != NULL"));
3453 suspend_in_progress = FALSE;
3454 if (sc_susp_scr < 0) {
3455 update_font(sc_console->sc->cur_scp);
3458 sc_switch_scr(sc_console->sc, sc_susp_scr);
3462 sc_clean_up(scr_stat *scp)
3468 if (scp->sc->flags & SC_SCRN_BLANKED) {
3469 sc_touch_scrn_saver();
3471 if ((error = wait_scrn_saver_stop(scp->sc)))
3475 scp->status |= MOUSE_HIDDEN;
3476 sc_remove_mouse_image(scp);
3477 sc_remove_cutmarking(scp);
3482 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3488 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3489 if (!discard && (old.vtb_flags & VTB_VALID)) {
3490 /* retain the current cursor position and buffer contants */
3491 scp->cursor_oldpos = scp->cursor_pos;
3493 * This works only if the old buffer has the same size as or larger
3494 * than the new one. XXX
3496 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3500 sc_vtb_destroy(&old);
3503 #ifndef SC_NO_SYSMOUSE
3504 /* move the mouse cursor at the center of the screen */
3505 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3510 *alloc_scp(sc_softc_t *sc, int vty)
3514 /* assert(sc_malloc) */
3516 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3517 init_scp(sc, vty, scp);
3519 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3520 if (sc_init_emulator(scp, SC_DFLT_TERM))
3521 sc_init_emulator(scp, "*");
3523 #ifndef SC_NO_CUTPASTE
3524 sc_alloc_cut_buffer(scp, TRUE);
3527 #ifndef SC_NO_HISTORY
3528 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3535 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3539 bzero(scp, sizeof(*scp));
3544 scp->mode = sc->initial_mode;
3545 vidd_get_info(sc->adp, scp->mode, &info);
3546 if (info.vi_flags & V_INFO_GRAPHICS) {
3547 scp->status |= GRAPHICS_MODE;
3548 scp->xpixel = info.vi_width;
3549 scp->ypixel = info.vi_height;
3550 scp->xsize = info.vi_width/info.vi_cwidth;
3551 scp->ysize = info.vi_height/info.vi_cheight;
3555 scp->xsize = info.vi_width;
3556 scp->ysize = info.vi_height;
3557 scp->xpixel = scp->xsize*info.vi_cwidth;
3558 scp->ypixel = scp->ysize*info.vi_cheight;
3561 scp->font_size = info.vi_cheight;
3562 scp->font_width = info.vi_cwidth;
3563 #ifndef SC_NO_FONT_LOADING
3564 if (info.vi_cheight < 14)
3565 scp->font = sc->font_8;
3566 else if (info.vi_cheight >= 16)
3567 scp->font = sc->font_16;
3569 scp->font = sc->font_14;
3574 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3576 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3578 scp->xoff = scp->yoff = 0;
3579 scp->xpos = scp->ypos = 0;
3580 scp->start = scp->xsize * scp->ysize - 1;
3585 scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3586 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3587 scp->mouse_cut_start = scp->xsize*scp->ysize;
3588 scp->mouse_cut_end = -1;
3589 scp->mouse_signal = 0;
3591 scp->mouse_proc = NULL;
3592 scp->kbd_mode = K_XLATE;
3593 scp->bell_pitch = bios_value.bell_pitch;
3594 scp->bell_duration = BELL_DURATION;
3595 scp->status |= (bios_value.shift_state & NLKED);
3596 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3599 scp->smode.mode = VT_AUTO;
3600 scp->history = NULL;
3601 scp->history_pos = 0;
3602 scp->history_size = 0;
3606 sc_init_emulator(scr_stat *scp, char *name)
3613 if (name == NULL) /* if no name is given, use the current emulator */
3615 else /* ...otherwise find the named emulator */
3616 sw = sc_term_match(name);
3621 if (strcmp(sw->te_renderer, "*") != 0) {
3622 rndr = sc_render_match(scp, sw->te_renderer,
3623 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3626 rndr = sc_render_match(scp, scp->sc->adp->va_name,
3627 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3632 if (sw == scp->tsw) {
3633 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3635 scp->rndr->init(scp);
3636 sc_clear_screen(scp);
3637 /* assert(error == 0); */
3641 if (sc_malloc && (sw->te_size > 0))
3642 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3645 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3650 (*scp->tsw->te_term)(scp, &scp->ts);
3651 if (scp->ts != NULL)
3652 free(scp->ts, M_DEVBUF);
3656 scp->rndr->init(scp);
3658 (*sw->te_default_attr)(scp, SC_NORM_ATTR, SC_NORM_REV_ATTR);
3659 sc_clear_screen(scp);
3665 * scgetc(flags) - get character from keyboard.
3666 * If flags & SCGETC_CN, then avoid harmful side effects.
3667 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3668 * return NOKEY if there is nothing there.
3671 scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp)
3674 #ifndef SC_NO_HISTORY
3682 if (sc->kbd == NULL)
3687 /* I don't like this, but... XXX */
3688 if (flags & SCGETC_CN)
3689 sccnupdate(sc->cur_scp);
3692 /* first see if there is something in the keyboard port */
3694 if (flags & SCGETC_CN)
3695 sccnscrunlock(sc, sp);
3696 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3697 if (flags & SCGETC_CN)
3698 sccnscrlock(sc, sp);
3700 if (!(flags & SCGETC_CN))
3701 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3702 } else if (c == NOKEY)
3708 /* make screensaver happy */
3710 sc_touch_scrn_saver();
3712 if (!(flags & SCGETC_CN))
3713 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
3715 if (sc->kbd_open_level == 0 && scp->kbd_mode != K_XLATE)
3718 /* if scroll-lock pressed allow history browsing */
3719 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3721 scp->status &= ~CURSOR_ENABLED;
3722 sc_remove_cursor_image(scp);
3724 #ifndef SC_NO_HISTORY
3725 if (!(scp->status & BUFFER_SAVED)) {
3726 scp->status |= BUFFER_SAVED;
3730 /* FIXME: key codes */
3731 case SPCLKEY | FKEY | F(49): /* home key */
3732 sc_remove_cutmarking(scp);
3736 case SPCLKEY | FKEY | F(57): /* end key */
3737 sc_remove_cutmarking(scp);
3741 case SPCLKEY | FKEY | F(50): /* up arrow key */
3742 sc_remove_cutmarking(scp);
3743 if (sc_hist_up_line(scp))
3744 if (!(flags & SCGETC_CN))
3745 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3748 case SPCLKEY | FKEY | F(58): /* down arrow key */
3749 sc_remove_cutmarking(scp);
3750 if (sc_hist_down_line(scp))
3751 if (!(flags & SCGETC_CN))
3752 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3755 case SPCLKEY | FKEY | F(51): /* page up key */
3756 sc_remove_cutmarking(scp);
3757 for (i=0; i<scp->ysize; i++)
3758 if (sc_hist_up_line(scp)) {
3759 if (!(flags & SCGETC_CN))
3760 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3765 case SPCLKEY | FKEY | F(59): /* page down key */
3766 sc_remove_cutmarking(scp);
3767 for (i=0; i<scp->ysize; i++)
3768 if (sc_hist_down_line(scp)) {
3769 if (!(flags & SCGETC_CN))
3770 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3775 #endif /* SC_NO_HISTORY */
3779 * Process and consume special keys here. Return a plain char code
3780 * or a char code with the META flag or a function key code.
3784 /* goto next_code */
3789 switch (KEYCHAR(c)) {
3791 case NLK: case CLK: case ALK:
3794 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3796 scp->status |= SLKED;
3798 if (scp->status & SLKED) {
3799 scp->status &= ~SLKED;
3800 #ifndef SC_NO_HISTORY
3801 if (scp->status & BUFFER_SAVED) {
3802 if (!sc_hist_restore(scp))
3803 sc_remove_cutmarking(scp);
3804 scp->status &= ~BUFFER_SAVED;
3805 scp->status |= CURSOR_ENABLED;
3806 sc_draw_cursor_image(scp);
3808 /* Only safe in Giant-locked context. */
3809 tp = SC_DEV(sc, scp->index);
3810 if (!(flags & SCGETC_CN) && tty_opened_ns(tp))
3811 sctty_outwakeup(tp);
3818 #ifndef SC_NO_CUTPASTE
3819 sc_mouse_paste(scp);
3823 /* NON-LOCKING KEYS */
3825 case LSH: case RSH: case LCTR: case RCTR:
3826 case LALT: case RALT: case ASH: case META:
3830 if (!(sc->flags & SC_SCRN_BLANKED))
3836 /* force activatation/deactivation of the screen saver */
3837 if (!(sc->flags & SC_SCRN_BLANKED)) {
3838 run_scrn_saver = TRUE;
3839 sc->scrn_time_stamp -= scrn_blank_time;
3843 * While devices are being probed, the screen saver need
3844 * to be invoked explicitly. XXX
3846 if (sc->flags & SC_SCRN_BLANKED) {
3847 scsplash_stick(FALSE);
3848 stop_scrn_saver(sc, current_saver);
3850 if (!ISGRAPHSC(scp)) {
3851 scsplash_stick(TRUE);
3852 (*current_saver)(sc, TRUE);
3856 #endif /* DEV_SPLASH */
3860 #ifndef SC_DISABLE_REBOOT
3861 if (enable_reboot && !(flags & SCGETC_CN))
3867 #ifndef SC_DISABLE_REBOOT
3868 if (enable_reboot && !(flags & SCGETC_CN))
3869 shutdown_nice(RB_HALT);
3874 #ifndef SC_DISABLE_REBOOT
3875 if (enable_reboot && !(flags & SCGETC_CN))
3876 shutdown_nice(RB_HALT|RB_POWEROFF);
3881 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3884 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3888 #ifndef SC_DISABLE_KDBKEY
3895 if (enable_panic_key)
3896 panic("Forced by the panic key");
3900 this_scr = scp->index;
3901 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3902 sc->first_vty + i != this_scr;
3903 i = (i + 1)%sc->vtys) {
3904 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3905 if (tty_opened_ns(tp)) {
3906 sc_switch_scr(scp->sc, sc->first_vty + i);
3913 this_scr = scp->index;
3914 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3915 sc->first_vty + i != this_scr;
3916 i = (i + sc->vtys - 1)%sc->vtys) {
3917 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3918 if (tty_opened_ns(tp)) {
3919 sc_switch_scr(scp->sc, sc->first_vty + i);
3926 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3927 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3930 /* assert(c & FKEY) */
3931 if (!(sc->flags & SC_SCRN_BLANKED))
3935 /* goto next_code */
3937 /* regular keys (maybe MKEY is set) */
3938 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
3940 kdb_alt_break(c, &sc->sc_altbrk);
3942 if (!(sc->flags & SC_SCRN_BLANKED))
3951 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
3952 int nprot, vm_memattr_t *memattr)
3956 scp = sc_get_stat(tp);
3957 if (scp != scp->sc->cur_scp)
3959 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
3963 update_font(scr_stat *scp)
3965 #ifndef SC_NO_FONT_LOADING
3966 /* load appropriate font */
3967 if (!(scp->status & GRAPHICS_MODE)) {
3968 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3969 if (scp->font_size < 14) {
3970 if (scp->sc->fonts_loaded & FONT_8)
3971 sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3972 } else if (scp->font_size >= 16) {
3973 if (scp->sc->fonts_loaded & FONT_16)
3974 sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3976 if (scp->sc->fonts_loaded & FONT_14)
3977 sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3981 * This is an interim kludge to display correct font.
3982 * Always use the font page #0 on the video plane 2.
3983 * Somehow we cannot show the font in other font pages on
3984 * some video cards... XXX
3986 sc_show_font(scp, 0);
3990 #endif /* !SC_NO_FONT_LOADING */
3994 save_kbd_state(scr_stat *scp)
3999 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4000 if (error == ENOIOCTL)
4003 scp->status &= ~LOCK_MASK;
4004 scp->status |= state;
4010 update_kbd_state(scr_stat *scp, int new_bits, int mask)
4015 if (mask != LOCK_MASK) {
4016 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4017 if (error == ENOIOCTL)
4022 state |= new_bits & mask;
4024 state = new_bits & LOCK_MASK;
4026 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
4027 if (error == ENOIOCTL)
4033 update_kbd_leds(scr_stat *scp, int which)
4038 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
4039 if (error == ENOIOCTL)
4045 set_mode(scr_stat *scp)
4049 /* reject unsupported mode */
4050 if (vidd_get_info(scp->sc->adp, scp->mode, &info))
4053 /* if this vty is not currently showing, do nothing */
4054 if (scp != scp->sc->cur_scp)
4057 /* setup video hardware for the given mode */
4058 vidd_set_mode(scp->sc->adp, scp->mode);
4059 scp->rndr->init(scp);
4061 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
4062 (void *)scp->sc->adp->va_window, FALSE);
4067 sc_set_border(scp, scp->border);
4068 sc_set_cursor_image(scp);
4074 sc_set_border(scr_stat *scp, int color)
4076 SC_VIDEO_LOCK(scp->sc);
4077 (*scp->rndr->draw_border)(scp, color);
4078 SC_VIDEO_UNLOCK(scp->sc);
4081 #ifndef SC_NO_FONT_LOADING
4083 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
4084 int base, int count)
4089 sc->font_loading_in_progress = TRUE;
4090 vidd_load_font(sc->adp, page, size, width, buf, base, count);
4091 sc->font_loading_in_progress = FALSE;
4095 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
4096 int base, int count)
4101 sc->font_loading_in_progress = TRUE;
4102 vidd_save_font(sc->adp, page, size, width, buf, base, count);
4103 sc->font_loading_in_progress = FALSE;
4107 sc_show_font(scr_stat *scp, int page)
4109 vidd_show_font(scp->sc->adp, page);
4111 #endif /* !SC_NO_FONT_LOADING */
4114 sc_paste(scr_stat *scp, const u_char *p, int count)
4119 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
4120 if (!tty_opened_ns(tp))
4122 rmap = scp->sc->scr_rmap;
4123 for (; count > 0; --count)
4124 ttydisc_rint(tp, rmap[*p++], 0);
4125 ttydisc_rint_done(tp);
4129 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup)
4133 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
4134 if (!tty_opened_ns(tp))
4136 ttydisc_rint_simple(tp, p, count);
4138 /* XXX: we can't always call ttydisc_rint_done() here! */
4139 ttydisc_rint_done(tp);
4144 sc_bell(scr_stat *scp, int pitch, int duration)
4146 if (cold || kdb_active || shutdown_in_progress || !enable_bell)
4149 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
4152 if (scp->sc->flags & SC_VISUAL_BELL) {
4153 if (scp->sc->blink_in_progress)
4155 scp->sc->blink_in_progress = 3;
4156 if (scp != scp->sc->cur_scp)
4157 scp->sc->blink_in_progress += 2;
4158 blink_screen(scp->sc->cur_scp);
4159 } else if (duration != 0 && pitch != 0) {
4160 if (scp != scp->sc->cur_scp)
4162 sysbeep(1193182 / pitch, duration);
4169 if (sc_console == NULL)
4170 return (SC_KERNEL_CONS_ATTR); /* for very early, before pcpu */
4171 return (sc_kattrtab[PCPU_GET(cpuid) % nitems(sc_kattrtab)]);
4175 blink_screen(void *arg)
4177 scr_stat *scp = arg;
4180 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
4181 scp->sc->blink_in_progress = 0;
4183 tp = SC_DEV(scp->sc, scp->index);
4184 if (tty_opened_ns(tp))
4185 sctty_outwakeup(tp);
4186 if (scp->sc->delayed_next_scr)
4187 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
4190 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
4191 scp->sc->blink_in_progress & 1);
4192 scp->sc->blink_in_progress--;
4193 callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0,
4194 blink_screen, scp, C_PREL(0));
4199 * Until sc_attach_unit() gets called no dev structures will be available
4200 * to store the per-screen current status. This is the case when the
4201 * kernel is initially booting and needs access to its console. During
4202 * this early phase of booting the console's current status is kept in
4203 * one statically defined scr_stat structure, and any pointers to the
4204 * dev structures will be NULL.
4208 sc_get_stat(struct tty *tp)
4211 return (&main_console);
4212 return (SC_STAT(tp));
4216 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
4217 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
4222 sc_allocate_keyboard(sc_softc_t *sc, int unit)
4228 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
4230 k0 = kbd_get_keyboard(idx0);
4232 for (idx = kbd_find_keyboard2("*", -1, 0);
4234 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
4235 k = kbd_get_keyboard(idx);
4237 if (idx == idx0 || KBD_IS_BUSY(k))
4240 bzero(&ki, sizeof(ki));
4241 strcpy(ki.kb_name, k->kb_name);
4242 ki.kb_unit = k->kb_unit;
4244 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
4247 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);