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