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