1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* $OpenBSD: vesafb.c,v 1.1 2006/11/27 18:04:28 gwk Exp $ */
/*-
* Copyright (c) 2006 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2006 Matthieu Herrb
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/ic/mc6845reg.h>
#include <dev/ic/pcdisplayvar.h>
#include <dev/ic/vgareg.h>
#include <dev/ic/vgavar.h>
#include <dev/pci/vga_pcivar.h>
#include <dev/vesa/vesabiosreg.h>
#include <dev/vesa/vesabiosvar.h>
#include <machine/frame.h>
#include <machine/kvm86.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#include <uvm/uvm_extern.h>
void vesafb_set_mode(struct vga_pci_softc *, int);
int vesafb_get_mode(struct vga_pci_softc *);
void vesafb_get_mode_info(struct vga_pci_softc *, int, struct modeinfoblock *);
void vesafb_set_palette(struct vga_pci_softc *, int, struct paletteentry);
int vesafb_putcmap(struct vga_pci_softc *, struct wsdisplay_cmap *);
int vesafb_getcmap(struct vga_pci_softc *, struct wsdisplay_cmap *);
int
vesafb_get_mode(struct vga_pci_softc *sc)
{
struct trapframe tf;
int res;
bzero(&tf, sizeof(struct trapframe));
tf.tf_eax = 0x4f03;
res = kvm86_bioscall(0x10, &tf);
if (res || (tf.tf_eax & 0xff) != 0x4f) {
printf("%s: vbecall: res=%d, ax=%x\n",
sc->sc_dev.dv_xname, res, tf.tf_eax);
}
return tf.tf_ebx & 0xffff;
}
void
vesafb_get_mode_info(struct vga_pci_softc *sc, int mode,
struct modeinfoblock *mi)
{
struct trapframe tf;
unsigned char *buf;
int res;
buf = kvm86_bios_addpage(0x2000);
memset(&tf, 0, sizeof(struct trapframe));
tf.tf_eax = 0x4f01; /* function code */
tf.tf_ecx = mode;
tf.tf_vm86_es = 0;
tf.tf_edi = 0x2000; /* buf ptr */
res = kvm86_bioscall(0x10, &tf);
if (res || (tf.tf_eax & 0xff) != 0x4f) {
printf("%s: vbecall: res=%d, ax=%x\n",
sc->sc_dev.dv_xname, res, tf.tf_eax);
printf("%s: error getting info for mode %04x\n",
sc->sc_dev.dv_xname, mode);
return;
}
memcpy(mi, buf, sizeof(struct modeinfoblock));
kvm86_bios_delpage(0x2000, buf);
}
void
vesafb_set_palette(struct vga_pci_softc *sc, int reg, struct paletteentry pe)
{
struct trapframe tf;
int res;
char *buf;
buf = kvm86_bios_addpage(0x2000);
/*
* this function takes 8 bit per palette as input, but we're
* working in 6 bit mode here
*/
pe.Red >>= 2;
pe.Green >>= 2;
pe.Blue >>= 2;
memcpy(buf, &pe, sizeof(struct paletteentry));
/* set palette */
memset(&tf, 0, sizeof(struct trapframe));
tf.tf_eax = 0x4f09; /* function code */
tf.tf_ebx = 0x0600; /* 6 bit per primary, set format */
tf.tf_ecx = 1;
tf.tf_edx = reg;
tf.tf_vm86_es = 0;
tf.tf_edi = 0x2000;
res = kvm86_bioscall(0x10, &tf);
if (res || (tf.tf_eax & 0xff) != 0x4f)
printf("%s: vbecall: res=%d, ax=%x\n",
sc->sc_dev.dv_xname, res, tf.tf_eax);
kvm86_bios_delpage(0x2000, buf);
return;
}
void
vesafb_set_mode(struct vga_pci_softc *sc, int mode)
{
struct trapframe tf;
int res;
bzero(&tf, sizeof(struct trapframe));
tf.tf_eax = 0x4f02;
tf.tf_ebx = mode | 0x4000; /* flat */
res = kvm86_bioscall(0x10, &tf);
if (res || (tf.tf_eax & 0xff) != 0x4f) {
printf("%s: vbecall: res=%d, ax=%x\n",
sc->sc_dev.dv_xname, res, tf.tf_eax);
return;
}
}
int
vesafb_find_mode(struct vga_pci_softc *sc, int width, int height, int bpp)
{
struct modeinfoblock mi;
int i;
if (vesabios_softc == NULL || vesabios_softc->sc_nmodes == 0)
return -1;
printf("vesafb_find_mode %d %d %d\n", width, height, bpp);
/* Choose a graphics mode */
for (i = 0; i < vesabios_softc->sc_nmodes; i++) {
vesafb_get_mode_info(sc, vesabios_softc->sc_modes[i], &mi);
if (mi.XResolution == width &&
mi.YResolution == height &&
mi.BitsPerPixel == bpp) {
sc->sc_width = mi.XResolution;
sc->sc_height = mi.YResolution;
sc->sc_depth = mi.BitsPerPixel;
sc->sc_linebytes = mi.BytesPerScanLine;
sc->sc_base = mi.PhysBasePtr;
break;
}
}
if (i == vesabios_softc->sc_nmodes)
return -1;
else
return vesabios_softc->sc_modes[i];
}
int
vesafb_putcmap(struct vga_pci_softc *sc, struct wsdisplay_cmap *cm)
{
struct paletteentry pe;
u_int idx, cnt;
u_char r[256], g[256], b[256];
u_char *rp, *gp, *bp;
int rv, i;
idx = cm->index;
cnt = cm->count;
if (idx >= 255 || cnt > 256 || idx + cnt > 256)
return EINVAL;
rv = copyin(cm->red, &r[idx], cnt);
if (rv)
return rv;
rv = copyin(cm->green, &g[idx], cnt);
if (rv)
return rv;
rv = copyin(cm->blue, &b[idx], cnt);
if (rv)
return rv;
memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
rp = &sc->sc_cmap_red[idx];
gp = &sc->sc_cmap_green[idx];
bp = &sc->sc_cmap_blue[idx];
for (i = 0; i < cnt; i++) {
pe.Blue = *bp;
pe.Green = *gp;
pe.Red = *rp;
pe.Alignment = 0;
vesafb_set_palette(sc, idx, pe);
idx++;
rp++, gp++, bp++;
}
return 0;
}
int
vesafb_getcmap(struct vga_pci_softc *sc, struct wsdisplay_cmap *cm)
{
u_int idx, cnt;
int rv;
idx = cm->index;
cnt = cm->count;
if (idx >= 255 || cnt > 256 || idx + cnt > 256)
return EINVAL;
rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
if (rv)
return rv;
rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
if (rv)
return rv;
rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
if (rv)
return rv;
return 0;
}
static int
vesafb_getdepthflag(struct modeinfoblock *mi)
{
int bpp, depth;
depth = mi->RedMaskSize + mi->GreenMaskSize + mi->BlueMaskSize;
bpp = mi->BitsPerPixel;
switch (depth) {
case 1:
return WSDISPLAYIO_DEPTH_1;
case 4:
return WSDISPLAYIO_DEPTH_4;
case 8:
return WSDISPLAYIO_DEPTH_8;
case 15:
return WSDISPLAYIO_DEPTH_15;
case 16:
return WSDISPLAYIO_DEPTH_16;
case 24:
switch (bpp) {
case 24:
return WSDISPLAYIO_DEPTH_24_24;
case 32:
return WSDISPLAYIO_DEPTH_24_32;
}
}
return 0;
}
int
vesafb_get_supported_depth(struct vga_pci_softc *sc)
{
int i, depths;
struct modeinfoblock mi;
depths = 0;
if (vesabios_softc == NULL || vesabios_softc->sc_nmodes == 0)
return 0;
for (i = 0; i < vesabios_softc->sc_nmodes; i++) {
vesafb_get_mode_info(sc, vesabios_softc->sc_modes[i], &mi);
depths |= vesafb_getdepthflag(&mi);
}
return depths;
}
|