SHARE
TWEET

OculusUSBfix.ps1

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