Home | History | Annotate | Line # | Download | only in wscons
wsdisplay.c revision 1.19
      1 /* $NetBSD: wsdisplay.c,v 1.19 1999/02/08 07:05:51 sommerfe 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 static const char _copyright[] __attribute__ ((unused)) =
     34     "Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.";
     35 static const char _rcsid[] __attribute__ ((unused)) =
     36     "$NetBSD: wsdisplay.c,v 1.19 1999/02/08 07:05:51 sommerfe Exp $";
     37 
     38 #include <sys/param.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/kernel.h>
     43 #include <sys/proc.h>
     44 #include <sys/malloc.h>
     45 #include <sys/syslog.h>
     46 #include <sys/systm.h>
     47 #include <sys/tty.h>
     48 #include <sys/signalvar.h>
     49 #include <sys/errno.h>
     50 #include <sys/fcntl.h>
     51 #include <sys/vnode.h>
     52 
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wscons/wsdisplayvar.h>
     55 #include <dev/wscons/wsksymvar.h>
     56 #include <dev/wscons/wsksymdef.h>
     57 #include <dev/wscons/wsemulvar.h>
     58 #include <dev/wscons/wscons_callbacks.h>
     59 #include <dev/cons.h>
     60 
     61 #include "opt_wsdisplay_compat.h"
     62 
     63 #include "wsdisplay.h"
     64 
     65 struct wsscreen_internal {
     66 	const struct wsdisplay_emulops *emulops;
     67 	void	*emulcookie;
     68 
     69 	const struct wsscreen_descr *scrdata;
     70 
     71 	const struct wsemul_ops *wsemul;
     72 	void	*wsemulcookie;
     73 };
     74 
     75 struct wsscreen {
     76 	struct wsscreen_internal *scr_dconf;
     77 
     78 	struct tty *scr_tty;
     79 	int	scr_hold_screen;		/* hold tty output */
     80 
     81 	int scr_flags;
     82 #define SCR_OPEN 1		/* is it open? */
     83 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
     84 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
     85 	const struct wscons_syncops *scr_syncops;
     86 	void *scr_synccookie;
     87 
     88 #ifdef WSDISPLAY_COMPAT_RAWKBD
     89 	int scr_rawkbd;
     90 #endif
     91 
     92 	struct wsdisplay_softc *sc;
     93 };
     94 
     95 struct wsscreen *wsscreen_attach __P((struct wsdisplay_softc *, int,
     96 				      const char *,
     97 				      const struct wsscreen_descr *, void *,
     98 				      int, int, long));
     99 void wsscreen_detach __P((struct wsscreen *));
    100 static const struct wsscreen_descr *
    101 wsdisplay_screentype_pick __P((const struct wsscreen_list *, char *));
    102 int wsdisplay_addscreen __P((struct wsdisplay_softc *, int, char *, char *));
    103 static void wsdisplay_closescreen __P((struct wsdisplay_softc *,
    104 				       struct wsscreen *));
    105 int wsdisplay_delscreen __P((struct wsdisplay_softc *, int, int));
    106 
    107 #define WSDISPLAY_MAXSCREEN 8
    108 
    109 struct wsdisplay_softc {
    110 	struct device sc_dv;
    111 
    112 	const struct wsdisplay_accessops *sc_accessops;
    113 	void	*sc_accesscookie;
    114 
    115 	const struct wsscreen_list *sc_scrdata;
    116 
    117 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
    118 	int sc_focusidx;
    119 	struct wsscreen *sc_focus;
    120 
    121 	int	sc_isconsole;
    122 	struct device *sc_kbddv;
    123 
    124 	int sc_flags;
    125 #define SC_SWITCHPENDING 1
    126 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
    127 
    128 #ifdef WSDISPLAY_COMPAT_RAWKBD
    129 	int sc_rawkbd;
    130 #endif
    131 };
    132 
    133 #if NWSDISPLAY > 0
    134 extern struct cfdriver wsdisplay_cd;
    135 #endif /* NWSDISPLAY > 0 */
    136 
    137 /* Autoconfiguration definitions. */
    138 static int wsdisplay_emul_match __P((struct device *, struct cfdata *,
    139 	    void *));
    140 static void wsdisplay_emul_attach __P((struct device *, struct device *,
    141 	    void *));
    142 static int wsdisplay_noemul_match __P((struct device *, struct cfdata *,
    143 	    void *));
    144 static void wsdisplay_noemul_attach __P((struct device *, struct device *,
    145 	    void *));
    146 
    147 struct cfattach wsdisplay_emul_ca = {
    148 	sizeof (struct wsdisplay_softc),
    149 	wsdisplay_emul_match,
    150 	wsdisplay_emul_attach,
    151 };
    152 
    153 struct cfattach wsdisplay_noemul_ca = {
    154 	sizeof (struct wsdisplay_softc),
    155 	wsdisplay_noemul_match,
    156 	wsdisplay_noemul_attach,
    157 };
    158 
    159 /* Exported tty- and cdevsw-related functions. */
    160 cdev_decl(wsdisplay);
    161 
    162 #if NWSDISPLAY > 0
    163 static void wsdisplaystart __P((struct tty *));
    164 static int wsdisplayparam __P((struct tty *, struct termios *));
    165 #endif /* NWSDISPLAY > 0 */
    166 
    167 
    168 /* Internal macros, functions, and variables. */
    169 #define	SET(t, f)	(t) |= (f)
    170 #define	CLR(t, f)	(t) &= ~(f)
    171 #define	ISSET(t, f)	((t) & (f))
    172 
    173 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
    174 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
    175 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
    176 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    177 #define	WSDISPLAYBURST		OBUFSIZ*4
    178 
    179 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
    180 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
    181 
    182 static void wsdisplay_common_attach __P((struct wsdisplay_softc *sc,
    183 	    int console, const struct wsscreen_list *,
    184 	    const struct wsdisplay_accessops *accessops,
    185 	    void *accesscookie));
    186 
    187 #ifdef WSDISPLAY_COMPAT_RAWKBD
    188 int wsdisplay_update_rawkbd __P((struct wsdisplay_softc *,
    189 				 struct wsscreen *));
    190 #endif
    191 
    192 static int wsdisplay_console_initted;
    193 static struct wsdisplay_softc *wsdisplay_console_device;
    194 static struct wsscreen_internal wsdisplay_console_conf;
    195 
    196 static int wsdisplay_getc_dummy __P((dev_t));
    197 static void wsdisplay_pollc_dummy __P((dev_t, int));
    198 
    199 static struct consdev wsdisplay_cons = {
    200 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
    201 	wsdisplay_pollc_dummy, NODEV, CN_NORMAL
    202 };
    203 
    204 int wsdisplay_switch1 __P((void *, int, int));
    205 int wsdisplay_switch3 __P((void *, int, int));
    206 
    207 int wsdisplay_clearonclose;
    208 
    209 struct wsscreen *
    210 wsscreen_attach(sc, console, emul, type, cookie, ccol, crow, defattr)
    211 	struct wsdisplay_softc *sc;
    212 	int console;
    213 	const char *emul;
    214 	const struct wsscreen_descr *type;
    215 	void *cookie;
    216 	int ccol, crow;
    217 	long defattr;
    218 {
    219 	struct wsscreen_internal *dconf;
    220 	struct wsscreen *scr;
    221 
    222 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
    223 	if (!scr)
    224 		return (NULL);
    225 
    226 	if (console) {
    227 		dconf = &wsdisplay_console_conf;
    228 		/*
    229 		 * If there's an emulation, tell it about the callback argument.
    230 		 * The other stuff is already there.
    231 		 */
    232 		if (dconf->wsemul != NULL)
    233 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
    234 	} else { /* not console */
    235 		dconf = malloc(sizeof(struct wsscreen_internal),
    236 			       M_DEVBUF, M_NOWAIT);
    237 		dconf->emulops = type->textops;
    238 		dconf->emulcookie = cookie;
    239 		if (dconf->emulops) {
    240 			dconf->wsemul = wsemul_pick(emul);
    241 			if (dconf->wsemul == NULL) {
    242 				free(dconf, M_DEVBUF);
    243 				free(scr, M_DEVBUF);
    244 				return (NULL);
    245 			}
    246 			dconf->wsemulcookie =
    247 			  (*dconf->wsemul->attach)(0, type, cookie,
    248 						   ccol, crow, scr, defattr);
    249 		} else
    250 			dconf->wsemul = NULL;
    251 		dconf->scrdata = type;
    252 	}
    253 
    254 	scr->scr_dconf = dconf;
    255 
    256 	scr->scr_tty = ttymalloc();
    257 	tty_attach(scr->scr_tty);
    258 	scr->scr_hold_screen = 0;
    259 	if (WSSCREEN_HAS_EMULATOR(scr))
    260 		scr->scr_flags = 0;
    261 	else
    262 		scr->scr_flags = SCR_GRAPHICS;
    263 
    264 	scr->scr_syncops = 0;
    265 	scr->sc = sc;
    266 #ifdef WSDISPLAY_COMPAT_RAWKBD
    267 	scr->scr_rawkbd = 0;
    268 #endif
    269 	return (scr);
    270 }
    271 
    272 void
    273 wsscreen_detach(scr)
    274 	struct wsscreen *scr;
    275 {
    276 	int ccol, crow; /* XXX */
    277 
    278 	if (WSSCREEN_HAS_TTY(scr)) {
    279 		tty_detach(scr->scr_tty);
    280 		ttyfree(scr->scr_tty);
    281 	}
    282 	if (WSSCREEN_HAS_EMULATOR(scr))
    283 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
    284 						  &ccol, &crow);
    285 	free(scr->scr_dconf, M_DEVBUF);
    286 	free(scr, M_DEVBUF);
    287 }
    288 
    289 static const struct wsscreen_descr *
    290 wsdisplay_screentype_pick(scrdata, name)
    291 	const struct wsscreen_list *scrdata;
    292 	char *name;
    293 {
    294 	int i;
    295 	const struct wsscreen_descr *scr;
    296 
    297 	KASSERT(scrdata->nscreens > 0);
    298 
    299 	if (name == NULL)
    300 		return (scrdata->screens[0]);
    301 
    302 	for (i = 0; i < scrdata->nscreens; i++) {
    303 		scr = scrdata->screens[i];
    304 		if (!strcmp(name, scr->name))
    305 			return (scr);
    306 	}
    307 
    308 	return (0);
    309 }
    310 
    311 int
    312 wsdisplay_addscreen(sc, idx, screentype, emul)
    313 	struct wsdisplay_softc *sc;
    314 	int idx;
    315 	char *screentype, *emul;
    316 {
    317 	const struct wsscreen_descr *scrdesc;
    318 	int error;
    319 	void *cookie;
    320 	int ccol, crow;
    321 	long defattr;
    322 	struct wsscreen *scr;
    323 
    324 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    325 		return (EINVAL);
    326 	if (sc->sc_scr[idx] != NULL)
    327 		return (EBUSY);
    328 
    329 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
    330 	if (!scrdesc)
    331 		return (ENXIO);
    332 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
    333 			scrdesc, &cookie, &ccol, &crow, &defattr);
    334 	if (error)
    335 		return (error);
    336 
    337 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
    338 			      cookie, ccol, crow, defattr);
    339 	if (scr == NULL) {
    340 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    341 						 cookie);
    342 		return (ENXIO);
    343 	}
    344 
    345 	sc->sc_scr[idx] = scr;
    346 
    347 	printf("%s: screen %d added (%s", sc->sc_dv.dv_xname, idx,
    348 	       scrdesc->name);
    349 	if (WSSCREEN_HAS_EMULATOR(scr))
    350 		printf(", %s emulation", scr->scr_dconf->wsemul->name);
    351 	printf(")\n");
    352 	return (0);
    353 }
    354 
    355 static void
    356 wsdisplay_closescreen(sc, scr)
    357 	struct wsdisplay_softc *sc;
    358 	struct wsscreen *scr;
    359 {
    360 	int maj, mn, idx;
    361 
    362 	/* hangup */
    363 	if (WSSCREEN_HAS_TTY(scr)) {
    364 		struct tty *tp = scr->scr_tty;
    365 		(*linesw[tp->t_line].l_modem)(tp, 0);
    366 	}
    367 
    368 	/* locate the major number */
    369 	for (maj = 0; maj < nchrdev; maj++)
    370 		if (cdevsw[maj].d_open == wsdisplayopen)
    371 			break;
    372 	/* locate the screen index */
    373 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
    374 		if (scr == sc->sc_scr[idx])
    375 			break;
    376 #ifdef DIAGNOSTIC
    377 	if (idx == WSDISPLAY_MAXSCREEN)
    378 		panic("wsdisplay_forceclose: bad screen");
    379 #endif
    380 
    381 	/* nuke the vnodes */
    382 	mn = WSDISPLAYMINOR(sc->sc_dv.dv_unit, idx);
    383 	vdevgone(maj, mn, mn, VCHR);
    384 }
    385 
    386 int
    387 wsdisplay_delscreen(sc, idx, flags)
    388 	struct wsdisplay_softc *sc;
    389 	int idx, flags;
    390 {
    391 	struct wsscreen *scr;
    392 	int s;
    393 	void *cookie;
    394 
    395 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
    396 		return (EINVAL);
    397 	scr = sc->sc_scr[idx];
    398 	if (!scr)
    399 		return (ENXIO);
    400 
    401 	if (scr->scr_dconf == &wsdisplay_console_conf ||
    402 	    scr->scr_syncops ||
    403 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
    404 		return(EBUSY);
    405 
    406 	wsdisplay_closescreen(sc, scr);
    407 
    408 	/*
    409 	 * delete pointers, so neither device entries
    410 	 * nor keyboard input can reference it anymore
    411 	 */
    412 	s = spltty();
    413 	if (sc->sc_focus == scr) {
    414 		sc->sc_focus = 0;
    415 #ifdef WSDISPLAY_COMPAT_RAWKBD
    416 		wsdisplay_update_rawkbd(sc, 0);
    417 #endif
    418 	}
    419 	sc->sc_scr[idx] = 0;
    420 	splx(s);
    421 
    422 	/*
    423 	 * Wake up processes waiting for the screen to
    424 	 * be activated. Sleepers must check whether
    425 	 * the screen still exists.
    426 	 */
    427 	if (scr->scr_flags & SCR_WAITACTIVE)
    428 		wakeup(scr);
    429 
    430 	/* save a reference to the graphics screen */
    431 	cookie = scr->scr_dconf->emulcookie;
    432 
    433 	wsscreen_detach(scr);
    434 
    435 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
    436 					 cookie);
    437 
    438 	printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
    439 	return (0);
    440 }
    441 
    442 /*
    443  * Autoconfiguration functions.
    444  */
    445 int
    446 wsdisplay_emul_match(parent, match, aux)
    447 	struct device *parent;
    448 	struct cfdata *match;
    449 	void *aux;
    450 {
    451 	struct wsemuldisplaydev_attach_args *ap = aux;
    452 
    453 	if (match->wsemuldisplaydevcf_console !=
    454 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
    455 		/*
    456 		 * If console-ness of device specified, either match
    457 		 * exactly (at high priority), or fail.
    458 		 */
    459 		if (match->wsemuldisplaydevcf_console != 0 &&
    460 		    ap->console != 0)
    461 			return (10);
    462 		else
    463 			return (0);
    464 	}
    465 
    466 	/* If console-ness unspecified, it wins. */
    467 	return (1);
    468 }
    469 
    470 void
    471 wsdisplay_emul_attach(parent, self, aux)
    472 	struct device *parent, *self;
    473 	void *aux;
    474 {
    475 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    476 	struct wsemuldisplaydev_attach_args *ap = aux;
    477 
    478 	wsdisplay_common_attach(sc, ap->console, ap->scrdata,
    479 				ap->accessops, ap->accesscookie);
    480 
    481 	if (ap->console) {
    482 		int maj;
    483 
    484 		/* locate the major number */
    485 		for (maj = 0; maj < nchrdev; maj++)
    486 			if (cdevsw[maj].d_open == wsdisplayopen)
    487 				break;
    488 
    489 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(self->dv_unit, 0));
    490 	}
    491 }
    492 
    493 /* Print function (for parent devices). */
    494 int
    495 wsemuldisplaydevprint(aux, pnp)
    496 	void *aux;
    497 	const char *pnp;
    498 {
    499 #if 0 /* -Wunused */
    500 	struct wsemuldisplaydev_attach_args *ap = aux;
    501 #endif
    502 
    503 	if (pnp)
    504 		printf("wsdisplay at %s", pnp);
    505 #if 0 /* don't bother; it's ugly */
    506 	printf(" console %d", ap->console);
    507 #endif
    508 
    509 	return (UNCONF);
    510 }
    511 
    512 int
    513 wsdisplay_noemul_match(parent, match, aux)
    514 	struct device *parent;
    515 	struct cfdata *match;
    516 	void *aux;
    517 {
    518 #if 0 /* -Wunused */
    519 	struct wsdisplaydev_attach_args *ap = aux;
    520 #endif
    521 
    522 	/* Always match. */
    523 	return (1);
    524 }
    525 
    526 void
    527 wsdisplay_noemul_attach(parent, self, aux)
    528 	struct device *parent, *self;
    529 	void *aux;
    530 {
    531 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
    532 	struct wsdisplaydev_attach_args *ap = aux;
    533 
    534 	wsdisplay_common_attach(sc, 0, NULL, ap->accessops, ap->accesscookie);
    535 }
    536 
    537 /* Print function (for parent devices). */
    538 int
    539 wsdisplaydevprint(aux, pnp)
    540 	void *aux;
    541 	const char *pnp;
    542 {
    543 #if 0 /* -Wunused */
    544 	struct wsdisplaydev_attach_args *ap = aux;
    545 #endif
    546 
    547 	if (pnp)
    548 		printf("wsdisplay at %s", pnp);
    549 
    550 	return (UNCONF);
    551 }
    552 
    553 static void
    554 wsdisplay_common_attach(sc, console, scrdata, accessops, accesscookie)
    555 	struct wsdisplay_softc *sc;
    556 	int console;
    557 	const struct wsscreen_list *scrdata;
    558 	const struct wsdisplay_accessops *accessops;
    559 	void *accesscookie;
    560 {
    561 	int i = 0;
    562 
    563 	if (console) {
    564 		KASSERT(wsdisplay_console_initted);
    565 		KASSERT(wsdisplay_console_device == NULL);
    566 
    567 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
    568 		wsdisplay_console_device = sc;
    569 
    570 		printf(": console (%s, %s emulation)",
    571 		       wsdisplay_console_conf.scrdata->name,
    572 		       wsdisplay_console_conf.wsemul->name);
    573 
    574 		sc->sc_focusidx = 0;
    575 		sc->sc_focus = sc->sc_scr[0];
    576 		i++;
    577 	}
    578 
    579 	printf("\n");
    580 
    581 	sc->sc_accessops = accessops;
    582 	sc->sc_accesscookie = accesscookie;
    583 	sc->sc_scrdata = scrdata;
    584 
    585 	sc->sc_isconsole = console;
    586 	sc->sc_kbddv = NULL;
    587 
    588 	wscons_glue_set_callback();
    589 
    590 	/*
    591 	 * Set up a number of virtual screens if possible. The
    592 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
    593 	 * should go away as soon as we have a userspace utility.
    594 	 */
    595 #ifndef WSDISPLAY_DEFAULTSCREENS
    596 #define WSDISPLAY_DEFAULTSCREENS WSDISPLAY_MAXSCREEN
    597 #endif
    598 	for (; i < WSDISPLAY_DEFAULTSCREENS; i++)
    599 		if (wsdisplay_addscreen(sc, i, 0, 0))
    600 			break;
    601 }
    602 
    603 void
    604 wsdisplay_cnattach(type, cookie, ccol, crow, defattr)
    605 	const struct wsscreen_descr *type;
    606 	void *cookie;
    607 	int ccol, crow;
    608 	long defattr;
    609 {
    610 	const struct wsemul_ops *wsemul;
    611 
    612 	KASSERT(!wsdisplay_console_initted);
    613 	KASSERT(type->nrows > 0);
    614 	KASSERT(type->ncols > 0);
    615 	KASSERT(crow < type->nrows);
    616 	KASSERT(ccol < type->ncols);
    617 
    618 	wsdisplay_console_conf.emulops = type->textops;
    619 	wsdisplay_console_conf.emulcookie = cookie;
    620 	wsdisplay_console_conf.scrdata = type;
    621 
    622 	wsemul = wsemul_pick(0); /* default */
    623 	wsdisplay_console_conf.wsemul = wsemul;
    624 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
    625 								  ccol, crow,
    626 								  defattr);
    627 
    628 	cn_tab = &wsdisplay_cons;
    629 
    630 	wsdisplay_console_initted = 1;
    631 }
    632 
    633 /*
    634  * Tty and cdevsw functions.
    635  */
    636 int
    637 wsdisplayopen(dev, flag, mode, p)
    638 	dev_t dev;
    639 	int flag, mode;
    640 	struct proc *p;
    641 {
    642 #if NWSDISPLAY > 0
    643 	struct wsdisplay_softc *sc;
    644 	struct tty *tp;
    645 	int unit, newopen, error;
    646 	struct wsscreen *scr;
    647 
    648 	unit = WSDISPLAYUNIT(dev);
    649 	if (unit >= wsdisplay_cd.cd_ndevs ||	/* make sure it was attached */
    650 	    (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
    651 		return (ENXIO);
    652 
    653 	if (ISWSDISPLAYCTL(dev))
    654 		return (0);
    655 
    656 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
    657 		return (ENXIO);
    658 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    659 	if (!scr)
    660 		return (ENXIO);
    661 
    662 	if (WSSCREEN_HAS_TTY(scr)) {
    663 		tp = scr->scr_tty;
    664 		tp->t_oproc = wsdisplaystart;
    665 		tp->t_param = wsdisplayparam;
    666 		tp->t_dev = dev;
    667 		newopen = (tp->t_state & TS_ISOPEN) == 0;
    668 		if (newopen) {
    669 			ttychars(tp);
    670 			tp->t_iflag = TTYDEF_IFLAG;
    671 			tp->t_oflag = TTYDEF_OFLAG;
    672 			tp->t_cflag = TTYDEF_CFLAG;
    673 			tp->t_lflag = TTYDEF_LFLAG;
    674 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    675 			wsdisplayparam(tp, &tp->t_termios);
    676 			ttsetwater(tp);
    677 		} else if ((tp->t_state & TS_XCLUDE) != 0 &&
    678 			   p->p_ucred->cr_uid != 0)
    679 			return EBUSY;
    680 		tp->t_state |= TS_CARR_ON;
    681 
    682 		error = ((*linesw[tp->t_line].l_open)(dev, tp));
    683 		if (error)
    684 			return (error);
    685 
    686 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
    687 			/* set window sizes as appropriate, and reset
    688 			 the emulation */
    689 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
    690 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
    691 
    692 			/* wsdisplay_set_emulation() */
    693 		}
    694 	}
    695 
    696 	scr->scr_flags |= SCR_OPEN;
    697 	return (0);
    698 #else
    699 	return (ENXIO);
    700 #endif /* NWSDISPLAY > 0 */
    701 }
    702 
    703 int
    704 wsdisplayclose(dev, flag, mode, p)
    705 	dev_t dev;
    706 	int flag, mode;
    707 	struct proc *p;
    708 {
    709 #if NWSDISPLAY > 0
    710 	struct wsdisplay_softc *sc;
    711 	struct tty *tp;
    712 	int unit;
    713 	struct wsscreen *scr;
    714 
    715 	unit = WSDISPLAYUNIT(dev);
    716 	sc = wsdisplay_cd.cd_devs[unit];
    717 
    718 	if (ISWSDISPLAYCTL(dev))
    719 		return (0);
    720 
    721 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    722 
    723 	if (WSSCREEN_HAS_TTY(scr)) {
    724 		if (scr->scr_hold_screen) {
    725 			int s;
    726 
    727 			/* XXX RESET KEYBOARD LEDS, etc. */
    728 			s = spltty();	/* avoid conflict with keyboard */
    729 			wsdisplay_kbdholdscreen((struct device *)sc, 0);
    730 			splx(s);
    731 		}
    732 		tp = scr->scr_tty;
    733 		(*linesw[tp->t_line].l_close)(tp, flag);
    734 		ttyclose(tp);
    735 	}
    736 
    737 	if (scr->scr_syncops)
    738 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
    739 
    740 	if (WSSCREEN_HAS_EMULATOR(scr)) {
    741 		scr->scr_flags &= ~SCR_GRAPHICS;
    742 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
    743 						 WSEMUL_RESET);
    744 		if (wsdisplay_clearonclose)
    745 			(*scr->scr_dconf->wsemul->reset)
    746 				(scr->scr_dconf->wsemulcookie,
    747 				 WSEMUL_CLEARSCREEN);
    748 	}
    749 
    750 #ifdef WSDISPLAY_COMPAT_RAWKBD
    751 	if (scr->scr_rawkbd) {
    752 		int kbmode = WSKBD_TRANSLATED;
    753 		(void) wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
    754 						(caddr_t)&kbmode, 0, p);
    755 	}
    756 #endif
    757 
    758 	scr->scr_flags &= ~SCR_OPEN;
    759 
    760 	return (0);
    761 #else
    762 	return (ENXIO);
    763 #endif /* NWSDISPLAY > 0 */
    764 }
    765 
    766 int
    767 wsdisplayread(dev, uio, flag)
    768 	dev_t dev;
    769 	struct uio *uio;
    770 	int flag;
    771 {
    772 #if NWSDISPLAY > 0
    773 	struct wsdisplay_softc *sc;
    774 	struct tty *tp;
    775 	int unit;
    776 	struct wsscreen *scr;
    777 
    778 	unit = WSDISPLAYUNIT(dev);
    779 	sc = wsdisplay_cd.cd_devs[unit];
    780 
    781 	if (ISWSDISPLAYCTL(dev))
    782 		return (0);
    783 
    784 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    785 
    786 	if (!WSSCREEN_HAS_TTY(scr))
    787 		return (ENODEV);
    788 
    789 	tp = scr->scr_tty;
    790 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    791 #else
    792 	return (ENXIO);
    793 #endif /* NWSDISPLAY > 0 */
    794 }
    795 
    796 int
    797 wsdisplaywrite(dev, uio, flag)
    798 	dev_t dev;
    799 	struct uio *uio;
    800 	int flag;
    801 {
    802 #if NWSDISPLAY > 0
    803 	struct wsdisplay_softc *sc;
    804 	struct tty *tp;
    805 	int unit;
    806 	struct wsscreen *scr;
    807 
    808 	unit = WSDISPLAYUNIT(dev);
    809 	sc = wsdisplay_cd.cd_devs[unit];
    810 
    811 	if (ISWSDISPLAYCTL(dev))
    812 		return (0);
    813 
    814 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    815 
    816 	if (!WSSCREEN_HAS_TTY(scr))
    817 		return (ENODEV);
    818 
    819 	tp = scr->scr_tty;
    820 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    821 #else
    822 	return (ENXIO);
    823 #endif /* NWSDISPLAY > 0 */
    824 }
    825 
    826 struct tty *
    827 wsdisplaytty(dev)
    828 	dev_t dev;
    829 {
    830 #if NWSDISPLAY > 0
    831 	struct wsdisplay_softc *sc;
    832 	int unit;
    833 	struct wsscreen *scr;
    834 
    835 	unit = WSDISPLAYUNIT(dev);
    836 	sc = wsdisplay_cd.cd_devs[unit];
    837 
    838 	if (ISWSDISPLAYCTL(dev))
    839 		panic("wsdisplaytty() on ctl device");
    840 
    841 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    842 
    843 	return (scr->scr_tty);
    844 #else
    845 	return (NULL);
    846 #endif /* NWSDISPLAY > 0 */
    847 }
    848 
    849 int
    850 wsdisplayioctl(dev, cmd, data, flag, p)
    851 	dev_t dev;
    852 	u_long cmd;
    853 	caddr_t data;
    854 	int flag;
    855 	struct proc *p;
    856 {
    857 #if NWSDISPLAY > 0
    858 	struct wsdisplay_softc *sc;
    859 	struct tty *tp;
    860 	int unit, error;
    861 	struct wsscreen *scr;
    862 
    863 	unit = WSDISPLAYUNIT(dev);
    864 	sc = wsdisplay_cd.cd_devs[unit];
    865 
    866 	if (ISWSDISPLAYCTL(dev))
    867 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, p));
    868 
    869 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
    870 
    871 	if (WSSCREEN_HAS_TTY(scr)) {
    872 		tp = scr->scr_tty;
    873 
    874 /* printf("disc\n"); */
    875 		/* do the line discipline ioctls first */
    876 		error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    877 		if (error >= 0)
    878 			return error;
    879 
    880 /* printf("tty\n"); */
    881 		/* then the tty ioctls */
    882 		error = ttioctl(tp, cmd, data, flag, p);
    883 		if (error >= 0)
    884 			return error;
    885 	}
    886 
    887 #ifdef WSDISPLAY_COMPAT_USL
    888 	error = wsdisplay_usl_ioctl(sc, scr, cmd, data, flag, p);
    889 	if (error >= 0)
    890 		return (error);
    891 #endif
    892 
    893 	error = wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p);
    894 	return (error != -1 ? error : ENOTTY);
    895 #else
    896 	return (ENXIO);
    897 #endif /* NWSDISPLAY > 0 */
    898 }
    899 
    900 int
    901 wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p)
    902 	struct wsdisplay_softc *sc;
    903 	struct wsscreen *scr;
    904 	u_long cmd;
    905 	caddr_t data;
    906 	int flag;
    907 	struct proc *p;
    908 {
    909 	int error;
    910 	char namebuf[16];
    911 	struct wsdisplay_font fd;
    912 
    913 	if (sc->sc_kbddv != NULL) {
    914 		/* check ioctls for keyboard */
    915 #ifdef WSDISPLAY_COMPAT_RAWKBD
    916 		switch (cmd) {
    917 		    case WSKBDIO_SETMODE:
    918 			scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
    919 			return (wsdisplay_update_rawkbd(sc, scr));
    920 		    case WSKBDIO_GETMODE:
    921 			*(int *)data = (scr->scr_rawkbd ?
    922 					WSKBD_RAW : WSKBD_TRANSLATED);
    923 			return (0);
    924 		}
    925 #endif
    926 /* printf("kbdcallback\n"); */
    927 		error = wskbd_displayioctl(sc->sc_kbddv, cmd, data, flag, p);
    928 		if (error >= 0)
    929 			return error;
    930 	}
    931 
    932 /* printf("display\n"); */
    933 	switch (cmd) {
    934 	case WSDISPLAYIO_GMODE:
    935 		*(u_int *)data = (scr->scr_flags & SCR_GRAPHICS ?
    936 				  WSDISPLAYIO_MODE_MAPPED :
    937 				  WSDISPLAYIO_MODE_EMUL);
    938 		return (0);
    939 
    940 	case WSDISPLAYIO_SMODE:
    941 #define d (*(int *)data)
    942 		if (d != WSDISPLAYIO_MODE_EMUL &&
    943 		    d != WSDISPLAYIO_MODE_MAPPED)
    944 			return (EINVAL);
    945 
    946 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
    947 		    scr->scr_flags &= ~SCR_GRAPHICS;
    948 		    if (d == WSDISPLAYIO_MODE_MAPPED)
    949 			    scr->scr_flags |= SCR_GRAPHICS;
    950 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
    951 		    return (EINVAL);
    952 	    return (0);
    953 #undef d
    954 
    955 	case WSDISPLAYIO_USEFONT:
    956 #define d ((struct wsdisplay_usefontdata *)data)
    957 		if (!sc->sc_accessops->load_font)
    958 			return (EINVAL);
    959 		if (d->name) {
    960 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
    961 			if (error)
    962 				return (error);
    963 			fd.name = namebuf;
    964 		} else
    965 			fd.name = 0;
    966 		fd.data = 0;
    967 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
    968 					scr->scr_dconf->emulcookie, &fd);
    969 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
    970 			(*scr->scr_dconf->wsemul->reset)
    971 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
    972 		return (error);
    973 #undef d
    974 	}
    975 
    976 	/* check ioctls for display */
    977 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
    978 	    flag, p));
    979 }
    980 
    981 int
    982 wsdisplay_cfg_ioctl(sc, cmd, data, flag, p)
    983 	struct wsdisplay_softc *sc;
    984 	u_long cmd;
    985 	caddr_t data;
    986 	int flag;
    987 	struct proc *p;
    988 {
    989 	int error;
    990 	char *type, typebuf[16], *emul, emulbuf[16];
    991 	void *buf;
    992 
    993 	switch (cmd) {
    994 	case WSDISPLAYIO_ADDSCREEN:
    995 #define d ((struct wsdisplay_addscreendata *)data)
    996 		if (d->screentype) {
    997 			error = copyinstr(d->screentype, typebuf,
    998 					  sizeof(typebuf), 0);
    999 			if (error)
   1000 				return (error);
   1001 			type = typebuf;
   1002 		} else
   1003 			type = 0;
   1004 		if (d->emul) {
   1005 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf), 0);
   1006 			if (error)
   1007 				return (error);
   1008 			emul = emulbuf;
   1009 		} else
   1010 			emul = 0;
   1011 
   1012 		return (wsdisplay_addscreen(sc, d->idx, type, emul));
   1013 #undef d
   1014 	case WSDISPLAYIO_DELSCREEN:
   1015 #define d ((struct wsdisplay_delscreendata *)data)
   1016 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
   1017 #undef d
   1018 	case WSDISPLAYIO_LDFONT:
   1019 #define d ((struct wsdisplay_font *)data)
   1020 		if (!sc->sc_accessops->load_font)
   1021 			return (EINVAL);
   1022 		if (d->name) {
   1023 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
   1024 			if (error)
   1025 				return (error);
   1026 			d->name = typebuf;
   1027 		} else
   1028 			d->name = "loaded"; /* ??? */
   1029 		buf = malloc(d->fontheight * d->stride * d->numchars,
   1030 			     M_DEVBUF, M_WAITOK);
   1031 		error = copyin(d->data, buf,
   1032 			       d->fontheight * d->stride * d->numchars);
   1033 		if (error) {
   1034 			free(buf, M_DEVBUF);
   1035 			return (error);
   1036 		}
   1037 		d->data = buf;
   1038 		error =
   1039 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
   1040 		free(buf, M_DEVBUF);
   1041 #undef d
   1042 		return (error);
   1043 	}
   1044 	return (EINVAL);
   1045 }
   1046 
   1047 int
   1048 wsdisplaymmap(dev, offset, prot)
   1049 	dev_t dev;
   1050 	int offset;		/* XXX */
   1051 	int prot;
   1052 {
   1053 #if NWSDISPLAY > 0
   1054 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
   1055 	struct wsscreen *scr;
   1056 
   1057 	if (ISWSDISPLAYCTL(dev))
   1058 		return (-1);
   1059 
   1060 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
   1061 
   1062 	if (!(scr->scr_flags & SCR_GRAPHICS))
   1063 		return (-1);
   1064 
   1065 	/* pass mmap to display */
   1066 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
   1067 #else
   1068 	return (-1);
   1069 #endif /* NWSDISPLAY > 0 */
   1070 }
   1071 
   1072 int
   1073 wsdisplaypoll(dev, events, p)
   1074 	dev_t dev;
   1075 	int events;
   1076 	struct proc *p;
   1077 {
   1078 #if NWSDISPLAY > 0
   1079 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
   1080 	struct wsscreen *scr;
   1081 
   1082 	if (ISWSDISPLAYCTL(dev))
   1083 		return (0);
   1084 
   1085 	scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
   1086 
   1087 	if (WSSCREEN_HAS_TTY(scr))
   1088 		return (ttpoll(dev, events, p));
   1089 	else
   1090 		return (0);
   1091 #else
   1092 	return (0);
   1093 #endif /* NWSDISPLAY > 0 */
   1094 }
   1095 
   1096 #if NWSDISPLAY > 0
   1097 void
   1098 wsdisplaystart(tp)
   1099 	register struct tty *tp;
   1100 {
   1101 	struct wsdisplay_softc *sc;
   1102 	struct wsscreen *scr;
   1103 	register int s, n;
   1104 	u_char buf[WSDISPLAYBURST];
   1105 
   1106 	s = spltty();
   1107 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
   1108 		splx(s);
   1109 		return;
   1110 	}
   1111 	sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(tp->t_dev)];
   1112 	scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)];
   1113 	if (scr->scr_hold_screen) {
   1114 		tp->t_state |= TS_TIMEOUT;
   1115 		splx(s);
   1116 		return;
   1117 	}
   1118 	tp->t_state |= TS_BUSY;
   1119 	splx(s);
   1120 
   1121 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
   1122 
   1123 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
   1124 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
   1125 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
   1126 		    buf, n, 0);
   1127 	}
   1128 
   1129 	s = spltty();
   1130 	tp->t_state &= ~TS_BUSY;
   1131 	/* Come back if there's more to do */
   1132 	if (tp->t_outq.c_cc) {
   1133 		tp->t_state |= TS_TIMEOUT;
   1134 		timeout(ttrstrt, tp, (hz > 128) ? (hz / 128) : 1);
   1135 	}
   1136 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1137 		if (tp->t_state&TS_ASLEEP) {
   1138 			tp->t_state &= ~TS_ASLEEP;
   1139 			wakeup((caddr_t)&tp->t_outq);
   1140 		}
   1141 		selwakeup(&tp->t_wsel);
   1142 	}
   1143 	splx(s);
   1144 }
   1145 #endif /* NWSDISPLAY > 0 */
   1146 
   1147 void
   1148 wsdisplaystop(tp, flag)
   1149 	struct tty *tp;
   1150 	int flag;
   1151 {
   1152 	int s;
   1153 
   1154 	s = spltty();
   1155 	if (ISSET(tp->t_state, TS_BUSY))
   1156 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1157 			SET(tp->t_state, TS_FLUSH);
   1158 	splx(s);
   1159 }
   1160 
   1161 #if NWSDISPLAY > 0
   1162 /* Set line parameters. */
   1163 int
   1164 wsdisplayparam(tp, t)
   1165 	struct tty *tp;
   1166 	struct termios *t;
   1167 {
   1168 
   1169 	tp->t_ispeed = t->c_ispeed;
   1170 	tp->t_ospeed = t->c_ospeed;
   1171 	tp->t_cflag = t->c_cflag;
   1172 	return 0;
   1173 }
   1174 #endif /* NWSDISPLAY > 0 */
   1175 
   1176 /*
   1177  * Callbacks for the emulation code.
   1178  */
   1179 void
   1180 wsdisplay_emulbell(v)
   1181 	void *v;
   1182 {
   1183 	struct wsscreen *scr = v;
   1184 
   1185 	if (scr == NULL)		/* console, before real attach */
   1186 		return;
   1187 
   1188 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
   1189 		return;
   1190 
   1191 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
   1192 					FWRITE, NULL);
   1193 }
   1194 
   1195 void
   1196 wsdisplay_emulinput(v, data, count)
   1197 	void *v;
   1198 	const u_char *data;
   1199 	u_int count;
   1200 {
   1201 	struct wsscreen *scr = v;
   1202 	struct tty *tp;
   1203 
   1204 	if (v == NULL)			/* console, before real attach */
   1205 		return;
   1206 
   1207 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
   1208 		return;
   1209 	if (!WSSCREEN_HAS_TTY(scr))
   1210 		return;
   1211 
   1212 	tp = scr->scr_tty;
   1213 	while (count-- > 0)
   1214 		(*linesw[tp->t_line].l_rint)(*data++, tp);
   1215 };
   1216 
   1217 /*
   1218  * Calls from the keyboard interface.
   1219  */
   1220 void
   1221 wsdisplay_kbdinput(dev, ks)
   1222 	struct device *dev;
   1223 	keysym_t ks;
   1224 {
   1225 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1226 	struct wsscreen *scr;
   1227 	char *dp;
   1228 	int count;
   1229 	struct tty *tp;
   1230 
   1231 	KASSERT(sc != NULL);
   1232 
   1233 	scr = sc->sc_focus;
   1234 
   1235 	if (!scr || !WSSCREEN_HAS_TTY(scr))
   1236 		return;
   1237 
   1238 	tp = scr->scr_tty;
   1239 
   1240 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1241 		(*linesw[tp->t_line].l_rint)(KS_VALUE(ks), tp);
   1242 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
   1243 		count = (*scr->scr_dconf->wsemul->translate)
   1244 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
   1245 		while (count-- > 0)
   1246 			(*linesw[tp->t_line].l_rint)(*dp++, tp);
   1247 	}
   1248 }
   1249 
   1250 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1251 int
   1252 wsdisplay_update_rawkbd(sc, scr)
   1253 	struct wsdisplay_softc *sc;
   1254 	struct wsscreen *scr;
   1255 {
   1256 	int s, raw, data, error;
   1257 	s = spltty();
   1258 
   1259 	raw = (scr ? scr->scr_rawkbd : 0);
   1260 
   1261 	if (!sc->sc_kbddv ||
   1262 	    scr != sc->sc_focus ||
   1263 	    sc->sc_rawkbd == raw) {
   1264 		splx(s);
   1265 		return (0);
   1266 	}
   1267 
   1268 	data = (raw ? WSKBD_RAW : WSKBD_TRANSLATED);
   1269 	error = wskbd_displayioctl(sc->sc_kbddv, WSKBDIO_SETMODE,
   1270 				   (caddr_t)&data, 0, 0);
   1271 	if (!error)
   1272 		sc->sc_rawkbd = raw;
   1273 	splx(s);
   1274 	return (error);
   1275 }
   1276 #endif
   1277 
   1278 int
   1279 wsdisplay_switch3(arg, error, waitok)
   1280 	void *arg;
   1281 	int error, waitok;
   1282 {
   1283 	struct wsdisplay_softc *sc = arg;
   1284 	int no;
   1285 	struct wsscreen *scr;
   1286 
   1287 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1288 		printf("wsdisplay_switch3: not switching\n");
   1289 		return (EINVAL);
   1290 	}
   1291 
   1292 	no = sc->sc_screenwanted;
   1293 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1294 		panic("wsdisplay_switch3: invalid screen %d", no);
   1295 	scr = sc->sc_scr[no];
   1296 	if (!scr) {
   1297 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
   1298 		error = ENXIO;
   1299 	}
   1300 
   1301 	if (error) {
   1302 		/* try to recover, avoid recursion */
   1303 
   1304 		if (sc->sc_oldscreen == -1) {
   1305 			printf("wsdisplay_switch3: giving up\n");
   1306 			sc->sc_focus = 0;
   1307 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1308 			wsdisplay_update_rawkbd(sc, 0);
   1309 #endif
   1310 			sc->sc_flags &= ~SC_SWITCHPENDING;
   1311 			return (error);
   1312 		}
   1313 
   1314 		sc->sc_screenwanted = sc->sc_oldscreen;
   1315 		sc->sc_oldscreen = -1;
   1316 		return (wsdisplay_switch1(arg, 0, waitok));
   1317 	}
   1318 
   1319 	sc->sc_flags &= ~SC_SWITCHPENDING;
   1320 
   1321 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
   1322 		wakeup(scr);
   1323 	return (error);
   1324 }
   1325 
   1326 int
   1327 wsdisplay_switch1(arg, error, waitok)
   1328 	void *arg;
   1329 	int error, waitok;
   1330 {
   1331 	struct wsdisplay_softc *sc = arg;
   1332 	int no;
   1333 	struct wsscreen *scr;
   1334 
   1335 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
   1336 		printf("wsdisplay_switch1: not switching\n");
   1337 		return (EINVAL);
   1338 	}
   1339 
   1340 	no = sc->sc_screenwanted;
   1341 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1342 		panic("wsdisplay_switch1: invalid screen %d", no);
   1343 	scr = sc->sc_scr[no];
   1344 	if (!scr) {
   1345 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
   1346 		error = ENXIO;
   1347 	}
   1348 
   1349 	if (error) {
   1350 		sc->sc_flags &= ~SC_SWITCHPENDING;
   1351 		return (error);
   1352 	}
   1353 
   1354 	(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
   1355 					 scr->scr_dconf->emulcookie);
   1356 	sc->sc_focusidx = no;
   1357 	sc->sc_focus = scr;
   1358 
   1359 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1360 	(void) wsdisplay_update_rawkbd(sc, scr);
   1361 #endif
   1362 	/* keyboard map??? */
   1363 
   1364 #define wsswitch_cb3 ((void (*) __P((void *, int, int)))wsdisplay_switch3)
   1365 	if (scr->scr_syncops) {
   1366 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
   1367 						    wsswitch_cb3, sc);
   1368 		if (error == EAGAIN) {
   1369 			/* switch will be done asynchronously */
   1370 			return (0);
   1371 		}
   1372 	}
   1373 
   1374 	return (wsdisplay_switch3(sc, error, waitok));
   1375 }
   1376 
   1377 int
   1378 wsdisplay_switch(dev, no, waitok)
   1379 	struct device *dev;
   1380 	int no, waitok;
   1381 {
   1382 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1383 	int s, res = 0;
   1384 	struct wsscreen *scr;
   1385 
   1386 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN || !sc->sc_scr[no])
   1387 		return (ENXIO);
   1388 
   1389 	s = spltty();
   1390 
   1391 	if (sc->sc_focus && no == sc->sc_focusidx) {
   1392 		splx(s);
   1393 		return (0);
   1394 	}
   1395 
   1396 	if (sc->sc_flags & SC_SWITCHPENDING) {
   1397 		splx(s);
   1398 		return (EBUSY);
   1399 	}
   1400 
   1401 	sc->sc_flags |= SC_SWITCHPENDING;
   1402 	sc->sc_screenwanted = no;
   1403 
   1404 	splx(s);
   1405 
   1406 	scr = sc->sc_focus;
   1407 	if (!scr) {
   1408 		sc->sc_oldscreen = -1;
   1409 		return (wsdisplay_switch1(sc, 0, waitok));
   1410 	} else
   1411 		sc->sc_oldscreen = sc->sc_focusidx;
   1412 
   1413 #define wsswitch_cb1 ((void (*) __P((void *, int, int)))wsdisplay_switch1)
   1414 	if (scr->scr_syncops) {
   1415 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
   1416 						  wsswitch_cb1, sc);
   1417 		if (res == EAGAIN) {
   1418 			/* switch will be done asynchronously */
   1419 			return (0);
   1420 		}
   1421 	} else if (scr->scr_flags & SCR_GRAPHICS) {
   1422 		/* no way to save state */
   1423 		res = EBUSY;
   1424 	}
   1425 
   1426 	return (wsdisplay_switch1(sc, res, waitok));
   1427 }
   1428 
   1429 void
   1430 wsdisplay_reset(dev, op)
   1431 	struct device *dev;
   1432 	enum wsdisplay_resetops op;
   1433 {
   1434 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1435 	struct wsscreen *scr;
   1436 
   1437 	KASSERT(sc != NULL);
   1438 	scr = sc->sc_focus;
   1439 
   1440 	if (!scr)
   1441 		return;
   1442 
   1443 	switch (op) {
   1444 	case WSDISPLAY_RESETEMUL:
   1445 		if (!WSSCREEN_HAS_EMULATOR(scr))
   1446 			break;
   1447 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
   1448 						 WSEMUL_RESET);
   1449 		break;
   1450 	case WSDISPLAY_RESETCLOSE:
   1451 		wsdisplay_closescreen(sc, scr);
   1452 		break;
   1453 	}
   1454 }
   1455 
   1456 /*
   1457  * Interface for (external) VT switch / process synchronization code
   1458  */
   1459 int
   1460 wsscreen_attach_sync(scr, ops, cookie)
   1461 	struct wsscreen *scr;
   1462 	const struct wscons_syncops *ops;
   1463 	void *cookie;
   1464 {
   1465 	if (scr->scr_syncops) {
   1466 		/*
   1467 		 * The screen is already claimed.
   1468 		 * Check if the owner is still alive.
   1469 		 */
   1470 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
   1471 			return (EBUSY);
   1472 	}
   1473 	scr->scr_syncops = ops;
   1474 	scr->scr_synccookie = cookie;
   1475 	return (0);
   1476 }
   1477 
   1478 int
   1479 wsscreen_detach_sync(scr)
   1480 	struct wsscreen *scr;
   1481 {
   1482 	if (!scr->scr_syncops)
   1483 		return (EINVAL);
   1484 	scr->scr_syncops = 0;
   1485 	return (0);
   1486 }
   1487 
   1488 int
   1489 wsscreen_lookup_sync(scr, ops, cookiep)
   1490 	struct wsscreen *scr;
   1491 	const struct wscons_syncops *ops; /* used as ID */
   1492 	void **cookiep;
   1493 {
   1494 	if (!scr->scr_syncops || ops != scr->scr_syncops)
   1495 		return (EINVAL);
   1496 	*cookiep = scr->scr_synccookie;
   1497 	return (0);
   1498 }
   1499 
   1500 /*
   1501  * Interface to virtual screen stuff
   1502  */
   1503 int
   1504 wsdisplay_maxscreenidx(sc)
   1505 	struct wsdisplay_softc *sc;
   1506 {
   1507 	return (WSDISPLAY_MAXSCREEN - 1);
   1508 }
   1509 
   1510 int
   1511 wsdisplay_screenstate(sc, idx)
   1512 	struct wsdisplay_softc *sc;
   1513 	int idx;
   1514 {
   1515 	if (idx >= WSDISPLAY_MAXSCREEN)
   1516 		return (EINVAL);
   1517 	if (!sc->sc_scr[idx])
   1518 		return (ENXIO);
   1519 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
   1520 }
   1521 
   1522 int
   1523 wsdisplay_getactivescreen(sc)
   1524 	struct wsdisplay_softc *sc;
   1525 {
   1526 	return (sc->sc_focusidx);
   1527 }
   1528 
   1529 int
   1530 wsscreen_switchwait(sc, no)
   1531 	struct wsdisplay_softc *sc;
   1532 	int no;
   1533 {
   1534 	struct wsscreen *scr;
   1535 	int s, res = 0;
   1536 
   1537 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
   1538 		return (ENXIO);
   1539 	scr = sc->sc_scr[no];
   1540 	if (!scr)
   1541 		return (ENXIO);
   1542 
   1543 	s = spltty();
   1544 	if (scr != sc->sc_focus) {
   1545 		scr->scr_flags |= SCR_WAITACTIVE;
   1546 		res = tsleep(scr, PCATCH, "wswait", 0);
   1547 		if (scr != sc->sc_scr[no])
   1548 			res = ENXIO; /* disappeared in the meantime */
   1549 		else
   1550 			scr->scr_flags &= ~SCR_WAITACTIVE;
   1551 	}
   1552 	splx(s);
   1553 	return (res);
   1554 }
   1555 
   1556 void
   1557 wsdisplay_kbdholdscreen(dev, hold)
   1558 	struct device *dev;
   1559 	int hold;
   1560 {
   1561 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
   1562 	struct wsscreen *scr;
   1563 
   1564 	scr = sc->sc_focus;
   1565 
   1566 	if (hold)
   1567 		scr->scr_hold_screen = 1;
   1568 	else {
   1569 		scr->scr_hold_screen = 0;
   1570 		timeout(ttrstrt, scr->scr_tty, 0);	/* "immediate" */
   1571 	}
   1572 }
   1573 
   1574 /*
   1575  * Calls from the glue code.
   1576  */
   1577 int
   1578 wsdisplay_is_console(dv)
   1579 	struct device *dv;
   1580 {
   1581 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1582 
   1583 	KASSERT(sc != NULL);
   1584 	return (sc->sc_isconsole);
   1585 }
   1586 
   1587 struct device *
   1588 wsdisplay_kbd(dv)
   1589 	struct device *dv;
   1590 {
   1591 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1592 
   1593 	KASSERT(sc != NULL);
   1594 	return (sc->sc_kbddv);
   1595 }
   1596 
   1597 void
   1598 wsdisplay_set_kbd(dv, kbddv)
   1599 	struct device *dv, *kbddv;
   1600 {
   1601 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
   1602 
   1603 	KASSERT(sc != NULL);
   1604 	if (sc->sc_kbddv) {
   1605 		/* disable old keyboard */
   1606 		wskbd_enable(sc->sc_kbddv, 0);
   1607 	}
   1608 	if (kbddv) {
   1609 		/* enable new keyboard */
   1610 		wskbd_enable(kbddv, 1);
   1611 	}
   1612 	sc->sc_kbddv = kbddv;
   1613 }
   1614 
   1615 /*
   1616  * Console interface.
   1617  */
   1618 void
   1619 wsdisplay_cnputc(dev, i)
   1620 	dev_t dev;
   1621 	int i;
   1622 {
   1623 	struct wsscreen_internal *dc;
   1624 	char c = i;
   1625 
   1626 	if (!wsdisplay_console_initted)
   1627 		return;
   1628 
   1629 	if (wsdisplay_console_device != NULL &&
   1630 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
   1631 		return;
   1632 
   1633 	dc = &wsdisplay_console_conf;
   1634 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
   1635 }
   1636 
   1637 static int
   1638 wsdisplay_getc_dummy(dev)
   1639 	dev_t dev;
   1640 {
   1641 	/* panic? */
   1642 	return (0);
   1643 }
   1644 
   1645 static void
   1646 wsdisplay_pollc_dummy(dev, on)
   1647 	dev_t dev;
   1648 	int on;
   1649 {
   1650 }
   1651 
   1652 void
   1653 wsdisplay_set_cons_kbd(get, poll)
   1654 	int (*get) __P((dev_t));
   1655 	void (*poll) __P((dev_t, int));
   1656 {
   1657 	wsdisplay_cons.cn_getc = get;
   1658 	wsdisplay_cons.cn_pollc = poll;
   1659 }
   1660