2 * Copyright (c) 1992-1998 Søren Schmidt
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_syscons.h"
37 #include "opt_splash.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/consio.h>
47 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
56 #include <sys/random.h>
57 #include <sys/reboot.h>
58 #include <sys/serial.h>
59 #include <sys/signalvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/power.h>
64 #include <machine/clock.h>
65 #if defined(__arm__) || defined(__mips__) || \
66 defined(__powerpc__) || defined(__sparc64__)
67 #include <machine/sc_machdep.h>
69 #include <machine/pc/display.h>
71 #if defined( __i386__) || defined(__amd64__)
72 #include <machine/psl.h>
73 #include <machine/frame.h>
75 #include <machine/stdarg.h>
77 #include <dev/kbd/kbdreg.h>
78 #include <dev/fb/fbreg.h>
79 #include <dev/fb/splashreg.h>
80 #include <dev/syscons/syscons.h>
85 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
86 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
88 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
90 /* NULL-safe version of "tty_opened()" */
91 #define tty_opened_ns(tp) ((tp) != NULL && tty_opened(tp))
93 typedef struct default_attr {
94 int std_color; /* normal hardware color */
95 int rev_color; /* reverse hardware color */
98 static default_attr user_default = {
103 static int sc_console_unit = -1;
104 static int sc_saver_keyb_only = 1;
105 static scr_stat *sc_console;
106 static struct consdev *sc_consptr;
107 static scr_stat main_console;
108 static struct tty *main_devs[MAXCONS];
110 static char init_done = COLD;
111 static int shutdown_in_progress = FALSE;
112 static int suspend_in_progress = FALSE;
113 static char sc_malloc = FALSE;
115 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
116 static int run_scrn_saver = FALSE; /* should run the saver? */
117 static int enable_bell = TRUE; /* enable beeper */
119 #ifndef SC_DISABLE_REBOOT
120 static int enable_reboot = TRUE; /* enable keyboard reboot */
123 #ifndef SC_DISABLE_KDBKEY
124 static int enable_kdbkey = TRUE; /* enable keyboard debug */
127 static long scrn_blank_time = 0; /* screen saver timeout value */
129 static int scrn_blanked; /* # of blanked screen */
130 static int sticky_splash = FALSE;
132 static void none_saver(sc_softc_t *sc, int blank) { }
133 static void (*current_saver)(sc_softc_t *, int) = none_saver;
136 #ifdef SC_NO_SUSPEND_VTYSWITCH
137 static int sc_no_suspend_vtswitch = 1;
139 static int sc_no_suspend_vtswitch = 0;
141 static int sc_susp_scr;
143 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
144 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
145 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
146 &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
147 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell,
149 #ifndef SC_DISABLE_REBOOT
150 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
151 0, "enable keyboard reboot");
153 #ifndef SC_DISABLE_KDBKEY
154 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
155 0, "enable keyboard debug");
157 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN,
158 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
159 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
163 tsw_ioctl_t *sc_user_ioctl;
165 static bios_values_t bios_value;
167 static int enable_panic_key;
168 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
169 0, "Enable panic via keypress specified in kbdmap(5)");
171 #define SC_CONSOLECTL 255
173 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty))
178 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
179 static int scvidprobe(int unit, int flags, int cons);
180 static int sckbdprobe(int unit, int flags, int cons);
181 static void scmeminit(void *arg);
182 static int scdevtounit(struct tty *tp);
183 static kbd_callback_func_t sckbdevent;
184 static void scinit(int unit, int flags);
185 static scr_stat *sc_get_stat(struct tty *tp);
186 static void scterm(int unit, int flags);
187 static void scshutdown(void *, int);
188 static void scsuspend(void *);
189 static void scresume(void *);
190 static u_int scgetc(sc_softc_t *sc, u_int flags);
191 static void sc_puts(scr_stat *scp, u_char *buf, int len, int kernel);
193 #define SCGETC_NONBLOCK 2
194 static void sccnupdate(scr_stat *scp);
195 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
196 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
197 static timeout_t scrn_timer;
198 static int and_region(int *s1, int *e1, int s2, int e2);
199 static void scrn_update(scr_stat *scp, int show_cursor);
202 static int scsplash_callback(int event, void *arg);
203 static void scsplash_saver(sc_softc_t *sc, int show);
204 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
205 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
206 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
207 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
208 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
209 static int wait_scrn_saver_stop(sc_softc_t *sc);
210 #define scsplash_stick(stick) (sticky_splash = (stick))
211 #else /* !DEV_SPLASH */
212 #define scsplash_stick(stick)
213 #endif /* DEV_SPLASH */
215 static int do_switch_scr(sc_softc_t *sc, int s);
216 static int vt_proc_alive(scr_stat *scp);
217 static int signal_vt_rel(scr_stat *scp);
218 static int signal_vt_acq(scr_stat *scp);
219 static int finish_vt_rel(scr_stat *scp, int release, int *s);
220 static int finish_vt_acq(scr_stat *scp);
221 static void exchange_scr(sc_softc_t *sc);
222 static void update_cursor_image(scr_stat *scp);
223 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
224 static void update_font(scr_stat *);
225 static int save_kbd_state(scr_stat *scp);
226 static int update_kbd_state(scr_stat *scp, int state, int mask);
227 static int update_kbd_leds(scr_stat *scp, int which);
228 static timeout_t blink_screen;
229 static struct tty *sc_alloc_tty(int, int);
231 static cn_probe_t sc_cnprobe;
232 static cn_init_t sc_cninit;
233 static cn_term_t sc_cnterm;
234 static cn_getc_t sc_cngetc;
235 static cn_putc_t sc_cnputc;
236 static cn_grab_t sc_cngrab;
237 static cn_ungrab_t sc_cnungrab;
241 static tsw_open_t sctty_open;
242 static tsw_close_t sctty_close;
243 static tsw_outwakeup_t sctty_outwakeup;
244 static tsw_ioctl_t sctty_ioctl;
245 static tsw_mmap_t sctty_mmap;
247 static struct ttydevsw sc_ttydevsw = {
248 .tsw_open = sctty_open,
249 .tsw_close = sctty_close,
250 .tsw_outwakeup = sctty_outwakeup,
251 .tsw_ioctl = sctty_ioctl,
252 .tsw_mmap = sctty_mmap,
255 static d_ioctl_t consolectl_ioctl;
256 static d_close_t consolectl_close;
258 static struct cdevsw consolectl_devsw = {
259 .d_version = D_VERSION,
260 .d_flags = D_NEEDGIANT | D_TRACKCLOSE,
261 .d_ioctl = consolectl_ioctl,
262 .d_close = consolectl_close,
263 .d_name = "consolectl",
267 sc_probe_unit(int unit, int flags)
269 if (!vty_enabled(VTY_SC))
271 if (!scvidprobe(unit, flags, FALSE)) {
273 printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
277 /* syscons will be attached even when there is no keyboard */
278 sckbdprobe(unit, flags, FALSE);
283 /* probe video adapters, return TRUE if found */
285 scvidprobe(int unit, int flags, int cons)
288 * Access the video adapter driver through the back door!
289 * Video adapter drivers need to be configured before syscons.
290 * However, when syscons is being probed as the low-level console,
291 * they have not been initialized yet. We force them to initialize
292 * themselves here. XXX
294 vid_configure(cons ? VIO_PROBE_ONLY : 0);
296 return (vid_find_adapter("*", unit) >= 0);
299 /* probe the keyboard, return TRUE if found */
301 sckbdprobe(int unit, int flags, int cons)
303 /* access the keyboard driver through the backdoor! */
304 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
306 return (kbd_find_keyboard("*", unit) >= 0);
310 *adapter_name(video_adapter_t *adp)
316 { KD_MONO, { "MDA", "MDA" } },
317 { KD_HERCULES, { "Hercules", "Hercules" } },
318 { KD_CGA, { "CGA", "CGA" } },
319 { KD_EGA, { "EGA", "EGA (mono)" } },
320 { KD_VGA, { "VGA", "VGA (mono)" } },
321 { KD_PC98, { "PC-98x1", "PC-98x1" } },
322 { KD_TGA, { "TGA", "TGA" } },
323 { -1, { "Unknown", "Unknown" } },
327 for (i = 0; names[i].type != -1; ++i)
328 if (names[i].type == adp->va_type)
330 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
334 sctty_outwakeup(struct tty *tp)
338 scr_stat *scp = sc_get_stat(tp);
340 if (scp->status & SLKED ||
341 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
345 len = ttydisc_getc(tp, buf, sizeof buf);
348 sc_puts(scp, buf, len, 0);
353 sc_alloc_tty(int index, int devnum)
355 struct sc_ttysoftc *stc;
358 /* Allocate TTY object and softc to store unit number. */
359 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK);
360 stc->st_index = index;
362 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant);
364 /* Create device node. */
365 tty_makedev(tp, NULL, "v%r", devnum);
372 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit)
382 (void)resource_int_value("sc", unit, "vesa_mode", &vmode);
383 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX ||
384 vidd_get_info(sc->adp, vmode, &info) != 0 ||
385 !sc_support_pixel_mode(&info))
389 * If the mode is unset or unsupported, search for an available
390 * 800x600 graphics mode with the highest color depth.
393 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++)
394 if (vidd_get_info(sc->adp, i, &info) == 0 &&
395 info.vi_width == 800 && info.vi_height == 600 &&
396 sc_support_pixel_mode(&info) &&
397 info.vi_depth > depth) {
399 depth = info.vi_depth;
403 vidd_get_info(sc->adp, vmode, &info);
406 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
407 fontsize = info.vi_cheight;
409 fontsize = scp->font_size;
413 else if (fontsize >= 16)
417 #ifndef SC_NO_FONT_LOADING
420 if ((sc->fonts_loaded & FONT_8) == 0)
425 if ((sc->fonts_loaded & FONT_14) == 0)
430 if ((sc->fonts_loaded & FONT_16) == 0)
439 if ((sc->flags & SC_SPLASH_SCRN) != 0)
440 splash_term(sc->adp);
442 #ifndef SC_NO_HISTORY
443 if (scp->history != NULL) {
444 sc_vtb_append(&scp->vtb, 0, scp->history,
445 scp->ypos * scp->xsize + scp->xpos);
446 scp->history_pos = sc_vtb_tail(scp->history);
449 vidd_set_mode(sc->adp, vmode);
450 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
451 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
452 scp->xpixel = info.vi_width;
453 scp->ypixel = info.vi_height;
454 scp->xsize = scp->xpixel / 8;
455 scp->ysize = scp->ypixel / fontsize;
457 scp->ypos = scp->ysize - 1;
458 scp->xoff = scp->yoff = 0;
460 scp->font_size = fontsize;
462 scp->start = scp->xsize * scp->ysize - 1;
464 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize;
465 scp->mode = sc->initial_mode = vmode;
467 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
468 (void *)sc->adp->va_window, FALSE);
470 sc_alloc_scr_buffer(scp, FALSE, FALSE);
471 sc_init_emulator(scp, NULL);
472 #ifndef SC_NO_CUTPASTE
473 sc_alloc_cut_buffer(scp, FALSE);
475 #ifndef SC_NO_HISTORY
476 sc_alloc_history_buffer(scp, 0, 0, FALSE);
478 sc_set_border(scp, scp->border);
479 sc_set_cursor_image(scp);
480 scp->status &= ~UNKNOWN_MODE;
482 if ((sc->flags & SC_SPLASH_SCRN) != 0)
483 splash_init(sc->adp, scsplash_callback, sc);
489 sc_attach_unit(int unit, int flags)
496 if (!vty_enabled(VTY_SC))
499 flags &= ~SC_KERNEL_CONSOLE;
501 if (sc_console_unit == unit) {
503 * If this unit is being used as the system console, we need to
504 * adjust some variables and buffers before and after scinit().
506 /* assert(sc_console != NULL) */
507 flags |= SC_KERNEL_CONSOLE;
512 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
514 callout_init(&sc->ctimeout, 0);
515 callout_init(&sc->cblink, 0);
516 scp = sc_get_stat(sc->dev[0]);
517 if (sc_console == NULL) /* sc_console_unit < 0 */
521 if ((sc->config & SC_VESAMODE) != 0)
522 sc_set_vesa_mode(scp, sc, unit);
523 #endif /* SC_PIXEL_MODE */
525 /* initialize cursor */
527 update_cursor_image(scp);
529 /* get screen update going */
532 /* set up the keyboard */
533 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
534 update_kbd_state(scp, scp->status, LOCK_MASK);
536 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
537 SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
539 printf("%s%d:", SC_DRIVER_NAME, unit);
540 if (sc->adapter >= 0)
541 printf(" fb%d", sc->adapter);
542 if (sc->keyboard >= 0)
543 printf(", kbd%d", sc->keyboard);
545 printf(", terminal emulator: %s (%s)",
546 scp->tsw->te_name, scp->tsw->te_desc);
550 /* Register suspend/resume/shutdown callbacks for the kernel console. */
551 if (sc_console_unit == unit) {
552 EVENTHANDLER_REGISTER(power_suspend_early, scsuspend, NULL,
553 EVENTHANDLER_PRI_ANY);
554 EVENTHANDLER_REGISTER(power_resume, scresume, NULL,
555 EVENTHANDLER_PRI_ANY);
556 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL,
557 SHUTDOWN_PRI_DEFAULT);
560 for (vc = 0; vc < sc->vtys; vc++) {
561 if (sc->dev[vc] == NULL) {
562 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS);
563 if (vc == 0 && sc->dev == main_devs)
564 SC_STAT(sc->dev[0]) = &main_console;
567 * The first vty already has struct tty and scr_stat initialized
568 * in scinit(). The other vtys will have these structs when
573 dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600,
575 dev->si_drv1 = sc->dev[0];
583 if (!vty_enabled(VTY_SC))
590 * As soon as malloc() becomes functional, we had better allocate
591 * various buffers for the kernel console.
594 if (sc_console_unit < 0) /* sc_console == NULL */
597 /* copy the temporary buffer to the final buffer */
598 sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
600 #ifndef SC_NO_CUTPASTE
601 sc_alloc_cut_buffer(sc_console, FALSE);
604 #ifndef SC_NO_HISTORY
605 /* initialize history buffer & pointers */
606 sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
611 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
614 scdevtounit(struct tty *tp)
616 int vty = SC_VTY(tp);
618 if (vty == SC_CONSOLECTL)
619 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
620 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
627 sctty_open(struct tty *tp)
629 int unit = scdevtounit(tp);
636 DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
637 devtoname(tp->t_dev), unit, SC_VTY(tp)));
639 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
643 if (!tty_opened(tp)) {
644 /* Use the current setting of the <-- key as default VERASE. */
645 /* If the Delete key is preferable, an stty is necessary */
647 if (sc->kbd != NULL) {
648 key.keynum = KEYCODE_BS;
649 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
650 tp->t_termios.c_cc[VERASE] = key.key.map[0];
655 scp = sc_get_stat(tp);
657 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp));
659 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8);
661 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
662 tp->t_winsize.ws_col = scp->xsize;
663 tp->t_winsize.ws_row = scp->ysize;
670 sctty_close(struct tty *tp)
675 if (SC_VTY(tp) != SC_CONSOLECTL) {
676 scp = sc_get_stat(tp);
677 /* were we in the middle of the VT switching process? */
678 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
680 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
681 cnavailable(sc_consptr, TRUE);
682 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */
683 DPRINTF(5, ("reset WAIT_REL, "));
684 if (finish_vt_acq(scp) == 0) /* force acknowledge */
685 DPRINTF(5, ("reset WAIT_ACQ, "));
687 if (scp == &main_console) {
690 scp->smode.mode = VT_AUTO;
693 sc_vtb_destroy(&scp->vtb);
695 sc_vtb_destroy(&scp->scr);
697 sc_free_history_buffer(scp, scp->ysize);
704 scp->smode.mode = VT_AUTO;
706 scp->kbd_mode = K_XLATE;
707 if (scp == scp->sc->cur_scp)
708 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
709 DPRINTF(5, ("done.\n"));
713 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */
715 scread(struct cdev *dev, struct uio *uio, int flag)
717 if (!sc_saver_keyb_only)
718 sc_touch_scrn_saver();
719 return ttyread(dev, uio, flag);
724 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
732 sc = (sc_softc_t *)arg;
733 /* assert(thiskbd == sc->kbd) */
740 case KBDIO_UNLOADING:
743 kbd_release(thiskbd, (void *)&sc->keyboard);
751 * Loop while there is still input to get from the keyboard.
752 * I don't think this is nessesary, and it doesn't fix
753 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
755 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
757 cur_tty = SC_DEV(sc, sc->cur_scp->index);
758 if (!tty_opened_ns(cur_tty))
761 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
764 switch (KEYFLAGS(c)) {
765 case 0x0000: /* normal key */
766 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
768 case FKEY: /* function key, return string */
769 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c);
771 ttydisc_rint_simple(cur_tty, cp, strlen(cp));
774 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
776 ttydisc_rint_simple(cur_tty, cp, len);
778 case MKEY: /* meta is active, prepend ESC */
779 ttydisc_rint(cur_tty, 0x1b, 0);
780 ttydisc_rint(cur_tty, KEYCHAR(c), 0);
782 case BKEY: /* backtab fixed sequence (esc [ Z) */
783 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3);
787 ttydisc_rint_done(cur_tty);
790 sc->cur_scp->status |= MOUSE_HIDDEN;
798 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
805 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
806 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
810 /* If there is a user_ioctl function call that first */
812 error = (*sc_user_ioctl)(tp, cmd, data, td);
813 if (error != ENOIOCTL)
817 error = sc_vid_ioctl(tp, cmd, data, td);
818 if (error != ENOIOCTL)
821 #ifndef SC_NO_HISTORY
822 error = sc_hist_ioctl(tp, cmd, data, td);
823 if (error != ENOIOCTL)
827 #ifndef SC_NO_SYSMOUSE
828 error = sc_mouse_ioctl(tp, cmd, data, td);
829 if (error != ENOIOCTL)
833 scp = sc_get_stat(tp);
834 /* assert(scp != NULL) */
835 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
839 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td);
840 if (error != ENOIOCTL)
844 switch (cmd) { /* process console hardware related ioctl's */
846 case GIO_ATTR: /* get current attributes */
847 /* this ioctl is not processed here, but in the terminal emulator */
850 case GIO_COLOR: /* is this a color console ? */
851 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
854 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
855 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
858 scrn_blank_time = *(int *)data;
859 run_scrn_saver = (scrn_blank_time != 0);
863 case CONS_CURSORTYPE: /* set cursor type (obsolete) */
865 *(int *)data &= CONS_CURSOR_ATTRS;
866 sc_change_cursor_shape(scp, *(int *)data, -1, -1);
870 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */
871 if (((int *)data)[0] & CONS_LOCAL_CURSOR) {
872 ((int *)data)[0] = scp->curr_curs_attr.flags;
873 ((int *)data)[1] = scp->curr_curs_attr.base;
874 ((int *)data)[2] = scp->curr_curs_attr.height;
876 ((int *)data)[0] = sc->curs_attr.flags;
877 ((int *)data)[1] = sc->curs_attr.base;
878 ((int *)data)[2] = sc->curs_attr.height;
882 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */
884 sc_change_cursor_shape(scp, ((int *)data)[0],
885 ((int *)data)[1], ((int *)data)[2]);
889 case CONS_BELLTYPE: /* set bell type sound/visual */
890 if ((*(int *)data) & CONS_VISUAL_BELL)
891 sc->flags |= SC_VISUAL_BELL;
893 sc->flags &= ~SC_VISUAL_BELL;
894 if ((*(int *)data) & CONS_QUIET_BELL)
895 sc->flags |= SC_QUIET_BELL;
897 sc->flags &= ~SC_QUIET_BELL;
900 case CONS_GETINFO: /* get current (virtual) console info */
902 vid_info_t *ptr = (vid_info_t*)data;
903 if (ptr->size == sizeof(struct vid_info)) {
904 ptr->m_num = sc->cur_scp->index;
905 ptr->font_size = scp->font_size;
906 ptr->mv_col = scp->xpos;
907 ptr->mv_row = scp->ypos;
908 ptr->mv_csz = scp->xsize;
909 ptr->mv_rsz = scp->ysize;
910 ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
912 * The following fields are filled by the terminal emulator. XXX
919 ptr->mv_grfc.fore = 0; /* not supported */
920 ptr->mv_grfc.back = 0; /* not supported */
921 ptr->mv_ovscan = scp->border;
922 if (scp == sc->cur_scp)
924 ptr->mk_keylock = scp->status & LOCK_MASK;
930 case CONS_GETVERS: /* get version number */
931 *(int*)data = 0x200; /* version 2.0 */
934 case CONS_IDLE: /* see if the screen has been idle */
936 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
937 * the user process may have been writing something on the
938 * screen and syscons is not aware of it. Declare the screen
939 * is NOT idle if it is in one of these modes. But there is
940 * an exception to it; if a screen saver is running in the
941 * graphics mode in the current screen, we should say that the
942 * screen has been idle.
944 *(int *)data = (sc->flags & SC_SCRN_IDLE)
945 && (!ISGRAPHSC(sc->cur_scp)
946 || (sc->cur_scp->status & SAVER_RUNNING));
949 case CONS_SAVERMODE: /* set saver mode */
950 switch(*(int *)data) {
953 /* if a LKM screen saver is running, stop it first. */
954 scsplash_stick(FALSE);
955 saver_mode = *(int *)data;
958 if ((error = wait_scrn_saver_stop(NULL))) {
963 run_scrn_saver = TRUE;
964 if (saver_mode == CONS_USR_SAVER)
965 scp->status |= SAVER_RUNNING;
967 scp->status &= ~SAVER_RUNNING;
968 scsplash_stick(TRUE);
973 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
974 scp->status &= ~SAVER_RUNNING;
975 saver_mode = *(int *)data;
983 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
985 * Note that this ioctl does not guarantee the screen saver
986 * actually starts or stops. It merely attempts to do so...
989 run_scrn_saver = (*(int *)data != 0);
991 sc->scrn_time_stamp -= scrn_blank_time;
995 case CONS_SCRSHOT: /* get a screen shot */
997 int retval, hist_rsz;
999 vm_offset_t frbp, hstp;
1001 scrshot_t *ptr = (scrshot_t *)data;
1002 void *outp = ptr->buf;
1004 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
1007 if (ISGRAPHSC(scp)) {
1011 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
1012 if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
1013 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
1018 lsize = scp->xsize * sizeof(u_int16_t);
1019 csize = ptr->xsize * sizeof(u_int16_t);
1020 /* Pointer to the last line of framebuffer */
1021 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
1023 /* Pointer to the last line of target buffer */
1024 outp = (char *)outp + ptr->ysize * csize;
1025 /* Pointer to the last line of history buffer */
1026 if (scp->history != NULL)
1027 hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
1028 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
1033 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
1034 if (lnum < scp->ysize) {
1038 if (hstp < scp->history->vtb_buffer)
1039 hstp += scp->history->vtb_rows * lsize;
1044 outp = (char *)outp - csize;
1045 retval = copyout((void *)frbp, outp, csize);
1053 case VT_SETMODE: /* set screen switcher mode */
1055 struct vt_mode *mode;
1058 mode = (struct vt_mode *)data;
1059 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
1060 if (scp->smode.mode == VT_PROCESS) {
1061 p1 = pfind(scp->pid);
1062 if (scp->proc == p1 && scp->proc != td->td_proc) {
1065 DPRINTF(5, ("error EPERM\n"));
1072 if (mode->mode == VT_AUTO) {
1073 scp->smode.mode = VT_AUTO;
1076 DPRINTF(5, ("VT_AUTO, "));
1077 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1078 cnavailable(sc_consptr, TRUE);
1079 /* were we in the middle of the vty switching process? */
1080 if (finish_vt_rel(scp, TRUE, &s) == 0)
1081 DPRINTF(5, ("reset WAIT_REL, "));
1082 if (finish_vt_acq(scp) == 0)
1083 DPRINTF(5, ("reset WAIT_ACQ, "));
1085 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1086 || !ISSIGVALID(mode->frsig)) {
1088 DPRINTF(5, ("error EINVAL\n"));
1091 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
1092 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1093 scp->proc = td->td_proc;
1094 scp->pid = scp->proc->p_pid;
1095 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1096 cnavailable(sc_consptr, FALSE);
1103 case VT_GETMODE: /* get screen switcher mode */
1104 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1107 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1108 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1110 ival = IOCPARM_IVAL(data);
1111 data = (caddr_t)&ival;
1114 case VT_RELDISP: /* screen switcher ioctl */
1117 * This must be the current vty which is in the VT_PROCESS
1120 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1124 /* ...and this process is controlling it. */
1125 if (scp->proc != td->td_proc) {
1130 switch(*(int *)data) {
1131 case VT_FALSE: /* user refuses to release screen, abort */
1132 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1133 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1135 case VT_TRUE: /* user has released screen, go on */
1136 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1137 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1139 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1140 if ((error = finish_vt_acq(scp)) == 0)
1141 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1149 case VT_OPENQRY: /* return free virtual console */
1150 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1152 if (!tty_opened_ns(tp)) {
1153 *(int *)data = i + 1;
1159 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1160 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1162 ival = IOCPARM_IVAL(data);
1163 data = (caddr_t)&ival;
1166 case VT_ACTIVATE: /* switch to screen *data */
1167 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1169 error = sc_clean_up(sc->cur_scp);
1173 error = sc_switch_scr(sc, i);
1176 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1177 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1179 ival = IOCPARM_IVAL(data);
1180 data = (caddr_t)&ival;
1183 case VT_WAITACTIVE: /* wait for switch to occur */
1184 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1185 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1187 if (i == sc->cur_scp->index)
1189 error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0);
1192 case VT_GETACTIVE: /* get active vty # */
1193 *(int *)data = sc->cur_scp->index + 1;
1196 case VT_GETINDEX: /* get this vty # */
1197 *(int *)data = scp->index + 1;
1200 case VT_LOCKSWITCH: /* prevent vty switching */
1201 if ((*(int *)data) & 0x01)
1202 sc->flags |= SC_SCRN_VTYLOCK;
1204 sc->flags &= ~SC_SCRN_VTYLOCK;
1207 case KDENABIO: /* allow io operations */
1208 error = priv_check(td, PRIV_IO);
1211 error = securelevel_gt(td->td_ucred, 0);
1215 td->td_frame->tf_eflags |= PSL_IOPL;
1216 #elif defined(__amd64__)
1217 td->td_frame->tf_rflags |= PSL_IOPL;
1221 case KDDISABIO: /* disallow io operations (default) */
1223 td->td_frame->tf_eflags &= ~PSL_IOPL;
1224 #elif defined(__amd64__)
1225 td->td_frame->tf_rflags &= ~PSL_IOPL;
1229 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1230 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1232 ival = IOCPARM_IVAL(data);
1233 data = (caddr_t)&ival;
1236 case KDSKBSTATE: /* set keyboard state (locks) */
1237 if (*(int *)data & ~LOCK_MASK)
1239 scp->status &= ~LOCK_MASK;
1240 scp->status |= *(int *)data;
1241 if (scp == sc->cur_scp)
1242 update_kbd_state(scp, scp->status, LOCK_MASK);
1245 case KDGKBSTATE: /* get keyboard state (locks) */
1246 if (scp == sc->cur_scp)
1247 save_kbd_state(scp);
1248 *(int *)data = scp->status & LOCK_MASK;
1251 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1252 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1253 error = kbdd_ioctl(sc->kbd, cmd, data);
1254 if (error == ENOIOCTL)
1258 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1259 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1261 ival = IOCPARM_IVAL(data);
1262 data = (caddr_t)&ival;
1265 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1266 if (*(int *)data & ~0x7f)
1268 error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1269 if (error == ENOIOCTL)
1273 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1274 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1276 ival = IOCPARM_IVAL(data);
1277 data = (caddr_t)&ival;
1280 case KDSKBMODE: /* set keyboard mode */
1281 switch (*(int *)data) {
1282 case K_XLATE: /* switch to XLT ascii mode */
1283 case K_RAW: /* switch to RAW scancode mode */
1284 case K_CODE: /* switch to CODE mode */
1285 scp->kbd_mode = *(int *)data;
1286 if (scp == sc->cur_scp)
1287 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1294 case KDGKBMODE: /* get keyboard mode */
1295 *(int *)data = scp->kbd_mode;
1299 error = kbdd_ioctl(sc->kbd, cmd, data);
1300 if (error == ENOIOCTL)
1304 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1305 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1307 ival = IOCPARM_IVAL(data);
1308 data = (caddr_t)&ival;
1311 case KDMKTONE: /* sound the bell */
1313 sc_bell(scp, (*(int*)data)&0xffff,
1314 (((*(int*)data)>>16)&0xffff)*hz/1000);
1316 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1319 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1320 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1322 ival = IOCPARM_IVAL(data);
1323 data = (caddr_t)&ival;
1326 case KIOCSOUND: /* make tone (*data) hz */
1327 if (scp == sc->cur_scp) {
1329 return sc_tone(*(int *)data);
1335 case KDGKBTYPE: /* get keyboard type */
1336 error = kbdd_ioctl(sc->kbd, cmd, data);
1337 if (error == ENOIOCTL) {
1338 /* always return something? XXX */
1343 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1344 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1346 ival = IOCPARM_IVAL(data);
1347 data = (caddr_t)&ival;
1350 case KDSETLED: /* set keyboard LED status */
1351 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1353 scp->status &= ~LED_MASK;
1354 scp->status |= *(int *)data;
1355 if (scp == sc->cur_scp)
1356 update_kbd_leds(scp, scp->status);
1359 case KDGETLED: /* get keyboard LED status */
1360 if (scp == sc->cur_scp)
1361 save_kbd_state(scp);
1362 *(int *)data = scp->status & LED_MASK;
1365 case KBADDKBD: /* add/remove keyboard to/from mux */
1367 error = kbdd_ioctl(sc->kbd, cmd, data);
1368 if (error == ENOIOCTL)
1372 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1373 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1375 ival = IOCPARM_IVAL(data);
1376 data = (caddr_t)&ival;
1379 case CONS_SETKBD: /* set the new keyboard */
1384 newkbd = kbd_get_keyboard(*(int *)data);
1385 if (newkbd == NULL) {
1390 if (sc->kbd != newkbd) {
1391 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1392 (void *)&sc->keyboard, sckbdevent, sc);
1393 /* i == newkbd->kb_index */
1395 if (sc->kbd != NULL) {
1396 save_kbd_state(sc->cur_scp);
1397 kbd_release(sc->kbd, (void *)&sc->keyboard);
1399 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1401 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1402 (caddr_t)&sc->cur_scp->kbd_mode);
1403 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1406 error = EPERM; /* XXX */
1413 case CONS_RELKBD: /* release the current keyboard */
1416 if (sc->kbd != NULL) {
1417 save_kbd_state(sc->cur_scp);
1418 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1427 case CONS_GETTERM: /* get the current terminal emulator info */
1431 if (((term_info_t *)data)->ti_index == 0) {
1434 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1437 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1438 sizeof(((term_info_t *)data)->ti_name));
1439 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1440 sizeof(((term_info_t *)data)->ti_desc));
1441 ((term_info_t *)data)->ti_flags = 0;
1444 ((term_info_t *)data)->ti_name[0] = '\0';
1445 ((term_info_t *)data)->ti_desc[0] = '\0';
1446 ((term_info_t *)data)->ti_flags = 0;
1451 case CONS_SETTERM: /* set the current terminal emulator */
1453 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1454 /* FIXME: what if scp == sc_console! XXX */
1458 case GIO_SCRNMAP: /* get output translation table */
1459 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1462 case PIO_SCRNMAP: /* set output translation table */
1463 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1464 for (i=0; i<sizeof(sc->scr_map); i++) {
1465 sc->scr_rmap[sc->scr_map[i]] = i;
1469 case GIO_KEYMAP: /* get keyboard translation table */
1470 case PIO_KEYMAP: /* set keyboard translation table */
1471 case OGIO_KEYMAP: /* get keyboard translation table (compat) */
1472 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1473 case GIO_DEADKEYMAP: /* get accent key translation table */
1474 case PIO_DEADKEYMAP: /* set accent key translation table */
1475 case GETFKEY: /* get function key string */
1476 case SETFKEY: /* set function key string */
1477 error = kbdd_ioctl(sc->kbd, cmd, data);
1478 if (error == ENOIOCTL)
1482 #ifndef SC_NO_FONT_LOADING
1484 case PIO_FONT8x8: /* set 8x8 dot font */
1485 if (!ISFONTAVAIL(sc->adp->va_flags))
1487 bcopy(data, sc->font_8, 8*256);
1488 sc->fonts_loaded |= FONT_8;
1491 * Always use the font page #0. XXX
1492 * Don't load if the current font size is not 8x8.
1494 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1495 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1498 case GIO_FONT8x8: /* get 8x8 dot font */
1499 if (!ISFONTAVAIL(sc->adp->va_flags))
1501 if (sc->fonts_loaded & FONT_8) {
1502 bcopy(sc->font_8, data, 8*256);
1508 case PIO_FONT8x14: /* set 8x14 dot font */
1509 if (!ISFONTAVAIL(sc->adp->va_flags))
1511 bcopy(data, sc->font_14, 14*256);
1512 sc->fonts_loaded |= FONT_14;
1515 * Always use the font page #0. XXX
1516 * Don't load if the current font size is not 8x14.
1518 if (ISTEXTSC(sc->cur_scp)
1519 && (sc->cur_scp->font_size >= 14)
1520 && (sc->cur_scp->font_size < 16))
1521 sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1524 case GIO_FONT8x14: /* get 8x14 dot font */
1525 if (!ISFONTAVAIL(sc->adp->va_flags))
1527 if (sc->fonts_loaded & FONT_14) {
1528 bcopy(sc->font_14, data, 14*256);
1534 case PIO_FONT8x16: /* set 8x16 dot font */
1535 if (!ISFONTAVAIL(sc->adp->va_flags))
1537 bcopy(data, sc->font_16, 16*256);
1538 sc->fonts_loaded |= FONT_16;
1541 * Always use the font page #0. XXX
1542 * Don't load if the current font size is not 8x16.
1544 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1545 sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1548 case GIO_FONT8x16: /* get 8x16 dot font */
1549 if (!ISFONTAVAIL(sc->adp->va_flags))
1551 if (sc->fonts_loaded & FONT_16) {
1552 bcopy(sc->font_16, data, 16*256);
1558 #endif /* SC_NO_FONT_LOADING */
1568 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1572 return sctty_ioctl(dev->si_drv1, cmd, data, td);
1576 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td)
1578 #ifndef SC_NO_SYSMOUSE
1580 memset(&info, 0, sizeof(info));
1581 info.operation = MOUSE_ACTION;
1584 * Make sure all buttons are released when moused and other
1585 * console daemons exit, so that no buttons are left pressed.
1587 (void) sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td);
1593 sc_cnprobe(struct consdev *cp)
1598 if (!vty_enabled(VTY_SC)) {
1599 cp->cn_pri = CN_DEAD;
1603 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1605 /* a video card is always required */
1606 if (!scvidprobe(unit, flags, TRUE))
1607 cp->cn_pri = CN_DEAD;
1609 /* syscons will become console even when there is no keyboard */
1610 sckbdprobe(unit, flags, TRUE);
1612 if (cp->cn_pri == CN_DEAD)
1615 /* initialize required fields */
1616 strcpy(cp->cn_name, "ttyv0");
1620 sc_cninit(struct consdev *cp)
1625 sc_get_cons_priority(&unit, &flags);
1626 scinit(unit, flags | SC_KERNEL_CONSOLE);
1627 sc_console_unit = unit;
1628 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1633 sc_cnterm(struct consdev *cp)
1635 /* we are not the kernel console any more, release everything */
1637 if (sc_console_unit < 0)
1638 return; /* shouldn't happen */
1641 sc_clear_screen(sc_console);
1642 sccnupdate(sc_console);
1645 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1646 sc_console_unit = -1;
1651 sc_cngrab(struct consdev *cp)
1656 sc_console->sc->cur_scp->index != sc_console->index &&
1657 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
1658 sc_console->smode.mode == VT_AUTO)
1659 sc_switch_scr(sc_console->sc, sc_console->index);
1661 scp = sc_console->sc->cur_scp;
1663 if (scp->sc->kbd == NULL)
1666 if (scp->grabbed++ > 0)
1670 * Make sure the keyboard is accessible even when the kbd device
1671 * driver is disabled.
1673 kbdd_enable(scp->sc->kbd);
1675 /* we shall always use the keyboard in the XLATE mode here */
1676 scp->kbd_prev_mode = scp->kbd_mode;
1677 scp->kbd_mode = K_XLATE;
1678 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1680 kbdd_poll(scp->sc->kbd, TRUE);
1684 sc_cnungrab(struct consdev *cp)
1688 scp = sc_console->sc->cur_scp; /* XXX */
1689 if (scp->sc->kbd == NULL)
1692 if (--scp->grabbed > 0)
1695 kbdd_poll(scp->sc->kbd, FALSE);
1697 scp->kbd_mode = scp->kbd_prev_mode;
1698 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1699 kbdd_disable(scp->sc->kbd);
1703 sc_cnputc(struct consdev *cd, int c)
1706 scr_stat *scp = sc_console;
1707 #ifndef SC_NO_HISTORY
1711 #endif /* !SC_NO_HISTORY */
1714 /* assert(sc_console != NULL) */
1716 #ifndef SC_NO_HISTORY
1717 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1718 scp->status &= ~SLKED;
1719 update_kbd_state(scp, scp->status, SLKED);
1720 if (scp->status & BUFFER_SAVED) {
1721 if (!sc_hist_restore(scp))
1722 sc_remove_cutmarking(scp);
1723 scp->status &= ~BUFFER_SAVED;
1724 scp->status |= CURSOR_ENABLED;
1725 sc_draw_cursor_image(scp);
1729 * XXX: Now that TTY's have their own locks, we cannot process
1730 * any data after disabling scroll lock. cnputs already holds a
1733 tp = SC_DEV(scp->sc, scp->index);
1734 /* XXX "tp" can be NULL */
1737 sctty_outwakeup(tp);
1741 #endif /* !SC_NO_HISTORY */
1744 sc_puts(scp, buf, 1, 1);
1746 s = spltty(); /* block sckbdevent and scrn_timer */
1752 sc_cngetc(struct consdev *cd)
1754 static struct fkeytab fkey;
1758 int s = spltty(); /* block sckbdevent and scrn_timer while we poll */
1761 /* assert(sc_console != NULL) */
1764 * Stop the screen saver and update the screen if necessary.
1765 * What if we have been running in the screen saver code... XXX
1767 sc_touch_scrn_saver();
1768 scp = sc_console->sc->cur_scp; /* XXX */
1771 if (fkeycp < fkey.len) {
1773 return fkey.str[fkeycp++];
1776 if (scp->sc->kbd == NULL) {
1781 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK);
1783 switch (KEYFLAGS(c)) {
1784 case 0: /* normal char */
1786 case FKEY: /* function key */
1787 p = (*scp->tsw->te_fkeystr)(scp, c);
1789 fkey.len = strlen(p);
1790 bcopy(p, fkey.str, fkey.len);
1794 p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1796 if ((p != NULL) && (fkey.len > 0)) {
1797 bcopy(p, fkey.str, fkey.len);
1811 sccnupdate(scr_stat *scp)
1813 /* this is a cut-down version of scrn_timer()... */
1815 if (suspend_in_progress || scp->sc->font_loading_in_progress)
1818 if (debugger > 0 || panicstr || shutdown_in_progress) {
1819 sc_touch_scrn_saver();
1820 } else if (scp != scp->sc->cur_scp) {
1824 if (!run_scrn_saver)
1825 scp->sc->flags &= ~SC_SCRN_IDLE;
1827 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1828 if (scp->sc->flags & SC_SCRN_BLANKED)
1829 stop_scrn_saver(scp->sc, current_saver);
1832 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1833 || scp->sc->switch_in_progress)
1836 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1837 * when write_in_progress is non-zero. XXX
1840 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1841 scrn_update(scp, TRUE);
1845 scrn_timer(void *arg)
1848 static time_t kbd_time_stamp = 0;
1854 again = (arg != NULL);
1856 sc = (sc_softc_t *)arg;
1857 else if (sc_console != NULL)
1858 sc = sc_console->sc;
1862 /* find the vty to update */
1865 /* don't do anything when we are performing some I/O operations */
1866 if (suspend_in_progress || sc->font_loading_in_progress)
1870 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1871 /* try to allocate a keyboard automatically */
1872 if (kbd_time_stamp != time_uptime) {
1873 kbd_time_stamp = time_uptime;
1874 sc->keyboard = sc_allocate_keyboard(sc, -1);
1875 if (sc->keyboard >= 0) {
1876 sc->kbd = kbd_get_keyboard(sc->keyboard);
1877 (void)kbdd_ioctl(sc->kbd, KDSKBMODE,
1878 (caddr_t)&sc->cur_scp->kbd_mode);
1879 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1886 /* should we stop the screen saver? */
1887 if (debugger > 0 || panicstr || shutdown_in_progress)
1888 sc_touch_scrn_saver();
1889 if (run_scrn_saver) {
1890 if (time_uptime > sc->scrn_time_stamp + scrn_blank_time)
1891 sc->flags |= SC_SCRN_IDLE;
1893 sc->flags &= ~SC_SCRN_IDLE;
1895 sc->scrn_time_stamp = time_uptime;
1896 sc->flags &= ~SC_SCRN_IDLE;
1897 if (scrn_blank_time > 0)
1898 run_scrn_saver = TRUE;
1901 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1902 if (sc->flags & SC_SCRN_BLANKED)
1903 stop_scrn_saver(sc, current_saver);
1906 /* should we just return ? */
1907 if (sc->blink_in_progress || sc->switch_in_progress
1908 || sc->write_in_progress)
1911 /* Update the screen */
1912 scp = sc->cur_scp; /* cur_scp may have changed... */
1913 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1914 scrn_update(scp, TRUE);
1917 /* should we activate the screen saver? */
1918 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1919 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1920 (*current_saver)(sc, TRUE);
1926 * Use reduced "refresh" rate if we are in graphics and that is not a
1927 * graphical screen saver. In such case we just have nothing to do.
1929 if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1933 callout_reset_sbt(&sc->ctimeout, SBT_1S / rate, 0,
1934 scrn_timer, sc, C_PREL(1));
1939 and_region(int *s1, int *e1, int s2, int e2)
1941 if (*e1 < s2 || e2 < *s1)
1943 *s1 = imax(*s1, s2);
1944 *e1 = imin(*e1, e2);
1949 scrn_update(scr_stat *scp, int show_cursor)
1956 /* assert(scp == scp->sc->cur_scp) */
1958 SC_VIDEO_LOCK(scp->sc);
1960 #ifndef SC_NO_CUTPASTE
1961 /* remove the previous mouse pointer image if necessary */
1962 if (scp->status & MOUSE_VISIBLE) {
1964 e = scp->mouse_pos + scp->xsize + 1;
1965 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1966 || and_region(&s, &e, scp->start, scp->end)
1967 || ((scp->status & CURSOR_ENABLED) &&
1968 (scp->cursor_pos != scp->cursor_oldpos) &&
1969 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1970 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1971 sc_remove_mouse_image(scp);
1972 if (scp->end >= scp->xsize*scp->ysize)
1973 scp->end = scp->xsize*scp->ysize - 1;
1976 #endif /* !SC_NO_CUTPASTE */
1980 if (scp->end >= scp->xsize*scp->ysize) {
1981 printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1982 scp->end = scp->xsize*scp->ysize - 1;
1984 if (scp->start < 0) {
1985 printf("scrn_update(): scp->start %d < 0\n", scp->start);
1990 /* update screen image */
1991 if (scp->start <= scp->end) {
1992 if (scp->mouse_cut_end >= 0) {
1993 /* there is a marked region for cut & paste */
1994 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1995 start = scp->mouse_cut_start;
1996 end = scp->mouse_cut_end;
1998 start = scp->mouse_cut_end;
1999 end = scp->mouse_cut_start - 1;
2003 /* does the cut-mark region overlap with the update region? */
2004 if (and_region(&s, &e, scp->start, scp->end)) {
2005 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2008 if (and_region(&s, &e, scp->start, scp->end))
2009 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2011 e = scp->xsize*scp->ysize - 1;
2012 if (and_region(&s, &e, scp->start, scp->end))
2013 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
2015 (*scp->rndr->draw)(scp, scp->start,
2016 scp->end - scp->start + 1, FALSE);
2019 (*scp->rndr->draw)(scp, scp->start,
2020 scp->end - scp->start + 1, FALSE);
2024 /* we are not to show the cursor and the mouse pointer... */
2027 scp->start = scp->xsize*scp->ysize - 1;
2028 SC_VIDEO_UNLOCK(scp->sc);
2032 /* update cursor image */
2033 if (scp->status & CURSOR_ENABLED) {
2036 /* did cursor move since last time ? */
2037 if (scp->cursor_pos != scp->cursor_oldpos) {
2038 /* do we need to remove old cursor image ? */
2039 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
2040 sc_remove_cursor_image(scp);
2041 sc_draw_cursor_image(scp);
2043 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
2044 /* cursor didn't move, but has been overwritten */
2045 sc_draw_cursor_image(scp);
2046 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
2047 /* if it's a blinking cursor, update it */
2048 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
2049 sc_inside_cutmark(scp,
2054 #ifndef SC_NO_CUTPASTE
2055 /* update "pseudo" mouse pointer image */
2056 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2057 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2058 scp->status &= ~MOUSE_MOVED;
2059 sc_draw_mouse_image(scp);
2062 #endif /* SC_NO_CUTPASTE */
2065 scp->start = scp->xsize*scp->ysize - 1;
2067 SC_VIDEO_UNLOCK(scp->sc);
2072 scsplash_callback(int event, void *arg)
2077 sc = (sc_softc_t *)arg;
2081 if (add_scrn_saver(scsplash_saver) == 0) {
2082 sc->flags &= ~SC_SAVER_FAILED;
2083 run_scrn_saver = TRUE;
2084 if (cold && !(boothowto & RB_VERBOSE)) {
2085 scsplash_stick(TRUE);
2086 (*current_saver)(sc, TRUE);
2092 if (current_saver == scsplash_saver) {
2093 scsplash_stick(FALSE);
2094 error = remove_scrn_saver(scsplash_saver);
2106 scsplash_saver(sc_softc_t *sc, int show)
2108 static int busy = FALSE;
2117 if (!(sc->flags & SC_SAVER_FAILED)) {
2118 if (!(sc->flags & SC_SCRN_BLANKED))
2119 set_scrn_saver_mode(scp, -1, NULL, 0);
2120 switch (splash(sc->adp, TRUE)) {
2121 case 0: /* succeeded */
2123 case EAGAIN: /* try later */
2124 restore_scrn_saver_mode(scp, FALSE);
2125 sc_touch_scrn_saver(); /* XXX */
2128 sc->flags |= SC_SAVER_FAILED;
2129 scsplash_stick(FALSE);
2130 restore_scrn_saver_mode(scp, TRUE);
2131 printf("scsplash_saver(): failed to put up the image\n");
2135 } else if (!sticky_splash) {
2136 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2137 restore_scrn_saver_mode(scp, TRUE);
2143 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2148 if (current_saver != none_saver) {
2149 error = remove_scrn_saver(current_saver);
2154 if (current_saver != none_saver)
2157 run_scrn_saver = FALSE;
2158 saver_mode = CONS_LKM_SAVER;
2159 current_saver = this_saver;
2164 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2166 if (current_saver != this_saver)
2171 * In order to prevent `current_saver' from being called by
2172 * the timeout routine `scrn_timer()' while we manipulate
2173 * the saver list, we shall set `current_saver' to `none_saver'
2174 * before stopping the current saver, rather than blocking by `splXX()'.
2176 current_saver = none_saver;
2178 stop_scrn_saver(this_saver);
2181 /* unblank all blanked screens */
2182 wait_scrn_saver_stop(NULL);
2186 current_saver = none_saver;
2191 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2195 /* assert(scp == scp->sc->cur_scp) */
2197 if (!ISGRAPHSC(scp))
2198 sc_remove_cursor_image(scp);
2199 scp->splash_save_mode = scp->mode;
2200 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2201 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2202 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2203 scp->sc->flags |= SC_SCRN_BLANKED;
2209 if (set_mode(scp) == 0) {
2210 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2211 scp->status |= GRAPHICS_MODE;
2212 #ifndef SC_NO_PALETTE_LOADING
2214 vidd_load_palette(scp->sc->adp, pal);
2216 sc_set_border(scp, border);
2220 scp->mode = scp->splash_save_mode;
2221 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2222 scp->status |= scp->splash_save_status;
2229 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2235 /* assert(scp == scp->sc->cur_scp) */
2238 status = scp->status;
2239 scp->mode = scp->splash_save_mode;
2240 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2241 scp->status |= scp->splash_save_status;
2242 scp->sc->flags &= ~SC_SCRN_BLANKED;
2244 if (!ISGRAPHSC(scp))
2245 sc_draw_cursor_image(scp);
2250 if (set_mode(scp) == 0) {
2251 #ifndef SC_NO_PALETTE_LOADING
2252 #ifdef SC_PIXEL_MODE
2253 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2254 vidd_load_palette(scp->sc->adp, scp->sc->palette2);
2257 vidd_load_palette(scp->sc->adp, scp->sc->palette);
2264 scp->status = status;
2271 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2273 (*saver)(sc, FALSE);
2274 run_scrn_saver = FALSE;
2275 /* the screen saver may have chosen not to stop after all... */
2276 if (sc->flags & SC_SCRN_BLANKED)
2279 mark_all(sc->cur_scp);
2280 if (sc->delayed_next_scr)
2281 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2283 wakeup(&scrn_blanked);
2287 wait_scrn_saver_stop(sc_softc_t *sc)
2291 while (scrn_blanked > 0) {
2292 run_scrn_saver = FALSE;
2293 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2297 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2298 if ((error != 0) && (error != ERESTART))
2301 run_scrn_saver = FALSE;
2304 #endif /* DEV_SPLASH */
2307 sc_touch_scrn_saver(void)
2309 scsplash_stick(FALSE);
2310 run_scrn_saver = FALSE;
2314 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2321 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2323 if (sc->cur_scp == NULL)
2326 /* prevent switch if previously requested */
2327 if (sc->flags & SC_SCRN_VTYLOCK) {
2328 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2329 sc->cur_scp->bell_duration);
2333 /* delay switch if the screen is blanked or being updated */
2334 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2335 || sc->blink_in_progress) {
2336 sc->delayed_next_scr = next_scr + 1;
2337 sc_touch_scrn_saver();
2338 DPRINTF(5, ("switch delayed\n"));
2341 sc->delayed_next_scr = 0;
2344 cur_scp = sc->cur_scp;
2346 /* we are in the middle of the vty switching process... */
2347 if (sc->switch_in_progress
2348 && (cur_scp->smode.mode == VT_PROCESS)
2350 p = pfind(cur_scp->pid);
2351 if (cur_scp->proc != p) {
2355 * The controlling process has died!!. Do some clean up.
2356 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2357 * are not reset here yet; they will be cleared later.
2359 DPRINTF(5, ("cur_scp controlling process %d died, ",
2361 if (cur_scp->status & SWITCH_WAIT_REL) {
2363 * Force the previous switch to finish, but return now
2366 DPRINTF(5, ("reset WAIT_REL, "));
2367 finish_vt_rel(cur_scp, TRUE, &s);
2369 DPRINTF(5, ("finishing previous switch\n"));
2371 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2372 /* let's assume screen switch has been completed. */
2373 DPRINTF(5, ("reset WAIT_ACQ, "));
2374 finish_vt_acq(cur_scp);
2377 * We are in between screen release and acquisition, and
2378 * reached here via scgetc() or scrn_timer() which has
2379 * interrupted exchange_scr(). Don't do anything stupid.
2381 DPRINTF(5, ("waiting nothing, "));
2387 * The controlling process is alive, but not responding...
2388 * It is either buggy or it may be just taking time.
2389 * The following code is a gross kludge to cope with this
2390 * problem for which there is no clean solution. XXX
2392 if (cur_scp->status & SWITCH_WAIT_REL) {
2393 switch (sc->switch_in_progress++) {
2397 DPRINTF(5, ("sending relsig again, "));
2398 signal_vt_rel(cur_scp);
2405 * Act as if the controlling program returned
2408 DPRINTF(5, ("force reset WAIT_REL, "));
2409 finish_vt_rel(cur_scp, FALSE, &s);
2411 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2414 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2415 switch (sc->switch_in_progress++) {
2419 DPRINTF(5, ("sending acqsig again, "));
2420 signal_vt_acq(cur_scp);
2426 /* clear the flag and finish the previous switch */
2427 DPRINTF(5, ("force reset WAIT_ACQ, "));
2428 finish_vt_acq(cur_scp);
2436 * Return error if an invalid argument is given, or vty switch
2437 * is still in progress.
2439 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2440 || sc->switch_in_progress) {
2442 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2443 DPRINTF(5, ("error 1\n"));
2448 * Don't allow switching away from the graphics mode vty
2449 * if the switch mode is VT_AUTO, unless the next vty is the same
2450 * as the current or the current vty has been closed (but showing).
2452 tp = SC_DEV(sc, cur_scp->index);
2453 if ((cur_scp->index != next_scr)
2454 && tty_opened_ns(tp)
2455 && (cur_scp->smode.mode == VT_AUTO)
2456 && ISGRAPHSC(cur_scp)) {
2458 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2459 DPRINTF(5, ("error, graphics mode\n"));
2464 * Is the wanted vty open? Don't allow switching to a closed vty.
2465 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2466 * Note that we always allow the user to switch to the kernel
2467 * console even if it is closed.
2469 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2470 tp = SC_DEV(sc, next_scr);
2471 if (!tty_opened_ns(tp)) {
2473 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2474 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2477 if ((debugger > 0) && (SC_STAT(tp)->smode.mode == VT_PROCESS)) {
2479 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2484 /* this is the start of vty switching process... */
2485 ++sc->switch_in_progress;
2486 sc->old_scp = cur_scp;
2487 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2488 if (sc->new_scp == sc->old_scp) {
2489 sc->switch_in_progress = 0;
2491 * XXX wakeup() locks the scheduler lock which will hang if
2492 * the lock is in an in-between state, e.g., when we stop at
2493 * a breakpoint at fork_exit. It has always been wrong to call
2494 * wakeup() when the debugger is active. In RELENG_4, wakeup()
2495 * is supposed to be locked by splhigh(), but the debugger may
2496 * be invoked at splhigh().
2499 wakeup(VTY_WCHAN(sc,next_scr));
2501 DPRINTF(5, ("switch done (new == old)\n"));
2505 /* has controlling process died? */
2506 vt_proc_alive(sc->old_scp);
2507 vt_proc_alive(sc->new_scp);
2509 /* wait for the controlling process to release the screen, if necessary */
2510 if (signal_vt_rel(sc->old_scp)) {
2515 /* go set up the new vty screen */
2520 /* wake up processes waiting for this vty */
2522 wakeup(VTY_WCHAN(sc,next_scr));
2524 /* wait for the controlling process to acknowledge, if necessary */
2525 if (signal_vt_acq(sc->cur_scp)) {
2530 sc->switch_in_progress = 0;
2531 if (sc->unit == sc_console_unit)
2532 cnavailable(sc_consptr, TRUE);
2534 DPRINTF(5, ("switch done\n"));
2540 do_switch_scr(sc_softc_t *sc, int s)
2542 vt_proc_alive(sc->new_scp);
2547 /* sc->cur_scp == sc->new_scp */
2548 wakeup(VTY_WCHAN(sc,sc->cur_scp->index));
2550 /* wait for the controlling process to acknowledge, if necessary */
2551 if (!signal_vt_acq(sc->cur_scp)) {
2552 sc->switch_in_progress = 0;
2553 if (sc->unit == sc_console_unit)
2554 cnavailable(sc_consptr, TRUE);
2561 vt_proc_alive(scr_stat *scp)
2566 if ((p = pfind(scp->pid)) != NULL)
2571 scp->smode.mode = VT_AUTO;
2572 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2578 signal_vt_rel(scr_stat *scp)
2580 if (scp->smode.mode != VT_PROCESS)
2582 scp->status |= SWITCH_WAIT_REL;
2583 PROC_LOCK(scp->proc);
2584 kern_psignal(scp->proc, scp->smode.relsig);
2585 PROC_UNLOCK(scp->proc);
2586 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2591 signal_vt_acq(scr_stat *scp)
2593 if (scp->smode.mode != VT_PROCESS)
2595 if (scp->sc->unit == sc_console_unit)
2596 cnavailable(sc_consptr, FALSE);
2597 scp->status |= SWITCH_WAIT_ACQ;
2598 PROC_LOCK(scp->proc);
2599 kern_psignal(scp->proc, scp->smode.acqsig);
2600 PROC_UNLOCK(scp->proc);
2601 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2606 finish_vt_rel(scr_stat *scp, int release, int *s)
2608 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2609 scp->status &= ~SWITCH_WAIT_REL;
2611 *s = do_switch_scr(scp->sc, *s);
2613 scp->sc->switch_in_progress = 0;
2620 finish_vt_acq(scr_stat *scp)
2622 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2623 scp->status &= ~SWITCH_WAIT_ACQ;
2624 scp->sc->switch_in_progress = 0;
2631 exchange_scr(sc_softc_t *sc)
2635 /* save the current state of video and keyboard */
2636 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2637 if (!ISGRAPHSC(sc->old_scp))
2638 sc_remove_cursor_image(sc->old_scp);
2639 if (sc->old_scp->kbd_mode == K_XLATE)
2640 save_kbd_state(sc->old_scp);
2642 /* set up the video for the new screen */
2643 scp = sc->cur_scp = sc->new_scp;
2645 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp))
2647 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2652 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2653 (void *)sc->adp->va_window, FALSE);
2655 scp->status |= MOUSE_HIDDEN;
2656 sc_move_cursor(scp, scp->xpos, scp->ypos);
2657 if (!ISGRAPHSC(scp))
2658 sc_set_cursor_image(scp);
2659 #ifndef SC_NO_PALETTE_LOADING
2660 if (ISGRAPHSC(sc->old_scp)) {
2661 #ifdef SC_PIXEL_MODE
2662 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
2663 vidd_load_palette(sc->adp, sc->palette2);
2666 vidd_load_palette(sc->adp, sc->palette);
2669 sc_set_border(scp, scp->border);
2671 /* set up the keyboard for the new screen */
2672 if (sc->old_scp->kbd_mode != scp->kbd_mode)
2673 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2674 update_kbd_state(scp, scp->status, LOCK_MASK);
2680 sc_puts(scr_stat *scp, u_char *buf, int len, int kernel)
2682 int need_unlock = 0;
2685 /* make screensaver happy */
2686 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2687 run_scrn_saver = FALSE;
2691 if (!kdb_active && !mtx_owned(&scp->scr_lock)) {
2693 mtx_lock_spin(&scp->scr_lock);
2695 (*scp->tsw->te_puts)(scp, buf, len, kernel);
2697 mtx_unlock_spin(&scp->scr_lock);
2700 if (scp->sc->delayed_next_scr)
2701 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2705 sc_draw_cursor_image(scr_stat *scp)
2707 /* assert(scp == scp->sc->cur_scp); */
2708 SC_VIDEO_LOCK(scp->sc);
2709 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2710 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2711 sc_inside_cutmark(scp, scp->cursor_pos));
2712 scp->cursor_oldpos = scp->cursor_pos;
2713 SC_VIDEO_UNLOCK(scp->sc);
2717 sc_remove_cursor_image(scr_stat *scp)
2719 /* assert(scp == scp->sc->cur_scp); */
2720 SC_VIDEO_LOCK(scp->sc);
2721 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2722 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2723 sc_inside_cutmark(scp, scp->cursor_oldpos));
2724 SC_VIDEO_UNLOCK(scp->sc);
2728 update_cursor_image(scr_stat *scp)
2730 /* assert(scp == scp->sc->cur_scp); */
2731 sc_remove_cursor_image(scp);
2732 sc_set_cursor_image(scp);
2733 sc_draw_cursor_image(scp);
2737 sc_set_cursor_image(scr_stat *scp)
2739 scp->curs_attr.flags = scp->curr_curs_attr.flags;
2740 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2741 /* hidden cursor is internally represented as zero-height underline */
2742 scp->curs_attr.flags = CONS_CHAR_CURSOR;
2743 scp->curs_attr.base = scp->curs_attr.height = 0;
2744 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2745 scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2746 scp->font_size - 1);
2747 scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2748 scp->font_size - scp->curs_attr.base);
2749 } else { /* block cursor */
2750 scp->curs_attr.base = 0;
2751 scp->curs_attr.height = scp->font_size;
2754 /* assert(scp == scp->sc->cur_scp); */
2755 SC_VIDEO_LOCK(scp->sc);
2756 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2757 scp->curs_attr.flags & CONS_BLINK_CURSOR);
2758 SC_VIDEO_UNLOCK(scp->sc);
2762 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2764 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2765 sc_remove_cursor_image(scp);
2768 scp->curr_curs_attr.base = base;
2770 scp->curr_curs_attr.height = height;
2771 if (flags & CONS_RESET_CURSOR)
2772 scp->curr_curs_attr = scp->dflt_curs_attr;
2774 scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2776 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2777 sc_set_cursor_image(scp);
2778 sc_draw_cursor_image(scp);
2783 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2791 if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2792 /* local (per vty) change */
2793 change_cursor_shape(scp, flags, base, height);
2801 sc->curs_attr.base = base;
2803 sc->curs_attr.height = height;
2805 if (flags & CONS_RESET_CURSOR)
2806 sc->curs_attr = sc->dflt_curs_attr;
2808 sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2811 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2812 if ((tp = SC_DEV(sc, i)) == NULL)
2814 if ((scp = sc_get_stat(tp)) == NULL)
2816 scp->dflt_curs_attr = sc->curs_attr;
2817 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2823 scinit(int unit, int flags)
2827 * When syscons is being initialized as the kernel console, malloc()
2828 * is not yet functional, because various kernel structures has not been
2829 * fully initialized yet. Therefore, we need to declare the following
2830 * static buffers for the console. This is less than ideal,
2831 * but is necessry evil for the time being. XXX
2834 static u_short sc_buffer[ROW*COL*2];/* XXX */
2836 static u_short sc_buffer[ROW*COL]; /* XXX */
2838 #ifndef SC_NO_FONT_LOADING
2839 static u_char font_8[256*8];
2840 static u_char font_14[256*14];
2841 static u_char font_16[256*16];
2846 video_adapter_t *adp;
2851 /* one time initialization */
2852 if (init_done == COLD)
2853 sc_get_bios_values(&bios_value);
2857 * Allocate resources. Even if we are being called for the second
2858 * time, we must allocate them again, because they might have
2861 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2862 if ((sc->flags & SC_INIT_DONE) == 0)
2863 SC_VIDEO_LOCKINIT(sc);
2866 if (sc->adapter >= 0) {
2867 vid_release(sc->adp, (void *)&sc->adapter);
2871 if (sc->keyboard >= 0) {
2872 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2873 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2874 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2875 if (sc->kbd != NULL) {
2876 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2877 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2881 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2882 sc->adp = vid_get_adapter(sc->adapter);
2883 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2885 sc->keyboard = sc_allocate_keyboard(sc, unit);
2886 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2888 sc->kbd = kbd_get_keyboard(sc->keyboard);
2889 if (sc->kbd != NULL) {
2890 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2891 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2894 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2896 sc->initial_mode = sc->adp->va_initial_mode;
2898 #ifndef SC_NO_FONT_LOADING
2899 if (flags & SC_KERNEL_CONSOLE) {
2900 sc->font_8 = font_8;
2901 sc->font_14 = font_14;
2902 sc->font_16 = font_16;
2903 } else if (sc->font_8 == NULL) {
2904 /* assert(sc_malloc) */
2905 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2906 sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2907 sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2911 /* extract the hardware cursor location and hide the cursor for now */
2912 vidd_read_hw_cursor(sc->adp, &col, &row);
2913 vidd_set_hw_cursor(sc->adp, -1, -1);
2915 /* set up the first console */
2916 sc->first_vty = unit*MAXCONS;
2917 sc->vtys = MAXCONS; /* XXX: should be configurable */
2918 if (flags & SC_KERNEL_CONSOLE) {
2920 * Set up devs structure but don't use it yet, calling make_dev()
2921 * might panic kernel. Wait for sc_attach_unit() to actually
2922 * create the devices.
2924 sc->dev = main_devs;
2925 scp = &main_console;
2926 init_scp(sc, sc->first_vty, scp);
2927 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2928 (void *)sc_buffer, FALSE);
2930 /* move cursors to the initial positions */
2931 if (col >= scp->xsize)
2933 if (row >= scp->ysize)
2934 row = scp->ysize - 1;
2937 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2939 if (sc_init_emulator(scp, SC_DFLT_TERM))
2940 sc_init_emulator(scp, "*");
2941 (*scp->tsw->te_default_attr)(scp,
2942 user_default.std_color,
2943 user_default.rev_color);
2945 /* assert(sc_malloc) */
2946 sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF,
2948 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS);
2949 scp = alloc_scp(sc, sc->first_vty);
2950 SC_STAT(sc->dev[0]) = scp;
2955 /* copy screen to temporary buffer */
2956 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2957 (void *)scp->sc->adp->va_window, FALSE);
2959 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2962 if (bios_value.cursor_end < scp->font_size)
2963 sc->dflt_curs_attr.base = scp->font_size -
2964 bios_value.cursor_end - 1;
2966 sc->dflt_curs_attr.base = 0;
2967 i = bios_value.cursor_end - bios_value.cursor_start + 1;
2968 sc->dflt_curs_attr.height = imin(i, scp->font_size);
2969 sc->dflt_curs_attr.flags = 0;
2970 sc->curs_attr = sc->dflt_curs_attr;
2971 scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2973 #ifndef SC_NO_SYSMOUSE
2974 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2976 if (!ISGRAPHSC(scp)) {
2977 sc_set_cursor_image(scp);
2978 sc_draw_cursor_image(scp);
2981 /* save font and palette */
2982 #ifndef SC_NO_FONT_LOADING
2983 sc->fonts_loaded = 0;
2984 if (ISFONTAVAIL(sc->adp->va_flags)) {
2986 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2987 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2988 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2989 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2990 if (scp->font_size < 14) {
2991 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2992 } else if (scp->font_size >= 16) {
2993 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2995 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2997 #else /* !SC_DFLT_FONT */
2998 if (scp->font_size < 14) {
2999 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
3000 sc->fonts_loaded = FONT_8;
3001 } else if (scp->font_size >= 16) {
3002 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
3003 sc->fonts_loaded = FONT_16;
3005 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
3006 sc->fonts_loaded = FONT_14;
3008 #endif /* SC_DFLT_FONT */
3009 /* FONT KLUDGE: always use the font page #0. XXX */
3010 sc_show_font(scp, 0);
3012 #endif /* !SC_NO_FONT_LOADING */
3014 #ifndef SC_NO_PALETTE_LOADING
3015 vidd_save_palette(sc->adp, sc->palette);
3016 #ifdef SC_PIXEL_MODE
3017 for (i = 0; i < sizeof(sc->palette2); i++)
3018 sc->palette2[i] = i / 3;
3023 if (!(sc->flags & SC_SPLASH_SCRN)) {
3024 /* we are ready to put up the splash image! */
3025 splash_init(sc->adp, scsplash_callback, sc);
3026 sc->flags |= SC_SPLASH_SCRN;
3031 /* the rest is not necessary, if we have done it once */
3032 if (sc->flags & SC_INIT_DONE)
3035 /* initialize mapscrn arrays to a one to one map */
3036 for (i = 0; i < sizeof(sc->scr_map); i++)
3037 sc->scr_map[i] = sc->scr_rmap[i] = i;
3039 sc->scr_map[0x5c] = (u_char)0xfc; /* for backslash */
3042 sc->flags |= SC_INIT_DONE;
3046 scterm(int unit, int flags)
3051 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3053 return; /* shouldn't happen */
3056 /* this console is no longer available for the splash screen */
3057 if (sc->flags & SC_SPLASH_SCRN) {
3058 splash_term(sc->adp);
3059 sc->flags &= ~SC_SPLASH_SCRN;
3064 /* move the hardware cursor to the upper-left corner */
3065 vidd_set_hw_cursor(sc->adp, 0, 0);
3068 /* release the keyboard and the video card */
3069 if (sc->keyboard >= 0)
3070 kbd_release(sc->kbd, &sc->keyboard);
3071 if (sc->adapter >= 0)
3072 vid_release(sc->adp, &sc->adapter);
3074 /* stop the terminal emulator, if any */
3075 scp = sc_get_stat(sc->dev[0]);
3077 (*scp->tsw->te_term)(scp, &scp->ts);
3078 if (scp->ts != NULL)
3079 free(scp->ts, M_DEVBUF);
3080 mtx_destroy(&scp->scr_lock);
3082 /* clear the structure */
3083 if (!(flags & SC_KERNEL_CONSOLE)) {
3084 /* XXX: We need delete_dev() for this */
3085 free(sc->dev, M_DEVBUF);
3087 /* XXX: We need a ttyunregister for this */
3088 free(sc->tty, M_DEVBUF);
3090 #ifndef SC_NO_FONT_LOADING
3091 free(sc->font_8, M_DEVBUF);
3092 free(sc->font_14, M_DEVBUF);
3093 free(sc->font_16, M_DEVBUF);
3095 /* XXX vtb, history */
3097 bzero(sc, sizeof(*sc));
3103 scshutdown(__unused void *arg, __unused int howto)
3106 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3107 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3108 KASSERT(sc_console->sc->cur_scp != NULL,
3109 ("sc_console->sc->cur_scp != NULL"));
3111 sc_touch_scrn_saver();
3113 sc_console->sc->cur_scp->index != sc_console->index &&
3114 sc_console->sc->cur_scp->smode.mode == VT_AUTO &&
3115 sc_console->smode.mode == VT_AUTO)
3116 sc_switch_scr(sc_console->sc, sc_console->index);
3117 shutdown_in_progress = TRUE;
3121 scsuspend(__unused void *arg)
3125 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3126 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3127 KASSERT(sc_console->sc->cur_scp != NULL,
3128 ("sc_console->sc->cur_scp != NULL"));
3130 sc_susp_scr = sc_console->sc->cur_scp->index;
3131 if (sc_no_suspend_vtswitch ||
3132 sc_susp_scr == sc_console->index) {
3133 sc_touch_scrn_saver();
3137 for (retry = 0; retry < 10; retry++) {
3138 sc_switch_scr(sc_console->sc, sc_console->index);
3139 if (!sc_console->sc->switch_in_progress)
3141 pause("scsuspend", hz);
3143 suspend_in_progress = TRUE;
3147 scresume(__unused void *arg)
3150 KASSERT(sc_console != NULL, ("sc_console != NULL"));
3151 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL"));
3152 KASSERT(sc_console->sc->cur_scp != NULL,
3153 ("sc_console->sc->cur_scp != NULL"));
3155 suspend_in_progress = FALSE;
3156 if (sc_susp_scr < 0) {
3157 update_font(sc_console->sc->cur_scp);
3160 sc_switch_scr(sc_console->sc, sc_susp_scr);
3164 sc_clean_up(scr_stat *scp)
3170 if (scp->sc->flags & SC_SCRN_BLANKED) {
3171 sc_touch_scrn_saver();
3173 if ((error = wait_scrn_saver_stop(scp->sc)))
3177 scp->status |= MOUSE_HIDDEN;
3178 sc_remove_mouse_image(scp);
3179 sc_remove_cutmarking(scp);
3184 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3190 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3191 if (!discard && (old.vtb_flags & VTB_VALID)) {
3192 /* retain the current cursor position and buffer contants */
3193 scp->cursor_oldpos = scp->cursor_pos;
3195 * This works only if the old buffer has the same size as or larger
3196 * than the new one. XXX
3198 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3202 sc_vtb_destroy(&old);
3205 #ifndef SC_NO_SYSMOUSE
3206 /* move the mouse cursor at the center of the screen */
3207 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3212 *alloc_scp(sc_softc_t *sc, int vty)
3216 /* assert(sc_malloc) */
3218 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3219 init_scp(sc, vty, scp);
3221 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3222 if (sc_init_emulator(scp, SC_DFLT_TERM))
3223 sc_init_emulator(scp, "*");
3225 #ifndef SC_NO_CUTPASTE
3226 sc_alloc_cut_buffer(scp, TRUE);
3229 #ifndef SC_NO_HISTORY
3230 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3237 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3241 bzero(scp, sizeof(*scp));
3246 scp->mode = sc->initial_mode;
3247 vidd_get_info(sc->adp, scp->mode, &info);
3248 if (info.vi_flags & V_INFO_GRAPHICS) {
3249 scp->status |= GRAPHICS_MODE;
3250 scp->xpixel = info.vi_width;
3251 scp->ypixel = info.vi_height;
3252 scp->xsize = info.vi_width/info.vi_cwidth;
3253 scp->ysize = info.vi_height/info.vi_cheight;
3257 scp->xsize = info.vi_width;
3258 scp->ysize = info.vi_height;
3259 scp->xpixel = scp->xsize*info.vi_cwidth;
3260 scp->ypixel = scp->ysize*info.vi_cheight;
3263 scp->font_size = info.vi_cheight;
3264 scp->font_width = info.vi_cwidth;
3265 #ifndef SC_NO_FONT_LOADING
3266 if (info.vi_cheight < 14)
3267 scp->font = sc->font_8;
3268 else if (info.vi_cheight >= 16)
3269 scp->font = sc->font_16;
3271 scp->font = sc->font_14;
3276 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3278 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3280 scp->xoff = scp->yoff = 0;
3281 scp->xpos = scp->ypos = 0;
3282 scp->start = scp->xsize * scp->ysize - 1;
3287 scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3288 scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3289 scp->mouse_cut_start = scp->xsize*scp->ysize;
3290 scp->mouse_cut_end = -1;
3291 scp->mouse_signal = 0;
3293 scp->mouse_proc = NULL;
3294 scp->kbd_mode = K_XLATE;
3295 scp->bell_pitch = bios_value.bell_pitch;
3296 scp->bell_duration = BELL_DURATION;
3297 scp->status |= (bios_value.shift_state & NLKED);
3298 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3301 scp->smode.mode = VT_AUTO;
3302 scp->history = NULL;
3303 scp->history_pos = 0;
3304 scp->history_size = 0;
3306 mtx_init(&scp->scr_lock, "scrlock", NULL, MTX_SPIN);
3310 sc_init_emulator(scr_stat *scp, char *name)
3317 if (name == NULL) /* if no name is given, use the current emulator */
3319 else /* ...otherwise find the named emulator */
3320 sw = sc_term_match(name);
3325 if (strcmp(sw->te_renderer, "*") != 0) {
3326 rndr = sc_render_match(scp, sw->te_renderer,
3327 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3330 rndr = sc_render_match(scp, scp->sc->adp->va_name,
3331 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3336 if (sw == scp->tsw) {
3337 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3339 scp->rndr->init(scp);
3340 sc_clear_screen(scp);
3341 /* assert(error == 0); */
3345 if (sc_malloc && (sw->te_size > 0))
3346 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3349 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3354 (*scp->tsw->te_term)(scp, &scp->ts);
3355 if (scp->ts != NULL)
3356 free(scp->ts, M_DEVBUF);
3360 scp->rndr->init(scp);
3363 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3364 sc_clear_screen(scp);
3370 * scgetc(flags) - get character from keyboard.
3371 * If flags & SCGETC_CN, then avoid harmful side effects.
3372 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3373 * return NOKEY if there is nothing there.
3376 scgetc(sc_softc_t *sc, u_int flags)
3379 #ifndef SC_NO_HISTORY
3387 if (sc->kbd == NULL)
3392 /* I don't like this, but... XXX */
3393 if (flags & SCGETC_CN)
3394 sccnupdate(sc->cur_scp);
3397 /* first see if there is something in the keyboard port */
3399 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3401 if (!(flags & SCGETC_CN))
3402 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3403 } else if (c == NOKEY)
3409 /* make screensaver happy */
3411 sc_touch_scrn_saver();
3413 if (!(flags & SCGETC_CN))
3414 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
3416 if (scp->kbd_mode != K_XLATE)
3419 /* if scroll-lock pressed allow history browsing */
3420 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3422 scp->status &= ~CURSOR_ENABLED;
3423 sc_remove_cursor_image(scp);
3425 #ifndef SC_NO_HISTORY
3426 if (!(scp->status & BUFFER_SAVED)) {
3427 scp->status |= BUFFER_SAVED;
3431 /* FIXME: key codes */
3432 case SPCLKEY | FKEY | F(49): /* home key */
3433 sc_remove_cutmarking(scp);
3437 case SPCLKEY | FKEY | F(57): /* end key */
3438 sc_remove_cutmarking(scp);
3442 case SPCLKEY | FKEY | F(50): /* up arrow key */
3443 sc_remove_cutmarking(scp);
3444 if (sc_hist_up_line(scp))
3445 if (!(flags & SCGETC_CN))
3446 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3449 case SPCLKEY | FKEY | F(58): /* down arrow key */
3450 sc_remove_cutmarking(scp);
3451 if (sc_hist_down_line(scp))
3452 if (!(flags & SCGETC_CN))
3453 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3456 case SPCLKEY | FKEY | F(51): /* page up key */
3457 sc_remove_cutmarking(scp);
3458 for (i=0; i<scp->ysize; i++)
3459 if (sc_hist_up_line(scp)) {
3460 if (!(flags & SCGETC_CN))
3461 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3466 case SPCLKEY | FKEY | F(59): /* page down key */
3467 sc_remove_cutmarking(scp);
3468 for (i=0; i<scp->ysize; i++)
3469 if (sc_hist_down_line(scp)) {
3470 if (!(flags & SCGETC_CN))
3471 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3476 #endif /* SC_NO_HISTORY */
3480 * Process and consume special keys here. Return a plain char code
3481 * or a char code with the META flag or a function key code.
3485 /* goto next_code */
3490 switch (KEYCHAR(c)) {
3492 case NLK: case CLK: case ALK:
3495 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3497 scp->status |= SLKED;
3499 if (scp->status & SLKED) {
3500 scp->status &= ~SLKED;
3501 #ifndef SC_NO_HISTORY
3502 if (scp->status & BUFFER_SAVED) {
3503 if (!sc_hist_restore(scp))
3504 sc_remove_cutmarking(scp);
3505 scp->status &= ~BUFFER_SAVED;
3506 scp->status |= CURSOR_ENABLED;
3507 sc_draw_cursor_image(scp);
3509 tp = SC_DEV(sc, scp->index);
3510 if (!kdb_active && tty_opened_ns(tp))
3511 sctty_outwakeup(tp);
3518 #ifndef SC_NO_CUTPASTE
3519 sc_mouse_paste(scp);
3523 /* NON-LOCKING KEYS */
3525 case LSH: case RSH: case LCTR: case RCTR:
3526 case LALT: case RALT: case ASH: case META:
3530 if (!(sc->flags & SC_SCRN_BLANKED))
3536 /* force activatation/deactivation of the screen saver */
3537 if (!(sc->flags & SC_SCRN_BLANKED)) {
3538 run_scrn_saver = TRUE;
3539 sc->scrn_time_stamp -= scrn_blank_time;
3543 * While devices are being probed, the screen saver need
3544 * to be invoked explicitly. XXX
3546 if (sc->flags & SC_SCRN_BLANKED) {
3547 scsplash_stick(FALSE);
3548 stop_scrn_saver(sc, current_saver);
3550 if (!ISGRAPHSC(scp)) {
3551 scsplash_stick(TRUE);
3552 (*current_saver)(sc, TRUE);
3556 #endif /* DEV_SPLASH */
3560 #ifndef SC_DISABLE_REBOOT
3567 #ifndef SC_DISABLE_REBOOT
3569 shutdown_nice(RB_HALT);
3574 #ifndef SC_DISABLE_REBOOT
3576 shutdown_nice(RB_HALT|RB_POWEROFF);
3581 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3584 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3588 #ifndef SC_DISABLE_KDBKEY
3595 if (enable_panic_key)
3596 panic("Forced by the panic key");
3600 this_scr = scp->index;
3601 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3602 sc->first_vty + i != this_scr;
3603 i = (i + 1)%sc->vtys) {
3604 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3605 if (tty_opened_ns(tp)) {
3606 sc_switch_scr(scp->sc, sc->first_vty + i);
3613 this_scr = scp->index;
3614 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3615 sc->first_vty + i != this_scr;
3616 i = (i + sc->vtys - 1)%sc->vtys) {
3617 struct tty *tp = SC_DEV(sc, sc->first_vty + i);
3618 if (tty_opened_ns(tp)) {
3619 sc_switch_scr(scp->sc, sc->first_vty + i);
3626 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3627 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3630 /* assert(c & FKEY) */
3631 if (!(sc->flags & SC_SCRN_BLANKED))
3635 /* goto next_code */
3637 /* regular keys (maybe MKEY is set) */
3638 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB)
3640 kdb_alt_break(c, &sc->sc_altbrk);
3642 if (!(sc->flags & SC_SCRN_BLANKED))
3651 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
3652 int nprot, vm_memattr_t *memattr)
3656 scp = sc_get_stat(tp);
3657 if (scp != scp->sc->cur_scp)
3659 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr);
3663 update_font(scr_stat *scp)
3665 #ifndef SC_NO_FONT_LOADING
3666 /* load appropriate font */
3667 if (!(scp->status & GRAPHICS_MODE)) {
3668 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3669 if (scp->font_size < 14) {
3670 if (scp->sc->fonts_loaded & FONT_8)
3671 sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3672 } else if (scp->font_size >= 16) {
3673 if (scp->sc->fonts_loaded & FONT_16)
3674 sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3676 if (scp->sc->fonts_loaded & FONT_14)
3677 sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3681 * This is an interim kludge to display correct font.
3682 * Always use the font page #0 on the video plane 2.
3683 * Somehow we cannot show the font in other font pages on
3684 * some video cards... XXX
3686 sc_show_font(scp, 0);
3690 #endif /* !SC_NO_FONT_LOADING */
3694 save_kbd_state(scr_stat *scp)
3699 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3700 if (error == ENOIOCTL)
3703 scp->status &= ~LOCK_MASK;
3704 scp->status |= state;
3710 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3715 if (mask != LOCK_MASK) {
3716 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3717 if (error == ENOIOCTL)
3722 state |= new_bits & mask;
3724 state = new_bits & LOCK_MASK;
3726 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3727 if (error == ENOIOCTL)
3733 update_kbd_leds(scr_stat *scp, int which)
3738 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3739 if (error == ENOIOCTL)
3745 set_mode(scr_stat *scp)
3749 /* reject unsupported mode */
3750 if (vidd_get_info(scp->sc->adp, scp->mode, &info))
3753 /* if this vty is not currently showing, do nothing */
3754 if (scp != scp->sc->cur_scp)
3757 /* setup video hardware for the given mode */
3758 vidd_set_mode(scp->sc->adp, scp->mode);
3759 scp->rndr->init(scp);
3761 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3762 (void *)scp->sc->adp->va_window, FALSE);
3767 sc_set_border(scp, scp->border);
3768 sc_set_cursor_image(scp);
3774 sc_set_border(scr_stat *scp, int color)
3776 SC_VIDEO_LOCK(scp->sc);
3777 (*scp->rndr->draw_border)(scp, color);
3778 SC_VIDEO_UNLOCK(scp->sc);
3781 #ifndef SC_NO_FONT_LOADING
3783 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3784 int base, int count)
3789 sc->font_loading_in_progress = TRUE;
3790 vidd_load_font(sc->adp, page, size, width, buf, base, count);
3791 sc->font_loading_in_progress = FALSE;
3795 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3796 int base, int count)
3801 sc->font_loading_in_progress = TRUE;
3802 vidd_save_font(sc->adp, page, size, width, buf, base, count);
3803 sc->font_loading_in_progress = FALSE;
3807 sc_show_font(scr_stat *scp, int page)
3809 vidd_show_font(scp->sc->adp, page);
3811 #endif /* !SC_NO_FONT_LOADING */
3814 sc_paste(scr_stat *scp, const u_char *p, int count)
3819 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3820 if (!tty_opened_ns(tp))
3822 rmap = scp->sc->scr_rmap;
3823 for (; count > 0; --count)
3824 ttydisc_rint(tp, rmap[*p++], 0);
3825 ttydisc_rint_done(tp);
3829 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup)
3833 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index);
3834 if (!tty_opened_ns(tp))
3836 ttydisc_rint_simple(tp, p, count);
3838 /* XXX: we can't always call ttydisc_rint_done() here! */
3839 ttydisc_rint_done(tp);
3844 sc_bell(scr_stat *scp, int pitch, int duration)
3846 if (cold || shutdown_in_progress || !enable_bell)
3849 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3852 if (scp->sc->flags & SC_VISUAL_BELL) {
3853 if (scp->sc->blink_in_progress)
3855 scp->sc->blink_in_progress = 3;
3856 if (scp != scp->sc->cur_scp)
3857 scp->sc->blink_in_progress += 2;
3858 blink_screen(scp->sc->cur_scp);
3859 } else if (duration != 0 && pitch != 0) {
3860 if (scp != scp->sc->cur_scp)
3862 sysbeep(1193182 / pitch, duration);
3867 blink_screen(void *arg)
3869 scr_stat *scp = arg;
3872 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3873 scp->sc->blink_in_progress = 0;
3875 tp = SC_DEV(scp->sc, scp->index);
3876 if (tty_opened_ns(tp))
3877 sctty_outwakeup(tp);
3878 if (scp->sc->delayed_next_scr)
3879 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3882 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3883 scp->sc->blink_in_progress & 1);
3884 scp->sc->blink_in_progress--;
3885 callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0,
3886 blink_screen, scp, C_PREL(0));
3891 * Until sc_attach_unit() gets called no dev structures will be available
3892 * to store the per-screen current status. This is the case when the
3893 * kernel is initially booting and needs access to its console. During
3894 * this early phase of booting the console's current status is kept in
3895 * one statically defined scr_stat structure, and any pointers to the
3896 * dev structures will be NULL.
3900 sc_get_stat(struct tty *tp)
3903 return (&main_console);
3904 return (SC_STAT(tp));
3908 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3909 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3914 sc_allocate_keyboard(sc_softc_t *sc, int unit)
3920 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3922 k0 = kbd_get_keyboard(idx0);
3924 for (idx = kbd_find_keyboard2("*", -1, 0);
3926 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
3927 k = kbd_get_keyboard(idx);
3929 if (idx == idx0 || KBD_IS_BUSY(k))
3932 bzero(&ki, sizeof(ki));
3933 strcpy(ki.kb_name, k->kb_name);
3934 ki.kb_unit = k->kb_unit;
3936 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3939 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);