Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions drivers/usb/usbccgp/fdo.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,13 @@ FDO_HandlePnp(
/* Delete the device object */
IoDeleteDevice(DeviceObject);

/* Request completed */
break;
/*
* The lower driver completes the IRP for removal:
* HACK: TODO:
* I'm not convinced this is actually what windows is doing.
* It's possible the issue is in the lower driver.
*/
return Status;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
case IRP_MN_START_DEVICE:
{
Expand Down
3 changes: 3 additions & 0 deletions drivers/usb/usbehci/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ add_library(usbehci MODULE
guid.c
usbehci.rc)

target_compile_definitions(usbehci
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe needs to be marked as "HACK" (+ same in the other cmakelists)

PRIVATE NO_KERNEL_LIST_ENTRY_CHECKS)

set_module_type(usbehci kernelmodedriver)
add_importlibs(usbehci usbport usbd hal ntoskrnl)
add_pch(usbehci usbehci.h SOURCE)
Expand Down
3 changes: 3 additions & 0 deletions drivers/usb/usbhub/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ add_library(usbhub MODULE
${PCH_SKIP_SOURCE}
usbhub.rc)

target_compile_definitions(usbhub
PRIVATE NO_KERNEL_LIST_ENTRY_CHECKS)

target_link_libraries(usbhub ${PSEH_LIB})
set_module_type(usbhub kernelmodedriver)
add_importlibs(usbhub ntoskrnl hal wmilib usbd)
Expand Down
25 changes: 19 additions & 6 deletions drivers/usb/usbhub/usbhub.c
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,12 @@ USBH_ProcessHubStateChange(IN PUSBHUB_FDO_EXTENSION HubExtension,
USBH_SyncClearHubStatus(HubExtension,
USBHUB_FEATURE_C_HUB_LOCAL_POWER);
}
else if (HubStatusChange.OverCurrentChange)
/*
* per USB 2.0 spec multiple hub change bits
* may be set simultaneously. Handle OverCurrentChange independently so
* we don’t miss clearing it when LocalPowerChange is also set.
*/
if (HubStatusChange.OverCurrentChange)
{
USBH_SyncClearHubStatus(HubExtension,
USBHUB_FEATURE_C_HUB_OVER_CURRENT);
Expand Down Expand Up @@ -1948,25 +1953,33 @@ USBH_ProcessPortStateChange(IN PUSBHUB_FDO_EXTENSION HubExtension,
}

IoInvalidateDeviceRelations(HubExtension->LowerPDO, BusRelations);
return;
}
else if (PortStatusChange.PortEnableDisableChange)

/*
* Multiple port change bits can be set at once (e.g. enable + reset).
* Handle each independently
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Handle each independently
* Handle each independently.

*/
if (PortStatusChange.PortEnableDisableChange)
{
RequestValue = USBHUB_FEATURE_C_PORT_ENABLE;
PortData->PortStatus = *PortStatus;
USBH_SyncClearPortStatus(HubExtension, Port, RequestValue);
return;
}
else if (PortStatusChange.SuspendChange)

if (PortStatusChange.SuspendChange)
{
DPRINT1("USBH_ProcessPortStateChange: SuspendChange UNIMPLEMENTED. FIXME\n");
DbgBreakPoint();
}
else if (PortStatusChange.OverCurrentIndicatorChange)

if (PortStatusChange.OverCurrentIndicatorChange)
{
DPRINT1("USBH_ProcessPortStateChange: OverCurrentIndicatorChange UNIMPLEMENTED. FIXME\n");
DbgBreakPoint();
}
else if (PortStatusChange.ResetChange)

if (PortStatusChange.ResetChange)
{
RequestValue = USBHUB_FEATURE_C_PORT_RESET;
PortData->PortStatus = *PortStatus;
Expand Down
3 changes: 3 additions & 0 deletions drivers/usb/usbohci/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ add_library(usbohci MODULE
guid.c
usbohci.rc)

target_compile_definitions(usbohci
PRIVATE NO_KERNEL_LIST_ENTRY_CHECKS)

set_module_type(usbohci kernelmodedriver)
add_importlibs(usbohci usbport usbd hal ntoskrnl)
add_pch(usbohci usbohci.h SOURCE)
Expand Down
3 changes: 3 additions & 0 deletions drivers/usb/usbport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ add_library(usbport MODULE
usbport.rc
${CMAKE_CURRENT_BINARY_DIR}/usbport.def)

target_compile_definitions(usbport
PRIVATE NO_KERNEL_LIST_ENTRY_CHECKS)

add_pch(usbport usbport.h "${PCH_SKIP_SOURCE}")
set_module_type(usbport kernelmodedriver)
add_importlibs(usbport ntoskrnl hal)
Expand Down
24 changes: 17 additions & 7 deletions drivers/usb/usbport/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1389,17 +1389,27 @@ USBPORT_InitializeDevice(IN PUSBPORT_DEVICE_HANDLE DeviceHandle,

if (NT_SUCCESS(Status))
{
ASSERT(TransferedLen == sizeof(USB_DEVICE_DESCRIPTOR));
ASSERT(DeviceHandle->DeviceDescriptor.bLength >= sizeof(USB_DEVICE_DESCRIPTOR));
ASSERT(DeviceHandle->DeviceDescriptor.bDescriptorType == USB_DEVICE_DESCRIPTOR_TYPE);
/* Some devices may return a short transfer here; accept it if the
header through bMaxPacketSize0 was received earlier during
USBPORT_CreateDevice. */
if (TransferedLen < sizeof(USB_DEVICE_DESCRIPTOR))
{
DPRINT1("USBPORT_InitializeDevice: Short device descriptor len %lu\n",
TransferedLen);
}

/* Use the known bMaxPacketSize0 (was obtained prior to SetAddress). */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Use the known bMaxPacketSize0 (was obtained prior to SetAddress). */
/* Use the known bMaxPacketSize0 (was obtained prior to SetAddress) */

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes you have me remove periods like this comment or add them like above, I'm confused on what you actually want here. What are you trying to do?

Copy link
Contributor

@HBelusca HBelusca Nov 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here my "logic" was:

  1. Single-line single sentence comment -- no dot;
  2. Two or more sentences -- all sentences end with dot (even if they are all on the same single line).
  3. Multi-line comment (with single or more sentences) -- all sentences end with dot.

Just a preference, there's nothing as such in our code style, so do as you personally prefer.

MaxPacketSize = DeviceHandle->DeviceDescriptor.bMaxPacketSize0;

ASSERT((MaxPacketSize == 8) ||
(MaxPacketSize == 16) ||
(MaxPacketSize == 32) ||
(MaxPacketSize == 64));
if (!(MaxPacketSize == 8 || MaxPacketSize == 16 ||
MaxPacketSize == 32 || MaxPacketSize == 64))
{
DPRINT1("USBPORT_InitializeDevice: Invalid MPS0 %u\n", MaxPacketSize);
Status = STATUS_DEVICE_DATA_ERROR;
goto ExitError;
}

/* Mark USB2 hub flag when identifiable (bDeviceClass available in header). */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Mark USB2 hub flag when identifiable (bDeviceClass available in header). */
/* Mark USB2 hub flag when identifiable (bDeviceClass available in header) */

if (DeviceHandle->DeviceSpeed == UsbHighSpeed &&
DeviceHandle->DeviceDescriptor.bDeviceClass == USB_DEVICE_CLASS_HUB)
{
Expand Down
14 changes: 14 additions & 0 deletions drivers/usb/usbport/roothub.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ USBPORT_RootHubClassCommand(IN PDEVICE_OBJECT FdoDevice,
return RHStatus;
}

if (Port == 0 ||
Port > PdoExtension->RootHubDescriptors->Descriptor.bNumberOfPorts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not Port >= PdoExtension->RootHubDescriptors->Descriptor.bNumberOfPorts instead?
Or is Port a 1-based value?

{
DPRINT1("USBPORT_RootHubClassCommand: CLEAR_FEATURE invalid port %u\n", Port);
return RHStatus;
}

switch (Feature)
{
case FEATURE_PORT_ENABLE:
Expand Down Expand Up @@ -249,6 +256,13 @@ USBPORT_RootHubClassCommand(IN PDEVICE_OBJECT FdoDevice,

Feature = SetupPacket->wValue.W;

if (Port == 0 ||
Port > PdoExtension->RootHubDescriptors->Descriptor.bNumberOfPorts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question

{
DPRINT1("USBPORT_RootHubClassCommand: SET_FEATURE invalid port %u\n", Port);
return RHStatus;
}

switch (Feature)
{
case FEATURE_PORT_ENABLE:
Expand Down
21 changes: 18 additions & 3 deletions drivers/usb/usbstor/scsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ USBSTOR_SendCBWRequest(
Context->cbw.Tag = PtrToUlong(Irp);
Context->cbw.DataTransferLength = Request->DataTransferLength;
Context->cbw.Flags = ((UCHAR)Request->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) << 1;
Context->cbw.LUN = PDODeviceExtension->LUN;

// Per BOT spec, LUN is 4 bits; clamp just in case
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BOT == Bulk Only Transfer ?

Context->cbw.LUN = (UCHAR)(PDODeviceExtension->LUN & 0x0F);
Context->cbw.CommandBlockLength = Request->CdbLength;

RtlCopyMemory(&Context->cbw.CommandBlock, Request->Cdb, Request->CdbLength);
Expand Down Expand Up @@ -552,8 +554,21 @@ USBSTOR_HandleExecuteSCSI(

DPRINT("USBSTOR_HandleExecuteSCSI Operation Code %x, Length %lu\n", SrbGetCdb(Request)->CDB10.OperationCode, Request->DataTransferLength);

// check that we're sending to the right LUN
ASSERT(SrbGetCdb(Request)->CDB10.LogicalUnitNumber == PDODeviceExtension->LUN);
/*
* Some stacks/devices don’t propagate the LUN in the CDB10 header bits.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they don't propagate the LUN in the CDB10 header, then what does the CDB10.LogicalUnitNumber member contain in that case? a random value? 0xFFFFFFFF ? or 0 ?

* USB BOT uses CBW.bCBWLUN for addressing.
*
* TODO: There's actually a bigger bug here though,
* while the above CAN HAPPPEN:
* Card Readers multiplex their card slots for example But we can deal with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Card Readers multiplex their card slots for example But we can deal with
* Card Readers multiplex their card slots for example, but we can deal with

* that as we put more effort into fixing the USB stack.
*/
if (SrbGetCdb(Request)->CDB10.LogicalUnitNumber != PDODeviceExtension->LUN)
{
DPRINT1("USBSTOR_HandleExecuteSCSI: CDB LUN %u != PDO LUN %u (using CBW LUN)\n",
(ULONG)SrbGetCdb(Request)->CDB10.LogicalUnitNumber,
(ULONG)PDODeviceExtension->LUN);
}

return USBSTOR_SendCBWRequest(PDODeviceExtension->LowerDeviceObject->DeviceExtension, Irp);
}
3 changes: 3 additions & 0 deletions drivers/usb/usbuhci/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ add_library(usbuhci MODULE
guid.c
usbuhci.rc)

target_compile_definitions(usbuhci
PRIVATE NO_KERNEL_LIST_ENTRY_CHECKS)

set_module_type(usbuhci kernelmodedriver)
add_importlibs(usbuhci usbport usbd hal ntoskrnl)
add_pch(usbuhci usbuhci.h SOURCE)
Expand Down