xref: /netbsd-src/sys/dev/wscons/wsdisplay.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /* $NetBSD: wsdisplay.c,v 1.111 2007/10/18 21:08:18 joerg Exp $ */
2 
3 /*
4  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: wsdisplay.c,v 1.111 2007/10/18 21:08:18 joerg Exp $");
35 
36 #include "opt_wsdisplay_compat.h"
37 #include "opt_wsmsgattrs.h"
38 #include "opt_compat_netbsd.h"
39 #include "wskbd.h"
40 #include "wsmux.h"
41 #include "wsdisplay.h"
42 
43 #include <sys/param.h>
44 #include <sys/conf.h>
45 #include <sys/device.h>
46 #include <sys/ioctl.h>
47 #include <sys/poll.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/malloc.h>
51 #include <sys/syslog.h>
52 #include <sys/systm.h>
53 #include <sys/tty.h>
54 #include <sys/signalvar.h>
55 #include <sys/errno.h>
56 #include <sys/fcntl.h>
57 #include <sys/vnode.h>
58 #include <sys/kauth.h>
59 
60 #include <dev/wscons/wseventvar.h>
61 #include <dev/wscons/wsmuxvar.h>
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/wscons/wsdisplayvar.h>
64 #include <dev/wscons/wsksymvar.h>
65 #include <dev/wscons/wsksymdef.h>
66 #include <dev/wscons/wsemulvar.h>
67 #include <dev/wscons/wscons_callbacks.h>
68 #include <dev/cons.h>
69 
70 struct wsscreen_internal {
71 	const struct wsdisplay_emulops *emulops;
72 	void	*emulcookie;
73 
74 	const struct wsscreen_descr *scrdata;
75 
76 	const struct wsemul_ops *wsemul;
77 	void	*wsemulcookie;
78 };
79 
80 struct wsscreen {
81 	struct wsscreen_internal *scr_dconf;
82 
83 	struct tty *scr_tty;
84 	int	scr_hold_screen;		/* hold tty output */
85 
86 	int scr_flags;
87 #define SCR_OPEN 1		/* is it open? */
88 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
89 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
90 #define	SCR_DUMBFB 8		/* in use as a dumb fb (iff SCR_GRAPHICS) */
91 	const struct wscons_syncops *scr_syncops;
92 	void *scr_synccookie;
93 
94 #ifdef WSDISPLAY_COMPAT_RAWKBD
95 	int scr_rawkbd;
96 #endif
97 
98 	struct wsdisplay_softc *sc;
99 };
100 
101 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int,
102 				 const char *,
103 				 const struct wsscreen_descr *, void *,
104 				 int, int, long);
105 void wsscreen_detach(struct wsscreen *);
106 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
107 static void wsdisplay_shutdownhook(void *);
108 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
109 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
110 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
111 
112 #define WSDISPLAY_MAXSCREEN 8
113 
114 struct wsdisplay_softc {
115 	device_t sc_dev;
116 
117 	const struct wsdisplay_accessops *sc_accessops;
118 	void	*sc_accesscookie;
119 
120 	const struct wsscreen_list *sc_scrdata;
121 #ifdef WSDISPLAY_SCROLLSUPPORT
122 	struct wsdisplay_scroll_data sc_scroll_values;
123 #endif
124 
125 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
126 	int sc_focusidx;	/* available only if sc_focus isn't null */
127 	struct wsscreen *sc_focus;
128 
129 	struct wseventvar evar;
130 
131 	int	sc_isconsole;
132 
133 	int sc_flags;
134 #define SC_SWITCHPENDING 1
135 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
136 
137 #if NWSKBD > 0
138 	struct wsevsrc *sc_input;
139 #ifdef WSDISPLAY_COMPAT_RAWKBD
140 	int sc_rawkbd;
141 #endif
142 #endif /* NWSKBD > 0 */
143 };
144 
145 #ifdef WSDISPLAY_SCROLLSUPPORT
146 
147 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
148 	WSDISPLAY_SCROLL_DOALL,
149 	25,
150 	2,
151 };
152 #endif
153 
154 extern struct cfdriver wsdisplay_cd;
155 
156 /* Autoconfiguration definitions. */
157 static int wsdisplay_emul_match(device_t , struct cfdata *, void *);
158 static void wsdisplay_emul_attach(device_t, device_t, void *);
159 static int wsdisplay_noemul_match(device_t, struct cfdata *, void *);
160 static void wsdisplay_noemul_attach(device_t, device_t, void *);
161 
162 CFATTACH_DECL_NEW(wsdisplay_emul, sizeof (struct wsdisplay_softc),
163     wsdisplay_emul_match, wsdisplay_emul_attach, NULL, NULL);
164 
165 CFATTACH_DECL_NEW(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
166     wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
167 
168 dev_type_open(wsdisplayopen);
169 dev_type_close(wsdisplayclose);
170 dev_type_read(wsdisplayread);
171 dev_type_write(wsdisplaywrite);
172 dev_type_ioctl(wsdisplayioctl);
173 dev_type_stop(wsdisplaystop);
174 dev_type_tty(wsdisplaytty);
175 dev_type_poll(wsdisplaypoll);
176 dev_type_mmap(wsdisplaymmap);
177 dev_type_kqfilter(wsdisplaykqfilter);
178 
179 const struct cdevsw wsdisplay_cdevsw = {
180 	wsdisplayopen, wsdisplayclose, wsdisplayread, wsdisplaywrite,
181 	wsdisplayioctl, wsdisplaystop, wsdisplaytty, wsdisplaypoll,
182 	wsdisplaymmap, wsdisplaykqfilter, D_TTY
183 };
184 
185 static void wsdisplaystart(struct tty *);
186 static int wsdisplayparam(struct tty *, struct termios *);
187 
188 
189 #define	WSDISPLAYUNIT(dev)	(minor(dev) >> 8)
190 #define	WSDISPLAYSCREEN(dev)	(minor(dev) & 0xff)
191 #define ISWSDISPLAYSTAT(dev)	(WSDISPLAYSCREEN(dev) == 254)
192 #define ISWSDISPLAYCTL(dev)	(WSDISPLAYSCREEN(dev) == 255)
193 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
194 
195 #define	WSSCREEN_HAS_EMULATOR(scr)	((scr)->scr_dconf->wsemul != NULL)
196 #define	WSSCREEN_HAS_TTY(scr)	((scr)->scr_tty != NULL)
197 
198 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
199 	    int console, int kbdmux, const struct wsscreen_list *,
200 	    const struct wsdisplay_accessops *accessops,
201 	    void *accesscookie);
202 
203 #ifdef WSDISPLAY_COMPAT_RAWKBD
204 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
205 				 struct wsscreen *);
206 #endif
207 
208 static int wsdisplay_console_initted;
209 static int wsdisplay_console_attached;
210 static struct wsdisplay_softc *wsdisplay_console_device;
211 static struct wsscreen_internal wsdisplay_console_conf;
212 
213 static int wsdisplay_getc_dummy(dev_t);
214 static void wsdisplay_pollc(dev_t, int);
215 
216 static int wsdisplay_cons_pollmode;
217 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
218 
219 static struct consdev wsdisplay_cons = {
220 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
221 	wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
222 };
223 
224 #ifndef WSDISPLAY_DEFAULTSCREENS
225 # define WSDISPLAY_DEFAULTSCREENS	0
226 #endif
227 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
228 
229 static int wsdisplay_switch1(device_t, int, int);
230 static void wsdisplay_switch1_cb(void *, int, int);
231 static int wsdisplay_switch2(device_t, int, int);
232 static void wsdisplay_switch2_cb(void *, int, int);
233 static int wsdisplay_switch3(device_t, int, int);
234 static void wsdisplay_switch3_cb(void *, int, int);
235 
236 int wsdisplay_clearonclose;
237 
238 struct wsscreen *
239 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
240 	const struct wsscreen_descr *type, void *cookie, int ccol,
241 	int crow, long defattr)
242 {
243 	struct wsscreen_internal *dconf;
244 	struct wsscreen *scr;
245 
246 	scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
247 	if (!scr)
248 		return (NULL);
249 
250 	if (console) {
251 		dconf = &wsdisplay_console_conf;
252 		/*
253 		 * If there's an emulation, tell it about the callback argument.
254 		 * The other stuff is already there.
255 		 */
256 		if (dconf->wsemul != NULL)
257 			(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
258 	} else { /* not console */
259 		dconf = malloc(sizeof(struct wsscreen_internal),
260 			       M_DEVBUF, M_NOWAIT);
261 		dconf->emulops = type->textops;
262 		dconf->emulcookie = cookie;
263 		if (dconf->emulops) {
264 			dconf->wsemul = wsemul_pick(emul);
265 			if (dconf->wsemul == NULL) {
266 				free(dconf, M_DEVBUF);
267 				free(scr, M_DEVBUF);
268 				return (NULL);
269 			}
270 			dconf->wsemulcookie =
271 			  (*dconf->wsemul->attach)(0, type, cookie,
272 						   ccol, crow, scr, defattr);
273 		} else
274 			dconf->wsemul = NULL;
275 		dconf->scrdata = type;
276 	}
277 
278 	scr->scr_dconf = dconf;
279 
280 	scr->scr_tty = ttymalloc();
281 	tty_attach(scr->scr_tty);
282 	scr->scr_hold_screen = 0;
283 	if (WSSCREEN_HAS_EMULATOR(scr))
284 		scr->scr_flags = 0;
285 	else
286 		scr->scr_flags = SCR_GRAPHICS;
287 
288 	scr->scr_syncops = 0;
289 	scr->sc = sc;
290 #ifdef WSDISPLAY_COMPAT_RAWKBD
291 	scr->scr_rawkbd = 0;
292 #endif
293 	return (scr);
294 }
295 
296 void
297 wsscreen_detach(struct wsscreen *scr)
298 {
299 	u_int ccol, crow; /* XXX */
300 
301 	if (WSSCREEN_HAS_TTY(scr)) {
302 		tty_detach(scr->scr_tty);
303 		ttyfree(scr->scr_tty);
304 	}
305 	if (WSSCREEN_HAS_EMULATOR(scr))
306 		(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
307 						  &ccol, &crow);
308 	free(scr->scr_dconf, M_DEVBUF);
309 	free(scr, M_DEVBUF);
310 }
311 
312 const struct wsscreen_descr *
313 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
314 {
315 	int i;
316 	const struct wsscreen_descr *scr;
317 
318 	KASSERT(scrdata->nscreens > 0);
319 
320 	if (name == NULL)
321 		return (scrdata->screens[0]);
322 
323 	for (i = 0; i < scrdata->nscreens; i++) {
324 		scr = scrdata->screens[i];
325 		if (!strcmp(name, scr->name))
326 			return (scr);
327 	}
328 
329 	return (0);
330 }
331 
332 /*
333  * print info about attached screen
334  */
335 static void
336 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
337 {
338 	aprint_verbose_dev(sc->sc_dev, "screen %d", idx);
339 	if (count > 1)
340 		aprint_verbose("-%d", idx + (count-1));
341 	aprint_verbose(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
342 	if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
343 		aprint_verbose(", %s emulation",
344 			sc->sc_scr[idx]->scr_dconf->wsemul->name);
345 	}
346 	aprint_verbose(")\n");
347 }
348 
349 int
350 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
351 	const char *screentype, const char *emul)
352 {
353 	const struct wsscreen_descr *scrdesc;
354 	int error;
355 	void *cookie;
356 	int ccol, crow;
357 	long defattr;
358 	struct wsscreen *scr;
359 	int s;
360 
361 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
362 		return (EINVAL);
363 	if (sc->sc_scr[idx] != NULL)
364 		return (EBUSY);
365 
366 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
367 	if (!scrdesc)
368 		return (ENXIO);
369 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
370 			scrdesc, &cookie, &ccol, &crow, &defattr);
371 	if (error)
372 		return (error);
373 
374 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
375 			      cookie, ccol, crow, defattr);
376 	if (scr == NULL) {
377 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
378 						 cookie);
379 		return (ENXIO);
380 	}
381 
382 	sc->sc_scr[idx] = scr;
383 
384 	/* if no screen has focus yet, activate the first we get */
385 	s = spltty();
386 	if (!sc->sc_focus) {
387 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
388 						 scr->scr_dconf->emulcookie,
389 						 0, 0, 0);
390 		sc->sc_focusidx = idx;
391 		sc->sc_focus = scr;
392 	}
393 	splx(s);
394 	return (0);
395 }
396 
397 static void
398 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
399 {
400 	int maj, mn, idx;
401 
402 	/* hangup */
403 	if (WSSCREEN_HAS_TTY(scr)) {
404 		struct tty *tp = scr->scr_tty;
405 		(*tp->t_linesw->l_modem)(tp, 0);
406 	}
407 
408 	/* locate the major number */
409 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
410 	/* locate the screen index */
411 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
412 		if (scr == sc->sc_scr[idx])
413 			break;
414 #ifdef DIAGNOSTIC
415 	if (idx == WSDISPLAY_MAXSCREEN)
416 		panic("wsdisplay_forceclose: bad screen");
417 #endif
418 
419 	/* nuke the vnodes */
420 	mn = WSDISPLAYMINOR(device_unit(sc->sc_dev), idx);
421 	vdevgone(maj, mn, mn, VCHR);
422 }
423 
424 #ifdef WSDISPLAY_SCROLLSUPPORT
425 void
426 wsdisplay_scroll(void *arg, int op)
427 {
428 	device_t dv = arg;
429 	struct wsdisplay_softc *sc = device_private(dv);
430 	struct wsscreen *scr;
431 	int lines;
432 
433 	scr = sc->sc_focus;
434 
435 	if (!scr)
436 		return;
437 
438 	if (op == WSDISPLAY_SCROLL_RESET)
439 		lines = 0;
440 	else {
441 		lines = (op & WSDISPLAY_SCROLL_LOW) ?
442 			sc->sc_scroll_values.slowlines :
443 			sc->sc_scroll_values.fastlines;
444 		if (op & WSDISPLAY_SCROLL_BACKWARD)
445 			lines = -(lines);
446 	}
447 
448 	if (sc->sc_accessops->scroll) {
449 		(*sc->sc_accessops->scroll)(sc->sc_accesscookie,
450 		    sc->sc_focus->scr_dconf->emulcookie, lines);
451 	}
452 }
453 #endif
454 
455 int
456 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
457 {
458 	struct wsscreen *scr;
459 	int s;
460 	void *cookie;
461 
462 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
463 		return (EINVAL);
464 	if ((scr = sc->sc_scr[idx]) == NULL)
465 		return (ENXIO);
466 
467 	if (scr->scr_dconf == &wsdisplay_console_conf ||
468 	    scr->scr_syncops ||
469 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
470 		return(EBUSY);
471 
472 	wsdisplay_closescreen(sc, scr);
473 
474 	/*
475 	 * delete pointers, so neither device entries
476 	 * nor keyboard input can reference it anymore
477 	 */
478 	s = spltty();
479 	if (sc->sc_focus == scr) {
480 		sc->sc_focus = 0;
481 #ifdef WSDISPLAY_COMPAT_RAWKBD
482 		wsdisplay_update_rawkbd(sc, 0);
483 #endif
484 	}
485 	sc->sc_scr[idx] = 0;
486 	splx(s);
487 
488 	/*
489 	 * Wake up processes waiting for the screen to
490 	 * be activated. Sleepers must check whether
491 	 * the screen still exists.
492 	 */
493 	if (scr->scr_flags & SCR_WAITACTIVE)
494 		wakeup(scr);
495 
496 	/* save a reference to the graphics screen */
497 	cookie = scr->scr_dconf->emulcookie;
498 
499 	wsscreen_detach(scr);
500 
501 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
502 					 cookie);
503 
504 	aprint_verbose_dev(sc->sc_dev, "screen %d deleted\n", idx);
505 	return (0);
506 }
507 
508 /*
509  * Autoconfiguration functions.
510  */
511 int
512 wsdisplay_emul_match(device_t parent, struct cfdata *match, void *aux)
513 {
514 	struct wsemuldisplaydev_attach_args *ap = aux;
515 
516 	if (match->wsemuldisplaydevcf_console !=
517 	    WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
518 		/*
519 		 * If console-ness of device specified, either match
520 		 * exactly (at high priority), or fail.
521 		 */
522 		if (match->wsemuldisplaydevcf_console != 0 &&
523 		    ap->console != 0)
524 			return (10);
525 		else
526 			return (0);
527 	}
528 
529 	/* If console-ness unspecified, it wins. */
530 	return (1);
531 }
532 
533 void
534 wsdisplay_emul_attach(device_t parent, device_t self, void *aux)
535 {
536 	struct wsdisplay_softc *sc = device_private(self);
537 	struct wsemuldisplaydev_attach_args *ap = aux;
538 
539 	sc->sc_dev = self;
540 
541 	/* Don't allow more than one console to attach */
542 	if (wsdisplay_console_attached && ap->console)
543 		ap->console = 0;
544 
545 	wsdisplay_common_attach(sc, ap->console,
546 	     device_cfdata(self)->wsemuldisplaydevcf_kbdmux, ap->scrdata,
547 	     ap->accessops, ap->accesscookie);
548 
549 	if (ap->console) {
550 		int maj;
551 
552 		/* locate the major number */
553 		maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
554 
555 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
556 					 0));
557 	}
558 }
559 
560 /* Print function (for parent devices). */
561 int
562 wsemuldisplaydevprint(void *aux, const char *pnp)
563 {
564 #if 0 /* -Wunused */
565 	struct wsemuldisplaydev_attach_args *ap = aux;
566 #endif
567 
568 	if (pnp)
569 		aprint_normal("wsdisplay at %s", pnp);
570 #if 0 /* don't bother; it's ugly */
571 	aprint_normal(" console %d", ap->console);
572 #endif
573 
574 	return (UNCONF);
575 }
576 
577 int
578 wsdisplay_noemul_match(device_t parent, struct cfdata *match, void *aux)
579 {
580 #if 0 /* -Wunused */
581 	struct wsdisplaydev_attach_args *ap = aux;
582 #endif
583 
584 	/* Always match. */
585 	return (1);
586 }
587 
588 void
589 wsdisplay_noemul_attach(device_t parent, device_t self, void *aux)
590 {
591 	struct wsdisplay_softc *sc = device_private(self);
592 	struct wsdisplaydev_attach_args *ap = aux;
593 
594 	sc->sc_dev = self;
595 
596 	wsdisplay_common_attach(sc, 0,
597 	    device_cfdata(self)->wsemuldisplaydevcf_kbdmux, NULL,
598 	    ap->accessops, ap->accesscookie);
599 }
600 
601 /* Print function (for parent devices). */
602 int
603 wsdisplaydevprint(void *aux, const char *pnp)
604 {
605 #if 0 /* -Wunused */
606 	struct wsdisplaydev_attach_args *ap = aux;
607 #endif
608 
609 	if (pnp)
610 		aprint_normal("wsdisplay at %s", pnp);
611 
612 	return (UNCONF);
613 }
614 
615 static void
616 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
617 	const struct wsscreen_list *scrdata,
618 	const struct wsdisplay_accessops *accessops,
619 	void *accesscookie)
620 {
621 	static int hookset;
622 	int i, start=0;
623 #if NWSKBD > 0
624 	struct wsevsrc *kme;
625 #if NWSMUX > 0
626 	struct wsmux_softc *mux;
627 
628 	if (kbdmux >= 0)
629 		mux = wsmux_getmux(kbdmux);
630 	else
631 		mux = wsmux_create("dmux", device_unit(sc->sc_dev));
632 	/* XXX panic()ing isn't nice, but attach cannot fail */
633 	if (mux == NULL)
634 		panic("wsdisplay_common_attach: no memory");
635 	sc->sc_input = &mux->sc_base;
636 	mux->sc_base.me_dispdv = sc->sc_dev;
637 	aprint_normal(" kbdmux %d", kbdmux);
638 #else
639 	if (kbdmux >= 0)
640 		aprint_normal(" (kbdmux ignored)");
641 #endif
642 #endif
643 
644 	sc->sc_isconsole = console;
645 
646 	if (console) {
647 		KASSERT(wsdisplay_console_initted);
648 		KASSERT(wsdisplay_console_device == NULL);
649 
650 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
651 		wsdisplay_console_device = sc;
652 
653 		aprint_normal(": console (%s, %s emulation)",
654 		       wsdisplay_console_conf.scrdata->name,
655 		       wsdisplay_console_conf.wsemul->name);
656 
657 #if NWSKBD > 0
658 		kme = wskbd_set_console_display(sc->sc_dev, sc->sc_input);
659 		if (kme != NULL)
660 			aprint_normal(", using %s", kme->me_dv.dv_xname);
661 #if NWSMUX == 0
662 		sc->sc_input = kme;
663 #endif
664 #endif
665 
666 		sc->sc_focusidx = 0;
667 		sc->sc_focus = sc->sc_scr[0];
668 		start = 1;
669 
670 		wsdisplay_console_attached = 1;
671 	}
672 	aprint_normal("\n");
673 
674 #if NWSKBD > 0 && NWSMUX > 0
675 	wsmux_set_display(mux, sc->sc_dev);
676 #endif
677 
678 	sc->sc_accessops = accessops;
679 	sc->sc_accesscookie = accesscookie;
680 	sc->sc_scrdata = scrdata;
681 
682 #ifdef WSDISPLAY_SCROLLSUPPORT
683 	sc->sc_scroll_values = wsdisplay_default_scroll_values;
684 #endif
685 
686 	/*
687 	 * Set up a number of virtual screens if wanted. The
688 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
689 	 * is for special cases like installation kernels.
690 	 */
691 	for (i = start; i < wsdisplay_defaultscreens; i++) {
692 		if (wsdisplay_addscreen(sc, i, 0, 0))
693 			break;
694 	}
695 
696 	if (i > start)
697 		wsdisplay_addscreen_print(sc, start, i-start);
698 
699 	if (hookset == 0)
700 		shutdownhook_establish(wsdisplay_shutdownhook, NULL);
701 	hookset = 1;
702 }
703 
704 void
705 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
706 	int ccol, int crow, long defattr)
707 {
708 	const struct wsemul_ops *wsemul;
709 
710 	KASSERT(wsdisplay_console_initted < 2);
711 	KASSERT(type->nrows > 0);
712 	KASSERT(type->ncols > 0);
713 	KASSERT(crow < type->nrows);
714 	KASSERT(ccol < type->ncols);
715 
716 	wsdisplay_console_conf.emulops = type->textops;
717 	wsdisplay_console_conf.emulcookie = cookie;
718 	wsdisplay_console_conf.scrdata = type;
719 
720 	wsemul = wsemul_pick(0); /* default */
721 	wsdisplay_console_conf.wsemul = wsemul;
722 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
723 								  ccol, crow,
724 								  defattr);
725 
726 	cn_tab = &wsdisplay_cons;
727 	wsdisplay_console_initted = 2;
728 }
729 
730 void
731 wsdisplay_preattach(const struct wsscreen_descr *type, void *cookie,
732 	int ccol, int crow, long defattr)
733 {
734 	const struct wsemul_ops *wsemul;
735 
736 	KASSERT(!wsdisplay_console_initted);
737 	KASSERT(type->nrows > 0);
738 	KASSERT(type->ncols > 0);
739 	KASSERT(crow < type->nrows);
740 	KASSERT(ccol < type->ncols);
741 
742 	wsdisplay_console_conf.emulops = type->textops;
743 	wsdisplay_console_conf.emulcookie = cookie;
744 	wsdisplay_console_conf.scrdata = type;
745 
746 	wsemul = wsemul_pick(0); /* default */
747 	wsdisplay_console_conf.wsemul = wsemul;
748 	wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
749 								  ccol, crow,
750 								  defattr);
751 
752 	cn_tab = &wsdisplay_cons;
753 	wsdisplay_console_initted = 1;
754 }
755 
756 /*
757  * Tty and cdevsw functions.
758  */
759 int
760 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
761 {
762 	device_t dv;
763 	struct wsdisplay_softc *sc;
764 	struct tty *tp;
765 	int newopen, error;
766 	struct wsscreen *scr;
767 
768 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
769 	if (dv == NULL)			/* make sure it was attached */
770 		return (ENXIO);
771 
772 	sc = device_private(dv);
773 
774 	if (ISWSDISPLAYSTAT(dev)) {
775 		wsevent_init(&sc->evar, l->l_proc);
776 		return (0);
777 	}
778 
779 	if (ISWSDISPLAYCTL(dev))
780 		return (0);
781 
782 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
783 		return (ENXIO);
784 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
785 		return (ENXIO);
786 
787 	if (WSSCREEN_HAS_TTY(scr)) {
788 		tp = scr->scr_tty;
789 		tp->t_oproc = wsdisplaystart;
790 		tp->t_param = wsdisplayparam;
791 		tp->t_dev = dev;
792 		newopen = (tp->t_state & TS_ISOPEN) == 0;
793 
794 		if (kauth_authorize_device_tty(l->l_cred,
795 			KAUTH_DEVICE_TTY_OPEN, tp))
796 			return (EBUSY);
797 
798 		if (newopen) {
799 			ttychars(tp);
800 			tp->t_iflag = TTYDEF_IFLAG;
801 			tp->t_oflag = TTYDEF_OFLAG;
802 			tp->t_cflag = TTYDEF_CFLAG;
803 			tp->t_lflag = TTYDEF_LFLAG;
804 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
805 			wsdisplayparam(tp, &tp->t_termios);
806 			ttsetwater(tp);
807 		}
808 		tp->t_state |= TS_CARR_ON;
809 
810 		error = ((*tp->t_linesw->l_open)(dev, tp));
811 		if (error)
812 			return (error);
813 
814 		if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
815 			/* set window sizes as appropriate, and reset
816 			 the emulation */
817 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
818 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
819 
820 			/* wsdisplay_set_emulation() */
821 		}
822 	}
823 
824 	scr->scr_flags |= SCR_OPEN;
825 	return (0);
826 }
827 
828 int
829 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
830 {
831 	device_t dv;
832 	struct wsdisplay_softc *sc;
833 	struct tty *tp;
834 	struct wsscreen *scr;
835 
836 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
837 	sc = device_private(dv);
838 
839 	if (ISWSDISPLAYSTAT(dev)) {
840 		wsevent_fini(&sc->evar);
841 		return (0);
842 	}
843 
844 	if (ISWSDISPLAYCTL(dev))
845 		return (0);
846 
847 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
848 		return (0);
849 
850 	if (WSSCREEN_HAS_TTY(scr)) {
851 		if (scr->scr_hold_screen) {
852 			int s;
853 
854 			/* XXX RESET KEYBOARD LEDS, etc. */
855 			s = spltty();	/* avoid conflict with keyboard */
856 			wsdisplay_kbdholdscreen(dv, 0);
857 			splx(s);
858 		}
859 		tp = scr->scr_tty;
860 		(*tp->t_linesw->l_close)(tp, flag);
861 		ttyclose(tp);
862 	}
863 
864 	if (scr->scr_syncops)
865 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
866 
867 	if (WSSCREEN_HAS_EMULATOR(scr)) {
868 		scr->scr_flags &= ~SCR_GRAPHICS;
869 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
870 						 WSEMUL_RESET);
871 		if (wsdisplay_clearonclose)
872 			(*scr->scr_dconf->wsemul->reset)
873 				(scr->scr_dconf->wsemulcookie,
874 				 WSEMUL_CLEARSCREEN);
875 	}
876 
877 #ifdef WSDISPLAY_COMPAT_RAWKBD
878 	if (scr->scr_rawkbd) {
879 		int kbmode = WSKBD_TRANSLATED;
880 		(void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
881 					       (void *)&kbmode, 0, l);
882 	}
883 #endif
884 
885 	scr->scr_flags &= ~SCR_OPEN;
886 
887 	return (0);
888 }
889 
890 int
891 wsdisplayread(dev_t dev, struct uio *uio, int flag)
892 {
893 	device_t dv;
894 	struct wsdisplay_softc *sc;
895 	struct tty *tp;
896 	struct wsscreen *scr;
897 	int error;
898 
899 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
900 	sc = device_private(dv);
901 
902 	if (ISWSDISPLAYSTAT(dev)) {
903 		error = wsevent_read(&sc->evar, uio, flag);
904 		return (error);
905 	}
906 
907 	if (ISWSDISPLAYCTL(dev))
908 		return (0);
909 
910 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
911 		return (ENXIO);
912 
913 	if (!WSSCREEN_HAS_TTY(scr))
914 		return (ENODEV);
915 
916 	tp = scr->scr_tty;
917 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
918 }
919 
920 int
921 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
922 {
923 	device_t dv;
924 	struct wsdisplay_softc *sc;
925 	struct tty *tp;
926 	struct wsscreen *scr;
927 
928 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
929 	sc = device_private(dv);
930 
931 	if (ISWSDISPLAYSTAT(dev)) {
932 		return (0);
933 	}
934 
935 	if (ISWSDISPLAYCTL(dev))
936 		return (0);
937 
938 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
939 		return (ENXIO);
940 
941 	if (!WSSCREEN_HAS_TTY(scr))
942 		return (ENODEV);
943 
944 	tp = scr->scr_tty;
945 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
946 }
947 
948 int
949 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
950 {
951 	device_t dv;
952 	struct wsdisplay_softc *sc;
953 	struct tty *tp;
954 	struct wsscreen *scr;
955 
956 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
957 	sc = device_private(dv);
958 
959 	if (ISWSDISPLAYSTAT(dev))
960 		return (wsevent_poll(&sc->evar, events, l));
961 
962 	if (ISWSDISPLAYCTL(dev))
963 		return (0);
964 
965 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
966 		return (POLLHUP);
967 
968 	if (!WSSCREEN_HAS_TTY(scr))
969 		return (POLLERR);
970 
971 	tp = scr->scr_tty;
972 	return ((*tp->t_linesw->l_poll)(tp, events, l));
973 }
974 
975 int
976 wsdisplaykqfilter(dev_t dev, struct knote *kn)
977 {
978 	device_t dv;
979 	struct wsdisplay_softc *sc;
980 	struct wsscreen *scr;
981 
982 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
983 	sc = device_private(dv);
984 
985 	if (ISWSDISPLAYCTL(dev))
986 		return (1);
987 
988 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
989 		return (1);
990 
991 
992 	if (WSSCREEN_HAS_TTY(scr))
993 		return (ttykqfilter(dev, kn));
994 	else
995 		return (1);
996 }
997 
998 struct tty *
999 wsdisplaytty(dev_t dev)
1000 {
1001 	device_t dv;
1002 	struct wsdisplay_softc *sc;
1003 	struct wsscreen *scr;
1004 
1005 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1006 	sc = device_private(dv);
1007 
1008 	if (ISWSDISPLAYSTAT(dev))
1009 		panic("wsdisplaytty() on status device");
1010 
1011 	if (ISWSDISPLAYCTL(dev))
1012 		panic("wsdisplaytty() on ctl device");
1013 
1014 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1015 		return NULL;
1016 
1017 	return (scr->scr_tty);
1018 }
1019 
1020 int
1021 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1022 {
1023 	device_t dv;
1024 	struct wsdisplay_softc *sc;
1025 	struct tty *tp;
1026 	int error;
1027 	struct wsscreen *scr;
1028 
1029 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1030 	sc = device_private(dv);
1031 
1032 #ifdef WSDISPLAY_COMPAT_USL
1033 	error = wsdisplay_usl_ioctl1(dv, cmd, data, flag, l);
1034 	if (error != EPASSTHROUGH)
1035 		return (error);
1036 #endif
1037 
1038 	if (ISWSDISPLAYSTAT(dev))
1039 		return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
1040 
1041 	if (ISWSDISPLAYCTL(dev))
1042 		return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
1043 
1044 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1045 		return (ENXIO);
1046 
1047 	if (WSSCREEN_HAS_TTY(scr)) {
1048 		tp = scr->scr_tty;
1049 
1050 /* printf("disc\n"); */
1051 		/* do the line discipline ioctls first */
1052 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1053 		if (error != EPASSTHROUGH)
1054 			return (error);
1055 
1056 /* printf("tty\n"); */
1057 		/* then the tty ioctls */
1058 		error = ttioctl(tp, cmd, data, flag, l);
1059 		if (error != EPASSTHROUGH)
1060 			return (error);
1061 	}
1062 
1063 #ifdef WSDISPLAY_COMPAT_USL
1064 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
1065 	if (error != EPASSTHROUGH)
1066 		return (error);
1067 #endif
1068 
1069 	return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
1070 }
1071 
1072 int
1073 wsdisplay_param(device_t dv, u_long cmd, struct wsdisplay_param *dp)
1074 {
1075 	struct wsdisplay_softc *sc = device_private(dv);
1076 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1077 					   sc->sc_focus->scr_dconf->emulcookie,
1078 					   cmd, (void *)dp, 0, NULL));
1079 }
1080 
1081 int
1082 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
1083 	u_long cmd, void *data, int flag, struct lwp *l)
1084 {
1085 	int error;
1086 	char namebuf[16];
1087 	struct wsdisplay_font fd;
1088 #ifdef WSDISPLAY_SCROLLSUPPORT
1089 	struct wsdisplay_scroll_data *ksdp, *usdp;
1090 #endif
1091 
1092 #if NWSKBD > 0
1093 	struct wsevsrc *inp;
1094 
1095 #ifdef WSDISPLAY_COMPAT_RAWKBD
1096 	switch (cmd) {
1097 	case WSKBDIO_SETMODE:
1098 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
1099 		return (wsdisplay_update_rawkbd(sc, scr));
1100 	case WSKBDIO_GETMODE:
1101 		*(int *)data = (scr->scr_rawkbd ?
1102 				WSKBD_RAW : WSKBD_TRANSLATED);
1103 		return (0);
1104 	}
1105 #endif
1106 	inp = sc->sc_input;
1107 	if (inp == NULL)
1108 		return (ENXIO);
1109 	error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
1110 	if (error != EPASSTHROUGH)
1111 		return (error);
1112 #endif /* NWSKBD > 0 */
1113 
1114 	switch (cmd) {
1115 	case WSDISPLAYIO_GMODE:
1116 		if (scr->scr_flags & SCR_GRAPHICS) {
1117 			if (scr->scr_flags & SCR_DUMBFB)
1118 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
1119 			else
1120 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
1121 		} else
1122 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
1123 		return (0);
1124 
1125 	case WSDISPLAYIO_SMODE:
1126 #define d (*(int *)data)
1127 		if (d != WSDISPLAYIO_MODE_EMUL &&
1128 		    d != WSDISPLAYIO_MODE_MAPPED &&
1129 		    d != WSDISPLAYIO_MODE_DUMBFB)
1130 			return (EINVAL);
1131 
1132 	    if (WSSCREEN_HAS_EMULATOR(scr)) {
1133 		    scr->scr_flags &= ~SCR_GRAPHICS;
1134 		    if (d == WSDISPLAYIO_MODE_MAPPED ||
1135 			d == WSDISPLAYIO_MODE_DUMBFB)
1136 			    scr->scr_flags |= SCR_GRAPHICS |
1137 				    ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
1138 	    } else if (d == WSDISPLAYIO_MODE_EMUL)
1139 		    return (EINVAL);
1140 
1141 	    (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1142 	        scr->scr_dconf->emulcookie, cmd, data, flag, l);
1143 
1144 	    return (0);
1145 #undef d
1146 
1147 #ifdef WSDISPLAY_SCROLLSUPPORT
1148 #define	SETSCROLLLINES(dstp, srcp, dfltp)				\
1149     do {								\
1150 	(dstp)->fastlines = ((srcp)->which &				\
1151 			     WSDISPLAY_SCROLL_DOFASTLINES) ?		\
1152 			     (srcp)->fastlines : (dfltp)->fastlines;	\
1153 	(dstp)->slowlines = ((srcp)->which &				\
1154 			     WSDISPLAY_SCROLL_DOSLOWLINES) ?		\
1155 			     (srcp)->slowlines : (dfltp)->slowlines;	\
1156 	(dstp)->which = WSDISPLAY_SCROLL_DOALL;				\
1157     } while (0)
1158 
1159 
1160 	case WSDISPLAYIO_DSSCROLL:
1161 		usdp = (struct wsdisplay_scroll_data *)data;
1162 		ksdp = &sc->sc_scroll_values;
1163 		SETSCROLLLINES(ksdp, usdp, ksdp);
1164 		return (0);
1165 
1166 	case WSDISPLAYIO_DGSCROLL:
1167 		usdp = (struct wsdisplay_scroll_data *)data;
1168 		ksdp = &sc->sc_scroll_values;
1169 		SETSCROLLLINES(usdp, ksdp, ksdp);
1170 		return (0);
1171 #else
1172 	case WSDISPLAYIO_DSSCROLL:
1173 	case WSDISPLAYIO_DGSCROLL:
1174 		return ENODEV;
1175 #endif
1176 
1177 	case WSDISPLAYIO_SFONT:
1178 #define d ((struct wsdisplay_usefontdata *)data)
1179 		if (!sc->sc_accessops->load_font)
1180 			return (EINVAL);
1181 		if (d->name) {
1182 			error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
1183 			if (error)
1184 				return (error);
1185 			fd.name = namebuf;
1186 		} else
1187 			fd.name = 0;
1188 		fd.data = 0;
1189 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
1190 					scr->scr_dconf->emulcookie, &fd);
1191 		if (!error && WSSCREEN_HAS_EMULATOR(scr))
1192 			(*scr->scr_dconf->wsemul->reset)
1193 				(scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
1194 		return (error);
1195 #undef d
1196 
1197 #ifdef WSDISPLAY_CUSTOM_OUTPUT
1198 	case WSDISPLAYIO_GMSGATTRS:
1199 #define d ((struct wsdisplay_msgattrs *)data)
1200 		(*scr->scr_dconf->wsemul->getmsgattrs)
1201 		    (scr->scr_dconf->wsemulcookie, d);
1202 		return (0);
1203 #undef d
1204 
1205 	case WSDISPLAYIO_SMSGATTRS: {
1206 #define d ((struct wsdisplay_msgattrs *)data)
1207 		int i;
1208 		for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
1209 			if (sc->sc_scr[i] != NULL)
1210 				(*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
1211 				    (sc->sc_scr[i]->scr_dconf->wsemulcookie,
1212 				     sc->sc_scr[i]->scr_dconf->scrdata,
1213 				     d);
1214 		}
1215 		return (0);
1216 #undef d
1217 #else
1218 	case WSDISPLAYIO_GMSGATTRS:
1219 	case WSDISPLAYIO_SMSGATTRS:
1220 		return (ENODEV);
1221 #endif
1222 	}
1223 
1224 	/* check ioctls for display */
1225 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1226 	    scr->scr_dconf->emulcookie, cmd, data, flag, l));
1227 }
1228 
1229 int
1230 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
1231 	int flag, struct lwp *l)
1232 {
1233 	switch (cmd) {
1234 	case WSDISPLAYIO_GETACTIVESCREEN:
1235 		*(int*)data = wsdisplay_getactivescreen(sc);
1236 		return (0);
1237 	}
1238 
1239 	return (EPASSTHROUGH);
1240 }
1241 
1242 int
1243 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
1244 	int flag, struct lwp *l)
1245 {
1246 	int error;
1247 	char *type, typebuf[16], *emul, emulbuf[16];
1248 	void *tbuf;
1249 	u_int fontsz;
1250 #if defined(COMPAT_14) && NWSKBD > 0
1251 	struct wsmux_device wsmuxdata;
1252 #endif
1253 #if NWSKBD > 0
1254 	struct wsevsrc *inp;
1255 #endif
1256 
1257 	switch (cmd) {
1258 	case WSDISPLAYIO_ADDSCREEN:
1259 #define d ((struct wsdisplay_addscreendata *)data)
1260 		if (d->screentype) {
1261 			error = copyinstr(d->screentype, typebuf,
1262 					  sizeof(typebuf), 0);
1263 			if (error)
1264 				return (error);
1265 			type = typebuf;
1266 		} else
1267 			type = 0;
1268 		if (d->emul) {
1269 			error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
1270 			if (error)
1271 				return (error);
1272 			emul = emulbuf;
1273 		} else
1274 			emul = 0;
1275 
1276 		if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
1277 			wsdisplay_addscreen_print(sc, d->idx, 0);
1278 		return (error);
1279 #undef d
1280 	case WSDISPLAYIO_DELSCREEN:
1281 #define d ((struct wsdisplay_delscreendata *)data)
1282 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
1283 #undef d
1284 	case WSDISPLAYIO_LDFONT:
1285 #define d ((struct wsdisplay_font *)data)
1286 		if (!sc->sc_accessops->load_font)
1287 			return (EINVAL);
1288 		if (d->name) {
1289 			error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
1290 			if (error)
1291 				return (error);
1292 			d->name = typebuf;
1293 		} else
1294 			d->name = "loaded"; /* ??? */
1295 		fontsz = d->fontheight * d->stride * d->numchars;
1296 		if (fontsz > WSDISPLAY_MAXFONTSZ)
1297 			return (EINVAL);
1298 
1299 		tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
1300 		error = copyin(d->data, tbuf, fontsz);
1301 		if (error) {
1302 			free(tbuf, M_DEVBUF);
1303 			return (error);
1304 		}
1305 		d->data = tbuf;
1306 		error =
1307 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
1308 		free(tbuf, M_DEVBUF);
1309 #undef d
1310 		return (error);
1311 
1312 #if NWSKBD > 0
1313 #ifdef COMPAT_14
1314 	case _O_WSDISPLAYIO_SETKEYBOARD:
1315 #define d ((struct wsdisplay_kbddata *)data)
1316 		inp = sc->sc_input;
1317 		if (inp == NULL)
1318 			return (ENXIO);
1319 		switch (d->op) {
1320 		case _O_WSDISPLAY_KBD_ADD:
1321 			if (d->idx == -1) {
1322 				d->idx = wskbd_pickfree();
1323 				if (d->idx == -1)
1324 					return (ENXIO);
1325 			}
1326 			wsmuxdata.type = WSMUX_KBD;
1327 			wsmuxdata.idx = d->idx;
1328 			return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
1329 					      &wsmuxdata, flag, l));
1330 		case _O_WSDISPLAY_KBD_DEL:
1331 			wsmuxdata.type = WSMUX_KBD;
1332 			wsmuxdata.idx = d->idx;
1333 			return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
1334 					      &wsmuxdata, flag, l));
1335 		default:
1336 			return (EINVAL);
1337 		}
1338 #undef d
1339 #endif
1340 
1341 	case WSMUXIO_ADD_DEVICE:
1342 #define d ((struct wsmux_device *)data)
1343 		if (d->idx == -1 && d->type == WSMUX_KBD)
1344 			d->idx = wskbd_pickfree();
1345 #undef d
1346 		/* fall into */
1347 	case WSMUXIO_INJECTEVENT:
1348 	case WSMUXIO_REMOVE_DEVICE:
1349 	case WSMUXIO_LIST_DEVICES:
1350 		inp = sc->sc_input;
1351 		if (inp == NULL)
1352 			return (ENXIO);
1353 		return (wsevsrc_ioctl(inp, cmd, data, flag, l));
1354 #endif /* NWSKBD > 0 */
1355 
1356 	}
1357 	return (EPASSTHROUGH);
1358 }
1359 
1360 int
1361 wsdisplay_stat_inject(device_t dv, u_int type, int value)
1362 {
1363 	struct wsdisplay_softc *sc = device_private(dv);
1364 	struct wseventvar *evar;
1365 	struct wscons_event event;
1366 
1367 	evar = &sc->evar;
1368 
1369 	if (evar == NULL)
1370 		return (0);
1371 
1372 	if (evar->q == NULL)
1373 		return (1);
1374 
1375 	event.type = type;
1376 	event.value = value;
1377 	if (wsevent_inject(evar, &event, 1) != 0) {
1378 		log(LOG_WARNING, "wsdisplay: event queue overflow\n");
1379 		return (1);
1380 	}
1381 
1382 	return (0);
1383 }
1384 
1385 paddr_t
1386 wsdisplaymmap(dev_t dev, off_t offset, int prot)
1387 {
1388 	device_t dv;
1389 	struct wsdisplay_softc *sc;
1390 	struct wsscreen *scr;
1391 
1392 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1393 	sc = device_private(dv);
1394 
1395 	if (ISWSDISPLAYSTAT(dev))
1396 		return (-1);
1397 
1398 	if (ISWSDISPLAYCTL(dev))
1399 		return (-1);
1400 
1401 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1402 		return (-1);
1403 
1404 	if (!(scr->scr_flags & SCR_GRAPHICS))
1405 		return (-1);
1406 
1407 	/* pass mmap to display */
1408 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
1409 	    scr->scr_dconf->emulcookie, offset, prot));
1410 }
1411 
1412 void
1413 wsdisplaystart(struct tty *tp)
1414 {
1415 	device_t dv;
1416 	struct wsdisplay_softc *sc;
1417 	struct wsscreen *scr;
1418 	int s, n;
1419 	u_char *tbuf;
1420 
1421 	s = spltty();
1422 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
1423 		splx(s);
1424 		return;
1425 	}
1426 	dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
1427 	sc = device_private(dv);
1428 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
1429 		splx(s);
1430 		return;
1431 	}
1432 
1433 	if (scr->scr_hold_screen) {
1434 		tp->t_state |= TS_TIMEOUT;
1435 		splx(s);
1436 		return;
1437 	}
1438 	tp->t_state |= TS_BUSY;
1439 	splx(s);
1440 
1441 	/*
1442 	 * Drain output from ring buffer.
1443 	 * The output will normally be in one contiguous chunk, but when the
1444 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
1445 	 * the other at the start.  For performance, rather than loop here,
1446 	 * we output one chunk, see if there's another one, and if so, output
1447 	 * it too.
1448 	 */
1449 
1450 	n = ndqb(&tp->t_outq, 0);
1451 	tbuf = tp->t_outq.c_cf;
1452 
1453 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
1454 		KASSERT(WSSCREEN_HAS_EMULATOR(scr));
1455 		(*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
1456 						  tbuf, n, 0);
1457 	}
1458 	ndflush(&tp->t_outq, n);
1459 
1460 	if ((n = ndqb(&tp->t_outq, 0)) > 0) {
1461 		tbuf = tp->t_outq.c_cf;
1462 
1463 		if (!(scr->scr_flags & SCR_GRAPHICS)) {
1464 			KASSERT(WSSCREEN_HAS_EMULATOR(scr));
1465 			(*scr->scr_dconf->wsemul->output)
1466 			    (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
1467 		}
1468 		ndflush(&tp->t_outq, n);
1469 	}
1470 
1471 	s = spltty();
1472 	tp->t_state &= ~TS_BUSY;
1473 	/* Come back if there's more to do */
1474 	if (tp->t_outq.c_cc) {
1475 		tp->t_state |= TS_TIMEOUT;
1476 		callout_schedule(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1);
1477 	}
1478 	if (tp->t_outq.c_cc <= tp->t_lowat) {
1479 		if (tp->t_state&TS_ASLEEP) {
1480 			tp->t_state &= ~TS_ASLEEP;
1481 			wakeup(&tp->t_outq);
1482 		}
1483 		selwakeup(&tp->t_wsel);
1484 	}
1485 	splx(s);
1486 }
1487 
1488 void
1489 wsdisplaystop(struct tty *tp, int flag)
1490 {
1491 	int s;
1492 
1493 	s = spltty();
1494 	if (ISSET(tp->t_state, TS_BUSY))
1495 		if (!ISSET(tp->t_state, TS_TTSTOP))
1496 			SET(tp->t_state, TS_FLUSH);
1497 	splx(s);
1498 }
1499 
1500 /* Set line parameters. */
1501 int
1502 wsdisplayparam(struct tty *tp, struct termios *t)
1503 {
1504 
1505 	tp->t_ispeed = t->c_ispeed;
1506 	tp->t_ospeed = t->c_ospeed;
1507 	tp->t_cflag = t->c_cflag;
1508 	return 0;
1509 }
1510 
1511 /*
1512  * Callbacks for the emulation code.
1513  */
1514 void
1515 wsdisplay_emulbell(void *v)
1516 {
1517 	struct wsscreen *scr = v;
1518 
1519 	if (scr == NULL)		/* console, before real attach */
1520 		return;
1521 
1522 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
1523 		return;
1524 
1525 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
1526 					FWRITE, NULL);
1527 }
1528 
1529 void
1530 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
1531 {
1532 	struct wsscreen *scr = v;
1533 	struct tty *tp;
1534 
1535 	if (v == NULL)			/* console, before real attach */
1536 		return;
1537 
1538 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
1539 		return;
1540 	if (!WSSCREEN_HAS_TTY(scr))
1541 		return;
1542 
1543 	tp = scr->scr_tty;
1544 	while (count-- > 0)
1545 		(*tp->t_linesw->l_rint)(*data++, tp);
1546 }
1547 
1548 /*
1549  * Calls from the keyboard interface.
1550  */
1551 void
1552 wsdisplay_kbdinput(device_t dv, keysym_t ks)
1553 {
1554 	struct wsdisplay_softc *sc = device_private(dv);
1555 	struct wsscreen *scr;
1556 	const char *dp;
1557 	int count;
1558 	struct tty *tp;
1559 
1560 	KASSERT(sc != NULL);
1561 
1562 	scr = sc->sc_focus;
1563 
1564 	if (!scr || !WSSCREEN_HAS_TTY(scr))
1565 		return;
1566 
1567 	tp = scr->scr_tty;
1568 
1569 	if (KS_GROUP(ks) == KS_GROUP_Ascii)
1570 		(*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
1571 	else if (WSSCREEN_HAS_EMULATOR(scr)) {
1572 		count = (*scr->scr_dconf->wsemul->translate)
1573 		    (scr->scr_dconf->wsemulcookie, ks, &dp);
1574 		while (count-- > 0)
1575 			(*tp->t_linesw->l_rint)(*dp++, tp);
1576 	}
1577 }
1578 
1579 #if defined(WSDISPLAY_COMPAT_RAWKBD)
1580 int
1581 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
1582 {
1583 #if NWSKBD > 0
1584 	int s, raw, data, error;
1585 	struct wsevsrc *inp;
1586 
1587 	s = spltty();
1588 
1589 	raw = (scr ? scr->scr_rawkbd : 0);
1590 
1591 	if (scr != sc->sc_focus ||
1592 	    sc->sc_rawkbd == raw) {
1593 		splx(s);
1594 		return (0);
1595 	}
1596 
1597 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
1598 	inp = sc->sc_input;
1599 	if (inp == NULL) {
1600 		splx(s);
1601 		return (ENXIO);
1602 	}
1603 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
1604 	if (!error)
1605 		sc->sc_rawkbd = raw;
1606 	splx(s);
1607 	return (error);
1608 #else
1609 	return (0);
1610 #endif
1611 }
1612 #endif
1613 
1614 static void
1615 wsdisplay_switch3_cb(void *arg, int error, int waitok)
1616 {
1617 	device_t dv = arg;
1618 
1619 	wsdisplay_switch3(dv, error, waitok);
1620 }
1621 
1622 static int
1623 wsdisplay_switch3(device_t dv, int error, int waitok)
1624 {
1625 	struct wsdisplay_softc *sc = device_private(dv);
1626 	int no;
1627 	struct wsscreen *scr;
1628 
1629 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1630 		aprint_error_dev(dv, "wsdisplay_switch3: not switching\n");
1631 		return (EINVAL);
1632 	}
1633 
1634 	no = sc->sc_screenwanted;
1635 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1636 		panic("wsdisplay_switch3: invalid screen %d", no);
1637 	scr = sc->sc_scr[no];
1638 	if (!scr) {
1639 		aprint_error_dev(dv,
1640 		    "wsdisplay_switch3: screen %d disappeared\n", no);
1641 		error = ENXIO;
1642 	}
1643 
1644 	if (error) {
1645 		/* try to recover, avoid recursion */
1646 
1647 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1648 			aprint_error_dev(dv, "wsdisplay_switch3: giving up\n");
1649 			sc->sc_focus = 0;
1650 #ifdef WSDISPLAY_COMPAT_RAWKBD
1651 			wsdisplay_update_rawkbd(sc, 0);
1652 #endif
1653 			sc->sc_flags &= ~SC_SWITCHPENDING;
1654 			return (error);
1655 		}
1656 
1657 		sc->sc_screenwanted = sc->sc_oldscreen;
1658 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1659 		return (wsdisplay_switch1(dv, 0, waitok));
1660 	}
1661 
1662 	sc->sc_flags &= ~SC_SWITCHPENDING;
1663 
1664 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
1665 		wakeup(scr);
1666 	return (error);
1667 }
1668 
1669 static void
1670 wsdisplay_switch2_cb(void *arg, int error, int waitok)
1671 {
1672 	device_t dv = arg;
1673 
1674 	wsdisplay_switch2(dv, error, waitok);
1675 }
1676 
1677 static int
1678 wsdisplay_switch2(device_t dv, int error, int waitok)
1679 {
1680 	struct wsdisplay_softc *sc = device_private(dv);
1681 	int no;
1682 	struct wsscreen *scr;
1683 
1684 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1685 		aprint_error_dev(dv, "wsdisplay_switch2: not switching\n");
1686 		return (EINVAL);
1687 	}
1688 
1689 	no = sc->sc_screenwanted;
1690 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1691 		panic("wsdisplay_switch2: invalid screen %d", no);
1692 	scr = sc->sc_scr[no];
1693 	if (!scr) {
1694 		aprint_error_dev(dv,
1695 		    "wsdisplay_switch2: screen %d disappeared\n", no);
1696 		error = ENXIO;
1697 	}
1698 
1699 	if (error) {
1700 		/* try to recover, avoid recursion */
1701 
1702 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1703 			aprint_error_dev(dv, "wsdisplay_switch2: giving up\n");
1704 			sc->sc_focus = 0;
1705 			sc->sc_flags &= ~SC_SWITCHPENDING;
1706 			return (error);
1707 		}
1708 
1709 		sc->sc_screenwanted = sc->sc_oldscreen;
1710 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1711 		return (wsdisplay_switch1(dv, 0, waitok));
1712 	}
1713 
1714 	sc->sc_focusidx = no;
1715 	sc->sc_focus = scr;
1716 
1717 #ifdef WSDISPLAY_COMPAT_RAWKBD
1718 	(void) wsdisplay_update_rawkbd(sc, scr);
1719 #endif
1720 	/* keyboard map??? */
1721 
1722 	if (scr->scr_syncops) {
1723 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
1724 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch3_cb, dv);
1725 		if (error == EAGAIN) {
1726 			/* switch will be done asynchronously */
1727 			return (0);
1728 		}
1729 	}
1730 
1731 	return (wsdisplay_switch3(dv, error, waitok));
1732 }
1733 
1734 static void
1735 wsdisplay_switch1_cb(void *arg, int error, int waitok)
1736 {
1737 	device_t dv = arg;
1738 
1739 	wsdisplay_switch1(dv, error, waitok);
1740 }
1741 
1742 static int
1743 wsdisplay_switch1(device_t dv, int error, int waitok)
1744 {
1745 	struct wsdisplay_softc *sc = device_private(dv);
1746 	int no;
1747 	struct wsscreen *scr;
1748 
1749 	if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1750 		aprint_error_dev(dv, "wsdisplay_switch1: not switching\n");
1751 		return (EINVAL);
1752 	}
1753 
1754 	no = sc->sc_screenwanted;
1755 	if (no == WSDISPLAY_NULLSCREEN) {
1756 		sc->sc_flags &= ~SC_SWITCHPENDING;
1757 		if (!error) {
1758 			sc->sc_focus = 0;
1759 		}
1760 		wakeup(sc);
1761 		return (error);
1762 	}
1763 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1764 		panic("wsdisplay_switch1: invalid screen %d", no);
1765 	scr = sc->sc_scr[no];
1766 	if (!scr) {
1767 		aprint_error_dev(dv, "wsdisplay_switch1: screen %d disappeared\n", no);
1768 		error = ENXIO;
1769 	}
1770 
1771 	if (error) {
1772 		sc->sc_flags &= ~SC_SWITCHPENDING;
1773 		return (error);
1774 	}
1775 
1776 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1777 						 scr->scr_dconf->emulcookie,
1778 						 waitok,
1779 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch2_cb, dv);
1780 	if (error == EAGAIN) {
1781 		/* switch will be done asynchronously */
1782 		return (0);
1783 	}
1784 
1785 	return (wsdisplay_switch2(dv, error, waitok));
1786 }
1787 
1788 int
1789 wsdisplay_switch(device_t dv, int no, int waitok)
1790 {
1791 	struct wsdisplay_softc *sc = device_private(dv);
1792 	int s, res = 0;
1793 	struct wsscreen *scr;
1794 
1795 	if (no != WSDISPLAY_NULLSCREEN) {
1796 		if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
1797 			return (EINVAL);
1798 		if (sc->sc_scr[no] == NULL)
1799 			return (ENXIO);
1800 	}
1801 
1802 	wsdisplay_stat_inject(dv, WSCONS_EVENT_SCREEN_SWITCH, no);
1803 
1804 	s = spltty();
1805 
1806 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
1807 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
1808 		splx(s);
1809 		return (0);
1810 	}
1811 
1812 	if (sc->sc_flags & SC_SWITCHPENDING) {
1813 		splx(s);
1814 		return (EBUSY);
1815 	}
1816 
1817 	sc->sc_flags |= SC_SWITCHPENDING;
1818 	sc->sc_screenwanted = no;
1819 
1820 	splx(s);
1821 
1822 	scr = sc->sc_focus;
1823 	if (!scr) {
1824 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1825 		return (wsdisplay_switch1(dv, 0, waitok));
1826 	} else
1827 		sc->sc_oldscreen = sc->sc_focusidx;
1828 
1829 	if (scr->scr_syncops) {
1830 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
1831 	  sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch1_cb, dv);
1832 		if (res == EAGAIN) {
1833 			/* switch will be done asynchronously */
1834 			return (0);
1835 		}
1836 	} else if (scr->scr_flags & SCR_GRAPHICS) {
1837 		/* no way to save state */
1838 		res = EBUSY;
1839 	}
1840 
1841 	return (wsdisplay_switch1(dv, res, waitok));
1842 }
1843 
1844 void
1845 wsdisplay_reset(device_t dv, enum wsdisplay_resetops op)
1846 {
1847 	struct wsdisplay_softc *sc = device_private(dv);
1848 	struct wsscreen *scr;
1849 
1850 	KASSERT(sc != NULL);
1851 	scr = sc->sc_focus;
1852 
1853 	if (!scr)
1854 		return;
1855 
1856 	switch (op) {
1857 	case WSDISPLAY_RESETEMUL:
1858 		if (!WSSCREEN_HAS_EMULATOR(scr))
1859 			break;
1860 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
1861 						 WSEMUL_RESET);
1862 		break;
1863 	case WSDISPLAY_RESETCLOSE:
1864 		wsdisplay_closescreen(sc, scr);
1865 		break;
1866 	}
1867 }
1868 
1869 /*
1870  * Interface for (external) VT switch / process synchronization code
1871  */
1872 int
1873 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
1874 	void *cookie)
1875 {
1876 	if (scr->scr_syncops) {
1877 		/*
1878 		 * The screen is already claimed.
1879 		 * Check if the owner is still alive.
1880 		 */
1881 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
1882 			return (EBUSY);
1883 	}
1884 	scr->scr_syncops = ops;
1885 	scr->scr_synccookie = cookie;
1886 	return (0);
1887 }
1888 
1889 int
1890 wsscreen_detach_sync(struct wsscreen *scr)
1891 {
1892 	if (!scr->scr_syncops)
1893 		return (EINVAL);
1894 	scr->scr_syncops = 0;
1895 	return (0);
1896 }
1897 
1898 int
1899 wsscreen_lookup_sync(struct wsscreen *scr,
1900 	const struct wscons_syncops *ops, /* used as ID */
1901 	void **cookiep)
1902 {
1903 	if (!scr->scr_syncops || ops != scr->scr_syncops)
1904 		return (EINVAL);
1905 	*cookiep = scr->scr_synccookie;
1906 	return (0);
1907 }
1908 
1909 /*
1910  * Interface to virtual screen stuff
1911  */
1912 int
1913 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
1914 {
1915 	return (WSDISPLAY_MAXSCREEN - 1);
1916 }
1917 
1918 int
1919 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
1920 {
1921 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
1922 		return (EINVAL);
1923 	if (!sc->sc_scr[idx])
1924 		return (ENXIO);
1925 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
1926 }
1927 
1928 int
1929 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
1930 {
1931 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
1932 }
1933 
1934 int
1935 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
1936 {
1937 	struct wsscreen *scr;
1938 	int s, res = 0;
1939 
1940 	if (no == WSDISPLAY_NULLSCREEN) {
1941 		s = spltty();
1942 		while (sc->sc_focus && res == 0) {
1943 			res = tsleep(sc, PCATCH, "wswait", 0);
1944 		}
1945 		splx(s);
1946 		return (res);
1947 	}
1948 
1949 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1950 		return (ENXIO);
1951 	scr = sc->sc_scr[no];
1952 	if (!scr)
1953 		return (ENXIO);
1954 
1955 	s = spltty();
1956 	if (scr != sc->sc_focus) {
1957 		scr->scr_flags |= SCR_WAITACTIVE;
1958 		res = tsleep(scr, PCATCH, "wswait", 0);
1959 		if (scr != sc->sc_scr[no])
1960 			res = ENXIO; /* disappeared in the meantime */
1961 		else
1962 			scr->scr_flags &= ~SCR_WAITACTIVE;
1963 	}
1964 	splx(s);
1965 	return (res);
1966 }
1967 
1968 void
1969 wsdisplay_kbdholdscreen(device_t dv, int hold)
1970 {
1971 	struct wsdisplay_softc *sc = device_private(dv);
1972 	struct wsscreen *scr;
1973 
1974 	scr = sc->sc_focus;
1975 
1976 	if (hold)
1977 		scr->scr_hold_screen = 1;
1978 	else {
1979 		scr->scr_hold_screen = 0;
1980 		callout_schedule(&scr->scr_tty->t_rstrt_ch, 0);
1981 	}
1982 }
1983 
1984 #if NWSKBD > 0
1985 void
1986 wsdisplay_set_console_kbd(struct wsevsrc *src)
1987 {
1988 	if (wsdisplay_console_device == NULL) {
1989 		src->me_dispdv = NULL;
1990 		return;
1991 	}
1992 #if NWSMUX > 0
1993 	if (wsmux_attach_sc((struct wsmux_softc *)
1994 			    wsdisplay_console_device->sc_input, src)) {
1995 		src->me_dispdv = NULL;
1996 		return;
1997 	}
1998 #else
1999 	wsdisplay_console_device->sc_input = src;
2000 #endif
2001 	src->me_dispdv = wsdisplay_console_device->sc_dev;
2002 }
2003 #endif /* NWSKBD > 0 */
2004 
2005 /*
2006  * Console interface.
2007  */
2008 void
2009 wsdisplay_cnputc(dev_t dev, int i)
2010 {
2011 	struct wsscreen_internal *dc;
2012 	u_char c = i;
2013 
2014 	if (!wsdisplay_console_initted)
2015 		return;
2016 
2017 	if ((wsdisplay_console_device != NULL) &&
2018 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
2019 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
2020 		return;
2021 
2022 	dc = &wsdisplay_console_conf;
2023 	(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
2024 }
2025 
2026 static int
2027 wsdisplay_getc_dummy(dev_t dev)
2028 {
2029 	/* panic? */
2030 	return (0);
2031 }
2032 
2033 static void
2034 wsdisplay_pollc(dev_t dev, int on)
2035 {
2036 
2037 	wsdisplay_cons_pollmode = on;
2038 
2039 	/* notify to fb drivers */
2040 	if (wsdisplay_console_device != NULL &&
2041 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
2042 		(*wsdisplay_console_device->sc_accessops->pollc)
2043 			(wsdisplay_console_device->sc_accesscookie, on);
2044 
2045 	/* notify to kbd drivers */
2046 	if (wsdisplay_cons_kbd_pollc)
2047 		(*wsdisplay_cons_kbd_pollc)(NODEV, on);
2048 }
2049 
2050 void
2051 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
2052 	void (*bell)(dev_t, u_int, u_int, u_int))
2053 {
2054 	wsdisplay_cons.cn_getc = get;
2055 	wsdisplay_cons.cn_bell = bell;
2056 	wsdisplay_cons_kbd_pollc = poll;
2057 }
2058 
2059 void
2060 wsdisplay_unset_cons_kbd(void)
2061 {
2062 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
2063 	wsdisplay_cons.cn_bell = NULL;
2064 	wsdisplay_cons_kbd_pollc = 0;
2065 }
2066 
2067 /*
2068  * Switch the console display to it's first screen.
2069  */
2070 void
2071 wsdisplay_switchtoconsole(void)
2072 {
2073 	struct wsdisplay_softc *sc;
2074 	struct wsscreen *scr;
2075 
2076 	if (wsdisplay_console_device != NULL) {
2077 		sc = wsdisplay_console_device;
2078 		if ((scr = sc->sc_scr[0]) == NULL)
2079 			return;
2080 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
2081 						 scr->scr_dconf->emulcookie,
2082 						 0, NULL, NULL);
2083 	}
2084 }
2085 
2086 /*
2087  * Switch the console at shutdown.
2088  */
2089 static void
2090 wsdisplay_shutdownhook(void *arg)
2091 {
2092 
2093 	wsdisplay_switchtoconsole();
2094 }
2095