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
19 #define BLINKING_CURSOR 0
\r
21 # define VESA_CURSOR_PERIOD 1000
\r
24 // === PROTOTYPES ===
\r
25 int Vesa_Install(char **Arguments);
\r
26 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
27 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
28 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data);
\r
29 int Vesa_Int_SetMode(int Mode);
\r
30 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data);
\r
31 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data);
\r
32 void Vesa_FlipCursor(void *Arg);
\r
33 // --- 2D Acceleration Functions --
\r
34 void Vesa_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour);
\r
35 void Vesa_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H);
\r
38 MODULE_DEFINE(0, VERSION, Vesa, Vesa_Install, NULL, "PCI", "VM8086", NULL);
\r
39 tDevFS_Driver gVesa_DriverStruct = {
\r
43 .Write = Vesa_Write,
\r
47 tSpinlock glVesa_Lock;
\r
48 tVM8086 *gpVesa_BiosState;
\r
49 int giVesaDriverId = -1;
\r
50 // --- Video Modes ---
\r
51 int giVesaCurrentMode = 0;
\r
52 int giVesaCurrentFormat = VIDEO_BUFFMT_TEXT;
\r
53 tVesa_Mode *gVesa_Modes;
\r
54 tVesa_Mode *gpVesaCurMode;
\r
55 int giVesaModeCount = 0;
\r
56 // --- Framebuffer ---
\r
57 char *gpVesa_Framebuffer = (void*)VESA_DEFAULT_FRAMEBUFFER;
\r
58 int giVesaPageCount = 0; //!< Framebuffer size in pages
\r
59 // --- Cursor Control ---
\r
60 int giVesaCursorX = -1;
\r
61 int giVesaCursorY = -1;
\r
62 int giVesaCursorTimer = -1; // Invalid timer
\r
63 // --- 2D Video Stream Handlers ---
\r
64 tDrvUtil_Video_2DHandlers gVesa_2DFunctions = {
\r
71 int Vesa_Install(char **Arguments)
\r
73 tVesa_CallInfo *info;
\r
75 tVesa_CallModeInfo *modeinfo;
\r
76 tFarPtr modeinfoPtr;
\r
80 // Allocate Info Block
\r
81 gpVesa_BiosState = VM8086_Init();
\r
82 info = VM8086_Allocate(gpVesa_BiosState, 512, &infoPtr.seg, &infoPtr.ofs);
\r
83 modeinfo = VM8086_Allocate(gpVesa_BiosState, 512, &modeinfoPtr.seg, &modeinfoPtr.ofs);
\r
84 // Set Requested Version
\r
85 memcpy(info->signature, "VBE2", 4);
\r
87 gpVesa_BiosState->AX = 0x4F00;
\r
88 gpVesa_BiosState->ES = infoPtr.seg; gpVesa_BiosState->DI = infoPtr.ofs;
\r
90 VM8086_Int(gpVesa_BiosState, 0x10);
\r
91 if(gpVesa_BiosState->AX != 0x004F) {
\r
92 Log_Warning("VESA", "Vesa_Install - VESA/VBE Unsupported (AX = 0x%x)", gpVesa_BiosState->AX);
\r
93 return MODULE_ERR_NOTNEEDED;
\r
96 Log_Debug("VESA", "info->VideoModes = %04x:%04x", info->VideoModes.seg, info->VideoModes.ofs);
\r
97 modes = (Uint16 *) VM8086_GetPointer(gpVesa_BiosState, info->VideoModes.seg, info->VideoModes.ofs);
\r
100 for( giVesaModeCount = 0; modes[giVesaModeCount] != 0xFFFF; giVesaModeCount++ );
\r
101 gVesa_Modes = (tVesa_Mode *)malloc( giVesaModeCount * sizeof(tVesa_Mode) );
\r
103 // Insert Text Mode
\r
104 gVesa_Modes[0].width = 80;
\r
105 gVesa_Modes[0].height = 25;
\r
106 gVesa_Modes[0].bpp = 12;
\r
107 gVesa_Modes[0].code = 0x3;
\r
108 gVesa_Modes[0].flags = 1;
\r
109 gVesa_Modes[0].fbSize = 80*25*2;
\r
110 gVesa_Modes[0].framebuffer = 0xB8000;
\r
112 for( i = 1; i < giVesaModeCount; i++ )
\r
114 gVesa_Modes[i].code = modes[i];
\r
116 gpVesa_BiosState->AX = 0x4F01;
\r
117 gpVesa_BiosState->CX = gVesa_Modes[i].code;
\r
118 gpVesa_BiosState->ES = modeinfoPtr.seg;
\r
119 gpVesa_BiosState->DI = modeinfoPtr.ofs;
\r
120 VM8086_Int(gpVesa_BiosState, 0x10);
\r
123 gVesa_Modes[i].flags = 0;
\r
124 if ( (modeinfo->attributes & 0x90) == 0x90 )
\r
126 gVesa_Modes[i].flags |= FLAG_LFB;
\r
127 gVesa_Modes[i].framebuffer = modeinfo->physbase;
\r
128 gVesa_Modes[i].fbSize = modeinfo->Xres*modeinfo->Yres*modeinfo->bpp/8;
\r
130 gVesa_Modes[i].framebuffer = 0;
\r
131 gVesa_Modes[i].fbSize = 0;
\r
134 gVesa_Modes[i].pitch = modeinfo->pitch;
\r
135 gVesa_Modes[i].width = modeinfo->Xres;
\r
136 gVesa_Modes[i].height = modeinfo->Yres;
\r
137 gVesa_Modes[i].bpp = modeinfo->bpp;
\r
140 Log_Log("VESA", "0x%x - %ix%ix%i",
\r
141 gVesa_Modes[i].code, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
147 giVesaDriverId = DevFS_AddDevice( &gVesa_DriverStruct );
\r
148 if(giVesaDriverId == -1) return MODULE_ERR_MISC;
\r
150 return MODULE_ERR_OK;
\r
153 /* Read from the framebuffer
\r
155 Uint64 Vesa_Read(tVFS_Node *Node, Uint64 off, Uint64 len, void *buffer)
\r
158 Log("Vesa_Read: () - NULL\n");
\r
164 * \brief Write to the framebuffer
\r
166 Uint64 Vesa_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
168 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
170 if(Buffer == NULL) {
\r
175 // Default Text mode
\r
176 if( giVesaCurrentMode == 0 )
\r
178 Uint16 *fb = (Uint16*)(KERNEL_BASE|0xB8000);
\r
179 tVT_Char *chars = Buffer;
\r
182 Length /= sizeof(tVT_Char);
\r
183 Offset /= sizeof(tVT_Char);
\r
185 if( giVesaCurrentFormat != VIDEO_BUFFMT_TEXT ) {
\r
186 Log_Warning("VESA", "Vesa_Write - Mode 0 is not framebuffer");
\r
191 if( Offset + Length > 25*80 ) {
\r
192 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
198 LOG("fb = %p", fb);
\r
199 for(rem = Length; rem --; fb ++, chars++)
\r
201 if( chars->Ch < 0x80 )
\r
202 *fb = chars->Ch & 0x7F;
\r
206 *fb |= (chars->FGCol & 0x888) == 0x888 ? 0x8 : 0;
\r
207 *fb |= (chars->FGCol & 0x700) > 0x300 ? 0x4 : 0;
\r
208 *fb |= (chars->FGCol & 0x070) > 0x030 ? 0x2 : 0;
\r
209 *fb |= (chars->FGCol & 0x007) > 0x003 ? 0x1 : 0;
\r
210 *fb |= (chars->BGCol & 0x888) == 0x888 ? 0x80 : 0;
\r
211 *fb |= (chars->BGCol & 0x700) > 0x300 ? 0x40 : 0;
\r
212 *fb |= (chars->BGCol & 0x070) > 0x030 ? 0x20 : 0;
\r
213 *fb |= (chars->BGCol & 0x007) > 0x003 ? 0x10 : 0;
\r
214 //LOG("%08x (%03x,%03x) = %04x",
\r
215 // chars->Ch, chars->BGCol, chars->FGCol, *fb);
\r
217 Length *= sizeof(tVT_Char);
\r
218 LEAVE('X', Length);
\r
222 if( gVesa_Modes[giVesaCurrentMode].framebuffer == 0 ) {
\r
223 Log_Warning("VESA", "Vesa_Write - Non-LFB Modes not yet supported.");
\r
229 switch( giVesaCurrentFormat )
\r
231 case VIDEO_BUFFMT_TEXT:
\r
233 tVT_Char *chars = Buffer;
\r
234 int pitch = gVesa_Modes[giVesaCurrentMode].width;
\r
235 int widthInChars = gVesa_Modes[giVesaCurrentMode].width/giVT_CharWidth;
\r
236 int heightInChars = gVesa_Modes[giVesaCurrentMode].height/giVT_CharHeight;
\r
238 Uint32 *dest = (void*)gpVesa_Framebuffer;
\r
241 Length /= sizeof(tVT_Char);
\r
242 Offset /= sizeof(tVT_Char);
\r
244 LOG("gVesa_Modes[%i].width = %i", giVesaCurrentMode, gVesa_Modes[giVesaCurrentMode].width);
\r
245 x = Offset % widthInChars;
\r
246 y = Offset / widthInChars;
\r
247 LOG("(x,y) = (%i,%i) = [%i,%i]", x, y, x * giVT_CharWidth, y * giVT_CharHeight * pitch);
\r
248 LOG("(w,h) = (%i,%i) = [%i,%i]",
\r
249 (int)(Length % widthInChars),
\r
250 (int)(Length / widthInChars),
\r
251 (int)((Length % widthInChars) * giVT_CharWidth),
\r
252 (int)((Length / widthInChars) * giVT_CharHeight * pitch)
\r
256 if( Offset > (Uint64)(heightInChars*widthInChars) ) {
\r
260 if(y >= heightInChars) {
\r
266 if( (int)Offset + (int)Length > heightInChars*widthInChars ) {
\r
267 Log_Debug("VESA", "%i + %i > %i*%i (%i)",
\r
268 (int)Offset, (int)Length, heightInChars, widthInChars, heightInChars*widthInChars);
\r
269 Length = heightInChars*widthInChars - Offset;
\r
270 Log_Notice("VESA", "Clipping write size to %i characters", (int)Length);
\r
273 dest += y * giVT_CharHeight * pitch;
\r
274 dest += x * giVT_CharWidth;
\r
276 LOG("dest = %p", dest);
\r
278 for( i = 0; i < (int)Length; i++ )
\r
282 dest + x*giVT_CharWidth, pitch,
\r
283 VT_Colour12to24(chars->BGCol),
\r
284 VT_Colour12to24(chars->FGCol)
\r
289 if( x >= widthInChars ) {
\r
292 dest += pitch*giVT_CharHeight;
\r
295 Length *= sizeof(tVT_Char);
\r
299 case VIDEO_BUFFMT_FRAMEBUFFER:
\r
301 Uint8 *destBuf = (Uint8*) ((Uint)gpVesa_Framebuffer + (Uint)Offset);
\r
303 if(gVesa_Modes[giVesaCurrentMode].fbSize < Offset+Length)
\r
305 Log_Warning("VESA", "Vesa_Write - Framebuffer Overflow");
\r
310 LOG("buffer = %p", Buffer);
\r
311 LOG("Updating Framebuffer (%p to %p)", destBuf, destBuf + (Uint)Length);
\r
314 // Copy to Frambuffer
\r
315 memcpy(destBuf, Buffer, Length);
\r
317 LOG("BGA Framebuffer updated");
\r
321 case VIDEO_BUFFMT_2DSTREAM:
\r
322 Length = DrvUtil_Video_2DStream(
\r
323 NULL, // Single framebuffer, so Ent is unused
\r
324 Buffer, Length, &gVesa_2DFunctions, sizeof(gVesa_2DFunctions)
\r
333 LEAVE('X', Length);
\r
338 * \fn int Vesa_Ioctl(vfs_node *node, int id, void *data)
\r
339 * \brief Handle messages to the device
\r
341 int Vesa_Ioctl(tVFS_Node *Node, int ID, void *Data)
\r
344 //Log_Debug("VESA", "Vesa_Ioctl: (Node=%p, ID=%i, Data=%p)", Node, ID, Data);
\r
347 case DRV_IOCTL_TYPE: return DRV_TYPE_VIDEO;
\r
348 case DRV_IOCTL_IDENT: memcpy("VESA", Data, 4); return 1;
\r
349 case DRV_IOCTL_VERSION: return VERSION;
\r
351 case VIDEO_IOCTL_GETSETMODE:
\r
352 if( !Data ) return giVesaCurrentMode;
\r
353 return Vesa_Int_SetMode( *(int*)Data );
\r
355 case VIDEO_IOCTL_FINDMODE:
\r
356 return Vesa_Int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
357 case VIDEO_IOCTL_MODEINFO:
\r
358 return Vesa_Int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
360 case VIDEO_IOCTL_SETBUFFORMAT:
\r
361 ret = giVesaCurrentFormat;
\r
363 //Log_Log("VESA", "Buffer mode to %i", *(int*)Data);
\r
364 giVesaCurrentFormat = *(int*)Data;
\r
368 case VIDEO_IOCTL_SETCURSOR: // Set cursor position
\r
369 #if !BLINKING_CURSOR
\r
370 if(giVesaCursorX > 0)
\r
371 Vesa_FlipCursor(Node);
\r
373 giVesaCursorX = ((tVideo_IOCtl_Pos*)Data)->x;
\r
374 giVesaCursorY = ((tVideo_IOCtl_Pos*)Data)->y;
\r
375 //Log_Debug("VESA", "Cursor position (%i,%i)", giVesaCursorX, giVesaCursorY);
\r
377 giVesaCursorX < 0 || giVesaCursorY < 0
\r
378 || giVesaCursorX >= gpVesaCurMode->width/giVT_CharWidth
\r
379 || giVesaCursorY >= gpVesaCurMode->height/giVT_CharHeight)
\r
381 #if BLINKING_CURSOR
\r
382 if(giVesaCursorTimer != -1) {
\r
383 Time_RemoveTimer(giVesaCursorTimer);
\r
384 giVesaCursorTimer = -1;
\r
387 giVesaCursorX = -1;
\r
388 giVesaCursorY = -1;
\r
391 #if BLINKING_CURSOR
\r
392 // Log_Debug("VESA", "Updating timer %i?", giVesaCursorTimer);
\r
393 if(giVesaCursorTimer == -1)
\r
394 giVesaCursorTimer = Time_CreateTimer(VESA_CURSOR_PERIOD, Vesa_FlipCursor, Node);
\r
396 Vesa_FlipCursor(Node);
\r
399 //Log_Debug("VESA", "Cursor position (%i,%i) Timer %i", giVesaCursorX, giVesaCursorY, giVesaCursorTimer);
\r
402 case VIDEO_IOCTL_REQLFB: // Request Linear Framebuffer
\r
409 * \brief Updates the video mode
\r
411 int Vesa_Int_SetMode(int mode)
\r
413 Log_Log("VESA", "Setting mode to %i", mode);
\r
415 // Sanity Check values
\r
416 if(mode < 0 || mode > giVesaModeCount) return -1;
\r
418 // Check for fast return
\r
419 if(mode == giVesaCurrentMode) return 1;
\r
421 Time_RemoveTimer(giVesaCursorTimer);
\r
422 giVesaCursorTimer = -1;
\r
424 LOCK( &glVesa_Lock );
\r
426 gpVesa_BiosState->AX = 0x4F02;
\r
427 gpVesa_BiosState->BX = gVesa_Modes[mode].code;
\r
428 if(gVesa_Modes[mode].flags & FLAG_LFB) {
\r
429 Log_Log("VESA", "Using LFB");
\r
430 gpVesa_BiosState->BX |= 0x4000; // Bit 14 - Use LFB
\r
434 VM8086_Int(gpVesa_BiosState, 0x10);
\r
437 if( (tVAddr)gpVesa_Framebuffer != VESA_DEFAULT_FRAMEBUFFER )
\r
438 MM_UnmapHWPages((tVAddr)gpVesa_Framebuffer, giVesaPageCount);
\r
439 giVesaPageCount = (gVesa_Modes[mode].fbSize + 0xFFF) >> 12;
\r
440 gpVesa_Framebuffer = (void*)MM_MapHWPages(gVesa_Modes[mode].framebuffer, giVesaPageCount);
\r
442 Log_Log("VESA", "Framebuffer (Phys) = 0x%x", gVesa_Modes[mode].framebuffer);
\r
443 Log_Log("VESA", "Framebuffer (Virt) = 0x%x", gpVesa_Framebuffer);
\r
446 giVesaCurrentMode = mode;
\r
447 gpVesaCurMode = &gVesa_Modes[giVesaCurrentMode];
\r
449 RELEASE( &glVesa_Lock );
\r
454 int Vesa_Int_FindMode(tVideo_IOCtl_Mode *data)
\r
457 int best = -1, bestFactor = 1000;
\r
460 ENTER("idata->width idata->height idata->bpp", data->width, data->height, data->bpp);
\r
462 for(i=0;i<giVesaModeCount;i++)
\r
464 LOG("Mode %i (%ix%ix%i)", i, gVesa_Modes[i].width, gVesa_Modes[i].height, gVesa_Modes[i].bpp);
\r
466 if(gVesa_Modes[i].width == data->width
\r
467 && gVesa_Modes[i].height == data->height
\r
468 && gVesa_Modes[i].bpp == data->bpp)
\r
475 tmp = gVesa_Modes[i].width * gVesa_Modes[i].height * gVesa_Modes[i].bpp;
\r
476 tmp -= data->width * data->height * data->bpp;
\r
477 tmp = tmp < 0 ? -tmp : tmp;
\r
478 factor = tmp * 100 / (data->width * data->height * data->bpp);
\r
480 LOG("factor = %i", factor);
\r
482 if(factor < bestFactor)
\r
484 bestFactor = factor;
\r
489 data->width = gVesa_Modes[best].width;
\r
490 data->height = gVesa_Modes[best].height;
\r
491 data->bpp = gVesa_Modes[best].bpp;
\r
496 int Vesa_Int_ModeInfo(tVideo_IOCtl_Mode *data)
\r
498 if(data->id < 0 || data->id > giVesaModeCount) return -1;
\r
499 data->width = gVesa_Modes[data->id].width;
\r
500 data->height = gVesa_Modes[data->id].height;
\r
501 data->bpp = gVesa_Modes[data->id].bpp;
\r
506 * \brief Updates the state of the text cursor
\r
507 * \note Just does a bitwise not on the cursor region
\r
509 void Vesa_FlipCursor(void *Arg)
\r
511 int pitch = gpVesaCurMode->pitch/4;
\r
512 int x = giVesaCursorX*giVT_CharWidth;
\r
513 int y = giVesaCursorY*giVT_CharHeight;
\r
515 Uint32 *fb = (void*)gpVesa_Framebuffer;
\r
517 //Debug("Cursor flip");
\r
520 if(giVesaCursorX < 0 || giVesaCursorY < 0
\r
521 || y*pitch + x + giVT_CharHeight*pitch > (int)gpVesaCurMode->fbSize/4) {
\r
522 Debug("Cursor OOB (%i,%i)", x, y);
\r
523 giVesaCursorTimer = -1;
\r
528 fb += (y+1)*pitch + x;
\r
529 for( i = 1; i < giVT_CharHeight-1; i++, fb += pitch )
\r
532 #if BLINKING_CURSOR
\r
533 giVesaCursorTimer = Time_CreateTimer(VESA_CURSOR_PERIOD, Vesa_FlipCursor, Arg);
\r
537 // ------------------------
\r
538 // --- 2D Accelleration ---
\r
539 // ------------------------
\r
540 void Vesa_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
\r
542 int pitch = gpVesaCurMode->pitch/4;
\r
543 Uint32 *buf = (Uint32*)gpVesa_Framebuffer + Y*pitch + X;
\r
545 memsetd(buf, Colour, W);
\r
550 void Vesa_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
\r
552 int scrnpitch = gVesa_Modes[giVesaCurrentMode].pitch;
\r
553 int dst = DstY*scrnpitch + DstX;
\r
554 int src = SrcY*scrnpitch + SrcX;
\r
557 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
\r
558 // Ent, DstX, DstY, SrcX, SrcY, W, H);
\r
560 if(SrcX + W > gVesa_Modes[giVesaCurrentMode].width)
\r
561 W = gVesa_Modes[giVesaCurrentMode].width - SrcX;
\r
562 if(DstX + W > gVesa_Modes[giVesaCurrentMode].width)
\r
563 W = gVesa_Modes[giVesaCurrentMode].width - DstX;
\r
564 if(SrcY + H > gVesa_Modes[giVesaCurrentMode].height)
\r
565 H = gVesa_Modes[giVesaCurrentMode].height - SrcY;
\r
566 if(DstY + H > gVesa_Modes[giVesaCurrentMode].height)
\r
567 H = gVesa_Modes[giVesaCurrentMode].height - DstY;
\r
569 //Debug("W = %i, H = %i", W, H);
\r
573 dst += H*scrnpitch;
\r
574 src += H*scrnpitch;
\r
579 for( tmp = W; tmp --; ) {
\r
580 *(Uint32*)(gpVesa_Framebuffer + dst + tmp) = *(Uint32*)(gpVesa_Framebuffer + src + tmp);
\r
585 // Normal copy is OK
\r
587 memcpy((void*)gpVesa_Framebuffer + dst, (void*)gpVesa_Framebuffer + src, W*sizeof(Uint32));
\r