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