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