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