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