Skip to content

Commit 370b011

Browse files
authored
Merge pull request #8830 from dhalbert/hid-wakeup
shared-module/usb_hid: allow HID to wake sleeping host computer
2 parents a54f5c2 + 62da707 commit 370b011

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

shared-bindings/usb_hid/Device.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
//| in_report_lengths=(5, 2),
7373
//| out_report_lengths=(6, 0),
7474
//| )
75+
//|
76+
//| The HID device is able to wake up a suspended (sleeping) host computer.
77+
//| See `send_report()` for details.
7578
//| """
7679
//| ...
7780
//| KEYBOARD: Device
@@ -166,6 +169,16 @@ STATIC mp_obj_t usb_hid_device_make_new(const mp_obj_type_t *type, size_t n_args
166169
//| """Send an HID report. If the device descriptor specifies zero or one report id's,
167170
//| you can supply `None` (the default) as the value of ``report_id``.
168171
//| Otherwise you must specify which report id to use when sending the report.
172+
//|
173+
//| If the USB host is suspended (sleeping), then `send_report()` will request that the host wake up.
174+
//| The ``report`` itself will be discarded, to prevent unwanted extraneous characters,
175+
//| mouse clicks, etc.
176+
//|
177+
//| Note: Host operating systems allow enabling and disabling specific devices
178+
//| and kinds of devices to do wakeup.
179+
//| The defaults are different for different operating systems.
180+
//| For instance, on Linux, only the primary keyboard may be enabled.
181+
//| In addition, there may be USB wakeup settings in the host computer BIOS/UEFI.
169182
//| """
170183
//| ...
171184
STATIC mp_obj_t usb_hid_device_send_report(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {

shared-module/usb_hid/Device.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,16 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t *
229229
RUN_BACKGROUND_TASKS;
230230
}
231231

232-
if (!tud_hid_ready()) {
233-
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("USB busy"));
234-
}
232+
if (!tud_suspended()) {
233+
if (!tud_hid_ready()) {
234+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("USB busy"));
235+
}
235236

236-
if (!tud_hid_report(report_id, report, len)) {
237-
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("USB error"));
237+
if (!tud_hid_report(report_id, report, len)) {
238+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("USB error"));
239+
}
240+
} else {
241+
tud_remote_wakeup();
238242
}
239243
}
240244

supervisor/shared/usb/usb_desc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static const uint8_t configuration_descriptor_template[] = {
105105
#define CONFIG_NUM_INTERFACES_INDEX (4)
106106
0x01, // 5 bConfigurationValue
107107
0x00, // 6 iConfiguration (String Index)
108-
0x80, // 7 bmAttributes
108+
0x80 | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, // 7 bmAttributes
109109
0x32, // 8 bMaxPower 100mA
110110
};
111111

0 commit comments

Comments
 (0)