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