annotate vidix/dhahelperwin/dhahelper.c @ 25275:d4fda3892e5b

sync with vidix.sf.net r319: remove useless vars
author ben
date Tue, 04 Dec 2007 22:33:46 +0000
parents 173d35b7aca3
children a1c020529cc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
1 /******************************************************************************
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
2 * dhahelper.c: direct hardware access under Windows NT/2000/XP
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
3 * Copyright (c) 2004 Sascha Sommer <saschasommer@freenet.de>.
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
4 * Patched to compile with MinGW by Kevin Kofler:
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
5 * Copyright (c) 2007 Kevin Kofler
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
6 *
23277
32873ccfb007 Add correct boilerplate license headers.
diego
parents: 22900
diff changeset
7 * This file is part of MPlayer.
32873ccfb007 Add correct boilerplate license headers.
diego
parents: 22900
diff changeset
8 *
32873ccfb007 Add correct boilerplate license headers.
diego
parents: 22900
diff changeset
9 * MPlayer is free software; you can redistribute it and/or modify
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
12 * (at your option) any later version.
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
13 *
23277
32873ccfb007 Add correct boilerplate license headers.
diego
parents: 22900
diff changeset
14 * MPlayer is distributed in the hope that it will be useful,
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
17 * GNU General Public License for more details.
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
18 *
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
23277
32873ccfb007 Add correct boilerplate license headers.
diego
parents: 22900
diff changeset
20 * along with MPlayer; if not, write to the Free Software
19614
36589811e5d0 The FSF changed postal address.
diego
parents: 12057
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
22 *
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
23 *****************************************************************************/
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
24
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
25
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
26 #if defined(_MSC_VER)
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
27 #include <ntddk.h>
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
28 #ifndef STDCALL
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
29 #define STDCALL /* nothing */
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
30 #endif
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
31 #elif defined(__MINGW32__)
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
32 #include <ddk/ntddk.h>
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
33 #define NO_SEH /* FIXME */
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
34 #else
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
35 #error Unsupported compiler. This driver requires MSVC+DDK or MinGW to build.
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
36 #endif
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
37
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
38 #include "dhahelper.h"
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
39
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
40 #define OutputDebugString DbgPrint
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
41
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
42 #define IOPM_SIZE 0x2000
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
43 typedef char IOPM[IOPM_SIZE];
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
44 static IOPM *pIOPM = NULL;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
45
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
46
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
47
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
48 typedef struct {
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
49 PMDL Mdl;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
50 PVOID SystemVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
51 PVOID UserVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
52 ULONG PhysMemSizeInBytes;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
53 }alloc_priv;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
54 static alloc_priv* alloclist;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
55 static unsigned int alloccount=0;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
56
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
57
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
58
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
59
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
60
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
61
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
62
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
63 static STDCALL NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
64 static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject);
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
65 static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress);
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
66 static STDCALL NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin);
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
67
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
68 void STDCALL Ke386SetIoAccessMap(int, IOPM *);
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
69 void STDCALL Ke386QueryIoAccessMap(int, IOPM *);
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
70 void STDCALL Ke386IoSetAccessProcess(PEPROCESS, int);
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
71
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
72
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
73
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
74
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
75 //entry point
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
76 STDCALL NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath){
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
77 UNICODE_STRING DeviceNameUnicodeString;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
78 UNICODE_STRING DeviceLinkUnicodeString;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
79 NTSTATUS ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
80 PDEVICE_OBJECT DeviceObject = NULL;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
81
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
82 OutputDebugString ("dhahelper: entering DriverEntry");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
83
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
84 RtlInitUnicodeString (&DeviceNameUnicodeString, L"\\Device\\DHAHELPER");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
85
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
86 // Create an EXCLUSIVE device object (only 1 thread at a time
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
87 // can make requests to this device).
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
88
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
89 ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_DHAHELPER,0,TRUE,&DeviceObject);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
90
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
91 if (NT_SUCCESS(ntStatus)){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
92 // Create dispatch points for device control, create, close.
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
93 DriverObject->MajorFunction[IRP_MJ_CREATE] =
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
94 DriverObject->MajorFunction[IRP_MJ_CLOSE] =
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
95 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = dhahelperdispatch;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
96 DriverObject->DriverUnload = dhahelperunload;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
97
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
98 // Create a symbolic link, e.g. a name that a Win32 app can specify
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
99 // to open the device.
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
100
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
101 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
102
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
103 ntStatus = IoCreateSymbolicLink(&DeviceLinkUnicodeString,&DeviceNameUnicodeString);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
104
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
105 if (!NT_SUCCESS(ntStatus)){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
106 // Symbolic link creation failed- note this & then delete the
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
107 // device object (it's useless if a Win32 app can't get at it).
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
108 OutputDebugString ("dhahelper: IoCreateSymbolicLink failed");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
109 IoDeleteDevice (DeviceObject);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
110 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
111 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
112 else{
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
113 OutputDebugString ("dhahelper: IoCreateDevice failed");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
114 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
115 OutputDebugString ("dhahelper: leaving DriverEntry");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
116 return ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
117 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
118
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
119
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
120 // Process the IRPs sent to this device
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
121
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
122 static STDCALL NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp){
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
123 PIO_STACK_LOCATION IrpStack;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
124 ULONG dwInputBufferLength;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
125 ULONG dwOutputBufferLength;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
126 ULONG dwIoControlCode;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
127 PVOID pvIOBuffer;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
128 NTSTATUS ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
129 dhahelper_t dhahelper_priv;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
130
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
131 OutputDebugString ("dhahelper: entering dhahelperdispatch");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
132
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
133 // Init to default settings
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
134
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
135 Irp->IoStatus.Status = STATUS_SUCCESS;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
136 Irp->IoStatus.Information = 0;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
137
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
138 IrpStack = IoGetCurrentIrpStackLocation(Irp);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
139
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
140 // Get the pointer to the input/output buffer and it's length
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
141
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
142 pvIOBuffer = Irp->AssociatedIrp.SystemBuffer;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
143 dwInputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
144 dwOutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
145
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
146 switch (IrpStack->MajorFunction){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
147 case IRP_MJ_CREATE:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
148 OutputDebugString("dhahelper: IRP_MJ_CREATE");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
149 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
150 case IRP_MJ_CLOSE:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
151 OutputDebugString("dhahelper: IRP_MJ_CLOSE");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
152 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
153 case IRP_MJ_DEVICE_CONTROL:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
154 OutputDebugString("dhahelper: IRP_MJ_DEVICE_CONTROL");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
155 dwIoControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
156 switch (dwIoControlCode){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
157 case IOCTL_DHAHELPER_ENABLEDIRECTIO:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
158 OutputDebugString("dhahelper: IOCTL_DHAHELPER_ENABLEDIRECTIO");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
159 pIOPM = MmAllocateNonCachedMemory(sizeof(IOPM));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
160 if (pIOPM){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
161 RtlZeroMemory(pIOPM, sizeof(IOPM));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
162 Ke386IoSetAccessProcess(PsGetCurrentProcess(), 1);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
163 Ke386SetIoAccessMap(1, pIOPM);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
164 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
165 else Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
166 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
167 case IOCTL_DHAHELPER_DISABLEDIRECTIO:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
168 OutputDebugString("dhahelper: IOCTL_DHAHELPER_DISABLEDIRECTIO");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
169 if (pIOPM){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
170 Ke386IoSetAccessProcess(PsGetCurrentProcess(), 0);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
171 Ke386SetIoAccessMap(1, pIOPM);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
172 MmFreeNonCachedMemory(pIOPM, sizeof(IOPM));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
173 pIOPM = NULL;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
174 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
175 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
176 case IOCTL_DHAHELPER_MAPPHYSTOLIN:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
177 OutputDebugString("dhahelper: IOCTL_DHAHELPER_MAPPHYSTOLIN");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
178 if (dwInputBufferLength){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
179 memcpy (&dhahelper_priv, pvIOBuffer, dwInputBufferLength);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
180 ntStatus = MapPhysicalMemoryToLinearSpace(dhahelper_priv.base,dhahelper_priv.size,&dhahelper_priv.ptr);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
181 if (NT_SUCCESS(ntStatus)){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
182 memcpy (pvIOBuffer, &dhahelper_priv, dwInputBufferLength);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
183 Irp->IoStatus.Information = dwInputBufferLength;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
184 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
185 Irp->IoStatus.Status = ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
186 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
187 else Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
188 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
189 case IOCTL_DHAHELPER_UNMAPPHYSADDR:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
190 OutputDebugString("dhahelper: IOCTL_DHAHELPER_UNMAPPHYSADDR");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
191 if (dwInputBufferLength){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
192 memcpy (&dhahelper_priv, pvIOBuffer, dwInputBufferLength);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
193 ntStatus = UnmapPhysicalMemory(dhahelper_priv.ptr);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
194 Irp->IoStatus.Status = ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
195 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
196 else
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
197 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
198 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
199 default:
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
200 OutputDebugString("dhahelper: unknown IRP_MJ_DEVICE_CONTROL");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
201 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
202 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
203 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
204 break;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
205 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
206
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
207 // DON'T get cute and try to use the status field of the irp in the
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
208 // return status. That IRP IS GONE as soon as you call IoCompleteRequest.
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
209
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
210 ntStatus = Irp->IoStatus.Status;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
211
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
212 IoCompleteRequest (Irp, IO_NO_INCREMENT);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
213
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
214 // We never have pending operation so always return the status code.
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
215
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
216 OutputDebugString("dhahelper: leaving dhahelperdispatch");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
217
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
218 return ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
219 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
220
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
221 // Delete the associated device and return
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
222
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
223 static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject){
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
224 UNICODE_STRING DeviceLinkUnicodeString;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
225 NTSTATUS ntStatus=STATUS_SUCCESS;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
226 OutputDebugString ("dhahelper: entering dhahelperunload");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
227 OutputDebugString ("dhahelper: unmapping remaining memory");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
228
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
229 while(alloccount && (ntStatus==STATUS_SUCCESS))ntStatus = UnmapPhysicalMemory(alloclist[alloccount-1].UserVirtualAddress);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
230 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
231 ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
232
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
233 if (NT_SUCCESS(ntStatus)){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
234 IoDeleteDevice (DriverObject->DeviceObject);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
235 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
236 else {
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
237 OutputDebugString ("dhahelper: IoDeleteSymbolicLink failed");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
238 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
239 OutputDebugString ("dhahelper: leaving dhahelperunload");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
240 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
241
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
242
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
243
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
244
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
245
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
246
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
247 /************************* memory mapping functions ******************************/
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
248 //unlike the functions of other io helpers these functions allow to map adapter memory on windows xp
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
249 //even if it has alread been mapped by the original driver
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
250 //the technique used is described in
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
251 //http://support.microsoft.com/default.aspx?scid=kb;en-us;q189327
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
252 //furthermore it keeps a list of mapped areas to free them when the driver gets unloaded
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
253 //I'm not sure what the limitations of ZwMapViewOfSection are but mapping 128MB videoram (that is probably already mapped by the gfxcard driver)
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
254 //won't work so it is generally a good idea to map only the memory you really need
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
255
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
256 static STDCALL NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin){
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
257 alloc_priv* alloclisttmp;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
258 PMDL Mdl=NULL;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
259 PVOID SystemVirtualAddress=NULL;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
260 PVOID UserVirtualAddress=NULL;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
261 PHYSICAL_ADDRESS pStartPhysAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
262 OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
263
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
264 #ifdef _WIN64
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
265 pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress;
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
266 #else
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
267 pStartPhysAddress.QuadPart = (ULONGLONG)(ULONG)pPhysAddress;
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
268 #endif
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
269 #ifndef NO_SEH
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
270 __try {
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
271 #endif
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
272 SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, /*MmWriteCombined*/MmNonCached);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
273 if(!SystemVirtualAddress){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
274 OutputDebugString("dhahelper: MmMapIoSpace failed");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
275 return STATUS_INVALID_PARAMETER;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
276 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
277 OutputDebugString("dhahelper: SystemVirtualAddress 0x%x",SystemVirtualAddress);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
278 Mdl=IoAllocateMdl(SystemVirtualAddress, PhysMemSizeInBytes, FALSE, FALSE,NULL);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
279 if(!Mdl){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
280 OutputDebugString("dhahelper: IoAllocateMdl failed");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
281 return STATUS_INSUFFICIENT_RESOURCES;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
282 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
283 OutputDebugString("dhahelper: Mdl 0x%x",Mdl);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
284 MmBuildMdlForNonPagedPool(Mdl);
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
285 #ifdef _WIN64
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
286 UserVirtualAddress = (PVOID)(((ULONGLONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl));
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
287 #else
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
288 UserVirtualAddress = (PVOID)(((ULONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl));
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
289 #endif
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
290 if(!UserVirtualAddress){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
291 OutputDebugString("dhahelper: MmMapLockedPages failed");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
292 return STATUS_INSUFFICIENT_RESOURCES;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
293 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
294 OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress);
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
295 #ifndef NO_SEH
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
296 }__except(EXCEPTION_EXECUTE_HANDLER){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
297 NTSTATUS ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
298 ntStatus = GetExceptionCode();
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
299 OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
300 return ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
301 }
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
302 #endif
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
303
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
304
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
305 OutputDebugString("dhahelper: adding data to internal allocation list");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
306 alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
307
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
308
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
309 if(!alloclisttmp){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
310 OutputDebugString("dhahelper: not enough memory to create temporary allocation list");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
311 MmUnmapLockedPages(UserVirtualAddress, Mdl);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
312 IoFreeMdl(Mdl);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
313 return STATUS_INSUFFICIENT_RESOURCES;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
314 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
315 if(alloccount){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
316 memcpy(alloclisttmp,alloclist,alloccount * sizeof(alloc_priv));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
317 MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
318 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
319 alloclist=alloclisttmp;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
320 alloclist[alloccount].Mdl=Mdl;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
321 alloclist[alloccount].SystemVirtualAddress=SystemVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
322 alloclist[alloccount].UserVirtualAddress=UserVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
323 alloclist[alloccount].PhysMemSizeInBytes=PhysMemSizeInBytes;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
324 ++alloccount;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
325
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
326 *PhysMemLin=UserVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
327
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
328 OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
329 return STATUS_SUCCESS;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
330 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
331
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
332 static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
333 unsigned int i;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
334 unsigned int x=0;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
335 unsigned int alloccounttmp=alloccount;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
336 OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
337 if(!alloccount){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
338 OutputDebugString("dhahelper: UnmapPhysicalMemory: nothing todo -> leaving...");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
339 return STATUS_SUCCESS;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
340 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
341
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
342 for(i=0;i<alloccount;i++){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
343 if(alloclist[i].UserVirtualAddress!=UserVirtualAddress){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
344 if(x!=i){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
345 alloclist[x].Mdl=alloclist[i].Mdl;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
346 alloclist[x].SystemVirtualAddress=alloclist[i].SystemVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
347 alloclist[x].UserVirtualAddress=alloclist[i].UserVirtualAddress;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
348 alloclist[x].PhysMemSizeInBytes=alloclist[i].PhysMemSizeInBytes;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
349
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
350 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
351 x++;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
352 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
353 else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
354 if(x==i){
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
355 #ifndef NO_SEH
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
356 __try {
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
357 #endif
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
358 MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
359 IoFreeMdl(alloclist[x].Mdl);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
360 MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes);
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
361 #ifndef NO_SEH
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
362 }__except(EXCEPTION_EXECUTE_HANDLER){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
363 NTSTATUS ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
364 ntStatus = GetExceptionCode();
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
365 OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
366 return ntStatus;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
367 }
24541
173d35b7aca3 - make dhahelperwin compile with mingw
faust3
parents: 23277
diff changeset
368 #endif
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
369 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
370 alloccounttmp--;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
371 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
372
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
373 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
374
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
375 if(alloccounttmp){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
376 alloc_priv* alloclisttmp;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
377 alloclisttmp=MmAllocateNonCachedMemory(alloccounttmp*sizeof(alloc_priv));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
378 if(!alloclisttmp){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
379 OutputDebugString("dhahelper: not enough memory to create temporary allocation list");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
380 return STATUS_INSUFFICIENT_RESOURCES;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
381 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
382 memcpy(alloclisttmp,alloclist,alloccounttmp * sizeof(alloc_priv));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
383 MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv));
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
384 alloclist=alloclisttmp;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
385 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
386 alloccount=alloccounttmp;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
387
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
388 OutputDebugString("dhahelper: leaving UnmapPhysicalMemory");
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
389 return STATUS_SUCCESS;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
390 }