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