Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.101
      1 /* $NetBSD: wsdisplay.c,v 1.101 2006/10/01 19:28:44 elad 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.101 2006/10/01 19:28:44 elad Exp $");
     35 
     36 #include "opt_wsdisplay_compat.h"
     37 #include "opt_wsmsgattrs.h"
     38 #include "opt_compat_netbsd.h"
     39 #include "wskbd.h"
     40 #include "wsmux.h"
     41 #include "wsdisplay.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/conf.h>
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/poll.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/malloc.h>
     51 #include <sys/syslog.h>
     52 #include <sys/systm.h>
     53 #include <sys/tty.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/errno.h>
     56 #include <sys/fcntl.h>
     57 #include <sys/vnode.h>
     58 #include <sys/kauth.h>
     59 
     60 #include <dev/wscons/wseventvar.h>
     61 #include <dev/wscons/wsmuxvar.h>
     62 #include <dev/wscons/wsconsio.h>
     63 #include <dev/wscons/wsdisplayvar.h>
     64 #include <dev/wscons/wsksymvar.h>
     65 #include <dev/wscons/wsksymdef.h>
     66 #include <dev/wscons/wsemulvar.h>
     67 #include <dev/wscons/wscons_callbacks.h>
     68 #include <dev/cons.h>
     69 
     70 struct wsscreen_internal {
     71 	const struct wsdisplay_emulops *emulops;
     72 	void	*emulcookie;
     73 
     74 	const struct wsscreen_descr *scrdata;
     75 
     76 	const struct wsemul_ops *wsemul;
     77 	void	*wsemulcookie;
     78 };
     79 
     80 struct wsscreen {
     81 	struct wsscreen_internal *scr_dconf;
     82 
     83 	struct tty *scr_tty;
     84 	int	scr_hold_screen;		/* hold tty output */
     85 
     86 	int scr_flags;
     87 #define SCR_OPEN 1		/* is it open? */
     88 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     89 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     90 #define	SCR_DUMBFB 8		/* in use as a dumb fb (iff SCR_GRAPHICS) */
     91 	const struct wscons_syncops *scr_syncops;
     92 	void *scr_synccookie;
     93 
     94 #ifdef WSDISPLAY_COMPAT_RAWKBD
     95 	int scr_rawkbd;
     96 #endif
     97 
     98 	struct wsdisplay_softc *sc;
     99 };
    100 
    101 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int,
    102 				 const char *,
    103 				 const struct wsscreen_descr *, void *,
    104 				 int, int, long);
    105 void wsscreen_detach(struct wsscreen *);
    106 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
    107 static void wsdisplay_shutdownhook(void *);
    108 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
    109 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
    110 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
    111 
    112 #define WSDISPLAY_MAXSCREEN 8
    113 
    114 struct wsdisplay_softc {
    115 	struct device sc_dv;
    116 
    117 	const struct wsdisplay_accessops *sc_accessops;
    118 	void	*sc_accesscookie;
    119 
    120 	const struct wsscreen_list *sc_scrdata;
    121 #ifdef WSDISPLAY_SCROLLSUPPORT
    122 	struct wsdisplay_scroll_data sc_scroll_values;
    123 #endif
    124 
    125 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    126 	int sc_focusidx;	/* available only if sc_focus isn't null */
    127 	struct wsscreen *sc_focus;
    128 
    129 	struct wseventvar evar;
    130 
    131 	int	sc_isconsole;
    132 
    133 	int sc_flags;
    134 #define SC_SWITCHPENDING 1
    135 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
    136 
    137 #if NWSKBD > 0
    138 	struct wsevsrc *sc_input;
    139 #ifdef WSDISPLAY_COMPAT_RAWKBD
    140 	int sc_rawkbd;
    141 #endif
    142 #endif /* NWSKBD > 0 */
    143 };
    144 
    145 #ifdef WSDISPLAY_SCROLLSUPPORT
    146 
    147 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
    148 	WSDISPLAY_SCROLL_DOALL,
    149 	25,
    150 	2,
    151 };
    152 #endif
    153 
    154 extern struct cfdriver wsdisplay_cd;
    155 
    156 /* Autoconfiguration definitions. */
    157 static int wsdisplay_emul_match(struct device *, struct cfdata *, void *);
    158 static void wsdisplay_emul_attach(struct device *, struct device *, void *);
    159 static int wsdisplay_noemul_match(struct device *, struct cfdata *, void *);
    160 static void wsdisplay_noemul_attach(struct device *, struct device *, void *);
    161 
    162 CFATTACH_DECL(wsdisplay_emul, sizeof (struct wsdisplay_softc),
    163     wsdisplay_emul_match, wsdisplay_emul_attach, NULL, NULL);
    164 
    165 CFATTACH_DECL(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
    166     wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
    167 
    168 dev_type_open(wsdisplayopen);
    169 dev_type_close(wsdisplayclose);
    170 dev_type_read(wsdisplayread);
    171 dev_type_write(wsdisplaywrite);
    172 dev_type_ioctl(wsdisplayioctl);
    173 dev_type_stop(wsdisplaystop);
    174 dev_type_tty(wsdisplaytty);
    175 dev_type_poll(wsdisplaypoll);
    176 dev_type_mmap(wsdisplaymmap);
    177 dev_type_kqfilter(wsdisplaykqfilter);
    178 
    179 const struct cdevsw wsdisplay_cdevsw = {
    180 	wsdisplayopen, wsdisplayclose, wsdisplayread, wsdisplaywrite,
    181 	wsdisplayioctl, wsdisplaystop, wsdisplaytty, wsdisplaypoll,
    182 	wsdisplaymmap, wsdisplaykqfilter, D_TTY
    183 };
    184 
    185 static void wsdisplaystart(struct tty *);
    186 static int wsdisplayparam(struct tty *, struct termios *);
    187 
    188 
    189 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    190 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    191 #define ISWSDISPLAYSTAT(dev)	(WSDISPLAYSCREEN(dev) == 254)
    192 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    193 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    194 
    195 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    196 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    197 
    198 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
    199 	    int console, int kbdmux, const struct wsscreen_list *,
    200 	    const struct wsdisplay_accessops *accessops,
    201 	    void *accesscookie);
    202 
    203 #ifdef WSDISPLAY_COMPAT_RAWKBD
    204 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
    205 				 struct wsscreen *);
    206 #endif
    207 
    208 static int wsdisplay_console_initted;
    209 static int wsdisplay_console_attached;
    210 static struct wsdisplay_softc *wsdisplay_console_device;
    211 static struct wsscreen_internal wsdisplay_console_conf;
    212 
    213 static int wsdisplay_getc_dummy(dev_t);
    214 static void wsdisplay_pollc(dev_t, int);
    215 
    216 static int wsdisplay_cons_pollmode;
    217 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
    218 
    219 static struct consdev wsdisplay_cons = {
    220 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
    221 	wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
    222 };
    223 
    224 #ifndef WSDISPLAY_DEFAULTSCREENS
    225 # define WSDISPLAY_DEFAULTSCREENS	0
    226 #endif
    227 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
    228 
    229 int wsdisplay_switch1(void *, int, int);
    230 int wsdisplay_switch2(void *, int, int);
    231 int wsdisplay_switch3(void *, int, int);
    232 
    233 int wsdisplay_clearonclose;
    234 
    235 struct wsscreen *
    236 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
    237 	const struct wsscreen_descr *type, void *cookie, int ccol,
    238 	int crow, long defattr)
    239 {
    240 	struct wsscreen_internal *dconf;
    241 	struct wsscreen *scr;
    242 
    243 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    244 	if (!scr)
    245 		return (NULL);
    246 
    247 	if (console) {
    248 		dconf = &wsdisplay_console_conf;
    249 		/*
    250 		 * If there's an emulation, tell it about the callback argument.
    251 		 * The other stuff is already there.
    252 		 */
    253 		if (dconf->wsemul != NULL)
    254 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    255 	} else { /* not console */
    256 		dconf = malloc(sizeof(struct wsscreen_internal),
    257 			       M_DEVBUF, M_NOWAIT);
    258 		dconf->emulops = type->textops;
    259 		dconf->emulcookie = cookie;
    260 		if (dconf->emulops) {
    261 			dconf->wsemul = wsemul_pick(emul);
    262 			if (dconf->wsemul == NULL) {
    263 				free(dconf, M_DEVBUF);
    264 				free(scr, M_DEVBUF);
    265 				return (NULL);
    266 			}
    267 			dconf->wsemulcookie =
    268 			  (*dconf->wsemul->attach)(0, type, cookie,
    269 						   ccol, crow, scr, defattr);
    270 		} else
    271 			dconf->wsemul = NULL;
    272 		dconf->scrdata = type;
    273 	}
    274 
    275 	scr->scr_dconf = dconf;
    276 
    277 	scr->scr_tty = ttymalloc();
    278 	tty_attach(scr->scr_tty);
    279 	scr->scr_hold_screen = 0;
    280 	if (WSSCREEN_HAS_EMULATOR(scr))
    281 		scr->scr_flags = 0;
    282 	else
    283 		scr->scr_flags = SCR_GRAPHICS;
    284 
    285 	scr->scr_syncops = 0;
    286 	scr->sc = sc;
    287 #ifdef WSDISPLAY_COMPAT_RAWKBD
    288 	scr->scr_rawkbd = 0;
    289 #endif
    290 	return (scr);
    291 }
    292 
    293 void
    294 wsscreen_detach(struct wsscreen *scr)
    295 {
    296 	u_int ccol, crow; /* XXX */
    297 
    298 	if (WSSCREEN_HAS_TTY(scr)) {
    299 		tty_detach(scr->scr_tty);
    300 		ttyfree(scr->scr_tty);
    301 	}
    302 	if (WSSCREEN_HAS_EMULATOR(scr))
    303 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    304 						  &ccol, &crow);
    305 	free(scr->scr_dconf, M_DEVBUF);
    306 	free(scr, M_DEVBUF);
    307 }
    308 
    309 const struct wsscreen_descr *
    310 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
    311 {
    312 	int i;
    313 	const struct wsscreen_descr *scr;
    314 
    315 	KASSERT(scrdata->nscreens > 0);
    316 
    317 	if (name == NULL)
    318 		return (scrdata->screens[0]);
    319 
    320 	for (i = 0; i < scrdata->nscreens; i++) {
    321 		scr = scrdata->screens[i];
    322 		if (!strcmp(name, scr->name))
    323 			return (scr);
    324 	}
    325 
    326 	return (0);
    327 }
    328 
    329 /*
    330  * print info about attached screen
    331  */
    332 static void
    333 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
    334 {
    335 	printf("%s: screen %d", sc->sc_dv.dv_xname, idx);
    336 	if (count > 1)
    337 		printf("-%d", idx + (count-1));
    338 	printf(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
    339 	if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
    340 		printf(", %s emulation",
    341 			sc->sc_scr[idx]->scr_dconf->wsemul->name);
    342 	}
    343 	printf(")\n");
    344 }
    345 
    346 int
    347 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
    348 	const char *screentype, const char *emul)
    349 {
    350 	const struct wsscreen_descr *scrdesc;
    351 	int error;
    352 	void *cookie;
    353 	int ccol, crow;
    354 	long defattr;
    355 	struct wsscreen *scr;
    356 	int s;
    357 
    358 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    359 		return (EINVAL);
    360 	if (sc->sc_scr[idx] != NULL)
    361 		return (EBUSY);
    362 
    363 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    364 	if (!scrdesc)
    365 		return (ENXIO);
    366 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    367 			scrdesc, &cookie, &ccol, &crow, &defattr);
    368 	if (error)
    369 		return (error);
    370 
    371 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    372 			      cookie, ccol, crow, defattr);
    373 	if (scr == NULL) {
    374 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    375 						 cookie);
    376 		return (ENXIO);
    377 	}
    378 
    379 	sc->sc_scr[idx] = scr;
    380 
    381 	/* if no screen has focus yet, activate the first we get */
    382 	s = spltty();
    383 	if (!sc->sc_focus) {
    384 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
    385 						 scr->scr_dconf->emulcookie,
    386 						 0, 0, 0);
    387 		sc->sc_focusidx = idx;
    388 		sc->sc_focus = scr;
    389 	}
    390 	splx(s);
    391 	return (0);
    392 }
    393 
    394 static void
    395 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
    396 {
    397 	int maj, mn, idx;
    398 
    399 	/* hangup */
    400 	if (WSSCREEN_HAS_TTY(scr)) {
    401 		struct tty *tp = scr->scr_tty;
    402 		(*tp->t_linesw->l_modem)(tp, 0);
    403 	}
    404 
    405 	/* locate the major number */
    406 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    407 	/* locate the screen index */
    408 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
    409 		if (scr == sc->sc_scr[idx])
    410 			break;
    411 #ifdef DIAGNOSTIC
    412 	if (idx == WSDISPLAY_MAXSCREEN)
    413 		panic("wsdisplay_forceclose: bad screen");
    414 #endif
    415 
    416 	/* nuke the vnodes */
    417 	mn = WSDISPLAYMINOR(device_unit(&sc->sc_dv), idx);
    418 	vdevgone(maj, mn, mn, VCHR);
    419 }
    420 
    421 #ifdef WSDISPLAY_SCROLLSUPPORT
    422 void
    423 wsdisplay_scroll(void *arg, int op)
    424 {
    425 	struct wsdisplay_softc *sc = arg;
    426 	int lines;
    427 
    428 	if (op == WSDISPLAY_SCROLL_RESET)
    429 		lines = 0;
    430 	else {
    431 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
    432 			sc->sc_scroll_values.slowlines :
    433 			sc->sc_scroll_values.fastlines;
    434 		if (op & WSDISPLAY_SCROLL_BACKWARD)
    435 			lines = -(lines);
    436 	}
    437 
    438 	if (sc->sc_accessops->scroll) {
    439 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
    440 		    sc->sc_focus->scr_dconf->emulcookie, lines);
    441 	}
    442 }
    443 #endif
    444 
    445 int
    446 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
    447 {
    448 	struct wsscreen *scr;
    449 	int s;
    450 	void *cookie;
    451 
    452 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    453 		return (EINVAL);
    454 	if ((scr = sc->sc_scr[idx]) == NULL)
    455 		return (ENXIO);
    456 
    457 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    458 	    scr->scr_syncops ||
    459 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    460 		return(EBUSY);
    461 
    462 	wsdisplay_closescreen(sc, scr);
    463 
    464 	/*
    465 	 * delete pointers, so neither device entries
    466 	 * nor keyboard input can reference it anymore
    467 	 */
    468 	s = spltty();
    469 	if (sc->sc_focus == scr) {
    470 		sc->sc_focus = 0;
    471 #ifdef WSDISPLAY_COMPAT_RAWKBD
    472 		wsdisplay_update_rawkbd(sc, 0);
    473 #endif
    474 	}
    475 	sc->sc_scr[idx] = 0;
    476 	splx(s);
    477 
    478 	/*
    479 	 * Wake up processes waiting for the screen to
    480 	 * be activated. Sleepers must check whether
    481 	 * the screen still exists.
    482 	 */
    483 	if (scr->scr_flags & SCR_WAITACTIVE)
    484 		wakeup(scr);
    485 
    486 	/* save a reference to the graphics screen */
    487 	cookie = scr->scr_dconf->emulcookie;
    488 
    489 	wsscreen_detach(scr);
    490 
    491 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    492 					 cookie);
    493 
    494 	printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
    495 	return (0);
    496 }
    497 
    498 /*
    499  * Autoconfiguration functions.
    500  */
    501 int
    502 wsdisplay_emul_match(struct device *parent, struct cfdata *match, void *aux)
    503 {
    504 	struct wsemuldisplaydev_attach_args *ap = aux;
    505 
    506 	if (match->wsemuldisplaydevcf_console !=
    507 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    508 		/*
    509 		 * If console-ness of device specified, either match
    510 		 * exactly (at high priority), or fail.
    511 		 */
    512 		if (match->wsemuldisplaydevcf_console != 0 &&
    513 		    ap->console != 0)
    514 			return (10);
    515 		else
    516 			return (0);
    517 	}
    518 
    519 	/* If console-ness unspecified, it wins. */
    520 	return (1);
    521 }
    522 
    523 void
    524 wsdisplay_emul_attach(struct device *parent, struct device *self, void *aux)
    525 {
    526 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    527 	struct wsemuldisplaydev_attach_args *ap = aux;
    528 
    529 	/* Don't allow more than one console to attach */
    530 	if (wsdisplay_console_attached && ap->console)
    531 		ap->console = 0;
    532 
    533 	wsdisplay_common_attach(sc, ap->console,
    534 	     device_cfdata(&sc->sc_dv)->wsemuldisplaydevcf_kbdmux, ap->scrdata,
    535 	     ap->accessops, ap->accesscookie);
    536 
    537 	if (ap->console) {
    538 		int maj;
    539 
    540 		/* locate the major number */
    541 		maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    542 
    543 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
    544 					 0));
    545 	}
    546 }
    547 
    548 /* Print function (for parent devices). */
    549 int
    550 wsemuldisplaydevprint(void *aux, const char *pnp)
    551 {
    552 #if 0 /* -Wunused */
    553 	struct wsemuldisplaydev_attach_args *ap = aux;
    554 #endif
    555 
    556 	if (pnp)
    557 		aprint_normal("wsdisplay at %s", pnp);
    558 #if 0 /* don't bother; it's ugly */
    559 	aprint_normal(" console %d", ap->console);
    560 #endif
    561 
    562 	return (UNCONF);
    563 }
    564 
    565 int
    566 wsdisplay_noemul_match(struct device *parent, struct cfdata *match, void *aux)
    567 {
    568 #if 0 /* -Wunused */
    569 	struct wsdisplaydev_attach_args *ap = aux;
    570 #endif
    571 
    572 	/* Always match. */
    573 	return (1);
    574 }
    575 
    576 void
    577 wsdisplay_noemul_attach(struct device *parent, struct device *self, void *aux)
    578 {
    579 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    580 	struct wsdisplaydev_attach_args *ap = aux;
    581 
    582 	wsdisplay_common_attach(sc, 0,
    583 	    device_cfdata(&sc->sc_dv)->wsemuldisplaydevcf_kbdmux, NULL,
    584 	    ap->accessops, ap->accesscookie);
    585 }
    586 
    587 /* Print function (for parent devices). */
    588 int
    589 wsdisplaydevprint(void *aux, const char *pnp)
    590 {
    591 #if 0 /* -Wunused */
    592 	struct wsdisplaydev_attach_args *ap = aux;
    593 #endif
    594 
    595 	if (pnp)
    596 		aprint_normal("wsdisplay at %s", pnp);
    597 
    598 	return (UNCONF);
    599 }
    600 
    601 static void
    602 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
    603 	const struct wsscreen_list *scrdata,
    604 	const struct wsdisplay_accessops *accessops,
    605 	void *accesscookie)
    606 {
    607 	static int hookset;
    608 	int i, start=0;
    609 #if NWSKBD > 0
    610 	struct wsevsrc *kme;
    611 #if NWSMUX > 0
    612 	struct wsmux_softc *mux;
    613 
    614 	if (kbdmux >= 0)
    615 		mux = wsmux_getmux(kbdmux);
    616 	else
    617 		mux = wsmux_create("dmux", device_unit(&sc->sc_dv));
    618 	/* XXX panic()ing isn't nice, but attach cannot fail */
    619 	if (mux == NULL)
    620 		panic("wsdisplay_common_attach: no memory");
    621 	sc->sc_input = &mux->sc_base;
    622 	mux->sc_base.me_dispdv = &sc->sc_dv;
    623 	printf(" kbdmux %d", kbdmux);
    624 #else
    625 	if (kbdmux >= 0)
    626 		printf(" (kbdmux ignored)");
    627 #endif
    628 #endif
    629 
    630 	sc->sc_isconsole = console;
    631 
    632 	if (console) {
    633 		KASSERT(wsdisplay_console_initted);
    634 		KASSERT(wsdisplay_console_device == NULL);
    635 
    636 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    637 		wsdisplay_console_device = sc;
    638 
    639 		printf(": console (%s, %s emulation)",
    640 		       wsdisplay_console_conf.scrdata->name,
    641 		       wsdisplay_console_conf.wsemul->name);
    642 
    643 #if NWSKBD > 0
    644 		kme = wskbd_set_console_display(&sc->sc_dv, sc->sc_input);
    645 		if (kme != NULL)
    646 			printf(", using %s", kme->me_dv.dv_xname);
    647 #if NWSMUX == 0
    648 		sc->sc_input = kme;
    649 #endif
    650 #endif
    651 
    652 		sc->sc_focusidx = 0;
    653 		sc->sc_focus = sc->sc_scr[0];
    654 		start = 1;
    655 
    656 		wsdisplay_console_attached = 1;
    657 	}
    658 	printf("\n");
    659 
    660 #if NWSKBD > 0 && NWSMUX > 0
    661 	wsmux_set_display(mux, &sc->sc_dv);
    662 #endif
    663 
    664 	sc->sc_accessops = accessops;
    665 	sc->sc_accesscookie = accesscookie;
    666 	sc->sc_scrdata = scrdata;
    667 
    668 #ifdef WSDISPLAY_SCROLLSUPPORT
    669 	sc->sc_scroll_values = wsdisplay_default_scroll_values;
    670 #endif
    671 
    672 	/*
    673 	 * Set up a number of virtual screens if wanted. The
    674 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    675 	 * is for special cases like installation kernels.
    676 	 */
    677 	for (i = start; i < wsdisplay_defaultscreens; i++) {
    678 		if (wsdisplay_addscreen(sc, i, 0, 0))
    679 			break;
    680 	}
    681 
    682 	if (i > start)
    683 		wsdisplay_addscreen_print(sc, start, i-start);
    684 
    685 	if (hookset == 0)
    686 		shutdownhook_establish(wsdisplay_shutdownhook, NULL);
    687 	hookset = 1;
    688 }
    689 
    690 void
    691 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
    692 	int ccol, int crow, long defattr)
    693 {
    694 	const struct wsemul_ops *wsemul;
    695 
    696 	KASSERT(!wsdisplay_console_initted);
    697 	KASSERT(type->nrows > 0);
    698 	KASSERT(type->ncols > 0);
    699 	KASSERT(crow < type->nrows);
    700 	KASSERT(ccol < type->ncols);
    701 
    702 	wsdisplay_console_conf.emulops = type->textops;
    703 	wsdisplay_console_conf.emulcookie = cookie;
    704 	wsdisplay_console_conf.scrdata = type;
    705 
    706 	wsemul = wsemul_pick(0); /* default */
    707 	wsdisplay_console_conf.wsemul = wsemul;
    708 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    709 								  ccol, crow,
    710 								  defattr);
    711 
    712 	cn_tab = &wsdisplay_cons;
    713 	wsdisplay_console_initted = 1;
    714 }
    715 
    716 /*
    717  * Tty and cdevsw functions.
    718  */
    719 int
    720 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
    721 {
    722 	struct wsdisplay_softc *sc;
    723 	struct tty *tp;
    724 	int newopen, error;
    725 	struct wsscreen *scr;
    726 
    727 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    728 	if (sc == NULL)			/* make sure it was attached */
    729 		return (ENXIO);
    730 
    731 	if (ISWSDISPLAYSTAT(dev)) {
    732 		wsevent_init(&sc->evar, l->l_proc);
    733 		return (0);
    734 	}
    735 
    736 	if (ISWSDISPLAYCTL(dev))
    737 		return (0);
    738 
    739 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    740 		return (ENXIO);
    741 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    742 		return (ENXIO);
    743 
    744 	if (WSSCREEN_HAS_TTY(scr)) {
    745 		tp = scr->scr_tty;
    746 		tp->t_oproc = wsdisplaystart;
    747 		tp->t_param = wsdisplayparam;
    748 		tp->t_dev = dev;
    749 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    750 
    751 		if (kauth_authorize_device_tty(l->l_cred,
    752 			KAUTH_DEVICE_TTY_OPEN, tp))
    753 			return (EBUSY);
    754 
    755 		if (newopen) {
    756 			ttychars(tp);
    757 			tp->t_iflag = TTYDEF_IFLAG;
    758 			tp->t_oflag = TTYDEF_OFLAG;
    759 			tp->t_cflag = TTYDEF_CFLAG;
    760 			tp->t_lflag = TTYDEF_LFLAG;
    761 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    762 			wsdisplayparam(tp, &tp->t_termios);
    763 			ttsetwater(tp);
    764 		}
    765 		tp->t_state |= TS_CARR_ON;
    766 
    767 		error = ((*tp->t_linesw->l_open)(dev, tp));
    768 		if (error)
    769 			return (error);
    770 
    771 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    772 			/* set window sizes as appropriate, and reset
    773 			 the emulation */
    774 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    775 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    776 
    777 			/* wsdisplay_set_emulation() */
    778 		}
    779 	}
    780 
    781 	scr->scr_flags |= SCR_OPEN;
    782 	return (0);
    783 }
    784 
    785 int
    786 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
    787 {
    788 	struct wsdisplay_softc *sc;
    789 	struct tty *tp;
    790 	struct wsscreen *scr;
    791 
    792 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    793 
    794 	if (ISWSDISPLAYSTAT(dev)) {
    795 		wsevent_fini(&sc->evar);
    796 		return (0);
    797 	}
    798 
    799 	if (ISWSDISPLAYCTL(dev))
    800 		return (0);
    801 
    802 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    803 		return (0);
    804 
    805 	if (WSSCREEN_HAS_TTY(scr)) {
    806 		if (scr->scr_hold_screen) {
    807 			int s;
    808 
    809 			/* XXX RESET KEYBOARD LEDS, etc. */
    810 			s = spltty();	/* avoid conflict with keyboard */
    811 			wsdisplay_kbdholdscreen((struct device *)sc, 0);
    812 			splx(s);
    813 		}
    814 		tp = scr->scr_tty;
    815 		(*tp->t_linesw->l_close)(tp, flag);
    816 		ttyclose(tp);
    817 	}
    818 
    819 	if (scr->scr_syncops)
    820 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    821 
    822 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    823 		scr->scr_flags &= ~SCR_GRAPHICS;
    824 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    825 						 WSEMUL_RESET);
    826 		if (wsdisplay_clearonclose)
    827 			(*scr->scr_dconf->wsemul->reset)
    828 				(scr->scr_dconf->wsemulcookie,
    829 				 WSEMUL_CLEARSCREEN);
    830 	}
    831 
    832 #ifdef WSDISPLAY_COMPAT_RAWKBD
    833 	if (scr->scr_rawkbd) {
    834 		int kbmode = WSKBD_TRANSLATED;
    835 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    836 					       (caddr_t)&kbmode, 0, l);
    837 	}
    838 #endif
    839 
    840 	scr->scr_flags &= ~SCR_OPEN;
    841 
    842 	return (0);
    843 }
    844 
    845 int
    846 wsdisplayread(dev_t dev, struct uio *uio, int flag)
    847 {
    848 	struct wsdisplay_softc *sc;
    849 	struct tty *tp;
    850 	struct wsscreen *scr;
    851 	int error;
    852 
    853 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    854 
    855 	if (ISWSDISPLAYSTAT(dev)) {
    856 		error = wsevent_read(&sc->evar, uio, flag);
    857 		return (error);
    858 	}
    859 
    860 	if (ISWSDISPLAYCTL(dev))
    861 		return (0);
    862 
    863 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    864 		return (ENXIO);
    865 
    866 	if (!WSSCREEN_HAS_TTY(scr))
    867 		return (ENODEV);
    868 
    869 	tp = scr->scr_tty;
    870 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    871 }
    872 
    873 int
    874 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
    875 {
    876 	struct wsdisplay_softc *sc;
    877 	struct tty *tp;
    878 	struct wsscreen *scr;
    879 
    880 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    881 
    882 	if (ISWSDISPLAYSTAT(dev)) {
    883 		return (0);
    884 	}
    885 
    886 	if (ISWSDISPLAYCTL(dev))
    887 		return (0);
    888 
    889 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    890 		return (ENXIO);
    891 
    892 	if (!WSSCREEN_HAS_TTY(scr))
    893 		return (ENODEV);
    894 
    895 	tp = scr->scr_tty;
    896 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    897 }
    898 
    899 int
    900 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
    901 {
    902 	struct wsdisplay_softc *sc;
    903 	struct tty *tp;
    904 	struct wsscreen *scr;
    905 
    906 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    907 
    908 	if (ISWSDISPLAYSTAT(dev))
    909 		return (wsevent_poll(&sc->evar, events, l));
    910 
    911 	if (ISWSDISPLAYCTL(dev))
    912 		return (0);
    913 
    914 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    915 		return (POLLHUP);
    916 
    917 	if (!WSSCREEN_HAS_TTY(scr))
    918 		return (POLLERR);
    919 
    920 	tp = scr->scr_tty;
    921 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    922 }
    923 
    924 int
    925 wsdisplaykqfilter(dev, kn)
    926 	dev_t dev;
    927 	struct knote *kn;
    928 {
    929 	struct wsdisplay_softc *sc =
    930 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    931 	struct wsscreen *scr;
    932 
    933 	if (ISWSDISPLAYCTL(dev))
    934 		return (1);
    935 
    936 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    937 		return (1);
    938 
    939 
    940 	if (WSSCREEN_HAS_TTY(scr))
    941 		return (ttykqfilter(dev, kn));
    942 	else
    943 		return (1);
    944 }
    945 
    946 struct tty *
    947 wsdisplaytty(dev_t dev)
    948 {
    949 	struct wsdisplay_softc *sc;
    950 	struct wsscreen *scr;
    951 
    952 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    953 
    954 	if (ISWSDISPLAYSTAT(dev))
    955 		panic("wsdisplaytty() on status device");
    956 
    957 	if (ISWSDISPLAYCTL(dev))
    958 		panic("wsdisplaytty() on ctl device");
    959 
    960 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    961 		return NULL;
    962 
    963 	return (scr->scr_tty);
    964 }
    965 
    966 int
    967 wsdisplayioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    968 {
    969 	struct wsdisplay_softc *sc;
    970 	struct tty *tp;
    971 	int error;
    972 	struct wsscreen *scr;
    973 
    974 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    975 
    976 #ifdef WSDISPLAY_COMPAT_USL
    977 	error = wsdisplay_usl_ioctl1(sc, cmd, data, flag, l);
    978 	if (error != EPASSTHROUGH)
    979 		return (error);
    980 #endif
    981 
    982 	if (ISWSDISPLAYSTAT(dev))
    983 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
    984 
    985 	if (ISWSDISPLAYCTL(dev))
    986 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
    987 
    988 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    989 		return (ENXIO);
    990 
    991 	if (WSSCREEN_HAS_TTY(scr)) {
    992 		tp = scr->scr_tty;
    993 
    994 /* printf("disc\n"); */
    995 		/* do the line discipline ioctls first */
    996 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    997 		if (error != EPASSTHROUGH)
    998 			return (error);
    999 
   1000 /* printf("tty\n"); */
   1001 		/* then the tty ioctls */
   1002 		error = ttioctl(tp, cmd, data, flag, l);
   1003 		if (error != EPASSTHROUGH)
   1004 			return (error);
   1005 	}
   1006 
   1007 #ifdef WSDISPLAY_COMPAT_USL
   1008 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
   1009 	if (error != EPASSTHROUGH)
   1010 		return (error);
   1011 #endif
   1012 
   1013 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
   1014 }
   1015 
   1016 int
   1017 wsdisplay_param(struct device *dev, u_long cmd, struct wsdisplay_param *dp)
   1018 {
   1019 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1020 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1021 					   sc->sc_focus->scr_dconf->emulcookie,
   1022 					   cmd, (caddr_t)dp, 0, NULL));
   1023 }
   1024 
   1025 int
   1026 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1027 	u_long cmd, caddr_t data, int flag, struct lwp *l)
   1028 {
   1029 	int error;
   1030 	char namebuf[16];
   1031 	struct wsdisplay_font fd;
   1032 #ifdef WSDISPLAY_SCROLLSUPPORT
   1033 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1034 #endif
   1035 
   1036 #if NWSKBD > 0
   1037 	struct wsevsrc *inp;
   1038 
   1039 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1040 	switch (cmd) {
   1041 	case WSKBDIO_SETMODE:
   1042 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1043 		return (wsdisplay_update_rawkbd(sc, scr));
   1044 	case WSKBDIO_GETMODE:
   1045 		*(int *)data = (scr->scr_rawkbd ?
   1046 				WSKBD_RAW : WSKBD_TRANSLATED);
   1047 		return (0);
   1048 	}
   1049 #endif
   1050 	inp = sc->sc_input;
   1051 	if (inp == NULL)
   1052 		return (ENXIO);
   1053 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1054 	if (error != EPASSTHROUGH)
   1055 		return (error);
   1056 #endif /* NWSKBD > 0 */
   1057 
   1058 	switch (cmd) {
   1059 	case WSDISPLAYIO_GMODE:
   1060 		if (scr->scr_flags & SCR_GRAPHICS) {
   1061 			if (scr->scr_flags & SCR_DUMBFB)
   1062 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1063 			else
   1064 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1065 		} else
   1066 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1067 		return (0);
   1068 
   1069 	case WSDISPLAYIO_SMODE:
   1070 #define d (*(int *)data)
   1071 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1072 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1073 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1074 			return (EINVAL);
   1075 
   1076 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1077 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1078 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1079 			d == WSDISPLAYIO_MODE_DUMBFB)
   1080 			    scr->scr_flags |= SCR_GRAPHICS |
   1081 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1082 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1083 		    return (EINVAL);
   1084 
   1085 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1086 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
   1087 
   1088 	    return (0);
   1089 #undef d
   1090 
   1091 #ifdef WSDISPLAY_SCROLLSUPPORT
   1092 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1093     do {								\
   1094 	(dstp)->fastlines = ((srcp)->which &				\
   1095 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1096 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1097 	(dstp)->slowlines = ((srcp)->which &				\
   1098 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1099 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1100 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1101     } while (0)
   1102 
   1103 
   1104 	case WSDISPLAYIO_DSSCROLL:
   1105 		usdp = (struct wsdisplay_scroll_data *)data;
   1106 		ksdp = &sc->sc_scroll_values;
   1107 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1108 		return (0);
   1109 
   1110 	case WSDISPLAYIO_DGSCROLL:
   1111 		usdp = (struct wsdisplay_scroll_data *)data;
   1112 		ksdp = &sc->sc_scroll_values;
   1113 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1114 		return (0);
   1115 #else
   1116 	case WSDISPLAYIO_DSSCROLL:
   1117 	case WSDISPLAYIO_DGSCROLL:
   1118 		return ENODEV;
   1119 #endif
   1120 
   1121 	case WSDISPLAYIO_SFONT:
   1122 #define d ((struct wsdisplay_usefontdata *)data)
   1123 		if (!sc->sc_accessops->load_font)
   1124 			return (EINVAL);
   1125 		if (d->name) {
   1126 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1127 			if (error)
   1128 				return (error);
   1129 			fd.name = namebuf;
   1130 		} else
   1131 			fd.name = 0;
   1132 		fd.data = 0;
   1133 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1134 					scr->scr_dconf->emulcookie, &fd);
   1135 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1136 			(*scr->scr_dconf->wsemul->reset)
   1137 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1138 		return (error);
   1139 #undef d
   1140 
   1141 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1142 	case WSDISPLAYIO_GMSGATTRS:
   1143 #define d ((struct wsdisplay_msgattrs *)data)
   1144 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1145 		    (scr->scr_dconf->wsemulcookie, d);
   1146 		return (0);
   1147 #undef d
   1148 
   1149 	case WSDISPLAYIO_SMSGATTRS: {
   1150 #define d ((struct wsdisplay_msgattrs *)data)
   1151 		int i;
   1152 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1153 			if (sc->sc_scr[i] != NULL)
   1154 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1155 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1156 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1157 				     d);
   1158 		}
   1159 		return (0);
   1160 #undef d
   1161 #else
   1162 	case WSDISPLAYIO_GMSGATTRS:
   1163 	case WSDISPLAYIO_SMSGATTRS:
   1164 		return (ENODEV);
   1165 #endif
   1166 	}
   1167 
   1168 	/* check ioctls for display */
   1169 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1170 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
   1171 }
   1172 
   1173 int
   1174 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1175 	int flag, struct lwp *l)
   1176 {
   1177 	switch (cmd) {
   1178 	case WSDISPLAYIO_GETACTIVESCREEN:
   1179 		*(int*)data = wsdisplay_getactivescreen(sc);
   1180 		return (0);
   1181 	}
   1182 
   1183 	return (EPASSTHROUGH);
   1184 }
   1185 
   1186 int
   1187 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1188 	int flag, struct lwp *l)
   1189 {
   1190 	int error;
   1191 	char *type, typebuf[16], *emul, emulbuf[16];
   1192 	void *tbuf;
   1193 	u_int fontsz;
   1194 #if defined(COMPAT_14) && NWSKBD > 0
   1195 	struct wsmux_device wsmuxdata;
   1196 #endif
   1197 #if NWSKBD > 0
   1198 	struct wsevsrc *inp;
   1199 #endif
   1200 
   1201 	switch (cmd) {
   1202 	case WSDISPLAYIO_ADDSCREEN:
   1203 #define d ((struct wsdisplay_addscreendata *)data)
   1204 		if (d->screentype) {
   1205 			error = copyinstr(d->screentype, typebuf,
   1206 					  sizeof(typebuf), 0);
   1207 			if (error)
   1208 				return (error);
   1209 			type = typebuf;
   1210 		} else
   1211 			type = 0;
   1212 		if (d->emul) {
   1213 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1214 			if (error)
   1215 				return (error);
   1216 			emul = emulbuf;
   1217 		} else
   1218 			emul = 0;
   1219 
   1220 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1221 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1222 		return (error);
   1223 #undef d
   1224 	case WSDISPLAYIO_DELSCREEN:
   1225 #define d ((struct wsdisplay_delscreendata *)data)
   1226 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1227 #undef d
   1228 	case WSDISPLAYIO_LDFONT:
   1229 #define d ((struct wsdisplay_font *)data)
   1230 		if (!sc->sc_accessops->load_font)
   1231 			return (EINVAL);
   1232 		if (d->name) {
   1233 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1234 			if (error)
   1235 				return (error);
   1236 			d->name = typebuf;
   1237 		} else
   1238 			d->name = "loaded"; /* ??? */
   1239 		fontsz = d->fontheight * d->stride * d->numchars;
   1240 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1241 			return (EINVAL);
   1242 
   1243 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1244 		error = copyin(d->data, tbuf, fontsz);
   1245 		if (error) {
   1246 			free(tbuf, M_DEVBUF);
   1247 			return (error);
   1248 		}
   1249 		d->data = tbuf;
   1250 		error =
   1251 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1252 		free(tbuf, M_DEVBUF);
   1253 #undef d
   1254 		return (error);
   1255 
   1256 #if NWSKBD > 0
   1257 #ifdef COMPAT_14
   1258 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1259 #define d ((struct wsdisplay_kbddata *)data)
   1260 		inp = sc->sc_input;
   1261 		if (inp == NULL)
   1262 			return (ENXIO);
   1263 		switch (d->op) {
   1264 		case _O_WSDISPLAY_KBD_ADD:
   1265 			if (d->idx == -1) {
   1266 				d->idx = wskbd_pickfree();
   1267 				if (d->idx == -1)
   1268 					return (ENXIO);
   1269 			}
   1270 			wsmuxdata.type = WSMUX_KBD;
   1271 			wsmuxdata.idx = d->idx;
   1272 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1273 					      &wsmuxdata, flag, l));
   1274 		case _O_WSDISPLAY_KBD_DEL:
   1275 			wsmuxdata.type = WSMUX_KBD;
   1276 			wsmuxdata.idx = d->idx;
   1277 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1278 					      &wsmuxdata, flag, l));
   1279 		default:
   1280 			return (EINVAL);
   1281 		}
   1282 #undef d
   1283 #endif
   1284 
   1285 	case WSMUXIO_ADD_DEVICE:
   1286 #define d ((struct wsmux_device *)data)
   1287 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1288 			d->idx = wskbd_pickfree();
   1289 #undef d
   1290 		/* fall into */
   1291 	case WSMUXIO_INJECTEVENT:
   1292 	case WSMUXIO_REMOVE_DEVICE:
   1293 	case WSMUXIO_LIST_DEVICES:
   1294 		inp = sc->sc_input;
   1295 		if (inp == NULL)
   1296 			return (ENXIO);
   1297 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1298 #endif /* NWSKBD > 0 */
   1299 
   1300 	}
   1301 	return (EPASSTHROUGH);
   1302 }
   1303 
   1304 int
   1305 wsdisplay_stat_inject(struct device *dev, u_int type, int value)
   1306 {
   1307 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *) dev;
   1308 	struct wseventvar *evar;
   1309 	struct wscons_event event;
   1310 
   1311 	evar = &sc->evar;
   1312 
   1313 	if (evar == NULL)
   1314 		return (0);
   1315 
   1316 	if (evar->q == NULL)
   1317 		return (1);
   1318 
   1319 	event.type = type;
   1320 	event.value = value;
   1321 	if (wsevent_inject(evar, &event, 1) != 0) {
   1322 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1323 		return (1);
   1324 	}
   1325 
   1326 	return (0);
   1327 }
   1328 
   1329 paddr_t
   1330 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1331 {
   1332 	struct wsdisplay_softc *sc =
   1333 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1334 	struct wsscreen *scr;
   1335 
   1336 	if (ISWSDISPLAYSTAT(dev))
   1337 		return (-1);
   1338 
   1339 	if (ISWSDISPLAYCTL(dev))
   1340 		return (-1);
   1341 
   1342 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1343 		return (-1);
   1344 
   1345 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1346 		return (-1);
   1347 
   1348 	/* pass mmap to display */
   1349 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
   1350 	    scr->scr_dconf->emulcookie, offset, prot));
   1351 }
   1352 
   1353 void
   1354 wsdisplaystart(struct tty *tp)
   1355 {
   1356 	struct wsdisplay_softc *sc;
   1357 	struct wsscreen *scr;
   1358 	int s, n;
   1359 	u_char *tbuf;
   1360 
   1361 	s = spltty();
   1362 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1363 		splx(s);
   1364 		return;
   1365 	}
   1366 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1367 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
   1368 		splx(s);
   1369 		return;
   1370 	}
   1371 
   1372 	if (scr->scr_hold_screen) {
   1373 		tp->t_state |= TS_TIMEOUT;
   1374 		splx(s);
   1375 		return;
   1376 	}
   1377 	tp->t_state |= TS_BUSY;
   1378 	splx(s);
   1379 
   1380 	/*
   1381 	 * Drain output from ring buffer.
   1382 	 * The output will normally be in one contiguous chunk, but when the
   1383 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1384 	 * the other at the start.  For performance, rather than loop here,
   1385 	 * we output one chunk, see if there's another one, and if so, output
   1386 	 * it too.
   1387 	 */
   1388 
   1389 	n = ndqb(&tp->t_outq, 0);
   1390 	tbuf = tp->t_outq.c_cf;
   1391 
   1392 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1393 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1394 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1395 						  tbuf, n, 0);
   1396 	}
   1397 	ndflush(&tp->t_outq, n);
   1398 
   1399 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1400 		tbuf = tp->t_outq.c_cf;
   1401 
   1402 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1403 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1404 			(*scr->scr_dconf->wsemul->output)
   1405 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
   1406 		}
   1407 		ndflush(&tp->t_outq, n);
   1408 	}
   1409 
   1410 	s = spltty();
   1411 	tp->t_state &= ~TS_BUSY;
   1412 	/* Come back if there's more to do */
   1413 	if (tp->t_outq.c_cc) {
   1414 		tp->t_state |= TS_TIMEOUT;
   1415 		callout_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
   1416 		    ttrstrt, tp);
   1417 	}
   1418 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1419 		if (tp->t_state&TS_ASLEEP) {
   1420 			tp->t_state &= ~TS_ASLEEP;
   1421 			wakeup(&tp->t_outq);
   1422 		}
   1423 		selwakeup(&tp->t_wsel);
   1424 	}
   1425 	splx(s);
   1426 }
   1427 
   1428 void
   1429 wsdisplaystop(struct tty *tp, int flag)
   1430 {
   1431 	int s;
   1432 
   1433 	s = spltty();
   1434 	if (ISSET(tp->t_state, TS_BUSY))
   1435 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1436 			SET(tp->t_state, TS_FLUSH);
   1437 	splx(s);
   1438 }
   1439 
   1440 /* Set line parameters. */
   1441 int
   1442 wsdisplayparam(struct tty *tp, struct termios *t)
   1443 {
   1444 
   1445 	tp->t_ispeed = t->c_ispeed;
   1446 	tp->t_ospeed = t->c_ospeed;
   1447 	tp->t_cflag = t->c_cflag;
   1448 	return 0;
   1449 }
   1450 
   1451 /*
   1452  * Callbacks for the emulation code.
   1453  */
   1454 void
   1455 wsdisplay_emulbell(void *v)
   1456 {
   1457 	struct wsscreen *scr = v;
   1458 
   1459 	if (scr == NULL)		/* console, before real attach */
   1460 		return;
   1461 
   1462 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1463 		return;
   1464 
   1465 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1466 					FWRITE, NULL);
   1467 }
   1468 
   1469 void
   1470 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1471 {
   1472 	struct wsscreen *scr = v;
   1473 	struct tty *tp;
   1474 
   1475 	if (v == NULL)			/* console, before real attach */
   1476 		return;
   1477 
   1478 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1479 		return;
   1480 	if (!WSSCREEN_HAS_TTY(scr))
   1481 		return;
   1482 
   1483 	tp = scr->scr_tty;
   1484 	while (count-- > 0)
   1485 		(*tp->t_linesw->l_rint)(*data++, tp);
   1486 }
   1487 
   1488 /*
   1489  * Calls from the keyboard interface.
   1490  */
   1491 void
   1492 wsdisplay_kbdinput(struct device *dev, keysym_t ks)
   1493 {
   1494 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1495 	struct wsscreen *scr;
   1496 	const char *dp;
   1497 	int count;
   1498 	struct tty *tp;
   1499 
   1500 	KASSERT(sc != NULL);
   1501 
   1502 	scr = sc->sc_focus;
   1503 
   1504 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1505 		return;
   1506 
   1507 	tp = scr->scr_tty;
   1508 
   1509 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1510 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
   1511 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1512 		count = (*scr->scr_dconf->wsemul->translate)
   1513 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1514 		while (count-- > 0)
   1515 			(*tp->t_linesw->l_rint)(*dp++, tp);
   1516 	}
   1517 }
   1518 
   1519 #if defined(WSDISPLAY_COMPAT_RAWKBD)
   1520 int
   1521 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
   1522 {
   1523 #if NWSKBD > 0
   1524 	int s, raw, data, error;
   1525 	struct wsevsrc *inp;
   1526 
   1527 	s = spltty();
   1528 
   1529 	raw = (scr ? scr->scr_rawkbd : 0);
   1530 
   1531 	if (scr != sc->sc_focus ||
   1532 	    sc->sc_rawkbd == raw) {
   1533 		splx(s);
   1534 		return (0);
   1535 	}
   1536 
   1537 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1538 	inp = sc->sc_input;
   1539 	if (inp == NULL) {
   1540 		splx(s);
   1541 		return (ENXIO);
   1542 	}
   1543 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1544 	if (!error)
   1545 		sc->sc_rawkbd = raw;
   1546 	splx(s);
   1547 	return (error);
   1548 #else
   1549 	return (0);
   1550 #endif
   1551 }
   1552 #endif
   1553 
   1554 int
   1555 wsdisplay_switch3(void *arg, int error, int waitok)
   1556 {
   1557 	struct wsdisplay_softc *sc = arg;
   1558 	int no;
   1559 	struct wsscreen *scr;
   1560 
   1561 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1562 		printf("wsdisplay_switch3: not switching\n");
   1563 		return (EINVAL);
   1564 	}
   1565 
   1566 	no = sc->sc_screenwanted;
   1567 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1568 		panic("wsdisplay_switch3: invalid screen %d", no);
   1569 	scr = sc->sc_scr[no];
   1570 	if (!scr) {
   1571 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1572 		error = ENXIO;
   1573 	}
   1574 
   1575 	if (error) {
   1576 		/* try to recover, avoid recursion */
   1577 
   1578 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1579 			printf("wsdisplay_switch3: giving up\n");
   1580 			sc->sc_focus = 0;
   1581 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1582 			wsdisplay_update_rawkbd(sc, 0);
   1583 #endif
   1584 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1585 			return (error);
   1586 		}
   1587 
   1588 		sc->sc_screenwanted = sc->sc_oldscreen;
   1589 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1590 		return (wsdisplay_switch1(arg, 0, waitok));
   1591 	}
   1592 
   1593 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1594 
   1595 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1596 		wakeup(scr);
   1597 	return (error);
   1598 }
   1599 
   1600 int
   1601 wsdisplay_switch2(void *arg, int error, int waitok)
   1602 {
   1603 	struct wsdisplay_softc *sc = arg;
   1604 	int no;
   1605 	struct wsscreen *scr;
   1606 
   1607 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1608 		printf("wsdisplay_switch2: not switching\n");
   1609 		return (EINVAL);
   1610 	}
   1611 
   1612 	no = sc->sc_screenwanted;
   1613 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1614 		panic("wsdisplay_switch2: invalid screen %d", no);
   1615 	scr = sc->sc_scr[no];
   1616 	if (!scr) {
   1617 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
   1618 		error = ENXIO;
   1619 	}
   1620 
   1621 	if (error) {
   1622 		/* try to recover, avoid recursion */
   1623 
   1624 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1625 			printf("wsdisplay_switch2: giving up\n");
   1626 			sc->sc_focus = 0;
   1627 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1628 			return (error);
   1629 		}
   1630 
   1631 		sc->sc_screenwanted = sc->sc_oldscreen;
   1632 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1633 		return (wsdisplay_switch1(arg, 0, waitok));
   1634 	}
   1635 
   1636 	sc->sc_focusidx = no;
   1637 	sc->sc_focus = scr;
   1638 
   1639 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1640 	(void) wsdisplay_update_rawkbd(sc, scr);
   1641 #endif
   1642 	/* keyboard map??? */
   1643 
   1644 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
   1645 	if (scr->scr_syncops) {
   1646 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1647 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
   1648 		if (error == EAGAIN) {
   1649 			/* switch will be done asynchronously */
   1650 			return (0);
   1651 		}
   1652 	}
   1653 
   1654 	return (wsdisplay_switch3(sc, error, waitok));
   1655 }
   1656 
   1657 int
   1658 wsdisplay_switch1(void *arg, int error, int waitok)
   1659 {
   1660 	struct wsdisplay_softc *sc = arg;
   1661 	int no;
   1662 	struct wsscreen *scr;
   1663 
   1664 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1665 		printf("wsdisplay_switch1: not switching\n");
   1666 		return (EINVAL);
   1667 	}
   1668 
   1669 	no = sc->sc_screenwanted;
   1670 	if (no == WSDISPLAY_NULLSCREEN) {
   1671 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1672 		if (!error) {
   1673 			sc->sc_focus = 0;
   1674 		}
   1675 		wakeup(sc);
   1676 		return (error);
   1677 	}
   1678 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1679 		panic("wsdisplay_switch1: invalid screen %d", no);
   1680 	scr = sc->sc_scr[no];
   1681 	if (!scr) {
   1682 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1683 		error = ENXIO;
   1684 	}
   1685 
   1686 	if (error) {
   1687 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1688 		return (error);
   1689 	}
   1690 
   1691 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
   1692 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1693 						 scr->scr_dconf->emulcookie,
   1694 						 waitok,
   1695 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
   1696 	if (error == EAGAIN) {
   1697 		/* switch will be done asynchronously */
   1698 		return (0);
   1699 	}
   1700 
   1701 	return (wsdisplay_switch2(sc, error, waitok));
   1702 }
   1703 
   1704 int
   1705 wsdisplay_switch(struct device *dev, int no, int waitok)
   1706 {
   1707 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1708 	int s, res = 0;
   1709 	struct wsscreen *scr;
   1710 
   1711 	if (no != WSDISPLAY_NULLSCREEN) {
   1712 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
   1713 			return (EINVAL);
   1714 		if (sc->sc_scr[no] == NULL)
   1715 			return (ENXIO);
   1716 	}
   1717 
   1718 	wsdisplay_stat_inject(dev, WSCONS_EVENT_SCREEN_SWITCH, no);
   1719 
   1720 	s = spltty();
   1721 
   1722 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   1723 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   1724 		splx(s);
   1725 		return (0);
   1726 	}
   1727 
   1728 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1729 		splx(s);
   1730 		return (EBUSY);
   1731 	}
   1732 
   1733 	sc->sc_flags |= SC_SWITCHPENDING;
   1734 	sc->sc_screenwanted = no;
   1735 
   1736 	splx(s);
   1737 
   1738 	scr = sc->sc_focus;
   1739 	if (!scr) {
   1740 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1741 		return (wsdisplay_switch1(sc, 0, waitok));
   1742 	} else
   1743 		sc->sc_oldscreen = sc->sc_focusidx;
   1744 
   1745 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
   1746 	if (scr->scr_syncops) {
   1747 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1748 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
   1749 		if (res == EAGAIN) {
   1750 			/* switch will be done asynchronously */
   1751 			return (0);
   1752 		}
   1753 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1754 		/* no way to save state */
   1755 		res = EBUSY;
   1756 	}
   1757 
   1758 	return (wsdisplay_switch1(sc, res, waitok));
   1759 }
   1760 
   1761 void
   1762 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
   1763 {
   1764 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1765 	struct wsscreen *scr;
   1766 
   1767 	KASSERT(sc != NULL);
   1768 	scr = sc->sc_focus;
   1769 
   1770 	if (!scr)
   1771 		return;
   1772 
   1773 	switch (op) {
   1774 	case WSDISPLAY_RESETEMUL:
   1775 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1776 			break;
   1777 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1778 						 WSEMUL_RESET);
   1779 		break;
   1780 	case WSDISPLAY_RESETCLOSE:
   1781 		wsdisplay_closescreen(sc, scr);
   1782 		break;
   1783 	}
   1784 }
   1785 
   1786 /*
   1787  * Interface for (external) VT switch / process synchronization code
   1788  */
   1789 int
   1790 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   1791 	void *cookie)
   1792 {
   1793 	if (scr->scr_syncops) {
   1794 		/*
   1795 		 * The screen is already claimed.
   1796 		 * Check if the owner is still alive.
   1797 		 */
   1798 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1799 			return (EBUSY);
   1800 	}
   1801 	scr->scr_syncops = ops;
   1802 	scr->scr_synccookie = cookie;
   1803 	return (0);
   1804 }
   1805 
   1806 int
   1807 wsscreen_detach_sync(struct wsscreen *scr)
   1808 {
   1809 	if (!scr->scr_syncops)
   1810 		return (EINVAL);
   1811 	scr->scr_syncops = 0;
   1812 	return (0);
   1813 }
   1814 
   1815 int
   1816 wsscreen_lookup_sync(struct wsscreen *scr,
   1817 	const struct wscons_syncops *ops, /* used as ID */
   1818 	void **cookiep)
   1819 {
   1820 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1821 		return (EINVAL);
   1822 	*cookiep = scr->scr_synccookie;
   1823 	return (0);
   1824 }
   1825 
   1826 /*
   1827  * Interface to virtual screen stuff
   1828  */
   1829 int
   1830 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   1831 {
   1832 	return (WSDISPLAY_MAXSCREEN - 1);
   1833 }
   1834 
   1835 int
   1836 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   1837 {
   1838 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   1839 		return (EINVAL);
   1840 	if (!sc->sc_scr[idx])
   1841 		return (ENXIO);
   1842 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1843 }
   1844 
   1845 int
   1846 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   1847 {
   1848 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   1849 }
   1850 
   1851 int
   1852 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   1853 {
   1854 	struct wsscreen *scr;
   1855 	int s, res = 0;
   1856 
   1857 	if (no == WSDISPLAY_NULLSCREEN) {
   1858 		s = spltty();
   1859 		while (sc->sc_focus && res == 0) {
   1860 			res = tsleep(sc, PCATCH, "wswait", 0);
   1861 		}
   1862 		splx(s);
   1863 		return (res);
   1864 	}
   1865 
   1866 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1867 		return (ENXIO);
   1868 	scr = sc->sc_scr[no];
   1869 	if (!scr)
   1870 		return (ENXIO);
   1871 
   1872 	s = spltty();
   1873 	if (scr != sc->sc_focus) {
   1874 		scr->scr_flags |= SCR_WAITACTIVE;
   1875 		res = tsleep(scr, PCATCH, "wswait", 0);
   1876 		if (scr != sc->sc_scr[no])
   1877 			res = ENXIO; /* disappeared in the meantime */
   1878 		else
   1879 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1880 	}
   1881 	splx(s);
   1882 	return (res);
   1883 }
   1884 
   1885 void
   1886 wsdisplay_kbdholdscreen(struct device *dev, int hold)
   1887 {
   1888 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1889 	struct wsscreen *scr;
   1890 
   1891 	scr = sc->sc_focus;
   1892 
   1893 	if (hold)
   1894 		scr->scr_hold_screen = 1;
   1895 	else {
   1896 		scr->scr_hold_screen = 0;
   1897 		callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
   1898 		    ttrstrt, scr->scr_tty);		/* "immediate" */
   1899 	}
   1900 }
   1901 
   1902 #if NWSKBD > 0
   1903 void
   1904 wsdisplay_set_console_kbd(struct wsevsrc *src)
   1905 {
   1906 	if (wsdisplay_console_device == NULL) {
   1907 		src->me_dispdv = NULL;
   1908 		return;
   1909 	}
   1910 #if NWSMUX > 0
   1911 	if (wsmux_attach_sc((struct wsmux_softc *)
   1912 			    wsdisplay_console_device->sc_input, src)) {
   1913 		src->me_dispdv = NULL;
   1914 		return;
   1915 	}
   1916 #else
   1917 	wsdisplay_console_device->sc_input = src;
   1918 #endif
   1919 	src->me_dispdv = &wsdisplay_console_device->sc_dv;
   1920 }
   1921 #endif /* NWSKBD > 0 */
   1922 
   1923 /*
   1924  * Console interface.
   1925  */
   1926 void
   1927 wsdisplay_cnputc(dev_t dev, int i)
   1928 {
   1929 	struct wsscreen_internal *dc;
   1930 	u_char c = i;
   1931 
   1932 	if (!wsdisplay_console_initted)
   1933 		return;
   1934 
   1935 	if ((wsdisplay_console_device != NULL) &&
   1936 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   1937 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1938 		return;
   1939 
   1940 	dc = &wsdisplay_console_conf;
   1941 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1942 }
   1943 
   1944 static int
   1945 wsdisplay_getc_dummy(dev_t dev)
   1946 {
   1947 	/* panic? */
   1948 	return (0);
   1949 }
   1950 
   1951 static void
   1952 wsdisplay_pollc(dev_t dev, int on)
   1953 {
   1954 
   1955 	wsdisplay_cons_pollmode = on;
   1956 
   1957 	/* notify to fb drivers */
   1958 	if (wsdisplay_console_device != NULL &&
   1959 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   1960 		(*wsdisplay_console_device->sc_accessops->pollc)
   1961 			(wsdisplay_console_device->sc_accesscookie, on);
   1962 
   1963 	/* notify to kbd drivers */
   1964 	if (wsdisplay_cons_kbd_pollc)
   1965 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   1966 }
   1967 
   1968 void
   1969 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   1970 	void (*bell)(dev_t, u_int, u_int, u_int))
   1971 {
   1972 	wsdisplay_cons.cn_getc = get;
   1973 	wsdisplay_cons.cn_bell = bell;
   1974 	wsdisplay_cons_kbd_pollc = poll;
   1975 }
   1976 
   1977 void
   1978 wsdisplay_unset_cons_kbd(void)
   1979 {
   1980 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   1981 	wsdisplay_cons.cn_bell = NULL;
   1982 	wsdisplay_cons_kbd_pollc = 0;
   1983 }
   1984 
   1985 /*
   1986  * Switch the console display to it's first screen.
   1987  */
   1988 void
   1989 wsdisplay_switchtoconsole(void)
   1990 {
   1991 	struct wsdisplay_softc *sc;
   1992 	struct wsscreen *scr;
   1993 
   1994 	if (wsdisplay_console_device != NULL) {
   1995 		sc = wsdisplay_console_device;
   1996 		if ((scr = sc->sc_scr[0]) == NULL)
   1997 			return;
   1998 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1999 						 scr->scr_dconf->emulcookie,
   2000 						 0, NULL, NULL);
   2001 	}
   2002 }
   2003 
   2004 /*
   2005  * Switch the console at shutdown.
   2006  */
   2007 static void
   2008 wsdisplay_shutdownhook(void *arg)
   2009 {
   2010 
   2011 	wsdisplay_switchtoconsole();
   2012 }
   2013