1 /* $NetBSD: wsdisplay.c,v 1.167 2025/09/19 05:22:20 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Christopher G. Demetriou 17 * for the NetBSD Project. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: wsdisplay.c,v 1.167 2025/09/19 05:22:20 mrg Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_wsdisplay_compat.h" 38 #include "opt_wsmsgattrs.h" 39 #endif 40 41 #include "wskbd.h" 42 #include "wsmux.h" 43 #include "wsdisplay.h" 44 45 #include <sys/param.h> 46 #include <sys/conf.h> 47 #include <sys/device.h> 48 #include <sys/ioctl.h> 49 #include <sys/poll.h> 50 #include <sys/kernel.h> 51 #include <sys/proc.h> 52 #include <sys/malloc.h> 53 #include <sys/syslog.h> 54 #include <sys/systm.h> 55 #include <sys/tty.h> 56 #include <sys/signalvar.h> 57 #include <sys/errno.h> 58 #include <sys/fcntl.h> 59 #include <sys/vnode.h> 60 #include <sys/kauth.h> 61 #include <sys/sysctl.h> 62 63 #include <dev/wscons/wsconsio.h> 64 #include <dev/wscons/wseventvar.h> 65 #include <dev/wscons/wsmuxvar.h> 66 #include <dev/wscons/wsdisplayvar.h> 67 #include <dev/wscons/wsksymvar.h> 68 #include <dev/wscons/wsksymdef.h> 69 #include <dev/wscons/wsemulvar.h> 70 #include <dev/wscons/wscons_callbacks.h> 71 #include <dev/cons.h> 72 73 #include "locators.h" 74 #include "ioconf.h" 75 76 #ifdef WSDISPLAY_MULTICONS 77 static bool wsdisplay_multicons_enable = true; 78 static bool wsdisplay_multicons_suspended = false; 79 #endif 80 81 /* Console device before replaced by wsdisplay */ 82 static struct consdev *wsdisplay_ocn; 83 84 struct wsscreen_internal { 85 const struct wsdisplay_emulops *emulops; 86 void *emulcookie; 87 88 const struct wsscreen_descr *scrdata; 89 90 const struct wsemul_ops *wsemul; 91 void *wsemulcookie; 92 }; 93 94 struct wsscreen { 95 struct wsscreen_internal *scr_dconf; 96 97 struct tty *scr_tty; 98 int scr_hold_screen; /* hold tty output */ 99 100 int scr_flags; 101 #define SCR_OPEN 1 /* is it open? */ 102 #define SCR_WAITACTIVE 2 /* someone waiting on activation */ 103 #define SCR_GRAPHICS 4 /* graphics mode, no text (emulation) output */ 104 #define SCR_DUMBFB 8 /* in use as a dumb fb (iff SCR_GRAPHICS) */ 105 const struct wscons_syncops *scr_syncops; 106 void *scr_synccookie; 107 108 #ifdef WSDISPLAY_COMPAT_RAWKBD 109 int scr_rawkbd; 110 #endif 111 112 #ifdef WSDISPLAY_MULTICONS 113 callout_t scr_getc_ch; 114 #endif 115 116 struct wsdisplay_softc *sc; 117 118 /* XXX this is to support a hack in emulinput, see comment below */ 119 int scr_in_ttyoutput; 120 }; 121 122 static struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int, 123 const char *, 124 const struct wsscreen_descr *, void *, 125 int, int, long); 126 static void wsscreen_detach(struct wsscreen *); 127 static int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *); 128 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int); 129 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *); 130 static int wsdisplay_delscreen(struct wsdisplay_softc *, int, int); 131 132 #define WSDISPLAY_MAXSCREEN 8 133 134 struct wsdisplay_softc { 135 device_t sc_dev; 136 137 const struct wsdisplay_accessops *sc_accessops; 138 void *sc_accesscookie; 139 140 const struct wsscreen_list *sc_scrdata; 141 #ifdef WSDISPLAY_SCROLLSUPPORT 142 struct wsdisplay_scroll_data sc_scroll_values; 143 #endif 144 145 struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN]; 146 int sc_focusidx; /* available only if sc_focus isn't null */ 147 struct wsscreen *sc_focus; 148 149 struct wseventvar evar; 150 151 int sc_isconsole; 152 153 int sc_flags; 154 #define SC_SWITCHPENDING 1 155 #define SC_SWITCHERROR 2 156 #define SC_XATTACHED 4 /* X server active */ 157 kmutex_t sc_flagsmtx; /* for flags, might also be used for focus */ 158 kcondvar_t sc_flagscv; 159 160 int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */ 161 162 #if NWSKBD > 0 163 struct wsevsrc *sc_input; 164 #ifdef WSDISPLAY_COMPAT_RAWKBD 165 int sc_rawkbd; 166 #endif 167 #endif /* NWSKBD > 0 */ 168 }; 169 170 #ifdef WSDISPLAY_SCROLLSUPPORT 171 172 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = { 173 WSDISPLAY_SCROLL_DOALL, 174 25, 175 2, 176 }; 177 #endif 178 179 /* Autoconfiguration definitions. */ 180 static int wsdisplay_emul_match(device_t , cfdata_t, void *); 181 static void wsdisplay_emul_attach(device_t, device_t, void *); 182 static int wsdisplay_emul_detach(device_t, int); 183 static int wsdisplay_noemul_match(device_t, cfdata_t, void *); 184 static void wsdisplay_noemul_attach(device_t, device_t, void *); 185 static bool wsdisplay_suspend(device_t, const pmf_qual_t *); 186 187 CFATTACH_DECL_NEW(wsdisplay_emul, sizeof (struct wsdisplay_softc), 188 wsdisplay_emul_match, wsdisplay_emul_attach, wsdisplay_emul_detach, NULL); 189 190 CFATTACH_DECL_NEW(wsdisplay_noemul, sizeof (struct wsdisplay_softc), 191 wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL); 192 193 dev_type_open(wsdisplayopen); 194 dev_type_close(wsdisplayclose); 195 dev_type_read(wsdisplayread); 196 dev_type_write(wsdisplaywrite); 197 dev_type_ioctl(wsdisplayioctl); 198 dev_type_stop(wsdisplaystop); 199 dev_type_tty(wsdisplaytty); 200 dev_type_poll(wsdisplaypoll); 201 dev_type_mmap(wsdisplaymmap); 202 dev_type_kqfilter(wsdisplaykqfilter); 203 204 const struct cdevsw wsdisplay_cdevsw = { 205 .d_open = wsdisplayopen, 206 .d_close = wsdisplayclose, 207 .d_read = wsdisplayread, 208 .d_write = wsdisplaywrite, 209 .d_ioctl = wsdisplayioctl, 210 .d_stop = wsdisplaystop, 211 .d_tty = wsdisplaytty, 212 .d_poll = wsdisplaypoll, 213 .d_mmap = wsdisplaymmap, 214 .d_kqfilter = wsdisplaykqfilter, 215 .d_discard = nodiscard, 216 .d_flag = D_TTY 217 }; 218 219 static void wsdisplaystart(struct tty *); 220 static int wsdisplayparam(struct tty *, struct termios *); 221 222 223 #define WSDISPLAYUNIT(dev) (minor(dev) >> 8) 224 #define WSDISPLAYSCREEN(dev) (minor(dev) & 0xff) 225 #define ISWSDISPLAYSTAT(dev) (WSDISPLAYSCREEN(dev) == 254) 226 #define ISWSDISPLAYCTL(dev) (WSDISPLAYSCREEN(dev) == 255) 227 #define WSDISPLAYMINOR(unit, screen) (((unit) << 8) | (screen)) 228 229 #define WSSCREEN_HAS_EMULATOR(scr) ((scr)->scr_dconf->wsemul != NULL) 230 #define WSSCREEN_HAS_TTY(scr) ((scr)->scr_tty != NULL) 231 232 static void wsdisplay_common_attach(struct wsdisplay_softc *sc, 233 int console, int kbdmux, const struct wsscreen_list *, 234 const struct wsdisplay_accessops *accessops, 235 void *accesscookie); 236 237 #ifdef WSDISPLAY_COMPAT_RAWKBD 238 int wsdisplay_update_rawkbd(struct wsdisplay_softc *, 239 struct wsscreen *); 240 #endif 241 242 static int wsdisplay_console_initted; 243 static int wsdisplay_console_attached; 244 static struct wsdisplay_softc *wsdisplay_console_device; 245 static struct wsscreen_internal wsdisplay_console_conf; 246 247 static int wsdisplay_getc(dev_t); 248 static void wsdisplay_pollc(dev_t, int); 249 250 static int wsdisplay_cons_pollmode; 251 static int (*wsdisplay_cons_kbd_getc)(dev_t); 252 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int); 253 254 static struct consdev wsdisplay_cons = { 255 .cn_getc = wsdisplay_getc, 256 .cn_putc = wsdisplay_cnputc, 257 .cn_pollc = wsdisplay_pollc, 258 .cn_dev = NODEV, 259 .cn_pri = CN_NORMAL 260 }; 261 262 #ifndef WSDISPLAY_DEFAULTSCREENS 263 # define WSDISPLAY_DEFAULTSCREENS 0 264 #endif 265 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS; 266 267 static int wsdisplay_switch1(device_t, int, int); 268 static void wsdisplay_switch1_cb(void *, int, int); 269 static int wsdisplay_switch2(device_t, int, int); 270 static void wsdisplay_switch2_cb(void *, int, int); 271 static int wsdisplay_switch3(device_t, int, int); 272 static void wsdisplay_switch3_cb(void *, int, int); 273 274 static void wsdisplay_swdone_cb(void *, int, int); 275 static int wsdisplay_dosync(struct wsdisplay_softc *, int); 276 277 int wsdisplay_clearonclose; 278 279 #ifdef WSDISPLAY_MULTICONS 280 /* 281 * Replace cn_isconsole() so that we can enter DDB from old console. 282 */ 283 bool 284 wsdisplay_cn_isconsole(dev_t dev) 285 { 286 287 return (cn_tab != NULL && cn_tab->cn_dev == dev) || 288 (cn_tab == &wsdisplay_cons && !wsdisplay_multicons_suspended && 289 wsdisplay_multicons_enable && wsdisplay_ocn != NULL && 290 wsdisplay_ocn->cn_dev == dev); 291 } 292 293 static void 294 wsscreen_getc_poll(void *priv) 295 { 296 struct wsscreen *scr = priv; 297 int c; 298 299 if (wsdisplay_multicons_enable && 300 wsdisplay_ocn && wsdisplay_ocn->cn_getc && 301 WSSCREEN_HAS_EMULATOR(scr) && WSSCREEN_HAS_TTY(scr)) { 302 struct tty *tp = scr->scr_tty; 303 do { 304 c = wsdisplay_ocn->cn_getc(wsdisplay_ocn->cn_dev); 305 if (c >= 0) 306 (*tp->t_linesw->l_rint)((unsigned char)c, tp); 307 } while (c >= 0); 308 } 309 310 callout_schedule(&scr->scr_getc_ch, mstohz(10)); 311 } 312 #endif 313 314 static struct wsscreen * 315 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul, 316 const struct wsscreen_descr *type, void *cookie, int ccol, 317 int crow, long defattr) 318 { 319 struct wsscreen_internal *dconf; 320 struct wsscreen *scr; 321 322 scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK); 323 324 if (console) { 325 dconf = &wsdisplay_console_conf; 326 /* 327 * If there's an emulation, tell it about the callback argument. 328 * The other stuff is already there. 329 */ 330 if (dconf->wsemul != NULL) 331 (*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0); 332 } else { /* not console */ 333 dconf = malloc(sizeof(struct wsscreen_internal), 334 M_DEVBUF, M_WAITOK); 335 dconf->emulops = type->textops; 336 dconf->emulcookie = cookie; 337 if (dconf->emulops) { 338 dconf->wsemul = wsemul_pick(emul); 339 if (dconf->wsemul == NULL) { 340 free(dconf, M_DEVBUF); 341 free(scr, M_DEVBUF); 342 return NULL; 343 } 344 dconf->wsemulcookie = 345 (*dconf->wsemul->attach)(0, type, cookie, 346 ccol, crow, scr, defattr); 347 } else 348 dconf->wsemul = NULL; 349 dconf->scrdata = type; 350 } 351 352 scr->scr_dconf = dconf; 353 354 scr->scr_tty = tty_alloc(); 355 tty_attach(scr->scr_tty); 356 scr->scr_hold_screen = 0; 357 if (WSSCREEN_HAS_EMULATOR(scr)) 358 scr->scr_flags = 0; 359 else 360 scr->scr_flags = SCR_GRAPHICS; 361 362 scr->scr_syncops = 0; 363 scr->sc = sc; 364 #ifdef WSDISPLAY_COMPAT_RAWKBD 365 scr->scr_rawkbd = 0; 366 #endif 367 #ifdef WSDISPLAY_MULTICONS 368 callout_init(&scr->scr_getc_ch, 0); 369 callout_setfunc(&scr->scr_getc_ch, wsscreen_getc_poll, scr); 370 if (console) 371 callout_schedule(&scr->scr_getc_ch, mstohz(10)); 372 #endif 373 return scr; 374 } 375 376 static void 377 wsscreen_detach(struct wsscreen *scr) 378 { 379 u_int ccol, crow; /* XXX */ 380 381 if (WSSCREEN_HAS_TTY(scr)) { 382 tty_detach(scr->scr_tty); 383 tty_free(scr->scr_tty); 384 } 385 if (WSSCREEN_HAS_EMULATOR(scr)) { 386 (*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie, 387 &ccol, &crow); 388 wsemul_drop(scr->scr_dconf->wsemul); 389 } 390 if (scr->scr_dconf->scrdata->capabilities & WSSCREEN_FREE) 391 free(__UNCONST(scr->scr_dconf->scrdata), M_DEVBUF); 392 #ifdef WSDISPLAY_MULTICONS 393 callout_halt(&scr->scr_getc_ch, NULL); 394 callout_destroy(&scr->scr_getc_ch); 395 #endif 396 free(scr->scr_dconf, M_DEVBUF); 397 free(scr, M_DEVBUF); 398 } 399 400 const struct wsscreen_descr * 401 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name) 402 { 403 int i; 404 const struct wsscreen_descr *scr; 405 406 KASSERT(scrdata->nscreens > 0); 407 if (name == NULL) 408 return scrdata->screens[0]; 409 410 for (i = 0; i < scrdata->nscreens; i++) { 411 scr = scrdata->screens[i]; 412 if (!strcmp(name, scr->name)) 413 return scr; 414 } 415 416 return 0; 417 } 418 419 /* 420 * print info about attached screen 421 */ 422 static void 423 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count) 424 { 425 aprint_verbose_dev(sc->sc_dev, "screen %d", idx); 426 if (count > 1) 427 aprint_verbose("-%d", idx + (count-1)); 428 aprint_verbose(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name); 429 if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) { 430 aprint_verbose(", %s emulation", 431 sc->sc_scr[idx]->scr_dconf->wsemul->name); 432 } 433 aprint_verbose(")\n"); 434 } 435 436 static int 437 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx, 438 const char *screentype, const char *emul) 439 { 440 const struct wsscreen_descr *scrdesc; 441 struct wsscreen_descr *scrdescr2; 442 int error; 443 void *cookie; 444 int ccol, crow; 445 long defattr; 446 struct wsscreen *scr; 447 int s; 448 449 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN) 450 return EINVAL; 451 if (sc->sc_scr[idx] != NULL) 452 return EBUSY; 453 scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype); 454 if (!scrdesc) 455 return ENXIO; 456 457 /* 458 * if this screen can resize we need to copy the descr so each screen 459 * gets its own 460 */ 461 if (scrdesc->capabilities & WSSCREEN_RESIZE) { 462 /* we want per screen wsscreen_descr */ 463 scrdescr2 = malloc(sizeof(struct wsscreen_descr), M_DEVBUF, M_WAITOK); 464 memcpy(scrdescr2, scrdesc, sizeof(struct wsscreen_descr)); 465 scrdescr2->capabilities |= WSSCREEN_FREE; 466 scrdesc = scrdescr2; 467 } 468 469 error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie, 470 scrdesc, &cookie, &ccol, &crow, &defattr); 471 if (error) 472 return error; 473 474 scr = wsscreen_attach(sc, 0, emul, scrdesc, 475 cookie, ccol, crow, defattr); 476 if (scr == NULL) { 477 (*sc->sc_accessops->free_screen)(sc->sc_accesscookie, 478 cookie); 479 return ENXIO; 480 } 481 482 sc->sc_scr[idx] = scr; 483 484 /* if no screen has focus yet, activate the first we get */ 485 s = spltty(); 486 if (!sc->sc_focus) { 487 (*sc->sc_accessops->show_screen)(sc->sc_accesscookie, 488 scr->scr_dconf->emulcookie, 489 0, 0, 0); 490 sc->sc_focusidx = idx; 491 sc->sc_focus = scr; 492 } 493 splx(s); 494 return 0; 495 } 496 497 static void 498 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr) 499 { 500 int maj, mn, idx; 501 502 /* hangup */ 503 if (WSSCREEN_HAS_TTY(scr)) { 504 struct tty *tp = scr->scr_tty; 505 (*tp->t_linesw->l_modem)(tp, 0); 506 } 507 508 /* locate the major number */ 509 maj = cdevsw_lookup_major(&wsdisplay_cdevsw); 510 /* locate the screen index */ 511 for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++) 512 if (scr == sc->sc_scr[idx]) 513 break; 514 #ifdef DIAGNOSTIC 515 if (idx == WSDISPLAY_MAXSCREEN) 516 panic("wsdisplay_forceclose: bad screen"); 517 #endif 518 519 /* nuke the vnodes */ 520 mn = WSDISPLAYMINOR(device_unit(sc->sc_dev), idx); 521 vdevgone(maj, mn, mn, VCHR); 522 } 523 524 #ifdef WSDISPLAY_SCROLLSUPPORT 525 void 526 wsdisplay_scroll(void *arg, int op) 527 { 528 device_t dv = arg; 529 struct wsdisplay_softc *sc = device_private(dv); 530 struct wsscreen *scr; 531 int lines; 532 533 scr = sc->sc_focus; 534 535 if (!scr) 536 return; 537 538 if (op == WSDISPLAY_SCROLL_RESET) 539 lines = 0; 540 else { 541 lines = (op & WSDISPLAY_SCROLL_LOW) ? 542 sc->sc_scroll_values.slowlines : 543 sc->sc_scroll_values.fastlines; 544 if (op & WSDISPLAY_SCROLL_BACKWARD) 545 lines = -(lines); 546 } 547 548 if (sc->sc_accessops->scroll) { 549 (*sc->sc_accessops->scroll)(sc->sc_accesscookie, 550 sc->sc_focus->scr_dconf->emulcookie, lines); 551 } 552 } 553 #endif 554 555 static int 556 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags) 557 { 558 struct wsscreen *scr; 559 int s; 560 void *cookie; 561 562 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN) 563 return EINVAL; 564 if ((scr = sc->sc_scr[idx]) == NULL) 565 return ENXIO; 566 567 if (scr->scr_dconf == &wsdisplay_console_conf || 568 scr->scr_syncops || 569 ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE))) 570 return EBUSY; 571 572 wsdisplay_closescreen(sc, scr); 573 574 /* 575 * delete pointers, so neither device entries 576 * nor keyboard input can reference it anymore 577 */ 578 s = spltty(); 579 if (sc->sc_focus == scr) { 580 sc->sc_focus = 0; 581 #ifdef WSDISPLAY_COMPAT_RAWKBD 582 wsdisplay_update_rawkbd(sc, 0); 583 #endif 584 } 585 sc->sc_scr[idx] = 0; 586 splx(s); 587 588 /* 589 * Wake up processes waiting for the screen to 590 * be activated. Sleepers must check whether 591 * the screen still exists. 592 */ 593 if (scr->scr_flags & SCR_WAITACTIVE) 594 wakeup(scr); 595 596 /* save a reference to the graphics screen */ 597 cookie = scr->scr_dconf->emulcookie; 598 599 wsscreen_detach(scr); 600 601 (*sc->sc_accessops->free_screen)(sc->sc_accesscookie, 602 cookie); 603 604 aprint_verbose_dev(sc->sc_dev, "screen %d deleted\n", idx); 605 return 0; 606 } 607 608 /* 609 * Autoconfiguration functions. 610 */ 611 int 612 wsdisplay_emul_match(device_t parent, cfdata_t match, void *aux) 613 { 614 struct wsemuldisplaydev_attach_args *ap = aux; 615 616 if (match->cf_loc[WSEMULDISPLAYDEVCF_CONSOLE] != 617 WSEMULDISPLAYDEVCF_CONSOLE_DEFAULT) { 618 /* 619 * If console-ness of device specified, either match 620 * exactly (at high priority), or fail. 621 */ 622 if (match->cf_loc[WSEMULDISPLAYDEVCF_CONSOLE] != 0 && 623 ap->console != 0) 624 return 10; 625 else 626 return 0; 627 } 628 629 /* If console-ness unspecified, it wins. */ 630 return 1; 631 } 632 633 void 634 wsdisplay_emul_attach(device_t parent, device_t self, void *aux) 635 { 636 struct wsdisplay_softc *sc = device_private(self); 637 struct wsemuldisplaydev_attach_args *ap = aux; 638 639 sc->sc_dev = self; 640 641 /* Don't allow more than one console to attach */ 642 if (wsdisplay_console_attached && ap->console) 643 ap->console = 0; 644 645 wsdisplay_common_attach(sc, ap->console, 646 device_cfdata(self)->cf_loc[WSEMULDISPLAYDEVCF_KBDMUX], 647 ap->scrdata, ap->accessops, ap->accesscookie); 648 649 if (ap->console) { 650 int maj; 651 652 /* locate the major number */ 653 maj = cdevsw_lookup_major(&wsdisplay_cdevsw); 654 655 cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self), 656 0)); 657 } 658 } 659 660 /* Print function (for parent devices). */ 661 int 662 wsemuldisplaydevprint(void *aux, const char *pnp) 663 { 664 #if 0 /* -Wunused */ 665 struct wsemuldisplaydev_attach_args *ap = aux; 666 #endif 667 668 if (pnp) 669 aprint_normal("wsdisplay at %s", pnp); 670 #if 0 /* don't bother; it's ugly */ 671 aprint_normal(" console %d", ap->console); 672 #endif 673 674 return UNCONF; 675 } 676 677 int 678 wsdisplay_emul_detach(device_t dev, int how) 679 { 680 struct wsdisplay_softc *sc = device_private(dev); 681 int flag, i, res; 682 683 flag = (how & DETACH_FORCE ? WSDISPLAY_DELSCR_FORCE : 0); 684 for (i = 0; i < WSDISPLAY_MAXSCREEN; i++) 685 if (sc->sc_scr[i]) { 686 res = wsdisplay_delscreen(sc, i, flag); 687 if (res) 688 return res; 689 } 690 691 cv_destroy(&sc->sc_flagscv); 692 mutex_destroy(&sc->sc_flagsmtx); 693 return 0; 694 } 695 696 int 697 wsdisplay_noemul_match(device_t parent, cfdata_t match, void *aux) 698 { 699 #if 0 /* -Wunused */ 700 struct wsdisplaydev_attach_args *ap = aux; 701 #endif 702 703 /* Always match. */ 704 return 1; 705 } 706 707 void 708 wsdisplay_noemul_attach(device_t parent, device_t self, void *aux) 709 { 710 struct wsdisplay_softc *sc = device_private(self); 711 struct wsdisplaydev_attach_args *ap = aux; 712 713 sc->sc_dev = self; 714 715 wsdisplay_common_attach(sc, 0, 716 device_cfdata(self)->cf_loc[WSDISPLAYDEVCF_KBDMUX], NULL, 717 ap->accessops, ap->accesscookie); 718 } 719 720 static void 721 wsdisplay_swdone_cb(void *arg, int error, int waitok) 722 { 723 struct wsdisplay_softc *sc = arg; 724 725 mutex_enter(&sc->sc_flagsmtx); 726 KASSERT(sc->sc_flags & SC_SWITCHPENDING); 727 if (error) 728 sc->sc_flags |= SC_SWITCHERROR; 729 sc->sc_flags &= ~SC_SWITCHPENDING; 730 cv_signal(&sc->sc_flagscv); 731 mutex_exit(&sc->sc_flagsmtx); 732 } 733 734 static int 735 wsdisplay_dosync(struct wsdisplay_softc *sc, int attach) 736 { 737 struct wsscreen *scr; 738 int (*op)(void *, int, void (*)(void *, int, int), void *); 739 int res; 740 741 scr = sc->sc_focus; 742 if (!scr || !scr->scr_syncops) 743 return 0; /* XXX check SCR_GRAPHICS? */ 744 745 sc->sc_flags |= SC_SWITCHPENDING; 746 sc->sc_flags &= ~SC_SWITCHERROR; 747 if (attach) 748 op = scr->scr_syncops->attach; 749 else 750 op = scr->scr_syncops->detach; 751 res = (*op)(scr->scr_synccookie, 1, wsdisplay_swdone_cb, sc); 752 if (res == EAGAIN) { 753 /* wait for callback */ 754 mutex_enter(&sc->sc_flagsmtx); 755 while (sc->sc_flags & SC_SWITCHPENDING) 756 cv_wait_sig(&sc->sc_flagscv, &sc->sc_flagsmtx); 757 mutex_exit(&sc->sc_flagsmtx); 758 if (sc->sc_flags & SC_SWITCHERROR) 759 return EIO; /* XXX pass real error */ 760 } else { 761 sc->sc_flags &= ~SC_SWITCHPENDING; 762 if (res) 763 return res; 764 } 765 if (attach) 766 sc->sc_flags |= SC_XATTACHED; 767 else 768 sc->sc_flags &= ~SC_XATTACHED; 769 return 0; 770 } 771 772 int 773 wsdisplay_handlex(int resume) 774 { 775 int i, res; 776 device_t dv; 777 778 for (i = 0; i < wsdisplay_cd.cd_ndevs; i++) { 779 dv = device_lookup(&wsdisplay_cd, i); 780 if (!dv) 781 continue; 782 res = wsdisplay_dosync(device_private(dv), resume); 783 if (res) 784 return res; 785 } 786 return 0; 787 } 788 789 static bool 790 wsdisplay_suspend(device_t dv, const pmf_qual_t *qual) 791 { 792 struct wsdisplay_softc *sc = device_private(dv); 793 struct wsscreen *scr = sc->sc_focus; 794 795 if (sc->sc_flags & SC_XATTACHED) { 796 KASSERT(scr); 797 KASSERT(scr->scr_syncops); 798 } 799 800 #if 1 801 /* 802 * XXX X servers should have been detached earlier. 803 * pmf currently ignores our return value and suspends the system 804 * after device suspend failures. We try to avoid bigger damage 805 * and try to detach the X server here. This is not safe because 806 * other parts of the system which the X server deals with 807 * might already be suspended. 808 */ 809 if (sc->sc_flags & SC_XATTACHED) { 810 printf("%s: emergency X server detach\n", device_xname(dv)); 811 wsdisplay_dosync(sc, 0); 812 } 813 #endif 814 return !(sc->sc_flags & SC_XATTACHED); 815 } 816 817 /* Print function (for parent devices). */ 818 int 819 wsdisplaydevprint(void *aux, const char *pnp) 820 { 821 #if 0 /* -Wunused */ 822 struct wsdisplaydev_attach_args *ap = aux; 823 #endif 824 825 if (pnp) 826 aprint_normal("wsdisplay at %s", pnp); 827 828 return UNCONF; 829 } 830 831 static void 832 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux, 833 const struct wsscreen_list *scrdata, 834 const struct wsdisplay_accessops *accessops, 835 void *accesscookie) 836 { 837 int i, start=0; 838 #if NWSKBD > 0 839 struct wsevsrc *kme; 840 #if NWSMUX > 0 841 struct wsmux_softc *mux; 842 843 if (kbdmux >= 0) 844 mux = wsmux_getmux(kbdmux); 845 else 846 mux = wsmux_create("dmux", device_unit(sc->sc_dev)); 847 sc->sc_input = &mux->sc_base; 848 mux->sc_base.me_dispdv = sc->sc_dev; 849 aprint_normal(" kbdmux %d", kbdmux); 850 #else 851 if (kbdmux >= 0) 852 aprint_normal(" (kbdmux ignored)"); 853 #endif 854 #endif 855 856 sc->sc_isconsole = console; 857 858 if (console) { 859 KASSERT(wsdisplay_console_initted); 860 KASSERT(wsdisplay_console_device == NULL); 861 862 sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0); 863 wsdisplay_console_device = sc; 864 865 aprint_normal(": console (%s, %s emulation)", 866 wsdisplay_console_conf.scrdata->name, 867 wsdisplay_console_conf.wsemul->name); 868 869 #if NWSKBD > 0 870 kme = wskbd_set_console_display(sc->sc_dev, sc->sc_input); 871 if (kme != NULL) 872 aprint_normal(", using %s", device_xname(kme->me_dv)); 873 #if NWSMUX == 0 874 sc->sc_input = kme; 875 #endif 876 #endif 877 878 sc->sc_focusidx = 0; 879 sc->sc_focus = sc->sc_scr[0]; 880 start = 1; 881 882 wsdisplay_console_attached = 1; 883 } 884 aprint_normal("\n"); 885 aprint_naive("\n"); 886 887 #if NWSKBD > 0 && NWSMUX > 0 888 wsmux_set_display(mux, sc->sc_dev); 889 #endif 890 891 mutex_init(&sc->sc_flagsmtx, MUTEX_DEFAULT, IPL_NONE); 892 cv_init(&sc->sc_flagscv, "wssw"); 893 894 sc->sc_accessops = accessops; 895 sc->sc_accesscookie = accesscookie; 896 sc->sc_scrdata = scrdata; 897 898 #ifdef WSDISPLAY_SCROLLSUPPORT 899 sc->sc_scroll_values = wsdisplay_default_scroll_values; 900 #endif 901 902 /* 903 * Set up a number of virtual screens if wanted. The 904 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code 905 * is for special cases like installation kernels. 906 */ 907 for (i = start; i < wsdisplay_defaultscreens; i++) { 908 if (wsdisplay_addscreen(sc, i, 0, 0)) 909 break; 910 } 911 912 if (i > start) 913 wsdisplay_addscreen_print(sc, start, i-start); 914 915 if (!pmf_device_register(sc->sc_dev, wsdisplay_suspend, NULL)) 916 aprint_error_dev(sc->sc_dev, 917 "couldn't establish power handler\n"); 918 } 919 920 void 921 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie, 922 int ccol, int crow, long defattr) 923 { 924 const struct wsemul_ops *wsemul; 925 926 KASSERT(wsdisplay_console_initted < 2); 927 KASSERT(type->nrows > 0); 928 KASSERT(type->ncols > 0); 929 KASSERT(crow < type->nrows); 930 KASSERT(ccol < type->ncols); 931 932 wsdisplay_console_conf.emulops = type->textops; 933 wsdisplay_console_conf.emulcookie = cookie; 934 wsdisplay_console_conf.scrdata = type; 935 936 wsemul = wsemul_pick(0); /* default */ 937 wsdisplay_console_conf.wsemul = wsemul; 938 wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie, 939 ccol, crow, 940 defattr); 941 942 if (cn_tab != &wsdisplay_cons) 943 wsdisplay_ocn = cn_tab; 944 945 if (wsdisplay_ocn != NULL && wsdisplay_ocn->cn_halt != NULL) 946 wsdisplay_ocn->cn_halt(wsdisplay_ocn->cn_dev); 947 948 cn_tab = &wsdisplay_cons; 949 wsdisplay_console_initted = 2; 950 } 951 952 void 953 wsdisplay_preattach(const struct wsscreen_descr *type, void *cookie, 954 int ccol, int crow, long defattr) 955 { 956 const struct wsemul_ops *wsemul; 957 958 KASSERT(!wsdisplay_console_initted); 959 KASSERT(type->nrows > 0); 960 KASSERT(type->ncols > 0); 961 KASSERT(crow < type->nrows); 962 KASSERT(ccol < type->ncols); 963 964 wsdisplay_console_conf.emulops = type->textops; 965 wsdisplay_console_conf.emulcookie = cookie; 966 wsdisplay_console_conf.scrdata = type; 967 968 wsemul = wsemul_pick(0); /* default */ 969 wsdisplay_console_conf.wsemul = wsemul; 970 wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie, 971 ccol, crow, 972 defattr); 973 974 if (cn_tab != &wsdisplay_cons) 975 wsdisplay_ocn = cn_tab; 976 977 if (wsdisplay_ocn != NULL && wsdisplay_ocn->cn_halt != NULL) 978 wsdisplay_ocn->cn_halt(wsdisplay_ocn->cn_dev); 979 980 cn_tab = &wsdisplay_cons; 981 wsdisplay_console_initted = 1; 982 } 983 984 void 985 wsdisplay_predetach(void) 986 { 987 KASSERT(wsdisplay_console_initted == 1); 988 989 cn_tab = wsdisplay_ocn; 990 wsdisplay_console_initted = 0; 991 } 992 993 void 994 wsdisplay_cndetach(void) 995 { 996 KASSERT(wsdisplay_console_initted == 2); 997 998 cn_tab = wsdisplay_ocn; 999 wsdisplay_console_initted = 0; 1000 } 1001 1002 /* 1003 * Tty and cdevsw functions. 1004 */ 1005 int 1006 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l) 1007 { 1008 struct wsdisplay_softc *sc; 1009 struct tty *tp; 1010 int newopen, error; 1011 struct wsscreen *scr; 1012 1013 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1014 if (sc == NULL) /* make sure it was attached */ 1015 return ENXIO; 1016 1017 if (ISWSDISPLAYSTAT(dev)) { 1018 wsevent_init(&sc->evar, l->l_proc); 1019 return 0; 1020 } 1021 1022 if (ISWSDISPLAYCTL(dev)) 1023 return 0; 1024 1025 if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN) 1026 return ENXIO; 1027 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1028 return ENXIO; 1029 1030 if (WSSCREEN_HAS_TTY(scr)) { 1031 tp = scr->scr_tty; 1032 tp->t_oproc = wsdisplaystart; 1033 tp->t_param = wsdisplayparam; 1034 tp->t_dev = dev; 1035 newopen = (tp->t_state & TS_ISOPEN) == 0; 1036 1037 if (kauth_authorize_device_tty(l->l_cred, 1038 KAUTH_DEVICE_TTY_OPEN, tp)) 1039 return EBUSY; 1040 1041 if (newopen) { 1042 ttychars(tp); 1043 tp->t_iflag = TTYDEF_IFLAG; 1044 tp->t_oflag = TTYDEF_OFLAG; 1045 tp->t_cflag = TTYDEF_CFLAG; 1046 tp->t_lflag = TTYDEF_LFLAG; 1047 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 1048 wsdisplayparam(tp, &tp->t_termios); 1049 ttsetwater(tp); 1050 } 1051 tp->t_state |= TS_CARR_ON; 1052 1053 error = ((*tp->t_linesw->l_open)(dev, tp)); 1054 if (error) 1055 return error; 1056 1057 if (newopen && WSSCREEN_HAS_EMULATOR(scr)) { 1058 /* set window sizes as appropriate, and reset 1059 the emulation */ 1060 tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows; 1061 tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols; 1062 1063 /* wsdisplay_set_emulation() */ 1064 } 1065 } 1066 1067 scr->scr_flags |= SCR_OPEN; 1068 return 0; 1069 } 1070 1071 int 1072 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l) 1073 { 1074 device_t dv; 1075 struct wsdisplay_softc *sc; 1076 struct tty *tp; 1077 struct wsscreen *scr; 1078 1079 dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1080 sc = device_private(dv); 1081 1082 if (ISWSDISPLAYSTAT(dev)) { 1083 wsevent_fini(&sc->evar); 1084 return 0; 1085 } 1086 1087 if (ISWSDISPLAYCTL(dev)) 1088 return 0; 1089 1090 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1091 return 0; 1092 1093 if (WSSCREEN_HAS_TTY(scr)) { 1094 if (scr->scr_hold_screen) { 1095 int s; 1096 1097 /* XXX RESET KEYBOARD LEDS, etc. */ 1098 s = spltty(); /* avoid conflict with keyboard */ 1099 wsdisplay_kbdholdscreen(dv, 0); 1100 splx(s); 1101 } 1102 tp = scr->scr_tty; 1103 (*tp->t_linesw->l_close)(tp, flag); 1104 ttyclose(tp); 1105 } 1106 1107 if (scr->scr_syncops) 1108 (*scr->scr_syncops->destroy)(scr->scr_synccookie); 1109 1110 if (WSSCREEN_HAS_EMULATOR(scr)) { 1111 scr->scr_flags &= ~SCR_GRAPHICS; 1112 (*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie, 1113 WSEMUL_RESET); 1114 if (wsdisplay_clearonclose) 1115 (*scr->scr_dconf->wsemul->reset) 1116 (scr->scr_dconf->wsemulcookie, 1117 WSEMUL_CLEARSCREEN); 1118 } 1119 1120 #ifdef WSDISPLAY_COMPAT_RAWKBD 1121 if (scr->scr_rawkbd) { 1122 int kbmode = WSKBD_TRANSLATED; 1123 (void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE, 1124 (void *)&kbmode, 0, l); 1125 } 1126 #endif 1127 1128 scr->scr_flags &= ~SCR_OPEN; 1129 1130 return 0; 1131 } 1132 1133 int 1134 wsdisplayread(dev_t dev, struct uio *uio, int flag) 1135 { 1136 struct wsdisplay_softc *sc; 1137 struct tty *tp; 1138 struct wsscreen *scr; 1139 int error; 1140 1141 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1142 1143 if (ISWSDISPLAYSTAT(dev)) { 1144 error = wsevent_read(&sc->evar, uio, flag); 1145 return error; 1146 } 1147 1148 if (ISWSDISPLAYCTL(dev)) 1149 return 0; 1150 1151 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1152 return ENXIO; 1153 1154 if (!WSSCREEN_HAS_TTY(scr)) 1155 return ENODEV; 1156 1157 tp = scr->scr_tty; 1158 return (*tp->t_linesw->l_read)(tp, uio, flag); 1159 } 1160 1161 int 1162 wsdisplaywrite(dev_t dev, struct uio *uio, int flag) 1163 { 1164 struct wsdisplay_softc *sc; 1165 struct tty *tp; 1166 struct wsscreen *scr; 1167 1168 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1169 1170 if (ISWSDISPLAYSTAT(dev)) { 1171 return 0; 1172 } 1173 1174 if (ISWSDISPLAYCTL(dev)) 1175 return 0; 1176 1177 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1178 return ENXIO; 1179 1180 if (!WSSCREEN_HAS_TTY(scr)) 1181 return ENODEV; 1182 1183 tp = scr->scr_tty; 1184 return (*tp->t_linesw->l_write)(tp, uio, flag); 1185 } 1186 1187 int 1188 wsdisplaypoll(dev_t dev, int events, struct lwp *l) 1189 { 1190 struct wsdisplay_softc *sc; 1191 struct tty *tp; 1192 struct wsscreen *scr; 1193 1194 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1195 1196 if (ISWSDISPLAYSTAT(dev)) 1197 return wsevent_poll(&sc->evar, events, l); 1198 1199 if (ISWSDISPLAYCTL(dev)) 1200 return 0; 1201 1202 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1203 return POLLHUP; 1204 1205 if (!WSSCREEN_HAS_TTY(scr)) 1206 return POLLERR; 1207 1208 tp = scr->scr_tty; 1209 return (*tp->t_linesw->l_poll)(tp, events, l); 1210 } 1211 1212 int 1213 wsdisplaykqfilter(dev_t dev, struct knote *kn) 1214 { 1215 struct wsdisplay_softc *sc; 1216 struct wsscreen *scr; 1217 1218 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1219 1220 if (ISWSDISPLAYCTL(dev)) 1221 return 1; 1222 1223 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1224 return 1; 1225 1226 1227 if (WSSCREEN_HAS_TTY(scr)) 1228 return ttykqfilter(dev, kn); 1229 else 1230 return 1; 1231 } 1232 1233 struct tty * 1234 wsdisplaytty(dev_t dev) 1235 { 1236 struct wsdisplay_softc *sc; 1237 struct wsscreen *scr; 1238 1239 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1240 1241 if (ISWSDISPLAYSTAT(dev)) 1242 panic("wsdisplaytty() on status device"); 1243 1244 if (ISWSDISPLAYCTL(dev)) 1245 panic("wsdisplaytty() on ctl device"); 1246 1247 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1248 return NULL; 1249 1250 return scr->scr_tty; 1251 } 1252 1253 int 1254 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 1255 { 1256 device_t dv; 1257 struct wsdisplay_softc *sc; 1258 struct tty *tp; 1259 int error; 1260 struct wsscreen *scr; 1261 1262 dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1263 sc = device_private(dv); 1264 1265 #ifdef WSDISPLAY_COMPAT_USL 1266 error = wsdisplay_usl_ioctl1(dv, cmd, data, flag, l); 1267 if (error != EPASSTHROUGH) 1268 return error; 1269 #endif 1270 1271 if (ISWSDISPLAYSTAT(dev)) 1272 return wsdisplay_stat_ioctl(sc, cmd, data, flag, l); 1273 1274 if (ISWSDISPLAYCTL(dev)) 1275 return wsdisplay_cfg_ioctl(sc, cmd, data, flag, l); 1276 1277 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1278 return ENXIO; 1279 1280 if (WSSCREEN_HAS_TTY(scr)) { 1281 tp = scr->scr_tty; 1282 1283 /* do the line discipline ioctls first */ 1284 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 1285 if (error != EPASSTHROUGH) 1286 return error; 1287 1288 /* then the tty ioctls */ 1289 error = ttioctl(tp, cmd, data, flag, l); 1290 if (error != EPASSTHROUGH) 1291 return error; 1292 } 1293 1294 #ifdef WSDISPLAY_COMPAT_USL 1295 error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l); 1296 if (error != EPASSTHROUGH) 1297 return error; 1298 #endif 1299 1300 return wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l); 1301 } 1302 1303 int 1304 wsdisplay_param(device_t dv, u_long cmd, struct wsdisplay_param *dp) 1305 { 1306 struct wsdisplay_softc *sc = device_private(dv); 1307 return (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, 1308 sc->sc_focus->scr_dconf->emulcookie, 1309 cmd, (void *)dp, 0, NULL); 1310 } 1311 1312 int 1313 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr, 1314 u_long cmd, void *data, int flag, struct lwp *l) 1315 { 1316 int error; 1317 char namebuf[32]; 1318 struct wsdisplay_font fd; 1319 #ifdef WSDISPLAY_SCROLLSUPPORT 1320 struct wsdisplay_scroll_data *ksdp, *usdp; 1321 #endif 1322 1323 #if NWSKBD > 0 1324 struct wsevsrc *inp; 1325 1326 #ifdef WSDISPLAY_COMPAT_RAWKBD 1327 switch (cmd) { 1328 case WSKBDIO_SETMODE: 1329 scr->scr_rawkbd = (*(int *)data == WSKBD_RAW); 1330 return wsdisplay_update_rawkbd(sc, scr); 1331 case WSKBDIO_GETMODE: 1332 *(int *)data = (scr->scr_rawkbd ? 1333 WSKBD_RAW : WSKBD_TRANSLATED); 1334 return 0; 1335 } 1336 #endif 1337 inp = sc->sc_input; 1338 if (inp == NULL) 1339 return ENXIO; 1340 error = wsevsrc_display_ioctl(inp, cmd, data, flag, l); 1341 if (error != EPASSTHROUGH) 1342 return error; 1343 #endif /* NWSKBD > 0 */ 1344 1345 switch (cmd) { 1346 case WSDISPLAYIO_GMODE: 1347 if (scr->scr_flags & SCR_GRAPHICS) { 1348 if (scr->scr_flags & SCR_DUMBFB) 1349 *(u_int *)data = WSDISPLAYIO_MODE_DUMBFB; 1350 else 1351 *(u_int *)data = WSDISPLAYIO_MODE_MAPPED; 1352 } else 1353 *(u_int *)data = WSDISPLAYIO_MODE_EMUL; 1354 return 0; 1355 1356 case WSDISPLAYIO_SMODE: 1357 #define d (*(int *)data) 1358 if (d != WSDISPLAYIO_MODE_EMUL && 1359 d != WSDISPLAYIO_MODE_MAPPED && 1360 d != WSDISPLAYIO_MODE_DUMBFB) 1361 return EINVAL; 1362 1363 if (WSSCREEN_HAS_EMULATOR(scr)) { 1364 scr->scr_flags &= ~SCR_GRAPHICS; 1365 if (d == WSDISPLAYIO_MODE_MAPPED || 1366 d == WSDISPLAYIO_MODE_DUMBFB) 1367 scr->scr_flags |= SCR_GRAPHICS | 1368 ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0); 1369 } else if (d == WSDISPLAYIO_MODE_EMUL) 1370 return EINVAL; 1371 1372 (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie, 1373 scr->scr_dconf->emulcookie, cmd, data, flag, l); 1374 1375 return 0; 1376 #undef d 1377 1378 #ifdef WSDISPLAY_SCROLLSUPPORT 1379 #define SETSCROLLLINES(dstp, srcp, dfltp) \ 1380 do { \ 1381 (dstp)->fastlines = ((srcp)->which & \ 1382 WSDISPLAY_SCROLL_DOFASTLINES) ? \ 1383 (srcp)->fastlines : (dfltp)->fastlines; \ 1384 (dstp)->slowlines = ((srcp)->which & \ 1385 WSDISPLAY_SCROLL_DOSLOWLINES) ? \ 1386 (srcp)->slowlines : (dfltp)->slowlines; \ 1387 (dstp)->which = WSDISPLAY_SCROLL_DOALL; \ 1388 } while (0) 1389 1390 1391 case WSDISPLAYIO_DSSCROLL: 1392 usdp = (struct wsdisplay_scroll_data *)data; 1393 ksdp = &sc->sc_scroll_values; 1394 SETSCROLLLINES(ksdp, usdp, ksdp); 1395 return 0; 1396 1397 case WSDISPLAYIO_DGSCROLL: 1398 usdp = (struct wsdisplay_scroll_data *)data; 1399 ksdp = &sc->sc_scroll_values; 1400 SETSCROLLLINES(usdp, ksdp, ksdp); 1401 return 0; 1402 #else 1403 case WSDISPLAYIO_DSSCROLL: 1404 case WSDISPLAYIO_DGSCROLL: 1405 return ENODEV; 1406 #endif 1407 1408 case WSDISPLAYIO_SFONT: 1409 #define d ((struct wsdisplay_usefontdata *)data) 1410 if (!sc->sc_accessops->load_font) 1411 return EINVAL; 1412 if (d->name) { 1413 error = copyinstr(d->name, namebuf, sizeof(namebuf), 0); 1414 if (error) 1415 return error; 1416 fd.name = namebuf; 1417 } else 1418 fd.name = 0; 1419 fd.data = 0; 1420 error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 1421 scr->scr_dconf->emulcookie, &fd); 1422 if (!error && WSSCREEN_HAS_EMULATOR(scr)) { 1423 (*scr->scr_dconf->wsemul->reset) 1424 (scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT); 1425 if (scr->scr_dconf->wsemul->resize) { 1426 (*scr->scr_dconf->wsemul->resize) 1427 (scr->scr_dconf->wsemulcookie, 1428 scr->scr_dconf->scrdata); 1429 /* update the tty's size */ 1430 scr->scr_tty->t_winsize.ws_row = 1431 scr->scr_dconf->scrdata->nrows; 1432 scr->scr_tty->t_winsize.ws_col = 1433 scr->scr_dconf->scrdata->ncols; 1434 /* send SIGWINCH to the process group on our tty */ 1435 kpreempt_disable(); 1436 ttysig(scr->scr_tty, TTYSIG_PG1, SIGWINCH); 1437 kpreempt_enable(); 1438 } 1439 } 1440 return error; 1441 #undef d 1442 1443 #ifdef WSDISPLAY_CUSTOM_OUTPUT 1444 case WSDISPLAYIO_GMSGATTRS: 1445 #define d ((struct wsdisplay_msgattrs *)data) 1446 (*scr->scr_dconf->wsemul->getmsgattrs) 1447 (scr->scr_dconf->wsemulcookie, d); 1448 return 0; 1449 #undef d 1450 1451 case WSDISPLAYIO_SMSGATTRS: { 1452 #define d ((struct wsdisplay_msgattrs *)data) 1453 int i; 1454 for (i = 0; i < WSDISPLAY_MAXSCREEN; i++) 1455 if (sc->sc_scr[i] != NULL) 1456 (*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs) 1457 (sc->sc_scr[i]->scr_dconf->wsemulcookie, 1458 sc->sc_scr[i]->scr_dconf->scrdata, 1459 d); 1460 } 1461 return 0; 1462 #undef d 1463 #else 1464 case WSDISPLAYIO_GMSGATTRS: 1465 case WSDISPLAYIO_SMSGATTRS: 1466 return ENODEV; 1467 #endif 1468 case WSDISPLAYIO_SETVERSION: 1469 return wsevent_setversion(&sc->evar, *(int *)data); 1470 } 1471 1472 /* check ioctls for display */ 1473 return (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, 1474 scr->scr_dconf->emulcookie, cmd, data, flag, l); 1475 } 1476 1477 int 1478 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data, 1479 int flag, struct lwp *l) 1480 { 1481 switch (cmd) { 1482 case WSDISPLAYIO_GETACTIVESCREEN: 1483 *(int*)data = wsdisplay_getactivescreen(sc); 1484 return 0; 1485 } 1486 1487 return EPASSTHROUGH; 1488 } 1489 1490 int 1491 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data, 1492 int flag, struct lwp *l) 1493 { 1494 int error; 1495 char *type, typebuf[16], *emul, emulbuf[16]; 1496 void *tbuf; 1497 u_int fontsz; 1498 #if defined(COMPAT_14) && NWSKBD > 0 1499 struct wsmux_device wsmuxdata; 1500 #endif 1501 #if NWSKBD > 0 1502 struct wsevsrc *inp; 1503 #endif 1504 1505 switch (cmd) { 1506 case WSDISPLAYIO_ADDSCREEN: 1507 #define d ((struct wsdisplay_addscreendata *)data) 1508 if (d->screentype) { 1509 error = copyinstr(d->screentype, typebuf, 1510 sizeof(typebuf), 0); 1511 if (error) 1512 return error; 1513 type = typebuf; 1514 } else 1515 type = 0; 1516 if (d->emul) { 1517 error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0); 1518 if (error) 1519 return error; 1520 emul = emulbuf; 1521 } else 1522 emul = 0; 1523 1524 if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0) 1525 wsdisplay_addscreen_print(sc, d->idx, 0); 1526 return error; 1527 #undef d 1528 case WSDISPLAYIO_DELSCREEN: 1529 #define d ((struct wsdisplay_delscreendata *)data) 1530 return wsdisplay_delscreen(sc, d->idx, d->flags); 1531 #undef d 1532 case WSDISPLAYIO_LDFONT: 1533 #define d ((struct wsdisplay_font *)data) 1534 if (!sc->sc_accessops->load_font) 1535 return EINVAL; 1536 if (d->name) { 1537 error = copyinstr(d->name, typebuf, sizeof(typebuf), 0); 1538 if (error) 1539 return error; 1540 d->name = typebuf; 1541 } else 1542 d->name = "loaded"; /* ??? */ 1543 fontsz = d->fontheight * d->stride * d->numchars; 1544 if (fontsz > WSDISPLAY_MAXFONTSZ) { 1545 d->name = NULL; 1546 return EINVAL; 1547 } 1548 1549 tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK); 1550 error = copyin(d->data, tbuf, fontsz); 1551 if (error) { 1552 d->name = NULL; 1553 free(tbuf, M_DEVBUF); 1554 return error; 1555 } 1556 d->data = tbuf; 1557 error = 1558 (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d); 1559 d->name = NULL; 1560 free(tbuf, M_DEVBUF); 1561 #undef d 1562 return error; 1563 1564 #if NWSKBD > 0 1565 #ifdef COMPAT_14 1566 case _O_WSDISPLAYIO_SETKEYBOARD: 1567 #define d ((struct wsdisplay_kbddata *)data) 1568 inp = sc->sc_input; 1569 if (inp == NULL) 1570 return ENXIO; 1571 switch (d->op) { 1572 case _O_WSDISPLAY_KBD_ADD: 1573 if (d->idx == -1) { 1574 d->idx = wskbd_pickfree(); 1575 if (d->idx == -1) 1576 return ENXIO; 1577 } 1578 wsmuxdata.type = WSMUX_KBD; 1579 wsmuxdata.idx = d->idx; 1580 return wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE, 1581 &wsmuxdata, flag, l); 1582 case _O_WSDISPLAY_KBD_DEL: 1583 wsmuxdata.type = WSMUX_KBD; 1584 wsmuxdata.idx = d->idx; 1585 return wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE, 1586 &wsmuxdata, flag, l); 1587 default: 1588 return EINVAL; 1589 } 1590 #undef d 1591 #endif 1592 1593 case WSMUXIO_ADD_DEVICE: 1594 #define d ((struct wsmux_device *)data) 1595 if (d->idx == -1 && d->type == WSMUX_KBD) 1596 d->idx = wskbd_pickfree(); 1597 #undef d 1598 /* FALLTHROUGH */ 1599 case WSMUXIO_INJECTEVENT: 1600 case WSMUXIO_REMOVE_DEVICE: 1601 case WSMUXIO_LIST_DEVICES: 1602 inp = sc->sc_input; 1603 if (inp == NULL) 1604 return ENXIO; 1605 return wsevsrc_ioctl(inp, cmd, data, flag, l); 1606 #endif /* NWSKBD > 0 */ 1607 1608 } 1609 return EPASSTHROUGH; 1610 } 1611 1612 int 1613 wsdisplay_stat_inject(device_t dv, u_int type, int value) 1614 { 1615 struct wsdisplay_softc *sc = device_private(dv); 1616 struct wseventvar *evar; 1617 struct wscons_event event; 1618 1619 evar = &sc->evar; 1620 1621 if (evar == NULL) 1622 return 0; 1623 1624 if (evar->q == NULL) 1625 return 1; 1626 1627 event.type = type; 1628 event.value = value; 1629 if (wsevent_inject(evar, &event, 1) != 0) { 1630 log(LOG_WARNING, "wsdisplay: event queue overflow\n"); 1631 return 1; 1632 } 1633 1634 return 0; 1635 } 1636 1637 paddr_t 1638 wsdisplaymmap(dev_t dev, off_t offset, int prot) 1639 { 1640 struct wsdisplay_softc *sc; 1641 struct wsscreen *scr; 1642 1643 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev)); 1644 1645 if (ISWSDISPLAYSTAT(dev)) 1646 return -1; 1647 1648 if (ISWSDISPLAYCTL(dev)) 1649 return -1; 1650 1651 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL) 1652 return -1; 1653 1654 if (!(scr->scr_flags & SCR_GRAPHICS)) 1655 return -1; 1656 1657 /* pass mmap to display */ 1658 return (*sc->sc_accessops->mmap)(sc->sc_accesscookie, 1659 scr->scr_dconf->emulcookie, offset, prot); 1660 } 1661 1662 void 1663 wsdisplaystart(struct tty *tp) 1664 { 1665 struct wsdisplay_softc *sc; 1666 struct wsscreen *scr; 1667 int s, n; 1668 u_char *tbuf; 1669 1670 s = spltty(); 1671 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 1672 splx(s); 1673 return; 1674 } 1675 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev)); 1676 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) { 1677 splx(s); 1678 return; 1679 } 1680 1681 if (scr->scr_hold_screen) { 1682 tp->t_state |= TS_TIMEOUT; 1683 splx(s); 1684 return; 1685 } 1686 tp->t_state |= TS_BUSY; 1687 splx(s); 1688 1689 #ifdef DIAGNOSTIC 1690 scr->scr_in_ttyoutput = 1; 1691 #endif 1692 1693 /* 1694 * Drain output from ring buffer. 1695 * The output will normally be in one contiguous chunk, but when the 1696 * ring wraps, it will be in two pieces.. one at the end of the ring, 1697 * the other at the start. For performance, rather than loop here, 1698 * we output one chunk, see if there's another one, and if so, output 1699 * it too. 1700 */ 1701 1702 n = ndqb(&tp->t_outq, 0); 1703 tbuf = tp->t_outq.c_cf; 1704 1705 if (!(scr->scr_flags & SCR_GRAPHICS)) { 1706 KASSERT(WSSCREEN_HAS_EMULATOR(scr)); 1707 (*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie, 1708 tbuf, n, 0); 1709 #ifdef WSDISPLAY_MULTICONS 1710 if (wsdisplay_multicons_enable && 1711 scr->scr_dconf == &wsdisplay_console_conf && 1712 wsdisplay_ocn && wsdisplay_ocn->cn_putc) { 1713 for (int i = 0; i < n; i++) 1714 wsdisplay_ocn->cn_putc( 1715 wsdisplay_ocn->cn_dev, tbuf[i]); 1716 } 1717 #endif 1718 } 1719 ndflush(&tp->t_outq, n); 1720 1721 if ((n = ndqb(&tp->t_outq, 0)) > 0) { 1722 tbuf = tp->t_outq.c_cf; 1723 1724 if (!(scr->scr_flags & SCR_GRAPHICS)) { 1725 KASSERT(WSSCREEN_HAS_EMULATOR(scr)); 1726 (*scr->scr_dconf->wsemul->output) 1727 (scr->scr_dconf->wsemulcookie, tbuf, n, 0); 1728 1729 #ifdef WSDISPLAY_MULTICONS 1730 if (wsdisplay_multicons_enable && 1731 scr->scr_dconf == &wsdisplay_console_conf && 1732 wsdisplay_ocn && wsdisplay_ocn->cn_putc) { 1733 for (int i = 0; i < n; i++) 1734 wsdisplay_ocn->cn_putc( 1735 wsdisplay_ocn->cn_dev, tbuf[i]); 1736 } 1737 #endif 1738 } 1739 ndflush(&tp->t_outq, n); 1740 } 1741 1742 #ifdef DIAGNOSTIC 1743 scr->scr_in_ttyoutput = 0; 1744 #endif 1745 1746 s = spltty(); 1747 tp->t_state &= ~TS_BUSY; 1748 /* Come back if there's more to do */ 1749 if (ttypull(tp)) { 1750 tp->t_state |= TS_TIMEOUT; 1751 callout_schedule(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1); 1752 } 1753 splx(s); 1754 } 1755 1756 void 1757 wsdisplaystop(struct tty *tp, int flag) 1758 { 1759 int s; 1760 1761 s = spltty(); 1762 if (ISSET(tp->t_state, TS_BUSY)) 1763 if (!ISSET(tp->t_state, TS_TTSTOP)) 1764 SET(tp->t_state, TS_FLUSH); 1765 splx(s); 1766 } 1767 1768 /* Set line parameters. */ 1769 int 1770 wsdisplayparam(struct tty *tp, struct termios *t) 1771 { 1772 1773 tp->t_ispeed = t->c_ispeed; 1774 tp->t_ospeed = t->c_ospeed; 1775 tp->t_cflag = t->c_cflag; 1776 return 0; 1777 } 1778 1779 /* 1780 * Callbacks for the emulation code. 1781 */ 1782 void 1783 wsdisplay_emulbell(void *v) 1784 { 1785 struct wsscreen *scr = v; 1786 1787 if (scr == NULL) /* console, before real attach */ 1788 return; 1789 1790 if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */ 1791 return; 1792 1793 (void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL, 1794 FWRITE, NULL); 1795 } 1796 1797 void 1798 wsdisplay_emulinput(void *v, const u_char *data, u_int count) 1799 { 1800 struct wsscreen *scr = v; 1801 struct tty *tp; 1802 int (*ifcn)(int, struct tty *); 1803 1804 if (v == NULL) /* console, before real attach */ 1805 return; 1806 1807 if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */ 1808 return; 1809 if (!WSSCREEN_HAS_TTY(scr)) 1810 return; 1811 1812 tp = scr->scr_tty; 1813 1814 /* 1815 * XXX bad hack to work around locking problems in tty.c: 1816 * ttyinput() will try to lock again, causing deadlock. 1817 * We assume that wsdisplay_emulinput() can only be called 1818 * from within wsdisplaystart(), and thus the tty lock 1819 * is already held. Use an entry point which doesn't lock. 1820 */ 1821 KASSERT(scr->scr_in_ttyoutput); 1822 ifcn = tp->t_linesw->l_rint; 1823 if (ifcn == ttyinput) 1824 ifcn = ttyinput_wlock; 1825 1826 while (count-- > 0) 1827 (*ifcn)(*data++, tp); 1828 } 1829 1830 /* 1831 * Calls from the keyboard interface. 1832 */ 1833 void 1834 wsdisplay_kbdinput(device_t dv, keysym_t ks) 1835 { 1836 struct wsdisplay_softc *sc = device_private(dv); 1837 struct wsscreen *scr; 1838 const char *dp; 1839 int count; 1840 struct tty *tp; 1841 1842 KASSERT(sc != NULL); 1843 1844 scr = sc->sc_focus; 1845 1846 if (!scr || !WSSCREEN_HAS_TTY(scr)) 1847 return; 1848 1849 tp = scr->scr_tty; 1850 1851 if (KS_GROUP(ks) == KS_GROUP_Plain && KS_VALUE(ks) <= 0x7f) 1852 (*tp->t_linesw->l_rint)(KS_VALUE(ks), tp); 1853 else if (WSSCREEN_HAS_EMULATOR(scr)) { 1854 count = (*scr->scr_dconf->wsemul->translate) 1855 (scr->scr_dconf->wsemulcookie, ks, &dp); 1856 while (count-- > 0) 1857 (*tp->t_linesw->l_rint)((unsigned char)(*dp++), tp); 1858 } 1859 } 1860 1861 #if defined(WSDISPLAY_COMPAT_RAWKBD) 1862 int 1863 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr) 1864 { 1865 #if NWSKBD > 0 1866 int s, raw, data, error; 1867 struct wsevsrc *inp; 1868 1869 s = spltty(); 1870 1871 raw = (scr ? scr->scr_rawkbd : 0); 1872 1873 if (scr != sc->sc_focus || 1874 sc->sc_rawkbd == raw) { 1875 splx(s); 1876 return 0; 1877 } 1878 1879 data = raw ? WSKBD_RAW : WSKBD_TRANSLATED; 1880 inp = sc->sc_input; 1881 if (inp == NULL) { 1882 splx(s); 1883 return ENXIO; 1884 } 1885 error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0); 1886 if (!error) 1887 sc->sc_rawkbd = raw; 1888 splx(s); 1889 return error; 1890 #else 1891 return 0; 1892 #endif 1893 } 1894 #endif 1895 1896 static void 1897 wsdisplay_switch3_cb(void *arg, int error, int waitok) 1898 { 1899 device_t dv = arg; 1900 1901 wsdisplay_switch3(dv, error, waitok); 1902 } 1903 1904 static int 1905 wsdisplay_switch3(device_t dv, int error, int waitok) 1906 { 1907 struct wsdisplay_softc *sc = device_private(dv); 1908 int no; 1909 struct wsscreen *scr; 1910 1911 if (!(sc->sc_flags & SC_SWITCHPENDING)) { 1912 aprint_error_dev(dv, "wsdisplay_switch3: not switching\n"); 1913 return EINVAL; 1914 } 1915 1916 no = sc->sc_screenwanted; 1917 if (no < 0 || no >= WSDISPLAY_MAXSCREEN) 1918 panic("wsdisplay_switch3: invalid screen %d", no); 1919 scr = sc->sc_scr[no]; 1920 if (!scr) { 1921 aprint_error_dev(dv, 1922 "wsdisplay_switch3: screen %d disappeared\n", no); 1923 error = ENXIO; 1924 } 1925 1926 if (error) { 1927 /* try to recover, avoid recursion */ 1928 1929 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) { 1930 aprint_error_dev(dv, "wsdisplay_switch3: giving up\n"); 1931 sc->sc_focus = 0; 1932 #ifdef WSDISPLAY_COMPAT_RAWKBD 1933 wsdisplay_update_rawkbd(sc, 0); 1934 #endif 1935 sc->sc_flags &= ~SC_SWITCHPENDING; 1936 return error; 1937 } 1938 1939 sc->sc_screenwanted = sc->sc_oldscreen; 1940 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN; 1941 return wsdisplay_switch1(dv, 0, waitok); 1942 } 1943 1944 if (scr->scr_syncops && !error) 1945 sc->sc_flags |= SC_XATTACHED; 1946 1947 sc->sc_flags &= ~SC_SWITCHPENDING; 1948 1949 if (!error && (scr->scr_flags & SCR_WAITACTIVE)) 1950 wakeup(scr); 1951 return error; 1952 } 1953 1954 static void 1955 wsdisplay_switch2_cb(void *arg, int error, int waitok) 1956 { 1957 device_t dv = arg; 1958 1959 wsdisplay_switch2(dv, error, waitok); 1960 } 1961 1962 static int 1963 wsdisplay_switch2(device_t dv, int error, int waitok) 1964 { 1965 struct wsdisplay_softc *sc = device_private(dv); 1966 int no; 1967 struct wsscreen *scr; 1968 1969 if (!(sc->sc_flags & SC_SWITCHPENDING)) { 1970 aprint_error_dev(dv, "wsdisplay_switch2: not switching\n"); 1971 return EINVAL; 1972 } 1973 1974 no = sc->sc_screenwanted; 1975 if (no < 0 || no >= WSDISPLAY_MAXSCREEN) 1976 panic("wsdisplay_switch2: invalid screen %d", no); 1977 scr = sc->sc_scr[no]; 1978 if (!scr) { 1979 aprint_error_dev(dv, 1980 "wsdisplay_switch2: screen %d disappeared\n", no); 1981 error = ENXIO; 1982 } 1983 1984 if (error) { 1985 /* try to recover, avoid recursion */ 1986 1987 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) { 1988 aprint_error_dev(dv, "wsdisplay_switch2: giving up\n"); 1989 sc->sc_focus = 0; 1990 sc->sc_flags &= ~SC_SWITCHPENDING; 1991 return error; 1992 } 1993 1994 sc->sc_screenwanted = sc->sc_oldscreen; 1995 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN; 1996 return wsdisplay_switch1(dv, 0, waitok); 1997 } 1998 1999 sc->sc_focusidx = no; 2000 sc->sc_focus = scr; 2001 2002 #ifdef WSDISPLAY_COMPAT_RAWKBD 2003 (void) wsdisplay_update_rawkbd(sc, scr); 2004 #endif 2005 /* keyboard map??? */ 2006 2007 if (scr->scr_syncops && 2008 !(sc->sc_isconsole && wsdisplay_cons_pollmode)) { 2009 error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok, 2010 wsdisplay_switch3_cb, dv); 2011 if (error == EAGAIN) { 2012 /* switch will be done asynchronously */ 2013 return 0; 2014 } 2015 } 2016 2017 return wsdisplay_switch3(dv, error, waitok); 2018 } 2019 2020 static void 2021 wsdisplay_switch1_cb(void *arg, int error, int waitok) 2022 { 2023 device_t dv = arg; 2024 2025 wsdisplay_switch1(dv, error, waitok); 2026 } 2027 2028 static int 2029 wsdisplay_switch1(device_t dv, int error, int waitok) 2030 { 2031 struct wsdisplay_softc *sc = device_private(dv); 2032 int no; 2033 struct wsscreen *scr; 2034 2035 if (!(sc->sc_flags & SC_SWITCHPENDING)) { 2036 aprint_error_dev(dv, "wsdisplay_switch1: not switching\n"); 2037 return EINVAL; 2038 } 2039 2040 no = sc->sc_screenwanted; 2041 if (no == WSDISPLAY_NULLSCREEN) { 2042 sc->sc_flags &= ~SC_SWITCHPENDING; 2043 if (!error) { 2044 sc->sc_flags &= ~SC_XATTACHED; 2045 sc->sc_focus = 0; 2046 } 2047 wakeup(sc); 2048 return error; 2049 } 2050 if (no < 0 || no >= WSDISPLAY_MAXSCREEN) 2051 panic("wsdisplay_switch1: invalid screen %d", no); 2052 scr = sc->sc_scr[no]; 2053 if (!scr) { 2054 aprint_error_dev(dv, 2055 "wsdisplay_switch1: screen %d disappeared\n", no); 2056 error = ENXIO; 2057 } 2058 2059 if (error) { 2060 sc->sc_flags &= ~SC_SWITCHPENDING; 2061 return error; 2062 } 2063 2064 sc->sc_flags &= ~SC_XATTACHED; 2065 2066 error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie, 2067 scr->scr_dconf->emulcookie, 2068 waitok, 2069 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch2_cb, dv); 2070 if (error == EAGAIN) { 2071 /* switch will be done asynchronously */ 2072 return 0; 2073 } 2074 2075 return wsdisplay_switch2(dv, error, waitok); 2076 } 2077 2078 int 2079 wsdisplay_switch(device_t dv, int no, int waitok) 2080 { 2081 struct wsdisplay_softc *sc = device_private(dv); 2082 int s, res = 0; 2083 struct wsscreen *scr; 2084 2085 if (no != WSDISPLAY_NULLSCREEN) { 2086 if ((no < 0 || no >= WSDISPLAY_MAXSCREEN)) 2087 return EINVAL; 2088 if (sc->sc_scr[no] == NULL) 2089 return ENXIO; 2090 } 2091 2092 wsdisplay_stat_inject(dv, WSCONS_EVENT_SCREEN_SWITCH, no); 2093 2094 s = spltty(); 2095 2096 if ((sc->sc_focus && no == sc->sc_focusidx) || 2097 (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) { 2098 splx(s); 2099 return 0; 2100 } 2101 2102 if (sc->sc_flags & SC_SWITCHPENDING) { 2103 splx(s); 2104 return EBUSY; 2105 } 2106 2107 sc->sc_flags |= SC_SWITCHPENDING; 2108 sc->sc_screenwanted = no; 2109 2110 splx(s); 2111 2112 scr = sc->sc_focus; 2113 if (!scr) { 2114 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN; 2115 return wsdisplay_switch1(dv, 0, waitok); 2116 } else 2117 sc->sc_oldscreen = sc->sc_focusidx; 2118 2119 if (scr->scr_syncops) { 2120 if (!(sc->sc_flags & SC_XATTACHED) || 2121 (sc->sc_isconsole && wsdisplay_cons_pollmode)) { 2122 /* nothing to do here */ 2123 return wsdisplay_switch1(dv, 0, waitok); 2124 } 2125 res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok, 2126 wsdisplay_switch1_cb, dv); 2127 if (res == EAGAIN) { 2128 /* switch will be done asynchronously */ 2129 return 0; 2130 } 2131 } else if (scr->scr_flags & SCR_GRAPHICS) { 2132 /* no way to save state */ 2133 res = EBUSY; 2134 } 2135 2136 return wsdisplay_switch1(dv, res, waitok); 2137 } 2138 2139 void 2140 wsdisplay_reset(device_t dv, enum wsdisplay_resetops op) 2141 { 2142 struct wsdisplay_softc *sc = device_private(dv); 2143 struct wsscreen *scr; 2144 2145 KASSERT(sc != NULL); 2146 scr = sc->sc_focus; 2147 2148 if (!scr) 2149 return; 2150 2151 switch (op) { 2152 case WSDISPLAY_RESETEMUL: 2153 if (!WSSCREEN_HAS_EMULATOR(scr)) 2154 break; 2155 (*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie, 2156 WSEMUL_RESET); 2157 break; 2158 case WSDISPLAY_RESETCLOSE: 2159 wsdisplay_closescreen(sc, scr); 2160 break; 2161 } 2162 } 2163 2164 2165 bool 2166 wsdisplay_isconsole(struct wsdisplay_softc *sc) 2167 { 2168 return sc->sc_isconsole; 2169 } 2170 2171 /* 2172 * Interface for (external) VT switch / process synchronization code 2173 */ 2174 int 2175 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops, 2176 void *cookie) 2177 { 2178 if (scr->scr_syncops) { 2179 /* 2180 * The screen is already claimed. 2181 * Check if the owner is still alive. 2182 */ 2183 if ((*scr->scr_syncops->check)(scr->scr_synccookie)) 2184 return EBUSY; 2185 } 2186 scr->scr_syncops = ops; 2187 scr->scr_synccookie = cookie; 2188 if (scr == scr->sc->sc_focus) 2189 scr->sc->sc_flags |= SC_XATTACHED; 2190 return 0; 2191 } 2192 2193 int 2194 wsscreen_detach_sync(struct wsscreen *scr) 2195 { 2196 if (!scr->scr_syncops) 2197 return EINVAL; 2198 scr->scr_syncops = 0; 2199 if (scr == scr->sc->sc_focus) 2200 scr->sc->sc_flags &= ~SC_XATTACHED; 2201 return 0; 2202 } 2203 2204 int 2205 wsscreen_lookup_sync(struct wsscreen *scr, 2206 const struct wscons_syncops *ops, /* used as ID */ 2207 void **cookiep) 2208 { 2209 if (!scr->scr_syncops || ops != scr->scr_syncops) 2210 return EINVAL; 2211 *cookiep = scr->scr_synccookie; 2212 return 0; 2213 } 2214 2215 /* 2216 * Interface to virtual screen stuff 2217 */ 2218 int 2219 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc) 2220 { 2221 return (WSDISPLAY_MAXSCREEN - 1); 2222 } 2223 2224 int 2225 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx) 2226 { 2227 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN) 2228 return EINVAL; 2229 if (!sc->sc_scr[idx]) 2230 return ENXIO; 2231 return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0); 2232 } 2233 2234 int 2235 wsdisplay_getactivescreen(struct wsdisplay_softc *sc) 2236 { 2237 return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN); 2238 } 2239 2240 int 2241 wsscreen_switchwait(struct wsdisplay_softc *sc, int no) 2242 { 2243 struct wsscreen *scr; 2244 int s, res = 0; 2245 2246 if (no == WSDISPLAY_NULLSCREEN) { 2247 s = spltty(); 2248 while (sc->sc_focus && res == 0) { 2249 res = tsleep(sc, PCATCH, "wswait", 0); 2250 } 2251 splx(s); 2252 return res; 2253 } 2254 2255 if (no < 0 || no >= WSDISPLAY_MAXSCREEN) 2256 return ENXIO; 2257 scr = sc->sc_scr[no]; 2258 if (!scr) 2259 return ENXIO; 2260 2261 s = spltty(); 2262 if (scr != sc->sc_focus) { 2263 scr->scr_flags |= SCR_WAITACTIVE; 2264 res = tsleep(scr, PCATCH, "wswait", 0); 2265 if (scr != sc->sc_scr[no]) 2266 res = ENXIO; /* disappeared in the meantime */ 2267 else 2268 scr->scr_flags &= ~SCR_WAITACTIVE; 2269 } 2270 splx(s); 2271 return res; 2272 } 2273 2274 void 2275 wsdisplay_kbdholdscreen(device_t dv, int hold) 2276 { 2277 struct wsdisplay_softc *sc = device_private(dv); 2278 struct wsscreen *scr; 2279 2280 scr = sc->sc_focus; 2281 2282 if (!scr) 2283 return; 2284 2285 if (hold) 2286 scr->scr_hold_screen = 1; 2287 else { 2288 scr->scr_hold_screen = 0; 2289 callout_schedule(&scr->scr_tty->t_rstrt_ch, 0); 2290 } 2291 } 2292 2293 #if NWSKBD > 0 2294 void 2295 wsdisplay_set_console_kbd(struct wsevsrc *src) 2296 { 2297 if (wsdisplay_console_device == NULL) { 2298 src->me_dispdv = NULL; 2299 return; 2300 } 2301 #if NWSMUX > 0 2302 if (wsmux_attach_sc((struct wsmux_softc *) 2303 wsdisplay_console_device->sc_input, src)) { 2304 src->me_dispdv = NULL; 2305 return; 2306 } 2307 #else 2308 wsdisplay_console_device->sc_input = src; 2309 #endif 2310 src->me_dispdv = wsdisplay_console_device->sc_dev; 2311 } 2312 #endif /* NWSKBD > 0 */ 2313 2314 /* 2315 * Console interface. 2316 */ 2317 void 2318 wsdisplay_cnputc(dev_t dev, int i) 2319 { 2320 struct wsscreen_internal *dc; 2321 u_char c = i; 2322 2323 if (!wsdisplay_console_initted) 2324 return; 2325 2326 if ((wsdisplay_console_device != NULL) && 2327 (wsdisplay_console_device->sc_scr[0] != NULL) && 2328 (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS)) 2329 return; 2330 2331 dc = &wsdisplay_console_conf; 2332 (*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1); 2333 2334 #ifdef WSDISPLAY_MULTICONS 2335 if (!wsdisplay_multicons_suspended && 2336 wsdisplay_multicons_enable && wsdisplay_ocn && wsdisplay_ocn->cn_putc) 2337 wsdisplay_ocn->cn_putc(wsdisplay_ocn->cn_dev, i); 2338 #endif 2339 } 2340 2341 static int 2342 wsdisplay_getc(dev_t dev) 2343 { 2344 int c; 2345 2346 if (wsdisplay_cons_kbd_getc) { 2347 c = wsdisplay_cons_kbd_getc(wsdisplay_cons.cn_dev); 2348 if (c >= 0) 2349 return c; 2350 } 2351 2352 #ifdef WSDISPLAY_MULTICONS 2353 if (!wsdisplay_multicons_suspended && 2354 wsdisplay_multicons_enable && wsdisplay_ocn && wsdisplay_ocn->cn_getc) { 2355 c = wsdisplay_ocn->cn_getc(wsdisplay_ocn->cn_dev); 2356 if (c >= 0) 2357 return c; 2358 } 2359 #endif 2360 return -1; 2361 } 2362 2363 static void 2364 wsdisplay_pollc(dev_t dev, int on) 2365 { 2366 2367 wsdisplay_cons_pollmode = on; 2368 2369 /* notify to fb drivers */ 2370 if (wsdisplay_console_device != NULL && 2371 wsdisplay_console_device->sc_accessops->pollc != NULL) 2372 (*wsdisplay_console_device->sc_accessops->pollc) 2373 (wsdisplay_console_device->sc_accesscookie, on); 2374 2375 /* notify to kbd drivers */ 2376 if (wsdisplay_cons_kbd_pollc) 2377 (*wsdisplay_cons_kbd_pollc)(NODEV, on); 2378 2379 #ifdef WSDISPLAY_MULTICONS 2380 /* notify to old console driver */ 2381 if (!wsdisplay_multicons_suspended && 2382 wsdisplay_multicons_enable && wsdisplay_ocn && wsdisplay_ocn->cn_pollc) 2383 wsdisplay_ocn->cn_pollc(wsdisplay_ocn->cn_dev, on); 2384 #endif 2385 } 2386 2387 void 2388 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int), 2389 void (*bell)(dev_t, u_int, u_int, u_int)) 2390 { 2391 wsdisplay_cons.cn_bell = bell; 2392 wsdisplay_cons_kbd_getc = get; 2393 wsdisplay_cons_kbd_pollc = poll; 2394 } 2395 2396 void 2397 wsdisplay_unset_cons_kbd(void) 2398 { 2399 wsdisplay_cons.cn_bell = NULL; 2400 wsdisplay_cons_kbd_getc = NULL; 2401 wsdisplay_cons_kbd_pollc = NULL; 2402 } 2403 2404 #ifdef WSDISPLAY_MULTICONS 2405 void 2406 wsdisplay_multicons_suspend(bool suspend) 2407 { 2408 wsdisplay_multicons_suspended = suspend; 2409 } 2410 #endif 2411 2412 #ifdef WSDISPLAY_MULTICONS 2413 SYSCTL_SETUP(sysctl_hw_wsdisplay_setup, "sysctl hw.wsdisplay subtree setup") 2414 { 2415 const struct sysctlnode *wsdisplay_node; 2416 2417 if (sysctl_createv(clog, 0, NULL, &wsdisplay_node, 2418 CTLFLAG_PERMANENT, 2419 CTLTYPE_NODE, "wsdisplay", NULL, 2420 NULL, 0, NULL, 0, 2421 CTL_HW, CTL_CREATE, CTL_EOL) != 0) 2422 return; 2423 2424 sysctl_createv(clog, 0, NULL, NULL, 2425 CTLFLAG_READWRITE, 2426 CTLTYPE_BOOL, "multicons", 2427 SYSCTL_DESCR("Enable wsdisplay multicons"), 2428 NULL, 0, &wsdisplay_multicons_enable, 0, 2429 CTL_HW, wsdisplay_node->sysctl_num, CTL_CREATE, CTL_EOL); 2430 } 2431 #endif 2432