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