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