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