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