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