2 * Copyright (c) 1998 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/sys/dev/syscons/scvidctl.c,v 1.19.2.2 2000/05/05 09:16:08 nyan Exp $
27 * $DragonFly: src/sys/dev/misc/syscons/scvidctl.c,v 1.2 2003/06/17 04:28:32 dillon Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/signalvar.h>
37 #include <sys/kernel.h>
39 #include <machine/console.h>
41 #include <dev/fb/fbreg.h>
42 #include <dev/syscons/syscons.h>
44 /* for compatibility with previous versions */
45 /* 3.0-RELEASE used the following structure */
46 typedef struct old_video_adapter {
50 /* flag bits are the same as the -CURRENT
51 #define V_ADP_COLOR (1<<0)
52 #define V_ADP_MODECHANGE (1<<1)
53 #define V_ADP_STATESAVE (1<<2)
54 #define V_ADP_STATELOAD (1<<3)
55 #define V_ADP_FONT (1<<4)
56 #define V_ADP_PALETTE (1<<5)
57 #define V_ADP_BORDER (1<<6)
58 #define V_ADP_VESA (1<<7)
61 u_int va_window; /* virtual address */
62 size_t va_window_size;
63 size_t va_window_gran;
64 u_int va_buffer; /* virtual address */
65 size_t va_buffer_size;
67 int va_initial_bios_mode;
69 } old_video_adapter_t;
71 #define OLD_CONS_ADPINFO _IOWR('c', 101, old_video_adapter_t)
73 /* 3.1-RELEASE used the following structure */
74 typedef struct old_video_adapter_info {
85 u_int va_window; /* virtual address */
86 size_t va_window_size;
87 size_t va_window_gran;
89 size_t va_buffer_size;
91 int va_initial_bios_mode;
94 } old_video_adapter_info_t;
96 #define OLD_CONS_ADPINFO2 _IOWR('c', 101, old_video_adapter_info_t)
98 /* 3.0-RELEASE and 3.1-RELEASE used the following structure */
99 typedef struct old_video_info {
102 /* flag bits are the same as the -CURRENT
103 #define V_INFO_COLOR (1<<0)
104 #define V_INFO_GRAPHICS (1<<1)
105 #define V_INFO_LINEAR (1<<2)
106 #define V_INFO_VESA (1<<3)
114 u_int vi_window; /* physical address */
115 size_t vi_window_size;
116 size_t vi_window_gran;
117 u_int vi_buffer; /* physical address */
118 size_t vi_buffer_size;
121 #define OLD_CONS_MODEINFO _IOWR('c', 102, old_video_info_t)
122 #define OLD_CONS_FINDMODE _IOWR('c', 103, old_video_info_t)
125 sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
134 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
137 /* adjust argument values */
139 fontsize = info.vi_cheight;
142 #ifndef SC_NO_FONT_LOADING
143 if (!(scp->sc->fonts_loaded & FONT_8))
145 font = scp->sc->font_8;
149 } else if (fontsize >= 16) {
151 #ifndef SC_NO_FONT_LOADING
152 if (!(scp->sc->fonts_loaded & FONT_16))
154 font = scp->sc->font_16;
160 #ifndef SC_NO_FONT_LOADING
161 if (!(scp->sc->fonts_loaded & FONT_14))
163 font = scp->sc->font_14;
168 if ((xsize <= 0) || (xsize > info.vi_width))
169 xsize = info.vi_width;
170 if ((ysize <= 0) || (ysize > info.vi_height))
171 ysize = info.vi_height;
173 /* stop screen saver, etc */
175 if ((error = sc_clean_up(scp))) {
180 if (sc_render_match(scp, scp->sc->adp->va_name, 0) == NULL) {
186 #ifndef SC_NO_HISTORY
187 if (scp->history != NULL)
190 prev_ysize = scp->ysize;
192 * This is a kludge to fend off scrn_update() while we
193 * muck around with scp. XXX
195 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
196 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
202 scp->xpixel = scp->xsize*8;
203 scp->ypixel = scp->ysize*fontsize;
205 scp->font_size = fontsize;
207 /* allocate buffers */
208 sc_alloc_scr_buffer(scp, TRUE, TRUE);
209 sc_init_emulator(scp, NULL);
210 #ifndef SC_NO_CUTPASTE
211 sc_alloc_cut_buffer(scp, FALSE);
213 #ifndef SC_NO_HISTORY
214 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
218 if (scp == scp->sc->cur_scp)
220 scp->status &= ~UNKNOWN_MODE;
224 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
225 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
226 if (tp->t_winsize.ws_col != scp->xsize
227 || tp->t_winsize.ws_row != scp->ysize) {
228 tp->t_winsize.ws_col = scp->xsize;
229 tp->t_winsize.ws_row = scp->ysize;
230 pgsignal(tp->t_pgrp, SIGWINCH, 1);
237 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
239 #ifdef SC_NO_MODE_CHANGE
246 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
249 /* stop screen saver, etc */
251 if ((error = sc_clean_up(scp))) {
256 if (sc_render_match(scp, scp->sc->adp->va_name, GRAPHICS_MODE) == NULL) {
262 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
263 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
266 * Don't change xsize and ysize; preserve the previous vty
267 * and history buffers.
271 scp->xpixel = info.vi_width;
272 scp->ypixel = info.vi_height;
275 #ifndef SC_NO_SYSMOUSE
276 /* move the mouse cursor at the center of the screen */
277 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
279 sc_init_emulator(scp, NULL);
282 if (scp == scp->sc->cur_scp)
284 /* clear_graphics();*/
285 scp->status &= ~UNKNOWN_MODE;
289 if (tp->t_winsize.ws_xpixel != scp->xpixel
290 || tp->t_winsize.ws_ypixel != scp->ypixel) {
291 tp->t_winsize.ws_xpixel = scp->xpixel;
292 tp->t_winsize.ws_ypixel = scp->ypixel;
293 pgsignal(tp->t_pgrp, SIGWINCH, 1);
297 #endif /* SC_NO_MODE_CHANGE */
301 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
304 #ifndef SC_PIXEL_MODE
313 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
314 return ENODEV; /* this shouldn't happen */
316 /* adjust argument values */
318 fontsize = info.vi_cheight;
321 #ifndef SC_NO_FONT_LOADING
322 if (!(scp->sc->fonts_loaded & FONT_8))
324 font = scp->sc->font_8;
328 } else if (fontsize >= 16) {
330 #ifndef SC_NO_FONT_LOADING
331 if (!(scp->sc->fonts_loaded & FONT_16))
333 font = scp->sc->font_16;
339 #ifndef SC_NO_FONT_LOADING
340 if (!(scp->sc->fonts_loaded & FONT_14))
342 font = scp->sc->font_14;
348 xsize = info.vi_width/8;
350 ysize = info.vi_height/fontsize;
352 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
355 /* only 16 color, 4 plane modes are supported XXX */
356 if ((info.vi_depth != 4) || (info.vi_planes != 4))
360 * set_pixel_mode() currently does not support video modes whose
361 * memory size is larger than 64K. Because such modes require
362 * bank switching to access the entire screen. XXX
364 if (info.vi_width*info.vi_height/8 > info.vi_window_size)
367 /* stop screen saver, etc */
369 if ((error = sc_clean_up(scp))) {
374 if (sc_render_match(scp, scp->sc->adp->va_name, PIXEL_MODE) == NULL) {
381 (*scp->tsw->te_term)(scp, scp->ts);
387 #ifndef SC_NO_HISTORY
388 if (scp->history != NULL)
391 prev_ysize = scp->ysize;
392 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
393 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
396 scp->xoff = (scp->xpixel/8 - xsize)/2;
397 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
399 scp->font_size = fontsize;
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);
407 #ifndef SC_NO_HISTORY
408 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
412 if (scp == scp->sc->cur_scp) {
413 sc_set_border(scp, scp->border);
414 sc_set_cursor_image(scp);
417 scp->status &= ~UNKNOWN_MODE;
421 if (tp->t_winsize.ws_col != scp->xsize
422 || tp->t_winsize.ws_row != scp->ysize) {
423 tp->t_winsize.ws_col = scp->xsize;
424 tp->t_winsize.ws_row = scp->ysize;
425 pgsignal(tp->t_pgrp, SIGWINCH, 1);
429 #endif /* SC_PIXEL_MODE */
432 #define fb_ioctl(a, c, d) \
433 (((a) == NULL) ? ENODEV : \
434 (*vidsw[(a)->va_index]->ioctl)((a), (c), (caddr_t)(d)))
437 sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p)
440 video_adapter_t *adp;
442 video_adapter_info_t adp_info;
446 scp = SC_STAT(tp->t_dev);
447 if (scp == NULL) /* tp == SC_MOUSE */
450 if (adp == NULL) /* shouldn't happen??? */
455 case CONS_CURRENTADP: /* get current adapter index */
457 return fb_ioctl(adp, FBIO_ADAPTER, data);
459 case CONS_CURRENT: /* get current adapter type */
461 return fb_ioctl(adp, FBIO_ADPTYPE, data);
463 case OLD_CONS_ADPINFO: /* adapter information (old interface) */
464 if (((old_video_adapter_t *)data)->va_index >= 0) {
465 adp = vid_get_adapter(((old_video_adapter_t *)data)->va_index);
469 ((old_video_adapter_t *)data)->va_index = adp->va_index;
470 ((old_video_adapter_t *)data)->va_type = adp->va_type;
471 ((old_video_adapter_t *)data)->va_flags = adp->va_flags;
472 ((old_video_adapter_t *)data)->va_crtc_addr = adp->va_crtc_addr;
473 ((old_video_adapter_t *)data)->va_window = adp->va_window;
474 ((old_video_adapter_t *)data)->va_window_size = adp->va_window_size;
475 ((old_video_adapter_t *)data)->va_window_gran = adp->va_window_gran;
476 ((old_video_adapter_t *)data)->va_buffer = adp->va_buffer;
477 ((old_video_adapter_t *)data)->va_buffer_size = adp->va_buffer_size;
478 ((old_video_adapter_t *)data)->va_mode = adp->va_mode;
479 ((old_video_adapter_t *)data)->va_initial_mode = adp->va_initial_mode;
480 ((old_video_adapter_t *)data)->va_initial_bios_mode
481 = adp->va_initial_bios_mode;
484 case OLD_CONS_ADPINFO2: /* adapter information (yet another old I/F) */
485 adp_info.va_index = ((old_video_adapter_info_t *)data)->va_index;
486 if (adp_info.va_index >= 0) {
487 adp = vid_get_adapter(adp_info.va_index);
491 error = fb_ioctl(adp, FBIO_ADPINFO, &adp_info);
493 bcopy(&adp_info, data, sizeof(old_video_adapter_info_t));
496 case CONS_ADPINFO: /* adapter information */
498 if (((video_adapter_info_t *)data)->va_index >= 0) {
499 adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
503 return fb_ioctl(adp, FBIO_ADPINFO, data);
505 case CONS_GET: /* get current video mode */
507 *(int *)data = scp->mode;
510 #ifndef SC_NO_MODE_CHANGE
511 case FBIO_SETMODE: /* set video mode */
512 if (!(adp->va_flags & V_ADP_MODECHANGE))
514 info.vi_mode = *(int *)data;
515 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
518 if (info.vi_flags & V_INFO_GRAPHICS)
519 return sc_set_graphics_mode(scp, tp, *(int *)data);
521 return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0);
522 #endif /* SC_NO_MODE_CHANGE */
524 case OLD_CONS_MODEINFO: /* get mode information (old infterface) */
525 info.vi_mode = ((old_video_info_t *)data)->vi_mode;
526 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
528 bcopy(&info, (old_video_info_t *)data, sizeof(old_video_info_t));
531 case CONS_MODEINFO: /* get mode information */
533 return fb_ioctl(adp, FBIO_MODEINFO, data);
535 case OLD_CONS_FINDMODE: /* find a matching video mode (old interface) */
536 bzero(&info, sizeof(info));
537 bcopy((old_video_info_t *)data, &info, sizeof(old_video_info_t));
538 error = fb_ioctl(adp, FBIO_FINDMODE, &info);
540 bcopy(&info, (old_video_info_t *)data, sizeof(old_video_info_t));
543 case CONS_FINDMODE: /* find a matching video mode */
545 return fb_ioctl(adp, FBIO_FINDMODE, data);
547 case CONS_SETWINORG: /* set frame buffer window origin */
549 if (scp != scp->sc->cur_scp)
550 return ENODEV; /* XXX */
551 return fb_ioctl(adp, FBIO_SETWINORG, data);
553 case FBIO_GETWINORG: /* get frame buffer window origin */
554 if (scp != scp->sc->cur_scp)
555 return ENODEV; /* XXX */
556 return fb_ioctl(adp, FBIO_GETWINORG, data);
558 case FBIO_GETDISPSTART:
559 case FBIO_SETDISPSTART:
560 case FBIO_GETLINEWIDTH:
561 case FBIO_SETLINEWIDTH:
562 if (scp != scp->sc->cur_scp)
563 return ENODEV; /* XXX */
564 return fb_ioctl(adp, cmd, data);
566 case FBIO_GETPALETTE:
567 case FBIO_SETPALETTE:
579 if (scp != scp->sc->cur_scp)
580 return ENODEV; /* XXX */
581 return fb_ioctl(adp, cmd, data);
583 #ifndef SC_NO_MODE_CHANGE
584 /* generic text modes */
585 case SW_TEXT_80x25: case SW_TEXT_80x30:
586 case SW_TEXT_80x43: case SW_TEXT_80x50:
592 case SW_VGA_C80x25: case SW_VGA_M80x25:
593 case SW_VGA_C80x30: case SW_VGA_M80x30:
594 case SW_VGA_C80x50: case SW_VGA_M80x50:
595 case SW_VGA_C80x60: case SW_VGA_M80x60:
596 case SW_VGA_C90x25: case SW_VGA_M90x25:
597 case SW_VGA_C90x30: case SW_VGA_M90x30:
598 case SW_VGA_C90x43: case SW_VGA_M90x43:
599 case SW_VGA_C90x50: case SW_VGA_M90x50:
600 case SW_VGA_C90x60: case SW_VGA_M90x60:
601 case SW_B40x25: case SW_C40x25:
602 case SW_B80x25: case SW_C80x25:
603 case SW_ENH_B40x25: case SW_ENH_C40x25:
604 case SW_ENH_B80x25: case SW_ENH_C80x25:
605 case SW_ENH_B80x43: case SW_ENH_C80x43:
606 case SW_EGAMONO80x25:
609 /* PC98 TEXT MODES */
613 if (!(adp->va_flags & V_ADP_MODECHANGE))
615 return sc_set_text_mode(scp, tp, cmd & 0xff, 0, 0, 0);
618 case SW_BG320: case SW_BG640:
619 case SW_CG320: case SW_CG320_D: case SW_CG640_E:
620 case SW_CG640x350: case SW_ENH_CG640:
621 case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
624 /* PC98 GRAPHICS MODES */
625 case SW_PC98_EGC640x400: case SW_PC98_PEGC640x400:
626 case SW_PC98_PEGC640x480:
628 if (!(adp->va_flags & V_ADP_MODECHANGE))
630 return sc_set_graphics_mode(scp, tp, cmd & 0xff);
631 #endif /* SC_NO_MODE_CHANGE */
633 case KDSETMODE: /* set current mode of this (virtual) console */
634 switch (*(int *)data) {
635 case KD_TEXT: /* switch to TEXT (known) mode */
637 * If scp->mode is of graphics modes, we don't know which
638 * text mode to switch back to...
640 if (scp->status & GRAPHICS_MODE)
642 /* restore fonts & palette ! */
644 #ifndef SC_NO_FONT_LOADING
645 if (ISFONTAVAIL(adp->va_flags)
646 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
649 * Don't load fonts for now... XXX
651 if (scp->sc->fonts_loaded & FONT_8)
652 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
653 if (scp->sc->fonts_loaded & FONT_14)
654 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
655 if (scp->sc->fonts_loaded & FONT_16)
656 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
658 #endif /* SC_NO_FONT_LOADING */
661 #ifndef SC_NO_PALETTE_LOADING
662 load_palette(adp, scp->sc->palette);
666 /* move hardware cursor out of the way */
667 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
672 case KD_TEXT1: /* switch to TEXT (known) mode */
674 * If scp->mode is of graphics modes, we don't know which
675 * text/pixel mode to switch back to...
677 if (scp->status & GRAPHICS_MODE)
680 if ((error = sc_clean_up(scp))) {
685 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
687 /* no restore fonts & palette */
688 if (scp == scp->sc->cur_scp)
690 sc_clear_screen(scp);
691 scp->status &= ~UNKNOWN_MODE;
693 scp->status &= ~UNKNOWN_MODE;
694 /* no restore fonts & palette */
695 if (scp == scp->sc->cur_scp)
697 sc_clear_screen(scp);
703 case KD_PIXEL: /* pixel (raster) display */
704 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
706 if (scp->status & GRAPHICS_MODE)
707 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
710 if ((error = sc_clean_up(scp))) {
714 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
716 if (scp == scp->sc->cur_scp) {
718 #ifndef SC_NO_PALETTE_LOADING
719 load_palette(adp, scp->sc->palette);
722 sc_clear_screen(scp);
723 scp->status &= ~UNKNOWN_MODE;
725 #endif /* SC_PIXEL_MODE */
727 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
729 if ((error = sc_clean_up(scp))) {
733 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
736 if (scp == scp->sc->cur_scp)
747 case KDRASTER: /* set pixel (raster) display mode */
748 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
750 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
752 #endif /* SC_PIXEL_MODE */
754 case KDGETMODE: /* get current mode of this (virtual) console */
756 * From the user program's point of view, KD_PIXEL is the same
759 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
762 case KDSBORDER: /* set border color of this (virtual) console */
764 if (scp == scp->sc->cur_scp)
765 sc_set_border(scp, scp->border);
772 static LIST_HEAD(, sc_renderer) sc_rndr_list =
773 LIST_HEAD_INITIALIZER(sc_rndr_list);
776 sc_render_add(sc_renderer_t *rndr)
778 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
783 sc_render_remove(sc_renderer_t *rndr)
786 LIST_REMOVE(rndr, link);
788 return EBUSY; /* XXX */
792 *sc_render_match(scr_stat *scp, char *name, int mode)
794 const sc_renderer_t **list;
795 const sc_renderer_t *p;
797 if (!LIST_EMPTY(&sc_rndr_list)) {
798 LIST_FOREACH(p, &sc_rndr_list, link) {
799 if ((strcmp(p->name, name) == 0)
800 && (mode == p->mode)) {
802 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
807 list = (const sc_renderer_t **)scrndr_set.ls_items;
808 while ((p = *list++) != NULL) {
809 if ((strcmp(p->name, name) == 0)
810 && (mode == p->mode)) {
812 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);