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