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
18 #define VESA_DEFAULT_FRAMEBUFFER (KERNEL_BASE|0xA0000)
\r
20 // === PROTOTYPES ===
\r
21 int Vesa_Install(char **Arguments);
\r
22 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
23 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
24 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data);
\r
25 int Vesa_Int_SetMode(int Mode);
\r
26 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data);
\r
27 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data);
\r
30 MODULE_DEFINE(0, VERSION, Vesa, Vesa_Install, NULL, "PCI", "VM8086", NULL);
\r
31 tDevFS_Driver gVesa_DriverStruct = {
\r
35 .Write = Vesa_Write,
\r
39 tSpinlock glVesa_Lock;
\r
40 tVM8086 *gpVesa_BiosState;
\r
41 int giVesaCurrentMode = 0;
\r
42 int giVesaCurrentFormat = VIDEO_BUFFMT_TEXT;
\r
43 int giVesaDriverId = -1;
\r
44 char *gpVesa_Framebuffer = (void*)VESA_DEFAULT_FRAMEBUFFER;
\r
45 tVesa_Mode *gVesa_Modes;
\r
46 int giVesaModeCount = 0;
\r
47 int giVesaPageCount = 0;
\r
50 int Vesa_Install(char **Arguments)
\r
52 tVesa_CallInfo *info;
\r
54 tVesa_CallModeInfo *modeinfo;
\r
55 tFarPtr modeinfoPtr;
\r
59 // Allocate Info Block
\r
60 gpVesa_BiosState = VM8086_Init();
\r
61 info = VM8086_Allocate(gpVesa_BiosState, 512, &infoPtr.seg, &infoPtr.ofs);
\r
62 modeinfo = VM8086_Allocate(gpVesa_BiosState, 512, &modeinfoPtr.seg, &modeinfoPtr.ofs);
\r
63 // Set Requested Version
\r
64 memcpy(info->signature, "VBE2", 4);
\r
66 gpVesa_BiosState->AX = 0x4F00;
\r
67 gpVesa_BiosState->ES = infoPtr.seg; gpVesa_BiosState->DI = infoPtr.ofs;
\r
69 VM8086_Int(gpVesa_BiosState, 0x10);
\r
70 if(gpVesa_BiosState->AX != 0x004F) {
\r
71 Log_Warning("VESA", "Vesa_Install - VESA/VBE Unsupported (AX = 0x%x)", gpVesa_BiosState->AX);
\r
72 return MODULE_ERR_NOTNEEDED;
\r
75 Log_Debug("VESA", "info->VideoModes = %04x:%04x", info->VideoModes.seg, info->VideoModes.ofs);
\r
76 modes = (Uint16 *) VM8086_GetPointer(gpVesa_BiosState, info->VideoModes.seg, info->VideoModes.ofs);
\r
79 for( giVesaModeCount = 0; modes[giVesaModeCount] != 0xFFFF; giVesaModeCount++ );
\r
80 gVesa_Modes = (tVesa_Mode *)malloc( giVesaModeCount * sizeof(tVesa_Mode) );
\r
83 gVesa_Modes[0].width = 80;
\r
84 gVesa_Modes[0].height = 25;
\r
85 gVesa_Modes[0].bpp = 12;
\r
86 gVesa_Modes[0].code = 0x3;
\r
87 gVesa_Modes[0].flags = 1;
\r
88 gVesa_Modes[0].fbSize = 80*25*2;
\r
89 gVesa_Modes[0].framebuffer = 0xB8000;
\r
91 for( i = 1; i < giVesaModeCount; i++ )
\r
93 gVesa_Modes[i].code = modes[i];
\r
95 gpVesa_BiosState->AX = 0x4F01;
\r
96 gpVesa_BiosState->CX = gVesa_Modes[i].code;
\r
97 gpVesa_BiosState->ES = modeinfoPtr.seg;
\r
98 gpVesa_BiosState->DI = modeinfoPtr.ofs;
\r
99 VM8086_Int(gpVesa_BiosState, 0x10);
\r
102 gVesa_Modes[i].flags = 0;
\r
103 if ( (modeinfo->attributes & 0x90) == 0x90 )
\r
105 gVesa_Modes[i].flags |= FLAG_LFB;
\r
106 gVesa_Modes[i].framebuffer = modeinfo->physbase;
\r
107 gVesa_Modes[i].fbSize = modeinfo->Xres*modeinfo->Yres*modeinfo->bpp/8;
\r
109 gVesa_Modes[i].framebuffer = 0;
\r
110 gVesa_Modes[i].fbSize = 0;
\r
113 gVesa_Modes[i].pitch = modeinfo->pitch;
\r
114 gVesa_Modes[i].width = modeinfo->Xres;
\r
115 gVesa_Modes[i].height = modeinfo->Yres;
\r
116 gVesa_Modes[i].bpp = modeinfo->bpp;
\r
119 Log_Log("VESA", "0x%x - %ix%ix%i",
\r
120 gVesa_Modes[i].code, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
126 giVesaDriverId = DevFS_AddDevice( &gVesa_DriverStruct );
\r
127 if(giVesaDriverId == -1) return MODULE_ERR_MISC;
\r
129 return MODULE_ERR_OK;
\r
132 /* Read from the framebuffer
\r
134 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 off, Uint64 len, void *buffer)
\r
137 Log("Vesa_Read: () - NULL\n");
\r
143 * \brief Write to the framebuffer
\r
145 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
147 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
149 if(Buffer == NULL) {
\r
154 // Default Text mode
\r
155 if( giVesaCurrentMode == 0 )
\r
157 Uint16 *fb = (Uint16*)(KERNEL_BASE|0xB8000);
\r
158 tVT_Char *chars = Buffer;
\r
161 Length /= sizeof(tVT_Char);
\r
162 Offset /= sizeof(tVT_Char);
\r
164 if( giVesaCurrentFormat != VIDEO_BUFFMT_TEXT ) {
\r
165 Log_Warning("VESA", "Vesa_Write - Mode 0 is not framebuffer");
\r
170 if( Offset + Length > 25*80 ) {
\r
171 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
177 LOG("fb = %p", fb);
\r
178 for(rem = Length; rem --; fb ++, chars++)
\r
180 if( chars->Ch < 0x80 )
\r
181 *fb = chars->Ch & 0x7F;
\r
185 *fb |= (chars->FGCol & 0x888) == 0x888 ? 0x8 : 0;
\r
186 *fb |= (chars->FGCol & 0x700) > 0x300 ? 0x4 : 0;
\r
187 *fb |= (chars->FGCol & 0x070) > 0x030 ? 0x2 : 0;
\r
188 *fb |= (chars->FGCol & 0x007) > 0x003 ? 0x1 : 0;
\r
189 *fb |= (chars->BGCol & 0x888) == 0x888 ? 0x80 : 0;
\r
190 *fb |= (chars->BGCol & 0x700) > 0x300 ? 0x40 : 0;
\r
191 *fb |= (chars->BGCol & 0x070) > 0x030 ? 0x20 : 0;
\r
192 *fb |= (chars->BGCol & 0x007) > 0x003 ? 0x10 : 0;
\r
193 //LOG("%08x (%03x,%03x) = %04x",
\r
194 // chars->Ch, chars->BGCol, chars->FGCol, *fb);
\r
196 Length *= sizeof(tVT_Char);
\r
197 LEAVE('X', Length);
\r
201 if( gVesa_Modes[giVesaCurrentMode].framebuffer == 0 ) {
\r
202 Log_Warning("VESA", "Vesa_Write - Non-LFB Modes not yet supported.");
\r
208 switch( giVesaCurrentFormat )
\r
210 case VIDEO_BUFFMT_TEXT:
\r
212 tVT_Char *chars = Buffer;
\r
213 int pitch = gVesa_Modes[giVesaCurrentMode].width;
\r
214 int widthInChars = gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth;
\r
215 int heightInChars = gVesa_Modes[giVesaCurrentMode].height/giVT_CharHeight;
\r
217 Uint32 *dest = (void*)gpVesa_Framebuffer;
\r
220 Length /= sizeof(tVT_Char);
\r
221 Offset /= sizeof(tVT_Char);
\r
223 LOG("gVesa_Modes[%i].width = %i", giVesaCurrentMode, gVesa_Modes[giVesaCurrentMode].width);
\r
224 x = Offset % widthInChars;
\r
225 y = Offset / widthInChars;
\r
226 LOG("(x,y) = (%i,%i) = [%i,%i]", x, y, x * giVT_CharWidth, y * giVT_CharHeight * pitch);
\r
227 LOG("(w,h) = (%i,%i) = [%i,%i]",
\r
228 (int)(Length % widthInChars),
\r
229 (int)(Length / widthInChars),
\r
230 (int)((Length % widthInChars) * giVT_CharWidth),
\r
231 (int)((Length / widthInChars) * giVT_CharHeight * pitch)
\r
235 if(y > heightInChars) {
\r
240 if( Offset + Length > heightInChars*widthInChars ) {
\r
241 Length = heightInChars*widthInChars - Offset;
\r
242 Log_Notice("VESA", "Clipping write size to %i characters", (int)Length);
\r
245 dest += y * giVT_CharHeight * pitch;
\r
246 dest += x * giVT_CharWidth;
\r
248 LOG("dest = %p", dest);
\r
250 for( i = 0; i < Length; i++ )
\r
254 dest + x*giVT_CharWidth, pitch,
\r
255 VT_Colour12to24(chars->BGCol),
\r
256 VT_Colour12to24(chars->FGCol)
\r
261 if( x >= widthInChars ) {
\r
264 dest += pitch*giVT_CharHeight;
\r
267 Length *= sizeof(tVT_Char);
\r
271 case VIDEO_BUFFMT_FRAMEBUFFER:
\r
273 Uint8 *destBuf = (Uint8*) ((Uint)gpVesa_Framebuffer + (Uint)Offset);
\r
275 if(gVesa_Modes[giVesaCurrentMode].fbSize < Offset+Length)
\r
277 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
282 LOG("buffer = %p", Buffer);
\r
283 LOG("Updating Framebuffer (%p to %p)", destBuf, destBuf + (Uint)Length);
\r
286 // Copy to Frambuffer
\r
287 memcpy(destBuf, Buffer, Length);
\r
289 LOG("BGA Framebuffer updated");
\r
297 LEAVE('X', Length);
\r
302 * \fn int Vesa_Ioctl(vfs_node *node, int id, void *data)
\r
303 * \brief Handle messages to the device
\r
305 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data)
\r
308 //Log_Debug("VESA", "Vesa_Ioctl: (Node=%p, ID=%i, Data=%p)", Node, ID, Data);
\r
311 case DRV_IOCTL_TYPE: return DRV_TYPE_VIDEO;
\r
312 case DRV_IOCTL_IDENT: memcpy("VESA", Data, 4); return 1;
\r
313 case DRV_IOCTL_VERSION: return VERSION;
\r
315 case VIDEO_IOCTL_GETSETMODE:
\r
316 if( !Data ) return giVesaCurrentMode;
\r
317 return Vesa_Int_SetMode( *(int*)Data );
\r
319 case VIDEO_IOCTL_FINDMODE:
\r
320 return Vesa_Int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
321 case VIDEO_IOCTL_MODEINFO:
\r
322 return Vesa_Int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
324 case VIDEO_IOCTL_SETBUFFORMAT:
\r
325 ret = giVesaCurrentFormat;
\r
327 Log_Log("VESA", "Buffer mode to %i", *(int*)Data);
\r
328 giVesaCurrentFormat = *(int*)Data;
\r
332 case VIDEO_IOCTL_SETCURSOR: // Set cursor position
\r
335 case VIDEO_IOCTL_REQLFB: // Request Linear Framebuffer
\r
341 int Vesa_Int_SetMode(int mode)
\r
343 Log_Log("VESA", "Setting mode to %i", mode);
\r
345 // Sanity Check values
\r
346 if(mode < 0 || mode > giVesaModeCount) return -1;
\r
348 // Check for fast return
\r
349 if(mode == giVesaCurrentMode) return 1;
\r
351 LOCK( &glVesa_Lock );
\r
353 gpVesa_BiosState->AX = 0x4F02;
\r
354 gpVesa_BiosState->BX = gVesa_Modes[mode].code;
\r
355 if(gVesa_Modes[mode].flags & FLAG_LFB) {
\r
356 Log_Log("VESA", "Using LFB");
\r
357 gpVesa_BiosState->BX |= 0x4000; // Bit 14 - Use LFB
\r
361 VM8086_Int(gpVesa_BiosState, 0x10);
\r
364 if( (tVAddr)gpVesa_Framebuffer != VESA_DEFAULT_FRAMEBUFFER )
\r
365 MM_UnmapHWPages((tVAddr)gpVesa_Framebuffer, giVesaPageCount);
\r
366 giVesaPageCount = (gVesa_Modes[mode].fbSize + 0xFFF) >> 12;
\r
367 gpVesa_Framebuffer = (void*)MM_MapHWPages(gVesa_Modes[mode].framebuffer, giVesaPageCount);
\r
369 Log_Log("VESA", "Framebuffer (Phys) = 0x%x", gVesa_Modes[mode].framebuffer);
\r
370 Log_Log("VESA", "Framebuffer (Virt) = 0x%x", gpVesa_Framebuffer);
\r
373 giVesaCurrentMode = mode;
\r
375 RELEASE( &glVesa_Lock );
\r
380 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data)
\r
383 int best = -1, bestFactor = 1000;
\r
386 ENTER("idata->width idata->height idata->bpp", data->width, data->height, data->bpp);
\r
388 for(i=0;i<giVesaModeCount;i++)
\r
390 LOG("Mode %i (%ix%ix%i)", i, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
392 if(gVesa_Modes[i].width == data->width
\r
393 && gVesa_Modes[i].height == data->height
\r
394 && gVesa_Modes[i].bpp == data->bpp)
\r
401 tmp = gVesa_Modes[i].width * gVesa_Modes[i].height * gVesa_Modes[i].bpp;
\r
402 tmp -= data->width * data->height * data->bpp;
\r
403 tmp = tmp < 0 ? -tmp : tmp;
\r
404 factor = tmp * 100 / (data->width * data->height * data->bpp);
\r
406 LOG("factor = %i", factor);
\r
408 if(factor < bestFactor)
\r
410 bestFactor = factor;
\r
415 data->width = gVesa_Modes[best].width;
\r
416 data->height = gVesa_Modes[best].height;
\r
417 data->bpp = gVesa_Modes[best].bpp;
\r
422 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data)
\r
424 if(data->id < 0 || data->id > giVesaModeCount) return -1;
\r
425 data->width = gVesa_Modes[data->id].width;
\r
426 data->height = gVesa_Modes[data->id].height;
\r
427 data->bpp = gVesa_Modes[data->id].bpp;
\r