comparison vidix/dhahelperwin/dhahelper.c @ 24541:173d35b7aca3

- make dhahelperwin compile with mingw - add dhahelper.rc based on code by Kevin Kofler and Romain Li¸«±vin <roms at lievin.net> from the tilp project http://svn.tilp.info/cgi-bin/viewcvs.cgi/libticables/trunk/src/win32/dha/
author faust3
date Mon, 17 Sep 2007 21:12:29 +0000
parents 32873ccfb007
children a1c020529cc0
comparison
equal deleted inserted replaced
24540:40a61e5ef8ca 24541:173d35b7aca3
1 /****************************************************************************** 1 /******************************************************************************
2 * dhahelper.c: direct hardware access under Windows NT/2000/XP 2 * dhahelper.c: direct hardware access under Windows NT/2000/XP
3 * Copyright (c) 2004 Sascha Sommer <saschasommer@freenet.de>. 3 * Copyright (c) 2004 Sascha Sommer <saschasommer@freenet.de>.
4 * Patched to compile with MinGW by Kevin Kofler:
5 * Copyright (c) 2007 Kevin Kofler
4 * 6 *
5 * This file is part of MPlayer. 7 * This file is part of MPlayer.
6 * 8 *
7 * MPlayer is free software; you can redistribute it and/or modify 9 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * 22 *
21 *****************************************************************************/ 23 *****************************************************************************/
22 24
23 25
26 #if defined(_MSC_VER)
24 #include <ntddk.h> 27 #include <ntddk.h>
28 #ifndef STDCALL
29 #define STDCALL /* nothing */
30 #endif
31 #elif defined(__MINGW32__)
32 #include <ddk/ntddk.h>
33 #define NO_SEH /* FIXME */
34 #else
35 #error Unsupported compiler. This driver requires MSVC+DDK or MinGW to build.
36 #endif
37
25 #include "dhahelper.h" 38 #include "dhahelper.h"
26 39
27 #define OutputDebugString DbgPrint 40 #define OutputDebugString DbgPrint
28 41
29 #define IOPM_SIZE 0x2000 42 #define IOPM_SIZE 0x2000
45 58
46 59
47 60
48 61
49 62
50 static NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp); 63 static STDCALL NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
51 static void dhahelperunload(IN PDRIVER_OBJECT DriverObject); 64 static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject);
52 static NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress); 65 static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress);
53 static NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin); 66 static STDCALL NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin);
54 67
55 void Ke386SetIoAccessMap(int, IOPM *); 68 void STDCALL Ke386SetIoAccessMap(int, IOPM *);
56 void Ke386QueryIoAccessMap(int, IOPM *); 69 void STDCALL Ke386QueryIoAccessMap(int, IOPM *);
57 void Ke386IoSetAccessProcess(PEPROCESS, int); 70 void STDCALL Ke386IoSetAccessProcess(PEPROCESS, int);
58 71
59 72
60 73
61 74
62 //entry point 75 //entry point
63 NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath){ 76 STDCALL NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath){
64 UNICODE_STRING DeviceNameUnicodeString; 77 UNICODE_STRING DeviceNameUnicodeString;
65 UNICODE_STRING DeviceLinkUnicodeString; 78 UNICODE_STRING DeviceLinkUnicodeString;
66 NTSTATUS ntStatus; 79 NTSTATUS ntStatus;
67 PDEVICE_OBJECT DeviceObject = NULL; 80 PDEVICE_OBJECT DeviceObject = NULL;
68 81
104 } 117 }
105 118
106 119
107 // Process the IRPs sent to this device 120 // Process the IRPs sent to this device
108 121
109 static NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp){ 122 static STDCALL NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp){
110 PIO_STACK_LOCATION IrpStack; 123 PIO_STACK_LOCATION IrpStack;
111 ULONG dwInputBufferLength; 124 ULONG dwInputBufferLength;
112 ULONG dwOutputBufferLength; 125 ULONG dwOutputBufferLength;
113 ULONG dwIoControlCode; 126 ULONG dwIoControlCode;
114 PVOID pvIOBuffer; 127 PVOID pvIOBuffer;
205 return ntStatus; 218 return ntStatus;
206 } 219 }
207 220
208 // Delete the associated device and return 221 // Delete the associated device and return
209 222
210 static void dhahelperunload(IN PDRIVER_OBJECT DriverObject){ 223 static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject){
211 UNICODE_STRING DeviceLinkUnicodeString; 224 UNICODE_STRING DeviceLinkUnicodeString;
212 NTSTATUS ntStatus=STATUS_SUCCESS; 225 NTSTATUS ntStatus=STATUS_SUCCESS;
213 OutputDebugString ("dhahelper: entering dhahelperunload"); 226 OutputDebugString ("dhahelper: entering dhahelperunload");
214 OutputDebugString ("dhahelper: unmapping remaining memory"); 227 OutputDebugString ("dhahelper: unmapping remaining memory");
215 228
238 //http://support.microsoft.com/default.aspx?scid=kb;en-us;q189327 251 //http://support.microsoft.com/default.aspx?scid=kb;en-us;q189327
239 //furthermore it keeps a list of mapped areas to free them when the driver gets unloaded 252 //furthermore it keeps a list of mapped areas to free them when the driver gets unloaded
240 //I'm not sure what the limitations of ZwMapViewOfSection are but mapping 128MB videoram (that is probably already mapped by the gfxcard driver) 253 //I'm not sure what the limitations of ZwMapViewOfSection are but mapping 128MB videoram (that is probably already mapped by the gfxcard driver)
241 //won't work so it is generally a good idea to map only the memory you really need 254 //won't work so it is generally a good idea to map only the memory you really need
242 255
243 static NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin){ 256 static STDCALL NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin){
244 alloc_priv* alloclisttmp; 257 alloc_priv* alloclisttmp;
245 PMDL Mdl=NULL; 258 PMDL Mdl=NULL;
246 PVOID SystemVirtualAddress=NULL; 259 PVOID SystemVirtualAddress=NULL;
247 PVOID UserVirtualAddress=NULL; 260 PVOID UserVirtualAddress=NULL;
248 PHYSICAL_ADDRESS pStartPhysAddress; 261 PHYSICAL_ADDRESS pStartPhysAddress;
249 OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace"); 262 OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace");
250 263
264 #ifdef _WIN64
251 pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress; 265 pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress;
266 #else
267 pStartPhysAddress.QuadPart = (ULONGLONG)(ULONG)pPhysAddress;
268 #endif
269 #ifndef NO_SEH
252 __try { 270 __try {
271 #endif
253 SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, /*MmWriteCombined*/MmNonCached); 272 SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, /*MmWriteCombined*/MmNonCached);
254 if(!SystemVirtualAddress){ 273 if(!SystemVirtualAddress){
255 OutputDebugString("dhahelper: MmMapIoSpace failed"); 274 OutputDebugString("dhahelper: MmMapIoSpace failed");
256 return STATUS_INVALID_PARAMETER; 275 return STATUS_INVALID_PARAMETER;
257 } 276 }
261 OutputDebugString("dhahelper: IoAllocateMdl failed"); 280 OutputDebugString("dhahelper: IoAllocateMdl failed");
262 return STATUS_INSUFFICIENT_RESOURCES; 281 return STATUS_INSUFFICIENT_RESOURCES;
263 } 282 }
264 OutputDebugString("dhahelper: Mdl 0x%x",Mdl); 283 OutputDebugString("dhahelper: Mdl 0x%x",Mdl);
265 MmBuildMdlForNonPagedPool(Mdl); 284 MmBuildMdlForNonPagedPool(Mdl);
285 #ifdef _WIN64
286 UserVirtualAddress = (PVOID)(((ULONGLONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl));
287 #else
266 UserVirtualAddress = (PVOID)(((ULONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl)); 288 UserVirtualAddress = (PVOID)(((ULONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl));
289 #endif
267 if(!UserVirtualAddress){ 290 if(!UserVirtualAddress){
268 OutputDebugString("dhahelper: MmMapLockedPages failed"); 291 OutputDebugString("dhahelper: MmMapLockedPages failed");
269 return STATUS_INSUFFICIENT_RESOURCES; 292 return STATUS_INSUFFICIENT_RESOURCES;
270 } 293 }
271 OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress); 294 OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress);
295 #ifndef NO_SEH
272 }__except(EXCEPTION_EXECUTE_HANDLER){ 296 }__except(EXCEPTION_EXECUTE_HANDLER){
273 NTSTATUS ntStatus; 297 NTSTATUS ntStatus;
274 ntStatus = GetExceptionCode(); 298 ntStatus = GetExceptionCode();
275 OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus); 299 OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus);
276 return ntStatus; 300 return ntStatus;
277 } 301 }
302 #endif
278 303
279 304
280 OutputDebugString("dhahelper: adding data to internal allocation list"); 305 OutputDebugString("dhahelper: adding data to internal allocation list");
281 alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv)); 306 alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv));
282 307
302 327
303 OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace"); 328 OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace");
304 return STATUS_SUCCESS; 329 return STATUS_SUCCESS;
305 } 330 }
306 331
307 static NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){ 332 static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){
308 unsigned int i; 333 unsigned int i;
309 unsigned int x=0; 334 unsigned int x=0;
310 unsigned int alloccounttmp=alloccount; 335 unsigned int alloccounttmp=alloccount;
311 OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress); 336 OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress);
312 if(!alloccount){ 337 if(!alloccount){
325 } 350 }
326 x++; 351 x++;
327 } 352 }
328 else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){ 353 else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){
329 if(x==i){ 354 if(x==i){
355 #ifndef NO_SEH
330 __try { 356 __try {
357 #endif
331 MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl); 358 MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl);
332 IoFreeMdl(alloclist[x].Mdl); 359 IoFreeMdl(alloclist[x].Mdl);
333 MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes); 360 MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes);
361 #ifndef NO_SEH
334 }__except(EXCEPTION_EXECUTE_HANDLER){ 362 }__except(EXCEPTION_EXECUTE_HANDLER){
335 NTSTATUS ntStatus; 363 NTSTATUS ntStatus;
336 ntStatus = GetExceptionCode(); 364 ntStatus = GetExceptionCode();
337 OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl); 365 OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl);
338 return ntStatus; 366 return ntStatus;
339 } 367 }
368 #endif
340 } 369 }
341 alloccounttmp--; 370 alloccounttmp--;
342 } 371 }
343 372
344 } 373 }