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 = -1;
\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].width = modeinfo->Xres;
\r
113 gVesa_Modes[i].height = modeinfo->Yres;
\r
114 gVesa_Modes[i].bpp = modeinfo->bpp;
\r
117 Log_Log("Vesa", "0x%x - %ix%ix%i",
\r
118 gVesa_Modes[i].code, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
124 giVesaDriverId = DevFS_AddDevice( &gVesa_DriverStruct );
\r
125 if(giVesaDriverId == -1) return MODULE_ERR_MISC;
\r
127 return MODULE_ERR_OK;
\r
130 /* Read from the framebuffer
\r
132 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 off, Uint64 len, void *buffer)
\r
135 LogF("Vesa_Read: () - NULL\n");
\r
141 * \brief Write to the framebuffer
\r
143 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
145 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
147 if(Buffer == NULL) {
\r
152 // Default Text mode
\r
153 if( giVesaCurrentMode == 0 )
\r
155 Uint8 *fb = (Uint8 *)(KERNEL_BASE|0xB8000);
\r
156 Uint32 *buf = Buffer;
\r
159 if( giVesaCurrentFormat != VIDEO_BUFFMT_TEXT ) {
\r
160 Log_Warning("VESA", "Vesa_Write - Mode 0 is not framebuffer");
\r
165 if( Offset + Length > 25*80*8 ) {
\r
166 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
172 for(rem = Length / sizeof(tVT_Char); rem --; fb += 2)
\r
181 fb[1] |= (*buf & 0x888) == 0x888 ? 0x8 : 0;
\r
182 fb[1] |= (*buf & 0x700) > 0x300 ? 0x4 : 0;
\r
183 fb[1] |= (*buf & 0x070) > 0x030 ? 0x2 : 0;
\r
184 fb[1] |= (*buf & 0x007) > 0x003 ? 0x1 : 0;
\r
185 fb[1] |= (*buf & 0x888000) == 0x888000 ? 0x80 : 0;
\r
186 fb[1] |= (*buf & 0x700000) > 0x300000 ? 0x40 : 0;
\r
187 fb[1] |= (*buf & 0x070000) > 0x030000 ? 0x20 : 0;
\r
188 fb[1] |= (*buf & 0x007000) > 0x003000 ? 0x10 : 0;
\r
191 Length /= sizeof(tVT_Char);
\r
192 Length *= sizeof(tVT_Char);
\r
193 LEAVE('X', Length);
\r
197 if( gVesa_Modes[giVesaCurrentMode].framebuffer == 0 ) {
\r
198 Log_Warning("VESA", "Vesa_Write - Non-LFB Modes not yet supported.");
\r
204 switch( giVesaCurrentFormat )
\r
206 case VIDEO_BUFFMT_TEXT:
\r
208 tVT_Char *chars = Buffer;
\r
209 int pitch = gVesa_Modes[giVesaCurrentMode].width * giVT_CharWidth;
\r
214 Offset /= sizeof(tVT_Char);
\r
215 dest = (void*)gVesaFramebuffer;
\r
216 x = (Offset % gVesa_Modes[giVesaCurrentMode].width) * giVT_CharWidth;
\r
217 y = (Offset / gVesa_Modes[giVesaCurrentMode].width) * giVT_CharHeight;
\r
220 if(y > gVesa_Modes[giVesaCurrentMode].height) {
\r
226 dest += x * giVT_CharWidth;
\r
227 for( rem = Length / sizeof(tVT_Char); rem--; )
\r
232 VT_Colour12to24(chars->BGCol),
\r
233 VT_Colour12to24(chars->FGCol)
\r
236 dest += giVT_CharWidth;
\r
239 x += giVT_CharWidth;
\r
242 y += giVT_CharHeight;
\r
243 dest += pitch*(giVT_CharHeight-1);
\r
246 Length /= sizeof(tVT_Char);
\r
247 Length *= sizeof(tVT_Char);
\r
251 case VIDEO_BUFFMT_FRAMEBUFFER:
\r
253 Uint8 *destBuf = (Uint8*) ((Uint)gVesaFramebuffer + (Uint)Offset);
\r
255 if(gVesa_Modes[giVesaCurrentMode].fbSize < Offset+Length)
\r
257 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
262 LOG("buffer = %p", Buffer);
\r
263 LOG("Updating Framebuffer (%p to %p)", destBuf, destBuf + (Uint)Length);
\r
266 // Copy to Frambuffer
\r
267 memcpy(destBuf, Buffer, Length);
\r
269 LOG("BGA Framebuffer updated");
\r
277 LEAVE('X', Length);
\r
282 * \fn int Vesa_Ioctl(vfs_node *node, int id, void *data)
\r
283 * \brief Handle messages to the device
\r
285 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data)
\r
290 case DRV_IOCTL_TYPE: return DRV_TYPE_VIDEO;
\r
291 case DRV_IOCTL_IDENT: memcpy("VESA", Data, 4); return 1;
\r
292 case DRV_IOCTL_VERSION: return VERSION;
\r
294 case VIDEO_IOCTL_GETSETMODE:
\r
295 if( !Data ) return giVesaCurrentMode;
\r
296 return Vesa_Int_SetMode( *(int*)Data );
\r
298 case VIDEO_IOCTL_FINDMODE:
\r
299 return Vesa_Int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
300 case VIDEO_IOCTL_MODEINFO:
\r
301 return Vesa_Int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
303 case VIDEO_IOCTL_SETBUFFORMAT:
\r
304 ret = giVesaCurrentFormat;
\r
305 if(Data) giVesaCurrentFormat = *(int*)Data;
\r
308 case VIDEO_IOCTL_REQLFB: // Request Linear Framebuffer
\r
314 int Vesa_Int_SetMode(int mode)
\r
317 LogF("Vesa_Int_SetMode: (mode=%i)\n", mode);
\r
320 // Sanity Check values
\r
321 if(mode < 0 || mode > giVesaModeCount) return -1;
\r
323 // Check for fast return
\r
324 if(mode == giVesaCurrentMode) return 1;
\r
326 LOCK( &glVesa_Lock );
\r
328 gpVesa_BiosState->AX = 0x4F02;
\r
329 gpVesa_BiosState->BX = gVesa_Modes[mode].code;
\r
330 if(gVesa_Modes[mode].flags & FLAG_LFB) {
\r
331 Log_Log("VESA", "Using LFB");
\r
332 gpVesa_BiosState->BX |= 0x4000; // Bit 14 - Use LFB
\r
336 VM8086_Int(gpVesa_BiosState, 0x10);
\r
339 MM_UnmapHWPages((tVAddr)gVesaFramebuffer, giVesaPageCount);
\r
340 giVesaPageCount = (gVesa_Modes[mode].fbSize + 0xFFF) >> 12;
\r
341 gVesaFramebuffer = (void*)MM_MapHWPages(gVesa_Modes[mode].framebuffer, giVesaPageCount);
\r
343 LogF("Vesa", "Framebuffer (Phys) = 0x%x", gVesa_Modes[mode].framebuffer);
\r
344 LogF("Vesa", "Framebuffer (Virt) = 0x%x", gVesaFramebuffer);
\r
347 giVesaCurrentMode = mode;
\r
349 RELEASE( &glVesa_Lock );
\r
354 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data)
\r
357 int best = -1, bestFactor = 1000;
\r
360 ENTER("idata->width idata->height idata->bpp", data->width, data->height, data->bpp);
\r
362 for(i=0;i<giVesaModeCount;i++)
\r
364 LOG("Mode %i (%ix%ix%i)", i, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
366 if(gVesa_Modes[i].width == data->width
\r
367 && gVesa_Modes[i].height == data->height
\r
368 && gVesa_Modes[i].bpp == data->bpp)
\r
375 tmp = gVesa_Modes[i].width * gVesa_Modes[i].height * gVesa_Modes[i].bpp;
\r
376 tmp -= data->width * data->height * data->bpp;
\r
377 tmp = tmp < 0 ? -tmp : tmp;
\r
378 factor = tmp * 100 / (data->width * data->height * data->bpp);
\r
380 LOG("factor = %i", factor);
\r
382 if(factor < bestFactor)
\r
384 bestFactor = factor;
\r
389 data->width = gVesa_Modes[best].width;
\r
390 data->height = gVesa_Modes[best].height;
\r
391 data->bpp = gVesa_Modes[best].bpp;
\r
396 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data)
\r
398 if(data->id < 0 || data->id > giVesaModeCount) return -1;
\r
399 data->width = gVesa_Modes[data->id].width;
\r
400 data->height = gVesa_Modes[data->id].height;
\r
401 data->bpp = gVesa_Modes[data->id].bpp;
\r