wsdisplay.c revision 1.109 1 /* $NetBSD: wsdisplay.c,v 1.109 2007/10/18 18:09:53 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.109 2007/10/18 18:09:53 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_private(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_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
1477 ttrstrt, tp);
1478 }
1479 if (tp->t_outq.c_cc <= tp->t_lowat) {
1480 if (tp->t_state&TS_ASLEEP) {
1481 tp->t_state &= ~TS_ASLEEP;
1482 wakeup(&tp->t_outq);
1483 }
1484 selwakeup(&tp->t_wsel);
1485 }
1486 splx(s);
1487 }
1488
1489 void
1490 wsdisplaystop(struct tty *tp, int flag)
1491 {
1492 int s;
1493
1494 s = spltty();
1495 if (ISSET(tp->t_state, TS_BUSY))
1496 if (!ISSET(tp->t_state, TS_TTSTOP))
1497 SET(tp->t_state, TS_FLUSH);
1498 splx(s);
1499 }
1500
1501 /* Set line parameters. */
1502 int
1503 wsdisplayparam(struct tty *tp, struct termios *t)
1504 {
1505
1506 tp->t_ispeed = t->c_ispeed;
1507 tp->t_ospeed = t->c_ospeed;
1508 tp->t_cflag = t->c_cflag;
1509 return 0;
1510 }
1511
1512 /*
1513 * Callbacks for the emulation code.
1514 */
1515 void
1516 wsdisplay_emulbell(void *v)
1517 {
1518 struct wsscreen *scr = v;
1519
1520 if (scr == NULL) /* console, before real attach */
1521 return;
1522
1523 if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
1524 return;
1525
1526 (void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
1527 FWRITE, NULL);
1528 }
1529
1530 void
1531 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
1532 {
1533 struct wsscreen *scr = v;
1534 struct tty *tp;
1535
1536 if (v == NULL) /* console, before real attach */
1537 return;
1538
1539 if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
1540 return;
1541 if (!WSSCREEN_HAS_TTY(scr))
1542 return;
1543
1544 tp = scr->scr_tty;
1545 while (count-- > 0)
1546 (*tp->t_linesw->l_rint)(*data++, tp);
1547 }
1548
1549 /*
1550 * Calls from the keyboard interface.
1551 */
1552 void
1553 wsdisplay_kbdinput(device_t dv, keysym_t ks)
1554 {
1555 struct wsdisplay_softc *sc = device_private(dv);
1556 struct wsscreen *scr;
1557 const char *dp;
1558 int count;
1559 struct tty *tp;
1560
1561 KASSERT(sc != NULL);
1562
1563 scr = sc->sc_focus;
1564
1565 if (!scr || !WSSCREEN_HAS_TTY(scr))
1566 return;
1567
1568 tp = scr->scr_tty;
1569
1570 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1571 (*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
1572 else if (WSSCREEN_HAS_EMULATOR(scr)) {
1573 count = (*scr->scr_dconf->wsemul->translate)
1574 (scr->scr_dconf->wsemulcookie, ks, &dp);
1575 while (count-- > 0)
1576 (*tp->t_linesw->l_rint)(*dp++, tp);
1577 }
1578 }
1579
1580 #if defined(WSDISPLAY_COMPAT_RAWKBD)
1581 int
1582 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
1583 {
1584 #if NWSKBD > 0
1585 int s, raw, data, error;
1586 struct wsevsrc *inp;
1587
1588 s = spltty();
1589
1590 raw = (scr ? scr->scr_rawkbd : 0);
1591
1592 if (scr != sc->sc_focus ||
1593 sc->sc_rawkbd == raw) {
1594 splx(s);
1595 return (0);
1596 }
1597
1598 data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
1599 inp = sc->sc_input;
1600 if (inp == NULL) {
1601 splx(s);
1602 return (ENXIO);
1603 }
1604 error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
1605 if (!error)
1606 sc->sc_rawkbd = raw;
1607 splx(s);
1608 return (error);
1609 #else
1610 return (0);
1611 #endif
1612 }
1613 #endif
1614
1615 static void
1616 wsdisplay_switch3_cb(void *arg, int error, int waitok)
1617 {
1618 device_t dv = arg;
1619
1620 wsdisplay_switch3(dv, error, waitok);
1621 }
1622
1623 static int
1624 wsdisplay_switch3(device_t dv, int error, int waitok)
1625 {
1626 struct wsdisplay_softc *sc = device_private(dv);
1627 int no;
1628 struct wsscreen *scr;
1629
1630 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1631 aprint_error_dev(dv, "wsdisplay_switch3: not switching\n");
1632 return (EINVAL);
1633 }
1634
1635 no = sc->sc_screenwanted;
1636 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1637 panic("wsdisplay_switch3: invalid screen %d", no);
1638 scr = sc->sc_scr[no];
1639 if (!scr) {
1640 aprint_error_dev(dv,
1641 "wsdisplay_switch3: screen %d disappeared\n", no);
1642 error = ENXIO;
1643 }
1644
1645 if (error) {
1646 /* try to recover, avoid recursion */
1647
1648 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1649 aprint_error_dev(dv, "wsdisplay_switch3: giving up\n");
1650 sc->sc_focus = 0;
1651 #ifdef WSDISPLAY_COMPAT_RAWKBD
1652 wsdisplay_update_rawkbd(sc, 0);
1653 #endif
1654 sc->sc_flags &= ~SC_SWITCHPENDING;
1655 return (error);
1656 }
1657
1658 sc->sc_screenwanted = sc->sc_oldscreen;
1659 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1660 return (wsdisplay_switch1(dv, 0, waitok));
1661 }
1662
1663 sc->sc_flags &= ~SC_SWITCHPENDING;
1664
1665 if (!error && (scr->scr_flags & SCR_WAITACTIVE))
1666 wakeup(scr);
1667 return (error);
1668 }
1669
1670 static void
1671 wsdisplay_switch2_cb(void *arg, int error, int waitok)
1672 {
1673 device_t dv = arg;
1674
1675 wsdisplay_switch2(dv, error, waitok);
1676 }
1677
1678 static int
1679 wsdisplay_switch2(device_t dv, int error, int waitok)
1680 {
1681 struct wsdisplay_softc *sc = device_private(dv);
1682 int no;
1683 struct wsscreen *scr;
1684
1685 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1686 aprint_error_dev(dv, "wsdisplay_switch2: not switching\n");
1687 return (EINVAL);
1688 }
1689
1690 no = sc->sc_screenwanted;
1691 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1692 panic("wsdisplay_switch2: invalid screen %d", no);
1693 scr = sc->sc_scr[no];
1694 if (!scr) {
1695 aprint_error_dev(dv,
1696 "wsdisplay_switch2: screen %d disappeared\n", no);
1697 error = ENXIO;
1698 }
1699
1700 if (error) {
1701 /* try to recover, avoid recursion */
1702
1703 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1704 aprint_error_dev(dv, "wsdisplay_switch2: giving up\n");
1705 sc->sc_focus = 0;
1706 sc->sc_flags &= ~SC_SWITCHPENDING;
1707 return (error);
1708 }
1709
1710 sc->sc_screenwanted = sc->sc_oldscreen;
1711 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1712 return (wsdisplay_switch1(dv, 0, waitok));
1713 }
1714
1715 sc->sc_focusidx = no;
1716 sc->sc_focus = scr;
1717
1718 #ifdef WSDISPLAY_COMPAT_RAWKBD
1719 (void) wsdisplay_update_rawkbd(sc, scr);
1720 #endif
1721 /* keyboard map??? */
1722
1723 if (scr->scr_syncops) {
1724 error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
1725 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch3_cb, dv);
1726 if (error == EAGAIN) {
1727 /* switch will be done asynchronously */
1728 return (0);
1729 }
1730 }
1731
1732 return (wsdisplay_switch3(dv, error, waitok));
1733 }
1734
1735 static void
1736 wsdisplay_switch1_cb(void *arg, int error, int waitok)
1737 {
1738 device_t dv = arg;
1739
1740 wsdisplay_switch1(dv, error, waitok);
1741 }
1742
1743 static int
1744 wsdisplay_switch1(device_t dv, int error, int waitok)
1745 {
1746 struct wsdisplay_softc *sc = device_private(dv);
1747 int no;
1748 struct wsscreen *scr;
1749
1750 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1751 aprint_error_dev(dv, "wsdisplay_switch1: not switching\n");
1752 return (EINVAL);
1753 }
1754
1755 no = sc->sc_screenwanted;
1756 if (no == WSDISPLAY_NULLSCREEN) {
1757 sc->sc_flags &= ~SC_SWITCHPENDING;
1758 if (!error) {
1759 sc->sc_focus = 0;
1760 }
1761 wakeup(sc);
1762 return (error);
1763 }
1764 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1765 panic("wsdisplay_switch1: invalid screen %d", no);
1766 scr = sc->sc_scr[no];
1767 if (!scr) {
1768 aprint_error_dev(dv, "wsdisplay_switch1: screen %d disappeared\n", no);
1769 error = ENXIO;
1770 }
1771
1772 if (error) {
1773 sc->sc_flags &= ~SC_SWITCHPENDING;
1774 return (error);
1775 }
1776
1777 error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1778 scr->scr_dconf->emulcookie,
1779 waitok,
1780 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch2_cb, dv);
1781 if (error == EAGAIN) {
1782 /* switch will be done asynchronously */
1783 return (0);
1784 }
1785
1786 return (wsdisplay_switch2(dv, error, waitok));
1787 }
1788
1789 int
1790 wsdisplay_switch(device_t dv, int no, int waitok)
1791 {
1792 struct wsdisplay_softc *sc = device_private(dv);
1793 int s, res = 0;
1794 struct wsscreen *scr;
1795
1796 if (no != WSDISPLAY_NULLSCREEN) {
1797 if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
1798 return (EINVAL);
1799 if (sc->sc_scr[no] == NULL)
1800 return (ENXIO);
1801 }
1802
1803 wsdisplay_stat_inject(dv, WSCONS_EVENT_SCREEN_SWITCH, no);
1804
1805 s = spltty();
1806
1807 if ((sc->sc_focus && no == sc->sc_focusidx) ||
1808 (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
1809 splx(s);
1810 return (0);
1811 }
1812
1813 if (sc->sc_flags & SC_SWITCHPENDING) {
1814 splx(s);
1815 return (EBUSY);
1816 }
1817
1818 sc->sc_flags |= SC_SWITCHPENDING;
1819 sc->sc_screenwanted = no;
1820
1821 splx(s);
1822
1823 scr = sc->sc_focus;
1824 if (!scr) {
1825 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1826 return (wsdisplay_switch1(dv, 0, waitok));
1827 } else
1828 sc->sc_oldscreen = sc->sc_focusidx;
1829
1830 if (scr->scr_syncops) {
1831 res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
1832 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch1_cb, dv);
1833 if (res == EAGAIN) {
1834 /* switch will be done asynchronously */
1835 return (0);
1836 }
1837 } else if (scr->scr_flags & SCR_GRAPHICS) {
1838 /* no way to save state */
1839 res = EBUSY;
1840 }
1841
1842 return (wsdisplay_switch1(dv, res, waitok));
1843 }
1844
1845 void
1846 wsdisplay_reset(device_t dv, enum wsdisplay_resetops op)
1847 {
1848 struct wsdisplay_softc *sc = device_private(dv);
1849 struct wsscreen *scr;
1850
1851 KASSERT(sc != NULL);
1852 scr = sc->sc_focus;
1853
1854 if (!scr)
1855 return;
1856
1857 switch (op) {
1858 case WSDISPLAY_RESETEMUL:
1859 if (!WSSCREEN_HAS_EMULATOR(scr))
1860 break;
1861 (*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
1862 WSEMUL_RESET);
1863 break;
1864 case WSDISPLAY_RESETCLOSE:
1865 wsdisplay_closescreen(sc, scr);
1866 break;
1867 }
1868 }
1869
1870 /*
1871 * Interface for (external) VT switch / process synchronization code
1872 */
1873 int
1874 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
1875 void *cookie)
1876 {
1877 if (scr->scr_syncops) {
1878 /*
1879 * The screen is already claimed.
1880 * Check if the owner is still alive.
1881 */
1882 if ((*scr->scr_syncops->check)(scr->scr_synccookie))
1883 return (EBUSY);
1884 }
1885 scr->scr_syncops = ops;
1886 scr->scr_synccookie = cookie;
1887 return (0);
1888 }
1889
1890 int
1891 wsscreen_detach_sync(struct wsscreen *scr)
1892 {
1893 if (!scr->scr_syncops)
1894 return (EINVAL);
1895 scr->scr_syncops = 0;
1896 return (0);
1897 }
1898
1899 int
1900 wsscreen_lookup_sync(struct wsscreen *scr,
1901 const struct wscons_syncops *ops, /* used as ID */
1902 void **cookiep)
1903 {
1904 if (!scr->scr_syncops || ops != scr->scr_syncops)
1905 return (EINVAL);
1906 *cookiep = scr->scr_synccookie;
1907 return (0);
1908 }
1909
1910 /*
1911 * Interface to virtual screen stuff
1912 */
1913 int
1914 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
1915 {
1916 return (WSDISPLAY_MAXSCREEN - 1);
1917 }
1918
1919 int
1920 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
1921 {
1922 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
1923 return (EINVAL);
1924 if (!sc->sc_scr[idx])
1925 return (ENXIO);
1926 return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
1927 }
1928
1929 int
1930 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
1931 {
1932 return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
1933 }
1934
1935 int
1936 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
1937 {
1938 struct wsscreen *scr;
1939 int s, res = 0;
1940
1941 if (no == WSDISPLAY_NULLSCREEN) {
1942 s = spltty();
1943 while (sc->sc_focus && res == 0) {
1944 res = tsleep(sc, PCATCH, "wswait", 0);
1945 }
1946 splx(s);
1947 return (res);
1948 }
1949
1950 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1951 return (ENXIO);
1952 scr = sc->sc_scr[no];
1953 if (!scr)
1954 return (ENXIO);
1955
1956 s = spltty();
1957 if (scr != sc->sc_focus) {
1958 scr->scr_flags |= SCR_WAITACTIVE;
1959 res = tsleep(scr, PCATCH, "wswait", 0);
1960 if (scr != sc->sc_scr[no])
1961 res = ENXIO; /* disappeared in the meantime */
1962 else
1963 scr->scr_flags &= ~SCR_WAITACTIVE;
1964 }
1965 splx(s);
1966 return (res);
1967 }
1968
1969 void
1970 wsdisplay_kbdholdscreen(device_t dv, int hold)
1971 {
1972 struct wsdisplay_softc *sc = device_private(dv);
1973 struct wsscreen *scr;
1974
1975 scr = sc->sc_focus;
1976
1977 if (hold)
1978 scr->scr_hold_screen = 1;
1979 else {
1980 scr->scr_hold_screen = 0;
1981 callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
1982 ttrstrt, scr->scr_tty); /* "immediate" */
1983 }
1984 }
1985
1986 #if NWSKBD > 0
1987 void
1988 wsdisplay_set_console_kbd(struct wsevsrc *src)
1989 {
1990 if (wsdisplay_console_device == NULL) {
1991 src->me_dispdv = NULL;
1992 return;
1993 }
1994 #if NWSMUX > 0
1995 if (wsmux_attach_sc((struct wsmux_softc *)
1996 wsdisplay_console_device->sc_input, src)) {
1997 src->me_dispdv = NULL;
1998 return;
1999 }
2000 #else
2001 wsdisplay_console_device->sc_input = src;
2002 #endif
2003 src->me_dispdv = wsdisplay_console_device->sc_dev;
2004 }
2005 #endif /* NWSKBD > 0 */
2006
2007 /*
2008 * Console interface.
2009 */
2010 void
2011 wsdisplay_cnputc(dev_t dev, int i)
2012 {
2013 struct wsscreen_internal *dc;
2014 u_char c = i;
2015
2016 if (!wsdisplay_console_initted)
2017 return;
2018
2019 if ((wsdisplay_console_device != NULL) &&
2020 (wsdisplay_console_device->sc_scr[0] != NULL) &&
2021 (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
2022 return;
2023
2024 dc = &wsdisplay_console_conf;
2025 (*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
2026 }
2027
2028 static int
2029 wsdisplay_getc_dummy(dev_t dev)
2030 {
2031 /* panic? */
2032 return (0);
2033 }
2034
2035 static void
2036 wsdisplay_pollc(dev_t dev, int on)
2037 {
2038
2039 wsdisplay_cons_pollmode = on;
2040
2041 /* notify to fb drivers */
2042 if (wsdisplay_console_device != NULL &&
2043 wsdisplay_console_device->sc_accessops->pollc != NULL)
2044 (*wsdisplay_console_device->sc_accessops->pollc)
2045 (wsdisplay_console_device->sc_accesscookie, on);
2046
2047 /* notify to kbd drivers */
2048 if (wsdisplay_cons_kbd_pollc)
2049 (*wsdisplay_cons_kbd_pollc)(NODEV, on);
2050 }
2051
2052 void
2053 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
2054 void (*bell)(dev_t, u_int, u_int, u_int))
2055 {
2056 wsdisplay_cons.cn_getc = get;
2057 wsdisplay_cons.cn_bell = bell;
2058 wsdisplay_cons_kbd_pollc = poll;
2059 }
2060
2061 void
2062 wsdisplay_unset_cons_kbd(void)
2063 {
2064 wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
2065 wsdisplay_cons.cn_bell = NULL;
2066 wsdisplay_cons_kbd_pollc = 0;
2067 }
2068
2069 /*
2070 * Switch the console display to it's first screen.
2071 */
2072 void
2073 wsdisplay_switchtoconsole(void)
2074 {
2075 struct wsdisplay_softc *sc;
2076 struct wsscreen *scr;
2077
2078 if (wsdisplay_console_device != NULL) {
2079 sc = wsdisplay_console_device;
2080 if ((scr = sc->sc_scr[0]) == NULL)
2081 return;
2082 (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
2083 scr->scr_dconf->emulcookie,
2084 0, NULL, NULL);
2085 }
2086 }
2087
2088 /*
2089 * Switch the console at shutdown.
2090 */
2091 static void
2092 wsdisplay_shutdownhook(void *arg)
2093 {
2094
2095 wsdisplay_switchtoconsole();
2096 }
2097