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