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
28 // --- 2D Acceleration Functions --
\r
29 void Vesa_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour);
\r
30 void Vesa_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H);
\r
33 MODULE_DEFINE(0, VERSION, Vesa, Vesa_Install, NULL, "PCI", "VM8086", NULL);
\r
34 tDevFS_Driver gVesa_DriverStruct = {
\r
38 .Write = Vesa_Write,
\r
42 tSpinlock glVesa_Lock;
\r
43 tVM8086 *gpVesa_BiosState;
\r
44 int giVesaCurrentMode = 0;
\r
45 int giVesaCurrentFormat = VIDEO_BUFFMT_TEXT;
\r
46 int giVesaDriverId = -1;
\r
47 char *gpVesa_Framebuffer = (void*)VESA_DEFAULT_FRAMEBUFFER;
\r
48 tVesa_Mode *gVesa_Modes;
\r
49 int giVesaModeCount = 0;
\r
50 int giVesaPageCount = 0;
\r
51 tDrvUtil_Video_2DHandlers gVesa_2DFunctions = {
\r
58 int Vesa_Install(char **Arguments)
\r
60 tVesa_CallInfo *info;
\r
62 tVesa_CallModeInfo *modeinfo;
\r
63 tFarPtr modeinfoPtr;
\r
67 // Allocate Info Block
\r
68 gpVesa_BiosState = VM8086_Init();
\r
69 info = VM8086_Allocate(gpVesa_BiosState, 512, &infoPtr.seg, &infoPtr.ofs);
\r
70 modeinfo = VM8086_Allocate(gpVesa_BiosState, 512, &modeinfoPtr.seg, &modeinfoPtr.ofs);
\r
71 // Set Requested Version
\r
72 memcpy(info->signature, "VBE2", 4);
\r
74 gpVesa_BiosState->AX = 0x4F00;
\r
75 gpVesa_BiosState->ES = infoPtr.seg; gpVesa_BiosState->DI = infoPtr.ofs;
\r
77 VM8086_Int(gpVesa_BiosState, 0x10);
\r
78 if(gpVesa_BiosState->AX != 0x004F) {
\r
79 Log_Warning("VESA", "Vesa_Install - VESA/VBE Unsupported (AX = 0x%x)", gpVesa_BiosState->AX);
\r
80 return MODULE_ERR_NOTNEEDED;
\r
83 Log_Debug("VESA", "info->VideoModes = %04x:%04x", info->VideoModes.seg, info->VideoModes.ofs);
\r
84 modes = (Uint16 *) VM8086_GetPointer(gpVesa_BiosState, info->VideoModes.seg, info->VideoModes.ofs);
\r
87 for( giVesaModeCount = 0; modes[giVesaModeCount] != 0xFFFF; giVesaModeCount++ );
\r
88 gVesa_Modes = (tVesa_Mode *)malloc( giVesaModeCount * sizeof(tVesa_Mode) );
\r
91 gVesa_Modes[0].width = 80;
\r
92 gVesa_Modes[0].height = 25;
\r
93 gVesa_Modes[0].bpp = 12;
\r
94 gVesa_Modes[0].code = 0x3;
\r
95 gVesa_Modes[0].flags = 1;
\r
96 gVesa_Modes[0].fbSize = 80*25*2;
\r
97 gVesa_Modes[0].framebuffer = 0xB8000;
\r
99 for( i = 1; i < giVesaModeCount; i++ )
\r
101 gVesa_Modes[i].code = modes[i];
\r
103 gpVesa_BiosState->AX = 0x4F01;
\r
104 gpVesa_BiosState->CX = gVesa_Modes[i].code;
\r
105 gpVesa_BiosState->ES = modeinfoPtr.seg;
\r
106 gpVesa_BiosState->DI = modeinfoPtr.ofs;
\r
107 VM8086_Int(gpVesa_BiosState, 0x10);
\r
110 gVesa_Modes[i].flags = 0;
\r
111 if ( (modeinfo->attributes & 0x90) == 0x90 )
\r
113 gVesa_Modes[i].flags |= FLAG_LFB;
\r
114 gVesa_Modes[i].framebuffer = modeinfo->physbase;
\r
115 gVesa_Modes[i].fbSize = modeinfo->Xres*modeinfo->Yres*modeinfo->bpp/8;
\r
117 gVesa_Modes[i].framebuffer = 0;
\r
118 gVesa_Modes[i].fbSize = 0;
\r
121 gVesa_Modes[i].pitch = modeinfo->pitch;
\r
122 gVesa_Modes[i].width = modeinfo->Xres;
\r
123 gVesa_Modes[i].height = modeinfo->Yres;
\r
124 gVesa_Modes[i].bpp = modeinfo->bpp;
\r
127 Log_Log("VESA", "0x%x - %ix%ix%i",
\r
128 gVesa_Modes[i].code, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
134 giVesaDriverId = DevFS_AddDevice( &gVesa_DriverStruct );
\r
135 if(giVesaDriverId == -1) return MODULE_ERR_MISC;
\r
137 return MODULE_ERR_OK;
\r
140 /* Read from the framebuffer
\r
142 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 off, Uint64 len, void *buffer)
\r
145 Log("Vesa_Read: () - NULL\n");
\r
151 * \brief Write to the framebuffer
\r
153 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
155 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
157 if(Buffer == NULL) {
\r
162 // Default Text mode
\r
163 if( giVesaCurrentMode == 0 )
\r
165 Uint16 *fb = (Uint16*)(KERNEL_BASE|0xB8000);
\r
166 tVT_Char *chars = Buffer;
\r
169 Length /= sizeof(tVT_Char);
\r
170 Offset /= sizeof(tVT_Char);
\r
172 if( giVesaCurrentFormat != VIDEO_BUFFMT_TEXT ) {
\r
173 Log_Warning("VESA", "Vesa_Write - Mode 0 is not framebuffer");
\r
178 if( Offset + Length > 25*80 ) {
\r
179 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
185 LOG("fb = %p", fb);
\r
186 for(rem = Length; rem --; fb ++, chars++)
\r
188 if( chars->Ch < 0x80 )
\r
189 *fb = chars->Ch & 0x7F;
\r
193 *fb |= (chars->FGCol & 0x888) == 0x888 ? 0x8 : 0;
\r
194 *fb |= (chars->FGCol & 0x700) > 0x300 ? 0x4 : 0;
\r
195 *fb |= (chars->FGCol & 0x070) > 0x030 ? 0x2 : 0;
\r
196 *fb |= (chars->FGCol & 0x007) > 0x003 ? 0x1 : 0;
\r
197 *fb |= (chars->BGCol & 0x888) == 0x888 ? 0x80 : 0;
\r
198 *fb |= (chars->BGCol & 0x700) > 0x300 ? 0x40 : 0;
\r
199 *fb |= (chars->BGCol & 0x070) > 0x030 ? 0x20 : 0;
\r
200 *fb |= (chars->BGCol & 0x007) > 0x003 ? 0x10 : 0;
\r
201 //LOG("%08x (%03x,%03x) = %04x",
\r
202 // chars->Ch, chars->BGCol, chars->FGCol, *fb);
\r
204 Length *= sizeof(tVT_Char);
\r
205 LEAVE('X', Length);
\r
209 if( gVesa_Modes[giVesaCurrentMode].framebuffer == 0 ) {
\r
210 Log_Warning("VESA", "Vesa_Write - Non-LFB Modes not yet supported.");
\r
216 switch( giVesaCurrentFormat )
\r
218 case VIDEO_BUFFMT_TEXT:
\r
220 tVT_Char *chars = Buffer;
\r
221 int pitch = gVesa_Modes[giVesaCurrentMode].width;
\r
222 int widthInChars = gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth;
\r
223 int heightInChars = gVesa_Modes[giVesaCurrentMode].height/giVT_CharHeight;
\r
225 Uint32 *dest = (void*)gpVesa_Framebuffer;
\r
228 Length /= sizeof(tVT_Char);
\r
229 Offset /= sizeof(tVT_Char);
\r
231 LOG("gVesa_Modes[%i].width = %i", giVesaCurrentMode, gVesa_Modes[giVesaCurrentMode].width);
\r
232 x = Offset % widthInChars;
\r
233 y = Offset / widthInChars;
\r
234 LOG("(x,y) = (%i,%i) = [%i,%i]", x, y, x * giVT_CharWidth, y * giVT_CharHeight * pitch);
\r
235 LOG("(w,h) = (%i,%i) = [%i,%i]",
\r
236 (int)(Length % widthInChars),
\r
237 (int)(Length / widthInChars),
\r
238 (int)((Length % widthInChars) * giVT_CharWidth),
\r
239 (int)((Length / widthInChars) * giVT_CharHeight * pitch)
\r
243 if(y >= heightInChars) {
\r
248 if( Offset + Length > heightInChars*widthInChars ) {
\r
249 Log_Debug("VESA", "%i + %i > %i*%i (%i)",
\r
250 (int)Offset, (int)Length, heightInChars, widthInChars, heightInChars*widthInChars);
\r
251 Length = heightInChars*widthInChars - Offset;
\r
252 Log_Notice("VESA", "Clipping write size to %i characters", (int)Length);
\r
255 dest += y * giVT_CharHeight * pitch;
\r
256 dest += x * giVT_CharWidth;
\r
258 LOG("dest = %p", dest);
\r
260 for( i = 0; i < Length; i++ )
\r
264 dest + x*giVT_CharWidth, pitch,
\r
265 VT_Colour12to24(chars->BGCol),
\r
266 VT_Colour12to24(chars->FGCol)
\r
271 if( x >= widthInChars ) {
\r
274 dest += pitch*giVT_CharHeight;
\r
277 Length *= sizeof(tVT_Char);
\r
281 case VIDEO_BUFFMT_FRAMEBUFFER:
\r
283 Uint8 *destBuf = (Uint8*) ((Uint)gpVesa_Framebuffer + (Uint)Offset);
\r
285 if(gVesa_Modes[giVesaCurrentMode].fbSize < Offset+Length)
\r
287 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
292 LOG("buffer = %p", Buffer);
\r
293 LOG("Updating Framebuffer (%p to %p)", destBuf, destBuf + (Uint)Length);
\r
296 // Copy to Frambuffer
\r
297 memcpy(destBuf, Buffer, Length);
\r
299 LOG("BGA Framebuffer updated");
\r
303 case VIDEO_BUFFMT_2DSTREAM:
\r
304 Length = DrvUtil_Video_2DStream(
\r
305 NULL, // Single framebuffer, so Ent is unused
\r
306 Buffer, Length, &gVesa_2DFunctions, sizeof(gVesa_2DFunctions)
\r
315 LEAVE('X', Length);
\r
320 * \fn int Vesa_Ioctl(vfs_node *node, int id, void *data)
\r
321 * \brief Handle messages to the device
\r
323 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data)
\r
326 //Log_Debug("VESA", "Vesa_Ioctl: (Node=%p, ID=%i, Data=%p)", Node, ID, Data);
\r
329 case DRV_IOCTL_TYPE: return DRV_TYPE_VIDEO;
\r
330 case DRV_IOCTL_IDENT: memcpy("VESA", Data, 4); return 1;
\r
331 case DRV_IOCTL_VERSION: return VERSION;
\r
333 case VIDEO_IOCTL_GETSETMODE:
\r
334 if( !Data ) return giVesaCurrentMode;
\r
335 return Vesa_Int_SetMode( *(int*)Data );
\r
337 case VIDEO_IOCTL_FINDMODE:
\r
338 return Vesa_Int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
339 case VIDEO_IOCTL_MODEINFO:
\r
340 return Vesa_Int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
342 case VIDEO_IOCTL_SETBUFFORMAT:
\r
343 ret = giVesaCurrentFormat;
\r
345 //Log_Log("VESA", "Buffer mode to %i", *(int*)Data);
\r
346 giVesaCurrentFormat = *(int*)Data;
\r
350 case VIDEO_IOCTL_SETCURSOR: // Set cursor position
\r
353 case VIDEO_IOCTL_REQLFB: // Request Linear Framebuffer
\r
359 int Vesa_Int_SetMode(int mode)
\r
361 Log_Log("VESA", "Setting mode to %i", mode);
\r
363 // Sanity Check values
\r
364 if(mode < 0 || mode > giVesaModeCount) return -1;
\r
366 // Check for fast return
\r
367 if(mode == giVesaCurrentMode) return 1;
\r
369 LOCK( &glVesa_Lock );
\r
371 gpVesa_BiosState->AX = 0x4F02;
\r
372 gpVesa_BiosState->BX = gVesa_Modes[mode].code;
\r
373 if(gVesa_Modes[mode].flags & FLAG_LFB) {
\r
374 Log_Log("VESA", "Using LFB");
\r
375 gpVesa_BiosState->BX |= 0x4000; // Bit 14 - Use LFB
\r
379 VM8086_Int(gpVesa_BiosState, 0x10);
\r
382 if( (tVAddr)gpVesa_Framebuffer != VESA_DEFAULT_FRAMEBUFFER )
\r
383 MM_UnmapHWPages((tVAddr)gpVesa_Framebuffer, giVesaPageCount);
\r
384 giVesaPageCount = (gVesa_Modes[mode].fbSize + 0xFFF) >> 12;
\r
385 gpVesa_Framebuffer = (void*)MM_MapHWPages(gVesa_Modes[mode].framebuffer, giVesaPageCount);
\r
387 Log_Log("VESA", "Framebuffer (Phys) = 0x%x", gVesa_Modes[mode].framebuffer);
\r
388 Log_Log("VESA", "Framebuffer (Virt) = 0x%x", gpVesa_Framebuffer);
\r
391 giVesaCurrentMode = mode;
\r
393 RELEASE( &glVesa_Lock );
\r
398 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data)
\r
401 int best = -1, bestFactor = 1000;
\r
404 ENTER("idata->width idata->height idata->bpp", data->width, data->height, data->bpp);
\r
406 for(i=0;i<giVesaModeCount;i++)
\r
408 LOG("Mode %i (%ix%ix%i)", i, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
410 if(gVesa_Modes[i].width == data->width
\r
411 && gVesa_Modes[i].height == data->height
\r
412 && gVesa_Modes[i].bpp == data->bpp)
\r
419 tmp = gVesa_Modes[i].width * gVesa_Modes[i].height * gVesa_Modes[i].bpp;
\r
420 tmp -= data->width * data->height * data->bpp;
\r
421 tmp = tmp < 0 ? -tmp : tmp;
\r
422 factor = tmp * 100 / (data->width * data->height * data->bpp);
\r
424 LOG("factor = %i", factor);
\r
426 if(factor < bestFactor)
\r
428 bestFactor = factor;
\r
433 data->width = gVesa_Modes[best].width;
\r
434 data->height = gVesa_Modes[best].height;
\r
435 data->bpp = gVesa_Modes[best].bpp;
\r
440 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data)
\r
442 if(data->id < 0 || data->id > giVesaModeCount) return -1;
\r
443 data->width = gVesa_Modes[data->id].width;
\r
444 data->height = gVesa_Modes[data->id].height;
\r
445 data->bpp = gVesa_Modes[data->id].bpp;
\r
449 // ------------------------
\r
450 // --- 2D Accelleration ---
\r
451 // ------------------------
\r
452 void Vesa_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
\r
454 int scrnwidth = gVesa_Modes[giVesaCurrentMode].width;
\r
455 Uint32 *buf = (Uint32*)gpVesa_Framebuffer + Y*scrnwidth + X;
\r
457 memsetd(buf, Colour, W);
\r
462 void Vesa_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
\r
464 int scrnpitch = gVesa_Modes[giVesaCurrentMode].pitch;
\r
465 int dst = DstY*scrnpitch + DstX;
\r
466 int src = SrcY*scrnpitch + SrcX;
\r
469 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
\r
470 // Ent, DstX, DstY, SrcX, SrcY, W, H);
\r
472 if(SrcX + W > gVesa_Modes[giVesaCurrentMode].width)
\r
473 W = gVesa_Modes[giVesaCurrentMode].width - SrcX;
\r
474 if(DstX + W > gVesa_Modes[giVesaCurrentMode].width)
\r
475 W = gVesa_Modes[giVesaCurrentMode].width - DstX;
\r
476 if(SrcY + H > gVesa_Modes[giVesaCurrentMode].height)
\r
477 H = gVesa_Modes[giVesaCurrentMode].height - SrcY;
\r
478 if(DstY + H > gVesa_Modes[giVesaCurrentMode].height)
\r
479 H = gVesa_Modes[giVesaCurrentMode].height - DstY;
\r
481 //Debug("W = %i, H = %i", W, H);
\r
485 dst += H*scrnpitch;
\r
486 src += H*scrnpitch;
\r
491 for( tmp = W; tmp --; ) {
\r
492 *(Uint32*)(gpVesa_Framebuffer + dst + tmp) = *(Uint32*)(gpVesa_Framebuffer + src + tmp);
\r
497 // Normal copy is OK
\r
499 memcpy((void*)gpVesa_Framebuffer + dst, (void*)gpVesa_Framebuffer + src, W*sizeof(Uint32));
\r