adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.02k stars 1.19k forks source link

How to enable keyboard to wake sleeping computer? #5380

Closed LostVector closed 7 months ago

LostVector commented 2 years ago

Hi,

I'm trying to figure out how to make an emulated USB keyboard (using a neokey 1x4) wake the computer it's attached to. I've verified the QT PY RP2040 is getting power and behaves normally when the computer is on, but it seems to go haywire when the pc is asleep and i press a key (windows 10).

Is this possible to support somehow with the current libraries?

dhalbert commented 2 years ago

Transferring to https://github.com/adafruit/circuitpython, as this is a core issue.

dhalbert commented 2 years ago

@hathach What would we need to do to make USB wake work? I assume we are not doing USB at all when we detect that the host has gone away (disconnect or suspend, I guess, I'm not sure which). Thanks.

jpconstantineau commented 2 years ago

Just a pointer to what perhaps does it... It's arduino code example on Tinyusb: https://github.com/adafruit/Adafruit_TinyUSB_Arduino/blob/master/examples/HID/hid_composite/hid_composite.ino#L87

@hathach to confirm if that's the API to implement for the wake up.

hathach commented 2 years ago

@dhalbert tud_remote_wakeup() is API to do this, however there is a few requirements, all is implemented by hid example for your reference https://github.com/hathach/tinyusb/tree/master/examples/device/hid_composite

  1. TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP bit must be set in the configuration descriptor https://github.com/hathach/tinyusb/blob/master/examples/device/hid_composite/src/usb_descriptors.c#L111
  2. MCU DCD port remote wakeup and suspend detection must be implemented, which is true for all/most of the port used by circuitpython thanks to recent effort to get them passed USBCV
  3. Depending on host OS, before suspending it will send control request to allow device to perform remote wakeup. In my experience, Windows will always allow HID device to perform remote wakeup, but Linux is a bit trickier. Linux may need explicitly enable, can check which device support remote wakeup with
    grep . /sys/bus/usb/devices/*/product
    grep . /sys/bus/usb/devices/*/power/wakeup
dhalbert commented 2 years ago

Bumped priority to do https://forums.adafruit.com/viewtopic.php?f=60&t=187399 and perhaps some other requests I have forgotten.

dhalbert commented 2 years ago
  1. Check descriptor power settings and remote wake
  2. Before sending a report, see if we need to wake.
maxammann commented 10 months ago

Super interesting to discover the lower level USB stuff. I have a Fomu FPGA running CircuitPython. I was able to wake up my screen, but not yet wakeup my device from sleep and I guess it is just not yet implemented.

Is this task just about sending a wakeup message before sending a key press or is it more involved to implement this?

dhalbert commented 10 months ago

Is this task just about sending a wakeup message before sending a key press or is it more involved to implement this?

I think we just need to do what is described in https://github.com/adafruit/circuitpython/issues/5380#issuecomment-925044500