Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.106.4.1
      1 /* $NetBSD: wsdisplay.c,v 1.106.4.1 2007/03/12 05:57:49 rmind 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.106.4.1 2007/03/12 05:57:49 rmind Exp $");
     35 
     36 #include "opt_wsdisplay_compat.h"
     37 #include "opt_wsmsgattrs.h"
     38 #include "opt_compat_netbsd.h"
     39 #include "wskbd.h"
     40 #include "wsmux.h"
     41 #include "wsdisplay.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/conf.h>
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/poll.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/malloc.h>
     51 #include <sys/syslog.h>
     52 #include <sys/systm.h>
     53 #include <sys/tty.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/errno.h>
     56 #include <sys/fcntl.h>
     57 #include <sys/vnode.h>
     58 #include <sys/kauth.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(device_unit(&sc->sc_dv), 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 	struct wsscreen *scr;
    427 	int lines;
    428 
    429 	scr = sc->sc_focus;
    430 
    431 	if (!scr)
    432 		return;
    433 
    434 	if (op == WSDISPLAY_SCROLL_RESET)
    435 		lines = 0;
    436 	else {
    437 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
    438 			sc->sc_scroll_values.slowlines :
    439 			sc->sc_scroll_values.fastlines;
    440 		if (op & WSDISPLAY_SCROLL_BACKWARD)
    441 			lines = -(lines);
    442 	}
    443 
    444 	if (sc->sc_accessops->scroll) {
    445 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
    446 		    sc->sc_focus->scr_dconf->emulcookie, lines);
    447 	}
    448 }
    449 #endif
    450 
    451 int
    452 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
    453 {
    454 	struct wsscreen *scr;
    455 	int s;
    456 	void *cookie;
    457 
    458 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    459 		return (EINVAL);
    460 	if ((scr = sc->sc_scr[idx]) == NULL)
    461 		return (ENXIO);
    462 
    463 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    464 	    scr->scr_syncops ||
    465 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    466 		return(EBUSY);
    467 
    468 	wsdisplay_closescreen(sc, scr);
    469 
    470 	/*
    471 	 * delete pointers, so neither device entries
    472 	 * nor keyboard input can reference it anymore
    473 	 */
    474 	s = spltty();
    475 	if (sc->sc_focus == scr) {
    476 		sc->sc_focus = 0;
    477 #ifdef WSDISPLAY_COMPAT_RAWKBD
    478 		wsdisplay_update_rawkbd(sc, 0);
    479 #endif
    480 	}
    481 	sc->sc_scr[idx] = 0;
    482 	splx(s);
    483 
    484 	/*
    485 	 * Wake up processes waiting for the screen to
    486 	 * be activated. Sleepers must check whether
    487 	 * the screen still exists.
    488 	 */
    489 	if (scr->scr_flags & SCR_WAITACTIVE)
    490 		wakeup(scr);
    491 
    492 	/* save a reference to the graphics screen */
    493 	cookie = scr->scr_dconf->emulcookie;
    494 
    495 	wsscreen_detach(scr);
    496 
    497 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    498 					 cookie);
    499 
    500 	printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
    501 	return (0);
    502 }
    503 
    504 /*
    505  * Autoconfiguration functions.
    506  */
    507 int
    508 wsdisplay_emul_match(struct device *parent, struct cfdata *match,
    509     void *aux)
    510 {
    511 	struct wsemuldisplaydev_attach_args *ap = aux;
    512 
    513 	if (match->wsemuldisplaydevcf_console !=
    514 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    515 		/*
    516 		 * If console-ness of device specified, either match
    517 		 * exactly (at high priority), or fail.
    518 		 */
    519 		if (match->wsemuldisplaydevcf_console != 0 &&
    520 		    ap->console != 0)
    521 			return (10);
    522 		else
    523 			return (0);
    524 	}
    525 
    526 	/* If console-ness unspecified, it wins. */
    527 	return (1);
    528 }
    529 
    530 void
    531 wsdisplay_emul_attach(struct device *parent, struct device *self,
    532     void *aux)
    533 {
    534 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    535 	struct wsemuldisplaydev_attach_args *ap = aux;
    536 
    537 	/* Don't allow more than one console to attach */
    538 	if (wsdisplay_console_attached && ap->console)
    539 		ap->console = 0;
    540 
    541 	wsdisplay_common_attach(sc, ap->console,
    542 	     device_cfdata(&sc->sc_dv)->wsemuldisplaydevcf_kbdmux, ap->scrdata,
    543 	     ap->accessops, ap->accesscookie);
    544 
    545 	if (ap->console) {
    546 		int maj;
    547 
    548 		/* locate the major number */
    549 		maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    550 
    551 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
    552 					 0));
    553 	}
    554 }
    555 
    556 /* Print function (for parent devices). */
    557 int
    558 wsemuldisplaydevprint(void *aux, const char *pnp)
    559 {
    560 #if 0 /* -Wunused */
    561 	struct wsemuldisplaydev_attach_args *ap = aux;
    562 #endif
    563 
    564 	if (pnp)
    565 		aprint_normal("wsdisplay at %s", pnp);
    566 #if 0 /* don't bother; it's ugly */
    567 	aprint_normal(" console %d", ap->console);
    568 #endif
    569 
    570 	return (UNCONF);
    571 }
    572 
    573 int
    574 wsdisplay_noemul_match(struct device *parent,
    575     struct cfdata *match, void *aux)
    576 {
    577 #if 0 /* -Wunused */
    578 	struct wsdisplaydev_attach_args *ap = aux;
    579 #endif
    580 
    581 	/* Always match. */
    582 	return (1);
    583 }
    584 
    585 void
    586 wsdisplay_noemul_attach(struct device *parent, struct device *self,
    587     void *aux)
    588 {
    589 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    590 	struct wsdisplaydev_attach_args *ap = aux;
    591 
    592 	wsdisplay_common_attach(sc, 0,
    593 	    device_cfdata(&sc->sc_dv)->wsemuldisplaydevcf_kbdmux, NULL,
    594 	    ap->accessops, ap->accesscookie);
    595 }
    596 
    597 /* Print function (for parent devices). */
    598 int
    599 wsdisplaydevprint(void *aux, const char *pnp)
    600 {
    601 #if 0 /* -Wunused */
    602 	struct wsdisplaydev_attach_args *ap = aux;
    603 #endif
    604 
    605 	if (pnp)
    606 		aprint_normal("wsdisplay at %s", pnp);
    607 
    608 	return (UNCONF);
    609 }
    610 
    611 static void
    612 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
    613 	const struct wsscreen_list *scrdata,
    614 	const struct wsdisplay_accessops *accessops,
    615 	void *accesscookie)
    616 {
    617 	static int hookset;
    618 	int i, start=0;
    619 #if NWSKBD > 0
    620 	struct wsevsrc *kme;
    621 #if NWSMUX > 0
    622 	struct wsmux_softc *mux;
    623 
    624 	if (kbdmux >= 0)
    625 		mux = wsmux_getmux(kbdmux);
    626 	else
    627 		mux = wsmux_create("dmux", device_unit(&sc->sc_dv));
    628 	/* XXX panic()ing isn't nice, but attach cannot fail */
    629 	if (mux == NULL)
    630 		panic("wsdisplay_common_attach: no memory");
    631 	sc->sc_input = &mux->sc_base;
    632 	mux->sc_base.me_dispdv = &sc->sc_dv;
    633 	printf(" kbdmux %d", kbdmux);
    634 #else
    635 	if (kbdmux >= 0)
    636 		printf(" (kbdmux ignored)");
    637 #endif
    638 #endif
    639 
    640 	sc->sc_isconsole = console;
    641 
    642 	if (console) {
    643 		KASSERT(wsdisplay_console_initted);
    644 		KASSERT(wsdisplay_console_device == NULL);
    645 
    646 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    647 		wsdisplay_console_device = sc;
    648 
    649 		printf(": console (%s, %s emulation)",
    650 		       wsdisplay_console_conf.scrdata->name,
    651 		       wsdisplay_console_conf.wsemul->name);
    652 
    653 #if NWSKBD > 0
    654 		kme = wskbd_set_console_display(&sc->sc_dv, sc->sc_input);
    655 		if (kme != NULL)
    656 			printf(", using %s", kme->me_dv.dv_xname);
    657 #if NWSMUX == 0
    658 		sc->sc_input = kme;
    659 #endif
    660 #endif
    661 
    662 		sc->sc_focusidx = 0;
    663 		sc->sc_focus = sc->sc_scr[0];
    664 		start = 1;
    665 
    666 		wsdisplay_console_attached = 1;
    667 	}
    668 	printf("\n");
    669 
    670 #if NWSKBD > 0 && NWSMUX > 0
    671 	wsmux_set_display(mux, &sc->sc_dv);
    672 #endif
    673 
    674 	sc->sc_accessops = accessops;
    675 	sc->sc_accesscookie = accesscookie;
    676 	sc->sc_scrdata = scrdata;
    677 
    678 #ifdef WSDISPLAY_SCROLLSUPPORT
    679 	sc->sc_scroll_values = wsdisplay_default_scroll_values;
    680 #endif
    681 
    682 	/*
    683 	 * Set up a number of virtual screens if wanted. The
    684 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    685 	 * is for special cases like installation kernels.
    686 	 */
    687 	for (i = start; i < wsdisplay_defaultscreens; i++) {
    688 		if (wsdisplay_addscreen(sc, i, 0, 0))
    689 			break;
    690 	}
    691 
    692 	if (i > start)
    693 		wsdisplay_addscreen_print(sc, start, i-start);
    694 
    695 	if (hookset == 0)
    696 		shutdownhook_establish(wsdisplay_shutdownhook, NULL);
    697 	hookset = 1;
    698 }
    699 
    700 void
    701 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
    702 	int ccol, int crow, long defattr)
    703 {
    704 	const struct wsemul_ops *wsemul;
    705 
    706 	KASSERT(wsdisplay_console_initted < 2);
    707 	KASSERT(type->nrows > 0);
    708 	KASSERT(type->ncols > 0);
    709 	KASSERT(crow < type->nrows);
    710 	KASSERT(ccol < type->ncols);
    711 
    712 	wsdisplay_console_conf.emulops = type->textops;
    713 	wsdisplay_console_conf.emulcookie = cookie;
    714 	wsdisplay_console_conf.scrdata = type;
    715 
    716 	wsemul = wsemul_pick(0); /* default */
    717 	wsdisplay_console_conf.wsemul = wsemul;
    718 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    719 								  ccol, crow,
    720 								  defattr);
    721 
    722 	cn_tab = &wsdisplay_cons;
    723 	wsdisplay_console_initted = 2;
    724 }
    725 
    726 void
    727 wsdisplay_preattach(const struct wsscreen_descr *type, void *cookie,
    728 	int ccol, int crow, long defattr)
    729 {
    730 	const struct wsemul_ops *wsemul;
    731 
    732 	KASSERT(!wsdisplay_console_initted);
    733 	KASSERT(type->nrows > 0);
    734 	KASSERT(type->ncols > 0);
    735 	KASSERT(crow < type->nrows);
    736 	KASSERT(ccol < type->ncols);
    737 
    738 	wsdisplay_console_conf.emulops = type->textops;
    739 	wsdisplay_console_conf.emulcookie = cookie;
    740 	wsdisplay_console_conf.scrdata = type;
    741 
    742 	wsemul = wsemul_pick(0); /* default */
    743 	wsdisplay_console_conf.wsemul = wsemul;
    744 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    745 								  ccol, crow,
    746 								  defattr);
    747 
    748 	cn_tab = &wsdisplay_cons;
    749 	wsdisplay_console_initted = 1;
    750 }
    751 
    752 /*
    753  * Tty and cdevsw functions.
    754  */
    755 int
    756 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
    757 {
    758 	struct wsdisplay_softc *sc;
    759 	struct tty *tp;
    760 	int newopen, error;
    761 	struct wsscreen *scr;
    762 
    763 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    764 	if (sc == NULL)			/* make sure it was attached */
    765 		return (ENXIO);
    766 
    767 	if (ISWSDISPLAYSTAT(dev)) {
    768 		wsevent_init(&sc->evar, l->l_proc);
    769 		return (0);
    770 	}
    771 
    772 	if (ISWSDISPLAYCTL(dev))
    773 		return (0);
    774 
    775 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    776 		return (ENXIO);
    777 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    778 		return (ENXIO);
    779 
    780 	if (WSSCREEN_HAS_TTY(scr)) {
    781 		tp = scr->scr_tty;
    782 		tp->t_oproc = wsdisplaystart;
    783 		tp->t_param = wsdisplayparam;
    784 		tp->t_dev = dev;
    785 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    786 
    787 		if (kauth_authorize_device_tty(l->l_cred,
    788 			KAUTH_DEVICE_TTY_OPEN, tp))
    789 			return (EBUSY);
    790 
    791 		if (newopen) {
    792 			ttychars(tp);
    793 			tp->t_iflag = TTYDEF_IFLAG;
    794 			tp->t_oflag = TTYDEF_OFLAG;
    795 			tp->t_cflag = TTYDEF_CFLAG;
    796 			tp->t_lflag = TTYDEF_LFLAG;
    797 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    798 			wsdisplayparam(tp, &tp->t_termios);
    799 			ttsetwater(tp);
    800 		}
    801 		tp->t_state |= TS_CARR_ON;
    802 
    803 		error = ((*tp->t_linesw->l_open)(dev, tp));
    804 		if (error)
    805 			return (error);
    806 
    807 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    808 			/* set window sizes as appropriate, and reset
    809 			 the emulation */
    810 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    811 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    812 
    813 			/* wsdisplay_set_emulation() */
    814 		}
    815 	}
    816 
    817 	scr->scr_flags |= SCR_OPEN;
    818 	return (0);
    819 }
    820 
    821 int
    822 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
    823 {
    824 	struct wsdisplay_softc *sc;
    825 	struct tty *tp;
    826 	struct wsscreen *scr;
    827 
    828 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    829 
    830 	if (ISWSDISPLAYSTAT(dev)) {
    831 		wsevent_fini(&sc->evar);
    832 		return (0);
    833 	}
    834 
    835 	if (ISWSDISPLAYCTL(dev))
    836 		return (0);
    837 
    838 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    839 		return (0);
    840 
    841 	if (WSSCREEN_HAS_TTY(scr)) {
    842 		if (scr->scr_hold_screen) {
    843 			int s;
    844 
    845 			/* XXX RESET KEYBOARD LEDS, etc. */
    846 			s = spltty();	/* avoid conflict with keyboard */
    847 			wsdisplay_kbdholdscreen((struct device *)sc, 0);
    848 			splx(s);
    849 		}
    850 		tp = scr->scr_tty;
    851 		(*tp->t_linesw->l_close)(tp, flag);
    852 		ttyclose(tp);
    853 	}
    854 
    855 	if (scr->scr_syncops)
    856 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    857 
    858 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    859 		scr->scr_flags &= ~SCR_GRAPHICS;
    860 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    861 						 WSEMUL_RESET);
    862 		if (wsdisplay_clearonclose)
    863 			(*scr->scr_dconf->wsemul->reset)
    864 				(scr->scr_dconf->wsemulcookie,
    865 				 WSEMUL_CLEARSCREEN);
    866 	}
    867 
    868 #ifdef WSDISPLAY_COMPAT_RAWKBD
    869 	if (scr->scr_rawkbd) {
    870 		int kbmode = WSKBD_TRANSLATED;
    871 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    872 					       (void *)&kbmode, 0, l);
    873 	}
    874 #endif
    875 
    876 	scr->scr_flags &= ~SCR_OPEN;
    877 
    878 	return (0);
    879 }
    880 
    881 int
    882 wsdisplayread(dev_t dev, struct uio *uio, int flag)
    883 {
    884 	struct wsdisplay_softc *sc;
    885 	struct tty *tp;
    886 	struct wsscreen *scr;
    887 	int error;
    888 
    889 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    890 
    891 	if (ISWSDISPLAYSTAT(dev)) {
    892 		error = wsevent_read(&sc->evar, uio, flag);
    893 		return (error);
    894 	}
    895 
    896 	if (ISWSDISPLAYCTL(dev))
    897 		return (0);
    898 
    899 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    900 		return (ENXIO);
    901 
    902 	if (!WSSCREEN_HAS_TTY(scr))
    903 		return (ENODEV);
    904 
    905 	tp = scr->scr_tty;
    906 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    907 }
    908 
    909 int
    910 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
    911 {
    912 	struct wsdisplay_softc *sc;
    913 	struct tty *tp;
    914 	struct wsscreen *scr;
    915 
    916 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    917 
    918 	if (ISWSDISPLAYSTAT(dev)) {
    919 		return (0);
    920 	}
    921 
    922 	if (ISWSDISPLAYCTL(dev))
    923 		return (0);
    924 
    925 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    926 		return (ENXIO);
    927 
    928 	if (!WSSCREEN_HAS_TTY(scr))
    929 		return (ENODEV);
    930 
    931 	tp = scr->scr_tty;
    932 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    933 }
    934 
    935 int
    936 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
    937 {
    938 	struct wsdisplay_softc *sc;
    939 	struct tty *tp;
    940 	struct wsscreen *scr;
    941 
    942 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    943 
    944 	if (ISWSDISPLAYSTAT(dev))
    945 		return (wsevent_poll(&sc->evar, events, l));
    946 
    947 	if (ISWSDISPLAYCTL(dev))
    948 		return (0);
    949 
    950 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    951 		return (POLLHUP);
    952 
    953 	if (!WSSCREEN_HAS_TTY(scr))
    954 		return (POLLERR);
    955 
    956 	tp = scr->scr_tty;
    957 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    958 }
    959 
    960 int
    961 wsdisplaykqfilter(dev, kn)
    962 	dev_t dev;
    963 	struct knote *kn;
    964 {
    965 	struct wsdisplay_softc *sc =
    966 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    967 	struct wsscreen *scr;
    968 
    969 	if (ISWSDISPLAYCTL(dev))
    970 		return (1);
    971 
    972 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    973 		return (1);
    974 
    975 
    976 	if (WSSCREEN_HAS_TTY(scr))
    977 		return (ttykqfilter(dev, kn));
    978 	else
    979 		return (1);
    980 }
    981 
    982 struct tty *
    983 wsdisplaytty(dev_t dev)
    984 {
    985 	struct wsdisplay_softc *sc;
    986 	struct wsscreen *scr;
    987 
    988 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    989 
    990 	if (ISWSDISPLAYSTAT(dev))
    991 		panic("wsdisplaytty() on status device");
    992 
    993 	if (ISWSDISPLAYCTL(dev))
    994 		panic("wsdisplaytty() on ctl device");
    995 
    996 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    997 		return NULL;
    998 
    999 	return (scr->scr_tty);
   1000 }
   1001 
   1002 int
   1003 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1004 {
   1005 	struct wsdisplay_softc *sc;
   1006 	struct tty *tp;
   1007 	int error;
   1008 	struct wsscreen *scr;
   1009 
   1010 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1011 
   1012 #ifdef WSDISPLAY_COMPAT_USL
   1013 	error = wsdisplay_usl_ioctl1(sc, cmd, data, flag, l);
   1014 	if (error != EPASSTHROUGH)
   1015 		return (error);
   1016 #endif
   1017 
   1018 	if (ISWSDISPLAYSTAT(dev))
   1019 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
   1020 
   1021 	if (ISWSDISPLAYCTL(dev))
   1022 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
   1023 
   1024 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1025 		return (ENXIO);
   1026 
   1027 	if (WSSCREEN_HAS_TTY(scr)) {
   1028 		tp = scr->scr_tty;
   1029 
   1030 /* printf("disc\n"); */
   1031 		/* do the line discipline ioctls first */
   1032 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1033 		if (error != EPASSTHROUGH)
   1034 			return (error);
   1035 
   1036 /* printf("tty\n"); */
   1037 		/* then the tty ioctls */
   1038 		error = ttioctl(tp, cmd, data, flag, l);
   1039 		if (error != EPASSTHROUGH)
   1040 			return (error);
   1041 	}
   1042 
   1043 #ifdef WSDISPLAY_COMPAT_USL
   1044 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
   1045 	if (error != EPASSTHROUGH)
   1046 		return (error);
   1047 #endif
   1048 
   1049 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
   1050 }
   1051 
   1052 int
   1053 wsdisplay_param(struct device *dev, u_long cmd, struct wsdisplay_param *dp)
   1054 {
   1055 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1056 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1057 					   sc->sc_focus->scr_dconf->emulcookie,
   1058 					   cmd, (void *)dp, 0, NULL));
   1059 }
   1060 
   1061 int
   1062 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1063 	u_long cmd, void *data, int flag, struct lwp *l)
   1064 {
   1065 	int error;
   1066 	char namebuf[16];
   1067 	struct wsdisplay_font fd;
   1068 #ifdef WSDISPLAY_SCROLLSUPPORT
   1069 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1070 #endif
   1071 
   1072 #if NWSKBD > 0
   1073 	struct wsevsrc *inp;
   1074 
   1075 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1076 	switch (cmd) {
   1077 	case WSKBDIO_SETMODE:
   1078 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1079 		return (wsdisplay_update_rawkbd(sc, scr));
   1080 	case WSKBDIO_GETMODE:
   1081 		*(int *)data = (scr->scr_rawkbd ?
   1082 				WSKBD_RAW : WSKBD_TRANSLATED);
   1083 		return (0);
   1084 	}
   1085 #endif
   1086 	inp = sc->sc_input;
   1087 	if (inp == NULL)
   1088 		return (ENXIO);
   1089 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1090 	if (error != EPASSTHROUGH)
   1091 		return (error);
   1092 #endif /* NWSKBD > 0 */
   1093 
   1094 	switch (cmd) {
   1095 	case WSDISPLAYIO_GMODE:
   1096 		if (scr->scr_flags & SCR_GRAPHICS) {
   1097 			if (scr->scr_flags & SCR_DUMBFB)
   1098 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1099 			else
   1100 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1101 		} else
   1102 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1103 		return (0);
   1104 
   1105 	case WSDISPLAYIO_SMODE:
   1106 #define d (*(int *)data)
   1107 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1108 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1109 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1110 			return (EINVAL);
   1111 
   1112 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1113 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1114 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1115 			d == WSDISPLAYIO_MODE_DUMBFB)
   1116 			    scr->scr_flags |= SCR_GRAPHICS |
   1117 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1118 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1119 		    return (EINVAL);
   1120 
   1121 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1122 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
   1123 
   1124 	    return (0);
   1125 #undef d
   1126 
   1127 #ifdef WSDISPLAY_SCROLLSUPPORT
   1128 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1129     do {								\
   1130 	(dstp)->fastlines = ((srcp)->which &				\
   1131 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1132 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1133 	(dstp)->slowlines = ((srcp)->which &				\
   1134 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1135 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1136 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1137     } while (0)
   1138 
   1139 
   1140 	case WSDISPLAYIO_DSSCROLL:
   1141 		usdp = (struct wsdisplay_scroll_data *)data;
   1142 		ksdp = &sc->sc_scroll_values;
   1143 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1144 		return (0);
   1145 
   1146 	case WSDISPLAYIO_DGSCROLL:
   1147 		usdp = (struct wsdisplay_scroll_data *)data;
   1148 		ksdp = &sc->sc_scroll_values;
   1149 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1150 		return (0);
   1151 #else
   1152 	case WSDISPLAYIO_DSSCROLL:
   1153 	case WSDISPLAYIO_DGSCROLL:
   1154 		return ENODEV;
   1155 #endif
   1156 
   1157 	case WSDISPLAYIO_SFONT:
   1158 #define d ((struct wsdisplay_usefontdata *)data)
   1159 		if (!sc->sc_accessops->load_font)
   1160 			return (EINVAL);
   1161 		if (d->name) {
   1162 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1163 			if (error)
   1164 				return (error);
   1165 			fd.name = namebuf;
   1166 		} else
   1167 			fd.name = 0;
   1168 		fd.data = 0;
   1169 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1170 					scr->scr_dconf->emulcookie, &fd);
   1171 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1172 			(*scr->scr_dconf->wsemul->reset)
   1173 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1174 		return (error);
   1175 #undef d
   1176 
   1177 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1178 	case WSDISPLAYIO_GMSGATTRS:
   1179 #define d ((struct wsdisplay_msgattrs *)data)
   1180 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1181 		    (scr->scr_dconf->wsemulcookie, d);
   1182 		return (0);
   1183 #undef d
   1184 
   1185 	case WSDISPLAYIO_SMSGATTRS: {
   1186 #define d ((struct wsdisplay_msgattrs *)data)
   1187 		int i;
   1188 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1189 			if (sc->sc_scr[i] != NULL)
   1190 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1191 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1192 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1193 				     d);
   1194 		}
   1195 		return (0);
   1196 #undef d
   1197 #else
   1198 	case WSDISPLAYIO_GMSGATTRS:
   1199 	case WSDISPLAYIO_SMSGATTRS:
   1200 		return (ENODEV);
   1201 #endif
   1202 	}
   1203 
   1204 	/* check ioctls for display */
   1205 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1206 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
   1207 }
   1208 
   1209 int
   1210 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1211 	int flag, struct lwp *l)
   1212 {
   1213 	switch (cmd) {
   1214 	case WSDISPLAYIO_GETACTIVESCREEN:
   1215 		*(int*)data = wsdisplay_getactivescreen(sc);
   1216 		return (0);
   1217 	}
   1218 
   1219 	return (EPASSTHROUGH);
   1220 }
   1221 
   1222 int
   1223 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1224 	int flag, struct lwp *l)
   1225 {
   1226 	int error;
   1227 	char *type, typebuf[16], *emul, emulbuf[16];
   1228 	void *tbuf;
   1229 	u_int fontsz;
   1230 #if defined(COMPAT_14) && NWSKBD > 0
   1231 	struct wsmux_device wsmuxdata;
   1232 #endif
   1233 #if NWSKBD > 0
   1234 	struct wsevsrc *inp;
   1235 #endif
   1236 
   1237 	switch (cmd) {
   1238 	case WSDISPLAYIO_ADDSCREEN:
   1239 #define d ((struct wsdisplay_addscreendata *)data)
   1240 		if (d->screentype) {
   1241 			error = copyinstr(d->screentype, typebuf,
   1242 					  sizeof(typebuf), 0);
   1243 			if (error)
   1244 				return (error);
   1245 			type = typebuf;
   1246 		} else
   1247 			type = 0;
   1248 		if (d->emul) {
   1249 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1250 			if (error)
   1251 				return (error);
   1252 			emul = emulbuf;
   1253 		} else
   1254 			emul = 0;
   1255 
   1256 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1257 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1258 		return (error);
   1259 #undef d
   1260 	case WSDISPLAYIO_DELSCREEN:
   1261 #define d ((struct wsdisplay_delscreendata *)data)
   1262 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1263 #undef d
   1264 	case WSDISPLAYIO_LDFONT:
   1265 #define d ((struct wsdisplay_font *)data)
   1266 		if (!sc->sc_accessops->load_font)
   1267 			return (EINVAL);
   1268 		if (d->name) {
   1269 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1270 			if (error)
   1271 				return (error);
   1272 			d->name = typebuf;
   1273 		} else
   1274 			d->name = "loaded"; /* ??? */
   1275 		fontsz = d->fontheight * d->stride * d->numchars;
   1276 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1277 			return (EINVAL);
   1278 
   1279 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1280 		error = copyin(d->data, tbuf, fontsz);
   1281 		if (error) {
   1282 			free(tbuf, M_DEVBUF);
   1283 			return (error);
   1284 		}
   1285 		d->data = tbuf;
   1286 		error =
   1287 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1288 		free(tbuf, M_DEVBUF);
   1289 #undef d
   1290 		return (error);
   1291 
   1292 #if NWSKBD > 0
   1293 #ifdef COMPAT_14
   1294 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1295 #define d ((struct wsdisplay_kbddata *)data)
   1296 		inp = sc->sc_input;
   1297 		if (inp == NULL)
   1298 			return (ENXIO);
   1299 		switch (d->op) {
   1300 		case _O_WSDISPLAY_KBD_ADD:
   1301 			if (d->idx == -1) {
   1302 				d->idx = wskbd_pickfree();
   1303 				if (d->idx == -1)
   1304 					return (ENXIO);
   1305 			}
   1306 			wsmuxdata.type = WSMUX_KBD;
   1307 			wsmuxdata.idx = d->idx;
   1308 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1309 					      &wsmuxdata, flag, l));
   1310 		case _O_WSDISPLAY_KBD_DEL:
   1311 			wsmuxdata.type = WSMUX_KBD;
   1312 			wsmuxdata.idx = d->idx;
   1313 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1314 					      &wsmuxdata, flag, l));
   1315 		default:
   1316 			return (EINVAL);
   1317 		}
   1318 #undef d
   1319 #endif
   1320 
   1321 	case WSMUXIO_ADD_DEVICE:
   1322 #define d ((struct wsmux_device *)data)
   1323 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1324 			d->idx = wskbd_pickfree();
   1325 #undef d
   1326 		/* fall into */
   1327 	case WSMUXIO_INJECTEVENT:
   1328 	case WSMUXIO_REMOVE_DEVICE:
   1329 	case WSMUXIO_LIST_DEVICES:
   1330 		inp = sc->sc_input;
   1331 		if (inp == NULL)
   1332 			return (ENXIO);
   1333 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1334 #endif /* NWSKBD > 0 */
   1335 
   1336 	}
   1337 	return (EPASSTHROUGH);
   1338 }
   1339 
   1340 int
   1341 wsdisplay_stat_inject(struct device *dev, u_int type, int value)
   1342 {
   1343 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *) dev;
   1344 	struct wseventvar *evar;
   1345 	struct wscons_event event;
   1346 
   1347 	evar = &sc->evar;
   1348 
   1349 	if (evar == NULL)
   1350 		return (0);
   1351 
   1352 	if (evar->q == NULL)
   1353 		return (1);
   1354 
   1355 	event.type = type;
   1356 	event.value = value;
   1357 	if (wsevent_inject(evar, &event, 1) != 0) {
   1358 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1359 		return (1);
   1360 	}
   1361 
   1362 	return (0);
   1363 }
   1364 
   1365 paddr_t
   1366 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1367 {
   1368 	struct wsdisplay_softc *sc =
   1369 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1370 	struct wsscreen *scr;
   1371 
   1372 	if (ISWSDISPLAYSTAT(dev))
   1373 		return (-1);
   1374 
   1375 	if (ISWSDISPLAYCTL(dev))
   1376 		return (-1);
   1377 
   1378 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1379 		return (-1);
   1380 
   1381 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1382 		return (-1);
   1383 
   1384 	/* pass mmap to display */
   1385 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
   1386 	    scr->scr_dconf->emulcookie, 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 		splx(s);
   1577 		return (ENXIO);
   1578 	}
   1579 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1580 	if (!error)
   1581 		sc->sc_rawkbd = raw;
   1582 	splx(s);
   1583 	return (error);
   1584 #else
   1585 	return (0);
   1586 #endif
   1587 }
   1588 #endif
   1589 
   1590 int
   1591 wsdisplay_switch3(void *arg, int error, int waitok)
   1592 {
   1593 	struct wsdisplay_softc *sc = arg;
   1594 	int no;
   1595 	struct wsscreen *scr;
   1596 
   1597 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1598 		printf("wsdisplay_switch3: not switching\n");
   1599 		return (EINVAL);
   1600 	}
   1601 
   1602 	no = sc->sc_screenwanted;
   1603 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1604 		panic("wsdisplay_switch3: invalid screen %d", no);
   1605 	scr = sc->sc_scr[no];
   1606 	if (!scr) {
   1607 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1608 		error = ENXIO;
   1609 	}
   1610 
   1611 	if (error) {
   1612 		/* try to recover, avoid recursion */
   1613 
   1614 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1615 			printf("wsdisplay_switch3: giving up\n");
   1616 			sc->sc_focus = 0;
   1617 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1618 			wsdisplay_update_rawkbd(sc, 0);
   1619 #endif
   1620 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1621 			return (error);
   1622 		}
   1623 
   1624 		sc->sc_screenwanted = sc->sc_oldscreen;
   1625 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1626 		return (wsdisplay_switch1(arg, 0, waitok));
   1627 	}
   1628 
   1629 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1630 
   1631 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1632 		wakeup(scr);
   1633 	return (error);
   1634 }
   1635 
   1636 int
   1637 wsdisplay_switch2(void *arg, int error, int waitok)
   1638 {
   1639 	struct wsdisplay_softc *sc = arg;
   1640 	int no;
   1641 	struct wsscreen *scr;
   1642 
   1643 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1644 		printf("wsdisplay_switch2: not switching\n");
   1645 		return (EINVAL);
   1646 	}
   1647 
   1648 	no = sc->sc_screenwanted;
   1649 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1650 		panic("wsdisplay_switch2: invalid screen %d", no);
   1651 	scr = sc->sc_scr[no];
   1652 	if (!scr) {
   1653 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
   1654 		error = ENXIO;
   1655 	}
   1656 
   1657 	if (error) {
   1658 		/* try to recover, avoid recursion */
   1659 
   1660 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1661 			printf("wsdisplay_switch2: giving up\n");
   1662 			sc->sc_focus = 0;
   1663 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1664 			return (error);
   1665 		}
   1666 
   1667 		sc->sc_screenwanted = sc->sc_oldscreen;
   1668 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1669 		return (wsdisplay_switch1(arg, 0, waitok));
   1670 	}
   1671 
   1672 	sc->sc_focusidx = no;
   1673 	sc->sc_focus = scr;
   1674 
   1675 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1676 	(void) wsdisplay_update_rawkbd(sc, scr);
   1677 #endif
   1678 	/* keyboard map??? */
   1679 
   1680 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
   1681 	if (scr->scr_syncops) {
   1682 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1683 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
   1684 		if (error == EAGAIN) {
   1685 			/* switch will be done asynchronously */
   1686 			return (0);
   1687 		}
   1688 	}
   1689 
   1690 	return (wsdisplay_switch3(sc, error, waitok));
   1691 }
   1692 
   1693 int
   1694 wsdisplay_switch1(void *arg, int error, int waitok)
   1695 {
   1696 	struct wsdisplay_softc *sc = arg;
   1697 	int no;
   1698 	struct wsscreen *scr;
   1699 
   1700 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1701 		printf("wsdisplay_switch1: not switching\n");
   1702 		return (EINVAL);
   1703 	}
   1704 
   1705 	no = sc->sc_screenwanted;
   1706 	if (no == WSDISPLAY_NULLSCREEN) {
   1707 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1708 		if (!error) {
   1709 			sc->sc_focus = 0;
   1710 		}
   1711 		wakeup(sc);
   1712 		return (error);
   1713 	}
   1714 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1715 		panic("wsdisplay_switch1: invalid screen %d", no);
   1716 	scr = sc->sc_scr[no];
   1717 	if (!scr) {
   1718 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1719 		error = ENXIO;
   1720 	}
   1721 
   1722 	if (error) {
   1723 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1724 		return (error);
   1725 	}
   1726 
   1727 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
   1728 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1729 						 scr->scr_dconf->emulcookie,
   1730 						 waitok,
   1731 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
   1732 	if (error == EAGAIN) {
   1733 		/* switch will be done asynchronously */
   1734 		return (0);
   1735 	}
   1736 
   1737 	return (wsdisplay_switch2(sc, error, waitok));
   1738 }
   1739 
   1740 int
   1741 wsdisplay_switch(struct device *dev, int no, int waitok)
   1742 {
   1743 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1744 	int s, res = 0;
   1745 	struct wsscreen *scr;
   1746 
   1747 	if (no != WSDISPLAY_NULLSCREEN) {
   1748 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
   1749 			return (EINVAL);
   1750 		if (sc->sc_scr[no] == NULL)
   1751 			return (ENXIO);
   1752 	}
   1753 
   1754 	wsdisplay_stat_inject(dev, WSCONS_EVENT_SCREEN_SWITCH, no);
   1755 
   1756 	s = spltty();
   1757 
   1758 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   1759 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   1760 		splx(s);
   1761 		return (0);
   1762 	}
   1763 
   1764 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1765 		splx(s);
   1766 		return (EBUSY);
   1767 	}
   1768 
   1769 	sc->sc_flags |= SC_SWITCHPENDING;
   1770 	sc->sc_screenwanted = no;
   1771 
   1772 	splx(s);
   1773 
   1774 	scr = sc->sc_focus;
   1775 	if (!scr) {
   1776 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1777 		return (wsdisplay_switch1(sc, 0, waitok));
   1778 	} else
   1779 		sc->sc_oldscreen = sc->sc_focusidx;
   1780 
   1781 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
   1782 	if (scr->scr_syncops) {
   1783 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1784 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
   1785 		if (res == EAGAIN) {
   1786 			/* switch will be done asynchronously */
   1787 			return (0);
   1788 		}
   1789 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1790 		/* no way to save state */
   1791 		res = EBUSY;
   1792 	}
   1793 
   1794 	return (wsdisplay_switch1(sc, res, waitok));
   1795 }
   1796 
   1797 void
   1798 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
   1799 {
   1800 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1801 	struct wsscreen *scr;
   1802 
   1803 	KASSERT(sc != NULL);
   1804 	scr = sc->sc_focus;
   1805 
   1806 	if (!scr)
   1807 		return;
   1808 
   1809 	switch (op) {
   1810 	case WSDISPLAY_RESETEMUL:
   1811 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1812 			break;
   1813 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1814 						 WSEMUL_RESET);
   1815 		break;
   1816 	case WSDISPLAY_RESETCLOSE:
   1817 		wsdisplay_closescreen(sc, scr);
   1818 		break;
   1819 	}
   1820 }
   1821 
   1822 /*
   1823  * Interface for (external) VT switch / process synchronization code
   1824  */
   1825 int
   1826 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   1827 	void *cookie)
   1828 {
   1829 	if (scr->scr_syncops) {
   1830 		/*
   1831 		 * The screen is already claimed.
   1832 		 * Check if the owner is still alive.
   1833 		 */
   1834 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1835 			return (EBUSY);
   1836 	}
   1837 	scr->scr_syncops = ops;
   1838 	scr->scr_synccookie = cookie;
   1839 	return (0);
   1840 }
   1841 
   1842 int
   1843 wsscreen_detach_sync(struct wsscreen *scr)
   1844 {
   1845 	if (!scr->scr_syncops)
   1846 		return (EINVAL);
   1847 	scr->scr_syncops = 0;
   1848 	return (0);
   1849 }
   1850 
   1851 int
   1852 wsscreen_lookup_sync(struct wsscreen *scr,
   1853 	const struct wscons_syncops *ops, /* used as ID */
   1854 	void **cookiep)
   1855 {
   1856 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1857 		return (EINVAL);
   1858 	*cookiep = scr->scr_synccookie;
   1859 	return (0);
   1860 }
   1861 
   1862 /*
   1863  * Interface to virtual screen stuff
   1864  */
   1865 int
   1866 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   1867 {
   1868 	return (WSDISPLAY_MAXSCREEN - 1);
   1869 }
   1870 
   1871 int
   1872 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   1873 {
   1874 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   1875 		return (EINVAL);
   1876 	if (!sc->sc_scr[idx])
   1877 		return (ENXIO);
   1878 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1879 }
   1880 
   1881 int
   1882 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   1883 {
   1884 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   1885 }
   1886 
   1887 int
   1888 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   1889 {
   1890 	struct wsscreen *scr;
   1891 	int s, res = 0;
   1892 
   1893 	if (no == WSDISPLAY_NULLSCREEN) {
   1894 		s = spltty();
   1895 		while (sc->sc_focus && res == 0) {
   1896 			res = tsleep(sc, PCATCH, "wswait", 0);
   1897 		}
   1898 		splx(s);
   1899 		return (res);
   1900 	}
   1901 
   1902 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1903 		return (ENXIO);
   1904 	scr = sc->sc_scr[no];
   1905 	if (!scr)
   1906 		return (ENXIO);
   1907 
   1908 	s = spltty();
   1909 	if (scr != sc->sc_focus) {
   1910 		scr->scr_flags |= SCR_WAITACTIVE;
   1911 		res = tsleep(scr, PCATCH, "wswait", 0);
   1912 		if (scr != sc->sc_scr[no])
   1913 			res = ENXIO; /* disappeared in the meantime */
   1914 		else
   1915 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1916 	}
   1917 	splx(s);
   1918 	return (res);
   1919 }
   1920 
   1921 void
   1922 wsdisplay_kbdholdscreen(struct device *dev, int hold)
   1923 {
   1924 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1925 	struct wsscreen *scr;
   1926 
   1927 	scr = sc->sc_focus;
   1928 
   1929 	if (hold)
   1930 		scr->scr_hold_screen = 1;
   1931 	else {
   1932 		scr->scr_hold_screen = 0;
   1933 		callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
   1934 		    ttrstrt, scr->scr_tty);		/* "immediate" */
   1935 	}
   1936 }
   1937 
   1938 #if NWSKBD > 0
   1939 void
   1940 wsdisplay_set_console_kbd(struct wsevsrc *src)
   1941 {
   1942 	if (wsdisplay_console_device == NULL) {
   1943 		src->me_dispdv = NULL;
   1944 		return;
   1945 	}
   1946 #if NWSMUX > 0
   1947 	if (wsmux_attach_sc((struct wsmux_softc *)
   1948 			    wsdisplay_console_device->sc_input, src)) {
   1949 		src->me_dispdv = NULL;
   1950 		return;
   1951 	}
   1952 #else
   1953 	wsdisplay_console_device->sc_input = src;
   1954 #endif
   1955 	src->me_dispdv = &wsdisplay_console_device->sc_dv;
   1956 }
   1957 #endif /* NWSKBD > 0 */
   1958 
   1959 /*
   1960  * Console interface.
   1961  */
   1962 void
   1963 wsdisplay_cnputc(dev_t dev, int i)
   1964 {
   1965 	struct wsscreen_internal *dc;
   1966 	u_char c = i;
   1967 
   1968 	if (!wsdisplay_console_initted)
   1969 		return;
   1970 
   1971 	if ((wsdisplay_console_device != NULL) &&
   1972 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   1973 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1974 		return;
   1975 
   1976 	dc = &wsdisplay_console_conf;
   1977 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1978 }
   1979 
   1980 static int
   1981 wsdisplay_getc_dummy(dev_t dev)
   1982 {
   1983 	/* panic? */
   1984 	return (0);
   1985 }
   1986 
   1987 static void
   1988 wsdisplay_pollc(dev_t dev, int on)
   1989 {
   1990 
   1991 	wsdisplay_cons_pollmode = on;
   1992 
   1993 	/* notify to fb drivers */
   1994 	if (wsdisplay_console_device != NULL &&
   1995 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   1996 		(*wsdisplay_console_device->sc_accessops->pollc)
   1997 			(wsdisplay_console_device->sc_accesscookie, on);
   1998 
   1999 	/* notify to kbd drivers */
   2000 	if (wsdisplay_cons_kbd_pollc)
   2001 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   2002 }
   2003 
   2004 void
   2005 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   2006 	void (*bell)(dev_t, u_int, u_int, u_int))
   2007 {
   2008 	wsdisplay_cons.cn_getc = get;
   2009 	wsdisplay_cons.cn_bell = bell;
   2010 	wsdisplay_cons_kbd_pollc = poll;
   2011 }
   2012 
   2013 void
   2014 wsdisplay_unset_cons_kbd(void)
   2015 {
   2016 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   2017 	wsdisplay_cons.cn_bell = NULL;
   2018 	wsdisplay_cons_kbd_pollc = 0;
   2019 }
   2020 
   2021 /*
   2022  * Switch the console display to it's first screen.
   2023  */
   2024 void
   2025 wsdisplay_switchtoconsole(void)
   2026 {
   2027 	struct wsdisplay_softc *sc;
   2028 	struct wsscreen *scr;
   2029 
   2030 	if (wsdisplay_console_device != NULL) {
   2031 		sc = wsdisplay_console_device;
   2032 		if ((scr = sc->sc_scr[0]) == NULL)
   2033 			return;
   2034 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   2035 						 scr->scr_dconf->emulcookie,
   2036 						 0, NULL, NULL);
   2037 	}
   2038 }
   2039 
   2040 /*
   2041  * Switch the console at shutdown.
   2042  */
   2043 static void
   2044 wsdisplay_shutdownhook(void *arg)
   2045 {
   2046 
   2047 	wsdisplay_switchtoconsole();
   2048 }
   2049