Home | History | Annotate | Line # | Download | only in ic
vga_raster.c revision 1.7
      1 /*	$NetBSD: vga_raster.c,v 1.7 2003/01/27 15:16:11 tsutsui 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[MC6845_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, VGA_MISC_DATAR);
    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, M_WAITOK);
    512 		vga_raster_init(vc, iot, memt);
    513 	}
    514 
    515 	vc->vc_type = type;
    516 	vc->vc_funcs = vf;
    517 
    518 	sc->sc_vc = vc;
    519 	vc->softc = sc;
    520 
    521 	aa.console = console;
    522 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    523 	aa.accessops = &vga_raster_accessops;
    524 	aa.accesscookie = vc;
    525 
    526 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    527 }
    528 
    529 int
    530 vga_is_console(bus_space_tag_t iot, int type)
    531 {
    532 	if (vgaconsole &&
    533 	    !vga_console_attached &&
    534 	    iot == vga_console_vc.hdl.vh_iot &&
    535 	    (vga_console_type == -1 || (type == vga_console_type)))
    536 		return (1);
    537 	return (0);
    538 }
    539 
    540 static int
    541 vga_get_video(struct vga_config *vc)
    542 {
    543 
    544 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
    545 }
    546 
    547 static void
    548 vga_set_video(struct vga_config *vc, int state)
    549 {
    550 	int val;
    551 
    552 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    553 	if (state) {					/* unblank screen */
    554 		val = vga_ts_read(&vc->hdl, mode);
    555 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
    556 #ifndef VGA_NO_VBLANK
    557 		val = vga_6845_read(&vc->hdl, mode);
    558 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    559 #endif
    560 	} else {					/* blank screen */
    561 		val = vga_ts_read(&vc->hdl, mode);
    562 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
    563 #ifndef VGA_NO_VBLANK
    564 		val = vga_6845_read(&vc->hdl, mode);
    565 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    566 #endif
    567 	}
    568 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    569 }
    570 
    571 int
    572 vga_raster_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    573 {
    574 	struct vga_config *vc = v;
    575 	const struct vga_funcs *vf = vc->vc_funcs;
    576 
    577 	switch (cmd) {
    578 	case WSDISPLAYIO_GTYPE:
    579 		*(int *)data = vc->vc_type;
    580 		return 0;
    581 
    582 	case WSDISPLAYIO_GINFO:
    583 		/* XXX should get detailed hardware information here */
    584 		return EPASSTHROUGH;
    585 
    586 	case WSDISPLAYIO_GVIDEO:
    587 #if 1
    588 		*(int *)data = (vga_get_video(vc) ?
    589 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
    590 		return 0;
    591 #endif
    592 
    593 	case WSDISPLAYIO_SVIDEO:
    594 #if 1
    595 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    596 		return 0;
    597 #endif
    598 
    599 	case WSDISPLAYIO_GETCMAP:
    600 	case WSDISPLAYIO_PUTCMAP:
    601 	case WSDISPLAYIO_GCURPOS:
    602 	case WSDISPLAYIO_SCURPOS:
    603 	case WSDISPLAYIO_GCURMAX:
    604 	case WSDISPLAYIO_GCURSOR:
    605 	case WSDISPLAYIO_SCURSOR:
    606 		/* NONE of these operations are by the generic VGA driver. */
    607 		return EPASSTHROUGH;
    608 	}
    609 
    610 	if (vc->vc_funcs == NULL)
    611 		return (EPASSTHROUGH);
    612 
    613 	if (vf->vf_ioctl == NULL)
    614 		return (EPASSTHROUGH);
    615 
    616 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
    617 }
    618 
    619 static paddr_t
    620 vga_raster_mmap(void *v, off_t offset, int prot)
    621 {
    622 	struct vga_config *vc = v;
    623 	const struct vga_funcs *vf = vc->vc_funcs;
    624 
    625 	if (vc->vc_funcs == NULL)
    626 		return (-1);
    627 
    628 	if (vf->vf_mmap == NULL)
    629 		return (-1);
    630 
    631 	return ((*vf->vf_mmap)(v, offset, prot));
    632 }
    633 
    634 int
    635 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
    636 			void **cookiep, int *curxp, int *curyp, long *defattrp)
    637 {
    638 	struct vga_config *vc = v;
    639 	struct vgascreen *scr;
    640 
    641 	if (vc->nscreens == 1) {
    642 		vc->screens.lh_first->mem = boot_scrmem;
    643 	}
    644 
    645 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    646 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    647 
    648 	if (vc->nscreens == 1) {
    649 		scr->active = 1;
    650 		vc->active = scr;
    651 		vc->currenttype = type;
    652 	} else {
    653 		scr->mem = malloc(sizeof(struct vga_scrmem) *
    654 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
    655 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
    656 	}
    657 
    658 	*cookiep = scr;
    659 	*curxp = scr->cursorcol;
    660 	*curyp = scr->cursorrow;
    661 
    662 	return (0);
    663 }
    664 
    665 void
    666 vga_raster_free_screen(void *v, void *cookie)
    667 {
    668 	struct vgascreen *vs = cookie;
    669 	struct vga_config *vc = vs->cfg;
    670 
    671 	LIST_REMOVE(vs, next);
    672 	if (vs != &vga_console_screen)
    673 		free(vs, M_DEVBUF);
    674 	else
    675 		panic("vga_raster_free_screen: console");
    676 
    677 	if (vc->active == vs)
    678 		vc->active = 0;
    679 }
    680 
    681 int
    682 vga_raster_show_screen(void *v, void *cookie, int waitok,
    683 		       void (*cb)(void *, int, int), void *cbarg)
    684 {
    685 	struct vgascreen *scr = cookie, *oldscr;
    686 	struct vga_config *vc = scr->cfg;
    687 
    688 	oldscr = vc->active; /* can be NULL! */
    689 	if (scr == oldscr) {
    690 		return (0);
    691 	}
    692 
    693 	vc->wantedscreen = cookie;
    694 	vc->switchcb = cb;
    695 	vc->switchcbarg = cbarg;
    696 	if (cb) {
    697 		callout_reset(&vc->vc_switch_callout, 0,
    698 		    (void(*)(void *))vga_switch_screen, vc);
    699 		return (EAGAIN);
    700 	}
    701 
    702 	vga_switch_screen(vc);
    703 	return (0);
    704 }
    705 
    706 void
    707 vga_switch_screen(struct vga_config *vc)
    708 {
    709 	struct vgascreen *scr, *oldscr;
    710 	struct vga_handle *vh = &vc->hdl;
    711 	const struct wsscreen_descr *type;
    712 
    713 	scr = vc->wantedscreen;
    714 	if (!scr) {
    715 		printf("vga_switch_screen: disappeared\n");
    716 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    717 		return;
    718 	}
    719 	type = scr->type;
    720 	oldscr = vc->active; /* can be NULL! */
    721 #ifdef DIAGNOSTIC
    722 	if (oldscr) {
    723 		if (!oldscr->active)
    724 			panic("vga_raster_show_screen: not active");
    725 		if (oldscr->type != vc->currenttype)
    726 			panic("vga_raster_show_screen: bad type");
    727 	}
    728 #endif
    729 	if (scr == oldscr) {
    730 		return;
    731 	}
    732 #ifdef DIAGNOSTIC
    733 	if (scr->active)
    734 		panic("vga_raster_show_screen: active");
    735 #endif
    736 
    737 	if (oldscr)
    738 		oldscr->active = 0;
    739 
    740 	if (vc->currenttype != type) {
    741 		vga_raster_setscreentype(vc, type);
    742 		vc->currenttype = type;
    743 	}
    744 
    745 	scr->dispoffset = scr->mindispoffset;
    746 
    747 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
    748 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
    749 		vga_6845_write(vh, startadrl, scr->dispoffset);
    750 	}
    751 
    752 	/* Clear the entire screen. */
    753 	vga_gdc_write(vh, mode, 0x02);
    754 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
    755 
    756 	scr->active = 1;
    757 	vga_restore_screen(scr, type, scr->mem);
    758 
    759 	vc->active = scr;
    760 
    761 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
    762 
    763 	vc->wantedscreen = 0;
    764 	if (vc->switchcb)
    765 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    766 }
    767 
    768 static int
    769 vga_raster_load_font(void *v, void *id, struct wsdisplay_font *data)
    770 {
    771 	/* XXX */
    772 
    773 	return (0);
    774 }
    775 
    776 void
    777 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
    778 {
    779 	struct vga_raster_font *vf;
    780 	struct wsdisplay_font *wf;
    781 	int cookie;
    782 
    783 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
    784 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0)) {
    785 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
    786 			return;
    787 		}
    788 	}
    789 
    790 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
    791 	    WSDISPLAY_FONTORDER_L2R, 0);
    792 	if (cookie == -1)
    793 		return;
    794 
    795 	if (wsfont_lock(cookie, &wf))
    796 		return;
    797 
    798 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_NOWAIT);
    799 	if (!vf) {
    800 		wsfont_unlock(cookie);
    801 		return;
    802 	}
    803 
    804 	vf->font = wf;
    805 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
    806 }
    807 
    808 void
    809 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
    810 {
    811 	int i;
    812 	int depth = 4;			/* XXXBJY hardcoded for now */
    813 	int palette[16] = {
    814 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
    815 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
    816 	};
    817 
    818 	/*
    819 	 * Compute hsync and vsync polarity.
    820 	 */
    821 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
    822 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
    823 	    	regs->miscout = 0x23;
    824 		if (mode->flags & VID_NHSYNC)
    825 			regs->miscout |= 0x40;
    826 		if (mode->flags & VID_NVSYNC)
    827 			regs->miscout |= 0x80;
    828 	} else {
    829 		if (mode->flags & VID_DBLSCAN)
    830 			mode->vdisplay *= 2;
    831 		if (mode->vdisplay < 400)
    832 			regs->miscout = 0xa3;
    833 		if (mode->vdisplay < 480)
    834 			regs->miscout = 0x63;
    835 		if (mode->vdisplay < 768)
    836 			regs->miscout = 0xe3;
    837 		else
    838 			regs->miscout = 0x23;
    839 	}
    840 
    841 	/*
    842 	 * Time sequencer
    843 	 */
    844 	if (depth == 4)
    845 		regs->ts[0] = 0x02;
    846 	else
    847 		regs->ts[0] = 0x00;
    848 	if (mode->flags & VID_CLKDIV2)
    849 		regs->ts[1] = 0x09;
    850 	else
    851 		regs->ts[1] = 0x01;
    852 	regs->ts[2] = 0x0f;
    853 	regs->ts[3] = 0x00;
    854 	if (depth < 8)
    855 		regs->ts[4] = 0x06;
    856 	else
    857 		regs->ts[4] = 0x0e;
    858 
    859 	/*
    860 	 * CRTC controller
    861 	 */
    862 	regs->crtc[0] = (mode->htotal >> 3) - 5;
    863 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
    864 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
    865 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
    866 	regs->crtc[4] = mode->hsync_start >> 3;
    867 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
    868 	    | (((mode->hsync_end >> 3)) & 0x1f);
    869 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
    870 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
    871 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
    872 	    | ((mode->vsync_start & 0x100) >> 6)
    873 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
    874 	    | 0x10
    875 	    | (((mode->vtotal - 2) & 0x200) >> 4)
    876 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
    877 	    | ((mode->vsync_start & 0x200) >> 2);
    878 	regs->crtc[8] = 0x00;
    879 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
    880 	if (mode->flags & VID_DBLSCAN)
    881 		regs->crtc[9] |= 0x80;
    882 	regs->crtc[10] = 0x00;
    883 	regs->crtc[11] = 0x00;
    884 	regs->crtc[12] = 0x00;
    885 	regs->crtc[13] = 0x00;
    886 	regs->crtc[14] = 0x00;
    887 	regs->crtc[15] = 0x00;
    888 	regs->crtc[16] = mode->vsync_start & 0xff;
    889 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
    890 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
    891 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
    892 	regs->crtc[20] = 0x00;
    893 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
    894 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
    895 	if (depth < 8)
    896 		regs->crtc[23] = 0xe3;
    897 	else
    898 		regs->crtc[23] = 0xc3;
    899 	regs->crtc[24] = 0xff;
    900 
    901 	/*
    902 	 * Graphics display controller
    903 	 */
    904 	regs->gdc[0] = 0x00;
    905 	regs->gdc[1] = 0x00;
    906 	regs->gdc[2] = 0x00;
    907 	regs->gdc[3] = 0x00;
    908 	regs->gdc[4] = 0x00;
    909 	if (depth == 4)
    910 		regs->gdc[5] = 0x02;
    911 	else
    912 		regs->gdc[5] = 0x40;
    913 	regs->gdc[6] = 0x01;
    914 	regs->gdc[7] = 0x0f;
    915 	regs->gdc[8] = 0xff;
    916 
    917 	/*
    918 	 * Attribute controller
    919 	 */
    920 	/* Set palette registers. */
    921 	for (i = 0; i < 16; i++)
    922 		regs->atc[i] = palette[i];
    923 	if (depth == 4)
    924 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
    925 	else
    926 		regs->atc[16] = 0x41;
    927 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
    928 	regs->atc[18] = 0x0f;
    929 	regs->atc[19] = 0x00;
    930 	regs->atc[20] = 0x00;
    931 }
    932 
    933 void
    934 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
    935 {
    936 	int i;
    937 
    938 	/* Disable display. */
    939 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
    940 
    941 	/* Write misc output register. */
    942 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
    943 	    regs->miscout);
    944 
    945 	/* Set synchronous reset. */
    946 	vga_ts_write(vh, syncreset, 0x01);
    947 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
    948 	for (i = 2; i < VGA_TS_NREGS; i++)
    949 		_vga_ts_write(vh, i, regs->ts[i]);
    950 	/* Clear synchronous reset. */
    951 	vga_ts_write(vh, syncreset, 0x03);
    952 
    953 	/* Unprotect CRTC registers 0-7. */
    954 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
    955 	/* Write CRTC registers. */
    956 	for (i = 0; i < MC6845_NREGS; i++)
    957 		_vga_6845_write(vh, i, regs->crtc[i]);
    958 
    959 	/* Write graphics display registers. */
    960 	for (i = 0; i < VGA_GDC_NREGS; i++)
    961 		_vga_gdc_write(vh, i, regs->gdc[i]);
    962 
    963 	/* Write attribute controller registers. */
    964 	for (i = 0; i < VGA_ATC_NREGS; i++)
    965 		_vga_attr_write(vh, i, regs->atc[i]);
    966 
    967 	/* Enable display. */
    968 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
    969 }
    970 
    971 void
    972 vga_raster_cursor_init(struct vgascreen *scr, int existing)
    973 {
    974 	struct vga_handle *vh = scr->hdl;
    975 	bus_space_tag_t memt;
    976 	bus_space_handle_t memh;
    977 	int off;
    978 
    979 	if (existing) {
    980 		/*
    981 		 * This is the first screen. At this point, scr->active is
    982 		 * false, so we can't use vga_raster_cursor() to do this.
    983 		 */
    984 		memt = vh->vh_memt;
    985 		memh = vh->vh_memh;
    986 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
    987 		    scr->dispoffset / 8;
    988 
    989 		scr->cursortmp = scr->mem[off];
    990 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
    991 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
    992 	} else {
    993 		scr->cursortmp.ch = 0;
    994 		scr->cursortmp.attr = 0;
    995 		scr->cursortmp.second = 0;
    996 		scr->cursortmp.enc = WSDISPLAY_FONTENC_IBM;
    997 	}
    998 
    999 	scr->cursoron = 1;
   1000 }
   1001 
   1002 void
   1003 vga_raster_cursor(void *id, int on, int row, int col)
   1004 {
   1005 	struct vgascreen *scr = id;
   1006 	int off, tmp;
   1007 
   1008 	/* Remove old cursor image */
   1009 	if (scr->cursoron) {
   1010 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1011 		if (scr->active) {
   1012 			tmp = scr->encoding;
   1013 			scr->encoding = scr->cursortmp.enc;
   1014 			if (scr->cursortmp.second)
   1015 				vga_raster_putchar(id, scr->cursorrow,
   1016 				    scr->cursorcol - 1, scr->cursortmp.ch,
   1017 				    scr->cursortmp.attr);
   1018 			else
   1019 				vga_raster_putchar(id, scr->cursorrow,
   1020 				    scr->cursorcol, scr->cursortmp.ch,
   1021 				    scr->cursortmp.attr);
   1022 			scr->encoding = tmp;
   1023 		}
   1024 	}
   1025 
   1026 	scr->cursorrow = row;
   1027 	scr->cursorcol = col;
   1028 
   1029 	if ((scr->cursoron = on) == 0)
   1030 		return;
   1031 
   1032 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1033 	scr->cursortmp = scr->mem[off];
   1034 	if (scr->active) {
   1035 		tmp = scr->encoding;
   1036 		scr->encoding = scr->cursortmp.enc;
   1037 		if (scr->cursortmp.second)
   1038 			vga_raster_putchar(id, scr->cursorrow,
   1039 			    scr->cursorcol - 1, scr->cursortmp.ch,
   1040 			    scr->cursortmp.attr ^ 0x77);
   1041 		else
   1042 			vga_raster_putchar(id, scr->cursorrow,
   1043 			    scr->cursorcol, scr->cursortmp.ch,
   1044 			    scr->cursortmp.attr ^ 0x77);
   1045 		scr->encoding = tmp;
   1046 	}
   1047 }
   1048 
   1049 static int
   1050 vga_raster_mapchar(void *id, int uni, u_int *index)
   1051 {
   1052 	struct vgascreen *scr = id;
   1053 
   1054 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
   1055 		return pcdisplay_mapchar(id, uni, index);
   1056 	else {
   1057 		*index = uni;
   1058 		return 5;
   1059 	}
   1060 }
   1061 
   1062 void
   1063 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
   1064 {
   1065 	struct vgascreen *scr = id;
   1066 	int off;
   1067 	struct vga_raster_font *fs;
   1068 	u_int tmp_ch;
   1069 
   1070 	off = row * scr->type->ncols + col;
   1071 
   1072 	LIST_FOREACH(fs, &scr->fontset, next) {
   1073 		if ((scr->encoding == fs->font->encoding) &&
   1074 		    (c >= fs->font->firstchar) &&
   1075 		    (c < fs->font->firstchar + fs->font->numchars) &&
   1076 		    (scr->type->fontheight == fs->font->fontheight)) {
   1077 			if (scr->active) {
   1078 				tmp_ch = c - fs->font->firstchar;
   1079 				_vga_raster_putchar(scr, row, col, tmp_ch,
   1080 				    attr, fs);
   1081 			}
   1082 
   1083 			scr->mem[off].ch = c;
   1084 			scr->mem[off].attr = attr;
   1085 			scr->mem[off].second = 0;
   1086 			scr->mem[off].enc = fs->font->encoding;
   1087 
   1088 			if (fs->font->stride == 2) {
   1089 				scr->mem[off + 1].ch = c;
   1090 				scr->mem[off + 1].attr = attr;
   1091 				scr->mem[off + 1].second = 1;
   1092 				scr->mem[off + 1].enc = fs->font->encoding;
   1093 			}
   1094 
   1095 			return;
   1096 		}
   1097 	}
   1098 
   1099 	/*
   1100 	 * No match found.
   1101 	 */
   1102 	if (scr->active)
   1103 		/*
   1104 		 * Put a single width space character no matter what the
   1105 		 * actual width of the character is.
   1106 		 */
   1107 		_vga_raster_putchar(scr, row, col, ' ', attr,
   1108 		    &vga_console_fontset_ascii);
   1109 	scr->mem[off].ch = c;
   1110 	scr->mem[off].attr = attr;
   1111 	scr->mem[off].second = 0;
   1112 	scr->mem[off].enc = WSDISPLAY_FONTENC_IBM;
   1113 }
   1114 
   1115 static void
   1116 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
   1117 		    struct vga_raster_font *fs)
   1118 {
   1119 	struct vgascreen *scr = id;
   1120 	struct vga_handle *vh = scr->hdl;
   1121 	bus_space_tag_t memt = vh->vh_memt;
   1122 	bus_space_handle_t memh = vh->vh_memh;
   1123 	int i;
   1124 	int rasoff, rasoff2;
   1125 	int fheight = scr->type->fontheight;
   1126 	volatile u_int8_t dummy, pattern;
   1127 	u_int8_t fgcolor, bgcolor;
   1128 
   1129 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
   1130 	rasoff2 = rasoff;
   1131 
   1132 #if 0
   1133 	bgcolor = bgansitopc[attr >> 4];
   1134 	fgcolor = fgansitopc[attr & 0x0f];
   1135 #else
   1136 	bgcolor = ((attr >> 4) & 0x0f);
   1137 	fgcolor = attr & 0x0f;
   1138 #endif
   1139 
   1140 	if (fs->font->stride == 1) {
   1141 		/* Paint background. */
   1142 		vga_gdc_write(vh, mode, 0x02);
   1143 		for (i = 0; i < fheight; i++) {
   1144 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1145 			rasoff += scr->type->ncols;
   1146 		}
   1147 
   1148 		/* Draw a single width character. */
   1149 		vga_gdc_write(vh, mode, 0x03);
   1150 		vga_gdc_write(vh, setres, fgcolor);
   1151 		for (i = 0; i < fheight; i++) {
   1152 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
   1153 			/* When pattern is 0, skip output for speed-up. */
   1154 			if (pattern != 0) {
   1155 				dummy = bus_space_read_1(memt, memh, rasoff2);
   1156 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1157 			}
   1158 			rasoff2 += scr->type->ncols;
   1159 		}
   1160 	} else if (fs->font->stride == 2) {
   1161 		/* Paint background. */
   1162 		vga_gdc_write(vh, mode, 0x02);
   1163 		for (i = 0; i < fheight; i++) {
   1164 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1165 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
   1166 			rasoff += scr->type->ncols;
   1167 		}
   1168 
   1169 		/* Draw a double width character. */
   1170 		vga_gdc_write(vh, mode, 0x03);
   1171 		vga_gdc_write(vh, setres, fgcolor);
   1172 		for (i = 0; i < fheight; i++) {
   1173 			pattern = ((u_int8_t *)fs->font->data)
   1174 			    [(c * fheight + i) * 2];
   1175 			if (pattern != 0) {
   1176 				dummy = bus_space_read_1(memt, memh, rasoff2);
   1177 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1178 			}
   1179 			pattern = ((u_int8_t *)fs->font->data)
   1180 			    [(c * fheight + i) * 2 + 1];
   1181 			if (pattern != 0) {
   1182 				rasoff2++;
   1183 				dummy = bus_space_read_1(memt, memh, rasoff2);
   1184 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1185 				rasoff2--;
   1186 			}
   1187 			rasoff2 += scr->type->ncols;
   1188 		}
   1189 	}
   1190 }
   1191 
   1192 void
   1193 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
   1194 {
   1195 	struct vgascreen *scr = id;
   1196 	struct vga_handle *vh = scr->hdl;
   1197 	bus_space_tag_t memt = vh->vh_memt;
   1198 	bus_space_handle_t memh = vh->vh_memh;
   1199 	bus_size_t srcoff, dstoff;
   1200 	bus_size_t rassrcoff, rasdstoff;
   1201 	int i;
   1202 	int fheight = scr->type->fontheight;
   1203 
   1204 	srcoff = row * scr->type->ncols + srccol;
   1205 	dstoff = row * scr->type->ncols + dstcol;
   1206 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
   1207 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
   1208 
   1209 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1210 	    ncols * sizeof(struct vga_scrmem));
   1211 
   1212 	vga_gdc_write(vh, mode, 0x01);
   1213 	if (scr->active) {
   1214 		for (i = 0; i < fheight; i++) {
   1215 			bus_space_copy_region_1(memt, memh,
   1216 			    rassrcoff + i * scr->type->ncols, memh,
   1217 			    rasdstoff + i * scr->type->ncols, ncols);
   1218 		}
   1219 	}
   1220 }
   1221 
   1222 void
   1223 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
   1224 {
   1225 	struct vgascreen *scr = id;
   1226 	int i;
   1227 
   1228 	if (scr->active == 0)
   1229 		return;
   1230 
   1231 	for (i = startcol; i < startcol + ncols; i++)
   1232 		vga_raster_putchar(id, row, i, ' ', fillattr);
   1233 }
   1234 
   1235 void
   1236 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1237 {
   1238 	struct vgascreen *scr = id;
   1239 	struct vga_handle *vh = scr->hdl;
   1240 	bus_space_tag_t memt = vh->vh_memt;
   1241 	bus_space_handle_t memh = vh->vh_memh;
   1242 	int ncols;
   1243 	bus_size_t srcoff, dstoff;
   1244 	bus_size_t rassrcoff, rasdstoff;
   1245 	int fheight;
   1246 
   1247 	ncols = scr->type->ncols;
   1248 	fheight = scr->type->fontheight;
   1249 
   1250 	srcoff = srcrow * ncols;
   1251 	dstoff = dstrow * ncols;
   1252 	rassrcoff = srcoff * fheight;
   1253 	rasdstoff = dstoff * fheight;
   1254 
   1255 	if (scr->active) {
   1256 		vga_gdc_write(vh, mode, 0x01);
   1257 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
   1258 			int cursoron = scr->cursoron;
   1259 
   1260 			if (cursoron)
   1261 				/* Disable cursor. */
   1262 				vga_raster_cursor(scr, 0,
   1263 				    scr->cursorrow, scr->cursorcol);
   1264 
   1265 			/* scroll up whole screen */
   1266 			if ((scr->dispoffset + srcrow * ncols * fheight)
   1267 			    <= scr->maxdispoffset)
   1268 				scr->dispoffset += srcrow * ncols * fheight;
   1269 			else {
   1270 				bus_space_copy_region_1(memt, memh,
   1271 				    scr->dispoffset + rassrcoff,
   1272 				    memh, scr->mindispoffset,
   1273 				    nrows * ncols * fheight);
   1274 				scr->dispoffset = scr->mindispoffset;
   1275 			}
   1276 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
   1277 			vga_6845_write(vh, startadrl, scr->dispoffset);
   1278 
   1279 			if (cursoron)
   1280 				/* Enable cursor. */
   1281 				vga_raster_cursor(scr, 1, scr->cursorrow,
   1282 				    scr->cursorcol);
   1283 		} else
   1284 			bus_space_copy_region_1(memt, memh,
   1285 			    scr->dispoffset + rassrcoff, memh,
   1286 			    scr->dispoffset + rasdstoff,
   1287 			    nrows * ncols * fheight);
   1288 	}
   1289 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1290 	    nrows * ncols * sizeof(struct vga_scrmem));
   1291 }
   1292 
   1293 void
   1294 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
   1295 {
   1296 	struct vgascreen *scr = id;
   1297 	struct vga_handle *vh = scr->hdl;
   1298 	bus_space_tag_t memt = vh->vh_memt;
   1299 	bus_space_handle_t memh = vh->vh_memh;
   1300 	bus_size_t off, count;
   1301 	bus_size_t rasoff, rascount;
   1302 	int i;
   1303 
   1304 	off = startrow * scr->type->ncols;
   1305 	count = nrows * scr->type->ncols;
   1306 	rasoff = off * scr->type->fontheight;
   1307 	rascount = count * scr->type->fontheight;
   1308 
   1309 	if (scr->active) {
   1310 		/* Paint background. */
   1311 		vga_gdc_write(vh, mode, 0x02);
   1312 		if (scr->type->ncols % 4 == 0)
   1313 			/* We can speed up I/O */
   1314 			for (i = rasoff; i < rasoff + rascount; i += 4)
   1315 				bus_space_write_4(memt, memh,
   1316 				    scr->dispoffset + i, fillattr >> 4);
   1317 		else
   1318 			for (i = rasoff; i < rasoff + rascount; i += 2)
   1319 				bus_space_write_2(memt, memh,
   1320 				    scr->dispoffset + i, fillattr >> 4);
   1321 	}
   1322 	for (i = 0; i < count; i++) {
   1323 		scr->mem[off + i].ch = ' ';
   1324 		scr->mem[off + i].attr = fillattr;
   1325 		scr->mem[off + i].second = 0;
   1326 		scr->mem[off + i].enc = WSDISPLAY_FONTENC_IBM;
   1327 	}
   1328 }
   1329 
   1330 static int
   1331 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
   1332 {
   1333 	struct vgascreen *scr = id;
   1334 	struct vga_config *vc = scr->cfg;
   1335 
   1336 	if (vc->hdl.vh_mono) {
   1337 		if (flags & WSATTR_WSCOLORS)
   1338 			return (EINVAL);
   1339 		if (flags & WSATTR_REVERSE)
   1340 			*attrp = 0x70;
   1341 		else
   1342 			*attrp = 0x07;
   1343 		if (flags & WSATTR_UNDERLINE)
   1344 			*attrp |= FG_UNDERLINE;
   1345 		if (flags & WSATTR_HILIT)
   1346 			*attrp |= FG_INTENSE;
   1347 	} else {
   1348 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1349 			return (EINVAL);
   1350 		if (flags & WSATTR_WSCOLORS)
   1351 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1352 		else
   1353 			*attrp = 7;
   1354 		if (flags & WSATTR_HILIT)
   1355 			*attrp += 8;
   1356 	}
   1357 	if (flags & WSATTR_BLINK)
   1358 		*attrp |= FG_BLINK;
   1359 	return (0);
   1360 }
   1361 
   1362 void
   1363 vga_restore_screen(struct vgascreen *scr,
   1364     const struct wsscreen_descr *type, struct vga_scrmem *mem)
   1365 {
   1366 	int i, j, off, tmp;
   1367 
   1368 	tmp = scr->encoding;
   1369 	for (i = 0; i < type->nrows; i++) {
   1370 		for (j = 0; j < type->ncols; j++) {
   1371 			off = i * type->ncols + j;
   1372 			if (mem[off].second != 1) {
   1373 				scr->encoding = mem[off].enc;
   1374 				vga_raster_putchar(scr, i, j, mem[off].ch,
   1375 				    mem[off].attr);
   1376 			}
   1377 		}
   1378 	}
   1379 	scr->encoding = tmp;
   1380 }
   1381 
   1382 void
   1383 vga_raster_setscreentype(struct vga_config *vc,
   1384     const struct wsscreen_descr *type)
   1385 {
   1386 	struct vga_handle *vh = &vc->hdl;
   1387 	struct vga_moderegs moderegs;
   1388 
   1389 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
   1390 	vga_set_mode(vh, &moderegs);
   1391 }
   1392