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