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