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