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