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