1 /* $Id: pnpdma.c,v 1.4 2003/10/20 06:03:29 vizzini Exp $
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)
9 * 22/09/2003 FiN Created
12 /* INCLUDES ******************************************************************/
14 #include <ddk/ntddk.h>
15 #include <ddk/pnptypes.h>
16 #include <ddk/pnpfuncs.h>
17 #include <reactos/bugcodes.h>
18 #include <internal/io.h>
19 #include <internal/po.h>
20 #include <internal/ldr.h>
21 #include <internal/module.h>
24 #include <internal/debug.h>
29 #include <ole32/guiddef.h>
31 DEFINE_GUID(GUID_BUS_INTERFACE_STANDARD
, 0x496B8280L
, 0x6F25, 0x11D0, 0xBE, 0xAF, 0x08, 0x00, 0x2B, 0xE2, 0x09, 0x2F);
33 typedef struct _DMA_ADAPTER_INTERNAL
{
36 PDMA_OPERATIONS DmaOperations
;
37 PADAPTER_OBJECT HalAdapter
;
38 } DMA_ADAPTER_INTERNAL
, *PDMA_ADAPTER_INTERNAL
;
40 /* FUNCTIONS *****************************************************************/
44 PDMA_ADAPTER DmaAdapter
)
46 ExFreePool(DmaAdapter
);
51 IopAllocateCommonBuffer(
52 IN PDMA_ADAPTER DmaAdapter
,
54 OUT PPHYSICAL_ADDRESS LogicalAddress
,
55 IN BOOLEAN CacheEnabled
)
57 return HalAllocateCommonBuffer(
58 ((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
,
59 Length
, LogicalAddress
, CacheEnabled
);
65 IN PDMA_ADAPTER DmaAdapter
,
67 IN PHYSICAL_ADDRESS LogicalAddress
,
68 IN PVOID VirtualAddress
,
69 IN BOOLEAN CacheEnabled
)
72 ((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
,
73 Length
, LogicalAddress
, VirtualAddress
, CacheEnabled
);
78 IopAllocateAdapterChannel(
79 IN PDMA_ADAPTER DmaAdapter
,
80 IN PDEVICE_OBJECT DeviceObject
,
81 IN ULONG NumberOfMapRegisters
,
82 IN PDRIVER_CONTROL ExecutionRoutine
,
85 return IoAllocateAdapterChannel(
86 ((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
,
87 DeviceObject
, NumberOfMapRegisters
, ExecutionRoutine
, Context
);
92 IopFlushAdapterBuffers(
93 IN PDMA_ADAPTER DmaAdapter
,
95 IN PVOID MapRegisterBase
,
98 IN BOOLEAN WriteToDevice
)
100 return IoFlushAdapterBuffers(
101 ((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
,
102 Mdl
, MapRegisterBase
, CurrentVa
, Length
, WriteToDevice
);
107 IopFreeAdapterChannel(
108 IN PDMA_ADAPTER DmaAdapter
)
110 IoFreeAdapterChannel(((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
);
116 IN PDMA_ADAPTER DmaAdapter
,
117 PVOID MapRegisterBase
,
118 ULONG NumberOfMapRegisters
)
121 ((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
,
122 MapRegisterBase
, NumberOfMapRegisters
);
128 IN PDMA_ADAPTER DmaAdapter
,
130 IN PVOID MapRegisterBase
,
132 IN OUT PULONG Length
,
133 IN BOOLEAN WriteToDevice
)
135 return IoMapTransfer(
136 ((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
,
137 Mdl
, MapRegisterBase
, CurrentVa
, Length
, WriteToDevice
);
143 IN PDMA_ADAPTER DmaAdapter
)
145 /* FIXME: This is actually true only on i386 and Amd64 */
152 IN PDMA_ADAPTER DmaAdapter
)
154 return HalReadDmaCounter(((PDMA_ADAPTER_INTERNAL
)DmaAdapter
)->HalAdapter
);
159 IopGetScatterGatherList(
160 IN PDMA_ADAPTER DmaAdapter
,
161 IN PDEVICE_OBJECT DeviceObject
,
165 IN PDRIVER_LIST_CONTROL ExecutionRoutine
,
167 IN BOOLEAN WriteToDevice
)
170 return STATUS_UNSUCCESSFUL
;
175 IopPutScatterGatherList(
176 IN PDMA_ADAPTER DmaAdapter
,
177 IN PSCATTER_GATHER_LIST ScatterGather
,
178 IN BOOLEAN WriteToDevice
)
190 IN PDEVICE_OBJECT PhysicalDeviceObject
,
191 IN PDEVICE_DESCRIPTION DeviceDescription
,
192 IN OUT PULONG NumberOfMapRegisters
)
196 BUS_INTERFACE_STANDARD BusInterface
;
197 IO_STATUS_BLOCK IoStatusBlock
;
198 IO_STACK_LOCATION Stack
;
199 DEVICE_DESCRIPTION PrivateDeviceDescription
;
200 PDMA_ADAPTER Result
= NULL
;
201 PDMA_ADAPTER_INTERNAL ResultInternal
= NULL
;
202 PADAPTER_OBJECT HalAdapter
;
204 DPRINT("IoGetDmaAdapter called\n");
207 * Try to create DMA adapter through bus driver
209 if (PhysicalDeviceObject
!= NULL
)
211 if (DeviceDescription
->InterfaceType
== 0x0F /*PNPBus*/ ||
212 DeviceDescription
->InterfaceType
== 0xFFFFFFFF)
214 memcpy(&PrivateDeviceDescription
, DeviceDescription
,
215 sizeof(DEVICE_DESCRIPTION
));
216 Status
= IoGetDeviceProperty(PhysicalDeviceObject
,
217 DevicePropertyLegacyBusType
, sizeof(INTERFACE_TYPE
),
218 &PrivateDeviceDescription
.InterfaceType
, &ResultLength
);
219 if (!NT_SUCCESS(Status
))
221 PrivateDeviceDescription
.InterfaceType
= Internal
;
223 DeviceDescription
= &PrivateDeviceDescription
;
226 Stack
.Parameters
.QueryInterface
.Size
= sizeof(BUS_INTERFACE_STANDARD
);
227 Stack
.Parameters
.QueryInterface
.Version
= 1;
228 Stack
.Parameters
.QueryInterface
.Interface
= (PINTERFACE
)&BusInterface
;
229 Stack
.Parameters
.QueryInterface
.InterfaceType
=
230 &GUID_BUS_INTERFACE_STANDARD
;
231 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
, &IoStatusBlock
,
232 IRP_MN_QUERY_BUS_INFORMATION
, &Stack
);
233 if (!NT_SUCCESS(Status
))
235 Result
= BusInterface
.GetDmaAdapter(BusInterface
.Context
,
236 DeviceDescription
, NumberOfMapRegisters
);
237 BusInterface
.InterfaceDereference(BusInterface
.Context
);
247 HalAdapter
= HalGetAdapter(DeviceDescription
, NumberOfMapRegisters
);
248 if (HalAdapter
== NULL
)
253 ResultInternal
= ExAllocatePool(PagedPool
, sizeof(DMA_ADAPTER_INTERNAL
) +
254 sizeof(DMA_OPERATIONS
));
260 ResultInternal
->Version
= DEVICE_DESCRIPTION_VERSION
;
261 ResultInternal
->Size
= sizeof(DMA_ADAPTER
);
262 ResultInternal
->DmaOperations
= (PDMA_OPERATIONS
)(ResultInternal
+ 1);
263 ResultInternal
->DmaOperations
->Size
= sizeof(DMA_OPERATIONS
);
264 ResultInternal
->DmaOperations
->PutDmaAdapter
= IopPutDmaAdapter
;
265 ResultInternal
->DmaOperations
->AllocateCommonBuffer
= IopAllocateCommonBuffer
;
266 ResultInternal
->DmaOperations
->FreeCommonBuffer
= IopFreeCommonBuffer
;
267 ResultInternal
->DmaOperations
->AllocateAdapterChannel
= IopAllocateAdapterChannel
;
268 ResultInternal
->DmaOperations
->FlushAdapterBuffers
= IopFlushAdapterBuffers
;
269 ResultInternal
->DmaOperations
->FreeAdapterChannel
= IopFreeAdapterChannel
;
270 ResultInternal
->DmaOperations
->FreeMapRegisters
= IopFreeMapRegisters
;
271 ResultInternal
->DmaOperations
->MapTransfer
= IopMapTransfer
;
272 ResultInternal
->DmaOperations
->GetDmaAlignment
= IopGetDmaAlignment
;
273 ResultInternal
->DmaOperations
->ReadDmaCounter
= IopReadDmaCounter
;
274 ResultInternal
->DmaOperations
->GetScatterGatherList
= IopGetScatterGatherList
;
275 ResultInternal
->DmaOperations
->PutScatterGatherList
= IopPutScatterGatherList
;
276 ResultInternal
->HalAdapter
= HalAdapter
;
278 return (PDMA_ADAPTER
)ResultInternal
;