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