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