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