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