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 * Simple font scaling code by Sascha Wildner and Matthew Dillon
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.
33 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/dev/syscons/syscons.c,v 1.336.2.17 2004/03/25 08:41:09 ru Exp $
36 #include "use_splash.h"
37 #include "opt_syscons.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/eventhandler.h>
43 #include <sys/reboot.h>
47 #include <sys/signalvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/kernel.h>
52 #include <sys/random.h>
55 #include <sys/thread2.h>
56 #include <sys/mutex2.h>
58 #include <machine/clock.h>
59 #include <machine/console.h>
60 #include <machine/psl.h>
61 #include <machine/pc/display.h>
62 #include <machine/frame.h>
64 #include <dev/drm/include/linux/fb.h>
65 #include <dev/misc/kbd/kbdreg.h>
66 #include <dev/video/fb/fbreg.h>
67 #include <dev/video/fb/splashreg.h>
73 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
74 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
76 #define SCRN_ASYNCOK 0x0001
77 #define SCRN_BULKUNLOCK 0x0002
79 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
80 #define WANT_UNLOCK(m) do { \
85 #define WANT_LOCK(m) do { \
91 MALLOC_DEFINE(M_SYSCONS, "syscons", "Syscons");
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 default_attr kernel_default = {
105 SC_KERNEL_CONS_REV_ATTR,
108 static int sc_console_unit = -1;
109 static scr_stat *sc_console;
110 static struct tty *sc_console_tty;
111 static void *kernel_console_ts;
113 static char init_done = COLD;
114 static char shutdown_in_progress = FALSE;
115 static char sc_malloc = FALSE;
117 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
118 static int run_scrn_saver = FALSE; /* should run the saver? */
119 static long scrn_blank_time = 0; /* screen saver timeout value */
121 static int scrn_blanked; /* # of blanked screen */
122 static int sticky_splash = FALSE;
124 static void none_saver(sc_softc_t *sc, int blank) { }
125 static void (*current_saver)(sc_softc_t *, int) = none_saver;
128 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
132 static bios_values_t bios_value;
134 static int enable_panic_key;
135 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
136 0, "Enable the panic key (CTRL-ALT-SHIFT-ESC)");
137 static int syscons_async;
138 SYSCTL_INT(_kern, OID_AUTO, syscons_async, CTLFLAG_RW, &syscons_async,
139 0, "Asynchronous bulk syscons fb updates");
141 #define SC_CONSOLECTL 255
143 #define VIRTUAL_TTY(sc, x) ((SC_DEV((sc),(x)) != NULL) ? \
144 (SC_DEV((sc),(x))->si_tty) : NULL)
145 #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
148 static cdev_t cctl_dev;
150 static timeout_t blink_screen_callout;
152 static void sc_blink_screen(scr_stat *scp);
153 static struct mtx syscons_mtx = MTX_INITIALIZER("syscons");
156 static int scvidprobe(int unit, int flags, int cons);
157 static int sckbdprobe(int unit, int flags, int cons);
158 static void scmeminit(void *arg);
159 static int scdevtounit(cdev_t dev);
160 static kbd_callback_func_t sckbdevent;
161 static int scparam(struct tty *tp, struct termios *t);
162 static void scstart(struct tty *tp);
163 static void scinit(int unit, int flags);
164 static void scterm(int unit, int flags);
165 static void scshutdown(void *arg, int howto);
166 static void sc_puts(scr_stat *scp, u_char *buf, int len);
167 static u_int scgetc(sc_softc_t *sc, u_int flags);
169 #define SCGETC_NONBLOCK 2
170 static int sccngetch(int flags);
171 static void sccnupdate(scr_stat *scp);
172 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
173 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
174 static timeout_t scrn_timer;
175 static int and_region(int *s1, int *e1, int s2, int e2);
176 static void scrn_update(scr_stat *scp, int show_cursor, int flags);
177 static void scrn_update_thread(void *arg);
180 static int scsplash_callback(int event, void *arg);
181 static void scsplash_saver(sc_softc_t *sc, int show);
182 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
183 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
184 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
185 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
186 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
187 static int wait_scrn_saver_stop(sc_softc_t *sc);
188 #define scsplash_stick(stick) (sticky_splash = (stick))
190 #define scsplash_stick(stick)
193 static void do_switch_scr(sc_softc_t *sc);
194 static int vt_proc_alive(scr_stat *scp);
195 static int signal_vt_rel(scr_stat *scp);
196 static int signal_vt_acq(scr_stat *scp);
197 static int finish_vt_rel(scr_stat *scp, int release);
198 static int finish_vt_acq(scr_stat *scp);
199 static void exchange_scr(sc_softc_t *sc);
200 static void update_cursor_image(scr_stat *scp);
201 static int save_kbd_state(scr_stat *scp, int unlock);
202 static int update_kbd_state(scr_stat *scp, int state, int mask, int unlock);
203 static int update_kbd_leds(scr_stat *scp, int which);
204 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
207 * Console locking support functions.
209 * We use mutex spinlocks here in order to allow reentrancy which should
210 * avoid issues during panics.
212 * A panic can stop a cpu while it is holding the syscons_mtx. If this
213 * occurs we try for up to 1/2 a second and then blow the spinlock away.
214 * The asynchronous update thread makes this happen more often.
219 if (panicstr || shutdown_in_progress) {
220 int retries = 500000;
221 while (mtx_spinlock_try(&syscons_mtx)) {
222 tsc_delay(1000); /* 1uS */
223 if (--retries == 0) {
224 mtx_init(&syscons_mtx, "syscons");
229 mtx_spinlock(&syscons_mtx);
234 * Returns 0 on success, EAGAIN on failure.
237 syscons_lock_nonblock(void)
239 return(mtx_spinlock_try(&syscons_mtx));
245 mtx_spinunlock(&syscons_mtx);
251 static cn_probe_t sccnprobe;
252 static cn_init_t sccninit;
253 static cn_init_t sccninit_fini;
254 static cn_getc_t sccngetc;
255 static cn_checkc_t sccncheckc;
256 static cn_putc_t sccnputc;
257 static cn_dbctl_t sccndbctl;
258 static cn_term_t sccnterm;
259 static cn_poll_t sccnpoll;
261 CONS_DRIVER(sc, sccnprobe, sccninit, sccninit_fini, sccnterm,
262 sccngetc, sccncheckc, sccnputc, sccndbctl, sccnpoll);
264 static d_open_t scopen;
265 static d_close_t scclose;
266 static d_read_t scread;
267 static d_ioctl_t scioctl;
268 static d_mmap_t scmmap;
270 static struct dev_ops sc_ops = {
278 .d_kqfilter = ttykqfilter,
279 .d_revoke = ttyrevoke
283 sc_probe_unit(int unit, int flags)
285 if (!scvidprobe(unit, flags, FALSE)) {
287 kprintf("sc%d: no video adapter found.\n", unit);
291 /* syscons will be attached even when there is no keyboard */
292 sckbdprobe(unit, flags, FALSE);
298 register_framebuffer(struct fb_info *info)
302 /* For now ignore framebuffers, which don't replace the vga display */
303 if (!info->is_vga_boot_display)
306 lwkt_gettoken(&tty_token);
307 sc = sc_get_softc(0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
309 lwkt_reltoken(&tty_token);
310 kprintf("%s: sc_get_softc(%d, %d) returned NULL\n", __func__,
311 0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
315 /* Ignore this framebuffer if we have already switched to a framebuffer */
316 if (sc->fbi != NULL) {
317 lwkt_reltoken(&tty_token);
323 if (sc->fbi != NULL) {
324 sc_update_render(sc->cur_scp);
325 sc->fbi->restore(sc->fbi->cookie);
328 lwkt_reltoken(&tty_token);
332 /* probe video adapters, return TRUE if found */
334 scvidprobe(int unit, int flags, int cons)
337 * Access the video adapter driver through the back door!
338 * Video adapter drivers need to be configured before syscons.
339 * However, when syscons is being probed as the low-level console,
340 * they have not been initialized yet. We force them to initialize
341 * themselves here. XXX
343 vid_configure(cons ? VIO_PROBE_ONLY : 0);
345 return (vid_find_adapter("*", unit) >= 0);
348 /* probe the keyboard, return TRUE if found */
350 sckbdprobe(int unit, int flags, int cons)
352 /* access the keyboard driver through the backdoor! */
353 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
355 return (kbd_find_keyboard("*", unit) >= 0);
359 adapter_name(video_adapter_t *adp)
365 { KD_MONO, { "MDA", "MDA" } },
366 { KD_HERCULES, { "Hercules", "Hercules" } },
367 { KD_CGA, { "CGA", "CGA" } },
368 { KD_EGA, { "EGA", "EGA (mono)" } },
369 { KD_VGA, { "VGA", "VGA (mono)" } },
370 { KD_TGA, { "TGA", "TGA" } },
371 { -1, { "Unknown", "Unknown" } },
375 for (i = 0; names[i].type != -1; ++i)
376 if (names[i].type == adp->va_type)
378 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
382 sc_attach_unit(int unit, int flags)
391 flags &= ~SC_KERNEL_CONSOLE;
393 if (sc_console_unit == unit) {
395 * If this unit is being used as the system console, we need to
396 * adjust some variables and buffers before and after scinit().
398 /* assert(sc_console != NULL) */
399 flags |= SC_KERNEL_CONSOLE;
404 if (sc_console->tsw->te_size > 0) {
405 /* assert(sc_console->ts != NULL); */
406 kernel_console_ts = sc_console->ts;
407 sc_console->ts = kmalloc(sc_console->tsw->te_size,
408 M_SYSCONS, M_WAITOK);
409 bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
410 (*sc_console->tsw->te_default_attr)(sc_console,
411 user_default.std_color,
412 user_default.rev_color);
418 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
421 * If this is the console we couldn't setup sc->dev before because
422 * malloc wasn't working. Set it up now.
424 if (flags & SC_KERNEL_CONSOLE) {
425 KKASSERT(sc->dev == NULL);
426 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK|M_ZERO);
427 sc->dev[0] = make_dev(&sc_ops, sc_console_unit*MAXCONS, UID_ROOT,
429 "ttyv%r", sc_console_unit*MAXCONS);
430 sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
431 sc->dev[0]->si_drv1 = sc_console;
435 * Finish up the standard attach
438 callout_init_mp(&sc->scrn_timer_ch);
439 scp = SC_STAT(sc->dev[0]);
440 if (sc_console == NULL) /* sc_console_unit < 0 */
444 if ((sc->config & SC_VESA800X600)
445 && ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
447 if (sc->flags & SC_SPLASH_SCRN)
448 splash_term(sc->adp);
450 sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
451 sc_set_pixel_mode(scp, NULL, 0, 0, 16);
452 sc->initial_mode = M_VESA_800x600;
454 /* put up the splash again! */
455 if (sc->flags & SC_SPLASH_SCRN)
456 splash_init(sc->adp, scsplash_callback, sc);
459 #endif /* SC_PIXEL_MODE */
461 /* initialize cursor */
463 update_cursor_image(scp);
465 /* get screen update going */
468 /* set up the keyboard */
469 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
470 update_kbd_state(scp, scp->status, LOCK_MASK, FALSE);
472 kprintf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
473 unit, adapter_name(sc->adp), sc->vtys, sc->config);
475 kprintf("sc%d:", unit);
476 if (sc->adapter >= 0)
477 kprintf(" fb%d", sc->adapter);
478 if (sc->keyboard >= 0)
479 kprintf(", kbd%d", sc->keyboard);
481 kprintf(", terminal emulator: %s (%s)",
482 scp->tsw->te_name, scp->tsw->te_desc);
486 /* register a shutdown callback for the kernel console */
487 if (sc_console_unit == unit)
488 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
489 (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
494 * The first vty already has struct tty and scr_stat initialized
495 * in scinit(). The other vtys will have these structs when
498 for (vc = 1; vc < sc->vtys; vc++) {
499 dev = make_dev(&sc_ops, vc + unit * MAXCONS,
501 0600, "ttyv%r", vc + unit * MAXCONS);
504 cctl_dev = make_dev(&sc_ops, SC_CONSOLECTL,
505 UID_ROOT, GID_WHEEL, 0600, "consolectl");
506 cctl_dev->si_tty = sc_console_tty = ttymalloc(sc_console_tty);
507 cctl_dev->si_drv1 = sc_console;
519 * As soon as malloc() becomes functional, we had better allocate
520 * various buffers for the kernel console.
523 if (sc_console_unit < 0) /* sc_console == NULL */
526 /* copy the temporary buffer to the final buffer */
527 sc_alloc_scr_buffer(sc_console, TRUE, FALSE);
529 #ifndef SC_NO_CUTPASTE
530 sc_alloc_cut_buffer(sc_console, TRUE);
533 #ifndef SC_NO_HISTORY
534 /* initialize history buffer & pointers */
535 sc_alloc_history_buffer(sc_console, 0, 0, TRUE);
539 SYSINIT(sc_mem, SI_BOOT1_POST, SI_ORDER_ANY, scmeminit, NULL);
542 scdevtounit(cdev_t dev)
544 int vty = SC_VTY(dev);
546 if (vty == SC_CONSOLECTL)
547 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
548 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
555 scopen(struct dev_open_args *ap)
557 cdev_t dev = ap->a_head.a_dev;
565 lwkt_gettoken(&tty_token);
566 unit = scdevtounit(dev);
567 DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
568 major(dev), minor(dev), unit, SC_VTY(dev)));
570 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
572 lwkt_reltoken(&tty_token);
576 tp = dev->si_tty = ttymalloc(dev->si_tty);
577 tp->t_oproc = scstart;
578 tp->t_param = scparam;
579 tp->t_stop = nottystop;
583 if (!ISTTYOPEN(tp)) {
585 /* Use the current setting of the <-- key as default VERASE. */
586 /* If the Delete key is preferable, an stty is necessary */
587 if (sc->kbd != NULL) {
588 key.keynum = KEYCODE_BS;
589 kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
590 tp->t_cc[VERASE] = key.key.map[0];
592 tp->t_iflag = TTYDEF_IFLAG;
593 tp->t_oflag = TTYDEF_OFLAG;
594 tp->t_cflag = TTYDEF_CFLAG;
595 tp->t_lflag = TTYDEF_LFLAG;
596 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
597 scparam(tp, &tp->t_termios);
598 (*linesw[tp->t_line].l_modem)(tp, 1);
601 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
602 lwkt_reltoken(&tty_token);
606 error = (*linesw[tp->t_line].l_open)(dev, tp);
610 scp = dev->si_drv1 = alloc_scp(sc, SC_VTY(dev));
613 sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
616 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
617 tp->t_winsize.ws_col = scp->xsize;
618 tp->t_winsize.ws_row = scp->ysize;
622 * Start optional support thread for syscons refreshes. This thread
623 * will execute the refresh without the syscons_lock held and will
624 * allow interrupts while it is doing so.
626 if (scp->asynctd == NULL) {
627 lwkt_create(scrn_update_thread, scp, &scp->asynctd, NULL, 0, -1,
628 "syscons%d", SC_VTY(dev));
630 lwkt_reltoken(&tty_token);
635 scclose(struct dev_close_args *ap)
637 cdev_t dev = ap->a_head.a_dev;
638 struct tty *tp = dev->si_tty;
641 lwkt_gettoken(&tty_token);
642 if (SC_VTY(dev) != SC_CONSOLECTL) {
643 scp = SC_STAT(tp->t_dev);
644 /* were we in the middle of the VT switching process? */
645 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
646 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
647 cons_unavail = FALSE;
648 if (finish_vt_rel(scp, TRUE) == 0) /* force release */
649 DPRINTF(5, ("reset WAIT_REL, "));
650 if (finish_vt_acq(scp) == 0) /* force acknowledge */
651 DPRINTF(5, ("reset WAIT_ACQ, "));
654 if (scp == &main_console) {
657 scp->smode.mode = VT_AUTO;
660 sc_vtb_destroy(&scp->vtb);
661 sc_vtb_destroy(&scp->scr);
662 sc_free_history_buffer(scp, scp->ysize);
664 kfree(scp, M_SYSCONS);
669 scp->smode.mode = VT_AUTO;
671 scp->kbd_mode = K_XLATE;
673 if (scp == scp->sc->cur_scp)
674 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
675 DPRINTF(5, ("done.\n"));
677 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
679 lwkt_reltoken(&tty_token);
685 scread(struct dev_read_args *ap)
689 lwkt_gettoken(&tty_token);
690 sc_touch_scrn_saver();
692 lwkt_reltoken(&tty_token);
697 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
705 lwkt_gettoken(&tty_token);
707 * WARNING: In early boot sc->dev may not be setup yet.
709 sc = (sc_softc_t *)arg;
710 /* assert(thiskbd == sc->kbd) */
714 if (sc->kbd && sc->kbd->kb_flags & KB_POLLED) {
715 lwkt_reltoken(&tty_token);
719 case KBDIO_UNLOADING:
724 kbd_release(thiskbd, (void *)&sc->keyboard);
725 lwkt_reltoken(&tty_token);
728 lwkt_reltoken(&tty_token);
733 * Loop while there is still input to get from the keyboard.
734 * I don't think this is nessesary, and it doesn't fix
735 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
737 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
738 cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
739 if (!ISTTYOPEN(cur_tty)) {
740 cur_tty = sc_console_tty;
741 if (!ISTTYOPEN(cur_tty))
746 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty)) {
752 switch (KEYFLAGS(c)) {
753 case 0x0000: /* normal key */
754 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
756 case FKEY: /* function key, return string */
757 cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
760 (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
763 case MKEY: /* meta is active, prepend ESC */
764 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
765 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
767 case BKEY: /* backtab fixed sequence (esc [ Z) */
768 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
769 (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
770 (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
776 sc->cur_scp->status |= MOUSE_HIDDEN;
779 lwkt_reltoken(&tty_token);
784 scparam(struct tty *tp, struct termios *t)
786 lwkt_gettoken(&tty_token);
787 tp->t_ispeed = t->c_ispeed;
788 tp->t_ospeed = t->c_ospeed;
789 tp->t_cflag = t->c_cflag;
790 lwkt_reltoken(&tty_token);
795 scioctl(struct dev_ioctl_args *ap)
797 cdev_t dev = ap->a_head.a_dev;
798 u_long cmd = ap->a_cmd;
799 caddr_t data = ap->a_data;
800 int flag = ap->a_fflag;
807 lwkt_gettoken(&tty_token);
810 error = sc_vid_ioctl(tp, cmd, data, flag);
811 if (error != ENOIOCTL) {
812 lwkt_reltoken(&tty_token);
816 #ifndef SC_NO_HISTORY
817 error = sc_hist_ioctl(tp, cmd, data, flag);
818 if (error != ENOIOCTL) {
819 lwkt_reltoken(&tty_token);
824 #ifndef SC_NO_SYSMOUSE
825 error = sc_mouse_ioctl(tp, cmd, data, flag);
826 if (error != ENOIOCTL) {
827 lwkt_reltoken(&tty_token);
832 scp = SC_STAT(tp->t_dev);
833 /* assert(scp != NULL) */
834 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
839 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag);
841 if (error != ENOIOCTL) {
842 lwkt_reltoken(&tty_token);
847 switch (cmd) { /* process console hardware related ioctl's */
849 case GIO_ATTR: /* get current attributes */
850 /* this ioctl is not processed here, but in the terminal emulator */
851 lwkt_reltoken(&tty_token);
854 case GIO_COLOR: /* is this a color console ? */
855 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
856 lwkt_reltoken(&tty_token);
859 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
860 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME) {
861 lwkt_reltoken(&tty_token);
865 scrn_blank_time = *(int *)data;
866 run_scrn_saver = (scrn_blank_time != 0);
868 lwkt_reltoken(&tty_token);
871 case CONS_CURSORTYPE: /* set cursor type blink/noblink */
873 if (!ISGRAPHSC(sc->cur_scp))
874 sc_remove_cursor_image(sc->cur_scp);
875 if ((*(int*)data) & 0x01)
876 sc->flags |= SC_BLINK_CURSOR;
878 sc->flags &= ~SC_BLINK_CURSOR;
879 if ((*(int*)data) & 0x02) {
880 sc->flags |= SC_CHAR_CURSOR;
882 sc->flags &= ~SC_CHAR_CURSOR;
884 * The cursor shape is global property; all virtual consoles
885 * are affected. Update the cursor in the current console...
887 if (!ISGRAPHSC(sc->cur_scp)) {
888 sc_set_cursor_image(sc->cur_scp);
889 sc_draw_cursor_image(sc->cur_scp);
892 lwkt_reltoken(&tty_token);
895 case CONS_BELLTYPE: /* set bell type sound/visual */
898 if ((*(int *)data) & 0x01)
899 sc->flags |= SC_VISUAL_BELL;
901 sc->flags &= ~SC_VISUAL_BELL;
903 if ((*(int *)data) & 0x02)
904 sc->flags |= SC_QUIET_BELL;
906 sc->flags &= ~SC_QUIET_BELL;
909 lwkt_reltoken(&tty_token);
912 case CONS_GETINFO: /* get current (virtual) console info */
914 vid_info_t *ptr = (vid_info_t*)data;
915 if (ptr->size == sizeof(struct vid_info)) {
916 ptr->m_num = sc->cur_scp->index;
917 ptr->font_size = scp->font_height;
918 ptr->mv_col = scp->xpos;
919 ptr->mv_row = scp->ypos;
920 ptr->mv_csz = scp->xsize;
921 ptr->mv_rsz = scp->ysize;
923 * The following fields are filled by the terminal emulator. XXX
930 ptr->mv_grfc.fore = 0; /* not supported */
931 ptr->mv_grfc.back = 0; /* not supported */
932 ptr->mv_ovscan = scp->border;
933 if (scp == sc->cur_scp)
934 save_kbd_state(scp, FALSE);
935 ptr->mk_keylock = scp->status & LOCK_MASK;
936 lwkt_reltoken(&tty_token);
939 lwkt_reltoken(&tty_token);
943 case CONS_GETVERS: /* get version number */
944 *(int*)data = 0x200; /* version 2.0 */
945 lwkt_reltoken(&tty_token);
948 case CONS_IDLE: /* see if the screen has been idle */
950 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
951 * the user process may have been writing something on the
952 * screen and syscons is not aware of it. Declare the screen
953 * is NOT idle if it is in one of these modes. But there is
954 * an exception to it; if a screen saver is running in the
955 * graphics mode in the current screen, we should say that the
956 * screen has been idle.
958 *(int *)data = (sc->flags & SC_SCRN_IDLE)
959 && (!ISGRAPHSC(sc->cur_scp)
960 || (sc->cur_scp->status & SAVER_RUNNING));
961 lwkt_reltoken(&tty_token);
964 case CONS_SAVERMODE: /* set saver mode */
965 switch(*(int *)data) {
969 /* if a LKM screen saver is running, stop it first. */
970 scsplash_stick(FALSE);
971 saver_mode = *(int *)data;
973 if ((error = wait_scrn_saver_stop(NULL))) {
975 lwkt_reltoken(&tty_token);
979 run_scrn_saver = TRUE;
980 if (saver_mode == CONS_USR_SAVER)
981 scp->status |= SAVER_RUNNING;
983 scp->status &= ~SAVER_RUNNING;
984 scsplash_stick(TRUE);
989 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
990 scp->status &= ~SAVER_RUNNING;
991 saver_mode = *(int *)data;
995 lwkt_reltoken(&tty_token);
998 lwkt_reltoken(&tty_token);
1001 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
1003 * Note that this ioctl does not guarantee the screen saver
1004 * actually starts or stops. It merely attempts to do so...
1007 run_scrn_saver = (*(int *)data != 0);
1009 sc->scrn_time_stamp -= scrn_blank_time;
1011 lwkt_reltoken(&tty_token);
1014 case CONS_SCRSHOT: /* get a screen shot */
1016 scrshot_t *ptr = (scrshot_t*)data;
1018 if (ISGRAPHSC(scp)) {
1020 lwkt_reltoken(&tty_token);
1023 if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
1025 lwkt_reltoken(&tty_token);
1029 copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
1030 ptr->xsize * ptr->ysize * sizeof(uint16_t));
1031 lwkt_reltoken(&tty_token);
1035 case VT_SETMODE: /* set screen switcher mode */
1037 struct vt_mode *mode;
1039 mode = (struct vt_mode *)data;
1040 DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
1041 if (scp->smode.mode == VT_PROCESS) {
1042 if (scp->proc == pfindn(scp->pid) && scp->proc != curproc) {
1043 DPRINTF(5, ("error EPERM\n"));
1044 lwkt_reltoken(&tty_token);
1049 if (mode->mode == VT_AUTO) {
1050 scp->smode.mode = VT_AUTO;
1053 DPRINTF(5, ("VT_AUTO, "));
1054 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1055 cons_unavail = FALSE;
1056 if (finish_vt_rel(scp, TRUE) == 0)
1057 DPRINTF(5, ("reset WAIT_REL, "));
1058 if (finish_vt_acq(scp) == 0)
1059 DPRINTF(5, ("reset WAIT_ACQ, "));
1061 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1062 || !ISSIGVALID(mode->frsig)) {
1064 DPRINTF(5, ("error EINVAL\n"));
1065 lwkt_reltoken(&tty_token);
1068 DPRINTF(5, ("VT_PROCESS %d, ", curproc->p_pid));
1069 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1070 scp->proc = curproc;
1071 scp->pid = scp->proc->p_pid;
1072 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1073 cons_unavail = TRUE;
1077 lwkt_reltoken(&tty_token);
1081 case VT_GETMODE: /* get screen switcher mode */
1082 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1083 lwkt_reltoken(&tty_token);
1086 case VT_RELDISP: /* screen switcher ioctl */
1088 * This must be the current vty which is in the VT_PROCESS
1092 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1094 lwkt_reltoken(&tty_token);
1097 /* ...and this process is controlling it. */
1098 if (scp->proc != curproc) {
1100 lwkt_reltoken(&tty_token);
1104 switch(*(int *)data) {
1105 case VT_FALSE: /* user refuses to release screen, abort */
1106 if ((error = finish_vt_rel(scp, FALSE)) == 0)
1107 DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
1109 case VT_TRUE: /* user has released screen, go on */
1110 if ((error = finish_vt_rel(scp, TRUE)) == 0)
1111 DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
1113 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1114 if ((error = finish_vt_acq(scp)) == 0)
1115 DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
1121 lwkt_reltoken(&tty_token);
1124 case VT_OPENQRY: /* return free virtual console */
1125 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1126 tp = VIRTUAL_TTY(sc, i);
1127 if (!ISTTYOPEN(tp)) {
1128 *(int *)data = i + 1;
1129 lwkt_reltoken(&tty_token);
1133 lwkt_reltoken(&tty_token);
1136 case VT_ACTIVATE: /* switch to screen *data */
1137 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1139 sc_clean_up(sc->cur_scp);
1140 error = sc_switch_scr(sc, i);
1142 lwkt_reltoken(&tty_token);
1145 case VT_WAITACTIVE: /* wait for switch to occur */
1146 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1147 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys)) {
1148 lwkt_reltoken(&tty_token);
1152 error = sc_clean_up(sc->cur_scp);
1155 lwkt_reltoken(&tty_token);
1160 * scp might be NULL, we aren't sure why. Check for NULL.
1162 * http://bugs.dragonflybsd.org/issues/2481
1164 scp = SC_STAT(SC_DEV(sc, i));
1165 if (scp == NULL || scp == scp->sc->cur_scp) {
1166 lwkt_reltoken(&tty_token);
1169 error = tsleep((caddr_t)&scp->smode, PCATCH, "waitvt", 0);
1170 /* May return ERESTART */
1171 lwkt_reltoken(&tty_token);
1174 case VT_GETACTIVE: /* get active vty # */
1175 *(int *)data = sc->cur_scp->index + 1;
1176 lwkt_reltoken(&tty_token);
1179 case VT_GETINDEX: /* get this vty # */
1180 *(int *)data = scp->index + 1;
1181 lwkt_reltoken(&tty_token);
1184 case VT_LOCKSWITCH: /* prevent vty switching */
1186 if ((*(int *)data) & 0x01)
1187 sc->flags |= SC_SCRN_VTYLOCK;
1189 sc->flags &= ~SC_SCRN_VTYLOCK;
1191 lwkt_reltoken(&tty_token);
1194 case KDENABIO: /* allow io operations */
1195 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1197 lwkt_reltoken(&tty_token);
1200 if (securelevel > 0) {
1201 lwkt_reltoken(&tty_token);
1204 #if defined(__i386__)
1205 curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
1206 #elif defined(__x86_64__)
1207 curthread->td_lwp->lwp_md.md_regs->tf_rflags |= PSL_IOPL;
1209 lwkt_reltoken(&tty_token);
1212 case KDDISABIO: /* disallow io operations (default) */
1213 #if defined(__i386__)
1214 curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;
1215 #elif defined(__x86_64__)
1216 curthread->td_lwp->lwp_md.md_regs->tf_rflags &= ~PSL_IOPL;
1218 lwkt_reltoken(&tty_token);
1221 case KDSKBSTATE: /* set keyboard state (locks) */
1222 if (*(int *)data & ~LOCK_MASK) {
1223 lwkt_reltoken(&tty_token);
1227 scp->status &= ~LOCK_MASK;
1228 scp->status |= *(int *)data;
1230 if (scp == sc->cur_scp)
1231 update_kbd_state(scp, scp->status, LOCK_MASK, FALSE);
1232 lwkt_reltoken(&tty_token);
1235 case KDGKBSTATE: /* get keyboard state (locks) */
1236 if (scp == sc->cur_scp)
1237 save_kbd_state(scp, FALSE);
1238 *(int *)data = scp->status & LOCK_MASK;
1239 lwkt_reltoken(&tty_token);
1242 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1243 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1244 error = kbd_ioctl(sc->kbd, cmd, data);
1245 if (error == ENOIOCTL)
1247 lwkt_reltoken(&tty_token);
1250 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1251 if (*(int *)data & ~0x7f) {
1252 lwkt_reltoken(&tty_token);
1255 error = kbd_ioctl(sc->kbd, cmd, data);
1256 if (error == ENOIOCTL)
1258 lwkt_reltoken(&tty_token);
1261 case KDSKBMODE: /* set keyboard mode */
1262 switch (*(int *)data) {
1263 case K_XLATE: /* switch to XLT ascii mode */
1264 case K_RAW: /* switch to RAW scancode mode */
1265 case K_CODE: /* switch to CODE mode */
1266 scp->kbd_mode = *(int *)data;
1267 if (scp == sc->cur_scp)
1268 kbd_ioctl(sc->kbd, cmd, data);
1269 lwkt_reltoken(&tty_token);
1272 lwkt_reltoken(&tty_token);
1277 case KDGKBMODE: /* get keyboard mode */
1278 *(int *)data = scp->kbd_mode;
1279 lwkt_reltoken(&tty_token);
1283 error = kbd_ioctl(sc->kbd, cmd, data);
1284 if (error == ENOIOCTL)
1286 lwkt_reltoken(&tty_token);
1289 case KDMKTONE: /* sound the bell */
1292 sc_bell(scp, (*(int*)data)&0xffff,
1293 (((*(int*)data)>>16)&0xffff)*hz/1000);
1295 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1297 lwkt_reltoken(&tty_token);
1300 case KIOCSOUND: /* make tone (*data) hz */
1302 if (scp == sc->cur_scp) {
1304 error = sc_tone(*(int *)data);
1312 lwkt_reltoken(&tty_token);
1315 case KDGKBTYPE: /* get keyboard type */
1316 error = kbd_ioctl(sc->kbd, cmd, data);
1317 if (error == ENOIOCTL) {
1318 /* always return something? XXX */
1321 lwkt_reltoken(&tty_token);
1324 case KDSETLED: /* set keyboard LED status */
1325 if (*(int *)data & ~LED_MASK) { /* FIXME: LOCK_MASK? */
1326 lwkt_reltoken(&tty_token);
1330 scp->status &= ~LED_MASK;
1331 scp->status |= *(int *)data;
1333 if (scp == sc->cur_scp)
1334 update_kbd_leds(scp, scp->status);
1335 lwkt_reltoken(&tty_token);
1338 case KDGETLED: /* get keyboard LED status */
1339 if (scp == sc->cur_scp)
1340 save_kbd_state(scp, FALSE);
1341 *(int *)data = scp->status & LED_MASK;
1342 lwkt_reltoken(&tty_token);
1345 case KBADDKBD: /* add/remove keyboard to/from mux */
1347 error = kbd_ioctl(sc->kbd, cmd, data);
1348 if (error == ENOIOCTL)
1350 lwkt_reltoken(&tty_token);
1353 case CONS_SETKBD: /* set the new keyboard */
1357 newkbd = kbd_get_keyboard(*(int *)data);
1358 if (newkbd == NULL) {
1359 lwkt_reltoken(&tty_token);
1363 if (sc->kbd != newkbd) {
1364 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1365 (void *)&sc->keyboard, sckbdevent, sc);
1366 /* i == newkbd->kb_index */
1368 if (sc->kbd != NULL) {
1369 save_kbd_state(sc->cur_scp, FALSE);
1370 kbd_release(sc->kbd, (void *)&sc->keyboard);
1373 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1376 kbd_ioctl(sc->kbd, KDSKBMODE,
1377 (caddr_t)&sc->cur_scp->kbd_mode);
1378 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1381 error = EPERM; /* XXX */
1384 lwkt_reltoken(&tty_token);
1388 case CONS_RELKBD: /* release the current keyboard */
1390 if (sc->kbd != NULL) {
1391 save_kbd_state(sc->cur_scp, FALSE);
1392 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1400 lwkt_reltoken(&tty_token);
1403 case CONS_GETTERM: /* get the current terminal emulator info */
1407 if (((term_info_t *)data)->ti_index == 0) {
1410 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1413 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1414 sizeof(((term_info_t *)data)->ti_name));
1415 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1416 sizeof(((term_info_t *)data)->ti_desc));
1417 ((term_info_t *)data)->ti_flags = 0;
1418 lwkt_reltoken(&tty_token);
1421 ((term_info_t *)data)->ti_name[0] = '\0';
1422 ((term_info_t *)data)->ti_desc[0] = '\0';
1423 ((term_info_t *)data)->ti_flags = 0;
1424 lwkt_reltoken(&tty_token);
1429 case CONS_SETTERM: /* set the current terminal emulator */
1431 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1432 /* FIXME: what if scp == sc_console! XXX */
1434 lwkt_reltoken(&tty_token);
1437 case GIO_SCRNMAP: /* get output translation table */
1438 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1439 lwkt_reltoken(&tty_token);
1442 case PIO_SCRNMAP: /* set output translation table */
1443 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1444 for (i=0; i<sizeof(sc->scr_map); i++) {
1445 sc->scr_rmap[sc->scr_map[i]] = i;
1447 lwkt_reltoken(&tty_token);
1450 case GIO_KEYMAP: /* get keyboard translation table */
1451 case PIO_KEYMAP: /* set keyboard translation table */
1452 case GIO_DEADKEYMAP: /* get accent key translation table */
1453 case PIO_DEADKEYMAP: /* set accent key translation table */
1454 case GETFKEY: /* get function key string */
1455 case SETFKEY: /* set function key string */
1456 error = kbd_ioctl(sc->kbd, cmd, data);
1457 if (error == ENOIOCTL)
1459 lwkt_reltoken(&tty_token);
1462 #ifndef SC_NO_FONT_LOADING
1464 case PIO_FONT8x8: /* set 8x8 dot font */
1465 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1466 lwkt_reltoken(&tty_token);
1470 bcopy(data, sc->font_8, 8*256);
1471 sc->fonts_loaded |= FONT_8;
1474 * Always use the font page #0. XXX
1475 * Don't load if the current font size is not 8x8.
1477 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_height < 14))
1478 sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
1480 lwkt_reltoken(&tty_token);
1483 case GIO_FONT8x8: /* get 8x8 dot font */
1484 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1485 lwkt_reltoken(&tty_token);
1488 if (sc->fonts_loaded & FONT_8) {
1489 bcopy(sc->font_8, data, 8*256);
1490 lwkt_reltoken(&tty_token);
1494 lwkt_reltoken(&tty_token);
1498 case PIO_FONT8x14: /* set 8x14 dot font */
1499 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1500 lwkt_reltoken(&tty_token);
1504 bcopy(data, sc->font_14, 14*256);
1505 sc->fonts_loaded |= FONT_14;
1508 * Always use the font page #0. XXX
1509 * Don't load if the current font size is not 8x14.
1511 if (ISTEXTSC(sc->cur_scp)
1512 && (sc->cur_scp->font_height >= 14)
1513 && (sc->cur_scp->font_height < 16)) {
1514 sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
1517 lwkt_reltoken(&tty_token);
1520 case GIO_FONT8x14: /* get 8x14 dot font */
1521 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1522 lwkt_reltoken(&tty_token);
1525 if (sc->fonts_loaded & FONT_14) {
1526 bcopy(sc->font_14, data, 14*256);
1527 lwkt_reltoken(&tty_token);
1531 lwkt_reltoken(&tty_token);
1535 case PIO_FONT8x16: /* set 8x16 dot font */
1536 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1537 lwkt_reltoken(&tty_token);
1541 bcopy(data, sc->font_16, 16*256);
1542 sc->fonts_loaded |= FONT_16;
1545 * Always use the font page #0. XXX
1546 * Don't load if the current font size is not 8x16.
1548 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_height >= 16))
1549 sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
1551 lwkt_reltoken(&tty_token);
1554 case GIO_FONT8x16: /* get 8x16 dot font */
1555 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1556 lwkt_reltoken(&tty_token);
1559 if (sc->fonts_loaded & FONT_16) {
1560 bcopy(sc->font_16, data, 16*256);
1561 lwkt_reltoken(&tty_token);
1565 lwkt_reltoken(&tty_token);
1569 #endif /* SC_NO_FONT_LOADING */
1575 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, ap->a_cred);
1576 if (error != ENOIOCTL) {
1577 lwkt_reltoken(&tty_token);
1580 error = ttioctl(tp, cmd, data, flag);
1581 if (error != ENOIOCTL) {
1582 lwkt_reltoken(&tty_token);
1585 lwkt_reltoken(&tty_token);
1590 scstart(struct tty *tp)
1594 u_char buf[PCBURST];
1595 scr_stat *scp = SC_STAT(tp->t_dev);
1598 if (scp->status & SLKED ||
1599 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
1604 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1605 tp->t_state |= TS_BUSY;
1608 len = q_to_b(rbp, buf, PCBURST);
1609 sc_puts(scp, buf, len);
1611 tp->t_state &= ~TS_BUSY;
1620 sccnprobe(struct consdev *cp)
1625 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1627 /* a video card is always required */
1628 if (!scvidprobe(unit, flags, TRUE))
1629 cp->cn_pri = CN_DEAD;
1631 /* syscons will become console even when there is no keyboard */
1632 sckbdprobe(unit, flags, TRUE);
1634 if (cp->cn_pri == CN_DEAD) {
1638 /* initialize required fields */
1639 cp->cn_probegood = 1;
1643 sccninit(struct consdev *cp)
1648 sc_get_cons_priority(&unit, &flags);
1649 scinit(unit, flags | SC_KERNEL_CONSOLE);
1650 sc_console_unit = unit;
1651 sc_console = sc_get_softc(unit, SC_KERNEL_CONSOLE)->console_scp;
1655 sccninit_fini(struct consdev *cp)
1657 if (cctl_dev == NULL)
1658 kprintf("sccninit_fini: WARNING: cctl_dev is NULL!\n");
1659 cp->cn_dev = cctl_dev;
1663 sccnterm(struct consdev *cp)
1665 /* we are not the kernel console any more, release everything */
1667 if (sc_console_unit < 0)
1668 return; /* shouldn't happen */
1672 sc_clear_screen(sc_console);
1673 sccnupdate(sc_console);
1676 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1677 sc_console_unit = -1;
1682 * Console path - cannot block!
1685 sccnputc(void *private, int c)
1688 scr_stat *scp = sc_console;
1690 #ifndef SC_NO_HISTORY
1694 #endif /* !SC_NO_HISTORY */
1696 /* assert(sc_console != NULL) */
1699 #ifndef SC_NO_HISTORY
1700 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1701 scp->status &= ~SLKED;
1703 /* This can block, illegal in the console path */
1704 update_kbd_state(scp, scp->status, SLKED, TRUE);
1706 if (scp->status & BUFFER_SAVED) {
1707 if (!sc_hist_restore(scp))
1708 sc_remove_cutmarking(scp);
1709 scp->status &= ~BUFFER_SAVED;
1710 scp->status |= CURSOR_ENABLED;
1711 sc_draw_cursor_image(scp);
1714 tp = VIRTUAL_TTY(scp->sc, scp->index);
1715 /* This can block, illegal in the console path */
1716 if (ISTTYOPEN(tp)) {
1721 #endif /* !SC_NO_HISTORY */
1724 if (kernel_console_ts != NULL)
1725 scp->ts = kernel_console_ts;
1727 sc_puts(scp, buf, 1);
1735 * Console path - cannot block!
1738 sccngetc(void *private)
1740 return sccngetch(0);
1744 * Console path - cannot block!
1747 sccncheckc(void *private)
1749 return sccngetch(SCGETC_NONBLOCK);
1753 * Set polling mode (also disables the tty feed), use when
1754 * polling for console key input.
1757 sccnpoll(void *private, int on)
1762 sc_touch_scrn_saver();
1763 scp = sc_console->sc->cur_scp; /* XXX */
1766 kbd_poll(scp->sc->kbd, on);
1770 sccndbctl(void *private, int on)
1772 /* assert(sc_console_unit >= 0) */
1773 /* try to switch to the kernel console screen */
1774 if (on && debugger == 0) {
1776 * TRY to make sure the screen saver is stopped,
1777 * and the screen is updated before switching to
1782 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1783 && sc_console->smode.mode == VT_AUTO) {
1784 sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
1786 sc_switch_scr(sc_console->sc, sc_console->index);
1797 * Console path - cannot block!
1800 sccngetch(int flags)
1802 static struct fkeytab fkey;
1810 /* assert(sc_console != NULL) */
1813 * Stop the screen saver and update the screen if necessary.
1814 * What if we have been running in the screen saver code... XXX
1816 sc_touch_scrn_saver();
1817 scp = sc_console->sc->cur_scp; /* XXX */
1821 if (fkeycp < fkey.len) {
1822 return fkey.str[fkeycp++];
1825 if (scp->sc->kbd == NULL) {
1830 * Make sure the keyboard is accessible even when the kbd device
1831 * driver is disabled.
1834 kbd_enable(scp->sc->kbd);
1836 /* we shall always use the keyboard in the XLATE mode here */
1837 cur_mode = scp->kbd_mode;
1838 scp->kbd_mode = K_XLATE;
1839 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1841 c = scgetc(scp->sc, SCGETC_CN | flags);
1843 scp->kbd_mode = cur_mode;
1844 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1845 kbd_disable(scp->sc->kbd);
1848 switch (KEYFLAGS(c)) {
1849 case 0: /* normal char */
1851 case FKEY: /* function key */
1852 p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1854 if ((p != NULL) && (fkey.len > 0)) {
1855 bcopy(p, fkey.str, fkey.len);
1861 if (flags & SCGETC_NONBLOCK)
1872 sccnupdate(scr_stat *scp)
1874 /* this is a cut-down version of scrn_timer()... */
1877 * Don't update if videoio is in progress. However, if we are paniced
1878 * or in the debugger it is possible that the variables might be stuck,
1879 * so ignore it in such cases.
1881 if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress) {
1882 if (panicstr == NULL && db_active == 0 && shutdown_in_progress == 0)
1886 if (debugger > 0 || panicstr || db_active || shutdown_in_progress) {
1887 sc_touch_scrn_saver();
1888 } else if (scp != scp->sc->cur_scp) {
1892 if (!run_scrn_saver)
1893 scp->sc->flags &= ~SC_SCRN_IDLE;
1896 * This is a hard path, we cannot call stop_scrn_saver() here.
1898 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1899 if (scp->sc->flags & SC_SCRN_BLANKED) {
1900 sc_touch_scrn_saver();
1901 /*stop_scrn_saver(scp->sc, current_saver);*/
1903 #endif /* NSPLASH */
1905 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1906 || scp->sc->switch_in_progress) {
1911 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1912 * when write_in_progress is non-zero. XXX
1914 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1915 scrn_update(scp, TRUE, SCRN_ASYNCOK);
1919 scrn_timer(void *arg)
1921 static int kbd_interval = 0;
1928 * Setup depending on who called us
1930 again = (arg != NULL);
1932 sc = (sc_softc_t *)arg;
1933 } else if (sc_console != NULL) {
1934 sc = sc_console->sc;
1940 * Don't do anything when we are performing some I/O operations.
1941 * (These are initiated by the frontend?)
1943 if (sc->font_loading_in_progress || sc->videoio_in_progress) {
1945 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1950 * Try to allocate a keyboard automatically
1952 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1953 if (++kbd_interval >= 25) {
1954 sc->keyboard = sc_allocate_keyboard(sc, -1);
1955 if (sc->keyboard >= 0) {
1956 sc->kbd = kbd_get_keyboard(sc->keyboard);
1957 kbd_ioctl(sc->kbd, KDSKBMODE,
1958 (caddr_t)&sc->cur_scp->kbd_mode);
1959 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1967 * Should we stop the screen saver? We need the syscons_lock
1968 * for most of this stuff.
1970 getmicrouptime(&tv);
1972 if (syscons_lock_nonblock() != 0) {
1973 /* failed to get the lock */
1975 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1978 /* successful lock */
1980 if (debugger > 0 || panicstr || shutdown_in_progress)
1981 sc_touch_scrn_saver();
1982 if (run_scrn_saver) {
1983 if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1984 sc->flags |= SC_SCRN_IDLE;
1986 sc->flags &= ~SC_SCRN_IDLE;
1988 sc->scrn_time_stamp = tv.tv_sec;
1989 sc->flags &= ~SC_SCRN_IDLE;
1990 if (scrn_blank_time > 0)
1991 run_scrn_saver = TRUE;
1994 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1995 if (sc->flags & SC_SCRN_BLANKED)
1996 stop_scrn_saver(sc, current_saver);
1997 #endif /* NSPLASH */
1999 /* should we just return ? */
2000 if (sc->blink_in_progress || sc->switch_in_progress ||
2001 sc->write_in_progress)
2005 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
2009 /* Update the screen */
2010 scp = sc->cur_scp; /* cur_scp may have changed... */
2011 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2012 scrn_update(scp, TRUE, SCRN_ASYNCOK);
2015 /* should we activate the screen saver? */
2016 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
2017 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
2018 (*current_saver)(sc, TRUE);
2019 #endif /* NSPLASH */
2023 callout_reset(&sc->scrn_timer_ch, hz / 25, scrn_timer, sc);
2027 and_region(int *s1, int *e1, int s2, int e2)
2029 if (*e1 < s2 || e2 < *s1)
2031 *s1 = imax(*s1, s2);
2032 *e1 = imin(*e1, e2);
2037 * Refresh modified frame buffer range. Also used to
2038 * scroll (entire FB is marked modified).
2040 * This function is allowed to run without the syscons_lock,
2041 * so scp fields can change unexpected. We cache most values
2042 * that we care about and silently discard illegal ranges.
2045 scrn_update(scr_stat *scp, int show_cursor, int flags)
2055 * Try to run large screen updates as an async operation, which will
2056 * call this function again from a thread with BULKUNLOCK set, allowing
2057 * interrupts during the update which tends to make the system behave
2058 * better (no sound glitches, etc).
2060 if ((flags & SCRN_ASYNCOK) && syscons_async && scp->asynctd &&
2061 (scp->end - scp->start) > 16 &&
2062 panicstr == NULL && db_active == 0 && shutdown_in_progress == 0) {
2063 scp->show_cursor = show_cursor;
2064 scp->queue_update_td = 1;
2065 wakeup(&scp->asynctd);
2068 if (flags & SCRN_BULKUNLOCK)
2072 * Synchronous operation or called from
2075 /* assert(scp == scp->sc->cur_scp) */
2076 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2078 cpos = scp->cursor_pos;
2079 copos = scp->cursor_oldpos;
2081 #ifndef SC_NO_CUTPASTE
2082 /* remove the previous mouse pointer image if necessary */
2083 if (scp->status & MOUSE_VISIBLE) {
2085 e = scp->mouse_pos + scp->xsize + 1;
2086 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
2087 || and_region(&s, &e, scp->start, scp->end)
2088 || ((scp->status & CURSOR_ENABLED) &&
2090 (and_region(&s, &e, cpos, cpos)
2091 || and_region(&s, &e, copos, copos)))) {
2092 sc_remove_mouse_image(scp);
2093 if (scp->end >= scp->xsize*scp->ysize)
2094 scp->end = scp->xsize*scp->ysize - 1;
2097 #endif /* !SC_NO_CUTPASTE */
2100 * WARNING: Don't add debugging kprintf()s with syscons locked.
2103 if (scp->end >= scp->xsize*scp->ysize) {
2104 scp->end = scp->xsize*scp->ysize - 1;
2106 if (scp->start < 0) {
2111 * Main update, extract update range and reset
2117 scp->start = scp->xsize * scp->ysize - 1;
2120 if (flags & SCRN_BULKUNLOCK) {
2121 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2124 (*scp->rndr->draw)(scp, start, end - start + 1, FALSE);
2125 if (flags & SCRN_BULKUNLOCK) {
2127 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2131 * Handle cut sequence
2133 s = scp->mouse_cut_start;
2134 e = scp->mouse_cut_end;
2135 cpu_ccfence(); /* allowed to race */
2143 /* does the cut-mark region overlap with the update region? */
2144 if (and_region(&s, &e, start, end)) {
2145 if (flags & SCRN_BULKUNLOCK) {
2146 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2149 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2150 if (flags & SCRN_BULKUNLOCK) {
2152 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2158 /* we are not to show the cursor and the mouse pointer... */
2162 /* update cursor image */
2163 if (scp->status & CURSOR_ENABLED) {
2166 /* did cursor move since last time ? */
2167 if (cpos != copos) {
2168 /* do we need to remove old cursor image ? */
2169 if (!and_region(&s, &e, copos, copos))
2170 sc_remove_cursor_image(scp);
2171 sc_draw_cursor_image(scp);
2173 if (s <= e && and_region(&s, &e, cpos, cpos)) {
2174 /* cursor didn't move, but has been overwritten */
2175 sc_draw_cursor_image(scp);
2176 } else if (scp->sc->flags & SC_BLINK_CURSOR) {
2177 /* if it's a blinking cursor, update it */
2178 (*scp->rndr->blink_cursor)(scp, cpos,
2179 sc_inside_cutmark(scp, cpos));
2184 #ifndef SC_NO_CUTPASTE
2185 /* update "pseudo" mouse pointer image */
2186 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2187 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2188 scp->status &= ~MOUSE_MOVED;
2189 sc_draw_mouse_image(scp);
2192 #endif /* SC_NO_CUTPASTE */
2198 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2199 if (flags & SCRN_BULKUNLOCK)
2204 * Thread handles potentially expensive screen updates. The function is
2205 * expected to operate safely without locks.
2208 scrn_update_thread(void *arg)
2210 scr_stat *scp = arg;
2212 if (scp->queue_update_td == 0) {
2213 tsleep_interlock(&scp->asynctd, 0);
2214 if (scp->queue_update_td == 0)
2215 tsleep(&scp->asynctd, PINTERLOCKED, "wait", 0);
2217 scp->queue_update_td = 0;
2218 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2219 scrn_update(scp, scp->show_cursor, SCRN_BULKUNLOCK);
2220 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2226 scsplash_callback(int event, void *arg)
2231 sc = (sc_softc_t *)arg;
2235 if (add_scrn_saver(scsplash_saver) == 0) {
2236 sc->flags &= ~SC_SAVER_FAILED;
2237 run_scrn_saver = TRUE;
2239 scsplash_stick(TRUE);
2240 (*current_saver)(sc, TRUE);
2246 if (current_saver == scsplash_saver) {
2247 scsplash_stick(FALSE);
2248 error = remove_scrn_saver(scsplash_saver);
2261 scsplash_saver(sc_softc_t *sc, int show)
2263 static int busy = FALSE;
2272 if (!(sc->flags & SC_SAVER_FAILED)) {
2273 if (!(sc->flags & SC_SCRN_BLANKED))
2274 set_scrn_saver_mode(scp, -1, NULL, 0);
2275 switch (splash(sc->adp, TRUE)) {
2276 case 0: /* succeeded */
2278 case EAGAIN: /* try later */
2279 restore_scrn_saver_mode(scp, FALSE);
2280 sc_touch_scrn_saver(); /* XXX */
2283 sc->flags |= SC_SAVER_FAILED;
2284 scsplash_stick(FALSE);
2285 restore_scrn_saver_mode(scp, TRUE);
2286 kprintf("scsplash_saver(): failed to put up the image\n");
2290 } else if (!sticky_splash) {
2291 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2292 restore_scrn_saver_mode(scp, TRUE);
2298 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2303 if (current_saver != none_saver) {
2304 error = remove_scrn_saver(current_saver);
2309 if (current_saver != none_saver) {
2313 run_scrn_saver = FALSE;
2314 saver_mode = CONS_LKM_SAVER;
2315 current_saver = this_saver;
2320 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2322 if (current_saver != this_saver)
2327 * In order to prevent `current_saver' from being called by
2328 * the timeout routine `scrn_timer()' while we manipulate
2329 * the saver list, we shall set `current_saver' to `none_saver'
2330 * before stopping the current saver, rather than blocking by `splXX()'.
2332 current_saver = none_saver;
2334 stop_scrn_saver(this_saver);
2336 /* unblank all blanked screens */
2337 wait_scrn_saver_stop(NULL);
2342 current_saver = none_saver;
2347 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2350 /* assert(scp == scp->sc->cur_scp) */
2352 if (!ISGRAPHSC(scp))
2353 sc_remove_cursor_image(scp);
2354 scp->splash_save_mode = scp->mode;
2355 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2356 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2357 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2358 scp->sc->flags |= SC_SCRN_BLANKED;
2365 if (set_mode(scp) == 0) {
2366 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2367 scp->status |= GRAPHICS_MODE;
2368 #ifndef SC_NO_PALETTE_LOADING
2370 load_palette(scp->sc->adp, pal);
2372 sc_set_border(scp, border);
2376 scp->mode = scp->splash_save_mode;
2377 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2378 scp->status |= scp->splash_save_status;
2386 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2391 /* assert(scp == scp->sc->cur_scp) */
2394 status = scp->status;
2395 scp->mode = scp->splash_save_mode;
2396 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2397 scp->status |= scp->splash_save_status;
2398 scp->sc->flags &= ~SC_SCRN_BLANKED;
2400 if (!ISGRAPHSC(scp))
2401 sc_draw_cursor_image(scp);
2406 if (set_mode(scp) == 0) {
2407 #ifndef SC_NO_PALETTE_LOADING
2408 load_palette(scp->sc->adp, scp->sc->palette);
2415 scp->status = status;
2423 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2425 (*saver)(sc, FALSE);
2426 run_scrn_saver = FALSE;
2427 /* the screen saver may have chosen not to stop after all... */
2428 if (sc->flags & SC_SCRN_BLANKED) {
2432 mark_all(sc->cur_scp);
2433 if (sc->delayed_next_scr)
2434 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2435 wakeup((caddr_t)&scrn_blanked);
2439 wait_scrn_saver_stop(sc_softc_t *sc)
2443 while (scrn_blanked > 0) {
2444 run_scrn_saver = FALSE;
2445 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2449 error = tsleep((caddr_t)&scrn_blanked, PCATCH, "scrsav", 0);
2450 /* May return ERESTART */
2454 run_scrn_saver = FALSE;
2457 #endif /* NSPLASH */
2460 sc_touch_scrn_saver(void)
2462 scsplash_stick(FALSE);
2463 run_scrn_saver = FALSE;
2467 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2472 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2474 /* prevent switch if previously requested */
2475 if (sc->flags & SC_SCRN_VTYLOCK) {
2476 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2477 sc->cur_scp->bell_duration);
2481 /* delay switch if the screen is blanked or being updated */
2482 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2483 || sc->blink_in_progress || sc->videoio_in_progress) {
2484 sc->delayed_next_scr = next_scr + 1;
2485 sc_touch_scrn_saver();
2486 DPRINTF(5, ("switch delayed\n"));
2490 cur_scp = sc->cur_scp;
2493 * we are in the middle of the vty switching process...
2495 * This may be in the console path, be very careful. pfindn() is
2496 * still going to use a spinlock but it no longer uses tokens so
2499 if (sc->switch_in_progress &&
2500 (cur_scp->smode.mode == VT_PROCESS) &&
2502 if (cur_scp->proc != pfindn(cur_scp->pid)) {
2504 * The controlling process has died!!. Do some clean up.
2505 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2506 * are not reset here yet; they will be cleared later.
2508 DPRINTF(5, ("cur_scp controlling process %d died, ", cur_scp->pid));
2509 if (cur_scp->status & SWITCH_WAIT_REL) {
2511 * Force the previous switch to finish, but return now
2515 DPRINTF(5, ("reset WAIT_REL, "));
2516 finish_vt_rel(cur_scp, TRUE);
2517 DPRINTF(5, ("finishing previous switch\n"));
2519 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2520 /* let's assume screen switch has been completed. */
2521 DPRINTF(5, ("reset WAIT_ACQ, "));
2522 finish_vt_acq(cur_scp);
2525 * We are in between screen release and acquisition, and
2526 * reached here via scgetc() or scrn_timer() which has
2527 * interrupted exchange_scr(). Don't do anything stupid.
2529 DPRINTF(5, ("waiting nothing, "));
2533 * The controlling process is alive, but not responding...
2534 * It is either buggy or it may be just taking time.
2535 * The following code is a gross kludge to cope with this
2536 * problem for which there is no clean solution. XXX
2538 if (cur_scp->status & SWITCH_WAIT_REL) {
2539 switch (sc->switch_in_progress++) {
2543 DPRINTF(5, ("sending relsig again, "));
2544 signal_vt_rel(cur_scp);
2551 * Act as if the controlling program returned
2555 DPRINTF(5, ("force reset WAIT_REL, "));
2556 finish_vt_rel(cur_scp, FALSE);
2557 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2560 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2561 switch (sc->switch_in_progress++) {
2565 DPRINTF(5, ("sending acqsig again, "));
2566 signal_vt_acq(cur_scp);
2572 /* clear the flag and finish the previous switch */
2573 DPRINTF(5, ("force reset WAIT_ACQ, "));
2574 finish_vt_acq(cur_scp);
2582 * Return error if an invalid argument is given, or vty switch
2583 * is still in progress.
2585 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2586 || sc->switch_in_progress) {
2587 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2588 DPRINTF(5, ("error 1\n"));
2593 * Don't allow switching away from the graphics mode vty
2594 * if the switch mode is VT_AUTO, unless the next vty is the same
2595 * as the current or the current vty has been closed (but showing).
2597 tp = VIRTUAL_TTY(sc, cur_scp->index);
2598 if ((cur_scp->index != next_scr)
2600 && (cur_scp->smode.mode == VT_AUTO)
2601 && ISGRAPHSC(cur_scp)) {
2602 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2603 DPRINTF(5, ("error, graphics mode\n"));
2608 * Is the wanted vty open? Don't allow switching to a closed vty.
2609 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2610 * Note that we always allow the user to switch to the kernel
2611 * console even if it is closed.
2613 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2614 tp = VIRTUAL_TTY(sc, next_scr);
2615 if (!ISTTYOPEN(tp)) {
2616 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2617 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2620 if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2621 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2626 /* this is the start of vty switching process... */
2627 ++sc->switch_in_progress;
2628 sc->delayed_next_scr = 0;
2629 sc->old_scp = cur_scp;
2630 sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2631 if (sc->new_scp == sc->old_scp) {
2632 sc->switch_in_progress = 0;
2633 wakeup((caddr_t)&sc->new_scp->smode);
2634 DPRINTF(5, ("switch done (new == old)\n"));
2638 /* has controlling process died? */
2639 vt_proc_alive(sc->old_scp);
2640 vt_proc_alive(sc->new_scp);
2642 /* wait for the controlling process to release the screen, if necessary */
2643 if (signal_vt_rel(sc->old_scp)) {
2647 /* go set up the new vty screen */
2650 /* wake up processes waiting for this vty */
2651 wakeup((caddr_t)&sc->cur_scp->smode);
2653 /* wait for the controlling process to acknowledge, if necessary */
2654 if (signal_vt_acq(sc->cur_scp)) {
2658 sc->switch_in_progress = 0;
2659 if (sc->unit == sc_console_unit)
2660 cons_unavail = FALSE;
2661 DPRINTF(5, ("switch done\n"));
2667 do_switch_scr(sc_softc_t *sc)
2669 lwkt_gettoken(&tty_token);
2670 vt_proc_alive(sc->new_scp);
2673 /* sc->cur_scp == sc->new_scp */
2674 wakeup((caddr_t)&sc->cur_scp->smode);
2676 /* wait for the controlling process to acknowledge, if necessary */
2677 if (!signal_vt_acq(sc->cur_scp)) {
2678 sc->switch_in_progress = 0;
2679 if (sc->unit == sc_console_unit)
2680 cons_unavail = FALSE;
2682 lwkt_reltoken(&tty_token);
2686 vt_proc_alive(scr_stat *scp)
2688 lwkt_gettoken(&tty_token);
2690 if (scp->proc == pfindn(scp->pid)) {
2691 lwkt_reltoken(&tty_token);
2695 scp->smode.mode = VT_AUTO;
2696 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2698 lwkt_reltoken(&tty_token);
2703 signal_vt_rel(scr_stat *scp)
2707 lwkt_gettoken(&tty_token);
2708 if (scp->smode.mode != VT_PROCESS) {
2709 lwkt_reltoken(&tty_token);
2712 scp->status |= SWITCH_WAIT_REL;
2715 ksignal(p, scp->smode.relsig);
2717 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2718 lwkt_reltoken(&tty_token);
2724 signal_vt_acq(scr_stat *scp)
2728 lwkt_gettoken(&tty_token);
2729 if (scp->smode.mode != VT_PROCESS) {
2730 lwkt_reltoken(&tty_token);
2733 if (scp->sc->unit == sc_console_unit)
2734 cons_unavail = TRUE;
2735 scp->status |= SWITCH_WAIT_ACQ;
2738 ksignal(p, scp->smode.acqsig);
2740 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2741 lwkt_reltoken(&tty_token);
2747 finish_vt_rel(scr_stat *scp, int release)
2749 lwkt_gettoken(&tty_token);
2750 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2751 scp->status &= ~SWITCH_WAIT_REL;
2753 do_switch_scr(scp->sc);
2755 scp->sc->switch_in_progress = 0;
2756 lwkt_reltoken(&tty_token);
2759 lwkt_reltoken(&tty_token);
2764 finish_vt_acq(scr_stat *scp)
2766 lwkt_gettoken(&tty_token);
2767 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2768 scp->status &= ~SWITCH_WAIT_ACQ;
2769 scp->sc->switch_in_progress = 0;
2770 lwkt_reltoken(&tty_token);
2773 lwkt_reltoken(&tty_token);
2778 exchange_scr(sc_softc_t *sc)
2782 lwkt_gettoken(&tty_token);
2783 /* save the current state of video and keyboard */
2784 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2785 if (!ISGRAPHSC(sc->old_scp))
2786 sc_remove_cursor_image(sc->old_scp);
2787 if (sc->old_scp->kbd_mode == K_XLATE)
2788 save_kbd_state(sc->old_scp, TRUE);
2790 /* set up the video for the new screen */
2791 scp = sc->cur_scp = sc->new_scp;
2792 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2795 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2796 (void *)sc->adp->va_window, FALSE);
2797 scp->status |= MOUSE_HIDDEN;
2798 sc_update_render(scp); /* Switch to kms renderer if necessary */
2799 sc_move_cursor(scp, scp->xpos, scp->ypos);
2800 if (!ISGRAPHSC(scp))
2801 sc_set_cursor_image(scp);
2802 #ifndef SC_NO_PALETTE_LOADING
2803 if (ISGRAPHSC(sc->old_scp))
2804 load_palette(sc->adp, sc->palette);
2806 sc_set_border(scp, scp->border);
2808 /* set up the keyboard for the new screen */
2809 if (sc->old_scp->kbd_mode != scp->kbd_mode)
2810 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2811 update_kbd_state(scp, scp->status, LOCK_MASK, TRUE);
2814 if (scp->sc->fbi != NULL) {
2815 scp->sc->fbi->restore(scp->sc->fbi->cookie);
2817 lwkt_reltoken(&tty_token);
2821 sc_puts(scr_stat *scp, u_char *buf, int len)
2824 /* make screensaver happy */
2825 if (!sticky_splash && scp == scp->sc->cur_scp)
2826 run_scrn_saver = FALSE;
2830 (*scp->tsw->te_puts)(scp, buf, len);
2832 if (scp->sc->delayed_next_scr)
2833 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2838 sc_draw_cursor_image(scr_stat *scp)
2840 /* assert(scp == scp->sc->cur_scp); */
2841 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2842 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2843 scp->sc->flags & SC_BLINK_CURSOR, TRUE,
2844 sc_inside_cutmark(scp, scp->cursor_pos));
2845 scp->cursor_oldpos = scp->cursor_pos;
2846 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2850 sc_remove_cursor_image(scr_stat *scp)
2852 /* assert(scp == scp->sc->cur_scp); */
2853 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2854 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2855 scp->sc->flags & SC_BLINK_CURSOR, FALSE,
2856 sc_inside_cutmark(scp, scp->cursor_oldpos));
2857 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2861 update_cursor_image(scr_stat *scp)
2865 if (scp->sc->flags & SC_CHAR_CURSOR) {
2866 scp->cursor_base = imax(0, scp->sc->cursor_base);
2867 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_height);
2869 scp->cursor_base = 0;
2870 scp->cursor_height = scp->font_height;
2872 blink = scp->sc->flags & SC_BLINK_CURSOR;
2874 /* assert(scp == scp->sc->cur_scp); */
2875 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2876 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE,
2877 sc_inside_cutmark(scp, scp->cursor_pos));
2878 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
2879 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE,
2880 sc_inside_cutmark(scp, scp->cursor_pos));
2881 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2885 sc_set_cursor_image(scr_stat *scp)
2887 if (scp->sc->flags & SC_CHAR_CURSOR) {
2888 scp->cursor_base = imax(0, scp->sc->cursor_base);
2889 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_height);
2891 scp->cursor_base = 0;
2892 scp->cursor_height = scp->font_height;
2895 /* assert(scp == scp->sc->cur_scp); */
2896 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2897 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
2898 scp->sc->flags & SC_BLINK_CURSOR);
2899 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2903 scinit(int unit, int flags)
2906 * When syscons is being initialized as the kernel console, malloc()
2907 * is not yet functional, because various kernel structures has not been
2908 * fully initialized yet. Therefore, we need to declare the following
2909 * static buffers for the console. This is less than ideal,
2910 * but is necessry evil for the time being. XXX
2912 static scr_stat main_console;
2913 static u_short sc_buffer[ROW*COL]; /* XXX */
2914 #ifndef SC_NO_FONT_LOADING
2915 static u_char font_8[256*8];
2916 static u_char font_14[256*14];
2917 static u_char font_16[256*16];
2922 video_adapter_t *adp;
2927 /* one time initialization */
2928 if (init_done == COLD)
2929 sc_get_bios_values(&bios_value);
2933 * Allocate resources. Even if we are being called for the second
2934 * time, we must allocate them again, because they might have
2937 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2939 if (sc->adapter >= 0) {
2940 vid_release(sc->adp, (void *)&sc->adapter);
2944 if (sc->keyboard >= 0) {
2945 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2946 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2947 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2948 if (sc->kbd != NULL) {
2949 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2950 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2954 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2955 sc->adp = vid_get_adapter(sc->adapter);
2956 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2957 sc->keyboard = sc_allocate_keyboard(sc, unit);
2958 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2959 sc->kbd = kbd_get_keyboard(sc->keyboard);
2960 if (sc->kbd != NULL) {
2961 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2962 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2965 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2967 sc->initial_mode = sc->adp->va_initial_mode;
2969 #ifndef SC_NO_FONT_LOADING
2970 if (flags & SC_KERNEL_CONSOLE) {
2971 sc->font_8 = font_8;
2972 sc->font_14 = font_14;
2973 sc->font_16 = font_16;
2974 } else if (sc->font_8 == NULL) {
2975 /* assert(sc_malloc) */
2976 sc->font_8 = kmalloc(sizeof(font_8), M_SYSCONS, M_WAITOK);
2977 sc->font_14 = kmalloc(sizeof(font_14), M_SYSCONS, M_WAITOK);
2978 sc->font_16 = kmalloc(sizeof(font_16), M_SYSCONS, M_WAITOK);
2982 lwkt_gettoken(&tty_token);
2983 /* extract the hardware cursor location and hide the cursor for now */
2984 (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2985 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2986 lwkt_reltoken(&tty_token);
2988 /* set up the first console */
2989 sc->first_vty = unit*MAXCONS;
2990 sc->vtys = MAXCONS; /* XXX: should be configurable */
2991 if (flags & SC_KERNEL_CONSOLE) {
2992 scp = &main_console;
2993 sc->console_scp = scp;
2994 init_scp(sc, sc->first_vty, scp);
2995 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2996 (void *)sc_buffer, FALSE);
2997 if (sc_init_emulator(scp, SC_DFLT_TERM))
2998 sc_init_emulator(scp, "*");
2999 (*scp->tsw->te_default_attr)(scp,
3000 kernel_default.std_color,
3001 kernel_default.rev_color);
3003 /* assert(sc_malloc) */
3004 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK | M_ZERO);
3006 sc->dev[0] = make_dev(&sc_ops, unit*MAXCONS, UID_ROOT,
3007 GID_WHEEL, 0600, "ttyv%r", unit*MAXCONS);
3009 sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
3010 scp = alloc_scp(sc, sc->first_vty);
3011 sc->dev[0]->si_drv1 = scp;
3015 /* copy screen to temporary buffer */
3016 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3017 (void *)scp->sc->adp->va_window, FALSE);
3019 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
3021 /* move cursors to the initial positions */
3022 if (col >= scp->xsize)
3024 if (row >= scp->ysize)
3025 row = scp->ysize - 1;
3028 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
3029 if (bios_value.cursor_end < scp->font_height)
3030 sc->cursor_base = scp->font_height - bios_value.cursor_end - 1;
3032 sc->cursor_base = 0;
3033 i = bios_value.cursor_end - bios_value.cursor_start + 1;
3034 sc->cursor_height = imin(i, scp->font_height);
3035 #ifndef SC_NO_SYSMOUSE
3036 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
3038 if (!ISGRAPHSC(scp)) {
3039 sc_set_cursor_image(scp);
3040 sc_draw_cursor_image(scp);
3043 /* save font and palette */
3044 #ifndef SC_NO_FONT_LOADING
3045 sc->fonts_loaded = 0;
3046 if (ISFONTAVAIL(sc->adp->va_flags)) {
3048 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
3049 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
3050 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
3051 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
3052 if (scp->font_height < 14) {
3053 sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
3054 } else if (scp->font_height >= 16) {
3055 sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
3057 sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
3059 #else /* !SC_DFLT_FONT */
3060 if (scp->font_height < 14) {
3061 sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
3062 sc->fonts_loaded = FONT_8;
3063 } else if (scp->font_height >= 16) {
3064 sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
3065 sc->fonts_loaded = FONT_16;
3067 sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
3068 sc->fonts_loaded = FONT_14;
3070 #endif /* SC_DFLT_FONT */
3071 /* FONT KLUDGE: always use the font page #0. XXX */
3072 sc_show_font(scp, 0);
3074 #endif /* !SC_NO_FONT_LOADING */
3076 #ifndef SC_NO_PALETTE_LOADING
3077 save_palette(sc->adp, sc->palette);
3081 if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
3082 /* we are ready to put up the splash image! */
3083 splash_init(sc->adp, scsplash_callback, sc);
3084 sc->flags |= SC_SPLASH_SCRN;
3086 #endif /* NSPLASH */
3089 /* the rest is not necessary, if we have done it once */
3090 if (sc->flags & SC_INIT_DONE) {
3094 /* initialize mapscrn arrays to a one to one map */
3095 for (i = 0; i < sizeof(sc->scr_map); i++)
3096 sc->scr_map[i] = sc->scr_rmap[i] = i;
3098 sc->flags |= SC_INIT_DONE;
3102 scterm(int unit, int flags)
3107 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3109 return; /* shouldn't happen */
3111 lwkt_gettoken(&tty_token);
3113 /* this console is no longer available for the splash screen */
3114 if (sc->flags & SC_SPLASH_SCRN) {
3115 splash_term(sc->adp);
3116 sc->flags &= ~SC_SPLASH_SCRN;
3118 #endif /* NSPLASH */
3121 /* move the hardware cursor to the upper-left corner */
3122 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
3125 /* release the keyboard and the video card */
3126 if (sc->keyboard >= 0)
3127 kbd_release(sc->kbd, &sc->keyboard);
3128 if (sc->adapter >= 0)
3129 vid_release(sc->adp, &sc->adapter);
3132 * Stop the terminal emulator, if any. If operating on the
3133 * kernel console sc->dev may not be setup yet.
3135 if (flags & SC_KERNEL_CONSOLE)
3136 scp = sc->console_scp;
3138 scp = SC_STAT(sc->dev[0]);
3140 (*scp->tsw->te_term)(scp, &scp->ts);
3141 if (scp->ts != NULL)
3142 kfree(scp->ts, M_SYSCONS);
3144 /* clear the structure */
3145 if (!(flags & SC_KERNEL_CONSOLE)) {
3146 /* XXX: We need delete_dev() for this */
3147 kfree(sc->dev, M_SYSCONS);
3149 /* XXX: We need a ttyunregister for this */
3150 kfree(sc->tty, M_SYSCONS);
3152 #ifndef SC_NO_FONT_LOADING
3153 kfree(sc->font_8, M_SYSCONS);
3154 kfree(sc->font_14, M_SYSCONS);
3155 kfree(sc->font_16, M_SYSCONS);
3157 /* XXX vtb, history */
3159 bzero(sc, sizeof(*sc));
3162 lwkt_reltoken(&tty_token);
3166 scshutdown(void *arg, int howto)
3168 /* assert(sc_console != NULL) */
3170 lwkt_gettoken(&tty_token);
3172 sc_touch_scrn_saver();
3173 if (!cold && sc_console
3174 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
3175 && sc_console->smode.mode == VT_AUTO) {
3176 sc_switch_scr(sc_console->sc, sc_console->index);
3178 shutdown_in_progress = TRUE;
3180 lwkt_reltoken(&tty_token);
3184 sc_clean_up(scr_stat *scp)
3188 #endif /* NSPLASH */
3190 lwkt_gettoken(&tty_token);
3191 if (scp->sc->flags & SC_SCRN_BLANKED) {
3192 sc_touch_scrn_saver();
3194 if ((error = wait_scrn_saver_stop(scp->sc))) {
3195 lwkt_reltoken(&tty_token);
3198 #endif /* NSPLASH */
3200 scp->status |= MOUSE_HIDDEN;
3201 sc_remove_mouse_image(scp);
3202 sc_remove_cutmarking(scp);
3203 lwkt_reltoken(&tty_token);
3208 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3213 lwkt_gettoken(&tty_token);
3215 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3216 if (!discard && (old.vtb_flags & VTB_VALID)) {
3217 /* retain the current cursor position and buffer contants */
3218 scp->cursor_oldpos = scp->cursor_pos;
3220 * This works only if the old buffer has the same size as or larger
3221 * than the new one. XXX
3223 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3227 sc_vtb_destroy(&old);
3230 #ifndef SC_NO_SYSMOUSE
3231 /* move the mouse cursor at the center of the screen */
3232 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3234 lwkt_reltoken(&tty_token);
3238 alloc_scp(sc_softc_t *sc, int vty)
3242 /* assert(sc_malloc) */
3244 scp = kmalloc(sizeof(scr_stat), M_SYSCONS, M_WAITOK);
3245 init_scp(sc, vty, scp);
3247 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3248 if (sc_init_emulator(scp, SC_DFLT_TERM))
3249 sc_init_emulator(scp, "*");
3251 #ifndef SC_NO_CUTPASTE
3252 sc_alloc_cut_buffer(scp, TRUE);
3255 #ifndef SC_NO_HISTORY
3256 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3262 * NOTE: Must be called with tty_token held.
3265 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3268 int scaled_font_height;
3270 bzero(scp, sizeof(*scp));
3275 scp->mode = sc->initial_mode;
3276 callout_init_mp(&scp->blink_screen_ch);
3277 lwkt_gettoken(&tty_token);
3278 (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
3279 lwkt_reltoken(&tty_token);
3280 if (info.vi_flags & V_INFO_GRAPHICS) {
3281 scp->status |= GRAPHICS_MODE;
3282 scp->xpixel = info.vi_width;
3283 scp->ypixel = info.vi_height;
3284 scp->xsize = info.vi_width/8;
3285 scp->ysize = info.vi_height/info.vi_cheight;
3286 scp->font_height = 0;
3287 scp->font_width = 0;
3290 scp->xsize = info.vi_width;
3291 scp->ysize = info.vi_height;
3292 scp->xpixel = scp->xsize*8;
3293 scp->ypixel = scp->ysize*info.vi_cheight;
3294 scp->font_width = 8;
3295 if (info.vi_cheight < 14) {
3296 scp->font_height = 8;
3297 #ifndef SC_NO_FONT_LOADING
3298 scp->font = sc->font_8;
3302 } else if (info.vi_cheight >= 16) {
3303 scp->font_height = 16;
3304 #ifndef SC_NO_FONT_LOADING
3305 scp->font = sc->font_16;
3310 scp->font_height = 14;
3311 #ifndef SC_NO_FONT_LOADING
3312 scp->font = sc->font_14;
3318 scp->xoff = scp->yoff = 0;
3319 scp->xpos = scp->ypos = 0;
3321 if (scp->fbi != NULL) {
3322 scp->xpixel = scp->fbi->width;
3323 scp->ypixel = scp->fbi->height;
3325 scp->blk_width = scp->xpixel / 80;
3326 scaled_font_height = scp->blk_width * 100 / scp->font_width;
3327 scp->blk_height = scp->ypixel * 100 / scaled_font_height;
3329 scp->xsize = scp->xpixel / scp->blk_width;
3330 scp->ysize = scp->ypixel / scp->blk_height;
3331 scp->xpad = scp->fbi->stride / 4 - scp->xsize * scp->blk_width;
3333 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3334 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3335 scp->start = scp->xsize * scp->ysize - 1;
3340 scp->border = BG_BLACK;
3341 scp->cursor_base = sc->cursor_base;
3342 scp->cursor_height = imin(sc->cursor_height, scp->font_height);
3343 scp->mouse_cut_start = scp->xsize * scp->ysize;
3344 scp->mouse_cut_end = -1;
3345 scp->mouse_signal = 0;
3347 scp->mouse_proc = NULL;
3348 scp->kbd_mode = K_XLATE;
3349 scp->bell_pitch = bios_value.bell_pitch;
3350 scp->bell_duration = BELL_DURATION;
3351 scp->status |= (bios_value.shift_state & NLKED);
3352 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3355 scp->smode.mode = VT_AUTO;
3356 scp->history = NULL;
3357 scp->history_pos = 0;
3358 scp->history_size = 0;
3362 sc_init_emulator(scr_stat *scp, char *name)
3369 if (name == NULL) /* if no name is given, use the current emulator */
3371 else /* ...otherwise find the named emulator */
3372 sw = sc_term_match(name);
3378 if (strcmp(sw->te_renderer, "*") != 0) {
3379 rndr = sc_render_match(scp, sw->te_renderer, scp->model);
3381 if (rndr == NULL && scp->sc->fbi != NULL) {
3382 rndr = sc_render_match(scp, "kms", scp->model);
3385 rndr = sc_render_match(scp, scp->sc->adp->va_name, scp->model);
3391 if (sw == scp->tsw) {
3392 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3394 sc_clear_screen(scp);
3395 /* assert(error == 0); */
3399 if (sc_malloc && (sw->te_size > 0))
3400 p = kmalloc(sw->te_size, M_SYSCONS, M_NOWAIT);
3403 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3409 (*scp->tsw->te_term)(scp, &scp->ts);
3410 if (scp->ts != NULL)
3411 kfree(scp->ts, M_SYSCONS);
3417 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3418 sc_clear_screen(scp);
3424 * scgetc(flags) - get character from keyboard.
3425 * If flags & SCGETC_CN, then avoid harmful side effects.
3426 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3427 * return NOKEY if there is nothing there.
3430 scgetc(sc_softc_t *sc, u_int flags)
3433 #ifndef SC_NO_HISTORY
3441 lwkt_gettoken(&tty_token);
3442 if (sc->kbd == NULL) {
3443 lwkt_reltoken(&tty_token);
3449 /* I don't like this, but... XXX */
3450 if (flags & SCGETC_CN) {
3452 sccnupdate(sc->cur_scp);
3457 /* first see if there is something in the keyboard port */
3459 c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3461 if (!(flags & SCGETC_CN))
3462 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3463 } else if (c == NOKEY) {
3464 lwkt_reltoken(&tty_token);
3471 /* make screensaver happy */
3473 sc_touch_scrn_saver();
3475 if (!(flags & SCGETC_CN))
3476 /* do the /dev/random device a favour */
3477 add_keyboard_randomness(c);
3479 if (scp->kbd_mode != K_XLATE) {
3480 lwkt_reltoken(&tty_token);
3484 /* if scroll-lock pressed allow history browsing */
3485 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3487 scp->status &= ~CURSOR_ENABLED;
3488 sc_remove_cursor_image(scp);
3490 #ifndef SC_NO_HISTORY
3491 if (!(scp->status & BUFFER_SAVED)) {
3492 scp->status |= BUFFER_SAVED;
3496 /* FIXME: key codes */
3497 case SPCLKEY | FKEY | F(49): /* home key */
3498 sc_remove_cutmarking(scp);
3502 case SPCLKEY | FKEY | F(57): /* end key */
3503 sc_remove_cutmarking(scp);
3507 case SPCLKEY | FKEY | F(50): /* up arrow key */
3508 sc_remove_cutmarking(scp);
3509 if (sc_hist_up_line(scp))
3510 if (!(flags & SCGETC_CN))
3511 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3514 case SPCLKEY | FKEY | F(58): /* down arrow key */
3515 sc_remove_cutmarking(scp);
3516 if (sc_hist_down_line(scp))
3517 if (!(flags & SCGETC_CN))
3518 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3521 case SPCLKEY | FKEY | F(51): /* page up key */
3522 sc_remove_cutmarking(scp);
3523 for (i=0; i<scp->ysize; i++) {
3524 if (sc_hist_up_line(scp)) {
3525 if (!(flags & SCGETC_CN))
3526 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3532 case SPCLKEY | FKEY | F(59): /* page down key */
3533 sc_remove_cutmarking(scp);
3534 for (i=0; i<scp->ysize; i++) {
3535 if (sc_hist_down_line(scp)) {
3536 if (!(flags & SCGETC_CN))
3537 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3543 #endif /* SC_NO_HISTORY */
3547 * Process and consume special keys here. Return a plain char code
3548 * or a char code with the META flag or a function key code.
3552 /* goto next_code */
3557 switch (KEYCHAR(c)) {
3559 case NLK: case CLK: case ALK:
3562 kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3564 scp->status |= SLKED;
3566 if (scp->status & SLKED) {
3567 scp->status &= ~SLKED;
3568 #ifndef SC_NO_HISTORY
3569 if (scp->status & BUFFER_SAVED) {
3570 if (!sc_hist_restore(scp))
3571 sc_remove_cutmarking(scp);
3572 scp->status &= ~BUFFER_SAVED;
3573 scp->status |= CURSOR_ENABLED;
3574 sc_draw_cursor_image(scp);
3576 tp = VIRTUAL_TTY(sc, scp->index);
3584 /* NON-LOCKING KEYS */
3586 case LSH: case RSH: case LCTR: case RCTR:
3587 case LALT: case RALT: case ASH: case META:
3591 if (!(sc->flags & SC_SCRN_BLANKED)) {
3592 lwkt_reltoken(&tty_token);
3599 /* force activatation/deactivation of the screen saver */
3600 if (!(sc->flags & SC_SCRN_BLANKED)) {
3601 run_scrn_saver = TRUE;
3602 sc->scrn_time_stamp -= scrn_blank_time;
3606 * While devices are being probed, the screen saver need
3607 * to be invoked explictly. XXX
3609 if (sc->flags & SC_SCRN_BLANKED) {
3610 scsplash_stick(FALSE);
3611 stop_scrn_saver(sc, current_saver);
3613 if (!ISGRAPHSC(scp)) {
3614 scsplash_stick(TRUE);
3615 (*current_saver)(sc, TRUE);
3619 #endif /* NSPLASH */
3623 #ifndef SC_DISABLE_REBOOT
3629 #ifndef SC_DISABLE_REBOOT
3630 shutdown_nice(RB_HALT);
3635 #ifndef SC_DISABLE_REBOOT
3636 shutdown_nice(RB_HALT|RB_POWEROFF);
3645 #ifndef SC_DISABLE_DDBKEY
3647 lwkt_reltoken(&tty_token);
3648 Debugger("manual escape to debugger");
3649 lwkt_gettoken(&tty_token);
3651 kprintf("No debugger in kernel\n");
3653 #else /* SC_DISABLE_DDBKEY */
3655 #endif /* SC_DISABLE_DDBKEY */
3659 if (enable_panic_key)
3660 panic("Forced by the panic key");
3664 this_scr = scp->index;
3665 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3666 sc->first_vty + i != this_scr;
3667 i = (i + 1)%sc->vtys) {
3668 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3669 if (ISTTYOPEN(tp)) {
3671 sc_switch_scr(scp->sc, sc->first_vty + i);
3679 this_scr = scp->index;
3680 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3681 sc->first_vty + i != this_scr;
3682 i = (i + sc->vtys - 1)%sc->vtys) {
3683 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3684 if (ISTTYOPEN(tp)) {
3686 sc_switch_scr(scp->sc, sc->first_vty + i);
3694 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3696 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3700 /* assert(c & FKEY) */
3701 if (!(sc->flags & SC_SCRN_BLANKED)) {
3702 lwkt_reltoken(&tty_token);
3707 /* goto next_code */
3709 /* regular keys (maybe MKEY is set) */
3710 if (!(sc->flags & SC_SCRN_BLANKED)) {
3711 lwkt_reltoken(&tty_token);
3721 scmmap(struct dev_mmap_args *ap)
3725 lwkt_gettoken(&tty_token);
3726 scp = SC_STAT(ap->a_head.a_dev);
3727 if (scp != scp->sc->cur_scp) {
3728 lwkt_reltoken(&tty_token);
3731 ap->a_result = (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, ap->a_offset,
3733 lwkt_reltoken(&tty_token);
3738 save_kbd_state(scr_stat *scp, int unlock)
3743 WANT_UNLOCK(unlock);
3744 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3747 if (error == ENOIOCTL)
3750 scp->status &= ~LOCK_MASK;
3751 scp->status |= state;
3757 update_kbd_state(scr_stat *scp, int new_bits, int mask, int unlock)
3762 if (mask != LOCK_MASK) {
3763 WANT_UNLOCK(unlock);
3764 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3767 if (error == ENOIOCTL)
3773 state |= new_bits & mask;
3775 state = new_bits & LOCK_MASK;
3777 WANT_UNLOCK(unlock);
3778 error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3780 if (error == ENOIOCTL)
3786 update_kbd_leds(scr_stat *scp, int which)
3791 error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3792 if (error == ENOIOCTL)
3798 set_mode(scr_stat *scp)
3802 lwkt_gettoken(&tty_token);
3803 /* reject unsupported mode */
3804 if (scp->sc->fbi == NULL && (*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info)) {
3805 lwkt_reltoken(&tty_token);
3809 /* if this vty is not currently showing, do nothing */
3810 if (scp != scp->sc->cur_scp) {
3811 lwkt_reltoken(&tty_token);
3815 /* setup video hardware for the given mode */
3816 if (scp->sc->fbi == NULL)
3817 (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3818 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3819 (void *)scp->sc->adp->va_window, FALSE);
3820 if (scp->sc->fbi != NULL)
3823 #ifndef SC_NO_FONT_LOADING
3824 /* load appropriate font */
3825 if (!(scp->status & GRAPHICS_MODE)) {
3826 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3827 if (scp->font_height < 14) {
3828 if (scp->sc->fonts_loaded & FONT_8)
3829 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
3830 } else if (scp->font_height >= 16) {
3831 if (scp->sc->fonts_loaded & FONT_16)
3832 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
3834 if (scp->sc->fonts_loaded & FONT_14)
3835 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
3839 * This is an interim kludge to display correct font.
3840 * Always use the font page #0 on the video plane 2.
3841 * Somehow we cannot show the font in other font pages on
3842 * some video cards... XXX
3844 sc_show_font(scp, 0);
3848 #endif /* !SC_NO_FONT_LOADING */
3850 sc_set_border(scp, scp->border);
3851 sc_set_cursor_image(scp);
3854 lwkt_reltoken(&tty_token);
3859 refresh_ega_palette(scr_stat *scp)
3863 int rsize, gsize, bsize;
3864 int rfld, gfld, bfld;
3867 rsize = scp->sc->adp->va_info.vi_pixel_fsizes[0];
3868 gsize = scp->sc->adp->va_info.vi_pixel_fsizes[1];
3869 bsize = scp->sc->adp->va_info.vi_pixel_fsizes[2];
3870 rfld = scp->sc->adp->va_info.vi_pixel_fields[0];
3871 gfld = scp->sc->adp->va_info.vi_pixel_fields[1];
3872 bfld = scp->sc->adp->va_info.vi_pixel_fields[2];
3874 for (i = 0; i < 16; i++) {
3875 reg = scp->sc->adp->va_palette_regs[i];
3877 r = scp->sc->palette[reg * 3] >> (8 - rsize);
3878 g = scp->sc->palette[reg * 3 + 1] >> (8 - gsize);
3879 b = scp->sc->palette[reg * 3 + 2] >> (8 - bsize);
3881 scp->ega_palette[i] = (r << rfld) + (g << gfld) + (b << bfld);
3886 sc_set_border(scr_stat *scp, int color)
3888 atomic_add_int(&scp->sc->videoio_in_progress, 1);
3889 (*scp->rndr->draw_border)(scp, color);
3890 atomic_add_int(&scp->sc->videoio_in_progress, -1);
3893 #ifndef SC_NO_FONT_LOADING
3895 sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
3896 int base, int count)
3901 sc->font_loading_in_progress = TRUE;
3902 (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
3903 sc->font_loading_in_progress = FALSE;
3907 sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
3908 int base, int count)
3913 sc->font_loading_in_progress = TRUE;
3914 (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
3915 sc->font_loading_in_progress = FALSE;
3919 sc_show_font(scr_stat *scp, int page)
3921 (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3923 #endif /* !SC_NO_FONT_LOADING */
3926 sc_paste(scr_stat *scp, u_char *p, int count)
3932 * Holy hell, don't try to inject a paste buffer if the keyboard
3933 * is not in ascii mode!
3935 if (scp->kbd_mode != K_XLATE)
3938 lwkt_gettoken(&tty_token);
3939 if (scp->status & MOUSE_VISIBLE) {
3940 tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3941 if (!ISTTYOPEN(tp)) {
3942 lwkt_reltoken(&tty_token);
3945 rmap = scp->sc->scr_rmap;
3946 for (; count > 0; --count)
3947 (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3949 lwkt_reltoken(&tty_token);
3953 sc_bell(scr_stat *scp, int pitch, int duration)
3955 if (cold || shutdown_in_progress)
3958 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL)) {
3962 if (scp->sc->flags & SC_VISUAL_BELL) {
3963 if (scp->sc->blink_in_progress) {
3966 scp->sc->blink_in_progress = 3;
3967 if (scp != scp->sc->cur_scp)
3968 scp->sc->blink_in_progress += 2;
3969 sc_blink_screen(scp->sc->cur_scp);
3970 } else if (duration != 0 && pitch != 0) {
3971 if (scp != scp->sc->cur_scp)
3973 sysbeep(pitch, duration);
3978 * Two versions of blink_screen(), one called from the console path
3979 * with the syscons locked, and one called from a timer callout.
3982 sc_blink_screen(scr_stat *scp)
3984 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3985 scp->sc->blink_in_progress = 0;
3987 if (scp->sc->delayed_next_scr)
3988 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3990 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3991 scp->sc->blink_in_progress & 1);
3992 scp->sc->blink_in_progress--;
3998 blink_screen_callout(void *arg)
4000 scr_stat *scp = arg;
4003 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
4005 scp->sc->blink_in_progress = 0;
4008 tp = VIRTUAL_TTY(scp->sc, scp->index);
4011 if (scp->sc->delayed_next_scr) {
4013 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
4018 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
4019 scp->sc->blink_in_progress & 1);
4020 scp->sc->blink_in_progress--;
4022 callout_reset(&scp->blink_screen_ch, hz / 10,
4023 blink_screen_callout, scp);
4029 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
4030 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
4035 sc_allocate_keyboard(sc_softc_t *sc, int unit)
4041 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
4043 k0 = kbd_get_keyboard(idx0);
4045 for (idx = kbd_find_keyboard2("*", -1, 0, 0);
4047 idx = kbd_find_keyboard2("*", -1, idx + 1, 0)) {
4048 k = kbd_get_keyboard(idx);
4050 if (idx == idx0 || KBD_IS_BUSY(k))
4053 bzero(&ki, sizeof(ki));
4054 strcpy(ki.kb_name, k->kb_name);
4055 ki.kb_unit = k->kb_unit;
4057 kbd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
4060 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);