3 * Video BIOS Extensions (Vesa) Driver
\r
6 #define VERSION 0x100
\r
10 #include <tpl_drv_video.h>
\r
11 #include <fs_devfs.h>
\r
12 #include <modules.h>
\r
16 // === CONSTANTS ===
\r
17 #define FLAG_LFB 0x1
\r
19 // === PROTOTYPES ===
\r
20 int Vesa_Install(char **Arguments);
\r
21 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
22 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
23 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data);
\r
24 int Vesa_Int_SetMode(int Mode);
\r
25 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data);
\r
26 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data);
\r
29 MODULE_DEFINE(0, VERSION, Vesa, Vesa_Install, NULL, "PCI", "VM8086", NULL);
\r
30 tDevFS_Driver gVesa_DriverStruct = {
\r
34 .Write = Vesa_Write,
\r
38 tSpinlock glVesa_Lock;
\r
39 tVM8086 *gpVesa_BiosState;
\r
40 int giVesaCurrentMode = 0;
\r
41 int giVesaCurrentFormat = VIDEO_BUFFMT_TEXT;
\r
42 int giVesaDriverId = -1;
\r
43 char *gVesaFramebuffer = (void*)0xC00A0000;
\r
44 tVesa_Mode *gVesa_Modes;
\r
45 int giVesaModeCount = 0;
\r
46 int giVesaPageCount = 0;
\r
49 int Vesa_Install(char **Arguments)
\r
51 tVesa_CallInfo *info;
\r
53 tVesa_CallModeInfo *modeinfo;
\r
54 tFarPtr modeinfoPtr;
\r
58 // Allocate Info Block
\r
59 gpVesa_BiosState = VM8086_Init();
\r
60 info = VM8086_Allocate(gpVesa_BiosState, 512, &infoPtr.seg, &infoPtr.ofs);
\r
61 modeinfo = VM8086_Allocate(gpVesa_BiosState, 512, &modeinfoPtr.seg, &modeinfoPtr.ofs);
\r
62 // Set Requested Version
\r
63 memcpy(info->signature, "VBE2", 4);
\r
65 gpVesa_BiosState->AX = 0x4F00;
\r
66 gpVesa_BiosState->ES = infoPtr.seg; gpVesa_BiosState->DI = infoPtr.ofs;
\r
68 VM8086_Int(gpVesa_BiosState, 0x10);
\r
69 if(gpVesa_BiosState->AX != 0x004F) {
\r
70 Log_Warning("Vesa", "Vesa_Install - VESA/VBE Unsupported (AX = 0x%x)\n", gpVesa_BiosState->AX);
\r
71 return MODULE_ERR_NOTNEEDED;
\r
74 Log_Debug("Vesa", "info->VideoModes = %04x:%04x", info->VideoModes.seg, info->VideoModes.ofs);
\r
75 modes = (Uint16 *) VM8086_GetPointer(gpVesa_BiosState, info->VideoModes.seg, info->VideoModes.ofs);
\r
78 for( giVesaModeCount = 0; modes[giVesaModeCount] != 0xFFFF; giVesaModeCount++ );
\r
79 gVesa_Modes = (tVesa_Mode *)malloc( giVesaModeCount * sizeof(tVesa_Mode) );
\r
82 gVesa_Modes[0].width = 80;
\r
83 gVesa_Modes[0].height = 25;
\r
84 gVesa_Modes[0].bpp = 12;
\r
85 gVesa_Modes[0].code = 0x3;
\r
86 gVesa_Modes[0].flags = 1;
\r
87 gVesa_Modes[0].fbSize = 80*25*2;
\r
88 gVesa_Modes[0].framebuffer = 0xB8000;
\r
90 for( i = 1; i < giVesaModeCount; i++ )
\r
92 gVesa_Modes[i].code = modes[i];
\r
94 gpVesa_BiosState->AX = 0x4F01;
\r
95 gpVesa_BiosState->CX = gVesa_Modes[i].code;
\r
96 gpVesa_BiosState->ES = modeinfoPtr.seg;
\r
97 gpVesa_BiosState->DI = modeinfoPtr.ofs;
\r
98 VM8086_Int(gpVesa_BiosState, 0x10);
\r
101 gVesa_Modes[i].flags = 0;
\r
102 if ( (modeinfo->attributes & 0x90) == 0x90 )
\r
104 gVesa_Modes[i].flags |= FLAG_LFB;
\r
105 gVesa_Modes[i].framebuffer = modeinfo->physbase;
\r
106 gVesa_Modes[i].fbSize = modeinfo->Xres*modeinfo->Yres*modeinfo->bpp/8;
\r
108 gVesa_Modes[i].framebuffer = 0;
\r
109 gVesa_Modes[i].fbSize = 0;
\r
112 gVesa_Modes[i].pitch = modeinfo->pitch;
\r
113 gVesa_Modes[i].width = modeinfo->Xres;
\r
114 gVesa_Modes[i].height = modeinfo->Yres;
\r
115 gVesa_Modes[i].bpp = modeinfo->bpp;
\r
118 Log_Log("Vesa", "0x%x - %ix%ix%i",
\r
119 gVesa_Modes[i].code, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
125 giVesaDriverId = DevFS_AddDevice( &gVesa_DriverStruct );
\r
126 if(giVesaDriverId == -1) return MODULE_ERR_MISC;
\r
128 return MODULE_ERR_OK;
\r
131 /* Read from the framebuffer
\r
133 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 off, Uint64 len, void *buffer)
\r
136 LogF("Vesa_Read: () - NULL\n");
\r
142 * \brief Write to the framebuffer
\r
144 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
146 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
148 if(Buffer == NULL) {
\r
153 // Default Text mode
\r
154 if( giVesaCurrentMode == 0 )
\r
156 Uint16 *fb = (Uint16*)(KERNEL_BASE|0xB8000);
\r
157 tVT_Char *chars = Buffer;
\r
160 if( giVesaCurrentFormat != VIDEO_BUFFMT_TEXT ) {
\r
161 Log_Warning("VESA", "Vesa_Write - Mode 0 is not framebuffer");
\r
166 if( Offset + Length > 25*80*sizeof(tVT_Char) ) {
\r
167 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
172 fb += 2*(Offset/sizeof(tVT_Char));
\r
173 LOG("fb = %p", fb);
\r
174 for(rem = Length / sizeof(tVT_Char); rem --; fb ++, chars++)
\r
176 if( chars->Ch < 0x80 )
\r
177 *fb = chars->Ch & 0x7F;
\r
181 *fb |= (chars->FGCol & 0x888) == 0x888 ? 0x8 : 0;
\r
182 *fb |= (chars->FGCol & 0x700) > 0x300 ? 0x4 : 0;
\r
183 *fb |= (chars->FGCol & 0x070) > 0x030 ? 0x2 : 0;
\r
184 *fb |= (chars->FGCol & 0x007) > 0x003 ? 0x1 : 0;
\r
185 *fb |= (chars->BGCol & 0x888) == 0x888 ? 0x80 : 0;
\r
186 *fb |= (chars->BGCol & 0x700) > 0x300 ? 0x40 : 0;
\r
187 *fb |= (chars->BGCol & 0x070) > 0x030 ? 0x20 : 0;
\r
188 *fb |= (chars->BGCol & 0x007) > 0x003 ? 0x10 : 0;
\r
189 //LOG("%08x (%03x,%03x) = %04x",
\r
190 // chars->Ch, chars->BGCol, chars->FGCol, *fb);
\r
192 Length /= sizeof(tVT_Char);
\r
193 Length *= sizeof(tVT_Char);
\r
194 LEAVE('X', Length);
\r
198 if( gVesa_Modes[giVesaCurrentMode].framebuffer == 0 ) {
\r
199 Log_Warning("VESA", "Vesa_Write - Non-LFB Modes not yet supported.");
\r
205 switch( giVesaCurrentFormat )
\r
207 case VIDEO_BUFFMT_TEXT:
\r
209 tVT_Char *chars = Buffer;
\r
210 int pitch = gVesa_Modes[giVesaCurrentMode].width * giVT_CharWidth;
\r
215 Offset /= sizeof(tVT_Char);
\r
216 dest = (void*)gVesaFramebuffer;
\r
217 LOG("gVesa_Modes[%i].width = %i", giVesaCurrentMode, gVesa_Modes[giVesaCurrentMode].width);
\r
218 x = (Offset % (gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth)) * giVT_CharWidth;
\r
219 y = (Offset / (gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth)) * giVT_CharHeight;
\r
220 LOG("(x,y) = (%i,%i)", x, y);
\r
223 if(y > gVesa_Modes[giVesaCurrentMode].height) {
\r
229 dest += x * giVT_CharWidth;
\r
230 for( rem = Length / sizeof(tVT_Char); rem--; )
\r
235 VT_Colour12to24(chars->BGCol),
\r
236 VT_Colour12to24(chars->FGCol)
\r
239 dest += giVT_CharWidth;
\r
242 x += giVT_CharWidth;
\r
245 y += giVT_CharHeight;
\r
246 dest += pitch*(giVT_CharHeight-1);
\r
249 Length /= sizeof(tVT_Char);
\r
250 Length *= sizeof(tVT_Char);
\r
254 case VIDEO_BUFFMT_FRAMEBUFFER:
\r
256 Uint8 *destBuf = (Uint8*) ((Uint)gVesaFramebuffer + (Uint)Offset);
\r
258 if(gVesa_Modes[giVesaCurrentMode].fbSize < Offset+Length)
\r
260 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
265 LOG("buffer = %p", Buffer);
\r
266 LOG("Updating Framebuffer (%p to %p)", destBuf, destBuf + (Uint)Length);
\r
269 // Copy to Frambuffer
\r
270 memcpy(destBuf, Buffer, Length);
\r
272 LOG("BGA Framebuffer updated");
\r
280 LEAVE('X', Length);
\r
285 * \fn int Vesa_Ioctl(vfs_node *node, int id, void *data)
\r
286 * \brief Handle messages to the device
\r
288 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data)
\r
293 case DRV_IOCTL_TYPE: return DRV_TYPE_VIDEO;
\r
294 case DRV_IOCTL_IDENT: memcpy("VESA", Data, 4); return 1;
\r
295 case DRV_IOCTL_VERSION: return VERSION;
\r
297 case VIDEO_IOCTL_GETSETMODE:
\r
298 if( !Data ) return giVesaCurrentMode;
\r
299 return Vesa_Int_SetMode( *(int*)Data );
\r
301 case VIDEO_IOCTL_FINDMODE:
\r
302 return Vesa_Int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
303 case VIDEO_IOCTL_MODEINFO:
\r
304 return Vesa_Int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
306 case VIDEO_IOCTL_SETBUFFORMAT:
\r
307 ret = giVesaCurrentFormat;
\r
308 if(Data) giVesaCurrentFormat = *(int*)Data;
\r
311 case VIDEO_IOCTL_REQLFB: // Request Linear Framebuffer
\r
317 int Vesa_Int_SetMode(int mode)
\r
320 LogF("Vesa_Int_SetMode: (mode=%i)\n", mode);
\r
323 // Sanity Check values
\r
324 if(mode < 0 || mode > giVesaModeCount) return -1;
\r
326 // Check for fast return
\r
327 if(mode == giVesaCurrentMode) return 1;
\r
329 LOCK( &glVesa_Lock );
\r
331 gpVesa_BiosState->AX = 0x4F02;
\r
332 gpVesa_BiosState->BX = gVesa_Modes[mode].code;
\r
333 if(gVesa_Modes[mode].flags & FLAG_LFB) {
\r
334 Log_Log("VESA", "Using LFB");
\r
335 gpVesa_BiosState->BX |= 0x4000; // Bit 14 - Use LFB
\r
339 VM8086_Int(gpVesa_BiosState, 0x10);
\r
342 MM_UnmapHWPages((tVAddr)gVesaFramebuffer, giVesaPageCount);
\r
343 giVesaPageCount = (gVesa_Modes[mode].fbSize + 0xFFF) >> 12;
\r
344 gVesaFramebuffer = (void*)MM_MapHWPages(gVesa_Modes[mode].framebuffer, giVesaPageCount);
\r
346 LogF("Vesa", "Framebuffer (Phys) = 0x%x", gVesa_Modes[mode].framebuffer);
\r
347 LogF("Vesa", "Framebuffer (Virt) = 0x%x", gVesaFramebuffer);
\r
350 giVesaCurrentMode = mode;
\r
352 RELEASE( &glVesa_Lock );
\r
357 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data)
\r
360 int best = -1, bestFactor = 1000;
\r
363 ENTER("idata->width idata->height idata->bpp", data->width, data->height, data->bpp);
\r
365 for(i=0;i<giVesaModeCount;i++)
\r
367 LOG("Mode %i (%ix%ix%i)", i, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
369 if(gVesa_Modes[i].width == data->width
\r
370 && gVesa_Modes[i].height == data->height
\r
371 && gVesa_Modes[i].bpp == data->bpp)
\r
378 tmp = gVesa_Modes[i].width * gVesa_Modes[i].height * gVesa_Modes[i].bpp;
\r
379 tmp -= data->width * data->height * data->bpp;
\r
380 tmp = tmp < 0 ? -tmp : tmp;
\r
381 factor = tmp * 100 / (data->width * data->height * data->bpp);
\r
383 LOG("factor = %i", factor);
\r
385 if(factor < bestFactor)
\r
387 bestFactor = factor;
\r
392 data->width = gVesa_Modes[best].width;
\r
393 data->height = gVesa_Modes[best].height;
\r
394 data->bpp = gVesa_Modes[best].bpp;
\r
399 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data)
\r
401 if(data->id < 0 || data->id > giVesaModeCount) return -1;
\r
402 data->width = gVesa_Modes[data->id].width;
\r
403 data->height = gVesa_Modes[data->id].height;
\r
404 data->bpp = gVesa_Modes[data->id].bpp;
\r