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 size_t BGA_Read(tVFS_Node *Node, off_t off, size_t len, void *buffer);
\r
67 size_t BGA_Write(tVFS_Node *Node, off_t off, size_t 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
106 if( version == 0xFFFF ) {
\r
107 // Floating bus, nothing there
\r
108 return MODULE_ERR_NOTNEEDED;
\r
111 // NOTE: This driver was written for BGA versions >= 0xBOC2
\r
112 // NOTE: However, Qemu is braindead and doesn't return the actual version
\r
113 if( version != 0xB0C0 && ((version & 0xFFF0) != 0xB0C0 || version < 0xB0C2) ) {
\r
114 Log_Warning("BGA", "Bochs Adapter Version is not compatible (need >= 0xB0C2), instead 0x%x", version);
\r
115 return MODULE_ERR_NOTNEEDED;
\r
118 // Get framebuffer base
\r
119 dev = PCI_GetDevice(0x1234, 0x1111, 0);
\r
121 base = VBE_DISPI_LFB_PHYSICAL_ADDRESS;
\r
123 base = PCI_GetBAR(dev, 0);
\r
125 // Map Framebuffer to hardware address
\r
126 gBGA_Framebuffer = (void *) MM_MapHWPages(base, 768); // 768 pages (3Mb)
\r
129 if( DevFS_AddDevice( &gBGA_DriverStruct ) == -1 )
\r
131 Log_Warning("BGA", "Unable to register with DevFS, maybe already loaded?");
\r
132 return MODULE_ERR_MISC;
\r
135 return MODULE_ERR_OK;
\r
139 * \brief Clean up driver resources before destruction
\r
141 void BGA_Uninstall(void)
\r
143 DevFS_DelDevice( &gBGA_DriverStruct );
\r
144 MM_UnmapHWPages( (tVAddr)gBGA_Framebuffer, 768 );
\r
148 * \brief Read from the framebuffer
\r
150 size_t BGA_Read(tVFS_Node *node, off_t off, size_t len, void *buffer)
\r
153 if(giBGA_CurrentMode == -1) return -1;
\r
155 // Check Offset and Length against Framebuffer Size
\r
156 if(off+len > gpBGA_CurrentMode->fbSize)
\r
159 // Copy from Framebuffer
\r
160 memcpy(buffer, (void*)((Uint)gBGA_Framebuffer + (Uint)off), len);
\r
165 * \brief Write to the framebuffer
\r
167 size_t BGA_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
\r
169 if( giBGA_CurrentMode == -1 ) BGA_int_UpdateMode(0);
\r
170 return DrvUtil_Video_WriteLFB(&gBGA_DrvUtil_BufInfo, Offset, Length, Buffer);
\r
173 const char *csaBGA_IOCtls[] = {DRV_IOCTLNAMES, DRV_VIDEO_IOCTLNAMES, NULL};
\r
175 * \brief Handle messages to the device
\r
177 int BGA_IOCtl(tVFS_Node *Node, int ID, void *Data)
\r
180 ENTER("pNode iId pData", Node, ID, Data);
\r
184 BASE_IOCTLS(DRV_TYPE_VIDEO, "BochsGA", VERSION, csaBGA_IOCtls);
\r
186 case VIDEO_IOCTL_GETSETMODE:
\r
187 if( Data ) BGA_int_UpdateMode(*(int*)(Data));
\r
188 ret = giBGA_CurrentMode;
\r
191 case VIDEO_IOCTL_FINDMODE:
\r
192 ret = BGA_int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
195 case VIDEO_IOCTL_MODEINFO:
\r
196 ret = BGA_int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
199 case VIDEO_IOCTL_SETBUFFORMAT:
\r
200 DrvUtil_Video_RemoveCursor( &gBGA_DrvUtil_BufInfo );
\r
201 ret = gBGA_DrvUtil_BufInfo.BufferFormat;
\r
203 gBGA_DrvUtil_BufInfo.BufferFormat = *(int*)Data;
\r
204 if(gBGA_DrvUtil_BufInfo.BufferFormat == VIDEO_BUFFMT_TEXT)
\r
205 DrvUtil_Video_SetCursor( &gBGA_DrvUtil_BufInfo, &gDrvUtil_TextModeCursor );
\r
208 case VIDEO_IOCTL_SETCURSOR:
\r
209 DrvUtil_Video_RemoveCursor( &gBGA_DrvUtil_BufInfo );
\r
210 gBGA_CursorPos.x = ((tVideo_IOCtl_Pos*)Data)->x;
\r
211 gBGA_CursorPos.y = ((tVideo_IOCtl_Pos*)Data)->y;
\r
212 if(gBGA_DrvUtil_BufInfo.BufferFormat == VIDEO_BUFFMT_TEXT)
\r
213 DrvUtil_Video_DrawCursor(
\r
214 &gBGA_DrvUtil_BufInfo,
\r
215 gBGA_CursorPos.x*giVT_CharWidth,
\r
216 gBGA_CursorPos.y*giVT_CharHeight
\r
219 DrvUtil_Video_DrawCursor(
\r
220 &gBGA_DrvUtil_BufInfo,
\r
221 gBGA_CursorPos.x, gBGA_CursorPos.y
\r
234 //== Internal Functions ==
\r
236 * \brief Writes to a BGA register
\r
238 void BGA_int_WriteRegister(Uint16 reg, Uint16 value)
\r
240 outw(VBE_DISPI_IOPORT_INDEX, reg);
\r
241 outw(VBE_DISPI_IOPORT_DATA, value);
\r
244 Uint16 BGA_int_ReadRegister(Uint16 reg)
\r
246 outw(VBE_DISPI_IOPORT_INDEX, reg);
\r
247 return inw(VBE_DISPI_IOPORT_DATA);
\r
251 * \brief Sets the video mode (32bpp only)
\r
253 void BGA_int_SetMode(Uint16 Width, Uint16 Height)
\r
255 ENTER("iWidth iHeight", Width, Height);
\r
256 BGA_int_WriteRegister(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED);
\r
257 BGA_int_WriteRegister(VBE_DISPI_INDEX_XRES, Width);
\r
258 BGA_int_WriteRegister(VBE_DISPI_INDEX_YRES, Height);
\r
259 BGA_int_WriteRegister(VBE_DISPI_INDEX_BPP, 32);
\r
260 BGA_int_WriteRegister(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_NOCLEARMEM | VBE_DISPI_LFB_ENABLED);
\r
265 * \fn int BGA_int_UpdateMode(int id)
\r
266 * \brief Set current vide mode given a mode id
\r
268 int BGA_int_UpdateMode(int id)
\r
271 if(id < 0 || id >= BGA_MODE_COUNT) return -1;
\r
274 gBGA_Modes[id].width,
\r
275 gBGA_Modes[id].height);
\r
277 gBGA_DrvUtil_BufInfo.Framebuffer = gBGA_Framebuffer;
\r
278 gBGA_DrvUtil_BufInfo.Pitch = gBGA_Modes[id].width * 4;
\r
279 gBGA_DrvUtil_BufInfo.Width = gBGA_Modes[id].width;
\r
280 gBGA_DrvUtil_BufInfo.Height = gBGA_Modes[id].height;
\r
281 gBGA_DrvUtil_BufInfo.Depth = gBGA_Modes[id].bpp;
\r
283 giBGA_CurrentMode = id;
\r
284 gpBGA_CurrentMode = &gBGA_Modes[id];
\r
289 * \fn int BGA_int_FindMode(tVideo_IOCtl_Mode *info)
\r
290 * \brief Find a mode matching the given options
\r
292 int BGA_int_FindMode(tVideo_IOCtl_Mode *info)
\r
295 int best = 0, bestFactor = 1000;
\r
297 int rqdProduct = info->width * info->height;
\r
299 ENTER("pinfo", info);
\r
300 LOG("info = {width:%i,height:%i,bpp:%i})\n", info->width, info->height, info->bpp);
\r
302 for(i = 0; i < BGA_MODE_COUNT; i++)
\r
305 LOG("Mode %i (%ix%i,%ibpp), ", i, gBGA_Modes[i].width, gBGA_Modes[i].height, gBGA_Modes[i].bpp);
\r
308 if( gBGA_Modes[i].bpp != info->bpp )
\r
311 // Ooh! A perfect match
\r
312 if(gBGA_Modes[i].width == info->width && gBGA_Modes[i].height == info->height)
\r
322 // If not, how close are we?
\r
323 tmp = gBGA_Modes[i].width * gBGA_Modes[i].height - rqdProduct;
\r
324 tmp = tmp < 0 ? -tmp : tmp; // tmp = ABS(tmp)
\r
327 LOG("tmp = %i", tmp);
\r
330 if(tmp < bestFactor)
\r
338 info->width = gBGA_Modes[best].width;
\r
339 info->height = gBGA_Modes[best].height;
\r
340 info->bpp = gBGA_Modes[best].bpp;
\r
347 * \fn int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info)
\r
348 * \brief Get mode information
\r
350 int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info)
\r
353 //if( !MM_IsUser( (Uint)info, sizeof(tVideo_IOCtl_Mode) ) ) {
\r
357 if(info->id < 0 || info->id >= BGA_MODE_COUNT) return -1;
\r
359 info->width = gBGA_Modes[info->id].width;
\r
360 info->height = gBGA_Modes[info->id].height;
\r
361 info->bpp = gBGA_Modes[info->id].bpp;
\r