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