[PATCH v3 10/10] HID: usbhid: Set USB mice as s2idle wakeup resources
From: Mario Limonciello
Date: Thu Jun 30 2022 - 22:34:10 EST
The USB HID transport layer doesn't configure mice for wakeup by default.
Thus users can not wake system from s2idle via USB mouse. However, users
can wake the same system from Modern Standby on Windows with the same USB
mouse.
Microsoft documentation indicates that all USB mice and touchpads should
be waking the system from Modern Standby.
Many people who have used Windows on a PC that supports Modern Standby
have an expectation that s2idle wakeup sources should behave the same in
Linux. For example if your PC is configured "dual-boot" and is used docked
it's very common to wakeup by using a USB mouse connected to your dock in
Windows. Switching to Linux this is not enabled by default and you'll
need to manually turn it on or use a different wakeup source than you did
for Windows.
Changes for wakeups have been made in other subsystems such as the PS/2
keyboard driver which align how wakeup sources in Linux and Modern Standby
in Windows behave. To align expectations from users on USB mice, make this
behavior the same when the system is configured both by the OEM and the
user to use s2idle in Linux.
This means that at a minimum supported mice will be able to wakeup by
clicking a button. If the USB mouse is powered over the s2idle cycle (such
as a wireless mouse with a battery) it's also possible that moving it
may wake up the system. This is HW dependent behavior.
If the user sets the system to use S3 instead of s2idle, or the OEM ships
the system defaulting to S3, this behavior will not be turned on by
default.
Users who have a modern laptop that supports s2idle and use s2idle but
prefer the previous Linux kernel behavior can turn this off via a udev
rule.
Link: https://docs.microsoft.com/en-us/windows-hardware/design/device-experiences/modern-standby-wake-sources#input-devices-1
Link: https://lore.kernel.org/linux-usb/20220404214557.3329796-1-richard.gong@xxxxxxx/
Suggested-by: Richard Gong <richard.gong@xxxxxxx>
Signed-off-by: Mario Limonciello <mario.limonciello@xxxxxxx>
---
More people keep coming to us confused that they couldn't wake a Linux system
up from sleep using a mouse, so this patch is being revived.
Microsoft documentation doesn't indicate any allowlist for this behavior, and
they actually prescribe it for all USB mice and touchpads.
changes from v2->v3:
* Use `pm_suspend_preferred_s2idle`
* Drop now unnecessary acpi.h header inclusion
* Update commit message
* Adjust comments from v2 per thread
changes from v1->v2:
* Resubmit by Mario
* Update commit message
* Only activate on systems configured by user and OEM for using s2idle
---
drivers/hid/usbhid/hid-core.c | 37 ++++++++++++++++++++++++-----------
1 file changed, 26 insertions(+), 11 deletions(-)
diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c
index 4490e2f7252a..d08511f00d3b 100644
--- a/drivers/hid/usbhid/hid-core.c
+++ b/drivers/hid/usbhid/hid-core.c
@@ -26,6 +26,7 @@
#include <linux/wait.h>
#include <linux/workqueue.h>
#include <linux/string.h>
+#include <linux/suspend.h>
#include <linux/usb.h>
@@ -1176,17 +1177,31 @@ static int usbhid_start(struct hid_device *hid)
usb_autopm_put_interface(usbhid->intf);
}
- /* Some keyboards don't work until their LEDs have been set.
- * Since BIOSes do set the LEDs, it must be safe for any device
- * that supports the keyboard boot protocol.
- * In addition, enable remote wakeup by default for all keyboard
- * devices supporting the boot protocol.
- */
- if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
- interface->desc.bInterfaceProtocol ==
- USB_INTERFACE_PROTOCOL_KEYBOARD) {
- usbhid_set_leds(hid);
- device_set_wakeup_enable(&dev->dev, 1);
+ if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
+ switch (interface->desc.bInterfaceProtocol) {
+ /* Some keyboards don't work until their LEDs have been set.
+ * Since BIOSes do set the LEDs, it must be safe for any device
+ * that supports the keyboard boot protocol.
+ * In addition, enable remote wakeup by default for all keyboard
+ * devices supporting the boot protocol.
+ */
+ case USB_INTERFACE_PROTOCOL_KEYBOARD:
+ usbhid_set_leds(hid);
+ device_set_wakeup_enable(&dev->dev, 1);
+ break;
+ /*
+ * Windows configures USB mice to be a wakeup source from Modern
+ * Standby, and users have expectations that s2idle wakeup sources
+ * behave the same. Thus setup remote wakeup by default for mice
+ * supporting boot protocol if the system supports s2idle and the user
+ * has not disabled it on the kernel command line.
+ */
+ case USB_INTERFACE_PROTOCOL_MOUSE:
+ if (pm_suspend_preferred_s2idle() &&
+ pm_suspend_default_s2idle())
+ device_set_wakeup_enable(&dev->dev, 1);
+ break;
+ }
}
mutex_unlock(&usbhid->mutex);
--
2.34.1