Home | History | Annotate | Line # | Download | only in ic
vga.c revision 1.71
      1 /* $NetBSD: vga.c,v 1.71 2003/06/29 22:30:14 fvdl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: vga.c,v 1.71 2003/06/29 22:30:14 fvdl Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/callout.h>
     36 #include <sys/kernel.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/queue.h>
     40 #include <machine/bus.h>
     41 
     42 #include <dev/ic/mc6845reg.h>
     43 #include <dev/ic/pcdisplayvar.h>
     44 #include <dev/ic/vgareg.h>
     45 #include <dev/ic/vgavar.h>
     46 
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/unicode.h>
     50 #include <dev/wsfont/wsfont.h>
     51 
     52 #include <dev/ic/pcdisplay.h>
     53 
     54 /* for WSCONS_SUPPORT_PCVTFONTS and WSDISPLAY_CHARFUNCS */
     55 #include "opt_wsdisplay_compat.h"
     56 
     57 int vga_no_builtinfont = 0;
     58 
     59 static struct wsdisplay_font _vga_builtinfont = {
     60 	"builtin",			/* typeface name */
     61 	0,				/* firstchar */
     62 	256,				/* numbers */
     63 	WSDISPLAY_FONTENC_IBM,		/* encoding */
     64 	8,				/* width */
     65 	16,				/* height */
     66 	1,				/* stride */
     67 	WSDISPLAY_FONTORDER_L2R,	/* bit order */
     68 	0,				/* byte order */
     69 	NULL				/* data */
     70 };
     71 
     72 struct egavga_font {
     73 	struct wsdisplay_font *wsfont;
     74 	int cookie; /* wsfont handle, -1 invalid */
     75 	int slot; /* in adapter RAM */
     76 	int usecount;
     77 	TAILQ_ENTRY(egavga_font) next; /* LRU queue */
     78 };
     79 
     80 static struct egavga_font vga_builtinfont = {
     81 	&_vga_builtinfont,
     82 	-1, 0
     83 };
     84 
     85 #ifdef VGA_CONSOLE_SCREENTYPE
     86 static struct egavga_font vga_consolefont;
     87 #endif
     88 
     89 struct vgascreen {
     90 	struct pcdisplayscreen pcs;
     91 
     92 	LIST_ENTRY(vgascreen) next;
     93 
     94 	struct vga_config *cfg;
     95 
     96 	/* videostate */
     97 	struct egavga_font *fontset1, *fontset2;
     98 	/* font data */
     99 	/* palette */
    100 
    101 	int mindispoffset, maxdispoffset;
    102 };
    103 
    104 static int vgaconsole, vga_console_type, vga_console_attached;
    105 static struct vgascreen vga_console_screen;
    106 static struct vga_config vga_console_vc;
    107 
    108 struct egavga_font *egavga_getfont(struct vga_config *, struct vgascreen *,
    109 				   const char *, int);
    110 void egavga_unreffont(struct vga_config *, struct egavga_font *);
    111 
    112 int vga_selectfont(struct vga_config *, struct vgascreen *, const char *,
    113 				   const char *);
    114 void vga_init_screen(struct vga_config *, struct vgascreen *,
    115 		     const struct wsscreen_descr *, int, long *);
    116 void vga_init(struct vga_config *, bus_space_tag_t, bus_space_tag_t);
    117 static void vga_setfont(struct vga_config *, struct vgascreen *);
    118 
    119 static int vga_mapchar(void *, int, unsigned int *);
    120 static int vga_allocattr(void *, int, int, int, long *);
    121 static void vga_copyrows(void *, int, int, int);
    122 
    123 const struct wsdisplay_emulops vga_emulops = {
    124 	pcdisplay_cursor,
    125 	vga_mapchar,
    126 	pcdisplay_putchar,
    127 	pcdisplay_copycols,
    128 	pcdisplay_erasecols,
    129 	vga_copyrows,
    130 	pcdisplay_eraserows,
    131 	vga_allocattr
    132 };
    133 
    134 /*
    135  * translate WS(=ANSI) color codes to standard pc ones
    136  */
    137 static const unsigned char fgansitopc[] = {
    138 #ifdef __alpha__
    139 	/*
    140 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
    141 	 * XXX We should probably not bother with this
    142 	 * XXX (reinitialize the palette registers).
    143 	 */
    144 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
    145 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
    146 #else
    147 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
    148 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
    149 #endif
    150 }, bgansitopc[] = {
    151 #ifdef __alpha__
    152 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
    153 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
    154 #else
    155 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
    156 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
    157 #endif
    158 };
    159 
    160 const struct wsscreen_descr vga_25lscreen = {
    161 	"80x25", 80, 25,
    162 	&vga_emulops,
    163 	8, 16,
    164 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    165 }, vga_25lscreen_mono = {
    166 	"80x25", 80, 25,
    167 	&vga_emulops,
    168 	8, 16,
    169 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    170 }, vga_25lscreen_bf = {
    171 	"80x25bf", 80, 25,
    172 	&vga_emulops,
    173 	8, 16,
    174 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    175 }, vga_40lscreen = {
    176 	"80x40", 80, 40,
    177 	&vga_emulops,
    178 	8, 10,
    179 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    180 }, vga_40lscreen_mono = {
    181 	"80x40", 80, 40,
    182 	&vga_emulops,
    183 	8, 10,
    184 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    185 }, vga_40lscreen_bf = {
    186 	"80x40bf", 80, 40,
    187 	&vga_emulops,
    188 	8, 10,
    189 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    190 }, vga_50lscreen = {
    191 	"80x50", 80, 50,
    192 	&vga_emulops,
    193 	8, 8,
    194 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    195 }, vga_50lscreen_mono = {
    196 	"80x50", 80, 50,
    197 	&vga_emulops,
    198 	8, 8,
    199 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    200 }, vga_50lscreen_bf = {
    201 	"80x50bf", 80, 50,
    202 	&vga_emulops,
    203 	8, 8,
    204 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    205 }, vga_24lscreen = {
    206 	"80x24", 80, 24,
    207 	&vga_emulops,
    208 	8, 16,
    209 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    210 }, vga_24lscreen_mono = {
    211 	"80x24", 80, 24,
    212 	&vga_emulops,
    213 	8, 16,
    214 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    215 }, vga_24lscreen_bf = {
    216 	"80x24bf", 80, 24,
    217 	&vga_emulops,
    218 	8, 16,
    219 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    220 };
    221 
    222 #define VGA_SCREEN_CANTWOFONTS(type) (!((type)->capabilities & WSSCREEN_HILIT))
    223 
    224 const struct wsscreen_descr *_vga_scrlist[] = {
    225 	&vga_25lscreen,
    226 	&vga_25lscreen_bf,
    227 	&vga_40lscreen,
    228 	&vga_40lscreen_bf,
    229 	&vga_50lscreen,
    230 	&vga_50lscreen_bf,
    231 	&vga_24lscreen,
    232 	&vga_24lscreen_bf,
    233 	/* XXX other formats, graphics screen? */
    234 }, *_vga_scrlist_mono[] = {
    235 	&vga_25lscreen_mono,
    236 	&vga_40lscreen_mono,
    237 	&vga_50lscreen_mono,
    238 	&vga_24lscreen_mono,
    239 	/* XXX other formats, graphics screen? */
    240 };
    241 
    242 const struct wsscreen_list vga_screenlist = {
    243 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
    244 	_vga_scrlist
    245 }, vga_screenlist_mono = {
    246 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
    247 	_vga_scrlist_mono
    248 };
    249 
    250 static int	vga_ioctl(void *, u_long, caddr_t, int, struct proc *);
    251 static paddr_t	vga_mmap(void *, off_t, int);
    252 static int	vga_alloc_screen(void *, const struct wsscreen_descr *,
    253 				 void **, int *, int *, long *);
    254 static void	vga_free_screen(void *, void *);
    255 static int	vga_show_screen(void *, void *, int,
    256 				void (*)(void *, int, int), void *);
    257 static int	vga_load_font(void *, void *, struct wsdisplay_font *);
    258 #ifdef WSDISPLAY_CHARFUNCS
    259 static int	vga_getwschar(void *, struct wsdisplay_char *);
    260 static int	vga_putwschar(void *, struct wsdisplay_char *);
    261 #endif /* WSDISPLAY_CHARFUNCS */
    262 
    263 void vga_doswitch(struct vga_config *);
    264 
    265 const struct wsdisplay_accessops vga_accessops = {
    266 	vga_ioctl,
    267 	vga_mmap,
    268 	vga_alloc_screen,
    269 	vga_free_screen,
    270 	vga_show_screen,
    271 	vga_load_font,
    272 	NULL,
    273 #ifdef WSDISPLAY_CHARFUNCS
    274 	vga_getwschar,
    275 	vga_putwschar
    276 #else /* WSDISPLAY_CHARFUNCS */
    277 	NULL,
    278 	NULL
    279 #endif /* WSDISPLAY_CHARFUNCS */
    280 };
    281 
    282 /*
    283  * We want at least ASCII 32..127 be present in the
    284  * first font slot.
    285  */
    286 #define vga_valid_primary_font(f) \
    287 	(f->wsfont->encoding == WSDISPLAY_FONTENC_IBM || \
    288 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO || \
    289 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO7)
    290 
    291 struct egavga_font *
    292 egavga_getfont(struct vga_config *vc, struct vgascreen *scr, const char *name,
    293 	       int primary)
    294 {
    295 	struct egavga_font *f;
    296 	int cookie;
    297 	struct wsdisplay_font *wf;
    298 
    299 	TAILQ_FOREACH(f, &vc->vc_fontlist, next) {
    300 		if (wsfont_matches(f->wsfont, name,
    301 		    8, scr->pcs.type->fontheight, 0) &&
    302 		    (!primary || vga_valid_primary_font(f))) {
    303 #ifdef VGAFONTDEBUG
    304 			if (scr != &vga_console_screen || vga_console_attached)
    305 				printf("vga_getfont: %s already present\n",
    306 				    name ? name : "<default>");
    307 #endif
    308 			goto found;
    309 		}
    310 	}
    311 
    312 	cookie = wsfont_find(name, 8, scr->pcs.type->fontheight, 0,
    313 	    WSDISPLAY_FONTORDER_L2R, 0);
    314 	/* XXX obey "primary" */
    315 	if (cookie == -1) {
    316 #ifdef VGAFONTDEBUG
    317 		if (scr != &vga_console_screen || vga_console_attached)
    318 			printf("vga_getfont: %s not found\n",
    319 			    name ? name : "<default>");
    320 #endif
    321 		return (0);
    322 	}
    323 
    324 	if (wsfont_lock(cookie, &wf))
    325 		return (0);
    326 
    327 #ifdef VGA_CONSOLE_SCREENTYPE
    328 	if (scr == &vga_console_screen)
    329 		f = &vga_consolefont;
    330 	else
    331 #endif
    332 	f = malloc(sizeof(struct egavga_font), M_DEVBUF, M_NOWAIT);
    333 	if (!f) {
    334 		wsfont_unlock(cookie);
    335 		return (0);
    336 	}
    337 	f->wsfont = wf;
    338 	f->cookie = cookie;
    339 	f->slot = -1; /* not yet loaded */
    340 	f->usecount = 0; /* incremented below */
    341 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
    342 
    343 found:
    344 	f->usecount++;
    345 #ifdef VGAFONTDEBUG
    346 	if (scr != &vga_console_screen || vga_console_attached)
    347 		printf("vga_getfont: usecount=%d\n", f->usecount);
    348 #endif
    349 	return (f);
    350 }
    351 
    352 void
    353 egavga_unreffont(struct vga_config *vc, struct egavga_font *f)
    354 {
    355 
    356 	f->usecount--;
    357 #ifdef VGAFONTDEBUG
    358 	printf("vga_unreffont: usecount=%d\n", f->usecount);
    359 #endif
    360 	if (f->usecount == 0 && f->cookie != -1) {
    361 		TAILQ_REMOVE(&vc->vc_fontlist, f, next);
    362 		if (f->slot != -1) {
    363 			KASSERT(vc->vc_fonts[f->slot] == f);
    364 			vc->vc_fonts[f->slot] = 0;
    365 		}
    366 		wsfont_unlock(f->cookie);
    367 #ifdef VGA_CONSOLE_SCREENTYPE
    368 		if (f != &vga_consolefont)
    369 #endif
    370 		free(f, M_DEVBUF);
    371 	}
    372 }
    373 
    374 int
    375 vga_selectfont(struct vga_config *vc, struct vgascreen *scr, const char *name1,
    376 	       const char *name2)
    377 {
    378 	const struct wsscreen_descr *type = scr->pcs.type;
    379 	struct egavga_font *f1, *f2;
    380 
    381 	f1 = egavga_getfont(vc, scr, name1, 1);
    382 	if (!f1)
    383 		return (ENXIO);
    384 
    385 	if (VGA_SCREEN_CANTWOFONTS(type) && name2) {
    386 		f2 = egavga_getfont(vc, scr, name2, 0);
    387 		if (!f2) {
    388 			egavga_unreffont(vc, f1);
    389 			return (ENXIO);
    390 		}
    391 	} else
    392 		f2 = 0;
    393 
    394 #ifdef VGAFONTDEBUG
    395 	if (scr != &vga_console_screen || vga_console_attached) {
    396 		printf("vga (%s): font1=%s (slot %d)", type->name,
    397 		    f1->wsfont->name, f1->slot);
    398 		if (f2)
    399 			printf(", font2=%s (slot %d)",
    400 			    f2->wsfont->name, f2->slot);
    401 		printf("\n");
    402 	}
    403 #endif
    404 	if (scr->fontset1)
    405 		egavga_unreffont(vc, scr->fontset1);
    406 	scr->fontset1 = f1;
    407 	if (scr->fontset2)
    408 		egavga_unreffont(vc, scr->fontset2);
    409 	scr->fontset2 = f2;
    410 	return (0);
    411 }
    412 
    413 void
    414 vga_init_screen(struct vga_config *vc, struct vgascreen *scr,
    415 		const struct wsscreen_descr *type, int existing, long *attrp)
    416 {
    417 	int cpos;
    418 	int res;
    419 
    420 	scr->cfg = vc;
    421 	scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
    422 	scr->pcs.type = type;
    423 	scr->pcs.active = existing;
    424 	scr->mindispoffset = 0;
    425 	if (vc->vc_quirks & VGA_QUIRK_NOFASTSCROLL)
    426 		scr->maxdispoffset = 0;
    427 	else
    428 		scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
    429 
    430 	if (existing) {
    431 		vc->active = scr;
    432 
    433 		cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
    434 		cpos |= vga_6845_read(&vc->hdl, cursorl);
    435 
    436 		/* make sure we have a valid cursor position */
    437 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    438 			cpos = 0;
    439 
    440 		scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
    441 		scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
    442 
    443 		/* make sure we have a valid memory offset */
    444 		if (scr->pcs.dispoffset < scr->mindispoffset ||
    445 		    scr->pcs.dispoffset > scr->maxdispoffset)
    446 			scr->pcs.dispoffset = scr->mindispoffset;
    447 
    448 		if (type != vc->currenttype) {
    449 			vga_setscreentype(&vc->hdl, type);
    450 			vc->currenttype = type;
    451 		}
    452 	} else {
    453 		cpos = 0;
    454 		scr->pcs.dispoffset = scr->mindispoffset;
    455 	}
    456 
    457 	scr->pcs.cursorrow = cpos / type->ncols;
    458 	scr->pcs.cursorcol = cpos % type->ncols;
    459 	pcdisplay_cursor_init(&scr->pcs, existing);
    460 
    461 #ifdef __alpha__
    462 	if (!vc->hdl.vh_mono)
    463 		/*
    464 		 * DEC firmware uses a blue background.
    465 		 */
    466 		res = vga_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
    467 		    WSATTR_WSCOLORS, attrp);
    468 	else
    469 #endif
    470 	res = vga_allocattr(scr, 0, 0, 0, attrp);
    471 #ifdef DIAGNOSTIC
    472 	if (res)
    473 		panic("vga_init_screen: attribute botch");
    474 #endif
    475 
    476 	scr->pcs.mem = NULL;
    477 
    478 	scr->fontset1 = scr->fontset2 = 0;
    479 	if (vga_selectfont(vc, scr, 0, 0)) {
    480 		if (scr == &vga_console_screen)
    481 			panic("vga_init_screen: no font");
    482 		else
    483 			printf("vga_init_screen: no font\n");
    484 	}
    485 	if (existing)
    486 		vga_setfont(vc, scr);
    487 
    488 	vc->nscreens++;
    489 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    490 }
    491 
    492 void
    493 vga_init(struct vga_config *vc, bus_space_tag_t iot, bus_space_tag_t memt)
    494 {
    495 	struct vga_handle *vh = &vc->hdl;
    496 	u_int8_t mor;
    497 	int i;
    498 
    499 	vh->vh_iot = iot;
    500 	vh->vh_memt = memt;
    501 
    502 	if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    503 		panic("vga_init: couldn't map vga io");
    504 
    505 	/* read "misc output register" */
    506 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAR);
    507 	vh->vh_mono = !(mor & 1);
    508 
    509 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    510 	    &vh->vh_ioh_6845))
    511 		panic("vga_init: couldn't map 6845 io");
    512 
    513 	if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    514 		panic("vga_init: couldn't map memory");
    515 
    516 	if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
    517 	    (vh->vh_mono ? 0x10000 : 0x18000), 0x8000, &vh->vh_memh))
    518 		panic("vga_init: mem subrange failed");
    519 
    520 	/* should only reserve the space (no need to map - save KVM) */
    521 	vc->vc_biostag = memt;
    522 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0, &vc->vc_bioshdl))
    523 		vc->vc_biosmapped = 0;
    524 	else
    525 		vc->vc_biosmapped = 1;
    526 
    527 	vc->nscreens = 0;
    528 	LIST_INIT(&vc->screens);
    529 	vc->active = NULL;
    530 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
    531 	callout_init(&vc->vc_switch_callout);
    532 
    533 	wsfont_init();
    534 	if (vga_no_builtinfont) {
    535 		struct wsdisplay_font *wf;
    536 		int cookie;
    537 
    538 		cookie = wsfont_find(NULL, 8, 16, 0,
    539 		     WSDISPLAY_FONTORDER_L2R, 0);
    540 		if (cookie == -1 || wsfont_lock(cookie, &wf))
    541 			panic("vga_init: can't load console font");
    542 		vga_loadchars(&vc->hdl, 0, wf->firstchar, wf->numchars,
    543 		    wf->fontheight, wf->data);
    544 		vga_builtinfont.wsfont = wf;
    545 		vga_builtinfont.cookie = cookie;
    546 		vga_builtinfont.slot = 0;
    547 	}
    548 	vc->vc_fonts[0] = &vga_builtinfont;
    549 	for (i = 1; i < 8; i++)
    550 		vc->vc_fonts[i] = 0;
    551 	TAILQ_INIT(&vc->vc_fontlist);
    552 	TAILQ_INSERT_HEAD(&vc->vc_fontlist, &vga_builtinfont, next);
    553 
    554 	vc->currentfontset1 = vc->currentfontset2 = 0;
    555 }
    556 
    557 void
    558 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
    559 		  bus_space_tag_t memt, int type, int quirks,
    560 		  const struct vga_funcs *vf)
    561 {
    562 	int console;
    563 	struct vga_config *vc;
    564 	struct wsemuldisplaydev_attach_args aa;
    565 
    566 	console = vga_is_console(iot, type);
    567 
    568 	if (console) {
    569 		vc = &vga_console_vc;
    570 		vga_console_attached = 1;
    571 	} else {
    572 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
    573 		vga_init(vc, iot, memt);
    574 	}
    575 
    576 	if (quirks & VGA_QUIRK_ONEFONT) {
    577 		vc->vc_nfontslots = 1;
    578 #ifndef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    579 		/*
    580 		 * XXX maybe invalidate font in slot > 0, but this can
    581 		 * only be happen with VGA_CONSOLE_SCREENTYPE, and then
    582 		 * we require VGA_CONSOLE_ATI_BROKEN_FONTSEL anyway.
    583 		 */
    584 #endif
    585 	} else {
    586 		vc->vc_nfontslots = 8;
    587 #ifndef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    588 		/*
    589 		 * XXX maybe validate builtin font shifted to slot 1 if
    590 		 * slot 0 got overwritten because of VGA_CONSOLE_SCREENTYPE,
    591 		 * but it will be reloaded anyway if needed.
    592 		 */
    593 #endif
    594 	}
    595 
    596 	/*
    597 	 * Save the builtin font to memory. In case it got overwritten
    598 	 * in console initialization, use the copy in slot 1.
    599 	 */
    600 #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    601 #define BUILTINFONTLOC (vga_builtinfont.slot == -1 ? 1 : 0)
    602 #else
    603 	KASSERT(vga_builtinfont.slot == 0);
    604 #define BUILTINFONTLOC (0)
    605 #endif
    606 	if (!vga_no_builtinfont) {
    607 		char *data =
    608 		    malloc(256 * vga_builtinfont.wsfont->fontheight,
    609 		    M_DEVBUF, M_WAITOK);
    610 		vga_readoutchars(&vc->hdl, BUILTINFONTLOC, 0, 256,
    611 		    vga_builtinfont.wsfont->fontheight, data);
    612 		vga_builtinfont.wsfont->data = data;
    613 	}
    614 
    615 	vc->vc_type = type;
    616 	vc->vc_funcs = vf;
    617 	vc->vc_quirks = quirks;
    618 
    619 	sc->sc_vc = vc;
    620 	vc->softc = sc;
    621 
    622 	aa.console = console;
    623 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    624 	aa.accessops = &vga_accessops;
    625 	aa.accesscookie = vc;
    626 
    627 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    628 }
    629 
    630 int
    631 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
    632 {
    633 	long defattr;
    634 	const struct wsscreen_descr *scr;
    635 
    636 	if (check && !vga_common_probe(iot, memt))
    637 		return (ENXIO);
    638 
    639 	/* set up bus-independent VGA configuration */
    640 	vga_init(&vga_console_vc, iot, memt);
    641 #ifdef VGA_CONSOLE_SCREENTYPE
    642 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    643 	    &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
    644 	if (!scr)
    645 		panic("vga_cnattach: invalid screen type");
    646 #else
    647 	scr = vga_console_vc.currenttype;
    648 #endif
    649 #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    650 	/*
    651 	 * On some (most/all?) ATI cards, only font slot 0 is usable.
    652 	 * vga_init_screen() might need font slot 0 for a non-default
    653 	 * console font, so save the builtin VGA font to another font slot.
    654 	 * The attach() code will take care later.
    655 	 */
    656 	vga_console_vc.vc_quirks |= VGA_QUIRK_ONEFONT; /* redundant */
    657 	vga_copyfont01(&vga_console_vc.hdl);
    658 	vga_console_vc.vc_nfontslots = 1;
    659 #else
    660 	vga_console_vc.vc_nfontslots = 8;
    661 #endif
    662 	/* until we know better, assume "fast scrolling" does not work */
    663 	vga_console_vc.vc_quirks |= VGA_QUIRK_NOFASTSCROLL;
    664 
    665 	vga_init_screen(&vga_console_vc, &vga_console_screen, scr, 1, &defattr);
    666 
    667 	wsdisplay_cnattach(scr, &vga_console_screen,
    668 	    vga_console_screen.pcs.cursorcol,
    669 	    vga_console_screen.pcs.cursorrow, defattr);
    670 
    671 	vgaconsole = 1;
    672 	vga_console_type = type;
    673 	return (0);
    674 }
    675 
    676 int
    677 vga_is_console(bus_space_tag_t iot, int type)
    678 {
    679 	if (vgaconsole &&
    680 	    !vga_console_attached &&
    681 	    iot == vga_console_vc.hdl.vh_iot &&
    682 	    (vga_console_type == -1 || (type == vga_console_type)))
    683 		return (1);
    684 	return (0);
    685 }
    686 
    687 static int
    688 vga_get_video(struct vga_config *vc)
    689 {
    690 
    691 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
    692 }
    693 
    694 static void
    695 vga_set_video(struct vga_config *vc, int state)
    696 {
    697 	int val;
    698 
    699 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    700 	if (state) {					/* unblank screen */
    701 		val = vga_ts_read(&vc->hdl, mode);
    702 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
    703 #ifndef VGA_NO_VBLANK
    704 		val = vga_6845_read(&vc->hdl, mode);
    705 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    706 #endif
    707 	} else {					/* blank screen */
    708 		val = vga_ts_read(&vc->hdl, mode);
    709 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
    710 #ifndef VGA_NO_VBLANK
    711 		val = vga_6845_read(&vc->hdl, mode);
    712 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    713 #endif
    714 	}
    715 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    716 }
    717 
    718 int
    719 vga_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    720 {
    721 	struct vga_config *vc = v;
    722 	const struct vga_funcs *vf = vc->vc_funcs;
    723 
    724 	switch (cmd) {
    725 	case WSDISPLAYIO_GTYPE:
    726 		*(int *)data = vc->vc_type;
    727 		return 0;
    728 
    729 	case WSDISPLAYIO_GINFO:
    730 		/* XXX should get detailed hardware information here */
    731 		return EPASSTHROUGH;
    732 
    733 	case WSDISPLAYIO_GVIDEO:
    734 		*(int *)data = (vga_get_video(vc) ?
    735 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
    736 		return 0;
    737 
    738 	case WSDISPLAYIO_SVIDEO:
    739 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    740 		return 0;
    741 
    742 	case WSDISPLAYIO_GETCMAP:
    743 	case WSDISPLAYIO_PUTCMAP:
    744 	case WSDISPLAYIO_GCURPOS:
    745 	case WSDISPLAYIO_SCURPOS:
    746 	case WSDISPLAYIO_GCURMAX:
    747 	case WSDISPLAYIO_GCURSOR:
    748 	case WSDISPLAYIO_SCURSOR:
    749 		/* NONE of these operations are by the generic VGA driver. */
    750 		return EPASSTHROUGH;
    751 	}
    752 
    753 	if (vc->vc_funcs == NULL)
    754 		return (EPASSTHROUGH);
    755 
    756 	if (vf->vf_ioctl == NULL)
    757 		return (EPASSTHROUGH);
    758 
    759 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
    760 }
    761 
    762 static paddr_t
    763 vga_mmap(void *v, off_t offset, int prot)
    764 {
    765 	struct vga_config *vc = v;
    766 	const struct vga_funcs *vf = vc->vc_funcs;
    767 
    768 	if (vc->vc_funcs == NULL)
    769 		return (-1);
    770 
    771 	if (vf->vf_mmap == NULL)
    772 		return (-1);
    773 
    774 	return ((*vf->vf_mmap)(v, offset, prot));
    775 }
    776 
    777 int
    778 vga_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    779 		 int *curxp, int *curyp, long *defattrp)
    780 {
    781 	struct vga_config *vc = v;
    782 	struct vgascreen *scr;
    783 
    784 	if (vc->nscreens == 1) {
    785 		struct vgascreen *scr1 = vc->screens.lh_first;
    786 		/*
    787 		 * When allocating the second screen, get backing store
    788 		 * for the first one too.
    789 		 * XXX We could be more clever and use video RAM.
    790 		 */
    791 		scr1->pcs.mem =
    792 		    malloc(scr1->pcs.type->ncols * scr1->pcs.type->nrows * 2,
    793 		    M_DEVBUF, M_WAITOK);
    794 	}
    795 
    796 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    797 	vga_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    798 
    799 	if (vc->nscreens > 1) {
    800 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
    801 		    M_DEVBUF, M_WAITOK);
    802 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
    803 	}
    804 
    805 	*cookiep = scr;
    806 	*curxp = scr->pcs.cursorcol;
    807 	*curyp = scr->pcs.cursorrow;
    808 
    809 	return (0);
    810 }
    811 
    812 void
    813 vga_free_screen(void *v, void *cookie)
    814 {
    815 	struct vgascreen *vs = cookie;
    816 	struct vga_config *vc = vs->cfg;
    817 
    818 	LIST_REMOVE(vs, next);
    819 	if (vs->fontset1)
    820 		egavga_unreffont(vc, vs->fontset1);
    821 	if (vs->fontset2)
    822 		egavga_unreffont(vc, vs->fontset2);
    823 
    824 	if (vs != &vga_console_screen)
    825 		free(vs, M_DEVBUF);
    826 	else
    827 		panic("vga_free_screen: console");
    828 
    829 	if (vc->active == vs)
    830 		vc->active = 0;
    831 }
    832 
    833 static void vga_usefont(struct vga_config *, struct egavga_font *);
    834 
    835 static void
    836 vga_usefont(struct vga_config *vc, struct egavga_font *f)
    837 {
    838 	int slot;
    839 	struct egavga_font *of;
    840 
    841 	if (f->slot != -1)
    842 		goto toend;
    843 
    844 	for (slot = 0; slot < vc->vc_nfontslots; slot++) {
    845 		if (!vc->vc_fonts[slot])
    846 			goto loadit;
    847 	}
    848 
    849 	/* have to kick out another one */
    850 	TAILQ_FOREACH(of, &vc->vc_fontlist, next) {
    851 		if (of->slot != -1) {
    852 			KASSERT(vc->vc_fonts[of->slot] == of);
    853 			slot = of->slot;
    854 			of->slot = -1;
    855 			goto loadit;
    856 		}
    857 	}
    858 	panic("vga_usefont");
    859 
    860 loadit:
    861 	vga_loadchars(&vc->hdl, slot, f->wsfont->firstchar,
    862 	    f->wsfont->numchars, f->wsfont->fontheight, f->wsfont->data);
    863 	f->slot = slot;
    864 	vc->vc_fonts[slot] = f;
    865 
    866 toend:
    867 	TAILQ_REMOVE(&vc->vc_fontlist, f, next);
    868 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
    869 }
    870 
    871 static void
    872 vga_setfont(struct vga_config *vc, struct vgascreen *scr)
    873 {
    874 	int fontslot1, fontslot2;
    875 
    876 	if (scr->fontset1)
    877 		vga_usefont(vc, scr->fontset1);
    878 	if (scr->fontset2)
    879 		vga_usefont(vc, scr->fontset2);
    880 
    881 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
    882 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
    883 	if (vc->currentfontset1 != fontslot1 ||
    884 	    vc->currentfontset2 != fontslot2) {
    885 		vga_setfontset(&vc->hdl, fontslot1, fontslot2);
    886 		vc->currentfontset1 = fontslot1;
    887 		vc->currentfontset2 = fontslot2;
    888 	}
    889 }
    890 
    891 int
    892 vga_show_screen(void *v, void *cookie, int waitok,
    893 		void (*cb)(void *, int, int), void *cbarg)
    894 {
    895 	struct vgascreen *scr = cookie, *oldscr;
    896 	struct vga_config *vc = scr->cfg;
    897 
    898 	oldscr = vc->active; /* can be NULL! */
    899 	if (scr == oldscr) {
    900 		return (0);
    901 	}
    902 
    903 	vc->wantedscreen = cookie;
    904 	vc->switchcb = cb;
    905 	vc->switchcbarg = cbarg;
    906 	if (cb) {
    907 		callout_reset(&vc->vc_switch_callout, 0,
    908 		    (void(*)(void *))vga_doswitch, vc);
    909 		return (EAGAIN);
    910 	}
    911 
    912 	vga_doswitch(vc);
    913 	return (0);
    914 }
    915 
    916 void
    917 vga_doswitch(struct vga_config *vc)
    918 {
    919 	struct vgascreen *scr, *oldscr;
    920 	struct vga_handle *vh = &vc->hdl;
    921 	const struct wsscreen_descr *type;
    922 
    923 	scr = vc->wantedscreen;
    924 	if (!scr) {
    925 		printf("vga_doswitch: disappeared\n");
    926 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    927 		return;
    928 	}
    929 	type = scr->pcs.type;
    930 	oldscr = vc->active; /* can be NULL! */
    931 #ifdef DIAGNOSTIC
    932 	if (oldscr) {
    933 		if (!oldscr->pcs.active)
    934 			panic("vga_show_screen: not active");
    935 		if (oldscr->pcs.type != vc->currenttype)
    936 			panic("vga_show_screen: bad type");
    937 	}
    938 #endif
    939 	if (scr == oldscr) {
    940 		return;
    941 	}
    942 #ifdef DIAGNOSTIC
    943 	if (scr->pcs.active)
    944 		panic("vga_show_screen: active");
    945 #endif
    946 
    947 	if (oldscr) {
    948 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
    949 
    950 		oldscr->pcs.active = 0;
    951 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
    952 		    oldscr->pcs.dispoffset, oldscr->pcs.mem,
    953 		    oldtype->ncols * oldtype->nrows);
    954 	}
    955 
    956 	if (vc->currenttype != type) {
    957 		vga_setscreentype(vh, type);
    958 		vc->currenttype = type;
    959 	}
    960 
    961 	vga_setfont(vc, scr);
    962 	/* XXX swich colours! */
    963 
    964 	scr->pcs.dispoffset = scr->mindispoffset;
    965 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
    966 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
    967 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
    968 	}
    969 
    970 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
    971 	    scr->pcs.dispoffset, scr->pcs.mem, type->ncols * type->nrows);
    972 	scr->pcs.active = 1;
    973 
    974 	vc->active = scr;
    975 
    976 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
    977 	    scr->pcs.cursorrow, scr->pcs.cursorcol);
    978 
    979 	vc->wantedscreen = 0;
    980 	if (vc->switchcb)
    981 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    982 }
    983 
    984 static int
    985 vga_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    986 {
    987 	struct vga_config *vc = v;
    988 	struct vgascreen *scr = cookie;
    989 	char *name2;
    990 	int res;
    991 
    992 	if (scr) {
    993 		name2 = NULL;
    994 		if (data->name) {
    995 			name2 = strchr(data->name, ',');
    996 			if (name2)
    997 				*name2++ = '\0';
    998 		}
    999 		res = vga_selectfont(vc, scr, data->name, name2);
   1000 		if (!res && scr->pcs.active)
   1001 			vga_setfont(vc, scr);
   1002 		return (res);
   1003 	}
   1004 
   1005 	return (0);
   1006 }
   1007 
   1008 static int
   1009 vga_allocattr(void *id, int fg, int bg, int flags, long *attrp)
   1010 {
   1011 	struct vgascreen *scr = id;
   1012 	struct vga_config *vc = scr->cfg;
   1013 
   1014 	if (vc->hdl.vh_mono) {
   1015 		if (flags & WSATTR_WSCOLORS)
   1016 			return (EINVAL);
   1017 		if (flags & WSATTR_REVERSE)
   1018 			*attrp = 0x70;
   1019 		else
   1020 			*attrp = 0x07;
   1021 		if (flags & WSATTR_UNDERLINE)
   1022 			*attrp |= FG_UNDERLINE;
   1023 		if (flags & WSATTR_HILIT)
   1024 			*attrp |= FG_INTENSE;
   1025 	} else {
   1026 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1027 			return (EINVAL);
   1028 		if (flags & WSATTR_WSCOLORS)
   1029 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1030 		else
   1031 			*attrp = 7;
   1032 		if (flags & WSATTR_HILIT)
   1033 			*attrp += 8;
   1034 	}
   1035 	if (flags & WSATTR_BLINK)
   1036 		*attrp |= FG_BLINK;
   1037 	return (0);
   1038 }
   1039 
   1040 static void
   1041 vga_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1042 {
   1043 	struct vgascreen *scr = id;
   1044 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
   1045 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
   1046 	int ncols = scr->pcs.type->ncols;
   1047 	bus_size_t srcoff, dstoff;
   1048 
   1049 	srcoff = srcrow * ncols + 0;
   1050 	dstoff = dstrow * ncols + 0;
   1051 
   1052 	if (scr->pcs.active) {
   1053 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
   1054 #ifdef PCDISPLAY_SOFTCURSOR
   1055 			int cursoron = scr->pcs.cursoron;
   1056 
   1057 			if (cursoron)
   1058 				pcdisplay_cursor(&scr->pcs, 0,
   1059 				    scr->pcs.cursorrow, scr->pcs.cursorcol);
   1060 #endif
   1061 			/* scroll up whole screen */
   1062 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
   1063 			    <= scr->maxdispoffset) {
   1064 				scr->pcs.dispoffset += srcrow * ncols * 2;
   1065 			} else {
   1066 				bus_space_copy_region_2(memt, memh,
   1067 				    scr->pcs.dispoffset + srcoff * 2,
   1068 				    memh, scr->mindispoffset, nrows * ncols);
   1069 				scr->pcs.dispoffset = scr->mindispoffset;
   1070 			}
   1071 			vga_6845_write(&scr->cfg->hdl, startadrh,
   1072 			    scr->pcs.dispoffset >> 9);
   1073 			vga_6845_write(&scr->cfg->hdl, startadrl,
   1074 			    scr->pcs.dispoffset >> 1);
   1075 #ifdef PCDISPLAY_SOFTCURSOR
   1076 			if (cursoron)
   1077 				pcdisplay_cursor(&scr->pcs, 1,
   1078 				    scr->pcs.cursorrow, scr->pcs.cursorcol);
   1079 #endif
   1080 		} else {
   1081 			bus_space_copy_region_2(memt, memh,
   1082 			    scr->pcs.dispoffset + srcoff * 2,
   1083 			    memh, scr->pcs.dispoffset + dstoff * 2,
   1084 			    nrows * ncols);
   1085 		}
   1086 	} else
   1087 		memcpy(&scr->pcs.mem[dstoff], &scr->pcs.mem[srcoff],
   1088 		    nrows * ncols * 2);
   1089 }
   1090 
   1091 #ifdef WSCONS_SUPPORT_PCVTFONTS
   1092 
   1093 #define NOTYET 0xffff
   1094 static const u_int16_t pcvt_unichars[0xa0] = {
   1095 /* 0 */	_e006U, /* N/L control */
   1096 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1097 	NOTYET,
   1098 	0x2409, /* SYMBOL FOR HORIZONTAL TABULATION */
   1099 	0x240a, /* SYMBOL FOR LINE FEED */
   1100 	0x240b, /* SYMBOL FOR VERTICAL TABULATION */
   1101 	0x240c, /* SYMBOL FOR FORM FEED */
   1102 	0x240d, /* SYMBOL FOR CARRIAGE RETURN */
   1103 	NOTYET, NOTYET,
   1104 /* 1 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1105 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1106 /* 2 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1107 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1108 /* 3 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1109 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1110 /* 4 */	0x03c1, /* GREEK SMALL LETTER RHO */
   1111 	0x03c8, /* GREEK SMALL LETTER PSI */
   1112 	0x2202, /* PARTIAL DIFFERENTIAL */
   1113 	0x03bb, /* GREEK SMALL LETTER LAMDA */
   1114 	0x03b9, /* GREEK SMALL LETTER IOTA */
   1115 	0x03b7, /* GREEK SMALL LETTER ETA */
   1116 	0x03b5, /* GREEK SMALL LETTER EPSILON */
   1117 	0x03c7, /* GREEK SMALL LETTER CHI */
   1118 	0x2228, /* LOGICAL OR */
   1119 	0x2227, /* LOGICAL AND */
   1120 	0x222a, /* UNION */
   1121 	0x2283, /* SUPERSET OF */
   1122 	0x2282, /* SUBSET OF */
   1123 	0x03a5, /* GREEK CAPITAL LETTER UPSILON */
   1124 	0x039e, /* GREEK CAPITAL LETTER XI */
   1125 	0x03a8, /* GREEK CAPITAL LETTER PSI */
   1126 /* 5 */	0x03a0, /* GREEK CAPITAL LETTER PI */
   1127 	0x21d2, /* RIGHTWARDS DOUBLE ARROW */
   1128 	0x21d4, /* LEFT RIGHT DOUBLE ARROW */
   1129 	0x039b, /* GREEK CAPITAL LETTER LAMDA */
   1130 	0x0398, /* GREEK CAPITAL LETTER THETA */
   1131 	0x2243, /* ASYMPTOTICALLY EQUAL TO */
   1132 	0x2207, /* NABLA */
   1133 	0x2206, /* INCREMENT */
   1134 	0x221d, /* PROPORTIONAL TO */
   1135 	0x2234, /* THEREFORE */
   1136 	0x222b, /* INTEGRAL */
   1137 	0x2215, /* DIVISION SLASH */
   1138 	0x2216, /* SET MINUS */
   1139 	_e00eU, /* angle? */
   1140 	_e00dU, /* inverted angle? */
   1141 	_e00bU, /* braceleftmid */
   1142 /* 6 */	_e00cU, /* bracerightmid */
   1143 	_e007U, /* bracelefttp */
   1144 	_e008U, /* braceleftbt */
   1145 	_e009U, /* bracerighttp */
   1146 	_e00aU, /* bracerightbt */
   1147 	0x221a, /* SQUARE ROOT */
   1148 	0x03c9, /* GREEK SMALL LETTER OMEGA */
   1149 	0x00a5, /* YEN SIGN */
   1150 	0x03be, /* GREEK SMALL LETTER XI */
   1151 	0x00fd, /* LATIN SMALL LETTER Y WITH ACUTE */
   1152 	0x00fe, /* LATIN SMALL LETTER THORN */
   1153 	0x00f0, /* LATIN SMALL LETTER ETH */
   1154 	0x00de, /* LATIN CAPITAL LETTER THORN */
   1155 	0x00dd, /* LATIN CAPITAL LETTER Y WITH ACUTE */
   1156 	0x00d7, /* MULTIPLICATION SIGN */
   1157 	0x00d0, /* LATIN CAPITAL LETTER ETH */
   1158 /* 7 */	0x00be, /* VULGAR FRACTION THREE QUARTERS */
   1159 	0x00b8, /* CEDILLA */
   1160 	0x00b4, /* ACUTE ACCENT */
   1161 	0x00af, /* MACRON */
   1162 	0x00ae, /* REGISTERED SIGN */
   1163 	0x00ad, /* SOFT HYPHEN */
   1164 	0x00ac, /* NOT SIGN */
   1165 	0x00a8, /* DIAERESIS */
   1166 	0x2260, /* NOT EQUAL TO */
   1167 	_e005U, /* scan 9 */
   1168 	_e004U, /* scan 7 */
   1169 	_e003U, /* scan 5 */
   1170 	_e002U, /* scan 3 */
   1171 	_e001U, /* scan 1 */
   1172 	0x03c5, /* GREEK SMALL LETTER UPSILON */
   1173 	0x00f8, /* LATIN SMALL LETTER O WITH STROKE */
   1174 /* 8 */	0x0153, /* LATIN SMALL LIGATURE OE */
   1175 	0x00f5, /* LATIN SMALL LETTER O WITH TILDE !!!doc bug */
   1176 	0x00e3, /* LATIN SMALL LETTER A WITH TILDE */
   1177 	0x0178, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
   1178 	0x00db, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
   1179 	0x00da, /* LATIN CAPITAL LETTER U WITH ACUTE */
   1180 	0x00d9, /* LATIN CAPITAL LETTER U WITH GRAVE */
   1181 	0x00d8, /* LATIN CAPITAL LETTER O WITH STROKE */
   1182 	0x0152, /* LATIN CAPITAL LIGATURE OE */
   1183 	0x00d5, /* LATIN CAPITAL LETTER O WITH TILDE */
   1184 	0x00d4, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
   1185 	0x00d3, /* LATIN CAPITAL LETTER O WITH ACUTE */
   1186 	0x00d2, /* LATIN CAPITAL LETTER O WITH GRAVE */
   1187 	0x00cf, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
   1188 	0x00ce, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
   1189 	0x00cd, /* LATIN CAPITAL LETTER I WITH ACUTE */
   1190 /* 9 */	0x00cc, /* LATIN CAPITAL LETTER I WITH GRAVE */
   1191 	0x00cb, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
   1192 	0x00ca, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
   1193 	0x00c8, /* LATIN CAPITAL LETTER E WITH GRAVE */
   1194 	0x00c3, /* LATIN CAPITAL LETTER A WITH TILDE */
   1195 	0x00c2, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
   1196 	0x00c1, /* LATIN CAPITAL LETTER A WITH ACUTE */
   1197 	0x00c0, /* LATIN CAPITAL LETTER A WITH GRAVE */
   1198 	0x00b9, /* SUPERSCRIPT ONE */
   1199 	0x00b7, /* MIDDLE DOT */
   1200 	0x03b6, /* GREEK SMALL LETTER ZETA */
   1201 	0x00b3, /* SUPERSCRIPT THREE */
   1202 	0x00a9, /* COPYRIGHT SIGN */
   1203 	0x00a4, /* CURRENCY SIGN */
   1204 	0x03ba, /* GREEK SMALL LETTER KAPPA */
   1205 	_e000U  /* mirrored question mark? */
   1206 };
   1207 
   1208 static int vga_pcvt_mapchar(int, u_int *);
   1209 
   1210 static int
   1211 vga_pcvt_mapchar(int uni, u_int *index)
   1212 {
   1213 	int i;
   1214 
   1215 	for (i = 0; i < 0xa0; i++) /* 0xa0..0xff are reserved */
   1216 		if (uni == pcvt_unichars[i]) {
   1217 			*index = i;
   1218 			return (5);
   1219 		}
   1220 	*index = 0x99; /* middle dot */
   1221 	return (0);
   1222 }
   1223 
   1224 #endif /* WSCONS_SUPPORT_PCVTFONTS */
   1225 
   1226 #ifdef WSCONS_SUPPORT_ISO7FONTS
   1227 
   1228 static int
   1229 vga_iso7_mapchar(int uni, u_int *index)
   1230 {
   1231 
   1232 	/*
   1233 	 * U+0384 (GREEK TONOS) to
   1234 	 * U+03ce (GREEK SMALL LETTER OMEGA WITH TONOS)
   1235 	 * map directly to the iso-9 font
   1236 	 */
   1237 	if (uni >= 0x0384 && uni <= 0x03ce) {
   1238 		/* U+0384 is at offset 0xb4 in the font */
   1239 		*index = uni - 0x0384 + 0xb4;
   1240 		return (5);
   1241 	}
   1242 
   1243 	/* XXX more chars in the iso-9 font */
   1244 
   1245 	*index = 0xa4; /* shaded rectangle */
   1246 	return (0);
   1247 }
   1248 
   1249 #endif /* WSCONS_SUPPORT_ISO7FONTS */
   1250 
   1251 static int _vga_mapchar(void *, const struct egavga_font *, int, u_int *);
   1252 
   1253 static int
   1254 _vga_mapchar(void *id, const struct egavga_font *font, int uni, u_int *index)
   1255 {
   1256 
   1257 	switch (font->wsfont->encoding) {
   1258 	case WSDISPLAY_FONTENC_ISO:
   1259 		if (uni < 256) {
   1260 			*index = uni;
   1261 			return (5);
   1262 		} else {
   1263 			*index = ' ';
   1264 			return (0);
   1265 		}
   1266 	case WSDISPLAY_FONTENC_IBM:
   1267 		return (pcdisplay_mapchar(id, uni, index));
   1268 #ifdef WSCONS_SUPPORT_PCVTFONTS
   1269 	case WSDISPLAY_FONTENC_PCVT:
   1270 		return (vga_pcvt_mapchar(uni, index));
   1271 #endif
   1272 #ifdef WSCONS_SUPPORT_ISO7FONTS
   1273 	case WSDISPLAY_FONTENC_ISO7:
   1274 		return (vga_iso7_mapchar(uni, index));
   1275 #endif
   1276 	default:
   1277 #ifdef VGAFONTDEBUG
   1278 		printf("_vga_mapchar: encoding=%d\n", font->wsfont->encoding);
   1279 #endif
   1280 		*index = ' ';
   1281 		return (0);
   1282 	}
   1283 }
   1284 
   1285 static int
   1286 vga_mapchar(void *id, int uni, u_int *index)
   1287 {
   1288 	struct vgascreen *scr = id;
   1289 	u_int idx1, idx2;
   1290 	int res1, res2;
   1291 
   1292 	res1 = 0;
   1293 	idx1 = ' '; /* space */
   1294 	if (scr->fontset1)
   1295 		res1 = _vga_mapchar(id, scr->fontset1, uni, &idx1);
   1296 	res2 = -1;
   1297 	if (scr->fontset2) {
   1298 		KASSERT(VGA_SCREEN_CANTWOFONTS(scr->pcs.type));
   1299 		res2 = _vga_mapchar(id, scr->fontset2, uni, &idx2);
   1300 	}
   1301 	if (res2 > res1) {
   1302 		*index = idx2 | 0x0800; /* attribute bit 3 */
   1303 		return (res2);
   1304 	}
   1305 	*index = idx1;
   1306 	return (res1);
   1307 }
   1308 
   1309 #ifdef WSDISPLAY_CHARFUNCS
   1310 int
   1311 vga_getwschar(void *cookie, struct wsdisplay_char *wschar)
   1312 {
   1313 	struct vgascreen *scr = cookie;
   1314 
   1315 	if (scr == NULL) return 0;
   1316 	return (pcdisplay_getwschar(&scr->pcs, wschar));
   1317 }
   1318 
   1319 int
   1320 vga_putwschar(void *cookie, struct wsdisplay_char *wschar)
   1321 {
   1322 	struct vgascreen *scr = cookie;
   1323 
   1324 	if (scr == NULL) return 0;
   1325 	return (pcdisplay_putwschar(&scr->pcs, wschar));
   1326 }
   1327 #endif /* WSDISPLAY_CHARFUNCS */
   1328