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