DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / dev / misc / syscons / syscons.c
CommitLineData
984263bc 1/*-
cd29885a 2 * Copyright (c) 1992-1998 Søren Schmidt
984263bc
MD
3 * All rights reserved.
4 *
2a85d763
MD
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
7 *
984263bc
MD
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
2123da6d 31 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/dev/syscons/syscons.c,v 1.336.2.17 2004/03/25 08:41:09 ru Exp $
da923c26 32 * $DragonFly: src/sys/dev/misc/syscons/syscons.c,v 1.35 2008/08/10 19:47:31 swildner Exp $
984263bc
MD
33 */
34
1f2de5d4 35#include "use_splash.h"
984263bc
MD
36#include "opt_syscons.h"
37#include "opt_ddb.h"
0855a2af 38#ifdef __i386__
1f2de5d4 39#include "use_apm.h"
0855a2af 40#endif
984263bc
MD
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/eventhandler.h>
45#include <sys/reboot.h>
46#include <sys/conf.h>
47#include <sys/proc.h>
895c1f85 48#include <sys/priv.h>
984263bc
MD
49#include <sys/signalvar.h>
50#include <sys/sysctl.h>
51#include <sys/tty.h>
52#include <sys/kernel.h>
984263bc
MD
53#include <sys/cons.h>
54#include <sys/random.h>
6bc31f17 55#include <sys/thread2.h>
984263bc
MD
56
57#include <machine/clock.h>
58#include <machine/console.h>
59#include <machine/psl.h>
60#include <machine/pc/display.h>
0855a2af 61#ifdef __i386__
984263bc 62#include <machine/apm_bios.h>
0855a2af 63#endif
984263bc 64#include <machine/frame.h>
984263bc 65
1f2de5d4
MD
66#include <dev/misc/kbd/kbdreg.h>
67#include <dev/video/fb/fbreg.h>
68#include <dev/video/fb/splashreg.h>
69#include "syscons.h"
984263bc
MD
70
71#define COLD 0
72#define WARM 1
73
74#define DEFAULT_BLANKTIME (5*60) /* 5 minutes */
75#define MAX_BLANKTIME (7*24*60*60) /* 7 days!? */
76
77#define KEYCODE_BS 0x0e /* "<-- Backspace" key, XXX */
78
b721b9ee
SW
79MALLOC_DEFINE(M_SYSCONS, "syscons", "Syscons");
80
984263bc
MD
81typedef struct default_attr {
82 int std_color; /* normal hardware color */
83 int rev_color; /* reverse hardware color */
84} default_attr;
85
86static default_attr user_default = {
87 SC_NORM_ATTR,
88 SC_NORM_REV_ATTR,
89};
90
91static default_attr kernel_default = {
92 SC_KERNEL_CONS_ATTR,
93 SC_KERNEL_CONS_REV_ATTR,
94};
95
96static int sc_console_unit = -1;
97static scr_stat *sc_console;
98static struct tty *sc_console_tty;
99static void *kernel_console_ts;
100
101static char init_done = COLD;
102static char shutdown_in_progress = FALSE;
103static char sc_malloc = FALSE;
104
105static int saver_mode = CONS_NO_SAVER; /* LKM/user saver */
106static int run_scrn_saver = FALSE; /* should run the saver? */
107static long scrn_blank_time = 0; /* screen saver timeout value */
108#if NSPLASH > 0
109static int scrn_blanked; /* # of blanked screen */
110static int sticky_splash = FALSE;
111
112static void none_saver(sc_softc_t *sc, int blank) { }
113static void (*current_saver)(sc_softc_t *, int) = none_saver;
114#endif
115
116#if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
117#include "font.h"
118#endif
119
984263bc
MD
120static bios_values_t bios_value;
121
122static int enable_panic_key;
123SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
124 0, "");
125
126#define SC_CONSOLECTL 255
127
59306b36
MD
128#define VIRTUAL_TTY(sc, x) ((SC_DEV((sc),(x)) != NULL) ? \
129 (SC_DEV((sc),(x))->si_tty) : NULL)
984263bc
MD
130#define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
131
cd29885a
MD
132static int debugger;
133static cdev_t cctl_dev;
984263bc
MD
134
135/* prototypes */
136static int scvidprobe(int unit, int flags, int cons);
137static int sckbdprobe(int unit, int flags, int cons);
138static void scmeminit(void *arg);
b13267a5 139static int scdevtounit(cdev_t dev);
984263bc
MD
140static kbd_callback_func_t sckbdevent;
141static int scparam(struct tty *tp, struct termios *t);
142static void scstart(struct tty *tp);
143static void scinit(int unit, int flags);
984263bc 144static void scterm(int unit, int flags);
984263bc
MD
145static void scshutdown(void *arg, int howto);
146static u_int scgetc(sc_softc_t *sc, u_int flags);
147#define SCGETC_CN 1
148#define SCGETC_NONBLOCK 2
149static int sccngetch(int flags);
150static void sccnupdate(scr_stat *scp);
151static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
152static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
153static timeout_t scrn_timer;
154static int and_region(int *s1, int *e1, int s2, int e2);
155static void scrn_update(scr_stat *scp, int show_cursor);
156
157#if NSPLASH > 0
158static int scsplash_callback(int event, void *arg);
159static void scsplash_saver(sc_softc_t *sc, int show);
160static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
161static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
162static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
163static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
164static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
165static int wait_scrn_saver_stop(sc_softc_t *sc);
166#define scsplash_stick(stick) (sticky_splash = (stick))
167#else /* !NSPLASH */
168#define scsplash_stick(stick)
169#endif /* NSPLASH */
170
6bc31f17 171static void do_switch_scr(sc_softc_t *sc);
984263bc
MD
172static int vt_proc_alive(scr_stat *scp);
173static int signal_vt_rel(scr_stat *scp);
174static int signal_vt_acq(scr_stat *scp);
6bc31f17 175static int finish_vt_rel(scr_stat *scp, int release);
984263bc
MD
176static int finish_vt_acq(scr_stat *scp);
177static void exchange_scr(sc_softc_t *sc);
178static void update_cursor_image(scr_stat *scp);
179static int save_kbd_state(scr_stat *scp);
180static int update_kbd_state(scr_stat *scp, int state, int mask);
181static int update_kbd_leds(scr_stat *scp, int which);
182static timeout_t blink_screen;
183
184#define CDEV_MAJOR 12
185
186static cn_probe_t sccnprobe;
187static cn_init_t sccninit;
ce81f184 188static cn_init_t sccninit_fini;
984263bc
MD
189static cn_getc_t sccngetc;
190static cn_checkc_t sccncheckc;
191static cn_putc_t sccnputc;
192static cn_dbctl_t sccndbctl;
193static cn_term_t sccnterm;
194
ce81f184
MD
195CONS_DRIVER(sc, sccnprobe, sccninit, sccninit_fini, sccnterm,
196 sccngetc, sccncheckc, sccnputc, sccndbctl);
984263bc
MD
197
198static d_open_t scopen;
199static d_close_t scclose;
200static d_read_t scread;
201static d_ioctl_t scioctl;
202static d_mmap_t scmmap;
203
fef8985e
MD
204static struct dev_ops sc_ops = {
205 { "sc", CDEV_MAJOR, D_TTY | D_KQFILTER },
206 .d_open = scopen,
207 .d_close = scclose,
208 .d_read = scread,
209 .d_write = ttywrite,
210 .d_ioctl = scioctl,
211 .d_poll = ttypoll,
212 .d_mmap = scmmap,
a32446b7
MD
213 .d_kqfilter = ttykqfilter,
214 .d_revoke = ttyrevoke
984263bc
MD
215};
216
217int
218sc_probe_unit(int unit, int flags)
219{
220 if (!scvidprobe(unit, flags, FALSE)) {
221 if (bootverbose)
e3869ec7 222 kprintf("sc%d: no video adapter found.\n", unit);
984263bc
MD
223 return ENXIO;
224 }
225
226 /* syscons will be attached even when there is no keyboard */
227 sckbdprobe(unit, flags, FALSE);
228
229 return 0;
230}
231
232/* probe video adapters, return TRUE if found */
233static int
234scvidprobe(int unit, int flags, int cons)
235{
236 /*
237 * Access the video adapter driver through the back door!
238 * Video adapter drivers need to be configured before syscons.
239 * However, when syscons is being probed as the low-level console,
240 * they have not been initialized yet. We force them to initialize
241 * themselves here. XXX
242 */
243 vid_configure(cons ? VIO_PROBE_ONLY : 0);
244
245 return (vid_find_adapter("*", unit) >= 0);
246}
247
248/* probe the keyboard, return TRUE if found */
249static int
250sckbdprobe(int unit, int flags, int cons)
251{
252 /* access the keyboard driver through the backdoor! */
253 kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
254
255 return (kbd_find_keyboard("*", unit) >= 0);
256}
257
dc98d15f
SW
258static char *
259adapter_name(video_adapter_t *adp)
984263bc
MD
260{
261 static struct {
262 int type;
263 char *name[2];
264 } names[] = {
265 { KD_MONO, { "MDA", "MDA" } },
266 { KD_HERCULES, { "Hercules", "Hercules" } },
267 { KD_CGA, { "CGA", "CGA" } },
268 { KD_EGA, { "EGA", "EGA (mono)" } },
269 { KD_VGA, { "VGA", "VGA (mono)" } },
984263bc
MD
270 { KD_TGA, { "TGA", "TGA" } },
271 { -1, { "Unknown", "Unknown" } },
272 };
273 int i;
274
275 for (i = 0; names[i].type != -1; ++i)
276 if (names[i].type == adp->va_type)
277 break;
278 return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
279}
280
281int
282sc_attach_unit(int unit, int flags)
283{
284 sc_softc_t *sc;
285 scr_stat *scp;
286#ifdef SC_PIXEL_MODE
287 video_info_t info;
288#endif
289 int vc;
b13267a5 290 cdev_t dev;
984263bc
MD
291
292 flags &= ~SC_KERNEL_CONSOLE;
293
294 if (sc_console_unit == unit) {
295 /*
296 * If this unit is being used as the system console, we need to
297 * adjust some variables and buffers before and after scinit().
298 */
299 /* assert(sc_console != NULL) */
300 flags |= SC_KERNEL_CONSOLE;
301 scmeminit(NULL);
302
303 scinit(unit, flags);
304
305 if (sc_console->tsw->te_size > 0) {
306 /* assert(sc_console->ts != NULL); */
307 kernel_console_ts = sc_console->ts;
77652cad 308 sc_console->ts = kmalloc(sc_console->tsw->te_size,
b721b9ee 309 M_SYSCONS, M_WAITOK);
984263bc
MD
310 bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
311 (*sc_console->tsw->te_default_attr)(sc_console,
312 user_default.std_color,
313 user_default.rev_color);
314 }
315 } else {
316 scinit(unit, flags);
317 }
318
319 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
ce81f184
MD
320
321 /*
322 * If this is the console we couldn't setup sc->dev before because
323 * malloc wasn't working. Set it up now.
324 */
325 if (flags & SC_KERNEL_CONSOLE) {
326 KKASSERT(sc->dev == NULL);
327 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK|M_ZERO);
328 sc->dev[0] = make_dev(&sc_ops, sc_console_unit*MAXCONS, UID_ROOT,
329 GID_WHEEL, 0600,
330 "ttyv%r", sc_console_unit*MAXCONS);
331 sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
332 sc->dev[0]->si_drv1 = sc_console;
333 }
334
335 /*
336 * Finish up the standard attach
337 */
984263bc 338 sc->config = flags;
356bb9cd 339 callout_init(&sc->scrn_timer_ch);
984263bc
MD
340 scp = SC_STAT(sc->dev[0]);
341 if (sc_console == NULL) /* sc_console_unit < 0 */
342 sc_console = scp;
343
344#ifdef SC_PIXEL_MODE
345 if ((sc->config & SC_VESA800X600)
346 && ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
347#if NSPLASH > 0
348 if (sc->flags & SC_SPLASH_SCRN)
349 splash_term(sc->adp);
350#endif
351 sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
352 sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
353 sc->initial_mode = M_VESA_800x600;
354#if NSPLASH > 0
355 /* put up the splash again! */
356 if (sc->flags & SC_SPLASH_SCRN)
357 splash_init(sc->adp, scsplash_callback, sc);
358#endif
359 }
360#endif /* SC_PIXEL_MODE */
361
362 /* initialize cursor */
363 if (!ISGRAPHSC(scp))
364 update_cursor_image(scp);
365
366 /* get screen update going */
367 scrn_timer(sc);
368
369 /* set up the keyboard */
370 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
371 update_kbd_state(scp, scp->status, LOCK_MASK);
372
e3869ec7 373 kprintf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
984263bc
MD
374 unit, adapter_name(sc->adp), sc->vtys, sc->config);
375 if (bootverbose) {
e3869ec7 376 kprintf("sc%d:", unit);
984263bc 377 if (sc->adapter >= 0)
e3869ec7 378 kprintf(" fb%d", sc->adapter);
984263bc 379 if (sc->keyboard >= 0)
e3869ec7 380 kprintf(", kbd%d", sc->keyboard);
984263bc 381 if (scp->tsw)
e3869ec7 382 kprintf(", terminal emulator: %s (%s)",
984263bc 383 scp->tsw->te_name, scp->tsw->te_desc);
e3869ec7 384 kprintf("\n");
984263bc
MD
385 }
386
387 /* register a shutdown callback for the kernel console */
388 if (sc_console_unit == unit)
389 EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
390 (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
391
e4c9c0c8 392 /*
3e82b46c
MD
393 * create devices.
394 *
395 * The first vty already has struct tty and scr_stat initialized
396 * in scinit(). The other vtys will have these structs when
397 * first opened.
e4c9c0c8 398 */
3e82b46c 399 for (vc = 1; vc < sc->vtys; vc++) {
fef8985e 400 dev = make_dev(&sc_ops, vc + unit * MAXCONS,
3e82b46c
MD
401 UID_ROOT, GID_WHEEL,
402 0600, "ttyv%r", vc + unit * MAXCONS);
984263bc 403 sc->dev[vc] = dev;
984263bc 404 }
cd29885a
MD
405 cctl_dev = make_dev(&sc_ops, SC_CONSOLECTL,
406 UID_ROOT, GID_WHEEL, 0600, "consolectl");
407 cctl_dev->si_tty = sc_console_tty = ttymalloc(sc_console_tty);
408 cctl_dev->si_drv1 = sc_console;
984263bc
MD
409 return 0;
410}
411
412static void
413scmeminit(void *arg)
414{
415 if (sc_malloc)
416 return;
417 sc_malloc = TRUE;
418
419 /*
420 * As soon as malloc() becomes functional, we had better allocate
421 * various buffers for the kernel console.
422 */
423
424 if (sc_console_unit < 0) /* sc_console == NULL */
425 return;
426
427 /* copy the temporary buffer to the final buffer */
53f1c39c 428 sc_alloc_scr_buffer(sc_console, TRUE, FALSE);
984263bc
MD
429
430#ifndef SC_NO_CUTPASTE
53f1c39c 431 sc_alloc_cut_buffer(sc_console, TRUE);
984263bc
MD
432#endif
433
434#ifndef SC_NO_HISTORY
435 /* initialize history buffer & pointers */
53f1c39c 436 sc_alloc_history_buffer(sc_console, 0, 0, TRUE);
984263bc
MD
437#endif
438}
439
ba39e2e0 440SYSINIT(sc_mem, SI_BOOT1_POST, SI_ORDER_ANY, scmeminit, NULL);
984263bc
MD
441
442static int
b13267a5 443scdevtounit(cdev_t dev)
984263bc
MD
444{
445 int vty = SC_VTY(dev);
446
447 if (vty == SC_CONSOLECTL)
448 return ((sc_console != NULL) ? sc_console->sc->unit : -1);
449 else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
450 return -1;
451 else
452 return vty/MAXCONS;
453}
454
455int
fef8985e 456scopen(struct dev_open_args *ap)
984263bc 457{
b13267a5 458 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
459 int unit = scdevtounit(dev);
460 sc_softc_t *sc;
461 struct tty *tp;
462 scr_stat *scp;
463 keyarg_t key;
464 int error;
465
466 DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
467 major(dev), minor(dev), unit, SC_VTY(dev)));
468
469 sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
470 if (sc == NULL)
471 return ENXIO;
472
473 tp = dev->si_tty = ttymalloc(dev->si_tty);
474 tp->t_oproc = scstart;
475 tp->t_param = scparam;
476 tp->t_stop = nottystop;
cd29885a 477
984263bc 478 tp->t_dev = dev;
cd29885a 479
984263bc
MD
480 if (!ISTTYOPEN(tp)) {
481 ttychars(tp);
482 /* Use the current setting of the <-- key as default VERASE. */
483 /* If the Delete key is preferable, an stty is necessary */
484 if (sc->kbd != NULL) {
485 key.keynum = KEYCODE_BS;
486 kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
487 tp->t_cc[VERASE] = key.key.map[0];
488 }
489 tp->t_iflag = TTYDEF_IFLAG;
490 tp->t_oflag = TTYDEF_OFLAG;
491 tp->t_cflag = TTYDEF_CFLAG;
492 tp->t_lflag = TTYDEF_LFLAG;
493 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
494 scparam(tp, &tp->t_termios);
495 (*linesw[tp->t_line].l_modem)(tp, 1);
496 }
497 else
895c1f85 498 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0))
984263bc
MD
499 return(EBUSY);
500
501 error = (*linesw[tp->t_line].l_open)(dev, tp);
502
503 scp = SC_STAT(dev);
504 if (scp == NULL) {
848cc936 505 scp = dev->si_drv1 = alloc_scp(sc, SC_VTY(dev));
984263bc
MD
506 if (ISGRAPHSC(scp))
507 sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
508 }
509 if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
510 tp->t_winsize.ws_col = scp->xsize;
511 tp->t_winsize.ws_row = scp->ysize;
512 }
513
514 return error;
515}
516
517int
fef8985e 518scclose(struct dev_close_args *ap)
984263bc 519{
b13267a5 520 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
521 struct tty *tp = dev->si_tty;
522 scr_stat *scp;
984263bc 523
6bc31f17 524 crit_enter();
984263bc
MD
525 if (SC_VTY(dev) != SC_CONSOLECTL) {
526 scp = SC_STAT(tp->t_dev);
527 /* were we in the middle of the VT switching process? */
528 DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
984263bc
MD
529 if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
530 cons_unavail = FALSE;
6bc31f17
MD
531 /*
532 * note: must be called from a critical section because finish_vt_rel
533 * will call do_switch_scr which releases it temporarily
534 */
535 if (finish_vt_rel(scp, TRUE) == 0) /* force release */
984263bc
MD
536 DPRINTF(5, ("reset WAIT_REL, "));
537 if (finish_vt_acq(scp) == 0) /* force acknowledge */
538 DPRINTF(5, ("reset WAIT_ACQ, "));
539#if not_yet_done
540 if (scp == &main_console) {
541 scp->pid = 0;
542 scp->proc = NULL;
543 scp->smode.mode = VT_AUTO;
544 }
545 else {
546 sc_vtb_destroy(&scp->vtb);
547 sc_vtb_destroy(&scp->scr);
548 sc_free_history_buffer(scp, scp->ysize);
549 SC_STAT(dev) = NULL;
efda3bd0 550 kfree(scp, M_SYSCONS);
984263bc
MD
551 }
552#else
553 scp->pid = 0;
554 scp->proc = NULL;
555 scp->smode.mode = VT_AUTO;
556#endif
557 scp->kbd_mode = K_XLATE;
558 if (scp == scp->sc->cur_scp)
559 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
560 DPRINTF(5, ("done.\n"));
561 }
fef8985e 562 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
984263bc 563 ttyclose(tp);
6bc31f17 564 crit_exit();
cd29885a 565
984263bc
MD
566 return(0);
567}
568
569int
fef8985e 570scread(struct dev_read_args *ap)
984263bc
MD
571{
572 sc_touch_scrn_saver();
fef8985e 573 return (ttyread(ap));
984263bc
MD
574}
575
576static int
577sckbdevent(keyboard_t *thiskbd, int event, void *arg)
578{
579 sc_softc_t *sc;
580 struct tty *cur_tty;
581 int c;
582 size_t len;
583 u_char *cp;
584
59306b36
MD
585 /*
586 * WARNING: In early boot sc->dev may not be setup yet.
587 */
984263bc
MD
588 sc = (sc_softc_t *)arg;
589 /* assert(thiskbd == sc->kbd) */
590
591 switch (event) {
592 case KBDIO_KEYINPUT:
593 break;
594 case KBDIO_UNLOADING:
595 sc->kbd = NULL;
596 sc->keyboard = -1;
597 kbd_release(thiskbd, (void *)&sc->keyboard);
598 return 0;
599 default:
600 return EINVAL;
601 }
602
603 /*
604 * Loop while there is still input to get from the keyboard.
605 * I don't think this is nessesary, and it doesn't fix
606 * the Xaccel-2.1 keyboard hang, but it can't hurt. XXX
607 */
608 while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
984263bc
MD
609 cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
610 if (!ISTTYOPEN(cur_tty)) {
611 cur_tty = sc_console_tty;
612 if (!ISTTYOPEN(cur_tty))
613 continue;
614 }
615
616 if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
617 continue;
618
619 switch (KEYFLAGS(c)) {
620 case 0x0000: /* normal key */
621 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
622 break;
623 case FKEY: /* function key, return string */
624 cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
625 if (cp != NULL) {
626 while (len-- > 0)
627 (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
628 }
629 break;
630 case MKEY: /* meta is active, prepend ESC */
631 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
632 (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
633 break;
634 case BKEY: /* backtab fixed sequence (esc [ Z) */
635 (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
636 (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
637 (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
638 break;
639 }
640 }
641
642 sc->cur_scp->status |= MOUSE_HIDDEN;
643
644 return 0;
645}
646
647static int
648scparam(struct tty *tp, struct termios *t)
649{
650 tp->t_ispeed = t->c_ispeed;
651 tp->t_ospeed = t->c_ospeed;
652 tp->t_cflag = t->c_cflag;
653 return 0;
654}
655
656int
fef8985e 657scioctl(struct dev_ioctl_args *ap)
984263bc 658{
b13267a5 659 cdev_t dev = ap->a_head.a_dev;
fef8985e
MD
660 u_long cmd = ap->a_cmd;
661 caddr_t data = ap->a_data;
662 int flag = ap->a_fflag;
984263bc
MD
663 int error;
664 int i;
665 struct tty *tp;
666 sc_softc_t *sc;
667 scr_stat *scp;
dadab5e9 668
984263bc
MD
669 tp = dev->si_tty;
670
fef8985e 671 error = sc_vid_ioctl(tp, cmd, data, flag);
984263bc
MD
672 if (error != ENOIOCTL)
673 return error;
674
675#ifndef SC_NO_HISTORY
fef8985e 676 error = sc_hist_ioctl(tp, cmd, data, flag);
984263bc
MD
677 if (error != ENOIOCTL)
678 return error;
679#endif
680
681#ifndef SC_NO_SYSMOUSE
fef8985e 682 error = sc_mouse_ioctl(tp, cmd, data, flag);
984263bc
MD
683 if (error != ENOIOCTL)
684 return error;
685#endif
686
687 scp = SC_STAT(tp->t_dev);
688 /* assert(scp != NULL) */
689 /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
690 sc = scp->sc;
691
692 if (scp->tsw) {
fef8985e 693 error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag);
984263bc
MD
694 if (error != ENOIOCTL)
695 return error;
696 }
697
698 switch (cmd) { /* process console hardware related ioctl's */
699
700 case GIO_ATTR: /* get current attributes */
701 /* this ioctl is not processed here, but in the terminal emulator */
702 return ENOTTY;
703
704 case GIO_COLOR: /* is this a color console ? */
705 *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
706 return 0;
707
708 case CONS_BLANKTIME: /* set screen saver timeout (0 = no saver) */
709 if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
710 return EINVAL;
6bc31f17 711 crit_enter();
984263bc
MD
712 scrn_blank_time = *(int *)data;
713 run_scrn_saver = (scrn_blank_time != 0);
6bc31f17 714 crit_exit();
984263bc
MD
715 return 0;
716
717 case CONS_CURSORTYPE: /* set cursor type blink/noblink */
6bc31f17 718 crit_enter();
984263bc
MD
719 if (!ISGRAPHSC(sc->cur_scp))
720 sc_remove_cursor_image(sc->cur_scp);
721 if ((*(int*)data) & 0x01)
722 sc->flags |= SC_BLINK_CURSOR;
723 else
724 sc->flags &= ~SC_BLINK_CURSOR;
725 if ((*(int*)data) & 0x02) {
726 sc->flags |= SC_CHAR_CURSOR;
727 } else
728 sc->flags &= ~SC_CHAR_CURSOR;
729 /*
730 * The cursor shape is global property; all virtual consoles
731 * are affected. Update the cursor in the current console...
732 */
733 if (!ISGRAPHSC(sc->cur_scp)) {
734 sc_set_cursor_image(sc->cur_scp);
735 sc_draw_cursor_image(sc->cur_scp);
736 }
6bc31f17 737 crit_exit();
984263bc
MD
738 return 0;
739
740 case CONS_BELLTYPE: /* set bell type sound/visual */
741 if ((*(int *)data) & 0x01)
742 sc->flags |= SC_VISUAL_BELL;
743 else
744 sc->flags &= ~SC_VISUAL_BELL;
745 if ((*(int *)data) & 0x02)
746 sc->flags |= SC_QUIET_BELL;
747 else
748 sc->flags &= ~SC_QUIET_BELL;
749 return 0;
750
751 case CONS_GETINFO: /* get current (virtual) console info */
752 {
753 vid_info_t *ptr = (vid_info_t*)data;
754 if (ptr->size == sizeof(struct vid_info)) {
755 ptr->m_num = sc->cur_scp->index;
756 ptr->font_size = scp->font_size;
757 ptr->mv_col = scp->xpos;
758 ptr->mv_row = scp->ypos;
759 ptr->mv_csz = scp->xsize;
760 ptr->mv_rsz = scp->ysize;
761 /*
762 * The following fields are filled by the terminal emulator. XXX
763 *
764 * ptr->mv_norm.fore
765 * ptr->mv_norm.back
766 * ptr->mv_rev.fore
767 * ptr->mv_rev.back
768 */
769 ptr->mv_grfc.fore = 0; /* not supported */
770 ptr->mv_grfc.back = 0; /* not supported */
771 ptr->mv_ovscan = scp->border;
772 if (scp == sc->cur_scp)
773 save_kbd_state(scp);
774 ptr->mk_keylock = scp->status & LOCK_MASK;
775 return 0;
776 }
777 return EINVAL;
778 }
779
780 case CONS_GETVERS: /* get version number */
781 *(int*)data = 0x200; /* version 2.0 */
782 return 0;
783
784 case CONS_IDLE: /* see if the screen has been idle */
785 /*
786 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
787 * the user process may have been writing something on the
788 * screen and syscons is not aware of it. Declare the screen
789 * is NOT idle if it is in one of these modes. But there is
790 * an exception to it; if a screen saver is running in the
791 * graphics mode in the current screen, we should say that the
792 * screen has been idle.
793 */
794 *(int *)data = (sc->flags & SC_SCRN_IDLE)
795 && (!ISGRAPHSC(sc->cur_scp)
796 || (sc->cur_scp->status & SAVER_RUNNING));
797 return 0;
798
799 case CONS_SAVERMODE: /* set saver mode */
800 switch(*(int *)data) {
801 case CONS_NO_SAVER:
802 case CONS_USR_SAVER:
803 /* if a LKM screen saver is running, stop it first. */
804 scsplash_stick(FALSE);
805 saver_mode = *(int *)data;
6bc31f17 806 crit_enter();
984263bc
MD
807#if NSPLASH > 0
808 if ((error = wait_scrn_saver_stop(NULL))) {
6bc31f17 809 crit_exit();
984263bc
MD
810 return error;
811 }
812#endif /* NSPLASH */
813 run_scrn_saver = TRUE;
814 if (saver_mode == CONS_USR_SAVER)
815 scp->status |= SAVER_RUNNING;
816 else
817 scp->status &= ~SAVER_RUNNING;
818 scsplash_stick(TRUE);
6bc31f17 819 crit_exit();
984263bc
MD
820 break;
821 case CONS_LKM_SAVER:
6bc31f17 822 crit_enter();
984263bc
MD
823 if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
824 scp->status &= ~SAVER_RUNNING;
825 saver_mode = *(int *)data;
6bc31f17 826 crit_exit();
984263bc
MD
827 break;
828 default:
829 return EINVAL;
830 }
831 return 0;
832
833 case CONS_SAVERSTART: /* immediately start/stop the screen saver */
834 /*
835 * Note that this ioctl does not guarantee the screen saver
836 * actually starts or stops. It merely attempts to do so...
837 */
6bc31f17 838 crit_enter();
984263bc
MD
839 run_scrn_saver = (*(int *)data != 0);
840 if (run_scrn_saver)
841 sc->scrn_time_stamp -= scrn_blank_time;
6bc31f17 842 crit_exit();
984263bc
MD
843 return 0;
844
845 case CONS_SCRSHOT: /* get a screen shot */
846 {
847 scrshot_t *ptr = (scrshot_t*)data;
6bc31f17 848 crit_enter();
984263bc 849 if (ISGRAPHSC(scp)) {
6bc31f17 850 crit_exit();
984263bc
MD
851 return EOPNOTSUPP;
852 }
853 if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
6bc31f17 854 crit_exit();
984263bc
MD
855 return EINVAL;
856 }
857 copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
47d95543 858 ptr->xsize * ptr->ysize * sizeof(uint16_t));
6bc31f17 859 crit_exit();
984263bc
MD
860 return 0;
861 }
862
863 case VT_SETMODE: /* set screen switcher mode */
864 {
865 struct vt_mode *mode;
866
867 mode = (struct vt_mode *)data;
868 DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
869 if (scp->smode.mode == VT_PROCESS) {
fef8985e 870 if (scp->proc == pfind(scp->pid) && scp->proc != curproc) {
984263bc
MD
871 DPRINTF(5, ("error EPERM\n"));
872 return EPERM;
873 }
874 }
6bc31f17 875 crit_enter();
984263bc
MD
876 if (mode->mode == VT_AUTO) {
877 scp->smode.mode = VT_AUTO;
878 scp->proc = NULL;
879 scp->pid = 0;
880 DPRINTF(5, ("VT_AUTO, "));
881 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
882 cons_unavail = FALSE;
6bc31f17
MD
883 /*
884 * note: must be called from a critical section because
885 * finish_vt_rel will call do_switch_scr which releases it
886 * temporarily.
887 */
888 if (finish_vt_rel(scp, TRUE) == 0)
984263bc
MD
889 DPRINTF(5, ("reset WAIT_REL, "));
890 if (finish_vt_acq(scp) == 0)
891 DPRINTF(5, ("reset WAIT_ACQ, "));
892 } else {
893 if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
894 || !ISSIGVALID(mode->frsig)) {
6bc31f17 895 crit_exit();
984263bc
MD
896 DPRINTF(5, ("error EINVAL\n"));
897 return EINVAL;
898 }
fef8985e 899 DPRINTF(5, ("VT_PROCESS %d, ", curproc->p_pid));
984263bc 900 bcopy(data, &scp->smode, sizeof(struct vt_mode));
fef8985e 901 scp->proc = curproc;
984263bc
MD
902 scp->pid = scp->proc->p_pid;
903 if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
904 cons_unavail = TRUE;
905 }
6bc31f17 906 crit_exit();
984263bc
MD
907 DPRINTF(5, ("\n"));
908 return 0;
909 }
910
911 case VT_GETMODE: /* get screen switcher mode */
912 bcopy(&scp->smode, data, sizeof(struct vt_mode));
913 return 0;
914
915 case VT_RELDISP: /* screen switcher ioctl */
6bc31f17 916 crit_enter();
984263bc
MD
917 /*
918 * This must be the current vty which is in the VT_PROCESS
919 * switching mode...
920 */
921 if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
6bc31f17 922 crit_exit();
984263bc
MD
923 return EINVAL;
924 }
925 /* ...and this process is controlling it. */
fef8985e 926 if (scp->proc != curproc) {
6bc31f17 927 crit_exit();
984263bc
MD
928 return EPERM;
929 }
930 error = EINVAL;
931 switch(*(int *)data) {
932 case VT_FALSE: /* user refuses to release screen, abort */
6bc31f17
MD
933 /*
934 * note: must be called from a critical section because
935 * finish_vt_rel will call do_switch_scr which releases it
936 * temporarily.
937 */
938 if ((error = finish_vt_rel(scp, FALSE)) == 0)
984263bc
MD
939 DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
940 break;
941 case VT_TRUE: /* user has released screen, go on */
6bc31f17
MD
942 /*
943 * note: must be called from a critical section because
944 * finish_vt_rel will call do_switch_scr which releases it
945 * temporarily.
946 */
947 if ((error = finish_vt_rel(scp, TRUE)) == 0)
984263bc
MD
948 DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
949 break;
950 case VT_ACKACQ: /* acquire acknowledged, switch completed */
951 if ((error = finish_vt_acq(scp)) == 0)
952 DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
953 break;
954 default:
955 break;
956 }
6bc31f17 957 crit_exit();
984263bc
MD
958 return error;
959
960 case VT_OPENQRY: /* return free virtual console */
961 for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
962 tp = VIRTUAL_TTY(sc, i);
963 if (!ISTTYOPEN(tp)) {
964 *(int *)data = i + 1;
965 return 0;
966 }
967 }
968 return EINVAL;
969
970 case VT_ACTIVATE: /* switch to screen *data */
971 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
6bc31f17 972 crit_enter();
984263bc 973 sc_clean_up(sc->cur_scp);
6bc31f17 974 crit_exit();
984263bc
MD
975 return sc_switch_scr(sc, i);
976
977 case VT_WAITACTIVE: /* wait for switch to occur */
978 i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
979 if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
980 return EINVAL;
6bc31f17 981 crit_enter();
984263bc 982 error = sc_clean_up(sc->cur_scp);
6bc31f17 983 crit_exit();
984263bc
MD
984 if (error)
985 return error;
986 scp = SC_STAT(SC_DEV(sc, i));
987 if (scp == scp->sc->cur_scp)
988 return 0;
f3628b86
MD
989 error = tsleep((caddr_t)&scp->smode, PCATCH, "waitvt", 0);
990 /* May return ERESTART */
984263bc
MD
991 return error;
992
993 case VT_GETACTIVE: /* get active vty # */
994 *(int *)data = sc->cur_scp->index + 1;
995 return 0;
996
997 case VT_GETINDEX: /* get this vty # */
998 *(int *)data = scp->index + 1;
999 return 0;
1000
1001 case VT_LOCKSWITCH: /* prevent vty switching */
1002 if ((*(int *)data) & 0x01)
1003 sc->flags |= SC_SCRN_VTYLOCK;
1004 else
1005 sc->flags &= ~SC_SCRN_VTYLOCK;
1006 return 0;
1007
1008 case KDENABIO: /* allow io operations */
895c1f85 1009 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
984263bc
MD
1010 if (error != 0)
1011 return error;
1012 if (securelevel > 0)
1013 return EPERM;
0855a2af 1014#if defined(__i386__)
08f2f1bb 1015 curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
0855a2af
JG
1016#elif defined(__amd64__)
1017 curthread->td_lwp->lwp_md.md_regs->tf_rflags |= PSL_IOPL;
1018#endif
984263bc
MD
1019 return 0;
1020
1021 case KDDISABIO: /* disallow io operations (default) */
0855a2af 1022#if defined(__i386__)
08f2f1bb 1023 curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;
0855a2af
JG
1024#elif defined(__amd64__)
1025 curthread->td_lwp->lwp_md.md_regs->tf_rflags &= ~PSL_IOPL;
1026#endif
984263bc
MD
1027 return 0;
1028
1029 case KDSKBSTATE: /* set keyboard state (locks) */
1030 if (*(int *)data & ~LOCK_MASK)
1031 return EINVAL;
1032 scp->status &= ~LOCK_MASK;
1033 scp->status |= *(int *)data;
1034 if (scp == sc->cur_scp)
1035 update_kbd_state(scp, scp->status, LOCK_MASK);
1036 return 0;
1037
1038 case KDGKBSTATE: /* get keyboard state (locks) */
1039 if (scp == sc->cur_scp)
1040 save_kbd_state(scp);
1041 *(int *)data = scp->status & LOCK_MASK;
1042 return 0;
1043
1044 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1045 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1046 error = kbd_ioctl(sc->kbd, cmd, data);
1047 if (error == ENOIOCTL)
1048 error = ENODEV;
1049 return error;
1050
1051 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1052 if (*(int *)data & ~0x7f)
1053 return EINVAL;
1054 error = kbd_ioctl(sc->kbd, cmd, data);
1055 if (error == ENOIOCTL)
1056 error = ENODEV;
1057 return error;
1058
1059 case KDSKBMODE: /* set keyboard mode */
1060 switch (*(int *)data) {
1061 case K_XLATE: /* switch to XLT ascii mode */
1062 case K_RAW: /* switch to RAW scancode mode */
1063 case K_CODE: /* switch to CODE mode */
1064 scp->kbd_mode = *(int *)data;
1065 if (scp == sc->cur_scp)
1066 kbd_ioctl(sc->kbd, cmd, data);
1067 return 0;
1068 default:
1069 return EINVAL;
1070 }
1071 /* NOT REACHED */
1072
1073 case KDGKBMODE: /* get keyboard mode */
1074 *(int *)data = scp->kbd_mode;
1075 return 0;
1076
1077 case KDGKBINFO:
1078 error = kbd_ioctl(sc->kbd, cmd, data);
1079 if (error == ENOIOCTL)
1080 error = ENODEV;
1081 return error;
1082
1083 case KDMKTONE: /* sound the bell */
1084 if (*(int*)data)
1085 sc_bell(scp, (*(int*)data)&0xffff,
1086 (((*(int*)data)>>16)&0xffff)*hz/1000);
1087 else
1088 sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1089 return 0;
1090
1091 case KIOCSOUND: /* make tone (*data) hz */
1092 if (scp == sc->cur_scp) {
1093 if (*(int *)data)
1094 return sc_tone(*(int *)data);
1095 else
1096 return sc_tone(0);
1097 }
1098 return 0;
1099
1100 case KDGKBTYPE: /* get keyboard type */
1101 error = kbd_ioctl(sc->kbd, cmd, data);
1102 if (error == ENOIOCTL) {
1103 /* always return something? XXX */
1104 *(int *)data = 0;
1105 }
1106 return 0;
1107
1108 case KDSETLED: /* set keyboard LED status */
1109 if (*(int *)data & ~LED_MASK) /* FIXME: LOCK_MASK? */
1110 return EINVAL;
1111 scp->status &= ~LED_MASK;
1112 scp->status |= *(int *)data;
1113 if (scp == sc->cur_scp)
1114 update_kbd_leds(scp, scp->status);
1115 return 0;
1116
1117 case KDGETLED: /* get keyboard LED status */
1118 if (scp == sc->cur_scp)
1119 save_kbd_state(scp);
1120 *(int *)data = scp->status & LED_MASK;
1121 return 0;
1122
1123 case CONS_SETKBD: /* set the new keyboard */
1124 {
1125 keyboard_t *newkbd;
1126
6bc31f17 1127 crit_enter();
984263bc
MD
1128 newkbd = kbd_get_keyboard(*(int *)data);
1129 if (newkbd == NULL) {
6bc31f17 1130 crit_exit();
984263bc
MD
1131 return EINVAL;
1132 }
1133 error = 0;
1134 if (sc->kbd != newkbd) {
1135 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1136 (void *)&sc->keyboard, sckbdevent, sc);
1137 /* i == newkbd->kb_index */
1138 if (i >= 0) {
1139 if (sc->kbd != NULL) {
1140 save_kbd_state(sc->cur_scp);
1141 kbd_release(sc->kbd, (void *)&sc->keyboard);
1142 }
1143 sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1144 sc->keyboard = i;
1145 kbd_ioctl(sc->kbd, KDSKBMODE,
1146 (caddr_t)&sc->cur_scp->kbd_mode);
1147 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1148 LOCK_MASK);
1149 } else {
1150 error = EPERM; /* XXX */
1151 }
1152 }
6bc31f17 1153 crit_exit();
984263bc
MD
1154 return error;
1155 }
1156
1157 case CONS_RELKBD: /* release the current keyboard */
6bc31f17 1158 crit_enter();
984263bc
MD
1159 error = 0;
1160 if (sc->kbd != NULL) {
1161 save_kbd_state(sc->cur_scp);
1162 error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1163 if (error == 0) {
1164 sc->kbd = NULL;
1165 sc->keyboard = -1;
1166 }
1167 }
6bc31f17 1168 crit_exit();
984263bc
MD
1169 return error;
1170
1171 case CONS_GETTERM: /* get the current terminal emulator info */
1172 {
1173 sc_term_sw_t *sw;
1174
1175 if (((term_info_t *)data)->ti_index == 0) {
1176 sw = scp->tsw;
1177 } else {
1178 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1179 }
1180 if (sw != NULL) {
1181 strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1182 sizeof(((term_info_t *)data)->ti_name));
1183 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1184 sizeof(((term_info_t *)data)->ti_desc));
1185 ((term_info_t *)data)->ti_flags = 0;
1186 return 0;
1187 } else {
1188 ((term_info_t *)data)->ti_name[0] = '\0';
1189 ((term_info_t *)data)->ti_desc[0] = '\0';
1190 ((term_info_t *)data)->ti_flags = 0;
1191 return EINVAL;
1192 }
1193 }
1194
1195 case CONS_SETTERM: /* set the current terminal emulator */
6bc31f17 1196 crit_enter();
984263bc
MD
1197 error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1198 /* FIXME: what if scp == sc_console! XXX */
6bc31f17 1199 crit_exit();
984263bc
MD
1200 return error;
1201
1202 case GIO_SCRNMAP: /* get output translation table */
1203 bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1204 return 0;
1205
1206 case PIO_SCRNMAP: /* set output translation table */
1207 bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1208 for (i=0; i<sizeof(sc->scr_map); i++) {
1209 sc->scr_rmap[sc->scr_map[i]] = i;
1210 }
1211 return 0;
1212
1213 case GIO_KEYMAP: /* get keyboard translation table */
1214 case PIO_KEYMAP: /* set keyboard translation table */
1215 case GIO_DEADKEYMAP: /* get accent key translation table */
1216 case PIO_DEADKEYMAP: /* set accent key translation table */
1217 case GETFKEY: /* get function key string */
1218 case SETFKEY: /* set function key string */
1219 error = kbd_ioctl(sc->kbd, cmd, data);
1220 if (error == ENOIOCTL)
1221 error = ENODEV;
1222 return error;
1223
1224#ifndef SC_NO_FONT_LOADING
1225
1226 case PIO_FONT8x8: /* set 8x8 dot font */
1227 if (!ISFONTAVAIL(sc->adp->va_flags))
1228 return ENXIO;
1229 bcopy(data, sc->font_8, 8*256);
1230 sc->fonts_loaded |= FONT_8;
1231 /*
1232 * FONT KLUDGE
1233 * Always use the font page #0. XXX
1234 * Don't load if the current font size is not 8x8.
1235 */
1236 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1237 sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
1238 return 0;
1239
1240 case GIO_FONT8x8: /* get 8x8 dot font */
1241 if (!ISFONTAVAIL(sc->adp->va_flags))
1242 return ENXIO;
1243 if (sc->fonts_loaded & FONT_8) {
1244 bcopy(sc->font_8, data, 8*256);
1245 return 0;
1246 }
1247 else
1248 return ENXIO;
1249
1250 case PIO_FONT8x14: /* set 8x14 dot font */
1251 if (!ISFONTAVAIL(sc->adp->va_flags))
1252 return ENXIO;
1253 bcopy(data, sc->font_14, 14*256);
1254 sc->fonts_loaded |= FONT_14;
1255 /*
1256 * FONT KLUDGE
1257 * Always use the font page #0. XXX
1258 * Don't load if the current font size is not 8x14.
1259 */
1260 if (ISTEXTSC(sc->cur_scp)
1261 && (sc->cur_scp->font_size >= 14)
1262 && (sc->cur_scp->font_size < 16))
1263 sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
1264 return 0;
1265
1266 case GIO_FONT8x14: /* get 8x14 dot font */
1267 if (!ISFONTAVAIL(sc->adp->va_flags))
1268 return ENXIO;
1269 if (sc->fonts_loaded & FONT_14) {
1270 bcopy(sc->font_14, data, 14*256);
1271 return 0;
1272 }
1273 else
1274 return ENXIO;
1275
1276 case PIO_FONT8x16: /* set 8x16 dot font */
1277 if (!ISFONTAVAIL(sc->adp->va_flags))
1278 return ENXIO;
1279 bcopy(data, sc->font_16, 16*256);
1280 sc->fonts_loaded |= FONT_16;
1281 /*
1282 * FONT KLUDGE
1283 * Always use the font page #0. XXX
1284 * Don't load if the current font size is not 8x16.
1285 */
1286 if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1287 sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
1288 return 0;
1289
1290 case GIO_FONT8x16: /* get 8x16 dot font */
1291 if (!ISFONTAVAIL(sc->adp->va_flags))
1292 return ENXIO;
1293 if (sc->fonts_loaded & FONT_16) {
1294 bcopy(sc->font_16, data, 16*256);
1295 return 0;
1296 }
1297 else
1298 return ENXIO;
1299
1300#endif /* SC_NO_FONT_LOADING */
1301
1302 default:
1303 break;
1304 }
1305
fef8985e 1306 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, ap->a_cred);
984263bc
MD
1307 if (error != ENOIOCTL)
1308 return(error);
1309 error = ttioctl(tp, cmd, data, flag);
1310 if (error != ENOIOCTL)
1311 return(error);
1312 return(ENOTTY);
1313}
1314
1315static void
1316scstart(struct tty *tp)
1317{
1318 struct clist *rbp;
6bc31f17 1319 int len;
984263bc
MD
1320 u_char buf[PCBURST];
1321 scr_stat *scp = SC_STAT(tp->t_dev);
1322
2123da6d
CP
1323 if (scp->status & SLKED ||
1324 (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
984263bc 1325 return;
6bc31f17 1326 crit_enter();
984263bc
MD
1327 if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1328 tp->t_state |= TS_BUSY;
1329 rbp = &tp->t_outq;
1330 while (rbp->c_cc) {
1331 len = q_to_b(rbp, buf, PCBURST);
6bc31f17 1332 crit_exit();
984263bc 1333 sc_puts(scp, buf, len);
6bc31f17 1334 crit_enter();
984263bc
MD
1335 }
1336 tp->t_state &= ~TS_BUSY;
1337 ttwwakeup(tp);
1338 }
6bc31f17 1339 crit_exit();
984263bc
MD
1340}
1341
1342static void
1343sccnprobe(struct consdev *cp)
1344{
984263bc
MD
1345 int unit;
1346 int flags;
1347
1348 cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1349
1350 /* a video card is always required */
1351 if (!scvidprobe(unit, flags, TRUE))
1352 cp->cn_pri = CN_DEAD;
1353
1354 /* syscons will become console even when there is no keyboard */
1355 sckbdprobe(unit, flags, TRUE);
1356
1357 if (cp->cn_pri == CN_DEAD)
1358 return;
1359
1360 /* initialize required fields */
ce81f184 1361 cp->cn_probegood = 1;
984263bc
MD
1362}
1363
1364static void
1365sccninit(struct consdev *cp)
1366{
984263bc
MD
1367 int unit;
1368 int flags;
1369
1370 sc_get_cons_priority(&unit, &flags);
1371 scinit(unit, flags | SC_KERNEL_CONSOLE);
1372 sc_console_unit = unit;
ce81f184
MD
1373 sc_console = sc_get_softc(unit, SC_KERNEL_CONSOLE)->console_scp;
1374}
1375
1376static void
1377sccninit_fini(struct consdev *cp)
1378{
cd29885a
MD
1379 if (cctl_dev == NULL)
1380 kprintf("sccninit_fini: WARNING: cctl_dev is NULL!\n");
1381 cp->cn_dev = cctl_dev;
984263bc
MD
1382}
1383
1384static void
1385sccnterm(struct consdev *cp)
1386{
1387 /* we are not the kernel console any more, release everything */
1388
1389 if (sc_console_unit < 0)
1390 return; /* shouldn't happen */
1391
984263bc
MD
1392#if 0 /* XXX */
1393 sc_clear_screen(sc_console);
1394 sccnupdate(sc_console);
1395#endif
1396 scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1397 sc_console_unit = -1;
1398 sc_console = NULL;
984263bc
MD
1399}
1400
984263bc 1401static void
ce81f184 1402sccnputc(void *private, int c)
984263bc
MD
1403{
1404 u_char buf[1];
1405 scr_stat *scp = sc_console;
1406 void *save;
1407#ifndef SC_NO_HISTORY
1408 struct tty *tp;
1409#endif /* !SC_NO_HISTORY */
984263bc
MD
1410
1411 /* assert(sc_console != NULL) */
1412
1413#ifndef SC_NO_HISTORY
1414 if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1415 scp->status &= ~SLKED;
1416 update_kbd_state(scp, scp->status, SLKED);
1417 if (scp->status & BUFFER_SAVED) {
1418 if (!sc_hist_restore(scp))
1419 sc_remove_cutmarking(scp);
1420 scp->status &= ~BUFFER_SAVED;
1421 scp->status |= CURSOR_ENABLED;
1422 sc_draw_cursor_image(scp);
1423 }
1424 tp = VIRTUAL_TTY(scp->sc, scp->index);
1425 if (ISTTYOPEN(tp))
1426 scstart(tp);
1427 }
1428#endif /* !SC_NO_HISTORY */
1429
1430 save = scp->ts;
1431 if (kernel_console_ts != NULL)
1432 scp->ts = kernel_console_ts;
1433 buf[0] = c;
1434 sc_puts(scp, buf, 1);
1435 scp->ts = save;
1436
6bc31f17 1437 crit_enter();
984263bc 1438 sccnupdate(scp);
6bc31f17 1439 crit_exit();
984263bc
MD
1440}
1441
1442static int
ce81f184 1443sccngetc(void *private)
984263bc
MD
1444{
1445 return sccngetch(0);
1446}
1447
1448static int
ce81f184 1449sccncheckc(void *private)
984263bc
MD
1450{
1451 return sccngetch(SCGETC_NONBLOCK);
1452}
1453
1454static void
ce81f184 1455sccndbctl(void *private, int on)
984263bc
MD
1456{
1457 /* assert(sc_console_unit >= 0) */
1458 /* try to switch to the kernel console screen */
1459 if (on && debugger == 0) {
1460 /*
1461 * TRY to make sure the screen saver is stopped,
1462 * and the screen is updated before switching to
1463 * the vty0.
1464 */
1465 scrn_timer(NULL);
1466 if (!cold
1467 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1468 && sc_console->smode.mode == VT_AUTO) {
1469 sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
1470 sc_switch_scr(sc_console->sc, sc_console->index);
1471 }
1472 }
1473 if (on)
1474 ++debugger;
1475 else
1476 --debugger;
1477}
1478
1479static int
1480sccngetch(int flags)
1481{
1482 static struct fkeytab fkey;
1483 static int fkeycp;
1484 scr_stat *scp;
1485 u_char *p;
1486 int cur_mode;
984263bc
MD
1487 int c;
1488
6bc31f17 1489 crit_enter();
984263bc
MD
1490 /* assert(sc_console != NULL) */
1491
1492 /*
1493 * Stop the screen saver and update the screen if necessary.
1494 * What if we have been running in the screen saver code... XXX
1495 */
1496 sc_touch_scrn_saver();
1497 scp = sc_console->sc->cur_scp; /* XXX */
1498 sccnupdate(scp);
1499
1500 if (fkeycp < fkey.len) {
6bc31f17 1501 crit_exit();
984263bc
MD
1502 return fkey.str[fkeycp++];
1503 }
1504
1505 if (scp->sc->kbd == NULL) {
6bc31f17 1506 crit_exit();
984263bc
MD
1507 return -1;
1508 }
1509
1510 /*
1511 * Make sure the keyboard is accessible even when the kbd device
1512 * driver is disabled.
1513 */
1514 kbd_enable(scp->sc->kbd);
1515
1516 /* we shall always use the keyboard in the XLATE mode here */
1517 cur_mode = scp->kbd_mode;
1518 scp->kbd_mode = K_XLATE;
1519 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1520
1521 kbd_poll(scp->sc->kbd, TRUE);
1522 c = scgetc(scp->sc, SCGETC_CN | flags);
1523 kbd_poll(scp->sc->kbd, FALSE);
1524
1525 scp->kbd_mode = cur_mode;
1526 kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1527 kbd_disable(scp->sc->kbd);
6bc31f17 1528 crit_exit();
984263bc
MD
1529
1530 switch (KEYFLAGS(c)) {
1531 case 0: /* normal char */
1532 return KEYCHAR(c);
1533 case FKEY: /* function key */
1534 p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1535 fkey.len = fkeycp;
1536 if ((p != NULL) && (fkey.len > 0)) {
1537 bcopy(p, fkey.str, fkey.len);
1538 fkeycp = 1;
1539 return fkey.str[0];
1540 }
1541 return c; /* XXX */
1542 case NOKEY:
1543 case ERRKEY:
1544 default:
1545 return -1;
1546 }
1547 /* NOT REACHED */
1548}
1549
1550static void
1551sccnupdate(scr_stat *scp)
1552{
1553 /* this is a cut-down version of scrn_timer()... */
1554
1555 if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress)
1556 return;
1557
1558 if (debugger > 0 || panicstr || shutdown_in_progress) {
1559 sc_touch_scrn_saver();
1560 } else if (scp != scp->sc->cur_scp) {
1561 return;
1562 }
1563
1564 if (!run_scrn_saver)
1565 scp->sc->flags &= ~SC_SCRN_IDLE;
1566#if NSPLASH > 0
1567 if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1568 if (scp->sc->flags & SC_SCRN_BLANKED)
1569 stop_scrn_saver(scp->sc, current_saver);
1570#endif /* NSPLASH */
1571
1572 if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1573 || scp->sc->switch_in_progress)
1574 return;
1575 /*
1576 * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1577 * when write_in_progress is non-zero. XXX
1578 */
1579
1580 if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1581 scrn_update(scp, TRUE);
1582}
1583
1584static void
1585scrn_timer(void *arg)
1586{
1587 static int kbd_interval = 0;
1588 struct timeval tv;
1589 sc_softc_t *sc;
1590 scr_stat *scp;
1591 int again;
984263bc
MD
1592
1593 again = (arg != NULL);
1594 if (arg != NULL)
1595 sc = (sc_softc_t *)arg;
1596 else if (sc_console != NULL)
1597 sc = sc_console->sc;
1598 else
1599 return;
1600
1601 /* don't do anything when we are performing some I/O operations */
1602 if (sc->font_loading_in_progress || sc->videoio_in_progress) {
1603 if (again)
356bb9cd 1604 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
984263bc
MD
1605 return;
1606 }
6bc31f17 1607 crit_enter();
984263bc
MD
1608
1609 if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1610 /* try to allocate a keyboard automatically */
1611 if (++kbd_interval >= 25) {
1612 sc->keyboard = kbd_allocate("*", -1, (void *)&sc->keyboard,
1613 sckbdevent, sc);
1614 if (sc->keyboard >= 0) {
1615 sc->kbd = kbd_get_keyboard(sc->keyboard);
1616 kbd_ioctl(sc->kbd, KDSKBMODE,
1617 (caddr_t)&sc->cur_scp->kbd_mode);
1618 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1619 LOCK_MASK);
1620 }
1621 kbd_interval = 0;
1622 }
1623 }
1624
984263bc
MD
1625 /* should we stop the screen saver? */
1626 getmicrouptime(&tv);
1627 if (debugger > 0 || panicstr || shutdown_in_progress)
1628 sc_touch_scrn_saver();
1629 if (run_scrn_saver) {
1630 if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1631 sc->flags |= SC_SCRN_IDLE;
1632 else
1633 sc->flags &= ~SC_SCRN_IDLE;
1634 } else {
1635 sc->scrn_time_stamp = tv.tv_sec;
1636 sc->flags &= ~SC_SCRN_IDLE;
1637 if (scrn_blank_time > 0)
1638 run_scrn_saver = TRUE;
1639 }
1640#if NSPLASH > 0
1641 if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1642 if (sc->flags & SC_SCRN_BLANKED)
1643 stop_scrn_saver(sc, current_saver);
1644#endif /* NSPLASH */
1645
1646 /* should we just return ? */
1647 if (sc->blink_in_progress || sc->switch_in_progress
1648 || sc->write_in_progress) {
1649 if (again)
356bb9cd 1650 callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
6bc31f17 1651 crit_exit();
984263bc
MD
1652 return;
1653 }
1654
1655 /* Update the screen */
1656 scp = sc->cur_scp; /* cur_scp may have changed... */
1657 if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1658 scrn_update(scp, TRUE);
1659
1660#if NSPLASH > 0
1661 /* should we activate the screen saver? */
1662 if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1663 if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1664 (*current_saver)(sc, TRUE);
1665#endif /* NSPLASH */
1666
1667 if (again)
356bb9cd 1668 callout_reset(&sc->scrn_timer_ch, hz / 25, scrn_timer, sc);
6bc31f17 1669 crit_exit();
984263bc
MD
1670}
1671
1672static int
1673and_region(int *s1, int *e1, int s2, int e2)
1674{
1675 if (*e1 < s2 || e2 < *s1)
1676 return FALSE;
1677 *s1 = imax(*s1, s2);
1678 *e1 = imin(*e1, e2);
1679 return TRUE;
1680}
1681
1682static void
1683scrn_update(scr_stat *scp, int show_cursor)
1684{
1685 int start;
1686 int end;
1687 int s;
1688 int e;
1689
1690 /* assert(scp == scp->sc->cur_scp) */
1691
1692 ++scp->sc->videoio_in_progress;
1693
1694#ifndef SC_NO_CUTPASTE
1695 /* remove the previous mouse pointer image if necessary */
1696 if (scp->status & MOUSE_VISIBLE) {
1697 s = scp->mouse_pos;
1698 e = scp->mouse_pos + scp->xsize + 1;
1699 if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1700 || and_region(&s, &e, scp->start, scp->end)
1701 || ((scp->status & CURSOR_ENABLED) &&
1702 (scp->cursor_pos != scp->cursor_oldpos) &&
1703 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1704 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1705 sc_remove_mouse_image(scp);
1706 if (scp->end >= scp->xsize*scp->ysize)
1707 scp->end = scp->xsize*scp->ysize - 1;
1708 }
1709 }
1710#endif /* !SC_NO_CUTPASTE */
1711
1712#if 1
1713 /* debug: XXX */
1714 if (scp->end >= scp->xsize*scp->ysize) {
e3869ec7 1715 kprintf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
984263bc
MD
1716 scp->end = scp->xsize*scp->ysize - 1;
1717 }
1718 if (scp->start < 0) {
e3869ec7 1719 kprintf("scrn_update(): scp->start %d < 0\n", scp->start);
984263bc
MD
1720 scp->start = 0;
1721 }
1722#endif
1723
1724 /* update screen image */
1725 if (scp->start <= scp->end) {
1726 if (scp->mouse_cut_end >= 0) {
1727 /* there is a marked region for cut & paste */
1728 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1729 start = scp->mouse_cut_start;
1730 end = scp->mouse_cut_end;
1731 } else {
1732 start = scp->mouse_cut_end;
1733 end = scp->mouse_cut_start - 1;
1734 }
1735 s = start;
1736 e = end;
1737 /* does the cut-mark region overlap with the update region? */
1738 if (and_region(&s, &e, scp->start, scp->end)) {
1739 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1740 s = 0;
1741 e = start - 1;
1742 if (and_region(&s, &e, scp->start, scp->end))
1743 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1744 s = end + 1;
1745 e = scp->xsize*scp->ysize - 1;
1746 if (and_region(&s, &e, scp->start, scp->end))
1747 (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1748 } else {
1749 (*scp->rndr->draw)(scp, scp->start,
1750 scp->end - scp->start + 1, FALSE);
1751 }
1752 } else {
1753 (*scp->rndr->draw)(scp, scp->start,
1754 scp->end - scp->start + 1, FALSE);
1755 }
1756 }
1757
1758 /* we are not to show the cursor and the mouse pointer... */
1759 if (!show_cursor) {
1760 scp->end = 0;
1761 scp->start = scp->xsize*scp->ysize - 1;
1762 --scp->sc->videoio_in_progress;
1763 return;
1764 }
1765
1766 /* update cursor image */
1767 if (scp->status & CURSOR_ENABLED) {
1768 s = scp->start;
1769 e = scp->end;
1770 /* did cursor move since last time ? */
1771 if (scp->cursor_pos != scp->cursor_oldpos) {
1772 /* do we need to remove old cursor image ? */
1773 if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1774 sc_remove_cursor_image(scp);
1775 sc_draw_cursor_image(scp);
1776 } else {
1777 if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1778 /* cursor didn't move, but has been overwritten */
1779 sc_draw_cursor_image(scp);
1780 else if (scp->sc->flags & SC_BLINK_CURSOR)
1781 /* if it's a blinking cursor, update it */
1782 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1783 sc_inside_cutmark(scp,
1784 scp->cursor_pos));
1785 }
1786 }
1787
1788#ifndef SC_NO_CUTPASTE
1789 /* update "pseudo" mouse pointer image */
1790 if (scp->sc->flags & SC_MOUSE_ENABLED) {
1791 if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1792 scp->status &= ~MOUSE_MOVED;
1793 sc_draw_mouse_image(scp);
1794 }
1795 }
1796#endif /* SC_NO_CUTPASTE */
1797
1798 scp->end = 0;
1799 scp->start = scp->xsize*scp->ysize - 1;
1800
1801 --scp->sc->videoio_in_progress;
1802}
1803
1804#if NSPLASH > 0
1805static int
1806scsplash_callback(int event, void *arg)
1807{
1808 sc_softc_t *sc;
1809 int error;
1810
1811 sc = (sc_softc_t *)arg;
1812
1813 switch (event) {
1814 case SPLASH_INIT:
1815 if (add_scrn_saver(scsplash_saver) == 0) {
1816 sc->flags &= ~SC_SAVER_FAILED;
1817 run_scrn_saver = TRUE;
1818 if (cold && !(boothowto & (RB_VERBOSE | RB_CONFIG))) {
1819 scsplash_stick(TRUE);
1820 (*current_saver)(sc, TRUE);
1821 }
1822 }
1823 return 0;
1824
1825 case SPLASH_TERM:
1826 if (current_saver == scsplash_saver) {
1827 scsplash_stick(FALSE);
1828 error = remove_scrn_saver(scsplash_saver);
1829 if (error)
1830 return error;
1831 }
1832 return 0;
1833
1834 default:
1835 return EINVAL;
1836 }
1837}
1838
1839static void
1840scsplash_saver(sc_softc_t *sc, int show)
1841{
1842 static int busy = FALSE;
1843 scr_stat *scp;
1844
1845 if (busy)
1846 return;
1847 busy = TRUE;
1848
1849 scp = sc->cur_scp;
1850 if (show) {
1851 if (!(sc->flags & SC_SAVER_FAILED)) {
1852 if (!(sc->flags & SC_SCRN_BLANKED))
1853 set_scrn_saver_mode(scp, -1, NULL, 0);
1854 switch (splash(sc->adp, TRUE)) {
1855 case 0: /* succeeded */
1856 break;
1857 case EAGAIN: /* try later */
1858 restore_scrn_saver_mode(scp, FALSE);
1859 sc_touch_scrn_saver(); /* XXX */
1860 break;
1861 default:
1862 sc->flags |= SC_SAVER_FAILED;
1863 scsplash_stick(FALSE);
1864 restore_scrn_saver_mode(scp, TRUE);
e3869ec7 1865 kprintf("scsplash_saver(): failed to put up the image\n");
984263bc
MD
1866 break;
1867 }
1868 }
1869 } else if (!sticky_splash) {
1870 if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1871 restore_scrn_saver_mode(scp, TRUE);
1872 }
1873 busy = FALSE;
1874}
1875
1876static int
1877add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1878{
1879#if 0
1880 int error;
1881
1882 if (current_saver != none_saver) {
1883 error = remove_scrn_saver(current_saver);
1884 if (error)
1885 return error;
1886 }
1887#endif
1888 if (current_saver != none_saver)
1889 return EBUSY;
1890
1891 run_scrn_saver = FALSE;
1892 saver_mode = CONS_LKM_SAVER;
1893 current_saver = this_saver;
1894 return 0;
1895}
1896
1897static int
1898remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1899{
1900 if (current_saver != this_saver)
1901 return EINVAL;
1902
1903#if 0
1904 /*
1905 * In order to prevent `current_saver' from being called by
1906 * the timeout routine `scrn_timer()' while we manipulate
1907 * the saver list, we shall set `current_saver' to `none_saver'
1908 * before stopping the current saver, rather than blocking by `splXX()'.
1909 */
1910 current_saver = none_saver;
1911 if (scrn_blanked)
1912 stop_scrn_saver(this_saver);
1913#endif
1914
1915 /* unblank all blanked screens */
1916 wait_scrn_saver_stop(NULL);
1917 if (scrn_blanked)
1918 return EBUSY;
1919
1920 current_saver = none_saver;
1921 return 0;
1922}
1923
1924static int
1925set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
1926{
984263bc
MD
1927
1928 /* assert(scp == scp->sc->cur_scp) */
6bc31f17 1929 crit_enter();
984263bc
MD
1930 if (!ISGRAPHSC(scp))
1931 sc_remove_cursor_image(scp);
1932 scp->splash_save_mode = scp->mode;
1933 scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
1934 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
1935 scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
1936 scp->sc->flags |= SC_SCRN_BLANKED;
1937 ++scrn_blanked;
6bc31f17 1938 crit_exit();
984263bc
MD
1939 if (mode < 0)
1940 return 0;
1941 scp->mode = mode;
1942 if (set_mode(scp) == 0) {
1943 if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
1944 scp->status |= GRAPHICS_MODE;
1945#ifndef SC_NO_PALETTE_LOADING
1946 if (pal != NULL)
1947 load_palette(scp->sc->adp, pal);
1948#endif
1949 sc_set_border(scp, border);
1950 return 0;
1951 } else {
6bc31f17 1952 crit_enter();
984263bc
MD
1953 scp->mode = scp->splash_save_mode;
1954 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
1955 scp->status |= scp->splash_save_status;
6bc31f17 1956 crit_exit();
984263bc
MD
1957 return 1;
1958 }
1959}
1960
1961static int
1962restore_scrn_saver_mode(scr_stat *scp, int changemode)
1963{
1964 int mode;
1965 int status;
984263bc
MD
1966
1967 /* assert(scp == scp->sc->cur_scp) */
6bc31f17 1968 crit_enter();
984263bc
MD
1969 mode = scp->mode;
1970 status = scp->status;
1971 scp->mode = scp->splash_save_mode;
1972 scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
1973 scp->status |= scp->splash_save_status;
1974 scp->sc->flags &= ~SC_SCRN_BLANKED;
1975 if (!changemode) {
1976 if (!ISGRAPHSC(scp))
1977 sc_draw_cursor_image(scp);
1978 --scrn_blanked;
6bc31f17 1979 crit_exit();
984263bc
MD
1980 return 0;
1981 }
1982 if (set_mode(scp) == 0) {
1983#ifndef SC_NO_PALETTE_LOADING
1984 load_palette(scp->sc->adp, scp->sc->palette);
1985#endif
1986 --scrn_blanked;
6bc31f17 1987 crit_exit();
984263bc
MD
1988 return 0;
1989 } else {
1990 scp->mode = mode;
1991 scp->status = status;
6bc31f17 1992 crit_exit();
984263bc
MD
1993 return 1;
1994 }
1995}
1996
1997static void
1998stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
1999{
2000 (*saver)(sc, FALSE);
2001 run_scrn_saver = FALSE;
2002 /* the screen saver may have chosen not to stop after all... */
2003 if (sc->flags & SC_SCRN_BLANKED)
2004 return;
2005
2006 mark_all(sc->cur_scp);
2007 if (sc->delayed_next_scr)
2008 sc_switch_scr(sc, sc->delayed_next_scr - 1);
2009 wakeup((caddr_t)&scrn_blanked);
2010}
2011
2012static int
2013wait_scrn_saver_stop(sc_softc_t *sc)
2014{
2015 int error = 0;
2016
2017 while (scrn_blanked > 0) {
2018 run_scrn_saver = FALSE;
2019 if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2020 error = 0;
2021 break;
2022 }
377d4740 2023 error = tsleep((caddr_t)&scrn_blanked, PCATCH, "scrsav", 0);
f3628b86
MD
2024 /* May return ERESTART */
2025 if (error)
2026 break;
984263bc
MD
2027 }
2028 run_scrn_saver = FALSE;
2029 return error;
2030}
2031#endif /* NSPLASH */
2032
2033void
2034sc_touch_scrn_saver(void)
2035{
2036 scsplash_stick(FALSE);
2037 run_scrn_saver = FALSE;
2038}
2039
2040int
2041sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2042{
2043 scr_stat *cur_scp;
2044 struct tty *tp;
984263bc
MD
2045
2046 DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2047
2048 /* prevent switch if previously requested */
2049 if (sc->flags & SC_SCRN_VTYLOCK) {
2050 sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2051 sc->cur_scp->bell_duration);
2052 return EPERM;
2053 }
2054
2055 /* delay switch if the screen is blanked or being updated */
2056 if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2057 || sc->blink_in_progress || sc->videoio_in_progress) {
2058 sc->delayed_next_scr = next_scr + 1;
2059 sc_touch_scrn_saver();
2060 DPRINTF(5, ("switch delayed\n"));
2061 return 0;
2062 }
2063
6bc31f17 2064 crit_enter();
984263bc
MD
2065 cur_scp = sc->cur_scp;
2066
2067 /* we are in the middle of the vty switching process... */
2068 if (sc->switch_in_progress
2069 && (cur_scp->smode.mode == VT_PROCESS)
2070 && cur_scp->proc) {
2071 if (cur_scp->proc != pfind(cur_scp->pid)) {
2072 /*
2073 * The controlling process has died!!. Do some clean up.
2074 * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2075 * are not reset here yet; they will be cleared later.
2076 */
2077 DPRINTF(5, ("cur_scp controlling process %d died, ",
2078 cur_scp->pid));
2079 if (cur_scp->status & SWITCH_WAIT_REL) {
2080 /*
2081 * Force the previous switch to finish, but return now
2082 * with error.
6bc31f17
MD
2083 *
2084 * note: must be called from a critical section because
2085 * finish_vt_rel will call do_switch_scr which releases it
2086 * temporarily.
984263bc
MD
2087 */
2088 DPRINTF(5, ("reset WAIT_REL, "));
6bc31f17
MD
2089 finish_vt_rel(cur_scp, TRUE);
2090 crit_exit();
984263bc
MD
2091 DPRINTF(5, ("finishing previous switch\n"));
2092 return EINVAL;
2093 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2094 /* let's assume screen switch has been completed. */
2095 DPRINTF(5, ("reset WAIT_ACQ, "));
2096 finish_vt_acq(cur_scp);
2097 } else {
2098 /*
2099 * We are in between screen release and acquisition, and
2100 * reached here via scgetc() or scrn_timer() which has
2101 * interrupted exchange_scr(). Don't do anything stupid.
2102 */
2103 DPRINTF(5, ("waiting nothing, "));
2104 }
2105 } else {
2106 /*
2107 * The controlling process is alive, but not responding...
2108 * It is either buggy or it may be just taking time.
2109 * The following code is a gross kludge to cope with this
2110 * problem for which there is no clean solution. XXX
2111 */
2112 if (cur_scp->status & SWITCH_WAIT_REL) {
2113 switch (sc->switch_in_progress++) {
2114 case 1:
2115 break;
2116 case 2:
2117 DPRINTF(5, ("sending relsig again, "));
2118 signal_vt_rel(cur_scp);
2119 break;
2120 case 3:
2121 break;
2122 case 4:
2123 default:
2124 /*
2125 * Act as if the controlling program returned
2126 * VT_FALSE.
6bc31f17
MD
2127 *
2128 * note: must be called from a critical section because
2129 * finish_vt_rel will call do_switch_scr which releases it
2130 * temporarily.
984263bc
MD
2131 */
2132 DPRINTF(5, ("force reset WAIT_REL, "));
6bc31f17
MD
2133 finish_vt_rel(cur_scp, FALSE);
2134 crit_exit();
984263bc
MD
2135 DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2136 return EINVAL;
2137 }
2138 } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2139 switch (sc->switch_in_progress++) {
2140 case 1:
2141 break;
2142 case 2:
2143 DPRINTF(5, ("sending acqsig again, "));
2144 signal_vt_acq(cur_scp);
2145 break;
2146 case 3:
2147 break;
2148 case 4:
2149 default:
2150 /* clear the flag and finish the previous switch */
2151 DPRINTF(5, ("force reset WAIT_ACQ, "));
2152 finish_vt_acq(cur_scp);
2153 break;
2154 }
2155 }
2156 }
2157 }
2158
2159 /*
2160 * Return error if an invalid argument is given, or vty switch
2161 * is still in progress.
2162 */
2163 if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2164 || sc->switch_in_progress) {
6bc31f17 2165 crit_exit();
984263bc
MD
2166 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2167 DPRINTF(5, ("error 1\n"));
2168 return EINVAL;
2169 }
2170
2171 /*
2172 * Don't allow switching away from the graphics mode vty
2173 * if the switch mode is VT_AUTO, unless the next vty is the same
2174 * as the current or the current vty has been closed (but showing).
2175 */
2176 tp = VIRTUAL_TTY(sc, cur_scp->index);
2177 if ((cur_scp->index != next_scr)
2178 && ISTTYOPEN(tp)
2179 && (cur_scp->smode.mode == VT_AUTO)
2180 && ISGRAPHSC(cur_scp)) {
6bc31f17 2181 crit_exit();
984263bc
MD
2182 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2183 DPRINTF(5, ("error, graphics mode\n"));
2184 return EINVAL;
2185 }
2186
2187 /*
2188 * Is the wanted vty open? Don't allow switching to a closed vty.
2189 * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2190 * Note that we always allow the user to switch to the kernel
2191 * console even if it is closed.
2192 */
2193 if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2194 tp = VIRTUAL_TTY(sc, next_scr);
2195 if (!ISTTYOPEN(tp)) {
6bc31f17 2196 crit_exit();
984263bc
MD
2197 sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2198 DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2199 return EINVAL;
2200 }
2201 if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
6bc31f17 2202 crit_exit();
984263bc
MD
2203 DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2204 return EINVAL;
2205 }
2206 }
2207
2208 /* this is the start of vty switching process... */
2209 ++sc->switch_in_progress;
2210 sc->delayed_next_scr = 0;
2211 sc->old_scp = cur_scp;
2212 sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2213 if (sc->new_scp == sc->old_scp) {
2214 sc->switch_in_progress = 0;
2215 wakeup((caddr_t)&sc->new_scp->smode);
6bc31f17 2216 crit_exit();
984263bc
MD
2217 DPRINTF(5, ("switch done (new == old)\n"));
2218 return 0;
2219 }
2220
2221 /* has controlling process died? */
2222 vt_proc_alive(sc->old_scp);
2223 vt_proc_alive(sc->new_scp);
2224
2225 /* wait for the controlling process to release the screen, if necessary */
2226 if (signal_vt_rel(sc->old_scp)) {
6bc31f17 2227 crit_exit();
984263bc
MD
2228 return 0;
2229 }
2230
2231 /* go set up the new vty screen */
6bc31f17 2232 crit_exit();
984263bc 2233 exchange_scr(sc);
6bc31f17 2234 crit_enter();
984263bc
MD
2235
2236 /* wake up processes waiting for this vty */
2237 wakeup((caddr_t)&sc->cur_scp->smode);
2238
2239 /* wait for the controlling process to acknowledge, if necessary */
2240 if (signal_vt_acq(sc->cur_scp)) {
6bc31f17 2241 crit_exit();
984263bc
MD
2242 return 0;
2243 }
2244
2245 sc->switch_in_progress = 0;
2246 if (sc->unit == sc_console_unit)
2247 cons_unavail = FALSE;
6bc31f17 2248 crit_exit();
984263bc
MD
2249 DPRINTF(5, ("switch done\n"));
2250
2251 return 0;
2252}
2253
6bc31f17
MD
2254/*
2255 * NOTE: must be called from a critical section because do_switch_scr
2256 * will release it temporarily.
2257 */
2258static void
2259do_switch_scr(sc_softc_t *sc)
984263bc
MD
2260{
2261 vt_proc_alive(sc->new_scp);
2262
6bc31f17 2263 crit_exit();
984263bc 2264 exchange_scr(sc);
6bc31f17 2265 crit_enter();
984263bc
MD
2266 /* sc->cur_scp == sc->new_scp */
2267 wakeup((caddr_t)&sc->cur_scp->smode);
2268
2269 /* wait for the controlling process to acknowledge, if necessary */
2270 if (!signal_vt_acq(sc->cur_scp)) {
2271 sc->switch_in_progress = 0;
2272 if (sc->unit == sc_console_unit)
2273 cons_unavail = FALSE;
2274 }
984263bc
MD
2275}
2276
2277static int
2278vt_proc_alive(scr_stat *scp)
2279{
2280 if (scp->proc) {
2281 if (scp->proc == pfind(scp->pid))
2282 return TRUE;
2283 scp->proc = NULL;
2284 scp->smode.mode = VT_AUTO;
2285 DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2286 }
2287 return FALSE;
2288}
2289
2290static int
2291signal_vt_rel(scr_stat *scp)
2292{
2293 if (scp->smode.mode != VT_PROCESS)
2294 return FALSE;
2295 scp->status |= SWITCH_WAIT_REL;
84204577 2296 ksignal(scp->proc, scp->smode.relsig);
984263bc
MD
2297 DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2298 return TRUE;
2299}
2300
2301static int
2302signal_vt_acq(scr_stat *scp)
2303{
2304 if (scp->smode.mode != VT_PROCESS)
2305 return FALSE;
2306 if (scp->sc->unit == sc_console_unit)
2307 cons_unavail = TRUE;
2308 scp->status |= SWITCH_WAIT_ACQ;
84204577 2309 ksignal(scp->proc, scp->smode.acqsig);
984263bc
MD
2310 DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2311 return TRUE;
2312}
2313
6bc31f17
MD
2314/*
2315 * NOTE: must be called from a critical section because do_switch_scr
2316 * will release it temporarily.
2317 */
984263bc 2318static int
6bc31f17 2319finish_vt_rel(scr_stat *scp, int release)
984263bc
MD
2320{
2321 if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2322 scp->status &= ~SWITCH_WAIT_REL;
2323 if (release)
6bc31f17 2324 do_switch_scr(scp->sc);
984263bc
MD
2325 else
2326 scp->sc->switch_in_progress = 0;
2327 return 0;
2328 }
2329 return EINVAL;
2330}
2331
2332static int
2333finish_vt_acq(scr_stat *scp)
2334{
2335 if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2336 scp->status &= ~SWITCH_WAIT_ACQ;
2337 scp->sc->switch_in_progress = 0;
2338 return 0;
2339 }
2340 return EINVAL;
2341}
2342
2343static void
2344exchange_scr(sc_softc_t *sc)
2345{
2346 scr_stat *scp;
2347
2348 /* save the current state of video and keyboard */
2349 sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2350 if (!ISGRAPHSC(sc->old_scp))
2351 sc_remove_cursor_image(sc->old_scp);
2352 if (sc->old_scp->kbd_mode == K_XLATE)
2353 save_kbd_state(sc->old_scp);
2354
2355 /* set up the video for the new screen */
2356 scp = sc->cur_scp = sc->new_scp;
2357 if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2358 set_mode(scp);
2359 else
2360 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2361 (void *)sc->adp->va_window, FALSE);
2362 scp->status |= MOUSE_HIDDEN;
2363 sc_move_cursor(scp, scp->xpos, scp->ypos);
2364 if (!ISGRAPHSC(scp))
2365 sc_set_cursor_image(scp);
2366#ifndef SC_NO_PALETTE_LOADING
2367 if (ISGRAPHSC(sc->old_scp))
2368 load_palette(sc->adp, sc->palette);
2369#endif
2370 sc_set_border(scp, scp->border);
2371
2372 /* set up the keyboard for the new screen */
2373 if (sc->old_scp->kbd_mode != scp->kbd_mode)
2374 kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2375 update_kbd_state(scp, scp->status, LOCK_MASK);
2376
2377 mark_all(scp);
2378}
2379
2380void
2381sc_puts(scr_stat *scp, u_char *buf, int len)
2382{
2383#if NSPLASH > 0
2384 /* make screensaver happy */
2385 if (!sticky_splash && scp == scp->sc->cur_scp)
2386 run_scrn_saver = FALSE;
2387#endif
2388
2389 if (scp->tsw)
2390 (*scp->tsw->te_puts)(scp, buf, len);
2391
2392 if (scp->sc->delayed_next_scr)
2393 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2394}
2395
2396void
2397sc_draw_cursor_image(scr_stat *scp)
2398{
2399 /* assert(scp == scp->sc->cur_scp); */
2400 ++scp->sc->videoio_in_progress;
2401 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2402 scp->sc->flags & SC_BLINK_CURSOR, TRUE,
2403 sc_inside_cutmark(scp, scp->cursor_pos));
2404 scp->cursor_oldpos = scp->cursor_pos;
2405 --scp->sc->videoio_in_progress;
2406}
2407
2408void
2409sc_remove_cursor_image(scr_stat *scp)
2410{
2411 /* assert(scp == scp->sc->cur_scp); */
2412 ++scp->sc->videoio_in_progress;
2413 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2414 scp->sc->flags & SC_BLINK_CURSOR, FALSE,
2415 sc_inside_cutmark(scp, scp->cursor_oldpos));
2416 --scp->sc->videoio_in_progress;
2417}
2418
2419static void
2420update_cursor_image(scr_stat *scp)
2421{
2422 int blink;
2423
2424 if (scp->sc->flags & SC_CHAR_CURSOR) {
2425 scp->cursor_base = imax(0, scp->sc->cursor_base);
2426 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2427 } else {
2428 scp->cursor_base = 0;
2429 scp->cursor_height = scp->font_size;
2430 }
2431 blink = scp->sc->flags & SC_BLINK_CURSOR;
2432
2433 /* assert(scp == scp->sc->cur_scp); */
2434 ++scp->sc->videoio_in_progress;
2435 (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE,
2436 sc_inside_cutmark(scp, scp->cursor_pos));
2437 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
2438 (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE,
2439 sc_inside_cutmark(scp, scp->cursor_pos));
2440 --scp->sc->videoio_in_progress;
2441}
2442
2443void
2444sc_set_cursor_image(scr_stat *scp)
2445{
2446 if (scp->sc->flags & SC_CHAR_CURSOR) {
2447 scp->cursor_base = imax(0, scp->sc->cursor_base);
2448 scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2449 } else {
2450 scp->cursor_base = 0;
2451 scp->cursor_height = scp->font_size;
2452 }
2453
2454 /* assert(scp == scp->sc->cur_scp); */
2455 ++scp->sc->videoio_in_progress;
2456 (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
2457 scp->sc->flags & SC_BLINK_CURSOR);
2458 --scp->sc->videoio_in_progress;
2459}
2460
2461static void
2462scinit(int unit, int flags)
2463{
2464 /*
2465 * When syscons is being initialized as the kernel console, malloc()
2466 * is not yet functional, because various kernel structures has not been
2467 * fully initialized yet. Therefore, we need to declare the following
2468 * static buffers for the console. This is less than ideal,
2469 * but is necessry evil for the time being. XXX
2470 */
2471 static scr_stat main_console;
984263bc
MD
2472 static u_short sc_buffer[ROW*COL]; /* XXX */
2473#ifndef SC_NO_FONT_LOADING
2474 static u_char font_8[256*8];
2475 static u_char font_14[256*14];
2476 static u_char font_16[256*16];
2477#endif
2478
2479 sc_softc_t *sc;
2480 scr_stat *scp;
2481 video_adapter_t *adp;
2482 int col;
2483 int row;
2484 int i;
2485
2486 /* one time initialization */
2487 if (init_done == COLD)
2488 sc_get_bios_values(&bios_value);
2489 init_done = WARM;
2490
2491 /*
2492 * Allocate resources. Even if we are being called for the second
2493 * time, we must allocate them again, because they might have
2494 * disappeared...
2495 */
2496 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2497 adp = NULL;
2498 if (sc->adapter >= 0) {
2499 vid_release(sc->adp, (void *)&sc->adapter);
2500 adp = sc->adp;
2501 sc->adp = NULL;
2502 }
2503 if (sc->keyboard >= 0) {
2504 DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2505 i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2506 DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2507 if (sc->kbd != NULL) {
2508 DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2509 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2510 }
2511 sc->kbd = NULL;
2512 }
2513 sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2514 sc->adp = vid_get_adapter(sc->adapter);
2515 /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2516 sc->keyboard = kbd_allocate("*", unit, (void *)&sc->keyboard,
2517 sckbdevent, sc);
2518 DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2519 sc->kbd = kbd_get_keyboard(sc->keyboard);
2520 if (sc->kbd != NULL) {
2521 DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2522 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2523 }
2524
2525 if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2526
2527 sc->initial_mode = sc->adp->va_initial_mode;
2528
2529#ifndef SC_NO_FONT_LOADING
2530 if (flags & SC_KERNEL_CONSOLE) {
2531 sc->font_8 = font_8;
2532 sc->font_14 = font_14;
2533 sc->font_16 = font_16;
2534 } else if (sc->font_8 == NULL) {
2535 /* assert(sc_malloc) */
efda3bd0
MD
2536 sc->font_8 = kmalloc(sizeof(font_8), M_SYSCONS, M_WAITOK);
2537 sc->font_14 = kmalloc(sizeof(font_14), M_SYSCONS, M_WAITOK);
2538 sc->font_16 = kmalloc(sizeof(font_16), M_SYSCONS, M_WAITOK);
984263bc
MD
2539 }
2540#endif
2541
2542 /* extract the hardware cursor location and hide the cursor for now */
2543 (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2544 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2545
2546 /* set up the first console */
2547 sc->first_vty = unit*MAXCONS;
2548 sc->vtys = MAXCONS; /* XXX: should be configurable */
2549 if (flags & SC_KERNEL_CONSOLE) {
984263bc 2550 scp = &main_console;
ce81f184 2551 sc->console_scp = scp;
984263bc
MD
2552 init_scp(sc, sc->first_vty, scp);
2553 sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2554 (void *)sc_buffer, FALSE);
2555 if (sc_init_emulator(scp, SC_DFLT_TERM))
2556 sc_init_emulator(scp, "*");
2557 (*scp->tsw->te_default_attr)(scp,
2558 kernel_default.std_color,
2559 kernel_default.rev_color);
2560 } else {
2561 /* assert(sc_malloc) */
b13267a5 2562 sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK | M_ZERO);
cd29885a 2563
fef8985e 2564 sc->dev[0] = make_dev(&sc_ops, unit*MAXCONS, UID_ROOT,
e4c9c0c8 2565 GID_WHEEL, 0600, "ttyv%r", unit*MAXCONS);
cd29885a 2566
984263bc
MD
2567 sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
2568 scp = alloc_scp(sc, sc->first_vty);
ce81f184 2569 sc->dev[0]->si_drv1 = scp;
984263bc 2570 }
984263bc
MD
2571 sc->cur_scp = scp;
2572
2573 /* copy screen to temporary buffer */
2574 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2575 (void *)scp->sc->adp->va_window, FALSE);
2576 if (ISTEXTSC(scp))
2577 sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2578
2579 /* move cursors to the initial positions */
2580 if (col >= scp->xsize)
2581 col = 0;
2582 if (row >= scp->ysize)
2583 row = scp->ysize - 1;
2584 scp->xpos = col;
2585 scp->ypos = row;
2586 scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2587 if (bios_value.cursor_end < scp->font_size)
2588 sc->cursor_base = scp->font_size - bios_value.cursor_end - 1;
2589 else
2590 sc->cursor_base = 0;
2591 i = bios_value.cursor_end - bios_value.cursor_start + 1;
2592 sc->cursor_height = imin(i, scp->font_size);
2593#ifndef SC_NO_SYSMOUSE
2594 sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2595#endif
2596 if (!ISGRAPHSC(scp)) {
2597 sc_set_cursor_image(scp);
2598 sc_draw_cursor_image(scp);
2599 }
2600
2601 /* save font and palette */
2602#ifndef SC_NO_FONT_LOADING
2603 sc->fonts_loaded = 0;
2604 if (ISFONTAVAIL(sc->adp->va_flags)) {
2605#ifdef SC_DFLT_FONT
2606 bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2607 bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2608 bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2609 sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2610 if (scp->font_size < 14) {
2611 sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
2612 } else if (scp->font_size >= 16) {
2613 sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
2614 } else {
2615 sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
2616 }
2617#else /* !SC_DFLT_FONT */
2618 if (scp->font_size < 14) {
2619 sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
2620 sc->fonts_loaded = FONT_8;
2621 } else if (scp->font_size >= 16) {
2622 sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
2623 sc->fonts_loaded = FONT_16;
2624 } else {
2625 sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
2626 sc->fonts_loaded = FONT_14;
2627 }
2628#endif /* SC_DFLT_FONT */
2629 /* FONT KLUDGE: always use the font page #0. XXX */
2630 sc_show_font(scp, 0);
2631 }
2632#endif /* !SC_NO_FONT_LOADING */
2633
2634#ifndef SC_NO_PALETTE_LOADING
2635 save_palette(sc->adp, sc->palette);
2636#endif
2637
2638#if NSPLASH > 0
2639 if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
2640 /* we are ready to put up the splash image! */
2641 splash_init(sc->adp, scsplash_callback, sc);
2642 sc->flags |= SC_SPLASH_SCRN;
2643 }
2644#endif /* NSPLASH */
2645 }
2646
2647 /* the rest is not necessary, if we have done it once */
2648 if (sc->flags & SC_INIT_DONE)
2649 return;
2650
2651 /* initialize mapscrn arrays to a one to one map */
2652 for (i = 0; i < sizeof(sc->scr_map); i++)
2653 sc->scr_map[i] = sc->scr_rmap[i] = i;
2654
2655 sc->flags |= SC_INIT_DONE;
2656}
2657
984263bc
MD
2658static void
2659scterm(int unit, int flags)
2660{
2661 sc_softc_t *sc;
2662 scr_stat *scp;
2663
2664 sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2665 if (sc == NULL)
2666 return; /* shouldn't happen */
2667
2668#if NSPLASH > 0
2669 /* this console is no longer available for the splash screen */
2670 if (sc->flags & SC_SPLASH_SCRN) {
2671 splash_term(sc->adp);
2672 sc->flags &= ~SC_SPLASH_SCRN;
2673 }
2674#endif /* NSPLASH */
2675
2676#if 0 /* XXX */
2677 /* move the hardware cursor to the upper-left corner */
2678 (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
2679#endif
2680
2681 /* release the keyboard and the video card */
2682 if (sc->keyboard >= 0)
2683 kbd_release(sc->kbd, &sc->keyboard);
2684 if (sc->adapter >= 0)
2685 vid_release(sc->adp, &sc->adapter);
2686
ce81f184
MD
2687 /*
2688 * Stop the terminal emulator, if any. If operating on the
2689 * kernel console sc->dev may not be setup yet.
2690 */
2691 if (flags & SC_KERNEL_CONSOLE)
2692 scp = sc->console_scp;
2693 else
2694 scp = SC_STAT(sc->dev[0]);
984263bc
MD
2695 if (scp->tsw)
2696 (*scp->tsw->te_term)(scp, &scp->ts);
2697 if (scp->ts != NULL)
efda3bd0 2698 kfree(scp->ts, M_SYSCONS);
984263bc
MD
2699
2700 /* clear the structure */
2701 if (!(flags & SC_KERNEL_CONSOLE)) {
2702 /* XXX: We need delete_dev() for this */
efda3bd0 2703 kfree(sc->dev, M_SYSCONS);
984263bc
MD
2704#if 0
2705 /* XXX: We need a ttyunregister for this */
efda3bd0 2706 kfree(sc->tty, M_SYSCONS);
984263bc
MD
2707#endif
2708#ifndef SC_NO_FONT_LOADING
efda3bd0
MD
2709 kfree(sc->font_8, M_SYSCONS);
2710 kfree(sc->font_14, M_SYSCONS);
2711 kfree(sc->font_16, M_SYSCONS);
984263bc
MD
2712#endif
2713 /* XXX vtb, history */
2714 }
2715 bzero(sc, sizeof(*sc));
2716 sc->keyboard = -1;
2717 sc->adapter = -1;
2718}
984263bc
MD
2719
2720static void
2721scshutdown(void *arg, int howto)
2722{
2723 /* assert(sc_console != NULL) */
2724
2725 sc_touch_scrn_saver();
2726 if (!cold && sc_console
2727 && sc_console->sc->cur_scp->smode.mode == VT_AUTO
2728 && sc_console->smode.mode == VT_AUTO)
2729 sc_switch_scr(sc_console->sc, sc_console->index);
2730 shutdown_in_progress = TRUE;
2731}
2732
2733int
2734sc_clean_up(scr_stat *scp)
2735{
2736#if NSPLASH > 0
2737 int error;
2738#endif /* NSPLASH */
2739
2740 if (scp->sc->flags & SC_SCRN_BLANKED) {
2741 sc_touch_scrn_saver();
2742#if NSPLASH > 0
2743 if ((error = wait_scrn_saver_stop(scp->sc)))
2744 return error;
2745#endif /* NSPLASH */
2746 }
2747 scp->status |= MOUSE_HIDDEN;
2748 sc_remove_mouse_image(scp);
2749 sc_remove_cutmarking(scp);
2750 return 0;
2751}
2752
2753void
2754sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
2755{
2756 sc_vtb_t new;
2757 sc_vtb_t old;
2758
2759 old = scp->vtb;
2760 sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
2761 if (!discard && (old.vtb_flags & VTB_VALID)) {
2762 /* retain the current cursor position and buffer contants */
2763 scp->cursor_oldpos = scp->cursor_pos;
2764 /*
2765 * This works only if the old buffer has the same size as or larger
2766 * than the new one. XXX
2767 */
2768 sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
2769 scp->vtb = new;
2770 } else {
2771 scp->vtb = new;
2772 sc_vtb_destroy(&old);
2773 }
2774
2775#ifndef SC_NO_SYSMOUSE
2776 /* move the mouse cursor at the center of the screen */
2777 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
2778#endif
2779}
2780
dc98d15f
SW
2781static scr_stat *
2782alloc_scp(sc_softc_t *sc, int vty)
984263bc
MD
2783{
2784 scr_stat *scp;
2785
2786 /* assert(sc_malloc) */
2787
efda3bd0 2788 scp = kmalloc(sizeof(scr_stat), M_SYSCONS, M_WAITOK);
984263bc
MD
2789 init_scp(sc, vty, scp);
2790
2791 sc_alloc_scr_buffer(scp, TRUE, TRUE);
2792 if (sc_init_emulator(scp, SC_DFLT_TERM))
2793 sc_init_emulator(scp, "*");
2794
2795#ifndef SC_NO_CUTPASTE
2796 sc_alloc_cut_buffer(scp, TRUE);
2797#endif
2798
2799#ifndef SC_NO_HISTORY
2800 sc_alloc_history_buffer(scp, 0, 0, TRUE);
2801#endif
2802
2803 return scp;
2804}
2805
2806static void
2807init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
2808{
2809 video_info_t info;
2810
2811 bzero(scp, sizeof(*scp));
2812
2813 scp->index = vty;
2814 scp->sc = sc;
2815 scp->status = 0;
2816 scp->mode = sc->initial_mode;
356bb9cd 2817 callout_init(&scp->blink_screen_ch);
984263bc
MD
2818 (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
2819 if (info.vi_flags & V_INFO_GRAPHICS) {
2820 scp->status |= GRAPHICS_MODE;
2821 scp->xpixel = info.vi_width;
2822 scp->ypixel = info.vi_height;
2823 scp->xsize = info.vi_width/8;
2824 scp->ysize = info.vi_height/info.vi_cheight;
2825 scp->font_size = 0;
2826 scp->font = NULL;
2827 } else {
2828 scp->xsize = info.vi_width;
2829 scp->ysize = info.vi_height;
2830 scp->xpixel = scp->xsize*8;
2831 scp->ypixel = scp->ysize*info.vi_cheight;
2832 if (info.vi_cheight < 14) {
2833 scp->font_size = 8;
2834#ifndef SC_NO_FONT_LOADING
2835 scp->font = sc->font_8;
2836#else
2837 scp->font = NULL;
2838#endif
2839 } else if (info.vi_cheight >= 16) {
2840 scp->font_size = 16;
2841#ifndef SC_NO_FONT_LOADING
2842 scp->font = sc->font_16;
2843#else
2844 scp->font = NULL;
2845#endif
2846 } else {
2847 scp->font_size = 14;
2848#ifndef SC_NO_FONT_LOADING
2849 scp->font = sc->font_14;
2850#else
2851 scp->font = NULL;
2852#endif
2853 }
2854 }
2855 sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
2856 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
2857 scp->xoff = scp->yoff = 0;
2858 scp->xpos = scp->ypos = 0;
2859 scp->start = scp->xsize * scp->ysize - 1;
2860 scp->end = 0;
2861 scp->tsw = NULL;
2862 scp->ts = NULL;
2863 scp->rndr = NULL;
2864 scp->border = BG_BLACK;
2865 scp->cursor_base = sc->cursor_base;
2866 scp->cursor_height = imin(sc->cursor_height, scp->font_size);
2867 scp->mouse_cut_start = scp->xsize*scp->ysize;
2868 scp->mouse_cut_end = -1;
2869 scp->mouse_signal = 0;
2870 scp->mouse_pid = 0;
2871 scp->mouse_proc = NULL;
2872 scp->kbd_mode = K_XLATE;
2873 scp->bell_pitch = bios_value.bell_pitch;
2874 scp->bell_duration = BELL_DURATION;
2875 scp->status |= (bios_value.shift_state & NLKED);
2876 scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
2877 scp->pid = 0;
2878 scp->proc = NULL;
2879 scp->smode.mode = VT_AUTO;
2880 scp->history = NULL;
2881 scp->history_pos = 0;
2882 scp->history_size = 0;
2883}
2884
2885int
2886sc_init_emulator(scr_stat *scp, char *name)
2887{
2888 sc_term_sw_t *sw;
2889 sc_rndr_sw_t *rndr;
2890 void *p;
2891 int error;
2892
2893 if (name == NULL) /* if no name is given, use the current emulator */
2894 sw = scp->tsw;
2895 else /* ...otherwise find the named emulator */
2896 sw = sc_term_match(name);
2897 if (sw == NULL)
2898 return EINVAL;
2899
2900 rndr = NULL;
2901 if (strcmp(sw->te_renderer, "*") != 0) {
2902 rndr = sc_render_match(scp, sw->te_renderer,
2903 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2904 }
2905 if (rndr == NULL) {
2906 rndr = sc_render_match(scp, scp->sc->adp->va_name,
2907 scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2908 if (rndr == NULL)
2909 return ENODEV;
2910 }
2911
2912 if (sw == scp->tsw) {
2913 error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
2914 scp->rndr = rndr;
2a85d763 2915 scp->rndr->init(scp);
984263bc
MD
2916 sc_clear_screen(scp);
2917 /* assert(error == 0); */
2918 return error;
2919 }
2920
2921 if (sc_malloc && (sw->te_size > 0))
efda3bd0 2922 p = kmalloc(sw->te_size, M_SYSCONS, M_NOWAIT);
984263bc
MD
2923 else
2924 p = NULL;
2925 error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
2926 if (error)
2927 return error;
2928
2929 if (scp->tsw)
2930 (*scp->tsw->te_term)(scp, &scp->ts);
2931 if (scp->ts != NULL)
efda3bd0 2932 kfree(scp->ts, M_SYSCONS);
984263bc
MD
2933 scp->tsw = sw;
2934 scp->ts = p;
2935 scp->rndr = rndr;
2a85d763 2936 scp->rndr->init(scp);
984263bc
MD
2937
2938 /* XXX */
2939 (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
2940 sc_clear_screen(scp);
2941
2942 return 0;
2943}
2944
2945/*
2946 * scgetc(flags) - get character from keyboard.
2947 * If flags & SCGETC_CN, then avoid harmful side effects.
2948 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
2949 * return NOKEY if there is nothing there.
2950 */
2951static u_int
2952scgetc(sc_softc_t *sc, u_int flags)
2953{
2954 scr_stat *scp;
2955#ifndef SC_NO_HISTORY
2956 struct tty *tp;
2957#endif
2958 u_int c;
2959 int this_scr;
2960 int f;
2961 int i;
2962
2963 if (sc->kbd == NULL)
2964 return NOKEY;
2965
2966next_code:
2967#if 1
2968 /* I don't like this, but... XXX */
2969 if (flags & SCGETC_CN)
2970 sccnupdate(sc->cur_scp);
2971#endif
2972 scp = sc->cur_scp;
2973 /* first see if there is something in the keyboard port */
2974 for (;;) {
2975 c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
2976 if (c == ERRKEY) {
2977 if (!(flags & SCGETC_CN))
2978 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
2979 } else if (c == NOKEY)
2980 return c;
2981 else
2982 break;
2983 }
2984
2985 /* make screensaver happy */
2986 if (!(c & RELKEY))
2987 sc_touch_scrn_saver();
2988
984263bc
MD
2989 if (!(flags & SCGETC_CN))
2990 /* do the /dev/random device a favour */
2991 add_keyboard_randomness(c);
984263bc
MD
2992
2993 if (scp->kbd_mode != K_XLATE)
2994 return KEYCHAR(c);
2995
2996 /* if scroll-lock pressed allow history browsing */
2997 if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
2998
2999 scp->status &= ~CURSOR_ENABLED;
3000 sc_remove_cursor_image(scp);
3001
3002#ifndef SC_NO_HISTORY
3003 if (!(scp->status & BUFFER_SAVED)) {
3004 scp->status |= BUFFER_SAVED;
3005 sc_hist_save(scp);
3006 }
3007 switch (c) {
3008 /* FIXME: key codes */
3009 case SPCLKEY | FKEY | F(49): /* home key */
3010 sc_remove_cutmarking(scp);
3011 sc_hist_home(scp);
3012 goto next_code;
3013
3014 case SPCLKEY | FKEY | F(57): /* end key */
3015 sc_remove_cutmarking(scp);
3016 sc_hist_end(scp);
3017 goto next_code;
3018
3019 case SPCLKEY | FKEY | F(50): /* up arrow key */
3020 sc_remove_cutmarking(scp);
3021 if (sc_hist_up_line(scp))
3022 if (!(flags & SCGETC_CN))
3023 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3024 goto next_code;
3025
3026 case SPCLKEY | FKEY | F(58): /* down arrow key */
3027 sc_remove_cutmarking(scp);
3028 if (sc_hist_down_line(scp))
3029 if (!(flags & SCGETC_CN))
3030 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3031 goto next_code;
3032
3033 case SPCLKEY | FKEY | F(51): /* page up key */
3034 sc_remove_cutmarking(scp);
3035 for (i=0; i<scp->ysize; i++)
3036 if (sc_hist_up_line(scp)) {
3037 if (!(flags & SCGETC_CN))
3038 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3039 break;
3040 }
3041 goto next_code;
3042
3043 case SPCLKEY | FKEY | F(59): /* page down key */
3044 sc_remove_cutmarking(scp);
3045 for (i=0; i<scp->ysize; i++)
3046 if (sc_hist_down_line(scp)) {
3047 if (!(flags & SCGETC_CN))
3048 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3049 break;
3050 }
3051 goto next_code;
3052 }
3053#endif /* SC_NO_HISTORY */
3054 }
3055
3056 /*
3057 * Process and consume special keys here. Return a plain char code
3058 * or a char code with the META flag or a function key code.
3059 */
3060 if (c & RELKEY) {
3061 /* key released */
3062 /* goto next_code */
3063 } else {
3064 /* key pressed */
3065 if (c & SPCLKEY) {
3066 c &= ~SPCLKEY;
3067 switch (KEYCHAR(c)) {
3068 /* LOCKING KEYS */
3069 case NLK: case CLK: case ALK:
3070 break;
3071 case SLK:
3072 kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3073 if (f & SLKED) {
3074 scp->status |= SLKED;
3075 } else {
3076 if (scp->status & SLKED) {
3077 scp->status &= ~SLKED;
3078#ifndef SC_NO_HISTORY
3079 if (scp->status & BUFFER_SAVED) {
3080 if (!sc_hist_restore(scp))
3081 sc_remove_cutmarking(scp);
3082 scp->status &= ~BUFFER_SAVED;
3083 scp->status |= CURSOR_ENABLED;
3084 sc_draw_cursor_image(scp);
3085 }
3086 tp = VIRTUAL_TTY(sc, scp->index);
3087 if (ISTTYOPEN(tp))
3088 scstart(tp);
3089#endif
3090 }
3091 }
3092 break;
3093
3094 /* NON-LOCKING KEYS */
3095 case NOP:
3096 case LSH: case RSH: case LCTR: case RCTR:
3097 case LALT: case RALT: case ASH: case META:
3098 break;
3099
3100 case BTAB:
3101 if (!(sc->flags & SC_SCRN_BLANKED))
3102 return c;
3103 break;
3104
3105 case SPSC:
3106#if NSPLASH > 0
3107 /* force activatation/deactivation of the screen saver */
3108 if (!(sc->flags & SC_SCRN_BLANKED)) {
3109 run_scrn_saver = TRUE;
3110 sc->scrn_time_stamp -= scrn_blank_time;
3111 }
3112 if (cold) {
3113 /*
3114 * While devices are being probed, the screen saver need
3115 * to be invoked explictly. XXX
3116 */
3117 if (sc->flags & SC_SCRN_BLANKED) {
3118 scsplash_stick(FALSE);
3119 stop_scrn_saver(sc, current_saver);
3120 } else {
3121 if (!ISGRAPHSC(scp)) {
3122 scsplash_stick(TRUE);
3123 (*current_saver)(sc, TRUE);
3124 }
3125 }
3126 }
3127#endif /* NSPLASH */
3128 break;
3129
3130 case RBT:
3131#ifndef SC_DISABLE_REBOOT
3132 shutdown_nice(0);
3133#endif
3134 break;
3135
3136 case HALT:
3137#ifndef SC_DISABLE_REBOOT
3138 shutdown_nice(RB_HALT);
3139#endif
3140 break;
3141
3142 case PDWN:
3143#ifndef SC_DISABLE_REBOOT
3144 shutdown_nice(RB_HALT|RB_POWEROFF);
3145#endif
3146 break;
3147
3148#if NAPM > 0
3149 case SUSP:
3150 apm_suspend(PMST_SUSPEND);
3151 break;
3152 case STBY:
3153 apm_suspend(PMST_STANDBY);
3154 break;
3155#else
3156 case SUSP:
3157 case STBY:
3158 break;
3159#endif
3160
3161 case DBG:
3162#ifndef SC_DISABLE_DDBKEY
3163#ifdef DDB
3164 Debugger("manual escape to debugger");
3165#else
e3869ec7 3166 kprintf("No debugger in kernel\n");
984263bc
MD
3167#endif
3168#else /* SC_DISABLE_DDBKEY */
3169 /* do nothing */
3170#endif /* SC_DISABLE_DDBKEY */
3171 break;
3172
3173 case PNC:
3174 if (enable_panic_key)
3175 panic("Forced by the panic key");
3176 break;
3177
3178 case NEXT:
3179 this_scr = scp->index;
3180 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3181 sc->first_vty + i != this_scr;
3182 i = (i + 1)%sc->vtys) {
3183 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3184 if (ISTTYOPEN(tp)) {
3185 sc_switch_scr(scp->sc, sc->first_vty + i);
3186 break;
3187 }
3188 }
3189 break;
3190
3191 case PREV:
3192 this_scr = scp->index;
3193 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3194 sc->first_vty + i != this_scr;
3195 i = (i + sc->vtys - 1)%sc->vtys) {
3196 struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3197 if (ISTTYOPEN(tp)) {
3198 sc_switch_scr(scp->sc, sc->first_vty + i);
3199 break;
3200 }
3201 }
3202 break;
3203
3204 default:
3205 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3206 sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3207 break;
3208 }
3209 /* assert(c & FKEY) */
3210 if (!(sc->flags & SC_SCRN_BLANKED))
3211 return c;
3212 break;
3213 }
3214 /* goto next_code */
3215 } else {
3216 /* regular keys (maybe MKEY is set) */
3217 if (!(sc->flags & SC_SCRN_BLANKED))
3218 return c;
3219 }
3220 }
3221
3222 goto next_code;
3223}
3224
3225int
fef8985e 3226scmmap(struct dev_mmap_args *ap)
984263bc
MD
3227{
3228 scr_stat *scp;
3229
fef8985e 3230 scp = SC_STAT(ap->a_head.a_dev);
984263bc 3231 if (scp != scp->sc->cur_scp)
fef8985e
MD
3232 return EINVAL;
3233 ap->a_result = (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, ap->a_offset,
3234 ap->a_nprot);
3235 return(0);
984263bc
MD
3236}
3237
3238static int
3239save_kbd_state(scr_stat *scp)
3240{
3241 int state;
3242 int error;
3243
3244 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3245 if (error == ENOIOCTL)
3246 error = ENODEV;
3247 if (error == 0) {
3248 scp->status &= ~LOCK_MASK;
3249 scp->status |= state;
3250 }
3251 return error;
3252}
3253
3254static int
3255update_kbd_state(scr_stat *scp, int new_bits, int mask)
3256{
3257 int state;
3258 int error;
3259
3260 if (mask != LOCK_MASK) {
3261 error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3262 if (error == ENOIOCTL)
3263 error = ENODEV;
3264 if (error)
3265 return error;
3266 state &= ~mask;
3267 state |= new_bits & mask;
3268 } else {
3269 state = new_bits & LOCK_MASK;
3270 }
3271 error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3272 if (error == ENOIOCTL)
3273 error = ENODEV;
3274 return error;
3275}
3276
3277static int
3278update_kbd_leds(scr_stat *scp, int which)
3279{
3280 int error;
3281
3282 which &= LOCK_MASK;
3283 error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3284 if (error == ENOIOCTL)
3285 error = ENODEV;
3286 return error;
3287}
3288
3289int
3290set_mode(scr_stat *scp)
3291{
3292 video_info_t info;
3293
3294 /* reject unsupported mode */
3295 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
3296 return 1;
3297
3298 /* if this vty is not currently showing, do nothing */
3299 if (scp != scp->sc->cur_scp)
3300 return 0;
3301
3302 /* setup video hardware for the given mode */
3303 (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
2a85d763 3304 scp->rndr->init(scp);
984263bc
MD
3305 sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3306 (void *)scp->sc->adp->va_window, FALSE);
3307
3308#ifndef SC_NO_FONT_LOADING
3309 /* load appropriate font */
3310 if (!(scp->status & GRAPHICS_MODE)) {
3311 if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3312 if (scp->font_size < 14) {
3313 if (scp->sc->fonts_loaded & FONT_8)
3314 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
3315 } else if (scp->font_size >= 16) {
3316 if (scp->sc->fonts_loaded & FONT_16)
3317 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
3318 } else {
3319 if (scp->sc->fonts_loaded & FONT_14)
3320 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
3321 }
3322 /*
3323 * FONT KLUDGE:
3324 * This is an interim kludge to display correct font.
3325 * Always use the font page #0 on the video plane 2.
3326 * Somehow we cannot show the font in other font pages on
3327 * some video cards... XXX
3328 */
3329 sc_show_font(scp, 0);
3330 }
3331 mark_all(scp);
3332 }
3333#endif /* !SC_NO_FONT_LOADING */
3334
3335 sc_set_border(scp, scp->border);
3336 sc_set_cursor_image(scp);
3337
3338 return 0;
3339}
3340
3341void
a5eb78ad
SW
3342refresh_ega_palette(scr_stat *scp)
3343{
3344 uint32_t r, g, b;
3345 int reg;
3346 int rsize, gsize, bsize;
3347 int rfld, gfld, bfld;
3348 int i;
3349
3350 rsize = scp->sc->adp->va_info.vi_pixel_fsizes[0];
3351 gsize = scp->sc->adp->va_info.vi_pixel_fsizes[1];
3352 bsize = scp->sc->adp->va_info.vi_pixel_fsizes[2];
3353 rfld = scp->sc->adp->va_info.vi_pixel_fields[0];
3354 gfld = scp->sc->adp->va_info.vi_pixel_fields[1];
3355 bfld = scp->sc->adp->va_info.vi_pixel_fields[2];
3356
3357 for (i = 0; i < 16; i++) {
3358 reg = scp->sc->adp->va_palette_regs[i];
3359
3360 r = scp->sc->palette[reg * 3] >> (8 - rsize);
3361 g = scp->sc->palette[reg * 3 + 1] >> (8 - gsize);
3362 b = scp->sc->palette[reg * 3 + 2] >> (8 - bsize);
3363
3364 scp->ega_palette[i] = (r << rfld) + (g << gfld) + (b << bfld);
3365 }
3366}
3367
3368void
984263bc
MD
3369sc_set_border(scr_stat *scp, int color)
3370{
3371 ++scp->sc->videoio_in_progress;
3372 (*scp->rndr->draw_border)(scp, color);
3373 --scp->sc->videoio_in_progress;
3374}
3375
3376#ifndef SC_NO_FONT_LOADING
3377void
3378sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
3379 int base, int count)
3380{
3381 sc_softc_t *sc;
3382
3383 sc = scp->sc;
3384 sc->font_loading_in_progress = TRUE;
3385 (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
3386 sc->font_loading_in_progress = FALSE;
3387}
3388
3389void
3390sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
3391 int base, int count)
3392{
3393 sc_softc_t *sc;
3394
3395 sc = scp->sc;
3396 sc->font_loading_in_progress = TRUE;
3397 (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
3398 sc->font_loading_in_progress = FALSE;
3399}
3400
3401void
3402sc_show_font(scr_stat *scp, int page)
3403{
3404 (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3405}
3406#endif /* !SC_NO_FONT_LOADING */
3407
3408void
3409sc_paste(scr_stat *scp, u_char *p, int count)
3410{
3411 struct tty *tp;
3412 u_char *rmap;
3413
3414 if (scp->status & MOUSE_VISIBLE) {
3415 tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3416 if (!ISTTYOPEN(tp))
3417 return;
3418 rmap = scp->sc->scr_rmap;
3419 for (; count > 0; --count)
3420 (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3421 }
3422}
3423
3424void
3425sc_bell(scr_stat *scp, int pitch, int duration)
3426{
3427 if (cold || shutdown_in_progress)
3428 return;
3429
3430 if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3431 return;
3432
3433 if (scp->sc->flags & SC_VISUAL_BELL) {
3434 if (scp->sc->blink_in_progress)
3435 return;
3436 scp->sc->blink_in_progress = 3;
3437 if (scp != scp->sc->cur_scp)
3438 scp->sc->blink_in_progress += 2;
3439 blink_screen(scp->sc->cur_scp);
2123da6d 3440 } else if (duration != 0 && pitch != 0) {
984263bc
MD
3441 if (scp != scp->sc->cur_scp)
3442 pitch *= 2;
3443 sysbeep(pitch, duration);
3444 }
3445}
3446
3447static void
3448blink_screen(void *arg)
3449{
3450 scr_stat *scp = arg;
3451 struct tty *tp;
3452
3453 if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3454 scp->sc->blink_in_progress = 0;
3455 mark_all(scp);
3456 tp = VIRTUAL_TTY(scp->sc, scp->index);
3457 if (ISTTYOPEN(tp))
3458 scstart(tp);
3459 if (scp->sc->delayed_next_scr)
3460 sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3461 }
3462 else {
3463 (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3464 scp->sc->blink_in_progress & 1);
3465 scp->sc->blink_in_progress--;
356bb9cd 3466 callout_reset(&scp->blink_screen_ch, hz / 10, blink_screen, scp);
984263bc
MD
3467 }
3468}