Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.90.2.2
      1 /* $NetBSD: wsdisplay.c,v 1.90.2.2 2006/03/10 14:39:03 elad Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: wsdisplay.c,v 1.90.2.2 2006/03/10 14:39:03 elad Exp $");
     35 
     36 #include "opt_wsdisplay_border.h"
     37 #include "opt_wsdisplay_compat.h"
     38 #include "opt_wsmsgattrs.h"
     39 #include "opt_compat_netbsd.h"
     40 #include "wskbd.h"
     41 #include "wsmux.h"
     42 #include "wsdisplay.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/conf.h>
     46 #include <sys/device.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/poll.h>
     49 #include <sys/kernel.h>
     50 #include <sys/proc.h>
     51 #include <sys/malloc.h>
     52 #include <sys/syslog.h>
     53 #include <sys/systm.h>
     54 #include <sys/tty.h>
     55 #include <sys/signalvar.h>
     56 #include <sys/errno.h>
     57 #include <sys/fcntl.h>
     58 #include <sys/vnode.h>
     59 
     60 #include <dev/wscons/wseventvar.h>
     61 #include <dev/wscons/wsmuxvar.h>
     62 #include <dev/wscons/wsconsio.h>
     63 #include <dev/wscons/wsdisplayvar.h>
     64 #include <dev/wscons/wsksymvar.h>
     65 #include <dev/wscons/wsksymdef.h>
     66 #include <dev/wscons/wsemulvar.h>
     67 #include <dev/wscons/wscons_callbacks.h>
     68 #include <dev/cons.h>
     69 
     70 struct wsscreen_internal {
     71 	const struct wsdisplay_emulops *emulops;
     72 	void	*emulcookie;
     73 
     74 	const struct wsscreen_descr *scrdata;
     75 
     76 	const struct wsemul_ops *wsemul;
     77 	void	*wsemulcookie;
     78 };
     79 
     80 struct wsscreen {
     81 	struct wsscreen_internal *scr_dconf;
     82 
     83 	struct tty *scr_tty;
     84 	int	scr_hold_screen;		/* hold tty output */
     85 
     86 	int scr_flags;
     87 #define SCR_OPEN 1		/* is it open? */
     88 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     89 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     90 #define	SCR_DUMBFB 8		/* in use as a dumb fb (iff SCR_GRAPHICS) */
     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(struct wsdisplay_softc *, int,
    102 				 const char *,
    103 				 const struct wsscreen_descr *, void *,
    104 				 int, int, long);
    105 void wsscreen_detach(struct wsscreen *);
    106 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
    107 static void wsdisplay_shutdownhook(void *);
    108 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
    109 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
    110 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
    111 
    112 #define WSDISPLAY_MAXSCREEN 8
    113 
    114 struct wsdisplay_softc {
    115 	struct device sc_dv;
    116 
    117 	const struct wsdisplay_accessops *sc_accessops;
    118 	void	*sc_accesscookie;
    119 
    120 	const struct wsscreen_list *sc_scrdata;
    121 #ifdef WSDISPLAY_SCROLLSUPPORT
    122 	struct wsdisplay_scroll_data sc_scroll_values;
    123 #endif
    124 
    125 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    126 	int sc_focusidx;	/* available only if sc_focus isn't null */
    127 	struct wsscreen *sc_focus;
    128 
    129 	struct wseventvar evar;
    130 
    131 	int	sc_isconsole;
    132 
    133 	int sc_flags;
    134 #define SC_SWITCHPENDING 1
    135 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
    136 
    137 #if NWSKBD > 0
    138 	struct wsevsrc *sc_input;
    139 #ifdef WSDISPLAY_COMPAT_RAWKBD
    140 	int sc_rawkbd;
    141 #endif
    142 #endif /* NWSKBD > 0 */
    143 };
    144 
    145 #ifdef WSDISPLAY_SCROLLSUPPORT
    146 
    147 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
    148 	WSDISPLAY_SCROLL_DOALL,
    149 	25,
    150 	2,
    151 };
    152 #endif
    153 
    154 extern struct cfdriver wsdisplay_cd;
    155 
    156 /* Autoconfiguration definitions. */
    157 static int wsdisplay_emul_match(struct device *, struct cfdata *, void *);
    158 static void wsdisplay_emul_attach(struct device *, struct device *, void *);
    159 static int wsdisplay_noemul_match(struct device *, struct cfdata *, void *);
    160 static void wsdisplay_noemul_attach(struct device *, struct device *, void *);
    161 
    162 CFATTACH_DECL(wsdisplay_emul, sizeof (struct wsdisplay_softc),
    163     wsdisplay_emul_match, wsdisplay_emul_attach, NULL, NULL);
    164 
    165 CFATTACH_DECL(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
    166     wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
    167 
    168 dev_type_open(wsdisplayopen);
    169 dev_type_close(wsdisplayclose);
    170 dev_type_read(wsdisplayread);
    171 dev_type_write(wsdisplaywrite);
    172 dev_type_ioctl(wsdisplayioctl);
    173 dev_type_stop(wsdisplaystop);
    174 dev_type_tty(wsdisplaytty);
    175 dev_type_poll(wsdisplaypoll);
    176 dev_type_mmap(wsdisplaymmap);
    177 dev_type_kqfilter(wsdisplaykqfilter);
    178 
    179 const struct cdevsw wsdisplay_cdevsw = {
    180 	wsdisplayopen, wsdisplayclose, wsdisplayread, wsdisplaywrite,
    181 	wsdisplayioctl, wsdisplaystop, wsdisplaytty, wsdisplaypoll,
    182 	wsdisplaymmap, wsdisplaykqfilter, D_TTY
    183 };
    184 
    185 static void wsdisplaystart(struct tty *);
    186 static int wsdisplayparam(struct tty *, struct termios *);
    187 
    188 
    189 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    190 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    191 #define ISWSDISPLAYSTAT(dev)	(WSDISPLAYSCREEN(dev) == 254)
    192 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    193 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    194 
    195 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    196 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    197 
    198 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
    199 	    int console, int kbdmux, const struct wsscreen_list *,
    200 	    const struct wsdisplay_accessops *accessops,
    201 	    void *accesscookie);
    202 
    203 #ifdef WSDISPLAY_COMPAT_RAWKBD
    204 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
    205 				 struct wsscreen *);
    206 #endif
    207 
    208 static int wsdisplay_console_initted;
    209 static int wsdisplay_console_attached;
    210 static struct wsdisplay_softc *wsdisplay_console_device;
    211 static struct wsscreen_internal wsdisplay_console_conf;
    212 
    213 static int wsdisplay_getc_dummy(dev_t);
    214 static void wsdisplay_pollc(dev_t, int);
    215 
    216 static int wsdisplay_cons_pollmode;
    217 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
    218 
    219 static struct consdev wsdisplay_cons = {
    220 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
    221 	wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
    222 };
    223 
    224 #ifndef WSDISPLAY_DEFAULTSCREENS
    225 # define WSDISPLAY_DEFAULTSCREENS	0
    226 #endif
    227 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
    228 
    229 int wsdisplay_switch1(void *, int, int);
    230 int wsdisplay_switch2(void *, int, int);
    231 int wsdisplay_switch3(void *, int, int);
    232 
    233 int wsdisplay_clearonclose;
    234 
    235 struct wsscreen *
    236 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
    237 	const struct wsscreen_descr *type, void *cookie, int ccol,
    238 	int crow, long defattr)
    239 {
    240 	struct wsscreen_internal *dconf;
    241 	struct wsscreen *scr;
    242 
    243 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    244 	if (!scr)
    245 		return (NULL);
    246 
    247 	if (console) {
    248 		dconf = &wsdisplay_console_conf;
    249 		/*
    250 		 * If there's an emulation, tell it about the callback argument.
    251 		 * The other stuff is already there.
    252 		 */
    253 		if (dconf->wsemul != NULL)
    254 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    255 	} else { /* not console */
    256 		dconf = malloc(sizeof(struct wsscreen_internal),
    257 			       M_DEVBUF, M_NOWAIT);
    258 		dconf->emulops = type->textops;
    259 		dconf->emulcookie = cookie;
    260 		if (dconf->emulops) {
    261 			dconf->wsemul = wsemul_pick(emul);
    262 			if (dconf->wsemul == NULL) {
    263 				free(dconf, M_DEVBUF);
    264 				free(scr, M_DEVBUF);
    265 				return (NULL);
    266 			}
    267 			dconf->wsemulcookie =
    268 			  (*dconf->wsemul->attach)(0, type, cookie,
    269 						   ccol, crow, scr, defattr);
    270 		} else
    271 			dconf->wsemul = NULL;
    272 		dconf->scrdata = type;
    273 	}
    274 
    275 	scr->scr_dconf = dconf;
    276 
    277 	scr->scr_tty = ttymalloc();
    278 	tty_attach(scr->scr_tty);
    279 	scr->scr_hold_screen = 0;
    280 	if (WSSCREEN_HAS_EMULATOR(scr))
    281 		scr->scr_flags = 0;
    282 	else
    283 		scr->scr_flags = SCR_GRAPHICS;
    284 
    285 	scr->scr_syncops = 0;
    286 	scr->sc = sc;
    287 #ifdef WSDISPLAY_COMPAT_RAWKBD
    288 	scr->scr_rawkbd = 0;
    289 #endif
    290 	return (scr);
    291 }
    292 
    293 void
    294 wsscreen_detach(struct wsscreen *scr)
    295 {
    296 	u_int ccol, crow; /* XXX */
    297 
    298 	if (WSSCREEN_HAS_TTY(scr)) {
    299 		tty_detach(scr->scr_tty);
    300 		ttyfree(scr->scr_tty);
    301 	}
    302 	if (WSSCREEN_HAS_EMULATOR(scr))
    303 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    304 						  &ccol, &crow);
    305 	free(scr->scr_dconf, M_DEVBUF);
    306 	free(scr, M_DEVBUF);
    307 }
    308 
    309 const struct wsscreen_descr *
    310 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
    311 {
    312 	int i;
    313 	const struct wsscreen_descr *scr;
    314 
    315 	KASSERT(scrdata->nscreens > 0);
    316 
    317 	if (name == NULL)
    318 		return (scrdata->screens[0]);
    319 
    320 	for (i = 0; i < scrdata->nscreens; i++) {
    321 		scr = scrdata->screens[i];
    322 		if (!strcmp(name, scr->name))
    323 			return (scr);
    324 	}
    325 
    326 	return (0);
    327 }
    328 
    329 /*
    330  * print info about attached screen
    331  */
    332 static void
    333 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
    334 {
    335 	printf("%s: screen %d", sc->sc_dv.dv_xname, idx);
    336 	if (count > 1)
    337 		printf("-%d", idx + (count-1));
    338 	printf(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
    339 	if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
    340 		printf(", %s emulation",
    341 			sc->sc_scr[idx]->scr_dconf->wsemul->name);
    342 	}
    343 	printf(")\n");
    344 }
    345 
    346 int
    347 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
    348 	const char *screentype, const char *emul)
    349 {
    350 	const struct wsscreen_descr *scrdesc;
    351 	int error;
    352 	void *cookie;
    353 	int ccol, crow;
    354 	long defattr;
    355 	struct wsscreen *scr;
    356 	int s;
    357 
    358 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    359 		return (EINVAL);
    360 	if (sc->sc_scr[idx] != NULL)
    361 		return (EBUSY);
    362 
    363 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    364 	if (!scrdesc)
    365 		return (ENXIO);
    366 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    367 			scrdesc, &cookie, &ccol, &crow, &defattr);
    368 	if (error)
    369 		return (error);
    370 
    371 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    372 			      cookie, ccol, crow, defattr);
    373 	if (scr == NULL) {
    374 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    375 						 cookie);
    376 		return (ENXIO);
    377 	}
    378 
    379 	sc->sc_scr[idx] = scr;
    380 
    381 	/* if no screen has focus yet, activate the first we get */
    382 	s = spltty();
    383 	if (!sc->sc_focus) {
    384 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
    385 						 scr->scr_dconf->emulcookie,
    386 						 0, 0, 0);
    387 		sc->sc_focusidx = idx;
    388 		sc->sc_focus = scr;
    389 	}
    390 	splx(s);
    391 	return (0);
    392 }
    393 
    394 static void
    395 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
    396 {
    397 	int maj, mn, idx;
    398 
    399 	/* hangup */
    400 	if (WSSCREEN_HAS_TTY(scr)) {
    401 		struct tty *tp = scr->scr_tty;
    402 		(*tp->t_linesw->l_modem)(tp, 0);
    403 	}
    404 
    405 	/* locate the major number */
    406 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    407 	/* locate the screen index */
    408 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
    409 		if (scr == sc->sc_scr[idx])
    410 			break;
    411 #ifdef DIAGNOSTIC
    412 	if (idx == WSDISPLAY_MAXSCREEN)
    413 		panic("wsdisplay_forceclose: bad screen");
    414 #endif
    415 
    416 	/* nuke the vnodes */
    417 	mn = WSDISPLAYMINOR(sc->sc_dv.dv_unit, idx);
    418 	vdevgone(maj, mn, mn, VCHR);
    419 }
    420 
    421 #ifdef WSDISPLAY_SCROLLSUPPORT
    422 void
    423 wsdisplay_scroll(void *arg, int op)
    424 {
    425 	struct wsdisplay_softc *sc = arg;
    426 	int lines;
    427 
    428 	if (op == WSDISPLAY_SCROLL_RESET)
    429 		lines = 0;
    430 	else {
    431 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
    432 			sc->sc_scroll_values.slowlines :
    433 			sc->sc_scroll_values.fastlines;
    434 		if (op & WSDISPLAY_SCROLL_BACKWARD)
    435 			lines = -(lines);
    436 	}
    437 
    438 	if (sc->sc_accessops->scroll) {
    439 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
    440 		    sc->sc_focus->scr_dconf->emulcookie, lines);
    441 	}
    442 }
    443 #endif
    444 
    445 int
    446 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
    447 {
    448 	struct wsscreen *scr;
    449 	int s;
    450 	void *cookie;
    451 
    452 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    453 		return (EINVAL);
    454 	if ((scr = sc->sc_scr[idx]) == NULL)
    455 		return (ENXIO);
    456 
    457 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    458 	    scr->scr_syncops ||
    459 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    460 		return(EBUSY);
    461 
    462 	wsdisplay_closescreen(sc, scr);
    463 
    464 	/*
    465 	 * delete pointers, so neither device entries
    466 	 * nor keyboard input can reference it anymore
    467 	 */
    468 	s = spltty();
    469 	if (sc->sc_focus == scr) {
    470 		sc->sc_focus = 0;
    471 #ifdef WSDISPLAY_COMPAT_RAWKBD
    472 		wsdisplay_update_rawkbd(sc, 0);
    473 #endif
    474 	}
    475 	sc->sc_scr[idx] = 0;
    476 	splx(s);
    477 
    478 	/*
    479 	 * Wake up processes waiting for the screen to
    480 	 * be activated. Sleepers must check whether
    481 	 * the screen still exists.
    482 	 */
    483 	if (scr->scr_flags & SCR_WAITACTIVE)
    484 		wakeup(scr);
    485 
    486 	/* save a reference to the graphics screen */
    487 	cookie = scr->scr_dconf->emulcookie;
    488 
    489 	wsscreen_detach(scr);
    490 
    491 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    492 					 cookie);
    493 
    494 	printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
    495 	return (0);
    496 }
    497 
    498 /*
    499  * Autoconfiguration functions.
    500  */
    501 int
    502 wsdisplay_emul_match(struct device *parent, struct cfdata *match, void *aux)
    503 {
    504 	struct wsemuldisplaydev_attach_args *ap = aux;
    505 
    506 	if (match->wsemuldisplaydevcf_console !=
    507 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    508 		/*
    509 		 * If console-ness of device specified, either match
    510 		 * exactly (at high priority), or fail.
    511 		 */
    512 		if (match->wsemuldisplaydevcf_console != 0 &&
    513 		    ap->console != 0)
    514 			return (10);
    515 		else
    516 			return (0);
    517 	}
    518 
    519 	/* If console-ness unspecified, it wins. */
    520 	return (1);
    521 }
    522 
    523 void
    524 wsdisplay_emul_attach(struct device *parent, struct device *self, void *aux)
    525 {
    526 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    527 	struct wsemuldisplaydev_attach_args *ap = aux;
    528 
    529 	/* Don't allow more than one console to attach */
    530 	if (wsdisplay_console_attached && ap->console)
    531 		ap->console = 0;
    532 
    533 	wsdisplay_common_attach(sc, ap->console,
    534 	     sc->sc_dv.dv_cfdata->wsemuldisplaydevcf_kbdmux, ap->scrdata,
    535 	     ap->accessops, ap->accesscookie);
    536 
    537 	if (ap->console) {
    538 		int maj;
    539 
    540 		/* locate the major number */
    541 		maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    542 
    543 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(self->dv_unit, 0));
    544 	}
    545 }
    546 
    547 /* Print function (for parent devices). */
    548 int
    549 wsemuldisplaydevprint(void *aux, const char *pnp)
    550 {
    551 #if 0 /* -Wunused */
    552 	struct wsemuldisplaydev_attach_args *ap = aux;
    553 #endif
    554 
    555 	if (pnp)
    556 		aprint_normal("wsdisplay at %s", pnp);
    557 #if 0 /* don't bother; it's ugly */
    558 	aprint_normal(" console %d", ap->console);
    559 #endif
    560 
    561 	return (UNCONF);
    562 }
    563 
    564 int
    565 wsdisplay_noemul_match(struct device *parent, struct cfdata *match, void *aux)
    566 {
    567 #if 0 /* -Wunused */
    568 	struct wsdisplaydev_attach_args *ap = aux;
    569 #endif
    570 
    571 	/* Always match. */
    572 	return (1);
    573 }
    574 
    575 void
    576 wsdisplay_noemul_attach(struct device *parent, struct device *self, void *aux)
    577 {
    578 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    579 	struct wsdisplaydev_attach_args *ap = aux;
    580 
    581 	wsdisplay_common_attach(sc, 0,
    582 	    sc->sc_dv.dv_cfdata->wsemuldisplaydevcf_kbdmux, NULL,
    583 	    ap->accessops, ap->accesscookie);
    584 }
    585 
    586 /* Print function (for parent devices). */
    587 int
    588 wsdisplaydevprint(void *aux, const char *pnp)
    589 {
    590 #if 0 /* -Wunused */
    591 	struct wsdisplaydev_attach_args *ap = aux;
    592 #endif
    593 
    594 	if (pnp)
    595 		aprint_normal("wsdisplay at %s", pnp);
    596 
    597 	return (UNCONF);
    598 }
    599 
    600 static void
    601 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
    602 	const struct wsscreen_list *scrdata,
    603 	const struct wsdisplay_accessops *accessops,
    604 	void *accesscookie)
    605 {
    606 	static int hookset;
    607 	int i, start=0;
    608 #if NWSKBD > 0
    609 	struct wsevsrc *kme;
    610 #if NWSMUX > 0
    611 	struct wsmux_softc *mux;
    612 
    613 	if (kbdmux >= 0)
    614 		mux = wsmux_getmux(kbdmux);
    615 	else
    616 		mux = wsmux_create("dmux", sc->sc_dv.dv_unit);
    617 	/* XXX panic()ing isn't nice, but attach cannot fail */
    618 	if (mux == NULL)
    619 		panic("wsdisplay_common_attach: no memory");
    620 	sc->sc_input = &mux->sc_base;
    621 	mux->sc_base.me_dispdv = &sc->sc_dv;
    622 	printf(" kbdmux %d", kbdmux);
    623 #else
    624 	if (kbdmux >= 0)
    625 		printf(" (kbdmux ignored)");
    626 #endif
    627 #endif
    628 
    629 	sc->sc_isconsole = console;
    630 
    631 	if (console) {
    632 		KASSERT(wsdisplay_console_initted);
    633 		KASSERT(wsdisplay_console_device == NULL);
    634 
    635 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    636 		wsdisplay_console_device = sc;
    637 
    638 		printf(": console (%s, %s emulation)",
    639 		       wsdisplay_console_conf.scrdata->name,
    640 		       wsdisplay_console_conf.wsemul->name);
    641 
    642 #if NWSKBD > 0
    643 		kme = wskbd_set_console_display(&sc->sc_dv, sc->sc_input);
    644 		if (kme != NULL)
    645 			printf(", using %s", kme->me_dv.dv_xname);
    646 #if NWSMUX == 0
    647 		sc->sc_input = kme;
    648 #endif
    649 #endif
    650 
    651 		sc->sc_focusidx = 0;
    652 		sc->sc_focus = sc->sc_scr[0];
    653 		start = 1;
    654 
    655 		wsdisplay_console_attached = 1;
    656 	}
    657 	printf("\n");
    658 
    659 #if NWSKBD > 0 && NWSMUX > 0
    660 	wsmux_set_display(mux, &sc->sc_dv);
    661 #endif
    662 
    663 	sc->sc_accessops = accessops;
    664 	sc->sc_accesscookie = accesscookie;
    665 	sc->sc_scrdata = scrdata;
    666 
    667 #ifdef WSDISPLAY_SCROLLSUPPORT
    668 	sc->sc_scroll_values = wsdisplay_default_scroll_values;
    669 #endif
    670 
    671 	/*
    672 	 * Set up a number of virtual screens if wanted. The
    673 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    674 	 * is for special cases like installation kernels.
    675 	 */
    676 	for (i = start; i < wsdisplay_defaultscreens; i++) {
    677 		if (wsdisplay_addscreen(sc, i, 0, 0))
    678 			break;
    679 	}
    680 
    681 	if (i > start)
    682 		wsdisplay_addscreen_print(sc, start, i-start);
    683 
    684 	if (hookset == 0)
    685 		shutdownhook_establish(wsdisplay_shutdownhook, NULL);
    686 	hookset = 1;
    687 }
    688 
    689 void
    690 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
    691 	int ccol, int crow, long defattr)
    692 {
    693 	const struct wsemul_ops *wsemul;
    694 
    695 	KASSERT(!wsdisplay_console_initted);
    696 	KASSERT(type->nrows > 0);
    697 	KASSERT(type->ncols > 0);
    698 	KASSERT(crow < type->nrows);
    699 	KASSERT(ccol < type->ncols);
    700 
    701 	wsdisplay_console_conf.emulops = type->textops;
    702 	wsdisplay_console_conf.emulcookie = cookie;
    703 	wsdisplay_console_conf.scrdata = type;
    704 
    705 	wsemul = wsemul_pick(0); /* default */
    706 	wsdisplay_console_conf.wsemul = wsemul;
    707 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    708 								  ccol, crow,
    709 								  defattr);
    710 
    711 	cn_tab = &wsdisplay_cons;
    712 	wsdisplay_console_initted = 1;
    713 }
    714 
    715 /*
    716  * Tty and cdevsw functions.
    717  */
    718 int
    719 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
    720 {
    721 	struct wsdisplay_softc *sc;
    722 	struct tty *tp;
    723 	int newopen, error;
    724 	struct wsscreen *scr;
    725 
    726 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    727 	if (sc == NULL)			/* make sure it was attached */
    728 		return (ENXIO);
    729 
    730 	if (ISWSDISPLAYSTAT(dev)) {
    731 		wsevent_init(&sc->evar, l->l_proc);
    732 		return (0);
    733 	}
    734 
    735 	if (ISWSDISPLAYCTL(dev))
    736 		return (0);
    737 
    738 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    739 		return (ENXIO);
    740 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    741 		return (ENXIO);
    742 
    743 	if (WSSCREEN_HAS_TTY(scr)) {
    744 		tp = scr->scr_tty;
    745 		tp->t_oproc = wsdisplaystart;
    746 		tp->t_param = wsdisplayparam;
    747 		tp->t_dev = dev;
    748 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    749 		if (newopen) {
    750 			ttychars(tp);
    751 			tp->t_iflag = TTYDEF_IFLAG;
    752 			tp->t_oflag = TTYDEF_OFLAG;
    753 			tp->t_cflag = TTYDEF_CFLAG;
    754 			tp->t_lflag = TTYDEF_LFLAG;
    755 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    756 			wsdisplayparam(tp, &tp->t_termios);
    757 			ttsetwater(tp);
    758 		} else if ((tp->t_state & TS_XCLUDE) != 0 &&
    759 			   kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER,
    760 					     &l->l_proc->p_acflag) != 0)
    761 			return EBUSY;
    762 		tp->t_state |= TS_CARR_ON;
    763 
    764 		error = ((*tp->t_linesw->l_open)(dev, tp));
    765 		if (error)
    766 			return (error);
    767 
    768 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    769 			/* set window sizes as appropriate, and reset
    770 			 the emulation */
    771 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    772 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    773 
    774 			/* wsdisplay_set_emulation() */
    775 		}
    776 	}
    777 
    778 	scr->scr_flags |= SCR_OPEN;
    779 	return (0);
    780 }
    781 
    782 int
    783 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
    784 {
    785 	struct wsdisplay_softc *sc;
    786 	struct tty *tp;
    787 	struct wsscreen *scr;
    788 
    789 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    790 
    791 	if (ISWSDISPLAYSTAT(dev)) {
    792 		wsevent_fini(&sc->evar);
    793 		return (0);
    794 	}
    795 
    796 	if (ISWSDISPLAYCTL(dev))
    797 		return (0);
    798 
    799 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    800 		return (0);
    801 
    802 	if (WSSCREEN_HAS_TTY(scr)) {
    803 		if (scr->scr_hold_screen) {
    804 			int s;
    805 
    806 			/* XXX RESET KEYBOARD LEDS, etc. */
    807 			s = spltty();	/* avoid conflict with keyboard */
    808 			wsdisplay_kbdholdscreen((struct device *)sc, 0);
    809 			splx(s);
    810 		}
    811 		tp = scr->scr_tty;
    812 		(*tp->t_linesw->l_close)(tp, flag);
    813 		ttyclose(tp);
    814 	}
    815 
    816 	if (scr->scr_syncops)
    817 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    818 
    819 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    820 		scr->scr_flags &= ~SCR_GRAPHICS;
    821 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    822 						 WSEMUL_RESET);
    823 		if (wsdisplay_clearonclose)
    824 			(*scr->scr_dconf->wsemul->reset)
    825 				(scr->scr_dconf->wsemulcookie,
    826 				 WSEMUL_CLEARSCREEN);
    827 	}
    828 
    829 #ifdef WSDISPLAY_COMPAT_RAWKBD
    830 	if (scr->scr_rawkbd) {
    831 		int kbmode = WSKBD_TRANSLATED;
    832 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    833 					       (caddr_t)&kbmode, 0, l);
    834 	}
    835 #endif
    836 
    837 	scr->scr_flags &= ~SCR_OPEN;
    838 
    839 	return (0);
    840 }
    841 
    842 int
    843 wsdisplayread(dev_t dev, struct uio *uio, int flag)
    844 {
    845 	struct wsdisplay_softc *sc;
    846 	struct tty *tp;
    847 	struct wsscreen *scr;
    848 	int error;
    849 
    850 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    851 
    852 	if (ISWSDISPLAYSTAT(dev)) {
    853 		error = wsevent_read(&sc->evar, uio, flag);
    854 		return (error);
    855 	}
    856 
    857 	if (ISWSDISPLAYCTL(dev))
    858 		return (0);
    859 
    860 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    861 		return (ENXIO);
    862 
    863 	if (!WSSCREEN_HAS_TTY(scr))
    864 		return (ENODEV);
    865 
    866 	tp = scr->scr_tty;
    867 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    868 }
    869 
    870 int
    871 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
    872 {
    873 	struct wsdisplay_softc *sc;
    874 	struct tty *tp;
    875 	struct wsscreen *scr;
    876 
    877 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    878 
    879 	if (ISWSDISPLAYSTAT(dev)) {
    880 		return (0);
    881 	}
    882 
    883 	if (ISWSDISPLAYCTL(dev))
    884 		return (0);
    885 
    886 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    887 		return (ENXIO);
    888 
    889 	if (!WSSCREEN_HAS_TTY(scr))
    890 		return (ENODEV);
    891 
    892 	tp = scr->scr_tty;
    893 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    894 }
    895 
    896 int
    897 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
    898 {
    899 	struct wsdisplay_softc *sc;
    900 	struct tty *tp;
    901 	struct wsscreen *scr;
    902 
    903 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    904 
    905 	if (ISWSDISPLAYSTAT(dev))
    906 		return (wsevent_poll(&sc->evar, events, l));
    907 
    908 	if (ISWSDISPLAYCTL(dev))
    909 		return (0);
    910 
    911 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    912 		return (POLLHUP);
    913 
    914 	if (!WSSCREEN_HAS_TTY(scr))
    915 		return (POLLERR);
    916 
    917 	tp = scr->scr_tty;
    918 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    919 }
    920 
    921 int
    922 wsdisplaykqfilter(dev, kn)
    923 	dev_t dev;
    924 	struct knote *kn;
    925 {
    926 	struct wsdisplay_softc *sc =
    927 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    928 	struct wsscreen *scr;
    929 
    930 	if (ISWSDISPLAYCTL(dev))
    931 		return (1);
    932 
    933 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    934 		return (1);
    935 
    936 
    937 	if (WSSCREEN_HAS_TTY(scr))
    938 		return (ttykqfilter(dev, kn));
    939 	else
    940 		return (1);
    941 }
    942 
    943 struct tty *
    944 wsdisplaytty(dev_t dev)
    945 {
    946 	struct wsdisplay_softc *sc;
    947 	struct wsscreen *scr;
    948 
    949 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    950 
    951 	if (ISWSDISPLAYSTAT(dev))
    952 		panic("wsdisplaytty() on status device");
    953 
    954 	if (ISWSDISPLAYCTL(dev))
    955 		panic("wsdisplaytty() on ctl device");
    956 
    957 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    958 		return NULL;
    959 
    960 	return (scr->scr_tty);
    961 }
    962 
    963 int
    964 wsdisplayioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    965 {
    966 	struct wsdisplay_softc *sc;
    967 	struct tty *tp;
    968 	int error;
    969 	struct wsscreen *scr;
    970 
    971 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    972 
    973 #ifdef WSDISPLAY_COMPAT_USL
    974 	error = wsdisplay_usl_ioctl1(sc, cmd, data, flag, l);
    975 	if (error != EPASSTHROUGH)
    976 		return (error);
    977 #endif
    978 
    979 	if (ISWSDISPLAYSTAT(dev))
    980 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
    981 
    982 	if (ISWSDISPLAYCTL(dev))
    983 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
    984 
    985 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    986 		return (ENXIO);
    987 
    988 	if (WSSCREEN_HAS_TTY(scr)) {
    989 		tp = scr->scr_tty;
    990 
    991 /* printf("disc\n"); */
    992 		/* do the line discipline ioctls first */
    993 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    994 		if (error != EPASSTHROUGH)
    995 			return (error);
    996 
    997 /* printf("tty\n"); */
    998 		/* then the tty ioctls */
    999 		error = ttioctl(tp, cmd, data, flag, l);
   1000 		if (error != EPASSTHROUGH)
   1001 			return (error);
   1002 	}
   1003 
   1004 #ifdef WSDISPLAY_COMPAT_USL
   1005 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
   1006 	if (error != EPASSTHROUGH)
   1007 		return (error);
   1008 #endif
   1009 
   1010 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
   1011 }
   1012 
   1013 int
   1014 wsdisplay_param(struct device *dev, u_long cmd, struct wsdisplay_param *dp)
   1015 {
   1016 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1017 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
   1018 					   (caddr_t)dp, 0, NULL));
   1019 }
   1020 
   1021 int
   1022 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1023 	u_long cmd, caddr_t data, int flag, struct lwp *l)
   1024 {
   1025 	int error;
   1026 	char namebuf[16];
   1027 	struct wsdisplay_font fd;
   1028 #ifdef WSDISPLAY_SCROLLSUPPORT
   1029 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1030 #endif
   1031 
   1032 #if NWSKBD > 0
   1033 	struct wsevsrc *inp;
   1034 
   1035 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1036 	switch (cmd) {
   1037 	case WSKBDIO_SETMODE:
   1038 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1039 		return (wsdisplay_update_rawkbd(sc, scr));
   1040 	case WSKBDIO_GETMODE:
   1041 		*(int *)data = (scr->scr_rawkbd ?
   1042 				WSKBD_RAW : WSKBD_TRANSLATED);
   1043 		return (0);
   1044 	}
   1045 #endif
   1046 	inp = sc->sc_input;
   1047 	if (inp == NULL)
   1048 		return (ENXIO);
   1049 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1050 	if (error != EPASSTHROUGH)
   1051 		return (error);
   1052 #endif /* NWSKBD > 0 */
   1053 
   1054 	switch (cmd) {
   1055 	case WSDISPLAYIO_GMODE:
   1056 		if (scr->scr_flags & SCR_GRAPHICS) {
   1057 			if (scr->scr_flags & SCR_DUMBFB)
   1058 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1059 			else
   1060 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1061 		} else
   1062 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1063 		return (0);
   1064 
   1065 	case WSDISPLAYIO_SMODE:
   1066 #define d (*(int *)data)
   1067 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1068 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1069 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1070 			return (EINVAL);
   1071 
   1072 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1073 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1074 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1075 			d == WSDISPLAYIO_MODE_DUMBFB)
   1076 			    scr->scr_flags |= SCR_GRAPHICS |
   1077 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1078 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1079 		    return (EINVAL);
   1080 
   1081 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1082 		    flag, l);
   1083 
   1084 	    return (0);
   1085 #undef d
   1086 
   1087 #ifdef WSDISPLAY_SCROLLSUPPORT
   1088 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1089     do {								\
   1090 	(dstp)->fastlines = ((srcp)->which &				\
   1091 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1092 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1093 	(dstp)->slowlines = ((srcp)->which &				\
   1094 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1095 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1096 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1097     } while (0)
   1098 
   1099 
   1100 	case WSDISPLAYIO_DSSCROLL:
   1101 		usdp = (struct wsdisplay_scroll_data *)data;
   1102 		ksdp = &sc->sc_scroll_values;
   1103 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1104 		return (0);
   1105 
   1106 	case WSDISPLAYIO_DGSCROLL:
   1107 		usdp = (struct wsdisplay_scroll_data *)data;
   1108 		ksdp = &sc->sc_scroll_values;
   1109 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1110 		return (0);
   1111 #else
   1112 	case WSDISPLAYIO_DSSCROLL:
   1113 	case WSDISPLAYIO_DGSCROLL:
   1114 		return ENODEV;
   1115 #endif
   1116 
   1117 	case WSDISPLAYIO_SFONT:
   1118 #define d ((struct wsdisplay_usefontdata *)data)
   1119 		if (!sc->sc_accessops->load_font)
   1120 			return (EINVAL);
   1121 		if (d->name) {
   1122 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1123 			if (error)
   1124 				return (error);
   1125 			fd.name = namebuf;
   1126 		} else
   1127 			fd.name = 0;
   1128 		fd.data = 0;
   1129 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1130 					scr->scr_dconf->emulcookie, &fd);
   1131 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1132 			(*scr->scr_dconf->wsemul->reset)
   1133 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1134 		return (error);
   1135 #undef d
   1136 
   1137 #if defined(WSDISPLAY_CHARFUNCS)
   1138 	case WSDISPLAYIO_GETWSCHAR:
   1139 #define d ((struct wsdisplay_char *)data)
   1140 		if (!sc->sc_accessops->getwschar)
   1141 			return (EINVAL);
   1142 		return ((*sc->sc_accessops->getwschar)
   1143 			(scr->scr_dconf->emulcookie, d));
   1144 #undef d
   1145 
   1146 	case WSDISPLAYIO_PUTWSCHAR:
   1147 #define d ((struct wsdisplay_char *)data)
   1148 		if (!sc->sc_accessops->putwschar)
   1149 			return (EINVAL);
   1150 		return ((*sc->sc_accessops->putwschar)
   1151 			(scr->scr_dconf->emulcookie, d));
   1152 #undef d
   1153 #else
   1154 	case WSDISPLAYIO_PUTWSCHAR:
   1155 	case WSDISPLAYIO_GETWSCHAR:
   1156 		return ENODEV;
   1157 #endif /* WSDISPLAY_CHARFUNCS */
   1158 
   1159 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1160 	case WSDISPLAYIO_GMSGATTRS:
   1161 #define d ((struct wsdisplay_msgattrs *)data)
   1162 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1163 		    (scr->scr_dconf->wsemulcookie, d);
   1164 		return (0);
   1165 #undef d
   1166 
   1167 	case WSDISPLAYIO_SMSGATTRS: {
   1168 #define d ((struct wsdisplay_msgattrs *)data)
   1169 		int i;
   1170 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1171 			if (sc->sc_scr[i] != NULL)
   1172 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1173 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1174 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1175 				     d);
   1176 		}
   1177 		return (0);
   1178 #undef d
   1179 #else
   1180 	case WSDISPLAYIO_GMSGATTRS:
   1181 	case WSDISPLAYIO_SMSGATTRS:
   1182 		return (ENODEV);
   1183 #endif
   1184 
   1185 #ifdef WSDISPLAY_CUSTOM_BORDER
   1186 	case WSDISPLAYIO_GBORDER:
   1187 		if (!sc->sc_accessops->getborder)
   1188 			return (EINVAL);
   1189 		*(u_int *)data = (*sc->sc_accessops->getborder)
   1190 			       (scr->scr_dconf->emulcookie);
   1191 		return (0);
   1192 	case WSDISPLAYIO_SBORDER:
   1193 		if (!sc->sc_accessops->setborder)
   1194 			return (EINVAL);
   1195 		return (*sc->sc_accessops->setborder)
   1196 		    (scr->scr_dconf->emulcookie, (*(u_int *)data));
   1197 #else /* WSDISPLAY_CUSTOM_BORDER */
   1198 	case WSDISPLAYIO_GBORDER:
   1199 	case WSDISPLAYIO_SBORDER:
   1200 		return (ENODEV);
   1201 #endif /* WSDISPLAY_CUSTOM_BORDER */
   1202 
   1203 	}
   1204 
   1205 	/* check ioctls for display */
   1206 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1207 	    flag, l));
   1208 }
   1209 
   1210 int
   1211 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1212 	int flag, struct lwp *l)
   1213 {
   1214 	switch (cmd) {
   1215 	case WSDISPLAYIO_GETACTIVESCREEN:
   1216 		*(int*)data = wsdisplay_getactivescreen(sc);
   1217 		return (0);
   1218 	}
   1219 
   1220 	return (EPASSTHROUGH);
   1221 }
   1222 
   1223 int
   1224 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1225 	int flag, struct lwp *l)
   1226 {
   1227 	int error;
   1228 	char *type, typebuf[16], *emul, emulbuf[16];
   1229 	void *tbuf;
   1230 	u_int fontsz;
   1231 #if defined(COMPAT_14) && NWSKBD > 0
   1232 	struct wsmux_device wsmuxdata;
   1233 #endif
   1234 #if NWSKBD > 0
   1235 	struct wsevsrc *inp;
   1236 #endif
   1237 
   1238 	switch (cmd) {
   1239 	case WSDISPLAYIO_ADDSCREEN:
   1240 #define d ((struct wsdisplay_addscreendata *)data)
   1241 		if (d->screentype) {
   1242 			error = copyinstr(d->screentype, typebuf,
   1243 					  sizeof(typebuf), 0);
   1244 			if (error)
   1245 				return (error);
   1246 			type = typebuf;
   1247 		} else
   1248 			type = 0;
   1249 		if (d->emul) {
   1250 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1251 			if (error)
   1252 				return (error);
   1253 			emul = emulbuf;
   1254 		} else
   1255 			emul = 0;
   1256 
   1257 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1258 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1259 		return (error);
   1260 #undef d
   1261 	case WSDISPLAYIO_DELSCREEN:
   1262 #define d ((struct wsdisplay_delscreendata *)data)
   1263 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1264 #undef d
   1265 	case WSDISPLAYIO_LDFONT:
   1266 #define d ((struct wsdisplay_font *)data)
   1267 		if (!sc->sc_accessops->load_font)
   1268 			return (EINVAL);
   1269 		if (d->name) {
   1270 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1271 			if (error)
   1272 				return (error);
   1273 			d->name = typebuf;
   1274 		} else
   1275 			d->name = "loaded"; /* ??? */
   1276 		fontsz = d->fontheight * d->stride * d->numchars;
   1277 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1278 			return (EINVAL);
   1279 
   1280 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1281 		error = copyin(d->data, tbuf, fontsz);
   1282 		if (error) {
   1283 			free(tbuf, M_DEVBUF);
   1284 			return (error);
   1285 		}
   1286 		d->data = tbuf;
   1287 		error =
   1288 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1289 		free(tbuf, M_DEVBUF);
   1290 #undef d
   1291 		return (error);
   1292 
   1293 #if NWSKBD > 0
   1294 #ifdef COMPAT_14
   1295 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1296 #define d ((struct wsdisplay_kbddata *)data)
   1297 		inp = sc->sc_input;
   1298 		if (inp == NULL)
   1299 			return (ENXIO);
   1300 		switch (d->op) {
   1301 		case _O_WSDISPLAY_KBD_ADD:
   1302 			if (d->idx == -1) {
   1303 				d->idx = wskbd_pickfree();
   1304 				if (d->idx == -1)
   1305 					return (ENXIO);
   1306 			}
   1307 			wsmuxdata.type = WSMUX_KBD;
   1308 			wsmuxdata.idx = d->idx;
   1309 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1310 					      &wsmuxdata, flag, l));
   1311 		case _O_WSDISPLAY_KBD_DEL:
   1312 			wsmuxdata.type = WSMUX_KBD;
   1313 			wsmuxdata.idx = d->idx;
   1314 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1315 					      &wsmuxdata, flag, l));
   1316 		default:
   1317 			return (EINVAL);
   1318 		}
   1319 #undef d
   1320 #endif
   1321 
   1322 	case WSMUXIO_ADD_DEVICE:
   1323 #define d ((struct wsmux_device *)data)
   1324 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1325 			d->idx = wskbd_pickfree();
   1326 #undef d
   1327 		/* fall into */
   1328 	case WSMUXIO_INJECTEVENT:
   1329 	case WSMUXIO_REMOVE_DEVICE:
   1330 	case WSMUXIO_LIST_DEVICES:
   1331 		inp = sc->sc_input;
   1332 		if (inp == NULL)
   1333 			return (ENXIO);
   1334 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1335 #endif /* NWSKBD > 0 */
   1336 
   1337 	}
   1338 	return (EPASSTHROUGH);
   1339 }
   1340 
   1341 int
   1342 wsdisplay_stat_inject(struct device *dev, u_int type, int value)
   1343 {
   1344 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *) dev;
   1345 	struct wseventvar *evar;
   1346 	struct wscons_event event;
   1347 
   1348 	evar = &sc->evar;
   1349 
   1350 	if (evar == NULL)
   1351 		return (0);
   1352 
   1353 	if (evar->q == NULL)
   1354 		return (1);
   1355 
   1356 	event.type = type;
   1357 	event.value = value;
   1358 	if (wsevent_inject(evar, &event, 1) != 0) {
   1359 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1360 		return (1);
   1361 	}
   1362 
   1363 	return (0);
   1364 }
   1365 
   1366 paddr_t
   1367 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1368 {
   1369 	struct wsdisplay_softc *sc =
   1370 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1371 	struct wsscreen *scr;
   1372 
   1373 	if (ISWSDISPLAYSTAT(dev))
   1374 		return (-1);
   1375 
   1376 	if (ISWSDISPLAYCTL(dev))
   1377 		return (-1);
   1378 
   1379 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1380 		return (-1);
   1381 
   1382 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1383 		return (-1);
   1384 
   1385 	/* pass mmap to display */
   1386 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
   1387 }
   1388 
   1389 void
   1390 wsdisplaystart(struct tty *tp)
   1391 {
   1392 	struct wsdisplay_softc *sc;
   1393 	struct wsscreen *scr;
   1394 	int s, n;
   1395 	u_char *tbuf;
   1396 
   1397 	s = spltty();
   1398 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1399 		splx(s);
   1400 		return;
   1401 	}
   1402 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1403 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
   1404 		splx(s);
   1405 		return;
   1406 	}
   1407 
   1408 	if (scr->scr_hold_screen) {
   1409 		tp->t_state |= TS_TIMEOUT;
   1410 		splx(s);
   1411 		return;
   1412 	}
   1413 	tp->t_state |= TS_BUSY;
   1414 	splx(s);
   1415 
   1416 	/*
   1417 	 * Drain output from ring buffer.
   1418 	 * The output will normally be in one contiguous chunk, but when the
   1419 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1420 	 * the other at the start.  For performance, rather than loop here,
   1421 	 * we output one chunk, see if there's another one, and if so, output
   1422 	 * it too.
   1423 	 */
   1424 
   1425 	n = ndqb(&tp->t_outq, 0);
   1426 	tbuf = tp->t_outq.c_cf;
   1427 
   1428 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1429 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1430 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1431 						  tbuf, n, 0);
   1432 	}
   1433 	ndflush(&tp->t_outq, n);
   1434 
   1435 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1436 		tbuf = tp->t_outq.c_cf;
   1437 
   1438 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1439 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1440 			(*scr->scr_dconf->wsemul->output)
   1441 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
   1442 		}
   1443 		ndflush(&tp->t_outq, n);
   1444 	}
   1445 
   1446 	s = spltty();
   1447 	tp->t_state &= ~TS_BUSY;
   1448 	/* Come back if there's more to do */
   1449 	if (tp->t_outq.c_cc) {
   1450 		tp->t_state |= TS_TIMEOUT;
   1451 		callout_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
   1452 		    ttrstrt, tp);
   1453 	}
   1454 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1455 		if (tp->t_state&TS_ASLEEP) {
   1456 			tp->t_state &= ~TS_ASLEEP;
   1457 			wakeup(&tp->t_outq);
   1458 		}
   1459 		selwakeup(&tp->t_wsel);
   1460 	}
   1461 	splx(s);
   1462 }
   1463 
   1464 void
   1465 wsdisplaystop(struct tty *tp, int flag)
   1466 {
   1467 	int s;
   1468 
   1469 	s = spltty();
   1470 	if (ISSET(tp->t_state, TS_BUSY))
   1471 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1472 			SET(tp->t_state, TS_FLUSH);
   1473 	splx(s);
   1474 }
   1475 
   1476 /* Set line parameters. */
   1477 int
   1478 wsdisplayparam(struct tty *tp, struct termios *t)
   1479 {
   1480 
   1481 	tp->t_ispeed = t->c_ispeed;
   1482 	tp->t_ospeed = t->c_ospeed;
   1483 	tp->t_cflag = t->c_cflag;
   1484 	return 0;
   1485 }
   1486 
   1487 /*
   1488  * Callbacks for the emulation code.
   1489  */
   1490 void
   1491 wsdisplay_emulbell(void *v)
   1492 {
   1493 	struct wsscreen *scr = v;
   1494 
   1495 	if (scr == NULL)		/* console, before real attach */
   1496 		return;
   1497 
   1498 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1499 		return;
   1500 
   1501 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1502 					FWRITE, NULL);
   1503 }
   1504 
   1505 void
   1506 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1507 {
   1508 	struct wsscreen *scr = v;
   1509 	struct tty *tp;
   1510 
   1511 	if (v == NULL)			/* console, before real attach */
   1512 		return;
   1513 
   1514 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1515 		return;
   1516 	if (!WSSCREEN_HAS_TTY(scr))
   1517 		return;
   1518 
   1519 	tp = scr->scr_tty;
   1520 	while (count-- > 0)
   1521 		(*tp->t_linesw->l_rint)(*data++, tp);
   1522 }
   1523 
   1524 /*
   1525  * Calls from the keyboard interface.
   1526  */
   1527 void
   1528 wsdisplay_kbdinput(struct device *dev, keysym_t ks)
   1529 {
   1530 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1531 	struct wsscreen *scr;
   1532 	const char *dp;
   1533 	int count;
   1534 	struct tty *tp;
   1535 
   1536 	KASSERT(sc != NULL);
   1537 
   1538 	scr = sc->sc_focus;
   1539 
   1540 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1541 		return;
   1542 
   1543 	tp = scr->scr_tty;
   1544 
   1545 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1546 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
   1547 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1548 		count = (*scr->scr_dconf->wsemul->translate)
   1549 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1550 		while (count-- > 0)
   1551 			(*tp->t_linesw->l_rint)(*dp++, tp);
   1552 	}
   1553 }
   1554 
   1555 #if defined(WSDISPLAY_COMPAT_RAWKBD)
   1556 int
   1557 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
   1558 {
   1559 #if NWSKBD > 0
   1560 	int s, raw, data, error;
   1561 	struct wsevsrc *inp;
   1562 
   1563 	s = spltty();
   1564 
   1565 	raw = (scr ? scr->scr_rawkbd : 0);
   1566 
   1567 	if (scr != sc->sc_focus ||
   1568 	    sc->sc_rawkbd == raw) {
   1569 		splx(s);
   1570 		return (0);
   1571 	}
   1572 
   1573 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1574 	inp = sc->sc_input;
   1575 	if (inp == NULL)
   1576 		return (ENXIO);
   1577 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1578 	if (!error)
   1579 		sc->sc_rawkbd = raw;
   1580 	splx(s);
   1581 	return (error);
   1582 #else
   1583 	return (0);
   1584 #endif
   1585 }
   1586 #endif
   1587 
   1588 int
   1589 wsdisplay_switch3(void *arg, int error, int waitok)
   1590 {
   1591 	struct wsdisplay_softc *sc = arg;
   1592 	int no;
   1593 	struct wsscreen *scr;
   1594 
   1595 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1596 		printf("wsdisplay_switch3: not switching\n");
   1597 		return (EINVAL);
   1598 	}
   1599 
   1600 	no = sc->sc_screenwanted;
   1601 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1602 		panic("wsdisplay_switch3: invalid screen %d", no);
   1603 	scr = sc->sc_scr[no];
   1604 	if (!scr) {
   1605 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1606 		error = ENXIO;
   1607 	}
   1608 
   1609 	if (error) {
   1610 		/* try to recover, avoid recursion */
   1611 
   1612 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1613 			printf("wsdisplay_switch3: giving up\n");
   1614 			sc->sc_focus = 0;
   1615 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1616 			wsdisplay_update_rawkbd(sc, 0);
   1617 #endif
   1618 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1619 			return (error);
   1620 		}
   1621 
   1622 		sc->sc_screenwanted = sc->sc_oldscreen;
   1623 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1624 		return (wsdisplay_switch1(arg, 0, waitok));
   1625 	}
   1626 
   1627 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1628 
   1629 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1630 		wakeup(scr);
   1631 	return (error);
   1632 }
   1633 
   1634 int
   1635 wsdisplay_switch2(void *arg, int error, int waitok)
   1636 {
   1637 	struct wsdisplay_softc *sc = arg;
   1638 	int no;
   1639 	struct wsscreen *scr;
   1640 
   1641 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1642 		printf("wsdisplay_switch2: not switching\n");
   1643 		return (EINVAL);
   1644 	}
   1645 
   1646 	no = sc->sc_screenwanted;
   1647 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1648 		panic("wsdisplay_switch2: invalid screen %d", no);
   1649 	scr = sc->sc_scr[no];
   1650 	if (!scr) {
   1651 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
   1652 		error = ENXIO;
   1653 	}
   1654 
   1655 	if (error) {
   1656 		/* try to recover, avoid recursion */
   1657 
   1658 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1659 			printf("wsdisplay_switch2: giving up\n");
   1660 			sc->sc_focus = 0;
   1661 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1662 			return (error);
   1663 		}
   1664 
   1665 		sc->sc_screenwanted = sc->sc_oldscreen;
   1666 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1667 		return (wsdisplay_switch1(arg, 0, waitok));
   1668 	}
   1669 
   1670 	sc->sc_focusidx = no;
   1671 	sc->sc_focus = scr;
   1672 
   1673 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1674 	(void) wsdisplay_update_rawkbd(sc, scr);
   1675 #endif
   1676 	/* keyboard map??? */
   1677 
   1678 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
   1679 	if (scr->scr_syncops) {
   1680 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1681 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
   1682 		if (error == EAGAIN) {
   1683 			/* switch will be done asynchronously */
   1684 			return (0);
   1685 		}
   1686 	}
   1687 
   1688 	return (wsdisplay_switch3(sc, error, waitok));
   1689 }
   1690 
   1691 int
   1692 wsdisplay_switch1(void *arg, int error, int waitok)
   1693 {
   1694 	struct wsdisplay_softc *sc = arg;
   1695 	int no;
   1696 	struct wsscreen *scr;
   1697 
   1698 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1699 		printf("wsdisplay_switch1: not switching\n");
   1700 		return (EINVAL);
   1701 	}
   1702 
   1703 	no = sc->sc_screenwanted;
   1704 	if (no == WSDISPLAY_NULLSCREEN) {
   1705 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1706 		if (!error) {
   1707 			sc->sc_focus = 0;
   1708 		}
   1709 		wakeup(sc);
   1710 		return (error);
   1711 	}
   1712 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1713 		panic("wsdisplay_switch1: invalid screen %d", no);
   1714 	scr = sc->sc_scr[no];
   1715 	if (!scr) {
   1716 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1717 		error = ENXIO;
   1718 	}
   1719 
   1720 	if (error) {
   1721 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1722 		return (error);
   1723 	}
   1724 
   1725 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
   1726 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1727 						 scr->scr_dconf->emulcookie,
   1728 						 waitok,
   1729 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
   1730 	if (error == EAGAIN) {
   1731 		/* switch will be done asynchronously */
   1732 		return (0);
   1733 	}
   1734 
   1735 	return (wsdisplay_switch2(sc, error, waitok));
   1736 }
   1737 
   1738 int
   1739 wsdisplay_switch(struct device *dev, int no, int waitok)
   1740 {
   1741 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1742 	int s, res = 0;
   1743 	struct wsscreen *scr;
   1744 
   1745 	if (no != WSDISPLAY_NULLSCREEN) {
   1746 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
   1747 			return (EINVAL);
   1748 		if (sc->sc_scr[no] == NULL)
   1749 			return (ENXIO);
   1750 	}
   1751 
   1752 	wsdisplay_stat_inject(dev, WSCONS_EVENT_SCREEN_SWITCH, no);
   1753 
   1754 	s = spltty();
   1755 
   1756 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   1757 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   1758 		splx(s);
   1759 		return (0);
   1760 	}
   1761 
   1762 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1763 		splx(s);
   1764 		return (EBUSY);
   1765 	}
   1766 
   1767 	sc->sc_flags |= SC_SWITCHPENDING;
   1768 	sc->sc_screenwanted = no;
   1769 
   1770 	splx(s);
   1771 
   1772 	scr = sc->sc_focus;
   1773 	if (!scr) {
   1774 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1775 		return (wsdisplay_switch1(sc, 0, waitok));
   1776 	} else
   1777 		sc->sc_oldscreen = sc->sc_focusidx;
   1778 
   1779 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
   1780 	if (scr->scr_syncops) {
   1781 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1782 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
   1783 		if (res == EAGAIN) {
   1784 			/* switch will be done asynchronously */
   1785 			return (0);
   1786 		}
   1787 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1788 		/* no way to save state */
   1789 		res = EBUSY;
   1790 	}
   1791 
   1792 	return (wsdisplay_switch1(sc, res, waitok));
   1793 }
   1794 
   1795 void
   1796 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
   1797 {
   1798 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1799 	struct wsscreen *scr;
   1800 
   1801 	KASSERT(sc != NULL);
   1802 	scr = sc->sc_focus;
   1803 
   1804 	if (!scr)
   1805 		return;
   1806 
   1807 	switch (op) {
   1808 	case WSDISPLAY_RESETEMUL:
   1809 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1810 			break;
   1811 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1812 						 WSEMUL_RESET);
   1813 		break;
   1814 	case WSDISPLAY_RESETCLOSE:
   1815 		wsdisplay_closescreen(sc, scr);
   1816 		break;
   1817 	}
   1818 }
   1819 
   1820 /*
   1821  * Interface for (external) VT switch / process synchronization code
   1822  */
   1823 int
   1824 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   1825 	void *cookie)
   1826 {
   1827 	if (scr->scr_syncops) {
   1828 		/*
   1829 		 * The screen is already claimed.
   1830 		 * Check if the owner is still alive.
   1831 		 */
   1832 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1833 			return (EBUSY);
   1834 	}
   1835 	scr->scr_syncops = ops;
   1836 	scr->scr_synccookie = cookie;
   1837 	return (0);
   1838 }
   1839 
   1840 int
   1841 wsscreen_detach_sync(struct wsscreen *scr)
   1842 {
   1843 	if (!scr->scr_syncops)
   1844 		return (EINVAL);
   1845 	scr->scr_syncops = 0;
   1846 	return (0);
   1847 }
   1848 
   1849 int
   1850 wsscreen_lookup_sync(struct wsscreen *scr,
   1851 	const struct wscons_syncops *ops, /* used as ID */
   1852 	void **cookiep)
   1853 {
   1854 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1855 		return (EINVAL);
   1856 	*cookiep = scr->scr_synccookie;
   1857 	return (0);
   1858 }
   1859 
   1860 /*
   1861  * Interface to virtual screen stuff
   1862  */
   1863 int
   1864 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   1865 {
   1866 	return (WSDISPLAY_MAXSCREEN - 1);
   1867 }
   1868 
   1869 int
   1870 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   1871 {
   1872 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   1873 		return (EINVAL);
   1874 	if (!sc->sc_scr[idx])
   1875 		return (ENXIO);
   1876 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1877 }
   1878 
   1879 int
   1880 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   1881 {
   1882 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   1883 }
   1884 
   1885 int
   1886 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   1887 {
   1888 	struct wsscreen *scr;
   1889 	int s, res = 0;
   1890 
   1891 	if (no == WSDISPLAY_NULLSCREEN) {
   1892 		s = spltty();
   1893 		while (sc->sc_focus && res == 0) {
   1894 			res = tsleep(sc, PCATCH, "wswait", 0);
   1895 		}
   1896 		splx(s);
   1897 		return (res);
   1898 	}
   1899 
   1900 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1901 		return (ENXIO);
   1902 	scr = sc->sc_scr[no];
   1903 	if (!scr)
   1904 		return (ENXIO);
   1905 
   1906 	s = spltty();
   1907 	if (scr != sc->sc_focus) {
   1908 		scr->scr_flags |= SCR_WAITACTIVE;
   1909 		res = tsleep(scr, PCATCH, "wswait", 0);
   1910 		if (scr != sc->sc_scr[no])
   1911 			res = ENXIO; /* disappeared in the meantime */
   1912 		else
   1913 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1914 	}
   1915 	splx(s);
   1916 	return (res);
   1917 }
   1918 
   1919 void
   1920 wsdisplay_kbdholdscreen(struct device *dev, int hold)
   1921 {
   1922 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1923 	struct wsscreen *scr;
   1924 
   1925 	scr = sc->sc_focus;
   1926 
   1927 	if (hold)
   1928 		scr->scr_hold_screen = 1;
   1929 	else {
   1930 		scr->scr_hold_screen = 0;
   1931 		callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
   1932 		    ttrstrt, scr->scr_tty);		/* "immediate" */
   1933 	}
   1934 }
   1935 
   1936 #if NWSKBD > 0
   1937 void
   1938 wsdisplay_set_console_kbd(struct wsevsrc *src)
   1939 {
   1940 	if (wsdisplay_console_device == NULL) {
   1941 		src->me_dispdv = NULL;
   1942 		return;
   1943 	}
   1944 #if NWSMUX > 0
   1945 	if (wsmux_attach_sc((struct wsmux_softc *)
   1946 			    wsdisplay_console_device->sc_input, src)) {
   1947 		src->me_dispdv = NULL;
   1948 		return;
   1949 	}
   1950 #else
   1951 	wsdisplay_console_device->sc_input = src;
   1952 #endif
   1953 	src->me_dispdv = &wsdisplay_console_device->sc_dv;
   1954 }
   1955 #endif /* NWSKBD > 0 */
   1956 
   1957 /*
   1958  * Console interface.
   1959  */
   1960 void
   1961 wsdisplay_cnputc(dev_t dev, int i)
   1962 {
   1963 	struct wsscreen_internal *dc;
   1964 	u_char c = i;
   1965 
   1966 	if (!wsdisplay_console_initted)
   1967 		return;
   1968 
   1969 	if ((wsdisplay_console_device != NULL) &&
   1970 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   1971 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1972 		return;
   1973 
   1974 	dc = &wsdisplay_console_conf;
   1975 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1976 }
   1977 
   1978 static int
   1979 wsdisplay_getc_dummy(dev_t dev)
   1980 {
   1981 	/* panic? */
   1982 	return (0);
   1983 }
   1984 
   1985 static void
   1986 wsdisplay_pollc(dev_t dev, int on)
   1987 {
   1988 
   1989 	wsdisplay_cons_pollmode = on;
   1990 
   1991 	/* notify to fb drivers */
   1992 	if (wsdisplay_console_device != NULL &&
   1993 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   1994 		(*wsdisplay_console_device->sc_accessops->pollc)
   1995 			(wsdisplay_console_device->sc_accesscookie, on);
   1996 
   1997 	/* notify to kbd drivers */
   1998 	if (wsdisplay_cons_kbd_pollc)
   1999 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   2000 }
   2001 
   2002 void
   2003 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   2004 	void (*bell)(dev_t, u_int, u_int, u_int))
   2005 {
   2006 	wsdisplay_cons.cn_getc = get;
   2007 	wsdisplay_cons.cn_bell = bell;
   2008 	wsdisplay_cons_kbd_pollc = poll;
   2009 }
   2010 
   2011 void
   2012 wsdisplay_unset_cons_kbd(void)
   2013 {
   2014 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   2015 	wsdisplay_cons.cn_bell = NULL;
   2016 	wsdisplay_cons_kbd_pollc = 0;
   2017 }
   2018 
   2019 /*
   2020  * Switch the console display to it's first screen.
   2021  */
   2022 void
   2023 wsdisplay_switchtoconsole(void)
   2024 {
   2025 	struct wsdisplay_softc *sc;
   2026 	struct wsscreen *scr;
   2027 
   2028 	if (wsdisplay_console_device != NULL) {
   2029 		sc = wsdisplay_console_device;
   2030 		if ((scr = sc->sc_scr[0]) == NULL)
   2031 			return;
   2032 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   2033 						 scr->scr_dconf->emulcookie,
   2034 						 0, NULL, NULL);
   2035 	}
   2036 }
   2037 
   2038 /*
   2039  * Switch the console at shutdown.
   2040  */
   2041 static void
   2042 wsdisplay_shutdownhook(void *arg)
   2043 {
   2044 
   2045 	wsdisplay_switchtoconsole();
   2046 }
   2047