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