ReactOS  0.4.14-dev-390-g34947ad
dma.c
Go to the documentation of this file.
1 /*
2  Routines to simplify the use of DMA for Sound Blaster driver
3 */
4 
5 #include <ntddk.h>
6 #include "sndblst.h"
7 
8 #if 0
10 {
11  // Don't forget to check for Compaq machines (they can't be configured
12  // manually...)
13 
14  return TRUE; // for now...
15 
16 // if (! CompaqBA)
17 // return (BOOLEAN) !((INPORT(pHw, BOARD_ID) & 0x80) && Device->DMA == 0);
18 // else
19 // {
20 // UCHAR CompaqPIDR;
21 // UCHAR Expected = (UCHAR)(Device->DMA == 0 ? 0x40 :
22 // Device->DMA == 1 ? 0x80 :
23 // 0xc0);
24 // CompaqPIDR = READ_PORT_UCHAR(pHw->CompaqBA + BOARD_ID);
25 // if (CompaqPIDR != 0xff)
26 // {
27 // if ((UCHAR)(CompaqPIDR & 0xc0) == Expected)
28 // return true;
29 // }
30 // }
31 
32  return FALSE;
33 }
34 #endif
35 
36 
39  IN PIRP Irp,
42 {
44  ULONG zzz;
46 
47  DPRINT("IoMapTransfer\n");
48  IoMapTransfer(Device->Adapter,
49  Device->Mdl,
52  &Device->BufferSize, // is this right?
53  TRUE);
54 
55  DPRINT("VBuffer == 0x%x (really 0x%x?) Bufsize == %u\n", Device->VirtualBuffer, MmGetPhysicalAddress(Device->VirtualBuffer), Device->BufferSize);
56 
57  DPRINT("Writing %u bytes of garbage...\n", Device->BufferSize);
58  // Write some garbage:
59  for (zzz = 0; zzz < Device->BufferSize; zzz ++)
60  *(VirtualAddress + zzz) = (UCHAR) zzz % 200;
61 
62  DPRINT("done\n");
63 
65 
66  return KeepObject;
67 }
68 
69 
71 {
72  DEVICE_DESCRIPTION Desc;
73  ULONG MappedRegs = 0;
75  KEVENT DMAEvent;
76  KIRQL OldIrql;
77 
78  // Buffersize should already be set but it isn't yet !
79  Device->BufferSize = SB_BUFSIZE;
80  DPRINT("Bufsize == %u\n", Device->BufferSize);
81 
82  RtlZeroMemory(&Desc, sizeof(DEVICE_DESCRIPTION));
83 
84  // Init memory!
86  Desc.Master = FALSE; // Slave
87  Desc.ScatterGather = FALSE; // Don't think so anyway
88  Desc.DemandMode = FALSE; // == !SingleModeDMA
89  Desc.AutoInitialize = TRUE; // ?
90  Desc.Dma32BitAddresses = FALSE; // I don't think we can
91  Desc.IgnoreCount = FALSE; // Should be OK
92  Desc.Reserved1 = 0;
93 // Desc.Reserved2 = 0;
94  Desc.BusNumber = 0;
95  Desc.DmaChannel = Device->DMA; // Our channel :)
96  Desc.InterfaceType = Isa; // (BusType == MicroChannel) ? MicroChannel : Isa;
97  Desc.DmaWidth = 0; // hmm... 8 bits?
98  Desc.DmaSpeed = 0; // double hmm (Compatible it should be)
99  Desc.MaximumLength = Device->BufferSize;
100 // Desc.MinimumLength = 0;
101  Desc.DmaPort = 0;
102 
103  DPRINT("Calling HalGetAdapter(), asking for %d mapped regs\n", MappedRegs);
104 
105  Device->Adapter = HalGetAdapter(&Desc, &MappedRegs);
106 
107  DPRINT("Called\n");
108 
109  if (! Device->Adapter)
110  {
111  DPRINT("HalGetAdapter() FAILED\n");
112  return FALSE;
113  }
114 
115  DPRINT("Bufsize == %u\n", Device->BufferSize);
116 
117  if (MappedRegs < BYTES_TO_PAGES(Device->BufferSize))
118  {
119  DPRINT("Could only allocate %u mapping registers\n", MappedRegs);
120 
121  if (MappedRegs == 0)
122  return FALSE;
123 
124  Device->BufferSize = MappedRegs * PAGE_SIZE;
125  DPRINT("Bufsize == %u\n", Device->BufferSize);
126  }
127 
128  DPRINT("Allocated %u mapping registers\n", MappedRegs);
129 
130  // Check if we already have memory here...
131 
132  // Check to make sure we're >= minimum
133 
134  DPRINT("Allocating buffer\n");
135 
136  DPRINT("Bufsize == %u\n", Device->BufferSize);
137 
138  Device->VirtualBuffer = HalAllocateCommonBuffer(Device->Adapter, Device->BufferSize,
139  &Device->Buffer, FALSE);
140 
141  // For some reason BufferSize == 0 here?!
142 // DPRINT("Buffer == 0x%x Bufsize == %u\n", Device->Buffer, Device->BufferSize);
143  DPRINT("Bufsize == %u,", Device->BufferSize);
144  DPRINT("Buffer == 0x%x\n", Device->Buffer);
145 
146  if (! Device->VirtualBuffer)
147  {
148  DPRINT("Could not allocate buffer :(\n");
149  // should try again with smaller buffer...
150  return FALSE;
151  }
152 
153 // DPRINT("Buffer == 0x%x Bufsize == %u\n", Device->Buffer, Device->BufferSize);
154  DPRINT("Bufsize == %u,", Device->BufferSize);
155  DPRINT("Buffer == 0x%x\n", Device->Buffer);
156 
157  DPRINT("Calling IoAllocateMdl()\n");
158  Device->Mdl = IoAllocateMdl(Device->VirtualBuffer, Device->BufferSize, FALSE, FALSE, NULL);
159  DPRINT("Bufsize == %u\n", Device->BufferSize);
160 
161  // IS THIS RIGHT:
162  if (! Device->Mdl)
163  {
164  DPRINT("IoAllocateMdl() FAILED\n");
165  // Free the HAL buffer
166  return FALSE;
167  }
168 
169  DPRINT("VBuffer == 0x%x Mdl == %u Bufsize == %u\n", Device->VirtualBuffer, Device->Mdl, Device->BufferSize);
170 
171  DPRINT("Calling MmBuildMdlForNonPagedPool\n");
173 
174  DPRINT("Bufsize == %u\n", Device->BufferSize);
175 
176  // part II:
178  // Raise IRQL
181  BYTES_TO_PAGES(Device->BufferSize),
182  SoundProgramDMA, &DMAEvent);
183  // Lower IRQL
185  DPRINT("VBuffer == 0x%x Bufsize == %u\n", Device->VirtualBuffer, Device->BufferSize);
187 
188 
189 // if (MappedRegs == 0)
190 // MappedRegs = 2;
191 // else
192 // MappedRegs ++;
193 
194 
195 // Status = IoAllocateAdapterChannel(
196 // Adapter,
197 // DeviceObject,
198 // MappedRegs,
199 // CALLBACK,
200 // DeviceObject); // Context
201  return TRUE;
202 }
203 
enum _IO_ALLOCATION_ACTION IO_ALLOCATION_ACTION
#define IN
Definition: typedefs.h:38
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
#define TRUE
Definition: types.h:120
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
_In_ PIRP _In_ PDEVICE_OBJECT Device
Definition: fatprocs.h:2020
#define MmGetMdlVirtualAddress(_Mdl)
_In_ PIRP Irp
Definition: csq.h:116
PHYSICAL_ADDRESS NTAPI IoMapTransfer(IN PADAPTER_OBJECT AdapterObject, IN PMDL Mdl, IN PVOID MapRegisterBase, IN PVOID CurrentVa, IN OUT PULONG Length, IN BOOLEAN WriteToDevice)
Definition: dma.c:144
unsigned char * PUCHAR
Definition: retypes.h:3
VOID NTAPI MmBuildMdlForNonPagedPool(IN PMDL Mdl)
Definition: mdlsup.c:428
DMA_SPEED DmaSpeed
Definition: iotypes.h:2038
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define DEVICE_DESCRIPTION_VERSION
Definition: iotypes.h:2020
#define SB_BUFSIZE
Definition: sndblst.h:41
UCHAR KIRQL
Definition: env_spec_w32.h:591
BOOLEAN CreateDMA(PDEVICE_OBJECT DeviceObject)
Definition: dma.c:70
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
BOOLEAN IgnoreCount
Definition: iotypes.h:2031
PVOID DeviceExtension
Definition: env_spec_w32.h:418
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
BOOLEAN Dma32BitAddresses
Definition: iotypes.h:2030
NTSTATUS NTAPI IoAllocateAdapterChannel(IN PADAPTER_OBJECT AdapterObject, IN PDEVICE_OBJECT DeviceObject, IN ULONG NumberOfMapRegisters, IN PDRIVER_CONTROL ExecutionRoutine, IN PVOID Context)
Definition: adapter.c:30
BOOLEAN DemandMode
Definition: iotypes.h:2028
BOOLEAN ScatterGather
Definition: iotypes.h:2027
DMA_WIDTH DmaWidth
Definition: iotypes.h:2037
#define BYTES_TO_PAGES(Size)
unsigned char UCHAR
Definition: xmlstorage.h:181
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
#define PAGE_SIZE
Definition: env_spec_w32.h:49
_In_ ULONG _In_ BOOLEAN _Must_inspect_result_ PVOID * VirtualAddress
Definition: ndis.h:3791
PMDL NTAPI IoAllocateMdl(IN PVOID VirtualAddress, IN ULONG Length, IN BOOLEAN SecondaryBuffer, IN BOOLEAN ChargeQuota, IN PIRP Irp)
Definition: iomdl.c:22
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
_Inout_ struct _IRP _In_ PVOID MapRegisterBase
Definition: iotypes.h:189
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
INTERFACE_TYPE InterfaceType
Definition: iotypes.h:2036
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
PHYSICAL_ADDRESS NTAPI MmGetPhysicalAddress(IN PVOID Address)
Definition: stubs.c:682
PVOID NTAPI HalAllocateCommonBuffer(IN PADAPTER_OBJECT AdapterObject, IN ULONG Length, IN PPHYSICAL_ADDRESS LogicalAddress, IN BOOLEAN CacheEnabled)
Definition: dma.c:46
static IO_ALLOCATION_ACTION NTAPI SoundProgramDMA(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID MapRegisterBase, IN PVOID Context)
Definition: dma.c:37
BOOLEAN AutoInitialize
Definition: iotypes.h:2029
PADAPTER_OBJECT NTAPI HalGetAdapter(IN PDEVICE_DESCRIPTION DeviceDescription, OUT PULONG NumberOfMapRegisters)
Definition: dma.c:22