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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
|
/*++
Copyright (c) Microsoft Corporation All Rights Reserved
Module Name:
Pdo.c
Abstract:
This module create a PDO and handles plug & play calls for the child device (PDO).
Environment:
kernel mode only
--*/
#include "driver.h"
#include "pdo.tmh"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, PdoCreate)
#pragma alloc_text(PAGE, PdoDevD0Exit)
#pragma alloc_text(PAGE, PdoDevD0Entry)
#pragma alloc_text(PAGE, PdoDevPrepareHardware)
#pragma alloc_text(PAGE, PdoDevReleaseHardware)
#pragma alloc_text(PAGE, PdoResetHandler)
#ifdef DYNAMIC_ENUM
#pragma alloc_text(PAGE, PdoCreateDynamic)
#pragma alloc_text(PAGE, PdoResetHandlerDynamic)
#endif // DYNAMIC_ENUM
#endif // ALLOC_PRAGMA
#define MAX_ID_LEN 80
#ifdef DYNAMIC_ENUM
_Use_decl_annotations_
NTSTATUS
PdoResetHandlerDynamic(
PVOID _InterfaceContext,
DEVICE_RESET_TYPE _ResetType,
ULONG _Flags,
PVOID _ResetParameters
)
/*++
Routine Description:
This is the reset handler invoked by a driver that queries our GUID_DEVICE_RESET_INTERFACE_STANDARD interface.
This is the version used if PDOs are created using WDF dynamic enumeration.
Arguments:
_InterfaceContext - This is expected to be a PPDO_EXTENSION.
_ResetType - This is expected to be PlatformLevelDeviceReset because that is all we support.
_Flags - Unused, because no flags are defined currently.
_ResetParameters - Unused, because it is only used for FunctionLevelDeviceReset.
Return Value:
NTSTATUS code.
--*/
{
PPDO_EXTENSION PdoExtension = (PPDO_EXTENSION) _InterfaceContext;
PAGED_CODE();
UNREFERENCED_PARAMETER(_Flags);
UNREFERENCED_PARAMETER(_ResetParameters);
if (_ResetType != PlatformLevelDeviceReset) {
return STATUS_NOT_SUPPORTED;
}
DeviceDoPLDR(PdoExtension->FdoExtension->WdfDevice);
//
// The bus driver is expected to cause the device to be surprise removed as part of PLDR. For
// many hardware buses, this happens naturally, but for a software bus like us, we need to do it
// ourselves. WDF dynamic enumeration makes this convenient because it already supports
// GUID_REENUMERATE_SELF_INTERFACE_STANDARD, so we can simply call WdfDeviceSetFailed to
// re-enumerate the device.
//
WdfDeviceSetFailed((WDFDEVICE) WdfObjectContextGetObject(PdoExtension), WdfDeviceFailedAttemptRestart);
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS
PdoCreateDynamic(
WDFDEVICE _Device,
PWDFDEVICE_INIT _DeviceInit,
PWCHAR _HardwareIds,
ULONG _SerialNo
)
/*++
Routine Description:
This routine creates and initialize a PDO.
Arguments:
Return Value:
NT Status code.
--*/
{
NTSTATUS Status;
PPDO_EXTENSION PdoExtension = NULL;
WDFDEVICE ChildDevice = NULL;
WDF_OBJECT_ATTRIBUTES PdoAttributes;
WDF_DEVICE_PNP_CAPABILITIES PnpCaps;
WDF_DEVICE_POWER_CAPABILITIES PowerCaps;
DECLARE_CONST_UNICODE_STRING( CompatId, BT_PDO_COMPATIBLE_IDS);
DECLARE_CONST_UNICODE_STRING( DeviceLocation, L"Serial HCI Bus - Bluetooth Function");
DECLARE_UNICODE_STRING_SIZE( Buffer, MAX_ID_LEN);
DECLARE_UNICODE_STRING_SIZE( DeviceId, MAX_ID_LEN);
WDF_IO_QUEUE_CONFIG QueueConfig;
WDFQUEUE Queue;
WDF_QUERY_INTERFACE_CONFIG DeviceResetInterfaceConfig;
DEVICE_RESET_INTERFACE_STANDARD ResetInterface;
PAGED_CODE();
UNREFERENCED_PARAMETER(_Device);
KdPrint(("Entered PdoCreateDynamic\n"));
//
// Set DeviceType
//
WdfDeviceInitSetDeviceType(_DeviceInit, FILE_DEVICE_BUS_EXTENDER);
//
// Provide DeviceID, HardwareIDs, CompatibleIDs and InstanceId
//
RtlInitUnicodeString(&DeviceId, _HardwareIds);
Status = WdfPdoInitAssignDeviceID(_DeviceInit, &DeviceId);
if (!NT_SUCCESS(Status)) {
return Status;
}
//
// NOTE: same string is used to initialize hardware id too
//
Status = WdfPdoInitAddHardwareID(_DeviceInit, &DeviceId);
if (!NT_SUCCESS(Status)) {
return Status;
}
Status = WdfPdoInitAddCompatibleID(_DeviceInit, &CompatId );
if (!NT_SUCCESS(Status)) {
return Status;
}
Status = RtlUnicodeStringPrintf(&Buffer, L"%02d", _SerialNo);
if (!NT_SUCCESS(Status)) {
return Status;
}
Status = WdfPdoInitAssignInstanceID(_DeviceInit, &Buffer);
if (!NT_SUCCESS(Status)) {
return Status;
}
//
// Provide a description about the device. This text is usually read from
// the device. In the case of USB device, this text comes from the string
// descriptor. This text is displayed momentarily by the PnP manager while
// it's looking for a matching INF. If it finds one, it uses the Device
// Description from the INF file or the friendly name created by
// coinstallers to display in the device manager. FriendlyName takes
// precedence over the DeviceDesc from the INF file.
//
Status = RtlUnicodeStringPrintf( &Buffer,
L"SerialHciBus_%02d",
_SerialNo );
if (!NT_SUCCESS(Status)) {
return Status;
}
//
// You can call WdfPdoInitAddDeviceText multiple times, adding device
// text for multiple locales. When the system displays the text, it
// chooses the text that matches the current locale, if available.
// Otherwise it will use the string for the default locale.
// The driver can specify the driver's default locale by calling
// WdfPdoInitSetDefaultLocale.
//
Status = WdfPdoInitAddDeviceText(_DeviceInit,
&Buffer,
&DeviceLocation,
0x409 );
if (!NT_SUCCESS(Status)) {
return Status;
}
WdfPdoInitSetDefaultLocale(_DeviceInit, 0x409);
//
// Initialize the attributes to specify the size of PDO device extension.
// All the state information private to the PDO will be tracked here.
//
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&PdoAttributes, PDO_EXTENSION);
//
// Allow to forward requests to its FDO of this bus driver by using
// WdfRequestForwardToParentDeviceIoQueue()in the DeviceIoControl callback.
//
WdfPdoInitAllowForwardingRequestToParent(_DeviceInit);
//
// Create a framework device object to represent PDO of this bus driver. In response
// to this call, framework creates a WDM deviceobject.
//
Status = WdfDeviceCreate(&_DeviceInit,
&PdoAttributes,
&ChildDevice);
if (!NT_SUCCESS(Status)) {
return Status;
}
//
// Note: Once the device is created successfully, framework frees the
// _DeviceInit memory and sets the _DeviceInit to NULL. So don't
// call any WdfDeviceInit functions after that.
//
//
// Initalize the PDO extension
//
PdoExtension = PdoGetExtension(ChildDevice);
RtlZeroMemory(PdoExtension, sizeof(PDO_EXTENSION));
PdoExtension->FdoExtension = FdoGetExtension(_Device);
PdoExtension->SerialNo = _SerialNo;
PdoExtension->ResetRecoveryType = SUPPORTED_RESET_RECOVERY_TYPE;
//
// Set some properties for the child device.
//
WDF_DEVICE_PNP_CAPABILITIES_INIT(&PnpCaps); // Zeros this structure (note: WdfFalse is 0)
//
// Bus driver sets this value to WdfFalse for this embedded device, which cannot
// be physically removed; its FDO must not set/override this.
//
PnpCaps.Removable = WdfFalse;
//
// Bus driver sets this value to WdfTrue. FDO can override this value (when this irp is on its
// way up) if it determines that this device cannot be safely surprise (not orderly) removed
// without data loss.
//
PnpCaps.SurpriseRemovalOK = WdfTrue;
PnpCaps.Address = _SerialNo;
PnpCaps.UINumber = _SerialNo;
WdfDeviceSetPnpCapabilities(ChildDevice, &PnpCaps);
WDF_DEVICE_POWER_CAPABILITIES_INIT(&PowerCaps);
PowerCaps.DeviceD1 = WdfFalse;
PowerCaps.DeviceD2 = WdfTrue;
PowerCaps.WakeFromD0 = WdfFalse;
PowerCaps.WakeFromD1 = WdfFalse;
PowerCaps.WakeFromD2 = WdfTrue;
PowerCaps.WakeFromD3 = WdfTrue;
PowerCaps.DeviceWake = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemWorking] = PowerDeviceD0;
PowerCaps.DeviceState[PowerSystemSleeping1] = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
PowerCaps.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
WdfDeviceSetPowerCapabilities(ChildDevice, &PowerCaps);
//
// Configure a default queue so that requests that are not
// configure-forwarded using WdfDeviceConfigureRequestDispatching to goto
// other queues get dispatched here.
//
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&QueueConfig, WdfIoQueueDispatchParallel);
//
// Cannot be power managed queue (dispatch only at D0) as
// BthMini issues BthX DDI to get version and capabilities
// before enter D0. A deadlock occurs if this is power managed.
//
QueueConfig.PowerManaged = WdfFalse;
QueueConfig.EvtIoDeviceControl = PdoIoQuDeviceControl;
Status = WdfIoQueueCreate(ChildDevice,
&QueueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&Queue);
DoTrace(LEVEL_INFO, TFLAG_PNP, (" WdfIoQueueCreate (%!STATUS!)", Status));
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
if (PdoExtension->ResetRecoveryType == ResetRecoveryTypeParentResetInterface) {
//
// Instruct WDF to simply forward a request for the reset interface to the parent stack, so
// that it reaches the ACPI bus/filter driver. That way when the reset interface is invoked,
// it is the parent stack that gets reset and re-enumerated.
//
WDF_QUERY_INTERFACE_CONFIG_INIT(&DeviceResetInterfaceConfig, NULL, &GUID_DEVICE_RESET_INTERFACE_STANDARD, NULL);
DeviceResetInterfaceConfig.SendQueryToParentStack = TRUE;
Status = WdfDeviceAddQueryInterface(ChildDevice, &DeviceResetInterfaceConfig);
if (!NT_SUCCESS(Status)) {
DoTrace(LEVEL_ERROR, TFLAG_PNP, ("WdfDeviceAddQueryInterface failed, %!STATUS!", Status));
goto Cleanup;
}
} else if (PdoExtension->ResetRecoveryType == ResetRecoveryTypeDriverImplemented) {
RtlZeroMemory(&ResetInterface, sizeof(ResetInterface));
ResetInterface.Size = sizeof(ResetInterface);
ResetInterface.Version = DEVICE_RESET_INTERFACE_VERSION;
ResetInterface.Context = PdoExtension;
//
// Since this interface is expected to be used only by drivers in the same stack, reference
// counting is not required. If there is an expectation that drivers may query for the
// interface using a remote I/O target to this stack (unusual for this interface), reference
// counting must be implemented.
//
ResetInterface.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
ResetInterface.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
ResetInterface.SupportedResetTypes = (1 << PlatformLevelDeviceReset);
ResetInterface.DeviceReset = PdoResetHandlerDynamic;
WDF_QUERY_INTERFACE_CONFIG_INIT(&DeviceResetInterfaceConfig, (PINTERFACE) &ResetInterface, &GUID_DEVICE_RESET_INTERFACE_STANDARD, NULL);
Status = WdfDeviceAddQueryInterface(ChildDevice, &DeviceResetInterfaceConfig);
if (!NT_SUCCESS(Status))
{
DoTrace(LEVEL_ERROR, TFLAG_PNP, ("WdfDeviceAddQueryInterface failed, %!STATUS!", Status));
goto Cleanup;
}
}
Cleanup:
//
// Call WdfDeviceInitFree if you encounter an error before the
// device is created. Once the device is created, framework
// NULLs the DeviceInit value.
//
if (!NT_SUCCESS(Status)) {
DoTrace(LEVEL_ERROR, TFLAG_PNP, (" -PdoCreateDynamic: exit %!STATUS!", Status));
if(ChildDevice) {
WdfObjectDelete(ChildDevice);
}
}
else
{
DoTrace(LEVEL_INFO, TFLAG_PNP, (" -PdoCreateDynamic: exit %!STATUS!", Status));
}
return Status;
}
#endif
_Use_decl_annotations_
NTSTATUS
PdoResetHandler(
PVOID _InterfaceContext,
DEVICE_RESET_TYPE _ResetType,
ULONG _Flags,
PVOID _ResetParameters
)
/*++
Routine Description:
This is the reset handler invoked by a driver that queries our GUID_DEVICE_RESET_INTERFACE_STANDARD interface.
This is the version used if PDOs are created using WDF static enumeration.
Arguments:
_InterfaceContext - This is expected to be a PPDO_EXTENSION.
_ResetType - This is expected to be PlatformLevelDeviceReset because that is all we support.
_Flags - Unused, because no flags are defined currently.
_ResetParameters - Unused, because it is only used for FunctionLevelDeviceReset.
Return Value:
NTSTATUS code.
--*/
{
PPDO_EXTENSION PdoExtension = (PPDO_EXTENSION) _InterfaceContext;
WDFDEVICE Fdo = PdoExtension->FdoExtension->WdfDevice;
PAGED_CODE();
UNREFERENCED_PARAMETER(_Flags);
UNREFERENCED_PARAMETER(_ResetParameters);
if (_ResetType != PlatformLevelDeviceReset) {
return STATUS_NOT_SUPPORTED;
}
DeviceDoPLDR(Fdo);
//
// The bus driver is expected to cause the device to be surprise removed as part of PLDR. For
// many hardware buses, this happens naturally, but for a software bus like us, we need to do it
// ourselves.
//
FdoRemoveOneChildDevice(Fdo, BLUETOOTH_FUNC_IDS);
FdoCreateOneChildDevice(Fdo, BT_PDO_HARDWARE_IDS, BLUETOOTH_FUNC_IDS);
return STATUS_SUCCESS;
}
NTSTATUS
PdoCreate(
_In_ WDFDEVICE _Device,
_In_ PWSTR _HardwareIds,
_In_ ULONG _SerialNo
)
/*++
Routine Description:
This routine creates and initialize a PDO to service a Bluetooth function.
Arguments:
_Device - A framework device object
_HardwareIds - a hardware ID for this device
-SerialNo - serial number of the child DO
Return Value:
NT Status code.
--*/
{
NTSTATUS Status;
PWDFDEVICE_INIT DeviceInit = NULL;
WDF_PNPPOWER_EVENT_CALLBACKS PnpPowerCallbacks;
PPDO_EXTENSION PdoExtension = NULL;
WDFDEVICE ChildDevice = NULL;
WDF_OBJECT_ATTRIBUTES Attributes;
WDF_DEVICE_PNP_CAPABILITIES PnpCaps;
WDF_DEVICE_POWER_CAPABILITIES PowerCaps;
UNICODE_STRING StaticString = {0};
UNICODE_STRING DeviceId;
DECLARE_UNICODE_STRING_SIZE( Buffer, MAX_ID_LEN);
UNICODE_STRING ContainerID = {0};
WDF_PDO_EVENT_CALLBACKS Callbacks;
WDF_IO_QUEUE_CONFIG QueueConfig;
WDFQUEUE Queue;
WDF_QUERY_INTERFACE_CONFIG DeviceResetInterfaceConfig;
DEVICE_RESET_INTERFACE_STANDARD ResetInterface;
DoTrace(LEVEL_INFO, TFLAG_PNP, (" +PdoCreate: HWID(%S), compatID(%S)", _HardwareIds, BT_PDO_COMPATIBLE_IDS));
PAGED_CODE();
//
// Allocate a WDFDEVICE_INIT structure and set the properties
// so that we can create a device object for the child.
//
DeviceInit = WdfPdoInitAllocate(_Device);
if (DeviceInit == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Cleanup;
}
//
// Set DeviceType
//
WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_BUS_EXTENDER);
//
// Provide DeviceID, HardwareIDs, CompatibleIDs and InstanceId
//
RtlInitUnicodeString(&DeviceId, _HardwareIds);
Status = WdfPdoInitAssignDeviceID(DeviceInit, &DeviceId);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
//
// Note: same string is used to initialize hardware id
//
Status = WdfPdoInitAddHardwareID(DeviceInit, &DeviceId);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
RtlInitUnicodeString(&StaticString, BT_PDO_COMPATIBLE_IDS);
Status = WdfPdoInitAddCompatibleID(DeviceInit, &StaticString);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
Status = RtlUnicodeStringPrintf(&Buffer, L"%02d", _SerialNo);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
Status = WdfPdoInitAssignInstanceID(DeviceInit, &Buffer);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
//
// Assign the containerID for an internally connected device
//
Status = RtlStringFromGUID(&GUID_CONTAINERID_INTERNALLY_CONNECTED_DEVICE, &ContainerID);
if (!NT_SUCCESS(Status)) {
DoTrace(LEVEL_ERROR, TFLAG_PNP, ("Failed to generate the ContainerID, %!STATUS!", Status));;
goto Cleanup;
}
Status = WdfPdoInitAssignContainerID(DeviceInit, &ContainerID);
if (!NT_SUCCESS(Status)) {
DoTrace(LEVEL_ERROR, TFLAG_PNP, ("Failed to assign the ContainerID, %!STATUS!", Status));
goto Cleanup;
}
//
// Provide a description about the device. This text is usually read from
// the device. This text is displayed momentarily by the PnP manager while
// it's looking for a matching INF. If it finds one, it uses the Device
// Description from the INF file or the friendly name created by
// coinstallers to display in the device manager. FriendlyName takes
// precedence over the DeviceDesc from the INF file.
//
Status = RtlUnicodeStringPrintf(&Buffer, L"SerialHciBus_%02d", _SerialNo );
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
//
// You can call WdfPdoInitAddDeviceText multiple times, adding device
// text for multiple locales. When the system displays the text, it
// chooses the text that matches the current locale, if available.
// Otherwise it will use the string for the default locale.
// The driver can specify the driver's default locale by calling
// WdfPdoInitSetDefaultLocale.
//
RtlInitUnicodeString(&StaticString, BT_PDO_DEVICE_LOCATION);
Status = WdfPdoInitAddDeviceText(DeviceInit,
&Buffer,
&StaticString,
0x409);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
WdfPdoInitSetDefaultLocale(DeviceInit, 0x409);
//
// Initialize the attributes to specify the size of PDO device extension.
// All the state information private to the PDO will be tracked here.
//
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&Attributes, PDO_EXTENSION);
//
// Set power callbacks to handle idle/active transition of the Bluetooth function
//
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&PnpPowerCallbacks);
//
// Register PnP callback
//
PnpPowerCallbacks.EvtDevicePrepareHardware = PdoDevPrepareHardware;
PnpPowerCallbacks.EvtDeviceReleaseHardware = PdoDevReleaseHardware;
//
// Register for Power callback
//
PnpPowerCallbacks.EvtDeviceD0Entry = PdoDevD0Entry;
PnpPowerCallbacks.EvtDeviceD0Exit = PdoDevD0Exit;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit,
&PnpPowerCallbacks);
//
// Allow to forward requests to its FDO of this bus driver by using
// WdfRequestForwardToParentDeviceIoQueue()in the DeviceIoControl callback.
//
WdfPdoInitAllowForwardingRequestToParent(DeviceInit);
//
// Register to handle bus level power management (arm for wake?)
//
WDF_PDO_EVENT_CALLBACKS_INIT(&Callbacks);
//
// Arm the device for wake:
//
// When the device is powered down, the framework calls the bus driver's
// EvtDeviceEnableWakeAtBus callback function at the beginning of the shutdown
// sequence, while the child device is still in the D0 state. In this callback
// function, the bus driver must do whatever is required at the bus level to
// enable the wake signal.
//
Callbacks.EvtDeviceEnableWakeAtBus = PdoDevEnableWakeAtBus;
//
// Disarm the device for wake:
//
// If the child device triggered a wake signal, the system and the framework
// return the device to D0. The framework calls the bus driver's
// EvtDeviceDisableWakeAtBus callback function during startup of the child
// device. In this callback function, the bus driver should do whatever is
// required at the bus level to disable the wake signal, so that the device
// can no longer trigger it. Thus, EvtDeviceDisableWakeAtBus reverses the
// actions of EvtDeviceEnableWakeAtBus.
//
Callbacks.EvtDeviceDisableWakeAtBus = PdoDevDisableWakeAtBus;
WdfPdoInitSetEventCallbacks(DeviceInit, &Callbacks);
//
// Create a framework device object to represent PDO of this bus driver. In response
// to this call, framework creates a WDM deviceobject.
//
Status = WdfDeviceCreate(&DeviceInit,
&Attributes,
&ChildDevice);
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
//
// Note: Once the device is created successfully, framework frees the
// DeviceInit memory and sets the DeviceInit to NULL. So don't
// call any WdfDeviceInit functions after that.
//
//
// Initalize the PDO extension
//
PdoExtension = PdoGetExtension(ChildDevice);
RtlZeroMemory(PdoExtension, sizeof(PDO_EXTENSION));
PdoExtension->FdoExtension = FdoGetExtension(_Device);
PdoExtension->SerialNo = _SerialNo;
PdoExtension->ResetRecoveryType = SUPPORTED_RESET_RECOVERY_TYPE;
//
// Set PnP and Power capabilities for this child device.
//
WDF_DEVICE_PNP_CAPABILITIES_INIT(&PnpCaps); // Zeros this structure (note: WdfFalse is 0)
//
// Bus driver sets this value to WdfFalse for this embedded device, which cannot
// be physically removed; its FDO must not set/override this.
//
PnpCaps.Removable = WdfFalse;
//
// Bus driver sets this value to WdfTrue. FDO can override this value (when this irp is on its
// way up) if it determines that this device cannot be safely surprise (not orderly) removed
// without data loss.
//
PnpCaps.SurpriseRemovalOK = WdfTrue;
PnpCaps.Address = _SerialNo;
PnpCaps.UINumber = _SerialNo;
WdfDeviceSetPnpCapabilities(ChildDevice, &PnpCaps);
WDF_DEVICE_POWER_CAPABILITIES_INIT(&PowerCaps);
PowerCaps.DeviceD1 = WdfFalse;
PowerCaps.DeviceD2 = WdfTrue;
PowerCaps.WakeFromD0 = WdfFalse;
PowerCaps.WakeFromD1 = WdfFalse;
PowerCaps.WakeFromD2 = WdfTrue;
PowerCaps.WakeFromD3 = WdfTrue;
PowerCaps.DeviceState[PowerSystemWorking] = PowerDeviceD0;
PowerCaps.DeviceState[PowerSystemSleeping1] = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemSleeping3] = PowerDeviceD2;
PowerCaps.DeviceState[PowerSystemHibernate] = PowerDeviceD3;
PowerCaps.DeviceState[PowerSystemShutdown] = PowerDeviceD3;
PowerCaps.DeviceWake = PowerDeviceD2; // Lowest-powered Dx state to send wake signal to system
WdfDeviceSetPowerCapabilities(ChildDevice, &PowerCaps);
//
// Configure a default queue so that requests that are not
// configure-fowarded using WdfDeviceConfigureRequestDispatching to goto
// other queues get dispatched here.
//
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&QueueConfig, WdfIoQueueDispatchParallel);
//
// Cannot be power managed queue (dispatch only at D0) as
// BthMini issues BthX DDI to get version and capabilities
// before enter D0. A deadlock occurs if this is power managed.
//
QueueConfig.PowerManaged = WdfFalse;
QueueConfig.EvtIoDeviceControl = PdoIoQuDeviceControl;
Status = WdfIoQueueCreate(ChildDevice,
&QueueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&Queue);
DoTrace(LEVEL_INFO, TFLAG_PNP, (" WdfIoQueueCreate (%!STATUS!)", Status));
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
if (PdoExtension->ResetRecoveryType == ResetRecoveryTypeParentResetInterface) {
//
// Instruct WDF to simply forward a request for the reset interface to the parent stack, so
// that it reaches the ACPI bus/filter driver. That way when the reset interface is invoked,
// it is the parent stack that gets reset and re-enumerated.
//
WDF_QUERY_INTERFACE_CONFIG_INIT(&DeviceResetInterfaceConfig, NULL, &GUID_DEVICE_RESET_INTERFACE_STANDARD, NULL);
DeviceResetInterfaceConfig.SendQueryToParentStack = TRUE;
Status = WdfDeviceAddQueryInterface(ChildDevice, &DeviceResetInterfaceConfig);
if (!NT_SUCCESS(Status)) {
DoTrace(LEVEL_ERROR, TFLAG_PNP, ("WdfDeviceAddQueryInterface failed, %!STATUS!", Status));
goto Cleanup;
}
} else if (PdoExtension->ResetRecoveryType == ResetRecoveryTypeDriverImplemented) {
RtlZeroMemory(&ResetInterface, sizeof(ResetInterface));
ResetInterface.Size = sizeof(ResetInterface);
ResetInterface.Version = DEVICE_RESET_INTERFACE_VERSION;
ResetInterface.Context = PdoExtension;
//
// Since this interface is expected to be used only by drivers in the same stack, reference
// counting is not required. If there is an expectation that drivers may query for the
// interface using a remote I/O target to this stack (unusual for this interface), reference
// counting must be implemented.
//
ResetInterface.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
ResetInterface.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
ResetInterface.SupportedResetTypes = (1 << PlatformLevelDeviceReset);
ResetInterface.DeviceReset = PdoResetHandler;
WDF_QUERY_INTERFACE_CONFIG_INIT(&DeviceResetInterfaceConfig, (PINTERFACE) &ResetInterface, &GUID_DEVICE_RESET_INTERFACE_STANDARD, NULL);
Status = WdfDeviceAddQueryInterface(ChildDevice, &DeviceResetInterfaceConfig);
if (!NT_SUCCESS(Status))
{
DoTrace(LEVEL_ERROR, TFLAG_PNP, ("WdfDeviceAddQueryInterface failed, %!STATUS!", Status));
goto Cleanup;
}
}
//
// Add this device to the FDO's collection of children.
// After the child device is added to the static collection successfully,
// driver must call WdfPdoMarkMissing to get the device deleted. It
// shouldn't delete the child device directly by calling WdfObjectDelete.
//
Status = WdfFdoAddStaticChild(_Device, ChildDevice);
DoTrace(LEVEL_INFO, TFLAG_PNP, (" WdfFdoAddStaticChild (%!STATUS!)", Status));
if (!NT_SUCCESS(Status)) {
goto Cleanup;
}
Cleanup:
//
// Call WdfDeviceInitFree if you encounter an error before the
// device is created. Once the device is created, framework
// NULLs the DeviceInit value.
//
if (!NT_SUCCESS(Status)) {
DoTrace(LEVEL_ERROR, TFLAG_PNP, (" -PdoCreate: exit %!STATUS!", Status));
if (DeviceInit != NULL) {
WdfDeviceInitFree(DeviceInit);
}
if(ChildDevice) {
WdfObjectDelete(ChildDevice);
}
}
else
{
DoTrace(LEVEL_INFO, TFLAG_PNP, (" -PdoCreate: exit %!STATUS!", Status));
}
if (NULL != ContainerID.Buffer) {
RtlFreeUnicodeString(&ContainerID);
}
return Status;
}
NTSTATUS
PdoDevPrepareHardware(
_In_ WDFDEVICE _Device,
_In_ WDFCMRESLIST _ResourcesRaw,
_In_ WDFCMRESLIST _ResourcesTranslated
)
/*++
Routine Description:
This PnP CB function take a refernce of its parent so it will not enter DxState while in S0Idle.
Arguments:
_Device - WDF Device object
_ResourcesRaw - (Not referenced)
_ResourcesTranslated - (Not referenced)
Return Value:
NTSTATUS
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
WDFDEVICE ParentDevice;
PAGED_CODE();
UNREFERENCED_PARAMETER(_ResourcesRaw);
UNREFERENCED_PARAMETER(_ResourcesTranslated);
DoTrace(LEVEL_INFO, TFLAG_PNP,("+PdoDevPrepareHardware"));
ParentDevice = WdfPdoGetParent(_Device);
//
// Take a reference to avoid FDO to enter DxState in IdleS0
//
Status = WdfDeviceStopIdle(ParentDevice, FALSE);
DoTrace(LEVEL_INFO, TFLAG_PNP, ("WdfDeviceStopIdle %!STATUS!", Status));
//
// Any failure Status code should be invesigated to ensure that the reference count is balanced.
//
NT_ASSERT(NT_SUCCESS(Status));
DoTrace(LEVEL_INFO, TFLAG_PNP, ("-PdoDevPrepareHardware"));
return Status;
}
NTSTATUS
PdoDevReleaseHardware(
_In_ WDFDEVICE _Device,
_In_ WDFCMRESLIST _ResourcesTranslated
)
/*++
Routine Description:
This PnP CB function release a refcount of its parent so it can enter DxState in S0Idle.
Arguments:
_Device - WDF Device object
_ResourcesTranslated - (Not referenced)
Return Value:
NTSTATUS
--*/
{
WDFDEVICE ParentDevice;
PAGED_CODE();
UNREFERENCED_PARAMETER(_ResourcesTranslated);
DoTrace(LEVEL_INFO, TFLAG_PNP,("+PdoDevReleaseHardware"));
ParentDevice = WdfPdoGetParent(_Device);
//
// Release a reference to allow FDO to enter DxState in IdleS0
//
WdfDeviceResumeIdle(ParentDevice);
return STATUS_SUCCESS;
}
NTSTATUS
PdoDevD0Entry(
_In_ WDFDEVICE _Device,
_In_ WDF_POWER_DEVICE_STATE _PreviousState
)
/*++
Routine Description:
This PnPPower CB function is invoked after device has entered D0 (working) state.
Arguments:
_Device - WDF Device object
PreviousState - Previous device power state
Return Value:
NTSTATUS
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE();
UNREFERENCED_PARAMETER(_Device);
UNREFERENCED_PARAMETER(_PreviousState);
DoTrace(LEVEL_INFO, TFLAG_UART, ("+PdoDevD0Entry"));
//
// Can bring the Bluetooth function back to active state
//
DoTrace(LEVEL_INFO, TFLAG_UART, ("-PdoDevD0Entry %!STATUS!", Status));
return Status;
}
NTSTATUS
PdoDevD0Exit(
_In_ WDFDEVICE _Device,
_In_ WDF_POWER_DEVICE_STATE _TargetState
)
/*++
Routine Description:
This PnP CB function is invoked when device has exited D0 (working) state.
Arguments:
_Device - WDF Device object
_TargetState - Next device power state that it is about to enter
Return Value:
NTSTATUS
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER(_Device);
UNREFERENCED_PARAMETER(_TargetState);
DoTrace(LEVEL_INFO, TFLAG_UART, ("+PdoDevD0Exit: D0 -> D%d", _TargetState-WdfPowerDeviceD0));
//
// Can prepare the Bluetooth function to enter lower power device state
//
DoTrace(LEVEL_INFO, TFLAG_UART, ("-PdoDevD0Exit"));
return STATUS_SUCCESS;
}
VOID
PdoDevDisableWakeAtBus(
_In_ WDFDEVICE _Device
)
/*++
Routine Description:
This framework callback routine performs bus-level operations that disable
the ability of one of the bus's devices to trigger a wake-up signal.
Arguments:
_Device - Framework device object
Return Value:
VOID
--*/
{
//
// Do not mark this function pageable to potentially reduce power up time.
//
DoTrace(LEVEL_INFO, TFLAG_POWER,("<==(D)== PdoDevDisableWakeAtBus"));
//
// Device specific implementation to disarm for wake
//
DeviceDisableWakeControl(_Device);
}
NTSTATUS
PdoDevEnableWakeAtBus(
_In_ WDFDEVICE _Device,
_In_ SYSTEM_POWER_STATE _PowerState
)
/*++
Routine Description:
This framework callback routine performs bus-level operations that enable
one of the bus's devices to trigger a wake-up signal.
Arguments:
_Device - Framework device object
_PowerState - identifies the system power state that the system or device will wake from.
Return Value:
NTSTATUS
--*/
{
//
// Do not mark this function pageable to potentially reduce power up time.
//
DoTrace(LEVEL_INFO, TFLAG_POWER,("==(E)==> PdoDevEnableWakeAtBus from %S",
_PowerState == PowerSystemWorking ? L"S0" : L"Sx"));
//
// Device specific implementation to arm for wake
//
return DeviceEnableWakeControl(_Device, _PowerState);
}
VOID
PdoIoQuDeviceControl(
_In_ WDFQUEUE _Queue,
_In_ WDFREQUEST _Request,
_In_ size_t _OutputBufferLength,
_In_ size_t _InputBufferLength,
_In_ ULONG _IoControlCode
)
/*++
Routine Description:
This routine is the dispatch routine for device control requests. This routine can be invoke
at the DISPATCH level from BthPort/mini.
Arguments:
_Queue - Handle to the framework queue object that is associated
with the I/O request.
_Request - Handle to a framework request object.
_OutputBufferLength - length of the request's output buffer,
if an output buffer is available.
_InputBufferLength - length of the request's input buffer,
if an input buffer is available.
_IoControlCode - the driver-defined or system-defined I/O control code
(IOCTL) that is associated with the request.
Return Value:
VOID
--*/
{
WDFDEVICE Device = NULL;
NTSTATUS Status = STATUS_INVALID_PARAMETER;
WDF_REQUEST_FORWARD_OPTIONS ForwardOptions;
WDFDEVICE ParentDevice;
ULONG ControlCode = (_IoControlCode & 0x00003ffc) >> 2;
UNREFERENCED_PARAMETER(_OutputBufferLength);
UNREFERENCED_PARAMETER(_InputBufferLength);
DoTrace(LEVEL_INFO, TFLAG_IOCTL,("+IoDeviceControl - InBufLen:%d, OutBufLen:%d",
(ULONG) _InputBufferLength, (ULONG) _OutputBufferLength));
switch (_IoControlCode) {
case IOCTL_BTHX_GET_VERSION:
case IOCTL_BTHX_SET_VERSION:
case IOCTL_BTHX_QUERY_CAPABILITIES:
case IOCTL_BTHX_WRITE_HCI:
case IOCTL_BTHX_READ_HCI:
Device = WdfIoQueueGetDevice(_Queue);
WDF_REQUEST_FORWARD_OPTIONS_INIT(&ForwardOptions);
ForwardOptions.Flags = WDF_REQUEST_FORWARD_OPTION_SEND_AND_FORGET;
ParentDevice = WdfPdoGetParent(Device);
//
// Forward known IOCTLs to FDO to process
//
Status = WdfRequestForwardToParentDeviceIoQueue(_Request,
WdfDeviceGetDefaultQueue(ParentDevice),
&ForwardOptions);
break;
default:
//
// Complete this unexptected IOCTL with default STATUS_INVALID_PARAMETER.
//
DoTrace(LEVEL_ERROR, TFLAG_IOCTL,("Unexpected IOCTL_(0x%x, Func %d)", _IoControlCode, ControlCode));
break;
}
if (!NT_SUCCESS(Status)){
DoTrace(LEVEL_ERROR, TFLAG_IOCTL,(" IOCTL_(0x%x, Func %d) failed %!STATUS!", _IoControlCode, ControlCode, Status));
WdfRequestComplete(_Request, Status);
return;
}
return;
}
|