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