This script is a PowerShell script for tuning XHCI (eXtensible Host Controller Interface) controller settings, specifically the IMOD (Interrupt Moderation) interval, and potentially other controller parameters. Below is a detailed explanation of the script's functionality:
Script Breakdown
Parameter Definition
param(
[switch]$verbose
)
- The
$verbose switch parameter indicates whether detailed logging should be shown.
Global Settings
$globalInterval = 0x0
$globalHCSPARAMSOffset = 0x4
$globalRTSOFF = 0x18
- These variables define default values for the IMOD interval and certain offsets that apply to all XHCI controllers unless overridden on a per-controller basis.
User Defined Data
$userDefinedData = @{}
- This hash table allows users to override the global settings for specific XHCI controllers identified by their hardware IDs.
Tool Path
$rwePath = "C:\Program Files\RW-Everything\Rw.exe"
- Path to the
Rw.exe tool, which is used to read and write memory on system hardware.
Helper Functions
-
Convert Decimal to Hex
function Dec-To-Hex($decimal) {
$hexValue = $decimal.ToString("X2")
return "0x$($hexValue)"
}
- Converts a decimal number to a hexadecimal string.
-
Read Value from Memory Address
function Get-Value-From-Address($address) {
$address = Dec-To-Hex -decimal ([uint64]$address)
$stdout = & $rwePath /Min /NoLogo /Stdout /Command="R32 $($address)" | Out-String
$splitString = $stdout -split " "
return [uint64]$splitString[-1]
}
- Reads a 32-bit value from a specified memory address using
Rw.exe.
-
Get Device Addresses
function Get-Device-Addresses() {
$data = @{}
$resources = Get-WmiObject -Class Win32_PNPAllocatedResource -ComputerName LocalHost -Namespace root\CIMV2
foreach ($resource in $resources) {
$deviceId = $resource.Dependent.Split("=")[1].Replace('"', '').Replace("\\", "\")
$physicalAddress = $resource.Antecedent.Split("=")[1].Replace('"', '')
if (-not $data.ContainsKey($deviceId) -and $deviceId -and $physicalAddress) {
$data[$deviceId] = [uint64]$physicalAddress
}
}
return $data
}
- Retrieves physical addresses of all devices in the system using WMI (Windows Management Instrumentation).
-
Check if Admin
function Is-Admin() {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
- Checks if the script is running with administrative privileges.
Main Function
function main() {
if (-not (Is-Admin)) {
Write-Host "error: administrator privileges required"
return 1
}
if (-not (Test-Path $rwePath -PathType Leaf)) {
Write-Host "error: $($rwePath) not exists, edit the script to change the path to Rw.exe"
Write-Host "http://rweverything.com/download"
return 1
}
Stop-Process -Name "Rw" -ErrorAction SilentlyContinue
$deviceMap = Get-Device-Addresses
foreach ($xhciController in Get-WmiObject Win32_USBController) {
$isDisabled = $xhciController.ConfigManagerErrorCode -eq 22
if ($isDisabled) {
continue
}
$deviceId = $xhciController.DeviceID
Write-Host "$($xhciController.Caption) - $($deviceId)"
if (-not $deviceMap.Contains($deviceId)) {
Write-Host "error: could not obtain base address`n"
continue
}
$desiredInterval = $globalInterval
$hcsparamsOffset = $globalHCSPARAMSOffset
$rtsoff = $globalRTSOFF
foreach ($hwid in $userDefinedData.Keys) {
if ($deviceId -match $hwid) {
$userDefinedController = $userDefinedData[$hwid]
if ($userDefinedController.ContainsKey("INTERVAL")) {
$desiredInterval = $userDefinedController["INTERVAL"]
}
if ($userDefinedController.ContainsKey("HCSPARAPS_OFFSET")) {
$hcsparamsOffset = $userDefinedController["HCSPARAPS_OFFSET"]
}
if ($userDefinedController.ContainsKey("RTSOFF")) {
$rtsoff = $userDefinedController["RTSOFF"]
}
}
}
$capabilityAddress = $deviceMap[$deviceId]
$HCSPARAMSValue = Get-Value-From-Address -address ($capabilityAddress + $hcsparamsOffset)
$HCSPARAMSBitmask = [Convert]::ToString($HCSPARAMSValue, 2)
$maxIntrs = [Convert]::ToInt32($HCSPARAMSBitmask.Substring($HCSPARAMSBitmask.Length - 16, 8), 2)
$RTSOFFValue = Get-Value-From-Address -address ($capabilityAddress + $rtsoff)
$runtimeAddress = $capabilityAddress + $RTSOFFValue
if ($verbose) {
$formattedCapabilityAddress = Dec-To-Hex -decimal $capabilityAddress
$formattedRTSOFFValue = Dec-To-Hex -decimal $RTSOFFValue
Write-Host "capability_address = $($formattedCapabilityAddress)"
Write-Host "HCSPARAMS_value = capability_address + hcsparams_offset = $($formattedCapabilityAddress) + $(Dec-To-Hex -decimal $hcsparamsOffset) = $(Dec-To-Hex -decimal $HCSPARAMSValue)"
Write-Host "HCSPARAMS_bitmask = $($HCSPARAMSBitmask)"
Write-Host "max_intrs = $($maxIntrs)"
Write-Host "RTSOFF_value = capability_address + rtsoff = $($formattedCapabilityAddress) + $(Dec-To-Hex -decimal $rtsoff) = $($formattedRTSOFFValue)"
Write-Host "runtime_address = capability_address + RTSOFF_value = $($formattedCapabilityAddress) + $($formattedRTSOFFValue) = $(Dec-To-Hex -decimal $runtimeAddress)"
}
for ($i = 0; $i -lt $maxIntrs; $i++) {
$interrupterAddress = Dec-To-Hex -decimal ([uint64]($runtimeAddress + 0x24 + (0x20 * $i)))
if ($verbose) {
Write-Host "`ninterrupter_address = runtime_address + 0x24 + (0x20 * index) = $(Dec-To-Hex -decimal $runtimeAddress) + 0x24 + (0x20 * $($i)) = $($interrupterAddress)"
Write-Host "Write DWORD = $(Dec-To-Hex -decimal $desiredInterval)"
}
& $rwePath /Min /NoLogo /Stdout /Command="W32 $($interrupterAddress) $($desiredInterval)" | Write-Host
}
Write-Host # new line
}
return 0
}
- Check Admin Privileges: Ensures the script is running with administrator rights.
- Check for
Rw.exe: Verifies the existence of the Rw.exe tool.
- Stop Preexisting
Rw Processes: Stops any preexisting Rw.exe processes to avoid conflicts.
- Get Device Addresses: Uses
Get-Device-Addresses to map device IDs to their base addresses.
- Process Each Controller: Iterates over all USB controllers, skips disabled ones, and applies global or user-specific settings for IMOD interval and other parameters.
- Read Parameters: Uses
Get-Value-From-Address to obtain parameter values from the controller memory.
- Write IMOD Interval: Writes the desired IMOD interval to each controller's memory location.
Execution
$_exitCode = main
Write-Host # new line
exit $_exitCode
- Calls the
main function and exits with its return code.
Summary
The script aims to configure XHCI controller settings by:
- Checking for administrative privileges.
- Using
Rw.exe to read and write from hardware memory.
- Applying global or per-controller settings for the IMOD interval and other parameters.
- Verbose logging if the
$verbose switch is used.