wsdisplay.c revision 1.138.4.4 1 /* $NetBSD: wsdisplay.c,v 1.138.4.4 2017/08/28 17:52:31 skrll 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.138.4.4 2017/08/28 17:52:31 skrll Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_wsdisplay_compat.h"
38 #include "opt_wsmsgattrs.h"
39 #endif
40
41 #include "wskbd.h"
42 #include "wsmux.h"
43 #include "wsdisplay.h"
44
45 #include <sys/param.h>
46 #include <sys/conf.h>
47 #include <sys/device.h>
48 #include <sys/ioctl.h>
49 #include <sys/poll.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/syslog.h>
54 #include <sys/systm.h>
55 #include <sys/tty.h>
56 #include <sys/signalvar.h>
57 #include <sys/errno.h>
58 #include <sys/fcntl.h>
59 #include <sys/vnode.h>
60 #include <sys/kauth.h>
61
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/wscons/wseventvar.h>
64 #include <dev/wscons/wsmuxvar.h>
65 #include <dev/wscons/wsdisplayvar.h>
66 #include <dev/wscons/wsksymvar.h>
67 #include <dev/wscons/wsksymdef.h>
68 #include <dev/wscons/wsemulvar.h>
69 #include <dev/wscons/wscons_callbacks.h>
70 #include <dev/cons.h>
71
72 #include "locators.h"
73
74 struct wsscreen_internal {
75 const struct wsdisplay_emulops *emulops;
76 void *emulcookie;
77
78 const struct wsscreen_descr *scrdata;
79
80 const struct wsemul_ops *wsemul;
81 void *wsemulcookie;
82 };
83
84 struct wsscreen {
85 struct wsscreen_internal *scr_dconf;
86
87 struct tty *scr_tty;
88 int scr_hold_screen; /* hold tty output */
89
90 int scr_flags;
91 #define SCR_OPEN 1 /* is it open? */
92 #define SCR_WAITACTIVE 2 /* someone waiting on activation */
93 #define SCR_GRAPHICS 4 /* graphics mode, no text (emulation) output */
94 #define SCR_DUMBFB 8 /* in use as a dumb fb (iff SCR_GRAPHICS) */
95 const struct wscons_syncops *scr_syncops;
96 void *scr_synccookie;
97
98 #ifdef WSDISPLAY_COMPAT_RAWKBD
99 int scr_rawkbd;
100 #endif
101
102 struct wsdisplay_softc *sc;
103
104 #ifdef DIAGNOSTIC
105 /* XXX this is to support a hack in emulinput, see comment below */
106 int scr_in_ttyoutput;
107 #endif
108 };
109
110 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int,
111 const char *,
112 const struct wsscreen_descr *, void *,
113 int, int, long);
114 void wsscreen_detach(struct wsscreen *);
115 int wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *, const char *);
116 static void wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
117 static void wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
118 int wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
119
120 #define WSDISPLAY_MAXSCREEN 8
121
122 struct wsdisplay_softc {
123 device_t sc_dev;
124
125 const struct wsdisplay_accessops *sc_accessops;
126 void *sc_accesscookie;
127
128 const struct wsscreen_list *sc_scrdata;
129 #ifdef WSDISPLAY_SCROLLSUPPORT
130 struct wsdisplay_scroll_data sc_scroll_values;
131 #endif
132
133 struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
134 int sc_focusidx; /* available only if sc_focus isn't null */
135 struct wsscreen *sc_focus;
136
137 struct wseventvar evar;
138
139 int sc_isconsole;
140
141 int sc_flags;
142 #define SC_SWITCHPENDING 1
143 #define SC_SWITCHERROR 2
144 #define SC_XATTACHED 4 /* X server active */
145 kmutex_t sc_flagsmtx; /* for flags, might also be used for focus */
146 kcondvar_t sc_flagscv;
147
148 int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
149
150 #if NWSKBD > 0
151 struct wsevsrc *sc_input;
152 #ifdef WSDISPLAY_COMPAT_RAWKBD
153 int sc_rawkbd;
154 #endif
155 #endif /* NWSKBD > 0 */
156 };
157
158 #ifdef WSDISPLAY_SCROLLSUPPORT
159
160 struct wsdisplay_scroll_data wsdisplay_default_scroll_values = {
161 WSDISPLAY_SCROLL_DOALL,
162 25,
163 2,
164 };
165 #endif
166
167 extern struct cfdriver wsdisplay_cd;
168
169 /* Autoconfiguration definitions. */
170 static int wsdisplay_emul_match(device_t , cfdata_t, void *);
171 static void wsdisplay_emul_attach(device_t, device_t, void *);
172 static int wsdisplay_emul_detach(device_t, int);
173 static int wsdisplay_noemul_match(device_t, cfdata_t, void *);
174 static void wsdisplay_noemul_attach(device_t, device_t, void *);
175 static bool wsdisplay_suspend(device_t, const pmf_qual_t *);
176
177 CFATTACH_DECL_NEW(wsdisplay_emul, sizeof (struct wsdisplay_softc),
178 wsdisplay_emul_match, wsdisplay_emul_attach, wsdisplay_emul_detach, NULL);
179
180 CFATTACH_DECL_NEW(wsdisplay_noemul, sizeof (struct wsdisplay_softc),
181 wsdisplay_noemul_match, wsdisplay_noemul_attach, NULL, NULL);
182
183 dev_type_open(wsdisplayopen);
184 dev_type_close(wsdisplayclose);
185 dev_type_read(wsdisplayread);
186 dev_type_write(wsdisplaywrite);
187 dev_type_ioctl(wsdisplayioctl);
188 dev_type_stop(wsdisplaystop);
189 dev_type_tty(wsdisplaytty);
190 dev_type_poll(wsdisplaypoll);
191 dev_type_mmap(wsdisplaymmap);
192 dev_type_kqfilter(wsdisplaykqfilter);
193
194 const struct cdevsw wsdisplay_cdevsw = {
195 .d_open = wsdisplayopen,
196 .d_close = wsdisplayclose,
197 .d_read = wsdisplayread,
198 .d_write = wsdisplaywrite,
199 .d_ioctl = wsdisplayioctl,
200 .d_stop = wsdisplaystop,
201 .d_tty = wsdisplaytty,
202 .d_poll = wsdisplaypoll,
203 .d_mmap = wsdisplaymmap,
204 .d_kqfilter = wsdisplaykqfilter,
205 .d_discard = nodiscard,
206 .d_flag = D_TTY
207 };
208
209 static void wsdisplaystart(struct tty *);
210 static int wsdisplayparam(struct tty *, struct termios *);
211
212
213 #define WSDISPLAYUNIT(dev) (minor(dev) >> 8)
214 #define WSDISPLAYSCREEN(dev) (minor(dev) & 0xff)
215 #define ISWSDISPLAYSTAT(dev) (WSDISPLAYSCREEN(dev) == 254)
216 #define ISWSDISPLAYCTL(dev) (WSDISPLAYSCREEN(dev) == 255)
217 #define WSDISPLAYMINOR(unit, screen) (((unit) << 8) | (screen))
218
219 #define WSSCREEN_HAS_EMULATOR(scr) ((scr)->scr_dconf->wsemul != NULL)
220 #define WSSCREEN_HAS_TTY(scr) ((scr)->scr_tty != NULL)
221
222 static void wsdisplay_common_attach(struct wsdisplay_softc *sc,
223 int console, int kbdmux, const struct wsscreen_list *,
224 const struct wsdisplay_accessops *accessops,
225 void *accesscookie);
226
227 #ifdef WSDISPLAY_COMPAT_RAWKBD
228 int wsdisplay_update_rawkbd(struct wsdisplay_softc *,
229 struct wsscreen *);
230 #endif
231
232 static int wsdisplay_console_initted;
233 static int wsdisplay_console_attached;
234 static struct wsdisplay_softc *wsdisplay_console_device;
235 static struct wsscreen_internal wsdisplay_console_conf;
236
237 static int wsdisplay_getc_dummy(dev_t);
238 static void wsdisplay_pollc(dev_t, int);
239
240 static int wsdisplay_cons_pollmode;
241 static void (*wsdisplay_cons_kbd_pollc)(dev_t, int);
242
243 static struct consdev wsdisplay_cons = {
244 NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
245 wsdisplay_pollc, NULL, NULL, NULL, NODEV, CN_NORMAL
246 };
247
248 #ifndef WSDISPLAY_DEFAULTSCREENS
249 # define WSDISPLAY_DEFAULTSCREENS 0
250 #endif
251 int wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
252
253 static int wsdisplay_switch1(device_t, int, int);
254 static void wsdisplay_switch1_cb(void *, int, int);
255 static int wsdisplay_switch2(device_t, int, int);
256 static void wsdisplay_switch2_cb(void *, int, int);
257 static int wsdisplay_switch3(device_t, int, int);
258 static void wsdisplay_switch3_cb(void *, int, int);
259
260 static void wsdisplay_swdone_cb(void *, int, int);
261 static int wsdisplay_dosync(struct wsdisplay_softc *, int);
262
263 int wsdisplay_clearonclose;
264
265 struct wsscreen *
266 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
267 const struct wsscreen_descr *type, void *cookie, int ccol,
268 int crow, long defattr)
269 {
270 struct wsscreen_internal *dconf;
271 struct wsscreen *scr;
272
273 scr = malloc(sizeof(struct wsscreen), M_DEVBUF, M_WAITOK);
274 if (!scr)
275 return (NULL);
276
277 if (console) {
278 dconf = &wsdisplay_console_conf;
279 /*
280 * If there's an emulation, tell it about the callback argument.
281 * The other stuff is already there.
282 */
283 if (dconf->wsemul != NULL)
284 (*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
285 } else { /* not console */
286 dconf = malloc(sizeof(struct wsscreen_internal),
287 M_DEVBUF, M_NOWAIT);
288 dconf->emulops = type->textops;
289 dconf->emulcookie = cookie;
290 if (dconf->emulops) {
291 dconf->wsemul = wsemul_pick(emul);
292 if (dconf->wsemul == NULL) {
293 free(dconf, M_DEVBUF);
294 free(scr, M_DEVBUF);
295 return (NULL);
296 }
297 dconf->wsemulcookie =
298 (*dconf->wsemul->attach)(0, type, cookie,
299 ccol, crow, scr, defattr);
300 } else
301 dconf->wsemul = NULL;
302 dconf->scrdata = type;
303 }
304
305 scr->scr_dconf = dconf;
306
307 scr->scr_tty = tty_alloc();
308 tty_attach(scr->scr_tty);
309 scr->scr_hold_screen = 0;
310 if (WSSCREEN_HAS_EMULATOR(scr))
311 scr->scr_flags = 0;
312 else
313 scr->scr_flags = SCR_GRAPHICS;
314
315 scr->scr_syncops = 0;
316 scr->sc = sc;
317 #ifdef WSDISPLAY_COMPAT_RAWKBD
318 scr->scr_rawkbd = 0;
319 #endif
320 return (scr);
321 }
322
323 void
324 wsscreen_detach(struct wsscreen *scr)
325 {
326 u_int ccol, crow; /* XXX */
327
328 if (WSSCREEN_HAS_TTY(scr)) {
329 tty_detach(scr->scr_tty);
330 tty_free(scr->scr_tty);
331 }
332 if (WSSCREEN_HAS_EMULATOR(scr)) {
333 (*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
334 &ccol, &crow);
335 wsemul_drop(scr->scr_dconf->wsemul);
336 }
337 if (scr->scr_dconf->scrdata->capabilities & WSSCREEN_FREE)
338 free(__UNCONST(scr->scr_dconf->scrdata), M_DEVBUF);
339 free(scr->scr_dconf, M_DEVBUF);
340 free(scr, M_DEVBUF);
341 }
342
343 const struct wsscreen_descr *
344 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
345 {
346 int i;
347 const struct wsscreen_descr *scr;
348
349 KASSERT(scrdata->nscreens > 0);
350 if (name == NULL)
351 return (scrdata->screens[0]);
352
353 for (i = 0; i < scrdata->nscreens; i++) {
354 scr = scrdata->screens[i];
355 if (!strcmp(name, scr->name))
356 return (scr);
357 }
358
359 return (0);
360 }
361
362 /*
363 * print info about attached screen
364 */
365 static void
366 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
367 {
368 aprint_verbose_dev(sc->sc_dev, "screen %d", idx);
369 if (count > 1)
370 aprint_verbose("-%d", idx + (count-1));
371 aprint_verbose(" added (%s", sc->sc_scr[idx]->scr_dconf->scrdata->name);
372 if (WSSCREEN_HAS_EMULATOR(sc->sc_scr[idx])) {
373 aprint_verbose(", %s emulation",
374 sc->sc_scr[idx]->scr_dconf->wsemul->name);
375 }
376 aprint_verbose(")\n");
377 }
378
379 int
380 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
381 const char *screentype, const char *emul)
382 {
383 const struct wsscreen_descr *scrdesc;
384 struct wsscreen_descr *scrdescr2;
385 int error;
386 void *cookie;
387 int ccol, crow;
388 long defattr;
389 struct wsscreen *scr;
390 int s;
391
392 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
393 return (EINVAL);
394 if (sc->sc_scr[idx] != NULL)
395 return (EBUSY);
396 scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
397 if (!scrdesc)
398 return (ENXIO);
399
400 /*
401 * if this screen can resize we need to copy the descr so each screen
402 * gets its own
403 */
404 if (scrdesc->capabilities & WSSCREEN_RESIZE) {
405 /* we want per screen wsscreen_descr */
406 scrdescr2 = malloc(sizeof(struct wsscreen_descr), M_DEVBUF, M_NOWAIT);
407 if (scrdescr2 == NULL)
408 return ENOMEM;
409 memcpy(scrdescr2, scrdesc, sizeof(struct wsscreen_descr));
410 scrdescr2->capabilities |= WSSCREEN_FREE;
411 scrdesc = scrdescr2;
412 }
413
414 error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
415 scrdesc, &cookie, &ccol, &crow, &defattr);
416 if (error)
417 return (error);
418
419 scr = wsscreen_attach(sc, 0, emul, scrdesc,
420 cookie, ccol, crow, defattr);
421 if (scr == NULL) {
422 (*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
423 cookie);
424 return (ENXIO);
425 }
426
427 sc->sc_scr[idx] = scr;
428
429 /* if no screen has focus yet, activate the first we get */
430 s = spltty();
431 if (!sc->sc_focus) {
432 (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
433 scr->scr_dconf->emulcookie,
434 0, 0, 0);
435 sc->sc_focusidx = idx;
436 sc->sc_focus = scr;
437 }
438 splx(s);
439 return (0);
440 }
441
442 static void
443 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
444 {
445 int maj, mn, idx;
446
447 /* hangup */
448 if (WSSCREEN_HAS_TTY(scr)) {
449 struct tty *tp = scr->scr_tty;
450 (*tp->t_linesw->l_modem)(tp, 0);
451 }
452
453 /* locate the major number */
454 maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
455 /* locate the screen index */
456 for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
457 if (scr == sc->sc_scr[idx])
458 break;
459 #ifdef DIAGNOSTIC
460 if (idx == WSDISPLAY_MAXSCREEN)
461 panic("wsdisplay_forceclose: bad screen");
462 #endif
463
464 /* nuke the vnodes */
465 mn = WSDISPLAYMINOR(device_unit(sc->sc_dev), idx);
466 vdevgone(maj, mn, mn, VCHR);
467 }
468
469 #ifdef WSDISPLAY_SCROLLSUPPORT
470 void
471 wsdisplay_scroll(void *arg, int op)
472 {
473 device_t dv = arg;
474 struct wsdisplay_softc *sc = device_private(dv);
475 struct wsscreen *scr;
476 int lines;
477
478 scr = sc->sc_focus;
479
480 if (!scr)
481 return;
482
483 if (op == WSDISPLAY_SCROLL_RESET)
484 lines = 0;
485 else {
486 lines = (op & WSDISPLAY_SCROLL_LOW) ?
487 sc->sc_scroll_values.slowlines :
488 sc->sc_scroll_values.fastlines;
489 if (op & WSDISPLAY_SCROLL_BACKWARD)
490 lines = -(lines);
491 }
492
493 if (sc->sc_accessops->scroll) {
494 (*sc->sc_accessops->scroll)(sc->sc_accesscookie,
495 sc->sc_focus->scr_dconf->emulcookie, lines);
496 }
497 }
498 #endif
499
500 int
501 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
502 {
503 struct wsscreen *scr;
504 int s;
505 void *cookie;
506
507 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
508 return (EINVAL);
509 if ((scr = sc->sc_scr[idx]) == NULL)
510 return (ENXIO);
511
512 if (scr->scr_dconf == &wsdisplay_console_conf ||
513 scr->scr_syncops ||
514 ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
515 return(EBUSY);
516
517 wsdisplay_closescreen(sc, scr);
518
519 /*
520 * delete pointers, so neither device entries
521 * nor keyboard input can reference it anymore
522 */
523 s = spltty();
524 if (sc->sc_focus == scr) {
525 sc->sc_focus = 0;
526 #ifdef WSDISPLAY_COMPAT_RAWKBD
527 wsdisplay_update_rawkbd(sc, 0);
528 #endif
529 }
530 sc->sc_scr[idx] = 0;
531 splx(s);
532
533 /*
534 * Wake up processes waiting for the screen to
535 * be activated. Sleepers must check whether
536 * the screen still exists.
537 */
538 if (scr->scr_flags & SCR_WAITACTIVE)
539 wakeup(scr);
540
541 /* save a reference to the graphics screen */
542 cookie = scr->scr_dconf->emulcookie;
543
544 wsscreen_detach(scr);
545
546 (*sc->sc_accessops->free_screen)(sc->sc_accesscookie,
547 cookie);
548
549 aprint_verbose_dev(sc->sc_dev, "screen %d deleted\n", idx);
550 return (0);
551 }
552
553 /*
554 * Autoconfiguration functions.
555 */
556 int
557 wsdisplay_emul_match(device_t parent, cfdata_t match, void *aux)
558 {
559 struct wsemuldisplaydev_attach_args *ap = aux;
560
561 if (match->cf_loc[WSEMULDISPLAYDEVCF_CONSOLE] !=
562 WSEMULDISPLAYDEVCF_CONSOLE_DEFAULT) {
563 /*
564 * If console-ness of device specified, either match
565 * exactly (at high priority), or fail.
566 */
567 if (match->cf_loc[WSEMULDISPLAYDEVCF_CONSOLE] != 0 &&
568 ap->console != 0)
569 return (10);
570 else
571 return (0);
572 }
573
574 /* If console-ness unspecified, it wins. */
575 return (1);
576 }
577
578 void
579 wsdisplay_emul_attach(device_t parent, device_t self, void *aux)
580 {
581 struct wsdisplay_softc *sc = device_private(self);
582 struct wsemuldisplaydev_attach_args *ap = aux;
583
584 sc->sc_dev = self;
585
586 /* Don't allow more than one console to attach */
587 if (wsdisplay_console_attached && ap->console)
588 ap->console = 0;
589
590 wsdisplay_common_attach(sc, ap->console,
591 device_cfdata(self)->cf_loc[WSEMULDISPLAYDEVCF_KBDMUX],
592 ap->scrdata, ap->accessops, ap->accesscookie);
593
594 if (ap->console) {
595 int maj;
596
597 /* locate the major number */
598 maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
599
600 cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(device_unit(self),
601 0));
602 }
603 }
604
605 /* Print function (for parent devices). */
606 int
607 wsemuldisplaydevprint(void *aux, const char *pnp)
608 {
609 #if 0 /* -Wunused */
610 struct wsemuldisplaydev_attach_args *ap = aux;
611 #endif
612
613 if (pnp)
614 aprint_normal("wsdisplay at %s", pnp);
615 #if 0 /* don't bother; it's ugly */
616 aprint_normal(" console %d", ap->console);
617 #endif
618
619 return (UNCONF);
620 }
621
622 int
623 wsdisplay_emul_detach(device_t dev, int how)
624 {
625 struct wsdisplay_softc *sc = device_private(dev);
626 int flag, i, res;
627
628 flag = (how & DETACH_FORCE ? WSDISPLAY_DELSCR_FORCE : 0);
629 for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
630 if (sc->sc_scr[i]) {
631 res = wsdisplay_delscreen(sc, i, flag);
632 if (res)
633 return res;
634 }
635
636 cv_destroy(&sc->sc_flagscv);
637 mutex_destroy(&sc->sc_flagsmtx);
638 return 0;
639 }
640
641 int
642 wsdisplay_noemul_match(device_t parent, cfdata_t match, void *aux)
643 {
644 #if 0 /* -Wunused */
645 struct wsdisplaydev_attach_args *ap = aux;
646 #endif
647
648 /* Always match. */
649 return (1);
650 }
651
652 void
653 wsdisplay_noemul_attach(device_t parent, device_t self, void *aux)
654 {
655 struct wsdisplay_softc *sc = device_private(self);
656 struct wsdisplaydev_attach_args *ap = aux;
657
658 sc->sc_dev = self;
659
660 wsdisplay_common_attach(sc, 0,
661 device_cfdata(self)->cf_loc[WSDISPLAYDEVCF_KBDMUX], NULL,
662 ap->accessops, ap->accesscookie);
663 }
664
665 static void
666 wsdisplay_swdone_cb(void *arg, int error, int waitok)
667 {
668 struct wsdisplay_softc *sc = arg;
669
670 mutex_enter(&sc->sc_flagsmtx);
671 KASSERT(sc->sc_flags & SC_SWITCHPENDING);
672 if (error)
673 sc->sc_flags |= SC_SWITCHERROR;
674 sc->sc_flags &= ~SC_SWITCHPENDING;
675 cv_signal(&sc->sc_flagscv);
676 mutex_exit(&sc->sc_flagsmtx);
677 }
678
679 static int
680 wsdisplay_dosync(struct wsdisplay_softc *sc, int attach)
681 {
682 struct wsscreen *scr;
683 int (*op)(void *, int, void (*)(void *, int, int), void *);
684 int res;
685
686 scr = sc->sc_focus;
687 if (!scr || !scr->scr_syncops)
688 return 0; /* XXX check SCR_GRAPHICS? */
689
690 sc->sc_flags |= SC_SWITCHPENDING;
691 sc->sc_flags &= ~SC_SWITCHERROR;
692 if (attach)
693 op = scr->scr_syncops->attach;
694 else
695 op = scr->scr_syncops->detach;
696 res = (*op)(scr->scr_synccookie, 1, wsdisplay_swdone_cb, sc);
697 if (res == EAGAIN) {
698 /* wait for callback */
699 mutex_enter(&sc->sc_flagsmtx);
700 while (sc->sc_flags & SC_SWITCHPENDING)
701 cv_wait_sig(&sc->sc_flagscv, &sc->sc_flagsmtx);
702 mutex_exit(&sc->sc_flagsmtx);
703 if (sc->sc_flags & SC_SWITCHERROR)
704 return (EIO); /* XXX pass real error */
705 } else {
706 sc->sc_flags &= ~SC_SWITCHPENDING;
707 if (res)
708 return (res);
709 }
710 if (attach)
711 sc->sc_flags |= SC_XATTACHED;
712 else
713 sc->sc_flags &= ~SC_XATTACHED;
714 return 0;
715 }
716
717 int
718 wsdisplay_handlex(int resume)
719 {
720 int i, res;
721 device_t dv;
722
723 for (i = 0; i < wsdisplay_cd.cd_ndevs; i++) {
724 dv = device_lookup(&wsdisplay_cd, i);
725 if (!dv)
726 continue;
727 res = wsdisplay_dosync(device_private(dv), resume);
728 if (res)
729 return (res);
730 }
731 return (0);
732 }
733
734 static bool
735 wsdisplay_suspend(device_t dv, const pmf_qual_t *qual)
736 {
737 struct wsdisplay_softc *sc = device_private(dv);
738 #ifdef DIAGNOSTIC
739 struct wsscreen *scr = sc->sc_focus;
740 if (sc->sc_flags & SC_XATTACHED) {
741 KASSERT(scr && scr->scr_syncops);
742 }
743 #endif
744 #if 1
745 /*
746 * XXX X servers should have been detached earlier.
747 * pmf currently ignores our return value and suspends the system
748 * after device suspend failures. We try to avoid bigger damage
749 * and try to detach the X server here. This is not safe because
750 * other parts of the system which the X server deals with
751 * might already be suspended.
752 */
753 if (sc->sc_flags & SC_XATTACHED) {
754 printf("%s: emergency X server detach\n", device_xname(dv));
755 wsdisplay_dosync(sc, 0);
756 }
757 #endif
758 return (!(sc->sc_flags & SC_XATTACHED));
759 }
760
761 /* Print function (for parent devices). */
762 int
763 wsdisplaydevprint(void *aux, const char *pnp)
764 {
765 #if 0 /* -Wunused */
766 struct wsdisplaydev_attach_args *ap = aux;
767 #endif
768
769 if (pnp)
770 aprint_normal("wsdisplay at %s", pnp);
771
772 return (UNCONF);
773 }
774
775 static void
776 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
777 const struct wsscreen_list *scrdata,
778 const struct wsdisplay_accessops *accessops,
779 void *accesscookie)
780 {
781 int i, start=0;
782 #if NWSKBD > 0
783 struct wsevsrc *kme;
784 #if NWSMUX > 0
785 struct wsmux_softc *mux;
786
787 if (kbdmux >= 0)
788 mux = wsmux_getmux(kbdmux);
789 else
790 mux = wsmux_create("dmux", device_unit(sc->sc_dev));
791 /* XXX panic()ing isn't nice, but attach cannot fail */
792 if (mux == NULL)
793 panic("wsdisplay_common_attach: no memory");
794 sc->sc_input = &mux->sc_base;
795 mux->sc_base.me_dispdv = sc->sc_dev;
796 aprint_normal(" kbdmux %d", kbdmux);
797 #else
798 if (kbdmux >= 0)
799 aprint_normal(" (kbdmux ignored)");
800 #endif
801 #endif
802
803 sc->sc_isconsole = console;
804
805 if (console) {
806 KASSERT(wsdisplay_console_initted);
807 KASSERT(wsdisplay_console_device == NULL);
808
809 sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
810 wsdisplay_console_device = sc;
811
812 aprint_normal(": console (%s, %s emulation)",
813 wsdisplay_console_conf.scrdata->name,
814 wsdisplay_console_conf.wsemul->name);
815
816 #if NWSKBD > 0
817 kme = wskbd_set_console_display(sc->sc_dev, sc->sc_input);
818 if (kme != NULL)
819 aprint_normal(", using %s", device_xname(kme->me_dv));
820 #if NWSMUX == 0
821 sc->sc_input = kme;
822 #endif
823 #endif
824
825 sc->sc_focusidx = 0;
826 sc->sc_focus = sc->sc_scr[0];
827 start = 1;
828
829 wsdisplay_console_attached = 1;
830 }
831 aprint_normal("\n");
832 aprint_naive("\n");
833
834 #if NWSKBD > 0 && NWSMUX > 0
835 wsmux_set_display(mux, sc->sc_dev);
836 #endif
837
838 mutex_init(&sc->sc_flagsmtx, MUTEX_DEFAULT, IPL_NONE);
839 cv_init(&sc->sc_flagscv, "wssw");
840
841 sc->sc_accessops = accessops;
842 sc->sc_accesscookie = accesscookie;
843 sc->sc_scrdata = scrdata;
844
845 #ifdef WSDISPLAY_SCROLLSUPPORT
846 sc->sc_scroll_values = wsdisplay_default_scroll_values;
847 #endif
848
849 /*
850 * Set up a number of virtual screens if wanted. The
851 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
852 * is for special cases like installation kernels.
853 */
854 for (i = start; i < wsdisplay_defaultscreens; i++) {
855 if (wsdisplay_addscreen(sc, i, 0, 0))
856 break;
857 }
858
859 if (i > start)
860 wsdisplay_addscreen_print(sc, start, i-start);
861
862 if (!pmf_device_register(sc->sc_dev, wsdisplay_suspend, NULL))
863 aprint_error_dev(sc->sc_dev,
864 "couldn't establish power handler\n");
865 }
866
867 void
868 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie,
869 int ccol, int crow, long defattr)
870 {
871 const struct wsemul_ops *wsemul;
872
873 KASSERT(wsdisplay_console_initted < 2);
874 KASSERT(type->nrows > 0);
875 KASSERT(type->ncols > 0);
876 KASSERT(crow < type->nrows);
877 KASSERT(ccol < type->ncols);
878
879 wsdisplay_console_conf.emulops = type->textops;
880 wsdisplay_console_conf.emulcookie = cookie;
881 wsdisplay_console_conf.scrdata = type;
882
883 wsemul = wsemul_pick(0); /* default */
884 wsdisplay_console_conf.wsemul = wsemul;
885 wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
886 ccol, crow,
887 defattr);
888
889 cn_tab = &wsdisplay_cons;
890 wsdisplay_console_initted = 2;
891 }
892
893 void
894 wsdisplay_preattach(const struct wsscreen_descr *type, void *cookie,
895 int ccol, int crow, long defattr)
896 {
897 const struct wsemul_ops *wsemul;
898
899 KASSERT(!wsdisplay_console_initted);
900 KASSERT(type->nrows > 0);
901 KASSERT(type->ncols > 0);
902 KASSERT(crow < type->nrows);
903 KASSERT(ccol < type->ncols);
904
905 wsdisplay_console_conf.emulops = type->textops;
906 wsdisplay_console_conf.emulcookie = cookie;
907 wsdisplay_console_conf.scrdata = type;
908
909 wsemul = wsemul_pick(0); /* default */
910 wsdisplay_console_conf.wsemul = wsemul;
911 wsdisplay_console_conf.wsemulcookie = (*wsemul->cnattach)(type, cookie,
912 ccol, crow,
913 defattr);
914
915 cn_tab = &wsdisplay_cons;
916 wsdisplay_console_initted = 1;
917 }
918
919 void
920 wsdisplay_cndetach(void)
921 {
922 KASSERT(wsdisplay_console_initted == 2);
923
924 cn_tab = NULL;
925 wsdisplay_console_initted = 0;
926 }
927
928 /*
929 * Tty and cdevsw functions.
930 */
931 int
932 wsdisplayopen(dev_t dev, int flag, int mode, struct lwp *l)
933 {
934 struct wsdisplay_softc *sc;
935 struct tty *tp;
936 int newopen, error;
937 struct wsscreen *scr;
938
939 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
940 if (sc == NULL) /* make sure it was attached */
941 return (ENXIO);
942
943 if (ISWSDISPLAYSTAT(dev)) {
944 wsevent_init(&sc->evar, l->l_proc);
945 return (0);
946 }
947
948 if (ISWSDISPLAYCTL(dev))
949 return (0);
950
951 if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
952 return (ENXIO);
953 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
954 return (ENXIO);
955
956 if (WSSCREEN_HAS_TTY(scr)) {
957 tp = scr->scr_tty;
958 tp->t_oproc = wsdisplaystart;
959 tp->t_param = wsdisplayparam;
960 tp->t_dev = dev;
961 newopen = (tp->t_state & TS_ISOPEN) == 0;
962
963 if (kauth_authorize_device_tty(l->l_cred,
964 KAUTH_DEVICE_TTY_OPEN, tp))
965 return (EBUSY);
966
967 if (newopen) {
968 ttychars(tp);
969 tp->t_iflag = TTYDEF_IFLAG;
970 tp->t_oflag = TTYDEF_OFLAG;
971 tp->t_cflag = TTYDEF_CFLAG;
972 tp->t_lflag = TTYDEF_LFLAG;
973 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
974 wsdisplayparam(tp, &tp->t_termios);
975 ttsetwater(tp);
976 }
977 tp->t_state |= TS_CARR_ON;
978
979 error = ((*tp->t_linesw->l_open)(dev, tp));
980 if (error)
981 return (error);
982
983 if (newopen && WSSCREEN_HAS_EMULATOR(scr)) {
984 /* set window sizes as appropriate, and reset
985 the emulation */
986 tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
987 tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
988
989 /* wsdisplay_set_emulation() */
990 }
991 }
992
993 scr->scr_flags |= SCR_OPEN;
994 return (0);
995 }
996
997 int
998 wsdisplayclose(dev_t dev, int flag, int mode, struct lwp *l)
999 {
1000 device_t dv;
1001 struct wsdisplay_softc *sc;
1002 struct tty *tp;
1003 struct wsscreen *scr;
1004
1005 dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1006 sc = device_private(dv);
1007
1008 if (ISWSDISPLAYSTAT(dev)) {
1009 wsevent_fini(&sc->evar);
1010 return (0);
1011 }
1012
1013 if (ISWSDISPLAYCTL(dev))
1014 return (0);
1015
1016 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1017 return (0);
1018
1019 if (WSSCREEN_HAS_TTY(scr)) {
1020 if (scr->scr_hold_screen) {
1021 int s;
1022
1023 /* XXX RESET KEYBOARD LEDS, etc. */
1024 s = spltty(); /* avoid conflict with keyboard */
1025 wsdisplay_kbdholdscreen(dv, 0);
1026 splx(s);
1027 }
1028 tp = scr->scr_tty;
1029 (*tp->t_linesw->l_close)(tp, flag);
1030 ttyclose(tp);
1031 }
1032
1033 if (scr->scr_syncops)
1034 (*scr->scr_syncops->destroy)(scr->scr_synccookie);
1035
1036 if (WSSCREEN_HAS_EMULATOR(scr)) {
1037 scr->scr_flags &= ~SCR_GRAPHICS;
1038 (*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
1039 WSEMUL_RESET);
1040 if (wsdisplay_clearonclose)
1041 (*scr->scr_dconf->wsemul->reset)
1042 (scr->scr_dconf->wsemulcookie,
1043 WSEMUL_CLEARSCREEN);
1044 }
1045
1046 #ifdef WSDISPLAY_COMPAT_RAWKBD
1047 if (scr->scr_rawkbd) {
1048 int kbmode = WSKBD_TRANSLATED;
1049 (void)wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
1050 (void *)&kbmode, 0, l);
1051 }
1052 #endif
1053
1054 scr->scr_flags &= ~SCR_OPEN;
1055
1056 return (0);
1057 }
1058
1059 int
1060 wsdisplayread(dev_t dev, struct uio *uio, int flag)
1061 {
1062 struct wsdisplay_softc *sc;
1063 struct tty *tp;
1064 struct wsscreen *scr;
1065 int error;
1066
1067 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1068
1069 if (ISWSDISPLAYSTAT(dev)) {
1070 error = wsevent_read(&sc->evar, uio, flag);
1071 return (error);
1072 }
1073
1074 if (ISWSDISPLAYCTL(dev))
1075 return (0);
1076
1077 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1078 return (ENXIO);
1079
1080 if (!WSSCREEN_HAS_TTY(scr))
1081 return (ENODEV);
1082
1083 tp = scr->scr_tty;
1084 return ((*tp->t_linesw->l_read)(tp, uio, flag));
1085 }
1086
1087 int
1088 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
1089 {
1090 struct wsdisplay_softc *sc;
1091 struct tty *tp;
1092 struct wsscreen *scr;
1093
1094 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1095
1096 if (ISWSDISPLAYSTAT(dev)) {
1097 return (0);
1098 }
1099
1100 if (ISWSDISPLAYCTL(dev))
1101 return (0);
1102
1103 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1104 return (ENXIO);
1105
1106 if (!WSSCREEN_HAS_TTY(scr))
1107 return (ENODEV);
1108
1109 tp = scr->scr_tty;
1110 return ((*tp->t_linesw->l_write)(tp, uio, flag));
1111 }
1112
1113 int
1114 wsdisplaypoll(dev_t dev, int events, struct lwp *l)
1115 {
1116 struct wsdisplay_softc *sc;
1117 struct tty *tp;
1118 struct wsscreen *scr;
1119
1120 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1121
1122 if (ISWSDISPLAYSTAT(dev))
1123 return (wsevent_poll(&sc->evar, events, l));
1124
1125 if (ISWSDISPLAYCTL(dev))
1126 return (0);
1127
1128 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1129 return (POLLHUP);
1130
1131 if (!WSSCREEN_HAS_TTY(scr))
1132 return (POLLERR);
1133
1134 tp = scr->scr_tty;
1135 return ((*tp->t_linesw->l_poll)(tp, events, l));
1136 }
1137
1138 int
1139 wsdisplaykqfilter(dev_t dev, struct knote *kn)
1140 {
1141 struct wsdisplay_softc *sc;
1142 struct wsscreen *scr;
1143
1144 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1145
1146 if (ISWSDISPLAYCTL(dev))
1147 return (1);
1148
1149 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1150 return (1);
1151
1152
1153 if (WSSCREEN_HAS_TTY(scr))
1154 return (ttykqfilter(dev, kn));
1155 else
1156 return (1);
1157 }
1158
1159 struct tty *
1160 wsdisplaytty(dev_t dev)
1161 {
1162 struct wsdisplay_softc *sc;
1163 struct wsscreen *scr;
1164
1165 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1166
1167 if (ISWSDISPLAYSTAT(dev))
1168 panic("wsdisplaytty() on status device");
1169
1170 if (ISWSDISPLAYCTL(dev))
1171 panic("wsdisplaytty() on ctl device");
1172
1173 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1174 return NULL;
1175
1176 return (scr->scr_tty);
1177 }
1178
1179 int
1180 wsdisplayioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1181 {
1182 device_t dv;
1183 struct wsdisplay_softc *sc;
1184 struct tty *tp;
1185 int error;
1186 struct wsscreen *scr;
1187
1188 dv = device_lookup(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1189 sc = device_private(dv);
1190
1191 #ifdef WSDISPLAY_COMPAT_USL
1192 error = wsdisplay_usl_ioctl1(dv, cmd, data, flag, l);
1193 if (error != EPASSTHROUGH)
1194 return (error);
1195 #endif
1196
1197 if (ISWSDISPLAYSTAT(dev))
1198 return (wsdisplay_stat_ioctl(sc, cmd, data, flag, l));
1199
1200 if (ISWSDISPLAYCTL(dev))
1201 return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, l));
1202
1203 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1204 return (ENXIO);
1205
1206 if (WSSCREEN_HAS_TTY(scr)) {
1207 tp = scr->scr_tty;
1208
1209 /* do the line discipline ioctls first */
1210 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1211 if (error != EPASSTHROUGH)
1212 return (error);
1213
1214 /* then the tty ioctls */
1215 error = ttioctl(tp, cmd, data, flag, l);
1216 if (error != EPASSTHROUGH)
1217 return (error);
1218 }
1219
1220 #ifdef WSDISPLAY_COMPAT_USL
1221 error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, l);
1222 if (error != EPASSTHROUGH)
1223 return (error);
1224 #endif
1225
1226 return (wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, l));
1227 }
1228
1229 int
1230 wsdisplay_param(device_t dv, u_long cmd, struct wsdisplay_param *dp)
1231 {
1232 struct wsdisplay_softc *sc = device_private(dv);
1233 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1234 sc->sc_focus->scr_dconf->emulcookie,
1235 cmd, (void *)dp, 0, NULL));
1236 }
1237
1238 int
1239 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
1240 u_long cmd, void *data, int flag, struct lwp *l)
1241 {
1242 int error;
1243 char namebuf[32];
1244 struct wsdisplay_font fd;
1245 #ifdef WSDISPLAY_SCROLLSUPPORT
1246 struct wsdisplay_scroll_data *ksdp, *usdp;
1247 #endif
1248
1249 #if NWSKBD > 0
1250 struct wsevsrc *inp;
1251
1252 #ifdef WSDISPLAY_COMPAT_RAWKBD
1253 switch (cmd) {
1254 case WSKBDIO_SETMODE:
1255 scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
1256 return (wsdisplay_update_rawkbd(sc, scr));
1257 case WSKBDIO_GETMODE:
1258 *(int *)data = (scr->scr_rawkbd ?
1259 WSKBD_RAW : WSKBD_TRANSLATED);
1260 return (0);
1261 }
1262 #endif
1263 inp = sc->sc_input;
1264 if (inp == NULL)
1265 return (ENXIO);
1266 error = wsevsrc_display_ioctl(inp, cmd, data, flag, l);
1267 if (error != EPASSTHROUGH)
1268 return (error);
1269 #endif /* NWSKBD > 0 */
1270
1271 switch (cmd) {
1272 case WSDISPLAYIO_GMODE:
1273 if (scr->scr_flags & SCR_GRAPHICS) {
1274 if (scr->scr_flags & SCR_DUMBFB)
1275 *(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
1276 else
1277 *(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
1278 } else
1279 *(u_int *)data = WSDISPLAYIO_MODE_EMUL;
1280 return (0);
1281
1282 case WSDISPLAYIO_SMODE:
1283 #define d (*(int *)data)
1284 if (d != WSDISPLAYIO_MODE_EMUL &&
1285 d != WSDISPLAYIO_MODE_MAPPED &&
1286 d != WSDISPLAYIO_MODE_DUMBFB)
1287 return (EINVAL);
1288
1289 if (WSSCREEN_HAS_EMULATOR(scr)) {
1290 scr->scr_flags &= ~SCR_GRAPHICS;
1291 if (d == WSDISPLAYIO_MODE_MAPPED ||
1292 d == WSDISPLAYIO_MODE_DUMBFB)
1293 scr->scr_flags |= SCR_GRAPHICS |
1294 ((d == WSDISPLAYIO_MODE_DUMBFB) ? SCR_DUMBFB : 0);
1295 } else if (d == WSDISPLAYIO_MODE_EMUL)
1296 return (EINVAL);
1297
1298 (void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1299 scr->scr_dconf->emulcookie, cmd, data, flag, l);
1300
1301 return (0);
1302 #undef d
1303
1304 #ifdef WSDISPLAY_SCROLLSUPPORT
1305 #define SETSCROLLLINES(dstp, srcp, dfltp) \
1306 do { \
1307 (dstp)->fastlines = ((srcp)->which & \
1308 WSDISPLAY_SCROLL_DOFASTLINES) ? \
1309 (srcp)->fastlines : (dfltp)->fastlines; \
1310 (dstp)->slowlines = ((srcp)->which & \
1311 WSDISPLAY_SCROLL_DOSLOWLINES) ? \
1312 (srcp)->slowlines : (dfltp)->slowlines; \
1313 (dstp)->which = WSDISPLAY_SCROLL_DOALL; \
1314 } while (0)
1315
1316
1317 case WSDISPLAYIO_DSSCROLL:
1318 usdp = (struct wsdisplay_scroll_data *)data;
1319 ksdp = &sc->sc_scroll_values;
1320 SETSCROLLLINES(ksdp, usdp, ksdp);
1321 return (0);
1322
1323 case WSDISPLAYIO_DGSCROLL:
1324 usdp = (struct wsdisplay_scroll_data *)data;
1325 ksdp = &sc->sc_scroll_values;
1326 SETSCROLLLINES(usdp, ksdp, ksdp);
1327 return (0);
1328 #else
1329 case WSDISPLAYIO_DSSCROLL:
1330 case WSDISPLAYIO_DGSCROLL:
1331 return ENODEV;
1332 #endif
1333
1334 case WSDISPLAYIO_SFONT:
1335 #define d ((struct wsdisplay_usefontdata *)data)
1336 if (!sc->sc_accessops->load_font)
1337 return (EINVAL);
1338 if (d->name) {
1339 error = copyinstr(d->name, namebuf, sizeof(namebuf), 0);
1340 if (error)
1341 return (error);
1342 fd.name = namebuf;
1343 } else
1344 fd.name = 0;
1345 fd.data = 0;
1346 error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
1347 scr->scr_dconf->emulcookie, &fd);
1348 if (!error && WSSCREEN_HAS_EMULATOR(scr)) {
1349 (*scr->scr_dconf->wsemul->reset)
1350 (scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
1351 #ifdef DEBUG
1352 printf("resize: %d %d\n",
1353 scr->scr_dconf->scrdata->nrows,
1354 scr->scr_dconf->scrdata->ncols);
1355 #endif
1356 if (scr->scr_dconf->wsemul->resize) {
1357 (*scr->scr_dconf->wsemul->resize)
1358 (scr->scr_dconf->wsemulcookie,
1359 scr->scr_dconf->scrdata);
1360 /* update the tty's size */
1361 scr->scr_tty->t_winsize.ws_row =
1362 scr->scr_dconf->scrdata->nrows;
1363 scr->scr_tty->t_winsize.ws_col =
1364 scr->scr_dconf->scrdata->ncols;
1365 /* send SIGWINCH to the process group on our tty */
1366 kpreempt_disable();
1367 ttysig(scr->scr_tty, TTYSIG_PG1, SIGWINCH);
1368 kpreempt_enable();
1369 }
1370 }
1371 return (error);
1372 #undef d
1373
1374 #ifdef WSDISPLAY_CUSTOM_OUTPUT
1375 case WSDISPLAYIO_GMSGATTRS:
1376 #define d ((struct wsdisplay_msgattrs *)data)
1377 (*scr->scr_dconf->wsemul->getmsgattrs)
1378 (scr->scr_dconf->wsemulcookie, d);
1379 return (0);
1380 #undef d
1381
1382 case WSDISPLAYIO_SMSGATTRS: {
1383 #define d ((struct wsdisplay_msgattrs *)data)
1384 int i;
1385 for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
1386 if (sc->sc_scr[i] != NULL)
1387 (*sc->sc_scr[i]->scr_dconf->wsemul->setmsgattrs)
1388 (sc->sc_scr[i]->scr_dconf->wsemulcookie,
1389 sc->sc_scr[i]->scr_dconf->scrdata,
1390 d);
1391 }
1392 return (0);
1393 #undef d
1394 #else
1395 case WSDISPLAYIO_GMSGATTRS:
1396 case WSDISPLAYIO_SMSGATTRS:
1397 return (ENODEV);
1398 #endif
1399 case WSDISPLAYIO_SETVERSION:
1400 return wsevent_setversion(&sc->evar, *(int *)data);
1401 }
1402
1403 /* check ioctls for display */
1404 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
1405 scr->scr_dconf->emulcookie, cmd, data, flag, l));
1406 }
1407
1408 int
1409 wsdisplay_stat_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
1410 int flag, struct lwp *l)
1411 {
1412 switch (cmd) {
1413 case WSDISPLAYIO_GETACTIVESCREEN:
1414 *(int*)data = wsdisplay_getactivescreen(sc);
1415 return (0);
1416 }
1417
1418 return (EPASSTHROUGH);
1419 }
1420
1421 int
1422 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, void *data,
1423 int flag, struct lwp *l)
1424 {
1425 int error;
1426 char *type, typebuf[16], *emul, emulbuf[16];
1427 void *tbuf;
1428 u_int fontsz;
1429 #if defined(COMPAT_14) && NWSKBD > 0
1430 struct wsmux_device wsmuxdata;
1431 #endif
1432 #if NWSKBD > 0
1433 struct wsevsrc *inp;
1434 #endif
1435
1436 switch (cmd) {
1437 case WSDISPLAYIO_ADDSCREEN:
1438 #define d ((struct wsdisplay_addscreendata *)data)
1439 if (d->screentype) {
1440 error = copyinstr(d->screentype, typebuf,
1441 sizeof(typebuf), 0);
1442 if (error)
1443 return (error);
1444 type = typebuf;
1445 } else
1446 type = 0;
1447 if (d->emul) {
1448 error = copyinstr(d->emul, emulbuf, sizeof(emulbuf),0);
1449 if (error)
1450 return (error);
1451 emul = emulbuf;
1452 } else
1453 emul = 0;
1454
1455 if ((error = wsdisplay_addscreen(sc, d->idx, type, emul)) == 0)
1456 wsdisplay_addscreen_print(sc, d->idx, 0);
1457 return (error);
1458 #undef d
1459 case WSDISPLAYIO_DELSCREEN:
1460 #define d ((struct wsdisplay_delscreendata *)data)
1461 return (wsdisplay_delscreen(sc, d->idx, d->flags));
1462 #undef d
1463 case WSDISPLAYIO_LDFONT:
1464 #define d ((struct wsdisplay_font *)data)
1465 if (!sc->sc_accessops->load_font)
1466 return (EINVAL);
1467 if (d->name) {
1468 error = copyinstr(d->name, typebuf, sizeof(typebuf), 0);
1469 if (error)
1470 return (error);
1471 d->name = typebuf;
1472 } else
1473 d->name = "loaded"; /* ??? */
1474 fontsz = d->fontheight * d->stride * d->numchars;
1475 if (fontsz > WSDISPLAY_MAXFONTSZ)
1476 return (EINVAL);
1477
1478 tbuf = malloc(fontsz, M_DEVBUF, M_WAITOK);
1479 error = copyin(d->data, tbuf, fontsz);
1480 if (error) {
1481 free(tbuf, M_DEVBUF);
1482 return (error);
1483 }
1484 d->data = tbuf;
1485 error =
1486 (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
1487 free(tbuf, M_DEVBUF);
1488 #undef d
1489 return (error);
1490
1491 #if NWSKBD > 0
1492 #ifdef COMPAT_14
1493 case _O_WSDISPLAYIO_SETKEYBOARD:
1494 #define d ((struct wsdisplay_kbddata *)data)
1495 inp = sc->sc_input;
1496 if (inp == NULL)
1497 return (ENXIO);
1498 switch (d->op) {
1499 case _O_WSDISPLAY_KBD_ADD:
1500 if (d->idx == -1) {
1501 d->idx = wskbd_pickfree();
1502 if (d->idx == -1)
1503 return (ENXIO);
1504 }
1505 wsmuxdata.type = WSMUX_KBD;
1506 wsmuxdata.idx = d->idx;
1507 return (wsevsrc_ioctl(inp, WSMUX_ADD_DEVICE,
1508 &wsmuxdata, flag, l));
1509 case _O_WSDISPLAY_KBD_DEL:
1510 wsmuxdata.type = WSMUX_KBD;
1511 wsmuxdata.idx = d->idx;
1512 return (wsevsrc_ioctl(inp, WSMUX_REMOVE_DEVICE,
1513 &wsmuxdata, flag, l));
1514 default:
1515 return (EINVAL);
1516 }
1517 #undef d
1518 #endif
1519
1520 case WSMUXIO_ADD_DEVICE:
1521 #define d ((struct wsmux_device *)data)
1522 if (d->idx == -1 && d->type == WSMUX_KBD)
1523 d->idx = wskbd_pickfree();
1524 #undef d
1525 /* fall into */
1526 case WSMUXIO_INJECTEVENT:
1527 case WSMUXIO_REMOVE_DEVICE:
1528 case WSMUXIO_LIST_DEVICES:
1529 inp = sc->sc_input;
1530 if (inp == NULL)
1531 return (ENXIO);
1532 return (wsevsrc_ioctl(inp, cmd, data, flag, l));
1533 #endif /* NWSKBD > 0 */
1534
1535 }
1536 return (EPASSTHROUGH);
1537 }
1538
1539 int
1540 wsdisplay_stat_inject(device_t dv, u_int type, int value)
1541 {
1542 struct wsdisplay_softc *sc = device_private(dv);
1543 struct wseventvar *evar;
1544 struct wscons_event event;
1545
1546 evar = &sc->evar;
1547
1548 if (evar == NULL)
1549 return (0);
1550
1551 if (evar->q == NULL)
1552 return (1);
1553
1554 event.type = type;
1555 event.value = value;
1556 if (wsevent_inject(evar, &event, 1) != 0) {
1557 log(LOG_WARNING, "wsdisplay: event queue overflow\n");
1558 return (1);
1559 }
1560
1561 return (0);
1562 }
1563
1564 paddr_t
1565 wsdisplaymmap(dev_t dev, off_t offset, int prot)
1566 {
1567 struct wsdisplay_softc *sc;
1568 struct wsscreen *scr;
1569
1570 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(dev));
1571
1572 if (ISWSDISPLAYSTAT(dev))
1573 return (-1);
1574
1575 if (ISWSDISPLAYCTL(dev))
1576 return (-1);
1577
1578 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1579 return (-1);
1580
1581 if (!(scr->scr_flags & SCR_GRAPHICS))
1582 return (-1);
1583
1584 /* pass mmap to display */
1585 return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie,
1586 scr->scr_dconf->emulcookie, offset, prot));
1587 }
1588
1589 void
1590 wsdisplaystart(struct tty *tp)
1591 {
1592 struct wsdisplay_softc *sc;
1593 struct wsscreen *scr;
1594 int s, n;
1595 u_char *tbuf;
1596
1597 s = spltty();
1598 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
1599 splx(s);
1600 return;
1601 }
1602 sc = device_lookup_private(&wsdisplay_cd, WSDISPLAYUNIT(tp->t_dev));
1603 if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
1604 splx(s);
1605 return;
1606 }
1607
1608 if (scr->scr_hold_screen) {
1609 tp->t_state |= TS_TIMEOUT;
1610 splx(s);
1611 return;
1612 }
1613 tp->t_state |= TS_BUSY;
1614 splx(s);
1615
1616 #ifdef DIAGNOSTIC
1617 scr->scr_in_ttyoutput = 1;
1618 #endif
1619
1620 /*
1621 * Drain output from ring buffer.
1622 * The output will normally be in one contiguous chunk, but when the
1623 * ring wraps, it will be in two pieces.. one at the end of the ring,
1624 * the other at the start. For performance, rather than loop here,
1625 * we output one chunk, see if there's another one, and if so, output
1626 * it too.
1627 */
1628
1629 n = ndqb(&tp->t_outq, 0);
1630 tbuf = tp->t_outq.c_cf;
1631
1632 if (!(scr->scr_flags & SCR_GRAPHICS)) {
1633 KASSERT(WSSCREEN_HAS_EMULATOR(scr));
1634 (*scr->scr_dconf->wsemul->output)(scr->scr_dconf->wsemulcookie,
1635 tbuf, n, 0);
1636 }
1637 ndflush(&tp->t_outq, n);
1638
1639 if ((n = ndqb(&tp->t_outq, 0)) > 0) {
1640 tbuf = tp->t_outq.c_cf;
1641
1642 if (!(scr->scr_flags & SCR_GRAPHICS)) {
1643 KASSERT(WSSCREEN_HAS_EMULATOR(scr));
1644 (*scr->scr_dconf->wsemul->output)
1645 (scr->scr_dconf->wsemulcookie, tbuf, n, 0);
1646 }
1647 ndflush(&tp->t_outq, n);
1648 }
1649
1650 #ifdef DIAGNOSTIC
1651 scr->scr_in_ttyoutput = 0;
1652 #endif
1653
1654 s = spltty();
1655 tp->t_state &= ~TS_BUSY;
1656 /* Come back if there's more to do */
1657 if (ttypull(tp)) {
1658 tp->t_state |= TS_TIMEOUT;
1659 callout_schedule(&tp->t_rstrt_ch, (hz > 128) ? (hz / 128) : 1);
1660 }
1661 splx(s);
1662 }
1663
1664 void
1665 wsdisplaystop(struct tty *tp, int flag)
1666 {
1667 int s;
1668
1669 s = spltty();
1670 if (ISSET(tp->t_state, TS_BUSY))
1671 if (!ISSET(tp->t_state, TS_TTSTOP))
1672 SET(tp->t_state, TS_FLUSH);
1673 splx(s);
1674 }
1675
1676 /* Set line parameters. */
1677 int
1678 wsdisplayparam(struct tty *tp, struct termios *t)
1679 {
1680
1681 tp->t_ispeed = t->c_ispeed;
1682 tp->t_ospeed = t->c_ospeed;
1683 tp->t_cflag = t->c_cflag;
1684 return 0;
1685 }
1686
1687 /*
1688 * Callbacks for the emulation code.
1689 */
1690 void
1691 wsdisplay_emulbell(void *v)
1692 {
1693 struct wsscreen *scr = v;
1694
1695 if (scr == NULL) /* console, before real attach */
1696 return;
1697
1698 if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
1699 return;
1700
1701 (void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
1702 FWRITE, NULL);
1703 }
1704
1705 void
1706 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
1707 {
1708 struct wsscreen *scr = v;
1709 struct tty *tp;
1710 int (*ifcn)(int, struct tty *);
1711
1712 if (v == NULL) /* console, before real attach */
1713 return;
1714
1715 if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
1716 return;
1717 if (!WSSCREEN_HAS_TTY(scr))
1718 return;
1719
1720 tp = scr->scr_tty;
1721
1722 /*
1723 * XXX bad hack to work around locking problems in tty.c:
1724 * ttyinput() will try to lock again, causing deadlock.
1725 * We assume that wsdisplay_emulinput() can only be called
1726 * from within wsdisplaystart(), and thus the tty lock
1727 * is already held. Use an entry point which doesn't lock.
1728 */
1729 KASSERT(scr->scr_in_ttyoutput);
1730 ifcn = tp->t_linesw->l_rint;
1731 if (ifcn == ttyinput)
1732 ifcn = ttyinput_wlock;
1733
1734 while (count-- > 0)
1735 (*ifcn)(*data++, tp);
1736 }
1737
1738 /*
1739 * Calls from the keyboard interface.
1740 */
1741 void
1742 wsdisplay_kbdinput(device_t dv, keysym_t ks)
1743 {
1744 struct wsdisplay_softc *sc = device_private(dv);
1745 struct wsscreen *scr;
1746 const char *dp;
1747 int count;
1748 struct tty *tp;
1749
1750 KASSERT(sc != NULL);
1751
1752 scr = sc->sc_focus;
1753
1754 if (!scr || !WSSCREEN_HAS_TTY(scr))
1755 return;
1756
1757 tp = scr->scr_tty;
1758
1759 if (KS_GROUP(ks) == KS_GROUP_Plain && KS_VALUE(ks) <= 0x7f)
1760 (*tp->t_linesw->l_rint)(KS_VALUE(ks), tp);
1761 else if (WSSCREEN_HAS_EMULATOR(scr)) {
1762 count = (*scr->scr_dconf->wsemul->translate)
1763 (scr->scr_dconf->wsemulcookie, ks, &dp);
1764 while (count-- > 0)
1765 (*tp->t_linesw->l_rint)((unsigned char)(*dp++), tp);
1766 }
1767 }
1768
1769 #if defined(WSDISPLAY_COMPAT_RAWKBD)
1770 int
1771 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
1772 {
1773 #if NWSKBD > 0
1774 int s, raw, data, error;
1775 struct wsevsrc *inp;
1776
1777 s = spltty();
1778
1779 raw = (scr ? scr->scr_rawkbd : 0);
1780
1781 if (scr != sc->sc_focus ||
1782 sc->sc_rawkbd == raw) {
1783 splx(s);
1784 return (0);
1785 }
1786
1787 data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
1788 inp = sc->sc_input;
1789 if (inp == NULL) {
1790 splx(s);
1791 return (ENXIO);
1792 }
1793 error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, 0, 0);
1794 if (!error)
1795 sc->sc_rawkbd = raw;
1796 splx(s);
1797 return (error);
1798 #else
1799 return (0);
1800 #endif
1801 }
1802 #endif
1803
1804 static void
1805 wsdisplay_switch3_cb(void *arg, int error, int waitok)
1806 {
1807 device_t dv = arg;
1808
1809 wsdisplay_switch3(dv, error, waitok);
1810 }
1811
1812 static int
1813 wsdisplay_switch3(device_t dv, int error, int waitok)
1814 {
1815 struct wsdisplay_softc *sc = device_private(dv);
1816 int no;
1817 struct wsscreen *scr;
1818
1819 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1820 aprint_error_dev(dv, "wsdisplay_switch3: not switching\n");
1821 return (EINVAL);
1822 }
1823
1824 no = sc->sc_screenwanted;
1825 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1826 panic("wsdisplay_switch3: invalid screen %d", no);
1827 scr = sc->sc_scr[no];
1828 if (!scr) {
1829 aprint_error_dev(dv,
1830 "wsdisplay_switch3: screen %d disappeared\n", no);
1831 error = ENXIO;
1832 }
1833
1834 if (error) {
1835 /* try to recover, avoid recursion */
1836
1837 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1838 aprint_error_dev(dv, "wsdisplay_switch3: giving up\n");
1839 sc->sc_focus = 0;
1840 #ifdef WSDISPLAY_COMPAT_RAWKBD
1841 wsdisplay_update_rawkbd(sc, 0);
1842 #endif
1843 sc->sc_flags &= ~SC_SWITCHPENDING;
1844 return (error);
1845 }
1846
1847 sc->sc_screenwanted = sc->sc_oldscreen;
1848 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1849 return (wsdisplay_switch1(dv, 0, waitok));
1850 }
1851
1852 if (scr->scr_syncops && !error)
1853 sc->sc_flags |= SC_XATTACHED;
1854
1855 sc->sc_flags &= ~SC_SWITCHPENDING;
1856
1857 if (!error && (scr->scr_flags & SCR_WAITACTIVE))
1858 wakeup(scr);
1859 return (error);
1860 }
1861
1862 static void
1863 wsdisplay_switch2_cb(void *arg, int error, int waitok)
1864 {
1865 device_t dv = arg;
1866
1867 wsdisplay_switch2(dv, error, waitok);
1868 }
1869
1870 static int
1871 wsdisplay_switch2(device_t dv, int error, int waitok)
1872 {
1873 struct wsdisplay_softc *sc = device_private(dv);
1874 int no;
1875 struct wsscreen *scr;
1876
1877 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1878 aprint_error_dev(dv, "wsdisplay_switch2: not switching\n");
1879 return (EINVAL);
1880 }
1881
1882 no = sc->sc_screenwanted;
1883 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1884 panic("wsdisplay_switch2: invalid screen %d", no);
1885 scr = sc->sc_scr[no];
1886 if (!scr) {
1887 aprint_error_dev(dv,
1888 "wsdisplay_switch2: screen %d disappeared\n", no);
1889 error = ENXIO;
1890 }
1891
1892 if (error) {
1893 /* try to recover, avoid recursion */
1894
1895 if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1896 aprint_error_dev(dv, "wsdisplay_switch2: giving up\n");
1897 sc->sc_focus = 0;
1898 sc->sc_flags &= ~SC_SWITCHPENDING;
1899 return (error);
1900 }
1901
1902 sc->sc_screenwanted = sc->sc_oldscreen;
1903 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1904 return (wsdisplay_switch1(dv, 0, waitok));
1905 }
1906
1907 sc->sc_focusidx = no;
1908 sc->sc_focus = scr;
1909
1910 #ifdef WSDISPLAY_COMPAT_RAWKBD
1911 (void) wsdisplay_update_rawkbd(sc, scr);
1912 #endif
1913 /* keyboard map??? */
1914
1915 if (scr->scr_syncops &&
1916 !(sc->sc_isconsole && wsdisplay_cons_pollmode)) {
1917 error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
1918 wsdisplay_switch3_cb, dv);
1919 if (error == EAGAIN) {
1920 /* switch will be done asynchronously */
1921 return (0);
1922 }
1923 }
1924
1925 return (wsdisplay_switch3(dv, error, waitok));
1926 }
1927
1928 static void
1929 wsdisplay_switch1_cb(void *arg, int error, int waitok)
1930 {
1931 device_t dv = arg;
1932
1933 wsdisplay_switch1(dv, error, waitok);
1934 }
1935
1936 static int
1937 wsdisplay_switch1(device_t dv, int error, int waitok)
1938 {
1939 struct wsdisplay_softc *sc = device_private(dv);
1940 int no;
1941 struct wsscreen *scr;
1942
1943 if (!(sc->sc_flags & SC_SWITCHPENDING)) {
1944 aprint_error_dev(dv, "wsdisplay_switch1: not switching\n");
1945 return (EINVAL);
1946 }
1947
1948 no = sc->sc_screenwanted;
1949 if (no == WSDISPLAY_NULLSCREEN) {
1950 sc->sc_flags &= ~SC_SWITCHPENDING;
1951 if (!error) {
1952 sc->sc_flags &= ~SC_XATTACHED;
1953 sc->sc_focus = 0;
1954 }
1955 wakeup(sc);
1956 return (error);
1957 }
1958 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1959 panic("wsdisplay_switch1: invalid screen %d", no);
1960 scr = sc->sc_scr[no];
1961 if (!scr) {
1962 aprint_error_dev(dv,
1963 "wsdisplay_switch1: screen %d disappeared\n", no);
1964 error = ENXIO;
1965 }
1966
1967 if (error) {
1968 sc->sc_flags &= ~SC_SWITCHPENDING;
1969 return (error);
1970 }
1971
1972 sc->sc_flags &= ~SC_XATTACHED;
1973
1974 error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1975 scr->scr_dconf->emulcookie,
1976 waitok,
1977 sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsdisplay_switch2_cb, dv);
1978 if (error == EAGAIN) {
1979 /* switch will be done asynchronously */
1980 return (0);
1981 }
1982
1983 return (wsdisplay_switch2(dv, error, waitok));
1984 }
1985
1986 int
1987 wsdisplay_switch(device_t dv, int no, int waitok)
1988 {
1989 struct wsdisplay_softc *sc = device_private(dv);
1990 int s, res = 0;
1991 struct wsscreen *scr;
1992
1993 if (no != WSDISPLAY_NULLSCREEN) {
1994 if ((no < 0 || no >= WSDISPLAY_MAXSCREEN))
1995 return (EINVAL);
1996 if (sc->sc_scr[no] == NULL)
1997 return (ENXIO);
1998 }
1999
2000 wsdisplay_stat_inject(dv, WSCONS_EVENT_SCREEN_SWITCH, no);
2001
2002 s = spltty();
2003
2004 if ((sc->sc_focus && no == sc->sc_focusidx) ||
2005 (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
2006 splx(s);
2007 return (0);
2008 }
2009
2010 if (sc->sc_flags & SC_SWITCHPENDING) {
2011 splx(s);
2012 return (EBUSY);
2013 }
2014
2015 sc->sc_flags |= SC_SWITCHPENDING;
2016 sc->sc_screenwanted = no;
2017
2018 splx(s);
2019
2020 scr = sc->sc_focus;
2021 if (!scr) {
2022 sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
2023 return (wsdisplay_switch1(dv, 0, waitok));
2024 } else
2025 sc->sc_oldscreen = sc->sc_focusidx;
2026
2027 if (scr->scr_syncops) {
2028 if (!(sc->sc_flags & SC_XATTACHED) ||
2029 (sc->sc_isconsole && wsdisplay_cons_pollmode)) {
2030 /* nothing to do here */
2031 return (wsdisplay_switch1(dv, 0, waitok));
2032 }
2033 res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
2034 wsdisplay_switch1_cb, dv);
2035 if (res == EAGAIN) {
2036 /* switch will be done asynchronously */
2037 return (0);
2038 }
2039 } else if (scr->scr_flags & SCR_GRAPHICS) {
2040 /* no way to save state */
2041 res = EBUSY;
2042 }
2043
2044 return (wsdisplay_switch1(dv, res, waitok));
2045 }
2046
2047 void
2048 wsdisplay_reset(device_t dv, enum wsdisplay_resetops op)
2049 {
2050 struct wsdisplay_softc *sc = device_private(dv);
2051 struct wsscreen *scr;
2052
2053 KASSERT(sc != NULL);
2054 scr = sc->sc_focus;
2055
2056 if (!scr)
2057 return;
2058
2059 switch (op) {
2060 case WSDISPLAY_RESETEMUL:
2061 if (!WSSCREEN_HAS_EMULATOR(scr))
2062 break;
2063 (*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
2064 WSEMUL_RESET);
2065 break;
2066 case WSDISPLAY_RESETCLOSE:
2067 wsdisplay_closescreen(sc, scr);
2068 break;
2069 }
2070 }
2071
2072 /*
2073 * Interface for (external) VT switch / process synchronization code
2074 */
2075 int
2076 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
2077 void *cookie)
2078 {
2079 if (scr->scr_syncops) {
2080 /*
2081 * The screen is already claimed.
2082 * Check if the owner is still alive.
2083 */
2084 if ((*scr->scr_syncops->check)(scr->scr_synccookie))
2085 return (EBUSY);
2086 }
2087 scr->scr_syncops = ops;
2088 scr->scr_synccookie = cookie;
2089 if (scr == scr->sc->sc_focus)
2090 scr->sc->sc_flags |= SC_XATTACHED;
2091 return (0);
2092 }
2093
2094 int
2095 wsscreen_detach_sync(struct wsscreen *scr)
2096 {
2097 if (!scr->scr_syncops)
2098 return (EINVAL);
2099 scr->scr_syncops = 0;
2100 if (scr == scr->sc->sc_focus)
2101 scr->sc->sc_flags &= ~SC_XATTACHED;
2102 return (0);
2103 }
2104
2105 int
2106 wsscreen_lookup_sync(struct wsscreen *scr,
2107 const struct wscons_syncops *ops, /* used as ID */
2108 void **cookiep)
2109 {
2110 if (!scr->scr_syncops || ops != scr->scr_syncops)
2111 return (EINVAL);
2112 *cookiep = scr->scr_synccookie;
2113 return (0);
2114 }
2115
2116 /*
2117 * Interface to virtual screen stuff
2118 */
2119 int
2120 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
2121 {
2122 return (WSDISPLAY_MAXSCREEN - 1);
2123 }
2124
2125 int
2126 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
2127 {
2128 if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
2129 return (EINVAL);
2130 if (!sc->sc_scr[idx])
2131 return (ENXIO);
2132 return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
2133 }
2134
2135 int
2136 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
2137 {
2138 return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
2139 }
2140
2141 int
2142 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
2143 {
2144 struct wsscreen *scr;
2145 int s, res = 0;
2146
2147 if (no == WSDISPLAY_NULLSCREEN) {
2148 s = spltty();
2149 while (sc->sc_focus && res == 0) {
2150 res = tsleep(sc, PCATCH, "wswait", 0);
2151 }
2152 splx(s);
2153 return (res);
2154 }
2155
2156 if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
2157 return (ENXIO);
2158 scr = sc->sc_scr[no];
2159 if (!scr)
2160 return (ENXIO);
2161
2162 s = spltty();
2163 if (scr != sc->sc_focus) {
2164 scr->scr_flags |= SCR_WAITACTIVE;
2165 res = tsleep(scr, PCATCH, "wswait", 0);
2166 if (scr != sc->sc_scr[no])
2167 res = ENXIO; /* disappeared in the meantime */
2168 else
2169 scr->scr_flags &= ~SCR_WAITACTIVE;
2170 }
2171 splx(s);
2172 return (res);
2173 }
2174
2175 void
2176 wsdisplay_kbdholdscreen(device_t dv, int hold)
2177 {
2178 struct wsdisplay_softc *sc = device_private(dv);
2179 struct wsscreen *scr;
2180
2181 scr = sc->sc_focus;
2182
2183 if (!scr)
2184 return;
2185
2186 if (hold)
2187 scr->scr_hold_screen = 1;
2188 else {
2189 scr->scr_hold_screen = 0;
2190 callout_schedule(&scr->scr_tty->t_rstrt_ch, 0);
2191 }
2192 }
2193
2194 #if NWSKBD > 0
2195 void
2196 wsdisplay_set_console_kbd(struct wsevsrc *src)
2197 {
2198 if (wsdisplay_console_device == NULL) {
2199 src->me_dispdv = NULL;
2200 return;
2201 }
2202 #if NWSMUX > 0
2203 if (wsmux_attach_sc((struct wsmux_softc *)
2204 wsdisplay_console_device->sc_input, src)) {
2205 src->me_dispdv = NULL;
2206 return;
2207 }
2208 #else
2209 wsdisplay_console_device->sc_input = src;
2210 #endif
2211 src->me_dispdv = wsdisplay_console_device->sc_dev;
2212 }
2213 #endif /* NWSKBD > 0 */
2214
2215 /*
2216 * Console interface.
2217 */
2218 void
2219 wsdisplay_cnputc(dev_t dev, int i)
2220 {
2221 struct wsscreen_internal *dc;
2222 u_char c = i;
2223
2224 if (!wsdisplay_console_initted)
2225 return;
2226
2227 if ((wsdisplay_console_device != NULL) &&
2228 (wsdisplay_console_device->sc_scr[0] != NULL) &&
2229 (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
2230 return;
2231
2232 dc = &wsdisplay_console_conf;
2233 (*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
2234 }
2235
2236 static int
2237 wsdisplay_getc_dummy(dev_t dev)
2238 {
2239 /* panic? */
2240 return (0);
2241 }
2242
2243 static void
2244 wsdisplay_pollc(dev_t dev, int on)
2245 {
2246
2247 wsdisplay_cons_pollmode = on;
2248
2249 /* notify to fb drivers */
2250 if (wsdisplay_console_device != NULL &&
2251 wsdisplay_console_device->sc_accessops->pollc != NULL)
2252 (*wsdisplay_console_device->sc_accessops->pollc)
2253 (wsdisplay_console_device->sc_accesscookie, on);
2254
2255 /* notify to kbd drivers */
2256 if (wsdisplay_cons_kbd_pollc)
2257 (*wsdisplay_cons_kbd_pollc)(NODEV, on);
2258 }
2259
2260 void
2261 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
2262 void (*bell)(dev_t, u_int, u_int, u_int))
2263 {
2264 wsdisplay_cons.cn_getc = get;
2265 wsdisplay_cons.cn_bell = bell;
2266 wsdisplay_cons_kbd_pollc = poll;
2267 }
2268
2269 void
2270 wsdisplay_unset_cons_kbd(void)
2271 {
2272 wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
2273 wsdisplay_cons.cn_bell = NULL;
2274 wsdisplay_cons_kbd_pollc = 0;
2275 }
2276