I have a USB game controller that works flawlessly during gameplay, but it keeps disconnecting and reconnecting every ~40 seconds whenever it is left idle. While it is mostly harmless in practice, it is annoying to see the kernel messages are flooded with messages about the game controller.
Many online resources, such as the Arch Wiki, suggest disabling USB autosuspend to fix this. Unfortunately, that didn’t solve the issue in my case. I suspect the controller expects to be continuously polled rather than entering a power-saving state, especially since it immediately tries to reconnect after dropping.
Since I primarily use this controller by passing it through to a VM, I needed a way to manually manage its connection state. Here is the workaround that eventually did the trick:
- Add the following rule to
udev. This ensures the controller is ignored by the host system upon connection.ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="xxxx", ATTR{idProduct}=="xxxx", PROGRAM="systemctl is-active --quiet my-gaming-vm.service", ATTR{authorized}="0"- Note the
PROGRAMcondition in the udev rule, it is needed because the VM might not grab the device fast enough (e.g. due to a slow boot), in which case the game controller will disconnect and reconnect. In this case we don’t want to write 0 toauthorized.
- Note the
- Write
1to<DEV_PATH>/authorizedright before booting the VM. This authorizes the controller so it can be successfully forwarded. - Write
0to<DEV_PATH>/authorizedafter the VM shuts down. This disconnects the driver once again.
How to Find DEV_PATH
In the steps above, <DEV_PATH> refers to the
device path of the USB controller. Normally, this can be conveniently
found by following the symbolic link in /dev/input/by-id.
However, because we are forcefully disconnecting the device by writing
to authorized, that standard method won’t work here.
One can find the <DEV_PATH> by traversing and
matching /sys/bus/usb/devices/*/{idVendor,idProduct}, but a
much cleaner approach is to create a custom symbolic link using
udev.
Add the following rule (note that there is no ACTION
condition):
SUBSYSTEM=="usb", ATTR{idVendor}=="xxxx", ATTR{idProduct}=="xxxx", SYMLINK+="my-controller"
With this rule in place, I can easily obtain the correct device path
as:
/sys$(udevadm info --query=path --name=/dev/my-controller)
Alternative Solutions (Untested)
- The xone driver handles device polling differently and may resolve the issue natively.
- Run
cat /dev/input/by-id/my-controller > /dev/nullas a background process to keep the device active. - Use
usbguardto manually allow/deny the device.
Comments