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