ReactOS 0.4.17-dev-1281-g065abb3
entry.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS SuperSpeed USB Hub Driver
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Driver entry, cleanup and the global registry policy
5 * COPYRIGHT: Copyright 2026 Justin Miller <justin.miller@reactos.org>
6 */
7
8#include "usbhub3.h"
9
10#define NDEBUG
11#include <debug.h>
12
14
15/* USB stack loaders check this to learn which USBD contract the hub speaks */
16extern "C" const ULONG Microsoft_USBD_Compat_Version = 0x110;
17
18extern "C" DRIVER_INITIALIZE DriverEntry;
19static EVT_WDF_OBJECT_CONTEXT_CLEANUP HubEvtDriverCleanup;
20
23static
26HubQueryRegistryULong(
27 _In_ WDFKEY Key,
30{
32
33 *Value = 0;
35
37}
38
40static
43HubOpenRegistryKey(
46 _Out_ WDFKEY* Key)
47{
49
52}
53
54/* One policy value and what a nonzero read does with it */
56{
59};
60
61static
62VOID
66{
67 if (Value != 0)
69}
70
71static
72VOID
76{
79 else
80 DPRINT1("Ignoring MsOsDescriptorMode %lu\n", Value);
81}
82
83static
84VOID
88{
89 if (Value != 0)
91}
92
93/* The default is on; only an explicit zero turns it off */
94static
95VOID
99{
100 if (Value == 0)
102}
103
104static
105VOID
106NTAPI
109{
110 if (Value != 0)
112}
113
114/* Bit 3 adds reserved field checks, bit 2 the strictest parsing */
115static
116VOID
117NTAPI
120{
121 if (Value == 0)
122 return;
123
125 if (Value & 0x8)
127 if (Value & 0x4)
129}
130
131static
132VOID
133NTAPI
136{
137 if (Value != 0)
139}
140
141static
142VOID
143NTAPI
146{
147 if (Value != 0)
149}
150
152{
153 { L"DisableSelectiveSuspendUI", HubApplyDisableSelectiveSuspendUI },
154 { L"MsOsDescriptorMode", HubApplyMsOsDescriptorMode },
155 { L"EnableDiagnosticMode", HubApplyEnableDiagnosticMode },
156 { L"DisableOnSoftRemove", HubApplyDisableOnSoftRemove },
157 { L"DisableUxdSupport", HubApplyDisableUxdSupport },
158 { L"EnableExtendedValidation", HubApplyEnableExtendedValidation },
159 { L"WakeOnConnectUI", HubApplyWakeOnConnectUI },
160 { L"PreventDebounceTimeForSuperSpeedDevices", HubApplyPreventSuperSpeedDebounce }
161};
162
163/* A missing value is skipped; any other read error ends the walk */
165static
166CODE_SEG("INIT")
167VOID
168NTAPI
170{
171 WDFKEY Key;
172 ULONG Index;
173 ULONG Value;
175
177
178 if (!NT_SUCCESS(HubOpenRegistryKey(L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\USBHUB\\hubg",
180 {
181 return;
182 }
183
185 {
186 Status = HubQueryRegistryULong(Key, HubGlobalPolicy[Index].Name, &Value);
188 continue;
189
190 if (!NT_SUCCESS(Status))
191 {
192 DPRINT1("Reading %S failed 0x%lx\n", HubGlobalPolicy[Index].Name, Status);
193 break;
194 }
195
197 }
198
200}
201
202/* On by default with an L1 timeout of 2; read errors are ignored */
204static
205CODE_SEG("INIT")
206VOID
207NTAPI
209{
210 WDFKEY Key;
211 ULONG Value;
212
215
216 if (!NT_SUCCESS(HubOpenRegistryKey(L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\usb\\Usb20HardwareLpm",
218 {
219 return;
220 }
221
222 if (NT_SUCCESS(HubQueryRegistryULong(Key, L"Usb20HardwareLpmOverride", &Value)))
223 {
224 if (Value != 0)
226 else
228 }
229
230 if (NT_SUCCESS(HubQueryRegistryULong(Key, L"Usb20HardwareLpmTimeout", &Value)) && Value <= MAXUCHAR)
232
234}
235
237static
238CODE_SEG("INIT")
239VOID
240NTAPI
242{
243 WDFKEY Key;
244 ULONG Value;
245
247
248 if (!NT_SUCCESS(HubOpenRegistryKey(L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\usb\\UsbLtm",
250 {
251 return;
252 }
253
254 if (NT_SUCCESS(HubQueryRegistryULong(Key, L"UsbLtmEnable", &Value)))
255 {
256 if (Value != 0)
258 else
260 }
261
263}
264
265/* The kernel export is optional; without it no global flags apply */
267static
268CODE_SEG("INIT")
269VOID
270NTAPI
272{
273 ULONG64 Flags = 0;
274
276 return;
277
278 HubDriver.KseQueryDeviceFlags(L"USBHUB:GLOBAL_FLAGS", L"USBHUB", &Flags);
279
280 if (Flags & 1)
282}
283
287NTAPI
289{
290 static const struct
291 {
292 PCWSTR Name;
294 } UxdValues[] =
295 {
296 { L"UxdGlobalDeleteOnShutdown", HubGlobal::UxdDeleteOnShutdown },
297 { L"UxdGlobalDeleteOnReload", HubGlobal::UxdDeleteOnReload },
298 { L"UxdGlobalDeleteOnDisconnect", HubGlobal::UxdDeleteOnDisconnect },
299 { L"UxdGlobalEnable", HubGlobal::UxdEnable }
300 };
301 WDFKEY Key;
302 ULONG Index;
303 ULONG Value;
305
306 for (Index = 0; Index < RTL_NUMBER_OF(UxdValues); Index++)
307 HubDriver.ClearFlag(UxdValues[Index].Flag);
308
309 Status = HubOpenRegistryKey(L"\\registry\\machine\\system\\currentcontrolset\\services\\usbhub\\uxd_control\\policy",
311 if (!NT_SUCCESS(Status))
312 return Status;
313
314 for (Index = 0; Index < RTL_NUMBER_OF(UxdValues); Index++)
315 {
316 if (NT_SUCCESS(HubQueryRegistryULong(Key, UxdValues[Index].Name, &Value)) && Value != 0)
317 HubDriver.SetFlag(UxdValues[Index].Flag);
318 }
319
321 return STATUS_SUCCESS;
322}
323
326NTAPI
327HubIsWinPe(VOID)
328{
330}
331
332/* Internal boot devices are never registered, so their handle stays NULL */
333VOID
334NTAPI
337{
340}
341
343static
344PVOID
345NTAPI
346HubResolveKernelRoutine(
348{
350
353}
354
355static
356VOID
357NTAPI
360{
362
363 PAGED_CODE();
364
367
369}
370
371extern "C"
372CODE_SEG("INIT")
374NTAPI
378{
382
386
388 Config.DriverPoolTag = HUB_TAG_DRIVER;
389
391 Attributes.EvtCleanupCallback = HubEvtDriverCleanup;
392
395 {
396 DPRINT1("WdfDriverCreate failed 0x%lx\n", Status);
397 return Status;
398 }
399
402
403 Status = WdfWaitLockCreate(&Attributes, &HubDriver.HubListLock);
404 if (!NT_SUCCESS(Status))
405 {
406 DPRINT1("Hub list lock create failed 0x%lx\n", Status);
407 return Status;
408 }
409
410 Status = WdfWaitLockCreate(&Attributes, &HubDriver.CompanionPortLock);
411 if (!NT_SUCCESS(Status))
412 {
413 DPRINT1("Connector map lock create failed 0x%lx\n", Status);
414 return Status;
415 }
416
420
422 (PFN_HUB_KSE_QUERY_DEVICE_FLAGS)HubResolveKernelRoutine(L"KseQueryDeviceFlags");
424 (PFN_HUB_REGISTER_POWER_SETTING)HubResolveKernelRoutine(L"PoRegisterPowerSettingCallback");
426 (PFN_HUB_UNREGISTER_POWER_SETTING)HubResolveKernelRoutine(L"PoUnregisterPowerSettingCallback");
428 (PFN_HUB_GET_INTERFACE_PROPERTY)HubResolveKernelRoutine(L"IoGetDeviceInterfacePropertyData");
429
431
432 DPRINT("Hub driver loaded, global flags 0x%lx\n", (ULONG)HubDriver.GlobalFlags);
433
435}
#define PAGED_CODE()
#define CODE_SEG(...)
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
unsigned char BOOLEAN
Definition: actypes.h:127
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
static EVT_WDF_DRIVER_DEVICE_ADD EvtDeviceAdd
Definition: fdo.h:135
_In_ ULONG Attributes
Definition: dispmprt.h:1077
_In_ D3DDDI_VIDEO_PRESENT_TARGET_ID _In_ ULONG _In_ ULONG Flags
Definition: dispmprt.h:245
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define L(x)
Definition: resources.c:13
#define _IRQL_requires_(irql)
Definition: driverspecs.h:229
HubReadUsb20LpmPolicy()
_In_ ACCESS_MASK Access
Definition: entry.cpp:45
static VOID NTAPI HubApplyWakeOnConnectUI(_In_ ULONG Value)
Definition: entry.cpp:134
static VOID NTAPI HubApplyDisableUxdSupport(_In_ ULONG Value)
Definition: entry.cpp:107
HubReadGlobalPolicy()
WDF_DRIVER_CONFIG_INIT & Config
Definition: entry.cpp:387
_In_ PCWSTR _Out_ PULONG Value
Definition: entry.cpp:30
RtlInitUnicodeString & KeyName
Definition: entry.cpp:50
static VOID NTAPI HubApplyEnableExtendedValidation(_In_ ULONG Value)
Definition: entry.cpp:118
static VOID NTAPI HubApplyDisableOnSoftRemove(_In_ ULONG Value)
Definition: entry.cpp:97
static VOID NTAPI HubApplyEnableDiagnosticMode(_In_ ULONG Value)
Definition: entry.cpp:86
static const HubPolicyValue HubGlobalPolicy[]
Definition: entry.cpp:151
static VOID NTAPI HubApplyPreventSuperSpeedDebounce(_In_ ULONG Value)
Definition: entry.cpp:144
return WdfRegistryQueryValue(Key, &ValueName, sizeof(*Value), Value, NULL, NULL)
static VOID NTAPI HubApplyDisableSelectiveSuspendUI(_In_ ULONG Value)
Definition: entry.cpp:64
DRIVER_INITIALIZE DriverEntry
Definition: entry.cpp:18
HubReadKseGlobalFlags()
const ULONG Microsoft_USBD_Compat_Version
Definition: entry.cpp:16
static EVT_WDF_OBJECT_CONTEXT_CLEANUP HubEvtDriverCleanup
Definition: entry.cpp:19
NTSTATUS Status
Definition: entry.cpp:381
return STATUS_SUCCESS
Definition: entry.cpp:434
RtlInitUnicodeString & ValueName
Definition: entry.cpp:34
HubReadLtmPolicy()
VOID NTAPI HubNotifyBootDeviceRemoval(_In_opt_ PVOID Handle)
Definition: entry.cpp:335
static VOID NTAPI HubApplyMsOsDescriptorMode(_In_ ULONG Value)
Definition: entry.cpp:74
WDF_OBJECT_ATTRIBUTES Attributes
Definition: entry.cpp:380
HubDriver DriverObject
Definition: entry.cpp:383
return WdfRegistryOpenKey(NULL, &KeyName, Access, WDF_NO_OBJECT_ATTRIBUTES, Key)
_In_ PCWSTR Name
Definition: entry.cpp:28
HubDriverState HubDriver
Definition: entry.cpp:13
_In_ ACCESS_MASK _Out_ WDFKEY * Key
Definition: entry.cpp:47
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
WdfRegistryClose(HardwareKey)
ULONG Handle
Definition: gdb_input.c:15
Status
Definition: gdiplustypes.h:24
_Must_inspect_result_ NTSTATUS(NTAPI * PFN_HUB_GET_INTERFACE_PROPERTY)(_In_ PUNICODE_STRING SymbolicLinkName, _In_ CONST DEVPROPKEY *PropertyKey, _In_ LCID Lcid, _Reserved_ ULONG Flags, _In_ ULONG Size, _Out_writes_bytes_to_(Size, *RequiredSize) PVOID Data, _Out_ PULONG RequiredSize, _Out_ PDEVPROPTYPE Type)
Definition: hubdriver.h:75
NTSTATUS(NTAPI * PFN_HUB_REGISTER_POWER_SETTING)(_In_opt_ PDEVICE_OBJECT DeviceObject, _In_ LPCGUID SettingGuid, _In_ PPOWER_SETTING_CALLBACK Callback, _In_opt_ PVOID Context, _Outptr_opt_ PVOID *Handle)
Definition: hubdriver.h:60
NTSTATUS(NTAPI * PFN_HUB_KSE_QUERY_DEVICE_FLAGS)(_In_ PCWSTR DeviceKey, _In_ PCWSTR DeviceClass, _Out_ PULONG64 DeviceFlags)
Definition: hubdriver.h:42
HubGlobal
Definition: hubdriver.h:12
@ StrictDescriptorChecks
@ EnableExtendedValidation
@ DisableUxdSupport
@ AlwaysSecondReset
@ PreventSuperSpeedDebounce
@ DisableSelectiveSuspendUI
@ UxdDeleteOnReload
@ EnableDiagnosticMode
@ UxdDeleteOnDisconnect
@ DisableOnSoftRemove
@ CheckReservedFields
@ EnableUsb20HardwareLpm
@ UxdDeleteOnShutdown
HubMsOsMode
Definition: hubdriver.h:34
NTSTATUS(NTAPI * PFN_HUB_UNREGISTER_POWER_SETTING)(_Inout_ PVOID Handle)
Definition: hubdriver.h:69
BOOLEAN Apply
HubReadUxdPolicy()
UNICODE_STRING Path
#define HUB_TAG_DRIVER
Definition: hubutil.h:11
unsigned __int64 ULONG64
Definition: imports.h:198
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
#define RTL_REGISTRY_CONTROL
Definition: nt_native.h:163
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define KEY_QUERY_VALUE
Definition: nt_native.h:1019
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:329
#define DPRINT
Definition: sndvol32.h:73
Definition: xml2sdb.h:103
PFN_HUB_KSE_QUERY_DEVICE_FLAGS KseQueryDeviceFlags
Definition: hubdriver.h:111
LIST_ENTRY HubList
Definition: hubdriver.h:95
PFN_HUB_UNREGISTER_POWER_SETTING UnregisterPowerSetting
Definition: hubdriver.h:115
volatile LONG GlobalFlags
Definition: hubdriver.h:91
PFN_HUB_NOTIFY_BOOT_DEVICE_REMOVAL NotifyBootDeviceRemoval
Definition: hubdriver.h:113
WDFWAITLOCK CompanionPortLock
Definition: hubdriver.h:100
VOID SetFlag(_In_ HubGlobal Flag)
Definition: hubdriver.h:126
WDFDRIVER Driver
Definition: hubdriver.h:88
UCHAR Usb20LpmTimeout
Definition: hubdriver.h:103
PFN_HUB_GET_INTERFACE_PROPERTY GetInterfaceProperty
Definition: hubdriver.h:116
PFN_HUB_REGISTER_POWER_SETTING RegisterPowerSetting
Definition: hubdriver.h:114
HubMsOsMode MsOsMode
Definition: hubdriver.h:92
WDFWAITLOCK HubListLock
Definition: hubdriver.h:96
LIST_ENTRY ConnectorMap
Definition: hubdriver.h:99
PDRIVER_OBJECT DriverObject
Definition: hubdriver.h:89
VOID ClearFlag(_In_ HubGlobal Flag)
Definition: hubdriver.h:133
PCWSTR Name
Definition: entry.cpp:57
VOID(NTAPI *Apply)(_In_ ULONG Value)
PVOID NTAPI MmGetSystemRoutineAddress(IN PUNICODE_STRING SystemRoutineName)
Definition: sysldr.c:3672
uint16_t * PWSTR
Definition: typedefs.h:56
uint32_t * PULONG
Definition: typedefs.h:59
const uint16_t * PCWSTR
Definition: typedefs.h:57
unsigned char UCHAR
Definition: typedefs.h:53
#define NTAPI
Definition: typedefs.h:36
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ PUCX_CONTROLLER_CONFIG Config
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
#define MAXUCHAR
Definition: umtypes.h:117
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2705
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:215
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
FORCEINLINE VOID WDF_DRIVER_CONFIG_INIT(_Out_ PWDF_DRIVER_CONFIG Config, _In_opt_ PFN_WDF_DRIVER_DEVICE_ADD EvtDriverDeviceAdd)
Definition: wdfdriver.h:148
FORCEINLINE VOID WDF_OBJECT_ATTRIBUTES_INIT(_Out_ PWDF_OBJECT_ATTRIBUTES Attributes)
Definition: wdfobject.h:147
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING ValueName
Definition: wdfregistry.h:243
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
#define WDF_NO_OBJECT_ATTRIBUTES
Definition: wdftypes.h:105
NTSYSAPI NTSTATUS WINAPI RtlCheckRegistryKey(ULONG, PWSTR)
#define NT_ASSERT
Definition: rtlfuncs.h:3334
RtlInitUnicodeString & RoutineName
Definition: xhcientry.cpp:59