2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992-1998 Søren Schmidt
7 * This code is derived from software contributed to The DragonFly Project
8 * by Sascha Wildner <saw@online.de>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_syscons.h"
38 #include "opt_splash.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
46 #include <sys/consio.h>
48 #include <sys/eventhandler.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
58 #include <sys/random.h>
59 #include <sys/reboot.h>
60 #include <sys/serial.h>
61 #include <sys/signalvar.h>
63 #include <sys/sysctl.h>
65 #include <sys/power.h>
67 #include <machine/clock.h>
68 #if defined(__arm__) || defined(__mips__) || \
69 defined(__powerpc__) || defined(__sparc64__)
70 #include <machine/sc_machdep.h>
72 #include <machine/pc/display.h>
74 #if defined( __i386__) || defined(__amd64__)
75 #include <machine/psl.h>
76 #include <machine/frame.h>
78 #include <machine/stdarg.h>
80 #if defined(__amd64__) || defined(__i386__)
81 #include <machine/vmparam.h>
87 #include <dev/kbd/kbdreg.h>
88 #include <dev/fb/fbreg.h>
89 #include <dev/fb/splashreg.h>
90 #include <dev/syscons/syscons.h>
95 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
96 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
98 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
100 /* NULL-safe version of "tty_opened()" */
101 #define tty_opened_ns(tp) ((tp) != NULL && tty_opened(tp))
103 static u_char sc_kattrtab[MAXCPU];
105 static int sc_console_unit = -1;
106 static int sc_saver_keyb_only = 1;
107 static scr_stat *sc_console;
108 static struct consdev *sc_consptr;
109 static void *sc_kts[MAXCPU];
110 static struct sc_term_sw *sc_ktsw;
111 static scr_stat main_console;
112 static struct tty *main_devs[MAXCONS];
114 static char init_done = COLD;
115 static int shutdown_in_progress = FALSE;
116 static int suspend_in_progress = FALSE;
117 static char sc_malloc = FALSE;
119 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
120 static int run_scrn_saver = FALSE; /* should run the saver? */
121 static int enable_bell = TRUE; /* enable beeper */
123 #ifndef SC_DISABLE_REBOOT
124 static int enable_reboot = TRUE; /* enable keyboard reboot */
127 #ifndef SC_DISABLE_KDBKEY
128 static int enable_kdbkey = TRUE; /* enable keyboard debug */
131 static long scrn_blank_time = 0; /* screen saver timeout value */
133 static int scrn_blanked; /* # of blanked screen */
134 static int sticky_splash = FALSE;
136 static void none_saver(sc_softc_t *sc, int blank) { }
137 static void (*current_saver)(sc_softc_t *, int) = none_saver;
140 #ifdef SC_NO_SUSPEND_VTYSWITCH
141 static int sc_no_suspend_vtswitch = 1;
143 static int sc_no_suspend_vtswitch = 0;
145 static int sc_susp_scr;
147 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
148 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
149 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
150 &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
151 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell,
153 #ifndef SC_DISABLE_REBOOT
154 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
155 0, "enable keyboard reboot");
157 #ifndef SC_DISABLE_KDBKEY
158 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
159 0, "enable keyboard debug");
161 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN,
162 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
163 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
167 tsw_ioctl_t *sc_user_ioctl;
169 static bios_values_t bios_value;
171 static int enable_panic_key;
172 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
173 0, "Enable panic via keypress specified in kbdmap(5)");
175 #define SC_CONSOLECTL 255
177 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
180 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
181 static int scvidprobe(int unit, int flags, int cons);
182 static int sckbdprobe(int unit, int flags, int cons);
183 static void scmeminit(void *arg);
184 static int scdevtounit(struct tty *tp);
185 static kbd_callback_func_t sckbdevent;
186 static void scinit(int unit, int flags);
187 static scr_stat *sc_get_stat(struct tty *tp);
188 static void scterm(int unit, int flags);
189 static void scshutdown(void *, int);
190 static void scsuspend(void *);
191 static void scresume(void *);
192 static u_int scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp);
193 static void sc_puts(scr_stat *scp, u_char *buf, int len);
195 #define SCGETC_NONBLOCK 2
196 static void sccnupdate(scr_stat *scp);
197 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
198 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
199 static timeout_t scrn_timer;
200 static int and_region(int *s1, int *e1, int s2, int e2);
201 static void scrn_update(scr_stat *scp, int show_cursor);
204 static int scsplash_callback(int event, void *arg);
205 static void scsplash_saver(sc_softc_t *sc, int show);
206 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
207 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
208 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
209 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
210 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
211 static int wait_scrn_saver_stop(sc_softc_t *sc);
212 #define scsplash_stick(stick) (sticky_splash = (stick))
213 #else /* !DEV_SPLASH */
214 #define scsplash_stick(stick)
215 #endif /* DEV_SPLASH */
217 static int do_switch_scr(sc_softc_t *sc, int s);
218 static int vt_proc_alive(scr_stat *scp);
219 static int signal_vt_rel(scr_stat *scp);
220 static int signal_vt_acq(scr_stat *scp);
221 static int finish_vt_rel(scr_stat *scp, int release, int *s);
222 static int finish_vt_acq(scr_stat *scp);
223 static void exchange_scr(sc_softc_t *sc);
224 static void update_cursor_image(scr_stat *scp);
225 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
226 static void update_font(scr_stat *);
227 static int save_kbd_state(scr_stat *scp);
228 static int update_kbd_state(scr_stat *scp, int state, int mask);
229 static int update_kbd_leds(scr_stat *scp, int which);
230 static int sc_kattr(void);
231 static timeout_t blink_screen;
232 static struct tty *sc_alloc_tty(int, int);
234 static cn_probe_t sc_cnprobe;
235 static cn_init_t sc_cninit;
236 static cn_term_t sc_cnterm;
237 static cn_getc_t sc_cngetc;
238 static cn_putc_t sc_cnputc;
239 static cn_grab_t sc_cngrab;
240 static cn_ungrab_t sc_cnungrab;
244 static tsw_open_t sctty_open;
245 static tsw_close_t sctty_close;
246 static tsw_outwakeup_t sctty_outwakeup;
247 static tsw_ioctl_t sctty_ioctl;
248 static tsw_mmap_t sctty_mmap;
250 static struct ttydevsw sc_ttydevsw = {
251 .tsw_open = sctty_open,
252 .tsw_close = sctty_close,
253 .tsw_outwakeup = sctty_outwakeup,
254 .tsw_ioctl = sctty_ioctl,
255 .tsw_mmap = sctty_mmap,
258 static d_ioctl_t consolectl_ioctl;
259 static d_close_t consolectl_close;
261 static struct cdevsw consolectl_devsw = {
262 .d_version = D_VERSION,
263 .d_flags = D_NEEDGIANT | D_TRACKCLOSE,
264 .d_ioctl = consolectl_ioctl,
265 .d_close = consolectl_close,
266 .d_name = "consolectl",
269 /* ec -- emergency console. */
271 static u_int ec_scroffset;
279 int attr, column, mysize, width, xsize, yborder, ysize;
281 if (c < 0 || c > 0xff || c == '\a')
283 if (sc_console == NULL) {
284 #if !defined(__amd64__) && !defined(__i386__)
288 * This is enough for ec_putc() to work very early on x86
289 * if the kernel starts in normal color text mode.
292 fb = KERNBASE + 0xb8000;
294 fb = PMAP_MAP_LOW + 0xb8000;
300 if (!ISTEXTSC(&main_console))
302 fb = main_console.sc->adp->va_window;
303 xsize = main_console.xsize;
304 ysize = main_console.ysize;
307 scrptr = (u_short *)(void *)fb + xsize * yborder;
308 mysize = xsize * (ysize - 2 * yborder);
311 column = ind % xsize;
312 width = (c == '\b' ? -1 : c == '\t' ? (column + 8) & ~7 :
313 c == '\r' ? -column : c == '\n' ? xsize - column : 1);
314 if (width == 0 || (width < 0 && ind < -width))
316 } while (atomic_cmpset_rel_int(&ec_scroffset, ind, ind + width) == 0);
317 if (c == '\b' || c == '\r')
320 ind += xsize; /* XXX clearing from new pos is not atomic */
323 if (c == '\t' || c == '\n')
326 scrptr[ind++ % mysize] = (attr << 8) | c;
327 while (--width != 0);
331 sc_probe_unit(int unit, int flags)
333 if (!vty_enabled(VTY_SC))
335 if (!scvidprobe(unit, flags, FALSE)) {
337 printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
341 /* syscons will be attached even when there is no keyboard */
342 sckbdprobe(unit, flags, FALSE);
347 /* probe video adapters, return TRUE if found */
349 scvidprobe(int unit, int flags, int cons)
352 * Access the video adapter driver through the back door!
353 * Video adapter drivers need to be configured before syscons.
354 * However, when syscons is being probed as the low-level console,
355 * they have not been initialized yet. We force them to initialize
356 * themselves here. XXX
358 vid_configure(cons ? VIO_PROBE_ONLY : 0);
360 return (vid_find_adapter("*", unit) >= 0);
363 /* probe the keyboard, return TRUE if found */
365 sckbdprobe(int unit, int flags, int cons)
367 /* access the keyboard driver through the backdoor! */
368 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
370 return (kbd_find_keyboard("*", unit) >= 0);
374 *adapter_name(video_adapter_t *adp)
380 { KD_MONO, { "MDA", "MDA" } },
381 { KD_HERCULES, { "Hercules", "Hercules" } },
382 { KD_CGA, { "CGA", "CGA" } },
383 { KD_EGA, { "EGA", "EGA (mono)" } },
384 { KD_VGA, { "VGA", "VGA (mono)" } },
385 { KD_TGA, { "TGA", "TGA" } },
386 { -1, { "Unknown", "Unknown" } },
390 for (i = 0; names[i].type != -1; ++i)
391 if (names[i].type == adp->va_type)
393 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
397 sctty_outwakeup(struct tty *tp)
401 scr_stat *scp = sc_get_stat(tp);
403 if (scp->status & SLKED ||
404 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
408 len = ttydisc_getc(tp, buf, sizeof buf);
411 SC_VIDEO_LOCK(scp->sc);
412 sc_puts(scp, buf, len);
413 SC_VIDEO_UNLOCK(scp->sc);
418 sc_alloc_tty(int index, int devnum)
420 struct sc_ttysoftc *stc;
423 /* Allocate TTY object and softc to store unit number. */
424 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
425 stc->st_index = index;
427 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
429 /* Create device node. */
430 tty_makedev(tp, NULL, "v%r", devnum);
437 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
447 (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
448 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
449 vidd_get_info(sc->adp, vmode, &info) != 0 ||
450 !sc_support_pixel_mode(&info))
454 * If the mode is unset or unsupported, search for an available
455 * 800x600 graphics mode with the highest color depth.
458 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
459 if (vidd_get_info(sc->adp, i, &info) == 0 &&
460 info.vi_width == 800 && info.vi_height == 600 &&
461 sc_support_pixel_mode(&info) &&
462 info.vi_depth > depth) {
464 depth = info.vi_depth;
468 vidd_get_info(sc->adp, vmode, &info);
471 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
472 fontsize = info.vi_cheight;
474 fontsize = scp->font_size;
478 else if (fontsize >= 16)
482 #ifndef SC_NO_FONT_LOADING
485 if ((sc->fonts_loaded & FONT_8) == 0)
490 if ((sc->fonts_loaded & FONT_14) == 0)
495 if ((sc->fonts_loaded & FONT_16) == 0)
504 if ((sc->flags & SC_SPLASH_SCRN) != 0)
505 splash_term(sc->adp);
507 #ifndef SC_NO_HISTORY
508 if (scp->history != NULL) {
509 sc_vtb_append(&scp->vtb, 0, scp->history,
510 scp->ypos * scp->xsize + scp->xpos);
511 scp->history_pos = sc_vtb_tail(scp->history);
514 vidd_set_mode(sc->adp, vmode);
515 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
516 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
517 scp->xpixel = info.vi_width;
518 scp->ypixel = info.vi_height;
519 scp->xsize = scp->xpixel / 8;
520 scp->ysize = scp->ypixel / fontsize;
522 scp->ypos = scp->ysize - 1;
523 scp->xoff = scp->yoff = 0;
525 scp->font_size = fontsize;
527 scp->start = scp->xsize * scp->ysize - 1;
529 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
530 scp->mode = sc->initial_mode = vmode;
532 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
533 (void *)sc->adp->va_window, FALSE);
535 sc_alloc_scr_buffer(scp, FALSE, FALSE);
536 sc_init_emulator(scp, NULL);
537 #ifndef SC_NO_CUTPASTE
538 sc_alloc_cut_buffer(scp, FALSE);
540 #ifndef SC_NO_HISTORY
541 sc_alloc_history_buffer(scp, 0, 0, FALSE);
543 sc_set_border(scp, scp->border);
544 sc_set_cursor_image(scp);
545 scp->status &= ~UNKNOWN_MODE;
547 if ((sc->flags & SC_SPLASH_SCRN) != 0)
548 splash_init(sc->adp, scsplash_callback, sc);
554 sc_attach_unit(int unit, int flags)
562 if (!vty_enabled(VTY_SC))
565 flags &= ~SC_KERNEL_CONSOLE;
567 if (sc_console_unit == unit) {
569 * If this unit is being used as the system console, we need to
570 * adjust some variables and buffers before and after scinit().
572 /* assert(sc_console != NULL) */
573 flags |= SC_KERNEL_CONSOLE;
578 if (sc_console->tsw->te_size > 0) {
579 sc_ktsw = sc_console->tsw;
580 /* assert(sc_console->ts != NULL); */
581 oldts = sc_console->ts;
582 for (i = 0; i <= mp_maxid; i++) {
583 ts = malloc(sc_console->tsw->te_size, M_DEVBUF,
585 (*sc_console->tsw->te_init)(sc_console, &ts, SC_TE_COLD_INIT);
587 (*sc_console->tsw->te_default_attr)(sc_console, sc_kattrtab[i],
588 SC_KERNEL_CONS_REV_ATTR);
591 sc_console->ts = oldts;
592 (*sc_console->tsw->te_default_attr)(sc_console, SC_NORM_ATTR,
599 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
601 callout_init(&sc->ctimeout, 0);
602 callout_init(&sc->cblink, 0);
603 scp = sc_get_stat(sc->dev[0]);
604 if (sc_console == NULL) /* sc_console_unit < 0 */
608 if ((sc->config & SC_VESAMODE) != 0)
609 sc_set_vesa_mode(scp, sc, unit);
610 #endif /* SC_PIXEL_MODE */
612 /* initialize cursor */
614 update_cursor_image(scp);
616 /* get screen update going */
619 /* set up the keyboard */
620 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
621 update_kbd_state(scp, scp->status, LOCK_MASK);
623 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
624 SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
626 printf("%s%d:", SC_DRIVER_NAME, unit);
627 if (sc->adapter >= 0)
628 printf(" fb%d", sc->adapter);
629 if (sc->keyboard >= 0)
630 printf(", kbd%d", sc->keyboard);
632 printf(", terminal emulator: %s (%s)",
633 scp->tsw->te_name, scp->tsw->te_desc);
637 /* Register suspend/resume/shutdown callbacks for the kernel console. */
638 if (sc_console_unit == unit) {
639 EVENTHANDLER_REGISTER(power_suspend_early, scsuspend, NULL,
640 EVENTHANDLER_PRI_ANY);
641 EVENTHANDLER_REGISTER(power_resume, scresume, NULL,
642 EVENTHANDLER_PRI_ANY);
643 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL,
644 SHUTDOWN_PRI_DEFAULT);
647 for (vc = 0; vc < sc->vtys; vc++) {
648 if (sc->dev[vc] == NULL) {
649 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
650 if (vc == 0 && sc->dev == main_devs)
651 SC_STAT(sc->dev[0]) = &main_console;
654 * The first vty already has struct tty and scr_stat initialized
655 * in scinit(). The other vtys will have these structs when
660 dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600,
662 dev->si_drv1 = sc->dev[0];
670 if (!vty_enabled(VTY_SC))
677 * As soon as malloc() becomes functional, we had better allocate
678 * various buffers for the kernel console.
681 if (sc_console_unit < 0) /* sc_console == NULL */
684 /* copy the temporary buffer to the final buffer */
685 sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
687 #ifndef SC_NO_CUTPASTE
688 sc_alloc_cut_buffer(sc_console, FALSE);
691 #ifndef SC_NO_HISTORY
692 /* initialize history buffer & pointers */
693 sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
698 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
701 scdevtounit(struct tty *tp)
703 int vty = SC_VTY(tp);
705 if (vty == SC_CONSOLECTL)
706 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
707 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
714 sctty_open(struct tty *tp)
716 int unit = scdevtounit(tp);
723 DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
724 devtoname(tp->t_dev), unit, SC_VTY(tp)));
726 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
730 if (!tty_opened(tp)) {
731 /* Use the current setting of the <-- key as default VERASE. */
732 /* If the Delete key is preferable, an stty is necessary */
734 if (sc->kbd != NULL) {
735 key.keynum = KEYCODE_BS;
736 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
737 tp->t_termios.c_cc[VERASE] = key.key.map[0];
742 scp = sc_get_stat(tp);
744 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
746 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
748 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
749 tp->t_winsize.ws_col = scp->xsize;
750 tp->t_winsize.ws_row = scp->ysize;
757 sctty_close(struct tty *tp)
762 if (SC_VTY(tp) != SC_CONSOLECTL) {
763 scp = sc_get_stat(tp);
764 /* were we in the middle of the VT switching process? */
765 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
767 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
768 cnavailable(sc_consptr, TRUE);
769 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */
770 DPRINTF(5, ("reset WAIT_REL, "));
771 if (finish_vt_acq(scp) == 0) /* force acknowledge */
772 DPRINTF(5, ("reset WAIT_ACQ, "));
774 if (scp == &main_console) {
777 scp->smode.mode = VT_AUTO;
780 sc_vtb_destroy(&scp->vtb);
782 sc_vtb_destroy(&scp->scr);
784 sc_free_history_buffer(scp, scp->ysize);
791 scp->smode.mode = VT_AUTO;
793 scp->kbd_mode = K_XLATE;
794 if (scp == scp->sc->cur_scp)
795 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
796 DPRINTF(5, ("done.\n"));
800 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
802 scread(struct cdev *dev, struct uio *uio, int flag)
804 if (!sc_saver_keyb_only)
805 sc_touch_scrn_saver();
806 return ttyread(dev, uio, flag);
811 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
819 sc = (sc_softc_t *)arg;
820 /* assert(thiskbd == sc->kbd) */
827 case KBDIO_UNLOADING:
830 kbd_release(thiskbd, (void *)&sc->keyboard);
838 * Loop while there is still input to get from the keyboard.
839 * I don't think this is nessesary, and it doesn't fix
840 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
842 while ((c = scgetc(sc, SCGETC_NONBLOCK, NULL)) != NOKEY) {
844 cur_tty = SC_DEV(sc, sc->cur_scp->index);
845 if (!tty_opened_ns(cur_tty))
848 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
851 switch (KEYFLAGS(c)) {
852 case 0x0000: /* normal key */
853 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
855 case FKEY: /* function key, return string */
856 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
858 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
861 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
863 ttydisc_rint_simple(cur_tty, cp, len);
865 case MKEY: /* meta is active, prepend ESC */
866 ttydisc_rint(cur_tty, 0x1b, 0);
867 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
869 case BKEY: /* backtab fixed sequence (esc [ Z) */
870 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
874 ttydisc_rint_done(cur_tty);
877 sc->cur_scp->status |= MOUSE_HIDDEN;
885 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
889 struct cursor_attr *cap;
893 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
894 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
898 /* If there is a user_ioctl function call that first */
900 error = (*sc_user_ioctl)(tp, cmd, data, td);
901 if (error != ENOIOCTL)
905 error = sc_vid_ioctl(tp, cmd, data, td);
906 if (error != ENOIOCTL)
909 #ifndef SC_NO_HISTORY
910 error = sc_hist_ioctl(tp, cmd, data, td);
911 if (error != ENOIOCTL)
915 #ifndef SC_NO_SYSMOUSE
916 error = sc_mouse_ioctl(tp, cmd, data, td);
917 if (error != ENOIOCTL)
921 scp = sc_get_stat(tp);
922 /* assert(scp != NULL) */
923 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
927 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
928 if (error != ENOIOCTL)
932 switch (cmd) { /* process console hardware related ioctl's */
934 case GIO_ATTR: /* get current attributes */
935 /* this ioctl is not processed here, but in the terminal emulator */
938 case GIO_COLOR: /* is this a color console ? */
939 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
942 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
943 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
946 scrn_blank_time = *(int *)data;
947 run_scrn_saver = (scrn_blank_time != 0);
951 case CONS_CURSORTYPE: /* set cursor type (old interface + HIDDEN) */
953 *(int *)data &= CONS_CURSOR_ATTRS;
954 sc_change_cursor_shape(scp, *(int *)data, -1, -1);
958 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */
959 switch (((int *)data)[0] & (CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR)) {
961 cap = &sc->curs_attr;
963 case CONS_LOCAL_CURSOR:
964 cap = &scp->base_curs_attr;
966 case CONS_DEFAULT_CURSOR:
967 cap = &sc->dflt_curs_attr;
969 case CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR:
970 cap = &scp->dflt_curs_attr;
973 if (((int *)data)[0] & CONS_CHARCURSOR_COLORS) {
974 ((int *)data)[1] = cap->bg[0];
975 ((int *)data)[2] = cap->bg[1];
976 } else if (((int *)data)[0] & CONS_MOUSECURSOR_COLORS) {
977 ((int *)data)[1] = cap->mouse_ba;
978 ((int *)data)[2] = cap->mouse_ia;
980 ((int *)data)[1] = cap->base;
981 ((int *)data)[2] = cap->height;
983 ((int *)data)[0] = cap->flags;
986 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */
988 sc_change_cursor_shape(scp, ((int *)data)[0],
989 ((int *)data)[1], ((int *)data)[2]);
993 case CONS_BELLTYPE: /* set bell type sound/visual */
994 if ((*(int *)data) & CONS_VISUAL_BELL)
995 sc->flags |= SC_VISUAL_BELL;
997 sc->flags &= ~SC_VISUAL_BELL;
998 if ((*(int *)data) & CONS_QUIET_BELL)
999 sc->flags |= SC_QUIET_BELL;
1001 sc->flags &= ~SC_QUIET_BELL;
1004 case CONS_GETINFO: /* get current (virtual) console info */
1006 vid_info_t *ptr = (vid_info_t*)data;
1007 if (ptr->size == sizeof(struct vid_info)) {
1008 ptr->m_num = sc->cur_scp->index;
1009 ptr->font_size = scp->font_size;
1010 ptr->mv_col = scp->xpos;
1011 ptr->mv_row = scp->ypos;
1012 ptr->mv_csz = scp->xsize;
1013 ptr->mv_rsz = scp->ysize;
1014 ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1016 * The following fields are filled by the terminal emulator. XXX
1023 ptr->mv_grfc.fore = 0; /* not supported */
1024 ptr->mv_grfc.back = 0; /* not supported */
1025 ptr->mv_ovscan = scp->border;
1026 if (scp == sc->cur_scp)
1027 save_kbd_state(scp);
1028 ptr->mk_keylock = scp->status & LOCK_MASK;
1034 case CONS_GETVERS: /* get version number */
1035 *(int*)data = 0x200; /* version 2.0 */
1038 case CONS_IDLE: /* see if the screen has been idle */
1040 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
1041 * the user process may have been writing something on the
1042 * screen and syscons is not aware of it. Declare the screen
1043 * is NOT idle if it is in one of these modes. But there is
1044 * an exception to it; if a screen saver is running in the
1045 * graphics mode in the current screen, we should say that the
1046 * screen has been idle.
1048 *(int *)data = (sc->flags & SC_SCRN_IDLE)
1049 && (!ISGRAPHSC(sc->cur_scp)
1050 || (sc->cur_scp->status & SAVER_RUNNING));
1053 case CONS_SAVERMODE: /* set saver mode */
1054 switch(*(int *)data) {
1056 case CONS_USR_SAVER:
1057 /* if a LKM screen saver is running, stop it first. */
1058 scsplash_stick(FALSE);
1059 saver_mode = *(int *)data;
1062 if ((error = wait_scrn_saver_stop(NULL))) {
1067 run_scrn_saver = TRUE;
1068 if (saver_mode == CONS_USR_SAVER)
1069 scp->status |= SAVER_RUNNING;
1071 scp->status &= ~SAVER_RUNNING;
1072 scsplash_stick(TRUE);
1075 case CONS_LKM_SAVER:
1077 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
1078 scp->status &= ~SAVER_RUNNING;
1079 saver_mode = *(int *)data;
1087 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
1089 * Note that this ioctl does not guarantee the screen saver
1090 * actually starts or stops. It merely attempts to do so...
1093 run_scrn_saver = (*(int *)data != 0);
1095 sc->scrn_time_stamp -= scrn_blank_time;
1099 case CONS_SCRSHOT: /* get a screen shot */
1101 int retval, hist_rsz;
1102 size_t lsize, csize;
1103 vm_offset_t frbp, hstp;
1105 scrshot_t *ptr = (scrshot_t *)data;
1106 void *outp = ptr->buf;
1108 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
1111 if (ISGRAPHSC(scp)) {
1115 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1116 if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
1117 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1122 lsize = scp->xsize * sizeof(u_int16_t);
1123 csize = ptr->xsize * sizeof(u_int16_t);
1124 /* Pointer to the last line of framebuffer */
1125 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
1127 /* Pointer to the last line of target buffer */
1128 outp = (char *)outp + ptr->ysize * csize;
1129 /* Pointer to the last line of history buffer */
1130 if (scp->history != NULL)
1131 hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
1132 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
1137 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1138 if (lnum < scp->ysize) {
1142 if (hstp < scp->history->vtb_buffer)
1143 hstp += scp->history->vtb_rows * lsize;
1148 outp = (char *)outp - csize;
1149 retval = copyout((void *)frbp, outp, csize);
1157 case VT_SETMODE: /* set screen switcher mode */
1159 struct vt_mode *mode;
1162 mode = (struct vt_mode *)data;
1163 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1164 if (scp->smode.mode == VT_PROCESS) {
1165 p1 = pfind(scp->pid);
1166 if (scp->proc == p1 && scp->proc != td->td_proc) {
1169 DPRINTF(5, ("error EPERM\n"));
1176 if (mode->mode == VT_AUTO) {
1177 scp->smode.mode = VT_AUTO;
1180 DPRINTF(5, ("VT_AUTO, "));
1181 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1182 cnavailable(sc_consptr, TRUE);
1183 /* were we in the middle of the vty switching process? */
1184 if (finish_vt_rel(scp, TRUE, &s) == 0)
1185 DPRINTF(5, ("reset WAIT_REL, "));
1186 if (finish_vt_acq(scp) == 0)
1187 DPRINTF(5, ("reset WAIT_ACQ, "));
1189 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1190 || !ISSIGVALID(mode->frsig)) {
1192 DPRINTF(5, ("error EINVAL\n"));
1195 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1196 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1197 scp->proc = td->td_proc;
1198 scp->pid = scp->proc->p_pid;
1199 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1200 cnavailable(sc_consptr, FALSE);
1207 case VT_GETMODE: /* get screen switcher mode */
1208 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1211 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1212 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1214 ival = IOCPARM_IVAL(data);
1215 data = (caddr_t)&ival;
1218 case VT_RELDISP: /* screen switcher ioctl */
1221 * This must be the current vty which is in the VT_PROCESS
1224 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1228 /* ...and this process is controlling it. */
1229 if (scp->proc != td->td_proc) {
1234 switch(*(int *)data) {
1235 case VT_FALSE: /* user refuses to release screen, abort */
1236 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1237 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1239 case VT_TRUE: /* user has released screen, go on */
1240 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1241 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1243 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1244 if ((error = finish_vt_acq(scp)) == 0)
1245 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1253 case VT_OPENQRY: /* return free virtual console */
1254 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1256 if (!tty_opened_ns(tp)) {
1257 *(int *)data = i + 1;
1263 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1264 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1266 ival = IOCPARM_IVAL(data);
1267 data = (caddr_t)&ival;
1270 case VT_ACTIVATE: /* switch to screen *data */
1271 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1273 error = sc_clean_up(sc->cur_scp);
1277 error = sc_switch_scr(sc, i);
1280 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1281 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1283 ival = IOCPARM_IVAL(data);
1284 data = (caddr_t)&ival;
1287 case VT_WAITACTIVE: /* wait for switch to occur */
1288 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1289 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1291 if (i == sc->cur_scp->index)
1293 error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1296 case VT_GETACTIVE: /* get active vty # */
1297 *(int *)data = sc->cur_scp->index + 1;
1300 case VT_GETINDEX: /* get this vty # */
1301 *(int *)data = scp->index + 1;
1304 case VT_LOCKSWITCH: /* prevent vty switching */
1305 if ((*(int *)data) & 0x01)
1306 sc->flags |= SC_SCRN_VTYLOCK;
1308 sc->flags &= ~SC_SCRN_VTYLOCK;
1311 case KDENABIO: /* allow io operations */
1312 error = priv_check(td, PRIV_IO);
1315 error = securelevel_gt(td->td_ucred, 0);
1319 td->td_frame->tf_eflags |= PSL_IOPL;
1320 #elif defined(__amd64__)
1321 td->td_frame->tf_rflags |= PSL_IOPL;
1325 case KDDISABIO: /* disallow io operations (default) */
1327 td->td_frame->tf_eflags &= ~PSL_IOPL;
1328 #elif defined(__amd64__)
1329 td->td_frame->tf_rflags &= ~PSL_IOPL;
1333 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1334 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1336 ival = IOCPARM_IVAL(data);
1337 data = (caddr_t)&ival;
1340 case KDSKBSTATE: /* set keyboard state (locks) */
1341 if (*(int *)data & ~LOCK_MASK)
1343 scp->status &= ~LOCK_MASK;
1344 scp->status |= *(int *)data;
1345 if (scp == sc->cur_scp)
1346 update_kbd_state(scp, scp->status, LOCK_MASK);
1349 case KDGKBSTATE: /* get keyboard state (locks) */
1350 if (scp == sc->cur_scp)
1351 save_kbd_state(scp);
1352 *(int *)data = scp->status & LOCK_MASK;
1355 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1356 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1357 error = kbdd_ioctl(sc->kbd, cmd, data);
1358 if (error == ENOIOCTL)
1362 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1363 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1365 ival = IOCPARM_IVAL(data);
1366 data = (caddr_t)&ival;
1369 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1370 if (*(int *)data & ~0x7f)
1372 error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1373 if (error == ENOIOCTL)
1377 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1378 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1380 ival = IOCPARM_IVAL(data);
1381 data = (caddr_t)&ival;
1384 case KDSKBMODE: /* set keyboard mode */
1385 switch (*(int *)data) {
1386 case K_XLATE: /* switch to XLT ascii mode */
1387 case K_RAW: /* switch to RAW scancode mode */
1388 case K_CODE: /* switch to CODE mode */
1389 scp->kbd_mode = *(int *)data;
1390 if (scp == sc->cur_scp)
1391 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1398 case KDGKBMODE: /* get keyboard mode */
1399 *(int *)data = scp->kbd_mode;
1403 error = kbdd_ioctl(sc->kbd, cmd, data);
1404 if (error == ENOIOCTL)
1408 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1409 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1411 ival = IOCPARM_IVAL(data);
1412 data = (caddr_t)&ival;
1415 case KDMKTONE: /* sound the bell */
1417 sc_bell(scp, (*(int*)data)&0xffff,
1418 (((*(int*)data)>>16)&0xffff)*hz/1000);
1420 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1423 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1424 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1426 ival = IOCPARM_IVAL(data);
1427 data = (caddr_t)&ival;
1430 case KIOCSOUND: /* make tone (*data) hz */
1431 if (scp == sc->cur_scp) {
1433 return sc_tone(*(int *)data);
1439 case KDGKBTYPE: /* get keyboard type */
1440 error = kbdd_ioctl(sc->kbd, cmd, data);
1441 if (error == ENOIOCTL) {
1442 /* always return something? XXX */
1447 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1448 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1450 ival = IOCPARM_IVAL(data);
1451 data = (caddr_t)&ival;
1454 case KDSETLED: /* set keyboard LED status */
1455 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1457 scp->status &= ~LED_MASK;
1458 scp->status |= *(int *)data;
1459 if (scp == sc->cur_scp)
1460 update_kbd_leds(scp, scp->status);
1463 case KDGETLED: /* get keyboard LED status */
1464 if (scp == sc->cur_scp)
1465 save_kbd_state(scp);
1466 *(int *)data = scp->status & LED_MASK;
1469 case KBADDKBD: /* add/remove keyboard to/from mux */
1471 error = kbdd_ioctl(sc->kbd, cmd, data);
1472 if (error == ENOIOCTL)
1476 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1477 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1479 ival = IOCPARM_IVAL(data);
1480 data = (caddr_t)&ival;
1483 case CONS_SETKBD: /* set the new keyboard */
1488 newkbd = kbd_get_keyboard(*(int *)data);
1489 if (newkbd == NULL) {
1494 if (sc->kbd != newkbd) {
1495 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1496 (void *)&sc->keyboard, sckbdevent, sc);
1497 /* i == newkbd->kb_index */
1499 if (sc->kbd != NULL) {
1500 save_kbd_state(sc->cur_scp);
1501 kbd_release(sc->kbd, (void *)&sc->keyboard);
1503 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1505 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1506 (caddr_t)&sc->cur_scp->kbd_mode);
1507 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1510 error = EPERM; /* XXX */
1517 case CONS_RELKBD: /* release the current keyboard */
1520 if (sc->kbd != NULL) {
1521 save_kbd_state(sc->cur_scp);
1522 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1531 case CONS_GETTERM: /* get the current terminal emulator info */
1535 if (((term_info_t *)data)->ti_index == 0) {
1538 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1541 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1542 sizeof(((term_info_t *)data)->ti_name));
1543 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1544 sizeof(((term_info_t *)data)->ti_desc));
1545 ((term_info_t *)data)->ti_flags = 0;
1548 ((term_info_t *)data)->ti_name[0] = '\0';
1549 ((term_info_t *)data)->ti_desc[0] = '\0';
1550 ((term_info_t *)data)->ti_flags = 0;
1555 case CONS_SETTERM: /* set the current terminal emulator */
1557 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1558 /* FIXME: what if scp == sc_console! XXX */
1562 case GIO_SCRNMAP: /* get output translation table */
1563 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1566 case PIO_SCRNMAP: /* set output translation table */
1567 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1568 for (i=0; i<sizeof(sc->scr_map); i++) {
1569 sc->scr_rmap[sc->scr_map[i]] = i;
1573 case GIO_KEYMAP: /* get keyboard translation table */
1574 case PIO_KEYMAP: /* set keyboard translation table */
1575 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
1576 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1577 case GIO_DEADKEYMAP: /* get accent key translation table */
1578 case PIO_DEADKEYMAP: /* set accent key translation table */
1579 case GETFKEY: /* get function key string */
1580 case SETFKEY: /* set function key string */
1581 error = kbdd_ioctl(sc->kbd, cmd, data);
1582 if (error == ENOIOCTL)
1586 #ifndef SC_NO_FONT_LOADING
1588 case PIO_FONT8x8: /* set 8x8 dot font */
1589 if (!ISFONTAVAIL(sc->adp->va_flags))
1591 bcopy(data, sc->font_8, 8*256);
1592 sc->fonts_loaded |= FONT_8;
1595 * Always use the font page #0. XXX
1596 * Don't load if the current font size is not 8x8.
1598 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1599 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1602 case GIO_FONT8x8: /* get 8x8 dot font */
1603 if (!ISFONTAVAIL(sc->adp->va_flags))
1605 if (sc->fonts_loaded & FONT_8) {
1606 bcopy(sc->font_8, data, 8*256);
1612 case PIO_FONT8x14: /* set 8x14 dot font */
1613 if (!ISFONTAVAIL(sc->adp->va_flags))
1615 bcopy(data, sc->font_14, 14*256);
1616 sc->fonts_loaded |= FONT_14;
1619 * Always use the font page #0. XXX
1620 * Don't load if the current font size is not 8x14.
1622 if (ISTEXTSC(sc->cur_scp)
1623 && (sc->cur_scp->font_size >= 14)
1624 && (sc->cur_scp->font_size < 16))
1625 sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1628 case GIO_FONT8x14: /* get 8x14 dot font */
1629 if (!ISFONTAVAIL(sc->adp->va_flags))
1631 if (sc->fonts_loaded & FONT_14) {
1632 bcopy(sc->font_14, data, 14*256);
1638 case PIO_FONT8x16: /* set 8x16 dot font */
1639 if (!ISFONTAVAIL(sc->adp->va_flags))
1641 bcopy(data, sc->font_16, 16*256);
1642 sc->fonts_loaded |= FONT_16;
1645 * Always use the font page #0. XXX
1646 * Don't load if the current font size is not 8x16.
1648 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1649 sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1652 case GIO_FONT8x16: /* get 8x16 dot font */
1653 if (!ISFONTAVAIL(sc->adp->va_flags))
1655 if (sc->fonts_loaded & FONT_16) {
1656 bcopy(sc->font_16, data, 16*256);
1662 #endif /* SC_NO_FONT_LOADING */
1672 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1676 return sctty_ioctl(dev->si_drv1, cmd, data, td);
1680 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td)
1682 #ifndef SC_NO_SYSMOUSE
1684 memset(&info, 0, sizeof(info));
1685 info.operation = MOUSE_ACTION;
1688 * Make sure all buttons are released when moused and other
1689 * console daemons exit, so that no buttons are left pressed.
1691 (void) sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td);
1697 sc_cnprobe(struct consdev *cp)
1702 if (!vty_enabled(VTY_SC)) {
1703 cp->cn_pri = CN_DEAD;
1707 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1709 /* a video card is always required */
1710 if (!scvidprobe(unit, flags, TRUE))
1711 cp->cn_pri = CN_DEAD;
1713 /* syscons will become console even when there is no keyboard */
1714 sckbdprobe(unit, flags, TRUE);
1716 if (cp->cn_pri == CN_DEAD)
1719 /* initialize required fields */
1720 strcpy(cp->cn_name, "ttyv0");
1724 sc_cninit(struct consdev *cp)
1729 sc_get_cons_priority(&unit, &flags);
1730 scinit(unit, flags | SC_KERNEL_CONSOLE);
1731 sc_console_unit = unit;
1732 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1737 sc_cnterm(struct consdev *cp)
1742 /* we are not the kernel console any more, release everything */
1744 if (sc_console_unit < 0)
1745 return; /* shouldn't happen */
1748 sc_clear_screen(sc_console);
1749 sccnupdate(sc_console);
1752 if (sc_ktsw != NULL) {
1753 for (i = 0; i <= mp_maxid; i++) {
1756 (*sc_ktsw->te_term)(sc_console, &ts);
1761 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1762 sc_console_unit = -1;
1766 static void sccnclose(sc_softc_t *sc, struct sc_cnstate *sp);
1767 static int sc_cngetc_locked(struct sc_cnstate *sp);
1768 static void sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp);
1769 static void sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp);
1770 static void sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags);
1771 static void sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp);
1772 static void sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp);
1775 sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp)
1778 * Locking method: hope for the best.
1779 * The keyboard is supposed to be Giant locked. We can't handle that
1780 * in general. The kdb_active case here is not safe, and we will
1781 * proceed without the lock in all cases.
1783 sp->kbd_locked = !kdb_active && mtx_trylock(&Giant);
1787 sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp)
1791 sp->kbd_locked = FALSE;
1795 sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp)
1801 * - if kdb_active and video_mtx is not owned by anyone, then lock
1802 * by kdb remaining active
1803 * - if !kdb_active, try to acquire video_mtx without blocking or
1804 * recursing; if we get it then it works normally.
1805 * Note that video_mtx is especially unusable if we already own it,
1806 * since then it is protecting something and syscons is not reentrant
1807 * enough to ignore the protection even in the kdb_active case.
1810 sp->kdb_locked = sc->video_mtx.mtx_lock == MTX_UNOWNED || panicstr;
1811 sp->mtx_locked = FALSE;
1813 sp->kdb_locked = FALSE;
1814 for (retries = 0; retries < 1000; retries++) {
1815 sp->mtx_locked = mtx_trylock_spin_flags(&sc->video_mtx,
1816 MTX_QUIET) != 0 || panicstr;
1825 sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp)
1828 mtx_unlock_spin(&sc->video_mtx);
1829 sp->mtx_locked = sp->kdb_locked = FALSE;
1833 sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags)
1837 /* assert(sc_console_unit >= 0) */
1839 sp->kbd_opened = FALSE;
1840 sp->scr_opened = FALSE;
1841 sp->kbd_locked = FALSE;
1843 /* Opening the keyboard is optional. */
1844 if (!(flags & 1) || sc->kbd == NULL)
1847 sccnkbdlock(sc, sp);
1850 * Make sure the keyboard is accessible even when the kbd device
1851 * driver is disabled.
1853 kbdd_enable(sc->kbd);
1855 /* Switch the keyboard to console mode (K_XLATE, polled) on all scp's. */
1857 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&kbd_mode);
1858 sc->kbd_open_level++;
1859 kbdd_poll(sc->kbd, TRUE);
1861 sp->kbd_opened = TRUE;
1864 /* The screen is opened iff locking it succeeds. */
1865 sccnscrlock(sc, sp);
1866 if (!sp->kdb_locked && !sp->mtx_locked)
1868 sp->scr_opened = TRUE;
1870 /* The screen switch is optional. */
1874 /* try to switch to the kernel console screen */
1876 sc->cur_scp->index != sc_console->index &&
1877 sc->cur_scp->smode.mode == VT_AUTO &&
1878 sc_console->smode.mode == VT_AUTO)
1879 sc_switch_scr(sc, sc_console->index);
1883 sccnclose(sc_softc_t *sc, struct sc_cnstate *sp)
1885 sp->scr_opened = FALSE;
1886 sccnscrunlock(sc, sp);
1888 if (!sp->kbd_opened)
1891 /* Restore keyboard mode (for the current, possibly-changed scp). */
1892 kbdd_poll(sc->kbd, FALSE);
1893 if (--sc->kbd_open_level == 0)
1894 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&sc->cur_scp->kbd_mode);
1896 kbdd_disable(sc->kbd);
1897 sp->kbd_opened = FALSE;
1898 sccnkbdunlock(sc, sp);
1902 * Grabbing switches the screen and keyboard focus to sc_console and the
1903 * keyboard mode to (K_XLATE, polled). Only switching to polled mode is
1904 * essential (for preventing the interrupt handler from eating input
1905 * between polls). Focus is part of the UI, and the other switches are
1906 * work just was well when they are done on every entry and exit.
1908 * Screen switches while grabbed are supported, and to maintain focus for
1909 * this ungrabbing and closing only restore the polling state and then
1910 * the keyboard mode if on the original screen.
1914 sc_cngrab(struct consdev *cp)
1919 sc = sc_console->sc;
1920 lev = atomic_fetchadd_int(&sc->grab_level, 1);
1921 if (lev >= 0 && lev < 2) {
1922 sccnopen(sc, &sc->grab_state[lev], 1 | 2);
1923 sccnscrunlock(sc, &sc->grab_state[lev]);
1924 sccnkbdunlock(sc, &sc->grab_state[lev]);
1929 sc_cnungrab(struct consdev *cp)
1934 sc = sc_console->sc;
1935 lev = atomic_load_acq_int(&sc->grab_level) - 1;
1936 if (lev >= 0 && lev < 2) {
1937 sccnkbdlock(sc, &sc->grab_state[lev]);
1938 sccnscrlock(sc, &sc->grab_state[lev]);
1939 sccnclose(sc, &sc->grab_state[lev]);
1941 atomic_add_int(&sc->grab_level, -1);
1944 static char sc_cnputc_log[0x1000];
1945 static u_int sc_cnputc_loghead;
1946 static u_int sc_cnputc_logtail;
1949 sc_cnputc(struct consdev *cd, int c)
1951 struct sc_cnstate st;
1953 scr_stat *scp = sc_console;
1955 struct sc_term_sw *oldtsw;
1956 #ifndef SC_NO_HISTORY
1960 #endif /* !SC_NO_HISTORY */
1964 /* assert(sc_console != NULL) */
1966 sccnopen(scp->sc, &st, 0);
1971 * In the unlocked case, the logging is intentionally only
1972 * perfectly atomic for the indexes.
1974 head = atomic_fetchadd_int(&sc_cnputc_loghead, 1);
1975 sc_cnputc_log[head % sizeof(sc_cnputc_log)] = c;
1978 * If we couldn't open, do special reentrant output and return to defer
1981 if (!st.scr_opened) {
1986 #ifndef SC_NO_HISTORY
1987 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1988 scp->status &= ~SLKED;
1989 update_kbd_state(scp, scp->status, SLKED);
1990 if (scp->status & BUFFER_SAVED) {
1991 if (!sc_hist_restore(scp))
1992 sc_remove_cutmarking(scp);
1993 scp->status &= ~BUFFER_SAVED;
1994 scp->status |= CURSOR_ENABLED;
1995 sc_draw_cursor_image(scp);
1999 * XXX: Now that TTY's have their own locks, we cannot process
2000 * any data after disabling scroll lock. cnputs already holds a
2003 tp = SC_DEV(scp->sc, scp->index);
2004 /* XXX "tp" can be NULL */
2007 sctty_outwakeup(tp);
2011 #endif /* !SC_NO_HISTORY */
2013 /* Play any output still in the log (our char may already be done). */
2014 while (sc_cnputc_logtail != atomic_load_acq_int(&sc_cnputc_loghead)) {
2015 buf[0] = sc_cnputc_log[sc_cnputc_logtail++ % sizeof(sc_cnputc_log)];
2016 if (atomic_load_acq_int(&sc_cnputc_loghead) - sc_cnputc_logtail >=
2017 sizeof(sc_cnputc_log))
2019 /* Console output has a per-CPU "input" state. Switch for it. */
2022 ts = sc_kts[PCPU_GET(cpuid)];
2026 (*scp->tsw->te_sync)(scp);
2028 sc_puts(scp, buf, 1);
2031 (*scp->tsw->te_sync)(scp);
2034 s = spltty(); /* block sckbdevent and scrn_timer */
2037 sccnclose(scp->sc, &st);
2041 sc_cngetc(struct consdev *cd)
2043 struct sc_cnstate st;
2046 /* assert(sc_console != NULL) */
2047 sccnopen(sc_console->sc, &st, 1);
2048 s = spltty(); /* block sckbdevent and scrn_timer while we poll */
2049 if (!st.kbd_opened) {
2051 sccnclose(sc_console->sc, &st);
2052 return -1; /* means no keyboard since we fudged the locking */
2054 c = sc_cngetc_locked(&st);
2056 sccnclose(sc_console->sc, &st);
2061 sc_cngetc_locked(struct sc_cnstate *sp)
2063 static struct fkeytab fkey;
2070 * Stop the screen saver and update the screen if necessary.
2071 * What if we have been running in the screen saver code... XXX
2074 sc_touch_scrn_saver();
2075 scp = sc_console->sc->cur_scp; /* XXX */
2079 if (fkeycp < fkey.len)
2080 return fkey.str[fkeycp++];
2082 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK, sp);
2084 switch (KEYFLAGS(c)) {
2085 case 0: /* normal char */
2087 case FKEY: /* function key */
2088 p = (*scp->tsw->te_fkeystr)(scp, c);
2090 fkey.len = strlen(p);
2091 bcopy(p, fkey.str, fkey.len);
2095 p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
2097 if ((p != NULL) && (fkey.len > 0)) {
2098 bcopy(p, fkey.str, fkey.len);
2112 sccnupdate(scr_stat *scp)
2114 /* this is a cut-down version of scrn_timer()... */
2116 if (suspend_in_progress || scp->sc->font_loading_in_progress)
2119 if (kdb_active || panicstr || shutdown_in_progress) {
2120 sc_touch_scrn_saver();
2121 } else if (scp != scp->sc->cur_scp) {
2125 if (!run_scrn_saver)
2126 scp->sc->flags &= ~SC_SCRN_IDLE;
2128 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
2129 if (scp->sc->flags & SC_SCRN_BLANKED)
2130 stop_scrn_saver(scp->sc, current_saver);
2133 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
2134 || scp->sc->switch_in_progress)
2137 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
2138 * when write_in_progress is non-zero. XXX
2141 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
2142 scrn_update(scp, TRUE);
2146 scrn_timer(void *arg)
2148 static time_t kbd_time_stamp = 0;
2153 again = (arg != NULL);
2155 sc = (sc_softc_t *)arg;
2156 else if (sc_console != NULL)
2157 sc = sc_console->sc;
2161 /* find the vty to update */
2164 /* don't do anything when we are performing some I/O operations */
2165 if (suspend_in_progress || sc->font_loading_in_progress)
2168 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
2169 /* try to allocate a keyboard automatically */
2170 if (kbd_time_stamp != time_uptime) {
2171 kbd_time_stamp = time_uptime;
2172 sc->keyboard = sc_allocate_keyboard(sc, -1);
2173 if (sc->keyboard >= 0) {
2174 sc->kbd = kbd_get_keyboard(sc->keyboard);
2175 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
2176 (caddr_t)&sc->cur_scp->kbd_mode);
2177 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
2183 /* should we stop the screen saver? */
2184 if (kdb_active || panicstr || shutdown_in_progress)
2185 sc_touch_scrn_saver();
2186 if (run_scrn_saver) {
2187 if (time_uptime > sc->scrn_time_stamp + scrn_blank_time)
2188 sc->flags |= SC_SCRN_IDLE;
2190 sc->flags &= ~SC_SCRN_IDLE;
2192 sc->scrn_time_stamp = time_uptime;
2193 sc->flags &= ~SC_SCRN_IDLE;
2194 if (scrn_blank_time > 0)
2195 run_scrn_saver = TRUE;
2198 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
2199 if (sc->flags & SC_SCRN_BLANKED)
2200 stop_scrn_saver(sc, current_saver);
2203 /* should we just return ? */
2204 if (sc->blink_in_progress || sc->switch_in_progress
2205 || sc->write_in_progress)
2208 /* Update the screen */
2209 scp = sc->cur_scp; /* cur_scp may have changed... */
2210 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2211 scrn_update(scp, TRUE);
2214 /* should we activate the screen saver? */
2215 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
2216 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
2217 (*current_saver)(sc, TRUE);
2223 * Use reduced "refresh" rate if we are in graphics and that is not a
2224 * graphical screen saver. In such case we just have nothing to do.
2226 if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2230 callout_reset_sbt(&sc->ctimeout, SBT_1S / rate, 0,
2231 scrn_timer, sc, C_PREL(1));
2236 and_region(int *s1, int *e1, int s2, int e2)
2238 if (*e1 < s2 || e2 < *s1)
2240 *s1 = imax(*s1, s2);
2241 *e1 = imin(*e1, e2);
2246 scrn_update(scr_stat *scp, int show_cursor)
2253 /* assert(scp == scp->sc->cur_scp) */
2255 SC_VIDEO_LOCK(scp->sc);
2257 #ifndef SC_NO_CUTPASTE
2258 /* remove the previous mouse pointer image if necessary */
2259 if (scp->status & MOUSE_VISIBLE) {
2261 e = scp->mouse_pos + scp->xsize + 1;
2262 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
2263 || and_region(&s, &e, scp->start, scp->end)
2264 || ((scp->status & CURSOR_ENABLED) &&
2265 (scp->cursor_pos != scp->cursor_oldpos) &&
2266 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
2267 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
2268 sc_remove_mouse_image(scp);
2269 if (scp->end >= scp->xsize*scp->ysize)
2270 scp->end = scp->xsize*scp->ysize - 1;
2273 #endif /* !SC_NO_CUTPASTE */
2277 if (scp->end >= scp->xsize*scp->ysize) {
2278 printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
2279 scp->end = scp->xsize*scp->ysize - 1;
2281 if (scp->start < 0) {
2282 printf("scrn_update(): scp->start %d < 0\n", scp->start);
2287 /* update screen image */
2288 if (scp->start <= scp->end) {
2289 if (scp->mouse_cut_end >= 0) {
2290 /* there is a marked region for cut & paste */
2291 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
2292 start = scp->mouse_cut_start;
2293 end = scp->mouse_cut_end;
2295 start = scp->mouse_cut_end;
2296 end = scp->mouse_cut_start - 1;
2300 /* does the cut-mark region overlap with the update region? */
2301 if (and_region(&s, &e, scp->start, scp->end)) {
2302 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2305 if (and_region(&s, &e, scp->start, scp->end))
2306 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2308 e = scp->xsize*scp->ysize - 1;
2309 if (and_region(&s, &e, scp->start, scp->end))
2310 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2312 (*scp->rndr->draw)(scp, scp->start,
2313 scp->end - scp->start + 1, FALSE);
2316 (*scp->rndr->draw)(scp, scp->start,
2317 scp->end - scp->start + 1, FALSE);
2321 /* we are not to show the cursor and the mouse pointer... */
2324 scp->start = scp->xsize*scp->ysize - 1;
2325 SC_VIDEO_UNLOCK(scp->sc);
2329 /* update cursor image */
2330 if (scp->status & CURSOR_ENABLED) {
2333 /* did cursor move since last time ? */
2334 if (scp->cursor_pos != scp->cursor_oldpos) {
2335 /* do we need to remove old cursor image ? */
2336 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2337 sc_remove_cursor_image(scp);
2338 sc_draw_cursor_image(scp);
2340 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
2341 /* cursor didn't move, but has been overwritten */
2342 sc_draw_cursor_image(scp);
2343 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
2344 /* if it's a blinking cursor, update it */
2345 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2346 sc_inside_cutmark(scp,
2351 #ifndef SC_NO_CUTPASTE
2352 /* update "pseudo" mouse pointer image */
2353 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2354 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2355 scp->status &= ~MOUSE_MOVED;
2356 sc_draw_mouse_image(scp);
2359 #endif /* SC_NO_CUTPASTE */
2362 scp->start = scp->xsize*scp->ysize - 1;
2364 SC_VIDEO_UNLOCK(scp->sc);
2369 scsplash_callback(int event, void *arg)
2374 sc = (sc_softc_t *)arg;
2378 if (add_scrn_saver(scsplash_saver) == 0) {
2379 sc->flags &= ~SC_SAVER_FAILED;
2380 run_scrn_saver = TRUE;
2381 if (cold && !(boothowto & RB_VERBOSE)) {
2382 scsplash_stick(TRUE);
2383 (*current_saver)(sc, TRUE);
2389 if (current_saver == scsplash_saver) {
2390 scsplash_stick(FALSE);
2391 error = remove_scrn_saver(scsplash_saver);
2403 scsplash_saver(sc_softc_t *sc, int show)
2405 static int busy = FALSE;
2414 if (!(sc->flags & SC_SAVER_FAILED)) {
2415 if (!(sc->flags & SC_SCRN_BLANKED))
2416 set_scrn_saver_mode(scp, -1, NULL, 0);
2417 switch (splash(sc->adp, TRUE)) {
2418 case 0: /* succeeded */
2420 case EAGAIN: /* try later */
2421 restore_scrn_saver_mode(scp, FALSE);
2422 sc_touch_scrn_saver(); /* XXX */
2425 sc->flags |= SC_SAVER_FAILED;
2426 scsplash_stick(FALSE);
2427 restore_scrn_saver_mode(scp, TRUE);
2428 printf("scsplash_saver(): failed to put up the image\n");
2432 } else if (!sticky_splash) {
2433 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2434 restore_scrn_saver_mode(scp, TRUE);
2440 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2445 if (current_saver != none_saver) {
2446 error = remove_scrn_saver(current_saver);
2451 if (current_saver != none_saver)
2454 run_scrn_saver = FALSE;
2455 saver_mode = CONS_LKM_SAVER;
2456 current_saver = this_saver;
2461 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2463 if (current_saver != this_saver)
2468 * In order to prevent `current_saver' from being called by
2469 * the timeout routine `scrn_timer()' while we manipulate
2470 * the saver list, we shall set `current_saver' to `none_saver'
2471 * before stopping the current saver, rather than blocking by `splXX()'.
2473 current_saver = none_saver;
2475 stop_scrn_saver(this_saver);
2478 /* unblank all blanked screens */
2479 wait_scrn_saver_stop(NULL);
2483 current_saver = none_saver;
2488 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2492 /* assert(scp == scp->sc->cur_scp) */
2494 if (!ISGRAPHSC(scp))
2495 sc_remove_cursor_image(scp);
2496 scp->splash_save_mode = scp->mode;
2497 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2498 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2499 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2500 scp->sc->flags |= SC_SCRN_BLANKED;
2506 if (set_mode(scp) == 0) {
2507 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2508 scp->status |= GRAPHICS_MODE;
2509 #ifndef SC_NO_PALETTE_LOADING
2511 vidd_load_palette(scp->sc->adp, pal);
2513 sc_set_border(scp, border);
2517 scp->mode = scp->splash_save_mode;
2518 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2519 scp->status |= scp->splash_save_status;
2526 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2532 /* assert(scp == scp->sc->cur_scp) */
2535 status = scp->status;
2536 scp->mode = scp->splash_save_mode;
2537 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2538 scp->status |= scp->splash_save_status;
2539 scp->sc->flags &= ~SC_SCRN_BLANKED;
2541 if (!ISGRAPHSC(scp))
2542 sc_draw_cursor_image(scp);
2547 if (set_mode(scp) == 0) {
2548 #ifndef SC_NO_PALETTE_LOADING
2549 #ifdef SC_PIXEL_MODE
2550 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2551 vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2554 vidd_load_palette(scp->sc->adp, scp->sc->palette);
2561 scp->status = status;
2568 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2570 (*saver)(sc, FALSE);
2571 run_scrn_saver = FALSE;
2572 /* the screen saver may have chosen not to stop after all... */
2573 if (sc->flags & SC_SCRN_BLANKED)
2576 mark_all(sc->cur_scp);
2577 if (sc->delayed_next_scr)
2578 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2580 wakeup(&scrn_blanked);
2584 wait_scrn_saver_stop(sc_softc_t *sc)
2588 while (scrn_blanked > 0) {
2589 run_scrn_saver = FALSE;
2590 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2594 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2595 if ((error != 0) && (error != ERESTART))
2598 run_scrn_saver = FALSE;
2601 #endif /* DEV_SPLASH */
2604 sc_touch_scrn_saver(void)
2606 scsplash_stick(FALSE);
2607 run_scrn_saver = FALSE;
2611 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2618 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2620 if (sc->cur_scp == NULL)
2623 /* prevent switch if previously requested */
2624 if (sc->flags & SC_SCRN_VTYLOCK) {
2625 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2626 sc->cur_scp->bell_duration);
2630 /* delay switch if the screen is blanked or being updated */
2631 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2632 || sc->blink_in_progress) {
2633 sc->delayed_next_scr = next_scr + 1;
2634 sc_touch_scrn_saver();
2635 DPRINTF(5, ("switch delayed\n"));
2638 sc->delayed_next_scr = 0;
2641 cur_scp = sc->cur_scp;
2643 /* we are in the middle of the vty switching process... */
2644 if (sc->switch_in_progress
2645 && (cur_scp->smode.mode == VT_PROCESS)
2647 p = pfind(cur_scp->pid);
2648 if (cur_scp->proc != p) {
2652 * The controlling process has died!!. Do some clean up.
2653 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2654 * are not reset here yet; they will be cleared later.
2656 DPRINTF(5, ("cur_scp controlling process %d died, ",
2658 if (cur_scp->status & SWITCH_WAIT_REL) {
2660 * Force the previous switch to finish, but return now
2663 DPRINTF(5, ("reset WAIT_REL, "));
2664 finish_vt_rel(cur_scp, TRUE, &s);
2666 DPRINTF(5, ("finishing previous switch\n"));
2668 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2669 /* let's assume screen switch has been completed. */
2670 DPRINTF(5, ("reset WAIT_ACQ, "));
2671 finish_vt_acq(cur_scp);
2674 * We are in between screen release and acquisition, and
2675 * reached here via scgetc() or scrn_timer() which has
2676 * interrupted exchange_scr(). Don't do anything stupid.
2678 DPRINTF(5, ("waiting nothing, "));
2684 * The controlling process is alive, but not responding...
2685 * It is either buggy or it may be just taking time.
2686 * The following code is a gross kludge to cope with this
2687 * problem for which there is no clean solution. XXX
2689 if (cur_scp->status & SWITCH_WAIT_REL) {
2690 switch (sc->switch_in_progress++) {
2694 DPRINTF(5, ("sending relsig again, "));
2695 signal_vt_rel(cur_scp);
2702 * Act as if the controlling program returned
2705 DPRINTF(5, ("force reset WAIT_REL, "));
2706 finish_vt_rel(cur_scp, FALSE, &s);
2708 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2711 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2712 switch (sc->switch_in_progress++) {
2716 DPRINTF(5, ("sending acqsig again, "));
2717 signal_vt_acq(cur_scp);
2723 /* clear the flag and finish the previous switch */
2724 DPRINTF(5, ("force reset WAIT_ACQ, "));
2725 finish_vt_acq(cur_scp);
2733 * Return error if an invalid argument is given, or vty switch
2734 * is still in progress.
2736 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2737 || sc->switch_in_progress) {
2739 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2740 DPRINTF(5, ("error 1\n"));
2745 * Don't allow switching away from the graphics mode vty
2746 * if the switch mode is VT_AUTO, unless the next vty is the same
2747 * as the current or the current vty has been closed (but showing).
2749 tp = SC_DEV(sc, cur_scp->index);
2750 if ((cur_scp->index != next_scr)
2751 && tty_opened_ns(tp)
2752 && (cur_scp->smode.mode == VT_AUTO)
2753 && ISGRAPHSC(cur_scp)) {
2755 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2756 DPRINTF(5, ("error, graphics mode\n"));
2761 * Is the wanted vty open? Don't allow switching to a closed vty.
2762 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2763 * Note that we always allow the user to switch to the kernel
2764 * console even if it is closed.
2766 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2767 tp = SC_DEV(sc, next_scr);
2768 if (!tty_opened_ns(tp)) {
2770 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2771 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2774 if (kdb_active && SC_STAT(tp)->smode.mode == VT_PROCESS) {
2776 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2781 /* this is the start of vty switching process... */
2782 ++sc->switch_in_progress;
2783 sc->old_scp = cur_scp;
2784 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2785 if (sc->new_scp == sc->old_scp) {
2786 sc->switch_in_progress = 0;
2788 * XXX wakeup() locks the scheduler lock which will hang if
2789 * the lock is in an in-between state, e.g., when we stop at
2790 * a breakpoint at fork_exit. It has always been wrong to call
2791 * wakeup() when the debugger is active. In RELENG_4, wakeup()
2792 * is supposed to be locked by splhigh(), but the debugger may
2793 * be invoked at splhigh().
2796 wakeup(VTY_WCHAN(sc,next_scr));
2798 DPRINTF(5, ("switch done (new == old)\n"));
2802 /* has controlling process died? */
2803 vt_proc_alive(sc->old_scp);
2804 vt_proc_alive(sc->new_scp);
2806 /* wait for the controlling process to release the screen, if necessary */
2807 if (signal_vt_rel(sc->old_scp)) {
2812 /* go set up the new vty screen */
2817 /* wake up processes waiting for this vty */
2819 wakeup(VTY_WCHAN(sc,next_scr));
2821 /* wait for the controlling process to acknowledge, if necessary */
2822 if (signal_vt_acq(sc->cur_scp)) {
2827 sc->switch_in_progress = 0;
2828 if (sc->unit == sc_console_unit)
2829 cnavailable(sc_consptr, TRUE);
2831 DPRINTF(5, ("switch done\n"));
2837 do_switch_scr(sc_softc_t *sc, int s)
2839 vt_proc_alive(sc->new_scp);
2844 /* sc->cur_scp == sc->new_scp */
2845 wakeup(VTY_WCHAN(sc,sc->cur_scp->index));
2847 /* wait for the controlling process to acknowledge, if necessary */
2848 if (!signal_vt_acq(sc->cur_scp)) {
2849 sc->switch_in_progress = 0;
2850 if (sc->unit == sc_console_unit)
2851 cnavailable(sc_consptr, TRUE);
2858 vt_proc_alive(scr_stat *scp)
2863 if ((p = pfind(scp->pid)) != NULL)
2868 scp->smode.mode = VT_AUTO;
2869 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2875 signal_vt_rel(scr_stat *scp)
2877 if (scp->smode.mode != VT_PROCESS)
2879 scp->status |= SWITCH_WAIT_REL;
2880 PROC_LOCK(scp->proc);
2881 kern_psignal(scp->proc, scp->smode.relsig);
2882 PROC_UNLOCK(scp->proc);
2883 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2888 signal_vt_acq(scr_stat *scp)
2890 if (scp->smode.mode != VT_PROCESS)
2892 if (scp->sc->unit == sc_console_unit)
2893 cnavailable(sc_consptr, FALSE);
2894 scp->status |= SWITCH_WAIT_ACQ;
2895 PROC_LOCK(scp->proc);
2896 kern_psignal(scp->proc, scp->smode.acqsig);
2897 PROC_UNLOCK(scp->proc);
2898 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2903 finish_vt_rel(scr_stat *scp, int release, int *s)
2905 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2906 scp->status &= ~SWITCH_WAIT_REL;
2908 *s = do_switch_scr(scp->sc, *s);
2910 scp->sc->switch_in_progress = 0;
2917 finish_vt_acq(scr_stat *scp)
2919 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2920 scp->status &= ~SWITCH_WAIT_ACQ;
2921 scp->sc->switch_in_progress = 0;
2928 exchange_scr(sc_softc_t *sc)
2932 /* save the current state of video and keyboard */
2933 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2934 if (!ISGRAPHSC(sc->old_scp))
2935 sc_remove_cursor_image(sc->old_scp);
2936 if (sc->old_scp->kbd_mode == K_XLATE)
2937 save_kbd_state(sc->old_scp);
2939 /* set up the video for the new screen */
2940 scp = sc->cur_scp = sc->new_scp;
2941 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2945 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2946 (void *)sc->adp->va_window, FALSE);
2948 scp->status |= MOUSE_HIDDEN;
2949 sc_move_cursor(scp, scp->xpos, scp->ypos);
2950 if (!ISGRAPHSC(scp))
2951 sc_set_cursor_image(scp);
2952 #ifndef SC_NO_PALETTE_LOADING
2953 if (ISGRAPHSC(sc->old_scp)) {
2954 #ifdef SC_PIXEL_MODE
2955 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2956 vidd_load_palette(sc->adp, sc->palette2);
2959 vidd_load_palette(sc->adp, sc->palette);
2962 sc_set_border(scp, scp->border);
2964 /* set up the keyboard for the new screen */
2965 if (sc->kbd_open_level == 0 && sc->old_scp->kbd_mode != scp->kbd_mode)
2966 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2967 update_kbd_state(scp, scp->status, LOCK_MASK);
2973 sc_puts(scr_stat *scp, u_char *buf, int len)
2976 /* make screensaver happy */
2977 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2978 run_scrn_saver = FALSE;
2982 (*scp->tsw->te_puts)(scp, buf, len);
2983 if (scp->sc->delayed_next_scr)
2984 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2988 sc_draw_cursor_image(scr_stat *scp)
2990 /* assert(scp == scp->sc->cur_scp); */
2991 SC_VIDEO_LOCK(scp->sc);
2992 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2993 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2994 sc_inside_cutmark(scp, scp->cursor_pos));
2995 scp->cursor_oldpos = scp->cursor_pos;
2996 SC_VIDEO_UNLOCK(scp->sc);
3000 sc_remove_cursor_image(scr_stat *scp)
3002 /* assert(scp == scp->sc->cur_scp); */
3003 SC_VIDEO_LOCK(scp->sc);
3004 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
3005 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
3006 sc_inside_cutmark(scp, scp->cursor_oldpos));
3007 SC_VIDEO_UNLOCK(scp->sc);
3011 update_cursor_image(scr_stat *scp)
3013 /* assert(scp == scp->sc->cur_scp); */
3014 sc_remove_cursor_image(scp);
3015 sc_set_cursor_image(scp);
3016 sc_draw_cursor_image(scp);
3020 sc_set_cursor_image(scr_stat *scp)
3022 scp->curs_attr = scp->base_curs_attr;
3023 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
3024 /* hidden cursor is internally represented as zero-height underline */
3025 scp->curs_attr.flags = CONS_CHAR_CURSOR;
3026 scp->curs_attr.base = scp->curs_attr.height = 0;
3027 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
3028 scp->curs_attr.base = imin(scp->base_curs_attr.base,
3029 scp->font_size - 1);
3030 scp->curs_attr.height = imin(scp->base_curs_attr.height,
3031 scp->font_size - scp->curs_attr.base);
3032 } else { /* block cursor */
3033 scp->curs_attr.base = 0;
3034 scp->curs_attr.height = scp->font_size;
3037 /* assert(scp == scp->sc->cur_scp); */
3038 SC_VIDEO_LOCK(scp->sc);
3039 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
3040 scp->curs_attr.flags & CONS_BLINK_CURSOR);
3041 SC_VIDEO_UNLOCK(scp->sc);
3045 sc_adjust_ca(struct cursor_attr *cap, int flags, int base, int height)
3047 if (flags & CONS_CHARCURSOR_COLORS) {
3048 cap->bg[0] = base & 0xff;
3049 cap->bg[1] = height & 0xff;
3050 } else if (flags & CONS_MOUSECURSOR_COLORS) {
3051 cap->mouse_ba = base & 0xff;
3052 cap->mouse_ia = height & 0xff;
3057 cap->height = height;
3058 if (!(flags & CONS_SHAPEONLY_CURSOR))
3059 cap->flags = flags & CONS_CURSOR_ATTRS;
3064 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
3066 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
3067 sc_remove_cursor_image(scp);
3069 if (flags & CONS_RESET_CURSOR)
3070 scp->base_curs_attr = scp->dflt_curs_attr;
3071 else if (flags & CONS_DEFAULT_CURSOR) {
3072 sc_adjust_ca(&scp->dflt_curs_attr, flags, base, height);
3073 scp->base_curs_attr = scp->dflt_curs_attr;
3075 sc_adjust_ca(&scp->base_curs_attr, flags, base, height);
3077 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
3078 sc_set_cursor_image(scp);
3079 sc_draw_cursor_image(scp);
3084 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
3092 flags = CONS_SHAPEONLY_CURSOR;
3095 if (flags & CONS_LOCAL_CURSOR) {
3096 /* local (per vty) change */
3097 change_cursor_shape(scp, flags, base, height);
3104 if (flags & CONS_RESET_CURSOR)
3105 sc->curs_attr = sc->dflt_curs_attr;
3106 else if (flags & CONS_DEFAULT_CURSOR) {
3107 sc_adjust_ca(&sc->dflt_curs_attr, flags, base, height);
3108 sc->curs_attr = sc->dflt_curs_attr;
3110 sc_adjust_ca(&sc->curs_attr, flags, base, height);
3112 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
3113 if ((tp = SC_DEV(sc, i)) == NULL)
3115 if ((scp = sc_get_stat(tp)) == NULL)
3117 scp->dflt_curs_attr = sc->curs_attr;
3118 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
3124 scinit(int unit, int flags)
3128 * When syscons is being initialized as the kernel console, malloc()
3129 * is not yet functional, because various kernel structures has not been
3130 * fully initialized yet. Therefore, we need to declare the following
3131 * static buffers for the console. This is less than ideal,
3132 * but is necessry evil for the time being. XXX
3134 static u_short sc_buffer[ROW*COL]; /* XXX */
3135 #ifndef SC_NO_FONT_LOADING
3136 static u_char font_8[256*8];
3137 static u_char font_14[256*14];
3138 static u_char font_16[256*16];
3143 video_adapter_t *adp;
3148 /* one time initialization */
3149 if (init_done == COLD) {
3150 sc_get_bios_values(&bios_value);
3151 for (i = 0; i < nitems(sc_kattrtab); i++) {
3152 #if SC_KERNEL_CONS_ATTR == FG_WHITE
3153 sc_kattrtab[i] = 8 + (i + FG_WHITE) % 8U;
3155 sc_kattrtab[i] = SC_KERNEL_CONS_ATTR;
3162 * Allocate resources. Even if we are being called for the second
3163 * time, we must allocate them again, because they might have
3166 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3167 if ((sc->flags & SC_INIT_DONE) == 0)
3168 SC_VIDEO_LOCKINIT(sc);
3171 if (sc->adapter >= 0) {
3172 vid_release(sc->adp, (void *)&sc->adapter);
3176 if (sc->keyboard >= 0) {
3177 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
3178 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
3179 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
3180 if (sc->kbd != NULL) {
3181 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
3182 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
3186 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
3187 sc->adp = vid_get_adapter(sc->adapter);
3188 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
3190 sc->keyboard = sc_allocate_keyboard(sc, unit);
3191 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
3193 sc->kbd = kbd_get_keyboard(sc->keyboard);
3194 if (sc->kbd != NULL) {
3195 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
3196 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
3199 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
3201 sc->initial_mode = sc->adp->va_initial_mode;
3203 #ifndef SC_NO_FONT_LOADING
3204 if (flags & SC_KERNEL_CONSOLE) {
3205 sc->font_8 = font_8;
3206 sc->font_14 = font_14;
3207 sc->font_16 = font_16;
3208 } else if (sc->font_8 == NULL) {
3209 /* assert(sc_malloc) */
3210 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
3211 sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
3212 sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
3216 /* extract the hardware cursor location and hide the cursor for now */
3217 vidd_read_hw_cursor(sc->adp, &col, &row);
3218 vidd_set_hw_cursor(sc->adp, -1, -1);
3220 /* set up the first console */
3221 sc->first_vty = unit*MAXCONS;
3222 sc->vtys = MAXCONS; /* XXX: should be configurable */
3223 if (flags & SC_KERNEL_CONSOLE) {
3225 * Set up devs structure but don't use it yet, calling make_dev()
3226 * might panic kernel. Wait for sc_attach_unit() to actually
3227 * create the devices.
3229 sc->dev = main_devs;
3230 scp = &main_console;
3231 init_scp(sc, sc->first_vty, scp);
3232 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
3233 (void *)sc_buffer, FALSE);
3234 if (sc_init_emulator(scp, SC_DFLT_TERM))
3235 sc_init_emulator(scp, "*");
3236 (*scp->tsw->te_default_attr)(scp, SC_KERNEL_CONS_ATTR,
3237 SC_KERNEL_CONS_REV_ATTR);
3239 /* assert(sc_malloc) */
3240 sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF,
3242 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
3243 scp = alloc_scp(sc, sc->first_vty);
3244 SC_STAT(sc->dev[0]) = scp;
3249 /* copy screen to temporary buffer */
3250 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3251 (void *)scp->sc->adp->va_window, FALSE);
3253 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
3256 /* Sync h/w cursor position to s/w (sc and teken). */
3257 if (col >= scp->xsize)
3259 if (row >= scp->ysize)
3260 row = scp->ysize - 1;
3263 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
3264 (*scp->tsw->te_sync)(scp);
3266 sc->dflt_curs_attr.base = 0;
3267 sc->dflt_curs_attr.height = howmany(scp->font_size, 8);
3268 sc->dflt_curs_attr.flags = 0;
3269 sc->dflt_curs_attr.bg[0] = FG_RED;
3270 sc->dflt_curs_attr.bg[1] = FG_LIGHTGREY;
3271 sc->dflt_curs_attr.bg[2] = FG_BLUE;
3272 sc->dflt_curs_attr.mouse_ba = FG_WHITE;
3273 sc->dflt_curs_attr.mouse_ia = FG_RED;
3274 sc->curs_attr = sc->dflt_curs_attr;
3275 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3276 scp->curs_attr = scp->base_curs_attr;
3278 #ifndef SC_NO_SYSMOUSE
3279 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
3281 if (!ISGRAPHSC(scp)) {
3282 sc_set_cursor_image(scp);
3283 sc_draw_cursor_image(scp);
3286 /* save font and palette */
3287 #ifndef SC_NO_FONT_LOADING
3288 sc->fonts_loaded = 0;
3289 if (ISFONTAVAIL(sc->adp->va_flags)) {
3291 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
3292 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
3293 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
3294 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
3295 if (scp->font_size < 14) {
3296 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3297 } else if (scp->font_size >= 16) {
3298 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3300 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3302 #else /* !SC_DFLT_FONT */
3303 if (scp->font_size < 14) {
3304 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3305 sc->fonts_loaded = FONT_8;
3306 } else if (scp->font_size >= 16) {
3307 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3308 sc->fonts_loaded = FONT_16;
3310 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3311 sc->fonts_loaded = FONT_14;
3313 #endif /* SC_DFLT_FONT */
3314 /* FONT KLUDGE: always use the font page #0. XXX */
3315 sc_show_font(scp, 0);
3317 #endif /* !SC_NO_FONT_LOADING */
3319 #ifndef SC_NO_PALETTE_LOADING
3320 vidd_save_palette(sc->adp, sc->palette);
3321 #ifdef SC_PIXEL_MODE
3322 for (i = 0; i < sizeof(sc->palette2); i++)
3323 sc->palette2[i] = i / 3;
3328 if (!(sc->flags & SC_SPLASH_SCRN)) {
3329 /* we are ready to put up the splash image! */
3330 splash_init(sc->adp, scsplash_callback, sc);
3331 sc->flags |= SC_SPLASH_SCRN;
3336 /* the rest is not necessary, if we have done it once */
3337 if (sc->flags & SC_INIT_DONE)
3340 /* initialize mapscrn arrays to a one to one map */
3341 for (i = 0; i < sizeof(sc->scr_map); i++)
3342 sc->scr_map[i] = sc->scr_rmap[i] = i;
3344 sc->flags |= SC_INIT_DONE;
3348 scterm(int unit, int flags)
3353 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3355 return; /* shouldn't happen */
3358 /* this console is no longer available for the splash screen */
3359 if (sc->flags & SC_SPLASH_SCRN) {
3360 splash_term(sc->adp);
3361 sc->flags &= ~SC_SPLASH_SCRN;
3366 /* move the hardware cursor to the upper-left corner */
3367 vidd_set_hw_cursor(sc->adp, 0, 0);
3370 /* release the keyboard and the video card */
3371 if (sc->keyboard >= 0)
3372 kbd_release(sc->kbd, &sc->keyboard);
3373 if (sc->adapter >= 0)
3374 vid_release(sc->adp, &sc->adapter);
3376 /* stop the terminal emulator, if any */
3377 scp = sc_get_stat(sc->dev[0]);
3379 (*scp->tsw->te_term)(scp, &scp->ts);
3380 mtx_destroy(&sc->video_mtx);
3382 /* clear the structure */
3383 if (!(flags & SC_KERNEL_CONSOLE)) {
3384 free(scp->ts, M_DEVBUF);
3385 /* XXX: We need delete_dev() for this */
3386 free(sc->dev, M_DEVBUF);
3388 /* XXX: We need a ttyunregister for this */
3389 free(sc->tty, M_DEVBUF);
3391 #ifndef SC_NO_FONT_LOADING
3392 free(sc->font_8, M_DEVBUF);
3393 free(sc->font_14, M_DEVBUF);
3394 free(sc->font_16, M_DEVBUF);
3396 /* XXX vtb, history */
3398 bzero(sc, sizeof(*sc));
3404 scshutdown(__unused void *arg, __unused int howto)
3407 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3408 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3409 KASSERT(sc_console->sc->cur_scp != NULL,
3410 ("sc_console->sc->cur_scp != NULL"));
3412 sc_touch_scrn_saver();
3414 sc_console->sc->cur_scp->index != sc_console->index &&
3415 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3416 sc_console->smode.mode == VT_AUTO)
3417 sc_switch_scr(sc_console->sc, sc_console->index);
3418 shutdown_in_progress = TRUE;
3422 scsuspend(__unused void *arg)
3426 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3427 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3428 KASSERT(sc_console->sc->cur_scp != NULL,
3429 ("sc_console->sc->cur_scp != NULL"));
3431 sc_susp_scr = sc_console->sc->cur_scp->index;
3432 if (sc_no_suspend_vtswitch ||
3433 sc_susp_scr == sc_console->index) {
3434 sc_touch_scrn_saver();
3438 for (retry = 0; retry < 10; retry++) {
3439 sc_switch_scr(sc_console->sc, sc_console->index);
3440 if (!sc_console->sc->switch_in_progress)
3442 pause("scsuspend", hz);
3444 suspend_in_progress = TRUE;
3448 scresume(__unused void *arg)
3451 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3452 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3453 KASSERT(sc_console->sc->cur_scp != NULL,
3454 ("sc_console->sc->cur_scp != NULL"));
3456 suspend_in_progress = FALSE;
3457 if (sc_susp_scr < 0) {
3458 update_font(sc_console->sc->cur_scp);
3461 sc_switch_scr(sc_console->sc, sc_susp_scr);
3465 sc_clean_up(scr_stat *scp)
3471 if (scp->sc->flags & SC_SCRN_BLANKED) {
3472 sc_touch_scrn_saver();
3474 if ((error = wait_scrn_saver_stop(scp->sc)))
3478 scp->status |= MOUSE_HIDDEN;
3479 sc_remove_mouse_image(scp);
3480 sc_remove_cutmarking(scp);
3485 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3491 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3492 if (!discard && (old.vtb_flags & VTB_VALID)) {
3493 /* retain the current cursor position and buffer contants */
3494 scp->cursor_oldpos = scp->cursor_pos;
3496 * This works only if the old buffer has the same size as or larger
3497 * than the new one. XXX
3499 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3503 sc_vtb_destroy(&old);
3506 #ifndef SC_NO_SYSMOUSE
3507 /* move the mouse cursor at the center of the screen */
3508 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3513 *alloc_scp(sc_softc_t *sc, int vty)
3517 /* assert(sc_malloc) */
3519 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3520 init_scp(sc, vty, scp);
3522 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3523 if (sc_init_emulator(scp, SC_DFLT_TERM))
3524 sc_init_emulator(scp, "*");
3526 #ifndef SC_NO_CUTPASTE
3527 sc_alloc_cut_buffer(scp, TRUE);
3530 #ifndef SC_NO_HISTORY
3531 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3538 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3542 bzero(scp, sizeof(*scp));
3547 scp->mode = sc->initial_mode;
3548 vidd_get_info(sc->adp, scp->mode, &info);
3549 if (info.vi_flags & V_INFO_GRAPHICS) {
3550 scp->status |= GRAPHICS_MODE;
3551 scp->xpixel = info.vi_width;
3552 scp->ypixel = info.vi_height;
3553 scp->xsize = info.vi_width/info.vi_cwidth;
3554 scp->ysize = info.vi_height/info.vi_cheight;
3558 scp->xsize = info.vi_width;
3559 scp->ysize = info.vi_height;
3560 scp->xpixel = scp->xsize*info.vi_cwidth;
3561 scp->ypixel = scp->ysize*info.vi_cheight;
3564 scp->font_size = info.vi_cheight;
3565 scp->font_width = info.vi_cwidth;
3566 #ifndef SC_NO_FONT_LOADING
3567 if (info.vi_cheight < 14)
3568 scp->font = sc->font_8;
3569 else if (info.vi_cheight >= 16)
3570 scp->font = sc->font_16;
3572 scp->font = sc->font_14;
3577 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3579 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3581 scp->xoff = scp->yoff = 0;
3582 scp->xpos = scp->ypos = 0;
3583 scp->start = scp->xsize * scp->ysize - 1;
3588 scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3589 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3590 scp->mouse_cut_start = scp->xsize*scp->ysize;
3591 scp->mouse_cut_end = -1;
3592 scp->mouse_signal = 0;
3594 scp->mouse_proc = NULL;
3595 scp->kbd_mode = K_XLATE;
3596 scp->bell_pitch = bios_value.bell_pitch;
3597 scp->bell_duration = BELL_DURATION;
3598 scp->status |= (bios_value.shift_state & NLKED);
3599 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3602 scp->smode.mode = VT_AUTO;
3603 scp->history = NULL;
3604 scp->history_pos = 0;
3605 scp->history_size = 0;
3609 sc_init_emulator(scr_stat *scp, char *name)
3616 if (name == NULL) /* if no name is given, use the current emulator */
3618 else /* ...otherwise find the named emulator */
3619 sw = sc_term_match(name);
3624 if (strcmp(sw->te_renderer, "*") != 0) {
3625 rndr = sc_render_match(scp, sw->te_renderer,
3626 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3629 rndr = sc_render_match(scp, scp->sc->adp->va_name,
3630 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3635 if (sw == scp->tsw) {
3636 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3638 scp->rndr->init(scp);
3639 sc_clear_screen(scp);
3640 /* assert(error == 0); */
3644 if (sc_malloc && (sw->te_size > 0))
3645 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3648 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3653 (*scp->tsw->te_term)(scp, &scp->ts);
3654 if (scp->ts != NULL)
3655 free(scp->ts, M_DEVBUF);
3659 scp->rndr->init(scp);
3661 (*sw->te_default_attr)(scp, SC_NORM_ATTR, SC_NORM_REV_ATTR);
3662 sc_clear_screen(scp);
3668 * scgetc(flags) - get character from keyboard.
3669 * If flags & SCGETC_CN, then avoid harmful side effects.
3670 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3671 * return NOKEY if there is nothing there.
3674 scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp)
3677 #ifndef SC_NO_HISTORY
3685 if (sc->kbd == NULL)
3690 /* I don't like this, but... XXX */
3691 if (flags & SCGETC_CN)
3692 sccnupdate(sc->cur_scp);
3695 /* first see if there is something in the keyboard port */
3697 if (flags & SCGETC_CN)
3698 sccnscrunlock(sc, sp);
3699 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3700 if (flags & SCGETC_CN)
3701 sccnscrlock(sc, sp);
3703 if (!(flags & SCGETC_CN))
3704 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3705 } else if (c == NOKEY)
3711 /* make screensaver happy */
3713 sc_touch_scrn_saver();
3715 if (!(flags & SCGETC_CN))
3716 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
3718 if (sc->kbd_open_level == 0 && scp->kbd_mode != K_XLATE)
3721 /* if scroll-lock pressed allow history browsing */
3722 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3724 scp->status &= ~CURSOR_ENABLED;
3725 sc_remove_cursor_image(scp);
3727 #ifndef SC_NO_HISTORY
3728 if (!(scp->status & BUFFER_SAVED)) {
3729 scp->status |= BUFFER_SAVED;
3733 /* FIXME: key codes */
3734 case SPCLKEY | FKEY | F(49): /* home key */
3735 sc_remove_cutmarking(scp);
3739 case SPCLKEY | FKEY | F(57): /* end key */
3740 sc_remove_cutmarking(scp);
3744 case SPCLKEY | FKEY | F(50): /* up arrow key */
3745 sc_remove_cutmarking(scp);
3746 if (sc_hist_up_line(scp))
3747 if (!(flags & SCGETC_CN))
3748 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3751 case SPCLKEY | FKEY | F(58): /* down arrow key */
3752 sc_remove_cutmarking(scp);
3753 if (sc_hist_down_line(scp))
3754 if (!(flags & SCGETC_CN))
3755 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3758 case SPCLKEY | FKEY | F(51): /* page up key */
3759 sc_remove_cutmarking(scp);
3760 for (i=0; i<scp->ysize; i++)
3761 if (sc_hist_up_line(scp)) {
3762 if (!(flags & SCGETC_CN))
3763 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3768 case SPCLKEY | FKEY | F(59): /* page down key */
3769 sc_remove_cutmarking(scp);
3770 for (i=0; i<scp->ysize; i++)
3771 if (sc_hist_down_line(scp)) {
3772 if (!(flags & SCGETC_CN))
3773 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3778 #endif /* SC_NO_HISTORY */
3782 * Process and consume special keys here. Return a plain char code
3783 * or a char code with the META flag or a function key code.
3787 /* goto next_code */
3792 switch (KEYCHAR(c)) {
3794 case NLK: case CLK: case ALK:
3797 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3799 scp->status |= SLKED;
3801 if (scp->status & SLKED) {
3802 scp->status &= ~SLKED;
3803 #ifndef SC_NO_HISTORY
3804 if (scp->status & BUFFER_SAVED) {
3805 if (!sc_hist_restore(scp))
3806 sc_remove_cutmarking(scp);
3807 scp->status &= ~BUFFER_SAVED;
3808 scp->status |= CURSOR_ENABLED;
3809 sc_draw_cursor_image(scp);
3811 /* Only safe in Giant-locked context. */
3812 tp = SC_DEV(sc, scp->index);
3813 if (!(flags & SCGETC_CN) && tty_opened_ns(tp))
3814 sctty_outwakeup(tp);
3821 #ifndef SC_NO_CUTPASTE
3822 sc_mouse_paste(scp);
3826 /* NON-LOCKING KEYS */
3828 case LSH: case RSH: case LCTR: case RCTR:
3829 case LALT: case RALT: case ASH: case META:
3833 if (!(sc->flags & SC_SCRN_BLANKED))
3839 /* force activatation/deactivation of the screen saver */
3840 if (!(sc->flags & SC_SCRN_BLANKED)) {
3841 run_scrn_saver = TRUE;
3842 sc->scrn_time_stamp -= scrn_blank_time;
3846 * While devices are being probed, the screen saver need
3847 * to be invoked explicitly. XXX
3849 if (sc->flags & SC_SCRN_BLANKED) {
3850 scsplash_stick(FALSE);
3851 stop_scrn_saver(sc, current_saver);
3853 if (!ISGRAPHSC(scp)) {
3854 scsplash_stick(TRUE);
3855 (*current_saver)(sc, TRUE);
3859 #endif /* DEV_SPLASH */
3863 #ifndef SC_DISABLE_REBOOT
3864 if (enable_reboot && !(flags & SCGETC_CN))
3870 #ifndef SC_DISABLE_REBOOT
3871 if (enable_reboot && !(flags & SCGETC_CN))
3872 shutdown_nice(RB_HALT);
3877 #ifndef SC_DISABLE_REBOOT
3878 if (enable_reboot && !(flags & SCGETC_CN))
3879 shutdown_nice(RB_HALT|RB_POWEROFF);
3884 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3887 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3891 #ifndef SC_DISABLE_KDBKEY
3898 if (enable_panic_key)
3899 panic("Forced by the panic key");
3903 this_scr = scp->index;
3904 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3905 sc->first_vty + i != this_scr;
3906 i = (i + 1)%sc->vtys) {
3907 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3908 if (tty_opened_ns(tp)) {
3909 sc_switch_scr(scp->sc, sc->first_vty + i);
3916 this_scr = scp->index;
3917 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3918 sc->first_vty + i != this_scr;
3919 i = (i + sc->vtys - 1)%sc->vtys) {
3920 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3921 if (tty_opened_ns(tp)) {
3922 sc_switch_scr(scp->sc, sc->first_vty + i);
3929 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3930 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3933 /* assert(c & FKEY) */
3934 if (!(sc->flags & SC_SCRN_BLANKED))
3938 /* goto next_code */
3940 /* regular keys (maybe MKEY is set) */
3941 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
3943 kdb_alt_break(c, &sc->sc_altbrk);
3945 if (!(sc->flags & SC_SCRN_BLANKED))
3954 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
3955 int nprot, vm_memattr_t *memattr)
3959 scp = sc_get_stat(tp);
3960 if (scp != scp->sc->cur_scp)
3962 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
3966 update_font(scr_stat *scp)
3968 #ifndef SC_NO_FONT_LOADING
3969 /* load appropriate font */
3970 if (!(scp->status & GRAPHICS_MODE)) {
3971 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3972 if (scp->font_size < 14) {
3973 if (scp->sc->fonts_loaded & FONT_8)
3974 sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3975 } else if (scp->font_size >= 16) {
3976 if (scp->sc->fonts_loaded & FONT_16)
3977 sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3979 if (scp->sc->fonts_loaded & FONT_14)
3980 sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3984 * This is an interim kludge to display correct font.
3985 * Always use the font page #0 on the video plane 2.
3986 * Somehow we cannot show the font in other font pages on
3987 * some video cards... XXX
3989 sc_show_font(scp, 0);
3993 #endif /* !SC_NO_FONT_LOADING */
3997 save_kbd_state(scr_stat *scp)
4002 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4003 if (error == ENOIOCTL)
4006 scp->status &= ~LOCK_MASK;
4007 scp->status |= state;
4013 update_kbd_state(scr_stat *scp, int new_bits, int mask)
4018 if (mask != LOCK_MASK) {
4019 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4020 if (error == ENOIOCTL)
4025 state |= new_bits & mask;
4027 state = new_bits & LOCK_MASK;
4029 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
4030 if (error == ENOIOCTL)
4036 update_kbd_leds(scr_stat *scp, int which)
4041 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
4042 if (error == ENOIOCTL)
4048 set_mode(scr_stat *scp)
4052 /* reject unsupported mode */
4053 if (vidd_get_info(scp->sc->adp, scp->mode, &info))
4056 /* if this vty is not currently showing, do nothing */
4057 if (scp != scp->sc->cur_scp)
4060 /* setup video hardware for the given mode */
4061 vidd_set_mode(scp->sc->adp, scp->mode);
4062 scp->rndr->init(scp);
4064 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
4065 (void *)scp->sc->adp->va_window, FALSE);
4070 sc_set_border(scp, scp->border);
4071 sc_set_cursor_image(scp);
4077 sc_set_border(scr_stat *scp, int color)
4079 SC_VIDEO_LOCK(scp->sc);
4080 (*scp->rndr->draw_border)(scp, color);
4081 SC_VIDEO_UNLOCK(scp->sc);
4084 #ifndef SC_NO_FONT_LOADING
4086 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
4087 int base, int count)
4092 sc->font_loading_in_progress = TRUE;
4093 vidd_load_font(sc->adp, page, size, width, buf, base, count);
4094 sc->font_loading_in_progress = FALSE;
4098 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
4099 int base, int count)
4104 sc->font_loading_in_progress = TRUE;
4105 vidd_save_font(sc->adp, page, size, width, buf, base, count);
4106 sc->font_loading_in_progress = FALSE;
4110 sc_show_font(scr_stat *scp, int page)
4112 vidd_show_font(scp->sc->adp, page);
4114 #endif /* !SC_NO_FONT_LOADING */
4117 sc_paste(scr_stat *scp, const u_char *p, int count)
4122 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
4123 if (!tty_opened_ns(tp))
4125 rmap = scp->sc->scr_rmap;
4126 for (; count > 0; --count)
4127 ttydisc_rint(tp, rmap[*p++], 0);
4128 ttydisc_rint_done(tp);
4132 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup)
4136 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
4137 if (!tty_opened_ns(tp))
4139 ttydisc_rint_simple(tp, p, count);
4141 /* XXX: we can't always call ttydisc_rint_done() here! */
4142 ttydisc_rint_done(tp);
4147 sc_bell(scr_stat *scp, int pitch, int duration)
4149 if (cold || kdb_active || shutdown_in_progress || !enable_bell)
4152 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
4155 if (scp->sc->flags & SC_VISUAL_BELL) {
4156 if (scp->sc->blink_in_progress)
4158 scp->sc->blink_in_progress = 3;
4159 if (scp != scp->sc->cur_scp)
4160 scp->sc->blink_in_progress += 2;
4161 blink_screen(scp->sc->cur_scp);
4162 } else if (duration != 0 && pitch != 0) {
4163 if (scp != scp->sc->cur_scp)
4165 sysbeep(1193182 / pitch, duration);
4172 if (sc_console == NULL)
4173 return (SC_KERNEL_CONS_ATTR); /* for very early, before pcpu */
4174 return (sc_kattrtab[PCPU_GET(cpuid) % nitems(sc_kattrtab)]);
4178 blink_screen(void *arg)
4180 scr_stat *scp = arg;
4183 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
4184 scp->sc->blink_in_progress = 0;
4186 tp = SC_DEV(scp->sc, scp->index);
4187 if (tty_opened_ns(tp))
4188 sctty_outwakeup(tp);
4189 if (scp->sc->delayed_next_scr)
4190 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
4193 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
4194 scp->sc->blink_in_progress & 1);
4195 scp->sc->blink_in_progress--;
4196 callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0,
4197 blink_screen, scp, C_PREL(0));
4202 * Until sc_attach_unit() gets called no dev structures will be available
4203 * to store the per-screen current status. This is the case when the
4204 * kernel is initially booting and needs access to its console. During
4205 * this early phase of booting the console's current status is kept in
4206 * one statically defined scr_stat structure, and any pointers to the
4207 * dev structures will be NULL.
4211 sc_get_stat(struct tty *tp)
4214 return (&main_console);
4215 return (SC_STAT(tp));
4219 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
4220 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
4225 sc_allocate_keyboard(sc_softc_t *sc, int unit)
4231 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
4233 k0 = kbd_get_keyboard(idx0);
4235 for (idx = kbd_find_keyboard2("*", -1, 0);
4237 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
4238 k = kbd_get_keyboard(idx);
4240 if (idx == idx0 || KBD_IS_BUSY(k))
4243 bzero(&ki, sizeof(ki));
4244 strcpy(ki.kb_name, k->kb_name);
4245 ki.kb_unit = k->kb_unit;
4247 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
4250 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);