xref: /netbsd-src/sys/dev/ic/vga.c (revision b519c70ad771d0a55b3c2277db6b97a05fa6465d)
1 /* $NetBSD: vga.c,v 1.45 2001/12/13 08:31:39 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.45 2001/12/13 08:31:39 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 #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 	/* XXX obey "primary" */
365 	if (cookie == -1) {
366 #ifdef VGAFONTDEBUG
367 		if (scr != &vga_console_screen || vga_console_attached)
368 			printf("vga_getfont: %s not found\n", name);
369 #endif
370 		return (0);
371 	}
372 
373 	if (wsfont_lock(cookie, &wf, WSDISPLAY_FONTORDER_L2R, 0) < 0)
374 		return (0);
375 
376 #ifdef VGA_CONSOLE_SCREENTYPE
377 	if (scr == &vga_console_screen)
378 		f = &vga_consolefont;
379 	else
380 #endif
381 	f = malloc(sizeof(struct egavga_font), M_DEVBUF, M_NOWAIT);
382 	if (!f) {
383 		wsfont_unlock(cookie);
384 		return (0);
385 	}
386 	f->wsfont = wf;
387 	f->cookie = cookie;
388 	f->slot = -1; /* not yet loaded */
389 	f->usecount = 0; /* incremented below */
390 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
391 
392 found:
393 	f->usecount++;
394 #ifdef VGAFONTDEBUG
395 	if (scr != &vga_console_screen || vga_console_attached)
396 		printf("vga_getfont: usecount=%d\n", f->usecount);
397 #endif
398 	return (f);
399 }
400 
401 void
402 egavga_unreffont(struct vga_config *vc, struct egavga_font *f)
403 {
404 
405 	f->usecount--;
406 #ifdef VGAFONTDEBUG
407 	printf("vga_unreffont: usecount=%d\n", f->usecount);
408 #endif
409 	if (f->usecount == 0 && f != &vga_builtinfont) {
410 		TAILQ_REMOVE(&vc->vc_fontlist, f, next);
411 		if (f->slot != -1) {
412 			KASSERT(vc->vc_fonts[f->slot] == f);
413 			vc->vc_fonts[f->slot] = 0;
414 		}
415 		wsfont_unlock(f->cookie);
416 #ifdef VGA_CONSOLE_SCREENTYPE
417 		if (f != &vga_consolefont)
418 #endif
419 		free(f, M_DEVBUF);
420 	}
421 }
422 
423 int
424 vga_selectfont(struct vga_config *vc, struct vgascreen *scr, char *name1,
425 	       char *name2)
426 {
427 	const struct wsscreen_descr *type = scr->pcs.type;
428 	struct egavga_font *f1, *f2;
429 
430 	f1 = egavga_getfont(vc, scr, name1, 1);
431 	if (!f1)
432 		return (ENXIO);
433 
434 	if (VGA_SCREEN_CANTWOFONTS(type) && name2) {
435 		f2 = egavga_getfont(vc, scr, name2, 0);
436 		if (!f2) {
437 			egavga_unreffont(vc, f1);
438 			return (ENXIO);
439 		}
440 	} else
441 		f2 = 0;
442 
443 #ifdef VGAFONTDEBUG
444 	if (scr != &vga_console_screen || vga_console_attached) {
445 		printf("vga (%s): font1=%s (slot %d)", type->name,
446 		       f1->wsfont->name, f1->slot);
447 		if (f2)
448 			printf(", font2=%s (slot %d)",
449 			       f2->wsfont->name, f2->slot);
450 		printf("\n");
451 	}
452 #endif
453 	if (scr->fontset1)
454 		egavga_unreffont(vc, scr->fontset1);
455 	scr->fontset1 = f1;
456 	if (scr->fontset2)
457 		egavga_unreffont(vc, scr->fontset2);
458 	scr->fontset2 = f2;
459 	return (0);
460 }
461 
462 void
463 vga_init_screen(struct vga_config *vc, struct vgascreen *scr,
464 		const struct wsscreen_descr *type, int existing, long *attrp)
465 {
466 	int cpos;
467 	int res;
468 
469 	scr->cfg = vc;
470 	scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
471 	scr->pcs.type = type;
472 	scr->pcs.active = existing;
473 	scr->mindispoffset = 0;
474 	scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
475 
476 	if (existing) {
477 		vc->active = scr;
478 
479 		cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
480 		cpos |= vga_6845_read(&vc->hdl, cursorl);
481 
482 		/* make sure we have a valid cursor position */
483 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
484 			cpos = 0;
485 
486 		scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
487 		scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
488 
489 		/* make sure we have a valid memory offset */
490 		if (scr->pcs.dispoffset < scr->mindispoffset ||
491 		    scr->pcs.dispoffset > scr->maxdispoffset)
492 			scr->pcs.dispoffset = scr->mindispoffset;
493 
494 		if (type != vc->currenttype) {
495 			vga_setscreentype(&vc->hdl, type);
496 			vc->currenttype = type;
497 		}
498 	} else {
499 		cpos = 0;
500 		scr->pcs.dispoffset = scr->mindispoffset;
501 	}
502 
503 	scr->pcs.vc_crow = cpos / type->ncols;
504 	scr->pcs.vc_ccol = cpos % type->ncols;
505 	pcdisplay_cursor_init(&scr->pcs, existing);
506 
507 #ifdef __alpha__
508 	if (!vc->hdl.vh_mono)
509 		/*
510 		 * DEC firmware uses a blue background.
511 		 */
512 		res = vga_alloc_attr(scr, WSCOL_WHITE, WSCOL_BLUE,
513 				     WSATTR_WSCOLORS, attrp);
514 	else
515 #endif
516 	res = vga_alloc_attr(scr, 0, 0, 0, attrp);
517 #ifdef DIAGNOSTIC
518 	if (res)
519 		panic("vga_init_screen: attribute botch");
520 #endif
521 
522 	scr->pcs.mem = NULL;
523 
524 	wsfont_init();
525 	scr->fontset1 = scr->fontset2 = 0;
526 	if (vga_selectfont(vc, scr, 0, 0)) {
527 		if (scr == &vga_console_screen)
528 			panic("vga_init_screen: no font");
529 		else
530 			printf("vga_init_screen: no font\n");
531 	}
532 	if (existing)
533 		vga_setfont(vc, scr);
534 
535 	vc->nscreens++;
536 	LIST_INSERT_HEAD(&vc->screens, scr, next);
537 }
538 
539 void
540 vga_init(struct vga_config *vc, bus_space_tag_t iot, bus_space_tag_t memt)
541 {
542 	struct vga_handle *vh = &vc->hdl;
543 	u_int8_t mor;
544 	int i;
545 
546         vh->vh_iot = iot;
547         vh->vh_memt = memt;
548 
549         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
550                 panic("vga_common_setup: couldn't map vga io");
551 
552 	/* read "misc output register" */
553 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, 0xc);
554 	vh->vh_mono = !(mor & 1);
555 
556 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
557 			  &vh->vh_ioh_6845))
558                 panic("vga_common_setup: couldn't map 6845 io");
559 
560         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
561                 panic("vga_common_setup: couldn't map memory");
562 
563         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
564 				(vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
565 				&vh->vh_memh))
566                 panic("vga_common_setup: mem subrange failed");
567 
568 	/* should only reserve the space (no need to map - save KVM) */
569 	vc->vc_biostag = memt;
570 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0,
571 			  &vc->vc_bioshdl))
572 		vc->vc_biosmapped = 0;
573 	else
574 		vc->vc_biosmapped = 1;
575 
576 	vc->nscreens = 0;
577 	LIST_INIT(&vc->screens);
578 	vc->active = NULL;
579 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
580 	callout_init(&vc->vc_switch_callout);
581 
582 	vc->vc_fonts[0] = &vga_builtinfont;
583 	for (i = 1; i < 8; i++)
584 		vc->vc_fonts[i] = 0;
585 	TAILQ_INIT(&vc->vc_fontlist);
586 	TAILQ_INSERT_HEAD(&vc->vc_fontlist, &vga_builtinfont, next);
587 
588 	vc->currentfontset1 = vc->currentfontset2 = 0;
589 }
590 
591 void
592 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
593 		  bus_space_tag_t memt, int type, const struct vga_funcs *vf)
594 {
595 	int console;
596 	struct vga_config *vc;
597 	struct wsemuldisplaydev_attach_args aa;
598 
599 	console = vga_is_console(iot, type);
600 
601 	if (console) {
602 		vc = &vga_console_vc;
603 		vga_console_attached = 1;
604 	} else {
605 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
606 		vga_init(vc, iot, memt);
607 	}
608 
609 	vc->vc_type = type;
610 	vc->vc_funcs = vf;
611 
612 	sc->sc_vc = vc;
613 	vc->softc = sc;
614 
615 	aa.console = console;
616 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
617 	aa.accessops = &vga_accessops;
618 	aa.accesscookie = vc;
619 
620         config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
621 }
622 
623 int
624 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
625 {
626 	long defattr;
627 	const struct wsscreen_descr *scr;
628 
629 	if (check && !vga_common_probe(iot, memt))
630 		return (ENXIO);
631 
632 	/* set up bus-independent VGA configuration */
633 	vga_init(&vga_console_vc, iot, memt);
634 #ifdef VGA_CONSOLE_SCREENTYPE
635 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
636 	       &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
637 	if (!scr)
638 		panic("vga_cnattach: invalid screen type");
639 #else
640 	scr = vga_console_vc.currenttype;
641 #endif
642 	vga_init_screen(&vga_console_vc, &vga_console_screen, scr, 1, &defattr);
643 
644 	wsdisplay_cnattach(scr, &vga_console_screen,
645 			   vga_console_screen.pcs.vc_ccol,
646 			   vga_console_screen.pcs.vc_crow,
647 			   defattr);
648 
649 	vgaconsole = 1;
650 	vga_console_type = type;
651 	return (0);
652 }
653 
654 int
655 vga_is_console(bus_space_tag_t iot, int type)
656 {
657 	if (vgaconsole &&
658 	    !vga_console_attached &&
659 	    iot == vga_console_vc.hdl.vh_iot &&
660 	    (vga_console_type == -1 || (type == vga_console_type)))
661 		return (1);
662 	return (0);
663 }
664 
665 int
666 vga_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
667 {
668 	struct vga_config *vc = v;
669 	const struct vga_funcs *vf = vc->vc_funcs;
670 
671 	switch (cmd) {
672 	case WSDISPLAYIO_GTYPE:
673 		*(int *)data = vc->vc_type;
674 		return 0;
675 
676 	case WSDISPLAYIO_GINFO:
677 		/* XXX should get detailed hardware information here */
678 		return ENOTTY;
679 
680 	case WSDISPLAYIO_GETCMAP:
681 	case WSDISPLAYIO_PUTCMAP:
682 	case WSDISPLAYIO_GVIDEO:
683 	case WSDISPLAYIO_SVIDEO:
684 	case WSDISPLAYIO_GCURPOS:
685 	case WSDISPLAYIO_SCURPOS:
686 	case WSDISPLAYIO_GCURMAX:
687 	case WSDISPLAYIO_GCURSOR:
688 	case WSDISPLAYIO_SCURSOR:
689 		/* NONE of these operations are by the generic VGA driver. */
690 		return ENOTTY;
691 	}
692 
693 	if (vc->vc_funcs == NULL)
694 		return (-1);
695 
696 	if (vf->vf_ioctl == NULL)
697 		return (-1);
698 
699 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
700 }
701 
702 static paddr_t
703 vga_mmap(void *v, off_t offset, int prot)
704 {
705 	struct vga_config *vc = v;
706 	const struct vga_funcs *vf = vc->vc_funcs;
707 
708 	if (vc->vc_funcs == NULL)
709 		return (-1);
710 
711 	if (vf->vf_mmap == NULL)
712 		return (-1);
713 
714 	return ((*vf->vf_mmap)(v, offset, prot));
715 }
716 
717 int
718 vga_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
719 		 int *curxp, int *curyp, long *defattrp)
720 {
721 	struct vga_config *vc = v;
722 	struct vgascreen *scr;
723 
724 	if (vc->nscreens == 1) {
725 		struct vgascreen *scr1 = vc->screens.lh_first;
726 		/*
727 		 * When allocating the second screen, get backing store
728 		 * for the first one too.
729 		 * XXX We could be more clever and use video RAM.
730 		 */
731 		scr1->pcs.mem =
732 		  malloc(scr1->pcs.type->ncols * scr1->pcs.type->nrows * 2,
733 			 M_DEVBUF, M_WAITOK);
734 	}
735 
736 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
737 	vga_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
738 
739 	if (vc->nscreens > 1) {
740 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
741 				      M_DEVBUF, M_WAITOK);
742 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
743 	}
744 
745 	*cookiep = scr;
746 	*curxp = scr->pcs.vc_ccol;
747 	*curyp = scr->pcs.vc_crow;
748 
749 	return (0);
750 }
751 
752 void
753 vga_free_screen(void *v, void *cookie)
754 {
755 	struct vgascreen *vs = cookie;
756 	struct vga_config *vc = vs->cfg;
757 
758 	LIST_REMOVE(vs, next);
759 	if (vs->fontset1)
760 		egavga_unreffont(vc, vs->fontset1);
761 	if (vs->fontset2)
762 		egavga_unreffont(vc, vs->fontset2);
763 
764 	if (vs != &vga_console_screen)
765 		free(vs, M_DEVBUF);
766 	else
767 		panic("vga_free_screen: console");
768 
769 	if (vc->active == vs)
770 		vc->active = 0;
771 }
772 
773 static void vga_usefont(struct vga_config *, struct egavga_font *);
774 
775 static void
776 vga_usefont(struct vga_config *vc, struct egavga_font *f)
777 {
778 	int slot;
779 	struct egavga_font *of;
780 
781 	if (f->slot != -1)
782 		goto toend;
783 
784 	for (slot = 0; slot < 8; slot++) {
785 		if (!vc->vc_fonts[slot])
786 			goto loadit;
787 	}
788 
789 	/* have to kick out another one */
790 	TAILQ_FOREACH(of, &vc->vc_fontlist, next) {
791 		if (of->slot != -1) {
792 			if (of == &vga_builtinfont)
793 				continue; /* XXX for now */
794 			KASSERT(vc->vc_fonts[of->slot] == of);
795 			slot = of->slot;
796 			of->slot = -1;
797 			goto loadit;
798 		}
799 	}
800 	panic("vga_usefont");
801 
802 loadit:
803 	vga_loadchars(&vc->hdl, slot, 0, 256,
804 		      f->wsfont->fontheight, f->wsfont->data);
805 	f->slot = slot;
806 	vc->vc_fonts[slot] = f;
807 
808 toend:
809 	TAILQ_REMOVE(&vc->vc_fontlist, f, next);
810 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
811 }
812 
813 static void
814 vga_setfont(struct vga_config *vc, struct vgascreen *scr)
815 {
816 	int fontslot1, fontslot2;
817 
818 	if (scr->fontset1)
819 		vga_usefont(vc, scr->fontset1);
820 	if (scr->fontset2)
821 		vga_usefont(vc, scr->fontset2);
822 
823 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
824 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
825 	if (vc->currentfontset1 != fontslot1 ||
826 	    vc->currentfontset2 != fontslot2) {
827 		vga_setfontset(&vc->hdl, fontslot1, fontslot2);
828 		vc->currentfontset1 = fontslot1;
829 		vc->currentfontset2 = fontslot2;
830 	}
831 }
832 
833 int
834 vga_show_screen(void *v, void *cookie, int waitok,
835 		void (*cb)(void *, int, int), void *cbarg)
836 {
837 	struct vgascreen *scr = cookie, *oldscr;
838 	struct vga_config *vc = scr->cfg;
839 
840 	oldscr = vc->active; /* can be NULL! */
841 	if (scr == oldscr) {
842 		return (0);
843 	}
844 
845 	vc->wantedscreen = cookie;
846 	vc->switchcb = cb;
847 	vc->switchcbarg = cbarg;
848 	if (cb) {
849 		callout_reset(&vc->vc_switch_callout, 0,
850 		    (void(*)(void *))vga_doswitch, vc);
851 		return (EAGAIN);
852 	}
853 
854 	vga_doswitch(vc);
855 	return (0);
856 }
857 
858 void
859 vga_doswitch(struct vga_config *vc)
860 {
861 	struct vgascreen *scr, *oldscr;
862 	struct vga_handle *vh = &vc->hdl;
863 	const struct wsscreen_descr *type;
864 
865 	scr = vc->wantedscreen;
866 	if (!scr) {
867 		printf("vga_doswitch: disappeared\n");
868 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
869 		return;
870 	}
871 	type = scr->pcs.type;
872 	oldscr = vc->active; /* can be NULL! */
873 #ifdef DIAGNOSTIC
874 	if (oldscr) {
875 		if (!oldscr->pcs.active)
876 			panic("vga_show_screen: not active");
877 		if (oldscr->pcs.type != vc->currenttype)
878 			panic("vga_show_screen: bad type");
879 	}
880 #endif
881 	if (scr == oldscr) {
882 		return;
883 	}
884 #ifdef DIAGNOSTIC
885 	if (scr->pcs.active)
886 		panic("vga_show_screen: active");
887 #endif
888 
889 	if (oldscr) {
890 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
891 
892 		oldscr->pcs.active = 0;
893 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
894 					oldscr->pcs.dispoffset, oldscr->pcs.mem,
895 					oldtype->ncols * oldtype->nrows);
896 	}
897 
898 	if (vc->currenttype != type) {
899 		vga_setscreentype(vh, type);
900 		vc->currenttype = type;
901 	}
902 
903 	vga_setfont(vc, scr);
904 	/* XXX swich colours! */
905 
906 	scr->pcs.dispoffset = scr->mindispoffset;
907 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
908 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
909 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
910 	}
911 
912 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
913 				scr->pcs.dispoffset, scr->pcs.mem,
914 				type->ncols * type->nrows);
915 	scr->pcs.active = 1;
916 
917 	vc->active = scr;
918 
919 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
920 			 scr->pcs.vc_crow, scr->pcs.vc_ccol);
921 
922 	vc->wantedscreen = 0;
923 	if (vc->switchcb)
924 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
925 }
926 
927 static int
928 vga_load_font(void *v, void *cookie, struct wsdisplay_font *data)
929 {
930 	struct vga_config *vc = v;
931 	struct vgascreen *scr = cookie;
932 	char *name2;
933 	int res;
934 
935 	if (scr) {
936 		name2 = strchr(data->name, ',');
937 		if (name2)
938 			*name2++ = '\0';
939 		res = vga_selectfont(vc, scr, data->name, name2);
940 		if (!res)
941 			vga_setfont(vc, scr);
942 		return (res);
943 	}
944 
945 	return (0);
946 }
947 
948 static int
949 vga_alloc_attr(void *id, int fg, int bg, int flags, long *attrp)
950 {
951 	struct vgascreen *scr = id;
952 	struct vga_config *vc = scr->cfg;
953 
954 	if (vc->hdl.vh_mono) {
955 		if (flags & WSATTR_WSCOLORS)
956 			return (EINVAL);
957 		if (flags & WSATTR_REVERSE)
958 			*attrp = 0x70;
959 		else
960 			*attrp = 0x07;
961 		if (flags & WSATTR_UNDERLINE)
962 			*attrp |= FG_UNDERLINE;
963 		if (flags & WSATTR_HILIT)
964 			*attrp |= FG_INTENSE;
965 	} else {
966 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
967 			return (EINVAL);
968 		if (flags & WSATTR_WSCOLORS)
969 			*attrp = fgansitopc[fg] | bgansitopc[bg];
970 		else
971 			*attrp = 7;
972 		if (flags & WSATTR_HILIT)
973 			*attrp += 8;
974 	}
975 	if (flags & WSATTR_BLINK)
976 		*attrp |= FG_BLINK;
977 	return (0);
978 }
979 
980 static void
981 vga_copyrows(void *id, int srcrow, int dstrow, int nrows)
982 {
983 	struct vgascreen *scr = id;
984 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
985 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
986 	int ncols = scr->pcs.type->ncols;
987 	bus_size_t srcoff, dstoff;
988 
989 	srcoff = srcrow * ncols + 0;
990 	dstoff = dstrow * ncols + 0;
991 
992 	if (scr->pcs.active) {
993 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
994 #ifdef PCDISPLAY_SOFTCURSOR
995 			int cursoron = scr->pcs.cursoron;
996 
997 			if (cursoron)
998 				pcdisplay_cursor(&scr->pcs, 0,
999 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
1000 #endif
1001 			/* scroll up whole screen */
1002 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
1003 			    <= scr->maxdispoffset) {
1004 				scr->pcs.dispoffset += srcrow * ncols * 2;
1005 			} else {
1006 				bus_space_copy_region_2(memt, memh,
1007 					scr->pcs.dispoffset + srcoff * 2,
1008 					memh, scr->mindispoffset,
1009 					nrows * ncols);
1010 				scr->pcs.dispoffset = scr->mindispoffset;
1011 			}
1012 			vga_6845_write(&scr->cfg->hdl, startadrh,
1013 				       scr->pcs.dispoffset >> 9);
1014 			vga_6845_write(&scr->cfg->hdl, startadrl,
1015 				       scr->pcs.dispoffset >> 1);
1016 #ifdef PCDISPLAY_SOFTCURSOR
1017 			if (cursoron)
1018 				pcdisplay_cursor(&scr->pcs, 1,
1019 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
1020 #endif
1021 		} else {
1022 			bus_space_copy_region_2(memt, memh,
1023 					scr->pcs.dispoffset + srcoff * 2,
1024 					memh, scr->pcs.dispoffset + dstoff * 2,
1025 					nrows * ncols);
1026 		}
1027 	} else
1028 		memcpy(&scr->pcs.mem[dstoff], &scr->pcs.mem[srcoff],
1029 		      nrows * ncols * 2);
1030 }
1031 
1032 #ifdef WSCONS_SUPPORT_PCVTFONTS
1033 
1034 #define NOTYET 0xffff
1035 static const u_int16_t pcvt_unichars[0xa0] = {
1036 /* 0 */	_e006U, /* N/L control */
1037 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1038 	NOTYET,
1039 	0x2409, /* SYMBOL FOR HORIZONTAL TABULATION */
1040 	0x240a, /* SYMBOL FOR LINE FEED */
1041 	0x240b, /* SYMBOL FOR VERTICAL TABULATION */
1042 	0x240c, /* SYMBOL FOR FORM FEED */
1043 	0x240d, /* SYMBOL FOR CARRIAGE RETURN */
1044 	NOTYET, NOTYET,
1045 /* 1 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1046 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1047 /* 2 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1048 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1049 /* 3 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1050 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1051 /* 4 */	0x03c1, /* GREEK SMALL LETTER RHO */
1052 	0x03c8, /* GREEK SMALL LETTER PSI */
1053 	0x2202, /* PARTIAL DIFFERENTIAL */
1054 	0x03bb, /* GREEK SMALL LETTER LAMDA */
1055 	0x03b9, /* GREEK SMALL LETTER IOTA */
1056 	0x03b7, /* GREEK SMALL LETTER ETA */
1057 	0x03b5, /* GREEK SMALL LETTER EPSILON */
1058 	0x03c7, /* GREEK SMALL LETTER CHI */
1059 	0x2228, /* LOGICAL OR */
1060 	0x2227, /* LOGICAL AND */
1061 	0x222a, /* UNION */
1062 	0x2283, /* SUPERSET OF */
1063 	0x2282, /* SUBSET OF */
1064 	0x03a5, /* GREEK CAPITAL LETTER UPSILON */
1065 	0x039e, /* GREEK CAPITAL LETTER XI */
1066 	0x03a8, /* GREEK CAPITAL LETTER PSI */
1067 /* 5 */	0x03a0, /* GREEK CAPITAL LETTER PI */
1068 	0x21d2, /* RIGHTWARDS DOUBLE ARROW */
1069 	0x21d4, /* LEFT RIGHT DOUBLE ARROW */
1070 	0x039b, /* GREEK CAPITAL LETTER LAMDA */
1071 	0x0398, /* GREEK CAPITAL LETTER THETA */
1072 	0x2243, /* ASYMPTOTICALLY EQUAL TO */
1073 	0x2207, /* NABLA */
1074 	0x2206, /* INCREMENT */
1075 	0x221d, /* PROPORTIONAL TO */
1076 	0x2234, /* THEREFORE */
1077 	0x222b, /* INTEGRAL */
1078 	0x2215, /* DIVISION SLASH */
1079 	0x2216, /* SET MINUS */
1080 	_e00eU, /* angle? */
1081 	_e00dU, /* inverted angle? */
1082 	_e00bU, /* braceleftmid */
1083 /* 6 */	_e00cU, /* bracerightmid */
1084 	_e007U, /* bracelefttp */
1085 	_e008U, /* braceleftbt */
1086 	_e009U, /* bracerighttp */
1087 	_e00aU, /* bracerightbt */
1088 	0x221a, /* SQUARE ROOT */
1089 	0x03c9, /* GREEK SMALL LETTER OMEGA */
1090 	0x00a5, /* YEN SIGN */
1091 	0x03be, /* GREEK SMALL LETTER XI */
1092 	0x00fd, /* LATIN SMALL LETTER Y WITH ACUTE */
1093 	0x00fe, /* LATIN SMALL LETTER THORN */
1094 	0x00f0, /* LATIN SMALL LETTER ETH */
1095 	0x00de, /* LATIN CAPITAL LETTER THORN */
1096 	0x00dd, /* LATIN CAPITAL LETTER Y WITH ACUTE */
1097 	0x00d7, /* MULTIPLICATION SIGN */
1098 	0x00d0, /* LATIN CAPITAL LETTER ETH */
1099 /* 7 */	0x00be, /* VULGAR FRACTION THREE QUARTERS */
1100 	0x00b8, /* CEDILLA */
1101 	0x00b4, /* ACUTE ACCENT */
1102 	0x00af, /* MACRON */
1103 	0x00ae, /* REGISTERED SIGN */
1104 	0x00ad, /* SOFT HYPHEN */
1105 	0x00ac, /* NOT SIGN */
1106 	0x00a8, /* DIAERESIS */
1107 	0x2260, /* NOT EQUAL TO */
1108 	_e005U, /* scan 9 */
1109 	_e004U, /* scan 7 */
1110 	_e003U, /* scan 5 */
1111 	_e002U, /* scan 3 */
1112 	_e001U, /* scan 1 */
1113 	0x03c5, /* GREEK SMALL LETTER UPSILON */
1114 	0x00f8, /* LATIN SMALL LETTER O WITH STROKE */
1115 /* 8 */	0x0153, /* LATIN SMALL LIGATURE OE */
1116 	0x00f5, /* LATIN SMALL LETTER O WITH TILDE !!!doc bug */
1117 	0x00e3, /* LATIN SMALL LETTER A WITH TILDE */
1118 	0x0178, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
1119 	0x00db, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
1120 	0x00da, /* LATIN CAPITAL LETTER U WITH ACUTE */
1121 	0x00d9, /* LATIN CAPITAL LETTER U WITH GRAVE */
1122 	0x00d8, /* LATIN CAPITAL LETTER O WITH STROKE */
1123 	0x0152, /* LATIN CAPITAL LIGATURE OE */
1124 	0x00d5, /* LATIN CAPITAL LETTER O WITH TILDE */
1125 	0x00d4, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
1126 	0x00d3, /* LATIN CAPITAL LETTER O WITH ACUTE */
1127 	0x00d2, /* LATIN CAPITAL LETTER O WITH GRAVE */
1128 	0x00cf, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
1129 	0x00ce, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
1130 	0x00cd, /* LATIN CAPITAL LETTER I WITH ACUTE */
1131 /* 9 */	0x00cc, /* LATIN CAPITAL LETTER I WITH GRAVE */
1132 	0x00cb, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
1133 	0x00ca, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
1134 	0x00c8, /* LATIN CAPITAL LETTER E WITH GRAVE */
1135 	0x00c3, /* LATIN CAPITAL LETTER A WITH TILDE */
1136 	0x00c2, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
1137 	0x00c1, /* LATIN CAPITAL LETTER A WITH ACUTE */
1138 	0x00c0, /* LATIN CAPITAL LETTER A WITH GRAVE */
1139 	0x00b9, /* SUPERSCRIPT ONE */
1140 	0x00b7, /* MIDDLE DOT */
1141 	0x03b6, /* GREEK SMALL LETTER ZETA */
1142 	0x00b3, /* SUPERSCRIPT THREE */
1143 	0x00a9, /* COPYRIGHT SIGN */
1144 	0x00a4, /* CURRENCY SIGN */
1145 	0x03ba, /* GREEK SMALL LETTER KAPPA */
1146 	_e000U  /* mirrored question mark? */
1147 };
1148 
1149 static int vga_pcvt_mapchar(int, u_int *);
1150 
1151 static int
1152 vga_pcvt_mapchar(int uni, u_int *index)
1153 {
1154 	int i;
1155 
1156 	for (i = 0; i < 0xa0; i++) /* 0xa0..0xff are reserved */
1157 		if (uni == pcvt_unichars[i]) {
1158 			*index = i;
1159 			return (5);
1160 		}
1161 	*index = 0x99; /* middle dot */
1162 	return (0);
1163 }
1164 
1165 #endif /* WSCONS_SUPPORT_PCVTFONTS */
1166 
1167 #ifdef WSCONS_SUPPORT_ISO7FONTS
1168 
1169 static int
1170 vga_iso7_mapchar(int uni, u_int *index)
1171 {
1172 
1173 	/*
1174 	 * U+0384 (GREEK TONOS) to
1175 	 * U+03ce (GREEK SMALL LETTER OMEGA WITH TONOS)
1176 	 * map directly to the iso-9 font
1177 	 */
1178 	if (uni >= 0x0384 && uni <= 0x03ce) {
1179 		/* U+0384 is at offset 0xb4 in the font */
1180 		*index = uni - 0x0384 + 0xb4;
1181 		return (5);
1182 	}
1183 
1184 	/* XXX more chars in the iso-9 font */
1185 
1186 	*index = 0xa4; /* shaded rectangle */
1187 	return (0);
1188 }
1189 
1190 #endif /* WSCONS_SUPPORT_ISO7FONTS */
1191 
1192 static int _vga_mapchar(void *, const struct egavga_font *, int, u_int *);
1193 
1194 static int
1195 _vga_mapchar(void *id, const struct egavga_font *font, int uni, u_int *index)
1196 {
1197 
1198 	switch (font->wsfont->encoding) {
1199 	case WSDISPLAY_FONTENC_ISO:
1200 		if (uni < 256) {
1201 			*index = uni;
1202 			return (5);
1203 		} else {
1204 			*index = ' ';
1205 			return (0);
1206 		}
1207 		break;
1208 	case WSDISPLAY_FONTENC_IBM:
1209 		return (pcdisplay_mapchar(id, uni, index));
1210 #ifdef WSCONS_SUPPORT_PCVTFONTS
1211 	case WSDISPLAY_FONTENC_PCVT:
1212 		return (vga_pcvt_mapchar(uni, index));
1213 #endif
1214 #ifdef WSCONS_SUPPORT_ISO7FONTS
1215 	case WSDISPLAY_FONTENC_ISO7:
1216 		return (vga_iso7_mapchar(uni, index));
1217 #endif
1218 	default:
1219 #ifdef VGAFONTDEBUG
1220 		printf("_vga_mapchar: encoding=%d\n", font->wsfont->encoding);
1221 #endif
1222 		*index = ' ';
1223 		return (0);
1224 	}
1225 }
1226 
1227 static int
1228 vga_mapchar(void *id, int uni, u_int *index)
1229 {
1230 	struct vgascreen *scr = id;
1231 	u_int idx1, idx2;
1232 	int res1, res2;
1233 
1234 	res1 = 0;
1235 	idx1 = ' '; /* space */
1236 	if (scr->fontset1)
1237 		res1 = _vga_mapchar(id, scr->fontset1, uni, &idx1);
1238 	res2 = -1;
1239 	if (scr->fontset2) {
1240 		KASSERT(VGA_SCREEN_CANTWOFONTS(scr->pcs.type));
1241 		res2 = _vga_mapchar(id, scr->fontset2, uni, &idx2);
1242 	}
1243 	if (res2 > res1) {
1244 		*index = idx2 | 0x0800; /* attribute bit 3 */
1245 		return (res2);
1246 	}
1247 	*index = idx1;
1248 	return (res1);
1249 }
1250