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