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