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