diff options
| author | xyzz <1065521+xyzz@users.noreply.github.com> | 2020-01-13 18:08:14 -0500 |
|---|---|---|
| committer | MechMerlin <30334081+mechmerlin@users.noreply.github.com> | 2020-01-13 15:08:14 -0800 |
| commit | 83be1aed7631efdeccf9fbcd5d702b3048f08fd7 (patch) | |
| tree | b22c45518550edfd690a19007bf31b23543c2f70 /tmk_core | |
| parent | c293d9049a53a9589152571e9322899155b5b623 (diff) | |
| download | qmk_firmware-83be1aed7631efdeccf9fbcd5d702b3048f08fd7.tar.gz qmk_firmware-83be1aed7631efdeccf9fbcd5d702b3048f08fd7.zip | |
chibios/usb_main: re-check USB status in send_keyboard after sleeping the thread (#7784)
* chibios/usb_main: re-check USB status in send_keyboard after sleeping the thread
* change send_keyboard to only have 1 exit point
Diffstat (limited to 'tmk_core')
| -rw-r--r-- | tmk_core/protocol/chibios/usb_main.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index a4f996b8d..8fbe877db 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c | |||
| @@ -620,10 +620,8 @@ uint8_t keyboard_leds(void) { return (uint8_t)(keyboard_led_stats & 0xFF); } | |||
| 620 | void send_keyboard(report_keyboard_t *report) { | 620 | void send_keyboard(report_keyboard_t *report) { |
| 621 | osalSysLock(); | 621 | osalSysLock(); |
| 622 | if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { | 622 | if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { |
| 623 | osalSysUnlock(); | 623 | goto unlock; |
| 624 | return; | ||
| 625 | } | 624 | } |
| 626 | osalSysUnlock(); | ||
| 627 | 625 | ||
| 628 | #ifdef NKRO_ENABLE | 626 | #ifdef NKRO_ENABLE |
| 629 | if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */ | 627 | if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */ |
| @@ -632,28 +630,35 @@ void send_keyboard(report_keyboard_t *report) { | |||
| 632 | * until *after* the packet has been transmitted. I think | 630 | * until *after* the packet has been transmitted. I think |
| 633 | * this is more efficient */ | 631 | * this is more efficient */ |
| 634 | /* busy wait, should be short and not very common */ | 632 | /* busy wait, should be short and not very common */ |
| 635 | osalSysLock(); | ||
| 636 | if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { | 633 | if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { |
| 637 | /* Need to either suspend, or loop and call unlock/lock during | 634 | /* Need to either suspend, or loop and call unlock/lock during |
| 638 | * every iteration - otherwise the system will remain locked, | 635 | * every iteration - otherwise the system will remain locked, |
| 639 | * no interrupts served, so USB not going through as well. | 636 | * no interrupts served, so USB not going through as well. |
| 640 | * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ | 637 | * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ |
| 641 | osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread); | 638 | osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread); |
| 639 | |||
| 640 | /* after osalThreadSuspendS returns USB status might have changed */ | ||
| 641 | if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { | ||
| 642 | goto unlock; | ||
| 643 | } | ||
| 642 | } | 644 | } |
| 643 | usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report)); | 645 | usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report)); |
| 644 | osalSysUnlock(); | ||
| 645 | } else | 646 | } else |
| 646 | #endif /* NKRO_ENABLE */ | 647 | #endif /* NKRO_ENABLE */ |
| 647 | { /* regular protocol */ | 648 | { /* regular protocol */ |
| 648 | /* need to wait until the previous packet has made it through */ | 649 | /* need to wait until the previous packet has made it through */ |
| 649 | /* busy wait, should be short and not very common */ | 650 | /* busy wait, should be short and not very common */ |
| 650 | osalSysLock(); | ||
| 651 | if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) { | 651 | if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) { |
| 652 | /* Need to either suspend, or loop and call unlock/lock during | 652 | /* Need to either suspend, or loop and call unlock/lock during |
| 653 | * every iteration - otherwise the system will remain locked, | 653 | * every iteration - otherwise the system will remain locked, |
| 654 | * no interrupts served, so USB not going through as well. | 654 | * no interrupts served, so USB not going through as well. |
| 655 | * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ | 655 | * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ |
| 656 | osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread); | 656 | osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread); |
| 657 | |||
| 658 | /* after osalThreadSuspendS returns USB status might have changed */ | ||
| 659 | if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { | ||
| 660 | goto unlock; | ||
| 661 | } | ||
| 657 | } | 662 | } |
| 658 | uint8_t *data, size; | 663 | uint8_t *data, size; |
| 659 | if (keyboard_protocol) { | 664 | if (keyboard_protocol) { |
| @@ -664,9 +669,11 @@ void send_keyboard(report_keyboard_t *report) { | |||
| 664 | size = 8; | 669 | size = 8; |
| 665 | } | 670 | } |
| 666 | usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size); | 671 | usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size); |
| 667 | osalSysUnlock(); | ||
| 668 | } | 672 | } |
| 669 | keyboard_report_sent = *report; | 673 | keyboard_report_sent = *report; |
| 674 | |||
| 675 | unlock: | ||
| 676 | osalSysUnlock(); | ||
| 670 | } | 677 | } |
| 671 | 678 | ||
| 672 | /* --------------------------------------------------------- | 679 | /* --------------------------------------------------------- |
