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