8000 shared-module/usb_hid: allow HID to wake sleeping host computer by dhalbert · Pull Request #8830 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

shared-module/usb_hid: allow HID to wake sleeping host computer #8830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2024

Conversation

dhalbert
Copy link
Collaborator
@dhalbert dhalbert commented Jan 24, 2024

Thanks very much to @meesokim for code to enable usb_hid to wake a sleeping host computer, and to @hathach for implementing this ability in TinyUSB in the past.

Tested successfully with MacroPad RP2040, Feather nRF52840, and Circuit Playground Express on Windows and macOS.

On Linux not all HID devices are automatically enabled to do wakeup. See @hathach's comment. See also https://askubuntu.com/questions/848698/wake-up-from-suspend-using-wireless-usb-keyboard-or-mouse-for-any-linux-distro/874701 for details on how to change these settings. I would have thought there was an easier way, and maybe there is for some Linux distributions.

For example on my Linux box, by default only my primary keyboard is enabled for wakeup. This is a nuisance and will mean that people using, say, a MacroPad on Linux will need to adjust these settings if they want the MacroPad to be able to initiate wakeup.

$  grep . /sys/bus/usb/devices/*/product
/sys/bus/usb/devices/1-12/product:HD Pro Webcam C920
/sys/bus/usb/devices/1-1/product:Freestyle Edge RGB Keyboard
/sys/bus/usb/devices/1-2.5/product:USB Billboard Device   
/sys/bus/usb/devices/1-2/product:USB2.0 Hub             
/sys/bus/usb/devices/1-3/product:Microsoft Basic Optical Mouse 
/sys/bus/usb/devices/1-6/product:Macropad RP2040
/sys/bus/usb/devices/2-2.1/product:USB 10/100/1000 LAN
/sys/bus/usb/devices/2-2/product:USB3.0 Hub             
/sys/bus/usb/devices/usb1/product:xHCI Host Controller
/sys/bus/usb/devices/usb2/product:xHCI Host Controller

$  grep . /sys/bus/usb/devices/*/power/wakeup
/sys/bus/usb/devices/1-10/power/wakeup:disabled
/sys/bus/usb/devices/1-1/power/wakeup:enabled
/sys/bus/usb/devices/1-2.5/power/wakeup:disabled
/sys/bus/usb/devices/1-2/power/wakeup:disabled
/sys/bus/usb/devices/1-3/power/wakeup:disabled
/sys/bus/usb/devices/1-6/power/wakeup:disabled
/sys/bus/usb/devices/1-8/power/wakeup:disabled
/sys/bus/usb/devices/2-2.1/power/wakeup:disabled
/sys/bus/usb/devices/2-2/power/wakeup:disabled
/sys/bus/usb/devices/usb1/power/wakeup:disabled
/sys/bus/usb/devices/usb2/power/wakeup:disabled

@dhalbert dhalbert requested review from tannewt and jepler January 24, 2024 16:18
@dhalbert
Copy link
Collaborator Author

A few ru builds don't fit anymore. @tannewt's upcoming error message shrinking should fix this, so we can ignore those failures.

Copy link
Member
@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment request. Thanks for doing this!

@dhalbert
Copy link
Collaborator Author

@tannewt I added documentation.

Copy link
Member
@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! You'll want to merge to get the file size reductions.

8000
@dhalbert dhalbert merged commit 370b011 into adafruit:main Jan 25, 2024
@dhalbert dhalbert deleted the hid-wakeup branch January 25, 2024 19:44
@bendavis78
Copy link
bendavis78 commented Jun 10, 2024

@dhalbert Is there any documentation on how to use this new wakeup feature in python?

@dhalbert
Copy link
Collaborator Author

@dhalbert Is there any documentation on how to use this new wakeup feature in python?

It should "just work". If the HID device sends a report (e.g. a key press) to the host, the host should wake up. This is how a keypress on your regular keyboard works.

@bendavis78
Copy link
bendavis78 commented Jun 11, 2024

I see, thanks! I tested on a Mac and a keypress does in fact wake it up. Side-note: I'm using a Pico W for this project.

My current problem is with waking a Linux host. I've tried the solution explained in the SO post:

$ lsusb | grep Adafruit
Bus 003 Device 018: ID 239a:8120 Adafruit Pico W
$ cat /etc/udev/rules.d/10-wakeup.rules
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="239a", ATTRS{idProduct}=="8120" RUN+="/bin/sh -c 'echo enabled > /sys/bus/usb/devices/usb3/power/wakeup'"
$ cat /sys/bus/usb/devices/usb3/power/wakeup
enabled

Unfortunately this doesn't seem to wake up the host. I've confirmed /sys/bus/usb/devices/usb3/power/wakeup is enabled each time before and after I test.

One interesting observation I've made is that when I do put the Linux host to sleep (suspend), the webserver on the Pico is much slower to respond than when I tested it on a Macbook that is in sleep mode. With the macbook it responds in less than a second and wakes up the computer. With the linux laptop it does eventually give a successful HTTP respond after a couple seconds, verifying the keypress was sent. However it doesn't seem to wake up the laptop. When the laptop is already fully awake, the response time is much quicker. I wonder if that has anything to do with it? Perhaps the pico has some kind of low power state that makes it work differently?

@bendavis78
Copy link

Update: It looks like it was just a matter of getting the devpath correct in the udev rule. I figured out a more rubust way to set the power/wakeup attribute (since the devpath can change between boots):

ACTION=="add", \
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="239a", ATTRS{idProduct}=="8120", \
ATTR{power/wakeup}="enabled", \
RUN+="/bin/sh -c 'echo set attributes for %p >> /var/log/wakeup-device.log'"

The idVendor and idProduct are what I found in lsusb. Not sure if that will be the same for everyone, but that part will be consistent between boots.

@dhalbert
Copy link
Collaborator Author

I see it's possible to use wildcards in udev rules: https://unix.stackexchange.com/questions/752425/writing-udev-rules-wildcards, so you could wildcard the idProduct if you need to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wakeup feature for usb_hid package How to enable keyboard to wake sleeping computer?
3 participants
0