nrelease: Clean up a bit the 'clean' target
[dragonfly.git] / sys / dev / misc / syscons / syscons.c
blob5336808a66e24b0d5520a19b9d3a1ab651723e3e
1 /*-
2 * Copyright (c) 1992-1998 Søren Schmidt
3 * All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Simple font scaling code by Sascha Wildner and Matthew Dillon
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/dev/syscons/syscons.c,v 1.336.2.17 2004/03/25 08:41:09 ru Exp $
36 #include "use_splash.h"
37 #include "opt_syscons.h"
38 #include "opt_ddb.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/eventhandler.h>
43 #include <sys/reboot.h>
44 #include <sys/caps.h>
45 #include <sys/conf.h>
46 #include <sys/proc.h>
47 #include <sys/caps.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/taskqueue.h>
51 #include <sys/tty.h>
52 #include <sys/ttydefaults.h> /* for TTYDEF_* */
53 #include <sys/kernel.h>
54 #include <sys/cons.h>
55 #include <sys/random.h>
56 #include <ddb/ddb.h>
58 #include <sys/thread2.h>
59 #include <sys/mutex2.h>
61 #include <machine/clock.h>
62 #include <machine/console.h>
63 #include <machine/psl.h>
64 #include <machine/pc/display.h>
65 #include <machine/frame.h>
66 #include <machine/framebuffer.h>
68 #include <dev/misc/kbd/kbdreg.h>
69 #include <dev/video/fb/fbreg.h>
70 #include <dev/video/fb/splashreg.h>
71 #include "syscons.h"
73 #define COLD 0
74 #define WARM 1
76 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
77 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
79 #define SCRN_ASYNCOK 0x0001
80 #define SCRN_BULKUNLOCK 0x0002
82 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
83 #define WANT_UNLOCK(m) do { \
84 if (m) \
85 syscons_unlock(); \
86 } while (0)
88 #define WANT_LOCK(m) do { \
89 if (m) \
90 syscons_lock(); \
91 } while(0)
94 MALLOC_DEFINE(M_SYSCONS, "syscons", "Syscons");
96 typedef struct default_attr {
97 int std_color; /* normal hardware color */
98 int rev_color; /* reverse hardware color */
99 } default_attr;
101 static default_attr user_default = {
102 SC_NORM_ATTR,
103 SC_NORM_REV_ATTR,
106 static default_attr kernel_default = {
107 SC_KERNEL_CONS_ATTR,
108 SC_KERNEL_CONS_REV_ATTR,
111 static int sc_console_unit = -1;
112 static scr_stat *sc_console;
113 static struct tty *sc_console_tty;
114 static void *kernel_console_ts;
116 static char init_done = COLD;
117 static char shutdown_in_progress = FALSE;
118 static char sc_malloc = FALSE;
119 static char sc_filled_border = FALSE; /* filled initial VT border */
121 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
122 static int run_scrn_saver = FALSE; /* should run the saver? */
123 static long scrn_blank_time = 0; /* screen saver timeout value */
124 #if NSPLASH > 0
125 static int scrn_blanked; /* # of blanked screen */
126 static int sticky_splash = FALSE;
128 static void none_saver(sc_softc_t *sc, int blank) { }
129 static void (*current_saver)(sc_softc_t *, int) = none_saver;
130 #endif
133 * Lock for asynchronous screen update thread, needed to safely modify the
134 * framebuffer information.
136 static struct lock sc_asynctd_lk;
138 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
139 #include "font.h"
140 #endif
142 static bios_values_t bios_value;
144 static int enable_panic_key;
145 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
146 0, "Enable the panic key (CTRL-ALT-SHIFT-ESC)");
147 static int syscons_async;
148 SYSCTL_INT(_kern, OID_AUTO, syscons_async, CTLFLAG_RW, &syscons_async,
149 0, "Asynchronous bulk syscons fb updates");
151 static int desired_cols = 0;
152 TUNABLE_INT("kern.kms_columns", &desired_cols);
154 #define SC_CONSOLECTL 255
156 #define VIRTUAL_TTY(sc, x) ((SC_DEV((sc),(x)) != NULL) ? \
157 (SC_DEV((sc),(x))->si_tty) : NULL)
158 #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
160 static int debugger;
161 static cdev_t cctl_dev;
162 static timeout_t blink_screen_callout;
163 static void sc_blink_screen(scr_stat *scp);
164 static struct mtx syscons_mtx = MTX_INITIALIZER("syscons");
165 static struct lock syscons_blink_lk = LOCK_INITIALIZER("sblnk", 0, 0);
167 /* prototypes */
168 static int scvidprobe(int unit, int flags, int cons);
169 static int sckbdprobe(int unit, int flags, int cons);
170 static void scmeminit(void *arg);
171 static int scdevtounit(cdev_t dev);
172 static kbd_callback_func_t sckbdevent;
173 static int scparam(struct tty *tp, struct termios *t);
174 static void scstart(struct tty *tp);
175 static void scinit(int unit, int flags);
176 #if 0
177 static void scterm(int unit, int flags);
178 #endif
179 static void scshutdown(void *arg, int howto);
180 static void sc_puts(scr_stat *scp, u_char *buf, int len);
181 static u_int scgetc(sc_softc_t *sc, u_int flags);
182 #define SCGETC_CN 1
183 #define SCGETC_NONBLOCK 2
184 static int sccngetch(int flags);
185 static void sccnupdate(scr_stat *scp);
186 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
187 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
188 static timeout_t scrn_timer;
189 static int and_region(int *s1, int *e1, int s2, int e2);
190 static void scrn_update(scr_stat *scp, int show_cursor, int flags);
191 static void scrn_update_thread(void *arg);
193 static void sc_fb_set_par(void *context, int pending);
194 #if NSPLASH > 0
195 static void sc_fb_blank(void *context, int pending);
196 #endif /* NSPLASH */
198 #if NSPLASH > 0
199 static void scframebuffer_saver(sc_softc_t *sc, int show);
200 static int scsplash_callback(int event, void *arg);
201 static void scsplash_saver(sc_softc_t *sc, int show);
202 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
203 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
204 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
205 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
206 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
207 static int wait_scrn_saver_stop(sc_softc_t *sc, int need_unlock);
208 #define scsplash_stick(stick) (sticky_splash = (stick))
209 #else /* !NSPLASH */
210 #define scsplash_stick(stick)
211 #endif /* NSPLASH */
213 static void do_switch_scr(sc_softc_t *sc);
214 static int vt_proc_alive(scr_stat *scp);
215 static int signal_vt_rel(scr_stat *scp);
216 static int signal_vt_acq(scr_stat *scp);
217 static int finish_vt_rel(scr_stat *scp, int release);
218 static int finish_vt_acq(scr_stat *scp);
219 static void exchange_scr(sc_softc_t *sc);
220 static void update_cursor_image(scr_stat *scp);
221 static int save_kbd_state(scr_stat *scp, int unlock);
222 static int update_kbd_state(scr_stat *scp, int state, int mask, int unlock);
223 static int update_kbd_leds(scr_stat *scp, int which);
224 static int sc_allocate_keyboard(sc_softc_t *sc, int unit);
227 * Console locking support functions.
229 * We use mutex spinlocks here in order to allow reentrancy which should
230 * avoid issues during panics.
232 * A panic can stop a cpu while it is holding the syscons_mtx. If this
233 * occurs we try for up to 1/2 a second and then blow the spinlock away.
234 * The asynchronous update thread makes this happen more often.
236 static void
237 syscons_lock(void)
239 if (panicstr || shutdown_in_progress) {
240 int retries = 500000;
241 while (mtx_spinlock_try(&syscons_mtx)) {
242 tsc_delay(1000); /* 1uS */
243 if (--retries == 0) {
244 mtx_init(&syscons_mtx, "syscons");
245 retries = 500000;
248 } else {
249 mtx_spinlock(&syscons_mtx);
254 * Returns 0 on success, EAGAIN on failure.
256 static int
257 syscons_lock_nonblock(void)
259 return(mtx_spinlock_try(&syscons_mtx));
262 static void
263 syscons_unlock(void)
265 mtx_spinunlock(&syscons_mtx);
269 * Console driver
271 static cn_probe_t sccnprobe;
272 static cn_init_t sccninit;
273 static cn_init_t sccninit_fini;
274 static cn_getc_t sccngetc;
275 static cn_checkc_t sccncheckc;
276 static cn_putc_t sccnputc;
277 static cn_dbctl_t sccndbctl;
278 static cn_term_t sccnterm;
279 static cn_poll_t sccnpoll;
281 CONS_DRIVER(sc, sccnprobe, sccninit, sccninit_fini, sccnterm,
282 sccngetc, sccncheckc, sccnputc, sccndbctl, sccnpoll);
284 static d_open_t scopen;
285 static d_close_t scclose;
286 static d_read_t scread;
287 static d_ioctl_t scioctl;
288 static d_mmap_t scmmap;
290 static struct dev_ops sc_ops = {
291 { "sc", 0, D_TTY | D_MPSAFE },
292 .d_open = scopen,
293 .d_close = scclose,
294 .d_read = scread,
295 .d_write = ttywrite,
296 .d_ioctl = scioctl,
297 .d_mmap = scmmap,
298 .d_kqfilter = ttykqfilter,
299 .d_revoke = ttyrevoke
303 sc_probe_unit(int unit, int flags)
305 if ((flags & SC_EFI_FB) && (probe_efi_fb(0) != 0)) {
306 if (bootverbose)
307 kprintf("sc%d: no EFI framebuffer found.\n", unit);
308 flags &= ~SC_EFI_FB;
311 if (!(flags & SC_EFI_FB) && !scvidprobe(unit, flags, FALSE)) {
312 if (bootverbose)
313 kprintf("sc%d: no video adapter found.\n", unit);
314 return ENXIO;
317 /* syscons will be attached even when there is no keyboard */
318 sckbdprobe(unit, flags, FALSE);
320 return 0;
324 register_framebuffer(struct fb_info *info)
326 sc_softc_t *sc;
328 /* For now ignore framebuffers, which don't replace the vga display */
329 if (!info->is_vga_boot_display)
330 return 0;
332 lwkt_gettoken(&vga_token);
333 sc = sc_get_softc(0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
334 if (sc == NULL) {
335 lwkt_reltoken(&vga_token);
336 kprintf("%s: sc_get_softc(%d, %d) returned NULL\n", __func__,
337 0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
338 return 0;
341 /* Ignore this framebuffer if we already switched to KMS framebuffer */
342 if (sc->fbi != NULL && sc->fbi != &efi_fb_info &&
343 sc->fbi != sc->dummy_fb_info) {
344 lwkt_reltoken(&vga_token);
345 return 0;
348 if (sc->fb_set_par_task == NULL) {
349 sc->fb_set_par_task = kmalloc(sizeof(struct task),
350 M_SYSCONS, M_WAITOK | M_ZERO);
352 TASK_INIT(sc->fb_set_par_task, 0, sc_fb_set_par, sc);
353 #if NSPLASH > 0
354 if (sc->fb_blank_task == NULL) {
355 sc->fb_blank_task = kmalloc(sizeof(struct task),
356 M_SYSCONS, M_WAITOK | M_ZERO);
358 TASK_INIT(sc->fb_blank_task, 0, sc_fb_blank, sc);
359 #endif /* NSPLASH */
362 * Make sure that console messages don't interfere too much with
363 * modesetting.
365 atomic_add_int(&sc->videoio_in_progress, 1);
367 /* Lock against synchronous and asynchronous screen updates */
368 lockmgr(&sc_asynctd_lk, LK_EXCLUSIVE);
369 syscons_lock();
370 sc->fbi = info;
371 sc->fbi_generation++;
372 syscons_unlock();
374 kprintf("kms console: xpixels %d ypixels %d\n",
375 sc->fbi->width, sc->fbi->height);
376 sc_update_render(sc->cur_scp);
377 if (info->fbops.fb_set_par != NULL)
378 info->fbops.fb_set_par(info);
379 atomic_add_int(&sc->videoio_in_progress, -1);
381 sc->fb_blanked = FALSE;
382 if (sc->unit == sc_console_unit && sc->cur_scp->smode.mode != VT_AUTO)
383 cons_unavail = FALSE;
384 #if NSPLASH > 0
385 if (info->fbops.fb_blank != NULL)
386 add_scrn_saver(scframebuffer_saver);
387 #endif /* NSPLASH */
389 lockmgr(&sc_asynctd_lk, LK_RELEASE);
390 lwkt_reltoken(&vga_token);
391 return 0;
394 void
395 unregister_framebuffer(struct fb_info *info)
397 sc_softc_t *sc;
399 lwkt_gettoken(&vga_token);
400 sc = sc_get_softc(0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
401 if (sc == NULL) {
402 lwkt_reltoken(&vga_token);
403 kprintf("%s: sc_get_softc(%d, %d) returned NULL\n", __func__,
404 0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
405 return;
408 if (sc->fbi != info) {
409 lwkt_reltoken(&vga_token);
410 return;
413 #if NSPLASH > 0
414 if (info->fbops.fb_blank != NULL)
415 remove_scrn_saver(scframebuffer_saver);
416 #endif /* NSPLASH */
418 if (sc->fb_set_par_task != NULL &&
419 taskqueue_cancel(taskqueue_thread[0], sc->fb_set_par_task, NULL)) {
420 taskqueue_drain(taskqueue_thread[0], sc->fb_set_par_task);
422 #if NSPLASH > 0
423 if (sc->fb_blank_task != NULL &&
424 taskqueue_cancel(taskqueue_thread[0], sc->fb_blank_task, NULL)) {
425 taskqueue_drain(taskqueue_thread[0], sc->fb_blank_task);
427 #endif /* NSPLASH */
428 sc->fb_blanked = TRUE;
429 if (sc->unit == sc_console_unit)
430 cons_unavail = TRUE;
432 if (sc->dummy_fb_info == NULL) {
433 sc->dummy_fb_info = kmalloc(sizeof(struct fb_info),
434 M_SYSCONS, M_WAITOK | M_ZERO);
435 } else {
436 memset(sc->dummy_fb_info, 0, sizeof(struct fb_info));
438 *sc->dummy_fb_info = *sc->fbi;
439 sc->dummy_fb_info->vaddr = 0;
440 sc->dummy_fb_info->fbops.fb_set_par = NULL;
441 sc->dummy_fb_info->fbops.fb_blank = NULL;
442 sc->dummy_fb_info->fbops.fb_debug_enter = NULL;
444 /* Lock against synchronous and asynchronous screen updates */
445 lockmgr(&sc_asynctd_lk, LK_EXCLUSIVE);
446 syscons_lock();
447 sc->fbi = sc->dummy_fb_info;
448 syscons_unlock();
449 lockmgr(&sc_asynctd_lk, LK_RELEASE);
451 lwkt_reltoken(&vga_token);
454 void
455 sc_font_scale(scr_stat *scp, int max_cols, int max_rows)
457 int cols, rows;
460 * If columns not specified in /boot/loader.conf then
461 * calculate a non-fractional scaling that yields a
462 * reasonable number of rows and columns. If it is <0,
463 * don't scale at all.
465 if (desired_cols == 0) {
466 int nomag = 1;
467 while (scp->xpixel / (scp->font_width * nomag) >= COL &&
468 scp->ypixel / (scp->font_height * nomag) >= ROW) {
469 ++nomag;
471 if (nomag > 1)
472 --nomag;
473 cols = scp->xpixel / (scp->font_width * nomag);
474 } else if (desired_cols < 0) {
475 cols = scp->xpixel / scp->font_width;
476 } else {
477 cols = desired_cols;
479 scp->blk_width = scp->xpixel / cols;
480 scp->blk_height = scp->blk_width * scp->font_height / scp->font_width;
481 rows = scp->ypixel / scp->blk_height;
483 /* scp->xsize = scp->xpixel / scp->blk_width; total possible */
484 scp->xsize = max_cols ? imin(max_cols, cols) : cols;
485 scp->ysize = max_rows ? imin(max_rows, rows) : rows;
488 /* probe video adapters, return TRUE if found */
489 static int
490 scvidprobe(int unit, int flags, int cons)
493 * Access the video adapter driver through the back door!
494 * Video adapter drivers need to be configured before syscons.
495 * However, when syscons is being probed as the low-level console,
496 * they have not been initialized yet. We force them to initialize
497 * themselves here. XXX
499 vid_configure(cons ? VIO_PROBE_ONLY : 0);
501 return (vid_find_adapter("*", unit) >= 0);
504 /* probe the keyboard, return TRUE if found */
505 static int
506 sckbdprobe(int unit, int flags, int cons)
508 /* access the keyboard driver through the backdoor! */
509 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
511 return (kbd_find_keyboard("*", unit) >= 0);
514 static char *
515 adapter_name(video_adapter_t *adp)
517 static struct {
518 int type;
519 char *name[2];
520 } names[] = {
521 { KD_MONO, { "MDA", "MDA" } },
522 { KD_HERCULES, { "Hercules", "Hercules" } },
523 { KD_CGA, { "CGA", "CGA" } },
524 { KD_EGA, { "EGA", "EGA (mono)" } },
525 { KD_VGA, { "VGA", "VGA (mono)" } },
526 { KD_TGA, { "TGA", "TGA" } },
527 { -1, { "Unknown", "Unknown" } },
529 int i;
531 for (i = 0; names[i].type != -1; ++i)
532 if (names[i].type == adp->va_type)
533 break;
534 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
538 sc_attach_unit(int unit, int flags)
540 sc_softc_t *sc;
541 scr_stat *scp;
542 int vc;
543 cdev_t dev;
544 flags &= ~SC_KERNEL_CONSOLE;
545 char tbuf[MAKEDEV_MINNBUF];
547 if ((flags & SC_EFI_FB) && probe_efi_fb(0) != 0)
548 flags &= ~SC_EFI_FB;
550 if (sc_console_unit == unit) {
552 * If this unit is being used as the system console, we need to
553 * adjust some variables and buffers before and after scinit().
555 /* assert(sc_console != NULL) */
556 flags |= SC_KERNEL_CONSOLE;
557 scmeminit(NULL);
559 scinit(unit, flags);
561 if (sc_console->tsw->te_size > 0) {
562 /* assert(sc_console->ts != NULL); */
563 kernel_console_ts = sc_console->ts;
564 sc_console->ts = kmalloc(sc_console->tsw->te_size,
565 M_SYSCONS, M_WAITOK);
566 bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
567 (*sc_console->tsw->te_default_attr)(sc_console,
568 user_default.std_color,
569 user_default.rev_color);
571 } else {
572 scinit(unit, flags);
575 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
578 * If this is the console we couldn't setup sc->dev before because
579 * malloc wasn't working. Set it up now.
581 if (flags & SC_KERNEL_CONSOLE) {
582 KKASSERT(sc->dev == NULL);
583 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK|M_ZERO);
584 sc->dev[0] = make_dev(&sc_ops, sc_console_unit*MAXCONS, UID_ROOT,
585 GID_WHEEL, 0600, "ttyv%s", makedev_unit_b32(tbuf,
586 sc_console_unit * MAXCONS));
587 ttymalloc(&sc->dev[0]->si_tty);
588 sc->dev[0]->si_drv1 = sc_console;
592 * Finish up the standard attach
594 sc->config = flags;
595 callout_init_mp(&sc->scrn_timer_ch);
596 scp = SC_STAT(sc->dev[0]);
597 if (sc_console == NULL) /* sc_console_unit < 0 */
598 sc_console = scp;
600 /* initialize cursor */
601 if (!ISGRAPHSC(scp))
602 update_cursor_image(scp);
604 /* get screen update going */
605 scrn_timer(sc);
607 /* set up the keyboard */
608 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
609 update_kbd_state(scp, scp->status, LOCK_MASK, FALSE);
611 kprintf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
612 unit, (sc->config & SC_EFI_FB) ? "EFI_FB" : adapter_name(sc->adp),
613 sc->vtys, sc->config);
614 if (bootverbose) {
615 kprintf("sc%d:", unit);
616 if (sc->adapter >= 0)
617 kprintf(" fb%d", sc->adapter);
618 if (sc->keyboard >= 0)
619 kprintf(", kbd%d", sc->keyboard);
620 if (scp->tsw)
621 kprintf(", terminal emulator: %s (%s)",
622 scp->tsw->te_name, scp->tsw->te_desc);
623 kprintf("\n");
626 /* register a shutdown callback for the kernel console */
627 if (sc_console_unit == unit)
628 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
629 (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
632 * create devices.
634 * The first vty already has struct tty and scr_stat initialized
635 * in scinit(). The other vtys will have these structs when
636 * first opened.
638 for (vc = 1; vc < sc->vtys; vc++) {
639 dev = make_dev(&sc_ops, vc + unit * MAXCONS,
640 UID_ROOT, GID_WHEEL, 0600, "ttyv%s",
641 makedev_unit_b32(tbuf, vc + unit * MAXCONS));
642 sc->dev[vc] = dev;
644 cctl_dev = make_dev(&sc_ops, SC_CONSOLECTL,
645 UID_ROOT, GID_WHEEL, 0600, "consolectl");
646 cctl_dev->si_tty = ttymalloc(&sc_console_tty);
647 cctl_dev->si_drv1 = sc_console;
648 return 0;
651 static void
652 scmeminit(void *arg)
654 if (sc_malloc)
655 return;
656 sc_malloc = TRUE;
659 * As soon as malloc() becomes functional, we had better allocate
660 * various buffers for the kernel console.
663 if (sc_console_unit < 0) /* sc_console == NULL */
664 return;
666 /* copy the temporary buffer to the final buffer */
667 sc_alloc_scr_buffer(sc_console, TRUE, FALSE);
669 #ifndef SC_NO_CUTPASTE
670 sc_alloc_cut_buffer(sc_console, TRUE);
671 #endif
673 #ifndef SC_NO_HISTORY
674 /* initialize history buffer & pointers */
675 sc_alloc_history_buffer(sc_console, 0, 0, TRUE);
676 #endif
678 /* Fill initial terminal border, efi_fb_info.vaddr should be non-NULL now */
679 if (!sc_filled_border) {
680 sc_filled_border = TRUE;
681 sc_set_border(sc_console, sc_console->border);
685 SYSINIT(sc_mem, SI_BOOT1_POST, SI_ORDER_ANY, scmeminit, NULL);
687 static int
688 scdevtounit(cdev_t dev)
690 int vty = SC_VTY(dev);
692 if (vty == SC_CONSOLECTL)
693 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
694 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
695 return -1;
696 else
697 return vty/MAXCONS;
700 static int
701 scopen(struct dev_open_args *ap)
703 cdev_t dev = ap->a_head.a_dev;
704 int unit;
705 sc_softc_t *sc;
706 struct tty *tp;
707 scr_stat *scp;
708 keyarg_t key;
709 int error;
712 * Disallow access to disk volumes if RESTRICTEDROOT
714 if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT))
715 return (EPERM);
717 lwkt_gettoken(&vga_token);
718 unit = scdevtounit(dev);
719 DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
720 major(dev), minor(dev), unit, SC_VTY(dev)));
722 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
723 if (sc == NULL) {
724 lwkt_reltoken(&vga_token);
725 return ENXIO;
728 tp = ttymalloc(&dev->si_tty);
729 lwkt_gettoken(&tp->t_token);
730 tp->t_oproc = scstart;
731 tp->t_param = scparam;
732 tp->t_stop = nottystop;
734 tp->t_dev = dev;
736 if (!ISTTYOPEN(tp)) {
737 ttychars(tp);
738 /* Use the current setting of the <-- key as default VERASE. */
739 /* If the Delete key is preferable, an stty is necessary */
740 if (sc->kbd != NULL) {
741 key.keynum = KEYCODE_BS;
742 kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
743 tp->t_cc[VERASE] = key.key.map[0];
745 tp->t_iflag = TTYDEF_IFLAG;
746 tp->t_oflag = TTYDEF_OFLAG;
747 tp->t_cflag = TTYDEF_CFLAG;
748 tp->t_lflag = TTYDEF_LFLAG;
749 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
750 scparam(tp, &tp->t_termios);
751 (*linesw[tp->t_line].l_modem)(tp, 1);
753 else
754 if ((tp->t_state & TS_XCLUDE) &&
755 caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT))
757 lwkt_reltoken(&tp->t_token);
758 lwkt_reltoken(&vga_token);
759 return(EBUSY);
762 error = (*linesw[tp->t_line].l_open)(dev, tp);
764 scp = SC_STAT(dev);
765 if (scp == NULL) {
766 scp = dev->si_drv1 = alloc_scp(sc, SC_VTY(dev));
767 syscons_lock();
768 if (ISGRAPHSC(scp))
769 sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
770 syscons_unlock();
772 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
773 tp->t_winsize.ws_col = scp->xsize;
774 tp->t_winsize.ws_row = scp->ysize;
778 * Start optional support thread for syscons refreshes. This thread
779 * will execute the refresh without the syscons_lock held and will
780 * allow interrupts while it is doing so.
782 if (scp->asynctd == NULL) {
783 lwkt_create(scrn_update_thread, scp, &scp->asynctd, NULL, 0, -1,
784 "syscons%d", SC_VTY(dev));
786 lwkt_reltoken(&tp->t_token);
787 lwkt_reltoken(&vga_token);
789 return error;
792 static int
793 scclose(struct dev_close_args *ap)
795 cdev_t dev = ap->a_head.a_dev;
796 struct tty *tp = dev->si_tty;
797 scr_stat *scp;
799 lwkt_gettoken(&vga_token);
800 lwkt_gettoken(&tp->t_token);
801 if (SC_VTY(dev) != SC_CONSOLECTL) {
802 scp = SC_STAT(tp->t_dev);
803 /* were we in the middle of the VT switching process? */
804 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
805 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit) &&
806 !scp->sc->fb_blanked) {
807 cons_unavail = FALSE;
809 if (finish_vt_rel(scp, TRUE) == 0) /* force release */
810 DPRINTF(5, ("reset WAIT_REL, "));
811 if (finish_vt_acq(scp) == 0) /* force acknowledge */
812 DPRINTF(5, ("reset WAIT_ACQ, "));
813 syscons_lock();
814 #if 0 /* notyet */
815 if (scp == &main_console) {
816 scp->pid = 0;
817 scp->proc = NULL;
818 scp->smode.mode = VT_AUTO;
820 else {
821 sc_vtb_destroy(&scp->vtb);
822 sc_vtb_destroy(&scp->scr);
823 sc_free_history_buffer(scp, scp->ysize);
824 SC_STAT(dev) = NULL;
825 kfree(scp, M_SYSCONS);
827 #else
828 scp->pid = 0;
829 scp->proc = NULL;
830 scp->smode.mode = VT_AUTO;
831 #endif
832 scp->kbd_mode = K_XLATE;
833 syscons_unlock();
834 if (scp == scp->sc->cur_scp)
835 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
836 DPRINTF(5, ("done.\n"));
838 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
839 ttyclose(tp);
840 lwkt_reltoken(&tp->t_token);
841 lwkt_reltoken(&vga_token);
843 return(0);
846 static int
847 scread(struct dev_read_args *ap)
849 int ret;
851 lwkt_gettoken(&vga_token);
852 sc_touch_scrn_saver();
853 ret = ttyread(ap);
854 lwkt_reltoken(&vga_token);
855 return ret;
858 static int
859 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
861 sc_softc_t *sc;
862 struct tty *cur_tty;
863 int c;
864 size_t len;
865 u_char *cp;
867 lwkt_gettoken(&vga_token);
869 * WARNING: In early boot sc->dev may not be setup yet.
871 sc = (sc_softc_t *)arg;
872 /* assert(thiskbd == sc->kbd) */
874 switch (event) {
875 case KBDIO_KEYINPUT:
876 if (sc->kbd && sc->kbd->kb_flags & KB_POLLED) {
877 lwkt_reltoken(&vga_token);
878 return 0;
880 break;
881 case KBDIO_UNLOADING:
882 syscons_lock();
883 sc->kbd = NULL;
884 sc->keyboard = -1;
885 syscons_unlock();
886 kbd_release(thiskbd, (void *)&sc->keyboard);
887 lwkt_reltoken(&vga_token);
888 return 0;
889 default:
890 lwkt_reltoken(&vga_token);
891 return EINVAL;
895 * Loop while there is still input to get from the keyboard.
896 * I don't think this is nessesary, and it doesn't fix
897 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
899 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
900 cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
901 if (!ISTTYOPEN(cur_tty)) {
902 cur_tty = sc_console_tty;
903 if (!ISTTYOPEN(cur_tty))
904 continue;
907 syscons_lock();
908 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty)) {
909 syscons_unlock();
910 continue;
912 syscons_unlock();
914 lwkt_gettoken(&cur_tty->t_token);
916 switch (KEYFLAGS(c)) {
917 case 0x0000: /* normal key */
918 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
919 break;
920 case FKEY: /* function key, return string */
921 cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
922 if (cp != NULL) {
923 while (len-- > 0)
924 (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
926 break;
927 case MKEY: /* meta is active, prepend ESC */
928 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
929 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
930 break;
931 case BKEY: /* backtab fixed sequence (esc [ Z) */
932 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
933 (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
934 (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
935 break;
937 lwkt_reltoken(&cur_tty->t_token);
940 syscons_lock();
941 sc->cur_scp->status |= MOUSE_HIDDEN;
942 syscons_unlock();
944 lwkt_reltoken(&vga_token);
945 return 0;
948 static int
949 scparam(struct tty *tp, struct termios *t)
951 lwkt_gettoken(&tp->t_token);
952 tp->t_ispeed = t->c_ispeed;
953 tp->t_ospeed = t->c_ospeed;
954 tp->t_cflag = t->c_cflag;
955 lwkt_reltoken(&tp->t_token);
957 return 0;
960 static int
961 scioctl(struct dev_ioctl_args *ap)
963 cdev_t dev = ap->a_head.a_dev;
964 u_long cmd = ap->a_cmd;
965 caddr_t data = ap->a_data;
966 int flag = ap->a_fflag;
967 int error;
968 int i;
969 struct tty *tp;
970 sc_softc_t *sc;
971 scr_stat *scp;
973 lwkt_gettoken(&vga_token);
974 tp = dev->si_tty;
976 error = sc_vid_ioctl(tp, cmd, data, flag);
977 if (error != ENOIOCTL) {
978 lwkt_reltoken(&vga_token);
979 return error;
982 #ifndef SC_NO_HISTORY
983 error = sc_hist_ioctl(tp, cmd, data, flag);
984 if (error != ENOIOCTL) {
985 lwkt_reltoken(&vga_token);
986 return error;
988 #endif
990 #ifndef SC_NO_SYSMOUSE
991 error = sc_mouse_ioctl(tp, cmd, data, flag);
992 if (error != ENOIOCTL) {
993 lwkt_reltoken(&vga_token);
994 return error;
996 #endif
998 scp = SC_STAT(tp->t_dev);
999 /* assert(scp != NULL) */
1000 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
1001 sc = scp->sc;
1003 if (scp->tsw) {
1004 syscons_lock();
1005 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag);
1006 syscons_unlock();
1007 if (error != ENOIOCTL) {
1008 lwkt_reltoken(&vga_token);
1009 return error;
1013 switch (cmd) { /* process console hardware related ioctl's */
1015 case GIO_ATTR: /* get current attributes */
1016 /* this ioctl is not processed here, but in the terminal emulator */
1017 lwkt_reltoken(&vga_token);
1018 return ENOTTY;
1020 case GIO_COLOR: /* is this a color console ? */
1021 if (sc->fbi != NULL)
1022 *(int *)data = 1;
1023 else
1024 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
1025 lwkt_reltoken(&vga_token);
1026 return 0;
1028 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
1029 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME) {
1030 lwkt_reltoken(&vga_token);
1031 return EINVAL;
1033 syscons_lock();
1034 scrn_blank_time = *(int *)data;
1035 run_scrn_saver = (scrn_blank_time != 0);
1036 syscons_unlock();
1037 lwkt_reltoken(&vga_token);
1038 return 0;
1040 case CONS_CURSORTYPE: /* set cursor type blink/noblink */
1041 syscons_lock();
1042 if (!ISGRAPHSC(sc->cur_scp))
1043 sc_remove_cursor_image(sc->cur_scp);
1044 if ((*(int*)data) & 0x01)
1045 sc->flags |= SC_BLINK_CURSOR;
1046 else
1047 sc->flags &= ~SC_BLINK_CURSOR;
1048 if ((*(int*)data) & 0x02) {
1049 sc->flags |= SC_CHAR_CURSOR;
1050 } else
1051 sc->flags &= ~SC_CHAR_CURSOR;
1053 * The cursor shape is global property; all virtual consoles
1054 * are affected. Update the cursor in the current console...
1056 if (!ISGRAPHSC(sc->cur_scp)) {
1057 sc_set_cursor_image(sc->cur_scp);
1058 sc_draw_cursor_image(sc->cur_scp);
1060 syscons_unlock();
1061 lwkt_reltoken(&vga_token);
1062 return 0;
1064 case CONS_BELLTYPE: /* set bell type sound/visual */
1065 syscons_lock();
1067 if ((*(int *)data) & 0x01)
1068 sc->flags |= SC_VISUAL_BELL;
1069 else
1070 sc->flags &= ~SC_VISUAL_BELL;
1072 if ((*(int *)data) & 0x02)
1073 sc->flags |= SC_QUIET_BELL;
1074 else
1075 sc->flags &= ~SC_QUIET_BELL;
1077 syscons_unlock();
1078 lwkt_reltoken(&vga_token);
1079 return 0;
1081 case CONS_GETINFO: /* get current (virtual) console info */
1083 vid_info_t *ptr = (vid_info_t*)data;
1084 if (ptr->size == sizeof(struct vid_info)) {
1085 ptr->m_num = sc->cur_scp->index;
1086 ptr->font_size = scp->font_height;
1087 ptr->mv_col = scp->xpos;
1088 ptr->mv_row = scp->ypos;
1089 ptr->mv_csz = scp->xsize;
1090 ptr->mv_rsz = scp->ysize;
1092 * The following fields are filled by the terminal emulator. XXX
1094 * ptr->mv_norm.fore
1095 * ptr->mv_norm.back
1096 * ptr->mv_rev.fore
1097 * ptr->mv_rev.back
1099 ptr->mv_grfc.fore = 0; /* not supported */
1100 ptr->mv_grfc.back = 0; /* not supported */
1101 ptr->mv_ovscan = scp->border;
1102 if (scp == sc->cur_scp)
1103 save_kbd_state(scp, FALSE);
1104 ptr->mk_keylock = scp->status & LOCK_MASK;
1105 lwkt_reltoken(&vga_token);
1106 return 0;
1108 lwkt_reltoken(&vga_token);
1109 return EINVAL;
1112 case CONS_GETVERS: /* get version number */
1113 *(int*)data = 0x200; /* version 2.0 */
1114 lwkt_reltoken(&vga_token);
1115 return 0;
1117 case CONS_IDLE: /* see if the screen has been idle */
1119 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
1120 * the user process may have been writing something on the
1121 * screen and syscons is not aware of it. Declare the screen
1122 * is NOT idle if it is in one of these modes. But there is
1123 * an exception to it; if a screen saver is running in the
1124 * graphics mode in the current screen, we should say that the
1125 * screen has been idle.
1127 *(int *)data = (sc->flags & SC_SCRN_IDLE)
1128 && (!ISGRAPHSC(sc->cur_scp)
1129 || (sc->cur_scp->status & SAVER_RUNNING));
1130 lwkt_reltoken(&vga_token);
1131 return 0;
1133 case CONS_SAVERMODE: /* set saver mode */
1134 switch(*(int *)data) {
1135 case CONS_NO_SAVER:
1136 case CONS_USR_SAVER:
1137 syscons_lock();
1138 /* if a LKM screen saver is running, stop it first. */
1139 scsplash_stick(FALSE);
1140 saver_mode = *(int *)data;
1141 #if NSPLASH > 0
1142 if ((error = wait_scrn_saver_stop(NULL, TRUE))) {
1143 syscons_unlock();
1144 lwkt_reltoken(&vga_token);
1145 return error;
1147 #endif /* NSPLASH */
1148 run_scrn_saver = TRUE;
1149 if (saver_mode == CONS_USR_SAVER)
1150 scp->status |= SAVER_RUNNING;
1151 else
1152 scp->status &= ~SAVER_RUNNING;
1153 scsplash_stick(TRUE);
1154 syscons_unlock();
1155 break;
1156 case CONS_LKM_SAVER:
1157 syscons_lock();
1158 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
1159 scp->status &= ~SAVER_RUNNING;
1160 saver_mode = *(int *)data;
1161 syscons_unlock();
1162 break;
1163 default:
1164 lwkt_reltoken(&vga_token);
1165 return EINVAL;
1167 lwkt_reltoken(&vga_token);
1168 return 0;
1170 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
1172 * Note that this ioctl does not guarantee the screen saver
1173 * actually starts or stops. It merely attempts to do so...
1175 syscons_lock();
1176 run_scrn_saver = (*(int *)data != 0);
1177 if (run_scrn_saver)
1178 sc->scrn_time_stamp -= scrn_blank_time;
1179 syscons_unlock();
1180 lwkt_reltoken(&vga_token);
1181 return 0;
1183 case CONS_SCRSHOT: /* get a screen shot */
1185 scrshot_t *ptr = (scrshot_t*)data;
1186 syscons_lock();
1187 if (ISGRAPHSC(scp)) {
1188 syscons_unlock();
1189 lwkt_reltoken(&vga_token);
1190 return EOPNOTSUPP;
1192 if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
1193 syscons_unlock();
1194 lwkt_reltoken(&vga_token);
1195 return EINVAL;
1197 syscons_unlock();
1198 copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
1199 ptr->xsize * ptr->ysize * sizeof(uint16_t));
1200 lwkt_reltoken(&vga_token);
1201 return 0;
1204 case VT_SETMODE: /* set screen switcher mode */
1206 struct vt_mode *mode;
1208 mode = (struct vt_mode *)data;
1209 DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
1210 if (scp->smode.mode == VT_PROCESS) {
1211 if (scp->proc == pfindn(scp->pid) && scp->proc != curproc) {
1212 DPRINTF(5, ("error EPERM\n"));
1213 lwkt_reltoken(&vga_token);
1214 return EPERM;
1217 syscons_lock();
1218 if (mode->mode == VT_AUTO) {
1219 scp->smode.mode = VT_AUTO;
1220 scp->proc = NULL;
1221 scp->pid = 0;
1222 DPRINTF(5, ("VT_AUTO, "));
1223 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit) &&
1224 !sc->fb_blanked) {
1225 cons_unavail = FALSE;
1227 if (finish_vt_rel(scp, TRUE) == 0)
1228 DPRINTF(5, ("reset WAIT_REL, "));
1229 if (finish_vt_acq(scp) == 0)
1230 DPRINTF(5, ("reset WAIT_ACQ, "));
1231 } else {
1232 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
1233 || !ISSIGVALID(mode->frsig)) {
1234 syscons_unlock();
1235 DPRINTF(5, ("error EINVAL\n"));
1236 lwkt_reltoken(&vga_token);
1237 return EINVAL;
1239 DPRINTF(5, ("VT_PROCESS %d, ", curproc->p_pid));
1240 bcopy(data, &scp->smode, sizeof(struct vt_mode));
1241 scp->proc = curproc;
1242 scp->pid = scp->proc->p_pid;
1243 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
1244 cons_unavail = TRUE;
1246 syscons_unlock();
1247 DPRINTF(5, ("\n"));
1248 lwkt_reltoken(&vga_token);
1249 return 0;
1252 case VT_GETMODE: /* get screen switcher mode */
1253 bcopy(&scp->smode, data, sizeof(struct vt_mode));
1254 lwkt_reltoken(&vga_token);
1255 return 0;
1257 case VT_RELDISP: /* screen switcher ioctl */
1259 * This must be the current vty which is in the VT_PROCESS
1260 * switching mode...
1262 syscons_lock();
1263 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1264 syscons_unlock();
1265 lwkt_reltoken(&vga_token);
1266 return EINVAL;
1268 /* ...and this process is controlling it. */
1269 if (scp->proc != curproc) {
1270 syscons_unlock();
1271 lwkt_reltoken(&vga_token);
1272 return EPERM;
1274 error = EINVAL;
1275 switch(*(int *)data) {
1276 case VT_FALSE: /* user refuses to release screen, abort */
1277 if ((error = finish_vt_rel(scp, FALSE)) == 0)
1278 DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
1279 break;
1280 case VT_TRUE: /* user has released screen, go on */
1281 if ((error = finish_vt_rel(scp, TRUE)) == 0)
1282 DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
1283 break;
1284 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1285 if ((error = finish_vt_acq(scp)) == 0)
1286 DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
1287 syscons_unlock();
1288 /* Wait for drm modesetting callback to finish. */
1289 lwkt_reltoken(&vga_token);
1290 if (sc->fb_set_par_task != NULL)
1291 taskqueue_drain(taskqueue_thread[0], sc->fb_set_par_task);
1292 return error;
1293 default:
1294 break;
1296 syscons_unlock();
1297 lwkt_reltoken(&vga_token);
1298 return error;
1300 case VT_OPENQRY: /* return free virtual console */
1301 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1302 tp = VIRTUAL_TTY(sc, i);
1303 if (!ISTTYOPEN(tp)) {
1304 *(int *)data = i + 1;
1305 lwkt_reltoken(&vga_token);
1306 return 0;
1309 lwkt_reltoken(&vga_token);
1310 return EINVAL;
1312 case VT_ACTIVATE: /* switch to screen *data */
1313 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1314 syscons_lock();
1315 sc_clean_up(sc->cur_scp, TRUE);
1316 error = sc_switch_scr(sc, i);
1317 syscons_unlock();
1318 lwkt_reltoken(&vga_token);
1319 return error;
1321 case VT_WAITACTIVE: /* wait for switch to occur */
1322 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1323 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys)) {
1324 lwkt_reltoken(&vga_token);
1325 return EINVAL;
1327 syscons_lock();
1328 error = sc_clean_up(sc->cur_scp, TRUE);
1329 syscons_unlock();
1330 if (error) {
1331 lwkt_reltoken(&vga_token);
1332 return error;
1336 * scp might be NULL, we aren't sure why. Check for NULL.
1338 * http://bugs.dragonflybsd.org/issues/2481
1340 scp = SC_STAT(SC_DEV(sc, i));
1341 if (scp == NULL || scp == scp->sc->cur_scp) {
1342 lwkt_reltoken(&vga_token);
1343 return 0;
1345 error = tsleep((caddr_t)&scp->smode, PCATCH, "waitvt", 0);
1346 /* May return ERESTART */
1347 lwkt_reltoken(&vga_token);
1348 /* Wait for drm modesetting callback to finish. */
1349 if (sc->fb_set_par_task != NULL)
1350 taskqueue_drain(taskqueue_thread[0], sc->fb_set_par_task);
1351 return error;
1353 case VT_GETACTIVE: /* get active vty # */
1354 *(int *)data = sc->cur_scp->index + 1;
1355 lwkt_reltoken(&vga_token);
1356 return 0;
1358 case VT_GETINDEX: /* get this vty # */
1359 *(int *)data = scp->index + 1;
1360 lwkt_reltoken(&vga_token);
1361 return 0;
1363 case VT_LOCKSWITCH: /* prevent vty switching */
1364 syscons_lock();
1365 if ((*(int *)data) & 0x01)
1366 sc->flags |= SC_SCRN_VTYLOCK;
1367 else
1368 sc->flags &= ~SC_SCRN_VTYLOCK;
1369 syscons_unlock();
1370 lwkt_reltoken(&vga_token);
1371 return 0;
1373 case KDENABIO: /* allow io operations */
1374 error = caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT);
1375 if (error != 0) {
1376 lwkt_reltoken(&vga_token);
1377 return error;
1379 if (securelevel > 0) {
1380 lwkt_reltoken(&vga_token);
1381 return EPERM;
1383 #if defined(__x86_64__)
1384 curthread->td_lwp->lwp_md.md_regs->tf_rflags |= PSL_IOPL;
1385 #endif
1386 lwkt_reltoken(&vga_token);
1387 return 0;
1389 case KDDISABIO: /* disallow io operations (default) */
1390 #if defined(__x86_64__)
1391 curthread->td_lwp->lwp_md.md_regs->tf_rflags &= ~PSL_IOPL;
1392 #endif
1393 lwkt_reltoken(&vga_token);
1394 return 0;
1396 case KDSKBSTATE: /* set keyboard state (locks) */
1397 if (*(int *)data & ~LOCK_MASK) {
1398 lwkt_reltoken(&vga_token);
1399 return EINVAL;
1401 syscons_lock();
1402 scp->status &= ~LOCK_MASK;
1403 scp->status |= *(int *)data;
1404 syscons_unlock();
1405 if (scp == sc->cur_scp)
1406 update_kbd_state(scp, scp->status, LOCK_MASK, FALSE);
1407 lwkt_reltoken(&vga_token);
1408 return 0;
1410 case KDGKBSTATE: /* get keyboard state (locks) */
1411 if (scp == sc->cur_scp)
1412 save_kbd_state(scp, FALSE);
1413 *(int *)data = scp->status & LOCK_MASK;
1414 lwkt_reltoken(&vga_token);
1415 return 0;
1417 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1418 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1419 error = kbd_ioctl(sc->kbd, cmd, data);
1420 if (error == ENOIOCTL)
1421 error = ENODEV;
1422 lwkt_reltoken(&vga_token);
1423 return error;
1425 case KDSKBMODE: /* set keyboard mode */
1426 switch (*(int *)data) {
1427 case K_XLATE: /* switch to XLT ascii mode */
1428 case K_RAW: /* switch to RAW scancode mode */
1429 case K_CODE: /* switch to CODE mode */
1430 scp->kbd_mode = *(int *)data;
1431 if (scp == sc->cur_scp)
1432 kbd_ioctl(sc->kbd, cmd, data);
1433 lwkt_reltoken(&vga_token);
1434 return 0;
1435 default:
1436 lwkt_reltoken(&vga_token);
1437 return EINVAL;
1439 /* NOT REACHED */
1441 case KDGKBMODE: /* get keyboard mode */
1442 *(int *)data = scp->kbd_mode;
1443 lwkt_reltoken(&vga_token);
1444 return 0;
1446 case KDGKBINFO:
1447 error = kbd_ioctl(sc->kbd, cmd, data);
1448 if (error == ENOIOCTL)
1449 error = ENODEV;
1450 lwkt_reltoken(&vga_token);
1451 return error;
1453 case KDMKTONE: /* sound the bell */
1454 syscons_lock();
1455 if (*(int*)data)
1456 sc_bell(scp, (*(int*)data)&0xffff,
1457 (((*(int*)data)>>16)&0xffff)*hz/1000);
1458 else
1459 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1460 syscons_unlock();
1461 lwkt_reltoken(&vga_token);
1462 return 0;
1464 case KIOCSOUND: /* make tone (*data) hz */
1465 syscons_lock();
1466 if (scp == sc->cur_scp) {
1467 if (*(int *)data) {
1468 error = sc_tone(*(int *)data);
1469 } else {
1470 error = sc_tone(0);
1472 } else {
1473 error = 0;
1475 syscons_unlock();
1476 lwkt_reltoken(&vga_token);
1477 return error;
1479 case KDGKBTYPE: /* get keyboard type */
1480 error = kbd_ioctl(sc->kbd, cmd, data);
1481 if (error == ENOIOCTL) {
1482 /* always return something? XXX */
1483 *(int *)data = 0;
1485 lwkt_reltoken(&vga_token);
1486 return 0;
1488 case KDSETLED: /* set keyboard LED status */
1489 if (*(int *)data & ~LED_MASK) { /* FIXME: LOCK_MASK? */
1490 lwkt_reltoken(&vga_token);
1491 return EINVAL;
1493 syscons_lock();
1494 scp->status &= ~LED_MASK;
1495 scp->status |= *(int *)data;
1496 syscons_unlock();
1497 if (scp == sc->cur_scp)
1498 update_kbd_leds(scp, scp->status);
1499 lwkt_reltoken(&vga_token);
1500 return 0;
1502 case KDGETLED: /* get keyboard LED status */
1503 if (scp == sc->cur_scp)
1504 save_kbd_state(scp, FALSE);
1505 *(int *)data = scp->status & LED_MASK;
1506 lwkt_reltoken(&vga_token);
1507 return 0;
1509 case KBADDKBD: /* add/remove keyboard to/from mux */
1510 case KBRELKBD:
1511 error = kbd_ioctl(sc->kbd, cmd, data);
1512 if (error == ENOIOCTL)
1513 error = ENODEV;
1514 lwkt_reltoken(&vga_token);
1515 return error;
1517 case CONS_SETKBD: /* set the new keyboard */
1519 keyboard_t *newkbd;
1521 newkbd = kbd_get_keyboard(*(int *)data);
1522 if (newkbd == NULL) {
1523 lwkt_reltoken(&vga_token);
1524 return EINVAL;
1526 error = 0;
1527 if (sc->kbd != newkbd) {
1528 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1529 (void *)&sc->keyboard, sckbdevent, sc);
1530 /* i == newkbd->kb_index */
1531 if (i >= 0) {
1532 if (sc->kbd != NULL) {
1533 save_kbd_state(sc->cur_scp, FALSE);
1534 kbd_release(sc->kbd, (void *)&sc->keyboard);
1536 syscons_lock();
1537 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1538 sc->keyboard = i;
1539 syscons_unlock();
1540 kbd_ioctl(sc->kbd, KDSKBMODE,
1541 (caddr_t)&sc->cur_scp->kbd_mode);
1542 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1543 LOCK_MASK, FALSE);
1544 } else {
1545 error = EPERM; /* XXX */
1548 lwkt_reltoken(&vga_token);
1549 return error;
1552 case CONS_RELKBD: /* release the current keyboard */
1553 error = 0;
1554 if (sc->kbd != NULL) {
1555 save_kbd_state(sc->cur_scp, FALSE);
1556 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1557 if (error == 0) {
1558 syscons_lock();
1559 sc->kbd = NULL;
1560 sc->keyboard = -1;
1561 syscons_unlock();
1564 lwkt_reltoken(&vga_token);
1565 return error;
1567 case CONS_GETTERM: /* get the current terminal emulator info */
1569 sc_term_sw_t *sw;
1571 if (((term_info_t *)data)->ti_index == 0) {
1572 sw = scp->tsw;
1573 } else {
1574 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1576 if (sw != NULL) {
1577 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1578 sizeof(((term_info_t *)data)->ti_name));
1579 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1580 sizeof(((term_info_t *)data)->ti_desc));
1581 ((term_info_t *)data)->ti_flags = 0;
1582 lwkt_reltoken(&vga_token);
1583 return 0;
1584 } else {
1585 ((term_info_t *)data)->ti_name[0] = '\0';
1586 ((term_info_t *)data)->ti_desc[0] = '\0';
1587 ((term_info_t *)data)->ti_flags = 0;
1588 lwkt_reltoken(&vga_token);
1589 return EINVAL;
1593 case CONS_SETTERM: /* set the current terminal emulator */
1594 syscons_lock();
1595 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1596 /* FIXME: what if scp == sc_console! XXX */
1597 syscons_unlock();
1598 lwkt_reltoken(&vga_token);
1599 return error;
1601 case GIO_SCRNMAP: /* get output translation table */
1602 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1603 lwkt_reltoken(&vga_token);
1604 return 0;
1606 case PIO_SCRNMAP: /* set output translation table */
1607 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1608 for (i=0; i<sizeof(sc->scr_map); i++) {
1609 sc->scr_rmap[sc->scr_map[i]] = i;
1611 lwkt_reltoken(&vga_token);
1612 return 0;
1614 case GIO_KEYMAP: /* get keyboard translation table */
1615 case PIO_KEYMAP: /* set keyboard translation table */
1616 case GIO_DEADKEYMAP: /* get accent key translation table */
1617 case PIO_DEADKEYMAP: /* set accent key translation table */
1618 case GETFKEY: /* get function key string */
1619 case SETFKEY: /* set function key string */
1620 error = kbd_ioctl(sc->kbd, cmd, data);
1621 if (error == ENOIOCTL)
1622 error = ENODEV;
1623 lwkt_reltoken(&vga_token);
1624 return error;
1626 #ifndef SC_NO_FONT_LOADING
1628 case PIO_FONT8x8: /* set 8x8 dot font */
1629 if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
1630 lwkt_reltoken(&vga_token);
1631 return ENXIO;
1633 syscons_lock();
1634 bcopy(data, sc->font_8, 8*256);
1635 sc->fonts_loaded |= FONT_8;
1637 * FONT KLUDGE
1638 * Always use the font page #0. XXX
1639 * Don't load if the current font size is not 8x8.
1641 if (sc->fbi == NULL
1642 && ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_height < 14)) {
1643 sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
1645 syscons_unlock();
1646 lwkt_reltoken(&vga_token);
1647 return 0;
1649 case GIO_FONT8x8: /* get 8x8 dot font */
1650 if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
1651 lwkt_reltoken(&vga_token);
1652 return ENXIO;
1654 if (sc->fonts_loaded & FONT_8) {
1655 bcopy(sc->font_8, data, 8*256);
1656 lwkt_reltoken(&vga_token);
1657 return 0;
1659 else {
1660 lwkt_reltoken(&vga_token);
1661 return ENXIO;
1664 case PIO_FONT8x14: /* set 8x14 dot font */
1665 if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
1666 lwkt_reltoken(&vga_token);
1667 return ENXIO;
1669 syscons_lock();
1670 bcopy(data, sc->font_14, 14*256);
1671 sc->fonts_loaded |= FONT_14;
1673 * FONT KLUDGE
1674 * Always use the font page #0. XXX
1675 * Don't load if the current font size is not 8x14.
1677 if (sc->fbi == NULL
1678 && ISTEXTSC(sc->cur_scp)
1679 && (sc->cur_scp->font_height >= 14)
1680 && (sc->cur_scp->font_height < 16)) {
1681 sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
1683 syscons_unlock();
1684 lwkt_reltoken(&vga_token);
1685 return 0;
1687 case GIO_FONT8x14: /* get 8x14 dot font */
1688 if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
1689 lwkt_reltoken(&vga_token);
1690 return ENXIO;
1692 if (sc->fonts_loaded & FONT_14) {
1693 bcopy(sc->font_14, data, 14*256);
1694 lwkt_reltoken(&vga_token);
1695 return 0;
1697 else {
1698 lwkt_reltoken(&vga_token);
1699 return ENXIO;
1702 case PIO_FONT8x16: /* set 8x16 dot font */
1703 if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
1704 lwkt_reltoken(&vga_token);
1705 return ENXIO;
1707 syscons_lock();
1708 bcopy(data, sc->font_16, 16*256);
1709 sc->fonts_loaded |= FONT_16;
1711 * FONT KLUDGE
1712 * Always use the font page #0. XXX
1713 * Don't load if the current font size is not 8x16.
1715 if (sc->fbi == NULL
1716 && ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_height >= 16)) {
1717 sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
1719 syscons_unlock();
1720 lwkt_reltoken(&vga_token);
1721 return 0;
1723 case GIO_FONT8x16: /* get 8x16 dot font */
1724 if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
1725 lwkt_reltoken(&vga_token);
1726 return ENXIO;
1728 if (sc->fonts_loaded & FONT_16) {
1729 bcopy(sc->font_16, data, 16*256);
1730 lwkt_reltoken(&vga_token);
1731 return 0;
1733 else {
1734 lwkt_reltoken(&vga_token);
1735 return ENXIO;
1738 #endif /* SC_NO_FONT_LOADING */
1740 default:
1741 break;
1744 lwkt_gettoken(&tp->t_token);
1745 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, ap->a_cred);
1746 if (error != ENOIOCTL) {
1747 lwkt_reltoken(&tp->t_token);
1748 lwkt_reltoken(&vga_token);
1750 return(error);
1753 error = ttioctl(tp, cmd, data, flag);
1754 if (error != ENOIOCTL) {
1755 lwkt_reltoken(&tp->t_token);
1756 lwkt_reltoken(&vga_token);
1758 return(error);
1761 lwkt_reltoken(&tp->t_token);
1762 lwkt_reltoken(&vga_token);
1764 return(ENOTTY);
1767 static void
1768 scstart(struct tty *tp)
1770 struct clist *rbp;
1771 int len;
1772 u_char buf[PCBURST];
1773 scr_stat *scp = SC_STAT(tp->t_dev);
1775 syscons_lock();
1776 if (scp->status & SLKED ||
1777 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
1779 syscons_unlock();
1780 return;
1782 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1783 tp->t_state |= TS_BUSY;
1784 rbp = &tp->t_outq;
1785 while (rbp->c_cc) {
1786 len = clist_qtob(rbp, buf, PCBURST);
1787 sc_puts(scp, buf, len);
1789 tp->t_state &= ~TS_BUSY;
1790 syscons_unlock();
1791 ttwwakeup(tp);
1792 } else {
1793 syscons_unlock();
1797 static void
1798 sccnprobe(struct consdev *cp)
1800 int unit;
1801 int flags;
1803 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1805 /* a video card is always required */
1806 if (probe_efi_fb(1) != 0 && !scvidprobe(unit, flags, TRUE))
1807 cp->cn_pri = CN_DEAD;
1809 /* syscons will become console even when there is no keyboard */
1810 sckbdprobe(unit, flags, TRUE);
1812 if (cp->cn_pri == CN_DEAD) {
1813 return;
1816 /* initialize required fields */
1817 cp->cn_probegood = 1;
1820 static void
1821 sccninit(struct consdev *cp)
1823 int unit;
1824 int flags;
1826 sc_get_cons_priority(&unit, &flags);
1827 scinit(unit, flags | SC_KERNEL_CONSOLE);
1828 sc_console_unit = unit;
1829 sc_console = sc_get_softc(unit, SC_KERNEL_CONSOLE)->console_scp;
1832 static void
1833 sccninit_fini(struct consdev *cp)
1835 if (cctl_dev == NULL)
1836 kprintf("sccninit_fini: WARNING: cctl_dev is NULL!\n");
1837 cp->cn_dev = cctl_dev;
1841 * This is called when the console is switched away from syscons to
1842 * a late-configuring device (such as a PCIe serial port). Since
1843 * late-configuring devices basically didn't exist until now, this
1844 * routine was never called, and SURPRISE! It doesn't work... causes
1845 * syscons to implode due to all the funny console-not-console structure
1846 * selection syscons does.
1848 * Make it a NOP. Just leave the console designation intact, even
1849 * though it is no longer a console.
1851 static void
1852 sccnterm(struct consdev *cp)
1854 #if 0
1855 /* we are not the kernel console any more, release everything */
1857 if (sc_console_unit < 0)
1858 return; /* shouldn't happen */
1860 #if 0 /* XXX */
1861 syscons_lock();
1862 sc_clear_screen(sc_console);
1863 sccnupdate(sc_console);
1864 syscons_unlock();
1865 #endif
1866 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1867 sc_console_unit = -1;
1868 sc_console = NULL;
1869 #endif
1873 * Console path - cannot block!
1875 static void
1876 sccnputc(void *private, int c)
1878 u_char buf[1];
1879 scr_stat *scp = sc_console;
1880 void *save;
1881 #ifndef SC_NO_HISTORY
1882 #if 0
1883 struct tty *tp;
1884 #endif
1885 #endif /* !SC_NO_HISTORY */
1887 /* assert(sc_console != NULL) */
1889 if (syscons_lock_nonblock() != 0)
1890 return;
1891 #ifndef SC_NO_HISTORY
1892 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1893 scp->status &= ~SLKED;
1894 #if 0
1895 /* This can block, illegal in the console path */
1896 update_kbd_state(scp, scp->status, SLKED, TRUE);
1897 #endif
1898 if (scp->status & BUFFER_SAVED) {
1899 if (!sc_hist_restore(scp))
1900 sc_remove_cutmarking(scp);
1901 scp->status &= ~BUFFER_SAVED;
1902 scp->status |= CURSOR_ENABLED;
1903 sc_draw_cursor_image(scp);
1905 #if 0
1906 tp = VIRTUAL_TTY(scp->sc, scp->index);
1907 /* This can block, illegal in the console path */
1908 if (ISTTYOPEN(tp)) {
1909 scstart(tp);
1911 #endif
1913 #endif /* !SC_NO_HISTORY */
1915 save = scp->ts;
1916 if (kernel_console_ts != NULL)
1917 scp->ts = kernel_console_ts;
1918 buf[0] = c;
1919 sc_puts(scp, buf, 1);
1920 scp->ts = save;
1922 sccnupdate(scp);
1923 syscons_unlock();
1927 * Console path - cannot block!
1929 static int
1930 sccngetc(void *private)
1932 return sccngetch(0);
1936 * Console path - cannot block!
1938 static int
1939 sccncheckc(void *private)
1941 return sccngetch(SCGETC_NONBLOCK);
1945 * Set polling mode (also disables the tty feed), use when
1946 * polling for console key input.
1948 static void
1949 sccnpoll(void *private, int on)
1951 scr_stat *scp;
1953 syscons_lock();
1954 sc_touch_scrn_saver();
1955 scp = sc_console->sc->cur_scp; /* XXX */
1956 syscons_unlock();
1957 if (scp->sc->kbd)
1958 kbd_poll(scp->sc->kbd, on);
1961 static void
1962 sccndbctl(void *private, int on)
1964 /* assert(sc_console_unit >= 0) */
1965 /* try to switch to the kernel console screen */
1966 if (on && debugger == 0) {
1968 * TRY to make sure the screen saver is stopped,
1969 * and the screen is updated before switching to
1970 * the vty0.
1972 scrn_timer(NULL);
1973 if (!cold
1974 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1975 && sc_console->smode.mode == VT_AUTO) {
1976 sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
1977 syscons_lock();
1978 sc_switch_scr(sc_console->sc, sc_console->index);
1979 syscons_unlock();
1982 if (on)
1983 ++debugger;
1984 else
1985 --debugger;
1989 * Console path - cannot block!
1991 static int
1992 sccngetch(int flags)
1994 static struct fkeytab fkey;
1995 static int fkeycp;
1996 scr_stat *scp;
1997 u_char *p;
1998 int cur_mode;
1999 int c;
2001 syscons_lock();
2002 /* assert(sc_console != NULL) */
2005 * Stop the screen saver and update the screen if necessary.
2006 * What if we have been running in the screen saver code... XXX
2008 sc_touch_scrn_saver();
2009 scp = sc_console->sc->cur_scp; /* XXX */
2010 sccnupdate(scp);
2011 syscons_unlock();
2013 if (fkeycp < fkey.len) {
2014 return fkey.str[fkeycp++];
2017 if (scp->sc->kbd == NULL) {
2018 return -1;
2022 * Make sure the keyboard is accessible even when the kbd device
2023 * driver is disabled.
2025 crit_enter();
2026 kbd_enable(scp->sc->kbd);
2028 /* we shall always use the keyboard in the XLATE mode here */
2029 cur_mode = scp->kbd_mode;
2030 scp->kbd_mode = K_XLATE;
2031 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2033 c = scgetc(scp->sc, SCGETC_CN | flags);
2035 scp->kbd_mode = cur_mode;
2036 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2037 kbd_disable(scp->sc->kbd);
2038 crit_exit();
2040 switch (KEYFLAGS(c)) {
2041 case 0: /* normal char */
2042 return KEYCHAR(c);
2043 case FKEY: /* function key */
2044 p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
2045 fkey.len = fkeycp;
2046 if ((p != NULL) && (fkey.len > 0)) {
2047 bcopy(p, fkey.str, fkey.len);
2048 fkeycp = 1;
2049 return fkey.str[0];
2051 return c; /* XXX */
2052 case NOKEY:
2053 if (flags & SCGETC_NONBLOCK)
2054 return c;
2055 /* fall through */
2056 case ERRKEY:
2057 default:
2058 return -1;
2060 /* NOT REACHED */
2063 static void
2064 sccnupdate(scr_stat *scp)
2066 int ddb_active;
2068 #ifdef DDB
2069 ddb_active = db_active;
2070 #else
2071 ddb_active = 0;
2072 #endif
2074 /* this is a cut-down version of scrn_timer()... */
2077 * Don't update if videoio is in progress. However, if we are paniced
2078 * or in the debugger it is possible that the variables might be stuck,
2079 * so ignore it in such cases.
2081 if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress) {
2082 if (panicstr == NULL && ddb_active == 0 && shutdown_in_progress == 0)
2083 return;
2086 if (debugger > 0 || panicstr || ddb_active || shutdown_in_progress) {
2087 sc_touch_scrn_saver();
2088 } else if (scp != scp->sc->cur_scp) {
2089 return;
2092 if (!run_scrn_saver)
2093 scp->sc->flags &= ~SC_SCRN_IDLE;
2094 #if NSPLASH > 0
2096 * This is a hard path, we cannot call stop_scrn_saver() here.
2098 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
2099 if (scp->sc->flags & SC_SCRN_BLANKED) {
2100 sc_touch_scrn_saver();
2101 /*stop_scrn_saver(scp->sc, current_saver);*/
2103 #endif /* NSPLASH */
2105 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
2106 || scp->sc->switch_in_progress) {
2107 return;
2111 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
2112 * when write_in_progress is non-zero. XXX
2114 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
2115 scrn_update(scp, TRUE, SCRN_ASYNCOK);
2118 static void
2119 scrn_timer(void *arg)
2121 static int kbd_interval = 0;
2122 struct timeval tv;
2123 sc_softc_t *sc;
2124 scr_stat *scp;
2125 int again;
2128 * Setup depending on who called us
2130 again = (arg != NULL);
2131 if (arg != NULL) {
2132 sc = (sc_softc_t *)arg;
2133 } else if (sc_console != NULL) {
2134 sc = sc_console->sc;
2135 } else {
2136 return;
2140 * Don't do anything when we are performing some I/O operations.
2141 * (These are initiated by the frontend?)
2143 if (sc->font_loading_in_progress || sc->videoio_in_progress) {
2144 if (again)
2145 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
2146 return;
2150 * Try to allocate a keyboard automatically
2152 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
2153 if (++kbd_interval >= 25) {
2154 sc->keyboard = sc_allocate_keyboard(sc, -1);
2155 if (sc->keyboard >= 0) {
2156 sc->kbd = kbd_get_keyboard(sc->keyboard);
2157 kbd_ioctl(sc->kbd, KDSKBMODE,
2158 (caddr_t)&sc->cur_scp->kbd_mode);
2159 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
2160 LOCK_MASK, FALSE);
2162 kbd_interval = 0;
2167 * Should we stop the screen saver? We need the syscons_lock
2168 * for most of this stuff.
2170 getmicrouptime(&tv);
2172 if (syscons_lock_nonblock() != 0) {
2173 /* failed to get the lock */
2174 if (again)
2175 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
2176 return;
2178 /* successful lock */
2180 if (debugger > 0 || panicstr || shutdown_in_progress)
2181 sc_touch_scrn_saver();
2182 if (run_scrn_saver) {
2183 if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
2184 sc->flags |= SC_SCRN_IDLE;
2185 else
2186 sc->flags &= ~SC_SCRN_IDLE;
2187 } else {
2188 sc->scrn_time_stamp = tv.tv_sec;
2189 sc->flags &= ~SC_SCRN_IDLE;
2190 if (scrn_blank_time > 0)
2191 run_scrn_saver = TRUE;
2193 #if NSPLASH > 0
2194 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE)) {
2195 if ((sc->flags & SC_SCRN_BLANKED) && !ISGRAPHSC(sc->cur_scp))
2196 stop_scrn_saver(sc, current_saver);
2198 #endif /* NSPLASH */
2200 /* should we just return ? */
2201 if (sc->blink_in_progress || sc->switch_in_progress ||
2202 sc->write_in_progress)
2204 syscons_unlock();
2205 if (again)
2206 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
2207 return;
2210 /* Update the screen */
2211 scp = sc->cur_scp; /* cur_scp may have changed... */
2212 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
2213 scrn_update(scp, TRUE, SCRN_ASYNCOK);
2215 #if NSPLASH > 0
2216 /* should we activate the screen saver? */
2217 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
2218 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
2219 (*current_saver)(sc, TRUE);
2220 #endif /* NSPLASH */
2222 syscons_unlock();
2223 if (again)
2224 callout_reset(&sc->scrn_timer_ch, hz / 25, scrn_timer, sc);
2227 static int
2228 and_region(int *s1, int *e1, int s2, int e2)
2230 if (*e1 < s2 || e2 < *s1)
2231 return FALSE;
2232 *s1 = imax(*s1, s2);
2233 *e1 = imin(*e1, e2);
2234 return TRUE;
2238 * Refresh modified frame buffer range. Also used to
2239 * scroll (entire FB is marked modified).
2241 * This function is allowed to run without the syscons_lock,
2242 * so scp fields can change unexpected. We cache most values
2243 * that we care about and silently discard illegal ranges.
2245 static void
2246 scrn_update(scr_stat *scp, int show_cursor, int flags)
2248 int start;
2249 int end;
2250 int cpos;
2251 int copos;
2252 int s;
2253 int e;
2254 int ddb_active;
2256 #ifdef DDB
2257 ddb_active = db_active;
2258 #else
2259 ddb_active = 0;
2260 #endif
2263 * Try to run large screen updates as an async operation, which will
2264 * call this function again from a thread with BULKUNLOCK set, allowing
2265 * interrupts during the update which tends to make the system behave
2266 * better (no sound glitches, etc).
2268 if ((flags & SCRN_ASYNCOK) && syscons_async && scp->asynctd &&
2269 (scp->end - scp->start) > 16 &&
2270 panicstr == NULL && ddb_active == 0 && shutdown_in_progress == 0) {
2271 scp->show_cursor = show_cursor;
2272 scp->queue_update_td = 1;
2273 wakeup(&scp->asynctd);
2274 return;
2276 if (flags & SCRN_BULKUNLOCK)
2277 syscons_lock();
2280 * Synchronous operation or called from
2283 /* assert(scp == scp->sc->cur_scp) */
2284 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2286 cpos = scp->cursor_pos;
2287 copos = scp->cursor_oldpos;
2289 #ifndef SC_NO_CUTPASTE
2290 /* remove the previous mouse pointer image if necessary */
2291 if (scp->status & MOUSE_VISIBLE) {
2292 s = scp->mouse_pos;
2293 e = scp->mouse_pos + scp->xsize + 1;
2294 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
2295 || and_region(&s, &e, scp->start, scp->end)
2296 || ((scp->status & CURSOR_ENABLED) &&
2297 (cpos != copos) &&
2298 (and_region(&s, &e, cpos, cpos)
2299 || and_region(&s, &e, copos, copos)))) {
2300 sc_remove_mouse_image(scp);
2301 if (scp->end >= scp->xsize*scp->ysize)
2302 scp->end = scp->xsize*scp->ysize - 1;
2305 #endif /* !SC_NO_CUTPASTE */
2308 * WARNING: Don't add debugging kprintf()s with syscons locked.
2309 * Or at all.
2311 if (scp->end >= scp->xsize*scp->ysize) {
2312 scp->end = scp->xsize*scp->ysize - 1;
2314 if (scp->start < 0) {
2315 scp->start = 0;
2319 * Main update, extract update range and reset
2320 * ASAP.
2322 start = scp->start;
2323 end = scp->end;
2324 scp->end = 0;
2325 scp->start = scp->xsize * scp->ysize - 1;
2327 if (start <= end) {
2328 if (flags & SCRN_BULKUNLOCK) {
2329 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2330 syscons_unlock();
2332 (*scp->rndr->draw)(scp, start, end - start + 1, FALSE);
2333 if (flags & SCRN_BULKUNLOCK) {
2334 syscons_lock();
2335 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2339 * Handle cut sequence
2341 s = scp->mouse_cut_start;
2342 e = scp->mouse_cut_end;
2343 cpu_ccfence(); /* allowed to race */
2345 if (e >= 0) {
2346 if (s > e) {
2347 int r = s;
2348 s = e;
2349 e = r;
2351 /* does the cut-mark region overlap with the update region? */
2352 if (and_region(&s, &e, start, end)) {
2353 if (flags & SCRN_BULKUNLOCK) {
2354 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2355 syscons_unlock();
2357 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
2358 if (flags & SCRN_BULKUNLOCK) {
2359 syscons_lock();
2360 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2366 /* we are not to show the cursor and the mouse pointer... */
2367 if (!show_cursor)
2368 goto cleanup;
2370 /* update cursor image */
2371 if (scp->status & CURSOR_ENABLED) {
2372 s = start;
2373 e = end;
2374 /* did cursor move since last time ? */
2375 if (cpos != copos) {
2376 /* do we need to remove old cursor image ? */
2377 if (!and_region(&s, &e, copos, copos))
2378 sc_remove_cursor_image(scp);
2379 sc_draw_cursor_image(scp);
2380 } else {
2381 if (s <= e && and_region(&s, &e, cpos, cpos)) {
2382 /* cursor didn't move, but has been overwritten */
2383 sc_draw_cursor_image(scp);
2384 } else if (scp->sc->flags & SC_BLINK_CURSOR) {
2385 /* if it's a blinking cursor, update it */
2386 (*scp->rndr->blink_cursor)(scp, cpos,
2387 sc_inside_cutmark(scp, cpos));
2392 #ifndef SC_NO_CUTPASTE
2393 /* update "pseudo" mouse pointer image */
2394 if (scp->sc->flags & SC_MOUSE_ENABLED) {
2395 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
2396 scp->status &= ~MOUSE_MOVED;
2397 sc_draw_mouse_image(scp);
2400 #endif /* SC_NO_CUTPASTE */
2403 * Cleanup
2405 cleanup:
2406 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2407 if (flags & SCRN_BULKUNLOCK)
2408 syscons_unlock();
2412 * Thread handles potentially expensive screen updates. The function is
2413 * expected to operate safely without locks.
2415 static void
2416 scrn_update_thread(void *arg)
2418 scr_stat *scp = arg;
2419 for (;;) {
2420 if (scp->queue_update_td == 0) {
2421 tsleep_interlock(&scp->asynctd, 0);
2422 if (scp->queue_update_td == 0)
2423 tsleep(&scp->asynctd, PINTERLOCKED, "wait", 0);
2425 scp->queue_update_td = 0;
2426 lockmgr(&sc_asynctd_lk, LK_EXCLUSIVE);
2427 atomic_add_int(&scp->sc->videoio_in_progress, 1);
2428 scrn_update(scp, scp->show_cursor, SCRN_BULKUNLOCK);
2429 atomic_add_int(&scp->sc->videoio_in_progress, -1);
2430 lockmgr(&sc_asynctd_lk, LK_RELEASE);
2434 static void
2435 sc_fb_set_par(void *context, int pending)
2437 sc_softc_t *sc = context;
2439 lwkt_gettoken(&vga_token);
2440 if (sc->fbi != NULL && sc->fbi->fbops.fb_set_par != NULL)
2441 sc->fbi->fbops.fb_set_par(sc->fbi);
2442 lwkt_reltoken(&vga_token);
2445 #if NSPLASH > 0
2446 static void
2447 sc_fb_blank(void *context, int pending)
2449 sc_softc_t *sc = context;
2451 lwkt_gettoken(&vga_token);
2452 if (sc->fbi != NULL && sc->fbi->fbops.fb_blank != NULL) {
2453 /* 0 == FB_BLANK_UNBLANK and 4 == FB_BLANK_POWERDOWN */
2454 if (sc->flags & SC_SCRN_BLANKED) {
2455 sc->fbi->fbops.fb_blank(4, sc->fbi);
2456 sc->fb_blanked = TRUE;
2457 if (sc->unit == sc_console_unit)
2458 cons_unavail = TRUE;
2459 } else {
2460 sc->fbi->fbops.fb_blank(0, sc->fbi);
2461 sc->fb_blanked = FALSE;
2462 if (sc->unit == sc_console_unit &&
2463 sc->cur_scp->smode.mode == VT_AUTO)
2464 cons_unavail = FALSE;
2467 lwkt_reltoken(&vga_token);
2470 static void
2471 scframebuffer_saver(sc_softc_t *sc, int show)
2473 scr_stat *scp = sc->cur_scp;
2475 if (panicstr)
2476 return;
2479 * Gets called on every screen update while screensaver is active,
2480 * so we need to check the SC_SCRN_BLANKED flag ourselves.
2482 if ((sc->flags & SC_SCRN_BLANKED) && show)
2483 return;
2485 taskqueue_cancel(taskqueue_thread[0], sc->fb_blank_task, NULL);
2486 crit_enter();
2487 if (show) {
2488 scp->status |= SAVER_RUNNING;
2489 sc->flags |= SC_SCRN_BLANKED;
2490 ++scrn_blanked;
2491 } else {
2492 scp->status &= ~SAVER_RUNNING;
2493 sc->flags &= ~SC_SCRN_BLANKED;
2494 --scrn_blanked;
2496 crit_exit();
2497 taskqueue_enqueue(taskqueue_thread[0], sc->fb_blank_task);
2500 static int
2501 scsplash_callback(int event, void *arg)
2503 sc_softc_t *sc;
2504 int error;
2506 sc = (sc_softc_t *)arg;
2508 switch (event) {
2509 case SPLASH_INIT:
2510 if (add_scrn_saver(scsplash_saver) == 0) {
2511 sc->flags &= ~SC_SAVER_FAILED;
2512 run_scrn_saver = TRUE;
2513 if (cold) {
2514 scsplash_stick(TRUE);
2515 (*current_saver)(sc, TRUE);
2518 return 0;
2520 case SPLASH_TERM:
2521 if (current_saver == scsplash_saver) {
2522 scsplash_stick(FALSE);
2523 error = remove_scrn_saver(scsplash_saver);
2524 if (error) {
2525 return error;
2528 return 0;
2530 default:
2531 return EINVAL;
2535 static void
2536 scsplash_saver(sc_softc_t *sc, int show)
2538 static int busy = FALSE;
2539 scr_stat *scp;
2541 if (busy)
2542 return;
2543 busy = TRUE;
2545 scp = sc->cur_scp;
2546 if (show) {
2547 if (!(sc->flags & SC_SAVER_FAILED)) {
2548 if (!(sc->flags & SC_SCRN_BLANKED))
2549 set_scrn_saver_mode(scp, -1, NULL, 0);
2550 switch (splash(sc->adp, TRUE)) {
2551 case 0: /* succeeded */
2552 break;
2553 case EAGAIN: /* try later */
2554 restore_scrn_saver_mode(scp, FALSE);
2555 sc_touch_scrn_saver(); /* XXX */
2556 break;
2557 default:
2558 sc->flags |= SC_SAVER_FAILED;
2559 scsplash_stick(FALSE);
2560 restore_scrn_saver_mode(scp, TRUE);
2561 kprintf("scsplash_saver(): failed to put up the image\n");
2562 break;
2565 } else if (!sticky_splash) {
2566 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
2567 restore_scrn_saver_mode(scp, TRUE);
2569 busy = FALSE;
2572 static int
2573 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2575 #if 0
2576 int error;
2578 if (current_saver != none_saver) {
2579 error = remove_scrn_saver(current_saver);
2580 if (error)
2581 return error;
2583 #endif
2584 if (current_saver != none_saver) {
2585 return EBUSY;
2588 run_scrn_saver = FALSE;
2589 saver_mode = CONS_LKM_SAVER;
2590 current_saver = this_saver;
2591 return 0;
2594 static int
2595 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2597 if (current_saver != this_saver)
2598 return EINVAL;
2600 #if 0
2602 * In order to prevent `current_saver' from being called by
2603 * the timeout routine `scrn_timer()' while we manipulate
2604 * the saver list, we shall set `current_saver' to `none_saver'
2605 * before stopping the current saver, rather than blocking by `splXX()'.
2607 current_saver = none_saver;
2608 if (scrn_blanked)
2609 stop_scrn_saver(this_saver);
2610 #endif
2611 /* unblank all blanked screens */
2612 wait_scrn_saver_stop(NULL, FALSE);
2613 if (scrn_blanked) {
2614 return EBUSY;
2617 current_saver = none_saver;
2618 return 0;
2621 static int
2622 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2625 /* assert(scp == scp->sc->cur_scp) */
2626 crit_enter();
2627 if (!ISGRAPHSC(scp))
2628 sc_remove_cursor_image(scp);
2629 scp->splash_save_mode = scp->mode;
2630 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2631 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2632 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2633 scp->sc->flags |= SC_SCRN_BLANKED;
2634 ++scrn_blanked;
2635 crit_exit();
2636 if (mode < 0) {
2637 return 0;
2639 scp->mode = mode;
2640 if (set_mode(scp) == 0) {
2641 if (scp->sc->fbi == NULL &&
2642 scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS) {
2643 scp->status |= GRAPHICS_MODE;
2645 if (scp->sc->fbi == NULL && pal != NULL)
2646 load_palette(scp->sc->adp, pal);
2647 sc_set_border(scp, border);
2648 return 0;
2649 } else {
2650 crit_enter();
2651 scp->mode = scp->splash_save_mode;
2652 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2653 scp->status |= scp->splash_save_status;
2654 crit_exit();
2655 return 1;
2657 /* NOTREACHED */
2660 static int
2661 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2663 int mode;
2664 int status;
2666 /* assert(scp == scp->sc->cur_scp) */
2667 crit_enter();
2668 mode = scp->mode;
2669 status = scp->status;
2670 scp->mode = scp->splash_save_mode;
2671 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2672 scp->status |= scp->splash_save_status;
2673 scp->sc->flags &= ~SC_SCRN_BLANKED;
2674 if (!changemode) {
2675 if (!ISGRAPHSC(scp))
2676 sc_draw_cursor_image(scp);
2677 --scrn_blanked;
2678 crit_exit();
2679 return 0;
2681 if (set_mode(scp) == 0) {
2682 if (scp->sc->fbi == NULL)
2683 load_palette(scp->sc->adp, scp->sc->palette);
2684 --scrn_blanked;
2685 crit_exit();
2686 return 0;
2687 } else {
2688 scp->mode = mode;
2689 scp->status = status;
2690 crit_exit();
2691 return 1;
2693 /* NOTREACHED */
2696 static void
2697 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2699 (*saver)(sc, FALSE);
2700 run_scrn_saver = FALSE;
2701 /* the screen saver may have chosen not to stop after all... */
2702 if (sc->flags & SC_SCRN_BLANKED) {
2703 return;
2706 mark_all(sc->cur_scp);
2707 if (sc->delayed_next_scr)
2708 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2709 wakeup(&scrn_blanked);
2712 static int
2713 wait_scrn_saver_stop(sc_softc_t *sc, int need_unlock)
2715 int error = 0;
2717 while (scrn_blanked > 0) {
2718 run_scrn_saver = FALSE;
2719 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2720 error = 0;
2721 break;
2723 if (need_unlock) {
2724 tsleep_interlock(&scrn_blanked, PCATCH);
2725 syscons_unlock();
2726 error = tsleep(&scrn_blanked, PCATCH | PINTERLOCKED, "scrsav", 0);
2727 syscons_lock();
2728 } else {
2729 error = tsleep(&scrn_blanked, PCATCH, "scrsav", 0);
2731 /* May return ERESTART */
2732 if (error)
2733 break;
2735 run_scrn_saver = FALSE;
2736 return error;
2738 #endif /* NSPLASH */
2740 void
2741 sc_start_scrn_saver(sc_softc_t *sc)
2743 #if NSPLASH > 0
2744 (*current_saver)(sc, TRUE);
2745 #endif
2748 void
2749 sc_stop_scrn_saver(sc_softc_t *sc)
2751 #if NSPLASH > 0
2752 if (sc->flags & SC_SCRN_BLANKED)
2753 stop_scrn_saver(sc, current_saver);
2754 #endif
2757 void
2758 sc_touch_scrn_saver(void)
2760 scsplash_stick(FALSE);
2761 run_scrn_saver = FALSE;
2765 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2767 scr_stat *cur_scp;
2768 struct tty *tp;
2770 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2772 /* prevent switch if previously requested */
2773 if (sc->flags & SC_SCRN_VTYLOCK) {
2774 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2775 sc->cur_scp->bell_duration);
2776 return EPERM;
2779 /* delay switch if the screen is blanked or being updated */
2780 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2781 || sc->blink_in_progress || sc->videoio_in_progress) {
2782 sc->delayed_next_scr = next_scr + 1;
2783 sc_touch_scrn_saver();
2784 DPRINTF(5, ("switch delayed\n"));
2785 return 0;
2788 cur_scp = sc->cur_scp;
2791 * we are in the middle of the vty switching process...
2793 * This may be in the console path, be very careful. pfindn() is
2794 * still going to use a spinlock but it no longer uses tokens so
2795 * we should be ok.
2797 if (sc->switch_in_progress &&
2798 (cur_scp->smode.mode == VT_PROCESS) &&
2799 cur_scp->proc) {
2800 if (cur_scp->proc != pfindn(cur_scp->pid)) {
2802 * The controlling process has died!!. Do some clean up.
2803 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2804 * are not reset here yet; they will be cleared later.
2806 DPRINTF(5, ("cur_scp controlling process %d died, ", cur_scp->pid));
2807 if (cur_scp->status & SWITCH_WAIT_REL) {
2809 * Force the previous switch to finish, but return now
2810 * with error.
2813 DPRINTF(5, ("reset WAIT_REL, "));
2814 finish_vt_rel(cur_scp, TRUE);
2815 DPRINTF(5, ("finishing previous switch\n"));
2816 return EINVAL;
2817 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2818 /* let's assume screen switch has been completed. */
2819 DPRINTF(5, ("reset WAIT_ACQ, "));
2820 finish_vt_acq(cur_scp);
2821 } else {
2823 * We are in between screen release and acquisition, and
2824 * reached here via scgetc() or scrn_timer() which has
2825 * interrupted exchange_scr(). Don't do anything stupid.
2827 DPRINTF(5, ("waiting nothing, "));
2829 } else {
2831 * The controlling process is alive, but not responding...
2832 * It is either buggy or it may be just taking time.
2833 * The following code is a gross kludge to cope with this
2834 * problem for which there is no clean solution. XXX
2836 if (cur_scp->status & SWITCH_WAIT_REL) {
2837 switch (sc->switch_in_progress++) {
2838 case 1:
2839 break;
2840 case 2:
2841 DPRINTF(5, ("sending relsig again, "));
2842 signal_vt_rel(cur_scp);
2843 break;
2844 case 3:
2845 break;
2846 case 4:
2847 default:
2849 * Act as if the controlling program returned
2850 * VT_FALSE.
2853 DPRINTF(5, ("force reset WAIT_REL, "));
2854 finish_vt_rel(cur_scp, FALSE);
2855 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2856 return EINVAL;
2858 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2859 switch (sc->switch_in_progress++) {
2860 case 1:
2861 break;
2862 case 2:
2863 DPRINTF(5, ("sending acqsig again, "));
2864 signal_vt_acq(cur_scp);
2865 break;
2866 case 3:
2867 break;
2868 case 4:
2869 default:
2870 /* clear the flag and finish the previous switch */
2871 DPRINTF(5, ("force reset WAIT_ACQ, "));
2872 finish_vt_acq(cur_scp);
2873 break;
2880 * Return error if an invalid argument is given, or vty switch
2881 * is still in progress.
2883 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2884 || sc->switch_in_progress) {
2885 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2886 DPRINTF(5, ("error 1\n"));
2887 return EINVAL;
2891 * Don't allow switching away from the graphics mode vty
2892 * if the switch mode is VT_AUTO, unless the next vty is the same
2893 * as the current or the current vty has been closed (but showing).
2895 tp = VIRTUAL_TTY(sc, cur_scp->index);
2896 if ((cur_scp->index != next_scr)
2897 && ISTTYOPEN(tp)
2898 && (cur_scp->smode.mode == VT_AUTO)
2899 && ISGRAPHSC(cur_scp)) {
2900 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2901 DPRINTF(5, ("error, graphics mode\n"));
2902 return EINVAL;
2906 * Is the wanted vty open? Don't allow switching to a closed vty.
2907 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2908 * Note that we always allow the user to switch to the kernel
2909 * console even if it is closed.
2911 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2912 tp = VIRTUAL_TTY(sc, next_scr);
2913 if (!ISTTYOPEN(tp)) {
2914 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2915 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2916 return EINVAL;
2918 if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2919 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2920 return EINVAL;
2924 /* this is the start of vty switching process... */
2925 ++sc->switch_in_progress;
2926 sc->delayed_next_scr = 0;
2927 sc->old_scp = cur_scp;
2928 sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2929 if (sc->new_scp == sc->old_scp) {
2930 sc->switch_in_progress = 0;
2931 wakeup((caddr_t)&sc->new_scp->smode);
2932 DPRINTF(5, ("switch done (new == old)\n"));
2933 return 0;
2936 /* has controlling process died? */
2937 vt_proc_alive(sc->old_scp);
2938 vt_proc_alive(sc->new_scp);
2940 /* wait for the controlling process to release the screen, if necessary */
2941 if (signal_vt_rel(sc->old_scp)) {
2942 return 0;
2945 /* go set up the new vty screen */
2946 exchange_scr(sc);
2948 /* wake up processes waiting for this vty */
2949 wakeup((caddr_t)&sc->cur_scp->smode);
2951 /* wait for the controlling process to acknowledge, if necessary */
2952 if (signal_vt_acq(sc->cur_scp)) {
2953 return 0;
2956 sc->switch_in_progress = 0;
2957 if (sc->unit == sc_console_unit && !sc->fb_blanked)
2958 cons_unavail = FALSE;
2959 DPRINTF(5, ("switch done\n"));
2961 return 0;
2964 static void
2965 do_switch_scr(sc_softc_t *sc)
2967 lwkt_gettoken(&vga_token);
2968 vt_proc_alive(sc->new_scp);
2970 exchange_scr(sc);
2971 /* sc->cur_scp == sc->new_scp */
2972 wakeup((caddr_t)&sc->cur_scp->smode);
2974 /* wait for the controlling process to acknowledge, if necessary */
2975 if (!signal_vt_acq(sc->cur_scp)) {
2976 sc->switch_in_progress = 0;
2977 if (sc->unit == sc_console_unit && !sc->fb_blanked)
2978 cons_unavail = FALSE;
2980 lwkt_reltoken(&vga_token);
2983 static int
2984 vt_proc_alive(scr_stat *scp)
2986 lwkt_gettoken(&vga_token);
2987 if (scp->proc) {
2988 if (scp->proc == pfindn(scp->pid)) {
2989 lwkt_reltoken(&vga_token);
2990 return TRUE;
2992 scp->proc = NULL;
2993 scp->smode.mode = VT_AUTO;
2994 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2996 lwkt_reltoken(&vga_token);
2997 return FALSE;
3000 static int
3001 signal_vt_rel(scr_stat *scp)
3003 struct proc *p;
3005 lwkt_gettoken(&vga_token);
3006 if (scp->smode.mode != VT_PROCESS) {
3007 lwkt_reltoken(&vga_token);
3008 return FALSE;
3010 scp->status |= SWITCH_WAIT_REL;
3011 p = scp->proc;
3012 PHOLD(p);
3013 ksignal(p, scp->smode.relsig);
3014 PRELE(p);
3015 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
3016 lwkt_reltoken(&vga_token);
3018 return TRUE;
3021 static int
3022 signal_vt_acq(scr_stat *scp)
3024 struct proc *p;
3026 lwkt_gettoken(&vga_token);
3027 if (scp->smode.mode != VT_PROCESS) {
3028 lwkt_reltoken(&vga_token);
3029 return FALSE;
3031 if (scp->sc->unit == sc_console_unit)
3032 cons_unavail = TRUE;
3033 scp->status |= SWITCH_WAIT_ACQ;
3034 p = scp->proc;
3035 PHOLD(p);
3036 ksignal(p, scp->smode.acqsig);
3037 PRELE(p);
3038 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
3039 lwkt_reltoken(&vga_token);
3041 return TRUE;
3044 static int
3045 finish_vt_rel(scr_stat *scp, int release)
3047 lwkt_gettoken(&vga_token);
3048 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
3049 scp->status &= ~SWITCH_WAIT_REL;
3050 if (release)
3051 do_switch_scr(scp->sc);
3052 else
3053 scp->sc->switch_in_progress = 0;
3054 lwkt_reltoken(&vga_token);
3055 return 0;
3057 lwkt_reltoken(&vga_token);
3058 return EINVAL;
3061 static int
3062 finish_vt_acq(scr_stat *scp)
3064 lwkt_gettoken(&vga_token);
3065 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
3066 scp->status &= ~SWITCH_WAIT_ACQ;
3067 scp->sc->switch_in_progress = 0;
3068 lwkt_reltoken(&vga_token);
3069 return 0;
3071 lwkt_reltoken(&vga_token);
3072 return EINVAL;
3075 static void
3076 exchange_scr(sc_softc_t *sc)
3078 scr_stat *scp;
3080 lwkt_gettoken(&vga_token);
3081 /* save the current state of video and keyboard */
3082 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
3083 if (!ISGRAPHSC(sc->old_scp))
3084 sc_remove_cursor_image(sc->old_scp);
3085 if (sc->old_scp->kbd_mode == K_XLATE)
3086 save_kbd_state(sc->old_scp, TRUE);
3088 /* set up the video for the new screen */
3089 scp = sc->cur_scp = sc->new_scp;
3090 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
3091 set_mode(scp);
3092 else if (sc->adp != NULL)
3093 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3094 (void *)sc->adp->va_window, FALSE);
3095 scp->status |= MOUSE_HIDDEN;
3096 sc_update_render(scp); /* Switch to kms renderer if necessary */
3097 sc_move_cursor(scp, scp->xpos, scp->ypos);
3098 if (!ISGRAPHSC(scp))
3099 sc_set_cursor_image(scp);
3100 if (sc->fbi == NULL && ISGRAPHSC(sc->old_scp))
3101 load_palette(sc->adp, sc->palette);
3102 if (!ISGRAPHSC(scp) || sc->fbi == NULL)
3103 sc_set_border(scp, scp->border);
3105 /* set up the keyboard for the new screen */
3106 if (sc->old_scp->kbd_mode != scp->kbd_mode)
3107 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
3108 update_kbd_state(scp, scp->status, LOCK_MASK, TRUE);
3110 mark_all(scp);
3113 * Restore DRM framebuffer console mode if necessary.
3114 * Especially avoid modesetting when switching to a vt which is in
3115 * graphics mode, because the fb_set_par() from the taskqueue thread
3116 * might race with the modesetting ioctl from the userland application.
3118 if (!ISTEXTSC(sc->old_scp) &&
3119 sc->fbi != NULL && sc->fbi->fbops.fb_set_par != NULL &&
3120 sc->fb_set_par_task != NULL) {
3121 taskqueue_enqueue(taskqueue_thread[0], sc->fb_set_par_task);
3124 lwkt_reltoken(&vga_token);
3127 static void
3128 sc_puts(scr_stat *scp, u_char *buf, int len)
3130 #if NSPLASH > 0
3131 /* make screensaver happy */
3132 if (!sticky_splash && scp == scp->sc->cur_scp)
3133 run_scrn_saver = FALSE;
3134 #endif
3136 if (scp->tsw)
3137 (*scp->tsw->te_puts)(scp, buf, len);
3139 if (scp->sc->delayed_next_scr)
3140 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3144 void
3145 sc_draw_cursor_image(scr_stat *scp)
3147 /* assert(scp == scp->sc->cur_scp); */
3148 atomic_add_int(&scp->sc->videoio_in_progress, 1);
3149 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
3150 scp->sc->flags & SC_BLINK_CURSOR, TRUE,
3151 sc_inside_cutmark(scp, scp->cursor_pos));
3152 scp->cursor_oldpos = scp->cursor_pos;
3153 atomic_add_int(&scp->sc->videoio_in_progress, -1);
3156 void
3157 sc_remove_cursor_image(scr_stat *scp)
3159 /* assert(scp == scp->sc->cur_scp); */
3160 atomic_add_int(&scp->sc->videoio_in_progress, 1);
3161 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
3162 scp->sc->flags & SC_BLINK_CURSOR, FALSE,
3163 sc_inside_cutmark(scp, scp->cursor_oldpos));
3164 atomic_add_int(&scp->sc->videoio_in_progress, -1);
3167 static void
3168 update_cursor_image(scr_stat *scp)
3170 int blink;
3172 if (scp->sc->flags & SC_CHAR_CURSOR) {
3173 scp->cursor_base = imax(0, scp->sc->cursor_base);
3174 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_height);
3175 } else {
3176 scp->cursor_base = 0;
3177 scp->cursor_height = scp->font_height;
3179 blink = scp->sc->flags & SC_BLINK_CURSOR;
3181 /* assert(scp == scp->sc->cur_scp); */
3182 atomic_add_int(&scp->sc->videoio_in_progress, 1);
3183 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE,
3184 sc_inside_cutmark(scp, scp->cursor_pos));
3185 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
3186 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE,
3187 sc_inside_cutmark(scp, scp->cursor_pos));
3188 atomic_add_int(&scp->sc->videoio_in_progress, -1);
3191 void
3192 sc_set_cursor_image(scr_stat *scp)
3194 if (scp->sc->flags & SC_CHAR_CURSOR) {
3195 scp->cursor_base = imax(0, scp->sc->cursor_base);
3196 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_height);
3197 } else {
3198 scp->cursor_base = 0;
3199 scp->cursor_height = scp->font_height;
3202 /* assert(scp == scp->sc->cur_scp); */
3203 atomic_add_int(&scp->sc->videoio_in_progress, 1);
3204 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
3205 scp->sc->flags & SC_BLINK_CURSOR);
3206 atomic_add_int(&scp->sc->videoio_in_progress, -1);
3209 static void
3210 scinit(int unit, int flags)
3213 * When syscons is being initialized as the kernel console, malloc()
3214 * is not yet functional, because various kernel structures has not been
3215 * fully initialized yet. Therefore, we need to declare the following
3216 * static buffers for the console. This is less than ideal,
3217 * but is necessry evil for the time being. XXX
3219 static scr_stat main_console;
3220 static u_short sc_buffer[2*ROW*2*COL]; /* XXX */
3221 #ifndef SC_NO_FONT_LOADING
3222 static u_char font_8[256*8];
3223 static u_char font_14[256*14];
3224 static u_char font_16[256*16];
3225 #endif
3227 sc_softc_t *sc;
3228 scr_stat *scp;
3229 video_adapter_t *adp;
3230 char tbuf[MAKEDEV_MINNBUF];
3231 int col;
3232 int row;
3233 int i;
3235 /* one time initialization */
3236 if (init_done == COLD)
3237 sc_get_bios_values(&bios_value);
3238 init_done = WARM;
3241 * Allocate resources. Even if we are being called for the second
3242 * time, we must allocate them again, because they might have
3243 * disappeared...
3245 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3246 adp = NULL;
3247 if (flags & SC_EFI_FB) {
3248 /* Don't replace already registered drm framebuffer here */
3249 if (sc->fbi == NULL) {
3250 sc->fbi = &efi_fb_info;
3251 sc->fbi_generation++;
3253 } else if (sc->adapter >= 0) {
3254 vid_release(sc->adp, (void *)&sc->adapter);
3255 adp = sc->adp;
3257 sc->adp = NULL;
3258 if (sc->keyboard >= 0) {
3259 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
3260 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
3261 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
3262 if (sc->kbd != NULL) {
3263 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
3264 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
3266 sc->kbd = NULL;
3268 if (!(flags & SC_EFI_FB)) {
3269 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
3270 sc->adp = vid_get_adapter(sc->adapter);
3271 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
3273 sc->keyboard = sc_allocate_keyboard(sc, unit);
3274 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
3275 sc->kbd = kbd_get_keyboard(sc->keyboard);
3276 if (sc->kbd != NULL) {
3277 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
3278 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
3281 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
3283 if (sc->fbi != NULL)
3284 sc->initial_mode = 0;
3285 else
3286 sc->initial_mode = sc->adp->va_initial_mode;
3288 #ifndef SC_NO_FONT_LOADING
3289 if (flags & SC_KERNEL_CONSOLE) {
3290 sc->font_8 = font_8;
3291 sc->font_14 = font_14;
3292 sc->font_16 = font_16;
3293 } else if (sc->font_8 == NULL) {
3294 /* assert(sc_malloc) */
3295 sc->font_8 = kmalloc(sizeof(font_8), M_SYSCONS, M_WAITOK);
3296 sc->font_14 = kmalloc(sizeof(font_14), M_SYSCONS, M_WAITOK);
3297 sc->font_16 = kmalloc(sizeof(font_16), M_SYSCONS, M_WAITOK);
3299 #endif
3301 if (sc->fbi != NULL) {
3302 col = 0;
3303 row = 0;
3304 } else {
3305 lwkt_gettoken(&vga_token);
3306 /* extract the hw cursor location and hide the cursor for now */
3307 (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
3308 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
3309 lwkt_reltoken(&vga_token);
3312 /* set up the first console */
3313 sc->first_vty = unit*MAXCONS;
3314 sc->vtys = MAXCONS; /* XXX: should be configurable */
3315 if (flags & SC_KERNEL_CONSOLE) {
3316 scp = &main_console;
3317 sc->console_scp = scp;
3318 init_scp(sc, sc->first_vty, scp);
3319 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
3320 (void *)sc_buffer, FALSE);
3321 if (sc_init_emulator(scp, SC_DFLT_TERM))
3322 sc_init_emulator(scp, "*");
3323 (*scp->tsw->te_default_attr)(scp,
3324 kernel_default.std_color,
3325 kernel_default.rev_color);
3326 } else {
3327 /* assert(sc_malloc) */
3328 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK | M_ZERO);
3330 sc->dev[0] = make_dev(&sc_ops, unit*MAXCONS, UID_ROOT,
3331 GID_WHEEL, 0600, "ttyv%s",
3332 makedev_unit_b32(tbuf, unit * MAXCONS));
3334 ttymalloc(&sc->dev[0]->si_tty);
3335 scp = alloc_scp(sc, sc->first_vty);
3336 sc->dev[0]->si_drv1 = scp;
3338 sc->cur_scp = scp;
3340 /* copy screen to temporary buffer */
3341 if (scp->sc->fbi == NULL) {
3342 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3343 (void *)scp->sc->adp->va_window, FALSE);
3344 if (ISTEXTSC(scp))
3345 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
3348 /* move cursors to the initial positions */
3349 if (col >= scp->xsize)
3350 col = 0;
3351 if (row >= scp->ysize)
3352 row = scp->ysize - 1;
3353 scp->xpos = col;
3354 scp->ypos = row;
3355 if (sc->fbi != NULL) {
3356 scp->cursor_pos = 0;
3357 sc->cursor_base = 0;
3358 sc->cursor_height = scp->font_height;
3359 } else {
3360 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
3361 if (bios_value.cursor_end < scp->font_height)
3362 sc->cursor_base = scp->font_height - bios_value.cursor_end - 1;
3363 else
3364 sc->cursor_base = 0;
3365 i = bios_value.cursor_end - bios_value.cursor_start + 1;
3366 sc->cursor_height = imin(i, scp->font_height);
3368 #ifndef SC_NO_SYSMOUSE
3369 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
3370 #endif
3371 if (!ISGRAPHSC(scp)) {
3372 sc_set_cursor_image(scp);
3373 sc_draw_cursor_image(scp);
3374 /* If framebuffer vaddr is still 0, we can't draw the border yet */
3375 if (scp->sc->fbi != NULL && scp->sc->fbi->vaddr != 0) {
3376 sc_filled_border = TRUE;
3377 sc_set_border(scp, scp->border);
3381 /* save font and palette */
3382 #ifndef SC_NO_FONT_LOADING
3383 sc->fonts_loaded = 0;
3384 #ifdef SC_DFLT_FONT
3385 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
3386 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
3387 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
3388 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
3389 #endif /* SC_DFLT_FONT */
3390 if (sc->adp != NULL && ISFONTAVAIL(sc->adp->va_flags)) {
3391 #ifdef SC_DFLT_FONT
3392 if (scp->font_height < 14) {
3393 sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
3394 } else if (scp->font_height >= 16) {
3395 sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
3396 } else {
3397 sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
3399 #else /* !SC_DFLT_FONT */
3400 if (scp->font_height < 14) {
3401 sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
3402 sc->fonts_loaded = FONT_8;
3403 } else if (scp->font_height >= 16) {
3404 sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
3405 sc->fonts_loaded = FONT_16;
3406 } else {
3407 sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
3408 sc->fonts_loaded = FONT_14;
3410 #endif /* SC_DFLT_FONT */
3411 /* FONT KLUDGE: always use the font page #0. XXX */
3412 sc_show_font(scp, 0);
3414 #endif /* !SC_NO_FONT_LOADING */
3416 if (!(flags & SC_EFI_FB))
3417 save_palette(sc->adp, sc->palette);
3419 #if NSPLASH > 0
3420 if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
3421 /* we are ready to put up the splash image! */
3422 splash_init(sc->adp, scsplash_callback, sc);
3423 sc->flags |= SC_SPLASH_SCRN;
3425 #endif /* NSPLASH */
3428 /* the rest is not necessary, if we have done it once */
3429 if (sc->flags & SC_INIT_DONE) {
3430 return;
3433 /* initialize mapscrn arrays to a one to one map */
3434 for (i = 0; i < sizeof(sc->scr_map); i++)
3435 sc->scr_map[i] = sc->scr_rmap[i] = i;
3437 sc->flags |= SC_INIT_DONE;
3440 #if 0
3441 static void
3442 scterm(int unit, int flags)
3444 sc_softc_t *sc;
3445 scr_stat *scp;
3447 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
3448 if (sc == NULL)
3449 return; /* shouldn't happen */
3451 lwkt_gettoken(&vga_token);
3452 #if NSPLASH > 0
3453 /* this console is no longer available for the splash screen */
3454 if (sc->flags & SC_SPLASH_SCRN) {
3455 splash_term(sc->adp);
3456 sc->flags &= ~SC_SPLASH_SCRN;
3458 #endif /* NSPLASH */
3460 #if 0 /* XXX */
3461 /* move the hardware cursor to the upper-left corner */
3462 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
3463 #endif
3465 /* release the keyboard and the video card */
3466 if (sc->keyboard >= 0)
3467 kbd_release(sc->kbd, &sc->keyboard);
3468 if (sc->adapter >= 0)
3469 vid_release(sc->adp, &sc->adapter);
3472 * Stop the terminal emulator, if any. If operating on the
3473 * kernel console sc->dev may not be setup yet.
3475 if (flags & SC_KERNEL_CONSOLE)
3476 scp = sc->console_scp;
3477 else
3478 scp = SC_STAT(sc->dev[0]);
3479 if (scp->tsw)
3480 (*scp->tsw->te_term)(scp, &scp->ts);
3481 if (scp->ts != NULL)
3482 kfree(scp->ts, M_SYSCONS);
3484 /* clear the structure */
3485 if (!(flags & SC_KERNEL_CONSOLE)) {
3486 /* XXX: We need delete_dev() for this */
3487 kfree(sc->dev, M_SYSCONS);
3488 #if 0
3489 /* XXX: We need a ttyunregister for this */
3490 /* XXX: do not race tty token access */
3491 kfree(sc->tty, M_SYSCONS);
3492 #endif
3493 #ifndef SC_NO_FONT_LOADING
3494 kfree(sc->font_8, M_SYSCONS);
3495 kfree(sc->font_14, M_SYSCONS);
3496 kfree(sc->font_16, M_SYSCONS);
3497 #endif
3498 /* XXX vtb, history */
3500 bzero(sc, sizeof(*sc));
3501 sc->keyboard = -1;
3502 sc->adapter = -1;
3503 lwkt_reltoken(&vga_token);
3505 #endif
3507 static void
3508 scshutdown(void *arg, int howto)
3510 /* assert(sc_console != NULL) */
3512 lwkt_gettoken(&vga_token);
3513 syscons_lock();
3514 sc_touch_scrn_saver();
3515 if (!cold && sc_console
3516 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
3517 && sc_console->smode.mode == VT_AUTO) {
3518 sc_switch_scr(sc_console->sc, sc_console->index);
3520 shutdown_in_progress = TRUE;
3521 syscons_unlock();
3522 lwkt_reltoken(&vga_token);
3526 sc_clean_up(scr_stat *scp, int need_unlock)
3528 #if NSPLASH > 0
3529 int error;
3530 #endif /* NSPLASH */
3532 lwkt_gettoken(&vga_token);
3533 if (scp->sc->flags & SC_SCRN_BLANKED) {
3534 sc_touch_scrn_saver();
3535 #if NSPLASH > 0
3536 if ((error = wait_scrn_saver_stop(scp->sc, need_unlock))) {
3537 lwkt_reltoken(&vga_token);
3538 return error;
3540 #endif /* NSPLASH */
3542 scp->status |= MOUSE_HIDDEN;
3543 sc_remove_mouse_image(scp);
3544 sc_remove_cutmarking(scp);
3545 lwkt_reltoken(&vga_token);
3546 return 0;
3549 void
3550 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
3552 sc_vtb_t new;
3553 sc_vtb_t old;
3555 lwkt_gettoken(&vga_token);
3556 old = scp->vtb;
3557 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
3558 if (!discard && (old.vtb_flags & VTB_VALID)) {
3559 /* retain the current cursor position and buffer contants */
3560 scp->cursor_oldpos = scp->cursor_pos;
3562 * This works only if the old buffer has the same size as or larger
3563 * than the new one. XXX
3565 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
3566 scp->vtb = new;
3567 } else {
3568 scp->vtb = new;
3569 sc_vtb_destroy(&old);
3572 #ifndef SC_NO_SYSMOUSE
3573 /* move the mouse cursor at the center of the screen */
3574 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
3575 #endif
3576 lwkt_reltoken(&vga_token);
3579 static scr_stat *
3580 alloc_scp(sc_softc_t *sc, int vty)
3582 scr_stat *scp;
3584 /* assert(sc_malloc) */
3586 scp = kmalloc(sizeof(scr_stat), M_SYSCONS, M_WAITOK);
3587 init_scp(sc, vty, scp);
3589 sc_alloc_scr_buffer(scp, TRUE, TRUE);
3590 if (sc_init_emulator(scp, SC_DFLT_TERM))
3591 sc_init_emulator(scp, "*");
3593 #ifndef SC_NO_CUTPASTE
3594 sc_alloc_cut_buffer(scp, TRUE);
3595 #endif
3597 #ifndef SC_NO_HISTORY
3598 sc_alloc_history_buffer(scp, 0, 0, TRUE);
3599 #endif
3600 return scp;
3604 * NOTE: Must be called with vga_token held.
3606 static void
3607 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3609 video_info_t info;
3611 bzero(scp, sizeof(*scp));
3613 scp->index = vty;
3614 scp->sc = sc;
3615 scp->status = 0;
3616 scp->mode = sc->initial_mode;
3617 scp->fbi_generation = sc->fbi_generation;
3618 callout_init_lk(&scp->blink_screen_ch, &syscons_blink_lk);
3619 if (scp->sc->fbi == NULL) {
3620 lwkt_gettoken(&vga_token);
3621 (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
3622 lwkt_reltoken(&vga_token);
3624 if (scp->sc->fbi != NULL) {
3625 scp->xpixel = sc->fbi->width;
3626 scp->ypixel = sc->fbi->height;
3627 scp->font_width = 8;
3628 scp->font_height = 16;
3630 /* The first vty uses a statically allocated 160x50 buffer */
3631 if (vty == sc->first_vty)
3632 sc_font_scale(scp, 2*COL, 2*ROW);
3633 else
3634 sc_font_scale(scp, 0, 0);
3636 #ifndef SC_NO_FONT_LOADING
3637 scp->font = sc->font_16;
3638 #else
3639 scp->font = NULL;
3640 #endif
3641 } else if (info.vi_flags & V_INFO_GRAPHICS) {
3642 scp->status |= GRAPHICS_MODE;
3643 scp->xpixel = info.vi_width;
3644 scp->ypixel = info.vi_height;
3645 scp->xsize = info.vi_width/8;
3646 scp->ysize = info.vi_height/info.vi_cheight;
3647 scp->font_height = 0;
3648 scp->font_width = 0;
3649 scp->font = NULL;
3650 } else {
3651 scp->xsize = info.vi_width;
3652 scp->ysize = info.vi_height;
3653 scp->xpixel = scp->xsize*8;
3654 scp->ypixel = scp->ysize*info.vi_cheight;
3655 scp->font_width = 8;
3656 if (info.vi_cheight < 14) {
3657 scp->font_height = 8;
3658 #ifndef SC_NO_FONT_LOADING
3659 scp->font = sc->font_8;
3660 #else
3661 scp->font = NULL;
3662 #endif
3663 } else if (info.vi_cheight >= 16) {
3664 scp->font_height = 16;
3665 #ifndef SC_NO_FONT_LOADING
3666 scp->font = sc->font_16;
3667 #else
3668 scp->font = NULL;
3669 #endif
3670 } else {
3671 scp->font_height = 14;
3672 #ifndef SC_NO_FONT_LOADING
3673 scp->font = sc->font_14;
3674 #else
3675 scp->font = NULL;
3676 #endif
3679 scp->xoff = scp->yoff = 0;
3680 scp->xpos = scp->ypos = 0;
3681 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3682 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3683 scp->start = scp->xsize * scp->ysize - 1;
3684 scp->end = 0;
3685 scp->tsw = NULL;
3686 scp->ts = NULL;
3687 scp->rndr = NULL;
3688 scp->border = SC_BORDER_COLOR;
3689 scp->cursor_base = sc->cursor_base;
3690 scp->cursor_height = imin(sc->cursor_height, scp->font_height);
3691 scp->mouse_cut_start = scp->xsize * scp->ysize;
3692 scp->mouse_cut_end = -1;
3693 scp->mouse_signal = 0;
3694 scp->mouse_pid = 0;
3695 scp->mouse_proc = NULL;
3696 scp->kbd_mode = K_XLATE;
3697 scp->bell_pitch = bios_value.bell_pitch;
3698 scp->bell_duration = BELL_DURATION;
3699 scp->status |= (bios_value.shift_state & NLKED);
3700 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3701 scp->pid = 0;
3702 scp->proc = NULL;
3703 scp->smode.mode = VT_AUTO;
3704 scp->history = NULL;
3705 scp->history_pos = 0;
3706 scp->history_size = 0;
3710 sc_init_emulator(scr_stat *scp, char *name)
3712 sc_term_sw_t *sw;
3713 sc_rndr_sw_t *rndr;
3714 void *p;
3715 int error;
3717 if (name == NULL) /* if no name is given, use the current emulator */
3718 sw = scp->tsw;
3719 else /* ...otherwise find the named emulator */
3720 sw = sc_term_match(name);
3721 if (sw == NULL) {
3722 return EINVAL;
3725 rndr = NULL;
3726 if (strcmp(sw->te_renderer, "*") != 0) {
3727 rndr = sc_render_match(scp, sw->te_renderer, scp->model);
3729 if (rndr == NULL && scp->sc->fbi != NULL) {
3730 rndr = sc_render_match(scp, "kms", scp->model);
3732 if (rndr == NULL) {
3733 rndr = sc_render_match(scp, scp->sc->adp->va_name, scp->model);
3734 if (rndr == NULL) {
3735 return ENODEV;
3739 if (sw == scp->tsw) {
3740 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3741 scp->rndr = rndr;
3742 sc_clear_screen(scp);
3743 /* assert(error == 0); */
3744 return error;
3747 if (sc_malloc && (sw->te_size > 0))
3748 p = kmalloc(sw->te_size, M_SYSCONS, M_NOWAIT);
3749 else
3750 p = NULL;
3751 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3752 if (error) {
3753 return error;
3756 if (scp->tsw)
3757 (*scp->tsw->te_term)(scp, &scp->ts);
3758 if (scp->ts != NULL)
3759 kfree(scp->ts, M_SYSCONS);
3760 scp->tsw = sw;
3761 scp->ts = p;
3762 scp->rndr = rndr;
3764 /* XXX */
3765 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3766 sc_clear_screen(scp);
3768 return 0;
3772 * scgetc(flags) - get character from keyboard.
3773 * If flags & SCGETC_CN, then avoid harmful side effects.
3774 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3775 * return NOKEY if there is nothing there.
3777 static u_int
3778 scgetc(sc_softc_t *sc, u_int flags)
3780 scr_stat *scp;
3781 #ifndef SC_NO_HISTORY
3782 struct tty *tp;
3783 #endif
3784 u_int c;
3785 int this_scr;
3786 int f;
3787 int i;
3789 lwkt_gettoken(&vga_token);
3790 if (sc->kbd == NULL) {
3791 lwkt_reltoken(&vga_token);
3792 return NOKEY;
3795 next_code:
3796 #if 1
3797 /* I don't like this, but... XXX */
3798 if (flags & SCGETC_CN) {
3799 syscons_lock();
3800 sccnupdate(sc->cur_scp);
3801 syscons_unlock();
3803 #endif
3804 scp = sc->cur_scp;
3805 /* first see if there is something in the keyboard port */
3806 for (;;) {
3807 c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3808 if (c == ERRKEY) {
3809 if (!(flags & SCGETC_CN))
3810 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3811 } else if (c == NOKEY) {
3812 lwkt_reltoken(&vga_token);
3813 return c;
3814 } else {
3815 break;
3819 /* make screensaver happy */
3820 if (!(c & RELKEY))
3821 sc_touch_scrn_saver();
3823 if (!(flags & SCGETC_CN))
3824 /* do the /dev/random device a favour */
3825 add_keyboard_randomness(c);
3827 if (scp->kbd_mode != K_XLATE) {
3828 lwkt_reltoken(&vga_token);
3829 return KEYCHAR(c);
3832 /* if scroll-lock pressed allow history browsing */
3833 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3835 scp->status &= ~CURSOR_ENABLED;
3836 sc_remove_cursor_image(scp);
3838 #ifndef SC_NO_HISTORY
3839 if (!(scp->status & BUFFER_SAVED)) {
3840 scp->status |= BUFFER_SAVED;
3841 sc_hist_save(scp);
3843 switch (c) {
3844 /* FIXME: key codes */
3845 case SPCLKEY | FKEY | F(49): /* home key */
3846 sc_remove_cutmarking(scp);
3847 sc_hist_home(scp);
3848 goto next_code;
3850 case SPCLKEY | FKEY | F(57): /* end key */
3851 sc_remove_cutmarking(scp);
3852 sc_hist_end(scp);
3853 goto next_code;
3855 case SPCLKEY | FKEY | F(50): /* up arrow key */
3856 sc_remove_cutmarking(scp);
3857 if (sc_hist_up_line(scp))
3858 if (!(flags & SCGETC_CN))
3859 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3860 goto next_code;
3862 case SPCLKEY | FKEY | F(58): /* down arrow key */
3863 sc_remove_cutmarking(scp);
3864 if (sc_hist_down_line(scp))
3865 if (!(flags & SCGETC_CN))
3866 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3867 goto next_code;
3869 case SPCLKEY | FKEY | F(51): /* page up key */
3870 sc_remove_cutmarking(scp);
3871 for (i=0; i<scp->ysize; i++) {
3872 if (sc_hist_up_line(scp)) {
3873 if (!(flags & SCGETC_CN))
3874 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3875 break;
3878 goto next_code;
3880 case SPCLKEY | FKEY | F(59): /* page down key */
3881 sc_remove_cutmarking(scp);
3882 for (i=0; i<scp->ysize; i++) {
3883 if (sc_hist_down_line(scp)) {
3884 if (!(flags & SCGETC_CN))
3885 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3886 break;
3889 goto next_code;
3891 #endif /* SC_NO_HISTORY */
3895 * Process and consume special keys here. Return a plain char code
3896 * or a char code with the META flag or a function key code.
3898 if (c & RELKEY) {
3899 /* key released */
3900 /* goto next_code */
3901 } else {
3902 /* key pressed */
3903 if (c & SPCLKEY) {
3904 c &= ~SPCLKEY;
3905 switch (KEYCHAR(c)) {
3906 /* LOCKING KEYS */
3907 case NLK: case CLK: case ALK:
3908 break;
3909 case SLK:
3910 kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3911 if (f & SLKED) {
3912 scp->status |= SLKED;
3913 } else {
3914 if (scp->status & SLKED) {
3915 scp->status &= ~SLKED;
3916 #ifndef SC_NO_HISTORY
3917 if (scp->status & BUFFER_SAVED) {
3918 if (!sc_hist_restore(scp))
3919 sc_remove_cutmarking(scp);
3920 scp->status &= ~BUFFER_SAVED;
3921 scp->status |= CURSOR_ENABLED;
3922 sc_draw_cursor_image(scp);
3924 tp = VIRTUAL_TTY(sc, scp->index);
3925 if (ISTTYOPEN(tp))
3926 scstart(tp);
3927 #endif
3930 break;
3932 /* NON-LOCKING KEYS */
3933 case NOP:
3934 case LSH: case RSH: case LCTR: case RCTR:
3935 case LALT: case RALT: case ASH: case META:
3936 break;
3938 case BTAB:
3939 if (!(sc->flags & SC_SCRN_BLANKED)) {
3940 lwkt_reltoken(&vga_token);
3941 return c;
3943 break;
3945 case SPSC:
3946 #if NSPLASH > 0
3947 /* force activatation/deactivation of the screen saver */
3948 if (!(sc->flags & SC_SCRN_BLANKED)) {
3949 run_scrn_saver = TRUE;
3950 sc->scrn_time_stamp -= scrn_blank_time;
3952 if (cold) {
3954 * While devices are being probed, the screen saver need
3955 * to be invoked explictly. XXX
3957 if (sc->flags & SC_SCRN_BLANKED) {
3958 scsplash_stick(FALSE);
3959 stop_scrn_saver(sc, current_saver);
3960 } else {
3961 if (!ISGRAPHSC(scp)) {
3962 scsplash_stick(TRUE);
3963 (*current_saver)(sc, TRUE);
3967 #endif /* NSPLASH */
3968 break;
3970 case RBT:
3971 #ifndef SC_DISABLE_REBOOT
3972 shutdown_nice(0);
3973 #endif
3974 break;
3976 case HALT:
3977 #ifndef SC_DISABLE_REBOOT
3978 shutdown_nice(RB_HALT);
3979 #endif
3980 break;
3982 case PDWN:
3983 #ifndef SC_DISABLE_REBOOT
3984 shutdown_nice(RB_HALT|RB_POWEROFF);
3985 #endif
3986 break;
3988 case SUSP:
3989 case STBY:
3990 break;
3992 case DBG:
3993 #ifndef SC_DISABLE_DDBKEY
3994 #ifdef DDB
3995 lwkt_reltoken(&vga_token);
3996 Debugger("manual escape to debugger");
3997 lwkt_gettoken(&vga_token);
3998 #else
3999 kprintf("No debugger in kernel\n");
4000 #endif
4001 #else /* SC_DISABLE_DDBKEY */
4002 /* do nothing */
4003 #endif /* SC_DISABLE_DDBKEY */
4004 break;
4006 case PNC:
4007 if (enable_panic_key)
4008 panic("Forced by the panic key");
4009 break;
4011 case NEXT:
4012 this_scr = scp->index;
4013 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
4014 sc->first_vty + i != this_scr;
4015 i = (i + 1)%sc->vtys) {
4016 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
4017 if (ISTTYOPEN(tp)) {
4018 syscons_lock();
4019 sc_switch_scr(scp->sc, sc->first_vty + i);
4020 syscons_unlock();
4021 break;
4024 break;
4026 case PREV:
4027 this_scr = scp->index;
4028 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
4029 sc->first_vty + i != this_scr;
4030 i = (i + sc->vtys - 1)%sc->vtys) {
4031 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
4032 if (ISTTYOPEN(tp)) {
4033 syscons_lock();
4034 sc_switch_scr(scp->sc, sc->first_vty + i);
4035 syscons_unlock();
4036 break;
4039 break;
4041 default:
4042 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
4043 syscons_lock();
4044 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
4045 syscons_unlock();
4046 break;
4048 /* assert(c & FKEY) */
4049 if (!(sc->flags & SC_SCRN_BLANKED)) {
4050 lwkt_reltoken(&vga_token);
4051 return c;
4053 break;
4055 /* goto next_code */
4056 } else {
4057 /* regular keys (maybe MKEY is set) */
4058 if (!(sc->flags & SC_SCRN_BLANKED)) {
4059 lwkt_reltoken(&vga_token);
4060 return c;
4065 goto next_code;
4068 static int
4069 scmmap(struct dev_mmap_args *ap)
4071 scr_stat *scp;
4073 lwkt_gettoken(&vga_token);
4074 scp = SC_STAT(ap->a_head.a_dev);
4075 if (scp != scp->sc->cur_scp) {
4076 lwkt_reltoken(&vga_token);
4077 return EINVAL;
4079 if (scp->sc->fbi != NULL) {
4080 size_t sz =
4081 roundup(scp->sc->fbi->height * scp->sc->fbi->stride, PAGE_SIZE);
4082 if (ap->a_offset > sz - PAGE_SIZE) {
4083 lwkt_reltoken(&vga_token);
4084 return EINVAL;
4085 } else {
4086 ap->a_result = atop(scp->sc->fbi->paddr + ap->a_offset);
4088 } else {
4089 ap->a_result = (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp,
4090 ap->a_offset,
4091 ap->a_nprot);
4093 lwkt_reltoken(&vga_token);
4094 return(0);
4097 static int
4098 save_kbd_state(scr_stat *scp, int unlock)
4100 int state;
4101 int error;
4103 WANT_UNLOCK(unlock);
4104 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4105 WANT_LOCK(unlock);
4107 if (error == ENOIOCTL)
4108 error = ENODEV;
4109 if (error == 0) {
4110 scp->status &= ~LOCK_MASK;
4111 scp->status |= state;
4113 return error;
4116 static int
4117 update_kbd_state(scr_stat *scp, int new_bits, int mask, int unlock)
4119 int state;
4120 int error;
4122 if (mask != LOCK_MASK) {
4123 WANT_UNLOCK(unlock);
4124 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
4125 WANT_LOCK(unlock);
4127 if (error == ENOIOCTL)
4128 error = ENODEV;
4129 if (error) {
4130 return error;
4132 state &= ~mask;
4133 state |= new_bits & mask;
4134 } else {
4135 state = new_bits & LOCK_MASK;
4137 WANT_UNLOCK(unlock);
4138 error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
4139 WANT_LOCK(unlock);
4140 if (error == ENOIOCTL)
4141 error = ENODEV;
4142 return error;
4145 static int
4146 update_kbd_leds(scr_stat *scp, int which)
4148 int error;
4150 which &= LOCK_MASK;
4151 error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
4152 if (error == ENOIOCTL)
4153 error = ENODEV;
4154 return error;
4158 set_mode(scr_stat *scp)
4160 video_info_t info;
4162 lwkt_gettoken(&vga_token);
4163 /* reject unsupported mode */
4164 if (scp->sc->fbi == NULL && (*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info)) {
4165 lwkt_reltoken(&vga_token);
4166 return 1;
4169 /* if this vty is not currently showing, do nothing */
4170 if (scp != scp->sc->cur_scp) {
4171 lwkt_reltoken(&vga_token);
4172 return 0;
4175 /* setup video hardware for the given mode */
4176 if (scp->sc->fbi == NULL) {
4177 (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
4178 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
4179 (void *)scp->sc->adp->va_window, FALSE);
4180 } else {
4181 goto done;
4184 #ifndef SC_NO_FONT_LOADING
4185 /* load appropriate font */
4186 if (!(scp->status & GRAPHICS_MODE)) {
4187 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
4188 if (scp->font_height < 14) {
4189 if (scp->sc->fonts_loaded & FONT_8)
4190 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
4191 } else if (scp->font_height >= 16) {
4192 if (scp->sc->fonts_loaded & FONT_16)
4193 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
4194 } else {
4195 if (scp->sc->fonts_loaded & FONT_14)
4196 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
4199 * FONT KLUDGE:
4200 * This is an interim kludge to display correct font.
4201 * Always use the font page #0 on the video plane 2.
4202 * Somehow we cannot show the font in other font pages on
4203 * some video cards... XXX
4205 sc_show_font(scp, 0);
4207 mark_all(scp);
4209 #endif /* !SC_NO_FONT_LOADING */
4211 sc_set_border(scp, scp->border);
4212 sc_set_cursor_image(scp);
4214 done:
4215 lwkt_reltoken(&vga_token);
4216 return 0;
4219 void
4220 refresh_ega_palette(scr_stat *scp)
4222 uint32_t r, g, b;
4223 int reg;
4224 int rsize, gsize, bsize;
4225 int rfld, gfld, bfld;
4226 int i;
4228 rsize = scp->sc->adp->va_info.vi_pixel_fsizes[0];
4229 gsize = scp->sc->adp->va_info.vi_pixel_fsizes[1];
4230 bsize = scp->sc->adp->va_info.vi_pixel_fsizes[2];
4231 rfld = scp->sc->adp->va_info.vi_pixel_fields[0];
4232 gfld = scp->sc->adp->va_info.vi_pixel_fields[1];
4233 bfld = scp->sc->adp->va_info.vi_pixel_fields[2];
4235 for (i = 0; i < 16; i++) {
4236 reg = scp->sc->adp->va_palette_regs[i];
4238 r = scp->sc->palette[reg * 3] >> (8 - rsize);
4239 g = scp->sc->palette[reg * 3 + 1] >> (8 - gsize);
4240 b = scp->sc->palette[reg * 3 + 2] >> (8 - bsize);
4242 scp->ega_palette[i] = (r << rfld) + (g << gfld) + (b << bfld);
4246 void
4247 sc_set_border(scr_stat *scp, int color)
4249 atomic_add_int(&scp->sc->videoio_in_progress, 1);
4250 (*scp->rndr->draw_border)(scp, color);
4251 atomic_add_int(&scp->sc->videoio_in_progress, -1);
4254 #ifndef SC_NO_FONT_LOADING
4255 void
4256 sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
4257 int base, int count)
4259 sc_softc_t *sc;
4261 sc = scp->sc;
4262 sc->font_loading_in_progress = TRUE;
4263 (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
4264 sc->font_loading_in_progress = FALSE;
4267 void
4268 sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
4269 int base, int count)
4271 sc_softc_t *sc;
4273 sc = scp->sc;
4274 sc->font_loading_in_progress = TRUE;
4275 (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
4276 sc->font_loading_in_progress = FALSE;
4279 void
4280 sc_show_font(scr_stat *scp, int page)
4282 (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
4284 #endif /* !SC_NO_FONT_LOADING */
4286 void
4287 sc_paste(scr_stat *scp, u_char *p, int count)
4289 struct tty *tp;
4290 u_char *rmap;
4293 * Holy hell, don't try to inject a paste buffer if the keyboard
4294 * is not in ascii mode!
4296 if (scp->kbd_mode != K_XLATE)
4297 return;
4299 lwkt_gettoken(&vga_token);
4300 if (scp->status & MOUSE_VISIBLE) {
4301 tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
4302 lwkt_gettoken(&tp->t_token);
4303 if (!ISTTYOPEN(tp)) {
4304 lwkt_reltoken(&tp->t_token);
4305 lwkt_reltoken(&vga_token);
4306 return;
4308 rmap = scp->sc->scr_rmap;
4309 for (; count > 0; --count)
4310 (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
4311 lwkt_reltoken(&tp->t_token);
4313 lwkt_reltoken(&vga_token);
4316 void
4317 sc_bell(scr_stat *scp, int pitch, int duration)
4319 if (cold || shutdown_in_progress)
4320 return;
4322 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL)) {
4323 return;
4326 if (scp->sc->flags & SC_VISUAL_BELL) {
4327 if (scp->sc->blink_in_progress) {
4328 return;
4330 scp->sc->blink_in_progress = 3;
4331 if (scp != scp->sc->cur_scp)
4332 scp->sc->blink_in_progress += 2;
4333 lockmgr(&syscons_blink_lk, LK_EXCLUSIVE);
4334 sc_blink_screen(scp->sc->cur_scp);
4335 lockmgr(&syscons_blink_lk, LK_RELEASE);
4336 } else if (duration != 0 && pitch != 0) {
4337 if (scp != scp->sc->cur_scp)
4338 pitch *= 2;
4339 sysbeep(pitch, duration);
4344 * Blink the screen, called with the syscons_mtx and syscons_blink_lk
4345 * held. Can be called directly or via the callout.
4347 static void
4348 sc_blink_screen(scr_stat *scp)
4350 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
4351 scp->sc->blink_in_progress = 0;
4352 mark_all(scp);
4353 if (scp->sc->delayed_next_scr)
4354 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
4355 } else {
4356 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
4357 scp->sc->blink_in_progress & 1);
4358 scp->sc->blink_in_progress--;
4359 callout_reset(&scp->blink_screen_ch, hz / 10,
4360 blink_screen_callout, scp);
4365 * Screen blink callout. Note that there is a lock order
4366 * reversal between syscons_blink_lk and the syscons lock.
4367 * Acquire the syscons lock non-blocking.
4369 static void
4370 blink_screen_callout(void *arg)
4372 scr_stat *scp = arg;
4374 if (syscons_lock_nonblock() == 0) {
4375 sc_blink_screen(scp);
4376 syscons_unlock();
4377 } else {
4378 callout_reset(&scp->blink_screen_ch, hz / 10,
4379 blink_screen_callout, scp);
4384 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
4385 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
4386 * any keyboard.
4389 static int
4390 sc_allocate_keyboard(sc_softc_t *sc, int unit)
4392 int idx0, idx;
4393 keyboard_t *k0, *k;
4394 keyboard_info_t ki;
4396 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
4397 if (idx0 != -1) {
4398 k0 = kbd_get_keyboard(idx0);
4400 for (idx = kbd_find_keyboard2("*", -1, 0, 0);
4401 idx != -1;
4402 idx = kbd_find_keyboard2("*", -1, idx + 1, 0)) {
4403 k = kbd_get_keyboard(idx);
4405 if (idx == idx0 || KBD_IS_BUSY(k))
4406 continue;
4408 bzero(&ki, sizeof(ki));
4409 strcpy(ki.kb_name, k->kb_name);
4410 ki.kb_unit = k->kb_unit;
4412 kbd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
4414 } else
4415 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);
4417 return (idx0);