- Update to r53061
[reactos.git] / drivers / multimedia / audio / sndblst.old / dma.c
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
9 BOOLEAN CheckDMA(PDEVICE_EXTENSION Device)
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
37 static IO_ALLOCATION_ACTION NTAPI SoundProgramDMA(
38 IN PDEVICE_OBJECT DeviceObject,
39 IN PIRP Irp,
40 IN PVOID MapRegisterBase,
41 IN PVOID Context)
42 {
43 PDEVICE_EXTENSION Device = DeviceObject->DeviceExtension;
44 ULONG zzz;
45 PUCHAR VirtualAddress = (PUCHAR) MmGetMdlVirtualAddress(Device->Mdl);
46
47 DPRINT("IoMapTransfer\n");
48 IoMapTransfer(Device->Adapter,
49 Device->Mdl,
50 MapRegisterBase,
51 (PUCHAR) MmGetMdlVirtualAddress(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
64 KeSetEvent(Context, 0, FALSE);
65
66 return KeepObject;
67 }
68
69
70 BOOLEAN CreateDMA(PDEVICE_OBJECT DeviceObject)
71 {
72 DEVICE_DESCRIPTION Desc;
73 ULONG MappedRegs = 0;
74 PDEVICE_EXTENSION Device = DeviceObject->DeviceExtension;
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!
85 Desc.Version = DEVICE_DESCRIPTION_VERSION;
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");
172 MmBuildMdlForNonPagedPool(Device->Mdl);
173
174 DPRINT("Bufsize == %u\n", Device->BufferSize);
175
176 // part II:
177 KeInitializeEvent(&DMAEvent, SynchronizationEvent, FALSE);
178 // Raise IRQL
179 KeRaiseIrql(DISPATCH_LEVEL,&OldIrql);
180 IoAllocateAdapterChannel(Device->Adapter, DeviceObject,
181 BYTES_TO_PAGES(Device->BufferSize),
182 SoundProgramDMA, &DMAEvent);
183 // Lower IRQL
184 KeLowerIrql(OldIrql);
185 DPRINT("VBuffer == 0x%x Bufsize == %u\n", Device->VirtualBuffer, Device->BufferSize);
186 KeWaitForSingleObject(&DMAEvent, Executive, KernelMode, FALSE, NULL);
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