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