wsdisplay.c revision 1.12 1 /* $NetBSD: wsdisplay.c,v 1.12 1998/12/15 14:25:59 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 static const char _copyright[] __attribute__ ((unused)) =
34 "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
35 static const char _rcsid[] __attribute__ ((unused)) =
36 "$NetBSD: wsdisplay.c,v 1.12 1998/12/15 14:25:59 drochner Exp $";
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/ioctl.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/syslog.h>
46 #include <sys/systm.h>
47 #include <sys/tty.h>
48 #include <sys/signalvar.h>
49 #include <sys/errno.h>
50 #include <sys/fcntl.h>
51
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsksymvar.h>
55 #include <dev/wscons/wsksymdef.h>
56 #include <dev/wscons/wsemulvar.h>
57 #include <dev/wscons/wscons_callbacks.h>
58 #include <dev/cons.h>
59
60 #include "opt_wsdisplay_compat.h"
61
62 #include "wsdisplay.h"
63
64 struct wsdisplay_conf {
65 const struct wsdisplay_emulops *emulops;
66 void *emulcookie;
67
68 const struct wsscreen_descr *scrdata;
69
70 const struct wsemul_ops *wsemul;
71 void *wsemulcookie;
72 };
73
74 struct wsscreen {
75 struct wsdisplay_conf *scr_dconf;
76
77 struct tty *scr_tty;
78 int scr_hold_screen; /* hold tty output */
79
80 int scr_flags;
81 #define SCR_OPEN 1 /* is it open? */
82 #define SCR_WAITACTIVE 2 /* someone waiting on activation */
83 #define SCR_GRAPHICS 4 /* graphics mode, no text (emulation) output */
84 const struct wscons_syncops *scr_syncops;
85 void *scr_synccookie;
86
87 #ifdef WSDISPLAY_COMPAT_RAWKBD
88 int scr_rawkbd;
89 #endif
90
91 struct wsdisplay_softc *sc;
92 };
93
94 struct wsscreen *wsscreen_attach __P((struct wsdisplay_softc *, int,
95 const char *,
96 const struct wsscreen_descr *, void *,
97 int, int, long));
98
99 #define WSDISPLAY_MAXSCREEN 8
100
101 struct wsdisplay_softc {
102 struct device sc_dv;
103
104 const struct wsdisplay_accessops *sc_accessops;
105 void *sc_accesscookie;
106
107 struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
108 int sc_focusidx;
109 struct wsscreen *sc_focus;
110
111 int sc_isconsole;
112 struct device *sc_kbddv;
113
114 int sc_flags;
115 #define SC_SWITCHPENDING 1
116 int sc_screenwanted; /* valid with SC_SWITCHPENDING */
117
118 #ifdef WSDISPLAY_COMPAT_RAWKBD
119 int sc_rawkbd;
120 #endif
121 };
122
123 #if NWSDISPLAY > 0
124 extern struct cfdriver wsdisplay_cd;
125 #endif /* NWSDISPLAY > 0 */
126
127 /* Autoconfiguration definitions. */
128 static int wsdisplay_emul_match __P((struct device *, struct cfdata *,
129 void *));
130 static void wsdisplay_emul_attach __P((struct device *, struct device *,
131 void *));
132 static int wsdisplay_noemul_match __P((struct device *, struct cfdata *,
133 void *));
134 static void wsdisplay_noemul_attach __P((struct device *, struct device *,
135 void *));
136
137 struct cfattach wsdisplay_emul_ca = {
138 sizeof (struct wsdisplay_softc),
139 wsdisplay_emul_match,
140 wsdisplay_emul_attach,
141 };
142
143 struct cfattach wsdisplay_noemul_ca = {
144 sizeof (struct wsdisplay_softc),
145 wsdisplay_noemul_match,
146 wsdisplay_noemul_attach,
147 };
148
149 /* Exported tty- and cdevsw-related functions. */
150 cdev_decl(wsdisplay);
151
152 #if NWSDISPLAY > 0
153 static void wsdisplaystart __P((struct tty *));
154 static int wsdisplayparam __P((struct tty *, struct termios *));
155 #endif /* NWSDISPLAY > 0 */
156
157
158 /* Internal macros, functions, and variables. */
159 #define SET(t, f) (t) |= (f)
160 #define CLR(t, f) (t) &= ~(f)
161 #define ISSET(t, f) ((t) & (f))
162
163 #define WSDISPLAYUNIT(dev) (minor(dev) >> 8)
164 #define WSDISPLAYSCREEN(dev) (minor(dev) & 0xff)
165 #define WSDISPLAYMINOR(unit, screen) (((unit) << 8) | (screen))
166 #define WSDISPLAYBURST (OBUFSIZ - 1)
167
168 #define WSSCREEN_HAS_EMULATOR(scr) ((scr)->scr_dconf->wsemul != NULL)
169 #define WSSCREEN_HAS_TTY(scr) ((scr)->scr_tty != NULL)
170
171 static void wsdisplay_common_attach __P((struct wsdisplay_softc *sc,
172 int console, const struct wsscreen_list *,
173 const struct wsdisplay_accessops *accessops,
174 void *accesscookie));
175
176 #ifdef WSDISPLAY_COMPAT_RAWKBD
177 int wsdisplay_update_rawkbd __P((struct wsdisplay_softc *,
178 struct wsscreen *));
179 #endif
180
181 static int wsdisplay_console_initted;
182 static struct wsdisplay_softc *wsdisplay_console_device;
183 static struct wsdisplay_conf wsdisplay_console_conf;
184
185 static int wsdisplay_getc_dummy __P((dev_t));
186 static void wsdisplay_pollc_dummy __P((dev_t, int));
187
188 static struct consdev wsdisplay_cons = {
189 NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
190 wsdisplay_pollc_dummy, NODEV, CN_NORMAL
191 };
192
193 int wsdisplay_switch1 __P((void *, int));
194
195 struct wsscreen *wsscreen_attach(sc, console, emul, type, cookie,
196 ccol, crow, defattr)
197 struct wsdisplay_softc *sc;
198 int console;
199 const char *emul;
200 const struct wsscreen_descr *type;
201 void *cookie;
202 int ccol, crow;
203 long defattr;
204 {
205 struct wsdisplay_conf *dconf;
206 struct wsscreen *scr;
207
208 scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
209 if (!scr)
210 return (NULL);
211
212 if (console) {
213 dconf = &wsdisplay_console_conf;
214 /*
215 * If there's an emulation, tell it about the callback argument.
216 * The other stuff is already there.
217 */
218 if (dconf->wsemul != NULL)
219 (*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
220 } else { /* not console */
221 dconf = malloc(sizeof(struct wsdisplay_conf),
222 M_DEVBUF, M_NOWAIT);
223 dconf->emulops = type->textops;
224 dconf->emulcookie = cookie;
225 if (dconf->emulops) {
226 dconf->wsemul = wsemul_pick(emul);
227 dconf->wsemulcookie =
228 (*dconf->wsemul->attach)(0, type, cookie,
229 ccol, crow, scr, defattr);
230 } else
231 dconf->wsemul = NULL;
232 dconf->scrdata = type;
233 }
234
235 scr->scr_dconf = dconf;
236
237 scr->scr_tty = ttymalloc();
238 tty_attach(scr->scr_tty);
239 scr->scr_hold_screen = 0;
240 if (WSSCREEN_HAS_EMULATOR(scr))
241 scr->scr_flags = 0;
242 else
243 scr->scr_flags = SCR_GRAPHICS;
244
245 scr->scr_syncops = 0;
246 scr->sc = sc;
247 #ifdef WSDISPLAY_COMPAT_RAWKBD
248 scr->scr_rawkbd = 0;
249 #endif
250 return (scr);
251 }
252
253 /*
254 * Autoconfiguration functions.
255 */
256 int
257 wsdisplay_emul_match(parent, match, aux)
258 struct device *parent;
259 struct cfdata *match;
260 void *aux;
261 {
262 struct wsemuldisplaydev_attach_args *ap = aux;
263
264 if (match->wsemuldisplaydevcf_console !=
265 WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
266 /*
267 * If console-ness of device specified, either match
268 * exactly (at high priority), or fail.
269 */
270 if (match->wsemuldisplaydevcf_console != 0 &&
271 ap->console != 0)
272 return (10);
273 else
274 return (0);
275 }
276
277 /* If console-ness unspecified, it wins. */
278 return (1);
279 }
280
281 void
282 wsdisplay_emul_attach(parent, self, aux)
283 struct device *parent, *self;
284 void *aux;
285 {
286 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
287 struct wsemuldisplaydev_attach_args *ap = aux;
288
289 wsdisplay_common_attach(sc, ap->console, ap->scrdata,
290 ap->accessops, ap->accesscookie);
291
292 if (ap->console) {
293 int maj;
294
295 /* locate the major number */
296 for (maj = 0; maj < nchrdev; maj++)
297 if (cdevsw[maj].d_open == wsdisplayopen)
298 break;
299
300 cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(self->dv_unit, 0));
301 }
302 }
303
304 /* Print function (for parent devices). */
305 int
306 wsemuldisplaydevprint(aux, pnp)
307 void *aux;
308 const char *pnp;
309 {
310 #if 0 /* -Wunused */
311 struct wsemuldisplaydev_attach_args *ap = aux;
312 #endif
313
314 if (pnp)
315 printf("wsdisplay at %s", pnp);
316 #if 0 /* don't bother; it's ugly */
317 printf(" console %d", ap->console);
318 #endif
319
320 return (UNCONF);
321 }
322
323 int
324 wsdisplay_noemul_match(parent, match, aux)
325 struct device *parent;
326 struct cfdata *match;
327 void *aux;
328 {
329 #if 0 /* -Wunused */
330 struct wsdisplaydev_attach_args *ap = aux;
331 #endif
332
333 /* Always match. */
334 return (1);
335 }
336
337 void
338 wsdisplay_noemul_attach(parent, self, aux)
339 struct device *parent, *self;
340 void *aux;
341 {
342 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
343 struct wsdisplaydev_attach_args *ap = aux;
344
345 wsdisplay_common_attach(sc, 0, NULL, ap->accessops, ap->accesscookie);
346 }
347
348 /* Print function (for parent devices). */
349 int
350 wsdisplaydevprint(aux, pnp)
351 void *aux;
352 const char *pnp;
353 {
354 #if 0 /* -Wunused */
355 struct wsdisplaydev_attach_args *ap = aux;
356 #endif
357
358 if (pnp)
359 printf("wsdisplay at %s", pnp);
360
361 return (UNCONF);
362 }
363
364 static void
365 wsdisplay_common_attach(sc, console, scrdata, accessops, accesscookie)
366 struct wsdisplay_softc *sc;
367 int console;
368 const struct wsscreen_list *scrdata;
369 const struct wsdisplay_accessops *accessops;
370 void *accesscookie;
371 {
372 const struct wsscreen_descr *scr;
373 int res, i = 0;
374
375 if (console) {
376 KASSERT(wsdisplay_console_initted);
377 KASSERT(wsdisplay_console_device == NULL);
378
379 sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
380 wsdisplay_console_device = sc;
381
382 printf(": console (%s, %s emulation)",
383 wsdisplay_console_conf.scrdata->name,
384 wsdisplay_console_conf.wsemul->name);
385
386 i++;
387 }
388
389 printf("\n");
390
391 #if 1 /* XXX do this in ioctl() - user selects screen type and emulation */
392 KASSERT(scrdata->nscreens > 0);
393 scr = scrdata->screens[0];
394
395 for (; i < WSDISPLAY_MAXSCREEN; i++) {
396 void *cookie;
397 int ccol, crow;
398 long defattr;
399
400 res = ((*accessops->alloc_screen)(accesscookie, scr,
401 &cookie, &ccol, &crow,
402 &defattr));
403 if (res)
404 break;
405
406 sc->sc_scr[i] = wsscreen_attach(sc, 0, 0, scr, cookie,
407 ccol, crow, defattr);
408 }
409 #endif
410
411 sc->sc_focusidx = 0;
412 sc->sc_focus = sc->sc_scr[0];
413
414 sc->sc_accessops = accessops;
415 sc->sc_accesscookie = accesscookie;
416
417 sc->sc_isconsole = console;
418 sc->sc_kbddv = NULL;
419
420 wscons_glue_set_callback();
421 }
422
423 void
424 wsdisplay_cnattach(type, cookie, ccol, crow, defattr)
425 const struct wsscreen_descr *type;
426 void *cookie;
427 int ccol, crow;
428 long defattr;
429 {
430 const struct wsemul_ops *wsemul;
431
432 KASSERT(!wsdisplay_console_initted);
433 KASSERT(type->nrows > 0);
434 KASSERT(type->ncols > 0);
435 KASSERT(crow < type->nrows);
436 KASSERT(ccol < type->ncols);
437
438 wsdisplay_console_conf.emulops = type->textops;
439 wsdisplay_console_conf.emulcookie = cookie;
440 wsdisplay_console_conf.scrdata = type;
441
442 wsemul = wsemul_pick(0); /* default */
443 wsdisplay_console_conf.wsemul = wsemul;
444 wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
445 ccol, crow,
446 defattr);
447
448 cn_tab = &wsdisplay_cons;
449
450 wsdisplay_console_initted = 1;
451 }
452
453 /*
454 * Tty and cdevsw functions.
455 */
456 int
457 wsdisplayopen(dev, flag, mode, p)
458 dev_t dev;
459 int flag, mode;
460 struct proc *p;
461 {
462 #if NWSDISPLAY > 0
463 struct wsdisplay_softc *sc;
464 struct tty *tp;
465 int unit, newopen, error;
466 struct wsscreen *scr;
467
468 unit = WSDISPLAYUNIT(dev);
469 if (unit >= wsdisplay_cd.cd_ndevs || /* make sure it was attached */
470 (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
471 return (ENXIO);
472
473 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
474 if (!scr)
475 return (ENXIO);
476
477 if (WSSCREEN_HAS_TTY(scr)) {
478 tp = scr->scr_tty;
479 tp->t_oproc = wsdisplaystart;
480 tp->t_param = wsdisplayparam;
481 tp->t_dev = dev;
482 newopen = (tp->t_state & TS_ISOPEN) == 0;
483 if (newopen) {
484 ttychars(tp);
485 tp->t_iflag = TTYDEF_IFLAG;
486 tp->t_oflag = TTYDEF_OFLAG;
487 tp->t_cflag = TTYDEF_CFLAG;
488 tp->t_lflag = TTYDEF_LFLAG;
489 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
490 wsdisplayparam(tp, &tp->t_termios);
491 ttsetwater(tp);
492 } else if ((tp->t_state & TS_XCLUDE) != 0 &&
493 p->p_ucred->cr_uid != 0)
494 return EBUSY;
495 tp->t_state |= TS_CARR_ON;
496
497 error = ((*linesw[tp->t_line].l_open)(dev, tp));
498 if (error)
499 return (error);
500
501 if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
502 /* set window sizes as appropriate, and reset
503 the emulation */
504 tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
505 tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
506
507 /* wsdisplay_set_emulation() */
508 }
509 }
510
511 scr->scr_flags |= SCR_OPEN;
512 return (0);
513 #else
514 return (ENXIO);
515 #endif /* NWSDISPLAY > 0 */
516 }
517
518 int
519 wsdisplayclose(dev, flag, mode, p)
520 dev_t dev;
521 int flag, mode;
522 struct proc *p;
523 {
524 #if NWSDISPLAY > 0
525 struct wsdisplay_softc *sc;
526 struct tty *tp;
527 int unit;
528 struct wsscreen *scr;
529
530 unit = WSDISPLAYUNIT(dev);
531 if (unit >= wsdisplay_cd.cd_ndevs || /* make sure it was attached */
532 (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
533 return (ENXIO);
534
535 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
536
537 if (WSSCREEN_HAS_TTY(scr)) {
538 if (scr->scr_hold_screen) {
539 int s;
540
541 /* XXX RESET KEYBOARD LEDS, etc. */
542 s = spltty(); /* avoid conflict with keyboard */
543 wsdisplay_kbdholdscreen((struct device *)sc, 0);
544 splx(s);
545 }
546 tp = scr->scr_tty;
547 (*linesw[tp->t_line].l_close)(tp, flag);
548 ttyclose(tp);
549 }
550 /* XXX RESET EMULATOR? */
551
552 if (scr->scr_syncops)
553 (*scr->scr_syncops->destroy)(scr->scr_synccookie);
554
555 if (WSSCREEN_HAS_EMULATOR(scr))
556 scr->scr_flags &= ~SCR_GRAPHICS;
557
558 #ifdef WSDISPLAY_COMPAT_RAWKBD
559 if (scr->scr_rawkbd) {
560 int kbmode = WSKBD_TRANSLATED;
561 (void) wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
562 (caddr_t)&kbmode, 0, p);
563 }
564 #endif
565
566 scr->scr_flags &= ~SCR_OPEN;
567
568 return (0);
569 #else
570 return (ENXIO);
571 #endif /* NWSDISPLAY > 0 */
572 }
573
574 int
575 wsdisplayread(dev, uio, flag)
576 dev_t dev;
577 struct uio *uio;
578 int flag;
579 {
580 #if NWSDISPLAY > 0
581 struct wsdisplay_softc *sc;
582 struct tty *tp;
583 int unit;
584 struct wsscreen *scr;
585
586 unit = WSDISPLAYUNIT(dev);
587 if (unit >= wsdisplay_cd.cd_ndevs || /* make sure it was attached */
588 (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
589 return (ENXIO);
590
591 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
592
593 if (!WSSCREEN_HAS_TTY(scr))
594 return (ENODEV);
595
596 tp = scr->scr_tty;
597 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
598 #else
599 return (ENXIO);
600 #endif /* NWSDISPLAY > 0 */
601 }
602
603 int
604 wsdisplaywrite(dev, uio, flag)
605 dev_t dev;
606 struct uio *uio;
607 int flag;
608 {
609 #if NWSDISPLAY > 0
610 struct wsdisplay_softc *sc;
611 struct tty *tp;
612 int unit;
613 struct wsscreen *scr;
614
615 unit = WSDISPLAYUNIT(dev);
616 if (unit >= wsdisplay_cd.cd_ndevs || /* make sure it was attached */
617 (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
618 return (ENXIO);
619
620 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
621
622 if (!WSSCREEN_HAS_TTY(scr))
623 return (ENODEV);
624
625 tp = scr->scr_tty;
626 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
627 #else
628 return (ENXIO);
629 #endif /* NWSDISPLAY > 0 */
630 }
631
632 struct tty *
633 wsdisplaytty(dev)
634 dev_t dev;
635 {
636 #if NWSDISPLAY > 0
637 struct wsdisplay_softc *sc;
638 int unit;
639 struct wsscreen *scr;
640
641 unit = WSDISPLAYUNIT(dev);
642 if (unit >= wsdisplay_cd.cd_ndevs || /* make sure it was attached */
643 (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
644 return (NULL);
645
646 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
647
648 return (scr->scr_tty);
649 #else
650 return (NULL);
651 #endif /* NWSDISPLAY > 0 */
652 }
653
654 int
655 wsdisplayioctl(dev, cmd, data, flag, p)
656 dev_t dev;
657 u_long cmd;
658 caddr_t data;
659 int flag;
660 struct proc *p;
661 {
662 #if NWSDISPLAY > 0
663 struct wsdisplay_softc *sc;
664 struct tty *tp;
665 int unit, error;
666 struct wsscreen *scr;
667
668 unit = WSDISPLAYUNIT(dev);
669 if (unit >= wsdisplay_cd.cd_ndevs || /* make sure it was attached */
670 (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
671 return (ENXIO);
672
673 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
674
675 if (WSSCREEN_HAS_TTY(scr)) {
676 tp = scr->scr_tty;
677
678 /* printf("disc\n"); */
679 /* do the line discipline ioctls first */
680 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
681 if (error >= 0)
682 return error;
683
684 /* printf("tty\n"); */
685 /* then the tty ioctls */
686 error = ttioctl(tp, cmd, data, flag, p);
687 if (error >= 0)
688 return error;
689 }
690
691 #ifdef WSDISPLAY_COMPAT_USL
692 error = wsdisplay_usl_ioctl(sc, scr, cmd, data, flag, p);
693 if (error >= 0)
694 return (error);
695 #endif
696
697 error = wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p);
698 return (error != -1 ? error : ENOTTY);
699 #else
700 return (ENXIO);
701 #endif /* NWSDISPLAY > 0 */
702 }
703
704 int
705 wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p)
706 struct wsdisplay_softc *sc;
707 struct wsscreen *scr;
708 u_long cmd;
709 caddr_t data;
710 int flag;
711 struct proc *p;
712 {
713 int error;
714 void *buf;
715
716 if (sc->sc_kbddv != NULL) {
717 /* check ioctls for keyboard */
718 #ifdef WSDISPLAY_COMPAT_RAWKBD
719 switch (cmd) {
720 case WSKBDIO_SETMODE:
721 scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
722 return (wsdisplay_update_rawkbd(sc, scr));
723 case WSKBDIO_GETMODE:
724 *(int *)data = (scr->scr_rawkbd ?
725 WSKBD_RAW : WSKBD_TRANSLATED);
726 return (0);
727 }
728 #endif
729 /* printf("kbdcallback\n"); */
730 error = wskbd_displayioctl(sc->sc_kbddv, cmd, data, flag, p);
731 if (error >= 0)
732 return error;
733 }
734
735 /* printf("display\n"); */
736 switch (cmd) {
737 case WSDISPLAYIO_GMODE:
738 *(u_int *)data = (scr->scr_flags & SCR_GRAPHICS ?
739 WSDISPLAYIO_MODE_MAPPED :
740 WSDISPLAYIO_MODE_EMUL);
741 return (0);
742
743 case WSDISPLAYIO_SMODE:
744 #define d (*(int *)data)
745 if (d != WSDISPLAYIO_MODE_EMUL &&
746 d != WSDISPLAYIO_MODE_MAPPED)
747 return (EINVAL);
748
749 if (WSSCREEN_HAS_EMULATOR(scr)) {
750 scr->scr_flags &= ~SCR_GRAPHICS;
751 if (d == WSDISPLAYIO_MODE_MAPPED)
752 scr->scr_flags |= SCR_GRAPHICS;
753 } else if (d == WSDISPLAYIO_MODE_EMUL)
754 return (EINVAL);
755 return (0);
756 #undef d
757 case WSDISPLAYIO_SFONT:
758 #define d ((struct wsdisplay_font *)data)
759 if (d->fontheight != scr->scr_dconf->scrdata->fontheight ||
760 d->fontwidth != scr->scr_dconf->scrdata->fontwidth)
761 return (EINVAL);
762 buf = malloc(d->fontheight * d->stride * d->numchars,
763 M_DEVBUF, M_WAITOK);
764 error = copyin(d->data, buf,
765 d->fontheight * d->stride * d->numchars);
766 if (error) {
767 free(buf, M_DEVBUF);
768 return (error);
769 }
770 error =
771 (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
772 scr->scr_dconf->emulcookie,
773 d->firstchar, d->numchars,
774 d->stride, buf);
775 free(buf, M_DEVBUF);
776 #undef d
777 return (error);
778 }
779
780 /* check ioctls for display */
781 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
782 flag, p));
783 }
784
785 int
786 wsdisplaymmap(dev, offset, prot)
787 dev_t dev;
788 int offset; /* XXX */
789 int prot;
790 {
791 #if NWSDISPLAY > 0
792 struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
793 struct wsscreen *scr;
794
795 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
796
797 if (!(scr->scr_flags & SCR_GRAPHICS))
798 return (-1);
799
800 /* pass mmap to display */
801 return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
802 #else
803 return (-1);
804 #endif /* NWSDISPLAY > 0 */
805 }
806
807 int
808 wsdisplaypoll(dev, events, p)
809 dev_t dev;
810 int events;
811 struct proc *p;
812 {
813 #if NWSDISPLAY > 0
814 struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
815 struct wsscreen *scr;
816
817 scr = sc->sc_scr[WSDISPLAYSCREEN(dev)];
818
819 if (WSSCREEN_HAS_TTY(scr))
820 return (ttpoll(dev, events, p));
821 else
822 return (0);
823 #else
824 return (0);
825 #endif /* NWSDISPLAY > 0 */
826 }
827
828 #if NWSDISPLAY > 0
829 void
830 wsdisplaystart(tp)
831 register struct tty *tp;
832 {
833 struct wsdisplay_softc *sc;
834 struct wsscreen *scr;
835 register int s, n;
836 u_char buf[WSDISPLAYBURST];
837
838 s = spltty();
839 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
840 splx(s);
841 return;
842 }
843 sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(tp->t_dev)];
844 scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)];
845 if (scr->scr_hold_screen) {
846 tp->t_state |= TS_TIMEOUT;
847 splx(s);
848 return;
849 }
850 tp->t_state |= TS_BUSY;
851 splx(s);
852
853 n = q_to_b(&tp->t_outq, buf, sizeof(buf));
854
855 if (!(scr->scr_flags & SCR_GRAPHICS)) {
856 KASSERT(WSSCREEN_HAS_EMULATOR(scr));
857 (*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
858 buf, n, 0);
859 }
860
861 s = spltty();
862 tp->t_state &= ~TS_BUSY;
863 /* Come back if there's more to do */
864 if (tp->t_outq.c_cc) {
865 tp->t_state |= TS_TIMEOUT;
866 timeout(ttrstrt, tp, (hz > 128) ? (hz / 128) : 1);
867 }
868 if (tp->t_outq.c_cc <= tp->t_lowat) {
869 if (tp->t_state&TS_ASLEEP) {
870 tp->t_state &= ~TS_ASLEEP;
871 wakeup((caddr_t)&tp->t_outq);
872 }
873 selwakeup(&tp->t_wsel);
874 }
875 splx(s);
876 }
877 #endif /* NWSDISPLAY > 0 */
878
879 void
880 wsdisplaystop(tp, flag)
881 struct tty *tp;
882 int flag;
883 {
884 int s;
885
886 s = spltty();
887 if (ISSET(tp->t_state, TS_BUSY))
888 if (!ISSET(tp->t_state, TS_TTSTOP))
889 SET(tp->t_state, TS_FLUSH);
890 splx(s);
891 }
892
893 #if NWSDISPLAY > 0
894 /* Set line parameters. */
895 int
896 wsdisplayparam(tp, t)
897 struct tty *tp;
898 struct termios *t;
899 {
900
901 tp->t_ispeed = t->c_ispeed;
902 tp->t_ospeed = t->c_ospeed;
903 tp->t_cflag = t->c_cflag;
904 return 0;
905 }
906 #endif /* NWSDISPLAY > 0 */
907
908 /*
909 * Callbacks for the emulation code.
910 */
911 void
912 wsdisplay_emulbell(v)
913 void *v;
914 {
915 struct wsscreen *scr = v;
916
917 if (scr == NULL) /* console, before real attach */
918 return;
919
920 if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
921 return;
922
923 (void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
924 FWRITE, NULL);
925 }
926
927 void
928 wsdisplay_emulinput(v, data, count)
929 void *v;
930 const u_char *data;
931 u_int count;
932 {
933 struct wsscreen *scr = v;
934 struct tty *tp;
935
936 if (v == NULL) /* console, before real attach */
937 return;
938
939 if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
940 return;
941 if (!WSSCREEN_HAS_TTY(scr))
942 return;
943
944 tp = scr->scr_tty;
945 while (count-- > 0)
946 (*linesw[tp->t_line].l_rint)(*data++, tp);
947 };
948
949 /*
950 * Calls from the keyboard interface.
951 */
952 void
953 wsdisplay_kbdinput(dev, ks)
954 struct device *dev;
955 keysym_t ks;
956 {
957 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
958 struct wsscreen *scr;
959 char *dp;
960 int count;
961 struct tty *tp;
962
963 KASSERT(sc != NULL);
964
965 scr = sc->sc_focus;
966 KASSERT(scr != NULL);
967
968 if (!WSSCREEN_HAS_TTY(scr))
969 return;
970
971 tp = scr->scr_tty;
972
973 if (KS_GROUP(ks) == KS_GROUP_Ascii)
974 (*linesw[tp->t_line].l_rint)(KS_VALUE(ks), tp);
975 else if (WSSCREEN_HAS_EMULATOR(scr)) {
976 count = (*scr->scr_dconf->wsemul->translate)
977 (scr->scr_dconf->wsemulcookie, ks, &dp);
978 while (count-- > 0)
979 (*linesw[tp->t_line].l_rint)(*dp++, tp);
980 }
981 }
982
983 #ifdef WSDISPLAY_COMPAT_RAWKBD
984 int
985 wsdisplay_update_rawkbd(sc, scr)
986 struct wsdisplay_softc *sc;
987 struct wsscreen *scr;
988 {
989 int s, data, error;
990 s = spltty();
991
992 if (!sc->sc_kbddv ||
993 scr != sc->sc_focus ||
994 sc->sc_rawkbd == scr->scr_rawkbd) {
995 splx(s);
996 return (0);
997 }
998
999 data = (scr->scr_rawkbd ? WSKBD_RAW : WSKBD_TRANSLATED);
1000 error = wskbd_displayioctl(sc->sc_kbddv, WSKBDIO_SETMODE,
1001 (caddr_t)&data, 0, 0);
1002 if (!error)
1003 sc->sc_rawkbd = scr->scr_rawkbd;
1004 splx(s);
1005 return (error);
1006 }
1007 #endif
1008
1009 int
1010 wsdisplay_switch1(arg, waitok)
1011 void *arg;
1012 int waitok;
1013 {
1014 struct wsdisplay_softc *sc = arg;
1015 int no;
1016 struct wsscreen *scr;
1017
1018 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1019 printf("wsdisplay_switchto: not switching\n");
1020 return (EINVAL);
1021 }
1022
1023 no = sc->sc_screenwanted;
1024 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1025 panic("wsdisplay_switch1: invalid screen %d", no);
1026 scr = sc->sc_scr[no];
1027 if (!scr) {
1028 printf("wsdisplay_switch1: screen %d disappeared\n", no);
1029 sc->sc_flags &= ~SC_SWITCHPENDING;
1030 return (ENXIO);
1031 }
1032
1033 (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1034 scr->scr_dconf->emulcookie);
1035 sc->sc_focusidx = no;
1036 sc->sc_focus = scr;
1037
1038 #ifdef WSDISPLAY_COMPAT_RAWKBD
1039 (void) wsdisplay_update_rawkbd(sc, scr);
1040 #endif
1041 /* keyboard map??? */
1042
1043 if (scr->scr_syncops) {
1044 (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok);
1045 /* XXX error handling */
1046 }
1047
1048 sc->sc_flags &= ~SC_SWITCHPENDING;
1049
1050 if (scr->scr_flags & SCR_WAITACTIVE)
1051 wakeup(scr);
1052 return (0);
1053 }
1054
1055 int
1056 wsdisplay_switch(dev, no, waitok)
1057 struct device *dev;
1058 int no, waitok;
1059 {
1060 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1061 int s, res = 0;
1062 struct wsscreen *scr;
1063
1064 if (no < 0 || no >= WSDISPLAY_MAXSCREEN || !sc->sc_scr[no])
1065 return (ENXIO);
1066
1067 s = spltty();
1068
1069 if (no == sc->sc_focusidx) {
1070 splx(s);
1071 return (0);
1072 }
1073
1074 if (sc->sc_flags & SC_SWITCHPENDING) {
1075 splx(s);
1076 return (EBUSY);
1077 }
1078
1079 sc->sc_flags |= SC_SWITCHPENDING;
1080 sc->sc_screenwanted = no;
1081
1082 splx(s);
1083
1084 scr = sc->sc_focus;
1085
1086 #define wsswitch_callback ((void (*) __P((void *, int)))wsdisplay_switch1)
1087 if (scr->scr_syncops) {
1088 res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
1089 wsswitch_callback, sc);
1090 if (res == EAGAIN) {
1091 /* switch will be done asynchronously */
1092 return (0);
1093 }
1094 } else if (scr->scr_flags & SCR_GRAPHICS) {
1095 /* no way to save state */
1096 res = EBUSY;
1097 }
1098
1099 if (res) {
1100 sc->sc_flags &= ~SC_SWITCHPENDING;
1101 return (res);
1102 } else
1103 return (wsdisplay_switch1(sc, waitok));
1104 }
1105
1106 /*
1107 * Interface for (external) VT switch / process synchronization code
1108 */
1109 int
1110 wsscreen_attach_sync(scr, ops, cookie)
1111 struct wsscreen *scr;
1112 const struct wscons_syncops *ops;
1113 void *cookie;
1114 {
1115 if (scr->scr_syncops) {
1116 /*
1117 * The screen is already claimed.
1118 * Check if the owner is still alive.
1119 */
1120 if ((*scr->scr_syncops->check)(scr->scr_synccookie))
1121 return (EBUSY);
1122 }
1123 scr->scr_syncops = ops;
1124 scr->scr_synccookie = cookie;
1125 return (0);
1126 }
1127
1128 int
1129 wsscreen_detach_sync(scr)
1130 struct wsscreen *scr;
1131 {
1132 if (!scr->scr_syncops)
1133 return (EINVAL);
1134 scr->scr_syncops = 0;
1135 return (0);
1136 }
1137
1138 int
1139 wsscreen_lookup_sync(scr, ops, cookiep)
1140 struct wsscreen *scr;
1141 const struct wscons_syncops *ops; /* used as ID */
1142 void **cookiep;
1143 {
1144 if (!scr->scr_syncops || ops != scr->scr_syncops)
1145 return (EINVAL);
1146 *cookiep = scr->scr_synccookie;
1147 return (0);
1148 }
1149
1150 /*
1151 * Interface to virtual screen stuff
1152 */
1153 int
1154 wsdisplay_maxscreenidx(sc)
1155 struct wsdisplay_softc *sc;
1156 {
1157 return (WSDISPLAY_MAXSCREEN - 1);
1158 }
1159
1160 int
1161 wsdisplay_screenstate(sc, idx)
1162 struct wsdisplay_softc *sc;
1163 int idx;
1164 {
1165 if (idx >= WSDISPLAY_MAXSCREEN)
1166 return (EINVAL);
1167 if (!sc->sc_scr[idx])
1168 return (ENXIO);
1169 return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
1170 }
1171
1172 int
1173 wsdisplay_getactivescreen(sc)
1174 struct wsdisplay_softc *sc;
1175 {
1176 return (sc->sc_focusidx);
1177 }
1178
1179 int
1180 wsscreen_switchwait(sc, no)
1181 struct wsdisplay_softc *sc;
1182 int no;
1183 {
1184 struct wsscreen *scr;
1185 int s, res = 0;
1186
1187 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1188 return (ENXIO);
1189 scr = sc->sc_scr[no];
1190 if (!scr)
1191 return (ENXIO);
1192
1193 s = spltty();
1194 if (scr != sc->sc_focus) {
1195 scr->scr_flags |= SCR_WAITACTIVE;
1196 res = tsleep(scr, PCATCH, "wswait", 0);
1197 scr->scr_flags &= ~SCR_WAITACTIVE;
1198 }
1199 splx(s);
1200 return (res);
1201 }
1202
1203 void
1204 wsdisplay_kbdholdscreen(dev, hold)
1205 struct device *dev;
1206 int hold;
1207 {
1208 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1209 struct wsscreen *scr;
1210
1211 scr = sc->sc_focus;
1212
1213 if (hold)
1214 scr->scr_hold_screen = 1;
1215 else {
1216 scr->scr_hold_screen = 0;
1217 timeout(ttrstrt, scr->scr_tty, 0); /* "immediate" */
1218 }
1219 }
1220
1221 /*
1222 * Calls from the glue code.
1223 */
1224 int
1225 wsdisplay_is_console(dv)
1226 struct device *dv;
1227 {
1228 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
1229
1230 KASSERT(sc != NULL);
1231 return (sc->sc_isconsole);
1232 }
1233
1234 int
1235 wsdisplay_has_emulator(dv)
1236 struct device *dv;
1237 {
1238 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
1239 struct wsscreen *scr;
1240
1241 scr = sc->sc_focus; /* ??? */
1242
1243 KASSERT(sc != NULL);
1244 KASSERT(scr != NULL);
1245 return (WSSCREEN_HAS_EMULATOR(scr)); /* XXX XXX */
1246 }
1247
1248 struct device *
1249 wsdisplay_kbd(dv)
1250 struct device *dv;
1251 {
1252 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
1253
1254 KASSERT(sc != NULL);
1255 return (sc->sc_kbddv);
1256 }
1257
1258 void
1259 wsdisplay_set_kbd(dv, kbddv)
1260 struct device *dv, *kbddv;
1261 {
1262 struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dv;
1263
1264 KASSERT(sc != NULL);
1265 if (sc->sc_kbddv) {
1266 /* disable old keyboard */
1267 wskbd_enable(sc->sc_kbddv, 0);
1268 }
1269 if (kbddv) {
1270 /* enable new keyboard */
1271 wskbd_enable(kbddv, 1);
1272 }
1273 sc->sc_kbddv = kbddv;
1274 }
1275
1276 /*
1277 * Console interface.
1278 */
1279 void
1280 wsdisplay_cnputc(dev, i)
1281 dev_t dev;
1282 int i;
1283 {
1284 struct wsdisplay_conf *dc;
1285 char c = i;
1286
1287 if (!wsdisplay_console_initted)
1288 return;
1289
1290 if (wsdisplay_console_device != NULL &&
1291 (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
1292 return;
1293
1294 dc = &wsdisplay_console_conf;
1295 (*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
1296 }
1297
1298 static int
1299 wsdisplay_getc_dummy(dev)
1300 dev_t dev;
1301 {
1302 /* panic? */
1303 return (0);
1304 }
1305
1306 static void
1307 wsdisplay_pollc_dummy(dev, on)
1308 dev_t dev;
1309 int on;
1310 {
1311 }
1312
1313 void
1314 wsdisplay_set_cons_kbd(get, poll)
1315 int (*get) __P((dev_t));
1316 void (*poll) __P((dev_t, int));
1317 {
1318 wsdisplay_cons.cn_getc = get;
1319 wsdisplay_cons.cn_pollc = poll;
1320 }
1321