1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992-1998 Søren Schmidt 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The DragonFly Project 8 * by Sascha Wildner <saw@online.de> 9 * 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. 21 * 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. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_compat.h" 38 #include "opt_syscons.h" 39 #include "opt_splash.h" 40 #include "opt_ddb.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/bus.h> 45 #include <sys/conf.h> 46 #include <sys/cons.h> 47 #include <sys/consio.h> 48 #include <sys/kdb.h> 49 #include <sys/eventhandler.h> 50 #include <sys/fbio.h> 51 #include <sys/kbio.h> 52 #include <sys/kernel.h> 53 #include <sys/lock.h> 54 #include <sys/malloc.h> 55 #include <sys/mutex.h> 56 #include <sys/pcpu.h> 57 #include <sys/priv.h> 58 #include <sys/proc.h> 59 #include <sys/random.h> 60 #include <sys/reboot.h> 61 #include <sys/serial.h> 62 #include <sys/signalvar.h> 63 #include <sys/smp.h> 64 #include <sys/sysctl.h> 65 #include <sys/tty.h> 66 #include <sys/power.h> 67 68 #include <machine/clock.h> 69 #if defined(__arm__) || defined(__mips__) || \ 70 defined(__powerpc__) || defined(__sparc64__) 71 #include <machine/sc_machdep.h> 72 #else 73 #include <machine/pc/display.h> 74 #endif 75 #if defined( __i386__) || defined(__amd64__) 76 #include <machine/psl.h> 77 #include <machine/frame.h> 78 #endif 79 #include <machine/stdarg.h> 80 81 #if defined(__amd64__) || defined(__i386__) 82 #include <machine/vmparam.h> 83 84 #include <vm/vm.h> 85 #include <vm/pmap.h> 86 #endif 87 88 #include <dev/kbd/kbdreg.h> 89 #include <dev/fb/fbreg.h> 90 #include <dev/fb/splashreg.h> 91 #include <dev/syscons/syscons.h> 92 93 #define COLD 0 94 #define WARM 1 95 96 #define DEFAULT_BLANKTIME (5*60) /* 5 minutes */ 97 #define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */ 98 99 #define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */ 100 101 /* NULL-safe version of "tty_opened()" */ 102 #define tty_opened_ns(tp) ((tp) != NULL && tty_opened(tp)) 103 104 static u_char sc_kattrtab[MAXCPU]; 105 106 static int sc_console_unit = -1; 107 static int sc_saver_keyb_only = 1; 108 static scr_stat *sc_console; 109 static struct consdev *sc_consptr; 110 static void *sc_kts[MAXCPU]; 111 static struct sc_term_sw *sc_ktsw; 112 static scr_stat main_console; 113 static struct tty *main_devs[MAXCONS]; 114 115 static char init_done = COLD; 116 static int shutdown_in_progress = FALSE; 117 static int suspend_in_progress = FALSE; 118 static char sc_malloc = FALSE; 119 120 static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */ 121 static int run_scrn_saver = FALSE; /* should run the saver? */ 122 static int enable_bell = TRUE; /* enable beeper */ 123 124 #ifndef SC_DISABLE_REBOOT 125 static int enable_reboot = TRUE; /* enable keyboard reboot */ 126 #endif 127 128 #ifndef SC_DISABLE_KDBKEY 129 static int enable_kdbkey = TRUE; /* enable keyboard debug */ 130 #endif 131 132 static long scrn_blank_time = 0; /* screen saver timeout value */ 133 #ifdef DEV_SPLASH 134 static int scrn_blanked; /* # of blanked screen */ 135 static int sticky_splash = FALSE; 136 137 static void none_saver(sc_softc_t *sc, int blank) { } 138 static void (*current_saver)(sc_softc_t *, int) = none_saver; 139 #endif 140 141 #ifdef SC_NO_SUSPEND_VTYSWITCH 142 static int sc_no_suspend_vtswitch = 1; 143 #else 144 static int sc_no_suspend_vtswitch = 0; 145 #endif 146 static int sc_susp_scr; 147 148 static SYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons"); 149 static SYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver"); 150 SYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW, 151 &sc_saver_keyb_only, 0, "screen saver interrupted by input only"); 152 SYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell, 153 0, "enable bell"); 154 #ifndef SC_DISABLE_REBOOT 155 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot, 156 0, "enable keyboard reboot"); 157 #endif 158 #ifndef SC_DISABLE_KDBKEY 159 SYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey, 160 0, "enable keyboard debug"); 161 #endif 162 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RWTUN, 163 &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend."); 164 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT) 165 #include "font.h" 166 #endif 167 168 tsw_ioctl_t *sc_user_ioctl; 169 170 static bios_values_t bios_value; 171 172 static int enable_panic_key; 173 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key, 174 0, "Enable panic via keypress specified in kbdmap(5)"); 175 176 #define SC_CONSOLECTL 255 177 178 #define VTY_WCHAN(sc, vty) (&SC_DEV(sc, vty)) 179 180 /* prototypes */ 181 static int sc_allocate_keyboard(sc_softc_t *sc, int unit); 182 static int scvidprobe(int unit, int flags, int cons); 183 static int sckbdprobe(int unit, int flags, int cons); 184 static void scmeminit(void *arg); 185 static int scdevtounit(struct tty *tp); 186 static kbd_callback_func_t sckbdevent; 187 static void scinit(int unit, int flags); 188 static scr_stat *sc_get_stat(struct tty *tp); 189 static void scterm(int unit, int flags); 190 static void scshutdown(void *, int); 191 static void scsuspend(void *); 192 static void scresume(void *); 193 static u_int scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp); 194 static void sc_puts(scr_stat *scp, u_char *buf, int len); 195 #define SCGETC_CN 1 196 #define SCGETC_NONBLOCK 2 197 static void sccnupdate(scr_stat *scp); 198 static scr_stat *alloc_scp(sc_softc_t *sc, int vty); 199 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp); 200 static timeout_t scrn_timer; 201 static int and_region(int *s1, int *e1, int s2, int e2); 202 static void scrn_update(scr_stat *scp, int show_cursor); 203 204 #ifdef DEV_SPLASH 205 static int scsplash_callback(int event, void *arg); 206 static void scsplash_saver(sc_softc_t *sc, int show); 207 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int)); 208 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int)); 209 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border); 210 static int restore_scrn_saver_mode(scr_stat *scp, int changemode); 211 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int)); 212 static int wait_scrn_saver_stop(sc_softc_t *sc); 213 #define scsplash_stick(stick) (sticky_splash = (stick)) 214 #else /* !DEV_SPLASH */ 215 #define scsplash_stick(stick) 216 #endif /* DEV_SPLASH */ 217 218 static int do_switch_scr(sc_softc_t *sc, int s); 219 static int vt_proc_alive(scr_stat *scp); 220 static int signal_vt_rel(scr_stat *scp); 221 static int signal_vt_acq(scr_stat *scp); 222 static int finish_vt_rel(scr_stat *scp, int release, int *s); 223 static int finish_vt_acq(scr_stat *scp); 224 static void exchange_scr(sc_softc_t *sc); 225 static void update_cursor_image(scr_stat *scp); 226 static void change_cursor_shape(scr_stat *scp, int flags, int base, int height); 227 static void update_font(scr_stat *); 228 static int save_kbd_state(scr_stat *scp); 229 static int update_kbd_state(scr_stat *scp, int state, int mask); 230 static int update_kbd_leds(scr_stat *scp, int which); 231 static int sc_kattr(void); 232 static timeout_t blink_screen; 233 static struct tty *sc_alloc_tty(int, int); 234 235 static cn_probe_t sc_cnprobe; 236 static cn_init_t sc_cninit; 237 static cn_term_t sc_cnterm; 238 static cn_getc_t sc_cngetc; 239 static cn_putc_t sc_cnputc; 240 static cn_grab_t sc_cngrab; 241 static cn_ungrab_t sc_cnungrab; 242 243 CONSOLE_DRIVER(sc); 244 245 static tsw_open_t sctty_open; 246 static tsw_close_t sctty_close; 247 static tsw_outwakeup_t sctty_outwakeup; 248 static tsw_ioctl_t sctty_ioctl; 249 static tsw_mmap_t sctty_mmap; 250 251 static struct ttydevsw sc_ttydevsw = { 252 .tsw_open = sctty_open, 253 .tsw_close = sctty_close, 254 .tsw_outwakeup = sctty_outwakeup, 255 .tsw_ioctl = sctty_ioctl, 256 .tsw_mmap = sctty_mmap, 257 }; 258 259 static d_ioctl_t consolectl_ioctl; 260 static d_close_t consolectl_close; 261 262 static struct cdevsw consolectl_devsw = { 263 .d_version = D_VERSION, 264 .d_flags = D_NEEDGIANT | D_TRACKCLOSE, 265 .d_ioctl = consolectl_ioctl, 266 .d_close = consolectl_close, 267 .d_name = "consolectl", 268 }; 269 270 /* ec -- emergency console. */ 271 272 static u_int ec_scroffset; 273 274 static void 275 ec_putc(int c) 276 { 277 uintptr_t fb; 278 u_short *scrptr; 279 u_int ind; 280 int attr, column, mysize, width, xsize, yborder, ysize; 281 282 if (c < 0 || c > 0xff || c == '\a') 283 return; 284 if (sc_console == NULL) { 285 #if !defined(__amd64__) && !defined(__i386__) 286 return; 287 #else 288 /* 289 * This is enough for ec_putc() to work very early on x86 290 * if the kernel starts in normal color text mode. 291 */ 292 fb = KERNBASE + 0xb8000; 293 xsize = 80; 294 ysize = 25; 295 #endif 296 } else { 297 if (!ISTEXTSC(&main_console)) 298 return; 299 fb = main_console.sc->adp->va_window; 300 xsize = main_console.xsize; 301 ysize = main_console.ysize; 302 } 303 yborder = ysize / 5; 304 scrptr = (u_short *)(void *)fb + xsize * yborder; 305 mysize = xsize * (ysize - 2 * yborder); 306 do { 307 ind = ec_scroffset; 308 column = ind % xsize; 309 width = (c == '\b' ? -1 : c == '\t' ? (column + 8) & ~7 : 310 c == '\r' ? -column : c == '\n' ? xsize - column : 1); 311 if (width == 0 || (width < 0 && ind < -width)) 312 return; 313 } while (atomic_cmpset_rel_int(&ec_scroffset, ind, ind + width) == 0); 314 if (c == '\b' || c == '\r') 315 return; 316 if (c == '\n') 317 ind += xsize; /* XXX clearing from new pos is not atomic */ 318 319 attr = sc_kattr(); 320 if (c == '\t' || c == '\n') 321 c = ' '; 322 do 323 scrptr[ind++ % mysize] = (attr << 8) | c; 324 while (--width != 0); 325 } 326 327 int 328 sc_probe_unit(int unit, int flags) 329 { 330 if (!vty_enabled(VTY_SC)) 331 return ENXIO; 332 if (!scvidprobe(unit, flags, FALSE)) { 333 if (bootverbose) 334 printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit); 335 return ENXIO; 336 } 337 338 /* syscons will be attached even when there is no keyboard */ 339 sckbdprobe(unit, flags, FALSE); 340 341 return 0; 342 } 343 344 /* probe video adapters, return TRUE if found */ 345 static int 346 scvidprobe(int unit, int flags, int cons) 347 { 348 /* 349 * Access the video adapter driver through the back door! 350 * Video adapter drivers need to be configured before syscons. 351 * However, when syscons is being probed as the low-level console, 352 * they have not been initialized yet. We force them to initialize 353 * themselves here. XXX 354 */ 355 vid_configure(cons ? VIO_PROBE_ONLY : 0); 356 357 return (vid_find_adapter("*", unit) >= 0); 358 } 359 360 /* probe the keyboard, return TRUE if found */ 361 static int 362 sckbdprobe(int unit, int flags, int cons) 363 { 364 /* access the keyboard driver through the backdoor! */ 365 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0); 366 367 return (kbd_find_keyboard("*", unit) >= 0); 368 } 369 370 static char 371 *adapter_name(video_adapter_t *adp) 372 { 373 static struct { 374 int type; 375 char *name[2]; 376 } names[] = { 377 { KD_MONO, { "MDA", "MDA" } }, 378 { KD_HERCULES, { "Hercules", "Hercules" } }, 379 { KD_CGA, { "CGA", "CGA" } }, 380 { KD_EGA, { "EGA", "EGA (mono)" } }, 381 { KD_VGA, { "VGA", "VGA (mono)" } }, 382 { KD_TGA, { "TGA", "TGA" } }, 383 { -1, { "Unknown", "Unknown" } }, 384 }; 385 int i; 386 387 for (i = 0; names[i].type != -1; ++i) 388 if (names[i].type == adp->va_type) 389 break; 390 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1]; 391 } 392 393 static void 394 sctty_outwakeup(struct tty *tp) 395 { 396 size_t len; 397 u_char buf[PCBURST]; 398 scr_stat *scp = sc_get_stat(tp); 399 400 if (scp->status & SLKED || 401 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress)) 402 return; 403 404 for (;;) { 405 len = ttydisc_getc(tp, buf, sizeof buf); 406 if (len == 0) 407 break; 408 SC_VIDEO_LOCK(scp->sc); 409 sc_puts(scp, buf, len); 410 SC_VIDEO_UNLOCK(scp->sc); 411 } 412 } 413 414 static struct tty * 415 sc_alloc_tty(int index, int devnum) 416 { 417 struct sc_ttysoftc *stc; 418 struct tty *tp; 419 420 /* Allocate TTY object and softc to store unit number. */ 421 stc = malloc(sizeof(struct sc_ttysoftc), M_DEVBUF, M_WAITOK); 422 stc->st_index = index; 423 stc->st_stat = NULL; 424 tp = tty_alloc_mutex(&sc_ttydevsw, stc, &Giant); 425 426 /* Create device node. */ 427 tty_makedev(tp, NULL, "v%r", devnum); 428 429 return (tp); 430 } 431 432 #ifdef SC_PIXEL_MODE 433 static void 434 sc_set_vesa_mode(scr_stat *scp, sc_softc_t *sc, int unit) 435 { 436 video_info_t info; 437 u_char *font; 438 int depth; 439 int fontsize; 440 int i; 441 int vmode; 442 443 vmode = 0; 444 (void)resource_int_value("sc", unit, "vesa_mode", &vmode); 445 if (vmode < M_VESA_BASE || vmode > M_VESA_MODE_MAX || 446 vidd_get_info(sc->adp, vmode, &info) != 0 || 447 !sc_support_pixel_mode(&info)) 448 vmode = 0; 449 450 /* 451 * If the mode is unset or unsupported, search for an available 452 * 800x600 graphics mode with the highest color depth. 453 */ 454 if (vmode == 0) { 455 for (depth = 0, i = M_VESA_BASE; i <= M_VESA_MODE_MAX; i++) 456 if (vidd_get_info(sc->adp, i, &info) == 0 && 457 info.vi_width == 800 && info.vi_height == 600 && 458 sc_support_pixel_mode(&info) && 459 info.vi_depth > depth) { 460 vmode = i; 461 depth = info.vi_depth; 462 } 463 if (vmode == 0) 464 return; 465 vidd_get_info(sc->adp, vmode, &info); 466 } 467 468 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT) 469 fontsize = info.vi_cheight; 470 #else 471 fontsize = scp->font_size; 472 #endif 473 if (fontsize < 14) 474 fontsize = 8; 475 else if (fontsize >= 16) 476 fontsize = 16; 477 else 478 fontsize = 14; 479 #ifndef SC_NO_FONT_LOADING 480 switch (fontsize) { 481 case 8: 482 if ((sc->fonts_loaded & FONT_8) == 0) 483 return; 484 font = sc->font_8; 485 break; 486 case 14: 487 if ((sc->fonts_loaded & FONT_14) == 0) 488 return; 489 font = sc->font_14; 490 break; 491 case 16: 492 if ((sc->fonts_loaded & FONT_16) == 0) 493 return; 494 font = sc->font_16; 495 break; 496 } 497 #else 498 font = NULL; 499 #endif 500 #ifdef DEV_SPLASH 501 if ((sc->flags & SC_SPLASH_SCRN) != 0) 502 splash_term(sc->adp); 503 #endif 504 #ifndef SC_NO_HISTORY 505 if (scp->history != NULL) { 506 sc_vtb_append(&scp->vtb, 0, scp->history, 507 scp->ypos * scp->xsize + scp->xpos); 508 scp->history_pos = sc_vtb_tail(scp->history); 509 } 510 #endif 511 vidd_set_mode(sc->adp, vmode); 512 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN); 513 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE); 514 scp->xpixel = info.vi_width; 515 scp->ypixel = info.vi_height; 516 scp->xsize = scp->xpixel / 8; 517 scp->ysize = scp->ypixel / fontsize; 518 scp->xpos = 0; 519 scp->ypos = scp->ysize - 1; 520 scp->xoff = scp->yoff = 0; 521 scp->font = font; 522 scp->font_size = fontsize; 523 scp->font_width = 8; 524 scp->start = scp->xsize * scp->ysize - 1; 525 scp->end = 0; 526 scp->cursor_pos = scp->cursor_oldpos = scp->xsize * scp->xsize; 527 scp->mode = sc->initial_mode = vmode; 528 #ifndef __sparc64__ 529 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, 530 (void *)sc->adp->va_window, FALSE); 531 #endif 532 sc_alloc_scr_buffer(scp, FALSE, FALSE); 533 sc_init_emulator(scp, NULL); 534 #ifndef SC_NO_CUTPASTE 535 sc_alloc_cut_buffer(scp, FALSE); 536 #endif 537 #ifndef SC_NO_HISTORY 538 sc_alloc_history_buffer(scp, 0, 0, FALSE); 539 #endif 540 sc_set_border(scp, scp->border); 541 sc_set_cursor_image(scp); 542 scp->status &= ~UNKNOWN_MODE; 543 #ifdef DEV_SPLASH 544 if ((sc->flags & SC_SPLASH_SCRN) != 0) 545 splash_init(sc->adp, scsplash_callback, sc); 546 #endif 547 } 548 #endif 549 550 int 551 sc_attach_unit(int unit, int flags) 552 { 553 sc_softc_t *sc; 554 scr_stat *scp; 555 struct cdev *dev; 556 void *oldts, *ts; 557 int i, vc; 558 559 if (!vty_enabled(VTY_SC)) 560 return ENXIO; 561 562 flags &= ~SC_KERNEL_CONSOLE; 563 564 if (sc_console_unit == unit) { 565 /* 566 * If this unit is being used as the system console, we need to 567 * adjust some variables and buffers before and after scinit(). 568 */ 569 /* assert(sc_console != NULL) */ 570 flags |= SC_KERNEL_CONSOLE; 571 scmeminit(NULL); 572 573 scinit(unit, flags); 574 575 if (sc_console->tsw->te_size > 0) { 576 sc_ktsw = sc_console->tsw; 577 /* assert(sc_console->ts != NULL); */ 578 oldts = sc_console->ts; 579 for (i = 0; i <= mp_maxid; i++) { 580 ts = malloc(sc_console->tsw->te_size, M_DEVBUF, 581 M_WAITOK | M_ZERO); 582 (*sc_console->tsw->te_init)(sc_console, &ts, SC_TE_COLD_INIT); 583 sc_console->ts = ts; 584 (*sc_console->tsw->te_default_attr)(sc_console, sc_kattrtab[i], 585 SC_KERNEL_CONS_REV_ATTR); 586 sc_kts[i] = ts; 587 } 588 sc_console->ts = oldts; 589 (*sc_console->tsw->te_default_attr)(sc_console, SC_NORM_ATTR, 590 SC_NORM_REV_ATTR); 591 } 592 } else { 593 scinit(unit, flags); 594 } 595 596 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE); 597 sc->config = flags; 598 callout_init(&sc->ctimeout, 0); 599 callout_init(&sc->cblink, 0); 600 scp = sc_get_stat(sc->dev[0]); 601 if (sc_console == NULL) /* sc_console_unit < 0 */ 602 sc_console = scp; 603 604 #ifdef SC_PIXEL_MODE 605 if ((sc->config & SC_VESAMODE) != 0) 606 sc_set_vesa_mode(scp, sc, unit); 607 #endif /* SC_PIXEL_MODE */ 608 609 /* initialize cursor */ 610 if (!ISGRAPHSC(scp)) 611 update_cursor_image(scp); 612 613 /* get screen update going */ 614 scrn_timer(sc); 615 616 /* set up the keyboard */ 617 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); 618 update_kbd_state(scp, scp->status, LOCK_MASK); 619 620 printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n", 621 SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config); 622 if (bootverbose) { 623 printf("%s%d:", SC_DRIVER_NAME, unit); 624 if (sc->adapter >= 0) 625 printf(" fb%d", sc->adapter); 626 if (sc->keyboard >= 0) 627 printf(", kbd%d", sc->keyboard); 628 if (scp->tsw) 629 printf(", terminal emulator: %s (%s)", 630 scp->tsw->te_name, scp->tsw->te_desc); 631 printf("\n"); 632 } 633 634 /* Register suspend/resume/shutdown callbacks for the kernel console. */ 635 if (sc_console_unit == unit) { 636 EVENTHANDLER_REGISTER(power_suspend_early, scsuspend, NULL, 637 EVENTHANDLER_PRI_ANY); 638 EVENTHANDLER_REGISTER(power_resume, scresume, NULL, 639 EVENTHANDLER_PRI_ANY); 640 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, NULL, 641 SHUTDOWN_PRI_DEFAULT); 642 } 643 644 for (vc = 0; vc < sc->vtys; vc++) { 645 if (sc->dev[vc] == NULL) { 646 sc->dev[vc] = sc_alloc_tty(vc, vc + unit * MAXCONS); 647 if (vc == 0 && sc->dev == main_devs) 648 SC_STAT(sc->dev[0]) = &main_console; 649 } 650 /* 651 * The first vty already has struct tty and scr_stat initialized 652 * in scinit(). The other vtys will have these structs when 653 * first opened. 654 */ 655 } 656 657 dev = make_dev(&consolectl_devsw, 0, UID_ROOT, GID_WHEEL, 0600, 658 "consolectl"); 659 dev->si_drv1 = sc->dev[0]; 660 661 return 0; 662 } 663 664 static void 665 scmeminit(void *arg) 666 { 667 if (!vty_enabled(VTY_SC)) 668 return; 669 if (sc_malloc) 670 return; 671 sc_malloc = TRUE; 672 673 /* 674 * As soon as malloc() becomes functional, we had better allocate 675 * various buffers for the kernel console. 676 */ 677 678 if (sc_console_unit < 0) /* sc_console == NULL */ 679 return; 680 681 /* copy the temporary buffer to the final buffer */ 682 sc_alloc_scr_buffer(sc_console, FALSE, FALSE); 683 684 #ifndef SC_NO_CUTPASTE 685 sc_alloc_cut_buffer(sc_console, FALSE); 686 #endif 687 688 #ifndef SC_NO_HISTORY 689 /* initialize history buffer & pointers */ 690 sc_alloc_history_buffer(sc_console, 0, 0, FALSE); 691 #endif 692 } 693 694 /* XXX */ 695 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL); 696 697 static int 698 scdevtounit(struct tty *tp) 699 { 700 int vty = SC_VTY(tp); 701 702 if (vty == SC_CONSOLECTL) 703 return ((sc_console != NULL) ? sc_console->sc->unit : -1); 704 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit())) 705 return -1; 706 else 707 return vty/MAXCONS; 708 } 709 710 static int 711 sctty_open(struct tty *tp) 712 { 713 int unit = scdevtounit(tp); 714 sc_softc_t *sc; 715 scr_stat *scp; 716 #ifndef __sparc64__ 717 keyarg_t key; 718 #endif 719 720 DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n", 721 devtoname(tp->t_dev), unit, SC_VTY(tp))); 722 723 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0); 724 if (sc == NULL) 725 return ENXIO; 726 727 if (!tty_opened(tp)) { 728 /* Use the current setting of the <-- key as default VERASE. */ 729 /* If the Delete key is preferable, an stty is necessary */ 730 #ifndef __sparc64__ 731 if (sc->kbd != NULL) { 732 key.keynum = KEYCODE_BS; 733 (void)kbdd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key); 734 tp->t_termios.c_cc[VERASE] = key.key.map[0]; 735 } 736 #endif 737 } 738 739 scp = sc_get_stat(tp); 740 if (scp == NULL) { 741 scp = SC_STAT(tp) = alloc_scp(sc, SC_VTY(tp)); 742 if (ISGRAPHSC(scp)) 743 sc_set_pixel_mode(scp, NULL, 0, 0, 16, 8); 744 } 745 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) { 746 tp->t_winsize.ws_col = scp->xsize; 747 tp->t_winsize.ws_row = scp->ysize; 748 } 749 750 return (0); 751 } 752 753 static void 754 sctty_close(struct tty *tp) 755 { 756 scr_stat *scp; 757 int s; 758 759 if (SC_VTY(tp) != SC_CONSOLECTL) { 760 scp = sc_get_stat(tp); 761 /* were we in the middle of the VT switching process? */ 762 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit)); 763 s = spltty(); 764 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit)) 765 cnavailable(sc_consptr, TRUE); 766 if (finish_vt_rel(scp, TRUE, &s) == 0) /* force release */ 767 DPRINTF(5, ("reset WAIT_REL, ")); 768 if (finish_vt_acq(scp) == 0) /* force acknowledge */ 769 DPRINTF(5, ("reset WAIT_ACQ, ")); 770 #ifdef not_yet_done 771 if (scp == &main_console) { 772 scp->pid = 0; 773 scp->proc = NULL; 774 scp->smode.mode = VT_AUTO; 775 } 776 else { 777 sc_vtb_destroy(&scp->vtb); 778 #ifndef __sparc64__ 779 sc_vtb_destroy(&scp->scr); 780 #endif 781 sc_free_history_buffer(scp, scp->ysize); 782 SC_STAT(tp) = NULL; 783 free(scp, M_DEVBUF); 784 } 785 #else 786 scp->pid = 0; 787 scp->proc = NULL; 788 scp->smode.mode = VT_AUTO; 789 #endif 790 scp->kbd_mode = K_XLATE; 791 if (scp == scp->sc->cur_scp) 792 (void)kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); 793 DPRINTF(5, ("done.\n")); 794 } 795 } 796 797 #if 0 /* XXX mpsafetty: fix screensaver. What about outwakeup? */ 798 static int 799 scread(struct cdev *dev, struct uio *uio, int flag) 800 { 801 if (!sc_saver_keyb_only) 802 sc_touch_scrn_saver(); 803 return ttyread(dev, uio, flag); 804 } 805 #endif 806 807 static int 808 sckbdevent(keyboard_t *thiskbd, int event, void *arg) 809 { 810 sc_softc_t *sc; 811 struct tty *cur_tty; 812 int c, error = 0; 813 size_t len; 814 const u_char *cp; 815 816 sc = (sc_softc_t *)arg; 817 /* assert(thiskbd == sc->kbd) */ 818 819 mtx_lock(&Giant); 820 821 switch (event) { 822 case KBDIO_KEYINPUT: 823 break; 824 case KBDIO_UNLOADING: 825 sc->kbd = NULL; 826 sc->keyboard = -1; 827 kbd_release(thiskbd, (void *)&sc->keyboard); 828 goto done; 829 default: 830 error = EINVAL; 831 goto done; 832 } 833 834 /* 835 * Loop while there is still input to get from the keyboard. 836 * I don't think this is nessesary, and it doesn't fix 837 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX 838 */ 839 while ((c = scgetc(sc, SCGETC_NONBLOCK, NULL)) != NOKEY) { 840 841 cur_tty = SC_DEV(sc, sc->cur_scp->index); 842 if (!tty_opened_ns(cur_tty)) 843 continue; 844 845 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty)) 846 continue; 847 848 switch (KEYFLAGS(c)) { 849 case 0x0000: /* normal key */ 850 ttydisc_rint(cur_tty, KEYCHAR(c), 0); 851 break; 852 case FKEY: /* function key, return string */ 853 cp = (*sc->cur_scp->tsw->te_fkeystr)(sc->cur_scp, c); 854 if (cp != NULL) { 855 ttydisc_rint_simple(cur_tty, cp, strlen(cp)); 856 break; 857 } 858 cp = kbdd_get_fkeystr(thiskbd, KEYCHAR(c), &len); 859 if (cp != NULL) 860 ttydisc_rint_simple(cur_tty, cp, len); 861 break; 862 case MKEY: /* meta is active, prepend ESC */ 863 ttydisc_rint(cur_tty, 0x1b, 0); 864 ttydisc_rint(cur_tty, KEYCHAR(c), 0); 865 break; 866 case BKEY: /* backtab fixed sequence (esc [ Z) */ 867 ttydisc_rint_simple(cur_tty, "\x1B[Z", 3); 868 break; 869 } 870 871 ttydisc_rint_done(cur_tty); 872 } 873 874 sc->cur_scp->status |= MOUSE_HIDDEN; 875 876 done: 877 mtx_unlock(&Giant); 878 return (error); 879 } 880 881 static int 882 sctty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 883 { 884 int error; 885 int i; 886 struct cursor_attr *cap; 887 sc_softc_t *sc; 888 scr_stat *scp; 889 int s; 890 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 891 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 892 int ival; 893 #endif 894 895 /* If there is a user_ioctl function call that first */ 896 if (sc_user_ioctl) { 897 error = (*sc_user_ioctl)(tp, cmd, data, td); 898 if (error != ENOIOCTL) 899 return error; 900 } 901 902 error = sc_vid_ioctl(tp, cmd, data, td); 903 if (error != ENOIOCTL) 904 return error; 905 906 #ifndef SC_NO_HISTORY 907 error = sc_hist_ioctl(tp, cmd, data, td); 908 if (error != ENOIOCTL) 909 return error; 910 #endif 911 912 #ifndef SC_NO_SYSMOUSE 913 error = sc_mouse_ioctl(tp, cmd, data, td); 914 if (error != ENOIOCTL) 915 return error; 916 #endif 917 918 scp = sc_get_stat(tp); 919 /* assert(scp != NULL) */ 920 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */ 921 sc = scp->sc; 922 923 if (scp->tsw) { 924 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, td); 925 if (error != ENOIOCTL) 926 return error; 927 } 928 929 switch (cmd) { /* process console hardware related ioctl's */ 930 931 case GIO_ATTR: /* get current attributes */ 932 /* this ioctl is not processed here, but in the terminal emulator */ 933 return ENOTTY; 934 935 case GIO_COLOR: /* is this a color console ? */ 936 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0; 937 return 0; 938 939 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */ 940 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME) 941 return EINVAL; 942 s = spltty(); 943 scrn_blank_time = *(int *)data; 944 run_scrn_saver = (scrn_blank_time != 0); 945 splx(s); 946 return 0; 947 948 case CONS_CURSORTYPE: /* set cursor type (old interface + HIDDEN) */ 949 s = spltty(); 950 *(int *)data &= CONS_CURSOR_ATTRS; 951 sc_change_cursor_shape(scp, *(int *)data, -1, -1); 952 splx(s); 953 return 0; 954 955 case CONS_GETCURSORSHAPE: /* get cursor shape (new interface) */ 956 switch (((int *)data)[0] & (CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR)) { 957 case 0: 958 cap = &sc->curs_attr; 959 break; 960 case CONS_LOCAL_CURSOR: 961 cap = &scp->base_curs_attr; 962 break; 963 case CONS_DEFAULT_CURSOR: 964 cap = &sc->dflt_curs_attr; 965 break; 966 case CONS_DEFAULT_CURSOR | CONS_LOCAL_CURSOR: 967 cap = &scp->dflt_curs_attr; 968 break; 969 } 970 if (((int *)data)[0] & CONS_CHARCURSOR_COLORS) { 971 ((int *)data)[1] = cap->bg[0]; 972 ((int *)data)[2] = cap->bg[1]; 973 } else if (((int *)data)[0] & CONS_MOUSECURSOR_COLORS) { 974 ((int *)data)[1] = cap->mouse_ba; 975 ((int *)data)[2] = cap->mouse_ia; 976 } else { 977 ((int *)data)[1] = cap->base; 978 ((int *)data)[2] = cap->height; 979 } 980 ((int *)data)[0] = cap->flags; 981 return 0; 982 983 case CONS_SETCURSORSHAPE: /* set cursor shape (new interface) */ 984 s = spltty(); 985 sc_change_cursor_shape(scp, ((int *)data)[0], 986 ((int *)data)[1], ((int *)data)[2]); 987 splx(s); 988 return 0; 989 990 case CONS_BELLTYPE: /* set bell type sound/visual */ 991 if ((*(int *)data) & CONS_VISUAL_BELL) 992 sc->flags |= SC_VISUAL_BELL; 993 else 994 sc->flags &= ~SC_VISUAL_BELL; 995 if ((*(int *)data) & CONS_QUIET_BELL) 996 sc->flags |= SC_QUIET_BELL; 997 else 998 sc->flags &= ~SC_QUIET_BELL; 999 return 0; 1000 1001 case CONS_GETINFO: /* get current (virtual) console info */ 1002 { 1003 vid_info_t *ptr = (vid_info_t*)data; 1004 if (ptr->size == sizeof(struct vid_info)) { 1005 ptr->m_num = sc->cur_scp->index; 1006 ptr->font_size = scp->font_size; 1007 ptr->mv_col = scp->xpos; 1008 ptr->mv_row = scp->ypos; 1009 ptr->mv_csz = scp->xsize; 1010 ptr->mv_rsz = scp->ysize; 1011 ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0; 1012 /* 1013 * The following fields are filled by the terminal emulator. XXX 1014 * 1015 * ptr->mv_norm.fore 1016 * ptr->mv_norm.back 1017 * ptr->mv_rev.fore 1018 * ptr->mv_rev.back 1019 */ 1020 ptr->mv_grfc.fore = 0; /* not supported */ 1021 ptr->mv_grfc.back = 0; /* not supported */ 1022 ptr->mv_ovscan = scp->border; 1023 if (scp == sc->cur_scp) 1024 save_kbd_state(scp); 1025 ptr->mk_keylock = scp->status & LOCK_MASK; 1026 return 0; 1027 } 1028 return EINVAL; 1029 } 1030 1031 case CONS_GETVERS: /* get version number */ 1032 *(int*)data = 0x200; /* version 2.0 */ 1033 return 0; 1034 1035 case CONS_IDLE: /* see if the screen has been idle */ 1036 /* 1037 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE, 1038 * the user process may have been writing something on the 1039 * screen and syscons is not aware of it. Declare the screen 1040 * is NOT idle if it is in one of these modes. But there is 1041 * an exception to it; if a screen saver is running in the 1042 * graphics mode in the current screen, we should say that the 1043 * screen has been idle. 1044 */ 1045 *(int *)data = (sc->flags & SC_SCRN_IDLE) 1046 && (!ISGRAPHSC(sc->cur_scp) 1047 || (sc->cur_scp->status & SAVER_RUNNING)); 1048 return 0; 1049 1050 case CONS_SAVERMODE: /* set saver mode */ 1051 switch(*(int *)data) { 1052 case CONS_NO_SAVER: 1053 case CONS_USR_SAVER: 1054 /* if a LKM screen saver is running, stop it first. */ 1055 scsplash_stick(FALSE); 1056 saver_mode = *(int *)data; 1057 s = spltty(); 1058 #ifdef DEV_SPLASH 1059 if ((error = wait_scrn_saver_stop(NULL))) { 1060 splx(s); 1061 return error; 1062 } 1063 #endif 1064 run_scrn_saver = TRUE; 1065 if (saver_mode == CONS_USR_SAVER) 1066 scp->status |= SAVER_RUNNING; 1067 else 1068 scp->status &= ~SAVER_RUNNING; 1069 scsplash_stick(TRUE); 1070 splx(s); 1071 break; 1072 case CONS_LKM_SAVER: 1073 s = spltty(); 1074 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING)) 1075 scp->status &= ~SAVER_RUNNING; 1076 saver_mode = *(int *)data; 1077 splx(s); 1078 break; 1079 default: 1080 return EINVAL; 1081 } 1082 return 0; 1083 1084 case CONS_SAVERSTART: /* immediately start/stop the screen saver */ 1085 /* 1086 * Note that this ioctl does not guarantee the screen saver 1087 * actually starts or stops. It merely attempts to do so... 1088 */ 1089 s = spltty(); 1090 run_scrn_saver = (*(int *)data != 0); 1091 if (run_scrn_saver) 1092 sc->scrn_time_stamp -= scrn_blank_time; 1093 splx(s); 1094 return 0; 1095 1096 case CONS_SCRSHOT: /* get a screen shot */ 1097 { 1098 int retval, hist_rsz; 1099 size_t lsize, csize; 1100 vm_offset_t frbp, hstp; 1101 unsigned lnum; 1102 scrshot_t *ptr = (scrshot_t *)data; 1103 void *outp = ptr->buf; 1104 1105 if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0) 1106 return EINVAL; 1107 s = spltty(); 1108 if (ISGRAPHSC(scp)) { 1109 splx(s); 1110 return EOPNOTSUPP; 1111 } 1112 hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0; 1113 if (((u_int)ptr->x + ptr->xsize) > scp->xsize || 1114 ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) { 1115 splx(s); 1116 return EINVAL; 1117 } 1118 1119 lsize = scp->xsize * sizeof(u_int16_t); 1120 csize = ptr->xsize * sizeof(u_int16_t); 1121 /* Pointer to the last line of framebuffer */ 1122 frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x * 1123 sizeof(u_int16_t); 1124 /* Pointer to the last line of target buffer */ 1125 outp = (char *)outp + ptr->ysize * csize; 1126 /* Pointer to the last line of history buffer */ 1127 if (scp->history != NULL) 1128 hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) * 1129 sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t); 1130 else 1131 hstp = 0; 1132 1133 retval = 0; 1134 for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) { 1135 if (lnum < scp->ysize) { 1136 frbp -= lsize; 1137 } else { 1138 hstp -= lsize; 1139 if (hstp < scp->history->vtb_buffer) 1140 hstp += scp->history->vtb_rows * lsize; 1141 frbp = hstp; 1142 } 1143 if (lnum < ptr->y) 1144 continue; 1145 outp = (char *)outp - csize; 1146 retval = copyout((void *)frbp, outp, csize); 1147 if (retval != 0) 1148 break; 1149 } 1150 splx(s); 1151 return retval; 1152 } 1153 1154 case VT_SETMODE: /* set screen switcher mode */ 1155 { 1156 struct vt_mode *mode; 1157 struct proc *p1; 1158 1159 mode = (struct vt_mode *)data; 1160 DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit)); 1161 if (scp->smode.mode == VT_PROCESS) { 1162 p1 = pfind(scp->pid); 1163 if (scp->proc == p1 && scp->proc != td->td_proc) { 1164 if (p1) 1165 PROC_UNLOCK(p1); 1166 DPRINTF(5, ("error EPERM\n")); 1167 return EPERM; 1168 } 1169 if (p1) 1170 PROC_UNLOCK(p1); 1171 } 1172 s = spltty(); 1173 if (mode->mode == VT_AUTO) { 1174 scp->smode.mode = VT_AUTO; 1175 scp->proc = NULL; 1176 scp->pid = 0; 1177 DPRINTF(5, ("VT_AUTO, ")); 1178 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit)) 1179 cnavailable(sc_consptr, TRUE); 1180 /* were we in the middle of the vty switching process? */ 1181 if (finish_vt_rel(scp, TRUE, &s) == 0) 1182 DPRINTF(5, ("reset WAIT_REL, ")); 1183 if (finish_vt_acq(scp) == 0) 1184 DPRINTF(5, ("reset WAIT_ACQ, ")); 1185 } else { 1186 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig) 1187 || !ISSIGVALID(mode->frsig)) { 1188 splx(s); 1189 DPRINTF(5, ("error EINVAL\n")); 1190 return EINVAL; 1191 } 1192 DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid)); 1193 bcopy(data, &scp->smode, sizeof(struct vt_mode)); 1194 scp->proc = td->td_proc; 1195 scp->pid = scp->proc->p_pid; 1196 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit)) 1197 cnavailable(sc_consptr, FALSE); 1198 } 1199 splx(s); 1200 DPRINTF(5, ("\n")); 1201 return 0; 1202 } 1203 1204 case VT_GETMODE: /* get screen switcher mode */ 1205 bcopy(&scp->smode, data, sizeof(struct vt_mode)); 1206 return 0; 1207 1208 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1209 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1210 case _IO('v', 4): 1211 ival = IOCPARM_IVAL(data); 1212 data = (caddr_t)&ival; 1213 /* FALLTHROUGH */ 1214 #endif 1215 case VT_RELDISP: /* screen switcher ioctl */ 1216 s = spltty(); 1217 /* 1218 * This must be the current vty which is in the VT_PROCESS 1219 * switching mode... 1220 */ 1221 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) { 1222 splx(s); 1223 return EINVAL; 1224 } 1225 /* ...and this process is controlling it. */ 1226 if (scp->proc != td->td_proc) { 1227 splx(s); 1228 return EPERM; 1229 } 1230 error = EINVAL; 1231 switch(*(int *)data) { 1232 case VT_FALSE: /* user refuses to release screen, abort */ 1233 if ((error = finish_vt_rel(scp, FALSE, &s)) == 0) 1234 DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit)); 1235 break; 1236 case VT_TRUE: /* user has released screen, go on */ 1237 if ((error = finish_vt_rel(scp, TRUE, &s)) == 0) 1238 DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit)); 1239 break; 1240 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 1241 if ((error = finish_vt_acq(scp)) == 0) 1242 DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit)); 1243 break; 1244 default: 1245 break; 1246 } 1247 splx(s); 1248 return error; 1249 1250 case VT_OPENQRY: /* return free virtual console */ 1251 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) { 1252 tp = SC_DEV(sc, i); 1253 if (!tty_opened_ns(tp)) { 1254 *(int *)data = i + 1; 1255 return 0; 1256 } 1257 } 1258 return EINVAL; 1259 1260 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1261 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1262 case _IO('v', 5): 1263 ival = IOCPARM_IVAL(data); 1264 data = (caddr_t)&ival; 1265 /* FALLTHROUGH */ 1266 #endif 1267 case VT_ACTIVATE: /* switch to screen *data */ 1268 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1); 1269 s = spltty(); 1270 error = sc_clean_up(sc->cur_scp); 1271 splx(s); 1272 if (error) 1273 return error; 1274 error = sc_switch_scr(sc, i); 1275 return (error); 1276 1277 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1278 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1279 case _IO('v', 6): 1280 ival = IOCPARM_IVAL(data); 1281 data = (caddr_t)&ival; 1282 /* FALLTHROUGH */ 1283 #endif 1284 case VT_WAITACTIVE: /* wait for switch to occur */ 1285 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1); 1286 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys)) 1287 return EINVAL; 1288 if (i == sc->cur_scp->index) 1289 return 0; 1290 error = tsleep(VTY_WCHAN(sc, i), (PZERO + 1) | PCATCH, "waitvt", 0); 1291 return error; 1292 1293 case VT_GETACTIVE: /* get active vty # */ 1294 *(int *)data = sc->cur_scp->index + 1; 1295 return 0; 1296 1297 case VT_GETINDEX: /* get this vty # */ 1298 *(int *)data = scp->index + 1; 1299 return 0; 1300 1301 case VT_LOCKSWITCH: /* prevent vty switching */ 1302 if ((*(int *)data) & 0x01) 1303 sc->flags |= SC_SCRN_VTYLOCK; 1304 else 1305 sc->flags &= ~SC_SCRN_VTYLOCK; 1306 return 0; 1307 1308 case KDENABIO: /* allow io operations */ 1309 error = priv_check(td, PRIV_IO); 1310 if (error != 0) 1311 return error; 1312 error = securelevel_gt(td->td_ucred, 0); 1313 if (error != 0) 1314 return error; 1315 #ifdef __i386__ 1316 td->td_frame->tf_eflags |= PSL_IOPL; 1317 #elif defined(__amd64__) 1318 td->td_frame->tf_rflags |= PSL_IOPL; 1319 #endif 1320 return 0; 1321 1322 case KDDISABIO: /* disallow io operations (default) */ 1323 #ifdef __i386__ 1324 td->td_frame->tf_eflags &= ~PSL_IOPL; 1325 #elif defined(__amd64__) 1326 td->td_frame->tf_rflags &= ~PSL_IOPL; 1327 #endif 1328 return 0; 1329 1330 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1331 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1332 case _IO('K', 20): 1333 ival = IOCPARM_IVAL(data); 1334 data = (caddr_t)&ival; 1335 /* FALLTHROUGH */ 1336 #endif 1337 case KDSKBSTATE: /* set keyboard state (locks) */ 1338 if (*(int *)data & ~LOCK_MASK) 1339 return EINVAL; 1340 scp->status &= ~LOCK_MASK; 1341 scp->status |= *(int *)data; 1342 if (scp == sc->cur_scp) 1343 update_kbd_state(scp, scp->status, LOCK_MASK); 1344 return 0; 1345 1346 case KDGKBSTATE: /* get keyboard state (locks) */ 1347 if (scp == sc->cur_scp) 1348 save_kbd_state(scp); 1349 *(int *)data = scp->status & LOCK_MASK; 1350 return 0; 1351 1352 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 1353 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 1354 error = kbdd_ioctl(sc->kbd, cmd, data); 1355 if (error == ENOIOCTL) 1356 error = ENODEV; 1357 return error; 1358 1359 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1360 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1361 case _IO('K', 67): 1362 ival = IOCPARM_IVAL(data); 1363 data = (caddr_t)&ival; 1364 /* FALLTHROUGH */ 1365 #endif 1366 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 1367 if (*(int *)data & ~0x7f) 1368 return EINVAL; 1369 error = kbdd_ioctl(sc->kbd, KDSETRAD, data); 1370 if (error == ENOIOCTL) 1371 error = ENODEV; 1372 return error; 1373 1374 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1375 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1376 case _IO('K', 7): 1377 ival = IOCPARM_IVAL(data); 1378 data = (caddr_t)&ival; 1379 /* FALLTHROUGH */ 1380 #endif 1381 case KDSKBMODE: /* set keyboard mode */ 1382 switch (*(int *)data) { 1383 case K_XLATE: /* switch to XLT ascii mode */ 1384 case K_RAW: /* switch to RAW scancode mode */ 1385 case K_CODE: /* switch to CODE mode */ 1386 scp->kbd_mode = *(int *)data; 1387 if (scp == sc->cur_scp) 1388 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, data); 1389 return 0; 1390 default: 1391 return EINVAL; 1392 } 1393 /* NOT REACHED */ 1394 1395 case KDGKBMODE: /* get keyboard mode */ 1396 *(int *)data = scp->kbd_mode; 1397 return 0; 1398 1399 case KDGKBINFO: 1400 error = kbdd_ioctl(sc->kbd, cmd, data); 1401 if (error == ENOIOCTL) 1402 error = ENODEV; 1403 return error; 1404 1405 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1406 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1407 case _IO('K', 8): 1408 ival = IOCPARM_IVAL(data); 1409 data = (caddr_t)&ival; 1410 /* FALLTHROUGH */ 1411 #endif 1412 case KDMKTONE: /* sound the bell */ 1413 if (*(int*)data) 1414 sc_bell(scp, (*(int*)data)&0xffff, 1415 (((*(int*)data)>>16)&0xffff)*hz/1000); 1416 else 1417 sc_bell(scp, scp->bell_pitch, scp->bell_duration); 1418 return 0; 1419 1420 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1421 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1422 case _IO('K', 63): 1423 ival = IOCPARM_IVAL(data); 1424 data = (caddr_t)&ival; 1425 /* FALLTHROUGH */ 1426 #endif 1427 case KIOCSOUND: /* make tone (*data) hz */ 1428 if (scp == sc->cur_scp) { 1429 if (*(int *)data) 1430 return sc_tone(*(int *)data); 1431 else 1432 return sc_tone(0); 1433 } 1434 return 0; 1435 1436 case KDGKBTYPE: /* get keyboard type */ 1437 error = kbdd_ioctl(sc->kbd, cmd, data); 1438 if (error == ENOIOCTL) { 1439 /* always return something? XXX */ 1440 *(int *)data = 0; 1441 } 1442 return 0; 1443 1444 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1445 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1446 case _IO('K', 66): 1447 ival = IOCPARM_IVAL(data); 1448 data = (caddr_t)&ival; 1449 /* FALLTHROUGH */ 1450 #endif 1451 case KDSETLED: /* set keyboard LED status */ 1452 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */ 1453 return EINVAL; 1454 scp->status &= ~LED_MASK; 1455 scp->status |= *(int *)data; 1456 if (scp == sc->cur_scp) 1457 update_kbd_leds(scp, scp->status); 1458 return 0; 1459 1460 case KDGETLED: /* get keyboard LED status */ 1461 if (scp == sc->cur_scp) 1462 save_kbd_state(scp); 1463 *(int *)data = scp->status & LED_MASK; 1464 return 0; 1465 1466 case KBADDKBD: /* add/remove keyboard to/from mux */ 1467 case KBRELKBD: 1468 error = kbdd_ioctl(sc->kbd, cmd, data); 1469 if (error == ENOIOCTL) 1470 error = ENODEV; 1471 return error; 1472 1473 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1474 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1475 case _IO('c', 110): 1476 ival = IOCPARM_IVAL(data); 1477 data = (caddr_t)&ival; 1478 /* FALLTHROUGH */ 1479 #endif 1480 case CONS_SETKBD: /* set the new keyboard */ 1481 { 1482 keyboard_t *newkbd; 1483 1484 s = spltty(); 1485 newkbd = kbd_get_keyboard(*(int *)data); 1486 if (newkbd == NULL) { 1487 splx(s); 1488 return EINVAL; 1489 } 1490 error = 0; 1491 if (sc->kbd != newkbd) { 1492 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit, 1493 (void *)&sc->keyboard, sckbdevent, sc); 1494 /* i == newkbd->kb_index */ 1495 if (i >= 0) { 1496 if (sc->kbd != NULL) { 1497 save_kbd_state(sc->cur_scp); 1498 kbd_release(sc->kbd, (void *)&sc->keyboard); 1499 } 1500 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */ 1501 sc->keyboard = i; 1502 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, 1503 (caddr_t)&sc->cur_scp->kbd_mode); 1504 update_kbd_state(sc->cur_scp, sc->cur_scp->status, 1505 LOCK_MASK); 1506 } else { 1507 error = EPERM; /* XXX */ 1508 } 1509 } 1510 splx(s); 1511 return error; 1512 } 1513 1514 case CONS_RELKBD: /* release the current keyboard */ 1515 s = spltty(); 1516 error = 0; 1517 if (sc->kbd != NULL) { 1518 save_kbd_state(sc->cur_scp); 1519 error = kbd_release(sc->kbd, (void *)&sc->keyboard); 1520 if (error == 0) { 1521 sc->kbd = NULL; 1522 sc->keyboard = -1; 1523 } 1524 } 1525 splx(s); 1526 return error; 1527 1528 case CONS_GETTERM: /* get the current terminal emulator info */ 1529 { 1530 sc_term_sw_t *sw; 1531 1532 if (((term_info_t *)data)->ti_index == 0) { 1533 sw = scp->tsw; 1534 } else { 1535 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index); 1536 } 1537 if (sw != NULL) { 1538 strncpy(((term_info_t *)data)->ti_name, sw->te_name, 1539 sizeof(((term_info_t *)data)->ti_name)); 1540 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc, 1541 sizeof(((term_info_t *)data)->ti_desc)); 1542 ((term_info_t *)data)->ti_flags = 0; 1543 return 0; 1544 } else { 1545 ((term_info_t *)data)->ti_name[0] = '\0'; 1546 ((term_info_t *)data)->ti_desc[0] = '\0'; 1547 ((term_info_t *)data)->ti_flags = 0; 1548 return EINVAL; 1549 } 1550 } 1551 1552 case CONS_SETTERM: /* set the current terminal emulator */ 1553 s = spltty(); 1554 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name); 1555 /* FIXME: what if scp == sc_console! XXX */ 1556 splx(s); 1557 return error; 1558 1559 case GIO_SCRNMAP: /* get output translation table */ 1560 bcopy(&sc->scr_map, data, sizeof(sc->scr_map)); 1561 return 0; 1562 1563 case PIO_SCRNMAP: /* set output translation table */ 1564 bcopy(data, &sc->scr_map, sizeof(sc->scr_map)); 1565 for (i=0; i<sizeof(sc->scr_map); i++) { 1566 sc->scr_rmap[sc->scr_map[i]] = i; 1567 } 1568 return 0; 1569 1570 case GIO_KEYMAP: /* get keyboard translation table */ 1571 case PIO_KEYMAP: /* set keyboard translation table */ 1572 case OGIO_KEYMAP: /* get keyboard translation table (compat) */ 1573 case OPIO_KEYMAP: /* set keyboard translation table (compat) */ 1574 case GIO_DEADKEYMAP: /* get accent key translation table */ 1575 case PIO_DEADKEYMAP: /* set accent key translation table */ 1576 case GETFKEY: /* get function key string */ 1577 case SETFKEY: /* set function key string */ 1578 error = kbdd_ioctl(sc->kbd, cmd, data); 1579 if (error == ENOIOCTL) 1580 error = ENODEV; 1581 return error; 1582 1583 #ifndef SC_NO_FONT_LOADING 1584 1585 case PIO_FONT8x8: /* set 8x8 dot font */ 1586 if (!ISFONTAVAIL(sc->adp->va_flags)) 1587 return ENXIO; 1588 bcopy(data, sc->font_8, 8*256); 1589 sc->fonts_loaded |= FONT_8; 1590 /* 1591 * FONT KLUDGE 1592 * Always use the font page #0. XXX 1593 * Don't load if the current font size is not 8x8. 1594 */ 1595 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14)) 1596 sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256); 1597 return 0; 1598 1599 case GIO_FONT8x8: /* get 8x8 dot font */ 1600 if (!ISFONTAVAIL(sc->adp->va_flags)) 1601 return ENXIO; 1602 if (sc->fonts_loaded & FONT_8) { 1603 bcopy(sc->font_8, data, 8*256); 1604 return 0; 1605 } 1606 else 1607 return ENXIO; 1608 1609 case PIO_FONT8x14: /* set 8x14 dot font */ 1610 if (!ISFONTAVAIL(sc->adp->va_flags)) 1611 return ENXIO; 1612 bcopy(data, sc->font_14, 14*256); 1613 sc->fonts_loaded |= FONT_14; 1614 /* 1615 * FONT KLUDGE 1616 * Always use the font page #0. XXX 1617 * Don't load if the current font size is not 8x14. 1618 */ 1619 if (ISTEXTSC(sc->cur_scp) 1620 && (sc->cur_scp->font_size >= 14) 1621 && (sc->cur_scp->font_size < 16)) 1622 sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256); 1623 return 0; 1624 1625 case GIO_FONT8x14: /* get 8x14 dot font */ 1626 if (!ISFONTAVAIL(sc->adp->va_flags)) 1627 return ENXIO; 1628 if (sc->fonts_loaded & FONT_14) { 1629 bcopy(sc->font_14, data, 14*256); 1630 return 0; 1631 } 1632 else 1633 return ENXIO; 1634 1635 case PIO_FONT8x16: /* set 8x16 dot font */ 1636 if (!ISFONTAVAIL(sc->adp->va_flags)) 1637 return ENXIO; 1638 bcopy(data, sc->font_16, 16*256); 1639 sc->fonts_loaded |= FONT_16; 1640 /* 1641 * FONT KLUDGE 1642 * Always use the font page #0. XXX 1643 * Don't load if the current font size is not 8x16. 1644 */ 1645 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16)) 1646 sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256); 1647 return 0; 1648 1649 case GIO_FONT8x16: /* get 8x16 dot font */ 1650 if (!ISFONTAVAIL(sc->adp->va_flags)) 1651 return ENXIO; 1652 if (sc->fonts_loaded & FONT_16) { 1653 bcopy(sc->font_16, data, 16*256); 1654 return 0; 1655 } 1656 else 1657 return ENXIO; 1658 1659 #endif /* SC_NO_FONT_LOADING */ 1660 1661 default: 1662 break; 1663 } 1664 1665 return (ENOIOCTL); 1666 } 1667 1668 static int 1669 consolectl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 1670 struct thread *td) 1671 { 1672 1673 return sctty_ioctl(dev->si_drv1, cmd, data, td); 1674 } 1675 1676 static int 1677 consolectl_close(struct cdev *dev, int flags, int mode, struct thread *td) 1678 { 1679 #ifndef SC_NO_SYSMOUSE 1680 mouse_info_t info; 1681 memset(&info, 0, sizeof(info)); 1682 info.operation = MOUSE_ACTION; 1683 1684 /* 1685 * Make sure all buttons are released when moused and other 1686 * console daemons exit, so that no buttons are left pressed. 1687 */ 1688 (void) sctty_ioctl(dev->si_drv1, CONS_MOUSECTL, (caddr_t)&info, td); 1689 #endif 1690 return (0); 1691 } 1692 1693 static void 1694 sc_cnprobe(struct consdev *cp) 1695 { 1696 int unit; 1697 int flags; 1698 1699 if (!vty_enabled(VTY_SC)) { 1700 cp->cn_pri = CN_DEAD; 1701 return; 1702 } 1703 1704 cp->cn_pri = sc_get_cons_priority(&unit, &flags); 1705 1706 /* a video card is always required */ 1707 if (!scvidprobe(unit, flags, TRUE)) 1708 cp->cn_pri = CN_DEAD; 1709 1710 /* syscons will become console even when there is no keyboard */ 1711 sckbdprobe(unit, flags, TRUE); 1712 1713 if (cp->cn_pri == CN_DEAD) 1714 return; 1715 1716 /* initialize required fields */ 1717 strcpy(cp->cn_name, "ttyv0"); 1718 } 1719 1720 static void 1721 sc_cninit(struct consdev *cp) 1722 { 1723 int unit; 1724 int flags; 1725 1726 sc_get_cons_priority(&unit, &flags); 1727 scinit(unit, flags | SC_KERNEL_CONSOLE); 1728 sc_console_unit = unit; 1729 sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]); 1730 sc_consptr = cp; 1731 } 1732 1733 static void 1734 sc_cnterm(struct consdev *cp) 1735 { 1736 void *ts; 1737 int i; 1738 1739 /* we are not the kernel console any more, release everything */ 1740 1741 if (sc_console_unit < 0) 1742 return; /* shouldn't happen */ 1743 1744 #if 0 /* XXX */ 1745 sc_clear_screen(sc_console); 1746 sccnupdate(sc_console); 1747 #endif 1748 1749 if (sc_ktsw != NULL) { 1750 for (i = 0; i <= mp_maxid; i++) { 1751 ts = sc_kts[i]; 1752 sc_kts[i] = NULL; 1753 (*sc_ktsw->te_term)(sc_console, &ts); 1754 free(ts, M_DEVBUF); 1755 } 1756 sc_ktsw = NULL; 1757 } 1758 scterm(sc_console_unit, SC_KERNEL_CONSOLE); 1759 sc_console_unit = -1; 1760 sc_console = NULL; 1761 } 1762 1763 static void sccnclose(sc_softc_t *sc, struct sc_cnstate *sp); 1764 static int sc_cngetc_locked(struct sc_cnstate *sp); 1765 static void sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp); 1766 static void sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp); 1767 static void sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags); 1768 static void sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp); 1769 static void sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp); 1770 1771 static void 1772 sccnkbdlock(sc_softc_t *sc, struct sc_cnstate *sp) 1773 { 1774 /* 1775 * Locking method: hope for the best. 1776 * The keyboard is supposed to be Giant locked. We can't handle that 1777 * in general. The kdb_active case here is not safe, and we will 1778 * proceed without the lock in all cases. 1779 */ 1780 sp->kbd_locked = !kdb_active && mtx_trylock(&Giant); 1781 } 1782 1783 static void 1784 sccnkbdunlock(sc_softc_t *sc, struct sc_cnstate *sp) 1785 { 1786 if (sp->kbd_locked) 1787 mtx_unlock(&Giant); 1788 sp->kbd_locked = FALSE; 1789 } 1790 1791 static void 1792 sccnscrlock(sc_softc_t *sc, struct sc_cnstate *sp) 1793 { 1794 int retries; 1795 1796 /** 1797 * Locking method: 1798 * - if kdb_active and video_mtx is not owned by anyone, then lock 1799 * by kdb remaining active 1800 * - if !kdb_active, try to acquire video_mtx without blocking or 1801 * recursing; if we get it then it works normally. 1802 * Note that video_mtx is especially unusable if we already own it, 1803 * since then it is protecting something and syscons is not reentrant 1804 * enough to ignore the protection even in the kdb_active case. 1805 */ 1806 if (kdb_active) { 1807 sp->kdb_locked = sc->video_mtx.mtx_lock == MTX_UNOWNED || panicstr; 1808 sp->mtx_locked = FALSE; 1809 } else { 1810 sp->kdb_locked = FALSE; 1811 for (retries = 0; retries < 1000; retries++) { 1812 sp->mtx_locked = mtx_trylock_spin_flags(&sc->video_mtx, 1813 MTX_QUIET) != 0 || panicstr; 1814 if (sp->mtx_locked) 1815 break; 1816 DELAY(1); 1817 } 1818 } 1819 } 1820 1821 static void 1822 sccnscrunlock(sc_softc_t *sc, struct sc_cnstate *sp) 1823 { 1824 if (sp->mtx_locked) 1825 mtx_unlock_spin(&sc->video_mtx); 1826 sp->mtx_locked = sp->kdb_locked = FALSE; 1827 } 1828 1829 static void 1830 sccnopen(sc_softc_t *sc, struct sc_cnstate *sp, int flags) 1831 { 1832 int kbd_mode; 1833 1834 /* assert(sc_console_unit >= 0) */ 1835 1836 sp->kbd_opened = FALSE; 1837 sp->scr_opened = FALSE; 1838 sp->kbd_locked = FALSE; 1839 1840 /* Opening the keyboard is optional. */ 1841 if (!(flags & 1) || sc->kbd == NULL) 1842 goto over_keyboard; 1843 1844 sccnkbdlock(sc, sp); 1845 1846 /* 1847 * Make sure the keyboard is accessible even when the kbd device 1848 * driver is disabled. 1849 */ 1850 kbdd_enable(sc->kbd); 1851 1852 /* Switch the keyboard to console mode (K_XLATE, polled) on all scp's. */ 1853 kbd_mode = K_XLATE; 1854 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&kbd_mode); 1855 sc->kbd_open_level++; 1856 kbdd_poll(sc->kbd, TRUE); 1857 1858 sp->kbd_opened = TRUE; 1859 over_keyboard: ; 1860 1861 /* The screen is opened iff locking it succeeds. */ 1862 sccnscrlock(sc, sp); 1863 if (!sp->kdb_locked && !sp->mtx_locked) 1864 return; 1865 sp->scr_opened = TRUE; 1866 1867 /* The screen switch is optional. */ 1868 if (!(flags & 2)) 1869 return; 1870 1871 /* try to switch to the kernel console screen */ 1872 if (!cold && 1873 sc->cur_scp->index != sc_console->index && 1874 sc->cur_scp->smode.mode == VT_AUTO && 1875 sc_console->smode.mode == VT_AUTO) 1876 sc_switch_scr(sc, sc_console->index); 1877 } 1878 1879 static void 1880 sccnclose(sc_softc_t *sc, struct sc_cnstate *sp) 1881 { 1882 sp->scr_opened = FALSE; 1883 sccnscrunlock(sc, sp); 1884 1885 if (!sp->kbd_opened) 1886 return; 1887 1888 /* Restore keyboard mode (for the current, possibly-changed scp). */ 1889 kbdd_poll(sc->kbd, FALSE); 1890 if (--sc->kbd_open_level == 0) 1891 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&sc->cur_scp->kbd_mode); 1892 1893 kbdd_disable(sc->kbd); 1894 sp->kbd_opened = FALSE; 1895 sccnkbdunlock(sc, sp); 1896 } 1897 1898 /* 1899 * Grabbing switches the screen and keyboard focus to sc_console and the 1900 * keyboard mode to (K_XLATE, polled). Only switching to polled mode is 1901 * essential (for preventing the interrupt handler from eating input 1902 * between polls). Focus is part of the UI, and the other switches are 1903 * work just was well when they are done on every entry and exit. 1904 * 1905 * Screen switches while grabbed are supported, and to maintain focus for 1906 * this ungrabbing and closing only restore the polling state and then 1907 * the keyboard mode if on the original screen. 1908 */ 1909 1910 static void 1911 sc_cngrab(struct consdev *cp) 1912 { 1913 sc_softc_t *sc; 1914 int lev; 1915 1916 sc = sc_console->sc; 1917 lev = atomic_fetchadd_int(&sc->grab_level, 1); 1918 if (lev >= 0 && lev < 2) { 1919 sccnopen(sc, &sc->grab_state[lev], 1 | 2); 1920 sccnscrunlock(sc, &sc->grab_state[lev]); 1921 sccnkbdunlock(sc, &sc->grab_state[lev]); 1922 } 1923 } 1924 1925 static void 1926 sc_cnungrab(struct consdev *cp) 1927 { 1928 sc_softc_t *sc; 1929 int lev; 1930 1931 sc = sc_console->sc; 1932 lev = atomic_load_acq_int(&sc->grab_level) - 1; 1933 if (lev >= 0 && lev < 2) { 1934 sccnkbdlock(sc, &sc->grab_state[lev]); 1935 sccnscrlock(sc, &sc->grab_state[lev]); 1936 sccnclose(sc, &sc->grab_state[lev]); 1937 } 1938 atomic_add_int(&sc->grab_level, -1); 1939 } 1940 1941 static char sc_cnputc_log[0x1000]; 1942 static u_int sc_cnputc_loghead; 1943 static u_int sc_cnputc_logtail; 1944 1945 static void 1946 sc_cnputc(struct consdev *cd, int c) 1947 { 1948 struct sc_cnstate st; 1949 u_char buf[1]; 1950 scr_stat *scp = sc_console; 1951 void *oldts, *ts; 1952 struct sc_term_sw *oldtsw; 1953 #ifndef SC_NO_HISTORY 1954 #if 0 1955 struct tty *tp; 1956 #endif 1957 #endif /* !SC_NO_HISTORY */ 1958 u_int head; 1959 int s; 1960 1961 /* assert(sc_console != NULL) */ 1962 1963 sccnopen(scp->sc, &st, 0); 1964 1965 /* 1966 * Log the output. 1967 * 1968 * In the unlocked case, the logging is intentionally only 1969 * perfectly atomic for the indexes. 1970 */ 1971 head = atomic_fetchadd_int(&sc_cnputc_loghead, 1); 1972 sc_cnputc_log[head % sizeof(sc_cnputc_log)] = c; 1973 1974 /* 1975 * If we couldn't open, do special reentrant output and return to defer 1976 * normal output. 1977 */ 1978 if (!st.scr_opened) { 1979 ec_putc(c); 1980 return; 1981 } 1982 1983 #ifndef SC_NO_HISTORY 1984 if (scp == scp->sc->cur_scp && scp->status & SLKED) { 1985 scp->status &= ~SLKED; 1986 update_kbd_state(scp, scp->status, SLKED); 1987 if (scp->status & BUFFER_SAVED) { 1988 if (!sc_hist_restore(scp)) 1989 sc_remove_cutmarking(scp); 1990 scp->status &= ~BUFFER_SAVED; 1991 scp->status |= CURSOR_ENABLED; 1992 sc_draw_cursor_image(scp); 1993 } 1994 #if 0 1995 /* 1996 * XXX: Now that TTY's have their own locks, we cannot process 1997 * any data after disabling scroll lock. cnputs already holds a 1998 * spinlock. 1999 */ 2000 tp = SC_DEV(scp->sc, scp->index); 2001 /* XXX "tp" can be NULL */ 2002 tty_lock(tp); 2003 if (tty_opened(tp)) 2004 sctty_outwakeup(tp); 2005 tty_unlock(tp); 2006 #endif 2007 } 2008 #endif /* !SC_NO_HISTORY */ 2009 2010 /* Play any output still in the log (our char may already be done). */ 2011 while (sc_cnputc_logtail != atomic_load_acq_int(&sc_cnputc_loghead)) { 2012 buf[0] = sc_cnputc_log[sc_cnputc_logtail++ % sizeof(sc_cnputc_log)]; 2013 if (atomic_load_acq_int(&sc_cnputc_loghead) - sc_cnputc_logtail >= 2014 sizeof(sc_cnputc_log)) 2015 continue; 2016 /* Console output has a per-CPU "input" state. Switch for it. */ 2017 oldtsw = scp->tsw; 2018 oldts = scp->ts; 2019 ts = sc_kts[PCPU_GET(cpuid)]; 2020 if (ts != NULL) { 2021 scp->tsw = sc_ktsw; 2022 scp->ts = ts; 2023 (*scp->tsw->te_sync)(scp); 2024 } 2025 sc_puts(scp, buf, 1); 2026 scp->tsw = oldtsw; 2027 scp->ts = oldts; 2028 (*scp->tsw->te_sync)(scp); 2029 } 2030 2031 s = spltty(); /* block sckbdevent and scrn_timer */ 2032 sccnupdate(scp); 2033 splx(s); 2034 sccnclose(scp->sc, &st); 2035 } 2036 2037 static int 2038 sc_cngetc(struct consdev *cd) 2039 { 2040 struct sc_cnstate st; 2041 int c, s; 2042 2043 /* assert(sc_console != NULL) */ 2044 sccnopen(sc_console->sc, &st, 1); 2045 s = spltty(); /* block sckbdevent and scrn_timer while we poll */ 2046 if (!st.kbd_opened) { 2047 splx(s); 2048 sccnclose(sc_console->sc, &st); 2049 return -1; /* means no keyboard since we fudged the locking */ 2050 } 2051 c = sc_cngetc_locked(&st); 2052 splx(s); 2053 sccnclose(sc_console->sc, &st); 2054 return c; 2055 } 2056 2057 static int 2058 sc_cngetc_locked(struct sc_cnstate *sp) 2059 { 2060 static struct fkeytab fkey; 2061 static int fkeycp; 2062 scr_stat *scp; 2063 const u_char *p; 2064 int c; 2065 2066 /* 2067 * Stop the screen saver and update the screen if necessary. 2068 * What if we have been running in the screen saver code... XXX 2069 */ 2070 if (sp->scr_opened) 2071 sc_touch_scrn_saver(); 2072 scp = sc_console->sc->cur_scp; /* XXX */ 2073 if (sp->scr_opened) 2074 sccnupdate(scp); 2075 2076 if (fkeycp < fkey.len) 2077 return fkey.str[fkeycp++]; 2078 2079 c = scgetc(scp->sc, SCGETC_CN | SCGETC_NONBLOCK, sp); 2080 2081 switch (KEYFLAGS(c)) { 2082 case 0: /* normal char */ 2083 return KEYCHAR(c); 2084 case FKEY: /* function key */ 2085 p = (*scp->tsw->te_fkeystr)(scp, c); 2086 if (p != NULL) { 2087 fkey.len = strlen(p); 2088 bcopy(p, fkey.str, fkey.len); 2089 fkeycp = 1; 2090 return fkey.str[0]; 2091 } 2092 p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp); 2093 fkey.len = fkeycp; 2094 if ((p != NULL) && (fkey.len > 0)) { 2095 bcopy(p, fkey.str, fkey.len); 2096 fkeycp = 1; 2097 return fkey.str[0]; 2098 } 2099 return c; /* XXX */ 2100 case NOKEY: 2101 case ERRKEY: 2102 default: 2103 return -1; 2104 } 2105 /* NOT REACHED */ 2106 } 2107 2108 static void 2109 sccnupdate(scr_stat *scp) 2110 { 2111 /* this is a cut-down version of scrn_timer()... */ 2112 2113 if (suspend_in_progress || scp->sc->font_loading_in_progress) 2114 return; 2115 2116 if (kdb_active || panicstr || shutdown_in_progress) { 2117 sc_touch_scrn_saver(); 2118 } else if (scp != scp->sc->cur_scp) { 2119 return; 2120 } 2121 2122 if (!run_scrn_saver) 2123 scp->sc->flags &= ~SC_SCRN_IDLE; 2124 #ifdef DEV_SPLASH 2125 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE)) 2126 if (scp->sc->flags & SC_SCRN_BLANKED) 2127 stop_scrn_saver(scp->sc, current_saver); 2128 #endif 2129 2130 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress 2131 || scp->sc->switch_in_progress) 2132 return; 2133 /* 2134 * FIXME: unlike scrn_timer(), we call scrn_update() from here even 2135 * when write_in_progress is non-zero. XXX 2136 */ 2137 2138 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED)) 2139 scrn_update(scp, TRUE); 2140 } 2141 2142 static void 2143 scrn_timer(void *arg) 2144 { 2145 static time_t kbd_time_stamp = 0; 2146 sc_softc_t *sc; 2147 scr_stat *scp; 2148 int again, rate; 2149 2150 again = (arg != NULL); 2151 if (arg != NULL) 2152 sc = (sc_softc_t *)arg; 2153 else if (sc_console != NULL) 2154 sc = sc_console->sc; 2155 else 2156 return; 2157 2158 /* find the vty to update */ 2159 scp = sc->cur_scp; 2160 2161 /* don't do anything when we are performing some I/O operations */ 2162 if (suspend_in_progress || sc->font_loading_in_progress) 2163 goto done; 2164 2165 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) { 2166 /* try to allocate a keyboard automatically */ 2167 if (kbd_time_stamp != time_uptime) { 2168 kbd_time_stamp = time_uptime; 2169 sc->keyboard = sc_allocate_keyboard(sc, -1); 2170 if (sc->keyboard >= 0) { 2171 sc->kbd = kbd_get_keyboard(sc->keyboard); 2172 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, 2173 (caddr_t)&sc->cur_scp->kbd_mode); 2174 update_kbd_state(sc->cur_scp, sc->cur_scp->status, 2175 LOCK_MASK); 2176 } 2177 } 2178 } 2179 2180 /* should we stop the screen saver? */ 2181 if (kdb_active || panicstr || shutdown_in_progress) 2182 sc_touch_scrn_saver(); 2183 if (run_scrn_saver) { 2184 if (time_uptime > sc->scrn_time_stamp + scrn_blank_time) 2185 sc->flags |= SC_SCRN_IDLE; 2186 else 2187 sc->flags &= ~SC_SCRN_IDLE; 2188 } else { 2189 sc->scrn_time_stamp = time_uptime; 2190 sc->flags &= ~SC_SCRN_IDLE; 2191 if (scrn_blank_time > 0) 2192 run_scrn_saver = TRUE; 2193 } 2194 #ifdef DEV_SPLASH 2195 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE)) 2196 if (sc->flags & SC_SCRN_BLANKED) 2197 stop_scrn_saver(sc, current_saver); 2198 #endif 2199 2200 /* should we just return ? */ 2201 if (sc->blink_in_progress || sc->switch_in_progress 2202 || sc->write_in_progress) 2203 goto done; 2204 2205 /* Update the screen */ 2206 scp = sc->cur_scp; /* cur_scp may have changed... */ 2207 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED)) 2208 scrn_update(scp, TRUE); 2209 2210 #ifdef DEV_SPLASH 2211 /* should we activate the screen saver? */ 2212 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE)) 2213 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED)) 2214 (*current_saver)(sc, TRUE); 2215 #endif 2216 2217 done: 2218 if (again) { 2219 /* 2220 * Use reduced "refresh" rate if we are in graphics and that is not a 2221 * graphical screen saver. In such case we just have nothing to do. 2222 */ 2223 if (ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED)) 2224 rate = 2; 2225 else 2226 rate = 30; 2227 callout_reset_sbt(&sc->ctimeout, SBT_1S / rate, 0, 2228 scrn_timer, sc, C_PREL(1)); 2229 } 2230 } 2231 2232 static int 2233 and_region(int *s1, int *e1, int s2, int e2) 2234 { 2235 if (*e1 < s2 || e2 < *s1) 2236 return FALSE; 2237 *s1 = imax(*s1, s2); 2238 *e1 = imin(*e1, e2); 2239 return TRUE; 2240 } 2241 2242 static void 2243 scrn_update(scr_stat *scp, int show_cursor) 2244 { 2245 int start; 2246 int end; 2247 int s; 2248 int e; 2249 2250 /* assert(scp == scp->sc->cur_scp) */ 2251 2252 SC_VIDEO_LOCK(scp->sc); 2253 2254 #ifndef SC_NO_CUTPASTE 2255 /* remove the previous mouse pointer image if necessary */ 2256 if (scp->status & MOUSE_VISIBLE) { 2257 s = scp->mouse_pos; 2258 e = scp->mouse_pos + scp->xsize + 1; 2259 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN)) 2260 || and_region(&s, &e, scp->start, scp->end) 2261 || ((scp->status & CURSOR_ENABLED) && 2262 (scp->cursor_pos != scp->cursor_oldpos) && 2263 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos) 2264 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) { 2265 sc_remove_mouse_image(scp); 2266 if (scp->end >= scp->xsize*scp->ysize) 2267 scp->end = scp->xsize*scp->ysize - 1; 2268 } 2269 } 2270 #endif /* !SC_NO_CUTPASTE */ 2271 2272 #if 1 2273 /* debug: XXX */ 2274 if (scp->end >= scp->xsize*scp->ysize) { 2275 printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end); 2276 scp->end = scp->xsize*scp->ysize - 1; 2277 } 2278 if (scp->start < 0) { 2279 printf("scrn_update(): scp->start %d < 0\n", scp->start); 2280 scp->start = 0; 2281 } 2282 #endif 2283 2284 /* update screen image */ 2285 if (scp->start <= scp->end) { 2286 if (scp->mouse_cut_end >= 0) { 2287 /* there is a marked region for cut & paste */ 2288 if (scp->mouse_cut_start <= scp->mouse_cut_end) { 2289 start = scp->mouse_cut_start; 2290 end = scp->mouse_cut_end; 2291 } else { 2292 start = scp->mouse_cut_end; 2293 end = scp->mouse_cut_start - 1; 2294 } 2295 s = start; 2296 e = end; 2297 /* does the cut-mark region overlap with the update region? */ 2298 if (and_region(&s, &e, scp->start, scp->end)) { 2299 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE); 2300 s = 0; 2301 e = start - 1; 2302 if (and_region(&s, &e, scp->start, scp->end)) 2303 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE); 2304 s = end + 1; 2305 e = scp->xsize*scp->ysize - 1; 2306 if (and_region(&s, &e, scp->start, scp->end)) 2307 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE); 2308 } else { 2309 (*scp->rndr->draw)(scp, scp->start, 2310 scp->end - scp->start + 1, FALSE); 2311 } 2312 } else { 2313 (*scp->rndr->draw)(scp, scp->start, 2314 scp->end - scp->start + 1, FALSE); 2315 } 2316 } 2317 2318 /* we are not to show the cursor and the mouse pointer... */ 2319 if (!show_cursor) { 2320 scp->end = 0; 2321 scp->start = scp->xsize*scp->ysize - 1; 2322 SC_VIDEO_UNLOCK(scp->sc); 2323 return; 2324 } 2325 2326 /* update cursor image */ 2327 if (scp->status & CURSOR_ENABLED) { 2328 s = scp->start; 2329 e = scp->end; 2330 /* did cursor move since last time ? */ 2331 if (scp->cursor_pos != scp->cursor_oldpos) { 2332 /* do we need to remove old cursor image ? */ 2333 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)) 2334 sc_remove_cursor_image(scp); 2335 sc_draw_cursor_image(scp); 2336 } else { 2337 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)) 2338 /* cursor didn't move, but has been overwritten */ 2339 sc_draw_cursor_image(scp); 2340 else if (scp->curs_attr.flags & CONS_BLINK_CURSOR) 2341 /* if it's a blinking cursor, update it */ 2342 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos, 2343 sc_inside_cutmark(scp, 2344 scp->cursor_pos)); 2345 } 2346 } 2347 2348 #ifndef SC_NO_CUTPASTE 2349 /* update "pseudo" mouse pointer image */ 2350 if (scp->sc->flags & SC_MOUSE_ENABLED) { 2351 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) { 2352 scp->status &= ~MOUSE_MOVED; 2353 sc_draw_mouse_image(scp); 2354 } 2355 } 2356 #endif /* SC_NO_CUTPASTE */ 2357 2358 scp->end = 0; 2359 scp->start = scp->xsize*scp->ysize - 1; 2360 2361 SC_VIDEO_UNLOCK(scp->sc); 2362 } 2363 2364 #ifdef DEV_SPLASH 2365 static int 2366 scsplash_callback(int event, void *arg) 2367 { 2368 sc_softc_t *sc; 2369 int error; 2370 2371 sc = (sc_softc_t *)arg; 2372 2373 switch (event) { 2374 case SPLASH_INIT: 2375 if (add_scrn_saver(scsplash_saver) == 0) { 2376 sc->flags &= ~SC_SAVER_FAILED; 2377 run_scrn_saver = TRUE; 2378 if (cold && !(boothowto & RB_VERBOSE)) { 2379 scsplash_stick(TRUE); 2380 (*current_saver)(sc, TRUE); 2381 } 2382 } 2383 return 0; 2384 2385 case SPLASH_TERM: 2386 if (current_saver == scsplash_saver) { 2387 scsplash_stick(FALSE); 2388 error = remove_scrn_saver(scsplash_saver); 2389 if (error) 2390 return error; 2391 } 2392 return 0; 2393 2394 default: 2395 return EINVAL; 2396 } 2397 } 2398 2399 static void 2400 scsplash_saver(sc_softc_t *sc, int show) 2401 { 2402 static int busy = FALSE; 2403 scr_stat *scp; 2404 2405 if (busy) 2406 return; 2407 busy = TRUE; 2408 2409 scp = sc->cur_scp; 2410 if (show) { 2411 if (!(sc->flags & SC_SAVER_FAILED)) { 2412 if (!(sc->flags & SC_SCRN_BLANKED)) 2413 set_scrn_saver_mode(scp, -1, NULL, 0); 2414 switch (splash(sc->adp, TRUE)) { 2415 case 0: /* succeeded */ 2416 break; 2417 case EAGAIN: /* try later */ 2418 restore_scrn_saver_mode(scp, FALSE); 2419 sc_touch_scrn_saver(); /* XXX */ 2420 break; 2421 default: 2422 sc->flags |= SC_SAVER_FAILED; 2423 scsplash_stick(FALSE); 2424 restore_scrn_saver_mode(scp, TRUE); 2425 printf("scsplash_saver(): failed to put up the image\n"); 2426 break; 2427 } 2428 } 2429 } else if (!sticky_splash) { 2430 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0)) 2431 restore_scrn_saver_mode(scp, TRUE); 2432 } 2433 busy = FALSE; 2434 } 2435 2436 static int 2437 add_scrn_saver(void (*this_saver)(sc_softc_t *, int)) 2438 { 2439 #if 0 2440 int error; 2441 2442 if (current_saver != none_saver) { 2443 error = remove_scrn_saver(current_saver); 2444 if (error) 2445 return error; 2446 } 2447 #endif 2448 if (current_saver != none_saver) 2449 return EBUSY; 2450 2451 run_scrn_saver = FALSE; 2452 saver_mode = CONS_LKM_SAVER; 2453 current_saver = this_saver; 2454 return 0; 2455 } 2456 2457 static int 2458 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int)) 2459 { 2460 if (current_saver != this_saver) 2461 return EINVAL; 2462 2463 #if 0 2464 /* 2465 * In order to prevent `current_saver' from being called by 2466 * the timeout routine `scrn_timer()' while we manipulate 2467 * the saver list, we shall set `current_saver' to `none_saver' 2468 * before stopping the current saver, rather than blocking by `splXX()'. 2469 */ 2470 current_saver = none_saver; 2471 if (scrn_blanked) 2472 stop_scrn_saver(this_saver); 2473 #endif 2474 2475 /* unblank all blanked screens */ 2476 wait_scrn_saver_stop(NULL); 2477 if (scrn_blanked) 2478 return EBUSY; 2479 2480 current_saver = none_saver; 2481 return 0; 2482 } 2483 2484 static int 2485 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border) 2486 { 2487 int s; 2488 2489 /* assert(scp == scp->sc->cur_scp) */ 2490 s = spltty(); 2491 if (!ISGRAPHSC(scp)) 2492 sc_remove_cursor_image(scp); 2493 scp->splash_save_mode = scp->mode; 2494 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE); 2495 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE); 2496 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING); 2497 scp->sc->flags |= SC_SCRN_BLANKED; 2498 ++scrn_blanked; 2499 splx(s); 2500 if (mode < 0) 2501 return 0; 2502 scp->mode = mode; 2503 if (set_mode(scp) == 0) { 2504 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS) 2505 scp->status |= GRAPHICS_MODE; 2506 #ifndef SC_NO_PALETTE_LOADING 2507 if (pal != NULL) 2508 vidd_load_palette(scp->sc->adp, pal); 2509 #endif 2510 sc_set_border(scp, border); 2511 return 0; 2512 } else { 2513 s = spltty(); 2514 scp->mode = scp->splash_save_mode; 2515 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING); 2516 scp->status |= scp->splash_save_status; 2517 splx(s); 2518 return 1; 2519 } 2520 } 2521 2522 static int 2523 restore_scrn_saver_mode(scr_stat *scp, int changemode) 2524 { 2525 int mode; 2526 int status; 2527 int s; 2528 2529 /* assert(scp == scp->sc->cur_scp) */ 2530 s = spltty(); 2531 mode = scp->mode; 2532 status = scp->status; 2533 scp->mode = scp->splash_save_mode; 2534 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING); 2535 scp->status |= scp->splash_save_status; 2536 scp->sc->flags &= ~SC_SCRN_BLANKED; 2537 if (!changemode) { 2538 if (!ISGRAPHSC(scp)) 2539 sc_draw_cursor_image(scp); 2540 --scrn_blanked; 2541 splx(s); 2542 return 0; 2543 } 2544 if (set_mode(scp) == 0) { 2545 #ifndef SC_NO_PALETTE_LOADING 2546 #ifdef SC_PIXEL_MODE 2547 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT) 2548 vidd_load_palette(scp->sc->adp, scp->sc->palette2); 2549 else 2550 #endif 2551 vidd_load_palette(scp->sc->adp, scp->sc->palette); 2552 #endif 2553 --scrn_blanked; 2554 splx(s); 2555 return 0; 2556 } else { 2557 scp->mode = mode; 2558 scp->status = status; 2559 splx(s); 2560 return 1; 2561 } 2562 } 2563 2564 static void 2565 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int)) 2566 { 2567 (*saver)(sc, FALSE); 2568 run_scrn_saver = FALSE; 2569 /* the screen saver may have chosen not to stop after all... */ 2570 if (sc->flags & SC_SCRN_BLANKED) 2571 return; 2572 2573 mark_all(sc->cur_scp); 2574 if (sc->delayed_next_scr) 2575 sc_switch_scr(sc, sc->delayed_next_scr - 1); 2576 if (!kdb_active) 2577 wakeup(&scrn_blanked); 2578 } 2579 2580 static int 2581 wait_scrn_saver_stop(sc_softc_t *sc) 2582 { 2583 int error = 0; 2584 2585 while (scrn_blanked > 0) { 2586 run_scrn_saver = FALSE; 2587 if (sc && !(sc->flags & SC_SCRN_BLANKED)) { 2588 error = 0; 2589 break; 2590 } 2591 error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0); 2592 if ((error != 0) && (error != ERESTART)) 2593 break; 2594 } 2595 run_scrn_saver = FALSE; 2596 return error; 2597 } 2598 #endif /* DEV_SPLASH */ 2599 2600 void 2601 sc_touch_scrn_saver(void) 2602 { 2603 scsplash_stick(FALSE); 2604 run_scrn_saver = FALSE; 2605 } 2606 2607 int 2608 sc_switch_scr(sc_softc_t *sc, u_int next_scr) 2609 { 2610 scr_stat *cur_scp; 2611 struct tty *tp; 2612 struct proc *p; 2613 int s; 2614 2615 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1)); 2616 2617 if (sc->cur_scp == NULL) 2618 return (0); 2619 2620 /* prevent switch if previously requested */ 2621 if (sc->flags & SC_SCRN_VTYLOCK) { 2622 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch, 2623 sc->cur_scp->bell_duration); 2624 return EPERM; 2625 } 2626 2627 /* delay switch if the screen is blanked or being updated */ 2628 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress 2629 || sc->blink_in_progress) { 2630 sc->delayed_next_scr = next_scr + 1; 2631 sc_touch_scrn_saver(); 2632 DPRINTF(5, ("switch delayed\n")); 2633 return 0; 2634 } 2635 sc->delayed_next_scr = 0; 2636 2637 s = spltty(); 2638 cur_scp = sc->cur_scp; 2639 2640 /* we are in the middle of the vty switching process... */ 2641 if (sc->switch_in_progress 2642 && (cur_scp->smode.mode == VT_PROCESS) 2643 && cur_scp->proc) { 2644 p = pfind(cur_scp->pid); 2645 if (cur_scp->proc != p) { 2646 if (p) 2647 PROC_UNLOCK(p); 2648 /* 2649 * The controlling process has died!!. Do some clean up. 2650 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode' 2651 * are not reset here yet; they will be cleared later. 2652 */ 2653 DPRINTF(5, ("cur_scp controlling process %d died, ", 2654 cur_scp->pid)); 2655 if (cur_scp->status & SWITCH_WAIT_REL) { 2656 /* 2657 * Force the previous switch to finish, but return now 2658 * with error. 2659 */ 2660 DPRINTF(5, ("reset WAIT_REL, ")); 2661 finish_vt_rel(cur_scp, TRUE, &s); 2662 splx(s); 2663 DPRINTF(5, ("finishing previous switch\n")); 2664 return EINVAL; 2665 } else if (cur_scp->status & SWITCH_WAIT_ACQ) { 2666 /* let's assume screen switch has been completed. */ 2667 DPRINTF(5, ("reset WAIT_ACQ, ")); 2668 finish_vt_acq(cur_scp); 2669 } else { 2670 /* 2671 * We are in between screen release and acquisition, and 2672 * reached here via scgetc() or scrn_timer() which has 2673 * interrupted exchange_scr(). Don't do anything stupid. 2674 */ 2675 DPRINTF(5, ("waiting nothing, ")); 2676 } 2677 } else { 2678 if (p) 2679 PROC_UNLOCK(p); 2680 /* 2681 * The controlling process is alive, but not responding... 2682 * It is either buggy or it may be just taking time. 2683 * The following code is a gross kludge to cope with this 2684 * problem for which there is no clean solution. XXX 2685 */ 2686 if (cur_scp->status & SWITCH_WAIT_REL) { 2687 switch (sc->switch_in_progress++) { 2688 case 1: 2689 break; 2690 case 2: 2691 DPRINTF(5, ("sending relsig again, ")); 2692 signal_vt_rel(cur_scp); 2693 break; 2694 case 3: 2695 break; 2696 case 4: 2697 default: 2698 /* 2699 * Act as if the controlling program returned 2700 * VT_FALSE. 2701 */ 2702 DPRINTF(5, ("force reset WAIT_REL, ")); 2703 finish_vt_rel(cur_scp, FALSE, &s); 2704 splx(s); 2705 DPRINTF(5, ("act as if VT_FALSE was seen\n")); 2706 return EINVAL; 2707 } 2708 } else if (cur_scp->status & SWITCH_WAIT_ACQ) { 2709 switch (sc->switch_in_progress++) { 2710 case 1: 2711 break; 2712 case 2: 2713 DPRINTF(5, ("sending acqsig again, ")); 2714 signal_vt_acq(cur_scp); 2715 break; 2716 case 3: 2717 break; 2718 case 4: 2719 default: 2720 /* clear the flag and finish the previous switch */ 2721 DPRINTF(5, ("force reset WAIT_ACQ, ")); 2722 finish_vt_acq(cur_scp); 2723 break; 2724 } 2725 } 2726 } 2727 } 2728 2729 /* 2730 * Return error if an invalid argument is given, or vty switch 2731 * is still in progress. 2732 */ 2733 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys) 2734 || sc->switch_in_progress) { 2735 splx(s); 2736 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION); 2737 DPRINTF(5, ("error 1\n")); 2738 return EINVAL; 2739 } 2740 2741 /* 2742 * Don't allow switching away from the graphics mode vty 2743 * if the switch mode is VT_AUTO, unless the next vty is the same 2744 * as the current or the current vty has been closed (but showing). 2745 */ 2746 tp = SC_DEV(sc, cur_scp->index); 2747 if ((cur_scp->index != next_scr) 2748 && tty_opened_ns(tp) 2749 && (cur_scp->smode.mode == VT_AUTO) 2750 && ISGRAPHSC(cur_scp)) { 2751 splx(s); 2752 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION); 2753 DPRINTF(5, ("error, graphics mode\n")); 2754 return EINVAL; 2755 } 2756 2757 /* 2758 * Is the wanted vty open? Don't allow switching to a closed vty. 2759 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode. 2760 * Note that we always allow the user to switch to the kernel 2761 * console even if it is closed. 2762 */ 2763 if ((sc_console == NULL) || (next_scr != sc_console->index)) { 2764 tp = SC_DEV(sc, next_scr); 2765 if (!tty_opened_ns(tp)) { 2766 splx(s); 2767 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION); 2768 DPRINTF(5, ("error 2, requested vty isn't open!\n")); 2769 return EINVAL; 2770 } 2771 if (kdb_active && SC_STAT(tp)->smode.mode == VT_PROCESS) { 2772 splx(s); 2773 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n")); 2774 return EINVAL; 2775 } 2776 } 2777 2778 /* this is the start of vty switching process... */ 2779 ++sc->switch_in_progress; 2780 sc->old_scp = cur_scp; 2781 sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr)); 2782 if (sc->new_scp == sc->old_scp) { 2783 sc->switch_in_progress = 0; 2784 /* 2785 * XXX wakeup() locks the scheduler lock which will hang if 2786 * the lock is in an in-between state, e.g., when we stop at 2787 * a breakpoint at fork_exit. It has always been wrong to call 2788 * wakeup() when the debugger is active. In RELENG_4, wakeup() 2789 * is supposed to be locked by splhigh(), but the debugger may 2790 * be invoked at splhigh(). 2791 */ 2792 if (!kdb_active) 2793 wakeup(VTY_WCHAN(sc,next_scr)); 2794 splx(s); 2795 DPRINTF(5, ("switch done (new == old)\n")); 2796 return 0; 2797 } 2798 2799 /* has controlling process died? */ 2800 vt_proc_alive(sc->old_scp); 2801 vt_proc_alive(sc->new_scp); 2802 2803 /* wait for the controlling process to release the screen, if necessary */ 2804 if (signal_vt_rel(sc->old_scp)) { 2805 splx(s); 2806 return 0; 2807 } 2808 2809 /* go set up the new vty screen */ 2810 splx(s); 2811 exchange_scr(sc); 2812 s = spltty(); 2813 2814 /* wake up processes waiting for this vty */ 2815 if (!kdb_active) 2816 wakeup(VTY_WCHAN(sc,next_scr)); 2817 2818 /* wait for the controlling process to acknowledge, if necessary */ 2819 if (signal_vt_acq(sc->cur_scp)) { 2820 splx(s); 2821 return 0; 2822 } 2823 2824 sc->switch_in_progress = 0; 2825 if (sc->unit == sc_console_unit) 2826 cnavailable(sc_consptr, TRUE); 2827 splx(s); 2828 DPRINTF(5, ("switch done\n")); 2829 2830 return 0; 2831 } 2832 2833 static int 2834 do_switch_scr(sc_softc_t *sc, int s) 2835 { 2836 vt_proc_alive(sc->new_scp); 2837 2838 splx(s); 2839 exchange_scr(sc); 2840 s = spltty(); 2841 /* sc->cur_scp == sc->new_scp */ 2842 wakeup(VTY_WCHAN(sc,sc->cur_scp->index)); 2843 2844 /* wait for the controlling process to acknowledge, if necessary */ 2845 if (!signal_vt_acq(sc->cur_scp)) { 2846 sc->switch_in_progress = 0; 2847 if (sc->unit == sc_console_unit) 2848 cnavailable(sc_consptr, TRUE); 2849 } 2850 2851 return s; 2852 } 2853 2854 static int 2855 vt_proc_alive(scr_stat *scp) 2856 { 2857 struct proc *p; 2858 2859 if (scp->proc) { 2860 if ((p = pfind(scp->pid)) != NULL) 2861 PROC_UNLOCK(p); 2862 if (scp->proc == p) 2863 return TRUE; 2864 scp->proc = NULL; 2865 scp->smode.mode = VT_AUTO; 2866 DPRINTF(5, ("vt controlling process %d died\n", scp->pid)); 2867 } 2868 return FALSE; 2869 } 2870 2871 static int 2872 signal_vt_rel(scr_stat *scp) 2873 { 2874 if (scp->smode.mode != VT_PROCESS) 2875 return FALSE; 2876 scp->status |= SWITCH_WAIT_REL; 2877 PROC_LOCK(scp->proc); 2878 kern_psignal(scp->proc, scp->smode.relsig); 2879 PROC_UNLOCK(scp->proc); 2880 DPRINTF(5, ("sending relsig to %d\n", scp->pid)); 2881 return TRUE; 2882 } 2883 2884 static int 2885 signal_vt_acq(scr_stat *scp) 2886 { 2887 if (scp->smode.mode != VT_PROCESS) 2888 return FALSE; 2889 if (scp->sc->unit == sc_console_unit) 2890 cnavailable(sc_consptr, FALSE); 2891 scp->status |= SWITCH_WAIT_ACQ; 2892 PROC_LOCK(scp->proc); 2893 kern_psignal(scp->proc, scp->smode.acqsig); 2894 PROC_UNLOCK(scp->proc); 2895 DPRINTF(5, ("sending acqsig to %d\n", scp->pid)); 2896 return TRUE; 2897 } 2898 2899 static int 2900 finish_vt_rel(scr_stat *scp, int release, int *s) 2901 { 2902 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) { 2903 scp->status &= ~SWITCH_WAIT_REL; 2904 if (release) 2905 *s = do_switch_scr(scp->sc, *s); 2906 else 2907 scp->sc->switch_in_progress = 0; 2908 return 0; 2909 } 2910 return EINVAL; 2911 } 2912 2913 static int 2914 finish_vt_acq(scr_stat *scp) 2915 { 2916 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) { 2917 scp->status &= ~SWITCH_WAIT_ACQ; 2918 scp->sc->switch_in_progress = 0; 2919 return 0; 2920 } 2921 return EINVAL; 2922 } 2923 2924 static void 2925 exchange_scr(sc_softc_t *sc) 2926 { 2927 scr_stat *scp; 2928 2929 /* save the current state of video and keyboard */ 2930 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos); 2931 if (!ISGRAPHSC(sc->old_scp)) 2932 sc_remove_cursor_image(sc->old_scp); 2933 if (sc->old_scp->kbd_mode == K_XLATE) 2934 save_kbd_state(sc->old_scp); 2935 2936 /* set up the video for the new screen */ 2937 scp = sc->cur_scp = sc->new_scp; 2938 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp)) 2939 set_mode(scp); 2940 #ifndef __sparc64__ 2941 else 2942 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, 2943 (void *)sc->adp->va_window, FALSE); 2944 #endif 2945 scp->status |= MOUSE_HIDDEN; 2946 sc_move_cursor(scp, scp->xpos, scp->ypos); 2947 if (!ISGRAPHSC(scp)) 2948 sc_set_cursor_image(scp); 2949 #ifndef SC_NO_PALETTE_LOADING 2950 if (ISGRAPHSC(sc->old_scp)) { 2951 #ifdef SC_PIXEL_MODE 2952 if (sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT) 2953 vidd_load_palette(sc->adp, sc->palette2); 2954 else 2955 #endif 2956 vidd_load_palette(sc->adp, sc->palette); 2957 } 2958 #endif 2959 sc_set_border(scp, scp->border); 2960 2961 /* set up the keyboard for the new screen */ 2962 if (sc->kbd_open_level == 0 && sc->old_scp->kbd_mode != scp->kbd_mode) 2963 (void)kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode); 2964 update_kbd_state(scp, scp->status, LOCK_MASK); 2965 2966 mark_all(scp); 2967 } 2968 2969 static void 2970 sc_puts(scr_stat *scp, u_char *buf, int len) 2971 { 2972 #ifdef DEV_SPLASH 2973 /* make screensaver happy */ 2974 if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only) 2975 run_scrn_saver = FALSE; 2976 #endif 2977 2978 if (scp->tsw) 2979 (*scp->tsw->te_puts)(scp, buf, len); 2980 if (scp->sc->delayed_next_scr) 2981 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1); 2982 } 2983 2984 void 2985 sc_draw_cursor_image(scr_stat *scp) 2986 { 2987 /* assert(scp == scp->sc->cur_scp); */ 2988 SC_VIDEO_LOCK(scp->sc); 2989 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, 2990 scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE, 2991 sc_inside_cutmark(scp, scp->cursor_pos)); 2992 scp->cursor_oldpos = scp->cursor_pos; 2993 SC_VIDEO_UNLOCK(scp->sc); 2994 } 2995 2996 void 2997 sc_remove_cursor_image(scr_stat *scp) 2998 { 2999 /* assert(scp == scp->sc->cur_scp); */ 3000 SC_VIDEO_LOCK(scp->sc); 3001 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, 3002 scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE, 3003 sc_inside_cutmark(scp, scp->cursor_oldpos)); 3004 SC_VIDEO_UNLOCK(scp->sc); 3005 } 3006 3007 static void 3008 update_cursor_image(scr_stat *scp) 3009 { 3010 /* assert(scp == scp->sc->cur_scp); */ 3011 sc_remove_cursor_image(scp); 3012 sc_set_cursor_image(scp); 3013 sc_draw_cursor_image(scp); 3014 } 3015 3016 void 3017 sc_set_cursor_image(scr_stat *scp) 3018 { 3019 scp->curs_attr = scp->base_curs_attr; 3020 if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) { 3021 /* hidden cursor is internally represented as zero-height underline */ 3022 scp->curs_attr.flags = CONS_CHAR_CURSOR; 3023 scp->curs_attr.base = scp->curs_attr.height = 0; 3024 } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) { 3025 scp->curs_attr.base = imin(scp->base_curs_attr.base, 3026 scp->font_size - 1); 3027 scp->curs_attr.height = imin(scp->base_curs_attr.height, 3028 scp->font_size - scp->curs_attr.base); 3029 } else { /* block cursor */ 3030 scp->curs_attr.base = 0; 3031 scp->curs_attr.height = scp->font_size; 3032 } 3033 3034 /* assert(scp == scp->sc->cur_scp); */ 3035 SC_VIDEO_LOCK(scp->sc); 3036 (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height, 3037 scp->curs_attr.flags & CONS_BLINK_CURSOR); 3038 SC_VIDEO_UNLOCK(scp->sc); 3039 } 3040 3041 static void 3042 sc_adjust_ca(struct cursor_attr *cap, int flags, int base, int height) 3043 { 3044 if (flags & CONS_CHARCURSOR_COLORS) { 3045 cap->bg[0] = base & 0xff; 3046 cap->bg[1] = height & 0xff; 3047 } else if (flags & CONS_MOUSECURSOR_COLORS) { 3048 cap->mouse_ba = base & 0xff; 3049 cap->mouse_ia = height & 0xff; 3050 } else { 3051 if (base >= 0) 3052 cap->base = base; 3053 if (height >= 0) 3054 cap->height = height; 3055 if (!(flags & CONS_SHAPEONLY_CURSOR)) 3056 cap->flags = flags & CONS_CURSOR_ATTRS; 3057 } 3058 } 3059 3060 static void 3061 change_cursor_shape(scr_stat *scp, int flags, int base, int height) 3062 { 3063 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) 3064 sc_remove_cursor_image(scp); 3065 3066 if (flags & CONS_RESET_CURSOR) 3067 scp->base_curs_attr = scp->dflt_curs_attr; 3068 else if (flags & CONS_DEFAULT_CURSOR) { 3069 sc_adjust_ca(&scp->dflt_curs_attr, flags, base, height); 3070 scp->base_curs_attr = scp->dflt_curs_attr; 3071 } else 3072 sc_adjust_ca(&scp->base_curs_attr, flags, base, height); 3073 3074 if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) { 3075 sc_set_cursor_image(scp); 3076 sc_draw_cursor_image(scp); 3077 } 3078 } 3079 3080 void 3081 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height) 3082 { 3083 sc_softc_t *sc; 3084 struct tty *tp; 3085 int s; 3086 int i; 3087 3088 if (flags == -1) 3089 flags = CONS_SHAPEONLY_CURSOR; 3090 3091 s = spltty(); 3092 if (flags & CONS_LOCAL_CURSOR) { 3093 /* local (per vty) change */ 3094 change_cursor_shape(scp, flags, base, height); 3095 splx(s); 3096 return; 3097 } 3098 3099 /* global change */ 3100 sc = scp->sc; 3101 if (flags & CONS_RESET_CURSOR) 3102 sc->curs_attr = sc->dflt_curs_attr; 3103 else if (flags & CONS_DEFAULT_CURSOR) { 3104 sc_adjust_ca(&sc->dflt_curs_attr, flags, base, height); 3105 sc->curs_attr = sc->dflt_curs_attr; 3106 } else 3107 sc_adjust_ca(&sc->curs_attr, flags, base, height); 3108 3109 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) { 3110 if ((tp = SC_DEV(sc, i)) == NULL) 3111 continue; 3112 if ((scp = sc_get_stat(tp)) == NULL) 3113 continue; 3114 scp->dflt_curs_attr = sc->curs_attr; 3115 change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1); 3116 } 3117 splx(s); 3118 } 3119 3120 static void 3121 scinit(int unit, int flags) 3122 { 3123 3124 /* 3125 * When syscons is being initialized as the kernel console, malloc() 3126 * is not yet functional, because various kernel structures has not been 3127 * fully initialized yet. Therefore, we need to declare the following 3128 * static buffers for the console. This is less than ideal, 3129 * but is necessry evil for the time being. XXX 3130 */ 3131 static u_short sc_buffer[ROW*COL]; /* XXX */ 3132 #ifndef SC_NO_FONT_LOADING 3133 static u_char font_8[256*8]; 3134 static u_char font_14[256*14]; 3135 static u_char font_16[256*16]; 3136 #endif 3137 3138 sc_softc_t *sc; 3139 scr_stat *scp; 3140 video_adapter_t *adp; 3141 int col; 3142 int row; 3143 int i; 3144 3145 /* one time initialization */ 3146 if (init_done == COLD) { 3147 sc_get_bios_values(&bios_value); 3148 for (i = 0; i < nitems(sc_kattrtab); i++) { 3149 #if SC_KERNEL_CONS_ATTR == FG_WHITE 3150 sc_kattrtab[i] = 8 + (i + FG_WHITE) % 8U; 3151 #else 3152 sc_kattrtab[i] = SC_KERNEL_CONS_ATTR; 3153 #endif 3154 } 3155 } 3156 init_done = WARM; 3157 3158 /* 3159 * Allocate resources. Even if we are being called for the second 3160 * time, we must allocate them again, because they might have 3161 * disappeared... 3162 */ 3163 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE); 3164 if ((sc->flags & SC_INIT_DONE) == 0) 3165 SC_VIDEO_LOCKINIT(sc); 3166 3167 adp = NULL; 3168 if (sc->adapter >= 0) { 3169 vid_release(sc->adp, (void *)&sc->adapter); 3170 adp = sc->adp; 3171 sc->adp = NULL; 3172 } 3173 if (sc->keyboard >= 0) { 3174 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard)); 3175 i = kbd_release(sc->kbd, (void *)&sc->keyboard); 3176 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i)); 3177 if (sc->kbd != NULL) { 3178 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n", 3179 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags)); 3180 } 3181 sc->kbd = NULL; 3182 } 3183 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter); 3184 sc->adp = vid_get_adapter(sc->adapter); 3185 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */ 3186 3187 sc->keyboard = sc_allocate_keyboard(sc, unit); 3188 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard)); 3189 3190 sc->kbd = kbd_get_keyboard(sc->keyboard); 3191 if (sc->kbd != NULL) { 3192 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n", 3193 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags)); 3194 } 3195 3196 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) { 3197 3198 sc->initial_mode = sc->adp->va_initial_mode; 3199 3200 #ifndef SC_NO_FONT_LOADING 3201 if (flags & SC_KERNEL_CONSOLE) { 3202 sc->font_8 = font_8; 3203 sc->font_14 = font_14; 3204 sc->font_16 = font_16; 3205 } else if (sc->font_8 == NULL) { 3206 /* assert(sc_malloc) */ 3207 sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK); 3208 sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK); 3209 sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK); 3210 } 3211 #endif 3212 3213 /* extract the hardware cursor location and hide the cursor for now */ 3214 vidd_read_hw_cursor(sc->adp, &col, &row); 3215 vidd_set_hw_cursor(sc->adp, -1, -1); 3216 3217 /* set up the first console */ 3218 sc->first_vty = unit*MAXCONS; 3219 sc->vtys = MAXCONS; /* XXX: should be configurable */ 3220 if (flags & SC_KERNEL_CONSOLE) { 3221 /* 3222 * Set up devs structure but don't use it yet, calling make_dev() 3223 * might panic kernel. Wait for sc_attach_unit() to actually 3224 * create the devices. 3225 */ 3226 sc->dev = main_devs; 3227 scp = &main_console; 3228 init_scp(sc, sc->first_vty, scp); 3229 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize, 3230 (void *)sc_buffer, FALSE); 3231 if (sc_init_emulator(scp, SC_DFLT_TERM)) 3232 sc_init_emulator(scp, "*"); 3233 (*scp->tsw->te_default_attr)(scp, SC_KERNEL_CONS_ATTR, 3234 SC_KERNEL_CONS_REV_ATTR); 3235 } else { 3236 /* assert(sc_malloc) */ 3237 sc->dev = malloc(sizeof(struct tty *)*sc->vtys, M_DEVBUF, 3238 M_WAITOK|M_ZERO); 3239 sc->dev[0] = sc_alloc_tty(0, unit * MAXCONS); 3240 scp = alloc_scp(sc, sc->first_vty); 3241 SC_STAT(sc->dev[0]) = scp; 3242 } 3243 sc->cur_scp = scp; 3244 3245 #ifndef __sparc64__ 3246 /* copy screen to temporary buffer */ 3247 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, 3248 (void *)scp->sc->adp->va_window, FALSE); 3249 if (ISTEXTSC(scp)) 3250 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize); 3251 #endif 3252 3253 /* Sync h/w cursor position to s/w (sc and teken). */ 3254 if (col >= scp->xsize) 3255 col = 0; 3256 if (row >= scp->ysize) 3257 row = scp->ysize - 1; 3258 scp->xpos = col; 3259 scp->ypos = row; 3260 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col; 3261 (*scp->tsw->te_sync)(scp); 3262 3263 sc->dflt_curs_attr.base = 0; 3264 sc->dflt_curs_attr.height = howmany(scp->font_size, 8); 3265 sc->dflt_curs_attr.flags = 0; 3266 sc->dflt_curs_attr.bg[0] = FG_RED; 3267 sc->dflt_curs_attr.bg[1] = FG_LIGHTGREY; 3268 sc->dflt_curs_attr.bg[2] = FG_BLUE; 3269 sc->dflt_curs_attr.mouse_ba = FG_WHITE; 3270 sc->dflt_curs_attr.mouse_ia = FG_RED; 3271 sc->curs_attr = sc->dflt_curs_attr; 3272 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr; 3273 scp->curs_attr = scp->base_curs_attr; 3274 3275 #ifndef SC_NO_SYSMOUSE 3276 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2); 3277 #endif 3278 if (!ISGRAPHSC(scp)) { 3279 sc_set_cursor_image(scp); 3280 sc_draw_cursor_image(scp); 3281 } 3282 3283 /* save font and palette */ 3284 #ifndef SC_NO_FONT_LOADING 3285 sc->fonts_loaded = 0; 3286 if (ISFONTAVAIL(sc->adp->va_flags)) { 3287 #ifdef SC_DFLT_FONT 3288 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8)); 3289 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14)); 3290 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16)); 3291 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8; 3292 if (scp->font_size < 14) { 3293 sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256); 3294 } else if (scp->font_size >= 16) { 3295 sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256); 3296 } else { 3297 sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256); 3298 } 3299 #else /* !SC_DFLT_FONT */ 3300 if (scp->font_size < 14) { 3301 sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256); 3302 sc->fonts_loaded = FONT_8; 3303 } else if (scp->font_size >= 16) { 3304 sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256); 3305 sc->fonts_loaded = FONT_16; 3306 } else { 3307 sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256); 3308 sc->fonts_loaded = FONT_14; 3309 } 3310 #endif /* SC_DFLT_FONT */ 3311 /* FONT KLUDGE: always use the font page #0. XXX */ 3312 sc_show_font(scp, 0); 3313 } 3314 #endif /* !SC_NO_FONT_LOADING */ 3315 3316 #ifndef SC_NO_PALETTE_LOADING 3317 vidd_save_palette(sc->adp, sc->palette); 3318 #ifdef SC_PIXEL_MODE 3319 for (i = 0; i < sizeof(sc->palette2); i++) 3320 sc->palette2[i] = i / 3; 3321 #endif 3322 #endif 3323 3324 #ifdef DEV_SPLASH 3325 if (!(sc->flags & SC_SPLASH_SCRN)) { 3326 /* we are ready to put up the splash image! */ 3327 splash_init(sc->adp, scsplash_callback, sc); 3328 sc->flags |= SC_SPLASH_SCRN; 3329 } 3330 #endif 3331 } 3332 3333 /* the rest is not necessary, if we have done it once */ 3334 if (sc->flags & SC_INIT_DONE) 3335 return; 3336 3337 /* initialize mapscrn arrays to a one to one map */ 3338 for (i = 0; i < sizeof(sc->scr_map); i++) 3339 sc->scr_map[i] = sc->scr_rmap[i] = i; 3340 3341 sc->flags |= SC_INIT_DONE; 3342 } 3343 3344 static void 3345 scterm(int unit, int flags) 3346 { 3347 sc_softc_t *sc; 3348 scr_stat *scp; 3349 3350 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE); 3351 if (sc == NULL) 3352 return; /* shouldn't happen */ 3353 3354 #ifdef DEV_SPLASH 3355 /* this console is no longer available for the splash screen */ 3356 if (sc->flags & SC_SPLASH_SCRN) { 3357 splash_term(sc->adp); 3358 sc->flags &= ~SC_SPLASH_SCRN; 3359 } 3360 #endif 3361 3362 #if 0 /* XXX */ 3363 /* move the hardware cursor to the upper-left corner */ 3364 vidd_set_hw_cursor(sc->adp, 0, 0); 3365 #endif 3366 3367 /* release the keyboard and the video card */ 3368 if (sc->keyboard >= 0) 3369 kbd_release(sc->kbd, &sc->keyboard); 3370 if (sc->adapter >= 0) 3371 vid_release(sc->adp, &sc->adapter); 3372 3373 /* stop the terminal emulator, if any */ 3374 scp = sc_get_stat(sc->dev[0]); 3375 if (scp->tsw) 3376 (*scp->tsw->te_term)(scp, &scp->ts); 3377 mtx_destroy(&sc->video_mtx); 3378 3379 /* clear the structure */ 3380 if (!(flags & SC_KERNEL_CONSOLE)) { 3381 free(scp->ts, M_DEVBUF); 3382 /* XXX: We need delete_dev() for this */ 3383 free(sc->dev, M_DEVBUF); 3384 #if 0 3385 /* XXX: We need a ttyunregister for this */ 3386 free(sc->tty, M_DEVBUF); 3387 #endif 3388 #ifndef SC_NO_FONT_LOADING 3389 free(sc->font_8, M_DEVBUF); 3390 free(sc->font_14, M_DEVBUF); 3391 free(sc->font_16, M_DEVBUF); 3392 #endif 3393 /* XXX vtb, history */ 3394 } 3395 bzero(sc, sizeof(*sc)); 3396 sc->keyboard = -1; 3397 sc->adapter = -1; 3398 } 3399 3400 static void 3401 scshutdown(__unused void *arg, __unused int howto) 3402 { 3403 3404 KASSERT(sc_console != NULL, ("sc_console != NULL")); 3405 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL")); 3406 KASSERT(sc_console->sc->cur_scp != NULL, 3407 ("sc_console->sc->cur_scp != NULL")); 3408 3409 sc_touch_scrn_saver(); 3410 if (!cold && 3411 sc_console->sc->cur_scp->index != sc_console->index && 3412 sc_console->sc->cur_scp->smode.mode == VT_AUTO && 3413 sc_console->smode.mode == VT_AUTO) 3414 sc_switch_scr(sc_console->sc, sc_console->index); 3415 shutdown_in_progress = TRUE; 3416 } 3417 3418 static void 3419 scsuspend(__unused void *arg) 3420 { 3421 int retry; 3422 3423 KASSERT(sc_console != NULL, ("sc_console != NULL")); 3424 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL")); 3425 KASSERT(sc_console->sc->cur_scp != NULL, 3426 ("sc_console->sc->cur_scp != NULL")); 3427 3428 sc_susp_scr = sc_console->sc->cur_scp->index; 3429 if (sc_no_suspend_vtswitch || 3430 sc_susp_scr == sc_console->index) { 3431 sc_touch_scrn_saver(); 3432 sc_susp_scr = -1; 3433 return; 3434 } 3435 for (retry = 0; retry < 10; retry++) { 3436 sc_switch_scr(sc_console->sc, sc_console->index); 3437 if (!sc_console->sc->switch_in_progress) 3438 break; 3439 pause("scsuspend", hz); 3440 } 3441 suspend_in_progress = TRUE; 3442 } 3443 3444 static void 3445 scresume(__unused void *arg) 3446 { 3447 3448 KASSERT(sc_console != NULL, ("sc_console != NULL")); 3449 KASSERT(sc_console->sc != NULL, ("sc_console->sc != NULL")); 3450 KASSERT(sc_console->sc->cur_scp != NULL, 3451 ("sc_console->sc->cur_scp != NULL")); 3452 3453 suspend_in_progress = FALSE; 3454 if (sc_susp_scr < 0) { 3455 update_font(sc_console->sc->cur_scp); 3456 return; 3457 } 3458 sc_switch_scr(sc_console->sc, sc_susp_scr); 3459 } 3460 3461 int 3462 sc_clean_up(scr_stat *scp) 3463 { 3464 #ifdef DEV_SPLASH 3465 int error; 3466 #endif 3467 3468 if (scp->sc->flags & SC_SCRN_BLANKED) { 3469 sc_touch_scrn_saver(); 3470 #ifdef DEV_SPLASH 3471 if ((error = wait_scrn_saver_stop(scp->sc))) 3472 return error; 3473 #endif 3474 } 3475 scp->status |= MOUSE_HIDDEN; 3476 sc_remove_mouse_image(scp); 3477 sc_remove_cutmarking(scp); 3478 return 0; 3479 } 3480 3481 void 3482 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard) 3483 { 3484 sc_vtb_t new; 3485 sc_vtb_t old; 3486 3487 old = scp->vtb; 3488 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait); 3489 if (!discard && (old.vtb_flags & VTB_VALID)) { 3490 /* retain the current cursor position and buffer contants */ 3491 scp->cursor_oldpos = scp->cursor_pos; 3492 /* 3493 * This works only if the old buffer has the same size as or larger 3494 * than the new one. XXX 3495 */ 3496 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize); 3497 scp->vtb = new; 3498 } else { 3499 scp->vtb = new; 3500 sc_vtb_destroy(&old); 3501 } 3502 3503 #ifndef SC_NO_SYSMOUSE 3504 /* move the mouse cursor at the center of the screen */ 3505 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2); 3506 #endif 3507 } 3508 3509 static scr_stat 3510 *alloc_scp(sc_softc_t *sc, int vty) 3511 { 3512 scr_stat *scp; 3513 3514 /* assert(sc_malloc) */ 3515 3516 scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK); 3517 init_scp(sc, vty, scp); 3518 3519 sc_alloc_scr_buffer(scp, TRUE, TRUE); 3520 if (sc_init_emulator(scp, SC_DFLT_TERM)) 3521 sc_init_emulator(scp, "*"); 3522 3523 #ifndef SC_NO_CUTPASTE 3524 sc_alloc_cut_buffer(scp, TRUE); 3525 #endif 3526 3527 #ifndef SC_NO_HISTORY 3528 sc_alloc_history_buffer(scp, 0, 0, TRUE); 3529 #endif 3530 3531 return scp; 3532 } 3533 3534 static void 3535 init_scp(sc_softc_t *sc, int vty, scr_stat *scp) 3536 { 3537 video_info_t info; 3538 3539 bzero(scp, sizeof(*scp)); 3540 3541 scp->index = vty; 3542 scp->sc = sc; 3543 scp->status = 0; 3544 scp->mode = sc->initial_mode; 3545 vidd_get_info(sc->adp, scp->mode, &info); 3546 if (info.vi_flags & V_INFO_GRAPHICS) { 3547 scp->status |= GRAPHICS_MODE; 3548 scp->xpixel = info.vi_width; 3549 scp->ypixel = info.vi_height; 3550 scp->xsize = info.vi_width/info.vi_cwidth; 3551 scp->ysize = info.vi_height/info.vi_cheight; 3552 scp->font_size = 0; 3553 scp->font = NULL; 3554 } else { 3555 scp->xsize = info.vi_width; 3556 scp->ysize = info.vi_height; 3557 scp->xpixel = scp->xsize*info.vi_cwidth; 3558 scp->ypixel = scp->ysize*info.vi_cheight; 3559 } 3560 3561 scp->font_size = info.vi_cheight; 3562 scp->font_width = info.vi_cwidth; 3563 #ifndef SC_NO_FONT_LOADING 3564 if (info.vi_cheight < 14) 3565 scp->font = sc->font_8; 3566 else if (info.vi_cheight >= 16) 3567 scp->font = sc->font_16; 3568 else 3569 scp->font = sc->font_14; 3570 #else 3571 scp->font = NULL; 3572 #endif 3573 3574 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE); 3575 #ifndef __sparc64__ 3576 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE); 3577 #endif 3578 scp->xoff = scp->yoff = 0; 3579 scp->xpos = scp->ypos = 0; 3580 scp->start = scp->xsize * scp->ysize - 1; 3581 scp->end = 0; 3582 scp->tsw = NULL; 3583 scp->ts = NULL; 3584 scp->rndr = NULL; 3585 scp->border = (SC_NORM_ATTR >> 4) & 0x0f; 3586 scp->base_curs_attr = scp->dflt_curs_attr = sc->curs_attr; 3587 scp->mouse_cut_start = scp->xsize*scp->ysize; 3588 scp->mouse_cut_end = -1; 3589 scp->mouse_signal = 0; 3590 scp->mouse_pid = 0; 3591 scp->mouse_proc = NULL; 3592 scp->kbd_mode = K_XLATE; 3593 scp->bell_pitch = bios_value.bell_pitch; 3594 scp->bell_duration = BELL_DURATION; 3595 scp->status |= (bios_value.shift_state & NLKED); 3596 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN; 3597 scp->pid = 0; 3598 scp->proc = NULL; 3599 scp->smode.mode = VT_AUTO; 3600 scp->history = NULL; 3601 scp->history_pos = 0; 3602 scp->history_size = 0; 3603 } 3604 3605 int 3606 sc_init_emulator(scr_stat *scp, char *name) 3607 { 3608 sc_term_sw_t *sw; 3609 sc_rndr_sw_t *rndr; 3610 void *p; 3611 int error; 3612 3613 if (name == NULL) /* if no name is given, use the current emulator */ 3614 sw = scp->tsw; 3615 else /* ...otherwise find the named emulator */ 3616 sw = sc_term_match(name); 3617 if (sw == NULL) 3618 return EINVAL; 3619 3620 rndr = NULL; 3621 if (strcmp(sw->te_renderer, "*") != 0) { 3622 rndr = sc_render_match(scp, sw->te_renderer, 3623 scp->status & (GRAPHICS_MODE | PIXEL_MODE)); 3624 } 3625 if (rndr == NULL) { 3626 rndr = sc_render_match(scp, scp->sc->adp->va_name, 3627 scp->status & (GRAPHICS_MODE | PIXEL_MODE)); 3628 if (rndr == NULL) 3629 return ENODEV; 3630 } 3631 3632 if (sw == scp->tsw) { 3633 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT); 3634 scp->rndr = rndr; 3635 scp->rndr->init(scp); 3636 sc_clear_screen(scp); 3637 /* assert(error == 0); */ 3638 return error; 3639 } 3640 3641 if (sc_malloc && (sw->te_size > 0)) 3642 p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT); 3643 else 3644 p = NULL; 3645 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT); 3646 if (error) 3647 return error; 3648 3649 if (scp->tsw) 3650 (*scp->tsw->te_term)(scp, &scp->ts); 3651 if (scp->ts != NULL) 3652 free(scp->ts, M_DEVBUF); 3653 scp->tsw = sw; 3654 scp->ts = p; 3655 scp->rndr = rndr; 3656 scp->rndr->init(scp); 3657 3658 (*sw->te_default_attr)(scp, SC_NORM_ATTR, SC_NORM_REV_ATTR); 3659 sc_clear_screen(scp); 3660 3661 return 0; 3662 } 3663 3664 /* 3665 * scgetc(flags) - get character from keyboard. 3666 * If flags & SCGETC_CN, then avoid harmful side effects. 3667 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else 3668 * return NOKEY if there is nothing there. 3669 */ 3670 static u_int 3671 scgetc(sc_softc_t *sc, u_int flags, struct sc_cnstate *sp) 3672 { 3673 scr_stat *scp; 3674 #ifndef SC_NO_HISTORY 3675 struct tty *tp; 3676 #endif 3677 u_int c; 3678 int this_scr; 3679 int f; 3680 int i; 3681 3682 if (sc->kbd == NULL) 3683 return NOKEY; 3684 3685 next_code: 3686 #if 1 3687 /* I don't like this, but... XXX */ 3688 if (flags & SCGETC_CN) 3689 sccnupdate(sc->cur_scp); 3690 #endif 3691 scp = sc->cur_scp; 3692 /* first see if there is something in the keyboard port */ 3693 for (;;) { 3694 if (flags & SCGETC_CN) 3695 sccnscrunlock(sc, sp); 3696 c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK)); 3697 if (flags & SCGETC_CN) 3698 sccnscrlock(sc, sp); 3699 if (c == ERRKEY) { 3700 if (!(flags & SCGETC_CN)) 3701 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); 3702 } else if (c == NOKEY) 3703 return c; 3704 else 3705 break; 3706 } 3707 3708 /* make screensaver happy */ 3709 if (!(c & RELKEY)) 3710 sc_touch_scrn_saver(); 3711 3712 if (!(flags & SCGETC_CN)) 3713 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD); 3714 3715 if (sc->kbd_open_level == 0 && scp->kbd_mode != K_XLATE) 3716 return KEYCHAR(c); 3717 3718 /* if scroll-lock pressed allow history browsing */ 3719 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) { 3720 3721 scp->status &= ~CURSOR_ENABLED; 3722 sc_remove_cursor_image(scp); 3723 3724 #ifndef SC_NO_HISTORY 3725 if (!(scp->status & BUFFER_SAVED)) { 3726 scp->status |= BUFFER_SAVED; 3727 sc_hist_save(scp); 3728 } 3729 switch (c) { 3730 /* FIXME: key codes */ 3731 case SPCLKEY | FKEY | F(49): /* home key */ 3732 sc_remove_cutmarking(scp); 3733 sc_hist_home(scp); 3734 goto next_code; 3735 3736 case SPCLKEY | FKEY | F(57): /* end key */ 3737 sc_remove_cutmarking(scp); 3738 sc_hist_end(scp); 3739 goto next_code; 3740 3741 case SPCLKEY | FKEY | F(50): /* up arrow key */ 3742 sc_remove_cutmarking(scp); 3743 if (sc_hist_up_line(scp)) 3744 if (!(flags & SCGETC_CN)) 3745 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); 3746 goto next_code; 3747 3748 case SPCLKEY | FKEY | F(58): /* down arrow key */ 3749 sc_remove_cutmarking(scp); 3750 if (sc_hist_down_line(scp)) 3751 if (!(flags & SCGETC_CN)) 3752 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); 3753 goto next_code; 3754 3755 case SPCLKEY | FKEY | F(51): /* page up key */ 3756 sc_remove_cutmarking(scp); 3757 for (i=0; i<scp->ysize; i++) 3758 if (sc_hist_up_line(scp)) { 3759 if (!(flags & SCGETC_CN)) 3760 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); 3761 break; 3762 } 3763 goto next_code; 3764 3765 case SPCLKEY | FKEY | F(59): /* page down key */ 3766 sc_remove_cutmarking(scp); 3767 for (i=0; i<scp->ysize; i++) 3768 if (sc_hist_down_line(scp)) { 3769 if (!(flags & SCGETC_CN)) 3770 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION); 3771 break; 3772 } 3773 goto next_code; 3774 } 3775 #endif /* SC_NO_HISTORY */ 3776 } 3777 3778 /* 3779 * Process and consume special keys here. Return a plain char code 3780 * or a char code with the META flag or a function key code. 3781 */ 3782 if (c & RELKEY) { 3783 /* key released */ 3784 /* goto next_code */ 3785 } else { 3786 /* key pressed */ 3787 if (c & SPCLKEY) { 3788 c &= ~SPCLKEY; 3789 switch (KEYCHAR(c)) { 3790 /* LOCKING KEYS */ 3791 case NLK: case CLK: case ALK: 3792 break; 3793 case SLK: 3794 (void)kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f); 3795 if (f & SLKED) { 3796 scp->status |= SLKED; 3797 } else { 3798 if (scp->status & SLKED) { 3799 scp->status &= ~SLKED; 3800 #ifndef SC_NO_HISTORY 3801 if (scp->status & BUFFER_SAVED) { 3802 if (!sc_hist_restore(scp)) 3803 sc_remove_cutmarking(scp); 3804 scp->status &= ~BUFFER_SAVED; 3805 scp->status |= CURSOR_ENABLED; 3806 sc_draw_cursor_image(scp); 3807 } 3808 /* Only safe in Giant-locked context. */ 3809 tp = SC_DEV(sc, scp->index); 3810 if (!(flags & SCGETC_CN) && tty_opened_ns(tp)) 3811 sctty_outwakeup(tp); 3812 #endif 3813 } 3814 } 3815 break; 3816 3817 case PASTE: 3818 #ifndef SC_NO_CUTPASTE 3819 sc_mouse_paste(scp); 3820 #endif 3821 break; 3822 3823 /* NON-LOCKING KEYS */ 3824 case NOP: 3825 case LSH: case RSH: case LCTR: case RCTR: 3826 case LALT: case RALT: case ASH: case META: 3827 break; 3828 3829 case BTAB: 3830 if (!(sc->flags & SC_SCRN_BLANKED)) 3831 return c; 3832 break; 3833 3834 case SPSC: 3835 #ifdef DEV_SPLASH 3836 /* force activatation/deactivation of the screen saver */ 3837 if (!(sc->flags & SC_SCRN_BLANKED)) { 3838 run_scrn_saver = TRUE; 3839 sc->scrn_time_stamp -= scrn_blank_time; 3840 } 3841 if (cold) { 3842 /* 3843 * While devices are being probed, the screen saver need 3844 * to be invoked explicitly. XXX 3845 */ 3846 if (sc->flags & SC_SCRN_BLANKED) { 3847 scsplash_stick(FALSE); 3848 stop_scrn_saver(sc, current_saver); 3849 } else { 3850 if (!ISGRAPHSC(scp)) { 3851 scsplash_stick(TRUE); 3852 (*current_saver)(sc, TRUE); 3853 } 3854 } 3855 } 3856 #endif /* DEV_SPLASH */ 3857 break; 3858 3859 case RBT: 3860 #ifndef SC_DISABLE_REBOOT 3861 if (enable_reboot && !(flags & SCGETC_CN)) 3862 shutdown_nice(0); 3863 #endif 3864 break; 3865 3866 case HALT: 3867 #ifndef SC_DISABLE_REBOOT 3868 if (enable_reboot && !(flags & SCGETC_CN)) 3869 shutdown_nice(RB_HALT); 3870 #endif 3871 break; 3872 3873 case PDWN: 3874 #ifndef SC_DISABLE_REBOOT 3875 if (enable_reboot && !(flags & SCGETC_CN)) 3876 shutdown_nice(RB_HALT|RB_POWEROFF); 3877 #endif 3878 break; 3879 3880 case SUSP: 3881 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND); 3882 break; 3883 case STBY: 3884 power_pm_suspend(POWER_SLEEP_STATE_STANDBY); 3885 break; 3886 3887 case DBG: 3888 #ifndef SC_DISABLE_KDBKEY 3889 if (enable_kdbkey) 3890 kdb_break(); 3891 #endif 3892 break; 3893 3894 case PNC: 3895 if (enable_panic_key) 3896 panic("Forced by the panic key"); 3897 break; 3898 3899 case NEXT: 3900 this_scr = scp->index; 3901 for (i = (this_scr - sc->first_vty + 1)%sc->vtys; 3902 sc->first_vty + i != this_scr; 3903 i = (i + 1)%sc->vtys) { 3904 struct tty *tp = SC_DEV(sc, sc->first_vty + i); 3905 if (tty_opened_ns(tp)) { 3906 sc_switch_scr(scp->sc, sc->first_vty + i); 3907 break; 3908 } 3909 } 3910 break; 3911 3912 case PREV: 3913 this_scr = scp->index; 3914 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys; 3915 sc->first_vty + i != this_scr; 3916 i = (i + sc->vtys - 1)%sc->vtys) { 3917 struct tty *tp = SC_DEV(sc, sc->first_vty + i); 3918 if (tty_opened_ns(tp)) { 3919 sc_switch_scr(scp->sc, sc->first_vty + i); 3920 break; 3921 } 3922 } 3923 break; 3924 3925 default: 3926 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) { 3927 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR); 3928 break; 3929 } 3930 /* assert(c & FKEY) */ 3931 if (!(sc->flags & SC_SCRN_BLANKED)) 3932 return c; 3933 break; 3934 } 3935 /* goto next_code */ 3936 } else { 3937 /* regular keys (maybe MKEY is set) */ 3938 #if !defined(SC_DISABLE_KDBKEY) && defined(KDB) 3939 if (enable_kdbkey) 3940 kdb_alt_break(c, &sc->sc_altbrk); 3941 #endif 3942 if (!(sc->flags & SC_SCRN_BLANKED)) 3943 return c; 3944 } 3945 } 3946 3947 goto next_code; 3948 } 3949 3950 static int 3951 sctty_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr, 3952 int nprot, vm_memattr_t *memattr) 3953 { 3954 scr_stat *scp; 3955 3956 scp = sc_get_stat(tp); 3957 if (scp != scp->sc->cur_scp) 3958 return -1; 3959 return vidd_mmap(scp->sc->adp, offset, paddr, nprot, memattr); 3960 } 3961 3962 static void 3963 update_font(scr_stat *scp) 3964 { 3965 #ifndef SC_NO_FONT_LOADING 3966 /* load appropriate font */ 3967 if (!(scp->status & GRAPHICS_MODE)) { 3968 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) { 3969 if (scp->font_size < 14) { 3970 if (scp->sc->fonts_loaded & FONT_8) 3971 sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256); 3972 } else if (scp->font_size >= 16) { 3973 if (scp->sc->fonts_loaded & FONT_16) 3974 sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256); 3975 } else { 3976 if (scp->sc->fonts_loaded & FONT_14) 3977 sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256); 3978 } 3979 /* 3980 * FONT KLUDGE: 3981 * This is an interim kludge to display correct font. 3982 * Always use the font page #0 on the video plane 2. 3983 * Somehow we cannot show the font in other font pages on 3984 * some video cards... XXX 3985 */ 3986 sc_show_font(scp, 0); 3987 } 3988 mark_all(scp); 3989 } 3990 #endif /* !SC_NO_FONT_LOADING */ 3991 } 3992 3993 static int 3994 save_kbd_state(scr_stat *scp) 3995 { 3996 int state; 3997 int error; 3998 3999 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state); 4000 if (error == ENOIOCTL) 4001 error = ENODEV; 4002 if (error == 0) { 4003 scp->status &= ~LOCK_MASK; 4004 scp->status |= state; 4005 } 4006 return error; 4007 } 4008 4009 static int 4010 update_kbd_state(scr_stat *scp, int new_bits, int mask) 4011 { 4012 int state; 4013 int error; 4014 4015 if (mask != LOCK_MASK) { 4016 error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state); 4017 if (error == ENOIOCTL) 4018 error = ENODEV; 4019 if (error) 4020 return error; 4021 state &= ~mask; 4022 state |= new_bits & mask; 4023 } else { 4024 state = new_bits & LOCK_MASK; 4025 } 4026 error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state); 4027 if (error == ENOIOCTL) 4028 error = ENODEV; 4029 return error; 4030 } 4031 4032 static int 4033 update_kbd_leds(scr_stat *scp, int which) 4034 { 4035 int error; 4036 4037 which &= LOCK_MASK; 4038 error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which); 4039 if (error == ENOIOCTL) 4040 error = ENODEV; 4041 return error; 4042 } 4043 4044 int 4045 set_mode(scr_stat *scp) 4046 { 4047 video_info_t info; 4048 4049 /* reject unsupported mode */ 4050 if (vidd_get_info(scp->sc->adp, scp->mode, &info)) 4051 return 1; 4052 4053 /* if this vty is not currently showing, do nothing */ 4054 if (scp != scp->sc->cur_scp) 4055 return 0; 4056 4057 /* setup video hardware for the given mode */ 4058 vidd_set_mode(scp->sc->adp, scp->mode); 4059 scp->rndr->init(scp); 4060 #ifndef __sparc64__ 4061 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize, 4062 (void *)scp->sc->adp->va_window, FALSE); 4063 #endif 4064 4065 update_font(scp); 4066 4067 sc_set_border(scp, scp->border); 4068 sc_set_cursor_image(scp); 4069 4070 return 0; 4071 } 4072 4073 void 4074 sc_set_border(scr_stat *scp, int color) 4075 { 4076 SC_VIDEO_LOCK(scp->sc); 4077 (*scp->rndr->draw_border)(scp, color); 4078 SC_VIDEO_UNLOCK(scp->sc); 4079 } 4080 4081 #ifndef SC_NO_FONT_LOADING 4082 void 4083 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf, 4084 int base, int count) 4085 { 4086 sc_softc_t *sc; 4087 4088 sc = scp->sc; 4089 sc->font_loading_in_progress = TRUE; 4090 vidd_load_font(sc->adp, page, size, width, buf, base, count); 4091 sc->font_loading_in_progress = FALSE; 4092 } 4093 4094 void 4095 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf, 4096 int base, int count) 4097 { 4098 sc_softc_t *sc; 4099 4100 sc = scp->sc; 4101 sc->font_loading_in_progress = TRUE; 4102 vidd_save_font(sc->adp, page, size, width, buf, base, count); 4103 sc->font_loading_in_progress = FALSE; 4104 } 4105 4106 void 4107 sc_show_font(scr_stat *scp, int page) 4108 { 4109 vidd_show_font(scp->sc->adp, page); 4110 } 4111 #endif /* !SC_NO_FONT_LOADING */ 4112 4113 void 4114 sc_paste(scr_stat *scp, const u_char *p, int count) 4115 { 4116 struct tty *tp; 4117 u_char *rmap; 4118 4119 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index); 4120 if (!tty_opened_ns(tp)) 4121 return; 4122 rmap = scp->sc->scr_rmap; 4123 for (; count > 0; --count) 4124 ttydisc_rint(tp, rmap[*p++], 0); 4125 ttydisc_rint_done(tp); 4126 } 4127 4128 void 4129 sc_respond(scr_stat *scp, const u_char *p, int count, int wakeup) 4130 { 4131 struct tty *tp; 4132 4133 tp = SC_DEV(scp->sc, scp->sc->cur_scp->index); 4134 if (!tty_opened_ns(tp)) 4135 return; 4136 ttydisc_rint_simple(tp, p, count); 4137 if (wakeup) { 4138 /* XXX: we can't always call ttydisc_rint_done() here! */ 4139 ttydisc_rint_done(tp); 4140 } 4141 } 4142 4143 void 4144 sc_bell(scr_stat *scp, int pitch, int duration) 4145 { 4146 if (cold || kdb_active || shutdown_in_progress || !enable_bell) 4147 return; 4148 4149 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL)) 4150 return; 4151 4152 if (scp->sc->flags & SC_VISUAL_BELL) { 4153 if (scp->sc->blink_in_progress) 4154 return; 4155 scp->sc->blink_in_progress = 3; 4156 if (scp != scp->sc->cur_scp) 4157 scp->sc->blink_in_progress += 2; 4158 blink_screen(scp->sc->cur_scp); 4159 } else if (duration != 0 && pitch != 0) { 4160 if (scp != scp->sc->cur_scp) 4161 pitch *= 2; 4162 sysbeep(1193182 / pitch, duration); 4163 } 4164 } 4165 4166 static int 4167 sc_kattr(void) 4168 { 4169 if (sc_console == NULL) 4170 return (SC_KERNEL_CONS_ATTR); /* for very early, before pcpu */ 4171 return (sc_kattrtab[PCPU_GET(cpuid) % nitems(sc_kattrtab)]); 4172 } 4173 4174 static void 4175 blink_screen(void *arg) 4176 { 4177 scr_stat *scp = arg; 4178 struct tty *tp; 4179 4180 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) { 4181 scp->sc->blink_in_progress = 0; 4182 mark_all(scp); 4183 tp = SC_DEV(scp->sc, scp->index); 4184 if (tty_opened_ns(tp)) 4185 sctty_outwakeup(tp); 4186 if (scp->sc->delayed_next_scr) 4187 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1); 4188 } 4189 else { 4190 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize, 4191 scp->sc->blink_in_progress & 1); 4192 scp->sc->blink_in_progress--; 4193 callout_reset_sbt(&scp->sc->cblink, SBT_1S / 15, 0, 4194 blink_screen, scp, C_PREL(0)); 4195 } 4196 } 4197 4198 /* 4199 * Until sc_attach_unit() gets called no dev structures will be available 4200 * to store the per-screen current status. This is the case when the 4201 * kernel is initially booting and needs access to its console. During 4202 * this early phase of booting the console's current status is kept in 4203 * one statically defined scr_stat structure, and any pointers to the 4204 * dev structures will be NULL. 4205 */ 4206 4207 static scr_stat * 4208 sc_get_stat(struct tty *tp) 4209 { 4210 if (tp == NULL) 4211 return (&main_console); 4212 return (SC_STAT(tp)); 4213 } 4214 4215 /* 4216 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and, 4217 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for 4218 * any keyboard. 4219 */ 4220 4221 static int 4222 sc_allocate_keyboard(sc_softc_t *sc, int unit) 4223 { 4224 int idx0, idx; 4225 keyboard_t *k0, *k; 4226 keyboard_info_t ki; 4227 4228 idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc); 4229 if (idx0 != -1) { 4230 k0 = kbd_get_keyboard(idx0); 4231 4232 for (idx = kbd_find_keyboard2("*", -1, 0); 4233 idx != -1; 4234 idx = kbd_find_keyboard2("*", -1, idx + 1)) { 4235 k = kbd_get_keyboard(idx); 4236 4237 if (idx == idx0 || KBD_IS_BUSY(k)) 4238 continue; 4239 4240 bzero(&ki, sizeof(ki)); 4241 strcpy(ki.kb_name, k->kb_name); 4242 ki.kb_unit = k->kb_unit; 4243 4244 (void)kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki); 4245 } 4246 } else 4247 idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc); 4248 4249 return (idx0); 4250 } 4251