Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.51.2.4
      1 /* $NetBSD: wsdisplay.c,v 1.51.2.4 2002/03/16 16:01:44 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.4 2002/03/16 16:01:44 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 >= 0)
    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 >= 0)
    906 			return (error);
    907 
    908 /* printf("tty\n"); */
    909 		/* then the tty ioctls */
    910 		error = ttioctl(tp, cmd, data, flag, p);
    911 		if (error >= 0)
    912 			return (error);
    913 	}
    914 
    915 #ifdef WSDISPLAY_COMPAT_USL
    916 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, p);
    917 	if (error >= 0)
    918 		return (error);
    919 #endif
    920 
    921 	error = wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p);
    922 	return (error != -1 ? error : ENOTTY);
    923 }
    924 
    925 int
    926 wsdisplay_param(struct device *dev, u_long cmd, struct wsdisplay_param *dp)
    927 {
    928 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
    929 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
    930 					   (caddr_t)dp, 0, NULL));
    931 }
    932 
    933 int
    934 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
    935 	u_long cmd, caddr_t data, int flag, struct proc *p)
    936 {
    937 	int error;
    938 	char namebuf[16];
    939 	struct wsdisplay_font fd;
    940 
    941 #if NWSKBD > 0
    942 	struct wsevsrc *inp;
    943 
    944 #ifdef WSDISPLAY_COMPAT_RAWKBD
    945 	switch (cmd) {
    946 	case WSKBDIO_SETMODE:
    947 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
    948 		return (wsdisplay_update_rawkbd(sc, scr));
    949 	case WSKBDIO_GETMODE:
    950 		*(int *)data = (scr->scr_rawkbd ?
    951 				WSKBD_RAW : WSKBD_TRANSLATED);
    952 		return (0);
    953 	}
    954 #endif
    955 	inp = sc->sc_input;
    956 	if (inp == NULL)
    957 		return (ENXIO);
    958 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, p);
    959 	if (error >= 0)
    960 		return (error);
    961 #endif /* NWSKBD > 0 */
    962 
    963 	switch (cmd) {
    964 	case WSDISPLAYIO_GMODE:
    965 		*(u_int *)data = (scr->scr_flags & SCR_GRAPHICS ?
    966 				  WSDISPLAYIO_MODE_MAPPED :
    967 				  WSDISPLAYIO_MODE_EMUL);
    968 		return (0);
    969 
    970 	case WSDISPLAYIO_SMODE:
    971 #define d (*(int *)data)
    972 		if (d != WSDISPLAYIO_MODE_EMUL &&
    973 		    d != WSDISPLAYIO_MODE_MAPPED)
    974 			return (EINVAL);
    975 
    976 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
    977 		    scr->scr_flags &= ~SCR_GRAPHICS;
    978 		    if (d == WSDISPLAYIO_MODE_MAPPED)
    979 			    scr->scr_flags |= SCR_GRAPHICS;
    980 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
    981 		    return (EINVAL);
    982 
    983 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
    984 		    flag, p);
    985 
    986 	    return (0);
    987 #undef d
    988 
    989 	case WSDISPLAYIO_USEFONT:
    990 #define d ((struct wsdisplay_usefontdata *)data)
    991 		if (!sc->sc_accessops->load_font)
    992 			return (EINVAL);
    993 		if (d->name) {
    994 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
    995 			if (error)
    996 				return (error);
    997 			fd.name = namebuf;
    998 		} else
    999 			fd.name = 0;
   1000 		fd.data = 0;
   1001 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1002 					scr->scr_dconf->emulcookie, &fd);
   1003 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1004 			(*scr->scr_dconf->wsemul->reset)
   1005 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1006 		return (error);
   1007 #undef d
   1008 	}
   1009 
   1010 	/* check ioctls for display */
   1011 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1012 	    flag, p));
   1013 }
   1014 
   1015 int
   1016 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1017 	int flag, struct proc *p)
   1018 {
   1019 	int error;
   1020 	char *type, typebuf[16], *emul, emulbuf[16];
   1021 	void *buf;
   1022 	u_int fontsz;
   1023 #if defined(COMPAT_14) && NWSKBD > 0
   1024 	struct wsmux_device wsmuxdata;
   1025 #endif
   1026 #if NWSKBD > 0
   1027 	struct wsevsrc *inp;
   1028 #endif
   1029 
   1030 	switch (cmd) {
   1031 	case WSDISPLAYIO_ADDSCREEN:
   1032 #define d ((struct wsdisplay_addscreendata *)data)
   1033 		if (d->screentype) {
   1034 			error = copyinstr(d->screentype, typebuf,
   1035 					  sizeof(typebuf), 0);
   1036 			if (error)
   1037 				return (error);
   1038 			type = typebuf;
   1039 		} else
   1040 			type = 0;
   1041 		if (d->emul) {
   1042 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1043 			if (error)
   1044 				return (error);
   1045 			emul = emulbuf;
   1046 		} else
   1047 			emul = 0;
   1048 
   1049 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1050 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1051 		return (error);
   1052 #undef d
   1053 	case WSDISPLAYIO_DELSCREEN:
   1054 #define d ((struct wsdisplay_delscreendata *)data)
   1055 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1056 #undef d
   1057 	case WSDISPLAYIO_LDFONT:
   1058 #define d ((struct wsdisplay_font *)data)
   1059 		if (!sc->sc_accessops->load_font)
   1060 			return (EINVAL);
   1061 		if (d->name) {
   1062 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1063 			if (error)
   1064 				return (error);
   1065 			d->name = typebuf;
   1066 		} else
   1067 			d->name = "loaded"; /* ??? */
   1068 		fontsz = d->fontheight * d->stride * d->numchars;
   1069 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1070 			return (EINVAL);
   1071 
   1072 		buf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1073 		error = copyin(d->data, buf, fontsz);
   1074 		if (error) {
   1075 			free(buf, M_DEVBUF);
   1076 			return (error);
   1077 		}
   1078 		d->data = buf;
   1079 		error =
   1080 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1081 		free(buf, M_DEVBUF);
   1082 #undef d
   1083 		return (error);
   1084 
   1085 #if NWSKBD > 0
   1086 #ifdef COMPAT_14
   1087 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1088 #define d ((struct wsdisplay_kbddata *)data)
   1089 		inp = sc->sc_input;
   1090 		if (inp == NULL)
   1091 			return (ENXIO);
   1092 		switch (d->op) {
   1093 		case _O_WSDISPLAY_KBD_ADD:
   1094 			if (d->idx == -1) {
   1095 				d->idx = wskbd_pickfree();
   1096 				if (d->idx == -1)
   1097 					return (ENXIO);
   1098 			}
   1099 			wsmuxdata.type = WSMUX_KBD;
   1100 			wsmuxdata.idx = d->idx;
   1101 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1102 					      &wsmuxdata, flag, p));
   1103 		case _O_WSDISPLAY_KBD_DEL:
   1104 			wsmuxdata.type = WSMUX_KBD;
   1105 			wsmuxdata.idx = d->idx;
   1106 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1107 					      &wsmuxdata, flag, p));
   1108 		default:
   1109 			return (EINVAL);
   1110 		}
   1111 #undef d
   1112 #endif
   1113 
   1114 	case WSMUXIO_ADD_DEVICE:
   1115 #define d ((struct wsmux_device *)data)
   1116 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1117 			d->idx = wskbd_pickfree();
   1118 #undef d
   1119 		/* fall into */
   1120 	case WSMUXIO_INJECTEVENT:
   1121 	case WSMUXIO_REMOVE_DEVICE:
   1122 	case WSMUXIO_LIST_DEVICES:
   1123 		inp = sc->sc_input;
   1124 		if (inp == NULL)
   1125 			return (ENXIO);
   1126 		return (wsevsrc_ioctl(inp, cmd, data, flag, p));
   1127 #endif /* NWSKBD > 0 */
   1128 
   1129 	}
   1130 	return (EINVAL);
   1131 }
   1132 
   1133 paddr_t
   1134 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1135 {
   1136 	struct wsdisplay_softc *sc =
   1137 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1138 	struct wsscreen *scr;
   1139 
   1140 	if (ISWSDISPLAYCTL(dev))
   1141 		return (-1);
   1142 
   1143 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
   1144 
   1145 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1146 		return (-1);
   1147 
   1148 	/* pass mmap to display */
   1149 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
   1150 }
   1151 
   1152 void
   1153 wsdisplaystart(struct tty *tp)
   1154 {
   1155 	struct wsdisplay_softc *sc;
   1156 	struct wsscreen *scr;
   1157 	int s, n;
   1158 	u_char *buf;
   1159 
   1160 	s = spltty();
   1161 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1162 		splx(s);
   1163 		return;
   1164 	}
   1165 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1166 	scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)];
   1167 	if (scr->scr_hold_screen) {
   1168 		tp->t_state |= TS_TIMEOUT;
   1169 		splx(s);
   1170 		return;
   1171 	}
   1172 	tp->t_state |= TS_BUSY;
   1173 	splx(s);
   1174 
   1175 	/*
   1176 	 * Drain output from ring buffer.
   1177 	 * The output will normally be in one contiguous chunk, but when the
   1178 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1179 	 * the other at the start.  For performance, rather than loop here,
   1180 	 * we output one chunk, see if there's another one, and if so, output
   1181 	 * it too.
   1182 	 */
   1183 
   1184 	n = ndqb(&tp->t_outq, 0);
   1185 	buf = tp->t_outq.c_cf;
   1186 
   1187 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1188 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1189 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1190 						  buf, n, 0);
   1191 	}
   1192 	ndflush(&tp->t_outq, n);
   1193 
   1194 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1195 		buf = tp->t_outq.c_cf;
   1196 
   1197 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1198 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1199 			(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1200 							  buf, n, 0);
   1201 		}
   1202 		ndflush(&tp->t_outq, n);
   1203 	}
   1204 
   1205 	s = spltty();
   1206 	tp->t_state &= ~TS_BUSY;
   1207 	/* Come back if there's more to do */
   1208 	if (tp->t_outq.c_cc) {
   1209 		tp->t_state |= TS_TIMEOUT;
   1210 		callout_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
   1211 		    ttrstrt, tp);
   1212 	}
   1213 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1214 		if (tp->t_state&TS_ASLEEP) {
   1215 			tp->t_state &= ~TS_ASLEEP;
   1216 			wakeup(&tp->t_outq);
   1217 		}
   1218 		selwakeup(&tp->t_wsel);
   1219 	}
   1220 	splx(s);
   1221 }
   1222 
   1223 void
   1224 wsdisplaystop(struct tty *tp, int flag)
   1225 {
   1226 	int s;
   1227 
   1228 	s = spltty();
   1229 	if (ISSET(tp->t_state, TS_BUSY))
   1230 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1231 			SET(tp->t_state, TS_FLUSH);
   1232 	splx(s);
   1233 }
   1234 
   1235 /* Set line parameters. */
   1236 int
   1237 wsdisplayparam(struct tty *tp, struct termios *t)
   1238 {
   1239 
   1240 	tp->t_ispeed = t->c_ispeed;
   1241 	tp->t_ospeed = t->c_ospeed;
   1242 	tp->t_cflag = t->c_cflag;
   1243 	return 0;
   1244 }
   1245 
   1246 /*
   1247  * Callbacks for the emulation code.
   1248  */
   1249 void
   1250 wsdisplay_emulbell(void *v)
   1251 {
   1252 	struct wsscreen *scr = v;
   1253 
   1254 	if (scr == NULL)		/* console, before real attach */
   1255 		return;
   1256 
   1257 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1258 		return;
   1259 
   1260 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1261 					FWRITE, NULL);
   1262 }
   1263 
   1264 void
   1265 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1266 {
   1267 	struct wsscreen *scr = v;
   1268 	struct tty *tp;
   1269 
   1270 	if (v == NULL)			/* console, before real attach */
   1271 		return;
   1272 
   1273 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1274 		return;
   1275 	if (!WSSCREEN_HAS_TTY(scr))
   1276 		return;
   1277 
   1278 	tp = scr->scr_tty;
   1279 	while (count-- > 0)
   1280 		(*tp->t_linesw->l_rint)(*data++, tp);
   1281 };
   1282 
   1283 /*
   1284  * Calls from the keyboard interface.
   1285  */
   1286 void
   1287 wsdisplay_kbdinput(struct device *dev, keysym_t ks)
   1288 {
   1289 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1290 	struct wsscreen *scr;
   1291 	char *dp;
   1292 	int count;
   1293 	struct tty *tp;
   1294 
   1295 	KASSERT(sc != NULL);
   1296 
   1297 	scr = sc->sc_focus;
   1298 
   1299 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1300 		return;
   1301 
   1302 	tp = scr->scr_tty;
   1303 
   1304 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1305 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
   1306 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1307 		count = (*scr->scr_dconf->wsemul->translate)
   1308 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1309 		while (count-- > 0)
   1310 			(*tp->t_linesw->l_rint)(*dp++, tp);
   1311 	}
   1312 }
   1313 
   1314 #if defined(WSDISPLAY_COMPAT_RAWKBD)
   1315 int
   1316 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
   1317 {
   1318 #if NWSKBD > 0
   1319 	int s, raw, data, error;
   1320 	struct wsevsrc *inp;
   1321 
   1322 	s = spltty();
   1323 
   1324 	raw = (scr ? scr->scr_rawkbd : 0);
   1325 
   1326 	if (scr != sc->sc_focus ||
   1327 	    sc->sc_rawkbd == raw) {
   1328 		splx(s);
   1329 		return (0);
   1330 	}
   1331 
   1332 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1333 	inp = sc->sc_input;
   1334 	if (inp == NULL)
   1335 		return (ENXIO);
   1336 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1337 	if (!error)
   1338 		sc->sc_rawkbd = raw;
   1339 	splx(s);
   1340 	return (error);
   1341 #else
   1342 	return (0);
   1343 #endif
   1344 }
   1345 #endif
   1346 
   1347 int
   1348 wsdisplay_switch3(void *arg, int error, int waitok)
   1349 {
   1350 	struct wsdisplay_softc *sc = arg;
   1351 	int no;
   1352 	struct wsscreen *scr;
   1353 
   1354 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1355 		printf("wsdisplay_switch3: not switching\n");
   1356 		return (EINVAL);
   1357 	}
   1358 
   1359 	no = sc->sc_screenwanted;
   1360 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1361 		panic("wsdisplay_switch3: invalid screen %d", no);
   1362 	scr = sc->sc_scr[no];
   1363 	if (!scr) {
   1364 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1365 		error = ENXIO;
   1366 	}
   1367 
   1368 	if (error) {
   1369 		/* try to recover, avoid recursion */
   1370 
   1371 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1372 			printf("wsdisplay_switch3: giving up\n");
   1373 			sc->sc_focus = 0;
   1374 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1375 			wsdisplay_update_rawkbd(sc, 0);
   1376 #endif
   1377 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1378 			return (error);
   1379 		}
   1380 
   1381 		sc->sc_screenwanted = sc->sc_oldscreen;
   1382 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1383 		return (wsdisplay_switch1(arg, 0, waitok));
   1384 	}
   1385 
   1386 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1387 
   1388 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1389 		wakeup(scr);
   1390 	return (error);
   1391 }
   1392 
   1393 int
   1394 wsdisplay_switch2(void *arg, int error, int waitok)
   1395 {
   1396 	struct wsdisplay_softc *sc = arg;
   1397 	int no;
   1398 	struct wsscreen *scr;
   1399 
   1400 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1401 		printf("wsdisplay_switch2: not switching\n");
   1402 		return (EINVAL);
   1403 	}
   1404 
   1405 	no = sc->sc_screenwanted;
   1406 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1407 		panic("wsdisplay_switch2: invalid screen %d", no);
   1408 	scr = sc->sc_scr[no];
   1409 	if (!scr) {
   1410 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
   1411 		error = ENXIO;
   1412 	}
   1413 
   1414 	if (error) {
   1415 		/* try to recover, avoid recursion */
   1416 
   1417 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1418 			printf("wsdisplay_switch2: giving up\n");
   1419 			sc->sc_focus = 0;
   1420 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1421 			return (error);
   1422 		}
   1423 
   1424 		sc->sc_screenwanted = sc->sc_oldscreen;
   1425 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1426 		return (wsdisplay_switch1(arg, 0, waitok));
   1427 	}
   1428 
   1429 	sc->sc_focusidx = no;
   1430 	sc->sc_focus = scr;
   1431 
   1432 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1433 	(void) wsdisplay_update_rawkbd(sc, scr);
   1434 #endif
   1435 	/* keyboard map??? */
   1436 
   1437 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
   1438 	if (scr->scr_syncops) {
   1439 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1440 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
   1441 		if (error == EAGAIN) {
   1442 			/* switch will be done asynchronously */
   1443 			return (0);
   1444 		}
   1445 	}
   1446 
   1447 	return (wsdisplay_switch3(sc, error, waitok));
   1448 }
   1449 
   1450 int
   1451 wsdisplay_switch1(void *arg, int error, int waitok)
   1452 {
   1453 	struct wsdisplay_softc *sc = arg;
   1454 	int no;
   1455 	struct wsscreen *scr;
   1456 
   1457 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1458 		printf("wsdisplay_switch1: not switching\n");
   1459 		return (EINVAL);
   1460 	}
   1461 
   1462 	no = sc->sc_screenwanted;
   1463 	if (no == WSDISPLAY_NULLSCREEN) {
   1464 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1465 		if (!error) {
   1466 			sc->sc_focus = 0;
   1467 		}
   1468 		wakeup(sc);
   1469 		return (error);
   1470 	}
   1471 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1472 		panic("wsdisplay_switch1: invalid screen %d", no);
   1473 	scr = sc->sc_scr[no];
   1474 	if (!scr) {
   1475 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1476 		error = ENXIO;
   1477 	}
   1478 
   1479 	if (error) {
   1480 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1481 		return (error);
   1482 	}
   1483 
   1484 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
   1485 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1486 						 scr->scr_dconf->emulcookie,
   1487 						 waitok,
   1488 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
   1489 	if (error == EAGAIN) {
   1490 		/* switch will be done asynchronously */
   1491 		return (0);
   1492 	}
   1493 
   1494 	return (wsdisplay_switch2(sc, error, waitok));
   1495 }
   1496 
   1497 int
   1498 wsdisplay_switch(struct device *dev, int no, int waitok)
   1499 {
   1500 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1501 	int s, res = 0;
   1502 	struct wsscreen *scr;
   1503 
   1504 	if (no != WSDISPLAY_NULLSCREEN &&
   1505 	    (no < 0 || no >= WSDISPLAY_MAXSCREEN || !sc->sc_scr[no]))
   1506 		return (ENXIO);
   1507 
   1508 	s = spltty();
   1509 
   1510 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   1511 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   1512 		splx(s);
   1513 		return (0);
   1514 	}
   1515 
   1516 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1517 		splx(s);
   1518 		return (EBUSY);
   1519 	}
   1520 
   1521 	sc->sc_flags |= SC_SWITCHPENDING;
   1522 	sc->sc_screenwanted = no;
   1523 
   1524 	splx(s);
   1525 
   1526 	scr = sc->sc_focus;
   1527 	if (!scr) {
   1528 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1529 		return (wsdisplay_switch1(sc, 0, waitok));
   1530 	} else
   1531 		sc->sc_oldscreen = sc->sc_focusidx;
   1532 
   1533 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
   1534 	if (scr->scr_syncops) {
   1535 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1536 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
   1537 		if (res == EAGAIN) {
   1538 			/* switch will be done asynchronously */
   1539 			return (0);
   1540 		}
   1541 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1542 		/* no way to save state */
   1543 		res = EBUSY;
   1544 	}
   1545 
   1546 	return (wsdisplay_switch1(sc, res, waitok));
   1547 }
   1548 
   1549 void
   1550 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
   1551 {
   1552 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1553 	struct wsscreen *scr;
   1554 
   1555 	KASSERT(sc != NULL);
   1556 	scr = sc->sc_focus;
   1557 
   1558 	if (!scr)
   1559 		return;
   1560 
   1561 	switch (op) {
   1562 	case WSDISPLAY_RESETEMUL:
   1563 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1564 			break;
   1565 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1566 						 WSEMUL_RESET);
   1567 		break;
   1568 	case WSDISPLAY_RESETCLOSE:
   1569 		wsdisplay_closescreen(sc, scr);
   1570 		break;
   1571 	}
   1572 }
   1573 
   1574 /*
   1575  * Interface for (external) VT switch / process synchronization code
   1576  */
   1577 int
   1578 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   1579 	void *cookie)
   1580 {
   1581 	if (scr->scr_syncops) {
   1582 		/*
   1583 		 * The screen is already claimed.
   1584 		 * Check if the owner is still alive.
   1585 		 */
   1586 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1587 			return (EBUSY);
   1588 	}
   1589 	scr->scr_syncops = ops;
   1590 	scr->scr_synccookie = cookie;
   1591 	return (0);
   1592 }
   1593 
   1594 int
   1595 wsscreen_detach_sync(struct wsscreen *scr)
   1596 {
   1597 	if (!scr->scr_syncops)
   1598 		return (EINVAL);
   1599 	scr->scr_syncops = 0;
   1600 	return (0);
   1601 }
   1602 
   1603 int
   1604 wsscreen_lookup_sync(struct wsscreen *scr,
   1605 	const struct wscons_syncops *ops, /* used as ID */
   1606 	void **cookiep)
   1607 {
   1608 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1609 		return (EINVAL);
   1610 	*cookiep = scr->scr_synccookie;
   1611 	return (0);
   1612 }
   1613 
   1614 /*
   1615  * Interface to virtual screen stuff
   1616  */
   1617 int
   1618 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   1619 {
   1620 	return (WSDISPLAY_MAXSCREEN - 1);
   1621 }
   1622 
   1623 int
   1624 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   1625 {
   1626 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   1627 		return (EINVAL);
   1628 	if (!sc->sc_scr[idx])
   1629 		return (ENXIO);
   1630 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1631 }
   1632 
   1633 int
   1634 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   1635 {
   1636 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   1637 }
   1638 
   1639 int
   1640 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   1641 {
   1642 	struct wsscreen *scr;
   1643 	int s, res = 0;
   1644 
   1645 	if (no == WSDISPLAY_NULLSCREEN) {
   1646 		s = spltty();
   1647 		while (sc->sc_focus && res == 0) {
   1648 			res = tsleep(sc, PCATCH, "wswait", 0);
   1649 		}
   1650 		splx(s);
   1651 		return (res);
   1652 	}
   1653 
   1654 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1655 		return (ENXIO);
   1656 	scr = sc->sc_scr[no];
   1657 	if (!scr)
   1658 		return (ENXIO);
   1659 
   1660 	s = spltty();
   1661 	if (scr != sc->sc_focus) {
   1662 		scr->scr_flags |= SCR_WAITACTIVE;
   1663 		res = tsleep(scr, PCATCH, "wswait", 0);
   1664 		if (scr != sc->sc_scr[no])
   1665 			res = ENXIO; /* disappeared in the meantime */
   1666 		else
   1667 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1668 	}
   1669 	splx(s);
   1670 	return (res);
   1671 }
   1672 
   1673 void
   1674 wsdisplay_kbdholdscreen(struct device *dev, int hold)
   1675 {
   1676 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1677 	struct wsscreen *scr;
   1678 
   1679 	scr = sc->sc_focus;
   1680 
   1681 	if (hold)
   1682 		scr->scr_hold_screen = 1;
   1683 	else {
   1684 		scr->scr_hold_screen = 0;
   1685 		callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
   1686 		    ttrstrt, scr->scr_tty);		/* "immediate" */
   1687 	}
   1688 }
   1689 
   1690 #if NWSKBD > 0
   1691 void
   1692 wsdisplay_set_console_kbd(struct wsevsrc *src)
   1693 {
   1694 	if (wsdisplay_console_device == NULL) {
   1695 		src->me_dispdv = NULL;
   1696 		return;
   1697 	}
   1698 #if NWSMUX > 0
   1699 	if (wsmux_attach_sc((struct wsmux_softc *)
   1700 			    wsdisplay_console_device->sc_input, src)) {
   1701 		src->me_dispdv = NULL;
   1702 		return;
   1703 	}
   1704 #else
   1705 	wsdisplay_console_device->sc_input = src;
   1706 #endif
   1707 	src->me_dispdv = &wsdisplay_console_device->sc_dv;
   1708 }
   1709 #endif /* NWSKBD > 0 */
   1710 
   1711 /*
   1712  * Console interface.
   1713  */
   1714 void
   1715 wsdisplay_cnputc(dev_t dev, int i)
   1716 {
   1717 	struct wsscreen_internal *dc;
   1718 	char c = i;
   1719 
   1720 	if (!wsdisplay_console_initted)
   1721 		return;
   1722 
   1723 	if (wsdisplay_console_device != NULL &&
   1724 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1725 		return;
   1726 
   1727 	dc = &wsdisplay_console_conf;
   1728 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1729 }
   1730 
   1731 static int
   1732 wsdisplay_getc_dummy(dev_t dev)
   1733 {
   1734 	/* panic? */
   1735 	return (0);
   1736 }
   1737 
   1738 static void
   1739 wsdisplay_pollc(dev_t dev, int on)
   1740 {
   1741 
   1742 	wsdisplay_cons_pollmode = on;
   1743 
   1744 	/* notify to fb drivers */
   1745 	if (wsdisplay_console_device != NULL &&
   1746 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   1747 		(*wsdisplay_console_device->sc_accessops->pollc)
   1748 			(wsdisplay_console_device->sc_accesscookie, on);
   1749 
   1750 	/* notify to kbd drivers */
   1751 	if (wsdisplay_cons_kbd_pollc)
   1752 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   1753 }
   1754 
   1755 void
   1756 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   1757 	void (*bell)(dev_t, u_int, u_int, u_int))
   1758 {
   1759 	wsdisplay_cons.cn_getc = get;
   1760 	wsdisplay_cons.cn_bell = bell;
   1761 	wsdisplay_cons_kbd_pollc = poll;
   1762 }
   1763 
   1764 void
   1765 wsdisplay_unset_cons_kbd(void)
   1766 {
   1767 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   1768 	wsdisplay_cons.cn_bell = NULL;
   1769 	wsdisplay_cons_kbd_pollc = 0;
   1770 }
   1771 
   1772 /*
   1773  * Switch the console display to it's first screen.
   1774  */
   1775 void
   1776 wsdisplay_switchtoconsole(void)
   1777 {
   1778 	struct wsdisplay_softc *sc;
   1779 	struct wsscreen *scr;
   1780 
   1781 	if (wsdisplay_console_device != NULL) {
   1782 		sc = wsdisplay_console_device;
   1783 		scr = sc->sc_scr[0];
   1784 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1785 						 scr->scr_dconf->emulcookie,
   1786 						 0, NULL, NULL);
   1787 	}
   1788 }
   1789 
   1790 /*
   1791  * Switch the console at shutdown.
   1792  */
   1793 static void
   1794 wsdisplay_shutdownhook(void *arg)
   1795 {
   1796 
   1797 	wsdisplay_switchtoconsole();
   1798 }
   1799