xref: /netbsd-src/sys/dev/ic/vga_raster.c (revision 230b95665bbd3a9d1a53658a36b1053f8382a519)
1 /*	$NetBSD: vga_raster.c,v 1.42 2014/08/21 13:52:22 macallan Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 Bang Jun-Young
5  * Copyright (c) 2004 Julio M. Merino Vidal
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
33  * All rights reserved.
34  *
35  * Author: Chris G. Demetriou
36  *
37  * Permission to use, copy, modify and distribute this software and
38  * its documentation is hereby granted, provided that both the copyright
39  * notice and this permission notice appear in all copies of the
40  * software, derivative works or modified versions, and any portions
41  * thereof, and that both notices appear in supporting documentation.
42  *
43  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
44  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
45  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46  *
47  * Carnegie Mellon requests users of this software to return to
48  *
49  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
50  *  School of Computer Science
51  *  Carnegie Mellon University
52  *  Pittsburgh PA 15213-3890
53  *
54  * any improvements or extensions that they make and grant Carnegie the
55  * rights to redistribute these changes.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: vga_raster.c,v 1.42 2014/08/21 13:52:22 macallan Exp $");
60 
61 #include "opt_vga.h"
62 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/callout.h>
67 #include <sys/kernel.h>
68 #include <sys/device.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/bus.h>
72 
73 #include <dev/ic/mc6845reg.h>
74 #include <dev/ic/pcdisplayvar.h>
75 #include <dev/ic/vgareg.h>
76 #include <dev/ic/vgavar.h>
77 #include <dev/videomode/videomode.h>
78 
79 #include <dev/wscons/wsdisplayvar.h>
80 #include <dev/wscons/wsconsio.h>
81 #include <dev/wsfont/wsfont.h>
82 
83 #include <dev/ic/pcdisplay.h>
84 
85 int vga_no_builtinfont = 0;
86 
87 u_int8_t builtinfont_data[256 * 16];
88 
89 struct wsdisplay_font builtinfont = {
90 	"builtin",			/* typeface name */
91 	0,				/* firstchar */
92 	256,				/* numchars */
93 	WSDISPLAY_FONTENC_IBM,		/* encoding */
94 	8,				/* width */
95 	16,				/* height */
96 	1,				/* stride */
97 	WSDISPLAY_FONTORDER_L2R,	/* bit order */
98 	WSDISPLAY_FONTORDER_L2R,	/* byte order */
99 	builtinfont_data		/* data */
100 };
101 
102 struct vga_scrmem {
103 	u_int16_t ch;
104 	u_int8_t attr;
105 	u_int8_t second;	/* XXXBJY should be u_int8_t len; */
106 	u_int8_t enc;
107 };
108 
109 #ifdef VGA_CONSOLE_SCREENTYPE
110 #define VGA_SCRMEM_SIZE		(80 * 30)
111 #else
112 #define VGA_SCRMEM_SIZE		(80 * 25)
113 #endif
114 
115 struct vga_scrmem boot_scrmem[VGA_SCRMEM_SIZE];
116 
117 struct vga_raster_font {
118 	LIST_ENTRY(vga_raster_font) next;
119 	struct wsdisplay_font *font;
120 };
121 
122 struct vgascreen {
123 	LIST_ENTRY(vgascreen) next;
124 	struct vga_config *cfg;
125 	struct vga_handle *hdl;
126 	const struct wsscreen_descr *type;
127 
128 	int active;
129 	struct vga_scrmem *mem;
130 	int encoding;
131 
132 	int dispoffset;
133 	int mindispoffset;
134 	int maxdispoffset;
135 
136 	int cursoron;			/* Is cursor displayed? */
137 	int cursorcol;			/* Current cursor column */
138 	int cursorrow;			/* Current cursor row */
139 	struct vga_scrmem cursortmp;
140 	int cursorstride;
141 
142 	LIST_HEAD(, vga_raster_font) fontset;
143 };
144 
145 struct vga_moderegs {
146 	u_int8_t miscout;		/* Misc. output */
147 	u_int8_t crtc[MC6845_NREGS];	/* CRTC controller */
148 	u_int8_t atc[VGA_ATC_NREGS];	/* Attribute controller */
149 	u_int8_t ts[VGA_TS_NREGS];	/* Time sequencer */
150 	u_int8_t gdc[VGA_GDC_NREGS];	/* Graphics display controller */
151 };
152 
153 static int vgaconsole, vga_console_type, vga_console_attached;
154 static struct vgascreen vga_console_screen;
155 static struct vga_config vga_console_vc;
156 static struct vga_raster_font vga_console_fontset_ascii;
157 static struct videomode vga_console_modes[2] = {
158 	/* 640x400 for 80x25, 80x40 and 80x50 modes */
159 	{
160 		25175, 640, 664, 760, 800, 400, 409, 411, 450, 0, NULL,
161 	},
162 	/* 640x480 for 80x30 mode */
163 	{
164 		25175, 640, 664, 760, 800, 480, 491, 493, 525, 0, NULL,
165 	}
166 };
167 
168 static void vga_raster_init(struct vga_config *, bus_space_tag_t,
169 		bus_space_tag_t);
170 static void vga_raster_init_screen(struct vga_config *, struct vgascreen *,
171 		const struct wsscreen_descr *, int, long *);
172 static void vga_raster_setup_font(struct vga_config *, struct vgascreen *);
173 static void vga_setup_regs(struct videomode *, struct vga_moderegs *);
174 static void vga_set_mode(struct vga_handle *, struct vga_moderegs *);
175 static void vga_restore_screen(struct vgascreen *,
176 		const struct wsscreen_descr *, struct vga_scrmem *);
177 static void vga_raster_cursor_init(struct vgascreen *, int);
178 static void _vga_raster_putchar(void *, int, int, u_int, long,
179 		struct vga_raster_font *);
180 
181 static void vga_raster_cursor(void *, int, int, int);
182 static int  vga_raster_mapchar(void *, int, u_int *);
183 static void vga_raster_putchar(void *, int, int, u_int, long);
184 static void vga_raster_copycols(void *, int, int, int, int);
185 static void vga_raster_erasecols(void *, int, int, int, long);
186 static void vga_raster_copyrows(void *, int, int, int);
187 static void vga_raster_eraserows(void *, int, int, long);
188 static int  vga_raster_allocattr(void *, int, int, int, long *);
189 #ifdef WSDISPLAY_CUSTOM_OUTPUT
190 static void vga_raster_replaceattr(void *, long, long);
191 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
192 
193 const struct wsdisplay_emulops vga_raster_emulops = {
194 	vga_raster_cursor,
195 	vga_raster_mapchar,
196 	vga_raster_putchar,
197 	vga_raster_copycols,
198 	vga_raster_erasecols,
199 	vga_raster_copyrows,
200 	vga_raster_eraserows,
201 	vga_raster_allocattr,
202 #ifdef WSDISPLAY_CUSTOM_OUTPUT
203 	vga_raster_replaceattr,
204 #else /* WSDISPLAY_CUSTOM_OUTPUT */
205 	NULL,
206 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
207 };
208 
209 /*
210  * translate WS(=ANSI) color codes to standard pc ones
211  */
212 static const unsigned char fgansitopc[] = {
213 #ifdef __alpha__
214 	/*
215 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
216 	 * XXX We should probably not bother with this
217 	 * XXX (reinitialize the palette registers).
218 	 */
219 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
220 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
221 #else
222 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
223 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
224 #endif
225 }, bgansitopc[] = {
226 #ifdef __alpha__
227 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
228 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
229 #else
230 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
231 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
232 #endif
233 };
234 
235 const struct wsscreen_descr vga_25lscreen = {
236 	"80x25", 80, 25,
237 	&vga_raster_emulops,
238 	8, 16,
239 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
240 	&vga_console_modes[0]
241 }, vga_25lscreen_mono = {
242 	"80x25", 80, 25,
243 	&vga_raster_emulops,
244 	8, 16,
245 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
246 	&vga_console_modes[0]
247 }, vga_30lscreen = {
248 	"80x30", 80, 30,
249 	&vga_raster_emulops,
250 	8, 16,
251 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
252 	&vga_console_modes[1]
253 }, vga_30lscreen_mono = {
254 	"80x30", 80, 30,
255 	&vga_raster_emulops,
256 	8, 16,
257 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
258 	&vga_console_modes[1]
259 }, vga_40lscreen = {
260 	"80x40", 80, 40,
261 	&vga_raster_emulops,
262 	8, 10,
263 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
264 	&vga_console_modes[0]
265 }, vga_40lscreen_mono = {
266 	"80x40", 80, 40,
267 	&vga_raster_emulops,
268 	8, 10,
269 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
270 	&vga_console_modes[0]
271 }, vga_50lscreen = {
272 	"80x50", 80, 50,
273 	&vga_raster_emulops,
274 	8, 8,
275 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
276 	&vga_console_modes[0]
277 }, vga_50lscreen_mono = {
278 	"80x50", 80, 50,
279 	&vga_raster_emulops,
280 	8, 8,
281 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
282 	&vga_console_modes[0]
283 };
284 
285 const struct wsscreen_descr *_vga_scrlist[] = {
286 	&vga_25lscreen,
287 	&vga_30lscreen,
288 	&vga_40lscreen,
289 	&vga_50lscreen,
290 }, *_vga_scrlist_mono[] = {
291 	&vga_25lscreen_mono,
292 	&vga_30lscreen_mono,
293 	&vga_40lscreen_mono,
294 	&vga_50lscreen_mono,
295 };
296 
297 const struct wsscreen_list vga_screenlist = {
298 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
299 	_vga_scrlist
300 }, vga_screenlist_mono = {
301 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
302 	_vga_scrlist_mono
303 };
304 
305 static int	vga_raster_ioctl(void *, void *, u_long, void *, int,
306 		    struct lwp *);
307 static paddr_t	vga_raster_mmap(void *, void *, off_t, int);
308 static int	vga_raster_alloc_screen(void *, const struct wsscreen_descr *,
309 		    void **, int *, int *, long *);
310 static void	vga_raster_free_screen(void *, void *);
311 static int	vga_raster_show_screen(void *, void *, int,
312 		    void (*)(void *, int, int), void *);
313 static int	vga_raster_load_font(void *, void *, struct wsdisplay_font *);
314 
315 static void 	vga_switch_screen(struct vga_config *);
316 static void 	vga_raster_setscreentype(struct vga_config *,
317 		    const struct wsscreen_descr *);
318 
319 const struct wsdisplay_accessops vga_raster_accessops = {
320 	vga_raster_ioctl,
321 	vga_raster_mmap,
322 	vga_raster_alloc_screen,
323 	vga_raster_free_screen,
324 	vga_raster_show_screen,
325 	vga_raster_load_font,
326 	NULL,	/* pollc */
327 	NULL,	/* scroll */
328 };
329 
330 int
331 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
332 {
333 	long defattr;
334 	const struct wsscreen_descr *scr;
335 #ifdef VGA_CONSOLE_SCREENTYPE
336 	const char *typestr = NULL;
337 #endif
338 
339 	if (check && !vga_common_probe(iot, memt))
340 		return (ENXIO);
341 
342 	/* set up bus-independent VGA configuration */
343 	vga_raster_init(&vga_console_vc, iot, memt);
344 #ifdef VGA_CONSOLE_SCREENTYPE
345 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
346 	    &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
347 	if (!scr)
348 		/* Invalid screen type, continue with the default mode. */
349 		typestr = "80x25";
350 	else if (scr->nrows > 30)
351 		/* Unsupported screen type, try 80x30. */
352 		typestr = "80x30";
353 	if (typestr)
354 		scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
355 		    &vga_screenlist_mono : &vga_screenlist, typestr);
356 	if (scr != vga_console_vc.currenttype)
357 		vga_console_vc.currenttype = scr;
358 #else
359 	scr = vga_console_vc.currenttype;
360 #endif
361 	vga_raster_init_screen(&vga_console_vc, &vga_console_screen, scr, 1,
362 	    &defattr);
363 
364 	vga_console_screen.active = 1;
365 	vga_console_vc.active = &vga_console_screen;
366 
367 	wsdisplay_cnattach(scr, &vga_console_screen,
368 	    vga_console_screen.cursorcol, vga_console_screen.cursorrow,
369 	    defattr);
370 
371 	vgaconsole = 1;
372 	vga_console_type = type;
373 	return (0);
374 }
375 
376 static void
377 vga_raster_init(struct vga_config *vc, bus_space_tag_t iot,
378     bus_space_tag_t memt)
379 {
380 	struct vga_handle *vh = &vc->hdl;
381 	u_int8_t mor;
382 	struct vga_raster_font *vf;
383 
384 	vh->vh_iot = iot;
385 	vh->vh_memt = memt;
386 
387 	if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
388 		panic("vga_raster_init: couldn't map vga io");
389 
390 	/* read "misc output register" */
391 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAR);
392 	vh->vh_mono = !(mor & 1);
393 
394 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
395 	    &vh->vh_ioh_6845))
396 		panic("vga_raster_init: couldn't map 6845 io");
397 
398 	if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
399 		panic("vga_raster_init: couldn't map memory");
400 
401 	if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
402 	    &vh->vh_memh))
403 		panic("vga_raster_init: mem subrange failed");
404 
405 #if 0
406 	/* should only reserve the space (no need to map - save KVM) */
407 	vc->vc_biostag = memt;
408 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0, &vc->vc_bioshdl))
409 		vc->vc_biosmapped = 0;
410 	else
411 		vc->vc_biosmapped = 1;
412 #endif
413 	vc->nscreens = 0;
414 	LIST_INIT(&vc->screens);
415 	vc->active = NULL;
416 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
417 	callout_init(&vc->vc_switch_callout, 0);
418 
419 	wsfont_init();
420 	vc->nfonts = 1;
421 	LIST_INIT(&vc->vc_fontlist);
422 	vf = &vga_console_fontset_ascii;
423 	if (vga_no_builtinfont) {
424 		struct wsdisplay_font *wf;
425 		int cookie;
426 
427 		/* prefer 8x16 pixel font */
428 		cookie = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
429 		    0, WSFONT_FIND_BITMAP);
430 		if (cookie == -1)
431 			cookie = wsfont_find(NULL, 0, 0, 0,
432 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
433 			    WSFONT_FIND_BITMAP);
434 		if (cookie == -1 || wsfont_lock(cookie, &wf))
435 			panic("vga_raster_init: can't load console font");
436 		vf->font = wf;
437 	} else {
438 		vga_load_builtinfont(vh, builtinfont_data, 0, 256);
439 		vf->font = &builtinfont;
440 	}
441 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
442 }
443 
444 static void
445 vga_raster_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 	struct vga_handle *vh;
451 
452 	scr->cfg = vc;
453 	scr->hdl = &vc->hdl;
454 	scr->type = type;
455 	scr->mindispoffset = 0;
456 	scr->maxdispoffset = scr->dispoffset +
457 	    type->nrows * type->ncols * type->fontheight;
458 	vh = &vc->hdl;
459 
460 	LIST_INIT(&scr->fontset);
461 	vga_raster_setup_font(vc, scr);
462 
463 	if (existing) {
464 		int i;
465 
466 		cpos = vga_6845_read(vh, cursorh) << 8;
467 		cpos |= vga_6845_read(vh, cursorl);
468 
469 		/* make sure we have a valid cursor position */
470 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
471 			cpos = 0;
472 
473 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
474 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
475 
476 		/* make sure we have a valid memory offset */
477 		if (scr->dispoffset < scr->mindispoffset ||
478 		    scr->dispoffset > scr->maxdispoffset)
479 			scr->dispoffset = scr->mindispoffset;
480 
481 		scr->mem = boot_scrmem;
482 		scr->active = 1;
483 
484 		/* Save the current screen to memory. XXXBJY assume 80x25 */
485 		for (i = 0; i < 80 * 25; i++) {
486 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
487 			    vh->vh_allmemh, 0x18000 + i * 2);
488 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
489 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
490 			scr->mem[i].enc = scr->encoding;
491 		}
492 
493 		vga_raster_setscreentype(vc, type);
494 
495 		/* Clear the entire screen. */
496 		vga_gdc_write(vh, mode, 0x02);
497 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
498 		    0x4000);
499 
500 		vga_restore_screen(scr, type, scr->mem);
501 	} else {
502 		cpos = 0;
503 		scr->dispoffset = scr->mindispoffset;
504 		scr->mem = NULL;
505 		scr->active = 0;
506 	}
507 
508 	scr->cursorrow = cpos / type->ncols;
509 	scr->cursorcol = cpos % type->ncols;
510 	vga_raster_cursor_init(scr, existing);
511 
512 #ifdef __alpha__
513 	if (!vc->hdl.vh_mono)
514 		/*
515 		 * DEC firmware uses a blue background.
516 		 */
517 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
518 		    WSATTR_WSCOLORS, attrp);
519 	else
520 #endif
521 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
522 #ifdef DIAGNOSTIC
523 	if (res)
524 		panic("vga_raster_init_screen: attribute botch");
525 #endif
526 
527 	vc->nscreens++;
528 	LIST_INSERT_HEAD(&vc->screens, scr, next);
529 }
530 
531 void
532 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
533     bus_space_tag_t memt, int type, int quirks,
534     const struct vga_funcs *vf)
535 {
536 	int console;
537 	struct vga_config *vc;
538 	struct wsemuldisplaydev_attach_args aa;
539 
540 	console = vga_is_console(iot, type);
541 
542 	if (console) {
543 		vc = &vga_console_vc;
544 		vga_console_attached = 1;
545 	} else {
546 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
547 		vga_raster_init(vc, iot, memt);
548 	}
549 
550 	vc->vc_type = type;
551 	vc->vc_funcs = vf;
552 
553 	sc->sc_vc = vc;
554 	vc->softc = sc;
555 
556 	aa.console = console;
557 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
558 	aa.accessops = &vga_raster_accessops;
559 	aa.accesscookie = vc;
560 
561 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
562 }
563 
564 int
565 vga_cndetach(void)
566 {
567 	struct vga_config *vc;
568 	struct vga_handle *vh;
569 
570 	vc = &vga_console_vc;
571 	vh = &vc->hdl;
572 
573 	if (vgaconsole) {
574 		wsdisplay_cndetach();
575 
576 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_vga, 0x10);
577 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_6845, 0x10);
578 
579 		vga_console_attached = 0;
580 		vgaconsole = 0;
581 
582 		return 1;
583 	}
584 
585 	return 0;
586 }
587 
588 int
589 vga_is_console(bus_space_tag_t iot, int type)
590 {
591 	if (vgaconsole &&
592 	    !vga_console_attached &&
593 	    iot == vga_console_vc.hdl.vh_iot &&
594 	    (vga_console_type == -1 || (type == vga_console_type)))
595 		return (1);
596 	return (0);
597 }
598 
599 static int
600 vga_get_video(struct vga_config *vc)
601 {
602 
603 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
604 }
605 
606 static void
607 vga_set_video(struct vga_config *vc, int state)
608 {
609 	int val;
610 
611 	vga_ts_write(&vc->hdl, syncreset, 0x01);
612 	if (state) {					/* unblank screen */
613 		val = vga_ts_read(&vc->hdl, mode);
614 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
615 #ifndef VGA_NO_VBLANK
616 		val = vga_6845_read(&vc->hdl, mode);
617 		vga_6845_write(&vc->hdl, mode, val | 0x80);
618 #endif
619 	} else {					/* blank screen */
620 		val = vga_ts_read(&vc->hdl, mode);
621 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
622 #ifndef VGA_NO_VBLANK
623 		val = vga_6845_read(&vc->hdl, mode);
624 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
625 #endif
626 	}
627 	vga_ts_write(&vc->hdl, syncreset, 0x03);
628 }
629 
630 int
631 vga_raster_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
632 	struct lwp *l)
633 {
634 	struct vga_config *vc = v;
635 	const struct vga_funcs *vf = vc->vc_funcs;
636 
637 	switch (cmd) {
638 	case WSDISPLAYIO_GTYPE:
639 		*(int *)data = vc->vc_type;
640 		return 0;
641 
642 	case WSDISPLAYIO_GINFO: {
643 		struct wsdisplay_fbinfo *fbi = data;
644 		const struct wsscreen_descr *wd = vc->currenttype;
645 		const struct videomode *vm = wd->modecookie;
646 		fbi->width = vm->hdisplay;
647 		fbi->height = vm->vdisplay;
648 		fbi->depth = 24;	/* xxx: ? */
649 		fbi->cmsize = 256;	/* xxx: from palette */
650 		return 0;
651 	}
652 
653 	case WSDISPLAYIO_GVIDEO:
654 		*(int *)data = (vga_get_video(vc) ?
655 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
656 		return 0;
657 
658 	case WSDISPLAYIO_SVIDEO:
659 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
660 		return 0;
661 
662 	case WSDISPLAYIO_GETCMAP:
663 	case WSDISPLAYIO_PUTCMAP:
664 	case WSDISPLAYIO_GCURPOS:
665 	case WSDISPLAYIO_SCURPOS:
666 	case WSDISPLAYIO_GCURMAX:
667 	case WSDISPLAYIO_GCURSOR:
668 	case WSDISPLAYIO_SCURSOR:
669 #ifdef DIAGNOSTIC
670 		printf("%s: 0x%lx unsupported\n", __func__, cmd);
671 #endif
672 		/* NONE of these operations are by the generic VGA driver. */
673 		return EPASSTHROUGH;
674 	}
675 
676 	if (vc->vc_funcs == NULL) {
677 #ifdef DIAGNOSTIC
678 		printf("%s: no vc_funcs\n", __func__);
679 #endif
680 		return EPASSTHROUGH;
681 	}
682 
683 	if (vf->vf_ioctl == NULL) {
684 #ifdef DIAGNOSTIC
685 		printf("%s: no vf_ioctl\n", __func__);
686 #endif
687 		return EPASSTHROUGH;
688 	}
689 
690 	return ((*vf->vf_ioctl)(v, cmd, data, flag, l));
691 }
692 
693 static paddr_t
694 vga_raster_mmap(void *v, void *vs, off_t offset, int prot)
695 {
696 	struct vga_config *vc = v;
697 	const struct vga_funcs *vf = vc->vc_funcs;
698 
699 	if (vc->vc_funcs == NULL)
700 		return (-1);
701 
702 	if (vf->vf_mmap == NULL)
703 		return (-1);
704 
705 	return ((*vf->vf_mmap)(v, offset, prot));
706 }
707 
708 static int
709 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
710     void **cookiep, int *curxp, int *curyp, long *defattrp)
711 {
712 	struct vga_config *vc = v;
713 	struct vgascreen *scr;
714 
715 	if (vc->nscreens == 1) {
716 		vc->screens.lh_first->mem = boot_scrmem;
717 	}
718 
719 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
720 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
721 
722 	if (vc->nscreens == 1) {
723 		scr->active = 1;
724 		vc->active = scr;
725 		vc->currenttype = type;
726 	} else {
727 		scr->mem = malloc(sizeof(struct vga_scrmem) *
728 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
729 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
730 	}
731 
732 	*cookiep = scr;
733 	*curxp = scr->cursorcol;
734 	*curyp = scr->cursorrow;
735 
736 	return (0);
737 }
738 
739 static void
740 vga_raster_free_screen(void *v, void *cookie)
741 {
742 	struct vgascreen *vs = cookie;
743 	struct vga_config *vc = vs->cfg;
744 
745 	LIST_REMOVE(vs, next);
746 	vc->nscreens--;
747 	if (vs != &vga_console_screen)
748 		free(vs, M_DEVBUF);
749 	else
750 		panic("vga_raster_free_screen: console");
751 
752 	if (vc->active == vs)
753 		vc->active = 0;
754 }
755 
756 static int
757 vga_raster_show_screen(void *v, void *cookie, int waitok,
758     void (*cb)(void *, int, int), void *cbarg)
759 {
760 	struct vgascreen *scr = cookie, *oldscr;
761 	struct vga_config *vc = scr->cfg;
762 
763 	oldscr = vc->active; /* can be NULL! */
764 	if (scr == oldscr) {
765 		return (0);
766 	}
767 
768 	vc->wantedscreen = cookie;
769 	vc->switchcb = cb;
770 	vc->switchcbarg = cbarg;
771 	if (cb) {
772 		callout_reset(&vc->vc_switch_callout, 0,
773 		    (void(*)(void *))vga_switch_screen, vc);
774 		return (EAGAIN);
775 	}
776 
777 	vga_switch_screen(vc);
778 	return (0);
779 }
780 
781 static void
782 vga_switch_screen(struct vga_config *vc)
783 {
784 	struct vgascreen *scr, *oldscr;
785 	struct vga_handle *vh = &vc->hdl;
786 	const struct wsscreen_descr *type;
787 
788 	scr = vc->wantedscreen;
789 	if (!scr) {
790 		printf("vga_switch_screen: disappeared\n");
791 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
792 		return;
793 	}
794 	type = scr->type;
795 	oldscr = vc->active; /* can be NULL! */
796 #ifdef DIAGNOSTIC
797 	if (oldscr) {
798 		if (!oldscr->active)
799 			panic("vga_raster_show_screen: not active");
800 		if (oldscr->type != vc->currenttype)
801 			panic("vga_raster_show_screen: bad type");
802 	}
803 #endif
804 	if (scr == oldscr) {
805 		return;
806 	}
807 #ifdef DIAGNOSTIC
808 	if (scr->active)
809 		panic("vga_raster_show_screen: active");
810 #endif
811 
812 	if (oldscr)
813 		oldscr->active = 0;
814 
815 	if (vc->currenttype != type) {
816 		vga_raster_setscreentype(vc, type);
817 		vc->currenttype = type;
818 	}
819 
820 	scr->dispoffset = scr->mindispoffset;
821 
822 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
823 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
824 		vga_6845_write(vh, startadrl, scr->dispoffset);
825 	}
826 
827 	/* Clear the entire screen. */
828 	vga_gdc_write(vh, mode, 0x02);
829 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
830 
831 	scr->active = 1;
832 	vga_restore_screen(scr, type, scr->mem);
833 
834 	vc->active = scr;
835 
836 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
837 
838 	vc->wantedscreen = 0;
839 	if (vc->switchcb)
840 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
841 }
842 
843 static int
844 vga_raster_load_font(void *v, void *id,
845     struct wsdisplay_font *data)
846 {
847 	/* XXX */
848 	printf("vga_raster_load_font: called\n");
849 
850 	return (0);
851 }
852 
853 static void
854 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
855 {
856 	struct vga_raster_font *vf;
857 	struct wsdisplay_font *wf;
858 	int cookie;
859 
860 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
861 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0,
862 		    WSFONT_FIND_BITMAP)) {
863 			scr->encoding = vf->font->encoding;
864 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
865 			return;
866 		}
867 	}
868 
869 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
870 	    WSDISPLAY_FONTORDER_L2R, 0, WSFONT_FIND_BITMAP);
871 	if (cookie == -1)
872 		return;
873 
874 	if (wsfont_lock(cookie, &wf))
875 		return;
876 
877 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_NOWAIT);
878 	if (!vf) {
879 		wsfont_unlock(cookie);
880 		return;
881 	}
882 
883 	vf->font = wf;
884 	scr->encoding = vf->font->encoding;
885 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
886 }
887 
888 static void
889 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
890 {
891 	int i;
892 	int depth = 4;			/* XXXBJY hardcoded for now */
893 	const u_int8_t palette[] = {
894 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
895 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
896 	};
897 
898 	/*
899 	 * Compute hsync and vsync polarity.
900 	 */
901 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
902 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
903 	    	regs->miscout = 0x23;
904 		if (mode->flags & VID_NHSYNC)
905 			regs->miscout |= 0x40;
906 		if (mode->flags & VID_NVSYNC)
907 			regs->miscout |= 0x80;
908 	} else {
909 		if (mode->flags & VID_DBLSCAN)
910 			mode->vdisplay *= 2;
911 		if (mode->vdisplay < 400)
912 			regs->miscout = 0xa3;
913 		else if (mode->vdisplay < 480)
914 			regs->miscout = 0x63;
915 		else if (mode->vdisplay < 768)
916 			regs->miscout = 0xe3;
917 		else
918 			regs->miscout = 0x23;
919 	}
920 
921 	/*
922 	 * Time sequencer
923 	 */
924 	if (depth == 4)
925 		regs->ts[0] = 0x02;
926 	else
927 		regs->ts[0] = 0x00;
928 	if (mode->flags & VID_CLKDIV2)
929 		regs->ts[1] = 0x09;
930 	else
931 		regs->ts[1] = 0x01;
932 	regs->ts[2] = 0x0f;
933 	regs->ts[3] = 0x00;
934 	if (depth < 8)
935 		regs->ts[4] = 0x06;
936 	else
937 		regs->ts[4] = 0x0e;
938 
939 	/*
940 	 * CRTC controller
941 	 */
942 	regs->crtc[0] = (mode->htotal >> 3) - 5;
943 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
944 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
945 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
946 	regs->crtc[4] = mode->hsync_start >> 3;
947 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
948 	    | (((mode->hsync_end >> 3)) & 0x1f);
949 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
950 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
951 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
952 	    | ((mode->vsync_start & 0x100) >> 6)
953 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
954 	    | 0x10
955 	    | (((mode->vtotal - 2) & 0x200) >> 4)
956 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
957 	    | ((mode->vsync_start & 0x200) >> 2);
958 	regs->crtc[8] = 0x00;
959 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
960 	if (mode->flags & VID_DBLSCAN)
961 		regs->crtc[9] |= 0x80;
962 	regs->crtc[10] = 0x00;
963 	regs->crtc[11] = 0x00;
964 	regs->crtc[12] = 0x00;
965 	regs->crtc[13] = 0x00;
966 	regs->crtc[14] = 0x00;
967 	regs->crtc[15] = 0x00;
968 	regs->crtc[16] = mode->vsync_start & 0xff;
969 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
970 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
971 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
972 	regs->crtc[20] = 0x00;
973 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
974 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
975 	if (depth < 8)
976 		regs->crtc[23] = 0xe3;
977 	else
978 		regs->crtc[23] = 0xc3;
979 	regs->crtc[24] = 0xff;
980 
981 	/*
982 	 * Graphics display controller
983 	 */
984 	regs->gdc[0] = 0x00;
985 	regs->gdc[1] = 0x00;
986 	regs->gdc[2] = 0x00;
987 	regs->gdc[3] = 0x00;
988 	regs->gdc[4] = 0x00;
989 	if (depth == 4)
990 		regs->gdc[5] = 0x02;
991 	else
992 		regs->gdc[5] = 0x40;
993 	regs->gdc[6] = 0x01;
994 	regs->gdc[7] = 0x0f;
995 	regs->gdc[8] = 0xff;
996 
997 	/*
998 	 * Attribute controller
999 	 */
1000 	/* Set palette registers. */
1001 	for (i = 0; i < 16; i++)
1002 		regs->atc[i] = palette[i];
1003 	if (depth == 4)
1004 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
1005 	else
1006 		regs->atc[16] = 0x41;
1007 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
1008 	regs->atc[18] = 0x0f;
1009 	regs->atc[19] = 0x00;
1010 	regs->atc[20] = 0x00;
1011 }
1012 
1013 static void
1014 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
1015 {
1016 	int i;
1017 
1018 	/* Disable display. */
1019 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
1020 
1021 	/* Write misc output register. */
1022 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
1023 	    regs->miscout);
1024 
1025 	/* Set synchronous reset. */
1026 	vga_ts_write(vh, syncreset, 0x01);
1027 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
1028 	for (i = 2; i < VGA_TS_NREGS; i++)
1029 		_vga_ts_write(vh, i, regs->ts[i]);
1030 	/* Clear synchronous reset. */
1031 	vga_ts_write(vh, syncreset, 0x03);
1032 
1033 	/* Unprotect CRTC registers 0-7. */
1034 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
1035 	/* Write CRTC registers. */
1036 	for (i = 0; i < MC6845_NREGS; i++)
1037 		_vga_6845_write(vh, i, regs->crtc[i]);
1038 
1039 	/* Write graphics display registers. */
1040 	for (i = 0; i < VGA_GDC_NREGS; i++)
1041 		_vga_gdc_write(vh, i, regs->gdc[i]);
1042 
1043 	/* Write attribute controller registers. */
1044 	for (i = 0; i < VGA_ATC_NREGS; i++)
1045 		_vga_attr_write(vh, i, regs->atc[i]);
1046 
1047 	/* Enable display. */
1048 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
1049 }
1050 
1051 static void
1052 vga_raster_cursor_init(struct vgascreen *scr, int existing)
1053 {
1054 	int off;
1055 
1056 	if (existing) {
1057 		/*
1058 		 * This is the first screen. At this point, scr->active is
1059 		 * false, so we can't use vga_raster_cursor() to do this.
1060 		 */
1061 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
1062 		    scr->dispoffset / 8;
1063 
1064 		scr->cursortmp = scr->mem[off];
1065 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
1066 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
1067 	} else {
1068 		scr->cursortmp.ch = 0;
1069 		scr->cursortmp.attr = 0;
1070 		scr->cursortmp.second = 0;
1071 		scr->cursortmp.enc = scr->encoding;
1072 	}
1073 
1074 	scr->cursoron = 1;
1075 }
1076 
1077 static void
1078 vga_raster_cursor(void *id, int on, int row, int col)
1079 {
1080 	struct vgascreen *scr = id;
1081 	int off, tmp;
1082 
1083 	/* Remove old cursor image */
1084 	if (scr->cursoron) {
1085 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1086 		if (scr->active) {
1087 			tmp = scr->encoding;
1088 			scr->encoding = scr->cursortmp.enc;
1089 			if (scr->cursortmp.second)
1090 				vga_raster_putchar(id, scr->cursorrow,
1091 				    scr->cursorcol - 1, scr->cursortmp.ch,
1092 				    scr->cursortmp.attr);
1093 			else
1094 				vga_raster_putchar(id, scr->cursorrow,
1095 				    scr->cursorcol, scr->cursortmp.ch,
1096 				    scr->cursortmp.attr);
1097 			scr->encoding = tmp;
1098 		}
1099 	}
1100 
1101 	scr->cursorrow = row;
1102 	scr->cursorcol = col;
1103 
1104 	if ((scr->cursoron = on) == 0)
1105 		return;
1106 
1107 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1108 	scr->cursortmp = scr->mem[off];
1109 	if (scr->active) {
1110 		tmp = scr->encoding;
1111 		scr->encoding = scr->cursortmp.enc;
1112 		if (scr->cursortmp.second)
1113 			vga_raster_putchar(id, scr->cursorrow,
1114 			    scr->cursorcol - 1, scr->cursortmp.ch,
1115 			    scr->cursortmp.attr ^ 0x77);
1116 		else
1117 			vga_raster_putchar(id, scr->cursorrow,
1118 			    scr->cursorcol, scr->cursortmp.ch,
1119 			    scr->cursortmp.attr ^ 0x77);
1120 		scr->encoding = tmp;
1121 	}
1122 }
1123 
1124 static int
1125 vga_raster_mapchar(void *id, int uni, u_int *index)
1126 {
1127 	struct vgascreen *scr = id;
1128 
1129 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
1130 		return pcdisplay_mapchar(id, uni, index);
1131 	else {
1132 		*index = uni;
1133 		return 5;
1134 	}
1135 }
1136 
1137 static void
1138 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
1139 {
1140 	struct vgascreen *scr = id;
1141 	size_t off;
1142 	struct vga_raster_font *fs;
1143 	u_int tmp_ch;
1144 
1145 	off = row * scr->type->ncols + col;
1146 
1147 	if (__predict_false(off >= (scr->type->ncols * scr->type->nrows)))
1148 		return;
1149 
1150 	LIST_FOREACH(fs, &scr->fontset, next) {
1151 		if ((scr->encoding == fs->font->encoding) &&
1152 		    (c >= fs->font->firstchar) &&
1153 		    (c < fs->font->firstchar + fs->font->numchars) &&
1154 		    (scr->type->fontheight == fs->font->fontheight)) {
1155 			if (scr->active) {
1156 				tmp_ch = c - fs->font->firstchar;
1157 				_vga_raster_putchar(scr, row, col, tmp_ch,
1158 				    attr, fs);
1159 			}
1160 
1161 			scr->mem[off].ch = c;
1162 			scr->mem[off].attr = attr;
1163 			scr->mem[off].second = 0;
1164 			scr->mem[off].enc = fs->font->encoding;
1165 
1166 			if (fs->font->stride == 2) {
1167 				scr->mem[off + 1].ch = c;
1168 				scr->mem[off + 1].attr = attr;
1169 				scr->mem[off + 1].second = 1;
1170 				scr->mem[off + 1].enc = fs->font->encoding;
1171 			}
1172 
1173 			return;
1174 		}
1175 	}
1176 
1177 	/*
1178 	 * No match found.
1179 	 */
1180 	if (scr->active)
1181 		/*
1182 		 * Put a single width space character no matter what the
1183 		 * actual width of the character is.
1184 		 */
1185 		_vga_raster_putchar(scr, row, col, ' ', attr,
1186 		    &vga_console_fontset_ascii);
1187 	scr->mem[off].ch = c;
1188 	scr->mem[off].attr = attr;
1189 	scr->mem[off].second = 0;
1190 	scr->mem[off].enc = scr->encoding;
1191 }
1192 
1193 static void
1194 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
1195     struct vga_raster_font *fs)
1196 {
1197 	struct vgascreen *scr = id;
1198 	struct vga_handle *vh = scr->hdl;
1199 	bus_space_tag_t memt = vh->vh_memt;
1200 	bus_space_handle_t memh = vh->vh_memh;
1201 	int i;
1202 	int rasoff, rasoff2;
1203 	int fheight = scr->type->fontheight;
1204 	volatile u_int8_t pattern;
1205 	u_int8_t fgcolor, bgcolor;
1206 
1207 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
1208 	rasoff2 = rasoff;
1209 
1210 #if 0
1211 	bgcolor = bgansitopc[attr >> 4];
1212 	fgcolor = fgansitopc[attr & 0x0f];
1213 #else
1214 	bgcolor = ((attr >> 4) & 0x0f);
1215 	fgcolor = attr & 0x0f;
1216 #endif
1217 
1218 	if (fs->font->stride == 1) {
1219 		/* Paint background. */
1220 		vga_gdc_write(vh, mode, 0x02);
1221 		for (i = 0; i < fheight; i++) {
1222 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1223 			rasoff += scr->type->ncols;
1224 		}
1225 
1226 		/* Draw a single width character. */
1227 		vga_gdc_write(vh, mode, 0x03);
1228 		vga_gdc_write(vh, setres, fgcolor);
1229 		for (i = 0; i < fheight; i++) {
1230 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
1231 			/* When pattern is 0, skip output for speed-up. */
1232 			if (pattern != 0) {
1233 				bus_space_read_1(memt, memh, rasoff2);
1234 				bus_space_write_1(memt, memh, rasoff2, pattern);
1235 			}
1236 			rasoff2 += scr->type->ncols;
1237 		}
1238 	} else if (fs->font->stride == 2) {
1239 		/* Paint background. */
1240 		vga_gdc_write(vh, mode, 0x02);
1241 		for (i = 0; i < fheight; i++) {
1242 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1243 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
1244 			rasoff += scr->type->ncols;
1245 		}
1246 
1247 		/* Draw a double width character. */
1248 		vga_gdc_write(vh, mode, 0x03);
1249 		vga_gdc_write(vh, setres, fgcolor);
1250 		for (i = 0; i < fheight; i++) {
1251 			pattern = ((u_int8_t *)fs->font->data)
1252 			    [(c * fheight + i) * 2];
1253 			if (pattern != 0) {
1254 				bus_space_read_1(memt, memh, rasoff2);
1255 				bus_space_write_1(memt, memh, rasoff2, pattern);
1256 			}
1257 			pattern = ((u_int8_t *)fs->font->data)
1258 			    [(c * fheight + i) * 2 + 1];
1259 			if (pattern != 0) {
1260 				rasoff2++;
1261 				bus_space_read_1(memt, memh, rasoff2);
1262 				bus_space_write_1(memt, memh, rasoff2, pattern);
1263 				rasoff2--;
1264 			}
1265 			rasoff2 += scr->type->ncols;
1266 		}
1267 	}
1268 }
1269 
1270 static void
1271 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
1272 {
1273 	struct vgascreen *scr = id;
1274 	struct vga_handle *vh = scr->hdl;
1275 	bus_space_tag_t memt = vh->vh_memt;
1276 	bus_space_handle_t memh = vh->vh_memh;
1277 	bus_size_t srcoff, dstoff;
1278 	bus_size_t rassrcoff, rasdstoff;
1279 	int i;
1280 	int fheight = scr->type->fontheight;
1281 
1282 	srcoff = row * scr->type->ncols + srccol;
1283 	dstoff = row * scr->type->ncols + dstcol;
1284 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
1285 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
1286 
1287 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1288 	    ncols * sizeof(struct vga_scrmem));
1289 
1290 	vga_gdc_write(vh, mode, 0x01);
1291 	if (scr->active) {
1292 		for (i = 0; i < fheight; i++) {
1293 			bus_space_copy_region_1(memt, memh,
1294 			    rassrcoff + i * scr->type->ncols, memh,
1295 			    rasdstoff + i * scr->type->ncols, ncols);
1296 		}
1297 	}
1298 }
1299 
1300 static void
1301 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
1302 {
1303 	struct vgascreen *scr = id;
1304 	int i;
1305 
1306 	if (scr->active == 0)
1307 		return;
1308 
1309 	for (i = startcol; i < startcol + ncols; i++)
1310 		vga_raster_putchar(id, row, i, ' ', fillattr);
1311 }
1312 
1313 static void
1314 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
1315 {
1316 	struct vgascreen *scr = id;
1317 	struct vga_handle *vh = scr->hdl;
1318 	bus_space_tag_t memt = vh->vh_memt;
1319 	bus_space_handle_t memh = vh->vh_memh;
1320 	int ncols;
1321 	bus_size_t srcoff, dstoff;
1322 	bus_size_t rassrcoff, rasdstoff;
1323 	int fheight;
1324 
1325 	ncols = scr->type->ncols;
1326 	fheight = scr->type->fontheight;
1327 
1328 	srcoff = srcrow * ncols;
1329 	dstoff = dstrow * ncols;
1330 	rassrcoff = srcoff * fheight;
1331 	rasdstoff = dstoff * fheight;
1332 
1333 	if (scr->active) {
1334 		vga_gdc_write(vh, mode, 0x01);
1335 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
1336 			int cursoron = scr->cursoron;
1337 
1338 			if (cursoron)
1339 				/* Disable cursor. */
1340 				vga_raster_cursor(scr, 0,
1341 				    scr->cursorrow, scr->cursorcol);
1342 
1343 			/* scroll up whole screen */
1344 			if ((scr->dispoffset + srcrow * ncols * fheight)
1345 			    <= scr->maxdispoffset)
1346 				scr->dispoffset += srcrow * ncols * fheight;
1347 			else {
1348 				bus_space_copy_region_1(memt, memh,
1349 				    scr->dispoffset + rassrcoff,
1350 				    memh, scr->mindispoffset,
1351 				    nrows * ncols * fheight);
1352 				scr->dispoffset = scr->mindispoffset;
1353 			}
1354 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
1355 			vga_6845_write(vh, startadrl, scr->dispoffset);
1356 
1357 			if (cursoron)
1358 				/* Enable cursor. */
1359 				vga_raster_cursor(scr, 1, scr->cursorrow,
1360 				    scr->cursorcol);
1361 		} else
1362 			bus_space_copy_region_1(memt, memh,
1363 			    scr->dispoffset + rassrcoff, memh,
1364 			    scr->dispoffset + rasdstoff,
1365 			    nrows * ncols * fheight);
1366 	}
1367 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1368 	    nrows * ncols * sizeof(struct vga_scrmem));
1369 }
1370 
1371 static void
1372 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
1373 {
1374 	struct vgascreen *scr = id;
1375 	struct vga_handle *vh = scr->hdl;
1376 	bus_space_tag_t memt = vh->vh_memt;
1377 	bus_space_handle_t memh = vh->vh_memh;
1378 	bus_size_t off, count;
1379 	bus_size_t rasoff, rascount;
1380 	int i;
1381 
1382 	off = startrow * scr->type->ncols;
1383 	count = nrows * scr->type->ncols;
1384 	rasoff = off * scr->type->fontheight;
1385 	rascount = count * scr->type->fontheight;
1386 
1387 	if (scr->active) {
1388 		u_int8_t bgcolor = (fillattr >> 4) & 0x0F;
1389 
1390 		/* Paint background. */
1391 		vga_gdc_write(vh, mode, 0x02);
1392 		if (scr->type->ncols % 4 == 0) {
1393 			u_int32_t fill = bgcolor | (bgcolor << 8) |
1394 			    (bgcolor << 16) | (bgcolor << 24);
1395 			/* We can speed up I/O */
1396 			for (i = rasoff; i < rasoff + rascount; i += 4)
1397 				bus_space_write_4(memt, memh,
1398 				    scr->dispoffset + i, fill);
1399 		} else {
1400 			u_int16_t fill = bgcolor | (bgcolor << 8);
1401 			for (i = rasoff; i < rasoff + rascount; i += 2)
1402 				bus_space_write_2(memt, memh,
1403 				    scr->dispoffset + i, fill);
1404 		}
1405 	}
1406 	for (i = 0; i < count; i++) {
1407 		scr->mem[off + i].ch = ' ';
1408 		scr->mem[off + i].attr = fillattr;
1409 		scr->mem[off + i].second = 0;
1410 		scr->mem[off + i].enc = scr->encoding;
1411 	}
1412 }
1413 
1414 static int
1415 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
1416 {
1417 	struct vgascreen *scr = id;
1418 	struct vga_config *vc = scr->cfg;
1419 
1420 	if (__predict_false((unsigned int)fg >= sizeof(fgansitopc) ||
1421 	    (unsigned int)bg >= sizeof(bgansitopc)))
1422 	    	return (EINVAL);
1423 
1424 	if (vc->hdl.vh_mono) {
1425 		if (flags & WSATTR_WSCOLORS)
1426 			return (EINVAL);
1427 		if (flags & WSATTR_REVERSE)
1428 			*attrp = 0x70;
1429 		else
1430 			*attrp = 0x07;
1431 		if (flags & WSATTR_UNDERLINE)
1432 			*attrp |= FG_UNDERLINE;
1433 		if (flags & WSATTR_HILIT)
1434 			*attrp |= FG_INTENSE;
1435 	} else {
1436 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
1437 			return (EINVAL);
1438 		if (flags & WSATTR_WSCOLORS)
1439 			*attrp = fgansitopc[fg] | bgansitopc[bg];
1440 		else
1441 			*attrp = 7;
1442 		if (flags & WSATTR_HILIT)
1443 			*attrp += 8;
1444 	}
1445 	if (flags & WSATTR_BLINK)
1446 		*attrp |= FG_BLINK;
1447 	return (0);
1448 }
1449 
1450 static void
1451 vga_restore_screen(struct vgascreen *scr,
1452     const struct wsscreen_descr *type, struct vga_scrmem *mem)
1453 {
1454 	int i, j, off, tmp;
1455 
1456 	tmp = scr->encoding;
1457 	for (i = 0; i < type->nrows; i++) {
1458 		for (j = 0; j < type->ncols; j++) {
1459 			off = i * type->ncols + j;
1460 			if (mem[off].second != 1) {
1461 				scr->encoding = mem[off].enc;
1462 				vga_raster_putchar(scr, i, j, mem[off].ch,
1463 				    mem[off].attr);
1464 			}
1465 		}
1466 	}
1467 	scr->encoding = tmp;
1468 }
1469 
1470 static void
1471 vga_raster_setscreentype(struct vga_config *vc,
1472     const struct wsscreen_descr *type)
1473 {
1474 	struct vga_handle *vh = &vc->hdl;
1475 	struct vga_moderegs moderegs;
1476 
1477 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
1478 	vga_set_mode(vh, &moderegs);
1479 }
1480 
1481 #ifdef WSDISPLAY_CUSTOM_OUTPUT
1482 static void
1483 vga_raster_replaceattr(void *id, long oldattr, long newattr)
1484 {
1485 	struct vgascreen *scr = id;
1486 	const struct wsscreen_descr *type = scr->type;
1487 	int off;
1488 
1489 	for (off = 0; off < type->nrows * type->ncols; off++) {
1490 		if (scr->mem[off].attr == oldattr)
1491 			scr->mem[off].attr = newattr;
1492 	}
1493 
1494 	/* Repaint the whole screen, if needed */
1495 	if (scr->active)
1496 		vga_restore_screen(scr, type, scr->mem);
1497 }
1498 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
1499 
1500 void
1501 vga_resume(struct vga_softc *sc)
1502 {
1503 #ifdef VGA_RESET_ON_RESUME
1504 	vga_initregs(&sc->sc_vc->hdl);
1505 #endif
1506 }
1507