- Minor device node fixes.
[reactos.git] / reactos / ntoskrnl / io / pnpdma.c
1 /* $Id: pnpdma.c,v 1.9 2004/10/23 17:32:51 navaraf Exp $
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 IopPutDmaAdapter(
35 PDMA_ADAPTER DmaAdapter)
36 {
37 DPRINT("IopPutDmaAdapter\n");
38 ExFreePool(DmaAdapter);
39 }
40
41
42 PVOID
43 IopAllocateCommonBuffer(
44 IN PDMA_ADAPTER DmaAdapter,
45 IN ULONG Length,
46 OUT PPHYSICAL_ADDRESS LogicalAddress,
47 IN BOOLEAN CacheEnabled)
48 {
49 DPRINT("IopAllocateCommonBuffer\n");
50 return HalAllocateCommonBuffer(
51 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
52 Length, LogicalAddress, CacheEnabled);
53 }
54
55
56 VOID
57 IopFreeCommonBuffer(
58 IN PDMA_ADAPTER DmaAdapter,
59 IN ULONG Length,
60 IN PHYSICAL_ADDRESS LogicalAddress,
61 IN PVOID VirtualAddress,
62 IN BOOLEAN CacheEnabled)
63 {
64 DPRINT("IopFreeCommonBuffer\n");
65 HalFreeCommonBuffer(
66 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
67 Length, LogicalAddress, VirtualAddress, CacheEnabled);
68 }
69
70
71 NTSTATUS
72 IopAllocateAdapterChannel(
73 IN PDMA_ADAPTER DmaAdapter,
74 IN PDEVICE_OBJECT DeviceObject,
75 IN ULONG NumberOfMapRegisters,
76 IN PDRIVER_CONTROL ExecutionRoutine,
77 IN PVOID Context)
78 {
79 DPRINT("IopAllocateAdapterChannel\n");
80 return IoAllocateAdapterChannel(
81 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
82 DeviceObject, NumberOfMapRegisters, ExecutionRoutine, Context);
83 }
84
85
86 BOOLEAN
87 IopFlushAdapterBuffers(
88 IN PDMA_ADAPTER DmaAdapter,
89 IN PMDL Mdl,
90 IN PVOID MapRegisterBase,
91 IN PVOID CurrentVa,
92 IN ULONG Length,
93 IN BOOLEAN WriteToDevice)
94 {
95 DPRINT("IopFlushAdapterBuffers\n");
96 return IoFlushAdapterBuffers(
97 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
98 Mdl, MapRegisterBase, CurrentVa, Length, WriteToDevice);
99 }
100
101
102 VOID
103 IopFreeAdapterChannel(
104 IN PDMA_ADAPTER DmaAdapter)
105 {
106 DPRINT("IopFreeAdapterChannel\n");
107 IoFreeAdapterChannel(((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter);
108 }
109
110
111 VOID
112 IopFreeMapRegisters(
113 IN PDMA_ADAPTER DmaAdapter,
114 PVOID MapRegisterBase,
115 ULONG NumberOfMapRegisters)
116 {
117 DPRINT("IopFreeMapRegisters\n");
118 IoFreeMapRegisters(
119 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
120 MapRegisterBase, NumberOfMapRegisters);
121 }
122
123
124 PHYSICAL_ADDRESS
125 IopMapTransfer(
126 IN PDMA_ADAPTER DmaAdapter,
127 IN PMDL Mdl,
128 IN PVOID MapRegisterBase,
129 IN PVOID CurrentVa,
130 IN OUT PULONG Length,
131 IN BOOLEAN WriteToDevice)
132 {
133 DPRINT("IopMapTransfer\n");
134 return IoMapTransfer(
135 ((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter,
136 Mdl, MapRegisterBase, CurrentVa, Length, WriteToDevice);
137 }
138
139
140 ULONG
141 IopGetDmaAlignment(
142 IN PDMA_ADAPTER DmaAdapter)
143 {
144 DPRINT("IopGetDmaAlignment\n");
145 /* FIXME: This is actually true only on i386 and Amd64 */
146 return 1L;
147 }
148
149
150 ULONG
151 IopReadDmaCounter(
152 IN PDMA_ADAPTER DmaAdapter)
153 {
154 DPRINT("IopReadDmaCounter\n");
155 return HalReadDmaCounter(((PDMA_ADAPTER_INTERNAL)DmaAdapter)->HalAdapter);
156 }
157
158
159 NTSTATUS
160 IopGetScatterGatherList(
161 IN PDMA_ADAPTER DmaAdapter,
162 IN PDEVICE_OBJECT DeviceObject,
163 IN PMDL Mdl,
164 IN PVOID CurrentVa,
165 IN ULONG Length,
166 IN PDRIVER_LIST_CONTROL ExecutionRoutine,
167 IN PVOID Context,
168 IN BOOLEAN WriteToDevice)
169 {
170 DPRINT("IopGetScatterGatherList\n");
171 /* FIXME */
172 return STATUS_UNSUCCESSFUL;
173 }
174
175
176 VOID
177 IopPutScatterGatherList(
178 IN PDMA_ADAPTER DmaAdapter,
179 IN PSCATTER_GATHER_LIST ScatterGather,
180 IN BOOLEAN WriteToDevice)
181 {
182 /* FIXME */
183 }
184
185
186 /*
187 * @implemented
188 */
189 PDMA_ADAPTER
190 STDCALL
191 IoGetDmaAdapter(
192 IN PDEVICE_OBJECT PhysicalDeviceObject,
193 IN PDEVICE_DESCRIPTION DeviceDescription,
194 IN OUT PULONG NumberOfMapRegisters)
195 {
196 static DMA_OPERATIONS HalDmaOperations = {
197 sizeof(DMA_OPERATIONS),
198 IopPutDmaAdapter,
199 IopAllocateCommonBuffer,
200 IopFreeCommonBuffer,
201 IopAllocateAdapterChannel,
202 IopFlushAdapterBuffers,
203 IopFreeAdapterChannel,
204 IopFreeMapRegisters,
205 IopMapTransfer,
206 IopGetDmaAlignment,
207 IopReadDmaCounter,
208 IopGetScatterGatherList,
209 IopPutScatterGatherList
210 };
211 NTSTATUS Status;
212 ULONG ResultLength;
213 BUS_INTERFACE_STANDARD BusInterface;
214 IO_STATUS_BLOCK IoStatusBlock;
215 IO_STACK_LOCATION Stack;
216 DEVICE_DESCRIPTION PrivateDeviceDescription;
217 PDMA_ADAPTER Result = NULL;
218 PDMA_ADAPTER_INTERNAL ResultInternal = NULL;
219 PADAPTER_OBJECT HalAdapter;
220
221 DPRINT("IoGetDmaAdapter called\n");
222
223 /*
224 * Try to create DMA adapter through bus driver
225 */
226 if (PhysicalDeviceObject != NULL)
227 {
228 if (DeviceDescription->InterfaceType == 0x0F /*PNPBus*/ ||
229 DeviceDescription->InterfaceType == 0xFFFFFFFF)
230 {
231 RtlCopyMemory(&PrivateDeviceDescription, DeviceDescription,
232 sizeof(DEVICE_DESCRIPTION));
233 Status = IoGetDeviceProperty(PhysicalDeviceObject,
234 DevicePropertyLegacyBusType, sizeof(INTERFACE_TYPE),
235 &PrivateDeviceDescription.InterfaceType, &ResultLength);
236 if (!NT_SUCCESS(Status))
237 {
238 PrivateDeviceDescription.InterfaceType = Internal;
239 }
240 DeviceDescription = &PrivateDeviceDescription;
241 }
242
243 Stack.Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
244 Stack.Parameters.QueryInterface.Version = 1;
245 Stack.Parameters.QueryInterface.Interface = (PINTERFACE)&BusInterface;
246 Stack.Parameters.QueryInterface.InterfaceType =
247 &GUID_BUS_INTERFACE_STANDARD;
248 Status = IopInitiatePnpIrp(PhysicalDeviceObject, &IoStatusBlock,
249 IRP_MN_QUERY_INTERFACE, &Stack);
250 if (NT_SUCCESS(Status))
251 {
252 Result = BusInterface.GetDmaAdapter(BusInterface.Context,
253 DeviceDescription, NumberOfMapRegisters);
254 BusInterface.InterfaceDereference(BusInterface.Context);
255 if (Result != NULL)
256 return Result;
257 }
258 }
259
260 /*
261 * Fallback to HAL
262 */
263
264 HalAdapter = HalGetAdapter(DeviceDescription, NumberOfMapRegisters);
265 if (HalAdapter == NULL)
266 return NULL;
267
268 ResultInternal = ExAllocatePool(PagedPool, sizeof(DMA_ADAPTER_INTERNAL));
269 if (ResultInternal == NULL)
270 return NULL;
271
272 ResultInternal->Version = DEVICE_DESCRIPTION_VERSION;
273 ResultInternal->Size = sizeof(DMA_ADAPTER);
274 ResultInternal->DmaOperations = &HalDmaOperations;
275 ResultInternal->HalAdapter = HalAdapter;
276
277 return (PDMA_ADAPTER)ResultInternal;
278 }
279
280 /* EOF */