syscons.c revision 97918
1116742Ssam/*-
2116904Ssam * Copyright (c) 1992-1998 S�ren Schmidt
3186904Ssam * All rights reserved.
4116742Ssam *
5116742Ssam * Redistribution and use in source and binary forms, with or without
6116742Ssam * modification, are permitted provided that the following conditions
7116742Ssam * are met:
8116742Ssam * 1. Redistributions of source code must retain the above copyright
9116742Ssam *    notice, this list of conditions and the following disclaimer,
10116742Ssam *    without modification, immediately at the beginning of the file.
11116742Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116742Ssam *    notice, this list of conditions and the following disclaimer in the
13116742Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam * 3. The name of the author may not be used to endorse or promote products
15116904Ssam *    derived from this software without specific prior written permission.
16116904Ssam *
17116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26116742Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27116742Ssam *
28116742Ssam * $FreeBSD: head/sys/dev/syscons/syscons.c 97918 2002-06-06 06:02:17Z alfred $
29116742Ssam */
30116742Ssam
31138568Ssam#include "opt_syscons.h"
32170530Ssam#include "opt_splash.h"
33116742Ssam#include "opt_ddb.h"
34138568Ssam
35178354Ssam#include <sys/param.h>
36178354Ssam#include <sys/systm.h>
37178354Ssam#include <sys/conf.h>
38195618Srpaulo#include <sys/cons.h>
39178354Ssam#include <sys/consio.h>
40178354Ssam#include <sys/eventhandler.h>
41178354Ssam#include <sys/fbio.h>
42178354Ssam#include <sys/kbio.h>
43178354Ssam#include <sys/kernel.h>
44178354Ssam#include <sys/lock.h>
45138568Ssam#include <sys/malloc.h>
46138568Ssam#include <sys/mutex.h>
47138568Ssam#include <sys/proc.h>
48138568Ssam#include <sys/random.h>
49138568Ssam#include <sys/reboot.h>
50138568Ssam#include <sys/signalvar.h>
51138568Ssam#include <sys/sysctl.h>
52138568Ssam#include <sys/tty.h>
53138568Ssam#include <sys/power.h>
54170530Ssam
55138568Ssam#include <machine/clock.h>
56172211Ssam#include <machine/pc/display.h>
57172211Ssam#ifdef __i386__
58172211Ssam#include <machine/psl.h>
59195618Srpaulo#include <machine/apm_bios.h>
60116742Ssam#include <machine/frame.h>
61195618Srpaulo#endif
62170530Ssam
63138568Ssam#include <dev/kbd/kbdreg.h>
64116742Ssam#include <dev/fb/fbreg.h>
65138568Ssam#include <dev/fb/splashreg.h>
66138568Ssam#include <dev/syscons/syscons.h>
67178354Ssam
68138568Ssam#define COLD 0
69116742Ssam#define WARM 1
70178354Ssam
71178354Ssam#define DEFAULT_BLANKTIME	(5*60)		/* 5 minutes */
72190093Srpaulo#define MAX_BLANKTIME		(7*24*60*60)	/* 7 days!? */
73178354Ssam
74178354Ssam#define KEYCODE_BS		0x0e		/* "<-- Backspace" key, XXX */
75178354Ssam
76178354Ssamtypedef struct default_attr {
77178354Ssam	int		std_color;		/* normal hardware color */
78178354Ssam	int		rev_color;		/* reverse hardware color */
79178354Ssam} default_attr;
80178354Ssam
81178354Ssamstatic default_attr user_default = {
82178354Ssam    SC_NORM_ATTR,
83186904Ssam    SC_NORM_REV_ATTR,
84195618Srpaulo};
85193239Ssam
86178354Ssamstatic default_attr kernel_default = {
87178354Ssam    SC_KERNEL_CONS_ATTR,
88178354Ssam    SC_KERNEL_CONS_REV_ATTR,
89178354Ssam};
90178354Ssam
91178354Ssamstatic	int		sc_console_unit = -1;
92195618Srpaulostatic	int		sc_saver_keyb_only = 1;
93195618Srpaulostatic  scr_stat    	*sc_console;
94195618Srpaulostatic	struct tty	*sc_console_tty;
95195618Srpaulostatic	void		*kernel_console_ts;
96195618Srpaulo
97195618Srpaulostatic  char        	init_done = COLD;
98195618Srpaulostatic  char		shutdown_in_progress = FALSE;
99195618Srpaulostatic	char		sc_malloc = FALSE;
100195618Srpaulo
101195618Srpaulostatic	int		saver_mode = CONS_NO_SAVER; /* LKM/user saver */
102195618Srpaulostatic	int		run_scrn_saver = FALSE;	/* should run the saver? */
103195618Srpaulostatic	long        	scrn_blank_time = 0;    /* screen saver timeout value */
104195618Srpaulo#ifdef DEV_SPLASH
105195618Srpaulostatic	int     	scrn_blanked;		/* # of blanked screen */
106116742Ssamstatic	int		sticky_splash = FALSE;
107116742Ssam
108116742Ssamstatic	void		none_saver(sc_softc_t *sc, int blank) { }
109116742Ssamstatic	void		(*current_saver)(sc_softc_t *, int) = none_saver;
110116742Ssam#endif
111116742Ssam
112178354SsamSYSCTL_NODE(_hw, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
113178354SsamSYSCTL_NODE(_hw_syscons, OID_AUTO, saver, CTLFLAG_RD, 0, "saver");
114178354SsamSYSCTL_INT(_hw_syscons_saver, OID_AUTO, keybonly, CTLFLAG_RW,
115178354Ssam    &sc_saver_keyb_only, 0, "screen saver interrupted by input only");
116178354Ssam#if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
117178354Ssam#include "font.h"
118120483Ssam#endif
119183252Ssam
120183252Ssam	d_ioctl_t	*sc_user_ioctl;
121183252Ssam
122183252Ssamstatic	bios_values_t	bios_value;
123183252Ssam
124183252Ssamstatic	int		enable_panic_key;
125183252SsamSYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
126183252Ssam	   0, "");
127183252Ssam
128183252Ssam#define SC_CONSOLECTL	255
129183252Ssam
130183252Ssam#define VIRTUAL_TTY(sc, x) (SC_DEV((sc), (x)) != NULL ?	\
131183252Ssam	SC_DEV((sc), (x))->si_tty : NULL)
132183255Ssam#define ISTTYOPEN(tp)	((tp) && ((tp)->t_state & TS_ISOPEN))
133183255Ssam
134183256Ssamstatic	int		debugger;
135183257Ssam
136183257Ssam/* prototypes */
137186099Ssamstatic int scvidprobe(int unit, int flags, int cons);
138193549Ssamstatic int sckbdprobe(int unit, int flags, int cons);
139193549Ssamstatic void scmeminit(void *arg);
140183252Ssamstatic int scdevtounit(dev_t dev);
141183252Ssamstatic kbd_callback_func_t sckbdevent;
142183252Ssamstatic int scparam(struct tty *tp, struct termios *t);
143170530Ssamstatic void scstart(struct tty *tp);
144170530Ssamstatic void scinit(int unit, int flags);
145170530Ssam#if __i386__ || __ia64__
146170530Ssamstatic void scterm(int unit, int flags);
147170530Ssam#endif
148170530Ssamstatic void scshutdown(void *arg, int howto);
149170530Ssamstatic u_int scgetc(sc_softc_t *sc, u_int flags);
150170530Ssam#define SCGETC_CN	1
151183252Ssam#define SCGETC_NONBLOCK	2
152170530Ssamstatic int sccngetch(int flags);
153183251Ssamstatic void sccnupdate(scr_stat *scp);
154173273Ssamstatic scr_stat *alloc_scp(sc_softc_t *sc, int vty);
155170530Ssamstatic void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
156178354Ssamstatic timeout_t scrn_timer;
157172225Ssamstatic int and_region(int *s1, int *e1, int s2, int e2);
158191756Ssamstatic void scrn_update(scr_stat *scp, int show_cursor);
159172225Ssam
160191756Ssam#ifdef DEV_SPLASH
161170530Ssamstatic int scsplash_callback(int event, void *arg);
162138568Ssamstatic void scsplash_saver(sc_softc_t *sc, int show);
163138568Ssamstatic int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
164116742Ssamstatic int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
165116742Ssamstatic int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
166178354Ssamstatic int restore_scrn_saver_mode(scr_stat *scp, int changemode);
167170530Ssamstatic void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
168116742Ssamstatic int wait_scrn_saver_stop(sc_softc_t *sc);
169220445Sadrian#define scsplash_stick(stick)		(sticky_splash = (stick))
170220445Sadrian#else /* !DEV_SPLASH */
171220445Sadrian#define scsplash_stick(stick)
172220445Sadrian#endif /* DEV_SPLASH */
173220445Sadrian
174220445Sadrianstatic int do_switch_scr(sc_softc_t *sc, int s);
175220445Sadrianstatic int vt_proc_alive(scr_stat *scp);
176116742Ssamstatic int signal_vt_rel(scr_stat *scp);
177170530Ssamstatic int signal_vt_acq(scr_stat *scp);
178170530Ssamstatic int finish_vt_rel(scr_stat *scp, int release, int *s);
179116742Ssamstatic int finish_vt_acq(scr_stat *scp);
180116742Ssamstatic void exchange_scr(sc_softc_t *sc);
181138568Ssamstatic void update_cursor_image(scr_stat *scp);
182170530Ssamstatic void change_cursor_shape(scr_stat *scp, int flags, int base, int height);
183178354Ssamstatic int save_kbd_state(scr_stat *scp);
184138568Ssamstatic int update_kbd_state(scr_stat *scp, int state, int mask);
185170530Ssamstatic int update_kbd_leds(scr_stat *scp, int which);
186170530Ssamstatic timeout_t blink_screen;
187170530Ssam
188170530Ssam#define	CDEV_MAJOR	12
189116742Ssam
190170530Ssamstatic cn_probe_t	sccnprobe;
191170530Ssamstatic cn_init_t	sccninit;
192170530Ssamstatic cn_getc_t	sccngetc;
193178354Ssamstatic cn_checkc_t	sccncheckc;
194170530Ssamstatic cn_putc_t	sccnputc;
195170530Ssamstatic cn_dbctl_t	sccndbctl;
196170530Ssamstatic cn_term_t	sccnterm;
197116742Ssam
198195618Srpaulo#if __alpha__
199195618Srpaulovoid sccnattach(void);
200195618Srpaulo#endif
201195618Srpaulo
202195618SrpauloCONS_DRIVER(sc, sccnprobe, sccninit, sccnterm, sccngetc, sccncheckc, sccnputc,
203195618Srpaulo	    sccndbctl);
204195618Srpaulo
205195618Srpaulostatic	d_open_t	scopen;
206195618Srpaulostatic	d_close_t	scclose;
207246497Smonthadarstatic	d_read_t	scread;
208246497Smonthadarstatic	d_ioctl_t	scioctl;
209195618Srpaulostatic	d_mmap_t	scmmap;
210170530Ssam
211170530Ssamstatic struct cdevsw sc_cdevsw = {
212170530Ssam	/* open */	scopen,
213170530Ssam	/* close */	scclose,
214170530Ssam	/* read */	scread,
215170530Ssam	/* write */	ttywrite,
216170530Ssam	/* ioctl */	scioctl,
217170530Ssam	/* poll */	ttypoll,
218170530Ssam	/* mmap */	scmmap,
219234324Sadrian	/* strategy */	nostrategy,
220170530Ssam	/* name */	"sc",
221170530Ssam	/* maj */	CDEV_MAJOR,
222288318Sadrian	/* dump */	nodump,
223288318Sadrian	/* psize */	nopsize,
224288318Sadrian	/* flags */	D_TTY,
225116742Ssam};
226138568Ssam
227138568Ssamint
228178354Ssamsc_probe_unit(int unit, int flags)
229184288Ssam{
230138568Ssam    if (!scvidprobe(unit, flags, FALSE)) {
231178354Ssam	if (bootverbose)
232178354Ssam	    printf("sc%d: no video adapter found.\n", unit);
233206358Srpaulo	return ENXIO;
234206358Srpaulo    }
235116742Ssam
236138568Ssam    /* syscons will be attached even when there is no keyboard */
237178354Ssam    sckbdprobe(unit, flags, FALSE);
238116742Ssam
239170530Ssam    return 0;
240173273Ssam}
241173273Ssam
242193549Ssam/* probe video adapters, return TRUE if found */
243193549Ssamstatic int
244182828Ssamscvidprobe(int unit, int flags, int cons)
245182828Ssam{
246193549Ssam    /*
247193549Ssam     * Access the video adapter driver through the back door!
248193549Ssam     * Video adapter drivers need to be configured before syscons.
249170530Ssam     * However, when syscons is being probed as the low-level console,
250187797Ssam     * they have not been initialized yet.  We force them to initialize
251187797Ssam     * themselves here. XXX
252187797Ssam     */
253187797Ssam    vid_configure(cons ? VIO_PROBE_ONLY : 0);
254187797Ssam
255138568Ssam    return (vid_find_adapter("*", unit) >= 0);
256138568Ssam}
257138568Ssam
258138568Ssam/* probe the keyboard, return TRUE if found */
259138568Ssamstatic int
260138568Ssamsckbdprobe(int unit, int flags, int cons)
261178354Ssam{
262178354Ssam    /* access the keyboard driver through the backdoor! */
263178354Ssam    kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
264178354Ssam
265178354Ssam    return (kbd_find_keyboard("*", unit) >= 0);
266178354Ssam}
267178354Ssam
268178354Ssamstatic char
269178354Ssam*adapter_name(video_adapter_t *adp)
270178354Ssam{
271178354Ssam    static struct {
272178354Ssam	int type;
273178354Ssam	char *name[2];
274178354Ssam    } names[] = {
275178354Ssam	{ KD_MONO,	{ "MDA",	"MDA" } },
276178354Ssam	{ KD_HERCULES,	{ "Hercules",	"Hercules" } },
277178354Ssam	{ KD_CGA,	{ "CGA",	"CGA" } },
278178354Ssam	{ KD_EGA,	{ "EGA",	"EGA (mono)" } },
279178354Ssam	{ KD_VGA,	{ "VGA",	"VGA (mono)" } },
280178354Ssam	{ KD_PC98,	{ "PC-98x1",	"PC-98x1" } },
281178354Ssam	{ KD_TGA,	{ "TGA",	"TGA" } },
282178354Ssam	{ -1,		{ "Unknown",	"Unknown" } },
283178354Ssam    };
284178354Ssam    int i;
285178354Ssam
286178354Ssam    for (i = 0; names[i].type != -1; ++i)
287178354Ssam	if (names[i].type == adp->va_type)
288178354Ssam	    break;
289178354Ssam    return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
290178354Ssam}
291178354Ssam
292178354Ssamint
293116742Ssamsc_attach_unit(int unit, int flags)
294116742Ssam{
295116742Ssam    sc_softc_t *sc;
296138568Ssam    scr_stat *scp;
297116742Ssam#ifdef SC_PIXEL_MODE
298116742Ssam    video_info_t info;
299116742Ssam#endif
300116742Ssam    int vc;
301116742Ssam    dev_t dev;
302116742Ssam
303138568Ssam    flags &= ~SC_KERNEL_CONSOLE;
304116742Ssam
305116742Ssam    if (sc_console_unit == unit) {
306116742Ssam	/*
307144618Ssam	 * If this unit is being used as the system console, we need to
308144618Ssam	 * adjust some variables and buffers before and after scinit().
309144618Ssam	 */
310178354Ssam	/* assert(sc_console != NULL) */
311178354Ssam	flags |= SC_KERNEL_CONSOLE;
312178354Ssam	scmeminit(NULL);
313127877Ssam
314138568Ssam	scinit(unit, flags);
315138568Ssam
316138568Ssam	if (sc_console->tsw->te_size > 0) {
317138568Ssam	    /* assert(sc_console->ts != NULL); */
318138568Ssam	    kernel_console_ts = sc_console->ts;
319116742Ssam	    sc_console->ts = malloc(sc_console->tsw->te_size,
320148302Ssam				    M_DEVBUF, M_WAITOK);
321148302Ssam	    bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
322138568Ssam    	    (*sc_console->tsw->te_default_attr)(sc_console,
323193966Ssam						user_default.std_color,
324178354Ssam						user_default.rev_color);
325178354Ssam	}
326178354Ssam    } else {
327178354Ssam	scinit(unit, flags);
328178354Ssam    }
329191746Sthompsa
330191746Sthompsa    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
331178354Ssam    sc->config = flags;
332233452Sadrian    scp = SC_STAT(sc->dev[0]);
333148306Ssam    if (sc_console == NULL)	/* sc_console_unit < 0 */
334170530Ssam	sc_console = scp;
335184274Ssam
336170530Ssam#ifdef SC_PIXEL_MODE
337178354Ssam    if ((sc->config & SC_VESA800X600)
338178354Ssam	&& ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
339138568Ssam#ifdef DEV_SPLASH
340178354Ssam	if (sc->flags & SC_SPLASH_SCRN)
341178354Ssam	    splash_term(sc->adp);
342178354Ssam#endif
343178354Ssam	sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
344178354Ssam	sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
345178354Ssam	sc->initial_mode = M_VESA_800x600;
346178354Ssam#ifdef DEV_SPLASH
347138568Ssam	/* put up the splash again! */
348138568Ssam	if (sc->flags & SC_SPLASH_SCRN)
349178354Ssam    	    splash_init(sc->adp, scsplash_callback, sc);
350178354Ssam#endif
351178354Ssam    }
352178354Ssam#endif /* SC_PIXEL_MODE */
353138568Ssam
354138568Ssam    /* initialize cursor */
355138568Ssam    if (!ISGRAPHSC(scp))
356138568Ssam    	update_cursor_image(scp);
357138568Ssam
358138568Ssam    /* get screen update going */
359170530Ssam    scrn_timer(sc);
360170530Ssam
361178354Ssam    /* set up the keyboard */
362138568Ssam    kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
363178354Ssam    update_kbd_state(scp, scp->status, LOCK_MASK);
364138568Ssam
365138568Ssam    printf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
366138568Ssam	   unit, adapter_name(sc->adp), sc->vtys, sc->config);
367178354Ssam    if (bootverbose) {
368178354Ssam	printf("sc%d:", unit);
369178354Ssam    	if (sc->adapter >= 0)
370178354Ssam	    printf(" fb%d", sc->adapter);
371178354Ssam	if (sc->keyboard >= 0)
372178354Ssam	    printf(", kbd%d", sc->keyboard);
373178354Ssam	if (scp->tsw)
374178354Ssam	    printf(", terminal emulator: %s (%s)",
375178354Ssam		   scp->tsw->te_name, scp->tsw->te_desc);
376178354Ssam	printf("\n");
377138568Ssam    }
378144618Ssam
379138568Ssam    /* register a shutdown callback for the kernel console */
380178354Ssam    if (sc_console_unit == unit)
381178354Ssam	EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown,
382178354Ssam			      (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
383178354Ssam
384170530Ssam    for (vc = 0; vc < sc->vtys; vc++) {
385178354Ssam	dev = make_dev(&sc_cdevsw, vc + unit * MAXCONS,
386138568Ssam	    UID_ROOT, GID_WHEEL, 0600, "ttyv%r", vc + unit * MAXCONS);
387178354Ssam	sc->dev[vc] = dev;
388178354Ssam	/*
389178354Ssam	 * The first vty already has struct tty and scr_stat initialized
390178354Ssam	 * in scinit().  The other vtys will have these structs when
391178354Ssam	 * first opened.
392178354Ssam	 */
393178354Ssam    }
394178354Ssam
395178354Ssam    dev = make_dev(&sc_cdevsw, SC_CONSOLECTL,
396178354Ssam		   UID_ROOT, GID_WHEEL, 0600, "consolectl");
397170530Ssam    dev->si_tty = sc_console_tty = ttymalloc(sc_console_tty);
398170530Ssam    SC_STAT(dev) = sc_console;
399138568Ssam
400148863Ssam    return 0;
401148863Ssam}
402170530Ssam
403148863Ssamstatic void
404178354Ssamscmeminit(void *arg)
405170530Ssam{
406170530Ssam    if (sc_malloc)
407138568Ssam	return;
408138568Ssam    sc_malloc = TRUE;
409178354Ssam
410178354Ssam    /*
411138568Ssam     * As soon as malloc() becomes functional, we had better allocate
412138568Ssam     * various buffers for the kernel console.
413178354Ssam     */
414178354Ssam
415178354Ssam    if (sc_console_unit < 0)	/* sc_console == NULL */
416178354Ssam	return;
417178354Ssam
418178354Ssam    /* copy the temporary buffer to the final buffer */
419178354Ssam    sc_alloc_scr_buffer(sc_console, FALSE, FALSE);
420178354Ssam
421178354Ssam#ifndef SC_NO_CUTPASTE
422178354Ssam    sc_alloc_cut_buffer(sc_console, FALSE);
423138568Ssam#endif
424144618Ssam
425178354Ssam#ifndef SC_NO_HISTORY
426178354Ssam    /* initialize history buffer & pointers */
427170530Ssam    sc_alloc_history_buffer(sc_console, 0, 0, FALSE);
428178354Ssam#endif
429178354Ssam}
430178354Ssam
431178354Ssam/* XXX */
432178354SsamSYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
433178354Ssam
434178354Ssamstatic int
435170530Ssamscdevtounit(dev_t dev)
436170530Ssam{
437148863Ssam    int vty = SC_VTY(dev);
438170530Ssam
439178354Ssam    if (vty == SC_CONSOLECTL)
440178354Ssam	return ((sc_console != NULL) ? sc_console->sc->unit : -1);
441138568Ssam    else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
442148863Ssam	return -1;
443170530Ssam    else
444138568Ssam	return vty/MAXCONS;
445116742Ssam}
446239312Sadrian
447239312Sadrianint
448144618Ssamscopen(dev_t dev, int flag, int mode, struct thread *td)
449116742Ssam{
450116742Ssam    int unit = scdevtounit(dev);
451178354Ssam    sc_softc_t *sc;
452144618Ssam    struct tty *tp;
453138568Ssam    scr_stat *scp;
454144618Ssam    keyarg_t key;
455138568Ssam    int error;
456178354Ssam
457178354Ssam    DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
458170530Ssam		major(dev), minor(dev), unit, SC_VTY(dev)));
459153073Ssam
460153073Ssam    sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
461153073Ssam    if (sc == NULL)
462178354Ssam	return ENXIO;
463148936Ssam
464148936Ssam    tp = dev->si_tty = ttymalloc(dev->si_tty);
465178354Ssam    tp->t_oproc = scstart;
466178354Ssam    tp->t_param = scparam;
467178354Ssam    tp->t_stop = nottystop;
468178354Ssam    tp->t_dev = dev;
469116742Ssam    if (!ISTTYOPEN(tp)) {
470	ttychars(tp);
471        /* Use the current setting of the <-- key as default VERASE. */
472        /* If the Delete key is preferable, an stty is necessary     */
473	if (sc->kbd != NULL) {
474	    key.keynum = KEYCODE_BS;
475	    kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
476            tp->t_cc[VERASE] = key.key.map[0];
477	}
478	tp->t_iflag = TTYDEF_IFLAG;
479	tp->t_oflag = TTYDEF_OFLAG;
480	tp->t_cflag = TTYDEF_CFLAG;
481	tp->t_lflag = TTYDEF_LFLAG;
482	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
483	scparam(tp, &tp->t_termios);
484	(*linesw[tp->t_line].l_modem)(tp, 1);
485    }
486    else
487	if (tp->t_state & TS_XCLUDE && suser(td))
488	    return(EBUSY);
489
490    error = (*linesw[tp->t_line].l_open)(dev, tp);
491
492    scp = SC_STAT(dev);
493    if (scp == NULL) {
494	scp = SC_STAT(dev) = alloc_scp(sc, SC_VTY(dev));
495	if (ISGRAPHSC(scp))
496	    sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
497    }
498    if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
499	tp->t_winsize.ws_col = scp->xsize;
500	tp->t_winsize.ws_row = scp->ysize;
501    }
502
503    return error;
504}
505
506int
507scclose(dev_t dev, int flag, int mode, struct thread *td)
508{
509    struct tty *tp = dev->si_tty;
510    scr_stat *scp;
511    int s;
512
513    if (SC_VTY(dev) != SC_CONSOLECTL) {
514	scp = SC_STAT(tp->t_dev);
515	/* were we in the middle of the VT switching process? */
516	DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
517	s = spltty();
518	if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
519	    cons_unavail = FALSE;
520	if (finish_vt_rel(scp, TRUE, &s) == 0)	/* force release */
521	    DPRINTF(5, ("reset WAIT_REL, "));
522	if (finish_vt_acq(scp) == 0)		/* force acknowledge */
523	    DPRINTF(5, ("reset WAIT_ACQ, "));
524#if not_yet_done
525	if (scp == &main_console) {
526	    scp->pid = 0;
527	    scp->proc = NULL;
528	    scp->smode.mode = VT_AUTO;
529	}
530	else {
531	    sc_vtb_destroy(&scp->vtb);
532	    sc_vtb_destroy(&scp->scr);
533	    sc_free_history_buffer(scp, scp->ysize);
534	    SC_STAT(dev) = NULL;
535	    free(scp, M_DEVBUF);
536	}
537#else
538	scp->pid = 0;
539	scp->proc = NULL;
540	scp->smode.mode = VT_AUTO;
541#endif
542	scp->kbd_mode = K_XLATE;
543	if (scp == scp->sc->cur_scp)
544	    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
545	DPRINTF(5, ("done.\n"));
546    }
547    spltty();
548    (*linesw[tp->t_line].l_close)(tp, flag);
549    ttyclose(tp);
550    spl0();
551    return(0);
552}
553
554int
555scread(dev_t dev, struct uio *uio, int flag)
556{
557    if (!sc_saver_keyb_only)
558	sc_touch_scrn_saver();
559    return ttyread(dev, uio, flag);
560}
561
562static int
563sckbdevent(keyboard_t *thiskbd, int event, void *arg)
564{
565    sc_softc_t *sc;
566    struct tty *cur_tty;
567    int c;
568    size_t len;
569    u_char *cp;
570
571    sc = (sc_softc_t *)arg;
572    /* assert(thiskbd == sc->kbd) */
573
574    switch (event) {
575    case KBDIO_KEYINPUT:
576	break;
577    case KBDIO_UNLOADING:
578	sc->kbd = NULL;
579	sc->keyboard = -1;
580	kbd_release(thiskbd, (void *)&sc->keyboard);
581	return 0;
582    default:
583	return EINVAL;
584    }
585
586    /*
587     * Loop while there is still input to get from the keyboard.
588     * I don't think this is nessesary, and it doesn't fix
589     * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
590     */
591    while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
592
593	cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
594	if (!ISTTYOPEN(cur_tty)) {
595	    cur_tty = sc_console_tty;
596	    if (!ISTTYOPEN(cur_tty))
597		continue;
598	}
599
600	if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
601	    continue;
602
603	switch (KEYFLAGS(c)) {
604	case 0x0000: /* normal key */
605	    (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
606	    break;
607	case FKEY:  /* function key, return string */
608	    cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
609	    if (cp != NULL) {
610	    	while (len-- >  0)
611		    (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
612	    }
613	    break;
614	case MKEY:  /* meta is active, prepend ESC */
615	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
616	    (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
617	    break;
618	case BKEY:  /* backtab fixed sequence (esc [ Z) */
619	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
620	    (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
621	    (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
622	    break;
623	}
624    }
625
626    sc->cur_scp->status |= MOUSE_HIDDEN;
627
628    return 0;
629}
630
631static int
632scparam(struct tty *tp, struct termios *t)
633{
634    tp->t_ispeed = t->c_ispeed;
635    tp->t_ospeed = t->c_ospeed;
636    tp->t_cflag = t->c_cflag;
637    return 0;
638}
639
640int
641scioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
642{
643    int error;
644    int i;
645    struct tty *tp;
646    sc_softc_t *sc;
647    scr_stat *scp;
648    int s;
649
650    tp = dev->si_tty;
651
652    /* If there is a user_ioctl function call that first */
653    if (sc_user_ioctl) {
654	error = (*sc_user_ioctl)(dev, cmd, data, flag, td);
655	if (error != ENOIOCTL)
656	    return error;
657    }
658
659    error = sc_vid_ioctl(tp, cmd, data, flag, td);
660    if (error != ENOIOCTL)
661	return error;
662
663#ifndef SC_NO_HISTORY
664    error = sc_hist_ioctl(tp, cmd, data, flag, td);
665    if (error != ENOIOCTL)
666	return error;
667#endif
668
669#ifndef SC_NO_SYSMOUSE
670    error = sc_mouse_ioctl(tp, cmd, data, flag, td);
671    if (error != ENOIOCTL)
672	return error;
673#endif
674
675    scp = SC_STAT(tp->t_dev);
676    /* assert(scp != NULL) */
677    /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
678    sc = scp->sc;
679
680    if (scp->tsw) {
681	error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag, td);
682	if (error != ENOIOCTL)
683	    return error;
684    }
685
686    switch (cmd) {  		/* process console hardware related ioctl's */
687
688    case GIO_ATTR:      	/* get current attributes */
689	/* this ioctl is not processed here, but in the terminal emulator */
690	return ENOTTY;
691
692    case GIO_COLOR:     	/* is this a color console ? */
693	*(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
694	return 0;
695
696    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
697	if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
698            return EINVAL;
699	s = spltty();
700	scrn_blank_time = *(int *)data;
701	run_scrn_saver = (scrn_blank_time != 0);
702	splx(s);
703	return 0;
704
705    case CONS_CURSORTYPE:   	/* set cursor type (obsolete) */
706	s = spltty();
707	*(int *)data &= CONS_CURSOR_ATTRS;
708	sc_change_cursor_shape(scp, *(int *)data, -1, -1);
709	splx(s);
710	return 0;
711
712    case CONS_GETCURSORSHAPE:   /* get cursor shape (new interface) */
713	if (((int *)data)[0] & CONS_LOCAL_CURSOR) {
714	    ((int *)data)[0] = scp->curr_curs_attr.flags;
715	    ((int *)data)[1] = scp->curr_curs_attr.base;
716	    ((int *)data)[2] = scp->curr_curs_attr.height;
717	} else {
718	    ((int *)data)[0] = sc->curs_attr.flags;
719	    ((int *)data)[1] = sc->curs_attr.base;
720	    ((int *)data)[2] = sc->curs_attr.height;
721	}
722	return 0;
723
724    case CONS_SETCURSORSHAPE:   /* set cursor shape (new interface) */
725	s = spltty();
726	sc_change_cursor_shape(scp, ((int *)data)[0],
727	    ((int *)data)[1], ((int *)data)[2]);
728	splx(s);
729	return 0;
730
731    case CONS_BELLTYPE: 	/* set bell type sound/visual */
732	if ((*(int *)data) & 0x01)
733	    sc->flags |= SC_VISUAL_BELL;
734	else
735	    sc->flags &= ~SC_VISUAL_BELL;
736	if ((*(int *)data) & 0x02)
737	    sc->flags |= SC_QUIET_BELL;
738	else
739	    sc->flags &= ~SC_QUIET_BELL;
740	return 0;
741
742    case CONS_GETINFO:  	/* get current (virtual) console info */
743    {
744	vid_info_t *ptr = (vid_info_t*)data;
745	if (ptr->size == sizeof(struct vid_info)) {
746	    ptr->m_num = sc->cur_scp->index;
747	    ptr->font_size = scp->font_size;
748	    ptr->mv_col = scp->xpos;
749	    ptr->mv_row = scp->ypos;
750	    ptr->mv_csz = scp->xsize;
751	    ptr->mv_rsz = scp->ysize;
752	    /*
753	     * The following fields are filled by the terminal emulator. XXX
754	     *
755	     * ptr->mv_norm.fore
756	     * ptr->mv_norm.back
757	     * ptr->mv_rev.fore
758	     * ptr->mv_rev.back
759	     */
760	    ptr->mv_grfc.fore = 0;      /* not supported */
761	    ptr->mv_grfc.back = 0;      /* not supported */
762	    ptr->mv_ovscan = scp->border;
763	    if (scp == sc->cur_scp)
764		save_kbd_state(scp);
765	    ptr->mk_keylock = scp->status & LOCK_MASK;
766	    return 0;
767	}
768	return EINVAL;
769    }
770
771    case CONS_GETVERS:  	/* get version number */
772	*(int*)data = 0x200;    /* version 2.0 */
773	return 0;
774
775    case CONS_IDLE:		/* see if the screen has been idle */
776	/*
777	 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
778	 * the user process may have been writing something on the
779	 * screen and syscons is not aware of it. Declare the screen
780	 * is NOT idle if it is in one of these modes. But there is
781	 * an exception to it; if a screen saver is running in the
782	 * graphics mode in the current screen, we should say that the
783	 * screen has been idle.
784	 */
785	*(int *)data = (sc->flags & SC_SCRN_IDLE)
786		       && (!ISGRAPHSC(sc->cur_scp)
787			   || (sc->cur_scp->status & SAVER_RUNNING));
788	return 0;
789
790    case CONS_SAVERMODE:	/* set saver mode */
791	switch(*(int *)data) {
792	case CONS_NO_SAVER:
793	case CONS_USR_SAVER:
794	    /* if a LKM screen saver is running, stop it first. */
795	    scsplash_stick(FALSE);
796	    saver_mode = *(int *)data;
797	    s = spltty();
798#ifdef DEV_SPLASH
799	    if ((error = wait_scrn_saver_stop(NULL))) {
800		splx(s);
801		return error;
802	    }
803#endif
804	    run_scrn_saver = TRUE;
805	    if (saver_mode == CONS_USR_SAVER)
806		scp->status |= SAVER_RUNNING;
807	    else
808		scp->status &= ~SAVER_RUNNING;
809	    scsplash_stick(TRUE);
810	    splx(s);
811	    break;
812	case CONS_LKM_SAVER:
813	    s = spltty();
814	    if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
815		scp->status &= ~SAVER_RUNNING;
816	    saver_mode = *(int *)data;
817	    splx(s);
818	    break;
819	default:
820	    return EINVAL;
821	}
822	return 0;
823
824    case CONS_SAVERSTART:	/* immediately start/stop the screen saver */
825	/*
826	 * Note that this ioctl does not guarantee the screen saver
827	 * actually starts or stops. It merely attempts to do so...
828	 */
829	s = spltty();
830	run_scrn_saver = (*(int *)data != 0);
831	if (run_scrn_saver)
832	    sc->scrn_time_stamp -= scrn_blank_time;
833	splx(s);
834	return 0;
835
836    case CONS_SCRSHOT:		/* get a screen shot */
837    {
838	scrshot_t *ptr = (scrshot_t*)data;
839	s = spltty();
840	if (ISGRAPHSC(scp)) {
841	    splx(s);
842	    return EOPNOTSUPP;
843	}
844	if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
845	    splx(s);
846	    return EINVAL;
847	}
848	copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
849		 ptr->xsize * ptr->ysize * sizeof(u_int16_t));
850	splx(s);
851	return 0;
852    }
853
854    case VT_SETMODE:    	/* set screen switcher mode */
855    {
856	struct vt_mode *mode;
857	struct proc *p1;
858
859	mode = (struct vt_mode *)data;
860	DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
861	if (scp->smode.mode == VT_PROCESS) {
862	    p1 = pfind(scp->pid);
863    	    if (scp->proc == p1 && scp->proc != td->td_proc) {
864		if (p1)
865		    PROC_UNLOCK(p1);
866		DPRINTF(5, ("error EPERM\n"));
867		return EPERM;
868	    }
869	    if (p1)
870		PROC_UNLOCK(p1);
871	}
872	s = spltty();
873	if (mode->mode == VT_AUTO) {
874	    scp->smode.mode = VT_AUTO;
875	    scp->proc = NULL;
876	    scp->pid = 0;
877	    DPRINTF(5, ("VT_AUTO, "));
878	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
879		cons_unavail = FALSE;
880	    /* were we in the middle of the vty switching process? */
881	    if (finish_vt_rel(scp, TRUE, &s) == 0)
882		DPRINTF(5, ("reset WAIT_REL, "));
883	    if (finish_vt_acq(scp) == 0)
884		DPRINTF(5, ("reset WAIT_ACQ, "));
885	} else {
886	    if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
887		|| !ISSIGVALID(mode->frsig)) {
888		splx(s);
889		DPRINTF(5, ("error EINVAL\n"));
890		return EINVAL;
891	    }
892	    DPRINTF(5, ("VT_PROCESS %d, ", td->td_proc->p_pid));
893	    bcopy(data, &scp->smode, sizeof(struct vt_mode));
894	    scp->proc = td->td_proc;
895	    scp->pid = scp->proc->p_pid;
896	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
897		cons_unavail = TRUE;
898	}
899	splx(s);
900	DPRINTF(5, ("\n"));
901	return 0;
902    }
903
904    case VT_GETMODE:    	/* get screen switcher mode */
905	bcopy(&scp->smode, data, sizeof(struct vt_mode));
906	return 0;
907
908    case VT_RELDISP:    	/* screen switcher ioctl */
909	s = spltty();
910	/*
911	 * This must be the current vty which is in the VT_PROCESS
912	 * switching mode...
913	 */
914	if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
915	    splx(s);
916	    return EINVAL;
917	}
918	/* ...and this process is controlling it. */
919	if (scp->proc != td->td_proc) {
920	    splx(s);
921	    return EPERM;
922	}
923	error = EINVAL;
924	switch(*(int *)data) {
925	case VT_FALSE:  	/* user refuses to release screen, abort */
926	    if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
927		DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
928	    break;
929	case VT_TRUE:   	/* user has released screen, go on */
930	    if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
931		DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
932	    break;
933	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
934	    if ((error = finish_vt_acq(scp)) == 0)
935		DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
936	    break;
937	default:
938	    break;
939	}
940	splx(s);
941	return error;
942
943    case VT_OPENQRY:    	/* return free virtual console */
944	for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
945	    tp = VIRTUAL_TTY(sc, i);
946	    if (!ISTTYOPEN(tp)) {
947		*(int *)data = i + 1;
948		return 0;
949	    }
950	}
951	return EINVAL;
952
953    case VT_ACTIVATE:   	/* switch to screen *data */
954	i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
955	s = spltty();
956	sc_clean_up(sc->cur_scp);
957	splx(s);
958	return sc_switch_scr(sc, i);
959
960    case VT_WAITACTIVE: 	/* wait for switch to occur */
961	i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
962	if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
963	    return EINVAL;
964	s = spltty();
965	error = sc_clean_up(sc->cur_scp);
966	splx(s);
967	if (error)
968	    return error;
969	scp = SC_STAT(SC_DEV(sc, i));
970	if (scp == scp->sc->cur_scp)
971	    return 0;
972	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
973			     "waitvt", 0)) == ERESTART) ;
974	return error;
975
976    case VT_GETACTIVE:		/* get active vty # */
977	*(int *)data = sc->cur_scp->index + 1;
978	return 0;
979
980    case VT_GETINDEX:		/* get this vty # */
981	*(int *)data = scp->index + 1;
982	return 0;
983
984    case KDENABIO:      	/* allow io operations */
985	error = suser(td);
986	if (error != 0)
987	    return error;
988	error = securelevel_gt(td->td_ucred, 0);
989	if (error != 0)
990		return error;
991#ifdef __i386__
992	td->td_frame->tf_eflags |= PSL_IOPL;
993#endif
994	return 0;
995
996    case KDDISABIO:     	/* disallow io operations (default) */
997#ifdef __i386__
998	td->td_frame->tf_eflags &= ~PSL_IOPL;
999#endif
1000	return 0;
1001
1002    case KDSKBSTATE:    	/* set keyboard state (locks) */
1003	if (*(int *)data & ~LOCK_MASK)
1004	    return EINVAL;
1005	scp->status &= ~LOCK_MASK;
1006	scp->status |= *(int *)data;
1007	if (scp == sc->cur_scp)
1008	    update_kbd_state(scp, scp->status, LOCK_MASK);
1009	return 0;
1010
1011    case KDGKBSTATE:    	/* get keyboard state (locks) */
1012	if (scp == sc->cur_scp)
1013	    save_kbd_state(scp);
1014	*(int *)data = scp->status & LOCK_MASK;
1015	return 0;
1016
1017    case KDGETREPEAT:      	/* get keyboard repeat & delay rates */
1018    case KDSETREPEAT:      	/* set keyboard repeat & delay rates (new) */
1019	error = kbd_ioctl(sc->kbd, cmd, data);
1020	if (error == ENOIOCTL)
1021	    error = ENODEV;
1022	return error;
1023
1024    case KDSETRAD:      	/* set keyboard repeat & delay rates (old) */
1025	if (*(int *)data & ~0x7f)
1026	    return EINVAL;
1027	error = kbd_ioctl(sc->kbd, cmd, data);
1028	if (error == ENOIOCTL)
1029	    error = ENODEV;
1030	return error;
1031
1032    case KDSKBMODE:     	/* set keyboard mode */
1033	switch (*(int *)data) {
1034	case K_XLATE:   	/* switch to XLT ascii mode */
1035	case K_RAW: 		/* switch to RAW scancode mode */
1036	case K_CODE: 		/* switch to CODE mode */
1037	    scp->kbd_mode = *(int *)data;
1038	    if (scp == sc->cur_scp)
1039		kbd_ioctl(sc->kbd, cmd, data);
1040	    return 0;
1041	default:
1042	    return EINVAL;
1043	}
1044	/* NOT REACHED */
1045
1046    case KDGKBMODE:     	/* get keyboard mode */
1047	*(int *)data = scp->kbd_mode;
1048	return 0;
1049
1050    case KDGKBINFO:
1051	error = kbd_ioctl(sc->kbd, cmd, data);
1052	if (error == ENOIOCTL)
1053	    error = ENODEV;
1054	return error;
1055
1056    case KDMKTONE:      	/* sound the bell */
1057	if (*(int*)data)
1058	    sc_bell(scp, (*(int*)data)&0xffff,
1059		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1060	else
1061	    sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1062	return 0;
1063
1064    case KIOCSOUND:     	/* make tone (*data) hz */
1065	if (scp == sc->cur_scp) {
1066	    if (*(int *)data)
1067		return sc_tone(*(int *)data);
1068	    else
1069		return sc_tone(0);
1070	}
1071	return 0;
1072
1073    case KDGKBTYPE:     	/* get keyboard type */
1074	error = kbd_ioctl(sc->kbd, cmd, data);
1075	if (error == ENOIOCTL) {
1076	    /* always return something? XXX */
1077	    *(int *)data = 0;
1078	}
1079	return 0;
1080
1081    case KDSETLED:      	/* set keyboard LED status */
1082	if (*(int *)data & ~LED_MASK)	/* FIXME: LOCK_MASK? */
1083	    return EINVAL;
1084	scp->status &= ~LED_MASK;
1085	scp->status |= *(int *)data;
1086	if (scp == sc->cur_scp)
1087	    update_kbd_leds(scp, scp->status);
1088	return 0;
1089
1090    case KDGETLED:      	/* get keyboard LED status */
1091	if (scp == sc->cur_scp)
1092	    save_kbd_state(scp);
1093	*(int *)data = scp->status & LED_MASK;
1094	return 0;
1095
1096    case CONS_SETKBD: 		/* set the new keyboard */
1097	{
1098	    keyboard_t *newkbd;
1099
1100	    s = spltty();
1101	    newkbd = kbd_get_keyboard(*(int *)data);
1102	    if (newkbd == NULL) {
1103		splx(s);
1104		return EINVAL;
1105	    }
1106	    error = 0;
1107	    if (sc->kbd != newkbd) {
1108		i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1109				 (void *)&sc->keyboard, sckbdevent, sc);
1110		/* i == newkbd->kb_index */
1111		if (i >= 0) {
1112		    if (sc->kbd != NULL) {
1113			save_kbd_state(sc->cur_scp);
1114			kbd_release(sc->kbd, (void *)&sc->keyboard);
1115		    }
1116		    sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1117		    sc->keyboard = i;
1118		    kbd_ioctl(sc->kbd, KDSKBMODE,
1119			      (caddr_t)&sc->cur_scp->kbd_mode);
1120		    update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1121				     LOCK_MASK);
1122		} else {
1123		    error = EPERM;	/* XXX */
1124		}
1125	    }
1126	    splx(s);
1127	    return error;
1128	}
1129
1130    case CONS_RELKBD: 		/* release the current keyboard */
1131	s = spltty();
1132	error = 0;
1133	if (sc->kbd != NULL) {
1134	    save_kbd_state(sc->cur_scp);
1135	    error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1136	    if (error == 0) {
1137		sc->kbd = NULL;
1138		sc->keyboard = -1;
1139	    }
1140	}
1141	splx(s);
1142	return error;
1143
1144    case CONS_GETTERM:		/* get the current terminal emulator info */
1145	{
1146	    sc_term_sw_t *sw;
1147
1148	    if (((term_info_t *)data)->ti_index == 0) {
1149		sw = scp->tsw;
1150	    } else {
1151		sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1152	    }
1153	    if (sw != NULL) {
1154		strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1155			sizeof(((term_info_t *)data)->ti_name));
1156		strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1157			sizeof(((term_info_t *)data)->ti_desc));
1158		((term_info_t *)data)->ti_flags = 0;
1159		return 0;
1160	    } else {
1161		((term_info_t *)data)->ti_name[0] = '\0';
1162		((term_info_t *)data)->ti_desc[0] = '\0';
1163		((term_info_t *)data)->ti_flags = 0;
1164		return EINVAL;
1165	    }
1166	}
1167
1168    case CONS_SETTERM:		/* set the current terminal emulator */
1169	s = spltty();
1170	error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1171	/* FIXME: what if scp == sc_console! XXX */
1172	splx(s);
1173	return error;
1174
1175    case GIO_SCRNMAP:   	/* get output translation table */
1176	bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1177	return 0;
1178
1179    case PIO_SCRNMAP:   	/* set output translation table */
1180	bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1181	for (i=0; i<sizeof(sc->scr_map); i++) {
1182	    sc->scr_rmap[sc->scr_map[i]] = i;
1183	}
1184	return 0;
1185
1186    case GIO_KEYMAP:		/* get keyboard translation table */
1187    case PIO_KEYMAP:		/* set keyboard translation table */
1188    case GIO_DEADKEYMAP:	/* get accent key translation table */
1189    case PIO_DEADKEYMAP:	/* set accent key translation table */
1190    case GETFKEY:		/* get function key string */
1191    case SETFKEY:		/* set function key string */
1192	error = kbd_ioctl(sc->kbd, cmd, data);
1193	if (error == ENOIOCTL)
1194	    error = ENODEV;
1195	return error;
1196
1197#ifndef SC_NO_FONT_LOADING
1198
1199    case PIO_FONT8x8:   	/* set 8x8 dot font */
1200	if (!ISFONTAVAIL(sc->adp->va_flags))
1201	    return ENXIO;
1202	bcopy(data, sc->font_8, 8*256);
1203	sc->fonts_loaded |= FONT_8;
1204	/*
1205	 * FONT KLUDGE
1206	 * Always use the font page #0. XXX
1207	 * Don't load if the current font size is not 8x8.
1208	 */
1209	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1210	    sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
1211	return 0;
1212
1213    case GIO_FONT8x8:   	/* get 8x8 dot font */
1214	if (!ISFONTAVAIL(sc->adp->va_flags))
1215	    return ENXIO;
1216	if (sc->fonts_loaded & FONT_8) {
1217	    bcopy(sc->font_8, data, 8*256);
1218	    return 0;
1219	}
1220	else
1221	    return ENXIO;
1222
1223    case PIO_FONT8x14:  	/* set 8x14 dot font */
1224	if (!ISFONTAVAIL(sc->adp->va_flags))
1225	    return ENXIO;
1226	bcopy(data, sc->font_14, 14*256);
1227	sc->fonts_loaded |= FONT_14;
1228	/*
1229	 * FONT KLUDGE
1230	 * Always use the font page #0. XXX
1231	 * Don't load if the current font size is not 8x14.
1232	 */
1233	if (ISTEXTSC(sc->cur_scp)
1234	    && (sc->cur_scp->font_size >= 14)
1235	    && (sc->cur_scp->font_size < 16))
1236	    sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
1237	return 0;
1238
1239    case GIO_FONT8x14:  	/* get 8x14 dot font */
1240	if (!ISFONTAVAIL(sc->adp->va_flags))
1241	    return ENXIO;
1242	if (sc->fonts_loaded & FONT_14) {
1243	    bcopy(sc->font_14, data, 14*256);
1244	    return 0;
1245	}
1246	else
1247	    return ENXIO;
1248
1249    case PIO_FONT8x16:  	/* set 8x16 dot font */
1250	if (!ISFONTAVAIL(sc->adp->va_flags))
1251	    return ENXIO;
1252	bcopy(data, sc->font_16, 16*256);
1253	sc->fonts_loaded |= FONT_16;
1254	/*
1255	 * FONT KLUDGE
1256	 * Always use the font page #0. XXX
1257	 * Don't load if the current font size is not 8x16.
1258	 */
1259	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1260	    sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
1261	return 0;
1262
1263    case GIO_FONT8x16:  	/* get 8x16 dot font */
1264	if (!ISFONTAVAIL(sc->adp->va_flags))
1265	    return ENXIO;
1266	if (sc->fonts_loaded & FONT_16) {
1267	    bcopy(sc->font_16, data, 16*256);
1268	    return 0;
1269	}
1270	else
1271	    return ENXIO;
1272
1273#endif /* SC_NO_FONT_LOADING */
1274
1275    default:
1276	break;
1277    }
1278
1279    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
1280    if (error != ENOIOCTL)
1281	return(error);
1282    error = ttioctl(tp, cmd, data, flag);
1283    if (error != ENOIOCTL)
1284	return(error);
1285    return(ENOTTY);
1286}
1287
1288static void
1289scstart(struct tty *tp)
1290{
1291    struct clist *rbp;
1292    int s, len;
1293    u_char buf[PCBURST];
1294    scr_stat *scp = SC_STAT(tp->t_dev);
1295
1296    if (scp->status & SLKED || scp->sc->blink_in_progress)
1297	return;
1298    s = spltty();
1299    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1300	tp->t_state |= TS_BUSY;
1301	rbp = &tp->t_outq;
1302	while (rbp->c_cc) {
1303	    len = q_to_b(rbp, buf, PCBURST);
1304	    splx(s);
1305	    sc_puts(scp, buf, len);
1306	    s = spltty();
1307	}
1308	tp->t_state &= ~TS_BUSY;
1309	ttwwakeup(tp);
1310    }
1311    splx(s);
1312}
1313
1314static void
1315sccnprobe(struct consdev *cp)
1316{
1317#if __i386__ || __ia64__
1318    int unit;
1319    int flags;
1320
1321    cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1322
1323    /* a video card is always required */
1324    if (!scvidprobe(unit, flags, TRUE))
1325	cp->cn_pri = CN_DEAD;
1326
1327    /* syscons will become console even when there is no keyboard */
1328    sckbdprobe(unit, flags, TRUE);
1329
1330    if (cp->cn_pri == CN_DEAD)
1331	return;
1332
1333    /* initialize required fields */
1334    cp->cn_dev = makedev(CDEV_MAJOR, SC_CONSOLECTL);
1335#endif /* __i386__ || __ia64__ */
1336
1337#if __alpha__
1338    /*
1339     * alpha use sccnattach() rather than cnprobe()/cninit()/cnterm()
1340     * interface to install the console.  Always return CN_DEAD from
1341     * here.
1342     */
1343    cp->cn_pri = CN_DEAD;
1344#endif /* __alpha__ */
1345}
1346
1347static void
1348sccninit(struct consdev *cp)
1349{
1350#if __i386__ || __ia64__
1351    int unit;
1352    int flags;
1353
1354    sc_get_cons_priority(&unit, &flags);
1355    scinit(unit, flags | SC_KERNEL_CONSOLE);
1356    sc_console_unit = unit;
1357    sc_console = SC_STAT(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1358#endif /* __i386__ */
1359
1360#if __alpha__
1361    /* SHOULDN'T REACH HERE */
1362#endif /* __alpha__ */
1363}
1364
1365static void
1366sccnterm(struct consdev *cp)
1367{
1368    /* we are not the kernel console any more, release everything */
1369
1370    if (sc_console_unit < 0)
1371	return;			/* shouldn't happen */
1372
1373#if __i386__ || __ia64__
1374#if 0 /* XXX */
1375    sc_clear_screen(sc_console);
1376    sccnupdate(sc_console);
1377#endif
1378    scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1379    sc_console_unit = -1;
1380    sc_console = NULL;
1381#endif /* __i386__ */
1382
1383#if __alpha__
1384    /* do nothing XXX */
1385#endif /* __alpha__ */
1386}
1387
1388#ifdef __alpha__
1389
1390void
1391sccnattach(void)
1392{
1393    static struct consdev consdev;
1394    int unit;
1395    int flags;
1396
1397    bcopy(&sc_consdev, &consdev, sizeof(sc_consdev));
1398    consdev.cn_pri = sc_get_cons_priority(&unit, &flags);
1399
1400    /* a video card is always required */
1401    if (!scvidprobe(unit, flags, TRUE))
1402	consdev.cn_pri = CN_DEAD;
1403
1404    /* alpha doesn't allow the console being without a keyboard... Why? */
1405    if (!sckbdprobe(unit, flags, TRUE))
1406	consdev.cn_pri = CN_DEAD;
1407
1408    if (consdev.cn_pri == CN_DEAD)
1409	return;
1410
1411    scinit(unit, flags | SC_KERNEL_CONSOLE);
1412    sc_console_unit = unit;
1413    sc_console = SC_STAT(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1414    consdev.cn_dev = makedev(CDEV_MAJOR, 0);
1415    cnadd(&consdev);
1416}
1417
1418#endif /* __alpha__ */
1419
1420static void
1421sccnputc(dev_t dev, int c)
1422{
1423    u_char buf[1];
1424    scr_stat *scp = sc_console;
1425    void *save;
1426#ifndef SC_NO_HISTORY
1427    struct tty *tp;
1428#endif /* !SC_NO_HISTORY */
1429    int s;
1430
1431    /* assert(sc_console != NULL) */
1432
1433#ifndef SC_NO_HISTORY
1434    if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1435	scp->status &= ~SLKED;
1436	update_kbd_state(scp, scp->status, SLKED);
1437	if (scp->status & BUFFER_SAVED) {
1438	    if (!sc_hist_restore(scp))
1439		sc_remove_cutmarking(scp);
1440	    scp->status &= ~BUFFER_SAVED;
1441	    scp->status |= CURSOR_ENABLED;
1442	    sc_draw_cursor_image(scp);
1443	}
1444	tp = VIRTUAL_TTY(scp->sc, scp->index);
1445	if (ISTTYOPEN(tp))
1446	    scstart(tp);
1447    }
1448#endif /* !SC_NO_HISTORY */
1449
1450    save = scp->ts;
1451    if (kernel_console_ts != NULL)
1452	scp->ts = kernel_console_ts;
1453    buf[0] = c;
1454    sc_puts(scp, buf, 1);
1455    scp->ts = save;
1456
1457    s = spltty();	/* block sckbdevent and scrn_timer */
1458    sccnupdate(scp);
1459    splx(s);
1460}
1461
1462static int
1463sccngetc(dev_t dev)
1464{
1465    return sccngetch(0);
1466}
1467
1468static int
1469sccncheckc(dev_t dev)
1470{
1471    return sccngetch(SCGETC_NONBLOCK);
1472}
1473
1474static void
1475sccndbctl(dev_t dev, int on)
1476{
1477    /* assert(sc_console_unit >= 0) */
1478    /* try to switch to the kernel console screen */
1479    if (on && debugger == 0) {
1480	/*
1481	 * TRY to make sure the screen saver is stopped,
1482	 * and the screen is updated before switching to
1483	 * the vty0.
1484	 */
1485	scrn_timer(NULL);
1486	if (!cold
1487	    && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1488	    && sc_console->smode.mode == VT_AUTO) {
1489	    sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
1490	    ++debugger;		/* XXX */
1491	    sc_switch_scr(sc_console->sc, sc_console->index);
1492	    --debugger;		/* XXX */
1493	}
1494    }
1495    if (on)
1496	++debugger;
1497    else
1498	--debugger;
1499}
1500
1501static int
1502sccngetch(int flags)
1503{
1504    static struct fkeytab fkey;
1505    static int fkeycp;
1506    scr_stat *scp;
1507    u_char *p;
1508    int cur_mode;
1509    int s = spltty();	/* block sckbdevent and scrn_timer while we poll */
1510    int c;
1511
1512    /* assert(sc_console != NULL) */
1513
1514    /*
1515     * Stop the screen saver and update the screen if necessary.
1516     * What if we have been running in the screen saver code... XXX
1517     */
1518    sc_touch_scrn_saver();
1519    scp = sc_console->sc->cur_scp;	/* XXX */
1520    sccnupdate(scp);
1521
1522    if (fkeycp < fkey.len) {
1523	splx(s);
1524	return fkey.str[fkeycp++];
1525    }
1526
1527    if (scp->sc->kbd == NULL) {
1528	splx(s);
1529	return -1;
1530    }
1531
1532    /*
1533     * Make sure the keyboard is accessible even when the kbd device
1534     * driver is disabled.
1535     */
1536    kbd_enable(scp->sc->kbd);
1537
1538    /* we shall always use the keyboard in the XLATE mode here */
1539    cur_mode = scp->kbd_mode;
1540    scp->kbd_mode = K_XLATE;
1541    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1542
1543    kbd_poll(scp->sc->kbd, TRUE);
1544    c = scgetc(scp->sc, SCGETC_CN | flags);
1545    kbd_poll(scp->sc->kbd, FALSE);
1546
1547    scp->kbd_mode = cur_mode;
1548    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1549    kbd_disable(scp->sc->kbd);
1550    splx(s);
1551
1552    switch (KEYFLAGS(c)) {
1553    case 0:	/* normal char */
1554	return KEYCHAR(c);
1555    case FKEY:	/* function key */
1556	p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1557	fkey.len = fkeycp;
1558	if ((p != NULL) && (fkey.len > 0)) {
1559	    bcopy(p, fkey.str, fkey.len);
1560	    fkeycp = 1;
1561	    return fkey.str[0];
1562	}
1563	return c;	/* XXX */
1564    case NOKEY:
1565    case ERRKEY:
1566    default:
1567	return -1;
1568    }
1569    /* NOT REACHED */
1570}
1571
1572static void
1573sccnupdate(scr_stat *scp)
1574{
1575    /* this is a cut-down version of scrn_timer()... */
1576
1577    if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress)
1578	return;
1579
1580    if (debugger > 0 || panicstr || shutdown_in_progress) {
1581	sc_touch_scrn_saver();
1582    } else if (scp != scp->sc->cur_scp) {
1583	return;
1584    }
1585
1586    if (!run_scrn_saver)
1587	scp->sc->flags &= ~SC_SCRN_IDLE;
1588#ifdef DEV_SPLASH
1589    if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1590	if (scp->sc->flags & SC_SCRN_BLANKED)
1591            stop_scrn_saver(scp->sc, current_saver);
1592#endif
1593
1594    if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1595	|| scp->sc->switch_in_progress)
1596	return;
1597    /*
1598     * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1599     * when write_in_progress is non-zero.  XXX
1600     */
1601
1602    if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1603	scrn_update(scp, TRUE);
1604}
1605
1606static void
1607scrn_timer(void *arg)
1608{
1609    static int kbd_interval = 0;
1610    struct timeval tv;
1611    sc_softc_t *sc;
1612    scr_stat *scp;
1613    int again;
1614    int s;
1615
1616    again = (arg != NULL);
1617    if (arg != NULL)
1618	sc = (sc_softc_t *)arg;
1619    else if (sc_console != NULL)
1620	sc = sc_console->sc;
1621    else
1622	return;
1623
1624    /* don't do anything when we are performing some I/O operations */
1625    if (sc->font_loading_in_progress || sc->videoio_in_progress) {
1626	if (again)
1627	    timeout(scrn_timer, sc, hz / 10);
1628	return;
1629    }
1630    s = spltty();
1631
1632    if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1633	/* try to allocate a keyboard automatically */
1634	if (++kbd_interval >= 25) {
1635	    sc->keyboard = kbd_allocate("*", -1, (void *)&sc->keyboard,
1636					sckbdevent, sc);
1637	    if (sc->keyboard >= 0) {
1638		sc->kbd = kbd_get_keyboard(sc->keyboard);
1639		kbd_ioctl(sc->kbd, KDSKBMODE,
1640			  (caddr_t)&sc->cur_scp->kbd_mode);
1641		update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1642				 LOCK_MASK);
1643	    }
1644	    kbd_interval = 0;
1645	}
1646    }
1647
1648    /* find the vty to update */
1649    scp = sc->cur_scp;
1650
1651    /* should we stop the screen saver? */
1652    getmicrouptime(&tv);
1653    if (debugger > 0 || panicstr || shutdown_in_progress)
1654	sc_touch_scrn_saver();
1655    if (run_scrn_saver) {
1656	if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1657	    sc->flags |= SC_SCRN_IDLE;
1658	else
1659	    sc->flags &= ~SC_SCRN_IDLE;
1660    } else {
1661	sc->scrn_time_stamp = tv.tv_sec;
1662	sc->flags &= ~SC_SCRN_IDLE;
1663	if (scrn_blank_time > 0)
1664	    run_scrn_saver = TRUE;
1665    }
1666#ifdef DEV_SPLASH
1667    if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1668	if (sc->flags & SC_SCRN_BLANKED)
1669            stop_scrn_saver(sc, current_saver);
1670#endif
1671
1672    /* should we just return ? */
1673    if (sc->blink_in_progress || sc->switch_in_progress
1674	|| sc->write_in_progress) {
1675	if (again)
1676	    timeout(scrn_timer, sc, hz / 10);
1677	splx(s);
1678	return;
1679    }
1680
1681    /* Update the screen */
1682    scp = sc->cur_scp;		/* cur_scp may have changed... */
1683    if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1684	scrn_update(scp, TRUE);
1685
1686#ifdef DEV_SPLASH
1687    /* should we activate the screen saver? */
1688    if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1689	if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1690	    (*current_saver)(sc, TRUE);
1691#endif
1692
1693    if (again)
1694	timeout(scrn_timer, sc, hz / 25);
1695    splx(s);
1696}
1697
1698static int
1699and_region(int *s1, int *e1, int s2, int e2)
1700{
1701    if (*e1 < s2 || e2 < *s1)
1702	return FALSE;
1703    *s1 = imax(*s1, s2);
1704    *e1 = imin(*e1, e2);
1705    return TRUE;
1706}
1707
1708static void
1709scrn_update(scr_stat *scp, int show_cursor)
1710{
1711    int start;
1712    int end;
1713    int s;
1714    int e;
1715
1716    /* assert(scp == scp->sc->cur_scp) */
1717
1718    ++scp->sc->videoio_in_progress;
1719
1720#ifndef SC_NO_CUTPASTE
1721    /* remove the previous mouse pointer image if necessary */
1722    if (scp->status & MOUSE_VISIBLE) {
1723	s = scp->mouse_pos;
1724	e = scp->mouse_pos + scp->xsize + 1;
1725	if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1726	    || and_region(&s, &e, scp->start, scp->end)
1727	    || ((scp->status & CURSOR_ENABLED) &&
1728		(scp->cursor_pos != scp->cursor_oldpos) &&
1729		(and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1730		 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1731	    sc_remove_mouse_image(scp);
1732	    if (scp->end >= scp->xsize*scp->ysize)
1733		scp->end = scp->xsize*scp->ysize - 1;
1734	}
1735    }
1736#endif /* !SC_NO_CUTPASTE */
1737
1738#if 1
1739    /* debug: XXX */
1740    if (scp->end >= scp->xsize*scp->ysize) {
1741	printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1742	scp->end = scp->xsize*scp->ysize - 1;
1743    }
1744    if (scp->start < 0) {
1745	printf("scrn_update(): scp->start %d < 0\n", scp->start);
1746	scp->start = 0;
1747    }
1748#endif
1749
1750    /* update screen image */
1751    if (scp->start <= scp->end)  {
1752	if (scp->mouse_cut_end >= 0) {
1753	    /* there is a marked region for cut & paste */
1754	    if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1755		start = scp->mouse_cut_start;
1756		end = scp->mouse_cut_end;
1757	    } else {
1758		start = scp->mouse_cut_end;
1759		end = scp->mouse_cut_start - 1;
1760	    }
1761	    s = start;
1762	    e = end;
1763	    /* does the cut-mark region overlap with the update region? */
1764	    if (and_region(&s, &e, scp->start, scp->end)) {
1765		(*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1766		s = 0;
1767		e = start - 1;
1768		if (and_region(&s, &e, scp->start, scp->end))
1769		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1770		s = end + 1;
1771		e = scp->xsize*scp->ysize - 1;
1772		if (and_region(&s, &e, scp->start, scp->end))
1773		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1774	    } else {
1775		(*scp->rndr->draw)(scp, scp->start,
1776				   scp->end - scp->start + 1, FALSE);
1777	    }
1778	} else {
1779	    (*scp->rndr->draw)(scp, scp->start,
1780			       scp->end - scp->start + 1, FALSE);
1781	}
1782    }
1783
1784    /* we are not to show the cursor and the mouse pointer... */
1785    if (!show_cursor) {
1786        scp->end = 0;
1787        scp->start = scp->xsize*scp->ysize - 1;
1788	--scp->sc->videoio_in_progress;
1789	return;
1790    }
1791
1792    /* update cursor image */
1793    if (scp->status & CURSOR_ENABLED) {
1794	s = scp->start;
1795	e = scp->end;
1796        /* did cursor move since last time ? */
1797        if (scp->cursor_pos != scp->cursor_oldpos) {
1798            /* do we need to remove old cursor image ? */
1799            if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1800                sc_remove_cursor_image(scp);
1801            sc_draw_cursor_image(scp);
1802        } else {
1803            if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1804		/* cursor didn't move, but has been overwritten */
1805		sc_draw_cursor_image(scp);
1806	    else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
1807		/* if it's a blinking cursor, update it */
1808		(*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1809					   sc_inside_cutmark(scp,
1810					       scp->cursor_pos));
1811        }
1812    }
1813
1814#ifndef SC_NO_CUTPASTE
1815    /* update "pseudo" mouse pointer image */
1816    if (scp->sc->flags & SC_MOUSE_ENABLED) {
1817	if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1818	    scp->status &= ~MOUSE_MOVED;
1819	    sc_draw_mouse_image(scp);
1820	}
1821    }
1822#endif /* SC_NO_CUTPASTE */
1823
1824    scp->end = 0;
1825    scp->start = scp->xsize*scp->ysize - 1;
1826
1827    --scp->sc->videoio_in_progress;
1828}
1829
1830#ifdef DEV_SPLASH
1831static int
1832scsplash_callback(int event, void *arg)
1833{
1834    sc_softc_t *sc;
1835    int error;
1836
1837    sc = (sc_softc_t *)arg;
1838
1839    switch (event) {
1840    case SPLASH_INIT:
1841	if (add_scrn_saver(scsplash_saver) == 0) {
1842	    sc->flags &= ~SC_SAVER_FAILED;
1843	    run_scrn_saver = TRUE;
1844	    if (cold && !(boothowto & (RB_VERBOSE | RB_CONFIG))) {
1845		scsplash_stick(TRUE);
1846		(*current_saver)(sc, TRUE);
1847	    }
1848	}
1849	return 0;
1850
1851    case SPLASH_TERM:
1852	if (current_saver == scsplash_saver) {
1853	    scsplash_stick(FALSE);
1854	    error = remove_scrn_saver(scsplash_saver);
1855	    if (error)
1856		return error;
1857	}
1858	return 0;
1859
1860    default:
1861	return EINVAL;
1862    }
1863}
1864
1865static void
1866scsplash_saver(sc_softc_t *sc, int show)
1867{
1868    static int busy = FALSE;
1869    scr_stat *scp;
1870
1871    if (busy)
1872	return;
1873    busy = TRUE;
1874
1875    scp = sc->cur_scp;
1876    if (show) {
1877	if (!(sc->flags & SC_SAVER_FAILED)) {
1878	    if (!(sc->flags & SC_SCRN_BLANKED))
1879		set_scrn_saver_mode(scp, -1, NULL, 0);
1880	    switch (splash(sc->adp, TRUE)) {
1881	    case 0:		/* succeeded */
1882		break;
1883	    case EAGAIN:	/* try later */
1884		restore_scrn_saver_mode(scp, FALSE);
1885		sc_touch_scrn_saver();		/* XXX */
1886		break;
1887	    default:
1888		sc->flags |= SC_SAVER_FAILED;
1889		scsplash_stick(FALSE);
1890		restore_scrn_saver_mode(scp, TRUE);
1891		printf("scsplash_saver(): failed to put up the image\n");
1892		break;
1893	    }
1894	}
1895    } else if (!sticky_splash) {
1896	if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1897	    restore_scrn_saver_mode(scp, TRUE);
1898    }
1899    busy = FALSE;
1900}
1901
1902static int
1903add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1904{
1905#if 0
1906    int error;
1907
1908    if (current_saver != none_saver) {
1909	error = remove_scrn_saver(current_saver);
1910	if (error)
1911	    return error;
1912    }
1913#endif
1914    if (current_saver != none_saver)
1915	return EBUSY;
1916
1917    run_scrn_saver = FALSE;
1918    saver_mode = CONS_LKM_SAVER;
1919    current_saver = this_saver;
1920    return 0;
1921}
1922
1923static int
1924remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1925{
1926    if (current_saver != this_saver)
1927	return EINVAL;
1928
1929#if 0
1930    /*
1931     * In order to prevent `current_saver' from being called by
1932     * the timeout routine `scrn_timer()' while we manipulate
1933     * the saver list, we shall set `current_saver' to `none_saver'
1934     * before stopping the current saver, rather than blocking by `splXX()'.
1935     */
1936    current_saver = none_saver;
1937    if (scrn_blanked)
1938        stop_scrn_saver(this_saver);
1939#endif
1940
1941    /* unblank all blanked screens */
1942    wait_scrn_saver_stop(NULL);
1943    if (scrn_blanked)
1944	return EBUSY;
1945
1946    current_saver = none_saver;
1947    return 0;
1948}
1949
1950static int
1951set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
1952{
1953    int s;
1954
1955    /* assert(scp == scp->sc->cur_scp) */
1956    s = spltty();
1957    if (!ISGRAPHSC(scp))
1958	sc_remove_cursor_image(scp);
1959    scp->splash_save_mode = scp->mode;
1960    scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
1961    scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
1962    scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
1963    scp->sc->flags |= SC_SCRN_BLANKED;
1964    ++scrn_blanked;
1965    splx(s);
1966    if (mode < 0)
1967	return 0;
1968    scp->mode = mode;
1969    if (set_mode(scp) == 0) {
1970	if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
1971	    scp->status |= GRAPHICS_MODE;
1972#ifndef SC_NO_PALETTE_LOADING
1973	if (pal != NULL)
1974	    load_palette(scp->sc->adp, pal);
1975#endif
1976	sc_set_border(scp, border);
1977	return 0;
1978    } else {
1979	s = spltty();
1980	scp->mode = scp->splash_save_mode;
1981	scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
1982	scp->status |= scp->splash_save_status;
1983	splx(s);
1984	return 1;
1985    }
1986}
1987
1988static int
1989restore_scrn_saver_mode(scr_stat *scp, int changemode)
1990{
1991    int mode;
1992    int status;
1993    int s;
1994
1995    /* assert(scp == scp->sc->cur_scp) */
1996    s = spltty();
1997    mode = scp->mode;
1998    status = scp->status;
1999    scp->mode = scp->splash_save_mode;
2000    scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2001    scp->status |= scp->splash_save_status;
2002    scp->sc->flags &= ~SC_SCRN_BLANKED;
2003    if (!changemode) {
2004	if (!ISGRAPHSC(scp))
2005	    sc_draw_cursor_image(scp);
2006	--scrn_blanked;
2007	splx(s);
2008	return 0;
2009    }
2010    if (set_mode(scp) == 0) {
2011#ifndef SC_NO_PALETTE_LOADING
2012	load_palette(scp->sc->adp, scp->sc->palette);
2013#endif
2014	--scrn_blanked;
2015	splx(s);
2016	return 0;
2017    } else {
2018	scp->mode = mode;
2019	scp->status = status;
2020	splx(s);
2021	return 1;
2022    }
2023}
2024
2025static void
2026stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2027{
2028    (*saver)(sc, FALSE);
2029    run_scrn_saver = FALSE;
2030    /* the screen saver may have chosen not to stop after all... */
2031    if (sc->flags & SC_SCRN_BLANKED)
2032	return;
2033
2034    mark_all(sc->cur_scp);
2035    if (sc->delayed_next_scr)
2036	sc_switch_scr(sc, sc->delayed_next_scr - 1);
2037    if (debugger == 0)
2038	wakeup((caddr_t)&scrn_blanked);
2039}
2040
2041static int
2042wait_scrn_saver_stop(sc_softc_t *sc)
2043{
2044    int error = 0;
2045
2046    while (scrn_blanked > 0) {
2047	run_scrn_saver = FALSE;
2048	if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2049	    error = 0;
2050	    break;
2051	}
2052	error = tsleep((caddr_t)&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2053	if ((error != 0) && (error != ERESTART))
2054	    break;
2055    }
2056    run_scrn_saver = FALSE;
2057    return error;
2058}
2059#endif /* DEV_SPLASH */
2060
2061void
2062sc_touch_scrn_saver(void)
2063{
2064    scsplash_stick(FALSE);
2065    run_scrn_saver = FALSE;
2066}
2067
2068int
2069sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2070{
2071    scr_stat *cur_scp;
2072    struct tty *tp;
2073    struct proc *p;
2074    int s;
2075
2076    DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2077
2078    /* delay switch if the screen is blanked or being updated */
2079    if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2080	|| sc->blink_in_progress || sc->videoio_in_progress) {
2081	sc->delayed_next_scr = next_scr + 1;
2082	sc_touch_scrn_saver();
2083	DPRINTF(5, ("switch delayed\n"));
2084	return 0;
2085    }
2086
2087    s = spltty();
2088    cur_scp = sc->cur_scp;
2089
2090    /* we are in the middle of the vty switching process... */
2091    if (sc->switch_in_progress
2092	&& (cur_scp->smode.mode == VT_PROCESS)
2093	&& cur_scp->proc) {
2094	p = pfind(cur_scp->pid);
2095	if (cur_scp->proc != p) {
2096	    if (p)
2097		PROC_UNLOCK(p);
2098	    /*
2099	     * The controlling process has died!!.  Do some clean up.
2100	     * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2101	     * are not reset here yet; they will be cleared later.
2102	     */
2103	    DPRINTF(5, ("cur_scp controlling process %d died, ",
2104	       cur_scp->pid));
2105	    if (cur_scp->status & SWITCH_WAIT_REL) {
2106		/*
2107		 * Force the previous switch to finish, but return now
2108		 * with error.
2109		 */
2110		DPRINTF(5, ("reset WAIT_REL, "));
2111		finish_vt_rel(cur_scp, TRUE, &s);
2112		splx(s);
2113		DPRINTF(5, ("finishing previous switch\n"));
2114		return EINVAL;
2115	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2116		/* let's assume screen switch has been completed. */
2117		DPRINTF(5, ("reset WAIT_ACQ, "));
2118		finish_vt_acq(cur_scp);
2119	    } else {
2120		/*
2121	 	 * We are in between screen release and acquisition, and
2122		 * reached here via scgetc() or scrn_timer() which has
2123		 * interrupted exchange_scr(). Don't do anything stupid.
2124		 */
2125		DPRINTF(5, ("waiting nothing, "));
2126	    }
2127	} else {
2128	    if (p)
2129		PROC_UNLOCK(p);
2130	    /*
2131	     * The controlling process is alive, but not responding...
2132	     * It is either buggy or it may be just taking time.
2133	     * The following code is a gross kludge to cope with this
2134	     * problem for which there is no clean solution. XXX
2135	     */
2136	    if (cur_scp->status & SWITCH_WAIT_REL) {
2137		switch (sc->switch_in_progress++) {
2138		case 1:
2139		    break;
2140		case 2:
2141		    DPRINTF(5, ("sending relsig again, "));
2142		    signal_vt_rel(cur_scp);
2143		    break;
2144		case 3:
2145		    break;
2146		case 4:
2147		default:
2148		    /*
2149		     * Act as if the controlling program returned
2150		     * VT_FALSE.
2151		     */
2152		    DPRINTF(5, ("force reset WAIT_REL, "));
2153		    finish_vt_rel(cur_scp, FALSE, &s);
2154		    splx(s);
2155		    DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2156		    return EINVAL;
2157		}
2158	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2159		switch (sc->switch_in_progress++) {
2160		case 1:
2161		    break;
2162		case 2:
2163		    DPRINTF(5, ("sending acqsig again, "));
2164		    signal_vt_acq(cur_scp);
2165		    break;
2166		case 3:
2167		    break;
2168		case 4:
2169		default:
2170		     /* clear the flag and finish the previous switch */
2171		    DPRINTF(5, ("force reset WAIT_ACQ, "));
2172		    finish_vt_acq(cur_scp);
2173		    break;
2174		}
2175	    }
2176	}
2177    }
2178
2179    /*
2180     * Return error if an invalid argument is given, or vty switch
2181     * is still in progress.
2182     */
2183    if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2184	|| sc->switch_in_progress) {
2185	splx(s);
2186	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2187	DPRINTF(5, ("error 1\n"));
2188	return EINVAL;
2189    }
2190
2191    /*
2192     * Don't allow switching away from the graphics mode vty
2193     * if the switch mode is VT_AUTO, unless the next vty is the same
2194     * as the current or the current vty has been closed (but showing).
2195     */
2196    tp = VIRTUAL_TTY(sc, cur_scp->index);
2197    if ((cur_scp->index != next_scr)
2198	&& ISTTYOPEN(tp)
2199	&& (cur_scp->smode.mode == VT_AUTO)
2200	&& ISGRAPHSC(cur_scp)) {
2201	splx(s);
2202	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2203	DPRINTF(5, ("error, graphics mode\n"));
2204	return EINVAL;
2205    }
2206
2207    /*
2208     * Is the wanted vty open? Don't allow switching to a closed vty.
2209     * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2210     * Note that we always allow the user to switch to the kernel
2211     * console even if it is closed.
2212     */
2213    if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2214	tp = VIRTUAL_TTY(sc, next_scr);
2215	if (!ISTTYOPEN(tp)) {
2216	    splx(s);
2217	    sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2218	    DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2219	    return EINVAL;
2220	}
2221	if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2222	    splx(s);
2223	    DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2224	    return EINVAL;
2225	}
2226    }
2227
2228    /* this is the start of vty switching process... */
2229    ++sc->switch_in_progress;
2230    sc->delayed_next_scr = 0;
2231    sc->old_scp = cur_scp;
2232    sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2233    if (sc->new_scp == sc->old_scp) {
2234	sc->switch_in_progress = 0;
2235	/*
2236	 * XXX wakeup() calls mtx_lock(&sched_lock) which will hang if
2237	 * sched_lock is in an in-between state, e.g., when we stop at
2238	 * a breakpoint at fork_exit.  It has always been wrong to call
2239	 * wakeup() when the debugger is active.  In RELENG_4, wakeup()
2240	 * is supposed to be locked by splhigh(), but the debugger may
2241	 * be invoked at splhigh().
2242	 */
2243	if (debugger == 0)
2244	    wakeup((caddr_t)&sc->new_scp->smode);
2245	splx(s);
2246	DPRINTF(5, ("switch done (new == old)\n"));
2247	return 0;
2248    }
2249
2250    /* has controlling process died? */
2251    vt_proc_alive(sc->old_scp);
2252    vt_proc_alive(sc->new_scp);
2253
2254    /* wait for the controlling process to release the screen, if necessary */
2255    if (signal_vt_rel(sc->old_scp)) {
2256	splx(s);
2257	return 0;
2258    }
2259
2260    /* go set up the new vty screen */
2261    splx(s);
2262    exchange_scr(sc);
2263    s = spltty();
2264
2265    /* wake up processes waiting for this vty */
2266    if (debugger == 0)
2267	wakeup((caddr_t)&sc->cur_scp->smode);
2268
2269    /* wait for the controlling process to acknowledge, if necessary */
2270    if (signal_vt_acq(sc->cur_scp)) {
2271	splx(s);
2272	return 0;
2273    }
2274
2275    sc->switch_in_progress = 0;
2276    if (sc->unit == sc_console_unit)
2277	cons_unavail = FALSE;
2278    splx(s);
2279    DPRINTF(5, ("switch done\n"));
2280
2281    return 0;
2282}
2283
2284static int
2285do_switch_scr(sc_softc_t *sc, int s)
2286{
2287    vt_proc_alive(sc->new_scp);
2288
2289    splx(s);
2290    exchange_scr(sc);
2291    s = spltty();
2292    /* sc->cur_scp == sc->new_scp */
2293    wakeup((caddr_t)&sc->cur_scp->smode);
2294
2295    /* wait for the controlling process to acknowledge, if necessary */
2296    if (!signal_vt_acq(sc->cur_scp)) {
2297	sc->switch_in_progress = 0;
2298	if (sc->unit == sc_console_unit)
2299	    cons_unavail = FALSE;
2300    }
2301
2302    return s;
2303}
2304
2305static int
2306vt_proc_alive(scr_stat *scp)
2307{
2308    struct proc *p;
2309
2310    if (scp->proc) {
2311	if ((p = pfind(scp->pid)) != NULL)
2312	    PROC_UNLOCK(p);
2313	if (scp->proc == p)
2314	    return TRUE;
2315	scp->proc = NULL;
2316	scp->smode.mode = VT_AUTO;
2317	DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2318    }
2319    return FALSE;
2320}
2321
2322static int
2323signal_vt_rel(scr_stat *scp)
2324{
2325    if (scp->smode.mode != VT_PROCESS)
2326	return FALSE;
2327    scp->status |= SWITCH_WAIT_REL;
2328    PROC_LOCK(scp->proc);
2329    psignal(scp->proc, scp->smode.relsig);
2330    PROC_UNLOCK(scp->proc);
2331    DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2332    return TRUE;
2333}
2334
2335static int
2336signal_vt_acq(scr_stat *scp)
2337{
2338    if (scp->smode.mode != VT_PROCESS)
2339	return FALSE;
2340    if (scp->sc->unit == sc_console_unit)
2341	cons_unavail = TRUE;
2342    scp->status |= SWITCH_WAIT_ACQ;
2343    PROC_LOCK(scp->proc);
2344    psignal(scp->proc, scp->smode.acqsig);
2345    PROC_UNLOCK(scp->proc);
2346    DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2347    return TRUE;
2348}
2349
2350static int
2351finish_vt_rel(scr_stat *scp, int release, int *s)
2352{
2353    if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2354	scp->status &= ~SWITCH_WAIT_REL;
2355	if (release)
2356	    *s = do_switch_scr(scp->sc, *s);
2357	else
2358	    scp->sc->switch_in_progress = 0;
2359	return 0;
2360    }
2361    return EINVAL;
2362}
2363
2364static int
2365finish_vt_acq(scr_stat *scp)
2366{
2367    if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2368	scp->status &= ~SWITCH_WAIT_ACQ;
2369	scp->sc->switch_in_progress = 0;
2370	return 0;
2371    }
2372    return EINVAL;
2373}
2374
2375static void
2376exchange_scr(sc_softc_t *sc)
2377{
2378    scr_stat *scp;
2379
2380    /* save the current state of video and keyboard */
2381    sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2382    if (!ISGRAPHSC(sc->old_scp))
2383	sc_remove_cursor_image(sc->old_scp);
2384    if (sc->old_scp->kbd_mode == K_XLATE)
2385	save_kbd_state(sc->old_scp);
2386
2387    /* set up the video for the new screen */
2388    scp = sc->cur_scp = sc->new_scp;
2389    if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2390	set_mode(scp);
2391    else
2392	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2393		    (void *)sc->adp->va_window, FALSE);
2394    scp->status |= MOUSE_HIDDEN;
2395    sc_move_cursor(scp, scp->xpos, scp->ypos);
2396    if (!ISGRAPHSC(scp))
2397	sc_set_cursor_image(scp);
2398#ifndef SC_NO_PALETTE_LOADING
2399    if (ISGRAPHSC(sc->old_scp))
2400	load_palette(sc->adp, sc->palette);
2401#endif
2402    sc_set_border(scp, scp->border);
2403
2404    /* set up the keyboard for the new screen */
2405    if (sc->old_scp->kbd_mode != scp->kbd_mode)
2406	kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2407    update_kbd_state(scp, scp->status, LOCK_MASK);
2408
2409    mark_all(scp);
2410}
2411
2412void
2413sc_puts(scr_stat *scp, u_char *buf, int len)
2414{
2415#ifdef DEV_SPLASH
2416    /* make screensaver happy */
2417    if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2418	run_scrn_saver = FALSE;
2419#endif
2420
2421    if (scp->tsw)
2422	(*scp->tsw->te_puts)(scp, buf, len);
2423
2424    if (scp->sc->delayed_next_scr)
2425	sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2426}
2427
2428void
2429sc_draw_cursor_image(scr_stat *scp)
2430{
2431    /* assert(scp == scp->sc->cur_scp); */
2432    ++scp->sc->videoio_in_progress;
2433    (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2434			      scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2435			      sc_inside_cutmark(scp, scp->cursor_pos));
2436    scp->cursor_oldpos = scp->cursor_pos;
2437    --scp->sc->videoio_in_progress;
2438}
2439
2440void
2441sc_remove_cursor_image(scr_stat *scp)
2442{
2443    /* assert(scp == scp->sc->cur_scp); */
2444    ++scp->sc->videoio_in_progress;
2445    (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2446			      scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2447			      sc_inside_cutmark(scp, scp->cursor_oldpos));
2448    --scp->sc->videoio_in_progress;
2449}
2450
2451static void
2452update_cursor_image(scr_stat *scp)
2453{
2454    /* assert(scp == scp->sc->cur_scp); */
2455    sc_remove_cursor_image(scp);
2456    sc_set_cursor_image(scp);
2457    sc_draw_cursor_image(scp);
2458}
2459
2460void
2461sc_set_cursor_image(scr_stat *scp)
2462{
2463    scp->curs_attr.flags = scp->curr_curs_attr.flags;
2464    if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2465	/* hidden cursor is internally represented as zero-height underline */
2466	scp->curs_attr.flags = CONS_CHAR_CURSOR;
2467	scp->curs_attr.base = scp->curs_attr.height = 0;
2468    } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2469	scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2470				  scp->font_size - 1);
2471	scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2472				    scp->font_size - scp->curs_attr.base);
2473    } else {	/* block cursor */
2474	scp->curs_attr.base = 0;
2475	scp->curs_attr.height = scp->font_size;
2476    }
2477
2478    /* assert(scp == scp->sc->cur_scp); */
2479    ++scp->sc->videoio_in_progress;
2480    (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2481			     scp->curs_attr.flags & CONS_BLINK_CURSOR);
2482    --scp->sc->videoio_in_progress;
2483}
2484
2485static void
2486change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2487{
2488    if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2489	sc_remove_cursor_image(scp);
2490
2491    if (base >= 0)
2492	scp->curr_curs_attr.base = base;
2493    if (height >= 0)
2494	scp->curr_curs_attr.height = height;
2495    if (flags & CONS_RESET_CURSOR)
2496	scp->curr_curs_attr = scp->dflt_curs_attr;
2497    else
2498	scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2499
2500    if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2501	sc_set_cursor_image(scp);
2502	sc_draw_cursor_image(scp);
2503    }
2504}
2505
2506void
2507sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2508{
2509    sc_softc_t *sc;
2510    dev_t dev;
2511    int s;
2512    int i;
2513
2514    s = spltty();
2515    if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2516	/* local (per vty) change */
2517	change_cursor_shape(scp, flags, base, height);
2518	splx(s);
2519	return;
2520    }
2521
2522    /* global change */
2523    sc = scp->sc;
2524    if (base >= 0)
2525	sc->curs_attr.base = base;
2526    if (height >= 0)
2527	sc->curs_attr.height = height;
2528    if (flags != -1) {
2529	if (flags & CONS_RESET_CURSOR)
2530	    sc->curs_attr = sc->dflt_curs_attr;
2531	else
2532	    sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2533    }
2534
2535    for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2536	if ((dev = SC_DEV(sc, i)) == NODEV)
2537	    continue;
2538	if ((scp = SC_STAT(dev)) == NULL)
2539	    continue;
2540	scp->dflt_curs_attr = sc->curs_attr;
2541	change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2542    }
2543    splx(s);
2544}
2545
2546static void
2547scinit(int unit, int flags)
2548{
2549    /*
2550     * When syscons is being initialized as the kernel console, malloc()
2551     * is not yet functional, because various kernel structures has not been
2552     * fully initialized yet.  Therefore, we need to declare the following
2553     * static buffers for the console.  This is less than ideal,
2554     * but is necessry evil for the time being.  XXX
2555     */
2556    static scr_stat main_console;
2557    static dev_t main_devs[MAXCONS];
2558    static struct tty main_tty;
2559    static u_short sc_buffer[ROW*COL];	/* XXX */
2560#ifndef SC_NO_FONT_LOADING
2561    static u_char font_8[256*8];
2562    static u_char font_14[256*14];
2563    static u_char font_16[256*16];
2564#endif
2565
2566    sc_softc_t *sc;
2567    scr_stat *scp;
2568    video_adapter_t *adp;
2569    int col;
2570    int row;
2571    int i;
2572
2573    /* one time initialization */
2574    if (init_done == COLD)
2575	sc_get_bios_values(&bios_value);
2576    init_done = WARM;
2577
2578    /*
2579     * Allocate resources.  Even if we are being called for the second
2580     * time, we must allocate them again, because they might have
2581     * disappeared...
2582     */
2583    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2584    adp = NULL;
2585    if (sc->adapter >= 0) {
2586	vid_release(sc->adp, (void *)&sc->adapter);
2587	adp = sc->adp;
2588	sc->adp = NULL;
2589    }
2590    if (sc->keyboard >= 0) {
2591	DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2592	i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2593	DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2594	if (sc->kbd != NULL) {
2595	    DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2596		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2597	}
2598	sc->kbd = NULL;
2599    }
2600    sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2601    sc->adp = vid_get_adapter(sc->adapter);
2602    /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2603    sc->keyboard = kbd_allocate("*", unit, (void *)&sc->keyboard,
2604				sckbdevent, sc);
2605    DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2606    sc->kbd = kbd_get_keyboard(sc->keyboard);
2607    if (sc->kbd != NULL) {
2608	DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2609		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2610    }
2611
2612    if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2613
2614	sc->initial_mode = sc->adp->va_initial_mode;
2615
2616#ifndef SC_NO_FONT_LOADING
2617	if (flags & SC_KERNEL_CONSOLE) {
2618	    sc->font_8 = font_8;
2619	    sc->font_14 = font_14;
2620	    sc->font_16 = font_16;
2621	} else if (sc->font_8 == NULL) {
2622	    /* assert(sc_malloc) */
2623	    sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2624	    sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2625	    sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2626	}
2627#endif
2628
2629	/* extract the hardware cursor location and hide the cursor for now */
2630	(*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2631	(*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2632
2633	/* set up the first console */
2634	sc->first_vty = unit*MAXCONS;
2635	sc->vtys = MAXCONS;		/* XXX: should be configurable */
2636	if (flags & SC_KERNEL_CONSOLE) {
2637	    sc->dev = main_devs;
2638	    sc->dev[0] = makedev(CDEV_MAJOR, unit*MAXCONS);
2639	    sc->dev[0]->si_tty = &main_tty;
2640	    ttyregister(&main_tty);
2641	    scp = &main_console;
2642	    init_scp(sc, sc->first_vty, scp);
2643	    sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2644			(void *)sc_buffer, FALSE);
2645	    if (sc_init_emulator(scp, SC_DFLT_TERM))
2646		sc_init_emulator(scp, "*");
2647	    (*scp->tsw->te_default_attr)(scp,
2648					 kernel_default.std_color,
2649					 kernel_default.rev_color);
2650	} else {
2651	    /* assert(sc_malloc) */
2652	    sc->dev = malloc(sizeof(dev_t)*sc->vtys, M_DEVBUF, M_WAITOK|M_ZERO);
2653	    sc->dev[0] = makedev(CDEV_MAJOR, unit*MAXCONS);
2654	    sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
2655	    scp = alloc_scp(sc, sc->first_vty);
2656	}
2657	SC_STAT(sc->dev[0]) = scp;
2658	sc->cur_scp = scp;
2659
2660	/* copy screen to temporary buffer */
2661	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2662		    (void *)scp->sc->adp->va_window, FALSE);
2663	if (ISTEXTSC(scp))
2664	    sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2665
2666	/* move cursors to the initial positions */
2667	if (col >= scp->xsize)
2668	    col = 0;
2669	if (row >= scp->ysize)
2670	    row = scp->ysize - 1;
2671	scp->xpos = col;
2672	scp->ypos = row;
2673	scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2674
2675	if (bios_value.cursor_end < scp->font_size)
2676	    sc->dflt_curs_attr.base = scp->font_size -
2677					  bios_value.cursor_end - 1;
2678	else
2679	    sc->dflt_curs_attr.base = 0;
2680	i = bios_value.cursor_end - bios_value.cursor_start + 1;
2681	sc->dflt_curs_attr.height = imin(i, scp->font_size);
2682	sc->dflt_curs_attr.flags = 0;
2683	sc->curs_attr = sc->dflt_curs_attr;
2684	scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2685
2686#ifndef SC_NO_SYSMOUSE
2687	sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2688#endif
2689	if (!ISGRAPHSC(scp)) {
2690    	    sc_set_cursor_image(scp);
2691    	    sc_draw_cursor_image(scp);
2692	}
2693
2694	/* save font and palette */
2695#ifndef SC_NO_FONT_LOADING
2696	sc->fonts_loaded = 0;
2697	if (ISFONTAVAIL(sc->adp->va_flags)) {
2698#ifdef SC_DFLT_FONT
2699	    bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2700	    bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2701	    bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2702	    sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2703	    if (scp->font_size < 14) {
2704		sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
2705	    } else if (scp->font_size >= 16) {
2706		sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
2707	    } else {
2708		sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
2709	    }
2710#else /* !SC_DFLT_FONT */
2711	    if (scp->font_size < 14) {
2712		sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
2713		sc->fonts_loaded = FONT_8;
2714	    } else if (scp->font_size >= 16) {
2715		sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
2716		sc->fonts_loaded = FONT_16;
2717	    } else {
2718		sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
2719		sc->fonts_loaded = FONT_14;
2720	    }
2721#endif /* SC_DFLT_FONT */
2722	    /* FONT KLUDGE: always use the font page #0. XXX */
2723	    sc_show_font(scp, 0);
2724	}
2725#endif /* !SC_NO_FONT_LOADING */
2726
2727#ifndef SC_NO_PALETTE_LOADING
2728	save_palette(sc->adp, sc->palette);
2729#endif
2730
2731#ifdef DEV_SPLASH
2732	if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
2733	    /* we are ready to put up the splash image! */
2734	    splash_init(sc->adp, scsplash_callback, sc);
2735	    sc->flags |= SC_SPLASH_SCRN;
2736	}
2737#endif
2738    }
2739
2740    /* the rest is not necessary, if we have done it once */
2741    if (sc->flags & SC_INIT_DONE)
2742	return;
2743
2744    /* initialize mapscrn arrays to a one to one map */
2745    for (i = 0; i < sizeof(sc->scr_map); i++)
2746	sc->scr_map[i] = sc->scr_rmap[i] = i;
2747
2748    sc->flags |= SC_INIT_DONE;
2749}
2750
2751#if __i386__ || __ia64__
2752static void
2753scterm(int unit, int flags)
2754{
2755    sc_softc_t *sc;
2756    scr_stat *scp;
2757
2758    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2759    if (sc == NULL)
2760	return;			/* shouldn't happen */
2761
2762#ifdef DEV_SPLASH
2763    /* this console is no longer available for the splash screen */
2764    if (sc->flags & SC_SPLASH_SCRN) {
2765	splash_term(sc->adp);
2766	sc->flags &= ~SC_SPLASH_SCRN;
2767    }
2768#endif
2769
2770#if 0 /* XXX */
2771    /* move the hardware cursor to the upper-left corner */
2772    (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
2773#endif
2774
2775    /* release the keyboard and the video card */
2776    if (sc->keyboard >= 0)
2777	kbd_release(sc->kbd, &sc->keyboard);
2778    if (sc->adapter >= 0)
2779	vid_release(sc->adp, &sc->adapter);
2780
2781    /* stop the terminal emulator, if any */
2782    scp = SC_STAT(sc->dev[0]);
2783    if (scp->tsw)
2784	(*scp->tsw->te_term)(scp, &scp->ts);
2785    if (scp->ts != NULL)
2786	free(scp->ts, M_DEVBUF);
2787
2788    /* clear the structure */
2789    if (!(flags & SC_KERNEL_CONSOLE)) {
2790	/* XXX: We need delete_dev() for this */
2791	free(sc->dev, M_DEVBUF);
2792#if 0
2793	/* XXX: We need a ttyunregister for this */
2794	free(sc->tty, M_DEVBUF);
2795#endif
2796#ifndef SC_NO_FONT_LOADING
2797	free(sc->font_8, M_DEVBUF);
2798	free(sc->font_14, M_DEVBUF);
2799	free(sc->font_16, M_DEVBUF);
2800#endif
2801	/* XXX vtb, history */
2802    }
2803    bzero(sc, sizeof(*sc));
2804    sc->keyboard = -1;
2805    sc->adapter = -1;
2806}
2807#endif
2808
2809static void
2810scshutdown(void *arg, int howto)
2811{
2812    /* assert(sc_console != NULL) */
2813
2814    sc_touch_scrn_saver();
2815    if (!cold && sc_console
2816	&& sc_console->sc->cur_scp->smode.mode == VT_AUTO
2817	&& sc_console->smode.mode == VT_AUTO)
2818	sc_switch_scr(sc_console->sc, sc_console->index);
2819    shutdown_in_progress = TRUE;
2820}
2821
2822int
2823sc_clean_up(scr_stat *scp)
2824{
2825#ifdef DEV_SPLASH
2826    int error;
2827#endif
2828
2829    if (scp->sc->flags & SC_SCRN_BLANKED) {
2830	sc_touch_scrn_saver();
2831#ifdef DEV_SPLASH
2832	if ((error = wait_scrn_saver_stop(scp->sc)))
2833	    return error;
2834#endif
2835    }
2836    scp->status |= MOUSE_HIDDEN;
2837    sc_remove_mouse_image(scp);
2838    sc_remove_cutmarking(scp);
2839    return 0;
2840}
2841
2842void
2843sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
2844{
2845    sc_vtb_t new;
2846    sc_vtb_t old;
2847
2848    old = scp->vtb;
2849    sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
2850    if (!discard && (old.vtb_flags & VTB_VALID)) {
2851	/* retain the current cursor position and buffer contants */
2852	scp->cursor_oldpos = scp->cursor_pos;
2853	/*
2854	 * This works only if the old buffer has the same size as or larger
2855	 * than the new one. XXX
2856	 */
2857	sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
2858	scp->vtb = new;
2859    } else {
2860	scp->vtb = new;
2861	sc_vtb_destroy(&old);
2862    }
2863
2864#ifndef SC_NO_SYSMOUSE
2865    /* move the mouse cursor at the center of the screen */
2866    sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
2867#endif
2868}
2869
2870static scr_stat
2871*alloc_scp(sc_softc_t *sc, int vty)
2872{
2873    scr_stat *scp;
2874
2875    /* assert(sc_malloc) */
2876
2877    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2878    init_scp(sc, vty, scp);
2879
2880    sc_alloc_scr_buffer(scp, TRUE, TRUE);
2881    if (sc_init_emulator(scp, SC_DFLT_TERM))
2882	sc_init_emulator(scp, "*");
2883
2884#ifndef SC_NO_CUTPASTE
2885    sc_alloc_cut_buffer(scp, TRUE);
2886#endif
2887
2888#ifndef SC_NO_HISTORY
2889    sc_alloc_history_buffer(scp, 0, 0, TRUE);
2890#endif
2891
2892    return scp;
2893}
2894
2895static void
2896init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
2897{
2898    video_info_t info;
2899
2900    bzero(scp, sizeof(*scp));
2901
2902    scp->index = vty;
2903    scp->sc = sc;
2904    scp->status = 0;
2905    scp->mode = sc->initial_mode;
2906    (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
2907    if (info.vi_flags & V_INFO_GRAPHICS) {
2908	scp->status |= GRAPHICS_MODE;
2909	scp->xpixel = info.vi_width;
2910	scp->ypixel = info.vi_height;
2911	scp->xsize = info.vi_width/info.vi_cwidth;
2912	scp->ysize = info.vi_height/info.vi_cheight;
2913	scp->font_size = 0;
2914	scp->font = NULL;
2915    } else {
2916	scp->xsize = info.vi_width;
2917	scp->ysize = info.vi_height;
2918	scp->xpixel = scp->xsize*info.vi_cwidth;
2919	scp->ypixel = scp->ysize*info.vi_cheight;
2920	if (info.vi_cheight < 14) {
2921	    scp->font_size = 8;
2922#ifndef SC_NO_FONT_LOADING
2923	    scp->font = sc->font_8;
2924#else
2925	    scp->font = NULL;
2926#endif
2927	} else if (info.vi_cheight >= 16) {
2928	    scp->font_size = 16;
2929#ifndef SC_NO_FONT_LOADING
2930	    scp->font = sc->font_16;
2931#else
2932	    scp->font = NULL;
2933#endif
2934	} else {
2935	    scp->font_size = 14;
2936#ifndef SC_NO_FONT_LOADING
2937	    scp->font = sc->font_14;
2938#else
2939	    scp->font = NULL;
2940#endif
2941	}
2942    }
2943    sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
2944    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
2945    scp->xoff = scp->yoff = 0;
2946    scp->xpos = scp->ypos = 0;
2947    scp->start = scp->xsize * scp->ysize - 1;
2948    scp->end = 0;
2949    scp->tsw = NULL;
2950    scp->ts = NULL;
2951    scp->rndr = NULL;
2952    scp->border = BG_BLACK;
2953    scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2954    scp->mouse_cut_start = scp->xsize*scp->ysize;
2955    scp->mouse_cut_end = -1;
2956    scp->mouse_signal = 0;
2957    scp->mouse_pid = 0;
2958    scp->mouse_proc = NULL;
2959    scp->kbd_mode = K_XLATE;
2960    scp->bell_pitch = bios_value.bell_pitch;
2961    scp->bell_duration = BELL_DURATION;
2962    scp->status |= (bios_value.shift_state & NLKED);
2963    scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
2964    scp->pid = 0;
2965    scp->proc = NULL;
2966    scp->smode.mode = VT_AUTO;
2967    scp->history = NULL;
2968    scp->history_pos = 0;
2969    scp->history_size = 0;
2970}
2971
2972int
2973sc_init_emulator(scr_stat *scp, char *name)
2974{
2975    sc_term_sw_t *sw;
2976    sc_rndr_sw_t *rndr;
2977    void *p;
2978    int error;
2979
2980    if (name == NULL)	/* if no name is given, use the current emulator */
2981	sw = scp->tsw;
2982    else		/* ...otherwise find the named emulator */
2983	sw = sc_term_match(name);
2984    if (sw == NULL)
2985	return EINVAL;
2986
2987    rndr = NULL;
2988    if (strcmp(sw->te_renderer, "*") != 0) {
2989	rndr = sc_render_match(scp, sw->te_renderer,
2990			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2991    }
2992    if (rndr == NULL) {
2993	rndr = sc_render_match(scp, scp->sc->adp->va_name,
2994			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2995	if (rndr == NULL)
2996	    return ENODEV;
2997    }
2998
2999    if (sw == scp->tsw) {
3000	error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3001	scp->rndr = rndr;
3002	sc_clear_screen(scp);
3003	/* assert(error == 0); */
3004	return error;
3005    }
3006
3007    if (sc_malloc && (sw->te_size > 0))
3008	p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3009    else
3010	p = NULL;
3011    error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3012    if (error)
3013	return error;
3014
3015    if (scp->tsw)
3016	(*scp->tsw->te_term)(scp, &scp->ts);
3017    if (scp->ts != NULL)
3018	free(scp->ts, M_DEVBUF);
3019    scp->tsw = sw;
3020    scp->ts = p;
3021    scp->rndr = rndr;
3022
3023    /* XXX */
3024    (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3025    sc_clear_screen(scp);
3026
3027    return 0;
3028}
3029
3030/*
3031 * scgetc(flags) - get character from keyboard.
3032 * If flags & SCGETC_CN, then avoid harmful side effects.
3033 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3034 * return NOKEY if there is nothing there.
3035 */
3036static u_int
3037scgetc(sc_softc_t *sc, u_int flags)
3038{
3039    scr_stat *scp;
3040#ifndef SC_NO_HISTORY
3041    struct tty *tp;
3042#endif
3043    u_int c;
3044    int this_scr;
3045    int f;
3046    int i;
3047
3048    if (sc->kbd == NULL)
3049	return NOKEY;
3050
3051next_code:
3052#if 1
3053    /* I don't like this, but... XXX */
3054    if (flags & SCGETC_CN)
3055	sccnupdate(sc->cur_scp);
3056#endif
3057    scp = sc->cur_scp;
3058    /* first see if there is something in the keyboard port */
3059    for (;;) {
3060	c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3061	if (c == ERRKEY) {
3062	    if (!(flags & SCGETC_CN))
3063		sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3064	} else if (c == NOKEY)
3065	    return c;
3066	else
3067	    break;
3068    }
3069
3070    /* make screensaver happy */
3071    if (!(c & RELKEY))
3072	sc_touch_scrn_saver();
3073
3074    if (!(flags & SCGETC_CN))
3075	random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD);
3076
3077    if (scp->kbd_mode != K_XLATE)
3078	return KEYCHAR(c);
3079
3080    /* if scroll-lock pressed allow history browsing */
3081    if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3082
3083	scp->status &= ~CURSOR_ENABLED;
3084	sc_remove_cursor_image(scp);
3085
3086#ifndef SC_NO_HISTORY
3087	if (!(scp->status & BUFFER_SAVED)) {
3088	    scp->status |= BUFFER_SAVED;
3089	    sc_hist_save(scp);
3090	}
3091	switch (c) {
3092	/* FIXME: key codes */
3093	case SPCLKEY | FKEY | F(49):  /* home key */
3094	    sc_remove_cutmarking(scp);
3095	    sc_hist_home(scp);
3096	    goto next_code;
3097
3098	case SPCLKEY | FKEY | F(57):  /* end key */
3099	    sc_remove_cutmarking(scp);
3100	    sc_hist_end(scp);
3101	    goto next_code;
3102
3103	case SPCLKEY | FKEY | F(50):  /* up arrow key */
3104	    sc_remove_cutmarking(scp);
3105	    if (sc_hist_up_line(scp))
3106		if (!(flags & SCGETC_CN))
3107		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3108	    goto next_code;
3109
3110	case SPCLKEY | FKEY | F(58):  /* down arrow key */
3111	    sc_remove_cutmarking(scp);
3112	    if (sc_hist_down_line(scp))
3113		if (!(flags & SCGETC_CN))
3114		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3115	    goto next_code;
3116
3117	case SPCLKEY | FKEY | F(51):  /* page up key */
3118	    sc_remove_cutmarking(scp);
3119	    for (i=0; i<scp->ysize; i++)
3120	    if (sc_hist_up_line(scp)) {
3121		if (!(flags & SCGETC_CN))
3122		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3123		break;
3124	    }
3125	    goto next_code;
3126
3127	case SPCLKEY | FKEY | F(59):  /* page down key */
3128	    sc_remove_cutmarking(scp);
3129	    for (i=0; i<scp->ysize; i++)
3130	    if (sc_hist_down_line(scp)) {
3131		if (!(flags & SCGETC_CN))
3132		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3133		break;
3134	    }
3135	    goto next_code;
3136	}
3137#endif /* SC_NO_HISTORY */
3138    }
3139
3140    /*
3141     * Process and consume special keys here.  Return a plain char code
3142     * or a char code with the META flag or a function key code.
3143     */
3144    if (c & RELKEY) {
3145	/* key released */
3146	/* goto next_code */
3147    } else {
3148	/* key pressed */
3149	if (c & SPCLKEY) {
3150	    c &= ~SPCLKEY;
3151	    switch (KEYCHAR(c)) {
3152	    /* LOCKING KEYS */
3153	    case NLK: case CLK: case ALK:
3154		break;
3155	    case SLK:
3156		kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3157		if (f & SLKED) {
3158		    scp->status |= SLKED;
3159		} else {
3160		    if (scp->status & SLKED) {
3161			scp->status &= ~SLKED;
3162#ifndef SC_NO_HISTORY
3163			if (scp->status & BUFFER_SAVED) {
3164			    if (!sc_hist_restore(scp))
3165				sc_remove_cutmarking(scp);
3166			    scp->status &= ~BUFFER_SAVED;
3167			    scp->status |= CURSOR_ENABLED;
3168			    sc_draw_cursor_image(scp);
3169			}
3170			tp = VIRTUAL_TTY(sc, scp->index);
3171			if (ISTTYOPEN(tp))
3172			    scstart(tp);
3173#endif
3174		    }
3175		}
3176		break;
3177
3178	    case PASTE:
3179#ifndef SC_NO_CUTPASTE
3180		sc_mouse_paste(scp);
3181#endif
3182		break;
3183
3184	    /* NON-LOCKING KEYS */
3185	    case NOP:
3186	    case LSH:  case RSH:  case LCTR: case RCTR:
3187	    case LALT: case RALT: case ASH:  case META:
3188		break;
3189
3190	    case BTAB:
3191		if (!(sc->flags & SC_SCRN_BLANKED))
3192		    return c;
3193		break;
3194
3195	    case SPSC:
3196#ifdef DEV_SPLASH
3197		/* force activatation/deactivation of the screen saver */
3198		if (!(sc->flags & SC_SCRN_BLANKED)) {
3199		    run_scrn_saver = TRUE;
3200		    sc->scrn_time_stamp -= scrn_blank_time;
3201		}
3202		if (cold) {
3203		    /*
3204		     * While devices are being probed, the screen saver need
3205		     * to be invoked explictly. XXX
3206		     */
3207		    if (sc->flags & SC_SCRN_BLANKED) {
3208			scsplash_stick(FALSE);
3209			stop_scrn_saver(sc, current_saver);
3210		    } else {
3211			if (!ISGRAPHSC(scp)) {
3212			    scsplash_stick(TRUE);
3213			    (*current_saver)(sc, TRUE);
3214			}
3215		    }
3216		}
3217#endif /* DEV_SPLASH */
3218		break;
3219
3220	    case RBT:
3221#ifndef SC_DISABLE_REBOOT
3222		shutdown_nice(0);
3223#endif
3224		break;
3225
3226	    case HALT:
3227#ifndef SC_DISABLE_REBOOT
3228		shutdown_nice(RB_HALT);
3229#endif
3230		break;
3231
3232	    case PDWN:
3233#ifndef SC_DISABLE_REBOOT
3234		shutdown_nice(RB_HALT|RB_POWEROFF);
3235#endif
3236		break;
3237
3238	    case SUSP:
3239		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3240		break;
3241	    case STBY:
3242		power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3243		break;
3244
3245	    case DBG:
3246#ifndef SC_DISABLE_DDBKEY
3247#ifdef DDB
3248		Debugger("manual escape to debugger");
3249#else
3250		printf("No debugger in kernel\n");
3251#endif
3252#else /* SC_DISABLE_DDBKEY */
3253		/* do nothing */
3254#endif /* SC_DISABLE_DDBKEY */
3255		break;
3256
3257	    case PNC:
3258		if (enable_panic_key)
3259			panic("Forced by the panic key");
3260		break;
3261
3262	    case NEXT:
3263		this_scr = scp->index;
3264		for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3265			sc->first_vty + i != this_scr;
3266			i = (i + 1)%sc->vtys) {
3267		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3268		    if (ISTTYOPEN(tp)) {
3269			sc_switch_scr(scp->sc, sc->first_vty + i);
3270			break;
3271		    }
3272		}
3273		break;
3274
3275	    case PREV:
3276		this_scr = scp->index;
3277		for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3278			sc->first_vty + i != this_scr;
3279			i = (i + sc->vtys - 1)%sc->vtys) {
3280		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3281		    if (ISTTYOPEN(tp)) {
3282			sc_switch_scr(scp->sc, sc->first_vty + i);
3283			break;
3284		    }
3285		}
3286		break;
3287
3288	    default:
3289		if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3290		    sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3291		    break;
3292		}
3293		/* assert(c & FKEY) */
3294		if (!(sc->flags & SC_SCRN_BLANKED))
3295		    return c;
3296		break;
3297	    }
3298	    /* goto next_code */
3299	} else {
3300	    /* regular keys (maybe MKEY is set) */
3301	    if (!(sc->flags & SC_SCRN_BLANKED))
3302		return c;
3303	}
3304    }
3305
3306    goto next_code;
3307}
3308
3309int
3310scmmap(dev_t dev, vm_offset_t offset, int nprot)
3311{
3312    scr_stat *scp;
3313
3314    scp = SC_STAT(dev);
3315    if (scp != scp->sc->cur_scp)
3316	return -1;
3317    return (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, offset, nprot);
3318}
3319
3320static int
3321save_kbd_state(scr_stat *scp)
3322{
3323    int state;
3324    int error;
3325
3326    error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3327    if (error == ENOIOCTL)
3328	error = ENODEV;
3329    if (error == 0) {
3330	scp->status &= ~LOCK_MASK;
3331	scp->status |= state;
3332    }
3333    return error;
3334}
3335
3336static int
3337update_kbd_state(scr_stat *scp, int new_bits, int mask)
3338{
3339    int state;
3340    int error;
3341
3342    if (mask != LOCK_MASK) {
3343	error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3344	if (error == ENOIOCTL)
3345	    error = ENODEV;
3346	if (error)
3347	    return error;
3348	state &= ~mask;
3349	state |= new_bits & mask;
3350    } else {
3351	state = new_bits & LOCK_MASK;
3352    }
3353    error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3354    if (error == ENOIOCTL)
3355	error = ENODEV;
3356    return error;
3357}
3358
3359static int
3360update_kbd_leds(scr_stat *scp, int which)
3361{
3362    int error;
3363
3364    which &= LOCK_MASK;
3365    error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3366    if (error == ENOIOCTL)
3367	error = ENODEV;
3368    return error;
3369}
3370
3371int
3372set_mode(scr_stat *scp)
3373{
3374    video_info_t info;
3375
3376    /* reject unsupported mode */
3377    if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
3378	return 1;
3379
3380    /* if this vty is not currently showing, do nothing */
3381    if (scp != scp->sc->cur_scp)
3382	return 0;
3383
3384    /* setup video hardware for the given mode */
3385    (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3386    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3387		(void *)scp->sc->adp->va_window, FALSE);
3388
3389#ifndef SC_NO_FONT_LOADING
3390    /* load appropriate font */
3391    if (!(scp->status & GRAPHICS_MODE)) {
3392	if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3393	    if (scp->font_size < 14) {
3394		if (scp->sc->fonts_loaded & FONT_8)
3395		    sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
3396	    } else if (scp->font_size >= 16) {
3397		if (scp->sc->fonts_loaded & FONT_16)
3398		    sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
3399	    } else {
3400		if (scp->sc->fonts_loaded & FONT_14)
3401		    sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
3402	    }
3403	    /*
3404	     * FONT KLUDGE:
3405	     * This is an interim kludge to display correct font.
3406	     * Always use the font page #0 on the video plane 2.
3407	     * Somehow we cannot show the font in other font pages on
3408	     * some video cards... XXX
3409	     */
3410	    sc_show_font(scp, 0);
3411	}
3412	mark_all(scp);
3413    }
3414#endif /* !SC_NO_FONT_LOADING */
3415
3416    sc_set_border(scp, scp->border);
3417    sc_set_cursor_image(scp);
3418
3419    return 0;
3420}
3421
3422void
3423sc_set_border(scr_stat *scp, int color)
3424{
3425    ++scp->sc->videoio_in_progress;
3426    (*scp->rndr->draw_border)(scp, color);
3427    --scp->sc->videoio_in_progress;
3428}
3429
3430#ifndef SC_NO_FONT_LOADING
3431void
3432sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
3433	     int base, int count)
3434{
3435    sc_softc_t *sc;
3436
3437    sc = scp->sc;
3438    sc->font_loading_in_progress = TRUE;
3439    (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
3440    sc->font_loading_in_progress = FALSE;
3441}
3442
3443void
3444sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
3445	     int base, int count)
3446{
3447    sc_softc_t *sc;
3448
3449    sc = scp->sc;
3450    sc->font_loading_in_progress = TRUE;
3451    (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
3452    sc->font_loading_in_progress = FALSE;
3453}
3454
3455void
3456sc_show_font(scr_stat *scp, int page)
3457{
3458    (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3459}
3460#endif /* !SC_NO_FONT_LOADING */
3461
3462void
3463sc_paste(scr_stat *scp, u_char *p, int count)
3464{
3465    struct tty *tp;
3466    u_char *rmap;
3467
3468    tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3469    if (!ISTTYOPEN(tp))
3470	return;
3471    rmap = scp->sc->scr_rmap;
3472    for (; count > 0; --count)
3473	(*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3474}
3475
3476void
3477sc_bell(scr_stat *scp, int pitch, int duration)
3478{
3479    if (cold || shutdown_in_progress)
3480	return;
3481
3482    if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3483	return;
3484
3485    if (scp->sc->flags & SC_VISUAL_BELL) {
3486	if (scp->sc->blink_in_progress)
3487	    return;
3488	scp->sc->blink_in_progress = 3;
3489	if (scp != scp->sc->cur_scp)
3490	    scp->sc->blink_in_progress += 2;
3491	blink_screen(scp->sc->cur_scp);
3492    } else {
3493	if (scp != scp->sc->cur_scp)
3494	    pitch *= 2;
3495	sysbeep(pitch, duration);
3496    }
3497}
3498
3499static void
3500blink_screen(void *arg)
3501{
3502    scr_stat *scp = arg;
3503    struct tty *tp;
3504
3505    if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3506	scp->sc->blink_in_progress = 0;
3507    	mark_all(scp);
3508	tp = VIRTUAL_TTY(scp->sc, scp->index);
3509	if (ISTTYOPEN(tp))
3510	    scstart(tp);
3511	if (scp->sc->delayed_next_scr)
3512	    sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3513    }
3514    else {
3515	(*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3516			   scp->sc->blink_in_progress & 1);
3517	scp->sc->blink_in_progress--;
3518	timeout(blink_screen, scp, hz / 10);
3519    }
3520}
3521