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