Fix DMA_OPERATIONS to use stdcall.
[reactos.git] / reactos / ntoskrnl / io / pnpdma.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/pnpmgr.c
6 * PURPOSE: PnP manager DMA routines
7 * PROGRAMMER: Filip Navara (xnavara@volny.cz)
8 * UPDATE HISTORY:
9 * 22/09/2003 FiN Created
10 */
11
12 /* INCLUDES ******************************************************************/
13
14 #include <ntoskrnl.h>
15 #define NDEBUG
16 #include <internal/debug.h>
17 #ifdef __USE_W32API
18 #include <initguid.h>
19 #else
20 #include <ole32/guiddef.h>
21 #endif
22 #include <ddk/wdmguid.h>
23
24 typedef struct _DMA_ADAPTER_INTERNAL {
25 USHORT Version;
26 USHORT Size;
27 PDMA_OPERATIONS DmaOperations;
28 PADAPTER_OBJECT HalAdapter;
29 } DMA_ADAPTER_INTERNAL, *PDMA_ADAPTER_INTERNAL;
30
31 /* FUNCTIONS *****************************************************************/
32
33 VOID
34 STDCALL
35 IopPutDmaAdapter(
36 PDMA_ADAPTER DmaAdapter)
37 {
38 DPRINT("IopPutDmaAdapter\n");
39 ExFreePool(DmaAdapter);
40 }
41
42
43 PVOID
44 STDCALL
45 IopAllocateCommonBuffer(
46 IN PDMA_ADAPTER DmaAdapter,
47 IN ULONG Length,
48 OUT PPHYSICAL_ADDRESS LogicalAddress,
49 IN BOOLEAN CacheEnabled)
50 {
51 DPRINT("IopAllocateCommonBuffer\n");
52 return HalAllocateCommonBuffer(
53 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
54 Length, LogicalAddress, CacheEnabled);
55 }
56
57
58 VOID
59 STDCALL
60 IopFreeCommonBuffer(
61 IN PDMA_ADAPTER DmaAdapter,
62 IN ULONG Length,
63 IN PHYSICAL_ADDRESS LogicalAddress,
64 IN PVOID VirtualAddress,
65 IN BOOLEAN CacheEnabled)
66 {
67 DPRINT("IopFreeCommonBuffer\n");
68 HalFreeCommonBuffer(
69 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
70 Length, LogicalAddress, VirtualAddress, CacheEnabled);
71 }
72
73
74 NTSTATUS
75 STDCALL
76 IopAllocateAdapterChannel(
77 IN PDMA_ADAPTER DmaAdapter,
78 IN PDEVICE_OBJECT DeviceObject,
79 IN ULONG NumberOfMapRegisters,
80 IN PDRIVER_CONTROL ExecutionRoutine,
81 IN PVOID Context)
82 {
83 DPRINT("IopAllocateAdapterChannel\n");
84 return IoAllocateAdapterChannel(
85 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
86 DeviceObject, NumberOfMapRegisters, ExecutionRoutine, Context);
87 }
88
89
90 BOOLEAN
91 STDCALL
92 IopFlushAdapterBuffers(
93 IN PDMA_ADAPTER DmaAdapter,
94 IN PMDL Mdl,
95 IN PVOID MapRegisterBase,
96 IN PVOID CurrentVa,
97 IN ULONG Length,
98 IN BOOLEAN WriteToDevice)
99 {
100 DPRINT("IopFlushAdapterBuffers\n");
101 return IoFlushAdapterBuffers(
102 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
103 Mdl, MapRegisterBase, CurrentVa, Length, WriteToDevice);
104 }
105
106
107 VOID
108 STDCALL
109 IopFreeAdapterChannel(
110 IN PDMA_ADAPTER DmaAdapter)
111 {
112 DPRINT("IopFreeAdapterChannel\n");
113 IoFreeAdapterChannel(((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter);
114 }
115
116
117 VOID
118 STDCALL
119 IopFreeMapRegisters(
120 IN PDMA_ADAPTER DmaAdapter,
121 PVOID MapRegisterBase,
122 ULONG NumberOfMapRegisters)
123 {
124 DPRINT("IopFreeMapRegisters\n");
125 IoFreeMapRegisters(
126 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
127 MapRegisterBase, NumberOfMapRegisters);
128 }
129
130
131 PHYSICAL_ADDRESS
132 STDCALL
133 IopMapTransfer(
134 IN PDMA_ADAPTER DmaAdapter,
135 IN PMDL Mdl,
136 IN PVOID MapRegisterBase,
137 IN PVOID CurrentVa,
138 IN OUT PULONG Length,
139 IN BOOLEAN WriteToDevice)
140 {
141 DPRINT("IopMapTransfer\n");
142 return IoMapTransfer(
143 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
144 Mdl, MapRegisterBase, CurrentVa, Length, WriteToDevice);
145 }
146
147
148 ULONG
149 STDCALL
150 IopGetDmaAlignment(
151 IN PDMA_ADAPTER DmaAdapter)
152 {
153 DPRINT("IopGetDmaAlignment\n");
154 /* FIXME: This is actually true only on i386 and Amd64 */
155 return 1L;
156 }
157
158
159 ULONG
160 STDCALL
161 IopReadDmaCounter(
162 IN PDMA_ADAPTER DmaAdapter)
163 {
164 DPRINT("IopReadDmaCounter\n");
165 return HalReadDmaCounter(((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter);
166 }
167
168
169 NTSTATUS
170 STDCALL
171 IopGetScatterGatherList(
172 IN PDMA_ADAPTER DmaAdapter,
173 IN PDEVICE_OBJECT DeviceObject,
174 IN PMDL Mdl,
175 IN PVOID CurrentVa,
176 IN ULONG Length,
177 IN PDRIVER_LIST_CONTROL ExecutionRoutine,
178 IN PVOID Context,
179 IN BOOLEAN WriteToDevice)
180 {
181 DPRINT("IopGetScatterGatherList\n");
182 /* FIXME */
183 return STATUS_UNSUCCESSFUL;
184 }
185
186
187 VOID
188 STDCALL
189 IopPutScatterGatherList(
190 IN PDMA_ADAPTER DmaAdapter,
191 IN PSCATTER_GATHER_LIST ScatterGather,
192 IN BOOLEAN WriteToDevice)
193 {
194 DPRINT("IopPutScatterGatherList\n");
195 /* FIXME */
196 }
197
198 /*
199 * @implemented
200 */
201 PDMA_ADAPTER
202 STDCALL
203 IoGetDmaAdapter(
204 IN PDEVICE_OBJECT PhysicalDeviceObject,
205 IN PDEVICE_DESCRIPTION DeviceDescription,
206 IN OUT PULONG NumberOfMapRegisters)
207 {
208 static DMA_OPERATIONS HalDmaOperations = {
209 sizeof(DMA_OPERATIONS),
210 IopPutDmaAdapter,
211 IopAllocateCommonBuffer,
212 IopFreeCommonBuffer,
213 IopAllocateAdapterChannel,
214 IopFlushAdapterBuffers,
215 IopFreeAdapterChannel,
216 IopFreeMapRegisters,
217 IopMapTransfer,
218 IopGetDmaAlignment,
219 IopReadDmaCounter,
220 IopGetScatterGatherList,
221 IopPutScatterGatherList
222 };
223 NTSTATUS Status;
224 ULONG ResultLength;
225 BUS_INTERFACE_STANDARD BusInterface;
226 IO_STATUS_BLOCK IoStatusBlock;
227 IO_STACK_LOCATION Stack;
228 DEVICE_DESCRIPTION PrivateDeviceDescription;
229 PDMA_ADAPTER Result = NULL;
230 PDMA_ADAPTER_INTERNAL ResultInternal = NULL;
231 PADAPTER_OBJECT HalAdapter;
232
233 DPRINT("IoGetDmaAdapter called\n");
234
235 /*
236 * Try to create DMA adapter through bus driver
237 */
238 if (PhysicalDeviceObject != NULL)
239 {
240 if (DeviceDescription->InterfaceType == 0x0F /*PNPBus*/ ||
241 DeviceDescription->InterfaceType == 0xFFFFFFFF)
242 {
243 RtlCopyMemory(&PrivateDeviceDescription, DeviceDescription,
244 sizeof(DEVICE_DESCRIPTION));
245 Status = IoGetDeviceProperty(PhysicalDeviceObject,
246 DevicePropertyLegacyBusType, sizeof(INTERFACE_TYPE),
247 &PrivateDeviceDescription.InterfaceType, &ResultLength);
248 if (!NT_SUCCESS(Status))
249 {
250 PrivateDeviceDescription.InterfaceType = Internal;
251 }
252 DeviceDescription = &PrivateDeviceDescription;
253 }
254
255 Stack.Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
256 Stack.Parameters.QueryInterface.Version = 1;
257 Stack.Parameters.QueryInterface.Interface = (PINTERFACE)&BusInterface;
258 Stack.Parameters.QueryInterface.InterfaceType =
259 &GUID_BUS_INTERFACE_STANDARD;
260 Status = IopInitiatePnpIrp(PhysicalDeviceObject, &IoStatusBlock,
261 IRP_MN_QUERY_INTERFACE, &Stack);
262 if (NT_SUCCESS(Status))
263 {
264 Result = BusInterface.GetDmaAdapter(BusInterface.Context,
265 DeviceDescription, NumberOfMapRegisters);
266 BusInterface.InterfaceDereference(BusInterface.Context);
267 if (Result != NULL)
268 return Result;
269 }
270 }
271
272 /*
273 * Fallback to HAL
274 */
275
276 HalAdapter = HalGetAdapter(DeviceDescription, NumberOfMapRegisters);
277 if (HalAdapter == NULL)
278 return NULL;
279
280 ResultInternal = ExAllocatePool(PagedPool, sizeof(DMA_ADAPTER_INTERNAL));
281 if (ResultInternal == NULL)
282 return NULL;
283
284 ResultInternal->Version = DEVICE_DESCRIPTION_VERSION;
285 ResultInternal->Size = sizeof(DMA_ADAPTER);
286 ResultInternal->DmaOperations = &HalDmaOperations;
287 ResultInternal->HalAdapter = HalAdapter;
288
289 return (PDMA_ADAPTER)ResultInternal;
290 }
291
292 /* EOF */