From 2d7cd6845937c6dc0ce5bb61d07b394868227549 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 22 Jul 2018 13:13:29 -0700 Subject: [PATCH] Make some improvements to the sleep/wake code --- right/src/usb_composite_device.c | 41 +++++++++++++++++++------------- right/src/usb_composite_device.h | 4 ++-- right/src/usb_report_updater.c | 8 ++----- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/right/src/usb_composite_device.c b/right/src/usb_composite_device.c index 390d370..acaa2de 100644 --- a/right/src/usb_composite_device.c +++ b/right/src/usb_composite_device.c @@ -163,23 +163,30 @@ static usb_device_class_config_list_struct_t UsbDeviceCompositeConfigList = { } }}; -bool IsHostSleeping = false; +volatile bool SleepModeActive = true; +static volatile bool wakeUpHostAllowed; -static void suspendHost(void) { - IsHostSleeping = true; +static void suspendUhk(void) { + SleepModeActive = true; LedSlaveDriver_DisableLeds(); } -void WakeUpHost(bool sendResume) { - if (sendResume) { // The device should wake up the host. - // Send resume signal - this will call USB_DeviceKhciControl(khciHandle, kUSB_DeviceControlResume, NULL); - USB_DeviceSetStatus(UsbCompositeDevice.deviceHandle, kUSB_DeviceStatusBus, NULL); - } - - IsHostSleeping = false; +static void wakeUpUhk(void) { + SleepModeActive = false; LedSlaveDriver_UpdateLeds(); } +void WakeUpHost(void) { + if (!wakeUpHostAllowed) { + return; + } + // Send resume signal - this will call USB_DeviceKhciControl(khciHandle, kUSB_DeviceControlResume, NULL); + USB_DeviceSetStatus(UsbCompositeDevice.deviceHandle, kUSB_DeviceStatusBus, NULL); + while (SleepModeActive) { + ; + } +} + static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, void *param) { usb_status_t status = kStatus_USB_Error; @@ -190,10 +197,6 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, return status; } - if (IsHostSleeping) { - WakeUpHost(false); // Wake up the keyboard if there is any activity on the bus. - } - switch (event) { case kUSB_DeviceEventBusReset: UsbCompositeDevice.attach = 0; @@ -201,17 +204,17 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, break; case kUSB_DeviceEventSuspend: if (UsbCompositeDevice.attach) { - suspendHost(); // The host sends this event when it goes to sleep, so turn off all the LEDs. + suspendUhk(); // The host sends this event when it goes to sleep, so turn off all the LEDs. status = kStatus_USB_Success; } break; case kUSB_DeviceEventResume: - // We will just wake up the host if there is any activity on the bus. - // The problem is that the host won't send a resume event when it boots, so the lights will never come back on. + wakeUpUhk(); status = kStatus_USB_Success; break; case kUSB_DeviceEventSetConfiguration: UsbCompositeDevice.attach = 1; + wakeUpUhk(); UsbCompositeDevice.currentConfiguration = *temp8; UsbGenericHidSetConfiguration(UsbCompositeDevice.genericHidHandle, *temp8); UsbBasicKeyboardSetConfiguration(UsbCompositeDevice.basicKeyboardHandle, *temp8); @@ -266,6 +269,10 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, case kUSB_DeviceEventGetHidPhysicalDescriptor: status = USB_DeviceGetHidPhysicalDescriptor(handle, (usb_device_get_hid_physical_descriptor_struct_t *)param); break; + case kUSB_DeviceEventSetRemoteWakeup: + wakeUpHostAllowed = *temp8; + status = kStatus_USB_Success; + break; } return status; diff --git a/right/src/usb_composite_device.h b/right/src/usb_composite_device.h index 7c35485..3367651 100644 --- a/right/src/usb_composite_device.h +++ b/right/src/usb_composite_device.h @@ -28,12 +28,12 @@ // Variables: - extern bool IsHostSleeping; + extern volatile bool SleepModeActive; extern usb_composite_device_t UsbCompositeDevice; //Functions: void InitUsb(void); - void WakeUpHost(bool sendResume); + void WakeUpHost(void); #endif diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index f317792..5ca0a1a 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -426,11 +426,11 @@ void UpdateUsbReports(void) KeyStates[SlotId_RightKeyboardHalf][keyId].current = RightKeyMatrix.keyStates[keyId]; } - if (IsHostSleeping) { + if (SleepModeActive) { for (uint8_t slotId = 0; slotId < SLOT_COUNT; slotId++) { for (uint8_t keyId = 0; keyId < MAX_KEY_COUNT_PER_MODULE; keyId++) { if (KeyStates[slotId][keyId].current) { - WakeUpHost(true); + WakeUpHost(); return; } } @@ -456,10 +456,6 @@ void UpdateUsbReports(void) bool HasUsbSystemKeyboardReportChanged = memcmp(ActiveUsbSystemKeyboardReport, GetInactiveUsbSystemKeyboardReport(), sizeof(usb_system_keyboard_report_t)) != 0; bool HasUsbMouseReportChanged = memcmp(ActiveUsbMouseReport, GetInactiveUsbMouseReport(), sizeof(usb_mouse_report_t)) != 0; - if (IsHostSleeping && (previousLayer != LayerId_Base || HasUsbBasicKeyboardReportChanged || HasUsbMediaKeyboardReportChanged || HasUsbSystemKeyboardReportChanged || HasUsbMouseReportChanged)) { - WakeUpHost(true); // Wake up the host if any key is pressed and the computer is sleeping. - } - if (HasUsbBasicKeyboardReportChanged) { usb_status_t status = UsbBasicKeyboardAction(); if (status == kStatus_USB_Success) {