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