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