-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasewave.cpp
More file actions
1298 lines (996 loc) · 30.6 KB
/
basewave.cpp
File metadata and controls
1298 lines (996 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <msvad.h>
#include "common.h"
#include "basewave.h"
#pragma warning (disable : 4127)
#define SWEETLY_LOOPBACK_MIN_SIZE (64 * 1024)
SWEETLY_LOOPBACK_BUFFER CMiniportWaveCyclicStreamMSVAD::s_LoopbackBuffer = { 0 };
NTSTATUS
CMiniportWaveCyclicStreamMSVAD::InitializeLoopbackBuffer
(
_In_ ULONG PreferredSize
)
{
if (s_LoopbackBuffer.Initialized)
{
return STATUS_SUCCESS;
}
ULONG bufferSize = PreferredSize;
if (bufferSize < SWEETLY_LOOPBACK_MIN_SIZE)
{
bufferSize = SWEETLY_LOOPBACK_MIN_SIZE;
}
s_LoopbackBuffer.Buffer = (PBYTE)ExAllocatePoolWithTag(NonPagedPool, bufferSize, MSVAD_POOLTAG);
if (!s_LoopbackBuffer.Buffer)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
s_LoopbackBuffer.Size = bufferSize;
s_LoopbackBuffer.WritePosition = 0;
s_LoopbackBuffer.ReadPosition = 0;
s_LoopbackBuffer.ValidBytes = 0;
KeInitializeSpinLock(&s_LoopbackBuffer.Lock);
s_LoopbackBuffer.Initialized = TRUE;
return STATUS_SUCCESS;
}
void
CMiniportWaveCyclicStreamMSVAD::LoopbackWrite
(
_In_reads_bytes_(ByteCount) PBYTE Data,
_In_ ULONG ByteCount
)
{
if (!s_LoopbackBuffer.Initialized || !Data || ByteCount == 0)
{
return;
}
KIRQL oldIrql;
KeAcquireSpinLock(&s_LoopbackBuffer.Lock, &oldIrql);
ULONG bytesToWrite = ByteCount;
if (bytesToWrite > s_LoopbackBuffer.Size)
{
Data += (bytesToWrite - s_LoopbackBuffer.Size);
bytesToWrite = s_LoopbackBuffer.Size;
}
ULONG remaining = bytesToWrite;
while (remaining)
{
ULONG chunk = min(remaining, s_LoopbackBuffer.Size - s_LoopbackBuffer.WritePosition);
RtlCopyMemory(s_LoopbackBuffer.Buffer + s_LoopbackBuffer.WritePosition, Data, chunk);
Data += chunk;
remaining -= chunk;
s_LoopbackBuffer.WritePosition = (s_LoopbackBuffer.WritePosition + chunk) % s_LoopbackBuffer.Size;
}
if (s_LoopbackBuffer.ValidBytes + bytesToWrite > s_LoopbackBuffer.Size)
{
ULONG overflow = (s_LoopbackBuffer.ValidBytes + bytesToWrite) - s_LoopbackBuffer.Size;
s_LoopbackBuffer.ReadPosition = (s_LoopbackBuffer.ReadPosition + overflow) % s_LoopbackBuffer.Size;
s_LoopbackBuffer.ValidBytes = s_LoopbackBuffer.Size;
}
else
{
s_LoopbackBuffer.ValidBytes += bytesToWrite;
}
KeReleaseSpinLock(&s_LoopbackBuffer.Lock, oldIrql);
}
void
CMiniportWaveCyclicStreamMSVAD::LoopbackRead
(
_Out_writes_bytes_(ByteCount) PBYTE Data,
_In_ ULONG ByteCount
)
{
if (!s_LoopbackBuffer.Initialized || !Data || ByteCount == 0)
{
if (Data && ByteCount)
{
RtlZeroMemory(Data, ByteCount);
}
return;
}
KIRQL oldIrql;
KeAcquireSpinLock(&s_LoopbackBuffer.Lock, &oldIrql);
ULONG bytesAvailable = min(ByteCount, s_LoopbackBuffer.ValidBytes);
ULONG remaining = bytesAvailable;
PBYTE dest = Data;
while (remaining)
{
ULONG chunk = min(remaining, s_LoopbackBuffer.Size - s_LoopbackBuffer.ReadPosition);
RtlCopyMemory(dest, s_LoopbackBuffer.Buffer + s_LoopbackBuffer.ReadPosition, chunk);
dest += chunk;
remaining -= chunk;
s_LoopbackBuffer.ReadPosition = (s_LoopbackBuffer.ReadPosition + chunk) % s_LoopbackBuffer.Size;
}
s_LoopbackBuffer.ValidBytes -= bytesAvailable;
KeReleaseSpinLock(&s_LoopbackBuffer.Lock, oldIrql);
if (bytesAvailable < ByteCount)
{
RtlZeroMemory(Data + bytesAvailable, ByteCount - bytesAvailable);
}
}
/*++
Copyright (c) 1997-2000 Microsoft Corporation All Rights Reserved
Module Name:
basewave.cpp
Abstract:
Implementation of wavecyclic miniport.
--*/
//=============================================================================
// CMiniportWaveCyclicMSVAD
//=============================================================================
//=============================================================================
#pragma code_seg("PAGE")
CMiniportWaveCyclicMSVAD::CMiniportWaveCyclicMSVAD
(
void
)
/*++
Routine Description:
Constructor for wavecyclic miniport.
Arguments:
Return Value:
--*/
{
PAGED_CODE();
DPF_ENTER(("[CMiniportWaveCyclicMSVAD::CMiniportWaveCyclicMSVAD]"));
// Initialize members.
//
m_AdapterCommon = NULL;
m_Port = NULL;
m_FilterDescriptor = NULL;
m_NotificationInterval = 0;
m_SamplingFrequency = 0;
m_ServiceGroup = NULL;
m_MaxDmaBufferSize = DMA_BUFFER_SIZE;
m_MaxOutputStreams = 0;
m_MaxInputStreams = 0;
m_MaxTotalStreams = 0;
m_MinChannels = 0;
m_MaxChannelsPcm = 0;
m_MinBitsPerSamplePcm = 0;
m_MaxBitsPerSamplePcm = 0;
m_MinSampleRatePcm = 0;
m_MaxSampleRatePcm = 0;
} // CMiniportWaveCyclicMSVAD
//=============================================================================
CMiniportWaveCyclicMSVAD::~CMiniportWaveCyclicMSVAD
(
void
)
/*++
Routine Description:
Destructor for wavecyclic miniport
Arguments:
Return Value:
--*/
{
PAGED_CODE();
DPF_ENTER(("[CMiniportWaveCyclicMSVAD::~CMiniportWaveCyclicMSVAD]"));
if (m_Port)
{
m_Port->Release();
}
if (m_ServiceGroup)
{
m_ServiceGroup->Release();
}
if (m_AdapterCommon)
{
m_AdapterCommon->Release();
}
} // ~CMiniportWaveCyclicMSVAD
//=============================================================================
STDMETHODIMP
CMiniportWaveCyclicMSVAD::GetDescription
(
_Out_ PPCFILTER_DESCRIPTOR * OutFilterDescriptor
)
/*++
Routine Description:
The GetDescription function gets a pointer to a filter description.
The descriptor is defined in wavtable.h for each MSVAD sample.
Arguments:
OutFilterDescriptor - Pointer to the filter description
Return Value:
NT status code.
--*/
{
PAGED_CODE();
ASSERT(OutFilterDescriptor);
DPF_ENTER(("[CMiniportWaveCyclicMSVAD::GetDescription]"));
*OutFilterDescriptor = m_FilterDescriptor;
return (STATUS_SUCCESS);
} // GetDescription
//=============================================================================
STDMETHODIMP
CMiniportWaveCyclicMSVAD::Init
(
_In_ PUNKNOWN UnknownAdapter_,
_In_ PRESOURCELIST ResourceList_,
_In_ PPORTWAVECYCLIC Port_
)
/*++
Routine Description:
Arguments:
UnknownAdapter_ - pointer to adapter common.
ResourceList_ - resource list. MSVAD does not use resources.
Port_ - pointer to the port
Return Value:
NT status code.
--*/
{
UNREFERENCED_PARAMETER(ResourceList_);
PAGED_CODE();
ASSERT(UnknownAdapter_);
ASSERT(Port_);
DPF_ENTER(("[CMiniportWaveCyclicMSVAD::Init]"));
// AddRef() is required because we are keeping this pointer.
//
m_Port = Port_;
m_Port->AddRef();
// We want the IAdapterCommon interface on the adapter common object,
// which is given to us as a IUnknown. The QueryInterface call gives us
// an AddRefed pointer to the interface we want.
//
NTSTATUS ntStatus =
UnknownAdapter_->QueryInterface
(
IID_IAdapterCommon,
(PVOID *) &m_AdapterCommon
);
if (NT_SUCCESS(ntStatus))
{
KeInitializeMutex(&m_SampleRateSync, 1);
ntStatus = PcNewServiceGroup(&m_ServiceGroup, NULL);
if (NT_SUCCESS(ntStatus))
{
m_AdapterCommon->SetWaveServiceGroup(m_ServiceGroup);
}
}
if (!NT_SUCCESS(ntStatus))
{
// clean up AdapterCommon
//
if (m_AdapterCommon)
{
// clean up the service group
//
if (m_ServiceGroup)
{
m_AdapterCommon->SetWaveServiceGroup(NULL);
m_ServiceGroup->Release();
m_ServiceGroup = NULL;
}
m_AdapterCommon->Release();
m_AdapterCommon = NULL;
}
// release the port
//
m_Port->Release();
m_Port = NULL;
}
return ntStatus;
} // Init
//=============================================================================
NTSTATUS
CMiniportWaveCyclicMSVAD::PropertyHandlerCpuResources
(
IN PPCPROPERTY_REQUEST PropertyRequest
)
/*++
Routine Description:
Processes KSPROPERTY_AUDIO_CPURESOURCES
Arguments:
PropertyRequest - property request structure
Return Value:
NT status code.
--*/
{
PAGED_CODE();
ASSERT(PropertyRequest);
DPF_ENTER(("[CMiniportWaveCyclicMSVAD::PropertyHandlerCpuResources]"));
NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;
if (PropertyRequest->Verb & KSPROPERTY_TYPE_GET)
{
ntStatus = ValidatePropertyParams(PropertyRequest, sizeof(LONG), 0);
if (NT_SUCCESS(ntStatus))
{
*(PLONG(PropertyRequest->Value)) = KSAUDIO_CPU_RESOURCES_NOT_HOST_CPU;
PropertyRequest->ValueSize = sizeof(LONG);
ntStatus = STATUS_SUCCESS;
}
}
else if (PropertyRequest->Verb & KSPROPERTY_TYPE_BASICSUPPORT)
{
ntStatus =
PropertyHandler_BasicSupport
(
PropertyRequest,
KSPROPERTY_TYPE_GET | KSPROPERTY_TYPE_BASICSUPPORT,
VT_I4
);
}
return ntStatus;
} // PropertyHandlerCpuResources
//=============================================================================
NTSTATUS
CMiniportWaveCyclicMSVAD::PropertyHandlerGeneric
(
IN PPCPROPERTY_REQUEST PropertyRequest
)
/*++
Routine Description:
Handles all properties for this miniport.
Arguments:
PropertyRequest - property request structure
Return Value:
NT status code.
--*/
{
PAGED_CODE();
ASSERT(PropertyRequest);
ASSERT(PropertyRequest->PropertyItem);
NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;
switch (PropertyRequest->PropertyItem->Id)
{
case KSPROPERTY_AUDIO_CPU_RESOURCES:
ntStatus = PropertyHandlerCpuResources(PropertyRequest);
break;
default:
DPF(D_TERSE, ("[PropertyHandlerGeneric: Invalid Device Request]"));
ntStatus = STATUS_INVALID_DEVICE_REQUEST;
}
return ntStatus;
} // PropertyHandlerGeneric
//=============================================================================
NTSTATUS
CMiniportWaveCyclicMSVAD::ValidateFormat
(
IN PKSDATAFORMAT pDataFormat
)
/*++
Routine Description:
Validates that the given dataformat is valid.
This version of the driver only supports PCM.
Arguments:
pDataFormat - The dataformat for validation.
Return Value:
NT status code.
--*/
{
PAGED_CODE();
ASSERT(pDataFormat);
DPF_ENTER(("[CMiniportWaveCyclicMSVAD::ValidateFormat]"));
NTSTATUS ntStatus = STATUS_INVALID_PARAMETER;
PWAVEFORMATEX pwfx;
pwfx = GetWaveFormatEx(pDataFormat);
if (pwfx)
{
if (IsEqualGUIDAligned(pDataFormat->SubFormat, KSDATAFORMAT_SUBTYPE_PCM))
{
if (pwfx->wFormatTag == WAVE_FORMAT_PCM)
{
ntStatus = ValidatePcm(pwfx);
}
}
else if (IsEqualGUIDAligned(pDataFormat->SubFormat, KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
{
if (pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
{
ntStatus = ValidateFloat(pwfx);
}
}
else
{
DPF(D_TERSE, ("Unsupported SubFormat"));
}
}
return ntStatus;
} // ValidateFormat
//-----------------------------------------------------------------------------
NTSTATUS
CMiniportWaveCyclicMSVAD::ValidatePcm
(
IN PWAVEFORMATEX pWfx
)
/*++
Routine Description:
Given a waveformatex and format size validates that the format is in device
datarange.
Arguments:
pWfx - wave format structure.
Return Value:
NT status code.
--*/
{
PAGED_CODE();
DPF_ENTER(("CMiniportWaveCyclicMSVAD::ValidatePcm"));
if
(
pWfx &&
(pWfx->cbSize == 0) &&
(pWfx->nChannels >= m_MinChannels) &&
(pWfx->nChannels <= m_MaxChannelsPcm) &&
(pWfx->nSamplesPerSec >= m_MinSampleRatePcm) &&
(pWfx->nSamplesPerSec <= m_MaxSampleRatePcm) &&
(pWfx->wBitsPerSample >= m_MinBitsPerSamplePcm) &&
(pWfx->wBitsPerSample <= m_MaxBitsPerSamplePcm)
)
{
return STATUS_SUCCESS;
}
DPF(D_TERSE, ("Invalid PCM format"));
return STATUS_INVALID_PARAMETER;
} // ValidatePcm
//-----------------------------------------------------------------------------
NTSTATUS
CMiniportWaveCyclicMSVAD::ValidateFloat
(
IN PWAVEFORMATEX pWfx
)
/*++
Routine Description:
Validates IEEE float formats against the device caps.
Arguments:
pWfx - wave format structure.
Return Value:
NT status code.
--*/
{
PAGED_CODE();
DPF_ENTER(("CMiniportWaveCyclicMSVAD::ValidateFloat"));
if
(
pWfx &&
(pWfx->cbSize == 0) &&
(pWfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) &&
(pWfx->nChannels == m_MaxChannelsPcm) &&
(pWfx->nSamplesPerSec == m_MinSampleRatePcm) &&
(pWfx->wBitsPerSample == 32) &&
(pWfx->nBlockAlign == (pWfx->nChannels * (pWfx->wBitsPerSample / 8))) &&
(pWfx->nAvgBytesPerSec == (pWfx->nSamplesPerSec * pWfx->nBlockAlign))
)
{
return STATUS_SUCCESS;
}
DPF(D_TERSE, ("Invalid FLOAT format"));
return STATUS_INVALID_PARAMETER;
} // ValidateFloat
//=============================================================================
// CMiniportWaveCyclicStreamMSVAD
//=============================================================================
CMiniportWaveCyclicStreamMSVAD::CMiniportWaveCyclicStreamMSVAD
(
void
)
{
PAGED_CODE();
m_pMiniport = NULL;
m_fCapture = FALSE;
m_fFormat16Bit = FALSE;
m_usBlockAlign = 0;
m_ksState = KSSTATE_STOP;
m_ulPin = (ULONG)-1;
m_pDpc = NULL;
m_pTimer = NULL;
m_fDmaActive = FALSE;
m_ulDmaPosition = 0;
m_ullElapsedTimeCarryForward = 0;
m_ulByteDisplacementCarryForward = 0;
m_pvDmaBuffer = NULL;
m_ulDmaBufferSize = 0;
m_ulDmaMovementRate = 0;
m_ullDmaTimeStamp = 0;
}
//=============================================================================
CMiniportWaveCyclicStreamMSVAD::~CMiniportWaveCyclicStreamMSVAD
(
void
)
/*++
Routine Description:
Destructor for wavecyclic stream
Arguments:
void
Return Value:
--*/
{
PAGED_CODE();
DPF_ENTER(("[CMiniportWaveCyclicStreamMS::~CMiniportWaveCyclicStreamMS]"));
if (m_pTimer)
{
KeCancelTimer(m_pTimer);
ExFreePoolWithTag(m_pTimer, MSVAD_POOLTAG);
}
// Since we just cancelled the timer, wait for all queued DPCs to complete
// before we free the DPC.
//
KeFlushQueuedDpcs();
if (m_pDpc)
{
ExFreePoolWithTag( m_pDpc, MSVAD_POOLTAG );
}
// Free the DMA buffer
//
FreeBuffer();
} // ~CMiniportWaveCyclicStreamMSVAD
//=============================================================================
#pragma warning (push)
#pragma warning (disable : 26165)
NTSTATUS
CMiniportWaveCyclicStreamMSVAD::Init
(
IN PCMiniportWaveCyclicMSVAD Miniport_,
IN ULONG Pin_,
IN BOOLEAN Capture_,
IN PKSDATAFORMAT DataFormat_
)
/*++
Routine Description:
Initializes the stream object. Allocate a DMA buffer, timer and DPC
Arguments:
Miniport_ - miniport object
Pin_ - pin id
Capture_ - TRUE if this is a capture stream
DataFormat_ - new dataformat
Return Value:
NT status code.
--*/
{
PAGED_CODE();
DPF_ENTER(("[CMiniportWaveCyclicStreamMSVAD::Init]"));
ASSERT(Miniport_);
ASSERT(DataFormat_);
NTSTATUS ntStatus = STATUS_SUCCESS;
PWAVEFORMATEX pWfx;
pWfx = GetWaveFormatEx(DataFormat_);
if (!pWfx)
{
DPF(D_TERSE, ("Invalid DataFormat param in NewStream"));
ntStatus = STATUS_INVALID_PARAMETER;
}
if (NT_SUCCESS(ntStatus))
{
m_pMiniport = Miniport_;
m_ulPin = Pin_;
m_fCapture = Capture_;
m_usBlockAlign = pWfx->nBlockAlign;
m_fFormat16Bit = (pWfx->wBitsPerSample == 16);
m_ksState = KSSTATE_STOP;
m_ulDmaPosition = 0;
m_ullElapsedTimeCarryForward = 0;
m_ulByteDisplacementCarryForward = 0;
m_fDmaActive = FALSE;
m_pDpc = NULL;
m_pTimer = NULL;
m_pvDmaBuffer = NULL;
// If this is not the capture stream, create the output file.
//
if (!m_fCapture)
{
DPF(D_TERSE, ("SaveData %p", &m_SaveData));
ntStatus = m_SaveData.SetDataFormat(DataFormat_);
if (NT_SUCCESS(ntStatus))
{
ntStatus = m_SaveData.Initialize();
}
m_SaveData.Disable(TRUE);
}
}
// Allocate DMA buffer for this stream.
//
if (NT_SUCCESS(ntStatus))
{
ntStatus = AllocateBuffer(m_pMiniport->m_MaxDmaBufferSize, NULL);
}
if (NT_SUCCESS(ntStatus))
{
ntStatus = InitializeLoopbackBuffer(m_pMiniport->m_MaxDmaBufferSize);
}
// Set sample frequency. Note that m_SampleRateSync access should
// be syncronized.
//
if (NT_SUCCESS(ntStatus))
{
ntStatus =
KeWaitForSingleObject
(
&m_pMiniport->m_SampleRateSync,
Executive,
KernelMode,
FALSE,
NULL
);
if (STATUS_SUCCESS == ntStatus)
{
m_pMiniport->m_SamplingFrequency = pWfx->nSamplesPerSec;
KeReleaseMutex(&m_pMiniport->m_SampleRateSync, FALSE);
}
else
{
DPF(D_TERSE, ("[SamplingFrequency Sync failed: %08X]", ntStatus));
}
}
if (NT_SUCCESS(ntStatus))
{
ntStatus = SetFormat(DataFormat_);
}
if (NT_SUCCESS(ntStatus))
{
m_pDpc = (PRKDPC)
ExAllocatePoolWithTag
(
NonPagedPool,
sizeof(KDPC),
MSVAD_POOLTAG
);
if (!m_pDpc)
{
DPF(D_TERSE, ("[Could not allocate memory for DPC]"));
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
}
if (NT_SUCCESS(ntStatus))
{
m_pTimer = (PKTIMER)
ExAllocatePoolWithTag
(
NonPagedPool,
sizeof(KTIMER),
MSVAD_POOLTAG
);
if (!m_pTimer)
{
DPF(D_TERSE, ("[Could not allocate memory for Timer]"));
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
}
if (NT_SUCCESS(ntStatus))
{
KeInitializeDpc(m_pDpc, TimerNotify, m_pMiniport);
KeInitializeTimerEx(m_pTimer, NotificationTimer);
}
return ntStatus;
} // Init
#pragma warning (pop)
#pragma code_seg()
//=============================================================================
// CMiniportWaveCyclicStreamMSVAD IMiniportWaveCyclicStream
//=============================================================================
//=============================================================================
STDMETHODIMP
CMiniportWaveCyclicStreamMSVAD::GetPosition
(
_Out_ PULONG Position
)
/*++
Routine Description:
The GetPosition function gets the current position of the DMA read or write
pointer for the stream. Callers of GetPosition should run at
IRQL <= DISPATCH_LEVEL.
Arguments:
Position - Position of the DMA pointer
Return Value:
NT status code.
--*/
{
if (m_fDmaActive)
{
// Get the current time
//
ULONGLONG CurrentTime = KeQueryInterruptTime();
// Calculate the time elapsed since the last call to GetPosition() or since the
// DMA engine started. Note that the division by 10000 to convert to milliseconds
// may cause us to lose some of the time, so we will carry the remainder forward
// to the next GetPosition() call.
//
ULONG TimeElapsedInMS =
( (ULONG) (CurrentTime - m_ullDmaTimeStamp + m_ullElapsedTimeCarryForward) ) / 10000;
// Carry forward the remainder of this division so we don't fall behind with our position.
//
m_ullElapsedTimeCarryForward =
(CurrentTime - m_ullDmaTimeStamp + m_ullElapsedTimeCarryForward) % 10000;
// Calculate how many bytes in the DMA buffer would have been processed in the elapsed
// time. Note that the division by 1000 to convert to milliseconds may cause us to
// lose some bytes, so we will carry the remainder forward to the next GetPosition() call.
//
ULONG ByteDisplacement =
((m_ulDmaMovementRate * TimeElapsedInMS) + m_ulByteDisplacementCarryForward) / 1000;
// Carry forward the remainder of this division so we don't fall behind with our position.
//
m_ulByteDisplacementCarryForward = (
(m_ulDmaMovementRate * TimeElapsedInMS) + m_ulByteDisplacementCarryForward) % 1000;
// Increment the DMA position by the number of bytes displaced since the last
// call to GetPosition() and ensure we properly wrap at buffer length.
//
m_ulDmaPosition =
(m_ulDmaPosition + ByteDisplacement) % m_ulDmaBufferSize;
// Return the new DMA position
//
*Position = m_ulDmaPosition;
// Update the DMA time stamp for the next call to GetPosition()
//
m_ullDmaTimeStamp = CurrentTime;
}
else
{
// DMA is inactive so just return the current DMA position.
//
*Position = m_ulDmaPosition;
}
return STATUS_SUCCESS;
} // GetPosition
//=============================================================================
STDMETHODIMP
CMiniportWaveCyclicStreamMSVAD::NormalizePhysicalPosition
(
_Inout_ PLONGLONG PhysicalPosition
)
/*++
Routine Description:
Given a physical position based on the actual number of bytes transferred,
NormalizePhysicalPosition converts the position to a time-based value of
100 nanosecond units. Callers of NormalizePhysicalPosition can run at any IRQL.
Arguments:
PhysicalPosition - On entry this variable contains the value to convert.
On return it contains the converted value
Return Value:
NT status code.
--*/
{
ASSERT(PhysicalPosition);
*PhysicalPosition =
( _100NS_UNITS_PER_SECOND / m_usBlockAlign * *PhysicalPosition ) /
m_pMiniport->m_SamplingFrequency;
return STATUS_SUCCESS;
} // NormalizePhysicalPosition
#pragma code_seg("PAGE")
//=============================================================================
STDMETHODIMP_(NTSTATUS)
CMiniportWaveCyclicStreamMSVAD::SetFormat
(
_In_ PKSDATAFORMAT Format
)
/*++
Routine Description:
The SetFormat function changes the format associated with a stream.
Callers of SetFormat should run at IRQL PASSIVE_LEVEL
Arguments:
Format - Pointer to a KSDATAFORMAT structure which indicates the new format
of the stream.
Return Value:
NT status code.
--*/
{
PAGED_CODE();
ASSERT(Format);
DPF_ENTER(("[CMiniportWaveCyclicStreamMSVAD::SetFormat]"));
NTSTATUS ntStatus = STATUS_INVALID_DEVICE_REQUEST;
PWAVEFORMATEX pWfx;
if (m_ksState != KSSTATE_RUN)
{
// MSVAD does not validate the format.
//
pWfx = GetWaveFormatEx(Format);