2 * Copyright (c) 1992-1998 Søren Schmidt
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_syscons.h"
37 #include "opt_splash.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/consio.h>
47 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
56 #include <sys/random.h>
57 #include <sys/reboot.h>
58 #include <sys/serial.h>
59 #include <sys/signalvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/power.h>
64 #include <machine/clock.h>
65 #if defined(__arm__) || defined(__mips__) || \
66 defined(__powerpc__) || defined(__sparc64__)
67 #include <machine/sc_machdep.h>
69 #include <machine/pc/display.h>
71 #if defined( __i386__) || defined(__amd64__)
72 #include <machine/psl.h>
73 #include <machine/frame.h>
75 #include <machine/stdarg.h>
77 #include <dev/kbd/kbdreg.h>
78 #include <dev/fb/fbreg.h>
79 #include <dev/fb/splashreg.h>
80 #include <dev/syscons/syscons.h>
85 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
86 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
88 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
90 /* NULL-safe version of "tty_opened()" */
91 #define tty_opened_ns(tp) ((tp) != NULL && tty_opened(tp))
93 typedef struct default_attr {
94 int std_color; /* normal hardware color */
95 int rev_color; /* reverse hardware color */
98 static default_attr user_default = {
103 static int sc_console_unit = -1;
104 static int sc_saver_keyb_only = 1;
105 static scr_stat *sc_console;
106 static struct consdev *sc_consptr;
107 static scr_stat main_console;
108 static struct tty *main_devs[MAXCONS];
110 static char init_done = COLD;
111 static int shutdown_in_progress = FALSE;
112 static int suspend_in_progress = FALSE;
113 static char sc_malloc = FALSE;
115 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
116 static int run_scrn_saver = FALSE; /* should run the saver? */
117 static int enable_bell = TRUE; /* enable beeper */
119 #ifndef SC_DISABLE_REBOOT
120 static int enable_reboot = TRUE; /* enable keyboard reboot */
123 #ifndef SC_DISABLE_KDBKEY
124 static int enable_kdbkey = TRUE; /* enable keyboard debug */
127 static long scrn_blank_time = 0; /* screen saver timeout value */
129 static int scrn_blanked; /* # of blanked screen */
130 static int sticky_splash = FALSE;
132 static void none_saver(sc_softc_t *sc, int blank) { }
133 static void (*current_saver)(sc_softc_t *, int) = none_saver;
136 #ifdef SC_NO_SUSPEND_VTYSWITCH
137 static int sc_no_suspend_vtswitch = 1;
139 static int sc_no_suspend_vtswitch = 0;
141 static int sc_susp_scr;
143 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
144 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
145 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
146 &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
147 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell,
149 #ifndef SC_DISABLE_REBOOT
150 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
151 0, "enable keyboard reboot");
153 #ifndef SC_DISABLE_KDBKEY
154 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
155 0, "enable keyboard debug");
157 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN,
158 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
159 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
163 tsw_ioctl_t *sc_user_ioctl;
165 static bios_values_t bios_value;
167 static int enable_panic_key;
168 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
169 0, "Enable panic via keypress specified in kbdmap(5)");
171 #define SC_CONSOLECTL 255
173 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
176 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
177 static int scvidprobe(int unit, int flags, int cons);
178 static int sckbdprobe(int unit, int flags, int cons);
179 static void scmeminit(void *arg);
180 static int scdevtounit(struct tty *tp);
181 static kbd_callback_func_t sckbdevent;
182 static void scinit(int unit, int flags);
183 static scr_stat *sc_get_stat(struct tty *tp);
184 static void scterm(int unit, int flags);
185 static void scshutdown(void *, int);
186 static void scsuspend(void *);
187 static void scresume(void *);
188 static u_int scgetc(sc_softc_t *sc, u_int flags);
189 static void sc_puts(scr_stat *scp, u_char *buf, int len, int kernel);
191 #define SCGETC_NONBLOCK 2
192 static void sccnupdate(scr_stat *scp);
193 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
194 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
195 static timeout_t scrn_timer;
196 static int and_region(int *s1, int *e1, int s2, int e2);
197 static void scrn_update(scr_stat *scp, int show_cursor);
200 static int scsplash_callback(int event, void *arg);
201 static void scsplash_saver(sc_softc_t *sc, int show);
202 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
203 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
204 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
205 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
206 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
207 static int wait_scrn_saver_stop(sc_softc_t *sc);
208 #define scsplash_stick(stick) (sticky_splash = (stick))
209 #else /* !DEV_SPLASH */
210 #define scsplash_stick(stick)
211 #endif /* DEV_SPLASH */
213 static int do_switch_scr(sc_softc_t *sc, int s);
214 static int vt_proc_alive(scr_stat *scp);
215 static int signal_vt_rel(scr_stat *scp);
216 static int signal_vt_acq(scr_stat *scp);
217 static int finish_vt_rel(scr_stat *scp, int release, int *s);
218 static int finish_vt_acq(scr_stat *scp);
219 static void exchange_scr(sc_softc_t *sc);
220 static void update_cursor_image(scr_stat *scp);
221 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
222 static void update_font(scr_stat *);
223 static int save_kbd_state(scr_stat *scp);
224 static int update_kbd_state(scr_stat *scp, int state, int mask);
225 static int update_kbd_leds(scr_stat *scp, int which);
226 static timeout_t blink_screen;
227 static struct tty *sc_alloc_tty(int, int);
229 static cn_probe_t sc_cnprobe;
230 static cn_init_t sc_cninit;
231 static cn_term_t sc_cnterm;
232 static cn_getc_t sc_cngetc;
233 static cn_putc_t sc_cnputc;
234 static cn_grab_t sc_cngrab;
235 static cn_ungrab_t sc_cnungrab;
239 static tsw_open_t sctty_open;
240 static tsw_close_t sctty_close;
241 static tsw_outwakeup_t sctty_outwakeup;
242 static tsw_ioctl_t sctty_ioctl;
243 static tsw_mmap_t sctty_mmap;
245 static struct ttydevsw sc_ttydevsw = {
246 .tsw_open = sctty_open,
247 .tsw_close = sctty_close,
248 .tsw_outwakeup = sctty_outwakeup,
249 .tsw_ioctl = sctty_ioctl,
250 .tsw_mmap = sctty_mmap,
253 static d_ioctl_t consolectl_ioctl;
254 static d_close_t consolectl_close;
256 static struct cdevsw consolectl_devsw = {
257 .d_version = D_VERSION,
258 .d_flags = D_NEEDGIANT | D_TRACKCLOSE,
259 .d_ioctl = consolectl_ioctl,
260 .d_close = consolectl_close,
261 .d_name = "consolectl",
265 sc_probe_unit(int unit, int flags)
267 if (!vty_enabled(VTY_SC))
269 if (!scvidprobe(unit, flags, FALSE)) {
271 printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
275 /* syscons will be attached even when there is no keyboard */
276 sckbdprobe(unit, flags, FALSE);
281 /* probe video adapters, return TRUE if found */
283 scvidprobe(int unit, int flags, int cons)
286 * Access the video adapter driver through the back door!
287 * Video adapter drivers need to be configured before syscons.
288 * However, when syscons is being probed as the low-level console,
289 * they have not been initialized yet. We force them to initialize
290 * themselves here. XXX
292 vid_configure(cons ? VIO_PROBE_ONLY : 0);
294 return (vid_find_adapter("*", unit) >= 0);
297 /* probe the keyboard, return TRUE if found */
299 sckbdprobe(int unit, int flags, int cons)
301 /* access the keyboard driver through the backdoor! */
302 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
304 return (kbd_find_keyboard("*", unit) >= 0);
308 *adapter_name(video_adapter_t *adp)
314 { KD_MONO, { "MDA", "MDA" } },
315 { KD_HERCULES, { "Hercules", "Hercules" } },
316 { KD_CGA, { "CGA", "CGA" } },
317 { KD_EGA, { "EGA", "EGA (mono)" } },
318 { KD_VGA, { "VGA", "VGA (mono)" } },
319 { KD_PC98, { "PC-98x1", "PC-98x1" } },
320 { KD_TGA, { "TGA", "TGA" } },
321 { -1, { "Unknown", "Unknown" } },
325 for (i = 0; names[i].type != -1; ++i)
326 if (names[i].type == adp->va_type)
328 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
332 sctty_outwakeup(struct tty *tp)
336 scr_stat *scp = sc_get_stat(tp);
338 if (scp->status & SLKED ||
339 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
343 len = ttydisc_getc(tp, buf, sizeof buf);
346 sc_puts(scp, buf, len, 0);
351 sc_alloc_tty(int index, int devnum)
353 struct sc_ttysoftc *stc;
356 /* Allocate TTY object and softc to store unit number. */
357 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
358 stc->st_index = index;
360 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
362 /* Create device node. */
363 tty_makedev(tp, NULL, "v%r", devnum);
370 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
380 (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
381 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
382 vidd_get_info(sc->adp, vmode, &info) != 0 ||
383 !sc_support_pixel_mode(&info))
387 * If the mode is unset or unsupported, search for an available
388 * 800x600 graphics mode with the highest color depth.
391 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
392 if (vidd_get_info(sc->adp, i, &info) == 0 &&
393 info.vi_width == 800 && info.vi_height == 600 &&
394 sc_support_pixel_mode(&info) &&
395 info.vi_depth > depth) {
397 depth = info.vi_depth;
401 vidd_get_info(sc->adp, vmode, &info);
404 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
405 fontsize = info.vi_cheight;
407 fontsize = scp->font_size;
411 else if (fontsize >= 16)
415 #ifndef SC_NO_FONT_LOADING
418 if ((sc->fonts_loaded & FONT_8) == 0)
423 if ((sc->fonts_loaded & FONT_14) == 0)
428 if ((sc->fonts_loaded & FONT_16) == 0)
437 if ((sc->flags & SC_SPLASH_SCRN) != 0)
438 splash_term(sc->adp);
440 #ifndef SC_NO_HISTORY
441 if (scp->history != NULL) {
442 sc_vtb_append(&scp->vtb, 0, scp->history,
443 scp->ypos * scp->xsize + scp->xpos);
444 scp->history_pos = sc_vtb_tail(scp->history);
447 vidd_set_mode(sc->adp, vmode);
448 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
449 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
450 scp->xpixel = info.vi_width;
451 scp->ypixel = info.vi_height;
452 scp->xsize = scp->xpixel / 8;
453 scp->ysize = scp->ypixel / fontsize;
455 scp->ypos = scp->ysize - 1;
456 scp->xoff = scp->yoff = 0;
458 scp->font_size = fontsize;
460 scp->start = scp->xsize * scp->ysize - 1;
462 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
463 scp->mode = sc->initial_mode = vmode;
465 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
466 (void *)sc->adp->va_window, FALSE);
468 sc_alloc_scr_buffer(scp, FALSE, FALSE);
469 sc_init_emulator(scp, NULL);
470 #ifndef SC_NO_CUTPASTE
471 sc_alloc_cut_buffer(scp, FALSE);
473 #ifndef SC_NO_HISTORY
474 sc_alloc_history_buffer(scp, 0, 0, FALSE);
476 sc_set_border(scp, scp->border);
477 sc_set_cursor_image(scp);
478 scp->status &= ~UNKNOWN_MODE;
480 if ((sc->flags & SC_SPLASH_SCRN) != 0)
481 splash_init(sc->adp, scsplash_callback, sc);
487 sc_attach_unit(int unit, int flags)
494 if (!vty_enabled(VTY_SC))
497 flags &= ~SC_KERNEL_CONSOLE;
499 if (sc_console_unit == unit) {
501 * If this unit is being used as the system console, we need to
502 * adjust some variables and buffers before and after scinit().
504 /* assert(sc_console != NULL) */
505 flags |= SC_KERNEL_CONSOLE;
510 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
512 callout_init(&sc->ctimeout, 0);
513 callout_init(&sc->cblink, 0);
514 scp = sc_get_stat(sc->dev[0]);
515 if (sc_console == NULL) /* sc_console_unit < 0 */
519 if ((sc->config & SC_VESAMODE) != 0)
520 sc_set_vesa_mode(scp, sc, unit);
521 #endif /* SC_PIXEL_MODE */
523 /* initialize cursor */
525 update_cursor_image(scp);
527 /* get screen update going */
530 /* set up the keyboard */
531 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
532 update_kbd_state(scp, scp->status, LOCK_MASK);
534 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
535 SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
537 printf("%s%d:", SC_DRIVER_NAME, unit);
538 if (sc->adapter >= 0)
539 printf(" fb%d", sc->adapter);
540 if (sc->keyboard >= 0)
541 printf(", kbd%d", sc->keyboard);
543 printf(", terminal emulator: %s (%s)",
544 scp->tsw->te_name, scp->tsw->te_desc);
548 /* Register suspend/resume/shutdown callbacks for the kernel console. */
549 if (sc_console_unit == unit) {
550 EVENTHANDLER_REGISTER(power_suspend_early, scsuspend, NULL,
551 EVENTHANDLER_PRI_ANY);
552 EVENTHANDLER_REGISTER(power_resume, scresume, NULL,
553 EVENTHANDLER_PRI_ANY);
554 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL,
555 SHUTDOWN_PRI_DEFAULT);
558 for (vc = 0; vc < sc->vtys; vc++) {
559 if (sc->dev[vc] == NULL) {
560 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
561 if (vc == 0 && sc->dev == main_devs)
562 SC_STAT(sc->dev[0]) = &main_console;
565 * The first vty already has struct tty and scr_stat initialized
566 * in scinit(). The other vtys will have these structs when
571 dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600,
573 dev->si_drv1 = sc->dev[0];
581 if (!vty_enabled(VTY_SC))
588 * As soon as malloc() becomes functional, we had better allocate
589 * various buffers for the kernel console.
592 if (sc_console_unit < 0) /* sc_console == NULL */
595 /* copy the temporary buffer to the final buffer */
596 sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
598 #ifndef SC_NO_CUTPASTE
599 sc_alloc_cut_buffer(sc_console, FALSE);
602 #ifndef SC_NO_HISTORY
603 /* initialize history buffer & pointers */
604 sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
609 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
612 scdevtounit(struct tty *tp)
614 int vty = SC_VTY(tp);
616 if (vty == SC_CONSOLECTL)
617 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
618 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
625 sctty_open(struct tty *tp)
627 int unit = scdevtounit(tp);
634 DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
635 devtoname(tp->t_dev), unit, SC_VTY(tp)));
637 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
641 if (!tty_opened(tp)) {
642 /* Use the current setting of the <-- key as default VERASE. */
643 /* If the Delete key is preferable, an stty is necessary */
645 if (sc->kbd != NULL) {
646 key.keynum = KEYCODE_BS;
647 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
648 tp->t_termios.c_cc[VERASE] = key.key.map[0];
653 scp = sc_get_stat(tp);
655 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
657 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
659 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
660 tp->t_winsize.ws_col = scp->xsize;
661 tp->t_winsize.ws_row = scp->ysize;
668 sctty_close(struct tty *tp)
673 if (SC_VTY(tp) != SC_CONSOLECTL) {
674 scp = sc_get_stat(tp);
675 /* were we in the middle of the VT switching process? */
676 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
678 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
679 cnavailable(sc_consptr, TRUE);
680 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */
681 DPRINTF(5, ("reset WAIT_REL, "));
682 if (finish_vt_acq(scp) == 0) /* force acknowledge */
683 DPRINTF(5, ("reset WAIT_ACQ, "));
685 if (scp == &main_console) {
688 scp->smode.mode = VT_AUTO;
691 sc_vtb_destroy(&scp->vtb);
693 sc_vtb_destroy(&scp->scr);
695 sc_free_history_buffer(scp, scp->ysize);
702 scp->smode.mode = VT_AUTO;
704 scp->kbd_mode = K_XLATE;
705 if (scp == scp->sc->cur_scp)
706 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
707 DPRINTF(5, ("done.\n"));
711 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
713 scread(struct cdev *dev, struct uio *uio, int flag)
715 if (!sc_saver_keyb_only)
716 sc_touch_scrn_saver();
717 return ttyread(dev, uio, flag);
722 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
730 sc = (sc_softc_t *)arg;
731 /* assert(thiskbd == sc->kbd) */
738 case KBDIO_UNLOADING:
741 kbd_release(thiskbd, (void *)&sc->keyboard);
749 * Loop while there is still input to get from the keyboard.
750 * I don't think this is nessesary, and it doesn't fix
751 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
753 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
755 cur_tty = SC_DEV(sc, sc->cur_scp->index);
756 if (!tty_opened_ns(cur_tty))
759 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
762 switch (KEYFLAGS(c)) {
763 case 0x0000: /* normal key */
764 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
766 case FKEY: /* function key, return string */
767 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
769 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
772 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
774 ttydisc_rint_simple(cur_tty, cp, len);
776 case MKEY: /* meta is active, prepend ESC */
777 ttydisc_rint(cur_tty, 0x1b, 0);
778 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
780 case BKEY: /* backtab fixed sequence (esc [ Z) */
781 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
785 ttydisc_rint_done(cur_tty);
788 sc->cur_scp->status |= MOUSE_HIDDEN;
796 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
803 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
804 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
808 /* If there is a user_ioctl function call that first */
810 error = (*sc_user_ioctl)(tp, cmd, data, td);
811 if (error != ENOIOCTL)
815 error = sc_vid_ioctl(tp, cmd, data, td);
816 if (error != ENOIOCTL)
819 #ifndef SC_NO_HISTORY
820 error = sc_hist_ioctl(tp, cmd, data, td);
821 if (error != ENOIOCTL)
825 #ifndef SC_NO_SYSMOUSE
826 error = sc_mouse_ioctl(tp, cmd, data, td);
827 if (error != ENOIOCTL)
831 scp = sc_get_stat(tp);
832 /* assert(scp != NULL) */
833 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
837 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
838 if (error != ENOIOCTL)
842 switch (cmd) { /* process console hardware related ioctl's */
844 case GIO_ATTR: /* get current attributes */
845 /* this ioctl is not processed here, but in the terminal emulator */
848 case GIO_COLOR: /* is this a color console ? */
849 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
852 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
853 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
856 scrn_blank_time = *(int *)data;
857 run_scrn_saver = (scrn_blank_time != 0);
861 case CONS_CURSORTYPE: /* set cursor type (obsolete) */
863 *(int *)data &= CONS_CURSOR_ATTRS;
864 sc_change_cursor_shape(scp, *(int *)data, -1, -1);
868 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */
869 if (((int *)data)[0] & CONS_LOCAL_CURSOR) {
870 ((int *)data)[0] = scp->curr_curs_attr.flags;
871 ((int *)data)[1] = scp->curr_curs_attr.base;
872 ((int *)data)[2] = scp->curr_curs_attr.height;
874 ((int *)data)[0] = sc->curs_attr.flags;
875 ((int *)data)[1] = sc->curs_attr.base;
876 ((int *)data)[2] = sc->curs_attr.height;
880 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */
882 sc_change_cursor_shape(scp, ((int *)data)[0],
883 ((int *)data)[1], ((int *)data)[2]);
887 case CONS_BELLTYPE: /* set bell type sound/visual */
888 if ((*(int *)data) & CONS_VISUAL_BELL)
889 sc->flags |= SC_VISUAL_BELL;
891 sc->flags &= ~SC_VISUAL_BELL;
892 if ((*(int *)data) & CONS_QUIET_BELL)
893 sc->flags |= SC_QUIET_BELL;
895 sc->flags &= ~SC_QUIET_BELL;
898 case CONS_GETINFO: /* get current (virtual) console info */
900 vid_info_t *ptr = (vid_info_t*)data;
901 if (ptr->size == sizeof(struct vid_info)) {
902 ptr->m_num = sc->cur_scp->index;
903 ptr->font_size = scp->font_size;
904 ptr->mv_col = scp->xpos;
905 ptr->mv_row = scp->ypos;
906 ptr->mv_csz = scp->xsize;
907 ptr->mv_rsz = scp->ysize;
908 ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
910 * The following fields are filled by the terminal emulator. XXX
917 ptr->mv_grfc.fore = 0; /* not supported */
918 ptr->mv_grfc.back = 0; /* not supported */
919 ptr->mv_ovscan = scp->border;
920 if (scp == sc->cur_scp)
922 ptr->mk_keylock = scp->status & LOCK_MASK;
928 case CONS_GETVERS: /* get version number */
929 *(int*)data = 0x200; /* version 2.0 */
932 case CONS_IDLE: /* see if the screen has been idle */
934 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
935 * the user process may have been writing something on the
936 * screen and syscons is not aware of it. Declare the screen
937 * is NOT idle if it is in one of these modes. But there is
938 * an exception to it; if a screen saver is running in the
939 * graphics mode in the current screen, we should say that the
940 * screen has been idle.
942 *(int *)data = (sc->flags & SC_SCRN_IDLE)
943 && (!ISGRAPHSC(sc->cur_scp)
944 || (sc->cur_scp->status & SAVER_RUNNING));
947 case CONS_SAVERMODE: /* set saver mode */
948 switch(*(int *)data) {
951 /* if a LKM screen saver is running, stop it first. */
952 scsplash_stick(FALSE);
953 saver_mode = *(int *)data;
956 if ((error = wait_scrn_saver_stop(NULL))) {
961 run_scrn_saver = TRUE;
962 if (saver_mode == CONS_USR_SAVER)
963 scp->status |= SAVER_RUNNING;
965 scp->status &= ~SAVER_RUNNING;
966 scsplash_stick(TRUE);
971 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
972 scp->status &= ~SAVER_RUNNING;
973 saver_mode = *(int *)data;
981 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
983 * Note that this ioctl does not guarantee the screen saver
984 * actually starts or stops. It merely attempts to do so...
987 run_scrn_saver = (*(int *)data != 0);
989 sc->scrn_time_stamp -= scrn_blank_time;
993 case CONS_SCRSHOT: /* get a screen shot */
995 int retval, hist_rsz;
997 vm_offset_t frbp, hstp;
999 scrshot_t *ptr = (scrshot_t *)data;
1000 void *outp = ptr->buf;
1002 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
1005 if (ISGRAPHSC(scp)) {
1009 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1010 if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
1011 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1016 lsize = scp->xsize * sizeof(u_int16_t);
1017 csize = ptr->xsize * sizeof(u_int16_t);
1018 /* Pointer to the last line of framebuffer */
1019 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
1021 /* Pointer to the last line of target buffer */
1022 outp = (char *)outp + ptr->ysize * csize;
1023 /* Pointer to the last line of history buffer */
1024 if (scp->history != NULL)
1025 hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
1026 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
1031 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1032 if (lnum < scp->ysize) {
1036 if (hstp < scp->history->vtb_buffer)
1037 hstp += scp->history->vtb_rows * lsize;
1042 outp = (char *)outp - csize;
1043 retval = copyout((void *)frbp, outp, csize);
1051 case VT_SETMODE: /* set screen switcher mode */
1053 struct vt_mode *mode;
1056 mode = (struct vt_mode *)data;
1057 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1058 if (scp->smode.mode == VT_PROCESS) {
1059 p1 = pfind(scp->pid);
1060 if (scp->proc == p1 && scp->proc != td->td_proc) {
1063 DPRINTF(5, ("error EPERM\n"));
1070 if (mode->mode == VT_AUTO) {
1071 scp->smode.mode = VT_AUTO;
1074 DPRINTF(5, ("VT_AUTO, "));
1075 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1076 cnavailable(sc_consptr, TRUE);
1077 /* were we in the middle of the vty switching process? */
1078 if (finish_vt_rel(scp, TRUE, &s) == 0)
1079 DPRINTF(5, ("reset WAIT_REL, "));
1080 if (finish_vt_acq(scp) == 0)
1081 DPRINTF(5, ("reset WAIT_ACQ, "));
1083 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1084 || !ISSIGVALID(mode->frsig)) {
1086 DPRINTF(5, ("error EINVAL\n"));
1089 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1090 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1091 scp->proc = td->td_proc;
1092 scp->pid = scp->proc->p_pid;
1093 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1094 cnavailable(sc_consptr, FALSE);
1101 case VT_GETMODE: /* get screen switcher mode */
1102 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1105 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1106 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1108 ival = IOCPARM_IVAL(data);
1109 data = (caddr_t)&ival;
1112 case VT_RELDISP: /* screen switcher ioctl */
1115 * This must be the current vty which is in the VT_PROCESS
1118 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1122 /* ...and this process is controlling it. */
1123 if (scp->proc != td->td_proc) {
1128 switch(*(int *)data) {
1129 case VT_FALSE: /* user refuses to release screen, abort */
1130 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1131 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1133 case VT_TRUE: /* user has released screen, go on */
1134 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1135 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1137 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1138 if ((error = finish_vt_acq(scp)) == 0)
1139 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1147 case VT_OPENQRY: /* return free virtual console */
1148 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1150 if (!tty_opened_ns(tp)) {
1151 *(int *)data = i + 1;
1157 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1158 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1160 ival = IOCPARM_IVAL(data);
1161 data = (caddr_t)&ival;
1164 case VT_ACTIVATE: /* switch to screen *data */
1165 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1167 error = sc_clean_up(sc->cur_scp);
1171 error = sc_switch_scr(sc, i);
1174 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1175 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1177 ival = IOCPARM_IVAL(data);
1178 data = (caddr_t)&ival;
1181 case VT_WAITACTIVE: /* wait for switch to occur */
1182 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1183 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1185 if (i == sc->cur_scp->index)
1187 error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1190 case VT_GETACTIVE: /* get active vty # */
1191 *(int *)data = sc->cur_scp->index + 1;
1194 case VT_GETINDEX: /* get this vty # */
1195 *(int *)data = scp->index + 1;
1198 case VT_LOCKSWITCH: /* prevent vty switching */
1199 if ((*(int *)data) & 0x01)
1200 sc->flags |= SC_SCRN_VTYLOCK;
1202 sc->flags &= ~SC_SCRN_VTYLOCK;
1205 case KDENABIO: /* allow io operations */
1206 error = priv_check(td, PRIV_IO);
1209 error = securelevel_gt(td->td_ucred, 0);
1213 td->td_frame->tf_eflags |= PSL_IOPL;
1214 #elif defined(__amd64__)
1215 td->td_frame->tf_rflags |= PSL_IOPL;
1219 case KDDISABIO: /* disallow io operations (default) */
1221 td->td_frame->tf_eflags &= ~PSL_IOPL;
1222 #elif defined(__amd64__)
1223 td->td_frame->tf_rflags &= ~PSL_IOPL;
1227 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1228 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1230 ival = IOCPARM_IVAL(data);
1231 data = (caddr_t)&ival;
1234 case KDSKBSTATE: /* set keyboard state (locks) */
1235 if (*(int *)data & ~LOCK_MASK)
1237 scp->status &= ~LOCK_MASK;
1238 scp->status |= *(int *)data;
1239 if (scp == sc->cur_scp)
1240 update_kbd_state(scp, scp->status, LOCK_MASK);
1243 case KDGKBSTATE: /* get keyboard state (locks) */
1244 if (scp == sc->cur_scp)
1245 save_kbd_state(scp);
1246 *(int *)data = scp->status & LOCK_MASK;
1249 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1250 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1251 error = kbdd_ioctl(sc->kbd, cmd, data);
1252 if (error == ENOIOCTL)
1256 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1257 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1259 ival = IOCPARM_IVAL(data);
1260 data = (caddr_t)&ival;
1263 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1264 if (*(int *)data & ~0x7f)
1266 error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1267 if (error == ENOIOCTL)
1271 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1272 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1274 ival = IOCPARM_IVAL(data);
1275 data = (caddr_t)&ival;
1278 case KDSKBMODE: /* set keyboard mode */
1279 switch (*(int *)data) {
1280 case K_XLATE: /* switch to XLT ascii mode */
1281 case K_RAW: /* switch to RAW scancode mode */
1282 case K_CODE: /* switch to CODE mode */
1283 scp->kbd_mode = *(int *)data;
1284 if (scp == sc->cur_scp)
1285 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1292 case KDGKBMODE: /* get keyboard mode */
1293 *(int *)data = scp->kbd_mode;
1297 error = kbdd_ioctl(sc->kbd, cmd, data);
1298 if (error == ENOIOCTL)
1302 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1303 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1305 ival = IOCPARM_IVAL(data);
1306 data = (caddr_t)&ival;
1309 case KDMKTONE: /* sound the bell */
1311 sc_bell(scp, (*(int*)data)&0xffff,
1312 (((*(int*)data)>>16)&0xffff)*hz/1000);
1314 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1317 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1318 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1320 ival = IOCPARM_IVAL(data);
1321 data = (caddr_t)&ival;
1324 case KIOCSOUND: /* make tone (*data) hz */
1325 if (scp == sc->cur_scp) {
1327 return sc_tone(*(int *)data);
1333 case KDGKBTYPE: /* get keyboard type */
1334 error = kbdd_ioctl(sc->kbd, cmd, data);
1335 if (error == ENOIOCTL) {
1336 /* always return something? XXX */
1341 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1342 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1344 ival = IOCPARM_IVAL(data);
1345 data = (caddr_t)&ival;
1348 case KDSETLED: /* set keyboard LED status */
1349 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1351 scp->status &= ~LED_MASK;
1352 scp->status |= *(int *)data;
1353 if (scp == sc->cur_scp)
1354 update_kbd_leds(scp, scp->status);
1357 case KDGETLED: /* get keyboard LED status */
1358 if (scp == sc->cur_scp)
1359 save_kbd_state(scp);
1360 *(int *)data = scp->status & LED_MASK;
1363 case KBADDKBD: /* add/remove keyboard to/from mux */
1365 error = kbdd_ioctl(sc->kbd, cmd, data);
1366 if (error == ENOIOCTL)
1370 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1371 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1373 ival = IOCPARM_IVAL(data);
1374 data = (caddr_t)&ival;
1377 case CONS_SETKBD: /* set the new keyboard */
1382 newkbd = kbd_get_keyboard(*(int *)data);
1383 if (newkbd == NULL) {
1388 if (sc->kbd != newkbd) {
1389 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1390 (void *)&sc->keyboard, sckbdevent, sc);
1391 /* i == newkbd->kb_index */
1393 if (sc->kbd != NULL) {
1394 save_kbd_state(sc->cur_scp);
1395 kbd_release(sc->kbd, (void *)&sc->keyboard);
1397 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1399 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1400 (caddr_t)&sc->cur_scp->kbd_mode);
1401 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1404 error = EPERM; /* XXX */
1411 case CONS_RELKBD: /* release the current keyboard */
1414 if (sc->kbd != NULL) {
1415 save_kbd_state(sc->cur_scp);
1416 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1425 case CONS_GETTERM: /* get the current terminal emulator info */
1429 if (((term_info_t *)data)->ti_index == 0) {
1432 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1435 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1436 sizeof(((term_info_t *)data)->ti_name));
1437 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1438 sizeof(((term_info_t *)data)->ti_desc));
1439 ((term_info_t *)data)->ti_flags = 0;
1442 ((term_info_t *)data)->ti_name[0] = '\0';
1443 ((term_info_t *)data)->ti_desc[0] = '\0';
1444 ((term_info_t *)data)->ti_flags = 0;
1449 case CONS_SETTERM: /* set the current terminal emulator */
1451 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1452 /* FIXME: what if scp == sc_console! XXX */
1456 case GIO_SCRNMAP: /* get output translation table */
1457 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1460 case PIO_SCRNMAP: /* set output translation table */
1461 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1462 for (i=0; i<sizeof(sc->scr_map); i++) {
1463 sc->scr_rmap[sc->scr_map[i]] = i;
1467 case GIO_KEYMAP: /* get keyboard translation table */
1468 case PIO_KEYMAP: /* set keyboard translation table */
1469 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
1470 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1471 case GIO_DEADKEYMAP: /* get accent key translation table */
1472 case PIO_DEADKEYMAP: /* set accent key translation table */
1473 case GETFKEY: /* get function key string */
1474 case SETFKEY: /* set function key string */
1475 error = kbdd_ioctl(sc->kbd, cmd, data);
1476 if (error == ENOIOCTL)
1480 #ifndef SC_NO_FONT_LOADING
1482 case PIO_FONT8x8: /* set 8x8 dot font */
1483 if (!ISFONTAVAIL(sc->adp->va_flags))
1485 bcopy(data, sc->font_8, 8*256);
1486 sc->fonts_loaded |= FONT_8;
1489 * Always use the font page #0. XXX
1490 * Don't load if the current font size is not 8x8.
1492 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1493 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1496 case GIO_FONT8x8: /* get 8x8 dot font */
1497 if (!ISFONTAVAIL(sc->adp->va_flags))
1499 if (sc->fonts_loaded & FONT_8) {
1500 bcopy(sc->font_8, data, 8*256);
1506 case PIO_FONT8x14: /* set 8x14 dot font */
1507 if (!ISFONTAVAIL(sc->adp->va_flags))
1509 bcopy(data, sc->font_14, 14*256);
1510 sc->fonts_loaded |= FONT_14;
1513 * Always use the font page #0. XXX
1514 * Don't load if the current font size is not 8x14.
1516 if (ISTEXTSC(sc->cur_scp)
1517 && (sc->cur_scp->font_size >= 14)
1518 && (sc->cur_scp->font_size < 16))
1519 sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1522 case GIO_FONT8x14: /* get 8x14 dot font */
1523 if (!ISFONTAVAIL(sc->adp->va_flags))
1525 if (sc->fonts_loaded & FONT_14) {
1526 bcopy(sc->font_14, data, 14*256);
1532 case PIO_FONT8x16: /* set 8x16 dot font */
1533 if (!ISFONTAVAIL(sc->adp->va_flags))
1535 bcopy(data, sc->font_16, 16*256);
1536 sc->fonts_loaded |= FONT_16;
1539 * Always use the font page #0. XXX
1540 * Don't load if the current font size is not 8x16.
1542 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1543 sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1546 case GIO_FONT8x16: /* get 8x16 dot font */
1547 if (!ISFONTAVAIL(sc->adp->va_flags))
1549 if (sc->fonts_loaded & FONT_16) {
1550 bcopy(sc->font_16, data, 16*256);
1556 #endif /* SC_NO_FONT_LOADING */
1566 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1570 return sctty_ioctl(dev->si_drv1, cmd, data, td);
1574 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td)
1576 #ifndef SC_NO_SYSMOUSE
1578 memset(&info, 0, sizeof(info));
1579 info.operation = MOUSE_ACTION;
1582 * Make sure all buttons are released when moused and other
1583 * console daemons exit, so that no buttons are left pressed.
1585 (void) sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td);
1591 sc_cnprobe(struct consdev *cp)
1596 if (!vty_enabled(VTY_SC)) {
1597 cp->cn_pri = CN_DEAD;
1601 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1603 /* a video card is always required */
1604 if (!scvidprobe(unit, flags, TRUE))
1605 cp->cn_pri = CN_DEAD;
1607 /* syscons will become console even when there is no keyboard */
1608 sckbdprobe(unit, flags, TRUE);
1610 if (cp->cn_pri == CN_DEAD)
1613 /* initialize required fields */
1614 strcpy(cp->cn_name, "ttyv0");
1618 sc_cninit(struct consdev *cp)
1623 sc_get_cons_priority(&unit, &flags);
1624 scinit(unit, flags | SC_KERNEL_CONSOLE);
1625 sc_console_unit = unit;
1626 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1631 sc_cnterm(struct consdev *cp)
1633 /* we are not the kernel console any more, release everything */
1635 if (sc_console_unit < 0)
1636 return; /* shouldn't happen */
1639 sc_clear_screen(sc_console);
1640 sccnupdate(sc_console);
1643 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1644 sc_console_unit = -1;
1649 sc_cngrab(struct consdev *cp)
1654 sc_console->sc->cur_scp->index != sc_console->index &&
1655 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
1656 sc_console->smode.mode == VT_AUTO)
1657 sc_switch_scr(sc_console->sc, sc_console->index);
1659 scp = sc_console->sc->cur_scp;
1661 if (scp->sc->kbd == NULL)
1664 if (scp->grabbed++ > 0)
1668 * Make sure the keyboard is accessible even when the kbd device
1669 * driver is disabled.
1671 kbdd_enable(scp->sc->kbd);
1673 /* we shall always use the keyboard in the XLATE mode here */
1674 scp->kbd_prev_mode = scp->kbd_mode;
1675 scp->kbd_mode = K_XLATE;
1676 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1678 kbdd_poll(scp->sc->kbd, TRUE);
1682 sc_cnungrab(struct consdev *cp)
1686 scp = sc_console->sc->cur_scp; /* XXX */
1687 if (scp->sc->kbd == NULL)
1690 if (--scp->grabbed > 0)
1693 kbdd_poll(scp->sc->kbd, FALSE);
1695 scp->kbd_mode = scp->kbd_prev_mode;
1696 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1697 kbdd_disable(scp->sc->kbd);
1701 sc_cnputc(struct consdev *cd, int c)
1704 scr_stat *scp = sc_console;
1705 #ifndef SC_NO_HISTORY
1709 #endif /* !SC_NO_HISTORY */
1712 /* assert(sc_console != NULL) */
1714 #ifndef SC_NO_HISTORY
1715 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1716 scp->status &= ~SLKED;
1717 update_kbd_state(scp, scp->status, SLKED);
1718 if (scp->status & BUFFER_SAVED) {
1719 if (!sc_hist_restore(scp))
1720 sc_remove_cutmarking(scp);
1721 scp->status &= ~BUFFER_SAVED;
1722 scp->status |= CURSOR_ENABLED;
1723 sc_draw_cursor_image(scp);
1727 * XXX: Now that TTY's have their own locks, we cannot process
1728 * any data after disabling scroll lock. cnputs already holds a
1731 tp = SC_DEV(scp->sc, scp->index);
1732 /* XXX "tp" can be NULL */
1735 sctty_outwakeup(tp);
1739 #endif /* !SC_NO_HISTORY */
1742 sc_puts(scp, buf, 1, 1);
1744 s = spltty(); /* block sckbdevent and scrn_timer */
1750 sc_cngetc(struct consdev *cd)
1752 static struct fkeytab fkey;
1756 int s = spltty(); /* block sckbdevent and scrn_timer while we poll */
1759 /* assert(sc_console != NULL) */
1762 * Stop the screen saver and update the screen if necessary.
1763 * What if we have been running in the screen saver code... XXX
1765 sc_touch_scrn_saver();
1766 scp = sc_console->sc->cur_scp; /* XXX */
1769 if (fkeycp < fkey.len) {
1771 return fkey.str[fkeycp++];
1774 if (scp->sc->kbd == NULL) {
1779 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK);
1781 switch (KEYFLAGS(c)) {
1782 case 0: /* normal char */
1784 case FKEY: /* function key */
1785 p = (*scp->tsw->te_fkeystr)(scp, c);
1787 fkey.len = strlen(p);
1788 bcopy(p, fkey.str, fkey.len);
1792 p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1794 if ((p != NULL) && (fkey.len > 0)) {
1795 bcopy(p, fkey.str, fkey.len);
1809 sccnupdate(scr_stat *scp)
1811 /* this is a cut-down version of scrn_timer()... */
1813 if (suspend_in_progress || scp->sc->font_loading_in_progress)
1816 if (kdb_active || panicstr || shutdown_in_progress) {
1817 sc_touch_scrn_saver();
1818 } else if (scp != scp->sc->cur_scp) {
1822 if (!run_scrn_saver)
1823 scp->sc->flags &= ~SC_SCRN_IDLE;
1825 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1826 if (scp->sc->flags & SC_SCRN_BLANKED)
1827 stop_scrn_saver(scp->sc, current_saver);
1830 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1831 || scp->sc->switch_in_progress)
1834 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1835 * when write_in_progress is non-zero. XXX
1838 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1839 scrn_update(scp, TRUE);
1843 scrn_timer(void *arg)
1846 static time_t kbd_time_stamp = 0;
1852 again = (arg != NULL);
1854 sc = (sc_softc_t *)arg;
1855 else if (sc_console != NULL)
1856 sc = sc_console->sc;
1860 /* find the vty to update */
1863 /* don't do anything when we are performing some I/O operations */
1864 if (suspend_in_progress || sc->font_loading_in_progress)
1868 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1869 /* try to allocate a keyboard automatically */
1870 if (kbd_time_stamp != time_uptime) {
1871 kbd_time_stamp = time_uptime;
1872 sc->keyboard = sc_allocate_keyboard(sc, -1);
1873 if (sc->keyboard >= 0) {
1874 sc->kbd = kbd_get_keyboard(sc->keyboard);
1875 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1876 (caddr_t)&sc->cur_scp->kbd_mode);
1877 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1884 /* should we stop the screen saver? */
1885 if (kdb_active || panicstr || shutdown_in_progress)
1886 sc_touch_scrn_saver();
1887 if (run_scrn_saver) {
1888 if (time_uptime > sc->scrn_time_stamp + scrn_blank_time)
1889 sc->flags |= SC_SCRN_IDLE;
1891 sc->flags &= ~SC_SCRN_IDLE;
1893 sc->scrn_time_stamp = time_uptime;
1894 sc->flags &= ~SC_SCRN_IDLE;
1895 if (scrn_blank_time > 0)
1896 run_scrn_saver = TRUE;
1899 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1900 if (sc->flags & SC_SCRN_BLANKED)
1901 stop_scrn_saver(sc, current_saver);
1904 /* should we just return ? */
1905 if (sc->blink_in_progress || sc->switch_in_progress
1906 || sc->write_in_progress)
1909 /* Update the screen */
1910 scp = sc->cur_scp; /* cur_scp may have changed... */
1911 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1912 scrn_update(scp, TRUE);
1915 /* should we activate the screen saver? */
1916 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1917 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1918 (*current_saver)(sc, TRUE);
1924 * Use reduced "refresh" rate if we are in graphics and that is not a
1925 * graphical screen saver. In such case we just have nothing to do.
1927 if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1931 callout_reset_sbt(&sc->ctimeout, SBT_1S / rate, 0,
1932 scrn_timer, sc, C_PREL(1));
1937 and_region(int *s1, int *e1, int s2, int e2)
1939 if (*e1 < s2 || e2 < *s1)
1941 *s1 = imax(*s1, s2);
1942 *e1 = imin(*e1, e2);
1947 scrn_update(scr_stat *scp, int show_cursor)
1954 /* assert(scp == scp->sc->cur_scp) */
1956 SC_VIDEO_LOCK(scp->sc);
1958 #ifndef SC_NO_CUTPASTE
1959 /* remove the previous mouse pointer image if necessary */
1960 if (scp->status & MOUSE_VISIBLE) {
1962 e = scp->mouse_pos + scp->xsize + 1;
1963 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1964 || and_region(&s, &e, scp->start, scp->end)
1965 || ((scp->status & CURSOR_ENABLED) &&
1966 (scp->cursor_pos != scp->cursor_oldpos) &&
1967 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1968 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1969 sc_remove_mouse_image(scp);
1970 if (scp->end >= scp->xsize*scp->ysize)
1971 scp->end = scp->xsize*scp->ysize - 1;
1974 #endif /* !SC_NO_CUTPASTE */
1978 if (scp->end >= scp->xsize*scp->ysize) {
1979 printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1980 scp->end = scp->xsize*scp->ysize - 1;
1982 if (scp->start < 0) {
1983 printf("scrn_update(): scp->start %d < 0\n", scp->start);
1988 /* update screen image */
1989 if (scp->start <= scp->end) {
1990 if (scp->mouse_cut_end >= 0) {
1991 /* there is a marked region for cut & paste */
1992 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1993 start = scp->mouse_cut_start;
1994 end = scp->mouse_cut_end;
1996 start = scp->mouse_cut_end;
1997 end = scp->mouse_cut_start - 1;
2001 /* does the cut-mark region overlap with the update region? */
2002 if (and_region(&s, &e, scp->start, scp->end)) {
2003 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2006 if (and_region(&s, &e, scp->start, scp->end))
2007 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2009 e = scp->xsize*scp->ysize - 1;
2010 if (and_region(&s, &e, scp->start, scp->end))
2011 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2013 (*scp->rndr->draw)(scp, scp->start,
2014 scp->end - scp->start + 1, FALSE);
2017 (*scp->rndr->draw)(scp, scp->start,
2018 scp->end - scp->start + 1, FALSE);
2022 /* we are not to show the cursor and the mouse pointer... */
2025 scp->start = scp->xsize*scp->ysize - 1;
2026 SC_VIDEO_UNLOCK(scp->sc);
2030 /* update cursor image */
2031 if (scp->status & CURSOR_ENABLED) {
2034 /* did cursor move since last time ? */
2035 if (scp->cursor_pos != scp->cursor_oldpos) {
2036 /* do we need to remove old cursor image ? */
2037 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2038 sc_remove_cursor_image(scp);
2039 sc_draw_cursor_image(scp);
2041 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
2042 /* cursor didn't move, but has been overwritten */
2043 sc_draw_cursor_image(scp);
2044 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
2045 /* if it's a blinking cursor, update it */
2046 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2047 sc_inside_cutmark(scp,
2052 #ifndef SC_NO_CUTPASTE
2053 /* update "pseudo" mouse pointer image */
2054 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2055 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2056 scp->status &= ~MOUSE_MOVED;
2057 sc_draw_mouse_image(scp);
2060 #endif /* SC_NO_CUTPASTE */
2063 scp->start = scp->xsize*scp->ysize - 1;
2065 SC_VIDEO_UNLOCK(scp->sc);
2070 scsplash_callback(int event, void *arg)
2075 sc = (sc_softc_t *)arg;
2079 if (add_scrn_saver(scsplash_saver) == 0) {
2080 sc->flags &= ~SC_SAVER_FAILED;
2081 run_scrn_saver = TRUE;
2082 if (cold && !(boothowto & RB_VERBOSE)) {
2083 scsplash_stick(TRUE);
2084 (*current_saver)(sc, TRUE);
2090 if (current_saver == scsplash_saver) {
2091 scsplash_stick(FALSE);
2092 error = remove_scrn_saver(scsplash_saver);
2104 scsplash_saver(sc_softc_t *sc, int show)
2106 static int busy = FALSE;
2115 if (!(sc->flags & SC_SAVER_FAILED)) {
2116 if (!(sc->flags & SC_SCRN_BLANKED))
2117 set_scrn_saver_mode(scp, -1, NULL, 0);
2118 switch (splash(sc->adp, TRUE)) {
2119 case 0: /* succeeded */
2121 case EAGAIN: /* try later */
2122 restore_scrn_saver_mode(scp, FALSE);
2123 sc_touch_scrn_saver(); /* XXX */
2126 sc->flags |= SC_SAVER_FAILED;
2127 scsplash_stick(FALSE);
2128 restore_scrn_saver_mode(scp, TRUE);
2129 printf("scsplash_saver(): failed to put up the image\n");
2133 } else if (!sticky_splash) {
2134 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2135 restore_scrn_saver_mode(scp, TRUE);
2141 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2146 if (current_saver != none_saver) {
2147 error = remove_scrn_saver(current_saver);
2152 if (current_saver != none_saver)
2155 run_scrn_saver = FALSE;
2156 saver_mode = CONS_LKM_SAVER;
2157 current_saver = this_saver;
2162 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2164 if (current_saver != this_saver)
2169 * In order to prevent `current_saver' from being called by
2170 * the timeout routine `scrn_timer()' while we manipulate
2171 * the saver list, we shall set `current_saver' to `none_saver'
2172 * before stopping the current saver, rather than blocking by `splXX()'.
2174 current_saver = none_saver;
2176 stop_scrn_saver(this_saver);
2179 /* unblank all blanked screens */
2180 wait_scrn_saver_stop(NULL);
2184 current_saver = none_saver;
2189 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2193 /* assert(scp == scp->sc->cur_scp) */
2195 if (!ISGRAPHSC(scp))
2196 sc_remove_cursor_image(scp);
2197 scp->splash_save_mode = scp->mode;
2198 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2199 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2200 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2201 scp->sc->flags |= SC_SCRN_BLANKED;
2207 if (set_mode(scp) == 0) {
2208 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2209 scp->status |= GRAPHICS_MODE;
2210 #ifndef SC_NO_PALETTE_LOADING
2212 vidd_load_palette(scp->sc->adp, pal);
2214 sc_set_border(scp, border);
2218 scp->mode = scp->splash_save_mode;
2219 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2220 scp->status |= scp->splash_save_status;
2227 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2233 /* assert(scp == scp->sc->cur_scp) */
2236 status = scp->status;
2237 scp->mode = scp->splash_save_mode;
2238 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2239 scp->status |= scp->splash_save_status;
2240 scp->sc->flags &= ~SC_SCRN_BLANKED;
2242 if (!ISGRAPHSC(scp))
2243 sc_draw_cursor_image(scp);
2248 if (set_mode(scp) == 0) {
2249 #ifndef SC_NO_PALETTE_LOADING
2250 #ifdef SC_PIXEL_MODE
2251 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2252 vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2255 vidd_load_palette(scp->sc->adp, scp->sc->palette);
2262 scp->status = status;
2269 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2271 (*saver)(sc, FALSE);
2272 run_scrn_saver = FALSE;
2273 /* the screen saver may have chosen not to stop after all... */
2274 if (sc->flags & SC_SCRN_BLANKED)
2277 mark_all(sc->cur_scp);
2278 if (sc->delayed_next_scr)
2279 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2281 wakeup(&scrn_blanked);
2285 wait_scrn_saver_stop(sc_softc_t *sc)
2289 while (scrn_blanked > 0) {
2290 run_scrn_saver = FALSE;
2291 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2295 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2296 if ((error != 0) && (error != ERESTART))
2299 run_scrn_saver = FALSE;
2302 #endif /* DEV_SPLASH */
2305 sc_touch_scrn_saver(void)
2307 scsplash_stick(FALSE);
2308 run_scrn_saver = FALSE;
2312 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2319 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2321 if (sc->cur_scp == NULL)
2324 /* prevent switch if previously requested */
2325 if (sc->flags & SC_SCRN_VTYLOCK) {
2326 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2327 sc->cur_scp->bell_duration);
2331 /* delay switch if the screen is blanked or being updated */
2332 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2333 || sc->blink_in_progress) {
2334 sc->delayed_next_scr = next_scr + 1;
2335 sc_touch_scrn_saver();
2336 DPRINTF(5, ("switch delayed\n"));
2339 sc->delayed_next_scr = 0;
2342 cur_scp = sc->cur_scp;
2344 /* we are in the middle of the vty switching process... */
2345 if (sc->switch_in_progress
2346 && (cur_scp->smode.mode == VT_PROCESS)
2348 p = pfind(cur_scp->pid);
2349 if (cur_scp->proc != p) {
2353 * The controlling process has died!!. Do some clean up.
2354 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2355 * are not reset here yet; they will be cleared later.
2357 DPRINTF(5, ("cur_scp controlling process %d died, ",
2359 if (cur_scp->status & SWITCH_WAIT_REL) {
2361 * Force the previous switch to finish, but return now
2364 DPRINTF(5, ("reset WAIT_REL, "));
2365 finish_vt_rel(cur_scp, TRUE, &s);
2367 DPRINTF(5, ("finishing previous switch\n"));
2369 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2370 /* let's assume screen switch has been completed. */
2371 DPRINTF(5, ("reset WAIT_ACQ, "));
2372 finish_vt_acq(cur_scp);
2375 * We are in between screen release and acquisition, and
2376 * reached here via scgetc() or scrn_timer() which has
2377 * interrupted exchange_scr(). Don't do anything stupid.
2379 DPRINTF(5, ("waiting nothing, "));
2385 * The controlling process is alive, but not responding...
2386 * It is either buggy or it may be just taking time.
2387 * The following code is a gross kludge to cope with this
2388 * problem for which there is no clean solution. XXX
2390 if (cur_scp->status & SWITCH_WAIT_REL) {
2391 switch (sc->switch_in_progress++) {
2395 DPRINTF(5, ("sending relsig again, "));
2396 signal_vt_rel(cur_scp);
2403 * Act as if the controlling program returned
2406 DPRINTF(5, ("force reset WAIT_REL, "));
2407 finish_vt_rel(cur_scp, FALSE, &s);
2409 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2412 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2413 switch (sc->switch_in_progress++) {
2417 DPRINTF(5, ("sending acqsig again, "));
2418 signal_vt_acq(cur_scp);
2424 /* clear the flag and finish the previous switch */
2425 DPRINTF(5, ("force reset WAIT_ACQ, "));
2426 finish_vt_acq(cur_scp);
2434 * Return error if an invalid argument is given, or vty switch
2435 * is still in progress.
2437 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2438 || sc->switch_in_progress) {
2440 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2441 DPRINTF(5, ("error 1\n"));
2446 * Don't allow switching away from the graphics mode vty
2447 * if the switch mode is VT_AUTO, unless the next vty is the same
2448 * as the current or the current vty has been closed (but showing).
2450 tp = SC_DEV(sc, cur_scp->index);
2451 if ((cur_scp->index != next_scr)
2452 && tty_opened_ns(tp)
2453 && (cur_scp->smode.mode == VT_AUTO)
2454 && ISGRAPHSC(cur_scp)) {
2456 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2457 DPRINTF(5, ("error, graphics mode\n"));
2462 * Is the wanted vty open? Don't allow switching to a closed vty.
2463 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2464 * Note that we always allow the user to switch to the kernel
2465 * console even if it is closed.
2467 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2468 tp = SC_DEV(sc, next_scr);
2469 if (!tty_opened_ns(tp)) {
2471 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2472 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2475 if (kdb_active && SC_STAT(tp)->smode.mode == VT_PROCESS) {
2477 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2482 /* this is the start of vty switching process... */
2483 ++sc->switch_in_progress;
2484 sc->old_scp = cur_scp;
2485 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2486 if (sc->new_scp == sc->old_scp) {
2487 sc->switch_in_progress = 0;
2489 * XXX wakeup() locks the scheduler lock which will hang if
2490 * the lock is in an in-between state, e.g., when we stop at
2491 * a breakpoint at fork_exit. It has always been wrong to call
2492 * wakeup() when the debugger is active. In RELENG_4, wakeup()
2493 * is supposed to be locked by splhigh(), but the debugger may
2494 * be invoked at splhigh().
2497 wakeup(VTY_WCHAN(sc,next_scr));
2499 DPRINTF(5, ("switch done (new == old)\n"));
2503 /* has controlling process died? */
2504 vt_proc_alive(sc->old_scp);
2505 vt_proc_alive(sc->new_scp);
2507 /* wait for the controlling process to release the screen, if necessary */
2508 if (signal_vt_rel(sc->old_scp)) {
2513 /* go set up the new vty screen */
2518 /* wake up processes waiting for this vty */
2520 wakeup(VTY_WCHAN(sc,next_scr));
2522 /* wait for the controlling process to acknowledge, if necessary */
2523 if (signal_vt_acq(sc->cur_scp)) {
2528 sc->switch_in_progress = 0;
2529 if (sc->unit == sc_console_unit)
2530 cnavailable(sc_consptr, TRUE);
2532 DPRINTF(5, ("switch done\n"));
2538 do_switch_scr(sc_softc_t *sc, int s)
2540 vt_proc_alive(sc->new_scp);
2545 /* sc->cur_scp == sc->new_scp */
2546 wakeup(VTY_WCHAN(sc,sc->cur_scp->index));
2548 /* wait for the controlling process to acknowledge, if necessary */
2549 if (!signal_vt_acq(sc->cur_scp)) {
2550 sc->switch_in_progress = 0;
2551 if (sc->unit == sc_console_unit)
2552 cnavailable(sc_consptr, TRUE);
2559 vt_proc_alive(scr_stat *scp)
2564 if ((p = pfind(scp->pid)) != NULL)
2569 scp->smode.mode = VT_AUTO;
2570 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2576 signal_vt_rel(scr_stat *scp)
2578 if (scp->smode.mode != VT_PROCESS)
2580 scp->status |= SWITCH_WAIT_REL;
2581 PROC_LOCK(scp->proc);
2582 kern_psignal(scp->proc, scp->smode.relsig);
2583 PROC_UNLOCK(scp->proc);
2584 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2589 signal_vt_acq(scr_stat *scp)
2591 if (scp->smode.mode != VT_PROCESS)
2593 if (scp->sc->unit == sc_console_unit)
2594 cnavailable(sc_consptr, FALSE);
2595 scp->status |= SWITCH_WAIT_ACQ;
2596 PROC_LOCK(scp->proc);
2597 kern_psignal(scp->proc, scp->smode.acqsig);
2598 PROC_UNLOCK(scp->proc);
2599 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2604 finish_vt_rel(scr_stat *scp, int release, int *s)
2606 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2607 scp->status &= ~SWITCH_WAIT_REL;
2609 *s = do_switch_scr(scp->sc, *s);
2611 scp->sc->switch_in_progress = 0;
2618 finish_vt_acq(scr_stat *scp)
2620 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2621 scp->status &= ~SWITCH_WAIT_ACQ;
2622 scp->sc->switch_in_progress = 0;
2629 exchange_scr(sc_softc_t *sc)
2633 /* save the current state of video and keyboard */
2634 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2635 if (!ISGRAPHSC(sc->old_scp))
2636 sc_remove_cursor_image(sc->old_scp);
2637 if (sc->old_scp->kbd_mode == K_XLATE)
2638 save_kbd_state(sc->old_scp);
2640 /* set up the video for the new screen */
2641 scp = sc->cur_scp = sc->new_scp;
2643 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp))
2645 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2650 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2651 (void *)sc->adp->va_window, FALSE);
2653 scp->status |= MOUSE_HIDDEN;
2654 sc_move_cursor(scp, scp->xpos, scp->ypos);
2655 if (!ISGRAPHSC(scp))
2656 sc_set_cursor_image(scp);
2657 #ifndef SC_NO_PALETTE_LOADING
2658 if (ISGRAPHSC(sc->old_scp)) {
2659 #ifdef SC_PIXEL_MODE
2660 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2661 vidd_load_palette(sc->adp, sc->palette2);
2664 vidd_load_palette(sc->adp, sc->palette);
2667 sc_set_border(scp, scp->border);
2669 /* set up the keyboard for the new screen */
2670 if (sc->old_scp->kbd_mode != scp->kbd_mode)
2671 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2672 update_kbd_state(scp, scp->status, LOCK_MASK);
2678 sc_puts(scr_stat *scp, u_char *buf, int len, int kernel)
2680 int need_unlock = 0;
2683 /* make screensaver happy */
2684 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2685 run_scrn_saver = FALSE;
2689 if (!kdb_active && !mtx_owned(&scp->sc->scr_lock)) {
2691 mtx_lock_spin(&scp->sc->scr_lock);
2693 (*scp->tsw->te_puts)(scp, buf, len, kernel);
2695 mtx_unlock_spin(&scp->sc->scr_lock);
2698 if (scp->sc->delayed_next_scr)
2699 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2703 sc_draw_cursor_image(scr_stat *scp)
2705 /* assert(scp == scp->sc->cur_scp); */
2706 SC_VIDEO_LOCK(scp->sc);
2707 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2708 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2709 sc_inside_cutmark(scp, scp->cursor_pos));
2710 scp->cursor_oldpos = scp->cursor_pos;
2711 SC_VIDEO_UNLOCK(scp->sc);
2715 sc_remove_cursor_image(scr_stat *scp)
2717 /* assert(scp == scp->sc->cur_scp); */
2718 SC_VIDEO_LOCK(scp->sc);
2719 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2720 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2721 sc_inside_cutmark(scp, scp->cursor_oldpos));
2722 SC_VIDEO_UNLOCK(scp->sc);
2726 update_cursor_image(scr_stat *scp)
2728 /* assert(scp == scp->sc->cur_scp); */
2729 sc_remove_cursor_image(scp);
2730 sc_set_cursor_image(scp);
2731 sc_draw_cursor_image(scp);
2735 sc_set_cursor_image(scr_stat *scp)
2737 scp->curs_attr.flags = scp->curr_curs_attr.flags;
2738 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2739 /* hidden cursor is internally represented as zero-height underline */
2740 scp->curs_attr.flags = CONS_CHAR_CURSOR;
2741 scp->curs_attr.base = scp->curs_attr.height = 0;
2742 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2743 scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2744 scp->font_size - 1);
2745 scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2746 scp->font_size - scp->curs_attr.base);
2747 } else { /* block cursor */
2748 scp->curs_attr.base = 0;
2749 scp->curs_attr.height = scp->font_size;
2752 /* assert(scp == scp->sc->cur_scp); */
2753 SC_VIDEO_LOCK(scp->sc);
2754 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2755 scp->curs_attr.flags & CONS_BLINK_CURSOR);
2756 SC_VIDEO_UNLOCK(scp->sc);
2760 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2762 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2763 sc_remove_cursor_image(scp);
2766 scp->curr_curs_attr.base = base;
2768 scp->curr_curs_attr.height = height;
2769 if (flags & CONS_RESET_CURSOR)
2770 scp->curr_curs_attr = scp->dflt_curs_attr;
2772 scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2774 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2775 sc_set_cursor_image(scp);
2776 sc_draw_cursor_image(scp);
2781 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2789 if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2790 /* local (per vty) change */
2791 change_cursor_shape(scp, flags, base, height);
2799 sc->curs_attr.base = base;
2801 sc->curs_attr.height = height;
2803 if (flags & CONS_RESET_CURSOR)
2804 sc->curs_attr = sc->dflt_curs_attr;
2806 sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2809 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2810 if ((tp = SC_DEV(sc, i)) == NULL)
2812 if ((scp = sc_get_stat(tp)) == NULL)
2814 scp->dflt_curs_attr = sc->curs_attr;
2815 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2821 scinit(int unit, int flags)
2825 * When syscons is being initialized as the kernel console, malloc()
2826 * is not yet functional, because various kernel structures has not been
2827 * fully initialized yet. Therefore, we need to declare the following
2828 * static buffers for the console. This is less than ideal,
2829 * but is necessry evil for the time being. XXX
2832 static u_short sc_buffer[ROW*COL*2];/* XXX */
2834 static u_short sc_buffer[ROW*COL]; /* XXX */
2836 #ifndef SC_NO_FONT_LOADING
2837 static u_char font_8[256*8];
2838 static u_char font_14[256*14];
2839 static u_char font_16[256*16];
2844 video_adapter_t *adp;
2849 /* one time initialization */
2850 if (init_done == COLD)
2851 sc_get_bios_values(&bios_value);
2855 * Allocate resources. Even if we are being called for the second
2856 * time, we must allocate them again, because they might have
2859 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2860 if ((sc->flags & SC_INIT_DONE) == 0) {
2861 mtx_init(&sc->scr_lock, "scrlock", NULL, MTX_SPIN);
2862 SC_VIDEO_LOCKINIT(sc);
2866 if (sc->adapter >= 0) {
2867 vid_release(sc->adp, (void *)&sc->adapter);
2871 if (sc->keyboard >= 0) {
2872 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2873 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2874 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2875 if (sc->kbd != NULL) {
2876 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2877 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2881 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2882 sc->adp = vid_get_adapter(sc->adapter);
2883 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2885 sc->keyboard = sc_allocate_keyboard(sc, unit);
2886 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2888 sc->kbd = kbd_get_keyboard(sc->keyboard);
2889 if (sc->kbd != NULL) {
2890 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2891 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2894 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2896 sc->initial_mode = sc->adp->va_initial_mode;
2898 #ifndef SC_NO_FONT_LOADING
2899 if (flags & SC_KERNEL_CONSOLE) {
2900 sc->font_8 = font_8;
2901 sc->font_14 = font_14;
2902 sc->font_16 = font_16;
2903 } else if (sc->font_8 == NULL) {
2904 /* assert(sc_malloc) */
2905 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2906 sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2907 sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2911 /* extract the hardware cursor location and hide the cursor for now */
2912 vidd_read_hw_cursor(sc->adp, &col, &row);
2913 vidd_set_hw_cursor(sc->adp, -1, -1);
2915 /* set up the first console */
2916 sc->first_vty = unit*MAXCONS;
2917 sc->vtys = MAXCONS; /* XXX: should be configurable */
2918 if (flags & SC_KERNEL_CONSOLE) {
2920 * Set up devs structure but don't use it yet, calling make_dev()
2921 * might panic kernel. Wait for sc_attach_unit() to actually
2922 * create the devices.
2924 sc->dev = main_devs;
2925 scp = &main_console;
2926 init_scp(sc, sc->first_vty, scp);
2927 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2928 (void *)sc_buffer, FALSE);
2930 /* move cursors to the initial positions */
2931 if (col >= scp->xsize)
2933 if (row >= scp->ysize)
2934 row = scp->ysize - 1;
2937 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2939 if (sc_init_emulator(scp, SC_DFLT_TERM))
2940 sc_init_emulator(scp, "*");
2941 (*scp->tsw->te_default_attr)(scp,
2942 user_default.std_color,
2943 user_default.rev_color);
2945 /* assert(sc_malloc) */
2946 sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF,
2948 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
2949 scp = alloc_scp(sc, sc->first_vty);
2950 SC_STAT(sc->dev[0]) = scp;
2955 /* copy screen to temporary buffer */
2956 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2957 (void *)scp->sc->adp->va_window, FALSE);
2959 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2962 if (bios_value.cursor_end < scp->font_size)
2963 sc->dflt_curs_attr.base = scp->font_size -
2964 bios_value.cursor_end - 1;
2966 sc->dflt_curs_attr.base = 0;
2967 i = bios_value.cursor_end - bios_value.cursor_start + 1;
2968 sc->dflt_curs_attr.height = imin(i, scp->font_size);
2969 sc->dflt_curs_attr.flags = 0;
2970 sc->curs_attr = sc->dflt_curs_attr;
2971 scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2973 #ifndef SC_NO_SYSMOUSE
2974 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2976 if (!ISGRAPHSC(scp)) {
2977 sc_set_cursor_image(scp);
2978 sc_draw_cursor_image(scp);
2981 /* save font and palette */
2982 #ifndef SC_NO_FONT_LOADING
2983 sc->fonts_loaded = 0;
2984 if (ISFONTAVAIL(sc->adp->va_flags)) {
2986 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2987 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2988 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2989 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2990 if (scp->font_size < 14) {
2991 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2992 } else if (scp->font_size >= 16) {
2993 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2995 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2997 #else /* !SC_DFLT_FONT */
2998 if (scp->font_size < 14) {
2999 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3000 sc->fonts_loaded = FONT_8;
3001 } else if (scp->font_size >= 16) {
3002 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3003 sc->fonts_loaded = FONT_16;
3005 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3006 sc->fonts_loaded = FONT_14;
3008 #endif /* SC_DFLT_FONT */
3009 /* FONT KLUDGE: always use the font page #0. XXX */
3010 sc_show_font(scp, 0);
3012 #endif /* !SC_NO_FONT_LOADING */
3014 #ifndef SC_NO_PALETTE_LOADING
3015 vidd_save_palette(sc->adp, sc->palette);
3016 #ifdef SC_PIXEL_MODE
3017 for (i = 0; i < sizeof(sc->palette2); i++)
3018 sc->palette2[i] = i / 3;
3023 if (!(sc->flags & SC_SPLASH_SCRN)) {
3024 /* we are ready to put up the splash image! */
3025 splash_init(sc->adp, scsplash_callback, sc);
3026 sc->flags |= SC_SPLASH_SCRN;
3031 /* the rest is not necessary, if we have done it once */
3032 if (sc->flags & SC_INIT_DONE)
3035 /* initialize mapscrn arrays to a one to one map */
3036 for (i = 0; i < sizeof(sc->scr_map); i++)
3037 sc->scr_map[i] = sc->scr_rmap[i] = i;
3039 sc->scr_map[0x5c] = (u_char)0xfc; /* for backslash */
3042 sc->flags |= SC_INIT_DONE;
3046 scterm(int unit, int flags)
3051 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3053 return; /* shouldn't happen */
3056 /* this console is no longer available for the splash screen */
3057 if (sc->flags & SC_SPLASH_SCRN) {
3058 splash_term(sc->adp);
3059 sc->flags &= ~SC_SPLASH_SCRN;
3064 /* move the hardware cursor to the upper-left corner */
3065 vidd_set_hw_cursor(sc->adp, 0, 0);
3068 /* release the keyboard and the video card */
3069 if (sc->keyboard >= 0)
3070 kbd_release(sc->kbd, &sc->keyboard);
3071 if (sc->adapter >= 0)
3072 vid_release(sc->adp, &sc->adapter);
3074 /* stop the terminal emulator, if any */
3075 scp = sc_get_stat(sc->dev[0]);
3077 (*scp->tsw->te_term)(scp, &scp->ts);
3078 if (scp->ts != NULL)
3079 free(scp->ts, M_DEVBUF);
3080 mtx_destroy(&sc->scr_lock);
3081 mtx_destroy(&sc->video_mtx);
3083 /* clear the structure */
3084 if (!(flags & SC_KERNEL_CONSOLE)) {
3085 /* XXX: We need delete_dev() for this */
3086 free(sc->dev, M_DEVBUF);
3088 /* XXX: We need a ttyunregister for this */
3089 free(sc->tty, M_DEVBUF);
3091 #ifndef SC_NO_FONT_LOADING
3092 free(sc->font_8, M_DEVBUF);
3093 free(sc->font_14, M_DEVBUF);
3094 free(sc->font_16, M_DEVBUF);
3096 /* XXX vtb, history */
3098 bzero(sc, sizeof(*sc));
3104 scshutdown(__unused void *arg, __unused int howto)
3107 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3108 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3109 KASSERT(sc_console->sc->cur_scp != NULL,
3110 ("sc_console->sc->cur_scp != NULL"));
3112 sc_touch_scrn_saver();
3114 sc_console->sc->cur_scp->index != sc_console->index &&
3115 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3116 sc_console->smode.mode == VT_AUTO)
3117 sc_switch_scr(sc_console->sc, sc_console->index);
3118 shutdown_in_progress = TRUE;
3122 scsuspend(__unused void *arg)
3126 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3127 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3128 KASSERT(sc_console->sc->cur_scp != NULL,
3129 ("sc_console->sc->cur_scp != NULL"));
3131 sc_susp_scr = sc_console->sc->cur_scp->index;
3132 if (sc_no_suspend_vtswitch ||
3133 sc_susp_scr == sc_console->index) {
3134 sc_touch_scrn_saver();
3138 for (retry = 0; retry < 10; retry++) {
3139 sc_switch_scr(sc_console->sc, sc_console->index);
3140 if (!sc_console->sc->switch_in_progress)
3142 pause("scsuspend", hz);
3144 suspend_in_progress = TRUE;
3148 scresume(__unused void *arg)
3151 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3152 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3153 KASSERT(sc_console->sc->cur_scp != NULL,
3154 ("sc_console->sc->cur_scp != NULL"));
3156 suspend_in_progress = FALSE;
3157 if (sc_susp_scr < 0) {
3158 update_font(sc_console->sc->cur_scp);
3161 sc_switch_scr(sc_console->sc, sc_susp_scr);
3165 sc_clean_up(scr_stat *scp)
3171 if (scp->sc->flags & SC_SCRN_BLANKED) {
3172 sc_touch_scrn_saver();
3174 if ((error = wait_scrn_saver_stop(scp->sc)))
3178 scp->status |= MOUSE_HIDDEN;
3179 sc_remove_mouse_image(scp);
3180 sc_remove_cutmarking(scp);
3185 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3191 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3192 if (!discard && (old.vtb_flags & VTB_VALID)) {
3193 /* retain the current cursor position and buffer contants */
3194 scp->cursor_oldpos = scp->cursor_pos;
3196 * This works only if the old buffer has the same size as or larger
3197 * than the new one. XXX
3199 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3203 sc_vtb_destroy(&old);
3206 #ifndef SC_NO_SYSMOUSE
3207 /* move the mouse cursor at the center of the screen */
3208 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3213 *alloc_scp(sc_softc_t *sc, int vty)
3217 /* assert(sc_malloc) */
3219 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3220 init_scp(sc, vty, scp);
3222 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3223 if (sc_init_emulator(scp, SC_DFLT_TERM))
3224 sc_init_emulator(scp, "*");
3226 #ifndef SC_NO_CUTPASTE
3227 sc_alloc_cut_buffer(scp, TRUE);
3230 #ifndef SC_NO_HISTORY
3231 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3238 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3242 bzero(scp, sizeof(*scp));
3247 scp->mode = sc->initial_mode;
3248 vidd_get_info(sc->adp, scp->mode, &info);
3249 if (info.vi_flags & V_INFO_GRAPHICS) {
3250 scp->status |= GRAPHICS_MODE;
3251 scp->xpixel = info.vi_width;
3252 scp->ypixel = info.vi_height;
3253 scp->xsize = info.vi_width/info.vi_cwidth;
3254 scp->ysize = info.vi_height/info.vi_cheight;
3258 scp->xsize = info.vi_width;
3259 scp->ysize = info.vi_height;
3260 scp->xpixel = scp->xsize*info.vi_cwidth;
3261 scp->ypixel = scp->ysize*info.vi_cheight;
3264 scp->font_size = info.vi_cheight;
3265 scp->font_width = info.vi_cwidth;
3266 #ifndef SC_NO_FONT_LOADING
3267 if (info.vi_cheight < 14)
3268 scp->font = sc->font_8;
3269 else if (info.vi_cheight >= 16)
3270 scp->font = sc->font_16;
3272 scp->font = sc->font_14;
3277 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3279 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3281 scp->xoff = scp->yoff = 0;
3282 scp->xpos = scp->ypos = 0;
3283 scp->start = scp->xsize * scp->ysize - 1;
3288 scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3289 scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3290 scp->mouse_cut_start = scp->xsize*scp->ysize;
3291 scp->mouse_cut_end = -1;
3292 scp->mouse_signal = 0;
3294 scp->mouse_proc = NULL;
3295 scp->kbd_mode = K_XLATE;
3296 scp->bell_pitch = bios_value.bell_pitch;
3297 scp->bell_duration = BELL_DURATION;
3298 scp->status |= (bios_value.shift_state & NLKED);
3299 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3302 scp->smode.mode = VT_AUTO;
3303 scp->history = NULL;
3304 scp->history_pos = 0;
3305 scp->history_size = 0;
3309 sc_init_emulator(scr_stat *scp, char *name)
3316 if (name == NULL) /* if no name is given, use the current emulator */
3318 else /* ...otherwise find the named emulator */
3319 sw = sc_term_match(name);
3324 if (strcmp(sw->te_renderer, "*") != 0) {
3325 rndr = sc_render_match(scp, sw->te_renderer,
3326 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3329 rndr = sc_render_match(scp, scp->sc->adp->va_name,
3330 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3335 if (sw == scp->tsw) {
3336 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3338 scp->rndr->init(scp);
3339 sc_clear_screen(scp);
3340 /* assert(error == 0); */
3344 if (sc_malloc && (sw->te_size > 0))
3345 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3348 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3353 (*scp->tsw->te_term)(scp, &scp->ts);
3354 if (scp->ts != NULL)
3355 free(scp->ts, M_DEVBUF);
3359 scp->rndr->init(scp);
3362 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3363 sc_clear_screen(scp);
3369 * scgetc(flags) - get character from keyboard.
3370 * If flags & SCGETC_CN, then avoid harmful side effects.
3371 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3372 * return NOKEY if there is nothing there.
3375 scgetc(sc_softc_t *sc, u_int flags)
3378 #ifndef SC_NO_HISTORY
3386 if (sc->kbd == NULL)
3391 /* I don't like this, but... XXX */
3392 if (flags & SCGETC_CN)
3393 sccnupdate(sc->cur_scp);
3396 /* first see if there is something in the keyboard port */
3398 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3400 if (!(flags & SCGETC_CN))
3401 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3402 } else if (c == NOKEY)
3408 /* make screensaver happy */
3410 sc_touch_scrn_saver();
3412 if (!(flags & SCGETC_CN))
3413 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
3415 if (scp->kbd_mode != K_XLATE)
3418 /* if scroll-lock pressed allow history browsing */
3419 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3421 scp->status &= ~CURSOR_ENABLED;
3422 sc_remove_cursor_image(scp);
3424 #ifndef SC_NO_HISTORY
3425 if (!(scp->status & BUFFER_SAVED)) {
3426 scp->status |= BUFFER_SAVED;
3430 /* FIXME: key codes */
3431 case SPCLKEY | FKEY | F(49): /* home key */
3432 sc_remove_cutmarking(scp);
3436 case SPCLKEY | FKEY | F(57): /* end key */
3437 sc_remove_cutmarking(scp);
3441 case SPCLKEY | FKEY | F(50): /* up arrow key */
3442 sc_remove_cutmarking(scp);
3443 if (sc_hist_up_line(scp))
3444 if (!(flags & SCGETC_CN))
3445 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3448 case SPCLKEY | FKEY | F(58): /* down arrow key */
3449 sc_remove_cutmarking(scp);
3450 if (sc_hist_down_line(scp))
3451 if (!(flags & SCGETC_CN))
3452 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3455 case SPCLKEY | FKEY | F(51): /* page up key */
3456 sc_remove_cutmarking(scp);
3457 for (i=0; i<scp->ysize; i++)
3458 if (sc_hist_up_line(scp)) {
3459 if (!(flags & SCGETC_CN))
3460 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3465 case SPCLKEY | FKEY | F(59): /* page down key */
3466 sc_remove_cutmarking(scp);
3467 for (i=0; i<scp->ysize; i++)
3468 if (sc_hist_down_line(scp)) {
3469 if (!(flags & SCGETC_CN))
3470 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3475 #endif /* SC_NO_HISTORY */
3479 * Process and consume special keys here. Return a plain char code
3480 * or a char code with the META flag or a function key code.
3484 /* goto next_code */
3489 switch (KEYCHAR(c)) {
3491 case NLK: case CLK: case ALK:
3494 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3496 scp->status |= SLKED;
3498 if (scp->status & SLKED) {
3499 scp->status &= ~SLKED;
3500 #ifndef SC_NO_HISTORY
3501 if (scp->status & BUFFER_SAVED) {
3502 if (!sc_hist_restore(scp))
3503 sc_remove_cutmarking(scp);
3504 scp->status &= ~BUFFER_SAVED;
3505 scp->status |= CURSOR_ENABLED;
3506 sc_draw_cursor_image(scp);
3508 tp = SC_DEV(sc, scp->index);
3509 if (!kdb_active && tty_opened_ns(tp))
3510 sctty_outwakeup(tp);
3517 #ifndef SC_NO_CUTPASTE
3518 sc_mouse_paste(scp);
3522 /* NON-LOCKING KEYS */
3524 case LSH: case RSH: case LCTR: case RCTR:
3525 case LALT: case RALT: case ASH: case META:
3529 if (!(sc->flags & SC_SCRN_BLANKED))
3535 /* force activatation/deactivation of the screen saver */
3536 if (!(sc->flags & SC_SCRN_BLANKED)) {
3537 run_scrn_saver = TRUE;
3538 sc->scrn_time_stamp -= scrn_blank_time;
3542 * While devices are being probed, the screen saver need
3543 * to be invoked explicitly. XXX
3545 if (sc->flags & SC_SCRN_BLANKED) {
3546 scsplash_stick(FALSE);
3547 stop_scrn_saver(sc, current_saver);
3549 if (!ISGRAPHSC(scp)) {
3550 scsplash_stick(TRUE);
3551 (*current_saver)(sc, TRUE);
3555 #endif /* DEV_SPLASH */
3559 #ifndef SC_DISABLE_REBOOT
3566 #ifndef SC_DISABLE_REBOOT
3568 shutdown_nice(RB_HALT);
3573 #ifndef SC_DISABLE_REBOOT
3575 shutdown_nice(RB_HALT|RB_POWEROFF);
3580 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3583 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3587 #ifndef SC_DISABLE_KDBKEY
3594 if (enable_panic_key)
3595 panic("Forced by the panic key");
3599 this_scr = scp->index;
3600 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3601 sc->first_vty + i != this_scr;
3602 i = (i + 1)%sc->vtys) {
3603 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3604 if (tty_opened_ns(tp)) {
3605 sc_switch_scr(scp->sc, sc->first_vty + i);
3612 this_scr = scp->index;
3613 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3614 sc->first_vty + i != this_scr;
3615 i = (i + sc->vtys - 1)%sc->vtys) {
3616 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3617 if (tty_opened_ns(tp)) {
3618 sc_switch_scr(scp->sc, sc->first_vty + i);
3625 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3626 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3629 /* assert(c & FKEY) */
3630 if (!(sc->flags & SC_SCRN_BLANKED))
3634 /* goto next_code */
3636 /* regular keys (maybe MKEY is set) */
3637 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
3639 kdb_alt_break(c, &sc->sc_altbrk);
3641 if (!(sc->flags & SC_SCRN_BLANKED))
3650 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
3651 int nprot, vm_memattr_t *memattr)
3655 scp = sc_get_stat(tp);
3656 if (scp != scp->sc->cur_scp)
3658 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
3662 update_font(scr_stat *scp)
3664 #ifndef SC_NO_FONT_LOADING
3665 /* load appropriate font */
3666 if (!(scp->status & GRAPHICS_MODE)) {
3667 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3668 if (scp->font_size < 14) {
3669 if (scp->sc->fonts_loaded & FONT_8)
3670 sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3671 } else if (scp->font_size >= 16) {
3672 if (scp->sc->fonts_loaded & FONT_16)
3673 sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3675 if (scp->sc->fonts_loaded & FONT_14)
3676 sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3680 * This is an interim kludge to display correct font.
3681 * Always use the font page #0 on the video plane 2.
3682 * Somehow we cannot show the font in other font pages on
3683 * some video cards... XXX
3685 sc_show_font(scp, 0);
3689 #endif /* !SC_NO_FONT_LOADING */
3693 save_kbd_state(scr_stat *scp)
3698 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3699 if (error == ENOIOCTL)
3702 scp->status &= ~LOCK_MASK;
3703 scp->status |= state;
3709 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3714 if (mask != LOCK_MASK) {
3715 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3716 if (error == ENOIOCTL)
3721 state |= new_bits & mask;
3723 state = new_bits & LOCK_MASK;
3725 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3726 if (error == ENOIOCTL)
3732 update_kbd_leds(scr_stat *scp, int which)
3737 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3738 if (error == ENOIOCTL)
3744 set_mode(scr_stat *scp)
3748 /* reject unsupported mode */
3749 if (vidd_get_info(scp->sc->adp, scp->mode, &info))
3752 /* if this vty is not currently showing, do nothing */
3753 if (scp != scp->sc->cur_scp)
3756 /* setup video hardware for the given mode */
3757 vidd_set_mode(scp->sc->adp, scp->mode);
3758 scp->rndr->init(scp);
3760 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3761 (void *)scp->sc->adp->va_window, FALSE);
3766 sc_set_border(scp, scp->border);
3767 sc_set_cursor_image(scp);
3773 sc_set_border(scr_stat *scp, int color)
3775 SC_VIDEO_LOCK(scp->sc);
3776 (*scp->rndr->draw_border)(scp, color);
3777 SC_VIDEO_UNLOCK(scp->sc);
3780 #ifndef SC_NO_FONT_LOADING
3782 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3783 int base, int count)
3788 sc->font_loading_in_progress = TRUE;
3789 vidd_load_font(sc->adp, page, size, width, buf, base, count);
3790 sc->font_loading_in_progress = FALSE;
3794 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3795 int base, int count)
3800 sc->font_loading_in_progress = TRUE;
3801 vidd_save_font(sc->adp, page, size, width, buf, base, count);
3802 sc->font_loading_in_progress = FALSE;
3806 sc_show_font(scr_stat *scp, int page)
3808 vidd_show_font(scp->sc->adp, page);
3810 #endif /* !SC_NO_FONT_LOADING */
3813 sc_paste(scr_stat *scp, const u_char *p, int count)
3818 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3819 if (!tty_opened_ns(tp))
3821 rmap = scp->sc->scr_rmap;
3822 for (; count > 0; --count)
3823 ttydisc_rint(tp, rmap[*p++], 0);
3824 ttydisc_rint_done(tp);
3828 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup)
3832 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3833 if (!tty_opened_ns(tp))
3835 ttydisc_rint_simple(tp, p, count);
3837 /* XXX: we can't always call ttydisc_rint_done() here! */
3838 ttydisc_rint_done(tp);
3843 sc_bell(scr_stat *scp, int pitch, int duration)
3845 if (cold || shutdown_in_progress || !enable_bell)
3848 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3851 if (scp->sc->flags & SC_VISUAL_BELL) {
3852 if (scp->sc->blink_in_progress)
3854 scp->sc->blink_in_progress = 3;
3855 if (scp != scp->sc->cur_scp)
3856 scp->sc->blink_in_progress += 2;
3857 blink_screen(scp->sc->cur_scp);
3858 } else if (duration != 0 && pitch != 0) {
3859 if (scp != scp->sc->cur_scp)
3861 sysbeep(1193182 / pitch, duration);
3866 blink_screen(void *arg)
3868 scr_stat *scp = arg;
3871 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3872 scp->sc->blink_in_progress = 0;
3874 tp = SC_DEV(scp->sc, scp->index);
3875 if (tty_opened_ns(tp))
3876 sctty_outwakeup(tp);
3877 if (scp->sc->delayed_next_scr)
3878 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3881 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3882 scp->sc->blink_in_progress & 1);
3883 scp->sc->blink_in_progress--;
3884 callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0,
3885 blink_screen, scp, C_PREL(0));
3890 * Until sc_attach_unit() gets called no dev structures will be available
3891 * to store the per-screen current status. This is the case when the
3892 * kernel is initially booting and needs access to its console. During
3893 * this early phase of booting the console's current status is kept in
3894 * one statically defined scr_stat structure, and any pointers to the
3895 * dev structures will be NULL.
3899 sc_get_stat(struct tty *tp)
3902 return (&main_console);
3903 return (SC_STAT(tp));
3907 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3908 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3913 sc_allocate_keyboard(sc_softc_t *sc, int unit)
3919 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3921 k0 = kbd_get_keyboard(idx0);
3923 for (idx = kbd_find_keyboard2("*", -1, 0);
3925 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
3926 k = kbd_get_keyboard(idx);
3928 if (idx == idx0 || KBD_IS_BUSY(k))
3931 bzero(&ki, sizeof(ki));
3932 strcpy(ki.kb_name, k->kb_name);
3933 ki.kb_unit = k->kb_unit;
3935 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3938 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);