2 * Acess2 Bochs graphics adapter Driver
\r
3 * - By John Hodge (thePowersGang)
\r
9 #define VERSION VER2(0,10)
\r
13 #include <modules.h>
\r
15 #include <fs_devfs.h>
\r
16 #include <drv_pci.h>
\r
17 #include <api_drv_video.h>
\r
20 typedef struct sBGA_Mode {
\r
27 // === CONSTANTS ===
\r
28 #define BGA_LFB_MAXSIZE (1024*768*4)
\r
29 #define VBE_DISPI_BANK_ADDRESS 0xA0000
\r
30 #define VBE_DISPI_LFB_PHYSICAL_ADDRESS 0xE0000000
\r
31 #define VBE_DISPI_IOPORT_INDEX 0x01CE
\r
32 #define VBE_DISPI_IOPORT_DATA 0x01CF
\r
33 #define VBE_DISPI_DISABLED 0x00
\r
34 #define VBE_DISPI_ENABLED 0x01
\r
35 #define VBE_DISPI_LFB_ENABLED 0x40
\r
36 #define VBE_DISPI_NOCLEARMEM 0x80
\r
39 VBE_DISPI_INDEX_XRES,
\r
40 VBE_DISPI_INDEX_YRES,
\r
41 VBE_DISPI_INDEX_BPP,
\r
42 VBE_DISPI_INDEX_ENABLE,
\r
43 VBE_DISPI_INDEX_BANK,
\r
44 VBE_DISPI_INDEX_VIRT_WIDTH,
\r
45 VBE_DISPI_INDEX_VIRT_HEIGHT,
\r
46 VBE_DISPI_INDEX_X_OFFSET,
\r
47 VBE_DISPI_INDEX_Y_OFFSET
\r
50 extern void MM_DumpTables(tVAddr Start, tVAddr End);
\r
52 // === PROTOTYPES ===
\r
54 int BGA_Install(char **Arguments);
\r
55 void BGA_Uninstall();
\r
57 void BGA_int_WriteRegister(Uint16 reg, Uint16 value);
\r
58 Uint16 BGA_int_ReadRegister(Uint16 reg);
\r
59 void BGA_int_SetBank(Uint16 bank);
\r
60 void BGA_int_SetMode(Uint16 width, Uint16 height);
\r
61 int BGA_int_UpdateMode(int id);
\r
62 int BGA_int_FindMode(tVideo_IOCtl_Mode *info);
\r
63 int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info);
\r
64 int BGA_int_MapFB(void *Dest);
\r
66 Uint64 BGA_Read(tVFS_Node *Node, Uint64 off, Uint64 len, void *buffer);
\r
67 Uint64 BGA_Write(tVFS_Node *Node, Uint64 off, Uint64 len, const void *buffer);
\r
68 int BGA_IOCtl(tVFS_Node *Node, int ID, void *Data);
\r
71 MODULE_DEFINE(0, VERSION, BochsGA, BGA_Install, NULL, "PCI", NULL);
\r
72 tVFS_NodeType gBGA_NodeType = {
\r
77 tDevFS_Driver gBGA_DriverStruct = {
\r
79 {.Type = &gBGA_NodeType}
\r
81 int giBGA_CurrentMode = -1;
\r
82 tVideo_IOCtl_Pos gBGA_CursorPos = {-1,-1};
\r
83 Uint *gBGA_Framebuffer;
\r
84 const tBGA_Mode *gpBGA_CurrentMode;
\r
85 const tBGA_Mode gBGA_Modes[] = {
\r
86 {640,480,32, 640*480*4},
\r
87 {800,600,32, 800*600*4},
\r
88 {1024,768,32, 1024*768*4}
\r
90 #define BGA_MODE_COUNT (sizeof(gBGA_Modes)/sizeof(gBGA_Modes[0]))
\r
91 tDrvUtil_Video_BufInfo gBGA_DrvUtil_BufInfo;
\r
95 * \fn int BGA_Install(char **Arguments)
\r
97 int BGA_Install(char **Arguments)
\r
103 // Check BGA Version
\r
104 version = BGA_int_ReadRegister(VBE_DISPI_INDEX_ID);
\r
105 LOG("version = 0x%x", version);
\r
107 // NOTE: This driver was written for BGA versions >= 0xBOC2
\r
108 // NOTE: However, Qemu is braindead and doesn't return the actual version
\r
109 if( version != 0xB0C0 && ((version & 0xFFF0) != 0xB0C0 || version < 0xB0C2) ) {
\r
110 Log_Warning("BGA", "Bochs Adapter Version is not compatible (need >= 0xB0C2), instead 0x%x", version);
\r
111 return MODULE_ERR_NOTNEEDED;
\r
114 // Get framebuffer base
\r
115 dev = PCI_GetDevice(0x1234, 0x1111, 0);
\r
117 base = VBE_DISPI_LFB_PHYSICAL_ADDRESS;
\r
119 base = PCI_GetBAR(dev, 0);
\r
121 // Map Framebuffer to hardware address
\r
122 gBGA_Framebuffer = (void *) MM_MapHWPages(base, 768); // 768 pages (3Mb)
\r
125 if( DevFS_AddDevice( &gBGA_DriverStruct ) == -1 )
\r
127 Log_Warning("BGA", "Unable to register with DevFS, maybe already loaded?");
\r
128 return MODULE_ERR_MISC;
\r
131 return MODULE_ERR_OK;
\r
135 * \brief Clean up driver resources before destruction
\r
137 void BGA_Uninstall(void)
\r
139 DevFS_DelDevice( &gBGA_DriverStruct );
\r
140 MM_UnmapHWPages( (tVAddr)gBGA_Framebuffer, 768 );
\r
144 * \fn Uint64 BGA_Read(tVFS_Node *node, Uint64 off, Uint64 len, void *buffer)
\r
145 * \brief Read from the framebuffer
\r
147 Uint64 BGA_Read(tVFS_Node *node, Uint64 off, Uint64 len, void *buffer)
\r
150 if(giBGA_CurrentMode == -1) return -1;
\r
152 // Check Offset and Length against Framebuffer Size
\r
153 if(off+len > gpBGA_CurrentMode->fbSize)
\r
156 // Copy from Framebuffer
\r
157 memcpy(buffer, (void*)((Uint)gBGA_Framebuffer + (Uint)off), len);
\r
162 * \brief Write to the framebuffer
\r
164 Uint64 BGA_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
\r
166 if( giBGA_CurrentMode == -1 ) BGA_int_UpdateMode(0);
\r
167 return DrvUtil_Video_WriteLFB(&gBGA_DrvUtil_BufInfo, Offset, Length, Buffer);
\r
170 const char *csaBGA_IOCtls[] = {DRV_IOCTLNAMES, DRV_VIDEO_IOCTLNAMES, NULL};
\r
172 * \brief Handle messages to the device
\r
174 int BGA_IOCtl(tVFS_Node *Node, int ID, void *Data)
\r
177 ENTER("pNode iId pData", Node, ID, Data);
\r
181 BASE_IOCTLS(DRV_TYPE_VIDEO, "BochsGA", VERSION, csaBGA_IOCtls);
\r
183 case VIDEO_IOCTL_GETSETMODE:
\r
184 if( Data ) BGA_int_UpdateMode(*(int*)(Data));
\r
185 ret = giBGA_CurrentMode;
\r
188 case VIDEO_IOCTL_FINDMODE:
\r
189 ret = BGA_int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
192 case VIDEO_IOCTL_MODEINFO:
\r
193 ret = BGA_int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
196 case VIDEO_IOCTL_SETBUFFORMAT:
\r
197 DrvUtil_Video_RemoveCursor( &gBGA_DrvUtil_BufInfo );
\r
198 ret = gBGA_DrvUtil_BufInfo.BufferFormat;
\r
200 gBGA_DrvUtil_BufInfo.BufferFormat = *(int*)Data;
\r
201 if(gBGA_DrvUtil_BufInfo.BufferFormat == VIDEO_BUFFMT_TEXT)
\r
202 DrvUtil_Video_SetCursor( &gBGA_DrvUtil_BufInfo, &gDrvUtil_TextModeCursor );
\r
205 case VIDEO_IOCTL_SETCURSOR:
\r
206 DrvUtil_Video_RemoveCursor( &gBGA_DrvUtil_BufInfo );
\r
207 gBGA_CursorPos.x = ((tVideo_IOCtl_Pos*)Data)->x;
\r
208 gBGA_CursorPos.y = ((tVideo_IOCtl_Pos*)Data)->y;
\r
209 if(gBGA_DrvUtil_BufInfo.BufferFormat == VIDEO_BUFFMT_TEXT)
\r
210 DrvUtil_Video_DrawCursor(
\r
211 &gBGA_DrvUtil_BufInfo,
\r
212 gBGA_CursorPos.x*giVT_CharWidth,
\r
213 gBGA_CursorPos.y*giVT_CharHeight
\r
216 DrvUtil_Video_DrawCursor(
\r
217 &gBGA_DrvUtil_BufInfo,
\r
218 gBGA_CursorPos.x, gBGA_CursorPos.y
\r
231 //== Internal Functions ==
\r
233 * \brief Writes to a BGA register
\r
235 void BGA_int_WriteRegister(Uint16 reg, Uint16 value)
\r
237 outw(VBE_DISPI_IOPORT_INDEX, reg);
\r
238 outw(VBE_DISPI_IOPORT_DATA, value);
\r
241 Uint16 BGA_int_ReadRegister(Uint16 reg)
\r
243 outw(VBE_DISPI_IOPORT_INDEX, reg);
\r
244 return inw(VBE_DISPI_IOPORT_DATA);
\r
248 * \brief Sets the video mode (32bpp only)
\r
250 void BGA_int_SetMode(Uint16 Width, Uint16 Height)
\r
252 ENTER("iWidth iHeight", Width, Height);
\r
253 BGA_int_WriteRegister(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED);
\r
254 BGA_int_WriteRegister(VBE_DISPI_INDEX_XRES, Width);
\r
255 BGA_int_WriteRegister(VBE_DISPI_INDEX_YRES, Height);
\r
256 BGA_int_WriteRegister(VBE_DISPI_INDEX_BPP, 32);
\r
257 BGA_int_WriteRegister(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_NOCLEARMEM | VBE_DISPI_LFB_ENABLED);
\r
262 * \fn int BGA_int_UpdateMode(int id)
\r
263 * \brief Set current vide mode given a mode id
\r
265 int BGA_int_UpdateMode(int id)
\r
268 if(id < 0 || id >= BGA_MODE_COUNT) return -1;
\r
271 gBGA_Modes[id].width,
\r
272 gBGA_Modes[id].height);
\r
274 gBGA_DrvUtil_BufInfo.Framebuffer = gBGA_Framebuffer;
\r
275 gBGA_DrvUtil_BufInfo.Pitch = gBGA_Modes[id].width * 4;
\r
276 gBGA_DrvUtil_BufInfo.Width = gBGA_Modes[id].width;
\r
277 gBGA_DrvUtil_BufInfo.Height = gBGA_Modes[id].height;
\r
278 gBGA_DrvUtil_BufInfo.Depth = gBGA_Modes[id].bpp;
\r
280 giBGA_CurrentMode = id;
\r
281 gpBGA_CurrentMode = &gBGA_Modes[id];
\r
286 * \fn int BGA_int_FindMode(tVideo_IOCtl_Mode *info)
\r
287 * \brief Find a mode matching the given options
\r
289 int BGA_int_FindMode(tVideo_IOCtl_Mode *info)
\r
292 int best = 0, bestFactor = 1000;
\r
294 int rqdProduct = info->width * info->height;
\r
296 ENTER("pinfo", info);
\r
297 LOG("info = {width:%i,height:%i,bpp:%i})\n", info->width, info->height, info->bpp);
\r
299 for(i = 0; i < BGA_MODE_COUNT; i++)
\r
302 LOG("Mode %i (%ix%i,%ibpp), ", i, gBGA_Modes[i].width, gBGA_Modes[i].height, gBGA_Modes[i].bpp);
\r
305 if( gBGA_Modes[i].bpp != info->bpp )
\r
308 // Ooh! A perfect match
\r
309 if(gBGA_Modes[i].width == info->width && gBGA_Modes[i].height == info->height)
\r
319 // If not, how close are we?
\r
320 tmp = gBGA_Modes[i].width * gBGA_Modes[i].height - rqdProduct;
\r
321 tmp = tmp < 0 ? -tmp : tmp; // tmp = ABS(tmp)
\r
324 LOG("tmp = %i", tmp);
\r
327 if(tmp < bestFactor)
\r
335 info->width = gBGA_Modes[best].width;
\r
336 info->height = gBGA_Modes[best].height;
\r
337 info->bpp = gBGA_Modes[best].bpp;
\r
344 * \fn int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info)
\r
345 * \brief Get mode information
\r
347 int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info)
\r
350 //if( !MM_IsUser( (Uint)info, sizeof(tVideo_IOCtl_Mode) ) ) {
\r
354 if(info->id < 0 || info->id >= BGA_MODE_COUNT) return -1;
\r
356 info->width = gBGA_Modes[info->id].width;
\r
357 info->height = gBGA_Modes[info->id].height;
\r
358 info->bpp = gBGA_Modes[info->id].bpp;
\r