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