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