xref: /netbsd/sys/dev/ic/vga.c (revision bf9ec67e)
1 /* $NetBSD: vga.c,v 1.50 2002/04/04 13:08:35 hannken 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.50 2002/04/04 13:08:35 hannken 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 #include "opt_wsdisplay_compat.h" /* for WSCONS_SUPPORT_PCVTFONTS */
55 
56 static struct wsdisplay_font _vga_builtinfont = {
57 	"builtin",
58 	0, 256,
59 	WSDISPLAY_FONTENC_IBM,
60 	8, 16, 1,
61 	WSDISPLAY_FONTORDER_L2R, 0,
62 	0
63 };
64 
65 struct egavga_font {
66 	struct wsdisplay_font *wsfont;
67 	int cookie; /* wsfont handle */
68 	int slot; /* in adapter RAM */
69 	int usecount;
70 	TAILQ_ENTRY(egavga_font) next; /* LRU queue */
71 };
72 
73 static struct egavga_font vga_builtinfont = {
74 	&_vga_builtinfont,
75 	0, 0
76 };
77 
78 #ifdef VGA_CONSOLE_SCREENTYPE
79 static struct egavga_font vga_consolefont;
80 #endif
81 
82 struct vgascreen {
83 	struct pcdisplayscreen pcs;
84 
85 	LIST_ENTRY(vgascreen) next;
86 
87 	struct vga_config *cfg;
88 
89 	/* videostate */
90 	struct egavga_font *fontset1, *fontset2;
91 	/* font data */
92 	/* palette */
93 
94 	int mindispoffset, maxdispoffset;
95 };
96 
97 static int vgaconsole, vga_console_type, vga_console_attached;
98 static struct vgascreen vga_console_screen;
99 static struct vga_config vga_console_vc;
100 
101 struct egavga_font *egavga_getfont(struct vga_config *, struct vgascreen *,
102 				   char *, int);
103 void egavga_unreffont(struct vga_config *, struct egavga_font *);
104 
105 int vga_selectfont(struct vga_config *, struct vgascreen *, char *, char *);
106 void vga_init_screen(struct vga_config *, struct vgascreen *,
107 		     const struct wsscreen_descr *, int, long *);
108 void vga_init(struct vga_config *, bus_space_tag_t, bus_space_tag_t);
109 static void vga_setfont(struct vga_config *, struct vgascreen *);
110 
111 static int vga_mapchar(void *, int, unsigned int *);
112 static int vga_alloc_attr(void *, int, int, int, long *);
113 static void vga_copyrows(void *, int, int, int);
114 
115 const struct wsdisplay_emulops vga_emulops = {
116 	pcdisplay_cursor,
117 	vga_mapchar,
118 	pcdisplay_putchar,
119 	pcdisplay_copycols,
120 	pcdisplay_erasecols,
121 	vga_copyrows,
122 	pcdisplay_eraserows,
123 	vga_alloc_attr
124 };
125 
126 /*
127  * translate WS(=ANSI) color codes to standard pc ones
128  */
129 static const unsigned char fgansitopc[] = {
130 #ifdef __alpha__
131 	/*
132 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
133 	 * XXX We should probably not bother with this
134 	 * XXX (reinitialize the palette registers).
135 	 */
136 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
137 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
138 #else
139 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
140 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
141 #endif
142 }, bgansitopc[] = {
143 #ifdef __alpha__
144 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
145 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
146 #else
147 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
148 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
149 #endif
150 };
151 
152 const struct wsscreen_descr vga_25lscreen = {
153 	"80x25", 80, 25,
154 	&vga_emulops,
155 	8, 16,
156 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
157 }, vga_25lscreen_mono = {
158 	"80x25", 80, 25,
159 	&vga_emulops,
160 	8, 16,
161 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
162 }, vga_25lscreen_bf = {
163 	"80x25bf", 80, 25,
164 	&vga_emulops,
165 	8, 16,
166 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
167 }, vga_40lscreen = {
168 	"80x40", 80, 40,
169 	&vga_emulops,
170 	8, 10,
171 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
172 }, vga_40lscreen_mono = {
173 	"80x40", 80, 40,
174 	&vga_emulops,
175 	8, 10,
176 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
177 }, vga_40lscreen_bf = {
178 	"80x40bf", 80, 40,
179 	&vga_emulops,
180 	8, 10,
181 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
182 }, vga_50lscreen = {
183 	"80x50", 80, 50,
184 	&vga_emulops,
185 	8, 8,
186 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
187 }, vga_50lscreen_mono = {
188 	"80x50", 80, 50,
189 	&vga_emulops,
190 	8, 8,
191 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
192 }, vga_50lscreen_bf = {
193 	"80x50bf", 80, 50,
194 	&vga_emulops,
195 	8, 8,
196 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
197 }, vga_24lscreen = {
198 	"80x24", 80, 24,
199 	&vga_emulops,
200 	8, 16,
201 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
202 }, vga_24lscreen_mono = {
203 	"80x24", 80, 24,
204 	&vga_emulops,
205 	8, 16,
206 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
207 }, vga_24lscreen_bf = {
208 	"80x24bf", 80, 24,
209 	&vga_emulops,
210 	8, 16,
211 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
212 };
213 
214 #define VGA_SCREEN_CANTWOFONTS(type) (!((type)->capabilities & WSSCREEN_HILIT))
215 
216 const struct wsscreen_descr *_vga_scrlist[] = {
217 	&vga_25lscreen,
218 	&vga_25lscreen_bf,
219 	&vga_40lscreen,
220 	&vga_40lscreen_bf,
221 	&vga_50lscreen,
222 	&vga_50lscreen_bf,
223 	&vga_24lscreen,
224 	&vga_24lscreen_bf,
225 	/* XXX other formats, graphics screen? */
226 }, *_vga_scrlist_mono[] = {
227 	&vga_25lscreen_mono,
228 	&vga_40lscreen_mono,
229 	&vga_50lscreen_mono,
230 	&vga_24lscreen_mono,
231 	/* XXX other formats, graphics screen? */
232 };
233 
234 const struct wsscreen_list vga_screenlist = {
235 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
236 	_vga_scrlist
237 }, vga_screenlist_mono = {
238 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
239 	_vga_scrlist_mono
240 };
241 
242 static int	vga_ioctl(void *, u_long, caddr_t, int, struct proc *);
243 static paddr_t	vga_mmap(void *, off_t, int);
244 static int	vga_alloc_screen(void *, const struct wsscreen_descr *,
245 				 void **, int *, int *, long *);
246 static void	vga_free_screen(void *, void *);
247 static int	vga_show_screen(void *, void *, int,
248 				void (*)(void *, int, int), void *);
249 static int	vga_load_font(void *, void *, struct wsdisplay_font *);
250 
251 void vga_doswitch(struct vga_config *);
252 
253 const struct wsdisplay_accessops vga_accessops = {
254 	vga_ioctl,
255 	vga_mmap,
256 	vga_alloc_screen,
257 	vga_free_screen,
258 	vga_show_screen,
259 	vga_load_font
260 };
261 
262 /*
263  * The following functions implement back-end configuration grabbing
264  * and attachment.
265  */
266 int
267 vga_common_probe(bus_space_tag_t iot, bus_space_tag_t memt)
268 {
269 	bus_space_handle_t ioh_vga, ioh_6845, memh;
270 	u_int8_t regval;
271 	u_int16_t vgadata;
272 	int gotio_vga, gotio_6845, gotmem, mono, rv;
273 	int dispoffset;
274 
275 	gotio_vga = gotio_6845 = gotmem = rv = 0;
276 
277 	if (bus_space_map(iot, 0x3c0, 0x10, 0, &ioh_vga))
278 		goto bad;
279 	gotio_vga = 1;
280 
281 	/* read "misc output register" */
282 	regval = bus_space_read_1(iot, ioh_vga, 0xc);
283 	mono = !(regval & 1);
284 
285 	if (bus_space_map(iot, (mono ? 0x3b0 : 0x3d0), 0x10, 0, &ioh_6845))
286 		goto bad;
287 	gotio_6845 = 1;
288 
289 	if (bus_space_map(memt, 0xa0000, 0x20000, 0, &memh))
290 		goto bad;
291 	gotmem = 1;
292 
293 	dispoffset = (mono ? 0x10000 : 0x18000);
294 
295 	vgadata = bus_space_read_2(memt, memh, dispoffset);
296 	bus_space_write_2(memt, memh, dispoffset, 0xa55a);
297 	if (bus_space_read_2(memt, memh, dispoffset) != 0xa55a)
298 		goto bad;
299 	bus_space_write_2(memt, memh, dispoffset, vgadata);
300 
301 	/*
302 	 * check if this is really a VGA
303 	 * (try to write "Color Select" register as XFree86 does)
304 	 * XXX check before if at least EGA?
305 	 */
306 	/* reset state */
307 	(void) bus_space_read_1(iot, ioh_6845, 10);
308 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
309 			  20 | 0x20); /* colselect | enable */
310 	regval = bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR);
311 	/* toggle the implemented bits */
312 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval ^ 0x0f);
313 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
314 			  20 | 0x20);
315 	/* read back */
316 	if (bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR) != (regval ^ 0x0f))
317 		goto bad;
318 	/* restore contents */
319 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval);
320 
321 	rv = 1;
322 bad:
323 	if (gotio_vga)
324 		bus_space_unmap(iot, ioh_vga, 0x10);
325 	if (gotio_6845)
326 		bus_space_unmap(iot, ioh_6845, 0x10);
327 	if (gotmem)
328 		bus_space_unmap(memt, memh, 0x20000);
329 
330 	return (rv);
331 }
332 
333 /*
334  * We want at least ASCII 32..127 be present in the
335  * first font slot.
336  */
337 #define vga_valid_primary_font(f) \
338 	(f->wsfont->encoding == WSDISPLAY_FONTENC_IBM || \
339 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO || \
340 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO7)
341 
342 struct egavga_font *
343 egavga_getfont(struct vga_config *vc, struct vgascreen *scr, char *name,
344 	       int primary)
345 {
346 	struct egavga_font *f;
347 	int cookie;
348 	struct wsdisplay_font *wf;
349 
350 	TAILQ_FOREACH(f, &vc->vc_fontlist, next) {
351 		if (wsfont_matches(f->wsfont, name,
352 				   8, scr->pcs.type->fontheight, 0) &&
353 		    (!primary || vga_valid_primary_font(f))) {
354 #ifdef VGAFONTDEBUG
355 			if (scr != &vga_console_screen || vga_console_attached)
356 				printf("vga_getfont: %s already present\n",
357 				       name ? name : "<default>");
358 #endif
359 			goto found;
360 		}
361 	}
362 
363 	cookie = wsfont_find(name, 8, scr->pcs.type->fontheight, 0,
364 	                     WSDISPLAY_FONTORDER_L2R, 0);
365 	/* XXX obey "primary" */
366 	if (cookie == -1) {
367 #ifdef VGAFONTDEBUG
368 		if (scr != &vga_console_screen || vga_console_attached)
369 			printf("vga_getfont: %s not found\n", name);
370 #endif
371 		return (0);
372 	}
373 
374 	if (wsfont_lock(cookie, &wf))
375 		return (0);
376 
377 #ifdef VGA_CONSOLE_SCREENTYPE
378 	if (scr == &vga_console_screen)
379 		f = &vga_consolefont;
380 	else
381 #endif
382 	f = malloc(sizeof(struct egavga_font), M_DEVBUF, M_NOWAIT);
383 	if (!f) {
384 		wsfont_unlock(cookie);
385 		return (0);
386 	}
387 	f->wsfont = wf;
388 	f->cookie = cookie;
389 	f->slot = -1; /* not yet loaded */
390 	f->usecount = 0; /* incremented below */
391 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
392 
393 found:
394 	f->usecount++;
395 #ifdef VGAFONTDEBUG
396 	if (scr != &vga_console_screen || vga_console_attached)
397 		printf("vga_getfont: usecount=%d\n", f->usecount);
398 #endif
399 	return (f);
400 }
401 
402 void
403 egavga_unreffont(struct vga_config *vc, struct egavga_font *f)
404 {
405 
406 	f->usecount--;
407 #ifdef VGAFONTDEBUG
408 	printf("vga_unreffont: usecount=%d\n", f->usecount);
409 #endif
410 	if (f->usecount == 0 && f != &vga_builtinfont) {
411 		TAILQ_REMOVE(&vc->vc_fontlist, f, next);
412 		if (f->slot != -1) {
413 			KASSERT(vc->vc_fonts[f->slot] == f);
414 			vc->vc_fonts[f->slot] = 0;
415 		}
416 		wsfont_unlock(f->cookie);
417 #ifdef VGA_CONSOLE_SCREENTYPE
418 		if (f != &vga_consolefont)
419 #endif
420 		free(f, M_DEVBUF);
421 	}
422 }
423 
424 int
425 vga_selectfont(struct vga_config *vc, struct vgascreen *scr, char *name1,
426 	       char *name2)
427 {
428 	const struct wsscreen_descr *type = scr->pcs.type;
429 	struct egavga_font *f1, *f2;
430 
431 	f1 = egavga_getfont(vc, scr, name1, 1);
432 	if (!f1)
433 		return (ENXIO);
434 
435 	if (VGA_SCREEN_CANTWOFONTS(type) && name2) {
436 		f2 = egavga_getfont(vc, scr, name2, 0);
437 		if (!f2) {
438 			egavga_unreffont(vc, f1);
439 			return (ENXIO);
440 		}
441 	} else
442 		f2 = 0;
443 
444 #ifdef VGAFONTDEBUG
445 	if (scr != &vga_console_screen || vga_console_attached) {
446 		printf("vga (%s): font1=%s (slot %d)", type->name,
447 		       f1->wsfont->name, f1->slot);
448 		if (f2)
449 			printf(", font2=%s (slot %d)",
450 			       f2->wsfont->name, f2->slot);
451 		printf("\n");
452 	}
453 #endif
454 	if (scr->fontset1)
455 		egavga_unreffont(vc, scr->fontset1);
456 	scr->fontset1 = f1;
457 	if (scr->fontset2)
458 		egavga_unreffont(vc, scr->fontset2);
459 	scr->fontset2 = f2;
460 	return (0);
461 }
462 
463 void
464 vga_init_screen(struct vga_config *vc, struct vgascreen *scr,
465 		const struct wsscreen_descr *type, int existing, long *attrp)
466 {
467 	int cpos;
468 	int res;
469 
470 	scr->cfg = vc;
471 	scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
472 	scr->pcs.type = type;
473 	scr->pcs.active = existing;
474 	scr->mindispoffset = 0;
475 	scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
476 
477 	if (existing) {
478 		vc->active = scr;
479 
480 		cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
481 		cpos |= vga_6845_read(&vc->hdl, cursorl);
482 
483 		/* make sure we have a valid cursor position */
484 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
485 			cpos = 0;
486 
487 		scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
488 		scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
489 
490 		/* make sure we have a valid memory offset */
491 		if (scr->pcs.dispoffset < scr->mindispoffset ||
492 		    scr->pcs.dispoffset > scr->maxdispoffset)
493 			scr->pcs.dispoffset = scr->mindispoffset;
494 
495 		if (type != vc->currenttype) {
496 			vga_setscreentype(&vc->hdl, type);
497 			vc->currenttype = type;
498 		}
499 	} else {
500 		cpos = 0;
501 		scr->pcs.dispoffset = scr->mindispoffset;
502 	}
503 
504 	scr->pcs.vc_crow = cpos / type->ncols;
505 	scr->pcs.vc_ccol = cpos % type->ncols;
506 	pcdisplay_cursor_init(&scr->pcs, existing);
507 
508 #ifdef __alpha__
509 	if (!vc->hdl.vh_mono)
510 		/*
511 		 * DEC firmware uses a blue background.
512 		 */
513 		res = vga_alloc_attr(scr, WSCOL_WHITE, WSCOL_BLUE,
514 				     WSATTR_WSCOLORS, attrp);
515 	else
516 #endif
517 	res = vga_alloc_attr(scr, 0, 0, 0, attrp);
518 #ifdef DIAGNOSTIC
519 	if (res)
520 		panic("vga_init_screen: attribute botch");
521 #endif
522 
523 	scr->pcs.mem = NULL;
524 
525 	wsfont_init();
526 	scr->fontset1 = scr->fontset2 = 0;
527 	if (vga_selectfont(vc, scr, 0, 0)) {
528 		if (scr == &vga_console_screen)
529 			panic("vga_init_screen: no font");
530 		else
531 			printf("vga_init_screen: no font\n");
532 	}
533 	if (existing)
534 		vga_setfont(vc, scr);
535 
536 	vc->nscreens++;
537 	LIST_INSERT_HEAD(&vc->screens, scr, next);
538 }
539 
540 void
541 vga_init(struct vga_config *vc, bus_space_tag_t iot, bus_space_tag_t memt)
542 {
543 	struct vga_handle *vh = &vc->hdl;
544 	u_int8_t mor;
545 	int i;
546 
547         vh->vh_iot = iot;
548         vh->vh_memt = memt;
549 
550         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
551                 panic("vga_common_setup: couldn't map vga io");
552 
553 	/* read "misc output register" */
554 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, 0xc);
555 	vh->vh_mono = !(mor & 1);
556 
557 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
558 			  &vh->vh_ioh_6845))
559                 panic("vga_common_setup: couldn't map 6845 io");
560 
561         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
562                 panic("vga_common_setup: couldn't map memory");
563 
564         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
565 				(vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
566 				&vh->vh_memh))
567                 panic("vga_common_setup: mem subrange failed");
568 
569 	/* should only reserve the space (no need to map - save KVM) */
570 	vc->vc_biostag = memt;
571 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0,
572 			  &vc->vc_bioshdl))
573 		vc->vc_biosmapped = 0;
574 	else
575 		vc->vc_biosmapped = 1;
576 
577 	vc->nscreens = 0;
578 	LIST_INIT(&vc->screens);
579 	vc->active = NULL;
580 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
581 	callout_init(&vc->vc_switch_callout);
582 
583 	vc->vc_fonts[0] = &vga_builtinfont;
584 	for (i = 1; i < 8; i++)
585 		vc->vc_fonts[i] = 0;
586 	TAILQ_INIT(&vc->vc_fontlist);
587 	TAILQ_INSERT_HEAD(&vc->vc_fontlist, &vga_builtinfont, next);
588 
589 	vc->currentfontset1 = vc->currentfontset2 = 0;
590 }
591 
592 void
593 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
594 		  bus_space_tag_t memt, int type, const struct vga_funcs *vf)
595 {
596 	int console;
597 	struct vga_config *vc;
598 	struct wsemuldisplaydev_attach_args aa;
599 
600 	console = vga_is_console(iot, type);
601 
602 	if (console) {
603 		vc = &vga_console_vc;
604 		vga_console_attached = 1;
605 	} else {
606 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
607 		vga_init(vc, iot, memt);
608 	}
609 
610 	vc->vc_type = type;
611 	vc->vc_funcs = vf;
612 
613 	sc->sc_vc = vc;
614 	vc->softc = sc;
615 
616 	aa.console = console;
617 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
618 	aa.accessops = &vga_accessops;
619 	aa.accesscookie = vc;
620 
621         config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
622 }
623 
624 int
625 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
626 {
627 	long defattr;
628 	const struct wsscreen_descr *scr;
629 
630 	if (check && !vga_common_probe(iot, memt))
631 		return (ENXIO);
632 
633 	/* set up bus-independent VGA configuration */
634 	vga_init(&vga_console_vc, iot, memt);
635 #ifdef VGA_CONSOLE_SCREENTYPE
636 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
637 	       &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
638 	if (!scr)
639 		panic("vga_cnattach: invalid screen type");
640 #else
641 	scr = vga_console_vc.currenttype;
642 #endif
643 	vga_init_screen(&vga_console_vc, &vga_console_screen, scr, 1, &defattr);
644 
645 	wsdisplay_cnattach(scr, &vga_console_screen,
646 			   vga_console_screen.pcs.vc_ccol,
647 			   vga_console_screen.pcs.vc_crow,
648 			   defattr);
649 
650 	vgaconsole = 1;
651 	vga_console_type = type;
652 	return (0);
653 }
654 
655 int
656 vga_is_console(bus_space_tag_t iot, int type)
657 {
658 	if (vgaconsole &&
659 	    !vga_console_attached &&
660 	    iot == vga_console_vc.hdl.vh_iot &&
661 	    (vga_console_type == -1 || (type == vga_console_type)))
662 		return (1);
663 	return (0);
664 }
665 
666 #define	VGA_TS_BLANK	0x20
667 
668 static int
669 vga_get_video(struct vga_config *vc)
670 {
671 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_BLANK) == 0;
672 }
673 
674 static void
675 vga_set_video(struct vga_config *vc, int state)
676 {
677 	int val;
678 
679 	vga_ts_write(&vc->hdl, syncreset, 0x01);
680 	if (state) {					/* unblank screen */
681 		val = vga_ts_read(&vc->hdl, mode);
682 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_BLANK);
683 #ifndef VGA_NO_VBLANK
684 		val = vga_6845_read(&vc->hdl, mode);
685 		vga_6845_write(&vc->hdl, mode, val | 0x80);
686 #endif
687 	} else {					/* blank screen */
688 		val = vga_ts_read(&vc->hdl, mode);
689 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_BLANK);
690 #ifndef VGA_NO_VBLANK
691 		val = vga_6845_read(&vc->hdl, mode);
692 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
693 #endif
694 	}
695 	vga_ts_write(&vc->hdl, syncreset, 0x03);
696 }
697 
698 int
699 vga_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
700 {
701 	struct vga_config *vc = v;
702 	const struct vga_funcs *vf = vc->vc_funcs;
703 
704 	switch (cmd) {
705 	case WSDISPLAYIO_GTYPE:
706 		*(int *)data = vc->vc_type;
707 		return 0;
708 
709 	case WSDISPLAYIO_GINFO:
710 		/* XXX should get detailed hardware information here */
711 		return EPASSTHROUGH;
712 
713 	case WSDISPLAYIO_GVIDEO:
714 		*(int *)data = (vga_get_video(vc) ? WSDISPLAYIO_VIDEO_ON :
715 				WSDISPLAYIO_VIDEO_OFF);
716 		return 0;
717 
718 	case WSDISPLAYIO_SVIDEO:
719 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
720 		return 0;
721 
722 	case WSDISPLAYIO_GETCMAP:
723 	case WSDISPLAYIO_PUTCMAP:
724 	case WSDISPLAYIO_GCURPOS:
725 	case WSDISPLAYIO_SCURPOS:
726 	case WSDISPLAYIO_GCURMAX:
727 	case WSDISPLAYIO_GCURSOR:
728 	case WSDISPLAYIO_SCURSOR:
729 		/* NONE of these operations are by the generic VGA driver. */
730 		return EPASSTHROUGH;
731 	}
732 
733 	if (vc->vc_funcs == NULL)
734 		return (EPASSTHROUGH);
735 
736 	if (vf->vf_ioctl == NULL)
737 		return (EPASSTHROUGH);
738 
739 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
740 }
741 
742 static paddr_t
743 vga_mmap(void *v, off_t offset, int prot)
744 {
745 	struct vga_config *vc = v;
746 	const struct vga_funcs *vf = vc->vc_funcs;
747 
748 	if (vc->vc_funcs == NULL)
749 		return (-1);
750 
751 	if (vf->vf_mmap == NULL)
752 		return (-1);
753 
754 	return ((*vf->vf_mmap)(v, offset, prot));
755 }
756 
757 int
758 vga_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
759 		 int *curxp, int *curyp, long *defattrp)
760 {
761 	struct vga_config *vc = v;
762 	struct vgascreen *scr;
763 
764 	if (vc->nscreens == 1) {
765 		struct vgascreen *scr1 = vc->screens.lh_first;
766 		/*
767 		 * When allocating the second screen, get backing store
768 		 * for the first one too.
769 		 * XXX We could be more clever and use video RAM.
770 		 */
771 		scr1->pcs.mem =
772 		  malloc(scr1->pcs.type->ncols * scr1->pcs.type->nrows * 2,
773 			 M_DEVBUF, M_WAITOK);
774 	}
775 
776 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
777 	vga_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
778 
779 	if (vc->nscreens > 1) {
780 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
781 				      M_DEVBUF, M_WAITOK);
782 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
783 	}
784 
785 	*cookiep = scr;
786 	*curxp = scr->pcs.vc_ccol;
787 	*curyp = scr->pcs.vc_crow;
788 
789 	return (0);
790 }
791 
792 void
793 vga_free_screen(void *v, void *cookie)
794 {
795 	struct vgascreen *vs = cookie;
796 	struct vga_config *vc = vs->cfg;
797 
798 	LIST_REMOVE(vs, next);
799 	if (vs->fontset1)
800 		egavga_unreffont(vc, vs->fontset1);
801 	if (vs->fontset2)
802 		egavga_unreffont(vc, vs->fontset2);
803 
804 	if (vs != &vga_console_screen)
805 		free(vs, M_DEVBUF);
806 	else
807 		panic("vga_free_screen: console");
808 
809 	if (vc->active == vs)
810 		vc->active = 0;
811 }
812 
813 static void vga_usefont(struct vga_config *, struct egavga_font *);
814 
815 static void
816 vga_usefont(struct vga_config *vc, struct egavga_font *f)
817 {
818 	int slot;
819 	struct egavga_font *of;
820 
821 	if (f->slot != -1)
822 		goto toend;
823 
824 	for (slot = 0; slot < 8; slot++) {
825 		if (!vc->vc_fonts[slot])
826 			goto loadit;
827 	}
828 
829 	/* have to kick out another one */
830 	TAILQ_FOREACH(of, &vc->vc_fontlist, next) {
831 		if (of->slot != -1) {
832 			if (of == &vga_builtinfont)
833 				continue; /* XXX for now */
834 			KASSERT(vc->vc_fonts[of->slot] == of);
835 			slot = of->slot;
836 			of->slot = -1;
837 			goto loadit;
838 		}
839 	}
840 	panic("vga_usefont");
841 
842 loadit:
843 	vga_loadchars(&vc->hdl, slot, 0, 256,
844 		      f->wsfont->fontheight, 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.vc_crow, scr->pcs.vc_ccol);
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)
984 			vga_setfont(vc, scr);
985 		return (res);
986 	}
987 
988 	return (0);
989 }
990 
991 static int
992 vga_alloc_attr(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.vc_crow, scr->pcs.vc_ccol);
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.vc_crow, scr->pcs.vc_ccol);
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