First import
[xorg_rtime.git] / xorg-server-1.4 / hw / kdrive / vesa / vga.c
blob0367a5f067a2534f987f4b49fe026c9998487ea9
1 /*
2 * Copyright © 2000 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Keith Packard makes no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
23 #ifdef HAVE_CONFIG_H
24 #include <kdrive-config.h>
25 #endif
26 #include "vesa.h"
28 static const VesaModeRec vgaModes[] = {
30 6, 0,
31 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_LINEAR,
32 1, 1, MEMORY_PLANAR,
33 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 640, 200, 80,
37 0xd, 0,
38 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_COLOUR,
39 4, 4, MEMORY_PLANAR,
40 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 320, 200, 40,
44 0xe, 0,
45 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_COLOUR,
46 4, 4, MEMORY_PLANAR,
47 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 640, 200, 80,
51 0x10, 0,
52 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_COLOUR,
53 4, 4, MEMORY_PLANAR,
54 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 640, 350, 80,
58 0x11, 0,
59 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_LINEAR,
60 1, 1, MEMORY_PLANAR,
61 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 640, 480, 80,
65 0x12, 0,
66 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_COLOUR,
67 4, 4, MEMORY_PLANAR,
68 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 640, 480, 80,
72 0x13, 0,
73 MODE_SUPPORTED | MODE_GRAPHICS | MODE_VGA | MODE_COLOUR | MODE_LINEAR,
74 8, 8, MEMORY_PSEUDO,
75 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 320, 200, 320,
80 #define NUM_VGA_MODE (sizeof vgaModes / sizeof vgaModes[0])
82 int
83 VgaGetNmode (Vm86InfoPtr vi)
85 return NUM_VGA_MODE;
88 int
89 VgaGetModes (Vm86InfoPtr vi, VesaModePtr mode, int nmode)
91 if (nmode > NUM_VGA_MODE)
92 nmode = NUM_VGA_MODE;
93 memcpy (mode, vgaModes, nmode * sizeof (VesaModeRec));
94 return nmode;
97 int
98 VgaSetMode(Vm86InfoPtr vi, int mode)
100 int code;
102 vi->vms.regs.eax = mode & 0x7f;
103 code = Vm86DoInterrupt (vi, 0x10);
104 if(code < 0)
105 return -1;
106 return 0;
110 VgaGetMode (Vm86InfoPtr vi, int *mode)
112 *mode = Vm86Memory (vi, 0x449);
113 return 0;
116 void
117 VgaSetWritePlaneMask(Vm86InfoPtr vi, int mask)
119 asm volatile ("outb %b0,%w1" : : "a" (2), "d" (0x3c4));
120 asm volatile ("outb %b0,%w1" : : "a" (mask), "d" (0x3c5));
123 void
124 VgaSetReadPlaneMap(Vm86InfoPtr vi, int map)
126 asm volatile ("outb %b0,%w1" : : "a" (4), "d" (0x3ce));
127 asm volatile ("outb %b0,%w1" : : "a" (map), "d" (0x3cf));
130 int
131 VgaSetPalette(Vm86InfoPtr vi, int first, int number, U8 *entries)
133 U8 *palette_scratch;
134 int mark;
135 int palette_base;
136 int i, j, code;
138 if(number == 0)
139 return 0;
141 if(first < 0 || number < 0 || first + number > 256) {
142 ErrorF("Cannot set %d, %d palette entries\n", first, number);
143 return -1;
146 mark = Vm86MarkMemory (vi);
147 palette_base = Vm86AllocateMemory (vi, 3 * 256);
149 palette_scratch = &LM(vi, palette_base);
151 vi->vms.regs.eax = 0x1012;
152 vi->vms.regs.ebx = first;
153 vi->vms.regs.ecx = number;
154 vi->vms.regs.es = POINTER_SEGMENT(palette_base);
155 vi->vms.regs.edx = POINTER_OFFSET(palette_base);
156 j = 0;
157 i = 0;
158 while (number--)
160 palette_scratch[j++] = entries[i++] >> 2;
161 palette_scratch[j++] = entries[i++] >> 2;
162 palette_scratch[j++] = entries[i++] >> 2;
163 i++;
165 code = Vm86DoInterrupt(vi, 0x10);
166 Vm86ReleaseMemory (vi, mark);
168 if(code < 0)
169 return -1;
170 return 0;
173 int
174 VgaGetPalette(Vm86InfoPtr vi, int first, int number, U8 *entries)
176 U8 *palette_scratch;
177 int mark;
178 int palette_base;
179 int i, j, code;
181 if(number == 0)
182 return 0;
184 if(first < 0 || number < 0 || first + number > 256) {
185 ErrorF("Cannot get %d, %d palette entries\n", first, number);
186 return -1;
189 mark = Vm86MarkMemory (vi);
190 palette_base = Vm86AllocateMemory (vi, 3 * 256);
192 palette_scratch = &LM(vi, palette_base);
194 vi->vms.regs.eax = 0x1017;
195 vi->vms.regs.ebx = first;
196 vi->vms.regs.ecx = number;
197 vi->vms.regs.es = POINTER_SEGMENT(palette_base);
198 vi->vms.regs.edx = POINTER_OFFSET(palette_base);
200 code = VbeDoInterrupt10(vi);
201 if(code < 0)
202 return -1;
204 j = 0;
205 i = 0;
206 while (number--)
208 entries[i++] = palette_scratch[j++] << 2;
209 entries[i++] = palette_scratch[j++] << 2;
210 entries[i++] = palette_scratch[j++] << 2;
211 entries[i++] = 0;
214 Vm86ReleaseMemory (vi, mark);
216 return 0;
219 #define VGA_FB(vm) ((vm) < 8 ? 0xb8000 : 0xa0000)
221 void *
222 VgaSetWindow (Vm86InfoPtr vi, int vmode, int bytes, int mode, int *size)
224 *size = 0x10000 - bytes;
225 return &LM(vi,VGA_FB(vmode) + bytes);
228 void *
229 VgaMapFramebuffer (Vm86InfoPtr vi, int vmode, int *size, CARD32 *ret_phys)
231 if (VGA_FB(vmode) == 0xa0000)
232 *size = 0x10000;
233 else
234 *size = 0x4000;
235 *ret_phys = VGA_FB(vmode);
236 return &LM(vi,VGA_FB(vmode));
239 void
240 VgaUnmapFramebuffer (Vm86InfoPtr vi)