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.
31 * $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 $
34 #include "use_splash.h"
35 #include "opt_syscons.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/eventhandler.h>
44 #include <sys/reboot.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/kernel.h>
53 #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>
63 #include <machine/apm_bios.h>
65 #include <machine/frame.h>
67 #include <dev/misc/kbd/kbdreg.h>
68 #include <dev/video/fb/fbreg.h>
69 #include <dev/video/fb/splashreg.h>
75 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
76 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
78 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
80 MALLOC_DEFINE(M_SYSCONS, "syscons", "Syscons");
82 typedef struct default_attr {
83 int std_color; /* normal hardware color */
84 int rev_color; /* reverse hardware color */
87 static default_attr user_default = {
92 static default_attr kernel_default = {
94 SC_KERNEL_CONS_REV_ATTR,
97 static int sc_console_unit = -1;
98 static scr_stat *sc_console;
99 static struct tty *sc_console_tty;
100 static void *kernel_console_ts;
102 static char init_done = COLD;
103 static char shutdown_in_progress = FALSE;
104 static char sc_malloc = FALSE;
106 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
107 static int run_scrn_saver = FALSE; /* should run the saver? */
108 static long scrn_blank_time = 0; /* screen saver timeout value */
110 static int scrn_blanked; /* # of blanked screen */
111 static int sticky_splash = FALSE;
113 static void none_saver(sc_softc_t *sc, int blank) { }
114 static void (*current_saver)(sc_softc_t *, int) = none_saver;
117 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
121 static bios_values_t bios_value;
123 static int enable_panic_key;
124 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
127 #define SC_CONSOLECTL 255
129 #define VIRTUAL_TTY(sc, x) ((SC_DEV((sc),(x)) != NULL) ? \
130 (SC_DEV((sc),(x))->si_tty) : NULL)
131 #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
134 static cdev_t cctl_dev;
135 static timeout_t blink_screen_callout;
136 static void sc_blink_screen(scr_stat *scp);
137 static struct mtx syscons_mtx = MTX_INITIALIZER;
140 static int scvidprobe(int unit, int flags, int cons);
141 static int sckbdprobe(int unit, int flags, int cons);
142 static void scmeminit(void *arg);
143 static int scdevtounit(cdev_t dev);
144 static kbd_callback_func_t sckbdevent;
145 static int scparam(struct tty *tp, struct termios *t);
146 static void scstart(struct tty *tp);
147 static void scinit(int unit, int flags);
148 static void scterm(int unit, int flags);
149 static void scshutdown(void *arg, int howto);
150 static void sc_puts(scr_stat *scp, u_char *buf, int len);
151 static u_int scgetc(sc_softc_t *sc, u_int flags);
153 #define SCGETC_NONBLOCK 2
154 static int sccngetch(int flags);
155 static void sccnupdate(scr_stat *scp);
156 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
157 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
158 static timeout_t scrn_timer;
159 static int and_region(int *s1, int *e1, int s2, int e2);
160 static void scrn_update(scr_stat *scp, int show_cursor);
163 static int scsplash_callback(int event, void *arg);
164 static void scsplash_saver(sc_softc_t *sc, int show);
165 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
166 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
167 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
168 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
169 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
170 static int wait_scrn_saver_stop(sc_softc_t *sc);
171 #define scsplash_stick(stick) (sticky_splash = (stick))
173 #define scsplash_stick(stick)
176 static void do_switch_scr(sc_softc_t *sc);
177 static int vt_proc_alive(scr_stat *scp);
178 static int signal_vt_rel(scr_stat *scp);
179 static int signal_vt_acq(scr_stat *scp);
180 static int finish_vt_rel(scr_stat *scp, int release);
181 static int finish_vt_acq(scr_stat *scp);
182 static void exchange_scr(sc_softc_t *sc);
183 static void update_cursor_image(scr_stat *scp);
184 static int save_kbd_state(scr_stat *scp);
185 static int update_kbd_state(scr_stat *scp, int state, int mask);
186 static int update_kbd_leds(scr_stat *scp, int which);
187 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
190 * Console locking support functions.
192 * We use mutex spinlocks here in order to allow reentrancy which should
193 * issues during panics.
198 mtx_spinlock(&syscons_mtx);
202 * Returns 0 on success, EAGAIN on failure.
205 syscons_lock_nonblock(void)
207 return(mtx_spinlock_try(&syscons_mtx));
213 mtx_spinunlock(&syscons_mtx);
219 static cn_probe_t sccnprobe;
220 static cn_init_t sccninit;
221 static cn_init_t sccninit_fini;
222 static cn_getc_t sccngetc;
223 static cn_checkc_t sccncheckc;
224 static cn_putc_t sccnputc;
225 static cn_dbctl_t sccndbctl;
226 static cn_term_t sccnterm;
228 CONS_DRIVER(sc, sccnprobe, sccninit, sccninit_fini, sccnterm,
229 sccngetc, sccncheckc, sccnputc, sccndbctl);
231 static d_open_t scopen;
232 static d_close_t scclose;
233 static d_read_t scread;
234 static d_ioctl_t scioctl;
235 static d_mmap_t scmmap;
237 static struct dev_ops sc_ops = {
245 .d_kqfilter = ttykqfilter,
246 .d_revoke = ttyrevoke
250 sc_probe_unit(int unit, int flags)
252 if (!scvidprobe(unit, flags, FALSE)) {
254 kprintf("sc%d: no video adapter found.\n", unit);
258 /* syscons will be attached even when there is no keyboard */
259 sckbdprobe(unit, flags, FALSE);
264 /* probe video adapters, return TRUE if found */
266 scvidprobe(int unit, int flags, int cons)
269 * Access the video adapter driver through the back door!
270 * Video adapter drivers need to be configured before syscons.
271 * However, when syscons is being probed as the low-level console,
272 * they have not been initialized yet. We force them to initialize
273 * themselves here. XXX
275 vid_configure(cons ? VIO_PROBE_ONLY : 0);
277 return (vid_find_adapter("*", unit) >= 0);
280 /* probe the keyboard, return TRUE if found */
282 sckbdprobe(int unit, int flags, int cons)
284 /* access the keyboard driver through the backdoor! */
285 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
287 return (kbd_find_keyboard("*", unit) >= 0);
291 adapter_name(video_adapter_t *adp)
297 { KD_MONO, { "MDA", "MDA" } },
298 { KD_HERCULES, { "Hercules", "Hercules" } },
299 { KD_CGA, { "CGA", "CGA" } },
300 { KD_EGA, { "EGA", "EGA (mono)" } },
301 { KD_VGA, { "VGA", "VGA (mono)" } },
302 { KD_TGA, { "TGA", "TGA" } },
303 { -1, { "Unknown", "Unknown" } },
307 for (i = 0; names[i].type != -1; ++i)
308 if (names[i].type == adp->va_type)
310 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
314 sc_attach_unit(int unit, int flags)
323 flags &= ~SC_KERNEL_CONSOLE;
325 if (sc_console_unit == unit) {
327 * If this unit is being used as the system console, we need to
328 * adjust some variables and buffers before and after scinit().
330 /* assert(sc_console != NULL) */
331 flags |= SC_KERNEL_CONSOLE;
336 if (sc_console->tsw->te_size > 0) {
337 /* assert(sc_console->ts != NULL); */
338 kernel_console_ts = sc_console->ts;
339 sc_console->ts = kmalloc(sc_console->tsw->te_size,
340 M_SYSCONS, M_WAITOK);
341 bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
342 (*sc_console->tsw->te_default_attr)(sc_console,
343 user_default.std_color,
344 user_default.rev_color);
350 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
353 * If this is the console we couldn't setup sc->dev before because
354 * malloc wasn't working. Set it up now.
356 if (flags & SC_KERNEL_CONSOLE) {
357 KKASSERT(sc->dev == NULL);
358 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK|M_ZERO);
359 sc->dev[0] = make_dev(&sc_ops, sc_console_unit*MAXCONS, UID_ROOT,
361 "ttyv%r", sc_console_unit*MAXCONS);
362 sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
363 sc->dev[0]->si_drv1 = sc_console;
367 * Finish up the standard attach
370 callout_init(&sc->scrn_timer_ch);
371 scp = SC_STAT(sc->dev[0]);
372 if (sc_console == NULL) /* sc_console_unit < 0 */
376 if ((sc->config & SC_VESA800X600)
377 && ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
379 if (sc->flags & SC_SPLASH_SCRN)
380 splash_term(sc->adp);
382 sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
383 sc_set_pixel_mode(scp, NULL, 0, 0, 16);
384 sc->initial_mode = M_VESA_800x600;
386 /* put up the splash again! */
387 if (sc->flags & SC_SPLASH_SCRN)
388 splash_init(sc->adp, scsplash_callback, sc);
391 #endif /* SC_PIXEL_MODE */
393 /* initialize cursor */
395 update_cursor_image(scp);
397 /* get screen update going */
400 /* set up the keyboard */
401 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
402 update_kbd_state(scp, scp->status, LOCK_MASK);
404 kprintf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
405 unit, adapter_name(sc->adp), sc->vtys, sc->config);
407 kprintf("sc%d:", unit);
408 if (sc->adapter >= 0)
409 kprintf(" fb%d", sc->adapter);
410 if (sc->keyboard >= 0)
411 kprintf(", kbd%d", sc->keyboard);
413 kprintf(", terminal emulator: %s (%s)",
414 scp->tsw->te_name, scp->tsw->te_desc);
418 /* register a shutdown callback for the kernel console */
419 if (sc_console_unit == unit)
420 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
421 (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
426 * The first vty already has struct tty and scr_stat initialized
427 * in scinit(). The other vtys will have these structs when
430 for (vc = 1; vc < sc->vtys; vc++) {
431 dev = make_dev(&sc_ops, vc + unit * MAXCONS,
433 0600, "ttyv%r", vc + unit * MAXCONS);
436 cctl_dev = make_dev(&sc_ops, SC_CONSOLECTL,
437 UID_ROOT, GID_WHEEL, 0600, "consolectl");
438 cctl_dev->si_tty = sc_console_tty = ttymalloc(sc_console_tty);
439 cctl_dev->si_drv1 = sc_console;
451 * As soon as malloc() becomes functional, we had better allocate
452 * various buffers for the kernel console.
455 if (sc_console_unit < 0) /* sc_console == NULL */
458 /* copy the temporary buffer to the final buffer */
459 sc_alloc_scr_buffer(sc_console, TRUE, FALSE);
461 #ifndef SC_NO_CUTPASTE
462 sc_alloc_cut_buffer(sc_console, TRUE);
465 #ifndef SC_NO_HISTORY
466 /* initialize history buffer & pointers */
467 sc_alloc_history_buffer(sc_console, 0, 0, TRUE);
471 SYSINIT(sc_mem, SI_BOOT1_POST, SI_ORDER_ANY, scmeminit, NULL);
474 scdevtounit(cdev_t dev)
476 int vty = SC_VTY(dev);
478 if (vty == SC_CONSOLECTL)
479 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
480 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
487 scopen(struct dev_open_args *ap)
489 cdev_t dev = ap->a_head.a_dev;
497 lwkt_gettoken(&tty_token);
498 unit = scdevtounit(dev);
499 DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
500 major(dev), minor(dev), unit, SC_VTY(dev)));
502 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
504 lwkt_reltoken(&tty_token);
508 tp = dev->si_tty = ttymalloc(dev->si_tty);
509 tp->t_oproc = scstart;
510 tp->t_param = scparam;
511 tp->t_stop = nottystop;
515 if (!ISTTYOPEN(tp)) {
517 /* Use the current setting of the <-- key as default VERASE. */
518 /* If the Delete key is preferable, an stty is necessary */
519 if (sc->kbd != NULL) {
520 key.keynum = KEYCODE_BS;
521 kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
522 tp->t_cc[VERASE] = key.key.map[0];
524 tp->t_iflag = TTYDEF_IFLAG;
525 tp->t_oflag = TTYDEF_OFLAG;
526 tp->t_cflag = TTYDEF_CFLAG;
527 tp->t_lflag = TTYDEF_LFLAG;
528 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
529 scparam(tp, &tp->t_termios);
530 (*linesw[tp->t_line].l_modem)(tp, 1);
533 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
534 lwkt_reltoken(&tty_token);
538 error = (*linesw[tp->t_line].l_open)(dev, tp);
542 scp = dev->si_drv1 = alloc_scp(sc, SC_VTY(dev));
545 sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
548 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
549 tp->t_winsize.ws_col = scp->xsize;
550 tp->t_winsize.ws_row = scp->ysize;
553 lwkt_reltoken(&tty_token);
558 scclose(struct dev_close_args *ap)
560 cdev_t dev = ap->a_head.a_dev;
561 struct tty *tp = dev->si_tty;
564 lwkt_gettoken(&tty_token);
565 if (SC_VTY(dev) != SC_CONSOLECTL) {
566 scp = SC_STAT(tp->t_dev);
567 /* were we in the middle of the VT switching process? */
568 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
569 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
570 cons_unavail = FALSE;
572 * note: must be called from a critical section because finish_vt_rel
573 * will call do_switch_scr which releases it temporarily
575 if (finish_vt_rel(scp, TRUE) == 0) /* force release */
576 DPRINTF(5, ("reset WAIT_REL, "));
577 if (finish_vt_acq(scp) == 0) /* force acknowledge */
578 DPRINTF(5, ("reset WAIT_ACQ, "));
581 if (scp == &main_console) {
584 scp->smode.mode = VT_AUTO;
587 sc_vtb_destroy(&scp->vtb);
588 sc_vtb_destroy(&scp->scr);
589 sc_free_history_buffer(scp, scp->ysize);
591 kfree(scp, M_SYSCONS);
596 scp->smode.mode = VT_AUTO;
598 scp->kbd_mode = K_XLATE;
600 if (scp == scp->sc->cur_scp)
601 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
602 DPRINTF(5, ("done.\n"));
604 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
606 lwkt_reltoken(&tty_token);
612 scread(struct dev_read_args *ap)
616 lwkt_gettoken(&tty_token);
617 sc_touch_scrn_saver();
619 lwkt_reltoken(&tty_token);
624 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
632 lwkt_gettoken(&tty_token);
634 * WARNING: In early boot sc->dev may not be setup yet.
636 sc = (sc_softc_t *)arg;
637 /* assert(thiskbd == sc->kbd) */
642 case KBDIO_UNLOADING:
647 kbd_release(thiskbd, (void *)&sc->keyboard);
648 lwkt_reltoken(&tty_token);
651 lwkt_reltoken(&tty_token);
656 * Loop while there is still input to get from the keyboard.
657 * I don't think this is nessesary, and it doesn't fix
658 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
660 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
661 cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
662 if (!ISTTYOPEN(cur_tty)) {
663 cur_tty = sc_console_tty;
664 if (!ISTTYOPEN(cur_tty))
669 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty)) {
675 switch (KEYFLAGS(c)) {
676 case 0x0000: /* normal key */
677 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
679 case FKEY: /* function key, return string */
680 cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
683 (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
686 case MKEY: /* meta is active, prepend ESC */
687 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
688 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
690 case BKEY: /* backtab fixed sequence (esc [ Z) */
691 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
692 (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
693 (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
699 sc->cur_scp->status |= MOUSE_HIDDEN;
702 lwkt_reltoken(&tty_token);
707 scparam(struct tty *tp, struct termios *t)
709 lwkt_gettoken(&tty_token);
710 tp->t_ispeed = t->c_ispeed;
711 tp->t_ospeed = t->c_ospeed;
712 tp->t_cflag = t->c_cflag;
713 lwkt_reltoken(&tty_token);
718 scioctl(struct dev_ioctl_args *ap)
720 cdev_t dev = ap->a_head.a_dev;
721 u_long cmd = ap->a_cmd;
722 caddr_t data = ap->a_data;
723 int flag = ap->a_fflag;
730 lwkt_gettoken(&tty_token);
733 error = sc_vid_ioctl(tp, cmd, data, flag);
734 if (error != ENOIOCTL) {
735 lwkt_reltoken(&tty_token);
739 #ifndef SC_NO_HISTORY
740 error = sc_hist_ioctl(tp, cmd, data, flag);
741 if (error != ENOIOCTL) {
742 lwkt_reltoken(&tty_token);
747 #ifndef SC_NO_SYSMOUSE
748 error = sc_mouse_ioctl(tp, cmd, data, flag);
749 if (error != ENOIOCTL) {
750 lwkt_reltoken(&tty_token);
755 scp = SC_STAT(tp->t_dev);
756 /* assert(scp != NULL) */
757 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
762 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag);
764 if (error != ENOIOCTL) {
765 lwkt_reltoken(&tty_token);
770 switch (cmd) { /* process console hardware related ioctl's */
772 case GIO_ATTR: /* get current attributes */
773 /* this ioctl is not processed here, but in the terminal emulator */
774 lwkt_reltoken(&tty_token);
777 case GIO_COLOR: /* is this a color console ? */
778 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
779 lwkt_reltoken(&tty_token);
782 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
783 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME) {
784 lwkt_reltoken(&tty_token);
788 scrn_blank_time = *(int *)data;
789 run_scrn_saver = (scrn_blank_time != 0);
791 lwkt_reltoken(&tty_token);
794 case CONS_CURSORTYPE: /* set cursor type blink/noblink */
796 if (!ISGRAPHSC(sc->cur_scp))
797 sc_remove_cursor_image(sc->cur_scp);
798 if ((*(int*)data) & 0x01)
799 sc->flags |= SC_BLINK_CURSOR;
801 sc->flags &= ~SC_BLINK_CURSOR;
802 if ((*(int*)data) & 0x02) {
803 sc->flags |= SC_CHAR_CURSOR;
805 sc->flags &= ~SC_CHAR_CURSOR;
807 * The cursor shape is global property; all virtual consoles
808 * are affected. Update the cursor in the current console...
810 if (!ISGRAPHSC(sc->cur_scp)) {
811 sc_set_cursor_image(sc->cur_scp);
812 sc_draw_cursor_image(sc->cur_scp);
815 lwkt_reltoken(&tty_token);
818 case CONS_BELLTYPE: /* set bell type sound/visual */
821 if ((*(int *)data) & 0x01)
822 sc->flags |= SC_VISUAL_BELL;
824 sc->flags &= ~SC_VISUAL_BELL;
826 if ((*(int *)data) & 0x02)
827 sc->flags |= SC_QUIET_BELL;
829 sc->flags &= ~SC_QUIET_BELL;
832 lwkt_reltoken(&tty_token);
835 case CONS_GETINFO: /* get current (virtual) console info */
837 vid_info_t *ptr = (vid_info_t*)data;
838 if (ptr->size == sizeof(struct vid_info)) {
839 ptr->m_num = sc->cur_scp->index;
840 ptr->font_size = scp->font_size;
841 ptr->mv_col = scp->xpos;
842 ptr->mv_row = scp->ypos;
843 ptr->mv_csz = scp->xsize;
844 ptr->mv_rsz = scp->ysize;
846 * The following fields are filled by the terminal emulator. XXX
853 ptr->mv_grfc.fore = 0; /* not supported */
854 ptr->mv_grfc.back = 0; /* not supported */
855 ptr->mv_ovscan = scp->border;
856 if (scp == sc->cur_scp)
858 ptr->mk_keylock = scp->status & LOCK_MASK;
859 lwkt_reltoken(&tty_token);
862 lwkt_reltoken(&tty_token);
866 case CONS_GETVERS: /* get version number */
867 *(int*)data = 0x200; /* version 2.0 */
868 lwkt_reltoken(&tty_token);
871 case CONS_IDLE: /* see if the screen has been idle */
873 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
874 * the user process may have been writing something on the
875 * screen and syscons is not aware of it. Declare the screen
876 * is NOT idle if it is in one of these modes. But there is
877 * an exception to it; if a screen saver is running in the
878 * graphics mode in the current screen, we should say that the
879 * screen has been idle.
881 *(int *)data = (sc->flags & SC_SCRN_IDLE)
882 && (!ISGRAPHSC(sc->cur_scp)
883 || (sc->cur_scp->status & SAVER_RUNNING));
884 lwkt_reltoken(&tty_token);
887 case CONS_SAVERMODE: /* set saver mode */
888 switch(*(int *)data) {
892 /* if a LKM screen saver is running, stop it first. */
893 scsplash_stick(FALSE);
894 saver_mode = *(int *)data;
896 if ((error = wait_scrn_saver_stop(NULL))) {
898 lwkt_reltoken(&tty_token);
902 run_scrn_saver = TRUE;
903 if (saver_mode == CONS_USR_SAVER)
904 scp->status |= SAVER_RUNNING;
906 scp->status &= ~SAVER_RUNNING;
907 scsplash_stick(TRUE);
912 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
913 scp->status &= ~SAVER_RUNNING;
914 saver_mode = *(int *)data;
918 lwkt_reltoken(&tty_token);
921 lwkt_reltoken(&tty_token);
924 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
926 * Note that this ioctl does not guarantee the screen saver
927 * actually starts or stops. It merely attempts to do so...
930 run_scrn_saver = (*(int *)data != 0);
932 sc->scrn_time_stamp -= scrn_blank_time;
934 lwkt_reltoken(&tty_token);
937 case CONS_SCRSHOT: /* get a screen shot */
939 scrshot_t *ptr = (scrshot_t*)data;
941 if (ISGRAPHSC(scp)) {
943 lwkt_reltoken(&tty_token);
946 if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
948 lwkt_reltoken(&tty_token);
952 copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
953 ptr->xsize * ptr->ysize * sizeof(uint16_t));
954 lwkt_reltoken(&tty_token);
958 case VT_SETMODE: /* set screen switcher mode */
960 struct vt_mode *mode;
962 mode = (struct vt_mode *)data;
963 DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
964 if (scp->smode.mode == VT_PROCESS) {
965 lwkt_gettoken(&proc_token);
966 if (scp->proc == pfind(scp->pid) && scp->proc != curproc) {
967 DPRINTF(5, ("error EPERM\n"));
968 lwkt_reltoken(&proc_token);
969 lwkt_reltoken(&tty_token);
972 lwkt_reltoken(&proc_token);
975 if (mode->mode == VT_AUTO) {
976 scp->smode.mode = VT_AUTO;
979 DPRINTF(5, ("VT_AUTO, "));
980 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
981 cons_unavail = FALSE;
983 * note: must be called from a critical section because
984 * finish_vt_rel will call do_switch_scr which releases it
987 if (finish_vt_rel(scp, TRUE) == 0)
988 DPRINTF(5, ("reset WAIT_REL, "));
989 if (finish_vt_acq(scp) == 0)
990 DPRINTF(5, ("reset WAIT_ACQ, "));
992 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
993 || !ISSIGVALID(mode->frsig)) {
995 DPRINTF(5, ("error EINVAL\n"));
996 lwkt_reltoken(&tty_token);
999 DPRINTF(5, ("VT_PROCESS %d, ", curproc->p_pid));
1001 lwkt_gettoken(&proc_token);
1003 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1004 scp->proc = curproc;
1005 scp->pid = scp->proc->p_pid;
1006 lwkt_reltoken(&proc_token);
1007 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1008 cons_unavail = TRUE;
1012 lwkt_reltoken(&tty_token);
1016 case VT_GETMODE: /* get screen switcher mode */
1017 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1018 lwkt_reltoken(&tty_token);
1021 case VT_RELDISP: /* screen switcher ioctl */
1023 * This must be the current vty which is in the VT_PROCESS
1027 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1029 lwkt_reltoken(&tty_token);
1032 /* ...and this process is controlling it. */
1033 if (scp->proc != curproc) {
1035 lwkt_reltoken(&tty_token);
1039 switch(*(int *)data) {
1040 case VT_FALSE: /* user refuses to release screen, abort */
1042 * note: must be called from a critical section because
1043 * finish_vt_rel will call do_switch_scr which releases it
1046 if ((error = finish_vt_rel(scp, FALSE)) == 0)
1047 DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
1049 case VT_TRUE: /* user has released screen, go on */
1051 * note: must be called from a critical section because
1052 * finish_vt_rel will call do_switch_scr which releases it
1055 if ((error = finish_vt_rel(scp, TRUE)) == 0)
1056 DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
1058 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1059 if ((error = finish_vt_acq(scp)) == 0)
1060 DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
1066 lwkt_reltoken(&tty_token);
1069 case VT_OPENQRY: /* return free virtual console */
1070 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1071 tp = VIRTUAL_TTY(sc, i);
1072 if (!ISTTYOPEN(tp)) {
1073 *(int *)data = i + 1;
1074 lwkt_reltoken(&tty_token);
1078 lwkt_reltoken(&tty_token);
1081 case VT_ACTIVATE: /* switch to screen *data */
1082 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1084 sc_clean_up(sc->cur_scp);
1085 error = sc_switch_scr(sc, i);
1087 lwkt_reltoken(&tty_token);
1090 case VT_WAITACTIVE: /* wait for switch to occur */
1091 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1092 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys)) {
1093 lwkt_reltoken(&tty_token);
1097 error = sc_clean_up(sc->cur_scp);
1100 lwkt_reltoken(&tty_token);
1103 scp = SC_STAT(SC_DEV(sc, i));
1104 if (scp == scp->sc->cur_scp) {
1105 lwkt_reltoken(&tty_token);
1108 error = tsleep((caddr_t)&scp->smode, PCATCH, "waitvt", 0);
1109 /* May return ERESTART */
1110 lwkt_reltoken(&tty_token);
1113 case VT_GETACTIVE: /* get active vty # */
1114 *(int *)data = sc->cur_scp->index + 1;
1115 lwkt_reltoken(&tty_token);
1118 case VT_GETINDEX: /* get this vty # */
1119 *(int *)data = scp->index + 1;
1120 lwkt_reltoken(&tty_token);
1123 case VT_LOCKSWITCH: /* prevent vty switching */
1125 if ((*(int *)data) & 0x01)
1126 sc->flags |= SC_SCRN_VTYLOCK;
1128 sc->flags &= ~SC_SCRN_VTYLOCK;
1130 lwkt_reltoken(&tty_token);
1133 case KDENABIO: /* allow io operations */
1134 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
1136 lwkt_reltoken(&tty_token);
1139 if (securelevel > 0) {
1140 lwkt_reltoken(&tty_token);
1143 lwkt_gettoken(&proc_token);
1144 #if defined(__i386__)
1145 curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
1146 #elif defined(__x86_64__)
1147 curthread->td_lwp->lwp_md.md_regs->tf_rflags |= PSL_IOPL;
1149 lwkt_reltoken(&proc_token);
1150 lwkt_reltoken(&tty_token);
1153 case KDDISABIO: /* disallow io operations (default) */
1154 lwkt_gettoken(&proc_token);
1155 #if defined(__i386__)
1156 curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;
1157 #elif defined(__x86_64__)
1158 curthread->td_lwp->lwp_md.md_regs->tf_rflags &= ~PSL_IOPL;
1160 lwkt_reltoken(&proc_token);
1161 lwkt_reltoken(&tty_token);
1164 case KDSKBSTATE: /* set keyboard state (locks) */
1165 if (*(int *)data & ~LOCK_MASK) {
1166 lwkt_reltoken(&tty_token);
1170 scp->status &= ~LOCK_MASK;
1171 scp->status |= *(int *)data;
1173 if (scp == sc->cur_scp)
1174 update_kbd_state(scp, scp->status, LOCK_MASK);
1175 lwkt_reltoken(&tty_token);
1178 case KDGKBSTATE: /* get keyboard state (locks) */
1179 if (scp == sc->cur_scp)
1180 save_kbd_state(scp);
1181 *(int *)data = scp->status & LOCK_MASK;
1182 lwkt_reltoken(&tty_token);
1185 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1186 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1187 error = kbd_ioctl(sc->kbd, cmd, data);
1188 if (error == ENOIOCTL)
1190 lwkt_reltoken(&tty_token);
1193 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1194 if (*(int *)data & ~0x7f) {
1195 lwkt_reltoken(&tty_token);
1198 error = kbd_ioctl(sc->kbd, cmd, data);
1199 if (error == ENOIOCTL)
1201 lwkt_reltoken(&tty_token);
1204 case KDSKBMODE: /* set keyboard mode */
1205 switch (*(int *)data) {
1206 case K_XLATE: /* switch to XLT ascii mode */
1207 case K_RAW: /* switch to RAW scancode mode */
1208 case K_CODE: /* switch to CODE mode */
1209 scp->kbd_mode = *(int *)data;
1210 if (scp == sc->cur_scp)
1211 kbd_ioctl(sc->kbd, cmd, data);
1212 lwkt_reltoken(&tty_token);
1215 lwkt_reltoken(&tty_token);
1220 case KDGKBMODE: /* get keyboard mode */
1221 *(int *)data = scp->kbd_mode;
1222 lwkt_reltoken(&tty_token);
1226 error = kbd_ioctl(sc->kbd, cmd, data);
1227 if (error == ENOIOCTL)
1229 lwkt_reltoken(&tty_token);
1232 case KDMKTONE: /* sound the bell */
1235 sc_bell(scp, (*(int*)data)&0xffff,
1236 (((*(int*)data)>>16)&0xffff)*hz/1000);
1238 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1240 lwkt_reltoken(&tty_token);
1243 case KIOCSOUND: /* make tone (*data) hz */
1245 if (scp == sc->cur_scp) {
1247 error = sc_tone(*(int *)data);
1255 lwkt_reltoken(&tty_token);
1258 case KDGKBTYPE: /* get keyboard type */
1259 error = kbd_ioctl(sc->kbd, cmd, data);
1260 if (error == ENOIOCTL) {
1261 /* always return something? XXX */
1264 lwkt_reltoken(&tty_token);
1267 case KDSETLED: /* set keyboard LED status */
1268 if (*(int *)data & ~LED_MASK) { /* FIXME: LOCK_MASK? */
1269 lwkt_reltoken(&tty_token);
1273 scp->status &= ~LED_MASK;
1274 scp->status |= *(int *)data;
1276 if (scp == sc->cur_scp)
1277 update_kbd_leds(scp, scp->status);
1278 lwkt_reltoken(&tty_token);
1281 case KDGETLED: /* get keyboard LED status */
1282 if (scp == sc->cur_scp)
1283 save_kbd_state(scp);
1284 *(int *)data = scp->status & LED_MASK;
1285 lwkt_reltoken(&tty_token);
1288 case KBADDKBD: /* add/remove keyboard to/from mux */
1290 error = kbd_ioctl(sc->kbd, cmd, data);
1291 if (error == ENOIOCTL)
1293 lwkt_reltoken(&tty_token);
1296 case CONS_SETKBD: /* set the new keyboard */
1300 newkbd = kbd_get_keyboard(*(int *)data);
1301 if (newkbd == NULL) {
1302 lwkt_reltoken(&tty_token);
1306 if (sc->kbd != newkbd) {
1307 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1308 (void *)&sc->keyboard, sckbdevent, sc);
1309 /* i == newkbd->kb_index */
1311 if (sc->kbd != NULL) {
1312 save_kbd_state(sc->cur_scp);
1313 kbd_release(sc->kbd, (void *)&sc->keyboard);
1316 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1319 kbd_ioctl(sc->kbd, KDSKBMODE,
1320 (caddr_t)&sc->cur_scp->kbd_mode);
1321 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1324 error = EPERM; /* XXX */
1327 lwkt_reltoken(&tty_token);
1331 case CONS_RELKBD: /* release the current keyboard */
1333 if (sc->kbd != NULL) {
1334 save_kbd_state(sc->cur_scp);
1335 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1343 lwkt_reltoken(&tty_token);
1346 case CONS_GETTERM: /* get the current terminal emulator info */
1350 if (((term_info_t *)data)->ti_index == 0) {
1353 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1356 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1357 sizeof(((term_info_t *)data)->ti_name));
1358 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1359 sizeof(((term_info_t *)data)->ti_desc));
1360 ((term_info_t *)data)->ti_flags = 0;
1361 lwkt_reltoken(&tty_token);
1364 ((term_info_t *)data)->ti_name[0] = '\0';
1365 ((term_info_t *)data)->ti_desc[0] = '\0';
1366 ((term_info_t *)data)->ti_flags = 0;
1367 lwkt_reltoken(&tty_token);
1372 case CONS_SETTERM: /* set the current terminal emulator */
1374 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1375 /* FIXME: what if scp == sc_console! XXX */
1377 lwkt_reltoken(&tty_token);
1380 case GIO_SCRNMAP: /* get output translation table */
1381 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1382 lwkt_reltoken(&tty_token);
1385 case PIO_SCRNMAP: /* set output translation table */
1386 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1387 for (i=0; i<sizeof(sc->scr_map); i++) {
1388 sc->scr_rmap[sc->scr_map[i]] = i;
1390 lwkt_reltoken(&tty_token);
1393 case GIO_KEYMAP: /* get keyboard translation table */
1394 case PIO_KEYMAP: /* set keyboard translation table */
1395 case GIO_DEADKEYMAP: /* get accent key translation table */
1396 case PIO_DEADKEYMAP: /* set accent key translation table */
1397 case GETFKEY: /* get function key string */
1398 case SETFKEY: /* set function key string */
1399 error = kbd_ioctl(sc->kbd, cmd, data);
1400 if (error == ENOIOCTL)
1402 lwkt_reltoken(&tty_token);
1405 #ifndef SC_NO_FONT_LOADING
1407 case PIO_FONT8x8: /* set 8x8 dot font */
1408 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1409 lwkt_reltoken(&tty_token);
1413 bcopy(data, sc->font_8, 8*256);
1414 sc->fonts_loaded |= FONT_8;
1417 * Always use the font page #0. XXX
1418 * Don't load if the current font size is not 8x8.
1420 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1421 sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
1423 lwkt_reltoken(&tty_token);
1426 case GIO_FONT8x8: /* get 8x8 dot font */
1427 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1428 lwkt_reltoken(&tty_token);
1431 if (sc->fonts_loaded & FONT_8) {
1432 bcopy(sc->font_8, data, 8*256);
1433 lwkt_reltoken(&tty_token);
1437 lwkt_reltoken(&tty_token);
1441 case PIO_FONT8x14: /* set 8x14 dot font */
1442 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1443 lwkt_reltoken(&tty_token);
1447 bcopy(data, sc->font_14, 14*256);
1448 sc->fonts_loaded |= FONT_14;
1451 * Always use the font page #0. XXX
1452 * Don't load if the current font size is not 8x14.
1454 if (ISTEXTSC(sc->cur_scp)
1455 && (sc->cur_scp->font_size >= 14)
1456 && (sc->cur_scp->font_size < 16)) {
1457 sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
1460 lwkt_reltoken(&tty_token);
1463 case GIO_FONT8x14: /* get 8x14 dot font */
1464 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1465 lwkt_reltoken(&tty_token);
1468 if (sc->fonts_loaded & FONT_14) {
1469 bcopy(sc->font_14, data, 14*256);
1470 lwkt_reltoken(&tty_token);
1474 lwkt_reltoken(&tty_token);
1478 case PIO_FONT8x16: /* set 8x16 dot font */
1479 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1480 lwkt_reltoken(&tty_token);
1484 bcopy(data, sc->font_16, 16*256);
1485 sc->fonts_loaded |= FONT_16;
1488 * Always use the font page #0. XXX
1489 * Don't load if the current font size is not 8x16.
1491 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1492 sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
1494 lwkt_reltoken(&tty_token);
1497 case GIO_FONT8x16: /* get 8x16 dot font */
1498 if (!ISFONTAVAIL(sc->adp->va_flags)) {
1499 lwkt_reltoken(&tty_token);
1502 if (sc->fonts_loaded & FONT_16) {
1503 bcopy(sc->font_16, data, 16*256);
1504 lwkt_reltoken(&tty_token);
1508 lwkt_reltoken(&tty_token);
1512 #endif /* SC_NO_FONT_LOADING */
1518 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, ap->a_cred);
1519 if (error != ENOIOCTL) {
1520 lwkt_reltoken(&tty_token);
1523 error = ttioctl(tp, cmd, data, flag);
1524 if (error != ENOIOCTL) {
1525 lwkt_reltoken(&tty_token);
1528 lwkt_reltoken(&tty_token);
1533 scstart(struct tty *tp)
1537 u_char buf[PCBURST];
1538 scr_stat *scp = SC_STAT(tp->t_dev);
1541 if (scp->status & SLKED ||
1542 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
1547 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1548 tp->t_state |= TS_BUSY;
1551 len = q_to_b(rbp, buf, PCBURST);
1552 sc_puts(scp, buf, len);
1554 tp->t_state &= ~TS_BUSY;
1563 sccnprobe(struct consdev *cp)
1568 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1570 /* a video card is always required */
1571 if (!scvidprobe(unit, flags, TRUE))
1572 cp->cn_pri = CN_DEAD;
1574 /* syscons will become console even when there is no keyboard */
1575 sckbdprobe(unit, flags, TRUE);
1577 if (cp->cn_pri == CN_DEAD) {
1581 /* initialize required fields */
1582 cp->cn_probegood = 1;
1586 sccninit(struct consdev *cp)
1591 sc_get_cons_priority(&unit, &flags);
1592 scinit(unit, flags | SC_KERNEL_CONSOLE);
1593 sc_console_unit = unit;
1594 sc_console = sc_get_softc(unit, SC_KERNEL_CONSOLE)->console_scp;
1598 sccninit_fini(struct consdev *cp)
1600 if (cctl_dev == NULL)
1601 kprintf("sccninit_fini: WARNING: cctl_dev is NULL!\n");
1602 cp->cn_dev = cctl_dev;
1606 sccnterm(struct consdev *cp)
1608 /* we are not the kernel console any more, release everything */
1610 if (sc_console_unit < 0)
1611 return; /* shouldn't happen */
1615 sc_clear_screen(sc_console);
1616 sccnupdate(sc_console);
1619 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1620 sc_console_unit = -1;
1625 * Console path - cannot block!
1628 sccnputc(void *private, int c)
1631 scr_stat *scp = sc_console;
1633 #ifndef SC_NO_HISTORY
1637 #endif /* !SC_NO_HISTORY */
1639 /* assert(sc_console != NULL) */
1642 #ifndef SC_NO_HISTORY
1643 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1644 scp->status &= ~SLKED;
1646 /* This can block, illegal in the console path */
1647 update_kbd_state(scp, scp->status, SLKED);
1649 if (scp->status & BUFFER_SAVED) {
1650 if (!sc_hist_restore(scp))
1651 sc_remove_cutmarking(scp);
1652 scp->status &= ~BUFFER_SAVED;
1653 scp->status |= CURSOR_ENABLED;
1654 sc_draw_cursor_image(scp);
1657 tp = VIRTUAL_TTY(scp->sc, scp->index);
1658 /* This can block, illegal in the console path */
1659 if (ISTTYOPEN(tp)) {
1664 #endif /* !SC_NO_HISTORY */
1667 if (kernel_console_ts != NULL)
1668 scp->ts = kernel_console_ts;
1670 sc_puts(scp, buf, 1);
1678 * Console path - cannot block!
1681 sccngetc(void *private)
1683 return sccngetch(0);
1687 * Console path - cannot block!
1690 sccncheckc(void *private)
1692 return sccngetch(SCGETC_NONBLOCK);
1696 sccndbctl(void *private, int on)
1698 /* assert(sc_console_unit >= 0) */
1699 /* try to switch to the kernel console screen */
1700 if (on && debugger == 0) {
1702 * TRY to make sure the screen saver is stopped,
1703 * and the screen is updated before switching to
1708 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1709 && sc_console->smode.mode == VT_AUTO) {
1710 sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
1712 sc_switch_scr(sc_console->sc, sc_console->index);
1723 * Console path - cannot block!
1726 sccngetch(int flags)
1728 static struct fkeytab fkey;
1736 /* assert(sc_console != NULL) */
1739 * Stop the screen saver and update the screen if necessary.
1740 * What if we have been running in the screen saver code... XXX
1742 sc_touch_scrn_saver();
1743 scp = sc_console->sc->cur_scp; /* XXX */
1747 if (fkeycp < fkey.len) {
1748 return fkey.str[fkeycp++];
1751 if (scp->sc->kbd == NULL) {
1756 * Make sure the keyboard is accessible even when the kbd device
1757 * driver is disabled.
1760 kbd_enable(scp->sc->kbd);
1762 /* we shall always use the keyboard in the XLATE mode here */
1763 cur_mode = scp->kbd_mode;
1764 scp->kbd_mode = K_XLATE;
1765 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1767 kbd_poll(scp->sc->kbd, TRUE);
1768 c = scgetc(scp->sc, SCGETC_CN | flags);
1769 kbd_poll(scp->sc->kbd, FALSE);
1771 scp->kbd_mode = cur_mode;
1772 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1773 kbd_disable(scp->sc->kbd);
1776 switch (KEYFLAGS(c)) {
1777 case 0: /* normal char */
1779 case FKEY: /* function key */
1780 p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1782 if ((p != NULL) && (fkey.len > 0)) {
1783 bcopy(p, fkey.str, fkey.len);
1797 sccnupdate(scr_stat *scp)
1799 /* this is a cut-down version of scrn_timer()... */
1801 if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress) {
1805 if (debugger > 0 || panicstr || shutdown_in_progress) {
1806 sc_touch_scrn_saver();
1807 } else if (scp != scp->sc->cur_scp) {
1811 if (!run_scrn_saver)
1812 scp->sc->flags &= ~SC_SCRN_IDLE;
1815 * This is a hard path, we cannot call stop_scrn_saver() here.
1817 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1818 if (scp->sc->flags & SC_SCRN_BLANKED) {
1819 sc_touch_scrn_saver();
1820 /*stop_scrn_saver(scp->sc, current_saver);*/
1822 #endif /* NSPLASH */
1824 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1825 || scp->sc->switch_in_progress) {
1829 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1830 * when write_in_progress is non-zero. XXX
1833 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1834 scrn_update(scp, TRUE);
1838 scrn_timer(void *arg)
1840 static int kbd_interval = 0;
1847 * Setup depending on who called us
1849 again = (arg != NULL);
1851 sc = (sc_softc_t *)arg;
1852 } else if (sc_console != NULL) {
1853 sc = sc_console->sc;
1859 * Don't do anything when we are performing some I/O operations.
1860 * (These are initiated by the frontend?)
1862 if (sc->font_loading_in_progress || sc->videoio_in_progress) {
1864 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1869 * Try to allocate a keyboard automatically
1871 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1872 if (++kbd_interval >= 25) {
1873 sc->keyboard = sc_allocate_keyboard(sc, -1);
1874 if (sc->keyboard >= 0) {
1875 sc->kbd = kbd_get_keyboard(sc->keyboard);
1876 kbd_ioctl(sc->kbd, KDSKBMODE,
1877 (caddr_t)&sc->cur_scp->kbd_mode);
1878 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1886 * Should we stop the screen saver? We need the syscons_lock
1887 * for most of this stuff.
1889 getmicrouptime(&tv);
1891 if (syscons_lock_nonblock() != 0) {
1892 /* failed to get the lock */
1894 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1897 /* successful lock */
1899 if (debugger > 0 || panicstr || shutdown_in_progress)
1900 sc_touch_scrn_saver();
1901 if (run_scrn_saver) {
1902 if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1903 sc->flags |= SC_SCRN_IDLE;
1905 sc->flags &= ~SC_SCRN_IDLE;
1907 sc->scrn_time_stamp = tv.tv_sec;
1908 sc->flags &= ~SC_SCRN_IDLE;
1909 if (scrn_blank_time > 0)
1910 run_scrn_saver = TRUE;
1913 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1914 if (sc->flags & SC_SCRN_BLANKED)
1915 stop_scrn_saver(sc, current_saver);
1916 #endif /* NSPLASH */
1918 /* should we just return ? */
1919 if (sc->blink_in_progress || sc->switch_in_progress ||
1920 sc->write_in_progress)
1924 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1928 /* Update the screen */
1929 scp = sc->cur_scp; /* cur_scp may have changed... */
1930 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1931 scrn_update(scp, TRUE);
1934 /* should we activate the screen saver? */
1935 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1936 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1937 (*current_saver)(sc, TRUE);
1938 #endif /* NSPLASH */
1942 callout_reset(&sc->scrn_timer_ch, hz / 25, scrn_timer, sc);
1946 and_region(int *s1, int *e1, int s2, int e2)
1948 if (*e1 < s2 || e2 < *s1)
1950 *s1 = imax(*s1, s2);
1951 *e1 = imin(*e1, e2);
1956 scrn_update(scr_stat *scp, int show_cursor)
1963 /* assert(scp == scp->sc->cur_scp) */
1965 ++scp->sc->videoio_in_progress;
1967 #ifndef SC_NO_CUTPASTE
1968 /* remove the previous mouse pointer image if necessary */
1969 if (scp->status & MOUSE_VISIBLE) {
1971 e = scp->mouse_pos + scp->xsize + 1;
1972 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1973 || and_region(&s, &e, scp->start, scp->end)
1974 || ((scp->status & CURSOR_ENABLED) &&
1975 (scp->cursor_pos != scp->cursor_oldpos) &&
1976 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1977 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1978 sc_remove_mouse_image(scp);
1979 if (scp->end >= scp->xsize*scp->ysize)
1980 scp->end = scp->xsize*scp->ysize - 1;
1983 #endif /* !SC_NO_CUTPASTE */
1987 if (scp->end >= scp->xsize*scp->ysize) {
1988 kprintf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1989 scp->end = scp->xsize*scp->ysize - 1;
1991 if (scp->start < 0) {
1992 kprintf("scrn_update(): scp->start %d < 0\n", scp->start);
1997 /* update screen image */
1998 if (scp->start <= scp->end) {
1999 if (scp->mouse_cut_end >= 0) {
2000 /* there is a marked region for cut & paste */
2001 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
2002 start = scp->mouse_cut_start;
2003 end = scp->mouse_cut_end;
2005 start = scp->mouse_cut_end;
2006 end = scp->mouse_cut_start - 1;
2010 /* does the cut-mark region overlap with the update region? */
2011 if (and_region(&s, &e, scp->start, scp->end)) {
2012 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2015 if (and_region(&s, &e, scp->start, scp->end))
2016 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2018 e = scp->xsize*scp->ysize - 1;
2019 if (and_region(&s, &e, scp->start, scp->end))
2020 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2022 (*scp->rndr->draw)(scp, scp->start,
2023 scp->end - scp->start + 1, FALSE);
2026 (*scp->rndr->draw)(scp, scp->start,
2027 scp->end - scp->start + 1, FALSE);
2031 /* we are not to show the cursor and the mouse pointer... */
2034 scp->start = scp->xsize*scp->ysize - 1;
2035 --scp->sc->videoio_in_progress;
2039 /* update cursor image */
2040 if (scp->status & CURSOR_ENABLED) {
2043 /* did cursor move since last time ? */
2044 if (scp->cursor_pos != scp->cursor_oldpos) {
2045 /* do we need to remove old cursor image ? */
2046 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2047 sc_remove_cursor_image(scp);
2048 sc_draw_cursor_image(scp);
2050 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
2051 /* cursor didn't move, but has been overwritten */
2052 sc_draw_cursor_image(scp);
2053 else if (scp->sc->flags & SC_BLINK_CURSOR)
2054 /* if it's a blinking cursor, update it */
2055 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2056 sc_inside_cutmark(scp,
2061 #ifndef SC_NO_CUTPASTE
2062 /* update "pseudo" mouse pointer image */
2063 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2064 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2065 scp->status &= ~MOUSE_MOVED;
2066 sc_draw_mouse_image(scp);
2069 #endif /* SC_NO_CUTPASTE */
2072 scp->start = scp->xsize*scp->ysize - 1;
2074 --scp->sc->videoio_in_progress;
2079 scsplash_callback(int event, void *arg)
2084 sc = (sc_softc_t *)arg;
2088 if (add_scrn_saver(scsplash_saver) == 0) {
2089 sc->flags &= ~SC_SAVER_FAILED;
2090 run_scrn_saver = TRUE;
2091 if (cold && !(boothowto & (RB_VERBOSE | RB_CONFIG))) {
2092 scsplash_stick(TRUE);
2093 (*current_saver)(sc, TRUE);
2099 if (current_saver == scsplash_saver) {
2100 scsplash_stick(FALSE);
2101 error = remove_scrn_saver(scsplash_saver);
2114 scsplash_saver(sc_softc_t *sc, int show)
2116 static int busy = FALSE;
2125 if (!(sc->flags & SC_SAVER_FAILED)) {
2126 if (!(sc->flags & SC_SCRN_BLANKED))
2127 set_scrn_saver_mode(scp, -1, NULL, 0);
2128 switch (splash(sc->adp, TRUE)) {
2129 case 0: /* succeeded */
2131 case EAGAIN: /* try later */
2132 restore_scrn_saver_mode(scp, FALSE);
2133 sc_touch_scrn_saver(); /* XXX */
2136 sc->flags |= SC_SAVER_FAILED;
2137 scsplash_stick(FALSE);
2138 restore_scrn_saver_mode(scp, TRUE);
2139 kprintf("scsplash_saver(): failed to put up the image\n");
2143 } else if (!sticky_splash) {
2144 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2145 restore_scrn_saver_mode(scp, TRUE);
2151 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2156 if (current_saver != none_saver) {
2157 error = remove_scrn_saver(current_saver);
2162 if (current_saver != none_saver) {
2166 run_scrn_saver = FALSE;
2167 saver_mode = CONS_LKM_SAVER;
2168 current_saver = this_saver;
2173 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2175 if (current_saver != this_saver)
2180 * In order to prevent `current_saver' from being called by
2181 * the timeout routine `scrn_timer()' while we manipulate
2182 * the saver list, we shall set `current_saver' to `none_saver'
2183 * before stopping the current saver, rather than blocking by `splXX()'.
2185 current_saver = none_saver;
2187 stop_scrn_saver(this_saver);
2189 /* unblank all blanked screens */
2190 wait_scrn_saver_stop(NULL);
2195 current_saver = none_saver;
2200 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2203 /* assert(scp == scp->sc->cur_scp) */
2205 if (!ISGRAPHSC(scp))
2206 sc_remove_cursor_image(scp);
2207 scp->splash_save_mode = scp->mode;
2208 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2209 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2210 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2211 scp->sc->flags |= SC_SCRN_BLANKED;
2218 if (set_mode(scp) == 0) {
2219 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2220 scp->status |= GRAPHICS_MODE;
2221 #ifndef SC_NO_PALETTE_LOADING
2223 load_palette(scp->sc->adp, pal);
2225 sc_set_border(scp, border);
2229 scp->mode = scp->splash_save_mode;
2230 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2231 scp->status |= scp->splash_save_status;
2239 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2244 /* assert(scp == scp->sc->cur_scp) */
2247 status = scp->status;
2248 scp->mode = scp->splash_save_mode;
2249 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2250 scp->status |= scp->splash_save_status;
2251 scp->sc->flags &= ~SC_SCRN_BLANKED;
2253 if (!ISGRAPHSC(scp))
2254 sc_draw_cursor_image(scp);
2259 if (set_mode(scp) == 0) {
2260 #ifndef SC_NO_PALETTE_LOADING
2261 load_palette(scp->sc->adp, scp->sc->palette);
2268 scp->status = status;
2276 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2278 (*saver)(sc, FALSE);
2279 run_scrn_saver = FALSE;
2280 /* the screen saver may have chosen not to stop after all... */
2281 if (sc->flags & SC_SCRN_BLANKED) {
2285 mark_all(sc->cur_scp);
2286 if (sc->delayed_next_scr)
2287 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2288 wakeup((caddr_t)&scrn_blanked);
2292 wait_scrn_saver_stop(sc_softc_t *sc)
2296 while (scrn_blanked > 0) {
2297 run_scrn_saver = FALSE;
2298 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2302 error = tsleep((caddr_t)&scrn_blanked, PCATCH, "scrsav", 0);
2303 /* May return ERESTART */
2307 run_scrn_saver = FALSE;
2310 #endif /* NSPLASH */
2313 sc_touch_scrn_saver(void)
2315 scsplash_stick(FALSE);
2316 run_scrn_saver = FALSE;
2320 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2325 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2327 /* prevent switch if previously requested */
2328 if (sc->flags & SC_SCRN_VTYLOCK) {
2329 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2330 sc->cur_scp->bell_duration);
2334 /* delay switch if the screen is blanked or being updated */
2335 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2336 || sc->blink_in_progress || sc->videoio_in_progress) {
2337 sc->delayed_next_scr = next_scr + 1;
2338 sc_touch_scrn_saver();
2339 DPRINTF(5, ("switch delayed\n"));
2343 cur_scp = sc->cur_scp;
2346 * we are in the middle of the vty switching process...
2348 * This may be in the console path, we can only deal with this case
2349 * if the proc_token is available non-blocking.
2351 if (sc->switch_in_progress &&
2352 (cur_scp->smode.mode == VT_PROCESS) &&
2354 lwkt_trytoken(&proc_token)) {
2356 if (cur_scp->proc != pfind(cur_scp->pid)) {
2358 * The controlling process has died!!. Do some clean up.
2359 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2360 * are not reset here yet; they will be cleared later.
2362 DPRINTF(5, ("cur_scp controlling process %d died, ",
2364 if (cur_scp->status & SWITCH_WAIT_REL) {
2366 * Force the previous switch to finish, but return now
2369 * note: must be called from a critical section because
2370 * finish_vt_rel will call do_switch_scr which releases it
2373 DPRINTF(5, ("reset WAIT_REL, "));
2374 finish_vt_rel(cur_scp, TRUE);
2375 DPRINTF(5, ("finishing previous switch\n"));
2376 lwkt_reltoken(&proc_token);
2378 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2379 /* let's assume screen switch has been completed. */
2380 DPRINTF(5, ("reset WAIT_ACQ, "));
2381 finish_vt_acq(cur_scp);
2384 * We are in between screen release and acquisition, and
2385 * reached here via scgetc() or scrn_timer() which has
2386 * interrupted exchange_scr(). Don't do anything stupid.
2388 DPRINTF(5, ("waiting nothing, "));
2392 * The controlling process is alive, but not responding...
2393 * It is either buggy or it may be just taking time.
2394 * The following code is a gross kludge to cope with this
2395 * problem for which there is no clean solution. XXX
2397 if (cur_scp->status & SWITCH_WAIT_REL) {
2398 switch (sc->switch_in_progress++) {
2402 DPRINTF(5, ("sending relsig again, "));
2403 signal_vt_rel(cur_scp);
2410 * Act as if the controlling program returned
2413 * note: must be called from a critical section because
2414 * finish_vt_rel will call do_switch_scr which releases it
2417 DPRINTF(5, ("force reset WAIT_REL, "));
2418 finish_vt_rel(cur_scp, FALSE);
2419 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2420 lwkt_reltoken(&proc_token);
2423 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2424 switch (sc->switch_in_progress++) {
2428 DPRINTF(5, ("sending acqsig again, "));
2429 signal_vt_acq(cur_scp);
2435 /* clear the flag and finish the previous switch */
2436 DPRINTF(5, ("force reset WAIT_ACQ, "));
2437 finish_vt_acq(cur_scp);
2442 lwkt_reltoken(&proc_token);
2446 * Return error if an invalid argument is given, or vty switch
2447 * is still in progress.
2449 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2450 || sc->switch_in_progress) {
2451 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2452 DPRINTF(5, ("error 1\n"));
2457 * Don't allow switching away from the graphics mode vty
2458 * if the switch mode is VT_AUTO, unless the next vty is the same
2459 * as the current or the current vty has been closed (but showing).
2461 tp = VIRTUAL_TTY(sc, cur_scp->index);
2462 if ((cur_scp->index != next_scr)
2464 && (cur_scp->smode.mode == VT_AUTO)
2465 && ISGRAPHSC(cur_scp)) {
2466 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2467 DPRINTF(5, ("error, graphics mode\n"));
2472 * Is the wanted vty open? Don't allow switching to a closed vty.
2473 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2474 * Note that we always allow the user to switch to the kernel
2475 * console even if it is closed.
2477 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2478 tp = VIRTUAL_TTY(sc, next_scr);
2479 if (!ISTTYOPEN(tp)) {
2480 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2481 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2484 if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2485 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2490 /* this is the start of vty switching process... */
2491 ++sc->switch_in_progress;
2492 sc->delayed_next_scr = 0;
2493 sc->old_scp = cur_scp;
2494 sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2495 if (sc->new_scp == sc->old_scp) {
2496 sc->switch_in_progress = 0;
2497 wakeup((caddr_t)&sc->new_scp->smode);
2498 DPRINTF(5, ("switch done (new == old)\n"));
2502 /* has controlling process died? */
2503 vt_proc_alive(sc->old_scp);
2504 vt_proc_alive(sc->new_scp);
2506 /* wait for the controlling process to release the screen, if necessary */
2507 if (signal_vt_rel(sc->old_scp)) {
2511 /* go set up the new vty screen */
2514 /* wake up processes waiting for this vty */
2515 wakeup((caddr_t)&sc->cur_scp->smode);
2517 /* wait for the controlling process to acknowledge, if necessary */
2518 if (signal_vt_acq(sc->cur_scp)) {
2522 sc->switch_in_progress = 0;
2523 if (sc->unit == sc_console_unit)
2524 cons_unavail = FALSE;
2525 DPRINTF(5, ("switch done\n"));
2531 * NOTE: must be called from a critical section because do_switch_scr
2532 * will release it temporarily.
2535 do_switch_scr(sc_softc_t *sc)
2537 lwkt_gettoken(&tty_token);
2538 vt_proc_alive(sc->new_scp);
2541 /* sc->cur_scp == sc->new_scp */
2542 wakeup((caddr_t)&sc->cur_scp->smode);
2544 /* wait for the controlling process to acknowledge, if necessary */
2545 if (!signal_vt_acq(sc->cur_scp)) {
2546 sc->switch_in_progress = 0;
2547 if (sc->unit == sc_console_unit)
2548 cons_unavail = FALSE;
2550 lwkt_reltoken(&tty_token);
2554 vt_proc_alive(scr_stat *scp)
2556 lwkt_gettoken(&tty_token);
2557 lwkt_gettoken(&proc_token);
2559 if (scp->proc == pfind(scp->pid)) {
2560 lwkt_reltoken(&proc_token);
2561 lwkt_reltoken(&tty_token);
2565 scp->smode.mode = VT_AUTO;
2566 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2568 lwkt_reltoken(&proc_token);
2569 lwkt_reltoken(&tty_token);
2574 signal_vt_rel(scr_stat *scp)
2576 lwkt_gettoken(&tty_token);
2577 if (scp->smode.mode != VT_PROCESS) {
2578 lwkt_reltoken(&tty_token);
2581 scp->status |= SWITCH_WAIT_REL;
2582 lwkt_gettoken(&proc_token);
2583 ksignal(scp->proc, scp->smode.relsig);
2584 lwkt_reltoken(&proc_token);
2585 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2586 lwkt_reltoken(&tty_token);
2591 signal_vt_acq(scr_stat *scp)
2593 lwkt_gettoken(&tty_token);
2594 if (scp->smode.mode != VT_PROCESS) {
2595 lwkt_reltoken(&tty_token);
2598 if (scp->sc->unit == sc_console_unit)
2599 cons_unavail = TRUE;
2600 scp->status |= SWITCH_WAIT_ACQ;
2601 lwkt_gettoken(&proc_token);
2602 ksignal(scp->proc, scp->smode.acqsig);
2603 lwkt_reltoken(&proc_token);
2604 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2605 lwkt_reltoken(&tty_token);
2610 * NOTE: must be called from a critical section because do_switch_scr
2611 * will release it temporarily.
2614 finish_vt_rel(scr_stat *scp, int release)
2616 lwkt_gettoken(&tty_token);
2617 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2618 scp->status &= ~SWITCH_WAIT_REL;
2620 do_switch_scr(scp->sc);
2622 scp->sc->switch_in_progress = 0;
2623 lwkt_reltoken(&tty_token);
2626 lwkt_reltoken(&tty_token);
2631 finish_vt_acq(scr_stat *scp)
2633 lwkt_gettoken(&tty_token);
2634 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2635 scp->status &= ~SWITCH_WAIT_ACQ;
2636 scp->sc->switch_in_progress = 0;
2637 lwkt_reltoken(&tty_token);
2640 lwkt_reltoken(&tty_token);
2645 exchange_scr(sc_softc_t *sc)
2649 lwkt_gettoken(&tty_token);
2650 /* save the current state of video and keyboard */
2651 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2652 if (!ISGRAPHSC(sc->old_scp))
2653 sc_remove_cursor_image(sc->old_scp);
2654 if (sc->old_scp->kbd_mode == K_XLATE)
2655 save_kbd_state(sc->old_scp);
2657 /* set up the video for the new screen */
2658 scp = sc->cur_scp = sc->new_scp;
2659 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2662 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2663 (void *)sc->adp->va_window, FALSE);
2664 scp->status |= MOUSE_HIDDEN;
2665 sc_move_cursor(scp, scp->xpos, scp->ypos);
2666 if (!ISGRAPHSC(scp))
2667 sc_set_cursor_image(scp);
2668 #ifndef SC_NO_PALETTE_LOADING
2669 if (ISGRAPHSC(sc->old_scp))
2670 load_palette(sc->adp, sc->palette);
2672 sc_set_border(scp, scp->border);
2674 /* set up the keyboard for the new screen */
2675 if (sc->old_scp->kbd_mode != scp->kbd_mode)
2676 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2677 update_kbd_state(scp, scp->status, LOCK_MASK);
2680 lwkt_reltoken(&tty_token);
2684 sc_puts(scr_stat *scp, u_char *buf, int len)
2687 /* make screensaver happy */
2688 if (!sticky_splash && scp == scp->sc->cur_scp)
2689 run_scrn_saver = FALSE;
2693 (*scp->tsw->te_puts)(scp, buf, len);
2695 if (scp->sc->delayed_next_scr)
2696 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2701 sc_draw_cursor_image(scr_stat *scp)
2703 /* assert(scp == scp->sc->cur_scp); */
2704 ++scp->sc->videoio_in_progress;
2705 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2706 scp->sc->flags & SC_BLINK_CURSOR, TRUE,
2707 sc_inside_cutmark(scp, scp->cursor_pos));
2708 scp->cursor_oldpos = scp->cursor_pos;
2709 --scp->sc->videoio_in_progress;
2713 sc_remove_cursor_image(scr_stat *scp)
2715 /* assert(scp == scp->sc->cur_scp); */
2716 ++scp->sc->videoio_in_progress;
2717 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2718 scp->sc->flags & SC_BLINK_CURSOR, FALSE,
2719 sc_inside_cutmark(scp, scp->cursor_oldpos));
2720 --scp->sc->videoio_in_progress;
2724 update_cursor_image(scr_stat *scp)
2728 if (scp->sc->flags & SC_CHAR_CURSOR) {
2729 scp->cursor_base = imax(0, scp->sc->cursor_base);
2730 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2732 scp->cursor_base = 0;
2733 scp->cursor_height = scp->font_size;
2735 blink = scp->sc->flags & SC_BLINK_CURSOR;
2737 /* assert(scp == scp->sc->cur_scp); */
2738 ++scp->sc->videoio_in_progress;
2739 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE,
2740 sc_inside_cutmark(scp, scp->cursor_pos));
2741 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
2742 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE,
2743 sc_inside_cutmark(scp, scp->cursor_pos));
2744 --scp->sc->videoio_in_progress;
2748 sc_set_cursor_image(scr_stat *scp)
2750 if (scp->sc->flags & SC_CHAR_CURSOR) {
2751 scp->cursor_base = imax(0, scp->sc->cursor_base);
2752 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2754 scp->cursor_base = 0;
2755 scp->cursor_height = scp->font_size;
2758 /* assert(scp == scp->sc->cur_scp); */
2759 ++scp->sc->videoio_in_progress;
2760 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
2761 scp->sc->flags & SC_BLINK_CURSOR);
2762 --scp->sc->videoio_in_progress;
2766 scinit(int unit, int flags)
2769 * When syscons is being initialized as the kernel console, malloc()
2770 * is not yet functional, because various kernel structures has not been
2771 * fully initialized yet. Therefore, we need to declare the following
2772 * static buffers for the console. This is less than ideal,
2773 * but is necessry evil for the time being. XXX
2775 static scr_stat main_console;
2776 static u_short sc_buffer[ROW*COL]; /* XXX */
2777 #ifndef SC_NO_FONT_LOADING
2778 static u_char font_8[256*8];
2779 static u_char font_14[256*14];
2780 static u_char font_16[256*16];
2785 video_adapter_t *adp;
2790 /* one time initialization */
2791 if (init_done == COLD)
2792 sc_get_bios_values(&bios_value);
2796 * Allocate resources. Even if we are being called for the second
2797 * time, we must allocate them again, because they might have
2800 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2802 if (sc->adapter >= 0) {
2803 vid_release(sc->adp, (void *)&sc->adapter);
2807 if (sc->keyboard >= 0) {
2808 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2809 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2810 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2811 if (sc->kbd != NULL) {
2812 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2813 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2817 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2818 sc->adp = vid_get_adapter(sc->adapter);
2819 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2820 sc->keyboard = sc_allocate_keyboard(sc, unit);
2821 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2822 sc->kbd = kbd_get_keyboard(sc->keyboard);
2823 if (sc->kbd != NULL) {
2824 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2825 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2828 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2830 sc->initial_mode = sc->adp->va_initial_mode;
2832 #ifndef SC_NO_FONT_LOADING
2833 if (flags & SC_KERNEL_CONSOLE) {
2834 sc->font_8 = font_8;
2835 sc->font_14 = font_14;
2836 sc->font_16 = font_16;
2837 } else if (sc->font_8 == NULL) {
2838 /* assert(sc_malloc) */
2839 sc->font_8 = kmalloc(sizeof(font_8), M_SYSCONS, M_WAITOK);
2840 sc->font_14 = kmalloc(sizeof(font_14), M_SYSCONS, M_WAITOK);
2841 sc->font_16 = kmalloc(sizeof(font_16), M_SYSCONS, M_WAITOK);
2845 lwkt_gettoken(&tty_token);
2846 /* extract the hardware cursor location and hide the cursor for now */
2847 (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2848 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2849 lwkt_reltoken(&tty_token);
2851 /* set up the first console */
2852 sc->first_vty = unit*MAXCONS;
2853 sc->vtys = MAXCONS; /* XXX: should be configurable */
2854 if (flags & SC_KERNEL_CONSOLE) {
2855 scp = &main_console;
2856 sc->console_scp = scp;
2857 init_scp(sc, sc->first_vty, scp);
2858 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2859 (void *)sc_buffer, FALSE);
2860 if (sc_init_emulator(scp, SC_DFLT_TERM))
2861 sc_init_emulator(scp, "*");
2862 (*scp->tsw->te_default_attr)(scp,
2863 kernel_default.std_color,
2864 kernel_default.rev_color);
2866 /* assert(sc_malloc) */
2867 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK | M_ZERO);
2869 sc->dev[0] = make_dev(&sc_ops, unit*MAXCONS, UID_ROOT,
2870 GID_WHEEL, 0600, "ttyv%r", unit*MAXCONS);
2872 sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
2873 scp = alloc_scp(sc, sc->first_vty);
2874 sc->dev[0]->si_drv1 = scp;
2878 /* copy screen to temporary buffer */
2879 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2880 (void *)scp->sc->adp->va_window, FALSE);
2882 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2884 /* move cursors to the initial positions */
2885 if (col >= scp->xsize)
2887 if (row >= scp->ysize)
2888 row = scp->ysize - 1;
2891 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2892 if (bios_value.cursor_end < scp->font_size)
2893 sc->cursor_base = scp->font_size - bios_value.cursor_end - 1;
2895 sc->cursor_base = 0;
2896 i = bios_value.cursor_end - bios_value.cursor_start + 1;
2897 sc->cursor_height = imin(i, scp->font_size);
2898 #ifndef SC_NO_SYSMOUSE
2899 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2901 if (!ISGRAPHSC(scp)) {
2902 sc_set_cursor_image(scp);
2903 sc_draw_cursor_image(scp);
2906 /* save font and palette */
2907 #ifndef SC_NO_FONT_LOADING
2908 sc->fonts_loaded = 0;
2909 if (ISFONTAVAIL(sc->adp->va_flags)) {
2911 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2912 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2913 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2914 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2915 if (scp->font_size < 14) {
2916 sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
2917 } else if (scp->font_size >= 16) {
2918 sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
2920 sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
2922 #else /* !SC_DFLT_FONT */
2923 if (scp->font_size < 14) {
2924 sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
2925 sc->fonts_loaded = FONT_8;
2926 } else if (scp->font_size >= 16) {
2927 sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
2928 sc->fonts_loaded = FONT_16;
2930 sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
2931 sc->fonts_loaded = FONT_14;
2933 #endif /* SC_DFLT_FONT */
2934 /* FONT KLUDGE: always use the font page #0. XXX */
2935 sc_show_font(scp, 0);
2937 #endif /* !SC_NO_FONT_LOADING */
2939 #ifndef SC_NO_PALETTE_LOADING
2940 save_palette(sc->adp, sc->palette);
2944 if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
2945 /* we are ready to put up the splash image! */
2946 splash_init(sc->adp, scsplash_callback, sc);
2947 sc->flags |= SC_SPLASH_SCRN;
2949 #endif /* NSPLASH */
2952 /* the rest is not necessary, if we have done it once */
2953 if (sc->flags & SC_INIT_DONE) {
2957 /* initialize mapscrn arrays to a one to one map */
2958 for (i = 0; i < sizeof(sc->scr_map); i++)
2959 sc->scr_map[i] = sc->scr_rmap[i] = i;
2961 sc->flags |= SC_INIT_DONE;
2965 scterm(int unit, int flags)
2970 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2972 return; /* shouldn't happen */
2974 lwkt_gettoken(&tty_token);
2976 /* this console is no longer available for the splash screen */
2977 if (sc->flags & SC_SPLASH_SCRN) {
2978 splash_term(sc->adp);
2979 sc->flags &= ~SC_SPLASH_SCRN;
2981 #endif /* NSPLASH */
2984 /* move the hardware cursor to the upper-left corner */
2985 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
2988 /* release the keyboard and the video card */
2989 if (sc->keyboard >= 0)
2990 kbd_release(sc->kbd, &sc->keyboard);
2991 if (sc->adapter >= 0)
2992 vid_release(sc->adp, &sc->adapter);
2995 * Stop the terminal emulator, if any. If operating on the
2996 * kernel console sc->dev may not be setup yet.
2998 if (flags & SC_KERNEL_CONSOLE)
2999 scp = sc->console_scp;
3001 scp = SC_STAT(sc->dev[0]);
3003 (*scp->tsw->te_term)(scp, &scp->ts);
3004 if (scp->ts != NULL)
3005 kfree(scp->ts, M_SYSCONS);
3007 /* clear the structure */
3008 if (!(flags & SC_KERNEL_CONSOLE)) {
3009 /* XXX: We need delete_dev() for this */
3010 kfree(sc->dev, M_SYSCONS);
3012 /* XXX: We need a ttyunregister for this */
3013 kfree(sc->tty, M_SYSCONS);
3015 #ifndef SC_NO_FONT_LOADING
3016 kfree(sc->font_8, M_SYSCONS);
3017 kfree(sc->font_14, M_SYSCONS);
3018 kfree(sc->font_16, M_SYSCONS);
3020 /* XXX vtb, history */
3022 bzero(sc, sizeof(*sc));
3025 lwkt_reltoken(&tty_token);
3029 scshutdown(void *arg, int howto)
3031 /* assert(sc_console != NULL) */
3033 lwkt_gettoken(&tty_token);
3035 sc_touch_scrn_saver();
3036 if (!cold && sc_console
3037 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
3038 && sc_console->smode.mode == VT_AUTO) {
3039 sc_switch_scr(sc_console->sc, sc_console->index);
3041 shutdown_in_progress = TRUE;
3043 lwkt_reltoken(&tty_token);
3047 sc_clean_up(scr_stat *scp)
3051 #endif /* NSPLASH */
3053 lwkt_gettoken(&tty_token);
3054 if (scp->sc->flags & SC_SCRN_BLANKED) {
3055 sc_touch_scrn_saver();
3057 if ((error = wait_scrn_saver_stop(scp->sc))) {
3058 lwkt_reltoken(&tty_token);
3061 #endif /* NSPLASH */
3063 scp->status |= MOUSE_HIDDEN;
3064 sc_remove_mouse_image(scp);
3065 sc_remove_cutmarking(scp);
3066 lwkt_reltoken(&tty_token);
3071 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3076 lwkt_gettoken(&tty_token);
3078 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3079 if (!discard && (old.vtb_flags & VTB_VALID)) {
3080 /* retain the current cursor position and buffer contants */
3081 scp->cursor_oldpos = scp->cursor_pos;
3083 * This works only if the old buffer has the same size as or larger
3084 * than the new one. XXX
3086 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3090 sc_vtb_destroy(&old);
3093 #ifndef SC_NO_SYSMOUSE
3094 /* move the mouse cursor at the center of the screen */
3095 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3097 lwkt_reltoken(&tty_token);
3101 alloc_scp(sc_softc_t *sc, int vty)
3105 /* assert(sc_malloc) */
3107 scp = kmalloc(sizeof(scr_stat), M_SYSCONS, M_WAITOK);
3108 init_scp(sc, vty, scp);
3110 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3111 if (sc_init_emulator(scp, SC_DFLT_TERM))
3112 sc_init_emulator(scp, "*");
3114 #ifndef SC_NO_CUTPASTE
3115 sc_alloc_cut_buffer(scp, TRUE);
3118 #ifndef SC_NO_HISTORY
3119 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3125 * NOTE: Must be called with tty_token held.
3128 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3132 bzero(scp, sizeof(*scp));
3137 scp->mode = sc->initial_mode;
3138 callout_init(&scp->blink_screen_ch);
3139 lwkt_gettoken(&tty_token);
3140 (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
3141 lwkt_reltoken(&tty_token);
3142 if (info.vi_flags & V_INFO_GRAPHICS) {
3143 scp->status |= GRAPHICS_MODE;
3144 scp->xpixel = info.vi_width;
3145 scp->ypixel = info.vi_height;
3146 scp->xsize = info.vi_width/8;
3147 scp->ysize = info.vi_height/info.vi_cheight;
3151 scp->xsize = info.vi_width;
3152 scp->ysize = info.vi_height;
3153 scp->xpixel = scp->xsize*8;
3154 scp->ypixel = scp->ysize*info.vi_cheight;
3155 if (info.vi_cheight < 14) {
3157 #ifndef SC_NO_FONT_LOADING
3158 scp->font = sc->font_8;
3162 } else if (info.vi_cheight >= 16) {
3163 scp->font_size = 16;
3164 #ifndef SC_NO_FONT_LOADING
3165 scp->font = sc->font_16;
3170 scp->font_size = 14;
3171 #ifndef SC_NO_FONT_LOADING
3172 scp->font = sc->font_14;
3178 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3179 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3180 scp->xoff = scp->yoff = 0;
3181 scp->xpos = scp->ypos = 0;
3182 scp->start = scp->xsize * scp->ysize - 1;
3187 scp->border = BG_BLACK;
3188 scp->cursor_base = sc->cursor_base;
3189 scp->cursor_height = imin(sc->cursor_height, scp->font_size);
3190 scp->mouse_cut_start = scp->xsize*scp->ysize;
3191 scp->mouse_cut_end = -1;
3192 scp->mouse_signal = 0;
3194 scp->mouse_proc = NULL;
3195 scp->kbd_mode = K_XLATE;
3196 scp->bell_pitch = bios_value.bell_pitch;
3197 scp->bell_duration = BELL_DURATION;
3198 scp->status |= (bios_value.shift_state & NLKED);
3199 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3202 scp->smode.mode = VT_AUTO;
3203 scp->history = NULL;
3204 scp->history_pos = 0;
3205 scp->history_size = 0;
3209 sc_init_emulator(scr_stat *scp, char *name)
3216 if (name == NULL) /* if no name is given, use the current emulator */
3218 else /* ...otherwise find the named emulator */
3219 sw = sc_term_match(name);
3225 if (strcmp(sw->te_renderer, "*") != 0) {
3226 rndr = sc_render_match(scp, sw->te_renderer, scp->model);
3229 rndr = sc_render_match(scp, scp->sc->adp->va_name, scp->model);
3235 if (sw == scp->tsw) {
3236 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3238 sc_clear_screen(scp);
3239 /* assert(error == 0); */
3243 if (sc_malloc && (sw->te_size > 0))
3244 p = kmalloc(sw->te_size, M_SYSCONS, M_NOWAIT);
3247 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3253 (*scp->tsw->te_term)(scp, &scp->ts);
3254 if (scp->ts != NULL)
3255 kfree(scp->ts, M_SYSCONS);
3261 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3262 sc_clear_screen(scp);
3268 * scgetc(flags) - get character from keyboard.
3269 * If flags & SCGETC_CN, then avoid harmful side effects.
3270 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3271 * return NOKEY if there is nothing there.
3274 scgetc(sc_softc_t *sc, u_int flags)
3277 #ifndef SC_NO_HISTORY
3285 lwkt_gettoken(&tty_token);
3286 if (sc->kbd == NULL) {
3287 lwkt_reltoken(&tty_token);
3293 /* I don't like this, but... XXX */
3294 if (flags & SCGETC_CN) {
3296 sccnupdate(sc->cur_scp);
3301 /* first see if there is something in the keyboard port */
3303 c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3305 if (!(flags & SCGETC_CN))
3306 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3307 } else if (c == NOKEY) {
3308 lwkt_reltoken(&tty_token);
3315 /* make screensaver happy */
3317 sc_touch_scrn_saver();
3319 if (!(flags & SCGETC_CN))
3320 /* do the /dev/random device a favour */
3321 add_keyboard_randomness(c);
3323 if (scp->kbd_mode != K_XLATE) {
3324 lwkt_reltoken(&tty_token);
3328 /* if scroll-lock pressed allow history browsing */
3329 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3331 scp->status &= ~CURSOR_ENABLED;
3332 sc_remove_cursor_image(scp);
3334 #ifndef SC_NO_HISTORY
3335 if (!(scp->status & BUFFER_SAVED)) {
3336 scp->status |= BUFFER_SAVED;
3340 /* FIXME: key codes */
3341 case SPCLKEY | FKEY | F(49): /* home key */
3342 sc_remove_cutmarking(scp);
3346 case SPCLKEY | FKEY | F(57): /* end key */
3347 sc_remove_cutmarking(scp);
3351 case SPCLKEY | FKEY | F(50): /* up arrow key */
3352 sc_remove_cutmarking(scp);
3353 if (sc_hist_up_line(scp))
3354 if (!(flags & SCGETC_CN))
3355 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3358 case SPCLKEY | FKEY | F(58): /* down arrow key */
3359 sc_remove_cutmarking(scp);
3360 if (sc_hist_down_line(scp))
3361 if (!(flags & SCGETC_CN))
3362 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3365 case SPCLKEY | FKEY | F(51): /* page up key */
3366 sc_remove_cutmarking(scp);
3367 for (i=0; i<scp->ysize; i++)
3368 if (sc_hist_up_line(scp)) {
3369 if (!(flags & SCGETC_CN))
3370 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3375 case SPCLKEY | FKEY | F(59): /* page down key */
3376 sc_remove_cutmarking(scp);
3377 for (i=0; i<scp->ysize; i++)
3378 if (sc_hist_down_line(scp)) {
3379 if (!(flags & SCGETC_CN))
3380 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3385 #endif /* SC_NO_HISTORY */
3389 * Process and consume special keys here. Return a plain char code
3390 * or a char code with the META flag or a function key code.
3394 /* goto next_code */
3399 switch (KEYCHAR(c)) {
3401 case NLK: case CLK: case ALK:
3404 kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3406 scp->status |= SLKED;
3408 if (scp->status & SLKED) {
3409 scp->status &= ~SLKED;
3410 #ifndef SC_NO_HISTORY
3411 if (scp->status & BUFFER_SAVED) {
3412 if (!sc_hist_restore(scp))
3413 sc_remove_cutmarking(scp);
3414 scp->status &= ~BUFFER_SAVED;
3415 scp->status |= CURSOR_ENABLED;
3416 sc_draw_cursor_image(scp);
3418 tp = VIRTUAL_TTY(sc, scp->index);
3426 /* NON-LOCKING KEYS */
3428 case LSH: case RSH: case LCTR: case RCTR:
3429 case LALT: case RALT: case ASH: case META:
3433 if (!(sc->flags & SC_SCRN_BLANKED)) {
3434 lwkt_reltoken(&tty_token);
3441 /* force activatation/deactivation of the screen saver */
3442 if (!(sc->flags & SC_SCRN_BLANKED)) {
3443 run_scrn_saver = TRUE;
3444 sc->scrn_time_stamp -= scrn_blank_time;
3448 * While devices are being probed, the screen saver need
3449 * to be invoked explictly. XXX
3451 if (sc->flags & SC_SCRN_BLANKED) {
3452 scsplash_stick(FALSE);
3453 stop_scrn_saver(sc, current_saver);
3455 if (!ISGRAPHSC(scp)) {
3456 scsplash_stick(TRUE);
3457 (*current_saver)(sc, TRUE);
3461 #endif /* NSPLASH */
3465 #ifndef SC_DISABLE_REBOOT
3471 #ifndef SC_DISABLE_REBOOT
3472 shutdown_nice(RB_HALT);
3477 #ifndef SC_DISABLE_REBOOT
3478 shutdown_nice(RB_HALT|RB_POWEROFF);
3484 apm_suspend(PMST_SUSPEND);
3487 apm_suspend(PMST_STANDBY);
3496 #ifndef SC_DISABLE_DDBKEY
3498 lwkt_reltoken(&tty_token);
3499 Debugger("manual escape to debugger");
3500 lwkt_gettoken(&tty_token);
3502 kprintf("No debugger in kernel\n");
3504 #else /* SC_DISABLE_DDBKEY */
3506 #endif /* SC_DISABLE_DDBKEY */
3510 if (enable_panic_key)
3511 panic("Forced by the panic key");
3515 this_scr = scp->index;
3516 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3517 sc->first_vty + i != this_scr;
3518 i = (i + 1)%sc->vtys) {
3519 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3520 if (ISTTYOPEN(tp)) {
3522 sc_switch_scr(scp->sc, sc->first_vty + i);
3530 this_scr = scp->index;
3531 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3532 sc->first_vty + i != this_scr;
3533 i = (i + sc->vtys - 1)%sc->vtys) {
3534 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3535 if (ISTTYOPEN(tp)) {
3537 sc_switch_scr(scp->sc, sc->first_vty + i);
3545 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3547 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3551 /* assert(c & FKEY) */
3552 if (!(sc->flags & SC_SCRN_BLANKED)) {
3553 lwkt_reltoken(&tty_token);
3558 /* goto next_code */
3560 /* regular keys (maybe MKEY is set) */
3561 if (!(sc->flags & SC_SCRN_BLANKED)) {
3562 lwkt_reltoken(&tty_token);
3572 scmmap(struct dev_mmap_args *ap)
3576 lwkt_gettoken(&tty_token);
3577 scp = SC_STAT(ap->a_head.a_dev);
3578 if (scp != scp->sc->cur_scp) {
3579 lwkt_reltoken(&tty_token);
3582 ap->a_result = (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, ap->a_offset,
3584 lwkt_reltoken(&tty_token);
3589 save_kbd_state(scr_stat *scp)
3594 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3595 if (error == ENOIOCTL)
3598 scp->status &= ~LOCK_MASK;
3599 scp->status |= state;
3605 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3610 if (mask != LOCK_MASK) {
3611 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3612 if (error == ENOIOCTL)
3618 state |= new_bits & mask;
3620 state = new_bits & LOCK_MASK;
3622 error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3623 if (error == ENOIOCTL)
3629 update_kbd_leds(scr_stat *scp, int which)
3634 error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3635 if (error == ENOIOCTL)
3641 set_mode(scr_stat *scp)
3645 lwkt_gettoken(&tty_token);
3646 /* reject unsupported mode */
3647 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info)) {
3648 lwkt_reltoken(&tty_token);
3652 /* if this vty is not currently showing, do nothing */
3653 if (scp != scp->sc->cur_scp) {
3654 lwkt_reltoken(&tty_token);
3658 /* setup video hardware for the given mode */
3659 (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3660 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3661 (void *)scp->sc->adp->va_window, FALSE);
3663 #ifndef SC_NO_FONT_LOADING
3664 /* load appropriate font */
3665 if (!(scp->status & GRAPHICS_MODE)) {
3666 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3667 if (scp->font_size < 14) {
3668 if (scp->sc->fonts_loaded & FONT_8)
3669 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
3670 } else if (scp->font_size >= 16) {
3671 if (scp->sc->fonts_loaded & FONT_16)
3672 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
3674 if (scp->sc->fonts_loaded & FONT_14)
3675 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
3679 * This is an interim kludge to display correct font.
3680 * Always use the font page #0 on the video plane 2.
3681 * Somehow we cannot show the font in other font pages on
3682 * some video cards... XXX
3684 sc_show_font(scp, 0);
3688 #endif /* !SC_NO_FONT_LOADING */
3690 sc_set_border(scp, scp->border);
3691 sc_set_cursor_image(scp);
3693 lwkt_reltoken(&tty_token);
3698 refresh_ega_palette(scr_stat *scp)
3702 int rsize, gsize, bsize;
3703 int rfld, gfld, bfld;
3706 rsize = scp->sc->adp->va_info.vi_pixel_fsizes[0];
3707 gsize = scp->sc->adp->va_info.vi_pixel_fsizes[1];
3708 bsize = scp->sc->adp->va_info.vi_pixel_fsizes[2];
3709 rfld = scp->sc->adp->va_info.vi_pixel_fields[0];
3710 gfld = scp->sc->adp->va_info.vi_pixel_fields[1];
3711 bfld = scp->sc->adp->va_info.vi_pixel_fields[2];
3713 for (i = 0; i < 16; i++) {
3714 reg = scp->sc->adp->va_palette_regs[i];
3716 r = scp->sc->palette[reg * 3] >> (8 - rsize);
3717 g = scp->sc->palette[reg * 3 + 1] >> (8 - gsize);
3718 b = scp->sc->palette[reg * 3 + 2] >> (8 - bsize);
3720 scp->ega_palette[i] = (r << rfld) + (g << gfld) + (b << bfld);
3725 sc_set_border(scr_stat *scp, int color)
3727 ++scp->sc->videoio_in_progress;
3728 (*scp->rndr->draw_border)(scp, color);
3729 --scp->sc->videoio_in_progress;
3732 #ifndef SC_NO_FONT_LOADING
3734 sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
3735 int base, int count)
3740 sc->font_loading_in_progress = TRUE;
3741 (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
3742 sc->font_loading_in_progress = FALSE;
3746 sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
3747 int base, int count)
3752 sc->font_loading_in_progress = TRUE;
3753 (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
3754 sc->font_loading_in_progress = FALSE;
3758 sc_show_font(scr_stat *scp, int page)
3760 (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3762 #endif /* !SC_NO_FONT_LOADING */
3765 sc_paste(scr_stat *scp, u_char *p, int count)
3770 lwkt_gettoken(&tty_token);
3771 if (scp->status & MOUSE_VISIBLE) {
3772 tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3773 if (!ISTTYOPEN(tp)) {
3774 lwkt_reltoken(&tty_token);
3777 rmap = scp->sc->scr_rmap;
3778 for (; count > 0; --count)
3779 (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3781 lwkt_reltoken(&tty_token);
3785 sc_bell(scr_stat *scp, int pitch, int duration)
3787 if (cold || shutdown_in_progress)
3790 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL)) {
3794 if (scp->sc->flags & SC_VISUAL_BELL) {
3795 if (scp->sc->blink_in_progress) {
3798 scp->sc->blink_in_progress = 3;
3799 if (scp != scp->sc->cur_scp)
3800 scp->sc->blink_in_progress += 2;
3801 sc_blink_screen(scp->sc->cur_scp);
3802 } else if (duration != 0 && pitch != 0) {
3803 if (scp != scp->sc->cur_scp)
3805 sysbeep(pitch, duration);
3810 * Two versions of blink_screen(), one called from the console path
3811 * with the syscons locked, and one called from a timer callout.
3814 sc_blink_screen(scr_stat *scp)
3816 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3817 scp->sc->blink_in_progress = 0;
3819 if (scp->sc->delayed_next_scr)
3820 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3822 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3823 scp->sc->blink_in_progress & 1);
3824 scp->sc->blink_in_progress--;
3829 blink_screen_callout(void *arg)
3831 scr_stat *scp = arg;
3834 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3836 scp->sc->blink_in_progress = 0;
3839 tp = VIRTUAL_TTY(scp->sc, scp->index);
3842 if (scp->sc->delayed_next_scr) {
3844 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3849 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3850 scp->sc->blink_in_progress & 1);
3851 scp->sc->blink_in_progress--;
3853 callout_reset(&scp->blink_screen_ch, hz / 10,
3854 blink_screen_callout, scp);
3860 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3861 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3866 sc_allocate_keyboard(sc_softc_t *sc, int unit)
3872 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3874 k0 = kbd_get_keyboard(idx0);
3876 for (idx = kbd_find_keyboard2("*", -1, 0, 0);
3878 idx = kbd_find_keyboard2("*", -1, idx + 1, 0)) {
3879 k = kbd_get_keyboard(idx);
3881 if (idx == idx0 || KBD_IS_BUSY(k))
3884 bzero(&ki, sizeof(ki));
3885 strcpy(ki.kb_name, k->kb_name);
3886 ki.kb_unit = k->kb_unit;
3888 kbd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3891 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);