2 * Copyright (c) 1998 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/dev/syscons/scvidctl.c,v 1.19.2.2 2000/05/05 09:16:08 nyan Exp $
30 * $DragonFly: src/sys/dev/misc/syscons/scvidctl.c,v 1.6 2004/09/04 06:15:08 dillon Exp $
33 #include "opt_syscons.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/signalvar.h>
40 #include <sys/kernel.h>
42 #include <machine/console.h>
44 #include <dev/video/fb/fbreg.h>
47 SET_DECLARE(scrndr_set, const sc_renderer_t);
49 /* for compatibility with previous versions */
50 /* 3.0-RELEASE used the following structure */
51 typedef struct old_video_adapter {
55 /* flag bits are the same as the -CURRENT
56 #define V_ADP_COLOR (1<<0)
57 #define V_ADP_MODECHANGE (1<<1)
58 #define V_ADP_STATESAVE (1<<2)
59 #define V_ADP_STATELOAD (1<<3)
60 #define V_ADP_FONT (1<<4)
61 #define V_ADP_PALETTE (1<<5)
62 #define V_ADP_BORDER (1<<6)
63 #define V_ADP_VESA (1<<7)
66 u_int va_window; /* virtual address */
67 size_t va_window_size;
68 size_t va_window_gran;
69 u_int va_buffer; /* virtual address */
70 size_t va_buffer_size;
72 int va_initial_bios_mode;
74 } old_video_adapter_t;
76 #define OLD_CONS_ADPINFO _IOWR('c', 101, old_video_adapter_t)
78 /* 3.1-RELEASE used the following structure */
79 typedef struct old_video_adapter_info {
90 u_int va_window; /* virtual address */
91 size_t va_window_size;
92 size_t va_window_gran;
94 size_t va_buffer_size;
96 int va_initial_bios_mode;
99 } old_video_adapter_info_t;
101 #define OLD_CONS_ADPINFO2 _IOWR('c', 101, old_video_adapter_info_t)
103 /* 3.0-RELEASE and 3.1-RELEASE used the following structure */
104 typedef struct old_video_info {
107 /* flag bits are the same as the -CURRENT
108 #define V_INFO_COLOR (1<<0)
109 #define V_INFO_GRAPHICS (1<<1)
110 #define V_INFO_LINEAR (1<<2)
111 #define V_INFO_VESA (1<<3)
119 u_int vi_window; /* physical address */
120 size_t vi_window_size;
121 size_t vi_window_gran;
122 u_int vi_buffer; /* physical address */
123 size_t vi_buffer_size;
126 #define OLD_CONS_MODEINFO _IOWR('c', 102, old_video_info_t)
127 #define OLD_CONS_FINDMODE _IOWR('c', 103, old_video_info_t)
130 sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
139 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
142 /* adjust argument values */
144 fontsize = info.vi_cheight;
147 #ifndef SC_NO_FONT_LOADING
148 if (!(scp->sc->fonts_loaded & FONT_8))
150 font = scp->sc->font_8;
154 } else if (fontsize >= 16) {
156 #ifndef SC_NO_FONT_LOADING
157 if (!(scp->sc->fonts_loaded & FONT_16))
159 font = scp->sc->font_16;
165 #ifndef SC_NO_FONT_LOADING
166 if (!(scp->sc->fonts_loaded & FONT_14))
168 font = scp->sc->font_14;
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;
178 /* stop screen saver, etc */
180 if ((error = sc_clean_up(scp))) {
185 if (sc_render_match(scp, scp->sc->adp->va_name, 0) == NULL) {
191 #ifndef SC_NO_HISTORY
192 if (scp->history != NULL)
195 prev_ysize = scp->ysize;
197 * This is a kludge to fend off scrn_update() while we
198 * muck around with scp. XXX
200 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
201 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
207 scp->xpixel = scp->xsize*8;
208 scp->ypixel = scp->ysize*fontsize;
210 scp->font_size = fontsize;
212 /* allocate buffers */
213 sc_alloc_scr_buffer(scp, TRUE, TRUE);
214 sc_init_emulator(scp, NULL);
215 #ifndef SC_NO_CUTPASTE
216 sc_alloc_cut_buffer(scp, FALSE);
218 #ifndef SC_NO_HISTORY
219 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
223 if (scp == scp->sc->cur_scp)
225 scp->status &= ~UNKNOWN_MODE;
229 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
230 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
231 if (tp->t_winsize.ws_col != scp->xsize
232 || tp->t_winsize.ws_row != scp->ysize) {
233 tp->t_winsize.ws_col = scp->xsize;
234 tp->t_winsize.ws_row = scp->ysize;
235 pgsignal(tp->t_pgrp, SIGWINCH, 1);
242 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
244 #ifdef SC_NO_MODE_CHANGE
251 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
254 /* stop screen saver, etc */
256 if ((error = sc_clean_up(scp))) {
261 if (sc_render_match(scp, scp->sc->adp->va_name, GRAPHICS_MODE) == NULL) {
267 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
268 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
271 * Don't change xsize and ysize; preserve the previous vty
272 * and history buffers.
276 scp->xpixel = info.vi_width;
277 scp->ypixel = info.vi_height;
280 #ifndef SC_NO_SYSMOUSE
281 /* move the mouse cursor at the center of the screen */
282 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
284 sc_init_emulator(scp, NULL);
287 if (scp == scp->sc->cur_scp)
289 /* clear_graphics();*/
290 scp->status &= ~UNKNOWN_MODE;
294 if (tp->t_winsize.ws_xpixel != scp->xpixel
295 || tp->t_winsize.ws_ypixel != scp->ypixel) {
296 tp->t_winsize.ws_xpixel = scp->xpixel;
297 tp->t_winsize.ws_ypixel = scp->ypixel;
298 pgsignal(tp->t_pgrp, SIGWINCH, 1);
302 #endif /* SC_NO_MODE_CHANGE */
306 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
309 #ifndef SC_PIXEL_MODE
318 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
319 return ENODEV; /* this shouldn't happen */
321 /* adjust argument values */
323 fontsize = info.vi_cheight;
326 #ifndef SC_NO_FONT_LOADING
327 if (!(scp->sc->fonts_loaded & FONT_8))
329 font = scp->sc->font_8;
333 } else if (fontsize >= 16) {
335 #ifndef SC_NO_FONT_LOADING
336 if (!(scp->sc->fonts_loaded & FONT_16))
338 font = scp->sc->font_16;
344 #ifndef SC_NO_FONT_LOADING
345 if (!(scp->sc->fonts_loaded & FONT_14))
347 font = scp->sc->font_14;
353 xsize = info.vi_width/8;
355 ysize = info.vi_height/fontsize;
357 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
361 * We currently support the following graphic modes:
363 * - 4 bpp planar modes whose memory size does not exceed 64K
364 * - 15, 16, 24 and 32 bpp linear modes
367 if (info.vi_mem_model == V_INFO_MM_PLANAR) {
368 if (info.vi_planes != 4)
372 * A memory size >64K requires bank switching to access the entire
376 if (info.vi_width * info.vi_height / 8 > info.vi_window_size)
378 } else if (info.vi_mem_model == V_INFO_MM_DIRECT) {
379 if ((info.vi_depth != 15) && (info.vi_depth != 16) &&
380 (info.vi_depth != 24) && (info.vi_depth != 32))
385 /* stop screen saver, etc */
387 if ((error = sc_clean_up(scp))) {
392 if (sc_render_match(scp, scp->sc->adp->va_name, PIXEL_MODE) == NULL) {
399 (*scp->tsw->te_term)(scp, scp->ts);
405 #ifndef SC_NO_HISTORY
406 if (scp->history != NULL)
409 prev_ysize = scp->ysize;
410 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
411 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
414 scp->xoff = (scp->xpixel/8 - xsize)/2;
415 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
417 scp->font_size = fontsize;
419 /* allocate buffers */
420 sc_alloc_scr_buffer(scp, TRUE, TRUE);
421 sc_init_emulator(scp, NULL);
422 #ifndef SC_NO_CUTPASTE
423 sc_alloc_cut_buffer(scp, FALSE);
425 #ifndef SC_NO_HISTORY
426 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
430 if (scp == scp->sc->cur_scp) {
431 sc_set_border(scp, scp->border);
432 sc_set_cursor_image(scp);
435 scp->status &= ~UNKNOWN_MODE;
439 if (tp->t_winsize.ws_col != scp->xsize
440 || tp->t_winsize.ws_row != scp->ysize) {
441 tp->t_winsize.ws_col = scp->xsize;
442 tp->t_winsize.ws_row = scp->ysize;
443 pgsignal(tp->t_pgrp, SIGWINCH, 1);
447 #endif /* SC_PIXEL_MODE */
450 #define fb_ioctl(a, c, d) \
451 (((a) == NULL) ? ENODEV : \
452 (*vidsw[(a)->va_index]->ioctl)((a), (c), (caddr_t)(d)))
455 sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
458 video_adapter_t *adp;
460 video_adapter_info_t adp_info;
464 scp = SC_STAT(tp->t_dev);
465 if (scp == NULL) /* tp == SC_MOUSE */
468 if (adp == NULL) /* shouldn't happen??? */
473 case CONS_CURRENTADP: /* get current adapter index */
475 return fb_ioctl(adp, FBIO_ADAPTER, data);
477 case CONS_CURRENT: /* get current adapter type */
479 return fb_ioctl(adp, FBIO_ADPTYPE, data);
481 case OLD_CONS_ADPINFO: /* adapter information (old interface) */
482 if (((old_video_adapter_t *)data)->va_index >= 0) {
483 adp = vid_get_adapter(((old_video_adapter_t *)data)->va_index);
487 ((old_video_adapter_t *)data)->va_index = adp->va_index;
488 ((old_video_adapter_t *)data)->va_type = adp->va_type;
489 ((old_video_adapter_t *)data)->va_flags = adp->va_flags;
490 ((old_video_adapter_t *)data)->va_crtc_addr = adp->va_crtc_addr;
491 ((old_video_adapter_t *)data)->va_window = adp->va_window;
492 ((old_video_adapter_t *)data)->va_window_size = adp->va_window_size;
493 ((old_video_adapter_t *)data)->va_window_gran = adp->va_window_gran;
494 ((old_video_adapter_t *)data)->va_buffer = adp->va_buffer;
495 ((old_video_adapter_t *)data)->va_buffer_size = adp->va_buffer_size;
496 ((old_video_adapter_t *)data)->va_mode = adp->va_mode;
497 ((old_video_adapter_t *)data)->va_initial_mode = adp->va_initial_mode;
498 ((old_video_adapter_t *)data)->va_initial_bios_mode
499 = adp->va_initial_bios_mode;
502 case OLD_CONS_ADPINFO2: /* adapter information (yet another old I/F) */
503 adp_info.va_index = ((old_video_adapter_info_t *)data)->va_index;
504 if (adp_info.va_index >= 0) {
505 adp = vid_get_adapter(adp_info.va_index);
509 error = fb_ioctl(adp, FBIO_ADPINFO, &adp_info);
511 bcopy(&adp_info, data, sizeof(old_video_adapter_info_t));
514 case CONS_ADPINFO: /* adapter information */
516 if (((video_adapter_info_t *)data)->va_index >= 0) {
517 adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
521 return fb_ioctl(adp, FBIO_ADPINFO, data);
523 case CONS_GET: /* get current video mode */
525 *(int *)data = scp->mode;
528 #ifndef SC_NO_MODE_CHANGE
529 case FBIO_SETMODE: /* set video mode */
530 if (!(adp->va_flags & V_ADP_MODECHANGE))
532 info.vi_mode = *(int *)data;
533 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
536 if (info.vi_flags & V_INFO_GRAPHICS)
537 return sc_set_graphics_mode(scp, tp, *(int *)data);
539 return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0);
540 #endif /* SC_NO_MODE_CHANGE */
542 case OLD_CONS_MODEINFO: /* get mode information (old infterface) */
543 info.vi_mode = ((old_video_info_t *)data)->vi_mode;
544 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
546 bcopy(&info, (old_video_info_t *)data, sizeof(old_video_info_t));
549 case CONS_MODEINFO: /* get mode information */
551 return fb_ioctl(adp, FBIO_MODEINFO, data);
553 case OLD_CONS_FINDMODE: /* find a matching video mode (old interface) */
554 bzero(&info, sizeof(info));
555 bcopy((old_video_info_t *)data, &info, sizeof(old_video_info_t));
556 error = fb_ioctl(adp, FBIO_FINDMODE, &info);
558 bcopy(&info, (old_video_info_t *)data, sizeof(old_video_info_t));
561 case CONS_FINDMODE: /* find a matching video mode */
563 return fb_ioctl(adp, FBIO_FINDMODE, data);
565 case CONS_SETWINORG: /* set frame buffer window origin */
567 if (scp != scp->sc->cur_scp)
568 return ENODEV; /* XXX */
569 return fb_ioctl(adp, FBIO_SETWINORG, data);
571 case FBIO_GETWINORG: /* get frame buffer window origin */
572 if (scp != scp->sc->cur_scp)
573 return ENODEV; /* XXX */
574 return fb_ioctl(adp, FBIO_GETWINORG, data);
576 case FBIO_GETDISPSTART:
577 case FBIO_SETDISPSTART:
578 case FBIO_GETLINEWIDTH:
579 case FBIO_SETLINEWIDTH:
580 if (scp != scp->sc->cur_scp)
581 return ENODEV; /* XXX */
582 return fb_ioctl(adp, cmd, data);
584 case FBIO_GETPALETTE:
585 case FBIO_SETPALETTE:
597 if (scp != scp->sc->cur_scp)
598 return ENODEV; /* XXX */
599 return fb_ioctl(adp, cmd, data);
601 #ifndef SC_NO_MODE_CHANGE
602 /* generic text modes */
603 case SW_TEXT_80x25: case SW_TEXT_80x30:
604 case SW_TEXT_80x43: case SW_TEXT_80x50:
610 case SW_VGA_C80x25: case SW_VGA_M80x25:
611 case SW_VGA_C80x30: case SW_VGA_M80x30:
612 case SW_VGA_C80x50: case SW_VGA_M80x50:
613 case SW_VGA_C80x60: case SW_VGA_M80x60:
614 case SW_VGA_C90x25: case SW_VGA_M90x25:
615 case SW_VGA_C90x30: case SW_VGA_M90x30:
616 case SW_VGA_C90x43: case SW_VGA_M90x43:
617 case SW_VGA_C90x50: case SW_VGA_M90x50:
618 case SW_VGA_C90x60: case SW_VGA_M90x60:
619 case SW_B40x25: case SW_C40x25:
620 case SW_B80x25: case SW_C80x25:
621 case SW_ENH_B40x25: case SW_ENH_C40x25:
622 case SW_ENH_B80x25: case SW_ENH_C80x25:
623 case SW_ENH_B80x43: case SW_ENH_C80x43:
624 case SW_EGAMONO80x25:
627 /* PC98 TEXT MODES */
631 if (!(adp->va_flags & V_ADP_MODECHANGE))
633 return sc_set_text_mode(scp, tp, cmd & 0xff, 0, 0, 0);
636 case SW_BG320: case SW_BG640:
637 case SW_CG320: case SW_CG320_D: case SW_CG640_E:
638 case SW_CG640x350: case SW_ENH_CG640:
639 case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
642 /* PC98 GRAPHICS MODES */
643 case SW_PC98_EGC640x400: case SW_PC98_PEGC640x400:
644 case SW_PC98_PEGC640x480:
646 if (!(adp->va_flags & V_ADP_MODECHANGE))
648 return sc_set_graphics_mode(scp, tp, cmd & 0xff);
649 #endif /* SC_NO_MODE_CHANGE */
651 case KDSETMODE: /* set current mode of this (virtual) console */
652 switch (*(int *)data) {
653 case KD_TEXT: /* switch to TEXT (known) mode */
655 * If scp->mode is of graphics modes, we don't know which
656 * text mode to switch back to...
658 if (scp->status & GRAPHICS_MODE)
660 /* restore fonts & palette ! */
662 #ifndef SC_NO_FONT_LOADING
663 if (ISFONTAVAIL(adp->va_flags)
664 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
667 * Don't load fonts for now... XXX
669 if (scp->sc->fonts_loaded & FONT_8)
670 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
671 if (scp->sc->fonts_loaded & FONT_14)
672 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
673 if (scp->sc->fonts_loaded & FONT_16)
674 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
676 #endif /* SC_NO_FONT_LOADING */
679 #ifndef SC_NO_PALETTE_LOADING
680 load_palette(adp, scp->sc->palette);
684 /* move hardware cursor out of the way */
685 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
690 case KD_TEXT1: /* switch to TEXT (known) mode */
692 * If scp->mode is of graphics modes, we don't know which
693 * text/pixel mode to switch back to...
695 if (scp->status & GRAPHICS_MODE)
698 if ((error = sc_clean_up(scp))) {
703 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
705 /* no restore fonts & palette */
706 if (scp == scp->sc->cur_scp)
708 sc_clear_screen(scp);
709 scp->status &= ~UNKNOWN_MODE;
711 scp->status &= ~UNKNOWN_MODE;
712 /* no restore fonts & palette */
713 if (scp == scp->sc->cur_scp)
715 sc_clear_screen(scp);
721 case KD_PIXEL: /* pixel (raster) display */
722 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
724 if (scp->status & GRAPHICS_MODE)
725 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
728 if ((error = sc_clean_up(scp))) {
732 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
734 if (scp == scp->sc->cur_scp) {
736 #ifndef SC_NO_PALETTE_LOADING
737 load_palette(adp, scp->sc->palette);
740 sc_clear_screen(scp);
741 scp->status &= ~UNKNOWN_MODE;
743 #endif /* SC_PIXEL_MODE */
745 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
747 if ((error = sc_clean_up(scp))) {
751 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
754 if (scp == scp->sc->cur_scp)
765 case KDRASTER: /* set pixel (raster) display mode */
766 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
768 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
770 #endif /* SC_PIXEL_MODE */
772 case KDGETMODE: /* get current mode of this (virtual) console */
774 * From the user program's point of view, KD_PIXEL is the same
777 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
780 case KDSBORDER: /* set border color of this (virtual) console */
782 if (scp == scp->sc->cur_scp)
783 sc_set_border(scp, scp->border);
790 static LIST_HEAD(, sc_renderer) sc_rndr_list =
791 LIST_HEAD_INITIALIZER(sc_rndr_list);
794 sc_render_add(sc_renderer_t *rndr)
796 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
801 sc_render_remove(sc_renderer_t *rndr)
804 LIST_REMOVE(rndr, link);
806 return EBUSY; /* XXX */
810 *sc_render_match(scr_stat *scp, char *name, int mode)
812 const sc_renderer_t **list;
813 const sc_renderer_t *p;
815 if (!LIST_EMPTY(&sc_rndr_list)) {
816 LIST_FOREACH(p, &sc_rndr_list, link) {
817 if ((strcmp(p->name, name) == 0)
818 && (mode == p->mode)) {
820 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
825 SET_FOREACH(list, scrndr_set) {
827 if ((strcmp(p->name, name) == 0)
828 && (mode == p->mode)) {
830 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);