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