Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.117.6.3
      1 /* $NetBSD: wsdisplay.c,v 1.117.6.3 2008/04/06 09:58:51 mjf Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: wsdisplay.c,v 1.117.6.3 2008/04/06 09:58:51 mjf Exp $");
     35 
     36 #include "opt_wsdisplay_compat.h"
     37 #include "opt_wsmsgattrs.h"
     38 #include "opt_compat_netbsd.h"
     39 #include "wskbd.h"
     40 #include "wsmux.h"
     41 #include "wsdisplay.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/conf.h>
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/poll.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/malloc.h>
     51 #include <sys/syslog.h>
     52 #include <sys/systm.h>
     53 #include <sys/tty.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/errno.h>
     56 #include <sys/fcntl.h>
     57 #include <sys/vnode.h>
     58 #include <sys/kauth.h>
     59 
     60 #include <dev/wscons/wseventvar.h>
     61 #include <dev/wscons/wsmuxvar.h>
     62 #include <dev/wscons/wsconsio.h>
     63 #include <dev/wscons/wsdisplayvar.h>
     64 #include <dev/wscons/wsksymvar.h>
     65 #include <dev/wscons/wsksymdef.h>
     66 #include <dev/wscons/wsemulvar.h>
     67 #include <dev/wscons/wscons_callbacks.h>
     68 #include <dev/cons.h>
     69 
     70 struct wsscreen_internal {
     71 	const struct wsdisplay_emulops *emulops;
     72 	void	*emulcookie;
     73 
     74 	const struct wsscreen_descr *scrdata;
     75 
     76 	const struct wsemul_ops *wsemul;
     77 	void	*wsemulcookie;
     78 };
     79 
     80 struct wsscreen {
     81 	struct wsscreen_internal *scr_dconf;
     82 
     83 	struct tty *scr_tty;
     84 	int	scr_hold_screen;		/* hold tty output */
     85 
     86 	int scr_flags;
     87 #define SCR_OPEN 1		/* is it open? */
     88 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     89 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     90 #define	SCR_DUMBFB 8		/* in use as a dumb fb (iff SCR_GRAPHICS) */
     91 	const struct wscons_syncops *scr_syncops;
     92 	void *scr_synccookie;
     93 
     94 #ifdef WSDISPLAY_COMPAT_RAWKBD
     95 	int scr_rawkbd;
     96 #endif
     97 
     98 	struct wsdisplay_softc *sc;
     99 };
    100 
    101 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int,
    102 				 const char *,
    103 				 const struct wsscreen_descr *, void *,
    104 				 int, int, long);
    105 void wsscreen_detach(struct wsscreen *);
    106 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
    107 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
    108 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
    109 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
    110 
    111 #define WSDISPLAY_MAXSCREEN 8
    112 
    113 struct wsdisplay_softc {
    114 	device_t sc_dev;
    115 
    116 	const struct wsdisplay_accessops *sc_accessops;
    117 	void	*sc_accesscookie;
    118 
    119 	const struct wsscreen_list *sc_scrdata;
    120 #ifdef WSDISPLAY_SCROLLSUPPORT
    121 	struct wsdisplay_scroll_data sc_scroll_values;
    122 #endif
    123 
    124 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    125 	int sc_focusidx;	/* available only if sc_focus isn't null */
    126 	struct wsscreen *sc_focus;
    127 
    128 	struct wseventvar evar;
    129 
    130 	int	sc_isconsole;
    131 
    132 	int sc_flags;
    133 #define SC_SWITCHPENDING 1
    134 #define SC_SWITCHERROR 2
    135 #define SC_XATTACHED 4 /* X server active */
    136 	kmutex_t sc_flagsmtx; /* for flags, might also be used for focus */
    137 	kcondvar_t sc_flagscv;
    138 
    139 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
    140 
    141 #if NWSKBD > 0
    142 	struct wsevsrc *sc_input;
    143 #ifdef WSDISPLAY_COMPAT_RAWKBD
    144 	int sc_rawkbd;
    145 #endif
    146 #endif /* NWSKBD > 0 */
    147 };
    148 
    149 #ifdef WSDISPLAY_SCROLLSUPPORT
    150 
    151 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
    152 	WSDISPLAY_SCROLL_DOALL,
    153 	25,
    154 	2,
    155 };
    156 #endif
    157 
    158 extern struct cfdriver wsdisplay_cd;
    159 
    160 /* Autoconfiguration definitions. */
    161 static int wsdisplay_emul_match(device_t , struct cfdata *, void *);
    162 static void wsdisplay_emul_attach(device_t, device_t, void *);
    163 static int wsdisplay_noemul_match(device_t, struct cfdata *, void *);
    164 static void wsdisplay_noemul_attach(device_t, device_t, void *);
    165 static bool wsdisplay_suspend(device_t PMF_FN_PROTO);
    166 
    167 CFATTACH_DECL_NEW(wsdisplay_emul, sizeof (struct wsdisplay_softc),
    168     wsdisplay_emul_match, wsdisplay_emul_attach, NULL, NULL);
    169 
    170 CFATTACH_DECL_NEW(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
    171     wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
    172 
    173 dev_type_open(wsdisplayopen);
    174 dev_type_close(wsdisplayclose);
    175 dev_type_read(wsdisplayread);
    176 dev_type_write(wsdisplaywrite);
    177 dev_type_ioctl(wsdisplayioctl);
    178 dev_type_stop(wsdisplaystop);
    179 dev_type_tty(wsdisplaytty);
    180 dev_type_poll(wsdisplaypoll);
    181 dev_type_mmap(wsdisplaymmap);
    182 dev_type_kqfilter(wsdisplaykqfilter);
    183 
    184 const struct cdevsw wsdisplay_cdevsw = {
    185 	wsdisplayopen, wsdisplayclose, wsdisplayread, wsdisplaywrite,
    186 	wsdisplayioctl, wsdisplaystop, wsdisplaytty, wsdisplaypoll,
    187 	wsdisplaymmap, wsdisplaykqfilter, D_TTY
    188 };
    189 
    190 static void wsdisplaystart(struct tty *);
    191 static int wsdisplayparam(struct tty *, struct termios *);
    192 
    193 
    194 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    195 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    196 #define ISWSDISPLAYSTAT(dev)	(WSDISPLAYSCREEN(dev) == 254)
    197 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    198 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    199 
    200 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    201 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    202 
    203 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
    204 	    int console, int kbdmux, const struct wsscreen_list *,
    205 	    const struct wsdisplay_accessops *accessops,
    206 	    void *accesscookie);
    207 
    208 #ifdef WSDISPLAY_COMPAT_RAWKBD
    209 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
    210 				 struct wsscreen *);
    211 #endif
    212 
    213 static int wsdisplay_console_initted;
    214 static int wsdisplay_console_attached;
    215 static struct wsdisplay_softc *wsdisplay_console_device;
    216 static struct wsscreen_internal wsdisplay_console_conf;
    217 
    218 static int wsdisplay_getc_dummy(dev_t);
    219 static void wsdisplay_pollc(dev_t, int);
    220 
    221 static int wsdisplay_cons_pollmode;
    222 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
    223 
    224 static struct consdev wsdisplay_cons = {
    225 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
    226 	wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
    227 };
    228 
    229 #ifndef WSDISPLAY_DEFAULTSCREENS
    230 # define WSDISPLAY_DEFAULTSCREENS	0
    231 #endif
    232 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
    233 
    234 static int wsdisplay_switch1(device_t, int, int);
    235 static void wsdisplay_switch1_cb(void *, int, int);
    236 static int wsdisplay_switch2(device_t, int, int);
    237 static void wsdisplay_switch2_cb(void *, int, int);
    238 static int wsdisplay_switch3(device_t, int, int);
    239 static void wsdisplay_switch3_cb(void *, int, int);
    240 
    241 int wsdisplay_clearonclose;
    242 
    243 struct wsscreen *
    244 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
    245 	const struct wsscreen_descr *type, void *cookie, int ccol,
    246 	int crow, long defattr)
    247 {
    248 	struct wsscreen_internal *dconf;
    249 	struct wsscreen *scr;
    250 
    251 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    252 	if (!scr)
    253 		return (NULL);
    254 
    255 	if (console) {
    256 		dconf = &wsdisplay_console_conf;
    257 		/*
    258 		 * If there's an emulation, tell it about the callback argument.
    259 		 * The other stuff is already there.
    260 		 */
    261 		if (dconf->wsemul != NULL)
    262 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    263 	} else { /* not console */
    264 		dconf = malloc(sizeof(struct wsscreen_internal),
    265 			       M_DEVBUF, M_NOWAIT);
    266 		dconf->emulops = type->textops;
    267 		dconf->emulcookie = cookie;
    268 		if (dconf->emulops) {
    269 			dconf->wsemul = wsemul_pick(emul);
    270 			if (dconf->wsemul == NULL) {
    271 				free(dconf, M_DEVBUF);
    272 				free(scr, M_DEVBUF);
    273 				return (NULL);
    274 			}
    275 			dconf->wsemulcookie =
    276 			  (*dconf->wsemul->attach)(0, type, cookie,
    277 						   ccol, crow, scr, defattr);
    278 		} else
    279 			dconf->wsemul = NULL;
    280 		dconf->scrdata = type;
    281 	}
    282 
    283 	scr->scr_dconf = dconf;
    284 
    285 	scr->scr_tty = ttymalloc();
    286 	tty_attach(scr->scr_tty);
    287 	scr->scr_hold_screen = 0;
    288 	if (WSSCREEN_HAS_EMULATOR(scr))
    289 		scr->scr_flags = 0;
    290 	else
    291 		scr->scr_flags = SCR_GRAPHICS;
    292 
    293 	scr->scr_syncops = 0;
    294 	scr->sc = sc;
    295 #ifdef WSDISPLAY_COMPAT_RAWKBD
    296 	scr->scr_rawkbd = 0;
    297 #endif
    298 	return (scr);
    299 }
    300 
    301 void
    302 wsscreen_detach(struct wsscreen *scr)
    303 {
    304 	u_int ccol, crow; /* XXX */
    305 
    306 	if (WSSCREEN_HAS_TTY(scr)) {
    307 		tty_detach(scr->scr_tty);
    308 		ttyfree(scr->scr_tty);
    309 	}
    310 	if (WSSCREEN_HAS_EMULATOR(scr))
    311 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    312 						  &ccol, &crow);
    313 	free(scr->scr_dconf, M_DEVBUF);
    314 	free(scr, M_DEVBUF);
    315 }
    316 
    317 const struct wsscreen_descr *
    318 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
    319 {
    320 	int i;
    321 	const struct wsscreen_descr *scr;
    322 
    323 	KASSERT(scrdata->nscreens > 0);
    324 
    325 	if (name == NULL)
    326 		return (scrdata->screens[0]);
    327 
    328 	for (i = 0; i < scrdata->nscreens; i++) {
    329 		scr = scrdata->screens[i];
    330 		if (!strcmp(name, scr->name))
    331 			return (scr);
    332 	}
    333 
    334 	return (0);
    335 }
    336 
    337 /*
    338  * print info about attached screen
    339  */
    340 static void
    341 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
    342 {
    343 	aprint_verbose_dev(sc->sc_dev, "screen %d", idx);
    344 	if (count > 1)
    345 		aprint_verbose("-%d", idx + (count-1));
    346 	aprint_verbose(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
    347 	if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
    348 		aprint_verbose(", %s emulation",
    349 			sc->sc_scr[idx]->scr_dconf->wsemul->name);
    350 	}
    351 	aprint_verbose(")\n");
    352 }
    353 
    354 int
    355 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
    356 	const char *screentype, const char *emul)
    357 {
    358 	const struct wsscreen_descr *scrdesc;
    359 	int error;
    360 	void *cookie;
    361 	int ccol, crow;
    362 	long defattr;
    363 	struct wsscreen *scr;
    364 	int s;
    365 	int unit;
    366 	int maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    367 
    368 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    369 		return (EINVAL);
    370 	if (sc->sc_scr[idx] != NULL)
    371 		return (EBUSY);
    372 
    373 	unit = device_unit(sc->sc_dev);
    374 	device_register_name(makedev(maj, WSDISPLAYMINOR(unit, idx - 1)),
    375 	    sc->sc_dev, true, DEV_TTY, "ttyE%d", idx - 1);
    376 
    377 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    378 	if (!scrdesc)
    379 		return (ENXIO);
    380 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    381 			scrdesc, &cookie, &ccol, &crow, &defattr);
    382 	if (error)
    383 		return (error);
    384 
    385 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    386 			      cookie, ccol, crow, defattr);
    387 	if (scr == NULL) {
    388 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    389 						 cookie);
    390 		return (ENXIO);
    391 	}
    392 
    393 	sc->sc_scr[idx] = scr;
    394 
    395 	/* if no screen has focus yet, activate the first we get */
    396 	s = spltty();
    397 	if (!sc->sc_focus) {
    398 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
    399 						 scr->scr_dconf->emulcookie,
    400 						 0, 0, 0);
    401 		sc->sc_focusidx = idx;
    402 		sc->sc_focus = scr;
    403 	}
    404 	splx(s);
    405 	return (0);
    406 }
    407 
    408 static void
    409 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
    410 {
    411 	int maj, mn, idx;
    412 
    413 	/* hangup */
    414 	if (WSSCREEN_HAS_TTY(scr)) {
    415 		struct tty *tp = scr->scr_tty;
    416 		(*tp->t_linesw->l_modem)(tp, 0);
    417 	}
    418 
    419 	/* locate the major number */
    420 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    421 	/* locate the screen index */
    422 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
    423 		if (scr == sc->sc_scr[idx])
    424 			break;
    425 #ifdef DIAGNOSTIC
    426 	if (idx == WSDISPLAY_MAXSCREEN)
    427 		panic("wsdisplay_forceclose: bad screen");
    428 #endif
    429 
    430 	/* nuke the vnodes */
    431 	mn = WSDISPLAYMINOR(device_unit(sc->sc_dev), idx);
    432 	vdevgone(maj, mn, mn, VCHR);
    433 }
    434 
    435 #ifdef WSDISPLAY_SCROLLSUPPORT
    436 void
    437 wsdisplay_scroll(void *arg, int op)
    438 {
    439 	device_t dv = arg;
    440 	struct wsdisplay_softc *sc = device_private(dv);
    441 	struct wsscreen *scr;
    442 	int lines;
    443 
    444 	scr = sc->sc_focus;
    445 
    446 	if (!scr)
    447 		return;
    448 
    449 	if (op == WSDISPLAY_SCROLL_RESET)
    450 		lines = 0;
    451 	else {
    452 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
    453 			sc->sc_scroll_values.slowlines :
    454 			sc->sc_scroll_values.fastlines;
    455 		if (op & WSDISPLAY_SCROLL_BACKWARD)
    456 			lines = -(lines);
    457 	}
    458 
    459 	if (sc->sc_accessops->scroll) {
    460 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
    461 		    sc->sc_focus->scr_dconf->emulcookie, lines);
    462 	}
    463 }
    464 #endif
    465 
    466 int
    467 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
    468 {
    469 	struct wsscreen *scr;
    470 	int s;
    471 	void *cookie;
    472 
    473 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    474 		return (EINVAL);
    475 	if ((scr = sc->sc_scr[idx]) == NULL)
    476 		return (ENXIO);
    477 
    478 	device_deregister_all(sc->sc_dev);
    479 
    480 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    481 	    scr->scr_syncops ||
    482 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    483 		return(EBUSY);
    484 
    485 	wsdisplay_closescreen(sc, scr);
    486 
    487 	/*
    488 	 * delete pointers, so neither device entries
    489 	 * nor keyboard input can reference it anymore
    490 	 */
    491 	s = spltty();
    492 	if (sc->sc_focus == scr) {
    493 		sc->sc_focus = 0;
    494 #ifdef WSDISPLAY_COMPAT_RAWKBD
    495 		wsdisplay_update_rawkbd(sc, 0);
    496 #endif
    497 	}
    498 	sc->sc_scr[idx] = 0;
    499 	splx(s);
    500 
    501 	/*
    502 	 * Wake up processes waiting for the screen to
    503 	 * be activated. Sleepers must check whether
    504 	 * the screen still exists.
    505 	 */
    506 	if (scr->scr_flags & SCR_WAITACTIVE)
    507 		wakeup(scr);
    508 
    509 	/* save a reference to the graphics screen */
    510 	cookie = scr->scr_dconf->emulcookie;
    511 
    512 	wsscreen_detach(scr);
    513 
    514 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    515 					 cookie);
    516 
    517 	aprint_verbose_dev(sc->sc_dev, "screen %d deleted\n", idx);
    518 	return (0);
    519 }
    520 
    521 /*
    522  * Autoconfiguration functions.
    523  */
    524 int
    525 wsdisplay_emul_match(device_t parent, struct cfdata *match, void *aux)
    526 {
    527 	struct wsemuldisplaydev_attach_args *ap = aux;
    528 
    529 	if (match->wsemuldisplaydevcf_console !=
    530 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    531 		/*
    532 		 * If console-ness of device specified, either match
    533 		 * exactly (at high priority), or fail.
    534 		 */
    535 		if (match->wsemuldisplaydevcf_console != 0 &&
    536 		    ap->console != 0)
    537 			return (10);
    538 		else
    539 			return (0);
    540 	}
    541 
    542 	/* If console-ness unspecified, it wins. */
    543 	return (1);
    544 }
    545 
    546 void
    547 wsdisplay_emul_attach(device_t parent, device_t self, void *aux)
    548 {
    549 	struct wsdisplay_softc *sc = device_private(self);
    550 	struct wsemuldisplaydev_attach_args *ap = aux;
    551 
    552 	/* locate the major number */
    553 	int maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    554 
    555 	sc->sc_dev = self;
    556 
    557 	/* Don't allow more than one console to attach */
    558 	if (wsdisplay_console_attached && ap->console)
    559 		ap->console = 0;
    560 
    561 	wsdisplay_common_attach(sc, ap->console,
    562 	     device_cfdata(self)->wsemuldisplaydevcf_kbdmux, ap->scrdata,
    563 	     ap->accessops, ap->accesscookie);
    564 
    565 	if (ap->console) {
    566 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
    567 					 0));
    568 	}
    569 
    570 	/* Setup the control and stat device nodes */
    571 	device_register_name(makedev(maj, 254), self, true, DEV_TTY, "ttyEstat");
    572 	device_register_name(makedev(maj, 255), self, true, DEV_TTY, "ttyEcfg");
    573 }
    574 
    575 /* Print function (for parent devices). */
    576 int
    577 wsemuldisplaydevprint(void *aux, const char *pnp)
    578 {
    579 #if 0 /* -Wunused */
    580 	struct wsemuldisplaydev_attach_args *ap = aux;
    581 #endif
    582 
    583 	if (pnp)
    584 		aprint_normal("wsdisplay at %s", pnp);
    585 #if 0 /* don't bother; it's ugly */
    586 	aprint_normal(" console %d", ap->console);
    587 #endif
    588 
    589 	return (UNCONF);
    590 }
    591 
    592 int
    593 wsdisplay_noemul_match(device_t parent, struct cfdata *match, void *aux)
    594 {
    595 #if 0 /* -Wunused */
    596 	struct wsdisplaydev_attach_args *ap = aux;
    597 #endif
    598 
    599 	/* Always match. */
    600 	return (1);
    601 }
    602 
    603 void
    604 wsdisplay_noemul_attach(device_t parent, device_t self, void *aux)
    605 {
    606 	struct wsdisplay_softc *sc = device_private(self);
    607 	struct wsdisplaydev_attach_args *ap = aux;
    608 
    609 	sc->sc_dev = self;
    610 
    611 	wsdisplay_common_attach(sc, 0,
    612 	    device_cfdata(self)->wsemuldisplaydevcf_kbdmux, NULL,
    613 	    ap->accessops, ap->accesscookie);
    614 }
    615 
    616 static void
    617 wsdisplay_swdone_cb(void *arg, int error, int waitok)
    618 {
    619 	struct wsdisplay_softc *sc = arg;
    620 
    621 	mutex_enter(&sc->sc_flagsmtx);
    622 	KASSERT(sc->sc_flags & SC_SWITCHPENDING);
    623 	if (error)
    624 		sc->sc_flags |= SC_SWITCHERROR;
    625 	sc->sc_flags &= ~SC_SWITCHPENDING;
    626 	cv_signal(&sc->sc_flagscv);
    627 	mutex_exit(&sc->sc_flagsmtx);
    628 }
    629 
    630 static int
    631 wsdisplay_dosync(struct wsdisplay_softc *sc, int attach)
    632 {
    633 	struct wsscreen *scr;
    634 	int (*op)(void *, int, void (*)(void *, int, int), void *);
    635 	int res;
    636 
    637 	scr = sc->sc_focus;
    638 	if (!scr || !scr->scr_syncops)
    639 		return 0; /* XXX check SCR_GRAPHICS? */
    640 
    641 	sc->sc_flags |= SC_SWITCHPENDING;
    642 	sc->sc_flags &= ~SC_SWITCHERROR;
    643 	if (attach)
    644 		op = scr->scr_syncops->attach;
    645 	else
    646 		op = scr->scr_syncops->detach;
    647 	res = (*op)(scr->scr_synccookie, 1, wsdisplay_swdone_cb, sc);
    648 	if (res == EAGAIN) {
    649 		/* wait for callback */
    650 		mutex_enter(&sc->sc_flagsmtx);
    651 		while (sc->sc_flags & SC_SWITCHPENDING)
    652 			cv_wait_sig(&sc->sc_flagscv, &sc->sc_flagsmtx);
    653 		mutex_exit(&sc->sc_flagsmtx);
    654 		if (sc->sc_flags & SC_SWITCHERROR)
    655 			return (EIO); /* XXX pass real error */
    656 	} else {
    657 		sc->sc_flags &= ~SC_SWITCHPENDING;
    658 		if (res)
    659 			return (res);
    660 	}
    661 	if (attach)
    662 		sc->sc_flags |= SC_XATTACHED;
    663 	else
    664 		sc->sc_flags &= ~SC_XATTACHED;
    665 	return 0;
    666 }
    667 
    668 int
    669 wsdisplay_handlex(int resume)
    670 {
    671 	int i, res;
    672 	device_t dv;
    673 
    674 	for (i = 0; i < wsdisplay_cd.cd_ndevs; i++) {
    675 		dv = wsdisplay_cd.cd_devs[i];
    676 		if (!dv)
    677 			continue;
    678 		res = wsdisplay_dosync(device_private(dv), resume);
    679 		if (res)
    680 			return (res);
    681 	}
    682 	return (0);
    683 }
    684 
    685 static bool
    686 wsdisplay_suspend(device_t dv PMF_FN_ARGS)
    687 {
    688 	struct wsdisplay_softc *sc = device_private(dv);
    689 #ifdef DIAGNOSTIC
    690 	struct wsscreen *scr = sc->sc_focus;
    691 	if (sc->sc_flags & SC_XATTACHED) {
    692 		KASSERT(scr && scr->scr_syncops);
    693 	}
    694 #endif
    695 #if 1
    696 	/*
    697 	 * XXX X servers should have been detached earlier.
    698 	 * pmf currently ignores our return value and suspends the system
    699 	 * after device suspend failures. We try to avoid bigger damage
    700 	 * and try to detach the X server here. This is not safe because
    701 	 * other parts of the system which the X server deals with
    702 	 * might already be suspended.
    703 	 */
    704 	if (sc->sc_flags & SC_XATTACHED) {
    705 		printf("%s: emergency X server detach\n", device_xname(dv));
    706 		wsdisplay_dosync(sc, 0);
    707 	}
    708 #endif
    709 	return (!(sc->sc_flags & SC_XATTACHED));
    710 }
    711 
    712 /* Print function (for parent devices). */
    713 int
    714 wsdisplaydevprint(void *aux, const char *pnp)
    715 {
    716 #if 0 /* -Wunused */
    717 	struct wsdisplaydev_attach_args *ap = aux;
    718 #endif
    719 
    720 	if (pnp)
    721 		aprint_normal("wsdisplay at %s", pnp);
    722 
    723 	return (UNCONF);
    724 }
    725 
    726 static void
    727 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
    728 	const struct wsscreen_list *scrdata,
    729 	const struct wsdisplay_accessops *accessops,
    730 	void *accesscookie)
    731 {
    732 	int i, start=0;
    733 #if NWSKBD > 0
    734 	struct wsevsrc *kme;
    735 #if NWSMUX > 0
    736 	struct wsmux_softc *mux;
    737 
    738 	if (kbdmux >= 0)
    739 		mux = wsmux_getmux(kbdmux);
    740 	else
    741 		mux = wsmux_create("dmux", device_unit(sc->sc_dev));
    742 	/* XXX panic()ing isn't nice, but attach cannot fail */
    743 	if (mux == NULL)
    744 		panic("wsdisplay_common_attach: no memory");
    745 	sc->sc_input = &mux->sc_base;
    746 	mux->sc_base.me_dispdv = sc->sc_dev;
    747 	aprint_normal(" kbdmux %d", kbdmux);
    748 #else
    749 	if (kbdmux >= 0)
    750 		aprint_normal(" (kbdmux ignored)");
    751 #endif
    752 #endif
    753 
    754 	sc->sc_isconsole = console;
    755 
    756 	if (console) {
    757 		KASSERT(wsdisplay_console_initted);
    758 		KASSERT(wsdisplay_console_device == NULL);
    759 
    760 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    761 		wsdisplay_console_device = sc;
    762 
    763 		aprint_normal(": console (%s, %s emulation)",
    764 		       wsdisplay_console_conf.scrdata->name,
    765 		       wsdisplay_console_conf.wsemul->name);
    766 
    767 #if NWSKBD > 0
    768 		kme = wskbd_set_console_display(sc->sc_dev, sc->sc_input);
    769 		if (kme != NULL)
    770 			aprint_normal(", using %s", device_xname(kme->me_dv));
    771 #if NWSMUX == 0
    772 		sc->sc_input = kme;
    773 #endif
    774 #endif
    775 
    776 		sc->sc_focusidx = 0;
    777 		sc->sc_focus = sc->sc_scr[0];
    778 		start = 1;
    779 
    780 		wsdisplay_console_attached = 1;
    781 	}
    782 	aprint_normal("\n");
    783 
    784 #if NWSKBD > 0 && NWSMUX > 0
    785 	wsmux_set_display(mux, sc->sc_dev);
    786 #endif
    787 
    788 	mutex_init(&sc->sc_flagsmtx, MUTEX_DEFAULT, IPL_NONE);
    789 	cv_init(&sc->sc_flagscv, "wssw");
    790 
    791 	sc->sc_accessops = accessops;
    792 	sc->sc_accesscookie = accesscookie;
    793 	sc->sc_scrdata = scrdata;
    794 
    795 #ifdef WSDISPLAY_SCROLLSUPPORT
    796 	sc->sc_scroll_values = wsdisplay_default_scroll_values;
    797 #endif
    798 
    799 	/*
    800 	 * Set up a number of virtual screens if wanted. The
    801 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    802 	 * is for special cases like installation kernels.
    803 	 */
    804 	for (i = start; i < wsdisplay_defaultscreens; i++) {
    805 		if (wsdisplay_addscreen(sc, i, 0, 0))
    806 			break;
    807 	}
    808 
    809 	if (i > start)
    810 		wsdisplay_addscreen_print(sc, start, i-start);
    811 
    812 	if (!pmf_device_register(sc->sc_dev, wsdisplay_suspend, NULL))
    813 		aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
    814 }
    815 
    816 void
    817 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
    818 	int ccol, int crow, long defattr)
    819 {
    820 	const struct wsemul_ops *wsemul;
    821 
    822 	KASSERT(wsdisplay_console_initted < 2);
    823 	KASSERT(type->nrows > 0);
    824 	KASSERT(type->ncols > 0);
    825 	KASSERT(crow < type->nrows);
    826 	KASSERT(ccol < type->ncols);
    827 
    828 	wsdisplay_console_conf.emulops = type->textops;
    829 	wsdisplay_console_conf.emulcookie = cookie;
    830 	wsdisplay_console_conf.scrdata = type;
    831 
    832 	wsemul = wsemul_pick(0); /* default */
    833 	wsdisplay_console_conf.wsemul = wsemul;
    834 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    835 								  ccol, crow,
    836 								  defattr);
    837 
    838 	cn_tab = &wsdisplay_cons;
    839 	wsdisplay_console_initted = 2;
    840 }
    841 
    842 void
    843 wsdisplay_preattach(const struct wsscreen_descr *type, void *cookie,
    844 	int ccol, int crow, long defattr)
    845 {
    846 	const struct wsemul_ops *wsemul;
    847 
    848 	KASSERT(!wsdisplay_console_initted);
    849 	KASSERT(type->nrows > 0);
    850 	KASSERT(type->ncols > 0);
    851 	KASSERT(crow < type->nrows);
    852 	KASSERT(ccol < type->ncols);
    853 
    854 	wsdisplay_console_conf.emulops = type->textops;
    855 	wsdisplay_console_conf.emulcookie = cookie;
    856 	wsdisplay_console_conf.scrdata = type;
    857 
    858 	wsemul = wsemul_pick(0); /* default */
    859 	wsdisplay_console_conf.wsemul = wsemul;
    860 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    861 								  ccol, crow,
    862 								  defattr);
    863 
    864 	cn_tab = &wsdisplay_cons;
    865 	wsdisplay_console_initted = 1;
    866 }
    867 
    868 /*
    869  * Tty and cdevsw functions.
    870  */
    871 int
    872 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
    873 {
    874 	device_t dv;
    875 	struct wsdisplay_softc *sc;
    876 	struct tty *tp;
    877 	int newopen, error;
    878 	struct wsscreen *scr;
    879 
    880 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    881 	if (dv == NULL)			/* make sure it was attached */
    882 		return (ENXIO);
    883 
    884 	sc = device_private(dv);
    885 
    886 	if (ISWSDISPLAYSTAT(dev)) {
    887 		wsevent_init(&sc->evar, l->l_proc);
    888 		return (0);
    889 	}
    890 
    891 	if (ISWSDISPLAYCTL(dev))
    892 		return (0);
    893 
    894 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    895 		return (ENXIO);
    896 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    897 		return (ENXIO);
    898 
    899 	if (WSSCREEN_HAS_TTY(scr)) {
    900 		tp = scr->scr_tty;
    901 		tp->t_oproc = wsdisplaystart;
    902 		tp->t_param = wsdisplayparam;
    903 		tp->t_dev = dev;
    904 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    905 
    906 		if (kauth_authorize_device_tty(l->l_cred,
    907 			KAUTH_DEVICE_TTY_OPEN, tp))
    908 			return (EBUSY);
    909 
    910 		if (newopen) {
    911 			ttychars(tp);
    912 			tp->t_iflag = TTYDEF_IFLAG;
    913 			tp->t_oflag = TTYDEF_OFLAG;
    914 			tp->t_cflag = TTYDEF_CFLAG;
    915 			tp->t_lflag = TTYDEF_LFLAG;
    916 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    917 			wsdisplayparam(tp, &tp->t_termios);
    918 			ttsetwater(tp);
    919 		}
    920 		tp->t_state |= TS_CARR_ON;
    921 
    922 		error = ((*tp->t_linesw->l_open)(dev, tp));
    923 		if (error)
    924 			return (error);
    925 
    926 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    927 			/* set window sizes as appropriate, and reset
    928 			 the emulation */
    929 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    930 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    931 
    932 			/* wsdisplay_set_emulation() */
    933 		}
    934 	}
    935 
    936 	scr->scr_flags |= SCR_OPEN;
    937 	return (0);
    938 }
    939 
    940 int
    941 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
    942 {
    943 	device_t dv;
    944 	struct wsdisplay_softc *sc;
    945 	struct tty *tp;
    946 	struct wsscreen *scr;
    947 
    948 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    949 	sc = device_private(dv);
    950 
    951 	if (ISWSDISPLAYSTAT(dev)) {
    952 		wsevent_fini(&sc->evar);
    953 		return (0);
    954 	}
    955 
    956 	if (ISWSDISPLAYCTL(dev))
    957 		return (0);
    958 
    959 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    960 		return (0);
    961 
    962 	if (WSSCREEN_HAS_TTY(scr)) {
    963 		if (scr->scr_hold_screen) {
    964 			int s;
    965 
    966 			/* XXX RESET KEYBOARD LEDS, etc. */
    967 			s = spltty();	/* avoid conflict with keyboard */
    968 			wsdisplay_kbdholdscreen(dv, 0);
    969 			splx(s);
    970 		}
    971 		tp = scr->scr_tty;
    972 		(*tp->t_linesw->l_close)(tp, flag);
    973 		ttyclose(tp);
    974 	}
    975 
    976 	if (scr->scr_syncops)
    977 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    978 
    979 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    980 		scr->scr_flags &= ~SCR_GRAPHICS;
    981 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    982 						 WSEMUL_RESET);
    983 		if (wsdisplay_clearonclose)
    984 			(*scr->scr_dconf->wsemul->reset)
    985 				(scr->scr_dconf->wsemulcookie,
    986 				 WSEMUL_CLEARSCREEN);
    987 	}
    988 
    989 #ifdef WSDISPLAY_COMPAT_RAWKBD
    990 	if (scr->scr_rawkbd) {
    991 		int kbmode = WSKBD_TRANSLATED;
    992 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    993 					       (void *)&kbmode, 0, l);
    994 	}
    995 #endif
    996 
    997 	scr->scr_flags &= ~SCR_OPEN;
    998 
    999 	return (0);
   1000 }
   1001 
   1002 int
   1003 wsdisplayread(dev_t dev, struct uio *uio, int flag)
   1004 {
   1005 	device_t dv;
   1006 	struct wsdisplay_softc *sc;
   1007 	struct tty *tp;
   1008 	struct wsscreen *scr;
   1009 	int error;
   1010 
   1011 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1012 	sc = device_private(dv);
   1013 
   1014 	if (ISWSDISPLAYSTAT(dev)) {
   1015 		error = wsevent_read(&sc->evar, uio, flag);
   1016 		return (error);
   1017 	}
   1018 
   1019 	if (ISWSDISPLAYCTL(dev))
   1020 		return (0);
   1021 
   1022 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1023 		return (ENXIO);
   1024 
   1025 	if (!WSSCREEN_HAS_TTY(scr))
   1026 		return (ENODEV);
   1027 
   1028 	tp = scr->scr_tty;
   1029 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
   1030 }
   1031 
   1032 int
   1033 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
   1034 {
   1035 	device_t dv;
   1036 	struct wsdisplay_softc *sc;
   1037 	struct tty *tp;
   1038 	struct wsscreen *scr;
   1039 
   1040 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1041 	sc = device_private(dv);
   1042 
   1043 	if (ISWSDISPLAYSTAT(dev)) {
   1044 		return (0);
   1045 	}
   1046 
   1047 	if (ISWSDISPLAYCTL(dev))
   1048 		return (0);
   1049 
   1050 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1051 		return (ENXIO);
   1052 
   1053 	if (!WSSCREEN_HAS_TTY(scr))
   1054 		return (ENODEV);
   1055 
   1056 	tp = scr->scr_tty;
   1057 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
   1058 }
   1059 
   1060 int
   1061 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
   1062 {
   1063 	device_t dv;
   1064 	struct wsdisplay_softc *sc;
   1065 	struct tty *tp;
   1066 	struct wsscreen *scr;
   1067 
   1068 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1069 	sc = device_private(dv);
   1070 
   1071 	if (ISWSDISPLAYSTAT(dev))
   1072 		return (wsevent_poll(&sc->evar, events, l));
   1073 
   1074 	if (ISWSDISPLAYCTL(dev))
   1075 		return (0);
   1076 
   1077 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1078 		return (POLLHUP);
   1079 
   1080 	if (!WSSCREEN_HAS_TTY(scr))
   1081 		return (POLLERR);
   1082 
   1083 	tp = scr->scr_tty;
   1084 	return ((*tp->t_linesw->l_poll)(tp, events, l));
   1085 }
   1086 
   1087 int
   1088 wsdisplaykqfilter(dev_t dev, struct knote *kn)
   1089 {
   1090 	device_t dv;
   1091 	struct wsdisplay_softc *sc;
   1092 	struct wsscreen *scr;
   1093 
   1094 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1095 	sc = device_private(dv);
   1096 
   1097 	if (ISWSDISPLAYCTL(dev))
   1098 		return (1);
   1099 
   1100 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1101 		return (1);
   1102 
   1103 
   1104 	if (WSSCREEN_HAS_TTY(scr))
   1105 		return (ttykqfilter(dev, kn));
   1106 	else
   1107 		return (1);
   1108 }
   1109 
   1110 struct tty *
   1111 wsdisplaytty(dev_t dev)
   1112 {
   1113 	device_t dv;
   1114 	struct wsdisplay_softc *sc;
   1115 	struct wsscreen *scr;
   1116 
   1117 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1118 	sc = device_private(dv);
   1119 
   1120 	if (ISWSDISPLAYSTAT(dev))
   1121 		panic("wsdisplaytty() on status device");
   1122 
   1123 	if (ISWSDISPLAYCTL(dev))
   1124 		panic("wsdisplaytty() on ctl device");
   1125 
   1126 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1127 		return NULL;
   1128 
   1129 	return (scr->scr_tty);
   1130 }
   1131 
   1132 int
   1133 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1134 {
   1135 	device_t dv;
   1136 	struct wsdisplay_softc *sc;
   1137 	struct tty *tp;
   1138 	int error;
   1139 	struct wsscreen *scr;
   1140 
   1141 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1142 	sc = device_private(dv);
   1143 
   1144 #ifdef WSDISPLAY_COMPAT_USL
   1145 	error = wsdisplay_usl_ioctl1(dv, cmd, data, flag, l);
   1146 	if (error != EPASSTHROUGH)
   1147 		return (error);
   1148 #endif
   1149 
   1150 	if (ISWSDISPLAYSTAT(dev))
   1151 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
   1152 
   1153 	if (ISWSDISPLAYCTL(dev))
   1154 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
   1155 
   1156 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1157 		return (ENXIO);
   1158 
   1159 	if (WSSCREEN_HAS_TTY(scr)) {
   1160 		tp = scr->scr_tty;
   1161 
   1162 /* printf("disc\n"); */
   1163 		/* do the line discipline ioctls first */
   1164 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1165 		if (error != EPASSTHROUGH)
   1166 			return (error);
   1167 
   1168 /* printf("tty\n"); */
   1169 		/* then the tty ioctls */
   1170 		error = ttioctl(tp, cmd, data, flag, l);
   1171 		if (error != EPASSTHROUGH)
   1172 			return (error);
   1173 	}
   1174 
   1175 #ifdef WSDISPLAY_COMPAT_USL
   1176 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
   1177 	if (error != EPASSTHROUGH)
   1178 		return (error);
   1179 #endif
   1180 
   1181 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
   1182 }
   1183 
   1184 int
   1185 wsdisplay_param(device_t dv, u_long cmd, struct wsdisplay_param *dp)
   1186 {
   1187 	struct wsdisplay_softc *sc = device_private(dv);
   1188 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1189 					   sc->sc_focus->scr_dconf->emulcookie,
   1190 					   cmd, (void *)dp, 0, NULL));
   1191 }
   1192 
   1193 int
   1194 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1195 	u_long cmd, void *data, int flag, struct lwp *l)
   1196 {
   1197 	int error;
   1198 	char namebuf[16];
   1199 	struct wsdisplay_font fd;
   1200 #ifdef WSDISPLAY_SCROLLSUPPORT
   1201 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1202 #endif
   1203 
   1204 #if NWSKBD > 0
   1205 	struct wsevsrc *inp;
   1206 
   1207 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1208 	switch (cmd) {
   1209 	case WSKBDIO_SETMODE:
   1210 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1211 		return (wsdisplay_update_rawkbd(sc, scr));
   1212 	case WSKBDIO_GETMODE:
   1213 		*(int *)data = (scr->scr_rawkbd ?
   1214 				WSKBD_RAW : WSKBD_TRANSLATED);
   1215 		return (0);
   1216 	}
   1217 #endif
   1218 	inp = sc->sc_input;
   1219 	if (inp == NULL)
   1220 		return (ENXIO);
   1221 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1222 	if (error != EPASSTHROUGH)
   1223 		return (error);
   1224 #endif /* NWSKBD > 0 */
   1225 
   1226 	switch (cmd) {
   1227 	case WSDISPLAYIO_GMODE:
   1228 		if (scr->scr_flags & SCR_GRAPHICS) {
   1229 			if (scr->scr_flags & SCR_DUMBFB)
   1230 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1231 			else
   1232 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1233 		} else
   1234 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1235 		return (0);
   1236 
   1237 	case WSDISPLAYIO_SMODE:
   1238 #define d (*(int *)data)
   1239 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1240 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1241 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1242 			return (EINVAL);
   1243 
   1244 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1245 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1246 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1247 			d == WSDISPLAYIO_MODE_DUMBFB)
   1248 			    scr->scr_flags |= SCR_GRAPHICS |
   1249 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1250 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1251 		    return (EINVAL);
   1252 
   1253 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1254 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
   1255 
   1256 	    return (0);
   1257 #undef d
   1258 
   1259 #ifdef WSDISPLAY_SCROLLSUPPORT
   1260 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1261     do {								\
   1262 	(dstp)->fastlines = ((srcp)->which &				\
   1263 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1264 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1265 	(dstp)->slowlines = ((srcp)->which &				\
   1266 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1267 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1268 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1269     } while (0)
   1270 
   1271 
   1272 	case WSDISPLAYIO_DSSCROLL:
   1273 		usdp = (struct wsdisplay_scroll_data *)data;
   1274 		ksdp = &sc->sc_scroll_values;
   1275 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1276 		return (0);
   1277 
   1278 	case WSDISPLAYIO_DGSCROLL:
   1279 		usdp = (struct wsdisplay_scroll_data *)data;
   1280 		ksdp = &sc->sc_scroll_values;
   1281 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1282 		return (0);
   1283 #else
   1284 	case WSDISPLAYIO_DSSCROLL:
   1285 	case WSDISPLAYIO_DGSCROLL:
   1286 		return ENODEV;
   1287 #endif
   1288 
   1289 	case WSDISPLAYIO_SFONT:
   1290 #define d ((struct wsdisplay_usefontdata *)data)
   1291 		if (!sc->sc_accessops->load_font)
   1292 			return (EINVAL);
   1293 		if (d->name) {
   1294 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1295 			if (error)
   1296 				return (error);
   1297 			fd.name = namebuf;
   1298 		} else
   1299 			fd.name = 0;
   1300 		fd.data = 0;
   1301 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1302 					scr->scr_dconf->emulcookie, &fd);
   1303 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1304 			(*scr->scr_dconf->wsemul->reset)
   1305 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1306 		return (error);
   1307 #undef d
   1308 
   1309 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1310 	case WSDISPLAYIO_GMSGATTRS:
   1311 #define d ((struct wsdisplay_msgattrs *)data)
   1312 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1313 		    (scr->scr_dconf->wsemulcookie, d);
   1314 		return (0);
   1315 #undef d
   1316 
   1317 	case WSDISPLAYIO_SMSGATTRS: {
   1318 #define d ((struct wsdisplay_msgattrs *)data)
   1319 		int i;
   1320 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1321 			if (sc->sc_scr[i] != NULL)
   1322 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1323 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1324 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1325 				     d);
   1326 		}
   1327 		return (0);
   1328 #undef d
   1329 #else
   1330 	case WSDISPLAYIO_GMSGATTRS:
   1331 	case WSDISPLAYIO_SMSGATTRS:
   1332 		return (ENODEV);
   1333 #endif
   1334 	}
   1335 
   1336 	/* check ioctls for display */
   1337 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1338 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
   1339 }
   1340 
   1341 int
   1342 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1343 	int flag, struct lwp *l)
   1344 {
   1345 	switch (cmd) {
   1346 	case WSDISPLAYIO_GETACTIVESCREEN:
   1347 		*(int*)data = wsdisplay_getactivescreen(sc);
   1348 		return (0);
   1349 	}
   1350 
   1351 	return (EPASSTHROUGH);
   1352 }
   1353 
   1354 int
   1355 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1356 	int flag, struct lwp *l)
   1357 {
   1358 	int error;
   1359 	char *type, typebuf[16], *emul, emulbuf[16];
   1360 	void *tbuf;
   1361 	u_int fontsz;
   1362 #if defined(COMPAT_14) && NWSKBD > 0
   1363 	struct wsmux_device wsmuxdata;
   1364 #endif
   1365 #if NWSKBD > 0
   1366 	struct wsevsrc *inp;
   1367 #endif
   1368 
   1369 	switch (cmd) {
   1370 	case WSDISPLAYIO_ADDSCREEN:
   1371 #define d ((struct wsdisplay_addscreendata *)data)
   1372 		if (d->screentype) {
   1373 			error = copyinstr(d->screentype, typebuf,
   1374 					  sizeof(typebuf), 0);
   1375 			if (error)
   1376 				return (error);
   1377 			type = typebuf;
   1378 		} else
   1379 			type = 0;
   1380 		if (d->emul) {
   1381 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1382 			if (error)
   1383 				return (error);
   1384 			emul = emulbuf;
   1385 		} else
   1386 			emul = 0;
   1387 
   1388 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1389 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1390 		return (error);
   1391 #undef d
   1392 	case WSDISPLAYIO_DELSCREEN:
   1393 #define d ((struct wsdisplay_delscreendata *)data)
   1394 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1395 #undef d
   1396 	case WSDISPLAYIO_LDFONT:
   1397 #define d ((struct wsdisplay_font *)data)
   1398 		if (!sc->sc_accessops->load_font)
   1399 			return (EINVAL);
   1400 		if (d->name) {
   1401 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1402 			if (error)
   1403 				return (error);
   1404 			d->name = typebuf;
   1405 		} else
   1406 			d->name = "loaded"; /* ??? */
   1407 		fontsz = d->fontheight * d->stride * d->numchars;
   1408 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1409 			return (EINVAL);
   1410 
   1411 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1412 		error = copyin(d->data, tbuf, fontsz);
   1413 		if (error) {
   1414 			free(tbuf, M_DEVBUF);
   1415 			return (error);
   1416 		}
   1417 		d->data = tbuf;
   1418 		error =
   1419 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1420 		free(tbuf, M_DEVBUF);
   1421 #undef d
   1422 		return (error);
   1423 
   1424 #if NWSKBD > 0
   1425 #ifdef COMPAT_14
   1426 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1427 #define d ((struct wsdisplay_kbddata *)data)
   1428 		inp = sc->sc_input;
   1429 		if (inp == NULL)
   1430 			return (ENXIO);
   1431 		switch (d->op) {
   1432 		case _O_WSDISPLAY_KBD_ADD:
   1433 			if (d->idx == -1) {
   1434 				d->idx = wskbd_pickfree();
   1435 				if (d->idx == -1)
   1436 					return (ENXIO);
   1437 			}
   1438 			wsmuxdata.type = WSMUX_KBD;
   1439 			wsmuxdata.idx = d->idx;
   1440 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1441 					      &wsmuxdata, flag, l));
   1442 		case _O_WSDISPLAY_KBD_DEL:
   1443 			wsmuxdata.type = WSMUX_KBD;
   1444 			wsmuxdata.idx = d->idx;
   1445 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1446 					      &wsmuxdata, flag, l));
   1447 		default:
   1448 			return (EINVAL);
   1449 		}
   1450 #undef d
   1451 #endif
   1452 
   1453 	case WSMUXIO_ADD_DEVICE:
   1454 #define d ((struct wsmux_device *)data)
   1455 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1456 			d->idx = wskbd_pickfree();
   1457 #undef d
   1458 		/* fall into */
   1459 	case WSMUXIO_INJECTEVENT:
   1460 	case WSMUXIO_REMOVE_DEVICE:
   1461 	case WSMUXIO_LIST_DEVICES:
   1462 		inp = sc->sc_input;
   1463 		if (inp == NULL)
   1464 			return (ENXIO);
   1465 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1466 #endif /* NWSKBD > 0 */
   1467 
   1468 	}
   1469 	return (EPASSTHROUGH);
   1470 }
   1471 
   1472 int
   1473 wsdisplay_stat_inject(device_t dv, u_int type, int value)
   1474 {
   1475 	struct wsdisplay_softc *sc = device_private(dv);
   1476 	struct wseventvar *evar;
   1477 	struct wscons_event event;
   1478 
   1479 	evar = &sc->evar;
   1480 
   1481 	if (evar == NULL)
   1482 		return (0);
   1483 
   1484 	if (evar->q == NULL)
   1485 		return (1);
   1486 
   1487 	event.type = type;
   1488 	event.value = value;
   1489 	if (wsevent_inject(evar, &event, 1) != 0) {
   1490 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1491 		return (1);
   1492 	}
   1493 
   1494 	return (0);
   1495 }
   1496 
   1497 paddr_t
   1498 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1499 {
   1500 	device_t dv;
   1501 	struct wsdisplay_softc *sc;
   1502 	struct wsscreen *scr;
   1503 
   1504 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1505 	sc = device_private(dv);
   1506 
   1507 	if (ISWSDISPLAYSTAT(dev))
   1508 		return (-1);
   1509 
   1510 	if (ISWSDISPLAYCTL(dev))
   1511 		return (-1);
   1512 
   1513 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1514 		return (-1);
   1515 
   1516 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1517 		return (-1);
   1518 
   1519 	/* pass mmap to display */
   1520 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
   1521 	    scr->scr_dconf->emulcookie, offset, prot));
   1522 }
   1523 
   1524 void
   1525 wsdisplaystart(struct tty *tp)
   1526 {
   1527 	device_t dv;
   1528 	struct wsdisplay_softc *sc;
   1529 	struct wsscreen *scr;
   1530 	int s, n;
   1531 	u_char *tbuf;
   1532 
   1533 	s = spltty();
   1534 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1535 		splx(s);
   1536 		return;
   1537 	}
   1538 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1539 	sc = device_private(dv);
   1540 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
   1541 		splx(s);
   1542 		return;
   1543 	}
   1544 
   1545 	if (scr->scr_hold_screen) {
   1546 		tp->t_state |= TS_TIMEOUT;
   1547 		splx(s);
   1548 		return;
   1549 	}
   1550 	tp->t_state |= TS_BUSY;
   1551 	splx(s);
   1552 
   1553 	/*
   1554 	 * Drain output from ring buffer.
   1555 	 * The output will normally be in one contiguous chunk, but when the
   1556 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1557 	 * the other at the start.  For performance, rather than loop here,
   1558 	 * we output one chunk, see if there's another one, and if so, output
   1559 	 * it too.
   1560 	 */
   1561 
   1562 	n = ndqb(&tp->t_outq, 0);
   1563 	tbuf = tp->t_outq.c_cf;
   1564 
   1565 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1566 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1567 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1568 						  tbuf, n, 0);
   1569 	}
   1570 	ndflush(&tp->t_outq, n);
   1571 
   1572 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1573 		tbuf = tp->t_outq.c_cf;
   1574 
   1575 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1576 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1577 			(*scr->scr_dconf->wsemul->output)
   1578 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
   1579 		}
   1580 		ndflush(&tp->t_outq, n);
   1581 	}
   1582 
   1583 	s = spltty();
   1584 	tp->t_state &= ~TS_BUSY;
   1585 	/* Come back if there's more to do */
   1586 	if (ttypull(tp)) {
   1587 		tp->t_state |= TS_TIMEOUT;
   1588 		callout_schedule(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1);
   1589 	}
   1590 	splx(s);
   1591 }
   1592 
   1593 void
   1594 wsdisplaystop(struct tty *tp, int flag)
   1595 {
   1596 	int s;
   1597 
   1598 	s = spltty();
   1599 	if (ISSET(tp->t_state, TS_BUSY))
   1600 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1601 			SET(tp->t_state, TS_FLUSH);
   1602 	splx(s);
   1603 }
   1604 
   1605 /* Set line parameters. */
   1606 int
   1607 wsdisplayparam(struct tty *tp, struct termios *t)
   1608 {
   1609 
   1610 	tp->t_ispeed = t->c_ispeed;
   1611 	tp->t_ospeed = t->c_ospeed;
   1612 	tp->t_cflag = t->c_cflag;
   1613 	return 0;
   1614 }
   1615 
   1616 /*
   1617  * Callbacks for the emulation code.
   1618  */
   1619 void
   1620 wsdisplay_emulbell(void *v)
   1621 {
   1622 	struct wsscreen *scr = v;
   1623 
   1624 	if (scr == NULL)		/* console, before real attach */
   1625 		return;
   1626 
   1627 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1628 		return;
   1629 
   1630 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1631 					FWRITE, NULL);
   1632 }
   1633 
   1634 void
   1635 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1636 {
   1637 	struct wsscreen *scr = v;
   1638 	struct tty *tp;
   1639 
   1640 	if (v == NULL)			/* console, before real attach */
   1641 		return;
   1642 
   1643 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1644 		return;
   1645 	if (!WSSCREEN_HAS_TTY(scr))
   1646 		return;
   1647 
   1648 	tp = scr->scr_tty;
   1649 	while (count-- > 0)
   1650 		(*tp->t_linesw->l_rint)(*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 (hold)
   2098 		scr->scr_hold_screen = 1;
   2099 	else {
   2100 		scr->scr_hold_screen = 0;
   2101 		callout_schedule(&scr->scr_tty->t_rstrt_ch, 0);
   2102 	}
   2103 }
   2104 
   2105 #if NWSKBD > 0
   2106 void
   2107 wsdisplay_set_console_kbd(struct wsevsrc *src)
   2108 {
   2109 	if (wsdisplay_console_device == NULL) {
   2110 		src->me_dispdv = NULL;
   2111 		return;
   2112 	}
   2113 #if NWSMUX > 0
   2114 	if (wsmux_attach_sc((struct wsmux_softc *)
   2115 			    wsdisplay_console_device->sc_input, src)) {
   2116 		src->me_dispdv = NULL;
   2117 		return;
   2118 	}
   2119 #else
   2120 	wsdisplay_console_device->sc_input = src;
   2121 #endif
   2122 	src->me_dispdv = wsdisplay_console_device->sc_dev;
   2123 }
   2124 #endif /* NWSKBD > 0 */
   2125 
   2126 /*
   2127  * Console interface.
   2128  */
   2129 void
   2130 wsdisplay_cnputc(dev_t dev, int i)
   2131 {
   2132 	struct wsscreen_internal *dc;
   2133 	u_char c = i;
   2134 
   2135 	if (!wsdisplay_console_initted)
   2136 		return;
   2137 
   2138 	if ((wsdisplay_console_device != NULL) &&
   2139 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   2140 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   2141 		return;
   2142 
   2143 	dc = &wsdisplay_console_conf;
   2144 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   2145 }
   2146 
   2147 static int
   2148 wsdisplay_getc_dummy(dev_t dev)
   2149 {
   2150 	/* panic? */
   2151 	return (0);
   2152 }
   2153 
   2154 static void
   2155 wsdisplay_pollc(dev_t dev, int on)
   2156 {
   2157 
   2158 	wsdisplay_cons_pollmode = on;
   2159 
   2160 	/* notify to fb drivers */
   2161 	if (wsdisplay_console_device != NULL &&
   2162 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   2163 		(*wsdisplay_console_device->sc_accessops->pollc)
   2164 			(wsdisplay_console_device->sc_accesscookie, on);
   2165 
   2166 	/* notify to kbd drivers */
   2167 	if (wsdisplay_cons_kbd_pollc)
   2168 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   2169 }
   2170 
   2171 void
   2172 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   2173 	void (*bell)(dev_t, u_int, u_int, u_int))
   2174 {
   2175 	wsdisplay_cons.cn_getc = get;
   2176 	wsdisplay_cons.cn_bell = bell;
   2177 	wsdisplay_cons_kbd_pollc = poll;
   2178 }
   2179 
   2180 void
   2181 wsdisplay_unset_cons_kbd(void)
   2182 {
   2183 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   2184 	wsdisplay_cons.cn_bell = NULL;
   2185 	wsdisplay_cons_kbd_pollc = 0;
   2186 }
   2187