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
This commit is contained in:
xyzz 2020-01-13 18:08:14 -05:00 committed by MechMerlin
parent c293d9049a
commit 83be1aed76

View File

@ -620,10 +620,8 @@ uint8_t keyboard_leds(void) { return (uint8_t)(keyboard_led_stats & 0xFF); }
void send_keyboard(report_keyboard_t *report) { void send_keyboard(report_keyboard_t *report) {
osalSysLock(); osalSysLock();
if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
osalSysUnlock(); goto unlock;
return;
} }
osalSysUnlock();
#ifdef NKRO_ENABLE #ifdef NKRO_ENABLE
if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */ if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */
@ -632,28 +630,35 @@ void send_keyboard(report_keyboard_t *report) {
* until *after* the packet has been transmitted. I think * until *after* the packet has been transmitted. I think
* this is more efficient */ * this is more efficient */
/* busy wait, should be short and not very common */ /* busy wait, should be short and not very common */
osalSysLock();
if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) {
/* Need to either suspend, or loop and call unlock/lock during /* Need to either suspend, or loop and call unlock/lock during
* every iteration - otherwise the system will remain locked, * every iteration - otherwise the system will remain locked,
* no interrupts served, so USB not going through as well. * no interrupts served, so USB not going through as well.
* Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread); osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread);
/* after osalThreadSuspendS returns USB status might have changed */
if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
goto unlock;
}
} }
usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report)); usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report));
osalSysUnlock();
} else } else
#endif /* NKRO_ENABLE */ #endif /* NKRO_ENABLE */
{ /* regular protocol */ { /* regular protocol */
/* need to wait until the previous packet has made it through */ /* need to wait until the previous packet has made it through */
/* busy wait, should be short and not very common */ /* busy wait, should be short and not very common */
osalSysLock();
if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) { if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) {
/* Need to either suspend, or loop and call unlock/lock during /* Need to either suspend, or loop and call unlock/lock during
* every iteration - otherwise the system will remain locked, * every iteration - otherwise the system will remain locked,
* no interrupts served, so USB not going through as well. * no interrupts served, so USB not going through as well.
* Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread); osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread);
/* after osalThreadSuspendS returns USB status might have changed */
if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
goto unlock;
}
} }
uint8_t *data, size; uint8_t *data, size;
if (keyboard_protocol) { if (keyboard_protocol) {
@ -664,9 +669,11 @@ void send_keyboard(report_keyboard_t *report) {
size = 8; size = 8;
} }
usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size); usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size);
osalSysUnlock();
} }
keyboard_report_sent = *report; keyboard_report_sent = *report;
unlock:
osalSysUnlock();
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------