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