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