xref: /netbsd-src/sys/dev/ic/vga_raster.c (revision f14316bcbc544b96a93e884bc5c2b15fd60e22ae)
1 /*	$NetBSD: vga_raster.c,v 1.41 2014/07/12 05:30:33 mlelstv 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.41 2014/07/12 05:30:33 mlelstv 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 	/* should only reserve the space (no need to map - save KVM) */
406 	vc->vc_biostag = memt;
407 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0, &vc->vc_bioshdl))
408 		vc->vc_biosmapped = 0;
409 	else
410 		vc->vc_biosmapped = 1;
411 
412 	vc->nscreens = 0;
413 	LIST_INIT(&vc->screens);
414 	vc->active = NULL;
415 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
416 	callout_init(&vc->vc_switch_callout, 0);
417 
418 	wsfont_init();
419 	vc->nfonts = 1;
420 	LIST_INIT(&vc->vc_fontlist);
421 	vf = &vga_console_fontset_ascii;
422 	if (vga_no_builtinfont) {
423 		struct wsdisplay_font *wf;
424 		int cookie;
425 
426 		/* prefer 8x16 pixel font */
427 		cookie = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
428 		    0, WSFONT_FIND_BITMAP);
429 		if (cookie == -1)
430 			cookie = wsfont_find(NULL, 0, 0, 0,
431 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
432 			    WSFONT_FIND_BITMAP);
433 		if (cookie == -1 || wsfont_lock(cookie, &wf))
434 			panic("vga_raster_init: can't load console font");
435 		vf->font = wf;
436 	} else {
437 		vga_load_builtinfont(vh, builtinfont_data, 0, 256);
438 		vf->font = &builtinfont;
439 	}
440 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
441 }
442 
443 static void
444 vga_raster_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 	struct vga_handle *vh;
450 
451 	scr->cfg = vc;
452 	scr->hdl = &vc->hdl;
453 	scr->type = type;
454 	scr->mindispoffset = 0;
455 	scr->maxdispoffset = scr->dispoffset +
456 	    type->nrows * type->ncols * type->fontheight;
457 	vh = &vc->hdl;
458 
459 	LIST_INIT(&scr->fontset);
460 	vga_raster_setup_font(vc, scr);
461 
462 	if (existing) {
463 		int i;
464 
465 		cpos = vga_6845_read(vh, cursorh) << 8;
466 		cpos |= vga_6845_read(vh, cursorl);
467 
468 		/* make sure we have a valid cursor position */
469 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
470 			cpos = 0;
471 
472 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
473 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
474 
475 		/* make sure we have a valid memory offset */
476 		if (scr->dispoffset < scr->mindispoffset ||
477 		    scr->dispoffset > scr->maxdispoffset)
478 			scr->dispoffset = scr->mindispoffset;
479 
480 		scr->mem = boot_scrmem;
481 		scr->active = 1;
482 
483 		/* Save the current screen to memory. XXXBJY assume 80x25 */
484 		for (i = 0; i < 80 * 25; i++) {
485 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
486 			    vh->vh_allmemh, 0x18000 + i * 2);
487 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
488 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
489 			scr->mem[i].enc = scr->encoding;
490 		}
491 
492 		vga_raster_setscreentype(vc, type);
493 
494 		/* Clear the entire screen. */
495 		vga_gdc_write(vh, mode, 0x02);
496 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
497 		    0x4000);
498 
499 		vga_restore_screen(scr, type, scr->mem);
500 	} else {
501 		cpos = 0;
502 		scr->dispoffset = scr->mindispoffset;
503 		scr->mem = NULL;
504 		scr->active = 0;
505 	}
506 
507 	scr->cursorrow = cpos / type->ncols;
508 	scr->cursorcol = cpos % type->ncols;
509 	vga_raster_cursor_init(scr, existing);
510 
511 #ifdef __alpha__
512 	if (!vc->hdl.vh_mono)
513 		/*
514 		 * DEC firmware uses a blue background.
515 		 */
516 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
517 		    WSATTR_WSCOLORS, attrp);
518 	else
519 #endif
520 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
521 #ifdef DIAGNOSTIC
522 	if (res)
523 		panic("vga_raster_init_screen: attribute botch");
524 #endif
525 
526 	vc->nscreens++;
527 	LIST_INSERT_HEAD(&vc->screens, scr, next);
528 }
529 
530 void
531 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
532     bus_space_tag_t memt, int type, int quirks,
533     const struct vga_funcs *vf)
534 {
535 	int console;
536 	struct vga_config *vc;
537 	struct wsemuldisplaydev_attach_args aa;
538 
539 	console = vga_is_console(iot, type);
540 
541 	if (console) {
542 		vc = &vga_console_vc;
543 		vga_console_attached = 1;
544 	} else {
545 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
546 		vga_raster_init(vc, iot, memt);
547 	}
548 
549 	vc->vc_type = type;
550 	vc->vc_funcs = vf;
551 
552 	sc->sc_vc = vc;
553 	vc->softc = sc;
554 
555 	aa.console = console;
556 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
557 	aa.accessops = &vga_raster_accessops;
558 	aa.accesscookie = vc;
559 
560 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
561 }
562 
563 int
564 vga_cndetach(void)
565 {
566 	struct vga_config *vc;
567 	struct vga_handle *vh;
568 
569 	vc = &vga_console_vc;
570 	vh = &vc->hdl;
571 
572 	if (vgaconsole) {
573 		wsdisplay_cndetach();
574 
575 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_vga, 0x10);
576 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_6845, 0x10);
577 
578 		vga_console_attached = 0;
579 		vgaconsole = 0;
580 
581 		return 1;
582 	}
583 
584 	return 0;
585 }
586 
587 int
588 vga_is_console(bus_space_tag_t iot, int type)
589 {
590 	if (vgaconsole &&
591 	    !vga_console_attached &&
592 	    iot == vga_console_vc.hdl.vh_iot &&
593 	    (vga_console_type == -1 || (type == vga_console_type)))
594 		return (1);
595 	return (0);
596 }
597 
598 static int
599 vga_get_video(struct vga_config *vc)
600 {
601 
602 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
603 }
604 
605 static void
606 vga_set_video(struct vga_config *vc, int state)
607 {
608 	int val;
609 
610 	vga_ts_write(&vc->hdl, syncreset, 0x01);
611 	if (state) {					/* unblank screen */
612 		val = vga_ts_read(&vc->hdl, mode);
613 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
614 #ifndef VGA_NO_VBLANK
615 		val = vga_6845_read(&vc->hdl, mode);
616 		vga_6845_write(&vc->hdl, mode, val | 0x80);
617 #endif
618 	} else {					/* blank screen */
619 		val = vga_ts_read(&vc->hdl, mode);
620 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
621 #ifndef VGA_NO_VBLANK
622 		val = vga_6845_read(&vc->hdl, mode);
623 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
624 #endif
625 	}
626 	vga_ts_write(&vc->hdl, syncreset, 0x03);
627 }
628 
629 int
630 vga_raster_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
631 	struct lwp *l)
632 {
633 	struct vga_config *vc = v;
634 	const struct vga_funcs *vf = vc->vc_funcs;
635 
636 	switch (cmd) {
637 	case WSDISPLAYIO_GTYPE:
638 		*(int *)data = vc->vc_type;
639 		return 0;
640 
641 	case WSDISPLAYIO_GINFO: {
642 		struct wsdisplay_fbinfo *fbi = data;
643 		const struct wsscreen_descr *wd = vc->currenttype;
644 		const struct videomode *vm = wd->modecookie;
645 		fbi->width = vm->hdisplay;
646 		fbi->height = vm->vdisplay;
647 		fbi->depth = 24;	/* xxx: ? */
648 		fbi->cmsize = 256;	/* xxx: from palette */
649 		return 0;
650 	}
651 
652 	case WSDISPLAYIO_GVIDEO:
653 		*(int *)data = (vga_get_video(vc) ?
654 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
655 		return 0;
656 
657 	case WSDISPLAYIO_SVIDEO:
658 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
659 		return 0;
660 
661 	case WSDISPLAYIO_GETCMAP:
662 	case WSDISPLAYIO_PUTCMAP:
663 	case WSDISPLAYIO_GCURPOS:
664 	case WSDISPLAYIO_SCURPOS:
665 	case WSDISPLAYIO_GCURMAX:
666 	case WSDISPLAYIO_GCURSOR:
667 	case WSDISPLAYIO_SCURSOR:
668 #ifdef DIAGNOSTIC
669 		printf("%s: 0x%lx unsupported\n", __func__, cmd);
670 #endif
671 		/* NONE of these operations are by the generic VGA driver. */
672 		return EPASSTHROUGH;
673 	}
674 
675 	if (vc->vc_funcs == NULL) {
676 #ifdef DIAGNOSTIC
677 		printf("%s: no vc_funcs\n", __func__);
678 #endif
679 		return EPASSTHROUGH;
680 	}
681 
682 	if (vf->vf_ioctl == NULL) {
683 #ifdef DIAGNOSTIC
684 		printf("%s: no vf_ioctl\n", __func__);
685 #endif
686 		return EPASSTHROUGH;
687 	}
688 
689 	return ((*vf->vf_ioctl)(v, cmd, data, flag, l));
690 }
691 
692 static paddr_t
693 vga_raster_mmap(void *v, void *vs, off_t offset, int prot)
694 {
695 	struct vga_config *vc = v;
696 	const struct vga_funcs *vf = vc->vc_funcs;
697 
698 	if (vc->vc_funcs == NULL)
699 		return (-1);
700 
701 	if (vf->vf_mmap == NULL)
702 		return (-1);
703 
704 	return ((*vf->vf_mmap)(v, offset, prot));
705 }
706 
707 static int
708 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
709     void **cookiep, int *curxp, int *curyp, long *defattrp)
710 {
711 	struct vga_config *vc = v;
712 	struct vgascreen *scr;
713 
714 	if (vc->nscreens == 1) {
715 		vc->screens.lh_first->mem = boot_scrmem;
716 	}
717 
718 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
719 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
720 
721 	if (vc->nscreens == 1) {
722 		scr->active = 1;
723 		vc->active = scr;
724 		vc->currenttype = type;
725 	} else {
726 		scr->mem = malloc(sizeof(struct vga_scrmem) *
727 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
728 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
729 	}
730 
731 	*cookiep = scr;
732 	*curxp = scr->cursorcol;
733 	*curyp = scr->cursorrow;
734 
735 	return (0);
736 }
737 
738 static void
739 vga_raster_free_screen(void *v, void *cookie)
740 {
741 	struct vgascreen *vs = cookie;
742 	struct vga_config *vc = vs->cfg;
743 
744 	LIST_REMOVE(vs, next);
745 	vc->nscreens--;
746 	if (vs != &vga_console_screen)
747 		free(vs, M_DEVBUF);
748 	else
749 		panic("vga_raster_free_screen: console");
750 
751 	if (vc->active == vs)
752 		vc->active = 0;
753 }
754 
755 static int
756 vga_raster_show_screen(void *v, void *cookie, int waitok,
757     void (*cb)(void *, int, int), void *cbarg)
758 {
759 	struct vgascreen *scr = cookie, *oldscr;
760 	struct vga_config *vc = scr->cfg;
761 
762 	oldscr = vc->active; /* can be NULL! */
763 	if (scr == oldscr) {
764 		return (0);
765 	}
766 
767 	vc->wantedscreen = cookie;
768 	vc->switchcb = cb;
769 	vc->switchcbarg = cbarg;
770 	if (cb) {
771 		callout_reset(&vc->vc_switch_callout, 0,
772 		    (void(*)(void *))vga_switch_screen, vc);
773 		return (EAGAIN);
774 	}
775 
776 	vga_switch_screen(vc);
777 	return (0);
778 }
779 
780 static void
781 vga_switch_screen(struct vga_config *vc)
782 {
783 	struct vgascreen *scr, *oldscr;
784 	struct vga_handle *vh = &vc->hdl;
785 	const struct wsscreen_descr *type;
786 
787 	scr = vc->wantedscreen;
788 	if (!scr) {
789 		printf("vga_switch_screen: disappeared\n");
790 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
791 		return;
792 	}
793 	type = scr->type;
794 	oldscr = vc->active; /* can be NULL! */
795 #ifdef DIAGNOSTIC
796 	if (oldscr) {
797 		if (!oldscr->active)
798 			panic("vga_raster_show_screen: not active");
799 		if (oldscr->type != vc->currenttype)
800 			panic("vga_raster_show_screen: bad type");
801 	}
802 #endif
803 	if (scr == oldscr) {
804 		return;
805 	}
806 #ifdef DIAGNOSTIC
807 	if (scr->active)
808 		panic("vga_raster_show_screen: active");
809 #endif
810 
811 	if (oldscr)
812 		oldscr->active = 0;
813 
814 	if (vc->currenttype != type) {
815 		vga_raster_setscreentype(vc, type);
816 		vc->currenttype = type;
817 	}
818 
819 	scr->dispoffset = scr->mindispoffset;
820 
821 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
822 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
823 		vga_6845_write(vh, startadrl, scr->dispoffset);
824 	}
825 
826 	/* Clear the entire screen. */
827 	vga_gdc_write(vh, mode, 0x02);
828 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
829 
830 	scr->active = 1;
831 	vga_restore_screen(scr, type, scr->mem);
832 
833 	vc->active = scr;
834 
835 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
836 
837 	vc->wantedscreen = 0;
838 	if (vc->switchcb)
839 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
840 }
841 
842 static int
843 vga_raster_load_font(void *v, void *id,
844     struct wsdisplay_font *data)
845 {
846 	/* XXX */
847 	printf("vga_raster_load_font: called\n");
848 
849 	return (0);
850 }
851 
852 static void
853 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
854 {
855 	struct vga_raster_font *vf;
856 	struct wsdisplay_font *wf;
857 	int cookie;
858 
859 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
860 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0,
861 		    WSFONT_FIND_BITMAP)) {
862 			scr->encoding = vf->font->encoding;
863 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
864 			return;
865 		}
866 	}
867 
868 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
869 	    WSDISPLAY_FONTORDER_L2R, 0, WSFONT_FIND_BITMAP);
870 	if (cookie == -1)
871 		return;
872 
873 	if (wsfont_lock(cookie, &wf))
874 		return;
875 
876 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_NOWAIT);
877 	if (!vf) {
878 		wsfont_unlock(cookie);
879 		return;
880 	}
881 
882 	vf->font = wf;
883 	scr->encoding = vf->font->encoding;
884 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
885 }
886 
887 static void
888 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
889 {
890 	int i;
891 	int depth = 4;			/* XXXBJY hardcoded for now */
892 	const u_int8_t palette[] = {
893 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
894 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
895 	};
896 
897 	/*
898 	 * Compute hsync and vsync polarity.
899 	 */
900 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
901 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
902 	    	regs->miscout = 0x23;
903 		if (mode->flags & VID_NHSYNC)
904 			regs->miscout |= 0x40;
905 		if (mode->flags & VID_NVSYNC)
906 			regs->miscout |= 0x80;
907 	} else {
908 		if (mode->flags & VID_DBLSCAN)
909 			mode->vdisplay *= 2;
910 		if (mode->vdisplay < 400)
911 			regs->miscout = 0xa3;
912 		else if (mode->vdisplay < 480)
913 			regs->miscout = 0x63;
914 		else if (mode->vdisplay < 768)
915 			regs->miscout = 0xe3;
916 		else
917 			regs->miscout = 0x23;
918 	}
919 
920 	/*
921 	 * Time sequencer
922 	 */
923 	if (depth == 4)
924 		regs->ts[0] = 0x02;
925 	else
926 		regs->ts[0] = 0x00;
927 	if (mode->flags & VID_CLKDIV2)
928 		regs->ts[1] = 0x09;
929 	else
930 		regs->ts[1] = 0x01;
931 	regs->ts[2] = 0x0f;
932 	regs->ts[3] = 0x00;
933 	if (depth < 8)
934 		regs->ts[4] = 0x06;
935 	else
936 		regs->ts[4] = 0x0e;
937 
938 	/*
939 	 * CRTC controller
940 	 */
941 	regs->crtc[0] = (mode->htotal >> 3) - 5;
942 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
943 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
944 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
945 	regs->crtc[4] = mode->hsync_start >> 3;
946 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
947 	    | (((mode->hsync_end >> 3)) & 0x1f);
948 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
949 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
950 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
951 	    | ((mode->vsync_start & 0x100) >> 6)
952 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
953 	    | 0x10
954 	    | (((mode->vtotal - 2) & 0x200) >> 4)
955 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
956 	    | ((mode->vsync_start & 0x200) >> 2);
957 	regs->crtc[8] = 0x00;
958 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
959 	if (mode->flags & VID_DBLSCAN)
960 		regs->crtc[9] |= 0x80;
961 	regs->crtc[10] = 0x00;
962 	regs->crtc[11] = 0x00;
963 	regs->crtc[12] = 0x00;
964 	regs->crtc[13] = 0x00;
965 	regs->crtc[14] = 0x00;
966 	regs->crtc[15] = 0x00;
967 	regs->crtc[16] = mode->vsync_start & 0xff;
968 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
969 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
970 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
971 	regs->crtc[20] = 0x00;
972 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
973 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
974 	if (depth < 8)
975 		regs->crtc[23] = 0xe3;
976 	else
977 		regs->crtc[23] = 0xc3;
978 	regs->crtc[24] = 0xff;
979 
980 	/*
981 	 * Graphics display controller
982 	 */
983 	regs->gdc[0] = 0x00;
984 	regs->gdc[1] = 0x00;
985 	regs->gdc[2] = 0x00;
986 	regs->gdc[3] = 0x00;
987 	regs->gdc[4] = 0x00;
988 	if (depth == 4)
989 		regs->gdc[5] = 0x02;
990 	else
991 		regs->gdc[5] = 0x40;
992 	regs->gdc[6] = 0x01;
993 	regs->gdc[7] = 0x0f;
994 	regs->gdc[8] = 0xff;
995 
996 	/*
997 	 * Attribute controller
998 	 */
999 	/* Set palette registers. */
1000 	for (i = 0; i < 16; i++)
1001 		regs->atc[i] = palette[i];
1002 	if (depth == 4)
1003 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
1004 	else
1005 		regs->atc[16] = 0x41;
1006 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
1007 	regs->atc[18] = 0x0f;
1008 	regs->atc[19] = 0x00;
1009 	regs->atc[20] = 0x00;
1010 }
1011 
1012 static void
1013 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
1014 {
1015 	int i;
1016 
1017 	/* Disable display. */
1018 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
1019 
1020 	/* Write misc output register. */
1021 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
1022 	    regs->miscout);
1023 
1024 	/* Set synchronous reset. */
1025 	vga_ts_write(vh, syncreset, 0x01);
1026 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
1027 	for (i = 2; i < VGA_TS_NREGS; i++)
1028 		_vga_ts_write(vh, i, regs->ts[i]);
1029 	/* Clear synchronous reset. */
1030 	vga_ts_write(vh, syncreset, 0x03);
1031 
1032 	/* Unprotect CRTC registers 0-7. */
1033 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
1034 	/* Write CRTC registers. */
1035 	for (i = 0; i < MC6845_NREGS; i++)
1036 		_vga_6845_write(vh, i, regs->crtc[i]);
1037 
1038 	/* Write graphics display registers. */
1039 	for (i = 0; i < VGA_GDC_NREGS; i++)
1040 		_vga_gdc_write(vh, i, regs->gdc[i]);
1041 
1042 	/* Write attribute controller registers. */
1043 	for (i = 0; i < VGA_ATC_NREGS; i++)
1044 		_vga_attr_write(vh, i, regs->atc[i]);
1045 
1046 	/* Enable display. */
1047 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
1048 }
1049 
1050 static void
1051 vga_raster_cursor_init(struct vgascreen *scr, int existing)
1052 {
1053 	int off;
1054 
1055 	if (existing) {
1056 		/*
1057 		 * This is the first screen. At this point, scr->active is
1058 		 * false, so we can't use vga_raster_cursor() to do this.
1059 		 */
1060 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
1061 		    scr->dispoffset / 8;
1062 
1063 		scr->cursortmp = scr->mem[off];
1064 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
1065 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
1066 	} else {
1067 		scr->cursortmp.ch = 0;
1068 		scr->cursortmp.attr = 0;
1069 		scr->cursortmp.second = 0;
1070 		scr->cursortmp.enc = scr->encoding;
1071 	}
1072 
1073 	scr->cursoron = 1;
1074 }
1075 
1076 static void
1077 vga_raster_cursor(void *id, int on, int row, int col)
1078 {
1079 	struct vgascreen *scr = id;
1080 	int off, tmp;
1081 
1082 	/* Remove old cursor image */
1083 	if (scr->cursoron) {
1084 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1085 		if (scr->active) {
1086 			tmp = scr->encoding;
1087 			scr->encoding = scr->cursortmp.enc;
1088 			if (scr->cursortmp.second)
1089 				vga_raster_putchar(id, scr->cursorrow,
1090 				    scr->cursorcol - 1, scr->cursortmp.ch,
1091 				    scr->cursortmp.attr);
1092 			else
1093 				vga_raster_putchar(id, scr->cursorrow,
1094 				    scr->cursorcol, scr->cursortmp.ch,
1095 				    scr->cursortmp.attr);
1096 			scr->encoding = tmp;
1097 		}
1098 	}
1099 
1100 	scr->cursorrow = row;
1101 	scr->cursorcol = col;
1102 
1103 	if ((scr->cursoron = on) == 0)
1104 		return;
1105 
1106 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1107 	scr->cursortmp = scr->mem[off];
1108 	if (scr->active) {
1109 		tmp = scr->encoding;
1110 		scr->encoding = scr->cursortmp.enc;
1111 		if (scr->cursortmp.second)
1112 			vga_raster_putchar(id, scr->cursorrow,
1113 			    scr->cursorcol - 1, scr->cursortmp.ch,
1114 			    scr->cursortmp.attr ^ 0x77);
1115 		else
1116 			vga_raster_putchar(id, scr->cursorrow,
1117 			    scr->cursorcol, scr->cursortmp.ch,
1118 			    scr->cursortmp.attr ^ 0x77);
1119 		scr->encoding = tmp;
1120 	}
1121 }
1122 
1123 static int
1124 vga_raster_mapchar(void *id, int uni, u_int *index)
1125 {
1126 	struct vgascreen *scr = id;
1127 
1128 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
1129 		return pcdisplay_mapchar(id, uni, index);
1130 	else {
1131 		*index = uni;
1132 		return 5;
1133 	}
1134 }
1135 
1136 static void
1137 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
1138 {
1139 	struct vgascreen *scr = id;
1140 	size_t off;
1141 	struct vga_raster_font *fs;
1142 	u_int tmp_ch;
1143 
1144 	off = row * scr->type->ncols + col;
1145 
1146 	if (__predict_false(off >= (scr->type->ncols * scr->type->nrows)))
1147 		return;
1148 
1149 	LIST_FOREACH(fs, &scr->fontset, next) {
1150 		if ((scr->encoding == fs->font->encoding) &&
1151 		    (c >= fs->font->firstchar) &&
1152 		    (c < fs->font->firstchar + fs->font->numchars) &&
1153 		    (scr->type->fontheight == fs->font->fontheight)) {
1154 			if (scr->active) {
1155 				tmp_ch = c - fs->font->firstchar;
1156 				_vga_raster_putchar(scr, row, col, tmp_ch,
1157 				    attr, fs);
1158 			}
1159 
1160 			scr->mem[off].ch = c;
1161 			scr->mem[off].attr = attr;
1162 			scr->mem[off].second = 0;
1163 			scr->mem[off].enc = fs->font->encoding;
1164 
1165 			if (fs->font->stride == 2) {
1166 				scr->mem[off + 1].ch = c;
1167 				scr->mem[off + 1].attr = attr;
1168 				scr->mem[off + 1].second = 1;
1169 				scr->mem[off + 1].enc = fs->font->encoding;
1170 			}
1171 
1172 			return;
1173 		}
1174 	}
1175 
1176 	/*
1177 	 * No match found.
1178 	 */
1179 	if (scr->active)
1180 		/*
1181 		 * Put a single width space character no matter what the
1182 		 * actual width of the character is.
1183 		 */
1184 		_vga_raster_putchar(scr, row, col, ' ', attr,
1185 		    &vga_console_fontset_ascii);
1186 	scr->mem[off].ch = c;
1187 	scr->mem[off].attr = attr;
1188 	scr->mem[off].second = 0;
1189 	scr->mem[off].enc = scr->encoding;
1190 }
1191 
1192 static void
1193 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
1194     struct vga_raster_font *fs)
1195 {
1196 	struct vgascreen *scr = id;
1197 	struct vga_handle *vh = scr->hdl;
1198 	bus_space_tag_t memt = vh->vh_memt;
1199 	bus_space_handle_t memh = vh->vh_memh;
1200 	int i;
1201 	int rasoff, rasoff2;
1202 	int fheight = scr->type->fontheight;
1203 	volatile u_int8_t pattern;
1204 	u_int8_t fgcolor, bgcolor;
1205 
1206 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
1207 	rasoff2 = rasoff;
1208 
1209 #if 0
1210 	bgcolor = bgansitopc[attr >> 4];
1211 	fgcolor = fgansitopc[attr & 0x0f];
1212 #else
1213 	bgcolor = ((attr >> 4) & 0x0f);
1214 	fgcolor = attr & 0x0f;
1215 #endif
1216 
1217 	if (fs->font->stride == 1) {
1218 		/* Paint background. */
1219 		vga_gdc_write(vh, mode, 0x02);
1220 		for (i = 0; i < fheight; i++) {
1221 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1222 			rasoff += scr->type->ncols;
1223 		}
1224 
1225 		/* Draw a single width character. */
1226 		vga_gdc_write(vh, mode, 0x03);
1227 		vga_gdc_write(vh, setres, fgcolor);
1228 		for (i = 0; i < fheight; i++) {
1229 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
1230 			/* When pattern is 0, skip output for speed-up. */
1231 			if (pattern != 0) {
1232 				bus_space_read_1(memt, memh, rasoff2);
1233 				bus_space_write_1(memt, memh, rasoff2, pattern);
1234 			}
1235 			rasoff2 += scr->type->ncols;
1236 		}
1237 	} else if (fs->font->stride == 2) {
1238 		/* Paint background. */
1239 		vga_gdc_write(vh, mode, 0x02);
1240 		for (i = 0; i < fheight; i++) {
1241 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1242 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
1243 			rasoff += scr->type->ncols;
1244 		}
1245 
1246 		/* Draw a double width character. */
1247 		vga_gdc_write(vh, mode, 0x03);
1248 		vga_gdc_write(vh, setres, fgcolor);
1249 		for (i = 0; i < fheight; i++) {
1250 			pattern = ((u_int8_t *)fs->font->data)
1251 			    [(c * fheight + i) * 2];
1252 			if (pattern != 0) {
1253 				bus_space_read_1(memt, memh, rasoff2);
1254 				bus_space_write_1(memt, memh, rasoff2, pattern);
1255 			}
1256 			pattern = ((u_int8_t *)fs->font->data)
1257 			    [(c * fheight + i) * 2 + 1];
1258 			if (pattern != 0) {
1259 				rasoff2++;
1260 				bus_space_read_1(memt, memh, rasoff2);
1261 				bus_space_write_1(memt, memh, rasoff2, pattern);
1262 				rasoff2--;
1263 			}
1264 			rasoff2 += scr->type->ncols;
1265 		}
1266 	}
1267 }
1268 
1269 static void
1270 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
1271 {
1272 	struct vgascreen *scr = id;
1273 	struct vga_handle *vh = scr->hdl;
1274 	bus_space_tag_t memt = vh->vh_memt;
1275 	bus_space_handle_t memh = vh->vh_memh;
1276 	bus_size_t srcoff, dstoff;
1277 	bus_size_t rassrcoff, rasdstoff;
1278 	int i;
1279 	int fheight = scr->type->fontheight;
1280 
1281 	srcoff = row * scr->type->ncols + srccol;
1282 	dstoff = row * scr->type->ncols + dstcol;
1283 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
1284 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
1285 
1286 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1287 	    ncols * sizeof(struct vga_scrmem));
1288 
1289 	vga_gdc_write(vh, mode, 0x01);
1290 	if (scr->active) {
1291 		for (i = 0; i < fheight; i++) {
1292 			bus_space_copy_region_1(memt, memh,
1293 			    rassrcoff + i * scr->type->ncols, memh,
1294 			    rasdstoff + i * scr->type->ncols, ncols);
1295 		}
1296 	}
1297 }
1298 
1299 static void
1300 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
1301 {
1302 	struct vgascreen *scr = id;
1303 	int i;
1304 
1305 	if (scr->active == 0)
1306 		return;
1307 
1308 	for (i = startcol; i < startcol + ncols; i++)
1309 		vga_raster_putchar(id, row, i, ' ', fillattr);
1310 }
1311 
1312 static void
1313 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
1314 {
1315 	struct vgascreen *scr = id;
1316 	struct vga_handle *vh = scr->hdl;
1317 	bus_space_tag_t memt = vh->vh_memt;
1318 	bus_space_handle_t memh = vh->vh_memh;
1319 	int ncols;
1320 	bus_size_t srcoff, dstoff;
1321 	bus_size_t rassrcoff, rasdstoff;
1322 	int fheight;
1323 
1324 	ncols = scr->type->ncols;
1325 	fheight = scr->type->fontheight;
1326 
1327 	srcoff = srcrow * ncols;
1328 	dstoff = dstrow * ncols;
1329 	rassrcoff = srcoff * fheight;
1330 	rasdstoff = dstoff * fheight;
1331 
1332 	if (scr->active) {
1333 		vga_gdc_write(vh, mode, 0x01);
1334 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
1335 			int cursoron = scr->cursoron;
1336 
1337 			if (cursoron)
1338 				/* Disable cursor. */
1339 				vga_raster_cursor(scr, 0,
1340 				    scr->cursorrow, scr->cursorcol);
1341 
1342 			/* scroll up whole screen */
1343 			if ((scr->dispoffset + srcrow * ncols * fheight)
1344 			    <= scr->maxdispoffset)
1345 				scr->dispoffset += srcrow * ncols * fheight;
1346 			else {
1347 				bus_space_copy_region_1(memt, memh,
1348 				    scr->dispoffset + rassrcoff,
1349 				    memh, scr->mindispoffset,
1350 				    nrows * ncols * fheight);
1351 				scr->dispoffset = scr->mindispoffset;
1352 			}
1353 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
1354 			vga_6845_write(vh, startadrl, scr->dispoffset);
1355 
1356 			if (cursoron)
1357 				/* Enable cursor. */
1358 				vga_raster_cursor(scr, 1, scr->cursorrow,
1359 				    scr->cursorcol);
1360 		} else
1361 			bus_space_copy_region_1(memt, memh,
1362 			    scr->dispoffset + rassrcoff, memh,
1363 			    scr->dispoffset + rasdstoff,
1364 			    nrows * ncols * fheight);
1365 	}
1366 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1367 	    nrows * ncols * sizeof(struct vga_scrmem));
1368 }
1369 
1370 static void
1371 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
1372 {
1373 	struct vgascreen *scr = id;
1374 	struct vga_handle *vh = scr->hdl;
1375 	bus_space_tag_t memt = vh->vh_memt;
1376 	bus_space_handle_t memh = vh->vh_memh;
1377 	bus_size_t off, count;
1378 	bus_size_t rasoff, rascount;
1379 	int i;
1380 
1381 	off = startrow * scr->type->ncols;
1382 	count = nrows * scr->type->ncols;
1383 	rasoff = off * scr->type->fontheight;
1384 	rascount = count * scr->type->fontheight;
1385 
1386 	if (scr->active) {
1387 		u_int8_t bgcolor = (fillattr >> 4) & 0x0F;
1388 
1389 		/* Paint background. */
1390 		vga_gdc_write(vh, mode, 0x02);
1391 		if (scr->type->ncols % 4 == 0) {
1392 			u_int32_t fill = bgcolor | (bgcolor << 8) |
1393 			    (bgcolor << 16) | (bgcolor << 24);
1394 			/* We can speed up I/O */
1395 			for (i = rasoff; i < rasoff + rascount; i += 4)
1396 				bus_space_write_4(memt, memh,
1397 				    scr->dispoffset + i, fill);
1398 		} else {
1399 			u_int16_t fill = bgcolor | (bgcolor << 8);
1400 			for (i = rasoff; i < rasoff + rascount; i += 2)
1401 				bus_space_write_2(memt, memh,
1402 				    scr->dispoffset + i, fill);
1403 		}
1404 	}
1405 	for (i = 0; i < count; i++) {
1406 		scr->mem[off + i].ch = ' ';
1407 		scr->mem[off + i].attr = fillattr;
1408 		scr->mem[off + i].second = 0;
1409 		scr->mem[off + i].enc = scr->encoding;
1410 	}
1411 }
1412 
1413 static int
1414 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
1415 {
1416 	struct vgascreen *scr = id;
1417 	struct vga_config *vc = scr->cfg;
1418 
1419 	if (__predict_false((unsigned int)fg >= sizeof(fgansitopc) ||
1420 	    (unsigned int)bg >= sizeof(bgansitopc)))
1421 	    	return (EINVAL);
1422 
1423 	if (vc->hdl.vh_mono) {
1424 		if (flags & WSATTR_WSCOLORS)
1425 			return (EINVAL);
1426 		if (flags & WSATTR_REVERSE)
1427 			*attrp = 0x70;
1428 		else
1429 			*attrp = 0x07;
1430 		if (flags & WSATTR_UNDERLINE)
1431 			*attrp |= FG_UNDERLINE;
1432 		if (flags & WSATTR_HILIT)
1433 			*attrp |= FG_INTENSE;
1434 	} else {
1435 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
1436 			return (EINVAL);
1437 		if (flags & WSATTR_WSCOLORS)
1438 			*attrp = fgansitopc[fg] | bgansitopc[bg];
1439 		else
1440 			*attrp = 7;
1441 		if (flags & WSATTR_HILIT)
1442 			*attrp += 8;
1443 	}
1444 	if (flags & WSATTR_BLINK)
1445 		*attrp |= FG_BLINK;
1446 	return (0);
1447 }
1448 
1449 static void
1450 vga_restore_screen(struct vgascreen *scr,
1451     const struct wsscreen_descr *type, struct vga_scrmem *mem)
1452 {
1453 	int i, j, off, tmp;
1454 
1455 	tmp = scr->encoding;
1456 	for (i = 0; i < type->nrows; i++) {
1457 		for (j = 0; j < type->ncols; j++) {
1458 			off = i * type->ncols + j;
1459 			if (mem[off].second != 1) {
1460 				scr->encoding = mem[off].enc;
1461 				vga_raster_putchar(scr, i, j, mem[off].ch,
1462 				    mem[off].attr);
1463 			}
1464 		}
1465 	}
1466 	scr->encoding = tmp;
1467 }
1468 
1469 static void
1470 vga_raster_setscreentype(struct vga_config *vc,
1471     const struct wsscreen_descr *type)
1472 {
1473 	struct vga_handle *vh = &vc->hdl;
1474 	struct vga_moderegs moderegs;
1475 
1476 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
1477 	vga_set_mode(vh, &moderegs);
1478 }
1479 
1480 #ifdef WSDISPLAY_CUSTOM_OUTPUT
1481 static void
1482 vga_raster_replaceattr(void *id, long oldattr, long newattr)
1483 {
1484 	struct vgascreen *scr = id;
1485 	const struct wsscreen_descr *type = scr->type;
1486 	int off;
1487 
1488 	for (off = 0; off < type->nrows * type->ncols; off++) {
1489 		if (scr->mem[off].attr == oldattr)
1490 			scr->mem[off].attr = newattr;
1491 	}
1492 
1493 	/* Repaint the whole screen, if needed */
1494 	if (scr->active)
1495 		vga_restore_screen(scr, type, scr->mem);
1496 }
1497 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
1498 
1499 void
1500 vga_resume(struct vga_softc *sc)
1501 {
1502 #ifdef VGA_RESET_ON_RESUME
1503 	vga_initregs(&sc->sc_vc->hdl);
1504 #endif
1505 }
1506