Home | History | Annotate | Line # | Download | only in ic
vga_raster.c revision 1.5
      1 /*	$NetBSD: vga_raster.c,v 1.5 2002/11/28 07:02:20 junyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002 Bang Jun-Young
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     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.
     28  */
     29 
     30 /*
     31  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     32  * All rights reserved.
     33  *
     34  * Author: Chris G. Demetriou
     35  *
     36  * Permission to use, copy, modify and distribute this software and
     37  * its documentation is hereby granted, provided that both the copyright
     38  * notice and this permission notice appear in all copies of the
     39  * software, derivative works or modified versions, and any portions
     40  * thereof, and that both notices appear in supporting documentation.
     41  *
     42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     44  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     45  *
     46  * Carnegie Mellon requests users of this software to return to
     47  *
     48  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     49  *  School of Computer Science
     50  *  Carnegie Mellon University
     51  *  Pittsburgh PA 15213-3890
     52  *
     53  * any improvements or extensions that they make and grant Carnegie the
     54  * rights to redistribute these changes.
     55  */
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/callout.h>
     60 #include <sys/kernel.h>
     61 #include <sys/device.h>
     62 #include <sys/malloc.h>
     63 #include <sys/queue.h>
     64 #include <machine/bus.h>
     65 
     66 #include <dev/ic/mc6845reg.h>
     67 #include <dev/ic/pcdisplayvar.h>
     68 #include <dev/ic/vgareg.h>
     69 #include <dev/ic/vgavar.h>
     70 #include <dev/ic/videomode.h>
     71 
     72 #include <dev/wscons/wsdisplayvar.h>
     73 #include <dev/wscons/wsconsio.h>
     74 #include <dev/wsfont/wsfont.h>
     75 
     76 #include <dev/ic/pcdisplay.h>
     77 
     78 u_int8_t builtinfont_data[256 * 16];
     79 
     80 struct wsdisplay_font builtinfont = {
     81 	"builtin",			/* typeface name */
     82 	0,				/* firstchar */
     83 	256,				/* numchars */
     84 	WSDISPLAY_FONTENC_IBM,		/* encoding */
     85 	8,				/* width */
     86 	16,				/* height */
     87 	1,				/* stride */
     88 	WSDISPLAY_FONTORDER_L2R,	/* bit order */
     89 	WSDISPLAY_FONTORDER_L2R,	/* byte order */
     90 	builtinfont_data		/* data */
     91 };
     92 
     93 struct vga_scrmem {
     94 	u_int16_t ch;
     95 	u_int8_t attr;
     96 	u_int8_t second;	/* XXXBJY should be u_int8_t len; */
     97 	u_int8_t enc;
     98 };
     99 
    100 #ifdef VGA_CONSOLE_SCREENTYPE
    101 #define VGA_SCRMEM_SIZE		(80 * 30)
    102 #else
    103 #define VGA_SCRMEM_SIZE		(80 * 25)
    104 #endif
    105 
    106 struct vga_scrmem boot_scrmem[VGA_SCRMEM_SIZE];
    107 
    108 struct vga_raster_font {
    109 	LIST_ENTRY(vga_raster_font) next;
    110 	struct wsdisplay_font *font;
    111 };
    112 
    113 struct vgascreen {
    114 	LIST_ENTRY(vgascreen) next;
    115 	struct vga_config *cfg;
    116 	struct vga_handle *hdl;
    117 	const struct wsscreen_descr *type;
    118 
    119 	int active;
    120 	struct vga_scrmem *mem;
    121 	int encoding;
    122 
    123 	int dispoffset;
    124 	int mindispoffset;
    125 	int maxdispoffset;
    126 
    127 	int cursoron;			/* Is cursor displayed? */
    128 	int cursorcol;			/* Current cursor column */
    129 	int cursorrow;			/* Current cursor row */
    130 	struct vga_scrmem cursortmp;
    131 	int cursorstride;
    132 
    133 	LIST_HEAD(, vga_raster_font) fontset;
    134 };
    135 
    136 struct vga_moderegs {
    137 	u_int8_t miscout;		/* Misc. output */
    138 	u_int8_t crtc[VGA_CRTC_NREGS];	/* CRTC controller */
    139 	u_int8_t atc[VGA_ATC_NREGS];	/* Attribute controller */
    140 	u_int8_t ts[VGA_TS_NREGS];	/* Time sequencer */
    141 	u_int8_t gdc[VGA_GDC_NREGS];	/* Graphics display controller */
    142 };
    143 
    144 static int vgaconsole, vga_console_type, vga_console_attached;
    145 static struct vgascreen vga_console_screen;
    146 static struct vga_config vga_console_vc;
    147 static struct vga_raster_font vga_console_fontset_ascii;
    148 static struct videomode vga_console_modes[2] = {
    149 	/* 640x400 for 80x25, 80x40 and 80x50 modes */
    150 	{
    151 		25175, 640, 664, 760, 800, 400, 409, 411, 450, 0
    152 	},
    153 	/* 640x480 for 80x30 mode */
    154 	{
    155 		25175, 640, 664, 760, 800, 480, 491, 493, 525, 0
    156 	}
    157 };
    158 
    159 static void vga_raster_init(struct vga_config *, bus_space_tag_t,
    160 		bus_space_tag_t);
    161 static void vga_raster_init_screen(struct vga_config *, struct vgascreen *,
    162 		const struct wsscreen_descr *, int, long *);
    163 static void vga_raster_setup_font(struct vga_config *, struct vgascreen *);
    164 static void vga_setup_regs(struct videomode *, struct vga_moderegs *);
    165 static void vga_set_mode(struct vga_handle *, struct vga_moderegs *);
    166 static void vga_restore_screen(struct vgascreen *,
    167 		const struct wsscreen_descr *, struct vga_scrmem *);
    168 static void vga_raster_cursor_init(struct vgascreen *, int);
    169 static void _vga_raster_putchar(void *, int, int, u_int, long,
    170 		struct vga_raster_font *);
    171 
    172 static void vga_raster_cursor(void *, int, int, int);
    173 static int  vga_raster_mapchar(void *, int, u_int *);
    174 static void vga_raster_putchar(void *, int, int, u_int, long);
    175 static void vga_raster_copycols(void *, int, int, int, int);
    176 static void vga_raster_erasecols(void *, int, int, int, long);
    177 static void vga_raster_copyrows(void *, int, int, int);
    178 static void vga_raster_eraserows(void *, int, int, long);
    179 static int  vga_raster_allocattr(void *, int, int, int, long *);
    180 
    181 const struct wsdisplay_emulops vga_raster_emulops = {
    182 	vga_raster_cursor,
    183 	vga_raster_mapchar,
    184 	vga_raster_putchar,
    185 	vga_raster_copycols,
    186 	vga_raster_erasecols,
    187 	vga_raster_copyrows,
    188 	vga_raster_eraserows,
    189 	vga_raster_allocattr,
    190 };
    191 
    192 /*
    193  * translate WS(=ANSI) color codes to standard pc ones
    194  */
    195 static const unsigned char fgansitopc[] = {
    196 #ifdef __alpha__
    197 	/*
    198 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
    199 	 * XXX We should probably not bother with this
    200 	 * XXX (reinitialize the palette registers).
    201 	 */
    202 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
    203 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
    204 #else
    205 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
    206 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
    207 #endif
    208 }, bgansitopc[] = {
    209 #ifdef __alpha__
    210 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
    211 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
    212 #else
    213 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
    214 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
    215 #endif
    216 };
    217 
    218 const struct wsscreen_descr vga_25lscreen = {
    219 	"80x25", 80, 25,
    220 	&vga_raster_emulops,
    221 	8, 16,
    222 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    223 	&vga_console_modes[0]
    224 }, vga_25lscreen_mono = {
    225 	"80x25", 80, 25,
    226 	&vga_raster_emulops,
    227 	8, 16,
    228 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    229 	&vga_console_modes[0]
    230 }, vga_30lscreen = {
    231 	"80x30", 80, 30,
    232 	&vga_raster_emulops,
    233 	8, 16,
    234 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    235 	&vga_console_modes[1]
    236 }, vga_30lscreen_mono = {
    237 	"80x30", 80, 30,
    238 	&vga_raster_emulops,
    239 	8, 16,
    240 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    241 	&vga_console_modes[1]
    242 }, vga_40lscreen = {
    243 	"80x40", 80, 40,
    244 	&vga_raster_emulops,
    245 	8, 10,
    246 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    247 	&vga_console_modes[0]
    248 }, vga_40lscreen_mono = {
    249 	"80x40", 80, 40,
    250 	&vga_raster_emulops,
    251 	8, 10,
    252 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    253 	&vga_console_modes[0]
    254 }, vga_50lscreen = {
    255 	"80x50", 80, 50,
    256 	&vga_raster_emulops,
    257 	8, 8,
    258 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    259 	&vga_console_modes[0]
    260 }, vga_50lscreen_mono = {
    261 	"80x50", 80, 50,
    262 	&vga_raster_emulops,
    263 	8, 8,
    264 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    265 	&vga_console_modes[0]
    266 };
    267 
    268 const struct wsscreen_descr *_vga_scrlist[] = {
    269 	&vga_25lscreen,
    270 	&vga_30lscreen,
    271 	&vga_40lscreen,
    272 	&vga_50lscreen,
    273 }, *_vga_scrlist_mono[] = {
    274 	&vga_25lscreen_mono,
    275 	&vga_30lscreen_mono,
    276 	&vga_40lscreen_mono,
    277 	&vga_50lscreen_mono,
    278 };
    279 
    280 const struct wsscreen_list vga_screenlist = {
    281 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
    282 	_vga_scrlist
    283 }, vga_screenlist_mono = {
    284 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
    285 	_vga_scrlist_mono
    286 };
    287 
    288 static int	vga_raster_ioctl(void *, u_long, caddr_t, int, struct proc *);
    289 static paddr_t	vga_raster_mmap(void *, off_t, int);
    290 static int	vga_raster_alloc_screen(void *, const struct wsscreen_descr *,
    291 		    void **, int *, int *, long *);
    292 static void	vga_raster_free_screen(void *, void *);
    293 static int	vga_raster_show_screen(void *, void *, int,
    294 		    void (*)(void *, int, int), void *);
    295 static int	vga_raster_load_font(void *, void *, struct wsdisplay_font *);
    296 
    297 static void 	vga_switch_screen(struct vga_config *);
    298 static void 	vga_raster_setscreentype(struct vga_config *,
    299 		    const struct wsscreen_descr *);
    300 
    301 const struct wsdisplay_accessops vga_raster_accessops = {
    302 	vga_raster_ioctl,
    303 	vga_raster_mmap,
    304 	vga_raster_alloc_screen,
    305 	vga_raster_free_screen,
    306 	vga_raster_show_screen,
    307 	vga_raster_load_font,
    308 };
    309 
    310 int
    311 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
    312 {
    313 	long defattr;
    314 	const struct wsscreen_descr *scr;
    315 #ifdef VGA_CONSOLE_SCREENTYPE
    316 	char *typestr;
    317 #endif
    318 
    319 	if (check && !vga_common_probe(iot, memt))
    320 		return (ENXIO);
    321 
    322 	/* set up bus-independent VGA configuration */
    323 	vga_raster_init(&vga_console_vc, iot, memt);
    324 #ifdef VGA_CONSOLE_SCREENTYPE
    325 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    326 	    &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
    327 	if (!scr)
    328 		/* Invalid screen type, continue with the default mode. */
    329 		typestr = "80x25";
    330 	else if (scr->nrows > 30)
    331 		/* Unsupported screen type, try 80x30. */
    332 		typestr = "80x30";
    333 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    334 	    &vga_screenlist_mono : &vga_screenlist, typestr);
    335 	if (scr != vga_console_vc.currenttype)
    336 		vga_console_vc.currenttype = scr;
    337 #else
    338 	scr = vga_console_vc.currenttype;
    339 #endif
    340 	vga_raster_init_screen(&vga_console_vc, &vga_console_screen, scr, 1,
    341 	    &defattr);
    342 
    343 	vga_console_screen.active = 1;
    344 	vga_console_vc.active = &vga_console_screen;
    345 
    346 	wsdisplay_cnattach(scr, &vga_console_screen,
    347 	    vga_console_screen.cursorcol, vga_console_screen.cursorrow,
    348 	    defattr);
    349 
    350 	vgaconsole = 1;
    351 	vga_console_type = type;
    352 	return (0);
    353 }
    354 
    355 void
    356 vga_raster_init(struct vga_config *vc, bus_space_tag_t iot,
    357     bus_space_tag_t memt)
    358 {
    359 	struct vga_handle *vh = &vc->hdl;
    360 	u_int8_t mor;
    361 	struct vga_raster_font *vf;
    362 
    363         vh->vh_iot = iot;
    364         vh->vh_memt = memt;
    365 
    366         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    367                 panic("vga_raster_init: couldn't map vga io");
    368 
    369 	/* read "misc output register" */
    370 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, 0xc);
    371 	vh->vh_mono = !(mor & 1);
    372 
    373 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    374 		&vh->vh_ioh_6845))
    375                 panic("vga_raster_init: couldn't map 6845 io");
    376 
    377         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    378                 panic("vga_raster_init: couldn't map memory");
    379 
    380         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
    381 		&vh->vh_memh))
    382                 panic("vga_raster_init: mem subrange failed");
    383 
    384 	/* should only reserve the space (no need to map - save KVM) */
    385 	vc->vc_biostag = memt;
    386 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0, &vc->vc_bioshdl))
    387 		vc->vc_biosmapped = 0;
    388 	else
    389 		vc->vc_biosmapped = 1;
    390 
    391 	vc->nscreens = 0;
    392 	LIST_INIT(&vc->screens);
    393 	vc->active = NULL;
    394 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
    395 	callout_init(&vc->vc_switch_callout);
    396 
    397 	vc->nfonts = 1;
    398 	LIST_INIT(&vc->vc_fontlist);
    399 	vf = &vga_console_fontset_ascii;
    400 	vga_load_builtinfont(vh, builtinfont_data, 0, 256);
    401 	vf->font = &builtinfont;
    402 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
    403 }
    404 
    405 void
    406 vga_raster_init_screen(struct vga_config *vc, struct vgascreen *scr,
    407     const struct wsscreen_descr *type, int existing, long *attrp)
    408 {
    409 	int cpos;
    410 	int res;
    411 	struct vga_handle *vh;
    412 
    413 	scr->cfg = vc;
    414 	scr->hdl = &vc->hdl;
    415 	scr->type = type;
    416 	scr->mindispoffset = 0;
    417 	scr->maxdispoffset = 0x10000;
    418 	scr->encoding = WSDISPLAY_FONTENC_IBM;
    419 	vh = &vc->hdl;
    420 
    421 	wsfont_init();
    422 	LIST_INIT(&scr->fontset);
    423 	vga_raster_setup_font(vc, scr);
    424 
    425 	if (existing) {
    426 		int i;
    427 
    428 		cpos = vga_6845_read(vh, cursorh) << 8;
    429 		cpos |= vga_6845_read(vh, cursorl);
    430 
    431 		/* make sure we have a valid cursor position */
    432 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    433 			cpos = 0;
    434 
    435 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
    436 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
    437 
    438 		/* make sure we have a valid memory offset */
    439 		if (scr->dispoffset < scr->mindispoffset ||
    440 		    scr->dispoffset > scr->maxdispoffset)
    441 			scr->dispoffset = scr->mindispoffset;
    442 
    443 		scr->mem = boot_scrmem;
    444 		scr->active = 1;
    445 
    446 		/* Save the current screen to memory. XXXBJY assume 80x25 */
    447 		for (i = 0; i < 80 * 25; i++) {
    448 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
    449 			    vh->vh_allmemh, 0x18000 + i * 2);
    450 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
    451 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
    452 			scr->mem[i].enc = WSDISPLAY_FONTENC_IBM;
    453 		}
    454 
    455 		vga_raster_setscreentype(vc, type);
    456 
    457 		/* Clear the entire screen. */
    458 		vga_gdc_write(vh, mode, 0x02);
    459 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
    460 		    0x4000);
    461 
    462 		vga_restore_screen(scr, type, scr->mem);
    463 
    464 		/* Delay to prevent the boot screen from being too
    465 		   fast scrolled up. */
    466 		delay(1000000);
    467 	} else {
    468 		cpos = 0;
    469 		scr->dispoffset = scr->mindispoffset;
    470 		scr->mem = NULL;
    471 		scr->active = 0;
    472 	}
    473 
    474 	scr->cursorrow = cpos / type->ncols;
    475 	scr->cursorcol = cpos % type->ncols;
    476 	vga_raster_cursor_init(scr, existing);
    477 
    478 #ifdef __alpha__
    479 	if (!vc->hdl.vh_mono)
    480 		/*
    481 		 * DEC firmware uses a blue background.
    482 		 */
    483 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
    484 		    WSATTR_WSCOLORS, attrp);
    485 	else
    486 #endif
    487 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
    488 #ifdef DIAGNOSTIC
    489 	if (res)
    490 		panic("vga_raster_init_screen: attribute botch");
    491 #endif
    492 
    493 	vc->nscreens++;
    494 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    495 }
    496 
    497 void
    498 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
    499     bus_space_tag_t memt, int type, int quirks, const struct vga_funcs *vf)
    500 {
    501 	int console;
    502 	struct vga_config *vc;
    503 	struct wsemuldisplaydev_attach_args aa;
    504 
    505 	console = vga_is_console(iot, type);
    506 
    507 	if (console) {
    508 		vc = &vga_console_vc;
    509 		vga_console_attached = 1;
    510 	} else {
    511 		vc = malloc(sizeof(struct vga_config), M_DEVBUF,
    512 		    M_WAITOK);
    513 		vga_raster_init(vc, iot, memt);
    514 	}
    515 
    516 	vc->vc_type = type;
    517 	vc->vc_funcs = vf;
    518 
    519 	sc->sc_vc = vc;
    520 	vc->softc = sc;
    521 
    522 	aa.console = console;
    523 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    524 	aa.accessops = &vga_raster_accessops;
    525 	aa.accesscookie = vc;
    526 
    527         config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    528 }
    529 
    530 int
    531 vga_is_console(bus_space_tag_t iot, int type)
    532 {
    533 	if (vgaconsole &&
    534 	    !vga_console_attached &&
    535 	    iot == vga_console_vc.hdl.vh_iot &&
    536 	    (vga_console_type == -1 || (type == vga_console_type)))
    537 		return (1);
    538 	return (0);
    539 }
    540 
    541 #define	VGA_TS_BLANK	0x20
    542 
    543 static int
    544 vga_get_video(struct vga_config *vc)
    545 {
    546 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_BLANK) == 0;
    547 }
    548 
    549 static void
    550 vga_set_video(struct vga_config *vc, int state)
    551 {
    552 	int val;
    553 
    554 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    555 	if (state) {					/* unblank screen */
    556 		val = vga_ts_read(&vc->hdl, mode);
    557 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_BLANK);
    558 #ifndef VGA_NO_VBLANK
    559 		val = vga_6845_read(&vc->hdl, mode);
    560 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    561 #endif
    562 	} else {					/* blank screen */
    563 		val = vga_ts_read(&vc->hdl, mode);
    564 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_BLANK);
    565 #ifndef VGA_NO_VBLANK
    566 		val = vga_6845_read(&vc->hdl, mode);
    567 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    568 #endif
    569 	}
    570 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    571 }
    572 
    573 int
    574 vga_raster_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    575 {
    576 	struct vga_config *vc = v;
    577 	const struct vga_funcs *vf = vc->vc_funcs;
    578 
    579 	switch (cmd) {
    580 	case WSDISPLAYIO_GTYPE:
    581 		*(int *)data = vc->vc_type;
    582 		return 0;
    583 
    584 	case WSDISPLAYIO_GINFO:
    585 		/* XXX should get detailed hardware information here */
    586 		return EPASSTHROUGH;
    587 
    588 	case WSDISPLAYIO_GVIDEO:
    589 #if 1
    590 		*(int *)data = (vga_get_video(vc) ? WSDISPLAYIO_VIDEO_ON :
    591 				WSDISPLAYIO_VIDEO_OFF);
    592 		return 0;
    593 #endif
    594 
    595 	case WSDISPLAYIO_SVIDEO:
    596 #if 1
    597 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    598 		return 0;
    599 #endif
    600 
    601 	case WSDISPLAYIO_GETCMAP:
    602 	case WSDISPLAYIO_PUTCMAP:
    603 	case WSDISPLAYIO_GCURPOS:
    604 	case WSDISPLAYIO_SCURPOS:
    605 	case WSDISPLAYIO_GCURMAX:
    606 	case WSDISPLAYIO_GCURSOR:
    607 	case WSDISPLAYIO_SCURSOR:
    608 		/* NONE of these operations are by the generic VGA driver. */
    609 		return EPASSTHROUGH;
    610 	}
    611 
    612 	if (vc->vc_funcs == NULL)
    613 		return (EPASSTHROUGH);
    614 
    615 	if (vf->vf_ioctl == NULL)
    616 		return (EPASSTHROUGH);
    617 
    618 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
    619 
    620 }
    621 
    622 static paddr_t
    623 vga_raster_mmap(void *v, off_t offset, int prot)
    624 {
    625 	struct vga_config *vc = v;
    626 	const struct vga_funcs *vf = vc->vc_funcs;
    627 
    628 	if (vc->vc_funcs == NULL)
    629 		return (-1);
    630 
    631 	if (vf->vf_mmap == NULL)
    632 		return (-1);
    633 
    634 	return ((*vf->vf_mmap)(v, offset, prot));
    635 }
    636 
    637 int
    638 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
    639 			void **cookiep, int *curxp, int *curyp, long *defattrp)
    640 {
    641 	struct vga_config *vc = v;
    642 	struct vgascreen *scr;
    643 
    644 	if (vc->nscreens == 1) {
    645 		vc->screens.lh_first->mem = boot_scrmem;
    646 	}
    647 
    648 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    649 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    650 
    651 	if (vc->nscreens == 1) {
    652 		scr->active = 1;
    653 		vc->active = scr;
    654 		vc->currenttype = type;
    655 	} else {
    656 		scr->mem = malloc(sizeof(struct vga_scrmem) *
    657 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
    658 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
    659 	}
    660 
    661 	*cookiep = scr;
    662 	*curxp = scr->cursorcol;
    663 	*curyp = scr->cursorrow;
    664 
    665 	return (0);
    666 }
    667 
    668 void
    669 vga_raster_free_screen(void *v, void *cookie)
    670 {
    671 	struct vgascreen *vs = cookie;
    672 	struct vga_config *vc = vs->cfg;
    673 
    674 	LIST_REMOVE(vs, next);
    675 	if (vs != &vga_console_screen)
    676 		free(vs, M_DEVBUF);
    677 	else
    678 		panic("vga_raster_free_screen: console");
    679 
    680 	if (vc->active == vs)
    681 		vc->active = 0;
    682 }
    683 
    684 int
    685 vga_raster_show_screen(void *v, void *cookie, int waitok,
    686 		       void (*cb)(void *, int, int), void *cbarg)
    687 {
    688 	struct vgascreen *scr = cookie, *oldscr;
    689 	struct vga_config *vc = scr->cfg;
    690 
    691 	oldscr = vc->active; /* can be NULL! */
    692 	if (scr == oldscr) {
    693 		return (0);
    694 	}
    695 
    696 	vc->wantedscreen = cookie;
    697 	vc->switchcb = cb;
    698 	vc->switchcbarg = cbarg;
    699 	if (cb) {
    700 		callout_reset(&vc->vc_switch_callout, 0,
    701 		    (void(*)(void *))vga_switch_screen, vc);
    702 		return (EAGAIN);
    703 	}
    704 
    705 	vga_switch_screen(vc);
    706 	return (0);
    707 }
    708 
    709 void
    710 vga_switch_screen(struct vga_config *vc)
    711 {
    712 	struct vgascreen *scr, *oldscr;
    713 	struct vga_handle *vh = &vc->hdl;
    714 	const struct wsscreen_descr *type;
    715 
    716 	scr = vc->wantedscreen;
    717 	if (!scr) {
    718 		printf("vga_switch_screen: disappeared\n");
    719 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    720 		return;
    721 	}
    722 	type = scr->type;
    723 	oldscr = vc->active; /* can be NULL! */
    724 #ifdef DIAGNOSTIC
    725 	if (oldscr) {
    726 		if (!oldscr->active)
    727 			panic("vga_raster_show_screen: not active");
    728 		if (oldscr->type != vc->currenttype)
    729 			panic("vga_raster_show_screen: bad type");
    730 	}
    731 #endif
    732 	if (scr == oldscr) {
    733 		return;
    734 	}
    735 #ifdef DIAGNOSTIC
    736 	if (scr->active)
    737 		panic("vga_raster_show_screen: active");
    738 #endif
    739 
    740 	if (oldscr)
    741 		oldscr->active = 0;
    742 
    743 	if (vc->currenttype != type) {
    744 		vga_raster_setscreentype(vc, type);
    745 		vc->currenttype = type;
    746 	}
    747 
    748 	scr->dispoffset = scr->mindispoffset;
    749 
    750 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
    751 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
    752 		vga_6845_write(vh, startadrl, scr->dispoffset);
    753 	}
    754 
    755 	/* Clear the entire screen. */
    756 	vga_gdc_write(vh, mode, 0x02);
    757 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
    758 
    759 	scr->active = 1;
    760 	vga_restore_screen(scr, type, scr->mem);
    761 
    762 	vc->active = scr;
    763 
    764 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
    765 
    766 	vc->wantedscreen = 0;
    767 	if (vc->switchcb)
    768 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    769 }
    770 
    771 static int
    772 vga_raster_load_font(void *v, void *id, struct wsdisplay_font *data)
    773 {
    774 	/* XXX */
    775 
    776 	return (0);
    777 }
    778 
    779 void
    780 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
    781 {
    782 	struct vga_raster_font *vf;
    783 	struct wsdisplay_font *wf;
    784 	int cookie;
    785 
    786 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
    787 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0)) {
    788 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
    789 			return;
    790 		}
    791 	}
    792 
    793 	cookie = wsfont_find(0, 0, scr->type->fontheight, 0,
    794 	    WSDISPLAY_FONTORDER_L2R, 0);
    795 	if (cookie == -1)
    796 		return;
    797 
    798 	if (wsfont_lock(cookie, &wf))
    799 		return;
    800 
    801 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_NOWAIT);
    802 	if (!vf) {
    803 		wsfont_unlock(cookie);
    804 		return;
    805 	}
    806 
    807 	vf->font = wf;
    808 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
    809 }
    810 
    811 void
    812 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
    813 {
    814 	int i;
    815 	int depth = 4;			/* XXXBJY hardcoded for now */
    816 	int palette[16] = {
    817 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
    818 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
    819 	};
    820 
    821 	/*
    822 	 * Compute hsync and vsync polarity.
    823 	 */
    824 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
    825 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
    826 	    	regs->miscout = 0x23;
    827 		if (mode->flags & VID_NHSYNC)
    828 			regs->miscout |= 0x40;
    829 		if (mode->flags & VID_NVSYNC)
    830 			regs->miscout |= 0x80;
    831 	} else {
    832 		if (mode->flags & VID_DBLSCAN)
    833 			mode->vdisplay *= 2;
    834 		if (mode->vdisplay < 400)
    835 			regs->miscout = 0xa3;
    836 		if (mode->vdisplay < 480)
    837 			regs->miscout = 0x63;
    838 		if (mode->vdisplay < 768)
    839 			regs->miscout = 0xe3;
    840 		else
    841 			regs->miscout = 0x23;
    842 	}
    843 
    844 	/*
    845 	 * Time sequencer
    846 	 */
    847 	if (depth == 4)
    848 		regs->ts[0] = 0x02;
    849 	else
    850 		regs->ts[0] = 0x00;
    851 	if (mode->flags & VID_CLKDIV2)
    852 		regs->ts[1] = 0x09;
    853 	else
    854 		regs->ts[1] = 0x01;
    855 	regs->ts[2] = 0x0f;
    856 	regs->ts[3] = 0x00;
    857 	if (depth < 8)
    858 		regs->ts[4] = 0x06;
    859 	else
    860 		regs->ts[4] = 0x0e;
    861 
    862 	/*
    863 	 * CRTC controller
    864 	 */
    865 	regs->crtc[0] = (mode->htotal >> 3) - 5;
    866 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
    867 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
    868 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
    869 	regs->crtc[4] = mode->hsync_start >> 3;
    870 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
    871 			    | (((mode->hsync_end >> 3)) & 0x1f);
    872 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
    873 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
    874 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
    875 	    | ((mode->vsync_start & 0x100) >> 6)
    876 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
    877 	    | 0x10
    878 	    | (((mode->vtotal - 2) & 0x200) >> 4)
    879 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
    880 	    | ((mode->vsync_start & 0x200) >> 2);
    881 	regs->crtc[8] = 0x00;
    882 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
    883 	if (mode->flags & VID_DBLSCAN)
    884 		regs->crtc[9] |= 0x80;
    885 	regs->crtc[10] = 0x00;
    886 	regs->crtc[11] = 0x00;
    887 	regs->crtc[12] = 0x00;
    888 	regs->crtc[13] = 0x00;
    889 	regs->crtc[14] = 0x00;
    890 	regs->crtc[15] = 0x00;
    891 	regs->crtc[16] = mode->vsync_start & 0xff;
    892 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
    893 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
    894 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
    895 	regs->crtc[20] = 0x00;
    896 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
    897 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
    898 	if (depth < 8)
    899 		regs->crtc[23] = 0xe3;
    900 	else
    901 		regs->crtc[23] = 0xc3;
    902 	regs->crtc[24] = 0xff;
    903 
    904 	/*
    905 	 * Graphics display controller
    906 	 */
    907 	regs->gdc[0] = 0x00;
    908 	regs->gdc[1] = 0x00;
    909 	regs->gdc[2] = 0x00;
    910 	regs->gdc[3] = 0x00;
    911 	regs->gdc[4] = 0x00;
    912 	if (depth == 4)
    913 		regs->gdc[5] = 0x02;
    914 	else
    915 		regs->gdc[5] = 0x40;
    916 	regs->gdc[6] = 0x01;
    917 	regs->gdc[7] = 0x0f;
    918 	regs->gdc[8] = 0xff;
    919 
    920 	/*
    921 	 * Attribute controller
    922 	 */
    923 	/* Set palette registers. */
    924 	for (i = 0; i < 16; i++)
    925 		regs->atc[i] = palette[i];
    926 	if (depth == 4)
    927 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
    928 	else
    929 		regs->atc[16] = 0x41;
    930 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
    931 	regs->atc[18] = 0x0f;
    932 	regs->atc[19] = 0x00;
    933 	regs->atc[20] = 0x00;
    934 }
    935 
    936 void
    937 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
    938 {
    939 	int i;
    940 
    941 	/* Disable display. */
    942 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | 0x20);
    943 
    944 	/* Write misc output register. */
    945 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
    946 	    regs->miscout);
    947 
    948 	/* Set synchronous reset. */
    949 	vga_ts_write(vh, syncreset, 0x01);
    950 	vga_ts_write(vh, mode, regs->ts[1] | 0x20);
    951 	for (i = 2; i < VGA_TS_NREGS; i++)
    952 		_vga_ts_write(vh, i, regs->ts[i]);
    953 	/* Clear synchronous reset. */
    954 	vga_ts_write(vh, syncreset, 0x03);
    955 
    956 	/* Unprotect CRTC registers 0-7. */
    957 	_vga_crtc_write(vh, 17, _vga_crtc_read(vh, 17) & 0x7f);
    958 	/* Write CRTC registers. */
    959 	for (i = 0; i < VGA_CRTC_NREGS; i++)
    960 		_vga_crtc_write(vh, i, regs->crtc[i]);
    961 
    962 	/* Write graphics display registers. */
    963 	for (i = 0; i < VGA_GDC_NREGS; i++)
    964 		_vga_gdc_write(vh, i, regs->gdc[i]);
    965 
    966 	/* Write attribute controller registers. */
    967 	for (i = 0; i < VGA_ATC_NREGS; i++)
    968 		_vga_attr_write(vh, i, regs->atc[i]);
    969 
    970 	/* Enable display. */
    971 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & 0xdf);
    972 }
    973 
    974 void
    975 vga_raster_cursor_init(struct vgascreen *scr, int existing)
    976 {
    977 	struct vga_handle *vh = scr->hdl;
    978 	bus_space_tag_t memt;
    979 	bus_space_handle_t memh;
    980 	int off;
    981 
    982 	if (existing) {
    983 		/*
    984 		 * This is the first screen. At this point, scr->active is
    985 		 * false, so we can't use vga_raster_cursor() to do this.
    986 		 */
    987 		memt = vh->vh_memt;
    988 		memh = vh->vh_memh;
    989 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
    990 		    scr->dispoffset / 8;
    991 
    992 		scr->cursortmp = scr->mem[off];
    993 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
    994 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
    995 	} else {
    996 		scr->cursortmp.ch = 0;
    997 		scr->cursortmp.attr = 0;
    998 		scr->cursortmp.second = 0;
    999 		scr->cursortmp.enc = WSDISPLAY_FONTENC_IBM;
   1000 	}
   1001 
   1002 	scr->cursoron = 1;
   1003 }
   1004 
   1005 void
   1006 vga_raster_cursor(void *id, int on, int row, int col)
   1007 {
   1008 	struct vgascreen *scr = id;
   1009 	int off, tmp;
   1010 
   1011 	/* Remove old cursor image */
   1012 	if (scr->cursoron) {
   1013 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1014 		if (scr->active) {
   1015 			tmp = scr->encoding;
   1016 			scr->encoding = scr->cursortmp.enc;
   1017 			if (scr->cursortmp.second)
   1018 				vga_raster_putchar(id, scr->cursorrow,
   1019 				    scr->cursorcol - 1, scr->cursortmp.ch,
   1020 				    scr->cursortmp.attr);
   1021 			else
   1022 				vga_raster_putchar(id, scr->cursorrow,
   1023 				    scr->cursorcol, scr->cursortmp.ch,
   1024 				    scr->cursortmp.attr);
   1025 			scr->encoding = tmp;
   1026 		}
   1027 	}
   1028 
   1029 	scr->cursorrow = row;
   1030 	scr->cursorcol = col;
   1031 
   1032 	if ((scr->cursoron = on) == 0)
   1033 		return;
   1034 
   1035 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1036 	scr->cursortmp = scr->mem[off];
   1037 	if (scr->active) {
   1038 		tmp = scr->encoding;
   1039 		scr->encoding = scr->cursortmp.enc;
   1040 		if (scr->cursortmp.second)
   1041 			vga_raster_putchar(id, scr->cursorrow,
   1042 			    scr->cursorcol - 1, scr->cursortmp.ch,
   1043 			    scr->cursortmp.attr ^ 0x77);
   1044 		else
   1045 			vga_raster_putchar(id, scr->cursorrow,
   1046 			    scr->cursorcol, scr->cursortmp.ch,
   1047 			    scr->cursortmp.attr ^ 0x77);
   1048 		scr->encoding = tmp;
   1049 	}
   1050 }
   1051 
   1052 static int
   1053 vga_raster_mapchar(void *id, int uni, u_int *index)
   1054 {
   1055 	struct vgascreen *scr = id;
   1056 
   1057 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
   1058 		return pcdisplay_mapchar(id, uni, index);
   1059 	else {
   1060 		*index = uni;
   1061 		return 5;
   1062 	}
   1063 }
   1064 
   1065 void
   1066 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
   1067 {
   1068 	struct vgascreen *scr = id;
   1069 	int off;
   1070 	struct vga_raster_font *fs;
   1071 	u_int tmp_ch;
   1072 
   1073 	off = row * scr->type->ncols + col;
   1074 
   1075 	LIST_FOREACH(fs, &scr->fontset, next) {
   1076 		if ((scr->encoding == fs->font->encoding) &&
   1077 		    (c >= fs->font->firstchar) &&
   1078 		    (c < fs->font->firstchar + fs->font->numchars) &&
   1079 		    (scr->type->fontheight == fs->font->fontheight)) {
   1080 			if (scr->active) {
   1081 				tmp_ch = c - fs->font->firstchar;
   1082 				_vga_raster_putchar(scr, row, col, tmp_ch,
   1083 				    attr, fs);
   1084 			}
   1085 
   1086 			scr->mem[off].ch = c;
   1087 			scr->mem[off].attr = attr;
   1088 			scr->mem[off].second = 0;
   1089 			scr->mem[off].enc = fs->font->encoding;
   1090 
   1091 			if (fs->font->stride == 2) {
   1092 				scr->mem[off + 1].ch = c;
   1093 				scr->mem[off + 1].attr = attr;
   1094 				scr->mem[off + 1].second = 1;
   1095 				scr->mem[off + 1].enc = fs->font->encoding;
   1096 			}
   1097 
   1098 			return;
   1099 		}
   1100 	}
   1101 
   1102 	/*
   1103 	 * No match found.
   1104 	 */
   1105 	if (scr->active)
   1106 		/* Put a single width space character no matter what the
   1107 		   actual width of the character is. */
   1108 		_vga_raster_putchar(scr, row, col, 0x20, attr,
   1109 		    &vga_console_fontset_ascii);
   1110 	scr->mem[off].ch = c;
   1111 	scr->mem[off].attr = attr;
   1112 	scr->mem[off].second = 0;
   1113 	scr->mem[off].enc = WSDISPLAY_FONTENC_IBM;
   1114 }
   1115 
   1116 static void
   1117 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
   1118 		    struct vga_raster_font *fs)
   1119 {
   1120 	struct vgascreen *scr = id;
   1121 	struct vga_handle *vh = scr->hdl;
   1122 	bus_space_tag_t memt = vh->vh_memt;
   1123 	bus_space_handle_t memh = vh->vh_memh;
   1124 	int i;
   1125 	int rasoff, rasoff2;
   1126 	int fheight = scr->type->fontheight;
   1127 	volatile u_int8_t dummy, pattern;
   1128 	u_int8_t fgcolor, bgcolor;
   1129 
   1130 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
   1131 	rasoff2 = rasoff;
   1132 
   1133 #if 0
   1134 	bgcolor = bgansitopc[attr >> 4];
   1135 	fgcolor = fgansitopc[attr & 0x0f];
   1136 #else
   1137 	bgcolor = ((attr >> 4) & 0x0f);
   1138 	fgcolor = attr & 0x0f;
   1139 #endif
   1140 
   1141 	if (fs->font->stride == 1) {
   1142 		/* Paint background. */
   1143 		vga_gdc_write(vh, mode, 0x02);
   1144 		for (i = 0; i < fheight; i++) {
   1145 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1146 			rasoff += scr->type->ncols;
   1147 		}
   1148 
   1149 		/* Draw a single width character. */
   1150 		vga_gdc_write(vh, mode, 0x03);
   1151 		vga_gdc_write(vh, setres, fgcolor);
   1152 		for (i = 0; i < fheight; i++) {
   1153 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
   1154 			/* When pattern is 0, skip output for speed-up. */
   1155 			if (pattern != 0) {
   1156 				dummy = bus_space_read_1(memt, memh, rasoff2);
   1157 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1158 			}
   1159 			rasoff2 += scr->type->ncols;
   1160 		}
   1161 	} else if (fs->font->stride == 2) {
   1162 		/* Paint background. */
   1163 		vga_gdc_write(vh, mode, 0x02);
   1164 		for (i = 0; i < fheight; i++) {
   1165 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1166 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
   1167 			rasoff += scr->type->ncols;
   1168 		}
   1169 
   1170 		/* Draw a double width character. */
   1171 		vga_gdc_write(vh, mode, 0x03);
   1172 		vga_gdc_write(vh, setres, fgcolor);
   1173 		for (i = 0; i < fheight; i++) {
   1174 			pattern = ((u_int8_t *)fs->font->data)
   1175 			    [(c * fheight + i) * 2];
   1176 			if (pattern != 0) {
   1177 				dummy = bus_space_read_1(memt, memh, rasoff2);
   1178 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1179 			}
   1180 			pattern = ((u_int8_t *)fs->font->data)
   1181 			    [(c * fheight + i) * 2 + 1];
   1182 			if (pattern != 0) {
   1183 				rasoff2++;
   1184 				dummy = bus_space_read_1(memt, memh, rasoff2);
   1185 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1186 				rasoff2--;
   1187 			}
   1188 			rasoff2 += scr->type->ncols;
   1189 		}
   1190 	}
   1191 }
   1192 
   1193 void
   1194 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
   1195 {
   1196 	struct vgascreen *scr = id;
   1197 	struct vga_handle *vh = scr->hdl;
   1198 	bus_space_tag_t memt = vh->vh_memt;
   1199 	bus_space_handle_t memh = vh->vh_memh;
   1200 	bus_size_t srcoff, dstoff;
   1201 	bus_size_t rassrcoff, rasdstoff;
   1202 	int i;
   1203 	int fheight = scr->type->fontheight;
   1204 
   1205 	srcoff = row * scr->type->ncols + srccol;
   1206 	dstoff = row * scr->type->ncols + dstcol;
   1207 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
   1208 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
   1209 
   1210 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1211 	    ncols * sizeof(struct vga_scrmem));
   1212 
   1213 	vga_gdc_write(vh, mode, 0x01);
   1214 	if (scr->active) {
   1215 		for (i = 0; i < fheight; i++) {
   1216 			bus_space_copy_region_1(memt, memh,
   1217 			    rassrcoff + i * scr->type->ncols, memh,
   1218 			    rasdstoff + i * scr->type->ncols, ncols);
   1219 		}
   1220 	}
   1221 }
   1222 
   1223 void
   1224 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
   1225 {
   1226 	struct vgascreen *scr = id;
   1227 	int i;
   1228 
   1229 	if (scr->active == 0)
   1230 		return;
   1231 
   1232 	for (i = startcol; i < startcol + ncols; i++)
   1233 		vga_raster_putchar(id, row, i, ' ', fillattr);
   1234 }
   1235 
   1236 void
   1237 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1238 {
   1239 	struct vgascreen *scr = id;
   1240 	struct vga_handle *vh = scr->hdl;
   1241 	bus_space_tag_t memt = vh->vh_memt;
   1242 	bus_space_handle_t memh = vh->vh_memh;
   1243 	int ncols;
   1244 	bus_size_t srcoff, dstoff;
   1245 	bus_size_t rassrcoff, rasdstoff;
   1246 	int fheight;
   1247 
   1248 	ncols = scr->type->ncols;
   1249 	fheight = scr->type->fontheight;
   1250 
   1251 	srcoff = srcrow * ncols;
   1252 	dstoff = dstrow * ncols;
   1253 	rassrcoff = srcoff * fheight;
   1254 	rasdstoff = dstoff * fheight;
   1255 
   1256 	if (scr->active) {
   1257 		vga_gdc_write(vh, mode, 0x01);
   1258 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
   1259 			int cursoron = scr->cursoron;
   1260 
   1261 			if (cursoron)
   1262 				/* Disable cursor. */
   1263 				vga_raster_cursor(scr, 0,
   1264 				    scr->cursorrow, scr->cursorcol);
   1265 
   1266 			/* scroll up whole screen */
   1267 			if ((scr->dispoffset + srcrow * ncols * fheight)
   1268 			    <= scr->maxdispoffset)
   1269 				scr->dispoffset += srcrow * ncols * fheight;
   1270 			else {
   1271 				bus_space_copy_region_1(memt, memh,
   1272 				    scr->dispoffset + rassrcoff,
   1273 				    memh, scr->mindispoffset,
   1274 				    nrows * ncols * fheight);
   1275 				scr->dispoffset = scr->mindispoffset;
   1276 			}
   1277 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
   1278 			vga_6845_write(vh, startadrl, scr->dispoffset);
   1279 
   1280 			if (cursoron)
   1281 				/* Enable cursor. */
   1282 				vga_raster_cursor(scr, 1, scr->cursorrow,
   1283 				    scr->cursorcol);
   1284 		} else
   1285 			bus_space_copy_region_1(memt, memh,
   1286 			    scr->dispoffset + rassrcoff, memh,
   1287 			    scr->dispoffset + rasdstoff,
   1288 			    nrows * ncols * fheight);
   1289 	}
   1290 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1291 	    nrows * ncols * sizeof(struct vga_scrmem));
   1292 }
   1293 
   1294 void
   1295 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
   1296 {
   1297 	struct vgascreen *scr = id;
   1298 	struct vga_handle *vh = scr->hdl;
   1299 	bus_space_tag_t memt = vh->vh_memt;
   1300 	bus_space_handle_t memh = vh->vh_memh;
   1301 	bus_size_t off, count;
   1302 	bus_size_t rasoff, rascount;
   1303 	int i;
   1304 
   1305 	off = startrow * scr->type->ncols;
   1306 	count = nrows * scr->type->ncols;
   1307 	rasoff = off * scr->type->fontheight;
   1308 	rascount = count * scr->type->fontheight;
   1309 
   1310 	if (scr->active) {
   1311 		/* Paint background. */
   1312 		vga_gdc_write(vh, mode, 0x02);
   1313 		if (scr->type->ncols % 4 == 0)
   1314 			/* We can speed up I/O */
   1315 			for (i = rasoff; i < rasoff + rascount; i += 4)
   1316 				bus_space_write_4(memt, memh,
   1317 				    scr->dispoffset + i, fillattr >> 4);
   1318 		else
   1319 			for (i = rasoff; i < rasoff + rascount; i += 2)
   1320 				bus_space_write_2(memt, memh,
   1321 				    scr->dispoffset + i, fillattr >> 4);
   1322 	}
   1323 	for (i = 0; i < count; i++) {
   1324 		scr->mem[off + i].ch = ' ';
   1325 		scr->mem[off + i].attr = fillattr;
   1326 		scr->mem[off + i].second = 0;
   1327 		scr->mem[off + i].enc = WSDISPLAY_FONTENC_IBM;
   1328 	}
   1329 }
   1330 
   1331 static int
   1332 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
   1333 {
   1334 	struct vgascreen *scr = id;
   1335 	struct vga_config *vc = scr->cfg;
   1336 
   1337 	if (vc->hdl.vh_mono) {
   1338 		if (flags & WSATTR_WSCOLORS)
   1339 			return (EINVAL);
   1340 		if (flags & WSATTR_REVERSE)
   1341 			*attrp = 0x70;
   1342 		else
   1343 			*attrp = 0x07;
   1344 		if (flags & WSATTR_UNDERLINE)
   1345 			*attrp |= FG_UNDERLINE;
   1346 		if (flags & WSATTR_HILIT)
   1347 			*attrp |= FG_INTENSE;
   1348 	} else {
   1349 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1350 			return (EINVAL);
   1351 		if (flags & WSATTR_WSCOLORS)
   1352 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1353 		else
   1354 			*attrp = 7;
   1355 		if (flags & WSATTR_HILIT)
   1356 			*attrp += 8;
   1357 	}
   1358 	if (flags & WSATTR_BLINK)
   1359 		*attrp |= FG_BLINK;
   1360 	return (0);
   1361 }
   1362 
   1363 void
   1364 vga_restore_screen(struct vgascreen *scr,
   1365     const struct wsscreen_descr *type, struct vga_scrmem *mem)
   1366 {
   1367 	int i, j, off, tmp;
   1368 
   1369 	tmp = scr->encoding;
   1370 	for (i = 0; i < type->nrows; i++) {
   1371 		for (j = 0; j < type->ncols; j++) {
   1372 			off = i * type->ncols + j;
   1373 			if (mem[off].second != 1) {
   1374 				scr->encoding = mem[off].enc;
   1375 				vga_raster_putchar(scr, i, j, mem[off].ch,
   1376 				    mem[off].attr);
   1377 			}
   1378 		}
   1379 	}
   1380 	scr->encoding = tmp;
   1381 }
   1382 
   1383 void
   1384 vga_raster_setscreentype(struct vga_config *vc,
   1385     const struct wsscreen_descr *type)
   1386 {
   1387 	struct vga_handle *vh = &vc->hdl;
   1388 	struct vga_moderegs moderegs;
   1389 
   1390 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
   1391 	vga_set_mode(vh, &moderegs);
   1392 }
   1393