Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.37.4.3
      1 /* $NetBSD: wsdisplay.c,v 1.37.4.3 2001/08/16 16:19:12 tv 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.37.4.3 2001/08/16 16:19:12 tv 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 <dev/wscons/wsconsio.h>
     52 #include <dev/wscons/wsdisplayvar.h>
     53 #include <dev/wscons/wsksymvar.h>
     54 #include <dev/wscons/wsksymdef.h>
     55 #include <dev/wscons/wsemulvar.h>
     56 #include <dev/wscons/wscons_callbacks.h>
     57 #include <dev/cons.h>
     58 
     59 #include "opt_wsdisplay_compat.h"
     60 #include "opt_compat_netbsd.h"
     61 #include "wskbd.h"
     62 #include "wsmux.h"
     63 
     64 #if NWSKBD > 0
     65 #include <dev/wscons/wseventvar.h>
     66 #include <dev/wscons/wsmuxvar.h>
     67 #endif
     68 
     69 struct wsscreen_internal {
     70 	const struct wsdisplay_emulops *emulops;
     71 	void	*emulcookie;
     72 
     73 	const struct wsscreen_descr *scrdata;
     74 
     75 	const struct wsemul_ops *wsemul;
     76 	void	*wsemulcookie;
     77 };
     78 
     79 struct wsscreen {
     80 	struct wsscreen_internal *scr_dconf;
     81 
     82 	struct tty *scr_tty;
     83 	int	scr_hold_screen;		/* hold tty output */
     84 
     85 	int scr_flags;
     86 #define SCR_OPEN 1		/* is it open? */
     87 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     88 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     89 	const struct wscons_syncops *scr_syncops;
     90 	void *scr_synccookie;
     91 
     92 #ifdef WSDISPLAY_COMPAT_RAWKBD
     93 	int scr_rawkbd;
     94 #endif
     95 
     96 	struct wsdisplay_softc *sc;
     97 };
     98 
     99 struct wsscreen *wsscreen_attach __P((struct wsdisplay_softc *, int,
    100 				      const char *,
    101 				      const struct wsscreen_descr *, void *,
    102 				      int, int, long));
    103 void wsscreen_detach __P((struct wsscreen *));
    104 static const struct wsscreen_descr *
    105 wsdisplay_screentype_pick __P((const struct wsscreen_list *, const char *));
    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;
    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 static 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 		(*linesw[tp->t_line].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(dev, flag, mode, p)
    689 	dev_t dev;
    690 	int flag, mode;
    691 	struct proc *p;
    692 {
    693 	struct wsdisplay_softc *sc;
    694 	struct tty *tp;
    695 	int unit, newopen, error;
    696 	struct wsscreen *scr;
    697 
    698 	unit = WSDISPLAYUNIT(dev);
    699 	if (unit >= wsdisplay_cd.cd_ndevs ||	/* make sure it was attached */
    700 	    (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
    701 		return (ENXIO);
    702 
    703 	if (ISWSDISPLAYCTL(dev))
    704 		return (0);
    705 
    706 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    707 		return (ENXIO);
    708 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    709 	if (!scr)
    710 		return (ENXIO);
    711 
    712 	if (WSSCREEN_HAS_TTY(scr)) {
    713 		tp = scr->scr_tty;
    714 		tp->t_oproc = wsdisplaystart;
    715 		tp->t_param = wsdisplayparam;
    716 		tp->t_dev = dev;
    717 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    718 		if (newopen) {
    719 			ttychars(tp);
    720 			tp->t_iflag = TTYDEF_IFLAG;
    721 			tp->t_oflag = TTYDEF_OFLAG;
    722 			tp->t_cflag = TTYDEF_CFLAG;
    723 			tp->t_lflag = TTYDEF_LFLAG;
    724 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    725 			wsdisplayparam(tp, &tp->t_termios);
    726 			ttsetwater(tp);
    727 		} else if ((tp->t_state & TS_XCLUDE) != 0 &&
    728 			   p->p_ucred->cr_uid != 0)
    729 			return EBUSY;
    730 		tp->t_state |= TS_CARR_ON;
    731 
    732 		error = ((*linesw[tp->t_line].l_open)(dev, tp));
    733 		if (error)
    734 			return (error);
    735 
    736 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    737 			/* set window sizes as appropriate, and reset
    738 			 the emulation */
    739 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    740 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    741 
    742 			/* wsdisplay_set_emulation() */
    743 		}
    744 	}
    745 
    746 	scr->scr_flags |= SCR_OPEN;
    747 	return (0);
    748 }
    749 
    750 int
    751 wsdisplayclose(dev, flag, mode, p)
    752 	dev_t dev;
    753 	int flag, mode;
    754 	struct proc *p;
    755 {
    756 	struct wsdisplay_softc *sc;
    757 	struct tty *tp;
    758 	int unit;
    759 	struct wsscreen *scr;
    760 
    761 	unit = WSDISPLAYUNIT(dev);
    762 	sc = wsdisplay_cd.cd_devs[unit];
    763 
    764 	if (ISWSDISPLAYCTL(dev))
    765 		return (0);
    766 
    767 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    768 
    769 	if (WSSCREEN_HAS_TTY(scr)) {
    770 		if (scr->scr_hold_screen) {
    771 			int s;
    772 
    773 			/* XXX RESET KEYBOARD LEDS, etc. */
    774 			s = spltty();	/* avoid conflict with keyboard */
    775 			wsdisplay_kbdholdscreen((struct device *)sc, 0);
    776 			splx(s);
    777 		}
    778 		tp = scr->scr_tty;
    779 		(*linesw[tp->t_line].l_close)(tp, flag);
    780 		ttyclose(tp);
    781 	}
    782 
    783 	if (scr->scr_syncops)
    784 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    785 
    786 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    787 		scr->scr_flags &= ~SCR_GRAPHICS;
    788 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    789 						 WSEMUL_RESET);
    790 		if (wsdisplay_clearonclose)
    791 			(*scr->scr_dconf->wsemul->reset)
    792 				(scr->scr_dconf->wsemulcookie,
    793 				 WSEMUL_CLEARSCREEN);
    794 	}
    795 
    796 #ifdef WSDISPLAY_COMPAT_RAWKBD
    797 	if (scr->scr_rawkbd) {
    798 		int kbmode = WSKBD_TRANSLATED;
    799 		(void) wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    800 						(caddr_t)&kbmode, 0, p);
    801 	}
    802 #endif
    803 
    804 	scr->scr_flags &= ~SCR_OPEN;
    805 
    806 	return (0);
    807 }
    808 
    809 int
    810 wsdisplayread(dev, uio, flag)
    811 	dev_t dev;
    812 	struct uio *uio;
    813 	int flag;
    814 {
    815 	struct wsdisplay_softc *sc;
    816 	struct tty *tp;
    817 	int unit;
    818 	struct wsscreen *scr;
    819 
    820 	unit = WSDISPLAYUNIT(dev);
    821 	sc = wsdisplay_cd.cd_devs[unit];
    822 
    823 	if (ISWSDISPLAYCTL(dev))
    824 		return (0);
    825 
    826 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    827 
    828 	if (!WSSCREEN_HAS_TTY(scr))
    829 		return (ENODEV);
    830 
    831 	tp = scr->scr_tty;
    832 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    833 }
    834 
    835 int
    836 wsdisplaywrite(dev, uio, flag)
    837 	dev_t dev;
    838 	struct uio *uio;
    839 	int flag;
    840 {
    841 	struct wsdisplay_softc *sc;
    842 	struct tty *tp;
    843 	int unit;
    844 	struct wsscreen *scr;
    845 
    846 	unit = WSDISPLAYUNIT(dev);
    847 	sc = wsdisplay_cd.cd_devs[unit];
    848 
    849 	if (ISWSDISPLAYCTL(dev))
    850 		return (0);
    851 
    852 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    853 
    854 	if (!WSSCREEN_HAS_TTY(scr))
    855 		return (ENODEV);
    856 
    857 	tp = scr->scr_tty;
    858 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    859 }
    860 
    861 struct tty *
    862 wsdisplaytty(dev)
    863 	dev_t dev;
    864 {
    865 	struct wsdisplay_softc *sc;
    866 	int unit;
    867 	struct wsscreen *scr;
    868 
    869 	unit = WSDISPLAYUNIT(dev);
    870 	sc = wsdisplay_cd.cd_devs[unit];
    871 
    872 	if (ISWSDISPLAYCTL(dev))
    873 		panic("wsdisplaytty() on ctl device");
    874 
    875 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    876 
    877 	return (scr->scr_tty);
    878 }
    879 
    880 int
    881 wsdisplayioctl(dev, cmd, data, flag, p)
    882 	dev_t dev;
    883 	u_long cmd;
    884 	caddr_t data;
    885 	int flag;
    886 	struct proc *p;
    887 {
    888 	struct wsdisplay_softc *sc;
    889 	struct tty *tp;
    890 	int unit, error;
    891 	struct wsscreen *scr;
    892 
    893 	unit = WSDISPLAYUNIT(dev);
    894 	sc = wsdisplay_cd.cd_devs[unit];
    895 
    896 #ifdef WSDISPLAY_COMPAT_USL
    897 	error = wsdisplay_usl_ioctl1(sc, cmd, data, flag, p);
    898 	if (error >= 0)
    899 		return (error);
    900 #endif
    901 
    902 	if (ISWSDISPLAYCTL(dev))
    903 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, p));
    904 
    905 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    906 
    907 	if (WSSCREEN_HAS_TTY(scr)) {
    908 		tp = scr->scr_tty;
    909 
    910 /* printf("disc\n"); */
    911 		/* do the line discipline ioctls first */
    912 		error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    913 		if (error >= 0)
    914 			return (error);
    915 
    916 /* printf("tty\n"); */
    917 		/* then the tty ioctls */
    918 		error = ttioctl(tp, cmd, data, flag, p);
    919 		if (error >= 0)
    920 			return (error);
    921 	}
    922 
    923 #ifdef WSDISPLAY_COMPAT_USL
    924 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, p);
    925 	if (error >= 0)
    926 		return (error);
    927 #endif
    928 
    929 	error = wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p);
    930 	return (error != -1 ? error : ENOTTY);
    931 }
    932 
    933 int
    934 wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p)
    935 	struct wsdisplay_softc *sc;
    936 	struct wsscreen *scr;
    937 	u_long cmd;
    938 	caddr_t data;
    939 	int flag;
    940 	struct proc *p;
    941 {
    942 	int error;
    943 	char namebuf[16];
    944 	struct wsdisplay_font fd;
    945 
    946 #if NWSKBD > 0
    947 #ifdef WSDISPLAY_COMPAT_RAWKBD
    948 	switch (cmd) {
    949 	case WSKBDIO_SETMODE:
    950 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
    951 		return (wsdisplay_update_rawkbd(sc, scr));
    952 	case WSKBDIO_GETMODE:
    953 		*(int *)data = (scr->scr_rawkbd ?
    954 				WSKBD_RAW : WSKBD_TRANSLATED);
    955 		return (0);
    956 	}
    957 #endif
    958 	error = wsmux_displayioctl(&sc->sc_muxdv->sc_dv, cmd, data, flag, p);
    959 	if (error >= 0)
    960 		return (error);
    961 #endif /* NWSKBD > 0 */
    962 
    963 	switch (cmd) {
    964 	case WSDISPLAYIO_GMODE:
    965 		*(u_int *)data = (scr->scr_flags & SCR_GRAPHICS ?
    966 				  WSDISPLAYIO_MODE_MAPPED :
    967 				  WSDISPLAYIO_MODE_EMUL);
    968 		return (0);
    969 
    970 	case WSDISPLAYIO_SMODE:
    971 #define d (*(int *)data)
    972 		if (d != WSDISPLAYIO_MODE_EMUL &&
    973 		    d != WSDISPLAYIO_MODE_MAPPED)
    974 			return (EINVAL);
    975 
    976 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
    977 		    scr->scr_flags &= ~SCR_GRAPHICS;
    978 		    if (d == WSDISPLAYIO_MODE_MAPPED)
    979 			    scr->scr_flags |= SCR_GRAPHICS;
    980 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
    981 		    return (EINVAL);
    982 	    return (0);
    983 #undef d
    984 
    985 	case WSDISPLAYIO_USEFONT:
    986 #define d ((struct wsdisplay_usefontdata *)data)
    987 		if (!sc->sc_accessops->load_font)
    988 			return (EINVAL);
    989 		if (d->name) {
    990 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
    991 			if (error)
    992 				return (error);
    993 			fd.name = namebuf;
    994 		} else
    995 			fd.name = 0;
    996 		fd.data = 0;
    997 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
    998 					scr->scr_dconf->emulcookie, &fd);
    999 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1000 			(*scr->scr_dconf->wsemul->reset)
   1001 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1002 		return (error);
   1003 #undef d
   1004 	}
   1005 
   1006 	/* check ioctls for display */
   1007 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1008 	    flag, p));
   1009 }
   1010 
   1011 int
   1012 wsdisplay_cfg_ioctl(sc, cmd, data, flag, p)
   1013 	struct wsdisplay_softc *sc;
   1014 	u_long cmd;
   1015 	caddr_t data;
   1016 	int flag;
   1017 	struct proc *p;
   1018 {
   1019 	int error;
   1020 	char *type, typebuf[16], *emul, emulbuf[16];
   1021 	void *buf;
   1022 	u_int fontsz;
   1023 #if defined(COMPAT_14) && NWSKBD > 0
   1024 	struct wsmux_device wsmuxdata;
   1025 #endif
   1026 
   1027 	switch (cmd) {
   1028 	case WSDISPLAYIO_ADDSCREEN:
   1029 #define d ((struct wsdisplay_addscreendata *)data)
   1030 		if (d->screentype) {
   1031 			error = copyinstr(d->screentype, typebuf,
   1032 					  sizeof(typebuf), 0);
   1033 			if (error)
   1034 				return (error);
   1035 			type = typebuf;
   1036 		} else
   1037 			type = 0;
   1038 		if (d->emul) {
   1039 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1040 			if (error)
   1041 				return (error);
   1042 			emul = emulbuf;
   1043 		} else
   1044 			emul = 0;
   1045 
   1046 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1047 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1048 		return (error);
   1049 #undef d
   1050 	case WSDISPLAYIO_DELSCREEN:
   1051 #define d ((struct wsdisplay_delscreendata *)data)
   1052 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1053 #undef d
   1054 	case WSDISPLAYIO_LDFONT:
   1055 #define d ((struct wsdisplay_font *)data)
   1056 		if (!sc->sc_accessops->load_font)
   1057 			return (EINVAL);
   1058 		if (d->name) {
   1059 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1060 			if (error)
   1061 				return (error);
   1062 			d->name = typebuf;
   1063 		} else
   1064 			d->name = "loaded"; /* ??? */
   1065 		fontsz = d->fontheight * d->stride * d->numchars;
   1066 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1067 			return (EINVAL);
   1068 
   1069 		buf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1070 		error = copyin(d->data, buf, fontsz);
   1071 		if (error) {
   1072 			free(buf, M_DEVBUF);
   1073 			return (error);
   1074 		}
   1075 		d->data = buf;
   1076 		error =
   1077 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1078 		free(buf, M_DEVBUF);
   1079 #undef d
   1080 		return (error);
   1081 
   1082 #if NWSKBD > 0
   1083 #ifdef COMPAT_14
   1084 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1085 #define d ((struct wsdisplay_kbddata *)data)
   1086 		switch (d->op) {
   1087 		case _O_WSDISPLAY_KBD_ADD:
   1088 			if (d->idx == -1) {
   1089 				d->idx = wskbd_pickfree();
   1090 				if (d->idx == -1)
   1091 					return (ENXIO);
   1092 			}
   1093 			wsmuxdata.type = WSMUX_KBD;
   1094 			wsmuxdata.idx = d->idx;
   1095 			return (wsmuxdoioctl(&sc->sc_muxdv->sc_dv,
   1096 					     WSMUX_ADD_DEVICE,
   1097 					     (caddr_t)&wsmuxdata, flag, p));
   1098 		case _O_WSDISPLAY_KBD_DEL:
   1099 			wsmuxdata.type = WSMUX_KBD;
   1100 			wsmuxdata.idx = d->idx;
   1101 			return (wsmuxdoioctl(&sc->sc_muxdv->sc_dv,
   1102 					     WSMUX_REMOVE_DEVICE,
   1103 					     (caddr_t)&wsmuxdata, flag, p));
   1104 		default:
   1105 			return (EINVAL);
   1106 		}
   1107 #undef d
   1108 #endif
   1109 
   1110 	case WSMUX_ADD_DEVICE:
   1111 #define d ((struct wsmux_device *)data)
   1112 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1113 			d->idx = wskbd_pickfree();
   1114 #undef d
   1115 		/* fall into */
   1116 	case WSMUX_INJECTEVENT:
   1117 	case WSMUX_REMOVE_DEVICE:
   1118 	case WSMUX_LIST_DEVICES:
   1119 		return (wsmuxdoioctl(&sc->sc_muxdv->sc_dv, cmd, data, flag,p));
   1120 #endif /* NWSKBD > 0 */
   1121 
   1122 	}
   1123 	return (EINVAL);
   1124 }
   1125 
   1126 paddr_t
   1127 wsdisplaymmap(dev, offset, prot)
   1128 	dev_t dev;
   1129 	off_t offset;
   1130 	int prot;
   1131 {
   1132 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
   1133 	struct wsscreen *scr;
   1134 
   1135 	if (ISWSDISPLAYCTL(dev))
   1136 		return (-1);
   1137 
   1138 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
   1139 
   1140 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1141 		return (-1);
   1142 
   1143 	/* pass mmap to display */
   1144 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
   1145 }
   1146 
   1147 int
   1148 wsdisplaypoll(dev, events, p)
   1149 	dev_t dev;
   1150 	int events;
   1151 	struct proc *p;
   1152 {
   1153 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
   1154 	struct wsscreen *scr;
   1155 
   1156 	if (ISWSDISPLAYCTL(dev))
   1157 		return (0);
   1158 
   1159 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
   1160 
   1161 	if (WSSCREEN_HAS_TTY(scr))
   1162 		return (ttpoll(dev, events, p));
   1163 	else
   1164 		return (0);
   1165 }
   1166 
   1167 void
   1168 wsdisplaystart(tp)
   1169 	struct tty *tp;
   1170 {
   1171 	struct wsdisplay_softc *sc;
   1172 	struct wsscreen *scr;
   1173 	int s, n;
   1174 	u_char *buf;
   1175 
   1176 	s = spltty();
   1177 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1178 		splx(s);
   1179 		return;
   1180 	}
   1181 	sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(tp->t_dev)];
   1182 	scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)];
   1183 	if (scr->scr_hold_screen) {
   1184 		tp->t_state |= TS_TIMEOUT;
   1185 		splx(s);
   1186 		return;
   1187 	}
   1188 	tp->t_state |= TS_BUSY;
   1189 	splx(s);
   1190 
   1191 	/*
   1192 	 * Drain output from ring buffer.
   1193 	 * The output will normally be in one contiguous chunk, but when the
   1194 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1195 	 * the other at the start.  For performance, rather than loop here,
   1196 	 * we output one chunk, see if there's another one, and if so, output
   1197 	 * it too.
   1198 	 */
   1199 
   1200 	n = ndqb(&tp->t_outq, 0);
   1201 	buf = tp->t_outq.c_cf;
   1202 
   1203 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1204 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1205 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1206 						  buf, n, 0);
   1207 	}
   1208 	ndflush(&tp->t_outq, n);
   1209 
   1210 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1211 		buf = tp->t_outq.c_cf;
   1212 
   1213 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1214 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1215 			(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1216 							  buf, n, 0);
   1217 		}
   1218 		ndflush(&tp->t_outq, n);
   1219 	}
   1220 
   1221 	s = spltty();
   1222 	tp->t_state &= ~TS_BUSY;
   1223 	/* Come back if there's more to do */
   1224 	if (tp->t_outq.c_cc) {
   1225 		tp->t_state |= TS_TIMEOUT;
   1226 		callout_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
   1227 		    ttrstrt, tp);
   1228 	}
   1229 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1230 		if (tp->t_state&TS_ASLEEP) {
   1231 			tp->t_state &= ~TS_ASLEEP;
   1232 			wakeup((caddr_t)&tp->t_outq);
   1233 		}
   1234 		selwakeup(&tp->t_wsel);
   1235 	}
   1236 	splx(s);
   1237 }
   1238 
   1239 void
   1240 wsdisplaystop(tp, flag)
   1241 	struct tty *tp;
   1242 	int flag;
   1243 {
   1244 	int s;
   1245 
   1246 	s = spltty();
   1247 	if (ISSET(tp->t_state, TS_BUSY))
   1248 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1249 			SET(tp->t_state, TS_FLUSH);
   1250 	splx(s);
   1251 }
   1252 
   1253 /* Set line parameters. */
   1254 int
   1255 wsdisplayparam(tp, t)
   1256 	struct tty *tp;
   1257 	struct termios *t;
   1258 {
   1259 
   1260 	tp->t_ispeed = t->c_ispeed;
   1261 	tp->t_ospeed = t->c_ospeed;
   1262 	tp->t_cflag = t->c_cflag;
   1263 	return 0;
   1264 }
   1265 
   1266 /*
   1267  * Callbacks for the emulation code.
   1268  */
   1269 void
   1270 wsdisplay_emulbell(v)
   1271 	void *v;
   1272 {
   1273 	struct wsscreen *scr = v;
   1274 
   1275 	if (scr == NULL)		/* console, before real attach */
   1276 		return;
   1277 
   1278 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1279 		return;
   1280 
   1281 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1282 					FWRITE, NULL);
   1283 }
   1284 
   1285 void
   1286 wsdisplay_emulinput(v, data, count)
   1287 	void *v;
   1288 	const u_char *data;
   1289 	u_int count;
   1290 {
   1291 	struct wsscreen *scr = v;
   1292 	struct tty *tp;
   1293 
   1294 	if (v == NULL)			/* console, before real attach */
   1295 		return;
   1296 
   1297 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1298 		return;
   1299 	if (!WSSCREEN_HAS_TTY(scr))
   1300 		return;
   1301 
   1302 	tp = scr->scr_tty;
   1303 	while (count-- > 0)
   1304 		(*linesw[tp->t_line].l_rint)(*data++, tp);
   1305 };
   1306 
   1307 /*
   1308  * Calls from the keyboard interface.
   1309  */
   1310 void
   1311 wsdisplay_kbdinput(dev, ks)
   1312 	struct device *dev;
   1313 	keysym_t ks;
   1314 {
   1315 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1316 	struct wsscreen *scr;
   1317 	char *dp;
   1318 	int count;
   1319 	struct tty *tp;
   1320 
   1321 	KASSERT(sc != NULL);
   1322 
   1323 	scr = sc->sc_focus;
   1324 
   1325 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1326 		return;
   1327 
   1328 	tp = scr->scr_tty;
   1329 
   1330 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1331 		(*linesw[tp->t_line].l_rint)(KS_VALUE(ks), tp);
   1332 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1333 		count = (*scr->scr_dconf->wsemul->translate)
   1334 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1335 		while (count-- > 0)
   1336 			(*linesw[tp->t_line].l_rint)(*dp++, tp);
   1337 	}
   1338 }
   1339 
   1340 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1341 int
   1342 wsdisplay_update_rawkbd(sc, scr)
   1343 	struct wsdisplay_softc *sc;
   1344 	struct wsscreen *scr;
   1345 {
   1346 	int s, raw, data, error;
   1347 	s = spltty();
   1348 
   1349 	raw = (scr ? scr->scr_rawkbd : 0);
   1350 
   1351 	if (scr != sc->sc_focus ||
   1352 	    sc->sc_rawkbd == raw) {
   1353 		splx(s);
   1354 		return (0);
   1355 	}
   1356 
   1357 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1358 	error = wsmux_displayioctl(&sc->sc_muxdv->sc_dv, WSKBDIO_SETMODE,
   1359 				   (caddr_t)&data, 0, 0);
   1360 	if (!error)
   1361 		sc->sc_rawkbd = raw;
   1362 	splx(s);
   1363 	return (error);
   1364 }
   1365 #endif
   1366 
   1367 int
   1368 wsdisplay_switch3(arg, error, waitok)
   1369 	void *arg;
   1370 	int error, waitok;
   1371 {
   1372 	struct wsdisplay_softc *sc = arg;
   1373 	int no;
   1374 	struct wsscreen *scr;
   1375 
   1376 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1377 		printf("wsdisplay_switch3: not switching\n");
   1378 		return (EINVAL);
   1379 	}
   1380 
   1381 	no = sc->sc_screenwanted;
   1382 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1383 		panic("wsdisplay_switch3: invalid screen %d", no);
   1384 	scr = sc->sc_scr[no];
   1385 	if (!scr) {
   1386 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1387 		error = ENXIO;
   1388 	}
   1389 
   1390 	if (error) {
   1391 		/* try to recover, avoid recursion */
   1392 
   1393 		if (sc->sc_oldscreen == -1) {
   1394 			printf("wsdisplay_switch3: giving up\n");
   1395 			sc->sc_focus = 0;
   1396 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1397 			wsdisplay_update_rawkbd(sc, 0);
   1398 #endif
   1399 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1400 			return (error);
   1401 		}
   1402 
   1403 		sc->sc_screenwanted = sc->sc_oldscreen;
   1404 		sc->sc_oldscreen = -1;
   1405 		return (wsdisplay_switch1(arg, 0, waitok));
   1406 	}
   1407 
   1408 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1409 
   1410 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1411 		wakeup(scr);
   1412 	return (error);
   1413 }
   1414 
   1415 int
   1416 wsdisplay_switch2(arg, error, waitok)
   1417 	void *arg;
   1418 	int error, waitok;
   1419 {
   1420 	struct wsdisplay_softc *sc = arg;
   1421 	int no;
   1422 	struct wsscreen *scr;
   1423 
   1424 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1425 		printf("wsdisplay_switch2: not switching\n");
   1426 		return (EINVAL);
   1427 	}
   1428 
   1429 	no = sc->sc_screenwanted;
   1430 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1431 		panic("wsdisplay_switch2: invalid screen %d", no);
   1432 	scr = sc->sc_scr[no];
   1433 	if (!scr) {
   1434 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
   1435 		error = ENXIO;
   1436 	}
   1437 
   1438 	if (error) {
   1439 		/* try to recover, avoid recursion */
   1440 
   1441 		if (sc->sc_oldscreen == -1) {
   1442 			printf("wsdisplay_switch2: giving up\n");
   1443 			sc->sc_focus = 0;
   1444 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1445 			return (error);
   1446 		}
   1447 
   1448 		sc->sc_screenwanted = sc->sc_oldscreen;
   1449 		sc->sc_oldscreen = -1;
   1450 		return (wsdisplay_switch1(arg, 0, waitok));
   1451 	}
   1452 
   1453 	sc->sc_focusidx = no;
   1454 	sc->sc_focus = scr;
   1455 
   1456 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1457 	(void) wsdisplay_update_rawkbd(sc, scr);
   1458 #endif
   1459 	/* keyboard map??? */
   1460 
   1461 #define wsswitch_cb3 ((void (*) __P((void *, int, int)))wsdisplay_switch3)
   1462 	if (scr->scr_syncops) {
   1463 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1464 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
   1465 		if (error == EAGAIN) {
   1466 			/* switch will be done asynchronously */
   1467 			return (0);
   1468 		}
   1469 	}
   1470 
   1471 	return (wsdisplay_switch3(sc, error, waitok));
   1472 }
   1473 
   1474 int
   1475 wsdisplay_switch1(arg, error, waitok)
   1476 	void *arg;
   1477 	int error, waitok;
   1478 {
   1479 	struct wsdisplay_softc *sc = arg;
   1480 	int no;
   1481 	struct wsscreen *scr;
   1482 
   1483 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1484 		printf("wsdisplay_switch1: not switching\n");
   1485 		return (EINVAL);
   1486 	}
   1487 
   1488 	no = sc->sc_screenwanted;
   1489 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1490 		panic("wsdisplay_switch1: invalid screen %d", no);
   1491 	scr = sc->sc_scr[no];
   1492 	if (!scr) {
   1493 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1494 		error = ENXIO;
   1495 	}
   1496 
   1497 	if (error) {
   1498 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1499 		return (error);
   1500 	}
   1501 
   1502 #define wsswitch_cb2 ((void (*) __P((void *, int, int)))wsdisplay_switch2)
   1503 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1504 						 scr->scr_dconf->emulcookie,
   1505 						 waitok,
   1506 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
   1507 	if (error == EAGAIN) {
   1508 		/* switch will be done asynchronously */
   1509 		return (0);
   1510 	}
   1511 
   1512 	return (wsdisplay_switch2(sc, error, waitok));
   1513 }
   1514 
   1515 int
   1516 wsdisplay_switch(dev, no, waitok)
   1517 	struct device *dev;
   1518 	int no, waitok;
   1519 {
   1520 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1521 	int s, res = 0;
   1522 	struct wsscreen *scr;
   1523 
   1524 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN || !sc->sc_scr[no])
   1525 		return (ENXIO);
   1526 
   1527 	s = spltty();
   1528 
   1529 	if (sc->sc_focus && no == sc->sc_focusidx) {
   1530 		splx(s);
   1531 		return (0);
   1532 	}
   1533 
   1534 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1535 		splx(s);
   1536 		return (EBUSY);
   1537 	}
   1538 
   1539 	sc->sc_flags |= SC_SWITCHPENDING;
   1540 	sc->sc_screenwanted = no;
   1541 
   1542 	splx(s);
   1543 
   1544 	scr = sc->sc_focus;
   1545 	if (!scr) {
   1546 		sc->sc_oldscreen = -1;
   1547 		return (wsdisplay_switch1(sc, 0, waitok));
   1548 	} else
   1549 		sc->sc_oldscreen = sc->sc_focusidx;
   1550 
   1551 #define wsswitch_cb1 ((void (*) __P((void *, int, int)))wsdisplay_switch1)
   1552 	if (scr->scr_syncops) {
   1553 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1554 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
   1555 		if (res == EAGAIN) {
   1556 			/* switch will be done asynchronously */
   1557 			return (0);
   1558 		}
   1559 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1560 		/* no way to save state */
   1561 		res = EBUSY;
   1562 	}
   1563 
   1564 	return (wsdisplay_switch1(sc, res, waitok));
   1565 }
   1566 
   1567 void
   1568 wsdisplay_reset(dev, op)
   1569 	struct device *dev;
   1570 	enum wsdisplay_resetops op;
   1571 {
   1572 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1573 	struct wsscreen *scr;
   1574 
   1575 	KASSERT(sc != NULL);
   1576 	scr = sc->sc_focus;
   1577 
   1578 	if (!scr)
   1579 		return;
   1580 
   1581 	switch (op) {
   1582 	case WSDISPLAY_RESETEMUL:
   1583 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1584 			break;
   1585 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1586 						 WSEMUL_RESET);
   1587 		break;
   1588 	case WSDISPLAY_RESETCLOSE:
   1589 		wsdisplay_closescreen(sc, scr);
   1590 		break;
   1591 	}
   1592 }
   1593 
   1594 /*
   1595  * Interface for (external) VT switch / process synchronization code
   1596  */
   1597 int
   1598 wsscreen_attach_sync(scr, ops, cookie)
   1599 	struct wsscreen *scr;
   1600 	const struct wscons_syncops *ops;
   1601 	void *cookie;
   1602 {
   1603 	if (scr->scr_syncops) {
   1604 		/*
   1605 		 * The screen is already claimed.
   1606 		 * Check if the owner is still alive.
   1607 		 */
   1608 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1609 			return (EBUSY);
   1610 	}
   1611 	scr->scr_syncops = ops;
   1612 	scr->scr_synccookie = cookie;
   1613 	return (0);
   1614 }
   1615 
   1616 int
   1617 wsscreen_detach_sync(scr)
   1618 	struct wsscreen *scr;
   1619 {
   1620 	if (!scr->scr_syncops)
   1621 		return (EINVAL);
   1622 	scr->scr_syncops = 0;
   1623 	return (0);
   1624 }
   1625 
   1626 int
   1627 wsscreen_lookup_sync(scr, ops, cookiep)
   1628 	struct wsscreen *scr;
   1629 	const struct wscons_syncops *ops; /* used as ID */
   1630 	void **cookiep;
   1631 {
   1632 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1633 		return (EINVAL);
   1634 	*cookiep = scr->scr_synccookie;
   1635 	return (0);
   1636 }
   1637 
   1638 /*
   1639  * Interface to virtual screen stuff
   1640  */
   1641 int
   1642 wsdisplay_maxscreenidx(sc)
   1643 	struct wsdisplay_softc *sc;
   1644 {
   1645 	return (WSDISPLAY_MAXSCREEN - 1);
   1646 }
   1647 
   1648 int
   1649 wsdisplay_screenstate(sc, idx)
   1650 	struct wsdisplay_softc *sc;
   1651 	int idx;
   1652 {
   1653 	if (idx >= WSDISPLAY_MAXSCREEN)
   1654 		return (EINVAL);
   1655 	if (!sc->sc_scr[idx])
   1656 		return (ENXIO);
   1657 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1658 }
   1659 
   1660 int
   1661 wsdisplay_getactivescreen(sc)
   1662 	struct wsdisplay_softc *sc;
   1663 {
   1664 	return (sc->sc_focusidx);
   1665 }
   1666 
   1667 int
   1668 wsscreen_switchwait(sc, no)
   1669 	struct wsdisplay_softc *sc;
   1670 	int no;
   1671 {
   1672 	struct wsscreen *scr;
   1673 	int s, res = 0;
   1674 
   1675 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1676 		return (ENXIO);
   1677 	scr = sc->sc_scr[no];
   1678 	if (!scr)
   1679 		return (ENXIO);
   1680 
   1681 	s = spltty();
   1682 	if (scr != sc->sc_focus) {
   1683 		scr->scr_flags |= SCR_WAITACTIVE;
   1684 		res = tsleep(scr, PCATCH, "wswait", 0);
   1685 		if (scr != sc->sc_scr[no])
   1686 			res = ENXIO; /* disappeared in the meantime */
   1687 		else
   1688 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1689 	}
   1690 	splx(s);
   1691 	return (res);
   1692 }
   1693 
   1694 void
   1695 wsdisplay_kbdholdscreen(dev, hold)
   1696 	struct device *dev;
   1697 	int hold;
   1698 {
   1699 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1700 	struct wsscreen *scr;
   1701 
   1702 	scr = sc->sc_focus;
   1703 
   1704 	if (hold)
   1705 		scr->scr_hold_screen = 1;
   1706 	else {
   1707 		scr->scr_hold_screen = 0;
   1708 		callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
   1709 		    ttrstrt, scr->scr_tty);		/* "immediate" */
   1710 	}
   1711 }
   1712 
   1713 #if NWSKBD > 0
   1714 struct device *
   1715 wsdisplay_set_console_kbd(kbddv)
   1716 	struct device *kbddv;
   1717 {
   1718 	if (!wsdisplay_console_device)
   1719 		return (0);
   1720 	if (wskbd_add_mux(kbddv->dv_unit, wsdisplay_console_device->sc_muxdv))
   1721 		return (0);
   1722 	return (&wsdisplay_console_device->sc_dv);
   1723 }
   1724 #endif /* NWSKBD > 0 */
   1725 
   1726 /*
   1727  * Console interface.
   1728  */
   1729 void
   1730 wsdisplay_cnputc(dev, i)
   1731 	dev_t dev;
   1732 	int i;
   1733 {
   1734 	struct wsscreen_internal *dc;
   1735 	char c = i;
   1736 
   1737 	if (!wsdisplay_console_initted)
   1738 		return;
   1739 
   1740 	if (wsdisplay_console_device != NULL &&
   1741 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1742 		return;
   1743 
   1744 	dc = &wsdisplay_console_conf;
   1745 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1746 }
   1747 
   1748 static int
   1749 wsdisplay_getc_dummy(dev)
   1750 	dev_t dev;
   1751 {
   1752 	/* panic? */
   1753 	return (0);
   1754 }
   1755 
   1756 static void
   1757 wsdisplay_pollc(dev, on)
   1758 	dev_t dev;
   1759 	int on;
   1760 {
   1761 
   1762 	wsdisplay_cons_pollmode = on;
   1763 
   1764 	if (wsdisplay_cons_kbd_pollc)
   1765 		(*wsdisplay_cons_kbd_pollc)(dev, on);
   1766 }
   1767 
   1768 void
   1769 wsdisplay_set_cons_kbd(get, poll, bell)
   1770 	int (*get) __P((dev_t));
   1771 	void (*poll) __P((dev_t, int));
   1772 	void (*bell) __P((dev_t, u_int, u_int, u_int));
   1773 {
   1774 	wsdisplay_cons.cn_getc = get;
   1775 	wsdisplay_cons.cn_bell = bell;
   1776 	wsdisplay_cons_kbd_pollc = poll;
   1777 }
   1778 
   1779 void
   1780 wsdisplay_unset_cons_kbd()
   1781 {
   1782 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   1783 	wsdisplay_cons.cn_bell = NULL;
   1784 	wsdisplay_cons_kbd_pollc = 0;
   1785 }
   1786 
   1787 /*
   1788  * Switch the console display to it's first screen.
   1789  */
   1790 void
   1791 wsdisplay_switchtoconsole()
   1792 {
   1793 	struct wsdisplay_softc *sc;
   1794 	struct wsscreen *scr;
   1795 
   1796 	if (wsdisplay_console_device != NULL) {
   1797 		sc = wsdisplay_console_device;
   1798 		scr = sc->sc_scr[0];
   1799 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1800 						 scr->scr_dconf->emulcookie,
   1801 						 0, NULL, NULL);
   1802 	}
   1803 }
   1804 
   1805 /*
   1806  * Switch the console at shutdown.
   1807  */
   1808 static void
   1809 wsdisplay_shutdownhook(arg)
   1810 	void *arg;
   1811 {
   1812 
   1813 	wsdisplay_switchtoconsole();
   1814 }
   1815