Skip to content
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

[Core] Cleanups to USB suspend logic #21537

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

KarlK90
Copy link
Member

@KarlK90 KarlK90 commented Jul 15, 2023

Description

This is a followup on #19780.

TBA.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Keyboard (addition or update)
  • Keymap/layout/userspace (addition or update)
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@KarlK90 KarlK90 requested a review from a team July 15, 2023 16:00
@github-actions github-actions bot added the core label Jul 15, 2023
@KarlK90 KarlK90 requested review from sigprof and zvecr July 15, 2023 16:00
platforms/chibios/suspend.c Show resolved Hide resolved
tmk_core/protocol/chibios/usb_main.c Outdated Show resolved Hide resolved
tmk_core/protocol/chibios/chibios.c Show resolved Hide resolved
@@ -534,40 +530,35 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
}
qmkusbConfigureHookI(&drivers.array[i].driver);
}
osalSysUnlockFromISR();
if (last_suspend_state) {
usb_event_queue_enqueue(USB_EVENT_WAKEUP);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

One remaining problem here is that last_suspend_state is set in usb_event_queue_task() (and without any locking too). If usb_event_queue_task() does not get called in time (which may happen if someone sticks a large delay into some code), we may potentially get here with a stale value for last_suspend_state, and therefore lose the wakeup event in case the wakeup goes through the full reconfiguration. Although getting that case after the other changes in the PR would probably be harder than before (with the older code you could get this broken state for any resume that ends up reconfiguring devices, because USB_EVENT_SUSPEND was not processed in time).

The options here are:

  1. Move all handling of last_suspend_state into usb_event_cb() (set it on USB_EVENT_SUSPEND and USB_EVENT_WAKEUP, and also after generating the USB_EVENT_WAKEUP event in the USB_EVENT_CONFIGURED path).
  2. Move all handling of last_suspend_state into usb_event_queue_task() (if USB_EVENT_CONFIGURED comes after USB_EVENT_SUSPEND, but without USB_EVENT_WAKEUP, perform wakeup processing too).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for spotting! I did the first approach and moved it into the callback and hopefully didn't miss anything.

@KarlK90 KarlK90 force-pushed the maintenance/usb-suspend-logic branch 2 times, most recently from c963338 to 6916f10 Compare July 15, 2023 18:54
@KarlK90 KarlK90 requested a review from sigprof July 15, 2023 18:55
case USB_EVENT_UNCONFIGURED:
/* Falls into.*/
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe keep these comments for readability?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmn, I'm not convinced that it actually adds readability. Though that is ofc subjective.

@KarlK90 KarlK90 force-pushed the maintenance/usb-suspend-logic branch 3 times, most recently from f4f713a to 6a88a07 Compare July 27, 2023 18:17
@github-actions github-actions bot removed the keyboard label Jul 27, 2023
@drashna drashna requested a review from a team July 27, 2023 19:47
@zvecr
Copy link
Member

zvecr commented Jul 28, 2023

Having issues with the current code on macos, will update this comment with more context asap.

KarlK90 added 6 commits August 2, 2023 17:45
suspend_wakeup_init isn't called from an ISR context anymore, thus we
can use the regular clear_keyboard function in this context, which will
also send a report to the host to reset all keys which might still be
registered as held.
Otherwise a incoming USB_EVENT_SUSPEND event might not be processed
before falling into the sleeping loop.
The queue is written from an ISR context and read from the main thread
which can lead to race conditions if the main thread is pre-empted while
accessing the queue. Therefore all interactions are moved into critical
sections.
As this was previously accessed from both ISR and main thread, which can
lead to race conditions as well.
...at this point the USB driver is already suspended so the message will
only be printed after resuming the operation, which is kind of
pointless.
@KarlK90 KarlK90 force-pushed the maintenance/usb-suspend-logic branch from 6a88a07 to 9abba9d Compare August 2, 2023 15:45
Copy link

Thank you for your contribution!
This pull request has been automatically marked as stale because it has not had activity in the last 45 days. It will be closed in 30 days if no further activity occurs. Please feel free to give a status update now, or re-open when it's ready.
For maintainers: Please label with bug, awaiting review, breaking_change, in progress, or on hold to prevent the issue from being re-flagged.

@github-actions github-actions bot added the stale Issues or pull requests that have become inactive without resolution. label Nov 12, 2023
Copy link

Thank you for your contribution!
This pull request has been automatically closed because it has not had activity in the last 30 days. Please feel free to give a status update now, ping for review, or re-open when it's ready.
// [stale-action-closed]

@github-actions github-actions bot closed this Dec 13, 2023
@KarlK90 KarlK90 reopened this Dec 13, 2023
@KarlK90 KarlK90 added awaiting changes awaiting_pr Relies on another PR to be merged first in progress labels Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes awaiting_pr Relies on another PR to be merged first core in progress stale Issues or pull requests that have become inactive without resolution.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants