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