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