wsdisplay.c revision 1.51.2.6 1 /* $NetBSD: wsdisplay.c,v 1.51.2.6 2002/09/06 08:47:30 jdolecek 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.51.2.6 2002/09/06 08:47:30 jdolecek 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 int
870 wsdisplaykqfilter(dev, kn)
871 dev_t dev;
872 struct knote *kn;
873 {
874 struct wsdisplay_softc *sc =
875 device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
876 struct wsscreen *scr;
877
878 if (ISWSDISPLAYCTL(dev))
879 return (0);
880
881 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
882
883 if (WSSCREEN_HAS_TTY(scr))
884 return (ttykqfilter(dev, kn));
885 else
886 return (0);
887 }
888
889 struct tty *
890 wsdisplaytty(dev_t dev)
891 {
892 struct wsdisplay_softc *sc;
893 struct wsscreen *scr;
894
895 sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
896
897 if (ISWSDISPLAYSTAT(dev))
898 panic("wsdisplaytty() on status device");
899
900 if (ISWSDISPLAYCTL(dev))
901 panic("wsdisplaytty() on ctl device");
902
903 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
904
905 return (scr->scr_tty);
906 }
907
908 int
909 wsdisplayioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
910 {
911 struct wsdisplay_softc *sc;
912 struct tty *tp;
913 int error;
914 struct wsscreen *scr;
915
916 sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
917
918 #ifdef WSDISPLAY_COMPAT_USL
919 error = wsdisplay_usl_ioctl1(sc, cmd, data, flag, p);
920 if (error != EPASSTHROUGH)
921 return (error);
922 #endif
923
924 if (ISWSDISPLAYSTAT(dev))
925 return (wsdisplay_stat_ioctl(sc, cmd, data, flag, p));
926
927 if (ISWSDISPLAYCTL(dev))
928 return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, p));
929
930 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
931
932 if (WSSCREEN_HAS_TTY(scr)) {
933 tp = scr->scr_tty;
934
935 /* printf("disc\n"); */
936 /* do the line discipline ioctls first */
937 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
938 if (error != EPASSTHROUGH)
939 return (error);
940
941 /* printf("tty\n"); */
942 /* then the tty ioctls */
943 error = ttioctl(tp, cmd, data, flag, p);
944 if (error != EPASSTHROUGH)
945 return (error);
946 }
947
948 #ifdef WSDISPLAY_COMPAT_USL
949 error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, p);
950 if (error != EPASSTHROUGH)
951 return (error);
952 #endif
953
954 return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p));
955 }
956
957 int
958 wsdisplay_param(struct device *dev, u_long cmd, struct wsdisplay_param *dp)
959 {
960 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
961 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
962 (caddr_t)dp, 0, NULL));
963 }
964
965 int
966 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
967 u_long cmd, caddr_t data, int flag, struct proc *p)
968 {
969 int error;
970 char namebuf[16];
971 struct wsdisplay_font fd;
972
973 #if NWSKBD > 0
974 struct wsevsrc *inp;
975
976 #ifdef WSDISPLAY_COMPAT_RAWKBD
977 switch (cmd) {
978 case WSKBDIO_SETMODE:
979 scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
980 return (wsdisplay_update_rawkbd(sc, scr));
981 case WSKBDIO_GETMODE:
982 *(int *)data = (scr->scr_rawkbd ?
983 WSKBD_RAW : WSKBD_TRANSLATED);
984 return (0);
985 }
986 #endif
987 inp = sc->sc_input;
988 if (inp == NULL)
989 return (ENXIO);
990 error = wsevsrc_display_ioctl(inp, cmd, data, flag, p);
991 if (error != EPASSTHROUGH)
992 return (error);
993 #endif /* NWSKBD > 0 */
994
995 switch (cmd) {
996 case WSDISPLAYIO_GMODE:
997 *(u_int *)data = (scr->scr_flags & SCR_GRAPHICS ?
998 WSDISPLAYIO_MODE_MAPPED :
999 WSDISPLAYIO_MODE_EMUL);
1000 return (0);
1001
1002 case WSDISPLAYIO_SMODE:
1003 #define d (*(int *)data)
1004 if (d != WSDISPLAYIO_MODE_EMUL &&
1005 d != WSDISPLAYIO_MODE_MAPPED)
1006 return (EINVAL);
1007
1008 if (WSSCREEN_HAS_EMULATOR(scr)) {
1009 scr->scr_flags &= ~SCR_GRAPHICS;
1010 if (d == WSDISPLAYIO_MODE_MAPPED)
1011 scr->scr_flags |= SCR_GRAPHICS;
1012 } else if (d == WSDISPLAYIO_MODE_EMUL)
1013 return (EINVAL);
1014
1015 (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1016 flag, p);
1017
1018 return (0);
1019 #undef d
1020
1021 case WSDISPLAYIO_SFONT:
1022 #define d ((struct wsdisplay_usefontdata *)data)
1023 if (!sc->sc_accessops->load_font)
1024 return (EINVAL);
1025 if (d->name) {
1026 error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
1027 if (error)
1028 return (error);
1029 fd.name = namebuf;
1030 } else
1031 fd.name = 0;
1032 fd.data = 0;
1033 error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
1034 scr->scr_dconf->emulcookie, &fd);
1035 if (!error && WSSCREEN_HAS_EMULATOR(scr))
1036 (*scr->scr_dconf->wsemul->reset)
1037 (scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
1038 return (error);
1039 #undef d
1040
1041 #if defined(WSDISPLAY_CHARFUNCS)
1042 case WSDISPLAYIO_GETWSCHAR:
1043 #define d ((struct wsdisplay_char *)data)
1044 if (!sc->sc_accessops->getwschar)
1045 return (EINVAL);
1046 return ((*sc->sc_accessops->getwschar)
1047 (scr->scr_dconf->emulcookie, d));
1048 #undef d
1049
1050 case WSDISPLAYIO_PUTWSCHAR:
1051 #define d ((struct wsdisplay_char *)data)
1052 if (!sc->sc_accessops->putwschar)
1053 return (EINVAL);
1054 return ((*sc->sc_accessops->putwschar)
1055 (scr->scr_dconf->emulcookie, d));
1056 #undef d
1057 return 1;
1058 #endif /* WSDISPLAY_CHARFUNCS */
1059
1060 }
1061
1062 /* check ioctls for display */
1063 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1064 flag, p));
1065 }
1066
1067 int
1068 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
1069 int flag, struct proc *p)
1070 {
1071 switch (cmd) {
1072 case WSDISPLAYIO_GETACTIVESCREEN:
1073 *(int*)data = wsdisplay_getactivescreen(sc);
1074 return (0);
1075 }
1076
1077 return (EPASSTHROUGH);
1078 }
1079
1080 int
1081 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
1082 int flag, struct proc *p)
1083 {
1084 int error;
1085 char *type, typebuf[16], *emul, emulbuf[16];
1086 void *buf;
1087 u_int fontsz;
1088 #if defined(COMPAT_14) && NWSKBD > 0
1089 struct wsmux_device wsmuxdata;
1090 #endif
1091 #if NWSKBD > 0
1092 struct wsevsrc *inp;
1093 #endif
1094
1095 switch (cmd) {
1096 case WSDISPLAYIO_ADDSCREEN:
1097 #define d ((struct wsdisplay_addscreendata *)data)
1098 if (d->screentype) {
1099 error = copyinstr(d->screentype, typebuf,
1100 sizeof(typebuf), 0);
1101 if (error)
1102 return (error);
1103 type = typebuf;
1104 } else
1105 type = 0;
1106 if (d->emul) {
1107 error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
1108 if (error)
1109 return (error);
1110 emul = emulbuf;
1111 } else
1112 emul = 0;
1113
1114 if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
1115 wsdisplay_addscreen_print(sc, d->idx, 0);
1116 return (error);
1117 #undef d
1118 case WSDISPLAYIO_DELSCREEN:
1119 #define d ((struct wsdisplay_delscreendata *)data)
1120 return (wsdisplay_delscreen(sc, d->idx, d->flags));
1121 #undef d
1122 case WSDISPLAYIO_LDFONT:
1123 #define d ((struct wsdisplay_font *)data)
1124 if (!sc->sc_accessops->load_font)
1125 return (EINVAL);
1126 if (d->name) {
1127 error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
1128 if (error)
1129 return (error);
1130 d->name = typebuf;
1131 } else
1132 d->name = "loaded"; /* ??? */
1133 fontsz = d->fontheight * d->stride * d->numchars;
1134 if (fontsz > WSDISPLAY_MAXFONTSZ)
1135 return (EINVAL);
1136
1137 buf = malloc(fontsz, M_DEVBUF, M_WAITOK);
1138 error = copyin(d->data, buf, fontsz);
1139 if (error) {
1140 free(buf, M_DEVBUF);
1141 return (error);
1142 }
1143 d->data = buf;
1144 error =
1145 (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
1146 free(buf, M_DEVBUF);
1147 #undef d
1148 return (error);
1149
1150 #if NWSKBD > 0
1151 #ifdef COMPAT_14
1152 case _O_WSDISPLAYIO_SETKEYBOARD:
1153 #define d ((struct wsdisplay_kbddata *)data)
1154 inp = sc->sc_input;
1155 if (inp == NULL)
1156 return (ENXIO);
1157 switch (d->op) {
1158 case _O_WSDISPLAY_KBD_ADD:
1159 if (d->idx == -1) {
1160 d->idx = wskbd_pickfree();
1161 if (d->idx == -1)
1162 return (ENXIO);
1163 }
1164 wsmuxdata.type = WSMUX_KBD;
1165 wsmuxdata.idx = d->idx;
1166 return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
1167 &wsmuxdata, flag, p));
1168 case _O_WSDISPLAY_KBD_DEL:
1169 wsmuxdata.type = WSMUX_KBD;
1170 wsmuxdata.idx = d->idx;
1171 return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
1172 &wsmuxdata, flag, p));
1173 default:
1174 return (EINVAL);
1175 }
1176 #undef d
1177 #endif
1178
1179 case WSMUXIO_ADD_DEVICE:
1180 #define d ((struct wsmux_device *)data)
1181 if (d->idx == -1 && d->type == WSMUX_KBD)
1182 d->idx = wskbd_pickfree();
1183 #undef d
1184 /* fall into */
1185 case WSMUXIO_INJECTEVENT:
1186 case WSMUXIO_REMOVE_DEVICE:
1187 case WSMUXIO_LIST_DEVICES:
1188 inp = sc->sc_input;
1189 if (inp == NULL)
1190 return (ENXIO);
1191 return (wsevsrc_ioctl(inp, cmd, data, flag, p));
1192 #endif /* NWSKBD > 0 */
1193
1194 }
1195 return (EPASSTHROUGH);
1196 }
1197
1198 int
1199 wsdisplay_stat_inject(struct device *dev, u_int type, int value)
1200 {
1201 struct wsdisplay_softc *sc = (struct wsdisplay_softc *) dev;
1202 struct wseventvar *evar;
1203 struct wscons_event *ev;
1204 struct timeval thistime;
1205 int put;
1206
1207 evar = &sc->evar;
1208
1209 if (evar == NULL)
1210 return (0);
1211
1212 if (evar->q == NULL)
1213 return (1);
1214
1215 put = evar->put;
1216 ev = &evar->q[put];
1217 put = (put + 1) % WSEVENT_QSIZE;
1218 if (put == evar->get) {
1219 log(LOG_WARNING, "wsdisplay: event queue overflow\n");
1220 return (1);
1221 }
1222 ev->type = type;
1223 ev->value = value;
1224 microtime(&thistime);
1225 TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
1226 evar->put = put;
1227 WSEVENT_WAKEUP(evar);
1228
1229 return (0);
1230 }
1231
1232 paddr_t
1233 wsdisplaymmap(dev_t dev, off_t offset, int prot)
1234 {
1235 struct wsdisplay_softc *sc =
1236 device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1237 struct wsscreen *scr;
1238
1239 if (ISWSDISPLAYSTAT(dev))
1240 return (-1);
1241
1242 if (ISWSDISPLAYCTL(dev))
1243 return (-1);
1244
1245 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
1246
1247 if (!(scr->scr_flags & SCR_GRAPHICS))
1248 return (-1);
1249
1250 /* pass mmap to display */
1251 return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
1252 }
1253
1254 void
1255 wsdisplaystart(struct tty *tp)
1256 {
1257 struct wsdisplay_softc *sc;
1258 struct wsscreen *scr;
1259 int s, n;
1260 u_char *buf;
1261
1262 s = spltty();
1263 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
1264 splx(s);
1265 return;
1266 }
1267 sc = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
1268 scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)];
1269 if (scr->scr_hold_screen) {
1270 tp->t_state |= TS_TIMEOUT;
1271 splx(s);
1272 return;
1273 }
1274 tp->t_state |= TS_BUSY;
1275 splx(s);
1276
1277 /*
1278 * Drain output from ring buffer.
1279 * The output will normally be in one contiguous chunk, but when the
1280 * ring wraps, it will be in two pieces.. one at the end of the ring,
1281 * the other at the start. For performance, rather than loop here,
1282 * we output one chunk, see if there's another one, and if so, output
1283 * it too.
1284 */
1285
1286 n = ndqb(&tp->t_outq, 0);
1287 buf = tp->t_outq.c_cf;
1288
1289 if (!(scr->scr_flags & SCR_GRAPHICS)) {
1290 KASSERT(WSSCREEN_HAS_EMULATOR(scr));
1291 (*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
1292 buf, n, 0);
1293 }
1294 ndflush(&tp->t_outq, n);
1295
1296 if ((n = ndqb(&tp->t_outq, 0)) > 0) {
1297 buf = tp->t_outq.c_cf;
1298
1299 if (!(scr->scr_flags & SCR_GRAPHICS)) {
1300 KASSERT(WSSCREEN_HAS_EMULATOR(scr));
1301 (*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
1302 buf, n, 0);
1303 }
1304 ndflush(&tp->t_outq, n);
1305 }
1306
1307 s = spltty();
1308 tp->t_state &= ~TS_BUSY;
1309 /* Come back if there's more to do */
1310 if (tp->t_outq.c_cc) {
1311 tp->t_state |= TS_TIMEOUT;
1312 callout_reset(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1,
1313 ttrstrt, tp);
1314 }
1315 if (tp->t_outq.c_cc <= tp->t_lowat) {
1316 if (tp->t_state&TS_ASLEEP) {
1317 tp->t_state &= ~TS_ASLEEP;
1318 wakeup(&tp->t_outq);
1319 }
1320 selwakeup(&tp->t_wsel);
1321 }
1322 splx(s);
1323 }
1324
1325 void
1326 wsdisplaystop(struct tty *tp, int flag)
1327 {
1328 int s;
1329
1330 s = spltty();
1331 if (ISSET(tp->t_state, TS_BUSY))
1332 if (!ISSET(tp->t_state, TS_TTSTOP))
1333 SET(tp->t_state, TS_FLUSH);
1334 splx(s);
1335 }
1336
1337 /* Set line parameters. */
1338 int
1339 wsdisplayparam(struct tty *tp, struct termios *t)
1340 {
1341
1342 tp->t_ispeed = t->c_ispeed;
1343 tp->t_ospeed = t->c_ospeed;
1344 tp->t_cflag = t->c_cflag;
1345 return 0;
1346 }
1347
1348 /*
1349 * Callbacks for the emulation code.
1350 */
1351 void
1352 wsdisplay_emulbell(void *v)
1353 {
1354 struct wsscreen *scr = v;
1355
1356 if (scr == NULL) /* console, before real attach */
1357 return;
1358
1359 if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
1360 return;
1361
1362 (void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
1363 FWRITE, NULL);
1364 }
1365
1366 void
1367 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
1368 {
1369 struct wsscreen *scr = v;
1370 struct tty *tp;
1371
1372 if (v == NULL) /* console, before real attach */
1373 return;
1374
1375 if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
1376 return;
1377 if (!WSSCREEN_HAS_TTY(scr))
1378 return;
1379
1380 tp = scr->scr_tty;
1381 while (count-- > 0)
1382 (*tp->t_linesw->l_rint)(*data++, tp);
1383 };
1384
1385 /*
1386 * Calls from the keyboard interface.
1387 */
1388 void
1389 wsdisplay_kbdinput(struct device *dev, keysym_t ks)
1390 {
1391 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1392 struct wsscreen *scr;
1393 char *dp;
1394 int count;
1395 struct tty *tp;
1396
1397 KASSERT(sc != NULL);
1398
1399 scr = sc->sc_focus;
1400
1401 if (!scr || !WSSCREEN_HAS_TTY(scr))
1402 return;
1403
1404 tp = scr->scr_tty;
1405
1406 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1407 (*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
1408 else if (WSSCREEN_HAS_EMULATOR(scr)) {
1409 count = (*scr->scr_dconf->wsemul->translate)
1410 (scr->scr_dconf->wsemulcookie, ks, &dp);
1411 while (count-- > 0)
1412 (*tp->t_linesw->l_rint)(*dp++, tp);
1413 }
1414 }
1415
1416 #if defined(WSDISPLAY_COMPAT_RAWKBD)
1417 int
1418 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
1419 {
1420 #if NWSKBD > 0
1421 int s, raw, data, error;
1422 struct wsevsrc *inp;
1423
1424 s = spltty();
1425
1426 raw = (scr ? scr->scr_rawkbd : 0);
1427
1428 if (scr != sc->sc_focus ||
1429 sc->sc_rawkbd == raw) {
1430 splx(s);
1431 return (0);
1432 }
1433
1434 data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
1435 inp = sc->sc_input;
1436 if (inp == NULL)
1437 return (ENXIO);
1438 error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
1439 if (!error)
1440 sc->sc_rawkbd = raw;
1441 splx(s);
1442 return (error);
1443 #else
1444 return (0);
1445 #endif
1446 }
1447 #endif
1448
1449 int
1450 wsdisplay_switch3(void *arg, int error, int waitok)
1451 {
1452 struct wsdisplay_softc *sc = arg;
1453 int no;
1454 struct wsscreen *scr;
1455
1456 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1457 printf("wsdisplay_switch3: not switching\n");
1458 return (EINVAL);
1459 }
1460
1461 no = sc->sc_screenwanted;
1462 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1463 panic("wsdisplay_switch3: invalid screen %d", no);
1464 scr = sc->sc_scr[no];
1465 if (!scr) {
1466 printf("wsdisplay_switch3: screen %d disappeared\n", no);
1467 error = ENXIO;
1468 }
1469
1470 if (error) {
1471 /* try to recover, avoid recursion */
1472
1473 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1474 printf("wsdisplay_switch3: giving up\n");
1475 sc->sc_focus = 0;
1476 #ifdef WSDISPLAY_COMPAT_RAWKBD
1477 wsdisplay_update_rawkbd(sc, 0);
1478 #endif
1479 sc->sc_flags &= ~SC_SWITCHPENDING;
1480 return (error);
1481 }
1482
1483 sc->sc_screenwanted = sc->sc_oldscreen;
1484 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1485 return (wsdisplay_switch1(arg, 0, waitok));
1486 }
1487
1488 sc->sc_flags &= ~SC_SWITCHPENDING;
1489
1490 if (!error && (scr->scr_flags & SCR_WAITACTIVE))
1491 wakeup(scr);
1492 return (error);
1493 }
1494
1495 int
1496 wsdisplay_switch2(void *arg, int error, int waitok)
1497 {
1498 struct wsdisplay_softc *sc = arg;
1499 int no;
1500 struct wsscreen *scr;
1501
1502 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1503 printf("wsdisplay_switch2: not switching\n");
1504 return (EINVAL);
1505 }
1506
1507 no = sc->sc_screenwanted;
1508 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1509 panic("wsdisplay_switch2: invalid screen %d", no);
1510 scr = sc->sc_scr[no];
1511 if (!scr) {
1512 printf("wsdisplay_switch2: screen %d disappeared\n", no);
1513 error = ENXIO;
1514 }
1515
1516 if (error) {
1517 /* try to recover, avoid recursion */
1518
1519 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1520 printf("wsdisplay_switch2: giving up\n");
1521 sc->sc_focus = 0;
1522 sc->sc_flags &= ~SC_SWITCHPENDING;
1523 return (error);
1524 }
1525
1526 sc->sc_screenwanted = sc->sc_oldscreen;
1527 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1528 return (wsdisplay_switch1(arg, 0, waitok));
1529 }
1530
1531 sc->sc_focusidx = no;
1532 sc->sc_focus = scr;
1533
1534 #ifdef WSDISPLAY_COMPAT_RAWKBD
1535 (void) wsdisplay_update_rawkbd(sc, scr);
1536 #endif
1537 /* keyboard map??? */
1538
1539 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
1540 if (scr->scr_syncops) {
1541 error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
1542 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb3, sc);
1543 if (error == EAGAIN) {
1544 /* switch will be done asynchronously */
1545 return (0);
1546 }
1547 }
1548
1549 return (wsdisplay_switch3(sc, error, waitok));
1550 }
1551
1552 int
1553 wsdisplay_switch1(void *arg, int error, int waitok)
1554 {
1555 struct wsdisplay_softc *sc = arg;
1556 int no;
1557 struct wsscreen *scr;
1558
1559 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1560 printf("wsdisplay_switch1: not switching\n");
1561 return (EINVAL);
1562 }
1563
1564 no = sc->sc_screenwanted;
1565 if (no == WSDISPLAY_NULLSCREEN) {
1566 sc->sc_flags &= ~SC_SWITCHPENDING;
1567 if (!error) {
1568 sc->sc_focus = 0;
1569 }
1570 wakeup(sc);
1571 return (error);
1572 }
1573 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1574 panic("wsdisplay_switch1: invalid screen %d", no);
1575 scr = sc->sc_scr[no];
1576 if (!scr) {
1577 printf("wsdisplay_switch1: screen %d disappeared\n", no);
1578 error = ENXIO;
1579 }
1580
1581 if (error) {
1582 sc->sc_flags &= ~SC_SWITCHPENDING;
1583 return (error);
1584 }
1585
1586 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
1587 error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1588 scr->scr_dconf->emulcookie,
1589 waitok,
1590 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
1591 if (error == EAGAIN) {
1592 /* switch will be done asynchronously */
1593 return (0);
1594 }
1595
1596 return (wsdisplay_switch2(sc, error, waitok));
1597 }
1598
1599 int
1600 wsdisplay_switch(struct device *dev, int no, int waitok)
1601 {
1602 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1603 int s, res = 0;
1604 struct wsscreen *scr;
1605
1606 if (no != WSDISPLAY_NULLSCREEN &&
1607 (no < 0 || no >= WSDISPLAY_MAXSCREEN || !sc->sc_scr[no]))
1608 return (ENXIO);
1609
1610 wsdisplay_stat_inject(dev, WSCONS_EVENT_SCREEN_SWITCH, no);
1611
1612 s = spltty();
1613
1614 if ((sc->sc_focus && no == sc->sc_focusidx) ||
1615 (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
1616 splx(s);
1617 return (0);
1618 }
1619
1620 if (sc->sc_flags & SC_SWITCHPENDING) {
1621 splx(s);
1622 return (EBUSY);
1623 }
1624
1625 sc->sc_flags |= SC_SWITCHPENDING;
1626 sc->sc_screenwanted = no;
1627
1628 splx(s);
1629
1630 scr = sc->sc_focus;
1631 if (!scr) {
1632 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1633 return (wsdisplay_switch1(sc, 0, waitok));
1634 } else
1635 sc->sc_oldscreen = sc->sc_focusidx;
1636
1637 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
1638 if (scr->scr_syncops) {
1639 res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
1640 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb1, sc);
1641 if (res == EAGAIN) {
1642 /* switch will be done asynchronously */
1643 return (0);
1644 }
1645 } else if (scr->scr_flags & SCR_GRAPHICS) {
1646 /* no way to save state */
1647 res = EBUSY;
1648 }
1649
1650 return (wsdisplay_switch1(sc, res, waitok));
1651 }
1652
1653 void
1654 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
1655 {
1656 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1657 struct wsscreen *scr;
1658
1659 KASSERT(sc != NULL);
1660 scr = sc->sc_focus;
1661
1662 if (!scr)
1663 return;
1664
1665 switch (op) {
1666 case WSDISPLAY_RESETEMUL:
1667 if (!WSSCREEN_HAS_EMULATOR(scr))
1668 break;
1669 (*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
1670 WSEMUL_RESET);
1671 break;
1672 case WSDISPLAY_RESETCLOSE:
1673 wsdisplay_closescreen(sc, scr);
1674 break;
1675 }
1676 }
1677
1678 /*
1679 * Interface for (external) VT switch / process synchronization code
1680 */
1681 int
1682 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
1683 void *cookie)
1684 {
1685 if (scr->scr_syncops) {
1686 /*
1687 * The screen is already claimed.
1688 * Check if the owner is still alive.
1689 */
1690 if ((*scr->scr_syncops->check)(scr->scr_synccookie))
1691 return (EBUSY);
1692 }
1693 scr->scr_syncops = ops;
1694 scr->scr_synccookie = cookie;
1695 return (0);
1696 }
1697
1698 int
1699 wsscreen_detach_sync(struct wsscreen *scr)
1700 {
1701 if (!scr->scr_syncops)
1702 return (EINVAL);
1703 scr->scr_syncops = 0;
1704 return (0);
1705 }
1706
1707 int
1708 wsscreen_lookup_sync(struct wsscreen *scr,
1709 const struct wscons_syncops *ops, /* used as ID */
1710 void **cookiep)
1711 {
1712 if (!scr->scr_syncops || ops != scr->scr_syncops)
1713 return (EINVAL);
1714 *cookiep = scr->scr_synccookie;
1715 return (0);
1716 }
1717
1718 /*
1719 * Interface to virtual screen stuff
1720 */
1721 int
1722 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
1723 {
1724 return (WSDISPLAY_MAXSCREEN - 1);
1725 }
1726
1727 int
1728 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
1729 {
1730 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
1731 return (EINVAL);
1732 if (!sc->sc_scr[idx])
1733 return (ENXIO);
1734 return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
1735 }
1736
1737 int
1738 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
1739 {
1740 return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
1741 }
1742
1743 int
1744 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
1745 {
1746 struct wsscreen *scr;
1747 int s, res = 0;
1748
1749 if (no == WSDISPLAY_NULLSCREEN) {
1750 s = spltty();
1751 while (sc->sc_focus && res == 0) {
1752 res = tsleep(sc, PCATCH, "wswait", 0);
1753 }
1754 splx(s);
1755 return (res);
1756 }
1757
1758 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1759 return (ENXIO);
1760 scr = sc->sc_scr[no];
1761 if (!scr)
1762 return (ENXIO);
1763
1764 s = spltty();
1765 if (scr != sc->sc_focus) {
1766 scr->scr_flags |= SCR_WAITACTIVE;
1767 res = tsleep(scr, PCATCH, "wswait", 0);
1768 if (scr != sc->sc_scr[no])
1769 res = ENXIO; /* disappeared in the meantime */
1770 else
1771 scr->scr_flags &= ~SCR_WAITACTIVE;
1772 }
1773 splx(s);
1774 return (res);
1775 }
1776
1777 void
1778 wsdisplay_kbdholdscreen(struct device *dev, int hold)
1779 {
1780 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1781 struct wsscreen *scr;
1782
1783 scr = sc->sc_focus;
1784
1785 if (hold)
1786 scr->scr_hold_screen = 1;
1787 else {
1788 scr->scr_hold_screen = 0;
1789 callout_reset(&scr->scr_tty->t_rstrt_ch, 0,
1790 ttrstrt, scr->scr_tty); /* "immediate" */
1791 }
1792 }
1793
1794 #if NWSKBD > 0
1795 void
1796 wsdisplay_set_console_kbd(struct wsevsrc *src)
1797 {
1798 if (wsdisplay_console_device == NULL) {
1799 src->me_dispdv = NULL;
1800 return;
1801 }
1802 #if NWSMUX > 0
1803 if (wsmux_attach_sc((struct wsmux_softc *)
1804 wsdisplay_console_device->sc_input, src)) {
1805 src->me_dispdv = NULL;
1806 return;
1807 }
1808 #else
1809 wsdisplay_console_device->sc_input = src;
1810 #endif
1811 src->me_dispdv = &wsdisplay_console_device->sc_dv;
1812 }
1813 #endif /* NWSKBD > 0 */
1814
1815 /*
1816 * Console interface.
1817 */
1818 void
1819 wsdisplay_cnputc(dev_t dev, int i)
1820 {
1821 struct wsscreen_internal *dc;
1822 char c = i;
1823
1824 if (!wsdisplay_console_initted)
1825 return;
1826
1827 if (wsdisplay_console_device != NULL &&
1828 (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
1829 return;
1830
1831 dc = &wsdisplay_console_conf;
1832 (*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
1833 }
1834
1835 static int
1836 wsdisplay_getc_dummy(dev_t dev)
1837 {
1838 /* panic? */
1839 return (0);
1840 }
1841
1842 static void
1843 wsdisplay_pollc(dev_t dev, int on)
1844 {
1845
1846 wsdisplay_cons_pollmode = on;
1847
1848 /* notify to fb drivers */
1849 if (wsdisplay_console_device != NULL &&
1850 wsdisplay_console_device->sc_accessops->pollc != NULL)
1851 (*wsdisplay_console_device->sc_accessops->pollc)
1852 (wsdisplay_console_device->sc_accesscookie, on);
1853
1854 /* notify to kbd drivers */
1855 if (wsdisplay_cons_kbd_pollc)
1856 (*wsdisplay_cons_kbd_pollc)(NODEV, on);
1857 }
1858
1859 void
1860 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
1861 void (*bell)(dev_t, u_int, u_int, u_int))
1862 {
1863 wsdisplay_cons.cn_getc = get;
1864 wsdisplay_cons.cn_bell = bell;
1865 wsdisplay_cons_kbd_pollc = poll;
1866 }
1867
1868 void
1869 wsdisplay_unset_cons_kbd(void)
1870 {
1871 wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
1872 wsdisplay_cons.cn_bell = NULL;
1873 wsdisplay_cons_kbd_pollc = 0;
1874 }
1875
1876 /*
1877 * Switch the console display to it's first screen.
1878 */
1879 void
1880 wsdisplay_switchtoconsole(void)
1881 {
1882 struct wsdisplay_softc *sc;
1883 struct wsscreen *scr;
1884
1885 if (wsdisplay_console_device != NULL) {
1886 sc = wsdisplay_console_device;
1887 scr = sc->sc_scr[0];
1888 (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1889 scr->scr_dconf->emulcookie,
1890 0, NULL, NULL);
1891 }
1892 }
1893
1894 /*
1895 * Switch the console at shutdown.
1896 */
1897 static void
1898 wsdisplay_shutdownhook(void *arg)
1899 {
1900
1901 wsdisplay_switchtoconsole();
1902 }
1903