summaryrefslogtreecommitdiff
path: root/tmk_core/protocol/chibios/usb_driver.c
diff options
context:
space:
mode:
authorPurdea Andrei <andrei@purdea.ro>2021-04-25 06:11:41 +0300
committerGitHub <noreply@github.com>2021-04-25 13:11:41 +1000
commitdbd65d01b656e0e43511da4b144dc3408f3046d1 (patch)
tree1c78777b28858779498a9390c86dff204b91ae57 /tmk_core/protocol/chibios/usb_driver.c
parentc7ca67a036e9e4cd87ff203cf3c3d1922cb79e6d (diff)
Fix how USB queue overflow is handled in chibios. (#12576)
* Fix how USB queue overflow is handled in chibios. This commit reverts PR 12472 (commit c823fe2d3f23ed090e36ce39beed4c448298bd2f), and it implements the original intent of the commit in a better way. The original intent of the above mentioned commit was to not deadlock the keyboard when console is enabled, and hid_listen is not started. The above mentioned commit had a few drawbacks: 1) When a lot of data was printed to the console, the queue would get full, and drop data, even if hid_listen was running. (For example having matrix debug enabled just didn't work right at all) 2) I believe the function in which this was implemented is used by all other USB endpoints, so with the above change, overflow, and data loss could happen in other important functions of QMK as well. This commit implements deadlock prevention in a slightly similar way to how it's done on AVR. There is an additional static local variable, that memorizes whether the console has timeouted before. If we are in the timeouted=false state, then we send the character normally with a 5ms timeout. If it does time out, then hid_listen is likely not running, and future characters should not be sent with a timeout, but those characters should still be sent if there is space in the queue. The difference between the AVR implementation and this one is that the AVR implementation checks the queue state directly, but this implementation instead attempts to write the character with a zero timeout. If it fails, then we remain in the timeouted=true state, if it succeeds, then hid_listen started removing data from the queue, so we can go out of the timeouted=true state. * Added comment explaining the timeouted logic to console flow control. * Console flow control: refactor chibios flowcontrol code to make it more readable, and rename the timeouted variable to timed_out on both chibios and lufa. Changed comments to says timed_out is an approximation of listener_disconnected, to make it clear that it's not the same thing * fix typo
Diffstat (limited to 'tmk_core/protocol/chibios/usb_driver.c')
-rw-r--r--tmk_core/protocol/chibios/usb_driver.c14
1 files changed, 1 insertions, 13 deletions
diff --git a/tmk_core/protocol/chibios/usb_driver.c b/tmk_core/protocol/chibios/usb_driver.c
index 40bfb8eb2b..cc0ce7600f 100644
--- a/tmk_core/protocol/chibios/usb_driver.c
+++ b/tmk_core/protocol/chibios/usb_driver.c
@@ -80,19 +80,7 @@ static bool qmkusb_start_receive(QMKUSBDriver *qmkusbp) {
* Interface implementation.
*/
-static size_t _write(void *ip, const uint8_t *bp, size_t n) {
- output_buffers_queue_t *obqueue = &((QMKUSBDriver *)ip)->obqueue;
- chSysLock();
- const bool full = obqIsFullI(obqueue);
- chSysUnlock();
- if (full || bqIsSuspendedX(obqueue)) {
- /* Discard any writes while the queue is suspended or full, i.e. the hidraw
- interface is not open. If we tried to send with an infinite timeout, we
- would deadlock the keyboard otherwise. */
- return -1;
- }
- return obqWriteTimeout(obqueue, bp, n, TIME_INFINITE);
-}
+static size_t _write(void *ip, const uint8_t *bp, size_t n) { return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, n, TIME_INFINITE); }
static size_t _read(void *ip, uint8_t *bp, size_t n) { return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, n, TIME_INFINITE); }