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