syscons.c revision 172250
1135446Strhodes/*-
2153816Sdougb * Copyright (c) 1992-1998 S�ren Schmidt
3135446Strhodes * All rights reserved.
4135446Strhodes *
5135446Strhodes * This code is derived from software contributed to The DragonFly Project
6135446Strhodes * by Sascha Wildner <saw@online.de>
7135446Strhodes *
8135446Strhodes * Redistribution and use in source and binary forms, with or without
9135446Strhodes * modification, are permitted provided that the following conditions
10135446Strhodes * are met:
11135446Strhodes * 1. Redistributions of source code must retain the above copyright
12135446Strhodes *    notice, this list of conditions and the following disclaimer,
13135446Strhodes *    without modification, immediately at the beginning of the file.
14135446Strhodes * 2. Redistributions in binary form must reproduce the above copyright
15135446Strhodes *    notice, this list of conditions and the following disclaimer in the
16135446Strhodes *    documentation and/or other materials provided with the distribution.
17135446Strhodes * 3. The name of the author may not be used to endorse or promote products
18153816Sdougb *    derived from this software without specific prior written permission.
19135446Strhodes *
20135446Strhodes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21135446Strhodes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22135446Strhodes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23153816Sdougb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24135446Strhodes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25135446Strhodes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26135446Strhodes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27135446Strhodes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28135446Strhodes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29135446Strhodes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30135446Strhodes */
31135446Strhodes
32135446Strhodes#include <sys/cdefs.h>
33135446Strhodes__FBSDID("$FreeBSD: head/sys/dev/syscons/syscons.c 172250 2007-09-20 04:05:59Z simokawa $");
34135446Strhodes
35135446Strhodes#include "opt_compat.h"
36135446Strhodes#include "opt_syscons.h"
37135446Strhodes#include "opt_splash.h"
38135446Strhodes#include "opt_ddb.h"
39135446Strhodes
40135446Strhodes#include <sys/param.h>
41135446Strhodes#include <sys/systm.h>
42135446Strhodes#include <sys/conf.h>
43135446Strhodes#include <sys/cons.h>
44135446Strhodes#include <sys/consio.h>
45135446Strhodes#include <sys/kdb.h>
46135446Strhodes#include <sys/eventhandler.h>
47135446Strhodes#include <sys/fbio.h>
48135446Strhodes#include <sys/kbio.h>
49135446Strhodes#include <sys/kernel.h>
50135446Strhodes#include <sys/lock.h>
51135446Strhodes#include <sys/malloc.h>
52135446Strhodes#include <sys/mutex.h>
53135446Strhodes#include <sys/priv.h>
54135446Strhodes#include <sys/proc.h>
55135446Strhodes#include <sys/random.h>
56135446Strhodes#include <sys/reboot.h>
57135446Strhodes#include <sys/signalvar.h>
58135446Strhodes#include <sys/sysctl.h>
59135446Strhodes#include <sys/tty.h>
60135446Strhodes#include <sys/power.h>
61135446Strhodes
62135446Strhodes#include <machine/clock.h>
63135446Strhodes#if defined(__sparc64__) || defined(__powerpc__)
64135446Strhodes#include <machine/sc_machdep.h>
65135446Strhodes#else
66135446Strhodes#include <machine/pc/display.h>
67135446Strhodes#endif
68135446Strhodes#if defined( __i386__) || defined(__amd64__)
69135446Strhodes#include <machine/psl.h>
70135446Strhodes#include <machine/frame.h>
71135446Strhodes#endif
72135446Strhodes
73135446Strhodes#include <dev/kbd/kbdreg.h>
74135446Strhodes#include <dev/fb/fbreg.h>
75135446Strhodes#include <dev/fb/splashreg.h>
76135446Strhodes#include <dev/syscons/syscons.h>
77135446Strhodes
78135446Strhodes#define COLD 0
79135446Strhodes#define WARM 1
80135446Strhodes
81135446Strhodes#define DEFAULT_BLANKTIME	(5*60)		/* 5 minutes */
82135446Strhodes#define MAX_BLANKTIME		(7*24*60*60)	/* 7 days!? */
83135446Strhodes
84135446Strhodes#define KEYCODE_BS		0x0e		/* "<-- Backspace" key, XXX */
85135446Strhodes
86135446Strhodestypedef struct default_attr {
87135446Strhodes	int		std_color;		/* normal hardware color */
88135446Strhodes	int		rev_color;		/* reverse hardware color */
89135446Strhodes} default_attr;
90135446Strhodes
91135446Strhodesstatic default_attr user_default = {
92135446Strhodes    SC_NORM_ATTR,
93135446Strhodes    SC_NORM_REV_ATTR,
94135446Strhodes};
95135446Strhodes
96135446Strhodesstatic default_attr kernel_default = {
97135446Strhodes    SC_KERNEL_CONS_ATTR,
98135446Strhodes    SC_KERNEL_CONS_REV_ATTR,
99135446Strhodes};
100135446Strhodes
101135446Strhodesstatic	int		sc_console_unit = -1;
102135446Strhodesstatic	int		sc_saver_keyb_only = 1;
103135446Strhodesstatic  scr_stat    	*sc_console;
104135446Strhodesstatic	struct tty	*sc_console_tty;
105135446Strhodesstatic  struct consdev	*sc_consptr;
106135446Strhodesstatic	void		*kernel_console_ts;
107135446Strhodesstatic	scr_stat	main_console;
108135446Strhodesstatic	struct cdev 	*main_devs[MAXCONS];
109135446Strhodes
110135446Strhodesstatic  char        	init_done = COLD;
111135446Strhodesstatic  char		shutdown_in_progress = FALSE;
112135446Strhodesstatic	char		sc_malloc = FALSE;
113135446Strhodes
114135446Strhodesstatic	int		saver_mode = CONS_NO_SAVER; /* LKM/user saver */
115135446Strhodesstatic	int		run_scrn_saver = FALSE;	/* should run the saver? */
116135446Strhodesstatic	int		enable_bell = TRUE; /* enable beeper */
117135446Strhodes
118135446Strhodes#ifndef SC_DISABLE_REBOOT
119135446Strhodesstatic  int		enable_reboot = TRUE; /* enable keyboard reboot */
120135446Strhodes#endif
121135446Strhodes
122135446Strhodes#ifndef SC_DISABLE_KDBKEY
123135446Strhodesstatic  int		enable_kdbkey = TRUE; /* enable keyboard debug */
124135446Strhodes#endif
125135446Strhodes
126135446Strhodesstatic	long        	scrn_blank_time = 0;    /* screen saver timeout value */
127135446Strhodes#ifdef DEV_SPLASH
128135446Strhodesstatic	int     	scrn_blanked;		/* # of blanked screen */
129135446Strhodesstatic	int		sticky_splash = FALSE;
130135446Strhodes
131135446Strhodesstatic	void		none_saver(sc_softc_t *sc, int blank) { }
132135446Strhodesstatic	void		(*current_saver)(sc_softc_t *, int) = none_saver;
133135446Strhodes#endif
134135446Strhodes
135135446StrhodesSYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
136135446StrhodesSYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
137135446StrhodesSYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
138135446Strhodes    &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
139135446StrhodesSYSCTL_INT(_hw_syscons, OID_AUTO, bell, CTLFLAG_RW, &enable_bell,
140135446Strhodes    0, "enable bell");
141135446Strhodes#ifndef SC_DISABLE_REBOOT
142135446StrhodesSYSCTL_INT(_hw_syscons, OID_AUTO, kbd_reboot, CTLFLAG_RW|CTLFLAG_SECURE, &enable_reboot,
143135446Strhodes    0, "enable keyboard reboot");
144135446Strhodes#endif
145135446Strhodes#ifndef SC_DISABLE_KDBKEY
146135446StrhodesSYSCTL_INT(_hw_syscons, OID_AUTO, kbd_debug, CTLFLAG_RW|CTLFLAG_SECURE, &enable_kdbkey,
147135446Strhodes    0, "enable keyboard debug");
148135446Strhodes#endif
149135446Strhodes#if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
150135446Strhodes#include "font.h"
151135446Strhodes#endif
152135446Strhodes
153135446Strhodes	d_ioctl_t	*sc_user_ioctl;
154135446Strhodes
155135446Strhodesstatic	bios_values_t	bios_value;
156135446Strhodes
157135446Strhodesstatic	int		enable_panic_key;
158135446StrhodesSYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
159135446Strhodes	   0, "Enable panic via keypress specified in kbdmap(5)");
160135446Strhodes
161135446Strhodes#define SC_CONSOLECTL	255
162135446Strhodes
163135446Strhodes#define VIRTUAL_TTY(sc, x) (SC_DEV((sc), (x)) != NULL ?	\
164135446Strhodes	SC_DEV((sc), (x))->si_tty : NULL)
165135446Strhodes#define ISTTYOPEN(tp)	((tp) && ((tp)->t_state & TS_ISOPEN))
166135446Strhodes
167135446Strhodesstatic	int		debugger;
168135446Strhodes
169135446Strhodes/* prototypes */
170135446Strhodesstatic int sc_allocate_keyboard(sc_softc_t *sc, int unit);
171135446Strhodesstatic struct tty *sc_alloc_tty(struct cdev *dev);
172135446Strhodesstatic int scvidprobe(int unit, int flags, int cons);
173135446Strhodesstatic int sckbdprobe(int unit, int flags, int cons);
174135446Strhodesstatic void scmeminit(void *arg);
175135446Strhodesstatic int scdevtounit(struct cdev *dev);
176135446Strhodesstatic kbd_callback_func_t sckbdevent;
177135446Strhodesstatic int scparam(struct tty *tp, struct termios *t);
178135446Strhodesstatic void scstart(struct tty *tp);
179135446Strhodesstatic void scinit(int unit, int flags);
180135446Strhodesstatic scr_stat *sc_get_stat(struct cdev *devptr);
181135446Strhodesstatic void scterm(int unit, int flags);
182135446Strhodesstatic void scshutdown(void *arg, int howto);
183135446Strhodesstatic u_int scgetc(sc_softc_t *sc, u_int flags);
184135446Strhodes#define SCGETC_CN	1
185135446Strhodes#define SCGETC_NONBLOCK	2
186135446Strhodesstatic int sccngetch(int flags);
187135446Strhodesstatic void sccnupdate(scr_stat *scp);
188135446Strhodesstatic scr_stat *alloc_scp(sc_softc_t *sc, int vty);
189135446Strhodesstatic void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
190135446Strhodesstatic timeout_t scrn_timer;
191135446Strhodesstatic int and_region(int *s1, int *e1, int s2, int e2);
192135446Strhodesstatic void scrn_update(scr_stat *scp, int show_cursor);
193135446Strhodes
194135446Strhodes#ifdef DEV_SPLASH
195135446Strhodesstatic int scsplash_callback(int event, void *arg);
196135446Strhodesstatic void scsplash_saver(sc_softc_t *sc, int show);
197135446Strhodesstatic int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
198135446Strhodesstatic int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
199135446Strhodesstatic int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
200135446Strhodesstatic int restore_scrn_saver_mode(scr_stat *scp, int changemode);
201135446Strhodesstatic void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
202135446Strhodesstatic int wait_scrn_saver_stop(sc_softc_t *sc);
203135446Strhodes#define scsplash_stick(stick)		(sticky_splash = (stick))
204135446Strhodes#else /* !DEV_SPLASH */
205135446Strhodes#define scsplash_stick(stick)
206135446Strhodes#endif /* DEV_SPLASH */
207135446Strhodes
208135446Strhodesstatic int do_switch_scr(sc_softc_t *sc, int s);
209135446Strhodesstatic int vt_proc_alive(scr_stat *scp);
210135446Strhodesstatic int signal_vt_rel(scr_stat *scp);
211135446Strhodesstatic int signal_vt_acq(scr_stat *scp);
212135446Strhodesstatic int finish_vt_rel(scr_stat *scp, int release, int *s);
213135446Strhodesstatic int finish_vt_acq(scr_stat *scp);
214135446Strhodesstatic void exchange_scr(sc_softc_t *sc);
215135446Strhodesstatic void update_cursor_image(scr_stat *scp);
216135446Strhodesstatic void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
217135446Strhodesstatic int save_kbd_state(scr_stat *scp);
218135446Strhodesstatic int update_kbd_state(scr_stat *scp, int state, int mask);
219135446Strhodesstatic int update_kbd_leds(scr_stat *scp, int which);
220135446Strhodesstatic timeout_t blink_screen;
221135446Strhodes
222135446Strhodesstatic cn_probe_t	sc_cnprobe;
223135446Strhodesstatic cn_init_t	sc_cninit;
224135446Strhodesstatic cn_term_t	sc_cnterm;
225135446Strhodesstatic cn_getc_t	sc_cngetc;
226135446Strhodesstatic cn_putc_t	sc_cnputc;
227135446Strhodes
228135446StrhodesCONSOLE_DRIVER(sc);
229135446Strhodes
230135446Strhodesstatic	d_open_t	scopen;
231135446Strhodesstatic	d_close_t	scclose;
232135446Strhodesstatic	d_read_t	scread;
233135446Strhodesstatic	d_ioctl_t	scioctl;
234135446Strhodesstatic	d_mmap_t	scmmap;
235135446Strhodes
236135446Strhodesstatic struct cdevsw sc_cdevsw = {
237135446Strhodes	.d_version =	D_VERSION,
238135446Strhodes	.d_open =	scopen,
239135446Strhodes	.d_close =	scclose,
240135446Strhodes	.d_read =	scread,
241135446Strhodes	.d_ioctl =	scioctl,
242135446Strhodes	.d_mmap =	scmmap,
243135446Strhodes	.d_name =	"sc",
244135446Strhodes	.d_flags =	D_TTY | D_NEEDGIANT,
245135446Strhodes};
246135446Strhodes
247135446Strhodesint
248135446Strhodessc_probe_unit(int unit, int flags)
249135446Strhodes{
250135446Strhodes    if (!scvidprobe(unit, flags, FALSE)) {
251135446Strhodes	if (bootverbose)
252135446Strhodes	    printf("%s%d: no video adapter found.\n", SC_DRIVER_NAME, unit);
253135446Strhodes	return ENXIO;
254135446Strhodes    }
255135446Strhodes
256135446Strhodes    /* syscons will be attached even when there is no keyboard */
257135446Strhodes    sckbdprobe(unit, flags, FALSE);
258135446Strhodes
259135446Strhodes    return 0;
260135446Strhodes}
261135446Strhodes
262135446Strhodes/* probe video adapters, return TRUE if found */
263135446Strhodesstatic int
264135446Strhodesscvidprobe(int unit, int flags, int cons)
265135446Strhodes{
266135446Strhodes    /*
267135446Strhodes     * Access the video adapter driver through the back door!
268135446Strhodes     * Video adapter drivers need to be configured before syscons.
269135446Strhodes     * However, when syscons is being probed as the low-level console,
270135446Strhodes     * they have not been initialized yet.  We force them to initialize
271135446Strhodes     * themselves here. XXX
272135446Strhodes     */
273135446Strhodes    vid_configure(cons ? VIO_PROBE_ONLY : 0);
274135446Strhodes
275135446Strhodes    return (vid_find_adapter("*", unit) >= 0);
276135446Strhodes}
277135446Strhodes
278135446Strhodes/* probe the keyboard, return TRUE if found */
279135446Strhodesstatic int
280135446Strhodessckbdprobe(int unit, int flags, int cons)
281135446Strhodes{
282135446Strhodes    /* access the keyboard driver through the backdoor! */
283135446Strhodes    kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
284135446Strhodes
285135446Strhodes    return (kbd_find_keyboard("*", unit) >= 0);
286135446Strhodes}
287135446Strhodes
288135446Strhodesstatic char
289135446Strhodes*adapter_name(video_adapter_t *adp)
290135446Strhodes{
291135446Strhodes    static struct {
292135446Strhodes	int type;
293135446Strhodes	char *name[2];
294135446Strhodes    } names[] = {
295135446Strhodes	{ KD_MONO,	{ "MDA",	"MDA" } },
296135446Strhodes	{ KD_HERCULES,	{ "Hercules",	"Hercules" } },
297135446Strhodes	{ KD_CGA,	{ "CGA",	"CGA" } },
298135446Strhodes	{ KD_EGA,	{ "EGA",	"EGA (mono)" } },
299135446Strhodes	{ KD_VGA,	{ "VGA",	"VGA (mono)" } },
300135446Strhodes	{ KD_PC98,	{ "PC-98x1",	"PC-98x1" } },
301135446Strhodes	{ KD_TGA,	{ "TGA",	"TGA" } },
302135446Strhodes	{ -1,		{ "Unknown",	"Unknown" } },
303135446Strhodes    };
304135446Strhodes    int i;
305135446Strhodes
306135446Strhodes    for (i = 0; names[i].type != -1; ++i)
307135446Strhodes	if (names[i].type == adp->va_type)
308135446Strhodes	    break;
309135446Strhodes    return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
310135446Strhodes}
311135446Strhodes
312135446Strhodesstatic struct tty *
313135446Strhodessc_alloc_tty(struct cdev *dev)
314135446Strhodes{
315135446Strhodes	struct tty *tp;
316135446Strhodes
317135446Strhodes	tp = dev->si_tty = ttyalloc();
318135446Strhodes	ttyinitmode(tp, 1, 0);
319135446Strhodes	tp->t_oproc = scstart;
320135446Strhodes	tp->t_param = scparam;
321135446Strhodes	tp->t_stop = nottystop;
322135446Strhodes	tp->t_dev = dev;
323135446Strhodes	return (tp);
324135446Strhodes}
325135446Strhodes
326135446Strhodesint
327135446Strhodessc_attach_unit(int unit, int flags)
328135446Strhodes{
329135446Strhodes    sc_softc_t *sc;
330135446Strhodes    scr_stat *scp;
331135446Strhodes#ifdef SC_PIXEL_MODE
332135446Strhodes    video_info_t info;
333135446Strhodes#endif
334135446Strhodes    int vc;
335135446Strhodes    struct cdev *dev;
336135446Strhodes
337135446Strhodes    flags &= ~SC_KERNEL_CONSOLE;
338135446Strhodes
339135446Strhodes    if (sc_console_unit == unit) {
340135446Strhodes	/*
341135446Strhodes	 * If this unit is being used as the system console, we need to
342135446Strhodes	 * adjust some variables and buffers before and after scinit().
343135446Strhodes	 */
344135446Strhodes	/* assert(sc_console != NULL) */
345135446Strhodes	flags |= SC_KERNEL_CONSOLE;
346135446Strhodes	scmeminit(NULL);
347135446Strhodes
348135446Strhodes	scinit(unit, flags);
349135446Strhodes
350135446Strhodes	if (sc_console->tsw->te_size > 0) {
351135446Strhodes	    /* assert(sc_console->ts != NULL); */
352135446Strhodes	    kernel_console_ts = sc_console->ts;
353135446Strhodes	    sc_console->ts = malloc(sc_console->tsw->te_size,
354135446Strhodes				    M_DEVBUF, M_WAITOK);
355135446Strhodes	    bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
356135446Strhodes    	    (*sc_console->tsw->te_default_attr)(sc_console,
357135446Strhodes						user_default.std_color,
358135446Strhodes						user_default.rev_color);
359135446Strhodes	}
360135446Strhodes    } else {
361135446Strhodes	scinit(unit, flags);
362135446Strhodes    }
363135446Strhodes
364135446Strhodes    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
365135446Strhodes    sc->config = flags;
366135446Strhodes    scp = sc_get_stat(sc->dev[0]);
367135446Strhodes    if (sc_console == NULL)	/* sc_console_unit < 0 */
368135446Strhodes	sc_console = scp;
369135446Strhodes
370135446Strhodes#ifdef SC_PIXEL_MODE
371135446Strhodes    if ((sc->config & SC_VESA800X600)
372135446Strhodes	&& ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
373135446Strhodes#ifdef DEV_SPLASH
374135446Strhodes	if (sc->flags & SC_SPLASH_SCRN)
375135446Strhodes	    splash_term(sc->adp);
376135446Strhodes#endif
377135446Strhodes	sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
378135446Strhodes	sc_set_pixel_mode(scp, NULL, COL, ROW, 16, 8);
379135446Strhodes	sc->initial_mode = M_VESA_800x600;
380135446Strhodes#ifdef DEV_SPLASH
381135446Strhodes	/* put up the splash again! */
382135446Strhodes	if (sc->flags & SC_SPLASH_SCRN)
383135446Strhodes    	    splash_init(sc->adp, scsplash_callback, sc);
384135446Strhodes#endif
385135446Strhodes    }
386135446Strhodes#endif /* SC_PIXEL_MODE */
387135446Strhodes
388135446Strhodes    /* initialize cursor */
389135446Strhodes    if (!ISGRAPHSC(scp))
390135446Strhodes    	update_cursor_image(scp);
391135446Strhodes
392135446Strhodes    /* get screen update going */
393135446Strhodes    scrn_timer(sc);
394135446Strhodes
395135446Strhodes    /* set up the keyboard */
396135446Strhodes    kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
397135446Strhodes    update_kbd_state(scp, scp->status, LOCK_MASK);
398135446Strhodes
399135446Strhodes    printf("%s%d: %s <%d virtual consoles, flags=0x%x>\n",
400135446Strhodes	   SC_DRIVER_NAME, unit, adapter_name(sc->adp), sc->vtys, sc->config);
401135446Strhodes    if (bootverbose) {
402135446Strhodes	printf("%s%d:", SC_DRIVER_NAME, unit);
403135446Strhodes    	if (sc->adapter >= 0)
404135446Strhodes	    printf(" fb%d", sc->adapter);
405135446Strhodes	if (sc->keyboard >= 0)
406135446Strhodes	    printf(", kbd%d", sc->keyboard);
407135446Strhodes	if (scp->tsw)
408135446Strhodes	    printf(", terminal emulator: %s (%s)",
409135446Strhodes		   scp->tsw->te_name, scp->tsw->te_desc);
410135446Strhodes	printf("\n");
411135446Strhodes    }
412135446Strhodes
413135446Strhodes    /* register a shutdown callback for the kernel console */
414135446Strhodes    if (sc_console_unit == unit)
415135446Strhodes	EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
416135446Strhodes			      (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
417135446Strhodes
418135446Strhodes    for (vc = 0; vc < sc->vtys; vc++) {
419135446Strhodes	if (sc->dev[vc] == NULL) {
420135446Strhodes		sc->dev[vc] = make_dev(&sc_cdevsw, vc + unit * MAXCONS,
421135446Strhodes		    UID_ROOT, GID_WHEEL, 0600, "ttyv%r", vc + unit * MAXCONS);
422135446Strhodes	    	sc_alloc_tty(sc->dev[vc]);
423135446Strhodes		if (vc == 0 && sc->dev == main_devs)
424135446Strhodes			SC_STAT(sc->dev[0]) = &main_console;
425135446Strhodes	}
426135446Strhodes	/*
427135446Strhodes	 * The first vty already has struct tty and scr_stat initialized
428135446Strhodes	 * in scinit().  The other vtys will have these structs when
429135446Strhodes	 * first opened.
430135446Strhodes	 */
431135446Strhodes    }
432135446Strhodes
433135446Strhodes    dev = make_dev(&sc_cdevsw, SC_CONSOLECTL,
434135446Strhodes		   UID_ROOT, GID_WHEEL, 0600, "consolectl");
435135446Strhodes    sc_console_tty = sc_alloc_tty(dev);
436135446Strhodes    ttyconsolemode(sc_console_tty, 0);
437135446Strhodes    SC_STAT(dev) = sc_console;
438135446Strhodes
439135446Strhodes    return 0;
440135446Strhodes}
441135446Strhodes
442135446Strhodesstatic void
443135446Strhodesscmeminit(void *arg)
444135446Strhodes{
445135446Strhodes    if (sc_malloc)
446135446Strhodes	return;
447135446Strhodes    sc_malloc = TRUE;
448135446Strhodes
449135446Strhodes    /*
450135446Strhodes     * As soon as malloc() becomes functional, we had better allocate
451135446Strhodes     * various buffers for the kernel console.
452135446Strhodes     */
453135446Strhodes
454135446Strhodes    if (sc_console_unit < 0)	/* sc_console == NULL */
455135446Strhodes	return;
456135446Strhodes
457135446Strhodes    /* copy the temporary buffer to the final buffer */
458135446Strhodes    sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
459135446Strhodes
460135446Strhodes#ifndef SC_NO_CUTPASTE
461135446Strhodes    sc_alloc_cut_buffer(sc_console, FALSE);
462135446Strhodes#endif
463135446Strhodes
464135446Strhodes#ifndef SC_NO_HISTORY
465135446Strhodes    /* initialize history buffer & pointers */
466135446Strhodes    sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
467135446Strhodes#endif
468135446Strhodes}
469135446Strhodes
470135446Strhodes/* XXX */
471135446StrhodesSYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
472135446Strhodes
473135446Strhodesstatic int
474135446Strhodesscdevtounit(struct cdev *dev)
475135446Strhodes{
476135446Strhodes    int vty = SC_VTY(dev);
477135446Strhodes
478135446Strhodes    if (vty == SC_CONSOLECTL)
479135446Strhodes	return ((sc_console != NULL) ? sc_console->sc->unit : -1);
480135446Strhodes    else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
481135446Strhodes	return -1;
482135446Strhodes    else
483135446Strhodes	return vty/MAXCONS;
484135446Strhodes}
485135446Strhodes
486135446Strhodesstatic int
487135446Strhodesscopen(struct cdev *dev, int flag, int mode, struct thread *td)
488135446Strhodes{
489135446Strhodes    int unit = scdevtounit(dev);
490135446Strhodes    sc_softc_t *sc;
491135446Strhodes    struct tty *tp;
492135446Strhodes    scr_stat *scp;
493135446Strhodes#ifndef __sparc64__
494135446Strhodes    keyarg_t key;
495135446Strhodes#endif
496135446Strhodes    int error;
497135446Strhodes
498135446Strhodes    DPRINTF(5, ("scopen: dev:%s, unit:%d, vty:%d\n",
499135446Strhodes		devtoname(dev), unit, SC_VTY(dev)));
500135446Strhodes
501135446Strhodes    tp = dev->si_tty;
502135446Strhodes    sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
503135446Strhodes    if (sc == NULL)
504135446Strhodes	return ENXIO;
505135446Strhodes
506135446Strhodes    if (!ISTTYOPEN(tp)) {
507135446Strhodes	tp->t_termios = tp->t_init_in;
508135446Strhodes        /* Use the current setting of the <-- key as default VERASE. */
509135446Strhodes        /* If the Delete key is preferable, an stty is necessary     */
510135446Strhodes#ifndef __sparc64__
511135446Strhodes	if (sc->kbd != NULL) {
512135446Strhodes	    key.keynum = KEYCODE_BS;
513135446Strhodes	    kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
514135446Strhodes            tp->t_cc[VERASE] = key.key.map[0];
515135446Strhodes	}
516135446Strhodes#endif
517135446Strhodes	scparam(tp, &tp->t_termios);
518135446Strhodes	ttyld_modem(tp, 1);
519135446Strhodes    }
520135446Strhodes    else
521135446Strhodes	if (tp->t_state & TS_XCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE))
522135446Strhodes	    return(EBUSY);
523135446Strhodes
524135446Strhodes    error = ttyld_open(tp, dev);
525135446Strhodes
526135446Strhodes    scp = sc_get_stat(dev);
527135446Strhodes    if (scp == NULL) {
528135446Strhodes	scp = SC_STAT(dev) = alloc_scp(sc, SC_VTY(dev));
529135446Strhodes	if (ISGRAPHSC(scp))
530135446Strhodes	    sc_set_pixel_mode(scp, NULL, COL, ROW, 16, 8);
531135446Strhodes    }
532135446Strhodes    if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
533135446Strhodes	tp->t_winsize.ws_col = scp->xsize;
534135446Strhodes	tp->t_winsize.ws_row = scp->ysize;
535135446Strhodes    }
536135446Strhodes
537135446Strhodes    return error;
538135446Strhodes}
539135446Strhodes
540135446Strhodesstatic int
541135446Strhodesscclose(struct cdev *dev, int flag, int mode, struct thread *td)
542135446Strhodes{
543135446Strhodes    struct tty *tp = dev->si_tty;
544135446Strhodes    scr_stat *scp;
545135446Strhodes    int s;
546135446Strhodes
547135446Strhodes    if (SC_VTY(dev) != SC_CONSOLECTL) {
548135446Strhodes	scp = sc_get_stat(tp->t_dev);
549135446Strhodes	/* were we in the middle of the VT switching process? */
550135446Strhodes	DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
551135446Strhodes	s = spltty();
552135446Strhodes	if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
553135446Strhodes	    cnavailable(sc_consptr, TRUE);
554135446Strhodes	if (finish_vt_rel(scp, TRUE, &s) == 0)	/* force release */
555135446Strhodes	    DPRINTF(5, ("reset WAIT_REL, "));
556135446Strhodes	if (finish_vt_acq(scp) == 0)		/* force acknowledge */
557135446Strhodes	    DPRINTF(5, ("reset WAIT_ACQ, "));
558135446Strhodes#ifdef not_yet_done
559135446Strhodes	if (scp == &main_console) {
560135446Strhodes	    scp->pid = 0;
561135446Strhodes	    scp->proc = NULL;
562135446Strhodes	    scp->smode.mode = VT_AUTO;
563135446Strhodes	}
564135446Strhodes	else {
565135446Strhodes	    sc_vtb_destroy(&scp->vtb);
566135446Strhodes#ifndef __sparc64__
567135446Strhodes	    sc_vtb_destroy(&scp->scr);
568135446Strhodes#endif
569135446Strhodes	    sc_free_history_buffer(scp, scp->ysize);
570135446Strhodes	    SC_STAT(dev) = NULL;
571135446Strhodes	    free(scp, M_DEVBUF);
572135446Strhodes	}
573135446Strhodes#else
574135446Strhodes	scp->pid = 0;
575135446Strhodes	scp->proc = NULL;
576135446Strhodes	scp->smode.mode = VT_AUTO;
577135446Strhodes#endif
578135446Strhodes	scp->kbd_mode = K_XLATE;
579135446Strhodes	if (scp == scp->sc->cur_scp)
580135446Strhodes	    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
581135446Strhodes	DPRINTF(5, ("done.\n"));
582135446Strhodes    }
583135446Strhodes    spltty();
584135446Strhodes    ttyld_close(tp, flag);
585135446Strhodes    tty_close(tp);
586135446Strhodes    spl0();
587135446Strhodes    return(0);
588135446Strhodes}
589135446Strhodes
590135446Strhodesstatic int
591135446Strhodesscread(struct cdev *dev, struct uio *uio, int flag)
592135446Strhodes{
593135446Strhodes    if (!sc_saver_keyb_only)
594135446Strhodes	sc_touch_scrn_saver();
595135446Strhodes    return ttyread(dev, uio, flag);
596135446Strhodes}
597135446Strhodes
598135446Strhodesstatic int
599135446Strhodessckbdevent(keyboard_t *thiskbd, int event, void *arg)
600135446Strhodes{
601135446Strhodes    sc_softc_t *sc;
602135446Strhodes    struct tty *cur_tty;
603135446Strhodes    int c;
604135446Strhodes    size_t len;
605135446Strhodes    u_char *cp;
606135446Strhodes
607135446Strhodes    sc = (sc_softc_t *)arg;
608135446Strhodes    /* assert(thiskbd == sc->kbd) */
609135446Strhodes
610135446Strhodes    switch (event) {
611135446Strhodes    case KBDIO_KEYINPUT:
612135446Strhodes	break;
613135446Strhodes    case KBDIO_UNLOADING:
614135446Strhodes	sc->kbd = NULL;
615135446Strhodes	sc->keyboard = -1;
616135446Strhodes	kbd_release(thiskbd, (void *)&sc->keyboard);
617135446Strhodes	return 0;
618135446Strhodes    default:
619135446Strhodes	return EINVAL;
620135446Strhodes    }
621135446Strhodes
622135446Strhodes    /*
623135446Strhodes     * Loop while there is still input to get from the keyboard.
624135446Strhodes     * I don't think this is nessesary, and it doesn't fix
625135446Strhodes     * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
626135446Strhodes     */
627135446Strhodes    while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
628135446Strhodes
629135446Strhodes	cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
630135446Strhodes	if (!ISTTYOPEN(cur_tty)) {
631135446Strhodes	    cur_tty = sc_console_tty;
632135446Strhodes	    if (!ISTTYOPEN(cur_tty))
633135446Strhodes		continue;
634135446Strhodes	}
635135446Strhodes
636135446Strhodes	if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
637135446Strhodes	    continue;
638135446Strhodes
639135446Strhodes	switch (KEYFLAGS(c)) {
640135446Strhodes	case 0x0000: /* normal key */
641135446Strhodes	    ttyld_rint(cur_tty, KEYCHAR(c));
642135446Strhodes	    break;
643135446Strhodes	case FKEY:  /* function key, return string */
644135446Strhodes	    cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
645135446Strhodes	    if (cp != NULL) {
646135446Strhodes	    	while (len-- >  0)
647135446Strhodes		    ttyld_rint(cur_tty, *cp++);
648135446Strhodes	    }
649135446Strhodes	    break;
650135446Strhodes	case MKEY:  /* meta is active, prepend ESC */
651135446Strhodes	    ttyld_rint(cur_tty, 0x1b);
652135446Strhodes	    ttyld_rint(cur_tty, KEYCHAR(c));
653135446Strhodes	    break;
654135446Strhodes	case BKEY:  /* backtab fixed sequence (esc [ Z) */
655135446Strhodes	    ttyld_rint(cur_tty, 0x1b);
656135446Strhodes	    ttyld_rint(cur_tty, '[');
657135446Strhodes	    ttyld_rint(cur_tty, 'Z');
658135446Strhodes	    break;
659135446Strhodes	}
660135446Strhodes    }
661135446Strhodes
662135446Strhodes    sc->cur_scp->status |= MOUSE_HIDDEN;
663135446Strhodes
664135446Strhodes    return 0;
665135446Strhodes}
666135446Strhodes
667135446Strhodesstatic int
668135446Strhodesscparam(struct tty *tp, struct termios *t)
669135446Strhodes{
670135446Strhodes    tp->t_ispeed = t->c_ispeed;
671135446Strhodes    tp->t_ospeed = t->c_ospeed;
672135446Strhodes    tp->t_cflag = t->c_cflag;
673135446Strhodes    return 0;
674135446Strhodes}
675135446Strhodes
676135446Strhodesstatic int
677135446Strhodesscioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
678135446Strhodes{
679135446Strhodes    int error;
680135446Strhodes    int i;
681135446Strhodes    struct tty *tp;
682135446Strhodes    sc_softc_t *sc;
683135446Strhodes    scr_stat *scp;
684135446Strhodes    int s;
685135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
686135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
687135446Strhodes    int ival;
688135446Strhodes#endif
689135446Strhodes
690135446Strhodes    tp = dev->si_tty;
691135446Strhodes
692135446Strhodes    /* If there is a user_ioctl function call that first */
693135446Strhodes    if (sc_user_ioctl) {
694135446Strhodes	error = (*sc_user_ioctl)(dev, cmd, data, flag, td);
695135446Strhodes	if (error != ENOIOCTL)
696135446Strhodes	    return error;
697135446Strhodes    }
698135446Strhodes
699135446Strhodes    error = sc_vid_ioctl(tp, cmd, data, flag, td);
700135446Strhodes    if (error != ENOIOCTL)
701135446Strhodes	return error;
702135446Strhodes
703135446Strhodes#ifndef SC_NO_HISTORY
704135446Strhodes    error = sc_hist_ioctl(tp, cmd, data, flag, td);
705135446Strhodes    if (error != ENOIOCTL)
706135446Strhodes	return error;
707135446Strhodes#endif
708135446Strhodes
709135446Strhodes#ifndef SC_NO_SYSMOUSE
710135446Strhodes    error = sc_mouse_ioctl(tp, cmd, data, flag, td);
711135446Strhodes    if (error != ENOIOCTL)
712135446Strhodes	return error;
713135446Strhodes#endif
714135446Strhodes
715135446Strhodes    scp = sc_get_stat(tp->t_dev);
716135446Strhodes    /* assert(scp != NULL) */
717135446Strhodes    /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
718135446Strhodes    sc = scp->sc;
719135446Strhodes
720135446Strhodes    if (scp->tsw) {
721135446Strhodes	error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag, td);
722135446Strhodes	if (error != ENOIOCTL)
723135446Strhodes	    return error;
724135446Strhodes    }
725135446Strhodes
726135446Strhodes    switch (cmd) {  		/* process console hardware related ioctl's */
727135446Strhodes
728135446Strhodes    case GIO_ATTR:      	/* get current attributes */
729135446Strhodes	/* this ioctl is not processed here, but in the terminal emulator */
730135446Strhodes	return ENOTTY;
731135446Strhodes
732135446Strhodes    case GIO_COLOR:     	/* is this a color console ? */
733135446Strhodes	*(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
734135446Strhodes	return 0;
735135446Strhodes
736135446Strhodes    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
737135446Strhodes	if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
738135446Strhodes            return EINVAL;
739135446Strhodes	s = spltty();
740135446Strhodes	scrn_blank_time = *(int *)data;
741135446Strhodes	run_scrn_saver = (scrn_blank_time != 0);
742135446Strhodes	splx(s);
743135446Strhodes	return 0;
744135446Strhodes
745135446Strhodes    case CONS_CURSORTYPE:   	/* set cursor type (obsolete) */
746135446Strhodes	s = spltty();
747135446Strhodes	*(int *)data &= CONS_CURSOR_ATTRS;
748135446Strhodes	sc_change_cursor_shape(scp, *(int *)data, -1, -1);
749135446Strhodes	splx(s);
750135446Strhodes	return 0;
751135446Strhodes
752135446Strhodes    case CONS_GETCURSORSHAPE:   /* get cursor shape (new interface) */
753135446Strhodes	if (((int *)data)[0] & CONS_LOCAL_CURSOR) {
754135446Strhodes	    ((int *)data)[0] = scp->curr_curs_attr.flags;
755135446Strhodes	    ((int *)data)[1] = scp->curr_curs_attr.base;
756135446Strhodes	    ((int *)data)[2] = scp->curr_curs_attr.height;
757135446Strhodes	} else {
758135446Strhodes	    ((int *)data)[0] = sc->curs_attr.flags;
759135446Strhodes	    ((int *)data)[1] = sc->curs_attr.base;
760135446Strhodes	    ((int *)data)[2] = sc->curs_attr.height;
761135446Strhodes	}
762135446Strhodes	return 0;
763135446Strhodes
764135446Strhodes    case CONS_SETCURSORSHAPE:   /* set cursor shape (new interface) */
765135446Strhodes	s = spltty();
766135446Strhodes	sc_change_cursor_shape(scp, ((int *)data)[0],
767135446Strhodes	    ((int *)data)[1], ((int *)data)[2]);
768135446Strhodes	splx(s);
769135446Strhodes	return 0;
770135446Strhodes
771135446Strhodes    case CONS_BELLTYPE: 	/* set bell type sound/visual */
772135446Strhodes	if ((*(int *)data) & CONS_VISUAL_BELL)
773135446Strhodes	    sc->flags |= SC_VISUAL_BELL;
774135446Strhodes	else
775135446Strhodes	    sc->flags &= ~SC_VISUAL_BELL;
776135446Strhodes	if ((*(int *)data) & CONS_QUIET_BELL)
777135446Strhodes	    sc->flags |= SC_QUIET_BELL;
778135446Strhodes	else
779135446Strhodes	    sc->flags &= ~SC_QUIET_BELL;
780135446Strhodes	return 0;
781135446Strhodes
782135446Strhodes    case CONS_GETINFO:  	/* get current (virtual) console info */
783135446Strhodes    {
784135446Strhodes	vid_info_t *ptr = (vid_info_t*)data;
785135446Strhodes	if (ptr->size == sizeof(struct vid_info)) {
786135446Strhodes	    ptr->m_num = sc->cur_scp->index;
787135446Strhodes	    ptr->font_size = scp->font_size;
788135446Strhodes	    ptr->mv_col = scp->xpos;
789135446Strhodes	    ptr->mv_row = scp->ypos;
790135446Strhodes	    ptr->mv_csz = scp->xsize;
791135446Strhodes	    ptr->mv_rsz = scp->ysize;
792135446Strhodes	    ptr->mv_hsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
793135446Strhodes	    /*
794135446Strhodes	     * The following fields are filled by the terminal emulator. XXX
795135446Strhodes	     *
796135446Strhodes	     * ptr->mv_norm.fore
797135446Strhodes	     * ptr->mv_norm.back
798135446Strhodes	     * ptr->mv_rev.fore
799135446Strhodes	     * ptr->mv_rev.back
800135446Strhodes	     */
801135446Strhodes	    ptr->mv_grfc.fore = 0;      /* not supported */
802135446Strhodes	    ptr->mv_grfc.back = 0;      /* not supported */
803135446Strhodes	    ptr->mv_ovscan = scp->border;
804135446Strhodes	    if (scp == sc->cur_scp)
805135446Strhodes		save_kbd_state(scp);
806135446Strhodes	    ptr->mk_keylock = scp->status & LOCK_MASK;
807135446Strhodes	    return 0;
808135446Strhodes	}
809135446Strhodes	return EINVAL;
810135446Strhodes    }
811135446Strhodes
812135446Strhodes    case CONS_GETVERS:  	/* get version number */
813135446Strhodes	*(int*)data = 0x200;    /* version 2.0 */
814135446Strhodes	return 0;
815135446Strhodes
816135446Strhodes    case CONS_IDLE:		/* see if the screen has been idle */
817135446Strhodes	/*
818135446Strhodes	 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
819135446Strhodes	 * the user process may have been writing something on the
820135446Strhodes	 * screen and syscons is not aware of it. Declare the screen
821135446Strhodes	 * is NOT idle if it is in one of these modes. But there is
822135446Strhodes	 * an exception to it; if a screen saver is running in the
823135446Strhodes	 * graphics mode in the current screen, we should say that the
824135446Strhodes	 * screen has been idle.
825135446Strhodes	 */
826135446Strhodes	*(int *)data = (sc->flags & SC_SCRN_IDLE)
827135446Strhodes		       && (!ISGRAPHSC(sc->cur_scp)
828135446Strhodes			   || (sc->cur_scp->status & SAVER_RUNNING));
829135446Strhodes	return 0;
830135446Strhodes
831135446Strhodes    case CONS_SAVERMODE:	/* set saver mode */
832135446Strhodes	switch(*(int *)data) {
833135446Strhodes	case CONS_NO_SAVER:
834135446Strhodes	case CONS_USR_SAVER:
835135446Strhodes	    /* if a LKM screen saver is running, stop it first. */
836135446Strhodes	    scsplash_stick(FALSE);
837135446Strhodes	    saver_mode = *(int *)data;
838135446Strhodes	    s = spltty();
839135446Strhodes#ifdef DEV_SPLASH
840135446Strhodes	    if ((error = wait_scrn_saver_stop(NULL))) {
841135446Strhodes		splx(s);
842135446Strhodes		return error;
843135446Strhodes	    }
844135446Strhodes#endif
845135446Strhodes	    run_scrn_saver = TRUE;
846135446Strhodes	    if (saver_mode == CONS_USR_SAVER)
847135446Strhodes		scp->status |= SAVER_RUNNING;
848135446Strhodes	    else
849135446Strhodes		scp->status &= ~SAVER_RUNNING;
850135446Strhodes	    scsplash_stick(TRUE);
851135446Strhodes	    splx(s);
852135446Strhodes	    break;
853135446Strhodes	case CONS_LKM_SAVER:
854135446Strhodes	    s = spltty();
855135446Strhodes	    if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
856135446Strhodes		scp->status &= ~SAVER_RUNNING;
857135446Strhodes	    saver_mode = *(int *)data;
858135446Strhodes	    splx(s);
859135446Strhodes	    break;
860135446Strhodes	default:
861135446Strhodes	    return EINVAL;
862135446Strhodes	}
863135446Strhodes	return 0;
864135446Strhodes
865135446Strhodes    case CONS_SAVERSTART:	/* immediately start/stop the screen saver */
866135446Strhodes	/*
867135446Strhodes	 * Note that this ioctl does not guarantee the screen saver
868135446Strhodes	 * actually starts or stops. It merely attempts to do so...
869135446Strhodes	 */
870135446Strhodes	s = spltty();
871135446Strhodes	run_scrn_saver = (*(int *)data != 0);
872135446Strhodes	if (run_scrn_saver)
873135446Strhodes	    sc->scrn_time_stamp -= scrn_blank_time;
874135446Strhodes	splx(s);
875135446Strhodes	return 0;
876135446Strhodes
877135446Strhodes    case CONS_SCRSHOT:		/* get a screen shot */
878135446Strhodes    {
879135446Strhodes	int retval, hist_rsz;
880135446Strhodes	size_t lsize, csize;
881135446Strhodes	vm_offset_t frbp, hstp;
882135446Strhodes	unsigned lnum;
883135446Strhodes	scrshot_t *ptr = (scrshot_t *)data;
884135446Strhodes	void *outp = ptr->buf;
885135446Strhodes
886135446Strhodes	if (ptr->x < 0 || ptr->y < 0 || ptr->xsize < 0 || ptr->ysize < 0)
887135446Strhodes		return EINVAL;
888135446Strhodes	s = spltty();
889135446Strhodes	if (ISGRAPHSC(scp)) {
890135446Strhodes	    splx(s);
891135446Strhodes	    return EOPNOTSUPP;
892135446Strhodes	}
893135446Strhodes	hist_rsz = (scp->history != NULL) ? scp->history->vtb_rows : 0;
894135446Strhodes	if (((u_int)ptr->x + ptr->xsize) > scp->xsize ||
895135446Strhodes	    ((u_int)ptr->y + ptr->ysize) > (scp->ysize + hist_rsz)) {
896135446Strhodes	    splx(s);
897135446Strhodes	    return EINVAL;
898135446Strhodes	}
899135446Strhodes
900135446Strhodes	lsize = scp->xsize * sizeof(u_int16_t);
901135446Strhodes	csize = ptr->xsize * sizeof(u_int16_t);
902135446Strhodes	/* Pointer to the last line of framebuffer */
903135446Strhodes	frbp = scp->vtb.vtb_buffer + scp->ysize * lsize + ptr->x *
904135446Strhodes	       sizeof(u_int16_t);
905135446Strhodes	/* Pointer to the last line of target buffer */
906135446Strhodes	outp = (char *)outp + ptr->ysize * csize;
907135446Strhodes	/* Pointer to the last line of history buffer */
908135446Strhodes	if (scp->history != NULL)
909135446Strhodes	    hstp = scp->history->vtb_buffer + sc_vtb_tail(scp->history) *
910135446Strhodes		sizeof(u_int16_t) + ptr->x * sizeof(u_int16_t);
911135446Strhodes	else
912135446Strhodes	    hstp = 0;
913135446Strhodes
914135446Strhodes	retval = 0;
915135446Strhodes	for (lnum = 0; lnum < (ptr->y + ptr->ysize); lnum++) {
916135446Strhodes	    if (lnum < scp->ysize) {
917135446Strhodes		frbp -= lsize;
918135446Strhodes	    } else {
919135446Strhodes		hstp -= lsize;
920135446Strhodes		if (hstp < scp->history->vtb_buffer)
921135446Strhodes		    hstp += scp->history->vtb_rows * lsize;
922135446Strhodes		frbp = hstp;
923135446Strhodes	    }
924135446Strhodes	    if (lnum < ptr->y)
925135446Strhodes		continue;
926135446Strhodes	    outp = (char *)outp - csize;
927135446Strhodes	    retval = copyout((void *)frbp, outp, csize);
928135446Strhodes	    if (retval != 0)
929135446Strhodes		break;
930135446Strhodes	}
931135446Strhodes	splx(s);
932135446Strhodes	return retval;
933135446Strhodes    }
934135446Strhodes
935135446Strhodes    case VT_SETMODE:    	/* set screen switcher mode */
936135446Strhodes    {
937135446Strhodes	struct vt_mode *mode;
938135446Strhodes	struct proc *p1;
939135446Strhodes
940135446Strhodes	mode = (struct vt_mode *)data;
941135446Strhodes	DPRINTF(5, ("%s%d: VT_SETMODE ", SC_DRIVER_NAME, sc->unit));
942135446Strhodes	if (scp->smode.mode == VT_PROCESS) {
943135446Strhodes	    p1 = pfind(scp->pid);
944135446Strhodes    	    if (scp->proc == p1 && scp->proc != td->td_proc) {
945135446Strhodes		if (p1)
946135446Strhodes		    PROC_UNLOCK(p1);
947135446Strhodes		DPRINTF(5, ("error EPERM\n"));
948135446Strhodes		return EPERM;
949135446Strhodes	    }
950135446Strhodes	    if (p1)
951135446Strhodes		PROC_UNLOCK(p1);
952135446Strhodes	}
953135446Strhodes	s = spltty();
954135446Strhodes	if (mode->mode == VT_AUTO) {
955135446Strhodes	    scp->smode.mode = VT_AUTO;
956135446Strhodes	    scp->proc = NULL;
957135446Strhodes	    scp->pid = 0;
958135446Strhodes	    DPRINTF(5, ("VT_AUTO, "));
959135446Strhodes	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
960135446Strhodes		cnavailable(sc_consptr, TRUE);
961135446Strhodes	    /* were we in the middle of the vty switching process? */
962135446Strhodes	    if (finish_vt_rel(scp, TRUE, &s) == 0)
963135446Strhodes		DPRINTF(5, ("reset WAIT_REL, "));
964135446Strhodes	    if (finish_vt_acq(scp) == 0)
965135446Strhodes		DPRINTF(5, ("reset WAIT_ACQ, "));
966135446Strhodes	} else {
967135446Strhodes	    if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
968135446Strhodes		|| !ISSIGVALID(mode->frsig)) {
969135446Strhodes		splx(s);
970135446Strhodes		DPRINTF(5, ("error EINVAL\n"));
971135446Strhodes		return EINVAL;
972135446Strhodes	    }
973135446Strhodes	    DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
974135446Strhodes	    bcopy(data, &scp->smode, sizeof(struct vt_mode));
975135446Strhodes	    scp->proc = td->td_proc;
976135446Strhodes	    scp->pid = scp->proc->p_pid;
977135446Strhodes	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
978135446Strhodes		cnavailable(sc_consptr, FALSE);
979135446Strhodes	}
980135446Strhodes	splx(s);
981135446Strhodes	DPRINTF(5, ("\n"));
982135446Strhodes	return 0;
983135446Strhodes    }
984135446Strhodes
985135446Strhodes    case VT_GETMODE:    	/* get screen switcher mode */
986135446Strhodes	bcopy(&scp->smode, data, sizeof(struct vt_mode));
987135446Strhodes	return 0;
988135446Strhodes
989135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
990135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
991135446Strhodes    case _IO('v', 4):
992135446Strhodes	ival = IOCPARM_IVAL(data);
993135446Strhodes	data = (caddr_t)&ival;
994135446Strhodes	/* FALLTHROUGH */
995135446Strhodes#endif
996135446Strhodes    case VT_RELDISP:    	/* screen switcher ioctl */
997135446Strhodes	s = spltty();
998135446Strhodes	/*
999135446Strhodes	 * This must be the current vty which is in the VT_PROCESS
1000135446Strhodes	 * switching mode...
1001135446Strhodes	 */
1002135446Strhodes	if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
1003135446Strhodes	    splx(s);
1004135446Strhodes	    return EINVAL;
1005135446Strhodes	}
1006135446Strhodes	/* ...and this process is controlling it. */
1007135446Strhodes	if (scp->proc != td->td_proc) {
1008135446Strhodes	    splx(s);
1009135446Strhodes	    return EPERM;
1010135446Strhodes	}
1011135446Strhodes	error = EINVAL;
1012135446Strhodes	switch(*(int *)data) {
1013135446Strhodes	case VT_FALSE:  	/* user refuses to release screen, abort */
1014135446Strhodes	    if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
1015135446Strhodes		DPRINTF(5, ("%s%d: VT_FALSE\n", SC_DRIVER_NAME, sc->unit));
1016135446Strhodes	    break;
1017135446Strhodes	case VT_TRUE:   	/* user has released screen, go on */
1018135446Strhodes	    if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
1019135446Strhodes		DPRINTF(5, ("%s%d: VT_TRUE\n", SC_DRIVER_NAME, sc->unit));
1020135446Strhodes	    break;
1021135446Strhodes	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
1022135446Strhodes	    if ((error = finish_vt_acq(scp)) == 0)
1023135446Strhodes		DPRINTF(5, ("%s%d: VT_ACKACQ\n", SC_DRIVER_NAME, sc->unit));
1024135446Strhodes	    break;
1025135446Strhodes	default:
1026135446Strhodes	    break;
1027135446Strhodes	}
1028135446Strhodes	splx(s);
1029135446Strhodes	return error;
1030135446Strhodes
1031135446Strhodes    case VT_OPENQRY:    	/* return free virtual console */
1032135446Strhodes	for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
1033135446Strhodes	    tp = VIRTUAL_TTY(sc, i);
1034135446Strhodes	    if (!ISTTYOPEN(tp)) {
1035135446Strhodes		*(int *)data = i + 1;
1036135446Strhodes		return 0;
1037135446Strhodes	    }
1038135446Strhodes	}
1039143731Sdougb	return EINVAL;
1040143731Sdougb
1041135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1042135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1043135446Strhodes    case _IO('v', 5):
1044135446Strhodes	ival = IOCPARM_IVAL(data);
1045135446Strhodes	data = (caddr_t)&ival;
1046135446Strhodes	/* FALLTHROUGH */
1047135446Strhodes#endif
1048143731Sdougb    case VT_ACTIVATE:   	/* switch to screen *data */
1049143731Sdougb	i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1050135446Strhodes	s = spltty();
1051135446Strhodes	error = sc_clean_up(sc->cur_scp);
1052135446Strhodes	splx(s);
1053135446Strhodes	if (error)
1054135446Strhodes	    return error;
1055135446Strhodes	return sc_switch_scr(sc, i);
1056135446Strhodes
1057135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1058135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1059135446Strhodes    case _IO('v', 6):
1060135446Strhodes	ival = IOCPARM_IVAL(data);
1061135446Strhodes	data = (caddr_t)&ival;
1062135446Strhodes	/* FALLTHROUGH */
1063135446Strhodes#endif
1064135446Strhodes    case VT_WAITACTIVE: 	/* wait for switch to occur */
1065135446Strhodes	i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
1066135446Strhodes	if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
1067135446Strhodes	    return EINVAL;
1068135446Strhodes	s = spltty();
1069135446Strhodes	error = sc_clean_up(sc->cur_scp);
1070135446Strhodes	splx(s);
1071135446Strhodes	if (error)
1072135446Strhodes	    return error;
1073135446Strhodes	scp = sc_get_stat(SC_DEV(sc, i));
1074135446Strhodes	if (scp == scp->sc->cur_scp)
1075135446Strhodes	    return 0;
1076135446Strhodes	error = tsleep(&scp->smode, PZERO | PCATCH, "waitvt", 0);
1077135446Strhodes	return error;
1078135446Strhodes
1079135446Strhodes    case VT_GETACTIVE:		/* get active vty # */
1080135446Strhodes	*(int *)data = sc->cur_scp->index + 1;
1081135446Strhodes	return 0;
1082135446Strhodes
1083135446Strhodes    case VT_GETINDEX:		/* get this vty # */
1084135446Strhodes	*(int *)data = scp->index + 1;
1085135446Strhodes	return 0;
1086135446Strhodes
1087135446Strhodes    case VT_LOCKSWITCH:		/* prevent vty switching */
1088135446Strhodes	if ((*(int *)data) & 0x01)
1089135446Strhodes	    sc->flags |= SC_SCRN_VTYLOCK;
1090135446Strhodes	else
1091135446Strhodes	    sc->flags &= ~SC_SCRN_VTYLOCK;
1092135446Strhodes	return 0;
1093135446Strhodes
1094135446Strhodes    case KDENABIO:      	/* allow io operations */
1095135446Strhodes	error = priv_check(td, PRIV_IO);
1096135446Strhodes	if (error != 0)
1097135446Strhodes	    return error;
1098135446Strhodes	error = securelevel_gt(td->td_ucred, 0);
1099135446Strhodes	if (error != 0)
1100135446Strhodes		return error;
1101135446Strhodes#ifdef __i386__
1102135446Strhodes	td->td_frame->tf_eflags |= PSL_IOPL;
1103135446Strhodes#elif defined(__amd64__)
1104135446Strhodes	td->td_frame->tf_rflags |= PSL_IOPL;
1105135446Strhodes#endif
1106135446Strhodes	return 0;
1107135446Strhodes
1108135446Strhodes    case KDDISABIO:     	/* disallow io operations (default) */
1109135446Strhodes#ifdef __i386__
1110135446Strhodes	td->td_frame->tf_eflags &= ~PSL_IOPL;
1111135446Strhodes#elif defined(__amd64__)
1112135446Strhodes	td->td_frame->tf_rflags &= ~PSL_IOPL;
1113135446Strhodes#endif
1114135446Strhodes	return 0;
1115135446Strhodes
1116135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1117135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1118135446Strhodes    case _IO('K', 20):
1119135446Strhodes	ival = IOCPARM_IVAL(data);
1120135446Strhodes	data = (caddr_t)&ival;
1121135446Strhodes	/* FALLTHROUGH */
1122135446Strhodes#endif
1123135446Strhodes    case KDSKBSTATE:    	/* set keyboard state (locks) */
1124135446Strhodes	if (*(int *)data & ~LOCK_MASK)
1125135446Strhodes	    return EINVAL;
1126135446Strhodes	scp->status &= ~LOCK_MASK;
1127135446Strhodes	scp->status |= *(int *)data;
1128135446Strhodes	if (scp == sc->cur_scp)
1129135446Strhodes	    update_kbd_state(scp, scp->status, LOCK_MASK);
1130135446Strhodes	return 0;
1131135446Strhodes
1132135446Strhodes    case KDGKBSTATE:    	/* get keyboard state (locks) */
1133135446Strhodes	if (scp == sc->cur_scp)
1134135446Strhodes	    save_kbd_state(scp);
1135135446Strhodes	*(int *)data = scp->status & LOCK_MASK;
1136135446Strhodes	return 0;
1137135446Strhodes
1138135446Strhodes    case KDGETREPEAT:      	/* get keyboard repeat & delay rates */
1139135446Strhodes    case KDSETREPEAT:      	/* set keyboard repeat & delay rates (new) */
1140135446Strhodes	error = kbd_ioctl(sc->kbd, cmd, data);
1141135446Strhodes	if (error == ENOIOCTL)
1142135446Strhodes	    error = ENODEV;
1143135446Strhodes	return error;
1144135446Strhodes
1145135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1146135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1147135446Strhodes    case _IO('K', 67):
1148135446Strhodes	ival = IOCPARM_IVAL(data);
1149135446Strhodes	data = (caddr_t)&ival;
1150135446Strhodes	/* FALLTHROUGH */
1151135446Strhodes#endif
1152135446Strhodes    case KDSETRAD:      	/* set keyboard repeat & delay rates (old) */
1153135446Strhodes	if (*(int *)data & ~0x7f)
1154135446Strhodes	    return EINVAL;
1155135446Strhodes	error = kbd_ioctl(sc->kbd, KDSETRAD, data);
1156135446Strhodes	if (error == ENOIOCTL)
1157135446Strhodes	    error = ENODEV;
1158135446Strhodes	return error;
1159135446Strhodes
1160135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1161135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1162135446Strhodes    case _IO('K', 7):
1163135446Strhodes	ival = IOCPARM_IVAL(data);
1164135446Strhodes	data = (caddr_t)&ival;
1165135446Strhodes	/* FALLTHROUGH */
1166135446Strhodes#endif
1167135446Strhodes    case KDSKBMODE:     	/* set keyboard mode */
1168135446Strhodes	switch (*(int *)data) {
1169135446Strhodes	case K_XLATE:   	/* switch to XLT ascii mode */
1170135446Strhodes	case K_RAW: 		/* switch to RAW scancode mode */
1171135446Strhodes	case K_CODE: 		/* switch to CODE mode */
1172135446Strhodes	    scp->kbd_mode = *(int *)data;
1173135446Strhodes	    if (scp == sc->cur_scp)
1174135446Strhodes		kbd_ioctl(sc->kbd, KDSKBMODE, data);
1175135446Strhodes	    return 0;
1176135446Strhodes	default:
1177135446Strhodes	    return EINVAL;
1178135446Strhodes	}
1179135446Strhodes	/* NOT REACHED */
1180135446Strhodes
1181135446Strhodes    case KDGKBMODE:     	/* get keyboard mode */
1182135446Strhodes	*(int *)data = scp->kbd_mode;
1183135446Strhodes	return 0;
1184135446Strhodes
1185135446Strhodes    case KDGKBINFO:
1186135446Strhodes	error = kbd_ioctl(sc->kbd, cmd, data);
1187135446Strhodes	if (error == ENOIOCTL)
1188135446Strhodes	    error = ENODEV;
1189135446Strhodes	return error;
1190135446Strhodes
1191135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1192135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1193135446Strhodes    case _IO('K', 8):
1194135446Strhodes	ival = IOCPARM_IVAL(data);
1195135446Strhodes	data = (caddr_t)&ival;
1196135446Strhodes	/* FALLTHROUGH */
1197135446Strhodes#endif
1198135446Strhodes    case KDMKTONE:      	/* sound the bell */
1199135446Strhodes	if (*(int*)data)
1200135446Strhodes	    sc_bell(scp, (*(int*)data)&0xffff,
1201135446Strhodes		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1202135446Strhodes	else
1203135446Strhodes	    sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1204135446Strhodes	return 0;
1205135446Strhodes
1206135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1207135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1208135446Strhodes    case _IO('K', 63):
1209135446Strhodes	ival = IOCPARM_IVAL(data);
1210135446Strhodes	data = (caddr_t)&ival;
1211135446Strhodes	/* FALLTHROUGH */
1212135446Strhodes#endif
1213135446Strhodes    case KIOCSOUND:     	/* make tone (*data) hz */
1214135446Strhodes	if (scp == sc->cur_scp) {
1215135446Strhodes	    if (*(int *)data)
1216135446Strhodes		return sc_tone(*(int *)data);
1217135446Strhodes	    else
1218135446Strhodes		return sc_tone(0);
1219135446Strhodes	}
1220135446Strhodes	return 0;
1221135446Strhodes
1222135446Strhodes    case KDGKBTYPE:     	/* get keyboard type */
1223135446Strhodes	error = kbd_ioctl(sc->kbd, cmd, data);
1224135446Strhodes	if (error == ENOIOCTL) {
1225135446Strhodes	    /* always return something? XXX */
1226135446Strhodes	    *(int *)data = 0;
1227135446Strhodes	}
1228135446Strhodes	return 0;
1229135446Strhodes
1230135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1231135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1232135446Strhodes    case _IO('K', 66):
1233135446Strhodes	ival = IOCPARM_IVAL(data);
1234135446Strhodes	data = (caddr_t)&ival;
1235135446Strhodes	/* FALLTHROUGH */
1236135446Strhodes#endif
1237135446Strhodes    case KDSETLED:      	/* set keyboard LED status */
1238135446Strhodes	if (*(int *)data & ~LED_MASK)	/* FIXME: LOCK_MASK? */
1239135446Strhodes	    return EINVAL;
1240135446Strhodes	scp->status &= ~LED_MASK;
1241135446Strhodes	scp->status |= *(int *)data;
1242135446Strhodes	if (scp == sc->cur_scp)
1243135446Strhodes	    update_kbd_leds(scp, scp->status);
1244135446Strhodes	return 0;
1245135446Strhodes
1246135446Strhodes    case KDGETLED:      	/* get keyboard LED status */
1247135446Strhodes	if (scp == sc->cur_scp)
1248135446Strhodes	    save_kbd_state(scp);
1249135446Strhodes	*(int *)data = scp->status & LED_MASK;
1250135446Strhodes	return 0;
1251135446Strhodes
1252135446Strhodes    case KBADDKBD:		/* add/remove keyboard to/from mux */
1253135446Strhodes    case KBRELKBD:
1254135446Strhodes	error = kbd_ioctl(sc->kbd, cmd, data);
1255135446Strhodes	if (error == ENOIOCTL)
1256135446Strhodes	    error = ENODEV;
1257135446Strhodes	return error;
1258135446Strhodes
1259135446Strhodes#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1260135446Strhodes    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1261135446Strhodes    case _IO('c', 110):
1262135446Strhodes	ival = IOCPARM_IVAL(data);
1263135446Strhodes	data = (caddr_t)&ival;
1264135446Strhodes	/* FALLTHROUGH */
1265135446Strhodes#endif
1266135446Strhodes    case CONS_SETKBD: 		/* set the new keyboard */
1267135446Strhodes	{
1268135446Strhodes	    keyboard_t *newkbd;
1269135446Strhodes
1270143731Sdougb	    s = spltty();
1271143731Sdougb	    newkbd = kbd_get_keyboard(*(int *)data);
1272135446Strhodes	    if (newkbd == NULL) {
1273135446Strhodes		splx(s);
1274135446Strhodes		return EINVAL;
1275135446Strhodes	    }
1276135446Strhodes	    error = 0;
1277135446Strhodes	    if (sc->kbd != newkbd) {
1278135446Strhodes		i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1279135446Strhodes				 (void *)&sc->keyboard, sckbdevent, sc);
1280135446Strhodes		/* i == newkbd->kb_index */
1281135446Strhodes		if (i >= 0) {
1282135446Strhodes		    if (sc->kbd != NULL) {
1283135446Strhodes			save_kbd_state(sc->cur_scp);
1284143731Sdougb			kbd_release(sc->kbd, (void *)&sc->keyboard);
1285143731Sdougb		    }
1286135446Strhodes		    sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1287135446Strhodes		    sc->keyboard = i;
1288135446Strhodes		    kbd_ioctl(sc->kbd, KDSKBMODE,
1289135446Strhodes			      (caddr_t)&sc->cur_scp->kbd_mode);
1290135446Strhodes		    update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1291135446Strhodes				     LOCK_MASK);
1292135446Strhodes		} else {
1293135446Strhodes		    error = EPERM;	/* XXX */
1294135446Strhodes		}
1295135446Strhodes	    }
1296135446Strhodes	    splx(s);
1297135446Strhodes	    return error;
1298135446Strhodes	}
1299135446Strhodes
1300135446Strhodes    case CONS_RELKBD: 		/* release the current keyboard */
1301135446Strhodes	s = spltty();
1302135446Strhodes	error = 0;
1303135446Strhodes	if (sc->kbd != NULL) {
1304135446Strhodes	    save_kbd_state(sc->cur_scp);
1305135446Strhodes	    error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1306135446Strhodes	    if (error == 0) {
1307135446Strhodes		sc->kbd = NULL;
1308135446Strhodes		sc->keyboard = -1;
1309135446Strhodes	    }
1310135446Strhodes	}
1311135446Strhodes	splx(s);
1312135446Strhodes	return error;
1313135446Strhodes
1314135446Strhodes    case CONS_GETTERM:		/* get the current terminal emulator info */
1315135446Strhodes	{
1316135446Strhodes	    sc_term_sw_t *sw;
1317135446Strhodes
1318135446Strhodes	    if (((term_info_t *)data)->ti_index == 0) {
1319135446Strhodes		sw = scp->tsw;
1320135446Strhodes	    } else {
1321135446Strhodes		sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1322135446Strhodes	    }
1323135446Strhodes	    if (sw != NULL) {
1324135446Strhodes		strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1325135446Strhodes			sizeof(((term_info_t *)data)->ti_name));
1326135446Strhodes		strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1327135446Strhodes			sizeof(((term_info_t *)data)->ti_desc));
1328135446Strhodes		((term_info_t *)data)->ti_flags = 0;
1329135446Strhodes		return 0;
1330135446Strhodes	    } else {
1331135446Strhodes		((term_info_t *)data)->ti_name[0] = '\0';
1332135446Strhodes		((term_info_t *)data)->ti_desc[0] = '\0';
1333135446Strhodes		((term_info_t *)data)->ti_flags = 0;
1334135446Strhodes		return EINVAL;
1335135446Strhodes	    }
1336135446Strhodes	}
1337135446Strhodes
1338135446Strhodes    case CONS_SETTERM:		/* set the current terminal emulator */
1339135446Strhodes	s = spltty();
1340135446Strhodes	error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1341135446Strhodes	/* FIXME: what if scp == sc_console! XXX */
1342135446Strhodes	splx(s);
1343135446Strhodes	return error;
1344135446Strhodes
1345135446Strhodes    case GIO_SCRNMAP:   	/* get output translation table */
1346135446Strhodes	bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1347135446Strhodes	return 0;
1348135446Strhodes
1349135446Strhodes    case PIO_SCRNMAP:   	/* set output translation table */
1350135446Strhodes	bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1351135446Strhodes	for (i=0; i<sizeof(sc->scr_map); i++) {
1352135446Strhodes	    sc->scr_rmap[sc->scr_map[i]] = i;
1353135446Strhodes	}
1354135446Strhodes	return 0;
1355135446Strhodes
1356143731Sdougb    case GIO_KEYMAP:		/* get keyboard translation table */
1357143731Sdougb    case PIO_KEYMAP:		/* set keyboard translation table */
1358135446Strhodes    case GIO_DEADKEYMAP:	/* get accent key translation table */
1359135446Strhodes    case PIO_DEADKEYMAP:	/* set accent key translation table */
1360135446Strhodes    case GETFKEY:		/* get function key string */
1361135446Strhodes    case SETFKEY:		/* set function key string */
1362135446Strhodes	error = kbd_ioctl(sc->kbd, cmd, data);
1363135446Strhodes	if (error == ENOIOCTL)
1364135446Strhodes	    error = ENODEV;
1365135446Strhodes	return error;
1366135446Strhodes
1367135446Strhodes#ifndef SC_NO_FONT_LOADING
1368135446Strhodes
1369135446Strhodes    case PIO_FONT8x8:   	/* set 8x8 dot font */
1370135446Strhodes	if (!ISFONTAVAIL(sc->adp->va_flags))
1371135446Strhodes	    return ENXIO;
1372135446Strhodes	bcopy(data, sc->font_8, 8*256);
1373135446Strhodes	sc->fonts_loaded |= FONT_8;
1374135446Strhodes	/*
1375135446Strhodes	 * FONT KLUDGE
1376135446Strhodes	 * Always use the font page #0. XXX
1377135446Strhodes	 * Don't load if the current font size is not 8x8.
1378135446Strhodes	 */
1379135446Strhodes	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1380135446Strhodes	    sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1381135446Strhodes	return 0;
1382135446Strhodes
1383135446Strhodes    case GIO_FONT8x8:   	/* get 8x8 dot font */
1384135446Strhodes	if (!ISFONTAVAIL(sc->adp->va_flags))
1385135446Strhodes	    return ENXIO;
1386135446Strhodes	if (sc->fonts_loaded & FONT_8) {
1387135446Strhodes	    bcopy(sc->font_8, data, 8*256);
1388135446Strhodes	    return 0;
1389135446Strhodes	}
1390135446Strhodes	else
1391135446Strhodes	    return ENXIO;
1392135446Strhodes
1393135446Strhodes    case PIO_FONT8x14:  	/* set 8x14 dot font */
1394135446Strhodes	if (!ISFONTAVAIL(sc->adp->va_flags))
1395135446Strhodes	    return ENXIO;
1396135446Strhodes	bcopy(data, sc->font_14, 14*256);
1397135446Strhodes	sc->fonts_loaded |= FONT_14;
1398135446Strhodes	/*
1399135446Strhodes	 * FONT KLUDGE
1400135446Strhodes	 * Always use the font page #0. XXX
1401135446Strhodes	 * Don't load if the current font size is not 8x14.
1402135446Strhodes	 */
1403135446Strhodes	if (ISTEXTSC(sc->cur_scp)
1404135446Strhodes	    && (sc->cur_scp->font_size >= 14)
1405135446Strhodes	    && (sc->cur_scp->font_size < 16))
1406135446Strhodes	    sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1407135446Strhodes	return 0;
1408135446Strhodes
1409135446Strhodes    case GIO_FONT8x14:  	/* get 8x14 dot font */
1410135446Strhodes	if (!ISFONTAVAIL(sc->adp->va_flags))
1411135446Strhodes	    return ENXIO;
1412135446Strhodes	if (sc->fonts_loaded & FONT_14) {
1413135446Strhodes	    bcopy(sc->font_14, data, 14*256);
1414135446Strhodes	    return 0;
1415135446Strhodes	}
1416135446Strhodes	else
1417135446Strhodes	    return ENXIO;
1418135446Strhodes
1419135446Strhodes    case PIO_FONT8x16:  	/* set 8x16 dot font */
1420135446Strhodes	if (!ISFONTAVAIL(sc->adp->va_flags))
1421135446Strhodes	    return ENXIO;
1422135446Strhodes	bcopy(data, sc->font_16, 16*256);
1423135446Strhodes	sc->fonts_loaded |= FONT_16;
1424135446Strhodes	/*
1425135446Strhodes	 * FONT KLUDGE
1426135446Strhodes	 * Always use the font page #0. XXX
1427135446Strhodes	 * Don't load if the current font size is not 8x16.
1428135446Strhodes	 */
1429135446Strhodes	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1430135446Strhodes	    sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1431135446Strhodes	return 0;
1432135446Strhodes
1433135446Strhodes    case GIO_FONT8x16:  	/* get 8x16 dot font */
1434135446Strhodes	if (!ISFONTAVAIL(sc->adp->va_flags))
1435135446Strhodes	    return ENXIO;
1436135446Strhodes	if (sc->fonts_loaded & FONT_16) {
1437135446Strhodes	    bcopy(sc->font_16, data, 16*256);
1438135446Strhodes	    return 0;
1439135446Strhodes	}
1440135446Strhodes	else
1441135446Strhodes	    return ENXIO;
1442135446Strhodes
1443135446Strhodes#endif /* SC_NO_FONT_LOADING */
1444135446Strhodes
1445135446Strhodes    default:
1446135446Strhodes	break;
1447135446Strhodes    }
1448135446Strhodes
1449135446Strhodes    return (ttyioctl(dev, cmd, data, flag, td));
1450135446Strhodes}
1451135446Strhodes
1452135446Strhodesstatic void
1453135446Strhodesscstart(struct tty *tp)
1454135446Strhodes{
1455135446Strhodes    struct clist *rbp;
1456135446Strhodes    int s, len;
1457135446Strhodes    u_char buf[PCBURST];
1458135446Strhodes    scr_stat *scp = sc_get_stat(tp->t_dev);
1459135446Strhodes
1460135446Strhodes    if (scp->status & SLKED ||
1461135446Strhodes	(scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
1462135446Strhodes	return;
1463135446Strhodes    s = spltty();
1464135446Strhodes    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1465135446Strhodes	tp->t_state |= TS_BUSY;
1466135446Strhodes	rbp = &tp->t_outq;
1467135446Strhodes	while (rbp->c_cc) {
1468135446Strhodes	    len = q_to_b(rbp, buf, PCBURST);
1469135446Strhodes	    splx(s);
1470135446Strhodes	    sc_puts(scp, buf, len);
1471135446Strhodes	    s = spltty();
1472135446Strhodes	}
1473135446Strhodes	tp->t_state &= ~TS_BUSY;
1474135446Strhodes	ttwwakeup(tp);
1475135446Strhodes    }
1476135446Strhodes    splx(s);
1477135446Strhodes}
1478135446Strhodes
1479135446Strhodesstatic void
1480135446Strhodessc_cnprobe(struct consdev *cp)
1481135446Strhodes{
1482135446Strhodes    int unit;
1483135446Strhodes    int flags;
1484135446Strhodes
1485135446Strhodes    cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1486135446Strhodes
1487135446Strhodes    /* a video card is always required */
1488135446Strhodes    if (!scvidprobe(unit, flags, TRUE))
1489135446Strhodes	cp->cn_pri = CN_DEAD;
1490135446Strhodes
1491135446Strhodes    /* syscons will become console even when there is no keyboard */
1492135446Strhodes    sckbdprobe(unit, flags, TRUE);
1493135446Strhodes
1494135446Strhodes    if (cp->cn_pri == CN_DEAD)
1495135446Strhodes	return;
1496135446Strhodes
1497135446Strhodes    /* initialize required fields */
1498135446Strhodes    sprintf(cp->cn_name, "consolectl");
1499135446Strhodes}
1500135446Strhodes
1501135446Strhodesstatic void
1502135446Strhodessc_cninit(struct consdev *cp)
1503135446Strhodes{
1504135446Strhodes    int unit;
1505135446Strhodes    int flags;
1506135446Strhodes
1507135446Strhodes    sc_get_cons_priority(&unit, &flags);
1508135446Strhodes    scinit(unit, flags | SC_KERNEL_CONSOLE);
1509135446Strhodes    sc_console_unit = unit;
1510135446Strhodes    sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1511135446Strhodes    sc_consptr = cp;
1512135446Strhodes}
1513135446Strhodes
1514135446Strhodesstatic void
1515135446Strhodessc_cnterm(struct consdev *cp)
1516135446Strhodes{
1517135446Strhodes    /* we are not the kernel console any more, release everything */
1518135446Strhodes
1519135446Strhodes    if (sc_console_unit < 0)
1520135446Strhodes	return;			/* shouldn't happen */
1521135446Strhodes
1522135446Strhodes#if 0 /* XXX */
1523135446Strhodes    sc_clear_screen(sc_console);
1524135446Strhodes    sccnupdate(sc_console);
1525135446Strhodes#endif
1526135446Strhodes
1527135446Strhodes    scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1528135446Strhodes    sc_console_unit = -1;
1529135446Strhodes    sc_console = NULL;
1530135446Strhodes}
1531135446Strhodes
1532135446Strhodesstatic void
1533135446Strhodessc_cnputc(struct consdev *cd, int c)
1534135446Strhodes{
1535135446Strhodes    u_char buf[1];
1536135446Strhodes    scr_stat *scp = sc_console;
1537135446Strhodes    void *save;
1538135446Strhodes#ifndef SC_NO_HISTORY
1539135446Strhodes    struct tty *tp;
1540135446Strhodes#endif /* !SC_NO_HISTORY */
1541135446Strhodes    int s;
1542135446Strhodes
1543135446Strhodes    /* assert(sc_console != NULL) */
1544135446Strhodes
1545135446Strhodes#ifndef SC_NO_HISTORY
1546135446Strhodes    if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1547135446Strhodes	scp->status &= ~SLKED;
1548135446Strhodes	update_kbd_state(scp, scp->status, SLKED);
1549135446Strhodes	if (scp->status & BUFFER_SAVED) {
1550143731Sdougb	    if (!sc_hist_restore(scp))
1551143731Sdougb		sc_remove_cutmarking(scp);
1552135446Strhodes	    scp->status &= ~BUFFER_SAVED;
1553135446Strhodes	    scp->status |= CURSOR_ENABLED;
1554135446Strhodes	    sc_draw_cursor_image(scp);
1555135446Strhodes	}
1556135446Strhodes	tp = VIRTUAL_TTY(scp->sc, scp->index);
1557135446Strhodes	if (ISTTYOPEN(tp))
1558135446Strhodes	    scstart(tp);
1559135446Strhodes    }
1560135446Strhodes#endif /* !SC_NO_HISTORY */
1561135446Strhodes
1562135446Strhodes    save = scp->ts;
1563135446Strhodes    if (kernel_console_ts != NULL)
1564135446Strhodes	scp->ts = kernel_console_ts;
1565135446Strhodes    buf[0] = c;
1566135446Strhodes    sc_puts(scp, buf, 1);
1567135446Strhodes    scp->ts = save;
1568153816Sdougb
1569135446Strhodes    s = spltty();	/* block sckbdevent and scrn_timer */
1570135446Strhodes    sccnupdate(scp);
1571135446Strhodes    splx(s);
1572135446Strhodes}
1573135446Strhodes
1574135446Strhodesstatic int
1575135446Strhodessc_cngetc(struct consdev *cd)
1576135446Strhodes{
1577135446Strhodes    return sccngetch(SCGETC_NONBLOCK);
1578135446Strhodes}
1579135446Strhodes
1580135446Strhodesstatic int
1581135446Strhodessccngetch(int flags)
1582135446Strhodes{
1583135446Strhodes    static struct fkeytab fkey;
1584135446Strhodes    static int fkeycp;
1585135446Strhodes    scr_stat *scp;
1586135446Strhodes    u_char *p;
1587135446Strhodes    int cur_mode;
1588135446Strhodes    int s = spltty();	/* block sckbdevent and scrn_timer while we poll */
1589135446Strhodes    int c;
1590135446Strhodes
1591135446Strhodes    /* assert(sc_console != NULL) */
1592135446Strhodes
1593135446Strhodes    /*
1594135446Strhodes     * Stop the screen saver and update the screen if necessary.
1595135446Strhodes     * What if we have been running in the screen saver code... XXX
1596135446Strhodes     */
1597135446Strhodes    sc_touch_scrn_saver();
1598135446Strhodes    scp = sc_console->sc->cur_scp;	/* XXX */
1599135446Strhodes    sccnupdate(scp);
1600135446Strhodes
1601135446Strhodes    if (fkeycp < fkey.len) {
1602135446Strhodes	splx(s);
1603135446Strhodes	return fkey.str[fkeycp++];
1604135446Strhodes    }
1605135446Strhodes
1606135446Strhodes    if (scp->sc->kbd == NULL) {
1607135446Strhodes	splx(s);
1608135446Strhodes	return -1;
1609135446Strhodes    }
1610135446Strhodes
1611135446Strhodes    /*
1612135446Strhodes     * Make sure the keyboard is accessible even when the kbd device
1613135446Strhodes     * driver is disabled.
1614135446Strhodes     */
1615135446Strhodes    kbd_enable(scp->sc->kbd);
1616135446Strhodes
1617135446Strhodes    /* we shall always use the keyboard in the XLATE mode here */
1618135446Strhodes    cur_mode = scp->kbd_mode;
1619135446Strhodes    scp->kbd_mode = K_XLATE;
1620135446Strhodes    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1621135446Strhodes
1622135446Strhodes    kbd_poll(scp->sc->kbd, TRUE);
1623135446Strhodes    c = scgetc(scp->sc, SCGETC_CN | flags);
1624135446Strhodes    kbd_poll(scp->sc->kbd, FALSE);
1625135446Strhodes
1626135446Strhodes    scp->kbd_mode = cur_mode;
1627135446Strhodes    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1628135446Strhodes    kbd_disable(scp->sc->kbd);
1629135446Strhodes    splx(s);
1630135446Strhodes
1631135446Strhodes    switch (KEYFLAGS(c)) {
1632135446Strhodes    case 0:	/* normal char */
1633135446Strhodes	return KEYCHAR(c);
1634135446Strhodes    case FKEY:	/* function key */
1635135446Strhodes	p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1636135446Strhodes	fkey.len = fkeycp;
1637135446Strhodes	if ((p != NULL) && (fkey.len > 0)) {
1638135446Strhodes	    bcopy(p, fkey.str, fkey.len);
1639135446Strhodes	    fkeycp = 1;
1640135446Strhodes	    return fkey.str[0];
1641135446Strhodes	}
1642135446Strhodes	return c;	/* XXX */
1643135446Strhodes    case NOKEY:
1644135446Strhodes    case ERRKEY:
1645135446Strhodes    default:
1646135446Strhodes	return -1;
1647135446Strhodes    }
1648135446Strhodes    /* NOT REACHED */
1649135446Strhodes}
1650135446Strhodes
1651135446Strhodesstatic void
1652135446Strhodessccnupdate(scr_stat *scp)
1653135446Strhodes{
1654135446Strhodes    /* this is a cut-down version of scrn_timer()... */
1655135446Strhodes
1656135446Strhodes    if (scp->sc->font_loading_in_progress)
1657135446Strhodes	return;
1658135446Strhodes
1659135446Strhodes    if (debugger > 0 || panicstr || shutdown_in_progress) {
1660135446Strhodes	sc_touch_scrn_saver();
1661135446Strhodes    } else if (scp != scp->sc->cur_scp) {
1662135446Strhodes	return;
1663135446Strhodes    }
1664135446Strhodes
1665135446Strhodes    if (!run_scrn_saver)
1666135446Strhodes	scp->sc->flags &= ~SC_SCRN_IDLE;
1667135446Strhodes#ifdef DEV_SPLASH
1668135446Strhodes    if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1669135446Strhodes	if (scp->sc->flags & SC_SCRN_BLANKED)
1670135446Strhodes            stop_scrn_saver(scp->sc, current_saver);
1671135446Strhodes#endif
1672135446Strhodes
1673135446Strhodes    if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1674135446Strhodes	|| scp->sc->switch_in_progress)
1675135446Strhodes	return;
1676135446Strhodes    /*
1677135446Strhodes     * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1678135446Strhodes     * when write_in_progress is non-zero.  XXX
1679135446Strhodes     */
1680135446Strhodes
1681135446Strhodes    if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1682135446Strhodes	scrn_update(scp, TRUE);
1683135446Strhodes}
1684135446Strhodes
1685135446Strhodesstatic void
1686135446Strhodesscrn_timer(void *arg)
1687135446Strhodes{
1688135446Strhodes#ifndef PC98
1689135446Strhodes    static int kbd_interval = 0;
1690135446Strhodes#endif
1691135446Strhodes    struct timeval tv;
1692135446Strhodes    sc_softc_t *sc;
1693135446Strhodes    scr_stat *scp;
1694135446Strhodes    int again;
1695135446Strhodes    int s;
1696135446Strhodes
1697135446Strhodes    again = (arg != NULL);
1698135446Strhodes    if (arg != NULL)
1699135446Strhodes	sc = (sc_softc_t *)arg;
1700135446Strhodes    else if (sc_console != NULL)
1701135446Strhodes	sc = sc_console->sc;
1702135446Strhodes    else
1703135446Strhodes	return;
1704135446Strhodes
1705135446Strhodes    /* don't do anything when we are performing some I/O operations */
1706135446Strhodes    if (sc->font_loading_in_progress) {
1707135446Strhodes	if (again)
1708135446Strhodes	    timeout(scrn_timer, sc, hz / 10);
1709135446Strhodes	return;
1710135446Strhodes    }
1711135446Strhodes    s = spltty();
1712135446Strhodes
1713135446Strhodes#ifndef PC98
1714135446Strhodes    if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1715135446Strhodes	/* try to allocate a keyboard automatically */
1716135446Strhodes	if (++kbd_interval >= 25) {
1717135446Strhodes	    sc->keyboard = sc_allocate_keyboard(sc, -1);
1718135446Strhodes	    if (sc->keyboard >= 0) {
1719135446Strhodes		sc->kbd = kbd_get_keyboard(sc->keyboard);
1720135446Strhodes		kbd_ioctl(sc->kbd, KDSKBMODE,
1721135446Strhodes			  (caddr_t)&sc->cur_scp->kbd_mode);
1722135446Strhodes		update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1723135446Strhodes				 LOCK_MASK);
1724135446Strhodes	    }
1725135446Strhodes	    kbd_interval = 0;
1726135446Strhodes	}
1727135446Strhodes    }
1728135446Strhodes#endif /* PC98 */
1729135446Strhodes
1730135446Strhodes    /* find the vty to update */
1731135446Strhodes    scp = sc->cur_scp;
1732135446Strhodes
1733135446Strhodes    /* should we stop the screen saver? */
1734135446Strhodes    getmicrouptime(&tv);
1735135446Strhodes    if (debugger > 0 || panicstr || shutdown_in_progress)
1736135446Strhodes	sc_touch_scrn_saver();
1737135446Strhodes    if (run_scrn_saver) {
1738135446Strhodes	if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1739135446Strhodes	    sc->flags |= SC_SCRN_IDLE;
1740135446Strhodes	else
1741135446Strhodes	    sc->flags &= ~SC_SCRN_IDLE;
1742135446Strhodes    } else {
1743135446Strhodes	sc->scrn_time_stamp = tv.tv_sec;
1744135446Strhodes	sc->flags &= ~SC_SCRN_IDLE;
1745135446Strhodes	if (scrn_blank_time > 0)
1746135446Strhodes	    run_scrn_saver = TRUE;
1747135446Strhodes    }
1748135446Strhodes#ifdef DEV_SPLASH
1749135446Strhodes    if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1750135446Strhodes	if (sc->flags & SC_SCRN_BLANKED)
1751135446Strhodes            stop_scrn_saver(sc, current_saver);
1752135446Strhodes#endif
1753135446Strhodes
1754153816Sdougb    /* should we just return ? */
1755153816Sdougb    if (sc->blink_in_progress || sc->switch_in_progress
1756135446Strhodes	|| sc->write_in_progress) {
1757135446Strhodes	if (again)
1758135446Strhodes	    timeout(scrn_timer, sc, hz / 10);
1759135446Strhodes	splx(s);
1760135446Strhodes	return;
1761135446Strhodes    }
1762135446Strhodes
1763135446Strhodes    /* Update the screen */
1764135446Strhodes    scp = sc->cur_scp;		/* cur_scp may have changed... */
1765135446Strhodes    if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1766135446Strhodes	scrn_update(scp, TRUE);
1767135446Strhodes
1768135446Strhodes#ifdef DEV_SPLASH
1769135446Strhodes    /* should we activate the screen saver? */
1770135446Strhodes    if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1771135446Strhodes	if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1772135446Strhodes	    (*current_saver)(sc, TRUE);
1773135446Strhodes#endif
1774135446Strhodes
1775135446Strhodes    if (again)
1776135446Strhodes	timeout(scrn_timer, sc, hz / 25);
1777135446Strhodes    splx(s);
1778135446Strhodes}
1779135446Strhodes
1780135446Strhodesstatic int
1781135446Strhodesand_region(int *s1, int *e1, int s2, int e2)
1782135446Strhodes{
1783135446Strhodes    if (*e1 < s2 || e2 < *s1)
1784153816Sdougb	return FALSE;
1785153816Sdougb    *s1 = imax(*s1, s2);
1786135446Strhodes    *e1 = imin(*e1, e2);
1787153816Sdougb    return TRUE;
1788135446Strhodes}
1789135446Strhodes
1790153816Sdougbstatic void
1791153816Sdougbscrn_update(scr_stat *scp, int show_cursor)
1792153816Sdougb{
1793153816Sdougb    int start;
1794153816Sdougb    int end;
1795135446Strhodes    int s;
1796135446Strhodes    int e;
1797135446Strhodes
1798135446Strhodes    /* assert(scp == scp->sc->cur_scp) */
1799135446Strhodes
1800135446Strhodes    SC_VIDEO_LOCK(scp->sc);
1801135446Strhodes
1802135446Strhodes#ifndef SC_NO_CUTPASTE
1803135446Strhodes    /* remove the previous mouse pointer image if necessary */
1804135446Strhodes    if (scp->status & MOUSE_VISIBLE) {
1805135446Strhodes	s = scp->mouse_pos;
1806135446Strhodes	e = scp->mouse_pos + scp->xsize + 1;
1807135446Strhodes	if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1808135446Strhodes	    || and_region(&s, &e, scp->start, scp->end)
1809135446Strhodes	    || ((scp->status & CURSOR_ENABLED) &&
1810135446Strhodes		(scp->cursor_pos != scp->cursor_oldpos) &&
1811135446Strhodes		(and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1812135446Strhodes		 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1813135446Strhodes	    sc_remove_mouse_image(scp);
1814135446Strhodes	    if (scp->end >= scp->xsize*scp->ysize)
1815135446Strhodes		scp->end = scp->xsize*scp->ysize - 1;
1816135446Strhodes	}
1817135446Strhodes    }
1818135446Strhodes#endif /* !SC_NO_CUTPASTE */
1819135446Strhodes
1820135446Strhodes#if 1
1821135446Strhodes    /* debug: XXX */
1822135446Strhodes    if (scp->end >= scp->xsize*scp->ysize) {
1823135446Strhodes	printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1824135446Strhodes	scp->end = scp->xsize*scp->ysize - 1;
1825135446Strhodes    }
1826135446Strhodes    if (scp->start < 0) {
1827135446Strhodes	printf("scrn_update(): scp->start %d < 0\n", scp->start);
1828135446Strhodes	scp->start = 0;
1829135446Strhodes    }
1830135446Strhodes#endif
1831135446Strhodes
1832135446Strhodes    /* update screen image */
1833135446Strhodes    if (scp->start <= scp->end)  {
1834135446Strhodes	if (scp->mouse_cut_end >= 0) {
1835135446Strhodes	    /* there is a marked region for cut & paste */
1836135446Strhodes	    if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1837135446Strhodes		start = scp->mouse_cut_start;
1838135446Strhodes		end = scp->mouse_cut_end;
1839135446Strhodes	    } else {
1840135446Strhodes		start = scp->mouse_cut_end;
1841135446Strhodes		end = scp->mouse_cut_start - 1;
1842135446Strhodes	    }
1843135446Strhodes	    s = start;
1844135446Strhodes	    e = end;
1845135446Strhodes	    /* does the cut-mark region overlap with the update region? */
1846135446Strhodes	    if (and_region(&s, &e, scp->start, scp->end)) {
1847135446Strhodes		(*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1848135446Strhodes		s = 0;
1849135446Strhodes		e = start - 1;
1850135446Strhodes		if (and_region(&s, &e, scp->start, scp->end))
1851135446Strhodes		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1852135446Strhodes		s = end + 1;
1853135446Strhodes		e = scp->xsize*scp->ysize - 1;
1854135446Strhodes		if (and_region(&s, &e, scp->start, scp->end))
1855135446Strhodes		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1856135446Strhodes	    } else {
1857135446Strhodes		(*scp->rndr->draw)(scp, scp->start,
1858135446Strhodes				   scp->end - scp->start + 1, FALSE);
1859135446Strhodes	    }
1860135446Strhodes	} else {
1861135446Strhodes	    (*scp->rndr->draw)(scp, scp->start,
1862135446Strhodes			       scp->end - scp->start + 1, FALSE);
1863135446Strhodes	}
1864135446Strhodes    }
1865135446Strhodes
1866135446Strhodes    /* we are not to show the cursor and the mouse pointer... */
1867135446Strhodes    if (!show_cursor) {
1868135446Strhodes        scp->end = 0;
1869135446Strhodes        scp->start = scp->xsize*scp->ysize - 1;
1870135446Strhodes	SC_VIDEO_UNLOCK(scp->sc);
1871135446Strhodes	return;
1872135446Strhodes    }
1873135446Strhodes
1874135446Strhodes    /* update cursor image */
1875135446Strhodes    if (scp->status & CURSOR_ENABLED) {
1876135446Strhodes	s = scp->start;
1877135446Strhodes	e = scp->end;
1878135446Strhodes        /* did cursor move since last time ? */
1879135446Strhodes        if (scp->cursor_pos != scp->cursor_oldpos) {
1880135446Strhodes            /* do we need to remove old cursor image ? */
1881135446Strhodes            if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1882135446Strhodes                sc_remove_cursor_image(scp);
1883135446Strhodes            sc_draw_cursor_image(scp);
1884135446Strhodes        } else {
1885135446Strhodes            if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1886135446Strhodes		/* cursor didn't move, but has been overwritten */
1887135446Strhodes		sc_draw_cursor_image(scp);
1888135446Strhodes	    else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
1889135446Strhodes		/* if it's a blinking cursor, update it */
1890135446Strhodes		(*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1891135446Strhodes					   sc_inside_cutmark(scp,
1892135446Strhodes					       scp->cursor_pos));
1893135446Strhodes        }
1894135446Strhodes    }
1895135446Strhodes
1896135446Strhodes#ifndef SC_NO_CUTPASTE
1897135446Strhodes    /* update "pseudo" mouse pointer image */
1898135446Strhodes    if (scp->sc->flags & SC_MOUSE_ENABLED) {
1899135446Strhodes	if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1900135446Strhodes	    scp->status &= ~MOUSE_MOVED;
1901135446Strhodes	    sc_draw_mouse_image(scp);
1902135446Strhodes	}
1903135446Strhodes    }
1904135446Strhodes#endif /* SC_NO_CUTPASTE */
1905135446Strhodes
1906135446Strhodes    scp->end = 0;
1907135446Strhodes    scp->start = scp->xsize*scp->ysize - 1;
1908135446Strhodes
1909135446Strhodes    SC_VIDEO_UNLOCK(scp->sc);
1910135446Strhodes}
1911135446Strhodes
1912135446Strhodes#ifdef DEV_SPLASH
1913135446Strhodesstatic int
1914135446Strhodesscsplash_callback(int event, void *arg)
1915135446Strhodes{
1916135446Strhodes    sc_softc_t *sc;
1917135446Strhodes    int error;
1918135446Strhodes
1919135446Strhodes    sc = (sc_softc_t *)arg;
1920135446Strhodes
1921135446Strhodes    switch (event) {
1922135446Strhodes    case SPLASH_INIT:
1923135446Strhodes	if (add_scrn_saver(scsplash_saver) == 0) {
1924135446Strhodes	    sc->flags &= ~SC_SAVER_FAILED;
1925135446Strhodes	    run_scrn_saver = TRUE;
1926135446Strhodes	    if (cold && !(boothowto & RB_VERBOSE)) {
1927135446Strhodes		scsplash_stick(TRUE);
1928135446Strhodes		(*current_saver)(sc, TRUE);
1929135446Strhodes	    }
1930135446Strhodes	}
1931135446Strhodes	return 0;
1932135446Strhodes
1933135446Strhodes    case SPLASH_TERM:
1934135446Strhodes	if (current_saver == scsplash_saver) {
1935135446Strhodes	    scsplash_stick(FALSE);
1936135446Strhodes	    error = remove_scrn_saver(scsplash_saver);
1937135446Strhodes	    if (error)
1938135446Strhodes		return error;
1939135446Strhodes	}
1940135446Strhodes	return 0;
1941135446Strhodes
1942135446Strhodes    default:
1943135446Strhodes	return EINVAL;
1944135446Strhodes    }
1945135446Strhodes}
1946135446Strhodes
1947135446Strhodesstatic void
1948135446Strhodesscsplash_saver(sc_softc_t *sc, int show)
1949135446Strhodes{
1950135446Strhodes    static int busy = FALSE;
1951135446Strhodes    scr_stat *scp;
1952135446Strhodes
1953135446Strhodes    if (busy)
1954135446Strhodes	return;
1955135446Strhodes    busy = TRUE;
1956135446Strhodes
1957135446Strhodes    scp = sc->cur_scp;
1958135446Strhodes    if (show) {
1959135446Strhodes	if (!(sc->flags & SC_SAVER_FAILED)) {
1960135446Strhodes	    if (!(sc->flags & SC_SCRN_BLANKED))
1961135446Strhodes		set_scrn_saver_mode(scp, -1, NULL, 0);
1962135446Strhodes	    switch (splash(sc->adp, TRUE)) {
1963135446Strhodes	    case 0:		/* succeeded */
1964135446Strhodes		break;
1965135446Strhodes	    case EAGAIN:	/* try later */
1966135446Strhodes		restore_scrn_saver_mode(scp, FALSE);
1967135446Strhodes		sc_touch_scrn_saver();		/* XXX */
1968135446Strhodes		break;
1969135446Strhodes	    default:
1970135446Strhodes		sc->flags |= SC_SAVER_FAILED;
1971135446Strhodes		scsplash_stick(FALSE);
1972135446Strhodes		restore_scrn_saver_mode(scp, TRUE);
1973135446Strhodes		printf("scsplash_saver(): failed to put up the image\n");
1974135446Strhodes		break;
1975135446Strhodes	    }
1976135446Strhodes	}
1977135446Strhodes    } else if (!sticky_splash) {
1978135446Strhodes	if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1979135446Strhodes	    restore_scrn_saver_mode(scp, TRUE);
1980135446Strhodes    }
1981135446Strhodes    busy = FALSE;
1982135446Strhodes}
1983135446Strhodes
1984135446Strhodesstatic int
1985135446Strhodesadd_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1986135446Strhodes{
1987135446Strhodes#if 0
1988135446Strhodes    int error;
1989135446Strhodes
1990135446Strhodes    if (current_saver != none_saver) {
1991135446Strhodes	error = remove_scrn_saver(current_saver);
1992135446Strhodes	if (error)
1993135446Strhodes	    return error;
1994135446Strhodes    }
1995135446Strhodes#endif
1996135446Strhodes    if (current_saver != none_saver)
1997135446Strhodes	return EBUSY;
1998135446Strhodes
1999135446Strhodes    run_scrn_saver = FALSE;
2000135446Strhodes    saver_mode = CONS_LKM_SAVER;
2001135446Strhodes    current_saver = this_saver;
2002135446Strhodes    return 0;
2003135446Strhodes}
2004135446Strhodes
2005135446Strhodesstatic int
2006135446Strhodesremove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2007135446Strhodes{
2008135446Strhodes    if (current_saver != this_saver)
2009135446Strhodes	return EINVAL;
2010135446Strhodes
2011135446Strhodes#if 0
2012135446Strhodes    /*
2013135446Strhodes     * In order to prevent `current_saver' from being called by
2014135446Strhodes     * the timeout routine `scrn_timer()' while we manipulate
2015135446Strhodes     * the saver list, we shall set `current_saver' to `none_saver'
2016135446Strhodes     * before stopping the current saver, rather than blocking by `splXX()'.
2017135446Strhodes     */
2018135446Strhodes    current_saver = none_saver;
2019135446Strhodes    if (scrn_blanked)
2020135446Strhodes        stop_scrn_saver(this_saver);
2021135446Strhodes#endif
2022135446Strhodes
2023135446Strhodes    /* unblank all blanked screens */
2024135446Strhodes    wait_scrn_saver_stop(NULL);
2025135446Strhodes    if (scrn_blanked)
2026135446Strhodes	return EBUSY;
2027135446Strhodes
2028135446Strhodes    current_saver = none_saver;
2029135446Strhodes    return 0;
2030135446Strhodes}
2031135446Strhodes
2032135446Strhodesstatic int
2033135446Strhodesset_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2034135446Strhodes{
2035135446Strhodes    int s;
2036135446Strhodes
2037135446Strhodes    /* assert(scp == scp->sc->cur_scp) */
2038135446Strhodes    s = spltty();
2039135446Strhodes    if (!ISGRAPHSC(scp))
2040135446Strhodes	sc_remove_cursor_image(scp);
2041135446Strhodes    scp->splash_save_mode = scp->mode;
2042135446Strhodes    scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2043135446Strhodes    scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2044135446Strhodes    scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2045135446Strhodes    scp->sc->flags |= SC_SCRN_BLANKED;
2046135446Strhodes    ++scrn_blanked;
2047135446Strhodes    splx(s);
2048135446Strhodes    if (mode < 0)
2049135446Strhodes	return 0;
2050135446Strhodes    scp->mode = mode;
2051135446Strhodes    if (set_mode(scp) == 0) {
2052135446Strhodes	if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2053135446Strhodes	    scp->status |= GRAPHICS_MODE;
2054135446Strhodes#ifndef SC_NO_PALETTE_LOADING
2055135446Strhodes	if (pal != NULL)
2056135446Strhodes	    load_palette(scp->sc->adp, pal);
2057135446Strhodes#endif
2058135446Strhodes	sc_set_border(scp, border);
2059135446Strhodes	return 0;
2060135446Strhodes    } else {
2061135446Strhodes	s = spltty();
2062135446Strhodes	scp->mode = scp->splash_save_mode;
2063135446Strhodes	scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2064135446Strhodes	scp->status |= scp->splash_save_status;
2065135446Strhodes	splx(s);
2066135446Strhodes	return 1;
2067135446Strhodes    }
2068135446Strhodes}
2069135446Strhodes
2070135446Strhodesstatic int
2071135446Strhodesrestore_scrn_saver_mode(scr_stat *scp, int changemode)
2072135446Strhodes{
2073135446Strhodes    int mode;
2074135446Strhodes    int status;
2075135446Strhodes    int s;
2076135446Strhodes
2077135446Strhodes    /* assert(scp == scp->sc->cur_scp) */
2078135446Strhodes    s = spltty();
2079135446Strhodes    mode = scp->mode;
2080135446Strhodes    status = scp->status;
2081135446Strhodes    scp->mode = scp->splash_save_mode;
2082135446Strhodes    scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2083135446Strhodes    scp->status |= scp->splash_save_status;
2084135446Strhodes    scp->sc->flags &= ~SC_SCRN_BLANKED;
2085135446Strhodes    if (!changemode) {
2086135446Strhodes	if (!ISGRAPHSC(scp))
2087135446Strhodes	    sc_draw_cursor_image(scp);
2088135446Strhodes	--scrn_blanked;
2089135446Strhodes	splx(s);
2090135446Strhodes	return 0;
2091135446Strhodes    }
2092135446Strhodes    if (set_mode(scp) == 0) {
2093135446Strhodes#ifndef SC_NO_PALETTE_LOADING
2094135446Strhodes	load_palette(scp->sc->adp, scp->sc->palette);
2095135446Strhodes#endif
2096135446Strhodes	--scrn_blanked;
2097135446Strhodes	splx(s);
2098135446Strhodes	return 0;
2099135446Strhodes    } else {
2100135446Strhodes	scp->mode = mode;
2101135446Strhodes	scp->status = status;
2102135446Strhodes	splx(s);
2103135446Strhodes	return 1;
2104135446Strhodes    }
2105135446Strhodes}
2106135446Strhodes
2107135446Strhodesstatic void
2108135446Strhodesstop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2109135446Strhodes{
2110135446Strhodes    (*saver)(sc, FALSE);
2111135446Strhodes    run_scrn_saver = FALSE;
2112135446Strhodes    /* the screen saver may have chosen not to stop after all... */
2113135446Strhodes    if (sc->flags & SC_SCRN_BLANKED)
2114135446Strhodes	return;
2115135446Strhodes
2116135446Strhodes    mark_all(sc->cur_scp);
2117135446Strhodes    if (sc->delayed_next_scr)
2118135446Strhodes	sc_switch_scr(sc, sc->delayed_next_scr - 1);
2119135446Strhodes    if (debugger == 0)
2120135446Strhodes	wakeup(&scrn_blanked);
2121135446Strhodes}
2122135446Strhodes
2123135446Strhodesstatic int
2124135446Strhodeswait_scrn_saver_stop(sc_softc_t *sc)
2125135446Strhodes{
2126135446Strhodes    int error = 0;
2127135446Strhodes
2128135446Strhodes    while (scrn_blanked > 0) {
2129135446Strhodes	run_scrn_saver = FALSE;
2130135446Strhodes	if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2131135446Strhodes	    error = 0;
2132135446Strhodes	    break;
2133135446Strhodes	}
2134135446Strhodes	error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2135135446Strhodes	if ((error != 0) && (error != ERESTART))
2136135446Strhodes	    break;
2137135446Strhodes    }
2138135446Strhodes    run_scrn_saver = FALSE;
2139135446Strhodes    return error;
2140135446Strhodes}
2141135446Strhodes#endif /* DEV_SPLASH */
2142135446Strhodes
2143void
2144sc_touch_scrn_saver(void)
2145{
2146    scsplash_stick(FALSE);
2147    run_scrn_saver = FALSE;
2148}
2149
2150int
2151sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2152{
2153    scr_stat *cur_scp;
2154    struct tty *tp;
2155    struct proc *p;
2156    int s;
2157
2158    DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2159
2160    if (sc->cur_scp == NULL)
2161	return (0);
2162
2163    /* prevent switch if previously requested */
2164    if (sc->flags & SC_SCRN_VTYLOCK) {
2165	    sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2166		sc->cur_scp->bell_duration);
2167	    return EPERM;
2168    }
2169
2170    /* delay switch if the screen is blanked or being updated */
2171    if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2172	|| sc->blink_in_progress) {
2173	sc->delayed_next_scr = next_scr + 1;
2174	sc_touch_scrn_saver();
2175	DPRINTF(5, ("switch delayed\n"));
2176	return 0;
2177    }
2178    sc->delayed_next_scr = 0;
2179
2180    s = spltty();
2181    cur_scp = sc->cur_scp;
2182
2183    /* we are in the middle of the vty switching process... */
2184    if (sc->switch_in_progress
2185	&& (cur_scp->smode.mode == VT_PROCESS)
2186	&& cur_scp->proc) {
2187	p = pfind(cur_scp->pid);
2188	if (cur_scp->proc != p) {
2189	    if (p)
2190		PROC_UNLOCK(p);
2191	    /*
2192	     * The controlling process has died!!.  Do some clean up.
2193	     * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2194	     * are not reset here yet; they will be cleared later.
2195	     */
2196	    DPRINTF(5, ("cur_scp controlling process %d died, ",
2197	       cur_scp->pid));
2198	    if (cur_scp->status & SWITCH_WAIT_REL) {
2199		/*
2200		 * Force the previous switch to finish, but return now
2201		 * with error.
2202		 */
2203		DPRINTF(5, ("reset WAIT_REL, "));
2204		finish_vt_rel(cur_scp, TRUE, &s);
2205		splx(s);
2206		DPRINTF(5, ("finishing previous switch\n"));
2207		return EINVAL;
2208	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2209		/* let's assume screen switch has been completed. */
2210		DPRINTF(5, ("reset WAIT_ACQ, "));
2211		finish_vt_acq(cur_scp);
2212	    } else {
2213		/*
2214	 	 * We are in between screen release and acquisition, and
2215		 * reached here via scgetc() or scrn_timer() which has
2216		 * interrupted exchange_scr(). Don't do anything stupid.
2217		 */
2218		DPRINTF(5, ("waiting nothing, "));
2219	    }
2220	} else {
2221	    if (p)
2222		PROC_UNLOCK(p);
2223	    /*
2224	     * The controlling process is alive, but not responding...
2225	     * It is either buggy or it may be just taking time.
2226	     * The following code is a gross kludge to cope with this
2227	     * problem for which there is no clean solution. XXX
2228	     */
2229	    if (cur_scp->status & SWITCH_WAIT_REL) {
2230		switch (sc->switch_in_progress++) {
2231		case 1:
2232		    break;
2233		case 2:
2234		    DPRINTF(5, ("sending relsig again, "));
2235		    signal_vt_rel(cur_scp);
2236		    break;
2237		case 3:
2238		    break;
2239		case 4:
2240		default:
2241		    /*
2242		     * Act as if the controlling program returned
2243		     * VT_FALSE.
2244		     */
2245		    DPRINTF(5, ("force reset WAIT_REL, "));
2246		    finish_vt_rel(cur_scp, FALSE, &s);
2247		    splx(s);
2248		    DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2249		    return EINVAL;
2250		}
2251	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2252		switch (sc->switch_in_progress++) {
2253		case 1:
2254		    break;
2255		case 2:
2256		    DPRINTF(5, ("sending acqsig again, "));
2257		    signal_vt_acq(cur_scp);
2258		    break;
2259		case 3:
2260		    break;
2261		case 4:
2262		default:
2263		     /* clear the flag and finish the previous switch */
2264		    DPRINTF(5, ("force reset WAIT_ACQ, "));
2265		    finish_vt_acq(cur_scp);
2266		    break;
2267		}
2268	    }
2269	}
2270    }
2271
2272    /*
2273     * Return error if an invalid argument is given, or vty switch
2274     * is still in progress.
2275     */
2276    if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2277	|| sc->switch_in_progress) {
2278	splx(s);
2279	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2280	DPRINTF(5, ("error 1\n"));
2281	return EINVAL;
2282    }
2283
2284    /*
2285     * Don't allow switching away from the graphics mode vty
2286     * if the switch mode is VT_AUTO, unless the next vty is the same
2287     * as the current or the current vty has been closed (but showing).
2288     */
2289    tp = VIRTUAL_TTY(sc, cur_scp->index);
2290    if ((cur_scp->index != next_scr)
2291	&& ISTTYOPEN(tp)
2292	&& (cur_scp->smode.mode == VT_AUTO)
2293	&& ISGRAPHSC(cur_scp)) {
2294	splx(s);
2295	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2296	DPRINTF(5, ("error, graphics mode\n"));
2297	return EINVAL;
2298    }
2299
2300    /*
2301     * Is the wanted vty open? Don't allow switching to a closed vty.
2302     * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2303     * Note that we always allow the user to switch to the kernel
2304     * console even if it is closed.
2305     */
2306    if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2307	tp = VIRTUAL_TTY(sc, next_scr);
2308	if (!ISTTYOPEN(tp)) {
2309	    splx(s);
2310	    sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2311	    DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2312	    return EINVAL;
2313	}
2314	if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2315	    splx(s);
2316	    DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2317	    return EINVAL;
2318	}
2319    }
2320
2321    /* this is the start of vty switching process... */
2322    ++sc->switch_in_progress;
2323    sc->old_scp = cur_scp;
2324    sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2325    if (sc->new_scp == sc->old_scp) {
2326	sc->switch_in_progress = 0;
2327	/*
2328	 * XXX wakeup() locks the scheduler lock which will hang if
2329	 * the lock is in an in-between state, e.g., when we stop at
2330	 * a breakpoint at fork_exit.  It has always been wrong to call
2331	 * wakeup() when the debugger is active.  In RELENG_4, wakeup()
2332	 * is supposed to be locked by splhigh(), but the debugger may
2333	 * be invoked at splhigh().
2334	 */
2335	if (debugger == 0)
2336	    wakeup(&sc->new_scp->smode);
2337	splx(s);
2338	DPRINTF(5, ("switch done (new == old)\n"));
2339	return 0;
2340    }
2341
2342    /* has controlling process died? */
2343    vt_proc_alive(sc->old_scp);
2344    vt_proc_alive(sc->new_scp);
2345
2346    /* wait for the controlling process to release the screen, if necessary */
2347    if (signal_vt_rel(sc->old_scp)) {
2348	splx(s);
2349	return 0;
2350    }
2351
2352    /* go set up the new vty screen */
2353    splx(s);
2354    exchange_scr(sc);
2355    s = spltty();
2356
2357    /* wake up processes waiting for this vty */
2358    if (debugger == 0)
2359	wakeup(&sc->cur_scp->smode);
2360
2361    /* wait for the controlling process to acknowledge, if necessary */
2362    if (signal_vt_acq(sc->cur_scp)) {
2363	splx(s);
2364	return 0;
2365    }
2366
2367    sc->switch_in_progress = 0;
2368    if (sc->unit == sc_console_unit)
2369	cnavailable(sc_consptr,  TRUE);
2370    splx(s);
2371    DPRINTF(5, ("switch done\n"));
2372
2373    return 0;
2374}
2375
2376static int
2377do_switch_scr(sc_softc_t *sc, int s)
2378{
2379    vt_proc_alive(sc->new_scp);
2380
2381    splx(s);
2382    exchange_scr(sc);
2383    s = spltty();
2384    /* sc->cur_scp == sc->new_scp */
2385    wakeup(&sc->cur_scp->smode);
2386
2387    /* wait for the controlling process to acknowledge, if necessary */
2388    if (!signal_vt_acq(sc->cur_scp)) {
2389	sc->switch_in_progress = 0;
2390	if (sc->unit == sc_console_unit)
2391	    cnavailable(sc_consptr,  TRUE);
2392    }
2393
2394    return s;
2395}
2396
2397static int
2398vt_proc_alive(scr_stat *scp)
2399{
2400    struct proc *p;
2401
2402    if (scp->proc) {
2403	if ((p = pfind(scp->pid)) != NULL)
2404	    PROC_UNLOCK(p);
2405	if (scp->proc == p)
2406	    return TRUE;
2407	scp->proc = NULL;
2408	scp->smode.mode = VT_AUTO;
2409	DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2410    }
2411    return FALSE;
2412}
2413
2414static int
2415signal_vt_rel(scr_stat *scp)
2416{
2417    if (scp->smode.mode != VT_PROCESS)
2418	return FALSE;
2419    scp->status |= SWITCH_WAIT_REL;
2420    PROC_LOCK(scp->proc);
2421    psignal(scp->proc, scp->smode.relsig);
2422    PROC_UNLOCK(scp->proc);
2423    DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2424    return TRUE;
2425}
2426
2427static int
2428signal_vt_acq(scr_stat *scp)
2429{
2430    if (scp->smode.mode != VT_PROCESS)
2431	return FALSE;
2432    if (scp->sc->unit == sc_console_unit)
2433	cnavailable(sc_consptr,  FALSE);
2434    scp->status |= SWITCH_WAIT_ACQ;
2435    PROC_LOCK(scp->proc);
2436    psignal(scp->proc, scp->smode.acqsig);
2437    PROC_UNLOCK(scp->proc);
2438    DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2439    return TRUE;
2440}
2441
2442static int
2443finish_vt_rel(scr_stat *scp, int release, int *s)
2444{
2445    if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2446	scp->status &= ~SWITCH_WAIT_REL;
2447	if (release)
2448	    *s = do_switch_scr(scp->sc, *s);
2449	else
2450	    scp->sc->switch_in_progress = 0;
2451	return 0;
2452    }
2453    return EINVAL;
2454}
2455
2456static int
2457finish_vt_acq(scr_stat *scp)
2458{
2459    if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2460	scp->status &= ~SWITCH_WAIT_ACQ;
2461	scp->sc->switch_in_progress = 0;
2462	return 0;
2463    }
2464    return EINVAL;
2465}
2466
2467static void
2468exchange_scr(sc_softc_t *sc)
2469{
2470    scr_stat *scp;
2471
2472    /* save the current state of video and keyboard */
2473    sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2474    if (!ISGRAPHSC(sc->old_scp))
2475	sc_remove_cursor_image(sc->old_scp);
2476    if (sc->old_scp->kbd_mode == K_XLATE)
2477	save_kbd_state(sc->old_scp);
2478
2479    /* set up the video for the new screen */
2480    scp = sc->cur_scp = sc->new_scp;
2481#ifdef PC98
2482    if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp))
2483#else
2484    if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2485#endif
2486	set_mode(scp);
2487#ifndef __sparc64__
2488    else
2489	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2490		    (void *)sc->adp->va_window, FALSE);
2491#endif
2492    scp->status |= MOUSE_HIDDEN;
2493    sc_move_cursor(scp, scp->xpos, scp->ypos);
2494    if (!ISGRAPHSC(scp))
2495	sc_set_cursor_image(scp);
2496#ifndef SC_NO_PALETTE_LOADING
2497    if (ISGRAPHSC(sc->old_scp))
2498	load_palette(sc->adp, sc->palette);
2499#endif
2500    sc_set_border(scp, scp->border);
2501
2502    /* set up the keyboard for the new screen */
2503    if (sc->old_scp->kbd_mode != scp->kbd_mode)
2504	kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2505    update_kbd_state(scp, scp->status, LOCK_MASK);
2506
2507    mark_all(scp);
2508}
2509
2510void
2511sc_puts(scr_stat *scp, u_char *buf, int len)
2512{
2513    int need_unlock = 0;
2514
2515#ifdef DEV_SPLASH
2516    /* make screensaver happy */
2517    if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2518	run_scrn_saver = FALSE;
2519#endif
2520
2521    if (scp->tsw) {
2522	if (!kdb_active && !mtx_owned(&scp->scr_lock)) {
2523		need_unlock = 1;
2524		mtx_lock_spin(&scp->scr_lock);
2525	}
2526	(*scp->tsw->te_puts)(scp, buf, len);
2527	if (need_unlock)
2528		mtx_unlock_spin(&scp->scr_lock);
2529    }
2530
2531    if (scp->sc->delayed_next_scr)
2532	sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2533}
2534
2535void
2536sc_draw_cursor_image(scr_stat *scp)
2537{
2538    /* assert(scp == scp->sc->cur_scp); */
2539    SC_VIDEO_LOCK(scp->sc);
2540    (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2541			      scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2542			      sc_inside_cutmark(scp, scp->cursor_pos));
2543    scp->cursor_oldpos = scp->cursor_pos;
2544    SC_VIDEO_UNLOCK(scp->sc);
2545}
2546
2547void
2548sc_remove_cursor_image(scr_stat *scp)
2549{
2550    /* assert(scp == scp->sc->cur_scp); */
2551    SC_VIDEO_LOCK(scp->sc);
2552    (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2553			      scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2554			      sc_inside_cutmark(scp, scp->cursor_oldpos));
2555    SC_VIDEO_UNLOCK(scp->sc);
2556}
2557
2558static void
2559update_cursor_image(scr_stat *scp)
2560{
2561    /* assert(scp == scp->sc->cur_scp); */
2562    sc_remove_cursor_image(scp);
2563    sc_set_cursor_image(scp);
2564    sc_draw_cursor_image(scp);
2565}
2566
2567void
2568sc_set_cursor_image(scr_stat *scp)
2569{
2570    scp->curs_attr.flags = scp->curr_curs_attr.flags;
2571    if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2572	/* hidden cursor is internally represented as zero-height underline */
2573	scp->curs_attr.flags = CONS_CHAR_CURSOR;
2574	scp->curs_attr.base = scp->curs_attr.height = 0;
2575    } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2576	scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2577				  scp->font_size - 1);
2578	scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2579				    scp->font_size - scp->curs_attr.base);
2580    } else {	/* block cursor */
2581	scp->curs_attr.base = 0;
2582	scp->curs_attr.height = scp->font_size;
2583    }
2584
2585    /* assert(scp == scp->sc->cur_scp); */
2586    SC_VIDEO_LOCK(scp->sc);
2587    (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2588			     scp->curs_attr.flags & CONS_BLINK_CURSOR);
2589    SC_VIDEO_UNLOCK(scp->sc);
2590}
2591
2592static void
2593change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2594{
2595    if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2596	sc_remove_cursor_image(scp);
2597
2598    if (base >= 0)
2599	scp->curr_curs_attr.base = base;
2600    if (height >= 0)
2601	scp->curr_curs_attr.height = height;
2602    if (flags & CONS_RESET_CURSOR)
2603	scp->curr_curs_attr = scp->dflt_curs_attr;
2604    else
2605	scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2606
2607    if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2608	sc_set_cursor_image(scp);
2609	sc_draw_cursor_image(scp);
2610    }
2611}
2612
2613void
2614sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2615{
2616    sc_softc_t *sc;
2617    struct cdev *dev;
2618    int s;
2619    int i;
2620
2621    s = spltty();
2622    if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2623	/* local (per vty) change */
2624	change_cursor_shape(scp, flags, base, height);
2625	splx(s);
2626	return;
2627    }
2628
2629    /* global change */
2630    sc = scp->sc;
2631    if (base >= 0)
2632	sc->curs_attr.base = base;
2633    if (height >= 0)
2634	sc->curs_attr.height = height;
2635    if (flags != -1) {
2636	if (flags & CONS_RESET_CURSOR)
2637	    sc->curs_attr = sc->dflt_curs_attr;
2638	else
2639	    sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2640    }
2641
2642    for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2643	if ((dev = SC_DEV(sc, i)) == NULL)
2644	    continue;
2645	if ((scp = sc_get_stat(dev)) == NULL)
2646	    continue;
2647	scp->dflt_curs_attr = sc->curs_attr;
2648	change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2649    }
2650    splx(s);
2651}
2652
2653static void
2654scinit(int unit, int flags)
2655{
2656
2657    /*
2658     * When syscons is being initialized as the kernel console, malloc()
2659     * is not yet functional, because various kernel structures has not been
2660     * fully initialized yet.  Therefore, we need to declare the following
2661     * static buffers for the console.  This is less than ideal,
2662     * but is necessry evil for the time being.  XXX
2663     */
2664#ifdef PC98
2665    static u_short sc_buffer[ROW*COL*2];/* XXX */
2666#else
2667    static u_short sc_buffer[ROW*COL];	/* XXX */
2668#endif
2669#ifndef SC_NO_FONT_LOADING
2670    static u_char font_8[256*8];
2671    static u_char font_14[256*14];
2672    static u_char font_16[256*16];
2673#endif
2674
2675    sc_softc_t *sc;
2676    scr_stat *scp;
2677    video_adapter_t *adp;
2678    int col;
2679    int row;
2680    int i;
2681
2682    /* one time initialization */
2683    if (init_done == COLD)
2684	sc_get_bios_values(&bios_value);
2685    init_done = WARM;
2686
2687    /*
2688     * Allocate resources.  Even if we are being called for the second
2689     * time, we must allocate them again, because they might have
2690     * disappeared...
2691     */
2692    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2693    if ((sc->flags & SC_INIT_DONE) == 0)
2694	SC_VIDEO_LOCKINIT(sc);
2695
2696    adp = NULL;
2697    if (sc->adapter >= 0) {
2698	vid_release(sc->adp, (void *)&sc->adapter);
2699	adp = sc->adp;
2700	sc->adp = NULL;
2701    }
2702    if (sc->keyboard >= 0) {
2703	DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2704	i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2705	DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2706	if (sc->kbd != NULL) {
2707	    DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2708		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2709	}
2710	sc->kbd = NULL;
2711    }
2712    sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2713    sc->adp = vid_get_adapter(sc->adapter);
2714    /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2715
2716    sc->keyboard = sc_allocate_keyboard(sc, unit);
2717    DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2718
2719    sc->kbd = kbd_get_keyboard(sc->keyboard);
2720    if (sc->kbd != NULL) {
2721	DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2722		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2723    }
2724
2725    if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2726
2727	sc->initial_mode = sc->adp->va_initial_mode;
2728
2729#ifndef SC_NO_FONT_LOADING
2730	if (flags & SC_KERNEL_CONSOLE) {
2731	    sc->font_8 = font_8;
2732	    sc->font_14 = font_14;
2733	    sc->font_16 = font_16;
2734	} else if (sc->font_8 == NULL) {
2735	    /* assert(sc_malloc) */
2736	    sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2737	    sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2738	    sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2739	}
2740#endif
2741
2742	/* extract the hardware cursor location and hide the cursor for now */
2743	(*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2744	(*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2745
2746	/* set up the first console */
2747	sc->first_vty = unit*MAXCONS;
2748	sc->vtys = MAXCONS;		/* XXX: should be configurable */
2749	if (flags & SC_KERNEL_CONSOLE) {
2750	    /*
2751	     * Set up devs structure but don't use it yet, calling make_dev()
2752	     * might panic kernel.  Wait for sc_attach_unit() to actually
2753	     * create the devices.
2754	     */
2755	    sc->dev = main_devs;
2756	    scp = &main_console;
2757	    init_scp(sc, sc->first_vty, scp);
2758	    sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2759			(void *)sc_buffer, FALSE);
2760	    if (sc_init_emulator(scp, SC_DFLT_TERM))
2761		sc_init_emulator(scp, "*");
2762	    (*scp->tsw->te_default_attr)(scp,
2763					 kernel_default.std_color,
2764					 kernel_default.rev_color);
2765	} else {
2766	    /* assert(sc_malloc) */
2767	    sc->dev = malloc(sizeof(struct cdev *)*sc->vtys, M_DEVBUF, M_WAITOK|M_ZERO);
2768	    sc->dev[0] = make_dev(&sc_cdevsw, unit * MAXCONS,
2769	        UID_ROOT, GID_WHEEL, 0600, "ttyv%r", unit * MAXCONS);
2770	    sc_alloc_tty(sc->dev[0]);
2771	    scp = alloc_scp(sc, sc->first_vty);
2772	    SC_STAT(sc->dev[0]) = scp;
2773	}
2774	sc->cur_scp = scp;
2775
2776#ifndef __sparc64__
2777	/* copy screen to temporary buffer */
2778	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2779		    (void *)scp->sc->adp->va_window, FALSE);
2780	if (ISTEXTSC(scp))
2781	    sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2782#endif
2783
2784	/* move cursors to the initial positions */
2785	if (col >= scp->xsize)
2786	    col = 0;
2787	if (row >= scp->ysize)
2788	    row = scp->ysize - 1;
2789	scp->xpos = col;
2790	scp->ypos = row;
2791	scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2792
2793	if (bios_value.cursor_end < scp->font_size)
2794	    sc->dflt_curs_attr.base = scp->font_size -
2795					  bios_value.cursor_end - 1;
2796	else
2797	    sc->dflt_curs_attr.base = 0;
2798	i = bios_value.cursor_end - bios_value.cursor_start + 1;
2799	sc->dflt_curs_attr.height = imin(i, scp->font_size);
2800	sc->dflt_curs_attr.flags = 0;
2801	sc->curs_attr = sc->dflt_curs_attr;
2802	scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2803
2804#ifndef SC_NO_SYSMOUSE
2805	sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2806#endif
2807	if (!ISGRAPHSC(scp)) {
2808    	    sc_set_cursor_image(scp);
2809    	    sc_draw_cursor_image(scp);
2810	}
2811
2812	/* save font and palette */
2813#ifndef SC_NO_FONT_LOADING
2814	sc->fonts_loaded = 0;
2815	if (ISFONTAVAIL(sc->adp->va_flags)) {
2816#ifdef SC_DFLT_FONT
2817	    bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2818	    bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2819	    bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2820	    sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2821	    if (scp->font_size < 14) {
2822		sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2823	    } else if (scp->font_size >= 16) {
2824		sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2825	    } else {
2826		sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2827	    }
2828#else /* !SC_DFLT_FONT */
2829	    if (scp->font_size < 14) {
2830		sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2831		sc->fonts_loaded = FONT_8;
2832	    } else if (scp->font_size >= 16) {
2833		sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2834		sc->fonts_loaded = FONT_16;
2835	    } else {
2836		sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2837		sc->fonts_loaded = FONT_14;
2838	    }
2839#endif /* SC_DFLT_FONT */
2840	    /* FONT KLUDGE: always use the font page #0. XXX */
2841	    sc_show_font(scp, 0);
2842	}
2843#endif /* !SC_NO_FONT_LOADING */
2844
2845#ifndef SC_NO_PALETTE_LOADING
2846	save_palette(sc->adp, sc->palette);
2847#endif
2848
2849#ifdef DEV_SPLASH
2850	if (!(sc->flags & SC_SPLASH_SCRN)) {
2851	    /* we are ready to put up the splash image! */
2852	    splash_init(sc->adp, scsplash_callback, sc);
2853	    sc->flags |= SC_SPLASH_SCRN;
2854	}
2855#endif
2856    }
2857
2858    /* the rest is not necessary, if we have done it once */
2859    if (sc->flags & SC_INIT_DONE)
2860	return;
2861
2862    /* initialize mapscrn arrays to a one to one map */
2863    for (i = 0; i < sizeof(sc->scr_map); i++)
2864	sc->scr_map[i] = sc->scr_rmap[i] = i;
2865#ifdef PC98
2866    sc->scr_map[0x5c] = (u_char)0xfc;	/* for backslash */
2867#endif
2868
2869    sc->flags |= SC_INIT_DONE;
2870}
2871
2872static void
2873scterm(int unit, int flags)
2874{
2875    sc_softc_t *sc;
2876    scr_stat *scp;
2877
2878    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2879    if (sc == NULL)
2880	return;			/* shouldn't happen */
2881
2882#ifdef DEV_SPLASH
2883    /* this console is no longer available for the splash screen */
2884    if (sc->flags & SC_SPLASH_SCRN) {
2885	splash_term(sc->adp);
2886	sc->flags &= ~SC_SPLASH_SCRN;
2887    }
2888#endif
2889
2890#if 0 /* XXX */
2891    /* move the hardware cursor to the upper-left corner */
2892    (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
2893#endif
2894
2895    /* release the keyboard and the video card */
2896    if (sc->keyboard >= 0)
2897	kbd_release(sc->kbd, &sc->keyboard);
2898    if (sc->adapter >= 0)
2899	vid_release(sc->adp, &sc->adapter);
2900
2901    /* stop the terminal emulator, if any */
2902    scp = sc_get_stat(sc->dev[0]);
2903    if (scp->tsw)
2904	(*scp->tsw->te_term)(scp, &scp->ts);
2905    if (scp->ts != NULL)
2906	free(scp->ts, M_DEVBUF);
2907    mtx_destroy(&scp->scr_lock);
2908
2909    /* clear the structure */
2910    if (!(flags & SC_KERNEL_CONSOLE)) {
2911	/* XXX: We need delete_dev() for this */
2912	free(sc->dev, M_DEVBUF);
2913#if 0
2914	/* XXX: We need a ttyunregister for this */
2915	free(sc->tty, M_DEVBUF);
2916#endif
2917#ifndef SC_NO_FONT_LOADING
2918	free(sc->font_8, M_DEVBUF);
2919	free(sc->font_14, M_DEVBUF);
2920	free(sc->font_16, M_DEVBUF);
2921#endif
2922	/* XXX vtb, history */
2923    }
2924    bzero(sc, sizeof(*sc));
2925    sc->keyboard = -1;
2926    sc->adapter = -1;
2927}
2928
2929static void
2930scshutdown(void *arg, int howto)
2931{
2932    /* assert(sc_console != NULL) */
2933
2934    sc_touch_scrn_saver();
2935    if (!cold && sc_console
2936	&& sc_console->sc->cur_scp->smode.mode == VT_AUTO
2937	&& sc_console->smode.mode == VT_AUTO)
2938	sc_switch_scr(sc_console->sc, sc_console->index);
2939    shutdown_in_progress = TRUE;
2940}
2941
2942int
2943sc_clean_up(scr_stat *scp)
2944{
2945#ifdef DEV_SPLASH
2946    int error;
2947#endif
2948
2949    if (scp->sc->flags & SC_SCRN_BLANKED) {
2950	sc_touch_scrn_saver();
2951#ifdef DEV_SPLASH
2952	if ((error = wait_scrn_saver_stop(scp->sc)))
2953	    return error;
2954#endif
2955    }
2956    scp->status |= MOUSE_HIDDEN;
2957    sc_remove_mouse_image(scp);
2958    sc_remove_cutmarking(scp);
2959    return 0;
2960}
2961
2962void
2963sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
2964{
2965    sc_vtb_t new;
2966    sc_vtb_t old;
2967
2968    old = scp->vtb;
2969    sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
2970    if (!discard && (old.vtb_flags & VTB_VALID)) {
2971	/* retain the current cursor position and buffer contants */
2972	scp->cursor_oldpos = scp->cursor_pos;
2973	/*
2974	 * This works only if the old buffer has the same size as or larger
2975	 * than the new one. XXX
2976	 */
2977	sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
2978	scp->vtb = new;
2979    } else {
2980	scp->vtb = new;
2981	sc_vtb_destroy(&old);
2982    }
2983
2984#ifndef SC_NO_SYSMOUSE
2985    /* move the mouse cursor at the center of the screen */
2986    sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
2987#endif
2988}
2989
2990static scr_stat
2991*alloc_scp(sc_softc_t *sc, int vty)
2992{
2993    scr_stat *scp;
2994
2995    /* assert(sc_malloc) */
2996
2997    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2998    init_scp(sc, vty, scp);
2999
3000    sc_alloc_scr_buffer(scp, TRUE, TRUE);
3001    if (sc_init_emulator(scp, SC_DFLT_TERM))
3002	sc_init_emulator(scp, "*");
3003
3004#ifndef SC_NO_CUTPASTE
3005    sc_alloc_cut_buffer(scp, TRUE);
3006#endif
3007
3008#ifndef SC_NO_HISTORY
3009    sc_alloc_history_buffer(scp, 0, 0, TRUE);
3010#endif
3011
3012    return scp;
3013}
3014
3015static void
3016init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3017{
3018    video_info_t info;
3019
3020    bzero(scp, sizeof(*scp));
3021
3022    scp->index = vty;
3023    scp->sc = sc;
3024    scp->status = 0;
3025    scp->mode = sc->initial_mode;
3026    (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
3027    if (info.vi_flags & V_INFO_GRAPHICS) {
3028	scp->status |= GRAPHICS_MODE;
3029	scp->xpixel = info.vi_width;
3030	scp->ypixel = info.vi_height;
3031	scp->xsize = info.vi_width/info.vi_cwidth;
3032	scp->ysize = info.vi_height/info.vi_cheight;
3033	scp->font_size = 0;
3034	scp->font = NULL;
3035    } else {
3036	scp->xsize = info.vi_width;
3037	scp->ysize = info.vi_height;
3038	scp->xpixel = scp->xsize*info.vi_cwidth;
3039	scp->ypixel = scp->ysize*info.vi_cheight;
3040	scp->font_size = info.vi_cheight;
3041	scp->font_width = info.vi_cwidth;
3042	if (info.vi_cheight < 14) {
3043#ifndef SC_NO_FONT_LOADING
3044	    scp->font = sc->font_8;
3045#else
3046	    scp->font = NULL;
3047#endif
3048	} else if (info.vi_cheight >= 16) {
3049#ifndef SC_NO_FONT_LOADING
3050	    scp->font = sc->font_16;
3051#else
3052	    scp->font = NULL;
3053#endif
3054	} else {
3055#ifndef SC_NO_FONT_LOADING
3056	    scp->font = sc->font_14;
3057#else
3058	    scp->font = NULL;
3059#endif
3060	}
3061    }
3062    sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3063#ifndef __sparc64__
3064    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3065#endif
3066    scp->xoff = scp->yoff = 0;
3067    scp->xpos = scp->ypos = 0;
3068    scp->start = scp->xsize * scp->ysize - 1;
3069    scp->end = 0;
3070    scp->tsw = NULL;
3071    scp->ts = NULL;
3072    scp->rndr = NULL;
3073    scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3074    scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3075    scp->mouse_cut_start = scp->xsize*scp->ysize;
3076    scp->mouse_cut_end = -1;
3077    scp->mouse_signal = 0;
3078    scp->mouse_pid = 0;
3079    scp->mouse_proc = NULL;
3080    scp->kbd_mode = K_XLATE;
3081    scp->bell_pitch = bios_value.bell_pitch;
3082    scp->bell_duration = BELL_DURATION;
3083    scp->status |= (bios_value.shift_state & NLKED);
3084    scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3085    scp->pid = 0;
3086    scp->proc = NULL;
3087    scp->smode.mode = VT_AUTO;
3088    scp->history = NULL;
3089    scp->history_pos = 0;
3090    scp->history_size = 0;
3091
3092    mtx_init(&scp->scr_lock, "scrlock", NULL, MTX_SPIN);
3093}
3094
3095int
3096sc_init_emulator(scr_stat *scp, char *name)
3097{
3098    sc_term_sw_t *sw;
3099    sc_rndr_sw_t *rndr;
3100    void *p;
3101    int error;
3102
3103    if (name == NULL)	/* if no name is given, use the current emulator */
3104	sw = scp->tsw;
3105    else		/* ...otherwise find the named emulator */
3106	sw = sc_term_match(name);
3107    if (sw == NULL)
3108	return EINVAL;
3109
3110    rndr = NULL;
3111    if (strcmp(sw->te_renderer, "*") != 0) {
3112	rndr = sc_render_match(scp, sw->te_renderer,
3113			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3114    }
3115    if (rndr == NULL) {
3116	rndr = sc_render_match(scp, scp->sc->adp->va_name,
3117			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3118	if (rndr == NULL)
3119	    return ENODEV;
3120    }
3121
3122    if (sw == scp->tsw) {
3123	error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3124	scp->rndr = rndr;
3125	scp->rndr->init(scp);
3126	sc_clear_screen(scp);
3127	/* assert(error == 0); */
3128	return error;
3129    }
3130
3131    if (sc_malloc && (sw->te_size > 0))
3132	p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3133    else
3134	p = NULL;
3135    error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3136    if (error)
3137	return error;
3138
3139    if (scp->tsw)
3140	(*scp->tsw->te_term)(scp, &scp->ts);
3141    if (scp->ts != NULL)
3142	free(scp->ts, M_DEVBUF);
3143    scp->tsw = sw;
3144    scp->ts = p;
3145    scp->rndr = rndr;
3146    scp->rndr->init(scp);
3147
3148    /* XXX */
3149    (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3150    sc_clear_screen(scp);
3151
3152    return 0;
3153}
3154
3155/*
3156 * scgetc(flags) - get character from keyboard.
3157 * If flags & SCGETC_CN, then avoid harmful side effects.
3158 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3159 * return NOKEY if there is nothing there.
3160 */
3161static u_int
3162scgetc(sc_softc_t *sc, u_int flags)
3163{
3164    scr_stat *scp;
3165#ifndef SC_NO_HISTORY
3166    struct tty *tp;
3167#endif
3168    u_int c;
3169    int this_scr;
3170    int f;
3171    int i;
3172
3173    if (sc->kbd == NULL)
3174	return NOKEY;
3175
3176next_code:
3177#if 1
3178    /* I don't like this, but... XXX */
3179    if (flags & SCGETC_CN)
3180	sccnupdate(sc->cur_scp);
3181#endif
3182    scp = sc->cur_scp;
3183    /* first see if there is something in the keyboard port */
3184    for (;;) {
3185	c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3186	if (c == ERRKEY) {
3187	    if (!(flags & SCGETC_CN))
3188		sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3189	} else if (c == NOKEY)
3190	    return c;
3191	else
3192	    break;
3193    }
3194
3195    /* make screensaver happy */
3196    if (!(c & RELKEY))
3197	sc_touch_scrn_saver();
3198
3199    if (!(flags & SCGETC_CN))
3200	random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD);
3201
3202    if (scp->kbd_mode != K_XLATE)
3203	return KEYCHAR(c);
3204
3205    /* if scroll-lock pressed allow history browsing */
3206    if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3207
3208	scp->status &= ~CURSOR_ENABLED;
3209	sc_remove_cursor_image(scp);
3210
3211#ifndef SC_NO_HISTORY
3212	if (!(scp->status & BUFFER_SAVED)) {
3213	    scp->status |= BUFFER_SAVED;
3214	    sc_hist_save(scp);
3215	}
3216	switch (c) {
3217	/* FIXME: key codes */
3218	case SPCLKEY | FKEY | F(49):  /* home key */
3219	    sc_remove_cutmarking(scp);
3220	    sc_hist_home(scp);
3221	    goto next_code;
3222
3223	case SPCLKEY | FKEY | F(57):  /* end key */
3224	    sc_remove_cutmarking(scp);
3225	    sc_hist_end(scp);
3226	    goto next_code;
3227
3228	case SPCLKEY | FKEY | F(50):  /* up arrow key */
3229	    sc_remove_cutmarking(scp);
3230	    if (sc_hist_up_line(scp))
3231		if (!(flags & SCGETC_CN))
3232		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3233	    goto next_code;
3234
3235	case SPCLKEY | FKEY | F(58):  /* down arrow key */
3236	    sc_remove_cutmarking(scp);
3237	    if (sc_hist_down_line(scp))
3238		if (!(flags & SCGETC_CN))
3239		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3240	    goto next_code;
3241
3242	case SPCLKEY | FKEY | F(51):  /* page up key */
3243	    sc_remove_cutmarking(scp);
3244	    for (i=0; i<scp->ysize; i++)
3245	    if (sc_hist_up_line(scp)) {
3246		if (!(flags & SCGETC_CN))
3247		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3248		break;
3249	    }
3250	    goto next_code;
3251
3252	case SPCLKEY | FKEY | F(59):  /* page down key */
3253	    sc_remove_cutmarking(scp);
3254	    for (i=0; i<scp->ysize; i++)
3255	    if (sc_hist_down_line(scp)) {
3256		if (!(flags & SCGETC_CN))
3257		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3258		break;
3259	    }
3260	    goto next_code;
3261	}
3262#endif /* SC_NO_HISTORY */
3263    }
3264
3265    /*
3266     * Process and consume special keys here.  Return a plain char code
3267     * or a char code with the META flag or a function key code.
3268     */
3269    if (c & RELKEY) {
3270	/* key released */
3271	/* goto next_code */
3272    } else {
3273	/* key pressed */
3274	if (c & SPCLKEY) {
3275	    c &= ~SPCLKEY;
3276	    switch (KEYCHAR(c)) {
3277	    /* LOCKING KEYS */
3278	    case NLK: case CLK: case ALK:
3279		break;
3280	    case SLK:
3281		kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3282		if (f & SLKED) {
3283		    scp->status |= SLKED;
3284		} else {
3285		    if (scp->status & SLKED) {
3286			scp->status &= ~SLKED;
3287#ifndef SC_NO_HISTORY
3288			if (scp->status & BUFFER_SAVED) {
3289			    if (!sc_hist_restore(scp))
3290				sc_remove_cutmarking(scp);
3291			    scp->status &= ~BUFFER_SAVED;
3292			    scp->status |= CURSOR_ENABLED;
3293			    sc_draw_cursor_image(scp);
3294			}
3295			tp = VIRTUAL_TTY(sc, scp->index);
3296			if (ISTTYOPEN(tp))
3297			    scstart(tp);
3298#endif
3299		    }
3300		}
3301		break;
3302
3303	    case PASTE:
3304#ifndef SC_NO_CUTPASTE
3305		sc_mouse_paste(scp);
3306#endif
3307		break;
3308
3309	    /* NON-LOCKING KEYS */
3310	    case NOP:
3311	    case LSH:  case RSH:  case LCTR: case RCTR:
3312	    case LALT: case RALT: case ASH:  case META:
3313		break;
3314
3315	    case BTAB:
3316		if (!(sc->flags & SC_SCRN_BLANKED))
3317		    return c;
3318		break;
3319
3320	    case SPSC:
3321#ifdef DEV_SPLASH
3322		/* force activatation/deactivation of the screen saver */
3323		if (!(sc->flags & SC_SCRN_BLANKED)) {
3324		    run_scrn_saver = TRUE;
3325		    sc->scrn_time_stamp -= scrn_blank_time;
3326		}
3327		if (cold) {
3328		    /*
3329		     * While devices are being probed, the screen saver need
3330		     * to be invoked explictly. XXX
3331		     */
3332		    if (sc->flags & SC_SCRN_BLANKED) {
3333			scsplash_stick(FALSE);
3334			stop_scrn_saver(sc, current_saver);
3335		    } else {
3336			if (!ISGRAPHSC(scp)) {
3337			    scsplash_stick(TRUE);
3338			    (*current_saver)(sc, TRUE);
3339			}
3340		    }
3341		}
3342#endif /* DEV_SPLASH */
3343		break;
3344
3345	    case RBT:
3346#ifndef SC_DISABLE_REBOOT
3347		if (enable_reboot)
3348			shutdown_nice(0);
3349#endif
3350		break;
3351
3352	    case HALT:
3353#ifndef SC_DISABLE_REBOOT
3354		if (enable_reboot)
3355			shutdown_nice(RB_HALT);
3356#endif
3357		break;
3358
3359	    case PDWN:
3360#ifndef SC_DISABLE_REBOOT
3361		if (enable_reboot)
3362			shutdown_nice(RB_HALT|RB_POWEROFF);
3363#endif
3364		break;
3365
3366	    case SUSP:
3367		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3368		break;
3369	    case STBY:
3370		power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3371		break;
3372
3373	    case DBG:
3374#ifndef SC_DISABLE_KDBKEY
3375		if (enable_kdbkey)
3376			kdb_enter("manual escape to debugger");
3377#endif
3378		break;
3379
3380	    case PNC:
3381		if (enable_panic_key)
3382			panic("Forced by the panic key");
3383		break;
3384
3385	    case NEXT:
3386		this_scr = scp->index;
3387		for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3388			sc->first_vty + i != this_scr;
3389			i = (i + 1)%sc->vtys) {
3390		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3391		    if (ISTTYOPEN(tp)) {
3392			sc_switch_scr(scp->sc, sc->first_vty + i);
3393			break;
3394		    }
3395		}
3396		break;
3397
3398	    case PREV:
3399		this_scr = scp->index;
3400		for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3401			sc->first_vty + i != this_scr;
3402			i = (i + sc->vtys - 1)%sc->vtys) {
3403		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3404		    if (ISTTYOPEN(tp)) {
3405			sc_switch_scr(scp->sc, sc->first_vty + i);
3406			break;
3407		    }
3408		}
3409		break;
3410
3411	    default:
3412		if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3413		    sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3414		    break;
3415		}
3416		/* assert(c & FKEY) */
3417		if (!(sc->flags & SC_SCRN_BLANKED))
3418		    return c;
3419		break;
3420	    }
3421	    /* goto next_code */
3422	} else {
3423	    /* regular keys (maybe MKEY is set) */
3424	    if (!(sc->flags & SC_SCRN_BLANKED))
3425		return c;
3426	}
3427    }
3428
3429    goto next_code;
3430}
3431
3432static int
3433scmmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
3434{
3435    scr_stat *scp;
3436
3437    scp = sc_get_stat(dev);
3438    if (scp != scp->sc->cur_scp)
3439	return -1;
3440    return (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, offset, paddr, nprot);
3441}
3442
3443static int
3444save_kbd_state(scr_stat *scp)
3445{
3446    int state;
3447    int error;
3448
3449    error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3450    if (error == ENOIOCTL)
3451	error = ENODEV;
3452    if (error == 0) {
3453	scp->status &= ~LOCK_MASK;
3454	scp->status |= state;
3455    }
3456    return error;
3457}
3458
3459static int
3460update_kbd_state(scr_stat *scp, int new_bits, int mask)
3461{
3462    int state;
3463    int error;
3464
3465    if (mask != LOCK_MASK) {
3466	error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3467	if (error == ENOIOCTL)
3468	    error = ENODEV;
3469	if (error)
3470	    return error;
3471	state &= ~mask;
3472	state |= new_bits & mask;
3473    } else {
3474	state = new_bits & LOCK_MASK;
3475    }
3476    error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3477    if (error == ENOIOCTL)
3478	error = ENODEV;
3479    return error;
3480}
3481
3482static int
3483update_kbd_leds(scr_stat *scp, int which)
3484{
3485    int error;
3486
3487    which &= LOCK_MASK;
3488    error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3489    if (error == ENOIOCTL)
3490	error = ENODEV;
3491    return error;
3492}
3493
3494int
3495set_mode(scr_stat *scp)
3496{
3497    video_info_t info;
3498
3499    /* reject unsupported mode */
3500    if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
3501	return 1;
3502
3503    /* if this vty is not currently showing, do nothing */
3504    if (scp != scp->sc->cur_scp)
3505	return 0;
3506
3507    /* setup video hardware for the given mode */
3508    (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3509    scp->rndr->init(scp);
3510#ifndef __sparc64__
3511    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3512		(void *)scp->sc->adp->va_window, FALSE);
3513#endif
3514
3515#ifndef SC_NO_FONT_LOADING
3516    /* load appropriate font */
3517    if (!(scp->status & GRAPHICS_MODE)) {
3518	if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3519	    if (scp->font_size < 14) {
3520		if (scp->sc->fonts_loaded & FONT_8)
3521		    sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3522	    } else if (scp->font_size >= 16) {
3523		if (scp->sc->fonts_loaded & FONT_16)
3524		    sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3525	    } else {
3526		if (scp->sc->fonts_loaded & FONT_14)
3527		    sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3528	    }
3529	    /*
3530	     * FONT KLUDGE:
3531	     * This is an interim kludge to display correct font.
3532	     * Always use the font page #0 on the video plane 2.
3533	     * Somehow we cannot show the font in other font pages on
3534	     * some video cards... XXX
3535	     */
3536	    sc_show_font(scp, 0);
3537	}
3538	mark_all(scp);
3539    }
3540#endif /* !SC_NO_FONT_LOADING */
3541
3542    sc_set_border(scp, scp->border);
3543    sc_set_cursor_image(scp);
3544
3545    return 0;
3546}
3547
3548void
3549sc_set_border(scr_stat *scp, int color)
3550{
3551    SC_VIDEO_LOCK(scp->sc);
3552    (*scp->rndr->draw_border)(scp, color);
3553    SC_VIDEO_UNLOCK(scp->sc);
3554}
3555
3556#ifndef SC_NO_FONT_LOADING
3557void
3558sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3559	     int base, int count)
3560{
3561    sc_softc_t *sc;
3562
3563    sc = scp->sc;
3564    sc->font_loading_in_progress = TRUE;
3565    (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, width, buf, base,
3566				     count);
3567    sc->font_loading_in_progress = FALSE;
3568}
3569
3570void
3571sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3572	     int base, int count)
3573{
3574    sc_softc_t *sc;
3575
3576    sc = scp->sc;
3577    sc->font_loading_in_progress = TRUE;
3578    (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, width, buf, base,
3579				     count);
3580    sc->font_loading_in_progress = FALSE;
3581}
3582
3583void
3584sc_show_font(scr_stat *scp, int page)
3585{
3586    (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3587}
3588#endif /* !SC_NO_FONT_LOADING */
3589
3590void
3591sc_paste(scr_stat *scp, u_char *p, int count)
3592{
3593    struct tty *tp;
3594    u_char *rmap;
3595
3596    tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3597    if (!ISTTYOPEN(tp))
3598	return;
3599    rmap = scp->sc->scr_rmap;
3600    for (; count > 0; --count)
3601	ttyld_rint(tp, rmap[*p++]);
3602}
3603
3604void
3605sc_bell(scr_stat *scp, int pitch, int duration)
3606{
3607    if (cold || shutdown_in_progress || !enable_bell)
3608	return;
3609
3610    if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3611	return;
3612
3613    if (scp->sc->flags & SC_VISUAL_BELL) {
3614	if (scp->sc->blink_in_progress)
3615	    return;
3616	scp->sc->blink_in_progress = 3;
3617	if (scp != scp->sc->cur_scp)
3618	    scp->sc->blink_in_progress += 2;
3619	blink_screen(scp->sc->cur_scp);
3620    } else if (duration != 0 && pitch != 0) {
3621	if (scp != scp->sc->cur_scp)
3622	    pitch *= 2;
3623	sysbeep(pitch, duration);
3624    }
3625}
3626
3627static void
3628blink_screen(void *arg)
3629{
3630    scr_stat *scp = arg;
3631    struct tty *tp;
3632
3633    if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3634	scp->sc->blink_in_progress = 0;
3635    	mark_all(scp);
3636	tp = VIRTUAL_TTY(scp->sc, scp->index);
3637	if (ISTTYOPEN(tp))
3638	    scstart(tp);
3639	if (scp->sc->delayed_next_scr)
3640	    sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3641    }
3642    else {
3643	(*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3644			   scp->sc->blink_in_progress & 1);
3645	scp->sc->blink_in_progress--;
3646	timeout(blink_screen, scp, hz / 10);
3647    }
3648}
3649
3650/*
3651 * Until sc_attach_unit() gets called no dev structures will be available
3652 * to store the per-screen current status.  This is the case when the
3653 * kernel is initially booting and needs access to its console.  During
3654 * this early phase of booting the console's current status is kept in
3655 * one statically defined scr_stat structure, and any pointers to the
3656 * dev structures will be NULL.
3657 */
3658
3659static scr_stat *
3660sc_get_stat(struct cdev *devptr)
3661{
3662	if (devptr == NULL)
3663		return (&main_console);
3664	return (SC_STAT(devptr));
3665}
3666
3667/*
3668 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3669 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3670 * any keyboard.
3671 */
3672
3673static int
3674sc_allocate_keyboard(sc_softc_t *sc, int unit)
3675{
3676	int		 idx0, idx;
3677	keyboard_t	*k0, *k;
3678	keyboard_info_t	 ki;
3679
3680	idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3681	if (idx0 != -1) {
3682		k0 = kbd_get_keyboard(idx0);
3683
3684		for (idx = kbd_find_keyboard2("*", -1, 0);
3685		     idx != -1;
3686		     idx = kbd_find_keyboard2("*", -1, idx + 1)) {
3687			k = kbd_get_keyboard(idx);
3688
3689			if (idx == idx0 || KBD_IS_BUSY(k))
3690				continue;
3691
3692			bzero(&ki, sizeof(ki));
3693			strcpy(ki.kb_name, k->kb_name);
3694			ki.kb_unit = k->kb_unit;
3695
3696			kbd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3697		}
3698	} else
3699		idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);
3700
3701	return (idx0);
3702}
3703