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.
29 #include "opt_syscons.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/signalvar.h>
36 #include <sys/kernel.h>
38 #include <sys/consio.h>
39 #include <sys/filedesc.h>
42 #include <sys/mutex.h>
45 #include <dev/fb/fbreg.h>
46 #include <dev/syscons/syscons.h>
48 SET_DECLARE(scrndr_set, const sc_renderer_t);
50 /* for compatibility with previous versions */
51 /* 3.0-RELEASE used the following structure */
52 typedef struct old_video_adapter {
56 /* flag bits are the same as the -CURRENT
57 #define V_ADP_COLOR (1<<0)
58 #define V_ADP_MODECHANGE (1<<1)
59 #define V_ADP_STATESAVE (1<<2)
60 #define V_ADP_STATELOAD (1<<3)
61 #define V_ADP_FONT (1<<4)
62 #define V_ADP_PALETTE (1<<5)
63 #define V_ADP_BORDER (1<<6)
64 #define V_ADP_VESA (1<<7)
67 u_int va_window; /* virtual address */
68 size_t va_window_size;
69 size_t va_window_gran;
70 u_int va_buffer; /* virtual address */
71 size_t va_buffer_size;
73 int va_initial_bios_mode;
75 } old_video_adapter_t;
77 #define OLD_CONS_ADPINFO _IOWR('c', 101, old_video_adapter_t)
79 /* 3.1-RELEASE used the following structure */
80 typedef struct old_video_adapter_info {
91 u_int va_window; /* virtual address */
92 size_t va_window_size;
93 size_t va_window_gran;
95 size_t va_buffer_size;
97 int va_initial_bios_mode;
100 } old_video_adapter_info_t;
102 #define OLD_CONS_ADPINFO2 _IOWR('c', 101, old_video_adapter_info_t)
104 /* 3.0-RELEASE and 3.1-RELEASE used the following structure */
105 typedef struct old_video_info {
108 /* flag bits are the same as the -CURRENT
109 #define V_INFO_COLOR (1<<0)
110 #define V_INFO_GRAPHICS (1<<1)
111 #define V_INFO_LINEAR (1<<2)
112 #define V_INFO_VESA (1<<3)
120 u_int vi_window; /* physical address */
121 size_t vi_window_size;
122 size_t vi_window_gran;
123 u_int vi_buffer; /* physical address */
124 size_t vi_buffer_size;
127 #define OLD_CONS_MODEINFO _IOWR('c', 102, old_video_info_t)
128 #define OLD_CONS_FINDMODE _IOWR('c', 103, old_video_info_t)
131 sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
140 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
143 /* adjust argument values */
145 fontsize = info.vi_cheight;
148 #ifndef SC_NO_FONT_LOADING
149 if (!(scp->sc->fonts_loaded & FONT_8))
151 font = scp->sc->font_8;
155 } else if (fontsize >= 16) {
157 #ifndef SC_NO_FONT_LOADING
158 if (!(scp->sc->fonts_loaded & FONT_16))
160 font = scp->sc->font_16;
166 #ifndef SC_NO_FONT_LOADING
167 if (!(scp->sc->fonts_loaded & FONT_14))
169 font = scp->sc->font_14;
174 if ((xsize <= 0) || (xsize > info.vi_width))
175 xsize = info.vi_width;
176 if ((ysize <= 0) || (ysize > info.vi_height))
177 ysize = info.vi_height;
179 /* stop screen saver, etc */
181 if ((error = sc_clean_up(scp))) {
186 if (sc_render_match(scp, scp->sc->adp->va_name, 0) == NULL) {
192 #ifndef SC_NO_HISTORY
193 if (scp->history != NULL)
196 prev_ysize = scp->ysize;
198 * This is a kludge to fend off scrn_update() while we
199 * muck around with scp. XXX
201 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
202 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
208 scp->xpixel = scp->xsize*8;
209 scp->ypixel = scp->ysize*fontsize;
211 scp->font_size = fontsize;
213 /* allocate buffers */
214 sc_alloc_scr_buffer(scp, TRUE, TRUE);
215 sc_init_emulator(scp, NULL);
216 #ifndef SC_NO_CUTPASTE
217 sc_alloc_cut_buffer(scp, FALSE);
219 #ifndef SC_NO_HISTORY
220 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
224 if (scp == scp->sc->cur_scp)
226 scp->status &= ~UNKNOWN_MODE;
230 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
231 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
232 if (tp->t_winsize.ws_col != scp->xsize
233 || tp->t_winsize.ws_row != scp->ysize) {
234 tp->t_winsize.ws_col = scp->xsize;
235 tp->t_winsize.ws_row = scp->ysize;
236 if (tp->t_pgrp != NULL) {
237 PGRP_LOCK(tp->t_pgrp);
238 pgsignal(tp->t_pgrp, SIGWINCH, 1);
239 PGRP_UNLOCK(tp->t_pgrp);
247 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
249 #ifdef SC_NO_MODE_CHANGE
256 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
259 /* stop screen saver, etc */
261 if ((error = sc_clean_up(scp))) {
266 if (sc_render_match(scp, scp->sc->adp->va_name, GRAPHICS_MODE) == NULL) {
272 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
273 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
276 * Don't change xsize and ysize; preserve the previous vty
277 * and history buffers.
281 scp->xpixel = info.vi_width;
282 scp->ypixel = info.vi_height;
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);
289 sc_init_emulator(scp, NULL);
292 if (scp == scp->sc->cur_scp)
294 /* clear_graphics();*/
295 scp->status &= ~UNKNOWN_MODE;
299 if (tp->t_winsize.ws_xpixel != scp->xpixel
300 || tp->t_winsize.ws_ypixel != scp->ypixel) {
301 tp->t_winsize.ws_xpixel = scp->xpixel;
302 tp->t_winsize.ws_ypixel = scp->ypixel;
303 if (tp->t_pgrp != NULL) {
304 PGRP_LOCK(tp->t_pgrp);
305 pgsignal(tp->t_pgrp, SIGWINCH, 1);
306 PGRP_UNLOCK(tp->t_pgrp);
311 #endif /* SC_NO_MODE_CHANGE */
315 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
318 #ifndef SC_PIXEL_MODE
327 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
328 return ENODEV; /* this shouldn't happen */
330 /* adjust argument values */
332 fontsize = info.vi_cheight;
335 #ifndef SC_NO_FONT_LOADING
336 if (!(scp->sc->fonts_loaded & FONT_8))
338 font = scp->sc->font_8;
342 } else if (fontsize >= 16) {
344 #ifndef SC_NO_FONT_LOADING
345 if (!(scp->sc->fonts_loaded & FONT_16))
347 font = scp->sc->font_16;
353 #ifndef SC_NO_FONT_LOADING
354 if (!(scp->sc->fonts_loaded & FONT_14))
356 font = scp->sc->font_14;
362 xsize = info.vi_width/8;
364 ysize = info.vi_height/fontsize;
366 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
369 /* only 16 color, 4 plane modes are supported XXX */
370 if ((info.vi_depth != 4) || (info.vi_planes != 4))
374 * set_pixel_mode() currently does not support video modes whose
375 * memory size is larger than 64K. Because such modes require
376 * bank switching to access the entire screen. XXX
378 if (info.vi_width*info.vi_height/8 > info.vi_window_size)
381 /* stop screen saver, etc */
383 if ((error = sc_clean_up(scp))) {
388 if (sc_render_match(scp, scp->sc->adp->va_name, PIXEL_MODE) == NULL) {
395 (*scp->tsw->te_term)(scp, scp->ts);
401 #ifndef SC_NO_HISTORY
402 if (scp->history != NULL)
405 prev_ysize = scp->ysize;
406 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
407 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
410 scp->xoff = (scp->xpixel/8 - xsize)/2;
411 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
413 scp->font_size = fontsize;
415 /* allocate buffers */
416 sc_alloc_scr_buffer(scp, TRUE, TRUE);
417 sc_init_emulator(scp, NULL);
418 #ifndef SC_NO_CUTPASTE
419 sc_alloc_cut_buffer(scp, FALSE);
421 #ifndef SC_NO_HISTORY
422 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
426 if (scp == scp->sc->cur_scp) {
427 sc_set_border(scp, scp->border);
428 sc_set_cursor_image(scp);
431 scp->status &= ~UNKNOWN_MODE;
435 if (tp->t_winsize.ws_col != scp->xsize
436 || tp->t_winsize.ws_row != scp->ysize) {
437 tp->t_winsize.ws_col = scp->xsize;
438 tp->t_winsize.ws_row = scp->ysize;
439 if (tp->t_pgrp != NULL) {
440 PGRP_LOCK(tp->t_pgrp);
441 pgsignal(tp->t_pgrp, SIGWINCH, 1);
442 PGRP_UNLOCK(tp->t_pgrp);
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);
602 if (scp != scp->sc->cur_scp)
603 return ENODEV; /* XXX */
604 return fb_ioctl(adp, cmd, data);
606 #ifndef SC_NO_MODE_CHANGE
607 /* generic text modes */
608 case SW_TEXT_80x25: case SW_TEXT_80x30:
609 case SW_TEXT_80x43: case SW_TEXT_80x50:
615 case SW_VGA_C80x25: case SW_VGA_M80x25:
616 case SW_VGA_C80x30: case SW_VGA_M80x30:
617 case SW_VGA_C80x50: case SW_VGA_M80x50:
618 case SW_VGA_C80x60: case SW_VGA_M80x60:
619 case SW_VGA_C90x25: case SW_VGA_M90x25:
620 case SW_VGA_C90x30: case SW_VGA_M90x30:
621 case SW_VGA_C90x43: case SW_VGA_M90x43:
622 case SW_VGA_C90x50: case SW_VGA_M90x50:
623 case SW_VGA_C90x60: case SW_VGA_M90x60:
624 case SW_B40x25: case SW_C40x25:
625 case SW_B80x25: case SW_C80x25:
626 case SW_ENH_B40x25: case SW_ENH_C40x25:
627 case SW_ENH_B80x25: case SW_ENH_C80x25:
628 case SW_ENH_B80x43: case SW_ENH_C80x43:
629 case SW_EGAMONO80x25:
632 /* PC98 TEXT MODES */
636 if (!(adp->va_flags & V_ADP_MODECHANGE))
638 return sc_set_text_mode(scp, tp, cmd & 0xff, 0, 0, 0);
641 case SW_BG320: case SW_BG640:
642 case SW_CG320: case SW_CG320_D: case SW_CG640_E:
643 case SW_CG640x350: case SW_ENH_CG640:
644 case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
647 /* PC98 GRAPHICS MODES */
648 case SW_PC98_EGC640x400: case SW_PC98_PEGC640x400:
649 case SW_PC98_PEGC640x480:
651 if (!(adp->va_flags & V_ADP_MODECHANGE))
653 return sc_set_graphics_mode(scp, tp, cmd & 0xff);
654 #endif /* SC_NO_MODE_CHANGE */
656 case KDSETMODE: /* set current mode of this (virtual) console */
657 switch (*(int *)data) {
658 case KD_TEXT: /* switch to TEXT (known) mode */
660 * If scp->mode is of graphics modes, we don't know which
661 * text mode to switch back to...
663 if (scp->status & GRAPHICS_MODE)
665 /* restore fonts & palette ! */
667 #ifndef SC_NO_FONT_LOADING
668 if (ISFONTAVAIL(adp->va_flags)
669 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
672 * Don't load fonts for now... XXX
674 if (scp->sc->fonts_loaded & FONT_8)
675 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
676 if (scp->sc->fonts_loaded & FONT_14)
677 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
678 if (scp->sc->fonts_loaded & FONT_16)
679 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
681 #endif /* SC_NO_FONT_LOADING */
684 #ifndef SC_NO_PALETTE_LOADING
685 load_palette(adp, scp->sc->palette);
689 /* move hardware cursor out of the way */
690 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
695 case KD_TEXT1: /* switch to TEXT (known) mode */
697 * If scp->mode is of graphics modes, we don't know which
698 * text/pixel mode to switch back to...
700 if (scp->status & GRAPHICS_MODE)
703 if ((error = sc_clean_up(scp))) {
708 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
710 /* no restore fonts & palette */
711 if (scp == scp->sc->cur_scp)
713 sc_clear_screen(scp);
714 scp->status &= ~UNKNOWN_MODE;
716 scp->status &= ~UNKNOWN_MODE;
717 /* no restore fonts & palette */
718 if (scp == scp->sc->cur_scp)
720 sc_clear_screen(scp);
726 case KD_PIXEL: /* pixel (raster) display */
727 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
729 if (scp->status & GRAPHICS_MODE)
730 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
733 if ((error = sc_clean_up(scp))) {
737 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
739 if (scp == scp->sc->cur_scp) {
741 #ifndef SC_NO_PALETTE_LOADING
742 load_palette(adp, scp->sc->palette);
745 sc_clear_screen(scp);
746 scp->status &= ~UNKNOWN_MODE;
748 #endif /* SC_PIXEL_MODE */
750 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
752 if ((error = sc_clean_up(scp))) {
756 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
759 if (scp == scp->sc->cur_scp)
770 case KDRASTER: /* set pixel (raster) display mode */
771 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
773 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
775 #endif /* SC_PIXEL_MODE */
777 case KDGETMODE: /* get current mode of this (virtual) console */
779 * From the user program's point of view, KD_PIXEL is the same
782 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
785 case KDSBORDER: /* set border color of this (virtual) console */
787 if (scp == scp->sc->cur_scp)
788 sc_set_border(scp, scp->border);
795 static LIST_HEAD(, sc_renderer) sc_rndr_list =
796 LIST_HEAD_INITIALIZER(sc_rndr_list);
799 sc_render_add(sc_renderer_t *rndr)
801 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
806 sc_render_remove(sc_renderer_t *rndr)
809 LIST_REMOVE(rndr, link);
811 return EBUSY; /* XXX */
815 *sc_render_match(scr_stat *scp, char *name, int mode)
817 const sc_renderer_t **list;
818 const sc_renderer_t *p;
820 if (!LIST_EMPTY(&sc_rndr_list)) {
821 LIST_FOREACH(p, &sc_rndr_list, link) {
822 if ((strcmp(p->name, name) == 0)
823 && (mode == p->mode)) {
825 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
830 SET_FOREACH(list, scrndr_set) {
832 if ((strcmp(p->name, name) == 0)
833 && (mode == p->mode)) {
835 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);