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