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