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