rewrite comments into one, unified style

This commit is contained in:
maksw committed 2026-06-15 10:17:48 +02:00
1 parent 6260de0085
commit 467a58c9c6
163 files changed
+3560 -2556

No files matched your search

+42 -10
View File
@@ -1,5 +1,5 @@
// usb_hub.c - USB hub function driver.
//
// USB hub driver. This file owns hub probing, port status handling, and child-device enumeration.
// Root hubs and external hubs are represented by the same FDO abstraction. The
// xHCI root hub feeds port-change events directly into this driver, while an
// external hub feeds the same port state machine from its interrupt-IN change
@@ -21,16 +21,19 @@ static const IO_DRIVER_ID_TABLE gUsbHubIdTable = {
#define USB_HUB_PORT_ENUMERATING 0xffu
#define USB_HUB_PORT_FAILED 0xfeu
// Implements the hub Slot Busy operation.
static bool UsbHubSlotBusy(uint8_t slot) {
return slot != 0 && slot != USB_HUB_PORT_ENUMERATING && slot != USB_HUB_PORT_FAILED;
}
// Implements the hub Root Port Connected operation.
static bool UsbHubRootPortConnected(XHCI_CONTROLLER* xhci, uint8_t port) {
if (!xhci || !xhci->Op || port == 0) return false;
uint32_t portsc = XhciRead32(xhci->Op, XhciPortReg(port));
return (portsc & XHCI_PORTSC_CCS) != 0;
}
// Implements the hub Clear Root Port Failure operation.
static void UsbHubClearRootPortFailure(USB_HUB_EXTENSION* hub, uint8_t port) {
if (!hub || port == 0 || port > USB_ROOT_HUB_MAX_PORTS) return;
hub->RootPortFailCount[port] = 0;
@@ -38,6 +41,7 @@ static void UsbHubClearRootPortFailure(USB_HUB_EXTENSION* hub, uint8_t port) {
hub->RootPortSlot[port] = 0;
}
// Implements the hub Mark Root Port Failed operation.
static void UsbHubMarkRootPortFailed(USB_HUB_EXTENSION* hub, uint8_t port) {
if (!hub || port == 0 || port > USB_ROOT_HUB_MAX_PORTS) return;
hub->RootPortSlot[port] = USB_HUB_PORT_FAILED;
@@ -71,14 +75,13 @@ static KSTATUS UsbHubEnumerateRootPortWithRetries(USB_HUB_EXTENSION* hub,
hub->RootPortFailCount[port] = attempt;
/*
* On real Intel laptop controllers some internal full-speed devices
* respond with xHCI USB Transaction Error during Address Device while
* the port is still settling. Retry that boundedly while the port is
* held in ENUMERATING state so queued PSC events cannot start parallel
* enumerations. Descriptor/control timeouts are not retried here; they
* are usually dead/firmware-owned ports and retrying them stalls boot.
*/
// On real Intel laptop controllers some internal full-speed devices
// respond with xHCI USB Transaction Error during Address Device while
// the port is still settling. Retry that boundedly while the port is
// held in ENUMERATING state so queued PSC events cannot start parallel
// enumerations. Descriptor/control timeouts are not retried here; they
// are usually dead/firmware-owned ports and retrying them stalls boot.
if (last != STATUS_DEVICE_ERROR || attempt == maxAttempts)
break;
@@ -97,6 +100,7 @@ static KSTATUS UsbHubEnumerateRootPortWithRetries(USB_HUB_EXTENSION* hub,
return last;
}
// Implements the hub Pop Pending Root Port operation.
static bool UsbHubPopPendingRootPort(USB_HUB_EXTENSION* hub, uint8_t* outPort) {
if (!hub || !outPort) return false;
@@ -118,6 +122,7 @@ static bool UsbHubPopPendingRootPort(USB_HUB_EXTENSION* hub, uint8_t* outPort) {
return false;
}
// Implements the hub Pop Pending External Port operation.
static bool UsbHubPopPendingExternalPort(USB_HUB_EXTENSION* hub, uint8_t* outPort) {
if (!hub || !outPort) return false;
@@ -142,6 +147,7 @@ static bool UsbHubPopPendingExternalPort(USB_HUB_EXTENSION* hub, uint8_t* outPor
return false;
}
// Implements the hub Record Root Port Change operation.
static void UsbHubRecordRootPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
if (!hub || port == 0 || port > hub->PortCount)
return;
@@ -154,6 +160,7 @@ static void UsbHubRecordRootPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
KeSignalSemaphore(hub->WorkerSemaphore);
}
// Implements the hub Queue External Port Change operation.
void UsbHubQueueExternalPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
if (!hub || port == 0 || port > hub->PortCount || port > USB_EXTERNAL_HUB_MAX_PORTS)
return;
@@ -164,6 +171,7 @@ void UsbHubQueueExternalPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
KeSignalSemaphore(hub->WorkerSemaphore);
}
// Implements the hub Remove Slot Child operation.
static void UsbHubRemoveSlotChild(USB_HUB_EXTENSION* hub, uint8_t port, uint8_t slot) {
if (!hub || !hub->Hcd || slot == 0)
return;
@@ -197,6 +205,7 @@ static void UsbHubRemoveSlotChild(USB_HUB_EXTENSION* hub, uint8_t port, uint8_t
}
}
// Implements the hub Remove Root Child operation.
static void UsbHubRemoveRootChild(USB_HUB_EXTENSION* hub, uint8_t port, uint8_t slot) {
UsbHubRemoveSlotChild(hub, port, slot);
}
@@ -249,6 +258,7 @@ static KSTATUS UsbHubControlNoData(USB_HUB_EXTENSION* hub,
return UsbSubmitUrbSync(hub->LowerDevice, &urb);
}
// Implements the hub Read Descriptor operation.
static KSTATUS UsbHubReadDescriptor(USB_HUB_EXTENSION* hub, USB_HUB_DESCRIPTOR* outDesc) {
if (!hub || !outDesc || !hub->ChangeBitmap || !hub->ChangeBitmapPhys)
return STATUS_INVALID_PARAMETER;
@@ -271,6 +281,7 @@ static KSTATUS UsbHubReadDescriptor(USB_HUB_EXTENSION* hub, USB_HUB_DESCRIPTOR*
return STATUS_SUCCESS;
}
// Implements the hub Get Port Status operation.
static KSTATUS UsbHubGetPortStatus(USB_HUB_EXTENSION* hub, uint8_t port, USB_HUB_PORT_STATUS* outStatus) {
if (!hub || !outStatus || port == 0)
return STATUS_INVALID_PARAMETER;
@@ -291,6 +302,7 @@ static KSTATUS UsbHubGetPortStatus(USB_HUB_EXTENSION* hub, uint8_t port, USB_HUB
return STATUS_SUCCESS;
}
// Implements the hub Set Port Feature operation.
static KSTATUS UsbHubSetPortFeature(USB_HUB_EXTENSION* hub, uint8_t port, uint16_t feature) {
return UsbHubControlNoData(hub,
USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
@@ -299,6 +311,7 @@ static KSTATUS UsbHubSetPortFeature(USB_HUB_EXTENSION* hub, uint8_t port, uint16
port);
}
// Implements the hub Clear Port Feature operation.
static KSTATUS UsbHubClearPortFeature(USB_HUB_EXTENSION* hub, uint8_t port, uint16_t feature) {
return UsbHubControlNoData(hub,
USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
@@ -307,6 +320,7 @@ static KSTATUS UsbHubClearPortFeature(USB_HUB_EXTENSION* hub, uint8_t port, uint
port);
}
// Implements the hub Speed From Port Status operation.
static uint8_t UsbHubSpeedFromPortStatus(uint16_t status) {
if (status & USB_HUB_PORT_STATUS_LOW_SPEED)
return 2u;
@@ -315,6 +329,7 @@ static uint8_t UsbHubSpeedFromPortStatus(uint16_t status) {
return 1u;
}
// Implements the hub Reset External Port operation.
static KSTATUS UsbHubResetExternalPort(USB_HUB_EXTENSION* hub, uint8_t port, uint8_t* outSpeed) {
if (outSpeed) *outSpeed = 0;
if (!hub || port == 0)
@@ -355,6 +370,7 @@ static KSTATUS UsbHubResetExternalPort(USB_HUB_EXTENSION* hub, uint8_t port, uin
return STATUS_SUCCESS;
}
// Implements the hub Handle External Port Change operation.
static void UsbHubHandleExternalPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
if (!hub || hub->Kind != UsbHubKindExternal || port == 0 || port > hub->PortCount || port > USB_EXTERNAL_HUB_MAX_PORTS)
return;
@@ -433,6 +449,7 @@ static void UsbHubHandleExternalPortChange(USB_HUB_EXTENSION* hub, uint8_t port)
}
}
// Implements the hub Handle Root Port Change operation.
static void UsbHubHandleRootPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
if (!hub || !hub->Hcd || port == 0 || port > hub->PortCount || port > USB_ROOT_HUB_MAX_PORTS)
return;
@@ -509,6 +526,7 @@ static void UsbHubHandleRootPortChange(USB_HUB_EXTENSION* hub, uint8_t port) {
static void UsbHubArmExternalInterrupt(USB_HUB_EXTENSION* hub);
static KSTATUS UsbHubDoRootPortScan(USB_HCD* hcd);
// Implements the hub Take Initial Root Scan operation.
static bool UsbHubTakeInitialRootScan(USB_HUB_EXTENSION* hub) {
if (!hub || hub->Kind != UsbHubKindRoot)
return false;
@@ -522,6 +540,7 @@ static bool UsbHubTakeInitialRootScan(USB_HUB_EXTENSION* hub) {
return take;
}
// Implements the hub Worker operation.
static void UsbHubWorker(void* arg) {
USB_HUB_EXTENSION* hub = (USB_HUB_EXTENSION*)arg;
while (hub && hub->WorkerRunning) {
@@ -546,6 +565,7 @@ static void UsbHubWorker(void* arg) {
}
}
// Implements the hub Do Root Port Scan operation.
static KSTATUS UsbHubDoRootPortScan(USB_HCD* hcd) {
if (!hcd || !hcd->RootHubContext)
return STATUS_INVALID_PARAMETER;
@@ -592,6 +612,7 @@ static KSTATUS UsbHubDoRootPortScan(USB_HCD* hcd) {
return STATUS_SUCCESS;
}
// Implements the hub Schedule Root Scan operation.
KSTATUS UsbHubScheduleRootScan(USB_HCD* hcd) {
if (!hcd || !hcd->RootHubContext)
return STATUS_INVALID_PARAMETER;
@@ -609,6 +630,7 @@ KSTATUS UsbHubScheduleRootScan(USB_HCD* hcd) {
return STATUS_SUCCESS;
}
// Implements the hub Queue Root Port Change operation.
void UsbHubQueueRootPortChange(USB_HCD* hcd, uint8_t port) {
if (!hcd || !hcd->RootHubContext)
return;
@@ -616,6 +638,7 @@ void UsbHubQueueRootPortChange(USB_HCD* hcd, uint8_t port) {
UsbHubRecordRootPortChange(hub, port);
}
// Implements the hub Scan External Ports operation.
static KSTATUS UsbHubScanExternalPorts(USB_HUB_EXTENSION* hub) {
if (!hub || hub->Kind != UsbHubKindExternal)
return STATUS_INVALID_PARAMETER;
@@ -643,6 +666,7 @@ static KSTATUS UsbHubScanExternalPorts(USB_HUB_EXTENSION* hub) {
return STATUS_SUCCESS;
}
// Implements the hub Find Interrupt In operation.
static const USB_ENDPOINT_DESCRIPTOR* UsbHubFindInterruptIn(USB_PDO_EXTENSION* pdoExt) {
if (!pdoExt)
return (const USB_ENDPOINT_DESCRIPTOR*)0;
@@ -676,6 +700,7 @@ static const USB_ENDPOINT_DESCRIPTOR* UsbHubFindInterruptIn(USB_PDO_EXTENSION* p
return (const USB_ENDPOINT_DESCRIPTOR*)0;
}
// Implements the hub External Change Complete operation.
static void UsbHubExternalChangeComplete(USB_URB* urb, void* context) {
USB_HUB_EXTENSION* hub = (USB_HUB_EXTENSION*)context;
if (!hub || urb != &hub->ChangeUrb)
@@ -697,6 +722,7 @@ static void UsbHubExternalChangeComplete(USB_URB* urb, void* context) {
}
}
// Implements the hub Arm External Interrupt operation.
static void UsbHubArmExternalInterrupt(USB_HUB_EXTENSION* hub) {
if (!hub || !hub->InterruptPipe || !hub->ChangeBitmap || hub->ChangeUrbPending)
return;
@@ -724,6 +750,7 @@ static void UsbHubArmExternalInterrupt(USB_HUB_EXTENSION* hub) {
}
}
// Implements the hub Start Root Device operation.
static KSTATUS UsbHubStartRootDevice(DEVICE_OBJECT* device, USB_HUB_EXTENSION* hub) {
hub->PortCount = hub->Hcd ? hub->Hcd->MaxRootPorts : 0;
if (hub->PortCount > USB_ROOT_HUB_MAX_PORTS)
@@ -752,6 +779,7 @@ static KSTATUS UsbHubStartRootDevice(DEVICE_OBJECT* device, USB_HUB_EXTENSION* h
return STATUS_SUCCESS;
}
// Implements the hub Start External Device operation.
static KSTATUS UsbHubStartExternalDevice(DEVICE_OBJECT* device, USB_HUB_EXTENSION* hub) {
if (!hub || !hub->Device)
return STATUS_INVALID_PARAMETER;
@@ -821,6 +849,7 @@ static KSTATUS UsbHubStartExternalDevice(DEVICE_OBJECT* device, USB_HUB_EXTENSIO
return STATUS_SUCCESS;
}
// Implements the hub Start Device operation.
static KSTATUS UsbHubStartDevice(DEVICE_OBJECT* device, USB_HUB_EXTENSION* hub) {
if (!device || !hub || hub->Started)
return STATUS_SUCCESS;
@@ -830,6 +859,7 @@ static KSTATUS UsbHubStartDevice(DEVICE_OBJECT* device, USB_HUB_EXTENSION* hub)
return UsbHubStartExternalDevice(device, hub);
}
// Implements the hub Dispatch Pnp operation.
KSTATUS UsbHubDispatchPnp(DEVICE_OBJECT* device, IRP* irp) {
if (!device || !irp)
return STATUS_INVALID_PARAMETER;
@@ -932,6 +962,7 @@ KSTATUS UsbHubDispatchPnp(DEVICE_OBJECT* device, IRP* irp) {
}
}
// Implements the hub Add Device operation.
KSTATUS UsbHubAddDevice(DRIVER_OBJECT* driver, DEVICE_OBJECT* physicalDeviceObject) {
if (!driver || !physicalDeviceObject || physicalDeviceObject->Type != DeviceObjectPhysical)
return STATUS_INVALID_PARAMETER;
@@ -1017,6 +1048,7 @@ KSTATUS UsbHubAddDevice(DRIVER_OBJECT* driver, DEVICE_OBJECT* physicalDeviceObje
return STATUS_SUCCESS;
}
// Initializes hub Driver.
KSTATUS UsbInitializeHubDriver(void) {
memset(&gUsbHubDriver, 0, sizeof(gUsbHubDriver));
IoInitializeDriverObject(&gUsbHubDriver, SYS_SV_LITERAL("usb-hub"));