Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.117.6.4
      1 /* $NetBSD: wsdisplay.c,v 1.117.6.4 2008/06/29 09:33:11 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.4 2008/06/29 09:33:11 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 , cfdata_t, void *);
    162 static void wsdisplay_emul_attach(device_t, device_t, void *);
    163 static int wsdisplay_noemul_match(device_t, cfdata_t, 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, cfdata_t 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, cfdata_t 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 = device_lookup(&wsdisplay_cd, 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 	struct wsdisplay_softc *sc;
    875 	struct tty *tp;
    876 	int newopen, error;
    877 	struct wsscreen *scr;
    878 
    879 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    880 	if (sc == NULL)			/* make sure it was attached */
    881 		return (ENXIO);
    882 
    883 	if (ISWSDISPLAYSTAT(dev)) {
    884 		wsevent_init(&sc->evar, l->l_proc);
    885 		return (0);
    886 	}
    887 
    888 	if (ISWSDISPLAYCTL(dev))
    889 		return (0);
    890 
    891 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    892 		return (ENXIO);
    893 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    894 		return (ENXIO);
    895 
    896 	if (WSSCREEN_HAS_TTY(scr)) {
    897 		tp = scr->scr_tty;
    898 		tp->t_oproc = wsdisplaystart;
    899 		tp->t_param = wsdisplayparam;
    900 		tp->t_dev = dev;
    901 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    902 
    903 		if (kauth_authorize_device_tty(l->l_cred,
    904 			KAUTH_DEVICE_TTY_OPEN, tp))
    905 			return (EBUSY);
    906 
    907 		if (newopen) {
    908 			ttychars(tp);
    909 			tp->t_iflag = TTYDEF_IFLAG;
    910 			tp->t_oflag = TTYDEF_OFLAG;
    911 			tp->t_cflag = TTYDEF_CFLAG;
    912 			tp->t_lflag = TTYDEF_LFLAG;
    913 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    914 			wsdisplayparam(tp, &tp->t_termios);
    915 			ttsetwater(tp);
    916 		}
    917 		tp->t_state |= TS_CARR_ON;
    918 
    919 		error = ((*tp->t_linesw->l_open)(dev, tp));
    920 		if (error)
    921 			return (error);
    922 
    923 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    924 			/* set window sizes as appropriate, and reset
    925 			 the emulation */
    926 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    927 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    928 
    929 			/* wsdisplay_set_emulation() */
    930 		}
    931 	}
    932 
    933 	scr->scr_flags |= SCR_OPEN;
    934 	return (0);
    935 }
    936 
    937 int
    938 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
    939 {
    940 	device_t dv;
    941 	struct wsdisplay_softc *sc;
    942 	struct tty *tp;
    943 	struct wsscreen *scr;
    944 
    945 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
    946 	sc = device_private(dv);
    947 
    948 	if (ISWSDISPLAYSTAT(dev)) {
    949 		wsevent_fini(&sc->evar);
    950 		return (0);
    951 	}
    952 
    953 	if (ISWSDISPLAYCTL(dev))
    954 		return (0);
    955 
    956 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
    957 		return (0);
    958 
    959 	if (WSSCREEN_HAS_TTY(scr)) {
    960 		if (scr->scr_hold_screen) {
    961 			int s;
    962 
    963 			/* XXX RESET KEYBOARD LEDS, etc. */
    964 			s = spltty();	/* avoid conflict with keyboard */
    965 			wsdisplay_kbdholdscreen(dv, 0);
    966 			splx(s);
    967 		}
    968 		tp = scr->scr_tty;
    969 		(*tp->t_linesw->l_close)(tp, flag);
    970 		ttyclose(tp);
    971 	}
    972 
    973 	if (scr->scr_syncops)
    974 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    975 
    976 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    977 		scr->scr_flags &= ~SCR_GRAPHICS;
    978 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    979 						 WSEMUL_RESET);
    980 		if (wsdisplay_clearonclose)
    981 			(*scr->scr_dconf->wsemul->reset)
    982 				(scr->scr_dconf->wsemulcookie,
    983 				 WSEMUL_CLEARSCREEN);
    984 	}
    985 
    986 #ifdef WSDISPLAY_COMPAT_RAWKBD
    987 	if (scr->scr_rawkbd) {
    988 		int kbmode = WSKBD_TRANSLATED;
    989 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    990 					       (void *)&kbmode, 0, l);
    991 	}
    992 #endif
    993 
    994 	scr->scr_flags &= ~SCR_OPEN;
    995 
    996 	return (0);
    997 }
    998 
    999 int
   1000 wsdisplayread(dev_t dev, struct uio *uio, int flag)
   1001 {
   1002 	struct wsdisplay_softc *sc;
   1003 	struct tty *tp;
   1004 	struct wsscreen *scr;
   1005 	int error;
   1006 
   1007 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1008 
   1009 	if (ISWSDISPLAYSTAT(dev)) {
   1010 		error = wsevent_read(&sc->evar, uio, flag);
   1011 		return (error);
   1012 	}
   1013 
   1014 	if (ISWSDISPLAYCTL(dev))
   1015 		return (0);
   1016 
   1017 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1018 		return (ENXIO);
   1019 
   1020 	if (!WSSCREEN_HAS_TTY(scr))
   1021 		return (ENODEV);
   1022 
   1023 	tp = scr->scr_tty;
   1024 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
   1025 }
   1026 
   1027 int
   1028 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
   1029 {
   1030 	struct wsdisplay_softc *sc;
   1031 	struct tty *tp;
   1032 	struct wsscreen *scr;
   1033 
   1034 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1035 
   1036 	if (ISWSDISPLAYSTAT(dev)) {
   1037 		return (0);
   1038 	}
   1039 
   1040 	if (ISWSDISPLAYCTL(dev))
   1041 		return (0);
   1042 
   1043 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1044 		return (ENXIO);
   1045 
   1046 	if (!WSSCREEN_HAS_TTY(scr))
   1047 		return (ENODEV);
   1048 
   1049 	tp = scr->scr_tty;
   1050 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
   1051 }
   1052 
   1053 int
   1054 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
   1055 {
   1056 	struct wsdisplay_softc *sc;
   1057 	struct tty *tp;
   1058 	struct wsscreen *scr;
   1059 
   1060 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1061 
   1062 	if (ISWSDISPLAYSTAT(dev))
   1063 		return (wsevent_poll(&sc->evar, events, l));
   1064 
   1065 	if (ISWSDISPLAYCTL(dev))
   1066 		return (0);
   1067 
   1068 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1069 		return (POLLHUP);
   1070 
   1071 	if (!WSSCREEN_HAS_TTY(scr))
   1072 		return (POLLERR);
   1073 
   1074 	tp = scr->scr_tty;
   1075 	return ((*tp->t_linesw->l_poll)(tp, events, l));
   1076 }
   1077 
   1078 int
   1079 wsdisplaykqfilter(dev_t dev, struct knote *kn)
   1080 {
   1081 	struct wsdisplay_softc *sc;
   1082 	struct wsscreen *scr;
   1083 
   1084 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1085 
   1086 	if (ISWSDISPLAYCTL(dev))
   1087 		return (1);
   1088 
   1089 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1090 		return (1);
   1091 
   1092 
   1093 	if (WSSCREEN_HAS_TTY(scr))
   1094 		return (ttykqfilter(dev, kn));
   1095 	else
   1096 		return (1);
   1097 }
   1098 
   1099 struct tty *
   1100 wsdisplaytty(dev_t dev)
   1101 {
   1102 	struct wsdisplay_softc *sc;
   1103 	struct wsscreen *scr;
   1104 
   1105 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1106 
   1107 	if (ISWSDISPLAYSTAT(dev))
   1108 		panic("wsdisplaytty() on status device");
   1109 
   1110 	if (ISWSDISPLAYCTL(dev))
   1111 		panic("wsdisplaytty() on ctl device");
   1112 
   1113 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1114 		return NULL;
   1115 
   1116 	return (scr->scr_tty);
   1117 }
   1118 
   1119 int
   1120 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1121 {
   1122 	device_t dv;
   1123 	struct wsdisplay_softc *sc;
   1124 	struct tty *tp;
   1125 	int error;
   1126 	struct wsscreen *scr;
   1127 
   1128 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1129 	sc = device_private(dv);
   1130 
   1131 #ifdef WSDISPLAY_COMPAT_USL
   1132 	error = wsdisplay_usl_ioctl1(dv, cmd, data, flag, l);
   1133 	if (error != EPASSTHROUGH)
   1134 		return (error);
   1135 #endif
   1136 
   1137 	if (ISWSDISPLAYSTAT(dev))
   1138 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
   1139 
   1140 	if (ISWSDISPLAYCTL(dev))
   1141 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
   1142 
   1143 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1144 		return (ENXIO);
   1145 
   1146 	if (WSSCREEN_HAS_TTY(scr)) {
   1147 		tp = scr->scr_tty;
   1148 
   1149 /* printf("disc\n"); */
   1150 		/* do the line discipline ioctls first */
   1151 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1152 		if (error != EPASSTHROUGH)
   1153 			return (error);
   1154 
   1155 /* printf("tty\n"); */
   1156 		/* then the tty ioctls */
   1157 		error = ttioctl(tp, cmd, data, flag, l);
   1158 		if (error != EPASSTHROUGH)
   1159 			return (error);
   1160 	}
   1161 
   1162 #ifdef WSDISPLAY_COMPAT_USL
   1163 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
   1164 	if (error != EPASSTHROUGH)
   1165 		return (error);
   1166 #endif
   1167 
   1168 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
   1169 }
   1170 
   1171 int
   1172 wsdisplay_param(device_t dv, u_long cmd, struct wsdisplay_param *dp)
   1173 {
   1174 	struct wsdisplay_softc *sc = device_private(dv);
   1175 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1176 					   sc->sc_focus->scr_dconf->emulcookie,
   1177 					   cmd, (void *)dp, 0, NULL));
   1178 }
   1179 
   1180 int
   1181 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
   1182 	u_long cmd, void *data, int flag, struct lwp *l)
   1183 {
   1184 	int error;
   1185 	char namebuf[16];
   1186 	struct wsdisplay_font fd;
   1187 #ifdef WSDISPLAY_SCROLLSUPPORT
   1188 	struct wsdisplay_scroll_data *ksdp, *usdp;
   1189 #endif
   1190 
   1191 #if NWSKBD > 0
   1192 	struct wsevsrc *inp;
   1193 
   1194 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1195 	switch (cmd) {
   1196 	case WSKBDIO_SETMODE:
   1197 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
   1198 		return (wsdisplay_update_rawkbd(sc, scr));
   1199 	case WSKBDIO_GETMODE:
   1200 		*(int *)data = (scr->scr_rawkbd ?
   1201 				WSKBD_RAW : WSKBD_TRANSLATED);
   1202 		return (0);
   1203 	}
   1204 #endif
   1205 	inp = sc->sc_input;
   1206 	if (inp == NULL)
   1207 		return (ENXIO);
   1208 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
   1209 	if (error != EPASSTHROUGH)
   1210 		return (error);
   1211 #endif /* NWSKBD > 0 */
   1212 
   1213 	switch (cmd) {
   1214 	case WSDISPLAYIO_GMODE:
   1215 		if (scr->scr_flags & SCR_GRAPHICS) {
   1216 			if (scr->scr_flags & SCR_DUMBFB)
   1217 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
   1218 			else
   1219 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
   1220 		} else
   1221 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
   1222 		return (0);
   1223 
   1224 	case WSDISPLAYIO_SMODE:
   1225 #define d (*(int *)data)
   1226 		if (d != WSDISPLAYIO_MODE_EMUL &&
   1227 		    d != WSDISPLAYIO_MODE_MAPPED &&
   1228 		    d != WSDISPLAYIO_MODE_DUMBFB)
   1229 			return (EINVAL);
   1230 
   1231 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
   1232 		    scr->scr_flags &= ~SCR_GRAPHICS;
   1233 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
   1234 			d == WSDISPLAYIO_MODE_DUMBFB)
   1235 			    scr->scr_flags |= SCR_GRAPHICS |
   1236 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
   1237 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
   1238 		    return (EINVAL);
   1239 
   1240 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1241 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
   1242 
   1243 	    return (0);
   1244 #undef d
   1245 
   1246 #ifdef WSDISPLAY_SCROLLSUPPORT
   1247 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
   1248     do {								\
   1249 	(dstp)->fastlines = ((srcp)->which &				\
   1250 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
   1251 			     (srcp)->fastlines : (dfltp)->fastlines;	\
   1252 	(dstp)->slowlines = ((srcp)->which &				\
   1253 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
   1254 			     (srcp)->slowlines : (dfltp)->slowlines;	\
   1255 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
   1256     } while (0)
   1257 
   1258 
   1259 	case WSDISPLAYIO_DSSCROLL:
   1260 		usdp = (struct wsdisplay_scroll_data *)data;
   1261 		ksdp = &sc->sc_scroll_values;
   1262 		SETSCROLLLINES(ksdp, usdp, ksdp);
   1263 		return (0);
   1264 
   1265 	case WSDISPLAYIO_DGSCROLL:
   1266 		usdp = (struct wsdisplay_scroll_data *)data;
   1267 		ksdp = &sc->sc_scroll_values;
   1268 		SETSCROLLLINES(usdp, ksdp, ksdp);
   1269 		return (0);
   1270 #else
   1271 	case WSDISPLAYIO_DSSCROLL:
   1272 	case WSDISPLAYIO_DGSCROLL:
   1273 		return ENODEV;
   1274 #endif
   1275 
   1276 	case WSDISPLAYIO_SFONT:
   1277 #define d ((struct wsdisplay_usefontdata *)data)
   1278 		if (!sc->sc_accessops->load_font)
   1279 			return (EINVAL);
   1280 		if (d->name) {
   1281 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
   1282 			if (error)
   1283 				return (error);
   1284 			fd.name = namebuf;
   1285 		} else
   1286 			fd.name = 0;
   1287 		fd.data = 0;
   1288 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
   1289 					scr->scr_dconf->emulcookie, &fd);
   1290 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
   1291 			(*scr->scr_dconf->wsemul->reset)
   1292 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
   1293 		return (error);
   1294 #undef d
   1295 
   1296 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1297 	case WSDISPLAYIO_GMSGATTRS:
   1298 #define d ((struct wsdisplay_msgattrs *)data)
   1299 		(*scr->scr_dconf->wsemul->getmsgattrs)
   1300 		    (scr->scr_dconf->wsemulcookie, d);
   1301 		return (0);
   1302 #undef d
   1303 
   1304 	case WSDISPLAYIO_SMSGATTRS: {
   1305 #define d ((struct wsdisplay_msgattrs *)data)
   1306 		int i;
   1307 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
   1308 			if (sc->sc_scr[i] != NULL)
   1309 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
   1310 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
   1311 				     sc->sc_scr[i]->scr_dconf->scrdata,
   1312 				     d);
   1313 		}
   1314 		return (0);
   1315 #undef d
   1316 #else
   1317 	case WSDISPLAYIO_GMSGATTRS:
   1318 	case WSDISPLAYIO_SMSGATTRS:
   1319 		return (ENODEV);
   1320 #endif
   1321 	}
   1322 
   1323 	/* check ioctls for display */
   1324 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
   1325 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
   1326 }
   1327 
   1328 int
   1329 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1330 	int flag, struct lwp *l)
   1331 {
   1332 	switch (cmd) {
   1333 	case WSDISPLAYIO_GETACTIVESCREEN:
   1334 		*(int*)data = wsdisplay_getactivescreen(sc);
   1335 		return (0);
   1336 	}
   1337 
   1338 	return (EPASSTHROUGH);
   1339 }
   1340 
   1341 int
   1342 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
   1343 	int flag, struct lwp *l)
   1344 {
   1345 	int error;
   1346 	char *type, typebuf[16], *emul, emulbuf[16];
   1347 	void *tbuf;
   1348 	u_int fontsz;
   1349 #if defined(COMPAT_14) && NWSKBD > 0
   1350 	struct wsmux_device wsmuxdata;
   1351 #endif
   1352 #if NWSKBD > 0
   1353 	struct wsevsrc *inp;
   1354 #endif
   1355 
   1356 	switch (cmd) {
   1357 	case WSDISPLAYIO_ADDSCREEN:
   1358 #define d ((struct wsdisplay_addscreendata *)data)
   1359 		if (d->screentype) {
   1360 			error = copyinstr(d->screentype, typebuf,
   1361 					  sizeof(typebuf), 0);
   1362 			if (error)
   1363 				return (error);
   1364 			type = typebuf;
   1365 		} else
   1366 			type = 0;
   1367 		if (d->emul) {
   1368 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
   1369 			if (error)
   1370 				return (error);
   1371 			emul = emulbuf;
   1372 		} else
   1373 			emul = 0;
   1374 
   1375 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
   1376 			wsdisplay_addscreen_print(sc, d->idx, 0);
   1377 		return (error);
   1378 #undef d
   1379 	case WSDISPLAYIO_DELSCREEN:
   1380 #define d ((struct wsdisplay_delscreendata *)data)
   1381 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1382 #undef d
   1383 	case WSDISPLAYIO_LDFONT:
   1384 #define d ((struct wsdisplay_font *)data)
   1385 		if (!sc->sc_accessops->load_font)
   1386 			return (EINVAL);
   1387 		if (d->name) {
   1388 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1389 			if (error)
   1390 				return (error);
   1391 			d->name = typebuf;
   1392 		} else
   1393 			d->name = "loaded"; /* ??? */
   1394 		fontsz = d->fontheight * d->stride * d->numchars;
   1395 		if (fontsz > WSDISPLAY_MAXFONTSZ)
   1396 			return (EINVAL);
   1397 
   1398 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
   1399 		error = copyin(d->data, tbuf, fontsz);
   1400 		if (error) {
   1401 			free(tbuf, M_DEVBUF);
   1402 			return (error);
   1403 		}
   1404 		d->data = tbuf;
   1405 		error =
   1406 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1407 		free(tbuf, M_DEVBUF);
   1408 #undef d
   1409 		return (error);
   1410 
   1411 #if NWSKBD > 0
   1412 #ifdef COMPAT_14
   1413 	case _O_WSDISPLAYIO_SETKEYBOARD:
   1414 #define d ((struct wsdisplay_kbddata *)data)
   1415 		inp = sc->sc_input;
   1416 		if (inp == NULL)
   1417 			return (ENXIO);
   1418 		switch (d->op) {
   1419 		case _O_WSDISPLAY_KBD_ADD:
   1420 			if (d->idx == -1) {
   1421 				d->idx = wskbd_pickfree();
   1422 				if (d->idx == -1)
   1423 					return (ENXIO);
   1424 			}
   1425 			wsmuxdata.type = WSMUX_KBD;
   1426 			wsmuxdata.idx = d->idx;
   1427 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
   1428 					      &wsmuxdata, flag, l));
   1429 		case _O_WSDISPLAY_KBD_DEL:
   1430 			wsmuxdata.type = WSMUX_KBD;
   1431 			wsmuxdata.idx = d->idx;
   1432 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
   1433 					      &wsmuxdata, flag, l));
   1434 		default:
   1435 			return (EINVAL);
   1436 		}
   1437 #undef d
   1438 #endif
   1439 
   1440 	case WSMUXIO_ADD_DEVICE:
   1441 #define d ((struct wsmux_device *)data)
   1442 		if (d->idx == -1 && d->type == WSMUX_KBD)
   1443 			d->idx = wskbd_pickfree();
   1444 #undef d
   1445 		/* fall into */
   1446 	case WSMUXIO_INJECTEVENT:
   1447 	case WSMUXIO_REMOVE_DEVICE:
   1448 	case WSMUXIO_LIST_DEVICES:
   1449 		inp = sc->sc_input;
   1450 		if (inp == NULL)
   1451 			return (ENXIO);
   1452 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
   1453 #endif /* NWSKBD > 0 */
   1454 
   1455 	}
   1456 	return (EPASSTHROUGH);
   1457 }
   1458 
   1459 int
   1460 wsdisplay_stat_inject(device_t dv, u_int type, int value)
   1461 {
   1462 	struct wsdisplay_softc *sc = device_private(dv);
   1463 	struct wseventvar *evar;
   1464 	struct wscons_event event;
   1465 
   1466 	evar = &sc->evar;
   1467 
   1468 	if (evar == NULL)
   1469 		return (0);
   1470 
   1471 	if (evar->q == NULL)
   1472 		return (1);
   1473 
   1474 	event.type = type;
   1475 	event.value = value;
   1476 	if (wsevent_inject(evar, &event, 1) != 0) {
   1477 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
   1478 		return (1);
   1479 	}
   1480 
   1481 	return (0);
   1482 }
   1483 
   1484 paddr_t
   1485 wsdisplaymmap(dev_t dev, off_t offset, int prot)
   1486 {
   1487 	struct wsdisplay_softc *sc;
   1488 	struct wsscreen *scr;
   1489 
   1490 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
   1491 
   1492 	if (ISWSDISPLAYSTAT(dev))
   1493 		return (-1);
   1494 
   1495 	if (ISWSDISPLAYCTL(dev))
   1496 		return (-1);
   1497 
   1498 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
   1499 		return (-1);
   1500 
   1501 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1502 		return (-1);
   1503 
   1504 	/* pass mmap to display */
   1505 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
   1506 	    scr->scr_dconf->emulcookie, offset, prot));
   1507 }
   1508 
   1509 void
   1510 wsdisplaystart(struct tty *tp)
   1511 {
   1512 	struct wsdisplay_softc *sc;
   1513 	struct wsscreen *scr;
   1514 	int s, n;
   1515 	u_char *tbuf;
   1516 
   1517 	s = spltty();
   1518 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1519 		splx(s);
   1520 		return;
   1521 	}
   1522 	sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
   1523 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
   1524 		splx(s);
   1525 		return;
   1526 	}
   1527 
   1528 	if (scr->scr_hold_screen) {
   1529 		tp->t_state |= TS_TIMEOUT;
   1530 		splx(s);
   1531 		return;
   1532 	}
   1533 	tp->t_state |= TS_BUSY;
   1534 	splx(s);
   1535 
   1536 	/*
   1537 	 * Drain output from ring buffer.
   1538 	 * The output will normally be in one contiguous chunk, but when the
   1539 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
   1540 	 * the other at the start.  For performance, rather than loop here,
   1541 	 * we output one chunk, see if there's another one, and if so, output
   1542 	 * it too.
   1543 	 */
   1544 
   1545 	n = ndqb(&tp->t_outq, 0);
   1546 	tbuf = tp->t_outq.c_cf;
   1547 
   1548 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1549 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1550 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1551 						  tbuf, n, 0);
   1552 	}
   1553 	ndflush(&tp->t_outq, n);
   1554 
   1555 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
   1556 		tbuf = tp->t_outq.c_cf;
   1557 
   1558 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1559 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1560 			(*scr->scr_dconf->wsemul->output)
   1561 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
   1562 		}
   1563 		ndflush(&tp->t_outq, n);
   1564 	}
   1565 
   1566 	s = spltty();
   1567 	tp->t_state &= ~TS_BUSY;
   1568 	/* Come back if there's more to do */
   1569 	if (ttypull(tp)) {
   1570 		tp->t_state |= TS_TIMEOUT;
   1571 		callout_schedule(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1);
   1572 	}
   1573 	splx(s);
   1574 }
   1575 
   1576 void
   1577 wsdisplaystop(struct tty *tp, int flag)
   1578 {
   1579 	int s;
   1580 
   1581 	s = spltty();
   1582 	if (ISSET(tp->t_state, TS_BUSY))
   1583 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1584 			SET(tp->t_state, TS_FLUSH);
   1585 	splx(s);
   1586 }
   1587 
   1588 /* Set line parameters. */
   1589 int
   1590 wsdisplayparam(struct tty *tp, struct termios *t)
   1591 {
   1592 
   1593 	tp->t_ispeed = t->c_ispeed;
   1594 	tp->t_ospeed = t->c_ospeed;
   1595 	tp->t_cflag = t->c_cflag;
   1596 	return 0;
   1597 }
   1598 
   1599 /*
   1600  * Callbacks for the emulation code.
   1601  */
   1602 void
   1603 wsdisplay_emulbell(void *v)
   1604 {
   1605 	struct wsscreen *scr = v;
   1606 
   1607 	if (scr == NULL)		/* console, before real attach */
   1608 		return;
   1609 
   1610 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1611 		return;
   1612 
   1613 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1614 					FWRITE, NULL);
   1615 }
   1616 
   1617 void
   1618 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
   1619 {
   1620 	struct wsscreen *scr = v;
   1621 	struct tty *tp;
   1622 
   1623 	if (v == NULL)			/* console, before real attach */
   1624 		return;
   1625 
   1626 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1627 		return;
   1628 	if (!WSSCREEN_HAS_TTY(scr))
   1629 		return;
   1630 
   1631 	tp = scr->scr_tty;
   1632 	while (count-- > 0)
   1633 		(*tp->t_linesw->l_rint)(*data++, tp);
   1634 }
   1635 
   1636 /*
   1637  * Calls from the keyboard interface.
   1638  */
   1639 void
   1640 wsdisplay_kbdinput(device_t dv, keysym_t ks)
   1641 {
   1642 	struct wsdisplay_softc *sc = device_private(dv);
   1643 	struct wsscreen *scr;
   1644 	const char *dp;
   1645 	int count;
   1646 	struct tty *tp;
   1647 
   1648 	KASSERT(sc != NULL);
   1649 
   1650 	scr = sc->sc_focus;
   1651 
   1652 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1653 		return;
   1654 
   1655 	tp = scr->scr_tty;
   1656 
   1657 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1658 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
   1659 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1660 		count = (*scr->scr_dconf->wsemul->translate)
   1661 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1662 		while (count-- > 0)
   1663 			(*tp->t_linesw->l_rint)(*dp++, tp);
   1664 	}
   1665 }
   1666 
   1667 #if defined(WSDISPLAY_COMPAT_RAWKBD)
   1668 int
   1669 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
   1670 {
   1671 #if NWSKBD > 0
   1672 	int s, raw, data, error;
   1673 	struct wsevsrc *inp;
   1674 
   1675 	s = spltty();
   1676 
   1677 	raw = (scr ? scr->scr_rawkbd : 0);
   1678 
   1679 	if (scr != sc->sc_focus ||
   1680 	    sc->sc_rawkbd == raw) {
   1681 		splx(s);
   1682 		return (0);
   1683 	}
   1684 
   1685 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
   1686 	inp = sc->sc_input;
   1687 	if (inp == NULL) {
   1688 		splx(s);
   1689 		return (ENXIO);
   1690 	}
   1691 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
   1692 	if (!error)
   1693 		sc->sc_rawkbd = raw;
   1694 	splx(s);
   1695 	return (error);
   1696 #else
   1697 	return (0);
   1698 #endif
   1699 }
   1700 #endif
   1701 
   1702 static void
   1703 wsdisplay_switch3_cb(void *arg, int error, int waitok)
   1704 {
   1705 	device_t dv = arg;
   1706 
   1707 	wsdisplay_switch3(dv, error, waitok);
   1708 }
   1709 
   1710 static int
   1711 wsdisplay_switch3(device_t dv, int error, int waitok)
   1712 {
   1713 	struct wsdisplay_softc *sc = device_private(dv);
   1714 	int no;
   1715 	struct wsscreen *scr;
   1716 
   1717 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1718 		aprint_error_dev(dv, "wsdisplay_switch3: not switching\n");
   1719 		return (EINVAL);
   1720 	}
   1721 
   1722 	no = sc->sc_screenwanted;
   1723 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1724 		panic("wsdisplay_switch3: invalid screen %d", no);
   1725 	scr = sc->sc_scr[no];
   1726 	if (!scr) {
   1727 		aprint_error_dev(dv,
   1728 		    "wsdisplay_switch3: screen %d disappeared\n", no);
   1729 		error = ENXIO;
   1730 	}
   1731 
   1732 	if (error) {
   1733 		/* try to recover, avoid recursion */
   1734 
   1735 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1736 			aprint_error_dev(dv, "wsdisplay_switch3: giving up\n");
   1737 			sc->sc_focus = 0;
   1738 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1739 			wsdisplay_update_rawkbd(sc, 0);
   1740 #endif
   1741 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1742 			return (error);
   1743 		}
   1744 
   1745 		sc->sc_screenwanted = sc->sc_oldscreen;
   1746 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1747 		return (wsdisplay_switch1(dv, 0, waitok));
   1748 	}
   1749 
   1750 	if (scr->scr_syncops && !error)
   1751 		sc->sc_flags |= SC_XATTACHED;
   1752 
   1753 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1754 
   1755 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1756 		wakeup(scr);
   1757 	return (error);
   1758 }
   1759 
   1760 static void
   1761 wsdisplay_switch2_cb(void *arg, int error, int waitok)
   1762 {
   1763 	device_t dv = arg;
   1764 
   1765 	wsdisplay_switch2(dv, error, waitok);
   1766 }
   1767 
   1768 static int
   1769 wsdisplay_switch2(device_t dv, int error, int waitok)
   1770 {
   1771 	struct wsdisplay_softc *sc = device_private(dv);
   1772 	int no;
   1773 	struct wsscreen *scr;
   1774 
   1775 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1776 		aprint_error_dev(dv, "wsdisplay_switch2: not switching\n");
   1777 		return (EINVAL);
   1778 	}
   1779 
   1780 	no = sc->sc_screenwanted;
   1781 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1782 		panic("wsdisplay_switch2: invalid screen %d", no);
   1783 	scr = sc->sc_scr[no];
   1784 	if (!scr) {
   1785 		aprint_error_dev(dv,
   1786 		    "wsdisplay_switch2: screen %d disappeared\n", no);
   1787 		error = ENXIO;
   1788 	}
   1789 
   1790 	if (error) {
   1791 		/* try to recover, avoid recursion */
   1792 
   1793 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
   1794 			aprint_error_dev(dv, "wsdisplay_switch2: giving up\n");
   1795 			sc->sc_focus = 0;
   1796 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1797 			return (error);
   1798 		}
   1799 
   1800 		sc->sc_screenwanted = sc->sc_oldscreen;
   1801 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1802 		return (wsdisplay_switch1(dv, 0, waitok));
   1803 	}
   1804 
   1805 	sc->sc_focusidx = no;
   1806 	sc->sc_focus = scr;
   1807 
   1808 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1809 	(void) wsdisplay_update_rawkbd(sc, scr);
   1810 #endif
   1811 	/* keyboard map??? */
   1812 
   1813 	if (scr->scr_syncops &&
   1814 	    !(sc->sc_isconsole && wsdisplay_cons_pollmode)) {
   1815 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1816 						    wsdisplay_switch3_cb, dv);
   1817 		if (error == EAGAIN) {
   1818 			/* switch will be done asynchronously */
   1819 			return (0);
   1820 		}
   1821 	}
   1822 
   1823 	return (wsdisplay_switch3(dv, error, waitok));
   1824 }
   1825 
   1826 static void
   1827 wsdisplay_switch1_cb(void *arg, int error, int waitok)
   1828 {
   1829 	device_t dv = arg;
   1830 
   1831 	wsdisplay_switch1(dv, error, waitok);
   1832 }
   1833 
   1834 static int
   1835 wsdisplay_switch1(device_t dv, int error, int waitok)
   1836 {
   1837 	struct wsdisplay_softc *sc = device_private(dv);
   1838 	int no;
   1839 	struct wsscreen *scr;
   1840 
   1841 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1842 		aprint_error_dev(dv, "wsdisplay_switch1: not switching\n");
   1843 		return (EINVAL);
   1844 	}
   1845 
   1846 	no = sc->sc_screenwanted;
   1847 	if (no == WSDISPLAY_NULLSCREEN) {
   1848 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1849 		if (!error) {
   1850 			sc->sc_flags &= ~SC_XATTACHED;
   1851 			sc->sc_focus = 0;
   1852 		}
   1853 		wakeup(sc);
   1854 		return (error);
   1855 	}
   1856 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1857 		panic("wsdisplay_switch1: invalid screen %d", no);
   1858 	scr = sc->sc_scr[no];
   1859 	if (!scr) {
   1860 		aprint_error_dev(dv, "wsdisplay_switch1: screen %d disappeared\n", no);
   1861 		error = ENXIO;
   1862 	}
   1863 
   1864 	if (error) {
   1865 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1866 		return (error);
   1867 	}
   1868 
   1869 	sc->sc_flags &= ~SC_XATTACHED;
   1870 
   1871 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1872 						 scr->scr_dconf->emulcookie,
   1873 						 waitok,
   1874 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch2_cb, dv);
   1875 	if (error == EAGAIN) {
   1876 		/* switch will be done asynchronously */
   1877 		return (0);
   1878 	}
   1879 
   1880 	return (wsdisplay_switch2(dv, error, waitok));
   1881 }
   1882 
   1883 int
   1884 wsdisplay_switch(device_t dv, int no, int waitok)
   1885 {
   1886 	struct wsdisplay_softc *sc = device_private(dv);
   1887 	int s, res = 0;
   1888 	struct wsscreen *scr;
   1889 
   1890 	if (no != WSDISPLAY_NULLSCREEN) {
   1891 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
   1892 			return (EINVAL);
   1893 		if (sc->sc_scr[no] == NULL)
   1894 			return (ENXIO);
   1895 	}
   1896 
   1897 	wsdisplay_stat_inject(dv, WSCONS_EVENT_SCREEN_SWITCH, no);
   1898 
   1899 	s = spltty();
   1900 
   1901 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
   1902 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
   1903 		splx(s);
   1904 		return (0);
   1905 	}
   1906 
   1907 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1908 		splx(s);
   1909 		return (EBUSY);
   1910 	}
   1911 
   1912 	sc->sc_flags |= SC_SWITCHPENDING;
   1913 	sc->sc_screenwanted = no;
   1914 
   1915 	splx(s);
   1916 
   1917 	scr = sc->sc_focus;
   1918 	if (!scr) {
   1919 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
   1920 		return (wsdisplay_switch1(dv, 0, waitok));
   1921 	} else
   1922 		sc->sc_oldscreen = sc->sc_focusidx;
   1923 
   1924 	if (scr->scr_syncops) {
   1925 		if (!(sc->sc_flags & SC_XATTACHED) ||
   1926 		    (sc->sc_isconsole && wsdisplay_cons_pollmode)) {
   1927 			/* nothing to do here */
   1928 			return (wsdisplay_switch1(dv, 0, waitok));
   1929 		}
   1930 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1931 						  wsdisplay_switch1_cb, dv);
   1932 		if (res == EAGAIN) {
   1933 			/* switch will be done asynchronously */
   1934 			return (0);
   1935 		}
   1936 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1937 		/* no way to save state */
   1938 		res = EBUSY;
   1939 	}
   1940 
   1941 	return (wsdisplay_switch1(dv, res, waitok));
   1942 }
   1943 
   1944 void
   1945 wsdisplay_reset(device_t dv, enum wsdisplay_resetops op)
   1946 {
   1947 	struct wsdisplay_softc *sc = device_private(dv);
   1948 	struct wsscreen *scr;
   1949 
   1950 	KASSERT(sc != NULL);
   1951 	scr = sc->sc_focus;
   1952 
   1953 	if (!scr)
   1954 		return;
   1955 
   1956 	switch (op) {
   1957 	case WSDISPLAY_RESETEMUL:
   1958 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1959 			break;
   1960 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1961 						 WSEMUL_RESET);
   1962 		break;
   1963 	case WSDISPLAY_RESETCLOSE:
   1964 		wsdisplay_closescreen(sc, scr);
   1965 		break;
   1966 	}
   1967 }
   1968 
   1969 /*
   1970  * Interface for (external) VT switch / process synchronization code
   1971  */
   1972 int
   1973 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
   1974 	void *cookie)
   1975 {
   1976 	if (scr->scr_syncops) {
   1977 		/*
   1978 		 * The screen is already claimed.
   1979 		 * Check if the owner is still alive.
   1980 		 */
   1981 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1982 			return (EBUSY);
   1983 	}
   1984 	scr->scr_syncops = ops;
   1985 	scr->scr_synccookie = cookie;
   1986 	if (scr == scr->sc->sc_focus)
   1987 		scr->sc->sc_flags |= SC_XATTACHED;
   1988 	return (0);
   1989 }
   1990 
   1991 int
   1992 wsscreen_detach_sync(struct wsscreen *scr)
   1993 {
   1994 	if (!scr->scr_syncops)
   1995 		return (EINVAL);
   1996 	scr->scr_syncops = 0;
   1997 	if (scr == scr->sc->sc_focus)
   1998 		scr->sc->sc_flags &= ~SC_XATTACHED;
   1999 	return (0);
   2000 }
   2001 
   2002 int
   2003 wsscreen_lookup_sync(struct wsscreen *scr,
   2004 	const struct wscons_syncops *ops, /* used as ID */
   2005 	void **cookiep)
   2006 {
   2007 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   2008 		return (EINVAL);
   2009 	*cookiep = scr->scr_synccookie;
   2010 	return (0);
   2011 }
   2012 
   2013 /*
   2014  * Interface to virtual screen stuff
   2015  */
   2016 int
   2017 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
   2018 {
   2019 	return (WSDISPLAY_MAXSCREEN - 1);
   2020 }
   2021 
   2022 int
   2023 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
   2024 {
   2025 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
   2026 		return (EINVAL);
   2027 	if (!sc->sc_scr[idx])
   2028 		return (ENXIO);
   2029 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   2030 }
   2031 
   2032 int
   2033 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
   2034 {
   2035 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
   2036 }
   2037 
   2038 int
   2039 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
   2040 {
   2041 	struct wsscreen *scr;
   2042 	int s, res = 0;
   2043 
   2044 	if (no == WSDISPLAY_NULLSCREEN) {
   2045 		s = spltty();
   2046 		while (sc->sc_focus && res == 0) {
   2047 			res = tsleep(sc, PCATCH, "wswait", 0);
   2048 		}
   2049 		splx(s);
   2050 		return (res);
   2051 	}
   2052 
   2053 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   2054 		return (ENXIO);
   2055 	scr = sc->sc_scr[no];
   2056 	if (!scr)
   2057 		return (ENXIO);
   2058 
   2059 	s = spltty();
   2060 	if (scr != sc->sc_focus) {
   2061 		scr->scr_flags |= SCR_WAITACTIVE;
   2062 		res = tsleep(scr, PCATCH, "wswait", 0);
   2063 		if (scr != sc->sc_scr[no])
   2064 			res = ENXIO; /* disappeared in the meantime */
   2065 		else
   2066 			scr->scr_flags &= ~SCR_WAITACTIVE;
   2067 	}
   2068 	splx(s);
   2069 	return (res);
   2070 }
   2071 
   2072 void
   2073 wsdisplay_kbdholdscreen(device_t dv, int hold)
   2074 {
   2075 	struct wsdisplay_softc *sc = device_private(dv);
   2076 	struct wsscreen *scr;
   2077 
   2078 	scr = sc->sc_focus;
   2079 
   2080 	if (hold)
   2081 		scr->scr_hold_screen = 1;
   2082 	else {
   2083 		scr->scr_hold_screen = 0;
   2084 		callout_schedule(&scr->scr_tty->t_rstrt_ch, 0);
   2085 	}
   2086 }
   2087 
   2088 #if NWSKBD > 0
   2089 void
   2090 wsdisplay_set_console_kbd(struct wsevsrc *src)
   2091 {
   2092 	if (wsdisplay_console_device == NULL) {
   2093 		src->me_dispdv = NULL;
   2094 		return;
   2095 	}
   2096 #if NWSMUX > 0
   2097 	if (wsmux_attach_sc((struct wsmux_softc *)
   2098 			    wsdisplay_console_device->sc_input, src)) {
   2099 		src->me_dispdv = NULL;
   2100 		return;
   2101 	}
   2102 #else
   2103 	wsdisplay_console_device->sc_input = src;
   2104 #endif
   2105 	src->me_dispdv = wsdisplay_console_device->sc_dev;
   2106 }
   2107 #endif /* NWSKBD > 0 */
   2108 
   2109 /*
   2110  * Console interface.
   2111  */
   2112 void
   2113 wsdisplay_cnputc(dev_t dev, int i)
   2114 {
   2115 	struct wsscreen_internal *dc;
   2116 	u_char c = i;
   2117 
   2118 	if (!wsdisplay_console_initted)
   2119 		return;
   2120 
   2121 	if ((wsdisplay_console_device != NULL) &&
   2122 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
   2123 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   2124 		return;
   2125 
   2126 	dc = &wsdisplay_console_conf;
   2127 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   2128 }
   2129 
   2130 static int
   2131 wsdisplay_getc_dummy(dev_t dev)
   2132 {
   2133 	/* panic? */
   2134 	return (0);
   2135 }
   2136 
   2137 static void
   2138 wsdisplay_pollc(dev_t dev, int on)
   2139 {
   2140 
   2141 	wsdisplay_cons_pollmode = on;
   2142 
   2143 	/* notify to fb drivers */
   2144 	if (wsdisplay_console_device != NULL &&
   2145 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
   2146 		(*wsdisplay_console_device->sc_accessops->pollc)
   2147 			(wsdisplay_console_device->sc_accesscookie, on);
   2148 
   2149 	/* notify to kbd drivers */
   2150 	if (wsdisplay_cons_kbd_pollc)
   2151 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
   2152 }
   2153 
   2154 void
   2155 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
   2156 	void (*bell)(dev_t, u_int, u_int, u_int))
   2157 {
   2158 	wsdisplay_cons.cn_getc = get;
   2159 	wsdisplay_cons.cn_bell = bell;
   2160 	wsdisplay_cons_kbd_pollc = poll;
   2161 }
   2162 
   2163 void
   2164 wsdisplay_unset_cons_kbd(void)
   2165 {
   2166 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
   2167 	wsdisplay_cons.cn_bell = NULL;
   2168 	wsdisplay_cons_kbd_pollc = 0;
   2169 }
   2170