Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / video / i386 / vesa / vesa.c
... / ...
CommitLineData
1/*-
2 * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/i386/isa/vesa.c,v 1.32.2.1 2002/08/13 02:42:33 rwatson Exp $
27 */
28
29#include "opt_vga.h"
30
31#ifndef VGA_NO_MODE_CHANGE
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/malloc.h>
38#include <sys/fbio.h>
39
40#include <vm/vm.h>
41#include <vm/vm_extern.h>
42#include <vm/vm_kern.h>
43#include <vm/pmap.h>
44
45#include <machine/md_var.h>
46#include <machine/vm86.h>
47#include <machine/pc/bios.h>
48#include <machine/pc/vesa.h>
49
50#include <dev/fb/fbreg.h>
51#include <dev/fb/vgareg.h>
52
53#ifndef __i386__
54#include <isa/isareg.h>
55#else
56#include <i386/isa/isa.h>
57#endif
58
59#ifndef VESA_DEBUG
60#define VESA_DEBUG 0
61#endif
62
63/* VESA video adapter state buffer stub */
64struct adp_state {
65 int sig;
66#define V_STATE_SIG 0x61736576
67 u_char regs[1];
68};
69typedef struct adp_state adp_state_t;
70
71/* VESA video adapter */
72static video_adapter_t *vesa_adp = NULL;
73static int vesa_state_buf_size = 0;
74
75/* VESA functions */
76#if 0
77static int vesa_nop(void);
78#endif
79static int vesa_error(void);
80static vi_probe_t vesa_probe;
81static vi_init_t vesa_init;
82static vi_get_info_t vesa_get_info;
83static vi_query_mode_t vesa_query_mode;
84static vi_set_mode_t vesa_set_mode;
85static vi_save_font_t vesa_save_font;
86static vi_load_font_t vesa_load_font;
87static vi_show_font_t vesa_show_font;
88static vi_save_palette_t vesa_save_palette;
89static vi_load_palette_t vesa_load_palette;
90static vi_set_border_t vesa_set_border;
91static vi_save_state_t vesa_save_state;
92static vi_load_state_t vesa_load_state;
93static vi_set_win_org_t vesa_set_origin;
94static vi_read_hw_cursor_t vesa_read_hw_cursor;
95static vi_set_hw_cursor_t vesa_set_hw_cursor;
96static vi_set_hw_cursor_shape_t vesa_set_hw_cursor_shape;
97static vi_blank_display_t vesa_blank_display;
98static vi_mmap_t vesa_mmap;
99static vi_ioctl_t vesa_ioctl;
100static vi_clear_t vesa_clear;
101static vi_fill_rect_t vesa_fill_rect;
102static vi_bitblt_t vesa_bitblt;
103static vi_diag_t vesa_diag;
104static int vesa_bios_info(int level);
105static struct vm86context vesa_vmcontext;
106
107static video_switch_t vesavidsw = {
108 vesa_probe,
109 vesa_init,
110 vesa_get_info,
111 vesa_query_mode,
112 vesa_set_mode,
113 vesa_save_font,
114 vesa_load_font,
115 vesa_show_font,
116 vesa_save_palette,
117 vesa_load_palette,
118 vesa_set_border,
119 vesa_save_state,
120 vesa_load_state,
121 vesa_set_origin,
122 vesa_read_hw_cursor,
123 vesa_set_hw_cursor,
124 vesa_set_hw_cursor_shape,
125 vesa_blank_display,
126 vesa_mmap,
127 vesa_ioctl,
128 vesa_clear,
129 vesa_fill_rect,
130 vesa_bitblt,
131 vesa_error,
132 vesa_error,
133 vesa_diag,
134};
135
136static video_switch_t *prevvidsw;
137
138/* VESA BIOS video modes */
139#define VESA_MAXMODES 64
140#define EOT (-1)
141#define NA (-2)
142
143#define MODE_TABLE_DELTA 8
144
145static int vesa_vmode_max = 0;
146static video_info_t vesa_vmode_empty = { EOT };
147static video_info_t *vesa_vmode = &vesa_vmode_empty;
148
149static int vesa_init_done = FALSE;
150static int has_vesa_bios = FALSE;
151static struct vesa_info *vesa_adp_info = NULL;
152static u_int16_t *vesa_vmodetab = NULL;
153static char *vesa_oemstr = NULL;
154static char *vesa_venderstr = NULL;
155static char *vesa_prodstr = NULL;
156static char *vesa_revstr = NULL;
157
158/* local macros and functions */
159#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
160
161static int int10_set_mode(int mode);
162static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
163static int vesa_bios_set_mode(int mode);
164static int vesa_bios_get_dac(void);
165static int vesa_bios_set_dac(int bits);
166static int vesa_bios_save_palette(int start, int colors, u_char *palette,
167 int bits);
168static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
169 u_char *b, int bits);
170static int vesa_bios_load_palette(int start, int colors, u_char *palette,
171 int bits);
172#ifdef notyet
173static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
174 u_char *b, int bits);
175#endif
176#define STATE_SIZE 0
177#define STATE_SAVE 1
178#define STATE_LOAD 2
179#define STATE_HW (1<<0)
180#define STATE_DATA (1<<1)
181#define STATE_DAC (1<<2)
182#define STATE_REG (1<<3)
183#define STATE_MOST (STATE_HW | STATE_DATA | STATE_REG)
184#define STATE_ALL (STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
185static int vesa_bios_state_buf_size(void);
186static int vesa_bios_save_restore(int code, void *p, size_t size);
187static int vesa_bios_get_line_length(void);
188static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
189#if 0
190static int vesa_bios_get_start(int *x, int *y);
191#endif
192static int vesa_bios_set_start(int x, int y);
193static int vesa_map_gen_mode_num(int type, int color, int mode);
194static int vesa_translate_flags(u_int16_t vflags);
195static int vesa_translate_mmodel(u_int8_t vmodel);
196static void *vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off,
197 u_char *buf);
198static int vesa_bios_init(void);
199static void vesa_clear_modes(video_info_t *info, int color);
200static vm_offset_t vesa_map_buffer(u_int paddr, size_t size);
201static void vesa_unmap_buffer(vm_offset_t vaddr, size_t size);
202
203#if 0
204static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
205#endif
206
207static void
208dump_buffer(u_char *buf, size_t len)
209{
210 int i;
211
212 for(i = 0; i < len;) {
213 printf("%02x ", buf[i]);
214 if ((++i % 16) == 0)
215 printf("\n");
216 }
217}
218
219/* INT 10 BIOS calls */
220static int
221int10_set_mode(int mode)
222{
223 struct vm86frame vmf;
224
225 bzero(&vmf, sizeof(vmf));
226 vmf.vmf_eax = 0x0000 | mode;
227 vm86_intcall(0x10, &vmf);
228 return 0;
229}
230
231/* VESA BIOS calls */
232static int
233vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
234{
235 struct vm86frame vmf;
236 u_char *buf;
237 int err;
238
239 bzero(&vmf, sizeof(vmf));
240 vmf.vmf_eax = 0x4f01;
241 vmf.vmf_ecx = mode;
242 buf = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
243 vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_di);
244
245 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
246 if ((err != 0) || (vmf.vmf_ax != 0x4f))
247 return 1;
248 bcopy(buf, vmode, sizeof(*vmode));
249 return 0;
250}
251
252static int
253vesa_bios_set_mode(int mode)
254{
255 struct vm86frame vmf;
256 int err;
257
258 bzero(&vmf, sizeof(vmf));
259 vmf.vmf_eax = 0x4f02;
260 vmf.vmf_ebx = mode;
261 err = vm86_intcall(0x10, &vmf);
262 return ((err != 0) || (vmf.vmf_ax != 0x4f));
263}
264
265static int
266vesa_bios_get_dac(void)
267{
268 struct vm86frame vmf;
269 int err;
270
271 bzero(&vmf, sizeof(vmf));
272 vmf.vmf_eax = 0x4f08;
273 vmf.vmf_ebx = 1; /* get DAC width */
274 err = vm86_intcall(0x10, &vmf);
275 if ((err != 0) || (vmf.vmf_ax != 0x4f))
276 return 6; /* XXX */
277 return ((vmf.vmf_ebx >> 8) & 0x00ff);
278}
279
280static int
281vesa_bios_set_dac(int bits)
282{
283 struct vm86frame vmf;
284 int err;
285
286 bzero(&vmf, sizeof(vmf));
287 vmf.vmf_eax = 0x4f08;
288 vmf.vmf_ebx = (bits << 8);
289 err = vm86_intcall(0x10, &vmf);
290 if ((err != 0) || (vmf.vmf_ax != 0x4f))
291 return 6; /* XXX */
292 return ((vmf.vmf_ebx >> 8) & 0x00ff);
293}
294
295static int
296vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
297{
298 struct vm86frame vmf;
299 u_char *p;
300 int err;
301 int i;
302
303 bzero(&vmf, sizeof(vmf));
304 vmf.vmf_eax = 0x4f09;
305 vmf.vmf_ebx = 1; /* get primary palette data */
306 vmf.vmf_ecx = colors;
307 vmf.vmf_edx = start;
308 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
309 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
310
311 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
312 if ((err != 0) || (vmf.vmf_ax != 0x4f))
313 return 1;
314
315 bits = 8 - bits;
316 for (i = 0; i < colors; ++i) {
317 palette[i*3] = p[i*4 + 2] << bits;
318 palette[i*3 + 1] = p[i*4 + 1] << bits;
319 palette[i*3 + 2] = p[i*4] << bits;
320 }
321 return 0;
322}
323
324static int
325vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
326 int bits)
327{
328 struct vm86frame vmf;
329 u_char *p;
330 int err;
331 int i;
332
333 bzero(&vmf, sizeof(vmf));
334 vmf.vmf_eax = 0x4f09;
335 vmf.vmf_ebx = 1; /* get primary palette data */
336 vmf.vmf_ecx = colors;
337 vmf.vmf_edx = start;
338 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
339 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
340
341 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
342 if ((err != 0) || (vmf.vmf_ax != 0x4f))
343 return 1;
344
345 bits = 8 - bits;
346 for (i = 0; i < colors; ++i) {
347 r[i] = p[i*4 + 2] << bits;
348 g[i] = p[i*4 + 1] << bits;
349 b[i] = p[i*4] << bits;
350 }
351 return 0;
352}
353
354static int
355vesa_bios_load_palette(int start, int colors, u_char *palette, int bits)
356{
357 struct vm86frame vmf;
358 u_char *p;
359 int err;
360 int i;
361
362 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
363 bits = 8 - bits;
364 for (i = 0; i < colors; ++i) {
365 p[i*4] = palette[i*3 + 2] >> bits;
366 p[i*4 + 1] = palette[i*3 + 1] >> bits;
367 p[i*4 + 2] = palette[i*3] >> bits;
368 p[i*4 + 3] = 0;
369 }
370
371 bzero(&vmf, sizeof(vmf));
372 vmf.vmf_eax = 0x4f09;
373 vmf.vmf_ebx = 0; /* set primary palette data */
374 vmf.vmf_ecx = colors;
375 vmf.vmf_edx = start;
376 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
377
378 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
379 return ((err != 0) || (vmf.vmf_ax != 0x4f));
380}
381
382#ifdef notyet
383static int
384vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
385 int bits)
386{
387 struct vm86frame vmf;
388 u_char *p;
389 int err;
390 int i;
391
392 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
393 bits = 8 - bits;
394 for (i = 0; i < colors; ++i) {
395 p[i*4] = b[i] >> bits;
396 p[i*4 + 1] = g[i] >> bits;
397 p[i*4 + 2] = r[i] >> bits;
398 p[i*4 + 3] = 0;
399 }
400
401 bzero(&vmf, sizeof(vmf));
402 vmf.vmf_eax = 0x4f09;
403 vmf.vmf_ebx = 0; /* set primary palette data */
404 vmf.vmf_ecx = colors;
405 vmf.vmf_edx = start;
406 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
407
408 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
409 return ((err != 0) || (vmf.vmf_ax != 0x4f));
410}
411#endif
412
413static int
414vesa_bios_state_buf_size(void)
415{
416 struct vm86frame vmf;
417 int err;
418
419 bzero(&vmf, sizeof(vmf));
420 vmf.vmf_eax = 0x4f04;
421 vmf.vmf_ecx = STATE_MOST;
422 vmf.vmf_edx = STATE_SIZE;
423 err = vm86_intcall(0x10, &vmf);
424 if ((err != 0) || (vmf.vmf_ax != 0x4f))
425 return 0;
426 return vmf.vmf_bx*64;
427}
428
429static int
430vesa_bios_save_restore(int code, void *p, size_t size)
431{
432 struct vm86frame vmf;
433 u_char *buf;
434 int err;
435
436 bzero(&vmf, sizeof(vmf));
437 vmf.vmf_eax = 0x4f04;
438 vmf.vmf_ecx = STATE_MOST;
439 vmf.vmf_edx = code; /* STATE_SAVE/STATE_LOAD */
440 buf = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
441 vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_di);
442 bcopy(p, buf, size);
443
444 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
445 return ((err != 0) || (vmf.vmf_ax != 0x4f));
446}
447
448static int
449vesa_bios_get_line_length(void)
450{
451 struct vm86frame vmf;
452 int err;
453
454 bzero(&vmf, sizeof(vmf));
455 vmf.vmf_eax = 0x4f06;
456 vmf.vmf_ebx = 1; /* get scan line length */
457 err = vm86_intcall(0x10, &vmf);
458 if ((err != 0) || (vmf.vmf_ax != 0x4f))
459 return -1;
460 return vmf.vmf_bx; /* line length in bytes */
461}
462
463static int
464vesa_bios_set_line_length(int pixel, int *bytes, int *lines)
465{
466 struct vm86frame vmf;
467 int err;
468
469 bzero(&vmf, sizeof(vmf));
470 vmf.vmf_eax = 0x4f06;
471 vmf.vmf_ebx = 0; /* set scan line length in pixel */
472 vmf.vmf_ecx = pixel;
473 err = vm86_intcall(0x10, &vmf);
474#if VESA_DEBUG > 1
475 printf("bx:%d, cx:%d, dx:%d\n", vmf.vmf_bx, vmf.vmf_cx, vmf.vmf_dx);
476#endif
477 if ((err != 0) || (vmf.vmf_ax != 0x4f))
478 return 1;
479 if (bytes)
480 *bytes = vmf.vmf_bx;
481 if (lines)
482 *lines = vmf.vmf_dx;
483 return 0;
484}
485
486#if 0
487static int
488vesa_bios_get_start(int *x, int *y)
489{
490 struct vm86frame vmf;
491 int err;
492
493 bzero(&vmf, sizeof(vmf));
494 vmf.vmf_eax = 0x4f07;
495 vmf.vmf_ebx = 1; /* get display start */
496 err = vm86_intcall(0x10, &vmf);
497 if ((err != 0) || (vmf.vmf_ax != 0x4f))
498 return 1;
499 *x = vmf.vmf_cx;
500 *y = vmf.vmf_dx;
501 return 0;
502}
503#endif
504
505static int
506vesa_bios_set_start(int x, int y)
507{
508 struct vm86frame vmf;
509 int err;
510
511 bzero(&vmf, sizeof(vmf));
512 vmf.vmf_eax = 0x4f07;
513 vmf.vmf_ebx = 0x80; /* set display start */
514 vmf.vmf_edx = y;
515 vmf.vmf_ecx = x;
516 err = vm86_intcall(0x10, &vmf);
517 return ((err != 0) || (vmf.vmf_ax != 0x4f));
518}
519
520/* map a generic video mode to a known mode */
521static int
522vesa_map_gen_mode_num(int type, int color, int mode)
523{
524 static struct {
525 int from;
526 int to;
527 } mode_map[] = {
528 { M_TEXT_132x25, M_VESA_C132x25 },
529 { M_TEXT_132x43, M_VESA_C132x43 },
530 { M_TEXT_132x50, M_VESA_C132x50 },
531 { M_TEXT_132x60, M_VESA_C132x60 },
532 };
533 int i;
534
535 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
536 if (mode_map[i].from == mode)
537 return mode_map[i].to;
538 }
539 return mode;
540}
541
542static int
543vesa_translate_flags(u_int16_t vflags)
544{
545 static struct {
546 u_int16_t mask;
547 int set;
548 int reset;
549 } ftable[] = {
550 { V_MODECOLOR, V_INFO_COLOR, 0 },
551 { V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
552 { V_MODELFB, V_INFO_LINEAR, 0 },
553 };
554 int flags;
555 int i;
556
557 for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
558 flags |= (vflags & ftable[i].mask) ?
559 ftable[i].set : ftable[i].reset;
560 }
561 return flags;
562}
563
564static int
565vesa_translate_mmodel(u_int8_t vmodel)
566{
567 static struct {
568 u_int8_t vmodel;
569 int mmodel;
570 } mtable[] = {
571 { V_MMTEXT, V_INFO_MM_TEXT },
572 { V_MMCGA, V_INFO_MM_CGA },
573 { V_MMHGC, V_INFO_MM_HGC },
574 { V_MMEGA, V_INFO_MM_PLANAR },
575 { V_MMPACKED, V_INFO_MM_PACKED },
576 { V_MMDIRCOLOR, V_INFO_MM_DIRECT },
577 };
578 int i;
579
580 for (i = 0; mtable[i].mmodel >= 0; ++i) {
581 if (mtable[i].vmodel == vmodel)
582 return mtable[i].mmodel;
583 }
584 return V_INFO_MM_OTHER;
585}
586
587static void
588*vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off, u_char *buf)
589{
590 if (p == 0)
591 return NULL;
592 if (((p >> 16) == seg) && ((p & 0xffff) >= off))
593 return (void *)(buf + ((p & 0xffff) - off));
594 else {
595 p = BIOS_SADDRTOLADDR(p);
596 return (void *)BIOS_PADDRTOVADDR(p);
597 }
598}
599
600static int
601vesa_bios_init(void)
602{
603 static u_char buf[512];
604 struct vm86frame vmf;
605 struct vesa_mode vmode;
606 video_info_t *p;
607 u_char *vmbuf;
608 int modes;
609 int err;
610 int i;
611
612 if (vesa_init_done)
613 return 0;
614
615 has_vesa_bios = FALSE;
616 vesa_adp_info = NULL;
617 vesa_vmode_max = 0;
618 vesa_vmode[0].vi_mode = EOT;
619
620 vmbuf = (u_char *)vm86_addpage(&vesa_vmcontext, 1, 0);
621 bzero(&vmf, sizeof(vmf)); /* paranoia */
622 bcopy("VBE2", vmbuf, 4); /* try for VBE2 data */
623 vmf.vmf_eax = 0x4f00;
624 vm86_getptr(&vesa_vmcontext, (vm_offset_t)vmbuf, &vmf.vmf_es, &vmf.vmf_di);
625
626 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
627 if ((err != 0) || (vmf.vmf_ax != 0x4f) || bcmp("VESA", vmbuf, 4))
628 return 1;
629 bcopy(vmbuf, buf, sizeof(buf));
630 vesa_adp_info = (struct vesa_info *)buf;
631 if (bootverbose) {
632 printf("VESA: information block\n");
633 dump_buffer(buf, 64);
634 }
635 if (vesa_adp_info->v_flags & V_NONVGA)
636 return 1;
637 if (vesa_adp_info->v_version < 0x0102) {
638 printf("VESA: VBE version %d.%d is not supported; "
639 "version 1.2 or later is required.\n",
640 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
641 + ((vesa_adp_info->v_version & 0x0f00) >> 8),
642 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
643 + (vesa_adp_info->v_version & 0x000f));
644 return 1;
645 }
646
647 /* fix string ptrs */
648 vesa_oemstr = (char *)vesa_fix_ptr(vesa_adp_info->v_oemstr,
649 vmf.vmf_es, vmf.vmf_di, buf);
650 if (vesa_adp_info->v_version >= 0x0200) {
651 vesa_venderstr =
652 (char *)vesa_fix_ptr(vesa_adp_info->v_venderstr,
653 vmf.vmf_es, vmf.vmf_di, buf);
654 vesa_prodstr =
655 (char *)vesa_fix_ptr(vesa_adp_info->v_prodstr,
656 vmf.vmf_es, vmf.vmf_di, buf);
657 vesa_revstr =
658 (char *)vesa_fix_ptr(vesa_adp_info->v_revstr,
659 vmf.vmf_es, vmf.vmf_di, buf);
660 }
661
662 /* obtain video mode information */
663 vesa_vmodetab = (u_int16_t *)vesa_fix_ptr(vesa_adp_info->v_modetable,
664 vmf.vmf_es, vmf.vmf_di, buf);
665 if (vesa_vmodetab == NULL)
666 return 1;
667 for (i = 0, modes = 0;
668 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
669 && (vesa_vmodetab[i] != 0xffff); ++i) {
670 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
671 continue;
672
673 /* reject unsupported modes */
674#if 0
675 if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
676 | V_MODENONVGA))
677 != (V_MODESUPP | V_MODEOPTINFO))
678 continue;
679#else
680 if ((vmode.v_modeattr & (V_MODEOPTINFO | V_MODENONVGA))
681 != (V_MODEOPTINFO))
682 continue;
683#endif
684
685 /* expand the array if necessary */
686 if (modes >= vesa_vmode_max) {
687 vesa_vmode_max += MODE_TABLE_DELTA;
688 p = malloc(sizeof(*vesa_vmode)*(vesa_vmode_max + 1),
689 M_DEVBUF, M_WAITOK);
690#if VESA_DEBUG > 1
691 printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
692 modes, vesa_vmode_max);
693#endif
694 if (modes > 0) {
695 bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
696 free(vesa_vmode, M_DEVBUF);
697 }
698 vesa_vmode = p;
699 }
700
701 /* copy some fields */
702 bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
703 vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
704 vesa_vmode[modes].vi_width = vmode.v_width;
705 vesa_vmode[modes].vi_height = vmode.v_height;
706 vesa_vmode[modes].vi_depth = vmode.v_bpp;
707 vesa_vmode[modes].vi_planes = vmode.v_planes;
708 vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
709 vesa_vmode[modes].vi_cheight = vmode.v_cheight;
710 vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
711 /* XXX window B */
712 vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024;
713 vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024;
714 if (vmode.v_modeattr & V_MODELFB)
715 vesa_vmode[modes].vi_buffer = vmode.v_lfb;
716 else
717 vesa_vmode[modes].vi_buffer = 0;
718 /* XXX */
719 vesa_vmode[modes].vi_buffer_size
720 = vesa_adp_info->v_memsize*64*1024;
721#if 0
722 if (vmode.v_offscreen > vmode.v_lfb)
723 vesa_vmode[modes].vi_buffer_size
724 = vmode.v_offscreen + vmode.v_offscreensize*1024
725 - vmode.v_lfb;
726 else
727 vesa_vmode[modes].vi_buffer_size
728 = vmode.v_offscreen + vmode.v_offscreensize*1024
729#endif
730 vesa_vmode[modes].vi_mem_model
731 = vesa_translate_mmodel(vmode.v_memmodel);
732 vesa_vmode[modes].vi_pixel_fields[0] = 0;
733 vesa_vmode[modes].vi_pixel_fields[1] = 0;
734 vesa_vmode[modes].vi_pixel_fields[2] = 0;
735 vesa_vmode[modes].vi_pixel_fields[3] = 0;
736 vesa_vmode[modes].vi_pixel_fsizes[0] = 0;
737 vesa_vmode[modes].vi_pixel_fsizes[1] = 0;
738 vesa_vmode[modes].vi_pixel_fsizes[2] = 0;
739 vesa_vmode[modes].vi_pixel_fsizes[3] = 0;
740 if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_PACKED) {
741 vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
742 } else if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_DIRECT) {
743 vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
744 vesa_vmode[modes].vi_pixel_fields[0]
745 = vmode.v_redfieldpos;
746 vesa_vmode[modes].vi_pixel_fields[1]
747 = vmode.v_greenfieldpos;
748 vesa_vmode[modes].vi_pixel_fields[2]
749 = vmode.v_bluefieldpos;
750 vesa_vmode[modes].vi_pixel_fields[3]
751 = vmode.v_resfieldpos;
752 vesa_vmode[modes].vi_pixel_fsizes[0]
753 = vmode.v_redmasksize;
754 vesa_vmode[modes].vi_pixel_fsizes[1]
755 = vmode.v_greenmasksize;
756 vesa_vmode[modes].vi_pixel_fsizes[2]
757 = vmode.v_bluemasksize;
758 vesa_vmode[modes].vi_pixel_fsizes[3]
759 = vmode.v_resmasksize;
760 } else {
761 vesa_vmode[modes].vi_pixel_size = 0;
762 }
763
764 vesa_vmode[modes].vi_flags
765 = vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
766 ++modes;
767 }
768 vesa_vmode[modes].vi_mode = EOT;
769 if (bootverbose)
770 printf("VESA: %d mode(s) found\n", modes);
771
772 has_vesa_bios = (modes > 0);
773 return (has_vesa_bios ? 0 : 1);
774}
775
776static void
777vesa_clear_modes(video_info_t *info, int color)
778{
779 while (info->vi_mode != EOT) {
780 if ((info->vi_flags & V_INFO_COLOR) != color)
781 info->vi_mode = NA;
782 ++info;
783 }
784}
785
786static vm_offset_t
787vesa_map_buffer(u_int paddr, size_t size)
788{
789 vm_offset_t vaddr;
790 u_int off;
791
792 off = paddr - trunc_page(paddr);
793 vaddr = (vm_offset_t)pmap_mapdev(paddr - off, size + off);
794#if VESA_DEBUG > 1
795 printf("vesa_map_buffer: paddr:%x vaddr:%x size:%x off:%x\n",
796 paddr, vaddr, size, off);
797#endif
798 return (vaddr + off);
799}
800
801static void
802vesa_unmap_buffer(vm_offset_t vaddr, size_t size)
803{
804#if VESA_DEBUG > 1
805 printf("vesa_unmap_buffer: vaddr:%x size:%x\n", vaddr, size);
806#endif
807 kmem_free(kernel_map, vaddr, size);
808}
809
810/* entry points */
811
812static int
813vesa_configure(int flags)
814{
815 video_adapter_t *adp;
816 int adapters;
817 int error;
818 int i;
819
820 if (vesa_init_done)
821 return 0;
822 if (flags & VIO_PROBE_ONLY)
823 return 0; /* XXX */
824
825 /*
826 * If the VESA module has already been loaded, abort loading
827 * the module this time.
828 */
829 for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
830 if (adp->va_flags & V_ADP_VESA)
831 return ENXIO;
832 if (adp->va_type == KD_VGA)
833 break;
834 }
835 /*
836 * The VGA adapter is not found. This is because either
837 * 1) the VGA driver has not been initialized, or 2) the VGA card
838 * is not present. If 1) is the case, we shall defer
839 * initialization for now and try again later.
840 */
841 if (adp == NULL) {
842 vga_sub_configure = vesa_configure;
843 return ENODEV;
844 }
845
846 /* count number of registered adapters */
847 for (++i; vid_get_adapter(i) != NULL; ++i)
848 ;
849 adapters = i;
850
851 /* call VESA BIOS */
852 vesa_adp = adp;
853 if (vesa_bios_init()) {
854 vesa_adp = NULL;
855 return ENXIO;
856 }
857 vesa_adp->va_flags |= V_ADP_VESA;
858
859 /* remove conflicting modes if we have more than one adapter */
860 if (adapters > 1) {
861 vesa_clear_modes(vesa_vmode,
862 (vesa_adp->va_flags & V_ADP_COLOR) ?
863 V_INFO_COLOR : 0);
864 }
865
866 if ((error = vesa_load_ioctl()) == 0) {
867 prevvidsw = vidsw[vesa_adp->va_index];
868 vidsw[vesa_adp->va_index] = &vesavidsw;
869 vesa_init_done = TRUE;
870 } else {
871 vesa_adp = NULL;
872 return error;
873 }
874
875 return 0;
876}
877
878#if 0
879static int
880vesa_nop(void)
881{
882 return 0;
883}
884#endif
885
886static int
887vesa_error(void)
888{
889 return 1;
890}
891
892static int
893vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
894{
895 return (*prevvidsw->probe)(unit, adpp, arg, flags);
896}
897
898static int
899vesa_init(int unit, video_adapter_t *adp, int flags)
900{
901 return (*prevvidsw->init)(unit, adp, flags);
902}
903
904static int
905vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
906{
907 int i;
908
909 if ((*prevvidsw->get_info)(adp, mode, info) == 0)
910 return 0;
911
912 if (adp != vesa_adp)
913 return 1;
914
915 mode = vesa_map_gen_mode_num(vesa_adp->va_type,
916 vesa_adp->va_flags & V_ADP_COLOR, mode);
917 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
918 if (vesa_vmode[i].vi_mode == NA)
919 continue;
920 if (vesa_vmode[i].vi_mode == mode) {
921 *info = vesa_vmode[i];
922 return 0;
923 }
924 }
925 return 1;
926}
927
928static int
929vesa_query_mode(video_adapter_t *adp, video_info_t *info)
930{
931 int i;
932
933 if ((*prevvidsw->query_mode)(adp, info) == 0)
934 return 0;
935 if (adp != vesa_adp)
936 return ENODEV;
937
938 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
939 if ((info->vi_width != 0)
940 && (info->vi_width != vesa_vmode[i].vi_width))
941 continue;
942 if ((info->vi_height != 0)
943 && (info->vi_height != vesa_vmode[i].vi_height))
944 continue;
945 if ((info->vi_cwidth != 0)
946 && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
947 continue;
948 if ((info->vi_cheight != 0)
949 && (info->vi_cheight != vesa_vmode[i].vi_cheight))
950 continue;
951 if ((info->vi_depth != 0)
952 && (info->vi_depth != vesa_vmode[i].vi_depth))
953 continue;
954 if ((info->vi_planes != 0)
955 && (info->vi_planes != vesa_vmode[i].vi_planes))
956 continue;
957 /* pixel format, memory model */
958 if ((info->vi_flags != 0)
959 && (info->vi_flags != vesa_vmode[i].vi_flags))
960 continue;
961 *info = vesa_vmode[i];
962 return 0;
963 }
964 return ENODEV;
965}
966
967static int
968vesa_set_mode(video_adapter_t *adp, int mode)
969{
970 video_info_t info;
971 int len;
972
973 if (adp != vesa_adp)
974 return (*prevvidsw->set_mode)(adp, mode);
975
976 mode = vesa_map_gen_mode_num(adp->va_type,
977 adp->va_flags & V_ADP_COLOR, mode);
978#if VESA_DEBUG > 0
979 printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
980 adp->va_mode, adp->va_mode, mode, mode);
981#endif
982 /*
983 * If the current mode is a VESA mode and the new mode is not,
984 * restore the state of the adapter first by setting one of the
985 * standard VGA mode, so that non-standard, extended SVGA registers
986 * are set to the state compatible with the standard VGA modes.
987 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
988 * the new mode correctly.
989 */
990 if (VESA_MODE(adp->va_mode)) {
991 if ((*prevvidsw->get_info)(adp, mode, &info) == 0) {
992 int10_set_mode(adp->va_initial_bios_mode);
993 if (adp->va_info.vi_flags & V_INFO_LINEAR)
994 vesa_unmap_buffer(adp->va_buffer,
995 vesa_adp_info->v_memsize*64*1024);
996 /*
997 * Once (*prevvidsw->get_info)() succeeded,
998 * (*prevvidsw->set_mode)() below won't fail...
999 */
1000 }
1001 }
1002
1003 /* we may not need to handle this mode after all... */
1004 if ((*prevvidsw->set_mode)(adp, mode) == 0)
1005 return 0;
1006
1007 /* is the new mode supported? */
1008 if (vesa_get_info(adp, mode, &info))
1009 return 1;
1010 /* assert(VESA_MODE(mode)); */
1011
1012#if VESA_DEBUG > 0
1013 printf("VESA: about to set a VESA mode...\n");
1014#endif
1015 /* don't use the linear frame buffer for text modes. XXX */
1016 if (!(info.vi_flags & V_INFO_GRAPHICS))
1017 info.vi_flags &= ~V_INFO_LINEAR;
1018
1019 if (vesa_bios_set_mode(mode | ((info.vi_flags & V_INFO_LINEAR) ? 0x4000 : 0)))
1020 return 1;
1021
1022 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1023 vesa_unmap_buffer(adp->va_buffer,
1024 vesa_adp_info->v_memsize*64*1024);
1025
1026#if VESA_DEBUG > 0
1027 printf("VESA: mode set!\n");
1028#endif
1029 vesa_adp->va_mode = mode;
1030 vesa_adp->va_flags &= ~V_ADP_COLOR;
1031 vesa_adp->va_flags |=
1032 (info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1033 vesa_adp->va_crtc_addr =
1034 (vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1035 if (info.vi_flags & V_INFO_LINEAR) {
1036#if VESA_DEBUG > 1
1037 printf("VESA: setting up LFB\n");
1038#endif
1039 vesa_adp->va_buffer =
1040 vesa_map_buffer(info.vi_buffer,
1041 vesa_adp_info->v_memsize*64*1024);
1042 vesa_adp->va_buffer_size = info.vi_buffer_size;
1043 vesa_adp->va_window = vesa_adp->va_buffer;
1044 vesa_adp->va_window_size = info.vi_buffer_size/info.vi_planes;
1045 vesa_adp->va_window_gran = info.vi_buffer_size/info.vi_planes;
1046 } else {
1047 vesa_adp->va_buffer = 0;
1048 vesa_adp->va_buffer_size = info.vi_buffer_size;
1049 vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
1050 vesa_adp->va_window_size = info.vi_window_size;
1051 vesa_adp->va_window_gran = info.vi_window_gran;
1052 }
1053 vesa_adp->va_window_orig = 0;
1054 len = vesa_bios_get_line_length();
1055 if (len > 0) {
1056 vesa_adp->va_line_width = len;
1057 } else if (info.vi_flags & V_INFO_GRAPHICS) {
1058 switch (info.vi_depth/info.vi_planes) {
1059 case 1:
1060 vesa_adp->va_line_width = info.vi_width/8;
1061 break;
1062 case 2:
1063 vesa_adp->va_line_width = info.vi_width/4;
1064 break;
1065 case 4:
1066 vesa_adp->va_line_width = info.vi_width/2;
1067 break;
1068 case 8:
1069 default: /* shouldn't happen */
1070 vesa_adp->va_line_width = info.vi_width;
1071 break;
1072 }
1073 } else {
1074 vesa_adp->va_line_width = info.vi_width;
1075 }
1076 vesa_adp->va_disp_start.x = 0;
1077 vesa_adp->va_disp_start.y = 0;
1078#if VESA_DEBUG > 0
1079 printf("vesa_set_mode(): vi_width:%d, len:%d, line_width:%d\n",
1080 info.vi_width, len, vesa_adp->va_line_width);
1081#endif
1082 bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1083
1084 /* move hardware cursor out of the way */
1085 (*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1086
1087 return 0;
1088}
1089
1090static int
1091vesa_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1092 int ch, int count)
1093{
1094 return (*prevvidsw->save_font)(adp, page, fontsize, data, ch, count);
1095}
1096
1097static int
1098vesa_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1099 int ch, int count)
1100{
1101 return (*prevvidsw->load_font)(adp, page, fontsize, data, ch, count);
1102}
1103
1104static int
1105vesa_show_font(video_adapter_t *adp, int page)
1106{
1107 return (*prevvidsw->show_font)(adp, page);
1108}
1109
1110static int
1111vesa_save_palette(video_adapter_t *adp, u_char *palette)
1112{
1113 int bits;
1114 int error;
1115
1116 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1117 && VESA_MODE(adp->va_mode)) {
1118 bits = vesa_bios_get_dac();
1119 error = vesa_bios_save_palette(0, 256, palette, bits);
1120 if (error == 0)
1121 return 0;
1122 if (bits != 6)
1123 return error;
1124 }
1125
1126 return (*prevvidsw->save_palette)(adp, palette);
1127}
1128
1129static int
1130vesa_load_palette(video_adapter_t *adp, u_char *palette)
1131{
1132#if notyet
1133 int bits;
1134 int error;
1135
1136 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1137 && VESA_MODE(adp->va_mode) && ((bits = vesa_bios_set_dac(8)) > 6)) {
1138 error = vesa_bios_load_palette(0, 256, palette, bits);
1139 if (error == 0)
1140 return 0;
1141 if (vesa_bios_set_dac(6) != 6)
1142 return 1;
1143 }
1144#endif /* notyet */
1145
1146 return (*prevvidsw->load_palette)(adp, palette);
1147}
1148
1149static int
1150vesa_set_border(video_adapter_t *adp, int color)
1151{
1152 return (*prevvidsw->set_border)(adp, color);
1153}
1154
1155static int
1156vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1157{
1158 if (adp != vesa_adp)
1159 return (*prevvidsw->save_state)(adp, p, size);
1160
1161 if (vesa_state_buf_size == 0)
1162 vesa_state_buf_size = vesa_bios_state_buf_size();
1163 if (size == 0)
1164 return (sizeof(int) + vesa_state_buf_size);
1165 else if (size < (sizeof(int) + vesa_state_buf_size))
1166 return 1;
1167
1168 ((adp_state_t *)p)->sig = V_STATE_SIG;
1169 bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1170 return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
1171 vesa_state_buf_size);
1172}
1173
1174static int
1175vesa_load_state(video_adapter_t *adp, void *p)
1176{
1177 if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1178 return (*prevvidsw->load_state)(adp, p);
1179
1180 return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
1181 vesa_state_buf_size);
1182}
1183
1184#if 0
1185static int
1186vesa_get_origin(video_adapter_t *adp, off_t *offset)
1187{
1188 struct vm86frame vmf;
1189 int err;
1190
1191 bzero(&vmf, sizeof(vmf));
1192 vmf.vmf_eax = 0x4f05;
1193 vmf.vmf_ebx = 0x10; /* WINDOW_A, XXX */
1194 err = vm86_intcall(0x10, &vmf);
1195 if ((err != 0) || (vmf.vmf_ax != 0x4f))
1196 return 1;
1197 *offset = vmf.vmf_dx*adp->va_window_gran;
1198 return 0;
1199}
1200#endif
1201
1202static int
1203vesa_set_origin(video_adapter_t *adp, off_t offset)
1204{
1205 struct vm86frame vmf;
1206 int err;
1207
1208 /*
1209 * This function should return as quickly as possible to
1210 * maintain good performance of the system. For this reason,
1211 * error checking is kept minimal and let the VESA BIOS to
1212 * detect error.
1213 */
1214 if (adp != vesa_adp)
1215 return (*prevvidsw->set_win_org)(adp, offset);
1216
1217 /* if this is a linear frame buffer, do nothing */
1218 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1219 return 0;
1220 /* XXX */
1221 if (adp->va_window_gran == 0)
1222 return 1;
1223
1224 bzero(&vmf, sizeof(vmf));
1225 vmf.vmf_eax = 0x4f05;
1226 vmf.vmf_ebx = 0; /* WINDOW_A, XXX */
1227 vmf.vmf_edx = offset/adp->va_window_gran;
1228 err = vm86_intcall(0x10, &vmf);
1229 if ((err != 0) || (vmf.vmf_ax != 0x4f))
1230 return 1;
1231 bzero(&vmf, sizeof(vmf));
1232 vmf.vmf_eax = 0x4f05;
1233 vmf.vmf_ebx = 1; /* WINDOW_B, XXX */
1234 vmf.vmf_edx = offset/adp->va_window_gran;
1235 err = vm86_intcall(0x10, &vmf);
1236 adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran;
1237 return 0; /* XXX */
1238}
1239
1240static int
1241vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1242{
1243 return (*prevvidsw->read_hw_cursor)(adp, col, row);
1244}
1245
1246static int
1247vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1248{
1249 return (*prevvidsw->set_hw_cursor)(adp, col, row);
1250}
1251
1252static int
1253vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1254 int celsize, int blink)
1255{
1256 return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1257 blink);
1258}
1259
1260static int
1261vesa_blank_display(video_adapter_t *adp, int mode)
1262{
1263 /* XXX: use VESA DPMS */
1264 return (*prevvidsw->blank_display)(adp, mode);
1265}
1266
1267static int
1268vesa_mmap(video_adapter_t *adp, vm_offset_t offset, int prot)
1269{
1270#if VESA_DEBUG > 0
1271 printf("vesa_mmap(): window:0x%x, buffer:0x%x, offset:0x%x\n",
1272 adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1273#endif
1274
1275 if ((adp == vesa_adp) && (adp->va_info.vi_flags & V_INFO_LINEAR)) {
1276 /* va_window_size == va_buffer_size/vi_planes */
1277 /* XXX: is this correct? */
1278 if (offset > adp->va_window_size - PAGE_SIZE)
1279 return -1;
1280#ifdef __i386__
1281 return i386_btop(adp->va_info.vi_buffer + offset);
1282#endif
1283#ifdef __alpha__ /* XXX */
1284 return alpha_btop(adp->va_info.vi_buffer + offset);
1285#endif
1286 } else {
1287 return (*prevvidsw->mmap)(adp, offset, prot);
1288 }
1289}
1290
1291static int
1292vesa_clear(video_adapter_t *adp)
1293{
1294 return (*prevvidsw->clear)(adp);
1295}
1296
1297static int
1298vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1299{
1300 return (*prevvidsw->fill_rect)(adp, val, x, y, cx, cy);
1301}
1302
1303static int
1304vesa_bitblt(video_adapter_t *adp,...)
1305{
1306 /* FIXME */
1307 return 1;
1308}
1309
1310static int
1311get_palette(video_adapter_t *adp, int base, int count,
1312 u_char *red, u_char *green, u_char *blue, u_char *trans)
1313{
1314 u_char *r;
1315 u_char *g;
1316 u_char *b;
1317 int bits;
1318 int error;
1319
1320 if ((base < 0) || (base >= 256) || (count < 0) || (count > 256))
1321 return 1;
1322 if (base + count > 256)
1323 return 1;
1324 if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode))
1325 return 1;
1326
1327 bits = vesa_bios_get_dac();
1328 if (bits <= 6)
1329 return 1;
1330
1331 r = malloc(count*3, M_DEVBUF, M_WAITOK);
1332 g = r + count;
1333 b = g + count;
1334 error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1335 if (error == 0) {
1336 copyout(r, red, count);
1337 copyout(g, green, count);
1338 copyout(b, blue, count);
1339 if (trans != NULL) {
1340 bzero(r, count);
1341 copyout(r, trans, count);
1342 }
1343 }
1344 free(r, M_DEVBUF);
1345
1346 /* if error && bits != 6 at this point, we are in in trouble... XXX */
1347 return error;
1348}
1349
1350static int
1351set_palette(video_adapter_t *adp, int base, int count,
1352 u_char *red, u_char *green, u_char *blue, u_char *trans)
1353{
1354 return 1;
1355#if notyet
1356 u_char *r;
1357 u_char *g;
1358 u_char *b;
1359 int bits;
1360 int error;
1361
1362 if ((base < 0) || (base >= 256) || (base + count > 256))
1363 return 1;
1364 if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode)
1365 || ((bits = vesa_bios_set_dac(8)) <= 6))
1366 return 1;
1367
1368 r = malloc(count*3, M_DEVBUF, M_WAITOK);
1369 g = r + count;
1370 b = g + count;
1371 copyin(red, r, count);
1372 copyin(green, g, count);
1373 copyin(blue, b, count);
1374
1375 error = vesa_bios_load_palette2(base, count, r, g, b, bits);
1376 free(r, M_DEVBUF);
1377 if (error == 0)
1378 return 0;
1379
1380 /* if the following call fails, we are in trouble... XXX */
1381 vesa_bios_set_dac(6);
1382 return 1;
1383#endif /* notyet */
1384}
1385
1386static int
1387vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1388{
1389 int bytes;
1390
1391 if (adp != vesa_adp)
1392 return (*prevvidsw->ioctl)(adp, cmd, arg);
1393
1394 switch (cmd) {
1395 case FBIO_SETWINORG: /* set frame buffer window origin */
1396 if (!VESA_MODE(adp->va_mode))
1397 return (*prevvidsw->ioctl)(adp, cmd, arg);
1398 return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1399
1400 case FBIO_SETDISPSTART: /* set display start address */
1401 if (!VESA_MODE(adp->va_mode))
1402 return (*prevvidsw->ioctl)(adp, cmd, arg);
1403 if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1404 ((video_display_start_t *)arg)->y))
1405 return ENODEV;
1406 adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1407 adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1408 return 0;
1409
1410 case FBIO_SETLINEWIDTH: /* set line length in pixel */
1411 if (!VESA_MODE(adp->va_mode))
1412 return (*prevvidsw->ioctl)(adp, cmd, arg);
1413 if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1414 return ENODEV;
1415 adp->va_line_width = bytes;
1416#if VESA_DEBUG > 1
1417 printf("new line width:%d\n", adp->va_line_width);
1418#endif
1419 return 0;
1420
1421 case FBIO_GETPALETTE: /* get color palette */
1422 if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1423 ((video_color_palette_t *)arg)->count,
1424 ((video_color_palette_t *)arg)->red,
1425 ((video_color_palette_t *)arg)->green,
1426 ((video_color_palette_t *)arg)->blue,
1427 ((video_color_palette_t *)arg)->transparent))
1428 return (*prevvidsw->ioctl)(adp, cmd, arg);
1429 return 0;
1430
1431
1432 case FBIO_SETPALETTE: /* set color palette */
1433 if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1434 ((video_color_palette_t *)arg)->count,
1435 ((video_color_palette_t *)arg)->red,
1436 ((video_color_palette_t *)arg)->green,
1437 ((video_color_palette_t *)arg)->blue,
1438 ((video_color_palette_t *)arg)->transparent))
1439 return (*prevvidsw->ioctl)(adp, cmd, arg);
1440 return 0;
1441
1442 case FBIOGETCMAP: /* get color palette */
1443 if (get_palette(adp, ((struct fbcmap *)arg)->index,
1444 ((struct fbcmap *)arg)->count,
1445 ((struct fbcmap *)arg)->red,
1446 ((struct fbcmap *)arg)->green,
1447 ((struct fbcmap *)arg)->blue, NULL))
1448 return (*prevvidsw->ioctl)(adp, cmd, arg);
1449 return 0;
1450
1451 case FBIOPUTCMAP: /* set color palette */
1452 if (set_palette(adp, ((struct fbcmap *)arg)->index,
1453 ((struct fbcmap *)arg)->count,
1454 ((struct fbcmap *)arg)->red,
1455 ((struct fbcmap *)arg)->green,
1456 ((struct fbcmap *)arg)->blue, NULL))
1457 return (*prevvidsw->ioctl)(adp, cmd, arg);
1458 return 0;
1459
1460 default:
1461 return (*prevvidsw->ioctl)(adp, cmd, arg);
1462 }
1463}
1464
1465static int
1466vesa_diag(video_adapter_t *adp, int level)
1467{
1468 int error;
1469
1470 /* call the previous handler first */
1471 error = (*prevvidsw->diag)(adp, level);
1472 if (error)
1473 return error;
1474
1475 if (adp != vesa_adp)
1476 return 1;
1477
1478 if (level <= 0)
1479 return 0;
1480
1481 return 0;
1482}
1483
1484static int
1485vesa_bios_info(int level)
1486{
1487#if VESA_DEBUG > 1
1488 struct vesa_mode vmode;
1489 int i;
1490#endif
1491
1492 /* general adapter information */
1493 printf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
1494 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
1495 + ((vesa_adp_info->v_version & 0x0f00) >> 8),
1496 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
1497 + (vesa_adp_info->v_version & 0x000f),
1498 vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1499 vesa_vmodetab, vesa_adp_info->v_modetable);
1500 /* OEM string */
1501 if (vesa_oemstr != NULL)
1502 printf("VESA: %s\n", vesa_oemstr);
1503
1504 if (level <= 0)
1505 return 0;
1506
1507 if (vesa_adp_info->v_version >= 0x0200) {
1508 /* vender name, product name, product revision */
1509 printf("VESA: %s %s %s\n",
1510 (vesa_venderstr != NULL) ? vesa_venderstr : "unknown",
1511 (vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1512 (vesa_revstr != NULL) ? vesa_revstr : "?");
1513 }
1514
1515#if VESA_DEBUG > 1
1516 /* mode information */
1517 for (i = 0;
1518 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1519 && (vesa_vmodetab[i] != 0xffff); ++i) {
1520 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
1521 continue;
1522
1523 /* print something for diagnostic purpose */
1524 printf("VESA: mode:0x%03x, flags:0x%04x",
1525 vesa_vmodetab[i], vmode.v_modeattr);
1526 if (vmode.v_modeattr & V_MODEOPTINFO) {
1527 if (vmode.v_modeattr & V_MODEGRAPHICS) {
1528 printf(", G %dx%dx%d %d, ",
1529 vmode.v_width, vmode.v_height,
1530 vmode.v_bpp, vmode.v_planes);
1531 } else {
1532 printf(", T %dx%d, ",
1533 vmode.v_width, vmode.v_height);
1534 }
1535 printf("font:%dx%d, ",
1536 vmode.v_cwidth, vmode.v_cheight);
1537 printf("pages:%d, mem:%d",
1538 vmode.v_ipages + 1, vmode.v_memmodel);
1539 }
1540 if (vmode.v_modeattr & V_MODELFB) {
1541 printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
1542 vmode.v_lfb, vmode.v_offscreen,
1543 vmode.v_offscreensize*1024);
1544 }
1545 printf("\n");
1546 printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
1547 vmode.v_waseg, vmode.v_waattr,
1548 vmode.v_wbseg, vmode.v_wbattr);
1549 printf("size:%dk, gran:%dk\n",
1550 vmode.v_wsize, vmode.v_wgran);
1551 }
1552#endif /* VESA_DEBUG > 1 */
1553
1554 return 0;
1555}
1556
1557/* module loading */
1558
1559static int
1560vesa_load(void)
1561{
1562 int error;
1563 int s;
1564
1565 if (vesa_init_done)
1566 return 0;
1567
1568 /* locate a VGA adapter */
1569 s = spltty();
1570 vesa_adp = NULL;
1571 error = vesa_configure(0);
1572 splx(s);
1573
1574 if (error == 0)
1575 vesa_bios_info(bootverbose);
1576
1577 return error;
1578}
1579
1580static int
1581vesa_unload(void)
1582{
1583 u_char palette[256*3];
1584 int error;
1585 int bits;
1586 int s;
1587
1588 /* if the adapter is currently in a VESA mode, don't unload */
1589 if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1590 return EBUSY;
1591 /*
1592 * FIXME: if there is at least one vty which is in a VESA mode,
1593 * we shouldn't be unloading! XXX
1594 */
1595
1596 s = spltty();
1597 if ((error = vesa_unload_ioctl()) == 0) {
1598 if (vesa_adp != NULL) {
1599 if (vesa_adp_info->v_flags & V_DAC8) {
1600 bits = vesa_bios_get_dac();
1601 if (bits > 6) {
1602 vesa_bios_save_palette(0, 256,
1603 palette, bits);
1604 vesa_bios_set_dac(6);
1605 vesa_bios_load_palette(0, 256,
1606 palette, 6);
1607 }
1608 }
1609 vesa_adp->va_flags &= ~V_ADP_VESA;
1610 vidsw[vesa_adp->va_index] = prevvidsw;
1611 }
1612 }
1613 splx(s);
1614
1615 return error;
1616}
1617
1618static int
1619vesa_mod_event(module_t mod, int type, void *data)
1620{
1621 switch (type) {
1622 case MOD_LOAD:
1623 return vesa_load();
1624 case MOD_UNLOAD:
1625 return vesa_unload();
1626 default:
1627 break;
1628 }
1629 return 0;
1630}
1631
1632static moduledata_t vesa_mod = {
1633 "vesa",
1634 vesa_mod_event,
1635 NULL,
1636};
1637
1638DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1639
1640#endif /* VGA_NO_MODE_CHANGE */