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