Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.13
      1 /* $NetBSD: wsdisplay.c,v 1.13 1999/01/09 15:42:34 drochner 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.13 1999/01/09 15:42:34 drochner 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 
     63 #include "wsdisplay.h"
     64 
     65 struct wsscreen_internal {
     66 	const struct wsdisplay_emulops *emulops;
     67 	void	*emulcookie;
     68 
     69 	const struct wsscreen_descr *scrdata;
     70 
     71 	const struct wsemul_ops *wsemul;
     72 	void	*wsemulcookie;
     73 };
     74 
     75 struct wsscreen {
     76 	struct wsscreen_internal *scr_dconf;
     77 
     78 	struct tty *scr_tty;
     79 	int	scr_hold_screen;		/* hold tty output */
     80 
     81 	int scr_flags;
     82 #define SCR_OPEN 1		/* is it open? */
     83 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     84 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     85 	const struct wscons_syncops *scr_syncops;
     86 	void *scr_synccookie;
     87 
     88 #ifdef WSDISPLAY_COMPAT_RAWKBD
     89 	int scr_rawkbd;
     90 #endif
     91 
     92 	struct wsdisplay_softc *sc;
     93 };
     94 
     95 struct wsscreen *wsscreen_attach __P((struct wsdisplay_softc *, int,
     96 				      const char *,
     97 				      const struct wsscreen_descr *, void *,
     98 				      int, int, long));
     99 void wsscreen_detach __P((struct wsscreen *));
    100 static const struct wsscreen_descr *
    101 wsdisplay_screentype_pick __P((const struct wsscreen_list *, char *));
    102 int wsdisplay_addscreen __P((struct wsdisplay_softc *, int, char *, char *));
    103 int wsdisplay_delscreen __P((struct wsdisplay_softc *, int, int));
    104 
    105 #define WSDISPLAY_MAXSCREEN 8
    106 
    107 struct wsdisplay_softc {
    108 	struct device sc_dv;
    109 
    110 	const struct wsdisplay_accessops *sc_accessops;
    111 	void	*sc_accesscookie;
    112 
    113 	const struct wsscreen_list *sc_scrdata;
    114 
    115 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    116 	int sc_focusidx;
    117 	struct wsscreen *sc_focus;
    118 
    119 	int	sc_isconsole;
    120 	struct device *sc_kbddv;
    121 
    122 	int sc_flags;
    123 #define SC_SWITCHPENDING 1
    124 	int sc_screenwanted; /* valid with SC_SWITCHPENDING */
    125 
    126 #ifdef WSDISPLAY_COMPAT_RAWKBD
    127 	int sc_rawkbd;
    128 #endif
    129 };
    130 
    131 #if NWSDISPLAY > 0
    132 extern struct cfdriver wsdisplay_cd;
    133 #endif /* NWSDISPLAY > 0 */
    134 
    135 /* Autoconfiguration definitions. */
    136 static int wsdisplay_emul_match __P((struct device *, struct cfdata *,
    137 	    void *));
    138 static void wsdisplay_emul_attach __P((struct device *, struct device *,
    139 	    void *));
    140 static int wsdisplay_noemul_match __P((struct device *, struct cfdata *,
    141 	    void *));
    142 static void wsdisplay_noemul_attach __P((struct device *, struct device *,
    143 	    void *));
    144 
    145 struct cfattach wsdisplay_emul_ca = {
    146 	sizeof (struct wsdisplay_softc),
    147 	wsdisplay_emul_match,
    148 	wsdisplay_emul_attach,
    149 };
    150 
    151 struct cfattach wsdisplay_noemul_ca = {
    152 	sizeof (struct wsdisplay_softc),
    153 	wsdisplay_noemul_match,
    154 	wsdisplay_noemul_attach,
    155 };
    156 
    157 /* Exported tty- and cdevsw-related functions. */
    158 cdev_decl(wsdisplay);
    159 
    160 #if NWSDISPLAY > 0
    161 static void wsdisplaystart __P((struct tty *));
    162 static int wsdisplayparam __P((struct tty *, struct termios *));
    163 #endif /* NWSDISPLAY > 0 */
    164 
    165 
    166 /* Internal macros, functions, and variables. */
    167 #define	SET(t, f)	(t) |= (f)
    168 #define	CLR(t, f)	(t) &= ~(f)
    169 #define	ISSET(t, f)	((t) & (f))
    170 
    171 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    172 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    173 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    174 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    175 #define	WSDISPLAYBURST		(OBUFSIZ - 1)
    176 
    177 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    178 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    179 
    180 static void wsdisplay_common_attach __P((struct wsdisplay_softc *sc,
    181 	    int console, const struct wsscreen_list *,
    182 	    const struct wsdisplay_accessops *accessops,
    183 	    void *accesscookie));
    184 
    185 #ifdef WSDISPLAY_COMPAT_RAWKBD
    186 int wsdisplay_update_rawkbd __P((struct wsdisplay_softc *,
    187 				 struct wsscreen *));
    188 #endif
    189 
    190 static int wsdisplay_console_initted;
    191 static struct wsdisplay_softc *wsdisplay_console_device;
    192 static struct wsscreen_internal wsdisplay_console_conf;
    193 
    194 static int wsdisplay_getc_dummy __P((dev_t));
    195 static void wsdisplay_pollc_dummy __P((dev_t, int));
    196 
    197 static struct consdev wsdisplay_cons = {
    198 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
    199 	wsdisplay_pollc_dummy, NODEV, CN_NORMAL
    200 };
    201 
    202 int wsdisplay_switch1 __P((void *, int));
    203 
    204 struct wsscreen *
    205 wsscreen_attach(sc, console, emul, type, cookie, ccol, crow, defattr)
    206 	struct wsdisplay_softc *sc;
    207 	int console;
    208 	const char *emul;
    209 	const struct wsscreen_descr *type;
    210 	void *cookie;
    211 	int ccol, crow;
    212 	long defattr;
    213 {
    214 	struct wsscreen_internal *dconf;
    215 	struct wsscreen *scr;
    216 
    217 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    218 	if (!scr)
    219 		return (NULL);
    220 
    221 	if (console) {
    222 		dconf = &wsdisplay_console_conf;
    223 		/*
    224 		 * If there's an emulation, tell it about the callback argument.
    225 		 * The other stuff is already there.
    226 		 */
    227 		if (dconf->wsemul != NULL)
    228 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    229 	} else { /* not console */
    230 		dconf = malloc(sizeof(struct wsscreen_internal),
    231 			       M_DEVBUF, M_NOWAIT);
    232 		dconf->emulops = type->textops;
    233 		dconf->emulcookie = cookie;
    234 		if (dconf->emulops) {
    235 			dconf->wsemul = wsemul_pick(emul);
    236 			if (dconf->wsemul == NULL) {
    237 				free(dconf, M_DEVBUF);
    238 				free(scr, M_DEVBUF);
    239 				return (NULL);
    240 			}
    241 			dconf->wsemulcookie =
    242 			  (*dconf->wsemul->attach)(0, type, cookie,
    243 						   ccol, crow, scr, defattr);
    244 		} else
    245 			dconf->wsemul = NULL;
    246 		dconf->scrdata = type;
    247 	}
    248 
    249 	scr->scr_dconf = dconf;
    250 
    251 	scr->scr_tty = ttymalloc();
    252 	tty_attach(scr->scr_tty);
    253 	scr->scr_hold_screen = 0;
    254 	if (WSSCREEN_HAS_EMULATOR(scr))
    255 		scr->scr_flags = 0;
    256 	else
    257 		scr->scr_flags = SCR_GRAPHICS;
    258 
    259 	scr->scr_syncops = 0;
    260 	scr->sc = sc;
    261 #ifdef WSDISPLAY_COMPAT_RAWKBD
    262 	scr->scr_rawkbd = 0;
    263 #endif
    264 	return (scr);
    265 }
    266 
    267 void
    268 wsscreen_detach(scr)
    269 	struct wsscreen *scr;
    270 {
    271 	int ccol, crow; /* XXX */
    272 
    273 	if (WSSCREEN_HAS_TTY(scr)) {
    274 		tty_detach(scr->scr_tty);
    275 		ttyfree(scr->scr_tty);
    276 	}
    277 	if (WSSCREEN_HAS_EMULATOR(scr))
    278 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    279 						  &ccol, &crow);
    280 	free(scr->scr_dconf, M_DEVBUF);
    281 	free(scr, M_DEVBUF);
    282 }
    283 
    284 static const struct wsscreen_descr *
    285 wsdisplay_screentype_pick(scrdata, name)
    286 	const struct wsscreen_list *scrdata;
    287 	char *name;
    288 {
    289 	int i;
    290 	const struct wsscreen_descr *scr;
    291 
    292 	KASSERT(scrdata->nscreens > 0);
    293 
    294 	if (name == NULL)
    295 		return (scrdata->screens[0]);
    296 
    297 	for (i = 0; i < scrdata->nscreens; i++) {
    298 		scr = scrdata->screens[i];
    299 		if (!strcmp(name, scr->name))
    300 			return (scr);
    301 	}
    302 
    303 	return (0);
    304 }
    305 
    306 int
    307 wsdisplay_addscreen(sc, idx, screentype, emul)
    308 	struct wsdisplay_softc *sc;
    309 	int idx;
    310 	char *screentype, *emul;
    311 {
    312 	const struct wsscreen_descr *scrdesc;
    313 	int error;
    314 	void *cookie;
    315 	int ccol, crow;
    316 	long defattr;
    317 	struct wsscreen *scr;
    318 
    319 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    320 		return (EINVAL);
    321 	if (sc->sc_scr[idx] != NULL)
    322 		return (EBUSY);
    323 
    324 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    325 	if (!scrdesc)
    326 		return (ENXIO);
    327 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    328 			scrdesc, &cookie, &ccol, &crow, &defattr);
    329 	if (error)
    330 		return (error);
    331 
    332 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    333 			      cookie, ccol, crow, defattr);
    334 	if (scr == NULL) {
    335 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    336 						 cookie);
    337 		return (ENXIO);
    338 	}
    339 
    340 	sc->sc_scr[idx] = scr;
    341 
    342 	printf("%s: screen %d added (%s", sc->sc_dv.dv_xname, idx,
    343 	       scrdesc->name);
    344 	if (WSSCREEN_HAS_EMULATOR(scr))
    345 		printf(", %s emulation", scr->scr_dconf->wsemul->name);
    346 	printf(")\n");
    347 	return (0);
    348 }
    349 
    350 int
    351 wsdisplay_delscreen(sc, idx, flags)
    352 	struct wsdisplay_softc *sc;
    353 	int idx, flags;
    354 {
    355 	struct wsscreen *scr;
    356 	int maj, mn, s;
    357 	void *cookie;
    358 
    359 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    360 		return (EINVAL);
    361 	scr = sc->sc_scr[idx];
    362 	if (!scr)
    363 		return (ENXIO);
    364 
    365 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    366 	    scr->scr_syncops ||
    367 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    368 		return(EBUSY);
    369 
    370 	/* locate the major number */
    371 	for (maj = 0; maj < nchrdev; maj++)
    372 		if (cdevsw[maj].d_open == wsdisplayopen)
    373 			break;
    374 
    375 	/* nuke the vnodes */
    376 	mn = WSDISPLAYMINOR(sc->sc_dv.dv_unit, idx);
    377 	vdevgone(maj, mn, mn, VCHR);
    378 
    379 	/*
    380 	 * delete pointers, so neither device entries
    381 	 * nor keyboard input can reference it anymore
    382 	 */
    383 	s = spltty();
    384 	if (sc->sc_focus == scr)
    385 		sc->sc_focus = 0;
    386 	sc->sc_scr[idx] = 0;
    387 	splx(s);
    388 
    389 	/*
    390 	 * Wake up processes waiting for the screen to
    391 	 * be activated. Sleepers must check whether
    392 	 * the screen still exists.
    393 	 */
    394 	if (scr->scr_flags & SCR_WAITACTIVE)
    395 		wakeup(scr);
    396 
    397 	/* save a reference to the graphics screen */
    398 	cookie = scr->scr_dconf->emulcookie;
    399 
    400 	wsscreen_detach(scr);
    401 
    402 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    403 					 cookie);
    404 
    405 	printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
    406 	return (0);
    407 }
    408 
    409 /*
    410  * Autoconfiguration functions.
    411  */
    412 int
    413 wsdisplay_emul_match(parent, match, aux)
    414 	struct device *parent;
    415 	struct cfdata *match;
    416 	void *aux;
    417 {
    418 	struct wsemuldisplaydev_attach_args *ap = aux;
    419 
    420 	if (match->wsemuldisplaydevcf_console !=
    421 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    422 		/*
    423 		 * If console-ness of device specified, either match
    424 		 * exactly (at high priority), or fail.
    425 		 */
    426 		if (match->wsemuldisplaydevcf_console != 0 &&
    427 		    ap->console != 0)
    428 			return (10);
    429 		else
    430 			return (0);
    431 	}
    432 
    433 	/* If console-ness unspecified, it wins. */
    434 	return (1);
    435 }
    436 
    437 void
    438 wsdisplay_emul_attach(parent, self, aux)
    439 	struct device *parent, *self;
    440 	void *aux;
    441 {
    442 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    443 	struct wsemuldisplaydev_attach_args *ap = aux;
    444 
    445 	wsdisplay_common_attach(sc, ap->console, ap->scrdata,
    446 				ap->accessops, ap->accesscookie);
    447 
    448 	if (ap->console) {
    449 		int maj;
    450 
    451 		/* locate the major number */
    452 		for (maj = 0; maj < nchrdev; maj++)
    453 			if (cdevsw[maj].d_open == wsdisplayopen)
    454 				break;
    455 
    456 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(self->dv_unit, 0));
    457 	}
    458 }
    459 
    460 /* Print function (for parent devices). */
    461 int
    462 wsemuldisplaydevprint(aux, pnp)
    463 	void *aux;
    464 	const char *pnp;
    465 {
    466 #if 0 /* -Wunused */
    467 	struct wsemuldisplaydev_attach_args *ap = aux;
    468 #endif
    469 
    470 	if (pnp)
    471 		printf("wsdisplay at %s", pnp);
    472 #if 0 /* don't bother; it's ugly */
    473 	printf(" console %d", ap->console);
    474 #endif
    475 
    476 	return (UNCONF);
    477 }
    478 
    479 int
    480 wsdisplay_noemul_match(parent, match, aux)
    481 	struct device *parent;
    482 	struct cfdata *match;
    483 	void *aux;
    484 {
    485 #if 0 /* -Wunused */
    486 	struct wsdisplaydev_attach_args *ap = aux;
    487 #endif
    488 
    489 	/* Always match. */
    490 	return (1);
    491 }
    492 
    493 void
    494 wsdisplay_noemul_attach(parent, self, aux)
    495 	struct device *parent, *self;
    496 	void *aux;
    497 {
    498 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    499 	struct wsdisplaydev_attach_args *ap = aux;
    500 
    501 	wsdisplay_common_attach(sc, 0, NULL, ap->accessops, ap->accesscookie);
    502 }
    503 
    504 /* Print function (for parent devices). */
    505 int
    506 wsdisplaydevprint(aux, pnp)
    507 	void *aux;
    508 	const char *pnp;
    509 {
    510 #if 0 /* -Wunused */
    511 	struct wsdisplaydev_attach_args *ap = aux;
    512 #endif
    513 
    514 	if (pnp)
    515 		printf("wsdisplay at %s", pnp);
    516 
    517 	return (UNCONF);
    518 }
    519 
    520 static void
    521 wsdisplay_common_attach(sc, console, scrdata, accessops, accesscookie)
    522 	struct wsdisplay_softc *sc;
    523 	int console;
    524 	const struct wsscreen_list *scrdata;
    525 	const struct wsdisplay_accessops *accessops;
    526 	void *accesscookie;
    527 {
    528 	int i = 0;
    529 
    530 	if (console) {
    531 		KASSERT(wsdisplay_console_initted);
    532 		KASSERT(wsdisplay_console_device == NULL);
    533 
    534 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    535 		wsdisplay_console_device = sc;
    536 
    537 		printf(": console (%s, %s emulation)",
    538 		       wsdisplay_console_conf.scrdata->name,
    539 		       wsdisplay_console_conf.wsemul->name);
    540 
    541 		sc->sc_focusidx = 0;
    542 		sc->sc_focus = sc->sc_scr[0];
    543 		i++;
    544 	}
    545 
    546 	printf("\n");
    547 
    548 	sc->sc_accessops = accessops;
    549 	sc->sc_accesscookie = accesscookie;
    550 	sc->sc_scrdata = scrdata;
    551 
    552 	sc->sc_isconsole = console;
    553 	sc->sc_kbddv = NULL;
    554 
    555 	wscons_glue_set_callback();
    556 
    557 	/*
    558 	 * Set up a number of virtual screens if possible. The
    559 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    560 	 * should go away as soon as we have a userspace utility.
    561 	 */
    562 #ifndef WSDISPLAY_DEFAULTSCREENS
    563 #define WSDISPLAY_DEFAULTSCREENS WSDISPLAY_MAXSCREEN
    564 #endif
    565 	for (; i < WSDISPLAY_DEFAULTSCREENS; i++)
    566 		if (wsdisplay_addscreen(sc, i, 0, 0))
    567 			break;
    568 }
    569 
    570 void
    571 wsdisplay_cnattach(type, cookie, ccol, crow, defattr)
    572 	const struct wsscreen_descr *type;
    573 	void *cookie;
    574 	int ccol, crow;
    575 	long defattr;
    576 {
    577 	const struct wsemul_ops *wsemul;
    578 
    579 	KASSERT(!wsdisplay_console_initted);
    580 	KASSERT(type->nrows > 0);
    581 	KASSERT(type->ncols > 0);
    582 	KASSERT(crow < type->nrows);
    583 	KASSERT(ccol < type->ncols);
    584 
    585 	wsdisplay_console_conf.emulops = type->textops;
    586 	wsdisplay_console_conf.emulcookie = cookie;
    587 	wsdisplay_console_conf.scrdata = type;
    588 
    589 	wsemul = wsemul_pick(0); /* default */
    590 	wsdisplay_console_conf.wsemul = wsemul;
    591 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    592 								  ccol, crow,
    593 								  defattr);
    594 
    595 	cn_tab = &wsdisplay_cons;
    596 
    597 	wsdisplay_console_initted = 1;
    598 }
    599 
    600 /*
    601  * Tty and cdevsw functions.
    602  */
    603 int
    604 wsdisplayopen(dev, flag, mode, p)
    605 	dev_t dev;
    606 	int flag, mode;
    607 	struct proc *p;
    608 {
    609 #if NWSDISPLAY > 0
    610 	struct wsdisplay_softc *sc;
    611 	struct tty *tp;
    612 	int unit, newopen, error;
    613 	struct wsscreen *scr;
    614 
    615 	unit = WSDISPLAYUNIT(dev);
    616 	if (unit >= wsdisplay_cd.cd_ndevs ||	/* make sure it was attached */
    617 	    (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
    618 		return (ENXIO);
    619 
    620 	if (ISWSDISPLAYCTL(dev))
    621 		return (0);
    622 
    623 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    624 		return (ENXIO);
    625 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    626 	if (!scr)
    627 		return (ENXIO);
    628 
    629 	if (WSSCREEN_HAS_TTY(scr)) {
    630 		tp = scr->scr_tty;
    631 		tp->t_oproc = wsdisplaystart;
    632 		tp->t_param = wsdisplayparam;
    633 		tp->t_dev = dev;
    634 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    635 		if (newopen) {
    636 			ttychars(tp);
    637 			tp->t_iflag = TTYDEF_IFLAG;
    638 			tp->t_oflag = TTYDEF_OFLAG;
    639 			tp->t_cflag = TTYDEF_CFLAG;
    640 			tp->t_lflag = TTYDEF_LFLAG;
    641 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    642 			wsdisplayparam(tp, &tp->t_termios);
    643 			ttsetwater(tp);
    644 		} else if ((tp->t_state & TS_XCLUDE) != 0 &&
    645 			   p->p_ucred->cr_uid != 0)
    646 			return EBUSY;
    647 		tp->t_state |= TS_CARR_ON;
    648 
    649 		error = ((*linesw[tp->t_line].l_open)(dev, tp));
    650 		if (error)
    651 			return (error);
    652 
    653 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    654 			/* set window sizes as appropriate, and reset
    655 			 the emulation */
    656 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    657 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    658 
    659 			/* wsdisplay_set_emulation() */
    660 		}
    661 	}
    662 
    663 	scr->scr_flags |= SCR_OPEN;
    664 	return (0);
    665 #else
    666 	return (ENXIO);
    667 #endif /* NWSDISPLAY > 0 */
    668 }
    669 
    670 int
    671 wsdisplayclose(dev, flag, mode, p)
    672 	dev_t dev;
    673 	int flag, mode;
    674 	struct proc *p;
    675 {
    676 #if NWSDISPLAY > 0
    677 	struct wsdisplay_softc *sc;
    678 	struct tty *tp;
    679 	int unit;
    680 	struct wsscreen *scr;
    681 
    682 	unit = WSDISPLAYUNIT(dev);
    683 	sc = wsdisplay_cd.cd_devs[unit];
    684 
    685 	if (ISWSDISPLAYCTL(dev))
    686 		return (0);
    687 
    688 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    689 
    690 	if (WSSCREEN_HAS_TTY(scr)) {
    691 		if (scr->scr_hold_screen) {
    692 			int s;
    693 
    694 			/* XXX RESET KEYBOARD LEDS, etc. */
    695 			s = spltty();	/* avoid conflict with keyboard */
    696 			wsdisplay_kbdholdscreen((struct device *)sc, 0);
    697 			splx(s);
    698 		}
    699 		tp = scr->scr_tty;
    700 		(*linesw[tp->t_line].l_close)(tp, flag);
    701 		ttyclose(tp);
    702 	}
    703 	/* XXX RESET EMULATOR? */
    704 
    705 	if (scr->scr_syncops)
    706 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    707 
    708 	if (WSSCREEN_HAS_EMULATOR(scr))
    709 		scr->scr_flags &= ~SCR_GRAPHICS;
    710 
    711 #ifdef WSDISPLAY_COMPAT_RAWKBD
    712 	if (scr->scr_rawkbd) {
    713 		int kbmode = WSKBD_TRANSLATED;
    714 		(void) wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    715 						(caddr_t)&kbmode, 0, p);
    716 	}
    717 #endif
    718 
    719 	scr->scr_flags &= ~SCR_OPEN;
    720 
    721 	return (0);
    722 #else
    723 	return (ENXIO);
    724 #endif /* NWSDISPLAY > 0 */
    725 }
    726 
    727 int
    728 wsdisplayread(dev, uio, flag)
    729 	dev_t dev;
    730 	struct uio *uio;
    731 	int flag;
    732 {
    733 #if NWSDISPLAY > 0
    734 	struct wsdisplay_softc *sc;
    735 	struct tty *tp;
    736 	int unit;
    737 	struct wsscreen *scr;
    738 
    739 	unit = WSDISPLAYUNIT(dev);
    740 	sc = wsdisplay_cd.cd_devs[unit];
    741 
    742 	if (ISWSDISPLAYCTL(dev))
    743 		return (0);
    744 
    745 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    746 
    747 	if (!WSSCREEN_HAS_TTY(scr))
    748 		return (ENODEV);
    749 
    750 	tp = scr->scr_tty;
    751 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    752 #else
    753 	return (ENXIO);
    754 #endif /* NWSDISPLAY > 0 */
    755 }
    756 
    757 int
    758 wsdisplaywrite(dev, uio, flag)
    759 	dev_t dev;
    760 	struct uio *uio;
    761 	int flag;
    762 {
    763 #if NWSDISPLAY > 0
    764 	struct wsdisplay_softc *sc;
    765 	struct tty *tp;
    766 	int unit;
    767 	struct wsscreen *scr;
    768 
    769 	unit = WSDISPLAYUNIT(dev);
    770 	sc = wsdisplay_cd.cd_devs[unit];
    771 
    772 	if (ISWSDISPLAYCTL(dev))
    773 		return (0);
    774 
    775 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    776 
    777 	if (!WSSCREEN_HAS_TTY(scr))
    778 		return (ENODEV);
    779 
    780 	tp = scr->scr_tty;
    781 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    782 #else
    783 	return (ENXIO);
    784 #endif /* NWSDISPLAY > 0 */
    785 }
    786 
    787 struct tty *
    788 wsdisplaytty(dev)
    789 	dev_t dev;
    790 {
    791 #if NWSDISPLAY > 0
    792 	struct wsdisplay_softc *sc;
    793 	int unit;
    794 	struct wsscreen *scr;
    795 
    796 	unit = WSDISPLAYUNIT(dev);
    797 	sc = wsdisplay_cd.cd_devs[unit];
    798 
    799 	if (ISWSDISPLAYCTL(dev))
    800 		panic("wsdisplaytty() on ctl device");
    801 
    802 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    803 
    804 	return (scr->scr_tty);
    805 #else
    806 	return (NULL);
    807 #endif /* NWSDISPLAY > 0 */
    808 }
    809 
    810 int
    811 wsdisplayioctl(dev, cmd, data, flag, p)
    812 	dev_t dev;
    813 	u_long cmd;
    814 	caddr_t data;
    815 	int flag;
    816 	struct proc *p;
    817 {
    818 #if NWSDISPLAY > 0
    819 	struct wsdisplay_softc *sc;
    820 	struct tty *tp;
    821 	int unit, error;
    822 	struct wsscreen *scr;
    823 
    824 	unit = WSDISPLAYUNIT(dev);
    825 	sc = wsdisplay_cd.cd_devs[unit];
    826 
    827 	if (ISWSDISPLAYCTL(dev))
    828 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, p));
    829 
    830 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    831 
    832 	if (WSSCREEN_HAS_TTY(scr)) {
    833 		tp = scr->scr_tty;
    834 
    835 /* printf("disc\n"); */
    836 		/* do the line discipline ioctls first */
    837 		error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    838 		if (error >= 0)
    839 			return error;
    840 
    841 /* printf("tty\n"); */
    842 		/* then the tty ioctls */
    843 		error = ttioctl(tp, cmd, data, flag, p);
    844 		if (error >= 0)
    845 			return error;
    846 	}
    847 
    848 #ifdef WSDISPLAY_COMPAT_USL
    849 	error = wsdisplay_usl_ioctl(sc, scr, cmd, data, flag, p);
    850 	if (error >= 0)
    851 		return (error);
    852 #endif
    853 
    854 	error = wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p);
    855 	return (error != -1 ? error : ENOTTY);
    856 #else
    857 	return (ENXIO);
    858 #endif /* NWSDISPLAY > 0 */
    859 }
    860 
    861 int
    862 wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p)
    863 	struct wsdisplay_softc *sc;
    864 	struct wsscreen *scr;
    865 	u_long cmd;
    866 	caddr_t data;
    867 	int flag;
    868 	struct proc *p;
    869 {
    870 	int error;
    871 	void *buf;
    872 
    873 	if (sc->sc_kbddv != NULL) {
    874 		/* check ioctls for keyboard */
    875 #ifdef WSDISPLAY_COMPAT_RAWKBD
    876 		switch (cmd) {
    877 		    case WSKBDIO_SETMODE:
    878 			scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
    879 			return (wsdisplay_update_rawkbd(sc, scr));
    880 		    case WSKBDIO_GETMODE:
    881 			*(int *)data = (scr->scr_rawkbd ?
    882 					WSKBD_RAW : WSKBD_TRANSLATED);
    883 			return (0);
    884 		}
    885 #endif
    886 /* printf("kbdcallback\n"); */
    887 		error = wskbd_displayioctl(sc->sc_kbddv, cmd, data, flag, p);
    888 		if (error >= 0)
    889 			return error;
    890 	}
    891 
    892 /* printf("display\n"); */
    893 	switch (cmd) {
    894 	case WSDISPLAYIO_GMODE:
    895 		*(u_int *)data = (scr->scr_flags & SCR_GRAPHICS ?
    896 				  WSDISPLAYIO_MODE_MAPPED :
    897 				  WSDISPLAYIO_MODE_EMUL);
    898 		return (0);
    899 
    900 	case WSDISPLAYIO_SMODE:
    901 #define d (*(int *)data)
    902 		if (d != WSDISPLAYIO_MODE_EMUL &&
    903 		    d != WSDISPLAYIO_MODE_MAPPED)
    904 			return (EINVAL);
    905 
    906 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
    907 		    scr->scr_flags &= ~SCR_GRAPHICS;
    908 		    if (d == WSDISPLAYIO_MODE_MAPPED)
    909 			    scr->scr_flags |= SCR_GRAPHICS;
    910 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
    911 		    return (EINVAL);
    912 	    return (0);
    913 #undef d
    914 	case WSDISPLAYIO_SFONT:
    915 #define d ((struct wsdisplay_font *)data)
    916 		if (d->fontheight != scr->scr_dconf->scrdata->fontheight ||
    917 		    d->fontwidth != scr->scr_dconf->scrdata->fontwidth)
    918 			return (EINVAL);
    919 		buf = malloc(d->fontheight * d->stride * d->numchars,
    920 			     M_DEVBUF, M_WAITOK);
    921 		error = copyin(d->data, buf,
    922 			       d->fontheight * d->stride * d->numchars);
    923 		if (error) {
    924 			free(buf, M_DEVBUF);
    925 			return (error);
    926 		}
    927 		error =
    928 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
    929 						 scr->scr_dconf->emulcookie,
    930 						 d->firstchar, d->numchars,
    931 						 d->stride, buf);
    932 		free(buf, M_DEVBUF);
    933 #undef d
    934 		return (error);
    935 	}
    936 
    937 	/* check ioctls for display */
    938 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
    939 	    flag, p));
    940 }
    941 
    942 int
    943 wsdisplay_cfg_ioctl(sc, cmd, data, flag, p)
    944 	struct wsdisplay_softc *sc;
    945 	u_long cmd;
    946 	caddr_t data;
    947 	int flag;
    948 	struct proc *p;
    949 {
    950 	int error;
    951 	char *type, typebuf[16], *emul, emulbuf[16];
    952 
    953 	switch (cmd) {
    954 	case WSDISPLAYIO_ADDSCREEN:
    955 #define d ((struct wsdisplay_addscreendata *)data)
    956 		if (d->screentype) {
    957 			error = copyinstr(d->screentype, typebuf,
    958 					  sizeof(typebuf), 0);
    959 			if (error)
    960 				return (error);
    961 			type = typebuf;
    962 		} else
    963 			type = 0;
    964 		if (d->emul) {
    965 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf), 0);
    966 			if (error)
    967 				return (error);
    968 			emul = emulbuf;
    969 		} else
    970 			emul = 0;
    971 
    972 		return (wsdisplay_addscreen(sc, d->idx, type, emul));
    973 #undef d
    974 	case WSDISPLAYIO_DELSCREEN:
    975 #define d ((struct wsdisplay_delscreendata *)data)
    976 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
    977 #undef d
    978 	}
    979 	return (EINVAL);
    980 }
    981 
    982 int
    983 wsdisplaymmap(dev, offset, prot)
    984 	dev_t dev;
    985 	int offset;		/* XXX */
    986 	int prot;
    987 {
    988 #if NWSDISPLAY > 0
    989 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
    990 	struct wsscreen *scr;
    991 
    992 	if (ISWSDISPLAYCTL(dev))
    993 		return (-1);
    994 
    995 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    996 
    997 	if (!(scr->scr_flags & SCR_GRAPHICS))
    998 		return (-1);
    999 
   1000 	/* pass mmap to display */
   1001 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
   1002 #else
   1003 	return (-1);
   1004 #endif /* NWSDISPLAY > 0 */
   1005 }
   1006 
   1007 int
   1008 wsdisplaypoll(dev, events, p)
   1009 	dev_t dev;
   1010 	int events;
   1011 	struct proc *p;
   1012 {
   1013 #if NWSDISPLAY > 0
   1014 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
   1015 	struct wsscreen *scr;
   1016 
   1017 	if (ISWSDISPLAYCTL(dev))
   1018 		return (0);
   1019 
   1020 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
   1021 
   1022 	if (WSSCREEN_HAS_TTY(scr))
   1023 		return (ttpoll(dev, events, p));
   1024 	else
   1025 		return (0);
   1026 #else
   1027 	return (0);
   1028 #endif /* NWSDISPLAY > 0 */
   1029 }
   1030 
   1031 #if NWSDISPLAY > 0
   1032 void
   1033 wsdisplaystart(tp)
   1034 	register struct tty *tp;
   1035 {
   1036 	struct wsdisplay_softc *sc;
   1037 	struct wsscreen *scr;
   1038 	register int s, n;
   1039 	u_char buf[WSDISPLAYBURST];
   1040 
   1041 	s = spltty();
   1042 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1043 		splx(s);
   1044 		return;
   1045 	}
   1046 	sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(tp->t_dev)];
   1047 	scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)];
   1048 	if (scr->scr_hold_screen) {
   1049 		tp->t_state |= TS_TIMEOUT;
   1050 		splx(s);
   1051 		return;
   1052 	}
   1053 	tp->t_state |= TS_BUSY;
   1054 	splx(s);
   1055 
   1056 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
   1057 
   1058 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1059 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1060 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1061 		    buf, n, 0);
   1062 	}
   1063 
   1064 	s = spltty();
   1065 	tp->t_state &= ~TS_BUSY;
   1066 	/* Come back if there's more to do */
   1067 	if (tp->t_outq.c_cc) {
   1068 		tp->t_state |= TS_TIMEOUT;
   1069 		timeout(ttrstrt, tp, (hz > 128) ? (hz / 128) : 1);
   1070 	}
   1071 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1072 		if (tp->t_state&TS_ASLEEP) {
   1073 			tp->t_state &= ~TS_ASLEEP;
   1074 			wakeup((caddr_t)&tp->t_outq);
   1075 		}
   1076 		selwakeup(&tp->t_wsel);
   1077 	}
   1078 	splx(s);
   1079 }
   1080 #endif /* NWSDISPLAY > 0 */
   1081 
   1082 void
   1083 wsdisplaystop(tp, flag)
   1084 	struct tty *tp;
   1085 	int flag;
   1086 {
   1087 	int s;
   1088 
   1089 	s = spltty();
   1090 	if (ISSET(tp->t_state, TS_BUSY))
   1091 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1092 			SET(tp->t_state, TS_FLUSH);
   1093 	splx(s);
   1094 }
   1095 
   1096 #if NWSDISPLAY > 0
   1097 /* Set line parameters. */
   1098 int
   1099 wsdisplayparam(tp, t)
   1100 	struct tty *tp;
   1101 	struct termios *t;
   1102 {
   1103 
   1104 	tp->t_ispeed = t->c_ispeed;
   1105 	tp->t_ospeed = t->c_ospeed;
   1106 	tp->t_cflag = t->c_cflag;
   1107 	return 0;
   1108 }
   1109 #endif /* NWSDISPLAY > 0 */
   1110 
   1111 /*
   1112  * Callbacks for the emulation code.
   1113  */
   1114 void
   1115 wsdisplay_emulbell(v)
   1116 	void *v;
   1117 {
   1118 	struct wsscreen *scr = v;
   1119 
   1120 	if (scr == NULL)		/* console, before real attach */
   1121 		return;
   1122 
   1123 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1124 		return;
   1125 
   1126 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1127 					FWRITE, NULL);
   1128 }
   1129 
   1130 void
   1131 wsdisplay_emulinput(v, data, count)
   1132 	void *v;
   1133 	const u_char *data;
   1134 	u_int count;
   1135 {
   1136 	struct wsscreen *scr = v;
   1137 	struct tty *tp;
   1138 
   1139 	if (v == NULL)			/* console, before real attach */
   1140 		return;
   1141 
   1142 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1143 		return;
   1144 	if (!WSSCREEN_HAS_TTY(scr))
   1145 		return;
   1146 
   1147 	tp = scr->scr_tty;
   1148 	while (count-- > 0)
   1149 		(*linesw[tp->t_line].l_rint)(*data++, tp);
   1150 };
   1151 
   1152 /*
   1153  * Calls from the keyboard interface.
   1154  */
   1155 void
   1156 wsdisplay_kbdinput(dev, ks)
   1157 	struct device *dev;
   1158 	keysym_t ks;
   1159 {
   1160 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1161 	struct wsscreen *scr;
   1162 	char *dp;
   1163 	int count;
   1164 	struct tty *tp;
   1165 
   1166 	KASSERT(sc != NULL);
   1167 
   1168 	scr = sc->sc_focus;
   1169 
   1170 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1171 		return;
   1172 
   1173 	tp = scr->scr_tty;
   1174 
   1175 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1176 		(*linesw[tp->t_line].l_rint)(KS_VALUE(ks), tp);
   1177 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1178 		count = (*scr->scr_dconf->wsemul->translate)
   1179 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1180 		while (count-- > 0)
   1181 			(*linesw[tp->t_line].l_rint)(*dp++, tp);
   1182 	}
   1183 }
   1184 
   1185 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1186 int
   1187 wsdisplay_update_rawkbd(sc, scr)
   1188 	struct wsdisplay_softc *sc;
   1189 	struct wsscreen *scr;
   1190 {
   1191 	int s, data, error;
   1192 	s = spltty();
   1193 
   1194 	if (!sc->sc_kbddv ||
   1195 	    scr != sc->sc_focus ||
   1196 	    sc->sc_rawkbd == scr->scr_rawkbd) {
   1197 		splx(s);
   1198 		return (0);
   1199 	}
   1200 
   1201 	data = (scr->scr_rawkbd ? WSKBD_RAW : WSKBD_TRANSLATED);
   1202 	error = wskbd_displayioctl(sc->sc_kbddv, WSKBDIO_SETMODE,
   1203 				   (caddr_t)&data, 0, 0);
   1204 	if (!error)
   1205 		sc->sc_rawkbd = scr->scr_rawkbd;
   1206 	splx(s);
   1207 	return (error);
   1208 }
   1209 #endif
   1210 
   1211 int
   1212 wsdisplay_switch1(arg, waitok)
   1213 	void *arg;
   1214 	int waitok;
   1215 {
   1216 	struct wsdisplay_softc *sc = arg;
   1217 	int no;
   1218 	struct wsscreen *scr;
   1219 
   1220 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1221 		printf("wsdisplay_switchto: not switching\n");
   1222 		return (EINVAL);
   1223 	}
   1224 
   1225 	no = sc->sc_screenwanted;
   1226 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1227 		panic("wsdisplay_switch1: invalid screen %d", no);
   1228 	scr = sc->sc_scr[no];
   1229 	if (!scr) {
   1230 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1231 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1232 		return (ENXIO);
   1233 	}
   1234 
   1235 	(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1236 					 scr->scr_dconf->emulcookie);
   1237 	sc->sc_focusidx = no;
   1238 	sc->sc_focus = scr;
   1239 
   1240 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1241 	(void) wsdisplay_update_rawkbd(sc, scr);
   1242 #endif
   1243 	/* keyboard map??? */
   1244 
   1245 	if (scr->scr_syncops) {
   1246 		(*scr->scr_syncops->attach)(scr->scr_synccookie, waitok);
   1247 		/* XXX error handling */
   1248 	}
   1249 
   1250 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1251 
   1252 	if (scr->scr_flags & SCR_WAITACTIVE)
   1253 		wakeup(scr);
   1254 	return (0);
   1255 }
   1256 
   1257 int
   1258 wsdisplay_switch(dev, no, waitok)
   1259 	struct device *dev;
   1260 	int no, waitok;
   1261 {
   1262 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1263 	int s, res = 0;
   1264 	struct wsscreen *scr;
   1265 
   1266 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN || !sc->sc_scr[no])
   1267 		return (ENXIO);
   1268 
   1269 	s = spltty();
   1270 
   1271 	if (no == sc->sc_focusidx) {
   1272 		splx(s);
   1273 		return (0);
   1274 	}
   1275 
   1276 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1277 		splx(s);
   1278 		return (EBUSY);
   1279 	}
   1280 
   1281 	sc->sc_flags |= SC_SWITCHPENDING;
   1282 	sc->sc_screenwanted = no;
   1283 
   1284 	splx(s);
   1285 
   1286 	scr = sc->sc_focus;
   1287 	if (!scr)
   1288 		return (wsdisplay_switch1(sc, waitok));
   1289 
   1290 #define wsswitch_callback ((void (*) __P((void *, int)))wsdisplay_switch1)
   1291 	if (scr->scr_syncops) {
   1292 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1293 						  wsswitch_callback, sc);
   1294 		if (res == EAGAIN) {
   1295 			/* switch will be done asynchronously */
   1296 			return (0);
   1297 		}
   1298 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1299 		/* no way to save state */
   1300 		res = EBUSY;
   1301 	}
   1302 
   1303 	if (res) {
   1304 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1305 		return (res);
   1306 	} else
   1307 		return (wsdisplay_switch1(sc, waitok));
   1308 }
   1309 
   1310 /*
   1311  * Interface for (external) VT switch / process synchronization code
   1312  */
   1313 int
   1314 wsscreen_attach_sync(scr, ops, cookie)
   1315 	struct wsscreen *scr;
   1316 	const struct wscons_syncops *ops;
   1317 	void *cookie;
   1318 {
   1319 	if (scr->scr_syncops) {
   1320 		/*
   1321 		 * The screen is already claimed.
   1322 		 * Check if the owner is still alive.
   1323 		 */
   1324 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1325 			return (EBUSY);
   1326 	}
   1327 	scr->scr_syncops = ops;
   1328 	scr->scr_synccookie = cookie;
   1329 	return (0);
   1330 }
   1331 
   1332 int
   1333 wsscreen_detach_sync(scr)
   1334 	struct wsscreen *scr;
   1335 {
   1336 	if (!scr->scr_syncops)
   1337 		return (EINVAL);
   1338 	scr->scr_syncops = 0;
   1339 	return (0);
   1340 }
   1341 
   1342 int
   1343 wsscreen_lookup_sync(scr, ops, cookiep)
   1344 	struct wsscreen *scr;
   1345 	const struct wscons_syncops *ops; /* used as ID */
   1346 	void **cookiep;
   1347 {
   1348 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1349 		return (EINVAL);
   1350 	*cookiep = scr->scr_synccookie;
   1351 	return (0);
   1352 }
   1353 
   1354 /*
   1355  * Interface to virtual screen stuff
   1356  */
   1357 int
   1358 wsdisplay_maxscreenidx(sc)
   1359 	struct wsdisplay_softc *sc;
   1360 {
   1361 	return (WSDISPLAY_MAXSCREEN - 1);
   1362 }
   1363 
   1364 int
   1365 wsdisplay_screenstate(sc, idx)
   1366 	struct wsdisplay_softc *sc;
   1367 	int idx;
   1368 {
   1369 	if (idx >= WSDISPLAY_MAXSCREEN)
   1370 		return (EINVAL);
   1371 	if (!sc->sc_scr[idx])
   1372 		return (ENXIO);
   1373 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1374 }
   1375 
   1376 int
   1377 wsdisplay_getactivescreen(sc)
   1378 	struct wsdisplay_softc *sc;
   1379 {
   1380 	return (sc->sc_focusidx);
   1381 }
   1382 
   1383 int
   1384 wsscreen_switchwait(sc, no)
   1385 	struct wsdisplay_softc *sc;
   1386 	int no;
   1387 {
   1388 	struct wsscreen *scr;
   1389 	int s, res = 0;
   1390 
   1391 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1392 		return (ENXIO);
   1393 	scr = sc->sc_scr[no];
   1394 	if (!scr)
   1395 		return (ENXIO);
   1396 
   1397 	s = spltty();
   1398 	if (scr != sc->sc_focus) {
   1399 		scr->scr_flags |= SCR_WAITACTIVE;
   1400 		res = tsleep(scr, PCATCH, "wswait", 0);
   1401 		if (scr != sc->sc_scr[no])
   1402 			res = ENXIO; /* disappeared in the meantime */
   1403 		else
   1404 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1405 	}
   1406 	splx(s);
   1407 	return (res);
   1408 }
   1409 
   1410 void
   1411 wsdisplay_kbdholdscreen(dev, hold)
   1412 	struct device *dev;
   1413 	int hold;
   1414 {
   1415 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1416 	struct wsscreen *scr;
   1417 
   1418 	scr = sc->sc_focus;
   1419 
   1420 	if (hold)
   1421 		scr->scr_hold_screen = 1;
   1422 	else {
   1423 		scr->scr_hold_screen = 0;
   1424 		timeout(ttrstrt, scr->scr_tty, 0);	/* "immediate" */
   1425 	}
   1426 }
   1427 
   1428 /*
   1429  * Calls from the glue code.
   1430  */
   1431 int
   1432 wsdisplay_is_console(dv)
   1433 	struct device *dv;
   1434 {
   1435 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1436 
   1437 	KASSERT(sc != NULL);
   1438 	return (sc->sc_isconsole);
   1439 }
   1440 
   1441 int
   1442 wsdisplay_has_emulator(dv)
   1443 	struct device *dv;
   1444 {
   1445 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1446 	struct wsscreen *scr;
   1447 
   1448 	scr = sc->sc_focus; /* ??? */
   1449 
   1450 	KASSERT(sc != NULL);
   1451 	KASSERT(scr != NULL);
   1452 	return (WSSCREEN_HAS_EMULATOR(scr)); /* XXX XXX */
   1453 }
   1454 
   1455 struct device *
   1456 wsdisplay_kbd(dv)
   1457 	struct device *dv;
   1458 {
   1459 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1460 
   1461 	KASSERT(sc != NULL);
   1462 	return (sc->sc_kbddv);
   1463 }
   1464 
   1465 void
   1466 wsdisplay_set_kbd(dv, kbddv)
   1467 	struct device *dv, *kbddv;
   1468 {
   1469 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1470 
   1471 	KASSERT(sc != NULL);
   1472 	if (sc->sc_kbddv) {
   1473 		/* disable old keyboard */
   1474 		wskbd_enable(sc->sc_kbddv, 0);
   1475 	}
   1476 	if (kbddv) {
   1477 		/* enable new keyboard */
   1478 		wskbd_enable(kbddv, 1);
   1479 	}
   1480 	sc->sc_kbddv = kbddv;
   1481 }
   1482 
   1483 /*
   1484  * Console interface.
   1485  */
   1486 void
   1487 wsdisplay_cnputc(dev, i)
   1488 	dev_t dev;
   1489 	int i;
   1490 {
   1491 	struct wsscreen_internal *dc;
   1492 	char c = i;
   1493 
   1494 	if (!wsdisplay_console_initted)
   1495 		return;
   1496 
   1497 	if (wsdisplay_console_device != NULL &&
   1498 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1499 		return;
   1500 
   1501 	dc = &wsdisplay_console_conf;
   1502 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1503 }
   1504 
   1505 static int
   1506 wsdisplay_getc_dummy(dev)
   1507 	dev_t dev;
   1508 {
   1509 	/* panic? */
   1510 	return (0);
   1511 }
   1512 
   1513 static void
   1514 wsdisplay_pollc_dummy(dev, on)
   1515 	dev_t dev;
   1516 	int on;
   1517 {
   1518 }
   1519 
   1520 void
   1521 wsdisplay_set_cons_kbd(get, poll)
   1522 	int (*get) __P((dev_t));
   1523 	void (*poll) __P((dev_t, int));
   1524 {
   1525 	wsdisplay_cons.cn_getc = get;
   1526 	wsdisplay_cons.cn_pollc = poll;
   1527 }
   1528