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
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 * \brief Read from the framebuffer
\r
146 size_t BGA_Read(tVFS_Node *node, off_t off, size_t len, void *buffer)
\r
149 if(giBGA_CurrentMode == -1) return -1;
\r
151 // Check Offset and Length against Framebuffer Size
\r
152 if(off+len > gpBGA_CurrentMode->fbSize)
\r
155 // Copy from Framebuffer
\r
156 memcpy(buffer, (void*)((Uint)gBGA_Framebuffer + (Uint)off), len);
\r
161 * \brief Write to the framebuffer
\r
163 size_t BGA_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
\r
165 if( giBGA_CurrentMode == -1 ) BGA_int_UpdateMode(0);
\r
166 return DrvUtil_Video_WriteLFB(&gBGA_DrvUtil_BufInfo, Offset, Length, Buffer);
\r
169 const char *csaBGA_IOCtls[] = {DRV_IOCTLNAMES, DRV_VIDEO_IOCTLNAMES, NULL};
\r
171 * \brief Handle messages to the device
\r
173 int BGA_IOCtl(tVFS_Node *Node, int ID, void *Data)
\r
176 ENTER("pNode iId pData", Node, ID, Data);
\r
180 BASE_IOCTLS(DRV_TYPE_VIDEO, "BochsGA", VERSION, csaBGA_IOCtls);
\r
182 case VIDEO_IOCTL_GETSETMODE:
\r
183 if( Data ) BGA_int_UpdateMode(*(int*)(Data));
\r
184 ret = giBGA_CurrentMode;
\r
187 case VIDEO_IOCTL_FINDMODE:
\r
188 ret = BGA_int_FindMode((tVideo_IOCtl_Mode*)Data);
\r
191 case VIDEO_IOCTL_MODEINFO:
\r
192 ret = BGA_int_ModeInfo((tVideo_IOCtl_Mode*)Data);
\r
195 case VIDEO_IOCTL_SETBUFFORMAT:
\r
196 DrvUtil_Video_RemoveCursor( &gBGA_DrvUtil_BufInfo );
\r
197 ret = gBGA_DrvUtil_BufInfo.BufferFormat;
\r
199 gBGA_DrvUtil_BufInfo.BufferFormat = *(int*)Data;
\r
200 if(gBGA_DrvUtil_BufInfo.BufferFormat == VIDEO_BUFFMT_TEXT)
\r
201 DrvUtil_Video_SetCursor( &gBGA_DrvUtil_BufInfo, &gDrvUtil_TextModeCursor );
\r
204 case VIDEO_IOCTL_SETCURSOR:
\r
205 DrvUtil_Video_RemoveCursor( &gBGA_DrvUtil_BufInfo );
\r
206 gBGA_CursorPos.x = ((tVideo_IOCtl_Pos*)Data)->x;
\r
207 gBGA_CursorPos.y = ((tVideo_IOCtl_Pos*)Data)->y;
\r
208 if(gBGA_DrvUtil_BufInfo.BufferFormat == VIDEO_BUFFMT_TEXT)
\r
209 DrvUtil_Video_DrawCursor(
\r
210 &gBGA_DrvUtil_BufInfo,
\r
211 gBGA_CursorPos.x*giVT_CharWidth,
\r
212 gBGA_CursorPos.y*giVT_CharHeight
\r
215 DrvUtil_Video_DrawCursor(
\r
216 &gBGA_DrvUtil_BufInfo,
\r
217 gBGA_CursorPos.x, gBGA_CursorPos.y
\r
230 //== Internal Functions ==
\r
232 * \brief Writes to a BGA register
\r
234 void BGA_int_WriteRegister(Uint16 reg, Uint16 value)
\r
236 outw(VBE_DISPI_IOPORT_INDEX, reg);
\r
237 outw(VBE_DISPI_IOPORT_DATA, value);
\r
240 Uint16 BGA_int_ReadRegister(Uint16 reg)
\r
242 outw(VBE_DISPI_IOPORT_INDEX, reg);
\r
243 return inw(VBE_DISPI_IOPORT_DATA);
\r
247 * \brief Sets the video mode (32bpp only)
\r
249 void BGA_int_SetMode(Uint16 Width, Uint16 Height)
\r
251 ENTER("iWidth iHeight", Width, Height);
\r
252 BGA_int_WriteRegister(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED);
\r
253 BGA_int_WriteRegister(VBE_DISPI_INDEX_XRES, Width);
\r
254 BGA_int_WriteRegister(VBE_DISPI_INDEX_YRES, Height);
\r
255 BGA_int_WriteRegister(VBE_DISPI_INDEX_BPP, 32);
\r
256 BGA_int_WriteRegister(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_NOCLEARMEM | VBE_DISPI_LFB_ENABLED);
\r
261 * \fn int BGA_int_UpdateMode(int id)
\r
262 * \brief Set current vide mode given a mode id
\r
264 int BGA_int_UpdateMode(int id)
\r
267 if(id < 0 || id >= BGA_MODE_COUNT) return -1;
\r
270 gBGA_Modes[id].width,
\r
271 gBGA_Modes[id].height);
\r
273 gBGA_DrvUtil_BufInfo.Framebuffer = gBGA_Framebuffer;
\r
274 gBGA_DrvUtil_BufInfo.Pitch = gBGA_Modes[id].width * 4;
\r
275 gBGA_DrvUtil_BufInfo.Width = gBGA_Modes[id].width;
\r
276 gBGA_DrvUtil_BufInfo.Height = gBGA_Modes[id].height;
\r
277 gBGA_DrvUtil_BufInfo.Depth = gBGA_Modes[id].bpp;
\r
279 giBGA_CurrentMode = id;
\r
280 gpBGA_CurrentMode = &gBGA_Modes[id];
\r
285 * \fn int BGA_int_FindMode(tVideo_IOCtl_Mode *info)
\r
286 * \brief Find a mode matching the given options
\r
288 int BGA_int_FindMode(tVideo_IOCtl_Mode *info)
\r
291 int best = 0, bestFactor = 1000;
\r
293 int rqdProduct = info->width * info->height;
\r
295 ENTER("pinfo", info);
\r
296 LOG("info = {width:%i,height:%i,bpp:%i})\n", info->width, info->height, info->bpp);
\r
298 for(i = 0; i < BGA_MODE_COUNT; i++)
\r
301 LOG("Mode %i (%ix%i,%ibpp), ", i, gBGA_Modes[i].width, gBGA_Modes[i].height, gBGA_Modes[i].bpp);
\r
304 if( gBGA_Modes[i].bpp != info->bpp )
\r
307 // Ooh! A perfect match
\r
308 if(gBGA_Modes[i].width == info->width && gBGA_Modes[i].height == info->height)
\r
318 // If not, how close are we?
\r
319 tmp = gBGA_Modes[i].width * gBGA_Modes[i].height - rqdProduct;
\r
320 tmp = tmp < 0 ? -tmp : tmp; // tmp = ABS(tmp)
\r
323 LOG("tmp = %i", tmp);
\r
326 if(tmp < bestFactor)
\r
334 info->width = gBGA_Modes[best].width;
\r
335 info->height = gBGA_Modes[best].height;
\r
336 info->bpp = gBGA_Modes[best].bpp;
\r
343 * \fn int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info)
\r
344 * \brief Get mode information
\r
346 int BGA_int_ModeInfo(tVideo_IOCtl_Mode *info)
\r
349 //if( !MM_IsUser( (Uint)info, sizeof(tVideo_IOCtl_Mode) ) ) {
\r
353 if(info->id < 0 || info->id >= BGA_MODE_COUNT) return -1;
\r
355 info->width = gBGA_Modes[info->id].width;
\r
356 info->height = gBGA_Modes[info->id].height;
\r
357 info->bpp = gBGA_Modes[info->id].bpp;
\r