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