SHARE
TWEET

OculusUSBfix.ps1

a guest Dec 23rd, 2016 308 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 3.0
  2. #Requires -RunAsAdministrator
  3.  
  4. # This script will disable all USB power saving settings on the system, in particular those associated with the Oculus Rift or Fresco Logic controllers.
  5. # It should run fine on Windows 8, Windows 8.1 and Windows 10. Windows 7 may work if you update Powershell.
  6.  
  7. # To run:
  8. # Save the script as 'OculusUSBfix.ps1' somewhere. Open the file's properties and check 'Unblock' and click Apply.
  9. # Go to Start, Windows Powershell, right click and select 'Run as administrator'.
  10. # Type 'Set-ExecutionPolicy RemoteSigned' once to be able to run PowerShell scripts (Or 'Set-ExecutionPolicy Unrestricted' if you did not unblock the file.)
  11. # Go to the folder where you saved the script and run it.
  12.  
  13. # These are the USB hardware IDs that we want to disable the 'Allow the computer to turn off this device to save power' setting on.
  14. # If you want to disable another USB devices power management, go to device manager, the properties of the device, details tab, Device ID / instance path, and copy the 'VID_nnnn&PID_nnnn' (Vendor ID and Product ID) to the following list. VID_2833 is Oculus VR LLC.
  15. $DeviceIDs = (
  16. "VID_2833&PID_0211", # Oculus Rift Sensor
  17. "VID_2833&PID_0330", # Oculus Rift HMD
  18. "VID_2833&PID_0031", # Oculus Rift HMD
  19. "ROOT_HUB_FL30" # Fresco Logic xHCI (USB3) Root Hub
  20. )
  21.  
  22.  
  23. # The script starts here.
  24. Write-Output "Starting script to fix Oculus Rift related USB power problems, and tune Fresco Logic USB settings."
  25. Write-Output "This script will not check USB driver versions or fix any physical issues. If the problem persists, the following may help:"
  26. Write-Output "- Check if you are running known good USB controller drivers or update them to a newer version."
  27. Write-Output "- Mount the sensors right-side-up."
  28. Write-Output "- Do not connect more than 2 sensors to the same USB controller on USB3 ports. Either use an extra card with USB3 ports, use USB2 ports, or use a USB2 extension cable."
  29. Write-Output "- Test without USB and HDMI extension cables."
  30. Write-Output "- Temporarily disconnect unneeded USB devices (The dongle for the Xbox controller is known to be bad about sharing the USB bus)."
  31. Write-Output "- Close down any software that might be using the CPU in the background while you play. (Steam, Origin, Battle.net, Google Drive, Dropbox, etc.)."
  32.  
  33. # To track if we make changes anywhere.
  34. $ChangesMade = $False
  35. $IsRebootRequired=$False
  36.  
  37. # Initiate a CIM session to use in the following CIM commands.
  38. Try {
  39.     $CIM_Session = New-CimSession -ErrorAction Stop
  40. } catch {
  41.     Throw "Could not establish a CIM session. The script cannot run."
  42. }
  43.  
  44. # If the 'USB 3 Link Power Management' option is currently hidden.
  45. $RegPath = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\2a737441-1930-4402-8d77-b2bebba308a3\d4e98f31-5ffe-4ce1-be31-1b38b384c009"
  46. If ( (Get-ItemProperty -Path $RegPath).Attributes -ne 2 ) {
  47.     Write-Output "Unhiding the 'USB 3 Link Power Management' option in 'Power Options', 'Edit Plan settings', 'Change advanced power settings', 'USB Settings'."
  48.     Set-ItemProperty -Path $RegPath -Name "Attributes" -Value 2
  49.     # We made a change. Track this.
  50.     $ChangesMade = $True
  51. }
  52.  
  53. # Get the currently active power plan.
  54. $CurrentPowerPlan = $PowerPlans | Where-Object { $_.IsActive }
  55.  
  56. # Make a list of power plans in the system. We are mainly interested in the current power plan, the 'High Powerformance' power plan that the oculus changes to.
  57. If ( $CurrentPowerPlan.InstanceID -like "*{a1841308-3541-4fab-bc81-f71556f20b4a}" ) {
  58.     # If the current power plan is the "Power Saver" power plan we are going to change all the power plans.
  59.     $PowerPlans = Get-CimInstance -CimSession $Cim_Session -ClassName Win32_PowerPlan -Namespace "root\cimv2\power"
  60. } Else {
  61.     # If the current power plan is not the "Power Saver" power plan we can exclude it from our list.
  62.     $PowerPlans = Get-CimInstance -CimSession $Cim_Session -ClassName Win32_PowerPlan -Namespace "root\cimv2\power" | Where-Object { $_.InstanceID -notlike "*{a1841308-3541-4fab-bc81-f71556f20b4a}" }
  63. }
  64.  
  65. # For each power plan in our list of power plans.
  66. Foreach ( $PowerPlan in $PowerPlans ) {
  67.     # Get the currently configured 'USB selective suspend setting' under power options.
  68.     $FilterString = "%$(($PowerPlan.InstanceID).SubString(($PowerPlan.InstanceID).Length-38))%AC%{48e6b7a6-50f5-4782-a5d4-53bb8f07e226}"
  69.     $USBSuspendSetting = Get-CimInstance -CimSession $Cim_Session -ClassName Win32_PowerSettingDataIndex -Namespace "root\cimv2\power" -Filter "InstanceID like '$FilterString'"
  70.  
  71.     # If 'USB selective suspend setting' is currently enabled.
  72.     If ( $USBSuspendSetting.SettingIndexValue -ne 0 ) {
  73.         Write-Output "Changing 'Power Options', '$($PowerPlan.ElementName)', 'Change (current) plan settings', 'Change advanced power settings', 'USB settings', 'USB selective suspend setting' to Disabled."
  74.         $USBSuspendSetting  | Set-CimInstance -CimSession $Cim_Session -Property @{SettingIndexValue = 0}
  75.         # If this power plan is the currently active power plan.
  76.         If ( $PowerPlan.IsActive ) {
  77.             Write-Output "Reactivating power plan $($PowerPlan.ElementName) to ensure our change is applied right now."
  78.             $return = Invoke-CimMethod -CimSession $Cim_Session -InputObject $PowerPlan -MethodName Activate -ErrorAction SilentlyContinue
  79.             If ( !$return.ReturnValue ) {
  80.                 Throw "There was an error while applying the power policy change."
  81.             }
  82.         }
  83.         # We made a change. Track this.
  84.         $ChangesMade = $True
  85.     }
  86.  
  87.     # Get the currently configured 'USB 3 Link Power Mangement' under power options.
  88.     $FilterString = "%$(($PowerPlan.InstanceID).SubString(($PowerPlan.InstanceID).Length-38))%AC%{d4e98f31-5ffe-4ce1-be31-1b38b384c009}"
  89.     $USBLinkPowerManagementSetting = Get-CimInstance -CimSession $Cim_Session -ClassName Win32_PowerSettingDataIndex -Namespace "root\cimv2\power" -Filter "InstanceID like '$FilterString'"
  90.  
  91.     # If 'USB 3 Link Power Mangement' is not set to 'Off'
  92.     If ( $USBLinkPowerManagementSetting.SettingIndexValue -ne 0 ) {
  93.         Write-Output "Changing 'Power Options', '$($PowerPlan.ElementName)', 'Change (current) plan settings', 'Change advanced power settings', 'USB settings', 'USB 3 Link Power Mangement' to Off."
  94.         $USBLinkPowerManagementSetting  | Set-CimInstance -CimSession $Cim_Session -Property @{SettingIndexValue = 0}
  95.         # If this power plan is the currently active power plan.
  96.         If ( $PowerPlan.IsActive ) {
  97.             Write-Output "Reactivating power plan $($PowerPlan.ElementName) to ensure our change is applied right now."
  98.             $return = Invoke-CimMethod -CimSession $Cim_Session -InputObject $PowerPlan -MethodName Activate -ErrorAction SilentlyContinue
  99.             If ( !$return.ReturnValue ) {
  100.                 Throw "There was an error while applying the power policy change."
  101.             }
  102.         }
  103.         # We made a change. Track this.
  104.         $ChangesMade = $True
  105.     }
  106. }
  107.  
  108. # Get a list of all the USB devices in the system so we can find user friendly names.
  109. $USBDevices = Get-CimInstance -CimSession $CIM_Session -ClassName Win32_PnPEntity -Namespace "root\cimv2"
  110.  
  111. # Get a list of all devices with power management in the system.
  112. $PowerDevices = Get-CimInstance -CimSession $Cim_Session -ClassName "MSPower_DeviceEnable" -Namespace "root\wmi"
  113.  
  114. # For each device with power management in the system.
  115. Foreach ( $PowerDevice in $PowerDevices ) {
  116.     # Check against the entire list of DeviceIDs defined at the beginning of the script.
  117.     Foreach ( $DeviceID in $DeviceIDs ) {
  118.         $PowerDeviceUpperCase = $PowerDevice.InstanceName.ToUpper()
  119.         $DeviceIDUpperCase = $DeviceID.ToUpper()
  120.         # Find where the USB device is the power device.
  121.         If ( "$PowerDeviceUpperCase" -like "*$DeviceIDUpperCase*" ) {
  122.             # If 'Allow the computer to turn off this device to save power' is enabled.
  123.             If ( $PowerDevice.Enable ) {
  124.                 # Retrieve the name of the USB device so we can display a user friendly name.
  125.                 $USBDevice = $USBDevices | Where-Object { $_.DeviceID -eq ($PowerDevice.InstanceName -replace "_0$") }
  126.                 $USBDeviceName = $USBDevice.Name
  127.                 Write-Output "Disabling 'Allow the computer to turn off this device to save power', under 'Device Manager', '$USBDeviceName', 'Properties'."
  128.                 $PowerDevice | Set-CimInstance -CimSession $Cim_Session -Property @{Enable = $False}
  129.                 # We made a change. Track this.
  130.                 $ChangesMade = $True
  131.             }
  132.         }
  133.     }
  134. }
  135.  
  136. # If a driver for a Fresco Logic FL1xxx USB controller is installed.
  137. If ( Test-Path -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters ) {
  138.     # If the U1U2LowPower registry setting does not exist or is not set to disabled (0).
  139.     If ( (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters).U1U2LowPower -ne 0 ) {
  140.         Write-Output "Adding registry key 'HKLM\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters\U1U2LowPower' to disable low power states for Fresco Logic USB controllers."
  141.         Try {
  142.             New-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters -Name U1U2LowPower -PropertyType DWORD -Value 0 -Force | Out-Null
  143.         } catch {
  144.             Throw "Could not create registry key."
  145.         }
  146.         # We made a change. Track this.
  147.         $ChangesMade = $true
  148.         # A registry setting change will not be picked up by the driver until the computer is rebooted. Track this.
  149.         $IsRebootRequired = $true
  150.     }
  151.     # If the BulkInRingBuffers does not exist is not set to decimal 256.
  152.     If ( (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters).BulkInRingBuffers -ne 256 ) {
  153.         Write-Output "Adding registry key 'HKLM\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters\BulkInRingBuffers' to increase buffers for Fresco Logic USB controllers."
  154.         Try {
  155.         New-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FLxHCIc\Parameters -Name BulkInRingBuffers -PropertyType DWORD -Value 256 -Force | Out-Null
  156.             } catch {
  157.             Throw "Could not create registry key."
  158.         }
  159.         # We made a change. Track this.
  160.         $ChangesMade = $true
  161.         # A registry setting change will not be picked up by the driver until the computer is rebooted. Track this.
  162.         $IsRebootRequired = $true
  163.     }
  164. }
  165.  
  166. # If we made any change.
  167. If ( $ChangesMade ) {    
  168.     If ( $IsRebootRequired ) {
  169.         Write-Output "Changes were applied. Due to changes in the registry for the Fresco Logic USB controller driver a REBOOT IS REQUIRED before the changes take effect."
  170.     } Else {        
  171.         Try {
  172.             Write-Output "Restarting the Oculus VR Runtime Service to ensure it works okay."
  173.             # We restart the Oculus VR Runtime only if a reboot of the computer isn't required.
  174.             Get-Service -Name "OVRService" | Restart-Service -ErrorAction Stop
  175.         } catch {
  176.             Throw "Could not restart the Oculus VR Runtime Service."
  177.         }
  178.         Write-Output "Changes were applied. No reboot is required."
  179.     }
  180. } Else {
  181.     Write-Output "No changes were needed."
  182. }
  183.  
  184. # Close the CIM session.
  185. Remove-CimSession -CimSession $CIM_Session -ErrorAction SilentlyContinue
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top