xref: /netbsd-src/sys/dev/ic/vga.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /* $NetBSD: vga.c,v 1.83 2005/12/11 12:21:29 christos 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.83 2005/12/11 12:21:29 christos 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 lwp *);
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 lwp *l)
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, l));
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 	vc->nscreens--;
864 	if (vs->fontset1)
865 		egavga_unreffont(vc, vs->fontset1);
866 	if (vs->fontset2)
867 		egavga_unreffont(vc, vs->fontset2);
868 
869 	if (vs != &vga_console_screen)
870 		free(vs, M_DEVBUF);
871 	else
872 		panic("vga_free_screen: console");
873 
874 	if (vc->active == vs)
875 		vc->active = 0;
876 }
877 
878 static void vga_usefont(struct vga_config *, struct egavga_font *);
879 
880 static void
881 vga_usefont(struct vga_config *vc, struct egavga_font *f)
882 {
883 	int slot;
884 	struct egavga_font *of;
885 
886 	if (f->slot != -1)
887 		goto toend;
888 
889 	for (slot = 0; slot < vc->vc_nfontslots; slot++) {
890 		if (!vc->vc_fonts[slot])
891 			goto loadit;
892 	}
893 
894 	/* have to kick out another one */
895 	TAILQ_FOREACH(of, &vc->vc_fontlist, next) {
896 		if (of->slot != -1) {
897 			KASSERT(vc->vc_fonts[of->slot] == of);
898 			slot = of->slot;
899 			of->slot = -1;
900 			goto loadit;
901 		}
902 	}
903 	panic("vga_usefont");
904 
905 loadit:
906 	vga_loadchars(&vc->hdl, slot, f->wsfont->firstchar,
907 	    f->wsfont->numchars, f->wsfont->fontheight, f->wsfont->data);
908 	f->slot = slot;
909 	vc->vc_fonts[slot] = f;
910 
911 toend:
912 	TAILQ_REMOVE(&vc->vc_fontlist, f, next);
913 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
914 }
915 
916 static void
917 vga_setfont(struct vga_config *vc, struct vgascreen *scr)
918 {
919 	int fontslot1, fontslot2;
920 
921 	if (scr->fontset1)
922 		vga_usefont(vc, scr->fontset1);
923 	if (scr->fontset2)
924 		vga_usefont(vc, scr->fontset2);
925 
926 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
927 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
928 	if (vc->currentfontset1 != fontslot1 ||
929 	    vc->currentfontset2 != fontslot2) {
930 		vga_setfontset(&vc->hdl, fontslot1, fontslot2);
931 		vc->currentfontset1 = fontslot1;
932 		vc->currentfontset2 = fontslot2;
933 	}
934 }
935 
936 int
937 vga_show_screen(void *v, void *cookie, int waitok,
938 		void (*cb)(void *, int, int), void *cbarg)
939 {
940 	struct vgascreen *scr = cookie, *oldscr;
941 	struct vga_config *vc = scr->cfg;
942 
943 	oldscr = vc->active; /* can be NULL! */
944 	if (scr == oldscr) {
945 		return (0);
946 	}
947 
948 	vc->wantedscreen = cookie;
949 	vc->switchcb = cb;
950 	vc->switchcbarg = cbarg;
951 	if (cb) {
952 		callout_reset(&vc->vc_switch_callout, 0,
953 		    (void(*)(void *))vga_doswitch, vc);
954 		return (EAGAIN);
955 	}
956 
957 	vga_doswitch(vc);
958 	return (0);
959 }
960 
961 void
962 vga_doswitch(struct vga_config *vc)
963 {
964 	struct vgascreen *scr, *oldscr;
965 	struct vga_handle *vh = &vc->hdl;
966 	const struct wsscreen_descr *type;
967 
968 	scr = vc->wantedscreen;
969 	if (!scr) {
970 		printf("vga_doswitch: disappeared\n");
971 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
972 		return;
973 	}
974 	type = scr->pcs.type;
975 	oldscr = vc->active; /* can be NULL! */
976 #ifdef DIAGNOSTIC
977 	if (oldscr) {
978 		if (!oldscr->pcs.active)
979 			panic("vga_show_screen: not active");
980 		if (oldscr->pcs.type != vc->currenttype)
981 			panic("vga_show_screen: bad type");
982 	}
983 #endif
984 	if (scr == oldscr) {
985 		return;
986 	}
987 #ifdef DIAGNOSTIC
988 	if (scr->pcs.active)
989 		panic("vga_show_screen: active");
990 #endif
991 
992 	if (oldscr) {
993 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
994 
995 		oldscr->pcs.active = 0;
996 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
997 		    oldscr->pcs.dispoffset, oldscr->pcs.mem,
998 		    oldtype->ncols * oldtype->nrows);
999 	}
1000 
1001 	if (vc->currenttype != type) {
1002 		vga_setscreentype(vh, type);
1003 		vc->currenttype = type;
1004 	}
1005 
1006 	vga_setfont(vc, scr);
1007 	/* XXX swich colours! */
1008 
1009 	scr->pcs.visibleoffset = scr->pcs.dispoffset = scr->mindispoffset;
1010 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
1011 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
1012 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
1013 	}
1014 
1015 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
1016 	    scr->pcs.dispoffset, scr->pcs.mem, type->ncols * type->nrows);
1017 	scr->pcs.active = 1;
1018 
1019 	vc->active = scr;
1020 
1021 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
1022 	    scr->pcs.cursorrow, scr->pcs.cursorcol);
1023 
1024 	vc->wantedscreen = 0;
1025 	if (vc->switchcb)
1026 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
1027 }
1028 
1029 static int
1030 vga_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1031 {
1032 	struct vga_config *vc = v;
1033 	struct vgascreen *scr = cookie;
1034 	char *name2;
1035 	int res;
1036 
1037 	if (scr) {
1038 		name2 = NULL;
1039 		if (data->name) {
1040 			name2 = strchr(data->name, ',');
1041 			if (name2)
1042 				*name2++ = '\0';
1043 		}
1044 		res = vga_selectfont(vc, scr, data->name, name2);
1045 		if (!res && scr->pcs.active)
1046 			vga_setfont(vc, scr);
1047 		return (res);
1048 	}
1049 
1050 	return (0);
1051 }
1052 
1053 static int
1054 vga_allocattr(void *id, int fg, int bg, int flags, long *attrp)
1055 {
1056 	struct vgascreen *scr = id;
1057 	struct vga_config *vc = scr->cfg;
1058 
1059 	if (vc->hdl.vh_mono) {
1060 		if (flags & WSATTR_WSCOLORS)
1061 			return (EINVAL);
1062 		if (flags & WSATTR_REVERSE)
1063 			*attrp = 0x70;
1064 		else
1065 			*attrp = 0x07;
1066 		if (flags & WSATTR_UNDERLINE)
1067 			*attrp |= FG_UNDERLINE;
1068 		if (flags & WSATTR_HILIT)
1069 			*attrp |= FG_INTENSE;
1070 	} else {
1071 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
1072 			return (EINVAL);
1073 		if (flags & WSATTR_WSCOLORS)
1074 			*attrp = fgansitopc[fg] | bgansitopc[bg];
1075 		else
1076 			*attrp = 7;
1077 		if (flags & WSATTR_HILIT)
1078 			*attrp += 8;
1079 	}
1080 	if (flags & WSATTR_BLINK)
1081 		*attrp |= FG_BLINK;
1082 	return (0);
1083 }
1084 
1085 static void
1086 vga_copyrows(void *id, int srcrow, int dstrow, int nrows)
1087 {
1088 	struct vgascreen *scr = id;
1089 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
1090 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
1091 	int ncols = scr->pcs.type->ncols;
1092 	bus_size_t srcoff, dstoff;
1093 
1094 	srcoff = srcrow * ncols + 0;
1095 	dstoff = dstrow * ncols + 0;
1096 
1097 	if (scr->pcs.active) {
1098 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
1099 #ifdef PCDISPLAY_SOFTCURSOR
1100 			int cursoron = scr->pcs.cursoron;
1101 
1102 			if (cursoron)
1103 				pcdisplay_cursor(&scr->pcs, 0,
1104 				    scr->pcs.cursorrow, scr->pcs.cursorcol);
1105 #endif
1106 			/* scroll up whole screen */
1107 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
1108 			    <= scr->maxdispoffset) {
1109 				scr->pcs.dispoffset += srcrow * ncols * 2;
1110 			} else {
1111 				bus_space_copy_region_2(memt, memh,
1112 				    scr->pcs.dispoffset + srcoff * 2,
1113 				    memh, scr->mindispoffset, nrows * ncols);
1114 				scr->pcs.dispoffset = scr->mindispoffset;
1115 			}
1116 			vga_6845_write(&scr->cfg->hdl, startadrh,
1117 			    scr->pcs.dispoffset >> 9);
1118 			vga_6845_write(&scr->cfg->hdl, startadrl,
1119 			    scr->pcs.dispoffset >> 1);
1120 #ifdef PCDISPLAY_SOFTCURSOR
1121 			if (cursoron)
1122 				pcdisplay_cursor(&scr->pcs, 1,
1123 				    scr->pcs.cursorrow, scr->pcs.cursorcol);
1124 #endif
1125 		} else {
1126 			bus_space_copy_region_2(memt, memh,
1127 			    scr->pcs.dispoffset + srcoff * 2,
1128 			    memh, scr->pcs.dispoffset + dstoff * 2,
1129 			    nrows * ncols);
1130 		}
1131 	} else
1132 		memcpy(&scr->pcs.mem[dstoff], &scr->pcs.mem[srcoff],
1133 		    nrows * ncols * 2);
1134 }
1135 
1136 #ifdef WSCONS_SUPPORT_PCVTFONTS
1137 
1138 #define NOTYET 0xffff
1139 static const u_int16_t pcvt_unichars[0xa0] = {
1140 /* 0 */	_e006U, /* N/L control */
1141 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1142 	NOTYET,
1143 	0x2409, /* SYMBOL FOR HORIZONTAL TABULATION */
1144 	0x240a, /* SYMBOL FOR LINE FEED */
1145 	0x240b, /* SYMBOL FOR VERTICAL TABULATION */
1146 	0x240c, /* SYMBOL FOR FORM FEED */
1147 	0x240d, /* SYMBOL FOR CARRIAGE RETURN */
1148 	NOTYET, NOTYET,
1149 /* 1 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1150 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1151 /* 2 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1152 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1153 /* 3 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1154 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
1155 /* 4 */	0x03c1, /* GREEK SMALL LETTER RHO */
1156 	0x03c8, /* GREEK SMALL LETTER PSI */
1157 	0x2202, /* PARTIAL DIFFERENTIAL */
1158 	0x03bb, /* GREEK SMALL LETTER LAMDA */
1159 	0x03b9, /* GREEK SMALL LETTER IOTA */
1160 	0x03b7, /* GREEK SMALL LETTER ETA */
1161 	0x03b5, /* GREEK SMALL LETTER EPSILON */
1162 	0x03c7, /* GREEK SMALL LETTER CHI */
1163 	0x2228, /* LOGICAL OR */
1164 	0x2227, /* LOGICAL AND */
1165 	0x222a, /* UNION */
1166 	0x2283, /* SUPERSET OF */
1167 	0x2282, /* SUBSET OF */
1168 	0x03a5, /* GREEK CAPITAL LETTER UPSILON */
1169 	0x039e, /* GREEK CAPITAL LETTER XI */
1170 	0x03a8, /* GREEK CAPITAL LETTER PSI */
1171 /* 5 */	0x03a0, /* GREEK CAPITAL LETTER PI */
1172 	0x21d2, /* RIGHTWARDS DOUBLE ARROW */
1173 	0x21d4, /* LEFT RIGHT DOUBLE ARROW */
1174 	0x039b, /* GREEK CAPITAL LETTER LAMDA */
1175 	0x0398, /* GREEK CAPITAL LETTER THETA */
1176 	0x2243, /* ASYMPTOTICALLY EQUAL TO */
1177 	0x2207, /* NABLA */
1178 	0x2206, /* INCREMENT */
1179 	0x221d, /* PROPORTIONAL TO */
1180 	0x2234, /* THEREFORE */
1181 	0x222b, /* INTEGRAL */
1182 	0x2215, /* DIVISION SLASH */
1183 	0x2216, /* SET MINUS */
1184 	_e00eU, /* angle? */
1185 	_e00dU, /* inverted angle? */
1186 	_e00bU, /* braceleftmid */
1187 /* 6 */	_e00cU, /* bracerightmid */
1188 	_e007U, /* bracelefttp */
1189 	_e008U, /* braceleftbt */
1190 	_e009U, /* bracerighttp */
1191 	_e00aU, /* bracerightbt */
1192 	0x221a, /* SQUARE ROOT */
1193 	0x03c9, /* GREEK SMALL LETTER OMEGA */
1194 	0x00a5, /* YEN SIGN */
1195 	0x03be, /* GREEK SMALL LETTER XI */
1196 	0x00fd, /* LATIN SMALL LETTER Y WITH ACUTE */
1197 	0x00fe, /* LATIN SMALL LETTER THORN */
1198 	0x00f0, /* LATIN SMALL LETTER ETH */
1199 	0x00de, /* LATIN CAPITAL LETTER THORN */
1200 	0x00dd, /* LATIN CAPITAL LETTER Y WITH ACUTE */
1201 	0x00d7, /* MULTIPLICATION SIGN */
1202 	0x00d0, /* LATIN CAPITAL LETTER ETH */
1203 /* 7 */	0x00be, /* VULGAR FRACTION THREE QUARTERS */
1204 	0x00b8, /* CEDILLA */
1205 	0x00b4, /* ACUTE ACCENT */
1206 	0x00af, /* MACRON */
1207 	0x00ae, /* REGISTERED SIGN */
1208 	0x00ad, /* SOFT HYPHEN */
1209 	0x00ac, /* NOT SIGN */
1210 	0x00a8, /* DIAERESIS */
1211 	0x2260, /* NOT EQUAL TO */
1212 	_e005U, /* scan 9 */
1213 	_e004U, /* scan 7 */
1214 	_e003U, /* scan 5 */
1215 	_e002U, /* scan 3 */
1216 	_e001U, /* scan 1 */
1217 	0x03c5, /* GREEK SMALL LETTER UPSILON */
1218 	0x00f8, /* LATIN SMALL LETTER O WITH STROKE */
1219 /* 8 */	0x0153, /* LATIN SMALL LIGATURE OE */
1220 	0x00f5, /* LATIN SMALL LETTER O WITH TILDE !!!doc bug */
1221 	0x00e3, /* LATIN SMALL LETTER A WITH TILDE */
1222 	0x0178, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
1223 	0x00db, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
1224 	0x00da, /* LATIN CAPITAL LETTER U WITH ACUTE */
1225 	0x00d9, /* LATIN CAPITAL LETTER U WITH GRAVE */
1226 	0x00d8, /* LATIN CAPITAL LETTER O WITH STROKE */
1227 	0x0152, /* LATIN CAPITAL LIGATURE OE */
1228 	0x00d5, /* LATIN CAPITAL LETTER O WITH TILDE */
1229 	0x00d4, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
1230 	0x00d3, /* LATIN CAPITAL LETTER O WITH ACUTE */
1231 	0x00d2, /* LATIN CAPITAL LETTER O WITH GRAVE */
1232 	0x00cf, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
1233 	0x00ce, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
1234 	0x00cd, /* LATIN CAPITAL LETTER I WITH ACUTE */
1235 /* 9 */	0x00cc, /* LATIN CAPITAL LETTER I WITH GRAVE */
1236 	0x00cb, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
1237 	0x00ca, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
1238 	0x00c8, /* LATIN CAPITAL LETTER E WITH GRAVE */
1239 	0x00c3, /* LATIN CAPITAL LETTER A WITH TILDE */
1240 	0x00c2, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
1241 	0x00c1, /* LATIN CAPITAL LETTER A WITH ACUTE */
1242 	0x00c0, /* LATIN CAPITAL LETTER A WITH GRAVE */
1243 	0x00b9, /* SUPERSCRIPT ONE */
1244 	0x00b7, /* MIDDLE DOT */
1245 	0x03b6, /* GREEK SMALL LETTER ZETA */
1246 	0x00b3, /* SUPERSCRIPT THREE */
1247 	0x00a9, /* COPYRIGHT SIGN */
1248 	0x00a4, /* CURRENCY SIGN */
1249 	0x03ba, /* GREEK SMALL LETTER KAPPA */
1250 	_e000U  /* mirrored question mark? */
1251 };
1252 
1253 static int vga_pcvt_mapchar(int, u_int *);
1254 
1255 static int
1256 vga_pcvt_mapchar(int uni, u_int *index)
1257 {
1258 	int i;
1259 
1260 	for (i = 0; i < 0xa0; i++) /* 0xa0..0xff are reserved */
1261 		if (uni == pcvt_unichars[i]) {
1262 			*index = i;
1263 			return (5);
1264 		}
1265 	*index = 0x99; /* middle dot */
1266 	return (0);
1267 }
1268 
1269 #endif /* WSCONS_SUPPORT_PCVTFONTS */
1270 
1271 #ifdef WSCONS_SUPPORT_ISO7FONTS
1272 
1273 static int
1274 vga_iso7_mapchar(int uni, u_int *index)
1275 {
1276 
1277 	/*
1278 	 * U+0384 (GREEK TONOS) to
1279 	 * U+03ce (GREEK SMALL LETTER OMEGA WITH TONOS)
1280 	 * map directly to the iso-9 font
1281 	 */
1282 	if (uni >= 0x0384 && uni <= 0x03ce) {
1283 		/* U+0384 is at offset 0xb4 in the font */
1284 		*index = uni - 0x0384 + 0xb4;
1285 		return (5);
1286 	}
1287 
1288 	/* XXX more chars in the iso-9 font */
1289 
1290 	*index = 0xa4; /* shaded rectangle */
1291 	return (0);
1292 }
1293 
1294 #endif /* WSCONS_SUPPORT_ISO7FONTS */
1295 
1296 static int _vga_mapchar(void *, const struct egavga_font *, int, u_int *);
1297 
1298 static int
1299 _vga_mapchar(void *id, const struct egavga_font *font, int uni, u_int *index)
1300 {
1301 
1302 	switch (font->wsfont->encoding) {
1303 	case WSDISPLAY_FONTENC_ISO:
1304 		if (uni < 256) {
1305 			*index = uni;
1306 			return (5);
1307 		} else {
1308 			*index = ' ';
1309 			return (0);
1310 		}
1311 	case WSDISPLAY_FONTENC_IBM:
1312 		return (pcdisplay_mapchar(id, uni, index));
1313 #ifdef WSCONS_SUPPORT_PCVTFONTS
1314 	case WSDISPLAY_FONTENC_PCVT:
1315 		return (vga_pcvt_mapchar(uni, index));
1316 #endif
1317 #ifdef WSCONS_SUPPORT_ISO7FONTS
1318 	case WSDISPLAY_FONTENC_ISO7:
1319 		return (vga_iso7_mapchar(uni, index));
1320 #endif
1321 	default:
1322 #ifdef VGAFONTDEBUG
1323 		printf("_vga_mapchar: encoding=%d\n", font->wsfont->encoding);
1324 #endif
1325 		*index = ' ';
1326 		return (0);
1327 	}
1328 }
1329 
1330 static int
1331 vga_mapchar(void *id, int uni, u_int *index)
1332 {
1333 	struct vgascreen *scr = id;
1334 	u_int idx1, idx2;
1335 	int res1, res2;
1336 
1337 	res1 = 0;
1338 	idx1 = ' '; /* space */
1339 	if (scr->fontset1)
1340 		res1 = _vga_mapchar(id, scr->fontset1, uni, &idx1);
1341 	res2 = -1;
1342 	if (scr->fontset2) {
1343 		KASSERT(VGA_SCREEN_CANTWOFONTS(scr->pcs.type));
1344 		res2 = _vga_mapchar(id, scr->fontset2, uni, &idx2);
1345 	}
1346 	if (res2 > res1) {
1347 		*index = idx2 | 0x0800; /* attribute bit 3 */
1348 		return (res2);
1349 	}
1350 	*index = idx1;
1351 	return (res1);
1352 }
1353 
1354 #ifdef WSDISPLAY_SCROLLSUPPORT
1355 void
1356 vga_scroll(void *v, void *cookie, int lines)
1357 {
1358 	struct vga_config *vc = v;
1359 	struct vgascreen *scr = cookie;
1360 	struct vga_handle *vh = &vc->hdl;
1361 
1362 	if (lines == 0) {
1363 		if (scr->pcs.visibleoffset == scr->pcs.dispoffset)
1364 			return;
1365 
1366 		scr->pcs.visibleoffset = scr->pcs.dispoffset;
1367 	}
1368 	else {
1369 		int vga_scr_end;
1370 		int margin = scr->pcs.type->ncols * 2;
1371 		int ul, we, p, st;
1372 
1373 		vga_scr_end = (scr->pcs.dispoffset + scr->pcs.type->ncols *
1374 		    scr->pcs.type->nrows * 2);
1375 		if (scr->vga_rollover > vga_scr_end + margin) {
1376 			ul = vga_scr_end;
1377 			we = scr->vga_rollover + scr->pcs.type->ncols * 2;
1378 		} else {
1379 			ul = 0;
1380 			we = 0x8000;
1381 		}
1382 		p = (scr->pcs.visibleoffset - ul + we) % we + lines *
1383 		    (scr->pcs.type->ncols * 2);
1384 		st = (scr->pcs.dispoffset - ul + we) % we;
1385 		if (p < margin)
1386 			p = 0;
1387 		if (p > st - margin)
1388 			p = st;
1389 		scr->pcs.visibleoffset = (p + ul) % we;
1390 	}
1391 
1392 	vga_6845_write(vh, startadrh, scr->pcs.visibleoffset >> 9);
1393 	vga_6845_write(vh, startadrl, scr->pcs.visibleoffset >> 1);
1394 }
1395 #endif
1396 
1397 void
1398 vga_putchar(void *c, int row, int col, u_int uc, long attr)
1399 {
1400 
1401 	pcdisplay_putchar(c, row, col, uc, attr);
1402 }
1403 
1404 
1405 #ifdef WSDISPLAY_CHARFUNCS
1406 int
1407 vga_getwschar(void *cookie, struct wsdisplay_char *wschar)
1408 {
1409 	struct vgascreen *scr = cookie;
1410 
1411 	if (scr == NULL) return 0;
1412 	return (pcdisplay_getwschar(&scr->pcs, wschar));
1413 }
1414 
1415 int
1416 vga_putwschar(void *cookie, struct wsdisplay_char *wschar)
1417 {
1418 	struct vgascreen *scr = cookie;
1419 
1420 	if (scr == NULL) return 0;
1421 	return (pcdisplay_putwschar(&scr->pcs, wschar));
1422 }
1423 #endif /* WSDISPLAY_CHARFUNCS */
1424 
1425 #ifdef WSDISPLAY_CUSTOM_BORDER
1426 static u_int
1427 vga_getborder(void *cookie)
1428 {
1429 	struct vgascreen *scr = cookie;
1430 	struct vga_handle *vh;
1431 	u_int idx;
1432 	u_int8_t value;
1433 
1434 	if (scr == NULL) return EINVAL;
1435 	vh = &scr->cfg->hdl;
1436 	if (vh->vh_mono) return ENODEV;
1437 
1438 	value = _vga_attr_read(vh, VGA_ATC_OVERSCAN);
1439 	for (idx = 0; idx < sizeof(fgansitopc); idx++)
1440 		if (fgansitopc[idx] == value)
1441 			break;
1442 	return idx == sizeof(fgansitopc) ? 0 : idx;
1443 }
1444 
1445 static int
1446 vga_setborder(void *cookie, u_int value)
1447 {
1448 	struct vgascreen *scr = cookie;
1449 	struct vga_handle *vh;
1450 
1451 	if (scr == NULL) return EINVAL;
1452 	vh = &scr->cfg->hdl;
1453 	if (vh->vh_mono) return ENODEV;
1454 	if (value >= sizeof(fgansitopc)) return EINVAL;
1455 
1456 	_vga_attr_write(vh, VGA_ATC_OVERSCAN, fgansitopc[value]);
1457 	return (0);
1458 }
1459 #endif /* WSDISPLAY_CUSTOM_BORDER */
1460