Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.90.2.3
      1 /* $NetBSD: wsdisplay.c,v 1.90.2.3 2006/04/19 03:26:39 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.3 2006/04/19 03:26:39 elad 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 
     59 #include <dev/wscons/wseventvar.h>
     60 #include <dev/wscons/wsmuxvar.h>
     61 #include <dev/wscons/wsconsio.h>
     62 #include <dev/wscons/wsdisplayvar.h>
     63 #include <dev/wscons/wsksymvar.h>
     64 #include <dev/wscons/wsksymdef.h>
     65 #include <dev/wscons/wsemulvar.h>
     66 #include <dev/wscons/wscons_callbacks.h>
     67 #include <dev/cons.h>
     68 
     69 struct wsscreen_internal {
     70 	const struct wsdisplay_emulops *emulops;
     71 	void	*emulcookie;
     72 
     73 	const struct wsscreen_descr *scrdata;
     74 
     75 	const struct wsemul_ops *wsemul;
     76 	void	*wsemulcookie;
     77 };
     78 
     79 struct wsscreen {
     80 	struct wsscreen_internal *scr_dconf;
     81 
     82 	struct tty *scr_tty;
     83 	int	scr_hold_screen;		/* hold tty output */
     84 
     85 	int scr_flags;
     86 #define SCR_OPEN 1		/* is it open? */
     87 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     88 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     89 #define	SCR_DUMBFB 8		/* in use as a dumb fb (iff SCR_GRAPHICS) */
     90 	const struct wscons_syncops *scr_syncops;
     91 	void *scr_synccookie;
     92 
     93 #ifdef WSDISPLAY_COMPAT_RAWKBD
     94 	int scr_rawkbd;
     95 #endif
     96 
     97 	struct wsdisplay_softc *sc;
     98 };
     99 
    100 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int,
    101 				 const char *,
    102 				 const struct wsscreen_descr *, void *,
    103 				 int, int, long);
    104 void wsscreen_detach(struct wsscreen *);
    105 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
    106 static void wsdisplay_shutdownhook(void *);
    107 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
    108 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
    109 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
    110 
    111 #define WSDISPLAY_MAXSCREEN 8
    112 
    113 struct wsdisplay_softc {
    114 	struct device sc_dv;
    115 
    116 	const struct wsdisplay_accessops *sc_accessops;
    117 	void	*sc_accesscookie;
    118 
    119 	const struct wsscreen_list *sc_scrdata;
    120 #ifdef WSDISPLAY_SCROLLSUPPORT
    121 	struct wsdisplay_scroll_data sc_scroll_values;
    122 #endif
    123 
    124 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    125 	int sc_focusidx;	/* available only if sc_focus isn't null */
    126 	struct wsscreen *sc_focus;
    127 
    128 	struct wseventvar evar;
    129 
    130 	int	sc_isconsole;
    131 
    132 	int sc_flags;
    133 #define SC_SWITCHPENDING 1
    134 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
    135 
    136 #if NWSKBD > 0
    137 	struct wsevsrc *sc_input;
    138 #ifdef WSDISPLAY_COMPAT_RAWKBD
    139 	int sc_rawkbd;
    140 #endif
    141 #endif /* NWSKBD > 0 */
    142 };
    143 
    144 #ifdef WSDISPLAY_SCROLLSUPPORT
    145 
    146 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
    147 	WSDISPLAY_SCROLL_DOALL,
    148 	25,
    149 	2,
    150 };
    151 #endif
    152 
    153 extern struct cfdriver wsdisplay_cd;
    154 
    155 /* Autoconfiguration definitions. */
    156 static int wsdisplay_emul_match(struct device *, struct cfdata *, void *);
    157 static void wsdisplay_emul_attach(struct device *, struct device *, void *);
    158 static int wsdisplay_noemul_match(struct device *, struct cfdata *, void *);
    159 static void wsdisplay_noemul_attach(struct device *, struct device *, void *);
    160 
    161 CFATTACH_DECL(wsdisplay_emul, sizeof (struct wsdisplay_softc),
    162     wsdisplay_emul_match, wsdisplay_emul_attach, NULL, NULL);
    163 
    164 CFATTACH_DECL(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
    165     wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
    166 
    167 dev_type_open(wsdisplayopen);
    168 dev_type_close(wsdisplayclose);
    169 dev_type_read(wsdisplayread);
    170 dev_type_write(wsdisplaywrite);
    171 dev_type_ioctl(wsdisplayioctl);
    172 dev_type_stop(wsdisplaystop);
    173 dev_type_tty(wsdisplaytty);
    174 dev_type_poll(wsdisplaypoll);
    175 dev_type_mmap(wsdisplaymmap);
    176 dev_type_kqfilter(wsdisplaykqfilter);
    177 
    178 const struct cdevsw wsdisplay_cdevsw = {
    179 	wsdisplayopen, wsdisplayclose, wsdisplayread, wsdisplaywrite,
    180 	wsdisplayioctl, wsdisplaystop, wsdisplaytty, wsdisplaypoll,
    181 	wsdisplaymmap, wsdisplaykqfilter, D_TTY
    182 };
    183 
    184 static void wsdisplaystart(struct tty *);
    185 static int wsdisplayparam(struct tty *, struct termios *);
    186 
    187 
    188 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    189 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    190 #define ISWSDISPLAYSTAT(dev)	(WSDISPLAYSCREEN(dev) == 254)
    191 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    192 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    193 
    194 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    195 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    196 
    197 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
    198 	    int console, int kbdmux, const struct wsscreen_list *,
    199 	    const struct wsdisplay_accessops *accessops,
    200 	    void *accesscookie);
    201 
    202 #ifdef WSDISPLAY_COMPAT_RAWKBD
    203 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
    204 				 struct wsscreen *);
    205 #endif
    206 
    207 static int wsdisplay_console_initted;
    208 static int wsdisplay_console_attached;
    209 static struct wsdisplay_softc *wsdisplay_console_device;
    210 static struct wsscreen_internal wsdisplay_console_conf;
    211 
    212 static int wsdisplay_getc_dummy(dev_t);
    213 static void wsdisplay_pollc(dev_t, int);
    214 
    215 static int wsdisplay_cons_pollmode;
    216 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
    217 
    218 static struct consdev wsdisplay_cons = {
    219 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
    220 	wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
    221 };
    222 
    223 #ifndef WSDISPLAY_DEFAULTSCREENS
    224 # define WSDISPLAY_DEFAULTSCREENS	0
    225 #endif
    226 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
    227 
    228 int wsdisplay_switch1(void *, int, int);
    229 int wsdisplay_switch2(void *, int, int);
    230 int wsdisplay_switch3(void *, int, int);
    231 
    232 int wsdisplay_clearonclose;
    233 
    234 struct wsscreen *
    235 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
    236 	const struct wsscreen_descr *type, void *cookie, int ccol,
    237 	int crow, long defattr)
    238 {
    239 	struct wsscreen_internal *dconf;
    240 	struct wsscreen *scr;
    241 
    242 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    243 	if (!scr)
    244 		return (NULL);
    245 
    246 	if (console) {
    247 		dconf = &wsdisplay_console_conf;
    248 		/*
    249 		 * If there's an emulation, tell it about the callback argument.
    250 		 * The other stuff is already there.
    251 		 */
    252 		if (dconf->wsemul != NULL)
    253 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    254 	} else { /* not console */
    255 		dconf = malloc(sizeof(struct wsscreen_internal),
    256 			       M_DEVBUF, M_NOWAIT);
    257 		dconf->emulops = type->textops;
    258 		dconf->emulcookie = cookie;
    259 		if (dconf->emulops) {
    260 			dconf->wsemul = wsemul_pick(emul);
    261 			if (dconf->wsemul == NULL) {
    262 				free(dconf, M_DEVBUF);
    263 				free(scr, M_DEVBUF);
    264 				return (NULL);
    265 			}
    266 			dconf->wsemulcookie =
    267 			  (*dconf->wsemul->attach)(0, type, cookie,
    268 						   ccol, crow, scr, defattr);
    269 		} else
    270 			dconf->wsemul = NULL;
    271 		dconf->scrdata = type;
    272 	}
    273 
    274 	scr->scr_dconf = dconf;
    275 
    276 	scr->scr_tty = ttymalloc();
    277 	tty_attach(scr->scr_tty);
    278 	scr->scr_hold_screen = 0;
    279 	if (WSSCREEN_HAS_EMULATOR(scr))
    280 		scr->scr_flags = 0;
    281 	else
    282 		scr->scr_flags = SCR_GRAPHICS;
    283 
    284 	scr->scr_syncops = 0;
    285 	scr->sc = sc;
    286 #ifdef WSDISPLAY_COMPAT_RAWKBD
    287 	scr->scr_rawkbd = 0;
    288 #endif
    289 	return (scr);
    290 }
    291 
    292 void
    293 wsscreen_detach(struct wsscreen *scr)
    294 {
    295 	u_int ccol, crow; /* XXX */
    296 
    297 	if (WSSCREEN_HAS_TTY(scr)) {
    298 		tty_detach(scr->scr_tty);
    299 		ttyfree(scr->scr_tty);
    300 	}
    301 	if (WSSCREEN_HAS_EMULATOR(scr))
    302 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    303 						  &ccol, &crow);
    304 	free(scr->scr_dconf, M_DEVBUF);
    305 	free(scr, M_DEVBUF);
    306 }
    307 
    308 const struct wsscreen_descr *
    309 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
    310 {
    311 	int i;
    312 	const struct wsscreen_descr *scr;
    313 
    314 	KASSERT(scrdata->nscreens > 0);
    315 
    316 	if (name == NULL)
    317 		return (scrdata->screens[0]);
    318 
    319 	for (i = 0; i < scrdata->nscreens; i++) {
    320 		scr = scrdata->screens[i];
    321 		if (!strcmp(name, scr->name))
    322 			return (scr);
    323 	}
    324 
    325 	return (0);
    326 }
    327 
    328 /*
    329  * print info about attached screen
    330  */
    331 static void
    332 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
    333 {
    334 	printf("%s: screen %d", sc->sc_dv.dv_xname, idx);
    335 	if (count > 1)
    336 		printf("-%d", idx + (count-1));
    337 	printf(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
    338 	if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
    339 		printf(", %s emulation",
    340 			sc->sc_scr[idx]->scr_dconf->wsemul->name);
    341 	}
    342 	printf(")\n");
    343 }
    344 
    345 int
    346 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
    347 	const char *screentype, const char *emul)
    348 {
    349 	const struct wsscreen_descr *scrdesc;
    350 	int error;
    351 	void *cookie;
    352 	int ccol, crow;
    353 	long defattr;
    354 	struct wsscreen *scr;
    355 	int s;
    356 
    357 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    358 		return (EINVAL);
    359 	if (sc->sc_scr[idx] != NULL)
    360 		return (EBUSY);
    361 
    362 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    363 	if (!scrdesc)
    364 		return (ENXIO);
    365 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    366 			scrdesc, &cookie, &ccol, &crow, &defattr);
    367 	if (error)
    368 		return (error);
    369 
    370 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    371 			      cookie, ccol, crow, defattr);
    372 	if (scr == NULL) {
    373 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    374 						 cookie);
    375 		return (ENXIO);
    376 	}
    377 
    378 	sc->sc_scr[idx] = scr;
    379 
    380 	/* if no screen has focus yet, activate the first we get */
    381 	s = spltty();
    382 	if (!sc->sc_focus) {
    383 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
    384 						 scr->scr_dconf->emulcookie,
    385 						 0, 0, 0);
    386 		sc->sc_focusidx = idx;
    387 		sc->sc_focus = scr;
    388 	}
    389 	splx(s);
    390 	return (0);
    391 }
    392 
    393 static void
    394 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
    395 {
    396 	int maj, mn, idx;
    397 
    398 	/* hangup */
    399 	if (WSSCREEN_HAS_TTY(scr)) {
    400 		struct tty *tp = scr->scr_tty;
    401 		(*tp->t_linesw->l_modem)(tp, 0);
    402 	}
    403 
    404 	/* locate the major number */
    405 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    406 	/* locate the screen index */
    407 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
    408 		if (scr == sc->sc_scr[idx])
    409 			break;
    410 #ifdef DIAGNOSTIC
    411 	if (idx == WSDISPLAY_MAXSCREEN)
    412 		panic("wsdisplay_forceclose: bad screen");
    413 #endif
    414 
    415 	/* nuke the vnodes */
    416 	mn = WSDISPLAYMINOR(device_unit(&sc->sc_dv), idx);
    417 	vdevgone(maj, mn, mn, VCHR);
    418 }
    419 
    420 #ifdef WSDISPLAY_SCROLLSUPPORT
    421 void
    422 wsdisplay_scroll(void *arg, int op)
    423 {
    424 	struct wsdisplay_softc *sc = arg;
    425 	int lines;
    426 
    427 	if (op == WSDISPLAY_SCROLL_RESET)
    428 		lines = 0;
    429 	else {
    430 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
    431 			sc->sc_scroll_values.slowlines :
    432 			sc->sc_scroll_values.fastlines;
    433 		if (op & WSDISPLAY_SCROLL_BACKWARD)
    434 			lines = -(lines);
    435 	}
    436 
    437 	if (sc->sc_accessops->scroll) {
    438 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
    439 		    sc->sc_focus->scr_dconf->emulcookie, lines);
    440 	}
    441 }
    442 #endif
    443 
    444 int
    445 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
    446 {
    447 	struct wsscreen *scr;
    448 	int s;
    449 	void *cookie;
    450 
    451 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    452 		return (EINVAL);
    453 	if ((scr = sc->sc_scr[idx]) == NULL)
    454 		return (ENXIO);
    455 
    456 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    457 	    scr->scr_syncops ||
    458 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    459 		return(EBUSY);
    460 
    461 	wsdisplay_closescreen(sc, scr);
    462 
    463 	/*
    464 	 * delete pointers, so neither device entries
    465 	 * nor keyboard input can reference it anymore
    466 	 */
    467 	s = spltty();
    468 	if (sc->sc_focus == scr) {
    469 		sc->sc_focus = 0;
    470 #ifdef WSDISPLAY_COMPAT_RAWKBD
    471 		wsdisplay_update_rawkbd(sc, 0);
    472 #endif
    473 	}
    474 	sc->sc_scr[idx] = 0;
    475 	splx(s);
    476 
    477 	/*
    478 	 * Wake up processes waiting for the screen to
    479 	 * be activated. Sleepers must check whether
    480 	 * the screen still exists.
    481 	 */
    482 	if (scr->scr_flags & SCR_WAITACTIVE)
    483 		wakeup(scr);
    484 
    485 	/* save a reference to the graphics screen */
    486 	cookie = scr->scr_dconf->emulcookie;
    487 
    488 	wsscreen_detach(scr);
    489 
    490 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    491 					 cookie);
    492 
    493 	printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
    494 	return (0);
    495 }
    496 
    497 /*
    498  * Autoconfiguration functions.
    499  */
    500 int
    501 wsdisplay_emul_match(struct device *parent, struct cfdata *match, void *aux)
    502 {
    503 	struct wsemuldisplaydev_attach_args *ap = aux;
    504 
    505 	if (match->wsemuldisplaydevcf_console !=
    506 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    507 		/*
    508 		 * If console-ness of device specified, either match
    509 		 * exactly (at high priority), or fail.
    510 		 */
    511 		if (match->wsemuldisplaydevcf_console != 0 &&
    512 		    ap->console != 0)
    513 			return (10);
    514 		else
    515 			return (0);
    516 	}
    517 
    518 	/* If console-ness unspecified, it wins. */
    519 	return (1);
    520 }
    521 
    522 void
    523 wsdisplay_emul_attach(struct device *parent, struct device *self, void *aux)
    524 {
    525 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    526 	struct wsemuldisplaydev_attach_args *ap = aux;
    527 
    528 	/* Don't allow more than one console to attach */
    529 	if (wsdisplay_console_attached && ap->console)
    530 		ap->console = 0;
    531 
    532 	wsdisplay_common_attach(sc, ap->console,
    533 	     device_cfdata(&sc->sc_dv)->wsemuldisplaydevcf_kbdmux, ap->scrdata,
    534 	     ap->accessops, ap->accesscookie);
    535 
    536 	if (ap->console) {
    537 		int maj;
    538 
    539 		/* locate the major number */
    540 		maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    541 
    542 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
    543 					 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 	    device_cfdata(&sc->sc_dv)->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", device_unit(&sc->sc_dv));
    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,
   1018 					   sc->sc_focus->scr_dconf->emulcookie,
   1019 					   cmd, (caddr_t)dp, 0, NULL));
   1020 }
   1021 
   1022 int
   1023 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1024 	u_long cmd, caddr_t data, int flag, struct lwp *l)
   1025 {
   1026 	int error;
   1027 	char namebuf[16];
   1028 	struct wsdisplay_font fd;
   1029 #ifdef WSDISPLAY_SCROLLSUPPORT
   1030 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1031 #endif
   1032 
   1033 #if NWSKBD > 0
   1034 	struct wsevsrc *inp;
   1035 
   1036 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1037 	switch (cmd) {
   1038 	case WSKBDIO_SETMODE:
   1039 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1040 		return (wsdisplay_update_rawkbd(sc, scr));
   1041 	case WSKBDIO_GETMODE:
   1042 		*(int *)data = (scr->scr_rawkbd ?
   1043 				WSKBD_RAW : WSKBD_TRANSLATED);
   1044 		return (0);
   1045 	}
   1046 #endif
   1047 	inp = sc->sc_input;
   1048 	if (inp == NULL)
   1049 		return (ENXIO);
   1050 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1051 	if (error != EPASSTHROUGH)
   1052 		return (error);
   1053 #endif /* NWSKBD > 0 */
   1054 
   1055 	switch (cmd) {
   1056 	case WSDISPLAYIO_GMODE:
   1057 		if (scr->scr_flags & SCR_GRAPHICS) {
   1058 			if (scr->scr_flags & SCR_DUMBFB)
   1059 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1060 			else
   1061 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1062 		} else
   1063 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1064 		return (0);
   1065 
   1066 	case WSDISPLAYIO_SMODE:
   1067 #define d (*(int *)data)
   1068 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1069 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1070 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1071 			return (EINVAL);
   1072 
   1073 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1074 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1075 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1076 			d == WSDISPLAYIO_MODE_DUMBFB)
   1077 			    scr->scr_flags |= SCR_GRAPHICS |
   1078 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1079 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1080 		    return (EINVAL);
   1081 
   1082 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1083 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
   1084 
   1085 	    return (0);
   1086 #undef d
   1087 
   1088 #ifdef WSDISPLAY_SCROLLSUPPORT
   1089 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1090     do {								\
   1091 	(dstp)->fastlines = ((srcp)->which &				\
   1092 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1093 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1094 	(dstp)->slowlines = ((srcp)->which &				\
   1095 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1096 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1097 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1098     } while (0)
   1099 
   1100 
   1101 	case WSDISPLAYIO_DSSCROLL:
   1102 		usdp = (struct wsdisplay_scroll_data *)data;
   1103 		ksdp = &sc->sc_scroll_values;
   1104 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1105 		return (0);
   1106 
   1107 	case WSDISPLAYIO_DGSCROLL:
   1108 		usdp = (struct wsdisplay_scroll_data *)data;
   1109 		ksdp = &sc->sc_scroll_values;
   1110 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1111 		return (0);
   1112 #else
   1113 	case WSDISPLAYIO_DSSCROLL:
   1114 	case WSDISPLAYIO_DGSCROLL:
   1115 		return ENODEV;
   1116 #endif
   1117 
   1118 	case WSDISPLAYIO_SFONT:
   1119 #define d ((struct wsdisplay_usefontdata *)data)
   1120 		if (!sc->sc_accessops->load_font)
   1121 			return (EINVAL);
   1122 		if (d->name) {
   1123 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1124 			if (error)
   1125 				return (error);
   1126 			fd.name = namebuf;
   1127 		} else
   1128 			fd.name = 0;
   1129 		fd.data = 0;
   1130 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1131 					scr->scr_dconf->emulcookie, &fd);
   1132 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1133 			(*scr->scr_dconf->wsemul->reset)
   1134 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1135 		return (error);
   1136 #undef d
   1137 
   1138 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1139 	case WSDISPLAYIO_GMSGATTRS:
   1140 #define d ((struct wsdisplay_msgattrs *)data)
   1141 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1142 		    (scr->scr_dconf->wsemulcookie, d);
   1143 		return (0);
   1144 #undef d
   1145 
   1146 	case WSDISPLAYIO_SMSGATTRS: {
   1147 #define d ((struct wsdisplay_msgattrs *)data)
   1148 		int i;
   1149 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1150 			if (sc->sc_scr[i] != NULL)
   1151 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1152 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1153 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1154 				     d);
   1155 		}
   1156 		return (0);
   1157 #undef d
   1158 #else
   1159 	case WSDISPLAYIO_GMSGATTRS:
   1160 	case WSDISPLAYIO_SMSGATTRS:
   1161 		return (ENODEV);
   1162 #endif
   1163 	}
   1164 
   1165 	/* check ioctls for display */
   1166 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1167 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
   1168 }
   1169 
   1170 int
   1171 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1172 	int flag, struct lwp *l)
   1173 {
   1174 	switch (cmd) {
   1175 	case WSDISPLAYIO_GETACTIVESCREEN:
   1176 		*(int*)data = wsdisplay_getactivescreen(sc);
   1177 		return (0);
   1178 	}
   1179 
   1180 	return (EPASSTHROUGH);
   1181 }
   1182 
   1183 int
   1184 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
   1185 	int flag, struct lwp *l)
   1186 {
   1187 	int error;
   1188 	char *type, typebuf[16], *emul, emulbuf[16];
   1189 	void *tbuf;
   1190 	u_int fontsz;
   1191 #if defined(COMPAT_14) && NWSKBD > 0
   1192 	struct wsmux_device wsmuxdata;
   1193 #endif
   1194 #if NWSKBD > 0
   1195 	struct wsevsrc *inp;
   1196 #endif
   1197 
   1198 	switch (cmd) {
   1199 	case WSDISPLAYIO_ADDSCREEN:
   1200 #define d ((struct wsdisplay_addscreendata *)data)
   1201 		if (d->screentype) {
   1202 			error = copyinstr(d->screentype, typebuf,
   1203 					  sizeof(typebuf), 0);
   1204 			if (error)
   1205 				return (error);
   1206 			type = typebuf;
   1207 		} else
   1208 			type = 0;
   1209 		if (d->emul) {
   1210 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1211 			if (error)
   1212 				return (error);
   1213 			emul = emulbuf;
   1214 		} else
   1215 			emul = 0;
   1216 
   1217 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1218 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1219 		return (error);
   1220 #undef d
   1221 	case WSDISPLAYIO_DELSCREEN:
   1222 #define d ((struct wsdisplay_delscreendata *)data)
   1223 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1224 #undef d
   1225 	case WSDISPLAYIO_LDFONT:
   1226 #define d ((struct wsdisplay_font *)data)
   1227 		if (!sc->sc_accessops->load_font)
   1228 			return (EINVAL);
   1229 		if (d->name) {
   1230 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1231 			if (error)
   1232 				return (error);
   1233 			d->name = typebuf;
   1234 		} else
   1235 			d->name = "loaded"; /* ??? */
   1236 		fontsz = d->fontheight * d->stride * d->numchars;
   1237 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1238 			return (EINVAL);
   1239 
   1240 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1241 		error = copyin(d->data, tbuf, fontsz);
   1242 		if (error) {
   1243 			free(tbuf, M_DEVBUF);
   1244 			return (error);
   1245 		}
   1246 		d->data = tbuf;
   1247 		error =
   1248 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1249 		free(tbuf, M_DEVBUF);
   1250 #undef d
   1251 		return (error);
   1252 
   1253 #if NWSKBD > 0
   1254 #ifdef COMPAT_14
   1255 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1256 #define d ((struct wsdisplay_kbddata *)data)
   1257 		inp = sc->sc_input;
   1258 		if (inp == NULL)
   1259 			return (ENXIO);
   1260 		switch (d->op) {
   1261 		case _O_WSDISPLAY_KBD_ADD:
   1262 			if (d->idx == -1) {
   1263 				d->idx = wskbd_pickfree();
   1264 				if (d->idx == -1)
   1265 					return (ENXIO);
   1266 			}
   1267 			wsmuxdata.type = WSMUX_KBD;
   1268 			wsmuxdata.idx = d->idx;
   1269 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1270 					      &wsmuxdata, flag, l));
   1271 		case _O_WSDISPLAY_KBD_DEL:
   1272 			wsmuxdata.type = WSMUX_KBD;
   1273 			wsmuxdata.idx = d->idx;
   1274 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1275 					      &wsmuxdata, flag, l));
   1276 		default:
   1277 			return (EINVAL);
   1278 		}
   1279 #undef d
   1280 #endif
   1281 
   1282 	case WSMUXIO_ADD_DEVICE:
   1283 #define d ((struct wsmux_device *)data)
   1284 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1285 			d->idx = wskbd_pickfree();
   1286 #undef d
   1287 		/* fall into */
   1288 	case WSMUXIO_INJECTEVENT:
   1289 	case WSMUXIO_REMOVE_DEVICE:
   1290 	case WSMUXIO_LIST_DEVICES:
   1291 		inp = sc->sc_input;
   1292 		if (inp == NULL)
   1293 			return (ENXIO);
   1294 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1295 #endif /* NWSKBD > 0 */
   1296 
   1297 	}
   1298 	return (EPASSTHROUGH);
   1299 }
   1300 
   1301 int
   1302 wsdisplay_stat_inject(struct device *dev, u_int type, int value)
   1303 {
   1304 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *) dev;
   1305 	struct wseventvar *evar;
   1306 	struct wscons_event event;
   1307 
   1308 	evar = &sc->evar;
   1309 
   1310 	if (evar == NULL)
   1311 		return (0);
   1312 
   1313 	if (evar->q == NULL)
   1314 		return (1);
   1315 
   1316 	event.type = type;
   1317 	event.value = value;
   1318 	if (wsevent_inject(evar, &event, 1) != 0) {
   1319 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1320 		return (1);
   1321 	}
   1322 
   1323 	return (0);
   1324 }
   1325 
   1326 paddr_t
   1327 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1328 {
   1329 	struct wsdisplay_softc *sc =
   1330 	    device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1331 	struct wsscreen *scr;
   1332 
   1333 	if (ISWSDISPLAYSTAT(dev))
   1334 		return (-1);
   1335 
   1336 	if (ISWSDISPLAYCTL(dev))
   1337 		return (-1);
   1338 
   1339 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1340 		return (-1);
   1341 
   1342 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1343 		return (-1);
   1344 
   1345 	/* pass mmap to display */
   1346 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
   1347 	    scr->scr_dconf->emulcookie, offset, prot));
   1348 }
   1349 
   1350 void
   1351 wsdisplaystart(struct tty *tp)
   1352 {
   1353 	struct wsdisplay_softc *sc;
   1354 	struct wsscreen *scr;
   1355 	int s, n;
   1356 	u_char *tbuf;
   1357 
   1358 	s = spltty();
   1359 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1360 		splx(s);
   1361 		return;
   1362 	}
   1363 	sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1364 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
   1365 		splx(s);
   1366 		return;
   1367 	}
   1368 
   1369 	if (scr->scr_hold_screen) {
   1370 		tp->t_state |= TS_TIMEOUT;
   1371 		splx(s);
   1372 		return;
   1373 	}
   1374 	tp->t_state |= TS_BUSY;
   1375 	splx(s);
   1376 
   1377 	/*
   1378 	 * Drain output from ring buffer.
   1379 	 * The output will normally be in one contiguous chunk, but when the
   1380 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1381 	 * the other at the start.  For performance, rather than loop here,
   1382 	 * we output one chunk, see if there's another one, and if so, output
   1383 	 * it too.
   1384 	 */
   1385 
   1386 	n = ndqb(&tp->t_outq, 0);
   1387 	tbuf = tp->t_outq.c_cf;
   1388 
   1389 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1390 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1391 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1392 						  tbuf, n, 0);
   1393 	}
   1394 	ndflush(&tp->t_outq, n);
   1395 
   1396 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1397 		tbuf = tp->t_outq.c_cf;
   1398 
   1399 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1400 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1401 			(*scr->scr_dconf->wsemul->output)
   1402 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
   1403 		}
   1404 		ndflush(&tp->t_outq, n);
   1405 	}
   1406 
   1407 	s = spltty();
   1408 	tp->t_state &= ~TS_BUSY;
   1409 	/* Come back if there's more to do */
   1410 	if (tp->t_outq.c_cc) {
   1411 		tp->t_state |= TS_TIMEOUT;
   1412 		callout_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
   1413 		    ttrstrt, tp);
   1414 	}
   1415 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1416 		if (tp->t_state&TS_ASLEEP) {
   1417 			tp->t_state &= ~TS_ASLEEP;
   1418 			wakeup(&tp->t_outq);
   1419 		}
   1420 		selwakeup(&tp->t_wsel);
   1421 	}
   1422 	splx(s);
   1423 }
   1424 
   1425 void
   1426 wsdisplaystop(struct tty *tp, int flag)
   1427 {
   1428 	int s;
   1429 
   1430 	s = spltty();
   1431 	if (ISSET(tp->t_state, TS_BUSY))
   1432 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1433 			SET(tp->t_state, TS_FLUSH);
   1434 	splx(s);
   1435 }
   1436 
   1437 /* Set line parameters. */
   1438 int
   1439 wsdisplayparam(struct tty *tp, struct termios *t)
   1440 {
   1441 
   1442 	tp->t_ispeed = t->c_ispeed;
   1443 	tp->t_ospeed = t->c_ospeed;
   1444 	tp->t_cflag = t->c_cflag;
   1445 	return 0;
   1446 }
   1447 
   1448 /*
   1449  * Callbacks for the emulation code.
   1450  */
   1451 void
   1452 wsdisplay_emulbell(void *v)
   1453 {
   1454 	struct wsscreen *scr = v;
   1455 
   1456 	if (scr == NULL)		/* console, before real attach */
   1457 		return;
   1458 
   1459 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1460 		return;
   1461 
   1462 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1463 					FWRITE, NULL);
   1464 }
   1465 
   1466 void
   1467 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1468 {
   1469 	struct wsscreen *scr = v;
   1470 	struct tty *tp;
   1471 
   1472 	if (v == NULL)			/* console, before real attach */
   1473 		return;
   1474 
   1475 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1476 		return;
   1477 	if (!WSSCREEN_HAS_TTY(scr))
   1478 		return;
   1479 
   1480 	tp = scr->scr_tty;
   1481 	while (count-- > 0)
   1482 		(*tp->t_linesw->l_rint)(*data++, tp);
   1483 }
   1484 
   1485 /*
   1486  * Calls from the keyboard interface.
   1487  */
   1488 void
   1489 wsdisplay_kbdinput(struct device *dev, keysym_t ks)
   1490 {
   1491 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1492 	struct wsscreen *scr;
   1493 	const char *dp;
   1494 	int count;
   1495 	struct tty *tp;
   1496 
   1497 	KASSERT(sc != NULL);
   1498 
   1499 	scr = sc->sc_focus;
   1500 
   1501 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1502 		return;
   1503 
   1504 	tp = scr->scr_tty;
   1505 
   1506 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1507 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
   1508 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1509 		count = (*scr->scr_dconf->wsemul->translate)
   1510 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1511 		while (count-- > 0)
   1512 			(*tp->t_linesw->l_rint)(*dp++, tp);
   1513 	}
   1514 }
   1515 
   1516 #if defined(WSDISPLAY_COMPAT_RAWKBD)
   1517 int
   1518 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
   1519 {
   1520 #if NWSKBD > 0
   1521 	int s, raw, data, error;
   1522 	struct wsevsrc *inp;
   1523 
   1524 	s = spltty();
   1525 
   1526 	raw = (scr ? scr->scr_rawkbd : 0);
   1527 
   1528 	if (scr != sc->sc_focus ||
   1529 	    sc->sc_rawkbd == raw) {
   1530 		splx(s);
   1531 		return (0);
   1532 	}
   1533 
   1534 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1535 	inp = sc->sc_input;
   1536 	if (inp == NULL)
   1537 		return (ENXIO);
   1538 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1539 	if (!error)
   1540 		sc->sc_rawkbd = raw;
   1541 	splx(s);
   1542 	return (error);
   1543 #else
   1544 	return (0);
   1545 #endif
   1546 }
   1547 #endif
   1548 
   1549 int
   1550 wsdisplay_switch3(void *arg, int error, int waitok)
   1551 {
   1552 	struct wsdisplay_softc *sc = arg;
   1553 	int no;
   1554 	struct wsscreen *scr;
   1555 
   1556 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1557 		printf("wsdisplay_switch3: not switching\n");
   1558 		return (EINVAL);
   1559 	}
   1560 
   1561 	no = sc->sc_screenwanted;
   1562 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1563 		panic("wsdisplay_switch3: invalid screen %d", no);
   1564 	scr = sc->sc_scr[no];
   1565 	if (!scr) {
   1566 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1567 		error = ENXIO;
   1568 	}
   1569 
   1570 	if (error) {
   1571 		/* try to recover, avoid recursion */
   1572 
   1573 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1574 			printf("wsdisplay_switch3: giving up\n");
   1575 			sc->sc_focus = 0;
   1576 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1577 			wsdisplay_update_rawkbd(sc, 0);
   1578 #endif
   1579 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1580 			return (error);
   1581 		}
   1582 
   1583 		sc->sc_screenwanted = sc->sc_oldscreen;
   1584 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1585 		return (wsdisplay_switch1(arg, 0, waitok));
   1586 	}
   1587 
   1588 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1589 
   1590 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1591 		wakeup(scr);
   1592 	return (error);
   1593 }
   1594 
   1595 int
   1596 wsdisplay_switch2(void *arg, int error, int waitok)
   1597 {
   1598 	struct wsdisplay_softc *sc = arg;
   1599 	int no;
   1600 	struct wsscreen *scr;
   1601 
   1602 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1603 		printf("wsdisplay_switch2: not switching\n");
   1604 		return (EINVAL);
   1605 	}
   1606 
   1607 	no = sc->sc_screenwanted;
   1608 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1609 		panic("wsdisplay_switch2: invalid screen %d", no);
   1610 	scr = sc->sc_scr[no];
   1611 	if (!scr) {
   1612 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
   1613 		error = ENXIO;
   1614 	}
   1615 
   1616 	if (error) {
   1617 		/* try to recover, avoid recursion */
   1618 
   1619 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1620 			printf("wsdisplay_switch2: giving up\n");
   1621 			sc->sc_focus = 0;
   1622 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1623 			return (error);
   1624 		}
   1625 
   1626 		sc->sc_screenwanted = sc->sc_oldscreen;
   1627 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1628 		return (wsdisplay_switch1(arg, 0, waitok));
   1629 	}
   1630 
   1631 	sc->sc_focusidx = no;
   1632 	sc->sc_focus = scr;
   1633 
   1634 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1635 	(void) wsdisplay_update_rawkbd(sc, scr);
   1636 #endif
   1637 	/* keyboard map??? */
   1638 
   1639 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
   1640 	if (scr->scr_syncops) {
   1641 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1642 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
   1643 		if (error == EAGAIN) {
   1644 			/* switch will be done asynchronously */
   1645 			return (0);
   1646 		}
   1647 	}
   1648 
   1649 	return (wsdisplay_switch3(sc, error, waitok));
   1650 }
   1651 
   1652 int
   1653 wsdisplay_switch1(void *arg, int error, int waitok)
   1654 {
   1655 	struct wsdisplay_softc *sc = arg;
   1656 	int no;
   1657 	struct wsscreen *scr;
   1658 
   1659 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1660 		printf("wsdisplay_switch1: not switching\n");
   1661 		return (EINVAL);
   1662 	}
   1663 
   1664 	no = sc->sc_screenwanted;
   1665 	if (no == WSDISPLAY_NULLSCREEN) {
   1666 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1667 		if (!error) {
   1668 			sc->sc_focus = 0;
   1669 		}
   1670 		wakeup(sc);
   1671 		return (error);
   1672 	}
   1673 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1674 		panic("wsdisplay_switch1: invalid screen %d", no);
   1675 	scr = sc->sc_scr[no];
   1676 	if (!scr) {
   1677 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1678 		error = ENXIO;
   1679 	}
   1680 
   1681 	if (error) {
   1682 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1683 		return (error);
   1684 	}
   1685 
   1686 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
   1687 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1688 						 scr->scr_dconf->emulcookie,
   1689 						 waitok,
   1690 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
   1691 	if (error == EAGAIN) {
   1692 		/* switch will be done asynchronously */
   1693 		return (0);
   1694 	}
   1695 
   1696 	return (wsdisplay_switch2(sc, error, waitok));
   1697 }
   1698 
   1699 int
   1700 wsdisplay_switch(struct device *dev, int no, int waitok)
   1701 {
   1702 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1703 	int s, res = 0;
   1704 	struct wsscreen *scr;
   1705 
   1706 	if (no != WSDISPLAY_NULLSCREEN) {
   1707 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
   1708 			return (EINVAL);
   1709 		if (sc->sc_scr[no] == NULL)
   1710 			return (ENXIO);
   1711 	}
   1712 
   1713 	wsdisplay_stat_inject(dev, WSCONS_EVENT_SCREEN_SWITCH, no);
   1714 
   1715 	s = spltty();
   1716 
   1717 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   1718 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   1719 		splx(s);
   1720 		return (0);
   1721 	}
   1722 
   1723 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1724 		splx(s);
   1725 		return (EBUSY);
   1726 	}
   1727 
   1728 	sc->sc_flags |= SC_SWITCHPENDING;
   1729 	sc->sc_screenwanted = no;
   1730 
   1731 	splx(s);
   1732 
   1733 	scr = sc->sc_focus;
   1734 	if (!scr) {
   1735 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1736 		return (wsdisplay_switch1(sc, 0, waitok));
   1737 	} else
   1738 		sc->sc_oldscreen = sc->sc_focusidx;
   1739 
   1740 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
   1741 	if (scr->scr_syncops) {
   1742 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1743 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
   1744 		if (res == EAGAIN) {
   1745 			/* switch will be done asynchronously */
   1746 			return (0);
   1747 		}
   1748 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1749 		/* no way to save state */
   1750 		res = EBUSY;
   1751 	}
   1752 
   1753 	return (wsdisplay_switch1(sc, res, waitok));
   1754 }
   1755 
   1756 void
   1757 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
   1758 {
   1759 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1760 	struct wsscreen *scr;
   1761 
   1762 	KASSERT(sc != NULL);
   1763 	scr = sc->sc_focus;
   1764 
   1765 	if (!scr)
   1766 		return;
   1767 
   1768 	switch (op) {
   1769 	case WSDISPLAY_RESETEMUL:
   1770 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1771 			break;
   1772 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1773 						 WSEMUL_RESET);
   1774 		break;
   1775 	case WSDISPLAY_RESETCLOSE:
   1776 		wsdisplay_closescreen(sc, scr);
   1777 		break;
   1778 	}
   1779 }
   1780 
   1781 /*
   1782  * Interface for (external) VT switch / process synchronization code
   1783  */
   1784 int
   1785 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   1786 	void *cookie)
   1787 {
   1788 	if (scr->scr_syncops) {
   1789 		/*
   1790 		 * The screen is already claimed.
   1791 		 * Check if the owner is still alive.
   1792 		 */
   1793 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1794 			return (EBUSY);
   1795 	}
   1796 	scr->scr_syncops = ops;
   1797 	scr->scr_synccookie = cookie;
   1798 	return (0);
   1799 }
   1800 
   1801 int
   1802 wsscreen_detach_sync(struct wsscreen *scr)
   1803 {
   1804 	if (!scr->scr_syncops)
   1805 		return (EINVAL);
   1806 	scr->scr_syncops = 0;
   1807 	return (0);
   1808 }
   1809 
   1810 int
   1811 wsscreen_lookup_sync(struct wsscreen *scr,
   1812 	const struct wscons_syncops *ops, /* used as ID */
   1813 	void **cookiep)
   1814 {
   1815 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1816 		return (EINVAL);
   1817 	*cookiep = scr->scr_synccookie;
   1818 	return (0);
   1819 }
   1820 
   1821 /*
   1822  * Interface to virtual screen stuff
   1823  */
   1824 int
   1825 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   1826 {
   1827 	return (WSDISPLAY_MAXSCREEN - 1);
   1828 }
   1829 
   1830 int
   1831 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   1832 {
   1833 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   1834 		return (EINVAL);
   1835 	if (!sc->sc_scr[idx])
   1836 		return (ENXIO);
   1837 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1838 }
   1839 
   1840 int
   1841 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   1842 {
   1843 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   1844 }
   1845 
   1846 int
   1847 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   1848 {
   1849 	struct wsscreen *scr;
   1850 	int s, res = 0;
   1851 
   1852 	if (no == WSDISPLAY_NULLSCREEN) {
   1853 		s = spltty();
   1854 		while (sc->sc_focus && res == 0) {
   1855 			res = tsleep(sc, PCATCH, "wswait", 0);
   1856 		}
   1857 		splx(s);
   1858 		return (res);
   1859 	}
   1860 
   1861 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1862 		return (ENXIO);
   1863 	scr = sc->sc_scr[no];
   1864 	if (!scr)
   1865 		return (ENXIO);
   1866 
   1867 	s = spltty();
   1868 	if (scr != sc->sc_focus) {
   1869 		scr->scr_flags |= SCR_WAITACTIVE;
   1870 		res = tsleep(scr, PCATCH, "wswait", 0);
   1871 		if (scr != sc->sc_scr[no])
   1872 			res = ENXIO; /* disappeared in the meantime */
   1873 		else
   1874 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1875 	}
   1876 	splx(s);
   1877 	return (res);
   1878 }
   1879 
   1880 void
   1881 wsdisplay_kbdholdscreen(struct device *dev, int hold)
   1882 {
   1883 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1884 	struct wsscreen *scr;
   1885 
   1886 	scr = sc->sc_focus;
   1887 
   1888 	if (hold)
   1889 		scr->scr_hold_screen = 1;
   1890 	else {
   1891 		scr->scr_hold_screen = 0;
   1892 		callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
   1893 		    ttrstrt, scr->scr_tty);		/* "immediate" */
   1894 	}
   1895 }
   1896 
   1897 #if NWSKBD > 0
   1898 void
   1899 wsdisplay_set_console_kbd(struct wsevsrc *src)
   1900 {
   1901 	if (wsdisplay_console_device == NULL) {
   1902 		src->me_dispdv = NULL;
   1903 		return;
   1904 	}
   1905 #if NWSMUX > 0
   1906 	if (wsmux_attach_sc((struct wsmux_softc *)
   1907 			    wsdisplay_console_device->sc_input, src)) {
   1908 		src->me_dispdv = NULL;
   1909 		return;
   1910 	}
   1911 #else
   1912 	wsdisplay_console_device->sc_input = src;
   1913 #endif
   1914 	src->me_dispdv = &wsdisplay_console_device->sc_dv;
   1915 }
   1916 #endif /* NWSKBD > 0 */
   1917 
   1918 /*
   1919  * Console interface.
   1920  */
   1921 void
   1922 wsdisplay_cnputc(dev_t dev, int i)
   1923 {
   1924 	struct wsscreen_internal *dc;
   1925 	u_char c = i;
   1926 
   1927 	if (!wsdisplay_console_initted)
   1928 		return;
   1929 
   1930 	if ((wsdisplay_console_device != NULL) &&
   1931 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   1932 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1933 		return;
   1934 
   1935 	dc = &wsdisplay_console_conf;
   1936 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1937 }
   1938 
   1939 static int
   1940 wsdisplay_getc_dummy(dev_t dev)
   1941 {
   1942 	/* panic? */
   1943 	return (0);
   1944 }
   1945 
   1946 static void
   1947 wsdisplay_pollc(dev_t dev, int on)
   1948 {
   1949 
   1950 	wsdisplay_cons_pollmode = on;
   1951 
   1952 	/* notify to fb drivers */
   1953 	if (wsdisplay_console_device != NULL &&
   1954 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   1955 		(*wsdisplay_console_device->sc_accessops->pollc)
   1956 			(wsdisplay_console_device->sc_accesscookie, on);
   1957 
   1958 	/* notify to kbd drivers */
   1959 	if (wsdisplay_cons_kbd_pollc)
   1960 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   1961 }
   1962 
   1963 void
   1964 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   1965 	void (*bell)(dev_t, u_int, u_int, u_int))
   1966 {
   1967 	wsdisplay_cons.cn_getc = get;
   1968 	wsdisplay_cons.cn_bell = bell;
   1969 	wsdisplay_cons_kbd_pollc = poll;
   1970 }
   1971 
   1972 void
   1973 wsdisplay_unset_cons_kbd(void)
   1974 {
   1975 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   1976 	wsdisplay_cons.cn_bell = NULL;
   1977 	wsdisplay_cons_kbd_pollc = 0;
   1978 }
   1979 
   1980 /*
   1981  * Switch the console display to it's first screen.
   1982  */
   1983 void
   1984 wsdisplay_switchtoconsole(void)
   1985 {
   1986 	struct wsdisplay_softc *sc;
   1987 	struct wsscreen *scr;
   1988 
   1989 	if (wsdisplay_console_device != NULL) {
   1990 		sc = wsdisplay_console_device;
   1991 		if ((scr = sc->sc_scr[0]) == NULL)
   1992 			return;
   1993 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1994 						 scr->scr_dconf->emulcookie,
   1995 						 0, NULL, NULL);
   1996 	}
   1997 }
   1998 
   1999 /*
   2000  * Switch the console at shutdown.
   2001  */
   2002 static void
   2003 wsdisplay_shutdownhook(void *arg)
   2004 {
   2005 
   2006 	wsdisplay_switchtoconsole();
   2007 }
   2008