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