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