ega.c revision 1.26 1 /* $NetBSD: ega.c,v 1.26 2009/03/14 21:04:20 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1999
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ega.c,v 1.26 2009/03/14 21:04:20 dsl Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/callout.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 #include <sys/bus.h>
39
40 #include <dev/isa/isavar.h>
41
42 #include <dev/ic/mc6845reg.h>
43 #include <dev/ic/pcdisplayvar.h>
44 #include <dev/ic/vgareg.h>
45 #include <dev/ic/vgavar.h>
46 #include <dev/isa/egavar.h>
47
48 #include <dev/ic/pcdisplay.h>
49
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wsdisplayvar.h>
52
53 static struct egafont {
54 char name[16];
55 int height;
56 int encoding;
57 int slot;
58 } ega_builtinfont = {
59 "builtin",
60 14,
61 WSDISPLAY_FONTENC_IBM,
62 0
63 };
64
65 struct egascreen {
66 struct pcdisplayscreen pcs;
67 LIST_ENTRY(egascreen) next;
68 struct ega_config *cfg;
69 struct egafont *fontset1, *fontset2;
70
71 int mindispoffset, maxdispoffset;
72 };
73
74 struct ega_config {
75 struct vga_handle hdl;
76
77 int nscreens;
78 LIST_HEAD(, egascreen) screens;
79 struct egascreen *active; /* current display */
80 const struct wsscreen_descr *currenttype;
81 int currentfontset1, currentfontset2;
82
83 struct egafont *vc_fonts[4];
84
85 struct egascreen *wantedscreen;
86 void (*switchcb)(void *, int, int);
87 void *switchcbarg;
88
89 callout_t switch_callout;
90 };
91
92 struct ega_softc {
93 struct device sc_dev;
94 struct ega_config *sc_dc;
95 int nscreens;
96 };
97
98 static int egaconsole, ega_console_attached;
99 static struct egascreen ega_console_screen;
100 static struct ega_config ega_console_dc;
101
102 int ega_match(struct device *, struct cfdata *, void *);
103 void ega_attach(struct device *, struct device *, void *);
104
105 static int ega_is_console(bus_space_tag_t);
106 static int ega_probe_col(bus_space_tag_t, bus_space_tag_t);
107 static int ega_probe_mono(bus_space_tag_t, bus_space_tag_t);
108 int ega_selectfont(struct ega_config *, struct egascreen *, char *, char *);
109 void ega_init_screen(struct ega_config *, struct egascreen *,
110 const struct wsscreen_descr *, int, long *);
111 static void ega_init(struct ega_config *, bus_space_tag_t, bus_space_tag_t,
112 int);
113 static void ega_setfont(struct ega_config *, struct egascreen *);
114 static int ega_allocattr(void *, int, int, int, long *);
115 void ega_copyrows(void *, int, int, int);
116
117 CFATTACH_DECL(ega, sizeof(struct ega_softc),
118 ega_match, ega_attach, NULL, NULL);
119
120 const struct wsdisplay_emulops ega_emulops = {
121 pcdisplay_cursor,
122 pcdisplay_mapchar,
123 pcdisplay_putchar,
124 pcdisplay_copycols,
125 pcdisplay_erasecols,
126 ega_copyrows,
127 pcdisplay_eraserows,
128 ega_allocattr
129 };
130
131 /*
132 * translate WS(=ANSI) color codes to standard pc ones
133 */
134 static unsigned char fgansitopc[] = {
135 FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
136 FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
137 }, bgansitopc[] = {
138 BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
139 BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
140 };
141
142 const struct wsscreen_descr ega_stdscreen = {
143 "80x25", 80, 25,
144 &ega_emulops,
145 8, 14,
146 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
147 }, ega_stdscreen_mono = {
148 "80x25", 80, 25,
149 &ega_emulops,
150 8, 14,
151 WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
152 }, ega_stdscreen_bf = {
153 "80x25bf", 80, 25,
154 &ega_emulops,
155 8, 14,
156 WSSCREEN_WSCOLORS | WSSCREEN_BLINK
157 }, ega_35lscreen = {
158 "80x35", 80, 35,
159 &ega_emulops,
160 8, 10,
161 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
162 }, ega_35lscreen_mono = {
163 "80x35", 80, 35,
164 &ega_emulops,
165 8, 10,
166 WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
167 }, ega_35lscreen_bf = {
168 "80x35bf", 80, 35,
169 &ega_emulops,
170 8, 10,
171 WSSCREEN_WSCOLORS | WSSCREEN_BLINK
172 }, ega_43lscreen = {
173 "80x43", 80, 43,
174 &ega_emulops,
175 8, 8,
176 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
177 }, ega_43lscreen_mono = {
178 "80x43", 80, 43,
179 &ega_emulops,
180 8, 8,
181 WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
182 }, ega_43lscreen_bf = {
183 "80x43bf", 80, 43,
184 &ega_emulops,
185 8, 8,
186 WSSCREEN_WSCOLORS | WSSCREEN_BLINK
187 };
188
189 #define VGA_SCREEN_CANTWOFONTS(type) (!((type)->capabilities & WSSCREEN_HILIT))
190
191 const struct wsscreen_descr *_ega_scrlist[] = {
192 &ega_stdscreen,
193 &ega_stdscreen_bf,
194 &ega_35lscreen,
195 &ega_35lscreen_bf,
196 &ega_43lscreen,
197 &ega_43lscreen_bf,
198 }, *_ega_scrlist_mono[] = {
199 &ega_stdscreen_mono,
200 &ega_35lscreen_mono,
201 &ega_43lscreen_mono,
202 };
203
204
205 const struct wsscreen_list ega_screenlist = {
206 sizeof(_ega_scrlist) / sizeof(struct wsscreen_descr *),
207 _ega_scrlist
208 }, ega_screenlist_mono = {
209 sizeof(_ega_scrlist_mono) / sizeof(struct wsscreen_descr *),
210 _ega_scrlist_mono
211 };
212
213 static int ega_ioctl(void *, void *, u_long, void *, int, struct proc *);
214 static paddr_t ega_mmap(void *, void *, off_t, int);
215 static int ega_alloc_screen(void *, const struct wsscreen_descr *,
216 void **, int *, int *, long *);
217 static void ega_free_screen(void *, void *);
218 static int ega_show_screen(void *, void *, int,
219 void (*) (void *, int, int), void *);
220 static int ega_load_font(void *, void *, struct wsdisplay_font *);
221
222 void ega_doswitch(struct ega_config *);
223
224 const struct wsdisplay_accessops ega_accessops = {
225 ega_ioctl,
226 ega_mmap,
227 ega_alloc_screen,
228 ega_free_screen,
229 ega_show_screen,
230 ega_load_font
231 };
232
233 static int
234 ega_probe_col(bus_space_tag_t iot, bus_space_tag_t memt)
235 {
236 bus_space_handle_t memh, ioh_6845;
237 u_int16_t oldval, val;
238
239 if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
240 return (0);
241 oldval = bus_space_read_2(memt, memh, 0);
242 bus_space_write_2(memt, memh, 0, 0xa55a);
243 val = bus_space_read_2(memt, memh, 0);
244 bus_space_write_2(memt, memh, 0, oldval);
245 bus_space_unmap(memt, memh, 0x8000);
246 if (val != 0xa55a)
247 return (0);
248
249 if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
250 return (0);
251 bus_space_unmap(iot, ioh_6845, 0x10);
252
253 return (1);
254 }
255
256 static int
257 ega_probe_mono(bus_space_tag_t iot, bus_space_tag_t memt)
258 {
259 bus_space_handle_t memh, ioh_6845;
260 u_int16_t oldval, val;
261
262 if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
263 return (0);
264 oldval = bus_space_read_2(memt, memh, 0);
265 bus_space_write_2(memt, memh, 0, 0xa55a);
266 val = bus_space_read_2(memt, memh, 0);
267 bus_space_write_2(memt, memh, 0, oldval);
268 bus_space_unmap(memt, memh, 0x8000);
269 if (val != 0xa55a)
270 return (0);
271
272 if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
273 return (0);
274 bus_space_unmap(iot, ioh_6845, 0x10);
275
276 return (1);
277 }
278 /*
279 * We want at least ASCII 32..127 be present in the
280 * first font slot.
281 */
282 #define vga_valid_primary_font(f) \
283 (f->encoding == WSDISPLAY_FONTENC_IBM || \
284 f->encoding == WSDISPLAY_FONTENC_ISO)
285
286 int
287 ega_selectfont(struct ega_config *vc, struct egascreen *scr, char *name1, char *name2)
288 /* name1, *name2: NULL: take first found */
289 {
290 const struct wsscreen_descr *type = scr->pcs.type;
291 struct egafont *f1, *f2;
292 int i;
293
294 f1 = f2 = 0;
295
296 for (i = 0; i < 4; i++) {
297 struct egafont *f = vc->vc_fonts[i];
298 if (!f || f->height != type->fontheight)
299 continue;
300 if (!f1 &&
301 vga_valid_primary_font(f) &&
302 (!name1 || !strcmp(name1, f->name))) {
303 f1 = f;
304 continue;
305 }
306 if (!f2 &&
307 VGA_SCREEN_CANTWOFONTS(type) &&
308 (!name2 || !strcmp(name2, f->name))) {
309 f2 = f;
310 continue;
311 }
312 }
313
314 /*
315 * The request fails if no primary font was found,
316 * or if a second font was requested but not found.
317 */
318 if (f1 && (!name2 || f2)) {
319 #ifdef EGAFONTDEBUG
320 if (scr != &ega_console_screen || ega_console_attached) {
321 printf("ega (%s): font1=%s (slot %d)", type->name,
322 f1->name, f1->slot);
323 if (f2)
324 printf(", font2=%s (slot %d)",
325 f2->name, f2->slot);
326 printf("\n");
327 }
328 #endif
329 scr->fontset1 = f1;
330 scr->fontset2 = f2;
331 return (0);
332 }
333 return (ENXIO);
334 }
335
336 void
337 ega_init_screen(struct ega_config *vc, struct egascreen *scr, const struct wsscreen_descr *type, int existing, long *attrp)
338 {
339 int cpos;
340 int res;
341
342 scr->cfg = vc;
343 scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
344 scr->pcs.type = type;
345 scr->pcs.active = 0;
346 scr->mindispoffset = 0;
347 scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
348
349 if (existing) {
350 cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
351 cpos |= vga_6845_read(&vc->hdl, cursorl);
352
353 /* make sure we have a valid cursor position */
354 if (cpos < 0 || cpos >= type->nrows * type->ncols)
355 cpos = 0;
356
357 scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
358 scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
359
360 /* make sure we have a valid memory offset */
361 if (scr->pcs.dispoffset < scr->mindispoffset ||
362 scr->pcs.dispoffset > scr->maxdispoffset)
363 scr->pcs.dispoffset = scr->mindispoffset;
364 } else {
365 cpos = 0;
366 scr->pcs.dispoffset = scr->mindispoffset;
367 }
368
369 scr->pcs.cursorrow = cpos / type->ncols;
370 scr->pcs.cursorcol = cpos % type->ncols;
371 pcdisplay_cursor_init(&scr->pcs, existing);
372
373 res = ega_allocattr(scr, 0, 0, 0, attrp);
374 #ifdef DIAGNOSTIC
375 if (res)
376 panic("ega_init_screen: attribute botch");
377 #endif
378
379 scr->pcs.mem = NULL;
380
381 scr->fontset1 = scr->fontset2 = 0;
382 if (ega_selectfont(vc, scr, 0, 0)) {
383 if (scr == &ega_console_screen)
384 panic("ega_init_screen: no font");
385 else
386 printf("ega_init_screen: no font\n");
387 }
388
389 vc->nscreens++;
390 LIST_INSERT_HEAD(&vc->screens, scr, next);
391 }
392
393 static void
394 ega_init(struct ega_config *vc, bus_space_tag_t iot, bus_space_tag_t memt, int mono)
395 {
396 struct vga_handle *vh = &vc->hdl;
397 int i;
398
399 vh->vh_iot = iot;
400 vh->vh_memt = memt;
401 vh->vh_mono = mono;
402
403 if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
404 panic("ega_common_setup: couldn't map ega io");
405
406 if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
407 &vh->vh_ioh_6845))
408 panic("ega_common_setup: couldn't map 6845 io");
409
410 if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
411 panic("ega_common_setup: couldn't map memory");
412
413 if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
414 (vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
415 &vh->vh_memh))
416 panic("ega_common_setup: mem subrange failed");
417
418 vc->nscreens = 0;
419 LIST_INIT(&vc->screens);
420 vc->active = NULL;
421 vc->currenttype = vh->vh_mono ? &ega_stdscreen_mono : &ega_stdscreen;
422 callout_init(&vc->switch_callout, 0);
423
424 vc->vc_fonts[0] = &ega_builtinfont;
425 for (i = 1; i < 4; i++)
426 vc->vc_fonts[i] = 0;
427
428 vc->currentfontset1 = vc->currentfontset2 = 0;
429 }
430
431 int
432 ega_match(struct device *parent, struct cfdata *match, void *aux)
433 {
434 struct isa_attach_args *ia = aux;
435 int mono;
436
437 if (ia->ia_nio < 1)
438 return (0);
439
440 if (ia->ia_iomem < 1)
441 return (0);
442
443 if (ia->ia_nirq < 1)
444 return (0);
445
446 if (ia->ia_ndrq < 1)
447 return (0);
448
449 if (ISA_DIRECT_CONFIG(ia))
450 return (0);
451
452 /* If values are hardwired to something that they can't be, punt. */
453 if ((ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
454 ia->ia_io[0].ir_addr != 0x3d0 &&
455 ia->ia_io[0].ir_addr != 0x3b0) ||
456 /* ia->ia_io[0].ir_size != 0 || XXX isa.c */
457 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
458 ia->ia_iomem[0].ir_addr != 0xb8000 &&
459 ia->ia_iomem[0].ir_addr != 0xb0000) ||
460 (ia->ia_iomem[0].ir_size != 0 &&
461 ia->ia_iomem[0].ir_size != 0x8000) ||
462 ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ ||
463 ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)
464 return (0);
465
466 if (ega_is_console(ia->ia_iot))
467 mono = ega_console_dc.hdl.vh_mono;
468 else if (ia->ia_io[0].ir_addr != 0x3b0 &&
469 ia->ia_iomem[0].ir_addr != 0xb0000 &&
470 ega_probe_col(ia->ia_iot, ia->ia_memt))
471 mono = 0;
472 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
473 ia->ia_iomem[0].ir_addr != 0xb8000 &&
474 ega_probe_mono(ia->ia_iot, ia->ia_memt))
475 mono = 1;
476 else
477 return (0);
478
479 ia->ia_io[0].ir_addr = mono ? 0x3b0 : 0x3d0;
480 ia->ia_io[0].ir_size = 0x10;
481 ia->ia_iomem[0].ir_addr = mono ? 0xb0000 : 0xb8000;
482 ia->ia_iomem[0].ir_size = 0x8000;
483 return (2); /* beat pcdisplay */
484 }
485
486 void
487 ega_attach(struct device *parent, struct device *self, void *aux)
488 {
489 struct isa_attach_args *ia = aux;
490 struct ega_softc *sc = (struct ega_softc *)self;
491 int console;
492 struct ega_config *dc;
493 struct wsemuldisplaydev_attach_args aa;
494
495 printf("\n");
496
497 console = ega_is_console(ia->ia_iot);
498
499 if (console) {
500 dc = &ega_console_dc;
501 sc->nscreens = 1;
502 ega_console_attached = 1;
503 } else {
504 dc = malloc(sizeof(struct ega_config),
505 M_DEVBUF, M_WAITOK);
506 if (ia->ia_io[0].ir_addr != 0x3b0 &&
507 ia->ia_iomem[0].ir_addr != 0xb0000 &&
508 ega_probe_col(ia->ia_iot, ia->ia_memt))
509 ega_init(dc, ia->ia_iot, ia->ia_memt, 0);
510 else if (ia->ia_io[0].ir_addr != 0x3d0 &&
511 ia->ia_iomem[0].ir_addr != 0xb8000 &&
512 ega_probe_mono(ia->ia_iot, ia->ia_memt))
513 ega_init(dc, ia->ia_iot, ia->ia_memt, 1);
514 else
515 panic("ega_attach: display disappeared");
516 }
517 sc->sc_dc = dc;
518
519 aa.console = console;
520 aa.scrdata = &ega_screenlist;
521 aa.accessops = &ega_accessops;
522 aa.accesscookie = dc;
523
524 config_found(self, &aa, wsemuldisplaydevprint);
525 }
526
527
528 int
529 ega_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
530 {
531 int mono;
532 long defattr;
533 const struct wsscreen_descr *scr;
534
535 if (ega_probe_col(iot, memt))
536 mono = 0;
537 else if (ega_probe_mono(iot, memt))
538 mono = 1;
539 else
540 return (ENXIO);
541
542 ega_init(&ega_console_dc, iot, memt, mono);
543 scr = ega_console_dc.currenttype;
544 ega_init_screen(&ega_console_dc, &ega_console_screen, scr, 1, &defattr);
545
546 ega_console_screen.pcs.active = 1;
547 ega_console_dc.active = &ega_console_screen;
548
549 wsdisplay_cnattach(scr, &ega_console_screen,
550 ega_console_screen.pcs.cursorcol,
551 ega_console_screen.pcs.cursorrow,
552 defattr);
553
554 egaconsole = 1;
555 return (0);
556 }
557
558 static int
559 ega_is_console(bus_space_tag_t iot)
560 {
561 if (egaconsole &&
562 !ega_console_attached &&
563 iot == ega_console_dc.hdl.vh_iot)
564 return (1);
565 return (0);
566 }
567
568 static int
569 ega_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct proc *p)
570 {
571 /*
572 * XXX "do something!"
573 */
574 return (EPASSTHROUGH);
575 }
576
577 static paddr_t
578 ega_mmap(void *v, void *vs, off_t offset, int prot)
579 {
580 return (-1);
581 }
582
583 static int
584 ega_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep, int *curxp, int *curyp, long *defattrp)
585 {
586 struct ega_config *vc = v;
587 struct egascreen *scr;
588
589 if (vc->nscreens == 1) {
590 /*
591 * When allocating the second screen, get backing store
592 * for the first one too.
593 * XXX We could be more clever and use video RAM.
594 */
595 vc->screens.lh_first->pcs.mem =
596 malloc(type->ncols * type->nrows * 2, M_DEVBUF, M_WAITOK);
597 }
598
599 scr = malloc(sizeof(struct egascreen), M_DEVBUF, M_WAITOK);
600 ega_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
601
602 if (vc->nscreens == 1) {
603 scr->pcs.active = 1;
604 vc->active = scr;
605 vc->currenttype = type;
606 } else {
607 scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
608 M_DEVBUF, M_WAITOK);
609 pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
610 }
611
612 *cookiep = scr;
613 *curxp = scr->pcs.cursorcol;
614 *curyp = scr->pcs.cursorrow;
615 return (0);
616 }
617
618 static void
619 ega_free_screen(void *v, void *cookie)
620 {
621 struct egascreen *vs = cookie;
622 struct ega_config *vc = vs->cfg;
623
624 LIST_REMOVE(vs, next);
625 if (vs != &ega_console_screen)
626 free(vs, M_DEVBUF);
627 else
628 panic("ega_free_screen: console");
629
630 if (vc->active == vs)
631 vc->active = 0;
632 }
633
634 static void
635 ega_setfont(struct ega_config *vc, struct egascreen *scr)
636 {
637 int fontslot1, fontslot2;
638
639 fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
640 fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
641 if (vc->currentfontset1 != fontslot1 ||
642 vc->currentfontset2 != fontslot2) {
643 vga_setfontset(&vc->hdl, 2 * fontslot1, 2 * fontslot2);
644 vc->currentfontset1 = fontslot1;
645 vc->currentfontset2 = fontslot2;
646 }
647 }
648
649 static int
650 ega_show_screen(v, cookie, waitok, cb, cbarg)
651 void *v;
652 void *cookie;
653 int waitok;
654 void (*cb)(void *, int, int);
655 void *cbarg;
656 {
657 struct egascreen *scr = cookie, *oldscr;
658 struct ega_config *vc = scr->cfg;
659
660 oldscr = vc->active; /* can be NULL! */
661 if (scr == oldscr) {
662 return (0);
663 }
664
665 vc->wantedscreen = cookie;
666 vc->switchcb = cb;
667 vc->switchcbarg = cbarg;
668 if (cb) {
669 callout_reset(&vc->switch_callout, 0,
670 (void(*)(void *))ega_doswitch);
671 return (EAGAIN);
672 }
673
674 ega_doswitch(vc);
675 return (0);
676 }
677
678 void
679 ega_doswitch(struct ega_config *vc)
680 {
681 struct egascreen *scr, *oldscr;
682 struct vga_handle *vh = &vc->hdl;
683 const struct wsscreen_descr *type;
684
685 scr = vc->wantedscreen;
686 if (!scr) {
687 printf("ega_doswitch: disappeared\n");
688 (*vc->switchcb)(vc->switchcbarg, EIO, 0);
689 return;
690 }
691 type = scr->pcs.type;
692 oldscr = vc->active; /* can be NULL! */
693 #ifdef DIAGNOSTIC
694 if (oldscr) {
695 if (!oldscr->pcs.active)
696 panic("ega_show_screen: not active");
697 if (oldscr->pcs.type != vc->currenttype)
698 panic("ega_show_screen: bad type");
699 }
700 #endif
701 if (scr == oldscr) {
702 return;
703 }
704 #ifdef DIAGNOSTIC
705 if (scr->pcs.active)
706 panic("ega_show_screen: active");
707 #endif
708
709 if (oldscr) {
710 const struct wsscreen_descr *oldtype = oldscr->pcs.type;
711
712 oldscr->pcs.active = 0;
713 bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
714 oldscr->pcs.dispoffset, oldscr->pcs.mem,
715 oldtype->ncols * oldtype->nrows);
716 }
717
718 if (vc->currenttype != type) {
719 vga_setscreentype(vh, type);
720 vc->currenttype = type;
721 }
722
723 ega_setfont(vc, scr);
724 /* XXX swich colours! */
725
726 scr->pcs.dispoffset = scr->mindispoffset;
727 if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
728 vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
729 vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
730 }
731
732 bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
733 scr->pcs.dispoffset, scr->pcs.mem,
734 type->ncols * type->nrows);
735 scr->pcs.active = 1;
736
737 vc->active = scr;
738
739 pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
740 scr->pcs.cursorrow, scr->pcs.cursorcol);
741
742 vc->wantedscreen = 0;
743 if (vc->switchcb)
744 (*vc->switchcb)(vc->switchcbarg, 0, 0);
745 }
746
747 static int
748 ega_load_font(void *v, void *cookie, struct wsdisplay_font *data)
749 {
750 struct ega_config *vc = v;
751 struct egascreen *scr = cookie;
752 char *name2;
753 int res, slot;
754 struct egafont *f;
755
756 if (scr) {
757 name2 = strchr(data->name, ',');
758 if (name2)
759 *name2++ = '\0';
760 res = ega_selectfont(vc, scr, data->name, name2);
761 if (!res)
762 ega_setfont(vc, scr);
763 return (res);
764 }
765
766 if (data->fontwidth != 8 || data->stride != 1)
767 return (EINVAL); /* XXX 1 byte per line */
768 if (data->firstchar != 0 || data->numchars != 256)
769 return (EINVAL);
770 #ifndef WSCONS_SUPPORT_PCVTFONTS
771 if (data->encoding == WSDISPLAY_FONTENC_PCVT) {
772 printf("vga: pcvt font support not built in, see vga(4)\n");
773 return (EINVAL);
774 }
775 #endif
776
777 for (slot = 0; slot < 4; slot++)
778 if (!vc->vc_fonts[slot])
779 break;
780 if (slot == 4)
781 return (ENOSPC);
782
783 f = malloc(sizeof(struct egafont), M_DEVBUF, M_WAITOK);
784 strncpy(f->name, data->name, sizeof(f->name));
785 f->height = data->fontheight;
786 f->encoding = data->encoding;
787 #ifdef notyet
788 f->firstchar = data->firstchar;
789 f->numchars = data->numchars;
790 #endif
791 #ifdef EGAFONTDEBUG
792 printf("ega: load %s (8x%d, enc %d) font to slot %d\n", f->name,
793 f->height, f->encoding, slot);
794 #endif
795 vga_loadchars(&vc->hdl, 2 * slot, 0, 256, f->height, data->data);
796 f->slot = slot;
797 vc->vc_fonts[slot] = f;
798
799 return (0);
800 }
801
802 static int
803 ega_allocattr(void *id, int fg, int bg, int flags, long *attrp)
804 {
805 struct egascreen *scr = id;
806 struct ega_config *vc = scr->cfg;
807
808 if (__predict_false((unsigned int)fg >= sizeof(fgansitopc) ||
809 (unsigned int)bg >= sizeof(bgansitopc)))
810 return (EINVAL);
811
812 if (vc->hdl.vh_mono) {
813 if (flags & WSATTR_WSCOLORS)
814 return (EINVAL);
815 if (flags & WSATTR_REVERSE)
816 *attrp = 0x70;
817 else
818 *attrp = 0x07;
819 if (flags & WSATTR_UNDERLINE)
820 *attrp |= FG_UNDERLINE;
821 if (flags & WSATTR_HILIT)
822 *attrp |= FG_INTENSE;
823 } else {
824 if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
825 return (EINVAL);
826 if (flags & WSATTR_WSCOLORS)
827 *attrp = fgansitopc[fg] | bgansitopc[bg];
828 else
829 *attrp = 7;
830 if (flags & WSATTR_HILIT)
831 *attrp += 8;
832 }
833 if (flags & WSATTR_BLINK)
834 *attrp |= FG_BLINK;
835 return (0);
836 }
837
838 void
839 ega_copyrows(void *id, int srcrow, int dstrow, int nrows)
840 {
841 struct egascreen *scr = id;
842 bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
843 bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
844 int ncols = scr->pcs.type->ncols;
845 bus_size_t srcoff, dstoff;
846
847 srcoff = srcrow * ncols + 0;
848 dstoff = dstrow * ncols + 0;
849
850 if (scr->pcs.active) {
851 if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
852 #ifdef PCDISPLAY_SOFTCURSOR
853 int cursoron = scr->pcs.cursoron;
854
855 if (cursoron)
856 pcdisplay_cursor(&scr->pcs, 0,
857 scr->pcs.cursorrow, scr->pcs.cursorcol);
858 #endif
859 /* scroll up whole screen */
860 if ((scr->pcs.dispoffset + srcrow * ncols * 2)
861 <= scr->maxdispoffset) {
862 scr->pcs.dispoffset += srcrow * ncols * 2;
863 } else {
864 bus_space_copy_region_2(memt, memh,
865 scr->pcs.dispoffset + srcoff * 2,
866 memh, scr->mindispoffset,
867 nrows * ncols);
868 scr->pcs.dispoffset = scr->mindispoffset;
869 }
870 vga_6845_write(&scr->cfg->hdl, startadrh,
871 scr->pcs.dispoffset >> 9);
872 vga_6845_write(&scr->cfg->hdl, startadrl,
873 scr->pcs.dispoffset >> 1);
874 #ifdef PCDISPLAY_SOFTCURSOR
875 if (cursoron)
876 pcdisplay_cursor(&scr->pcs, 1,
877 scr->pcs.cursorrow, scr->pcs.cursorcol);
878 #endif
879 } else {
880 bus_space_copy_region_2(memt, memh,
881 scr->pcs.dispoffset + srcoff * 2,
882 memh, scr->pcs.dispoffset + dstoff * 2,
883 nrows * ncols);
884 }
885 } else
886 memcpy(&scr->pcs.mem[dstoff], &scr->pcs.mem[srcoff],
887 nrows * ncols * 2);
888 }
889