forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.cpp
1099 lines (873 loc) · 31.8 KB
/
adapter.cpp
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
/*++
Copyright (c) Microsoft Corporation All Rights Reserved
Module Name:
adapter.cpp
Abstract:
Setup and miniport installation. No resources are used by sysvad.
This sample is to demonstrate how to develop a full featured audio miniport driver.
--*/
#pragma warning (disable : 4127)
//
// All the GUIDS for all the miniports end up in this object.
//
#define PUT_GUIDS_HERE
#include <sysvad.h>
#include <ContosoKeywordDetector.h>
#include "IHVPrivatePropertySet.h"
#include "simple.h"
#include "minipairs.h"
#ifdef SYSVAD_BTH_BYPASS
#include "bthhfpminipairs.h"
#endif // SYSVAD_BTH_BYPASS
typedef void (*fnPcDriverUnload) (PDRIVER_OBJECT);
fnPcDriverUnload gPCDriverUnloadRoutine = NULL;
extern "C" DRIVER_UNLOAD DriverUnload;
#ifdef _USE_SingleComponentMultiFxStates
C_ASSERT(sizeof(POHANDLE) == sizeof(PVOID));
//=============================================================================
//
// The number of F-states, the transition latency and residency requirement values
// used here are for illustration purposes only. The driver should use values that
// are appropriate for its device.
//
#define SYSVAD_FSTATE_COUNT 4
#define SYSVAD_F0_LATENCY_IN_MS 0
#define SYSVAD_F0_RESIDENCY_IN_SEC 0
#define SYSVAD_F1_LATENCY_IN_MS 200
#define SYSVAD_F1_RESIDENCY_IN_SEC 3
#define SYSVAD_F2_LATENCY_IN_MS 400
#define SYSVAD_F2_RESIDENCY_IN_SEC 6
#define SYSVAD_F3_LATENCY_IN_MS 800
#define SYSVAD_F3_RESIDENCY_IN_SEC 12
#define SYSVAD_DEEPEST_FSTATE_LATENCY_IN_MS SYSVAD_F3_LATENCY_IN_MS
#define SYSVAD_DEEPEST_FSTATE_RESIDENCY_IN_SEC SYSVAD_F3_RESIDENCY_IN_SEC
//-----------------------------------------------------------------------------
// PoFx - Single Component - Multi Fx States support.
//-----------------------------------------------------------------------------
PO_FX_COMPONENT_IDLE_STATE_CALLBACK PcPowerFxComponentIdleStateCallback;
PO_FX_COMPONENT_ACTIVE_CONDITION_CALLBACK PcPowerFxComponentActiveConditionCallback;
PO_FX_COMPONENT_IDLE_CONDITION_CALLBACK PcPowerFxComponentIdleConditionCallback;
PO_FX_POWER_CONTROL_CALLBACK PcPowerFxPowerControlCallback;
EVT_PC_POST_PO_FX_REGISTER_DEVICE PcPowerFxRegisterDevice;
EVT_PC_PRE_PO_FX_UNREGISTER_DEVICE PcPowerFxUnregisterDevice;
#pragma code_seg("PAGE")
_Function_class_(EVT_PC_POST_PO_FX_REGISTER_DEVICE)
_IRQL_requires_same_
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
PcPowerFxRegisterDevice(
_In_ PVOID PoFxDeviceContext,
_In_ POHANDLE PoHandle
)
{
PDEVICE_OBJECT DeviceObject = (PDEVICE_OBJECT)PoFxDeviceContext;
PortClassDeviceContext* pExtension = static_cast<PortClassDeviceContext*>(DeviceObject->DeviceExtension);
PAGED_CODE();
DPF(D_VERBOSE, ("PcPowerFxRegisterDevice Context %p, PoHandle %p", PoFxDeviceContext, PoHandle));
pExtension->m_poHandle = PoHandle;
//
// Set latency and residency hints so that the power framework chooses lower
// powered F-states when we are idle.
// The values used here are for illustration purposes only. The driver
// should use values that are appropriate for its device.
//
PoFxSetComponentLatency(
PoHandle,
0, // Component
(WDF_ABS_TIMEOUT_IN_MS(SYSVAD_DEEPEST_FSTATE_LATENCY_IN_MS) + 1)
);
PoFxSetComponentResidency(
PoHandle,
0, // Component
(WDF_ABS_TIMEOUT_IN_SEC(SYSVAD_DEEPEST_FSTATE_RESIDENCY_IN_SEC) + 1)
);
return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
_Function_class_(EVT_PC_PRE_PO_FX_UNREGISTER_DEVICE)
_IRQL_requires_same_
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
PcPowerFxUnregisterDevice(
_In_ PVOID PoFxDeviceContext,
_In_ POHANDLE PoHandle
)
{
PDEVICE_OBJECT DeviceObject = (PDEVICE_OBJECT)PoFxDeviceContext;
PortClassDeviceContext* pExtension = static_cast<PortClassDeviceContext*>(DeviceObject->DeviceExtension);
UNREFERENCED_PARAMETER(PoHandle);
PAGED_CODE();
DPF(D_VERBOSE, ("PcPowerFxUnregisterDevice Context %p, PoHandle %p", PoFxDeviceContext, PoHandle));
//
// Driver must not use the PoHandle after this call.
//
ASSERT(pExtension->m_poHandle == PoHandle);
pExtension->m_poHandle = NULL;
}
#pragma code_seg()
__drv_functionClass(PO_FX_COMPONENT_IDLE_STATE_CALLBACK)
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
PcPowerFxComponentIdleStateCallback(
_In_ PVOID Context,
_In_ ULONG Component,
_In_ ULONG State
)
{
PDEVICE_OBJECT DeviceObject = (PDEVICE_OBJECT)Context;
PortClassDeviceContext* pExtension = static_cast<PortClassDeviceContext*>(DeviceObject->DeviceExtension);
UNREFERENCED_PARAMETER(State);
DPF(D_VERBOSE, ("PcPowerFxComponentIdleStateCallback Context %p, Component %d, State %d",
Context, Component, State));
PoFxCompleteIdleState(pExtension->m_poHandle, Component);
}
#pragma code_seg()
__drv_functionClass(PO_FX_COMPONENT_ACTIVE_CONDITION_CALLBACK)
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
PcPowerFxComponentActiveConditionCallback(
_In_ PVOID Context,
_In_ ULONG Component
)
{
UNREFERENCED_PARAMETER(Context);
UNREFERENCED_PARAMETER(Component);
DPF(D_VERBOSE, ("PcPowerFxComponentActiveConditionCallback Context %p, Component %d",
Context, Component));
}
#pragma code_seg()
__drv_functionClass(PO_FX_COMPONENT_IDLE_CONDITION_CALLBACK)
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
PcPowerFxComponentIdleConditionCallback(
_In_ PVOID Context,
_In_ ULONG Component
)
{
PDEVICE_OBJECT DeviceObject = (PDEVICE_OBJECT)Context;
PortClassDeviceContext* pExtension = static_cast<PortClassDeviceContext*>(DeviceObject->DeviceExtension);
DPF(D_VERBOSE, ("PcPowerFxComponentIdleConditionCallback Context %p, Component %d",
Context, Component));
PoFxCompleteIdleCondition(pExtension->m_poHandle, Component);
}
#pragma code_seg()
__drv_functionClass(PO_FX_POWER_CONTROL_CALLBACK)
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PcPowerFxPowerControlCallback(
_In_ PVOID DeviceContext,
_In_ LPCGUID PowerControlCode,
_In_reads_bytes_opt_(InBufferSize) PVOID InBuffer,
_In_ SIZE_T InBufferSize,
_Out_writes_bytes_opt_(OutBufferSize) PVOID OutBuffer,
_In_ SIZE_T OutBufferSize,
_Out_opt_ PSIZE_T BytesReturned
)
{
UNREFERENCED_PARAMETER(DeviceContext);
UNREFERENCED_PARAMETER(PowerControlCode);
UNREFERENCED_PARAMETER(InBuffer);
UNREFERENCED_PARAMETER(InBufferSize);
UNREFERENCED_PARAMETER(OutBuffer);
UNREFERENCED_PARAMETER(OutBufferSize);
UNREFERENCED_PARAMETER(BytesReturned);
DPF(D_VERBOSE, ("PcPowerFxPowerControlCallback Context %p", DeviceContext));
return STATUS_SUCCESS;
}
#endif // _USE_SingleComponentMultiFxStates
//-----------------------------------------------------------------------------
// Referenced forward.
//-----------------------------------------------------------------------------
DRIVER_ADD_DEVICE AddDevice;
NTSTATUS
StartDevice
(
_In_ PDEVICE_OBJECT,
_In_ PIRP,
_In_ PRESOURCELIST
);
_Dispatch_type_(IRP_MJ_PNP)
DRIVER_DISPATCH PnpHandler;
//
// Rendering streams are saved to a file by default. Use the registry value
// DoNotCreateDataFiles (DWORD) > 0 to override this default.
//
DWORD g_DoNotCreateDataFiles = 0; // default is off.
DWORD g_DisableToneGenerator = 0; // default is to generate tones.
#ifdef SYSVAD_BTH_BYPASS
//
// This driver listens for arrival/removal of the bth sco bypass interfaces by
// default. Use the registry value DisableBthScoBypass (DWORD) > 0 to override
// this default.
//
DWORD g_DisableBthScoBypass = 0; // default is SCO bypass enabled.
#endif // SYSVAD_BTH_BYPASS
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
//=============================================================================
#pragma code_seg("PAGE")
extern "C"
void DriverUnload
(
_In_ PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Our driver unload routine. This just frees the WDF driver object.
Arguments:
DriverObject - pointer to the driver object
Environment:
PASSIVE_LEVEL
--*/
{
PAGED_CODE();
DPF(D_TERSE, ("[DriverUnload]"));
if (DriverObject == NULL)
{
goto Done;
}
//
// Invoke first the port unload.
//
if (gPCDriverUnloadRoutine != NULL)
{
gPCDriverUnloadRoutine(DriverObject);
}
//
// Unload WDF driver object.
//
if (WdfGetDriver() != NULL)
{
WdfDriverMiniportUnload(WdfGetDriver());
}
Done:
return;
}
//=============================================================================
#pragma code_seg("INIT")
__drv_requiresIRQL(PASSIVE_LEVEL)
NTSTATUS
GetRegistrySettings(
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Initialize Driver Framework settings from the driver
specific registry settings under
\REGISTRY\MACHINE\SYSTEM\ControlSetxxx\Services\<driver>\Parameters
Arguments:
RegistryPath - Registry path passed to DriverEntry
Returns:
NTSTATUS - SUCCESS if able to configure the framework
--*/
{
NTSTATUS ntStatus;
UNICODE_STRING parametersPath;
RTL_QUERY_REGISTRY_TABLE paramTable[] = {
// QueryRoutine Flags Name EntryContext DefaultType DefaultData DefaultLength
{ NULL, RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_TYPECHECK, L"DoNotCreateDataFiles", &g_DoNotCreateDataFiles, (REG_DWORD << RTL_QUERY_REGISTRY_TYPECHECK_SHIFT) | REG_DWORD, &g_DoNotCreateDataFiles, sizeof(ULONG)},
{ NULL, RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_TYPECHECK, L"DisableToneGenerator", &g_DisableToneGenerator, (REG_DWORD << RTL_QUERY_REGISTRY_TYPECHECK_SHIFT) | REG_DWORD, &g_DisableToneGenerator, sizeof(ULONG)},
#ifdef SYSVAD_BTH_BYPASS
{ NULL, RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_TYPECHECK, L"DisableBthScoBypass", &g_DisableBthScoBypass, (REG_DWORD << RTL_QUERY_REGISTRY_TYPECHECK_SHIFT) | REG_DWORD, &g_DisableBthScoBypass, sizeof(ULONG)},
#endif
{ NULL, 0, NULL, NULL, 0, NULL, 0}
};
DPF(D_TERSE, ("[GetRegistrySettings]"));
PAGED_CODE();
RtlInitUnicodeString(¶metersPath, NULL);
parametersPath.MaximumLength =
RegistryPath->Length + sizeof(L"\\Parameters") + sizeof(WCHAR);
parametersPath.Buffer = (PWCH) ExAllocatePoolWithTag(PagedPool, parametersPath.MaximumLength, MINADAPTER_POOLTAG);
if (parametersPath.Buffer == NULL)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlZeroMemory(parametersPath.Buffer, parametersPath.MaximumLength);
RtlAppendUnicodeToString(¶metersPath, RegistryPath->Buffer);
RtlAppendUnicodeToString(¶metersPath, L"\\Parameters");
ntStatus = RtlQueryRegistryValues(
RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL,
parametersPath.Buffer,
¶mTable[0],
NULL,
NULL
);
if (!NT_SUCCESS(ntStatus))
{
DPF(D_VERBOSE, ("RtlQueryRegistryValues failed, using default values, 0x%x", ntStatus));
//
// Don't return error because we will operate with default values.
//
}
//
// Dump settings.
//
DPF(D_VERBOSE, ("DoNotCreateDataFiles: %u", g_DoNotCreateDataFiles));
DPF(D_VERBOSE, ("DisableToneGenerator: %u", g_DisableToneGenerator));
#ifdef SYSVAD_BTH_BYPASS
DPF(D_VERBOSE, ("DisableBthScoBypass: %u", g_DisableBthScoBypass));
#endif // SYSVAD_BTH_BYPASS
//
// Cleanup.
//
ExFreePool(parametersPath.Buffer);
return STATUS_SUCCESS;
}
#pragma code_seg("INIT")
extern "C" DRIVER_INITIALIZE DriverEntry;
extern "C" NTSTATUS
DriverEntry
(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPathName
)
{
/*++
Routine Description:
Installable driver initialization entry point.
This entry point is called directly by the I/O system.
All audio adapter drivers can use this code without change.
Arguments:
DriverObject - pointer to the driver object
RegistryPath - pointer to a unicode string representing the path,
to driver-specific key in the registry.
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise.
--*/
NTSTATUS ntStatus;
WDF_DRIVER_CONFIG config;
DPF(D_TERSE, ("[DriverEntry]"));
//
// Get registry configuration.
//
ntStatus = GetRegistrySettings(RegistryPathName);
IF_FAILED_ACTION_JUMP(
ntStatus,
DPF(D_ERROR, ("Registry Configuration error 0x%x", ntStatus)),
Done);
WDF_DRIVER_CONFIG_INIT(&config, WDF_NO_EVENT_CALLBACK);
//
// Set WdfDriverInitNoDispatchOverride flag to tell the framework
// not to provide dispatch routines for the driver. In other words,
// the framework must not intercept IRPs that the I/O manager has
// directed to the driver. In this case, they will be handled by Audio
// port driver.
//
config.DriverInitFlags |= WdfDriverInitNoDispatchOverride;
config.DriverPoolTag = MINADAPTER_POOLTAG;
ntStatus = WdfDriverCreate(DriverObject,
RegistryPathName,
WDF_NO_OBJECT_ATTRIBUTES,
&config,
WDF_NO_HANDLE);
IF_FAILED_ACTION_JUMP(
ntStatus,
DPF(D_ERROR, ("WdfDriverCreate failed, 0x%x", ntStatus)),
Done);
//
// Tell the class driver to initialize the driver.
//
ntStatus = PcInitializeAdapterDriver(DriverObject,
RegistryPathName,
(PDRIVER_ADD_DEVICE)AddDevice);
IF_FAILED_ACTION_JUMP(
ntStatus,
DPF(D_ERROR, ("PcInitializeAdapterDriver failed, 0x%x", ntStatus)),
Done);
//
// To intercept stop/remove/surprise-remove.
//
DriverObject->MajorFunction[IRP_MJ_PNP] = PnpHandler;
//
// Hook the port class unload function
//
gPCDriverUnloadRoutine = DriverObject->DriverUnload;
DriverObject->DriverUnload = DriverUnload;
//
// All done.
//
ntStatus = STATUS_SUCCESS;
Done:
if (!NT_SUCCESS(ntStatus))
{
if (WdfGetDriver() != NULL)
{
WdfDriverMiniportUnload(WdfGetDriver());
}
}
return ntStatus;
} // DriverEntry
#pragma code_seg()
// disable prefast warning 28152 because
// DO_DEVICE_INITIALIZING is cleared in PcAddAdapterDevice
#pragma warning(disable:28152)
#pragma code_seg("PAGE")
//=============================================================================
NTSTATUS AddDevice
(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PDEVICE_OBJECT PhysicalDeviceObject
)
/*++
Routine Description:
The Plug & Play subsystem is handing us a brand new PDO, for which we
(by means of INF registration) have been asked to provide a driver.
We need to determine if we need to be in the driver stack for the device.
Create a function device object to attach to the stack
Initialize that device object
Return status success.
All audio adapter drivers can use this code without change.
Arguments:
DriverObject - pointer to a driver object
PhysicalDeviceObject - pointer to a device object created by the
underlying bus driver.
Return Value:
NT status code.
--*/
{
PAGED_CODE();
NTSTATUS ntStatus;
ULONG maxObjects;
DPF(D_TERSE, ("[AddDevice]"));
maxObjects = g_MaxMiniports;
#ifdef SYSVAD_BTH_BYPASS
//
// Allow three (3) Bluetooth hands-free profile devices.
//
maxObjects += g_MaxBthHfpMiniports * 3;
#endif // SYSVAD_BTH_BYPASS
// Tell the class driver to add the device.
//
ntStatus =
PcAddAdapterDevice
(
DriverObject,
PhysicalDeviceObject,
PCPFNSTARTDEVICE(StartDevice),
maxObjects,
0
);
return ntStatus;
} // AddDevice
#pragma code_seg()
NTSTATUS
_IRQL_requires_max_(DISPATCH_LEVEL)
PowerControlCallback
(
_In_ LPCGUID PowerControlCode,
_In_opt_ PVOID InBuffer,
_In_ SIZE_T InBufferSize,
_Out_writes_bytes_to_(OutBufferSize, *BytesReturned) PVOID OutBuffer,
_In_ SIZE_T OutBufferSize,
_Out_opt_ PSIZE_T BytesReturned,
_In_opt_ PVOID Context
)
{
UNREFERENCED_PARAMETER(PowerControlCode);
UNREFERENCED_PARAMETER(BytesReturned);
UNREFERENCED_PARAMETER(InBuffer);
UNREFERENCED_PARAMETER(OutBuffer);
UNREFERENCED_PARAMETER(OutBufferSize);
UNREFERENCED_PARAMETER(InBufferSize);
UNREFERENCED_PARAMETER(Context);
return STATUS_NOT_IMPLEMENTED;
}
#pragma code_seg("PAGE")
NTSTATUS
InstallEndpointRenderFilters(
_In_ PDEVICE_OBJECT _pDeviceObject,
_In_ PIRP _pIrp,
_In_ PADAPTERCOMMON _pAdapterCommon,
_In_ PENDPOINT_MINIPAIR _pAeMiniports
)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PUNKNOWN unknownTopology = NULL;
PUNKNOWN unknownWave = NULL;
PPORTCLSETWHELPER pPortClsEtwHelper = NULL;
#ifdef _USE_IPortClsRuntimePower
PPORTCLSRUNTIMEPOWER pPortClsRuntimePower = NULL;
#endif // _USE_IPortClsRuntimePower
PPORTCLSStreamResourceManager pPortClsResMgr = NULL;
PPORTCLSStreamResourceManager2 pPortClsResMgr2 = NULL;
PAGED_CODE();
UNREFERENCED_PARAMETER(_pDeviceObject);
ntStatus = _pAdapterCommon->InstallEndpointFilters(
_pIrp,
_pAeMiniports,
NULL,
&unknownTopology,
&unknownWave,
NULL, NULL);
if (unknownWave) // IID_IPortClsEtwHelper and IID_IPortClsRuntimePower interfaces are only exposed on the WaveRT port.
{
ntStatus = unknownWave->QueryInterface (IID_IPortClsEtwHelper, (PVOID *)&pPortClsEtwHelper);
if (NT_SUCCESS(ntStatus))
{
_pAdapterCommon->SetEtwHelper(pPortClsEtwHelper);
ASSERT(pPortClsEtwHelper != NULL);
pPortClsEtwHelper->Release();
}
#ifdef _USE_IPortClsRuntimePower
// Let's get the runtime power interface on PortCls.
ntStatus = unknownWave->QueryInterface(IID_IPortClsRuntimePower, (PVOID *)&pPortClsRuntimePower);
if (NT_SUCCESS(ntStatus))
{
// This interface would typically be stashed away for later use. Instead,
// let's just send an empty control with GUID_NULL.
NTSTATUS ntStatusTest =
pPortClsRuntimePower->SendPowerControl
(
_pDeviceObject,
&GUID_NULL,
NULL,
0,
NULL,
0,
NULL
);
if (NT_SUCCESS(ntStatusTest) || STATUS_NOT_IMPLEMENTED == ntStatusTest || STATUS_NOT_SUPPORTED == ntStatusTest)
{
ntStatus = pPortClsRuntimePower->RegisterPowerControlCallback(_pDeviceObject, &PowerControlCallback, NULL);
if (NT_SUCCESS(ntStatus))
{
ntStatus = pPortClsRuntimePower->UnregisterPowerControlCallback(_pDeviceObject);
}
}
else
{
ntStatus = ntStatusTest;
}
pPortClsRuntimePower->Release();
}
#endif // _USE_IPortClsRuntimePower
//
// Test: add and remove current thread as streaming audio resource.
// In a real driver you should only add interrupts and driver-owned threads
// (i.e., do NOT add the current thread as streaming resource).
//
// testing IPortClsStreamResourceManager:
ntStatus = unknownWave->QueryInterface(IID_IPortClsStreamResourceManager, (PVOID *)&pPortClsResMgr);
if (NT_SUCCESS(ntStatus))
{
PCSTREAMRESOURCE_DESCRIPTOR res;
PCSTREAMRESOURCE hRes = NULL;
PDEVICE_OBJECT pdo = NULL;
PcGetPhysicalDeviceObject(_pDeviceObject, &pdo);
PCSTREAMRESOURCE_DESCRIPTOR_INIT(&res);
res.Pdo = pdo;
res.Type = ePcStreamResourceThread;
res.Resource.Thread = PsGetCurrentThread();
NTSTATUS ntStatusTest = pPortClsResMgr->AddStreamResource(NULL, &res, &hRes);
if (NT_SUCCESS(ntStatusTest))
{
pPortClsResMgr->RemoveStreamResource(hRes);
hRes = NULL;
}
pPortClsResMgr->Release();
pPortClsResMgr = NULL;
}
// testing IPortClsStreamResourceManager2:
ntStatus = unknownWave->QueryInterface(IID_IPortClsStreamResourceManager2, (PVOID *)&pPortClsResMgr2);
if (NT_SUCCESS(ntStatus))
{
PCSTREAMRESOURCE_DESCRIPTOR res;
PCSTREAMRESOURCE hRes = NULL;
PDEVICE_OBJECT pdo = NULL;
PcGetPhysicalDeviceObject(_pDeviceObject, &pdo);
PCSTREAMRESOURCE_DESCRIPTOR_INIT(&res);
res.Pdo = pdo;
res.Type = ePcStreamResourceThread;
res.Resource.Thread = PsGetCurrentThread();
NTSTATUS ntStatusTest = pPortClsResMgr2->AddStreamResource2(pdo, NULL, &res, &hRes);
if (NT_SUCCESS(ntStatusTest))
{
pPortClsResMgr2->RemoveStreamResource(hRes);
hRes = NULL;
}
pPortClsResMgr2->Release();
pPortClsResMgr2 = NULL;
}
}
SAFE_RELEASE(unknownTopology);
SAFE_RELEASE(unknownWave);
return ntStatus;
}
#pragma code_seg("PAGE")
NTSTATUS
InstallAllRenderFilters(
_In_ PDEVICE_OBJECT _pDeviceObject,
_In_ PIRP _pIrp,
_In_ PADAPTERCOMMON _pAdapterCommon
)
{
NTSTATUS ntStatus;
PENDPOINT_MINIPAIR* ppAeMiniports = g_RenderEndpoints;
PAGED_CODE();
for(ULONG i = 0; i < g_cRenderEndpoints; ++i, ++ppAeMiniports)
{
ntStatus = InstallEndpointRenderFilters(_pDeviceObject, _pIrp, _pAdapterCommon, *ppAeMiniports);
IF_FAILED_JUMP(ntStatus, Exit);
}
ntStatus = STATUS_SUCCESS;
Exit:
return ntStatus;
}
#pragma code_seg("PAGE")
NTSTATUS
InstallEndpointCaptureFilters(
_In_ PDEVICE_OBJECT _pDeviceObject,
_In_ PIRP _pIrp,
_In_ PADAPTERCOMMON _pAdapterCommon,
_In_ PENDPOINT_MINIPAIR _pAeMiniports
)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PAGED_CODE();
UNREFERENCED_PARAMETER(_pDeviceObject);
ntStatus = _pAdapterCommon->InstallEndpointFilters(
_pIrp,
_pAeMiniports,
NULL,
NULL,
NULL,
NULL, NULL);
return ntStatus;
}
#pragma code_seg("PAGE")
NTSTATUS
InstallAllCaptureFilters(
_In_ PDEVICE_OBJECT _pDeviceObject,
_In_ PIRP _pIrp,
_In_ PADAPTERCOMMON _pAdapterCommon
)
{
NTSTATUS ntStatus;
PENDPOINT_MINIPAIR* ppAeMiniports = g_CaptureEndpoints;
PAGED_CODE();
for(ULONG i = 0; i < g_cCaptureEndpoints; ++i, ++ppAeMiniports)
{
ntStatus = InstallEndpointCaptureFilters(_pDeviceObject, _pIrp, _pAdapterCommon, *ppAeMiniports);
IF_FAILED_JUMP(ntStatus, Exit);
}
ntStatus = STATUS_SUCCESS;
Exit:
return ntStatus;
}
#ifdef _USE_SingleComponentMultiFxStates
//=============================================================================
#pragma code_seg("PAGE")
NTSTATUS
UseSingleComponentMultiFxStates(
_In_ PDEVICE_OBJECT DeviceObject
)
{
NTSTATUS status;
PC_POWER_FRAMEWORK_SETTINGS poFxSettings;
PO_FX_COMPONENT component;
PO_FX_COMPONENT_IDLE_STATE idleStates[SYSVAD_FSTATE_COUNT];
//
// Note that we initialize the 'idleStates' array below based on the
// assumption that SYSVAD_FSTATE_COUNT is 4.
// If we increase the value of SYSVAD_FSTATE_COUNT, we need to initialize those
// additional F-states below. If we decrease the value of SYSVAD_FSTATE_COUNT,
// we need to remove the corresponding initializations below.
//
C_ASSERT(SYSVAD_FSTATE_COUNT == 4);
PAGED_CODE();
//
// Initialization
//
RtlZeroMemory(&component, sizeof(component));
RtlZeroMemory(idleStates, sizeof(idleStates));
//
// F0
//
idleStates[0].TransitionLatency = WDF_ABS_TIMEOUT_IN_MS(SYSVAD_F0_LATENCY_IN_MS);
idleStates[0].ResidencyRequirement = WDF_ABS_TIMEOUT_IN_SEC(SYSVAD_F0_RESIDENCY_IN_SEC);
idleStates[0].NominalPower = 0;
//
// F1
//
idleStates[1].TransitionLatency = WDF_ABS_TIMEOUT_IN_MS(SYSVAD_F1_LATENCY_IN_MS);
idleStates[1].ResidencyRequirement = WDF_ABS_TIMEOUT_IN_SEC(SYSVAD_F1_RESIDENCY_IN_SEC);
idleStates[1].NominalPower = 0;
//
// F2
//
idleStates[2].TransitionLatency = WDF_ABS_TIMEOUT_IN_MS(SYSVAD_F2_LATENCY_IN_MS);
idleStates[2].ResidencyRequirement = WDF_ABS_TIMEOUT_IN_SEC(SYSVAD_F2_RESIDENCY_IN_SEC);
idleStates[2].NominalPower = 0;
//
// F3
//
idleStates[3].TransitionLatency = WDF_ABS_TIMEOUT_IN_MS(SYSVAD_F3_LATENCY_IN_MS);
idleStates[3].ResidencyRequirement = WDF_ABS_TIMEOUT_IN_SEC(SYSVAD_F3_RESIDENCY_IN_SEC);
idleStates[3].NominalPower = 0;
//
// Component 0 (the only component)
//
component.IdleStateCount = SYSVAD_FSTATE_COUNT;
component.IdleStates = idleStates;
PC_POWER_FRAMEWORK_SETTINGS_INIT(&poFxSettings);
poFxSettings.EvtPcPostPoFxRegisterDevice = PcPowerFxRegisterDevice;
poFxSettings.EvtPcPrePoFxUnregisterDevice = PcPowerFxUnregisterDevice;
poFxSettings.ComponentIdleStateCallback = PcPowerFxComponentIdleStateCallback;
poFxSettings.ComponentActiveConditionCallback = PcPowerFxComponentActiveConditionCallback;
poFxSettings.ComponentIdleConditionCallback = PcPowerFxComponentIdleConditionCallback;
poFxSettings.PowerControlCallback = PcPowerFxPowerControlCallback;
poFxSettings.Component = &component;
poFxSettings.PoFxDeviceContext = (PVOID) DeviceObject;
status = PcAssignPowerFrameworkSettings(DeviceObject, &poFxSettings);
if (!NT_SUCCESS(status)) {
DPF(D_ERROR, ("PcAssignPowerFrameworkSettings failed with status 0x%x", status));
}
return status;
}
#endif // _USE_SingleComponentMultiFxStates
//=============================================================================
#pragma code_seg("PAGE")
NTSTATUS
StartDevice
(
_In_ PDEVICE_OBJECT DeviceObject,
_In_ PIRP Irp,
_In_ PRESOURCELIST ResourceList
)
{
/*++
Routine Description:
This function is called by the operating system when the device is
started.
It is responsible for starting the miniports. This code is specific to
the adapter because it calls out miniports for functions that are specific
to the adapter.
Arguments:
DeviceObject - pointer to the driver object
Irp - pointer to the irp
ResourceList - pointer to the resource list assigned by PnP manager
Return Value:
NT status code.
--*/
UNREFERENCED_PARAMETER(ResourceList);
PAGED_CODE();
ASSERT(DeviceObject);
ASSERT(Irp);
ASSERT(ResourceList);
NTSTATUS ntStatus = STATUS_SUCCESS;
PADAPTERCOMMON pAdapterCommon = NULL;
PUNKNOWN pUnknownCommon = NULL;
PortClassDeviceContext* pExtension = static_cast<PortClassDeviceContext*>(DeviceObject->DeviceExtension);
DPF_ENTER(("[StartDevice]"));
//
// create a new adapter common object
//
ntStatus = NewAdapterCommon(
&pUnknownCommon,
IID_IAdapterCommon,
NULL,
NonPagedPoolNx
);
IF_FAILED_JUMP(ntStatus, Exit);
ntStatus = pUnknownCommon->QueryInterface( IID_IAdapterCommon,(PVOID *) &pAdapterCommon);
IF_FAILED_JUMP(ntStatus, Exit);
ntStatus = pAdapterCommon->Init(DeviceObject);
IF_FAILED_JUMP(ntStatus, Exit);
//
// register with PortCls for power-management services
ntStatus = PcRegisterAdapterPowerManagement( PUNKNOWN(pAdapterCommon), DeviceObject);
IF_FAILED_JUMP(ntStatus, Exit);
//
// Install wave+topology filters for render devices
//
ntStatus = InstallAllRenderFilters(DeviceObject, Irp, pAdapterCommon);
IF_FAILED_JUMP(ntStatus, Exit);
//
// Install wave+topology filters for capture devices
//
ntStatus = InstallAllCaptureFilters(DeviceObject, Irp, pAdapterCommon);
IF_FAILED_JUMP(ntStatus, Exit);
#ifdef SYSVAD_BTH_BYPASS
if (!g_DisableBthScoBypass)
{
//
// Init infrastructure for Bluetooth HFP - SCO Bypass devices.
//
ntStatus = pAdapterCommon->InitBthScoBypass();
IF_FAILED_JUMP(ntStatus, Exit);