1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1998 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The DragonFly Project
8 * by Sascha Wildner <saw@online.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer as
15 *    the first lines of this file unmodified.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#include "opt_syscons.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/conf.h>
38#include <sys/signalvar.h>
39#include <sys/tty.h>
40#include <sys/kernel.h>
41#include <sys/fbio.h>
42#include <sys/consio.h>
43#include <sys/filedesc.h>
44#include <sys/lock.h>
45#include <sys/sx.h>
46#include <sys/mutex.h>
47#include <sys/proc.h>
48
49#include <dev/fb/fbreg.h>
50#include <dev/syscons/syscons.h>
51
52SET_DECLARE(scrndr_set, const sc_renderer_t);
53
54/* for compatibility with previous versions */
55/* 3.0-RELEASE used the following structure */
56typedef struct old_video_adapter {
57    int			va_index;
58    int			va_type;
59    int			va_flags;
60/* flag bits are the same as the -CURRENT
61#define V_ADP_COLOR	(1<<0)
62#define V_ADP_MODECHANGE (1<<1)
63#define V_ADP_STATESAVE	(1<<2)
64#define V_ADP_STATELOAD	(1<<3)
65#define V_ADP_FONT	(1<<4)
66#define V_ADP_PALETTE	(1<<5)
67#define V_ADP_BORDER	(1<<6)
68#define V_ADP_VESA	(1<<7)
69*/
70    int			va_crtc_addr;
71    u_int		va_window;	/* virtual address */
72    size_t		va_window_size;
73    size_t		va_window_gran;
74    u_int		va_buffer;	/* virtual address */
75    size_t		va_buffer_size;
76    int			va_initial_mode;
77    int			va_initial_bios_mode;
78    int			va_mode;
79} old_video_adapter_t;
80
81#define OLD_CONS_ADPINFO _IOWR('c', 101, old_video_adapter_t)
82
83/* 3.1-RELEASE used the following structure */
84typedef struct old_video_adapter_info {
85    int			va_index;
86    int			va_type;
87    char		va_name[16];
88    int			va_unit;
89    int			va_flags;
90    int			va_io_base;
91    int			va_io_size;
92    int			va_crtc_addr;
93    int			va_mem_base;
94    int			va_mem_size;
95    u_int		va_window;	/* virtual address */
96    size_t		va_window_size;
97    size_t		va_window_gran;
98    u_int		va_buffer;
99    size_t		va_buffer_size;
100    int			va_initial_mode;
101    int			va_initial_bios_mode;
102    int			va_mode;
103    int			va_line_width;
104} old_video_adapter_info_t;
105
106#define OLD_CONS_ADPINFO2 _IOWR('c', 101, old_video_adapter_info_t)
107
108/* 3.0-RELEASE and 3.1-RELEASE used the following structure */
109typedef struct old_video_info {
110    int			vi_mode;
111    int			vi_flags;
112/* flag bits are the same as the -CURRENT
113#define V_INFO_COLOR	(1<<0)
114#define V_INFO_GRAPHICS	(1<<1)
115#define V_INFO_LINEAR	(1<<2)
116#define V_INFO_VESA	(1<<3)
117*/
118    int			vi_width;
119    int			vi_height;
120    int			vi_cwidth;
121    int			vi_cheight;
122    int			vi_depth;
123    int			vi_planes;
124    u_int		vi_window;	/* physical address */
125    size_t		vi_window_size;
126    size_t		vi_window_gran;
127    u_int		vi_buffer;	/* physical address */
128    size_t		vi_buffer_size;
129} old_video_info_t;
130
131#define OLD_CONS_MODEINFO _IOWR('c', 102, old_video_info_t)
132#define OLD_CONS_FINDMODE _IOWR('c', 103, old_video_info_t)
133
134int
135sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
136		 int fontsize, int fontwidth)
137{
138    video_info_t info;
139    struct winsize wsz;
140    u_char *font;
141#ifndef SC_NO_HISTORY
142    int prev_ysize;
143#endif
144    int error;
145    int s;
146
147    if (vidd_get_info(scp->sc->adp, mode, &info))
148	return ENODEV;
149
150    /* adjust argument values */
151    if (fontwidth <= 0)
152	fontwidth = info.vi_cwidth;
153    if (fontsize <= 0)
154	fontsize = info.vi_cheight;
155    if (fontsize < 14)
156	fontsize = 8;
157    else if (fontsize >= 16)
158	fontsize = 16;
159    else
160	fontsize = 14;
161#ifndef SC_NO_FONT_LOADING
162    switch (fontsize) {
163    case 8:
164	if ((scp->sc->fonts_loaded & FONT_8) == 0)
165	    return (EINVAL);
166	font = scp->sc->font_8;
167	break;
168    case 14:
169	if ((scp->sc->fonts_loaded & FONT_14) == 0)
170	    return (EINVAL);
171	font = scp->sc->font_14;
172	break;
173    case 16:
174	if ((scp->sc->fonts_loaded & FONT_16) == 0)
175	    return (EINVAL);
176	font = scp->sc->font_16;
177	break;
178    }
179#else
180    font = NULL;
181#endif
182    if ((xsize <= 0) || (xsize > info.vi_width))
183	xsize = info.vi_width;
184    if ((ysize <= 0) || (ysize > info.vi_height))
185	ysize = info.vi_height;
186
187    /* stop screen saver, etc */
188    s = spltty();
189    if ((error = sc_clean_up(scp))) {
190	splx(s);
191	return error;
192    }
193
194    if (sc_render_match(scp, scp->sc->adp->va_name, 0) == NULL) {
195	splx(s);
196	return ENODEV;
197    }
198
199    /* set up scp */
200#ifndef SC_NO_HISTORY
201    if (scp->history != NULL)
202	sc_hist_save(scp);
203    prev_ysize = scp->ysize;
204#endif
205    /*
206     * This is a kludge to fend off scrn_update() while we
207     * muck around with scp. XXX
208     */
209    scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
210    scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
211    scp->mode = mode;
212    scp->xsize = xsize;
213    scp->ysize = ysize;
214    scp->xoff = 0;
215    scp->yoff = 0;
216    scp->xpixel = scp->xsize*8;
217    scp->ypixel = scp->ysize*fontsize;
218    scp->font = font;
219    scp->font_size = fontsize;
220    scp->font_width = fontwidth;
221
222    /* allocate buffers */
223    sc_alloc_scr_buffer(scp, TRUE, TRUE);
224    sc_init_emulator(scp, NULL);
225#ifndef SC_NO_CUTPASTE
226    sc_alloc_cut_buffer(scp, FALSE);
227#endif
228#ifndef SC_NO_HISTORY
229    sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
230#endif
231    splx(s);
232
233    if (scp == scp->sc->cur_scp)
234	set_mode(scp);
235    scp->status &= ~UNKNOWN_MODE;
236
237    if (tp == NULL)
238	return 0;
239    wsz.ws_col = scp->xsize;
240    wsz.ws_row = scp->ysize;
241    tty_set_winsize(tp, &wsz);
242    return 0;
243}
244
245int
246sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
247{
248#ifdef SC_NO_MODE_CHANGE
249    return ENODEV;
250#else
251    video_info_t info;
252    struct winsize wsz;
253    int error;
254    int s;
255
256    if (vidd_get_info(scp->sc->adp, mode, &info))
257	return ENODEV;
258
259    /* stop screen saver, etc */
260    s = spltty();
261    if ((error = sc_clean_up(scp))) {
262	splx(s);
263	return error;
264    }
265
266    if (sc_render_match(scp, scp->sc->adp->va_name, GRAPHICS_MODE) == NULL) {
267	splx(s);
268	return ENODEV;
269    }
270
271    /* set up scp */
272    scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
273    scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
274    scp->mode = mode;
275    /*
276     * Don't change xsize and ysize; preserve the previous vty
277     * and history buffers.
278     */
279    scp->xoff = 0;
280    scp->yoff = 0;
281    scp->xpixel = info.vi_width;
282    scp->ypixel = info.vi_height;
283    scp->font = NULL;
284    scp->font_size = 0;
285#ifndef SC_NO_SYSMOUSE
286    /* move the mouse cursor at the center of the screen */
287    sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
288#endif
289    sc_init_emulator(scp, NULL);
290    splx(s);
291
292    if (scp == scp->sc->cur_scp)
293	set_mode(scp);
294    /* clear_graphics();*/
295    scp->status &= ~UNKNOWN_MODE;
296
297    if (tp == NULL)
298	return 0;
299    wsz.ws_col = scp->xsize;
300    wsz.ws_row = scp->ysize;
301    tty_set_winsize(tp, &wsz);
302    return 0;
303#endif /* SC_NO_MODE_CHANGE */
304}
305
306int
307sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
308		  int fontsize, int fontwidth)
309{
310#ifndef SC_PIXEL_MODE
311    return ENODEV;
312#else
313    video_info_t info;
314    struct winsize wsz;
315    u_char *font;
316#ifndef SC_NO_HISTORY
317    int prev_ysize;
318#endif
319    int error;
320    int s;
321
322    if (vidd_get_info(scp->sc->adp, scp->mode, &info))
323	return ENODEV;		/* this shouldn't happen */
324
325    /* adjust argument values */
326    if (fontsize <= 0)
327	fontsize = info.vi_cheight;
328    if (fontsize < 14)
329	fontsize = 8;
330    else if (fontsize >= 16)
331	fontsize = 16;
332    else
333	fontsize = 14;
334#ifndef SC_NO_FONT_LOADING
335    switch (fontsize) {
336    case 8:
337	if ((scp->sc->fonts_loaded & FONT_8) == 0)
338	    return (EINVAL);
339	font = scp->sc->font_8;
340	break;
341    case 14:
342	if ((scp->sc->fonts_loaded & FONT_14) == 0)
343	    return (EINVAL);
344	font = scp->sc->font_14;
345	break;
346    case 16:
347	if ((scp->sc->fonts_loaded & FONT_16) == 0)
348	    return (EINVAL);
349	font = scp->sc->font_16;
350	break;
351    }
352#else
353    font = NULL;
354#endif
355    if (xsize <= 0)
356	xsize = info.vi_width/8;
357    if (ysize <= 0)
358	ysize = info.vi_height/fontsize;
359
360    if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
361	return EINVAL;
362
363    if (!sc_support_pixel_mode(&info))
364	return ENODEV;
365
366    /* stop screen saver, etc */
367    s = spltty();
368    if ((error = sc_clean_up(scp))) {
369	splx(s);
370	return error;
371    }
372
373    if (sc_render_match(scp, scp->sc->adp->va_name, PIXEL_MODE) == NULL) {
374	splx(s);
375	return ENODEV;
376    }
377
378#if 0
379    if (scp->tsw)
380	(*scp->tsw->te_term)(scp, scp->ts);
381    scp->tsw = NULL;
382    scp->ts = NULL;
383#endif
384
385    /* set up scp */
386#ifndef SC_NO_HISTORY
387    if (scp->history != NULL)
388	sc_hist_save(scp);
389    prev_ysize = scp->ysize;
390#endif
391    scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
392    scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
393    scp->xsize = xsize;
394    scp->ysize = ysize;
395    scp->xoff = (scp->xpixel/8 - xsize)/2;
396    scp->yoff = (scp->ypixel/fontsize - ysize)/2;
397    scp->font = font;
398    scp->font_size = fontsize;
399    scp->font_width = fontwidth;
400
401    /* allocate buffers */
402    sc_alloc_scr_buffer(scp, TRUE, TRUE);
403    sc_init_emulator(scp, NULL);
404#ifndef SC_NO_CUTPASTE
405    sc_alloc_cut_buffer(scp, FALSE);
406#endif
407#ifndef SC_NO_HISTORY
408    sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
409#endif
410    splx(s);
411
412    if (scp == scp->sc->cur_scp) {
413	sc_set_border(scp, scp->border);
414	sc_set_cursor_image(scp);
415    }
416
417    scp->status &= ~UNKNOWN_MODE;
418
419    if (tp == NULL)
420	return 0;
421    wsz.ws_col = scp->xsize;
422    wsz.ws_row = scp->ysize;
423    tty_set_winsize(tp, &wsz);
424    return 0;
425#endif /* SC_PIXEL_MODE */
426}
427
428int
429sc_support_pixel_mode(void *arg)
430{
431#ifdef SC_PIXEL_MODE
432	video_info_t *info = arg;
433
434	if ((info->vi_flags & V_INFO_GRAPHICS) == 0)
435		return (0);
436
437	/*
438	 * We currently support the following graphic modes:
439	 *
440	 * - 4 bpp planar modes whose memory size does not exceed 64K
441	 * - 15, 16, 24 and 32 bpp linear modes
442	 */
443	switch (info->vi_mem_model) {
444	case V_INFO_MM_PLANAR:
445		if (info->vi_planes != 4)
446			break;
447		/*
448		 * A memory size >64K requires bank switching to access
449		 * the entire screen. XXX
450		 */
451		if (info->vi_width * info->vi_height / 8 > info->vi_window_size)
452			break;
453		return (1);
454	case V_INFO_MM_DIRECT:
455		if ((info->vi_flags & V_INFO_LINEAR) == 0 &&
456		    info->vi_depth != 15 && info->vi_depth != 16 &&
457		    info->vi_depth != 24 && info->vi_depth != 32)
458			break;
459		return (1);
460	case V_INFO_MM_PACKED:
461		if ((info->vi_flags & V_INFO_LINEAR) == 0 &&
462		    info->vi_depth != 8)
463			break;
464		return (1);
465	}
466#endif
467	return (0);
468}
469
470#define fb_ioctl(a, c, d)		\
471	(((a) == NULL) ? ENODEV : 	\
472			 vidd_ioctl((a), (c), (caddr_t)(d)))
473
474int
475sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
476{
477    scr_stat *scp;
478    video_adapter_t *adp;
479    video_info_t info;
480    video_adapter_info_t adp_info;
481    int error;
482    int s;
483#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
484    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
485    int ival;
486#endif
487
488    scp = SC_STAT(tp);
489    if (scp == NULL)		/* tp == SC_MOUSE */
490	return ENOIOCTL;
491    adp = scp->sc->adp;
492    if (adp == NULL)		/* shouldn't happen??? */
493	return ENODEV;
494
495    switch (cmd) {
496    case CONS_CURRENTADP:	/* get current adapter index */
497    case FBIO_ADAPTER:
498	return fb_ioctl(adp, FBIO_ADAPTER, data);
499
500    case CONS_CURRENT:  	/* get current adapter type */
501    case FBIO_ADPTYPE:
502	return fb_ioctl(adp, FBIO_ADPTYPE, data);
503
504    case OLD_CONS_ADPINFO:	/* adapter information (old interface) */
505	if (((old_video_adapter_t *)data)->va_index >= 0) {
506	    adp = vid_get_adapter(((old_video_adapter_t *)data)->va_index);
507	    if (adp == NULL)
508		return ENODEV;
509	}
510	((old_video_adapter_t *)data)->va_index = adp->va_index;
511	((old_video_adapter_t *)data)->va_type = adp->va_type;
512	((old_video_adapter_t *)data)->va_flags = adp->va_flags;
513	((old_video_adapter_t *)data)->va_crtc_addr = adp->va_crtc_addr;
514	((old_video_adapter_t *)data)->va_window = adp->va_window;
515	((old_video_adapter_t *)data)->va_window_size = adp->va_window_size;
516	((old_video_adapter_t *)data)->va_window_gran = adp->va_window_gran;
517	((old_video_adapter_t *)data)->va_buffer = adp->va_buffer;
518	((old_video_adapter_t *)data)->va_buffer_size = adp->va_buffer_size;
519	((old_video_adapter_t *)data)->va_mode = adp->va_mode;
520	((old_video_adapter_t *)data)->va_initial_mode = adp->va_initial_mode;
521	((old_video_adapter_t *)data)->va_initial_bios_mode
522	    = adp->va_initial_bios_mode;
523	return 0;
524
525    case OLD_CONS_ADPINFO2:	/* adapter information (yet another old I/F) */
526	adp_info.va_index = ((old_video_adapter_info_t *)data)->va_index;
527	if (adp_info.va_index >= 0) {
528	    adp = vid_get_adapter(adp_info.va_index);
529	    if (adp == NULL)
530		return ENODEV;
531	}
532	error = fb_ioctl(adp, FBIO_ADPINFO, &adp_info);
533	if (error == 0)
534	    bcopy(&adp_info, data, sizeof(old_video_adapter_info_t));
535	return error;
536
537    case CONS_ADPINFO:		/* adapter information */
538    case FBIO_ADPINFO:
539	if (((video_adapter_info_t *)data)->va_index >= 0) {
540	    adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
541	    if (adp == NULL)
542		return ENODEV;
543	}
544	return fb_ioctl(adp, FBIO_ADPINFO, data);
545
546    case CONS_GET:      	/* get current video mode */
547    case FBIO_GETMODE:
548	*(int *)data = scp->mode;
549	return 0;
550
551#ifndef SC_NO_MODE_CHANGE
552    case FBIO_SETMODE:		/* set video mode */
553	if (!(adp->va_flags & V_ADP_MODECHANGE))
554 	    return ENODEV;
555	info.vi_mode = *(int *)data;
556	error = fb_ioctl(adp, FBIO_MODEINFO, &info);
557	if (error)
558	    return error;
559	if (info.vi_flags & V_INFO_GRAPHICS)
560	    return sc_set_graphics_mode(scp, tp, *(int *)data);
561	else
562	    return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0, 0);
563#endif /* SC_NO_MODE_CHANGE */
564
565    case OLD_CONS_MODEINFO:	/* get mode information (old infterface) */
566	info.vi_mode = ((old_video_info_t *)data)->vi_mode;
567	error = fb_ioctl(adp, FBIO_MODEINFO, &info);
568	if (error == 0)
569	    bcopy(&info, (old_video_info_t *)data, sizeof(old_video_info_t));
570	return error;
571
572    case CONS_MODEINFO:		/* get mode information */
573    case FBIO_MODEINFO:
574	return fb_ioctl(adp, FBIO_MODEINFO, data);
575
576    case OLD_CONS_FINDMODE:	/* find a matching video mode (old interface) */
577	bzero(&info, sizeof(info));
578	bcopy((old_video_info_t *)data, &info, sizeof(old_video_info_t));
579	error = fb_ioctl(adp, FBIO_FINDMODE, &info);
580	if (error == 0)
581	    bcopy(&info, (old_video_info_t *)data, sizeof(old_video_info_t));
582	return error;
583
584    case CONS_FINDMODE:		/* find a matching video mode */
585    case FBIO_FINDMODE:
586	return fb_ioctl(adp, FBIO_FINDMODE, data);
587
588#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
589    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
590    case _IO('c', 104):
591	ival = IOCPARM_IVAL(data);
592	data = (caddr_t)&ival;
593	/* FALLTHROUGH */
594#endif
595    case CONS_SETWINORG:	/* set frame buffer window origin */
596    case FBIO_SETWINORG:
597	if (scp != scp->sc->cur_scp)
598	    return ENODEV;	/* XXX */
599	return fb_ioctl(adp, FBIO_SETWINORG, data);
600
601    case FBIO_GETWINORG:	/* get frame buffer window origin */
602	if (scp != scp->sc->cur_scp)
603	    return ENODEV;	/* XXX */
604	return fb_ioctl(adp, FBIO_GETWINORG, data);
605
606    case FBIO_GETDISPSTART:
607    case FBIO_SETDISPSTART:
608    case FBIO_GETLINEWIDTH:
609    case FBIO_SETLINEWIDTH:
610	if (scp != scp->sc->cur_scp)
611	    return ENODEV;	/* XXX */
612	return fb_ioctl(adp, cmd, data);
613
614    case FBIO_GETPALETTE:
615    case FBIO_SETPALETTE:
616    case FBIOPUTCMAP:
617    case FBIOGETCMAP:
618    case FBIOGTYPE:
619	if (scp != scp->sc->cur_scp)
620	    return ENODEV;	/* XXX */
621	return fb_ioctl(adp, cmd, data);
622
623    case FBIO_BLANK:
624	if (scp != scp->sc->cur_scp)
625	    return ENODEV;	/* XXX */
626	return fb_ioctl(adp, cmd, data);
627
628#ifndef SC_NO_MODE_CHANGE
629    /* generic text modes */
630    case SW_TEXT_80x25:	case SW_TEXT_80x30:
631    case SW_TEXT_80x43: case SW_TEXT_80x50:
632    case SW_TEXT_80x60:
633	/* FALLTHROUGH */
634
635    /* VGA TEXT MODES */
636    case SW_VGA_C40x25:
637    case SW_VGA_C80x25: case SW_VGA_M80x25:
638    case SW_VGA_C80x30: case SW_VGA_M80x30:
639    case SW_VGA_C80x50: case SW_VGA_M80x50:
640    case SW_VGA_C80x60: case SW_VGA_M80x60:
641    case SW_VGA_C90x25: case SW_VGA_M90x25:
642    case SW_VGA_C90x30: case SW_VGA_M90x30:
643    case SW_VGA_C90x43: case SW_VGA_M90x43:
644    case SW_VGA_C90x50: case SW_VGA_M90x50:
645    case SW_VGA_C90x60: case SW_VGA_M90x60:
646    case SW_B40x25:     case SW_C40x25:
647    case SW_B80x25:     case SW_C80x25:
648    case SW_ENH_B40x25: case SW_ENH_C40x25:
649    case SW_ENH_B80x25: case SW_ENH_C80x25:
650    case SW_ENH_B80x43: case SW_ENH_C80x43:
651    case SW_EGAMONO80x25:
652	if (!(adp->va_flags & V_ADP_MODECHANGE))
653 	    return ENODEV;
654	return sc_set_text_mode(scp, tp, cmd & 0xff, 0, 0, 0, 0);
655
656    /* GRAPHICS MODES */
657    case SW_BG320:     case SW_BG640:
658    case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
659    case SW_CG640x350: case SW_ENH_CG640:
660    case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
661    case SW_VGA_MODEX:
662	if (!(adp->va_flags & V_ADP_MODECHANGE))
663	    return ENODEV;
664	return sc_set_graphics_mode(scp, tp, cmd & 0xff);
665#endif /* SC_NO_MODE_CHANGE */
666
667#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
668    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
669    case _IO('K', 10):
670	ival = IOCPARM_IVAL(data);
671	data = (caddr_t)&ival;
672	/* FALLTHROUGH */
673#endif
674    case KDSETMODE:     	/* set current mode of this (virtual) console */
675	switch (*(int *)data) {
676	case KD_TEXT:   	/* switch to TEXT (known) mode */
677	    /*
678	     * If scp->mode is of graphics modes, we don't know which
679	     * text mode to switch back to...
680	     */
681	    if (scp->status & GRAPHICS_MODE)
682		return EINVAL;
683	    /* restore fonts & palette ! */
684#if 0
685#ifndef SC_NO_FONT_LOADING
686	    if (ISFONTAVAIL(adp->va_flags)
687		&& !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
688		/*
689		 * FONT KLUDGE
690		 * Don't load fonts for now... XXX
691		 */
692		if (scp->sc->fonts_loaded & FONT_8)
693		    sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
694		if (scp->sc->fonts_loaded & FONT_14)
695		    sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
696		if (scp->sc->fonts_loaded & FONT_16)
697		    sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
698	    }
699#endif /* SC_NO_FONT_LOADING */
700#endif
701
702#ifndef SC_NO_PALETTE_LOADING
703#ifdef SC_PIXEL_MODE
704	    if (adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
705		vidd_load_palette(adp, scp->sc->palette2);
706	    else
707#endif
708	    vidd_load_palette(adp, scp->sc->palette);
709#endif
710
711	    /* move hardware cursor out of the way */
712	    vidd_set_hw_cursor(adp, -1, -1);
713
714	    /* FALLTHROUGH */
715
716	case KD_TEXT1:  	/* switch to TEXT (known) mode */
717	    /*
718	     * If scp->mode is of graphics modes, we don't know which
719	     * text/pixel mode to switch back to...
720	     */
721	    if (scp->status & GRAPHICS_MODE)
722		return EINVAL;
723	    s = spltty();
724	    if ((error = sc_clean_up(scp))) {
725		splx(s);
726		return error;
727	    }
728	    scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
729	    splx(s);
730	    /* no restore fonts & palette */
731	    if (scp == scp->sc->cur_scp)
732		set_mode(scp);
733	    sc_clear_screen(scp);
734	    scp->status &= ~UNKNOWN_MODE;
735	    return 0;
736
737#ifdef SC_PIXEL_MODE
738	case KD_PIXEL:		/* pixel (raster) display */
739	    if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
740		return EINVAL;
741	    if (scp->status & GRAPHICS_MODE)
742		return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
743					 scp->font_size, scp->font_width);
744	    s = spltty();
745	    if ((error = sc_clean_up(scp))) {
746		splx(s);
747		return error;
748	    }
749	    scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
750	    splx(s);
751	    if (scp == scp->sc->cur_scp) {
752		set_mode(scp);
753#ifndef SC_NO_PALETTE_LOADING
754		if (adp->va_info.vi_mem_model == V_INFO_MM_DIRECT)
755		    vidd_load_palette(adp, scp->sc->palette2);
756		else
757		    vidd_load_palette(adp, scp->sc->palette);
758#endif
759	    }
760	    sc_clear_screen(scp);
761	    scp->status &= ~UNKNOWN_MODE;
762	    return 0;
763#endif /* SC_PIXEL_MODE */
764
765	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
766	    s = spltty();
767	    if ((error = sc_clean_up(scp))) {
768		splx(s);
769		return error;
770	    }
771	    scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
772	    splx(s);
773	    return 0;
774
775	default:
776	    return EINVAL;
777	}
778	/* NOT REACHED */
779
780#ifdef SC_PIXEL_MODE
781    case KDRASTER:		/* set pixel (raster) display mode */
782	if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
783	    return ENODEV;
784	return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
785				 ((int *)data)[2], 8);
786#endif /* SC_PIXEL_MODE */
787
788    case KDGETMODE:     	/* get current mode of this (virtual) console */
789	/*
790	 * From the user program's point of view, KD_PIXEL is the same
791	 * as KD_TEXT...
792	 */
793	*data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
794	return 0;
795
796#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
797    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
798    case _IO('K', 13):
799	ival = IOCPARM_IVAL(data);
800	data = (caddr_t)&ival;
801	/* FALLTHROUGH */
802#endif
803    case KDSBORDER:     	/* set border color of this (virtual) console */
804	scp->border = *(int *)data;
805	if (scp == scp->sc->cur_scp)
806	    sc_set_border(scp, scp->border);
807	return 0;
808    }
809
810    return ENOIOCTL;
811}
812
813static LIST_HEAD(, sc_renderer) sc_rndr_list =
814	LIST_HEAD_INITIALIZER(sc_rndr_list);
815
816int
817sc_render_add(sc_renderer_t *rndr)
818{
819	LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
820	return 0;
821}
822
823int
824sc_render_remove(sc_renderer_t *rndr)
825{
826	/*
827	LIST_REMOVE(rndr, link);
828	*/
829	return EBUSY;	/* XXX */
830}
831
832sc_rndr_sw_t
833*sc_render_match(scr_stat *scp, char *name, int mode)
834{
835	const sc_renderer_t **list;
836	const sc_renderer_t *p;
837
838	if (!LIST_EMPTY(&sc_rndr_list)) {
839		LIST_FOREACH(p, &sc_rndr_list, link) {
840			if ((strcmp(p->name, name) == 0)
841				&& (mode == p->mode)) {
842				scp->status &=
843				    ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
844				return p->rndrsw;
845			}
846		}
847	} else {
848		SET_FOREACH(list, scrndr_set) {
849			p = *list;
850			if ((strcmp(p->name, name) == 0)
851				&& (mode == p->mode)) {
852				scp->status &=
853				    ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
854				return p->rndrsw;
855			}
856		}
857	}
858
859	return NULL;
860}
861