Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.150
      1 /* $NetBSD: wsdisplay.c,v 1.150 2019/01/30 02:43:47 jmcneill 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.150 2019/01/30 02:43:47 jmcneill Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_wsdisplay_compat.h"
     38 #include "opt_wsmsgattrs.h"
     39 #endif
     40 
     41 #include "wskbd.h"
     42 #include "wsmux.h"
     43 #include "wsdisplay.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/conf.h>
     47 #include <sys/device.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/poll.h>
     50 #include <sys/kernel.h>
     51 #include <sys/proc.h>
     52 #include <sys/malloc.h>
     53 #include <sys/syslog.h>
     54 #include <sys/systm.h>
     55 #include <sys/tty.h>
     56 #include <sys/signalvar.h>
     57 #include <sys/errno.h>
     58 #include <sys/fcntl.h>
     59 #include <sys/vnode.h>
     60 #include <sys/kauth.h>
     61 #include <sys/sysctl.h>
     62 
     63 #include <dev/wscons/wsconsio.h>
     64 #include <dev/wscons/wseventvar.h>
     65 #include <dev/wscons/wsmuxvar.h>
     66 #include <dev/wscons/wsdisplayvar.h>
     67 #include <dev/wscons/wsksymvar.h>
     68 #include <dev/wscons/wsksymdef.h>
     69 #include <dev/wscons/wsemulvar.h>
     70 #include <dev/wscons/wscons_callbacks.h>
     71 #include <dev/cons.h>
     72 
     73 #include "locators.h"
     74 
     75 #ifdef WSDISPLAY_MULTICONS
     76 static bool wsdisplay_multicons_enable = true;
     77 #endif
     78 
     79 /* Console device before replaced by wsdisplay */
     80 static struct consdev *wsdisplay_ocn;
     81 
     82 struct wsscreen_internal {
     83 	const struct wsdisplay_emulops *emulops;
     84 	void	*emulcookie;
     85 
     86 	const struct wsscreen_descr *scrdata;
     87 
     88 	const struct wsemul_ops *wsemul;
     89 	void	*wsemulcookie;
     90 };
     91 
     92 struct wsscreen {
     93 	struct wsscreen_internal *scr_dconf;
     94 
     95 	struct tty *scr_tty;
     96 	int	scr_hold_screen;		/* hold tty output */
     97 
     98 	int scr_flags;
     99 #define SCR_OPEN 1		/* is it open? */
    100 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
    101 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
    102 #define	SCR_DUMBFB 8		/* in use as a dumb fb (iff SCR_GRAPHICS) */
    103 	const struct wscons_syncops *scr_syncops;
    104 	void *scr_synccookie;
    105 
    106 #ifdef WSDISPLAY_COMPAT_RAWKBD
    107 	int scr_rawkbd;
    108 #endif
    109 
    110 #ifdef WSDISPLAY_MULTICONS
    111 	callout_t scr_getc_ch;
    112 #endif
    113 
    114 	struct wsdisplay_softc *sc;
    115 
    116 #ifdef DIAGNOSTIC
    117 	/* XXX this is to support a hack in emulinput, see comment below */
    118 	int scr_in_ttyoutput;
    119 #endif
    120 };
    121 
    122 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int,
    123 				 const char *,
    124 				 const struct wsscreen_descr *, void *,
    125 				 int, int, long);
    126 void wsscreen_detach(struct wsscreen *);
    127 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
    128 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
    129 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
    130 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
    131 
    132 #define WSDISPLAY_MAXSCREEN 8
    133 
    134 struct wsdisplay_softc {
    135 	device_t sc_dev;
    136 
    137 	const struct wsdisplay_accessops *sc_accessops;
    138 	void	*sc_accesscookie;
    139 
    140 	const struct wsscreen_list *sc_scrdata;
    141 #ifdef WSDISPLAY_SCROLLSUPPORT
    142 	struct wsdisplay_scroll_data sc_scroll_values;
    143 #endif
    144 
    145 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    146 	int sc_focusidx;	/* available only if sc_focus isn't null */
    147 	struct wsscreen *sc_focus;
    148 
    149 	struct wseventvar evar;
    150 
    151 	int	sc_isconsole;
    152 
    153 	int sc_flags;
    154 #define SC_SWITCHPENDING 1
    155 #define SC_SWITCHERROR 2
    156 #define SC_XATTACHED 4 /* X server active */
    157 	kmutex_t sc_flagsmtx; /* for flags, might also be used for focus */
    158 	kcondvar_t sc_flagscv;
    159 
    160 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
    161 
    162 #if NWSKBD > 0
    163 	struct wsevsrc *sc_input;
    164 #ifdef WSDISPLAY_COMPAT_RAWKBD
    165 	int sc_rawkbd;
    166 #endif
    167 #endif /* NWSKBD > 0 */
    168 };
    169 
    170 #ifdef WSDISPLAY_SCROLLSUPPORT
    171 
    172 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
    173 	WSDISPLAY_SCROLL_DOALL,
    174 	25,
    175 	2,
    176 };
    177 #endif
    178 
    179 extern struct cfdriver wsdisplay_cd;
    180 
    181 /* Autoconfiguration definitions. */
    182 static int wsdisplay_emul_match(device_t , cfdata_t, void *);
    183 static void wsdisplay_emul_attach(device_t, device_t, void *);
    184 static int wsdisplay_emul_detach(device_t, int);
    185 static int wsdisplay_noemul_match(device_t, cfdata_t, void *);
    186 static void wsdisplay_noemul_attach(device_t, device_t, void *);
    187 static bool wsdisplay_suspend(device_t, const pmf_qual_t *);
    188 
    189 CFATTACH_DECL_NEW(wsdisplay_emul, sizeof (struct wsdisplay_softc),
    190     wsdisplay_emul_match, wsdisplay_emul_attach, wsdisplay_emul_detach, NULL);
    191 
    192 CFATTACH_DECL_NEW(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
    193     wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
    194 
    195 dev_type_open(wsdisplayopen);
    196 dev_type_close(wsdisplayclose);
    197 dev_type_read(wsdisplayread);
    198 dev_type_write(wsdisplaywrite);
    199 dev_type_ioctl(wsdisplayioctl);
    200 dev_type_stop(wsdisplaystop);
    201 dev_type_tty(wsdisplaytty);
    202 dev_type_poll(wsdisplaypoll);
    203 dev_type_mmap(wsdisplaymmap);
    204 dev_type_kqfilter(wsdisplaykqfilter);
    205 
    206 const struct cdevsw wsdisplay_cdevsw = {
    207 	.d_open = wsdisplayopen,
    208 	.d_close = wsdisplayclose,
    209 	.d_read = wsdisplayread,
    210 	.d_write = wsdisplaywrite,
    211 	.d_ioctl = wsdisplayioctl,
    212 	.d_stop = wsdisplaystop,
    213 	.d_tty = wsdisplaytty,
    214 	.d_poll = wsdisplaypoll,
    215 	.d_mmap = wsdisplaymmap,
    216 	.d_kqfilter = wsdisplaykqfilter,
    217 	.d_discard = nodiscard,
    218 	.d_flag = D_TTY
    219 };
    220 
    221 static void wsdisplaystart(struct tty *);
    222 static int wsdisplayparam(struct tty *, struct termios *);
    223 
    224 
    225 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    226 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    227 #define ISWSDISPLAYSTAT(dev)	(WSDISPLAYSCREEN(dev) == 254)
    228 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    229 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    230 
    231 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    232 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    233 
    234 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
    235 	    int console, int kbdmux, const struct wsscreen_list *,
    236 	    const struct wsdisplay_accessops *accessops,
    237 	    void *accesscookie);
    238 
    239 #ifdef WSDISPLAY_COMPAT_RAWKBD
    240 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
    241 				 struct wsscreen *);
    242 #endif
    243 
    244 static int wsdisplay_console_initted;
    245 static int wsdisplay_console_attached;
    246 static struct wsdisplay_softc *wsdisplay_console_device;
    247 static struct wsscreen_internal wsdisplay_console_conf;
    248 
    249 static int wsdisplay_getc(dev_t);
    250 static void wsdisplay_pollc(dev_t, int);
    251 
    252 static int wsdisplay_cons_pollmode;
    253 static int (*wsdisplay_cons_kbd_getc)(dev_t);
    254 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
    255 
    256 static struct consdev wsdisplay_cons = {
    257 	NULL, NULL, wsdisplay_getc, wsdisplay_cnputc,
    258 	wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
    259 };
    260 
    261 #ifndef WSDISPLAY_DEFAULTSCREENS
    262 # define WSDISPLAY_DEFAULTSCREENS	0
    263 #endif
    264 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
    265 
    266 static int wsdisplay_switch1(device_t, int, int);
    267 static void wsdisplay_switch1_cb(void *, int, int);
    268 static int wsdisplay_switch2(device_t, int, int);
    269 static void wsdisplay_switch2_cb(void *, int, int);
    270 static int wsdisplay_switch3(device_t, int, int);
    271 static void wsdisplay_switch3_cb(void *, int, int);
    272 
    273 static void wsdisplay_swdone_cb(void *, int, int);
    274 static int wsdisplay_dosync(struct wsdisplay_softc *, int);
    275 
    276 int wsdisplay_clearonclose;
    277 
    278 #ifdef WSDISPLAY_MULTICONS
    279 static void
    280 wsscreen_getc_poll(void *priv)
    281 {
    282 	struct wsscreen *scr = priv;
    283 	int c;
    284 
    285 	if (wsdisplay_multicons_enable &&
    286 	    wsdisplay_ocn && wsdisplay_ocn->cn_getc &&
    287 	    WSSCREEN_HAS_EMULATOR(scr) && WSSCREEN_HAS_TTY(scr)) {
    288 		struct tty *tp = scr->scr_tty;
    289 		do {
    290 			c = wsdisplay_ocn->cn_getc(wsdisplay_ocn->cn_dev);
    291 			if (c != -1)
    292 				(*tp->t_linesw->l_rint)((unsigned char)c, tp);
    293 		} while (c != -1);
    294 	}
    295 
    296 	callout_schedule(&scr->scr_getc_ch, mstohz(10));
    297 }
    298 #endif
    299 
    300 struct wsscreen *
    301 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
    302 	const struct wsscreen_descr *type, void *cookie, int ccol,
    303 	int crow, long defattr)
    304 {
    305 	struct wsscreen_internal *dconf;
    306 	struct wsscreen *scr;
    307 
    308 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    309 
    310 	if (console) {
    311 		dconf = &wsdisplay_console_conf;
    312 		/*
    313 		 * If there's an emulation, tell it about the callback argument.
    314 		 * The other stuff is already there.
    315 		 */
    316 		if (dconf->wsemul != NULL)
    317 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    318 	} else { /* not console */
    319 		dconf = malloc(sizeof(struct wsscreen_internal),
    320 			       M_DEVBUF, M_WAITOK);
    321 		dconf->emulops = type->textops;
    322 		dconf->emulcookie = cookie;
    323 		if (dconf->emulops) {
    324 			dconf->wsemul = wsemul_pick(emul);
    325 			if (dconf->wsemul == NULL) {
    326 				free(dconf, M_DEVBUF);
    327 				free(scr, M_DEVBUF);
    328 				return (NULL);
    329 			}
    330 			dconf->wsemulcookie =
    331 			  (*dconf->wsemul->attach)(0, type, cookie,
    332 						   ccol, crow, scr, defattr);
    333 		} else
    334 			dconf->wsemul = NULL;
    335 		dconf->scrdata = type;
    336 	}
    337 
    338 	scr->scr_dconf = dconf;
    339 
    340 	scr->scr_tty = tty_alloc();
    341 	tty_attach(scr->scr_tty);
    342 	scr->scr_hold_screen = 0;
    343 	if (WSSCREEN_HAS_EMULATOR(scr))
    344 		scr->scr_flags = 0;
    345 	else
    346 		scr->scr_flags = SCR_GRAPHICS;
    347 
    348 	scr->scr_syncops = 0;
    349 	scr->sc = sc;
    350 #ifdef WSDISPLAY_COMPAT_RAWKBD
    351 	scr->scr_rawkbd = 0;
    352 #endif
    353 #ifdef WSDISPLAY_MULTICONS
    354 	callout_init(&scr->scr_getc_ch, 0);
    355 	callout_setfunc(&scr->scr_getc_ch, wsscreen_getc_poll, scr);
    356 	if (console)
    357 		callout_schedule(&scr->scr_getc_ch, mstohz(10));
    358 #endif
    359 	return (scr);
    360 }
    361 
    362 void
    363 wsscreen_detach(struct wsscreen *scr)
    364 {
    365 	u_int ccol, crow; /* XXX */
    366 
    367 	if (WSSCREEN_HAS_TTY(scr)) {
    368 		tty_detach(scr->scr_tty);
    369 		tty_free(scr->scr_tty);
    370 	}
    371 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    372 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    373 						  &ccol, &crow);
    374 		wsemul_drop(scr->scr_dconf->wsemul);
    375 	}
    376 	if (scr->scr_dconf->scrdata->capabilities & WSSCREEN_FREE)
    377 		free(__UNCONST(scr->scr_dconf->scrdata), M_DEVBUF);
    378 #ifdef WSDISPLAY_MULTICONS
    379 	callout_halt(&scr->scr_getc_ch, NULL);
    380 	callout_destroy(&scr->scr_getc_ch);
    381 #endif
    382 	free(scr->scr_dconf, M_DEVBUF);
    383 	free(scr, M_DEVBUF);
    384 }
    385 
    386 const struct wsscreen_descr *
    387 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
    388 {
    389 	int i;
    390 	const struct wsscreen_descr *scr;
    391 
    392 	KASSERT(scrdata->nscreens > 0);
    393 	if (name == NULL)
    394 		return (scrdata->screens[0]);
    395 
    396 	for (i = 0; i < scrdata->nscreens; i++) {
    397 		scr = scrdata->screens[i];
    398 		if (!strcmp(name, scr->name))
    399 			return (scr);
    400 	}
    401 
    402 	return (0);
    403 }
    404 
    405 /*
    406  * print info about attached screen
    407  */
    408 static void
    409 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
    410 {
    411 	aprint_verbose_dev(sc->sc_dev, "screen %d", idx);
    412 	if (count > 1)
    413 		aprint_verbose("-%d", idx + (count-1));
    414 	aprint_verbose(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
    415 	if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
    416 		aprint_verbose(", %s emulation",
    417 			sc->sc_scr[idx]->scr_dconf->wsemul->name);
    418 	}
    419 	aprint_verbose(")\n");
    420 }
    421 
    422 int
    423 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
    424 	const char *screentype, const char *emul)
    425 {
    426 	const struct wsscreen_descr *scrdesc;
    427 	struct wsscreen_descr *scrdescr2;
    428 	int error;
    429 	void *cookie;
    430 	int ccol, crow;
    431 	long defattr;
    432 	struct wsscreen *scr;
    433 	int s;
    434 
    435 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    436 		return (EINVAL);
    437 	if (sc->sc_scr[idx] != NULL)
    438 		return (EBUSY);
    439 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    440 	if (!scrdesc)
    441 		return (ENXIO);
    442 
    443 	/*
    444 	 * if this screen can resize we need to copy the descr so each screen
    445 	 * gets its own
    446 	 */
    447 	if (scrdesc->capabilities & WSSCREEN_RESIZE) {
    448 		/* we want per screen wsscreen_descr */
    449 		scrdescr2 = malloc(sizeof(struct wsscreen_descr), M_DEVBUF, M_NOWAIT);
    450 		if (scrdescr2 == NULL)
    451 			return ENOMEM;
    452 		memcpy(scrdescr2, scrdesc, sizeof(struct wsscreen_descr));
    453 		scrdescr2->capabilities |= WSSCREEN_FREE;
    454 		scrdesc = scrdescr2;
    455 	}
    456 
    457 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    458 			scrdesc, &cookie, &ccol, &crow, &defattr);
    459 	if (error)
    460 		return (error);
    461 
    462 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    463 			      cookie, ccol, crow, defattr);
    464 	if (scr == NULL) {
    465 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    466 						 cookie);
    467 		return (ENXIO);
    468 	}
    469 
    470 	sc->sc_scr[idx] = scr;
    471 
    472 	/* if no screen has focus yet, activate the first we get */
    473 	s = spltty();
    474 	if (!sc->sc_focus) {
    475 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
    476 						 scr->scr_dconf->emulcookie,
    477 						 0, 0, 0);
    478 		sc->sc_focusidx = idx;
    479 		sc->sc_focus = scr;
    480 	}
    481 	splx(s);
    482 	return (0);
    483 }
    484 
    485 static void
    486 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
    487 {
    488 	int maj, mn, idx;
    489 
    490 	/* hangup */
    491 	if (WSSCREEN_HAS_TTY(scr)) {
    492 		struct tty *tp = scr->scr_tty;
    493 		(*tp->t_linesw->l_modem)(tp, 0);
    494 	}
    495 
    496 	/* locate the major number */
    497 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    498 	/* locate the screen index */
    499 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
    500 		if (scr == sc->sc_scr[idx])
    501 			break;
    502 #ifdef DIAGNOSTIC
    503 	if (idx == WSDISPLAY_MAXSCREEN)
    504 		panic("wsdisplay_forceclose: bad screen");
    505 #endif
    506 
    507 	/* nuke the vnodes */
    508 	mn = WSDISPLAYMINOR(device_unit(sc->sc_dev), idx);
    509 	vdevgone(maj, mn, mn, VCHR);
    510 }
    511 
    512 #ifdef WSDISPLAY_SCROLLSUPPORT
    513 void
    514 wsdisplay_scroll(void *arg, int op)
    515 {
    516 	device_t dv = arg;
    517 	struct wsdisplay_softc *sc = device_private(dv);
    518 	struct wsscreen *scr;
    519 	int lines;
    520 
    521 	scr = sc->sc_focus;
    522 
    523 	if (!scr)
    524 		return;
    525 
    526 	if (op == WSDISPLAY_SCROLL_RESET)
    527 		lines = 0;
    528 	else {
    529 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
    530 			sc->sc_scroll_values.slowlines :
    531 			sc->sc_scroll_values.fastlines;
    532 		if (op & WSDISPLAY_SCROLL_BACKWARD)
    533 			lines = -(lines);
    534 	}
    535 
    536 	if (sc->sc_accessops->scroll) {
    537 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
    538 		    sc->sc_focus->scr_dconf->emulcookie, lines);
    539 	}
    540 }
    541 #endif
    542 
    543 int
    544 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
    545 {
    546 	struct wsscreen *scr;
    547 	int s;
    548 	void *cookie;
    549 
    550 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    551 		return (EINVAL);
    552 	if ((scr = sc->sc_scr[idx]) == NULL)
    553 		return (ENXIO);
    554 
    555 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    556 	    scr->scr_syncops ||
    557 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    558 		return(EBUSY);
    559 
    560 	wsdisplay_closescreen(sc, scr);
    561 
    562 	/*
    563 	 * delete pointers, so neither device entries
    564 	 * nor keyboard input can reference it anymore
    565 	 */
    566 	s = spltty();
    567 	if (sc->sc_focus == scr) {
    568 		sc->sc_focus = 0;
    569 #ifdef WSDISPLAY_COMPAT_RAWKBD
    570 		wsdisplay_update_rawkbd(sc, 0);
    571 #endif
    572 	}
    573 	sc->sc_scr[idx] = 0;
    574 	splx(s);
    575 
    576 	/*
    577 	 * Wake up processes waiting for the screen to
    578 	 * be activated. Sleepers must check whether
    579 	 * the screen still exists.
    580 	 */
    581 	if (scr->scr_flags & SCR_WAITACTIVE)
    582 		wakeup(scr);
    583 
    584 	/* save a reference to the graphics screen */
    585 	cookie = scr->scr_dconf->emulcookie;
    586 
    587 	wsscreen_detach(scr);
    588 
    589 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    590 					 cookie);
    591 
    592 	aprint_verbose_dev(sc->sc_dev, "screen %d deleted\n", idx);
    593 	return (0);
    594 }
    595 
    596 /*
    597  * Autoconfiguration functions.
    598  */
    599 int
    600 wsdisplay_emul_match(device_t parent, cfdata_t match, void *aux)
    601 {
    602 	struct wsemuldisplaydev_attach_args *ap = aux;
    603 
    604 	if (match->cf_loc[WSEMULDISPLAYDEVCF_CONSOLE] !=
    605 	    WSEMULDISPLAYDEVCF_CONSOLE_DEFAULT) {
    606 		/*
    607 		 * If console-ness of device specified, either match
    608 		 * exactly (at high priority), or fail.
    609 		 */
    610 		if (match->cf_loc[WSEMULDISPLAYDEVCF_CONSOLE] != 0 &&
    611 		    ap->console != 0)
    612 			return (10);
    613 		else
    614 			return (0);
    615 	}
    616 
    617 	/* If console-ness unspecified, it wins. */
    618 	return (1);
    619 }
    620 
    621 void
    622 wsdisplay_emul_attach(device_t parent, device_t self, void *aux)
    623 {
    624 	struct wsdisplay_softc *sc = device_private(self);
    625 	struct wsemuldisplaydev_attach_args *ap = aux;
    626 
    627 	sc->sc_dev = self;
    628 
    629 	/* Don't allow more than one console to attach */
    630 	if (wsdisplay_console_attached && ap->console)
    631 		ap->console = 0;
    632 
    633 	wsdisplay_common_attach(sc, ap->console,
    634 	     device_cfdata(self)->cf_loc[WSEMULDISPLAYDEVCF_KBDMUX],
    635 	     ap->scrdata, ap->accessops, ap->accesscookie);
    636 
    637 	if (ap->console) {
    638 		int maj;
    639 
    640 		/* locate the major number */
    641 		maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    642 
    643 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
    644 					 0));
    645 	}
    646 }
    647 
    648 /* Print function (for parent devices). */
    649 int
    650 wsemuldisplaydevprint(void *aux, const char *pnp)
    651 {
    652 #if 0 /* -Wunused */
    653 	struct wsemuldisplaydev_attach_args *ap = aux;
    654 #endif
    655 
    656 	if (pnp)
    657 		aprint_normal("wsdisplay at %s", pnp);
    658 #if 0 /* don't bother; it's ugly */
    659 	aprint_normal(" console %d", ap->console);
    660 #endif
    661 
    662 	return (UNCONF);
    663 }
    664 
    665 int
    666 wsdisplay_emul_detach(device_t dev, int how)
    667 {
    668 	struct wsdisplay_softc *sc = device_private(dev);
    669 	int flag, i, res;
    670 
    671 	flag = (how & DETACH_FORCE ? WSDISPLAY_DELSCR_FORCE : 0);
    672 	for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
    673 		if (sc->sc_scr[i]) {
    674 			res = wsdisplay_delscreen(sc, i, flag);
    675 			if (res)
    676 				return res;
    677 		}
    678 
    679 	cv_destroy(&sc->sc_flagscv);
    680 	mutex_destroy(&sc->sc_flagsmtx);
    681 	return 0;
    682 }
    683 
    684 int
    685 wsdisplay_noemul_match(device_t parent, cfdata_t match, void *aux)
    686 {
    687 #if 0 /* -Wunused */
    688 	struct wsdisplaydev_attach_args *ap = aux;
    689 #endif
    690 
    691 	/* Always match. */
    692 	return (1);
    693 }
    694 
    695 void
    696 wsdisplay_noemul_attach(device_t parent, device_t self, void *aux)
    697 {
    698 	struct wsdisplay_softc *sc = device_private(self);
    699 	struct wsdisplaydev_attach_args *ap = aux;
    700 
    701 	sc->sc_dev = self;
    702 
    703 	wsdisplay_common_attach(sc, 0,
    704 	    device_cfdata(self)->cf_loc[WSDISPLAYDEVCF_KBDMUX], NULL,
    705 	    ap->accessops, ap->accesscookie);
    706 }
    707 
    708 static void
    709 wsdisplay_swdone_cb(void *arg, int error, int waitok)
    710 {
    711 	struct wsdisplay_softc *sc = arg;
    712 
    713 	mutex_enter(&sc->sc_flagsmtx);
    714 	KASSERT(sc->sc_flags & SC_SWITCHPENDING);
    715 	if (error)
    716 		sc->sc_flags |= SC_SWITCHERROR;
    717 	sc->sc_flags &= ~SC_SWITCHPENDING;
    718 	cv_signal(&sc->sc_flagscv);
    719 	mutex_exit(&sc->sc_flagsmtx);
    720 }
    721 
    722 static int
    723 wsdisplay_dosync(struct wsdisplay_softc *sc, int attach)
    724 {
    725 	struct wsscreen *scr;
    726 	int (*op)(void *, int, void (*)(void *, int, int), void *);
    727 	int res;
    728 
    729 	scr = sc->sc_focus;
    730 	if (!scr || !scr->scr_syncops)
    731 		return 0; /* XXX check SCR_GRAPHICS? */
    732 
    733 	sc->sc_flags |= SC_SWITCHPENDING;
    734 	sc->sc_flags &= ~SC_SWITCHERROR;
    735 	if (attach)
    736 		op = scr->scr_syncops->attach;
    737 	else
    738 		op = scr->scr_syncops->detach;
    739 	res = (*op)(scr->scr_synccookie, 1, wsdisplay_swdone_cb, sc);
    740 	if (res == EAGAIN) {
    741 		/* wait for callback */
    742 		mutex_enter(&sc->sc_flagsmtx);
    743 		while (sc->sc_flags & SC_SWITCHPENDING)
    744 			cv_wait_sig(&sc->sc_flagscv, &sc->sc_flagsmtx);
    745 		mutex_exit(&sc->sc_flagsmtx);
    746 		if (sc->sc_flags & SC_SWITCHERROR)
    747 			return (EIO); /* XXX pass real error */
    748 	} else {
    749 		sc->sc_flags &= ~SC_SWITCHPENDING;
    750 		if (res)
    751 			return (res);
    752 	}
    753 	if (attach)
    754 		sc->sc_flags |= SC_XATTACHED;
    755 	else
    756 		sc->sc_flags &= ~SC_XATTACHED;
    757 	return 0;
    758 }
    759 
    760 int
    761 wsdisplay_handlex(int resume)
    762 {
    763 	int i, res;
    764 	device_t dv;
    765 
    766 	for (i = 0; i < wsdisplay_cd.cd_ndevs; i++) {
    767 		dv = device_lookup(&wsdisplay_cd, i);
    768 		if (!dv)
    769 			continue;
    770 		res = wsdisplay_dosync(device_private(dv), resume);
    771 		if (res)
    772 			return (res);
    773 	}
    774 	return (0);
    775 }
    776 
    777 static bool
    778 wsdisplay_suspend(device_t dv, const pmf_qual_t *qual)
    779 {
    780 	struct wsdisplay_softc *sc = device_private(dv);
    781 #ifdef DIAGNOSTIC
    782 	struct wsscreen *scr = sc->sc_focus;
    783 	if (sc->sc_flags & SC_XATTACHED) {
    784 		KASSERT(scr && scr->scr_syncops);
    785 	}
    786 #endif
    787 #if 1
    788 	/*
    789 	 * XXX X servers should have been detached earlier.
    790 	 * pmf currently ignores our return value and suspends the system
    791 	 * after device suspend failures. We try to avoid bigger damage
    792 	 * and try to detach the X server here. This is not safe because
    793 	 * other parts of the system which the X server deals with
    794 	 * might already be suspended.
    795 	 */
    796 	if (sc->sc_flags & SC_XATTACHED) {
    797 		printf("%s: emergency X server detach\n", device_xname(dv));
    798 		wsdisplay_dosync(sc, 0);
    799 	}
    800 #endif
    801 	return (!(sc->sc_flags & SC_XATTACHED));
    802 }
    803 
    804 /* Print function (for parent devices). */
    805 int
    806 wsdisplaydevprint(void *aux, const char *pnp)
    807 {
    808 #if 0 /* -Wunused */
    809 	struct wsdisplaydev_attach_args *ap = aux;
    810 #endif
    811 
    812 	if (pnp)
    813 		aprint_normal("wsdisplay at %s", pnp);
    814 
    815 	return (UNCONF);
    816 }
    817 
    818 static void
    819 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
    820 	const struct wsscreen_list *scrdata,
    821 	const struct wsdisplay_accessops *accessops,
    822 	void *accesscookie)
    823 {
    824 	int i, start=0;
    825 #if NWSKBD > 0
    826 	struct wsevsrc *kme;
    827 #if NWSMUX > 0
    828 	struct wsmux_softc *mux;
    829 
    830 	if (kbdmux >= 0)
    831 		mux = wsmux_getmux(kbdmux);
    832 	else
    833 		mux = wsmux_create("dmux", device_unit(sc->sc_dev));
    834 	/* XXX panic()ing isn't nice, but attach cannot fail */
    835 	if (mux == NULL)
    836 		panic("wsdisplay_common_attach: no memory");
    837 	sc->sc_input = &mux->sc_base;
    838 	mux->sc_base.me_dispdv = sc->sc_dev;
    839 	aprint_normal(" kbdmux %d", kbdmux);
    840 #else
    841 	if (kbdmux >= 0)
    842 		aprint_normal(" (kbdmux ignored)");
    843 #endif
    844 #endif
    845 
    846 	sc->sc_isconsole = console;
    847 
    848 	if (console) {
    849 		KASSERT(wsdisplay_console_initted);
    850 		KASSERT(wsdisplay_console_device == NULL);
    851 
    852 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    853 		wsdisplay_console_device = sc;
    854 
    855 		aprint_normal(": console (%s, %s emulation)",
    856 		       wsdisplay_console_conf.scrdata->name,
    857 		       wsdisplay_console_conf.wsemul->name);
    858 
    859 #if NWSKBD > 0
    860 		kme = wskbd_set_console_display(sc->sc_dev, sc->sc_input);
    861 		if (kme != NULL)
    862 			aprint_normal(", using %s", device_xname(kme->me_dv));
    863 #if NWSMUX == 0
    864 		sc->sc_input = kme;
    865 #endif
    866 #endif
    867 
    868 		sc->sc_focusidx = 0;
    869 		sc->sc_focus = sc->sc_scr[0];
    870 		start = 1;
    871 
    872 		wsdisplay_console_attached = 1;
    873 	}
    874 	aprint_normal("\n");
    875 	aprint_naive("\n");
    876 
    877 #if NWSKBD > 0 && NWSMUX > 0
    878 	wsmux_set_display(mux, sc->sc_dev);
    879 #endif
    880 
    881 	mutex_init(&sc->sc_flagsmtx, MUTEX_DEFAULT, IPL_NONE);
    882 	cv_init(&sc->sc_flagscv, "wssw");
    883 
    884 	sc->sc_accessops = accessops;
    885 	sc->sc_accesscookie = accesscookie;
    886 	sc->sc_scrdata = scrdata;
    887 
    888 #ifdef WSDISPLAY_SCROLLSUPPORT
    889 	sc->sc_scroll_values = wsdisplay_default_scroll_values;
    890 #endif
    891 
    892 	/*
    893 	 * Set up a number of virtual screens if wanted. The
    894 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    895 	 * is for special cases like installation kernels.
    896 	 */
    897 	for (i = start; i < wsdisplay_defaultscreens; i++) {
    898 		if (wsdisplay_addscreen(sc, i, 0, 0))
    899 			break;
    900 	}
    901 
    902 	if (i > start)
    903 		wsdisplay_addscreen_print(sc, start, i-start);
    904 
    905 	if (!pmf_device_register(sc->sc_dev, wsdisplay_suspend, NULL))
    906 		aprint_error_dev(sc->sc_dev,
    907 		    "couldn't establish power handler\n");
    908 }
    909 
    910 void
    911 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
    912 	int ccol, int crow, long defattr)
    913 {
    914 	const struct wsemul_ops *wsemul;
    915 
    916 	KASSERT(wsdisplay_console_initted < 2);
    917 	KASSERT(type->nrows > 0);
    918 	KASSERT(type->ncols > 0);
    919 	KASSERT(crow < type->nrows);
    920 	KASSERT(ccol < type->ncols);
    921 
    922 	wsdisplay_console_conf.emulops = type->textops;
    923 	wsdisplay_console_conf.emulcookie = cookie;
    924 	wsdisplay_console_conf.scrdata = type;
    925 
    926 	wsemul = wsemul_pick(0); /* default */
    927 	wsdisplay_console_conf.wsemul = wsemul;
    928 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    929 								  ccol, crow,
    930 								  defattr);
    931 
    932 	if (cn_tab != &wsdisplay_cons)
    933 		wsdisplay_ocn = cn_tab;
    934 	cn_tab = &wsdisplay_cons;
    935 	wsdisplay_console_initted = 2;
    936 }
    937 
    938 void
    939 wsdisplay_preattach(const struct wsscreen_descr *type, void *cookie,
    940 	int ccol, int crow, long defattr)
    941 {
    942 	const struct wsemul_ops *wsemul;
    943 
    944 	KASSERT(!wsdisplay_console_initted);
    945 	KASSERT(type->nrows > 0);
    946 	KASSERT(type->ncols > 0);
    947 	KASSERT(crow < type->nrows);
    948 	KASSERT(ccol < type->ncols);
    949 
    950 	wsdisplay_console_conf.emulops = type->textops;
    951 	wsdisplay_console_conf.emulcookie = cookie;
    952 	wsdisplay_console_conf.scrdata = type;
    953 
    954 	wsemul = wsemul_pick(0); /* default */
    955 	wsdisplay_console_conf.wsemul = wsemul;
    956 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    957 								  ccol, crow,
    958 								  defattr);
    959 
    960 	if (cn_tab != &wsdisplay_cons)
    961 		wsdisplay_ocn = cn_tab;
    962 	cn_tab = &wsdisplay_cons;
    963 	wsdisplay_console_initted = 1;
    964 }
    965 
    966 void
    967 wsdisplay_cndetach(void)
    968 {
    969 	if (wsdisplay_console_initted > 0) {
    970 		cn_tab = wsdisplay_ocn;
    971 		wsdisplay_console_initted = 0;
    972 		wsdisplay_console_attached = 0;
    973 		wsdisplay_console_device = NULL;
    974 	}
    975 }
    976 
    977 /*
    978  * Tty and cdevsw functions.
    979  */
    980 int
    981 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
    982 {
    983 	struct wsdisplay_softc *sc;
    984 	struct tty *tp;
    985 	int newopen, error;
    986 	struct wsscreen *scr;
    987 
    988 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    989 	if (sc == NULL)			/* make sure it was attached */
    990 		return (ENXIO);
    991 
    992 	if (ISWSDISPLAYSTAT(dev)) {
    993 		wsevent_init(&sc->evar, l->l_proc);
    994 		return (0);
    995 	}
    996 
    997 	if (ISWSDISPLAYCTL(dev))
    998 		return (0);
    999 
   1000 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
   1001 		return (ENXIO);
   1002 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1003 		return (ENXIO);
   1004 
   1005 	if (WSSCREEN_HAS_TTY(scr)) {
   1006 		tp = scr->scr_tty;
   1007 		tp->t_oproc = wsdisplaystart;
   1008 		tp->t_param = wsdisplayparam;
   1009 		tp->t_dev = dev;
   1010 		newopen = (tp->t_state & TS_ISOPEN) == 0;
   1011 
   1012 		if (kauth_authorize_device_tty(l->l_cred,
   1013 			KAUTH_DEVICE_TTY_OPEN, tp))
   1014 			return (EBUSY);
   1015 
   1016 		if (newopen) {
   1017 			ttychars(tp);
   1018 			tp->t_iflag = TTYDEF_IFLAG;
   1019 			tp->t_oflag = TTYDEF_OFLAG;
   1020 			tp->t_cflag = TTYDEF_CFLAG;
   1021 			tp->t_lflag = TTYDEF_LFLAG;
   1022 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
   1023 			wsdisplayparam(tp, &tp->t_termios);
   1024 			ttsetwater(tp);
   1025 		}
   1026 		tp->t_state |= TS_CARR_ON;
   1027 
   1028 		error = ((*tp->t_linesw->l_open)(dev, tp));
   1029 		if (error)
   1030 			return (error);
   1031 
   1032 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
   1033 			/* set window sizes as appropriate, and reset
   1034 			 the emulation */
   1035 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
   1036 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
   1037 
   1038 			/* wsdisplay_set_emulation() */
   1039 		}
   1040 	}
   1041 
   1042 	scr->scr_flags |= SCR_OPEN;
   1043 	return (0);
   1044 }
   1045 
   1046 int
   1047 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
   1048 {
   1049 	device_t dv;
   1050 	struct wsdisplay_softc *sc;
   1051 	struct tty *tp;
   1052 	struct wsscreen *scr;
   1053 
   1054 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1055 	sc = device_private(dv);
   1056 
   1057 	if (ISWSDISPLAYSTAT(dev)) {
   1058 		wsevent_fini(&sc->evar);
   1059 		return (0);
   1060 	}
   1061 
   1062 	if (ISWSDISPLAYCTL(dev))
   1063 		return (0);
   1064 
   1065 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1066 		return (0);
   1067 
   1068 	if (WSSCREEN_HAS_TTY(scr)) {
   1069 		if (scr->scr_hold_screen) {
   1070 			int s;
   1071 
   1072 			/* XXX RESET KEYBOARD LEDS, etc. */
   1073 			s = spltty();	/* avoid conflict with keyboard */
   1074 			wsdisplay_kbdholdscreen(dv, 0);
   1075 			splx(s);
   1076 		}
   1077 		tp = scr->scr_tty;
   1078 		(*tp->t_linesw->l_close)(tp, flag);
   1079 		ttyclose(tp);
   1080 	}
   1081 
   1082 	if (scr->scr_syncops)
   1083 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
   1084 
   1085 	if (WSSCREEN_HAS_EMULATOR(scr)) {
   1086 		scr->scr_flags &= ~SCR_GRAPHICS;
   1087 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1088 						 WSEMUL_RESET);
   1089 		if (wsdisplay_clearonclose)
   1090 			(*scr->scr_dconf->wsemul->reset)
   1091 				(scr->scr_dconf->wsemulcookie,
   1092 				 WSEMUL_CLEARSCREEN);
   1093 	}
   1094 
   1095 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1096 	if (scr->scr_rawkbd) {
   1097 		int kbmode = WSKBD_TRANSLATED;
   1098 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
   1099 					       (void *)&kbmode, 0, l);
   1100 	}
   1101 #endif
   1102 
   1103 	scr->scr_flags &= ~SCR_OPEN;
   1104 
   1105 	return (0);
   1106 }
   1107 
   1108 int
   1109 wsdisplayread(dev_t dev, struct uio *uio, int flag)
   1110 {
   1111 	struct wsdisplay_softc *sc;
   1112 	struct tty *tp;
   1113 	struct wsscreen *scr;
   1114 	int error;
   1115 
   1116 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1117 
   1118 	if (ISWSDISPLAYSTAT(dev)) {
   1119 		error = wsevent_read(&sc->evar, uio, flag);
   1120 		return (error);
   1121 	}
   1122 
   1123 	if (ISWSDISPLAYCTL(dev))
   1124 		return (0);
   1125 
   1126 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1127 		return (ENXIO);
   1128 
   1129 	if (!WSSCREEN_HAS_TTY(scr))
   1130 		return (ENODEV);
   1131 
   1132 	tp = scr->scr_tty;
   1133 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
   1134 }
   1135 
   1136 int
   1137 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
   1138 {
   1139 	struct wsdisplay_softc *sc;
   1140 	struct tty *tp;
   1141 	struct wsscreen *scr;
   1142 
   1143 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1144 
   1145 	if (ISWSDISPLAYSTAT(dev)) {
   1146 		return (0);
   1147 	}
   1148 
   1149 	if (ISWSDISPLAYCTL(dev))
   1150 		return (0);
   1151 
   1152 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1153 		return (ENXIO);
   1154 
   1155 	if (!WSSCREEN_HAS_TTY(scr))
   1156 		return (ENODEV);
   1157 
   1158 	tp = scr->scr_tty;
   1159 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
   1160 }
   1161 
   1162 int
   1163 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
   1164 {
   1165 	struct wsdisplay_softc *sc;
   1166 	struct tty *tp;
   1167 	struct wsscreen *scr;
   1168 
   1169 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1170 
   1171 	if (ISWSDISPLAYSTAT(dev))
   1172 		return (wsevent_poll(&sc->evar, events, l));
   1173 
   1174 	if (ISWSDISPLAYCTL(dev))
   1175 		return (0);
   1176 
   1177 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1178 		return (POLLHUP);
   1179 
   1180 	if (!WSSCREEN_HAS_TTY(scr))
   1181 		return (POLLERR);
   1182 
   1183 	tp = scr->scr_tty;
   1184 	return ((*tp->t_linesw->l_poll)(tp, events, l));
   1185 }
   1186 
   1187 int
   1188 wsdisplaykqfilter(dev_t dev, struct knote *kn)
   1189 {
   1190 	struct wsdisplay_softc *sc;
   1191 	struct wsscreen *scr;
   1192 
   1193 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1194 
   1195 	if (ISWSDISPLAYCTL(dev))
   1196 		return (1);
   1197 
   1198 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1199 		return (1);
   1200 
   1201 
   1202 	if (WSSCREEN_HAS_TTY(scr))
   1203 		return (ttykqfilter(dev, kn));
   1204 	else
   1205 		return (1);
   1206 }
   1207 
   1208 struct tty *
   1209 wsdisplaytty(dev_t dev)
   1210 {
   1211 	struct wsdisplay_softc *sc;
   1212 	struct wsscreen *scr;
   1213 
   1214 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1215 
   1216 	if (ISWSDISPLAYSTAT(dev))
   1217 		panic("wsdisplaytty() on status device");
   1218 
   1219 	if (ISWSDISPLAYCTL(dev))
   1220 		panic("wsdisplaytty() on ctl device");
   1221 
   1222 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1223 		return NULL;
   1224 
   1225 	return (scr->scr_tty);
   1226 }
   1227 
   1228 int
   1229 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1230 {
   1231 	device_t dv;
   1232 	struct wsdisplay_softc *sc;
   1233 	struct tty *tp;
   1234 	int error;
   1235 	struct wsscreen *scr;
   1236 
   1237 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1238 	sc = device_private(dv);
   1239 
   1240 #ifdef WSDISPLAY_COMPAT_USL
   1241 	error = wsdisplay_usl_ioctl1(dv, cmd, data, flag, l);
   1242 	if (error != EPASSTHROUGH)
   1243 		return (error);
   1244 #endif
   1245 
   1246 	if (ISWSDISPLAYSTAT(dev))
   1247 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
   1248 
   1249 	if (ISWSDISPLAYCTL(dev))
   1250 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
   1251 
   1252 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1253 		return (ENXIO);
   1254 
   1255 	if (WSSCREEN_HAS_TTY(scr)) {
   1256 		tp = scr->scr_tty;
   1257 
   1258 		/* do the line discipline ioctls first */
   1259 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1260 		if (error != EPASSTHROUGH)
   1261 			return (error);
   1262 
   1263 		/* then the tty ioctls */
   1264 		error = ttioctl(tp, cmd, data, flag, l);
   1265 		if (error != EPASSTHROUGH)
   1266 			return (error);
   1267 	}
   1268 
   1269 #ifdef WSDISPLAY_COMPAT_USL
   1270 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
   1271 	if (error != EPASSTHROUGH)
   1272 		return (error);
   1273 #endif
   1274 
   1275 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
   1276 }
   1277 
   1278 int
   1279 wsdisplay_param(device_t dv, u_long cmd, struct wsdisplay_param *dp)
   1280 {
   1281 	struct wsdisplay_softc *sc = device_private(dv);
   1282 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1283 					   sc->sc_focus->scr_dconf->emulcookie,
   1284 					   cmd, (void *)dp, 0, NULL));
   1285 }
   1286 
   1287 int
   1288 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1289 	u_long cmd, void *data, int flag, struct lwp *l)
   1290 {
   1291 	int error;
   1292 	char namebuf[32];
   1293 	struct wsdisplay_font fd;
   1294 #ifdef WSDISPLAY_SCROLLSUPPORT
   1295 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1296 #endif
   1297 
   1298 #if NWSKBD > 0
   1299 	struct wsevsrc *inp;
   1300 
   1301 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1302 	switch (cmd) {
   1303 	case WSKBDIO_SETMODE:
   1304 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1305 		return (wsdisplay_update_rawkbd(sc, scr));
   1306 	case WSKBDIO_GETMODE:
   1307 		*(int *)data = (scr->scr_rawkbd ?
   1308 				WSKBD_RAW : WSKBD_TRANSLATED);
   1309 		return (0);
   1310 	}
   1311 #endif
   1312 	inp = sc->sc_input;
   1313 	if (inp == NULL)
   1314 		return (ENXIO);
   1315 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1316 	if (error != EPASSTHROUGH)
   1317 		return (error);
   1318 #endif /* NWSKBD > 0 */
   1319 
   1320 	switch (cmd) {
   1321 	case WSDISPLAYIO_GMODE:
   1322 		if (scr->scr_flags & SCR_GRAPHICS) {
   1323 			if (scr->scr_flags & SCR_DUMBFB)
   1324 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1325 			else
   1326 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1327 		} else
   1328 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1329 		return (0);
   1330 
   1331 	case WSDISPLAYIO_SMODE:
   1332 #define d (*(int *)data)
   1333 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1334 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1335 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1336 			return (EINVAL);
   1337 
   1338 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1339 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1340 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1341 			d == WSDISPLAYIO_MODE_DUMBFB)
   1342 			    scr->scr_flags |= SCR_GRAPHICS |
   1343 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1344 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1345 		    return (EINVAL);
   1346 
   1347 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1348 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
   1349 
   1350 	    return (0);
   1351 #undef d
   1352 
   1353 #ifdef WSDISPLAY_SCROLLSUPPORT
   1354 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1355     do {								\
   1356 	(dstp)->fastlines = ((srcp)->which &				\
   1357 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1358 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1359 	(dstp)->slowlines = ((srcp)->which &				\
   1360 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1361 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1362 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1363     } while (0)
   1364 
   1365 
   1366 	case WSDISPLAYIO_DSSCROLL:
   1367 		usdp = (struct wsdisplay_scroll_data *)data;
   1368 		ksdp = &sc->sc_scroll_values;
   1369 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1370 		return (0);
   1371 
   1372 	case WSDISPLAYIO_DGSCROLL:
   1373 		usdp = (struct wsdisplay_scroll_data *)data;
   1374 		ksdp = &sc->sc_scroll_values;
   1375 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1376 		return (0);
   1377 #else
   1378 	case WSDISPLAYIO_DSSCROLL:
   1379 	case WSDISPLAYIO_DGSCROLL:
   1380 		return ENODEV;
   1381 #endif
   1382 
   1383 	case WSDISPLAYIO_SFONT:
   1384 #define d ((struct wsdisplay_usefontdata *)data)
   1385 		if (!sc->sc_accessops->load_font)
   1386 			return (EINVAL);
   1387 		if (d->name) {
   1388 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1389 			if (error)
   1390 				return (error);
   1391 			fd.name = namebuf;
   1392 		} else
   1393 			fd.name = 0;
   1394 		fd.data = 0;
   1395 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1396 					scr->scr_dconf->emulcookie, &fd);
   1397 		if (!error && WSSCREEN_HAS_EMULATOR(scr)) {
   1398 			(*scr->scr_dconf->wsemul->reset)
   1399 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1400 			if (scr->scr_dconf->wsemul->resize) {
   1401 				(*scr->scr_dconf->wsemul->resize)
   1402 					(scr->scr_dconf->wsemulcookie,
   1403 					 scr->scr_dconf->scrdata);
   1404 				/* update the tty's size */
   1405 				scr->scr_tty->t_winsize.ws_row =
   1406 				    scr->scr_dconf->scrdata->nrows;
   1407 				scr->scr_tty->t_winsize.ws_col =
   1408 				    scr->scr_dconf->scrdata->ncols;
   1409 				/* send SIGWINCH to the process group on our tty */
   1410 				kpreempt_disable();
   1411 				ttysig(scr->scr_tty, TTYSIG_PG1, SIGWINCH);
   1412 				kpreempt_enable();
   1413 			}
   1414 		}
   1415 		return (error);
   1416 #undef d
   1417 
   1418 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1419 	case WSDISPLAYIO_GMSGATTRS:
   1420 #define d ((struct wsdisplay_msgattrs *)data)
   1421 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1422 		    (scr->scr_dconf->wsemulcookie, d);
   1423 		return (0);
   1424 #undef d
   1425 
   1426 	case WSDISPLAYIO_SMSGATTRS: {
   1427 #define d ((struct wsdisplay_msgattrs *)data)
   1428 		int i;
   1429 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1430 			if (sc->sc_scr[i] != NULL)
   1431 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1432 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1433 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1434 				     d);
   1435 		}
   1436 		return (0);
   1437 #undef d
   1438 #else
   1439 	case WSDISPLAYIO_GMSGATTRS:
   1440 	case WSDISPLAYIO_SMSGATTRS:
   1441 		return (ENODEV);
   1442 #endif
   1443 	case WSDISPLAYIO_SETVERSION:
   1444 		return wsevent_setversion(&sc->evar, *(int *)data);
   1445 	}
   1446 
   1447 	/* check ioctls for display */
   1448 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1449 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
   1450 }
   1451 
   1452 int
   1453 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1454 	int flag, struct lwp *l)
   1455 {
   1456 	switch (cmd) {
   1457 	case WSDISPLAYIO_GETACTIVESCREEN:
   1458 		*(int*)data = wsdisplay_getactivescreen(sc);
   1459 		return (0);
   1460 	}
   1461 
   1462 	return (EPASSTHROUGH);
   1463 }
   1464 
   1465 int
   1466 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1467 	int flag, struct lwp *l)
   1468 {
   1469 	int error;
   1470 	char *type, typebuf[16], *emul, emulbuf[16];
   1471 	void *tbuf;
   1472 	u_int fontsz;
   1473 #if defined(COMPAT_14) && NWSKBD > 0
   1474 	struct wsmux_device wsmuxdata;
   1475 #endif
   1476 #if NWSKBD > 0
   1477 	struct wsevsrc *inp;
   1478 #endif
   1479 
   1480 	switch (cmd) {
   1481 	case WSDISPLAYIO_ADDSCREEN:
   1482 #define d ((struct wsdisplay_addscreendata *)data)
   1483 		if (d->screentype) {
   1484 			error = copyinstr(d->screentype, typebuf,
   1485 					  sizeof(typebuf), 0);
   1486 			if (error)
   1487 				return (error);
   1488 			type = typebuf;
   1489 		} else
   1490 			type = 0;
   1491 		if (d->emul) {
   1492 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1493 			if (error)
   1494 				return (error);
   1495 			emul = emulbuf;
   1496 		} else
   1497 			emul = 0;
   1498 
   1499 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1500 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1501 		return (error);
   1502 #undef d
   1503 	case WSDISPLAYIO_DELSCREEN:
   1504 #define d ((struct wsdisplay_delscreendata *)data)
   1505 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1506 #undef d
   1507 	case WSDISPLAYIO_LDFONT:
   1508 #define d ((struct wsdisplay_font *)data)
   1509 		if (!sc->sc_accessops->load_font)
   1510 			return (EINVAL);
   1511 		if (d->name) {
   1512 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1513 			if (error)
   1514 				return (error);
   1515 			d->name = typebuf;
   1516 		} else
   1517 			d->name = "loaded"; /* ??? */
   1518 		fontsz = d->fontheight * d->stride * d->numchars;
   1519 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1520 			return (EINVAL);
   1521 
   1522 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1523 		error = copyin(d->data, tbuf, fontsz);
   1524 		if (error) {
   1525 			free(tbuf, M_DEVBUF);
   1526 			return (error);
   1527 		}
   1528 		d->data = tbuf;
   1529 		error =
   1530 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1531 		free(tbuf, M_DEVBUF);
   1532 #undef d
   1533 		return (error);
   1534 
   1535 #if NWSKBD > 0
   1536 #ifdef COMPAT_14
   1537 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1538 #define d ((struct wsdisplay_kbddata *)data)
   1539 		inp = sc->sc_input;
   1540 		if (inp == NULL)
   1541 			return (ENXIO);
   1542 		switch (d->op) {
   1543 		case _O_WSDISPLAY_KBD_ADD:
   1544 			if (d->idx == -1) {
   1545 				d->idx = wskbd_pickfree();
   1546 				if (d->idx == -1)
   1547 					return (ENXIO);
   1548 			}
   1549 			wsmuxdata.type = WSMUX_KBD;
   1550 			wsmuxdata.idx = d->idx;
   1551 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1552 					      &wsmuxdata, flag, l));
   1553 		case _O_WSDISPLAY_KBD_DEL:
   1554 			wsmuxdata.type = WSMUX_KBD;
   1555 			wsmuxdata.idx = d->idx;
   1556 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1557 					      &wsmuxdata, flag, l));
   1558 		default:
   1559 			return (EINVAL);
   1560 		}
   1561 #undef d
   1562 #endif
   1563 
   1564 	case WSMUXIO_ADD_DEVICE:
   1565 #define d ((struct wsmux_device *)data)
   1566 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1567 			d->idx = wskbd_pickfree();
   1568 #undef d
   1569 		/* fall into */
   1570 	case WSMUXIO_INJECTEVENT:
   1571 	case WSMUXIO_REMOVE_DEVICE:
   1572 	case WSMUXIO_LIST_DEVICES:
   1573 		inp = sc->sc_input;
   1574 		if (inp == NULL)
   1575 			return (ENXIO);
   1576 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1577 #endif /* NWSKBD > 0 */
   1578 
   1579 	}
   1580 	return (EPASSTHROUGH);
   1581 }
   1582 
   1583 int
   1584 wsdisplay_stat_inject(device_t dv, u_int type, int value)
   1585 {
   1586 	struct wsdisplay_softc *sc = device_private(dv);
   1587 	struct wseventvar *evar;
   1588 	struct wscons_event event;
   1589 
   1590 	evar = &sc->evar;
   1591 
   1592 	if (evar == NULL)
   1593 		return (0);
   1594 
   1595 	if (evar->q == NULL)
   1596 		return (1);
   1597 
   1598 	event.type = type;
   1599 	event.value = value;
   1600 	if (wsevent_inject(evar, &event, 1) != 0) {
   1601 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1602 		return (1);
   1603 	}
   1604 
   1605 	return (0);
   1606 }
   1607 
   1608 paddr_t
   1609 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1610 {
   1611 	struct wsdisplay_softc *sc;
   1612 	struct wsscreen *scr;
   1613 
   1614 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1615 
   1616 	if (ISWSDISPLAYSTAT(dev))
   1617 		return (-1);
   1618 
   1619 	if (ISWSDISPLAYCTL(dev))
   1620 		return (-1);
   1621 
   1622 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1623 		return (-1);
   1624 
   1625 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1626 		return (-1);
   1627 
   1628 	/* pass mmap to display */
   1629 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
   1630 	    scr->scr_dconf->emulcookie, offset, prot));
   1631 }
   1632 
   1633 void
   1634 wsdisplaystart(struct tty *tp)
   1635 {
   1636 	struct wsdisplay_softc *sc;
   1637 	struct wsscreen *scr;
   1638 	int s, n;
   1639 	u_char *tbuf;
   1640 
   1641 	s = spltty();
   1642 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1643 		splx(s);
   1644 		return;
   1645 	}
   1646 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1647 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
   1648 		splx(s);
   1649 		return;
   1650 	}
   1651 
   1652 	if (scr->scr_hold_screen) {
   1653 		tp->t_state |= TS_TIMEOUT;
   1654 		splx(s);
   1655 		return;
   1656 	}
   1657 	tp->t_state |= TS_BUSY;
   1658 	splx(s);
   1659 
   1660 #ifdef DIAGNOSTIC
   1661 	scr->scr_in_ttyoutput = 1;
   1662 #endif
   1663 
   1664 	/*
   1665 	 * Drain output from ring buffer.
   1666 	 * The output will normally be in one contiguous chunk, but when the
   1667 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1668 	 * the other at the start.  For performance, rather than loop here,
   1669 	 * we output one chunk, see if there's another one, and if so, output
   1670 	 * it too.
   1671 	 */
   1672 
   1673 	n = ndqb(&tp->t_outq, 0);
   1674 	tbuf = tp->t_outq.c_cf;
   1675 
   1676 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1677 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1678 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1679 						  tbuf, n, 0);
   1680 #ifdef WSDISPLAY_MULTICONS
   1681 		if (wsdisplay_multicons_enable &&
   1682 		    scr->scr_dconf == &wsdisplay_console_conf &&
   1683 		    wsdisplay_ocn && wsdisplay_ocn->cn_putc) {
   1684 			for (int i = 0; i < n; i++)
   1685 				wsdisplay_ocn->cn_putc(
   1686 				    wsdisplay_ocn->cn_dev, tbuf[i]);
   1687 		}
   1688 #endif
   1689 	}
   1690 	ndflush(&tp->t_outq, n);
   1691 
   1692 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1693 		tbuf = tp->t_outq.c_cf;
   1694 
   1695 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1696 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1697 			(*scr->scr_dconf->wsemul->output)
   1698 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
   1699 
   1700 #ifdef WSDISPLAY_MULTICONS
   1701 			if (wsdisplay_multicons_enable &&
   1702 			    scr->scr_dconf == &wsdisplay_console_conf &&
   1703 			    wsdisplay_ocn && wsdisplay_ocn->cn_putc) {
   1704 				for (int i = 0; i < n; i++)
   1705 					wsdisplay_ocn->cn_putc(
   1706 					    wsdisplay_ocn->cn_dev, tbuf[i]);
   1707 			}
   1708 #endif
   1709 		}
   1710 		ndflush(&tp->t_outq, n);
   1711 	}
   1712 
   1713 #ifdef DIAGNOSTIC
   1714 	scr->scr_in_ttyoutput = 0;
   1715 #endif
   1716 
   1717 	s = spltty();
   1718 	tp->t_state &= ~TS_BUSY;
   1719 	/* Come back if there's more to do */
   1720 	if (ttypull(tp)) {
   1721 		tp->t_state |= TS_TIMEOUT;
   1722 		callout_schedule(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1);
   1723 	}
   1724 	splx(s);
   1725 }
   1726 
   1727 void
   1728 wsdisplaystop(struct tty *tp, int flag)
   1729 {
   1730 	int s;
   1731 
   1732 	s = spltty();
   1733 	if (ISSET(tp->t_state, TS_BUSY))
   1734 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1735 			SET(tp->t_state, TS_FLUSH);
   1736 	splx(s);
   1737 }
   1738 
   1739 /* Set line parameters. */
   1740 int
   1741 wsdisplayparam(struct tty *tp, struct termios *t)
   1742 {
   1743 
   1744 	tp->t_ispeed = t->c_ispeed;
   1745 	tp->t_ospeed = t->c_ospeed;
   1746 	tp->t_cflag = t->c_cflag;
   1747 	return 0;
   1748 }
   1749 
   1750 /*
   1751  * Callbacks for the emulation code.
   1752  */
   1753 void
   1754 wsdisplay_emulbell(void *v)
   1755 {
   1756 	struct wsscreen *scr = v;
   1757 
   1758 	if (scr == NULL)		/* console, before real attach */
   1759 		return;
   1760 
   1761 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1762 		return;
   1763 
   1764 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1765 					FWRITE, NULL);
   1766 }
   1767 
   1768 void
   1769 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1770 {
   1771 	struct wsscreen *scr = v;
   1772 	struct tty *tp;
   1773 	int (*ifcn)(int, struct tty *);
   1774 
   1775 	if (v == NULL)			/* console, before real attach */
   1776 		return;
   1777 
   1778 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1779 		return;
   1780 	if (!WSSCREEN_HAS_TTY(scr))
   1781 		return;
   1782 
   1783 	tp = scr->scr_tty;
   1784 
   1785 	/*
   1786 	 * XXX bad hack to work around locking problems in tty.c:
   1787 	 * ttyinput() will try to lock again, causing deadlock.
   1788 	 * We assume that wsdisplay_emulinput() can only be called
   1789 	 * from within wsdisplaystart(), and thus the tty lock
   1790 	 * is already held. Use an entry point which doesn't lock.
   1791 	 */
   1792 	KASSERT(scr->scr_in_ttyoutput);
   1793 	ifcn = tp->t_linesw->l_rint;
   1794 	if (ifcn == ttyinput)
   1795 		ifcn = ttyinput_wlock;
   1796 
   1797 	while (count-- > 0)
   1798 		(*ifcn)(*data++, tp);
   1799 }
   1800 
   1801 /*
   1802  * Calls from the keyboard interface.
   1803  */
   1804 void
   1805 wsdisplay_kbdinput(device_t dv, keysym_t ks)
   1806 {
   1807 	struct wsdisplay_softc *sc = device_private(dv);
   1808 	struct wsscreen *scr;
   1809 	const char *dp;
   1810 	int count;
   1811 	struct tty *tp;
   1812 
   1813 	KASSERT(sc != NULL);
   1814 
   1815 	scr = sc->sc_focus;
   1816 
   1817 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1818 		return;
   1819 
   1820 	tp = scr->scr_tty;
   1821 
   1822 	if (KS_GROUP(ks) == KS_GROUP_Plain && KS_VALUE(ks) <= 0x7f)
   1823 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
   1824 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1825 		count = (*scr->scr_dconf->wsemul->translate)
   1826 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1827 		while (count-- > 0)
   1828 			(*tp->t_linesw->l_rint)((unsigned char)(*dp++), tp);
   1829 	}
   1830 }
   1831 
   1832 #if defined(WSDISPLAY_COMPAT_RAWKBD)
   1833 int
   1834 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
   1835 {
   1836 #if NWSKBD > 0
   1837 	int s, raw, data, error;
   1838 	struct wsevsrc *inp;
   1839 
   1840 	s = spltty();
   1841 
   1842 	raw = (scr ? scr->scr_rawkbd : 0);
   1843 
   1844 	if (scr != sc->sc_focus ||
   1845 	    sc->sc_rawkbd == raw) {
   1846 		splx(s);
   1847 		return (0);
   1848 	}
   1849 
   1850 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1851 	inp = sc->sc_input;
   1852 	if (inp == NULL) {
   1853 		splx(s);
   1854 		return (ENXIO);
   1855 	}
   1856 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1857 	if (!error)
   1858 		sc->sc_rawkbd = raw;
   1859 	splx(s);
   1860 	return (error);
   1861 #else
   1862 	return (0);
   1863 #endif
   1864 }
   1865 #endif
   1866 
   1867 static void
   1868 wsdisplay_switch3_cb(void *arg, int error, int waitok)
   1869 {
   1870 	device_t dv = arg;
   1871 
   1872 	wsdisplay_switch3(dv, error, waitok);
   1873 }
   1874 
   1875 static int
   1876 wsdisplay_switch3(device_t dv, int error, int waitok)
   1877 {
   1878 	struct wsdisplay_softc *sc = device_private(dv);
   1879 	int no;
   1880 	struct wsscreen *scr;
   1881 
   1882 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1883 		aprint_error_dev(dv, "wsdisplay_switch3: not switching\n");
   1884 		return (EINVAL);
   1885 	}
   1886 
   1887 	no = sc->sc_screenwanted;
   1888 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1889 		panic("wsdisplay_switch3: invalid screen %d", no);
   1890 	scr = sc->sc_scr[no];
   1891 	if (!scr) {
   1892 		aprint_error_dev(dv,
   1893 		    "wsdisplay_switch3: screen %d disappeared\n", no);
   1894 		error = ENXIO;
   1895 	}
   1896 
   1897 	if (error) {
   1898 		/* try to recover, avoid recursion */
   1899 
   1900 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1901 			aprint_error_dev(dv, "wsdisplay_switch3: giving up\n");
   1902 			sc->sc_focus = 0;
   1903 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1904 			wsdisplay_update_rawkbd(sc, 0);
   1905 #endif
   1906 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1907 			return (error);
   1908 		}
   1909 
   1910 		sc->sc_screenwanted = sc->sc_oldscreen;
   1911 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1912 		return (wsdisplay_switch1(dv, 0, waitok));
   1913 	}
   1914 
   1915 	if (scr->scr_syncops && !error)
   1916 		sc->sc_flags |= SC_XATTACHED;
   1917 
   1918 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1919 
   1920 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1921 		wakeup(scr);
   1922 	return (error);
   1923 }
   1924 
   1925 static void
   1926 wsdisplay_switch2_cb(void *arg, int error, int waitok)
   1927 {
   1928 	device_t dv = arg;
   1929 
   1930 	wsdisplay_switch2(dv, error, waitok);
   1931 }
   1932 
   1933 static int
   1934 wsdisplay_switch2(device_t dv, int error, int waitok)
   1935 {
   1936 	struct wsdisplay_softc *sc = device_private(dv);
   1937 	int no;
   1938 	struct wsscreen *scr;
   1939 
   1940 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1941 		aprint_error_dev(dv, "wsdisplay_switch2: not switching\n");
   1942 		return (EINVAL);
   1943 	}
   1944 
   1945 	no = sc->sc_screenwanted;
   1946 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1947 		panic("wsdisplay_switch2: invalid screen %d", no);
   1948 	scr = sc->sc_scr[no];
   1949 	if (!scr) {
   1950 		aprint_error_dev(dv,
   1951 		    "wsdisplay_switch2: screen %d disappeared\n", no);
   1952 		error = ENXIO;
   1953 	}
   1954 
   1955 	if (error) {
   1956 		/* try to recover, avoid recursion */
   1957 
   1958 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1959 			aprint_error_dev(dv, "wsdisplay_switch2: giving up\n");
   1960 			sc->sc_focus = 0;
   1961 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1962 			return (error);
   1963 		}
   1964 
   1965 		sc->sc_screenwanted = sc->sc_oldscreen;
   1966 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1967 		return (wsdisplay_switch1(dv, 0, waitok));
   1968 	}
   1969 
   1970 	sc->sc_focusidx = no;
   1971 	sc->sc_focus = scr;
   1972 
   1973 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1974 	(void) wsdisplay_update_rawkbd(sc, scr);
   1975 #endif
   1976 	/* keyboard map??? */
   1977 
   1978 	if (scr->scr_syncops &&
   1979 	    !(sc->sc_isconsole && wsdisplay_cons_pollmode)) {
   1980 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1981 						    wsdisplay_switch3_cb, dv);
   1982 		if (error == EAGAIN) {
   1983 			/* switch will be done asynchronously */
   1984 			return (0);
   1985 		}
   1986 	}
   1987 
   1988 	return (wsdisplay_switch3(dv, error, waitok));
   1989 }
   1990 
   1991 static void
   1992 wsdisplay_switch1_cb(void *arg, int error, int waitok)
   1993 {
   1994 	device_t dv = arg;
   1995 
   1996 	wsdisplay_switch1(dv, error, waitok);
   1997 }
   1998 
   1999 static int
   2000 wsdisplay_switch1(device_t dv, int error, int waitok)
   2001 {
   2002 	struct wsdisplay_softc *sc = device_private(dv);
   2003 	int no;
   2004 	struct wsscreen *scr;
   2005 
   2006 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   2007 		aprint_error_dev(dv, "wsdisplay_switch1: not switching\n");
   2008 		return (EINVAL);
   2009 	}
   2010 
   2011 	no = sc->sc_screenwanted;
   2012 	if (no == WSDISPLAY_NULLSCREEN) {
   2013 		sc->sc_flags &= ~SC_SWITCHPENDING;
   2014 		if (!error) {
   2015 			sc->sc_flags &= ~SC_XATTACHED;
   2016 			sc->sc_focus = 0;
   2017 		}
   2018 		wakeup(sc);
   2019 		return (error);
   2020 	}
   2021 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   2022 		panic("wsdisplay_switch1: invalid screen %d", no);
   2023 	scr = sc->sc_scr[no];
   2024 	if (!scr) {
   2025 		aprint_error_dev(dv,
   2026 		    "wsdisplay_switch1: screen %d disappeared\n", no);
   2027 		error = ENXIO;
   2028 	}
   2029 
   2030 	if (error) {
   2031 		sc->sc_flags &= ~SC_SWITCHPENDING;
   2032 		return (error);
   2033 	}
   2034 
   2035 	sc->sc_flags &= ~SC_XATTACHED;
   2036 
   2037 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   2038 						 scr->scr_dconf->emulcookie,
   2039 						 waitok,
   2040 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch2_cb, dv);
   2041 	if (error == EAGAIN) {
   2042 		/* switch will be done asynchronously */
   2043 		return (0);
   2044 	}
   2045 
   2046 	return (wsdisplay_switch2(dv, error, waitok));
   2047 }
   2048 
   2049 int
   2050 wsdisplay_switch(device_t dv, int no, int waitok)
   2051 {
   2052 	struct wsdisplay_softc *sc = device_private(dv);
   2053 	int s, res = 0;
   2054 	struct wsscreen *scr;
   2055 
   2056 	if (no != WSDISPLAY_NULLSCREEN) {
   2057 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
   2058 			return (EINVAL);
   2059 		if (sc->sc_scr[no] == NULL)
   2060 			return (ENXIO);
   2061 	}
   2062 
   2063 	wsdisplay_stat_inject(dv, WSCONS_EVENT_SCREEN_SWITCH, no);
   2064 
   2065 	s = spltty();
   2066 
   2067 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   2068 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   2069 		splx(s);
   2070 		return (0);
   2071 	}
   2072 
   2073 	if (sc->sc_flags & SC_SWITCHPENDING) {
   2074 		splx(s);
   2075 		return (EBUSY);
   2076 	}
   2077 
   2078 	sc->sc_flags |= SC_SWITCHPENDING;
   2079 	sc->sc_screenwanted = no;
   2080 
   2081 	splx(s);
   2082 
   2083 	scr = sc->sc_focus;
   2084 	if (!scr) {
   2085 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   2086 		return (wsdisplay_switch1(dv, 0, waitok));
   2087 	} else
   2088 		sc->sc_oldscreen = sc->sc_focusidx;
   2089 
   2090 	if (scr->scr_syncops) {
   2091 		if (!(sc->sc_flags & SC_XATTACHED) ||
   2092 		    (sc->sc_isconsole && wsdisplay_cons_pollmode)) {
   2093 			/* nothing to do here */
   2094 			return (wsdisplay_switch1(dv, 0, waitok));
   2095 		}
   2096 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   2097 						  wsdisplay_switch1_cb, dv);
   2098 		if (res == EAGAIN) {
   2099 			/* switch will be done asynchronously */
   2100 			return (0);
   2101 		}
   2102 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   2103 		/* no way to save state */
   2104 		res = EBUSY;
   2105 	}
   2106 
   2107 	return (wsdisplay_switch1(dv, res, waitok));
   2108 }
   2109 
   2110 void
   2111 wsdisplay_reset(device_t dv, enum wsdisplay_resetops op)
   2112 {
   2113 	struct wsdisplay_softc *sc = device_private(dv);
   2114 	struct wsscreen *scr;
   2115 
   2116 	KASSERT(sc != NULL);
   2117 	scr = sc->sc_focus;
   2118 
   2119 	if (!scr)
   2120 		return;
   2121 
   2122 	switch (op) {
   2123 	case WSDISPLAY_RESETEMUL:
   2124 		if (!WSSCREEN_HAS_EMULATOR(scr))
   2125 			break;
   2126 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   2127 						 WSEMUL_RESET);
   2128 		break;
   2129 	case WSDISPLAY_RESETCLOSE:
   2130 		wsdisplay_closescreen(sc, scr);
   2131 		break;
   2132 	}
   2133 }
   2134 
   2135 
   2136 bool
   2137 wsdisplay_isconsole(struct wsdisplay_softc *sc)
   2138 {
   2139 	return sc->sc_isconsole;
   2140 }
   2141 
   2142 /*
   2143  * Interface for (external) VT switch / process synchronization code
   2144  */
   2145 int
   2146 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   2147 	void *cookie)
   2148 {
   2149 	if (scr->scr_syncops) {
   2150 		/*
   2151 		 * The screen is already claimed.
   2152 		 * Check if the owner is still alive.
   2153 		 */
   2154 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   2155 			return (EBUSY);
   2156 	}
   2157 	scr->scr_syncops = ops;
   2158 	scr->scr_synccookie = cookie;
   2159 	if (scr == scr->sc->sc_focus)
   2160 		scr->sc->sc_flags |= SC_XATTACHED;
   2161 	return (0);
   2162 }
   2163 
   2164 int
   2165 wsscreen_detach_sync(struct wsscreen *scr)
   2166 {
   2167 	if (!scr->scr_syncops)
   2168 		return (EINVAL);
   2169 	scr->scr_syncops = 0;
   2170 	if (scr == scr->sc->sc_focus)
   2171 		scr->sc->sc_flags &= ~SC_XATTACHED;
   2172 	return (0);
   2173 }
   2174 
   2175 int
   2176 wsscreen_lookup_sync(struct wsscreen *scr,
   2177 	const struct wscons_syncops *ops, /* used as ID */
   2178 	void **cookiep)
   2179 {
   2180 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   2181 		return (EINVAL);
   2182 	*cookiep = scr->scr_synccookie;
   2183 	return (0);
   2184 }
   2185 
   2186 /*
   2187  * Interface to virtual screen stuff
   2188  */
   2189 int
   2190 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   2191 {
   2192 	return (WSDISPLAY_MAXSCREEN - 1);
   2193 }
   2194 
   2195 int
   2196 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   2197 {
   2198 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   2199 		return (EINVAL);
   2200 	if (!sc->sc_scr[idx])
   2201 		return (ENXIO);
   2202 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   2203 }
   2204 
   2205 int
   2206 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   2207 {
   2208 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   2209 }
   2210 
   2211 int
   2212 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   2213 {
   2214 	struct wsscreen *scr;
   2215 	int s, res = 0;
   2216 
   2217 	if (no == WSDISPLAY_NULLSCREEN) {
   2218 		s = spltty();
   2219 		while (sc->sc_focus && res == 0) {
   2220 			res = tsleep(sc, PCATCH, "wswait", 0);
   2221 		}
   2222 		splx(s);
   2223 		return (res);
   2224 	}
   2225 
   2226 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   2227 		return (ENXIO);
   2228 	scr = sc->sc_scr[no];
   2229 	if (!scr)
   2230 		return (ENXIO);
   2231 
   2232 	s = spltty();
   2233 	if (scr != sc->sc_focus) {
   2234 		scr->scr_flags |= SCR_WAITACTIVE;
   2235 		res = tsleep(scr, PCATCH, "wswait", 0);
   2236 		if (scr != sc->sc_scr[no])
   2237 			res = ENXIO; /* disappeared in the meantime */
   2238 		else
   2239 			scr->scr_flags &= ~SCR_WAITACTIVE;
   2240 	}
   2241 	splx(s);
   2242 	return (res);
   2243 }
   2244 
   2245 void
   2246 wsdisplay_kbdholdscreen(device_t dv, int hold)
   2247 {
   2248 	struct wsdisplay_softc *sc = device_private(dv);
   2249 	struct wsscreen *scr;
   2250 
   2251 	scr = sc->sc_focus;
   2252 
   2253 	if (!scr)
   2254 		return;
   2255 
   2256 	if (hold)
   2257 		scr->scr_hold_screen = 1;
   2258 	else {
   2259 		scr->scr_hold_screen = 0;
   2260 		callout_schedule(&scr->scr_tty->t_rstrt_ch, 0);
   2261 	}
   2262 }
   2263 
   2264 #if NWSKBD > 0
   2265 void
   2266 wsdisplay_set_console_kbd(struct wsevsrc *src)
   2267 {
   2268 	if (wsdisplay_console_device == NULL) {
   2269 		src->me_dispdv = NULL;
   2270 		return;
   2271 	}
   2272 #if NWSMUX > 0
   2273 	if (wsmux_attach_sc((struct wsmux_softc *)
   2274 			    wsdisplay_console_device->sc_input, src)) {
   2275 		src->me_dispdv = NULL;
   2276 		return;
   2277 	}
   2278 #else
   2279 	wsdisplay_console_device->sc_input = src;
   2280 #endif
   2281 	src->me_dispdv = wsdisplay_console_device->sc_dev;
   2282 }
   2283 #endif /* NWSKBD > 0 */
   2284 
   2285 /*
   2286  * Console interface.
   2287  */
   2288 void
   2289 wsdisplay_cnputc(dev_t dev, int i)
   2290 {
   2291 	struct wsscreen_internal *dc;
   2292 	u_char c = i;
   2293 
   2294 	if (!wsdisplay_console_initted)
   2295 		return;
   2296 
   2297 	if ((wsdisplay_console_device != NULL) &&
   2298 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   2299 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   2300 		return;
   2301 
   2302 	dc = &wsdisplay_console_conf;
   2303 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   2304 
   2305 #ifdef WSDISPLAY_MULTICONS
   2306 	if (wsdisplay_multicons_enable && wsdisplay_ocn && wsdisplay_ocn->cn_putc)
   2307 		wsdisplay_ocn->cn_putc(wsdisplay_ocn->cn_dev, i);
   2308 #endif
   2309 }
   2310 
   2311 static int
   2312 wsdisplay_getc(dev_t dev)
   2313 {
   2314 	int c;
   2315 
   2316 	if (wsdisplay_cons_kbd_getc) {
   2317 		c = wsdisplay_cons_kbd_getc(wsdisplay_cons.cn_dev);
   2318 		if (c >= 0)
   2319 			return c;
   2320 	}
   2321 
   2322 #ifdef WSDISPLAY_MULTICONS
   2323 	if (wsdisplay_multicons_enable && wsdisplay_ocn && wsdisplay_ocn->cn_getc) {
   2324 		c = wsdisplay_ocn->cn_getc(wsdisplay_ocn->cn_dev);
   2325 		if (c >= 0)
   2326 			return c;
   2327 	}
   2328 #endif
   2329 	return -1;
   2330 }
   2331 
   2332 static void
   2333 wsdisplay_pollc(dev_t dev, int on)
   2334 {
   2335 
   2336 	wsdisplay_cons_pollmode = on;
   2337 
   2338 	/* notify to fb drivers */
   2339 	if (wsdisplay_console_device != NULL &&
   2340 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   2341 		(*wsdisplay_console_device->sc_accessops->pollc)
   2342 			(wsdisplay_console_device->sc_accesscookie, on);
   2343 
   2344 	/* notify to kbd drivers */
   2345 	if (wsdisplay_cons_kbd_pollc)
   2346 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   2347 
   2348 #ifdef WSDISPLAY_MULTICONS
   2349 	/* notify to old console driver */
   2350 	if (wsdisplay_multicons_enable && wsdisplay_ocn && wsdisplay_ocn->cn_pollc)
   2351 		wsdisplay_ocn->cn_pollc(wsdisplay_ocn->cn_dev, on);
   2352 #endif
   2353 }
   2354 
   2355 void
   2356 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   2357 	void (*bell)(dev_t, u_int, u_int, u_int))
   2358 {
   2359 	wsdisplay_cons.cn_bell = bell;
   2360 	wsdisplay_cons_kbd_getc = get;
   2361 	wsdisplay_cons_kbd_pollc = poll;
   2362 }
   2363 
   2364 void
   2365 wsdisplay_unset_cons_kbd(void)
   2366 {
   2367 	wsdisplay_cons.cn_bell = NULL;
   2368 	wsdisplay_cons_kbd_getc = NULL;
   2369 	wsdisplay_cons_kbd_pollc = NULL;
   2370 }
   2371 
   2372 #ifdef WSDISPLAY_MULTICONS
   2373 SYSCTL_SETUP(sysctl_hw_wsdisplay_setup, "sysctl hw.wsdisplay subtree setup")
   2374 {
   2375 	const struct sysctlnode *wsdisplay_node;
   2376 
   2377 	if (sysctl_createv(clog, 0, NULL, &wsdisplay_node,
   2378 	    CTLFLAG_PERMANENT,
   2379 	    CTLTYPE_NODE, "wsdisplay", NULL,
   2380 	    NULL, 0, NULL, 0,
   2381 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   2382 		return;
   2383 
   2384 	sysctl_createv(clog, 0, NULL, NULL,
   2385 	    CTLFLAG_READWRITE,
   2386 	    CTLTYPE_BOOL, "multicons",
   2387 	    SYSCTL_DESCR("Enable wsdisplay multicons"),
   2388 	    NULL, 0, &wsdisplay_multicons_enable, 0,
   2389 	    CTL_HW, wsdisplay_node->sysctl_num, CTL_CREATE, CTL_EOL);
   2390 }
   2391 #endif
   2392